]>
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.1.1.3 1996/12/10 23:59:02 joerg Exp $
33 #include <sys/param.h>
40 static int print_user(struct passwd
* pwd
, int pretty
);
41 static uid_t
pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
);
42 static uid_t
pw_gidpolicy(struct userconf
* cnf
, struct cargs
* args
, char *nam
, gid_t prefer
);
43 static time_t pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
);
44 static time_t pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
);
45 static char *pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
);
46 static char *pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
);
47 static char *pw_password(struct userconf
* cnf
, struct cargs
* args
, char const * user
);
48 static char *pw_checkname(char *name
, int gecos
);
49 static char *shell_path(char const * path
, char *shells
[], char *sh
);
50 static void rmat(uid_t uid
);
53 * -C config configuration file
57 * -c comment user name/comment
58 * -d directory home directory
59 * -e date account expiry date
60 * -p date password expiry date
61 * -g grp primary group
62 * -G grp1,grp2 additional groups
63 * -m [ -k dir ] create and set up home
64 * -s shell name of login shell
67 * -l name new login name
68 * -h fd password filehandle
69 * -F force print or add
71 * -D set user defaults
72 * -b dir default home root dir
73 * -e period default expiry period
74 * -p period default password change period
75 * -g group default group
76 * -G grp1,grp2.. default additional groups
77 * -L class default login class
78 * -k dir default home skeleton
79 * -s shell default shell
80 * -w method default password method
84 pw_user(struct userconf
* cnf
, int mode
, struct cargs
* args
)
90 struct passwd
*pwd
= NULL
;
95 static struct passwd fakeuser
=
110 * With M_NEXT, we only need to return the
115 uid_t next
= pw_uidpolicy(cnf
, args
);
116 if (getarg(args
, 'q'))
118 printf("%ld:", (long)next
);
119 pw_group(cnf
, mode
, args
);
124 * We can do all of the common legwork here
127 if ((arg
= getarg(args
, 'b')) != NULL
) {
128 cnf
->home
= arg
->val
;
129 if (stat(cnf
->home
, &st
) == -1 || !S_ISDIR(st
.st_mode
))
130 cmderr(EX_OSFILE
, "root home `%s' is not a directory or does not exist\n", cnf
->home
);
132 if ((arg
= getarg(args
, 'e')) != NULL
)
133 cnf
->expire_days
= atoi(arg
->val
);
135 if ((arg
= getarg(args
, 'p')) != NULL
&& arg
->val
)
136 cnf
->password_days
= atoi(arg
->val
);
138 if ((arg
= getarg(args
, 'g')) != NULL
) {
140 if ((grp
= getgrnam(p
)) == NULL
) {
141 if (!isdigit(*p
) || (grp
= getgrgid((gid_t
) atoi(p
))) == NULL
)
142 cmderr(EX_NOUSER
, "group `%s' does not exist\n", p
);
144 cnf
->default_group
= newstr(grp
->gr_name
);
146 if ((arg
= getarg(args
, 'L')) != NULL
)
147 cnf
->default_class
= pw_checkname(arg
->val
, 0);
149 if ((arg
= getarg(args
, 'G')) != NULL
&& arg
->val
) {
152 for (p
= strtok(arg
->val
, ", \t"); i
< _UC_MAXGROUPS
&& p
!= NULL
; p
= strtok(NULL
, ", \t")) {
153 if ((grp
= getgrnam(p
)) == NULL
) {
154 if (!isdigit(*p
) || (grp
= getgrgid((gid_t
) atoi(p
))) == NULL
)
155 cmderr(EX_NOUSER
, "group `%s' does not exist\n", p
);
157 cnf
->groups
[i
++] = newstr(grp
->gr_name
);
159 while (i
< _UC_MAXGROUPS
)
160 cnf
->groups
[i
++] = NULL
;
162 if ((arg
= getarg(args
, 'k')) != NULL
) {
163 if (stat(cnf
->dotdir
= arg
->val
, &st
) == -1 || S_ISDIR(st
.st_mode
))
164 cmderr(EX_OSFILE
, "skeleton `%s' is not a directory or does not exist\n", cnf
->dotdir
);
166 if ((arg
= getarg(args
, 's')) != NULL
)
167 cnf
->shell_default
= arg
->val
;
169 if (mode
== M_ADD
&& getarg(args
, 'D')) {
170 if (getarg(args
, 'n') != NULL
)
171 cmderr(EX_DATAERR
, "can't combine `-D' with `-n name'\n");
172 if ((arg
= getarg(args
, 'u')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
173 if ((cnf
->min_uid
= (uid_t
) atoi(p
)) == 0)
175 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_uid
= (uid_t
) atoi(p
)) < cnf
->min_uid
)
176 cnf
->max_uid
= 32000;
178 if ((arg
= getarg(args
, 'i')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
179 if ((cnf
->min_gid
= (gid_t
) atoi(p
)) == 0)
181 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_gid
= (gid_t
) atoi(p
)) < cnf
->min_gid
)
182 cnf
->max_gid
= 32000;
184 if ((arg
= getarg(args
, 'w')) != NULL
)
185 cnf
->default_password
= boolean_val(arg
->val
, cnf
->default_password
);
187 arg
= getarg(args
, 'C');
188 if (write_userconfig(arg
? arg
->val
: NULL
))
190 perror("config update");
193 if (mode
== M_PRINT
&& getarg(args
, 'a')) {
194 int pretty
= getarg(args
, 'P') != NULL
;
197 while ((pwd
= getpwent()) != NULL
)
198 print_user(pwd
, pretty
);
202 if ((a_name
= getarg(args
, 'n')) != NULL
)
203 pwd
= getpwnam(pw_checkname(a_name
->val
, 0));
204 a_uid
= getarg(args
, 'u');
208 cmderr(EX_DATAERR
, "user name or id required\n");
211 * Determine whether 'n' switch is name or uid - we don't
212 * really don't really care which we have, but we need to
215 if (mode
!= M_ADD
&& pwd
== NULL
&& isdigit(*a_name
->val
) && atoi(a_name
->val
) > 0) { /* Assume uid */
216 (a_uid
= a_name
)->ch
= 'u';
221 * Update, delete & print require that the user exists
223 if (mode
== M_UPDATE
|| mode
== M_DELETE
|| mode
== M_PRINT
) {
224 if (a_name
== NULL
&& pwd
== NULL
) /* Try harder */
225 pwd
= getpwuid(atoi(a_uid
->val
));
228 if (mode
== M_PRINT
&& getarg(args
, 'F')) {
229 fakeuser
.pw_name
= a_name
? a_name
->val
: "nouser";
230 fakeuser
.pw_uid
= a_uid
? (uid_t
) atol(a_uid
->val
) : -1;
231 return print_user(&fakeuser
, getarg(args
, 'P') != NULL
);
234 cmderr(EX_NOUSER
, "no such uid `%s'\n", a_uid
->val
);
235 cmderr(EX_NOUSER
, "no such user `%s'\n", a_name
->val
);
237 if (a_name
== NULL
) /* May be needed later */
238 a_name
= addarg(args
, 'n', newstr(pwd
->pw_name
));
241 * Handle deletions now
243 if (mode
== M_DELETE
) {
244 char file
[MAXPATHLEN
];
245 char home
[MAXPATHLEN
];
246 uid_t uid
= pwd
->pw_uid
;
248 if (strcmp(pwd
->pw_name
, "root") == 0)
249 cmderr(EX_DATAERR
, "cannot remove user 'root'\n");
254 sprintf(file
, "/var/cron/tabs/%s", pwd
->pw_name
);
255 if (access(file
, F_OK
) == 0) {
256 sprintf(file
, "crontab -u %s -r", pwd
->pw_name
);
260 * Save these for later, since contents of pwd may be
261 * invalidated by deletion
263 sprintf(file
, "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
264 strncpy(home
, pwd
->pw_dir
, sizeof home
);
265 home
[sizeof home
- 1] = '\0';
268 cmderr(EX_IOERR
, "Error updating passwd file: %s\n", strerror(errno
));
269 editgroups(a_name
->val
, NULL
);
271 pw_log(cnf
, mode
, W_USER
, "%s(%ld) account removed", a_name
->val
, (long) uid
);
281 if (getpwuid(uid
) == NULL
)
285 * Remove home directory and contents
287 if (getarg(args
, 'r') != NULL
&& *home
== '/' && getpwuid(uid
) == NULL
) {
288 if (stat(home
, &st
) != -1) {
290 pw_log(cnf
, mode
, W_USER
, "%s(%ld) home '%s' %sremoved",
291 a_name
->val
, (long) uid
, home
,
292 stat(home
, &st
) == -1 ? "" : "not completely ");
296 } else if (mode
== M_PRINT
)
297 return print_user(pwd
, getarg(args
, 'P') != NULL
);
300 * The rest is edit code
302 if ((arg
= getarg(args
, 'l')) != NULL
) {
303 if (strcmp(pwd
->pw_name
, "root") == 0)
304 cmderr(EX_DATAERR
, "can't rename `root' account\n");
305 pwd
->pw_name
= pw_checkname(arg
->val
, 0);
307 if ((arg
= getarg(args
, 'u')) != NULL
&& isdigit(*arg
->val
)) {
308 pwd
->pw_uid
= (uid_t
) atol(arg
->val
);
309 if (pwd
->pw_uid
!= 0 && strcmp(pwd
->pw_name
, "root") == 0)
310 cmderr(EX_DATAERR
, "can't change uid of `root' account\n");
311 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
312 fprintf(stderr
, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd
->pw_name
);
314 if ((arg
= getarg(args
, 'g')) != NULL
&& pwd
->pw_uid
!= 0) /* Already checked this */
315 pwd
->pw_gid
= (gid_t
) getgrnam(cnf
->default_group
)->gr_gid
;
317 if ((arg
= getarg(args
, 'p')) != NULL
) {
318 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0)
321 time_t now
= time(NULL
);
322 time_t expire
= parse_date(now
, arg
->val
);
325 cmderr(EX_DATAERR
, "Invalid password change date `%s'\n", arg
->val
);
326 pwd
->pw_change
= expire
;
329 if ((arg
= getarg(args
, 'e')) != NULL
) {
330 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0)
333 time_t now
= time(NULL
);
334 time_t expire
= parse_date(now
, arg
->val
);
337 cmderr(EX_DATAERR
, "Invalid account expiry date `%s'\n", arg
->val
);
338 pwd
->pw_expire
= expire
;
341 if ((arg
= getarg(args
, 's')) != NULL
)
342 pwd
->pw_shell
= shell_path(cnf
->shelldir
, cnf
->shells
, arg
->val
);
344 if (getarg(args
, 'L'))
345 pwd
->pw_class
= cnf
->default_class
;
347 if ((arg
= getarg(args
, 'w')) != NULL
&& getarg(args
, 'h') == NULL
)
348 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
351 if (a_name
== NULL
) /* Required */
352 cmderr(EX_DATAERR
, "login name required\n");
353 else if ((pwd
= getpwnam(a_name
->val
)) != NULL
) /* Exists */
354 cmderr(EX_DATAERR
, "login name `%s' already exists\n", a_name
->val
);
357 * Now, set up defaults for a new user
360 pwd
->pw_name
= a_name
->val
;
361 pwd
->pw_class
= cnf
->default_class
? cnf
->default_class
: "";
362 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
363 pwd
->pw_uid
= pw_uidpolicy(cnf
, args
);
364 pwd
->pw_gid
= pw_gidpolicy(cnf
, args
, pwd
->pw_name
, (gid_t
) pwd
->pw_uid
);
365 pwd
->pw_change
= pw_pwdpolicy(cnf
, args
);
366 pwd
->pw_expire
= pw_exppolicy(cnf
, args
);
367 pwd
->pw_dir
= pw_homepolicy(cnf
, args
, pwd
->pw_name
);
368 pwd
->pw_shell
= pw_shellpolicy(cnf
, args
, NULL
);
370 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
371 fprintf(stderr
, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd
->pw_name
);
375 * Shared add/edit code
377 if ((arg
= getarg(args
, 'c')) != NULL
)
378 pwd
->pw_gecos
= pw_checkname(arg
->val
, 1);
380 if ((arg
= getarg(args
, 'h')) != NULL
) {
381 if (strcmp(arg
->val
, "-") == 0)
382 pwd
->pw_passwd
= "*"; /* No access */
384 int fd
= atoi(arg
->val
);
386 int istty
= isatty(fd
);
390 if (tcgetattr(fd
, &t
) == -1)
393 struct termios n
= t
;
396 n
.c_lflag
&= ~(ECHO
);
397 tcsetattr(fd
, TCSANOW
, &n
);
398 printf("%sassword for user %s:", (mode
== M_UPDATE
) ? "New p" : "P", pwd
->pw_name
);
402 b
= read(fd
, line
, sizeof(line
) - 1);
403 if (istty
) { /* Restore state */
404 tcsetattr(fd
, TCSANOW
, &t
);
409 perror("-h file descriptor");
413 if ((p
= strpbrk(line
, " \t\r\n")) != NULL
)
416 cmderr(EX_DATAERR
, "empty password read on file descriptor %d\n", fd
);
417 pwd
->pw_passwd
= pw_pwcrypt(line
);
422 * Special case: -N only displays & exits
424 if (getarg(args
, 'N') != NULL
)
425 return print_user(pwd
, getarg(args
, 'P') != NULL
);
427 if ((mode
== M_ADD
&& !addpwent(pwd
)) ||
428 (mode
== M_UPDATE
&& !chgpwent(a_name
->val
, pwd
))) {
429 perror("password update");
433 * Ok, user is created or changed - now edit group file
436 if (mode
== M_ADD
|| getarg(args
, 'G') != NULL
)
437 editgroups(pwd
->pw_name
, cnf
->groups
);
439 /* pwd may have been invalidated */
440 if ((pwd
= getpwnam(a_name
->val
)) == NULL
)
441 cmderr(EX_NOUSER
, "user '%s' disappeared during update\n", a_name
->val
);
443 grp
= getgrgid(pwd
->pw_gid
);
444 pw_log(cnf
, mode
, W_USER
, "%s(%ld):%s(%d):%s:%s:%s",
445 pwd
->pw_name
, (long) pwd
->pw_uid
,
446 grp
? grp
->gr_name
: "unknown", (long) (grp
? grp
->gr_gid
: -1),
447 pwd
->pw_gecos
, pwd
->pw_dir
, pwd
->pw_shell
);
450 * If adding, let's touch and chown the user's mail file. This is not
451 * strictly necessary under BSD with a 0755 maildir but it also
452 * doesn't hurt anything to create the empty mailfile
457 sprintf(line
, "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
458 close(open(line
, O_RDWR
| O_CREAT
, 0600)); /* Preserve contents &
460 chown(line
, pwd
->pw_uid
, pwd
->pw_gid
);
463 * Send mail to the new user as well, if we are asked to
465 if (cnf
->newmail
&& *cnf
->newmail
&& (fp
= fopen(cnf
->newmail
, "r")) != NULL
) {
466 FILE *pfp
= popen(_PATH_SENDMAIL
" -t", "w");
471 fprintf(pfp
, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd
->pw_name
);
472 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
473 /* Do substitutions? */
477 pw_log(cnf
, mode
, W_USER
, "%s(%ld) new user mail sent",
478 pwd
->pw_name
, (long) pwd
->pw_uid
);
484 * Finally, let's create and populate the user's home directory. Note
485 * that this also `works' for editing users if -m is used, but
486 * existing files will *not* be overwritten.
488 if (getarg(args
, 'm') != NULL
&& pwd
->pw_dir
&& *pwd
->pw_dir
== '/' && pwd
->pw_dir
[1]) {
489 copymkdir(pwd
->pw_dir
, cnf
->dotdir
, 0755, pwd
->pw_uid
, pwd
->pw_gid
);
490 pw_log(cnf
, mode
, W_USER
, "%s(%ld) home %s made",
491 pwd
->pw_name
, (long) pwd
->pw_uid
, pwd
->pw_dir
);
498 pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
)
501 uid_t uid
= (uid_t
) - 1;
502 struct carg
*a_uid
= getarg(args
, 'u');
505 * Check the given uid, if any
508 uid
= (uid_t
) atol(a_uid
->val
);
510 if ((pwd
= getpwuid(uid
)) != NULL
&& getarg(args
, 'o') == NULL
)
511 cmderr(EX_DATAERR
, "uid `%ld' has already been allocated\n", (long) pwd
->pw_uid
);
516 * We need to allocate the next available uid under one of
517 * two policies a) Grab the first unused uid b) Grab the
518 * highest possible unused uid
520 if (cnf
->min_uid
>= cnf
->max_uid
) { /* Sanity
521 * claus^H^H^H^Hheck */
523 cnf
->max_uid
= 32000;
525 bm
= bm_alloc(cnf
->max_uid
- cnf
->min_uid
+ 1);
528 * Now, let's fill the bitmap from the password file
531 while ((pwd
= getpwent()) != NULL
)
532 if (pwd
->pw_uid
>= (int) cnf
->min_uid
&& pwd
->pw_uid
<= (int) cnf
->max_uid
)
533 bm_setbit(&bm
, pwd
->pw_uid
- cnf
->min_uid
);
537 * Then apply the policy, with fallback to reuse if necessary
539 if (cnf
->reuse_uids
|| (uid
= (uid_t
) (bm_lastset(&bm
) + cnf
->min_uid
+ 1)) > cnf
->max_uid
)
540 uid
= (uid_t
) (bm_firstunset(&bm
) + cnf
->min_uid
);
543 * Another sanity check
545 if (uid
< cnf
->min_uid
|| uid
> cnf
->max_uid
)
546 cmderr(EX_SOFTWARE
, "unable to allocate a new uid - range fully used\n");
554 pw_gidpolicy(struct userconf
* cnf
, struct cargs
* args
, char *nam
, gid_t prefer
)
557 gid_t gid
= (uid_t
) - 1;
558 struct carg
*a_gid
= getarg(args
, 'g');
561 * If no arg given, see if default can help out
563 if (a_gid
== NULL
&& cnf
->default_group
&& *cnf
->default_group
)
564 a_gid
= addarg(args
, 'g', cnf
->default_group
);
567 * Check the given gid, if any
571 if ((grp
= getgrnam(a_gid
->val
)) == NULL
) {
572 gid
= (gid_t
) atol(a_gid
->val
);
573 if ((gid
== 0 && !isdigit(*a_gid
->val
)) || (grp
= getgrgid(gid
)) == NULL
)
574 cmderr(EX_NOUSER
, "group `%s' is not defined\n", a_gid
->val
);
577 } else if ((grp
= getgrnam(nam
)) != NULL
&& grp
->gr_mem
[0] == NULL
) {
578 gid
= grp
->gr_gid
; /* Already created? Use it anyway... */
580 struct cargs grpargs
;
584 addarg(&grpargs
, 'n', nam
);
587 * We need to auto-create a group with the user's name. We
588 * can send all the appropriate output to our sister routine
589 * bit first see if we can create a group with gid==uid so we
590 * can keep the user and group ids in sync. We purposely do
591 * NOT check the gid range if we can force the sync. If the
592 * user's name dups an existing group, then the group add
593 * function will happily handle that case for us and exit.
595 if (getgrgid(prefer
) == NULL
) {
596 sprintf(tmp
, "%lu", (unsigned long) prefer
);
597 addarg(&grpargs
, 'g', tmp
);
599 if (getarg(args
, 'N'))
601 addarg(&grpargs
, 'N', NULL
);
602 addarg(&grpargs
, 'q', NULL
);
603 gid
= pw_group(cnf
, M_NEXT
, &grpargs
);
607 pw_group(cnf
, M_ADD
, &grpargs
);
608 if ((grp
= getgrnam(nam
)) != NULL
)
611 a_gid
= grpargs
.lh_first
;
612 while (a_gid
!= NULL
) {
613 struct carg
*t
= a_gid
->list
.le_next
;
614 LIST_REMOVE(a_gid
, list
);
624 pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
)
627 time_t now
= time(NULL
);
628 struct carg
*arg
= getarg(args
, 'e');
631 if ((result
= parse_date(now
, arg
->val
)) == now
)
632 cmderr(EX_DATAERR
, "invalid date/time `%s'\n", arg
->val
);
633 } else if (cnf
->password_days
> 0)
634 result
= now
+ ((long) cnf
->password_days
* 86400L);
640 pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
)
643 time_t now
= time(NULL
);
644 struct carg
*arg
= getarg(args
, 'e');
647 if ((result
= parse_date(now
, arg
->val
)) == now
)
648 cmderr(EX_DATAERR
, "invalid date/time `%s'\n", arg
->val
);
649 } else if (cnf
->expire_days
> 0)
650 result
= now
+ ((long) cnf
->expire_days
* 86400L);
656 pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
658 struct carg
*arg
= getarg(args
, 'd');
663 static char home
[128];
665 if (cnf
->home
== NULL
|| *cnf
->home
== '\0')
666 cmderr(EX_CONFIG
, "no base home directory set\n");
667 sprintf(home
, "%s/%s", cnf
->home
, user
);
673 shell_path(char const * path
, char *shells
[], char *sh
)
675 if (sh
!= NULL
&& (*sh
== '/' || *sh
== '\0'))
676 return sh
; /* specified full path or forced none */
679 char paths
[_UC_MAXLINE
];
682 * We need to search paths
684 strncpy(paths
, path
, sizeof paths
);
685 paths
[sizeof paths
- 1] = '\0';
686 for (p
= strtok(paths
, ": \t\r\n"); p
!= NULL
; p
= strtok(NULL
, ": \t\r\n")) {
688 static char shellpath
[256];
691 sprintf(shellpath
, "%s/%s", p
, sh
);
692 if (access(shellpath
, X_OK
) == 0)
695 for (i
= 0; i
< _UC_MAXSHELLS
&& shells
[i
] != NULL
; i
++) {
696 sprintf(shellpath
, "%s/%s", p
, shells
[i
]);
697 if (access(shellpath
, X_OK
) == 0)
702 cmderr(EX_OSFILE
, "can't find shell `%s' in shell paths\n", sh
);
703 cmderr(EX_CONFIG
, "no default shell available or defined\n");
710 pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
)
713 struct carg
*arg
= getarg(args
, 's');
715 if (newshell
== NULL
&& arg
!= NULL
)
717 return shell_path(cnf
->shelldir
, cnf
->shells
, sh
? sh
: cnf
->shell_default
);
720 static char const chars
[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
723 pw_pwcrypt(char *password
)
728 static char buf
[256];
731 * Calculate a salt value
733 srandom((unsigned) (time(NULL
) | getpid()));
734 for (i
= 0; i
< 8; i
++)
735 salt
[i
] = chars
[random() % 63];
738 return strcpy(buf
, crypt(password
, salt
));
743 pw_password(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
748 switch (cnf
->default_password
) {
749 case -1: /* Random password */
750 srandom((unsigned) (time(NULL
) | getpid()));
751 l
= (random() % 8 + 8); /* 8 - 16 chars */
752 for (i
= 0; i
< l
; i
++)
753 pwbuf
[i
] = chars
[random() % sizeof(chars
)];
757 * We give this information back to the user
759 if (getarg(args
, 'h') == NULL
&& getarg(args
, 'N') == NULL
) {
761 printf("Password is: ");
762 printf("%s\n", pwbuf
);
767 case -2: /* No password at all! */
770 case 0: /* No login - default */
774 case 1: /* user's name */
775 strncpy(pwbuf
, user
, sizeof pwbuf
);
776 pwbuf
[sizeof pwbuf
- 1] = '\0';
779 return pw_pwcrypt(pwbuf
);
784 print_user(struct passwd
* pwd
, int pretty
)
787 char buf
[_UC_MAXLINE
];
794 struct group
*grp
= getgrgid(pwd
->pw_gid
);
795 char uname
[60] = "User &", office
[60] = "[None]",
796 wphone
[60] = "[None]", hphone
[60] = "[None]";
798 if ((p
= strtok(pwd
->pw_gecos
, ",")) != NULL
) {
799 strncpy(uname
, p
, sizeof uname
);
800 uname
[sizeof uname
- 1] = '\0';
801 if ((p
= strtok(NULL
, ",")) != NULL
) {
802 strncpy(office
, p
, sizeof office
);
803 office
[sizeof office
- 1] = '\0';
804 if ((p
= strtok(NULL
, ",")) != NULL
) {
805 strncpy(wphone
, p
, sizeof wphone
);
806 wphone
[sizeof wphone
- 1] = '\0';
807 if ((p
= strtok(NULL
, "")) != NULL
) {
808 strncpy(hphone
, p
, sizeof hphone
);
809 hphone
[sizeof hphone
- 1] = '\0';
815 * Handle '&' in gecos field
817 if ((p
= strchr(uname
, '&')) != NULL
) {
818 int l
= strlen(pwd
->pw_name
);
821 memmove(p
+ l
, p
+ 1, m
);
822 memmove(p
, pwd
->pw_name
, l
);
823 *p
= (char) toupper(*p
);
825 printf("Login Name : %-10s #%-22ld Group : %-10s #%ld\n"
827 " Home : %-32.32s Class : %s\n"
828 " Shell : %-32.32s Office : %s\n"
829 "Work Phone : %-32.32s Home Phone : %s\n",
831 pwd
->pw_name
, (long) pwd
->pw_uid
,
832 grp
? grp
->gr_name
: "(invalid)", (long) pwd
->pw_gid
,
833 uname
, pwd
->pw_dir
, pwd
->pw_class
,
834 pwd
->pw_shell
, office
, wphone
, hphone
);
837 while ((grp
=getgrent()) != NULL
)
840 while (i
< _UC_MAXGROUPS
&& grp
->gr_mem
[i
] != NULL
)
842 if (strcmp(grp
->gr_mem
[i
], pwd
->pw_name
)==0)
844 printf(j
++ == 0 ? " Groups : %s" : ",%s", grp
->gr_name
);
851 printf("%s\n", j
? "\n" : "");
857 pw_checkname(char *name
, int gecos
)
860 char const *notch
= gecos
? ":" : " ,\t:+-&#%$^()!@~*?<>=|\\/\"";
863 if (strchr(notch
, name
[l
]) != NULL
|| name
[l
] < ' ' || name
[l
] > 126)
864 cmderr(EX_DATAERR
, (name
[l
]<' ' || (unsigned char)name
[l
] > 126)
865 ? "invalid character `%c' in field\n"
866 : "invalid character 0x$02x in field\n",
870 if (!gecos
&& l
> MAXLOGNAME
)
871 cmderr(EX_DATAERR
, "name too long `%s'\n", name
);
879 DIR *d
= opendir("/var/at/jobs");
884 while ((e
= readdir(d
)) != NULL
) {
887 if (strncmp(e
->d_name
, ".lock", 5) != 0 &&
888 stat(e
->d_name
, &st
) == 0 &&
889 !S_ISDIR(st
.st_mode
) &&
891 char tmp
[MAXPATHLEN
];
893 sprintf(tmp
, "/usr/bin/atrm %s", e
->d_name
);