]>
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>
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
);
54 static void rmskey(char const * name
);
57 * -C config configuration file
61 * -c comment user name/comment
62 * -d directory home directory
63 * -e date account expiry date
64 * -p date password expiry date
65 * -g grp primary group
66 * -G grp1,grp2 additional groups
67 * -m [ -k dir ] create and set up home
68 * -s shell name of login shell
71 * -l name new login name
72 * -h fd password filehandle
73 * -F force print or add
75 * -D set user defaults
76 * -b dir default home root dir
77 * -e period default expiry period
78 * -p period default password change period
79 * -g group default group
80 * -G grp1,grp2.. default additional groups
81 * -L class default login class
82 * -k dir default home skeleton
83 * -s shell default shell
84 * -w method default password method
88 pw_user(struct userconf
* cnf
, int mode
, struct cargs
* args
)
95 struct passwd
*pwd
= NULL
;
98 char line
[_PASSWORD_LEN
+1];
100 static struct passwd fakeuser
=
115 * With M_NEXT, we only need to return the
120 uid_t next
= pw_uidpolicy(cnf
, args
);
121 if (getarg(args
, 'q'))
123 printf("%ld:", (long)next
);
124 pw_group(cnf
, mode
, args
);
129 * We can do all of the common legwork here
132 if ((arg
= getarg(args
, 'b')) != NULL
) {
133 cnf
->home
= arg
->val
;
137 * If we'll need to use it or we're updating it,
138 * then create the base home directory if necessary
140 if (arg
!= NULL
|| getarg(args
, 'm') != NULL
) {
141 int l
= strlen(cnf
->home
);
143 if (l
> 1 && cnf
->home
[l
-1] == '/') /* Shave off any trailing path delimiter */
144 cnf
->home
[--l
] = '\0';
146 if (l
< 2 || *cnf
->home
!= '/') /* Check for absolute path name */
147 cmderr(EX_DATAERR
, "invalid base directory for home '%s'\n", cnf
->home
);
149 if (stat(cnf
->home
, &st
) == -1) {
150 char dbuf
[MAXPATHLEN
];
153 * This is a kludge especially for Joerg :)
154 * If the home directory would be created in the root partition, then
155 * we really create it under /usr which is likely to have more space.
156 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
158 if (strchr(cnf
->home
+1, '/') == NULL
) {
159 strcpy(dbuf
, "/usr");
160 strncat(dbuf
, cnf
->home
, MAXPATHLEN
-5);
161 if (mkdir(dbuf
, 0755) != -1 || errno
== EEXIST
) {
163 symlink(dbuf
, cnf
->home
);
165 /* If this falls, fall back to old method */
167 p
= strncpy(dbuf
, cnf
->home
, sizeof dbuf
);
168 dbuf
[MAXPATHLEN
-1] = '\0';
169 if (stat(dbuf
, &st
) == -1) {
170 while ((p
= strchr(++p
, '/')) != NULL
) {
172 if (stat(dbuf
, &st
) == -1) {
173 if (mkdir(dbuf
, 0755) == -1)
176 } else if (!S_ISDIR(st
.st_mode
))
177 cmderr(EX_OSFILE
, "'%s' (root home parent) is not a directory\n", dbuf
);
181 if (stat(dbuf
, &st
) == -1) {
182 if (mkdir(dbuf
, 0755) == -1) {
183 direrr
: cmderr(EX_OSFILE
, "mkdir '%s': %s\n", dbuf
, strerror(errno
));
187 } else if (!S_ISDIR(st
.st_mode
))
188 cmderr(EX_OSFILE
, "root home `%s' is not a directory\n", cnf
->home
);
192 if ((arg
= getarg(args
, 'e')) != NULL
)
193 cnf
->expire_days
= atoi(arg
->val
);
195 if ((arg
= getarg(args
, 'y')) != NULL
)
196 cnf
->nispasswd
= arg
->val
;
198 if ((arg
= getarg(args
, 'p')) != NULL
&& arg
->val
)
199 cnf
->password_days
= atoi(arg
->val
);
201 if ((arg
= getarg(args
, 'g')) != NULL
) {
203 if ((grp
= getgrnam(p
)) == NULL
) {
204 if (!isdigit(*p
) || (grp
= getgrgid((gid_t
) atoi(p
))) == NULL
)
205 cmderr(EX_NOUSER
, "group `%s' does not exist\n", p
);
207 cnf
->default_group
= newstr(grp
->gr_name
);
209 if ((arg
= getarg(args
, 'L')) != NULL
)
210 cnf
->default_class
= pw_checkname((u_char
*)arg
->val
, 0);
212 if ((arg
= getarg(args
, 'G')) != NULL
&& arg
->val
) {
215 for (p
= strtok(arg
->val
, ", \t"); p
!= NULL
; p
= strtok(NULL
, ", \t")) {
216 if ((grp
= getgrnam(p
)) == NULL
) {
217 if (!isdigit(*p
) || (grp
= getgrgid((gid_t
) atoi(p
))) == NULL
)
218 cmderr(EX_NOUSER
, "group `%s' does not exist\n", p
);
220 if (extendarray(&cnf
->groups
, &cnf
->numgroups
, i
+ 2) != -1)
221 cnf
->groups
[i
++] = newstr(grp
->gr_name
);
223 while (i
< cnf
->numgroups
)
224 cnf
->groups
[i
++] = NULL
;
226 if ((arg
= getarg(args
, 'k')) != NULL
) {
227 if (stat(cnf
->dotdir
= arg
->val
, &st
) == -1 || S_ISDIR(st
.st_mode
))
228 cmderr(EX_OSFILE
, "skeleton `%s' is not a directory or does not exist\n", cnf
->dotdir
);
230 if ((arg
= getarg(args
, 's')) != NULL
)
231 cnf
->shell_default
= arg
->val
;
233 if (mode
== M_ADD
&& getarg(args
, 'D')) {
234 if (getarg(args
, 'n') != NULL
)
235 cmderr(EX_DATAERR
, "can't combine `-D' with `-n name'\n");
236 if ((arg
= getarg(args
, 'u')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
237 if ((cnf
->min_uid
= (uid_t
) atoi(p
)) == 0)
239 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_uid
= (uid_t
) atoi(p
)) < cnf
->min_uid
)
240 cnf
->max_uid
= 32000;
242 if ((arg
= getarg(args
, 'i')) != NULL
&& (p
= strtok(arg
->val
, ", \t")) != NULL
) {
243 if ((cnf
->min_gid
= (gid_t
) atoi(p
)) == 0)
245 if ((p
= strtok(NULL
, " ,\t")) == NULL
|| (cnf
->max_gid
= (gid_t
) atoi(p
)) < cnf
->min_gid
)
246 cnf
->max_gid
= 32000;
248 if ((arg
= getarg(args
, 'w')) != NULL
)
249 cnf
->default_password
= boolean_val(arg
->val
, cnf
->default_password
);
251 arg
= getarg(args
, 'C');
252 if (write_userconfig(arg
? arg
->val
: NULL
))
254 perror("config update");
257 if (mode
== M_PRINT
&& getarg(args
, 'a')) {
258 int pretty
= getarg(args
, 'P') != NULL
;
261 while ((pwd
= getpwent()) != NULL
)
262 print_user(pwd
, pretty
);
266 if ((a_name
= getarg(args
, 'n')) != NULL
)
267 pwd
= getpwnam(pw_checkname((u_char
*)a_name
->val
, 0));
268 a_uid
= getarg(args
, 'u');
272 cmderr(EX_DATAERR
, "user name or id required\n");
275 * Determine whether 'n' switch is name or uid - we don't
276 * really don't really care which we have, but we need to
279 if (mode
!= M_ADD
&& pwd
== NULL
&& isdigit(*a_name
->val
) && atoi(a_name
->val
) > 0) { /* Assume uid */
280 (a_uid
= a_name
)->ch
= 'u';
285 * Update, delete & print require that the user exists
287 if (mode
== M_UPDATE
|| mode
== M_DELETE
|| mode
== M_PRINT
) {
288 if (a_name
== NULL
&& pwd
== NULL
) /* Try harder */
289 pwd
= getpwuid(atoi(a_uid
->val
));
292 if (mode
== M_PRINT
&& getarg(args
, 'F')) {
293 fakeuser
.pw_name
= a_name
? a_name
->val
: "nouser";
294 fakeuser
.pw_uid
= a_uid
? (uid_t
) atol(a_uid
->val
) : -1;
295 return print_user(&fakeuser
, getarg(args
, 'P') != NULL
);
298 cmderr(EX_NOUSER
, "no such uid `%s'\n", a_uid
->val
);
299 cmderr(EX_NOUSER
, "no such user `%s'\n", a_name
->val
);
301 if (a_name
== NULL
) /* May be needed later */
302 a_name
= addarg(args
, 'n', newstr(pwd
->pw_name
));
305 * Handle deletions now
307 if (mode
== M_DELETE
) {
308 char file
[MAXPATHLEN
];
309 char home
[MAXPATHLEN
];
310 uid_t uid
= pwd
->pw_uid
;
312 if (strcmp(pwd
->pw_name
, "root") == 0)
313 cmderr(EX_DATAERR
, "cannot remove user 'root'\n");
316 * Remove skey record from /etc/skeykeys
319 rmskey(pwd
->pw_name
);
324 sprintf(file
, "/var/cron/tabs/%s", pwd
->pw_name
);
325 if (access(file
, F_OK
) == 0) {
326 sprintf(file
, "crontab -u %s -r", pwd
->pw_name
);
330 * Save these for later, since contents of pwd may be
331 * invalidated by deletion
333 sprintf(file
, "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
334 strncpy(home
, pwd
->pw_dir
, sizeof home
);
335 home
[sizeof home
- 1] = '\0';
338 cmderr(EX_IOERR
, "Error updating passwd file: %s\n", strerror(errno
));
340 if (cnf
->nispasswd
&& *cnf
->nispasswd
=='/' && !delnispwent(cnf
->nispasswd
, a_name
->val
))
341 perror("WARNING: NIS passwd update");
343 editgroups(a_name
->val
, NULL
);
345 pw_log(cnf
, mode
, W_USER
, "%s(%ld) account removed", a_name
->val
, (long) uid
);
355 if (getpwuid(uid
) == NULL
)
359 * Remove home directory and contents
361 if (getarg(args
, 'r') != NULL
&& *home
== '/' && getpwuid(uid
) == NULL
) {
362 if (stat(home
, &st
) != -1) {
364 pw_log(cnf
, mode
, W_USER
, "%s(%ld) home '%s' %sremoved",
365 a_name
->val
, (long) uid
, home
,
366 stat(home
, &st
) == -1 ? "" : "not completely ");
370 } else if (mode
== M_PRINT
)
371 return print_user(pwd
, getarg(args
, 'P') != NULL
);
374 * The rest is edit code
376 if ((arg
= getarg(args
, 'l')) != NULL
) {
377 if (strcmp(pwd
->pw_name
, "root") == 0)
378 cmderr(EX_DATAERR
, "can't rename `root' account\n");
379 pwd
->pw_name
= pw_checkname((u_char
*)arg
->val
, 0);
381 if ((arg
= getarg(args
, 'u')) != NULL
&& isdigit(*arg
->val
)) {
382 pwd
->pw_uid
= (uid_t
) atol(arg
->val
);
383 if (pwd
->pw_uid
!= 0 && strcmp(pwd
->pw_name
, "root") == 0)
384 cmderr(EX_DATAERR
, "can't change uid of `root' account\n");
385 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
386 fprintf(stderr
, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd
->pw_name
);
388 if ((arg
= getarg(args
, 'g')) != NULL
&& pwd
->pw_uid
!= 0) /* Already checked this */
389 pwd
->pw_gid
= (gid_t
) getgrnam(cnf
->default_group
)->gr_gid
;
391 if ((arg
= getarg(args
, 'p')) != NULL
) {
392 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0)
395 time_t now
= time(NULL
);
396 time_t expire
= parse_date(now
, arg
->val
);
399 cmderr(EX_DATAERR
, "Invalid password change date `%s'\n", arg
->val
);
400 pwd
->pw_change
= expire
;
403 if ((arg
= getarg(args
, 'e')) != NULL
) {
404 if (*arg
->val
== '\0' || strcmp(arg
->val
, "0") == 0)
407 time_t now
= time(NULL
);
408 time_t expire
= parse_date(now
, arg
->val
);
411 cmderr(EX_DATAERR
, "Invalid account expiry date `%s'\n", arg
->val
);
412 pwd
->pw_expire
= expire
;
415 if ((arg
= getarg(args
, 's')) != NULL
)
416 pwd
->pw_shell
= shell_path(cnf
->shelldir
, cnf
->shells
, arg
->val
);
418 if (getarg(args
, 'L'))
419 pwd
->pw_class
= cnf
->default_class
;
421 if ((arg
= getarg(args
, 'd')) != NULL
) {
422 if (stat(pwd
->pw_dir
= arg
->val
, &st
) == -1) {
423 if (getarg(args
, 'm') == NULL
&& strcmp(pwd
->pw_dir
, "/nonexistent") != 0)
424 fprintf(stderr
, "WARNING: home `%s' does not exist\n", pwd
->pw_dir
);
425 } else if (!S_ISDIR(st
.st_mode
))
426 fprintf(stderr
, "WARNING: home `%s' is not a directory\n", pwd
->pw_dir
);
429 if ((arg
= getarg(args
, 'w')) != NULL
&& getarg(args
, 'h') == NULL
)
430 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
433 if (a_name
== NULL
) /* Required */
434 cmderr(EX_DATAERR
, "login name required\n");
435 else if ((pwd
= getpwnam(a_name
->val
)) != NULL
) /* Exists */
436 cmderr(EX_DATAERR
, "login name `%s' already exists\n", a_name
->val
);
439 * Now, set up defaults for a new user
442 pwd
->pw_name
= a_name
->val
;
443 pwd
->pw_class
= cnf
->default_class
? cnf
->default_class
: "";
444 pwd
->pw_passwd
= pw_password(cnf
, args
, pwd
->pw_name
);
445 pwd
->pw_uid
= pw_uidpolicy(cnf
, args
);
446 pwd
->pw_gid
= pw_gidpolicy(cnf
, args
, pwd
->pw_name
, (gid_t
) pwd
->pw_uid
);
447 pwd
->pw_change
= pw_pwdpolicy(cnf
, args
);
448 pwd
->pw_expire
= pw_exppolicy(cnf
, args
);
449 pwd
->pw_dir
= pw_homepolicy(cnf
, args
, pwd
->pw_name
);
450 pwd
->pw_shell
= pw_shellpolicy(cnf
, args
, NULL
);
452 if (pwd
->pw_uid
== 0 && strcmp(pwd
->pw_name
, "root") != 0)
453 fprintf(stderr
, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd
->pw_name
);
457 * Shared add/edit code
459 if ((arg
= getarg(args
, 'c')) != NULL
)
460 pwd
->pw_gecos
= pw_checkname((u_char
*)arg
->val
, 1);
462 if ((arg
= getarg(args
, 'h')) != NULL
) {
463 if (strcmp(arg
->val
, "-") == 0)
464 pwd
->pw_passwd
= "*"; /* No access */
466 int fd
= atoi(arg
->val
);
468 int istty
= isatty(fd
);
472 if (tcgetattr(fd
, &t
) == -1)
475 struct termios n
= t
;
478 n
.c_lflag
&= ~(ECHO
);
479 tcsetattr(fd
, TCSANOW
, &n
);
480 printf("%sassword for user %s:", (mode
== M_UPDATE
) ? "New p" : "P", pwd
->pw_name
);
484 b
= read(fd
, line
, sizeof(line
) - 1);
485 if (istty
) { /* Restore state */
486 tcsetattr(fd
, TCSANOW
, &t
);
491 perror("-h file descriptor");
495 if ((p
= strpbrk(line
, " \t\r\n")) != NULL
)
498 cmderr(EX_DATAERR
, "empty password read on file descriptor %d\n", fd
);
499 pwd
->pw_passwd
= pw_pwcrypt(line
);
504 * Special case: -N only displays & exits
506 if (getarg(args
, 'N') != NULL
)
507 return print_user(pwd
, getarg(args
, 'P') != NULL
);
512 if (r
&& cnf
->nispasswd
&& *cnf
->nispasswd
=='/')
513 r1
= addnispwent(cnf
->nispasswd
, pwd
);
514 } else if (mode
== M_UPDATE
) {
515 r
= chgpwent(a_name
->val
, pwd
);
516 if (r
&& cnf
->nispasswd
&& *cnf
->nispasswd
=='/')
517 r1
= chgnispwent(cnf
->nispasswd
, a_name
->val
, pwd
);
521 perror("password update");
524 perror("WARNING: NIS password update");
525 /* Keep on trucking */
529 * Ok, user is created or changed - now edit group file
532 if (mode
== M_ADD
|| getarg(args
, 'G') != NULL
)
533 editgroups(pwd
->pw_name
, cnf
->groups
);
535 /* pwd may have been invalidated */
536 if ((pwd
= getpwnam(a_name
->val
)) == NULL
)
537 cmderr(EX_NOUSER
, "user '%s' disappeared during update\n", a_name
->val
);
539 grp
= getgrgid(pwd
->pw_gid
);
540 pw_log(cnf
, mode
, W_USER
, "%s(%ld):%s(%d):%s:%s:%s",
541 pwd
->pw_name
, (long) pwd
->pw_uid
,
542 grp
? grp
->gr_name
: "unknown", (long) (grp
? grp
->gr_gid
: -1),
543 pwd
->pw_gecos
, pwd
->pw_dir
, pwd
->pw_shell
);
546 * If adding, let's touch and chown the user's mail file. This is not
547 * strictly necessary under BSD with a 0755 maildir but it also
548 * doesn't hurt anything to create the empty mailfile
553 sprintf(line
, "%s/%s", _PATH_MAILDIR
, pwd
->pw_name
);
554 close(open(line
, O_RDWR
| O_CREAT
, 0600)); /* Preserve contents &
556 chown(line
, pwd
->pw_uid
, pwd
->pw_gid
);
559 * Send mail to the new user as well, if we are asked to
561 if (cnf
->newmail
&& *cnf
->newmail
&& (fp
= fopen(cnf
->newmail
, "r")) != NULL
) {
562 FILE *pfp
= popen(_PATH_SENDMAIL
" -t", "w");
567 fprintf(pfp
, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd
->pw_name
);
568 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
569 /* Do substitutions? */
573 pw_log(cnf
, mode
, W_USER
, "%s(%ld) new user mail sent",
574 pwd
->pw_name
, (long) pwd
->pw_uid
);
580 * Finally, let's create and populate the user's home directory. Note
581 * that this also `works' for editing users if -m is used, but
582 * existing files will *not* be overwritten.
584 if (getarg(args
, 'm') != NULL
&& pwd
->pw_dir
&& *pwd
->pw_dir
== '/' && pwd
->pw_dir
[1]) {
585 copymkdir(pwd
->pw_dir
, cnf
->dotdir
, 0755, pwd
->pw_uid
, pwd
->pw_gid
);
586 pw_log(cnf
, mode
, W_USER
, "%s(%ld) home %s made",
587 pwd
->pw_name
, (long) pwd
->pw_uid
, pwd
->pw_dir
);
594 pw_uidpolicy(struct userconf
* cnf
, struct cargs
* args
)
597 uid_t uid
= (uid_t
) - 1;
598 struct carg
*a_uid
= getarg(args
, 'u');
601 * Check the given uid, if any
604 uid
= (uid_t
) atol(a_uid
->val
);
606 if ((pwd
= getpwuid(uid
)) != NULL
&& getarg(args
, 'o') == NULL
)
607 cmderr(EX_DATAERR
, "uid `%ld' has already been allocated\n", (long) pwd
->pw_uid
);
612 * We need to allocate the next available uid under one of
613 * two policies a) Grab the first unused uid b) Grab the
614 * highest possible unused uid
616 if (cnf
->min_uid
>= cnf
->max_uid
) { /* Sanity
617 * claus^H^H^H^Hheck */
619 cnf
->max_uid
= 32000;
621 bm
= bm_alloc(cnf
->max_uid
- cnf
->min_uid
+ 1);
624 * Now, let's fill the bitmap from the password file
627 while ((pwd
= getpwent()) != NULL
)
628 if (pwd
->pw_uid
>= (int) cnf
->min_uid
&& pwd
->pw_uid
<= (int) cnf
->max_uid
)
629 bm_setbit(&bm
, pwd
->pw_uid
- cnf
->min_uid
);
633 * Then apply the policy, with fallback to reuse if necessary
635 if (cnf
->reuse_uids
|| (uid
= (uid_t
) (bm_lastset(&bm
) + cnf
->min_uid
+ 1)) > cnf
->max_uid
)
636 uid
= (uid_t
) (bm_firstunset(&bm
) + cnf
->min_uid
);
639 * Another sanity check
641 if (uid
< cnf
->min_uid
|| uid
> cnf
->max_uid
)
642 cmderr(EX_SOFTWARE
, "unable to allocate a new uid - range fully used\n");
650 pw_gidpolicy(struct userconf
* cnf
, struct cargs
* args
, char *nam
, gid_t prefer
)
653 gid_t gid
= (uid_t
) - 1;
654 struct carg
*a_gid
= getarg(args
, 'g');
657 * If no arg given, see if default can help out
659 if (a_gid
== NULL
&& cnf
->default_group
&& *cnf
->default_group
)
660 a_gid
= addarg(args
, 'g', cnf
->default_group
);
663 * Check the given gid, if any
667 if ((grp
= getgrnam(a_gid
->val
)) == NULL
) {
668 gid
= (gid_t
) atol(a_gid
->val
);
669 if ((gid
== 0 && !isdigit(*a_gid
->val
)) || (grp
= getgrgid(gid
)) == NULL
)
670 cmderr(EX_NOUSER
, "group `%s' is not defined\n", a_gid
->val
);
673 } else if ((grp
= getgrnam(nam
)) != NULL
&& grp
->gr_mem
[0] == NULL
) {
674 gid
= grp
->gr_gid
; /* Already created? Use it anyway... */
676 struct cargs grpargs
;
680 addarg(&grpargs
, 'n', nam
);
683 * We need to auto-create a group with the user's name. We
684 * can send all the appropriate output to our sister routine
685 * bit first see if we can create a group with gid==uid so we
686 * can keep the user and group ids in sync. We purposely do
687 * NOT check the gid range if we can force the sync. If the
688 * user's name dups an existing group, then the group add
689 * function will happily handle that case for us and exit.
691 if (getgrgid(prefer
) == NULL
) {
692 sprintf(tmp
, "%lu", (unsigned long) prefer
);
693 addarg(&grpargs
, 'g', tmp
);
695 if (getarg(args
, 'N'))
697 addarg(&grpargs
, 'N', NULL
);
698 addarg(&grpargs
, 'q', NULL
);
699 gid
= pw_group(cnf
, M_NEXT
, &grpargs
);
703 pw_group(cnf
, M_ADD
, &grpargs
);
704 if ((grp
= getgrnam(nam
)) != NULL
)
707 a_gid
= grpargs
.lh_first
;
708 while (a_gid
!= NULL
) {
709 struct carg
*t
= a_gid
->list
.le_next
;
710 LIST_REMOVE(a_gid
, list
);
720 pw_pwdpolicy(struct userconf
* cnf
, struct cargs
* args
)
723 time_t now
= time(NULL
);
724 struct carg
*arg
= getarg(args
, 'e');
727 if ((result
= parse_date(now
, arg
->val
)) == now
)
728 cmderr(EX_DATAERR
, "invalid date/time `%s'\n", arg
->val
);
729 } else if (cnf
->password_days
> 0)
730 result
= now
+ ((long) cnf
->password_days
* 86400L);
736 pw_exppolicy(struct userconf
* cnf
, struct cargs
* args
)
739 time_t now
= time(NULL
);
740 struct carg
*arg
= getarg(args
, 'e');
743 if ((result
= parse_date(now
, arg
->val
)) == now
)
744 cmderr(EX_DATAERR
, "invalid date/time `%s'\n", arg
->val
);
745 } else if (cnf
->expire_days
> 0)
746 result
= now
+ ((long) cnf
->expire_days
* 86400L);
752 pw_homepolicy(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
754 struct carg
*arg
= getarg(args
, 'd');
759 static char home
[128];
761 if (cnf
->home
== NULL
|| *cnf
->home
== '\0')
762 cmderr(EX_CONFIG
, "no base home directory set\n");
763 sprintf(home
, "%s/%s", cnf
->home
, user
);
769 shell_path(char const * path
, char *shells
[], char *sh
)
771 if (sh
!= NULL
&& (*sh
== '/' || *sh
== '\0'))
772 return sh
; /* specified full path or forced none */
775 char paths
[_UC_MAXLINE
];
778 * We need to search paths
780 strncpy(paths
, path
, sizeof paths
);
781 paths
[sizeof paths
- 1] = '\0';
782 for (p
= strtok(paths
, ": \t\r\n"); p
!= NULL
; p
= strtok(NULL
, ": \t\r\n")) {
784 static char shellpath
[256];
787 sprintf(shellpath
, "%s/%s", p
, sh
);
788 if (access(shellpath
, X_OK
) == 0)
791 for (i
= 0; i
< _UC_MAXSHELLS
&& shells
[i
] != NULL
; i
++) {
792 sprintf(shellpath
, "%s/%s", p
, shells
[i
]);
793 if (access(shellpath
, X_OK
) == 0)
798 cmderr(EX_OSFILE
, "can't find shell `%s' in shell paths\n", sh
);
799 cmderr(EX_CONFIG
, "no default shell available or defined\n");
806 pw_shellpolicy(struct userconf
* cnf
, struct cargs
* args
, char *newshell
)
809 struct carg
*arg
= getarg(args
, 's');
811 if (newshell
== NULL
&& arg
!= NULL
)
813 return shell_path(cnf
->shelldir
, cnf
->shells
, sh
? sh
: cnf
->shell_default
);
816 static char const chars
[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
819 pw_pwcrypt(char *password
)
824 static char buf
[256];
827 * Calculate a salt value
829 srandom((unsigned) (time(NULL
) ^ getpid()));
830 for (i
= 0; i
< 8; i
++)
831 salt
[i
] = chars
[random() % 63];
834 return strcpy(buf
, crypt(password
, salt
));
837 #if defined(__FreeBSD__)
839 #if defined(USE_MD5RAND)
841 pw_getrand(u_char
*buf
, int len
) /* cryptographically secure rng */
844 for (i
=0;i
<len
;i
+=16) {
848 struct timeval tv
, tvo
;
855 MD5Update (&md5_ctx
, (u_char
*)&t
, sizeof t
);
857 MD5Update (&md5_ctx
, (u_char
*)&t
, sizeof t
);
858 gettimeofday (&tvo
, NULL
);
860 getrusage (RUSAGE_SELF
, &ru
);
861 MD5Update (&md5_ctx
, (u_char
*)&ru
, sizeof ru
);
862 gettimeofday (&tv
, NULL
);
863 MD5Update (&md5_ctx
, (u_char
*)&tv
, sizeof tv
);
864 } while (n
++<20 || tv
.tv_usec
-tvo
.tv_usec
<100*1000);
865 MD5Final (ubuf
, &md5_ctx
);
866 memcpy(buf
+i
, ubuf
, MIN(16, len
-n
));
871 #else /* Use random device (preferred) */
874 pw_getrand(u_char
*buf
, int len
)
877 fd
= open("/dev/urandom", O_RDONLY
);
879 cmderr(EX_OSFILE
, "can't open /dev/urandom: %s\n", strerror(errno
));
880 else if (read(fd
, buf
, len
)!=len
)
881 cmderr(EX_IOERR
, "read error on /dev/urandom\n");
888 #else /* Portable version */
891 pw_getrand(u_char
*buf
, int len
)
895 for (i
= 0; i
< len
; i
++) {
896 unsigned val
= random();
897 /* Use all bits in the random value */
898 buf
[i
]=(u_char
)((val
>> 24) ^ (val
>> 16) ^ (val
>> 8) ^ val
);
906 pw_password(struct userconf
* cnf
, struct cargs
* args
, char const * user
)
910 u_char rndbuf
[sizeof pwbuf
];
912 switch (cnf
->default_password
) {
913 case -1: /* Random password */
914 srandom((unsigned) (time(NULL
) ^ getpid()));
915 l
= (random() % 8 + 8); /* 8 - 16 chars */
916 pw_getrand(rndbuf
, l
);
917 for (i
= 0; i
< l
; i
++)
918 pwbuf
[i
] = chars
[rndbuf
[i
] % sizeof(chars
)];
922 * We give this information back to the user
924 if (getarg(args
, 'h') == NULL
&& getarg(args
, 'N') == NULL
) {
926 printf("Password for '%s' is: ", user
);
927 printf("%s\n", pwbuf
);
932 case -2: /* No password at all! */
935 case 0: /* No login - default */
939 case 1: /* user's name */
940 strncpy(pwbuf
, user
, sizeof pwbuf
);
941 pwbuf
[sizeof pwbuf
- 1] = '\0';
944 return pw_pwcrypt(pwbuf
);
949 print_user(struct passwd
* pwd
, int pretty
)
952 char buf
[_UC_MAXLINE
];
959 struct group
*grp
= getgrgid(pwd
->pw_gid
);
960 char uname
[60] = "User &", office
[60] = "[None]",
961 wphone
[60] = "[None]", hphone
[60] = "[None]";
962 char acexpire
[32] = "[None]", pwexpire
[32] = "[None]";
965 if ((p
= strtok(pwd
->pw_gecos
, ",")) != NULL
) {
966 strncpy(uname
, p
, sizeof uname
);
967 uname
[sizeof uname
- 1] = '\0';
968 if ((p
= strtok(NULL
, ",")) != NULL
) {
969 strncpy(office
, p
, sizeof office
);
970 office
[sizeof office
- 1] = '\0';
971 if ((p
= strtok(NULL
, ",")) != NULL
) {
972 strncpy(wphone
, p
, sizeof wphone
);
973 wphone
[sizeof wphone
- 1] = '\0';
974 if ((p
= strtok(NULL
, "")) != NULL
) {
975 strncpy(hphone
, p
, sizeof hphone
);
976 hphone
[sizeof hphone
- 1] = '\0';
982 * Handle '&' in gecos field
984 if ((p
= strchr(uname
, '&')) != NULL
) {
985 int l
= strlen(pwd
->pw_name
);
988 memmove(p
+ l
, p
+ 1, m
);
989 memmove(p
, pwd
->pw_name
, l
);
990 *p
= (char) toupper(*p
);
992 if (pwd
->pw_expire
> (time_t)0 && (tptr
= localtime(&pwd
->pw_expire
)) != NULL
)
993 strftime(acexpire
, sizeof acexpire
, "%c", tptr
);
994 if (pwd
->pw_change
> (time_t)9 && (tptr
= localtime(&pwd
->pw_change
)) != NULL
)
995 strftime(pwexpire
, sizeof pwexpire
, "%c", tptr
);
996 printf("Login Name: %-10s #%-16ld Group: %-10s #%ld\n"
998 " Home: %-26.26s Class: %s\n"
999 " Shell: %-26.26s Office: %s\n"
1000 "Work Phone: %-26.26s Home Phone: %s\n"
1001 "Acc Expire: %-26.26s Pwd Expire: %s\n",
1002 pwd
->pw_name
, (long) pwd
->pw_uid
,
1003 grp
? grp
->gr_name
: "(invalid)", (long) pwd
->pw_gid
,
1004 uname
, pwd
->pw_dir
, pwd
->pw_class
,
1005 pwd
->pw_shell
, office
, wphone
, hphone
,
1006 acexpire
, pwexpire
);
1009 while ((grp
=getgrent()) != NULL
)
1012 while (grp
->gr_mem
[i
] != NULL
)
1014 if (strcmp(grp
->gr_mem
[i
], pwd
->pw_name
)==0)
1016 printf(j
++ == 0 ? " Groups: %s" : ",%s", grp
->gr_name
);
1023 printf("%s\n", j
? "\n" : "");
1025 return EXIT_SUCCESS
;
1029 pw_checkname(u_char
*name
, int gecos
)
1032 char const *notch
= gecos
? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1035 if (strchr(notch
, name
[l
]) != NULL
|| name
[l
] < ' ' || name
[l
] == 127 ||
1036 (!gecos
&& l
==0 && name
[l
] == '-') || /* leading '-' */
1037 (!gecos
&& name
[l
] & 0x80)) /* 8-bit */
1038 cmderr(EX_DATAERR
, (name
[l
] >= ' ' && name
[l
] < 127)
1039 ? "invalid character `%c' in field\n"
1040 : "invalid character 0x%02x in field\n",
1044 if (!gecos
&& l
> MAXLOGNAME
)
1045 cmderr(EX_DATAERR
, "name too long `%s'\n", name
);
1046 return (char *)name
;
1053 DIR *d
= opendir("/var/at/jobs");
1058 while ((e
= readdir(d
)) != NULL
) {
1061 if (strncmp(e
->d_name
, ".lock", 5) != 0 &&
1062 stat(e
->d_name
, &st
) == 0 &&
1063 !S_ISDIR(st
.st_mode
) &&
1065 char tmp
[MAXPATHLEN
];
1067 sprintf(tmp
, "/usr/bin/atrm %s", e
->d_name
);
1076 rmskey(char const * name
)
1078 static const char etcskey
[] = "/etc/skeykeys";
1079 FILE *fp
= fopen(etcskey
, "r+");
1084 int length
= strlen(name
);
1086 while (fgets(tmp
, sizeof tmp
, fp
) != NULL
) {
1087 if (strncmp(name
, tmp
, length
) == 0 && tmp
[length
]==' ') {
1088 if (fseek(fp
, atofs
, SEEK_SET
) == 0) {
1089 fwrite("#", 1, 1, fp
); /* Comment username out */
1096 * If we got an error of any sort, don't update!