]>
git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw_user.c
3 * David L. Nugent. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 static const char rcsid
[] =
33 #include <sys/param.h>
34 #include <sys/types.h>
45 #include <login_cap.h>
56 #define LOGNAMESIZE (MAXLOGNAME-1)
58 static char locked_str
[] = "*LOCKED*";
60 static struct passwd fakeuser
= {
74 static int print_user(struct passwd
*pwd
, bool pretty
, bool v7
);
75 static uid_t
pw_uidpolicy(struct userconf
*cnf
, intmax_t id
);
76 static uid_t
pw_gidpolicy(struct userconf
*cnf
, char *grname
, char *nam
,
77 gid_t prefer
, bool dryrun
);
78 static char *pw_homepolicy(struct userconf
* cnf
, char *homedir
,
80 static char *pw_shellpolicy(struct userconf
* cnf
);
81 static char *pw_password(struct userconf
* cnf
, char const * user
,
83 static char *shell_path(char const * path
, char *shells
[], char *sh
);
84 static void rmat(uid_t uid
);
85 static void rmopie(char const * name
);
88 mkdir_home_parents(int dfd
, const char *dir
)
94 errx(EX_DATAERR
, "invalid base directory for home '%s'", dir
);
98 if (fstatat(dfd
, dir
, &st
, 0) != -1) {
99 if (S_ISDIR(st
.st_mode
))
101 errx(EX_OSFILE
, "root home `/%s' is not a directory", dir
);
106 errx(EX_UNAVAILABLE
, "out of memory");
108 tmp
= strrchr(dirs
, '/');
116 * This is a kludge especially for Joerg :)
117 * If the home directory would be created in the root partition, then
118 * we really create it under /usr which is likely to have more space.
119 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
121 if (strchr(dirs
, '/') == NULL
) {
122 asprintf(&tmp
, "usr/%s", dirs
);
124 errx(EX_UNAVAILABLE
, "out of memory");
125 if (mkdirat(dfd
, tmp
, _DEF_DIRMODE
) != -1 || errno
== EEXIST
) {
126 fchownat(dfd
, tmp
, 0, 0, 0);
127 symlinkat(tmp
, dfd
, dirs
);
132 if (fstatat(dfd
, dirs
, &st
, 0) == -1) {
133 while ((tmp
= strchr(tmp
+ 1, '/')) != NULL
) {
135 if (fstatat(dfd
, dirs
, &st
, 0) == -1) {
136 if (mkdirat(dfd
, dirs
, _DEF_DIRMODE
) == -1)
137 err(EX_OSFILE
, "'%s' (root home parent) is not a directory", dirs
);
142 if (fstatat(dfd
, dirs
, &st
, 0) == -1) {
143 if (mkdirat(dfd
, dirs
, _DEF_DIRMODE
) == -1)
144 err(EX_OSFILE
, "'%s' (root home parent) is not a directory", dirs
);
145 fchownat(dfd
, dirs
, 0, 0, 0);
152 create_and_populate_homedir(struct userconf
*cnf
, struct passwd
*pwd
,
153 const char *skeldir
, mode_t homemode
, bool update
)
157 /* Create home parents directories */
158 mkdir_home_parents(conf
.rootfd
, pwd
->pw_dir
);
160 if (skeldir
!= NULL
&& *skeldir
!= '\0') {
163 skelfd
= openat(conf
.rootfd
, skeldir
, O_DIRECTORY
|O_CLOEXEC
);
166 copymkdir(conf
.rootfd
, pwd
->pw_dir
, skelfd
, homemode
, pwd
->pw_uid
,
168 pw_log(cnf
, update
? M_UPDATE
: M_ADD
, W_USER
, "%s(%ju) home %s made",
169 pwd
->pw_name
, (uintmax_t)pwd
->pw_uid
, pwd
->pw_dir
);
173 pw_set_passwd(struct passwd
*pwd
, int fd
, bool precrypted
, bool update
)
178 char line
[_PASSWORD_LEN
+1];
182 if (!pwd
->pw_passwd
|| *pwd
->pw_passwd
!= '*') {
183 pwd
->pw_passwd
= "*"; /* No access */
189 if ((istty
= isatty(fd
))) {
190 if (tcgetattr(fd
, &t
) == -1)
194 n
.c_lflag
&= ~(ECHO
);
195 tcsetattr(fd
, TCSANOW
, &n
);
196 printf("%s%spassword for user %s:",
197 update
? "new " : "",
198 precrypted
? "encrypted " : "",
203 b
= read(fd
, line
, sizeof(line
) - 1);
204 if (istty
) { /* Restore state */
205 tcsetattr(fd
, TCSANOW
, &t
);
211 err(EX_IOERR
, "-%c file descriptor",
212 precrypted
? 'H' : 'h');
214 if ((p
= strpbrk(line
, "\r\n")) != NULL
)
217 errx(EX_DATAERR
, "empty password read on file descriptor %d",
220 if (strchr(line
, ':') != NULL
)
221 errx(EX_DATAERR
, "bad encrypted password");
222 pwd
->pw_passwd
= strdup(line
);
224 lc
= login_getpwclass(pwd
);
226 login_setcryptfmt(lc
, "sha512", NULL
) == NULL
)
227 warn("setting crypt(3) format");
229 pwd
->pw_passwd
= pw_pwcrypt(line
);
235 perform_chgpwent(const char *name
, struct passwd
*pwd
, char *nispasswd
)
238 struct passwd
*nispwd
;
240 /* duplicate for nis so that chgpwent is not modifying before NIS */
241 if (nispasswd
&& *nispasswd
== '/')
242 nispwd
= pw_dup(pwd
);
244 rc
= chgpwent(name
, pwd
);
246 errx(EX_IOERR
, "user '%s' does not exist (NIS?)", pwd
->pw_name
);
248 err(EX_IOERR
, "passwd file update");
250 if (nispasswd
&& *nispasswd
== '/') {
251 rc
= chgnispwent(nispasswd
, name
, nispwd
);
253 warn("User '%s' not found in NIS passwd", pwd
->pw_name
);
255 warn("NIS passwd update");
256 /* NOTE: NIS-only update errors are not fatal */
261 * The M_LOCK and M_UNLOCK functions simply add or remove
262 * a "*LOCKED*" prefix from in front of the password to
263 * prevent it decoding correctly, and therefore prevents
264 * access. Of course, this only prevents access via
265 * password authentication (not ssh, kerberos or any
266 * other method that does not use the UNIX password) but
267 * that is a known limitation.
270 pw_userlock(char *arg1
, int mode
)
272 struct passwd
*pwd
= NULL
;
273 char *passtmp
= NULL
;
276 uid_t id
= (uid_t
)-1;
279 errx(EX_NOPERM
, "you must be root");
282 errx(EX_DATAERR
, "username or id required");
285 if (arg1
[strspn(name
, "0123456789")] == '\0')
286 id
= pw_checkid(name
, UID_MAX
);
288 pwd
= GETPWNAM(pw_checkname(name
, 0));
289 if (pwd
== NULL
&& id
!= (uid_t
)-1) {
296 errx(EX_NOUSER
, "no such name or uid `%ju'", (uintmax_t) id
);
297 errx(EX_NOUSER
, "no such user `%s'", name
);
303 if (strncmp(pwd
->pw_passwd
, locked_str
, sizeof(locked_str
) -1) == 0)
305 if (mode
== M_LOCK
&& locked
)
306 errx(EX_DATAERR
, "user '%s' is already locked", pwd
->pw_name
);
307 if (mode
== M_UNLOCK
&& !locked
)
308 errx(EX_DATAERR
, "user '%s' is not locked", pwd
->pw_name
);
310 if (mode
== M_LOCK
) {
311 asprintf(&passtmp
, "%s%s", locked_str
, pwd
->pw_passwd
);
312 if (passtmp
== NULL
) /* disaster */
313 errx(EX_UNAVAILABLE
, "out of memory");
314 pwd
->pw_passwd
= passtmp
;
316 pwd
->pw_passwd
+= sizeof(locked_str
)-1;
319 perform_chgpwent(name
, pwd
, NULL
);
322 return (EXIT_SUCCESS
);
326 pw_uidpolicy(struct userconf
* cnf
, intmax_t id
)
330 uid_t uid
= (uid_t
) - 1;
333 * Check the given uid, if any
338 if ((pwd
= GETPWUID(uid
)) != NULL
&& conf
.checkduplicate
)
339 errx(EX_DATAERR
, "uid `%ju' has already been allocated",
340 (uintmax_t)pwd
->pw_uid
);
344 * We need to allocate the next available uid under one of
345 * two policies a) Grab the first unused uid b) Grab the
346 * highest possible unused uid
348 if (cnf
->min_uid
>= cnf
->max_uid
) { /* Sanity
349 * claus^H^H^H^Hheck */
351 cnf
->max_uid
= 32000;
353 bm
= bm_alloc(cnf
->max_uid
- cnf
->min_uid
+ 1);
356 * Now, let's fill the bitmap from the password file
359 while ((pwd
= GETPWENT()) != NULL
)
360 if (pwd
->pw_uid
>= (uid_t
) cnf
->min_uid
&& pwd
->pw_uid
<= (uid_t
) cnf
->max_uid
)
361 bm_setbit(&bm
, pwd
->pw_uid
- cnf
->min_uid
);
365 * Then apply the policy, with fallback to reuse if necessary
367 if (cnf
->reuse_uids
|| (uid
= (uid_t
) (bm_lastset(&bm
) + cnf
->min_uid
+ 1)) > cnf
->max_uid
)
368 uid
= (uid_t
) (bm_firstunset(&bm
) + cnf
->min_uid
);
371 * Another sanity check
373 if (uid
< cnf
->min_uid
|| uid
> cnf
->max_uid
)
374 errx(EX_SOFTWARE
, "unable to allocate a new uid - range fully used");
380 pw_gidpolicy(struct userconf
*cnf
, char *grname
, char *nam
, gid_t prefer
, bool dryrun
)
383 gid_t gid
= (uid_t
) - 1;
386 * Check the given gid, if any
390 if ((grp
= GETGRNAM(grname
)) == NULL
) {
391 gid
= pw_checkid(grname
, GID_MAX
);
395 } else if ((grp
= GETGRNAM(nam
)) != NULL
&&
396 (grp
->gr_mem
== NULL
|| grp
->gr_mem
[0] == NULL
)) {
397 gid
= grp
->gr_gid
; /* Already created? Use it anyway... */
402 * We need to auto-create a group with the user's name. We
403 * can send all the appropriate output to our sister routine
404 * bit first see if we can create a group with gid==uid so we
405 * can keep the user and group ids in sync. We purposely do
406 * NOT check the gid range if we can force the sync. If the
407 * user's name dups an existing group, then the group add
408 * function will happily handle that case for us and exit.
410 if (GETGRGID(prefer
) == NULL
)
413 gid
= pw_groupnext(cnf
, true);
416 grid
= pw_groupnext(cnf
, true);
417 groupadd(cnf
, nam
, grid
, NULL
, -1, false, false, false);
418 if ((grp
= GETGRNAM(nam
)) != NULL
)
427 pw_homepolicy(struct userconf
* cnf
, char *homedir
, const char *user
)
429 static char home
[128];
434 if (cnf
->home
== NULL
|| *cnf
->home
== '\0')
435 errx(EX_CONFIG
, "no base home directory set");
436 snprintf(home
, sizeof(home
), "%s/%s", cnf
->home
, user
);
442 shell_path(char const * path
, char *shells
[], char *sh
)
444 if (sh
!= NULL
&& (*sh
== '/' || *sh
== '\0'))
445 return sh
; /* specified full path or forced none */
448 char paths
[_UC_MAXLINE
];
451 * We need to search paths
453 strlcpy(paths
, path
, sizeof(paths
));
454 for (p
= strtok(paths
, ": \t\r\n"); p
!= NULL
; p
= strtok(NULL
, ": \t\r\n")) {
456 static char shellpath
[256];
459 snprintf(shellpath
, sizeof(shellpath
), "%s/%s", p
, sh
);
460 if (access(shellpath
, X_OK
) == 0)
463 for (i
= 0; i
< _UC_MAXSHELLS
&& shells
[i
] != NULL
; i
++) {
464 snprintf(shellpath
, sizeof(shellpath
), "%s/%s", p
, shells
[i
]);
465 if (access(shellpath
, X_OK
) == 0)
470 errx(EX_OSFILE
, "can't find shell `%s' in shell paths", sh
);
471 errx(EX_CONFIG
, "no default shell available or defined");
477 pw_shellpolicy(struct userconf
* cnf
)
480 return shell_path(cnf
->shelldir
, cnf
->shells
, cnf
->shell_default
);
485 static char const chars
[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
488 pw_pwcrypt(char *password
)
491 char salt
[SALTSIZE
+ 1];
493 static char buf
[256];
497 * Calculate a salt value
499 for (i
= 0; i
< SALTSIZE
; i
++)
500 salt
[i
] = chars
[arc4random_uniform(sizeof(chars
) - 1)];
501 salt
[SALTSIZE
] = '\0';
503 cryptpw
= crypt(password
, salt
);
505 errx(EX_CONFIG
, "crypt(3) failure");
506 pwlen
= strlcpy(buf
, cryptpw
, sizeof(buf
));
507 assert(pwlen
< sizeof(buf
));
512 pw_password(struct userconf
* cnf
, char const * user
, bool dryrun
)
517 switch (cnf
->default_password
) {
518 case -1: /* Random password */
519 l
= (arc4random() % 8 + 8); /* 8 - 16 chars */
520 for (i
= 0; i
< l
; i
++)
521 pwbuf
[i
] = chars
[arc4random_uniform(sizeof(chars
)-1)];
525 * We give this information back to the user
527 if (conf
.fd
== -1 && !dryrun
) {
528 if (isatty(STDOUT_FILENO
))
529 printf("Password for '%s' is: ", user
);
530 printf("%s\n", pwbuf
);
535 case -2: /* No password at all! */
538 case 0: /* No login - default */
542 case 1: /* user's name */
543 strlcpy(pwbuf
, user
, sizeof(pwbuf
));
546 return pw_pwcrypt(pwbuf
);
550 print_user(struct passwd
* pwd
, bool pretty
, bool v7
)
554 struct group
*grp
= GETGRGID(pwd
->pw_gid
);
555 char uname
[60] = "User &", office
[60] = "[None]",
556 wphone
[60] = "[None]", hphone
[60] = "[None]";
557 char acexpire
[32] = "[None]", pwexpire
[32] = "[None]";
561 p
= v7
? pw_make_v7(pwd
) : pw_make(pwd
);
564 return (EXIT_SUCCESS
);
567 if ((p
= strtok(pwd
->pw_gecos
, ",")) != NULL
) {
568 strlcpy(uname
, p
, sizeof(uname
));
569 if ((p
= strtok(NULL
, ",")) != NULL
) {
570 strlcpy(office
, p
, sizeof(office
));
571 if ((p
= strtok(NULL
, ",")) != NULL
) {
572 strlcpy(wphone
, p
, sizeof(wphone
));
573 if ((p
= strtok(NULL
, "")) != NULL
) {
574 strlcpy(hphone
, p
, sizeof(hphone
));
580 * Handle '&' in gecos field
582 if ((p
= strchr(uname
, '&')) != NULL
) {
583 int l
= strlen(pwd
->pw_name
);
586 memmove(p
+ l
, p
+ 1, m
);
587 memmove(p
, pwd
->pw_name
, l
);
588 *p
= (char) toupper((unsigned char)*p
);
590 if (pwd
->pw_expire
> (time_t)0 && (tptr
= localtime(&pwd
->pw_expire
)) != NULL
)
591 strftime(acexpire
, sizeof acexpire
, "%c", tptr
);
592 if (pwd
->pw_change
> (time_t)0 && (tptr
= localtime(&pwd
->pw_change
)) != NULL
)
593 strftime(pwexpire
, sizeof pwexpire
, "%c", tptr
);
594 printf("Login Name: %-15s #%-12ju Group: %-15s #%ju\n"
596 " Home: %-26.26s Class: %s\n"
597 " Shell: %-26.26s Office: %s\n"
598 "Work Phone: %-26.26s Home Phone: %s\n"
599 "Acc Expire: %-26.26s Pwd Expire: %s\n",
600 pwd
->pw_name
, (uintmax_t)pwd
->pw_uid
,
601 grp
? grp
->gr_name
: "(invalid)", (uintmax_t)pwd
->pw_gid
,
602 uname
, pwd
->pw_dir
, pwd
->pw_class
,
603 pwd
->pw_shell
, office
, wphone
, hphone
,
607 while ((grp
=GETGRENT()) != NULL
) {
609 if (grp
->gr_mem
!= NULL
) {
610 while (grp
->gr_mem
[i
] != NULL
) {
611 if (strcmp(grp
->gr_mem
[i
], pwd
->pw_name
)==0) {
612 printf(j
++ == 0 ? " Groups: %s" : ",%s", grp
->gr_name
);
620 printf("%s", j
? "\n" : "");
621 return (EXIT_SUCCESS
);
625 pw_checkname(char *name
, int gecos
)
628 const char *badchars
, *ch
, *showtype
;
634 /* See if the name is valid as a gecos (comment) field. */
636 showtype
= "gecos field";
638 /* See if the name is valid as a userid or group. */
639 badchars
= " ,\t:+&#%$^()!@~*?<>=|\\/\"";
640 showtype
= "userid/group name";
641 /* Userids and groups can not have a leading '-'. */
647 if (strchr(badchars
, *ch
) != NULL
||
648 (!gecos
&& *ch
< ' ') ||
653 /* 8-bit characters are only allowed in GECOS fields */
654 if (!gecos
&& (*ch
& 0x80)) {
662 * A `$' is allowed as the final character for userids and groups,
663 * mainly for the benefit of samba.
665 if (reject
&& !gecos
) {
666 if (*ch
== '$' && *(ch
+ 1) == '\0') {
672 snprintf(showch
, sizeof(showch
), (*ch
>= ' ' && *ch
< 127)
673 ? "`%c'" : "0x%02x", *ch
);
674 errx(EX_DATAERR
, "invalid character %s at position %td in %s",
675 showch
, (ch
- name
), showtype
);
677 if (!gecos
&& (ch
- name
) > LOGNAMESIZE
)
678 errx(EX_USAGE
, "name too long `%s' (max is %d)", name
,
687 DIR *d
= opendir("/var/at/jobs");
692 while ((e
= readdir(d
)) != NULL
) {
695 if (strncmp(e
->d_name
, ".lock", 5) != 0 &&
696 stat(e
->d_name
, &st
) == 0 &&
697 !S_ISDIR(st
.st_mode
) &&
699 char tmp
[MAXPATHLEN
];
701 snprintf(tmp
, sizeof(tmp
), "/usr/bin/atrm %s",
711 rmopie(char const * name
)
719 if ((fd
= openat(conf
.rootfd
, "etc/opiekeys", O_RDWR
)) == -1)
722 fp
= fdopen(fd
, "r+");
725 while (fgets(tmp
, sizeof(tmp
), fp
) != NULL
) {
726 if (strncmp(name
, tmp
, len
) == 0 && tmp
[len
]==' ') {
727 /* Comment username out */
728 if (fseek(fp
, atofs
, SEEK_SET
) == 0)
729 fwrite("#", 1, 1, fp
);
735 * If we got an error of any sort, don't update!
741 pw_user_next(int argc
, char **argv
, char *name __unused
)
743 struct userconf
*cnf
= NULL
;
744 const char *cfg
= NULL
;
749 while ((ch
= getopt(argc
, argv
, "C:q")) != -1) {
761 freopen(_PATH_DEVNULL
, "w", stderr
);
763 cnf
= get_userconfig(cfg
);
765 next
= pw_uidpolicy(cnf
, -1);
767 printf("%ju:", (uintmax_t)next
);
768 pw_groupnext(cnf
, quiet
);
770 return (EXIT_SUCCESS
);
774 pw_user_show(int argc
, char **argv
, char *arg1
)
776 struct passwd
*pwd
= NULL
;
787 if (arg1
[strspn(arg1
, "0123456789")] == '\0')
788 id
= pw_checkid(arg1
, UID_MAX
);
793 while ((ch
= getopt(argc
, argv
, "C:qn:u:FPa7")) != -1) {
796 /* ignore compatibility */
805 id
= pw_checkid(optarg
, UID_MAX
);
823 freopen(_PATH_DEVNULL
, "w", stderr
);
827 while ((pwd
= GETPWENT()) != NULL
)
828 print_user(pwd
, pretty
, v7
);
830 return (EXIT_SUCCESS
);
833 if (id
< 0 && name
== NULL
)
834 errx(EX_DATAERR
, "username or id required");
836 pwd
= (name
!= NULL
) ? GETPWNAM(pw_checkname(name
, 0)) : GETPWUID(id
);
842 errx(EX_NOUSER
, "no such uid `%ju'",
844 errx(EX_NOUSER
, "no such user `%s'", name
);
848 return (print_user(pwd
, pretty
, v7
));
852 pw_user_del(int argc
, char **argv
, char *arg1
)
854 struct userconf
*cnf
= NULL
;
855 struct passwd
*pwd
= NULL
;
856 struct group
*gr
, *grp
;
858 char grname
[MAXLOGNAME
];
859 char *nispasswd
= NULL
;
860 char file
[MAXPATHLEN
];
861 char home
[MAXPATHLEN
];
862 const char *cfg
= NULL
;
867 bool deletehome
= false;
871 if (arg1
[strspn(arg1
, "0123456789")] == '\0')
872 id
= pw_checkid(arg1
, UID_MAX
);
877 while ((ch
= getopt(argc
, argv
, "C:qn:u:rYy:")) != -1) {
889 id
= pw_checkid(optarg
, UID_MAX
);
904 freopen(_PATH_DEVNULL
, "w", stderr
);
906 if (id
< 0 && name
== NULL
)
907 errx(EX_DATAERR
, "username or id required");
909 cnf
= get_userconfig(cfg
);
911 if (nispasswd
== NULL
)
912 nispasswd
= cnf
->nispasswd
;
914 pwd
= (name
!= NULL
) ? GETPWNAM(pw_checkname(name
, 0)) : GETPWUID(id
);
917 errx(EX_NOUSER
, "no such uid `%ju'", (uintmax_t) id
);
918 errx(EX_NOUSER
, "no such user `%s'", name
);
921 if (PWF
._altdir
== PWF_REGULAR
&&
922 ((pwd
->pw_fields
& _PWF_SOURCE
) != _PWF_FILES
)) {
923 if ((pwd
->pw_fields
& _PWF_SOURCE
) == _PWF_NIS
) {
924 if (!nis
&& nispasswd
&& *nispasswd
!= '/')
925 errx(EX_NOUSER
, "Cannot remove NIS user `%s'",
928 errx(EX_NOUSER
, "Cannot remove non local user `%s'",
937 if (strcmp(pwd
->pw_name
, "root") == 0)
938 errx(EX_DATAERR
, "cannot remove user 'root'");
940 /* Remove opie record from /etc/opiekeys */
941 if (PWALTDIR() != PWF_ALT
)
942 rmopie(pwd
->pw_name
);
945 /* Remove crontabs */
946 snprintf(file
, sizeof(file
), "/var/cron/tabs/%s", pwd
->pw_name
);
947 if (access(file
, F_OK
) == 0) {
948 snprintf(file
, sizeof(file
), "crontab -u %s -r",
955 * Save these for later, since contents of pwd may be
956 * invalidated by deletion
958 snprintf(file
, sizeof(file
), "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
959 strlcpy(home
, pwd
->pw_dir
, sizeof(home
));
960 gr
= GETGRGID(pwd
->pw_gid
);
962 strlcpy(grname
, gr
->gr_name
, LOGNAMESIZE
);
968 err(EX_IOERR
, "user '%s' does not exist", pwd
->pw_name
);
970 err(EX_IOERR
, "passwd update");
972 if (nis
&& nispasswd
&& *nispasswd
=='/') {
973 rc
= delnispwent(nispasswd
, name
);
975 warnx("WARNING: user '%s' does not exist in NIS passwd",
978 warn("WARNING: NIS passwd update");
981 grp
= GETGRNAM(name
);
983 (grp
->gr_mem
== NULL
|| *grp
->gr_mem
== NULL
) &&
984 strcmp(name
, grname
) == 0)
985 delgrent(GETGRNAM(name
));
987 while ((grp
= GETGRENT()) != NULL
) {
989 char group
[MAXLOGNAME
];
990 if (grp
->gr_mem
== NULL
)
993 for (i
= 0; grp
->gr_mem
[i
] != NULL
; i
++) {
994 if (strcmp(grp
->gr_mem
[i
], name
) != 0)
997 for (j
= i
; grp
->gr_mem
[j
] != NULL
; j
++)
998 grp
->gr_mem
[j
] = grp
->gr_mem
[j
+1];
999 strlcpy(group
, grp
->gr_name
, MAXLOGNAME
);
1000 chggrent(group
, grp
);
1005 pw_log(cnf
, M_DELETE
, W_USER
, "%s(%ju) account removed", name
,
1008 /* Remove mail file */
1009 if (PWALTDIR() != PWF_ALT
)
1010 unlinkat(conf
.rootfd
, file
+ 1, 0);
1012 /* Remove at jobs */
1013 if (!PWALTDIR() && getpwuid(id
) == NULL
)
1016 /* Remove home directory and contents */
1017 if (PWALTDIR() != PWF_ALT
&& deletehome
&& *home
== '/' &&
1018 GETPWUID(id
) == NULL
&&
1019 fstatat(conf
.rootfd
, home
+ 1, &st
, 0) != -1) {
1020 rm_r(conf
.rootfd
, home
, id
);
1021 pw_log(cnf
, M_DELETE
, W_USER
, "%s(%ju) home '%s' %s"
1022 "removed", name
, (uintmax_t)id
, home
,
1023 fstatat(conf
.rootfd
, home
+ 1, &st
, 0) == -1 ? "" : "not "
1027 return (EXIT_SUCCESS
);
1031 pw_user_lock(int argc
, char **argv
, char *arg1
)
1035 while ((ch
= getopt(argc
, argv
, "Cq")) != -1) {
1044 return (pw_userlock(arg1
, M_LOCK
));
1048 pw_user_unlock(int argc
, char **argv
, char *arg1
)
1052 while ((ch
= getopt(argc
, argv
, "Cq")) != -1) {
1061 return (pw_userlock(arg1
, M_UNLOCK
));
1064 static struct group
*
1065 group_from_name_or_id(char *name
)
1067 const char *errstr
= NULL
;
1071 if ((grp
= GETGRNAM(name
)) == NULL
) {
1072 id
= strtounum(name
, 0, GID_MAX
, &errstr
);
1074 errx(EX_NOUSER
, "group `%s' does not exist", name
);
1077 errx(EX_NOUSER
, "group `%s' does not exist", name
);
1084 split_groups(StringList
**groups
, char *groupsstr
)
1088 char tok
[] = ", \t";
1090 if (*groups
== NULL
)
1091 *groups
= sl_init();
1092 for (p
= strtok(groupsstr
, tok
); p
!= NULL
; p
= strtok(NULL
, tok
)) {
1093 grp
= group_from_name_or_id(p
);
1094 sl_add(*groups
, newstr(grp
->gr_name
));
1099 validate_grname(struct userconf
*cnf
, char *group
)
1103 if (group
== NULL
|| *group
== '\0') {
1104 cnf
->default_group
= "";
1107 grp
= group_from_name_or_id(group
);
1108 cnf
->default_group
= newstr(grp
->gr_name
);
1112 validate_mode(char *mode
)
1117 if ((set
= setmode(mode
)) == NULL
)
1118 errx(EX_DATAERR
, "invalid directory creation mode '%s'", mode
);
1120 m
= getmode(set
, _DEF_DIRMODE
);
1126 mix_config(struct userconf
*cmdcnf
, struct userconf
*cfg
)
1129 if (cmdcnf
->default_password
== 0)
1130 cmdcnf
->default_password
= cfg
->default_password
;
1131 if (cmdcnf
->reuse_uids
== 0)
1132 cmdcnf
->reuse_uids
= cfg
->reuse_uids
;
1133 if (cmdcnf
->reuse_gids
== 0)
1134 cmdcnf
->reuse_gids
= cfg
->reuse_gids
;
1135 if (cmdcnf
->nispasswd
== NULL
)
1136 cmdcnf
->nispasswd
= cfg
->nispasswd
;
1137 if (cmdcnf
->dotdir
== NULL
)
1138 cmdcnf
->dotdir
= cfg
->dotdir
;
1139 if (cmdcnf
->newmail
== NULL
)
1140 cmdcnf
->newmail
= cfg
->newmail
;
1141 if (cmdcnf
->logfile
== NULL
)
1142 cmdcnf
->logfile
= cfg
->logfile
;
1143 if (cmdcnf
->home
== NULL
)
1144 cmdcnf
->home
= cfg
->home
;
1145 if (cmdcnf
->homemode
== 0)
1146 cmdcnf
->homemode
= cfg
->homemode
;
1147 if (cmdcnf
->shelldir
== NULL
)
1148 cmdcnf
->shelldir
= cfg
->shelldir
;
1149 if (cmdcnf
->shells
== NULL
)
1150 cmdcnf
->shells
= cfg
->shells
;
1151 if (cmdcnf
->shell_default
== NULL
)
1152 cmdcnf
->shell_default
= cfg
->shell_default
;
1153 if (cmdcnf
->default_group
== NULL
)
1154 cmdcnf
->default_group
= cfg
->default_group
;
1155 if (cmdcnf
->groups
== NULL
)
1156 cmdcnf
->groups
= cfg
->groups
;
1157 if (cmdcnf
->default_class
== NULL
)
1158 cmdcnf
->default_class
= cfg
->default_class
;
1159 if (cmdcnf
->min_uid
== 0)
1160 cmdcnf
->min_uid
= cfg
->min_uid
;
1161 if (cmdcnf
->max_uid
== 0)
1162 cmdcnf
->max_uid
= cfg
->max_uid
;
1163 if (cmdcnf
->min_gid
== 0)
1164 cmdcnf
->min_gid
= cfg
->min_gid
;
1165 if (cmdcnf
->max_gid
== 0)
1166 cmdcnf
->max_gid
= cfg
->max_gid
;
1167 if (cmdcnf
->expire_days
== 0)
1168 cmdcnf
->expire_days
= cfg
->expire_days
;
1169 if (cmdcnf
->password_days
== 0)
1170 cmdcnf
->password_days
= cfg
->password_days
;
1174 pw_user_add(int argc
, char **argv
, char *arg1
)
1176 struct userconf
*cnf
, *cmdcnf
;
1180 char args
[] = "C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y";
1181 char line
[_PASSWORD_LEN
+1], path
[MAXPATHLEN
];
1182 char *gecos
, *homedir
, *skel
, *walk
, *userid
, *groupid
, *grname
;
1183 char *default_passwd
, *name
, *p
;
1184 const char *cfg
= NULL
;
1189 int rc
, ch
, fd
= -1;
1191 bool dryrun
, nis
, pretty
, quiet
, createhome
, precrypted
, genconf
;
1193 dryrun
= nis
= pretty
= quiet
= createhome
= precrypted
= false;
1195 gecos
= homedir
= skel
= userid
= groupid
= default_passwd
= NULL
;
1196 grname
= name
= NULL
;
1198 if ((cmdcnf
= calloc(1, sizeof(struct userconf
))) == NULL
)
1199 err(EXIT_FAILURE
, "calloc()");
1202 if (arg1
[strspn(arg1
, "0123456789")] == '\0')
1203 id
= pw_checkid(arg1
, UID_MAX
);
1208 while ((ch
= getopt(argc
, argv
, args
)) != -1) {
1223 gecos
= pw_checkname(optarg
, 1);
1230 cmdcnf
->expire_days
= parse_date(now
, optarg
);
1234 cmdcnf
->password_days
= parse_date(now
, optarg
);
1237 validate_grname(cmdcnf
, optarg
);
1241 split_groups(&cmdcnf
->groups
, optarg
);
1247 cmdcnf
->homemode
= validate_mode(optarg
);
1250 walk
= skel
= optarg
;
1253 if (fstatat(conf
.rootfd
, walk
, &st
, 0) == -1)
1254 errx(EX_OSFILE
, "skeleton `%s' does not "
1256 if (!S_ISDIR(st
.st_mode
))
1257 errx(EX_OSFILE
, "skeleton `%s' is not a "
1259 cmdcnf
->dotdir
= skel
;
1262 cmdcnf
->shell_default
= optarg
;
1265 conf
.checkduplicate
= false;
1268 cmdcnf
->default_class
= pw_checkname(optarg
, 0);
1274 default_passwd
= optarg
;
1278 errx(EX_USAGE
, "'-h' and '-H' are mutually "
1279 "exclusive options");
1280 fd
= pw_checkfd(optarg
);
1283 errx(EX_USAGE
, "-H expects a file descriptor");
1287 errx(EX_USAGE
, "'-h' and '-H' are mutually "
1288 "exclusive options");
1289 fd
= pw_checkfd(optarg
);
1295 cmdcnf
->home
= optarg
;
1304 cmdcnf
->nispasswd
= optarg
;
1312 if (geteuid() != 0 && ! dryrun
)
1313 errx(EX_NOPERM
, "you must be root");
1316 freopen(_PATH_DEVNULL
, "w", stderr
);
1318 cnf
= get_userconfig(cfg
);
1320 mix_config(cmdcnf
, cnf
);
1322 cmdcnf
->default_password
= passwd_val(default_passwd
,
1323 cnf
->default_password
);
1326 errx(EX_DATAERR
, "can't combine `-D' with `-n name'");
1327 if (userid
!= NULL
) {
1328 if ((p
= strtok(userid
, ", \t")) != NULL
)
1329 cmdcnf
->min_uid
= pw_checkid(p
, UID_MAX
);
1330 if (cmdcnf
->min_uid
== 0)
1331 cmdcnf
->min_uid
= 1000;
1332 if ((p
= strtok(NULL
, " ,\t")) != NULL
)
1333 cmdcnf
->max_uid
= pw_checkid(p
, UID_MAX
);
1334 if (cmdcnf
->max_uid
== 0)
1335 cmdcnf
->max_uid
= 32000;
1337 if (groupid
!= NULL
) {
1338 if ((p
= strtok(groupid
, ", \t")) != NULL
)
1339 cmdcnf
->min_gid
= pw_checkid(p
, GID_MAX
);
1340 if (cmdcnf
->min_gid
== 0)
1341 cmdcnf
->min_gid
= 1000;
1342 if ((p
= strtok(NULL
, " ,\t")) != NULL
)
1343 cmdcnf
->max_gid
= pw_checkid(p
, GID_MAX
);
1344 if (cmdcnf
->max_gid
== 0)
1345 cmdcnf
->max_gid
= 32000;
1347 if (write_userconfig(cmdcnf
, cfg
))
1348 return (EXIT_SUCCESS
);
1349 err(EX_IOERR
, "config update");
1353 id
= pw_checkid(userid
, UID_MAX
);
1354 if (id
< 0 && name
== NULL
)
1355 errx(EX_DATAERR
, "user name or id required");
1358 errx(EX_DATAERR
, "login name required");
1360 if (GETPWNAM(name
) != NULL
)
1361 errx(EX_DATAERR
, "login name `%s' already exists", name
);
1364 grname
= cmdcnf
->default_group
;
1367 pwd
->pw_name
= name
;
1368 pwd
->pw_class
= cmdcnf
->default_class
? cmdcnf
->default_class
: "";
1369 pwd
->pw_uid
= pw_uidpolicy(cmdcnf
, id
);
1370 pwd
->pw_gid
= pw_gidpolicy(cnf
, grname
, pwd
->pw_name
,
1371 (gid_t
) pwd
->pw_uid
, dryrun
);
1372 pwd
->pw_change
= cmdcnf
->password_days
;
1373 pwd
->pw_expire
= cmdcnf
->expire_days
;
1374 pwd
->pw_dir
= pw_homepolicy(cmdcnf
, homedir
, pwd
->pw_name
);
1375 pwd
->pw_shell
= pw_shellpolicy(cmdcnf
);
1376 lc
= login_getpwclass(pwd
);
1377 if (lc
== NULL
|| login_setcryptfmt(lc
, "sha512", NULL
) == NULL
)
1378 warn("setting crypt(3) format");
1380 pwd
->pw_passwd
= pw_password(cmdcnf
, pwd
->pw_name
, dryrun
);
1381 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
1382 warnx("WARNING: new account `%s' has a uid of 0 "
1383 "(superuser access!)", pwd
->pw_name
);
1385 pwd
->pw_gecos
= gecos
;
1388 pw_set_passwd(pwd
, fd
, precrypted
, false);
1391 return (print_user(pwd
, pretty
, false));
1393 if ((rc
= addpwent(pwd
)) != 0) {
1395 errx(EX_IOERR
, "user '%s' already exists",
1398 err(EX_IOERR
, "passwd file update");
1400 if (nis
&& cmdcnf
->nispasswd
&& *cmdcnf
->nispasswd
== '/') {
1401 printf("%s\n", cmdcnf
->nispasswd
);
1402 rc
= addnispwent(cmdcnf
->nispasswd
, pwd
);
1404 warnx("User '%s' already exists in NIS passwd",
1407 warn("NIS passwd update");
1408 /* NOTE: we treat NIS-only update errors as non-fatal */
1411 if (cmdcnf
->groups
!= NULL
) {
1412 for (i
= 0; i
< cmdcnf
->groups
->sl_cur
; i
++) {
1413 grp
= GETGRNAM(cmdcnf
->groups
->sl_str
[i
]);
1414 grp
= gr_add(grp
, pwd
->pw_name
);
1416 * grp can only be NULL in 2 cases:
1417 * - the new member is already a member
1418 * - a problem with memory occurs
1419 * in both cases we want to skip now.
1423 chggrent(grp
->gr_name
, grp
);
1428 pwd
= GETPWNAM(name
);
1430 errx(EX_NOUSER
, "user '%s' disappeared during update", name
);
1432 grp
= GETGRGID(pwd
->pw_gid
);
1433 pw_log(cnf
, M_ADD
, W_USER
, "%s(%ju):%s(%ju):%s:%s:%s",
1434 pwd
->pw_name
, (uintmax_t)pwd
->pw_uid
,
1435 grp
? grp
->gr_name
: "unknown",
1436 (uintmax_t)(grp
? grp
->gr_gid
: (uid_t
)-1),
1437 pwd
->pw_gecos
, pwd
->pw_dir
, pwd
->pw_shell
);
1440 * let's touch and chown the user's mail file. This is not
1441 * strictly necessary under BSD with a 0755 maildir but it also
1442 * doesn't hurt anything to create the empty mailfile
1444 if (PWALTDIR() != PWF_ALT
) {
1445 snprintf(path
, sizeof(path
), "%s/%s", _PATH_MAILDIR
,
1447 /* Preserve contents & mtime */
1448 close(openat(conf
.rootfd
, path
+1, O_RDWR
| O_CREAT
, 0600));
1449 fchownat(conf
.rootfd
, path
+ 1, pwd
->pw_uid
, pwd
->pw_gid
,
1450 AT_SYMLINK_NOFOLLOW
);
1454 * Let's create and populate the user's home directory. Note
1455 * that this also `works' for editing users if -m is used, but
1456 * existing files will *not* be overwritten.
1458 if (PWALTDIR() != PWF_ALT
&& createhome
&& pwd
->pw_dir
&&
1459 *pwd
->pw_dir
== '/' && pwd
->pw_dir
[1])
1460 create_and_populate_homedir(cmdcnf
, pwd
, cmdcnf
->dotdir
,
1461 cmdcnf
->homemode
, false);
1463 if (!PWALTDIR() && cmdcnf
->newmail
&& *cmdcnf
->newmail
&&
1464 (fp
= fopen(cnf
->newmail
, "r")) != NULL
) {
1465 if ((pfp
= popen(_PATH_SENDMAIL
" -t", "w")) == NULL
)
1468 fprintf(pfp
, "From: root\n" "To: %s\n"
1469 "Subject: Welcome!\n\n", pwd
->pw_name
);
1470 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1471 /* Do substitutions? */
1475 pw_log(cnf
, M_ADD
, W_USER
, "%s(%ju) new user mail sent",
1476 pwd
->pw_name
, (uintmax_t)pwd
->pw_uid
);
1481 if (nis
&& nis_update() == 0)
1482 pw_log(cnf
, M_ADD
, W_USER
, "NIS maps updated");
1484 return (EXIT_SUCCESS
);
1488 pw_user_mod(int argc
, char **argv
, char *arg1
)
1490 struct userconf
*cnf
;
1493 StringList
*groups
= NULL
;
1494 char args
[] = "C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:NPYy:";
1495 const char *cfg
= NULL
;
1496 char *gecos
, *homedir
, *grname
, *name
, *newname
, *walk
, *skel
, *shell
;
1497 char *passwd
, *class, *nispasswd
;
1503 bool quiet
, createhome
, pretty
, dryrun
, nis
, edited
;
1505 mode_t homemode
= 0;
1506 time_t expire_days
, password_days
, now
;
1508 expire_days
= password_days
= -1;
1509 gecos
= homedir
= grname
= name
= newname
= skel
= shell
=NULL
;
1511 class = nispasswd
= NULL
;
1512 quiet
= createhome
= pretty
= dryrun
= nis
= precrypted
= false;
1516 if (arg1
[strspn(arg1
, "0123456789")] == '\0')
1517 id
= pw_checkid(arg1
, UID_MAX
);
1522 while ((ch
= getopt(argc
, argv
, args
)) != -1) {
1534 id
= pw_checkid(optarg
, UID_MAX
);
1537 gecos
= pw_checkname(optarg
, 1);
1544 expire_days
= parse_date(now
, optarg
);
1548 password_days
= parse_date(now
, optarg
);
1551 group_from_name_or_id(optarg
);
1555 split_groups(&groups
, optarg
);
1561 homemode
= validate_mode(optarg
);
1567 walk
= skel
= optarg
;
1570 if (fstatat(conf
.rootfd
, walk
, &st
, 0) == -1)
1571 errx(EX_OSFILE
, "skeleton `%s' does not "
1573 if (!S_ISDIR(st
.st_mode
))
1574 errx(EX_OSFILE
, "skeleton `%s' is not a "
1584 class = pw_checkname(optarg
, 0);
1588 errx(EX_USAGE
, "'-h' and '-H' are mutually "
1589 "exclusive options");
1590 fd
= pw_checkfd(optarg
);
1593 errx(EX_USAGE
, "-H expects a file descriptor");
1597 errx(EX_USAGE
, "'-h' and '-H' are mutually "
1598 "exclusive options");
1599 fd
= pw_checkfd(optarg
);
1616 if (geteuid() != 0 && ! dryrun
)
1617 errx(EX_NOPERM
, "you must be root");
1620 freopen(_PATH_DEVNULL
, "w", stderr
);
1622 cnf
= get_userconfig(cfg
);
1624 if (id
< 0 && name
== NULL
)
1625 errx(EX_DATAERR
, "username or id required");
1627 pwd
= (name
!= NULL
) ? GETPWNAM(pw_checkname(name
, 0)) : GETPWUID(id
);
1630 errx(EX_NOUSER
, "no such uid `%ju'",
1632 errx(EX_NOUSER
, "no such user `%s'", name
);
1636 name
= pwd
->pw_name
;
1638 if (nis
&& nispasswd
== NULL
)
1639 nispasswd
= cnf
->nispasswd
;
1641 if (PWF
._altdir
== PWF_REGULAR
&&
1642 ((pwd
->pw_fields
& _PWF_SOURCE
) != _PWF_FILES
)) {
1643 if ((pwd
->pw_fields
& _PWF_SOURCE
) == _PWF_NIS
) {
1644 if (!nis
&& nispasswd
&& *nispasswd
!= '/')
1645 errx(EX_NOUSER
, "Cannot modify NIS user `%s'",
1648 errx(EX_NOUSER
, "Cannot modify non local user `%s'",
1654 if (strcmp(pwd
->pw_name
, "root") == 0)
1655 errx(EX_DATAERR
, "can't rename `root' account");
1656 if (strcmp(pwd
->pw_name
, newname
) != 0) {
1657 pwd
->pw_name
= pw_checkname(newname
, 0);
1662 if (id
>= 0 && pwd
->pw_uid
!= id
) {
1665 if (pwd
->pw_uid
!= 0 && strcmp(pwd
->pw_name
, "root") == 0)
1666 errx(EX_DATAERR
, "can't change uid of `root' account");
1667 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
1668 warnx("WARNING: account `%s' will have a uid of 0 "
1669 "(superuser access!)", pwd
->pw_name
);
1672 if (grname
&& pwd
->pw_uid
!= 0) {
1673 grp
= GETGRNAM(grname
);
1675 grp
= GETGRGID(pw_checkid(grname
, GID_MAX
));
1676 if (grp
->gr_gid
!= pwd
->pw_gid
) {
1677 pwd
->pw_gid
= grp
->gr_gid
;
1682 if (password_days
>= 0 && pwd
->pw_change
!= password_days
) {
1683 pwd
->pw_change
= password_days
;
1687 if (expire_days
>= 0 && pwd
->pw_expire
!= expire_days
) {
1688 pwd
->pw_expire
= expire_days
;
1693 shell
= shell_path(cnf
->shelldir
, cnf
->shells
, shell
);
1696 if (strcmp(shell
, pwd
->pw_shell
) != 0) {
1697 pwd
->pw_shell
= shell
;
1702 if (class && strcmp(pwd
->pw_class
, class) != 0) {
1703 pwd
->pw_class
= class;
1707 if (homedir
&& strcmp(pwd
->pw_dir
, homedir
) != 0) {
1708 pwd
->pw_dir
= homedir
;
1710 if (fstatat(conf
.rootfd
, pwd
->pw_dir
, &st
, 0) == -1) {
1712 warnx("WARNING: home `%s' does not exist",
1714 } else if (!S_ISDIR(st
.st_mode
)) {
1715 warnx("WARNING: home `%s' is not a directory",
1720 if (passwd
&& conf
.fd
== -1) {
1721 lc
= login_getpwclass(pwd
);
1722 if (lc
== NULL
|| login_setcryptfmt(lc
, "sha512", NULL
) == NULL
)
1723 warn("setting crypt(3) format");
1725 cnf
->default_password
= passwd_val(passwd
,
1726 cnf
->default_password
);
1727 pwd
->pw_passwd
= pw_password(cnf
, pwd
->pw_name
, dryrun
);
1731 if (gecos
&& strcmp(pwd
->pw_gecos
, gecos
) != 0) {
1732 pwd
->pw_gecos
= gecos
;
1737 edited
= pw_set_passwd(pwd
, fd
, precrypted
, true);
1740 return (print_user(pwd
, pretty
, false));
1742 if (edited
) /* Only updated this if required */
1743 perform_chgpwent(name
, pwd
, nis
? nispasswd
: NULL
);
1744 /* Now perform the needed changes concern groups */
1745 if (groups
!= NULL
) {
1746 /* Delete User from groups using old name */
1748 while ((grp
= GETGRENT()) != NULL
) {
1749 if (grp
->gr_mem
== NULL
)
1751 for (i
= 0; grp
->gr_mem
[i
] != NULL
; i
++) {
1752 if (strcmp(grp
->gr_mem
[i
] , name
) != 0)
1754 for (j
= i
; grp
->gr_mem
[j
] != NULL
; j
++)
1755 grp
->gr_mem
[j
] = grp
->gr_mem
[j
+1];
1756 chggrent(grp
->gr_name
, grp
);
1761 /* Add the user to the needed groups */
1762 for (i
= 0; i
< groups
->sl_cur
; i
++) {
1763 grp
= GETGRNAM(groups
->sl_str
[i
]);
1764 grp
= gr_add(grp
, pwd
->pw_name
);
1767 chggrent(grp
->gr_name
, grp
);
1771 /* In case of rename we need to walk over the different groups */
1774 while ((grp
= GETGRENT()) != NULL
) {
1775 if (grp
->gr_mem
== NULL
)
1777 for (i
= 0; grp
->gr_mem
[i
] != NULL
; i
++) {
1778 if (strcmp(grp
->gr_mem
[i
], name
) != 0)
1780 grp
->gr_mem
[i
] = newname
;
1781 chggrent(grp
->gr_name
, grp
);
1787 /* go get a current version of pwd */
1790 pwd
= GETPWNAM(name
);
1792 errx(EX_NOUSER
, "user '%s' disappeared during update", name
);
1793 grp
= GETGRGID(pwd
->pw_gid
);
1794 pw_log(cnf
, M_UPDATE
, W_USER
, "%s(%ju):%s(%ju):%s:%s:%s",
1795 pwd
->pw_name
, (uintmax_t)pwd
->pw_uid
,
1796 grp
? grp
->gr_name
: "unknown",
1797 (uintmax_t)(grp
? grp
->gr_gid
: (uid_t
)-1),
1798 pwd
->pw_gecos
, pwd
->pw_dir
, pwd
->pw_shell
);
1801 * Let's create and populate the user's home directory. Note
1802 * that this also `works' for editing users if -m is used, but
1803 * existing files will *not* be overwritten.
1805 if (PWALTDIR() != PWF_ALT
&& createhome
&& pwd
->pw_dir
&&
1806 *pwd
->pw_dir
== '/' && pwd
->pw_dir
[1]) {
1810 homemode
= cnf
->homemode
;
1811 create_and_populate_homedir(cnf
, pwd
, skel
, homemode
, true);
1814 if (nis
&& nis_update() == 0)
1815 pw_log(cnf
, M_UPDATE
, W_USER
, "NIS maps updated");
1817 return (EXIT_SUCCESS
);