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