]>
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
[] =
36 #include <sys/param.h>
40 #include <sys/types.h>
42 #include <sys/resource.h>
43 #include <login_cap.h>
50 #define LOGNAMESIZE (MAXLOGNAME-1)
52 static char locked_str
[] = "*LOCKED*";
54 static int delete_user(struct userconf
*cnf
, struct passwd
*pwd
,
55 struct carg
*a_name
, int delete, int mode
);
56 static int print_user(struct passwd
* pwd
);
57 static uid_t
pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
);
58 static uid_t
pw_gidpolicy(struct cargs
* args
, char *nam
, gid_t prefer
);
59 static time_t pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
);
60 static time_t pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
);
61 static char *pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
);
62 static char *pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
);
63 static char *pw_password(struct userconf
* cnf
, struct cargs
* args
, char const * user
);
64 static char *shell_path(char const * path
, char *shells
[], char *sh
);
65 static void rmat(uid_t uid
);
66 static void rmopie(char const * name
);
69 create_and_populate_homedir(int mode
, struct passwd
*pwd
)
71 char *homedir
, *dotdir
;
72 struct userconf
*cnf
= conf
.userconf
;
74 homedir
= dotdir
= NULL
;
76 if (conf
.rootdir
[0] != '\0') {
77 asprintf(&homedir
, "%s/%s", conf
.rootdir
, pwd
->pw_dir
);
79 errx(EX_OSERR
, "out of memory");
80 asprintf(&dotdir
, "%s/%s", conf
.rootdir
, cnf
->dotdir
);
83 copymkdir(homedir
? homedir
: pwd
->pw_dir
, dotdir
? dotdir
: cnf
->dotdir
,
84 cnf
->homemode
, pwd
->pw_uid
, pwd
->pw_gid
);
85 pw_log(cnf
, mode
, W_USER
, "%s(%u) home %s made", pwd
->pw_name
,
86 pwd
->pw_uid
, pwd
->pw_dir
);
90 * -C config configuration file
94 * -c comment user name/comment
95 * -d directory home directory
96 * -e date account expiry date
97 * -p date password expiry date
98 * -g grp primary group
99 * -G grp1,grp2 additional groups
100 * -m [ -k dir ] create and set up home
101 * -s shell name of login shell
102 * -o duplicate uid ok
103 * -L class user class
104 * -l name new login name
105 * -h fd password filehandle
106 * -H fd encrypted password filehandle
107 * -F force print or add
109 * -D set user defaults
110 * -b dir default home root dir
111 * -e period default expiry period
112 * -p period default password change period
113 * -g group default group
114 * -G grp1,grp2.. default additional groups
115 * -L class default login class
116 * -k dir default home skeleton
117 * -s shell default shell
118 * -w method default password method
122 pw_user(int mode
, struct cargs
* args
)
130 struct passwd
*pwd
= NULL
;
133 struct userconf
*cnf
;
134 char line
[_PASSWORD_LEN
+1];
135 char path
[MAXPATHLEN
];
140 static struct passwd fakeuser
=
152 #if defined(__FreeBSD__)
160 * With M_NEXT, we only need to return the
165 uid_t next
= pw_uidpolicy(cnf
, args
);
166 if (getarg(args
, 'q'))
169 pw_group(mode
, args
);
174 * We can do all of the common legwork here
177 if ((arg
= getarg(args
, 'b')) != NULL
) {
178 cnf
->home
= arg
->val
;
181 if ((arg
= getarg(args
, 'M')) != NULL
) {
183 if ((set
= setmode(dmode_c
)) == NULL
)
184 errx(EX_DATAERR
, "invalid directory creation mode '%s'",
186 cnf
->homemode
= getmode(set
, _DEF_DIRMODE
);
191 * If we'll need to use it or we're updating it,
192 * then create the base home directory if necessary
194 if (arg
!= NULL
|| getarg(args
, 'm') != NULL
) {
195 int l
= strlen(cnf
->home
);
197 if (l
> 1 && cnf
->home
[l
-1] == '/') /* Shave off any trailing path delimiter */
198 cnf
->home
[--l
] = '\0';
200 if (l
< 2 || *cnf
->home
!= '/') /* Check for absolute path name */
201 errx(EX_DATAERR
, "invalid base directory for home '%s'", cnf
->home
);
203 if (stat(cnf
->home
, &st
) == -1) {
204 char dbuf
[MAXPATHLEN
];
207 * This is a kludge especially for Joerg :)
208 * If the home directory would be created in the root partition, then
209 * we really create it under /usr which is likely to have more space.
210 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
212 if (strchr(cnf
->home
+1, '/') == NULL
) {
213 snprintf(dbuf
, MAXPATHLEN
, "/usr%s", cnf
->home
);
214 if (mkdir(dbuf
, _DEF_DIRMODE
) != -1 || errno
== EEXIST
) {
217 * Skip first "/" and create symlink:
220 symlink(dbuf
+1, cnf
->home
);
222 /* If this falls, fall back to old method */
224 strlcpy(dbuf
, cnf
->home
, sizeof(dbuf
));
226 if (stat(dbuf
, &st
) == -1) {
227 while ((p
= strchr(p
+ 1, '/')) != NULL
) {
229 if (stat(dbuf
, &st
) == -1) {
230 if (mkdir(dbuf
, _DEF_DIRMODE
) == -1)
233 } else if (!S_ISDIR(st
.st_mode
))
234 errx(EX_OSFILE
, "'%s' (root home parent) is not a directory", dbuf
);
238 if (stat(dbuf
, &st
) == -1) {
239 if (mkdir(dbuf
, _DEF_DIRMODE
) == -1) {
240 direrr
: err(EX_OSFILE
, "mkdir '%s'", dbuf
);
244 } else if (!S_ISDIR(st
.st_mode
))
245 errx(EX_OSFILE
, "root home `%s' is not a directory", cnf
->home
);
248 if ((arg
= getarg(args
, 'e')) != NULL
)
249 cnf
->expire_days
= atoi(arg
->val
);
251 if ((arg
= getarg(args
, 'y')) != NULL
)
252 cnf
->nispasswd
= arg
->val
;
254 if ((arg
= getarg(args
, 'p')) != NULL
&& arg
->val
)
255 cnf
->password_days
= atoi(arg
->val
);
257 if ((arg
= getarg(args
, 'g')) != NULL
) {
258 if (!*(p
= arg
->val
)) /* Handle empty group list specially */
259 cnf
->default_group
= "";
261 if ((grp
= GETGRNAM(p
)) == NULL
) {
262 if (!isdigit((unsigned char)*p
) || (grp
= GETGRGID((gid_t
) atoi(p
))) == NULL
)
263 errx(EX_NOUSER
, "group `%s' does not exist", p
);
265 cnf
->default_group
= newstr(grp
->gr_name
);
268 if ((arg
= getarg(args
, 'L')) != NULL
)
269 cnf
->default_class
= pw_checkname(arg
->val
, 0);
271 if ((arg
= getarg(args
, 'G')) != NULL
&& arg
->val
) {
274 for (p
= strtok(arg
->val
, ", \t"); p
!= NULL
; p
= strtok(NULL
, ", \t")) {
275 if ((grp
= GETGRNAM(p
)) == NULL
) {
276 if (!isdigit((unsigned char)*p
) || (grp
= GETGRGID((gid_t
) atoi(p
))) == NULL
)
277 errx(EX_NOUSER
, "group `%s' does not exist", p
);
279 if (extendarray(&cnf
->groups
, &cnf
->numgroups
, i
+ 2) != -1)
280 cnf
->groups
[i
++] = newstr(grp
->gr_name
);
282 while (i
< cnf
->numgroups
)
283 cnf
->groups
[i
++] = NULL
;
286 if ((arg
= getarg(args
, 'k')) != NULL
) {
287 if (stat(cnf
->dotdir
= arg
->val
, &st
) == -1 || !S_ISDIR(st
.st_mode
))
288 errx(EX_OSFILE
, "skeleton `%s' is not a directory or does not exist", cnf
->dotdir
);
291 if ((arg
= getarg(args
, 's')) != NULL
)
292 cnf
->shell_default
= arg
->val
;
294 if ((arg
= getarg(args
, 'w')) != NULL
)
295 cnf
->default_password
= boolean_val(arg
->val
, cnf
->default_password
);
296 if (mode
== M_ADD
&& getarg(args
, 'D')) {
297 if (getarg(args
, 'n') != NULL
)
298 errx(EX_DATAERR
, "can't combine `-D' with `-n name'");
299 if ((arg
= getarg(args
, 'u')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
300 if ((cnf
->min_uid
= (uid_t
) atoi(p
)) == 0)
302 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_uid
= (uid_t
) atoi(p
)) < cnf
->min_uid
)
303 cnf
->max_uid
= 32000;
305 if ((arg
= getarg(args
, 'i')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
306 if ((cnf
->min_gid
= (gid_t
) atoi(p
)) == 0)
308 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_gid
= (gid_t
) atoi(p
)) < cnf
->min_gid
)
309 cnf
->max_gid
= 32000;
312 arg
= getarg(args
, 'C');
313 if (write_userconfig(arg
? arg
->val
: NULL
))
315 err(EX_IOERR
, "config udpate");
318 if (mode
== M_PRINT
&& getarg(args
, 'a')) {
320 while ((pwd
= GETPWENT()) != NULL
)
326 if ((a_name
= getarg(args
, 'n')) != NULL
)
327 pwd
= GETPWNAM(pw_checkname(a_name
->val
, 0));
328 a_uid
= getarg(args
, 'u');
332 errx(EX_DATAERR
, "user name or id required");
335 * Determine whether 'n' switch is name or uid - we don't
336 * really don't really care which we have, but we need to
339 if (mode
!= M_ADD
&& pwd
== NULL
340 && strspn(a_name
->val
, "0123456789") == strlen(a_name
->val
)
342 (a_uid
= a_name
)->ch
= 'u';
346 if (strspn(a_uid
->val
, "0123456789") != strlen(a_uid
->val
))
347 errx(EX_USAGE
, "-u expects a number");
351 * Update, delete & print require that the user exists
353 if (mode
== M_UPDATE
|| mode
== M_DELETE
||
354 mode
== M_PRINT
|| mode
== M_LOCK
|| mode
== M_UNLOCK
) {
356 if (a_name
== NULL
&& pwd
== NULL
) /* Try harder */
357 pwd
= GETPWUID(atoi(a_uid
->val
));
360 if (mode
== M_PRINT
&& getarg(args
, 'F')) {
361 fakeuser
.pw_name
= a_name
? a_name
->val
: "nouser";
362 fakeuser
.pw_uid
= a_uid
? (uid_t
) atol(a_uid
->val
) : (uid_t
) -1;
363 return print_user(&fakeuser
);
366 errx(EX_NOUSER
, "no such uid `%s'", a_uid
->val
);
367 errx(EX_NOUSER
, "no such user `%s'", a_name
->val
);
370 if (a_name
== NULL
) /* May be needed later */
371 a_name
= addarg(args
, 'n', newstr(pwd
->pw_name
));
374 * The M_LOCK and M_UNLOCK functions simply add or remove
375 * a "*LOCKED*" prefix from in front of the password to
376 * prevent it decoding correctly, and therefore prevents
377 * access. Of course, this only prevents access via
378 * password authentication (not ssh, kerberos or any
379 * other method that does not use the UNIX password) but
380 * that is a known limitation.
383 if (mode
== M_LOCK
) {
384 if (strncmp(pwd
->pw_passwd
, locked_str
, sizeof(locked_str
)-1) == 0)
385 errx(EX_DATAERR
, "user '%s' is already locked", pwd
->pw_name
);
386 asprintf(&passtmp
, "%s%s", locked_str
, pwd
->pw_passwd
);
387 if (passtmp
== NULL
) /* disaster */
388 errx(EX_UNAVAILABLE
, "out of memory");
389 pwd
->pw_passwd
= passtmp
;
391 } else if (mode
== M_UNLOCK
) {
392 if (strncmp(pwd
->pw_passwd
, locked_str
, sizeof(locked_str
)-1) != 0)
393 errx(EX_DATAERR
, "user '%s' is not locked", pwd
->pw_name
);
394 pwd
->pw_passwd
+= sizeof(locked_str
)-1;
396 } else if (mode
== M_DELETE
)
397 return (delete_user(cnf
, pwd
, a_name
,
398 getarg(args
, 'r') != NULL
, mode
));
399 else if (mode
== M_PRINT
)
400 return print_user(pwd
);
403 * The rest is edit code
405 if ((arg
= getarg(args
, 'l')) != NULL
) {
406 if (strcmp(pwd
->pw_name
, "root") == 0)
407 errx(EX_DATAERR
, "can't rename `root' account");
408 pwd
->pw_name
= pw_checkname(arg
->val
, 0);
412 if ((arg
= getarg(args
, 'u')) != NULL
&& isdigit((unsigned char)*arg
->val
)) {
413 pwd
->pw_uid
= (uid_t
) atol(arg
->val
);
415 if (pwd
->pw_uid
!= 0 && strcmp(pwd
->pw_name
, "root") == 0)
416 errx(EX_DATAERR
, "can't change uid of `root' account");
417 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
418 warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd
->pw_name
);
421 if ((arg
= getarg(args
, 'g')) != NULL
&& pwd
->pw_uid
!= 0) { /* Already checked this */
422 gid_t newgid
= (gid_t
) GETGRNAM(cnf
->default_group
)->gr_gid
;
423 if (newgid
!= pwd
->pw_gid
) {
425 pwd
->pw_gid
= newgid
;
429 if ((arg
= getarg(args
, 'p')) != NULL
) {
430 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0) {
431 if (pwd
->pw_change
!= 0) {
437 time_t now
= time(NULL
);
438 time_t expire
= parse_date(now
, arg
->val
);
440 if (pwd
->pw_change
!= expire
) {
441 pwd
->pw_change
= expire
;
447 if ((arg
= getarg(args
, 'e')) != NULL
) {
448 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0) {
449 if (pwd
->pw_expire
!= 0) {
455 time_t now
= time(NULL
);
456 time_t expire
= parse_date(now
, arg
->val
);
458 if (pwd
->pw_expire
!= expire
) {
459 pwd
->pw_expire
= expire
;
465 if ((arg
= getarg(args
, 's')) != NULL
) {
466 char *shell
= shell_path(cnf
->shelldir
, cnf
->shells
, arg
->val
);
469 if (strcmp(shell
, pwd
->pw_shell
) != 0) {
470 pwd
->pw_shell
= shell
;
475 if (getarg(args
, 'L')) {
476 if (cnf
->default_class
== NULL
)
477 cnf
->default_class
= "";
478 if (strcmp(pwd
->pw_class
, cnf
->default_class
) != 0) {
479 pwd
->pw_class
= cnf
->default_class
;
484 if ((arg
= getarg(args
, 'd')) != NULL
) {
485 if (strcmp(pwd
->pw_dir
, arg
->val
))
487 if (stat(pwd
->pw_dir
= arg
->val
, &st
) == -1) {
488 if (getarg(args
, 'm') == NULL
&& strcmp(pwd
->pw_dir
, "/nonexistent") != 0)
489 warnx("WARNING: home `%s' does not exist", pwd
->pw_dir
);
490 } else if (!S_ISDIR(st
.st_mode
))
491 warnx("WARNING: home `%s' is not a directory", pwd
->pw_dir
);
494 if ((arg
= getarg(args
, 'w')) != NULL
&&
495 getarg(args
, 'h') == NULL
&& getarg(args
, 'H') == NULL
) {
498 lc
= login_getpwclass(pwd
);
500 login_setcryptfmt(lc
, "sha512", NULL
) == NULL
)
501 warn("setting crypt(3) format");
503 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
514 if (a_name
== NULL
) /* Required */
515 errx(EX_DATAERR
, "login name required");
516 else if ((pwd
= GETPWNAM(a_name
->val
)) != NULL
) /* Exists */
517 errx(EX_DATAERR
, "login name `%s' already exists", a_name
->val
);
520 * Now, set up defaults for a new user
523 pwd
->pw_name
= a_name
->val
;
524 pwd
->pw_class
= cnf
->default_class
? cnf
->default_class
: "";
525 pwd
->pw_uid
= pw_uidpolicy(cnf
, args
);
526 pwd
->pw_gid
= pw_gidpolicy(args
, pwd
->pw_name
, (gid_t
) pwd
->pw_uid
);
527 pwd
->pw_change
= pw_pwdpolicy(cnf
, args
);
528 pwd
->pw_expire
= pw_exppolicy(cnf
, args
);
529 pwd
->pw_dir
= pw_homepolicy(cnf
, args
, pwd
->pw_name
);
530 pwd
->pw_shell
= pw_shellpolicy(cnf
, args
, NULL
);
531 lc
= login_getpwclass(pwd
);
532 if (lc
== NULL
|| login_setcryptfmt(lc
, "sha512", NULL
) == NULL
)
533 warn("setting crypt(3) format");
535 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
538 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
539 warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd
->pw_name
);
543 * Shared add/edit code
545 if ((arg
= getarg(args
, 'c')) != NULL
) {
546 char *gecos
= pw_checkname(arg
->val
, 1);
547 if (strcmp(pwd
->pw_gecos
, gecos
) != 0) {
548 pwd
->pw_gecos
= gecos
;
553 if ((arg
= getarg(args
, 'h')) != NULL
||
554 (arg
= getarg(args
, 'H')) != NULL
) {
555 if (strcmp(arg
->val
, "-") == 0) {
556 if (!pwd
->pw_passwd
|| *pwd
->pw_passwd
!= '*') {
557 pwd
->pw_passwd
= "*"; /* No access */
561 int fd
= atoi(arg
->val
);
562 int precrypt
= (arg
->ch
== 'H');
564 int istty
= isatty(fd
);
569 if (tcgetattr(fd
, &t
) == -1)
572 struct termios n
= t
;
575 n
.c_lflag
&= ~(ECHO
);
576 tcsetattr(fd
, TCSANOW
, &n
);
577 printf("%s%spassword for user %s:",
578 (mode
== M_UPDATE
) ? "new " : "",
579 precrypt
? "encrypted " : "",
584 b
= read(fd
, line
, sizeof(line
) - 1);
585 if (istty
) { /* Restore state */
586 tcsetattr(fd
, TCSANOW
, &t
);
591 err(EX_IOERR
, "-%c file descriptor",
592 precrypt
? 'H' : 'h');
594 if ((p
= strpbrk(line
, "\r\n")) != NULL
)
597 errx(EX_DATAERR
, "empty password read on file descriptor %d", fd
);
599 if (strchr(line
, ':') != NULL
)
601 pwd
->pw_passwd
= line
;
603 lc
= login_getpwclass(pwd
);
605 login_setcryptfmt(lc
, "sha512", NULL
) == NULL
)
606 warn("setting crypt(3) format");
608 pwd
->pw_passwd
= pw_pwcrypt(line
);
615 * Special case: -N only displays & exits
618 return print_user(pwd
);
621 edited
= 1; /* Always */
624 errx(EX_IOERR
, "user '%s' already exists",
627 err(EX_IOERR
, "passwd file update");
628 if (cnf
->nispasswd
&& *cnf
->nispasswd
=='/') {
629 rc
= addnispwent(cnf
->nispasswd
, pwd
);
631 warnx("User '%s' already exists in NIS passwd", pwd
->pw_name
);
633 warn("NIS passwd update");
634 /* NOTE: we treat NIS-only update errors as non-fatal */
636 } else if (mode
== M_UPDATE
|| mode
== M_LOCK
|| mode
== M_UNLOCK
) {
637 if (edited
) { /* Only updated this if required */
638 rc
= chgpwent(a_name
->val
, pwd
);
640 errx(EX_IOERR
, "user '%s' does not exist (NIS?)", pwd
->pw_name
);
642 err(EX_IOERR
, "passwd file update");
643 if ( cnf
->nispasswd
&& *cnf
->nispasswd
=='/') {
644 rc
= chgnispwent(cnf
->nispasswd
, a_name
->val
, pwd
);
646 warn("User '%s' not found in NIS passwd", pwd
->pw_name
);
648 warn("NIS passwd update");
649 /* NOTE: NIS-only update errors are not fatal */
655 * Ok, user is created or changed - now edit group file
658 if (mode
== M_ADD
|| getarg(args
, 'G') != NULL
) {
660 /* First remove the user from all group */
662 while ((grp
= GETGRENT()) != NULL
) {
663 char group
[MAXLOGNAME
];
664 if (grp
->gr_mem
== NULL
)
666 for (i
= 0; grp
->gr_mem
[i
] != NULL
; i
++) {
667 if (strcmp(grp
->gr_mem
[i
] , pwd
->pw_name
) != 0)
669 for (j
= i
; grp
->gr_mem
[j
] != NULL
; j
++)
670 grp
->gr_mem
[j
] = grp
->gr_mem
[j
+1];
671 strlcpy(group
, grp
->gr_name
, MAXLOGNAME
);
672 chggrent(group
, grp
);
677 /* now add to group where needed */
678 for (i
= 0; cnf
->groups
[i
] != NULL
; i
++) {
679 grp
= GETGRNAM(cnf
->groups
[i
]);
680 grp
= gr_add(grp
, pwd
->pw_name
);
682 * grp can only be NULL in 2 cases:
683 * - the new member is already a member
684 * - a problem with memory occurs
685 * in both cases we want to skip now.
689 chggrent(cnf
->groups
[i
], grp
);
695 /* go get a current version of pwd */
696 pwd
= GETPWNAM(a_name
->val
);
698 /* This will fail when we rename, so special case that */
699 if (mode
== M_UPDATE
&& (arg
= getarg(args
, 'l')) != NULL
) {
700 a_name
->val
= arg
->val
; /* update new name */
701 pwd
= GETPWNAM(a_name
->val
); /* refetch renamed rec */
704 if (pwd
== NULL
) /* can't go on without this */
705 errx(EX_NOUSER
, "user '%s' disappeared during update", a_name
->val
);
707 grp
= GETGRGID(pwd
->pw_gid
);
708 pw_log(cnf
, mode
, W_USER
, "%s(%u):%s(%u):%s:%s:%s",
709 pwd
->pw_name
, pwd
->pw_uid
,
710 grp
? grp
->gr_name
: "unknown", (grp
? grp
->gr_gid
: (uid_t
)-1),
711 pwd
->pw_gecos
, pwd
->pw_dir
, pwd
->pw_shell
);
714 * If adding, let's touch and chown the user's mail file. This is not
715 * strictly necessary under BSD with a 0755 maildir but it also
716 * doesn't hurt anything to create the empty mailfile
719 if (PWALTDIR() != PWF_ALT
) {
720 arg
= getarg(args
, 'R');
721 snprintf(path
, sizeof(path
), "%s%s/%s",
722 arg
? arg
->val
: "", _PATH_MAILDIR
, pwd
->pw_name
);
723 close(open(path
, O_RDWR
| O_CREAT
, 0600)); /* Preserve contents &
725 chown(path
, pwd
->pw_uid
, pwd
->pw_gid
);
730 * Let's create and populate the user's home directory. Note
731 * that this also `works' for editing users if -m is used, but
732 * existing files will *not* be overwritten.
734 if (PWALTDIR() != PWF_ALT
&& getarg(args
, 'm') != NULL
&& pwd
->pw_dir
&&
735 *pwd
->pw_dir
== '/' && pwd
->pw_dir
[1])
736 create_and_populate_homedir(mode
, pwd
);
739 * Finally, send mail to the new user as well, if we are asked to
741 if (mode
== M_ADD
&& !PWALTDIR() && cnf
->newmail
&& *cnf
->newmail
&& (fp
= fopen(cnf
->newmail
, "r")) != NULL
) {
742 FILE *pfp
= popen(_PATH_SENDMAIL
" -t", "w");
747 fprintf(pfp
, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd
->pw_name
);
748 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
749 /* Do substitutions? */
753 pw_log(cnf
, mode
, W_USER
, "%s(%u) new user mail sent",
754 pwd
->pw_name
, pwd
->pw_uid
);
764 pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
)
767 uid_t uid
= (uid_t
) - 1;
768 struct carg
*a_uid
= getarg(args
, 'u');
771 * Check the given uid, if any
774 uid
= (uid_t
) atol(a_uid
->val
);
776 if ((pwd
= GETPWUID(uid
)) != NULL
&& getarg(args
, 'o') == NULL
)
777 errx(EX_DATAERR
, "uid `%u' has already been allocated", pwd
->pw_uid
);
782 * We need to allocate the next available uid under one of
783 * two policies a) Grab the first unused uid b) Grab the
784 * highest possible unused uid
786 if (cnf
->min_uid
>= cnf
->max_uid
) { /* Sanity
787 * claus^H^H^H^Hheck */
789 cnf
->max_uid
= 32000;
791 bm
= bm_alloc(cnf
->max_uid
- cnf
->min_uid
+ 1);
794 * Now, let's fill the bitmap from the password file
797 while ((pwd
= GETPWENT()) != NULL
)
798 if (pwd
->pw_uid
>= (uid_t
) cnf
->min_uid
&& pwd
->pw_uid
<= (uid_t
) cnf
->max_uid
)
799 bm_setbit(&bm
, pwd
->pw_uid
- cnf
->min_uid
);
803 * Then apply the policy, with fallback to reuse if necessary
805 if (cnf
->reuse_uids
|| (uid
= (uid_t
) (bm_lastset(&bm
) + cnf
->min_uid
+ 1)) > cnf
->max_uid
)
806 uid
= (uid_t
) (bm_firstunset(&bm
) + cnf
->min_uid
);
809 * Another sanity check
811 if (uid
< cnf
->min_uid
|| uid
> cnf
->max_uid
)
812 errx(EX_SOFTWARE
, "unable to allocate a new uid - range fully used");
820 pw_gidpolicy(struct cargs
* args
, char *nam
, gid_t prefer
)
823 gid_t gid
= (uid_t
) - 1;
824 struct carg
*a_gid
= getarg(args
, 'g');
825 struct userconf
*cnf
= conf
.userconf
;
828 * If no arg given, see if default can help out
830 if (a_gid
== NULL
&& cnf
->default_group
&& *cnf
->default_group
)
831 a_gid
= addarg(args
, 'g', cnf
->default_group
);
834 * Check the given gid, if any
838 if ((grp
= GETGRNAM(a_gid
->val
)) == NULL
) {
839 gid
= (gid_t
) atol(a_gid
->val
);
840 if ((gid
== 0 && !isdigit((unsigned char)*a_gid
->val
)) || (grp
= GETGRGID(gid
)) == NULL
)
841 errx(EX_NOUSER
, "group `%s' is not defined", a_gid
->val
);
844 } else if ((grp
= GETGRNAM(nam
)) != NULL
&&
845 (grp
->gr_mem
== NULL
|| grp
->gr_mem
[0] == NULL
)) {
846 gid
= grp
->gr_gid
; /* Already created? Use it anyway... */
848 struct cargs grpargs
;
852 addarg(&grpargs
, 'n', nam
);
855 * We need to auto-create a group with the user's name. We
856 * can send all the appropriate output to our sister routine
857 * bit first see if we can create a group with gid==uid so we
858 * can keep the user and group ids in sync. We purposely do
859 * NOT check the gid range if we can force the sync. If the
860 * user's name dups an existing group, then the group add
861 * function will happily handle that case for us and exit.
863 if (GETGRGID(prefer
) == NULL
) {
864 snprintf(tmp
, sizeof(tmp
), "%u", prefer
);
865 addarg(&grpargs
, 'g', tmp
);
868 addarg(&grpargs
, 'q', NULL
);
869 gid
= pw_group(M_NEXT
, &grpargs
);
873 pw_group(M_ADD
, &grpargs
);
874 if ((grp
= GETGRNAM(nam
)) != NULL
)
877 a_gid
= LIST_FIRST(&grpargs
);
878 while (a_gid
!= NULL
) {
879 struct carg
*t
= LIST_NEXT(a_gid
, list
);
880 LIST_REMOVE(a_gid
, list
);
890 pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
)
893 time_t now
= time(NULL
);
894 struct carg
*arg
= getarg(args
, 'p');
897 if ((result
= parse_date(now
, arg
->val
)) == now
)
898 errx(EX_DATAERR
, "invalid date/time `%s'", arg
->val
);
899 } else if (cnf
->password_days
> 0)
900 result
= now
+ ((long) cnf
->password_days
* 86400L);
906 pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
)
909 time_t now
= time(NULL
);
910 struct carg
*arg
= getarg(args
, 'e');
913 if ((result
= parse_date(now
, arg
->val
)) == now
)
914 errx(EX_DATAERR
, "invalid date/time `%s'", arg
->val
);
915 } else if (cnf
->expire_days
> 0)
916 result
= now
+ ((long) cnf
->expire_days
* 86400L);
922 pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
924 struct carg
*arg
= getarg(args
, 'd');
925 static char home
[128];
930 if (cnf
->home
== NULL
|| *cnf
->home
== '\0')
931 errx(EX_CONFIG
, "no base home directory set");
932 snprintf(home
, sizeof(home
), "%s/%s", cnf
->home
, user
);
938 shell_path(char const * path
, char *shells
[], char *sh
)
940 if (sh
!= NULL
&& (*sh
== '/' || *sh
== '\0'))
941 return sh
; /* specified full path or forced none */
944 char paths
[_UC_MAXLINE
];
947 * We need to search paths
949 strlcpy(paths
, path
, sizeof(paths
));
950 for (p
= strtok(paths
, ": \t\r\n"); p
!= NULL
; p
= strtok(NULL
, ": \t\r\n")) {
952 static char shellpath
[256];
955 snprintf(shellpath
, sizeof(shellpath
), "%s/%s", p
, sh
);
956 if (access(shellpath
, X_OK
) == 0)
959 for (i
= 0; i
< _UC_MAXSHELLS
&& shells
[i
] != NULL
; i
++) {
960 snprintf(shellpath
, sizeof(shellpath
), "%s/%s", p
, shells
[i
]);
961 if (access(shellpath
, X_OK
) == 0)
966 errx(EX_OSFILE
, "can't find shell `%s' in shell paths", sh
);
967 errx(EX_CONFIG
, "no default shell available or defined");
974 pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
)
977 struct carg
*arg
= getarg(args
, 's');
979 if (newshell
== NULL
&& arg
!= NULL
)
981 return shell_path(cnf
->shelldir
, cnf
->shells
, sh
? sh
: cnf
->shell_default
);
986 static char const chars
[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
989 pw_pwcrypt(char *password
)
992 char salt
[SALTSIZE
+ 1];
995 static char buf
[256];
998 * Calculate a salt value
1000 for (i
= 0; i
< SALTSIZE
; i
++)
1001 salt
[i
] = chars
[arc4random_uniform(sizeof(chars
) - 1)];
1002 salt
[SALTSIZE
] = '\0';
1004 cryptpw
= crypt(password
, salt
);
1005 if (cryptpw
== NULL
)
1006 errx(EX_CONFIG
, "crypt(3) failure");
1007 return strcpy(buf
, cryptpw
);
1012 pw_password(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
1017 switch (cnf
->default_password
) {
1018 case -1: /* Random password */
1019 l
= (arc4random() % 8 + 8); /* 8 - 16 chars */
1020 for (i
= 0; i
< l
; i
++)
1021 pwbuf
[i
] = chars
[arc4random_uniform(sizeof(chars
)-1)];
1025 * We give this information back to the user
1027 if (getarg(args
, 'h') == NULL
&& getarg(args
, 'H') == NULL
&&
1029 if (isatty(STDOUT_FILENO
))
1030 printf("Password for '%s' is: ", user
);
1031 printf("%s\n", pwbuf
);
1036 case -2: /* No password at all! */
1039 case 0: /* No login - default */
1043 case 1: /* user's name */
1044 strlcpy(pwbuf
, user
, sizeof(pwbuf
));
1047 return pw_pwcrypt(pwbuf
);
1051 delete_user(struct userconf
*cnf
, struct passwd
*pwd
, struct carg
*a_name
,
1052 int delete, int mode
)
1054 char file
[MAXPATHLEN
];
1055 char home
[MAXPATHLEN
];
1056 uid_t uid
= pwd
->pw_uid
;
1057 struct group
*gr
, *grp
;
1058 char grname
[LOGNAMESIZE
];
1062 if (strcmp(pwd
->pw_name
, "root") == 0)
1063 errx(EX_DATAERR
, "cannot remove user 'root'");
1067 * Remove opie record from /etc/opiekeys
1070 rmopie(pwd
->pw_name
);
1075 snprintf(file
, sizeof(file
), "/var/cron/tabs/%s", pwd
->pw_name
);
1076 if (access(file
, F_OK
) == 0) {
1077 snprintf(file
, sizeof(file
), "crontab -u %s -r", pwd
->pw_name
);
1082 * Save these for later, since contents of pwd may be
1083 * invalidated by deletion
1085 snprintf(file
, sizeof(file
), "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
1086 strlcpy(home
, pwd
->pw_dir
, sizeof(home
));
1087 gr
= GETGRGID(pwd
->pw_gid
);
1089 strlcpy(grname
, gr
->gr_name
, LOGNAMESIZE
);
1095 err(EX_IOERR
, "user '%s' does not exist", pwd
->pw_name
);
1097 err(EX_IOERR
, "passwd update");
1099 if (cnf
->nispasswd
&& *cnf
->nispasswd
=='/') {
1100 rc
= delnispwent(cnf
->nispasswd
, a_name
->val
);
1102 warnx("WARNING: user '%s' does not exist in NIS passwd", pwd
->pw_name
);
1104 warn("WARNING: NIS passwd update");
1108 grp
= GETGRNAM(a_name
->val
);
1110 (grp
->gr_mem
== NULL
|| *grp
->gr_mem
== NULL
) &&
1111 strcmp(a_name
->val
, grname
) == 0)
1112 delgrent(GETGRNAM(a_name
->val
));
1114 while ((grp
= GETGRENT()) != NULL
) {
1116 char group
[MAXLOGNAME
];
1117 if (grp
->gr_mem
== NULL
)
1120 for (i
= 0; grp
->gr_mem
[i
] != NULL
; i
++) {
1121 if (strcmp(grp
->gr_mem
[i
], a_name
->val
) != 0)
1124 for (j
= i
; grp
->gr_mem
[j
] != NULL
; j
++)
1125 grp
->gr_mem
[j
] = grp
->gr_mem
[j
+1];
1126 strlcpy(group
, grp
->gr_name
, MAXLOGNAME
);
1127 chggrent(group
, grp
);
1132 pw_log(cnf
, mode
, W_USER
, "%s(%u) account removed", a_name
->val
, uid
);
1143 if (getpwuid(uid
) == NULL
)
1147 * Remove home directory and contents
1149 if (delete && *home
== '/' && getpwuid(uid
) == NULL
&&
1150 stat(home
, &st
) != -1) {
1152 pw_log(cnf
, mode
, W_USER
, "%s(%u) home '%s' %sremoved",
1153 a_name
->val
, uid
, home
,
1154 stat(home
, &st
) == -1 ? "" : "not completely ");
1158 return (EXIT_SUCCESS
);
1162 print_user(struct passwd
* pwd
)
1168 pwd
->pw_passwd
= (pwd
->pw_passwd
== NULL
) ? "" : "*";
1170 buf
= conf
.v7
? pw_make_v7(pwd
) : pw_make(pwd
);
1171 printf("%s\n", buf
);
1176 struct group
*grp
= GETGRGID(pwd
->pw_gid
);
1177 char uname
[60] = "User &", office
[60] = "[None]",
1178 wphone
[60] = "[None]", hphone
[60] = "[None]";
1179 char acexpire
[32] = "[None]", pwexpire
[32] = "[None]";
1182 if ((p
= strtok(pwd
->pw_gecos
, ",")) != NULL
) {
1183 strlcpy(uname
, p
, sizeof(uname
));
1184 if ((p
= strtok(NULL
, ",")) != NULL
) {
1185 strlcpy(office
, p
, sizeof(office
));
1186 if ((p
= strtok(NULL
, ",")) != NULL
) {
1187 strlcpy(wphone
, p
, sizeof(wphone
));
1188 if ((p
= strtok(NULL
, "")) != NULL
) {
1196 * Handle '&' in gecos field
1198 if ((p
= strchr(uname
, '&')) != NULL
) {
1199 int l
= strlen(pwd
->pw_name
);
1202 memmove(p
+ l
, p
+ 1, m
);
1203 memmove(p
, pwd
->pw_name
, l
);
1204 *p
= (char) toupper((unsigned char)*p
);
1206 if (pwd
->pw_expire
> (time_t)0 && (tptr
= localtime(&pwd
->pw_expire
)) != NULL
)
1207 strftime(acexpire
, sizeof acexpire
, "%c", tptr
);
1208 if (pwd
->pw_change
> (time_t)0 && (tptr
= localtime(&pwd
->pw_change
)) != NULL
)
1209 strftime(pwexpire
, sizeof pwexpire
, "%c", tptr
);
1210 printf("Login Name: %-15s #%-12u Group: %-15s #%u\n"
1212 " Home: %-26.26s Class: %s\n"
1213 " Shell: %-26.26s Office: %s\n"
1214 "Work Phone: %-26.26s Home Phone: %s\n"
1215 "Acc Expire: %-26.26s Pwd Expire: %s\n",
1216 pwd
->pw_name
, pwd
->pw_uid
,
1217 grp
? grp
->gr_name
: "(invalid)", pwd
->pw_gid
,
1218 uname
, pwd
->pw_dir
, pwd
->pw_class
,
1219 pwd
->pw_shell
, office
, wphone
, hphone
,
1220 acexpire
, pwexpire
);
1223 while ((grp
=GETGRENT()) != NULL
)
1226 if (grp
->gr_mem
!= NULL
) {
1227 while (grp
->gr_mem
[i
] != NULL
)
1229 if (strcmp(grp
->gr_mem
[i
], pwd
->pw_name
)==0)
1231 printf(j
++ == 0 ? " Groups: %s" : ",%s", grp
->gr_name
);
1239 printf("%s", j
? "\n" : "");
1241 return EXIT_SUCCESS
;
1245 pw_checkname(char *name
, int gecos
)
1248 const char *badchars
, *ch
, *showtype
;
1254 /* See if the name is valid as a gecos (comment) field. */
1256 showtype
= "gecos field";
1258 /* See if the name is valid as a userid or group. */
1259 badchars
= " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1260 showtype
= "userid/group name";
1261 /* Userids and groups can not have a leading '-'. */
1267 if (strchr(badchars
, *ch
) != NULL
|| *ch
< ' ' ||
1272 /* 8-bit characters are only allowed in GECOS fields */
1273 if (!gecos
&& (*ch
& 0x80)) {
1281 * A `$' is allowed as the final character for userids and groups,
1282 * mainly for the benefit of samba.
1284 if (reject
&& !gecos
) {
1285 if (*ch
== '$' && *(ch
+ 1) == '\0') {
1291 snprintf(showch
, sizeof(showch
), (*ch
>= ' ' && *ch
< 127)
1292 ? "`%c'" : "0x%02x", *ch
);
1293 errx(EX_DATAERR
, "invalid character %s at position %td in %s",
1294 showch
, (ch
- name
), showtype
);
1296 if (!gecos
&& (ch
- name
) > LOGNAMESIZE
)
1297 errx(EX_DATAERR
, "name too long `%s' (max is %d)", name
,
1307 DIR *d
= opendir("/var/at/jobs");
1312 while ((e
= readdir(d
)) != NULL
) {
1315 if (strncmp(e
->d_name
, ".lock", 5) != 0 &&
1316 stat(e
->d_name
, &st
) == 0 &&
1317 !S_ISDIR(st
.st_mode
) &&
1319 char tmp
[MAXPATHLEN
];
1321 snprintf(tmp
, sizeof(tmp
), "/usr/bin/atrm %s", e
->d_name
);
1330 rmopie(char const * name
)
1332 static const char etcopie
[] = "/etc/opiekeys";
1333 FILE *fp
= fopen(etcopie
, "r+");
1338 int length
= strlen(name
);
1340 while (fgets(tmp
, sizeof tmp
, fp
) != NULL
) {
1341 if (strncmp(name
, tmp
, length
) == 0 && tmp
[length
]==' ') {
1342 if (fseek(fp
, atofs
, SEEK_SET
) == 0) {
1343 fwrite("#", 1, 1, fp
); /* Comment username out */
1350 * If we got an error of any sort, don't update!