user.c revision 1.74 1 /* $NetBSD: user.c,v 1.74 2004/01/14 08:25:30 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.74 2004/01/14 08:25:30 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((unsigned char) *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 unsigned char *cp;
640
641 if (strlen(login_name) >= LOGIN_NAME_MAX) {
642 return 0;
643 }
644 for (cp = login_name ; *cp ; cp++) {
645 if (!isalnum(*cp) && *cp != '.' && *cp != '_' && *cp != '-') {
646 return 0;
647 }
648 }
649 return 1;
650 }
651
652 /* return 1 if `group' is a valid group name */
653 static int
654 valid_group(char *group)
655 {
656 unsigned char *cp;
657
658 for (cp = group ; *cp ; cp++) {
659 if (!isalnum(*cp)) {
660 return 0;
661 }
662 }
663 return 1;
664 }
665
666 /* find the next gid in the range lo .. hi */
667 static int
668 getnextgid(int *gidp, int lo, int hi)
669 {
670 for (*gidp = lo ; *gidp < hi ; *gidp += 1) {
671 if (getgrgid((gid_t)*gidp) == NULL) {
672 return 1;
673 }
674 }
675 return 0;
676 }
677
678 #ifdef EXTENSIONS
679 /* save a range of uids */
680 static int
681 save_range(user_t *up, char *cp)
682 {
683 int from;
684 int to;
685 int i;
686
687 if (up->u_rsize == 0) {
688 up->u_rsize = 32;
689 NEWARRAY(range_t, up->u_rv, up->u_rsize, return(0));
690 } else if (up->u_rc == up->u_rsize) {
691 up->u_rsize *= 2;
692 RENEW(range_t, up->u_rv, up->u_rsize, return(0));
693 }
694 if (up->u_rv && sscanf(cp, "%d..%d", &from, &to) == 2) {
695 for (i = up->u_defrc ; i < up->u_rc ; i++) {
696 if (up->u_rv[i].r_from == from && up->u_rv[i].r_to == to) {
697 break;
698 }
699 }
700 if (i == up->u_rc) {
701 up->u_rv[up->u_rc].r_from = from;
702 up->u_rv[up->u_rc].r_to = to;
703 up->u_rc += 1;
704 }
705 } else {
706 warnx("Bad range `%s'", cp);
707 return 0;
708 }
709 return 1;
710 }
711 #endif
712
713 /* set the defaults in the defaults file */
714 static int
715 setdefaults(user_t *up)
716 {
717 char template[MaxFileNameLen];
718 FILE *fp;
719 int ret;
720 int fd;
721 #ifdef EXTENSIONS
722 int i;
723 #endif
724
725 (void) snprintf(template, sizeof(template), "%s.XXXXXX", CONFFILE);
726 if ((fd = mkstemp(template)) < 0) {
727 warnx("can't mkstemp `%s' for writing", CONFFILE);
728 return 0;
729 }
730 if ((fp = fdopen(fd, "w")) == NULL) {
731 warn("can't fdopen `%s' for writing", CONFFILE);
732 return 0;
733 }
734 ret = 1;
735 if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 ||
736 fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 ||
737 fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 ||
738 fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 ||
739 #ifdef EXTENSIONS
740 fprintf(fp, "class\t\t%s\n", up->u_class) <= 0 ||
741 #endif
742 fprintf(fp, "inactive\t%s\n", (up->u_inactive == NULL) ? UNSET_INACTIVE : up->u_inactive) <= 0 ||
743 fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ? UNSET_EXPIRY : up->u_expire) <= 0 ||
744 fprintf(fp, "preserve\t%s\n", (up->u_preserve == 0) ? "false" : "true") <= 0) {
745 warn("can't write to `%s'", CONFFILE);
746 ret = 0;
747 }
748 #ifdef EXTENSIONS
749 for (i = (up->u_defrc != up->u_rc) ? up->u_defrc : 0 ; i < up->u_rc ; i++) {
750 if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from, up->u_rv[i].r_to) <= 0) {
751 warn("can't write to `%s'", CONFFILE);
752 ret = 0;
753 }
754 }
755 #endif
756 (void) fclose(fp);
757 if (ret) {
758 ret = ((rename(template, CONFFILE) == 0) && (chmod(CONFFILE, 0644) == 0));
759 }
760 return ret;
761 }
762
763 /* read the defaults file */
764 static void
765 read_defaults(user_t *up)
766 {
767 struct stat st;
768 size_t lineno;
769 size_t len;
770 FILE *fp;
771 unsigned char *cp;
772 unsigned char *s;
773
774 memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP));
775 memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR));
776 memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR));
777 memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL));
778 memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT));
779 #ifdef EXTENSIONS
780 memsave(&up->u_class, DEF_CLASS, strlen(DEF_CLASS));
781 #endif
782 up->u_rsize = 16;
783 up->u_defrc = 0;
784 NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1));
785 up->u_inactive = DEF_INACTIVE;
786 up->u_expire = DEF_EXPIRE;
787 if ((fp = fopen(CONFFILE, "r")) == NULL) {
788 if (stat(CONFFILE, &st) < 0 && !setdefaults(up)) {
789 warn("can't create `%s' defaults file", CONFFILE);
790 }
791 fp = fopen(CONFFILE, "r");
792 }
793 if (fp != NULL) {
794 while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) {
795 if (strncmp(s, "group", 5) == 0) {
796 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
797 }
798 memsave(&up->u_primgrp, cp, strlen(cp));
799 } else if (strncmp(s, "base_dir", 8) == 0) {
800 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
801 }
802 memsave(&up->u_basedir, cp, strlen(cp));
803 } else if (strncmp(s, "skel_dir", 8) == 0) {
804 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
805 }
806 memsave(&up->u_skeldir, cp, strlen(cp));
807 } else if (strncmp(s, "shell", 5) == 0) {
808 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
809 }
810 memsave(&up->u_shell, cp, strlen(cp));
811 #ifdef EXTENSIONS
812 } else if (strncmp(s, "class", 5) == 0) {
813 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
814 }
815 memsave(&up->u_class, cp, strlen(cp));
816 #endif
817 } else if (strncmp(s, "inactive", 8) == 0) {
818 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
819 }
820 if (strcmp(cp, UNSET_INACTIVE) == 0) {
821 if (up->u_inactive) {
822 FREE(up->u_inactive);
823 }
824 up->u_inactive = NULL;
825 } else {
826 memsave(&up->u_inactive, cp, strlen(cp));
827 }
828 #ifdef EXTENSIONS
829 } else if (strncmp(s, "range", 5) == 0) {
830 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
831 }
832 (void) save_range(up, cp);
833 #endif
834 #ifdef EXTENSIONS
835 } else if (strncmp(s, "preserve", 8) == 0) {
836 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
837 }
838 up->u_preserve = (strncmp(cp, "true", 4) == 0) ? 1 :
839 (strncmp(cp, "yes", 3) == 0) ? 1 :
840 atoi(cp);
841 #endif
842 } else if (strncmp(s, "expire", 6) == 0) {
843 for (cp = s + 6 ; *cp && isspace(*cp) ; cp++) {
844 }
845 if (strcmp(cp, UNSET_EXPIRY) == 0) {
846 if (up->u_expire) {
847 FREE(up->u_expire);
848 }
849 up->u_expire = NULL;
850 } else {
851 memsave(&up->u_expire, cp, strlen(cp));
852 }
853 }
854 (void) free(s);
855 }
856 (void) fclose(fp);
857 }
858 if (up->u_rc == 0) {
859 up->u_rv[up->u_rc].r_from = DEF_LOWUID;
860 up->u_rv[up->u_rc].r_to = DEF_HIGHUID;
861 up->u_rc += 1;
862 }
863 up->u_defrc = up->u_rc;
864 }
865
866 /* return the next valid unused uid */
867 static int
868 getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid)
869 {
870 for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) {
871 if (getpwuid((uid_t)(*uid)) == NULL && *uid != NOBODY_UID) {
872 if (sync_uid_gid) {
873 if (getgrgid((gid_t)(*uid)) == NULL) {
874 return 1;
875 }
876 } else {
877 return 1;
878 }
879 }
880 }
881 return 0;
882 }
883
884 /* structure which defines a password type */
885 typedef struct passwd_type_t {
886 const char *type; /* optional type descriptor */
887 int desc_length; /* length of type descriptor */
888 int length; /* length of password */
889 const char *regex; /* regexp to output the password */
890 int re_sub; /* subscript of regexp to use */
891 } passwd_type_t;
892
893 static passwd_type_t passwd_types[] = {
894 { "$2a", 3, 54, "\\$[^$]+\\$[^$]+\\$(.*)", 1 }, /* Blowfish */
895 { "$1", 2, 34, NULL, 0 }, /* MD5 */
896 { "", 0, DES_Len,NULL, 0 }, /* standard DES */
897 { NULL, -1, -1, NULL, 0 } /* none - terminate search */
898 };
899
900 /* return non-zero if it's a valid password - check length for cipher type */
901 static int
902 valid_password_length(char *newpasswd)
903 {
904 passwd_type_t *pwtp;
905 regmatch_t matchv[10];
906 regex_t r;
907
908 for (pwtp = passwd_types ; pwtp->desc_length >= 0 ; pwtp++) {
909 if (strncmp(newpasswd, pwtp->type, pwtp->desc_length) == 0) {
910 if (pwtp->regex == NULL) {
911 return strlen(newpasswd) == pwtp->length;
912 }
913 (void) regcomp(&r, pwtp->regex, REG_EXTENDED);
914 if (regexec(&r, newpasswd, 10, matchv, 0) == 0) {
915 regfree(&r);
916 return (int)(matchv[pwtp->re_sub].rm_eo - matchv[pwtp->re_sub].rm_so + 1) == pwtp->length;
917 }
918 regfree(&r);
919 }
920 }
921 return 0;
922 }
923
924 /* look for a valid time, return 0 if it was specified but bad */
925 static int
926 scantime(time_t *tp, char *s)
927 {
928 struct tm tm;
929
930 *tp = 0;
931 if (s != NULL) {
932 (void) memset(&tm, 0, sizeof(tm));
933 if (strptime(s, "%c", &tm) != NULL) {
934 *tp = mktime(&tm);
935 } else if (strptime(s, "%B %d %Y", &tm) != NULL) {
936 *tp = mktime(&tm);
937 } else if (isdigit((unsigned char) *s)) {
938 *tp = atoi(s);
939 } else {
940 return 0;
941 }
942 }
943 return 1;
944 }
945
946 /* add a user */
947 static int
948 adduser(char *login_name, user_t *up)
949 {
950 struct group *grp;
951 struct stat st;
952 time_t expire;
953 time_t inactive;
954 char password[PasswordLength + 1];
955 char home[MaxFileNameLen];
956 char buf[MaxFileNameLen];
957 int sync_uid_gid;
958 int masterfd;
959 int ptmpfd;
960 int gid;
961 int cc;
962 int i;
963
964 if (!valid_login(login_name)) {
965 errx(EXIT_FAILURE, "`%s' is not a valid login name", login_name);
966 }
967 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
968 err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD);
969 }
970 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
971 err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD);
972 }
973 pw_init();
974 if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
975 (void) close(masterfd);
976 err(EXIT_FAILURE, "can't obtain pw_lock");
977 }
978 while ((cc = read(masterfd, buf, sizeof(buf))) > 0) {
979 if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
980 (void) close(masterfd);
981 (void) close(ptmpfd);
982 pw_abort();
983 err(EXIT_FAILURE, "short write to /etc/ptmp (not %d chars)", cc);
984 }
985 }
986 /* if no uid was specified, get next one in [low_uid..high_uid] range */
987 sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0);
988 if (up->u_uid == -1) {
989 int got_id = 0;
990
991 /*
992 * Look for a free UID in the command line ranges (if any).
993 * These start after the ranges specified in the config file.
994 */
995 for (i = up->u_defrc; !got_id && i < up->u_rc ; i++) {
996 got_id = getnextuid(sync_uid_gid, &up->u_uid,
997 up->u_rv[i].r_from, up->u_rv[i].r_to);
998 }
999 /*
1000 * If there were no free UIDs in the command line ranges,
1001 * try the ranges from the config file (there will always
1002 * be at least one default).
1003 */
1004 for (i = 0; !got_id && i < up->u_defrc; i++) {
1005 got_id = getnextuid(sync_uid_gid, &up->u_uid,
1006 up->u_rv[i].r_from, up->u_rv[i].r_to);
1007 }
1008 if (!got_id) {
1009 (void) close(ptmpfd);
1010 pw_abort();
1011 errx(EXIT_FAILURE, "can't get next uid for %d", up->u_uid);
1012 }
1013 }
1014 /* check uid isn't already allocated */
1015 if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) {
1016 (void) close(ptmpfd);
1017 pw_abort();
1018 errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid);
1019 }
1020 /* if -g=uid was specified, check gid is unused */
1021 if (sync_uid_gid) {
1022 if (getgrgid((gid_t)(up->u_uid)) != NULL) {
1023 (void) close(ptmpfd);
1024 pw_abort();
1025 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
1026 }
1027 gid = up->u_uid;
1028 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) {
1029 gid = grp->gr_gid;
1030 } else if (is_number(up->u_primgrp) &&
1031 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) {
1032 gid = grp->gr_gid;
1033 } else {
1034 (void) close(ptmpfd);
1035 pw_abort();
1036 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
1037 }
1038 /* check name isn't already in use */
1039 if (!(up->u_flags & F_DUPUID) && getpwnam(login_name) != NULL) {
1040 (void) close(ptmpfd);
1041 pw_abort();
1042 errx(EXIT_FAILURE, "already a `%s' user", login_name);
1043 }
1044 if (up->u_flags & F_HOMEDIR) {
1045 (void) strlcpy(home, up->u_home, sizeof(home));
1046 } else {
1047 /* if home directory hasn't been given, make it up */
1048 (void) snprintf(home, sizeof(home), "%s/%s", up->u_basedir, login_name);
1049 }
1050 if (!scantime(&inactive, up->u_inactive)) {
1051 warnx("Warning: inactive time `%s' invalid, account expiry off",
1052 up->u_inactive);
1053 }
1054 if (!scantime(&expire, up->u_expire)) {
1055 warnx("Warning: expire time `%s' invalid, password expiry off",
1056 up->u_expire);
1057 }
1058 if (lstat(home, &st) < 0 && !(up->u_flags & F_MKDIR)) {
1059 warnx("Warning: home directory `%s' doesn't exist, and -m was not specified",
1060 home);
1061 }
1062 password[sizeof(password) - 1] = '\0';
1063 if (up->u_password != NULL && valid_password_length(up->u_password)) {
1064 (void) strlcpy(password, up->u_password, sizeof(password));
1065 } else {
1066 (void) memset(password, '*', DES_Len);
1067 password[DES_Len] = 0;
1068 if (up->u_password != NULL) {
1069 warnx("Password `%s' is invalid: setting it to `%s'",
1070 up->u_password, password);
1071 }
1072 }
1073 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
1074 login_name,
1075 password,
1076 up->u_uid,
1077 gid,
1078 #ifdef EXTENSIONS
1079 up->u_class,
1080 #else
1081 "",
1082 #endif
1083 (long) inactive,
1084 (long) expire,
1085 up->u_comment,
1086 home,
1087 up->u_shell);
1088 if (write(ptmpfd, buf, (size_t) cc) != cc) {
1089 (void) close(ptmpfd);
1090 pw_abort();
1091 err(EXIT_FAILURE, "can't add `%s'", buf);
1092 }
1093 if (up->u_flags & F_MKDIR) {
1094 if (lstat(home, &st) == 0) {
1095 (void) close(ptmpfd);
1096 pw_abort();
1097 errx(EXIT_FAILURE, "home directory `%s' already exists", home);
1098 } else {
1099 if (asystem("%s -p %s", MKDIR, home) != 0) {
1100 (void) close(ptmpfd);
1101 pw_abort();
1102 err(EXIT_FAILURE, "can't mkdir `%s'", home);
1103 }
1104 (void) copydotfiles(up->u_skeldir, up->u_uid, gid, home);
1105 }
1106 }
1107 if (strcmp(up->u_primgrp, "=uid") == 0 &&
1108 getgrnam(login_name) == NULL &&
1109 !creategid(login_name, gid, login_name)) {
1110 (void) close(ptmpfd);
1111 pw_abort();
1112 errx(EXIT_FAILURE, "can't create gid %d for login name %s", gid, login_name);
1113 }
1114 if (up->u_groupc > 0 && !append_group(login_name, up->u_groupc, up->u_groupv)) {
1115 (void) close(ptmpfd);
1116 pw_abort();
1117 errx(EXIT_FAILURE, "can't append `%s' to new groups", login_name);
1118 }
1119 (void) close(ptmpfd);
1120 #if PW_MKDB_ARGC == 2
1121 if (pw_mkdb(login_name, 0) < 0) {
1122 pw_abort();
1123 err(EXIT_FAILURE, "pw_mkdb failed");
1124 }
1125 #else
1126 if (pw_mkdb() < 0) {
1127 pw_abort();
1128 err(EXIT_FAILURE, "pw_mkdb failed");
1129 }
1130 #endif
1131 syslog(LOG_INFO, "new user added: name=%s, uid=%d, gid=%d, home=%s, shell=%s",
1132 login_name, up->u_uid, gid, home, up->u_shell);
1133 return 1;
1134 }
1135
1136 /* remove a user from the groups file */
1137 static int
1138 rm_user_from_groups(char *login_name)
1139 {
1140 struct stat st;
1141 regmatch_t matchv[10];
1142 regex_t r;
1143 FILE *from;
1144 FILE *to;
1145 char line[MaxEntryLen];
1146 char buf[MaxEntryLen];
1147 char f[MaxFileNameLen];
1148 int fd;
1149 int cc;
1150 int sc;
1151
1152 (void) snprintf(line, sizeof(line), "(:|,)(%s)(,|$)", login_name);
1153 if (regcomp(&r, line, REG_EXTENDED|REG_NEWLINE) != 0) {
1154 warn("can't compile regular expression `%s'", line);
1155 return 0;
1156 }
1157 if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
1158 warn("can't remove gid for `%s': can't open `%s'", login_name, _PATH_GROUP);
1159 return 0;
1160 }
1161 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
1162 warn("can't lock `%s'", _PATH_GROUP);
1163 }
1164 (void) fstat(fileno(from), &st);
1165 (void) snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
1166 if ((fd = mkstemp(f)) < 0) {
1167 (void) fclose(from);
1168 warn("can't create gid: mkstemp failed");
1169 return 0;
1170 }
1171 if ((to = fdopen(fd, "w")) == NULL) {
1172 (void) fclose(from);
1173 (void) close(fd);
1174 (void) unlink(f);
1175 warn("can't create gid: fdopen `%s' failed", f);
1176 return 0;
1177 }
1178 while (fgets(buf, sizeof(buf), from) > 0) {
1179 cc = strlen(buf);
1180 if (regexec(&r, buf, 10, matchv, 0) == 0) {
1181 if (buf[(int)matchv[1].rm_so] == ',') {
1182 matchv[2].rm_so = matchv[1].rm_so;
1183 } else if (matchv[2].rm_eo != matchv[3].rm_eo) {
1184 matchv[2].rm_eo = matchv[3].rm_eo;
1185 }
1186 cc -= (int) matchv[2].rm_eo;
1187 sc = (int) matchv[2].rm_so;
1188 if (fwrite(buf, sizeof(char), sc, to) != sc ||
1189 fwrite(&buf[(int)matchv[2].rm_eo], sizeof(char), cc, to) != cc) {
1190 (void) fclose(from);
1191 (void) close(fd);
1192 (void) unlink(f);
1193 warn("can't create gid: short write to `%s'", f);
1194 return 0;
1195 }
1196 } else if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
1197 (void) fclose(from);
1198 (void) close(fd);
1199 (void) unlink(f);
1200 warn("can't create gid: short write to `%s'", f);
1201 return 0;
1202 }
1203 }
1204 (void) fclose(from);
1205 (void) fclose(to);
1206 if (rename(f, _PATH_GROUP) < 0) {
1207 (void) unlink(f);
1208 warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
1209 return 0;
1210 }
1211 (void) chmod(_PATH_GROUP, st.st_mode & 07777);
1212 return 1;
1213 }
1214
1215 /* check that the user or group is local, not from YP/NIS */
1216 static int
1217 is_local(char *name, const char *file)
1218 {
1219 regmatch_t matchv[10];
1220 regex_t r;
1221 FILE *fp;
1222 char buf[MaxEntryLen];
1223 char re[MaxEntryLen];
1224 int ret;
1225
1226 (void) snprintf(re, sizeof(re), "^%s:", name);
1227 if (regcomp(&r, re, REG_EXTENDED) != 0) {
1228 errx(EXIT_FAILURE, "can't compile regular expression `%s'", re);
1229 }
1230 if ((fp = fopen(file, "r")) == NULL) {
1231 err(EXIT_FAILURE, "can't open `%s'", file);
1232 }
1233 for (ret = 0 ; fgets(buf, sizeof(buf), fp) != NULL ; ) {
1234 if (regexec(&r, buf, 10, matchv, 0) == 0) {
1235 ret = 1;
1236 break;
1237 }
1238 }
1239 (void) fclose(fp);
1240 return ret;
1241 }
1242
1243 /* modify a user */
1244 static int
1245 moduser(char *login_name, char *newlogin, user_t *up)
1246 {
1247 struct passwd *pwp;
1248 struct group *grp;
1249 const char *homedir;
1250 size_t colonc;
1251 size_t loginc;
1252 size_t len;
1253 size_t cc;
1254 FILE *master;
1255 char newdir[MaxFileNameLen];
1256 char *buf;
1257 char *colon;
1258 int masterfd;
1259 int ptmpfd;
1260 int error;
1261
1262 if (!valid_login(newlogin)) {
1263 errx(EXIT_FAILURE, "`%s' is not a valid login name", login_name);
1264 }
1265 if ((pwp = getpwnam(login_name)) == NULL) {
1266 errx(EXIT_FAILURE, "No such user `%s'", login_name);
1267 }
1268 if (!is_local(login_name, _PATH_MASTERPASSWD)) {
1269 errx(EXIT_FAILURE, "User `%s' must be a local user", login_name);
1270 }
1271 /* keep dir name in case we need it for '-m' */
1272 homedir = pwp->pw_dir;
1273
1274 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
1275 err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD);
1276 }
1277 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
1278 err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD);
1279 }
1280 pw_init();
1281 if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
1282 (void) close(masterfd);
1283 err(EXIT_FAILURE, "can't obtain pw_lock");
1284 }
1285 if ((master = fdopen(masterfd, "r")) == NULL) {
1286 (void) close(masterfd);
1287 (void) close(ptmpfd);
1288 pw_abort();
1289 err(EXIT_FAILURE, "can't fdopen fd for %s", _PATH_MASTERPASSWD);
1290 }
1291 if (up != NULL) {
1292 if (up->u_flags & F_USERNAME) {
1293 /* if changing name, check new name isn't already in use */
1294 if (strcmp(login_name, newlogin) != 0 && getpwnam(newlogin) != NULL) {
1295 (void) close(ptmpfd);
1296 pw_abort();
1297 errx(EXIT_FAILURE, "already a `%s' user", newlogin);
1298 }
1299 pwp->pw_name = newlogin;
1300
1301 /*
1302 * Provide a new directory name in case the
1303 * home directory is to be moved.
1304 */
1305 if (up->u_flags & F_MKDIR) {
1306 snprintf(newdir, sizeof(newdir), "%s/%s", up->u_basedir, newlogin);
1307 pwp->pw_dir = newdir;
1308 }
1309 }
1310 if (up->u_flags & F_PASSWORD) {
1311 if (up->u_password != NULL) {
1312 if (!valid_password_length(up->u_password)) {
1313 (void) close(ptmpfd);
1314 pw_abort();
1315 errx(EXIT_FAILURE, "Invalid password: `%s'",
1316 up->u_password);
1317 }
1318 pwp->pw_passwd = up->u_password;
1319 }
1320 }
1321 if (up->u_flags & F_UID) {
1322 /* check uid isn't already allocated */
1323 if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) {
1324 (void) close(ptmpfd);
1325 pw_abort();
1326 errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid);
1327 }
1328 pwp->pw_uid = up->u_uid;
1329 }
1330 if (up->u_flags & F_GROUP) {
1331 /* if -g=uid was specified, check gid is unused */
1332 if (strcmp(up->u_primgrp, "=uid") == 0) {
1333 if (getgrgid((gid_t)(up->u_uid)) != NULL) {
1334 (void) close(ptmpfd);
1335 pw_abort();
1336 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
1337 }
1338 pwp->pw_gid = up->u_uid;
1339 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) {
1340 pwp->pw_gid = grp->gr_gid;
1341 } else if (is_number(up->u_primgrp) &&
1342 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) {
1343 pwp->pw_gid = grp->gr_gid;
1344 } else {
1345 (void) close(ptmpfd);
1346 pw_abort();
1347 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
1348 }
1349 }
1350 if (up->u_flags & F_INACTIVE) {
1351 if (!scantime(&pwp->pw_change, up->u_inactive)) {
1352 warnx("Warning: inactive time `%s' invalid, password expiry off",
1353 up->u_inactive);
1354 }
1355 }
1356 if (up->u_flags & F_EXPIRE) {
1357 if (!scantime(&pwp->pw_expire, up->u_expire)) {
1358 warnx("Warning: expire time `%s' invalid, password expiry off",
1359 up->u_expire);
1360 }
1361 }
1362 if (up->u_flags & F_COMMENT)
1363 pwp->pw_gecos = up->u_comment;
1364 if (up->u_flags & F_HOMEDIR)
1365 pwp->pw_dir = up->u_home;
1366 if (up->u_flags & F_SHELL)
1367 pwp->pw_shell = up->u_shell;
1368 #ifdef EXTENSIONS
1369 if (up->u_flags & F_CLASS)
1370 pwp->pw_class = up->u_class;
1371 #endif
1372 }
1373 loginc = strlen(login_name);
1374 while (fgets(buf, sizeof(buf), master) != NULL) {
1375 if ((colon = strchr(buf, ':')) == NULL) {
1376 warnx("Malformed entry `%s'. Skipping", buf);
1377 continue;
1378 }
1379 colonc = (size_t)(colon - buf);
1380 if (strncmp(login_name, buf, loginc) == 0 && loginc == colonc) {
1381 if (up != NULL) {
1382 len = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:"
1383 #ifdef EXTENSIONS
1384 "%s"
1385 #endif
1386 ":%ld:%ld:%s:%s:%s\n",
1387 newlogin,
1388 pwp->pw_passwd,
1389 pwp->pw_uid,
1390 pwp->pw_gid,
1391 #ifdef EXTENSIONS
1392 pwp->pw_class,
1393 #endif
1394 (long)pwp->pw_change,
1395 (long)pwp->pw_expire,
1396 pwp->pw_gecos,
1397 pwp->pw_dir,
1398 pwp->pw_shell);
1399 if (write(ptmpfd, buf, len) != len) {
1400 (void) close(ptmpfd);
1401 pw_abort();
1402 err(EXIT_FAILURE, "can't add `%s'", buf);
1403 }
1404 (void) free(buf);
1405 }
1406 } else {
1407 len = strlen(buf);
1408 if ((cc = write(ptmpfd, buf, len)) != len) {
1409 (void) close(masterfd);
1410 (void) close(ptmpfd);
1411 pw_abort();
1412 err(EXIT_FAILURE, "short write to /etc/ptmp (%lld not %lld chars)",
1413 (long long)cc,
1414 (long long)len);
1415 }
1416 }
1417 }
1418 if (up != NULL) {
1419 if ((up->u_flags & F_MKDIR) &&
1420 asystem("%s %s %s", MV, homedir, pwp->pw_dir) != 0) {
1421 (void) close(ptmpfd);
1422 pw_abort();
1423 err(EXIT_FAILURE, "can't move `%s' to `%s'",
1424 homedir, pwp->pw_dir);
1425 }
1426 if (up->u_groupc > 0 &&
1427 !append_group(newlogin, up->u_groupc, up->u_groupv)) {
1428 (void) close(ptmpfd);
1429 pw_abort();
1430 errx(EXIT_FAILURE, "can't append `%s' to new groups",
1431 newlogin);
1432 }
1433 }
1434 (void) close(ptmpfd);
1435 #if PW_MKDB_ARGC == 2
1436 if (up != NULL && strcmp(login_name, newlogin) == 0) {
1437 error = pw_mkdb(login_name, 0);
1438 } else {
1439 error = pw_mkdb(NULL, 0);
1440 }
1441 #else
1442 error = pw_mkdb();
1443 #endif
1444 if (error < 0) {
1445 pw_abort();
1446 err(EXIT_FAILURE, "pw_mkdb failed");
1447 }
1448 if (up == NULL) {
1449 syslog(LOG_INFO, "user removed: name=%s", login_name);
1450 } else if (strcmp(login_name, newlogin) == 0) {
1451 syslog(LOG_INFO, "user information modified: name=%s, uid=%d, gid=%d, home=%s, shell=%s",
1452 login_name, pwp->pw_uid, pwp->pw_gid, pwp->pw_dir, pwp->pw_shell);
1453 } else {
1454 syslog(LOG_INFO, "user information modified: name=%s, new name=%s, uid=%d, gid=%d, home=%s, shell=%s",
1455 login_name, newlogin, pwp->pw_uid, pwp->pw_gid, pwp->pw_dir, pwp->pw_shell);
1456 }
1457 return 1;
1458 }
1459
1460
1461 #ifdef EXTENSIONS
1462 /* see if we can find out the user struct */
1463 static struct passwd *
1464 find_user_info(char *name)
1465 {
1466 struct passwd *pwp;
1467
1468 if ((pwp = getpwnam(name)) != NULL) {
1469 return pwp;
1470 }
1471 if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != NULL) {
1472 return pwp;
1473 }
1474 return NULL;
1475 }
1476 #endif
1477
1478 #ifdef EXTENSIONS
1479 /* see if we can find out the group struct */
1480 static struct group *
1481 find_group_info(char *name)
1482 {
1483 struct group *grp;
1484
1485 if ((grp = getgrnam(name)) != NULL) {
1486 return grp;
1487 }
1488 if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != NULL) {
1489 return grp;
1490 }
1491 return NULL;
1492 }
1493 #endif
1494
1495 /* print out usage message, and then exit */
1496 void
1497 usermgmt_usage(const char *prog)
1498 {
1499 if (strcmp(prog, "useradd") == 0) {
1500 (void) fprintf(stderr, "usage: %s -D [-b basedir] [-e expiry] "
1501 "[-f inactive] [-g group]\n\t[-r lowuid..highuid] "
1502 "[-s shell] [-L class]\n", prog);
1503 (void) fprintf(stderr, "usage: %s [-G group] [-b basedir] "
1504 "[-c comment] [-d homedir] [-e expiry]\n\t[-f inactive] "
1505 "[-g group] [-k skeletondir] [-m] [-o] [-p password]\n"
1506 "\t[-r lowuid..highuid] [-s shell]\n\t[-u uid] [-v] user\n",
1507 prog);
1508 } else if (strcmp(prog, "usermod") == 0) {
1509 (void) fprintf(stderr, "usage: %s [-G group] [-c comment] "
1510 "[-d homedir] [-e expire] [-f inactive]\n\t[-g group] "
1511 "[-l newname] [-m] [-o] [-p password] [-s shell] [-u uid]\n"
1512 "\t[-L class] [-v] user\n", prog);
1513 } else if (strcmp(prog, "userdel") == 0) {
1514 (void) fprintf(stderr, "usage: %s -D [-p preserve]\n", prog);
1515 (void) fprintf(stderr,
1516 "usage: %s [-p preserve] [-r] [-v] user\n", prog);
1517 #ifdef EXTENSIONS
1518 } else if (strcmp(prog, "userinfo") == 0) {
1519 (void) fprintf(stderr, "usage: %s [-e] [-v] user\n", prog);
1520 #endif
1521 } else if (strcmp(prog, "groupadd") == 0) {
1522 (void) fprintf(stderr, "usage: %s [-g gid] [-o] [-v] group\n",
1523 prog);
1524 } else if (strcmp(prog, "groupdel") == 0) {
1525 (void) fprintf(stderr, "usage: %s [-v] group\n", prog);
1526 } else if (strcmp(prog, "groupmod") == 0) {
1527 (void) fprintf(stderr,
1528 "usage: %s [-g gid] [-o] [-n newname] [-v] group\n", prog);
1529 } else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) {
1530 (void) fprintf(stderr,
1531 "usage: %s ( add | del | mod | info ) ...\n", prog);
1532 #ifdef EXTENSIONS
1533 } else if (strcmp(prog, "groupinfo") == 0) {
1534 (void) fprintf(stderr, "usage: %s [-e] [-v] group\n", prog);
1535 #endif
1536 }
1537 exit(EXIT_FAILURE);
1538 /* NOTREACHED */
1539 }
1540
1541 #ifdef EXTENSIONS
1542 #define ADD_OPT_EXTENSIONS "p:r:vL:"
1543 #else
1544 #define ADD_OPT_EXTENSIONS
1545 #endif
1546
1547 int
1548 useradd(int argc, char **argv)
1549 {
1550 user_t u;
1551 int defaultfield;
1552 int bigD;
1553 int c;
1554 #ifdef EXTENSIONS
1555 int i;
1556 #endif
1557
1558 (void) memset(&u, 0, sizeof(u));
1559 read_defaults(&u);
1560 u.u_uid = -1;
1561 defaultfield = bigD = 0;
1562 while ((c = getopt(argc, argv, "DG:b:c:d:e:f:g:k:mou:s:" ADD_OPT_EXTENSIONS)) != -1) {
1563 switch(c) {
1564 case 'D':
1565 bigD = 1;
1566 break;
1567 case 'G':
1568 while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
1569 u.u_groupc < NGROUPS_MAX) {
1570 if (u.u_groupv[u.u_groupc][0] != 0) {
1571 u.u_groupc++;
1572 }
1573 }
1574 if (optarg != NULL) {
1575 warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX);
1576 }
1577 break;
1578 case 'b':
1579 defaultfield = 1;
1580 memsave(&u.u_basedir, optarg, strlen(optarg));
1581 break;
1582 case 'c':
1583 memsave(&u.u_comment, optarg, strlen(optarg));
1584 break;
1585 case 'd':
1586 memsave(&u.u_home, optarg, strlen(optarg));
1587 u.u_flags |= F_HOMEDIR;
1588 break;
1589 case 'e':
1590 defaultfield = 1;
1591 memsave(&u.u_expire, optarg, strlen(optarg));
1592 break;
1593 case 'f':
1594 defaultfield = 1;
1595 memsave(&u.u_inactive, optarg, strlen(optarg));
1596 break;
1597 case 'g':
1598 defaultfield = 1;
1599 memsave(&u.u_primgrp, optarg, strlen(optarg));
1600 break;
1601 case 'k':
1602 defaultfield = 1;
1603 memsave(&u.u_skeldir, optarg, strlen(optarg));
1604 break;
1605 #ifdef EXTENSIONS
1606 case 'L':
1607 defaultfield = 1;
1608 memsave(&u.u_class, optarg, strlen(optarg));
1609 break;
1610 #endif
1611 case 'm':
1612 u.u_flags |= F_MKDIR;
1613 break;
1614 case 'o':
1615 u.u_flags |= F_DUPUID;
1616 break;
1617 #ifdef EXTENSIONS
1618 case 'p':
1619 memsave(&u.u_password, optarg, strlen(optarg));
1620 break;
1621 #endif
1622 #ifdef EXTENSIONS
1623 case 'r':
1624 defaultfield = 1;
1625 (void) save_range(&u, optarg);
1626 break;
1627 #endif
1628 case 's':
1629 defaultfield = 1;
1630 memsave(&u.u_shell, optarg, strlen(optarg));
1631 break;
1632 case 'u':
1633 if (!is_number(optarg)) {
1634 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
1635 }
1636 u.u_uid = atoi(optarg);
1637 break;
1638 #ifdef EXTENSIONS
1639 case 'v':
1640 verbose = 1;
1641 break;
1642 #endif
1643 default:
1644 usermgmt_usage("useradd");
1645 /* NOTREACHED */
1646 }
1647 }
1648 if (bigD) {
1649 if (defaultfield) {
1650 checkeuid();
1651 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1652 }
1653 (void) printf("group\t\t%s\n", u.u_primgrp);
1654 (void) printf("base_dir\t%s\n", u.u_basedir);
1655 (void) printf("skel_dir\t%s\n", u.u_skeldir);
1656 (void) printf("shell\t\t%s\n", u.u_shell);
1657 #ifdef EXTENSIONS
1658 (void) printf("class\t\t%s\n", u.u_class);
1659 #endif
1660 (void) printf("inactive\t%s\n", (u.u_inactive == NULL) ? UNSET_INACTIVE : u.u_inactive);
1661 (void) printf("expire\t\t%s\n", (u.u_expire == NULL) ? UNSET_EXPIRY : u.u_expire);
1662 #ifdef EXTENSIONS
1663 for (i = 0 ; i < u.u_rc ; i++) {
1664 (void) printf("range\t\t%d..%d\n", u.u_rv[i].r_from, u.u_rv[i].r_to);
1665 }
1666 #endif
1667 return EXIT_SUCCESS;
1668 }
1669 argc -= optind;
1670 argv += optind;
1671 if (argc != 1) {
1672 usermgmt_usage("useradd");
1673 }
1674 checkeuid();
1675 openlog("useradd", LOG_PID, LOG_USER);
1676 return adduser(*argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1677 }
1678
1679 #ifdef EXTENSIONS
1680 #define MOD_OPT_EXTENSIONS "p:vL:"
1681 #else
1682 #define MOD_OPT_EXTENSIONS
1683 #endif
1684
1685 int
1686 usermod(int argc, char **argv)
1687 {
1688 user_t u;
1689 char newuser[MaxUserNameLen + 1];
1690 int c, have_new_user;
1691
1692 (void) memset(&u, 0, sizeof(u));
1693 (void) memset(newuser, 0, sizeof(newuser));
1694 read_defaults(&u);
1695 have_new_user = 0;
1696 while ((c = getopt(argc, argv, "G:c:d:e:f:g:l:mos:u:" MOD_OPT_EXTENSIONS)) != -1) {
1697 switch(c) {
1698 case 'G':
1699 while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
1700 u.u_groupc < NGROUPS_MAX) {
1701 if (u.u_groupv[u.u_groupc][0] != 0) {
1702 u.u_groupc++;
1703 }
1704 }
1705 if (optarg != NULL) {
1706 warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX);
1707 }
1708 u.u_flags |= F_SECGROUP;
1709 break;
1710 case 'c':
1711 memsave(&u.u_comment, optarg, strlen(optarg));
1712 u.u_flags |= F_COMMENT;
1713 break;
1714 case 'd':
1715 memsave(&u.u_home, optarg, strlen(optarg));
1716 u.u_flags |= F_HOMEDIR;
1717 break;
1718 case 'e':
1719 memsave(&u.u_expire, optarg, strlen(optarg));
1720 u.u_flags |= F_EXPIRE;
1721 break;
1722 case 'f':
1723 memsave(&u.u_inactive, optarg, strlen(optarg));
1724 u.u_flags |= F_INACTIVE;
1725 break;
1726 case 'g':
1727 memsave(&u.u_primgrp, optarg, strlen(optarg));
1728 u.u_flags |= F_GROUP;
1729 break;
1730 case 'l':
1731 (void) strlcpy(newuser, optarg, sizeof(newuser));
1732 have_new_user = 1;
1733 u.u_flags |= F_USERNAME;
1734 break;
1735 #ifdef EXTENSIONS
1736 case 'L':
1737 memsave(&u.u_class, optarg, strlen(optarg));
1738 u.u_flags |= F_CLASS;
1739 break;
1740 #endif
1741 case 'm':
1742 u.u_flags |= F_MKDIR;
1743 break;
1744 case 'o':
1745 u.u_flags |= F_DUPUID;
1746 break;
1747 #ifdef EXTENSIONS
1748 case 'p':
1749 memsave(&u.u_password, optarg, strlen(optarg));
1750 u.u_flags |= F_PASSWORD;
1751 break;
1752 #endif
1753 case 's':
1754 memsave(&u.u_shell, optarg, strlen(optarg));
1755 u.u_flags |= F_SHELL;
1756 break;
1757 case 'u':
1758 if (!is_number(optarg)) {
1759 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
1760 }
1761 u.u_uid = atoi(optarg);
1762 u.u_flags |= F_UID;
1763 break;
1764 #ifdef EXTENSIONS
1765 case 'v':
1766 verbose = 1;
1767 break;
1768 #endif
1769 default:
1770 usermgmt_usage("usermod");
1771 /* NOTREACHED */
1772 }
1773 }
1774 if ((u.u_flags & F_MKDIR) && !(u.u_flags & F_HOMEDIR) &&
1775 !(u.u_flags & F_USERNAME)) {
1776 warnx("option 'm' useless without 'd' or 'l' -- ignored");
1777 u.u_flags &= ~F_MKDIR;
1778 }
1779 argc -= optind;
1780 argv += optind;
1781 if (argc != 1) {
1782 usermgmt_usage("usermod");
1783 }
1784 checkeuid();
1785 openlog("usermod", LOG_PID, LOG_USER);
1786 return moduser(*argv, (have_new_user) ? newuser : *argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1787 }
1788
1789 #ifdef EXTENSIONS
1790 #define DEL_OPT_EXTENSIONS "Dp:v"
1791 #else
1792 #define DEL_OPT_EXTENSIONS
1793 #endif
1794
1795 int
1796 userdel(int argc, char **argv)
1797 {
1798 struct passwd *pwp;
1799 user_t u;
1800 char password[PasswordLength + 1];
1801 int defaultfield;
1802 int rmhome;
1803 int bigD;
1804 int c;
1805
1806 (void) memset(&u, 0, sizeof(u));
1807 read_defaults(&u);
1808 defaultfield = bigD = rmhome = 0;
1809 while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) {
1810 switch(c) {
1811 #ifdef EXTENSIONS
1812 case 'D':
1813 bigD = 1;
1814 break;
1815 #endif
1816 #ifdef EXTENSIONS
1817 case 'p':
1818 defaultfield = 1;
1819 u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 :
1820 (strcmp(optarg, "yes") == 0) ? 1 :
1821 atoi(optarg);
1822 break;
1823 #endif
1824 case 'r':
1825 rmhome = 1;
1826 break;
1827 #ifdef EXTENSIONS
1828 case 'v':
1829 verbose = 1;
1830 break;
1831 #endif
1832 default:
1833 usermgmt_usage("userdel");
1834 /* NOTREACHED */
1835 }
1836 }
1837 #ifdef EXTENSIONS
1838 if (bigD) {
1839 if (defaultfield) {
1840 checkeuid();
1841 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1842 }
1843 (void) printf("preserve\t%s\n", (u.u_preserve) ? "true" : "false");
1844 return EXIT_SUCCESS;
1845 }
1846 #endif
1847 argc -= optind;
1848 argv += optind;
1849 if (argc != 1) {
1850 usermgmt_usage("userdel");
1851 }
1852 checkeuid();
1853 if ((pwp = getpwnam(*argv)) == NULL) {
1854 warnx("No such user `%s'", *argv);
1855 return EXIT_FAILURE;
1856 }
1857 if (rmhome)
1858 (void)removehomedir(pwp->pw_name, pwp->pw_uid, pwp->pw_dir);
1859 if (u.u_preserve) {
1860 u.u_flags |= F_SHELL;
1861 memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN));
1862 (void) memset(password, '*', DES_Len);
1863 password[DES_Len] = 0;
1864 memsave(&u.u_password, password, strlen(password));
1865 u.u_flags |= F_PASSWORD;
1866 openlog("userdel", LOG_PID, LOG_USER);
1867 return moduser(*argv, *argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1868 }
1869 if (!rm_user_from_groups(*argv)) {
1870 return 0;
1871 }
1872 openlog("userdel", LOG_PID, LOG_USER);
1873 return moduser(*argv, *argv, NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
1874 }
1875
1876 #ifdef EXTENSIONS
1877 #define GROUP_ADD_OPT_EXTENSIONS "v"
1878 #else
1879 #define GROUP_ADD_OPT_EXTENSIONS
1880 #endif
1881
1882 /* add a group */
1883 int
1884 groupadd(int argc, char **argv)
1885 {
1886 int dupgid;
1887 int gid;
1888 int c;
1889
1890 gid = -1;
1891 dupgid = 0;
1892 while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) {
1893 switch(c) {
1894 case 'g':
1895 if (!is_number(optarg)) {
1896 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
1897 }
1898 gid = atoi(optarg);
1899 break;
1900 case 'o':
1901 dupgid = 1;
1902 break;
1903 #ifdef EXTENSIONS
1904 case 'v':
1905 verbose = 1;
1906 break;
1907 #endif
1908 default:
1909 usermgmt_usage("groupadd");
1910 /* NOTREACHED */
1911 }
1912 }
1913 argc -= optind;
1914 argv += optind;
1915 if (argc != 1) {
1916 usermgmt_usage("groupadd");
1917 }
1918 checkeuid();
1919 if (gid < 0 && !getnextgid(&gid, LowGid, HighGid)) {
1920 err(EXIT_FAILURE, "can't add group: can't get next gid");
1921 }
1922 if (!dupgid && getgrgid((gid_t) gid) != NULL) {
1923 errx(EXIT_FAILURE, "can't add group: gid %d is a duplicate", gid);
1924 }
1925 if (!valid_group(*argv)) {
1926 warnx("warning - invalid group name `%s'", *argv);
1927 }
1928 openlog("groupadd", LOG_PID, LOG_USER);
1929 if (!creategid(*argv, gid, "")) {
1930 errx(EXIT_FAILURE, "can't add group: problems with %s file", _PATH_GROUP);
1931 }
1932 return EXIT_SUCCESS;
1933 }
1934
1935 #ifdef EXTENSIONS
1936 #define GROUP_DEL_OPT_EXTENSIONS "v"
1937 #else
1938 #define GROUP_DEL_OPT_EXTENSIONS
1939 #endif
1940
1941 /* remove a group */
1942 int
1943 groupdel(int argc, char **argv)
1944 {
1945 int c;
1946
1947 while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) {
1948 switch(c) {
1949 #ifdef EXTENSIONS
1950 case 'v':
1951 verbose = 1;
1952 break;
1953 #endif
1954 default:
1955 usermgmt_usage("groupdel");
1956 /* NOTREACHED */
1957 }
1958 }
1959 argc -= optind;
1960 argv += optind;
1961 if (argc != 1) {
1962 usermgmt_usage("groupdel");
1963 }
1964 checkeuid();
1965 openlog("groupdel", LOG_PID, LOG_USER);
1966 if (!modify_gid(*argv, NULL)) {
1967 err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP);
1968 }
1969 return EXIT_SUCCESS;
1970 }
1971
1972 #ifdef EXTENSIONS
1973 #define GROUP_MOD_OPT_EXTENSIONS "v"
1974 #else
1975 #define GROUP_MOD_OPT_EXTENSIONS
1976 #endif
1977
1978 /* modify a group */
1979 int
1980 groupmod(int argc, char **argv)
1981 {
1982 struct group *grp;
1983 char buf[MaxEntryLen];
1984 char *newname;
1985 char **cpp;
1986 int dupgid;
1987 int gid;
1988 int cc;
1989 int c;
1990
1991 gid = -1;
1992 dupgid = 0;
1993 newname = NULL;
1994 while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) {
1995 switch(c) {
1996 case 'g':
1997 if (!is_number(optarg)) {
1998 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
1999 }
2000 gid = atoi(optarg);
2001 break;
2002 case 'o':
2003 dupgid = 1;
2004 break;
2005 case 'n':
2006 memsave(&newname, optarg, strlen(optarg));
2007 break;
2008 #ifdef EXTENSIONS
2009 case 'v':
2010 verbose = 1;
2011 break;
2012 #endif
2013 default:
2014 usermgmt_usage("groupmod");
2015 /* NOTREACHED */
2016 }
2017 }
2018 argc -= optind;
2019 argv += optind;
2020 if (argc != 1) {
2021 usermgmt_usage("groupmod");
2022 }
2023 checkeuid();
2024 if (gid < 0 && newname == NULL) {
2025 err(EXIT_FAILURE, "Nothing to change");
2026 }
2027 if (dupgid && gid < 0) {
2028 err(EXIT_FAILURE, "Duplicate which gid?");
2029 }
2030 if ((grp = getgrnam(*argv)) == NULL) {
2031 err(EXIT_FAILURE, "can't find group `%s' to modify", *argv);
2032 }
2033 if (!is_local(*argv, _PATH_GROUP)) {
2034 errx(EXIT_FAILURE, "Group `%s' must be a local group", *argv);
2035 }
2036 if (newname != NULL && !valid_group(newname)) {
2037 warn("warning - invalid group name `%s'", newname);
2038 }
2039 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:",
2040 (newname) ? newname : grp->gr_name,
2041 grp->gr_passwd,
2042 (gid < 0) ? grp->gr_gid : gid);
2043 for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) {
2044 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp,
2045 (cpp[1] == NULL) ? "" : ",");
2046 }
2047 cc += snprintf(&buf[cc], sizeof(buf) - cc, "\n");
2048 openlog("groupmod", LOG_PID, LOG_USER);
2049 if (!modify_gid(*argv, buf)) {
2050 err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP);
2051 }
2052 return EXIT_SUCCESS;
2053 }
2054
2055 #ifdef EXTENSIONS
2056 /* display user information */
2057 int
2058 userinfo(int argc, char **argv)
2059 {
2060 struct passwd *pwp;
2061 struct group *grp;
2062 char buf[MaxEntryLen];
2063 char **cpp;
2064 int exists;
2065 int cc;
2066 int i;
2067
2068 exists = 0;
2069 while ((i = getopt(argc, argv, "ev")) != -1) {
2070 switch(i) {
2071 case 'e':
2072 exists = 1;
2073 break;
2074 case 'v':
2075 verbose = 1;
2076 break;
2077 default:
2078 usermgmt_usage("userinfo");
2079 /* NOTREACHED */
2080 }
2081 }
2082 argc -= optind;
2083 argv += optind;
2084 if (argc != 1) {
2085 usermgmt_usage("userinfo");
2086 }
2087 pwp = find_user_info(*argv);
2088 if (exists) {
2089 exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE);
2090 }
2091 if (pwp == NULL) {
2092 errx(EXIT_FAILURE, "can't find user `%s'", *argv);
2093 }
2094 (void) printf("login\t%s\n", pwp->pw_name);
2095 (void) printf("passwd\t%s\n", pwp->pw_passwd);
2096 (void) printf("uid\t%d\n", pwp->pw_uid);
2097 for (cc = 0 ; (grp = getgrent()) != NULL ; ) {
2098 for (cpp = grp->gr_mem ; *cpp ; cpp++) {
2099 if (strcmp(*cpp, *argv) == 0 && grp->gr_gid != pwp->pw_gid) {
2100 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s ", grp->gr_name);
2101 }
2102 }
2103 }
2104 if ((grp = getgrgid(pwp->pw_gid)) == NULL) {
2105 (void) printf("groups\t%d %s\n", pwp->pw_gid, buf);
2106 } else {
2107 (void) printf("groups\t%s %s\n", grp->gr_name, buf);
2108 }
2109 (void) printf("change\t%s", pwp->pw_change ? ctime(&pwp->pw_change) : "NEVER\n");
2110 #ifdef EXTENSIONS
2111 (void) printf("class\t%s\n", pwp->pw_class);
2112 #endif
2113 (void) printf("gecos\t%s\n", pwp->pw_gecos);
2114 (void) printf("dir\t%s\n", pwp->pw_dir);
2115 (void) printf("shell\t%s\n", pwp->pw_shell);
2116 (void) printf("expire\t%s", pwp->pw_expire ? ctime(&pwp->pw_expire) : "NEVER\n");
2117 return EXIT_SUCCESS;
2118 }
2119 #endif
2120
2121 #ifdef EXTENSIONS
2122 /* display user information */
2123 int
2124 groupinfo(int argc, char **argv)
2125 {
2126 struct group *grp;
2127 char **cpp;
2128 int exists;
2129 int i;
2130
2131 exists = 0;
2132 while ((i = getopt(argc, argv, "ev")) != -1) {
2133 switch(i) {
2134 case 'e':
2135 exists = 1;
2136 break;
2137 case 'v':
2138 verbose = 1;
2139 break;
2140 default:
2141 usermgmt_usage("groupinfo");
2142 /* NOTREACHED */
2143 }
2144 }
2145 argc -= optind;
2146 argv += optind;
2147 if (argc != 1) {
2148 usermgmt_usage("groupinfo");
2149 }
2150 grp = find_group_info(*argv);
2151 if (exists) {
2152 exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE);
2153 }
2154 if (grp == NULL) {
2155 errx(EXIT_FAILURE, "can't find group `%s'", *argv);
2156 }
2157 (void) printf("name\t%s\n", grp->gr_name);
2158 (void) printf("passwd\t%s\n", grp->gr_passwd);
2159 (void) printf("gid\t%d\n", grp->gr_gid);
2160 (void) printf("members\t");
2161 for (cpp = grp->gr_mem ; *cpp ; cpp++) {
2162 (void) printf("%s ", *cpp);
2163 }
2164 (void) fputc('\n', stdout);
2165 return EXIT_SUCCESS;
2166 }
2167 #endif
2168