]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - libutil/pw_util.c
Recommit everything, add chpass, improve history (except for a few files that git...
[pw-darwin.git] / libutil / pw_util.c
index d7903281d7cc96433fa5ad9fdf789511f9141502..ad2b36ab765df472687f9f6210b3277a97e0476d 100644 (file)
@@ -1,4 +1,6 @@
-/*-
+/*--
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
  * Copyright (c) 1990, 1993, 1994
  *     The Regents of the University of California.  All rights reserved.
  * Copyright (c) 2002 Networks Associates Technology, Inc.
@@ -17,7 +19,7 @@
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
  *
  * SUCH DAMAGE.
  */
 
-#ifndef lint
-#if 0
-static const char sccsid[] = "@(#)pw_util.c    8.3 (Berkeley) 4/2/94";
-#endif
-static const char rcsid[] =
-  "$FreeBSD$";
-#endif /* not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+__SCCSID("@(#)pw_util.c        8.3 (Berkeley) 4/2/94");
 
 /*
  * This file is used by all the "password" programs; vipw(8), chpass(1),
@@ -66,6 +64,10 @@ static const char rcsid[] =
 #include <string.h>
 #include <unistd.h>
 
+#include <os/availability.h>
+API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
+void * reallocarray(void * in_ptr, size_t nmemb, size_t size) __DARWIN_EXTSN(reallocarray) __result_use_check;
+
 #include "libutil.h"
 
 static pid_t editpid = -1;
@@ -97,7 +99,7 @@ pw_init(const char *dir, const char *master)
 #endif
 
        if (dir == NULL) {
-               strcpy(passwd_dir, _PATH_ETC);
+               strcpy(passwd_dir, _PATH_PWD);
        } else {
                if (strlen(dir) >= sizeof(passwd_dir)) {
                        errno = ENAMETOOLONG;
@@ -183,7 +185,7 @@ pw_lock(void)
                        if (errno == EWOULDBLOCK) {
                                errx(1, "the password db file is busy");
                        } else {
-                               err(1, "could not lock the passwd file");
+                               err(1, "could not lock the passwd file");
                        }
                }
 
@@ -193,7 +195,7 @@ pw_lock(void)
                 * close and retry.
                 */
                if (fstat(lockfd, &st) == -1)
-                       err(1, "fstat() failed");
+                       err(1, "fstat() failed");
                if (st.st_nlink != 0)
                        break;
                close(lockfd);
@@ -310,12 +312,13 @@ pw_edit(int notsetuid)
                sigaction(SIGQUIT, &sa_quit, NULL);
                sigprocmask(SIG_SETMASK, &oldsigset, NULL);
                if (notsetuid) {
-                       (void)setgid(getgid());
-                       (void)setuid(getuid());
+                       if (setgid(getgid()) == -1)
+                               err(1, "setgid");
+                       if (setuid(getuid()) == -1)
+                               err(1, "setuid");
                }
-               errno = 0;
                execlp(editor, editor, tempname, (char *)NULL);
-               _exit(errno);
+               err(1, "%s", editor);
        default:
                /* parent */
                break;
@@ -329,7 +332,9 @@ pw_edit(int notsetuid)
                        break;
                } else if (WIFSTOPPED(pstat)) {
                        raise(WSTOPSIG(pstat));
-               } else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0) {
+               } else if (WIFEXITED(pstat)) {
+                       if (WEXITSTATUS(pstat) != 0)
+                               errx(1, "\"%s\" exited with status %d", editor, WEXITSTATUS(pstat));
                        editpid = -1;
                        break;
                } else {
@@ -343,8 +348,8 @@ pw_edit(int notsetuid)
        sigprocmask(SIG_SETMASK, &oldsigset, NULL);
        if (stat(tempname, &st2) == -1)
                return (-1);
-       return (st1.st_mtim.tv_sec != st2.st_mtim.tv_sec ||
-           st1.st_mtim.tv_nsec != st2.st_mtim.tv_nsec);
+       return (st1.st_mtimespec.tv_sec != st2.st_mtimespec.tv_sec ||
+           st1.st_mtimespec.tv_nsec != st2.st_mtimespec.tv_nsec);
 }
 
 /*
@@ -427,11 +432,12 @@ pw_make_v7(const struct passwd *pw)
 int
 pw_copy(int ffd, int tfd, const struct passwd *pw, struct passwd *old_pw)
 {
-       char buf[8192], *end, *line, *p, *q, *r, t;
+       char *buf, *end, *line, *p, *q, *r, *tmp;
        struct passwd *fpw;
        const struct passwd *spw;
-       size_t len;
+       size_t len, size;
        int eof, readlen;
+       char t;
 
        if (old_pw == NULL && pw == NULL)
                        return (-1);
@@ -449,6 +455,10 @@ pw_copy(int ffd, int tfd, const struct passwd *pw, struct passwd *old_pw)
        if (spw == NULL)
                spw = pw;
 
+       /* initialize the buffer */
+       if ((buf = malloc(size = 1024)) == NULL)
+               goto err;
+
        eof = 0;
        len = 0;
        p = q = end = buf;
