]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - libutil/pw_util.c
libutil: move group_line_format into the scop of its only user.
[pw-darwin.git] / libutil / pw_util.c
index 63c63de81aa68c755619ef69fc06b79fe9c8895f..befd1fb02d701ee9f8bf67f83077073139b36dc2 100644 (file)
@@ -179,11 +179,8 @@ pw_lock(void)
        for (;;) {
                struct stat st;
 
-               lockfd = open(masterpasswd, O_RDONLY, 0);
-               if (lockfd < 0 || fcntl(lockfd, F_SETFD, 1) == -1)
-                       err(1, "%s", masterpasswd);
-               /* XXX vulnerable to race conditions */
-               if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
+               lockfd = flopen(masterpasswd, O_RDONLY|O_NONBLOCK|O_CLOEXEC, 0);
+               if (lockfd == -1) {
                        if (errno == EWOULDBLOCK) {
                                errx(1, "the password db file is busy");
                        } else {
@@ -437,14 +434,21 @@ pw_copy(int ffd, int tfd, const struct passwd *pw, struct passwd *old_pw)
        size_t len;
        int eof, readlen;
 
-       spw = pw;
+       if (old_pw == NULL && pw == NULL)
+                       return (-1);
+
+       spw = old_pw;
+       /* deleting a user */
        if (pw == NULL) {
                line = NULL;
-               if (old_pw == NULL)
+       } else {
+               if ((line = pw_make(pw)) == NULL)
                        return (-1);
-               spw = old_pw;
-       } else if ((line = pw_make(pw)) == NULL)
-               return (-1);
+       }
+
+       /* adding a user */
+       if (spw == NULL)
+               spw = pw;
 
        eof = 0;
        len = 0;
@@ -511,7 +515,7 @@ pw_copy(int ffd, int tfd, const struct passwd *pw, struct passwd *old_pw)
                 */
 
                *q = t;
-               if (fpw == NULL || fpw->pw_uid != spw->pw_uid) {
+               if (fpw == NULL || strcmp(fpw->pw_name, spw->pw_name) != 0) {
                        /* nope */
                        if (fpw != NULL)
                                free(fpw);