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