if the check of the pw db fails return the failed value
[pw-darwin.git] / pw / pwupd.c
index 4ab0f0196777203e48cc29c5181fec0de9d13592..51d732ee3cc6f2f65dd1a201c571ef498472bb69 100644 (file)
@@ -45,9 +45,6 @@ static const char rcsid[] =
 
 #include "pwupd.h"
 
-#define HAVE_PWDB_C    1
-#define        HAVE_PWDB_U     1
-
 static char pathpwd[] = _PATH_PWD;
 static char * pwpath = pathpwd;
  
@@ -55,14 +52,13 @@ int
 setpwdir(const char * dir)
 {
        if (dir == NULL)
-               return -1;
-       else {
-               char * d = malloc(strlen(dir)+1);
-               if (d == NULL)
-                       return -1;
-               pwpath = strcpy(d, dir);
-       }
-       return 0;
+               return (-1);
+       else
+               pwpath = strdup(dir);
+       if (pwpath == NULL)
+               return (-1);
+
+       return (0);
 }
 
 char *
@@ -71,7 +67,8 @@ getpwpath(char const * file)
        static char pathbuf[MAXPATHLEN];
 
        snprintf(pathbuf, sizeof pathbuf, "%s/%s", pwpath, file);
-       return pathbuf;
+
+       return (pathbuf);
 }
 
 static int
@@ -106,68 +103,66 @@ pwdb(char *arg,...)
                        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("-C", NULL)) != 0)
+               return (rc);
+
+       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()");
+       }
        /*
-        * 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()");
-               }
-               if (pw_mkdb(user) == -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
@@ -176,5 +171,6 @@ delpwent(struct passwd * pwd)
        char login[MAXLOGNAME];
        
        strlcpy(login, pwd->pw_name, MAXLOGNAME);
-       return pw_update(NULL, login);
+
+       return (pw_update(NULL, login));
 }