user.c revision 1.2 1 /* $NetBSD: user.c,v 1.2 1999/12/06 22:29:02 simonb Exp $ */
2
3 /*
4 * Copyright 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/types.h>
34 #include <sys/param.h>
35 #include <sys/stat.h>
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <dirent.h>
41 #include <string.h>
42 #include <stdarg.h>
43 #include <ctype.h>
44 #include <fcntl.h>
45 #include <time.h>
46 #include <util.h>
47 #include <pwd.h>
48 #include <grp.h>
49 #include <err.h>
50
51 #include "defs.h"
52 #include "usermgmt.h"
53
54 static int verbose;
55
56 static int useradd __P((int, char **));
57 static int usermod __P((int, char **));
58 static int userdel __P((int, char **));
59 static int groupadd __P((int, char **));
60 static int groupdel __P((int, char **));
61 static int groupmod __P((int, char **));
62
63 /* if *cpp is non-null, free it, then assign `n' chars of `s' to it */
64 static void
65 memsave(char **cpp, char *s, size_t n)
66 {
67 if (*cpp != (char *) NULL) {
68 FREE(*cpp);
69 }
70 NEWARRAY(char, *cpp, n + 1, exit(1));
71 (void) memcpy(*cpp, s, n);
72 (*cpp)[n] = 0;
73 }
74
75 /* a replacement for system(3) */
76 static int
77 asystem(char *fmt, ...)
78 {
79 va_list vp;
80 char buf[MaxCommandLen];
81 int ret;
82
83 va_start(vp, fmt);
84 (void) vsnprintf(buf, sizeof(buf), fmt, vp);
85 va_end(vp);
86 if (verbose) {
87 (void) printf("Command: %s\n", buf);
88 }
89 if ((ret = system(buf)) != 0) {
90 warnx("[Warning] can't system `%s'", buf);
91 }
92 return ret;
93 }
94
95 /* bounds checking strncpy */
96 static char *
97 strnncpy(char *to, size_t tosize, char *from, size_t fromsize)
98 {
99 size_t n = MIN(tosize - 1, fromsize);
100
101 (void) memcpy(to, from, n);
102 to[n] = 0;
103 return to;
104 }
105
106 /* copy any dot files into the user's home directory */
107 static int
108 copydotfiles(char *skeldir, int uid, int gid, char *dir)
109 {
110 struct dirent *dp;
111 DIR *dirp;
112 int n;
113
114 if ((dirp = opendir(skeldir)) == (DIR *) NULL) {
115 warn("can't open source . files dir `%s'", skeldir);
116 return 0;
117 }
118 for (n = 0; (dp = readdir(dirp)) != (struct dirent *) NULL && n == 0 ; ) {
119 if (strcmp(dp->d_name, ".") == 0 ||
120 strcmp(dp->d_name, "..") == 0) {
121 continue;
122 }
123 if (dp->d_name[0] == '.' && isalnum(dp->d_name[1])) {
124 n = 1;
125 }
126 }
127 (void) closedir(dirp);
128 if (n == 0) {
129 warnx("No \"dot\" initialisation files found");
130 } else {
131 (void) asystem("%s -p -R %s/.[A-z]* %s", CP, skeldir, dir);
132 }
133 (void) asystem("%s -R %d:%d %s", CHOWN, uid, gid, dir);
134 return n;
135 }
136
137 /* create a group entry with gid `gid' */
138 static int
139 creategid(char *group, int gid, char *name)
140 {
141 struct stat st;
142 FILE *from;
143 FILE *to;
144 char buf[MaxEntryLen];
145 char f[MaxFileNameLen];
146 int fd;
147 int cc;
148
149 if ((from = fopen(ETCGROUP, "r")) == (FILE *) NULL) {
150 warn("can't create gid for %s: can't open %s", name, ETCGROUP);
151 return 0;
152 }
153 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
154 warn("can't lock `%s'", ETCGROUP);
155 }
156 (void) fstat(fileno(from), &st);
157 (void) snprintf(f, sizeof(f), "%s.XXXXXX", ETCGROUP);
158 if ((fd = mkstemp(f)) < 0) {
159 (void) fclose(from);
160 warn("can't create gid: mkstemp failed");
161 return 0;
162 }
163 if ((to = fdopen(fd, "w")) == (FILE *) NULL) {
164 (void) fclose(from);
165 (void) close(fd);
166 (void) unlink(f);
167 warn("can't create gid: fdopen `%s' failed", f);
168 return 0;
169 }
170 while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) {
171 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
172 (void) fclose(from);
173 (void) close(fd);
174 (void) unlink(f);
175 warn("can't create gid: short write to `%s'", f);
176 return 0;
177 }
178 }
179 (void) fprintf(to, "%s:*:%d:%s\n", group, gid, name);
180 (void) fclose(from);
181 (void) fclose(to);
182 if (rename(f, ETCGROUP) < 0) {
183 warn("can't create gid: can't rename `%s' to `%s'", f, ETCGROUP);
184 return 0;
185 }
186 (void) chmod(ETCGROUP, st.st_mode & 07777);
187 return 1;
188 }
189
190 /* modify the group entry with name `group' to be newent */
191 static int
192 modify_gid(char *group, char *newent)
193 {
194 struct stat st;
195 FILE *from;
196 FILE *to;
197 char buf[MaxEntryLen];
198 char f[MaxFileNameLen];
199 char *colon;
200 int groupc;
201 int entc;
202 int fd;
203 int cc;
204
205 if ((from = fopen(ETCGROUP, "r")) == (FILE *) NULL) {
206 warn("can't create gid for %s: can't open %s", group, ETCGROUP);
207 return 0;
208 }
209 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
210 warn("can't lock `%s'", ETCGROUP);
211 }
212 (void) fstat(fileno(from), &st);
213 (void) snprintf(f, sizeof(f), "%s.XXXXXX", ETCGROUP);
214 if ((fd = mkstemp(f)) < 0) {
215 (void) fclose(from);
216 warn("can't create gid: mkstemp failed");
217 return 0;
218 }
219 if ((to = fdopen(fd, "w")) == (FILE *) NULL) {
220 (void) fclose(from);
221 (void) close(fd);
222 (void) unlink(f);
223 warn("can't create gid: fdopen `%s' failed", f);
224 return 0;
225 }
226 groupc = strlen(group);
227 while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) {
228 if ((colon = strchr(buf, ':')) == (char *) NULL) {
229 warn("badly formed entry `%s'", buf);
230 continue;
231 }
232 entc = (int)(colon - buf);
233 if (entc == groupc && strncmp(group, buf, (unsigned) entc) == 0) {
234 if (newent == (char *) NULL) {
235 continue;
236 } else {
237 cc = strlen(newent);
238 (void) strnncpy(buf, sizeof(buf), newent, (unsigned) cc);
239 }
240 }
241 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
242 (void) fclose(from);
243 (void) close(fd);
244 (void) unlink(f);
245 warn("can't create gid: short write to `%s'", f);
246 return 0;
247 }
248 }
249 (void) fclose(from);
250 (void) fclose(to);
251 if (rename(f, ETCGROUP) < 0) {
252 warn("can't create gid: can't rename `%s' to `%s'", f, ETCGROUP);
253 return 0;
254 }
255 (void) chmod(ETCGROUP, st.st_mode & 07777);
256 return 1;
257 }
258
259 /* return 1 if `login' is a valid login name */
260 static int
261 valid_login(char *login)
262 {
263 char *cp;
264
265 for (cp = login ; *cp ; cp++) {
266 if (!isalnum(*cp) && *cp != '.' && *cp != '_' && *cp != '-') {
267 return 0;
268 }
269 }
270 return 1;
271 }
272
273 /* return 1 if `group' is a valid group name */
274 static int
275 valid_group(char *group)
276 {
277 char *cp;
278
279 for (cp = group ; *cp ; cp++) {
280 if (!isalnum(*cp)) {
281 return 0;
282 }
283 }
284 return 1;
285 }
286
287 /* find the next gid in the range lo .. hi */
288 static int
289 getnextgid(int *gidp, int lo, int hi)
290 {
291 for (*gidp = lo ; *gidp < hi ; *gidp += 1) {
292 if (getgrgid((gid_t)*gidp) == (struct group *) NULL) {
293 return 1;
294 }
295 }
296 return 0;
297 }
298
299 #ifdef EXTENSIONS
300 /* save a range of uids */
301 static int
302 save_range(user_t *up, char *cp)
303 {
304 int from;
305 int to;
306 int i;
307
308 if (up->u_rsize == 0) {
309 up->u_rsize = 32;
310 NEWARRAY(range_t, up->u_rv, up->u_rsize, return(0));
311 } else if (up->u_rc == up->u_rsize) {
312 up->u_rsize *= 2;
313 RENEW(range_t, up->u_rv, up->u_rsize, return(0));
314 }
315 if (up->u_rv && sscanf(cp, "%d..%d", &from, &to) == 2) {
316 for (i = 0 ; i < up->u_rc ; i++) {
317 if (up->u_rv[i].r_from == from && up->u_rv[i].r_to == to) {
318 break;
319 }
320 }
321 if (i == up->u_rc) {
322 up->u_rv[up->u_rc].r_from = from;
323 up->u_rv[up->u_rc].r_to = to;
324 up->u_rc += 1;
325 }
326 } else {
327 warnx("Bad range `%s'", cp);
328 return 0;
329 }
330 return 1;
331 }
332 #endif
333
334 /* set the defaults in the defaults file */
335 static int
336 setdefaults(user_t *up)
337 {
338 char template[MaxFileNameLen];
339 FILE *fp;
340 int ret;
341 int fd;
342 int i;
343
344 (void) snprintf(template, sizeof(template), "%s.XXXXXX", CONFFILE);
345 if ((fd = mkstemp(template)) < 0) {
346 warnx("can't mkstemp `%s' for writing", CONFFILE);
347 return 0;
348 }
349 if ((fp = fdopen(fd, "w")) == (FILE *) NULL) {
350 warn("can't fdopen `%s' for writing", CONFFILE);
351 return 0;
352 }
353 ret = 1;
354 if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 ||
355 fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 ||
356 fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 ||
357 fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 ||
358 fprintf(fp, "inactive\t%d\n", up->u_inactive) <= 0 ||
359 fprintf(fp, "expire\t\t%s\n", (up->u_expire == (char *) NULL) ? UNSET_EXPIRY : up->u_expire) <= 0) {
360 warn("can't write to `%s'", CONFFILE);
361 ret = 0;
362 }
363 #ifdef EXTENSIONS
364 for (i = 0 ; i < up->u_rc ; i++) {
365 if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from, up->u_rv[i].r_to) <= 0) {
366 warn("can't write to `%s'", CONFFILE);
367 ret = 0;
368 }
369 }
370 #endif
371 (void) fclose(fp);
372 if (ret) {
373 ret = ((rename(template, CONFFILE) == 0) && (chmod(CONFFILE, 0644) == 0));
374 }
375 return ret;
376 }
377
378 /* read the defaults file */
379 static void
380 read_defaults(user_t *up)
381 {
382 struct stat st;
383 size_t lineno;
384 size_t len;
385 FILE *fp;
386 char *cp;
387 char *s;
388
389 memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP));
390 memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR));
391 memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR));
392 memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL));
393 memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT));
394 up->u_inactive = DEF_INACTIVE;
395 up->u_rsize = 16;
396 NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1));
397 up->u_rv[up->u_rc].r_from = DEF_LOWUID;
398 up->u_rv[up->u_rc].r_to = DEF_HIGHUID;
399 up->u_rc += 1;
400 up->u_expire = DEF_EXPIRE;
401 if ((fp = fopen(CONFFILE, "r")) == (FILE *) NULL) {
402 if (stat(CONFFILE, &st) < 0 && !setdefaults(up)) {
403 warn("can't create `%s' defaults file", CONFFILE);
404 }
405 fp = fopen(CONFFILE, "r");
406 }
407 if (fp != (FILE *) NULL) {
408 while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != (char *) NULL) {
409 if (strncmp(s, "group", 5) == 0) {
410 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
411 }
412 memsave(&up->u_primgrp, cp, strlen(cp));
413 } else if (strncmp(s, "base_dir", 8) == 0) {
414 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
415 }
416 memsave(&up->u_basedir, cp, strlen(cp));
417 } else if (strncmp(s, "skel_dir", 8) == 0) {
418 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
419 }
420 memsave(&up->u_skeldir, cp, strlen(cp));
421 } else if (strncmp(s, "shell", 5) == 0) {
422 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
423 }
424 memsave(&up->u_shell, cp, strlen(cp));
425 } else if (strncmp(s, "inactive", 8) == 0) {
426 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
427 }
428 up->u_inactive = atoi(cp);
429 #ifdef EXTENSIONS
430 } else if (strncmp(s, "range", 5) == 0) {
431 for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
432 }
433 (void) save_range(up, cp);
434 #endif
435 #ifdef EXTENSIONS
436 } else if (strncmp(s, "preserve", 8) == 0) {
437 for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
438 }
439 up->u_preserve = (strncmp(cp, "true", 4) == 0) ? 1 :
440 (strncmp(cp, "yes", 3) == 0) ? 1 :
441 atoi(cp);
442 #endif
443 } else if (strncmp(s, "expire", 6) == 0) {
444 for (cp = s + 6 ; *cp && isspace(*cp) ; cp++) {
445 }
446 if (strcmp(cp, UNSET_EXPIRY) == 0) {
447 if (up->u_expire) {
448 FREE(up->u_expire);
449 }
450 up->u_expire = (char *) NULL;
451 } else {
452 memsave(&up->u_expire, cp, strlen(cp));
453 }
454 }
455 (void) free(s);
456 }
457 (void) fclose(fp);
458 }
459 }
460
461 /* return the next valid unused uid */
462 static int
463 getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid)
464 {
465 for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) {
466 if (getpwuid((uid_t)(*uid)) == (struct passwd *) NULL && *uid != NOBODY_UID) {
467 if (sync_uid_gid) {
468 if (getgrgid((gid_t)(*uid)) == (struct group *) NULL) {
469 return 1;
470 }
471 } else {
472 return 1;
473 }
474 }
475 }
476 return 0;
477 }
478
479 /* add a user */
480 static int
481 adduser(char *login, user_t *up)
482 {
483 struct group *grp;
484 struct stat st;
485 struct tm tm;
486 time_t expire;
487 char password[PasswordLength + 1];
488 char home[MaxFileNameLen];
489 char buf[MaxFileNameLen];
490 int sync_uid_gid;
491 int masterfd;
492 int ptmpfd;
493 int gid;
494 int cc;
495 int i;
496
497 if (!valid_login(login)) {
498 errx(EXIT_FAILURE, "`%s' is not a valid login name", login);
499 }
500 if ((masterfd = open(MASTER, O_RDONLY)) < 0) {
501 err(EXIT_FAILURE, "can't open `%s'", MASTER);
502 }
503 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
504 err(EXIT_FAILURE, "can't lock `%s'", MASTER);
505 }
506 pw_init();
507 if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
508 (void) close(masterfd);
509 err(EXIT_FAILURE, "can't obtain pw_lock");
510 }
511 while ((cc = read(masterfd, buf, sizeof(buf))) > 0) {
512 if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
513 (void) close(masterfd);
514 (void) close(ptmpfd);
515 (void) pw_abort();
516 err(EXIT_FAILURE, "short write to /etc/ptmp (not %d chars)", cc);
517 }
518 }
519 /* if no uid was specified, get next one in [low_uid..high_uid] range */
520 sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0);
521 if (up->u_uid == -1) {
522 for (i = 0 ; i < up->u_rc ; i++) {
523 if (getnextuid(sync_uid_gid, &up->u_uid, up->u_rv[i].r_from, up->u_rv[i].r_to)) {
524 break;
525 }
526 }
527 if (i == up->u_rc) {
528 (void) close(ptmpfd);
529 (void) pw_abort();
530 errx(EXIT_FAILURE, "can't get next uid for %d", up->u_uid);
531 }
532 }
533 /* check uid isn't already allocated */
534 if (!up->u_dupuid && getpwuid((uid_t)(up->u_uid)) != (struct passwd *) NULL) {
535 (void) close(ptmpfd);
536 (void) pw_abort();
537 errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid);
538 }
539 /* if -g=uid was specified, check gid is unused */
540 if (sync_uid_gid) {
541 if (getgrgid((gid_t)(up->u_uid)) != (struct group *) NULL) {
542 (void) close(ptmpfd);
543 (void) pw_abort();
544 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
545 }
546 gid = up->u_uid;
547 } else if ((grp = getgrnam(up->u_primgrp)) != (struct group *) NULL) {
548 gid = grp->gr_gid;
549 } else if ((grp = getgrgid((gid_t)atoi(up->u_primgrp))) != (struct group *) NULL) {
550 gid = grp->gr_gid;
551 } else {
552 (void) close(ptmpfd);
553 (void) pw_abort();
554 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
555 }
556 /* check name isn't already in use */
557 if (!up->u_dupuid && getpwnam(login) != (struct passwd *) NULL) {
558 (void) close(ptmpfd);
559 (void) pw_abort();
560 errx(EXIT_FAILURE, "already a `%s' user", login);
561 }
562 /* if home directory hasn't been given, make it up */
563 if (!up->u_homeset) {
564 (void) snprintf(home, sizeof(home), "%s/%s", up->u_basedir, login);
565 }
566 expire = 0;
567 if (up->u_expire != (char *) NULL) {
568 (void) memset(&tm, 0, sizeof(tm));
569 if (strptime(up->u_expire, "%c", &tm) == (char *) NULL) {
570 warnx("invalid time format `%s'", optarg);
571 } else {
572 expire = mktime(&tm);
573 }
574 }
575 password[PasswordLength] = 0;
576 if (up->u_password != (char *) NULL &&
577 strlen(up->u_password) == PasswordLength) {
578 (void) memcpy(password, up->u_password, PasswordLength);
579 } else {
580 (void) memset(password, '*', PasswordLength);
581 if (up->u_password != (char *) NULL) {
582 warnx("Password `%s' is invalid: setting it to `%s'",
583 password, "*");
584 }
585 }
586 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d::%d:%ld:%s:%s:%s\n",
587 login,
588 password,
589 up->u_uid,
590 gid,
591 up->u_inactive,
592 (long)expire,
593 up->u_comment,
594 home,
595 up->u_shell);
596 if (write(ptmpfd, buf, (size_t) cc) != cc) {
597 (void) close(ptmpfd);
598 (void) pw_abort();
599 err(EXIT_FAILURE, "can't add `%s'", buf);
600 }
601 if (up->u_mkdir) {
602 if (lstat(home, &st) < 0 && asystem("%s -p %s", MKDIR, home) != 0) {
603 (void) close(ptmpfd);
604 (void) pw_abort();
605 err(EXIT_FAILURE, "can't mkdir `%s'", home);
606 }
607 (void) copydotfiles(up->u_skeldir, up->u_uid, gid, home);
608 }
609 if (strcmp(up->u_primgrp, "=uid") == 0 &&
610 getgrnam(login) == (struct group *) NULL &&
611 !creategid(login, gid, login)) {
612 (void) close(ptmpfd);
613 (void) pw_abort();
614 err(EXIT_FAILURE, "can't create gid %d for login name %s", gid, login);
615 }
616 (void) close(ptmpfd);
617 if (pw_mkdb() < 0) {
618 err(EXIT_FAILURE, "pw_mkdb failed");
619 }
620 return 1;
621 }
622
623 /* modify a user */
624 static int
625 moduser(char *login, char *newlogin, user_t *up)
626 {
627 struct passwd *pwp;
628 struct group *grp;
629 struct tm tm;
630 time_t expire;
631 size_t loginc;
632 size_t colonc;
633 FILE *master;
634 char password[PasswordLength + 1];
635 char oldhome[MaxFileNameLen];
636 char home[MaxFileNameLen];
637 char buf[MaxFileNameLen];
638 char *colon;
639 int masterfd;
640 int ptmpfd;
641 int gid;
642 int cc;
643
644 if (!valid_login(newlogin)) {
645 errx(EXIT_FAILURE, "`%s' is not a valid login name", login);
646 }
647 if ((pwp = getpwnam(login)) == (struct passwd *) NULL) {
648 err(EXIT_FAILURE, "No such user `%s'", 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 if ((master = fdopen(masterfd, "r")) == (FILE *) NULL) {
662 (void) close(masterfd);
663 (void) close(ptmpfd);
664 (void) pw_abort();
665 err(EXIT_FAILURE, "can't fdopen fd for %s", MASTER);
666 }
667 if (up != (user_t *) NULL) {
668 if (up->u_mkdir) {
669 (void) strcpy(oldhome, pwp->pw_dir);
670 }
671 if (up->u_uid == -1) {
672 up->u_uid = pwp->pw_uid;
673 }
674 /* if -g=uid was specified, check gid is unused */
675 if (strcmp(up->u_primgrp, "=uid") == 0) {
676 if (getgrgid((gid_t)(up->u_uid)) != (struct group *) NULL) {
677 (void) close(ptmpfd);
678 (void) pw_abort();
679 errx(EXIT_FAILURE, "gid %d is already in use", up->u_uid);
680 }
681 gid = up->u_uid;
682 } else if ((grp = getgrnam(up->u_primgrp)) != (struct group *) NULL) {
683 gid = grp->gr_gid;
684 } else if ((grp = getgrgid((gid_t)atoi(up->u_primgrp))) != (struct group *) NULL) {
685 gid = grp->gr_gid;
686 } else {
687 (void) close(ptmpfd);
688 (void) pw_abort();
689 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
690 }
691 /* if changing name, check new name isn't already in use */
692 if (strcmp(login, newlogin) != 0 && getpwnam(newlogin) != (struct passwd *) NULL) {
693 (void) close(ptmpfd);
694 (void) pw_abort();
695 errx(EXIT_FAILURE, "already a `%s' user", newlogin);
696 }
697 /* if home directory hasn't been given, use the old one */
698 if (!up->u_homeset) {
699 (void) strcpy(home, pwp->pw_dir);
700 }
701 expire = 0;
702 if (up->u_expire != (char *) NULL) {
703 (void) memset(&tm, 0, sizeof(tm));
704 if (strptime(up->u_expire, "%c", &tm) == (char *) NULL) {
705 warnx("invalid time format `%s'", optarg);
706 } else {
707 expire = mktime(&tm);
708 }
709 }
710 password[PasswordLength] = 0;
711 if (up->u_password != (char *) NULL &&
712 strlen(up->u_password) == PasswordLength) {
713 (void) memcpy(password, up->u_password, PasswordLength);
714 } else {
715 (void) memcpy(password, pwp->pw_passwd, PasswordLength);
716 }
717 if (strcmp(up->u_comment, DEF_COMMENT) == 0) {
718 memsave(&up->u_comment, pwp->pw_gecos, strlen(pwp->pw_gecos));
719 }
720 if (strcmp(up->u_shell, DEF_SHELL) == 0 && strcmp(pwp->pw_shell, DEF_SHELL) != 0) {
721 memsave(&up->u_comment, pwp->pw_shell, strlen(pwp->pw_shell));
722 }
723 }
724 loginc = strlen(login);
725 while (fgets(buf, sizeof(buf), master) != (char *) NULL) {
726 cc = strlen(buf);
727 if ((colon = strchr(buf, ':')) == (char *) NULL) {
728 warnx("Malformed entry `%s'. Skipping", buf);
729 continue;
730 }
731 colonc = (size_t)(colon - buf);
732 if (strncmp(login, buf, loginc) == 0 && loginc == colonc) {
733 if (up != (user_t *) NULL) {
734 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d::%d:%ld:%s:%s:%s\n",
735 newlogin,
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 }
750 } else if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
751 (void) close(masterfd);
752 (void) close(ptmpfd);
753 (void) pw_abort();
754 err(EXIT_FAILURE, "short write to /etc/ptmp (not %d chars)", cc);
755 }
756 }
757 if (up != (user_t *) NULL &&
758 up->u_mkdir &&
759 asystem("%s %s %s", MV, oldhome, home) != 0) {
760 (void) close(ptmpfd);
761 (void) pw_abort();
762 err(EXIT_FAILURE, "can't move `%s' to `%s'", oldhome, home);
763 }
764 (void) close(ptmpfd);
765 if (pw_mkdb() < 0) {
766 err(EXIT_FAILURE, "pw_mkdb failed");
767 }
768 return 1;
769 }
770
771 /* print out usage message, and then exit */
772 static void
773 usage(char *prog)
774 {
775 if (strcmp(prog, "useradd") == 0) {
776 (void) fprintf(stderr, "%s -D [-bbasedir] [-eexpiry] [-finactive] [-ggroup] [-rrange] [-sshell]\n", prog);
777 (void) fprintf(stderr, "%s [-Ggroup] [-bbasedir] [-ccomment] [-dhomedir] [-eexpiry] [-finactive]\n\t[-ggroup] [-kskeletondir] [-m] [-o] [-ppassword] [-rrange] [-sshell]\n\t[-uuid] [-v] user\n", prog);
778 } else if (strcmp(prog, "usermod") == 0) {
779 (void) fprintf(stderr, "%s [-Ggroup] [-ccomment] [-dhomedir] [-eexpire] [-finactive] [-ggroup] [-lnewname] [-m] [-o] [-ppassword] [-sshell] [-uuid] [-v] user\n", prog);
780 } else if (strcmp(prog, "userdel") == 0) {
781 (void) fprintf(stderr, "%s -D [-ppreserve]\n", prog);
782 (void) fprintf(stderr, "%s [-ppreserve] [-r] [-v] user\n", prog);
783 } else if (strcmp(prog, "groupadd") == 0) {
784 (void) fprintf(stderr, "%s [-ggid] [-o] [-v] group\n", prog);
785 } else if (strcmp(prog, "groupdel") == 0) {
786 (void) fprintf(stderr, "%s [-v] group\n", prog);
787 } else if (strcmp(prog, "groupmod") == 0) {
788 (void) fprintf(stderr, "%s [-ggid] [-o] [-nnewname] [-v] group\n", prog);
789 }
790 exit(EXIT_FAILURE);
791 /* NOTREACHED */
792 }
793
794 extern int optind;
795 extern char *optarg;
796
797 #ifdef EXTENSIONS
798 #define ADD_OPT_EXTENSIONS "p:r:v"
799 #else
800 #define ADD_OPT_EXTENSIONS
801 #endif
802
803 static int
804 useradd(int argc, char **argv)
805 {
806 user_t u;
807 int defaultfield;
808 int bigD;
809 int c;
810 int i;
811
812 (void) memset(&u, 0, sizeof(u));
813 read_defaults(&u);
814 u.u_uid = -1;
815 defaultfield = bigD = 0;
816 while ((c = getopt(argc, argv, "DG:b:c:d:e:f:g:k:mou:s:" ADD_OPT_EXTENSIONS)) != -1) {
817 switch(c) {
818 case 'D':
819 bigD = 1;
820 break;
821 case 'G':
822 memsave(&u.u_groupv[u.u_groupc++], optarg, strlen(optarg));
823 break;
824 case 'b':
825 defaultfield = 1;
826 memsave(&u.u_basedir, optarg, strlen(optarg));
827 break;
828 case 'c':
829 memsave(&u.u_comment, optarg, strlen(optarg));
830 break;
831 case 'd':
832 u.u_homeset = 1;
833 memsave(&u.u_home, optarg, strlen(optarg));
834 break;
835 case 'e':
836 defaultfield = 1;
837 memsave(&u.u_expire, optarg, strlen(optarg));
838 break;
839 case 'f':
840 defaultfield = 1;
841 u.u_inactive = atoi(optarg);
842 break;
843 case 'g':
844 defaultfield = 1;
845 memsave(&u.u_primgrp, optarg, strlen(optarg));
846 break;
847 case 'k':
848 memsave(&u.u_skeldir, optarg, strlen(optarg));
849 break;
850 case 'm':
851 u.u_mkdir = 1;
852 break;
853 case 'o':
854 u.u_dupuid = 1;
855 break;
856 #ifdef EXTENSIONS
857 case 'p':
858 memsave(&u.u_password, optarg, strlen(optarg));
859 break;
860 #endif
861 #ifdef EXTENSIONS
862 case 'r':
863 defaultfield = 1;
864 (void) save_range(&u, optarg);
865 break;
866 #endif
867 case 's':
868 defaultfield = 1;
869 memsave(&u.u_shell, optarg, strlen(optarg));
870 break;
871 case 'u':
872 u.u_uid = atoi(optarg);
873 break;
874 #ifdef EXTENSIONS
875 case 'v':
876 verbose = 1;
877 break;
878 #endif
879 }
880 }
881 if (bigD) {
882 if (defaultfield) {
883 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
884 }
885 (void) printf("group\t\t%s\n", u.u_primgrp);
886 (void) printf("base_dir\t%s\n", u.u_basedir);
887 (void) printf("skel_dir\t%s\n", u.u_skeldir);
888 (void) printf("shell\t\t%s\n", u.u_shell);
889 (void) printf("inactive\t%d\n", u.u_inactive);
890 (void) printf("expire\t\t%s\n", (u.u_expire == (char *) NULL) ? UNSET_EXPIRY : u.u_expire);
891 #ifdef EXTENSIONS
892 for (i = 0 ; i < u.u_rc ; i++) {
893 (void) printf("range\t\t%d..%d\n", u.u_rv[i].r_from, u.u_rv[i].r_to);
894 }
895 #endif
896 return EXIT_SUCCESS;
897 }
898 if (argc == optind) {
899 usage("useradd");
900 }
901 return adduser(argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
902 }
903
904 #ifdef EXTENSIONS
905 #define MOD_OPT_EXTENSIONS "p:v"
906 #else
907 #define MOD_OPT_EXTENSIONS
908 #endif
909
910 static int
911 usermod(int argc, char **argv)
912 {
913 user_t u;
914 char newuser[MaxUserNameLen + 1];
915 int have_new_user;
916 int c;
917
918 (void) memset(&u, 0, sizeof(u));
919 (void) memset(newuser, 0, sizeof(newuser));
920 read_defaults(&u);
921 u.u_uid = -1;
922 have_new_user = 0;
923 while ((c = getopt(argc, argv, "G:c:d:e:f:g:l:mos:u:" MOD_OPT_EXTENSIONS)) != -1) {
924 switch(c) {
925 case 'G':
926 memsave(&u.u_groupv[u.u_groupc++], optarg, strlen(optarg));
927 break;
928 case 'c':
929 memsave(&u.u_comment, optarg, strlen(optarg));
930 break;
931 case 'd':
932 u.u_homeset = 1;
933 memsave(&u.u_home, optarg, strlen(optarg));
934 break;
935 case 'e':
936 memsave(&u.u_expire, optarg, strlen(optarg));
937 break;
938 case 'f':
939 u.u_inactive = atoi(optarg);
940 break;
941 case 'g':
942 memsave(&u.u_primgrp, optarg, strlen(optarg));
943 break;
944 case 'l':
945 have_new_user = 1;
946 (void) strnncpy(newuser, sizeof(newuser), optarg, strlen(optarg));
947 break;
948 case 'm':
949 u.u_mkdir = 1;
950 break;
951 case 'o':
952 u.u_dupuid = 1;
953 break;
954 #ifdef EXTENSIONS
955 case 'p':
956 memsave(&u.u_password, optarg, strlen(optarg));
957 break;
958 #endif
959 case 's':
960 memsave(&u.u_shell, optarg, strlen(optarg));
961 break;
962 case 'u':
963 u.u_uid = atoi(optarg);
964 break;
965 #ifdef EXTENSIONS
966 case 'v':
967 verbose = 1;
968 break;
969 #endif
970 }
971 }
972 if (argc == optind) {
973 usage("usermod");
974 }
975 return moduser(argv[optind], (have_new_user) ? newuser : argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
976 }
977
978 #ifdef EXTENSIONS
979 #define DEL_OPT_EXTENSIONS "Dp:v"
980 #else
981 #define DEL_OPT_EXTENSIONS
982 #endif
983
984 static int
985 userdel(int argc, char **argv)
986 {
987 struct passwd *pwp;
988 struct stat st;
989 user_t u;
990 char password[PasswordLength + 1];
991 int defaultfield;
992 int rmhome;
993 int bigD;
994 int c;
995
996 (void) memset(&u, 0, sizeof(u));
997 read_defaults(&u);
998 defaultfield = bigD = rmhome = 0;
999 while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) {
1000 switch(c) {
1001 #ifdef EXTENSIONS
1002 case 'D':
1003 bigD = 1;
1004 break;
1005 #endif
1006 #ifdef EXTENSIONS
1007 case 'p':
1008 defaultfield = 1;
1009 u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 :
1010 (strcmp(optarg, "yes") == 0) ? 1 :
1011 atoi(optarg);
1012 break;
1013 #endif
1014 case 'r':
1015 rmhome = 1;
1016 break;
1017 #ifdef EXTENSIONS
1018 case 'v':
1019 verbose = 1;
1020 break;
1021 #endif
1022 }
1023 }
1024 #ifdef EXTENSIONS
1025 if (bigD) {
1026 if (defaultfield) {
1027 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1028 }
1029 (void) printf("preserve\t%s\n", (u.u_preserve) ? "true" : "false");
1030 return EXIT_SUCCESS;
1031 }
1032 #endif
1033 if (argc == optind) {
1034 usage("usermod");
1035 }
1036 if ((pwp = getpwnam(argv[optind])) == (struct passwd *) NULL) {
1037 warn("No such user `%s'", argv[optind]);
1038 return EXIT_FAILURE;
1039 }
1040 if (rmhome) {
1041 if (stat(pwp->pw_dir, &st) < 0) {
1042 warn("Home directory `%s' does not exist", pwp->pw_dir);
1043 return EXIT_FAILURE;
1044 }
1045 (void) asystem("%s -rf %s", RM, pwp->pw_dir);
1046 }
1047 if (u.u_preserve) {
1048 memsave(&u.u_shell, FALSE_PROG, strlen(FALSE_PROG));
1049 (void) memset(password, '*', PasswordLength);
1050 password[PasswordLength] = 0;
1051 memsave(&u.u_password, password, PasswordLength);
1052 return moduser(argv[optind], argv[optind], &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1053 }
1054 return moduser(argv[optind], argv[optind], (user_t *) NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
1055 }
1056
1057 #ifdef EXTENSIONS
1058 #define GROUP_ADD_OPT_EXTENSIONS "v"
1059 #else
1060 #define GROUP_ADD_OPT_EXTENSIONS
1061 #endif
1062
1063 /* add a group */
1064 int
1065 groupadd(int argc, char **argv)
1066 {
1067 int dupgid;
1068 int gid;
1069 int c;
1070
1071 gid = -1;
1072 dupgid = 0;
1073 while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) {
1074 switch(c) {
1075 case 'g':
1076 gid = atoi(optarg);
1077 break;
1078 case 'o':
1079 dupgid = 1;
1080 break;
1081 #ifdef EXTENSIONS
1082 case 'v':
1083 verbose = 1;
1084 break;
1085 #endif
1086 }
1087 }
1088 if (argc == optind) {
1089 usage("groupadd");
1090 }
1091 if (gid < 0 && !getnextgid(&gid, LowGid, HighGid)) {
1092 err(EXIT_FAILURE, "can't add group: can't get next gid");
1093 }
1094 if (!dupgid && getgrgid((gid_t) gid) != (struct group *) NULL) {
1095 err(EXIT_FAILURE, "can't add group: gid %d is a duplicate", gid);
1096 }
1097 if (!valid_group(argv[optind])) {
1098 warn("warning - invalid group name `%s'", argv[optind]);
1099 }
1100 if (!creategid(argv[optind], gid, "")) {
1101 err(EXIT_FAILURE, "can't add group: problems with %s file", ETCGROUP);
1102 }
1103 return EXIT_SUCCESS;
1104 }
1105
1106 #ifdef EXTENSIONS
1107 #define GROUP_DEL_OPT_EXTENSIONS "v"
1108 #else
1109 #define GROUP_DEL_OPT_EXTENSIONS
1110 #endif
1111
1112 /* remove a group */
1113 int
1114 groupdel(int argc, char **argv)
1115 {
1116 int c;
1117
1118 while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) {
1119 switch(c) {
1120 #ifdef EXTENSIONS
1121 case 'v':
1122 verbose = 1;
1123 break;
1124 #endif
1125 }
1126 }
1127 if (argc == optind) {
1128 usage("groupdel");
1129 }
1130 if (!modify_gid(argv[optind], (char *) NULL)) {
1131 err(EXIT_FAILURE, "can't change %s file", ETCGROUP);
1132 }
1133 return EXIT_SUCCESS;
1134 }
1135
1136 #ifdef EXTENSIONS
1137 #define GROUP_MOD_OPT_EXTENSIONS "v"
1138 #else
1139 #define GROUP_MOD_OPT_EXTENSIONS
1140 #endif
1141
1142 /* modify a group */
1143 int
1144 groupmod(int argc, char **argv)
1145 {
1146 struct group *grp;
1147 char buf[MaxEntryLen];
1148 char *newname;
1149 char **cpp;
1150 int dupgid;
1151 int gid;
1152 int cc;
1153 int c;
1154
1155 gid = -1;
1156 dupgid = 0;
1157 newname = (char *) NULL;
1158 while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) {
1159 switch(c) {
1160 case 'g':
1161 gid = atoi(optarg);
1162 break;
1163 case 'o':
1164 dupgid = 1;
1165 break;
1166 case 'n':
1167 memsave(&newname, optarg, strlen(optarg));
1168 break;
1169 #ifdef EXTENSIONS
1170 case 'v':
1171 verbose = 1;
1172 break;
1173 #endif
1174 }
1175 }
1176 if (argc == optind) {
1177 usage("userdel");
1178 }
1179 if (gid < 0 && newname == (char *) NULL) {
1180 err(EXIT_FAILURE, "Nothing to change");
1181 }
1182 if (dupgid && gid < 0) {
1183 err(EXIT_FAILURE, "Duplicate which gid?");
1184 }
1185 if ((grp = getgrnam(argv[optind])) == (struct group *) NULL) {
1186 err(EXIT_FAILURE, "can't find group `%s' to modify", argv[optind]);
1187 }
1188 if (newname != (char *) NULL && !valid_group(newname)) {
1189 warn("warning - invalid group name `%s'", newname);
1190 }
1191 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:",
1192 (newname) ? newname : grp->gr_name,
1193 grp->gr_passwd,
1194 (gid < 0) ? grp->gr_gid : gid);
1195 for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) {
1196 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp,
1197 (cpp[1] == (char *) NULL) ? "" : ",");
1198 }
1199 if (!modify_gid(argv[optind], buf)) {
1200 err(EXIT_FAILURE, "can't change %s file", ETCGROUP);
1201 }
1202 return EXIT_SUCCESS;
1203 }
1204
1205 /* this struct describes a command */
1206 typedef struct cmd_t {
1207 char *c_progname; /* program name */
1208 char *c_word1; /* alternative program name */
1209 char *c_word2; /* alternative command word */
1210 int (*c_func)(int argc, char **argv); /* called function */
1211 } cmd_t;
1212
1213 /* despatch table for commands */
1214 static cmd_t cmds[] = {
1215 { "useradd", "user", "add", useradd },
1216 { "usermod", "user", "mod", usermod },
1217 { "userdel", "user", "del", userdel },
1218 { "groupadd", "group", "add", groupadd },
1219 { "groupmod", "group", "mod", groupmod },
1220 { "groupdel", "group", "del", groupdel },
1221 { NULL }
1222 };
1223
1224 extern char *__progname;
1225
1226 int
1227 main(int argc, char **argv)
1228 {
1229 cmd_t *cmdp;
1230
1231 for (cmdp = cmds ; cmdp->c_progname ; cmdp++) {
1232 if (strcmp(__progname, cmdp->c_progname) == 0) {
1233 return (*cmdp->c_func)(argc, argv);
1234 } else if (strcmp(__progname, cmdp->c_word1) == 0 &&
1235 strcmp(argv[1], cmdp->c_word2) == 0) {
1236 return (*cmdp->c_func)(argc - 1, argv + 1);
1237 }
1238 }
1239 errx(EXIT_FAILURE, "Program `%s' not recognised", __progname);
1240 /* NOTREACHED */
1241 }
1242