]>
git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw_user.c
2 * Copyright (c) 1996 by David L. Nugent <davidn@blaze.net.au>.
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 as
10 * the first lines of this file unmodified.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by David L. Nugent.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE DAVID L. NUGENT ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/param.h>
46 static int print_user(struct passwd
* pwd
, int pretty
);
47 static uid_t
pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
);
48 static uid_t
pw_gidpolicy(struct userconf
* cnf
, struct cargs
* args
, char *nam
, gid_t prefer
);
49 static time_t pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
);
50 static time_t pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
);
51 static char *pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
);
52 static char *pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
);
53 static char *pw_password(struct userconf
* cnf
, struct cargs
* args
, char const * user
);
54 static char *pw_checkname(char *name
, int gecos
);
55 static char *shell_path(char const * path
, char *shells
[], char *sh
);
56 static void rmat(uid_t uid
);
59 * -C config configuration file
63 * -c comment user name/comment
64 * -d directory home directory
65 * -e date account expiry date
66 * -p date password expiry date
67 * -g grp primary group
68 * -G grp1,grp2 additional groups
69 * -m [ -k dir ] create and set up home
70 * -s shell name of login shell
73 * -l name new login name
74 * -h fd password filehandle
75 * -F force print or add
77 * -D set user defaults
78 * -b dir default home root dir
79 * -e period default expiry period
80 * -p period default password change period
81 * -g group default group
82 * -G grp1,grp2.. default additional groups
83 * -L class default login class
84 * -k dir default home skeleton
85 * -s shell default shell
86 * -w method default password method
90 pw_user(struct userconf
* cnf
, int mode
, struct cargs
* args
)
96 struct passwd
*pwd
= NULL
;
101 static struct passwd fakeuser
=
116 * With M_NEXT, we only need to return the
121 uid_t next
= pw_uidpolicy(cnf
, args
);
122 if (getarg(args
, 'q'))
124 printf("%ld:", (long)next
);
125 pw_group(cnf
, mode
, args
);
130 * We can do all of the common legwork here
133 if ((arg
= getarg(args
, 'b')) != NULL
) {
134 cnf
->home
= arg
->val
;
135 if (stat(cnf
->home
, &st
) == -1 || !S_ISDIR(st
.st_mode
))
136 cmderr(EX_OSFILE
, "root home `%s' is not a directory or does not exist\n", cnf
->home
);
138 if ((arg
= getarg(args
, 'e')) != NULL
)
139 cnf
->expire_days
= atoi(arg
->val
);
141 if ((arg
= getarg(args
, 'p')) != NULL
&& arg
->val
)
142 cnf
->password_days
= atoi(arg
->val
);
144 if ((arg
= getarg(args
, 'g')) != NULL
) {
146 if ((grp
= getgrnam(p
)) == NULL
) {
147 if (!isdigit(*p
) || (grp
= getgrgid((gid_t
) atoi(p
))) == NULL
)
148 cmderr(EX_NOUSER
, "group `%s' does not exist\n", p
);
150 cnf
->default_group
= newstr(grp
->gr_name
);
152 if ((arg
= getarg(args
, 'L')) != NULL
)
153 cnf
->default_class
= pw_checkname(arg
->val
, 0);
155 if ((arg
= getarg(args
, 'G')) != NULL
&& arg
->val
) {
158 for (p
= strtok(arg
->val
, ", \t"); i
< _UC_MAXGROUPS
&& p
!= NULL
; p
= strtok(NULL
, ", \t")) {
159 if ((grp
= getgrnam(p
)) == NULL
) {
160 if (!isdigit(*p
) || (grp
= getgrgid((gid_t
) atoi(p
))) == NULL
)
161 cmderr(EX_NOUSER
, "group `%s' does not exist\n", p
);
163 cnf
->groups
[i
++] = newstr(grp
->gr_name
);
165 while (i
< _UC_MAXGROUPS
)
166 cnf
->groups
[i
++] = NULL
;
168 if ((arg
= getarg(args
, 'k')) != NULL
) {
169 if (stat(cnf
->dotdir
= arg
->val
, &st
) == -1 || S_ISDIR(st
.st_mode
))
170 cmderr(EX_OSFILE
, "skeleton `%s' is not a directory or does not exist\n", cnf
->dotdir
);
172 if ((arg
= getarg(args
, 's')) != NULL
)
173 cnf
->shell_default
= arg
->val
;
175 if (mode
== M_ADD
&& getarg(args
, 'D')) {
176 if (getarg(args
, 'n') != NULL
)
177 cmderr(EX_DATAERR
, "can't combine `-D' with `-n name'\n");
178 if ((arg
= getarg(args
, 'u')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
179 if ((cnf
->min_uid
= (uid_t
) atoi(p
)) == 0)
181 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_uid
= (uid_t
) atoi(p
)) < cnf
->min_uid
)
182 cnf
->max_uid
= 32000;
184 if ((arg
= getarg(args
, 'i')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
185 if ((cnf
->min_gid
= (gid_t
) atoi(p
)) == 0)
187 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_gid
= (gid_t
) atoi(p
)) < cnf
->min_gid
)
188 cnf
->max_gid
= 32000;
190 if ((arg
= getarg(args
, 'w')) != NULL
)
191 cnf
->default_password
= boolean_val(arg
->val
, cnf
->default_password
);
193 arg
= getarg(args
, 'C');
194 if (write_userconfig(arg
? arg
->val
: NULL
))
196 perror("config update");
199 if (mode
== M_PRINT
&& getarg(args
, 'a')) {
200 int pretty
= getarg(args
, 'P') != NULL
;
203 while ((pwd
= getpwent()) != NULL
)
204 print_user(pwd
, pretty
);
208 if ((a_name
= getarg(args
, 'n')) != NULL
)
209 pwd
= getpwnam(pw_checkname(a_name
->val
, 0));
210 a_uid
= getarg(args
, 'u');
214 cmderr(EX_DATAERR
, "user name or id required\n");
217 * Determine whether 'n' switch is name or uid - we don't
218 * really don't really care which we have, but we need to
221 if (mode
!= M_ADD
&& pwd
== NULL
&& isdigit(*a_name
->val
) && atoi(a_name
->val
) > 0) { /* Assume uid */
222 (a_uid
= a_name
)->ch
= 'u';
227 * Update, delete & print require that the user exists
229 if (mode
== M_UPDATE
|| mode
== M_DELETE
|| mode
== M_PRINT
) {
230 if (a_name
== NULL
&& pwd
== NULL
) /* Try harder */
231 pwd
= getpwuid(atoi(a_uid
->val
));
234 if (mode
== M_PRINT
&& getarg(args
, 'F')) {
235 fakeuser
.pw_name
= a_name
? a_name
->val
: "nouser";
236 fakeuser
.pw_uid
= a_uid
? (uid_t
) atol(a_uid
->val
) : -1;
237 return print_user(&fakeuser
, getarg(args
, 'P') != NULL
);
240 cmderr(EX_NOUSER
, "no such uid `%s'\n", a_uid
->val
);
241 cmderr(EX_NOUSER
, "no such user `%s'\n", a_name
->val
);
243 if (a_name
== NULL
) /* May be needed later */
244 a_name
= addarg(args
, 'n', newstr(pwd
->pw_name
));
247 * Handle deletions now
249 if (mode
== M_DELETE
) {
250 char file
[MAXPATHLEN
];
251 char home
[MAXPATHLEN
];
252 uid_t uid
= pwd
->pw_uid
;
254 if (strcmp(pwd
->pw_name
, "root") == 0)
255 cmderr(EX_DATAERR
, "cannot remove user 'root'\n");
260 sprintf(file
, "/var/cron/tabs/%s", pwd
->pw_name
);
261 if (access(file
, F_OK
) == 0) {
262 sprintf(file
, "crontab -u %s -r", pwd
->pw_name
);
266 * Save these for later, since contents of pwd may be
267 * invalidated by deletion
269 sprintf(file
, "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
270 strncpy(home
, pwd
->pw_dir
, sizeof home
);
271 home
[sizeof home
- 1] = '\0';
274 cmderr(EX_IOERR
, "Error updating passwd file: %s\n", strerror(errno
));
275 editgroups(a_name
->val
, NULL
);
277 pw_log(cnf
, mode
, W_USER
, "%s(%ld) account removed", a_name
->val
, (long) uid
);
287 if (getpwuid(uid
) == NULL
)
291 * Remove home directory and contents
293 if (getarg(args
, 'r') != NULL
&& *home
== '/' && getpwuid(uid
) == NULL
) {
294 if (stat(home
, &st
) != -1) {
296 pw_log(cnf
, mode
, W_USER
, "%s(%ld) home '%s' %sremoved",
297 a_name
->val
, (long) uid
, home
,
298 stat(home
, &st
) == -1 ? "" : "not completely ");
302 } else if (mode
== M_PRINT
)
303 return print_user(pwd
, getarg(args
, 'P') != NULL
);
306 * The rest is edit code
308 if ((arg
= getarg(args
, 'l')) != NULL
) {
309 if (strcmp(pwd
->pw_name
, "root") == 0)
310 cmderr(EX_DATAERR
, "can't rename `root' account\n");
311 pwd
->pw_name
= pw_checkname(arg
->val
, 0);
313 if ((arg
= getarg(args
, 'u')) != NULL
&& isdigit(*arg
->val
)) {
314 pwd
->pw_uid
= (uid_t
) atol(arg
->val
);
315 if (pwd
->pw_uid
!= 0 && strcmp(pwd
->pw_name
, "root") == 0)
316 cmderr(EX_DATAERR
, "can't change uid of `root' account\n");
317 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
318 fprintf(stderr
, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd
->pw_name
);
320 if ((arg
= getarg(args
, 'g')) != NULL
&& pwd
->pw_uid
!= 0) /* Already checked this */
321 pwd
->pw_gid
= (gid_t
) getgrnam(cnf
->default_group
)->gr_gid
;
323 if ((arg
= getarg(args
, 'p')) != NULL
) {
324 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0)
327 time_t now
= time(NULL
);
328 time_t expire
= parse_date(now
, arg
->val
);
331 cmderr(EX_DATAERR
, "Invalid password change date `%s'\n", arg
->val
);
332 pwd
->pw_change
= expire
;
335 if ((arg
= getarg(args
, 'e')) != NULL
) {
336 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0)
339 time_t now
= time(NULL
);
340 time_t expire
= parse_date(now
, arg
->val
);
343 cmderr(EX_DATAERR
, "Invalid account expiry date `%s'\n", arg
->val
);
344 pwd
->pw_expire
= expire
;
347 if ((arg
= getarg(args
, 's')) != NULL
)
348 pwd
->pw_shell
= shell_path(cnf
->shelldir
, cnf
->shells
, arg
->val
);
350 if (getarg(args
, 'L'))
351 pwd
->pw_class
= cnf
->default_class
;
353 if ((arg
= getarg(args
, 'w')) != NULL
&& getarg(args
, 'h') == NULL
)
354 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
357 if (a_name
== NULL
) /* Required */
358 cmderr(EX_DATAERR
, "login name required\n");
359 else if ((pwd
= getpwnam(a_name
->val
)) != NULL
) /* Exists */
360 cmderr(EX_DATAERR
, "login name `%s' already exists\n", a_name
->val
);
363 * Now, set up defaults for a new user
366 pwd
->pw_name
= a_name
->val
;
367 pwd
->pw_class
= cnf
->default_class
? cnf
->default_class
: "";
368 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
369 pwd
->pw_uid
= pw_uidpolicy(cnf
, args
);
370 pwd
->pw_gid
= pw_gidpolicy(cnf
, args
, pwd
->pw_name
, (gid_t
) pwd
->pw_uid
);
371 pwd
->pw_change
= pw_pwdpolicy(cnf
, args
);
372 pwd
->pw_expire
= pw_exppolicy(cnf
, args
);
373 pwd
->pw_dir
= pw_homepolicy(cnf
, args
, pwd
->pw_name
);
374 pwd
->pw_shell
= pw_shellpolicy(cnf
, args
, NULL
);
376 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
377 fprintf(stderr
, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd
->pw_name
);
381 * Shared add/edit code
383 if ((arg
= getarg(args
, 'c')) != NULL
)
384 pwd
->pw_gecos
= pw_checkname(arg
->val
, 1);
386 if ((arg
= getarg(args
, 'h')) != NULL
) {
387 if (strcmp(arg
->val
, "-") == 0)
388 pwd
->pw_passwd
= "*"; /* No access */
390 int fd
= atoi(arg
->val
);
392 int istty
= isatty(fd
);
396 if (tcgetattr(fd
, &t
) == -1)
399 struct termios n
= t
;
402 n
.c_lflag
&= ~(ECHO
);
403 tcsetattr(fd
, TCSANOW
, &n
);
404 printf("%sassword for user %s:", (mode
== M_UPDATE
) ? "New p" : "P", pwd
->pw_name
);
408 b
= read(fd
, line
, sizeof(line
) - 1);
409 if (istty
) { /* Restore state */
410 tcsetattr(fd
, TCSANOW
, &t
);
415 perror("-h file descriptor");
419 if ((p
= strpbrk(line
, " \t\r\n")) != NULL
)
422 cmderr(EX_DATAERR
, "empty password read on file descriptor %d\n", fd
);
423 pwd
->pw_passwd
= pw_pwcrypt(line
);
428 * Special case: -N only displays & exits
430 if (getarg(args
, 'N') != NULL
)
431 return print_user(pwd
, getarg(args
, 'P') != NULL
);
433 if ((mode
== M_ADD
&& !addpwent(pwd
)) ||
434 (mode
== M_UPDATE
&& !chgpwent(a_name
->val
, pwd
))) {
435 perror("password update");
439 * Ok, user is created or changed - now edit group file
442 if (mode
== M_ADD
|| getarg(args
, 'G') != NULL
)
443 editgroups(pwd
->pw_name
, cnf
->groups
);
445 /* pwd may have been invalidated */
446 if ((pwd
= getpwnam(a_name
->val
)) == NULL
)
447 cmderr(EX_NOUSER
, "user '%s' disappeared during update\n", a_name
->val
);
449 grp
= getgrgid(pwd
->pw_gid
);
450 pw_log(cnf
, mode
, W_USER
, "%s(%ld):%s(%d):%s:%s:%s",
451 pwd
->pw_name
, (long) pwd
->pw_uid
,
452 grp
? grp
->gr_name
: "unknown", (long) (grp
? grp
->gr_gid
: -1),
453 pwd
->pw_gecos
, pwd
->pw_dir
, pwd
->pw_shell
);
456 * If adding, let's touch and chown the user's mail file. This is not
457 * strictly necessary under BSD with a 0755 maildir but it also
458 * doesn't hurt anything to create the empty mailfile
463 sprintf(line
, "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
464 close(open(line
, O_RDWR
| O_CREAT
, 0600)); /* Preserve contents &
466 chown(line
, pwd
->pw_uid
, pwd
->pw_gid
);
469 * Send mail to the new user as well, if we are asked to
471 if (cnf
->newmail
&& *cnf
->newmail
&& (fp
= fopen(cnf
->newmail
, "r")) != NULL
) {
472 FILE *pfp
= popen(_PATH_SENDMAIL
" -t", "w");
477 fprintf(pfp
, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd
->pw_name
);
478 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
479 /* Do substitutions? */
483 pw_log(cnf
, mode
, W_USER
, "%s(%ld) new user mail sent",
484 pwd
->pw_name
, (long) pwd
->pw_uid
);
490 * Finally, let's create and populate the user's home directory. Note
491 * that this also `works' for editing users if -m is used, but
492 * existing files will *not* be overwritten.
494 if (getarg(args
, 'm') != NULL
&& pwd
->pw_dir
&& *pwd
->pw_dir
== '/' && pwd
->pw_dir
[1]) {
495 copymkdir(pwd
->pw_dir
, cnf
->dotdir
, 0755, pwd
->pw_uid
, pwd
->pw_gid
);
496 pw_log(cnf
, mode
, W_USER
, "%s(%ld) home %s made",
497 pwd
->pw_name
, (long) pwd
->pw_uid
, pwd
->pw_dir
);
504 pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
)
507 uid_t uid
= (uid_t
) - 1;
508 struct carg
*a_uid
= getarg(args
, 'u');
511 * Check the given uid, if any
514 uid
= (uid_t
) atol(a_uid
->val
);
516 if ((pwd
= getpwuid(uid
)) != NULL
&& getarg(args
, 'o') == NULL
)
517 cmderr(EX_DATAERR
, "uid `%ld' has already been allocated\n", (long) pwd
->pw_uid
);
522 * We need to allocate the next available uid under one of
523 * two policies a) Grab the first unused uid b) Grab the
524 * highest possible unused uid
526 if (cnf
->min_uid
>= cnf
->max_uid
) { /* Sanity
527 * claus^H^H^H^Hheck */
529 cnf
->max_uid
= 32000;
531 bm
= bm_alloc(cnf
->max_uid
- cnf
->min_uid
+ 1);
534 * Now, let's fill the bitmap from the password file
537 while ((pwd
= getpwent()) != NULL
)
538 if (pwd
->pw_uid
>= (int) cnf
->min_uid
&& pwd
->pw_uid
<= (int) cnf
->max_uid
)
539 bm_setbit(&bm
, pwd
->pw_uid
- cnf
->min_uid
);
543 * Then apply the policy, with fallback to reuse if necessary
545 if (cnf
->reuse_uids
|| (uid
= (uid_t
) (bm_lastset(&bm
) + cnf
->min_uid
+ 1)) > cnf
->max_uid
)
546 uid
= (uid_t
) (bm_firstunset(&bm
) + cnf
->min_uid
);
549 * Another sanity check
551 if (uid
< cnf
->min_uid
|| uid
> cnf
->max_uid
)
552 cmderr(EX_SOFTWARE
, "unable to allocate a new uid - range fully used\n");
560 pw_gidpolicy(struct userconf
* cnf
, struct cargs
* args
, char *nam
, gid_t prefer
)
563 gid_t gid
= (uid_t
) - 1;
564 struct carg
*a_gid
= getarg(args
, 'g');
567 * If no arg given, see if default can help out
569 if (a_gid
== NULL
&& cnf
->default_group
&& *cnf
->default_group
)
570 a_gid
= addarg(args
, 'g', cnf
->default_group
);
573 * Check the given gid, if any
577 if ((grp
= getgrnam(a_gid
->val
)) == NULL
) {
578 gid
= (gid_t
) atol(a_gid
->val
);
579 if ((gid
== 0 && !isdigit(*a_gid
->val
)) || (grp
= getgrgid(gid
)) == NULL
)
580 cmderr(EX_NOUSER
, "group `%s' is not defined\n", a_gid
->val
);
583 } else if ((grp
= getgrnam(nam
)) != NULL
&& grp
->gr_mem
[0] == NULL
) {
584 gid
= grp
->gr_gid
; /* Already created? Use it anyway... */
586 struct cargs grpargs
;
590 addarg(&grpargs
, 'n', nam
);
593 * We need to auto-create a group with the user's name. We
594 * can send all the appropriate output to our sister routine
595 * bit first see if we can create a group with gid==uid so we
596 * can keep the user and group ids in sync. We purposely do
597 * NOT check the gid range if we can force the sync. If the
598 * user's name dups an existing group, then the group add
599 * function will happily handle that case for us and exit.
601 if (getgrgid(prefer
) == NULL
) {
602 sprintf(tmp
, "%lu", (unsigned long) prefer
);
603 addarg(&grpargs
, 'g', tmp
);
605 if (getarg(args
, 'N'))
607 addarg(&grpargs
, 'N', NULL
);
608 addarg(&grpargs
, 'q', NULL
);
609 gid
= pw_group(cnf
, M_NEXT
, &grpargs
);
613 pw_group(cnf
, M_ADD
, &grpargs
);
614 if ((grp
= getgrnam(nam
)) != NULL
)
617 a_gid
= grpargs
.lh_first
;
618 while (a_gid
!= NULL
) {
619 struct carg
*t
= a_gid
->list
.le_next
;
620 LIST_REMOVE(a_gid
, list
);
630 pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
)
633 time_t now
= time(NULL
);
634 struct carg
*arg
= getarg(args
, 'e');
637 if ((result
= parse_date(now
, arg
->val
)) == now
)
638 cmderr(EX_DATAERR
, "invalid date/time `%s'\n", arg
->val
);
639 } else if (cnf
->password_days
> 0)
640 result
= now
+ ((long) cnf
->password_days
* 86400L);
646 pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
)
649 time_t now
= time(NULL
);
650 struct carg
*arg
= getarg(args
, 'e');
653 if ((result
= parse_date(now
, arg
->val
)) == now
)
654 cmderr(EX_DATAERR
, "invalid date/time `%s'\n", arg
->val
);
655 } else if (cnf
->expire_days
> 0)
656 result
= now
+ ((long) cnf
->expire_days
* 86400L);
662 pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
664 struct carg
*arg
= getarg(args
, 'd');
669 static char home
[128];
671 if (cnf
->home
== NULL
|| *cnf
->home
== '\0')
672 cmderr(EX_CONFIG
, "no base home directory set\n");
673 sprintf(home
, "%s/%s", cnf
->home
, user
);
679 shell_path(char const * path
, char *shells
[], char *sh
)
681 if (sh
!= NULL
&& (*sh
== '/' || *sh
== '\0'))
682 return sh
; /* specified full path or forced none */
685 char paths
[_UC_MAXLINE
];
688 * We need to search paths
690 strncpy(paths
, path
, sizeof paths
);
691 paths
[sizeof paths
- 1] = '\0';
692 for (p
= strtok(paths
, ": \t\r\n"); p
!= NULL
; p
= strtok(NULL
, ": \t\r\n")) {
694 static char shellpath
[256];
697 sprintf(shellpath
, "%s/%s", p
, sh
);
698 if (access(shellpath
, X_OK
) == 0)
701 for (i
= 0; i
< _UC_MAXSHELLS
&& shells
[i
] != NULL
; i
++) {
702 sprintf(shellpath
, "%s/%s", p
, shells
[i
]);
703 if (access(shellpath
, X_OK
) == 0)
708 cmderr(EX_OSFILE
, "can't find shell `%s' in shell paths\n", sh
);
709 cmderr(EX_CONFIG
, "no default shell available or defined\n");
716 pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
)
719 struct carg
*arg
= getarg(args
, 's');
721 if (newshell
== NULL
&& arg
!= NULL
)
723 return shell_path(cnf
->shelldir
, cnf
->shells
, sh
? sh
: cnf
->shell_default
);
726 static char const chars
[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
729 pw_pwcrypt(char *password
)
734 static char buf
[256];
737 * Calculate a salt value
739 srandom((unsigned) (time(NULL
) | getpid()));
740 for (i
= 0; i
< 8; i
++)
741 salt
[i
] = chars
[random() % 63];
744 return strcpy(buf
, crypt(password
, salt
));
749 pw_password(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
754 switch (cnf
->default_password
) {
755 case -1: /* Random password */
756 srandom((unsigned) (time(NULL
) | getpid()));
757 l
= (random() % 8 + 8); /* 8 - 16 chars */
758 for (i
= 0; i
< l
; i
++)
759 pwbuf
[i
] = chars
[random() % sizeof(chars
)];
763 * We give this information back to the user
765 if (getarg(args
, 'h') == NULL
&& getarg(args
, 'N') == NULL
) {
767 printf("Password is: ");
768 printf("%s\n", pwbuf
);
773 case -2: /* No password at all! */
776 case 0: /* No login - default */
780 case 1: /* user's name */
781 strncpy(pwbuf
, user
, sizeof pwbuf
);
782 pwbuf
[sizeof pwbuf
- 1] = '\0';
785 return pw_pwcrypt(pwbuf
);
790 print_user(struct passwd
* pwd
, int pretty
)
793 char buf
[_UC_MAXLINE
];
800 struct group
*grp
= getgrgid(pwd
->pw_gid
);
801 char uname
[60] = "User &", office
[60] = "[None]",
802 wphone
[60] = "[None]", hphone
[60] = "[None]";
804 if ((p
= strtok(pwd
->pw_gecos
, ",")) != NULL
) {
805 strncpy(uname
, p
, sizeof uname
);
806 uname
[sizeof uname
- 1] = '\0';
807 if ((p
= strtok(NULL
, ",")) != NULL
) {
808 strncpy(office
, p
, sizeof office
);
809 office
[sizeof office
- 1] = '\0';
810 if ((p
= strtok(NULL
, ",")) != NULL
) {
811 strncpy(wphone
, p
, sizeof wphone
);
812 wphone
[sizeof wphone
- 1] = '\0';
813 if ((p
= strtok(NULL
, "")) != NULL
) {
814 strncpy(hphone
, p
, sizeof hphone
);
815 hphone
[sizeof hphone
- 1] = '\0';
821 * Handle '&' in gecos field
823 if ((p
= strchr(uname
, '&')) != NULL
) {
824 int l
= strlen(pwd
->pw_name
);
827 memmove(p
+ l
, p
+ 1, m
);
828 memmove(p
, pwd
->pw_name
, l
);
829 *p
= (char) toupper(*p
);
831 printf("Login Name : %-10s #%-22ld Group : %-10s #%ld\n"
833 " Home : %-32.32s Class : %s\n"
834 " Shell : %-32.32s Office : %s\n"
835 "Work Phone : %-32.32s Home Phone : %s\n",
837 pwd
->pw_name
, (long) pwd
->pw_uid
,
838 grp
? grp
->gr_name
: "(invalid)", (long) pwd
->pw_gid
,
839 uname
, pwd
->pw_dir
, pwd
->pw_class
,
840 pwd
->pw_shell
, office
, wphone
, hphone
);
843 while ((grp
=getgrent()) != NULL
)
846 while (i
< _UC_MAXGROUPS
&& grp
->gr_mem
[i
] != NULL
)
848 if (strcmp(grp
->gr_mem
[i
], pwd
->pw_name
)==0)
850 printf(j
++ == 0 ? " Groups : %s" : ",%s", grp
->gr_name
);
857 printf("%s\n", j
? "\n" : "");
863 pw_checkname(char *name
, int gecos
)
866 char const *notch
= gecos
? ":" : " :+-&#%$^()!@~*?<>=|\\/\"";
869 if (strchr(notch
, name
[l
]) != NULL
|| name
[l
] < ' ')
870 cmderr(EX_DATAERR
, "invalid character `%c' in field\n", name
[l
]);
874 cmderr(EX_DATAERR
, "name too long `%s'\n", name
);
882 DIR *d
= opendir("/var/at/jobs");
887 while ((e
= readdir(d
)) != NULL
) {
890 if (strncmp(e
->d_name
, ".lock", 5) != 0 &&
891 stat(e
->d_name
, &st
) == 0 &&
892 !S_ISDIR(st
.st_mode
) &&
894 char tmp
[MAXPATHLEN
];
896 sprintf(tmp
, "/usr/bin/atrm %s", e
->d_name
);