user.c revision 1.48 1 /* $NetBSD: user.c,v 1.48 2002/05/03 08:07:02 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.48 2002/05/03 08:07:02 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 = 0 ; 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 NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1));
770 up->u_inactive = DEF_INACTIVE;
771 up->u_expire = DEF_EXPIRE;
772 if ((fp = fopen(CONFFILE, "r")) == NULL) {
773 if (stat(CONFFILE, &st) < 0 && !setdefaults(up)) {
774 warn("can't create `%s' defaults file", CONFFILE);
775 }
776 fp = fopen(CONFFILE, "r");
777 }
778 if (fp != NULL) {
779 while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) {
780 if (strncmp(s, "group", 5) == 0) {
781 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
782 }
783 memsave(&up->u_primgrp, cp, strlen(cp));
784 } else if (strncmp(s, "base_dir", 8) == 0) {
785 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
786 }
787 memsave(&up->u_basedir, cp, strlen(cp));
788 } else if (strncmp(s, "skel_dir", 8) == 0) {
789 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
790 }
791 memsave(&up->u_skeldir, cp, strlen(cp));
792 } else if (strncmp(s, "shell", 5) == 0) {
793 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
794 }
795 memsave(&up->u_shell, cp, strlen(cp));
796 #ifdef EXTENSIONS
797 } else if (strncmp(s, "class", 5) == 0) {
798 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
799 }
800 memsave(&up->u_class, cp, strlen(cp));
801 #endif
802 } else if (strncmp(s, "inactive", 8) == 0) {
803 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
804 }
805 up->u_inactive = atoi(cp);
806 #ifdef EXTENSIONS
807 } else if (strncmp(s, "range", 5) == 0) {
808 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
809 }
810 (void) save_range(up, cp);
811 #endif
812 #ifdef EXTENSIONS
813 } else if (strncmp(s, "preserve", 8) == 0) {
814 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
815 }
816 up->u_preserve = (strncmp(cp, "true", 4) == 0) ? 1 :
817 (strncmp(cp, "yes", 3) == 0) ? 1 :
818 atoi(cp);
819 #endif
820 } else if (strncmp(s, "expire", 6) == 0) {
821 for (cp = s + 6 ; *cp && isspace(*cp) ; cp++) {
822 }
823 if (strcmp(cp, UNSET_EXPIRY) == 0) {
824 if (up->u_expire) {
825 FREE(up->u_expire);
826 }
827 up->u_expire = NULL;
828 } else {
829 memsave(&up->u_expire, cp, strlen(cp));
830 }
831 }
832 (void) free(s);
833 }
834 (void) fclose(fp);
835 }
836 if (up->u_rc == 0) {
837 up->u_rv[up->u_rc].r_from = DEF_LOWUID;
838 up->u_rv[up->u_rc].r_to = DEF_HIGHUID;
839 up->u_rc += 1;
840 }
841 up->u_defrc = up->u_rc;
842 }
843
844 /* return the next valid unused uid */
845 static int
846 getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid)
847 {
848 for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) {
849 if (getpwuid((uid_t)(*uid)) == NULL && *uid != NOBODY_UID) {
850 if (sync_uid_gid) {
851 if (getgrgid((gid_t)(*uid)) == NULL) {
852 return 1;
853 }
854 } else {
855 return 1;
856 }
857 }
858 }
859 return 0;
860 }
861
862 /* add a user */
863 static int
864 adduser(char *login, user_t *up)
865 {
866 struct group *grp;
867 struct stat st;
868 struct tm tm;
869 time_t expire;
870 char password[PasswordLength + 1];
871 char home[MaxFileNameLen];
872 char buf[MaxFileNameLen];
873 int sync_uid_gid;
874 int masterfd;
875 int ptmpfd;
876 int gid;
877 int cc;
878 int i;
879
880 if (!valid_login(login)) {
881 errx(EXIT_FAILURE, "`%s' is not a valid login name", login);
882 }
883 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
884 err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD);
885 }
886 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
887 err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD);
888 }
889 pw_init();
890 if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
891 (void) close(masterfd);
892 err(EXIT_FAILURE, "can't obtain pw_lock");
893 }
894 while ((cc = read(masterfd, buf, sizeof(buf))) > 0) {
895 if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
896 (void) close(masterfd);
897 (void) close(ptmpfd);
898 (void) pw_abort();
899 err(EXIT_FAILURE, "short write to /etc/ptmp (not %d chars)", cc);
900 }
901 }
902 /* if no uid was specified, get next one in [low_uid..high_uid] range */
903 sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0);
904 if (up->u_uid == -1) {
905 /* default is in index '0' in u_rv array */
906 for (i = 1 ; i < up->u_rc ; i++) {
907 if (getnextuid(sync_uid_gid, &up->u_uid, up->u_rv[i].r_from, up->u_rv[i].r_to)) {
908 break;
909 }
910 }
911 if (i == up->u_rc &&
912 !getnextuid(sync_uid_gid, &up->u_uid, up->u_rv[0].r_from, up->u_rv[0].r_to)) {
913 (void) close(ptmpfd);
914 (void) pw_abort();
915 errx(EXIT_FAILURE, "can't get next uid for %d", up->u_uid);
916 }
917 }
918 /* check uid isn't already allocated */
919 if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) {
920 (void) close(ptmpfd);
921 (void) pw_abort();
922 errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid);
923 }
924 /* if -g=uid was specified, check gid is unused */
925 if (sync_uid_gid) {
926 if (getgrgid((gid_t)(up->u_uid)) != NULL) {
927 (void) close(ptmpfd);
928 (void) pw_abort();
929 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
930 }
931 gid = up->u_uid;
932 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) {
933 gid = grp->gr_gid;
934 } else if (is_number(up->u_primgrp) &&
935 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) {
936 gid = grp->gr_gid;
937 } else {
938 (void) close(ptmpfd);
939 (void) pw_abort();
940 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
941 }
942 /* check name isn't already in use */
943 if (!(up->u_flags & F_DUPUID) && getpwnam(login) != NULL) {
944 (void) close(ptmpfd);
945 (void) pw_abort();
946 errx(EXIT_FAILURE, "already a `%s' user", login);
947 }
948 if (up->u_flags & F_HOMEDIR) {
949 (void) strlcpy(home, up->u_home, sizeof(home));
950 } else {
951 /* if home directory hasn't been given, make it up */
952 (void) snprintf(home, sizeof(home), "%s/%s", up->u_basedir, login);
953 }
954 expire = 0;
955 if (up->u_expire != NULL) {
956 (void) memset(&tm, 0, sizeof(tm));
957 if (strptime(up->u_expire, "%c", &tm) == NULL) {
958 warnx("invalid time format `%s'", optarg);
959 } else {
960 expire = mktime(&tm);
961 }
962 }
963 if (lstat(home, &st) < 0 && !(up->u_flags & F_MKDIR)) {
964 warnx("Warning: home directory `%s' doesn't exist, and -m was not specified",
965 home);
966 }
967 password[PasswordLength] = '\0';
968 if (up->u_password != NULL &&
969 strlen(up->u_password) == PasswordLength) {
970 (void) memcpy(password, up->u_password, PasswordLength);
971 } else {
972 (void) memset(password, '*', PasswordLength);
973 if (up->u_password != NULL) {
974 warnx("Password `%s' is invalid: setting it to `%s'",
975 up->u_password, password);
976 }
977 }
978 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:%s:%d:%ld:%s:%s:%s\n",
979 login,
980 password,
981 up->u_uid,
982 gid,
983 #ifdef EXTENSIONS
984 up->u_class,
985 #else
986 "",
987 #endif
988 up->u_inactive,
989 (long) expire,
990 up->u_comment,
991 home,
992 up->u_shell);
993 if (write(ptmpfd, buf, (size_t) cc) != cc) {
994 (void) close(ptmpfd);
995 (void) pw_abort();
996 err(EXIT_FAILURE, "can't add `%s'", buf);
997 }
998 if (up->u_flags & F_MKDIR) {
999 if (lstat(home, &st) == 0) {
1000 (void) close(ptmpfd);
1001 (void) pw_abort();
1002 errx(EXIT_FAILURE, "home directory `%s' already exists", home);
1003 } else {
1004 if (asystem("%s -p %s", MKDIR, home) != 0) {
1005 (void) close(ptmpfd);
1006 (void) pw_abort();
1007 err(EXIT_FAILURE, "can't mkdir `%s'", home);
1008 }
1009 (void) copydotfiles(up->u_skeldir, up->u_uid, gid, home);
1010 }
1011 }
1012 if (strcmp(up->u_primgrp, "=uid") == 0 &&
1013 getgrnam(login) == NULL &&
1014 !creategid(login, gid, login)) {
1015 (void) close(ptmpfd);
1016 (void) pw_abort();
1017 errx(EXIT_FAILURE, "can't create gid %d for login name %s", gid, login);
1018 }
1019 if (up->u_groupc > 0 && !append_group(login, up->u_groupc, up->u_groupv)) {
1020 (void) close(ptmpfd);
1021 (void) pw_abort();
1022 errx(EXIT_FAILURE, "can't append `%s' to new groups", login);
1023 }
1024 (void) close(ptmpfd);
1025 #if PW_MKDB_ARGC == 2
1026 if (pw_mkdb(login, 0) < 0) {
1027 err(EXIT_FAILURE, "pw_mkdb failed");
1028 }
1029 #else
1030 if (pw_mkdb() < 0) {
1031 err(EXIT_FAILURE, "pw_mkdb failed");
1032 }
1033 #endif
1034 return 1;
1035 }
1036
1037 /* modify a user */
1038 static int
1039 moduser(char *login, char *newlogin, user_t *up)
1040 {
1041 struct passwd *pwp;
1042 struct group *grp;
1043 struct tm tm;
1044 const char *homedir;
1045 size_t colonc, len, loginc;
1046 size_t cc;
1047 FILE *master;
1048 char newdir[MaxFileNameLen];
1049 char *buf, *colon, *line;
1050 int masterfd;
1051 int ptmpfd;
1052 int error;
1053
1054 if (!valid_login(newlogin)) {
1055 errx(EXIT_FAILURE, "`%s' is not a valid login name", login);
1056 }
1057 if ((pwp = getpwnam(login)) == NULL) {
1058 errx(EXIT_FAILURE, "No such user `%s'", login);
1059 }
1060 /* keep dir name in case we need it for '-m' */
1061 homedir = pwp->pw_dir;
1062
1063 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
1064 err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD);
1065 }
1066 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
1067 err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD);
1068 }
1069 pw_init();
1070 if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
1071 (void) close(masterfd);
1072 err(EXIT_FAILURE, "can't obtain pw_lock");
1073 }
1074 if ((master = fdopen(masterfd, "r")) == NULL) {
1075 (void) close(masterfd);
1076 (void) close(ptmpfd);
1077 (void) pw_abort();
1078 err(EXIT_FAILURE, "can't fdopen fd for %s", _PATH_MASTERPASSWD);
1079 }
1080 if (up != NULL) {
1081 if (up->u_flags & F_USERNAME) {
1082 /* if changing name, check new name isn't already in use */
1083 if (strcmp(login, newlogin) != 0 && getpwnam(newlogin) != NULL) {
1084 (void) close(ptmpfd);
1085 (void) pw_abort();
1086 errx(EXIT_FAILURE, "already a `%s' user", newlogin);
1087 }
1088 pwp->pw_name = newlogin;
1089
1090 /*
1091 * Provide a new directory name in case the
1092 * home directory is to be moved.
1093 */
1094 if (up->u_flags & F_MKDIR) {
1095 snprintf(newdir, sizeof(newdir), "%s/%s", up->u_basedir, newlogin);
1096 pwp->pw_dir = newdir;
1097 }
1098 }
1099 if (up->u_flags & F_PASSWORD) {
1100 if (up->u_password != NULL && strlen(up->u_password) == PasswordLength)
1101 pwp->pw_passwd = up->u_password;
1102 }
1103 if (up->u_flags & F_UID) {
1104 /* check uid isn't already allocated */
1105 if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) {
1106 (void) close(ptmpfd);
1107 (void) pw_abort();
1108 errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid);
1109 }
1110 pwp->pw_uid = up->u_uid;
1111 }
1112 if (up->u_flags & F_GROUP) {
1113 /* if -g=uid was specified, check gid is unused */
1114 if (strcmp(up->u_primgrp, "=uid") == 0) {
1115 if (getgrgid((gid_t)(up->u_uid)) != NULL) {
1116 (void) close(ptmpfd);
1117 (void) pw_abort();
1118 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
1119 }
1120 pwp->pw_gid = up->u_uid;
1121 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) {
1122 pwp->pw_gid = grp->gr_gid;
1123 } else if (is_number(up->u_primgrp) &&
1124 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) {
1125 pwp->pw_gid = grp->gr_gid;
1126 } else {
1127 (void) close(ptmpfd);
1128 (void) pw_abort();
1129 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
1130 }
1131 }
1132 if (up->u_flags |= F_INACTIVE)
1133 pwp->pw_change = up->u_inactive;
1134 if (up->u_flags & F_EXPIRE) {
1135 (void) memset(&tm, 0, sizeof(tm));
1136 if (strptime(up->u_expire, "%c", &tm) == NULL)
1137 warnx("invalid time format `%s'", optarg);
1138 else
1139 pwp->pw_expire = mktime(&tm);
1140 }
1141 if (up->u_flags & F_COMMENT)
1142 pwp->pw_gecos = up->u_comment;
1143 if (up->u_flags & F_HOMEDIR)
1144 pwp->pw_dir = up->u_home;
1145 if (up->u_flags & F_SHELL)
1146 pwp->pw_shell = up->u_shell;
1147 #ifdef EXTENSIONS
1148 if (up->u_flags & F_CLASS)
1149 pwp->pw_class = up->u_class;
1150 #endif
1151 }
1152 loginc = strlen(login);
1153 while ((line = fgetln(master, &len)) != NULL) {
1154 if ((colon = strchr(line, ':')) == NULL) {
1155 warnx("Malformed entry `%s'. Skipping", line);
1156 continue;
1157 }
1158 colonc = (size_t)(colon - line);
1159 if (strncmp(login, line, loginc) == 0 && loginc == colonc) {
1160 if (up != NULL) {
1161 len = (int)asprintf(&buf, "%s:%s:%d:%d:"
1162 #ifdef EXTENSIONS
1163 "%s"
1164 #endif
1165 ":%ld:%ld:%s:%s:%s\n",
1166 newlogin,
1167 pwp->pw_passwd,
1168 pwp->pw_uid,
1169 pwp->pw_gid,
1170 #ifdef EXTENSIONS
1171 pwp->pw_class,
1172 #endif
1173 (long)pwp->pw_change,
1174 (long)pwp->pw_expire,
1175 pwp->pw_gecos,
1176 pwp->pw_dir,
1177 pwp->pw_shell);
1178 if (write(ptmpfd, buf, len) != len) {
1179 (void) close(ptmpfd);
1180 (void) pw_abort();
1181 err(EXIT_FAILURE, "can't add `%s'", buf);
1182 }
1183 (void) free(buf);
1184 }
1185 } else if ((cc = write(ptmpfd, line, len)) != len) {
1186 (void) close(masterfd);
1187 (void) close(ptmpfd);
1188 (void) pw_abort();
1189 err(EXIT_FAILURE, "short write to /etc/ptmp (%lld not %lld chars)",
1190 (long long)cc,
1191 (long long)len);
1192 }
1193 }
1194 if (up != NULL) {
1195 if ((up->u_flags & F_MKDIR) &&
1196 asystem("%s %s %s", MV, homedir, pwp->pw_dir) != 0) {
1197 (void) close(ptmpfd);
1198 (void) pw_abort();
1199 err(EXIT_FAILURE, "can't move `%s' to `%s'",
1200 homedir, pwp->pw_dir);
1201 }
1202 if (up->u_groupc > 0 &&
1203 !append_group(newlogin, up->u_groupc, up->u_groupv)) {
1204 (void) close(ptmpfd);
1205 (void) pw_abort();
1206 errx(EXIT_FAILURE, "can't append `%s' to new groups",
1207 newlogin);
1208 }
1209 }
1210 (void) close(ptmpfd);
1211 #if PW_MKDB_ARGC == 2
1212 if (up != NULL && strcmp(login, newlogin) == 0) {
1213 error = pw_mkdb(login, 0);
1214 } else {
1215 error = pw_mkdb(NULL, 0);
1216 }
1217 #else
1218 error = pw_mkdb();
1219 #endif
1220 if (error < 0) {
1221 err(EXIT_FAILURE, "pw_mkdb failed");
1222 }
1223
1224 return 1;
1225 }
1226
1227
1228 #ifdef EXTENSIONS
1229 /* see if we can find out the user struct */
1230 static struct passwd *
1231 find_user_info(char *name)
1232 {
1233 struct passwd *pwp;
1234
1235 if ((pwp = getpwnam(name)) != NULL) {
1236 return pwp;
1237 }
1238 if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != NULL) {
1239 return pwp;
1240 }
1241 return NULL;
1242 }
1243 #endif
1244
1245 #ifdef EXTENSIONS
1246 /* see if we can find out the group struct */
1247 static struct group *
1248 find_group_info(char *name)
1249 {
1250 struct group *grp;
1251
1252 if ((grp = getgrnam(name)) != NULL) {
1253 return grp;
1254 }
1255 if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != NULL) {
1256 return grp;
1257 }
1258 return NULL;
1259 }
1260 #endif
1261
1262 /* print out usage message, and then exit */
1263 void
1264 usermgmt_usage(const char *prog)
1265 {
1266 if (strcmp(prog, "useradd") == 0) {
1267 (void) fprintf(stderr, "Usage: %s -D [-b basedir] [-e expiry] "
1268 "[-f inactive] [-g group]\n\t[-r lowuid..highuid] "
1269 "[-s shell] [-L class]\n", prog);
1270 (void) fprintf(stderr, "Usage: %s [-G group] [-b basedir] "
1271 "[-c comment] [-d homedir] [-e expiry]\n\t[-f inactive] "
1272 "[-g group] [-k skeletondir] [-m] [-o] [-p password]\n"
1273 "\t[-r lowuid..highuid] [-s shell]\n\t[-u uid] [-v] user\n",
1274 prog);
1275 } else if (strcmp(prog, "usermod") == 0) {
1276 (void) fprintf(stderr, "Usage: %s [-G group] [-c comment] "
1277 "[-d homedir] [-e expire] [-f inactive]\n\t[-g group] "
1278 "[-l newname] [-m] [-o] [-p password] [-s shell] [-u uid]\n"
1279 "\t[-L class] [-v] user\n", prog);
1280 } else if (strcmp(prog, "userdel") == 0) {
1281 (void) fprintf(stderr, "Usage: %s -D [-p preserve]\n", prog);
1282 (void) fprintf(stderr,
1283 "Usage: %s [-p preserve] [-r] [-v] user\n", prog);
1284 #ifdef EXTENSIONS
1285 } else if (strcmp(prog, "userinfo") == 0) {
1286 (void) fprintf(stderr, "Usage: %s [-e] [-v] user\n", prog);
1287 #endif
1288 } else if (strcmp(prog, "groupadd") == 0) {
1289 (void) fprintf(stderr, "Usage: %s [-g gid] [-o] [-v] group\n",
1290 prog);
1291 } else if (strcmp(prog, "groupdel") == 0) {
1292 (void) fprintf(stderr, "Usage: %s [-v] group\n", prog);
1293 } else if (strcmp(prog, "groupmod") == 0) {
1294 (void) fprintf(stderr,
1295 "Usage: %s [-g gid] [-o] [-n newname] [-v] group\n", prog);
1296 } else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) {
1297 (void) fprintf(stderr,
1298 "Usage: %s ( add | del | mod | info ) ...\n", prog);
1299 #ifdef EXTENSIONS
1300 } else if (strcmp(prog, "groupinfo") == 0) {
1301 (void) fprintf(stderr, "Usage: %s [-e] [-v] group\n", prog);
1302 #endif
1303 }
1304 exit(EXIT_FAILURE);
1305 /* NOTREACHED */
1306 }
1307
1308 #ifdef EXTENSIONS
1309 #define ADD_OPT_EXTENSIONS "p:r:vL:"
1310 #else
1311 #define ADD_OPT_EXTENSIONS
1312 #endif
1313
1314 int
1315 useradd(int argc, char **argv)
1316 {
1317 user_t u;
1318 int defaultfield;
1319 int bigD;
1320 int c;
1321 #ifdef EXTENSIONS
1322 int i;
1323 #endif
1324
1325 (void) memset(&u, 0, sizeof(u));
1326 read_defaults(&u);
1327 u.u_uid = -1;
1328 defaultfield = bigD = 0;
1329 while ((c = getopt(argc, argv, "DG:b:c:d:e:f:g:k:mou:s:" ADD_OPT_EXTENSIONS)) != -1) {
1330 switch(c) {
1331 case 'D':
1332 bigD = 1;
1333 break;
1334 case 'G':
1335 while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
1336 u.u_groupc < NGROUPS_MAX) {
1337 if (u.u_groupv[u.u_groupc][0] != 0) {
1338 u.u_groupc++;
1339 }
1340 }
1341 if (optarg != NULL) {
1342 warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX);
1343 }
1344 break;
1345 case 'b':
1346 defaultfield = 1;
1347 memsave(&u.u_basedir, optarg, strlen(optarg));
1348 break;
1349 case 'c':
1350 memsave(&u.u_comment, optarg, strlen(optarg));
1351 break;
1352 case 'd':
1353 memsave(&u.u_home, optarg, strlen(optarg));
1354 u.u_flags |= F_HOMEDIR;
1355 break;
1356 case 'e':
1357 defaultfield = 1;
1358 memsave(&u.u_expire, optarg, strlen(optarg));
1359 break;
1360 case 'f':
1361 defaultfield = 1;
1362 u.u_inactive = atoi(optarg);
1363 break;
1364 case 'g':
1365 defaultfield = 1;
1366 memsave(&u.u_primgrp, optarg, strlen(optarg));
1367 break;
1368 case 'k':
1369 memsave(&u.u_skeldir, optarg, strlen(optarg));
1370 break;
1371 #ifdef EXTENSIONS
1372 case 'L':
1373 defaultfield = 1;
1374 memsave(&u.u_class, optarg, strlen(optarg));
1375 break;
1376 #endif
1377 case 'm':
1378 u.u_flags |= F_MKDIR;
1379 break;
1380 case 'o':
1381 u.u_flags |= F_DUPUID;
1382 break;
1383 #ifdef EXTENSIONS
1384 case 'p':
1385 memsave(&u.u_password, optarg, strlen(optarg));
1386 break;
1387 #endif
1388 #ifdef EXTENSIONS
1389 case 'r':
1390 defaultfield = 1;
1391 (void) save_range(&u, optarg);
1392 break;
1393 #endif
1394 case 's':
1395 defaultfield = 1;
1396 memsave(&u.u_shell, optarg, strlen(optarg));
1397 break;
1398 case 'u':
1399 if (!is_number(optarg)) {
1400 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
1401 }
1402 u.u_uid = atoi(optarg);
1403 break;
1404 #ifdef EXTENSIONS
1405 case 'v':
1406 verbose = 1;
1407 break;
1408 #endif
1409 }
1410 }
1411 if (bigD) {
1412 if (defaultfield) {
1413 checkeuid();
1414 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1415 }
1416 (void) printf("group\t\t%s\n", u.u_primgrp);
1417 (void) printf("base_dir\t%s\n", u.u_basedir);
1418 (void) printf("skel_dir\t%s\n", u.u_skeldir);
1419 (void) printf("shell\t\t%s\n", u.u_shell);
1420 #ifdef EXTENSIONS
1421 (void) printf("class\t\t%s\n", u.u_class);
1422 #endif
1423 (void) printf("inactive\t%d\n", u.u_inactive);
1424 (void) printf("expire\t\t%s\n", (u.u_expire == NULL) ? UNSET_EXPIRY : u.u_expire);
1425 #ifdef EXTENSIONS
1426 for (i = 0 ; i < u.u_rc ; i++) {
1427 (void) printf("range\t\t%d..%d\n", u.u_rv[i].r_from, u.u_rv[i].r_to);
1428 }
1429 #endif
1430 return EXIT_SUCCESS;
1431 }
1432 argc -= optind;
1433 argv += optind;
1434 if (argc != 1) {
1435 usermgmt_usage("useradd");
1436 }
1437 checkeuid();
1438 return adduser(*argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1439 }
1440
1441 #ifdef EXTENSIONS
1442 #define MOD_OPT_EXTENSIONS "p:vL:"
1443 #else
1444 #define MOD_OPT_EXTENSIONS
1445 #endif
1446
1447 int
1448 usermod(int argc, char **argv)
1449 {
1450 user_t u;
1451 char newuser[MaxUserNameLen + 1];
1452 int c, have_new_user;
1453
1454 (void) memset(&u, 0, sizeof(u));
1455 (void) memset(newuser, 0, sizeof(newuser));
1456 read_defaults(&u);
1457 have_new_user = 0;
1458 while ((c = getopt(argc, argv, "G:c:d:e:f:g:l:mos:u:" MOD_OPT_EXTENSIONS)) != -1) {
1459 switch(c) {
1460 case 'G':
1461 while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
1462 u.u_groupc < NGROUPS_MAX) {
1463 if (u.u_groupv[u.u_groupc][0] != 0) {
1464 u.u_groupc++;
1465 }
1466 }
1467 if (optarg != NULL) {
1468 warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX);
1469 }
1470 u.u_flags |= F_SECGROUP;
1471 break;
1472 case 'c':
1473 memsave(&u.u_comment, optarg, strlen(optarg));
1474 u.u_flags |= F_COMMENT;
1475 break;
1476 case 'd':
1477 memsave(&u.u_home, optarg, strlen(optarg));
1478 u.u_flags |= F_HOMEDIR;
1479 break;
1480 case 'e':
1481 memsave(&u.u_expire, optarg, strlen(optarg));
1482 u.u_flags |= F_EXPIRE;
1483 break;
1484 case 'f':
1485 u.u_inactive = atoi(optarg);
1486 u.u_flags |= F_INACTIVE;
1487 break;
1488 case 'g':
1489 memsave(&u.u_primgrp, optarg, strlen(optarg));
1490 u.u_flags |= F_GROUP;
1491 break;
1492 case 'l':
1493 (void) strlcpy(newuser, optarg, sizeof(newuser));
1494 have_new_user = 1;
1495 u.u_flags |= F_USERNAME;
1496 break;
1497 #ifdef EXTENSIONS
1498 case 'L':
1499 memsave(&u.u_class, optarg, strlen(optarg));
1500 u.u_flags |= F_CLASS;
1501 break;
1502 #endif
1503 case 'm':
1504 u.u_flags |= F_MKDIR;
1505 break;
1506 case 'o':
1507 u.u_flags |= F_DUPUID;
1508 break;
1509 #ifdef EXTENSIONS
1510 case 'p':
1511 memsave(&u.u_password, optarg, strlen(optarg));
1512 u.u_flags |= F_PASSWORD;
1513 break;
1514 #endif
1515 case 's':
1516 memsave(&u.u_shell, optarg, strlen(optarg));
1517 u.u_flags |= F_SHELL;
1518 break;
1519 case 'u':
1520 if (!is_number(optarg)) {
1521 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
1522 }
1523 u.u_uid = atoi(optarg);
1524 u.u_flags |= F_UID;
1525 break;
1526 #ifdef EXTENSIONS
1527 case 'v':
1528 verbose = 1;
1529 break;
1530 #endif
1531 }
1532 }
1533 if ((u.u_flags & F_MKDIR) && !(u.u_flags & F_HOMEDIR) &&
1534 !(u.u_flags & F_USERNAME)) {
1535 warnx("option 'm' useless without 'd' or 'l' -- ignored");
1536 u.u_flags &= ~F_MKDIR;
1537 }
1538 argc -= optind;
1539 argv += optind;
1540 if (argc != 1) {
1541 usermgmt_usage("usermod");
1542 }
1543 checkeuid();
1544 return moduser(*argv, (have_new_user) ? newuser : *argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1545 }
1546
1547 #ifdef EXTENSIONS
1548 #define DEL_OPT_EXTENSIONS "Dp:v"
1549 #else
1550 #define DEL_OPT_EXTENSIONS
1551 #endif
1552
1553 int
1554 userdel(int argc, char **argv)
1555 {
1556 struct passwd *pwp;
1557 user_t u;
1558 char password[PasswordLength + 1];
1559 int defaultfield;
1560 int rmhome;
1561 int bigD;
1562 int c;
1563
1564 (void) memset(&u, 0, sizeof(u));
1565 read_defaults(&u);
1566 defaultfield = bigD = rmhome = 0;
1567 while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) {
1568 switch(c) {
1569 #ifdef EXTENSIONS
1570 case 'D':
1571 bigD = 1;
1572 break;
1573 #endif
1574 #ifdef EXTENSIONS
1575 case 'p':
1576 defaultfield = 1;
1577 u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 :
1578 (strcmp(optarg, "yes") == 0) ? 1 :
1579 atoi(optarg);
1580 break;
1581 #endif
1582 case 'r':
1583 rmhome = 1;
1584 break;
1585 #ifdef EXTENSIONS
1586 case 'v':
1587 verbose = 1;
1588 break;
1589 #endif
1590 }
1591 }
1592 #ifdef EXTENSIONS
1593 if (bigD) {
1594 if (defaultfield) {
1595 checkeuid();
1596 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1597 }
1598 (void) printf("preserve\t%s\n", (u.u_preserve) ? "true" : "false");
1599 return EXIT_SUCCESS;
1600 }
1601 #endif
1602 argc -= optind;
1603 argv += optind;
1604 if (argc != 1) {
1605 usermgmt_usage("userdel");
1606 }
1607 checkeuid();
1608 if ((pwp = getpwnam(*argv)) == NULL) {
1609 warnx("No such user `%s'", *argv);
1610 return EXIT_FAILURE;
1611 }
1612 if (rmhome)
1613 (void)removehomedir(pwp->pw_name, pwp->pw_uid, pwp->pw_dir);
1614 if (u.u_preserve) {
1615 u.u_flags |= F_SHELL;
1616 memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN));
1617 (void) memset(password, '*', PasswordLength);
1618 password[PasswordLength] = '\0';
1619 memsave(&u.u_password, password, PasswordLength);
1620 u.u_flags |= F_PASSWORD;
1621 return moduser(*argv, *argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1622 }
1623 return moduser(*argv, *argv, NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
1624 }
1625
1626 #ifdef EXTENSIONS
1627 #define GROUP_ADD_OPT_EXTENSIONS "v"
1628 #else
1629 #define GROUP_ADD_OPT_EXTENSIONS
1630 #endif
1631
1632 /* add a group */
1633 int
1634 groupadd(int argc, char **argv)
1635 {
1636 int dupgid;
1637 int gid;
1638 int c;
1639
1640 gid = -1;
1641 dupgid = 0;
1642 while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) {
1643 switch(c) {
1644 case 'g':
1645 if (!is_number(optarg)) {
1646 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
1647 }
1648 gid = atoi(optarg);
1649 break;
1650 case 'o':
1651 dupgid = 1;
1652 break;
1653 #ifdef EXTENSIONS
1654 case 'v':
1655 verbose = 1;
1656 break;
1657 #endif
1658 }
1659 }
1660 argc -= optind;
1661 argv += optind;
1662 if (argc != 1) {
1663 usermgmt_usage("groupadd");
1664 }
1665 checkeuid();
1666 if (gid < 0 && !getnextgid(&gid, LowGid, HighGid)) {
1667 err(EXIT_FAILURE, "can't add group: can't get next gid");
1668 }
1669 if (!dupgid && getgrgid((gid_t) gid) != NULL) {
1670 errx(EXIT_FAILURE, "can't add group: gid %d is a duplicate", gid);
1671 }
1672 if (!valid_group(*argv)) {
1673 warnx("warning - invalid group name `%s'", *argv);
1674 }
1675 if (!creategid(*argv, gid, "")) {
1676 errx(EXIT_FAILURE, "can't add group: problems with %s file", _PATH_GROUP);
1677 }
1678 return EXIT_SUCCESS;
1679 }
1680
1681 #ifdef EXTENSIONS
1682 #define GROUP_DEL_OPT_EXTENSIONS "v"
1683 #else
1684 #define GROUP_DEL_OPT_EXTENSIONS
1685 #endif
1686
1687 /* remove a group */
1688 int
1689 groupdel(int argc, char **argv)
1690 {
1691 int c;
1692
1693 while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) {
1694 switch(c) {
1695 #ifdef EXTENSIONS
1696 case 'v':
1697 verbose = 1;
1698 break;
1699 #endif
1700 }
1701 }
1702 argc -= optind;
1703 argv += optind;
1704 if (argc != 1) {
1705 usermgmt_usage("groupdel");
1706 }
1707 checkeuid();
1708 if (!modify_gid(*argv, NULL)) {
1709 err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP);
1710 }
1711 return EXIT_SUCCESS;
1712 }
1713
1714 #ifdef EXTENSIONS
1715 #define GROUP_MOD_OPT_EXTENSIONS "v"
1716 #else
1717 #define GROUP_MOD_OPT_EXTENSIONS
1718 #endif
1719
1720 /* modify a group */
1721 int
1722 groupmod(int argc, char **argv)
1723 {
1724 struct group *grp;
1725 char buf[MaxEntryLen];
1726 char *newname;
1727 char **cpp;
1728 int dupgid;
1729 int gid;
1730 int cc;
1731 int c;
1732
1733 gid = -1;
1734 dupgid = 0;
1735 newname = NULL;
1736 while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) {
1737 switch(c) {
1738 case 'g':
1739 if (!is_number(optarg)) {
1740 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
1741 }
1742 gid = atoi(optarg);
1743 break;
1744 case 'o':
1745 dupgid = 1;
1746 break;
1747 case 'n':
1748 memsave(&newname, optarg, strlen(optarg));
1749 break;
1750 #ifdef EXTENSIONS
1751 case 'v':
1752 verbose = 1;
1753 break;
1754 #endif
1755 }
1756 }
1757 argc -= optind;
1758 argv += optind;
1759 if (argc != 1) {
1760 usermgmt_usage("groupmod");
1761 }
1762 checkeuid();
1763 if (gid < 0 && newname == NULL) {
1764 err(EXIT_FAILURE, "Nothing to change");
1765 }
1766 if (dupgid && gid < 0) {
1767 err(EXIT_FAILURE, "Duplicate which gid?");
1768 }
1769 if ((grp = getgrnam(*argv)) == NULL) {
1770 err(EXIT_FAILURE, "can't find group `%s' to modify", *argv);
1771 }
1772 if (newname != NULL && !valid_group(newname)) {
1773 warn("warning - invalid group name `%s'", newname);
1774 }
1775 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:",
1776 (newname) ? newname : grp->gr_name,
1777 grp->gr_passwd,
1778 (gid < 0) ? grp->gr_gid : gid);
1779 for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) {
1780 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp,
1781 (cpp[1] == NULL) ? "" : ",");
1782 }
1783 cc += snprintf(&buf[cc], sizeof(buf) - cc, "\n");
1784 if (!modify_gid(*argv, buf)) {
1785 err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP);
1786 }
1787 return EXIT_SUCCESS;
1788 }
1789
1790 #ifdef EXTENSIONS
1791 /* display user information */
1792 int
1793 userinfo(int argc, char **argv)
1794 {
1795 struct passwd *pwp;
1796 struct group *grp;
1797 char buf[MaxEntryLen];
1798 char **cpp;
1799 int exists;
1800 int cc;
1801 int i;
1802
1803 exists = 0;
1804 while ((i = getopt(argc, argv, "ev")) != -1) {
1805 switch(i) {
1806 case 'e':
1807 exists = 1;
1808 break;
1809 case 'v':
1810 verbose = 1;
1811 break;
1812 }
1813 }
1814 argc -= optind;
1815 argv += optind;
1816 if (argc != 1) {
1817 usermgmt_usage("userinfo");
1818 }
1819 pwp = find_user_info(*argv);
1820 if (exists) {
1821 exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE);
1822 }
1823 if (pwp == NULL) {
1824 errx(EXIT_FAILURE, "can't find user `%s'", *argv);
1825 }
1826 (void) printf("login\t%s\n", pwp->pw_name);
1827 (void) printf("passwd\t%s\n", pwp->pw_passwd);
1828 (void) printf("uid\t%d\n", pwp->pw_uid);
1829 for (cc = 0 ; (grp = getgrent()) != NULL ; ) {
1830 for (cpp = grp->gr_mem ; *cpp ; cpp++) {
1831 if (strcmp(*cpp, *argv) == 0 && grp->gr_gid != pwp->pw_gid) {
1832 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s ", grp->gr_name);
1833 }
1834 }
1835 }
1836 if ((grp = getgrgid(pwp->pw_gid)) == NULL) {
1837 (void) printf("groups\t%d %s\n", pwp->pw_gid, buf);
1838 } else {
1839 (void) printf("groups\t%s %s\n", grp->gr_name, buf);
1840 }
1841 (void) printf("change\t%s", pwp->pw_change ? ctime(&pwp->pw_change) : "NEVER\n");
1842 #ifdef EXTENSIONS
1843 (void) printf("class\t%s\n", pwp->pw_class);
1844 #endif
1845 (void) printf("gecos\t%s\n", pwp->pw_gecos);
1846 (void) printf("dir\t%s\n", pwp->pw_dir);
1847 (void) printf("shell\t%s\n", pwp->pw_shell);
1848 (void) printf("expire\t%s", pwp->pw_expire ? ctime(&pwp->pw_expire) : "NEVER\n");
1849 return EXIT_SUCCESS;
1850 }
1851 #endif
1852
1853 #ifdef EXTENSIONS
1854 /* display user information */
1855 int
1856 groupinfo(int argc, char **argv)
1857 {
1858 struct group *grp;
1859 char **cpp;
1860 int exists;
1861 int i;
1862
1863 exists = 0;
1864 while ((i = getopt(argc, argv, "ev")) != -1) {
1865 switch(i) {
1866 case 'e':
1867 exists = 1;
1868 break;
1869 case 'v':
1870 verbose = 1;
1871 break;
1872 }
1873 }
1874 argc -= optind;
1875 argv += optind;
1876 if (argc != 1) {
1877 usermgmt_usage("groupinfo");
1878 }
1879 grp = find_group_info(*argv);
1880 if (exists) {
1881 exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE);
1882 }
1883 if (grp == NULL) {
1884 errx(EXIT_FAILURE, "can't find group `%s'", *argv);
1885 }
1886 (void) printf("name\t%s\n", grp->gr_name);
1887 (void) printf("passwd\t%s\n", grp->gr_passwd);
1888 (void) printf("gid\t%d\n", grp->gr_gid);
1889 (void) printf("members\t");
1890 for (cpp = grp->gr_mem ; *cpp ; cpp++) {
1891 (void) printf("%s ", *cpp);
1892 }
1893 (void) fputc('\n', stdout);
1894 return EXIT_SUCCESS;
1895 }
1896 #endif
1897