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