xref: /openbsd-src/usr.sbin/user/user.c (revision 8e836504eae97cf1437d92d79fa2a1ba2422193f)
1*8e836504Smillert /* $OpenBSD: user.c,v 1.131 2023/05/18 18:29:28 millert Exp $ */
2658e04adSmillert /* $NetBSD: user.c,v 1.69 2003/04/14 17:40:07 agc Exp $ */
3e02380dbSjakob 
4e02380dbSjakob /*
5e02380dbSjakob  * Copyright (c) 1999 Alistair G. Crooks.  All rights reserved.
6e02380dbSjakob  *
7e02380dbSjakob  * Redistribution and use in source and binary forms, with or without
8e02380dbSjakob  * modification, are permitted provided that the following conditions
9e02380dbSjakob  * are met:
10e02380dbSjakob  * 1. Redistributions of source code must retain the above copyright
11e02380dbSjakob  *    notice, this list of conditions and the following disclaimer.
12e02380dbSjakob  * 2. Redistributions in binary form must reproduce the above copyright
13e02380dbSjakob  *    notice, this list of conditions and the following disclaimer in the
14e02380dbSjakob  *    documentation and/or other materials provided with the distribution.
157a3d14ecSjsg  * 3. The name of the author may not be used to endorse or promote
16e02380dbSjakob  *    products derived from this software without specific prior written
17e02380dbSjakob  *    permission.
18e02380dbSjakob  *
19e02380dbSjakob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20e02380dbSjakob  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21e02380dbSjakob  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22e02380dbSjakob  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23e02380dbSjakob  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24e02380dbSjakob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25e02380dbSjakob  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26e02380dbSjakob  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27e02380dbSjakob  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28e02380dbSjakob  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29e02380dbSjakob  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30e02380dbSjakob  */
31206491c0Sjakob 
32b9fc9a72Sderaadt #include <sys/types.h>
33e02380dbSjakob #include <sys/stat.h>
34*8e836504Smillert #include <sys/wait.h>
35e02380dbSjakob 
36e02380dbSjakob #include <ctype.h>
37e02380dbSjakob #include <dirent.h>
38e02380dbSjakob #include <err.h>
39ffb4dd05Sguenther #include <errno.h>
40e02380dbSjakob #include <fcntl.h>
41e02380dbSjakob #include <grp.h>
42e2fea6e1Smestre #include <limits.h>
433162cc8dSmillert #include <login_cap.h>
443c76c7c0Smillert #include <paths.h>
45e02380dbSjakob #include <pwd.h>
46e02380dbSjakob #include <stdio.h>
47e02380dbSjakob #include <stdlib.h>
48e02380dbSjakob #include <string.h>
49f65c14a9Smillert #include <syslog.h>
50e02380dbSjakob #include <time.h>
51e02380dbSjakob #include <unistd.h>
52e02380dbSjakob #include <util.h>
53e02380dbSjakob 
54e02380dbSjakob #include "usermgmt.h"
55e02380dbSjakob 
563c76c7c0Smillert 
57e02380dbSjakob /* this struct describes a uid range */
58e02380dbSjakob typedef struct range_t {
595b0f5f19Smillert 	uid_t	r_from;		/* low uid */
605b0f5f19Smillert 	uid_t	r_to;		/* high uid */
61e02380dbSjakob } range_t;
62e02380dbSjakob 
63e02380dbSjakob /* this struct encapsulates the user information */
64e02380dbSjakob typedef struct user_t {
653c76c7c0Smillert 	int		u_flags;		/* see below */
665b0f5f19Smillert 	uid_t		u_uid;			/* uid of user */
67e02380dbSjakob 	char	       *u_password;		/* encrypted password */
68e02380dbSjakob 	char	       *u_comment;		/* comment field */
69e02380dbSjakob 	char	       *u_home;		/* home directory */
70e02380dbSjakob 	char	       *u_primgrp;		/* primary group */
71e02380dbSjakob 	int		u_groupc;		/* # of secondary groups */
72be1ff426Smillert 	const char     *u_groupv[NGROUPS_MAX];	/* secondary groups */
73e02380dbSjakob 	char	       *u_shell;		/* user's shell */
74e02380dbSjakob 	char	       *u_basedir;		/* base directory for home */
7544e41a3bSotto 	char	       *u_expire;		/* when account will expire */
7644e41a3bSotto 	char	       *u_inactive;		/* when password will expire */
77e02380dbSjakob 	char	       *u_skeldir;		/* directory for startup files */
783c76c7c0Smillert 	char	       *u_class;		/* login class */
795b0f5f19Smillert 	unsigned int	u_rsize;		/* size of range array */
805b0f5f19Smillert 	unsigned int	u_rc;			/* # of ranges */
81e02380dbSjakob 	range_t	       *u_rv;			/* the ranges */
825b0f5f19Smillert 	unsigned int	u_defrc;		/* # of ranges in defaults */
83e02380dbSjakob 	int		u_preserve;		/* preserve uids on deletion */
84e02380dbSjakob } user_t;
85e02380dbSjakob 
863c76c7c0Smillert /* flags for which fields of the user_t replace the passwd entry */
873c76c7c0Smillert enum {
883c76c7c0Smillert 	F_COMMENT	= 0x0001,
893c76c7c0Smillert 	F_DUPUID	= 0x0002,
903c76c7c0Smillert 	F_EXPIRE	= 0x0004,
913c76c7c0Smillert 	F_GROUP		= 0x0008,
923c76c7c0Smillert 	F_HOMEDIR	= 0x0010,
933c76c7c0Smillert 	F_MKDIR		= 0x0020,
943c76c7c0Smillert 	F_INACTIVE	= 0x0040,
953c76c7c0Smillert 	F_PASSWORD	= 0x0080,
963c76c7c0Smillert 	F_SECGROUP	= 0x0100,
973c76c7c0Smillert 	F_SHELL		= 0x0200,
983c76c7c0Smillert 	F_UID		= 0x0400,
993c76c7c0Smillert 	F_USERNAME	= 0x0800,
1000e0fd575Sajacoutot 	F_CLASS		= 0x1000,
101c742a9b2Sajacoutot 	F_SETSECGROUP	= 0x4000,
102c742a9b2Sajacoutot 	F_ACCTLOCK	= 0x8000,
103c742a9b2Sajacoutot 	F_ACCTUNLOCK	= 0x10000
1043c76c7c0Smillert };
1053c76c7c0Smillert 
106*8e836504Smillert /* flags for runas() */
107*8e836504Smillert enum {
108*8e836504Smillert 	RUNAS_DUP_DEVNULL	= 0x01,
109*8e836504Smillert 	RUNAS_IGNORE_EXITVAL	= 0x02
110*8e836504Smillert };
111*8e836504Smillert 
112e02380dbSjakob #define CONFFILE		"/etc/usermgmt.conf"
113efe89df2Smestre #define _PATH_NONEXISTENT	"/nonexistent"
114e02380dbSjakob 
115e02380dbSjakob #ifndef DEF_GROUP
116fbf3c684Sajacoutot #define DEF_GROUP	"=uid"
117e02380dbSjakob #endif
118e02380dbSjakob 
119e02380dbSjakob #ifndef DEF_BASEDIR
120e02380dbSjakob #define DEF_BASEDIR	"/home"
121e02380dbSjakob #endif
122e02380dbSjakob 
123e02380dbSjakob #ifndef DEF_SKELDIR
124e02380dbSjakob #define DEF_SKELDIR	"/etc/skel"
125e02380dbSjakob #endif
126e02380dbSjakob 
127e02380dbSjakob #ifndef DEF_SHELL
128b47c3c13Sderaadt #define DEF_SHELL	_PATH_KSHELL
129e02380dbSjakob #endif
130e02380dbSjakob 
131e02380dbSjakob #ifndef DEF_COMMENT
132e02380dbSjakob #define DEF_COMMENT	""
133e02380dbSjakob #endif
134e02380dbSjakob 
135e02380dbSjakob #ifndef DEF_LOWUID
136e02380dbSjakob #define DEF_LOWUID	1000
137e02380dbSjakob #endif
138e02380dbSjakob 
139e02380dbSjakob #ifndef DEF_HIGHUID
140e02380dbSjakob #define DEF_HIGHUID	60000
141e02380dbSjakob #endif
142e02380dbSjakob 
143e02380dbSjakob #ifndef DEF_INACTIVE
144e02380dbSjakob #define DEF_INACTIVE	0
145e02380dbSjakob #endif
146e02380dbSjakob 
147e02380dbSjakob #ifndef DEF_EXPIRE
1483c76c7c0Smillert #define DEF_EXPIRE	NULL
1493c76c7c0Smillert #endif
1503c76c7c0Smillert 
1513c76c7c0Smillert #ifndef DEF_CLASS
1523c76c7c0Smillert #define DEF_CLASS	""
153e02380dbSjakob #endif
154e02380dbSjakob 
155e02380dbSjakob #ifndef WAITSECS
156e02380dbSjakob #define WAITSECS	10
157e02380dbSjakob #endif
158e02380dbSjakob 
159e02380dbSjakob #ifndef NOBODY_UID
160e02380dbSjakob #define NOBODY_UID	32767
161e02380dbSjakob #endif
162e02380dbSjakob 
163e02380dbSjakob /* some useful constants */
164e02380dbSjakob enum {
165e02380dbSjakob 	MaxShellNameLen = 256,
166b9fc9a72Sderaadt 	MaxFileNameLen = PATH_MAX,
167f5c2ddb1Smillert 	MaxUserNameLen = _PW_NAME_LEN,
168e02380dbSjakob 	MaxCommandLen = 2048,
169ee38b178Sjakob 	PasswordLength = _PASSWORD_LEN,
170e02380dbSjakob 	LowGid = DEF_LOWUID,
171e02380dbSjakob 	HighGid = DEF_HIGHUID
172e02380dbSjakob };
173e02380dbSjakob 
174e02380dbSjakob /* Full paths of programs used here */
1753c76c7c0Smillert #define CHMOD		"/bin/chmod"
176e406556cSmillert #define CHOWN		"/sbin/chown"
177e02380dbSjakob #define MKDIR		"/bin/mkdir"
178e02380dbSjakob #define MV		"/bin/mv"
179e02380dbSjakob #define NOLOGIN		"/sbin/nologin"
180eb9ee6b2Smillert #define CP		"/bin/cp"
181e02380dbSjakob #define RM		"/bin/rm"
182e02380dbSjakob 
183ddcf21ceSmillert #define UNSET_INACTIVE	"Null (unset)"
184e02380dbSjakob #define UNSET_EXPIRY	"Null (unset)"
185e02380dbSjakob 
186e2fea6e1Smestre static int adduser(char *, user_t *);
187e2fea6e1Smestre static int append_group(char *, int, const char **);
188e2fea6e1Smestre static int copydotfiles(char *, char *);
189e2fea6e1Smestre static int creategid(char *, gid_t, const char *);
190e2fea6e1Smestre static int getnextgid(uid_t *, uid_t, uid_t);
191e2fea6e1Smestre static int getnextuid(int, uid_t *, uid_t, uid_t);
192e2fea6e1Smestre static int is_local(char *, const char *);
193e2fea6e1Smestre static int modify_gid(char *, char *);
194e2fea6e1Smestre static int moduser(char *, char *, user_t *);
195e2fea6e1Smestre static int removehomedir(const char *, uid_t, const char *);
196e2fea6e1Smestre static int rm_user_from_groups(char *);
197e2fea6e1Smestre static int save_range(user_t *, char *);
198e2fea6e1Smestre static int scantime(time_t *, char *);
199e2fea6e1Smestre static int setdefaults(user_t *);
200e2fea6e1Smestre static int valid_class(char *);
201e2fea6e1Smestre static int valid_group(char *);
202e2fea6e1Smestre static int valid_login(char *);
203e2fea6e1Smestre static size_t expand_len(const char *, const char *);
204e2fea6e1Smestre static struct group *find_group_info(const char *);
205e2fea6e1Smestre static struct passwd *find_user_info(const char *);
206e2fea6e1Smestre static void checkeuid(void);
207580bd31cSmillert static void strsave(char **, const char *);
208e2fea6e1Smestre static void read_defaults(user_t *);
2093c76c7c0Smillert 
2103c76c7c0Smillert static int	verbose;
211e02380dbSjakob 
212580bd31cSmillert /* free *cpp, then store a copy of `s' in it */
213e02380dbSjakob static void
strsave(char ** cpp,const char * s)214580bd31cSmillert strsave(char **cpp, const char *s)
215e02380dbSjakob {
2168be1120aSmestre 	free(*cpp);
217580bd31cSmillert 	if ((*cpp = strdup(s)) == NULL)
218efe89df2Smestre 		err(1, NULL);
219e02380dbSjakob }
220e02380dbSjakob 
221*8e836504Smillert /* run the given command with optional arguments as the specified uid */
222e02380dbSjakob static int
runas(const char * path,const char * const argv[],uid_t uid,int flags)223*8e836504Smillert runas(const char *path, const char *const argv[], uid_t uid, int flags)
224e02380dbSjakob {
225*8e836504Smillert 	int argc, status, ret = 0;
226e02380dbSjakob 	char buf[MaxCommandLen];
227*8e836504Smillert 	pid_t child;
228e02380dbSjakob 
229*8e836504Smillert 	strlcpy(buf, path, sizeof(buf));
230*8e836504Smillert 	for (argc = 1; argv[argc] != NULL; argc++) {
231*8e836504Smillert 		strlcat(buf, " ", sizeof(buf));
232*8e836504Smillert 		strlcat(buf, argv[argc], sizeof(buf));
233e02380dbSjakob 	}
234*8e836504Smillert 	if (verbose)
235*8e836504Smillert 		printf("Command: %s\n", buf);
236*8e836504Smillert 
237*8e836504Smillert 	child = fork();
238*8e836504Smillert 	switch (child) {
239*8e836504Smillert 	case -1:
240*8e836504Smillert 		err(EXIT_FAILURE, "fork");
241*8e836504Smillert 	case 0:
242*8e836504Smillert 		if (flags & RUNAS_DUP_DEVNULL) {
243*8e836504Smillert 			/* Redirect output to /dev/null if possible. */
244*8e836504Smillert 			int dev_null = open(_PATH_DEVNULL, O_RDWR);
245*8e836504Smillert 			if (dev_null != -1) {
246*8e836504Smillert 				dup2(dev_null, STDOUT_FILENO);
247*8e836504Smillert 				dup2(dev_null, STDERR_FILENO);
248*8e836504Smillert 				if (dev_null > STDERR_FILENO)
249*8e836504Smillert 					close(dev_null);
250*8e836504Smillert 			} else {
251*8e836504Smillert 				warn("%s", _PATH_DEVNULL);
252*8e836504Smillert 			}
253*8e836504Smillert 		}
254*8e836504Smillert 		if (uid != -1) {
255*8e836504Smillert 			if (setresuid(uid, uid, uid) == -1)
256*8e836504Smillert 				warn("setresuid(%u, %u, %u)", uid, uid, uid);
257*8e836504Smillert 		}
258*8e836504Smillert 		execv(path, (char **)argv);
259*8e836504Smillert 		warn("%s", buf);
260*8e836504Smillert 		_exit(EXIT_FAILURE);
261*8e836504Smillert 	default:
262*8e836504Smillert 		while (waitpid(child, &status, 0) == -1) {
263*8e836504Smillert 			if (errno != EINTR)
264*8e836504Smillert 				err(EXIT_FAILURE, "waitpid");
265*8e836504Smillert 		}
266*8e836504Smillert 		if (WIFSIGNALED(status)) {
267*8e836504Smillert 			ret = WTERMSIG(status);
268*8e836504Smillert 			warnx("[Warning] `%s' killed by signal %d", buf, ret);
269*8e836504Smillert 			ret |= 128;
270*8e836504Smillert 		} else {
271*8e836504Smillert 			if (!(flags & RUNAS_IGNORE_EXITVAL))
272*8e836504Smillert 				ret = WEXITSTATUS(status);
273*8e836504Smillert 			if (ret != 0) {
274*8e836504Smillert 				warnx("[Warning] `%s' failed with status %d",
275*8e836504Smillert 				    buf, ret);
276*8e836504Smillert 			}
277e02380dbSjakob 		}
278e02380dbSjakob 		return ret;
279e02380dbSjakob 	}
280*8e836504Smillert }
281*8e836504Smillert 
282*8e836504Smillert /* run the given command with optional arguments */
283*8e836504Smillert static int
run(const char * path,const char * const argv[])284*8e836504Smillert run(const char *path, const char *const argv[])
285*8e836504Smillert {
286*8e836504Smillert 	return runas(path, argv, -1, 0);
287*8e836504Smillert }
288e02380dbSjakob 
2893c76c7c0Smillert /* remove a users home directory, returning 1 for success (ie, no problems encountered) */
2903c76c7c0Smillert static int
removehomedir(const char * user,uid_t uid,const char * dir)2915b0f5f19Smillert removehomedir(const char *user, uid_t uid, const char *dir)
2923c76c7c0Smillert {
293*8e836504Smillert 	const char *rm_argv[] = { "rm", "-rf", dir, NULL };
2943c76c7c0Smillert 	struct stat st;
2953c76c7c0Smillert 
2963c76c7c0Smillert 	/* userid not root? */
2973c76c7c0Smillert 	if (uid == 0) {
2983c76c7c0Smillert 		warnx("Not deleting home directory `%s'; userid is 0", dir);
2993c76c7c0Smillert 		return 0;
3003c76c7c0Smillert 	}
3013c76c7c0Smillert 
3023c76c7c0Smillert 	/* directory exists (and is a directory!) */
303df69c215Sderaadt 	if (stat(dir, &st) == -1) {
3043c76c7c0Smillert 		warnx("Home directory `%s' doesn't exist", dir);
3053c76c7c0Smillert 		return 0;
3063c76c7c0Smillert 	}
3073c76c7c0Smillert 	if (!S_ISDIR(st.st_mode)) {
3083c76c7c0Smillert 		warnx("Home directory `%s' is not a directory", dir);
3093c76c7c0Smillert 		return 0;
3103c76c7c0Smillert 	}
3113c76c7c0Smillert 
3123c76c7c0Smillert 	/* userid matches directory owner? */
3133c76c7c0Smillert 	if (st.st_uid != uid) {
31446347781Smpech 		warnx("User `%s' doesn't own directory `%s', not removed",
3153c76c7c0Smillert 		    user, dir);
3163c76c7c0Smillert 		return 0;
3173c76c7c0Smillert 	}
3183c76c7c0Smillert 
319*8e836504Smillert 	/* run "rm -rf dir 2>&1/dev/null" as user, not root */
320*8e836504Smillert 	(void) runas(RM, rm_argv, uid, RUNAS_DUP_DEVNULL|RUNAS_IGNORE_EXITVAL);
321*8e836504Smillert 	if (rmdir(dir) == -1 && errno != ENOENT) {
32246347781Smpech 		warnx("Unable to remove all files in `%s'", dir);
3233c76c7c0Smillert 		return 0;
3243c76c7c0Smillert 	}
3253c76c7c0Smillert 	return 1;
3263c76c7c0Smillert }
327844c8300Sjakob 
328e02380dbSjakob /*
329e02380dbSjakob  * check that the effective uid is 0 - called from funcs which will
330e02380dbSjakob  * modify data and config files.
331e02380dbSjakob  */
332e02380dbSjakob static void
checkeuid(void)333e02380dbSjakob checkeuid(void)
334e02380dbSjakob {
335e02380dbSjakob 	if (geteuid() != 0) {
336e02380dbSjakob 		errx(EXIT_FAILURE, "Program must be run as root");
337e02380dbSjakob 	}
338e02380dbSjakob }
339e02380dbSjakob 
340e02380dbSjakob /* copy any dot files into the user's home directory */
341e02380dbSjakob static int
copydotfiles(char * skeldir,char * dst)342*8e836504Smillert copydotfiles(char *skeldir, char *dst)
343e02380dbSjakob {
344*8e836504Smillert 	char		src[MaxFileNameLen];
345e02380dbSjakob 	struct dirent	*dp;
346e02380dbSjakob 	DIR		*dirp;
347*8e836504Smillert 	int		len, n;
348e02380dbSjakob 
3490508ab9cSmillert 	if (*skeldir == '\0')
3500508ab9cSmillert 		return 0;
3513c76c7c0Smillert 	if ((dirp = opendir(skeldir)) == NULL) {
352e02380dbSjakob 		warn("can't open source . files dir `%s'", skeldir);
353e02380dbSjakob 		return 0;
354e02380dbSjakob 	}
3553c76c7c0Smillert 	for (n = 0; (dp = readdir(dirp)) != NULL && n == 0 ; ) {
356e02380dbSjakob 		if (strcmp(dp->d_name, ".") == 0 ||
357e02380dbSjakob 		    strcmp(dp->d_name, "..") == 0) {
358e02380dbSjakob 			continue;
359e02380dbSjakob 		}
360e02380dbSjakob 		n = 1;
361e02380dbSjakob 	}
362e02380dbSjakob 	(void) closedir(dirp);
363e02380dbSjakob 	if (n == 0) {
364e02380dbSjakob 		warnx("No \"dot\" initialisation files found");
365e02380dbSjakob 	} else {
366*8e836504Smillert 		len = snprintf(src, sizeof(src), "%s/.", skeldir);
367*8e836504Smillert 		if (len < 0 || len >= sizeof(src)) {
368*8e836504Smillert 			warnx("skeleton directory `%s' too long", skeldir);
369*8e836504Smillert 			n = 0;
370*8e836504Smillert 		} else {
371*8e836504Smillert 			const char *cp_argv[] = { "cp", "-a", src, dst, NULL };
372*8e836504Smillert 			if (verbose)
373*8e836504Smillert 				cp_argv[1] = "-av";
374*8e836504Smillert 			run(CP, cp_argv);
375*8e836504Smillert 		}
376e02380dbSjakob 	}
377e02380dbSjakob 	return n;
378e02380dbSjakob }
379e02380dbSjakob 
38065b77256Smillert /* returns 1 if the specified gid exists in the group file, else 0 */
38165b77256Smillert static int
gid_exists(gid_t gid)38265b77256Smillert gid_exists(gid_t gid)
38365b77256Smillert {
38465b77256Smillert     return group_from_gid(gid, 1) != NULL;
38565b77256Smillert }
38665b77256Smillert 
38765b77256Smillert /* return 1 if the specified group exists in the group file, else 0 */
38865b77256Smillert static int
group_exists(const char * group)38965b77256Smillert group_exists(const char *group)
39065b77256Smillert {
39165b77256Smillert     gid_t gid;
39265b77256Smillert 
39365b77256Smillert     return gid_from_group(group, &gid) != -1;
39465b77256Smillert }
39565b77256Smillert 
396e02380dbSjakob /* create a group entry with gid `gid' */
397e02380dbSjakob static int
creategid(char * group,gid_t gid,const char * name)398be1ff426Smillert creategid(char *group, gid_t gid, const char *name)
399e02380dbSjakob {
400e02380dbSjakob 	struct stat	st;
401e02380dbSjakob 	FILE		*from;
402e02380dbSjakob 	FILE		*to;
403ac109e4dSderaadt 	char		*buf;
404e02380dbSjakob 	char		f[MaxFileNameLen];
405ac109e4dSderaadt 	int		fd, ret;
406ac109e4dSderaadt 	int		wroteit = 0;
407ac109e4dSderaadt 	size_t		len;
408e02380dbSjakob 
40965b77256Smillert 	if (group_exists(group)) {
410e02380dbSjakob 		warnx("group `%s' already exists", group);
411e02380dbSjakob 		return 0;
412e02380dbSjakob 	}
4133c76c7c0Smillert 	if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
414ddcf21ceSmillert 		warn("can't create gid for `%s': can't open `%s'", group,
4153c76c7c0Smillert 		    _PATH_GROUP);
416e02380dbSjakob 		return 0;
417e02380dbSjakob 	}
418df69c215Sderaadt 	if (flock(fileno(from), LOCK_EX | LOCK_NB) == -1) {
419ca33ea46Sjakob 		warn("can't lock `%s'", _PATH_GROUP);
420e02380dbSjakob 	}
421e02380dbSjakob 	(void) fstat(fileno(from), &st);
4223c76c7c0Smillert 	(void) snprintf(f, sizeof(f), "%s.XXXXXXXX", _PATH_GROUP);
423c2d43ecaSderaadt 	if ((fd = mkstemp(f)) == -1) {
424e02380dbSjakob 		warn("can't create gid: mkstemp failed");
4255520f1ccSderaadt 		fclose(from);
426e02380dbSjakob 		return 0;
427e02380dbSjakob 	}
4283c76c7c0Smillert 	if ((to = fdopen(fd, "w")) == NULL) {
429ffb4dd05Sguenther 		warn("can't create gid: fdopen `%s' failed", f);
4305520f1ccSderaadt 		fclose(from);
4315520f1ccSderaadt 		close(fd);
4325520f1ccSderaadt 		unlink(f);
433e02380dbSjakob 		return 0;
434e02380dbSjakob 	}
435ac109e4dSderaadt 	while ((buf = fgetln(from, &len)) != NULL && len > 0) {
436ac109e4dSderaadt 		ret = 0;
437ac109e4dSderaadt 		if (buf[0] == '+' && wroteit == 0) {
438ac109e4dSderaadt 			ret = fprintf(to, "%s:*:%u:%s\n", group, gid, name);
439ac109e4dSderaadt 			wroteit = 1;
440ac109e4dSderaadt 		}
441ac109e4dSderaadt 		if (ret == -1 ||
442ac109e4dSderaadt 		    fprintf(to, "%*.*s", (int)len, (int)len, buf) != len) {
443ffb4dd05Sguenther 			warn("can't create gid: short write to `%s'", f);
4445520f1ccSderaadt 			fclose(from);
4455520f1ccSderaadt 			fclose(to);
4465520f1ccSderaadt 			unlink(f);
447e02380dbSjakob 			return 0;
448e02380dbSjakob 		}
449e02380dbSjakob 	}
450ac109e4dSderaadt 	ret = 0;
451ac109e4dSderaadt 	if (wroteit == 0)
452ac109e4dSderaadt 		ret = fprintf(to, "%s:*:%u:%s\n", group, gid, name);
4535520f1ccSderaadt 	fclose(from);
454ac109e4dSderaadt 	if (fclose(to) == EOF || ret == -1) {
455ac109e4dSderaadt 		warn("can't create gid: short write to `%s'", f);
4565520f1ccSderaadt 		unlink(f);
457ac109e4dSderaadt 		return 0;
458ac109e4dSderaadt 	}
459df69c215Sderaadt 	if (rename(f, _PATH_GROUP) == -1) {
4603c76c7c0Smillert 		warn("can't create gid: can't rename `%s' to `%s'", f,
4613c76c7c0Smillert 		    _PATH_GROUP);
4625520f1ccSderaadt 		unlink(f);
463e02380dbSjakob 		return 0;
464e02380dbSjakob 	}
465beaa942cSderaadt 	(void) chmod(_PATH_GROUP, st.st_mode & 0777);
46601214246Smillert 	syslog(LOG_INFO, "new group added: name=%s, gid=%u", group, gid);
467e02380dbSjakob 	return 1;
468e02380dbSjakob }
469e02380dbSjakob 
470e02380dbSjakob /* modify the group entry with name `group' to be newent */
471e02380dbSjakob static int
modify_gid(char * group,char * newent)472e02380dbSjakob modify_gid(char *group, char *newent)
473e02380dbSjakob {
474e02380dbSjakob 	struct stat	st;
475e02380dbSjakob 	FILE		*from;
476e02380dbSjakob 	FILE		*to;
4773c76c7c0Smillert 	char		buf[LINE_MAX];
478e02380dbSjakob 	char		f[MaxFileNameLen];
479e02380dbSjakob 	char		*colon;
480e02380dbSjakob 	int		groupc;
481e02380dbSjakob 	int		entc;
482e02380dbSjakob 	int		fd;
483e02380dbSjakob 	int		cc;
484e02380dbSjakob 
4853c76c7c0Smillert 	if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
4866202ca67Smillert 		warn("can't modify gid for `%s': can't open `%s'", group,
487571ba9d6Smillert 		    _PATH_GROUP);
488e02380dbSjakob 		return 0;
489e02380dbSjakob 	}
490df69c215Sderaadt 	if (flock(fileno(from), LOCK_EX | LOCK_NB) == -1) {
491ca33ea46Sjakob 		warn("can't lock `%s'", _PATH_GROUP);
492e02380dbSjakob 	}
493e02380dbSjakob 	(void) fstat(fileno(from), &st);
4943c76c7c0Smillert 	(void) snprintf(f, sizeof(f), "%s.XXXXXXXX", _PATH_GROUP);
495c2d43ecaSderaadt 	if ((fd = mkstemp(f)) == -1) {
4966202ca67Smillert 		warn("can't modify gid: mkstemp failed");
4975520f1ccSderaadt 		fclose(from);
498e02380dbSjakob 		return 0;
499e02380dbSjakob 	}
5003c76c7c0Smillert 	if ((to = fdopen(fd, "w")) == NULL) {
501ffb4dd05Sguenther 		warn("can't modify gid: fdopen `%s' failed", f);
5025520f1ccSderaadt 		fclose(from);
5035520f1ccSderaadt 		close(fd);
5045520f1ccSderaadt 		unlink(f);
505e02380dbSjakob 		return 0;
506e02380dbSjakob 	}
507e02380dbSjakob 	groupc = strlen(group);
508e02380dbSjakob 	while (fgets(buf, sizeof(buf), from) != NULL) {
509e02380dbSjakob 		cc = strlen(buf);
51072c1cea9Schl 		if (cc > 0 && buf[cc - 1] != '\n' && !feof(from)) {
5116202ca67Smillert 			while (fgetc(from) != '\n' && !feof(from))
5123c76c7c0Smillert 				cc++;
513bc51f5a6Smiod 			warnx("%s: line `%s' too long (%d bytes), skipping",
5146202ca67Smillert 			    _PATH_GROUP, buf, cc);
5153c76c7c0Smillert 			continue;
5163c76c7c0Smillert 		}
517e02380dbSjakob 		if ((colon = strchr(buf, ':')) == NULL) {
518bc51f5a6Smiod 			/*
519bc51f5a6Smiod 			 * The only valid entry with no column is the all-YP
520bc51f5a6Smiod 			 * line.
521bc51f5a6Smiod 			 */
522bc51f5a6Smiod 			if (strcmp(buf, "+\n") != 0) {
523bc51f5a6Smiod 				warnx("badly formed entry `%.*s'", cc - 1, buf);
524e02380dbSjakob 				continue;
525e02380dbSjakob 			}
526bc51f5a6Smiod 		} else {
527e02380dbSjakob 			entc = (int)(colon - buf);
5283c76c7c0Smillert 			if (entc == groupc && strncmp(group, buf, entc) == 0) {
529e02380dbSjakob 				if (newent == NULL) {
530e02380dbSjakob 					continue;
531e02380dbSjakob 				} else {
5325f05abf7Sray 					cc = strlcpy(buf, newent, sizeof(buf));
5335f05abf7Sray 					if (cc >= sizeof(buf)) {
534bc51f5a6Smiod 						warnx("group `%s' entry too long",
535bc51f5a6Smiod 						    newent);
53663f4fac5Smestre 						fclose(from);
53763f4fac5Smestre 						fclose(to);
53863f4fac5Smestre 						unlink(f);
5395f05abf7Sray 						return (0);
5405f05abf7Sray 					}
541e02380dbSjakob 				}
542e02380dbSjakob 			}
543bc51f5a6Smiod 		}
5446202ca67Smillert 		if (fwrite(buf, cc, 1, to) != 1) {
545ffb4dd05Sguenther 			warn("can't modify gid: short write to `%s'", f);
5465520f1ccSderaadt 			fclose(from);
5475520f1ccSderaadt 			fclose(to);
5485520f1ccSderaadt 			unlink(f);
549e02380dbSjakob 			return 0;
550e02380dbSjakob 		}
551e02380dbSjakob 	}
5525520f1ccSderaadt 	fclose(from);
553bd28cd76Sotto 	if (fclose(to) == EOF) {
554bd28cd76Sotto 		warn("can't modify gid: short write to `%s'", f);
5555520f1ccSderaadt 		unlink(f);
556bd28cd76Sotto 		return 0;
557bd28cd76Sotto 	}
558df69c215Sderaadt 	if (rename(f, _PATH_GROUP) == -1) {
5596202ca67Smillert 		warn("can't modify gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
5605520f1ccSderaadt 		unlink(f);
561e02380dbSjakob 		return 0;
562e02380dbSjakob 	}
563beaa942cSderaadt 	(void) chmod(_PATH_GROUP, st.st_mode & 0777);
564f65c14a9Smillert 	if (newent == NULL) {
565f65c14a9Smillert 		syslog(LOG_INFO, "group deleted: name=%s", group);
566f65c14a9Smillert 	} else {
567f65c14a9Smillert 		syslog(LOG_INFO, "group information modified: name=%s", group);
568f65c14a9Smillert 	}
569e02380dbSjakob 	return 1;
570e02380dbSjakob }
571e02380dbSjakob 
5723c76c7c0Smillert /* modify the group entries for all `groups', by adding `user' */
573d50f5675Sho static int
append_group(char * user,int ngroups,const char ** groups)574be1ff426Smillert append_group(char *user, int ngroups, const char **groups)
575d50f5675Sho {
5763c76c7c0Smillert 	struct group	*grp;
5770f7f76edSajacoutot 	struct passwd	*pwp;
578d50f5675Sho 	struct stat	st;
5793c76c7c0Smillert 	FILE		*from;
5803c76c7c0Smillert 	FILE		*to;
5813c76c7c0Smillert 	char		buf[LINE_MAX];
5823c76c7c0Smillert 	char		f[MaxFileNameLen];
5833c76c7c0Smillert 	char		*colon;
584cdc5a29bSmillert 	const char	*ugid = NULL;
5853c76c7c0Smillert 	int		fd;
5863c76c7c0Smillert 	int		cc;
5873c76c7c0Smillert 	int		i;
5883c76c7c0Smillert 	int		j;
589d50f5675Sho 
5900f7f76edSajacoutot 	if ((pwp = getpwnam(user))) {
5910f7f76edSajacoutot 		if ((ugid = group_from_gid(pwp->pw_gid, 1)) == NULL) {
5920f7f76edSajacoutot 			warnx("can't get primary group for user `%s'", user);
5930f7f76edSajacoutot 			return 0;
5940f7f76edSajacoutot 		}
5950f7f76edSajacoutot 	}
5960f7f76edSajacoutot 
5973c76c7c0Smillert 	for (i = 0 ; i < ngroups ; i++) {
5983c76c7c0Smillert 		if ((grp = getgrnam(groups[i])) == NULL) {
5993c76c7c0Smillert 			warnx("can't append group `%s' for user `%s'",
6003c76c7c0Smillert 			    groups[i], user);
6013c76c7c0Smillert 		} else {
6023c76c7c0Smillert 			for (j = 0 ; grp->gr_mem[j] ; j++) {
6033c76c7c0Smillert 				if (strcmp(user, grp->gr_mem[j]) == 0) {
6043c76c7c0Smillert 					/* already in it */
6053c76c7c0Smillert 					groups[i] = "";
6063c76c7c0Smillert 				}
6073c76c7c0Smillert 			}
6083c76c7c0Smillert 		}
6093c76c7c0Smillert 	}
6103c76c7c0Smillert 	if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
611ddcf21ceSmillert 		warn("can't append group for `%s': can't open `%s'", user,
6123c76c7c0Smillert 		    _PATH_GROUP);
613d50f5675Sho 		return 0;
614d50f5675Sho 	}
615df69c215Sderaadt 	if (flock(fileno(from), LOCK_EX | LOCK_NB) == -1) {
616d50f5675Sho 		warn("can't lock `%s'", _PATH_GROUP);
617d50f5675Sho 	}
618d50f5675Sho 	(void) fstat(fileno(from), &st);
6193c76c7c0Smillert 	(void) snprintf(f, sizeof(f), "%s.XXXXXXXX", _PATH_GROUP);
620c2d43ecaSderaadt 	if ((fd = mkstemp(f)) == -1) {
6216202ca67Smillert 		warn("can't append group: mkstemp failed");
6225520f1ccSderaadt 		fclose(from);
623d50f5675Sho 		return 0;
624d50f5675Sho 	}
6253c76c7c0Smillert 	if ((to = fdopen(fd, "w")) == NULL) {
626ffb4dd05Sguenther 		warn("can't append group: fdopen `%s' failed", f);
6275520f1ccSderaadt 		fclose(from);
6285520f1ccSderaadt 		close(fd);
6295520f1ccSderaadt 		unlink(f);
630d50f5675Sho 		return 0;
631d50f5675Sho 	}
632d50f5675Sho 	while (fgets(buf, sizeof(buf), from) != NULL) {
6333c76c7c0Smillert 		cc = strlen(buf);
63472c1cea9Schl 		if (cc > 0 && buf[cc - 1] != '\n' && !feof(from)) {
6356202ca67Smillert 			while (fgetc(from) != '\n' && !feof(from))
6363c76c7c0Smillert 				cc++;
637bc51f5a6Smiod 			warnx("%s: line `%s' too long (%d bytes), skipping",
6386202ca67Smillert 			    _PATH_GROUP, buf, cc);
6393c76c7c0Smillert 			continue;
6403c76c7c0Smillert 		}
641d50f5675Sho 		if ((colon = strchr(buf, ':')) == NULL) {
642571ba9d6Smillert 			warnx("badly formed entry `%s'", buf);
643d50f5675Sho 			continue;
644d50f5675Sho 		}
6453c76c7c0Smillert 		for (i = 0 ; i < ngroups ; i++) {
6463b85cd77Smillert 			j = (int)(colon - buf);
6470f7f76edSajacoutot 			if (ugid) {
6480f7f76edSajacoutot 				if (strcmp(ugid, groups[i]) == 0) {
6490f7f76edSajacoutot 					/* user's primary group, no need to append */
6500f7f76edSajacoutot 					groups[i] = "";
6510f7f76edSajacoutot 				}
6520f7f76edSajacoutot 			}
6533b85cd77Smillert 			if (strncmp(groups[i], buf, j) == 0 &&
6543b85cd77Smillert 			    groups[i][j] == '\0') {
65530468d50Sderaadt 				while (isspace((unsigned char)buf[cc - 1]))
6561715a73eSmillert 					cc--;
6571715a73eSmillert 				buf[(j = cc)] = '\0';
65888d9b758Smillert 				if (buf[strlen(buf) - 1] != ':')
6591715a73eSmillert 					strlcat(buf, ",", sizeof(buf));
6601715a73eSmillert 				cc = strlcat(buf, user, sizeof(buf)) + 1;
6613c76c7c0Smillert 				if (cc >= sizeof(buf)) {
6621715a73eSmillert 					warnx("Warning: group `%s' would "
6631715a73eSmillert 					    "become too long, not modifying",
6641715a73eSmillert 					    groups[i]);
6651715a73eSmillert 					cc = j + 1;
6663c76c7c0Smillert 				}
6671715a73eSmillert 				buf[cc - 1] = '\n';
6681715a73eSmillert 				buf[cc] = '\0';
669d50f5675Sho 			}
670d50f5675Sho 		}
6716202ca67Smillert 		if (fwrite(buf, cc, 1, to) != 1) {
672ffb4dd05Sguenther 			warn("can't append group: short write to `%s'", f);
6735520f1ccSderaadt 			fclose(from);
6745520f1ccSderaadt 			fclose(to);
6755520f1ccSderaadt 			unlink(f);
676d50f5675Sho 			return 0;
677d50f5675Sho 		}
678d50f5675Sho 	}
6795520f1ccSderaadt 	fclose(from);
680bd28cd76Sotto 	if (fclose(to) == EOF) {
681bd28cd76Sotto 		warn("can't append group: short write to `%s'", f);
6825520f1ccSderaadt 		unlink(f);
683bd28cd76Sotto 		return 0;
684bd28cd76Sotto 	}
685df69c215Sderaadt 	if (rename(f, _PATH_GROUP) == -1) {
6866202ca67Smillert 		warn("can't append group: can't rename `%s' to `%s'", f, _PATH_GROUP);
6875520f1ccSderaadt 		unlink(f);
688d50f5675Sho 		return 0;
689d50f5675Sho 	}
690beaa942cSderaadt 	(void) chmod(_PATH_GROUP, st.st_mode & 0777);
691d50f5675Sho 	return 1;
692d50f5675Sho }
693d50f5675Sho 
694e02380dbSjakob /* return 1 if `login' is a valid login name */
695e02380dbSjakob static int
valid_login(char * login_name)696be1ff426Smillert valid_login(char *login_name)
697e02380dbSjakob {
698af911b7cSmillert 	unsigned char	*cp;
699e02380dbSjakob 
700e0c47750Smillert 	/* The first character cannot be a hyphen */
701be1ff426Smillert 	if (*login_name == '-')
702e0c47750Smillert 		return 0;
703e0c47750Smillert 
704be1ff426Smillert 	for (cp = login_name ; *cp ; cp++) {
705acdeee01Smillert 		/* We allow '$' as the last character for samba */
70630468d50Sderaadt 		if (!isalnum((unsigned char)*cp) && *cp != '.' &&
70730468d50Sderaadt 		    *cp != '_' && *cp != '-' &&
708acdeee01Smillert 		    !(*cp == '$' && *(cp + 1) == '\0')) {
709e02380dbSjakob 			return 0;
710e02380dbSjakob 		}
711e02380dbSjakob 	}
712af911b7cSmillert 	if ((char *)cp - login_name > MaxUserNameLen)
71356b04c1eSmillert 		return 0;
714e02380dbSjakob 	return 1;
715e02380dbSjakob }
716e02380dbSjakob 
717e02380dbSjakob /* return 1 if `group' is a valid group name */
718e02380dbSjakob static int
valid_group(char * group)719e02380dbSjakob valid_group(char *group)
720e02380dbSjakob {
721af911b7cSmillert 	unsigned char	*cp;
722e02380dbSjakob 
723e02380dbSjakob 	for (cp = group ; *cp ; cp++) {
72430468d50Sderaadt 		if (!isalnum((unsigned char)*cp) && *cp != '.' &&
72530468d50Sderaadt 		    *cp != '_' && *cp != '-') {
726e02380dbSjakob 			return 0;
727e02380dbSjakob 		}
728e02380dbSjakob 	}
729af911b7cSmillert 	if ((char *)cp - group > MaxUserNameLen)
730ce08dfceSmillert 		return 0;
731e02380dbSjakob 	return 1;
732e02380dbSjakob }
733e02380dbSjakob 
7343162cc8dSmillert /* return 1 if `class' exists */
7353162cc8dSmillert static int
valid_class(char * class)7363162cc8dSmillert valid_class(char *class)
7373162cc8dSmillert {
7383162cc8dSmillert 	login_cap_t *lc;
7393162cc8dSmillert 
7403162cc8dSmillert 	if ((lc = login_getclass(class)) != NULL)
7413162cc8dSmillert 		login_close(lc);
7423162cc8dSmillert 	return lc != NULL;
7433162cc8dSmillert }
7443162cc8dSmillert 
745e02380dbSjakob /* find the next gid in the range lo .. hi */
746e02380dbSjakob static int
getnextgid(uid_t * gidp,uid_t lo,uid_t hi)7475b0f5f19Smillert getnextgid(uid_t *gidp, uid_t lo, uid_t hi)
748e02380dbSjakob {
749e02380dbSjakob 	for (*gidp = lo ; *gidp < hi ; *gidp += 1) {
75065b77256Smillert 		if (!gid_exists((gid_t)*gidp)) {
751e02380dbSjakob 			return 1;
752e02380dbSjakob 		}
753e02380dbSjakob 	}
754e02380dbSjakob 	return 0;
755e02380dbSjakob }
756e02380dbSjakob 
757e02380dbSjakob /* save a range of uids */
758e02380dbSjakob static int
save_range(user_t * up,char * cp)759e02380dbSjakob save_range(user_t *up, char *cp)
760e02380dbSjakob {
76101214246Smillert 	uid_t	from;
76201214246Smillert 	uid_t	to;
763e02380dbSjakob 	int	i;
764e02380dbSjakob 
765efe89df2Smestre 	if (up->u_rc == up->u_rsize) {
766e02380dbSjakob 		up->u_rsize *= 2;
767efe89df2Smestre 		if ((up->u_rv = reallocarray(up->u_rv, up->u_rsize,
768efe89df2Smestre 		    sizeof(range_t))) == NULL) {
769b8692dddSmestre 			warn(NULL);
770efe89df2Smestre 			return 0;
771efe89df2Smestre 		}
772e02380dbSjakob 	}
77301214246Smillert 	if (up->u_rv && sscanf(cp, "%u..%u", &from, &to) == 2) {
7745252bc90Sjakob 		for (i = up->u_defrc ; i < up->u_rc ; i++) {
775e02380dbSjakob 			if (up->u_rv[i].r_from == from && up->u_rv[i].r_to == to) {
776e02380dbSjakob 				break;
777e02380dbSjakob 			}
778e02380dbSjakob 		}
779e02380dbSjakob 		if (i == up->u_rc) {
780e02380dbSjakob 			up->u_rv[up->u_rc].r_from = from;
781e02380dbSjakob 			up->u_rv[up->u_rc].r_to = to;
782e02380dbSjakob 			up->u_rc += 1;
783e02380dbSjakob 		}
784e02380dbSjakob 	} else {
785446b5bdaSmillert 		warnx("Bad uid range `%s'", cp);
786e02380dbSjakob 		return 0;
787e02380dbSjakob 	}
788e02380dbSjakob 	return 1;
789e02380dbSjakob }
790e02380dbSjakob 
791e02380dbSjakob /* set the defaults in the defaults file */
792e02380dbSjakob static int
setdefaults(user_t * up)793e02380dbSjakob setdefaults(user_t *up)
794e02380dbSjakob {
795e02380dbSjakob 	char	template[MaxFileNameLen];
796e02380dbSjakob 	FILE	*fp;
797e02380dbSjakob 	int	ret;
798e02380dbSjakob 	int	fd;
799e02380dbSjakob 	int	i;
800e02380dbSjakob 
8013c76c7c0Smillert 	(void) snprintf(template, sizeof(template), "%s.XXXXXXXX", CONFFILE);
802c2d43ecaSderaadt 	if ((fd = mkstemp(template)) == -1) {
803e02380dbSjakob 		warnx("can't mkstemp `%s' for writing", CONFFILE);
804e02380dbSjakob 		return 0;
805e02380dbSjakob 	}
8063c76c7c0Smillert 	if ((fp = fdopen(fd, "w")) == NULL) {
807e02380dbSjakob 		warn("can't fdopen `%s' for writing", CONFFILE);
808e02380dbSjakob 		return 0;
809e02380dbSjakob 	}
810e02380dbSjakob 	ret = 1;
811e02380dbSjakob 	if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 ||
812e02380dbSjakob 	    fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 ||
813e02380dbSjakob 	    fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 ||
814e02380dbSjakob 	    fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 ||
8153c76c7c0Smillert 	    fprintf(fp, "class\t\t%s\n", up->u_class) <= 0 ||
816ddcf21ceSmillert 	    fprintf(fp, "inactive\t%s\n", (up->u_inactive == NULL) ? UNSET_INACTIVE : up->u_inactive) <= 0 ||
8173c76c7c0Smillert 	    fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ? UNSET_EXPIRY : up->u_expire) <= 0 ||
8183c76c7c0Smillert 	    fprintf(fp, "preserve\t%s\n", (up->u_preserve == 0) ? "false" : "true") <= 0) {
819e02380dbSjakob 		warn("can't write to `%s'", CONFFILE);
820e02380dbSjakob 		ret = 0;
821e02380dbSjakob 	}
822e02380dbSjakob 	for (i = (up->u_defrc != up->u_rc) ? up->u_defrc : 0 ; i < up->u_rc ; i++) {
82301214246Smillert 		if (fprintf(fp, "range\t\t%u..%u\n", up->u_rv[i].r_from, up->u_rv[i].r_to) <= 0) {
824e02380dbSjakob 			warn("can't write to `%s'", CONFFILE);
825e02380dbSjakob 			ret = 0;
826e02380dbSjakob 		}
827e02380dbSjakob 	}
828bd28cd76Sotto 	if (fclose(fp) == EOF) {
829bd28cd76Sotto 		warn("can't write to `%s'", CONFFILE);
830bd28cd76Sotto 		ret = 0;
831bd28cd76Sotto 	}
832e02380dbSjakob 	if (ret) {
833e02380dbSjakob 		ret = ((rename(template, CONFFILE) == 0) && (chmod(CONFFILE, 0644) == 0));
834e02380dbSjakob 	}
835e02380dbSjakob 	return ret;
836e02380dbSjakob }
837e02380dbSjakob 
838e02380dbSjakob /* read the defaults file */
839e02380dbSjakob static void
read_defaults(user_t * up)840e02380dbSjakob read_defaults(user_t *up)
841e02380dbSjakob {
842e02380dbSjakob 	struct stat	st;
843e02380dbSjakob 	size_t		lineno;
844e02380dbSjakob 	size_t		len;
845e02380dbSjakob 	FILE		*fp;
846af911b7cSmillert 	unsigned char	*cp;
847af911b7cSmillert 	unsigned char	*s;
848e02380dbSjakob 
849580bd31cSmillert 	strsave(&up->u_primgrp, DEF_GROUP);
850580bd31cSmillert 	strsave(&up->u_basedir, DEF_BASEDIR);
851580bd31cSmillert 	strsave(&up->u_skeldir, DEF_SKELDIR);
852580bd31cSmillert 	strsave(&up->u_shell, DEF_SHELL);
853580bd31cSmillert 	strsave(&up->u_comment, DEF_COMMENT);
854580bd31cSmillert 	strsave(&up->u_class, DEF_CLASS);
855e02380dbSjakob 	up->u_rsize = 16;
8565252bc90Sjakob 	up->u_defrc = 0;
857efe89df2Smestre 	if ((up->u_rv = calloc(up->u_rsize, sizeof(range_t))) == NULL)
858efe89df2Smestre 		err(1, NULL);
859e02380dbSjakob 	up->u_inactive = DEF_INACTIVE;
860e02380dbSjakob 	up->u_expire = DEF_EXPIRE;
8613c76c7c0Smillert 	if ((fp = fopen(CONFFILE, "r")) == NULL) {
862df69c215Sderaadt 		if (stat(CONFFILE, &st) == -1 && !setdefaults(up)) {
863e02380dbSjakob 			warn("can't create `%s' defaults file", CONFFILE);
864e02380dbSjakob 		}
865e02380dbSjakob 		fp = fopen(CONFFILE, "r");
866e02380dbSjakob 	}
8673c76c7c0Smillert 	if (fp != NULL) {
868e02380dbSjakob 		while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) {
869e02380dbSjakob 			if (strncmp(s, "group", 5) == 0) {
87030468d50Sderaadt 				for (cp = s + 5 ; isspace((unsigned char)*cp); cp++) {
871e02380dbSjakob 				}
872580bd31cSmillert 				strsave(&up->u_primgrp, cp);
873e02380dbSjakob 			} else if (strncmp(s, "base_dir", 8) == 0) {
87430468d50Sderaadt 				for (cp = s + 8 ; isspace((unsigned char)*cp); cp++) {
875e02380dbSjakob 				}
876580bd31cSmillert 				strsave(&up->u_basedir, cp);
877e02380dbSjakob 			} else if (strncmp(s, "skel_dir", 8) == 0) {
87830468d50Sderaadt 				for (cp = s + 8 ; isspace((unsigned char)*cp); cp++) {
879e02380dbSjakob 				}
880580bd31cSmillert 				strsave(&up->u_skeldir, cp);
881e02380dbSjakob 			} else if (strncmp(s, "shell", 5) == 0) {
88230468d50Sderaadt 				for (cp = s + 5 ; isspace((unsigned char)*cp); cp++) {
883e02380dbSjakob 				}
884580bd31cSmillert 				strsave(&up->u_shell, cp);
885ee38b178Sjakob 			} else if (strncmp(s, "password", 8) == 0) {
88630468d50Sderaadt 				for (cp = s + 8 ; isspace((unsigned char)*cp); cp++) {
887ee38b178Sjakob 				}
888580bd31cSmillert 				strsave(&up->u_password, cp);
8893c76c7c0Smillert 			} else if (strncmp(s, "class", 5) == 0) {
89030468d50Sderaadt 				for (cp = s + 5 ; isspace((unsigned char)*cp); cp++) {
8913c76c7c0Smillert 				}
892580bd31cSmillert 				strsave(&up->u_class, cp);
893e02380dbSjakob 			} else if (strncmp(s, "inactive", 8) == 0) {
89430468d50Sderaadt 				for (cp = s + 8 ; isspace((unsigned char)*cp); cp++) {
895e02380dbSjakob 				}
896ddcf21ceSmillert 				if (strcmp(cp, UNSET_INACTIVE) == 0) {
8978be1120aSmestre 					free(up->u_inactive);
898ddcf21ceSmillert 					up->u_inactive = NULL;
899ddcf21ceSmillert 				} else {
900580bd31cSmillert 					strsave(&up->u_inactive, cp);
901ddcf21ceSmillert 				}
902e02380dbSjakob 			} else if (strncmp(s, "range", 5) == 0) {
90330468d50Sderaadt 				for (cp = s + 5 ; isspace((unsigned char)*cp); cp++) {
904e02380dbSjakob 				}
905e02380dbSjakob 				(void) save_range(up, cp);
906e02380dbSjakob 			} else if (strncmp(s, "preserve", 8) == 0) {
90730468d50Sderaadt 				for (cp = s + 8 ; isspace((unsigned char)*cp); cp++) {
908e02380dbSjakob 				}
909e02380dbSjakob 				up->u_preserve = (strncmp(cp, "true", 4) == 0) ? 1 :
910e02380dbSjakob 						  (strncmp(cp, "yes", 3) == 0) ? 1 :
9117f0ae8dbSdlg 						   strtonum(cp, INT_MIN, INT_MAX, NULL);
912e02380dbSjakob 			} else if (strncmp(s, "expire", 6) == 0) {
91330468d50Sderaadt 				for (cp = s + 6 ; isspace((unsigned char)*cp); cp++) {
914e02380dbSjakob 				}
915e02380dbSjakob 				if (strcmp(cp, UNSET_EXPIRY) == 0) {
9168be1120aSmestre 					free(up->u_expire);
917e02380dbSjakob 					up->u_expire = NULL;
918e02380dbSjakob 				} else {
919580bd31cSmillert 					strsave(&up->u_expire, cp);
920e02380dbSjakob 				}
921e02380dbSjakob 			}
922b72161a5Smestre 			free(s);
923e02380dbSjakob 		}
9245520f1ccSderaadt 		fclose(fp);
925e02380dbSjakob 	}
926e02380dbSjakob 	if (up->u_rc == 0) {
927e02380dbSjakob 		up->u_rv[up->u_rc].r_from = DEF_LOWUID;
928e02380dbSjakob 		up->u_rv[up->u_rc].r_to = DEF_HIGHUID;
929e02380dbSjakob 		up->u_rc += 1;
930e02380dbSjakob 	}
931e02380dbSjakob 	up->u_defrc = up->u_rc;
932e02380dbSjakob }
933e02380dbSjakob 
93465b77256Smillert /* return 1 if the specified uid exists in the passwd file, else 0 */
93565b77256Smillert static int
uid_exists(uid_t uid)93665b77256Smillert uid_exists(uid_t uid)
93765b77256Smillert {
93865b77256Smillert     return user_from_uid(uid, 1) != NULL;
93965b77256Smillert }
94065b77256Smillert 
94165b77256Smillert /* return 1 if the specified user exists in the passwd file, else 0 */
94265b77256Smillert static int
user_exists(const char * user)94365b77256Smillert user_exists(const char *user)
94465b77256Smillert {
94565b77256Smillert     uid_t uid;
94665b77256Smillert 
94765b77256Smillert     return uid_from_user(user, &uid) != -1;
94865b77256Smillert }
94965b77256Smillert 
950e02380dbSjakob /* return the next valid unused uid */
951e02380dbSjakob static int
getnextuid(int sync_uid_gid,uid_t * uid,uid_t low_uid,uid_t high_uid)9525b0f5f19Smillert getnextuid(int sync_uid_gid, uid_t *uid, uid_t low_uid, uid_t high_uid)
953e02380dbSjakob {
954e02380dbSjakob 	for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) {
95565b77256Smillert 		if (!uid_exists((uid_t)*uid) && *uid != NOBODY_UID) {
956e02380dbSjakob 			if (sync_uid_gid) {
95765b77256Smillert 				if (!gid_exists((gid_t)*uid)) {
958e02380dbSjakob 					return 1;
959e02380dbSjakob 				}
960e02380dbSjakob 			} else {
961e02380dbSjakob 				return 1;
962e02380dbSjakob 			}
963e02380dbSjakob 		}
964e02380dbSjakob 	}
965e02380dbSjakob 	return 0;
966e02380dbSjakob }
967e02380dbSjakob 
968af911b7cSmillert /* look for a valid time, return 0 if it was specified but bad */
969af911b7cSmillert static int
scantime(time_t * tp,char * s)970af911b7cSmillert scantime(time_t *tp, char *s)
971af911b7cSmillert {
972af911b7cSmillert 	struct tm	tm;
973af911b7cSmillert 
974af911b7cSmillert 	*tp = 0;
975af911b7cSmillert 	if (s != NULL) {
9765520f1ccSderaadt 		memset(&tm, 0, sizeof(tm));
9779dd94cc7Sguenther 		tm.tm_isdst = -1;
978af911b7cSmillert 		if (strptime(s, "%c", &tm) != NULL) {
979af911b7cSmillert 			*tp = mktime(&tm);
980af911b7cSmillert 		} else if (strptime(s, "%B %d %Y", &tm) != NULL) {
981af911b7cSmillert 			*tp = mktime(&tm);
982691235adSmiod 		} else if (isdigit((unsigned char) s[0]) != 0) {
983cea4a14cSderaadt 			*tp = (time_t)atoll(s);
984af911b7cSmillert 		} else {
985af911b7cSmillert 			return 0;
986af911b7cSmillert 		}
987af911b7cSmillert 	}
988af911b7cSmillert 	return 1;
989af911b7cSmillert }
990af911b7cSmillert 
991301af6cfSotto /* compute the extra length '&' expansion consumes */
992301af6cfSotto static size_t
expand_len(const char * p,const char * username)993301af6cfSotto expand_len(const char *p, const char *username)
994301af6cfSotto {
995301af6cfSotto 	size_t alen;
996301af6cfSotto 	size_t ulen;
997301af6cfSotto 
998301af6cfSotto 	ulen = strlen(username);
999301af6cfSotto 	for (alen = 0; *p != '\0'; p++)
1000301af6cfSotto 		if (*p == '&')
1001301af6cfSotto 			alen += ulen - 1;
1002301af6cfSotto 	return alen;
1003301af6cfSotto }
1004301af6cfSotto 
1005944329d0Smillert /* see if we can find out the user struct */
1006944329d0Smillert static struct passwd *
find_user_info(const char * name)1007944329d0Smillert find_user_info(const char *name)
1008944329d0Smillert {
1009944329d0Smillert 	struct passwd	*pwp;
1010944329d0Smillert 	const char	*errstr;
1011944329d0Smillert 	uid_t		uid;
1012944329d0Smillert 
1013944329d0Smillert 	if ((pwp = getpwnam(name)) == NULL) {
1014944329d0Smillert 		uid = strtonum(name, -1, UID_MAX, &errstr);
1015944329d0Smillert 		if (errstr == NULL)
1016944329d0Smillert 			pwp = getpwuid(uid);
1017944329d0Smillert 	}
1018944329d0Smillert 	return pwp;
1019944329d0Smillert }
1020944329d0Smillert 
1021944329d0Smillert /* see if we can find out the group struct */
1022944329d0Smillert static struct group *
find_group_info(const char * name)1023944329d0Smillert find_group_info(const char *name)
1024944329d0Smillert {
1025944329d0Smillert 	struct group	*grp;
1026944329d0Smillert 	const char	*errstr;
1027944329d0Smillert 	gid_t		gid;
1028944329d0Smillert 
1029944329d0Smillert 	if ((grp = getgrnam(name)) == NULL) {
1030944329d0Smillert 		gid = strtonum(name, -1, GID_MAX, &errstr);
1031944329d0Smillert 		if (errstr == NULL)
1032944329d0Smillert 			grp = getgrgid(gid);
1033944329d0Smillert 	}
1034944329d0Smillert 	return grp;
1035944329d0Smillert }
1036944329d0Smillert 
1037e02380dbSjakob /* add a user */
1038e02380dbSjakob static int
adduser(char * login_name,user_t * up)1039be1ff426Smillert adduser(char *login_name, user_t *up)
1040e02380dbSjakob {
1041e02380dbSjakob 	struct group	*grp;
1042e02380dbSjakob 	struct stat	st;
1043e02380dbSjakob 	time_t		expire;
1044ddcf21ceSmillert 	time_t		inactive;
1045e02380dbSjakob 	char		password[PasswordLength + 1];
1046e02380dbSjakob 	char		home[MaxFileNameLen];
10475b0f5f19Smillert 	char		buf[LINE_MAX];
1048e02380dbSjakob 	int		sync_uid_gid;
1049e02380dbSjakob 	int		masterfd;
1050e02380dbSjakob 	int		ptmpfd;
10515b0f5f19Smillert 	gid_t		gid;
1052e02380dbSjakob 	int		cc;
10531f7f71d0Sotto 	int		i, yp = 0;
10541f7f71d0Sotto 	FILE		*fp;
1055e02380dbSjakob 
1056be1ff426Smillert 	if (!valid_login(login_name)) {
1057be1ff426Smillert 		errx(EXIT_FAILURE, "`%s' is not a valid login name", login_name);
1058e02380dbSjakob 	}
10593162cc8dSmillert 	if (!valid_class(up->u_class)) {
10603162cc8dSmillert 		errx(EXIT_FAILURE, "No such login class `%s'", up->u_class);
10613162cc8dSmillert 	}
1062df69c215Sderaadt 	if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) == -1) {
1063ca33ea46Sjakob 		err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD);
1064e02380dbSjakob 	}
1065df69c215Sderaadt 	if (flock(masterfd, LOCK_EX | LOCK_NB) == -1) {
1066ca33ea46Sjakob 		err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD);
1067e02380dbSjakob 	}
1068e02380dbSjakob 	pw_init();
1069df69c215Sderaadt 	if ((ptmpfd = pw_lock(WAITSECS)) == -1) {
1070ffb4dd05Sguenther 		int saved_errno = errno;
10715520f1ccSderaadt 		close(masterfd);
1072ffb4dd05Sguenther 		errc(EXIT_FAILURE, saved_errno, "can't obtain pw_lock");
1073e02380dbSjakob 	}
10741f7f71d0Sotto 	if ((fp = fdopen(masterfd, "r")) == NULL) {
1075ffb4dd05Sguenther 		int saved_errno = errno;
10765520f1ccSderaadt 		close(masterfd);
10775520f1ccSderaadt 		close(ptmpfd);
10785a2e16ecSmillert 		pw_abort();
1079ffb4dd05Sguenther 		errc(EXIT_FAILURE, saved_errno,
1080ffb4dd05Sguenther 		    "can't fdopen `%s' for reading", _PATH_MASTERPASSWD);
10811f7f71d0Sotto 	}
10821f7f71d0Sotto 	while (fgets(buf, sizeof(buf), fp) != NULL) {
10831f7f71d0Sotto 		cc = strlen(buf);
10844c200ecfSmiod 		/*
10854c200ecfSmiod 		 * Stop copying the file at the yp entry; we want to
10864c200ecfSmiod 		 * put the new user before it, and preserve entries
10874c200ecfSmiod 		 * after the yp entry.
10884c200ecfSmiod 		 */
10891f7f71d0Sotto 		if (cc > 1 && buf[0] == '+' && buf[1] == ':') {
10901f7f71d0Sotto 			yp = 1;
10914c200ecfSmiod 			break;
10921f7f71d0Sotto 		}
10931f7f71d0Sotto 		if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
1094ffb4dd05Sguenther 			int saved_errno = errno;
10955520f1ccSderaadt 			fclose(fp);
10965520f1ccSderaadt 			close(ptmpfd);
10971f7f71d0Sotto 			pw_abort();
1098ffb4dd05Sguenther 			errc(EXIT_FAILURE, saved_errno,
1099ffb4dd05Sguenther 			    "short write to /etc/ptmp (not %d chars)", cc);
1100e02380dbSjakob 		}
1101e02380dbSjakob 	}
11021f7f71d0Sotto 	if (ferror(fp)) {
1103ffb4dd05Sguenther 		int saved_errno = errno;
11045520f1ccSderaadt 		fclose(fp);
11055520f1ccSderaadt 		close(ptmpfd);
11061f7f71d0Sotto 		pw_abort();
1107ffb4dd05Sguenther 		errc(EXIT_FAILURE, saved_errno, "read error on %s",
1108ffb4dd05Sguenther 		    _PATH_MASTERPASSWD);
11091f7f71d0Sotto 	}
1110e02380dbSjakob 	/* if no uid was specified, get next one in [low_uid..high_uid] range */
1111e02380dbSjakob 	sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0);
1112b4de0548Smillert 	if (up->u_uid == -1) {
11133b422d36Smillert 		int got_id = 0;
11143b422d36Smillert 
11153b422d36Smillert 		/*
11163b422d36Smillert 		 * Look for a free UID in the command line ranges (if any).
11173b422d36Smillert 		 * These start after the ranges specified in the config file.
11183b422d36Smillert 		 */
11193b422d36Smillert 		for (i = up->u_defrc; got_id == 0 && i < up->u_rc ; i++) {
11203b422d36Smillert 			got_id = getnextuid(sync_uid_gid, &up->u_uid,
11213b422d36Smillert 			    up->u_rv[i].r_from, up->u_rv[i].r_to);
11223b422d36Smillert 		}
11233b422d36Smillert 		/*
11243b422d36Smillert 		 * If there were no free UIDs in the command line ranges,
11253b422d36Smillert 		 * try the ranges from the config file (there will always
11263b422d36Smillert 		 * be at least one default).
11273b422d36Smillert 		 */
11283b422d36Smillert 		if (got_id == 0) {
11293b422d36Smillert 			for (i = 0; got_id == 0 && i < up->u_defrc; i++) {
11303b422d36Smillert 				got_id = getnextuid(sync_uid_gid, &up->u_uid,
11313b422d36Smillert 				    up->u_rv[i].r_from, up->u_rv[i].r_to);
1132e02380dbSjakob 			}
1133e02380dbSjakob 		}
11343b422d36Smillert 		if (got_id == 0) {
11355520f1ccSderaadt 			close(ptmpfd);
11365a2e16ecSmillert 			pw_abort();
11375b0f5f19Smillert 			errx(EXIT_FAILURE, "can't get next uid for %u", up->u_uid);
1138e02380dbSjakob 		}
1139e02380dbSjakob 	}
1140e02380dbSjakob 	/* check uid isn't already allocated */
114165b77256Smillert 	if (!(up->u_flags & F_DUPUID) && uid_exists((uid_t)up->u_uid)) {
11425520f1ccSderaadt 		close(ptmpfd);
11435a2e16ecSmillert 		pw_abort();
11445b0f5f19Smillert 		errx(EXIT_FAILURE, "uid %u is already in use", up->u_uid);
1145e02380dbSjakob 	}
1146e02380dbSjakob 	/* if -g=uid was specified, check gid is unused */
1147e02380dbSjakob 	if (sync_uid_gid) {
114865b77256Smillert 		if (gid_exists((gid_t)up->u_uid)) {
11495520f1ccSderaadt 			close(ptmpfd);
11505a2e16ecSmillert 			pw_abort();
11515b0f5f19Smillert 			errx(EXIT_FAILURE, "gid %u is already in use", up->u_uid);
1152e02380dbSjakob 		}
1153e02380dbSjakob 		gid = up->u_uid;
1154e02380dbSjakob 	} else {
1155944329d0Smillert 		if ((grp = find_group_info(up->u_primgrp)) == NULL) {
11565520f1ccSderaadt 			close(ptmpfd);
11575a2e16ecSmillert 			pw_abort();
1158e02380dbSjakob 			errx(EXIT_FAILURE, "group %s not found", up->u_primgrp);
1159e02380dbSjakob 		}
1160944329d0Smillert 		gid = grp->gr_gid;
1161944329d0Smillert 	}
1162e02380dbSjakob 	/* check name isn't already in use */
116365b77256Smillert 	if (!(up->u_flags & F_DUPUID) && user_exists(login_name)) {
11645520f1ccSderaadt 		close(ptmpfd);
11655a2e16ecSmillert 		pw_abort();
1166be1ff426Smillert 		errx(EXIT_FAILURE, "already a `%s' user", login_name);
1167e02380dbSjakob 	}
11683c76c7c0Smillert 	if (up->u_flags & F_HOMEDIR) {
116997b78fa8Sderaadt 		if (strlcpy(home, up->u_home, sizeof(home)) >= sizeof(home)) {
11705520f1ccSderaadt 			close(ptmpfd);
117197b78fa8Sderaadt 			pw_abort();
117297b78fa8Sderaadt 			errx(EXIT_FAILURE, "home directory `%s' too long",
117397b78fa8Sderaadt 			    up->u_home);
117497b78fa8Sderaadt 		}
1175e02380dbSjakob 	} else {
1176e02380dbSjakob 		/* if home directory hasn't been given, make it up */
117797b78fa8Sderaadt 		if (snprintf(home, sizeof(home), "%s/%s", up->u_basedir,
117897b78fa8Sderaadt 		    login_name) >= sizeof(home)) {
11795520f1ccSderaadt 			close(ptmpfd);
118097b78fa8Sderaadt 			pw_abort();
118197b78fa8Sderaadt 			errx(EXIT_FAILURE, "home directory `%s/%s' too long",
118297b78fa8Sderaadt 			    up->u_basedir, login_name);
118397b78fa8Sderaadt 		}
1184e02380dbSjakob 	}
1185af911b7cSmillert 	if (!scantime(&inactive, up->u_inactive)) {
118644e41a3bSotto 		warnx("Warning: inactive time `%s' invalid, password expiry off",
1187ddcf21ceSmillert 				up->u_inactive);
1188ddcf21ceSmillert 	}
1189af911b7cSmillert 	if (!scantime(&expire, up->u_expire)) {
119044e41a3bSotto 		warnx("Warning: expire time `%s' invalid, account expiry off",
1191ddcf21ceSmillert 				up->u_expire);
1192e02380dbSjakob 	}
1193df69c215Sderaadt 	if (lstat(home, &st) == -1 && !(up->u_flags & F_MKDIR) &&
1194ab48e2e5Smillert 	    strcmp(home, _PATH_NONEXISTENT) != 0) {
11953c76c7c0Smillert 		warnx("Warning: home directory `%s' doesn't exist, and -m was"
11963c76c7c0Smillert 		    " not specified", home);
11973c76c7c0Smillert 	}
1198150ddd88Smillert 	(void) strlcpy(password, up->u_password ? up->u_password : "*",
1199150ddd88Smillert 	    sizeof(password));
1200b07bf859Smillert 	cc = snprintf(buf, sizeof(buf), "%s:%s:%u:%u:%s:%lld:%lld:%s:%s:%s\n",
1201be1ff426Smillert 	    login_name,
1202e02380dbSjakob 	    password,
1203e02380dbSjakob 	    up->u_uid,
1204e02380dbSjakob 	    gid,
12053c76c7c0Smillert 	    up->u_class,
1206b07bf859Smillert 	    (long long) inactive,
1207b07bf859Smillert 	    (long long) expire,
1208e02380dbSjakob 	    up->u_comment,
1209e02380dbSjakob 	    home,
1210ddcf21ceSmillert 	    up->u_shell);
1211301af6cfSotto 	if (cc >= sizeof(buf) || cc < 0 ||
1212301af6cfSotto 	    cc + expand_len(up->u_comment, login_name) >= 1023) {
12135520f1ccSderaadt 		close(ptmpfd);
12145a2e16ecSmillert 		pw_abort();
1215634ea1d6Sho 		errx(EXIT_FAILURE, "can't add `%s', line too long", buf);
1216d05dec83Sjakob 	}
1217e02380dbSjakob 	if (write(ptmpfd, buf, (size_t) cc) != cc) {
1218ffb4dd05Sguenther 		int saved_errno = errno;
12195520f1ccSderaadt 		close(ptmpfd);
12205a2e16ecSmillert 		pw_abort();
1221ffb4dd05Sguenther 		errc(EXIT_FAILURE, saved_errno, "can't add `%s'", buf);
1222e02380dbSjakob 	}
12231f7f71d0Sotto 	if (yp) {
12244c200ecfSmiod 		/* put back the + line */
12251f7f71d0Sotto 		cc = snprintf(buf, sizeof(buf), "+:*::::::::\n");
1226515e489cSderaadt 		if (cc < 0 || cc >= sizeof(buf)) {
12275520f1ccSderaadt 			close(ptmpfd);
1228c71fb469Sderaadt 			pw_abort();
1229c71fb469Sderaadt 			errx(EXIT_FAILURE, "can't add `%s', line too long", buf);
1230c71fb469Sderaadt 		}
12311f7f71d0Sotto 		if (write(ptmpfd, buf, (size_t) cc) != cc) {
1232ffb4dd05Sguenther 			int saved_errno = errno;
12335520f1ccSderaadt 			close(ptmpfd);
12341f7f71d0Sotto 			pw_abort();
1235ffb4dd05Sguenther 			errc(EXIT_FAILURE, saved_errno, "can't add `%s'", buf);
12361f7f71d0Sotto 		}
12374c200ecfSmiod 		/* copy the entries following it, if any */
12384c200ecfSmiod 		while (fgets(buf, sizeof(buf), fp) != NULL) {
12394c200ecfSmiod 			cc = strlen(buf);
12404c200ecfSmiod 			if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
1241ffb4dd05Sguenther 				int saved_errno = errno;
12425520f1ccSderaadt 				fclose(fp);
12435520f1ccSderaadt 				close(ptmpfd);
12444c200ecfSmiod 				pw_abort();
1245ffb4dd05Sguenther 				errc(EXIT_FAILURE, saved_errno,
1246ffb4dd05Sguenther 				    "short write to /etc/ptmp (not %d chars)",
1247ffb4dd05Sguenther 				    cc);
12484c200ecfSmiod 			}
12494c200ecfSmiod 		}
12504c200ecfSmiod 		if (ferror(fp)) {
1251ffb4dd05Sguenther 			int saved_errno = errno;
12525520f1ccSderaadt 			fclose(fp);
12535520f1ccSderaadt 			close(ptmpfd);
12544c200ecfSmiod 			pw_abort();
1255ffb4dd05Sguenther 			errc(EXIT_FAILURE, saved_errno, "read error on %s",
1256ffb4dd05Sguenther 			    _PATH_MASTERPASSWD);
12574c200ecfSmiod 		}
12581f7f71d0Sotto 	}
12593c76c7c0Smillert 	if (up->u_flags & F_MKDIR) {
12603c76c7c0Smillert 		if (lstat(home, &st) == 0) {
12615520f1ccSderaadt 			close(ptmpfd);
12625a2e16ecSmillert 			pw_abort();
12633c76c7c0Smillert 			errx(EXIT_FAILURE, "home directory `%s' already exists",
12643c76c7c0Smillert 			    home);
12653c76c7c0Smillert 		} else {
1266*8e836504Smillert 			char idstr[64];
1267*8e836504Smillert 			const char *mkdir_argv[] =
1268*8e836504Smillert 			    { "mkdir", "-p", home, NULL };
1269*8e836504Smillert 			const char *chown_argv[] =
1270*8e836504Smillert 			    { "chown", "-RP", idstr, home, NULL };
1271*8e836504Smillert 			const char *chmod_argv[] =
1272*8e836504Smillert 			    { "chmod", "-R", "u+w", home, NULL };
1273*8e836504Smillert 
1274*8e836504Smillert 			if (run(MKDIR, mkdir_argv) != 0) {
1275ffb4dd05Sguenther 				int saved_errno = errno;
12765520f1ccSderaadt 				close(ptmpfd);
12775a2e16ecSmillert 				pw_abort();
1278ffb4dd05Sguenther 				errc(EXIT_FAILURE, saved_errno,
1279ffb4dd05Sguenther 				    "can't mkdir `%s'", home);
1280e02380dbSjakob 			}
1281e2fea6e1Smestre 			(void) copydotfiles(up->u_skeldir, home);
1282*8e836504Smillert 			(void) snprintf(idstr, sizeof(idstr), "%u:%u",
1283*8e836504Smillert 			    up->u_uid, gid);
1284*8e836504Smillert 			(void) run(CHOWN, chown_argv);
1285*8e836504Smillert 			(void) run(CHMOD, chmod_argv);
1286e02380dbSjakob 		}
1287e02380dbSjakob 	}
128865b77256Smillert 	if (strcmp(up->u_primgrp, "=uid") == 0 && !group_exists(login_name) &&
12890f7f76edSajacoutot 	    !creategid(login_name, gid, "")) {
12905520f1ccSderaadt 		close(ptmpfd);
12915a2e16ecSmillert 		pw_abort();
129201214246Smillert 		errx(EXIT_FAILURE, "can't create gid %u for login name %s",
1293be1ff426Smillert 		    gid, login_name);
12943c76c7c0Smillert 	}
1295be1ff426Smillert 	if (up->u_groupc > 0 && !append_group(login_name, up->u_groupc, up->u_groupv)) {
12965520f1ccSderaadt 		close(ptmpfd);
12975a2e16ecSmillert 		pw_abort();
1298be1ff426Smillert 		errx(EXIT_FAILURE, "can't append `%s' to new groups", login_name);
1299d50f5675Sho 	}
130063f4fac5Smestre 	fclose(fp);
13015520f1ccSderaadt 	close(ptmpfd);
1302df69c215Sderaadt 	if (pw_mkdb(yp ? NULL : login_name, 0) == -1) {
13035a2e16ecSmillert 		pw_abort();
1304e02380dbSjakob 		err(EXIT_FAILURE, "pw_mkdb failed");
1305e02380dbSjakob 	}
130601214246Smillert 	syslog(LOG_INFO, "new user added: name=%s, uid=%u, gid=%u, home=%s, shell=%s",
1307f65c14a9Smillert 		login_name, up->u_uid, gid, home, up->u_shell);
1308e02380dbSjakob 	return 1;
1309e02380dbSjakob }
1310e02380dbSjakob 
1311be1ff426Smillert /* remove a user from the groups file */
1312be1ff426Smillert static int
rm_user_from_groups(char * login_name)1313be1ff426Smillert rm_user_from_groups(char *login_name)
1314be1ff426Smillert {
1315be1ff426Smillert 	struct stat	st;
13163821fe31Smillert 	size_t		login_len;
1317be1ff426Smillert 	FILE		*from;
1318be1ff426Smillert 	FILE		*to;
1319be1ff426Smillert 	char		buf[LINE_MAX];
1320be1ff426Smillert 	char		f[MaxFileNameLen];
13213821fe31Smillert 	char		*cp, *ep;
1322be1ff426Smillert 	int		fd;
1323be1ff426Smillert 	int		cc;
1324be1ff426Smillert 
13253821fe31Smillert 	login_len = strlen(login_name);
1326be1ff426Smillert 	if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
13273821fe31Smillert 		warn("can't remove gid for `%s': can't open `%s'",
13283821fe31Smillert 		    login_name, _PATH_GROUP);
1329be1ff426Smillert 		return 0;
1330be1ff426Smillert 	}
1331df69c215Sderaadt 	if (flock(fileno(from), LOCK_EX | LOCK_NB) == -1) {
1332be1ff426Smillert 		warn("can't lock `%s'", _PATH_GROUP);
1333be1ff426Smillert 	}
1334be1ff426Smillert 	(void) fstat(fileno(from), &st);
1335be1ff426Smillert 	(void) snprintf(f, sizeof(f), "%s.XXXXXXXX", _PATH_GROUP);
1336c2d43ecaSderaadt 	if ((fd = mkstemp(f)) == -1) {
13373821fe31Smillert 		warn("can't remove gid for `%s': mkstemp failed", login_name);
13385520f1ccSderaadt 		fclose(from);
1339be1ff426Smillert 		return 0;
1340be1ff426Smillert 	}
1341be1ff426Smillert 	if ((to = fdopen(fd, "w")) == NULL) {
1342ffb4dd05Sguenther 		warn("can't remove gid for `%s': fdopen `%s' failed",
1343ffb4dd05Sguenther 		    login_name, f);
13445520f1ccSderaadt 		fclose(from);
13455520f1ccSderaadt 		close(fd);
13465520f1ccSderaadt 		unlink(f);
1347be1ff426Smillert 		return 0;
1348be1ff426Smillert 	}
13496005ac08Sajacoutot 	while (fgets(buf, sizeof(buf), from) != NULL) {
1350be1ff426Smillert 		cc = strlen(buf);
135172c1cea9Schl 		if (cc > 0 && buf[cc - 1] != '\n' && !feof(from)) {
13526202ca67Smillert 			while (fgetc(from) != '\n' && !feof(from))
13536202ca67Smillert 				cc++;
1354bc51f5a6Smiod 			warnx("%s: line `%s' too long (%d bytes), skipping",
13556202ca67Smillert 			    _PATH_GROUP, buf, cc);
13566202ca67Smillert 			continue;
13576202ca67Smillert 		}
13583821fe31Smillert 
13593821fe31Smillert 		/* Break out the group list. */
13603821fe31Smillert 		for (cp = buf, cc = 0; *cp != '\0' && cc < 3; cp++) {
13613821fe31Smillert 			if (*cp == ':')
13623821fe31Smillert 				cc++;
1363be1ff426Smillert 		}
13643821fe31Smillert 		if (cc != 3) {
136572c1cea9Schl 			buf[strcspn(buf, "\n")] = '\0';
136672c1cea9Schl 			warnx("Malformed entry `%s'. Skipping", buf);
13673821fe31Smillert 			continue;
13683821fe31Smillert 		}
13693821fe31Smillert 		while ((cp = strstr(cp, login_name)) != NULL) {
13703821fe31Smillert 			if ((cp[-1] == ':' || cp[-1] == ',') &&
13713821fe31Smillert 			    (cp[login_len] == ',' || cp[login_len] == '\n')) {
13723821fe31Smillert 				ep = cp + login_len;
13733821fe31Smillert 				if (cp[login_len] == ',')
13743821fe31Smillert 					ep++;
13753821fe31Smillert 				else if (cp[-1] == ',')
13763821fe31Smillert 					cp--;
13773821fe31Smillert 				memmove(cp, ep, strlen(ep) + 1);
13783821fe31Smillert 			} else {
13793821fe31Smillert 				if ((cp = strchr(cp, ',')) == NULL)
13803821fe31Smillert 					break;
13813821fe31Smillert 				cp++;
13823821fe31Smillert 			}
13833821fe31Smillert 		}
13843821fe31Smillert 		if (fwrite(buf, strlen(buf), 1, to) != 1) {
1385ffb4dd05Sguenther 			warn("can't remove gid for `%s': short write to `%s'",
1386ffb4dd05Sguenther 			    login_name, f);
13875520f1ccSderaadt 			fclose(from);
13885520f1ccSderaadt 			fclose(to);
13895520f1ccSderaadt 			unlink(f);
1390be1ff426Smillert 			return 0;
1391be1ff426Smillert 		}
1392be1ff426Smillert 	}
1393beaa942cSderaadt 	(void) fchmod(fileno(to), st.st_mode & 0777);
13945520f1ccSderaadt 	fclose(from);
1395bd28cd76Sotto 	if (fclose(to) == EOF) {
1396bd28cd76Sotto 		warn("can't remove gid for `%s': short write to `%s'",
1397bd28cd76Sotto 		    login_name, f);
13985520f1ccSderaadt 		unlink(f);
1399bd28cd76Sotto 		return 0;
1400bd28cd76Sotto 	}
1401df69c215Sderaadt 	if (rename(f, _PATH_GROUP) == -1) {
14023821fe31Smillert 		warn("can't remove gid for `%s': can't rename `%s' to `%s'",
14033821fe31Smillert 		    login_name, f, _PATH_GROUP);
14045520f1ccSderaadt 		unlink(f);
1405be1ff426Smillert 		return 0;
1406be1ff426Smillert 	}
1407be1ff426Smillert 	return 1;
1408be1ff426Smillert }
1409be1ff426Smillert 
1410be1ff426Smillert /* check that the user or group is local, not from YP/NIS */
1411be1ff426Smillert static int
is_local(char * name,const char * file)1412be1ff426Smillert is_local(char *name, const char *file)
1413be1ff426Smillert {
1414be1ff426Smillert 	FILE		*fp;
1415be1ff426Smillert 	char		buf[LINE_MAX];
141645080305Sray 	size_t		len;
1417be1ff426Smillert 	int		ret;
14186202ca67Smillert 	int		cc;
1419be1ff426Smillert 
1420be1ff426Smillert 	if ((fp = fopen(file, "r")) == NULL) {
1421be1ff426Smillert 		err(EXIT_FAILURE, "can't open `%s'", file);
1422be1ff426Smillert 	}
142345080305Sray 	len = strlen(name);
1424be1ff426Smillert 	for (ret = 0 ; fgets(buf, sizeof(buf), fp) != NULL ; ) {
14256202ca67Smillert 		cc = strlen(buf);
142672c1cea9Schl 		if (cc > 0 && buf[cc - 1] != '\n' && !feof(fp)) {
14276202ca67Smillert 			while (fgetc(fp) != '\n' && !feof(fp))
14286202ca67Smillert 				cc++;
1429bc51f5a6Smiod 			warnx("%s: line `%s' too long (%d bytes), skipping",
14306202ca67Smillert 			    file, buf, cc);
14316202ca67Smillert 			continue;
14326202ca67Smillert 		}
143345080305Sray 		if (strncmp(buf, name, len) == 0 && buf[len] == ':') {
1434be1ff426Smillert 			ret = 1;
1435be1ff426Smillert 			break;
1436be1ff426Smillert 		}
1437be1ff426Smillert 	}
14385520f1ccSderaadt 	fclose(fp);
1439be1ff426Smillert 	return ret;
1440be1ff426Smillert }
1441be1ff426Smillert 
1442e02380dbSjakob /* modify a user */
1443e02380dbSjakob static int
moduser(char * login_name,char * newlogin,user_t * up)1444be1ff426Smillert moduser(char *login_name, char *newlogin, user_t *up)
1445e02380dbSjakob {
1446716c272bSmestre 	struct passwd	*pwp = NULL;
1447e02380dbSjakob 	struct group	*grp;
14483c76c7c0Smillert 	const char	*homedir;
14493c76c7c0Smillert 	char		buf[LINE_MAX];
1450c742a9b2Sajacoutot 	char		acctlock_str[] = "-";
1451c742a9b2Sajacoutot 	char		pwlock_str[] = "*";
1452c742a9b2Sajacoutot 	char		pw_len[PasswordLength + 1];
1453c742a9b2Sajacoutot 	char		shell_len[MaxShellNameLen];
1454c742a9b2Sajacoutot 	char		*shell_last_char;
1455301af6cfSotto 	size_t		colonc, loginc;
145648bbee4aSmillert 	size_t		cc;
14577fe7cd4dSajacoutot 	size_t		shell_buf;
1458e02380dbSjakob 	FILE		*master;
145948bbee4aSmillert 	char		newdir[MaxFileNameLen];
1460a0873783Smillert 	char		*colon;
1461c742a9b2Sajacoutot 	char		*pw_tmp = NULL;
1462c742a9b2Sajacoutot 	char		*shell_tmp = NULL;
1463301af6cfSotto 	int		len;
1464c742a9b2Sajacoutot 	int		locked = 0;
1465c742a9b2Sajacoutot 	int		unlocked = 0;
1466e02380dbSjakob 	int		masterfd;
1467e02380dbSjakob 	int		ptmpfd;
14683c76c7c0Smillert 	int		rval;
14698512dbedSajacoutot 	int		i;
1470e02380dbSjakob 
1471e02380dbSjakob 	if (!valid_login(newlogin)) {
1472be1ff426Smillert 		errx(EXIT_FAILURE, "`%s' is not a valid login name", login_name);
1473e02380dbSjakob 	}
1474716c272bSmestre 	if ((pwp = getpwnam_shadow(login_name)) == NULL) {
1475be1ff426Smillert 		errx(EXIT_FAILURE, "No such user `%s'", login_name);
1476be1ff426Smillert 	}
1477716c272bSmestre 	if (up != NULL) {
1478b9bcb8e1Sjsg 		if ((*pwp->pw_passwd != '\0') &&
1479b9bcb8e1Sjsg 		    (up->u_flags & F_PASSWORD) == 0) {
1480716c272bSmestre 			up->u_flags |= F_PASSWORD;
1481580bd31cSmillert 			strsave(&up->u_password, pwp->pw_passwd);
14824f8d1789Smestre 			explicit_bzero(pwp->pw_passwd, strlen(pwp->pw_passwd));
1483716c272bSmestre 		}
1484716c272bSmestre 	}
1485716c272bSmestre 	endpwent();
1486716c272bSmestre 
1487716c272bSmestre 	if (pledge("stdio rpath wpath cpath fattr flock proc exec getpw id",
1488716c272bSmestre 	    NULL) == -1)
1489716c272bSmestre 		err(1, "pledge");
1490716c272bSmestre 
1491be1ff426Smillert 	if (!is_local(login_name, _PATH_MASTERPASSWD)) {
1492be1ff426Smillert 		errx(EXIT_FAILURE, "User `%s' must be a local user", login_name);
1493e02380dbSjakob 	}
1494c742a9b2Sajacoutot 	if (up != NULL) {
14957fe7cd4dSajacoutot 		if ((up->u_flags & (F_ACCTLOCK | F_ACCTUNLOCK)) && (pwp->pw_uid < 1000))
1496c742a9b2Sajacoutot 			errx(EXIT_FAILURE, "(un)locking is not supported for the `%s' account", pwp->pw_name);
1497c742a9b2Sajacoutot 	}
14983c76c7c0Smillert 	/* keep dir name in case we need it for '-m' */
14993c76c7c0Smillert 	homedir = pwp->pw_dir;
15003c76c7c0Smillert 
1501c742a9b2Sajacoutot 	/* get the last char of the shell in case we need it for '-U' or '-Z' */
1502c742a9b2Sajacoutot 	shell_last_char = pwp->pw_shell+strlen(pwp->pw_shell) - 1;
1503c742a9b2Sajacoutot 
1504df69c215Sderaadt 	if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) == -1) {
1505ca33ea46Sjakob 		err(EXIT_FAILURE, "can't open `%s'", _PATH_MASTERPASSWD);
1506e02380dbSjakob 	}
1507df69c215Sderaadt 	if (flock(masterfd, LOCK_EX | LOCK_NB) == -1) {
1508ca33ea46Sjakob 		err(EXIT_FAILURE, "can't lock `%s'", _PATH_MASTERPASSWD);
1509e02380dbSjakob 	}
1510e02380dbSjakob 	pw_init();
1511df69c215Sderaadt 	if ((ptmpfd = pw_lock(WAITSECS)) == -1) {
1512ffb4dd05Sguenther 		int saved_errno = errno;
15135520f1ccSderaadt 		close(masterfd);
1514ffb4dd05Sguenther 		errc(EXIT_FAILURE, saved_errno, "can't obtain pw_lock");
1515e02380dbSjakob 	}
15163c76c7c0Smillert 	if ((master = fdopen(masterfd, "r")) == NULL) {
1517ffb4dd05Sguenther 		int saved_errno = errno;
15185520f1ccSderaadt 		close(masterfd);
15195520f1ccSderaadt 		close(ptmpfd);
15205a2e16ecSmillert 		pw_abort();
1521ffb4dd05Sguenther 		errc(EXIT_FAILURE, saved_errno, "can't fdopen fd for %s",
1522ffb4dd05Sguenther 		    _PATH_MASTERPASSWD);
1523e02380dbSjakob 	}
15243c76c7c0Smillert 	if (up != NULL) {
15253c76c7c0Smillert 		if (up->u_flags & F_USERNAME) {
15263c76c7c0Smillert 			/* if changing name, check new name isn't already in use */
1527636b7295Smestre 			if (strcmp(login_name, newlogin) != 0 &&
152865b77256Smillert 			    user_exists(newlogin)) {
15295520f1ccSderaadt 				close(ptmpfd);
15305a2e16ecSmillert 				pw_abort();
15313c76c7c0Smillert 				errx(EXIT_FAILURE, "already a `%s' user", newlogin);
1532e02380dbSjakob 			}
15333c76c7c0Smillert 			pwp->pw_name = newlogin;
15343c76c7c0Smillert 
15353c76c7c0Smillert 			/*
15363c76c7c0Smillert 			 * Provide a new directory name in case the
15373c76c7c0Smillert 			 * home directory is to be moved.
15383c76c7c0Smillert 			 */
15393c76c7c0Smillert 			if (up->u_flags & F_MKDIR) {
15403c76c7c0Smillert 				(void) snprintf(newdir, sizeof(newdir),
15413c76c7c0Smillert 				    "%s/%s", up->u_basedir, newlogin);
15423c76c7c0Smillert 				pwp->pw_dir = newdir;
1543e02380dbSjakob 			}
15443c76c7c0Smillert 		}
15453c76c7c0Smillert 		if (up->u_flags & F_PASSWORD) {
1546150ddd88Smillert 			if (up->u_password != NULL)
15473c76c7c0Smillert 				pwp->pw_passwd = up->u_password;
15483c76c7c0Smillert 		}
1549c742a9b2Sajacoutot 		if (up->u_flags & F_ACCTLOCK) {
1550c742a9b2Sajacoutot 			/* lock the account */
1551c742a9b2Sajacoutot 			if (*shell_last_char != *acctlock_str) {
1552c742a9b2Sajacoutot 				shell_tmp = malloc(strlen(pwp->pw_shell) + sizeof(acctlock_str));
1553c742a9b2Sajacoutot 				if (shell_tmp == NULL) {
15545520f1ccSderaadt 					close(ptmpfd);
1555c742a9b2Sajacoutot 					pw_abort();
1556c742a9b2Sajacoutot 					errx(EXIT_FAILURE, "account lock: cannot allocate memory");
1557c742a9b2Sajacoutot 				}
1558c742a9b2Sajacoutot 				strlcpy(shell_tmp, pwp->pw_shell, sizeof(shell_len));
1559c742a9b2Sajacoutot 				strlcat(shell_tmp, acctlock_str, sizeof(shell_len));
1560c742a9b2Sajacoutot 				pwp->pw_shell = shell_tmp;
1561c742a9b2Sajacoutot 			} else {
1562c742a9b2Sajacoutot 				locked++;
1563c742a9b2Sajacoutot 			}
1564c742a9b2Sajacoutot 			/* lock the password */
1565c742a9b2Sajacoutot 			if (strncmp(pwp->pw_passwd, pwlock_str, sizeof(pwlock_str)-1) != 0) {
1566c742a9b2Sajacoutot 				pw_tmp = malloc(strlen(pwp->pw_passwd) + sizeof(pwlock_str));
1567c742a9b2Sajacoutot 				if (pw_tmp == NULL) {
15685520f1ccSderaadt 					close(ptmpfd);
1569c742a9b2Sajacoutot 					pw_abort();
1570c742a9b2Sajacoutot 					errx(EXIT_FAILURE, "password lock: cannot allocate memory");
1571c742a9b2Sajacoutot 				}
1572c742a9b2Sajacoutot 				strlcpy(pw_tmp, pwlock_str, sizeof(pw_len));
1573c742a9b2Sajacoutot 				strlcat(pw_tmp, pwp->pw_passwd, sizeof(pw_len));
1574c742a9b2Sajacoutot 				pwp->pw_passwd = pw_tmp;
1575c742a9b2Sajacoutot 			} else {
1576c742a9b2Sajacoutot 				locked++;
1577c742a9b2Sajacoutot 			}
1578c742a9b2Sajacoutot 
1579c742a9b2Sajacoutot 			if (locked > 1)
1580c742a9b2Sajacoutot 				warnx("account `%s' is already locked", pwp->pw_name);
1581c742a9b2Sajacoutot 		}
1582c742a9b2Sajacoutot 		if (up->u_flags & F_ACCTUNLOCK) {
1583c742a9b2Sajacoutot 			/* unlock the password */
158434fd0875Sajacoutot 			if (strcmp(pwp->pw_passwd, pwlock_str) != 0 &&
158534fd0875Sajacoutot 			    strcmp(pwp->pw_passwd, "*************") != 0) {
1586c742a9b2Sajacoutot 				if (strncmp(pwp->pw_passwd, pwlock_str, sizeof(pwlock_str)-1) == 0) {
1587c742a9b2Sajacoutot 					pwp->pw_passwd += sizeof(pwlock_str)-1;
1588c742a9b2Sajacoutot 				} else {
1589c742a9b2Sajacoutot 					unlocked++;
1590c742a9b2Sajacoutot 				}
159134fd0875Sajacoutot 			} else {
159234fd0875Sajacoutot 				warnx("account `%s' has no password: cannot fully unlock", pwp->pw_name);
159334fd0875Sajacoutot 			}
1594c742a9b2Sajacoutot 			/* unlock the account */
1595c742a9b2Sajacoutot 			if (*shell_last_char == *acctlock_str) {
15967fe7cd4dSajacoutot 				shell_buf = strlen(pwp->pw_shell) + 2 - sizeof(acctlock_str);
15977fe7cd4dSajacoutot 				shell_tmp = malloc(shell_buf);
1598c742a9b2Sajacoutot 				if (shell_tmp == NULL) {
15995520f1ccSderaadt 					close(ptmpfd);
1600c742a9b2Sajacoutot 					pw_abort();
1601c742a9b2Sajacoutot 					errx(EXIT_FAILURE, "unlock: cannot allocate memory");
1602c742a9b2Sajacoutot 				}
16037fe7cd4dSajacoutot 				strlcpy(shell_tmp, pwp->pw_shell, shell_buf);
1604c742a9b2Sajacoutot 				pwp->pw_shell = shell_tmp;
1605c742a9b2Sajacoutot 			} else {
1606c742a9b2Sajacoutot 				unlocked++;
1607c742a9b2Sajacoutot 			}
1608c742a9b2Sajacoutot 
1609c742a9b2Sajacoutot 			if (unlocked > 1)
1610c742a9b2Sajacoutot 				warnx("account `%s' is not locked", pwp->pw_name);
1611c742a9b2Sajacoutot 		}
16123c76c7c0Smillert 		if (up->u_flags & F_UID) {
16133c76c7c0Smillert 			/* check uid isn't already allocated */
161465b77256Smillert 			if (!(up->u_flags & F_DUPUID) &&
161565b77256Smillert 			    uid_exists((uid_t)up->u_uid)) {
16165520f1ccSderaadt 				close(ptmpfd);
16175a2e16ecSmillert 				pw_abort();
16185b0f5f19Smillert 				errx(EXIT_FAILURE, "uid %u is already in use", up->u_uid);
16193c76c7c0Smillert 			}
16203c76c7c0Smillert 			pwp->pw_uid = up->u_uid;
16213c76c7c0Smillert 		}
16223c76c7c0Smillert 		if (up->u_flags & F_GROUP) {
1623e02380dbSjakob 			/* if -g=uid was specified, check gid is unused */
16243c76c7c0Smillert 			if (strcmp(up->u_primgrp, "=uid") == 0) {
162565b77256Smillert 				if (gid_exists((gid_t)pwp->pw_uid)) {
16265520f1ccSderaadt 					close(ptmpfd);
16275a2e16ecSmillert 					pw_abort();
1628db5687b1Smestre 					errx(EXIT_FAILURE, "gid %u is already "
1629db5687b1Smestre 					    "in use", pwp->pw_uid);
1630e02380dbSjakob 				}
1631db5687b1Smestre 				pwp->pw_gid = pwp->pw_uid;
16326a23a2c0Smestre 				if (!creategid(newlogin, pwp->pw_gid, "")) {
1633db5687b1Smestre 					close(ptmpfd);
1634db5687b1Smestre 					pw_abort();
1635db5687b1Smestre 					errx(EXIT_FAILURE, "could not create "
16366a23a2c0Smestre 					    "group %s with gid %u", newlogin,
16376a23a2c0Smestre 					    pwp->pw_gid);
1638db5687b1Smestre 				}
1639e02380dbSjakob 			} else {
1640944329d0Smillert 				if ((grp = find_group_info(up->u_primgrp)) == NULL) {
16415520f1ccSderaadt 					close(ptmpfd);
16425a2e16ecSmillert 					pw_abort();
1643944329d0Smillert 					errx(EXIT_FAILURE, "group %s not found",
1644944329d0Smillert 					    up->u_primgrp);
1645944329d0Smillert 				}
1646944329d0Smillert 				pwp->pw_gid = grp->gr_gid;
1647e02380dbSjakob 			}
1648e02380dbSjakob 		}
1649ddcf21ceSmillert 		if (up->u_flags & F_INACTIVE) {
1650af911b7cSmillert 			if (!scantime(&pwp->pw_change, up->u_inactive)) {
1651ddcf21ceSmillert 				warnx("Warning: inactive time `%s' invalid, password expiry off",
1652ddcf21ceSmillert 					up->u_inactive);
1653ddcf21ceSmillert 			}
1654ddcf21ceSmillert 		}
16553c76c7c0Smillert 		if (up->u_flags & F_EXPIRE) {
1656af911b7cSmillert 			if (!scantime(&pwp->pw_expire, up->u_expire)) {
165744e41a3bSotto 				warnx("Warning: expire time `%s' invalid, account expiry off",
1658ddcf21ceSmillert 					up->u_expire);
1659ddcf21ceSmillert 			}
1660e02380dbSjakob 		}
16613c76c7c0Smillert 		if (up->u_flags & F_COMMENT)
16623c76c7c0Smillert 			pwp->pw_gecos = up->u_comment;
16633c76c7c0Smillert 		if (up->u_flags & F_HOMEDIR)
16643c76c7c0Smillert 			pwp->pw_dir = up->u_home;
16653c76c7c0Smillert 		if (up->u_flags & F_SHELL)
16663c76c7c0Smillert 			pwp->pw_shell = up->u_shell;
16673fe53365Smillert 		if (up->u_flags & F_CLASS) {
16683fe53365Smillert 			if (!valid_class(up->u_class)) {
16695520f1ccSderaadt 				close(ptmpfd);
16703fe53365Smillert 				pw_abort();
16713fe53365Smillert 				errx(EXIT_FAILURE,
16723fe53365Smillert 				    "No such login class `%s'", up->u_class);
16733fe53365Smillert 			}
16743c76c7c0Smillert 			pwp->pw_class = up->u_class;
16753fe53365Smillert 		}
1676e02380dbSjakob 	}
1677be1ff426Smillert 	loginc = strlen(login_name);
1678a0873783Smillert 	while (fgets(buf, sizeof(buf), master) != NULL) {
1679a0873783Smillert 		if ((colon = strchr(buf, ':')) == NULL) {
1680a0873783Smillert 			warnx("Malformed entry `%s'. Skipping", buf);
1681e02380dbSjakob 			continue;
1682e02380dbSjakob 		}
1683a0873783Smillert 		colonc = (size_t)(colon - buf);
1684a0873783Smillert 		if (strncmp(login_name, buf, loginc) == 0 && loginc == colonc) {
16853c76c7c0Smillert 			if (up != NULL) {
16864f182e61Smillert 				if ((len = snprintf(buf, sizeof(buf),
1687b07bf859Smillert 				    "%s:%s:%u:%u:%s:%lld:%lld:%s:%s:%s\n",
1688e02380dbSjakob 				    newlogin,
16893c76c7c0Smillert 				    pwp->pw_passwd,
16903c76c7c0Smillert 				    pwp->pw_uid,
16913c76c7c0Smillert 				    pwp->pw_gid,
16923c76c7c0Smillert 				    pwp->pw_class,
1693b07bf859Smillert 				    (long long)pwp->pw_change,
1694b07bf859Smillert 				    (long long)pwp->pw_expire,
16953c76c7c0Smillert 				    pwp->pw_gecos,
16963c76c7c0Smillert 				    pwp->pw_dir,
16974f182e61Smillert 				    pwp->pw_shell)) >= sizeof(buf) || len < 0 ||
1698301af6cfSotto 				    len + expand_len(pwp->pw_gecos, newlogin)
1699301af6cfSotto 				    >= 1023) {
17005520f1ccSderaadt 					close(ptmpfd);
17015a2e16ecSmillert 					pw_abort();
170290647191Sgrange 					errx(EXIT_FAILURE, "can't add `%s', "
170371872659Sajacoutot 					    "line too long (%zu bytes)", buf,
1704301af6cfSotto 					    len + expand_len(pwp->pw_gecos,
1705301af6cfSotto 					    newlogin));
1706d05dec83Sjakob 				}
17073c76c7c0Smillert 				if (write(ptmpfd, buf, len) != len) {
1708ffb4dd05Sguenther 					int saved_errno = errno;
17095520f1ccSderaadt 					close(ptmpfd);
17105a2e16ecSmillert 					pw_abort();
1711ffb4dd05Sguenther 					errc(EXIT_FAILURE, saved_errno,
1712ffb4dd05Sguenther 					    "can't add `%s'", buf);
1713e02380dbSjakob 				}
1714d50f5675Sho 			}
1715a0873783Smillert 		} else {
1716a0873783Smillert 			len = strlen(buf);
1717a0873783Smillert 			if ((cc = write(ptmpfd, buf, len)) != len) {
1718ffb4dd05Sguenther 				int saved_errno = errno;
17195520f1ccSderaadt 				close(masterfd);
17205520f1ccSderaadt 				close(ptmpfd);
17215a2e16ecSmillert 				pw_abort();
1722ffb4dd05Sguenther 				errc(EXIT_FAILURE, saved_errno,
1723ffb4dd05Sguenther 				    "short write to /etc/ptmp (%lld not %lld chars)",
172448bbee4aSmillert 				    (long long)cc, (long long)len);
1725e02380dbSjakob 			}
1726e02380dbSjakob 		}
1727a0873783Smillert 	}
17283c76c7c0Smillert 	if (up != NULL) {
1729*8e836504Smillert 		const char *mv_argv[] = { "mv", homedir, pwp->pw_dir, NULL };
17303c76c7c0Smillert 		if ((up->u_flags & F_MKDIR) &&
1731*8e836504Smillert 		    run(MV, mv_argv) != 0) {
1732ffb4dd05Sguenther 			int saved_errno = errno;
17335520f1ccSderaadt 			close(ptmpfd);
17345a2e16ecSmillert 			pw_abort();
1735ffb4dd05Sguenther 			errc(EXIT_FAILURE, saved_errno,
1736ffb4dd05Sguenther 			    "can't move `%s' to `%s'", homedir, pwp->pw_dir);
17373c76c7c0Smillert 		}
17388512dbedSajacoutot 		if (up->u_flags & F_SETSECGROUP) {
17398512dbedSajacoutot 			for (i = 0 ; i < up->u_groupc ; i++) {
174065b77256Smillert 				if (!group_exists(up->u_groupv[i])) {
17415520f1ccSderaadt 					close(ptmpfd);
17428512dbedSajacoutot 					pw_abort();
17435520f1ccSderaadt 					errx(EXIT_FAILURE,
17445520f1ccSderaadt 					    "aborting, group `%s' does not exist",
17458512dbedSajacoutot 					    up->u_groupv[i]);
17468512dbedSajacoutot 				}
17478512dbedSajacoutot 			}
17488512dbedSajacoutot 			if (!rm_user_from_groups(newlogin)) {
17495520f1ccSderaadt 				close(ptmpfd);
17500e0fd575Sajacoutot 				pw_abort();
17515520f1ccSderaadt 				errx(EXIT_FAILURE,
17525520f1ccSderaadt 				    "can't reset groups for `%s'", newlogin);
17530e0fd575Sajacoutot 			}
17548512dbedSajacoutot 		}
1755f440ff29Sajacoutot 		if (up->u_groupc > 0) {
17560e0fd575Sajacoutot 		    if (!append_group(newlogin, up->u_groupc, up->u_groupv)) {
17575520f1ccSderaadt 			close(ptmpfd);
17585a2e16ecSmillert 			pw_abort();
17593c76c7c0Smillert 			errx(EXIT_FAILURE, "can't append `%s' to new groups",
17603c76c7c0Smillert 			    newlogin);
17613c76c7c0Smillert 		    }
1762e02380dbSjakob 		}
17630e0fd575Sajacoutot 	}
176463f4fac5Smestre 	fclose(master);
17655520f1ccSderaadt 	close(ptmpfd);
17668be1120aSmestre 	free(pw_tmp);
17678be1120aSmestre 	free(shell_tmp);
1768be1ff426Smillert 	if (up != NULL && strcmp(login_name, newlogin) == 0)
1769be1ff426Smillert 		rval = pw_mkdb(login_name, 0);
17700a14dc83Smillert 	else
17713c76c7c0Smillert 		rval = pw_mkdb(NULL, 0);
17725a2e16ecSmillert 	if (rval == -1) {
17735a2e16ecSmillert 		pw_abort();
1774e02380dbSjakob 		err(EXIT_FAILURE, "pw_mkdb failed");
17755a2e16ecSmillert 	}
1776f65c14a9Smillert 	if (up == NULL) {
1777f65c14a9Smillert 		syslog(LOG_INFO, "user removed: name=%s", login_name);
1778f65c14a9Smillert 	} else if (strcmp(login_name, newlogin) == 0) {
177901214246Smillert 		syslog(LOG_INFO, "user information modified: name=%s, uid=%u, gid=%u, home=%s, shell=%s",
1780f65c14a9Smillert 			login_name, pwp->pw_uid, pwp->pw_gid, pwp->pw_dir, pwp->pw_shell);
1781f65c14a9Smillert 	} else {
178201214246Smillert 		syslog(LOG_INFO, "user information modified: name=%s, new name=%s, uid=%u, gid=%u, home=%s, shell=%s",
1783f65c14a9Smillert 			login_name, newlogin, pwp->pw_uid, pwp->pw_gid, pwp->pw_dir, pwp->pw_shell);
1784f65c14a9Smillert 	}
1785e02380dbSjakob 	return 1;
1786e02380dbSjakob }
1787e02380dbSjakob 
1788e02380dbSjakob /* print out usage message, and then exit */
1789e02380dbSjakob void
usermgmt_usage(const char * prog)17903c76c7c0Smillert usermgmt_usage(const char *prog)
1791e02380dbSjakob {
1792e02380dbSjakob 	if (strcmp(prog, "useradd") == 0) {
17935520f1ccSderaadt 		fprintf(stderr, "usage: %s -D [-b base-directory] "
17940f5c715dSsobrado 		    "[-e expiry-time] [-f inactive-time]\n"
17950f5c715dSsobrado 		    "               [-g gid | name | =uid] [-k skel-directory] "
17960f5c715dSsobrado 		    "[-L login-class]\n"
17970f5c715dSsobrado 		    "               [-r low..high] [-s shell]\n", prog);
17985520f1ccSderaadt 		fprintf(stderr, "       %s [-mov] [-b base-directory] "
17990f5c715dSsobrado 		    "[-c comment] [-d home-directory]\n"
18000f5c715dSsobrado 		    "               [-e expiry-time] [-f inactive-time]\n"
18010f5c715dSsobrado 		    "               [-G secondary-group[,group,...]] "
18020f5c715dSsobrado 		    "[-g gid | name | =uid]\n"
18030f5c715dSsobrado 		    "               [-k skel-directory] [-L login-class] "
18040f5c715dSsobrado 		    "[-p password] [-r low..high]\n"
18050f5c715dSsobrado 		    "               [-s shell] [-u uid] user\n", prog);
1806e02380dbSjakob 	} else if (strcmp(prog, "usermod") == 0) {
18075520f1ccSderaadt 		fprintf(stderr, "usage: %s [-moUvZ] "
1808eb963bfcSjmc 		    "[-c comment] [-d home-directory] [-e expiry-time]\n"
1809eb963bfcSjmc 		    "               [-f inactive-time] "
1810eb963bfcSjmc 		    "[-G secondary-group[,group,...]]\n"
18110f5c715dSsobrado 		    "               [-g gid | name | =uid] [-L login-class] "
18120f5c715dSsobrado 		    "[-l new-login]\n"
18130e0fd575Sajacoutot 		    "               [-p password] "
18140e0fd575Sajacoutot 		    "[-S secondary-group[,group,...]]\n"
18150e0fd575Sajacoutot 		    "               [-s shell] [-u uid] user\n",
18163c76c7c0Smillert 		    prog);
1817e02380dbSjakob 	} else if (strcmp(prog, "userdel") == 0) {
18185520f1ccSderaadt 		fprintf(stderr, "usage: %s -D [-p preserve-value]\n",
18190f5c715dSsobrado 		    prog);
18205520f1ccSderaadt 		fprintf(stderr, "       %s [-rv] [-p preserve-value] "
1821fe0353c0Sjmc 		    "user\n", prog);
1822e02380dbSjakob 	} else if (strcmp(prog, "userinfo") == 0) {
18235520f1ccSderaadt 		fprintf(stderr, "usage: %s [-e] user\n", prog);
1824e02380dbSjakob 	} else if (strcmp(prog, "groupadd") == 0) {
18255520f1ccSderaadt 		fprintf(stderr, "usage: %s [-ov] [-g gid] group\n",
1826e02380dbSjakob 		    prog);
1827e02380dbSjakob 	} else if (strcmp(prog, "groupdel") == 0) {
18285520f1ccSderaadt 		fprintf(stderr, "usage: %s [-v] group\n", prog);
1829e02380dbSjakob 	} else if (strcmp(prog, "groupmod") == 0) {
18305520f1ccSderaadt 		fprintf(stderr, "usage: %s [-ov] [-g gid] [-n newname] "
18313c76c7c0Smillert 		    "group\n", prog);
1832e02380dbSjakob 	} else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) {
18335520f1ccSderaadt 		fprintf(stderr, "usage: %s [add | del | mod"
1834844c8300Sjakob 		" | info"
183528918b2cSjakob 		"] ...\n",
1836e02380dbSjakob 		    prog);
1837e02380dbSjakob 	} else if (strcmp(prog, "groupinfo") == 0) {
18385520f1ccSderaadt 		fprintf(stderr, "usage: %s [-e] group\n", prog);
18392c389526Sjakob 	} else {
18405520f1ccSderaadt 		fprintf(stderr, "This program must be called as {user,group}{add,del,mod,info},\n%s is not an understood name.\n", prog);
1841e02380dbSjakob 	}
1842e02380dbSjakob 	exit(EXIT_FAILURE);
1843e02380dbSjakob }
1844e02380dbSjakob 
1845e02380dbSjakob int
useradd(int argc,char ** argv)1846e02380dbSjakob useradd(int argc, char **argv)
1847e02380dbSjakob {
1848e02380dbSjakob 	user_t	u;
1849944329d0Smillert 	const char *errstr;
1850e02380dbSjakob 	int	defaultfield;
1851e02380dbSjakob 	int	bigD;
1852e02380dbSjakob 	int	c;
1853e02380dbSjakob 	int	i;
1854e02380dbSjakob 
18555520f1ccSderaadt 	memset(&u, 0, sizeof(u));
1856e02380dbSjakob 	read_defaults(&u);
1857b4de0548Smillert 	u.u_uid = -1;
1858e02380dbSjakob 	defaultfield = bigD = 0;
18595c797a00Sajacoutot 	while ((c = getopt(argc, argv, "DG:L:b:c:d:e:f:g:k:mop:r:s:u:v")) != -1) {
1860e02380dbSjakob 		switch(c) {
1861e02380dbSjakob 		case 'D':
1862e02380dbSjakob 			bigD = 1;
1863e02380dbSjakob 			break;
1864e02380dbSjakob 		case 'G':
18653c76c7c0Smillert 			while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
18663c76c7c0Smillert 			    u.u_groupc < NGROUPS_MAX - 2) {
18673c76c7c0Smillert 				if (u.u_groupv[u.u_groupc][0] != 0) {
18683c76c7c0Smillert 					u.u_groupc++;
18693c76c7c0Smillert 				}
18703c76c7c0Smillert 			}
18713c76c7c0Smillert 			if (optarg != NULL) {
18723c76c7c0Smillert 				warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX - 2);
18733c76c7c0Smillert 			}
1874e02380dbSjakob 			break;
1875e02380dbSjakob 		case 'b':
1876e02380dbSjakob 			defaultfield = 1;
1877580bd31cSmillert 			strsave(&u.u_basedir, optarg);
1878e02380dbSjakob 			break;
1879e02380dbSjakob 		case 'c':
1880580bd31cSmillert 			strsave(&u.u_comment, optarg);
1881e02380dbSjakob 			break;
1882e02380dbSjakob 		case 'd':
1883580bd31cSmillert 			strsave(&u.u_home, optarg);
18843c76c7c0Smillert 			u.u_flags |= F_HOMEDIR;
1885e02380dbSjakob 			break;
1886e02380dbSjakob 		case 'e':
1887e02380dbSjakob 			defaultfield = 1;
1888580bd31cSmillert 			strsave(&u.u_expire, optarg);
1889e02380dbSjakob 			break;
1890e02380dbSjakob 		case 'f':
1891e02380dbSjakob 			defaultfield = 1;
1892580bd31cSmillert 			strsave(&u.u_inactive, optarg);
1893e02380dbSjakob 			break;
1894e02380dbSjakob 		case 'g':
1895e02380dbSjakob 			defaultfield = 1;
1896580bd31cSmillert 			strsave(&u.u_primgrp, optarg);
1897e02380dbSjakob 			break;
1898e02380dbSjakob 		case 'k':
18994c74a5e1Smillert 			defaultfield = 1;
1900580bd31cSmillert 			strsave(&u.u_skeldir, optarg);
1901e02380dbSjakob 			break;
19023c76c7c0Smillert 		case 'L':
19033c76c7c0Smillert 			defaultfield = 1;
1904580bd31cSmillert 			strsave(&u.u_class, optarg);
19053c76c7c0Smillert 			break;
1906e02380dbSjakob 		case 'm':
19073c76c7c0Smillert 			u.u_flags |= F_MKDIR;
1908e02380dbSjakob 			break;
1909e02380dbSjakob 		case 'o':
19103c76c7c0Smillert 			u.u_flags |= F_DUPUID;
1911e02380dbSjakob 			break;
1912e02380dbSjakob 		case 'p':
1913580bd31cSmillert 			strsave(&u.u_password, optarg);
19144f8d1789Smestre 			explicit_bzero(optarg, strlen(optarg));
1915e02380dbSjakob 			break;
1916e02380dbSjakob 		case 'r':
1917e02380dbSjakob 			defaultfield = 1;
1918446b5bdaSmillert 			if (!save_range(&u, optarg))
1919446b5bdaSmillert 				exit(EXIT_FAILURE);
1920e02380dbSjakob 			break;
1921e02380dbSjakob 		case 's':
1922e02380dbSjakob 			defaultfield = 1;
1923580bd31cSmillert 			strsave(&u.u_shell, optarg);
1924e02380dbSjakob 			break;
1925e02380dbSjakob 		case 'u':
1926944329d0Smillert 			u.u_uid = strtonum(optarg, -1, UID_MAX, &errstr);
1927944329d0Smillert 			if (errstr != NULL) {
1928e02380dbSjakob 				errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
1929e02380dbSjakob 			}
1930e02380dbSjakob 			break;
1931e02380dbSjakob 		case 'v':
1932e02380dbSjakob 			verbose = 1;
1933e02380dbSjakob 			break;
1934658e04adSmillert 		default:
1935658e04adSmillert 			usermgmt_usage("useradd");
1936e02380dbSjakob 		}
1937e02380dbSjakob 	}
1938d79ad1b7Smestre 
1939d79ad1b7Smestre 	if (pledge("stdio rpath wpath cpath fattr flock proc exec getpw id",
1940d79ad1b7Smestre 	    NULL) == -1)
1941d79ad1b7Smestre 		err(1, "pledge");
1942d79ad1b7Smestre 
1943e02380dbSjakob 	if (bigD) {
1944e02380dbSjakob 		if (defaultfield) {
1945e02380dbSjakob 			checkeuid();
1946e02380dbSjakob 			return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
1947e02380dbSjakob 		}
19485520f1ccSderaadt 		printf("group\t\t%s\n", u.u_primgrp);
19495520f1ccSderaadt 		printf("base_dir\t%s\n", u.u_basedir);
19505520f1ccSderaadt 		printf("skel_dir\t%s\n", u.u_skeldir);
19515520f1ccSderaadt 		printf("shell\t\t%s\n", u.u_shell);
19525520f1ccSderaadt 		printf("class\t\t%s\n", u.u_class);
19535520f1ccSderaadt 		printf("inactive\t%s\n", (u.u_inactive == NULL) ? UNSET_INACTIVE : u.u_inactive);
19545520f1ccSderaadt 		printf("expire\t\t%s\n", (u.u_expire == NULL) ? UNSET_EXPIRY : u.u_expire);
1955e02380dbSjakob 		for (i = 0 ; i < u.u_rc ; i++) {
19565520f1ccSderaadt 			printf("range\t\t%u..%u\n", u.u_rv[i].r_from, u.u_rv[i].r_to);
1957e02380dbSjakob 		}
1958e02380dbSjakob 		return EXIT_SUCCESS;
1959e02380dbSjakob 	}
19603c76c7c0Smillert 	argc -= optind;
19613c76c7c0Smillert 	argv += optind;
19623c76c7c0Smillert 	if (argc != 1) {
1963e02380dbSjakob 		usermgmt_usage("useradd");
1964e02380dbSjakob 	}
1965e02380dbSjakob 	checkeuid();
1966f65c14a9Smillert 	openlog("useradd", LOG_PID, LOG_USER);
19673c76c7c0Smillert 	return adduser(*argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
1968e02380dbSjakob }
1969e02380dbSjakob 
1970e02380dbSjakob int
usermod(int argc,char ** argv)1971e02380dbSjakob usermod(int argc, char **argv)
1972e02380dbSjakob {
1973e02380dbSjakob 	user_t	u;
1974e02380dbSjakob 	char	newuser[MaxUserNameLen + 1];
19753c76c7c0Smillert 	int	c, have_new_user;
1976944329d0Smillert 	const char *errstr;
1977e02380dbSjakob 
19785520f1ccSderaadt 	memset(&u, 0, sizeof(u));
19795520f1ccSderaadt 	memset(newuser, 0, sizeof(newuser));
1980e02380dbSjakob 	read_defaults(&u);
19819835155aSho 	free(u.u_primgrp);
19829835155aSho 	u.u_primgrp = NULL;
1983e02380dbSjakob 	have_new_user = 0;
1984c742a9b2Sajacoutot 	while ((c = getopt(argc, argv, "G:L:S:UZc:d:e:f:g:l:mop:s:u:v")) != -1) {
1985e02380dbSjakob 		switch(c) {
1986e02380dbSjakob 		case 'G':
19873c76c7c0Smillert 			while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
19883c76c7c0Smillert 			    u.u_groupc < NGROUPS_MAX - 2) {
19893c76c7c0Smillert 				if (u.u_groupv[u.u_groupc][0] != 0) {
19903c76c7c0Smillert 					u.u_groupc++;
19913c76c7c0Smillert 				}
19923c76c7c0Smillert 			}
19933c76c7c0Smillert 			if (optarg != NULL) {
19943c76c7c0Smillert 				warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX - 2);
19953c76c7c0Smillert 			}
19963c76c7c0Smillert 			u.u_flags |= F_SECGROUP;
1997e02380dbSjakob 			break;
19980e0fd575Sajacoutot 		case 'S':
19990e0fd575Sajacoutot 			while ((u.u_groupv[u.u_groupc] = strsep(&optarg, ",")) != NULL &&
20000e0fd575Sajacoutot 			    u.u_groupc < NGROUPS_MAX - 2) {
20010e0fd575Sajacoutot 				if (u.u_groupv[u.u_groupc][0] != 0) {
20020e0fd575Sajacoutot 					u.u_groupc++;
20030e0fd575Sajacoutot 				}
20040e0fd575Sajacoutot 			}
20050e0fd575Sajacoutot 			if (optarg != NULL) {
20060e0fd575Sajacoutot 				warnx("Truncated list of secondary groups to %d entries", NGROUPS_MAX - 2);
20070e0fd575Sajacoutot 			}
20080e0fd575Sajacoutot 			u.u_flags |= F_SETSECGROUP;
20090e0fd575Sajacoutot 			break;
2010c742a9b2Sajacoutot 		case 'U':
2011c742a9b2Sajacoutot 			u.u_flags |= F_ACCTUNLOCK;
2012c742a9b2Sajacoutot 			break;
2013c742a9b2Sajacoutot 		case 'Z':
2014c742a9b2Sajacoutot 			u.u_flags |= F_ACCTLOCK;
2015c742a9b2Sajacoutot 			break;
2016e02380dbSjakob 		case 'c':
2017580bd31cSmillert 			strsave(&u.u_comment, optarg);
20183c76c7c0Smillert 			u.u_flags |= F_COMMENT;
2019e02380dbSjakob 			break;
2020e02380dbSjakob 		case 'd':
2021580bd31cSmillert 			strsave(&u.u_home, optarg);
20223c76c7c0Smillert 			u.u_flags |= F_HOMEDIR;
2023e02380dbSjakob 			break;
2024e02380dbSjakob 		case 'e':
2025580bd31cSmillert 			strsave(&u.u_expire, optarg);
20263c76c7c0Smillert 			u.u_flags |= F_EXPIRE;
2027e02380dbSjakob 			break;
2028e02380dbSjakob 		case 'f':
2029580bd31cSmillert 			strsave(&u.u_inactive, optarg);
20303c76c7c0Smillert 			u.u_flags |= F_INACTIVE;
2031e02380dbSjakob 			break;
2032e02380dbSjakob 		case 'g':
2033580bd31cSmillert 			strsave(&u.u_primgrp, optarg);
20343c76c7c0Smillert 			u.u_flags |= F_GROUP;
2035e02380dbSjakob 			break;
2036e02380dbSjakob 		case 'l':
203797b78fa8Sderaadt 			if (strlcpy(newuser, optarg, sizeof(newuser)) >=
203897b78fa8Sderaadt 			    sizeof(newuser))
203997b78fa8Sderaadt 				errx(EXIT_FAILURE, "username `%s' too long",
204097b78fa8Sderaadt 				    optarg);
20413c76c7c0Smillert 			have_new_user = 1;
20423c76c7c0Smillert 			u.u_flags |= F_USERNAME;
2043e02380dbSjakob 			break;
20443c76c7c0Smillert 		case 'L':
2045580bd31cSmillert 			strsave(&u.u_class, optarg);
20463c76c7c0Smillert 			u.u_flags |= F_CLASS;
20473c76c7c0Smillert 			break;
2048e02380dbSjakob 		case 'm':
20493c76c7c0Smillert 			u.u_flags |= F_MKDIR;
2050e02380dbSjakob 			break;
2051e02380dbSjakob 		case 'o':
20523c76c7c0Smillert 			u.u_flags |= F_DUPUID;
2053e02380dbSjakob 			break;
2054e02380dbSjakob 		case 'p':
2055580bd31cSmillert 			strsave(&u.u_password, optarg);
20564f8d1789Smestre 			explicit_bzero(optarg, strlen(optarg));
20573c76c7c0Smillert 			u.u_flags |= F_PASSWORD;
2058e02380dbSjakob 			break;
2059e02380dbSjakob 		case 's':
2060580bd31cSmillert 			strsave(&u.u_shell, optarg);
20613c76c7c0Smillert 			u.u_flags |= F_SHELL;
2062e02380dbSjakob 			break;
2063e02380dbSjakob 		case 'u':
2064944329d0Smillert 			u.u_uid = strtonum(optarg, -1, UID_MAX, &errstr);
2065944329d0Smillert 			u.u_flags |= F_UID;
2066944329d0Smillert 			if (errstr != NULL) {
2067e02380dbSjakob 				errx(EXIT_FAILURE, "When using [-u uid], the uid must be numeric");
2068e02380dbSjakob 			}
2069e02380dbSjakob 			break;
2070e02380dbSjakob 		case 'v':
2071e02380dbSjakob 			verbose = 1;
2072e02380dbSjakob 			break;
2073658e04adSmillert 		default:
2074658e04adSmillert 			usermgmt_usage("usermod");
2075e02380dbSjakob 		}
2076e02380dbSjakob 	}
2077d79ad1b7Smestre 
20783c76c7c0Smillert 	if ((u.u_flags & F_MKDIR) && !(u.u_flags & F_HOMEDIR) &&
20793c76c7c0Smillert 	    !(u.u_flags & F_USERNAME)) {
20803c76c7c0Smillert 		warnx("option 'm' useless without 'd' or 'l' -- ignored");
20813c76c7c0Smillert 		u.u_flags &= ~F_MKDIR;
20823c76c7c0Smillert 	}
20830e0fd575Sajacoutot 	if ((u.u_flags & F_SECGROUP) && (u.u_flags & F_SETSECGROUP))
20840e0fd575Sajacoutot 		errx(EXIT_FAILURE, "options 'G' and 'S' are mutually exclusive");
2085c742a9b2Sajacoutot 	if ((u.u_flags & F_ACCTLOCK) && (u.u_flags & F_ACCTUNLOCK))
2086c742a9b2Sajacoutot 		errx(EXIT_FAILURE, "options 'U' and 'Z' are mutually exclusive");
2087c742a9b2Sajacoutot 	if ((u.u_flags & F_PASSWORD) && (u.u_flags & (F_ACCTLOCK | F_ACCTUNLOCK)))
2088c742a9b2Sajacoutot 		errx(EXIT_FAILURE, "options 'U' or 'Z' with 'p' are mutually exclusive");
20893c76c7c0Smillert 	argc -= optind;
20903c76c7c0Smillert 	argv += optind;
20913c76c7c0Smillert 	if (argc != 1) {
2092e02380dbSjakob 		usermgmt_usage("usermod");
2093e02380dbSjakob 	}
2094e02380dbSjakob 	checkeuid();
2095f65c14a9Smillert 	openlog("usermod", LOG_PID, LOG_USER);
20963c76c7c0Smillert 	return moduser(*argv, (have_new_user) ? newuser : *argv, &u) ?
20973c76c7c0Smillert 	    EXIT_SUCCESS : EXIT_FAILURE;
2098e02380dbSjakob }
2099e02380dbSjakob 
2100e02380dbSjakob int
userdel(int argc,char ** argv)2101e02380dbSjakob userdel(int argc, char **argv)
2102e02380dbSjakob {
2103e02380dbSjakob 	struct passwd	*pwp;
2104e02380dbSjakob 	user_t		u;
2105e02380dbSjakob 	int		defaultfield;
2106e02380dbSjakob 	int		rmhome;
2107e02380dbSjakob 	int		bigD;
2108e02380dbSjakob 	int		c;
2109e02380dbSjakob 
21105520f1ccSderaadt 	memset(&u, 0, sizeof(u));
2111e02380dbSjakob 	read_defaults(&u);
2112e02380dbSjakob 	defaultfield = bigD = rmhome = 0;
21135c797a00Sajacoutot 	while ((c = getopt(argc, argv, "Dp:rv")) != -1) {
2114e02380dbSjakob 		switch(c) {
2115e02380dbSjakob 		case 'D':
2116e02380dbSjakob 			bigD = 1;
2117e02380dbSjakob 			break;
2118e02380dbSjakob 		case 'p':
2119e02380dbSjakob 			defaultfield = 1;
2120e02380dbSjakob 			u.u_preserve = (strcmp(optarg, "true") == 0) ? 1 :
2121e02380dbSjakob 					(strcmp(optarg, "yes") == 0) ? 1 :
2122944329d0Smillert 					 strtonum(optarg, INT_MIN, INT_MAX, NULL);
2123e02380dbSjakob 			break;
2124e02380dbSjakob 		case 'r':
2125e02380dbSjakob 			rmhome = 1;
2126e02380dbSjakob 			break;
2127e02380dbSjakob 		case 'v':
2128e02380dbSjakob 			verbose = 1;
2129e02380dbSjakob 			break;
2130658e04adSmillert 		default:
2131658e04adSmillert 			usermgmt_usage("userdel");
2132e02380dbSjakob 		}
2133e02380dbSjakob 	}
2134e02380dbSjakob 	if (bigD) {
2135e02380dbSjakob 		if (defaultfield) {
2136e02380dbSjakob 			checkeuid();
2137e02380dbSjakob 			return setdefaults(&u) ? EXIT_SUCCESS : EXIT_FAILURE;
2138e02380dbSjakob 		}
21395520f1ccSderaadt 		printf("preserve\t%s\n", (u.u_preserve) ? "true" : "false");
2140e02380dbSjakob 		return EXIT_SUCCESS;
2141e02380dbSjakob 	}
21423c76c7c0Smillert 	argc -= optind;
21433c76c7c0Smillert 	argv += optind;
21443c76c7c0Smillert 	if (argc != 1) {
2145e02380dbSjakob 		usermgmt_usage("userdel");
2146e02380dbSjakob 	}
2147d79ad1b7Smestre 
2148d79ad1b7Smestre 	if (pledge("stdio rpath wpath cpath fattr flock proc exec getpw id",
2149d79ad1b7Smestre 	    NULL) == -1)
2150d79ad1b7Smestre 		err(1, "pledge");
2151d79ad1b7Smestre 
2152e02380dbSjakob 	checkeuid();
21533c76c7c0Smillert 	if ((pwp = getpwnam(*argv)) == NULL) {
21543c76c7c0Smillert 		warnx("No such user `%s'", *argv);
2155e02380dbSjakob 		return EXIT_FAILURE;
2156e02380dbSjakob 	}
21573c76c7c0Smillert 	if (rmhome)
21583c76c7c0Smillert 		(void)removehomedir(pwp->pw_name, pwp->pw_uid, pwp->pw_dir);
2159e02380dbSjakob 	if (u.u_preserve) {
216048bbee4aSmillert 		u.u_flags |= F_SHELL;
2161580bd31cSmillert 		strsave(&u.u_shell, NOLOGIN);
2162580bd31cSmillert 		strsave(&u.u_password, "*");
21633c76c7c0Smillert 		u.u_flags |= F_PASSWORD;
2164f65c14a9Smillert 		openlog("userdel", LOG_PID, LOG_USER);
21653c76c7c0Smillert 		return moduser(*argv, *argv, &u) ? EXIT_SUCCESS : EXIT_FAILURE;
2166e02380dbSjakob 	}
2167be1ff426Smillert 	if (!rm_user_from_groups(*argv)) {
2168be1ff426Smillert 		return 0;
2169be1ff426Smillert 	}
2170f65c14a9Smillert 	openlog("userdel", LOG_PID, LOG_USER);
21713c76c7c0Smillert 	return moduser(*argv, *argv, NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
2172e02380dbSjakob }
2173e02380dbSjakob 
2174e02380dbSjakob /* add a group */
2175e02380dbSjakob int
groupadd(int argc,char ** argv)2176e02380dbSjakob groupadd(int argc, char **argv)
2177e02380dbSjakob {
2178e02380dbSjakob 	int	dupgid;
2179e02380dbSjakob 	int	gid;
2180e02380dbSjakob 	int	c;
2181944329d0Smillert 	const char *errstr;
2182e02380dbSjakob 
2183b4de0548Smillert 	gid = -1;
2184e02380dbSjakob 	dupgid = 0;
21855c797a00Sajacoutot 	while ((c = getopt(argc, argv, "g:ov")) != -1) {
2186e02380dbSjakob 		switch(c) {
2187e02380dbSjakob 		case 'g':
2188944329d0Smillert 			gid = strtonum(optarg, -1, GID_MAX, &errstr);
2189944329d0Smillert 			if (errstr != NULL) {
2190e02380dbSjakob 				errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
2191e02380dbSjakob 			}
2192e02380dbSjakob 			break;
2193e02380dbSjakob 		case 'o':
2194e02380dbSjakob 			dupgid = 1;
2195e02380dbSjakob 			break;
2196e02380dbSjakob 		case 'v':
2197e02380dbSjakob 			verbose = 1;
2198e02380dbSjakob 			break;
2199658e04adSmillert 		default:
2200658e04adSmillert 			usermgmt_usage("groupadd");
2201e02380dbSjakob 		}
2202e02380dbSjakob 	}
22033c76c7c0Smillert 	argc -= optind;
22043c76c7c0Smillert 	argv += optind;
22053c76c7c0Smillert 	if (argc != 1) {
2206e02380dbSjakob 		usermgmt_usage("groupadd");
2207e02380dbSjakob 	}
2208d79ad1b7Smestre 
2209d79ad1b7Smestre 	if (pledge("stdio rpath wpath cpath fattr flock getpw", NULL) == -1)
2210d79ad1b7Smestre 		err(1, "pledge");
2211d79ad1b7Smestre 
2212e02380dbSjakob 	checkeuid();
2213ce08dfceSmillert 	if (!valid_group(*argv)) {
2214ce08dfceSmillert 		errx(EXIT_FAILURE, "invalid group name `%s'", *argv);
2215ce08dfceSmillert 	}
2216e02380dbSjakob 	if (gid < 0 && !getnextgid(&gid, LowGid, HighGid)) {
2217571ba9d6Smillert 		errx(EXIT_FAILURE, "can't add group: can't get next gid");
2218e02380dbSjakob 	}
221965b77256Smillert 	if (!dupgid && gid_exists((gid_t)gid)) {
2220e02380dbSjakob 		errx(EXIT_FAILURE, "can't add group: gid %d is a duplicate", gid);
2221e02380dbSjakob 	}
2222f65c14a9Smillert 	openlog("groupadd", LOG_PID, LOG_USER);
22233c76c7c0Smillert 	if (!creategid(*argv, gid, "")) {
222448bbee4aSmillert 		errx(EXIT_FAILURE, "can't add group: problems with %s file",
22253c76c7c0Smillert 		    _PATH_GROUP);
2226e02380dbSjakob 	}
2227e02380dbSjakob 	return EXIT_SUCCESS;
2228e02380dbSjakob }
2229e02380dbSjakob 
2230e02380dbSjakob /* remove a group */
2231e02380dbSjakob int
groupdel(int argc,char ** argv)2232e02380dbSjakob groupdel(int argc, char **argv)
2233e02380dbSjakob {
2234e02380dbSjakob 	int	c;
2235e02380dbSjakob 
22365c797a00Sajacoutot 	while ((c = getopt(argc, argv, "v")) != -1) {
2237e02380dbSjakob 		switch(c) {
2238e02380dbSjakob 		case 'v':
2239e02380dbSjakob 			verbose = 1;
2240e02380dbSjakob 			break;
2241f65c14a9Smillert 		default:
2242f65c14a9Smillert 			usermgmt_usage("groupdel");
2243e02380dbSjakob 		}
2244e02380dbSjakob 	}
22453c76c7c0Smillert 	argc -= optind;
22463c76c7c0Smillert 	argv += optind;
22473c76c7c0Smillert 	if (argc != 1) {
2248e02380dbSjakob 		usermgmt_usage("groupdel");
2249e02380dbSjakob 	}
2250e02380dbSjakob 	checkeuid();
2251f65c14a9Smillert 	openlog("groupdel", LOG_PID, LOG_USER);
225265b77256Smillert 	if (!group_exists(*argv)) {
225345b96233Smillert 		warnx("No such group: `%s'", *argv);
225445b96233Smillert 		return EXIT_FAILURE;
225545b96233Smillert 	}
2256d79ad1b7Smestre 
2257d79ad1b7Smestre 	if (pledge("stdio rpath wpath cpath fattr flock", NULL) == -1)
2258d79ad1b7Smestre 		err(1, "pledge");
2259d79ad1b7Smestre 
22603c76c7c0Smillert 	if (!modify_gid(*argv, NULL)) {
2261ca33ea46Sjakob 		err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP);
2262e02380dbSjakob 	}
2263e02380dbSjakob 	return EXIT_SUCCESS;
2264e02380dbSjakob }
2265e02380dbSjakob 
2266e02380dbSjakob /* modify a group */
2267e02380dbSjakob int
groupmod(int argc,char ** argv)2268e02380dbSjakob groupmod(int argc, char **argv)
2269e02380dbSjakob {
2270e02380dbSjakob 	struct group	*grp;
2271944329d0Smillert 	const char	*errstr;
22723c76c7c0Smillert 	char		buf[LINE_MAX];
2273e02380dbSjakob 	char		*newname;
2274e02380dbSjakob 	char		**cpp;
2275e02380dbSjakob 	int		dupgid;
2276e02380dbSjakob 	int		gid;
2277e02380dbSjakob 	int		cc;
2278e02380dbSjakob 	int		c;
2279e02380dbSjakob 
2280b4de0548Smillert 	gid = -1;
2281e02380dbSjakob 	dupgid = 0;
2282e02380dbSjakob 	newname = NULL;
22835c797a00Sajacoutot 	while ((c = getopt(argc, argv, "g:n:ov")) != -1) {
2284e02380dbSjakob 		switch(c) {
2285e02380dbSjakob 		case 'g':
2286944329d0Smillert 			gid = strtonum(optarg, -1, GID_MAX, &errstr);
2287944329d0Smillert 			if (errstr != NULL) {
2288e02380dbSjakob 				errx(EXIT_FAILURE, "When using [-g gid], the gid must be numeric");
2289e02380dbSjakob 			}
2290e02380dbSjakob 			break;
2291e02380dbSjakob 		case 'o':
2292e02380dbSjakob 			dupgid = 1;
2293e02380dbSjakob 			break;
2294e02380dbSjakob 		case 'n':
2295580bd31cSmillert 			strsave(&newname, optarg);
2296e02380dbSjakob 			break;
2297e02380dbSjakob 		case 'v':
2298e02380dbSjakob 			verbose = 1;
2299e02380dbSjakob 			break;
2300658e04adSmillert 		default:
2301658e04adSmillert 			usermgmt_usage("groupmod");
2302e02380dbSjakob 		}
2303e02380dbSjakob 	}
23043c76c7c0Smillert 	argc -= optind;
23053c76c7c0Smillert 	argv += optind;
23063c76c7c0Smillert 	if (argc != 1) {
2307e02380dbSjakob 		usermgmt_usage("groupmod");
2308e02380dbSjakob 	}
2309e02380dbSjakob 	checkeuid();
2310e02380dbSjakob 	if (gid < 0 && newname == NULL) {
2311571ba9d6Smillert 		errx(EXIT_FAILURE, "Nothing to change");
2312e02380dbSjakob 	}
2313e02380dbSjakob 	if (dupgid && gid < 0) {
2314571ba9d6Smillert 		errx(EXIT_FAILURE, "Duplicate which gid?");
2315e02380dbSjakob 	}
23163c76c7c0Smillert 	if ((grp = getgrnam(*argv)) == NULL) {
2317571ba9d6Smillert 		errx(EXIT_FAILURE, "can't find group `%s' to modify", *argv);
2318e02380dbSjakob 	}
2319d79ad1b7Smestre 
2320d79ad1b7Smestre 	if (pledge("stdio rpath wpath cpath fattr flock", NULL) == -1)
2321d79ad1b7Smestre 		err(1, "pledge");
2322d79ad1b7Smestre 
2323c03f5275Sray 	if (!is_local(*argv, _PATH_GROUP)) {
2324c03f5275Sray 		errx(EXIT_FAILURE, "Group `%s' must be a local group", *argv);
2325c03f5275Sray 	}
2326e02380dbSjakob 	if (newname != NULL && !valid_group(newname)) {
2327ce08dfceSmillert 		errx(EXIT_FAILURE, "invalid group name `%s'", newname);
2328e02380dbSjakob 	}
23294f182e61Smillert 	if ((cc = snprintf(buf, sizeof(buf), "%s:%s:%u:",
23303c76c7c0Smillert 	    (newname) ? newname : grp->gr_name, grp->gr_passwd,
23314f182e61Smillert 	    (gid < 0) ? grp->gr_gid : gid)) >= sizeof(buf) || cc < 0)
2332571ba9d6Smillert 		errx(EXIT_FAILURE, "group `%s' entry too long", grp->gr_name);
23333c76c7c0Smillert 
23345b0f5f19Smillert 	for (cpp = grp->gr_mem ; *cpp ; cpp++) {
23355b0f5f19Smillert 		cc = strlcat(buf, *cpp, sizeof(buf)) + 1;
23365b0f5f19Smillert 		if (cc >= sizeof(buf))
2337571ba9d6Smillert 			errx(EXIT_FAILURE, "group `%s' entry too long",
23385b0f5f19Smillert 			    grp->gr_name);
23391cead3afSmillert 		if (cpp[1] != NULL) {
23405b0f5f19Smillert 			buf[cc - 1] = ',';
23415b0f5f19Smillert 			buf[cc] = '\0';
23425b0f5f19Smillert 		}
23431cead3afSmillert 	}
23441cead3afSmillert 	cc = strlcat(buf, "\n", sizeof(buf));
23451cead3afSmillert 	if (cc >= sizeof(buf))
2346571ba9d6Smillert 		errx(EXIT_FAILURE, "group `%s' entry too long", grp->gr_name);
23475b0f5f19Smillert 
2348f65c14a9Smillert 	openlog("groupmod", LOG_PID, LOG_USER);
23493c76c7c0Smillert 	if (!modify_gid(*argv, buf))
2350ca33ea46Sjakob 		err(EXIT_FAILURE, "can't change %s file", _PATH_GROUP);
2351e02380dbSjakob 	return EXIT_SUCCESS;
2352e02380dbSjakob }
2353e02380dbSjakob 
2354e02380dbSjakob /* display user information */
2355e02380dbSjakob int
userinfo(int argc,char ** argv)2356e02380dbSjakob userinfo(int argc, char **argv)
2357e02380dbSjakob {
2358e02380dbSjakob 	struct passwd	*pwp;
2359e02380dbSjakob 	struct group	*grp;
2360e02380dbSjakob 	char		**cpp;
2361e02380dbSjakob 	int		exists;
2362e02380dbSjakob 	int		i;
2363e02380dbSjakob 
2364e02380dbSjakob 	exists = 0;
2365e02380dbSjakob 	while ((i = getopt(argc, argv, "ev")) != -1) {
2366e02380dbSjakob 		switch(i) {
2367e02380dbSjakob 		case 'e':
2368e02380dbSjakob 			exists = 1;
2369e02380dbSjakob 			break;
2370e02380dbSjakob 		case 'v':
2371e02380dbSjakob 			verbose = 1;
2372e02380dbSjakob 			break;
2373f65c14a9Smillert 		default:
2374f65c14a9Smillert 			usermgmt_usage("userinfo");
2375e02380dbSjakob 		}
2376e02380dbSjakob 	}
23773c76c7c0Smillert 	argc -= optind;
23783c76c7c0Smillert 	argv += optind;
23793c76c7c0Smillert 	if (argc != 1) {
2380e02380dbSjakob 		usermgmt_usage("userinfo");
2381e02380dbSjakob 	}
2382d79ad1b7Smestre 
2383d79ad1b7Smestre 	if (pledge("stdio getpw", NULL) == -1)
2384d79ad1b7Smestre 		err(1, "pledge");
2385d79ad1b7Smestre 
23863c76c7c0Smillert 	pwp = find_user_info(*argv);
2387e02380dbSjakob 	if (exists) {
2388e02380dbSjakob 		exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE);
2389e02380dbSjakob 	}
23903c76c7c0Smillert 	if (pwp == NULL) {
23913c76c7c0Smillert 		errx(EXIT_FAILURE, "can't find user `%s'", *argv);
2392e02380dbSjakob 	}
23935520f1ccSderaadt 	printf("login\t%s\n", pwp->pw_name);
23945520f1ccSderaadt 	printf("passwd\t%s\n", pwp->pw_passwd);
23955520f1ccSderaadt 	printf("uid\t%u\n", pwp->pw_uid);
23963c76c7c0Smillert 	if ((grp = getgrgid(pwp->pw_gid)) == NULL)
23975520f1ccSderaadt 		printf("groups\t%u", pwp->pw_gid);
23983c76c7c0Smillert 	else
23995520f1ccSderaadt 		printf("groups\t%s", grp->gr_name);
24003c76c7c0Smillert 	while ((grp = getgrent()) != NULL) {
2401e02380dbSjakob 		for (cpp = grp->gr_mem ; *cpp ; cpp++) {
24027382253fSotto 			if (strcmp(*cpp, pwp->pw_name) == 0 &&
24037382253fSotto 			    grp->gr_gid != pwp->pw_gid)
24045520f1ccSderaadt 				printf(" %s", grp->gr_name);
2405e02380dbSjakob 		}
2406e02380dbSjakob 	}
24075520f1ccSderaadt 	fputc('\n', stdout);
24085520f1ccSderaadt 	printf("change\t%s", pwp->pw_change ? ctime(&pwp->pw_change) : "NEVER\n");
24095520f1ccSderaadt 	printf("class\t%s\n", pwp->pw_class);
24105520f1ccSderaadt 	printf("gecos\t%s\n", pwp->pw_gecos);
24115520f1ccSderaadt 	printf("dir\t%s\n", pwp->pw_dir);
24125520f1ccSderaadt 	printf("shell\t%s\n", pwp->pw_shell);
24135520f1ccSderaadt 	printf("expire\t%s", pwp->pw_expire ? ctime(&pwp->pw_expire) : "NEVER\n");
2414e02380dbSjakob 	return EXIT_SUCCESS;
2415e02380dbSjakob }
2416e02380dbSjakob 
2417e02380dbSjakob /* display user information */
2418e02380dbSjakob int
groupinfo(int argc,char ** argv)2419e02380dbSjakob groupinfo(int argc, char **argv)
2420e02380dbSjakob {
2421e02380dbSjakob 	struct group	*grp;
2422e02380dbSjakob 	char		**cpp;
2423e02380dbSjakob 	int		exists;
2424e02380dbSjakob 	int		i;
2425e02380dbSjakob 
2426e02380dbSjakob 	exists = 0;
2427e02380dbSjakob 	while ((i = getopt(argc, argv, "ev")) != -1) {
2428e02380dbSjakob 		switch(i) {
2429e02380dbSjakob 		case 'e':
2430e02380dbSjakob 			exists = 1;
2431e02380dbSjakob 			break;
2432e02380dbSjakob 		case 'v':
2433e02380dbSjakob 			verbose = 1;
2434e02380dbSjakob 			break;
2435658e04adSmillert 		default:
2436658e04adSmillert 			usermgmt_usage("groupinfo");
2437e02380dbSjakob 		}
2438e02380dbSjakob 	}
24393c76c7c0Smillert 	argc -= optind;
24403c76c7c0Smillert 	argv += optind;
24413c76c7c0Smillert 	if (argc != 1) {
2442e02380dbSjakob 		usermgmt_usage("groupinfo");
2443e02380dbSjakob 	}
2444d79ad1b7Smestre 
2445d79ad1b7Smestre 	if (pledge("stdio getpw", NULL) == -1)
2446d79ad1b7Smestre 		err(1, "pledge");
2447d79ad1b7Smestre 
24483c76c7c0Smillert 	grp = find_group_info(*argv);
2449e02380dbSjakob 	if (exists) {
2450e02380dbSjakob 		exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE);
2451e02380dbSjakob 	}
24523c76c7c0Smillert 	if (grp == NULL) {
24533c76c7c0Smillert 		errx(EXIT_FAILURE, "can't find group `%s'", *argv);
2454e02380dbSjakob 	}
24555520f1ccSderaadt 	printf("name\t%s\n", grp->gr_name);
24565520f1ccSderaadt 	printf("passwd\t%s\n", grp->gr_passwd);
24575520f1ccSderaadt 	printf("gid\t%u\n", grp->gr_gid);
24585520f1ccSderaadt 	printf("members\t");
2459e02380dbSjakob 	for (cpp = grp->gr_mem ; *cpp ; cpp++) {
24605520f1ccSderaadt 		printf("%s ", *cpp);
2461e02380dbSjakob 	}
24625520f1ccSderaadt 	fputc('\n', stdout);
2463e02380dbSjakob 	return EXIT_SUCCESS;
2464e02380dbSjakob }
2465