@@ -462,10 +472,16 @@ pw_copy(int ffd, int tfd, const struct passwd *pw, struct passwd *old_pw)
                if (q >= end) {
                        if (eof)
                                break;
-                       if ((size_t)(q - p) >= sizeof(buf)) {
-                               warnx("passwd line too long");
-                               errno = EINVAL; /* hack */
-                               goto err;
+                       while ((size_t)(q - p) >= size) {
+                               if ((tmp = reallocarray(buf, 2, size)) == NULL) {
+                                       warnx("passwd line too long");
+                                       goto err;
+                               }
+                               p = tmp + (p - buf);
+                               q = tmp + (q - buf);
+                               end = tmp + (end - buf);
+                               buf = tmp;
+                               size = size * 2;
                        }
                        if (p < end) {
                                q = memmove(buf, p, end - p);
@@ -473,7 +489,7 @@ pw_copy(int ffd, int tfd, const struct passwd *pw, struct passwd *old_pw)
                        } else {
                                p = q = end = buf;
                        }
-                       readlen = read(ffd, end, sizeof(buf) - (end - buf));
+                       readlen = read(ffd, end, size - (end - buf));
                        if (readlen == -1)
                                goto err;
                        else
@@ -482,7 +498,7 @@ pw_copy(int ffd, int tfd, const struct passwd *pw, struct passwd *old_pw)
                                break;
                        end += len;
                        len = end - buf;
-                       if (len < (ssize_t)sizeof(buf)) {
+                       if (len < size) {
                                eof = 1;
                                if (len > 0 && buf[len - 1] != '\n')
                                        ++len, *end++ = '\n';
@@ -545,7 +561,7 @@ pw_copy(int ffd, int tfd, const struct passwd *pw, struct passwd *old_pw)
                        if (write(tfd, q, end - q) != end - q)
                                goto err;
                        q = buf;
-                       readlen = read(ffd, buf, sizeof(buf));
+                       readlen = read(ffd, buf, size);
                        if (readlen == 0)
                                break;
                        else
@@ -567,12 +583,12 @@ pw_copy(int ffd, int tfd, const struct passwd *pw, struct passwd *old_pw)
            write(tfd, "\n", 1) != 1)
                goto err;
  done:
-       if (line != NULL)
-               free(line);
+       free(line);
+       free(buf);
        return (0);
  err:
-       if (line != NULL)
-               free(line);
+       free(line);
+       free(buf);
        return (-1);
 }
 
@@ -643,8 +659,16 @@ pw_dup(const struct passwd *pw)
 #include "pw_scan.h"
 
 /*
- * Wrapper around an internal libc function
+ * Wrapper around some internal libc functions.
  */
+
+void
+pw_initpwd(struct passwd *pw)
+{
+
+       __pw_initpwd(pw);
+}
+
 struct passwd *
 pw_scan(const char *line, int flags)
 {
@@ -653,6 +677,7 @@ pw_scan(const char *line, int flags)
 
        if ((bp = strdup(line)) == NULL)
                return (NULL);
+       __pw_initpwd(&pw);
        if (!__pw_scan(bp, &pw, flags)) {
                free(bp);
                return (NULL);