]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - pw/pw_user.c
Rework the home directory creation and copy or the skel content to use *at
[pw-darwin.git] / pw / pw_user.c
index 6aa0c4719f8ce8e9998e9078cde880a995db067b..afc163fc916c7a812de57c754faf4758a0ed6b33 100644 (file)
@@ -67,20 +67,19 @@ static void     rmopie(char const * name);
 static void
 create_and_populate_homedir(struct passwd *pwd)
 {
-       char *homedir, *dotdir;
        struct userconf *cnf = conf.userconf;
+       const char *skeldir;
+       int skelfd = -1;
 
-       homedir = dotdir = NULL;
+       skeldir = cnf->dotdir;
 
-       if (conf.rootdir[0] != '\0') {
-               asprintf(&homedir, "%s/%s", conf.rootdir, pwd->pw_dir);
-               if (homedir == NULL)
-                       errx(EX_OSERR, "out of memory");
-               asprintf(&dotdir, "%s/%s", conf.rootdir, cnf->dotdir);
+       if (skeldir != NULL && *skeldir != '\0') {
+               skelfd = openat(conf.rootfd, cnf->dotdir,
+                   O_DIRECTORY|O_CLOEXEC);
        }
 
-       copymkdir(homedir ? homedir : pwd->pw_dir, dotdir ? dotdir: cnf->dotdir,
-           cnf->homemode, pwd->pw_uid, pwd->pw_gid);
+       copymkdir(conf.rootfd, pwd->pw_dir, skelfd, cnf->homemode, pwd->pw_uid,
+           pwd->pw_gid, 0);
        pw_log(cnf, M_ADD, W_USER, "%s(%u) home %s made", pwd->pw_name,
            pwd->pw_uid, pwd->pw_dir);
 }
@@ -440,18 +439,13 @@ pw_user(int mode, char *name, long id, struct cargs * args)
                cnf->default_class = pw_checkname(arg->val, 0);
 
        if ((arg = getarg(args, 'G')) != NULL && arg->val) {
-               int i = 0;
-
                for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
                        if ((grp = GETGRNAM(p)) == NULL) {
                                if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
                                        errx(EX_NOUSER, "group `%s' does not exist", p);
                        }
-                       if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
-                               cnf->groups[i++] = newstr(grp->gr_name);
+                       sl_add(cnf->groups, newstr(grp->gr_name));
                }
-               while (i < cnf->numgroups)
-                       cnf->groups[i++] = NULL;
        }
 
        if ((arg = getarg(args, 'k')) != NULL) {
@@ -690,7 +684,8 @@ pw_user(int mode, char *name, long id, struct cargs * args)
         */
 
        if (mode == M_ADD || getarg(args, 'G') != NULL) {
-               int i, j;
+               int j;
+               size_t i;
                /* First remove the user from all group */
                SETGRENT();
                while ((grp = GETGRENT()) != NULL) {
@@ -709,8 +704,8 @@ pw_user(int mode, char *name, long id, struct cargs * args)
                ENDGRENT();
 
                /* now add to group where needed */
-               for (i = 0; cnf->groups[i] != NULL; i++) {
-                       grp = GETGRNAM(cnf->groups[i]);
+               for (i = 0; i < cnf->groups->sl_cur; i++) {
+                       grp = GETGRNAM(cnf->groups->sl_str[i]);
                        grp = gr_add(grp, pwd->pw_name);
                        /*
                         * grp can only be NULL in 2 cases:
@@ -720,7 +715,7 @@ pw_user(int mode, char *name, long id, struct cargs * args)
                         */
                        if (grp == NULL)
                                continue;
-                       chggrent(cnf->groups[i], grp);
+                       chggrent(grp->gr_name, grp);
                        free(grp);
                }
        }
@@ -878,10 +873,7 @@ pw_gidpolicy(struct cargs * args, char *nam, gid_t prefer)
            (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) {
                gid = grp->gr_gid;  /* Already created? Use it anyway... */
        } else {
-               struct cargs    grpargs;
-               char            tmp[32];
-
-               LIST_INIT(&grpargs);
+               gid_t           grid = -1;
 
                /*
                 * We need to auto-create a group with the user's name. We
@@ -892,23 +884,15 @@ pw_gidpolicy(struct cargs * args, char *nam, gid_t prefer)
                 * user's name dups an existing group, then the group add
                 * function will happily handle that case for us and exit.
                 */
-               if (GETGRGID(prefer) == NULL) {
-                       snprintf(tmp, sizeof(tmp), "%u", prefer);
-                       addarg(&grpargs, 'g', tmp);
-               }
+               if (GETGRGID(prefer) == NULL)
+                       grid = prefer;
                if (conf.dryrun) {
                        gid = pw_groupnext(cnf, true);
                } else {
-                       pw_group(M_ADD, nam, -1, &grpargs);
+                       pw_group(M_ADD, nam, grid, NULL);
                        if ((grp = GETGRNAM(nam)) != NULL)
                                gid = grp->gr_gid;
                }
-               a_gid = LIST_FIRST(&grpargs);
-               while (a_gid != NULL) {
-                       struct carg    *t = LIST_NEXT(a_gid, list);
-                       LIST_REMOVE(a_gid, list);
-                       a_gid = t;
-               }
        }
        ENDGRENT();
        return gid;