user.c revision 1.58 1 /* $NetBSD: user.c,v 1.58 2002/08/27 11:25:29 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.58 2002/08/27 11:25:29 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, '\0', PasswordLength);
1015 password[0] = '*';
1016 if (up->u_password != NULL) {
1017 warnx("Password `%s' is invalid: setting it to `%s'",
1018 up->u_password, password);
1019 }
1020 }
1021 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
1022 login_name,
1023 password,
1024 up->u_uid,
1025 gid,
1026 #ifdef EXTENSIONS
1027 up->u_class,
1028 #else
1029 "",
1030 #endif
1031 (long) inactive,
1032 (long) expire,
1033 up->u_comment,
1034 home,
1035 up->u_shell);
1036 if (write(ptmpfd, buf, (size_t) cc) != cc) {
1037 (void) close(ptmpfd);
1038 pw_abort();
1039 err(EXIT_FAILURE, "can't add `%s'", buf);
1040 }
1041 if (up->u_flags & F_MKDIR) {
1042 if (lstat(home, &st) == 0) {
1043 (void) close(ptmpfd);
1044 pw_abort();
1045 errx(EXIT_FAILURE, "home directory `%s' already exists", home);
1046 } else {
1047 if (asystem("%s -p %s", MKDIR, home) != 0) {
1048 (void) close(ptmpfd);
1049 pw_abort();
1050 err(EXIT_FAILURE, "can't mkdir `%s'", home);
1051 }
1052 (void) copydotfiles(up->u_skeldir, up->u_uid, gid, home);
1053 }
1054 }
1055 if (strcmp(up->u_primgrp, "=uid") == 0 &&
1056 getgrnam(login_name) == NULL &&
1057 !creategid(login_name, gid, login_name)) {
1058 (void) close(ptmpfd);
1059 pw_abort();
1060 errx(EXIT_FAILURE, "can't create gid %d for login name %s", gid, login_name);
1061 }
1062 if (up->u_groupc > 0 && !append_group(login_name, up->u_groupc, up->u_groupv)) {
1063 (void) close(ptmpfd);
1064 pw_abort();
1065 errx(EXIT_FAILURE, "can't append `%s' to new groups", login_name);
1066 }
1067 (void) close(ptmpfd);
1068 #if PW_MKDB_ARGC == 2
1069 if (pw_mkdb(login_name, 0) < 0) {
1070 pw_abort();
1071 err(EXIT_FAILURE, "pw_mkdb failed");
1072 }
1073 #else
1074 if (pw_mkdb() < 0) {
1075 pw_abort();
1076 err(EXIT_FAILURE, "pw_mkdb failed");
1077 }
1078 #endif
1079 return 1;
1080 }
1081
1082 /* remove a user from the groups file */
1083 static int
1084 rm_user_from_groups(char *login_name)
1085 {
1086 struct stat st;
1087 regmatch_t matchv[10];
1088 regex_t r;
1089 FILE *from;
1090 FILE *to;
1091 char line[MaxEntryLen];
1092 char buf[MaxEntryLen];
1093 char f[MaxFileNameLen];
1094 int fd;
1095 int cc;
1096 int sc;
1097
1098 (void) snprintf(line, sizeof(line), "(:|,)%s(,|\n|$)", login_name);
1099 if (regcomp(&r, line, REG_EXTENDED) != 0) {
1100 warn("can't compile regular expression `%s'", line);
1101 return 0;
1102 }
1103 if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
1104 warn("can't remove gid for `%s': can't open `%s'", login_name, _PATH_GROUP);
1105 return 0;
1106 }
1107 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
1108 warn("can't lock `%s'", _PATH_GROUP);
1109 }
1110 (void) fstat(fileno(from), &st);
1111 (void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
1112 if ((fd = mkstemp(f)) < 0) {
1113 (void) fclose(from);
1114 warn("can't create gid: mkstemp failed");
1115 return 0;
1116 }
1117 if ((to = fdopen(fd, "w")) == NULL) {
1118 (void) fclose(from);
1119 (void) close(fd);
1120 (void) unlink(f);
1121 warn("can't create gid: fdopen `%s' failed", f);
1122 return 0;
1123 }
1124 while (fgets(buf, sizeof(buf), from) > 0) {
1125 cc = strlen(buf);
1126 if (regexec(&r, buf, 10, matchv, 0) == 0) {
1127 cc -= (int)(matchv[0].rm_eo);
1128 sc = (int) matchv[0].rm_so;
1129 if (cc > 0) {
1130 sc += 1;
1131 }
1132 if (fwrite(buf, sizeof(char), sc, to) != sc ||
1133 fwrite(&buf[(int)matchv[0].rm_eo], sizeof(char), cc, to) != cc ||
1134 (buf[(int)matchv[0].rm_eo - 1] != ',' && fwrite("\n", sizeof(char), 1, to) != 1)) {
1135 (void) fclose(from);
1136 (void) close(fd);
1137 (void) unlink(f);
1138 warn("can't create gid: short write to `%s'", f);
1139 return 0;
1140 }
1141 } else if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
1142 (void) fclose(from);
1143 (void) close(fd);
1144 (void) unlink(f);
1145 warn("can't create gid: short write to `%s'", f);
1146 return 0;
1147 }
1148 }
1149 (void) fclose(from);
1150 (void) fclose(to);
1151 if (rename(f, _PATH_GROUP) < 0) {
1152 (void) unlink(f);
1153 warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
1154 return 0;
1155 }
1156 (void) chmod(_PATH_GROUP, st.st_mode & 07777);
1157 return 1;
1158 }
1159
1160 /* check that the user or group is local, not from YP/NIS */
1161 static int
1162 is_local(char *name, const char *file)
1163 {
1164 regmatch_t matchv[10];
1165 regex_t r;
1166 FILE *fp;
1167 char buf[MaxEntryLen];
1168 char re[MaxEntryLen];
1169 int ret;
1170
1171 (void) snprintf(re, sizeof(re), "^%s:", name);
1172 if (regcomp(&r, re, REG_EXTENDED) != 0) {
1173 errx(EXIT_FAILURE, "can't compile regular expression `%s'", re);
1174 }
1175 if ((fp = fopen(file, "r")) == NULL) {
1176 err(EXIT_FAILURE, "can't open `%s'", file);
1177 }
1178 for (ret = 0 ; fgets(buf, sizeof(buf), fp) != NULL ; ) {
1179 if (regexec(&r, buf, 10, matchv, 0) == 0) {
1180 ret = 1;
1181 break;
1182 }
1183 }
1184 (void) fclose(fp);
1185 return ret;
1186 }
1187
1188 /* modify a user */
1189 static int
1190 moduser(char *login_name, char *newlogin, user_t *up)
1191 {
1192 struct passwd *pwp;
1193 struct group *grp;
1194 const char *homedir;
1195 struct tm tm;
1196 size_t colonc;
1197 size_t loginc;
1198 size_t len;
1199 size_t cc;
1200 FILE *master;
1201 char newdir[MaxFileNameLen];
1202 char *buf;
1203 char *colon;
1204 char *line;
1205 int masterfd;
1206 int ptmpfd;
1207 int error;
1208
1209 if (!valid_login(newlogin)) {
1210 errx(EXIT_FAILURE, "`%s' is not a valid login name", login_name);
1211 }
1212 if ((pwp = getpwnam(login_name)) == NULL) {
1213 errx(EXIT_FAILURE, "No such user `%s'", login_name);
1214 }
1215 if (!is_local(login_name, _PATH_MASTERPASSWD)) {
1216 errx(EXIT_FAILURE, "User `%s' must be a local user", login_name);
1217 }
1218 /* keep dir name in case we need it for '-m' */
1219 homedir = pwp->pw_dir;
1220
1221 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
1222 err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD);
1223 }
1224 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
1225 err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD);
1226 }
1227 pw_init();
1228 if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
1229 (void) close(masterfd);
1230 err(EXIT_FAILURE, "can't obtain pw_lock");
1231 }
1232 if ((master = fdopen(masterfd, "r")) == NULL) {
1233 (void) close(masterfd);
1234 (void) close(ptmpfd);
1235 pw_abort();
1236 err(EXIT_FAILURE, "can't fdopen fd for %s", _PATH_MASTERPASSWD);
1237 }
1238 if (up != NULL) {
1239 if (up->u_flags & F_USERNAME) {
1240 /* if changing name, check new name isn't already in use */
1241 if (strcmp(login_name, newlogin) != 0 && getpwnam(newlogin) != NULL) {
1242 (void) close(ptmpfd);
1243 pw_abort();
1244 errx(EXIT_FAILURE, "already a `%s' user", newlogin);
1245 }
1246 pwp->pw_name = newlogin;
1247
1248 /*
1249 * Provide a new directory name in case the
1250 * home directory is to be moved.
1251 */
1252 if (up->u_flags & F_MKDIR) {
1253 snprintf(newdir, sizeof(newdir), "%s/%s", up->u_basedir, newlogin);
1254 pwp->pw_dir = newdir;
1255 }
1256 }
1257 if (up->u_flags & F_PASSWORD) {
1258 if (up->u_password != NULL && strlen(up->u_password) == PasswordLength)
1259 pwp->pw_passwd = up->u_password;
1260 }
1261 if (up->u_flags & F_UID) {
1262 /* check uid isn't already allocated */
1263 if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) {
1264 (void) close(ptmpfd);
1265 pw_abort();
1266 errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid);
1267 }
1268 pwp->pw_uid = up->u_uid;
1269 }
1270 if (up->u_flags & F_GROUP) {
1271 /* if -g=uid was specified, check gid is unused */
1272 if (strcmp(up->u_primgrp, "=uid") == 0) {
1273 if (getgrgid((gid_t)(up->u_uid)) != NULL) {
1274 (void) close(ptmpfd);
1275 pw_abort();
1276 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
1277 }
1278 pwp->pw_gid = up->u_uid;
1279 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) {
1280 pwp->pw_gid = grp->gr_gid;
1281 } else if (is_number(up->u_primgrp) &&
1282 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) {
1283 pwp->pw_gid = grp->gr_gid;
1284 } else {
1285 (void) close(ptmpfd);
1286 pw_abort();
1287 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
1288 }
1289 }
1290 if (up->u_flags & F_INACTIVE) {
1291 (void) memset(&tm, 0, sizeof(tm));
1292 if (strptime(up->u_inactive, "%c", &tm) != NULL) {
1293 pwp->pw_change = mktime(&tm);
1294 } else if (strptime(up->u_inactive, "%B %d %Y", &tm) != NULL) {
1295 pwp->pw_change = mktime(&tm);
1296 } else if (isdigit(up->u_inactive[0]) != NULL) {
1297 pwp->pw_change = atoi(up->u_inactive);
1298 } else {
1299 warnx("Warning: inactive time `%s' invalid, password expiry off",
1300 up->u_inactive);
1301 }
1302 }
1303 if (up->u_flags & F_EXPIRE) {
1304 (void) memset(&tm, 0, sizeof(tm));
1305 if (strptime(up->u_expire, "%c", &tm) != NULL) {
1306 pwp->pw_expire = mktime(&tm);
1307 } else if (strptime(up->u_expire, "%B %d %Y", &tm) != NULL) {
1308 pwp->pw_expire = mktime(&tm);
1309 } else if (isdigit(up->u_expire[0]) != NULL) {
1310 pwp->pw_expire = atoi(up->u_expire);
1311 } else {
1312 warnx("Warning: expire time `%s' invalid, password expiry off",
1313 up->u_expire);
1314 }
1315 }
1316 if (up->u_flags & F_COMMENT)
1317 pwp->pw_gecos = up->u_comment;
1318 if (up->u_flags & F_HOMEDIR)
1319 pwp->pw_dir = up->u_home;
1320 if (up->u_flags & F_SHELL)
1321 pwp->pw_shell = up->u_shell;
1322 #ifdef EXTENSIONS
1323 if (up->u_flags & F_CLASS)
1324 pwp->pw_class = up->u_class;
1325 #endif
1326 }
1327 loginc = strlen(login_name);
1328 while ((line = fgetln(master, &len)) != NULL) {
1329 if ((colon = strchr(line, ':')) == NULL) {
1330 warnx("Malformed entry `%s'. Skipping", line);
1331 continue;
1332 }
1333 colonc = (size_t)(colon - line);
1334 if (strncmp(login_name, line, loginc) == 0 && loginc == colonc) {
1335 if (up != NULL) {
1336 len = (int)asprintf(&buf, "%s:%s:%d:%d:"
1337 #ifdef EXTENSIONS
1338 "%s"
1339 #endif
1340 ":%ld:%ld:%s:%s:%s\n",
1341 newlogin,
1342 pwp->pw_passwd,
1343 pwp->pw_uid,
1344 pwp->pw_gid,
1345 #ifdef EXTENSIONS
1346 pwp->pw_class,
1347 #endif
1348 (long)pwp->pw_change,
1349 (long)pwp->pw_expire,
1350 pwp->pw_gecos,
1351 pwp->pw_dir,
1352 pwp->pw_shell);
1353 if (write(ptmpfd, buf, len) != len) {
1354 (void) close(ptmpfd);
1355 pw_abort();
1356 err(EXIT_FAILURE, "can't add `%s'", buf);
1357 }
1358 (void) free(buf);
1359 }
1360 } else if ((cc = write(ptmpfd, line, len)) != len) {
1361 (void) close(masterfd);
1362 (void) close(ptmpfd);
1363 pw_abort();
1364 err(EXIT_FAILURE, "short write to /etc/ptmp (%lld not %lld chars)",
1365 (long long)cc,
1366 (long long)len);
1367 }
1368 }
1369 if (up != NULL) {
1370 if ((up->u_flags & F_MKDIR) &&
1371 asystem("%s %s %s", MV, homedir, pwp->pw_dir) != 0) {
1372 (void) close(ptmpfd);
1373 pw_abort();
1374 err(EXIT_FAILURE, "can't move `%s' to `%s'",
1375 homedir, pwp->pw_dir);
1376 }
1377 if (up->u_groupc > 0 &&
1378 !append_group(newlogin, up->u_groupc, up->u_groupv)) {
1379 (void) close(ptmpfd);
1380 pw_abort();
1381 errx(EXIT_FAILURE, "can't append `%s' to new groups",
1382 newlogin);
1383 }
1384 }
1385 (void) close(ptmpfd);
1386 #if PW_MKDB_ARGC == 2
1387 if (up != NULL && strcmp(login_name, newlogin) == 0) {
1388 error = pw_mkdb(login_name, 0);
1389 } else {
1390 error = pw_mkdb(NULL, 0);
1391 }
1392 #else
1393 error = pw_mkdb();
1394 #endif
1395 if (error < 0) {
1396 pw_abort();
1397 err(EXIT_FAILURE, "pw_mkdb failed");
1398 }
1399
1400 return 1;
1401 }
1402
1403
1404 #ifdef EXTENSIONS
1405 /* see if we can find out the user struct */
1406 static struct passwd *
1407 find_user_info(char *name)
1408 {
1409 struct passwd *pwp;
1410
1411 if ((pwp = getpwnam(name)) != NULL) {
1412 return pwp;
1413 }
1414 if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != NULL) {
1415 return pwp;
1416 }
1417 return NULL;
1418 }
1419 #endif
1420
1421 #ifdef EXTENSIONS
1422 /* see if we can find out the group struct */
1423 static struct group *
1424 find_group_info(char *name)
1425 {
1426 struct group *grp;
1427
1428 if ((grp = getgrnam(name)) != NULL) {
1429 return grp;
1430 }
1431 if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != NULL) {
1432 return grp;
1433 }
1434 return NULL;
1435 }
1436 #endif
1437
1438 /* print out usage message, and then exit */
1439 void
1440 usermgmt_usage(const char *prog)
1441 {
1442 if (strcmp(prog, "useradd") == 0) {
1443 (void) fprintf(stderr, "Usage: %s -D [-b basedir] [-e expiry] "
1444 "[-f inactive] [-g group]\n\t[-r lowuid..highuid] "
1445 "[-s shell] [-L class]\n", prog);
1446 (void) fprintf(stderr, "Usage: %s [-G group] [-b basedir] "
1447 "[-c comment] [-d homedir] [-e expiry]\n\t[-f inactive] "
1448 "[-g group] [-k skeletondir] [-m] [-o] [-p password]\n"
1449 "\t[-r lowuid..highuid] [-s shell]\n\t[-u uid] [-v] user\n",
1450 prog);
1451 } else if (strcmp(prog, "usermod") == 0) {
1452 (void) fprintf(stderr, "Usage: %s [-G group] [-c comment] "
1453 "[-d homedir] [-e expire] [-f inactive]\n\t[-g group] "
1454 "[-l newname] [-m] [-o] [-p password] [-s shell] [-u uid]\n"
1455 "\t[-L class] [-v] user\n", prog);
1456 } else if (strcmp(prog, "userdel") == 0) {
1457 (void) fprintf(stderr, "Usage: %s -D [-p preserve]\n", prog);
1458 (void) fprintf(stderr,
1459 "Usage: %s [-p preserve] [-r] [-v] user\n", prog);
1460 #ifdef EXTENSIONS
1461 } else if (strcmp(prog, "userinfo") == 0) {
1462 (void) fprintf(stderr, "Usage: %s [-e] [-v] user\n", prog);
1463 #endif
1464 } else if (strcmp(prog, "groupadd") == 0) {
1465 (void) fprintf(stderr, "Usage: %s [-g gid] [-o] [-v] group\n",
1466 prog);
1467 } else if (strcmp(prog, "groupdel") == 0) {
1468 (void) fprintf(stderr, "Usage: %s [-v] group\n", prog);
1469 } else if (strcmp(prog, "groupmod") == 0) {
1470 (void) fprintf(stderr,
1471 "Usage: %s [-g gid] [-o] [-n newname] [-v] group\n", prog);
1472 } else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) {
1473 (void) fprintf(stderr,
1474 "Usage: %s ( add | del | mod | info ) ...\n", prog);
1475 #ifdef EXTENSIONS
1476 } else if (strcmp(prog, "groupinfo") == 0) {
1477 (void) fprintf(stderr, "Usage: %s [-e] [-v] group\n", prog);
1478 #endif
1479 }
1480 exit(EXIT_FAILURE);
1481 /* NOTREACHED */
1482 }
1483
1484 #ifdef EXTENSIONS
1485 #define ADD_OPT_EXTENSIONS "p:r:vL:"
1486 #else
1487 #define ADD_OPT_EXTENSIONS
1488 #endif
1489
1490 int
1491 useradd(int argc, char **argv)
1492 {
1493 user_t u;
1494 int defaultfield;
1495 int bigD;
1496 int c;
1497 #ifdef EXTENSIONS
1498 int i;
1499 #endif
1500
1501 (void) memset(&u, 0, sizeof(u));
1502 read_defaults(&u);
1503 u.u_uid = -1;
1504 defaultfield = bigD = 0;
1505 while ((c = getopt(argc, argv, "DG:b:c:d:e:f:g:k:mou:s:" ADD_OPT_EXTENSIONS)) != -1) {
1506 switch(c) {
1507 case 'D':
1508 bigD = 1;
1509 break;
1510 case 'G':
1511 while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
1512 u.u_groupc < NGROUPS_MAX) {
1513 if (u.u_groupv[u.u_groupc][0] != 0) {
1514 u.u_groupc++;
1515 }
1516 }
1517 if (optarg != NULL) {
1518 warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX);
1519 }
1520 break;
1521 case 'b':
1522 defaultfield = 1;
1523 memsave(&u.u_basedir, optarg, strlen(optarg));
1524 break;
1525 case 'c':
1526 memsave(&u.u_comment, optarg, strlen(optarg));
1527 break;
1528 case 'd':
1529 memsave(&u.u_home, optarg, strlen(optarg));
1530 u.u_flags |= F_HOMEDIR;
1531 break;
1532 case 'e':
1533 defaultfield = 1;
1534 memsave(&u.u_expire, optarg, strlen(optarg));
1535 break;
1536 case 'f':
1537 defaultfield = 1;
1538 memsave(&u.u_inactive, optarg, strlen(optarg));
1539 break;
1540 case 'g':
1541 defaultfield = 1;
1542 memsave(&u.u_primgrp, optarg, strlen(optarg));
1543 break;
1544 case 'k':
1545 defaultfield = 1;
1546 memsave(&u.u_skeldir, optarg, strlen(optarg));
1547 break;
1548 #ifdef EXTENSIONS
1549 case 'L':
1550 defaultfield = 1;
1551 memsave(&u.u_class, optarg, strlen(optarg));
1552 break;
1553 #endif
1554 case 'm':
1555 u.u_flags |= F_MKDIR;
1556 break;
1557 case 'o':
1558 u.u_flags |= F_DUPUID;
1559 break;
1560 #ifdef EXTENSIONS
1561 case 'p':
1562 memsave(&u.u_password, optarg, strlen(optarg));
1563 break;
1564 #endif
1565 #ifdef EXTENSIONS
1566 case 'r':
1567 defaultfield = 1;
1568 (void) save_range(&u, optarg);
1569 break;
1570 #endif
1571 case 's':
1572 defaultfield = 1;
1573 memsave(&u.u_shell, optarg, strlen(optarg));
1574 break;
1575 case 'u':
1576 if (!is_number(optarg)) {
1577 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
1578 }
1579 u.u_uid = atoi(optarg);
1580 break;
1581 #ifdef EXTENSIONS
1582 case 'v':
1583 verbose = 1;
1584 break;
1585 #endif
1586 }
1587 }
1588 if (bigD) {
1589 if (defaultfield) {
1590 checkeuid();
1591 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1592 }
1593 (void) printf("group\t\t%s\n", u.u_primgrp);
1594 (void) printf("base_dir\t%s\n", u.u_basedir);
1595 (void) printf("skel_dir\t%s\n", u.u_skeldir);
1596 (void) printf("shell\t\t%s\n", u.u_shell);
1597 #ifdef EXTENSIONS
1598 (void) printf("class\t\t%s\n", u.u_class);
1599 #endif
1600 (void) printf("inactive\t%s\n", (u.u_inactive == NULL) ? UNSET_INACTIVE : u.u_inactive);
1601 (void) printf("expire\t\t%s\n", (u.u_expire == NULL) ? UNSET_EXPIRY : u.u_expire);
1602 #ifdef EXTENSIONS
1603 for (i = 0 ; i < u.u_rc ; i++) {
1604 (void) printf("range\t\t%d..%d\n", u.u_rv[i].r_from, u.u_rv[i].r_to);
1605 }
1606 #endif
1607 return EXIT_SUCCESS;
1608 }
1609 argc -= optind;
1610 argv += optind;
1611 if (argc != 1) {
1612 usermgmt_usage("useradd");
1613 }
1614 checkeuid();
1615 return adduser(*argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1616 }
1617
1618 #ifdef EXTENSIONS
1619 #define MOD_OPT_EXTENSIONS "p:vL:"
1620 #else
1621 #define MOD_OPT_EXTENSIONS
1622 #endif
1623
1624 int
1625 usermod(int argc, char **argv)
1626 {
1627 user_t u;
1628 char newuser[MaxUserNameLen + 1];
1629 int c, have_new_user;
1630
1631 (void) memset(&u, 0, sizeof(u));
1632 (void) memset(newuser, 0, sizeof(newuser));
1633 read_defaults(&u);
1634 have_new_user = 0;
1635 while ((c = getopt(argc, argv, "G:c:d:e:f:g:l:mos:u:" MOD_OPT_EXTENSIONS)) != -1) {
1636 switch(c) {
1637 case 'G':
1638 while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
1639 u.u_groupc < NGROUPS_MAX) {
1640 if (u.u_groupv[u.u_groupc][0] != 0) {
1641 u.u_groupc++;
1642 }
1643 }
1644 if (optarg != NULL) {
1645 warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX);
1646 }
1647 u.u_flags |= F_SECGROUP;
1648 break;
1649 case 'c':
1650 memsave(&u.u_comment, optarg, strlen(optarg));
1651 u.u_flags |= F_COMMENT;
1652 break;
1653 case 'd':
1654 memsave(&u.u_home, optarg, strlen(optarg));
1655 u.u_flags |= F_HOMEDIR;
1656 break;
1657 case 'e':
1658 memsave(&u.u_expire, optarg, strlen(optarg));
1659 u.u_flags |= F_EXPIRE;
1660 break;
1661 case 'f':
1662 memsave(&u.u_inactive, optarg, strlen(optarg));
1663 u.u_flags |= F_INACTIVE;
1664 break;
1665 case 'g':
1666 memsave(&u.u_primgrp, optarg, strlen(optarg));
1667 u.u_flags |= F_GROUP;
1668 break;
1669 case 'l':
1670 (void) strlcpy(newuser, optarg, sizeof(newuser));
1671 have_new_user = 1;
1672 u.u_flags |= F_USERNAME;
1673 break;
1674 #ifdef EXTENSIONS
1675 case 'L':
1676 memsave(&u.u_class, optarg, strlen(optarg));
1677 u.u_flags |= F_CLASS;
1678 break;
1679 #endif
1680 case 'm':
1681 u.u_flags |= F_MKDIR;
1682 break;
1683 case 'o':
1684 u.u_flags |= F_DUPUID;
1685 break;
1686 #ifdef EXTENSIONS
1687 case 'p':
1688 memsave(&u.u_password, optarg, strlen(optarg));
1689 u.u_flags |= F_PASSWORD;
1690 break;
1691 #endif
1692 case 's':
1693 memsave(&u.u_shell, optarg, strlen(optarg));
1694 u.u_flags |= F_SHELL;
1695 break;
1696 case 'u':
1697 if (!is_number(optarg)) {
1698 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
1699 }
1700 u.u_uid = atoi(optarg);
1701 u.u_flags |= F_UID;
1702 break;
1703 #ifdef EXTENSIONS
1704 case 'v':
1705 verbose = 1;
1706 break;
1707 #endif
1708 }
1709 }
1710 if ((u.u_flags & F_MKDIR) && !(u.u_flags & F_HOMEDIR) &&
1711 !(u.u_flags & F_USERNAME)) {
1712 warnx("option 'm' useless without 'd' or 'l' -- ignored");
1713 u.u_flags &= ~F_MKDIR;
1714 }
1715 argc -= optind;
1716 argv += optind;
1717 if (argc != 1) {
1718 usermgmt_usage("usermod");
1719 }
1720 checkeuid();
1721 return moduser(*argv, (have_new_user) ? newuser : *argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1722 }
1723
1724 #ifdef EXTENSIONS
1725 #define DEL_OPT_EXTENSIONS "Dp:v"
1726 #else
1727 #define DEL_OPT_EXTENSIONS
1728 #endif
1729
1730 int
1731 userdel(int argc, char **argv)
1732 {
1733 struct passwd *pwp;
1734 user_t u;
1735 char password[PasswordLength + 1];
1736 int defaultfield;
1737 int rmhome;
1738 int bigD;
1739 int c;
1740
1741 (void) memset(&u, 0, sizeof(u));
1742 read_defaults(&u);
1743 defaultfield = bigD = rmhome = 0;
1744 while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) {
1745 switch(c) {
1746 #ifdef EXTENSIONS
1747 case 'D':
1748 bigD = 1;
1749 break;
1750 #endif
1751 #ifdef EXTENSIONS
1752 case 'p':
1753 defaultfield = 1;
1754 u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 :
1755 (strcmp(optarg, "yes") == 0) ? 1 :
1756 atoi(optarg);
1757 break;
1758 #endif
1759 case 'r':
1760 rmhome = 1;
1761 break;
1762 #ifdef EXTENSIONS
1763 case 'v':
1764 verbose = 1;
1765 break;
1766 #endif
1767 }
1768 }
1769 #ifdef EXTENSIONS
1770 if (bigD) {
1771 if (defaultfield) {
1772 checkeuid();
1773 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1774 }
1775 (void) printf("preserve\t%s\n", (u.u_preserve) ? "true" : "false");
1776 return EXIT_SUCCESS;
1777 }
1778 #endif
1779 argc -= optind;
1780 argv += optind;
1781 if (argc != 1) {
1782 usermgmt_usage("userdel");
1783 }
1784 checkeuid();
1785 if ((pwp = getpwnam(*argv)) == NULL) {
1786 warnx("No such user `%s'", *argv);
1787 return EXIT_FAILURE;
1788 }
1789 if (rmhome)
1790 (void)removehomedir(pwp->pw_name, pwp->pw_uid, pwp->pw_dir);
1791 if (u.u_preserve) {
1792 u.u_flags |= F_SHELL;
1793 memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN));
1794 (void) memset(password, '\0', PasswordLength);
1795 password[0] = '*';
1796 memsave(&u.u_password, password, PasswordLength);
1797 u.u_flags |= F_PASSWORD;
1798 return moduser(*argv, *argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1799 }
1800 if (!rm_user_from_groups(*argv)) {
1801 return 0;
1802 }
1803 return moduser(*argv, *argv, NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
1804 }
1805
1806 #ifdef EXTENSIONS
1807 #define GROUP_ADD_OPT_EXTENSIONS "v"
1808 #else
1809 #define GROUP_ADD_OPT_EXTENSIONS
1810 #endif
1811
1812 /* add a group */
1813 int
1814 groupadd(int argc, char **argv)
1815 {
1816 int dupgid;
1817 int gid;
1818 int c;
1819
1820 gid = -1;
1821 dupgid = 0;
1822 while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) {
1823 switch(c) {
1824 case 'g':
1825 if (!is_number(optarg)) {
1826 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
1827 }
1828 gid = atoi(optarg);
1829 break;
1830 case 'o':
1831 dupgid = 1;
1832 break;
1833 #ifdef EXTENSIONS
1834 case 'v':
1835 verbose = 1;
1836 break;
1837 #endif
1838 }
1839 }
1840 argc -= optind;
1841 argv += optind;
1842 if (argc != 1) {
1843 usermgmt_usage("groupadd");
1844 }
1845 checkeuid();
1846 if (gid < 0 && !getnextgid(&gid, LowGid, HighGid)) {
1847 err(EXIT_FAILURE, "can't add group: can't get next gid");
1848 }
1849 if (!dupgid && getgrgid((gid_t) gid) != NULL) {
1850 errx(EXIT_FAILURE, "can't add group: gid %d is a duplicate", gid);
1851 }
1852 if (!valid_group(*argv)) {
1853 warnx("warning - invalid group name `%s'", *argv);
1854 }
1855 if (!creategid(*argv, gid, "")) {
1856 errx(EXIT_FAILURE, "can't add group: problems with %s file", _PATH_GROUP);
1857 }
1858 return EXIT_SUCCESS;
1859 }
1860
1861 #ifdef EXTENSIONS
1862 #define GROUP_DEL_OPT_EXTENSIONS "v"
1863 #else
1864 #define GROUP_DEL_OPT_EXTENSIONS
1865 #endif
1866
1867 /* remove a group */
1868 int
1869 groupdel(int argc, char **argv)
1870 {
1871 int c;
1872
1873 while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) {
1874 switch(c) {
1875 #ifdef EXTENSIONS
1876 case 'v':
1877 verbose = 1;
1878 break;
1879 #endif
1880 }
1881 }
1882 argc -= optind;
1883 argv += optind;
1884 if (argc != 1) {
1885 usermgmt_usage("groupdel");
1886 }
1887 checkeuid();
1888 if (!modify_gid(*argv, NULL)) {
1889 err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP);
1890 }
1891 return EXIT_SUCCESS;
1892 }
1893
1894 #ifdef EXTENSIONS
1895 #define GROUP_MOD_OPT_EXTENSIONS "v"
1896 #else
1897 #define GROUP_MOD_OPT_EXTENSIONS
1898 #endif
1899
1900 /* modify a group */
1901 int
1902 groupmod(int argc, char **argv)
1903 {
1904 struct group *grp;
1905 char buf[MaxEntryLen];
1906 char *newname;
1907 char **cpp;
1908 int dupgid;
1909 int gid;
1910 int cc;
1911 int c;
1912
1913 gid = -1;
1914 dupgid = 0;
1915 newname = NULL;
1916 while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) {
1917 switch(c) {
1918 case 'g':
1919 if (!is_number(optarg)) {
1920 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
1921 }
1922 gid = atoi(optarg);
1923 break;
1924 case 'o':
1925 dupgid = 1;
1926 break;
1927 case 'n':
1928 memsave(&newname, optarg, strlen(optarg));
1929 break;
1930 #ifdef EXTENSIONS
1931 case 'v':
1932 verbose = 1;
1933 break;
1934 #endif
1935 }
1936 }
1937 argc -= optind;
1938 argv += optind;
1939 if (argc != 1) {
1940 usermgmt_usage("groupmod");
1941 }
1942 checkeuid();
1943 if (gid < 0 && newname == NULL) {
1944 err(EXIT_FAILURE, "Nothing to change");
1945 }
1946 if (dupgid && gid < 0) {
1947 err(EXIT_FAILURE, "Duplicate which gid?");
1948 }
1949 if ((grp = getgrnam(*argv)) == NULL) {
1950 err(EXIT_FAILURE, "can't find group `%s' to modify", *argv);
1951 }
1952 if (!is_local(*argv, _PATH_GROUP)) {
1953 errx(EXIT_FAILURE, "Group `%s' must be a local group", *argv);
1954 }
1955 if (newname != NULL && !valid_group(newname)) {
1956 warn("warning - invalid group name `%s'", newname);
1957 }
1958 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:",
1959 (newname) ? newname : grp->gr_name,
1960 grp->gr_passwd,
1961 (gid < 0) ? grp->gr_gid : gid);
1962 for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) {
1963 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp,
1964 (cpp[1] == NULL) ? "" : ",");
1965 }
1966 cc += snprintf(&buf[cc], sizeof(buf) - cc, "\n");
1967 if (!modify_gid(*argv, buf)) {
1968 err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP);
1969 }
1970 return EXIT_SUCCESS;
1971 }
1972
1973 #ifdef EXTENSIONS
1974 /* display user information */
1975 int
1976 userinfo(int argc, char **argv)
1977 {
1978 struct passwd *pwp;
1979 struct group *grp;
1980 char buf[MaxEntryLen];
1981 char **cpp;
1982 int exists;
1983 int cc;
1984 int i;
1985
1986 exists = 0;
1987 while ((i = getopt(argc, argv, "ev")) != -1) {
1988 switch(i) {
1989 case 'e':
1990 exists = 1;
1991 break;
1992 case 'v':
1993 verbose = 1;
1994 break;
1995 }
1996 }
1997 argc -= optind;
1998 argv += optind;
1999 if (argc != 1) {
2000 usermgmt_usage("userinfo");
2001 }
2002 pwp = find_user_info(*argv);
2003 if (exists) {
2004 exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE);
2005 }
2006 if (pwp == NULL) {
2007 errx(EXIT_FAILURE, "can't find user `%s'", *argv);
2008 }
2009 (void) printf("login\t%s\n", pwp->pw_name);
2010 (void) printf("passwd\t%s\n", pwp->pw_passwd);
2011 (void) printf("uid\t%d\n", pwp->pw_uid);
2012 for (cc = 0 ; (grp = getgrent()) != NULL ; ) {
2013 for (cpp = grp->gr_mem ; *cpp ; cpp++) {
2014 if (strcmp(*cpp, *argv) == 0 && grp->gr_gid != pwp->pw_gid) {
2015 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s ", grp->gr_name);
2016 }
2017 }
2018 }
2019 if ((grp = getgrgid(pwp->pw_gid)) == NULL) {
2020 (void) printf("groups\t%d %s\n", pwp->pw_gid, buf);
2021 } else {
2022 (void) printf("groups\t%s %s\n", grp->gr_name, buf);
2023 }
2024 (void) printf("change\t%s", pwp->pw_change ? ctime(&pwp->pw_change) : "NEVER\n");
2025 #ifdef EXTENSIONS
2026 (void) printf("class\t%s\n", pwp->pw_class);
2027 #endif
2028 (void) printf("gecos\t%s\n", pwp->pw_gecos);
2029 (void) printf("dir\t%s\n", pwp->pw_dir);
2030 (void) printf("shell\t%s\n", pwp->pw_shell);
2031 (void) printf("expire\t%s", pwp->pw_expire ? ctime(&pwp->pw_expire) : "NEVER\n");
2032 return EXIT_SUCCESS;
2033 }
2034 #endif
2035
2036 #ifdef EXTENSIONS
2037 /* display user information */
2038 int
2039 groupinfo(int argc, char **argv)
2040 {
2041 struct group *grp;
2042 char **cpp;
2043 int exists;
2044 int i;
2045
2046 exists = 0;
2047 while ((i = getopt(argc, argv, "ev")) != -1) {
2048 switch(i) {
2049 case 'e':
2050 exists = 1;
2051 break;
2052 case 'v':
2053 verbose = 1;
2054 break;
2055 }
2056 }
2057 argc -= optind;
2058 argv += optind;
2059 if (argc != 1) {
2060 usermgmt_usage("groupinfo");
2061 }
2062 grp = find_group_info(*argv);
2063 if (exists) {
2064 exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE);
2065 }
2066 if (grp == NULL) {
2067 errx(EXIT_FAILURE, "can't find group `%s'", *argv);
2068 }
2069 (void) printf("name\t%s\n", grp->gr_name);
2070 (void) printf("passwd\t%s\n", grp->gr_passwd);
2071 (void) printf("gid\t%d\n", grp->gr_gid);
2072 (void) printf("members\t");
2073 for (cpp = grp->gr_mem ; *cpp ; cpp++) {
2074 (void) printf("%s ", *cpp);
2075 }
2076 (void) fputc('\n', stdout);
2077 return EXIT_SUCCESS;
2078 }
2079 #endif
2080