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