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