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