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