]>
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
33 #include <sys/param.h>
36 #include <sys/types.h>
38 #include <sys/resource.h>
40 #if defined(USE_MD5RAND)
47 #if (MAXLOGNAME-1) > UT_NAMESIZE
48 #define LOGNAMESIZE UT_NAMESIZE
50 #define LOGNAMESIZE (MAXLOGNAME-1)
53 static int print_user(struct passwd
* pwd
, int pretty
);
54 static uid_t
pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
);
55 static uid_t
pw_gidpolicy(struct userconf
* cnf
, struct cargs
* args
, char *nam
, gid_t prefer
);
56 static time_t pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
);
57 static time_t pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
);
58 static char *pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
);
59 static char *pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
);
60 static char *pw_password(struct userconf
* cnf
, struct cargs
* args
, char const * user
);
61 static char *shell_path(char const * path
, char *shells
[], char *sh
);
62 static void rmat(uid_t uid
);
63 static void rmskey(char const * name
);
66 * -C config configuration file
70 * -c comment user name/comment
71 * -d directory home directory
72 * -e date account expiry date
73 * -p date password expiry date
74 * -g grp primary group
75 * -G grp1,grp2 additional groups
76 * -m [ -k dir ] create and set up home
77 * -s shell name of login shell
80 * -l name new login name
81 * -h fd password filehandle
82 * -F force print or add
84 * -D set user defaults
85 * -b dir default home root dir
86 * -e period default expiry period
87 * -p period default password change period
88 * -g group default group
89 * -G grp1,grp2.. default additional groups
90 * -L class default login class
91 * -k dir default home skeleton
92 * -s shell default shell
93 * -w method default password method
97 pw_user(struct userconf
* cnf
, int mode
, struct cargs
* args
)
104 struct passwd
*pwd
= NULL
;
107 char line
[_PASSWORD_LEN
+1];
109 static struct passwd fakeuser
=
124 * With M_NEXT, we only need to return the
129 uid_t next
= pw_uidpolicy(cnf
, args
);
130 if (getarg(args
, 'q'))
132 printf("%ld:", (long)next
);
133 pw_group(cnf
, mode
, args
);
138 * We can do all of the common legwork here
141 if ((arg
= getarg(args
, 'b')) != NULL
) {
142 cnf
->home
= arg
->val
;
146 * If we'll need to use it or we're updating it,
147 * then create the base home directory if necessary
149 if (arg
!= NULL
|| getarg(args
, 'm') != NULL
) {
150 int l
= strlen(cnf
->home
);
152 if (l
> 1 && cnf
->home
[l
-1] == '/') /* Shave off any trailing path delimiter */
153 cnf
->home
[--l
] = '\0';
155 if (l
< 2 || *cnf
->home
!= '/') /* Check for absolute path name */
156 cmderr(EX_DATAERR
, "invalid base directory for home '%s'\n", cnf
->home
);
158 if (stat(cnf
->home
, &st
) == -1) {
159 char dbuf
[MAXPATHLEN
];
162 * This is a kludge especially for Joerg :)
163 * If the home directory would be created in the root partition, then
164 * we really create it under /usr which is likely to have more space.
165 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
167 if (strchr(cnf
->home
+1, '/') == NULL
) {
168 strcpy(dbuf
, "/usr");
169 strncat(dbuf
, cnf
->home
, MAXPATHLEN
-5);
170 if (mkdir(dbuf
, 0755) != -1 || errno
== EEXIST
) {
172 symlink(dbuf
, cnf
->home
);
174 /* If this falls, fall back to old method */
176 p
= strncpy(dbuf
, cnf
->home
, sizeof dbuf
);
177 dbuf
[MAXPATHLEN
-1] = '\0';
178 if (stat(dbuf
, &st
) == -1) {
179 while ((p
= strchr(++p
, '/')) != NULL
) {
181 if (stat(dbuf
, &st
) == -1) {
182 if (mkdir(dbuf
, 0755) == -1)
185 } else if (!S_ISDIR(st
.st_mode
))
186 cmderr(EX_OSFILE
, "'%s' (root home parent) is not a directory\n", dbuf
);
190 if (stat(dbuf
, &st
) == -1) {
191 if (mkdir(dbuf
, 0755) == -1) {
192 direrr
: cmderr(EX_OSFILE
, "mkdir '%s': %s\n", dbuf
, strerror(errno
));
196 } else if (!S_ISDIR(st
.st_mode
))
197 cmderr(EX_OSFILE
, "root home `%s' is not a directory\n", cnf
->home
);
201 if ((arg
= getarg(args
, 'e')) != NULL
)
202 cnf
->expire_days
= atoi(arg
->val
);
204 if ((arg
= getarg(args
, 'y')) != NULL
)
205 cnf
->nispasswd
= arg
->val
;
207 if ((arg
= getarg(args
, 'p')) != NULL
&& arg
->val
)
208 cnf
->password_days
= atoi(arg
->val
);
210 if ((arg
= getarg(args
, 'g')) != NULL
) {
212 if ((grp
= getgrnam(p
)) == NULL
) {
213 if (!isdigit(*p
) || (grp
= getgrgid((gid_t
) atoi(p
))) == NULL
)
214 cmderr(EX_NOUSER
, "group `%s' does not exist\n", p
);
216 cnf
->default_group
= newstr(grp
->gr_name
);
218 if ((arg
= getarg(args
, 'L')) != NULL
)
219 cnf
->default_class
= pw_checkname((u_char
*)arg
->val
, 0);
221 if ((arg
= getarg(args
, 'G')) != NULL
&& arg
->val
) {
224 for (p
= strtok(arg
->val
, ", \t"); p
!= NULL
; p
= strtok(NULL
, ", \t")) {
225 if ((grp
= getgrnam(p
)) == NULL
) {
226 if (!isdigit(*p
) || (grp
= getgrgid((gid_t
) atoi(p
))) == NULL
)
227 cmderr(EX_NOUSER
, "group `%s' does not exist\n", p
);
229 if (extendarray(&cnf
->groups
, &cnf
->numgroups
, i
+ 2) != -1)
230 cnf
->groups
[i
++] = newstr(grp
->gr_name
);
232 while (i
< cnf
->numgroups
)
233 cnf
->groups
[i
++] = NULL
;
235 if ((arg
= getarg(args
, 'k')) != NULL
) {
236 if (stat(cnf
->dotdir
= arg
->val
, &st
) == -1 || S_ISDIR(st
.st_mode
))
237 cmderr(EX_OSFILE
, "skeleton `%s' is not a directory or does not exist\n", cnf
->dotdir
);
239 if ((arg
= getarg(args
, 's')) != NULL
)
240 cnf
->shell_default
= arg
->val
;
242 if (mode
== M_ADD
&& getarg(args
, 'D')) {
243 if (getarg(args
, 'n') != NULL
)
244 cmderr(EX_DATAERR
, "can't combine `-D' with `-n name'\n");
245 if ((arg
= getarg(args
, 'u')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
246 if ((cnf
->min_uid
= (uid_t
) atoi(p
)) == 0)
248 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_uid
= (uid_t
) atoi(p
)) < cnf
->min_uid
)
249 cnf
->max_uid
= 32000;
251 if ((arg
= getarg(args
, 'i')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
252 if ((cnf
->min_gid
= (gid_t
) atoi(p
)) == 0)
254 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_gid
= (gid_t
) atoi(p
)) < cnf
->min_gid
)
255 cnf
->max_gid
= 32000;
257 if ((arg
= getarg(args
, 'w')) != NULL
)
258 cnf
->default_password
= boolean_val(arg
->val
, cnf
->default_password
);
260 arg
= getarg(args
, 'C');
261 if (write_userconfig(arg
? arg
->val
: NULL
))
263 perror("config update");
266 if (mode
== M_PRINT
&& getarg(args
, 'a')) {
267 int pretty
= getarg(args
, 'P') != NULL
;
270 while ((pwd
= getpwent()) != NULL
)
271 print_user(pwd
, pretty
);
275 if ((a_name
= getarg(args
, 'n')) != NULL
)
276 pwd
= getpwnam(pw_checkname((u_char
*)a_name
->val
, 0));
277 a_uid
= getarg(args
, 'u');
281 cmderr(EX_DATAERR
, "user name or id required\n");
284 * Determine whether 'n' switch is name or uid - we don't
285 * really don't really care which we have, but we need to
288 if (mode
!= M_ADD
&& pwd
== NULL
&& isdigit(*a_name
->val
) && atoi(a_name
->val
) > 0) { /* Assume uid */
289 (a_uid
= a_name
)->ch
= 'u';
294 * Update, delete & print require that the user exists
296 if (mode
== M_UPDATE
|| mode
== M_DELETE
|| mode
== M_PRINT
) {
297 if (a_name
== NULL
&& pwd
== NULL
) /* Try harder */
298 pwd
= getpwuid(atoi(a_uid
->val
));
301 if (mode
== M_PRINT
&& getarg(args
, 'F')) {
302 fakeuser
.pw_name
= a_name
? a_name
->val
: "nouser";
303 fakeuser
.pw_uid
= a_uid
? (uid_t
) atol(a_uid
->val
) : -1;
304 return print_user(&fakeuser
, getarg(args
, 'P') != NULL
);
307 cmderr(EX_NOUSER
, "no such uid `%s'\n", a_uid
->val
);
308 cmderr(EX_NOUSER
, "no such user `%s'\n", a_name
->val
);
310 if (a_name
== NULL
) /* May be needed later */
311 a_name
= addarg(args
, 'n', newstr(pwd
->pw_name
));
314 * Handle deletions now
316 if (mode
== M_DELETE
) {
317 char file
[MAXPATHLEN
];
318 char home
[MAXPATHLEN
];
319 uid_t uid
= pwd
->pw_uid
;
321 if (strcmp(pwd
->pw_name
, "root") == 0)
322 cmderr(EX_DATAERR
, "cannot remove user 'root'\n");
325 * Remove skey record from /etc/skeykeys
328 rmskey(pwd
->pw_name
);
333 sprintf(file
, "/var/cron/tabs/%s", pwd
->pw_name
);
334 if (access(file
, F_OK
) == 0) {
335 sprintf(file
, "crontab -u %s -r", pwd
->pw_name
);
339 * Save these for later, since contents of pwd may be
340 * invalidated by deletion
342 sprintf(file
, "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
343 strncpy(home
, pwd
->pw_dir
, sizeof home
);
344 home
[sizeof home
- 1] = '\0';
347 cmderr(EX_IOERR
, "Error updating passwd file: %s\n", strerror(errno
));
349 if (cnf
->nispasswd
&& *cnf
->nispasswd
=='/' && !delnispwent(cnf
->nispasswd
, a_name
->val
))
350 perror("WARNING: NIS passwd update");
352 editgroups(a_name
->val
, NULL
);
354 pw_log(cnf
, mode
, W_USER
, "%s(%ld) account removed", a_name
->val
, (long) uid
);
364 if (getpwuid(uid
) == NULL
)
368 * Remove home directory and contents
370 if (getarg(args
, 'r') != NULL
&& *home
== '/' && getpwuid(uid
) == NULL
) {
371 if (stat(home
, &st
) != -1) {
373 pw_log(cnf
, mode
, W_USER
, "%s(%ld) home '%s' %sremoved",
374 a_name
->val
, (long) uid
, home
,
375 stat(home
, &st
) == -1 ? "" : "not completely ");
379 } else if (mode
== M_PRINT
)
380 return print_user(pwd
, getarg(args
, 'P') != NULL
);
383 * The rest is edit code
385 if ((arg
= getarg(args
, 'l')) != NULL
) {
386 if (strcmp(pwd
->pw_name
, "root") == 0)
387 cmderr(EX_DATAERR
, "can't rename `root' account\n");
388 pwd
->pw_name
= pw_checkname((u_char
*)arg
->val
, 0);
390 if ((arg
= getarg(args
, 'u')) != NULL
&& isdigit(*arg
->val
)) {
391 pwd
->pw_uid
= (uid_t
) atol(arg
->val
);
392 if (pwd
->pw_uid
!= 0 && strcmp(pwd
->pw_name
, "root") == 0)
393 cmderr(EX_DATAERR
, "can't change uid of `root' account\n");
394 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
395 fprintf(stderr
, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd
->pw_name
);
397 if ((arg
= getarg(args
, 'g')) != NULL
&& pwd
->pw_uid
!= 0) /* Already checked this */
398 pwd
->pw_gid
= (gid_t
) getgrnam(cnf
->default_group
)->gr_gid
;
400 if ((arg
= getarg(args
, 'p')) != NULL
) {
401 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0)
404 time_t now
= time(NULL
);
405 time_t expire
= parse_date(now
, arg
->val
);
408 cmderr(EX_DATAERR
, "Invalid password change date `%s'\n", arg
->val
);
409 pwd
->pw_change
= expire
;
412 if ((arg
= getarg(args
, 'e')) != NULL
) {
413 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0)
416 time_t now
= time(NULL
);
417 time_t expire
= parse_date(now
, arg
->val
);
420 cmderr(EX_DATAERR
, "Invalid account expiry date `%s'\n", arg
->val
);
421 pwd
->pw_expire
= expire
;
424 if ((arg
= getarg(args
, 's')) != NULL
)
425 pwd
->pw_shell
= shell_path(cnf
->shelldir
, cnf
->shells
, arg
->val
);
427 if (getarg(args
, 'L'))
428 pwd
->pw_class
= cnf
->default_class
;
430 if ((arg
= getarg(args
, 'd')) != NULL
) {
431 if (stat(pwd
->pw_dir
= arg
->val
, &st
) == -1) {
432 if (getarg(args
, 'm') == NULL
&& strcmp(pwd
->pw_dir
, "/nonexistent") != 0)
433 fprintf(stderr
, "WARNING: home `%s' does not exist\n", pwd
->pw_dir
);
434 } else if (!S_ISDIR(st
.st_mode
))
435 fprintf(stderr
, "WARNING: home `%s' is not a directory\n", pwd
->pw_dir
);
438 if ((arg
= getarg(args
, 'w')) != NULL
&& getarg(args
, 'h') == NULL
)
439 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
442 if (a_name
== NULL
) /* Required */
443 cmderr(EX_DATAERR
, "login name required\n");
444 else if ((pwd
= getpwnam(a_name
->val
)) != NULL
) /* Exists */
445 cmderr(EX_DATAERR
, "login name `%s' already exists\n", a_name
->val
);
448 * Now, set up defaults for a new user
451 pwd
->pw_name
= a_name
->val
;
452 pwd
->pw_class
= cnf
->default_class
? cnf
->default_class
: "";
453 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
454 pwd
->pw_uid
= pw_uidpolicy(cnf
, args
);
455 pwd
->pw_gid
= pw_gidpolicy(cnf
, args
, pwd
->pw_name
, (gid_t
) pwd
->pw_uid
);
456 pwd
->pw_change
= pw_pwdpolicy(cnf
, args
);
457 pwd
->pw_expire
= pw_exppolicy(cnf
, args
);
458 pwd
->pw_dir
= pw_homepolicy(cnf
, args
, pwd
->pw_name
);
459 pwd
->pw_shell
= pw_shellpolicy(cnf
, args
, NULL
);
461 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
462 fprintf(stderr
, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd
->pw_name
);
466 * Shared add/edit code
468 if ((arg
= getarg(args
, 'c')) != NULL
)
469 pwd
->pw_gecos
= pw_checkname((u_char
*)arg
->val
, 1);
471 if ((arg
= getarg(args
, 'h')) != NULL
) {
472 if (strcmp(arg
->val
, "-") == 0)
473 pwd
->pw_passwd
= "*"; /* No access */
475 int fd
= atoi(arg
->val
);
477 int istty
= isatty(fd
);
481 if (tcgetattr(fd
, &t
) == -1)
484 struct termios n
= t
;
487 n
.c_lflag
&= ~(ECHO
);
488 tcsetattr(fd
, TCSANOW
, &n
);
489 printf("%sassword for user %s:", (mode
== M_UPDATE
) ? "New p" : "P", pwd
->pw_name
);
493 b
= read(fd
, line
, sizeof(line
) - 1);
494 if (istty
) { /* Restore state */
495 tcsetattr(fd
, TCSANOW
, &t
);
500 perror("-h file descriptor");
504 if ((p
= strpbrk(line
, " \t\r\n")) != NULL
)
507 cmderr(EX_DATAERR
, "empty password read on file descriptor %d\n", fd
);
508 pwd
->pw_passwd
= pw_pwcrypt(line
);
513 * Special case: -N only displays & exits
515 if (getarg(args
, 'N') != NULL
)
516 return print_user(pwd
, getarg(args
, 'P') != NULL
);
521 if (r
&& cnf
->nispasswd
&& *cnf
->nispasswd
=='/')
522 r1
= addnispwent(cnf
->nispasswd
, pwd
);
523 } else if (mode
== M_UPDATE
) {
524 r
= chgpwent(a_name
->val
, pwd
);
525 if (r
&& cnf
->nispasswd
&& *cnf
->nispasswd
=='/')
526 r1
= chgnispwent(cnf
->nispasswd
, a_name
->val
, pwd
);
530 perror("password update");
533 perror("WARNING: NIS password update");
534 /* Keep on trucking */
538 * Ok, user is created or changed - now edit group file
541 if (mode
== M_ADD
|| getarg(args
, 'G') != NULL
)
542 editgroups(pwd
->pw_name
, cnf
->groups
);
544 /* pwd may have been invalidated */
545 if ((pwd
= getpwnam(a_name
->val
)) == NULL
)
546 cmderr(EX_NOUSER
, "user '%s' disappeared during update\n", a_name
->val
);
548 grp
= getgrgid(pwd
->pw_gid
);
549 pw_log(cnf
, mode
, W_USER
, "%s(%ld):%s(%d):%s:%s:%s",
550 pwd
->pw_name
, (long) pwd
->pw_uid
,
551 grp
? grp
->gr_name
: "unknown", (long) (grp
? grp
->gr_gid
: -1),
552 pwd
->pw_gecos
, pwd
->pw_dir
, pwd
->pw_shell
);
555 * If adding, let's touch and chown the user's mail file. This is not
556 * strictly necessary under BSD with a 0755 maildir but it also
557 * doesn't hurt anything to create the empty mailfile
562 sprintf(line
, "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
563 close(open(line
, O_RDWR
| O_CREAT
, 0600)); /* Preserve contents &
565 chown(line
, pwd
->pw_uid
, pwd
->pw_gid
);
568 * Send mail to the new user as well, if we are asked to
570 if (cnf
->newmail
&& *cnf
->newmail
&& (fp
= fopen(cnf
->newmail
, "r")) != NULL
) {
571 FILE *pfp
= popen(_PATH_SENDMAIL
" -t", "w");
576 fprintf(pfp
, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd
->pw_name
);
577 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
578 /* Do substitutions? */
582 pw_log(cnf
, mode
, W_USER
, "%s(%ld) new user mail sent",
583 pwd
->pw_name
, (long) pwd
->pw_uid
);
589 * Finally, let's create and populate the user's home directory. Note
590 * that this also `works' for editing users if -m is used, but
591 * existing files will *not* be overwritten.
593 if (getarg(args
, 'm') != NULL
&& pwd
->pw_dir
&& *pwd
->pw_dir
== '/' && pwd
->pw_dir
[1]) {
594 copymkdir(pwd
->pw_dir
, cnf
->dotdir
, 0755, pwd
->pw_uid
, pwd
->pw_gid
);
595 pw_log(cnf
, mode
, W_USER
, "%s(%ld) home %s made",
596 pwd
->pw_name
, (long) pwd
->pw_uid
, pwd
->pw_dir
);
603 pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
)
606 uid_t uid
= (uid_t
) - 1;
607 struct carg
*a_uid
= getarg(args
, 'u');
610 * Check the given uid, if any
613 uid
= (uid_t
) atol(a_uid
->val
);
615 if ((pwd
= getpwuid(uid
)) != NULL
&& getarg(args
, 'o') == NULL
)
616 cmderr(EX_DATAERR
, "uid `%ld' has already been allocated\n", (long) pwd
->pw_uid
);
621 * We need to allocate the next available uid under one of
622 * two policies a) Grab the first unused uid b) Grab the
623 * highest possible unused uid
625 if (cnf
->min_uid
>= cnf
->max_uid
) { /* Sanity
626 * claus^H^H^H^Hheck */
628 cnf
->max_uid
= 32000;
630 bm
= bm_alloc(cnf
->max_uid
- cnf
->min_uid
+ 1);
633 * Now, let's fill the bitmap from the password file
636 while ((pwd
= getpwent()) != NULL
)
637 if (pwd
->pw_uid
>= (int) cnf
->min_uid
&& pwd
->pw_uid
<= (int) cnf
->max_uid
)
638 bm_setbit(&bm
, pwd
->pw_uid
- cnf
->min_uid
);
642 * Then apply the policy, with fallback to reuse if necessary
644 if (cnf
->reuse_uids
|| (uid
= (uid_t
) (bm_lastset(&bm
) + cnf
->min_uid
+ 1)) > cnf
->max_uid
)
645 uid
= (uid_t
) (bm_firstunset(&bm
) + cnf
->min_uid
);
648 * Another sanity check
650 if (uid
< cnf
->min_uid
|| uid
> cnf
->max_uid
)
651 cmderr(EX_SOFTWARE
, "unable to allocate a new uid - range fully used\n");
659 pw_gidpolicy(struct userconf
* cnf
, struct cargs
* args
, char *nam
, gid_t prefer
)
662 gid_t gid
= (uid_t
) - 1;
663 struct carg
*a_gid
= getarg(args
, 'g');
666 * If no arg given, see if default can help out
668 if (a_gid
== NULL
&& cnf
->default_group
&& *cnf
->default_group
)
669 a_gid
= addarg(args
, 'g', cnf
->default_group
);
672 * Check the given gid, if any
676 if ((grp
= getgrnam(a_gid
->val
)) == NULL
) {
677 gid
= (gid_t
) atol(a_gid
->val
);
678 if ((gid
== 0 && !isdigit(*a_gid
->val
)) || (grp
= getgrgid(gid
)) == NULL
)
679 cmderr(EX_NOUSER
, "group `%s' is not defined\n", a_gid
->val
);
682 } else if ((grp
= getgrnam(nam
)) != NULL
&& grp
->gr_mem
[0] == NULL
) {
683 gid
= grp
->gr_gid
; /* Already created? Use it anyway... */
685 struct cargs grpargs
;
689 addarg(&grpargs
, 'n', nam
);
692 * We need to auto-create a group with the user's name. We
693 * can send all the appropriate output to our sister routine
694 * bit first see if we can create a group with gid==uid so we
695 * can keep the user and group ids in sync. We purposely do
696 * NOT check the gid range if we can force the sync. If the
697 * user's name dups an existing group, then the group add
698 * function will happily handle that case for us and exit.
700 if (getgrgid(prefer
) == NULL
) {
701 sprintf(tmp
, "%lu", (unsigned long) prefer
);
702 addarg(&grpargs
, 'g', tmp
);
704 if (getarg(args
, 'N'))
706 addarg(&grpargs
, 'N', NULL
);
707 addarg(&grpargs
, 'q', NULL
);
708 gid
= pw_group(cnf
, M_NEXT
, &grpargs
);
712 pw_group(cnf
, M_ADD
, &grpargs
);
713 if ((grp
= getgrnam(nam
)) != NULL
)
716 a_gid
= grpargs
.lh_first
;
717 while (a_gid
!= NULL
) {
718 struct carg
*t
= a_gid
->list
.le_next
;
719 LIST_REMOVE(a_gid
, list
);
729 pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
)
732 time_t now
= time(NULL
);
733 struct carg
*arg
= getarg(args
, 'e');
736 if ((result
= parse_date(now
, arg
->val
)) == now
)
737 cmderr(EX_DATAERR
, "invalid date/time `%s'\n", arg
->val
);
738 } else if (cnf
->password_days
> 0)
739 result
= now
+ ((long) cnf
->password_days
* 86400L);
745 pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
)
748 time_t now
= time(NULL
);
749 struct carg
*arg
= getarg(args
, 'e');
752 if ((result
= parse_date(now
, arg
->val
)) == now
)
753 cmderr(EX_DATAERR
, "invalid date/time `%s'\n", arg
->val
);
754 } else if (cnf
->expire_days
> 0)
755 result
= now
+ ((long) cnf
->expire_days
* 86400L);
761 pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
763 struct carg
*arg
= getarg(args
, 'd');
768 static char home
[128];
770 if (cnf
->home
== NULL
|| *cnf
->home
== '\0')
771 cmderr(EX_CONFIG
, "no base home directory set\n");
772 sprintf(home
, "%s/%s", cnf
->home
, user
);
778 shell_path(char const * path
, char *shells
[], char *sh
)
780 if (sh
!= NULL
&& (*sh
== '/' || *sh
== '\0'))
781 return sh
; /* specified full path or forced none */
784 char paths
[_UC_MAXLINE
];
787 * We need to search paths
789 strncpy(paths
, path
, sizeof paths
);
790 paths
[sizeof paths
- 1] = '\0';
791 for (p
= strtok(paths
, ": \t\r\n"); p
!= NULL
; p
= strtok(NULL
, ": \t\r\n")) {
793 static char shellpath
[256];
796 sprintf(shellpath
, "%s/%s", p
, sh
);
797 if (access(shellpath
, X_OK
) == 0)
800 for (i
= 0; i
< _UC_MAXSHELLS
&& shells
[i
] != NULL
; i
++) {
801 sprintf(shellpath
, "%s/%s", p
, shells
[i
]);
802 if (access(shellpath
, X_OK
) == 0)
807 cmderr(EX_OSFILE
, "can't find shell `%s' in shell paths\n", sh
);
808 cmderr(EX_CONFIG
, "no default shell available or defined\n");
815 pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
)
818 struct carg
*arg
= getarg(args
, 's');
820 if (newshell
== NULL
&& arg
!= NULL
)
822 return shell_path(cnf
->shelldir
, cnf
->shells
, sh
? sh
: cnf
->shell_default
);
825 static char const chars
[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
828 pw_pwcrypt(char *password
)
833 static char buf
[256];
836 * Calculate a salt value
838 srandom((unsigned) (time(NULL
) ^ getpid()));
839 for (i
= 0; i
< 8; i
++)
840 salt
[i
] = chars
[random() % 63];
843 return strcpy(buf
, crypt(password
, salt
));
846 #if defined(__FreeBSD__)
848 #if defined(USE_MD5RAND)
850 pw_getrand(u_char
*buf
, int len
) /* cryptographically secure rng */
853 for (i
=0;i
<len
;i
+=16) {
857 struct timeval tv
, tvo
;
864 MD5Update (&md5_ctx
, (u_char
*)&t
, sizeof t
);
866 MD5Update (&md5_ctx
, (u_char
*)&t
, sizeof t
);
867 gettimeofday (&tvo
, NULL
);
869 getrusage (RUSAGE_SELF
, &ru
);
870 MD5Update (&md5_ctx
, (u_char
*)&ru
, sizeof ru
);
871 gettimeofday (&tv
, NULL
);
872 MD5Update (&md5_ctx
, (u_char
*)&tv
, sizeof tv
);
873 } while (n
++<20 || tv
.tv_usec
-tvo
.tv_usec
<100*1000);
874 MD5Final (ubuf
, &md5_ctx
);
875 memcpy(buf
+i
, ubuf
, MIN(16, len
-n
));
880 #else /* Use random device (preferred) */
883 pw_getrand(u_char
*buf
, int len
)
886 fd
= open("/dev/urandom", O_RDONLY
);
888 cmderr(EX_OSFILE
, "can't open /dev/urandom: %s\n", strerror(errno
));
889 else if (read(fd
, buf
, len
)!=len
)
890 cmderr(EX_IOERR
, "read error on /dev/urandom\n");
897 #else /* Portable version */
900 pw_getrand(u_char
*buf
, int len
)
904 for (i
= 0; i
< len
; i
++) {
905 unsigned val
= random();
906 /* Use all bits in the random value */
907 buf
[i
]=(u_char
)((val
>> 24) ^ (val
>> 16) ^ (val
>> 8) ^ val
);
915 pw_password(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
919 u_char rndbuf
[sizeof pwbuf
];
921 switch (cnf
->default_password
) {
922 case -1: /* Random password */
923 srandom((unsigned) (time(NULL
) ^ getpid()));
924 l
= (random() % 8 + 8); /* 8 - 16 chars */
925 pw_getrand(rndbuf
, l
);
926 for (i
= 0; i
< l
; i
++)
927 pwbuf
[i
] = chars
[rndbuf
[i
] % sizeof(chars
)];
931 * We give this information back to the user
933 if (getarg(args
, 'h') == NULL
&& getarg(args
, 'N') == NULL
) {
935 printf("Password for '%s' is: ", user
);
936 printf("%s\n", pwbuf
);
941 case -2: /* No password at all! */
944 case 0: /* No login - default */
948 case 1: /* user's name */
949 strncpy(pwbuf
, user
, sizeof pwbuf
);
950 pwbuf
[sizeof pwbuf
- 1] = '\0';
953 return pw_pwcrypt(pwbuf
);
958 print_user(struct passwd
* pwd
, int pretty
)
961 char buf
[_UC_MAXLINE
];
968 struct group
*grp
= getgrgid(pwd
->pw_gid
);
969 char uname
[60] = "User &", office
[60] = "[None]",
970 wphone
[60] = "[None]", hphone
[60] = "[None]";
971 char acexpire
[32] = "[None]", pwexpire
[32] = "[None]";
974 if ((p
= strtok(pwd
->pw_gecos
, ",")) != NULL
) {
975 strncpy(uname
, p
, sizeof uname
);
976 uname
[sizeof uname
- 1] = '\0';
977 if ((p
= strtok(NULL
, ",")) != NULL
) {
978 strncpy(office
, p
, sizeof office
);
979 office
[sizeof office
- 1] = '\0';
980 if ((p
= strtok(NULL
, ",")) != NULL
) {
981 strncpy(wphone
, p
, sizeof wphone
);
982 wphone
[sizeof wphone
- 1] = '\0';
983 if ((p
= strtok(NULL
, "")) != NULL
) {
984 strncpy(hphone
, p
, sizeof hphone
);
985 hphone
[sizeof hphone
- 1] = '\0';
991 * Handle '&' in gecos field
993 if ((p
= strchr(uname
, '&')) != NULL
) {
994 int l
= strlen(pwd
->pw_name
);
997 memmove(p
+ l
, p
+ 1, m
);
998 memmove(p
, pwd
->pw_name
, l
);
999 *p
= (char) toupper(*p
);
1001 if (pwd
->pw_expire
> (time_t)0 && (tptr
= localtime(&pwd
->pw_expire
)) != NULL
)
1002 strftime(acexpire
, sizeof acexpire
, "%c", tptr
);
1003 if (pwd
->pw_change
> (time_t)9 && (tptr
= localtime(&pwd
->pw_change
)) != NULL
)
1004 strftime(pwexpire
, sizeof pwexpire
, "%c", tptr
);
1005 printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n"
1007 " Home: %-26.26s Class: %s\n"
1008 " Shell: %-26.26s Office: %s\n"
1009 "Work Phone: %-26.26s Home Phone: %s\n"
1010 "Acc Expire: %-26.26s Pwd Expire: %s\n",
1011 pwd
->pw_name
, (long) pwd
->pw_uid
,
1012 grp
? grp
->gr_name
: "(invalid)", (long) pwd
->pw_gid
,
1013 uname
, pwd
->pw_dir
, pwd
->pw_class
,
1014 pwd
->pw_shell
, office
, wphone
, hphone
,
1015 acexpire
, pwexpire
);
1018 while ((grp
=getgrent()) != NULL
)
1021 while (grp
->gr_mem
[i
] != NULL
)
1023 if (strcmp(grp
->gr_mem
[i
], pwd
->pw_name
)==0)
1025 printf(j
++ == 0 ? " Groups: %s" : ",%s", grp
->gr_name
);
1032 printf("%s\n", j
? "\n" : "");
1034 return EXIT_SUCCESS
;
1038 pw_checkname(u_char
*name
, int gecos
)
1041 char const *notch
= gecos
? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1044 if (strchr(notch
, name
[l
]) != NULL
|| name
[l
] < ' ' || name
[l
] == 127 ||
1045 (!gecos
&& l
==0 && name
[l
] == '-') || /* leading '-' */
1046 (!gecos
&& name
[l
] & 0x80)) /* 8-bit */
1047 cmderr(EX_DATAERR
, (name
[l
] >= ' ' && name
[l
] < 127)
1048 ? "invalid character `%c' in field\n"
1049 : "invalid character 0x%02x in field\n",
1053 if (!gecos
&& l
> LOGNAMESIZE
)
1054 cmderr(EX_DATAERR
, "name too long `%s'\n", name
);
1055 return (char *)name
;
1062 DIR *d
= opendir("/var/at/jobs");
1067 while ((e
= readdir(d
)) != NULL
) {
1070 if (strncmp(e
->d_name
, ".lock", 5) != 0 &&
1071 stat(e
->d_name
, &st
) == 0 &&
1072 !S_ISDIR(st
.st_mode
) &&
1074 char tmp
[MAXPATHLEN
];
1076 sprintf(tmp
, "/usr/bin/atrm %s", e
->d_name
);
1085 rmskey(char const * name
)
1087 static const char etcskey
[] = "/etc/skeykeys";
1088 FILE *fp
= fopen(etcskey
, "r+");
1093 int length
= strlen(name
);
1095 while (fgets(tmp
, sizeof tmp
, fp
) != NULL
) {
1096 if (strncmp(name
, tmp
, length
) == 0 && tmp
[length
]==' ') {
1097 if (fseek(fp
, atofs
, SEEK_SET
) == 0) {
1098 fwrite("#", 1, 1, fp
); /* Comment username out */
1105 * If we got an error of any sort, don't update!