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