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