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