user.c revision 1.16 1 /* $NetBSD: user.c,v 1.16 2000/03/31 04:09:04 soren 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.16 2000/03/31 04:09:04 soren 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) != NULL) {
374 cc = strlen(buf);
375 if ((colon = strchr(buf, ':')) == 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 == 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 == 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)) != 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 = 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 (up->u_homeset) {
714 (void) strlcpy(home, up->u_home, sizeof(home));
715 } else {
716 /* if home directory hasn't been given, make it up */
717 (void) snprintf(home, sizeof(home), "%s/%s", up->u_basedir, login);
718 }
719 expire = 0;
720 if (up->u_expire != NULL) {
721 (void) memset(&tm, 0, sizeof(tm));
722 if (strptime(up->u_expire, "%c", &tm) == NULL) {
723 warnx("invalid time format `%s'", optarg);
724 } else {
725 expire = mktime(&tm);
726 }
727 }
728 password[PasswordLength] = '\0';
729 if (up->u_password != NULL &&
730 strlen(up->u_password) == PasswordLength) {
731 (void) memcpy(password, up->u_password, PasswordLength);
732 } else {
733 (void) memset(password, '*', PasswordLength);
734 if (up->u_password != NULL) {
735 warnx("Password `%s' is invalid: setting it to `%s'",
736 up->u_password, password);
737 }
738 }
739 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d::%d:%ld:%s:%s:%s\n",
740 login,
741 password,
742 up->u_uid,
743 gid,
744 up->u_inactive,
745 (long) expire,
746 up->u_comment,
747 home,
748 up->u_shell);
749 if (write(ptmpfd, buf, (size_t) cc) != cc) {
750 (void) close(ptmpfd);
751 (void) pw_abort();
752 err(EXIT_FAILURE, "can't add `%s'", buf);
753 }
754 if (up->u_mkdir) {
755 if (lstat(home, &st) < 0 && asystem("%s -p %s", MKDIR, home) != 0) {
756 (void) close(ptmpfd);
757 (void) pw_abort();
758 err(EXIT_FAILURE, "can't mkdir `%s'", home);
759 }
760 (void) copydotfiles(up->u_skeldir, up->u_uid, gid, home);
761 }
762 if (strcmp(up->u_primgrp, "=uid") == 0 &&
763 getgrnam(login) == (struct group *) NULL &&
764 !creategid(login, gid, login)) {
765 (void) close(ptmpfd);
766 (void) pw_abort();
767 err(EXIT_FAILURE, "can't create gid %d for login name %s", gid, login);
768 }
769 (void) close(ptmpfd);
770 if (pw_mkdb() < 0) {
771 err(EXIT_FAILURE, "pw_mkdb failed");
772 }
773 return 1;
774 }
775
776 /* modify a user */
777 static int
778 moduser(char *login, char *newlogin, user_t *up)
779 {
780 struct passwd *pwp;
781 struct group *grp;
782 struct tm tm;
783 time_t expire;
784 size_t loginc;
785 size_t colonc;
786 FILE *master;
787 char password[PasswordLength + 1];
788 char oldhome[MaxFileNameLen];
789 char home[MaxFileNameLen];
790 char buf[MaxFileNameLen];
791 char *colon;
792 int masterfd;
793 int ptmpfd;
794 int gid;
795 int cc;
796
797 if (!valid_login(newlogin)) {
798 errx(EXIT_FAILURE, "`%s' is not a valid login name", login);
799 }
800 if ((pwp = getpwnam(login)) == (struct passwd *) NULL) {
801 errx(EXIT_FAILURE, "No such user `%s'", login);
802 }
803 if ((masterfd = open(MASTER, O_RDONLY)) < 0) {
804 err(EXIT_FAILURE, "can't open `%s'", MASTER);
805 }
806 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
807 err(EXIT_FAILURE, "can't lock `%s'", MASTER);
808 }
809 pw_init();
810 if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
811 (void) close(masterfd);
812 err(EXIT_FAILURE, "can't obtain pw_lock");
813 }
814 if ((master = fdopen(masterfd, "r")) == (FILE *) NULL) {
815 (void) close(masterfd);
816 (void) close(ptmpfd);
817 (void) pw_abort();
818 err(EXIT_FAILURE, "can't fdopen fd for %s", MASTER);
819 }
820 if (up != (user_t *) NULL) {
821 if (up->u_mkdir) {
822 (void) strcpy(oldhome, pwp->pw_dir);
823 }
824 if (up->u_uid == -1) {
825 up->u_uid = pwp->pw_uid;
826 }
827 /* if -g=uid was specified, check gid is unused */
828 if (strcmp(up->u_primgrp, "=uid") == 0) {
829 if (getgrgid((gid_t)(up->u_uid)) != (struct group *) NULL) {
830 (void) close(ptmpfd);
831 (void) pw_abort();
832 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
833 }
834 gid = up->u_uid;
835 } else if ((grp = getgrnam(up->u_primgrp)) != (struct group *) NULL) {
836 gid = grp->gr_gid;
837 } else if (is_number(up->u_primgrp) &&
838 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != (struct group *) NULL) {
839 gid = grp->gr_gid;
840 } else {
841 (void) close(ptmpfd);
842 (void) pw_abort();
843 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
844 }
845 /* if changing name, check new name isn't already in use */
846 if (strcmp(login, newlogin) != 0 && getpwnam(newlogin) != (struct passwd *) NULL) {
847 (void) close(ptmpfd);
848 (void) pw_abort();
849 errx(EXIT_FAILURE, "already a `%s' user", newlogin);
850 }
851 /* if home directory hasn't been given, use the old one */
852 if (!up->u_homeset) {
853 (void) strcpy(home, pwp->pw_dir);
854 }
855 expire = 0;
856 if (up->u_expire != NULL) {
857 (void) memset(&tm, 0, sizeof(tm));
858 if (strptime(up->u_expire, "%c", &tm) == NULL) {
859 warnx("invalid time format `%s'", optarg);
860 } else {
861 expire = mktime(&tm);
862 }
863 }
864 password[PasswordLength] = '\0';
865 if (up->u_password != NULL &&
866 strlen(up->u_password) == PasswordLength) {
867 (void) memcpy(password, up->u_password, PasswordLength);
868 } else {
869 (void) memcpy(password, pwp->pw_passwd, PasswordLength);
870 }
871 if (strcmp(up->u_comment, DEF_COMMENT) == 0) {
872 memsave(&up->u_comment, pwp->pw_gecos, strlen(pwp->pw_gecos));
873 }
874 if (strcmp(up->u_shell, DEF_SHELL) == 0 && strcmp(pwp->pw_shell, DEF_SHELL) != 0) {
875 memsave(&up->u_comment, pwp->pw_shell, strlen(pwp->pw_shell));
876 }
877 }
878 loginc = strlen(login);
879 while (fgets(buf, sizeof(buf), master) != NULL) {
880 cc = strlen(buf);
881 if ((colon = strchr(buf, ':')) == NULL) {
882 warnx("Malformed entry `%s'. Skipping", buf);
883 continue;
884 }
885 colonc = (size_t)(colon - buf);
886 if (strncmp(login, buf, loginc) == 0 && loginc == colonc) {
887 if (up != (user_t *) NULL) {
888 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d::%d:%ld:%s:%s:%s\n",
889 newlogin,
890 password,
891 up->u_uid,
892 gid,
893 up->u_inactive,
894 (long) expire,
895 up->u_comment,
896 home,
897 up->u_shell);
898 if (write(ptmpfd, buf, (size_t) cc) != cc) {
899 (void) close(ptmpfd);
900 (void) pw_abort();
901 err(EXIT_FAILURE, "can't add `%s'", buf);
902 }
903 }
904 } else if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
905 (void) close(masterfd);
906 (void) close(ptmpfd);
907 (void) pw_abort();
908 err(EXIT_FAILURE, "short write to /etc/ptmp (not %d chars)", cc);
909 }
910 }
911 if (up != (user_t *) NULL &&
912 up->u_mkdir &&
913 asystem("%s %s %s", MV, oldhome, home) != 0) {
914 (void) close(ptmpfd);
915 (void) pw_abort();
916 err(EXIT_FAILURE, "can't move `%s' to `%s'", oldhome, home);
917 }
918 (void) close(ptmpfd);
919 if (pw_mkdb() < 0) {
920 err(EXIT_FAILURE, "pw_mkdb failed");
921 }
922 return 1;
923 }
924
925
926 #ifdef EXTENSIONS
927 /* see if we can find out the user struct */
928 static struct passwd *
929 find_user_info(char *name)
930 {
931 struct passwd *pwp;
932
933 if ((pwp = getpwnam(name)) != (struct passwd *) NULL) {
934 return pwp;
935 }
936 if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != (struct passwd *) NULL) {
937 return pwp;
938 }
939 return (struct passwd *) NULL;
940 }
941 #endif
942
943 #ifdef EXTENSIONS
944 /* see if we can find out the group struct */
945 static struct group *
946 find_group_info(char *name)
947 {
948 struct group *grp;
949
950 if ((grp = getgrnam(name)) != (struct group *) NULL) {
951 return grp;
952 }
953 if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != (struct group *) NULL) {
954 return grp;
955 }
956 return (struct group *) NULL;
957 }
958 #endif
959
960 /* print out usage message, and then exit */
961 void
962 usermgmt_usage(char *prog)
963 {
964 if (strcmp(prog, "useradd") == 0) {
965 (void) fprintf(stderr, "Usage: %s -D [-b basedir] [-e expiry] "
966 "[-f inactive] [-g group] [-r lowuid..highuid] [-s shell]"
967 "\n", prog);
968 (void) fprintf(stderr, "Usage: %s [-G group] [-b basedir] "
969 "[-c comment] [-d homedir] [-e expiry] [-f inactive]\n"
970 "\t[-g group] [-k skeletondir] [-m] [-o] [-p password] "
971 "[-r lowuid..highuid] [-s shell]\n\t[-u uid] [-v] user\n",
972 prog);
973 } else if (strcmp(prog, "usermod") == 0) {
974 (void) fprintf(stderr, "Usage: %s [-G group] [-c comment] "
975 "[-d homedir] [-e expire] [-f inactive] [-g group] "
976 "[-l newname] [-m] [-o] [-p password] [-s shell] [-u uid] "
977 "[-v] user\n", prog);
978 } else if (strcmp(prog, "userdel") == 0) {
979 (void) fprintf(stderr, "Usage: %s -D [-p preserve]\n", prog);
980 (void) fprintf(stderr, "Usage: %s [-p preserve] [-r] [-v] "
981 "user\n", prog);
982 #ifdef EXTENSIONS
983 } else if (strcmp(prog, "userinfo") == 0) {
984 (void) fprintf(stderr, "Usage: %s [-e] [-v] user\n", prog);
985 #endif
986 } else if (strcmp(prog, "groupadd") == 0) {
987 (void) fprintf(stderr, "Usage: %s [-g gid] [-o] [-v] group\n",
988 prog);
989 } else if (strcmp(prog, "groupdel") == 0) {
990 (void) fprintf(stderr, "Usage: %s [-v] group\n", prog);
991 } else if (strcmp(prog, "groupmod") == 0) {
992 (void) fprintf(stderr, "Usage: %s [-g gid] [-o] [-n newname] "
993 "[-v] group\n", prog);
994 } else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) {
995 (void) fprintf(stderr, "Usage: %s ( add | del | mod ) ...\n",
996 prog);
997 #ifdef EXTENSIONS
998 } else if (strcmp(prog, "groupinfo") == 0) {
999 (void) fprintf(stderr, "Usage: %s [-e] [-v] group\n", prog);
1000 #endif
1001 }
1002 exit(EXIT_FAILURE);
1003 /* NOTREACHED */
1004 }
1005
1006 extern int optind;
1007 extern char *optarg;
1008
1009 #ifdef EXTENSIONS
1010 #define ADD_OPT_EXTENSIONS "p:r:v"
1011 #else
1012 #define ADD_OPT_EXTENSIONS
1013 #endif
1014
1015 int
1016 useradd(int argc, char **argv)
1017 {
1018 user_t u;
1019 int defaultfield;
1020 int bigD;
1021 int c;
1022 int i;
1023
1024 (void) memset(&u, 0, sizeof(u));
1025 read_defaults(&u);
1026 u.u_uid = -1;
1027 defaultfield = bigD = 0;
1028 while ((c = getopt(argc, argv, "DG:b:c:d:e:f:g:k:mou:s:" ADD_OPT_EXTENSIONS)) != -1) {
1029 switch(c) {
1030 case 'D':
1031 bigD = 1;
1032 break;
1033 case 'G':
1034 memsave(&u.u_groupv[u.u_groupc++], optarg, strlen(optarg));
1035 break;
1036 case 'b':
1037 defaultfield = 1;
1038 memsave(&u.u_basedir, optarg, strlen(optarg));
1039 break;
1040 case 'c':
1041 memsave(&u.u_comment, optarg, strlen(optarg));
1042 break;
1043 case 'd':
1044 u.u_homeset = 1;
1045 memsave(&u.u_home, optarg, strlen(optarg));
1046 break;
1047 case 'e':
1048 defaultfield = 1;
1049 memsave(&u.u_expire, optarg, strlen(optarg));
1050 break;
1051 case 'f':
1052 defaultfield = 1;
1053 u.u_inactive = atoi(optarg);
1054 break;
1055 case 'g':
1056 defaultfield = 1;
1057 memsave(&u.u_primgrp, optarg, strlen(optarg));
1058 break;
1059 case 'k':
1060 memsave(&u.u_skeldir, optarg, strlen(optarg));
1061 break;
1062 case 'm':
1063 u.u_mkdir = 1;
1064 break;
1065 case 'o':
1066 u.u_dupuid = 1;
1067 break;
1068 #ifdef EXTENSIONS
1069 case 'p':
1070 memsave(&u.u_password, optarg, strlen(optarg));
1071 break;
1072 #endif
1073 #ifdef EXTENSIONS
1074 case 'r':
1075 defaultfield = 1;
1076 (void) save_range(&u, optarg);
1077 break;
1078 #endif
1079 case 's':
1080 defaultfield = 1;
1081 memsave(&u.u_shell, optarg, strlen(optarg));
1082 break;
1083 case 'u':
1084 if (!is_number(optarg)) {
1085 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
1086 }
1087 u.u_uid = atoi(optarg);
1088 break;
1089 #ifdef EXTENSIONS
1090 case 'v':
1091 verbose = 1;
1092 break;
1093 #endif
1094 }
1095 }
1096 if (bigD) {
1097 if (defaultfield) {
1098 checkeuid();
1099 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1100 }
1101 (void) printf("group\t\t%s\n", u.u_primgrp);
1102 (void) printf("base_dir\t%s\n", u.u_basedir);
1103 (void) printf("skel_dir\t%s\n", u.u_skeldir);
1104 (void) printf("shell\t\t%s\n", u.u_shell);
1105 (void) printf("inactive\t%d\n", u.u_inactive);
1106 (void) printf("expire\t\t%s\n", (u.u_expire == NULL) ? UNSET_EXPIRY : u.u_expire);
1107 #ifdef EXTENSIONS
1108 for (i = 0 ; i < u.u_rc ; i++) {
1109 (void) printf("range\t\t%d..%d\n", u.u_rv[i].r_from, u.u_rv[i].r_to);
1110 }
1111 #endif
1112 return EXIT_SUCCESS;
1113 }
1114 if (argc == optind) {
1115 usermgmt_usage("useradd");
1116 }
1117 checkeuid();
1118 return adduser(argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1119 }
1120
1121 #ifdef EXTENSIONS
1122 #define MOD_OPT_EXTENSIONS "p:v"
1123 #else
1124 #define MOD_OPT_EXTENSIONS
1125 #endif
1126
1127 int
1128 usermod(int argc, char **argv)
1129 {
1130 user_t u;
1131 char newuser[MaxUserNameLen + 1];
1132 int have_new_user;
1133 int c;
1134
1135 (void) memset(&u, 0, sizeof(u));
1136 (void) memset(newuser, 0, sizeof(newuser));
1137 read_defaults(&u);
1138 u.u_uid = -1;
1139 have_new_user = 0;
1140 while ((c = getopt(argc, argv, "G:c:d:e:f:g:l:mos:u:" MOD_OPT_EXTENSIONS)) != -1) {
1141 switch(c) {
1142 case 'G':
1143 memsave(&u.u_groupv[u.u_groupc++], optarg, strlen(optarg));
1144 break;
1145 case 'c':
1146 memsave(&u.u_comment, optarg, strlen(optarg));
1147 break;
1148 case 'd':
1149 u.u_homeset = 1;
1150 memsave(&u.u_home, optarg, strlen(optarg));
1151 break;
1152 case 'e':
1153 memsave(&u.u_expire, optarg, strlen(optarg));
1154 break;
1155 case 'f':
1156 u.u_inactive = atoi(optarg);
1157 break;
1158 case 'g':
1159 memsave(&u.u_primgrp, optarg, strlen(optarg));
1160 break;
1161 case 'l':
1162 have_new_user = 1;
1163 (void) strlcpy(newuser, optarg, sizeof(newuser));
1164 break;
1165 case 'm':
1166 u.u_mkdir = 1;
1167 break;
1168 case 'o':
1169 u.u_dupuid = 1;
1170 break;
1171 #ifdef EXTENSIONS
1172 case 'p':
1173 memsave(&u.u_password, optarg, strlen(optarg));
1174 break;
1175 #endif
1176 case 's':
1177 memsave(&u.u_shell, optarg, strlen(optarg));
1178 break;
1179 case 'u':
1180 if (!is_number(optarg)) {
1181 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
1182 }
1183 u.u_uid = atoi(optarg);
1184 break;
1185 #ifdef EXTENSIONS
1186 case 'v':
1187 verbose = 1;
1188 break;
1189 #endif
1190 }
1191 }
1192 if (argc == optind) {
1193 usermgmt_usage("usermod");
1194 }
1195 checkeuid();
1196 return moduser(argv[optind], (have_new_user) ? newuser : argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1197 }
1198
1199 #ifdef EXTENSIONS
1200 #define DEL_OPT_EXTENSIONS "Dp:v"
1201 #else
1202 #define DEL_OPT_EXTENSIONS
1203 #endif
1204
1205 int
1206 userdel(int argc, char **argv)
1207 {
1208 struct passwd *pwp;
1209 struct stat st;
1210 user_t u;
1211 char password[PasswordLength + 1];
1212 int defaultfield;
1213 int rmhome;
1214 int bigD;
1215 int c;
1216
1217 (void) memset(&u, 0, sizeof(u));
1218 read_defaults(&u);
1219 defaultfield = bigD = rmhome = 0;
1220 while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) {
1221 switch(c) {
1222 #ifdef EXTENSIONS
1223 case 'D':
1224 bigD = 1;
1225 break;
1226 #endif
1227 #ifdef EXTENSIONS
1228 case 'p':
1229 defaultfield = 1;
1230 u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 :
1231 (strcmp(optarg, "yes") == 0) ? 1 :
1232 atoi(optarg);
1233 break;
1234 #endif
1235 case 'r':
1236 rmhome = 1;
1237 break;
1238 #ifdef EXTENSIONS
1239 case 'v':
1240 verbose = 1;
1241 break;
1242 #endif
1243 }
1244 }
1245 #ifdef EXTENSIONS
1246 if (bigD) {
1247 if (defaultfield) {
1248 checkeuid();
1249 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1250 }
1251 (void) printf("preserve\t%s\n", (u.u_preserve) ? "true" : "false");
1252 return EXIT_SUCCESS;
1253 }
1254 #endif
1255 if (argc == optind) {
1256 usermgmt_usage("userdel");
1257 }
1258 checkeuid();
1259 if ((pwp = getpwnam(argv[optind])) == (struct passwd *) NULL) {
1260 warnx("No such user `%s'", argv[optind]);
1261 return EXIT_FAILURE;
1262 }
1263 if (rmhome) {
1264 if (stat(pwp->pw_dir, &st) < 0) {
1265 warn("Home directory `%s' does not exist", pwp->pw_dir);
1266 return EXIT_FAILURE;
1267 }
1268 (void) asystem("%s -rf %s", RM, pwp->pw_dir);
1269 }
1270 if (u.u_preserve) {
1271 memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN));
1272 (void) memset(password, '*', PasswordLength);
1273 password[PasswordLength] = '\0';
1274 memsave(&u.u_password, password, PasswordLength);
1275 return moduser(argv[optind], argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1276 }
1277 return moduser(argv[optind], argv[optind], (user_t *) NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
1278 }
1279
1280 #ifdef EXTENSIONS
1281 #define GROUP_ADD_OPT_EXTENSIONS "v"
1282 #else
1283 #define GROUP_ADD_OPT_EXTENSIONS
1284 #endif
1285
1286 /* add a group */
1287 int
1288 groupadd(int argc, char **argv)
1289 {
1290 int dupgid;
1291 int gid;
1292 int c;
1293
1294 gid = -1;
1295 dupgid = 0;
1296 while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) {
1297 switch(c) {
1298 case 'g':
1299 if (!is_number(optarg)) {
1300 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
1301 }
1302 gid = atoi(optarg);
1303 break;
1304 case 'o':
1305 dupgid = 1;
1306 break;
1307 #ifdef EXTENSIONS
1308 case 'v':
1309 verbose = 1;
1310 break;
1311 #endif
1312 }
1313 }
1314 if (argc == optind) {
1315 usermgmt_usage("groupadd");
1316 }
1317 checkeuid();
1318 if (gid < 0 && !getnextgid(&gid, LowGid, HighGid)) {
1319 err(EXIT_FAILURE, "can't add group: can't get next gid");
1320 }
1321 if (!dupgid && getgrgid((gid_t) gid) != (struct group *) NULL) {
1322 errx(EXIT_FAILURE, "can't add group: gid %d is a duplicate", gid);
1323 }
1324 if (!valid_group(argv[optind])) {
1325 warnx("warning - invalid group name `%s'", argv[optind]);
1326 }
1327 if (!creategid(argv[optind], gid, "")) {
1328 err(EXIT_FAILURE, "can't add group: problems with %s file", ETCGROUP);
1329 }
1330 return EXIT_SUCCESS;
1331 }
1332
1333 #ifdef EXTENSIONS
1334 #define GROUP_DEL_OPT_EXTENSIONS "v"
1335 #else
1336 #define GROUP_DEL_OPT_EXTENSIONS
1337 #endif
1338
1339 /* remove a group */
1340 int
1341 groupdel(int argc, char **argv)
1342 {
1343 int c;
1344
1345 while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) {
1346 switch(c) {
1347 #ifdef EXTENSIONS
1348 case 'v':
1349 verbose = 1;
1350 break;
1351 #endif
1352 }
1353 }
1354 if (argc == optind) {
1355 usermgmt_usage("groupdel");
1356 }
1357 checkeuid();
1358 if (!modify_gid(argv[optind], NULL)) {
1359 err(EXIT_FAILURE, "can't change %s file", ETCGROUP);
1360 }
1361 return EXIT_SUCCESS;
1362 }
1363
1364 #ifdef EXTENSIONS
1365 #define GROUP_MOD_OPT_EXTENSIONS "v"
1366 #else
1367 #define GROUP_MOD_OPT_EXTENSIONS
1368 #endif
1369
1370 /* modify a group */
1371 int
1372 groupmod(int argc, char **argv)
1373 {
1374 struct group *grp;
1375 char buf[MaxEntryLen];
1376 char *newname;
1377 char **cpp;
1378 int dupgid;
1379 int gid;
1380 int cc;
1381 int c;
1382
1383 gid = -1;
1384 dupgid = 0;
1385 newname = NULL;
1386 while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) {
1387 switch(c) {
1388 case 'g':
1389 if (!is_number(optarg)) {
1390 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
1391 }
1392 gid = atoi(optarg);
1393 break;
1394 case 'o':
1395 dupgid = 1;
1396 break;
1397 case 'n':
1398 memsave(&newname, optarg, strlen(optarg));
1399 break;
1400 #ifdef EXTENSIONS
1401 case 'v':
1402 verbose = 1;
1403 break;
1404 #endif
1405 }
1406 }
1407 if (argc == optind) {
1408 usermgmt_usage("groupmod");
1409 }
1410 checkeuid();
1411 if (gid < 0 && newname == NULL) {
1412 err(EXIT_FAILURE, "Nothing to change");
1413 }
1414 if (dupgid && gid < 0) {
1415 err(EXIT_FAILURE, "Duplicate which gid?");
1416 }
1417 if ((grp = getgrnam(argv[optind])) == (struct group *) NULL) {
1418 err(EXIT_FAILURE, "can't find group `%s' to modify", argv[optind]);
1419 }
1420 if (newname != NULL && !valid_group(newname)) {
1421 warn("warning - invalid group name `%s'", newname);
1422 }
1423 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:",
1424 (newname) ? newname : grp->gr_name,
1425 grp->gr_passwd,
1426 (gid < 0) ? grp->gr_gid : gid);
1427 for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) {
1428 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp,
1429 (cpp[1] == NULL) ? "" : ",");
1430 }
1431 if (!modify_gid(argv[optind], buf)) {
1432 err(EXIT_FAILURE, "can't change %s file", ETCGROUP);
1433 }
1434 return EXIT_SUCCESS;
1435 }
1436
1437 #ifdef EXTENSIONS
1438 /* display user information */
1439 int
1440 userinfo(int argc, char **argv)
1441 {
1442 struct passwd *pwp;
1443 struct group *grp;
1444 char buf[MaxEntryLen];
1445 char **cpp;
1446 int exists;
1447 int cc;
1448 int i;
1449
1450 exists = 0;
1451 while ((i = getopt(argc, argv, "ev")) != -1) {
1452 switch(i) {
1453 case 'e':
1454 exists = 1;
1455 break;
1456 case 'v':
1457 verbose = 1;
1458 break;
1459 }
1460 }
1461 if (argc == optind) {
1462 usermgmt_usage("userinfo");
1463 }
1464 pwp = find_user_info(argv[optind]);
1465 if (exists) {
1466 exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE);
1467 }
1468 if (pwp == (struct passwd *) NULL) {
1469 errx(EXIT_FAILURE, "can't find user `%s'", argv[optind]);
1470 }
1471 (void) printf("login\t%s\n", pwp->pw_name);
1472 (void) printf("passwd\t%s\n", pwp->pw_passwd);
1473 (void) printf("uid\t%d\n", pwp->pw_uid);
1474 for (cc = 0 ; (grp = getgrent()) != (struct group *) NULL ; ) {
1475 for (cpp = grp->gr_mem ; *cpp ; cpp++) {
1476 if (strcmp(*cpp, argv[optind]) == 0 && grp->gr_gid != pwp->pw_gid) {
1477 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s ", grp->gr_name);
1478 }
1479 }
1480 }
1481 if ((grp = getgrgid(pwp->pw_gid)) == (struct group *) NULL) {
1482 (void) printf("groups\t%d %s\n", pwp->pw_gid, buf);
1483 } else {
1484 (void) printf("groups\t%s %s\n", grp->gr_name, buf);
1485 }
1486 (void) printf("change\t%s", ctime(&pwp->pw_change));
1487 (void) printf("class\t%s\n", pwp->pw_class);
1488 (void) printf("gecos\t%s\n", pwp->pw_gecos);
1489 (void) printf("dir\t%s\n", pwp->pw_dir);
1490 (void) printf("shell\t%s\n", pwp->pw_shell);
1491 (void) printf("expire\t%s", ctime(&pwp->pw_expire));
1492 return EXIT_SUCCESS;
1493 }
1494 #endif
1495
1496 #ifdef EXTENSIONS
1497 /* display user information */
1498 int
1499 groupinfo(int argc, char **argv)
1500 {
1501 struct group *grp;
1502 char **cpp;
1503 int exists;
1504 int i;
1505
1506 exists = 0;
1507 while ((i = getopt(argc, argv, "ev")) != -1) {
1508 switch(i) {
1509 case 'e':
1510 exists = 1;
1511 break;
1512 case 'v':
1513 verbose = 1;
1514 break;
1515 }
1516 }
1517 if (argc == optind) {
1518 usermgmt_usage("groupinfo");
1519 }
1520 grp = find_group_info(argv[optind]);
1521 if (exists) {
1522 exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE);
1523 }
1524 if (grp == (struct group *) NULL) {
1525 errx(EXIT_FAILURE, "can't find group `%s'", argv[optind]);
1526 }
1527 (void) printf("name\t%s\n", grp->gr_name);
1528 (void) printf("passwd\t%s\n", grp->gr_passwd);
1529 (void) printf("gid\t%d\n", grp->gr_gid);
1530 (void) printf("members\t");
1531 for (cpp = grp->gr_mem ; *cpp ; cpp++) {
1532 (void) printf("%s ", *cpp);
1533 }
1534 (void) fputc('\n', stdout);
1535 return EXIT_SUCCESS;
1536 }
1537 #endif
1538