SUMMARY: putpwent() Question

From: <haymanR_at_icefog.uacn.alaska.edu>
Date: Fri, 10 Mar 1995 07:33:16 -0900

My original post:

------- Forwarded Message
Greetings -

While this may not be specific to Alpha OSF/1, it may be due to our running
Enhanced Security also, and there may be some interrelations I don't know
about...

I have a routine which is intending to modify information in /etc/passwd for
an account. The problem is that after I call putpwent, I end up with two
entries for the account in question (only in /etc/passwd). Below is a code
fragment. What am I missing? BTW, I have no problems with the tcb database
modifications.

thanks. Summary pending responses.

Randy M. Hayman
haymanr_at_icefog.alaska.edu


...
#define PWD_FILE "/etc/passwd"
FILE *pw_fil;

...
        pw_fil = fopen(PWD_FILE, "r+");
        setpwent();
...

/*--------------------username exist?-----------------------------------*/
        if (NULL == (upr = getpwnam(name)) ) /* /etc/passwd */
        {
                fprintf(stderr, "Error getting entry for %s. User likely doesn't
exist (passwd).\n", name);
                exit(1);
        }

        if (NULL == (pr = getprpwnam(name)) ) /* tcb database */
        {
                fprintf(stderr, "Error getting entry for %s. User likely doesn't
exist (tcb).\n", name);
                exit(1);
        }

...
/*--------------------get user data and change it-----------------------*/

        upr = getpwnam(name); /* /etc/passwd structure */
        pr = getprpwnam(name); /* tcb database structure */

        strcpy(old_home, upr->pw_dir);
        old_uid = upr->pw_uid;
        if (old_uid != pr->ufld.fd_uid)
        {
                fprintf(stderr, "/etc/passwd uid field %u does not match tcb uid
field %u\n", old_uid, pr->ufld.fd_uid);
                exit(1);
        }

...
/*--------------------write it out now----------------------------------*/
/*--------------------to both /etc/passwd and tcb database--------------*/

        if (0 == putprpwnam(name, pr))
        {
                fprintf(stdout, "\n\t***Failure to Update Account (tcb)***");
                exit(1);
        }
        else
        {
                fprintf(stdout, "\ntcb Account: %s Old UID: %u New UID:
%u\n", name, old_uid, new_uid);
        }


        if (NULL != putpwent(upr, pw_fil))
        {
                fprintf(stdout, "\n\t***Failure to Update Account
(passwd)***\n");
                exit(1);
        }
        else
        {
                endpwent();
                fprintf(stdout, "\npasswd Account: %s Old UID: %u New UID:
%u \n\tOld Home: %s New Home: %s\n",
                        name, old_uid, new_uid, old_home, new_home);
        }

...

------- End of Forwarded Message

I received but a single response from Knut.Hellebo_at_nho.hydro.com, inquiring
if the hashed passwd databases are fooling me. No, unfortunately not.


The work-around (haven't yet determined if putpwent() is doing what it is
designed to) which I now use is as follows:

while getpwent from /etc/passwd !NULL
  if (user I'm looking for)
    modify
  putpwent to tmp_passwd

mv tmp_passwd /etc/passwd
mkpasswd


Randy M. Hayman
haymanr_at_icefog.alaska.edu
Received on Fri Mar 10 1995 - 11:32:18 NZDT

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:45 NZDT