]>
git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw_user.c
0eb9802be596aa5087c76d2d8821907a5f7fa5ae
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
[] =
30 "$Id: pw_user.c,v 1.27 1999/02/23 07:15:10 davidn Exp $";
36 #include <sys/param.h>
40 #include <sys/types.h>
42 #include <sys/resource.h>
45 #if defined(USE_MD5RAND)
51 #if (MAXLOGNAME-1) > UT_NAMESIZE
52 #define LOGNAMESIZE UT_NAMESIZE
54 #define LOGNAMESIZE (MAXLOGNAME-1)
59 static int print_user(struct passwd
* pwd
, int pretty
, int v7
);
60 static uid_t
pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
);
61 static uid_t
pw_gidpolicy(struct userconf
* cnf
, struct cargs
* args
, char *nam
, gid_t prefer
);
62 static time_t pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
);
63 static time_t pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
);
64 static char *pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
);
65 static char *pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
);
66 static char *pw_password(struct userconf
* cnf
, struct cargs
* args
, char const * user
);
67 static char *shell_path(char const * path
, char *shells
[], char *sh
);
68 static void rmat(uid_t uid
);
69 static void rmskey(char const * name
);
72 * -C config configuration file
76 * -c comment user name/comment
77 * -d directory home directory
78 * -e date account expiry date
79 * -p date password expiry date
80 * -g grp primary group
81 * -G grp1,grp2 additional groups
82 * -m [ -k dir ] create and set up home
83 * -s shell name of login shell
86 * -l name new login name
87 * -h fd password filehandle
88 * -F force print or add
90 * -D set user defaults
91 * -b dir default home root dir
92 * -e period default expiry period
93 * -p period default password change period
94 * -g group default group
95 * -G grp1,grp2.. default additional groups
96 * -L class default login class
97 * -k dir default home skeleton
98 * -s shell default shell
99 * -w method default password method
103 pw_user(struct userconf
* cnf
, int mode
, struct cargs
* args
)
110 struct passwd
*pwd
= NULL
;
113 char line
[_PASSWORD_LEN
+1];
115 static struct passwd fakeuser
=
130 * With M_NEXT, we only need to return the
135 uid_t next
= pw_uidpolicy(cnf
, args
);
136 if (getarg(args
, 'q'))
138 printf("%ld:", (long)next
);
139 pw_group(cnf
, mode
, args
);
144 * We can do all of the common legwork here
147 if ((arg
= getarg(args
, 'b')) != NULL
) {
148 cnf
->home
= arg
->val
;
152 * If we'll need to use it or we're updating it,
153 * then create the base home directory if necessary
155 if (arg
!= NULL
|| getarg(args
, 'm') != NULL
) {
156 int l
= strlen(cnf
->home
);
158 if (l
> 1 && cnf
->home
[l
-1] == '/') /* Shave off any trailing path delimiter */
159 cnf
->home
[--l
] = '\0';
161 if (l
< 2 || *cnf
->home
!= '/') /* Check for absolute path name */
162 errx(EX_DATAERR
, "invalid base directory for home '%s'", cnf
->home
);
164 if (stat(cnf
->home
, &st
) == -1) {
165 char dbuf
[MAXPATHLEN
];
168 * This is a kludge especially for Joerg :)
169 * If the home directory would be created in the root partition, then
170 * we really create it under /usr which is likely to have more space.
171 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
173 if (strchr(cnf
->home
+1, '/') == NULL
) {
174 strcpy(dbuf
, "/usr");
175 strncat(dbuf
, cnf
->home
, MAXPATHLEN
-5);
176 if (mkdir(dbuf
, 0755) != -1 || errno
== EEXIST
) {
178 symlink(dbuf
, cnf
->home
);
180 /* If this falls, fall back to old method */
182 p
= strncpy(dbuf
, cnf
->home
, sizeof dbuf
);
183 dbuf
[MAXPATHLEN
-1] = '\0';
184 if (stat(dbuf
, &st
) == -1) {
185 while ((p
= strchr(++p
, '/')) != NULL
) {
187 if (stat(dbuf
, &st
) == -1) {
188 if (mkdir(dbuf
, 0755) == -1)
191 } else if (!S_ISDIR(st
.st_mode
))
192 errx(EX_OSFILE
, "'%s' (root home parent) is not a directory", dbuf
);
196 if (stat(dbuf
, &st
) == -1) {
197 if (mkdir(dbuf
, 0755) == -1) {
198 direrr
: err(EX_OSFILE
, "mkdir '%s'", dbuf
);
202 } else if (!S_ISDIR(st
.st_mode
))
203 errx(EX_OSFILE
, "root home `%s' is not a directory", cnf
->home
);
207 if ((arg
= getarg(args
, 'e')) != NULL
)
208 cnf
->expire_days
= atoi(arg
->val
);
210 if ((arg
= getarg(args
, 'y')) != NULL
)
211 cnf
->nispasswd
= arg
->val
;
213 if ((arg
= getarg(args
, 'p')) != NULL
&& arg
->val
)
214 cnf
->password_days
= atoi(arg
->val
);
216 if ((arg
= getarg(args
, 'g')) != NULL
) {
218 if ((grp
= GETGRNAM(p
)) == NULL
) {
219 if (!isdigit(*p
) || (grp
= GETGRGID((gid_t
) atoi(p
))) == NULL
)
220 errx(EX_NOUSER
, "group `%s' does not exist", p
);
222 cnf
->default_group
= newstr(grp
->gr_name
);
224 if ((arg
= getarg(args
, 'L')) != NULL
)
225 cnf
->default_class
= pw_checkname((u_char
*)arg
->val
, 0);
227 if ((arg
= getarg(args
, 'G')) != NULL
&& arg
->val
) {
230 for (p
= strtok(arg
->val
, ", \t"); p
!= NULL
; p
= strtok(NULL
, ", \t")) {
231 if ((grp
= GETGRNAM(p
)) == NULL
) {
232 if (!isdigit(*p
) || (grp
= GETGRGID((gid_t
) atoi(p
))) == NULL
)
233 errx(EX_NOUSER
, "group `%s' does not exist", p
);
235 if (extendarray(&cnf
->groups
, &cnf
->numgroups
, i
+ 2) != -1)
236 cnf
->groups
[i
++] = newstr(grp
->gr_name
);
238 while (i
< cnf
->numgroups
)
239 cnf
->groups
[i
++] = NULL
;
241 if ((arg
= getarg(args
, 'k')) != NULL
) {
242 if (stat(cnf
->dotdir
= arg
->val
, &st
) == -1 || !S_ISDIR(st
.st_mode
))
243 errx(EX_OSFILE
, "skeleton `%s' is not a directory or does not exist", cnf
->dotdir
);
245 if ((arg
= getarg(args
, 's')) != NULL
)
246 cnf
->shell_default
= arg
->val
;
248 if (mode
== M_ADD
&& getarg(args
, 'D')) {
249 if (getarg(args
, 'n') != NULL
)
250 errx(EX_DATAERR
, "can't combine `-D' with `-n name'");
251 if ((arg
= getarg(args
, 'u')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
252 if ((cnf
->min_uid
= (uid_t
) atoi(p
)) == 0)
254 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_uid
= (uid_t
) atoi(p
)) < cnf
->min_uid
)
255 cnf
->max_uid
= 32000;
257 if ((arg
= getarg(args
, 'i')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
258 if ((cnf
->min_gid
= (gid_t
) atoi(p
)) == 0)
260 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_gid
= (gid_t
) atoi(p
)) < cnf
->min_gid
)
261 cnf
->max_gid
= 32000;
263 if ((arg
= getarg(args
, 'w')) != NULL
)
264 cnf
->default_password
= boolean_val(arg
->val
, cnf
->default_password
);
266 arg
= getarg(args
, 'C');
267 if (write_userconfig(arg
? arg
->val
: NULL
))
269 warn("config update");
272 if (mode
== M_PRINT
&& getarg(args
, 'a')) {
273 int pretty
= getarg(args
, 'P') != NULL
;
274 int v7
= getarg(args
, '7') != NULL
;
277 while ((pwd
= GETPWENT()) != NULL
)
278 print_user(pwd
, pretty
, v7
);
282 if ((a_name
= getarg(args
, 'n')) != NULL
)
283 pwd
= GETPWNAM(pw_checkname((u_char
*)a_name
->val
, 0));
284 a_uid
= getarg(args
, 'u');
288 errx(EX_DATAERR
, "user name or id required");
291 * Determine whether 'n' switch is name or uid - we don't
292 * really don't really care which we have, but we need to
295 if (mode
!= M_ADD
&& pwd
== NULL
296 && strspn(a_name
->val
, "0123456789") == strlen(a_name
->val
)
297 && atoi(a_name
->val
) > 0) { /* Assume uid */
298 (a_uid
= a_name
)->ch
= 'u';
303 * Update, delete & print require that the user exists
305 if (mode
== M_UPDATE
|| mode
== M_DELETE
|| mode
== M_PRINT
) {
306 if (a_name
== NULL
&& pwd
== NULL
) /* Try harder */
307 pwd
= GETPWUID(atoi(a_uid
->val
));
310 if (mode
== M_PRINT
&& getarg(args
, 'F')) {
311 fakeuser
.pw_name
= a_name
? a_name
->val
: "nouser";
312 fakeuser
.pw_uid
= a_uid
? (uid_t
) atol(a_uid
->val
) : -1;
313 return print_user(&fakeuser
,
314 getarg(args
, 'P') != NULL
,
315 getarg(args
, '7') != NULL
);
318 errx(EX_NOUSER
, "no such uid `%s'", a_uid
->val
);
319 errx(EX_NOUSER
, "no such user `%s'", a_name
->val
);
321 if (a_name
== NULL
) /* May be needed later */
322 a_name
= addarg(args
, 'n', newstr(pwd
->pw_name
));
325 * Handle deletions now
327 if (mode
== M_DELETE
) {
328 char file
[MAXPATHLEN
];
329 char home
[MAXPATHLEN
];
330 uid_t uid
= pwd
->pw_uid
;
332 if (strcmp(pwd
->pw_name
, "root") == 0)
333 errx(EX_DATAERR
, "cannot remove user 'root'");
337 * Remove skey record from /etc/skeykeys
340 rmskey(pwd
->pw_name
);
345 sprintf(file
, "/var/cron/tabs/%s", pwd
->pw_name
);
346 if (access(file
, F_OK
) == 0) {
347 sprintf(file
, "crontab -u %s -r", pwd
->pw_name
);
352 * Save these for later, since contents of pwd may be
353 * invalidated by deletion
355 sprintf(file
, "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
356 strncpy(home
, pwd
->pw_dir
, sizeof home
);
357 home
[sizeof home
- 1] = '\0';
360 err(EX_IOERR
, "error updating passwd file");
362 if (cnf
->nispasswd
&& *cnf
->nispasswd
=='/' && !delnispwent(cnf
->nispasswd
, a_name
->val
))
363 warn("WARNING: NIS passwd update");
365 editgroups(a_name
->val
, NULL
);
367 pw_log(cnf
, mode
, W_USER
, "%s(%ld) account removed", a_name
->val
, (long) uid
);
378 if (getpwuid(uid
) == NULL
)
382 * Remove home directory and contents
384 if (getarg(args
, 'r') != NULL
&& *home
== '/' && getpwuid(uid
) == NULL
) {
385 if (stat(home
, &st
) != -1) {
387 pw_log(cnf
, mode
, W_USER
, "%s(%ld) home '%s' %sremoved",
388 a_name
->val
, (long) uid
, home
,
389 stat(home
, &st
) == -1 ? "" : "not completely ");
394 } else if (mode
== M_PRINT
)
395 return print_user(pwd
,
396 getarg(args
, 'P') != NULL
,
397 getarg(args
, '7') != NULL
);
400 * The rest is edit code
402 if ((arg
= getarg(args
, 'l')) != NULL
) {
403 if (strcmp(pwd
->pw_name
, "root") == 0)
404 errx(EX_DATAERR
, "can't rename `root' account");
405 pwd
->pw_name
= pw_checkname((u_char
*)arg
->val
, 0);
407 if ((arg
= getarg(args
, 'u')) != NULL
&& isdigit(*arg
->val
)) {
408 pwd
->pw_uid
= (uid_t
) atol(arg
->val
);
409 if (pwd
->pw_uid
!= 0 && strcmp(pwd
->pw_name
, "root") == 0)
410 errx(EX_DATAERR
, "can't change uid of `root' account");
411 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
412 warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd
->pw_name
);
414 if ((arg
= getarg(args
, 'g')) != NULL
&& pwd
->pw_uid
!= 0) /* Already checked this */
415 pwd
->pw_gid
= (gid_t
) GETGRNAM(cnf
->default_group
)->gr_gid
;
417 if ((arg
= getarg(args
, 'p')) != NULL
) {
418 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0)
421 time_t now
= time(NULL
);
422 time_t expire
= parse_date(now
, arg
->val
);
425 errx(EX_DATAERR
, "invalid password change date `%s'", arg
->val
);
426 pwd
->pw_change
= expire
;
429 if ((arg
= getarg(args
, 'e')) != NULL
) {
430 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0)
433 time_t now
= time(NULL
);
434 time_t expire
= parse_date(now
, arg
->val
);
437 errx(EX_DATAERR
, "invalid account expiry date `%s'", arg
->val
);
438 pwd
->pw_expire
= expire
;
441 if ((arg
= getarg(args
, 's')) != NULL
)
442 pwd
->pw_shell
= shell_path(cnf
->shelldir
, cnf
->shells
, arg
->val
);
444 if (getarg(args
, 'L'))
445 pwd
->pw_class
= cnf
->default_class
;
447 if ((arg
= getarg(args
, 'd')) != NULL
) {
448 if (stat(pwd
->pw_dir
= arg
->val
, &st
) == -1) {
449 if (getarg(args
, 'm') == NULL
&& strcmp(pwd
->pw_dir
, "/nonexistent") != 0)
450 warnx("WARNING: home `%s' does not exist", pwd
->pw_dir
);
451 } else if (!S_ISDIR(st
.st_mode
))
452 warnx("WARNING: home `%s' is not a directory", pwd
->pw_dir
);
455 if ((arg
= getarg(args
, 'w')) != NULL
&& getarg(args
, 'h') == NULL
)
456 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
459 if (a_name
== NULL
) /* Required */
460 errx(EX_DATAERR
, "login name required");
461 else if ((pwd
= GETPWNAM(a_name
->val
)) != NULL
) /* Exists */
462 errx(EX_DATAERR
, "login name `%s' already exists", a_name
->val
);
465 * Now, set up defaults for a new user
468 pwd
->pw_name
= a_name
->val
;
469 pwd
->pw_class
= cnf
->default_class
? cnf
->default_class
: "";
470 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
471 pwd
->pw_uid
= pw_uidpolicy(cnf
, args
);
472 pwd
->pw_gid
= pw_gidpolicy(cnf
, args
, pwd
->pw_name
, (gid_t
) pwd
->pw_uid
);
473 pwd
->pw_change
= pw_pwdpolicy(cnf
, args
);
474 pwd
->pw_expire
= pw_exppolicy(cnf
, args
);
475 pwd
->pw_dir
= pw_homepolicy(cnf
, args
, pwd
->pw_name
);
476 pwd
->pw_shell
= pw_shellpolicy(cnf
, args
, NULL
);
478 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
479 warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd
->pw_name
);
483 * Shared add/edit code
485 if ((arg
= getarg(args
, 'c')) != NULL
)
486 pwd
->pw_gecos
= pw_checkname((u_char
*)arg
->val
, 1);
488 if ((arg
= getarg(args
, 'h')) != NULL
) {
489 if (strcmp(arg
->val
, "-") == 0)
490 pwd
->pw_passwd
= "*"; /* No access */
492 int fd
= atoi(arg
->val
);
494 int istty
= isatty(fd
);
498 if (tcgetattr(fd
, &t
) == -1)
501 struct termios n
= t
;
504 n
.c_lflag
&= ~(ECHO
);
505 tcsetattr(fd
, TCSANOW
, &n
);
506 printf("%sassword for user %s:", (mode
== M_UPDATE
) ? "New p" : "P", pwd
->pw_name
);
510 b
= read(fd
, line
, sizeof(line
) - 1);
511 if (istty
) { /* Restore state */
512 tcsetattr(fd
, TCSANOW
, &t
);
517 warn("-h file descriptor");
521 if ((p
= strpbrk(line
, " \t\r\n")) != NULL
)
524 errx(EX_DATAERR
, "empty password read on file descriptor %d", fd
);
525 pwd
->pw_passwd
= pw_pwcrypt(line
);
530 * Special case: -N only displays & exits
532 if (getarg(args
, 'N') != NULL
)
533 return print_user(pwd
,
534 getarg(args
, 'P') != NULL
,
535 getarg(args
, '7') != NULL
);
540 if (r
&& cnf
->nispasswd
&& *cnf
->nispasswd
=='/')
541 r1
= addnispwent(cnf
->nispasswd
, pwd
);
542 } else if (mode
== M_UPDATE
) {
543 r
= chgpwent(a_name
->val
, pwd
);
544 if (r
&& cnf
->nispasswd
&& *cnf
->nispasswd
=='/')
545 r1
= chgnispwent(cnf
->nispasswd
, a_name
->val
, pwd
);
549 warn("password update");
552 warn("WARNING: NIS password update");
553 /* Keep on trucking */
557 * Ok, user is created or changed - now edit group file
560 if (mode
== M_ADD
|| getarg(args
, 'G') != NULL
)
561 editgroups(pwd
->pw_name
, cnf
->groups
);
563 /* pwd may have been invalidated */
564 if ((pwd
= GETPWNAM(a_name
->val
)) == NULL
)
565 errx(EX_NOUSER
, "user '%s' disappeared during update", a_name
->val
);
567 grp
= GETGRGID(pwd
->pw_gid
);
568 pw_log(cnf
, mode
, W_USER
, "%s(%ld):%s(%d):%s:%s:%s",
569 pwd
->pw_name
, (long) pwd
->pw_uid
,
570 grp
? grp
->gr_name
: "unknown", (long) (grp
? grp
->gr_gid
: -1),
571 pwd
->pw_gecos
, pwd
->pw_dir
, pwd
->pw_shell
);
574 * If adding, let's touch and chown the user's mail file. This is not
575 * strictly necessary under BSD with a 0755 maildir but it also
576 * doesn't hurt anything to create the empty mailfile
582 sprintf(line
, "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
583 close(open(line
, O_RDWR
| O_CREAT
, 0600)); /* Preserve contents &
585 chown(line
, pwd
->pw_uid
, pwd
->pw_gid
);
588 * Send mail to the new user as well, if we are asked to
590 if (cnf
->newmail
&& *cnf
->newmail
&& (fp
= fopen(cnf
->newmail
, "r")) != NULL
) {
591 FILE *pfp
= popen(_PATH_SENDMAIL
" -t", "w");
596 fprintf(pfp
, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd
->pw_name
);
597 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
598 /* Do substitutions? */
602 pw_log(cnf
, mode
, W_USER
, "%s(%ld) new user mail sent",
603 pwd
->pw_name
, (long) pwd
->pw_uid
);
610 * Finally, let's create and populate the user's home directory. Note
611 * that this also `works' for editing users if -m is used, but
612 * existing files will *not* be overwritten.
614 if (!PWALTDIR() && getarg(args
, 'm') != NULL
&& pwd
->pw_dir
&& *pwd
->pw_dir
== '/' && pwd
->pw_dir
[1]) {
615 copymkdir(pwd
->pw_dir
, cnf
->dotdir
, 0755, pwd
->pw_uid
, pwd
->pw_gid
);
616 pw_log(cnf
, mode
, W_USER
, "%s(%ld) home %s made",
617 pwd
->pw_name
, (long) pwd
->pw_uid
, pwd
->pw_dir
);
624 pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
)
627 uid_t uid
= (uid_t
) - 1;
628 struct carg
*a_uid
= getarg(args
, 'u');
631 * Check the given uid, if any
634 uid
= (uid_t
) atol(a_uid
->val
);
636 if ((pwd
= GETPWUID(uid
)) != NULL
&& getarg(args
, 'o') == NULL
)
637 errx(EX_DATAERR
, "uid `%ld' has already been allocated", (long) pwd
->pw_uid
);
642 * We need to allocate the next available uid under one of
643 * two policies a) Grab the first unused uid b) Grab the
644 * highest possible unused uid
646 if (cnf
->min_uid
>= cnf
->max_uid
) { /* Sanity
647 * claus^H^H^H^Hheck */
649 cnf
->max_uid
= 32000;
651 bm
= bm_alloc(cnf
->max_uid
- cnf
->min_uid
+ 1);
654 * Now, let's fill the bitmap from the password file
657 while ((pwd
= GETPWENT()) != NULL
)
658 if (pwd
->pw_uid
>= (uid_t
) cnf
->min_uid
&& pwd
->pw_uid
<= (uid_t
) cnf
->max_uid
)
659 bm_setbit(&bm
, pwd
->pw_uid
- cnf
->min_uid
);
663 * Then apply the policy, with fallback to reuse if necessary
665 if (cnf
->reuse_uids
|| (uid
= (uid_t
) (bm_lastset(&bm
) + cnf
->min_uid
+ 1)) > cnf
->max_uid
)
666 uid
= (uid_t
) (bm_firstunset(&bm
) + cnf
->min_uid
);
669 * Another sanity check
671 if (uid
< cnf
->min_uid
|| uid
> cnf
->max_uid
)
672 errx(EX_SOFTWARE
, "unable to allocate a new uid - range fully used");
680 pw_gidpolicy(struct userconf
* cnf
, struct cargs
* args
, char *nam
, gid_t prefer
)
683 gid_t gid
= (uid_t
) - 1;
684 struct carg
*a_gid
= getarg(args
, 'g');
687 * If no arg given, see if default can help out
689 if (a_gid
== NULL
&& cnf
->default_group
&& *cnf
->default_group
)
690 a_gid
= addarg(args
, 'g', cnf
->default_group
);
693 * Check the given gid, if any
697 if ((grp
= GETGRNAM(a_gid
->val
)) == NULL
) {
698 gid
= (gid_t
) atol(a_gid
->val
);
699 if ((gid
== 0 && !isdigit(*a_gid
->val
)) || (grp
= GETGRGID(gid
)) == NULL
)
700 errx(EX_NOUSER
, "group `%s' is not defined", a_gid
->val
);
703 } else if ((grp
= GETGRNAM(nam
)) != NULL
&& grp
->gr_mem
[0] == NULL
) {
704 gid
= grp
->gr_gid
; /* Already created? Use it anyway... */
706 struct cargs grpargs
;
710 addarg(&grpargs
, 'n', nam
);
713 * We need to auto-create a group with the user's name. We
714 * can send all the appropriate output to our sister routine
715 * bit first see if we can create a group with gid==uid so we
716 * can keep the user and group ids in sync. We purposely do
717 * NOT check the gid range if we can force the sync. If the
718 * user's name dups an existing group, then the group add
719 * function will happily handle that case for us and exit.
721 if (GETGRGID(prefer
) == NULL
) {
722 sprintf(tmp
, "%lu", (unsigned long) prefer
);
723 addarg(&grpargs
, 'g', tmp
);
725 if (getarg(args
, 'N'))
727 addarg(&grpargs
, 'N', NULL
);
728 addarg(&grpargs
, 'q', NULL
);
729 gid
= pw_group(cnf
, M_NEXT
, &grpargs
);
733 pw_group(cnf
, M_ADD
, &grpargs
);
734 if ((grp
= GETGRNAM(nam
)) != NULL
)
737 a_gid
= grpargs
.lh_first
;
738 while (a_gid
!= NULL
) {
739 struct carg
*t
= a_gid
->list
.le_next
;
740 LIST_REMOVE(a_gid
, list
);
750 pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
)
753 time_t now
= time(NULL
);
754 struct carg
*arg
= getarg(args
, 'p');
757 if ((result
= parse_date(now
, arg
->val
)) == now
)
758 errx(EX_DATAERR
, "invalid date/time `%s'", arg
->val
);
759 } else if (cnf
->password_days
> 0)
760 result
= now
+ ((long) cnf
->password_days
* 86400L);
766 pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
)
769 time_t now
= time(NULL
);
770 struct carg
*arg
= getarg(args
, 'e');
773 if ((result
= parse_date(now
, arg
->val
)) == now
)
774 errx(EX_DATAERR
, "invalid date/time `%s'", arg
->val
);
775 } else if (cnf
->expire_days
> 0)
776 result
= now
+ ((long) cnf
->expire_days
* 86400L);
782 pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
784 struct carg
*arg
= getarg(args
, 'd');
789 static char home
[128];
791 if (cnf
->home
== NULL
|| *cnf
->home
== '\0')
792 errx(EX_CONFIG
, "no base home directory set");
793 sprintf(home
, "%s/%s", cnf
->home
, user
);
799 shell_path(char const * path
, char *shells
[], char *sh
)
801 if (sh
!= NULL
&& (*sh
== '/' || *sh
== '\0'))
802 return sh
; /* specified full path or forced none */
805 char paths
[_UC_MAXLINE
];
808 * We need to search paths
810 strncpy(paths
, path
, sizeof paths
);
811 paths
[sizeof paths
- 1] = '\0';
812 for (p
= strtok(paths
, ": \t\r\n"); p
!= NULL
; p
= strtok(NULL
, ": \t\r\n")) {
814 static char shellpath
[256];
817 sprintf(shellpath
, "%s/%s", p
, sh
);
818 if (access(shellpath
, X_OK
) == 0)
821 for (i
= 0; i
< _UC_MAXSHELLS
&& shells
[i
] != NULL
; i
++) {
822 sprintf(shellpath
, "%s/%s", p
, shells
[i
]);
823 if (access(shellpath
, X_OK
) == 0)
828 errx(EX_OSFILE
, "can't find shell `%s' in shell paths", sh
);
829 errx(EX_CONFIG
, "no default shell available or defined");
836 pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
)
839 struct carg
*arg
= getarg(args
, 's');
841 if (newshell
== NULL
&& arg
!= NULL
)
843 return shell_path(cnf
->shelldir
, cnf
->shells
, sh
? sh
: cnf
->shell_default
);
846 static char const chars
[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
849 pw_pwcrypt(char *password
)
854 static char buf
[256];
857 * Calculate a salt value
864 srandom((unsigned long) (time(NULL
) ^ getpid()));
867 for (i
= 0; i
< 8; i
++)
868 salt
[i
] = chars
[random() % 63];
871 return strcpy(buf
, crypt(password
, salt
));
874 #if defined(USE_MD5RAND)
876 pw_getrand(u_char
*buf
, int len
) /* cryptographically secure rng */
879 for (i
=0;i
<len
;i
+=16) {
883 struct timeval tv
, tvo
;
890 MD5Update (&md5_ctx
, (u_char
*)&t
, sizeof t
);
892 MD5Update (&md5_ctx
, (u_char
*)&t
, sizeof t
);
893 gettimeofday (&tvo
, NULL
);
895 getrusage (RUSAGE_SELF
, &ru
);
896 MD5Update (&md5_ctx
, (u_char
*)&ru
, sizeof ru
);
897 gettimeofday (&tv
, NULL
);
898 MD5Update (&md5_ctx
, (u_char
*)&tv
, sizeof tv
);
899 } while (n
++<20 || tv
.tv_usec
-tvo
.tv_usec
<100*1000);
900 MD5Final (ubuf
, &md5_ctx
);
901 memcpy(buf
+i
, ubuf
, MIN(16, len
-n
));
906 #else /* Portable version */
909 pw_getrand(u_char
*buf
, int len
)
913 for (i
= 0; i
< len
; i
++) {
914 unsigned long val
= random();
915 /* Use all bits in the random value */
916 buf
[i
]=(u_char
)((val
>> 24) ^ (val
>> 16) ^ (val
>> 8) ^ val
);
924 pw_password(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
928 u_char rndbuf
[sizeof pwbuf
];
930 switch (cnf
->default_password
) {
931 case -1: /* Random password */
937 srandom((unsigned long) (time(NULL
) ^ getpid()));
940 l
= (random() % 8 + 8); /* 8 - 16 chars */
941 pw_getrand(rndbuf
, l
);
942 for (i
= 0; i
< l
; i
++)
943 pwbuf
[i
] = chars
[rndbuf
[i
] % (sizeof(chars
)-1)];
947 * We give this information back to the user
949 if (getarg(args
, 'h') == NULL
&& getarg(args
, 'N') == NULL
) {
951 printf("Password for '%s' is: ", user
);
952 printf("%s\n", pwbuf
);
957 case -2: /* No password at all! */
960 case 0: /* No login - default */
964 case 1: /* user's name */
965 strncpy(pwbuf
, user
, sizeof pwbuf
);
966 pwbuf
[sizeof pwbuf
- 1] = '\0';
969 return pw_pwcrypt(pwbuf
);
974 print_user(struct passwd
* pwd
, int pretty
, int v7
)
977 char buf
[_UC_MAXLINE
];
979 fmtpwentry(buf
, pwd
, v7
? PWF_PASSWD
: PWF_STANDARD
);
984 struct group
*grp
= GETGRGID(pwd
->pw_gid
);
985 char uname
[60] = "User &", office
[60] = "[None]",
986 wphone
[60] = "[None]", hphone
[60] = "[None]";
987 char acexpire
[32] = "[None]", pwexpire
[32] = "[None]";
990 if ((p
= strtok(pwd
->pw_gecos
, ",")) != NULL
) {
991 strncpy(uname
, p
, sizeof uname
);
992 uname
[sizeof uname
- 1] = '\0';
993 if ((p
= strtok(NULL
, ",")) != NULL
) {
994 strncpy(office
, p
, sizeof office
);
995 office
[sizeof office
- 1] = '\0';
996 if ((p
= strtok(NULL
, ",")) != NULL
) {
997 strncpy(wphone
, p
, sizeof wphone
);
998 wphone
[sizeof wphone
- 1] = '\0';
999 if ((p
= strtok(NULL
, "")) != NULL
) {
1000 strncpy(hphone
, p
, sizeof hphone
);
1001 hphone
[sizeof hphone
- 1] = '\0';
1007 * Handle '&' in gecos field
1009 if ((p
= strchr(uname
, '&')) != NULL
) {
1010 int l
= strlen(pwd
->pw_name
);
1013 memmove(p
+ l
, p
+ 1, m
);
1014 memmove(p
, pwd
->pw_name
, l
);
1015 *p
= (char) toupper(*p
);
1017 if (pwd
->pw_expire
> (time_t)0 && (tptr
= localtime(&pwd
->pw_expire
)) != NULL
)
1018 strftime(acexpire
, sizeof acexpire
, "%c", tptr
);
1019 if (pwd
->pw_change
> (time_t)9 && (tptr
= localtime(&pwd
->pw_change
)) != NULL
)
1020 strftime(pwexpire
, sizeof pwexpire
, "%c", tptr
);
1021 printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n"
1023 " Home: %-26.26s Class: %s\n"
1024 " Shell: %-26.26s Office: %s\n"
1025 "Work Phone: %-26.26s Home Phone: %s\n"
1026 "Acc Expire: %-26.26s Pwd Expire: %s\n",
1027 pwd
->pw_name
, (long) pwd
->pw_uid
,
1028 grp
? grp
->gr_name
: "(invalid)", (long) pwd
->pw_gid
,
1029 uname
, pwd
->pw_dir
, pwd
->pw_class
,
1030 pwd
->pw_shell
, office
, wphone
, hphone
,
1031 acexpire
, pwexpire
);
1034 while ((grp
=GETGRENT()) != NULL
)
1037 while (grp
->gr_mem
[i
] != NULL
)
1039 if (strcmp(grp
->gr_mem
[i
], pwd
->pw_name
)==0)
1041 printf(j
++ == 0 ? " Groups: %s" : ",%s", grp
->gr_name
);
1048 printf("%s\n", j
? "\n" : "");
1050 return EXIT_SUCCESS
;
1054 pw_checkname(u_char
*name
, int gecos
)
1057 char const *notch
= gecos
? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1060 if (strchr(notch
, name
[l
]) != NULL
|| name
[l
] < ' ' || name
[l
] == 127 ||
1061 (!gecos
&& l
==0 && name
[l
] == '-') || /* leading '-' */
1062 (!gecos
&& name
[l
] & 0x80)) /* 8-bit */
1063 errx(EX_DATAERR
, (name
[l
] >= ' ' && name
[l
] < 127)
1064 ? "invalid character `%c' in field"
1065 : "invalid character 0x%02x in field",
1069 if (!gecos
&& l
> LOGNAMESIZE
)
1070 errx(EX_DATAERR
, "name too long `%s'", name
);
1071 return (char *)name
;
1078 DIR *d
= opendir("/var/at/jobs");
1083 while ((e
= readdir(d
)) != NULL
) {
1086 if (strncmp(e
->d_name
, ".lock", 5) != 0 &&
1087 stat(e
->d_name
, &st
) == 0 &&
1088 !S_ISDIR(st
.st_mode
) &&
1090 char tmp
[MAXPATHLEN
];
1092 sprintf(tmp
, "/usr/bin/atrm %s", e
->d_name
);
1101 rmskey(char const * name
)
1103 static const char etcskey
[] = "/etc/skeykeys";
1104 FILE *fp
= fopen(etcskey
, "r+");
1109 int length
= strlen(name
);
1111 while (fgets(tmp
, sizeof tmp
, fp
) != NULL
) {
1112 if (strncmp(name
, tmp
, length
) == 0 && tmp
[length
]==' ') {
1113 if (fseek(fp
, atofs
, SEEK_SET
) == 0) {
1114 fwrite("#", 1, 1, fp
); /* Comment username out */
1121 * If we got an error of any sort, don't update!