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