user.c revision 1.19 1 /* $NetBSD: user.c,v 1.19 2000/05/09 09:25:21 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.19 2000/05/09 09:25:21 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) != 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 #ifdef EXTENSIONS
1007 #define ADD_OPT_EXTENSIONS "p:r:v"
1008 #else
1009 #define ADD_OPT_EXTENSIONS
1010 #endif
1011
1012 int
1013 useradd(int argc, char **argv)
1014 {
1015 user_t u;
1016 int defaultfield;
1017 int bigD;
1018 int c;
1019 int i;
1020
1021 (void) memset(&u, 0, sizeof(u));
1022 read_defaults(&u);
1023 u.u_uid = -1;
1024 defaultfield = bigD = 0;
1025 while ((c = getopt(argc, argv, "DG:b:c:d:e:f:g:k:mou:s:" ADD_OPT_EXTENSIONS)) != -1) {
1026 switch(c) {
1027 case 'D':
1028 bigD = 1;
1029 break;
1030 case 'G':
1031 memsave(&u.u_groupv[u.u_groupc++], optarg, strlen(optarg));
1032 break;
1033 case 'b':
1034 defaultfield = 1;
1035 memsave(&u.u_basedir, optarg, strlen(optarg));
1036 break;
1037 case 'c':
1038 memsave(&u.u_comment, optarg, strlen(optarg));
1039 break;
1040 case 'd':
1041 u.u_homeset = 1;
1042 memsave(&u.u_home, optarg, strlen(optarg));
1043 break;
1044 case 'e':
1045 defaultfield = 1;
1046 memsave(&u.u_expire, optarg, strlen(optarg));
1047 break;
1048 case 'f':
1049 defaultfield = 1;
1050 u.u_inactive = atoi(optarg);
1051 break;
1052 case 'g':
1053 defaultfield = 1;
1054 memsave(&u.u_primgrp, optarg, strlen(optarg));
1055 break;
1056 case 'k':
1057 memsave(&u.u_skeldir, optarg, strlen(optarg));
1058 break;
1059 case 'm':
1060 u.u_mkdir = 1;
1061 break;
1062 case 'o':
1063 u.u_dupuid = 1;
1064 break;
1065 #ifdef EXTENSIONS
1066 case 'p':
1067 memsave(&u.u_password, optarg, strlen(optarg));
1068 break;
1069 #endif
1070 #ifdef EXTENSIONS
1071 case 'r':
1072 defaultfield = 1;
1073 (void) save_range(&u, optarg);
1074 break;
1075 #endif
1076 case 's':
1077 defaultfield = 1;
1078 memsave(&u.u_shell, optarg, strlen(optarg));
1079 break;
1080 case 'u':
1081 if (!is_number(optarg)) {
1082 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
1083 }
1084 u.u_uid = atoi(optarg);
1085 break;
1086 #ifdef EXTENSIONS
1087 case 'v':
1088 verbose = 1;
1089 break;
1090 #endif
1091 }
1092 }
1093 if (bigD) {
1094 if (defaultfield) {
1095 checkeuid();
1096 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1097 }
1098 (void) printf("group\t\t%s\n", u.u_primgrp);
1099 (void) printf("base_dir\t%s\n", u.u_basedir);
1100 (void) printf("skel_dir\t%s\n", u.u_skeldir);
1101 (void) printf("shell\t\t%s\n", u.u_shell);
1102 (void) printf("inactive\t%d\n", u.u_inactive);
1103 (void) printf("expire\t\t%s\n", (u.u_expire == NULL) ? UNSET_EXPIRY : u.u_expire);
1104 #ifdef EXTENSIONS
1105 for (i = 0 ; i < u.u_rc ; i++) {
1106 (void) printf("range\t\t%d..%d\n", u.u_rv[i].r_from, u.u_rv[i].r_to);
1107 }
1108 #endif
1109 return EXIT_SUCCESS;
1110 }
1111 if (argc == optind) {
1112 usermgmt_usage("useradd");
1113 }
1114 checkeuid();
1115 return adduser(argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1116 }
1117
1118 #ifdef EXTENSIONS
1119 #define MOD_OPT_EXTENSIONS "p:v"
1120 #else
1121 #define MOD_OPT_EXTENSIONS
1122 #endif
1123
1124 int
1125 usermod(int argc, char **argv)
1126 {
1127 user_t u;
1128 char newuser[MaxUserNameLen + 1];
1129 int have_new_user;
1130 int c;
1131
1132 (void) memset(&u, 0, sizeof(u));
1133 (void) memset(newuser, 0, sizeof(newuser));
1134 read_defaults(&u);
1135 u.u_uid = -1;
1136 have_new_user = 0;
1137 while ((c = getopt(argc, argv, "G:c:d:e:f:g:l:mos:u:" MOD_OPT_EXTENSIONS)) != -1) {
1138 switch(c) {
1139 case 'G':
1140 memsave(&u.u_groupv[u.u_groupc++], optarg, strlen(optarg));
1141 break;
1142 case 'c':
1143 memsave(&u.u_comment, optarg, strlen(optarg));
1144 break;
1145 case 'd':
1146 u.u_homeset = 1;
1147 memsave(&u.u_home, optarg, strlen(optarg));
1148 break;
1149 case 'e':
1150 memsave(&u.u_expire, optarg, strlen(optarg));
1151 break;
1152 case 'f':
1153 u.u_inactive = atoi(optarg);
1154 break;
1155 case 'g':
1156 memsave(&u.u_primgrp, optarg, strlen(optarg));
1157 break;
1158 case 'l':
1159 have_new_user = 1;
1160 (void) strlcpy(newuser, optarg, sizeof(newuser));
1161 break;
1162 case 'm':
1163 u.u_mkdir = 1;
1164 break;
1165 case 'o':
1166 u.u_dupuid = 1;
1167 break;
1168 #ifdef EXTENSIONS
1169 case 'p':
1170 memsave(&u.u_password, optarg, strlen(optarg));
1171 break;
1172 #endif
1173 case 's':
1174 memsave(&u.u_shell, optarg, strlen(optarg));
1175 break;
1176 case 'u':
1177 if (!is_number(optarg)) {
1178 errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
1179 }
1180 u.u_uid = atoi(optarg);
1181 break;
1182 #ifdef EXTENSIONS
1183 case 'v':
1184 verbose = 1;
1185 break;
1186 #endif
1187 }
1188 }
1189 if (argc == optind) {
1190 usermgmt_usage("usermod");
1191 }
1192 checkeuid();
1193 return moduser(argv[optind], (have_new_user) ? newuser : argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1194 }
1195
1196 #ifdef EXTENSIONS
1197 #define DEL_OPT_EXTENSIONS "Dp:v"
1198 #else
1199 #define DEL_OPT_EXTENSIONS
1200 #endif
1201
1202 int
1203 userdel(int argc, char **argv)
1204 {
1205 struct passwd *pwp;
1206 struct stat st;
1207 user_t u;
1208 char password[PasswordLength + 1];
1209 int defaultfield;
1210 int rmhome;
1211 int bigD;
1212 int c;
1213
1214 (void) memset(&u, 0, sizeof(u));
1215 read_defaults(&u);
1216 defaultfield = bigD = rmhome = 0;
1217 while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) {
1218 switch(c) {
1219 #ifdef EXTENSIONS
1220 case 'D':
1221 bigD = 1;
1222 break;
1223 #endif
1224 #ifdef EXTENSIONS
1225 case 'p':
1226 defaultfield = 1;
1227 u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 :
1228 (strcmp(optarg, "yes") == 0) ? 1 :
1229 atoi(optarg);
1230 break;
1231 #endif
1232 case 'r':
1233 rmhome = 1;
1234 break;
1235 #ifdef EXTENSIONS
1236 case 'v':
1237 verbose = 1;
1238 break;
1239 #endif
1240 }
1241 }
1242 #ifdef EXTENSIONS
1243 if (bigD) {
1244 if (defaultfield) {
1245 checkeuid();
1246 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1247 }
1248 (void) printf("preserve\t%s\n", (u.u_preserve) ? "true" : "false");
1249 return EXIT_SUCCESS;
1250 }
1251 #endif
1252 if (argc == optind) {
1253 usermgmt_usage("userdel");
1254 }
1255 checkeuid();
1256 if ((pwp = getpwnam(argv[optind])) == (struct passwd *) NULL) {
1257 warnx("No such user `%s'", argv[optind]);
1258 return EXIT_FAILURE;
1259 }
1260 if (rmhome) {
1261 if (stat(pwp->pw_dir, &st) < 0) {
1262 warn("Home directory `%s' does not exist", pwp->pw_dir);
1263 return EXIT_FAILURE;
1264 }
1265 (void) asystem("%s -rf %s", RM, pwp->pw_dir);
1266 }
1267 if (u.u_preserve) {
1268 memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN));
1269 (void) memset(password, '*', PasswordLength);
1270 password[PasswordLength] = '\0';
1271 memsave(&u.u_password, password, PasswordLength);
1272 return moduser(argv[optind], argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1273 }
1274 return moduser(argv[optind], argv[optind], (user_t *) NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
1275 }
1276
1277 #ifdef EXTENSIONS
1278 #define GROUP_ADD_OPT_EXTENSIONS "v"
1279 #else
1280 #define GROUP_ADD_OPT_EXTENSIONS
1281 #endif
1282
1283 /* add a group */
1284 int
1285 groupadd(int argc, char **argv)
1286 {
1287 int dupgid;
1288 int gid;
1289 int c;
1290
1291 gid = -1;
1292 dupgid = 0;
1293 while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) {
1294 switch(c) {
1295 case 'g':
1296 if (!is_number(optarg)) {
1297 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
1298 }
1299 gid = atoi(optarg);
1300 break;
1301 case 'o':
1302 dupgid = 1;
1303 break;
1304 #ifdef EXTENSIONS
1305 case 'v':
1306 verbose = 1;
1307 break;
1308 #endif
1309 }
1310 }
1311 if (argc == optind) {
1312 usermgmt_usage("groupadd");
1313 }
1314 checkeuid();
1315 if (gid < 0 && !getnextgid(&gid, LowGid, HighGid)) {
1316 err(EXIT_FAILURE, "can't add group: can't get next gid");
1317 }
1318 if (!dupgid && getgrgid((gid_t) gid) != (struct group *) NULL) {
1319 errx(EXIT_FAILURE, "can't add group: gid %d is a duplicate", gid);
1320 }
1321 if (!valid_group(argv[optind])) {
1322 warnx("warning - invalid group name `%s'", argv[optind]);
1323 }
1324 if (!creategid(argv[optind], gid, "")) {
1325 err(EXIT_FAILURE, "can't add group: problems with %s file", ETCGROUP);
1326 }
1327 return EXIT_SUCCESS;
1328 }
1329
1330 #ifdef EXTENSIONS
1331 #define GROUP_DEL_OPT_EXTENSIONS "v"
1332 #else
1333 #define GROUP_DEL_OPT_EXTENSIONS
1334 #endif
1335
1336 /* remove a group */
1337 int
1338 groupdel(int argc, char **argv)
1339 {
1340 int c;
1341
1342 while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) {
1343 switch(c) {
1344 #ifdef EXTENSIONS
1345 case 'v':
1346 verbose = 1;
1347 break;
1348 #endif
1349 }
1350 }
1351 if (argc == optind) {
1352 usermgmt_usage("groupdel");
1353 }
1354 checkeuid();
1355 if (!modify_gid(argv[optind], NULL)) {
1356 err(EXIT_FAILURE, "can't change %s file", ETCGROUP);
1357 }
1358 return EXIT_SUCCESS;
1359 }
1360
1361 #ifdef EXTENSIONS
1362 #define GROUP_MOD_OPT_EXTENSIONS "v"
1363 #else
1364 #define GROUP_MOD_OPT_EXTENSIONS
1365 #endif
1366
1367 /* modify a group */
1368 int
1369 groupmod(int argc, char **argv)
1370 {
1371 struct group *grp;
1372 char buf[MaxEntryLen];
1373 char *newname;
1374 char **cpp;
1375 int dupgid;
1376 int gid;
1377 int cc;
1378 int c;
1379
1380 gid = -1;
1381 dupgid = 0;
1382 newname = NULL;
1383 while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) {
1384 switch(c) {
1385 case 'g':
1386 if (!is_number(optarg)) {
1387 errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
1388 }
1389 gid = atoi(optarg);
1390 break;
1391 case 'o':
1392 dupgid = 1;
1393 break;
1394 case 'n':
1395 memsave(&newname, optarg, strlen(optarg));
1396 break;
1397 #ifdef EXTENSIONS
1398 case 'v':
1399 verbose = 1;
1400 break;
1401 #endif
1402 }
1403 }
1404 if (argc == optind) {
1405 usermgmt_usage("groupmod");
1406 }
1407 checkeuid();
1408 if (gid < 0 && newname == NULL) {
1409 err(EXIT_FAILURE, "Nothing to change");
1410 }
1411 if (dupgid && gid < 0) {
1412 err(EXIT_FAILURE, "Duplicate which gid?");
1413 }
1414 if ((grp = getgrnam(argv[optind])) == (struct group *) NULL) {
1415 err(EXIT_FAILURE, "can't find group `%s' to modify", argv[optind]);
1416 }
1417 if (newname != NULL && !valid_group(newname)) {
1418 warn("warning - invalid group name `%s'", newname);
1419 }
1420 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:",
1421 (newname) ? newname : grp->gr_name,
1422 grp->gr_passwd,
1423 (gid < 0) ? grp->gr_gid : gid);
1424 for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) {
1425 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp,
1426 (cpp[1] == NULL) ? "" : ",");
1427 }
1428 if (!modify_gid(argv[optind], buf)) {
1429 err(EXIT_FAILURE, "can't change %s file", ETCGROUP);
1430 }
1431 return EXIT_SUCCESS;
1432 }
1433
1434 #ifdef EXTENSIONS
1435 /* display user information */
1436 int
1437 userinfo(int argc, char **argv)
1438 {
1439 struct passwd *pwp;
1440 struct group *grp;
1441 char buf[MaxEntryLen];
1442 char **cpp;
1443 int exists;
1444 int cc;
1445 int i;
1446
1447 exists = 0;
1448 while ((i = getopt(argc, argv, "ev")) != -1) {
1449 switch(i) {
1450 case 'e':
1451 exists = 1;
1452 break;
1453 case 'v':
1454 verbose = 1;
1455 break;
1456 }
1457 }
1458 if (argc == optind) {
1459 usermgmt_usage("userinfo");
1460 }
1461 pwp = find_user_info(argv[optind]);
1462 if (exists) {
1463 exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE);
1464 }
1465 if (pwp == (struct passwd *) NULL) {
1466 errx(EXIT_FAILURE, "can't find user `%s'", argv[optind]);
1467 }
1468 (void) printf("login\t%s\n", pwp->pw_name);
1469 (void) printf("passwd\t%s\n", pwp->pw_passwd);
1470 (void) printf("uid\t%d\n", pwp->pw_uid);
1471 for (cc = 0 ; (grp = getgrent()) != (struct group *) NULL ; ) {
1472 for (cpp = grp->gr_mem ; *cpp ; cpp++) {
1473 if (strcmp(*cpp, argv[optind]) == 0 && grp->gr_gid != pwp->pw_gid) {
1474 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s ", grp->gr_name);
1475 }
1476 }
1477 }
1478 if ((grp = getgrgid(pwp->pw_gid)) == (struct group *) NULL) {
1479 (void) printf("groups\t%d %s\n", pwp->pw_gid, buf);
1480 } else {
1481 (void) printf("groups\t%s %s\n", grp->gr_name, buf);
1482 }
1483 (void) printf("change\t%s", ctime(&pwp->pw_change));
1484 (void) printf("class\t%s\n", pwp->pw_class);
1485 (void) printf("gecos\t%s\n", pwp->pw_gecos);
1486 (void) printf("dir\t%s\n", pwp->pw_dir);
1487 (void) printf("shell\t%s\n", pwp->pw_shell);
1488 (void) printf("expire\t%s", ctime(&pwp->pw_expire));
1489 return EXIT_SUCCESS;
1490 }
1491 #endif
1492
1493 #ifdef EXTENSIONS
1494 /* display user information */
1495 int
1496 groupinfo(int argc, char **argv)
1497 {
1498 struct group *grp;
1499 char **cpp;
1500 int exists;
1501 int i;
1502
1503 exists = 0;
1504 while ((i = getopt(argc, argv, "ev")) != -1) {
1505 switch(i) {
1506 case 'e':
1507 exists = 1;
1508 break;
1509 case 'v':
1510 verbose = 1;
1511 break;
1512 }
1513 }
1514 if (argc == optind) {
1515 usermgmt_usage("groupinfo");
1516 }
1517 grp = find_group_info(argv[optind]);
1518 if (exists) {
1519 exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE);
1520 }
1521 if (grp == (struct group *) NULL) {
1522 errx(EXIT_FAILURE, "can't find group `%s'", argv[optind]);
1523 }
1524 (void) printf("name\t%s\n", grp->gr_name);
1525 (void) printf("passwd\t%s\n", grp->gr_passwd);
1526 (void) printf("gid\t%d\n", grp->gr_gid);
1527 (void) printf("members\t");
1528 for (cpp = grp->gr_mem ; *cpp ; cpp++) {
1529 (void) printf("%s ", *cpp);
1530 }
1531 (void) fputc('\n', stdout);
1532 return EXIT_SUCCESS;
1533 }
1534 #endif
1535