]>
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.5 1996/12/17 14:15:35 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 *shell_path(char const * path
, char *shells
[], char *sh
);
53 static void rmat(uid_t uid
);
56 * -C config configuration file
60 * -c comment user name/comment
61 * -d directory home directory
62 * -e date account expiry date
63 * -p date password expiry date
64 * -g grp primary group
65 * -G grp1,grp2 additional groups
66 * -m [ -k dir ] create and set up home
67 * -s shell name of login shell
70 * -l name new login name
71 * -h fd password filehandle
72 * -F force print or add
74 * -D set user defaults
75 * -b dir default home root dir
76 * -e period default expiry period
77 * -p period default password change period
78 * -g group default group
79 * -G grp1,grp2.. default additional groups
80 * -L class default login class
81 * -k dir default home skeleton
82 * -s shell default shell
83 * -w method default password method
87 pw_user(struct userconf
* cnf
, int mode
, struct cargs
* args
)
93 struct passwd
*pwd
= NULL
;
98 static struct passwd fakeuser
=
113 * With M_NEXT, we only need to return the
118 uid_t next
= pw_uidpolicy(cnf
, args
);
119 if (getarg(args
, 'q'))
121 printf("%ld:", (long)next
);
122 pw_group(cnf
, mode
, args
);
127 * We can do all of the common legwork here
130 if ((arg
= getarg(args
, 'b')) != NULL
) {
131 cnf
->home
= arg
->val
;
132 if (stat(cnf
->home
, &st
) == -1 || !S_ISDIR(st
.st_mode
))
133 cmderr(EX_OSFILE
, "root home `%s' is not a directory or does not exist\n", cnf
->home
);
135 if ((arg
= getarg(args
, 'e')) != NULL
)
136 cnf
->expire_days
= atoi(arg
->val
);
138 if ((arg
= getarg(args
, 'p')) != NULL
&& arg
->val
)
139 cnf
->password_days
= atoi(arg
->val
);
141 if ((arg
= getarg(args
, 'g')) != NULL
) {
143 if ((grp
= getgrnam(p
)) == NULL
) {
144 if (!isdigit(*p
) || (grp
= getgrgid((gid_t
) atoi(p
))) == NULL
)
145 cmderr(EX_NOUSER
, "group `%s' does not exist\n", p
);
147 cnf
->default_group
= newstr(grp
->gr_name
);
149 if ((arg
= getarg(args
, 'L')) != NULL
)
150 cnf
->default_class
= pw_checkname((u_char
*)arg
->val
, 0);
152 if ((arg
= getarg(args
, 'G')) != NULL
&& arg
->val
) {
155 for (p
= strtok(arg
->val
, ", \t"); i
< _UC_MAXGROUPS
&& p
!= NULL
; p
= strtok(NULL
, ", \t")) {
156 if ((grp
= getgrnam(p
)) == NULL
) {
157 if (!isdigit(*p
) || (grp
= getgrgid((gid_t
) atoi(p
))) == NULL
)
158 cmderr(EX_NOUSER
, "group `%s' does not exist\n", p
);
160 cnf
->groups
[i
++] = newstr(grp
->gr_name
);
162 while (i
< _UC_MAXGROUPS
)
163 cnf
->groups
[i
++] = NULL
;
165 if ((arg
= getarg(args
, 'k')) != NULL
) {
166 if (stat(cnf
->dotdir
= arg
->val
, &st
) == -1 || S_ISDIR(st
.st_mode
))
167 cmderr(EX_OSFILE
, "skeleton `%s' is not a directory or does not exist\n", cnf
->dotdir
);
169 if ((arg
= getarg(args
, 's')) != NULL
)
170 cnf
->shell_default
= arg
->val
;
172 if (mode
== M_ADD
&& getarg(args
, 'D')) {
173 if (getarg(args
, 'n') != NULL
)
174 cmderr(EX_DATAERR
, "can't combine `-D' with `-n name'\n");
175 if ((arg
= getarg(args
, 'u')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
176 if ((cnf
->min_uid
= (uid_t
) atoi(p
)) == 0)
178 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_uid
= (uid_t
) atoi(p
)) < cnf
->min_uid
)
179 cnf
->max_uid
= 32000;
181 if ((arg
= getarg(args
, 'i')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
182 if ((cnf
->min_gid
= (gid_t
) atoi(p
)) == 0)
184 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_gid
= (gid_t
) atoi(p
)) < cnf
->min_gid
)
185 cnf
->max_gid
= 32000;
187 if ((arg
= getarg(args
, 'w')) != NULL
)
188 cnf
->default_password
= boolean_val(arg
->val
, cnf
->default_password
);
190 arg
= getarg(args
, 'C');
191 if (write_userconfig(arg
? arg
->val
: NULL
))
193 perror("config update");
196 if (mode
== M_PRINT
&& getarg(args
, 'a')) {
197 int pretty
= getarg(args
, 'P') != NULL
;
200 while ((pwd
= getpwent()) != NULL
)
201 print_user(pwd
, pretty
);
205 if ((a_name
= getarg(args
, 'n')) != NULL
)
206 pwd
= getpwnam(pw_checkname((u_char
*)a_name
->val
, 0));
207 a_uid
= getarg(args
, 'u');
211 cmderr(EX_DATAERR
, "user name or id required\n");
214 * Determine whether 'n' switch is name or uid - we don't
215 * really don't really care which we have, but we need to
218 if (mode
!= M_ADD
&& pwd
== NULL
&& isdigit(*a_name
->val
) && atoi(a_name
->val
) > 0) { /* Assume uid */
219 (a_uid
= a_name
)->ch
= 'u';
224 * Update, delete & print require that the user exists
226 if (mode
== M_UPDATE
|| mode
== M_DELETE
|| mode
== M_PRINT
) {
227 if (a_name
== NULL
&& pwd
== NULL
) /* Try harder */
228 pwd
= getpwuid(atoi(a_uid
->val
));
231 if (mode
== M_PRINT
&& getarg(args
, 'F')) {
232 fakeuser
.pw_name
= a_name
? a_name
->val
: "nouser";
233 fakeuser
.pw_uid
= a_uid
? (uid_t
) atol(a_uid
->val
) : -1;
234 return print_user(&fakeuser
, getarg(args
, 'P') != NULL
);
237 cmderr(EX_NOUSER
, "no such uid `%s'\n", a_uid
->val
);
238 cmderr(EX_NOUSER
, "no such user `%s'\n", a_name
->val
);
240 if (a_name
== NULL
) /* May be needed later */
241 a_name
= addarg(args
, 'n', newstr(pwd
->pw_name
));
244 * Handle deletions now
246 if (mode
== M_DELETE
) {
247 char file
[MAXPATHLEN
];
248 char home
[MAXPATHLEN
];
249 uid_t uid
= pwd
->pw_uid
;
251 if (strcmp(pwd
->pw_name
, "root") == 0)
252 cmderr(EX_DATAERR
, "cannot remove user 'root'\n");
257 sprintf(file
, "/var/cron/tabs/%s", pwd
->pw_name
);
258 if (access(file
, F_OK
) == 0) {
259 sprintf(file
, "crontab -u %s -r", pwd
->pw_name
);
263 * Save these for later, since contents of pwd may be
264 * invalidated by deletion
266 sprintf(file
, "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
267 strncpy(home
, pwd
->pw_dir
, sizeof home
);
268 home
[sizeof home
- 1] = '\0';
271 cmderr(EX_IOERR
, "Error updating passwd file: %s\n", strerror(errno
));
272 editgroups(a_name
->val
, NULL
);
274 pw_log(cnf
, mode
, W_USER
, "%s(%ld) account removed", a_name
->val
, (long) uid
);
284 if (getpwuid(uid
) == NULL
)
288 * Remove home directory and contents
290 if (getarg(args
, 'r') != NULL
&& *home
== '/' && getpwuid(uid
) == NULL
) {
291 if (stat(home
, &st
) != -1) {
293 pw_log(cnf
, mode
, W_USER
, "%s(%ld) home '%s' %sremoved",
294 a_name
->val
, (long) uid
, home
,
295 stat(home
, &st
) == -1 ? "" : "not completely ");
299 } else if (mode
== M_PRINT
)
300 return print_user(pwd
, getarg(args
, 'P') != NULL
);
303 * The rest is edit code
305 if ((arg
= getarg(args
, 'l')) != NULL
) {
306 if (strcmp(pwd
->pw_name
, "root") == 0)
307 cmderr(EX_DATAERR
, "can't rename `root' account\n");
308 pwd
->pw_name
= pw_checkname((u_char
*)arg
->val
, 0);
310 if ((arg
= getarg(args
, 'u')) != NULL
&& isdigit(*arg
->val
)) {
311 pwd
->pw_uid
= (uid_t
) atol(arg
->val
);
312 if (pwd
->pw_uid
!= 0 && strcmp(pwd
->pw_name
, "root") == 0)
313 cmderr(EX_DATAERR
, "can't change uid of `root' account\n");
314 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
315 fprintf(stderr
, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd
->pw_name
);
317 if ((arg
= getarg(args
, 'g')) != NULL
&& pwd
->pw_uid
!= 0) /* Already checked this */
318 pwd
->pw_gid
= (gid_t
) getgrnam(cnf
->default_group
)->gr_gid
;
320 if ((arg
= getarg(args
, 'p')) != NULL
) {
321 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0)
324 time_t now
= time(NULL
);
325 time_t expire
= parse_date(now
, arg
->val
);
328 cmderr(EX_DATAERR
, "Invalid password change date `%s'\n", arg
->val
);
329 pwd
->pw_change
= expire
;
332 if ((arg
= getarg(args
, 'e')) != NULL
) {
333 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0)
336 time_t now
= time(NULL
);
337 time_t expire
= parse_date(now
, arg
->val
);
340 cmderr(EX_DATAERR
, "Invalid account expiry date `%s'\n", arg
->val
);
341 pwd
->pw_expire
= expire
;
344 if ((arg
= getarg(args
, 's')) != NULL
)
345 pwd
->pw_shell
= shell_path(cnf
->shelldir
, cnf
->shells
, arg
->val
);
347 if (getarg(args
, 'L'))
348 pwd
->pw_class
= cnf
->default_class
;
350 if ((arg
= getarg(args
, 'w')) != NULL
&& getarg(args
, 'h') == NULL
)
351 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
354 if (a_name
== NULL
) /* Required */
355 cmderr(EX_DATAERR
, "login name required\n");
356 else if ((pwd
= getpwnam(a_name
->val
)) != NULL
) /* Exists */
357 cmderr(EX_DATAERR
, "login name `%s' already exists\n", a_name
->val
);
360 * Now, set up defaults for a new user
363 pwd
->pw_name
= a_name
->val
;
364 pwd
->pw_class
= cnf
->default_class
? cnf
->default_class
: "";
365 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
366 pwd
->pw_uid
= pw_uidpolicy(cnf
, args
);
367 pwd
->pw_gid
= pw_gidpolicy(cnf
, args
, pwd
->pw_name
, (gid_t
) pwd
->pw_uid
);
368 pwd
->pw_change
= pw_pwdpolicy(cnf
, args
);
369 pwd
->pw_expire
= pw_exppolicy(cnf
, args
);
370 pwd
->pw_dir
= pw_homepolicy(cnf
, args
, pwd
->pw_name
);
371 pwd
->pw_shell
= pw_shellpolicy(cnf
, args
, NULL
);
373 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
374 fprintf(stderr
, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd
->pw_name
);
378 * Shared add/edit code
380 if ((arg
= getarg(args
, 'c')) != NULL
)
381 pwd
->pw_gecos
= pw_checkname((u_char
*)arg
->val
, 1);
383 if ((arg
= getarg(args
, 'h')) != NULL
) {
384 if (strcmp(arg
->val
, "-") == 0)
385 pwd
->pw_passwd
= "*"; /* No access */
387 int fd
= atoi(arg
->val
);
389 int istty
= isatty(fd
);
393 if (tcgetattr(fd
, &t
) == -1)
396 struct termios n
= t
;
399 n
.c_lflag
&= ~(ECHO
);
400 tcsetattr(fd
, TCSANOW
, &n
);
401 printf("%sassword for user %s:", (mode
== M_UPDATE
) ? "New p" : "P", pwd
->pw_name
);
405 b
= read(fd
, line
, sizeof(line
) - 1);
406 if (istty
) { /* Restore state */
407 tcsetattr(fd
, TCSANOW
, &t
);
412 perror("-h file descriptor");
416 if ((p
= strpbrk(line
, " \t\r\n")) != NULL
)
419 cmderr(EX_DATAERR
, "empty password read on file descriptor %d\n", fd
);
420 pwd
->pw_passwd
= pw_pwcrypt(line
);
425 * Special case: -N only displays & exits
427 if (getarg(args
, 'N') != NULL
)
428 return print_user(pwd
, getarg(args
, 'P') != NULL
);
430 if ((mode
== M_ADD
&& !addpwent(pwd
)) ||
431 (mode
== M_UPDATE
&& !chgpwent(a_name
->val
, pwd
))) {
432 perror("password update");
436 * Ok, user is created or changed - now edit group file
439 if (mode
== M_ADD
|| getarg(args
, 'G') != NULL
)
440 editgroups(pwd
->pw_name
, cnf
->groups
);
442 /* pwd may have been invalidated */
443 if ((pwd
= getpwnam(a_name
->val
)) == NULL
)
444 cmderr(EX_NOUSER
, "user '%s' disappeared during update\n", a_name
->val
);
446 grp
= getgrgid(pwd
->pw_gid
);
447 pw_log(cnf
, mode
, W_USER
, "%s(%ld):%s(%d):%s:%s:%s",
448 pwd
->pw_name
, (long) pwd
->pw_uid
,
449 grp
? grp
->gr_name
: "unknown", (long) (grp
? grp
->gr_gid
: -1),
450 pwd
->pw_gecos
, pwd
->pw_dir
, pwd
->pw_shell
);
453 * If adding, let's touch and chown the user's mail file. This is not
454 * strictly necessary under BSD with a 0755 maildir but it also
455 * doesn't hurt anything to create the empty mailfile
460 sprintf(line
, "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
461 close(open(line
, O_RDWR
| O_CREAT
, 0600)); /* Preserve contents &
463 chown(line
, pwd
->pw_uid
, pwd
->pw_gid
);
466 * Send mail to the new user as well, if we are asked to
468 if (cnf
->newmail
&& *cnf
->newmail
&& (fp
= fopen(cnf
->newmail
, "r")) != NULL
) {
469 FILE *pfp
= popen(_PATH_SENDMAIL
" -t", "w");
474 fprintf(pfp
, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd
->pw_name
);
475 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
476 /* Do substitutions? */
480 pw_log(cnf
, mode
, W_USER
, "%s(%ld) new user mail sent",
481 pwd
->pw_name
, (long) pwd
->pw_uid
);
487 * Finally, let's create and populate the user's home directory. Note
488 * that this also `works' for editing users if -m is used, but
489 * existing files will *not* be overwritten.
491 if (getarg(args
, 'm') != NULL
&& pwd
->pw_dir
&& *pwd
->pw_dir
== '/' && pwd
->pw_dir
[1]) {
492 copymkdir(pwd
->pw_dir
, cnf
->dotdir
, 0755, pwd
->pw_uid
, pwd
->pw_gid
);
493 pw_log(cnf
, mode
, W_USER
, "%s(%ld) home %s made",
494 pwd
->pw_name
, (long) pwd
->pw_uid
, pwd
->pw_dir
);
501 pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
)
504 uid_t uid
= (uid_t
) - 1;
505 struct carg
*a_uid
= getarg(args
, 'u');
508 * Check the given uid, if any
511 uid
= (uid_t
) atol(a_uid
->val
);
513 if ((pwd
= getpwuid(uid
)) != NULL
&& getarg(args
, 'o') == NULL
)
514 cmderr(EX_DATAERR
, "uid `%ld' has already been allocated\n", (long) pwd
->pw_uid
);
519 * We need to allocate the next available uid under one of
520 * two policies a) Grab the first unused uid b) Grab the
521 * highest possible unused uid
523 if (cnf
->min_uid
>= cnf
->max_uid
) { /* Sanity
524 * claus^H^H^H^Hheck */
526 cnf
->max_uid
= 32000;
528 bm
= bm_alloc(cnf
->max_uid
- cnf
->min_uid
+ 1);
531 * Now, let's fill the bitmap from the password file
534 while ((pwd
= getpwent()) != NULL
)
535 if (pwd
->pw_uid
>= (int) cnf
->min_uid
&& pwd
->pw_uid
<= (int) cnf
->max_uid
)
536 bm_setbit(&bm
, pwd
->pw_uid
- cnf
->min_uid
);
540 * Then apply the policy, with fallback to reuse if necessary
542 if (cnf
->reuse_uids
|| (uid
= (uid_t
) (bm_lastset(&bm
) + cnf
->min_uid
+ 1)) > cnf
->max_uid
)
543 uid
= (uid_t
) (bm_firstunset(&bm
) + cnf
->min_uid
);
546 * Another sanity check
548 if (uid
< cnf
->min_uid
|| uid
> cnf
->max_uid
)
549 cmderr(EX_SOFTWARE
, "unable to allocate a new uid - range fully used\n");
557 pw_gidpolicy(struct userconf
* cnf
, struct cargs
* args
, char *nam
, gid_t prefer
)
560 gid_t gid
= (uid_t
) - 1;
561 struct carg
*a_gid
= getarg(args
, 'g');
564 * If no arg given, see if default can help out
566 if (a_gid
== NULL
&& cnf
->default_group
&& *cnf
->default_group
)
567 a_gid
= addarg(args
, 'g', cnf
->default_group
);
570 * Check the given gid, if any
574 if ((grp
= getgrnam(a_gid
->val
)) == NULL
) {
575 gid
= (gid_t
) atol(a_gid
->val
);
576 if ((gid
== 0 && !isdigit(*a_gid
->val
)) || (grp
= getgrgid(gid
)) == NULL
)
577 cmderr(EX_NOUSER
, "group `%s' is not defined\n", a_gid
->val
);
580 } else if ((grp
= getgrnam(nam
)) != NULL
&& grp
->gr_mem
[0] == NULL
) {
581 gid
= grp
->gr_gid
; /* Already created? Use it anyway... */
583 struct cargs grpargs
;
587 addarg(&grpargs
, 'n', nam
);
590 * We need to auto-create a group with the user's name. We
591 * can send all the appropriate output to our sister routine
592 * bit first see if we can create a group with gid==uid so we
593 * can keep the user and group ids in sync. We purposely do
594 * NOT check the gid range if we can force the sync. If the
595 * user's name dups an existing group, then the group add
596 * function will happily handle that case for us and exit.
598 if (getgrgid(prefer
) == NULL
) {
599 sprintf(tmp
, "%lu", (unsigned long) prefer
);
600 addarg(&grpargs
, 'g', tmp
);
602 if (getarg(args
, 'N'))
604 addarg(&grpargs
, 'N', NULL
);
605 addarg(&grpargs
, 'q', NULL
);
606 gid
= pw_group(cnf
, M_NEXT
, &grpargs
);
610 pw_group(cnf
, M_ADD
, &grpargs
);
611 if ((grp
= getgrnam(nam
)) != NULL
)
614 a_gid
= grpargs
.lh_first
;
615 while (a_gid
!= NULL
) {
616 struct carg
*t
= a_gid
->list
.le_next
;
617 LIST_REMOVE(a_gid
, list
);
627 pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
)
630 time_t now
= time(NULL
);
631 struct carg
*arg
= getarg(args
, 'e');
634 if ((result
= parse_date(now
, arg
->val
)) == now
)
635 cmderr(EX_DATAERR
, "invalid date/time `%s'\n", arg
->val
);
636 } else if (cnf
->password_days
> 0)
637 result
= now
+ ((long) cnf
->password_days
* 86400L);
643 pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
)
646 time_t now
= time(NULL
);
647 struct carg
*arg
= getarg(args
, 'e');
650 if ((result
= parse_date(now
, arg
->val
)) == now
)
651 cmderr(EX_DATAERR
, "invalid date/time `%s'\n", arg
->val
);
652 } else if (cnf
->expire_days
> 0)
653 result
= now
+ ((long) cnf
->expire_days
* 86400L);
659 pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
661 struct carg
*arg
= getarg(args
, 'd');
666 static char home
[128];
668 if (cnf
->home
== NULL
|| *cnf
->home
== '\0')
669 cmderr(EX_CONFIG
, "no base home directory set\n");
670 sprintf(home
, "%s/%s", cnf
->home
, user
);
676 shell_path(char const * path
, char *shells
[], char *sh
)
678 if (sh
!= NULL
&& (*sh
== '/' || *sh
== '\0'))
679 return sh
; /* specified full path or forced none */
682 char paths
[_UC_MAXLINE
];
685 * We need to search paths
687 strncpy(paths
, path
, sizeof paths
);
688 paths
[sizeof paths
- 1] = '\0';
689 for (p
= strtok(paths
, ": \t\r\n"); p
!= NULL
; p
= strtok(NULL
, ": \t\r\n")) {
691 static char shellpath
[256];
694 sprintf(shellpath
, "%s/%s", p
, sh
);
695 if (access(shellpath
, X_OK
) == 0)
698 for (i
= 0; i
< _UC_MAXSHELLS
&& shells
[i
] != NULL
; i
++) {
699 sprintf(shellpath
, "%s/%s", p
, shells
[i
]);
700 if (access(shellpath
, X_OK
) == 0)
705 cmderr(EX_OSFILE
, "can't find shell `%s' in shell paths\n", sh
);
706 cmderr(EX_CONFIG
, "no default shell available or defined\n");
713 pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
)
716 struct carg
*arg
= getarg(args
, 's');
718 if (newshell
== NULL
&& arg
!= NULL
)
720 return shell_path(cnf
->shelldir
, cnf
->shells
, sh
? sh
: cnf
->shell_default
);
723 static char const chars
[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
726 pw_pwcrypt(char *password
)
731 static char buf
[256];
734 * Calculate a salt value
736 srandom((unsigned) (time(NULL
) ^ getpid()));
737 for (i
= 0; i
< 8; i
++)
738 salt
[i
] = chars
[random() % 63];
741 return strcpy(buf
, crypt(password
, salt
));
744 #if defined(__FreeBSD__)
746 #if defined(USE_MD5RAND)
748 pw_getrand(u_char
*buf
, int len
) /* cryptographically secure rng */
751 for (i
=0;i
<len
;i
+=16) {
755 struct timeval tv
, tvo
;
762 MD5Update (&md5_ctx
, (u_char
*)&t
, sizeof t
);
764 MD5Update (&md5_ctx
, (u_char
*)&t
, sizeof t
);
765 gettimeofday (&tvo
, NULL
);
767 getrusage (RUSAGE_SELF
, &ru
);
768 MD5Update (&md5_ctx
, (u_char
*)&ru
, sizeof ru
);
769 gettimeofday (&tv
, NULL
);
770 MD5Update (&md5_ctx
, (u_char
*)&tv
, sizeof tv
);
771 } while (n
++<20 || tv
.tv_usec
-tvo
.tv_usec
<100*1000);
772 MD5Final (ubuf
, &md5_ctx
);
773 memcpy(buf
+i
, ubuf
, MIN(16, len
-n
));
778 #else /* Use random device (preferred) */
781 pw_getrand(u_char
*buf
, int len
)
784 fd
= open("/dev/urandom", O_RDONLY
);
786 cmderr(EX_OSFILE
, "can't open /dev/urandom: %s\n", strerror(errno
));
787 else if (read(fd
, buf
, len
)!=len
)
788 cmderr(EX_IOERR
, "read error on /dev/urandom\n");
795 #else /* Portable version */
798 pw_getrand(u_char
*buf
, int len
)
802 for (i
= 0; i
< len
; i
++) {
803 unsigned val
= random();
804 /* Use all bits in the random value */
805 buf
[i
]=(u_char
)((val
>> 24) ^ (val
>> 16) ^ (val
>> 8) ^ val
);
813 pw_password(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
817 u_char rndbuf
[sizeof pwbuf
];
819 switch (cnf
->default_password
) {
820 case -1: /* Random password */
821 srandom((unsigned) (time(NULL
) ^ getpid()));
822 l
= (random() % 8 + 8); /* 8 - 16 chars */
823 pw_getrand(rndbuf
, l
);
824 for (i
= 0; i
< l
; i
++)
825 pwbuf
[i
] = chars
[rndbuf
[i
] % sizeof(chars
)];
829 * We give this information back to the user
831 if (getarg(args
, 'h') == NULL
&& getarg(args
, 'N') == NULL
) {
833 printf("Password is: ");
834 printf("%s\n", pwbuf
);
839 case -2: /* No password at all! */
842 case 0: /* No login - default */
846 case 1: /* user's name */
847 strncpy(pwbuf
, user
, sizeof pwbuf
);
848 pwbuf
[sizeof pwbuf
- 1] = '\0';
851 return pw_pwcrypt(pwbuf
);
856 print_user(struct passwd
* pwd
, int pretty
)
859 char buf
[_UC_MAXLINE
];
866 struct group
*grp
= getgrgid(pwd
->pw_gid
);
867 char uname
[60] = "User &", office
[60] = "[None]",
868 wphone
[60] = "[None]", hphone
[60] = "[None]";
869 char acexpire
[32] = "[None]", pwexpire
[32] = "[None]";
872 if ((p
= strtok(pwd
->pw_gecos
, ",")) != NULL
) {
873 strncpy(uname
, p
, sizeof uname
);
874 uname
[sizeof uname
- 1] = '\0';
875 if ((p
= strtok(NULL
, ",")) != NULL
) {
876 strncpy(office
, p
, sizeof office
);
877 office
[sizeof office
- 1] = '\0';
878 if ((p
= strtok(NULL
, ",")) != NULL
) {
879 strncpy(wphone
, p
, sizeof wphone
);
880 wphone
[sizeof wphone
- 1] = '\0';
881 if ((p
= strtok(NULL
, "")) != NULL
) {
882 strncpy(hphone
, p
, sizeof hphone
);
883 hphone
[sizeof hphone
- 1] = '\0';
889 * Handle '&' in gecos field
891 if ((p
= strchr(uname
, '&')) != NULL
) {
892 int l
= strlen(pwd
->pw_name
);
895 memmove(p
+ l
, p
+ 1, m
);
896 memmove(p
, pwd
->pw_name
, l
);
897 *p
= (char) toupper(*p
);
899 if (pwd
->pw_expire
> (time_t)0 && (tptr
= localtime(&pwd
->pw_expire
)) != NULL
)
900 strftime(acexpire
, sizeof acexpire
, "%c", tptr
);
901 if (pwd
->pw_change
> (time_t)9 && (tptr
= localtime(&pwd
->pw_change
)) != NULL
)
902 strftime(pwexpire
, sizeof pwexpire
, "%c", tptr
);
903 printf("Login Name : %-10s #%-22ld Group : %-10s #%ld\n"
905 " Home : %-32.32s Class : %s\n"
906 " Shell : %-32.32s Office : %s\n"
907 "Work Phone : %-32.32s Home Phone : %s\n"
908 "Acc Expire : %-32.32s Pwd Expire : %s\n",
909 pwd
->pw_name
, (long) pwd
->pw_uid
,
910 grp
? grp
->gr_name
: "(invalid)", (long) pwd
->pw_gid
,
911 uname
, pwd
->pw_dir
, pwd
->pw_class
,
912 pwd
->pw_shell
, office
, wphone
, hphone
,
916 while ((grp
=getgrent()) != NULL
)
919 while (i
< _UC_MAXGROUPS
&& grp
->gr_mem
[i
] != NULL
)
921 if (strcmp(grp
->gr_mem
[i
], pwd
->pw_name
)==0)
923 printf(j
++ == 0 ? " Groups : %s" : ",%s", grp
->gr_name
);
930 printf("%s\n", j
? "\n" : "");
936 pw_checkname(u_char
*name
, int gecos
)
939 char const *notch
= gecos
? ":" : " ,\t:+-&#%$^()!@~*?<>=|\\/\"";
942 if (strchr(notch
, name
[l
]) != NULL
|| name
[l
] < ' ' ||
943 name
[l
] == 127 || (!gecos
&& name
[l
] & 0x80)) /* 8-bit */
944 cmderr(EX_DATAERR
, (name
[l
]<' ' || 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
);