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