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
26 * $FreeBSD: src/usr.sbin/pw/pw_user.c,v 1.72 2012/02/22 06:27:20 kevlo Exp $
32 #include <sys/param.h>
36 #include <sys/types.h>
38 #include <sys/resource.h>
40 #include <login_cap.h>
44 #define LOGNAMESIZE (MAXLOGNAME-1)
46 static char locked_str
[] = "*LOCKED*";
48 static int print_user(struct passwd
* pwd
, int pretty
, int v7
);
49 static uid_t
pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
);
50 static uid_t
pw_gidpolicy(struct userconf
* cnf
, struct cargs
* args
, char *nam
, gid_t prefer
);
51 static time_t pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
);
52 static time_t pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
);
53 static char *pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
);
54 static char *pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
);
55 static char *pw_password(struct userconf
* cnf
, struct cargs
* args
, char const * user
);
56 static char *shell_path(char const * path
, char *shells
[], char *sh
);
57 static void rmat(uid_t uid
);
58 static void rmopie(char const * name
);
61 * -C config configuration file
65 * -c comment user name/comment
66 * -d directory home directory
67 * -e date account expiry date
68 * -p date password expiry date
69 * -g grp primary group
70 * -G grp1,grp2 additional groups
71 * -m [ -k dir ] create and set up home
72 * -s shell name of login shell
75 * -l name new login name
76 * -h fd password filehandle
77 * -H fd encrypted password filehandle
78 * -F force print or add
80 * -D set user defaults
81 * -b dir default home root dir
82 * -e period default expiry period
83 * -p period default password change period
84 * -g group default group
85 * -G grp1,grp2.. default additional groups
86 * -L class default login class
87 * -k dir default home skeleton
88 * -s shell default shell
89 * -w method default password method
93 pw_user(struct userconf
* cnf
, int mode
, struct cargs
* args
)
101 struct passwd
*pwd
= NULL
;
104 char line
[_PASSWORD_LEN
+1];
109 static struct passwd fakeuser
=
126 * With M_NEXT, we only need to return the
131 uid_t next
= pw_uidpolicy(cnf
, args
);
132 if (getarg(args
, 'q'))
134 printf("%ld:", (long)next
);
135 pw_group(cnf
, mode
, args
);
140 * We can do all of the common legwork here
143 if ((arg
= getarg(args
, 'b')) != NULL
) {
144 cnf
->home
= arg
->val
;
147 if ((arg
= getarg(args
, 'M')) != NULL
) {
149 if ((set
= setmode(dmode_c
)) == NULL
)
150 errx(EX_DATAERR
, "invalid directory creation mode '%s'",
152 cnf
->homemode
= getmode(set
, _DEF_DIRMODE
);
157 * If we'll need to use it or we're updating it,
158 * then create the base home directory if necessary
160 if (arg
!= NULL
|| getarg(args
, 'm') != NULL
) {
161 int l
= strlen(cnf
->home
);
163 if (l
> 1 && cnf
->home
[l
-1] == '/') /* Shave off any trailing path delimiter */
164 cnf
->home
[--l
] = '\0';
166 if (l
< 2 || *cnf
->home
!= '/') /* Check for absolute path name */
167 errx(EX_DATAERR
, "invalid base directory for home '%s'", cnf
->home
);
169 if (stat(cnf
->home
, &st
) == -1) {
170 char dbuf
[MAXPATHLEN
];
173 * This is a kludge especially for Joerg :)
174 * If the home directory would be created in the root partition, then
175 * we really create it under /usr which is likely to have more space.
176 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
178 if (strchr(cnf
->home
+1, '/') == NULL
) {
179 strcpy(dbuf
, "/usr");
180 strncat(dbuf
, cnf
->home
, MAXPATHLEN
-5);
181 if (mkdir(dbuf
, _DEF_DIRMODE
) != -1 || errno
== EEXIST
) {
184 * Skip first "/" and create symlink:
187 symlink(dbuf
+1, cnf
->home
);
189 /* If this falls, fall back to old method */
191 strlcpy(dbuf
, cnf
->home
, sizeof(dbuf
));
193 if (stat(dbuf
, &st
) == -1) {
194 while ((p
= strchr(++p
, '/')) != NULL
) {
196 if (stat(dbuf
, &st
) == -1) {
197 if (mkdir(dbuf
, _DEF_DIRMODE
) == -1)
200 } else if (!S_ISDIR(st
.st_mode
))
201 errx(EX_OSFILE
, "'%s' (root home parent) is not a directory", dbuf
);
205 if (stat(dbuf
, &st
) == -1) {
206 if (mkdir(dbuf
, _DEF_DIRMODE
) == -1) {
207 direrr
: err(EX_OSFILE
, "mkdir '%s'", dbuf
);
211 } else if (!S_ISDIR(st
.st_mode
))
212 errx(EX_OSFILE
, "root home `%s' is not a directory", cnf
->home
);
215 if ((arg
= getarg(args
, 'e')) != NULL
)
216 cnf
->expire_days
= atoi(arg
->val
);
218 if ((arg
= getarg(args
, 'y')) != NULL
)
219 cnf
->nispasswd
= arg
->val
;
221 if ((arg
= getarg(args
, 'p')) != NULL
&& arg
->val
)
222 cnf
->password_days
= atoi(arg
->val
);
224 if ((arg
= getarg(args
, 'g')) != NULL
) {
225 if (!*(p
= arg
->val
)) /* Handle empty group list specially */
226 cnf
->default_group
= "";
228 if ((grp
= GETGRNAM(p
)) == NULL
) {
229 if (!isdigit((unsigned char)*p
) || (grp
= GETGRGID((gid_t
) atoi(p
))) == NULL
)
230 errx(EX_NOUSER
, "group `%s' does not exist", p
);
232 cnf
->default_group
= newstr(grp
->gr_name
);
235 if ((arg
= getarg(args
, 'L')) != NULL
)
236 cnf
->default_class
= pw_checkname((u_char
*)arg
->val
, 0);
238 if ((arg
= getarg(args
, 'G')) != NULL
&& arg
->val
) {
241 for (p
= strtok(arg
->val
, ", \t"); p
!= NULL
; p
= strtok(NULL
, ", \t")) {
242 if ((grp
= GETGRNAM(p
)) == NULL
) {
243 if (!isdigit((unsigned char)*p
) || (grp
= GETGRGID((gid_t
) atoi(p
))) == NULL
)
244 errx(EX_NOUSER
, "group `%s' does not exist", p
);
246 if (extendarray(&cnf
->groups
, &cnf
->numgroups
, i
+ 2) != -1)
247 cnf
->groups
[i
++] = newstr(grp
->gr_name
);
249 while (i
< cnf
->numgroups
)
250 cnf
->groups
[i
++] = NULL
;
253 if ((arg
= getarg(args
, 'k')) != NULL
) {
254 if (stat(cnf
->dotdir
= arg
->val
, &st
) == -1 || !S_ISDIR(st
.st_mode
))
255 errx(EX_OSFILE
, "skeleton `%s' is not a directory or does not exist", cnf
->dotdir
);
258 if ((arg
= getarg(args
, 's')) != NULL
)
259 cnf
->shell_default
= arg
->val
;
261 if ((arg
= getarg(args
, 'w')) != NULL
)
262 cnf
->default_password
= boolean_val(arg
->val
, cnf
->default_password
);
263 if (mode
== M_ADD
&& getarg(args
, 'D')) {
264 if (getarg(args
, 'n') != NULL
)
265 errx(EX_DATAERR
, "can't combine `-D' with `-n name'");
266 if ((arg
= getarg(args
, 'u')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
267 if ((cnf
->min_uid
= (uid_t
) atoi(p
)) == 0)
269 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_uid
= (uid_t
) atoi(p
)) < cnf
->min_uid
)
270 cnf
->max_uid
= 32000;
272 if ((arg
= getarg(args
, 'i')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
273 if ((cnf
->min_gid
= (gid_t
) atoi(p
)) == 0)
275 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_gid
= (gid_t
) atoi(p
)) < cnf
->min_gid
)
276 cnf
->max_gid
= 32000;
279 arg
= getarg(args
, 'C');
280 if (write_userconfig(arg
? arg
->val
: NULL
))
282 warn("config update");
286 if (mode
== M_PRINT
&& getarg(args
, 'a')) {
287 int pretty
= getarg(args
, 'P') != NULL
;
288 int v7
= getarg(args
, '7') != NULL
;
291 while ((pwd
= GETPWENT()) != NULL
)
292 print_user(pwd
, pretty
, v7
);
297 if ((a_name
= getarg(args
, 'n')) != NULL
)
298 pwd
= GETPWNAM(pw_checkname((u_char
*)a_name
->val
, 0));
299 a_uid
= getarg(args
, 'u');
303 errx(EX_DATAERR
, "user name or id required");
306 * Determine whether 'n' switch is name or uid - we don't
307 * really don't really care which we have, but we need to
310 if (mode
!= M_ADD
&& pwd
== NULL
311 && strspn(a_name
->val
, "0123456789") == strlen(a_name
->val
)
312 && atoi(a_name
->val
) > 0) { /* Assume uid */
313 (a_uid
= a_name
)->ch
= 'u';
319 * Update, delete & print require that the user exists
321 if (mode
== M_UPDATE
|| mode
== M_DELETE
||
322 mode
== M_PRINT
|| mode
== M_LOCK
|| mode
== M_UNLOCK
) {
324 if (a_name
== NULL
&& pwd
== NULL
) /* Try harder */
325 pwd
= GETPWUID(atoi(a_uid
->val
));
328 if (mode
== M_PRINT
&& getarg(args
, 'F')) {
329 fakeuser
.pw_name
= a_name
? a_name
->val
: "nouser";
330 fakeuser
.pw_uid
= a_uid
? (uid_t
) atol(a_uid
->val
) : -1;
331 return print_user(&fakeuser
,
332 getarg(args
, 'P') != NULL
,
333 getarg(args
, '7') != NULL
);
336 errx(EX_NOUSER
, "no such uid `%s'", a_uid
->val
);
337 errx(EX_NOUSER
, "no such user `%s'", a_name
->val
);
340 if (a_name
== NULL
) /* May be needed later */
341 a_name
= addarg(args
, 'n', newstr(pwd
->pw_name
));
344 * The M_LOCK and M_UNLOCK functions simply add or remove
345 * a "*LOCKED*" prefix from in front of the password to
346 * prevent it decoding correctly, and therefore prevents
347 * access. Of course, this only prevents access via
348 * password authentication (not ssh, kerberos or any
349 * other method that does not use the UNIX password) but
350 * that is a known limitation.
353 if (mode
== M_LOCK
) {
354 if (strncmp(pwd
->pw_passwd
, locked_str
, sizeof(locked_str
)-1) == 0)
355 errx(EX_DATAERR
, "user '%s' is already locked", pwd
->pw_name
);
356 passtmp
= malloc(strlen(pwd
->pw_passwd
) + sizeof(locked_str
));
357 if (passtmp
== NULL
) /* disaster */
358 errx(EX_UNAVAILABLE
, "out of memory");
359 strcpy(passtmp
, locked_str
);
360 strcat(passtmp
, pwd
->pw_passwd
);
361 pwd
->pw_passwd
= passtmp
;
363 } else if (mode
== M_UNLOCK
) {
364 if (strncmp(pwd
->pw_passwd
, locked_str
, sizeof(locked_str
)-1) != 0)
365 errx(EX_DATAERR
, "user '%s' is not locked", pwd
->pw_name
);
366 pwd
->pw_passwd
+= sizeof(locked_str
)-1;
368 } else if (mode
== M_DELETE
) {
370 * Handle deletions now
372 char file
[MAXPATHLEN
];
373 char home
[MAXPATHLEN
];
374 uid_t uid
= pwd
->pw_uid
;
376 if (strcmp(pwd
->pw_name
, "root") == 0)
377 errx(EX_DATAERR
, "cannot remove user 'root'");
381 * Remove opie record from /etc/opiekeys
384 rmopie(pwd
->pw_name
);
389 sprintf(file
, "/var/cron/tabs/%s", pwd
->pw_name
);
390 if (access(file
, F_OK
) == 0) {
391 sprintf(file
, "crontab -u %s -r", pwd
->pw_name
);
396 * Save these for later, since contents of pwd may be
397 * invalidated by deletion
399 sprintf(file
, "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
400 strlcpy(home
, pwd
->pw_dir
, sizeof(home
));
404 err(EX_IOERR
, "user '%s' does not exist", pwd
->pw_name
);
406 warn("passwd update");
410 if (cnf
->nispasswd
&& *cnf
->nispasswd
=='/') {
411 rc
= delnispwent(cnf
->nispasswd
, a_name
->val
);
413 warnx("WARNING: user '%s' does not exist in NIS passwd", pwd
->pw_name
);
415 warn("WARNING: NIS passwd update");
419 editgroups(a_name
->val
, NULL
);
421 pw_log(cnf
, mode
, W_USER
, "%s(%ld) account removed", a_name
->val
, (long) uid
);
432 if (getpwuid(uid
) == NULL
)
436 * Remove home directory and contents
438 if (getarg(args
, 'r') != NULL
&& *home
== '/' && getpwuid(uid
) == NULL
) {
439 if (stat(home
, &st
) != -1) {
441 pw_log(cnf
, mode
, W_USER
, "%s(%ld) home '%s' %sremoved",
442 a_name
->val
, (long) uid
, home
,
443 stat(home
, &st
) == -1 ? "" : "not completely ");
448 } else if (mode
== M_PRINT
)
449 return print_user(pwd
,
450 getarg(args
, 'P') != NULL
,
451 getarg(args
, '7') != NULL
);
454 * The rest is edit code
456 if ((arg
= getarg(args
, 'l')) != NULL
) {
457 if (strcmp(pwd
->pw_name
, "root") == 0)
458 errx(EX_DATAERR
, "can't rename `root' account");
459 pwd
->pw_name
= pw_checkname((u_char
*)arg
->val
, 0);
463 if ((arg
= getarg(args
, 'u')) != NULL
&& isdigit((unsigned char)*arg
->val
)) {
464 pwd
->pw_uid
= (uid_t
) atol(arg
->val
);
466 if (pwd
->pw_uid
!= 0 && strcmp(pwd
->pw_name
, "root") == 0)
467 errx(EX_DATAERR
, "can't change uid of `root' account");
468 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
469 warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd
->pw_name
);
472 if ((arg
= getarg(args
, 'g')) != NULL
&& pwd
->pw_uid
!= 0) { /* Already checked this */
473 gid_t newgid
= (gid_t
) GETGRNAM(cnf
->default_group
)->gr_gid
;
474 if (newgid
!= pwd
->pw_gid
) {
476 pwd
->pw_gid
= newgid
;
480 if ((arg
= getarg(args
, 'p')) != NULL
) {
481 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0) {
482 if (pwd
->pw_change
!= 0) {
488 time_t now
= time(NULL
);
489 time_t expire
= parse_date(now
, arg
->val
);
492 errx(EX_DATAERR
, "invalid password change date `%s'", arg
->val
);
493 if (pwd
->pw_change
!= expire
) {
494 pwd
->pw_change
= expire
;
500 if ((arg
= getarg(args
, 'e')) != NULL
) {
501 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0) {
502 if (pwd
->pw_expire
!= 0) {
508 time_t now
= time(NULL
);
509 time_t expire
= parse_date(now
, arg
->val
);
512 errx(EX_DATAERR
, "invalid account expiry date `%s'", arg
->val
);
513 if (pwd
->pw_expire
!= expire
) {
514 pwd
->pw_expire
= expire
;
520 if ((arg
= getarg(args
, 's')) != NULL
) {
521 char *shell
= shell_path(cnf
->shelldir
, cnf
->shells
, arg
->val
);
524 if (strcmp(shell
, pwd
->pw_shell
) != 0) {
525 pwd
->pw_shell
= shell
;
530 if (getarg(args
, 'L')) {
531 if (cnf
->default_class
== NULL
)
532 cnf
->default_class
= "";
533 if (strcmp(pwd
->pw_class
, cnf
->default_class
) != 0) {
534 pwd
->pw_class
= cnf
->default_class
;
539 if ((arg
= getarg(args
, 'd')) != NULL
) {
540 if (strcmp(pwd
->pw_dir
, arg
->val
))
542 if (stat(pwd
->pw_dir
= arg
->val
, &st
) == -1) {
543 if (getarg(args
, 'm') == NULL
&& strcmp(pwd
->pw_dir
, "/nonexistent") != 0)
544 warnx("WARNING: home `%s' does not exist", pwd
->pw_dir
);
545 } else if (!S_ISDIR(st
.st_mode
))
546 warnx("WARNING: home `%s' is not a directory", pwd
->pw_dir
);
549 if ((arg
= getarg(args
, 'w')) != NULL
&&
550 getarg(args
, 'h') == NULL
&& getarg(args
, 'H') == NULL
) {
553 lc
= login_getpwclass(pwd
);
555 login_setcryptfmt(lc
, "md5", NULL
) == NULL
)
556 warn("setting crypt(3) format");
558 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
569 if (a_name
== NULL
) /* Required */
570 errx(EX_DATAERR
, "login name required");
571 else if ((pwd
= GETPWNAM(a_name
->val
)) != NULL
) /* Exists */
572 errx(EX_DATAERR
, "login name `%s' already exists", a_name
->val
);
575 * Now, set up defaults for a new user
578 pwd
->pw_name
= a_name
->val
;
579 pwd
->pw_class
= cnf
->default_class
? cnf
->default_class
: "";
580 pwd
->pw_uid
= pw_uidpolicy(cnf
, args
);
581 pwd
->pw_gid
= pw_gidpolicy(cnf
, args
, pwd
->pw_name
, (gid_t
) pwd
->pw_uid
);
582 pwd
->pw_change
= pw_pwdpolicy(cnf
, args
);
583 pwd
->pw_expire
= pw_exppolicy(cnf
, args
);
584 pwd
->pw_dir
= pw_homepolicy(cnf
, args
, pwd
->pw_name
);
585 pwd
->pw_shell
= pw_shellpolicy(cnf
, args
, NULL
);
586 lc
= login_getpwclass(pwd
);
587 if (lc
== NULL
|| login_setcryptfmt(lc
, "md5", NULL
) == NULL
)
588 warn("setting crypt(3) format");
590 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
593 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
594 warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd
->pw_name
);
598 * Shared add/edit code
600 if ((arg
= getarg(args
, 'c')) != NULL
) {
601 char *gecos
= pw_checkname((u_char
*)arg
->val
, 1);
602 if (strcmp(pwd
->pw_gecos
, gecos
) != 0) {
603 pwd
->pw_gecos
= gecos
;
608 if ((arg
= getarg(args
, 'h')) != NULL
||
609 (arg
= getarg(args
, 'H')) != NULL
) {
610 if (strcmp(arg
->val
, "-") == 0) {
611 if (!pwd
->pw_passwd
|| *pwd
->pw_passwd
!= '*') {
612 pwd
->pw_passwd
= "*"; /* No access */
616 int fd
= atoi(arg
->val
);
617 int precrypt
= (arg
->ch
== 'H');
619 int istty
= isatty(fd
);
624 if (tcgetattr(fd
, &t
) == -1)
627 struct termios n
= t
;
630 n
.c_lflag
&= ~(ECHO
);
631 tcsetattr(fd
, TCSANOW
, &n
);
632 printf("%s%spassword for user %s:",
633 (mode
== M_UPDATE
) ? "new " : "",
634 precrypt
? "encrypted " : "",
639 b
= read(fd
, line
, sizeof(line
) - 1);
640 if (istty
) { /* Restore state */
641 tcsetattr(fd
, TCSANOW
, &t
);
646 warn("-%c file descriptor", precrypt
? 'H' :
651 if ((p
= strpbrk(line
, "\r\n")) != NULL
)
654 errx(EX_DATAERR
, "empty password read on file descriptor %d", fd
);
656 if (strchr(line
, ':') != NULL
)
658 pwd
->pw_passwd
= line
;
660 lc
= login_getpwclass(pwd
);
662 login_setcryptfmt(lc
, "md5", NULL
) == NULL
)
663 warn("setting crypt(3) format");
665 pwd
->pw_passwd
= pw_pwcrypt(line
);
672 * Special case: -N only displays & exits
674 if (getarg(args
, 'N') != NULL
)
675 return print_user(pwd
,
676 getarg(args
, 'P') != NULL
,
677 getarg(args
, '7') != NULL
);
680 edited
= 1; /* Always */
683 warnx("user '%s' already exists", pwd
->pw_name
);
685 } else if (rc
!= 0) {
686 warn("passwd file update");
689 if (cnf
->nispasswd
&& *cnf
->nispasswd
=='/') {
690 rc
= addnispwent(cnf
->nispasswd
, pwd
);
692 warnx("User '%s' already exists in NIS passwd", pwd
->pw_name
);
694 warn("NIS passwd update");
695 /* NOTE: we treat NIS-only update errors as non-fatal */
697 } else if (mode
== M_UPDATE
|| mode
== M_LOCK
|| mode
== M_UNLOCK
) {
698 if (edited
) { /* Only updated this if required */
699 rc
= chgpwent(a_name
->val
, pwd
);
701 warnx("user '%s' does not exist (NIS?)", pwd
->pw_name
);
703 } else if (rc
!= 0) {
704 warn("passwd file update");
707 if ( cnf
->nispasswd
&& *cnf
->nispasswd
=='/') {
708 rc
= chgnispwent(cnf
->nispasswd
, a_name
->val
, pwd
);
710 warn("User '%s' not found in NIS passwd", pwd
->pw_name
);
712 warn("NIS passwd update");
713 /* NOTE: NIS-only update errors are not fatal */
719 * Ok, user is created or changed - now edit group file
722 if (mode
== M_ADD
|| getarg(args
, 'G') != NULL
)
723 editgroups(pwd
->pw_name
, cnf
->groups
);
725 /* go get a current version of pwd */
726 pwd
= GETPWNAM(a_name
->val
);
728 /* This will fail when we rename, so special case that */
729 if (mode
== M_UPDATE
&& (arg
= getarg(args
, 'l')) != NULL
) {
730 a_name
->val
= arg
->val
; /* update new name */
731 pwd
= GETPWNAM(a_name
->val
); /* refetch renamed rec */
734 if (pwd
== NULL
) /* can't go on without this */
735 errx(EX_NOUSER
, "user '%s' disappeared during update", a_name
->val
);
737 grp
= GETGRGID(pwd
->pw_gid
);
738 pw_log(cnf
, mode
, W_USER
, "%s(%ld):%s(%ld):%s:%s:%s",
739 pwd
->pw_name
, (long) pwd
->pw_uid
,
740 grp
? grp
->gr_name
: "unknown", (long) (grp
? grp
->gr_gid
: -1),
741 pwd
->pw_gecos
, pwd
->pw_dir
, pwd
->pw_shell
);
744 * If adding, let's touch and chown the user's mail file. This is not
745 * strictly necessary under BSD with a 0755 maildir but it also
746 * doesn't hurt anything to create the empty mailfile
750 sprintf(line
, "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
751 close(open(line
, O_RDWR
| O_CREAT
, 0600)); /* Preserve contents &
753 chown(line
, pwd
->pw_uid
, pwd
->pw_gid
);
758 * Let's create and populate the user's home directory. Note
759 * that this also `works' for editing users if -m is used, but
760 * existing files will *not* be overwritten.
762 if (!PWALTDIR() && getarg(args
, 'm') != NULL
&& pwd
->pw_dir
&& *pwd
->pw_dir
== '/' && pwd
->pw_dir
[1]) {
763 copymkdir(pwd
->pw_dir
, cnf
->dotdir
, cnf
->homemode
, pwd
->pw_uid
, pwd
->pw_gid
);
764 pw_log(cnf
, mode
, W_USER
, "%s(%ld) home %s made",
765 pwd
->pw_name
, (long) pwd
->pw_uid
, pwd
->pw_dir
);
770 * Finally, send mail to the new user as well, if we are asked to
772 if (mode
== M_ADD
&& !PWALTDIR() && cnf
->newmail
&& *cnf
->newmail
&& (fp
= fopen(cnf
->newmail
, "r")) != NULL
) {
773 FILE *pfp
= popen(_PATH_SENDMAIL
" -t", "w");
778 fprintf(pfp
, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd
->pw_name
);
779 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
780 /* Do substitutions? */
784 pw_log(cnf
, mode
, W_USER
, "%s(%ld) new user mail sent",
785 pwd
->pw_name
, (long) pwd
->pw_uid
);
795 pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
)
798 uid_t uid
= (uid_t
) - 1;
799 struct carg
*a_uid
= getarg(args
, 'u');
802 * Check the given uid, if any
805 uid
= (uid_t
) atol(a_uid
->val
);
807 if ((pwd
= GETPWUID(uid
)) != NULL
&& getarg(args
, 'o') == NULL
)
808 errx(EX_DATAERR
, "uid `%ld' has already been allocated", (long) pwd
->pw_uid
);
813 * We need to allocate the next available uid under one of
814 * two policies a) Grab the first unused uid b) Grab the
815 * highest possible unused uid
817 if (cnf
->min_uid
>= cnf
->max_uid
) { /* Sanity
818 * claus^H^H^H^Hheck */
820 cnf
->max_uid
= 32000;
822 bm
= bm_alloc(cnf
->max_uid
- cnf
->min_uid
+ 1);
825 * Now, let's fill the bitmap from the password file
828 while ((pwd
= GETPWENT()) != NULL
)
829 if (pwd
->pw_uid
>= (uid_t
) cnf
->min_uid
&& pwd
->pw_uid
<= (uid_t
) cnf
->max_uid
)
830 bm_setbit(&bm
, pwd
->pw_uid
- cnf
->min_uid
);
834 * Then apply the policy, with fallback to reuse if necessary
836 if (cnf
->reuse_uids
|| (uid
= (uid_t
) (bm_lastset(&bm
) + cnf
->min_uid
+ 1)) > cnf
->max_uid
)
837 uid
= (uid_t
) (bm_firstunset(&bm
) + cnf
->min_uid
);
840 * Another sanity check
842 if (uid
< cnf
->min_uid
|| uid
> cnf
->max_uid
)
843 errx(EX_SOFTWARE
, "unable to allocate a new uid - range fully used");
851 pw_gidpolicy(struct userconf
* cnf
, struct cargs
* args
, char *nam
, gid_t prefer
)
854 gid_t gid
= (uid_t
) - 1;
855 struct carg
*a_gid
= getarg(args
, 'g');
858 * If no arg given, see if default can help out
860 if (a_gid
== NULL
&& cnf
->default_group
&& *cnf
->default_group
)
861 a_gid
= addarg(args
, 'g', cnf
->default_group
);
864 * Check the given gid, if any
868 if ((grp
= GETGRNAM(a_gid
->val
)) == NULL
) {
869 gid
= (gid_t
) atol(a_gid
->val
);
870 if ((gid
== 0 && !isdigit((unsigned char)*a_gid
->val
)) || (grp
= GETGRGID(gid
)) == NULL
)
871 errx(EX_NOUSER
, "group `%s' is not defined", a_gid
->val
);
874 } else if ((grp
= GETGRNAM(nam
)) != NULL
&& grp
->gr_mem
[0] == NULL
) {
875 gid
= grp
->gr_gid
; /* Already created? Use it anyway... */
877 struct cargs grpargs
;
881 addarg(&grpargs
, 'n', nam
);
884 * We need to auto-create a group with the user's name. We
885 * can send all the appropriate output to our sister routine
886 * bit first see if we can create a group with gid==uid so we
887 * can keep the user and group ids in sync. We purposely do
888 * NOT check the gid range if we can force the sync. If the
889 * user's name dups an existing group, then the group add
890 * function will happily handle that case for us and exit.
892 if (GETGRGID(prefer
) == NULL
) {
893 sprintf(tmp
, "%lu", (unsigned long) prefer
);
894 addarg(&grpargs
, 'g', tmp
);
896 if (getarg(args
, 'N'))
898 addarg(&grpargs
, 'N', NULL
);
899 addarg(&grpargs
, 'q', NULL
);
900 gid
= pw_group(cnf
, M_NEXT
, &grpargs
);
904 pw_group(cnf
, M_ADD
, &grpargs
);
905 if ((grp
= GETGRNAM(nam
)) != NULL
)
908 a_gid
= LIST_FIRST(&grpargs
);
909 while (a_gid
!= NULL
) {
910 struct carg
*t
= LIST_NEXT(a_gid
, list
);
911 LIST_REMOVE(a_gid
, list
);
921 pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
)
924 time_t now
= time(NULL
);
925 struct carg
*arg
= getarg(args
, 'p');
928 if ((result
= parse_date(now
, arg
->val
)) == now
)
929 errx(EX_DATAERR
, "invalid date/time `%s'", arg
->val
);
930 } else if (cnf
->password_days
> 0)
931 result
= now
+ ((long) cnf
->password_days
* 86400L);
937 pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
)
940 time_t now
= time(NULL
);
941 struct carg
*arg
= getarg(args
, 'e');
944 if ((result
= parse_date(now
, arg
->val
)) == now
)
945 errx(EX_DATAERR
, "invalid date/time `%s'", arg
->val
);
946 } else if (cnf
->expire_days
> 0)
947 result
= now
+ ((long) cnf
->expire_days
* 86400L);
953 pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
955 struct carg
*arg
= getarg(args
, 'd');
960 static char home
[128];
962 if (cnf
->home
== NULL
|| *cnf
->home
== '\0')
963 errx(EX_CONFIG
, "no base home directory set");
964 sprintf(home
, "%s/%s", cnf
->home
, user
);
970 shell_path(char const * path
, char *shells
[], char *sh
)
972 if (sh
!= NULL
&& (*sh
== '/' || *sh
== '\0'))
973 return sh
; /* specified full path or forced none */
976 char paths
[_UC_MAXLINE
];
979 * We need to search paths
981 strlcpy(paths
, path
, sizeof(paths
));
982 for (p
= strtok(paths
, ": \t\r\n"); p
!= NULL
; p
= strtok(NULL
, ": \t\r\n")) {
984 static char shellpath
[256];
987 sprintf(shellpath
, "%s/%s", p
, sh
);
988 if (access(shellpath
, X_OK
) == 0)
991 for (i
= 0; i
< _UC_MAXSHELLS
&& shells
[i
] != NULL
; i
++) {
992 sprintf(shellpath
, "%s/%s", p
, shells
[i
]);
993 if (access(shellpath
, X_OK
) == 0)
998 errx(EX_OSFILE
, "can't find shell `%s' in shell paths", sh
);
999 errx(EX_CONFIG
, "no default shell available or defined");
1006 pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
)
1008 char *sh
= newshell
;
1009 struct carg
*arg
= getarg(args
, 's');
1011 if (newshell
== NULL
&& arg
!= NULL
)
1013 return shell_path(cnf
->shelldir
, cnf
->shells
, sh
? sh
: cnf
->shell_default
);
1018 static char const chars
[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
1021 pw_pwcrypt(char *password
)
1024 char salt
[SALTSIZE
+ 1];
1027 static char buf
[256];
1030 * Calculate a salt value
1032 for (i
= 0; i
< SALTSIZE
; i
++)
1033 salt
[i
] = chars
[arc4random_uniform(sizeof(chars
) - 1)];
1034 salt
[SALTSIZE
] = '\0';
1036 cryptpw
= crypt(password
, salt
);
1037 if (cryptpw
== NULL
)
1038 errx(EX_CONFIG
, "crypt(3) failure");
1039 return strcpy(buf
, cryptpw
);
1044 pw_password(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
1049 switch (cnf
->default_password
) {
1050 case -1: /* Random password */
1051 l
= (arc4random() % 8 + 8); /* 8 - 16 chars */
1052 for (i
= 0; i
< l
; i
++)
1053 pwbuf
[i
] = chars
[arc4random_uniform(sizeof(chars
)-1)];
1057 * We give this information back to the user
1059 if (getarg(args
, 'h') == NULL
&& getarg(args
, 'H') == NULL
&&
1060 getarg(args
, 'N') == NULL
) {
1061 if (isatty(STDOUT_FILENO
))
1062 printf("Password for '%s' is: ", user
);
1063 printf("%s\n", pwbuf
);
1068 case -2: /* No password at all! */
1071 case 0: /* No login - default */
1075 case 1: /* user's name */
1076 strlcpy(pwbuf
, user
, sizeof(pwbuf
));
1079 return pw_pwcrypt(pwbuf
);
1084 print_user(struct passwd
* pwd
, int pretty
, int v7
)
1087 char buf
[_UC_MAXLINE
];
1089 fmtpwentry(buf
, pwd
, v7
? PWF_PASSWD
: PWF_STANDARD
);
1094 struct group
*grp
= GETGRGID(pwd
->pw_gid
);
1095 char uname
[60] = "User &", office
[60] = "[None]",
1096 wphone
[60] = "[None]", hphone
[60] = "[None]";
1097 char acexpire
[32] = "[None]", pwexpire
[32] = "[None]";
1100 if ((p
= strtok(pwd
->pw_gecos
, ",")) != NULL
) {
1101 strlcpy(uname
, p
, sizeof(uname
));
1102 if ((p
= strtok(NULL
, ",")) != NULL
) {
1103 strlcpy(office
, p
, sizeof(office
));
1104 if ((p
= strtok(NULL
, ",")) != NULL
) {
1105 strlcpy(wphone
, p
, sizeof(wphone
));
1106 if ((p
= strtok(NULL
, "")) != NULL
) {
1114 * Handle '&' in gecos field
1116 if ((p
= strchr(uname
, '&')) != NULL
) {
1117 int l
= strlen(pwd
->pw_name
);
1120 memmove(p
+ l
, p
+ 1, m
);
1121 memmove(p
, pwd
->pw_name
, l
);
1122 *p
= (char) toupper((unsigned char)*p
);
1124 if (pwd
->pw_expire
> (time_t)0 && (tptr
= localtime(&pwd
->pw_expire
)) != NULL
)
1125 strftime(acexpire
, sizeof acexpire
, "%c", tptr
);
1126 if (pwd
->pw_change
> (time_t)0 && (tptr
= localtime(&pwd
->pw_change
)) != NULL
)
1127 strftime(pwexpire
, sizeof pwexpire
, "%c", tptr
);
1128 printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n"
1130 " Home: %-26.26s Class: %s\n"
1131 " Shell: %-26.26s Office: %s\n"
1132 "Work Phone: %-26.26s Home Phone: %s\n"
1133 "Acc Expire: %-26.26s Pwd Expire: %s\n",
1134 pwd
->pw_name
, (long) pwd
->pw_uid
,
1135 grp
? grp
->gr_name
: "(invalid)", (long) pwd
->pw_gid
,
1136 uname
, pwd
->pw_dir
, pwd
->pw_class
,
1137 pwd
->pw_shell
, office
, wphone
, hphone
,
1138 acexpire
, pwexpire
);
1141 while ((grp
=GETGRENT()) != NULL
)
1144 while (grp
->gr_mem
[i
] != NULL
)
1146 if (strcmp(grp
->gr_mem
[i
], pwd
->pw_name
)==0)
1148 printf(j
++ == 0 ? " Groups: %s" : ",%s", grp
->gr_name
);
1155 printf("%s", j
? "\n" : "");
1157 return EXIT_SUCCESS
;
1161 pw_checkname(u_char
*name
, int gecos
)
1164 u_char
const *badchars
, *ch
, *showtype
;
1170 /* See if the name is valid as a gecos (comment) field. */
1172 showtype
= "gecos field";
1174 /* See if the name is valid as a userid or group. */
1175 badchars
= " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1176 showtype
= "userid/group name";
1177 /* Userids and groups can not have a leading '-'. */
1183 if (strchr(badchars
, *ch
) != NULL
|| *ch
< ' ' ||
1188 /* 8-bit characters are only allowed in GECOS fields */
1189 if (!gecos
&& (*ch
& 0x80)) {
1197 * A `$' is allowed as the final character for userids and groups,
1198 * mainly for the benefit of samba.
1200 if (reject
&& !gecos
) {
1201 if (*ch
== '$' && *(ch
+ 1) == '\0') {
1207 snprintf(showch
, sizeof(showch
), (*ch
>= ' ' && *ch
< 127)
1208 ? "`%c'" : "0x%02x", *ch
);
1209 errx(EX_DATAERR
, "invalid character %s at position %td in %s",
1210 showch
, (ch
- name
), showtype
);
1212 if (!gecos
&& (ch
- name
) > LOGNAMESIZE
)
1213 errx(EX_DATAERR
, "name too long `%s' (max is %d)", name
,
1215 return (char *)name
;
1222 DIR *d
= opendir("/var/at/jobs");
1227 while ((e
= readdir(d
)) != NULL
) {
1230 if (strncmp(e
->d_name
, ".lock", 5) != 0 &&
1231 stat(e
->d_name
, &st
) == 0 &&
1232 !S_ISDIR(st
.st_mode
) &&
1234 char tmp
[MAXPATHLEN
];
1236 sprintf(tmp
, "/usr/bin/atrm %s", e
->d_name
);
1245 rmopie(char const * name
)
1247 static const char etcopie
[] = "/etc/opiekeys";
1248 FILE *fp
= fopen(etcopie
, "r+");
1253 int length
= strlen(name
);
1255 while (fgets(tmp
, sizeof tmp
, fp
) != NULL
) {
1256 if (strncmp(name
, tmp
, length
) == 0 && tmp
[length
]==' ') {
1257 if (fseek(fp
, atofs
, SEEK_SET
) == 0) {
1258 fwrite("#", 1, 1, fp
); /* Comment username out */
1265 * If we got an error of any sort, don't update!