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