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