user.c revision 1.89 1 /* $NetBSD: user.c,v 1.89 2005/09/09 21:48:10 wiz 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("@(#) Copyright (c) 1999 \
37 The NetBSD Foundation, Inc. All rights reserved.");
38 __RCSID("$NetBSD: user.c,v 1.89 2005/09/09 21:48:10 wiz Exp $");
39 #endif
40
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/stat.h>
44
45 #include <ctype.h>
46 #include <dirent.h>
47 #include <err.h>
48 #include <fcntl.h>
49 #include <grp.h>
50 #ifdef EXTENSIONS
51 #include <login_cap.h>
52 #endif
53 #include <paths.h>
54 #include <pwd.h>
55 #include <regex.h>
56 #include <stdarg.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <syslog.h>
61 #include <time.h>
62 #include <unistd.h>
63 #include <util.h>
64 #include <errno.h>
65
66 #include "defs.h"
67 #include "usermgmt.h"
68
69
70 /* this struct describes a uid range */
71 typedef struct range_t {
72 int r_from; /* low uid */
73 int r_to; /* high uid */
74 } range_t;
75
76 /* this struct encapsulates the user information */
77 typedef struct user_t {
78 int u_flags; /* see below */
79 int u_uid; /* uid of user */
80 char *u_password; /* encrypted password */
81 char *u_comment; /* comment field */
82 char *u_home; /* home directory */
83 char *u_primgrp; /* primary group */
84 int u_groupc; /* # of secondary groups */
85 const char *u_groupv[NGROUPS_MAX]; /* secondary groups */
86 char *u_shell; /* user's shell */
87 char *u_basedir; /* base directory for home */
88 char *u_expire; /* when password will expire */
89 char *u_inactive; /* when account will expire */
90 char *u_skeldir; /* directory for startup files */
91 char *u_class; /* login class */
92 unsigned u_rsize; /* size of range array */
93 unsigned u_rc; /* # of ranges */
94 range_t *u_rv; /* the ranges */
95 unsigned u_defrc; /* # of ranges in defaults */
96 int u_preserve; /* preserve uids on deletion */
97 int u_allow_samba; /* allow trailing '$' for samba login names */
98 int u_locked; /* user account lock */
99 } user_t;
100
101 /* flags for which fields of the user_t replace the passwd entry */
102 enum {
103 F_COMMENT = 0x0001,
104 F_DUPUID = 0x0002,
105 F_EXPIRE = 0x0004,
106 F_GROUP = 0x0008,
107 F_HOMEDIR = 0x0010,
108 F_MKDIR = 0x0020,
109 F_INACTIVE = 0x0040,
110 F_PASSWORD = 0x0080,
111 F_SECGROUP = 0x0100,
112 F_SHELL = 0x0200,
113 F_UID = 0x0400,
114 F_USERNAME = 0x0800,
115 F_CLASS = 0x1000
116 };
117
118 #define UNLOCK 0
119 #define LOCK 1
120 #define LOCKED "*LOCKED*"
121
122 #define CONFFILE "/etc/usermgmt.conf"
123 #define PATH_LOGINCONF "/etc/login.conf"
124
125 #ifndef DEF_GROUP
126 #define DEF_GROUP "users"
127 #endif
128
129 #ifndef DEF_BASEDIR
130 #define DEF_BASEDIR "/home"
131 #endif
132
133 #ifndef DEF_SKELDIR
134 #define DEF_SKELDIR "/etc/skel"
135 #endif
136
137 #ifndef DEF_SHELL
138 #define DEF_SHELL _PATH_CSHELL
139 #endif
140
141 #ifndef DEF_COMMENT
142 #define DEF_COMMENT ""
143 #endif
144
145 #ifndef DEF_LOWUID
146 #define DEF_LOWUID 1000
147 #endif
148
149 #ifndef DEF_HIGHUID
150 #define DEF_HIGHUID 60000
151 #endif
152
153 #ifndef DEF_INACTIVE
154 #define DEF_INACTIVE 0
155 #endif
156
157 #ifndef DEF_EXPIRE
158 #define DEF_EXPIRE NULL
159 #endif
160
161 #ifndef DEF_CLASS
162 #define DEF_CLASS ""
163 #endif
164
165 #ifndef WAITSECS
166 #define WAITSECS 10
167 #endif
168
169 #ifndef NOBODY_UID
170 #define NOBODY_UID 32767
171 #endif
172
173 /* some useful constants */
174 enum {
175 MaxShellNameLen = 256,
176 MaxFileNameLen = MAXPATHLEN,
177 MaxUserNameLen = 32,
178 MaxFieldNameLen = 32,
179 MaxCommandLen = 2048,
180 MaxEntryLen = 2048,
181 PasswordLength = 2048,
182
183 DES_Len = 13,
184
185 LowGid = DEF_LOWUID,
186 HighGid = DEF_HIGHUID
187 };
188
189 /* Full paths of programs used here */
190 #define CHMOD "/bin/chmod"
191 #define CHOWN "/usr/sbin/chown"
192 #define MKDIR "/bin/mkdir"
193 #define MV "/bin/mv"
194 #define NOLOGIN "/sbin/nologin"
195 #define PAX "/bin/pax"
196 #define RM "/bin/rm"
197
198 #define UNSET_INACTIVE "Null (unset)"
199 #define UNSET_EXPIRY "Null (unset)"
200
201 static int asystem(const char *fmt, ...)
202 __attribute__((__format__(__printf__, 1, 2)));
203 static int is_number(const char *);
204
205 static int verbose;
206
207 static char *
208 skipspace(char *s)
209 {
210 for (; *s && isspace((unsigned char)*s) ; s++)
211 continue;
212 return s;
213 }
214
215 static int
216 check_numeric(const char *val, const char *name)
217 {
218 if (!is_number(val))
219 errx(EXIT_FAILURE, "When using [-%c %s], "
220 "the %s must be numeric", *name, name, name);
221 return atoi(val);
222 }
223
224 /* if *cpp is non-null, free it, then assign `n' chars of `s' to it */
225 static void
226 memsave(char **cpp, const char *s, size_t n)
227 {
228 if (*cpp != NULL) {
229 FREE(*cpp);
230 }
231 NEWARRAY(char, *cpp, n + 1, exit(1));
232 (void)memcpy(*cpp, s, n);
233 (*cpp)[n] = '\0';
234 }
235
236 /* a replacement for system(3) */
237 static int
238 asystem(const char *fmt, ...)
239 {
240 va_list vp;
241 char buf[MaxCommandLen];
242 int ret;
243
244 va_start(vp, fmt);
245 (void)vsnprintf(buf, sizeof(buf), fmt, vp);
246 va_end(vp);
247 if (verbose) {
248 (void)printf("Command: %s\n", buf);
249 }
250 if ((ret = system(buf)) != 0) {
251 warn("Error running `%s'", buf);
252 }
253 return ret;
254 }
255
256 /* remove a users home directory, returning 1 for success (ie, no problems encountered) */
257 static int
258 removehomedir(const char *user, uid_t uid, const char *dir)
259 {
260 struct stat st;
261
262 /* userid not root? */
263 if (uid == 0) {
264 warnx("Not deleting home directory `%s'; userid is 0", dir);
265 return 0;
266 }
267
268 /* directory exists (and is a directory!) */
269 if (stat(dir, &st) < 0) {
270 warn("Cannot access home directory `%s'", dir);
271 return 0;
272 }
273 if (!S_ISDIR(st.st_mode)) {
274 warnx("Home directory `%s' is not a directory", dir);
275 return 0;
276 }
277
278 /* userid matches directory owner? */
279 if (st.st_uid != uid) {
280 warnx("User `%s' doesn't own directory `%s', not removed",
281 user, dir);
282 return 0;
283 }
284
285 (void)seteuid(uid);
286 /* we add the "|| true" to keep asystem() quiet if there is a non-zero exit status. */
287 (void)asystem("%s -rf %s > /dev/null 2>&1 || true", RM, dir);
288 (void)seteuid(0);
289 if (rmdir(dir) < 0) {
290 warn("Unable to remove all files in `%s'", dir);
291 return 0;
292 }
293 return 1;
294 }
295
296 #define NetBSD_1_4_K 104110000
297
298 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ < NetBSD_1_4_K)
299 /* bounds checking strncpy */
300 static int
301 strlcpy(char *to, char *from, size_t tosize)
302 {
303 size_t n;
304 int fromsize;
305
306 fromsize = strlen(from);
307 n = MIN(tosize - 1, fromsize);
308 (void)memcpy(to, from, n);
309 to[n] = '\0';
310 return fromsize;
311 }
312 #endif /* NetBSD < 1.4K */
313
314 /*
315 * Copyright (c) 1997 Todd C. Miller <Todd.Miller (at) courtesan.com>
316 * All rights reserved.
317 *
318 * Redistribution and use in source and binary forms, with or without
319 * modification, are permitted provided that the following conditions
320 * are met:
321 * 1. Redistributions of source code must retain the above copyright
322 * notice, this list of conditions and the following disclaimer.
323 * 2. Redistributions in binary form must reproduce the above copyright
324 * notice, this list of conditions and the following disclaimer in the
325 * documentation and/or other materials provided with the distribution.
326 * 3. The name of the author may not be used to endorse or promote products
327 * derived from this software without specific prior written permission.
328 *
329 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
330 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
331 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
332 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
333 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
334 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
335 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
336 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
337 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
338 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
339 */
340
341 /*
342 * research has shown that NetBSD 1.3H was the first version of -current
343 * with asprintf in libc. agc
344 */
345 #define NetBSD_1_3_H 103080000
346
347 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ < NetBSD_1_3_H)
348
349 int
350 asprintf(char **str, char const *fmt, ...)
351 {
352 int ret;
353 va_list ap;
354 FILE f;
355 unsigned char *_base;
356
357 f._flags = __SWR | __SSTR | __SALC;
358 f._bf._base = f._p = (unsigned char *)malloc(128);
359 if (f._bf._base == NULL)
360 goto err;
361 f._bf._size = f._w = 127; /* Leave room for the NUL */
362 va_start(ap, fmt);
363 ret = vfprintf(&f, fmt, ap);
364 va_end(ap);
365 if (ret == -1)
366 goto err;
367 *f._p = '\0';
368 _base = realloc(f._bf._base, (size_t)(ret + 1));
369 if (_base == NULL)
370 goto err;
371 *str = (char *)_base;
372 return (ret);
373
374 err:
375 if (f._bf._base)
376 free(f._bf._base);
377 *str = NULL;
378 return (-1);
379 }
380 #endif /* NetBSD < 1.3H */
381
382 /* return 1 if all of `s' is numeric */
383 static int
384 is_number(const char *s)
385 {
386 for ( ; *s ; s++) {
387 if (!isdigit((unsigned char) *s)) {
388 return 0;
389 }
390 }
391 return 1;
392 }
393
394 /*
395 * check that the effective uid is 0 - called from funcs which will
396 * modify data and config files.
397 */
398 static void
399 checkeuid(void)
400 {
401 if (geteuid() != 0) {
402 errx(EXIT_FAILURE, "Program must be run as root");
403 }
404 }
405
406 /* copy any dot files into the user's home directory */
407 static int
408 copydotfiles(char *skeldir, int uid, int gid, char *dir)
409 {
410 struct dirent *dp;
411 DIR *dirp;
412 int n;
413
414 if ((dirp = opendir(skeldir)) == NULL) {
415 warn("Can't open source . files dir `%s'", skeldir);
416 return 0;
417 }
418 for (n = 0; (dp = readdir(dirp)) != NULL && n == 0 ; ) {
419 if (strcmp(dp->d_name, ".") == 0 ||
420 strcmp(dp->d_name, "..") == 0) {
421 continue;
422 }
423 n = 1;
424 }
425 (void)closedir(dirp);
426 if (n == 0) {
427 warnx("No \"dot\" initialisation files found");
428 } else {
429 (void)asystem("cd %s && %s -rw -pe %s . %s",
430 skeldir, PAX, (verbose) ? "-v" : "", dir);
431 }
432 (void)asystem("%s -R -h %d:%d %s", CHOWN, uid, gid, dir);
433 (void)asystem("%s -R u+w %s", CHMOD, dir);
434 return n;
435 }
436
437 /* create a group entry with gid `gid' */
438 static int
439 creategid(char *group, int gid, const char *name)
440 {
441 struct stat st;
442 FILE *from;
443 FILE *to;
444 char buf[MaxEntryLen];
445 char f[MaxFileNameLen];
446 int fd;
447 int cc;
448
449 if (getgrnam(group) != NULL) {
450 warnx("Group `%s' already exists", group);
451 return 0;
452 }
453 if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
454 warn("Can't create gid for `%s': can't open `%s'", name,
455 _PATH_GROUP);
456 return 0;
457 }
458 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
459 warn("Can't lock `%s'", _PATH_GROUP);
460 }
461 (void)fstat(fileno(from), &st);
462 (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
463 if ((fd = mkstemp(f)) < 0) {
464 warn("Can't create gid: mkstemp failed");
465 (void)fclose(from);
466 return 0;
467 }
468 if ((to = fdopen(fd, "w")) == NULL) {
469 warn("Can't create gid: fdopen `%s' failed", f);
470 (void)fclose(from);
471 (void)close(fd);
472 (void)unlink(f);
473 return 0;
474 }
475 while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) {
476 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
477 warn("Can't create gid: short write to `%s'", f);
478 (void)fclose(from);
479 (void)close(fd);
480 (void)unlink(f);
481 return 0;
482 }
483 }
484 (void)fprintf(to, "%s:*:%d:%s\n", group, gid, name);
485 (void)fclose(from);
486 (void)fclose(to);
487 if (rename(f, _PATH_GROUP) < 0) {
488 warn("Can't create gid: can't rename `%s' to `%s'", f,
489 _PATH_GROUP);
490 (void)unlink(f);
491 return 0;
492 }
493 (void)chmod(_PATH_GROUP, st.st_mode & 07777);
494 syslog(LOG_INFO, "New group added: name=%s, gid=%d", group, gid);
495 return 1;
496 }
497
498 /* modify the group entry with name `group' to be newent */
499 static int
500 modify_gid(char *group, char *newent)
501 {
502 struct stat st;
503 FILE *from;
504 FILE *to;
505 char buf[MaxEntryLen];
506 char f[MaxFileNameLen];
507 char *colon;
508 int groupc;
509 int entc;
510 int fd;
511 int cc;
512
513 if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
514 warn("Can't create gid for `%s': can't open `%s'",
515 group, _PATH_GROUP);
516 return 0;
517 }
518 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
519 warn("Can't lock `%s'", _PATH_GROUP);
520 }
521 (void)fstat(fileno(from), &st);
522 (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
523 if ((fd = mkstemp(f)) < 0) {
524 warn("Can't create gid: mkstemp failed");
525 (void)fclose(from);
526 return 0;
527 }
528 if ((to = fdopen(fd, "w")) == NULL) {
529 warn("Can't create gid: fdopen `%s' failed", f);
530 (void)fclose(from);
531 (void)close(fd);
532 (void)unlink(f);
533 return 0;
534 }
535 groupc = strlen(group);
536 while (fgets(buf, sizeof(buf), from) != NULL) {
537 cc = strlen(buf);
538 if ((colon = strchr(buf, ':')) == NULL) {
539 warnx("Badly formed entry `%s'", buf);
540 continue;
541 }
542 entc = (int)(colon - buf);
543 if (entc == groupc &&
544 strncmp(group, buf, (unsigned) entc) == 0) {
545 if (newent == NULL) {
546 continue;
547 } else {
548 cc = strlen(newent);
549 (void)strlcpy(buf, newent, sizeof(buf));
550 }
551 }
552 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
553 warn("Can't create gid: short write to `%s'", f);
554 (void)fclose(from);
555 (void)close(fd);
556 (void)unlink(f);
557 return 0;
558 }
559 }
560 (void)fclose(from);
561 (void)fclose(to);
562 if (rename(f, _PATH_GROUP) < 0) {
563 warn("Can't create gid: can't rename `%s' to `%s'", f,
564 _PATH_GROUP);
565 (void)unlink(f);
566 return 0;
567 }
568 (void)chmod(_PATH_GROUP, st.st_mode & 07777);
569 if (newent == NULL) {
570 syslog(LOG_INFO, "group deleted: name=%s", group);
571 } else {
572 syslog(LOG_INFO, "group information modified: name=%s", group);
573 }
574 return 1;
575 }
576
577 /* modify the group entries for all `groups', by adding `user' */
578 static int
579 append_group(char *user, int ngroups, const char **groups)
580 {
581 struct group *grp;
582 struct stat st;
583 FILE *from;
584 FILE *to;
585 char buf[MaxEntryLen];
586 char f[MaxFileNameLen];
587 char *colon;
588 int groupc;
589 int entc;
590 int fd;
591 int nc;
592 int cc;
593 int i;
594 int j;
595
596 for (i = 0 ; i < ngroups ; i++) {
597 if ((grp = getgrnam(groups[i])) == NULL) {
598 warnx("Can't append group `%s' for user `%s'",
599 groups[i], user);
600 } else {
601 for (j = 0 ; grp->gr_mem[j] ; j++) {
602 if (strcmp(user, grp->gr_mem[j]) == 0) {
603 /* already in it */
604 groups[i] = "";
605 }
606 }
607 }
608 }
609 if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
610 warn("Can't append group for `%s': can't open `%s'",
611 user, _PATH_GROUP);
612 return 0;
613 }
614 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
615 warn("Can't lock `%s'", _PATH_GROUP);
616 }
617 (void)fstat(fileno(from), &st);
618 (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
619 if ((fd = mkstemp(f)) < 0) {
620 warn("Can't create gid: mkstemp failed");
621 (void)fclose(from);
622 return 0;
623 }
624 if ((to = fdopen(fd, "w")) == NULL) {
625 warn("Can't create gid: fdopen `%s' failed", f);
626 (void)fclose(from);
627 (void)close(fd);
628 (void)unlink(f);
629 return 0;
630 }
631 while (fgets(buf, sizeof(buf), from) != NULL) {
632 cc = strlen(buf);
633 if ((colon = strchr(buf, ':')) == NULL) {
634 warnx("Badly formed entry `%s'", buf);
635 continue;
636 }
637 entc = (int)(colon - buf);
638 for (i = 0 ; i < ngroups ; i++) {
639 if ((groupc = strlen(groups[i])) == 0) {
640 continue;
641 }
642 if (entc == groupc &&
643 strncmp(groups[i], buf, (unsigned) entc) == 0) {
644 if ((nc = snprintf(&buf[cc - 1],
645 sizeof(buf) - cc + 1, "%s%s\n",
646 (buf[cc - 2] == ':') ? "" : ",", user)) < 0)
647 warnx("Warning: group `%s' "
648 "entry too long", groups[i]);
649 cc += nc - 1;
650 }
651 }
652 if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
653 warn("Can't create gid: short write to `%s'", f);
654 (void)fclose(from);
655 (void)close(fd);
656 (void)unlink(f);
657 return 0;
658 }
659 }
660 (void)fclose(from);
661 (void)fclose(to);
662 if (rename(f, _PATH_GROUP) < 0) {
663 warn("Can't create gid: can't rename `%s' to `%s'",
664 f, _PATH_GROUP);
665 (void)unlink(f);
666 return 0;
667 }
668 (void)chmod(_PATH_GROUP, st.st_mode & 07777);
669 return 1;
670 }
671
672 /* the valid characters for login and group names */
673 #define VALID_CHAR(c) (isalnum(c) || (c) == '.' || (c) == '_' || (c) == '-')
674
675 /* return 1 if `login' is a valid login name */
676 static int
677 valid_login(char *login_name, int allow_samba)
678 {
679 unsigned char *cp;
680
681 if (strlen(login_name) >= LOGIN_NAME_MAX) {
682 return 0;
683 }
684 for (cp = (unsigned char *)login_name ; *cp ; cp++) {
685 if (!VALID_CHAR(*cp)) {
686 #ifdef EXTENSIONS
687 /* check for a trailing '$' in a Samba user name */
688 if (allow_samba && *cp == '$' && *(cp + 1) == 0x0) {
689 return 1;
690 }
691 #endif
692 return 0;
693 }
694 }
695 return 1;
696 }
697
698 /* return 1 if `group' is a valid group name */
699 static int
700 valid_group(char *group)
701 {
702 unsigned char *cp;
703
704 for (cp = (unsigned char *)group; *cp; cp++) {
705 if (!VALID_CHAR(*cp)) {
706 return 0;
707 }
708 }
709 return 1;
710 }
711
712 /* find the next gid in the range lo .. hi */
713 static int
714 getnextgid(int *gidp, int lo, int hi)
715 {
716 for (*gidp = lo ; *gidp < hi ; *gidp += 1) {
717 if (getgrgid((gid_t)*gidp) == NULL) {
718 return 1;
719 }
720 }
721 return 0;
722 }
723
724 #ifdef EXTENSIONS
725 /* save a range of uids */
726 static int
727 save_range(user_t *up, char *cp)
728 {
729 int from;
730 int to;
731 int i;
732
733 if (up->u_rsize == 0) {
734 up->u_rsize = 32;
735 NEWARRAY(range_t, up->u_rv, up->u_rsize, return(0));
736 } else if (up->u_rc == up->u_rsize) {
737 up->u_rsize *= 2;
738 RENEW(range_t, up->u_rv, up->u_rsize, return(0));
739 }
740 if (up->u_rv && sscanf(cp, "%d..%d", &from, &to) == 2) {
741 for (i = up->u_defrc ; i < up->u_rc ; i++) {
742 if (up->u_rv[i].r_from == from &&
743 up->u_rv[i].r_to == to) {
744 break;
745 }
746 }
747 if (i == up->u_rc) {
748 up->u_rv[up->u_rc].r_from = from;
749 up->u_rv[up->u_rc].r_to = to;
750 up->u_rc += 1;
751 }
752 } else {
753 warnx("Bad range `%s'", cp);
754 return 0;
755 }
756 return 1;
757 }
758 #endif
759
760 /* set the defaults in the defaults file */
761 static int
762 setdefaults(user_t *up)
763 {
764 char template[MaxFileNameLen];
765 FILE *fp;
766 int ret;
767 int fd;
768 #ifdef EXTENSIONS
769 int i;
770 #endif
771
772 (void)snprintf(template, sizeof(template), "%s.XXXXXX", CONFFILE);
773 if ((fd = mkstemp(template)) < 0) {
774 warnx("Can't mkstemp `%s' for writing", CONFFILE);
775 return 0;
776 }
777 if ((fp = fdopen(fd, "w")) == NULL) {
778 warn("Can't fdopen `%s' for writing", CONFFILE);
779 return 0;
780 }
781 ret = 1;
782 if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 ||
783 fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 ||
784 fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 ||
785 fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 ||
786 #ifdef EXTENSIONS
787 fprintf(fp, "class\t\t%s\n", up->u_class) <= 0 ||
788 #endif
789 fprintf(fp, "inactive\t%s\n", (up->u_inactive == NULL) ?
790 UNSET_INACTIVE : up->u_inactive) <= 0 ||
791 fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ?
792 UNSET_EXPIRY : up->u_expire) <= 0 ||
793 fprintf(fp, "preserve\t%s\n", (up->u_preserve == 0) ?
794 "false" : "true") <= 0) {
795 warn("Can't write to `%s'", CONFFILE);
796 ret = 0;
797 }
798 #ifdef EXTENSIONS
799 for (i = (up->u_defrc != up->u_rc) ? up->u_defrc : 0;
800 i < up->u_rc ; i++) {
801 if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from,
802 up->u_rv[i].r_to) <= 0) {
803 warn("Can't write to `%s'", CONFFILE);
804 ret = 0;
805 }
806 }
807 #endif
808 (void)fclose(fp);
809 if (ret) {
810 ret = ((rename(template, CONFFILE) == 0) &&
811 (chmod(CONFFILE, 0644) == 0));
812 }
813 return ret;
814 }
815
816 /* read the defaults file */
817 static void
818 read_defaults(user_t *up)
819 {
820 struct stat st;
821 size_t lineno;
822 size_t len;
823 FILE *fp;
824 char *cp;
825 char *s;
826
827 memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP));
828 memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR));
829 memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR));
830 memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL));
831 memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT));
832 #ifdef EXTENSIONS
833 memsave(&up->u_class, DEF_CLASS, strlen(DEF_CLASS));
834 #endif
835 up->u_rsize = 16;
836 up->u_defrc = 0;
837 NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1));
838 up->u_inactive = DEF_INACTIVE;
839 up->u_expire = DEF_EXPIRE;
840 if ((fp = fopen(CONFFILE, "r")) == NULL) {
841 if (stat(CONFFILE, &st) < 0 && !setdefaults(up)) {
842 warn("Can't create `%s' defaults file", CONFFILE);
843 }
844 fp = fopen(CONFFILE, "r");
845 }
846 if (fp != NULL) {
847 while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) {
848 if (strncmp(s, "group", 5) == 0) {
849 cp = skipspace(s + 5);
850 memsave(&up->u_primgrp, (char *)cp, strlen(cp));
851 } else if (strncmp(s, "base_dir", 8) == 0) {
852 cp = skipspace(s + 8);
853 memsave(&up->u_basedir, (char *)cp, strlen(cp));
854 } else if (strncmp(s, "skel_dir", 8) == 0) {
855 cp = skipspace(s + 8);
856 memsave(&up->u_skeldir, (char *)cp, strlen(cp));
857 } else if (strncmp(s, "shell", 5) == 0) {
858 cp = skipspace(s + 5);
859 memsave(&up->u_shell, cp, strlen(cp));
860 #ifdef EXTENSIONS
861 } else if (strncmp((char *)s, "class", 5) == 0) {
862 cp = skipspace(s + 5);
863 memsave(&up->u_class, cp, strlen(cp));
864 #endif
865 } else if (strncmp(s, "inactive", 8) == 0) {
866 cp = skipspace(s + 8);
867 if (strcmp(cp, UNSET_INACTIVE) == 0) {
868 if (up->u_inactive) {
869 FREE(up->u_inactive);
870 }
871 up->u_inactive = NULL;
872 } else {
873 memsave(&up->u_inactive, cp, strlen(cp));
874 }
875 #ifdef EXTENSIONS
876 } else if (strncmp(s, "range", 5) == 0) {
877 cp = skipspace(s + 5);
878 (void)save_range(up, cp);
879 #endif
880 #ifdef EXTENSIONS
881 } else if (strncmp(s, "preserve", 8) == 0) {
882 cp = skipspace(s + 8);
883 up->u_preserve =
884 (strncmp(cp, "true", 4) == 0) ? 1 :
885 (strncmp(cp, "yes", 3) == 0) ? 1 : atoi(cp);
886 #endif
887 } else if (strncmp(s, "expire", 6) == 0) {
888 cp = skipspace(s + 6);
889 if (strcmp(cp, UNSET_EXPIRY) == 0) {
890 if (up->u_expire) {
891 FREE(up->u_expire);
892 }
893 up->u_expire = NULL;
894 } else {
895 memsave(&up->u_expire, cp, strlen(cp));
896 }
897 }
898 (void)free(s);
899 }
900 (void)fclose(fp);
901 }
902 if (up->u_rc == 0) {
903 up->u_rv[up->u_rc].r_from = DEF_LOWUID;
904 up->u_rv[up->u_rc].r_to = DEF_HIGHUID;
905 up->u_rc += 1;
906 }
907 up->u_defrc = up->u_rc;
908 }
909
910 /* return the next valid unused uid */
911 static int
912 getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid)
913 {
914 for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) {
915 if (getpwuid((uid_t)(*uid)) == NULL && *uid != NOBODY_UID) {
916 if (sync_uid_gid) {
917 if (getgrgid((gid_t)(*uid)) == NULL) {
918 return 1;
919 }
920 } else {
921 return 1;
922 }
923 }
924 }
925 return 0;
926 }
927
928 /* structure which defines a password type */
929 typedef struct passwd_type_t {
930 const char *type; /* optional type descriptor */
931 size_t desc_length; /* length of type descriptor */
932 size_t length; /* length of password */
933 const char *regex; /* regexp to output the password */
934 size_t re_sub; /* subscript of regexp to use */
935 } passwd_type_t;
936
937 static passwd_type_t passwd_types[] = {
938 { "$sha1", 5, 28, "\\$[^$]+\\$[^$]+\\$[^$]+\\$(.*)", 1 }, /* SHA1 */
939 { "$2a", 3, 54, "\\$[^$]+\\$[^$]+\\$(.*)", 1 }, /* Blowfish */
940 { "$1", 2, 34, NULL, 0 }, /* MD5 */
941 { "", 0, DES_Len,NULL, 0 }, /* standard DES */
942 { NULL, (size_t)~0, (size_t)~0, NULL, 0 }
943 /* none - terminate search */
944 };
945
946 /* return non-zero if it's a valid password - check length for cipher type */
947 static int
948 valid_password_length(char *newpasswd)
949 {
950 passwd_type_t *pwtp;
951 regmatch_t matchv[10];
952 regex_t r;
953
954 for (pwtp = passwd_types; pwtp->desc_length != (size_t)~0; pwtp++) {
955 if (strncmp(newpasswd, pwtp->type, pwtp->desc_length) == 0) {
956 if (pwtp->regex == NULL) {
957 return strlen(newpasswd) == pwtp->length;
958 }
959 (void)regcomp(&r, pwtp->regex, REG_EXTENDED);
960 if (regexec(&r, newpasswd, 10, matchv, 0) == 0) {
961 regfree(&r);
962 return (int)(matchv[pwtp->re_sub].rm_eo -
963 matchv[pwtp->re_sub].rm_so + 1) ==
964 pwtp->length;
965 }
966 regfree(&r);
967 }
968 }
969 return 0;
970 }
971
972 #ifdef EXTENSIONS
973 static int
974 valid_class(char *class)
975 {
976 login_cap_t *lc;
977
978 if (class == NULL || *class == '\0')
979 return 1;
980 /*
981 * Check if /etc/login.conf exists. login_getclass() will
982 * return 1 due to it not existing, so not informing the
983 * user the actual login class does not exist.
984 */
985
986 if (access(PATH_LOGINCONF, R_OK) == -1) {
987 warn("Access failed for `%s'; will not validate class `%s'",
988 PATH_LOGINCONF, class);
989 return 1;
990 }
991
992 if ((lc = login_getclass(class)) != NULL) {
993 login_close(lc);
994 return 1;
995 }
996 return 0;
997 }
998 #endif
999
1000 /* look for a valid time, return 0 if it was specified but bad */
1001 static int
1002 scantime(time_t *tp, char *s)
1003 {
1004 struct tm tm;
1005 char *ep;
1006 long val;
1007
1008 *tp = 0;
1009 if (s != NULL) {
1010 (void)memset(&tm, 0, sizeof(tm));
1011 if (strptime(s, "%c", &tm) != NULL) {
1012 *tp = mktime(&tm);
1013 if (*tp == -1)
1014 return 0;
1015 return 1;
1016 } else if (strptime(s, "%B %d %Y", &tm) != NULL) {
1017 *tp = mktime(&tm);
1018 if (*tp == -1)
1019 return 0;
1020 return 1;
1021 } else {
1022 errno = 0;
1023 *tp = val = strtol(s, &ep, 10);
1024 if (*ep != '\0' || *tp < -1 || errno == ERANGE) {
1025 *tp = 0;
1026 return 0;
1027 }
1028 if (*tp != val)
1029 return 0;
1030 }
1031 }
1032 return 1;
1033 }
1034
1035 /* add a user */
1036 static int
1037 adduser(char *login_name, user_t *up)
1038 {
1039 struct group *grp;
1040 struct stat st;
1041 time_t expire;
1042 time_t inactive;
1043 char password[PasswordLength + 1];
1044 char home[MaxFileNameLen];
1045 char buf[MaxFileNameLen];
1046 int sync_uid_gid;
1047 int masterfd;
1048 int ptmpfd;
1049 int gid;
1050 int cc;
1051 int i;
1052
1053 if (!valid_login(login_name, up->u_allow_samba)) {
1054 errx(EXIT_FAILURE, "`%s' is not a valid login name", login_name);
1055 }
1056 #ifdef EXTENSIONS
1057 if (!valid_class(up->u_class)) {
1058 errx(EXIT_FAILURE, "No such login class `%s'", up->u_class);
1059 }
1060 #endif
1061 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
1062 err(EXIT_FAILURE, "Can't open `%s'", _PATH_MASTERPASSWD);
1063 }
1064 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
1065 err(EXIT_FAILURE, "Can't lock `%s'", _PATH_MASTERPASSWD);
1066 }
1067 pw_init();
1068 if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
1069 int serrno = errno;
1070 (void)close(masterfd);
1071 errno = serrno;
1072 err(EXIT_FAILURE, "Can't obtain pw_lock");
1073 }
1074 while ((cc = read(masterfd, buf, sizeof(buf))) > 0) {
1075 if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
1076 int serrno = errno;
1077 (void)close(masterfd);
1078 (void)close(ptmpfd);
1079 (void)pw_abort();
1080 errno = serrno;
1081 err(EXIT_FAILURE, "Short write to /etc/ptmp "
1082 "(not %d chars)", cc);
1083 }
1084 }
1085 /* if no uid was specified, get next one in [low_uid..high_uid] range */
1086 sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0);
1087 if (up->u_uid == -1) {
1088 int got_id = 0;
1089
1090 /*
1091 * Look for a free UID in the command line ranges (if any).
1092 * These start after the ranges specified in the config file.
1093 */
1094 for (i = up->u_defrc; !got_id && i < up->u_rc ; i++) {
1095 got_id = getnextuid(sync_uid_gid, &up->u_uid,
1096 up->u_rv[i].r_from, up->u_rv[i].r_to);
1097 }
1098 /*
1099 * If there were no free UIDs in the command line ranges,
1100 * try the ranges from the config file (there will always
1101 * be at least one default).
1102 */
1103 for (i = 0; !got_id && i < up->u_defrc; i++) {
1104 got_id = getnextuid(sync_uid_gid, &up->u_uid,
1105 up->u_rv[i].r_from, up->u_rv[i].r_to);
1106 }
1107 if (!got_id) {
1108 (void)close(ptmpfd);
1109 (void)pw_abort();
1110 errx(EXIT_FAILURE, "can't get next uid for %d",
1111 up->u_uid);
1112 }
1113 }
1114 /* check uid isn't already allocated */
1115 if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) {
1116 (void)close(ptmpfd);
1117 (void)pw_abort();
1118 errx(EXIT_FAILURE, "uid %d is already in use", up->u_uid);
1119 }
1120 /* if -g=uid was specified, check gid is unused */
1121 if (sync_uid_gid) {
1122 if (getgrgid((gid_t)(up->u_uid)) != NULL) {
1123 (void)close(ptmpfd);
1124 (void)pw_abort();
1125 errx(EXIT_FAILURE, "gid %d is already in use",
1126 up->u_uid);
1127 }
1128 gid = up->u_uid;
1129 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) {
1130 gid = grp->gr_gid;
1131 } else if (is_number(up->u_primgrp) &&
1132 (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) {
1133 gid = grp->gr_gid;
1134 } else {
1135 (void)close(ptmpfd);
1136 (void)pw_abort();
1137 errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
1138 }
1139 /* check name isn't already in use */
1140 if (!(up->u_flags & F_DUPUID) && getpwnam(login_name) != NULL) {
1141 (void)close(ptmpfd);
1142 (void)pw_abort();
1143 errx(EXIT_FAILURE, "already a `%s' user", login_name);
1144 }
1145 if (up->u_flags & F_HOMEDIR) {
1146 (void)strlcpy(home, up->u_home, sizeof(home));
1147 } else {
1148 /* if home directory hasn't been given, make it up */
1149 (void)snprintf(home, sizeof(home), "%s/%s", up->u_basedir,
1150 login_name);
1151 }
1152 if (!scantime(&inactive, up->u_inactive)) {
1153 warnx("Warning: inactive time `%s' invalid, password expiry off",
1154 up->u_inactive);
1155 }
1156 if (!scantime(&expire, up->u_expire) || expire == -1) {
1157 warnx("Warning: expire time `%s' invalid, account expiry off",
1158 up->u_expire);
1159 expire = 0; /* Just in case. */
1160 }
1161 if (lstat(home, &st) < 0 && !(up->u_flags & F_MKDIR)) {
1162 warnx("Warning: home directory `%s' doesn't exist, "
1163 "and -m was not specified", home);
1164 }
1165 password[sizeof(password) - 1] = '\0';
1166 if (up->u_password != NULL && valid_password_length(up->u_password)) {
1167 (void)strlcpy(password, up->u_password, sizeof(password));
1168 } else {
1169 (void)memset(password, '*', DES_Len);
1170 password[DES_Len] = 0;
1171 if (up->u_password != NULL) {
1172 warnx("Password `%s' is invalid: setting it to `%s'",
1173 up->u_password, password);
1174 }
1175 }
1176 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
1177 login_name,
1178 password,
1179 up->u_uid,
1180 gid,
1181 #ifdef EXTENSIONS
1182 up->u_class,
1183 #else
1184 "",
1185 #endif
1186 (long) inactive,
1187 (long) expire,
1188 up->u_comment,
1189 home,
1190 up->u_shell);
1191 if (write(ptmpfd, buf, (size_t) cc) != cc) {
1192 int serrno = errno;
1193 (void)close(ptmpfd);
1194 (void)pw_abort();
1195 errno = serrno;
1196 err(EXIT_FAILURE, "Can't add `%s'", buf);
1197 }
1198 if (up->u_flags & F_MKDIR) {
1199 if (lstat(home, &st) == 0) {
1200 (void)close(ptmpfd);
1201 (void)pw_abort();
1202 errx(EXIT_FAILURE,
1203 "Home directory `%s' already exists", home);
1204 } else {
1205 if (asystem("%s -p %s", MKDIR, home) != 0) {
1206 int serrno = errno;
1207 (void)close(ptmpfd);
1208 (void)pw_abort();
1209 errno = serrno;
1210 err(EXIT_FAILURE, "Can't mkdir `%s'", home);
1211 }
1212 (void)copydotfiles(up->u_skeldir, up->u_uid, gid, home);
1213 }
1214 }
1215 if (strcmp(up->u_primgrp, "=uid") == 0 &&
1216 getgrnam(login_name) == NULL &&
1217 !creategid(login_name, gid, login_name)) {
1218 (void)close(ptmpfd);
1219 (void)pw_abort();
1220 errx(EXIT_FAILURE, "Can't create gid %d for login name %s",
1221 gid, login_name);
1222 }
1223 if (up->u_groupc > 0 &&
1224 !append_group(login_name, up->u_groupc, up->u_groupv)) {
1225 (void)close(ptmpfd);
1226 (void)pw_abort();
1227 errx(EXIT_FAILURE, "Can't append `%s' to new groups",
1228 login_name);
1229 }
1230 (void)close(ptmpfd);
1231 #if PW_MKDB_ARGC == 2
1232 if (pw_mkdb(login_name, 0) < 0)
1233 #else
1234 if (pw_mkdb() < 0)
1235 #endif
1236 {
1237 (void)pw_abort();
1238 errx(EXIT_FAILURE, "pw_mkdb failed");
1239 }
1240 syslog(LOG_INFO, "New user added: name=%s, uid=%d, gid=%d, home=%s, "
1241 "shell=%s", login_name, up->u_uid, gid, home, up->u_shell);
1242 return 1;
1243 }
1244
1245 /* remove a user from the groups file */
1246 static int
1247 rm_user_from_groups(char *login_name)
1248 {
1249 struct stat st;
1250 regmatch_t matchv[10];
1251 regex_t r;
1252 FILE *from;
1253 FILE *to;
1254 char line[MaxEntryLen];
1255 char buf[MaxEntryLen];
1256 char f[MaxFileNameLen];
1257 int fd;
1258 int cc;
1259 int sc;
1260
1261 (void)snprintf(line, sizeof(line), "(:|,)(%s)(,|$)", login_name);
1262 if ((sc = regcomp(&r, line, REG_EXTENDED|REG_NEWLINE)) != 0) {
1263 (void)regerror(sc, &r, buf, sizeof(buf));
1264 warnx("Can't compile regular expression `%s' (%s)", line,
1265 buf);
1266 return 0;
1267 }
1268 if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
1269 warn("Can't remove gid for `%s': can't open `%s'",
1270 login_name, _PATH_GROUP);
1271 return 0;
1272 }
1273 if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
1274 warn("can't lock `%s'", _PATH_GROUP);
1275 }
1276 (void)fstat(fileno(from), &st);
1277 (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
1278 if ((fd = mkstemp(f)) < 0) {
1279 warn("Can't create gid: mkstemp failed");
1280 (void)fclose(from);
1281 return 0;
1282 }
1283 if ((to = fdopen(fd, "w")) == NULL) {
1284 warn("Can't create gid: fdopen `%s' failed", f);
1285 (void)fclose(from);
1286 (void)close(fd);
1287 (void)unlink(f);
1288 return 0;
1289 }
1290 while (fgets(buf, sizeof(buf), from) != NULL) {
1291 cc = strlen(buf);
1292 if (regexec(&r, buf, 10, matchv, 0) == 0) {
1293 if (buf[(int)matchv[1].rm_so] == ',') {
1294 matchv[2].rm_so = matchv[1].rm_so;
1295 } else if (matchv[2].rm_eo != matchv[3].rm_eo) {
1296 matchv[2].rm_eo = matchv[3].rm_eo;
1297 }
1298 cc -= (int) matchv[2].rm_eo;
1299 sc = (int) matchv[2].rm_so;
1300 if (fwrite(buf, sizeof(char), (size_t)sc, to) != sc ||
1301 fwrite(&buf[(int)matchv[2].rm_eo], sizeof(char),
1302 (size_t)cc, to) != cc) {
1303 warn("Can't create gid: short write to `%s'",
1304 f);
1305 (void)fclose(from);
1306 (void)close(fd);
1307 (void)unlink(f);
1308 return 0;
1309 }
1310 } else if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
1311 warn("Can't create gid: short write to `%s'", f);
1312 (void)fclose(from);
1313 (void)close(fd);
1314 (void)unlink(f);
1315 return 0;
1316 }
1317 }
1318 (void)fclose(from);
1319 (void)fclose(to);
1320 if (rename(f, _PATH_GROUP) < 0) {
1321 warn("Can't create gid: can't rename `%s' to `%s'",
1322 f, _PATH_GROUP);
1323 (void)unlink(f);
1324 return 0;
1325 }
1326 (void)chmod(_PATH_GROUP, st.st_mode & 07777);
1327 return 1;
1328 }
1329
1330 /* check that the user or group is local, not from YP/NIS */
1331 static int
1332 is_local(char *name, const char *file)
1333 {
1334 FILE *fp;
1335 char buf[MaxEntryLen];
1336 size_t len;
1337 int ret;
1338
1339 if ((fp = fopen(file, "r")) == NULL) {
1340 err(EXIT_FAILURE, "Can't open `%s'", file);
1341 }
1342 len = strlen(name);
1343 for (ret = 0 ; fgets(buf, sizeof(buf), fp) != NULL ; ) {
1344 if (strncmp(buf, name, len) == 0 && buf[len] == ':') {
1345 ret = 1;
1346 break;
1347 }
1348 }
1349 (void)fclose(fp);
1350 return ret;
1351 }
1352
1353 /* modify a user */
1354 static int
1355 moduser(char *login_name, char *newlogin, user_t *up, int allow_samba)
1356 {
1357 struct passwd *pwp;
1358 struct group *grp;
1359 const char *homedir;
1360 char *locked_pwd;
1361 size_t colonc;
1362 size_t loginc;
1363 size_t len;
1364 size_t cc;
1365 FILE *master;
1366 char newdir[MaxFileNameLen];
1367 char buf[MaxEntryLen];
1368 char *colon;
1369 int masterfd;
1370 int ptmpfd;
1371 int error;
1372
1373 if (!valid_login(newlogin, allow_samba)) {
1374 errx(EXIT_FAILURE, "`%s' is not a valid login name",
1375 login_name);
1376 }
1377 if ((pwp = getpwnam(login_name)) == NULL) {
1378 errx(EXIT_FAILURE, "No such user `%s'", login_name);
1379 }
1380 if (!is_local(login_name, _PATH_MASTERPASSWD)) {
1381 errx(EXIT_FAILURE, "User `%s' must be a local user",
1382 login_name);
1383 }
1384 /* keep dir name in case we need it for '-m' */
1385 homedir = pwp->pw_dir;
1386
1387 if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
1388 err(EXIT_FAILURE, "Can't open `%s'", _PATH_MASTERPASSWD);
1389 }
1390 if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
1391 err(EXIT_FAILURE, "Can't lock `%s'", _PATH_MASTERPASSWD);
1392 }
1393 pw_init();
1394 if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
1395 int serrno = errno;
1396 (void)close(masterfd);
1397 errno = serrno;
1398 err(EXIT_FAILURE, "Can't obtain pw_lock");
1399 }
1400 if ((master = fdopen(masterfd, "r")) == NULL) {
1401 int serrno = errno;
1402 (void)close(masterfd);
1403 (void)close(ptmpfd);
1404 (void)pw_abort();
1405 errno = serrno;
1406 err(EXIT_FAILURE, "Can't fdopen fd for %s", _PATH_MASTERPASSWD);
1407 }
1408 if (up != NULL) {
1409 if (up->u_flags & F_USERNAME) {
1410 /*
1411 * If changing name,
1412 * check new name isn't already in use
1413 */
1414 if (strcmp(login_name, newlogin) != 0 &&
1415 getpwnam(newlogin) != NULL) {
1416 (void)close(ptmpfd);
1417 (void)pw_abort();
1418 errx(EXIT_FAILURE, "Already a `%s' user",
1419 newlogin);
1420 }
1421 pwp->pw_name = newlogin;
1422
1423 /*
1424 * Provide a new directory name in case the
1425 * home directory is to be moved.
1426 */
1427 if (up->u_flags & F_MKDIR) {
1428 (void)snprintf(newdir, sizeof(newdir), "%s/%s",
1429 up->u_basedir, newlogin);
1430 pwp->pw_dir = newdir;
1431 }
1432 }
1433 if (up->u_flags & F_PASSWORD) {
1434 if (up->u_password != NULL) {
1435 if (!valid_password_length(up->u_password)) {
1436 (void)close(ptmpfd);
1437 (void)pw_abort();
1438 errx(EXIT_FAILURE,
1439 "Invalid password: `%s'",
1440 up->u_password);
1441 }
1442 if ((locked_pwd =
1443 strstr(pwp->pw_passwd, LOCKED)) != NULL) {
1444 /*
1445 * account is locked - keep it locked
1446 * and just change the password.
1447 */
1448 if (asprintf(&locked_pwd, "%s%s",
1449 LOCKED, up->u_password) == -1)
1450 err(EXIT_FAILURE,
1451 "asprintf failed");
1452 pwp->pw_passwd = locked_pwd;
1453 } else
1454 pwp->pw_passwd = up->u_password;
1455 }
1456 }
1457
1458 /* check whether we should lock the account. */
1459 if (up->u_locked == LOCK) {
1460 /* check to see account if already locked. */
1461 if ((locked_pwd = strstr(pwp->pw_passwd, LOCKED))
1462 != NULL)
1463 warnx("Account is already locked");
1464 else {
1465 if (asprintf(&locked_pwd, "%s%s", LOCKED,
1466 pwp->pw_passwd) == -1)
1467 err(EXIT_FAILURE, "asprintf failed");
1468 pwp->pw_passwd = locked_pwd;
1469 }
1470 } else if (up->u_locked == UNLOCK) {
1471 if ((locked_pwd = strstr(pwp->pw_passwd, LOCKED))
1472 == NULL)
1473 warnx("Account '%s' is not locked", login_name);
1474 else
1475 pwp->pw_passwd = locked_pwd + strlen(LOCKED);
1476 }
1477
1478 if (up->u_flags & F_UID) {
1479 /* check uid isn't already allocated */
1480 if (!(up->u_flags & F_DUPUID) &&
1481 getpwuid((uid_t)(up->u_uid)) != NULL) {
1482 (void)close(ptmpfd);
1483 (void)pw_abort();
1484 errx(EXIT_FAILURE, "Uid %d is already in use",
1485 up->u_uid);
1486 }
1487 pwp->pw_uid = up->u_uid;
1488 }
1489 if (up->u_flags & F_GROUP) {
1490 /* if -g=uid was specified, check gid is unused */
1491 if (strcmp(up->u_primgrp, "=uid") == 0) {
1492 if (getgrgid((gid_t)(up->u_uid)) != NULL) {
1493 (void)close(ptmpfd);
1494 (void)pw_abort();
1495 errx(EXIT_FAILURE,
1496 "Gid %d is already in use",
1497 up->u_uid);
1498 }
1499 pwp->pw_gid = up->u_uid;
1500 } else if ((grp = getgrnam(up->u_primgrp)) != NULL) {
1501 pwp->pw_gid = grp->gr_gid;
1502 } else if (is_number(up->u_primgrp) &&
1503 (grp = getgrgid(
1504 (gid_t)atoi(up->u_primgrp))) != NULL) {
1505 pwp->pw_gid = grp->gr_gid;
1506 } else {
1507 (void)close(ptmpfd);
1508 (void)pw_abort();
1509 errx(EXIT_FAILURE, "Group %s not found",
1510 up->u_primgrp);
1511 }
1512 }
1513 if (up->u_flags & F_INACTIVE) {
1514 if (!scantime(&pwp->pw_change, up->u_inactive)) {
1515 warnx("Warning: inactive time `%s' invalid, "
1516 "password expiry off",
1517 up->u_inactive);
1518 }
1519 }
1520 if (up->u_flags & F_EXPIRE) {
1521 if (!scantime(&pwp->pw_expire, up->u_expire) ||
1522 pwp->pw_expire == -1) {
1523 warnx("Warning: expire time `%s' invalid, "
1524 "account expiry off",
1525 up->u_expire);
1526 pwp->pw_expire = 0;
1527 }
1528 }
1529 if (up->u_flags & F_COMMENT)
1530 pwp->pw_gecos = up->u_comment;
1531 if (up->u_flags & F_HOMEDIR)
1532 pwp->pw_dir = up->u_home;
1533 if (up->u_flags & F_SHELL)
1534 pwp->pw_shell = up->u_shell;
1535 #ifdef EXTENSIONS
1536 if (up->u_flags & F_CLASS) {
1537 if (!valid_class(up->u_class)) {
1538 (void)close(ptmpfd);
1539 (void)pw_abort();
1540 errx(EXIT_FAILURE, "No such login class `%s'",
1541 up->u_class);
1542 }
1543 pwp->pw_class = up->u_class;
1544 }
1545 #endif
1546 }
1547 loginc = strlen(login_name);
1548 while (fgets(buf, sizeof(buf), master) != NULL) {
1549 if ((colon = strchr(buf, ':')) == NULL) {
1550 warnx("Malformed entry `%s'. Skipping", buf);
1551 continue;
1552 }
1553 colonc = (size_t)(colon - buf);
1554 if (strncmp(login_name, buf, loginc) == 0 && loginc == colonc) {
1555 if (up != NULL) {
1556 len = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:"
1557 #ifdef EXTENSIONS
1558 "%s"
1559 #endif
1560 ":%ld:%ld:%s:%s:%s\n",
1561 newlogin,
1562 pwp->pw_passwd,
1563 pwp->pw_uid,
1564 pwp->pw_gid,
1565 #ifdef EXTENSIONS
1566 pwp->pw_class,
1567 #endif
1568 (long)pwp->pw_change,
1569 (long)pwp->pw_expire,
1570 pwp->pw_gecos,
1571 pwp->pw_dir,
1572 pwp->pw_shell);
1573 if (write(ptmpfd, buf, len) != len) {
1574 int serrno = errno;
1575 (void)close(ptmpfd);
1576 (void)pw_abort();
1577 errno = serrno;
1578 err(EXIT_FAILURE, "Can't add `%s'",
1579 buf);
1580 }
1581 }
1582 } else {
1583 len = strlen(buf);
1584 if ((cc = write(ptmpfd, buf, len)) != len) {
1585 int serrno = errno;
1586 (void)close(masterfd);
1587 (void)close(ptmpfd);
1588 (void)pw_abort();
1589 errno = serrno;
1590 err(EXIT_FAILURE, "Short write to /etc/ptmp "
1591 "(%ld not %ld chars)",
1592 (unsigned long)cc,
1593 (unsigned long)len);
1594 }
1595 }
1596 }
1597 if (up != NULL) {
1598 if ((up->u_flags & F_MKDIR) &&
1599 asystem("%s %s %s", MV, homedir, pwp->pw_dir) != 0) {
1600 int serrno = errno;
1601 (void)close(ptmpfd);
1602 (void)pw_abort();
1603 errno = serrno;
1604 err(EXIT_FAILURE, "Can't move `%s' to `%s'",
1605 homedir, pwp->pw_dir);
1606 }
1607 if (up->u_groupc > 0 &&
1608 !append_group(newlogin, up->u_groupc, up->u_groupv)) {
1609 (void)close(ptmpfd);
1610 (void)pw_abort();
1611 errx(EXIT_FAILURE, "Can't append `%s' to new groups",
1612 newlogin);
1613 }
1614 }
1615 (void)close(ptmpfd);
1616 #if PW_MKDB_ARGC == 2
1617 if (up != NULL && strcmp(login_name, newlogin) == 0) {
1618 error = pw_mkdb(login_name, 0);
1619 } else {
1620 error = pw_mkdb(NULL, 0);
1621 }
1622 #else
1623 error = pw_mkdb();
1624 #endif
1625 if (error < 0) {
1626 (void)pw_abort();
1627 errx(EXIT_FAILURE, "pw_mkdb failed");
1628 }
1629 if (up == NULL) {
1630 syslog(LOG_INFO, "User removed: name=%s", login_name);
1631 } else if (strcmp(login_name, newlogin) == 0) {
1632 syslog(LOG_INFO, "User information modified: name=%s, uid=%d, "
1633 "gid=%d, home=%s, shell=%s",
1634 login_name, pwp->pw_uid, pwp->pw_gid, pwp->pw_dir,
1635 pwp->pw_shell);
1636 } else {
1637 syslog(LOG_INFO, "User information modified: name=%s, "
1638 "new name=%s, uid=%d, gid=%d, home=%s, shell=%s",
1639 login_name, newlogin, pwp->pw_uid, pwp->pw_gid,
1640 pwp->pw_dir, pwp->pw_shell);
1641 }
1642 return 1;
1643 }
1644
1645 #ifdef EXTENSIONS
1646 /* see if we can find out the user struct */
1647 static struct passwd *
1648 find_user_info(const char *name)
1649 {
1650 struct passwd *pwp;
1651
1652 if ((pwp = getpwnam(name)) != NULL) {
1653 return pwp;
1654 }
1655 if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != NULL) {
1656 return pwp;
1657 }
1658 return NULL;
1659 }
1660 #endif
1661
1662 #ifdef EXTENSIONS
1663 /* see if we can find out the group struct */
1664 static struct group *
1665 find_group_info(const char *name)
1666 {
1667 struct group *grp;
1668
1669 if ((grp = getgrnam(name)) != NULL) {
1670 return grp;
1671 }
1672 if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != NULL) {
1673 return grp;
1674 }
1675 return NULL;
1676 }
1677 #endif
1678
1679 /* print out usage message, and then exit */
1680 void
1681 usermgmt_usage(const char *prog)
1682 {
1683 if (strcmp(prog, "useradd") == 0) {
1684 (void)fprintf(stderr, "usage: %s -D [-F] [-b base-dir] "
1685 "[-e expiry-time] [-f inactive-time]\n"
1686 "\t[-g gid | name | =uid] [-k skel-dir] [-L login-class]\n"
1687 "\t[-r lowuid..highuid] [-s shell]\n", prog);
1688 (void)fprintf(stderr, "usage: %s [-moSv] [-b base-dir] "
1689 "[-c comment] [-d home-dir] [-e expiry-time]\n"
1690 "\t[-f inactive-time] [-G secondary-group] "
1691 "[-g gid | name | =uid]\n"
1692 "\t[-k skeletondir] [-L login-class] [-p password]"
1693 "[-r lowuid..highuid]\n"
1694 "\t[-s shell] [-u uid] user\n",
1695 prog);
1696 } else if (strcmp(prog, "usermod") == 0) {
1697 (void)fprintf(stderr, "usage: %s [-FmoSv] [-C yes/no] "
1698 "[-c comment] [-d home-dir] [-e expiry-time]\n"
1699 "\t[-f inactive] [-G secondary-group] "
1700 "[-g gid | name | =uid]\n"
1701 "\t[-L login-class] [-l new-login] [-p password] "
1702 "[-s shell] [-u uid]\n"
1703 "\tuser\n", prog);
1704 } else if (strcmp(prog, "userdel") == 0) {
1705 (void)fprintf(stderr, "usage: %s -D [-p preserve-value]\n", prog);
1706 (void)fprintf(stderr,
1707 "usage: %s [-rSv] [-p preserve-value] user\n", prog);
1708 #ifdef EXTENSIONS
1709 } else if (strcmp(prog, "userinfo") == 0) {
1710 (void)fprintf(stderr, "usage: %s [-ev] user\n", prog);
1711 #endif
1712 } else if (strcmp(prog, "groupadd") == 0) {
1713 (void)fprintf(stderr, "usage: %s [-g gid] [-o]"
1714 " [-r lowgid..highgid] [-v] group\n", prog);
1715 } else if (strcmp(prog, "groupdel") == 0) {
1716 (void)fprintf(stderr, "usage: %s [-v] group\n", prog);
1717 } else if (strcmp(prog, "groupmod") == 0) {
1718 (void)fprintf(stderr,
1719 "usage: %s [-g gid] [-o] [-n newname] [-v] group\n", prog);
1720 } else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) {
1721 (void)fprintf(stderr,
1722 "usage: %s ( add | del | mod | info ) ...\n", prog);
1723 #ifdef EXTENSIONS
1724 } else if (strcmp(prog, "groupinfo") == 0) {
1725 (void)fprintf(stderr, "usage: %s [-e] [-v] group\n", prog);
1726 #endif
1727 }
1728 exit(EXIT_FAILURE);
1729 /* NOTREACHED */
1730 }
1731
1732 #ifdef EXTENSIONS
1733 #define ADD_OPT_EXTENSIONS "p:r:vL:S"
1734 #else
1735 #define ADD_OPT_EXTENSIONS
1736 #endif
1737
1738 int
1739 useradd(int argc, char **argv)
1740 {
1741 user_t u;
1742 int defaultfield;
1743 int bigD;
1744 int c;
1745 #ifdef EXTENSIONS
1746 int i;
1747 #endif
1748
1749 (void)memset(&u, 0, sizeof(u));
1750 read_defaults(&u);
1751 u.u_uid = -1;
1752 defaultfield = bigD = 0;
1753 while ((c = getopt(argc, argv, "DFG:b:c:d:e:f:g:k:mou:s:"
1754 ADD_OPT_EXTENSIONS)) != -1) {
1755 switch(c) {
1756 case 'D':
1757 bigD = 1;
1758 break;
1759 case 'F':
1760 /*
1761 * Setting -1 will force the new user to
1762 * change their password as soon as they
1763 * next log in - passwd(5).
1764 */
1765 defaultfield = 1;
1766 memsave(&u.u_inactive, "-1", strlen("-1"));
1767 break;
1768 case 'G':
1769 while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
1770 u.u_groupc < NGROUPS_MAX) {
1771 if (u.u_groupv[u.u_groupc][0] != 0) {
1772 u.u_groupc++;
1773 }
1774 }
1775 if (optarg != NULL) {
1776 warnx("Truncated list of secondary groups "
1777 "to %d entries", NGROUPS_MAX);
1778 }
1779 break;
1780 #ifdef EXTENSIONS
1781 case 'S':
1782 u.u_allow_samba = 1;
1783 break;
1784 #endif
1785 case 'b':
1786 defaultfield = 1;
1787 memsave(&u.u_basedir, optarg, strlen(optarg));
1788 break;
1789 case 'c':
1790 memsave(&u.u_comment, optarg, strlen(optarg));
1791 break;
1792 case 'd':
1793 memsave(&u.u_home, optarg, strlen(optarg));
1794 u.u_flags |= F_HOMEDIR;
1795 break;
1796 case 'e':
1797 defaultfield = 1;
1798 memsave(&u.u_expire, optarg, strlen(optarg));
1799 break;
1800 case 'f':
1801 defaultfield = 1;
1802 memsave(&u.u_inactive, optarg, strlen(optarg));
1803 break;
1804 case 'g':
1805 defaultfield = 1;
1806 memsave(&u.u_primgrp, optarg, strlen(optarg));
1807 break;
1808 case 'k':
1809 defaultfield = 1;
1810 memsave(&u.u_skeldir, optarg, strlen(optarg));
1811 break;
1812 #ifdef EXTENSIONS
1813 case 'L':
1814 defaultfield = 1;
1815 memsave(&u.u_class, optarg, strlen(optarg));
1816 break;
1817 #endif
1818 case 'm':
1819 u.u_flags |= F_MKDIR;
1820 break;
1821 case 'o':
1822 u.u_flags |= F_DUPUID;
1823 break;
1824 #ifdef EXTENSIONS
1825 case 'p':
1826 memsave(&u.u_password, optarg, strlen(optarg));
1827 break;
1828 #endif
1829 #ifdef EXTENSIONS
1830 case 'r':
1831 defaultfield = 1;
1832 (void)save_range(&u, optarg);
1833 break;
1834 #endif
1835 case 's':
1836 defaultfield = 1;
1837 memsave(&u.u_shell, optarg, strlen(optarg));
1838 break;
1839 case 'u':
1840 u.u_uid = check_numeric(optarg, "uid");
1841 break;
1842 #ifdef EXTENSIONS
1843 case 'v':
1844 verbose = 1;
1845 break;
1846 #endif
1847 default:
1848 usermgmt_usage("useradd");
1849 /* NOTREACHED */
1850 }
1851 }
1852 if (bigD) {
1853 if (defaultfield) {
1854 checkeuid();
1855 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1856 }
1857 (void)printf("group\t\t%s\n", u.u_primgrp);
1858 (void)printf("base_dir\t%s\n", u.u_basedir);
1859 (void)printf("skel_dir\t%s\n", u.u_skeldir);
1860 (void)printf("shell\t\t%s\n", u.u_shell);
1861 #ifdef EXTENSIONS
1862 (void)printf("class\t\t%s\n", u.u_class);
1863 #endif
1864 (void)printf("inactive\t%s\n", (u.u_inactive == NULL) ?
1865 UNSET_INACTIVE : u.u_inactive);
1866 (void)printf("expire\t\t%s\n", (u.u_expire == NULL) ?
1867 UNSET_EXPIRY : u.u_expire);
1868 #ifdef EXTENSIONS
1869 for (i = 0 ; i < u.u_rc ; i++) {
1870 (void)printf("range\t\t%d..%d\n",
1871 u.u_rv[i].r_from, u.u_rv[i].r_to);
1872 }
1873 #endif
1874 return EXIT_SUCCESS;
1875 }
1876 argc -= optind;
1877 argv += optind;
1878 if (argc != 1) {
1879 usermgmt_usage("useradd");
1880 }
1881 checkeuid();
1882 openlog("useradd", LOG_PID, LOG_USER);
1883 return adduser(*argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1884 }
1885
1886 #ifdef EXTENSIONS
1887 #define MOD_OPT_EXTENSIONS "p:vL:S"
1888 #else
1889 #define MOD_OPT_EXTENSIONS
1890 #endif
1891
1892 int
1893 usermod(int argc, char **argv)
1894 {
1895 user_t u;
1896 char newuser[MaxUserNameLen + 1];
1897 int c, have_new_user;
1898
1899 (void)memset(&u, 0, sizeof(u));
1900 (void)memset(newuser, 0, sizeof(newuser));
1901 read_defaults(&u);
1902 have_new_user = 0;
1903 u.u_locked = -1;
1904 while ((c = getopt(argc, argv, "C:FG:c:d:e:f:g:l:mos:u:"
1905 MOD_OPT_EXTENSIONS)) != -1) {
1906 switch(c) {
1907 case 'G':
1908 while ((u.u_groupv[u.u_groupc] =
1909 strsep(&optarg, ",")) != NULL &&
1910 u.u_groupc < NGROUPS_MAX) {
1911 if (u.u_groupv[u.u_groupc][0] != 0) {
1912 u.u_groupc++;
1913 }
1914 }
1915 if (optarg != NULL) {
1916 warnx("Truncated list of secondary groups "
1917 "to %d entries", NGROUPS_MAX);
1918 }
1919 u.u_flags |= F_SECGROUP;
1920 break;
1921 #ifdef EXTENSIONS
1922 case 'S':
1923 u.u_allow_samba = 1;
1924 break;
1925 #endif
1926 case 'c':
1927 memsave(&u.u_comment, optarg, strlen(optarg));
1928 u.u_flags |= F_COMMENT;
1929 break;
1930 case 'C':
1931 if (strcasecmp(optarg, "yes") == 0)
1932 u.u_locked = LOCK;
1933 else if (strcasecmp(optarg, "no") == 0)
1934 u.u_locked = UNLOCK;
1935 else
1936 /* No idea. */
1937 errx(1, "Please type 'yes' or 'no'");
1938
1939 break;
1940 case 'F':
1941 memsave(&u.u_inactive, "-1", strlen("-1"));
1942 u.u_flags |= F_INACTIVE;
1943 break;
1944 case 'd':
1945 memsave(&u.u_home, optarg, strlen(optarg));
1946 u.u_flags |= F_HOMEDIR;
1947 break;
1948 case 'e':
1949 memsave(&u.u_expire, optarg, strlen(optarg));
1950 u.u_flags |= F_EXPIRE;
1951 break;
1952 case 'f':
1953 memsave(&u.u_inactive, optarg, strlen(optarg));
1954 u.u_flags |= F_INACTIVE;
1955 break;
1956 case 'g':
1957 memsave(&u.u_primgrp, optarg, strlen(optarg));
1958 u.u_flags |= F_GROUP;
1959 break;
1960 case 'l':
1961 (void)strlcpy(newuser, optarg, sizeof(newuser));
1962 have_new_user = 1;
1963 u.u_flags |= F_USERNAME;
1964 break;
1965 #ifdef EXTENSIONS
1966 case 'L':
1967 memsave(&u.u_class, optarg, strlen(optarg));
1968 u.u_flags |= F_CLASS;
1969 break;
1970 #endif
1971 case 'm':
1972 u.u_flags |= F_MKDIR;
1973 break;
1974 case 'o':
1975 u.u_flags |= F_DUPUID;
1976 break;
1977 #ifdef EXTENSIONS
1978 case 'p':
1979 memsave(&u.u_password, optarg, strlen(optarg));
1980 u.u_flags |= F_PASSWORD;
1981 break;
1982 #endif
1983 case 's':
1984 memsave(&u.u_shell, optarg, strlen(optarg));
1985 u.u_flags |= F_SHELL;
1986 break;
1987 case 'u':
1988 u.u_uid = check_numeric(optarg, "uid");
1989 u.u_flags |= F_UID;
1990 break;
1991 #ifdef EXTENSIONS
1992 case 'v':
1993 verbose = 1;
1994 break;
1995 #endif
1996 default:
1997 usermgmt_usage("usermod");
1998 /* NOTREACHED */
1999 }
2000 }
2001 if ((u.u_flags & F_MKDIR) && !(u.u_flags & F_HOMEDIR) &&
2002 !(u.u_flags & F_USERNAME)) {
2003 warnx("Option 'm' useless without 'd' or 'l' -- ignored");
2004 u.u_flags &= ~F_MKDIR;
2005 }
2006 argc -= optind;
2007 argv += optind;
2008 if (argc != 1) {
2009 usermgmt_usage("usermod");
2010 }
2011 checkeuid();
2012 openlog("usermod", LOG_PID, LOG_USER);
2013 return moduser(*argv, (have_new_user) ? newuser : *argv, &u,
2014 u.u_allow_samba) ? EXIT_SUCCESS : EXIT_FAILURE;
2015 }
2016
2017 #ifdef EXTENSIONS
2018 #define DEL_OPT_EXTENSIONS "Dp:vS"
2019 #else
2020 #define DEL_OPT_EXTENSIONS
2021 #endif
2022
2023 int
2024 userdel(int argc, char **argv)
2025 {
2026 struct passwd *pwp;
2027 user_t u;
2028 char password[PasswordLength + 1];
2029 int defaultfield;
2030 int rmhome;
2031 int bigD;
2032 int c;
2033
2034 (void)memset(&u, 0, sizeof(u));
2035 read_defaults(&u);
2036 defaultfield = bigD = rmhome = 0;
2037 while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) {
2038 switch(c) {
2039 #ifdef EXTENSIONS
2040 case 'D':
2041 bigD = 1;
2042 break;
2043 #endif
2044 #ifdef EXTENSIONS
2045 case 'S':
2046 u.u_allow_samba = 1;
2047 break;
2048 #endif
2049 #ifdef EXTENSIONS
2050 case 'p':
2051 defaultfield = 1;
2052 u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 :
2053 (strcmp(optarg, "yes") == 0) ? 1 :
2054 atoi(optarg);
2055 break;
2056 #endif
2057 case 'r':
2058 rmhome = 1;
2059 break;
2060 #ifdef EXTENSIONS
2061 case 'v':
2062 verbose = 1;
2063 break;
2064 #endif
2065 default:
2066 usermgmt_usage("userdel");
2067 /* NOTREACHED */
2068 }
2069 }
2070 #ifdef EXTENSIONS
2071 if (bigD) {
2072 if (defaultfield) {
2073 checkeuid();
2074 return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
2075 }
2076 (void)printf("preserve\t%s\n", (u.u_preserve) ? "true" :
2077 "false");
2078 return EXIT_SUCCESS;
2079 }
2080 #endif
2081 argc -= optind;
2082 argv += optind;
2083 if (argc != 1) {
2084 usermgmt_usage("userdel");
2085 }
2086 checkeuid();
2087 if ((pwp = getpwnam(*argv)) == NULL) {
2088 warnx("No such user `%s'", *argv);
2089 return EXIT_FAILURE;
2090 }
2091 if (rmhome)
2092 (void)removehomedir(pwp->pw_name, pwp->pw_uid, pwp->pw_dir);
2093 if (u.u_preserve) {
2094 u.u_flags |= F_SHELL;
2095 memsave(&u.u_shell, NOLOGIN, strlen(NOLOGIN));
2096 (void)memset(password, '*', DES_Len);
2097 password[DES_Len] = 0;
2098 memsave(&u.u_password, password, strlen(password));
2099 u.u_flags |= F_PASSWORD;
2100 openlog("userdel", LOG_PID, LOG_USER);
2101 return moduser(*argv, *argv, &u, u.u_allow_samba) ?
2102 EXIT_SUCCESS : EXIT_FAILURE;
2103 }
2104 if (!rm_user_from_groups(*argv)) {
2105 return 0;
2106 }
2107 openlog("userdel", LOG_PID, LOG_USER);
2108 return moduser(*argv, *argv, NULL, u.u_allow_samba) ?
2109 EXIT_SUCCESS : EXIT_FAILURE;
2110 }
2111
2112 #ifdef EXTENSIONS
2113 #define GROUP_ADD_OPT_EXTENSIONS "r:v"
2114 #else
2115 #define GROUP_ADD_OPT_EXTENSIONS
2116 #endif
2117
2118 /* add a group */
2119 int
2120 groupadd(int argc, char **argv)
2121 {
2122 int dupgid;
2123 int gid;
2124 int c;
2125 int lowgid;
2126 int highgid;
2127
2128 gid = -1;
2129 dupgid = 0;
2130 lowgid = LowGid;
2131 highgid = HighGid;
2132 while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) {
2133 switch(c) {
2134 case 'g':
2135 gid = check_numeric(optarg, "gid");
2136 break;
2137 case 'o':
2138 dupgid = 1;
2139 break;
2140 #ifdef EXTENSIONS
2141 case 'r':
2142 if (sscanf(optarg, "%d..%d", &lowgid, &highgid) != 2) {
2143 errx(EXIT_FAILURE, "Bad range `%s'", optarg);
2144 }
2145 break;
2146 case 'v':
2147 verbose = 1;
2148 break;
2149 #endif
2150 default:
2151 usermgmt_usage("groupadd");
2152 /* NOTREACHED */
2153 }
2154 }
2155 argc -= optind;
2156 argv += optind;
2157 if (argc != 1) {
2158 usermgmt_usage("groupadd");
2159 }
2160 checkeuid();
2161 if (gid < 0 && !getnextgid(&gid, lowgid, highgid)) {
2162 err(EXIT_FAILURE, "Can't add group: can't get next gid");
2163 }
2164 if (!dupgid && getgrgid((gid_t) gid) != NULL) {
2165 errx(EXIT_FAILURE, "Can't add group: gid %d is a duplicate",
2166 gid);
2167 }
2168 if (!valid_group(*argv)) {
2169 warnx("Invalid group name `%s'", *argv);
2170 }
2171 openlog("groupadd", LOG_PID, LOG_USER);
2172 if (!creategid(*argv, gid, "")) {
2173 errx(EXIT_FAILURE, "Can't add group: problems with %s file",
2174 _PATH_GROUP);
2175 }
2176 return EXIT_SUCCESS;
2177 }
2178
2179 #ifdef EXTENSIONS
2180 #define GROUP_DEL_OPT_EXTENSIONS "v"
2181 #else
2182 #define GROUP_DEL_OPT_EXTENSIONS
2183 #endif
2184
2185 /* remove a group */
2186 int
2187 groupdel(int argc, char **argv)
2188 {
2189 int c;
2190
2191 while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) {
2192 switch(c) {
2193 #ifdef EXTENSIONS
2194 case 'v':
2195 verbose = 1;
2196 break;
2197 #endif
2198 default:
2199 usermgmt_usage("groupdel");
2200 /* NOTREACHED */
2201 }
2202 }
2203 argc -= optind;
2204 argv += optind;
2205 if (argc != 1) {
2206 usermgmt_usage("groupdel");
2207 }
2208 if (getgrnam(*argv) == NULL) {
2209 errx(EXIT_FAILURE, "No such group `%s'", *argv);
2210 }
2211 checkeuid();
2212 openlog("groupdel", LOG_PID, LOG_USER);
2213 if (!modify_gid(*argv, NULL)) {
2214 errx(EXIT_FAILURE, "Can't change `%s' file", _PATH_GROUP);
2215 }
2216 return EXIT_SUCCESS;
2217 }
2218
2219 #ifdef EXTENSIONS
2220 #define GROUP_MOD_OPT_EXTENSIONS "v"
2221 #else
2222 #define GROUP_MOD_OPT_EXTENSIONS
2223 #endif
2224
2225 /* modify a group */
2226 int
2227 groupmod(int argc, char **argv)
2228 {
2229 struct group *grp;
2230 char buf[MaxEntryLen];
2231 char *newname;
2232 char **cpp;
2233 int dupgid;
2234 int gid;
2235 int cc;
2236 int c;
2237
2238 gid = -1;
2239 dupgid = 0;
2240 newname = NULL;
2241 while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) {
2242 switch(c) {
2243 case 'g':
2244 gid = check_numeric(optarg, "gid");
2245 break;
2246 case 'o':
2247 dupgid = 1;
2248 break;
2249 case 'n':
2250 memsave(&newname, optarg, strlen(optarg));
2251 break;
2252 #ifdef EXTENSIONS
2253 case 'v':
2254 verbose = 1;
2255 break;
2256 #endif
2257 default:
2258 usermgmt_usage("groupmod");
2259 /* NOTREACHED */
2260 }
2261 }
2262 argc -= optind;
2263 argv += optind;
2264 if (argc != 1) {
2265 usermgmt_usage("groupmod");
2266 }
2267 checkeuid();
2268 if (gid < 0 && newname == NULL) {
2269 errx(EXIT_FAILURE, "Nothing to change");
2270 }
2271 if (dupgid && gid < 0) {
2272 errx(EXIT_FAILURE, "Duplicate which gid?");
2273 }
2274 if ((grp = find_group_info(*argv)) == NULL) {
2275 errx(EXIT_FAILURE, "Can't find group `%s' to modify", *argv);
2276 }
2277 if (!is_local(*argv, _PATH_GROUP)) {
2278 errx(EXIT_FAILURE, "Group `%s' must be a local group", *argv);
2279 }
2280 if (newname != NULL && !valid_group(newname)) {
2281 warnx("Invalid group name `%s'", newname);
2282 }
2283 cc = snprintf(buf, sizeof(buf), "%s:%s:%d:",
2284 (newname) ? newname : grp->gr_name,
2285 grp->gr_passwd,
2286 (gid < 0) ? grp->gr_gid : gid);
2287 for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) {
2288 cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp,
2289 (cpp[1] == NULL) ? "" : ",");
2290 }
2291 cc += snprintf(&buf[cc], sizeof(buf) - cc, "\n");
2292 openlog("groupmod", LOG_PID, LOG_USER);
2293 if (!modify_gid(*argv, buf)) {
2294 errx(EXIT_FAILURE, "Can't change %s file", _PATH_GROUP);
2295 }
2296 return EXIT_SUCCESS;
2297 }
2298
2299 #ifdef EXTENSIONS
2300 /* display user information */
2301 int
2302 userinfo(int argc, char **argv)
2303 {
2304 struct passwd *pwp;
2305 struct group *grp;
2306 char buf[MaxEntryLen];
2307 char **cpp;
2308 int exists;
2309 int cc;
2310 int i;
2311
2312 exists = 0;
2313 while ((i = getopt(argc, argv, "ev")) != -1) {
2314 switch(i) {
2315 case 'e':
2316 exists = 1;
2317 break;
2318 case 'v':
2319 verbose = 1;
2320 break;
2321 default:
2322 usermgmt_usage("userinfo");
2323 /* NOTREACHED */
2324 }
2325 }
2326 argc -= optind;
2327 argv += optind;
2328 if (argc != 1) {
2329 usermgmt_usage("userinfo");
2330 }
2331 pwp = find_user_info(*argv);
2332 if (exists) {
2333 exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE);
2334 }
2335 if (pwp == NULL) {
2336 errx(EXIT_FAILURE, "Can't find user `%s'", *argv);
2337 }
2338 (void)printf("login\t%s\n", pwp->pw_name);
2339 (void)printf("passwd\t%s\n", pwp->pw_passwd);
2340 (void)printf("uid\t%d\n", pwp->pw_uid);
2341 for (cc = 0 ; (grp = getgrent()) != NULL ; ) {
2342 for (cpp = grp->gr_mem ; *cpp ; cpp++) {
2343 if (strcmp(*cpp, *argv) == 0 &&
2344 grp->gr_gid != pwp->pw_gid) {
2345 cc += snprintf(&buf[cc], sizeof(buf) - cc,
2346 "%s ", grp->gr_name);
2347 }
2348 }
2349 }
2350 if ((grp = getgrgid(pwp->pw_gid)) == NULL) {
2351 (void)printf("groups\t%d %s\n", pwp->pw_gid, buf);
2352 } else {
2353 (void)printf("groups\t%s %s\n", grp->gr_name, buf);
2354 }
2355 (void)printf("change\t%s", pwp->pw_change ?
2356 ctime(&pwp->pw_change) : "NEVER\n");
2357 #ifdef EXTENSIONS
2358 (void)printf("class\t%s\n", pwp->pw_class);
2359 #endif
2360 (void)printf("gecos\t%s\n", pwp->pw_gecos);
2361 (void)printf("dir\t%s\n", pwp->pw_dir);
2362 (void)printf("shell\t%s\n", pwp->pw_shell);
2363 (void)printf("expire\t%s", pwp->pw_expire ?
2364 ctime(&pwp->pw_expire) : "NEVER\n");
2365 return EXIT_SUCCESS;
2366 }
2367 #endif
2368
2369 #ifdef EXTENSIONS
2370 /* display user information */
2371 int
2372 groupinfo(int argc, char **argv)
2373 {
2374 struct group *grp;
2375 char **cpp;
2376 int exists;
2377 int i;
2378
2379 exists = 0;
2380 while ((i = getopt(argc, argv, "ev")) != -1) {
2381 switch(i) {
2382 case 'e':
2383 exists = 1;
2384 break;
2385 case 'v':
2386 verbose = 1;
2387 break;
2388 default:
2389 usermgmt_usage("groupinfo");
2390 /* NOTREACHED */
2391 }
2392 }
2393 argc -= optind;
2394 argv += optind;
2395 if (argc != 1) {
2396 usermgmt_usage("groupinfo");
2397 }
2398 grp = find_group_info(*argv);
2399 if (exists) {
2400 exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE);
2401 }
2402 if (grp == NULL) {
2403 errx(EXIT_FAILURE, "Can't find group `%s'", *argv);
2404 }
2405 (void)printf("name\t%s\n", grp->gr_name);
2406 (void)printf("passwd\t%s\n", grp->gr_passwd);
2407 (void)printf("gid\t%d\n", grp->gr_gid);
2408 (void)printf("members\t");
2409 for (cpp = grp->gr_mem ; *cpp ; cpp++) {
2410 (void)printf("%s", *cpp);
2411 if (*(cpp + 1))
2412 (void) printf(", ");
2413 }
2414 (void)fputc('\n', stdout);
2415 return EXIT_SUCCESS;
2416 }
2417 #endif
2418