user.c revision 1.27 1 /* $NetBSD: user.c,v 1.27 2000/10/17 05:31:50 simonb Exp $ */
2
3 /*
4 * Copyright (c) 1999 Alistair G. Crooks. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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 Alistair G. Crooks.
17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior written
19 * permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 #include <sys/cdefs.h>
34
35 #ifndef lint
36 __COPYRIGHT("@(#) Copyright (c) 1999 \
37 The NetBSD Foundation, Inc. All rights reserved.");
38 __RCSID("$NetBSD: user.c,v 1.27 2000/10/17 05:31:50 simonb Exp $");
39 #endif
40
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/stat.h>
44
45 #include <ctype.h>
46 #include <dirent.h>
47 #include <err.h>
48 #include <fcntl.h>
49 #include <grp.h>
50 #include <paths.h>
51 #include <pwd.h>
52 #include <stdarg.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <time.h>
57 #include <unistd.h>
58 #include <util.h>
59
60 #include "defs.h"
61 #include "usermgmt.h"
62
63
64 /* this struct describes a uid range */
65 typedef struct range_t {
66 int r_from; /* low uid */
67 int r_to; /* high uid */
68 } range_t;
69
70 /* this struct encapsulates the user information */
71 typedef struct user_t {
72 int u_flags; /* see below */
73 int u_uid; /* uid of user */
74 char *u_password; /* encrypted password */
75 char *u_comment; /* comment field */
76 char *u_home; /* home directory */
77 char *u_primgrp; /* primary group */
78 int u_groupc; /* # of secondary groups */
79 char *u_groupv[NGROUPS_MAX]; /* secondary groups */
80 char *u_shell; /* user's shell */
81 char *u_basedir; /* base directory for home */
82 char *u_expire; /* when password will expire */
83 int u_inactive; /* inactive */
84 char *u_skeldir; /* directory for startup files */
85 unsigned u_rsize; /* size of range array */
86 unsigned u_rc; /* # of ranges */
87 range_t *u_rv; /* the ranges */
88 unsigned u_defrc; /* # of ranges in defaults */
89 int u_preserve; /* preserve uids on deletion */
90 } user_t;
91
92 /* flags for which fields of the user_t replace the passwd entry */
93 enum {
94 F_COMMENT = 0x0001,
95 F_DUPUID = 0x0002,
96 F_EXPIRE = 0x0004,
97 F_GROUP = 0x0008,
98 F_HOMEDIR = 0x0010,
99 F_MKDIR = 0x0020,
100 F_INACTIVE = 0x0040,
101 F_PASSWORD = 0x0080,
102 F_SECGROUP = 0x0100,
103 F_SHELL = 0x0200,
104 F_UID = 0x0400,
105 F_USERNAME = 0x0800
106 };
107
108 #define CONFFILE "/etc/usermgmt.conf"
109
110 #ifndef DEF_GROUP
111 #define DEF_GROUP "users"
112 #endif
113
114 #ifndef DEF_BASEDIR
115 #define DEF_BASEDIR "/home"
116 #endif
117
118 #ifndef DEF_SKELDIR
119 #define DEF_SKELDIR "/etc/skel"
120 #endif
121
122 #ifndef DEF_SHELL
123 #define DEF_SHELL _PATH_CSHELL
124 #endif
125
126 #ifndef DEF_COMMENT
127 #define DEF_COMMENT ""
128 #endif
129
130 #ifndef DEF_LOWUID
131 #define DEF_LOWUID 1000
132 #endif
133
134 #ifndef DEF_HIGHUID
135 #define DEF_HIGHUID 60000
136 #endif
137
138 #ifndef DEF_INACTIVE
139 #define DEF_INACTIVE 0
140 #endif
141
142 #ifndef DEF_EXPIRE
143 #define DEF_EXPIRE NULL
144 #endif
145
146 #ifndef WAITSECS
147 #define WAITSECS 10
148 #endif
149
150 #ifndef NOBODY_UID
151 #define NOBODY_UID 32767
152 #endif
153
154 /* some useful constants */
155 enum {
156 MaxShellNameLen = 256,
157 MaxFileNameLen = MAXPATHLEN,
158 MaxUserNameLen = 32,
159 MaxFieldNameLen = 32,
160 MaxCommandLen = 2048,
161 MaxEntryLen = 2048,
162 PasswordLength = 13,
163
164 LowGid = DEF_LOWUID,
165 HighGid = DEF_HIGHUID
166 };
167
168 /* Full paths of programs used here */
169 #define CHOWN "/usr/sbin/chown"
170 #define MKDIR "/bin/mkdir"
171 #define MV "/bin/mv"
172 #define NOLOGIN "/sbin/nologin"
173 #define PAX "/bin/pax"
174 #define RM "/bin/rm"
175
176 #define UNSET_EXPIRY "Null (unset)"
177
178 static int asystem(const char *fmt, ...)
179 __attribute__((__format__(__printf__, 1, 2)));
180
181 static int verbose;
182
183 /* if *cpp is non-null, free it, then assign `n' chars of `s' to it */
184 static void
185 memsave(char **cpp, char *s, size_t n)
186 {
187 if (*cpp != NULL) {
188 FREE(*cpp);
189 }
190 NEWARRAY(char, *cpp, n + 1, exit(1));
191 (void) memcpy(*cpp, s, n);
192 (*cpp)[n] = '\0';
193 }
194
195 /* a replacement for system(3) */
196 static int
197 asystem(const char *fmt, ...)
198 {
199 va_list vp;
200 char buf[MaxCommandLen];
201 int ret;
202
203 va_start(vp, fmt);
204 (void) vsnprintf(buf, sizeof(buf), fmt, vp);
205 va_end(vp);
206 if (verbose) {
207 (void) printf("Command: %s\n", buf);
208 }
209 if ((ret = system(buf)) != 0) {
210 warnx("[Warning] can't system `%s'", buf);
211 }
212 return ret;
213 }
214
215 /* remove a users home directory, returning 1 for success (ie, no problems encountered) */
216 static int
217 removehomedir(const char *user, int uid, const char *dir)
218 {
219 struct stat st;
220
221 /* userid not root? */
222 if (uid == 0) {
223 warnx("Not deleting home directory `%s'; userid is 0", dir);
224 return 0;
225 }
226
227 /* directory exists (and is a directory!) */
228 if (stat(dir, &st) < 0) {
229 warnx("Home directory `%s' doesn't exist", dir);
230 return 0;
231 }
232 if (!S_ISDIR(st.st_mode)) {
233 warnx("Home directory `%s' is not a directory", dir);
234 return 0;
235 }
236
237 /* userid matches directory owner? */
238 if (st.st_uid != uid) {
239 warnx("User `%s' doesn't own directory `%s', not removed\n", user, dir);
240 return 0;
241 }
242
243 (void) seteuid(uid);
244 /* we add the "|| true" to keep asystem() quiet if there is a non-zero exit status. */
245 #if 1
246 printf("XXX: %s -rf %s > /dev/null 2>&1 || true", RM, dir);
247 #else
248 (void) asystem("%s -rf %s > /dev/null 2>&1 || true", RM, dir);
249 (void) seteuid(0);
250 if (rmdir(dir) < 0) {
251 warnx("Unable to remove all files in `%s'\n", dir);
252 return 0;
253 }
254 #endif
255 return 1;
256 }
257
258 #define NetBSD_1_4_K 104110000
259
260 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ < NetBSD_1_4_K)
261 /* bounds checking strncpy */
262 static int
263 strlcpy(char *to, char *from, size_t tosize)
264 {
265 size_t n;
266 int fromsize;
267
268 fromsize = strlen(from);
269 n = MIN(tosize - 1, fromsize);
270 (void) memcpy(to, from, n);
271 to[n] = '\0';
272 return fromsize;
273 }
274 #endif /* NetBSD < 1.4K */
275
276 /*
277 * Copyright (c) 1997 Todd C. Miller <Todd.Miller (at) courtesan.com>
278 * All rights reserved.
279 *
280 * Redistribution and use in source and binary forms, with or without
281 * modification, are permitted provided that the following conditions
282 * are met:
283 * 1. Redistributions of source code must retain the above copyright
284 * notice, this list of conditions and the following disclaimer.
285 * 2. Redistributions in binary form must reproduce the above copyright
286 * notice, this list of conditions and the following disclaimer in the
287 * documentation and/or other materials provided with the distribution.
288 * 3. The name of the author may not be used to endorse or promote products
289 * derived from this software without specific prior written permission.
290 *
291 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
292 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
293 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
294 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
295 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
296 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
297 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
298 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
299 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
300 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
301 */
302
303 /*
304 * research has shown that NetBSD 1.3H was the first version of -current
305 * with asprintf in libc. agc
306 */
307 #define NetBSD_1_3_H 103080000
308
309 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ < NetBSD_1_3_H)
310
311 int
312 asprintf(char **str, char const *fmt, ...)
313 {
314 int ret;
315 va_list ap;
316 FILE f;
317 unsigned char *_base;
318
319 va_start(ap, fmt);
320 f._flags = __SWR | __SSTR | __SALC;
321 f._bf._base = f._p = (unsigned char *)malloc(128);
322 if (f._bf._base == NULL)
323 goto err;
324 f._bf._size = f._w = 127; /* Leave room for the NUL */
325 ret = vfprintf(&f, fmt, ap);
326 if (ret == -1)
327 goto err;
328 *f._p = '\0';
329 va_end(ap);
330 _base = realloc(f._bf._base, (size_t)(ret + 1));
331 if (_base == NULL)
332 goto err;
333 *str = (char *)_base;
334 return (ret);
335
336 err:
337 if (f._bf._base)
338 free(f._bf._base);
339 *str = NULL;
340 return (-1);
341 }
342 #endif /* NetBSD < 1.3H */
343
344 /* return 1 if all of `s' is numeric */
345 static int
346 is_number(char *s)
347 {
348 for ( ; *s ; s++) {
349 if (!isdigit(*s)) {
350 return 0;
351 }
352 }
353 return 1;
354 }
355
356 /*
357 * check that the effective uid is 0 - called from funcs which will
358 * modify data and config files.
359 */
360 static void
361 checkeuid(void)
362 {
363 if (geteuid() != 0) {
364 errx(EXIT_FAILURE, "Program must be run as root");
365 }
366 }
367
368 /* copy any dot files into the user's home directory */
369 static int
370 copydotfiles(char *skeldir, int uid, int gid, char *dir)
371 {
372 struct dirent *dp;
373 DIR *dirp;
374 int n;
375
376 if ((dirp = opendir(skeldir)) == NULL) {
377 warn("can't open source . files dir `%s'", skeldir);
378 return 0;
379 }
380 for (n = 0; (dp = readdir(dirp)) != NULL && n == 0 ; ) {
381 if (strcmp(dp->d_name, ".") == 0 ||
382 strcmp(dp->d_name, "..") == 0) {
383 continue;
384 }
385 n = 1;
386 }
387 (void) closedir(dirp);
388 if (n == 0) {
389 warnx("No \"dot\" initialisation files found");
390 } else {
391 (void) asystem("cd %s; %s -rw -pe %s . %s",
392 skeldir, PAX, (verbose) ? "-v" : "", dir);
393 }
394 (void) asystem("%s -R -h %d:%d %s", CHOWN, uid, gid, dir);
395 return n;
396 }
397
398 /* create a group entry with gid `gid' */
399 static int
400 creategid(char *group, int gid, char *name)
401 {
402 struct stat st;
403 FILE *from;
404 FILE *to;
405 char buf[MaxEntryLen];
406 char f[MaxFileNameLen];
407 int fd;
408 int cc;
409
410 if (getgrnam(group) != NULL) {
411 warnx("group `%s' already exists", group);
412 return 0;
413 }
414 if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
415 warn("can't create gid for %s: can't open %s", name, _PATH_GROUP);
416 return 0;
417 }
418 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
419 warn("can't lock `%s'", _PATH_GROUP);
420 }
421 (void) fstat(fileno(from), &st);
422 (void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
423 if ((fd = mkstemp(f)) < 0) {
424 (void) fclose(from);
425 warn("can't create gid: mkstemp failed");
426 return 0;
427 }
428 if ((to = fdopen(fd, "w")) == NULL) {
429 (void) fclose(from);
430 (void) close(fd);
431 (void) unlink(f);
432 warn("can't create gid: fdopen `%s' failed", f);
433 return 0;
434 }
435 while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) {
436 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
437 (void) fclose(from);
438 (void) close(fd);
439 (void) unlink(f);
440 warn("can't create gid: short write to `%s'", f);
441 return 0;
442 }
443 }
444 (void) fprintf(to, "%s:*:%d:%s\n", group, gid, name);
445 (void) fclose(from);
446 (void) fclose(to);
447 if (rename(f, _PATH_GROUP) < 0) {
448 warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
449 return 0;
450 }
451 (void) chmod(_PATH_GROUP, st.st_mode & 07777);
452 return 1;
453 }
454
455 /* modify the group entry with name `group' to be newent */
456 static int
457 modify_gid(char *group, char *newent)
458 {
459 struct stat st;
460 FILE *from;
461 FILE *to;
462 char buf[MaxEntryLen];
463 char f[MaxFileNameLen];
464 char *colon;
465 int groupc;
466 int entc;
467 int fd;
468 int cc;
469
470 if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
471 warn("can't create gid for %s: can't open %s", group, _PATH_GROUP);
472 return 0;
473 }
474 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
475 warn("can't lock `%s'", _PATH_GROUP);
476 }
477 (void) fstat(fileno(from), &st);
478 (void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
479 if ((fd = mkstemp(f)) < 0) {
480 (void) fclose(from);
481 warn("can't create gid: mkstemp failed");
482 return 0;
483 }
484 if ((to = fdopen(fd, "w")) == NULL) {
485 (void) fclose(from);
486 (void) close(fd);
487 (void) unlink(f);
488 warn("can't create gid: fdopen `%s' failed", f);
489 return 0;
490 }
491 groupc = strlen(group);
492 while (fgets(buf, sizeof(buf), from) != NULL) {
493 cc = strlen(buf);
494 if ((colon = strchr(buf, ':')) == NULL) {
495 warn("badly formed entry `%s'", buf);
496 continue;
497 }
498 entc = (int)(colon - buf);
499 if (entc == groupc && strncmp(group, buf, (unsigned) entc) == 0) {
500 if (newent == NULL) {
501 continue;
502 } else {
503 cc = strlen(newent);
504 (void) strlcpy(buf, newent, sizeof(buf));
505 }
506 }
507 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
508 (void) fclose(from);
509 (void) close(fd);
510 (void) unlink(f);
511 warn("can't create gid: short write to `%s'", f);
512 return 0;
513 }
514 }
515 (void) fclose(from);
516 (void) fclose(to);
517 if (rename(f, _PATH_GROUP) < 0) {
518 warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
519 return 0;
520 }
521 (void) chmod(_PATH_GROUP, st.st_mode & 07777);
522 return 1;
523 }
524
525 /* modify the group entries for all `groups', by adding `user' */
526 static int
527 append_group(char *user, int ngroups, char **groups)
528 {
529 struct group *grp;
530 struct stat st;
531 FILE *from;
532 FILE *to;
533 char buf[MaxEntryLen];
534 char f[MaxFileNameLen];
535 char *colon;
536 int groupc;
537 int entc;
538 int fd;
539 int nc;
540 int cc;
541 int i;
542 int j;
543
544 for (i = 0 ; i < ngroups ; i++) {
545 if ((grp = getgrnam(groups[i])) == NULL) {
546 warnx("can't append group `%s' for user `%s'", groups[i], user);
547 } else {
548 for (j = 0 ; grp->gr_mem[j] ; j++) {
549 if (strcmp(user, grp->gr_mem[j]) == 0) {
550 /* already in it */
551 groups[i] = "";
552 }
553 }
554 }
555 }
556 if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
557 warn("can't append group for %s: can't open %s", user, _PATH_GROUP);
558 return 0;
559 }
560 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
561 warn("can't lock `%s'", _PATH_GROUP);
562 }
563 (void) fstat(fileno(from), &st);
564 (void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
565 if ((fd = mkstemp(f)) < 0) {
566 (void) fclose(from);
567 warn("can't create gid: mkstemp failed");
568 return 0;
569 }
570 if ((to = fdopen(fd, "w")) == NULL) {
571 (void) fclose(from);
572 (void) close(fd);
573 (void) unlink(f);
574 warn("can't create gid: fdopen `%s' failed", f);
575 return 0;
576 }
577 while (fgets(buf, sizeof(buf), from) != NULL) {
578 cc = strlen(buf);
579 if ((colon = strchr(buf, ':')) == NULL) {
580 warn("badly formed entry `%s'", buf);
581 continue;
582 }
583 entc = (int)(colon - buf);
584 for (i = 0 ; i < ngroups ; i++) {
585 if ((groupc = strlen(groups[i])) == 0) {
586 continue;
587 }
588 if (entc == groupc && strncmp(groups[i], buf, (unsigned) entc) == 0) {
589 if ((nc = snprintf(&buf[cc - 1],
590 sizeof(buf) - cc + 1,
591 "%s%s\n",
592 (buf[cc - 2] == ':') ? "" : ",",
593 user)) < 0) {
594 warnx("Warning: group `%s' entry too long", groups[i]);
595 }
596 cc += nc - 1;
597 }
598 }
599 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
600 (void) fclose(from);
601 (void) close(fd);
602 (void) unlink(f);
603 warn("can't create gid: short write to `%s'", f);
604 return 0;
605 }
606 }
607 (void) fclose(from);
608 (void) fclose(to);
609 if (rename(f, _PATH_GROUP) < 0) {
610 warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
611 return 0;
612 }
613 (void) chmod(_PATH_GROUP, st.st_mode & 07777);
614 return 1;
615 }
616
617 /* return 1 if `login' is a valid login name */
618 static int
619 valid_login(char *login)
620 {
621 char *cp;
622
623 for (cp = login ; *cp ; cp++) {
624 if (!isalnum(*cp) && *cp != '.' && *cp != '_' && *cp != '-') {
625 return 0;
626 }
627 }
628 return 1;
629 }
630
631 /* return 1 if `group' is a valid group name */
632 static int
633 valid_group(char *group)
634 {
635 char *cp;
636
637 for (cp = group ; *cp ; cp++) {
638 if (!isalnum(*cp)) {
639 return 0;
640 }
641 }
642 return 1;
643 }
644
645 /* find the next gid in the range lo .. hi */
646 static int
647 getnextgid(int *gidp, int lo, int hi)
648 {
649 for (*gidp = lo ; *gidp < hi ; *gidp += 1) {
650 if (getgrgid((gid_t)*gidp) == NULL) {
651 return 1;
652 }
653 }
654 return 0;
655 }
656
657 #ifdef EXTENSIONS
658 /* save a range of uids */
659 static int
660 save_range(user_t *up, char *cp)
661 {
662 int from;
663 int to;
664 int i;
665
666 if (up->u_rsize == 0) {
667 up->u_rsize = 32;
668 NEWARRAY(range_t, up->u_rv, up->u_rsize, return(0));
669 } else if (up->u_rc == up->u_rsize) {
670 up->u_rsize *= 2;
671 RENEW(range_t, up->u_rv, up->u_rsize, return(0));
672 }
673 if (up->u_rv && sscanf(cp, "%d..%d", &from, &to) == 2) {
674 for (i = 0 ; i < up->u_rc ; i++) {
675 if (up->u_rv[i].r_from == from && up->u_rv[i].r_to == to) {
676 break;
677 }
678 }
679 if (i == up->u_rc) {
680 up->u_rv[up->u_rc].r_from = from;
681 up->u_rv[up->u_rc].r_to = to;
682 up->u_rc += 1;
683 }
684 } else {
685 warnx("Bad range `%s'", cp);
686 return 0;
687 }
688 return 1;
689 }
690 #endif
691
692 /* set the defaults in the defaults file */
693 static int
694 setdefaults(user_t *up)
695 {
696 char template[MaxFileNameLen];
697 FILE *fp;
698 int ret;
699 int fd;
700 int i;
701
702 (void) snprintf(template, sizeof(template), "%s.XXXXXX", CONFFILE);
703 if ((fd = mkstemp(template)) < 0) {
704 warnx("can't mkstemp `%s' for writing", CONFFILE);
705 return 0;
706 }
707 if ((fp = fdopen(fd, "w")) == NULL) {
708 warn("can't fdopen `%s' for writing", CONFFILE);
709 return 0;
710 }
711 ret = 1;
712 if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 ||
713 fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 ||
714 fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 ||
715 fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 ||
716 fprintf(fp, "inactive\t%d\n", up->u_inactive) <= 0 ||
717 fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ? UNSET_EXPIRY : up->u_expire) <= 0 ||
718 fprintf(fp, "preserve\t%s\n", (up->u_preserve == 0) ? "false" : "true") <= 0) {
719 warn("can't write to `%s'", CONFFILE);
720 ret = 0;
721 }
722 #ifdef EXTENSIONS
723 for (i = (up->u_defrc != up->u_rc) ? up->u_defrc : 0 ; i < up->u_rc ; i++) {
724 if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from, up->u_rv[i].r_to) <= 0) {
725 warn("can't write to `%s'", CONFFILE);
726 ret = 0;
727 }
728 }
729 #endif
730 (void) fclose(fp);
731 if (ret) {
732 ret = ((rename(template, CONFFILE) == 0) && (chmod(CONFFILE, 0644) == 0));
733 }
734 return ret;
735 }
736
737 /* read the defaults file */
738 static void
739 read_defaults(user_t *up)
740 {
741 struct stat st;
742 size_t lineno;
743 size_t len;
744 FILE *fp;
745 char *cp;
746 char *s;
747
748 memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP));
749 memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR));
750 memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR));
751 memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL));
752 memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT));
753 up->u_rsize = 16;
754 NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1));
755 up->u_inactive = DEF_INACTIVE;
756 up->u_expire = DEF_EXPIRE;
757 if ((fp = fopen(CONFFILE, "r")) == NULL) {
758 if (stat(CONFFILE, &st) < 0 && !setdefaults(up)) {
759 warn("can't create `%s' defaults file", CONFFILE);
760 }
761 fp = fopen(CONFFILE, "r");
762 }
763 if (fp != NULL) {
764 while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) {
765 if (strncmp(s, "group", 5) == 0) {
766 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
767 }
768 memsave(&up->u_primgrp, cp, strlen(cp));
769 } else if (strncmp(s, "base_dir", 8) == 0) {
770 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
771 }
772 memsave(&up->u_basedir, cp, strlen(cp));
773 } else if (strncmp(s, "skel_dir", 8) == 0) {
774 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
775 }
776 memsave(&up->u_skeldir, cp, strlen(cp));
777 } else if (strncmp(s, "shell", 5) == 0) {
778 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
779 }
780 memsave(&up->u_shell, cp, strlen(cp));
781 } else if (strncmp(s, "inactive", 8) == 0) {
782 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
783 }
784 up->u_inactive = atoi(cp);
785 #ifdef EXTENSIONS
786 } else if (strncmp(s, "range", 5) == 0) {
787 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
788 }
789 (void) save_range(up, cp);
790 #endif
791 #ifdef EXTENSIONS
792 } else if (strncmp(s, "preserve", 8) == 0) {
793 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
794 }
795 up->u_preserve = (strncmp(cp, "true", 4) == 0) ? 1 :
796 (strncmp(cp, "yes", 3) == 0) ? 1 :
797 atoi(cp);
798 #endif
799 } else if (strncmp(s, "expire", 6) == 0) {
800 for (cp = s + 6 ; *cp && isspace(*cp) ; cp++) {
801 }
802 if (strcmp(cp, UNSET_EXPIRY) == 0) {
803 if (up->u_expire) {
804 FREE(up->u_expire);
805 }
806 up->u_expire = NULL;
807 } else {
808 memsave(&up->u_expire, cp, strlen(cp));
809 }
810 }
811 (void) free(s);
812 }
813 (void) fclose(fp);
814 }
815 if (up->u_rc == 0) {
816 up->u_rv[up->u_rc].r_from = DEF_LOWUID;
817 up->u_rv[up->u_rc].r_to = DEF_HIGHUID;
818 up->u_rc += 1;
819 }
820 up->u_defrc = up->u_rc;
821 }
822
823 /* return the next valid unused uid */
824 static int
825 getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid)
826 {
827 for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) {
828 if (getpwuid((uid_t)(*uid)) == NULL && *uid != NOBODY_UID) {
829 if (sync_uid_gid) {
830 if (getgrgid((gid_t)(*uid)) == NULL) {
831 return 1;
832 }
833 } else {
834 return 1;
835 }
836 }
837 }
838 return 0;
839 }
840
841 /* add a user */
842 static int
843 adduser(char *login, user_t *up)
844 {
845 struct group *grp;
846 struct stat st;
847 struct tm tm;
848 time_t expire;
849 char password[PasswordLength + 1];
850 char home[MaxFileNameLen];
851 char buf[MaxFileNameLen];
852 int sync_uid_gid;
853 int masterfd;
854 int ptmpfd;
855 int gid;
856 int cc;
857 int i;
858
859 if (!valid_login(login)) {
860 errx(EXIT_FAILURE, "`%s' is not a valid login name", login);
861 }
862 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
863 err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD);
864 }
865 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
866 err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD);
867 }
868 pw_init();
869 if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
870 (void) close(masterfd);
871 err(EXIT_FAILURE, "can't obtain pw_lock");
872 }
873 while ((cc = read(masterfd, buf, sizeof(buf))) > 0) {
874 if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
875 (void) close(masterfd);
876 (void) close(ptmpfd);
877 (void) pw_abort();
878 err(EXIT_FAILURE, "short write to /etc/ptmp (not %d chars)", cc);
879 }
880 }
881 /* if no uid was specified, get next one in [low_uid..high_uid] range */
882 sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0);
883 if (up->u_uid == -1) {
884 for (i = 0 ; i < up->u_rc ; i++) {
885 if (getnextuid(sync_uid_gid, &up->u_uid, up->u_rv[i].r_from, up->u_rv[i].r_to)) {
886 break;
887 }
888 }
889 if (i == up->u_rc) {
890 (void) close(ptmpfd);
891 (void) pw_abort();
892 errx(EXIT_FAILURE, "can't get next uid for %d", up->u_uid);
893 }
894 }
895 /* check uid isn't already allocated */
896 if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) {
897 (void) close(ptmpfd);
898 (void) pw_abort();
899 errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid);
900 }
901 /* if -g=uid was specified, check gid is unused */
902 if (sync_uid_gid) {
903 if (getgrgid((gid_t)(up->u_uid)) != NULL) {
904 (void) close(ptmpfd);
905 (void) pw_abort();
906 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
907 }
908 gid = up->u_uid;
909 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) {
910 gid = grp->gr_gid;
911 } else if (is_number(up->u_primgrp) &&
912 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) {
913 gid = grp->gr_gid;
914 } else {
915 (void) close(ptmpfd);
916 (void) pw_abort();
917 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
918 }
919 /* check name isn't already in use */
920 if (!(up->u_flags & F_DUPUID) && getpwnam(login) != NULL) {
921 (void) close(ptmpfd);
922 (void) pw_abort();
923 errx(EXIT_FAILURE, "already a `%s' user", login);
924 }
925 if (up->u_flags & F_HOMEDIR) {
926 (void) strlcpy(home, up->u_home, sizeof(home));
927 } else {
928 /* if home directory hasn't been given, make it up */
929 (void) snprintf(home, sizeof(home), "%s/%s", up->u_basedir, login);
930 }
931 expire = 0;
932 if (up->u_expire != NULL) {
933 (void) memset(&tm, 0, sizeof(tm));
934 if (strptime(up->u_expire, "%c", &tm) == NULL) {
935 warnx("invalid time format `%s'", optarg);
936 } else {
937 expire = mktime(&tm);
938 }
939 }
940 if (lstat(home, &st) < 0 && !(up->u_flags & F_MKDIR)) {
941 warnx("Warning: home directory `%s' doesn't exist, and -m was not specified",
942 home);
943 }
944 password[PasswordLength] = '\0';
945 if (up->u_password != NULL &&
946 strlen(up->u_password) == PasswordLength) {
947 (void) memcpy(password, up->u_password, PasswordLength);
948 } else {
949 (void) memset(password, '*', PasswordLength);
950 if (up->u_password != NULL) {
951 warnx("Password `%s' is invalid: setting it to `%s'",
952 up->u_password, password);
953 }
954 }
955 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d::%d:%ld:%s:%s:%s\n",
956 login,
957 password,
958 up->u_uid,
959 gid,
960 up->u_inactive,
961 (long) expire,
962 up->u_comment,
963 home,
964 up->u_shell);
965 if (write(ptmpfd, buf, (size_t) cc) != cc) {
966 (void) close(ptmpfd);
967 (void) pw_abort();
968 err(EXIT_FAILURE, "can't add `%s'", buf);
969 }
970 if (up->u_flags & F_MKDIR) {
971 if (lstat(home, &st) < 0 && asystem("%s -p %s", MKDIR, home) != 0) {
972 (void) close(ptmpfd);
973 (void) pw_abort();
974 err(EXIT_FAILURE, "can't mkdir `%s'", home);
975 }
976 (void) copydotfiles(up->u_skeldir, up->u_uid, gid, home);
977 }
978 if (strcmp(up->u_primgrp, "=uid") == 0 &&
979 getgrnam(login) == NULL &&
980 !creategid(login, gid, login)) {
981 (void) close(ptmpfd);
982 (void) pw_abort();
983 err(EXIT_FAILURE, "can't create gid %d for login name %s", gid, login);
984 }
985 if (up->u_groupc > 0 && !append_group(login, up->u_groupc, up->u_groupv)) {
986 (void) close(ptmpfd);
987 (void) pw_abort();
988 errx(EXIT_FAILURE, "can't append `%s' to new groups", login);
989 }
990 (void) close(ptmpfd);
991 if (pw_mkdb() < 0) {
992 err(EXIT_FAILURE, "pw_mkdb failed");
993 }
994 return 1;
995 }
996
997 /* modify a user */
998 static int
999 moduser(char *login, char *newlogin, user_t *up)
1000 {
1001 struct passwd *pwp, *newpwp;
1002 struct group *grp;
1003 struct tm tm;
1004 size_t colonc, len, loginc;
1005 FILE *master;
1006 char *buf, *colon, *line;
1007 int masterfd;
1008 int ptmpfd;
1009
1010 if (!valid_login(newlogin)) {
1011 errx(EXIT_FAILURE, "`%s' is not a valid login name", login);
1012 }
1013 if ((pwp = getpwnam(login)) == NULL) {
1014 errx(EXIT_FAILURE, "No such user `%s'", login);
1015 }
1016 newpwp = pwp;
1017 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
1018 err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD);
1019 }
1020 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
1021 err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD);
1022 }
1023 pw_init();
1024 if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
1025 (void) close(masterfd);
1026 err(EXIT_FAILURE, "can't obtain pw_lock");
1027 }
1028 if ((master = fdopen(masterfd, "r")) == NULL) {
1029 (void) close(masterfd);
1030 (void) close(ptmpfd);
1031 (void) pw_abort();
1032 err(EXIT_FAILURE, "can't fdopen fd for %s", _PATH_MASTERPASSWD);
1033 }
1034 if (up != NULL) {
1035 if (up->u_flags & F_USERNAME) {
1036 /* if changing name, check new name isn't already in use */
1037 if (strcmp(login, newlogin) != 0 && getpwnam(newlogin) != NULL) {
1038 (void) close(ptmpfd);
1039 (void) pw_abort();
1040 errx(EXIT_FAILURE, "already a `%s' user", newlogin);
1041 }
1042 newpwp->pw_name = newlogin;
1043 }
1044 if (up->u_flags & F_PASSWORD) {
1045 if (up->u_password != NULL && strlen(up->u_password) == PasswordLength)
1046 newpwp->pw_passwd = up->u_password;
1047 }
1048 if (up->u_flags & F_UID) {
1049 /* check uid isn't already allocated */
1050 if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) {
1051 (void) close(ptmpfd);
1052 (void) pw_abort();
1053 errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid);
1054 }
1055 newpwp->pw_uid = up->u_uid;
1056 }
1057 if (up->u_flags & F_GROUP) {
1058 /* if -g=uid was specified, check gid is unused */
1059 if (strcmp(up->u_primgrp, "=uid") == 0) {
1060 if (getgrgid((gid_t)(up->u_uid)) != NULL) {
1061 (void) close(ptmpfd);
1062 (void) pw_abort();
1063 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
1064 }
1065 newpwp->pw_gid = up->u_uid;
1066 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) {
1067 newpwp->pw_gid = grp->gr_gid;
1068 } else if (is_number(up->u_primgrp) &&
1069 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) {
1070 newpwp->pw_gid = grp->gr_gid;
1071 } else {
1072 (void) close(ptmpfd);
1073 (void) pw_abort();
1074 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
1075 }
1076 }
1077 if (up->u_flags |= F_INACTIVE)
1078 newpwp->pw_change = up->u_inactive;
1079 if (up->u_flags & F_EXPIRE) {
1080 (void) memset(&tm, 0, sizeof(tm));
1081 if (strptime(up->u_expire, "%c", &tm) == NULL)
1082 warnx("invalid time format `%s'", optarg);
1083 else
1084 newpwp->pw_expire = mktime(&tm);
1085 }
1086 if (up->u_flags & F_COMMENT)
1087 newpwp->pw_gecos = up->u_comment;
1088 if (up->u_flags & F_HOMEDIR)
1089 newpwp->pw_dir = up->u_home;
1090 if (up->u_flags & F_SHELL)
1091 newpwp->pw_shell = up->u_shell;
1092 }
1093 loginc = strlen(login);
1094 while ((line = fgetln(master, &len)) != NULL) {
1095 if ((colon = strchr(line, ':')) == NULL) {
1096 warnx("Malformed entry `%s'. Skipping", line);
1097 continue;
1098 }
1099 colonc = (size_t)(colon - line);
1100 if (strncmp(login, line, loginc) == 0 && loginc == colonc) {
1101 if (up != NULL) {
1102 len = (int)asprintf(&buf, "%s:%s:%d:%d::%d:%ld:%s:%s:%s\n",
1103 newlogin,
1104 newpwp->pw_passwd,
1105 newpwp->pw_uid,
1106 newpwp->pw_gid,
1107 newpwp->pw_change,
1108 (long)newpwp->pw_expire,
1109 newpwp->pw_gecos,
1110 newpwp->pw_dir,
1111 newpwp->pw_shell);
1112 if (write(ptmpfd, buf, len) != len) {
1113 (void) close(ptmpfd);
1114 (void) pw_abort();
1115 err(EXIT_FAILURE, "can't add `%s'", buf);
1116 }
1117 }
1118 /* } else if (write(ptmpfd, line, len) != len) { */
1119 } else if ((colonc = write(ptmpfd, line, len)) != len) {
1120 (void) close(masterfd);
1121 (void) close(ptmpfd);
1122 (void) pw_abort();
1123 err(EXIT_FAILURE, "short write to /etc/ptmp (%lld not %lld chars)",
1124 (long long)colonc,
1125 (long long)len);
1126 }
1127 }
1128 if (up != NULL) {
1129 if ((up->u_flags & F_MKDIR) &&
1130 asystem("%s %s %s", MV, pwp->pw_dir, newpwp->pw_dir) != 0) {
1131 (void) close(ptmpfd);
1132 (void) pw_abort();
1133 err(EXIT_FAILURE, "can't move `%s' to `%s'",
1134 pwp->pw_dir, newpwp->pw_dir);
1135 }
1136 if (up->u_groupc > 0 &&
1137 !append_group(newlogin, up->u_groupc, up->u_groupv)) {
1138 (void) close(ptmpfd);
1139 (void) pw_abort();
1140 errx(EXIT_FAILURE, "can't append `%s' to new groups",
1141 newlogin);
1142 }
1143 }
1144 (void) close(ptmpfd);
1145 if (pw_mkdb() < 0) {
1146 err(EXIT_FAILURE, "pw_mkdb failed");
1147 }
1148 return 1;
1149 }
1150
1151
1152 #ifdef EXTENSIONS
1153 /* see if we can find out the user struct */
1154 static struct passwd *
1155 find_user_info(char *name)
1156 {
1157 struct passwd *pwp;
1158
1159 if ((pwp = getpwnam(name)) != NULL) {
1160 return pwp;
1161 }
1162 if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != NULL) {
1163 return pwp;
1164 }
1165 return NULL;
1166 }
1167 #endif
1168
1169 #ifdef EXTENSIONS
1170 /* see if we can find out the group struct */
1171 static struct group *
1172 find_group_info(char *name)
1173 {
1174 struct group *grp;
1175
1176 if ((grp = getgrnam(name)) != NULL) {
1177 return grp;
1178 }
1179 if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != NULL) {
1180 return grp;
1181 }
1182 return NULL;
1183 }
1184 #endif
1185
1186 /* print out usage message, and then exit */
1187 void
1188 usermgmt_usage(char *prog)
1189 {
1190 if (strcmp(prog, "useradd") == 0) {
1191 (void) fprintf(stderr, "Usage: %s -D [-b basedir] [-e expiry] "
1192 "[-f inactive] [-g group] [-r lowuid..highuid] [-s shell]"
1193 "\n", prog);
1194 (void) fprintf(stderr, "Usage: %s [-G group] [-b basedir] "
1195 "[-c comment] [-d homedir] [-e expiry] [-f inactive]\n"
1196 "\t[-g group] [-k skeletondir] [-m] [-o] [-p password] "
1197 "[-r lowuid..highuid] [-s shell]\n\t[-u uid] [-v] user\n",
1198 prog);
1199 } else if (strcmp(prog, "usermod") == 0) {
1200 (void) fprintf(stderr, "Usage: %s [-G group] [-c comment] "
1201 "[-d homedir] [-e expire] [-f inactive] [-g group] "
1202 "[-l newname] [-m] [-o] [-p password] [-s shell] [-u uid] "
1203 "[-v] user\n", prog);
1204 } else if (strcmp(prog, "userdel") == 0) {
1205 (void) fprintf(stderr, "Usage: %s -D [-p preserve]\n", prog);
1206 (void) fprintf(stderr, "Usage: %s [-p preserve] [-r] [-v] "
1207 "user\n", prog);
1208 #ifdef EXTENSIONS
1209 } else if (strcmp(prog, "userinfo") == 0) {
1210 (void) fprintf(stderr, "Usage: %s [-e] [-v] user\n", prog);
1211 #endif
1212 } else if (strcmp(prog, "groupadd") == 0) {
1213 (void) fprintf(stderr, "Usage: %s [-g gid] [-o] [-v] group\n",
1214 prog);
1215 } else if (strcmp(prog, "groupdel") == 0) {
1216 (void) fprintf(stderr, "Usage: %s [-v] group\n", prog);
1217 } else if (strcmp(prog, "groupmod") == 0) {
1218 (void) fprintf(stderr, "Usage: %s [-g gid] [-o] [-n newname] "
1219 "[-v] group\n", prog);
1220 } else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) {
1221 (void) fprintf(stderr,
1222 "Usage: %s ( add | del | mod | info ) ...\n",
1223 prog);
1224 #ifdef EXTENSIONS
1225 } else if (strcmp(prog, "groupinfo") == 0) {
1226 (void) fprintf(stderr, "Usage: %s [-e] [-v] group\n", prog);
1227 #endif
1228 }
1229 exit(EXIT_FAILURE);
1230 /* NOTREACHED */
1231 }
1232
1233 #ifdef EXTENSIONS
1234 #define ADD_OPT_EXTENSIONS "p:r:v"
1235 #else
1236 #define ADD_OPT_EXTENSIONS
1237 #endif
1238
1239 int
1240 useradd(int argc, char **argv)
1241 {
1242 user_t u;
1243 int defaultfield;
1244 int bigD;
1245 int c;
1246 int i;
1247
1248 (void) memset(&u, 0, sizeof(u));
1249 read_defaults(&u);
1250 u.u_uid = -1;
1251 defaultfield = bigD = 0;
1252 while ((c = getopt(argc, argv, "DG:b:c:d:e:f:g:k:mou:s:" ADD_OPT_EXTENSIONS)) != -1) {
1253 switch(c) {
1254 case 'D':
1255 bigD = 1;
1256 break;
1257 case 'G':
1258 while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
1259 u.u_groupc < NGROUPS_MAX) {
1260 if (u.u_groupv[u.u_groupc][0] != 0) {
1261 u.u_groupc++;
1262 }
1263 }
1264 if (optarg != NULL) {
1265 warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX);
1266 }
1267 break;
1268 case 'b':
1269 defaultfield = 1;
1270 memsave(&u.u_basedir, optarg, strlen(optarg));
1271 break;
1272 case 'c':
1273 memsave(&u.u_comment, optarg, strlen(optarg));
1274 break;
1275 case 'd':
1276 memsave(&u.u_home, optarg, strlen(optarg));
1277 u.u_flags |= (F_MKDIR | F_HOMEDIR);
1278 break;
1279 case 'e':
1280 defaultfield = 1;
1281 memsave(&u.u_expire, optarg, strlen(optarg));
1282 break;
1283 case 'f':
1284 defaultfield = 1;
1285 u.u_inactive = atoi(optarg);
1286 break;
1287 case 'g':
1288 defaultfield = 1;
1289 memsave(&u.u_primgrp, optarg, strlen(optarg));
1290 break;
1291 case 'k':
1292 memsave(&u.u_skeldir, optarg, strlen(optarg));
1293 break;
1294 case 'm':
1295 u.u_flags |= F_MKDIR;
1296 break;
1297 case 'o':
1298 u.u_flags |= F_DUPUID;
1299 break;
1300 #ifdef EXTENSIONS
1301 case 'p':
1302 memsave(&u.u_password, optarg, strlen(optarg));
1303 break;
1304 #endif
1305 #ifdef EXTENSIONS
1306 case 'r':
1307 defaultfield = 1;
1308 (void) save_range(&u, optarg);
1309 break;
1310 #endif
1311 case 's':
1312 defaultfield = 1;
1313 memsave(&u.u_shell, optarg, strlen(optarg));
1314 break;
1315 case 'u':
1316 if (!is_number(optarg)) {
1317 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
1318 }
1319 u.u_uid = atoi(optarg);
1320 break;
1321 #ifdef EXTENSIONS
1322 case 'v':
1323 verbose = 1;
1324 break;
1325 #endif
1326 }
1327 }
1328 if (bigD) {
1329 if (defaultfield) {
1330 checkeuid();
1331 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1332 }
1333 (void) printf("group\t\t%s\n", u.u_primgrp);
1334 (void) printf("base_dir\t%s\n", u.u_basedir);
1335 (void) printf("skel_dir\t%s\n", u.u_skeldir);
1336 (void) printf("shell\t\t%s\n", u.u_shell);
1337 (void) printf("inactive\t%d\n", u.u_inactive);
1338 (void) printf("expire\t\t%s\n", (u.u_expire == NULL) ? UNSET_EXPIRY : u.u_expire);
1339 #ifdef EXTENSIONS
1340 for (i = 0 ; i < u.u_rc ; i++) {
1341 (void) printf("range\t\t%d..%d\n", u.u_rv[i].r_from, u.u_rv[i].r_to);
1342 }
1343 #endif
1344 return EXIT_SUCCESS;
1345 }
1346 if (argc == optind) {
1347 usermgmt_usage("useradd");
1348 }
1349 checkeuid();
1350 return adduser(argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1351 }
1352
1353 #ifdef EXTENSIONS
1354 #define MOD_OPT_EXTENSIONS "p:v"
1355 #else
1356 #define MOD_OPT_EXTENSIONS
1357 #endif
1358
1359 int
1360 usermod(int argc, char **argv)
1361 {
1362 user_t u;
1363 char newuser[MaxUserNameLen + 1];
1364 int c, have_new_user;
1365
1366 (void) memset(&u, 0, sizeof(u));
1367 (void) memset(newuser, 0, sizeof(newuser));
1368 read_defaults(&u);
1369 have_new_user = 0;
1370 while ((c = getopt(argc, argv, "G:c:d:e:f:g:l:mos:u:" MOD_OPT_EXTENSIONS)) != -1) {
1371 switch(c) {
1372 case 'G':
1373 while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
1374 u.u_groupc < NGROUPS_MAX) {
1375 if (u.u_groupv[u.u_groupc][0] != 0) {
1376 u.u_groupc++;
1377 }
1378 }
1379 if (optarg != NULL) {
1380 warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX);
1381 }
1382 u.u_flags |= F_SECGROUP;
1383 break;
1384 case 'c':
1385 memsave(&u.u_comment, optarg, strlen(optarg));
1386 u.u_flags |= F_COMMENT;
1387 break;
1388 case 'd':
1389 memsave(&u.u_home, optarg, strlen(optarg));
1390 u.u_flags |= (F_MKDIR | F_HOMEDIR);
1391 break;
1392 case 'e':
1393 memsave(&u.u_expire, optarg, strlen(optarg));
1394 u.u_flags |= F_EXPIRE;
1395 break;
1396 case 'f':
1397 u.u_inactive = atoi(optarg);
1398 u.u_flags |= F_INACTIVE;
1399 break;
1400 case 'g':
1401 memsave(&u.u_primgrp, optarg, strlen(optarg));
1402 u.u_flags |= F_GROUP;
1403 break;
1404 case 'l':
1405 (void) strlcpy(newuser, optarg, sizeof(newuser));
1406 have_new_user = 1;
1407 u.u_flags |= F_USERNAME;
1408 break;
1409 case 'm':
1410 u.u_flags |= F_MKDIR;
1411 break;
1412 case 'o':
1413 u.u_flags |= F_DUPUID;
1414 break;
1415 #ifdef EXTENSIONS
1416 case 'p':
1417 memsave(&u.u_password, optarg, strlen(optarg));
1418 u.u_flags |= F_PASSWORD;
1419 break;
1420 #endif
1421 case 's':
1422 memsave(&u.u_shell, optarg, strlen(optarg));
1423 u.u_flags |= F_SHELL;
1424 break;
1425 case 'u':
1426 if (!is_number(optarg)) {
1427 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
1428 }
1429 u.u_uid = atoi(optarg);
1430 u.u_flags |= F_UID;
1431 break;
1432 #ifdef EXTENSIONS
1433 case 'v':
1434 verbose = 1;
1435 break;
1436 #endif
1437 }
1438 }
1439 if (argc == optind) {
1440 usermgmt_usage("usermod");
1441 }
1442 checkeuid();
1443 return moduser(argv[optind], (have_new_user) ? newuser : argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1444 }
1445
1446 #ifdef EXTENSIONS
1447 #define DEL_OPT_EXTENSIONS "Dp:v"
1448 #else
1449 #define DEL_OPT_EXTENSIONS
1450 #endif
1451
1452 int
1453 userdel(int argc, char **argv)
1454 {
1455 struct passwd *pwp;
1456 user_t u;
1457 char password[PasswordLength + 1];
1458 int defaultfield;
1459 int rmhome;
1460 int bigD;
1461 int c;
1462
1463 (void) memset(&u, 0, sizeof(u));
1464 read_defaults(&u);
1465 defaultfield = bigD = rmhome = 0;
1466 while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) {
1467 switch(c) {
1468 #ifdef EXTENSIONS
1469 case 'D':
1470 bigD = 1;
1471 break;
1472 #endif
1473 #ifdef EXTENSIONS
1474 case 'p':
1475 defaultfield = 1;
1476 u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 :
1477 (strcmp(optarg, "yes") == 0) ? 1 :
1478 atoi(optarg);
1479 break;
1480 #endif
1481 case 'r':
1482 rmhome = 1;
1483 break;
1484 #ifdef EXTENSIONS
1485 case 'v':
1486 verbose = 1;
1487 break;
1488 #endif
1489 }
1490 }
1491 #ifdef EXTENSIONS
1492 if (bigD) {
1493 if (defaultfield) {
1494 checkeuid();
1495 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1496 }
1497 (void) printf("preserve\t%s\n", (u.u_preserve) ? "true" : "false");
1498 return EXIT_SUCCESS;
1499 }
1500 #endif
1501 if (argc == optind) {
1502 usermgmt_usage("userdel");
1503 }
1504 checkeuid();
1505 if ((pwp = getpwnam(argv[optind])) == NULL) {
1506 warnx("No such user `%s'", argv[optind]);
1507 return EXIT_FAILURE;
1508 }
1509 if (rmhome)
1510 (void)removehomedir(pwp->pw_name, pwp->pw_uid, pwp->pw_dir);
1511 if (u.u_preserve) {
1512 memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN));
1513 (void) memset(password, '*', PasswordLength);
1514 password[PasswordLength] = '\0';
1515 memsave(&u.u_password, password, PasswordLength);
1516 u.u_flags |= F_PASSWORD;
1517 return moduser(argv[optind], argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1518 }
1519 return moduser(argv[optind], argv[optind], NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
1520 }
1521
1522 #ifdef EXTENSIONS
1523 #define GROUP_ADD_OPT_EXTENSIONS "v"
1524 #else
1525 #define GROUP_ADD_OPT_EXTENSIONS
1526 #endif
1527
1528 /* add a group */
1529 int
1530 groupadd(int argc, char **argv)
1531 {
1532 int dupgid;
1533 int gid;
1534 int c;
1535
1536 gid = -1;
1537 dupgid = 0;
1538 while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) {
1539 switch(c) {
1540 case 'g':
1541 if (!is_number(optarg)) {
1542 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
1543 }
1544 gid = atoi(optarg);
1545 break;
1546 case 'o':
1547 dupgid = 1;
1548 break;
1549 #ifdef EXTENSIONS
1550 case 'v':
1551 verbose = 1;
1552 break;
1553 #endif
1554 }
1555 }
1556 if (argc == optind) {
1557 usermgmt_usage("groupadd");
1558 }
1559 checkeuid();
1560 if (gid < 0 && !getnextgid(&gid, LowGid, HighGid)) {
1561 err(EXIT_FAILURE, "can't add group: can't get next gid");
1562 }
1563 if (!dupgid && getgrgid((gid_t) gid) != NULL) {
1564 errx(EXIT_FAILURE, "can't add group: gid %d is a duplicate", gid);
1565 }
1566 if (!valid_group(argv[optind])) {
1567 warnx("warning - invalid group name `%s'", argv[optind]);
1568 }
1569 if (!creategid(argv[optind], gid, "")) {
1570 err(EXIT_FAILURE, "can't add group: problems with %s file", _PATH_GROUP);
1571 }
1572 return EXIT_SUCCESS;
1573 }
1574
1575 #ifdef EXTENSIONS
1576 #define GROUP_DEL_OPT_EXTENSIONS "v"
1577 #else
1578 #define GROUP_DEL_OPT_EXTENSIONS
1579 #endif
1580
1581 /* remove a group */
1582 int
1583 groupdel(int argc, char **argv)
1584 {
1585 int c;
1586
1587 while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) {
1588 switch(c) {
1589 #ifdef EXTENSIONS
1590 case 'v':
1591 verbose = 1;
1592 break;
1593 #endif
1594 }
1595 }
1596 if (argc == optind) {
1597 usermgmt_usage("groupdel");
1598 }
1599 checkeuid();
1600 if (!modify_gid(argv[optind], NULL)) {
1601 err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP);
1602 }
1603 return EXIT_SUCCESS;
1604 }
1605
1606 #ifdef EXTENSIONS
1607 #define GROUP_MOD_OPT_EXTENSIONS "v"
1608 #else
1609 #define GROUP_MOD_OPT_EXTENSIONS
1610 #endif
1611
1612 /* modify a group */
1613 int
1614 groupmod(int argc, char **argv)
1615 {
1616 struct group *grp;
1617 char buf[MaxEntryLen];
1618 char *newname;
1619 char **cpp;
1620 int dupgid;
1621 int gid;
1622 int cc;
1623 int c;
1624
1625 gid = -1;
1626 dupgid = 0;
1627 newname = NULL;
1628 while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) {
1629 switch(c) {
1630 case 'g':
1631 if (!is_number(optarg)) {
1632 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
1633 }
1634 gid = atoi(optarg);
1635 break;
1636 case 'o':
1637 dupgid = 1;
1638 break;
1639 case 'n':
1640 memsave(&newname, optarg, strlen(optarg));
1641 break;
1642 #ifdef EXTENSIONS
1643 case 'v':
1644 verbose = 1;
1645 break;
1646 #endif
1647 }
1648 }
1649 if (argc == optind) {
1650 usermgmt_usage("groupmod");
1651 }
1652 checkeuid();
1653 if (gid < 0 && newname == NULL) {
1654 err(EXIT_FAILURE, "Nothing to change");
1655 }
1656 if (dupgid && gid < 0) {
1657 err(EXIT_FAILURE, "Duplicate which gid?");
1658 }
1659 if ((grp = getgrnam(argv[optind])) == NULL) {
1660 err(EXIT_FAILURE, "can't find group `%s' to modify", argv[optind]);
1661 }
1662 if (newname != NULL && !valid_group(newname)) {
1663 warn("warning - invalid group name `%s'", newname);
1664 }
1665 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:",
1666 (newname) ? newname : grp->gr_name,
1667 grp->gr_passwd,
1668 (gid < 0) ? grp->gr_gid : gid);
1669 for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) {
1670 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp,
1671 (cpp[1] == NULL) ? "" : ",");
1672 }
1673 if (!modify_gid(argv[optind], buf)) {
1674 err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP);
1675 }
1676 return EXIT_SUCCESS;
1677 }
1678
1679 #ifdef EXTENSIONS
1680 /* display user information */
1681 int
1682 userinfo(int argc, char **argv)
1683 {
1684 struct passwd *pwp;
1685 struct group *grp;
1686 char buf[MaxEntryLen];
1687 char **cpp;
1688 int exists;
1689 int cc;
1690 int i;
1691
1692 exists = 0;
1693 while ((i = getopt(argc, argv, "ev")) != -1) {
1694 switch(i) {
1695 case 'e':
1696 exists = 1;
1697 break;
1698 case 'v':
1699 verbose = 1;
1700 break;
1701 }
1702 }
1703 if (argc == optind) {
1704 usermgmt_usage("userinfo");
1705 }
1706 pwp = find_user_info(argv[optind]);
1707 if (exists) {
1708 exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE);
1709 }
1710 if (pwp == NULL) {
1711 errx(EXIT_FAILURE, "can't find user `%s'", argv[optind]);
1712 }
1713 (void) printf("login\t%s\n", pwp->pw_name);
1714 (void) printf("passwd\t%s\n", pwp->pw_passwd);
1715 (void) printf("uid\t%d\n", pwp->pw_uid);
1716 for (cc = 0 ; (grp = getgrent()) != NULL ; ) {
1717 for (cpp = grp->gr_mem ; *cpp ; cpp++) {
1718 if (strcmp(*cpp, argv[optind]) == 0 && grp->gr_gid != pwp->pw_gid) {
1719 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s ", grp->gr_name);
1720 }
1721 }
1722 }
1723 if ((grp = getgrgid(pwp->pw_gid)) == NULL) {
1724 (void) printf("groups\t%d %s\n", pwp->pw_gid, buf);
1725 } else {
1726 (void) printf("groups\t%s %s\n", grp->gr_name, buf);
1727 }
1728 (void) printf("change\t%s", ctime(&pwp->pw_change));
1729 (void) printf("class\t%s\n", pwp->pw_class);
1730 (void) printf("gecos\t%s\n", pwp->pw_gecos);
1731 (void) printf("dir\t%s\n", pwp->pw_dir);
1732 (void) printf("shell\t%s\n", pwp->pw_shell);
1733 (void) printf("expire\t%s", ctime(&pwp->pw_expire));
1734 return EXIT_SUCCESS;
1735 }
1736 #endif
1737
1738 #ifdef EXTENSIONS
1739 /* display user information */
1740 int
1741 groupinfo(int argc, char **argv)
1742 {
1743 struct group *grp;
1744 char **cpp;
1745 int exists;
1746 int i;
1747
1748 exists = 0;
1749 while ((i = getopt(argc, argv, "ev")) != -1) {
1750 switch(i) {
1751 case 'e':
1752 exists = 1;
1753 break;
1754 case 'v':
1755 verbose = 1;
1756 break;
1757 }
1758 }
1759 if (argc == optind) {
1760 usermgmt_usage("groupinfo");
1761 }
1762 grp = find_group_info(argv[optind]);
1763 if (exists) {
1764 exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE);
1765 }
1766 if (grp == NULL) {
1767 errx(EXIT_FAILURE, "can't find group `%s'", argv[optind]);
1768 }
1769 (void) printf("name\t%s\n", grp->gr_name);
1770 (void) printf("passwd\t%s\n", grp->gr_passwd);
1771 (void) printf("gid\t%d\n", grp->gr_gid);
1772 (void) printf("members\t");
1773 for (cpp = grp->gr_mem ; *cpp ; cpp++) {
1774 (void) printf("%s ", *cpp);
1775 }
1776 (void) fputc('\n', stdout);
1777 return EXIT_SUCCESS;
1778 }
1779 #endif
1780