pw.c revision 69793
120253Sjoerg/*-
220302Sjoerg * Copyright (C) 1996
320302Sjoerg *	David L. Nugent.  All rights reserved.
420253Sjoerg *
520253Sjoerg * Redistribution and use in source and binary forms, with or without
620253Sjoerg * modification, are permitted provided that the following conditions
720253Sjoerg * are met:
820253Sjoerg * 1. Redistributions of source code must retain the above copyright
920302Sjoerg *    notice, this list of conditions and the following disclaimer.
1020253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1120253Sjoerg *    notice, this list of conditions and the following disclaimer in the
1220253Sjoerg *    documentation and/or other materials provided with the distribution.
1320253Sjoerg *
1420302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
1520253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1620253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1720302Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
1820253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1920253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2020253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2120253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2220253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2320253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2420253Sjoerg * SUCH DAMAGE.
2520253Sjoerg */
2620253Sjoerg
2730259Scharnier#ifndef lint
2830259Scharnierstatic const char rcsid[] =
2950479Speter  "$FreeBSD: head/usr.sbin/pw/pw.c 69793 2000-12-09 09:35:55Z obrien $";
3030259Scharnier#endif /* not lint */
3130259Scharnier
3230259Scharnier#include <err.h>
3338112Snate#include <fcntl.h>
3461957Sache#include <locale.h>
3521330Sdavidn#include <paths.h>
3621330Sdavidn#include <sys/wait.h>
3744229Sdavidn#include "pw.h"
3820253Sjoerg
3956000Sdavidn#if !defined(_PATH_YP)
4056000Sdavidn#define	_PATH_YP	"/var/yp/"
4156000Sdavidn#endif
4252512Sdavidnconst char     *Modes[] = {
4352512Sdavidn  "add", "del", "mod", "show", "next",
4452512Sdavidn  NULL};
4520253Sjoergconst char     *Which[] = {"user", "group", NULL};
4620267Sjoergstatic const char *Combo1[] = {
4720267Sjoerg  "useradd", "userdel", "usermod", "usershow", "usernext",
4852512Sdavidn  "lock", "unlock",
4920267Sjoerg  "groupadd", "groupdel", "groupmod", "groupshow", "groupnext",
5020267Sjoerg  NULL};
5120267Sjoergstatic const char *Combo2[] = {
5220267Sjoerg  "adduser", "deluser", "moduser", "showuser", "nextuser",
5352512Sdavidn  "lock", "unlock",
5420267Sjoerg  "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup",
5552512Sdavidn  NULL};
5620253Sjoerg
5744229Sdavidnstruct pwf PWF =
5844229Sdavidn{
5944229Sdavidn	0,
6044229Sdavidn	setpwent,
6144229Sdavidn	endpwent,
6244229Sdavidn	getpwent,
6344229Sdavidn	getpwuid,
6444229Sdavidn	getpwnam,
6544229Sdavidn	pwdb,
6644229Sdavidn	setgrent,
6744229Sdavidn	endgrent,
6844229Sdavidn	getgrent,
6944229Sdavidn	getgrgid,
7044229Sdavidn	getgrnam,
7144229Sdavidn	grdb
7244229Sdavidn
7344229Sdavidn};
7444229Sdavidnstruct pwf VPWF =
7544229Sdavidn{
7644229Sdavidn	1,
7744229Sdavidn	vsetpwent,
7844229Sdavidn	vendpwent,
7944229Sdavidn	vgetpwent,
8044229Sdavidn	vgetpwuid,
8144229Sdavidn	vgetpwnam,
8244229Sdavidn	vpwdb,
8344229Sdavidn	vsetgrent,
8444229Sdavidn	vendgrent,
8544229Sdavidn	vgetgrent,
8644229Sdavidn	vgetgrgid,
8744229Sdavidn	vgetgrnam,
8844229Sdavidn	vgrdb
8944229Sdavidn};
9044229Sdavidn
9120253Sjoergstatic struct cargs arglist;
9220253Sjoerg
9320253Sjoergstatic int      getindex(const char *words[], const char *word);
9420253Sjoergstatic void     cmdhelp(int mode, int which);
9520253Sjoerg
9620253Sjoerg
9720253Sjoergint
9820253Sjoergmain(int argc, char *argv[])
9920253Sjoerg{
10020253Sjoerg	int             ch;
10120253Sjoerg	int             mode = -1;
10220253Sjoerg	int             which = -1;
10344229Sdavidn	char		*config = NULL;
10420253Sjoerg	struct userconf *cnf;
10520253Sjoerg
10620253Sjoerg	static const char *opts[W_NUM][M_NUM] =
10720253Sjoerg	{
10820267Sjoerg		{ /* user */
10944231Sdavidn			"V:C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db:NPy:Y",
11044231Sdavidn			"V:C:qn:u:rY",
11144231Sdavidn			"V:C:qn:u:c:d:e:p:g:G:ml:k:s:w:L:h:FNPY",
11244386Sdavidn			"V:C:qn:u:FPa7",
11352512Sdavidn			"V:C:q",
11452512Sdavidn			"V:C:q",
11544231Sdavidn			"V:C:q"
11620267Sjoerg		},
11720267Sjoerg		{ /* grp  */
11844231Sdavidn			"V:C:qn:g:h:M:pNPY",
11944231Sdavidn			"V:C:qn:g:Y",
12044231Sdavidn			"V:C:qn:g:l:h:FM:m:NPY",
12144231Sdavidn			"V:C:qn:g:FPa",
12244231Sdavidn			"V:C:q"
12320267Sjoerg		 }
12420253Sjoerg	};
12520253Sjoerg
12620253Sjoerg	static int      (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) =
12720253Sjoerg	{			/* Request handlers */
12820253Sjoerg		pw_user,
12920253Sjoerg		pw_group
13020253Sjoerg	};
13120253Sjoerg
13220253Sjoerg	umask(0);		/* We wish to handle this manually */
13320253Sjoerg	LIST_INIT(&arglist);
13420253Sjoerg
13562034Sache	(void)setlocale(LC_ALL, "");
13661957Sache
13720253Sjoerg	/*
13820253Sjoerg	 * Break off the first couple of words to determine what exactly
13920253Sjoerg	 * we're being asked to do
14020253Sjoerg	 */
14144229Sdavidn	while (argc > 1) {
14220253Sjoerg		int             tmp;
14320253Sjoerg
14444229Sdavidn		if (*argv[1] == '-') {
14544229Sdavidn			/*
14644229Sdavidn			 * Special case, allow pw -V<dir> <operation> [args] for scripts etc.
14744229Sdavidn			 */
14844229Sdavidn			if (argv[1][1] == 'V') {
14944229Sdavidn				optarg = &argv[1][2];
15044229Sdavidn				if (*optarg == '\0') {
15144229Sdavidn					optarg = argv[2];
15244229Sdavidn					++argv;
15344229Sdavidn					--argc;
15444229Sdavidn				}
15544229Sdavidn				addarg(&arglist, 'V', optarg);
15644231Sdavidn			} else
15744231Sdavidn				break;
15844229Sdavidn		}
15961760Sdavidn		else if (mode == -1 && (tmp = getindex(Modes, argv[1])) != -1)
16020253Sjoerg			mode = tmp;
16161760Sdavidn		else if (which == -1 && (tmp = getindex(Which, argv[1])) != -1)
16220253Sjoerg			which = tmp;
16361760Sdavidn		else if ((mode == -1 && which == -1) &&
16461760Sdavidn			 ((tmp = getindex(Combo1, argv[1])) != -1 ||
16561760Sdavidn			  (tmp = getindex(Combo2, argv[1])) != -1)) {
16620253Sjoerg			which = tmp / M_NUM;
16720253Sjoerg			mode = tmp % M_NUM;
16861760Sdavidn		} else if (strcmp(argv[1], "help") == 0 && argv[2] == NULL)
16920253Sjoerg			cmdhelp(mode, which);
17044232Sdavidn		else if (which != -1 && mode != -1)
17120253Sjoerg			addarg(&arglist, 'n', argv[1]);
17220253Sjoerg		else
17330259Scharnier			errx(EX_USAGE, "unknown keyword `%s'", argv[1]);
17420253Sjoerg		++argv;
17520253Sjoerg		--argc;
17620253Sjoerg	}
17720253Sjoerg
17820253Sjoerg	/*
17920253Sjoerg	 * Bail out unless the user is specific!
18020253Sjoerg	 */
18120253Sjoerg	if (mode == -1 || which == -1)
18220253Sjoerg		cmdhelp(mode, which);
18320253Sjoerg
18420253Sjoerg	/*
18520253Sjoerg	 * We know which mode we're in and what we're about to do, so now
18620253Sjoerg	 * let's dispatch the remaining command line args in a genric way.
18720253Sjoerg	 */
18820253Sjoerg	optarg = NULL;
18920253Sjoerg
19020253Sjoerg	while ((ch = getopt(argc, argv, opts[which][mode])) != -1) {
19120253Sjoerg		if (ch == '?')
19256000Sdavidn			errx(EX_USAGE, "unknown switch");
19320253Sjoerg		else
19420253Sjoerg			addarg(&arglist, ch, optarg);
19520253Sjoerg		optarg = NULL;
19620253Sjoerg	}
19720253Sjoerg
19820253Sjoerg	/*
19920267Sjoerg	 * Must be root to attempt an update
20020267Sjoerg	 */
20127474Sdavidn	if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && getarg(&arglist, 'N')==NULL)
20230259Scharnier		errx(EX_NOPERM, "you must be root to run this program");
20320267Sjoerg
20420267Sjoerg	/*
20520253Sjoerg	 * We should immediately look for the -q 'quiet' switch so that we
20620253Sjoerg	 * don't bother with extraneous errors
20720253Sjoerg	 */
20820253Sjoerg	if (getarg(&arglist, 'q') != NULL)
20969793Sobrien		freopen(_PATH_DEVNULL, "w", stderr);
21020253Sjoerg
21120253Sjoerg	/*
21244229Sdavidn	 * Set our base working path if not overridden
21344229Sdavidn	 */
21444229Sdavidn
21544229Sdavidn	config = getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL;
21644229Sdavidn
21744229Sdavidn	if (getarg(&arglist, 'V') != NULL) {
21844229Sdavidn		char * etcpath = getarg(&arglist, 'V')->val;
21944229Sdavidn		if (*etcpath) {
22044229Sdavidn			if (config == NULL) {	/* Only override config location if -C not specified */
22144229Sdavidn				config = malloc(MAXPATHLEN);
22244229Sdavidn				snprintf(config, MAXPATHLEN, "%s/pw.conf", etcpath);
22344229Sdavidn			}
22444229Sdavidn			memcpy(&PWF, &VPWF, sizeof PWF);
22544229Sdavidn			setpwdir(etcpath);
22644229Sdavidn			setgrdir(etcpath);
22744229Sdavidn		}
22844229Sdavidn	}
22944229Sdavidn
23044229Sdavidn	/*
23120253Sjoerg	 * Now, let's do the common initialisation
23220253Sjoerg	 */
23344229Sdavidn	cnf = read_userconfig(config);
23438112Snate
23538110Snate	ch = funcs[which] (cnf, mode, &arglist);
23621330Sdavidn
23721330Sdavidn	/*
23821330Sdavidn	 * If everything went ok, and we've been asked to update
23921330Sdavidn	 * the NIS maps, then do it now
24021330Sdavidn	 */
24121330Sdavidn	if (ch == EXIT_SUCCESS && getarg(&arglist, 'Y') != NULL) {
24221330Sdavidn		pid_t	pid;
24321330Sdavidn
24421330Sdavidn		fflush(NULL);
24521330Sdavidn		if (chdir(_PATH_YP) == -1)
24630259Scharnier			warn("chdir(" _PATH_YP ")");
24721330Sdavidn		else if ((pid = fork()) == -1)
24830259Scharnier			warn("fork()");
24921330Sdavidn		else if (pid == 0) {
25021330Sdavidn			/* Is make anywhere else? */
25121330Sdavidn			execlp("/usr/bin/make", "make", NULL);
25221330Sdavidn			_exit(1);
25321330Sdavidn		} else {
25421330Sdavidn			int   i;
25521330Sdavidn			waitpid(pid, &i, 0);
25621330Sdavidn			if ((i = WEXITSTATUS(i)) != 0)
25730259Scharnier				errx(ch, "make exited with status %d", i);
25821330Sdavidn			else
25921330Sdavidn				pw_log(cnf, mode, which, "NIS maps updated");
26021330Sdavidn		}
26121330Sdavidn	}
26221330Sdavidn	return ch;
26320253Sjoerg}
26420253Sjoerg
26538112Snate
26620253Sjoergstatic int
26720253Sjoerggetindex(const char *words[], const char *word)
26820253Sjoerg{
26920253Sjoerg	int             i = 0;
27020253Sjoerg
27120253Sjoerg	while (words[i]) {
27220253Sjoerg		if (strcmp(words[i], word) == 0)
27320253Sjoerg			return i;
27420253Sjoerg		i++;
27520253Sjoerg	}
27620253Sjoerg	return -1;
27720253Sjoerg}
27820253Sjoerg
27920253Sjoerg
28020253Sjoerg/*
28120253Sjoerg * This is probably an overkill for a cmdline help system, but it reflects
28220253Sjoerg * the complexity of the command line.
28320253Sjoerg */
28420253Sjoerg
28520253Sjoergstatic void
28620253Sjoergcmdhelp(int mode, int which)
28720253Sjoerg{
28820253Sjoerg	if (which == -1)
28952512Sdavidn		fprintf(stderr, "usage:\n  pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n");
29020253Sjoerg	else if (mode == -1)
29152512Sdavidn		fprintf(stderr, "usage:\n  pw %s [add|del|mod|show|next] [help|switches/values]\n", Which[which]);
29220253Sjoerg	else {
29320253Sjoerg
29420253Sjoerg		/*
29520253Sjoerg		 * We need to give mode specific help
29620253Sjoerg		 */
29720253Sjoerg		static const char *help[W_NUM][M_NUM] =
29820253Sjoerg		{
29920253Sjoerg			{
30030259Scharnier				"usage: pw useradd [name] [switches]\n"
30144229Sdavidn				"\t-V etcdir      alternate /etc location\n"
30220253Sjoerg				"\t-C config      configuration file\n"
30320253Sjoerg				"\t-q             quiet operation\n"
30420253Sjoerg				"  Adding users:\n"
30520253Sjoerg				"\t-n name        login name\n"
30620253Sjoerg				"\t-u uid         user id\n"
30720253Sjoerg				"\t-c comment     user name/comment\n"
30820253Sjoerg				"\t-d directory   home directory\n"
30920253Sjoerg				"\t-e date        account expiry date\n"
31020253Sjoerg				"\t-p date        password expiry date\n"
31120253Sjoerg				"\t-g grp         initial group\n"
31220253Sjoerg				"\t-G grp1,grp2   additional groups\n"
31320253Sjoerg				"\t-m [ -k dir ]  create and set up home\n"
31420253Sjoerg				"\t-s shell       name of login shell\n"
31520253Sjoerg				"\t-o             duplicate uid ok\n"
31620253Sjoerg				"\t-L class       user class\n"
31720253Sjoerg				"\t-h fd          read password on fd\n"
31821330Sdavidn				"\t-Y             update NIS maps\n"
31920267Sjoerg				"\t-N             no update\n"
32020253Sjoerg				"  Setting defaults:\n"
32144229Sdavidn				"\t-V etcdir      alternate /etc location\n"
32244229Sdavidn			        "\t-D             set user defaults\n"
32320253Sjoerg				"\t-b dir         default home root dir\n"
32420253Sjoerg				"\t-e period      default expiry period\n"
32520253Sjoerg				"\t-p period      default password change period\n"
32620253Sjoerg				"\t-g group       default group\n"
32720253Sjoerg				"\t-G grp1,grp2   additional groups\n"
32820253Sjoerg				"\t-L class       default user class\n"
32920253Sjoerg				"\t-k dir         default home skeleton\n"
33020253Sjoerg				"\t-u min,max     set min,max uids\n"
33120253Sjoerg				"\t-i min,max     set min,max gids\n"
33220253Sjoerg				"\t-w method      set default password method\n"
33321330Sdavidn				"\t-s shell       default shell\n"
33421330Sdavidn				"\t-y path        set NIS passwd file path\n",
33530259Scharnier				"usage: pw userdel [uid|name] [switches]\n"
33644229Sdavidn				"\t-V etcdir      alternate /etc location\n"
33720253Sjoerg				"\t-n name        login name\n"
33820253Sjoerg				"\t-u uid         user id\n"
33921330Sdavidn				"\t-Y             update NIS maps\n"
34020253Sjoerg				"\t-r             remove home & contents\n",
34130259Scharnier				"usage: pw usermod [uid|name] [switches]\n"
34244229Sdavidn				"\t-V etcdir      alternate /etc location\n"
34320253Sjoerg				"\t-C config      configuration file\n"
34420253Sjoerg				"\t-q             quiet operation\n"
34520253Sjoerg				"\t-F             force add if no user\n"
34620253Sjoerg				"\t-n name        login name\n"
34720253Sjoerg				"\t-u uid         user id\n"
34820253Sjoerg				"\t-c comment     user name/comment\n"
34920253Sjoerg				"\t-d directory   home directory\n"
35020253Sjoerg				"\t-e date        account expiry date\n"
35120253Sjoerg				"\t-p date        password expiry date\n"
35220253Sjoerg				"\t-g grp         initial group\n"
35320253Sjoerg				"\t-G grp1,grp2   additional groups\n"
35420253Sjoerg				"\t-l name        new login name\n"
35520253Sjoerg				"\t-L class       user class\n"
35620253Sjoerg				"\t-m [ -k dir ]  create and set up home\n"
35720253Sjoerg				"\t-s shell       name of login shell\n"
35820267Sjoerg				"\t-w method      set new password using method\n"
35920267Sjoerg				"\t-h fd          read password on fd\n"
36021330Sdavidn				"\t-Y             update NIS maps\n"
36120267Sjoerg				"\t-N             no update\n",
36230259Scharnier				"usage: pw usershow [uid|name] [switches]\n"
36344229Sdavidn				"\t-V etcdir      alternate /etc location\n"
36420253Sjoerg				"\t-n name        login name\n"
36520253Sjoerg				"\t-u uid         user id\n"
36620253Sjoerg				"\t-F             force print\n"
36720267Sjoerg				"\t-P             prettier format\n"
36844386Sdavidn				"\t-a             print all users\n"
36944386Sdavidn				"\t-7             print in v7 format\n",
37030259Scharnier				"usage: pw usernext [switches]\n"
37144229Sdavidn				"\t-V etcdir      alternate /etc location\n"
37220267Sjoerg				"\t-C config      configuration file\n"
37320253Sjoerg			},
37420253Sjoerg			{
37530259Scharnier				"usage: pw groupadd [group|gid] [switches]\n"
37644229Sdavidn				"\t-V etcdir      alternate /etc location\n"
37720253Sjoerg				"\t-C config      configuration file\n"
37820253Sjoerg				"\t-q             quiet operation\n"
37920253Sjoerg				"\t-n group       group name\n"
38020253Sjoerg				"\t-g gid         group id\n"
38120267Sjoerg				"\t-M usr1,usr2   add users as group members\n"
38220267Sjoerg				"\t-o             duplicate gid ok\n"
38321330Sdavidn				"\t-Y             update NIS maps\n"
38420267Sjoerg				"\t-N             no update\n",
38530259Scharnier				"usage: pw groupdel [group|gid] [switches]\n"
38644229Sdavidn				"\t-V etcdir      alternate /etc location\n"
38720253Sjoerg				"\t-n name        group name\n"
38821330Sdavidn				"\t-g gid         group id\n"
38921330Sdavidn				"\t-Y             update NIS maps\n",
39030259Scharnier				"usage: pw groupmod [group|gid] [switches]\n"
39144229Sdavidn				"\t-V etcdir      alternate /etc location\n"
39220253Sjoerg				"\t-C config      configuration file\n"
39320253Sjoerg				"\t-q             quiet operation\n"
39420253Sjoerg				"\t-F             force add if not exists\n"
39520253Sjoerg				"\t-n name        group name\n"
39620253Sjoerg				"\t-g gid         group id\n"
39720267Sjoerg				"\t-M usr1,usr2   replaces users as group members\n"
39820267Sjoerg				"\t-m usr1,usr2   add users as group members\n"
39920267Sjoerg				"\t-l name        new group name\n"
40021330Sdavidn				"\t-Y             update NIS maps\n"
40120267Sjoerg				"\t-N             no update\n",
40230259Scharnier				"usage: pw groupshow [group|gid] [switches]\n"
40344229Sdavidn				"\t-V etcdir      alternate /etc location\n"
40420253Sjoerg				"\t-n name        group name\n"
40520253Sjoerg				"\t-g gid         group id\n"
40620253Sjoerg				"\t-F             force print\n"
40720267Sjoerg				"\t-P             prettier format\n"
40820267Sjoerg				"\t-a             print all accounting groups\n",
40930259Scharnier				"usage: pw groupnext [switches]\n"
41044229Sdavidn				"\t-V etcdir      alternate /etc location\n"
41120267Sjoerg				"\t-C config      configuration file\n"
41220253Sjoerg			}
41320253Sjoerg		};
41420253Sjoerg
41530259Scharnier		fprintf(stderr, help[which][mode]);
41620253Sjoerg	}
41720267Sjoerg	exit(EXIT_FAILURE);
41820253Sjoerg}
41920253Sjoerg
42020253Sjoergstruct carg    *
42120253Sjoerggetarg(struct cargs * _args, int ch)
42220253Sjoerg{
42320253Sjoerg	struct carg    *c = _args->lh_first;
42420253Sjoerg
42520253Sjoerg	while (c != NULL && c->ch != ch)
42620253Sjoerg		c = c->list.le_next;
42720253Sjoerg	return c;
42820253Sjoerg}
42920253Sjoerg
43020253Sjoergstruct carg    *
43120253Sjoergaddarg(struct cargs * _args, int ch, char *argstr)
43220253Sjoerg{
43320253Sjoerg	struct carg    *ca = malloc(sizeof(struct carg));
43420253Sjoerg
43520253Sjoerg	if (ca == NULL)
43630259Scharnier		errx(EX_OSERR, "out of memory");
43720253Sjoerg	ca->ch = ch;
43820253Sjoerg	ca->val = argstr;
43920253Sjoerg	LIST_INSERT_HEAD(_args, ca, list);
44020253Sjoerg	return ca;
44120253Sjoerg}
442