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