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