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