]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - pw/pwupd.c
Remove a death threat from the FreeBSD sources
[pw-darwin.git] / pw / pwupd.c
index 22662dbd0b34a33af2ce9897cbab8400587dba8f..87c3c994bada86698e0fd233f964143866ca99a4 100644 (file)
@@ -29,66 +29,42 @@ static const char rcsid[] =
   "$FreeBSD$";
 #endif /* not lint */
 
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <pwd.h>
+#include <libutil.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <stdarg.h>
-#include <pwd.h>
-#include <libutil.h>
-#include <errno.h>
-#include <err.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/param.h>
-#include <sys/wait.h>
 
 #include "pwupd.h"
 
-#define HAVE_PWDB_C    1
-#define        HAVE_PWDB_U     1
-
-static char pathpwd[] = _PATH_PWD;
-static char * pwpath = pathpwd;
-int
-setpwdir(const char * dir)
-{
-       if (dir == NULL)
-               return -1;
-       else
-               pwpath = strdup(dir);
-       if (pwpath == NULL)
-               return -1;
-       return 0;
-}
-
 char *
 getpwpath(char const * file)
 {
        static char pathbuf[MAXPATHLEN];
 
-       snprintf(pathbuf, sizeof pathbuf, "%s/%s", pwpath, file);
-       return pathbuf;
+       snprintf(pathbuf, sizeof pathbuf, "%s/%s", conf.etcpath, file);
+
+       return (pathbuf);
 }
 
 static int
-pwdb(char *arg,...)
+pwdb_check(void)
 {
        int             i = 0;
        pid_t           pid;
-       va_list         ap;
        char           *args[10];
 
        args[i++] = _PATH_PWD_MKDB;
-       va_start(ap, arg);
-       while (i < 6 && arg != NULL) {
-               args[i++] = arg;
-               arg = va_arg(ap, char *);
-       }
-       if (pwpath != pathpwd) {
+       args[i++] = "-C";
+
+       if (strcmp(conf.etcpath, _PATH_PWD) != 0) {
                args[i++] = "-d";
-               args[i++] = pwpath;
+               args[i++] = conf.etcpath;
        }
        args[i++] = getpwpath(_MASTERPASSWD);
        args[i] = NULL;
@@ -103,80 +79,74 @@ pwdb(char *arg,...)
                if (WEXITSTATUS(i))
                        i = EIO;
        }
-       va_end(ap);
-       return i;
+
+       return (i);
 }
 
 static int
 pw_update(struct passwd * pwd, char const * user)
 {
-       int             rc = 0;
+       struct passwd   *pw = NULL;
+       struct passwd   *old_pw = NULL;
+       int              rc, pfd, tfd;
+
+       if ((rc = pwdb_check()) != 0)
+               return (rc);
 
+       if (pwd != NULL)
+               pw = pw_dup(pwd);
+
+       if (user != NULL)
+               old_pw = GETPWNAM(user);
+
+       if (pw_init(conf.etcpath, NULL))
+               err(1, "pw_init()");
+       if ((pfd = pw_lock()) == -1) {
+               pw_fini();
+               err(1, "pw_lock()");
+       }
+       if ((tfd = pw_tmp(-1)) == -1) {
+               pw_fini();
+               err(1, "pw_tmp()");
+       }
+       if (pw_copy(pfd, tfd, pw, old_pw) == -1) {
+               pw_fini();
+               close(tfd);
+               err(1, "pw_copy()");
+       }
+       fsync(tfd);
+       close(tfd);
        /*
-        * First, let's check the see if the database is alright
-        * Note: -C is only available in FreeBSD 2.2 and above
+        * in case of deletion of a user, the whole database
+        * needs to be regenerated
         */
-#ifdef HAVE_PWDB_C
-       rc = pwdb("-C", (char *)NULL);  /* Check only */
-       if (rc == 0) {
-#else
-       {                               /* No -C */
-#endif
-               int pfd, tfd;
-               struct passwd *pw = NULL;
-               struct passwd *old_pw = NULL;
-
-               if (pwd != NULL)
-                      pw = pw_dup(pwd);
-
-               if (user != NULL)
-                       old_pw = GETPWNAM(user);
-
-               if (pw_init(pwpath, NULL))
-                       err(1, "pw_init()");
-               if ((pfd = pw_lock()) == -1) {
-                       pw_fini();
-                       err(1, "pw_lock()");
-               }
-               if ((tfd = pw_tmp(-1)) == -1) {
-                       pw_fini();
-                       err(1, "pw_tmp()");
-               }
-               if (pw_copy(pfd, tfd, pw, old_pw) == -1) {
-                       pw_fini();
-                       err(1, "pw_copy()");
-               }
-               /*
-                * in case of deletion of a user, the whole database
-                * needs to be regenerated
-                */
-               if (pw_mkdb(pw != NULL ? user : NULL) == -1) {
-                       pw_fini();
-                       err(1, "pw_mkdb()");
-               }
-               free(pw);
+       if (pw_mkdb(pw != NULL ? pw->pw_name : NULL) == -1) {
                pw_fini();
+               err(1, "pw_mkdb()");
        }
-       return 0;
+       free(pw);
+       pw_fini();
+
+       return (0);
 }
 
 int
 addpwent(struct passwd * pwd)
 {
-       return pw_update(pwd, NULL);
+
+       return (pw_update(pwd, NULL));
 }
 
 int
 chgpwent(char const * login, struct passwd * pwd)
 {
-       return pw_update(pwd, login);
+
+       return (pw_update(pwd, login));
 }
 
 int
 delpwent(struct passwd * pwd)
 {
-       char login[MAXLOGNAME];
-       
-       strlcpy(login, pwd->pw_name, MAXLOGNAME);
-       return pw_update(NULL, login);
+
+       return (pw_update(NULL, pwd->pw_name));
 }