]>
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
, int pretty
, int v7
);
57 static uid_t
pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
);
58 static uid_t
pw_gidpolicy(struct userconf
* cnf
, 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 cargs
*args
, struct passwd
*pwd
,
72 char *homedir
, *dotdir
;
75 homedir
= dotdir
= NULL
;
77 if ((arg
= getarg(args
, 'R'))) {
78 asprintf(&homedir
, "%s/%s", arg
->val
, pwd
->pw_dir
);
80 errx(EX_OSERR
, "out of memory");
81 asprintf(&dotdir
, "%s/%s", arg
->val
, cnf
->dotdir
);
84 copymkdir(homedir
? homedir
: pwd
->pw_dir
, dotdir
? dotdir
: cnf
->dotdir
,
85 cnf
->homemode
, pwd
->pw_uid
, pwd
->pw_gid
);
86 pw_log(cnf
, mode
, W_USER
, "%s(%u) home %s made", pwd
->pw_name
,
87 pwd
->pw_uid
, pwd
->pw_dir
);
91 * -C config configuration file
95 * -c comment user name/comment
96 * -d directory home directory
97 * -e date account expiry date
98 * -p date password expiry date
99 * -g grp primary group
100 * -G grp1,grp2 additional groups
101 * -m [ -k dir ] create and set up home
102 * -s shell name of login shell
103 * -o duplicate uid ok
104 * -L class user class
105 * -l name new login name
106 * -h fd password filehandle
107 * -H fd encrypted password filehandle
108 * -F force print or add
110 * -D set user defaults
111 * -b dir default home root dir
112 * -e period default expiry period
113 * -p period default password change period
114 * -g group default group
115 * -G grp1,grp2.. default additional groups
116 * -L class default login class
117 * -k dir default home skeleton
118 * -s shell default shell
119 * -w method default password method
123 pw_user(struct userconf
* cnf
, int mode
, struct cargs
* args
)
131 struct passwd
*pwd
= NULL
;
134 char line
[_PASSWORD_LEN
+1];
135 char path
[MAXPATHLEN
];
140 static struct passwd fakeuser
=
152 #if defined(__FreeBSD__)
159 * With M_NEXT, we only need to return the
164 uid_t next
= pw_uidpolicy(cnf
, args
);
165 if (getarg(args
, 'q'))
168 pw_group(cnf
, mode
, args
);
173 * We can do all of the common legwork here
176 if ((arg
= getarg(args
, 'b')) != NULL
) {
177 cnf
->home
= arg
->val
;
180 if ((arg
= getarg(args
, 'M')) != NULL
) {
182 if ((set
= setmode(dmode_c
)) == NULL
)
183 errx(EX_DATAERR
, "invalid directory creation mode '%s'",
185 cnf
->homemode
= getmode(set
, _DEF_DIRMODE
);
190 * If we'll need to use it or we're updating it,
191 * then create the base home directory if necessary
193 if (arg
!= NULL
|| getarg(args
, 'm') != NULL
) {
194 int l
= strlen(cnf
->home
);
196 if (l
> 1 && cnf
->home
[l
-1] == '/') /* Shave off any trailing path delimiter */
197 cnf
->home
[--l
] = '\0';
199 if (l
< 2 || *cnf
->home
!= '/') /* Check for absolute path name */
200 errx(EX_DATAERR
, "invalid base directory for home '%s'", cnf
->home
);
202 if (stat(cnf
->home
, &st
) == -1) {
203 char dbuf
[MAXPATHLEN
];
206 * This is a kludge especially for Joerg :)
207 * If the home directory would be created in the root partition, then
208 * we really create it under /usr which is likely to have more space.
209 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
211 if (strchr(cnf
->home
+1, '/') == NULL
) {
212 snprintf(dbuf
, MAXPATHLEN
, "/usr%s", cnf
->home
);
213 if (mkdir(dbuf
, _DEF_DIRMODE
) != -1 || errno
== EEXIST
) {
216 * Skip first "/" and create symlink:
219 symlink(dbuf
+1, cnf
->home
);
221 /* If this falls, fall back to old method */
223 strlcpy(dbuf
, cnf
->home
, sizeof(dbuf
));
225 if (stat(dbuf
, &st
) == -1) {
226 while ((p
= strchr(p
+ 1, '/')) != NULL
) {
228 if (stat(dbuf
, &st
) == -1) {
229 if (mkdir(dbuf
, _DEF_DIRMODE
) == -1)
232 } else if (!S_ISDIR(st
.st_mode
))
233 errx(EX_OSFILE
, "'%s' (root home parent) is not a directory", dbuf
);
237 if (stat(dbuf
, &st
) == -1) {
238 if (mkdir(dbuf
, _DEF_DIRMODE
) == -1) {
239 direrr
: err(EX_OSFILE
, "mkdir '%s'", dbuf
);
243 } else if (!S_ISDIR(st
.st_mode
))
244 errx(EX_OSFILE
, "root home `%s' is not a directory", cnf
->home
);
247 if ((arg
= getarg(args
, 'e')) != NULL
)
248 cnf
->expire_days
= atoi(arg
->val
);
250 if ((arg
= getarg(args
, 'y')) != NULL
)
251 cnf
->nispasswd
= arg
->val
;
253 if ((arg
= getarg(args
, 'p')) != NULL
&& arg
->val
)
254 cnf
->password_days
= atoi(arg
->val
);
256 if ((arg
= getarg(args
, 'g')) != NULL
) {
257 if (!*(p
= arg
->val
)) /* Handle empty group list specially */
258 cnf
->default_group
= "";
260 if ((grp
= GETGRNAM(p
)) == NULL
) {
261 if (!isdigit((unsigned char)*p
) || (grp
= GETGRGID((gid_t
) atoi(p
))) == NULL
)
262 errx(EX_NOUSER
, "group `%s' does not exist", p
);
264 cnf
->default_group
= newstr(grp
->gr_name
);
267 if ((arg
= getarg(args
, 'L')) != NULL
)
268 cnf
->default_class
= pw_checkname(arg
->val
, 0);
270 if ((arg
= getarg(args
, 'G')) != NULL
&& arg
->val
) {
273 for (p
= strtok(arg
->val
, ", \t"); p
!= NULL
; p
= strtok(NULL
, ", \t")) {
274 if ((grp
= GETGRNAM(p
)) == NULL
) {
275 if (!isdigit((unsigned char)*p
) || (grp
= GETGRGID((gid_t
) atoi(p
))) == NULL
)
276 errx(EX_NOUSER
, "group `%s' does not exist", p
);
278 if (extendarray(&cnf
->groups
, &cnf
->numgroups
, i
+ 2) != -1)
279 cnf
->groups
[i
++] = newstr(grp
->gr_name
);
281 while (i
< cnf
->numgroups
)
282 cnf
->groups
[i
++] = NULL
;
285 if ((arg
= getarg(args
, 'k')) != NULL
) {
286 if (stat(cnf
->dotdir
= arg
->val
, &st
) == -1 || !S_ISDIR(st
.st_mode
))
287 errx(EX_OSFILE
, "skeleton `%s' is not a directory or does not exist", cnf
->dotdir
);
290 if ((arg
= getarg(args
, 's')) != NULL
)
291 cnf
->shell_default
= arg
->val
;
293 if ((arg
= getarg(args
, 'w')) != NULL
)
294 cnf
->default_password
= boolean_val(arg
->val
, cnf
->default_password
);
295 if (mode
== M_ADD
&& getarg(args
, 'D')) {
296 if (getarg(args
, 'n') != NULL
)
297 errx(EX_DATAERR
, "can't combine `-D' with `-n name'");
298 if ((arg
= getarg(args
, 'u')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
299 if ((cnf
->min_uid
= (uid_t
) atoi(p
)) == 0)
301 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_uid
= (uid_t
) atoi(p
)) < cnf
->min_uid
)
302 cnf
->max_uid
= 32000;
304 if ((arg
= getarg(args
, 'i')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
305 if ((cnf
->min_gid
= (gid_t
) atoi(p
)) == 0)
307 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_gid
= (gid_t
) atoi(p
)) < cnf
->min_gid
)
308 cnf
->max_gid
= 32000;
311 arg
= getarg(args
, 'C');
312 if (write_userconfig(arg
? arg
->val
: NULL
))
314 err(EX_IOERR
, "config udpate");
317 if (mode
== M_PRINT
&& getarg(args
, 'a')) {
318 int pretty
= getarg(args
, 'P') != NULL
;
319 int v7
= getarg(args
, '7') != NULL
;
321 while ((pwd
= GETPWENT()) != NULL
)
322 print_user(pwd
, pretty
, v7
);
327 if ((a_name
= getarg(args
, 'n')) != NULL
)
328 pwd
= GETPWNAM(pw_checkname(a_name
->val
, 0));
329 a_uid
= getarg(args
, 'u');
333 errx(EX_DATAERR
, "user name or id required");
336 * Determine whether 'n' switch is name or uid - we don't
337 * really don't really care which we have, but we need to
340 if (mode
!= M_ADD
&& pwd
== NULL
341 && strspn(a_name
->val
, "0123456789") == strlen(a_name
->val
)
343 (a_uid
= a_name
)->ch
= 'u';
347 if (strspn(a_uid
->val
, "0123456789") != strlen(a_uid
->val
))
348 errx(EX_USAGE
, "-u expects a number");
352 * Update, delete & print require that the user exists
354 if (mode
== M_UPDATE
|| mode
== M_DELETE
||
355 mode
== M_PRINT
|| mode
== M_LOCK
|| mode
== M_UNLOCK
) {
357 if (a_name
== NULL
&& pwd
== NULL
) /* Try harder */
358 pwd
= GETPWUID(atoi(a_uid
->val
));
361 if (mode
== M_PRINT
&& getarg(args
, 'F')) {
362 fakeuser
.pw_name
= a_name
? a_name
->val
: "nouser";
363 fakeuser
.pw_uid
= a_uid
? (uid_t
) atol(a_uid
->val
) : (uid_t
) -1;
364 return print_user(&fakeuser
,
365 getarg(args
, 'P') != NULL
,
366 getarg(args
, '7') != NULL
);
369 errx(EX_NOUSER
, "no such uid `%s'", a_uid
->val
);
370 errx(EX_NOUSER
, "no such user `%s'", a_name
->val
);
373 if (a_name
== NULL
) /* May be needed later */
374 a_name
= addarg(args
, 'n', newstr(pwd
->pw_name
));
377 * The M_LOCK and M_UNLOCK functions simply add or remove
378 * a "*LOCKED*" prefix from in front of the password to
379 * prevent it decoding correctly, and therefore prevents
380 * access. Of course, this only prevents access via
381 * password authentication (not ssh, kerberos or any
382 * other method that does not use the UNIX password) but
383 * that is a known limitation.
386 if (mode
== M_LOCK
) {
387 if (strncmp(pwd
->pw_passwd
, locked_str
, sizeof(locked_str
)-1) == 0)
388 errx(EX_DATAERR
, "user '%s' is already locked", pwd
->pw_name
);
389 asprintf(&passtmp
, "%s%s", locked_str
, pwd
->pw_passwd
);
390 if (passtmp
== NULL
) /* disaster */
391 errx(EX_UNAVAILABLE
, "out of memory");
392 pwd
->pw_passwd
= passtmp
;
394 } else if (mode
== M_UNLOCK
) {
395 if (strncmp(pwd
->pw_passwd
, locked_str
, sizeof(locked_str
)-1) != 0)
396 errx(EX_DATAERR
, "user '%s' is not locked", pwd
->pw_name
);
397 pwd
->pw_passwd
+= sizeof(locked_str
)-1;
399 } else if (mode
== M_DELETE
)
400 return (delete_user(cnf
, pwd
, a_name
,
401 getarg(args
, 'r') != NULL
, mode
));
402 else if (mode
== M_PRINT
)
403 return print_user(pwd
,
404 getarg(args
, 'P') != NULL
,
405 getarg(args
, '7') != NULL
);
408 * The rest is edit code
410 if ((arg
= getarg(args
, 'l')) != NULL
) {
411 if (strcmp(pwd
->pw_name
, "root") == 0)
412 errx(EX_DATAERR
, "can't rename `root' account");
413 pwd
->pw_name
= pw_checkname(arg
->val
, 0);
417 if ((arg
= getarg(args
, 'u')) != NULL
&& isdigit((unsigned char)*arg
->val
)) {
418 pwd
->pw_uid
= (uid_t
) atol(arg
->val
);
420 if (pwd
->pw_uid
!= 0 && strcmp(pwd
->pw_name
, "root") == 0)
421 errx(EX_DATAERR
, "can't change uid of `root' account");
422 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
423 warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd
->pw_name
);
426 if ((arg
= getarg(args
, 'g')) != NULL
&& pwd
->pw_uid
!= 0) { /* Already checked this */
427 gid_t newgid
= (gid_t
) GETGRNAM(cnf
->default_group
)->gr_gid
;
428 if (newgid
!= pwd
->pw_gid
) {
430 pwd
->pw_gid
= newgid
;
434 if ((arg
= getarg(args
, 'p')) != NULL
) {
435 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0) {
436 if (pwd
->pw_change
!= 0) {
442 time_t now
= time(NULL
);
443 time_t expire
= parse_date(now
, arg
->val
);
445 if (pwd
->pw_change
!= expire
) {
446 pwd
->pw_change
= expire
;
452 if ((arg
= getarg(args
, 'e')) != NULL
) {
453 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0) {
454 if (pwd
->pw_expire
!= 0) {
460 time_t now
= time(NULL
);
461 time_t expire
= parse_date(now
, arg
->val
);
463 if (pwd
->pw_expire
!= expire
) {
464 pwd
->pw_expire
= expire
;
470 if ((arg
= getarg(args
, 's')) != NULL
) {
471 char *shell
= shell_path(cnf
->shelldir
, cnf
->shells
, arg
->val
);
474 if (strcmp(shell
, pwd
->pw_shell
) != 0) {
475 pwd
->pw_shell
= shell
;
480 if (getarg(args
, 'L')) {
481 if (cnf
->default_class
== NULL
)
482 cnf
->default_class
= "";
483 if (strcmp(pwd
->pw_class
, cnf
->default_class
) != 0) {
484 pwd
->pw_class
= cnf
->default_class
;
489 if ((arg
= getarg(args
, 'd')) != NULL
) {
490 if (strcmp(pwd
->pw_dir
, arg
->val
))
492 if (stat(pwd
->pw_dir
= arg
->val
, &st
) == -1) {
493 if (getarg(args
, 'm') == NULL
&& strcmp(pwd
->pw_dir
, "/nonexistent") != 0)
494 warnx("WARNING: home `%s' does not exist", pwd
->pw_dir
);
495 } else if (!S_ISDIR(st
.st_mode
))
496 warnx("WARNING: home `%s' is not a directory", pwd
->pw_dir
);
499 if ((arg
= getarg(args
, 'w')) != NULL
&&
500 getarg(args
, 'h') == NULL
&& getarg(args
, 'H') == NULL
) {
503 lc
= login_getpwclass(pwd
);
505 login_setcryptfmt(lc
, "sha512", NULL
) == NULL
)
506 warn("setting crypt(3) format");
508 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
519 if (a_name
== NULL
) /* Required */
520 errx(EX_DATAERR
, "login name required");
521 else if ((pwd
= GETPWNAM(a_name
->val
)) != NULL
) /* Exists */
522 errx(EX_DATAERR
, "login name `%s' already exists", a_name
->val
);
525 * Now, set up defaults for a new user
528 pwd
->pw_name
= a_name
->val
;
529 pwd
->pw_class
= cnf
->default_class
? cnf
->default_class
: "";
530 pwd
->pw_uid
= pw_uidpolicy(cnf
, args
);
531 pwd
->pw_gid
= pw_gidpolicy(cnf
, args
, pwd
->pw_name
, (gid_t
) pwd
->pw_uid
);
532 pwd
->pw_change
= pw_pwdpolicy(cnf
, args
);
533 pwd
->pw_expire
= pw_exppolicy(cnf
, args
);
534 pwd
->pw_dir
= pw_homepolicy(cnf
, args
, pwd
->pw_name
);
535 pwd
->pw_shell
= pw_shellpolicy(cnf
, args
, NULL
);
536 lc
= login_getpwclass(pwd
);
537 if (lc
== NULL
|| login_setcryptfmt(lc
, "sha512", NULL
) == NULL
)
538 warn("setting crypt(3) format");
540 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
543 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
544 warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd
->pw_name
);
548 * Shared add/edit code
550 if ((arg
= getarg(args
, 'c')) != NULL
) {
551 char *gecos
= pw_checkname(arg
->val
, 1);
552 if (strcmp(pwd
->pw_gecos
, gecos
) != 0) {
553 pwd
->pw_gecos
= gecos
;
558 if ((arg
= getarg(args
, 'h')) != NULL
||
559 (arg
= getarg(args
, 'H')) != NULL
) {
560 if (strcmp(arg
->val
, "-") == 0) {
561 if (!pwd
->pw_passwd
|| *pwd
->pw_passwd
!= '*') {
562 pwd
->pw_passwd
= "*"; /* No access */
566 int fd
= atoi(arg
->val
);
567 int precrypt
= (arg
->ch
== 'H');
569 int istty
= isatty(fd
);
574 if (tcgetattr(fd
, &t
) == -1)
577 struct termios n
= t
;
580 n
.c_lflag
&= ~(ECHO
);
581 tcsetattr(fd
, TCSANOW
, &n
);
582 printf("%s%spassword for user %s:",
583 (mode
== M_UPDATE
) ? "new " : "",
584 precrypt
? "encrypted " : "",
589 b
= read(fd
, line
, sizeof(line
) - 1);
590 if (istty
) { /* Restore state */
591 tcsetattr(fd
, TCSANOW
, &t
);
596 err(EX_IOERR
, "-%c file descriptor",
597 precrypt
? 'H' : 'h');
599 if ((p
= strpbrk(line
, "\r\n")) != NULL
)
602 errx(EX_DATAERR
, "empty password read on file descriptor %d", fd
);
604 if (strchr(line
, ':') != NULL
)
606 pwd
->pw_passwd
= line
;
608 lc
= login_getpwclass(pwd
);
610 login_setcryptfmt(lc
, "sha512", NULL
) == NULL
)
611 warn("setting crypt(3) format");
613 pwd
->pw_passwd
= pw_pwcrypt(line
);
620 * Special case: -N only displays & exits
622 if (getarg(args
, 'N') != NULL
)
623 return print_user(pwd
,
624 getarg(args
, 'P') != NULL
,
625 getarg(args
, '7') != NULL
);
628 edited
= 1; /* Always */
631 errx(EX_IOERR
, "user '%s' already exists",
634 err(EX_IOERR
, "passwd file update");
635 if (cnf
->nispasswd
&& *cnf
->nispasswd
=='/') {
636 rc
= addnispwent(cnf
->nispasswd
, pwd
);
638 warnx("User '%s' already exists in NIS passwd", pwd
->pw_name
);
640 warn("NIS passwd update");
641 /* NOTE: we treat NIS-only update errors as non-fatal */
643 } else if (mode
== M_UPDATE
|| mode
== M_LOCK
|| mode
== M_UNLOCK
) {
644 if (edited
) { /* Only updated this if required */
645 rc
= chgpwent(a_name
->val
, pwd
);
647 errx(EX_IOERR
, "user '%s' does not exist (NIS?)", pwd
->pw_name
);
649 err(EX_IOERR
, "passwd file update");
650 if ( cnf
->nispasswd
&& *cnf
->nispasswd
=='/') {
651 rc
= chgnispwent(cnf
->nispasswd
, a_name
->val
, pwd
);
653 warn("User '%s' not found in NIS passwd", pwd
->pw_name
);
655 warn("NIS passwd update");
656 /* NOTE: NIS-only update errors are not fatal */
662 * Ok, user is created or changed - now edit group file
665 if (mode
== M_ADD
|| getarg(args
, 'G') != NULL
) {
667 /* First remove the user from all group */
669 while ((grp
= GETGRENT()) != NULL
) {
670 char group
[MAXLOGNAME
];
671 if (grp
->gr_mem
== NULL
)
673 for (i
= 0; grp
->gr_mem
[i
] != NULL
; i
++) {
674 if (strcmp(grp
->gr_mem
[i
] , pwd
->pw_name
) != 0)
676 for (j
= i
; grp
->gr_mem
[j
] != NULL
; j
++)
677 grp
->gr_mem
[j
] = grp
->gr_mem
[j
+1];
678 strlcpy(group
, grp
->gr_name
, MAXLOGNAME
);
679 chggrent(group
, grp
);
684 /* now add to group where needed */
685 for (i
= 0; cnf
->groups
[i
] != NULL
; i
++) {
686 grp
= GETGRNAM(cnf
->groups
[i
]);
687 grp
= gr_add(grp
, pwd
->pw_name
);
689 * grp can only be NULL in 2 cases:
690 * - the new member is already a member
691 * - a problem with memory occurs
692 * in both cases we want to skip now.
696 chggrent(cnf
->groups
[i
], grp
);
702 /* go get a current version of pwd */
703 pwd
= GETPWNAM(a_name
->val
);
705 /* This will fail when we rename, so special case that */
706 if (mode
== M_UPDATE
&& (arg
= getarg(args
, 'l')) != NULL
) {
707 a_name
->val
= arg
->val
; /* update new name */
708 pwd
= GETPWNAM(a_name
->val
); /* refetch renamed rec */
711 if (pwd
== NULL
) /* can't go on without this */
712 errx(EX_NOUSER
, "user '%s' disappeared during update", a_name
->val
);
714 grp
= GETGRGID(pwd
->pw_gid
);
715 pw_log(cnf
, mode
, W_USER
, "%s(%u):%s(%u):%s:%s:%s",
716 pwd
->pw_name
, pwd
->pw_uid
,
717 grp
? grp
->gr_name
: "unknown", (grp
? grp
->gr_gid
: (uid_t
)-1),
718 pwd
->pw_gecos
, pwd
->pw_dir
, pwd
->pw_shell
);
721 * If adding, let's touch and chown the user's mail file. This is not
722 * strictly necessary under BSD with a 0755 maildir but it also
723 * doesn't hurt anything to create the empty mailfile
726 if (PWALTDIR() != PWF_ALT
) {
727 arg
= getarg(args
, 'R');
728 snprintf(path
, sizeof(path
), "%s%s/%s",
729 arg
? arg
->val
: "", _PATH_MAILDIR
, pwd
->pw_name
);
730 close(open(path
, O_RDWR
| O_CREAT
, 0600)); /* Preserve contents &
732 chown(path
, pwd
->pw_uid
, pwd
->pw_gid
);
737 * Let's create and populate the user's home directory. Note
738 * that this also `works' for editing users if -m is used, but
739 * existing files will *not* be overwritten.
741 if (PWALTDIR() != PWF_ALT
&& getarg(args
, 'm') != NULL
&& pwd
->pw_dir
&&
742 *pwd
->pw_dir
== '/' && pwd
->pw_dir
[1])
743 create_and_populate_homedir(mode
, args
, pwd
, cnf
);
746 * Finally, send mail to the new user as well, if we are asked to
748 if (mode
== M_ADD
&& !PWALTDIR() && cnf
->newmail
&& *cnf
->newmail
&& (fp
= fopen(cnf
->newmail
, "r")) != NULL
) {
749 FILE *pfp
= popen(_PATH_SENDMAIL
" -t", "w");
754 fprintf(pfp
, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd
->pw_name
);
755 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
756 /* Do substitutions? */
760 pw_log(cnf
, mode
, W_USER
, "%s(%u) new user mail sent",
761 pwd
->pw_name
, pwd
->pw_uid
);
771 pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
)
774 uid_t uid
= (uid_t
) - 1;
775 struct carg
*a_uid
= getarg(args
, 'u');
778 * Check the given uid, if any
781 uid
= (uid_t
) atol(a_uid
->val
);
783 if ((pwd
= GETPWUID(uid
)) != NULL
&& getarg(args
, 'o') == NULL
)
784 errx(EX_DATAERR
, "uid `%u' has already been allocated", pwd
->pw_uid
);
789 * We need to allocate the next available uid under one of
790 * two policies a) Grab the first unused uid b) Grab the
791 * highest possible unused uid
793 if (cnf
->min_uid
>= cnf
->max_uid
) { /* Sanity
794 * claus^H^H^H^Hheck */
796 cnf
->max_uid
= 32000;
798 bm
= bm_alloc(cnf
->max_uid
- cnf
->min_uid
+ 1);
801 * Now, let's fill the bitmap from the password file
804 while ((pwd
= GETPWENT()) != NULL
)
805 if (pwd
->pw_uid
>= (uid_t
) cnf
->min_uid
&& pwd
->pw_uid
<= (uid_t
) cnf
->max_uid
)
806 bm_setbit(&bm
, pwd
->pw_uid
- cnf
->min_uid
);
810 * Then apply the policy, with fallback to reuse if necessary
812 if (cnf
->reuse_uids
|| (uid
= (uid_t
) (bm_lastset(&bm
) + cnf
->min_uid
+ 1)) > cnf
->max_uid
)
813 uid
= (uid_t
) (bm_firstunset(&bm
) + cnf
->min_uid
);
816 * Another sanity check
818 if (uid
< cnf
->min_uid
|| uid
> cnf
->max_uid
)
819 errx(EX_SOFTWARE
, "unable to allocate a new uid - range fully used");
827 pw_gidpolicy(struct userconf
* cnf
, struct cargs
* args
, char *nam
, gid_t prefer
)
830 gid_t gid
= (uid_t
) - 1;
831 struct carg
*a_gid
= getarg(args
, 'g');
834 * If no arg given, see if default can help out
836 if (a_gid
== NULL
&& cnf
->default_group
&& *cnf
->default_group
)
837 a_gid
= addarg(args
, 'g', cnf
->default_group
);
840 * Check the given gid, if any
844 if ((grp
= GETGRNAM(a_gid
->val
)) == NULL
) {
845 gid
= (gid_t
) atol(a_gid
->val
);
846 if ((gid
== 0 && !isdigit((unsigned char)*a_gid
->val
)) || (grp
= GETGRGID(gid
)) == NULL
)
847 errx(EX_NOUSER
, "group `%s' is not defined", a_gid
->val
);
850 } else if ((grp
= GETGRNAM(nam
)) != NULL
&&
851 (grp
->gr_mem
== NULL
|| grp
->gr_mem
[0] == NULL
)) {
852 gid
= grp
->gr_gid
; /* Already created? Use it anyway... */
854 struct cargs grpargs
;
858 addarg(&grpargs
, 'n', nam
);
861 * We need to auto-create a group with the user's name. We
862 * can send all the appropriate output to our sister routine
863 * bit first see if we can create a group with gid==uid so we
864 * can keep the user and group ids in sync. We purposely do
865 * NOT check the gid range if we can force the sync. If the
866 * user's name dups an existing group, then the group add
867 * function will happily handle that case for us and exit.
869 if (GETGRGID(prefer
) == NULL
) {
870 snprintf(tmp
, sizeof(tmp
), "%u", prefer
);
871 addarg(&grpargs
, 'g', tmp
);
873 if (getarg(args
, 'N'))
875 addarg(&grpargs
, 'N', NULL
);
876 addarg(&grpargs
, 'q', NULL
);
877 gid
= pw_group(cnf
, M_NEXT
, &grpargs
);
881 pw_group(cnf
, M_ADD
, &grpargs
);
882 if ((grp
= GETGRNAM(nam
)) != NULL
)
885 a_gid
= LIST_FIRST(&grpargs
);
886 while (a_gid
!= NULL
) {
887 struct carg
*t
= LIST_NEXT(a_gid
, list
);
888 LIST_REMOVE(a_gid
, list
);
898 pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
)
901 time_t now
= time(NULL
);
902 struct carg
*arg
= getarg(args
, 'p');
905 if ((result
= parse_date(now
, arg
->val
)) == now
)
906 errx(EX_DATAERR
, "invalid date/time `%s'", arg
->val
);
907 } else if (cnf
->password_days
> 0)
908 result
= now
+ ((long) cnf
->password_days
* 86400L);
914 pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
)
917 time_t now
= time(NULL
);
918 struct carg
*arg
= getarg(args
, 'e');
921 if ((result
= parse_date(now
, arg
->val
)) == now
)
922 errx(EX_DATAERR
, "invalid date/time `%s'", arg
->val
);
923 } else if (cnf
->expire_days
> 0)
924 result
= now
+ ((long) cnf
->expire_days
* 86400L);
930 pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
932 struct carg
*arg
= getarg(args
, 'd');
933 static char home
[128];
938 if (cnf
->home
== NULL
|| *cnf
->home
== '\0')
939 errx(EX_CONFIG
, "no base home directory set");
940 snprintf(home
, sizeof(home
), "%s/%s", cnf
->home
, user
);
946 shell_path(char const * path
, char *shells
[], char *sh
)
948 if (sh
!= NULL
&& (*sh
== '/' || *sh
== '\0'))
949 return sh
; /* specified full path or forced none */
952 char paths
[_UC_MAXLINE
];
955 * We need to search paths
957 strlcpy(paths
, path
, sizeof(paths
));
958 for (p
= strtok(paths
, ": \t\r\n"); p
!= NULL
; p
= strtok(NULL
, ": \t\r\n")) {
960 static char shellpath
[256];
963 snprintf(shellpath
, sizeof(shellpath
), "%s/%s", p
, sh
);
964 if (access(shellpath
, X_OK
) == 0)
967 for (i
= 0; i
< _UC_MAXSHELLS
&& shells
[i
] != NULL
; i
++) {
968 snprintf(shellpath
, sizeof(shellpath
), "%s/%s", p
, shells
[i
]);
969 if (access(shellpath
, X_OK
) == 0)
974 errx(EX_OSFILE
, "can't find shell `%s' in shell paths", sh
);
975 errx(EX_CONFIG
, "no default shell available or defined");
982 pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
)
985 struct carg
*arg
= getarg(args
, 's');
987 if (newshell
== NULL
&& arg
!= NULL
)
989 return shell_path(cnf
->shelldir
, cnf
->shells
, sh
? sh
: cnf
->shell_default
);
994 static char const chars
[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
997 pw_pwcrypt(char *password
)
1000 char salt
[SALTSIZE
+ 1];
1003 static char buf
[256];
1006 * Calculate a salt value
1008 for (i
= 0; i
< SALTSIZE
; i
++)
1009 salt
[i
] = chars
[arc4random_uniform(sizeof(chars
) - 1)];
1010 salt
[SALTSIZE
] = '\0';
1012 cryptpw
= crypt(password
, salt
);
1013 if (cryptpw
== NULL
)
1014 errx(EX_CONFIG
, "crypt(3) failure");
1015 return strcpy(buf
, cryptpw
);
1020 pw_password(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
1025 switch (cnf
->default_password
) {
1026 case -1: /* Random password */
1027 l
= (arc4random() % 8 + 8); /* 8 - 16 chars */
1028 for (i
= 0; i
< l
; i
++)
1029 pwbuf
[i
] = chars
[arc4random_uniform(sizeof(chars
)-1)];
1033 * We give this information back to the user
1035 if (getarg(args
, 'h') == NULL
&& getarg(args
, 'H') == NULL
&&
1036 getarg(args
, 'N') == NULL
) {
1037 if (isatty(STDOUT_FILENO
))
1038 printf("Password for '%s' is: ", user
);
1039 printf("%s\n", pwbuf
);
1044 case -2: /* No password at all! */
1047 case 0: /* No login - default */
1051 case 1: /* user's name */
1052 strlcpy(pwbuf
, user
, sizeof(pwbuf
));
1055 return pw_pwcrypt(pwbuf
);
1059 delete_user(struct userconf
*cnf
, struct passwd
*pwd
, struct carg
*a_name
,
1060 int delete, int mode
)
1062 char file
[MAXPATHLEN
];
1063 char home
[MAXPATHLEN
];
1064 uid_t uid
= pwd
->pw_uid
;
1065 struct group
*gr
, *grp
;
1066 char grname
[LOGNAMESIZE
];
1070 if (strcmp(pwd
->pw_name
, "root") == 0)
1071 errx(EX_DATAERR
, "cannot remove user 'root'");
1075 * Remove opie record from /etc/opiekeys
1078 rmopie(pwd
->pw_name
);
1083 snprintf(file
, sizeof(file
), "/var/cron/tabs/%s", pwd
->pw_name
);
1084 if (access(file
, F_OK
) == 0) {
1085 snprintf(file
, sizeof(file
), "crontab -u %s -r", pwd
->pw_name
);
1090 * Save these for later, since contents of pwd may be
1091 * invalidated by deletion
1093 snprintf(file
, sizeof(file
), "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
1094 strlcpy(home
, pwd
->pw_dir
, sizeof(home
));
1095 gr
= GETGRGID(pwd
->pw_gid
);
1097 strlcpy(grname
, gr
->gr_name
, LOGNAMESIZE
);
1103 err(EX_IOERR
, "user '%s' does not exist", pwd
->pw_name
);
1105 err(EX_IOERR
, "passwd update");
1107 if (cnf
->nispasswd
&& *cnf
->nispasswd
=='/') {
1108 rc
= delnispwent(cnf
->nispasswd
, a_name
->val
);
1110 warnx("WARNING: user '%s' does not exist in NIS passwd", pwd
->pw_name
);
1112 warn("WARNING: NIS passwd update");
1116 grp
= GETGRNAM(a_name
->val
);
1118 (grp
->gr_mem
== NULL
|| *grp
->gr_mem
== NULL
) &&
1119 strcmp(a_name
->val
, grname
) == 0)
1120 delgrent(GETGRNAM(a_name
->val
));
1122 while ((grp
= GETGRENT()) != NULL
) {
1124 char group
[MAXLOGNAME
];
1125 if (grp
->gr_mem
!= NULL
) {
1126 for (i
= 0; grp
->gr_mem
[i
] != NULL
; i
++) {
1127 if (!strcmp(grp
->gr_mem
[i
], a_name
->val
)) {
1128 for (j
= i
; grp
->gr_mem
[j
] != NULL
; j
++)
1129 grp
->gr_mem
[j
] = grp
->gr_mem
[j
+1];
1130 strlcpy(group
, grp
->gr_name
, MAXLOGNAME
);
1131 chggrent(group
, grp
);
1138 pw_log(cnf
, mode
, W_USER
, "%s(%u) account removed", a_name
->val
, uid
);
1149 if (getpwuid(uid
) == NULL
)
1153 * Remove home directory and contents
1155 if (delete && *home
== '/' && getpwuid(uid
) == NULL
) {
1156 if (stat(home
, &st
) != -1) {
1158 pw_log(cnf
, mode
, W_USER
, "%s(%u) home '%s' %sremoved",
1159 a_name
->val
, uid
, home
,
1160 stat(home
, &st
) == -1 ? "" : "not completely ");
1165 return (EXIT_SUCCESS
);
1169 print_user(struct passwd
* pwd
, int pretty
, int v7
)
1175 pwd
->pw_passwd
= (pwd
->pw_passwd
== NULL
) ? "" : "*";
1177 buf
= v7
? pw_make_v7(pwd
) : pw_make(pwd
);
1178 printf("%s\n", buf
);
1183 struct group
*grp
= GETGRGID(pwd
->pw_gid
);
1184 char uname
[60] = "User &", office
[60] = "[None]",
1185 wphone
[60] = "[None]", hphone
[60] = "[None]";
1186 char acexpire
[32] = "[None]", pwexpire
[32] = "[None]";
1189 if ((p
= strtok(pwd
->pw_gecos
, ",")) != NULL
) {
1190 strlcpy(uname
, p
, sizeof(uname
));
1191 if ((p
= strtok(NULL
, ",")) != NULL
) {
1192 strlcpy(office
, p
, sizeof(office
));
1193 if ((p
= strtok(NULL
, ",")) != NULL
) {
1194 strlcpy(wphone
, p
, sizeof(wphone
));
1195 if ((p
= strtok(NULL
, "")) != NULL
) {
1203 * Handle '&' in gecos field
1205 if ((p
= strchr(uname
, '&')) != NULL
) {
1206 int l
= strlen(pwd
->pw_name
);
1209 memmove(p
+ l
, p
+ 1, m
);
1210 memmove(p
, pwd
->pw_name
, l
);
1211 *p
= (char) toupper((unsigned char)*p
);
1213 if (pwd
->pw_expire
> (time_t)0 && (tptr
= localtime(&pwd
->pw_expire
)) != NULL
)
1214 strftime(acexpire
, sizeof acexpire
, "%c", tptr
);
1215 if (pwd
->pw_change
> (time_t)0 && (tptr
= localtime(&pwd
->pw_change
)) != NULL
)
1216 strftime(pwexpire
, sizeof pwexpire
, "%c", tptr
);
1217 printf("Login Name: %-15s #%-12u Group: %-15s #%u\n"
1219 " Home: %-26.26s Class: %s\n"
1220 " Shell: %-26.26s Office: %s\n"
1221 "Work Phone: %-26.26s Home Phone: %s\n"
1222 "Acc Expire: %-26.26s Pwd Expire: %s\n",
1223 pwd
->pw_name
, pwd
->pw_uid
,
1224 grp
? grp
->gr_name
: "(invalid)", pwd
->pw_gid
,
1225 uname
, pwd
->pw_dir
, pwd
->pw_class
,
1226 pwd
->pw_shell
, office
, wphone
, hphone
,
1227 acexpire
, pwexpire
);
1230 while ((grp
=GETGRENT()) != NULL
)
1233 if (grp
->gr_mem
!= NULL
) {
1234 while (grp
->gr_mem
[i
] != NULL
)
1236 if (strcmp(grp
->gr_mem
[i
], pwd
->pw_name
)==0)
1238 printf(j
++ == 0 ? " Groups: %s" : ",%s", grp
->gr_name
);
1246 printf("%s", j
? "\n" : "");
1248 return EXIT_SUCCESS
;
1252 pw_checkname(char *name
, int gecos
)
1255 const char *badchars
, *ch
, *showtype
;
1261 /* See if the name is valid as a gecos (comment) field. */
1263 showtype
= "gecos field";
1265 /* See if the name is valid as a userid or group. */
1266 badchars
= " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1267 showtype
= "userid/group name";
1268 /* Userids and groups can not have a leading '-'. */
1274 if (strchr(badchars
, *ch
) != NULL
|| *ch
< ' ' ||
1279 /* 8-bit characters are only allowed in GECOS fields */
1280 if (!gecos
&& (*ch
& 0x80)) {
1288 * A `$' is allowed as the final character for userids and groups,
1289 * mainly for the benefit of samba.
1291 if (reject
&& !gecos
) {
1292 if (*ch
== '$' && *(ch
+ 1) == '\0') {
1298 snprintf(showch
, sizeof(showch
), (*ch
>= ' ' && *ch
< 127)
1299 ? "`%c'" : "0x%02x", *ch
);
1300 errx(EX_DATAERR
, "invalid character %s at position %td in %s",
1301 showch
, (ch
- name
), showtype
);
1303 if (!gecos
&& (ch
- name
) > LOGNAMESIZE
)
1304 errx(EX_DATAERR
, "name too long `%s' (max is %d)", name
,
1314 DIR *d
= opendir("/var/at/jobs");
1319 while ((e
= readdir(d
)) != NULL
) {
1322 if (strncmp(e
->d_name
, ".lock", 5) != 0 &&
1323 stat(e
->d_name
, &st
) == 0 &&
1324 !S_ISDIR(st
.st_mode
) &&
1326 char tmp
[MAXPATHLEN
];
1328 snprintf(tmp
, sizeof(tmp
), "/usr/bin/atrm %s", e
->d_name
);
1337 rmopie(char const * name
)
1339 static const char etcopie
[] = "/etc/opiekeys";
1340 FILE *fp
= fopen(etcopie
, "r+");
1345 int length
= strlen(name
);
1347 while (fgets(tmp
, sizeof tmp
, fp
) != NULL
) {
1348 if (strncmp(name
, tmp
, length
) == 0 && tmp
[length
]==' ') {
1349 if (fseek(fp
, atofs
, SEEK_SET
) == 0) {
1350 fwrite("#", 1, 1, fp
); /* Comment username out */
1357 * If we got an error of any sort, don't update!