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