]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - libutil/pw_util.c
Don't forget to '\n'-terminate new entries. This unbreaks chpass -a.
[pw-darwin.git] / libutil / pw_util.c
index e28f3c956b7d6c3c53a13801e52551f18d453ad2..d2199176c15d954c2f99b3a4b9faf8aa4731b0d9 100644 (file)
@@ -185,16 +185,21 @@ pw_lock(void)
                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))
-                       errx(1, "the password db file is busy");
+               if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
+                       if (errno == EWOULDBLOCK) {
+                               errx(1, "the password db file is busy");
+                       } else {
+                               err(1, "could not lock the passwd file: ");
+                       }
+               }
 
                /*
                 * If the password file was replaced while we were trying to
                 * get the lock, our hardlink count will be 0 and we have to
                 * close and retry.
                 */
-               if (fstat(lockfd, &st) < 0)
-                       errx(1, "fstat() failed");
+               if (fstat(lockfd, &st) == -1)
+                       err(1, "fstat() failed: ");
                if (st.st_nlink != 0)
                        break;
                close(lockfd);
@@ -458,7 +463,8 @@ pw_copy(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw)
                *q = '\0';
                fpw = pw_scan(r, PWSCAN_MASTER);
                *q = t;
-               if (old_pw == NULL || !pw_equal(pw, old_pw)) {
+               if ((old_pw && !pw_equal(fpw, old_pw)) ||
+                   (!old_pw && strcmp(fpw->pw_name, pw->pw_name))) {
                        /* nope */
                        free(fpw);
                        if (write(tfd, p, q - p + 1) != q - p + 1)
@@ -490,7 +496,8 @@ pw_copy(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw)
 
        /* if we got here, we have a new entry */
        len = strlen(line);
-       if (write(tfd, line, len) != len)
+       if (write(tfd, line, len) != len ||
+           write(tfd, "\n", 1) != 1)
                goto err;
  done:
        free(line);