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