user.c revision 1.18 1 /* $NetBSD: user.c,v 1.18 2000/05/05 01:27:47 hubertf 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(
37 "@(#) Copyright (c) 1999 \
38 The NetBSD Foundation, Inc. All rights reserved.");
39 __RCSID("$NetBSD: user.c,v 1.18 2000/05/05 01:27:47 hubertf Exp $");
40 #endif
41
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/stat.h>
45
46 #include <ctype.h>
47 #include <dirent.h>
48 #include <err.h>
49 #include <fcntl.h>
50 #include <grp.h>
51 #include <pwd.h>
52 #include <stdarg.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <time.h>
57 #include <unistd.h>
58 #include <util.h>
59
60 #include "defs.h"
61 #include "usermgmt.h"
62
63 /* this struct describes a uid range */
64 typedef struct range_t {
65 int r_from; /* low uid */
66 int r_to; /* high uid */
67 } range_t;
68
69 /* this struct encapsulates the user information */
70 typedef struct user_t {
71 int u_uid; /* uid of user */
72 char *u_password; /* encrypted password */
73 char *u_comment; /* comment field */
74 int u_homeset; /* home dir has been set */
75 char *u_home; /* home directory */
76 char *u_primgrp; /* primary group */
77 int u_groupc; /* # of secondary groups */
78 char *u_groupv[NGROUPS_MAX]; /* secondary groups */
79 char *u_shell; /* user's shell */
80 char *u_basedir; /* base directory for home */
81 char *u_expire; /* when password will expire */
82 int u_inactive; /* inactive */
83 int u_mkdir; /* make the home directory */
84 int u_dupuid; /* duplicate uids are allowed */
85 char *u_skeldir; /* directory for startup files */
86 unsigned u_rsize; /* size of range array */
87 unsigned u_rc; /* # of ranges */
88 range_t *u_rv; /* the ranges */
89 unsigned u_defrc; /* # of ranges in defaults */
90 int u_preserve; /* preserve uids on deletion */
91 } user_t;
92
93 #define CONFFILE "/etc/usermgmt.conf"
94
95 #ifndef DEF_GROUP
96 #define DEF_GROUP "users"
97 #endif
98
99 #ifndef DEF_BASEDIR
100 #define DEF_BASEDIR "/home"
101 #endif
102
103 #ifndef DEF_SKELDIR
104 #define DEF_SKELDIR "/etc/skel"
105 #endif
106
107 #ifndef DEF_SHELL
108 #define DEF_SHELL "/bin/csh"
109 #endif
110
111 #ifndef DEF_COMMENT
112 #define DEF_COMMENT ""
113 #endif
114
115 #ifndef DEF_LOWUID
116 #define DEF_LOWUID 1000
117 #endif
118
119 #ifndef DEF_HIGHUID
120 #define DEF_HIGHUID 60000
121 #endif
122
123 #ifndef DEF_INACTIVE
124 #define DEF_INACTIVE 0
125 #endif
126
127 #ifndef DEF_EXPIRE
128 #define DEF_EXPIRE (char *) NULL
129 #endif
130
131 #ifndef MASTER
132 #define MASTER "/etc/master.passwd"
133 #endif
134
135 #ifndef ETCGROUP
136 #define ETCGROUP "/etc/group"
137 #endif
138
139 #ifndef WAITSECS
140 #define WAITSECS 10
141 #endif
142
143 #ifndef NOBODY_UID
144 #define NOBODY_UID 32767
145 #endif
146
147 /* some useful constants */
148 enum {
149 MaxShellNameLen = 256,
150 MaxFileNameLen = MAXPATHLEN,
151 MaxUserNameLen = 32,
152 MaxFieldNameLen = 32,
153 MaxCommandLen = 2048,
154 MaxEntryLen = 2048,
155 PasswordLength = 13,
156
157 LowGid = DEF_LOWUID,
158 HighGid = DEF_HIGHUID
159 };
160
161 /* Full paths of programs used here */
162 #define CHOWN "/usr/sbin/chown"
163 #define MKDIR "/bin/mkdir"
164 #define MV "/bin/mv"
165 #define NOLOGIN "/sbin/nologin"
166 #define PAX "/bin/pax"
167 #define RM "/bin/rm"
168
169 #define UNSET_EXPIRY "Null (unset)"
170
171 static int verbose;
172
173 /* if *cpp is non-null, free it, then assign `n' chars of `s' to it */
174 static void
175 memsave(char **cpp, char *s, size_t n)
176 {
177 if (*cpp != (char *) NULL) {
178 FREE(*cpp);
179 }
180 NEWARRAY(char, *cpp, n + 1, exit(1));
181 (void) memcpy(*cpp, s, n);
182 (*cpp)[n] = '\0';
183 }
184
185 /* a replacement for system(3) */
186 static int
187 asystem(char *fmt, ...)
188 {
189 va_list vp;
190 char buf[MaxCommandLen];
191 int ret;
192
193 va_start(vp, fmt);
194 (void) vsnprintf(buf, sizeof(buf), fmt, vp);
195 va_end(vp);
196 if (verbose) {
197 (void) printf("Command: %s\n", buf);
198 }
199 if ((ret = system(buf)) != 0) {
200 warnx("[Warning] can't system `%s'", buf);
201 }
202 return ret;
203 }
204
205 #ifdef EXTENSIONS
206 /* return 1 if all of `s' is numeric */
207 static int
208 is_number(char *s)
209 {
210 for ( ; *s ; s++) {
211 if (!isdigit(*s)) {
212 return 0;
213 }
214 }
215 return 1;
216 }
217 #endif
218
219 /*
220 * check that the effective uid is 0 - called from funcs which will
221 * modify data and config files.
222 */
223 static void
224 checkeuid(void)
225 {
226 if (geteuid() != 0) {
227 errx(EXIT_FAILURE, "Program must be run as root");
228 }
229 }
230
231 /* copy any dot files into the user's home directory */
232 static int
233 copydotfiles(char *skeldir, int uid, int gid, char *dir)
234 {
235 struct dirent *dp;
236 DIR *dirp;
237 int n;
238
239 if ((dirp = opendir(skeldir)) == (DIR *) NULL) {
240 warn("can't open source . files dir `%s'", skeldir);
241 return 0;
242 }
243 for (n = 0; (dp = readdir(dirp)) != (struct dirent *) NULL && n == 0 ; ) {
244 if (strcmp(dp->d_name, ".") == 0 ||
245 strcmp(dp->d_name, "..") == 0) {
246 continue;
247 }
248 n = 1;
249 }
250 (void) closedir(dirp);
251 if (n == 0) {
252 warnx("No \"dot\" initialisation files found");
253 } else {
254 (void) asystem("cd %s; %s -rw -pe %s . %s",
255 skeldir, PAX, (verbose) ? "-v" : "", dir);
256 }
257 (void) asystem("%s -R -h %d:%d %s", CHOWN, uid, gid, dir);
258 return n;
259 }
260
261 /* create a group entry with gid `gid' */
262 static int
263 creategid(char *group, int gid, char *name)
264 {
265 struct stat st;
266 FILE *from;
267 FILE *to;
268 char buf[MaxEntryLen];
269 char f[MaxFileNameLen];
270 int fd;
271 int cc;
272
273 if (getgrnam(group) != (struct group *) NULL) {
274 warnx("group `%s' already exists", group);
275 return 0;
276 }
277 if ((from = fopen(ETCGROUP, "r")) == (FILE *) NULL) {
278 warn("can't create gid for %s: can't open %s", name, ETCGROUP);
279 return 0;
280 }
281 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
282 warn("can't lock `%s'", ETCGROUP);
283 }
284 (void) fstat(fileno(from), &st);
285 (void) snprintf(f, sizeof(f), "%s.XXXXXX", ETCGROUP);
286 if ((fd = mkstemp(f)) < 0) {
287 (void) fclose(from);
288 warn("can't create gid: mkstemp failed");
289 return 0;
290 }
291 if ((to = fdopen(fd, "w")) == (FILE *) NULL) {
292 (void) fclose(from);
293 (void) close(fd);
294 (void) unlink(f);
295 warn("can't create gid: fdopen `%s' failed", f);
296 return 0;
297 }
298 while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) {
299 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
300 (void) fclose(from);
301 (void) close(fd);
302 (void) unlink(f);
303 warn("can't create gid: short write to `%s'", f);
304 return 0;
305 }
306 }
307 (void) fprintf(to, "%s:*:%d:%s\n", group, gid, name);
308 (void) fclose(from);
309 (void) fclose(to);
310 if (rename(f, ETCGROUP) < 0) {
311 warn("can't create gid: can't rename `%s' to `%s'", f, ETCGROUP);
312 return 0;
313 }
314 (void) chmod(ETCGROUP, st.st_mode & 07777);
315 return 1;
316 }
317
318 /* modify the group entry with name `group' to be newent */
319 static int
320 modify_gid(char *group, char *newent)
321 {
322 struct stat st;
323 FILE *from;
324 FILE *to;
325 char buf[MaxEntryLen];
326 char f[MaxFileNameLen];
327 char *colon;
328 int groupc;
329 int entc;
330 int fd;
331 int cc;
332
333 if ((from = fopen(ETCGROUP, "r")) == (FILE *) NULL) {
334 warn("can't create gid for %s: can't open %s", group, ETCGROUP);
335 return 0;
336 }
337 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
338 warn("can't lock `%s'", ETCGROUP);
339 }
340 (void) fstat(fileno(from), &st);
341 (void) snprintf(f, sizeof(f), "%s.XXXXXX", ETCGROUP);
342 if ((fd = mkstemp(f)) < 0) {
343 (void) fclose(from);
344 warn("can't create gid: mkstemp failed");
345 return 0;
346 }
347 if ((to = fdopen(fd, "w")) == (FILE *) NULL) {
348 (void) fclose(from);
349 (void) close(fd);
350 (void) unlink(f);
351 warn("can't create gid: fdopen `%s' failed", f);
352 return 0;
353 }
354 groupc = strlen(group);
355 while (fgets(buf, sizeof(buf), from) != NULL) {
356 cc = strlen(buf);
357 if ((colon = strchr(buf, ':')) == NULL) {
358 warn("badly formed entry `%s'", buf);
359 continue;
360 }
361 entc = (int)(colon - buf);
362 if (entc == groupc && strncmp(group, buf, (unsigned) entc) == 0) {
363 if (newent == NULL) {
364 continue;
365 } else {
366 cc = strlen(newent);
367 (void) strlcpy(buf, newent, sizeof(buf));
368 }
369 }
370 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
371 (void) fclose(from);
372 (void) close(fd);
373 (void) unlink(f);
374 warn("can't create gid: short write to `%s'", f);
375 return 0;
376 }
377 }
378 (void) fclose(from);
379 (void) fclose(to);
380 if (rename(f, ETCGROUP) < 0) {
381 warn("can't create gid: can't rename `%s' to `%s'", f, ETCGROUP);
382 return 0;
383 }
384 (void) chmod(ETCGROUP, st.st_mode & 07777);
385 return 1;
386 }
387
388 /* return 1 if `login' is a valid login name */
389 static int
390 valid_login(char *login)
391 {
392 char *cp;
393
394 for (cp = login ; *cp ; cp++) {
395 if (!isalnum(*cp) && *cp != '.' && *cp != '_' && *cp != '-') {
396 return 0;
397 }
398 }
399 return 1;
400 }
401
402 /* return 1 if `group' is a valid group name */
403 static int
404 valid_group(char *group)
405 {
406 char *cp;
407
408 for (cp = group ; *cp ; cp++) {
409 if (!isalnum(*cp)) {
410 return 0;
411 }
412 }
413 return 1;
414 }
415
416 /* find the next gid in the range lo .. hi */
417 static int
418 getnextgid(int *gidp, int lo, int hi)
419 {
420 for (*gidp = lo ; *gidp < hi ; *gidp += 1) {
421 if (getgrgid((gid_t)*gidp) == (struct group *) NULL) {
422 return 1;
423 }
424 }
425 return 0;
426 }
427
428 #ifdef EXTENSIONS
429 /* save a range of uids */
430 static int
431 save_range(user_t *up, char *cp)
432 {
433 int from;
434 int to;
435 int i;
436
437 if (up->u_rsize == 0) {
438 up->u_rsize = 32;
439 NEWARRAY(range_t, up->u_rv, up->u_rsize, return(0));
440 } else if (up->u_rc == up->u_rsize) {
441 up->u_rsize *= 2;
442 RENEW(range_t, up->u_rv, up->u_rsize, return(0));
443 }
444 if (up->u_rv && sscanf(cp, "%d..%d", &from, &to) == 2) {
445 for (i = 0 ; i < up->u_rc ; i++) {
446 if (up->u_rv[i].r_from == from && up->u_rv[i].r_to == to) {
447 break;
448 }
449 }
450 if (i == up->u_rc) {
451 up->u_rv[up->u_rc].r_from = from;
452 up->u_rv[up->u_rc].r_to = to;
453 up->u_rc += 1;
454 }
455 } else {
456 warnx("Bad range `%s'", cp);
457 return 0;
458 }
459 return 1;
460 }
461 #endif
462
463 /* set the defaults in the defaults file */
464 static int
465 setdefaults(user_t *up)
466 {
467 char template[MaxFileNameLen];
468 FILE *fp;
469 int ret;
470 int fd;
471 int i;
472
473 (void) snprintf(template, sizeof(template), "%s.XXXXXX", CONFFILE);
474 if ((fd = mkstemp(template)) < 0) {
475 warnx("can't mkstemp `%s' for writing", CONFFILE);
476 return 0;
477 }
478 if ((fp = fdopen(fd, "w")) == (FILE *) NULL) {
479 warn("can't fdopen `%s' for writing", CONFFILE);
480 return 0;
481 }
482 ret = 1;
483 if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 ||
484 fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 ||
485 fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 ||
486 fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 ||
487 fprintf(fp, "inactive\t%d\n", up->u_inactive) <= 0 ||
488 fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ? UNSET_EXPIRY : up->u_expire) <= 0) {
489 warn("can't write to `%s'", CONFFILE);
490 ret = 0;
491 }
492 #ifdef EXTENSIONS
493 for (i = (up->u_defrc != up->u_rc) ? up->u_defrc : 0 ; i < up->u_rc ; i++) {
494 if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from, up->u_rv[i].r_to) <= 0) {
495 warn("can't write to `%s'", CONFFILE);
496 ret = 0;
497 }
498 }
499 #endif
500 (void) fclose(fp);
501 if (ret) {
502 ret = ((rename(template, CONFFILE) == 0) && (chmod(CONFFILE, 0644) == 0));
503 }
504 return ret;
505 }
506
507 /* read the defaults file */
508 static void
509 read_defaults(user_t *up)
510 {
511 struct stat st;
512 size_t lineno;
513 size_t len;
514 FILE *fp;
515 char *cp;
516 char *s;
517
518 memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP));
519 memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR));
520 memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR));
521 memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL));
522 memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT));
523 up->u_rsize = 16;
524 NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1));
525 up->u_inactive = DEF_INACTIVE;
526 up->u_expire = DEF_EXPIRE;
527 if ((fp = fopen(CONFFILE, "r")) == (FILE *) NULL) {
528 if (stat(CONFFILE, &st) < 0 && !setdefaults(up)) {
529 warn("can't create `%s' defaults file", CONFFILE);
530 }
531 fp = fopen(CONFFILE, "r");
532 }
533 if (fp != (FILE *) NULL) {
534 while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) {
535 if (strncmp(s, "group", 5) == 0) {
536 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
537 }
538 memsave(&up->u_primgrp, cp, strlen(cp));
539 } else if (strncmp(s, "base_dir", 8) == 0) {
540 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
541 }
542 memsave(&up->u_basedir, cp, strlen(cp));
543 } else if (strncmp(s, "skel_dir", 8) == 0) {
544 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
545 }
546 memsave(&up->u_skeldir, cp, strlen(cp));
547 } else if (strncmp(s, "shell", 5) == 0) {
548 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
549 }
550 memsave(&up->u_shell, cp, strlen(cp));
551 } else if (strncmp(s, "inactive", 8) == 0) {
552 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
553 }
554 up->u_inactive = atoi(cp);
555 #ifdef EXTENSIONS
556 } else if (strncmp(s, "range", 5) == 0) {
557 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
558 }
559 (void) save_range(up, cp);
560 #endif
561 #ifdef EXTENSIONS
562 } else if (strncmp(s, "preserve", 8) == 0) {
563 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
564 }
565 up->u_preserve = (strncmp(cp, "true", 4) == 0) ? 1 :
566 (strncmp(cp, "yes", 3) == 0) ? 1 :
567 atoi(cp);
568 #endif
569 } else if (strncmp(s, "expire", 6) == 0) {
570 for (cp = s + 6 ; *cp && isspace(*cp) ; cp++) {
571 }
572 if (strcmp(cp, UNSET_EXPIRY) == 0) {
573 if (up->u_expire) {
574 FREE(up->u_expire);
575 }
576 up->u_expire = NULL;
577 } else {
578 memsave(&up->u_expire, cp, strlen(cp));
579 }
580 }
581 (void) free(s);
582 }
583 (void) fclose(fp);
584 }
585 if (up->u_rc == 0) {
586 up->u_rv[up->u_rc].r_from = DEF_LOWUID;
587 up->u_rv[up->u_rc].r_to = DEF_HIGHUID;
588 up->u_rc += 1;
589 }
590 up->u_defrc = up->u_rc;
591 }
592
593 /* return the next valid unused uid */
594 static int
595 getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid)
596 {
597 for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) {
598 if (getpwuid((uid_t)(*uid)) == (struct passwd *) NULL && *uid != NOBODY_UID) {
599 if (sync_uid_gid) {
600 if (getgrgid((gid_t)(*uid)) == (struct group *) NULL) {
601 return 1;
602 }
603 } else {
604 return 1;
605 }
606 }
607 }
608 return 0;
609 }
610
611 /* add a user */
612 static int
613 adduser(char *login, user_t *up)
614 {
615 struct group *grp;
616 struct stat st;
617 struct tm tm;
618 time_t expire;
619 char password[PasswordLength + 1];
620 char home[MaxFileNameLen];
621 char buf[MaxFileNameLen];
622 int sync_uid_gid;
623 int masterfd;
624 int ptmpfd;
625 int gid;
626 int cc;
627 int i;
628
629 if (!valid_login(login)) {
630 errx(EXIT_FAILURE, "`%s' is not a valid login name", login);
631 }
632 if ((masterfd = open(MASTER, O_RDONLY)) < 0) {
633 err(EXIT_FAILURE, "can't open `%s'", MASTER);
634 }
635 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
636 err(EXIT_FAILURE, "can't lock `%s'", MASTER);
637 }
638 pw_init();
639 if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
640 (void) close(masterfd);
641 err(EXIT_FAILURE, "can't obtain pw_lock");
642 }
643 while ((cc = read(masterfd, buf, sizeof(buf))) > 0) {
644 if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
645 (void) close(masterfd);
646 (void) close(ptmpfd);
647 (void) pw_abort();
648 err(EXIT_FAILURE, "short write to /etc/ptmp (not %d chars)", cc);
649 }
650 }
651 /* if no uid was specified, get next one in [low_uid..high_uid] range */
652 sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0);
653 if (up->u_uid == -1) {
654 for (i = 0 ; i < up->u_rc ; i++) {
655 if (getnextuid(sync_uid_gid, &up->u_uid, up->u_rv[i].r_from, up->u_rv[i].r_to)) {
656 break;
657 }
658 }
659 if (i == up->u_rc) {
660 (void) close(ptmpfd);
661 (void) pw_abort();
662 errx(EXIT_FAILURE, "can't get next uid for %d", up->u_uid);
663 }
664 }
665 /* check uid isn't already allocated */
666 if (!up->u_dupuid && getpwuid((uid_t)(up->u_uid)) != (struct passwd *) NULL) {
667 (void) close(ptmpfd);
668 (void) pw_abort();
669 errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid);
670 }
671 /* if -g=uid was specified, check gid is unused */
672 if (sync_uid_gid) {
673 if (getgrgid((gid_t)(up->u_uid)) != (struct group *) NULL) {
674 (void) close(ptmpfd);
675 (void) pw_abort();
676 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
677 }
678 gid = up->u_uid;
679 } else if ((grp = getgrnam(up->u_primgrp)) != (struct group *) NULL) {
680 gid = grp->gr_gid;
681 } else if (is_number(up->u_primgrp) &&
682 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != (struct group *) NULL) {
683 gid = grp->gr_gid;
684 } else {
685 (void) close(ptmpfd);
686 (void) pw_abort();
687 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
688 }
689 /* check name isn't already in use */
690 if (!up->u_dupuid && getpwnam(login) != (struct passwd *) NULL) {
691 (void) close(ptmpfd);
692 (void) pw_abort();
693 errx(EXIT_FAILURE, "already a `%s' user", login);
694 }
695 if (up->u_homeset) {
696 (void) strlcpy(home, up->u_home, sizeof(home));
697 } else {
698 /* if home directory hasn't been given, make it up */
699 (void) snprintf(home, sizeof(home), "%s/%s", up->u_basedir, login);
700 }
701 expire = 0;
702 if (up->u_expire != NULL) {
703 (void) memset(&tm, 0, sizeof(tm));
704 if (strptime(up->u_expire, "%c", &tm) == NULL) {
705 warnx("invalid time format `%s'", optarg);
706 } else {
707 expire = mktime(&tm);
708 }
709 }
710 password[PasswordLength] = '\0';
711 if (up->u_password != NULL &&
712 strlen(up->u_password) == PasswordLength) {
713 (void) memcpy(password, up->u_password, PasswordLength);
714 } else {
715 (void) memset(password, '*', PasswordLength);
716 if (up->u_password != NULL) {
717 warnx("Password `%s' is invalid: setting it to `%s'",
718 up->u_password, password);
719 }
720 }
721 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d::%d:%ld:%s:%s:%s\n",
722 login,
723 password,
724 up->u_uid,
725 gid,
726 up->u_inactive,
727 (long) expire,
728 up->u_comment,
729 home,
730 up->u_shell);
731 if (write(ptmpfd, buf, (size_t) cc) != cc) {
732 (void) close(ptmpfd);
733 (void) pw_abort();
734 err(EXIT_FAILURE, "can't add `%s'", buf);
735 }
736 if (up->u_mkdir) {
737 if (lstat(home, &st) < 0 && asystem("%s -p %s", MKDIR, home) != 0) {
738 (void) close(ptmpfd);
739 (void) pw_abort();
740 err(EXIT_FAILURE, "can't mkdir `%s'", home);
741 }
742 (void) copydotfiles(up->u_skeldir, up->u_uid, gid, home);
743 }
744 if (strcmp(up->u_primgrp, "=uid") == 0 &&
745 getgrnam(login) == (struct group *) NULL &&
746 !creategid(login, gid, login)) {
747 (void) close(ptmpfd);
748 (void) pw_abort();
749 err(EXIT_FAILURE, "can't create gid %d for login name %s", gid, login);
750 }
751 (void) close(ptmpfd);
752 if (pw_mkdb() < 0) {
753 err(EXIT_FAILURE, "pw_mkdb failed");
754 }
755 return 1;
756 }
757
758 /* modify a user */
759 static int
760 moduser(char *login, char *newlogin, user_t *up)
761 {
762 struct passwd *pwp;
763 struct group *grp;
764 struct tm tm;
765 time_t expire;
766 size_t loginc;
767 size_t colonc;
768 FILE *master;
769 char password[PasswordLength + 1];
770 char oldhome[MaxFileNameLen];
771 char home[MaxFileNameLen];
772 char buf[MaxFileNameLen];
773 char *colon;
774 int masterfd;
775 int ptmpfd;
776 int gid;
777 int cc;
778
779 if (!valid_login(newlogin)) {
780 errx(EXIT_FAILURE, "`%s' is not a valid login name", login);
781 }
782 if ((pwp = getpwnam(login)) == (struct passwd *) NULL) {
783 errx(EXIT_FAILURE, "No such user `%s'", login);
784 }
785 if ((masterfd = open(MASTER, O_RDONLY)) < 0) {
786 err(EXIT_FAILURE, "can't open `%s'", MASTER);
787 }
788 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
789 err(EXIT_FAILURE, "can't lock `%s'", MASTER);
790 }
791 pw_init();
792 if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
793 (void) close(masterfd);
794 err(EXIT_FAILURE, "can't obtain pw_lock");
795 }
796 if ((master = fdopen(masterfd, "r")) == (FILE *) NULL) {
797 (void) close(masterfd);
798 (void) close(ptmpfd);
799 (void) pw_abort();
800 err(EXIT_FAILURE, "can't fdopen fd for %s", MASTER);
801 }
802 if (up != (user_t *) NULL) {
803 if (up->u_mkdir) {
804 (void) strcpy(oldhome, pwp->pw_dir);
805 }
806 if (up->u_uid == -1) {
807 up->u_uid = pwp->pw_uid;
808 }
809 /* if -g=uid was specified, check gid is unused */
810 if (strcmp(up->u_primgrp, "=uid") == 0) {
811 if (getgrgid((gid_t)(up->u_uid)) != (struct group *) NULL) {
812 (void) close(ptmpfd);
813 (void) pw_abort();
814 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
815 }
816 gid = up->u_uid;
817 } else if ((grp = getgrnam(up->u_primgrp)) != (struct group *) NULL) {
818 gid = grp->gr_gid;
819 } else if (is_number(up->u_primgrp) &&
820 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != (struct group *) NULL) {
821 gid = grp->gr_gid;
822 } else {
823 (void) close(ptmpfd);
824 (void) pw_abort();
825 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
826 }
827 /* if changing name, check new name isn't already in use */
828 if (strcmp(login, newlogin) != 0 && getpwnam(newlogin) != (struct passwd *) NULL) {
829 (void) close(ptmpfd);
830 (void) pw_abort();
831 errx(EXIT_FAILURE, "already a `%s' user", newlogin);
832 }
833 /* if home directory hasn't been given, use the old one */
834 if (!up->u_homeset) {
835 (void) strcpy(home, pwp->pw_dir);
836 }
837 expire = 0;
838 if (up->u_expire != NULL) {
839 (void) memset(&tm, 0, sizeof(tm));
840 if (strptime(up->u_expire, "%c", &tm) == NULL) {
841 warnx("invalid time format `%s'", optarg);
842 } else {
843 expire = mktime(&tm);
844 }
845 }
846 password[PasswordLength] = '\0';
847 if (up->u_password != NULL &&
848 strlen(up->u_password) == PasswordLength) {
849 (void) memcpy(password, up->u_password, PasswordLength);
850 } else {
851 (void) memcpy(password, pwp->pw_passwd, PasswordLength);
852 }
853 if (strcmp(up->u_comment, DEF_COMMENT) == 0) {
854 memsave(&up->u_comment, pwp->pw_gecos, strlen(pwp->pw_gecos));
855 }
856 if (strcmp(up->u_shell, DEF_SHELL) == 0 && strcmp(pwp->pw_shell, DEF_SHELL) != 0) {
857 memsave(&up->u_comment, pwp->pw_shell, strlen(pwp->pw_shell));
858 }
859 }
860 loginc = strlen(login);
861 while (fgets(buf, sizeof(buf), master) != NULL) {
862 cc = strlen(buf);
863 if ((colon = strchr(buf, ':')) == NULL) {
864 warnx("Malformed entry `%s'. Skipping", buf);
865 continue;
866 }
867 colonc = (size_t)(colon - buf);
868 if (strncmp(login, buf, loginc) == 0 && loginc == colonc) {
869 if (up != (user_t *) NULL) {
870 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d::%d:%ld:%s:%s:%s\n",
871 newlogin,
872 password,
873 up->u_uid,
874 gid,
875 up->u_inactive,
876 (long) expire,
877 up->u_comment,
878 home,
879 up->u_shell);
880 if (write(ptmpfd, buf, (size_t) cc) != cc) {
881 (void) close(ptmpfd);
882 (void) pw_abort();
883 err(EXIT_FAILURE, "can't add `%s'", buf);
884 }
885 }
886 } else if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
887 (void) close(masterfd);
888 (void) close(ptmpfd);
889 (void) pw_abort();
890 err(EXIT_FAILURE, "short write to /etc/ptmp (not %d chars)", cc);
891 }
892 }
893 if (up != (user_t *) NULL &&
894 up->u_mkdir &&
895 asystem("%s %s %s", MV, oldhome, home) != 0) {
896 (void) close(ptmpfd);
897 (void) pw_abort();
898 err(EXIT_FAILURE, "can't move `%s' to `%s'", oldhome, home);
899 }
900 (void) close(ptmpfd);
901 if (pw_mkdb() < 0) {
902 err(EXIT_FAILURE, "pw_mkdb failed");
903 }
904 return 1;
905 }
906
907
908 #ifdef EXTENSIONS
909 /* see if we can find out the user struct */
910 static struct passwd *
911 find_user_info(char *name)
912 {
913 struct passwd *pwp;
914
915 if ((pwp = getpwnam(name)) != (struct passwd *) NULL) {
916 return pwp;
917 }
918 if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != (struct passwd *) NULL) {
919 return pwp;
920 }
921 return (struct passwd *) NULL;
922 }
923 #endif
924
925 #ifdef EXTENSIONS
926 /* see if we can find out the group struct */
927 static struct group *
928 find_group_info(char *name)
929 {
930 struct group *grp;
931
932 if ((grp = getgrnam(name)) != (struct group *) NULL) {
933 return grp;
934 }
935 if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != (struct group *) NULL) {
936 return grp;
937 }
938 return (struct group *) NULL;
939 }
940 #endif
941
942 /* print out usage message, and then exit */
943 void
944 usermgmt_usage(char *prog)
945 {
946 if (strcmp(prog, "useradd") == 0) {
947 (void) fprintf(stderr, "Usage: %s -D [-b basedir] [-e expiry] "
948 "[-f inactive] [-g group] [-r lowuid..highuid] [-s shell]"
949 "\n", prog);
950 (void) fprintf(stderr, "Usage: %s [-G group] [-b basedir] "
951 "[-c comment] [-d homedir] [-e expiry] [-f inactive]\n"
952 "\t[-g group] [-k skeletondir] [-m] [-o] [-p password] "
953 "[-r lowuid..highuid] [-s shell]\n\t[-u uid] [-v] user\n",
954 prog);
955 } else if (strcmp(prog, "usermod") == 0) {
956 (void) fprintf(stderr, "Usage: %s [-G group] [-c comment] "
957 "[-d homedir] [-e expire] [-f inactive] [-g group] "
958 "[-l newname] [-m] [-o] [-p password] [-s shell] [-u uid] "
959 "[-v] user\n", prog);
960 } else if (strcmp(prog, "userdel") == 0) {
961 (void) fprintf(stderr, "Usage: %s -D [-p preserve]\n", prog);
962 (void) fprintf(stderr, "Usage: %s [-p preserve] [-r] [-v] "
963 "user\n", prog);
964 #ifdef EXTENSIONS
965 } else if (strcmp(prog, "userinfo") == 0) {
966 (void) fprintf(stderr, "Usage: %s [-e] [-v] user\n", prog);
967 #endif
968 } else if (strcmp(prog, "groupadd") == 0) {
969 (void) fprintf(stderr, "Usage: %s [-g gid] [-o] [-v] group\n",
970 prog);
971 } else if (strcmp(prog, "groupdel") == 0) {
972 (void) fprintf(stderr, "Usage: %s [-v] group\n", prog);
973 } else if (strcmp(prog, "groupmod") == 0) {
974 (void) fprintf(stderr, "Usage: %s [-g gid] [-o] [-n newname] "
975 "[-v] group\n", prog);
976 } else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) {
977 (void) fprintf(stderr, "Usage: %s ( add | del | mod ) ...\n",
978 prog);
979 #ifdef EXTENSIONS
980 } else if (strcmp(prog, "groupinfo") == 0) {
981 (void) fprintf(stderr, "Usage: %s [-e] [-v] group\n", prog);
982 #endif
983 }
984 exit(EXIT_FAILURE);
985 /* NOTREACHED */
986 }
987
988 #ifdef EXTENSIONS
989 #define ADD_OPT_EXTENSIONS "p:r:v"
990 #else
991 #define ADD_OPT_EXTENSIONS
992 #endif
993
994 int
995 useradd(int argc, char **argv)
996 {
997 user_t u;
998 int defaultfield;
999 int bigD;
1000 int c;
1001 int i;
1002
1003 (void) memset(&u, 0, sizeof(u));
1004 read_defaults(&u);
1005 u.u_uid = -1;
1006 defaultfield = bigD = 0;
1007 while ((c = getopt(argc, argv, "DG:b:c:d:e:f:g:k:mou:s:" ADD_OPT_EXTENSIONS)) != -1) {
1008 switch(c) {
1009 case 'D':
1010 bigD = 1;
1011 break;
1012 case 'G':
1013 memsave(&u.u_groupv[u.u_groupc++], optarg, strlen(optarg));
1014 break;
1015 case 'b':
1016 defaultfield = 1;
1017 memsave(&u.u_basedir, optarg, strlen(optarg));
1018 break;
1019 case 'c':
1020 memsave(&u.u_comment, optarg, strlen(optarg));
1021 break;
1022 case 'd':
1023 u.u_homeset = 1;
1024 memsave(&u.u_home, optarg, strlen(optarg));
1025 break;
1026 case 'e':
1027 defaultfield = 1;
1028 memsave(&u.u_expire, optarg, strlen(optarg));
1029 break;
1030 case 'f':
1031 defaultfield = 1;
1032 u.u_inactive = atoi(optarg);
1033 break;
1034 case 'g':
1035 defaultfield = 1;
1036 memsave(&u.u_primgrp, optarg, strlen(optarg));
1037 break;
1038 case 'k':
1039 memsave(&u.u_skeldir, optarg, strlen(optarg));
1040 break;
1041 case 'm':
1042 u.u_mkdir = 1;
1043 break;
1044 case 'o':
1045 u.u_dupuid = 1;
1046 break;
1047 #ifdef EXTENSIONS
1048 case 'p':
1049 memsave(&u.u_password, optarg, strlen(optarg));
1050 break;
1051 #endif
1052 #ifdef EXTENSIONS
1053 case 'r':
1054 defaultfield = 1;
1055 (void) save_range(&u, optarg);
1056 break;
1057 #endif
1058 case 's':
1059 defaultfield = 1;
1060 memsave(&u.u_shell, optarg, strlen(optarg));
1061 break;
1062 case 'u':
1063 if (!is_number(optarg)) {
1064 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
1065 }
1066 u.u_uid = atoi(optarg);
1067 break;
1068 #ifdef EXTENSIONS
1069 case 'v':
1070 verbose = 1;
1071 break;
1072 #endif
1073 }
1074 }
1075 if (bigD) {
1076 if (defaultfield) {
1077 checkeuid();
1078 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1079 }
1080 (void) printf("group\t\t%s\n", u.u_primgrp);
1081 (void) printf("base_dir\t%s\n", u.u_basedir);
1082 (void) printf("skel_dir\t%s\n", u.u_skeldir);
1083 (void) printf("shell\t\t%s\n", u.u_shell);
1084 (void) printf("inactive\t%d\n", u.u_inactive);
1085 (void) printf("expire\t\t%s\n", (u.u_expire == NULL) ? UNSET_EXPIRY : u.u_expire);
1086 #ifdef EXTENSIONS
1087 for (i = 0 ; i < u.u_rc ; i++) {
1088 (void) printf("range\t\t%d..%d\n", u.u_rv[i].r_from, u.u_rv[i].r_to);
1089 }
1090 #endif
1091 return EXIT_SUCCESS;
1092 }
1093 if (argc == optind) {
1094 usermgmt_usage("useradd");
1095 }
1096 checkeuid();
1097 return adduser(argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1098 }
1099
1100 #ifdef EXTENSIONS
1101 #define MOD_OPT_EXTENSIONS "p:v"
1102 #else
1103 #define MOD_OPT_EXTENSIONS
1104 #endif
1105
1106 int
1107 usermod(int argc, char **argv)
1108 {
1109 user_t u;
1110 char newuser[MaxUserNameLen + 1];
1111 int have_new_user;
1112 int c;
1113
1114 (void) memset(&u, 0, sizeof(u));
1115 (void) memset(newuser, 0, sizeof(newuser));
1116 read_defaults(&u);
1117 u.u_uid = -1;
1118 have_new_user = 0;
1119 while ((c = getopt(argc, argv, "G:c:d:e:f:g:l:mos:u:" MOD_OPT_EXTENSIONS)) != -1) {
1120 switch(c) {
1121 case 'G':
1122 memsave(&u.u_groupv[u.u_groupc++], optarg, strlen(optarg));
1123 break;
1124 case 'c':
1125 memsave(&u.u_comment, optarg, strlen(optarg));
1126 break;
1127 case 'd':
1128 u.u_homeset = 1;
1129 memsave(&u.u_home, optarg, strlen(optarg));
1130 break;
1131 case 'e':
1132 memsave(&u.u_expire, optarg, strlen(optarg));
1133 break;
1134 case 'f':
1135 u.u_inactive = atoi(optarg);
1136 break;
1137 case 'g':
1138 memsave(&u.u_primgrp, optarg, strlen(optarg));
1139 break;
1140 case 'l':
1141 have_new_user = 1;
1142 (void) strlcpy(newuser, optarg, sizeof(newuser));
1143 break;
1144 case 'm':
1145 u.u_mkdir = 1;
1146 break;
1147 case 'o':
1148 u.u_dupuid = 1;
1149 break;
1150 #ifdef EXTENSIONS
1151 case 'p':
1152 memsave(&u.u_password, optarg, strlen(optarg));
1153 break;
1154 #endif
1155 case 's':
1156 memsave(&u.u_shell, optarg, strlen(optarg));
1157 break;
1158 case 'u':
1159 if (!is_number(optarg)) {
1160 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
1161 }
1162 u.u_uid = atoi(optarg);
1163 break;
1164 #ifdef EXTENSIONS
1165 case 'v':
1166 verbose = 1;
1167 break;
1168 #endif
1169 }
1170 }
1171 if (argc == optind) {
1172 usermgmt_usage("usermod");
1173 }
1174 checkeuid();
1175 return moduser(argv[optind], (have_new_user) ? newuser : argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1176 }
1177
1178 #ifdef EXTENSIONS
1179 #define DEL_OPT_EXTENSIONS "Dp:v"
1180 #else
1181 #define DEL_OPT_EXTENSIONS
1182 #endif
1183
1184 int
1185 userdel(int argc, char **argv)
1186 {
1187 struct passwd *pwp;
1188 struct stat st;
1189 user_t u;
1190 char password[PasswordLength + 1];
1191 int defaultfield;
1192 int rmhome;
1193 int bigD;
1194 int c;
1195
1196 (void) memset(&u, 0, sizeof(u));
1197 read_defaults(&u);
1198 defaultfield = bigD = rmhome = 0;
1199 while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) {
1200 switch(c) {
1201 #ifdef EXTENSIONS
1202 case 'D':
1203 bigD = 1;
1204 break;
1205 #endif
1206 #ifdef EXTENSIONS
1207 case 'p':
1208 defaultfield = 1;
1209 u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 :
1210 (strcmp(optarg, "yes") == 0) ? 1 :
1211 atoi(optarg);
1212 break;
1213 #endif
1214 case 'r':
1215 rmhome = 1;
1216 break;
1217 #ifdef EXTENSIONS
1218 case 'v':
1219 verbose = 1;
1220 break;
1221 #endif
1222 }
1223 }
1224 #ifdef EXTENSIONS
1225 if (bigD) {
1226 if (defaultfield) {
1227 checkeuid();
1228 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1229 }
1230 (void) printf("preserve\t%s\n", (u.u_preserve) ? "true" : "false");
1231 return EXIT_SUCCESS;
1232 }
1233 #endif
1234 if (argc == optind) {
1235 usermgmt_usage("userdel");
1236 }
1237 checkeuid();
1238 if ((pwp = getpwnam(argv[optind])) == (struct passwd *) NULL) {
1239 warnx("No such user `%s'", argv[optind]);
1240 return EXIT_FAILURE;
1241 }
1242 if (rmhome) {
1243 if (stat(pwp->pw_dir, &st) < 0) {
1244 warn("Home directory `%s' does not exist", pwp->pw_dir);
1245 return EXIT_FAILURE;
1246 }
1247 (void) asystem("%s -rf %s", RM, pwp->pw_dir);
1248 }
1249 if (u.u_preserve) {
1250 memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN));
1251 (void) memset(password, '*', PasswordLength);
1252 password[PasswordLength] = '\0';
1253 memsave(&u.u_password, password, PasswordLength);
1254 return moduser(argv[optind], argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1255 }
1256 return moduser(argv[optind], argv[optind], (user_t *) NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
1257 }
1258
1259 #ifdef EXTENSIONS
1260 #define GROUP_ADD_OPT_EXTENSIONS "v"
1261 #else
1262 #define GROUP_ADD_OPT_EXTENSIONS
1263 #endif
1264
1265 /* add a group */
1266 int
1267 groupadd(int argc, char **argv)
1268 {
1269 int dupgid;
1270 int gid;
1271 int c;
1272
1273 gid = -1;
1274 dupgid = 0;
1275 while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) {
1276 switch(c) {
1277 case 'g':
1278 if (!is_number(optarg)) {
1279 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
1280 }
1281 gid = atoi(optarg);
1282 break;
1283 case 'o':
1284 dupgid = 1;
1285 break;
1286 #ifdef EXTENSIONS
1287 case 'v':
1288 verbose = 1;
1289 break;
1290 #endif
1291 }
1292 }
1293 if (argc == optind) {
1294 usermgmt_usage("groupadd");
1295 }
1296 checkeuid();
1297 if (gid < 0 && !getnextgid(&gid, LowGid, HighGid)) {
1298 err(EXIT_FAILURE, "can't add group: can't get next gid");
1299 }
1300 if (!dupgid && getgrgid((gid_t) gid) != (struct group *) NULL) {
1301 errx(EXIT_FAILURE, "can't add group: gid %d is a duplicate", gid);
1302 }
1303 if (!valid_group(argv[optind])) {
1304 warnx("warning - invalid group name `%s'", argv[optind]);
1305 }
1306 if (!creategid(argv[optind], gid, "")) {
1307 err(EXIT_FAILURE, "can't add group: problems with %s file", ETCGROUP);
1308 }
1309 return EXIT_SUCCESS;
1310 }
1311
1312 #ifdef EXTENSIONS
1313 #define GROUP_DEL_OPT_EXTENSIONS "v"
1314 #else
1315 #define GROUP_DEL_OPT_EXTENSIONS
1316 #endif
1317
1318 /* remove a group */
1319 int
1320 groupdel(int argc, char **argv)
1321 {
1322 int c;
1323
1324 while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) {
1325 switch(c) {
1326 #ifdef EXTENSIONS
1327 case 'v':
1328 verbose = 1;
1329 break;
1330 #endif
1331 }
1332 }
1333 if (argc == optind) {
1334 usermgmt_usage("groupdel");
1335 }
1336 checkeuid();
1337 if (!modify_gid(argv[optind], NULL)) {
1338 err(EXIT_FAILURE, "can't change %s file", ETCGROUP);
1339 }
1340 return EXIT_SUCCESS;
1341 }
1342
1343 #ifdef EXTENSIONS
1344 #define GROUP_MOD_OPT_EXTENSIONS "v"
1345 #else
1346 #define GROUP_MOD_OPT_EXTENSIONS
1347 #endif
1348
1349 /* modify a group */
1350 int
1351 groupmod(int argc, char **argv)
1352 {
1353 struct group *grp;
1354 char buf[MaxEntryLen];
1355 char *newname;
1356 char **cpp;
1357 int dupgid;
1358 int gid;
1359 int cc;
1360 int c;
1361
1362 gid = -1;
1363 dupgid = 0;
1364 newname = NULL;
1365 while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) {
1366 switch(c) {
1367 case 'g':
1368 if (!is_number(optarg)) {
1369 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
1370 }
1371 gid = atoi(optarg);
1372 break;
1373 case 'o':
1374 dupgid = 1;
1375 break;
1376 case 'n':
1377 memsave(&newname, optarg, strlen(optarg));
1378 break;
1379 #ifdef EXTENSIONS
1380 case 'v':
1381 verbose = 1;
1382 break;
1383 #endif
1384 }
1385 }
1386 if (argc == optind) {
1387 usermgmt_usage("groupmod");
1388 }
1389 checkeuid();
1390 if (gid < 0 && newname == NULL) {
1391 err(EXIT_FAILURE, "Nothing to change");
1392 }
1393 if (dupgid && gid < 0) {
1394 err(EXIT_FAILURE, "Duplicate which gid?");
1395 }
1396 if ((grp = getgrnam(argv[optind])) == (struct group *) NULL) {
1397 err(EXIT_FAILURE, "can't find group `%s' to modify", argv[optind]);
1398 }
1399 if (newname != NULL && !valid_group(newname)) {
1400 warn("warning - invalid group name `%s'", newname);
1401 }
1402 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:",
1403 (newname) ? newname : grp->gr_name,
1404 grp->gr_passwd,
1405 (gid < 0) ? grp->gr_gid : gid);
1406 for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) {
1407 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp,
1408 (cpp[1] == NULL) ? "" : ",");
1409 }
1410 if (!modify_gid(argv[optind], buf)) {
1411 err(EXIT_FAILURE, "can't change %s file", ETCGROUP);
1412 }
1413 return EXIT_SUCCESS;
1414 }
1415
1416 #ifdef EXTENSIONS
1417 /* display user information */
1418 int
1419 userinfo(int argc, char **argv)
1420 {
1421 struct passwd *pwp;
1422 struct group *grp;
1423 char buf[MaxEntryLen];
1424 char **cpp;
1425 int exists;
1426 int cc;
1427 int i;
1428
1429 exists = 0;
1430 while ((i = getopt(argc, argv, "ev")) != -1) {
1431 switch(i) {
1432 case 'e':
1433 exists = 1;
1434 break;
1435 case 'v':
1436 verbose = 1;
1437 break;
1438 }
1439 }
1440 if (argc == optind) {
1441 usermgmt_usage("userinfo");
1442 }
1443 pwp = find_user_info(argv[optind]);
1444 if (exists) {
1445 exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE);
1446 }
1447 if (pwp == (struct passwd *) NULL) {
1448 errx(EXIT_FAILURE, "can't find user `%s'", argv[optind]);
1449 }
1450 (void) printf("login\t%s\n", pwp->pw_name);
1451 (void) printf("passwd\t%s\n", pwp->pw_passwd);
1452 (void) printf("uid\t%d\n", pwp->pw_uid);
1453 for (cc = 0 ; (grp = getgrent()) != (struct group *) NULL ; ) {
1454 for (cpp = grp->gr_mem ; *cpp ; cpp++) {
1455 if (strcmp(*cpp, argv[optind]) == 0 && grp->gr_gid != pwp->pw_gid) {
1456 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s ", grp->gr_name);
1457 }
1458 }
1459 }
1460 if ((grp = getgrgid(pwp->pw_gid)) == (struct group *) NULL) {
1461 (void) printf("groups\t%d %s\n", pwp->pw_gid, buf);
1462 } else {
1463 (void) printf("groups\t%s %s\n", grp->gr_name, buf);
1464 }
1465 (void) printf("change\t%s", ctime(&pwp->pw_change));
1466 (void) printf("class\t%s\n", pwp->pw_class);
1467 (void) printf("gecos\t%s\n", pwp->pw_gecos);
1468 (void) printf("dir\t%s\n", pwp->pw_dir);
1469 (void) printf("shell\t%s\n", pwp->pw_shell);
1470 (void) printf("expire\t%s", ctime(&pwp->pw_expire));
1471 return EXIT_SUCCESS;
1472 }
1473 #endif
1474
1475 #ifdef EXTENSIONS
1476 /* display user information */
1477 int
1478 groupinfo(int argc, char **argv)
1479 {
1480 struct group *grp;
1481 char **cpp;
1482 int exists;
1483 int i;
1484
1485 exists = 0;
1486 while ((i = getopt(argc, argv, "ev")) != -1) {
1487 switch(i) {
1488 case 'e':
1489 exists = 1;
1490 break;
1491 case 'v':
1492 verbose = 1;
1493 break;
1494 }
1495 }
1496 if (argc == optind) {
1497 usermgmt_usage("groupinfo");
1498 }
1499 grp = find_group_info(argv[optind]);
1500 if (exists) {
1501 exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE);
1502 }
1503 if (grp == (struct group *) NULL) {
1504 errx(EXIT_FAILURE, "can't find group `%s'", argv[optind]);
1505 }
1506 (void) printf("name\t%s\n", grp->gr_name);
1507 (void) printf("passwd\t%s\n", grp->gr_passwd);
1508 (void) printf("gid\t%d\n", grp->gr_gid);
1509 (void) printf("members\t");
1510 for (cpp = grp->gr_mem ; *cpp ; cpp++) {
1511 (void) printf("%s ", *cpp);
1512 }
1513 (void) fputc('\n', stdout);
1514 return EXIT_SUCCESS;
1515 }
1516 #endif
1517