]>
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 pw_userdel(char *name
, long id
);
55 static int print_user(struct passwd
* pwd
);
56 static uid_t
pw_uidpolicy(struct userconf
* cnf
, long id
);
57 static uid_t
pw_gidpolicy(struct cargs
* args
, char *nam
, gid_t prefer
);
58 static time_t pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
);
59 static time_t pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
);
60 static char *pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
);
61 static char *pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
);
62 static char *pw_password(struct userconf
* cnf
, char const * user
);
63 static char *shell_path(char const * path
, char *shells
[], char *sh
);
64 static void rmat(uid_t uid
);
65 static void rmopie(char const * name
);
68 create_and_populate_homedir(struct passwd
*pwd
)
70 char *homedir
, *dotdir
;
71 struct userconf
*cnf
= conf
.userconf
;
73 homedir
= dotdir
= NULL
;
75 if (conf
.rootdir
[0] != '\0') {
76 asprintf(&homedir
, "%s/%s", conf
.rootdir
, pwd
->pw_dir
);
78 errx(EX_OSERR
, "out of memory");
79 asprintf(&dotdir
, "%s/%s", conf
.rootdir
, cnf
->dotdir
);
82 copymkdir(homedir
? homedir
: pwd
->pw_dir
, dotdir
? dotdir
: cnf
->dotdir
,
83 cnf
->homemode
, pwd
->pw_uid
, pwd
->pw_gid
);
84 pw_log(cnf
, M_ADD
, W_USER
, "%s(%u) home %s made", pwd
->pw_name
,
85 pwd
->pw_uid
, pwd
->pw_dir
);
89 set_passwd(struct passwd
*pwd
, bool update
)
94 char line
[_PASSWORD_LEN
+1];
98 if (!pwd
->pw_passwd
|| *pwd
->pw_passwd
!= '*') {
99 pwd
->pw_passwd
= "*"; /* No access */
105 if ((istty
= isatty(conf
.fd
))) {
106 if (tcgetattr(conf
.fd
, &t
) == -1)
110 n
.c_lflag
&= ~(ECHO
);
111 tcsetattr(conf
.fd
, TCSANOW
, &n
);
112 printf("%s%spassword for user %s:",
113 update
? "new " : "",
114 conf
.precrypted
? "encrypted " : "",
119 b
= read(conf
.fd
, line
, sizeof(line
) - 1);
120 if (istty
) { /* Restore state */
121 tcsetattr(conf
.fd
, TCSANOW
, &t
);
127 err(EX_IOERR
, "-%c file descriptor",
128 conf
.precrypted
? 'H' : 'h');
130 if ((p
= strpbrk(line
, "\r\n")) != NULL
)
133 errx(EX_DATAERR
, "empty password read on file descriptor %d",
135 if (conf
.precrypted
) {
136 if (strchr(line
, ':') != NULL
)
137 errx(EX_DATAERR
, "bad encrypted password");
138 pwd
->pw_passwd
= line
;
140 lc
= login_getpwclass(pwd
);
142 login_setcryptfmt(lc
, "sha512", NULL
) == NULL
)
143 warn("setting crypt(3) format");
145 pwd
->pw_passwd
= pw_pwcrypt(line
);
151 pw_usernext(struct userconf
*cnf
, bool quiet
)
153 uid_t next
= pw_uidpolicy(cnf
, -1);
159 pw_groupnext(cnf
, quiet
);
161 return (EXIT_SUCCESS
);
165 pw_usershow(char *name
, long id
, struct passwd
*fakeuser
)
167 struct passwd
*pwd
= NULL
;
169 if (id
< 0 && name
== NULL
&& !conf
.all
)
170 errx(EX_DATAERR
, "username or id or '-a' required");
174 while ((pwd
= GETPWENT()) != NULL
)
177 return (EXIT_SUCCESS
);
180 pwd
= (name
!= NULL
) ? GETPWNAM(pw_checkname(name
, 0)) : GETPWUID(id
);
186 errx(EX_NOUSER
, "no such uid `%ld'", id
);
187 errx(EX_NOUSER
, "no such user `%s'", name
);
191 return (print_user(pwd
));
195 perform_chgpwent(const char *name
, struct passwd
*pwd
)
199 rc
= chgpwent(name
, pwd
);
201 errx(EX_IOERR
, "user '%s' does not exist (NIS?)", pwd
->pw_name
);
203 err(EX_IOERR
, "passwd file update");
205 if (conf
.userconf
->nispasswd
&& *conf
.userconf
->nispasswd
== '/') {
206 rc
= chgnispwent(conf
.userconf
->nispasswd
, name
, pwd
);
208 warn("User '%s' not found in NIS passwd", pwd
->pw_name
);
210 warn("NIS passwd update");
211 /* NOTE: NIS-only update errors are not fatal */
216 * The M_LOCK and M_UNLOCK functions simply add or remove
217 * a "*LOCKED*" prefix from in front of the password to
218 * prevent it decoding correctly, and therefore prevents
219 * access. Of course, this only prevents access via
220 * password authentication (not ssh, kerberos or any
221 * other method that does not use the UNIX password) but
222 * that is a known limitation.
225 pw_userlock(char *name
, long id
, int mode
)
227 struct passwd
*pwd
= NULL
;
228 char *passtmp
= NULL
;
231 if (id
< 0 && name
== NULL
)
232 errx(EX_DATAERR
, "username or id required");
234 pwd
= (name
!= NULL
) ? GETPWNAM(pw_checkname(name
, 0)) : GETPWUID(id
);
237 errx(EX_NOUSER
, "no such uid `%ld'", id
);
238 errx(EX_NOUSER
, "no such user `%s'", name
);
244 if (strncmp(pwd
->pw_passwd
, locked_str
, sizeof(locked_str
) -1) == 0)
246 if (mode
== M_LOCK
&& locked
)
247 errx(EX_DATAERR
, "user '%s' is already locked", pwd
->pw_name
);
248 if (mode
== M_UNLOCK
&& !locked
)
249 errx(EX_DATAERR
, "user '%s' is not locked", pwd
->pw_name
);
251 if (mode
== M_LOCK
) {
252 asprintf(&passtmp
, "%s%s", locked_str
, pwd
->pw_passwd
);
253 if (passtmp
== NULL
) /* disaster */
254 errx(EX_UNAVAILABLE
, "out of memory");
255 pwd
->pw_passwd
= passtmp
;
257 pwd
->pw_passwd
+= sizeof(locked_str
)-1;
260 perform_chgpwent(name
, pwd
);
263 return (EXIT_SUCCESS
);
267 * -C config configuration file
271 * -c comment user name/comment
272 * -d directory home directory
273 * -e date account expiry date
274 * -p date password expiry date
275 * -g grp primary group
276 * -G grp1,grp2 additional groups
277 * -m [ -k dir ] create and set up home
278 * -s shell name of login shell
279 * -o duplicate uid ok
280 * -L class user class
281 * -l name new login name
282 * -h fd password filehandle
283 * -H fd encrypted password filehandle
284 * -F force print or add
286 * -D set user defaults
287 * -b dir default home root dir
288 * -e period default expiry period
289 * -p period default password change period
290 * -g group default group
291 * -G grp1,grp2.. default additional groups
292 * -L class default login class
293 * -k dir default home skeleton
294 * -s shell default shell
295 * -w method default password method
299 pw_user(int mode
, char *name
, long id
, struct cargs
* args
)
304 struct passwd
*pwd
= NULL
;
307 struct userconf
*cnf
;
308 char line
[_PASSWORD_LEN
+1];
309 char path
[MAXPATHLEN
];
314 static struct passwd fakeuser
=
326 #if defined(__FreeBSD__)
334 return (pw_usernext(cnf
, conf
.quiet
));
337 return (pw_usershow(name
, id
, &fakeuser
));
339 if (mode
== M_DELETE
)
340 return (pw_userdel(name
, id
));
342 if (mode
== M_LOCK
|| mode
== M_UNLOCK
)
343 return (pw_userlock(name
, id
, mode
));
346 * We can do all of the common legwork here
349 if ((arg
= getarg(args
, 'b')) != NULL
) {
350 cnf
->home
= arg
->val
;
353 if ((arg
= getarg(args
, 'M')) != NULL
) {
355 if ((set
= setmode(dmode_c
)) == NULL
)
356 errx(EX_DATAERR
, "invalid directory creation mode '%s'",
358 cnf
->homemode
= getmode(set
, _DEF_DIRMODE
);
363 * If we'll need to use it or we're updating it,
364 * then create the base home directory if necessary
366 if (arg
!= NULL
|| getarg(args
, 'm') != NULL
) {
367 int l
= strlen(cnf
->home
);
369 if (l
> 1 && cnf
->home
[l
-1] == '/') /* Shave off any trailing path delimiter */
370 cnf
->home
[--l
] = '\0';
372 if (l
< 2 || *cnf
->home
!= '/') /* Check for absolute path name */
373 errx(EX_DATAERR
, "invalid base directory for home '%s'", cnf
->home
);
375 if (stat(cnf
->home
, &st
) == -1) {
376 char dbuf
[MAXPATHLEN
];
379 * This is a kludge especially for Joerg :)
380 * If the home directory would be created in the root partition, then
381 * we really create it under /usr which is likely to have more space.
382 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
384 if (strchr(cnf
->home
+1, '/') == NULL
) {
385 snprintf(dbuf
, MAXPATHLEN
, "/usr%s", cnf
->home
);
386 if (mkdir(dbuf
, _DEF_DIRMODE
) != -1 || errno
== EEXIST
) {
389 * Skip first "/" and create symlink:
392 symlink(dbuf
+1, cnf
->home
);
394 /* If this falls, fall back to old method */
396 strlcpy(dbuf
, cnf
->home
, sizeof(dbuf
));
398 if (stat(dbuf
, &st
) == -1) {
399 while ((p
= strchr(p
+ 1, '/')) != NULL
) {
401 if (stat(dbuf
, &st
) == -1) {
402 if (mkdir(dbuf
, _DEF_DIRMODE
) == -1)
403 err(EX_OSFILE
, "mkdir '%s'", dbuf
);
405 } else if (!S_ISDIR(st
.st_mode
))
406 errx(EX_OSFILE
, "'%s' (root home parent) is not a directory", dbuf
);
410 if (stat(dbuf
, &st
) == -1) {
411 if (mkdir(dbuf
, _DEF_DIRMODE
) == -1)
412 err(EX_OSFILE
, "mkdir '%s'", dbuf
);
415 } else if (!S_ISDIR(st
.st_mode
))
416 errx(EX_OSFILE
, "root home `%s' is not a directory", cnf
->home
);
419 if ((arg
= getarg(args
, 'e')) != NULL
)
420 cnf
->expire_days
= atoi(arg
->val
);
422 if ((arg
= getarg(args
, 'y')) != NULL
)
423 cnf
->nispasswd
= arg
->val
;
425 if ((arg
= getarg(args
, 'p')) != NULL
&& arg
->val
)
426 cnf
->password_days
= atoi(arg
->val
);
428 if ((arg
= getarg(args
, 'g')) != NULL
) {
429 if (!*(p
= arg
->val
)) /* Handle empty group list specially */
430 cnf
->default_group
= "";
432 if ((grp
= GETGRNAM(p
)) == NULL
) {
433 if (!isdigit((unsigned char)*p
) || (grp
= GETGRGID((gid_t
) atoi(p
))) == NULL
)
434 errx(EX_NOUSER
, "group `%s' does not exist", p
);
436 cnf
->default_group
= newstr(grp
->gr_name
);
439 if ((arg
= getarg(args
, 'L')) != NULL
)
440 cnf
->default_class
= pw_checkname(arg
->val
, 0);
442 if ((arg
= getarg(args
, 'G')) != NULL
&& arg
->val
) {
445 for (p
= strtok(arg
->val
, ", \t"); p
!= NULL
; p
= strtok(NULL
, ", \t")) {
446 if ((grp
= GETGRNAM(p
)) == NULL
) {
447 if (!isdigit((unsigned char)*p
) || (grp
= GETGRGID((gid_t
) atoi(p
))) == NULL
)
448 errx(EX_NOUSER
, "group `%s' does not exist", p
);
450 if (extendarray(&cnf
->groups
, &cnf
->numgroups
, i
+ 2) != -1)
451 cnf
->groups
[i
++] = newstr(grp
->gr_name
);
453 while (i
< cnf
->numgroups
)
454 cnf
->groups
[i
++] = NULL
;
457 if ((arg
= getarg(args
, 'k')) != NULL
) {
458 if (stat(cnf
->dotdir
= arg
->val
, &st
) == -1 || !S_ISDIR(st
.st_mode
))
459 errx(EX_OSFILE
, "skeleton `%s' is not a directory or does not exist", cnf
->dotdir
);
462 if ((arg
= getarg(args
, 's')) != NULL
)
463 cnf
->shell_default
= arg
->val
;
465 if ((arg
= getarg(args
, 'w')) != NULL
)
466 cnf
->default_password
= boolean_val(arg
->val
, cnf
->default_password
);
467 if (mode
== M_ADD
&& getarg(args
, 'D')) {
469 errx(EX_DATAERR
, "can't combine `-D' with `-n name'");
470 if ((arg
= getarg(args
, 'u')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
471 if ((cnf
->min_uid
= (uid_t
) atoi(p
)) == 0)
473 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_uid
= (uid_t
) atoi(p
)) < cnf
->min_uid
)
474 cnf
->max_uid
= 32000;
476 if ((arg
= getarg(args
, 'i')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
477 if ((cnf
->min_gid
= (gid_t
) atoi(p
)) == 0)
479 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_gid
= (gid_t
) atoi(p
)) < cnf
->min_gid
)
480 cnf
->max_gid
= 32000;
483 if (write_userconfig(conf
.config
))
484 return (EXIT_SUCCESS
);
485 err(EX_IOERR
, "config udpate");
489 pwd
= GETPWNAM(pw_checkname(name
, 0));
491 if (id
< 0 && name
== NULL
)
492 errx(EX_DATAERR
, "user name or id required");
495 * Update require that the user exists
497 if (mode
== M_UPDATE
) {
499 if (name
== NULL
&& pwd
== NULL
) /* Try harder */
504 errx(EX_NOUSER
, "no such uid `%ld'", id
);
505 errx(EX_NOUSER
, "no such user `%s'", name
);
512 * The rest is edit code
514 if (conf
.newname
!= NULL
) {
515 if (strcmp(pwd
->pw_name
, "root") == 0)
516 errx(EX_DATAERR
, "can't rename `root' account");
517 pwd
->pw_name
= pw_checkname(conf
.newname
, 0);
521 if (id
> 0 && isdigit((unsigned char)*arg
->val
)) {
522 pwd
->pw_uid
= (uid_t
)id
;
524 if (pwd
->pw_uid
!= 0 && strcmp(pwd
->pw_name
, "root") == 0)
525 errx(EX_DATAERR
, "can't change uid of `root' account");
526 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
527 warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd
->pw_name
);
530 if ((arg
= getarg(args
, 'g')) != NULL
&& pwd
->pw_uid
!= 0) { /* Already checked this */
531 gid_t newgid
= (gid_t
) GETGRNAM(cnf
->default_group
)->gr_gid
;
532 if (newgid
!= pwd
->pw_gid
) {
534 pwd
->pw_gid
= newgid
;
538 if ((arg
= getarg(args
, 'p')) != NULL
) {
539 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0) {
540 if (pwd
->pw_change
!= 0) {
546 time_t now
= time(NULL
);
547 time_t expire
= parse_date(now
, arg
->val
);
549 if (pwd
->pw_change
!= expire
) {
550 pwd
->pw_change
= expire
;
556 if ((arg
= getarg(args
, 'e')) != NULL
) {
557 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0) {
558 if (pwd
->pw_expire
!= 0) {
564 time_t now
= time(NULL
);
565 time_t expire
= parse_date(now
, arg
->val
);
567 if (pwd
->pw_expire
!= expire
) {
568 pwd
->pw_expire
= expire
;
574 if ((arg
= getarg(args
, 's')) != NULL
) {
575 char *shell
= shell_path(cnf
->shelldir
, cnf
->shells
, arg
->val
);
578 if (strcmp(shell
, pwd
->pw_shell
) != 0) {
579 pwd
->pw_shell
= shell
;
584 if (getarg(args
, 'L')) {
585 if (cnf
->default_class
== NULL
)
586 cnf
->default_class
= "";
587 if (strcmp(pwd
->pw_class
, cnf
->default_class
) != 0) {
588 pwd
->pw_class
= cnf
->default_class
;
593 if ((arg
= getarg(args
, 'd')) != NULL
) {
594 if (strcmp(pwd
->pw_dir
, arg
->val
))
596 if (stat(pwd
->pw_dir
= arg
->val
, &st
) == -1) {
597 if (getarg(args
, 'm') == NULL
&& strcmp(pwd
->pw_dir
, "/nonexistent") != 0)
598 warnx("WARNING: home `%s' does not exist", pwd
->pw_dir
);
599 } else if (!S_ISDIR(st
.st_mode
))
600 warnx("WARNING: home `%s' is not a directory", pwd
->pw_dir
);
603 if ((arg
= getarg(args
, 'w')) != NULL
&& conf
.fd
== -1) {
606 lc
= login_getpwclass(pwd
);
608 login_setcryptfmt(lc
, "sha512", NULL
) == NULL
)
609 warn("setting crypt(3) format");
611 pwd
->pw_passwd
= pw_password(cnf
, pwd
->pw_name
);
622 if (name
== NULL
) /* Required */
623 errx(EX_DATAERR
, "login name required");
624 else if ((pwd
= GETPWNAM(name
)) != NULL
) /* Exists */
625 errx(EX_DATAERR
, "login name `%s' already exists", name
);
628 * Now, set up defaults for a new user
632 pwd
->pw_class
= cnf
->default_class
? cnf
->default_class
: "";
633 pwd
->pw_uid
= pw_uidpolicy(cnf
, id
);
634 pwd
->pw_gid
= pw_gidpolicy(args
, pwd
->pw_name
, (gid_t
) pwd
->pw_uid
);
635 pwd
->pw_change
= pw_pwdpolicy(cnf
, args
);
636 pwd
->pw_expire
= pw_exppolicy(cnf
, args
);
637 pwd
->pw_dir
= pw_homepolicy(cnf
, args
, pwd
->pw_name
);
638 pwd
->pw_shell
= pw_shellpolicy(cnf
, args
, NULL
);
639 lc
= login_getpwclass(pwd
);
640 if (lc
== NULL
|| login_setcryptfmt(lc
, "sha512", NULL
) == NULL
)
641 warn("setting crypt(3) format");
643 pwd
->pw_passwd
= pw_password(cnf
, pwd
->pw_name
);
646 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
647 warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd
->pw_name
);
651 * Shared add/edit code
653 if (conf
.gecos
!= NULL
) {
654 if (strcmp(pwd
->pw_gecos
, conf
.gecos
) != 0) {
655 pwd
->pw_gecos
= conf
.gecos
;
661 edited
= set_passwd(pwd
, mode
== M_UPDATE
);
664 * Special case: -N only displays & exits
667 return print_user(pwd
);
670 edited
= 1; /* Always */
673 errx(EX_IOERR
, "user '%s' already exists",
676 err(EX_IOERR
, "passwd file update");
677 if (cnf
->nispasswd
&& *cnf
->nispasswd
=='/') {
678 rc
= addnispwent(cnf
->nispasswd
, pwd
);
680 warnx("User '%s' already exists in NIS passwd", pwd
->pw_name
);
682 warn("NIS passwd update");
683 /* NOTE: we treat NIS-only update errors as non-fatal */
685 } else if (mode
== M_UPDATE
&& edited
) /* Only updated this if required */
686 perform_chgpwent(name
, pwd
);
689 * Ok, user is created or changed - now edit group file
692 if (mode
== M_ADD
|| getarg(args
, 'G') != NULL
) {
694 /* First remove the user from all group */
696 while ((grp
= GETGRENT()) != NULL
) {
697 char group
[MAXLOGNAME
];
698 if (grp
->gr_mem
== NULL
)
700 for (i
= 0; grp
->gr_mem
[i
] != NULL
; i
++) {
701 if (strcmp(grp
->gr_mem
[i
] , pwd
->pw_name
) != 0)
703 for (j
= i
; grp
->gr_mem
[j
] != NULL
; j
++)
704 grp
->gr_mem
[j
] = grp
->gr_mem
[j
+1];
705 strlcpy(group
, grp
->gr_name
, MAXLOGNAME
);
706 chggrent(group
, grp
);
711 /* now add to group where needed */
712 for (i
= 0; cnf
->groups
[i
] != NULL
; i
++) {
713 grp
= GETGRNAM(cnf
->groups
[i
]);
714 grp
= gr_add(grp
, pwd
->pw_name
);
716 * grp can only be NULL in 2 cases:
717 * - the new member is already a member
718 * - a problem with memory occurs
719 * in both cases we want to skip now.
723 chggrent(cnf
->groups
[i
], grp
);
729 /* go get a current version of pwd */
730 pwd
= GETPWNAM(name
);
732 /* This will fail when we rename, so special case that */
733 if (mode
== M_UPDATE
&& conf
.newname
!= NULL
) {
734 name
= conf
.newname
; /* update new name */
735 pwd
= GETPWNAM(name
); /* refetch renamed rec */
738 if (pwd
== NULL
) /* can't go on without this */
739 errx(EX_NOUSER
, "user '%s' disappeared during update", name
);
741 grp
= GETGRGID(pwd
->pw_gid
);
742 pw_log(cnf
, mode
, W_USER
, "%s(%u):%s(%u):%s:%s:%s",
743 pwd
->pw_name
, pwd
->pw_uid
,
744 grp
? grp
->gr_name
: "unknown", (grp
? grp
->gr_gid
: (uid_t
)-1),
745 pwd
->pw_gecos
, pwd
->pw_dir
, pwd
->pw_shell
);
748 * If adding, let's touch and chown the user's mail file. This is not
749 * strictly necessary under BSD with a 0755 maildir but it also
750 * doesn't hurt anything to create the empty mailfile
753 if (PWALTDIR() != PWF_ALT
) {
754 arg
= getarg(args
, 'R');
755 snprintf(path
, sizeof(path
), "%s%s/%s",
756 arg
? arg
->val
: "", _PATH_MAILDIR
, pwd
->pw_name
);
757 close(open(path
, O_RDWR
| O_CREAT
, 0600)); /* Preserve contents &
759 chown(path
, pwd
->pw_uid
, pwd
->pw_gid
);
764 * Let's create and populate the user's home directory. Note
765 * that this also `works' for editing users if -m is used, but
766 * existing files will *not* be overwritten.
768 if (PWALTDIR() != PWF_ALT
&& getarg(args
, 'm') != NULL
&& pwd
->pw_dir
&&
769 *pwd
->pw_dir
== '/' && pwd
->pw_dir
[1])
770 create_and_populate_homedir(pwd
);
773 * Finally, send mail to the new user as well, if we are asked to
775 if (mode
== M_ADD
&& !PWALTDIR() && cnf
->newmail
&& *cnf
->newmail
&& (fp
= fopen(cnf
->newmail
, "r")) != NULL
) {
776 FILE *pfp
= popen(_PATH_SENDMAIL
" -t", "w");
781 fprintf(pfp
, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd
->pw_name
);
782 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
783 /* Do substitutions? */
787 pw_log(cnf
, mode
, W_USER
, "%s(%u) new user mail sent",
788 pwd
->pw_name
, pwd
->pw_uid
);
798 pw_uidpolicy(struct userconf
* cnf
, long id
)
801 uid_t uid
= (uid_t
) - 1;
804 * Check the given uid, if any
809 if ((pwd
= GETPWUID(uid
)) != NULL
&& conf
.checkduplicate
)
810 errx(EX_DATAERR
, "uid `%u' has already been allocated", pwd
->pw_uid
);
815 * We need to allocate the next available uid under one of
816 * two policies a) Grab the first unused uid b) Grab the
817 * highest possible unused uid
819 if (cnf
->min_uid
>= cnf
->max_uid
) { /* Sanity
820 * claus^H^H^H^Hheck */
822 cnf
->max_uid
= 32000;
824 bm
= bm_alloc(cnf
->max_uid
- cnf
->min_uid
+ 1);
827 * Now, let's fill the bitmap from the password file
830 while ((pwd
= GETPWENT()) != NULL
)
831 if (pwd
->pw_uid
>= (uid_t
) cnf
->min_uid
&& pwd
->pw_uid
<= (uid_t
) cnf
->max_uid
)
832 bm_setbit(&bm
, pwd
->pw_uid
- cnf
->min_uid
);
836 * Then apply the policy, with fallback to reuse if necessary
838 if (cnf
->reuse_uids
|| (uid
= (uid_t
) (bm_lastset(&bm
) + cnf
->min_uid
+ 1)) > cnf
->max_uid
)
839 uid
= (uid_t
) (bm_firstunset(&bm
) + cnf
->min_uid
);
842 * Another sanity check
844 if (uid
< cnf
->min_uid
|| uid
> cnf
->max_uid
)
845 errx(EX_SOFTWARE
, "unable to allocate a new uid - range fully used");
853 pw_gidpolicy(struct cargs
* args
, char *nam
, gid_t prefer
)
856 gid_t gid
= (uid_t
) - 1;
857 struct carg
*a_gid
= getarg(args
, 'g');
858 struct userconf
*cnf
= conf
.userconf
;
861 * If no arg given, see if default can help out
863 if (a_gid
== NULL
&& cnf
->default_group
&& *cnf
->default_group
)
864 a_gid
= addarg(args
, 'g', cnf
->default_group
);
867 * Check the given gid, if any
871 if ((grp
= GETGRNAM(a_gid
->val
)) == NULL
) {
872 gid
= (gid_t
) atol(a_gid
->val
);
873 if ((gid
== 0 && !isdigit((unsigned char)*a_gid
->val
)) || (grp
= GETGRGID(gid
)) == NULL
)
874 errx(EX_NOUSER
, "group `%s' is not defined", a_gid
->val
);
877 } else if ((grp
= GETGRNAM(nam
)) != NULL
&&
878 (grp
->gr_mem
== NULL
|| grp
->gr_mem
[0] == NULL
)) {
879 gid
= grp
->gr_gid
; /* Already created? Use it anyway... */
881 struct cargs grpargs
;
887 * We need to auto-create a group with the user's name. We
888 * can send all the appropriate output to our sister routine
889 * bit first see if we can create a group with gid==uid so we
890 * can keep the user and group ids in sync. We purposely do
891 * NOT check the gid range if we can force the sync. If the
892 * user's name dups an existing group, then the group add
893 * function will happily handle that case for us and exit.
895 if (GETGRGID(prefer
) == NULL
) {
896 snprintf(tmp
, sizeof(tmp
), "%u", prefer
);
897 addarg(&grpargs
, 'g', tmp
);
900 gid
= pw_groupnext(cnf
, true);
902 pw_group(M_ADD
, nam
, -1, &grpargs
);
903 if ((grp
= GETGRNAM(nam
)) != NULL
)
906 a_gid
= LIST_FIRST(&grpargs
);
907 while (a_gid
!= NULL
) {
908 struct carg
*t
= LIST_NEXT(a_gid
, list
);
909 LIST_REMOVE(a_gid
, list
);
919 pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
)
922 time_t now
= time(NULL
);
923 struct carg
*arg
= getarg(args
, 'p');
926 if ((result
= parse_date(now
, arg
->val
)) == now
)
927 errx(EX_DATAERR
, "invalid date/time `%s'", arg
->val
);
928 } else if (cnf
->password_days
> 0)
929 result
= now
+ ((long) cnf
->password_days
* 86400L);
935 pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
)
938 time_t now
= time(NULL
);
939 struct carg
*arg
= getarg(args
, 'e');
942 if ((result
= parse_date(now
, arg
->val
)) == now
)
943 errx(EX_DATAERR
, "invalid date/time `%s'", arg
->val
);
944 } else if (cnf
->expire_days
> 0)
945 result
= now
+ ((long) cnf
->expire_days
* 86400L);
951 pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
953 struct carg
*arg
= getarg(args
, 'd');
954 static char home
[128];
959 if (cnf
->home
== NULL
|| *cnf
->home
== '\0')
960 errx(EX_CONFIG
, "no base home directory set");
961 snprintf(home
, sizeof(home
), "%s/%s", cnf
->home
, user
);
967 shell_path(char const * path
, char *shells
[], char *sh
)
969 if (sh
!= NULL
&& (*sh
== '/' || *sh
== '\0'))
970 return sh
; /* specified full path or forced none */
973 char paths
[_UC_MAXLINE
];
976 * We need to search paths
978 strlcpy(paths
, path
, sizeof(paths
));
979 for (p
= strtok(paths
, ": \t\r\n"); p
!= NULL
; p
= strtok(NULL
, ": \t\r\n")) {
981 static char shellpath
[256];
984 snprintf(shellpath
, sizeof(shellpath
), "%s/%s", p
, sh
);
985 if (access(shellpath
, X_OK
) == 0)
988 for (i
= 0; i
< _UC_MAXSHELLS
&& shells
[i
] != NULL
; i
++) {
989 snprintf(shellpath
, sizeof(shellpath
), "%s/%s", p
, shells
[i
]);
990 if (access(shellpath
, X_OK
) == 0)
995 errx(EX_OSFILE
, "can't find shell `%s' in shell paths", sh
);
996 errx(EX_CONFIG
, "no default shell available or defined");
1003 pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
)
1005 char *sh
= newshell
;
1006 struct carg
*arg
= getarg(args
, 's');
1008 if (newshell
== NULL
&& arg
!= NULL
)
1010 return shell_path(cnf
->shelldir
, cnf
->shells
, sh
? sh
: cnf
->shell_default
);
1015 static char const chars
[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
1018 pw_pwcrypt(char *password
)
1021 char salt
[SALTSIZE
+ 1];
1024 static char buf
[256];
1027 * Calculate a salt value
1029 for (i
= 0; i
< SALTSIZE
; i
++)
1030 salt
[i
] = chars
[arc4random_uniform(sizeof(chars
) - 1)];
1031 salt
[SALTSIZE
] = '\0';
1033 cryptpw
= crypt(password
, salt
);
1034 if (cryptpw
== NULL
)
1035 errx(EX_CONFIG
, "crypt(3) failure");
1036 return strcpy(buf
, cryptpw
);
1041 pw_password(struct userconf
* cnf
, char const * user
)
1046 switch (cnf
->default_password
) {
1047 case -1: /* Random password */
1048 l
= (arc4random() % 8 + 8); /* 8 - 16 chars */
1049 for (i
= 0; i
< l
; i
++)
1050 pwbuf
[i
] = chars
[arc4random_uniform(sizeof(chars
)-1)];
1054 * We give this information back to the user
1056 if (conf
.fd
== -1 && !conf
.dryrun
) {
1057 if (isatty(STDOUT_FILENO
))
1058 printf("Password for '%s' is: ", user
);
1059 printf("%s\n", pwbuf
);
1064 case -2: /* No password at all! */
1067 case 0: /* No login - default */
1071 case 1: /* user's name */
1072 strlcpy(pwbuf
, user
, sizeof(pwbuf
));
1075 return pw_pwcrypt(pwbuf
);
1079 pw_userdel(char *name
, long id
)
1081 struct passwd
*pwd
= NULL
;
1082 char file
[MAXPATHLEN
];
1083 char home
[MAXPATHLEN
];
1085 struct group
*gr
, *grp
;
1086 char grname
[LOGNAMESIZE
];
1090 if (id
< 0 && name
== NULL
)
1091 errx(EX_DATAERR
, "username or id required");
1093 pwd
= (name
!= NULL
) ? GETPWNAM(pw_checkname(name
, 0)) : GETPWUID(id
);
1096 errx(EX_NOUSER
, "no such uid `%ld'", id
);
1097 errx(EX_NOUSER
, "no such user `%s'", name
);
1101 name
= pwd
->pw_name
;
1103 if (strcmp(pwd
->pw_name
, "root") == 0)
1104 errx(EX_DATAERR
, "cannot remove user 'root'");
1108 * Remove opie record from /etc/opiekeys
1111 rmopie(pwd
->pw_name
);
1116 snprintf(file
, sizeof(file
), "/var/cron/tabs/%s", pwd
->pw_name
);
1117 if (access(file
, F_OK
) == 0) {
1118 snprintf(file
, sizeof(file
), "crontab -u %s -r", pwd
->pw_name
);
1123 * Save these for later, since contents of pwd may be
1124 * invalidated by deletion
1126 snprintf(file
, sizeof(file
), "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
1127 strlcpy(home
, pwd
->pw_dir
, sizeof(home
));
1128 gr
= GETGRGID(pwd
->pw_gid
);
1130 strlcpy(grname
, gr
->gr_name
, LOGNAMESIZE
);
1136 err(EX_IOERR
, "user '%s' does not exist", pwd
->pw_name
);
1138 err(EX_IOERR
, "passwd update");
1140 if (conf
.userconf
->nispasswd
&& *conf
.userconf
->nispasswd
=='/') {
1141 rc
= delnispwent(conf
.userconf
->nispasswd
, name
);
1143 warnx("WARNING: user '%s' does not exist in NIS passwd",
1146 warn("WARNING: NIS passwd update");
1150 grp
= GETGRNAM(name
);
1152 (grp
->gr_mem
== NULL
|| *grp
->gr_mem
== NULL
) &&
1153 strcmp(name
, grname
) == 0)
1154 delgrent(GETGRNAM(name
));
1156 while ((grp
= GETGRENT()) != NULL
) {
1158 char group
[MAXLOGNAME
];
1159 if (grp
->gr_mem
== NULL
)
1162 for (i
= 0; grp
->gr_mem
[i
] != NULL
; i
++) {
1163 if (strcmp(grp
->gr_mem
[i
], name
) != 0)
1166 for (j
= i
; grp
->gr_mem
[j
] != NULL
; j
++)
1167 grp
->gr_mem
[j
] = grp
->gr_mem
[j
+1];
1168 strlcpy(group
, grp
->gr_name
, MAXLOGNAME
);
1169 chggrent(group
, grp
);
1174 pw_log(conf
.userconf
, M_DELETE
, W_USER
, "%s(%u) account removed", name
,
1186 if (getpwuid(uid
) == NULL
)
1190 * Remove home directory and contents
1192 if (conf
.deletehome
&& *home
== '/' && getpwuid(uid
) == NULL
&&
1193 stat(home
, &st
) != -1) {
1195 pw_log(conf
.userconf
, M_DELETE
, W_USER
, "%s(%u) home '%s' %sremoved",
1197 stat(home
, &st
) == -1 ? "" : "not completely ");
1201 return (EXIT_SUCCESS
);
1205 print_user(struct passwd
* pwd
)
1210 buf
= conf
.v7
? pw_make_v7(pwd
) : pw_make(pwd
);
1211 printf("%s\n", buf
);
1216 struct group
*grp
= GETGRGID(pwd
->pw_gid
);
1217 char uname
[60] = "User &", office
[60] = "[None]",
1218 wphone
[60] = "[None]", hphone
[60] = "[None]";
1219 char acexpire
[32] = "[None]", pwexpire
[32] = "[None]";
1222 if ((p
= strtok(pwd
->pw_gecos
, ",")) != NULL
) {
1223 strlcpy(uname
, p
, sizeof(uname
));
1224 if ((p
= strtok(NULL
, ",")) != NULL
) {
1225 strlcpy(office
, p
, sizeof(office
));
1226 if ((p
= strtok(NULL
, ",")) != NULL
) {
1227 strlcpy(wphone
, p
, sizeof(wphone
));
1228 if ((p
= strtok(NULL
, "")) != NULL
) {
1236 * Handle '&' in gecos field
1238 if ((p
= strchr(uname
, '&')) != NULL
) {
1239 int l
= strlen(pwd
->pw_name
);
1242 memmove(p
+ l
, p
+ 1, m
);
1243 memmove(p
, pwd
->pw_name
, l
);
1244 *p
= (char) toupper((unsigned char)*p
);
1246 if (pwd
->pw_expire
> (time_t)0 && (tptr
= localtime(&pwd
->pw_expire
)) != NULL
)
1247 strftime(acexpire
, sizeof acexpire
, "%c", tptr
);
1248 if (pwd
->pw_change
> (time_t)0 && (tptr
= localtime(&pwd
->pw_change
)) != NULL
)
1249 strftime(pwexpire
, sizeof pwexpire
, "%c", tptr
);
1250 printf("Login Name: %-15s #%-12u Group: %-15s #%u\n"
1252 " Home: %-26.26s Class: %s\n"
1253 " Shell: %-26.26s Office: %s\n"
1254 "Work Phone: %-26.26s Home Phone: %s\n"
1255 "Acc Expire: %-26.26s Pwd Expire: %s\n",
1256 pwd
->pw_name
, pwd
->pw_uid
,
1257 grp
? grp
->gr_name
: "(invalid)", pwd
->pw_gid
,
1258 uname
, pwd
->pw_dir
, pwd
->pw_class
,
1259 pwd
->pw_shell
, office
, wphone
, hphone
,
1260 acexpire
, pwexpire
);
1263 while ((grp
=GETGRENT()) != NULL
)
1266 if (grp
->gr_mem
!= NULL
) {
1267 while (grp
->gr_mem
[i
] != NULL
)
1269 if (strcmp(grp
->gr_mem
[i
], pwd
->pw_name
)==0)
1271 printf(j
++ == 0 ? " Groups: %s" : ",%s", grp
->gr_name
);
1279 printf("%s", j
? "\n" : "");
1281 return EXIT_SUCCESS
;
1285 pw_checkname(char *name
, int gecos
)
1288 const char *badchars
, *ch
, *showtype
;
1294 /* See if the name is valid as a gecos (comment) field. */
1296 showtype
= "gecos field";
1298 /* See if the name is valid as a userid or group. */
1299 badchars
= " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1300 showtype
= "userid/group name";
1301 /* Userids and groups can not have a leading '-'. */
1307 if (strchr(badchars
, *ch
) != NULL
|| *ch
< ' ' ||
1312 /* 8-bit characters are only allowed in GECOS fields */
1313 if (!gecos
&& (*ch
& 0x80)) {
1321 * A `$' is allowed as the final character for userids and groups,
1322 * mainly for the benefit of samba.
1324 if (reject
&& !gecos
) {
1325 if (*ch
== '$' && *(ch
+ 1) == '\0') {
1331 snprintf(showch
, sizeof(showch
), (*ch
>= ' ' && *ch
< 127)
1332 ? "`%c'" : "0x%02x", *ch
);
1333 errx(EX_DATAERR
, "invalid character %s at position %td in %s",
1334 showch
, (ch
- name
), showtype
);
1336 if (!gecos
&& (ch
- name
) > LOGNAMESIZE
)
1337 errx(EX_DATAERR
, "name too long `%s' (max is %d)", name
,
1347 DIR *d
= opendir("/var/at/jobs");
1352 while ((e
= readdir(d
)) != NULL
) {
1355 if (strncmp(e
->d_name
, ".lock", 5) != 0 &&
1356 stat(e
->d_name
, &st
) == 0 &&
1357 !S_ISDIR(st
.st_mode
) &&
1359 char tmp
[MAXPATHLEN
];
1361 snprintf(tmp
, sizeof(tmp
), "/usr/bin/atrm %s", e
->d_name
);
1370 rmopie(char const * name
)
1372 static const char etcopie
[] = "/etc/opiekeys";
1373 FILE *fp
= fopen(etcopie
, "r+");
1378 int length
= strlen(name
);
1380 while (fgets(tmp
, sizeof tmp
, fp
) != NULL
) {
1381 if (strncmp(name
, tmp
, length
) == 0 && tmp
[length
]==' ') {
1382 if (fseek(fp
, atofs
, SEEK_SET
) == 0) {
1383 fwrite("#", 1, 1, fp
); /* Comment username out */
1390 * If we got an error of any sort, don't update!