]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - pw/pwupd.c
Merge from head
[pw-darwin.git] / pw / pwupd.c
index fea551cc0ff7202d6d5ad7afc75544cea8058523..c2a9a535ec452280c68fcfde1b515508f19e67d2 100644 (file)
@@ -34,7 +34,10 @@ static const char rcsid[] =
 #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>
@@ -42,8 +45,6 @@ static const char rcsid[] =
 
 #include "pwupd.h"
 
-#define HAVE_PWDB_C    1
-
 static char pathpwd[] = _PATH_PWD;
 static char * pwpath = pathpwd;
  
@@ -52,12 +53,10 @@ 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);
-       }
+       else
+               pwpath = strdup(dir);
+       if (pwpath == NULL)
+               return -1;
        return 0;
 }
 
@@ -70,7 +69,7 @@ getpwpath(char const * file)
        return pathbuf;
 }
 
-int
+static int
 pwdb(char *arg,...)
 {
        int             i = 0;
@@ -101,101 +100,72 @@ pwdb(char *arg,...)
                if (WEXITSTATUS(i))
                        i = EIO;
        }
+       va_end(ap);
        return i;
 }
 
-int
-fmtpwentry(char *buf, struct passwd * pwd, int type)
-{
-       int             l;
-       char           *pw;
-
-       pw = (pwd->pw_passwd == NULL || !*pwd->pw_passwd) ? "" : (type == PWF_MASTER) ? pwd->pw_passwd : "*";
-
-       if (type == PWF_PASSWD)
-               l = sprintf(buf, "%s:*:%ld:%ld:%s:%s:%s\n",
-                      pwd->pw_name, (long) pwd->pw_uid, (long) pwd->pw_gid,
-                           pwd->pw_gecos ? pwd->pw_gecos : "User &",
-                           pwd->pw_dir, pwd->pw_shell);
-       else
-               l = sprintf(buf, "%s:%s:%ld:%ld:%s:%lu:%lu:%s:%s:%s\n",
-                  pwd->pw_name, pw, (long) pwd->pw_uid, (long) pwd->pw_gid,
-                           pwd->pw_class ? pwd->pw_class : "",
-                           (unsigned long) pwd->pw_change,
-                           (unsigned long) pwd->pw_expire,
-                           pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
-       return l;
-}
-
-
-int
-fmtpwent(char *buf, struct passwd * pwd)
-{
-       return fmtpwentry(buf, pwd, PWF_STANDARD);
-}
-
 static int
-pw_update(struct passwd * pwd, char const * user, int mode)
+pw_update(struct passwd * pwd, char const * user)
 {
        int             rc = 0;
 
-       ENDPWENT();
-
-       /*
-        * First, let's check the see if the database is alright
-        * Note: -C is only available in FreeBSD 2.2 and above
-        */
-#ifdef HAVE_PWDB_C
-       if (pwdb("-C", NULL) == 0) {    /* Check only */
-#else
-       {                               /* No -C */
-#endif
-               char            pfx[32];
-               char            pwbuf[PWBUFSZ];
-               int             l = sprintf(pfx, "%s:", user);
+       rc = pwdb("-C", (char *)NULL);  /* Check only */
+       if (rc == 0) {
+               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()");
+               }
                /*
-                * Update the passwd file first
+                * in case of deletion of a user, the whole database
+                * needs to be regenerated
                 */
-               if (pwd == NULL)
-                       *pwbuf = '\0';
-               else
-                       fmtpwentry(pwbuf, pwd, PWF_PASSWD);
-
-               rc = fileupdate(getpwpath(_PASSWD), 0644, pwbuf, pfx, l, mode);
-               if (rc == 0) {
-
-                       /*
-                        * Then the master.passwd file
-                        */
-                       if (pwd != NULL)
-                               fmtpwentry(pwbuf, pwd, PWF_MASTER);
-                       rc = fileupdate(getpwpath(_MASTERPASSWD), 0644, pwbuf, pfx, l, mode);
-                       if (rc == 0) {
-                               if (mode == UPD_DELETE)
-                                       rc = pwdb(NULL);
-                               else
-                                       rc = pwdb("-u", user, NULL);
-                       }
+               if (pw_mkdb(pw != NULL ? pw->pw_name : NULL) == -1) {
+                       pw_fini();
+                       err(1, "pw_mkdb()");
                }
+               free(pw);
+               pw_fini();
        }
-       return rc;
+       return 0;
 }
 
 int
 addpwent(struct passwd * pwd)
 {
-       return pw_update(pwd, pwd->pw_name, UPD_CREATE);
+       return pw_update(pwd, NULL);
 }
 
 int
 chgpwent(char const * login, struct passwd * pwd)
 {
-       return pw_update(pwd, login, UPD_REPLACE);
+       return pw_update(pwd, login);
 }
 
 int
 delpwent(struct passwd * pwd)
 {
-       return pw_update(NULL, pwd->pw_name, UPD_DELETE);
+       char login[MAXLOGNAME];
+       
+       strlcpy(login, pwd->pw_name, MAXLOGNAME);
+       return pw_update(NULL, login);
 }