]>
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
26 * $Id: pw_user.c,v 1.4 1996/12/17 01:43:30 davidn Exp $
33 #include <sys/param.h>
36 #include <sys/types.h>
38 #include <sys/resource.h>
44 static int print_user(struct passwd
* pwd
, int pretty
);
45 static uid_t
pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
);
46 static uid_t
pw_gidpolicy(struct userconf
* cnf
, struct cargs
* args
, char *nam
, gid_t prefer
);
47 static time_t pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
);
48 static time_t pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
);
49 static char *pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
);
50 static char *pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
);
51 static char *pw_password(struct userconf
* cnf
, struct cargs
* args
, char const * user
);
52 static char *pw_checkname(char *name
, int gecos
);
53 static char *shell_path(char const * path
, char *shells
[], char *sh
);
54 static void rmat(uid_t uid
);
57 * -C config configuration file
61 * -c comment user name/comment
62 * -d directory home directory
63 * -e date account expiry date
64 * -p date password expiry date
65 * -g grp primary group
66 * -G grp1,grp2 additional groups
67 * -m [ -k dir ] create and set up home
68 * -s shell name of login shell
71 * -l name new login name
72 * -h fd password filehandle
73 * -F force print or add
75 * -D set user defaults
76 * -b dir default home root dir
77 * -e period default expiry period
78 * -p period default password change period
79 * -g group default group
80 * -G grp1,grp2.. default additional groups
81 * -L class default login class
82 * -k dir default home skeleton
83 * -s shell default shell
84 * -w method default password method
88 pw_user(struct userconf
* cnf
, int mode
, struct cargs
* args
)
94 struct passwd
*pwd
= NULL
;
99 static struct passwd fakeuser
=
114 * With M_NEXT, we only need to return the
119 uid_t next
= pw_uidpolicy(cnf
, args
);
120 if (getarg(args
, 'q'))
122 printf("%ld:", (long)next
);
123 pw_group(cnf
, mode
, args
);
128 * We can do all of the common legwork here
131 if ((arg
= getarg(args
, 'b')) != NULL
) {
132 cnf
->home
= arg
->val
;
133 if (stat(cnf
->home
, &st
) == -1 || !S_ISDIR(st
.st_mode
))
134 cmderr(EX_OSFILE
, "root home `%s' is not a directory or does not exist\n", cnf
->home
);
136 if ((arg
= getarg(args
, 'e')) != NULL
)
137 cnf
->expire_days
= atoi(arg
->val
);
139 if ((arg
= getarg(args
, 'p')) != NULL
&& arg
->val
)
140 cnf
->password_days
= atoi(arg
->val
);
142 if ((arg
= getarg(args
, 'g')) != NULL
) {
144 if ((grp
= getgrnam(p
)) == NULL
) {
145 if (!isdigit(*p
) || (grp
= getgrgid((gid_t
) atoi(p
))) == NULL
)
146 cmderr(EX_NOUSER
, "group `%s' does not exist\n", p
);
148 cnf
->default_group
= newstr(grp
->gr_name
);
150 if ((arg
= getarg(args
, 'L')) != NULL
)
151 cnf
->default_class
= pw_checkname(arg
->val
, 0);
153 if ((arg
= getarg(args
, 'G')) != NULL
&& arg
->val
) {
156 for (p
= strtok(arg
->val
, ", \t"); i
< _UC_MAXGROUPS
&& p
!= NULL
; p
= strtok(NULL
, ", \t")) {
157 if ((grp
= getgrnam(p
)) == NULL
) {
158 if (!isdigit(*p
) || (grp
= getgrgid((gid_t
) atoi(p
))) == NULL
)
159 cmderr(EX_NOUSER
, "group `%s' does not exist\n", p
);
161 cnf
->groups
[i
++] = newstr(grp
->gr_name
);
163 while (i
< _UC_MAXGROUPS
)
164 cnf
->groups
[i
++] = NULL
;
166 if ((arg
= getarg(args
, 'k')) != NULL
) {
167 if (stat(cnf
->dotdir
= arg
->val
, &st
) == -1 || S_ISDIR(st
.st_mode
))
168 cmderr(EX_OSFILE
, "skeleton `%s' is not a directory or does not exist\n", cnf
->dotdir
);
170 if ((arg
= getarg(args
, 's')) != NULL
)
171 cnf
->shell_default
= arg
->val
;
173 if (mode
== M_ADD
&& getarg(args
, 'D')) {
174 if (getarg(args
, 'n') != NULL
)
175 cmderr(EX_DATAERR
, "can't combine `-D' with `-n name'\n");
176 if ((arg
= getarg(args
, 'u')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
177 if ((cnf
->min_uid
= (uid_t
) atoi(p
)) == 0)
179 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_uid
= (uid_t
) atoi(p
)) < cnf
->min_uid
)
180 cnf
->max_uid
= 32000;
182 if ((arg
= getarg(args
, 'i')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
183 if ((cnf
->min_gid
= (gid_t
) atoi(p
)) == 0)
185 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_gid
= (gid_t
) atoi(p
)) < cnf
->min_gid
)
186 cnf
->max_gid
= 32000;
188 if ((arg
= getarg(args
, 'w')) != NULL
)
189 cnf
->default_password
= boolean_val(arg
->val
, cnf
->default_password
);
191 arg
= getarg(args
, 'C');
192 if (write_userconfig(arg
? arg
->val
: NULL
))
194 perror("config update");
197 if (mode
== M_PRINT
&& getarg(args
, 'a')) {
198 int pretty
= getarg(args
, 'P') != NULL
;
201 while ((pwd
= getpwent()) != NULL
)
202 print_user(pwd
, pretty
);
206 if ((a_name
= getarg(args
, 'n')) != NULL
)
207 pwd
= getpwnam(pw_checkname(a_name
->val
, 0));
208 a_uid
= getarg(args
, 'u');
212 cmderr(EX_DATAERR
, "user name or id required\n");
215 * Determine whether 'n' switch is name or uid - we don't
216 * really don't really care which we have, but we need to
219 if (mode
!= M_ADD
&& pwd
== NULL
&& isdigit(*a_name
->val
) && atoi(a_name
->val
) > 0) { /* Assume uid */
220 (a_uid
= a_name
)->ch
= 'u';
225 * Update, delete & print require that the user exists
227 if (mode
== M_UPDATE
|| mode
== M_DELETE
|| mode
== M_PRINT
) {
228 if (a_name
== NULL
&& pwd
== NULL
) /* Try harder */
229 pwd
= getpwuid(atoi(a_uid
->val
));
232 if (mode
== M_PRINT
&& getarg(args
, 'F')) {
233 fakeuser
.pw_name
= a_name
? a_name
->val
: "nouser";
234 fakeuser
.pw_uid
= a_uid
? (uid_t
) atol(a_uid
->val
) : -1;
235 return print_user(&fakeuser
, getarg(args
, 'P') != NULL
);
238 cmderr(EX_NOUSER
, "no such uid `%s'\n", a_uid
->val
);
239 cmderr(EX_NOUSER
, "no such user `%s'\n", a_name
->val
);
241 if (a_name
== NULL
) /* May be needed later */
242 a_name
= addarg(args
, 'n', newstr(pwd
->pw_name
));
245 * Handle deletions now
247 if (mode
== M_DELETE
) {
248 char file
[MAXPATHLEN
];
249 char home
[MAXPATHLEN
];
250 uid_t uid
= pwd
->pw_uid
;
252 if (strcmp(pwd
->pw_name
, "root") == 0)
253 cmderr(EX_DATAERR
, "cannot remove user 'root'\n");
258 sprintf(file
, "/var/cron/tabs/%s", pwd
->pw_name
);
259 if (access(file
, F_OK
) == 0) {
260 sprintf(file
, "crontab -u %s -r", pwd
->pw_name
);
264 * Save these for later, since contents of pwd may be
265 * invalidated by deletion
267 sprintf(file
, "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
268 strncpy(home
, pwd
->pw_dir
, sizeof home
);
269 home
[sizeof home
- 1] = '\0';
272 cmderr(EX_IOERR
, "Error updating passwd file: %s\n", strerror(errno
));
273 editgroups(a_name
->val
, NULL
);
275 pw_log(cnf
, mode
, W_USER
, "%s(%ld) account removed", a_name
->val
, (long) uid
);
285 if (getpwuid(uid
) == NULL
)
289 * Remove home directory and contents
291 if (getarg(args
, 'r') != NULL
&& *home
== '/' && getpwuid(uid
) == NULL
) {
292 if (stat(home
, &st
) != -1) {
294 pw_log(cnf
, mode
, W_USER
, "%s(%ld) home '%s' %sremoved",
295 a_name
->val
, (long) uid
, home
,
296 stat(home
, &st
) == -1 ? "" : "not completely ");
300 } else if (mode
== M_PRINT
)
301 return print_user(pwd
, getarg(args
, 'P') != NULL
);
304 * The rest is edit code
306 if ((arg
= getarg(args
, 'l')) != NULL
) {
307 if (strcmp(pwd
->pw_name
, "root") == 0)
308 cmderr(EX_DATAERR
, "can't rename `root' account\n");
309 pwd
->pw_name
= pw_checkname(arg
->val
, 0);
311 if ((arg
= getarg(args
, 'u')) != NULL
&& isdigit(*arg
->val
)) {
312 pwd
->pw_uid
= (uid_t
) atol(arg
->val
);
313 if (pwd
->pw_uid
!= 0 && strcmp(pwd
->pw_name
, "root") == 0)
314 cmderr(EX_DATAERR
, "can't change uid of `root' account\n");
315 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
316 fprintf(stderr
, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd
->pw_name
);
318 if ((arg
= getarg(args
, 'g')) != NULL
&& pwd
->pw_uid
!= 0) /* Already checked this */
319 pwd
->pw_gid
= (gid_t
) getgrnam(cnf
->default_group
)->gr_gid
;
321 if ((arg
= getarg(args
, 'p')) != NULL
) {
322 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0)
325 time_t now
= time(NULL
);
326 time_t expire
= parse_date(now
, arg
->val
);
329 cmderr(EX_DATAERR
, "Invalid password change date `%s'\n", arg
->val
);
330 pwd
->pw_change
= expire
;
333 if ((arg
= getarg(args
, 'e')) != NULL
) {
334 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0)
337 time_t now
= time(NULL
);
338 time_t expire
= parse_date(now
, arg
->val
);
341 cmderr(EX_DATAERR
, "Invalid account expiry date `%s'\n", arg
->val
);
342 pwd
->pw_expire
= expire
;
345 if ((arg
= getarg(args
, 's')) != NULL
)
346 pwd
->pw_shell
= shell_path(cnf
->shelldir
, cnf
->shells
, arg
->val
);
348 if (getarg(args
, 'L'))
349 pwd
->pw_class
= cnf
->default_class
;
351 if ((arg
= getarg(args
, 'w')) != NULL
&& getarg(args
, 'h') == NULL
)
352 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
355 if (a_name
== NULL
) /* Required */
356 cmderr(EX_DATAERR
, "login name required\n");
357 else if ((pwd
= getpwnam(a_name
->val
)) != NULL
) /* Exists */
358 cmderr(EX_DATAERR
, "login name `%s' already exists\n", a_name
->val
);
361 * Now, set up defaults for a new user
364 pwd
->pw_name
= a_name
->val
;
365 pwd
->pw_class
= cnf
->default_class
? cnf
->default_class
: "";
366 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
367 pwd
->pw_uid
= pw_uidpolicy(cnf
, args
);
368 pwd
->pw_gid
= pw_gidpolicy(cnf
, args
, pwd
->pw_name
, (gid_t
) pwd
->pw_uid
);
369 pwd
->pw_change
= pw_pwdpolicy(cnf
, args
);
370 pwd
->pw_expire
= pw_exppolicy(cnf
, args
);
371 pwd
->pw_dir
= pw_homepolicy(cnf
, args
, pwd
->pw_name
);
372 pwd
->pw_shell
= pw_shellpolicy(cnf
, args
, NULL
);
374 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
375 fprintf(stderr
, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd
->pw_name
);
379 * Shared add/edit code
381 if ((arg
= getarg(args
, 'c')) != NULL
)
382 pwd
->pw_gecos
= pw_checkname(arg
->val
, 1);
384 if ((arg
= getarg(args
, 'h')) != NULL
) {
385 if (strcmp(arg
->val
, "-") == 0)
386 pwd
->pw_passwd
= "*"; /* No access */
388 int fd
= atoi(arg
->val
);
390 int istty
= isatty(fd
);
394 if (tcgetattr(fd
, &t
) == -1)
397 struct termios n
= t
;
400 n
.c_lflag
&= ~(ECHO
);
401 tcsetattr(fd
, TCSANOW
, &n
);
402 printf("%sassword for user %s:", (mode
== M_UPDATE
) ? "New p" : "P", pwd
->pw_name
);
406 b
= read(fd
, line
, sizeof(line
) - 1);
407 if (istty
) { /* Restore state */
408 tcsetattr(fd
, TCSANOW
, &t
);
413 perror("-h file descriptor");
417 if ((p
= strpbrk(line
, " \t\r\n")) != NULL
)
420 cmderr(EX_DATAERR
, "empty password read on file descriptor %d\n", fd
);
421 pwd
->pw_passwd
= pw_pwcrypt(line
);
426 * Special case: -N only displays & exits
428 if (getarg(args
, 'N') != NULL
)
429 return print_user(pwd
, getarg(args
, 'P') != NULL
);
431 if ((mode
== M_ADD
&& !addpwent(pwd
)) ||
432 (mode
== M_UPDATE
&& !chgpwent(a_name
->val
, pwd
))) {
433 perror("password update");
437 * Ok, user is created or changed - now edit group file
440 if (mode
== M_ADD
|| getarg(args
, 'G') != NULL
)
441 editgroups(pwd
->pw_name
, cnf
->groups
);
443 /* pwd may have been invalidated */
444 if ((pwd
= getpwnam(a_name
->val
)) == NULL
)
445 cmderr(EX_NOUSER
, "user '%s' disappeared during update\n", a_name
->val
);
447 grp
= getgrgid(pwd
->pw_gid
);
448 pw_log(cnf
, mode
, W_USER
, "%s(%ld):%s(%d):%s:%s:%s",
449 pwd
->pw_name
, (long) pwd
->pw_uid
,
450 grp
? grp
->gr_name
: "unknown", (long) (grp
? grp
->gr_gid
: -1),
451 pwd
->pw_gecos
, pwd
->pw_dir
, pwd
->pw_shell
);
454 * If adding, let's touch and chown the user's mail file. This is not
455 * strictly necessary under BSD with a 0755 maildir but it also
456 * doesn't hurt anything to create the empty mailfile
461 sprintf(line
, "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
462 close(open(line
, O_RDWR
| O_CREAT
, 0600)); /* Preserve contents &
464 chown(line
, pwd
->pw_uid
, pwd
->pw_gid
);
467 * Send mail to the new user as well, if we are asked to
469 if (cnf
->newmail
&& *cnf
->newmail
&& (fp
= fopen(cnf
->newmail
, "r")) != NULL
) {
470 FILE *pfp
= popen(_PATH_SENDMAIL
" -t", "w");
475 fprintf(pfp
, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd
->pw_name
);
476 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
477 /* Do substitutions? */
481 pw_log(cnf
, mode
, W_USER
, "%s(%ld) new user mail sent",
482 pwd
->pw_name
, (long) pwd
->pw_uid
);
488 * Finally, let's create and populate the user's home directory. Note
489 * that this also `works' for editing users if -m is used, but
490 * existing files will *not* be overwritten.
492 if (getarg(args
, 'm') != NULL
&& pwd
->pw_dir
&& *pwd
->pw_dir
== '/' && pwd
->pw_dir
[1]) {
493 copymkdir(pwd
->pw_dir
, cnf
->dotdir
, 0755, pwd
->pw_uid
, pwd
->pw_gid
);
494 pw_log(cnf
, mode
, W_USER
, "%s(%ld) home %s made",
495 pwd
->pw_name
, (long) pwd
->pw_uid
, pwd
->pw_dir
);
502 pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
)
505 uid_t uid
= (uid_t
) - 1;
506 struct carg
*a_uid
= getarg(args
, 'u');
509 * Check the given uid, if any
512 uid
= (uid_t
) atol(a_uid
->val
);
514 if ((pwd
= getpwuid(uid
)) != NULL
&& getarg(args
, 'o') == NULL
)
515 cmderr(EX_DATAERR
, "uid `%ld' has already been allocated\n", (long) pwd
->pw_uid
);
520 * We need to allocate the next available uid under one of
521 * two policies a) Grab the first unused uid b) Grab the
522 * highest possible unused uid
524 if (cnf
->min_uid
>= cnf
->max_uid
) { /* Sanity
525 * claus^H^H^H^Hheck */
527 cnf
->max_uid
= 32000;
529 bm
= bm_alloc(cnf
->max_uid
- cnf
->min_uid
+ 1);
532 * Now, let's fill the bitmap from the password file
535 while ((pwd
= getpwent()) != NULL
)
536 if (pwd
->pw_uid
>= (int) cnf
->min_uid
&& pwd
->pw_uid
<= (int) cnf
->max_uid
)
537 bm_setbit(&bm
, pwd
->pw_uid
- cnf
->min_uid
);
541 * Then apply the policy, with fallback to reuse if necessary
543 if (cnf
->reuse_uids
|| (uid
= (uid_t
) (bm_lastset(&bm
) + cnf
->min_uid
+ 1)) > cnf
->max_uid
)
544 uid
= (uid_t
) (bm_firstunset(&bm
) + cnf
->min_uid
);
547 * Another sanity check
549 if (uid
< cnf
->min_uid
|| uid
> cnf
->max_uid
)
550 cmderr(EX_SOFTWARE
, "unable to allocate a new uid - range fully used\n");
558 pw_gidpolicy(struct userconf
* cnf
, struct cargs
* args
, char *nam
, gid_t prefer
)
561 gid_t gid
= (uid_t
) - 1;
562 struct carg
*a_gid
= getarg(args
, 'g');
565 * If no arg given, see if default can help out
567 if (a_gid
== NULL
&& cnf
->default_group
&& *cnf
->default_group
)
568 a_gid
= addarg(args
, 'g', cnf
->default_group
);
571 * Check the given gid, if any
575 if ((grp
= getgrnam(a_gid
->val
)) == NULL
) {
576 gid
= (gid_t
) atol(a_gid
->val
);
577 if ((gid
== 0 && !isdigit(*a_gid
->val
)) || (grp
= getgrgid(gid
)) == NULL
)
578 cmderr(EX_NOUSER
, "group `%s' is not defined\n", a_gid
->val
);
581 } else if ((grp
= getgrnam(nam
)) != NULL
&& grp
->gr_mem
[0] == NULL
) {
582 gid
= grp
->gr_gid
; /* Already created? Use it anyway... */
584 struct cargs grpargs
;
588 addarg(&grpargs
, 'n', nam
);
591 * We need to auto-create a group with the user's name. We
592 * can send all the appropriate output to our sister routine
593 * bit first see if we can create a group with gid==uid so we
594 * can keep the user and group ids in sync. We purposely do
595 * NOT check the gid range if we can force the sync. If the
596 * user's name dups an existing group, then the group add
597 * function will happily handle that case for us and exit.
599 if (getgrgid(prefer
) == NULL
) {
600 sprintf(tmp
, "%lu", (unsigned long) prefer
);
601 addarg(&grpargs
, 'g', tmp
);
603 if (getarg(args
, 'N'))
605 addarg(&grpargs
, 'N', NULL
);
606 addarg(&grpargs
, 'q', NULL
);
607 gid
= pw_group(cnf
, M_NEXT
, &grpargs
);
611 pw_group(cnf
, M_ADD
, &grpargs
);
612 if ((grp
= getgrnam(nam
)) != NULL
)
615 a_gid
= grpargs
.lh_first
;
616 while (a_gid
!= NULL
) {
617 struct carg
*t
= a_gid
->list
.le_next
;
618 LIST_REMOVE(a_gid
, list
);
628 pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
)
631 time_t now
= time(NULL
);
632 struct carg
*arg
= getarg(args
, 'e');
635 if ((result
= parse_date(now
, arg
->val
)) == now
)
636 cmderr(EX_DATAERR
, "invalid date/time `%s'\n", arg
->val
);
637 } else if (cnf
->password_days
> 0)
638 result
= now
+ ((long) cnf
->password_days
* 86400L);
644 pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
)
647 time_t now
= time(NULL
);
648 struct carg
*arg
= getarg(args
, 'e');
651 if ((result
= parse_date(now
, arg
->val
)) == now
)
652 cmderr(EX_DATAERR
, "invalid date/time `%s'\n", arg
->val
);
653 } else if (cnf
->expire_days
> 0)
654 result
= now
+ ((long) cnf
->expire_days
* 86400L);
660 pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
662 struct carg
*arg
= getarg(args
, 'd');
667 static char home
[128];
669 if (cnf
->home
== NULL
|| *cnf
->home
== '\0')
670 cmderr(EX_CONFIG
, "no base home directory set\n");
671 sprintf(home
, "%s/%s", cnf
->home
, user
);
677 shell_path(char const * path
, char *shells
[], char *sh
)
679 if (sh
!= NULL
&& (*sh
== '/' || *sh
== '\0'))
680 return sh
; /* specified full path or forced none */
683 char paths
[_UC_MAXLINE
];
686 * We need to search paths
688 strncpy(paths
, path
, sizeof paths
);
689 paths
[sizeof paths
- 1] = '\0';
690 for (p
= strtok(paths
, ": \t\r\n"); p
!= NULL
; p
= strtok(NULL
, ": \t\r\n")) {
692 static char shellpath
[256];
695 sprintf(shellpath
, "%s/%s", p
, sh
);
696 if (access(shellpath
, X_OK
) == 0)
699 for (i
= 0; i
< _UC_MAXSHELLS
&& shells
[i
] != NULL
; i
++) {
700 sprintf(shellpath
, "%s/%s", p
, shells
[i
]);
701 if (access(shellpath
, X_OK
) == 0)
706 cmderr(EX_OSFILE
, "can't find shell `%s' in shell paths\n", sh
);
707 cmderr(EX_CONFIG
, "no default shell available or defined\n");
714 pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
)
717 struct carg
*arg
= getarg(args
, 's');
719 if (newshell
== NULL
&& arg
!= NULL
)
721 return shell_path(cnf
->shelldir
, cnf
->shells
, sh
? sh
: cnf
->shell_default
);
724 static char const chars
[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
727 pw_pwcrypt(char *password
)
732 static char buf
[256];
735 * Calculate a salt value
737 srandom((unsigned) (time(NULL
) ^ getpid()));
738 for (i
= 0; i
< 8; i
++)
739 salt
[i
] = chars
[random() % 63];
742 return strcpy(buf
, crypt(password
, salt
));
745 #if defined(__FreeBSD__)
747 #if defined(USE_MD5RAND)
749 pw_getrand(u_char
*buf
, int len
) /* cryptographically secure rng */
752 for (i
=0;i
<len
;i
+=16) {
756 struct timeval tv
, tvo
;
763 MD5Update (&md5_ctx
, (u_char
*)&t
, sizeof t
);
765 MD5Update (&md5_ctx
, (u_char
*)&t
, sizeof t
);
766 gettimeofday (&tvo
, NULL
);
768 getrusage (RUSAGE_SELF
, &ru
);
769 MD5Update (&md5_ctx
, (u_char
*)&ru
, sizeof ru
);
770 gettimeofday (&tv
, NULL
);
771 MD5Update (&md5_ctx
, (u_char
*)&tv
, sizeof tv
);
772 } while (n
++<20 || tv
.tv_usec
-tvo
.tv_usec
<100*1000);
773 MD5Final (ubuf
, &md5_ctx
);
774 memcpy(buf
+i
, ubuf
, MIN(16, len
-n
));
779 #else /* Use random device (preferred) */
782 pw_getrand(u_char
*buf
, int len
)
785 fd
= open("/dev/urandom", O_RDONLY
);
787 cmderr(EX_OSFILE
, "can't open /dev/urandom: %s\n", strerror(errno
));
788 else if (read(fd
, buf
, len
)!=len
)
789 cmderr(EX_IOERR
, "read error on /dev/urandom\n");
796 #else /* Portable version */
799 pw_getrand(u_char
*buf
, int len
)
803 for (i
= 0; i
< len
; i
++) {
804 unsigned val
= random();
805 /* Use all bits in the random value */
806 buf
[i
]=(u_char
)((val
>> 24) ^ (val
>> 16) ^ (val
>> 8) ^ val
);
814 pw_password(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
818 u_char rndbuf
[sizeof pwbuf
];
820 switch (cnf
->default_password
) {
821 case -1: /* Random password */
822 srandom((unsigned) (time(NULL
) ^ getpid()));
823 l
= (random() % 8 + 8); /* 8 - 16 chars */
824 pw_getrand(rndbuf
, l
);
825 for (i
= 0; i
< l
; i
++)
826 pwbuf
[i
] = chars
[rndbuf
[i
] % sizeof(chars
)];
830 * We give this information back to the user
832 if (getarg(args
, 'h') == NULL
&& getarg(args
, 'N') == NULL
) {
834 printf("Password is: ");
835 printf("%s\n", pwbuf
);
840 case -2: /* No password at all! */
843 case 0: /* No login - default */
847 case 1: /* user's name */
848 strncpy(pwbuf
, user
, sizeof pwbuf
);
849 pwbuf
[sizeof pwbuf
- 1] = '\0';
852 return pw_pwcrypt(pwbuf
);
857 print_user(struct passwd
* pwd
, int pretty
)
860 char buf
[_UC_MAXLINE
];
867 struct group
*grp
= getgrgid(pwd
->pw_gid
);
868 char uname
[60] = "User &", office
[60] = "[None]",
869 wphone
[60] = "[None]", hphone
[60] = "[None]";
870 char acexpire
[32] = "[None]", pwexpire
[32] = "[None]";
873 if ((p
= strtok(pwd
->pw_gecos
, ",")) != NULL
) {
874 strncpy(uname
, p
, sizeof uname
);
875 uname
[sizeof uname
- 1] = '\0';
876 if ((p
= strtok(NULL
, ",")) != NULL
) {
877 strncpy(office
, p
, sizeof office
);
878 office
[sizeof office
- 1] = '\0';
879 if ((p
= strtok(NULL
, ",")) != NULL
) {
880 strncpy(wphone
, p
, sizeof wphone
);
881 wphone
[sizeof wphone
- 1] = '\0';
882 if ((p
= strtok(NULL
, "")) != NULL
) {
883 strncpy(hphone
, p
, sizeof hphone
);
884 hphone
[sizeof hphone
- 1] = '\0';
890 * Handle '&' in gecos field
892 if ((p
= strchr(uname
, '&')) != NULL
) {
893 int l
= strlen(pwd
->pw_name
);
896 memmove(p
+ l
, p
+ 1, m
);
897 memmove(p
, pwd
->pw_name
, l
);
898 *p
= (char) toupper(*p
);
900 if (pwd
->pw_expire
> (time_t)0 && (tptr
= localtime(&pwd
->pw_expire
)) != NULL
)
901 strftime(acexpire
, sizeof acexpire
, "%c", tptr
);
902 if (pwd
->pw_change
> (time_t)9 && (tptr
= localtime(&pwd
->pw_change
)) != NULL
)
903 strftime(pwexpire
, sizeof pwexpire
, "%c", tptr
);
904 printf("Login Name : %-10s #%-22ld Group : %-10s #%ld\n"
906 " Home : %-32.32s Class : %s\n"
907 " Shell : %-32.32s Office : %s\n"
908 "Work Phone : %-32.32s Home Phone : %s\n"
909 "Acc Expire : %-32.32s Pwd Expire : %s\n",
910 pwd
->pw_name
, (long) pwd
->pw_uid
,
911 grp
? grp
->gr_name
: "(invalid)", (long) pwd
->pw_gid
,
912 uname
, pwd
->pw_dir
, pwd
->pw_class
,
913 pwd
->pw_shell
, office
, wphone
, hphone
,
917 while ((grp
=getgrent()) != NULL
)
920 while (i
< _UC_MAXGROUPS
&& grp
->gr_mem
[i
] != NULL
)
922 if (strcmp(grp
->gr_mem
[i
], pwd
->pw_name
)==0)
924 printf(j
++ == 0 ? " Groups : %s" : ",%s", grp
->gr_name
);
931 printf("%s\n", j
? "\n" : "");
937 pw_checkname(char *name
, int gecos
)
940 char const *notch
= gecos
? ":" : " ,\t:+-&#%$^()!@~*?<>=|\\/\"";
943 if (strchr(notch
, name
[l
]) != NULL
|| name
[l
] < ' ' || name
[l
] > 126)
944 cmderr(EX_DATAERR
, (name
[l
]<' ' || (unsigned char)name
[l
] > 126)
945 ? "invalid character `%c' in field\n"
946 : "invalid character 0x$02x in field\n",
950 if (!gecos
&& l
> MAXLOGNAME
)
951 cmderr(EX_DATAERR
, "name too long `%s'\n", name
);
959 DIR *d
= opendir("/var/at/jobs");
964 while ((e
= readdir(d
)) != NULL
) {
967 if (strncmp(e
->d_name
, ".lock", 5) != 0 &&
968 stat(e
->d_name
, &st
) == 0 &&
969 !S_ISDIR(st
.st_mode
) &&
971 char tmp
[MAXPATHLEN
];
973 sprintf(tmp
, "/usr/bin/atrm %s", e
->d_name
);