pw.c revision 50479
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 50479 1999-08-28 01:35:59Z peter $";
3030259Scharnier#endif /* not lint */
3130259Scharnier
3230259Scharnier#include <err.h>
3338112Snate#include <fcntl.h>
3421330Sdavidn#include <paths.h>
3521330Sdavidn#include <sys/wait.h>
3644229Sdavidn#include "pw.h"
3720253Sjoerg
3820267Sjoergconst char     *Modes[] = {"add", "del", "mod", "show", "next", NULL};
3920253Sjoergconst char     *Which[] = {"user", "group", NULL};
4020267Sjoergstatic const char *Combo1[] = {
4120267Sjoerg  "useradd", "userdel", "usermod", "usershow", "usernext",
4220267Sjoerg  "groupadd", "groupdel", "groupmod", "groupshow", "groupnext",
4320267Sjoerg  NULL};
4420267Sjoergstatic const char *Combo2[] = {
4520267Sjoerg  "adduser", "deluser", "moduser", "showuser", "nextuser",
4620267Sjoerg  "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup",
4720253SjoergNULL};
4820253Sjoerg
4944229Sdavidnstruct pwf PWF =
5044229Sdavidn{
5144229Sdavidn	0,
5244229Sdavidn	setpwent,
5344229Sdavidn	endpwent,
5444229Sdavidn	getpwent,
5544229Sdavidn	getpwuid,
5644229Sdavidn	getpwnam,
5744229Sdavidn	pwdb,
5844229Sdavidn	setgrent,
5944229Sdavidn	endgrent,
6044229Sdavidn	getgrent,
6144229Sdavidn	getgrgid,
6244229Sdavidn	getgrnam,
6344229Sdavidn	grdb
6444229Sdavidn
6544229Sdavidn};
6644229Sdavidnstruct pwf VPWF =
6744229Sdavidn{
6844229Sdavidn	1,
6944229Sdavidn	vsetpwent,
7044229Sdavidn	vendpwent,
7144229Sdavidn	vgetpwent,
7244229Sdavidn	vgetpwuid,
7344229Sdavidn	vgetpwnam,
7444229Sdavidn	vpwdb,
7544229Sdavidn	vsetgrent,
7644229Sdavidn	vendgrent,
7744229Sdavidn	vgetgrent,
7844229Sdavidn	vgetgrgid,
7944229Sdavidn	vgetgrnam,
8044229Sdavidn	vgrdb
8144229Sdavidn};
8244229Sdavidn
8320253Sjoergstatic struct cargs arglist;
8420253Sjoerg
8520253Sjoergstatic int      getindex(const char *words[], const char *word);
8620253Sjoergstatic void     cmdhelp(int mode, int which);
8720253Sjoerg
8820253Sjoerg
8920253Sjoergint
9020253Sjoergmain(int argc, char *argv[])
9120253Sjoerg{
9220253Sjoerg	int             ch;
9320253Sjoerg	int             mode = -1;
9420253Sjoerg	int             which = -1;
9544229Sdavidn	char		*config = NULL;
9620253Sjoerg	struct userconf *cnf;
9720253Sjoerg
9820253Sjoerg	static const char *opts[W_NUM][M_NUM] =
9920253Sjoerg	{
10020267Sjoerg		{ /* user */
10144231Sdavidn			"V:C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db:NPy:Y",
10244231Sdavidn			"V:C:qn:u:rY",
10344231Sdavidn			"V:C:qn:u:c:d:e:p:g:G:ml:k:s:w:L:h:FNPY",
10444386Sdavidn			"V:C:qn:u:FPa7",
10544231Sdavidn			"V:C:q"
10620267Sjoerg		},
10720267Sjoerg		{ /* grp  */
10844231Sdavidn			"V:C:qn:g:h:M:pNPY",
10944231Sdavidn			"V:C:qn:g:Y",
11044231Sdavidn			"V:C:qn:g:l:h:FM:m:NPY",
11144231Sdavidn			"V:C:qn:g:FPa",
11244231Sdavidn			"V:C:q"
11320267Sjoerg		 }
11420253Sjoerg	};
11520253Sjoerg
11620253Sjoerg	static int      (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) =
11720253Sjoerg	{			/* Request handlers */
11820253Sjoerg		pw_user,
11920253Sjoerg		pw_group
12020253Sjoerg	};
12120253Sjoerg
12220253Sjoerg	umask(0);		/* We wish to handle this manually */
12320253Sjoerg	LIST_INIT(&arglist);
12420253Sjoerg
12520253Sjoerg	/*
12620253Sjoerg	 * Break off the first couple of words to determine what exactly
12720253Sjoerg	 * we're being asked to do
12820253Sjoerg	 */
12944229Sdavidn	while (argc > 1) {
13020253Sjoerg		int             tmp;
13120253Sjoerg
13244229Sdavidn		if (*argv[1] == '-') {
13344229Sdavidn			/*
13444229Sdavidn			 * Special case, allow pw -V<dir> <operation> [args] for scripts etc.
13544229Sdavidn			 */
13644229Sdavidn			if (argv[1][1] == 'V') {
13744229Sdavidn				optarg = &argv[1][2];
13844229Sdavidn				if (*optarg == '\0') {
13944229Sdavidn					optarg = argv[2];
14044229Sdavidn					++argv;
14144229Sdavidn					--argc;
14244229Sdavidn				}
14344229Sdavidn				addarg(&arglist, 'V', optarg);
14444231Sdavidn			} else
14544231Sdavidn				break;
14644229Sdavidn		}
14744229Sdavidn		else if ((tmp = getindex(Modes, argv[1])) != -1)
14820253Sjoerg			mode = tmp;
14920253Sjoerg		else if ((tmp = getindex(Which, argv[1])) != -1)
15020253Sjoerg			which = tmp;
15120253Sjoerg		else if ((tmp = getindex(Combo1, argv[1])) != -1 || (tmp = getindex(Combo2, argv[1])) != -1) {
15220253Sjoerg			which = tmp / M_NUM;
15320253Sjoerg			mode = tmp % M_NUM;
15420253Sjoerg		} else if (strcmp(argv[1], "help") == 0)
15520253Sjoerg			cmdhelp(mode, which);
15644232Sdavidn		else if (which != -1 && mode != -1)
15720253Sjoerg			addarg(&arglist, 'n', argv[1]);
15820253Sjoerg		else
15930259Scharnier			errx(EX_USAGE, "unknown keyword `%s'", argv[1]);
16020253Sjoerg		++argv;
16120253Sjoerg		--argc;
16220253Sjoerg	}
16320253Sjoerg
16420253Sjoerg	/*
16520253Sjoerg	 * Bail out unless the user is specific!
16620253Sjoerg	 */
16720253Sjoerg	if (mode == -1 || which == -1)
16820253Sjoerg		cmdhelp(mode, which);
16920253Sjoerg
17020253Sjoerg	/*
17120253Sjoerg	 * We know which mode we're in and what we're about to do, so now
17220253Sjoerg	 * let's dispatch the remaining command line args in a genric way.
17320253Sjoerg	 */
17420253Sjoerg	optarg = NULL;
17520253Sjoerg
17620253Sjoerg	while ((ch = getopt(argc, argv, opts[which][mode])) != -1) {
17720253Sjoerg		if (ch == '?')
17830259Scharnier			errx(EX_USAGE, NULL);
17920253Sjoerg		else
18020253Sjoerg			addarg(&arglist, ch, optarg);
18120253Sjoerg		optarg = NULL;
18220253Sjoerg	}
18320253Sjoerg
18420253Sjoerg	/*
18520267Sjoerg	 * Must be root to attempt an update
18620267Sjoerg	 */
18727474Sdavidn	if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && getarg(&arglist, 'N')==NULL)
18830259Scharnier		errx(EX_NOPERM, "you must be root to run this program");
18920267Sjoerg
19020267Sjoerg	/*
19120253Sjoerg	 * We should immediately look for the -q 'quiet' switch so that we
19220253Sjoerg	 * don't bother with extraneous errors
19320253Sjoerg	 */
19420253Sjoerg	if (getarg(&arglist, 'q') != NULL)
19520253Sjoerg		freopen("/dev/null", "w", stderr);
19620253Sjoerg
19720253Sjoerg	/*
19844229Sdavidn	 * Set our base working path if not overridden
19944229Sdavidn	 */
20044229Sdavidn
20144229Sdavidn	config = getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL;
20244229Sdavidn
20344229Sdavidn	if (getarg(&arglist, 'V') != NULL) {
20444229Sdavidn		char * etcpath = getarg(&arglist, 'V')->val;
20544229Sdavidn		if (*etcpath) {
20644229Sdavidn			if (config == NULL) {	/* Only override config location if -C not specified */
20744229Sdavidn				config = malloc(MAXPATHLEN);
20844229Sdavidn				snprintf(config, MAXPATHLEN, "%s/pw.conf", etcpath);
20944229Sdavidn			}
21044229Sdavidn			memcpy(&PWF, &VPWF, sizeof PWF);
21144229Sdavidn			setpwdir(etcpath);
21244229Sdavidn			setgrdir(etcpath);
21344229Sdavidn		}
21444229Sdavidn	}
21544229Sdavidn
21644229Sdavidn	/*
21720253Sjoerg	 * Now, let's do the common initialisation
21820253Sjoerg	 */
21944229Sdavidn	cnf = read_userconfig(config);
22038112Snate
22138110Snate	ch = funcs[which] (cnf, mode, &arglist);
22221330Sdavidn
22321330Sdavidn	/*
22421330Sdavidn	 * If everything went ok, and we've been asked to update
22521330Sdavidn	 * the NIS maps, then do it now
22621330Sdavidn	 */
22721330Sdavidn	if (ch == EXIT_SUCCESS && getarg(&arglist, 'Y') != NULL) {
22821330Sdavidn		pid_t	pid;
22921330Sdavidn
23021330Sdavidn		fflush(NULL);
23121330Sdavidn		if (chdir(_PATH_YP) == -1)
23230259Scharnier			warn("chdir(" _PATH_YP ")");
23321330Sdavidn		else if ((pid = fork()) == -1)
23430259Scharnier			warn("fork()");
23521330Sdavidn		else if (pid == 0) {
23621330Sdavidn			/* Is make anywhere else? */
23721330Sdavidn			execlp("/usr/bin/make", "make", NULL);
23821330Sdavidn			_exit(1);
23921330Sdavidn		} else {
24021330Sdavidn			int   i;
24121330Sdavidn			waitpid(pid, &i, 0);
24221330Sdavidn			if ((i = WEXITSTATUS(i)) != 0)
24330259Scharnier				errx(ch, "make exited with status %d", i);
24421330Sdavidn			else
24521330Sdavidn				pw_log(cnf, mode, which, "NIS maps updated");
24621330Sdavidn		}
24721330Sdavidn	}
24821330Sdavidn	return ch;
24920253Sjoerg}
25020253Sjoerg
25138112Snate
25220253Sjoergstatic int
25320253Sjoerggetindex(const char *words[], const char *word)
25420253Sjoerg{
25520253Sjoerg	int             i = 0;
25620253Sjoerg
25720253Sjoerg	while (words[i]) {
25820253Sjoerg		if (strcmp(words[i], word) == 0)
25920253Sjoerg			return i;
26020253Sjoerg		i++;
26120253Sjoerg	}
26220253Sjoerg	return -1;
26320253Sjoerg}
26420253Sjoerg
26520253Sjoerg
26620253Sjoerg/*
26720253Sjoerg * This is probably an overkill for a cmdline help system, but it reflects
26820253Sjoerg * the complexity of the command line.
26920253Sjoerg */
27020253Sjoerg
27120253Sjoergstatic void
27220253Sjoergcmdhelp(int mode, int which)
27320253Sjoerg{
27420253Sjoerg	if (which == -1)
27530259Scharnier		fprintf(stderr, "usage: pw [user|group] [add|del|mod|show|next] [ help | switches/values ]\n");
27620253Sjoerg	else if (mode == -1)
27730259Scharnier		fprintf(stderr, "usage: pw %s [add|del|mod|show|next] [ help | switches/values ]\n", Which[which]);
27820253Sjoerg	else {
27920253Sjoerg
28020253Sjoerg		/*
28120253Sjoerg		 * We need to give mode specific help
28220253Sjoerg		 */
28320253Sjoerg		static const char *help[W_NUM][M_NUM] =
28420253Sjoerg		{
28520253Sjoerg			{
28630259Scharnier				"usage: pw useradd [name] [switches]\n"
28744229Sdavidn				"\t-V etcdir      alternate /etc location\n"
28820253Sjoerg				"\t-C config      configuration file\n"
28920253Sjoerg				"\t-q             quiet operation\n"
29020253Sjoerg				"  Adding users:\n"
29120253Sjoerg				"\t-n name        login name\n"
29220253Sjoerg				"\t-u uid         user id\n"
29320253Sjoerg				"\t-c comment     user name/comment\n"
29420253Sjoerg				"\t-d directory   home directory\n"
29520253Sjoerg				"\t-e date        account expiry date\n"
29620253Sjoerg				"\t-p date        password expiry date\n"
29720253Sjoerg				"\t-g grp         initial group\n"
29820253Sjoerg				"\t-G grp1,grp2   additional groups\n"
29920253Sjoerg				"\t-m [ -k dir ]  create and set up home\n"
30020253Sjoerg				"\t-s shell       name of login shell\n"
30120253Sjoerg				"\t-o             duplicate uid ok\n"
30220253Sjoerg				"\t-L class       user class\n"
30320253Sjoerg				"\t-h fd          read password on fd\n"
30421330Sdavidn				"\t-Y             update NIS maps\n"
30520267Sjoerg				"\t-N             no update\n"
30620253Sjoerg				"  Setting defaults:\n"
30744229Sdavidn				"\t-V etcdir      alternate /etc location\n"
30844229Sdavidn			        "\t-D             set user defaults\n"
30920253Sjoerg				"\t-b dir         default home root dir\n"
31020253Sjoerg				"\t-e period      default expiry period\n"
31120253Sjoerg				"\t-p period      default password change period\n"
31220253Sjoerg				"\t-g group       default group\n"
31320253Sjoerg				"\t-G grp1,grp2   additional groups\n"
31420253Sjoerg				"\t-L class       default user class\n"
31520253Sjoerg				"\t-k dir         default home skeleton\n"
31620253Sjoerg				"\t-u min,max     set min,max uids\n"
31720253Sjoerg				"\t-i min,max     set min,max gids\n"
31820253Sjoerg				"\t-w method      set default password method\n"
31921330Sdavidn				"\t-s shell       default shell\n"
32021330Sdavidn				"\t-y path        set NIS passwd file path\n",
32130259Scharnier				"usage: pw userdel [uid|name] [switches]\n"
32244229Sdavidn				"\t-V etcdir      alternate /etc location\n"
32320253Sjoerg				"\t-n name        login name\n"
32420253Sjoerg				"\t-u uid         user id\n"
32521330Sdavidn				"\t-Y             update NIS maps\n"
32620253Sjoerg				"\t-r             remove home & contents\n",
32730259Scharnier				"usage: pw usermod [uid|name] [switches]\n"
32844229Sdavidn				"\t-V etcdir      alternate /etc location\n"
32920253Sjoerg				"\t-C config      configuration file\n"
33020253Sjoerg				"\t-q             quiet operation\n"
33120253Sjoerg				"\t-F             force add if no user\n"
33220253Sjoerg				"\t-n name        login name\n"
33320253Sjoerg				"\t-u uid         user id\n"
33420253Sjoerg				"\t-c comment     user name/comment\n"
33520253Sjoerg				"\t-d directory   home directory\n"
33620253Sjoerg				"\t-e date        account expiry date\n"
33720253Sjoerg				"\t-p date        password expiry date\n"
33820253Sjoerg				"\t-g grp         initial group\n"
33920253Sjoerg				"\t-G grp1,grp2   additional groups\n"
34020253Sjoerg				"\t-l name        new login name\n"
34120253Sjoerg				"\t-L class       user class\n"
34220253Sjoerg				"\t-m [ -k dir ]  create and set up home\n"
34320253Sjoerg				"\t-s shell       name of login shell\n"
34420267Sjoerg				"\t-w method      set new password using method\n"
34520267Sjoerg				"\t-h fd          read password on fd\n"
34621330Sdavidn				"\t-Y             update NIS maps\n"
34720267Sjoerg				"\t-N             no update\n",
34830259Scharnier				"usage: pw usershow [uid|name] [switches]\n"
34944229Sdavidn				"\t-V etcdir      alternate /etc location\n"
35020253Sjoerg				"\t-n name        login name\n"
35120253Sjoerg				"\t-u uid         user id\n"
35220253Sjoerg				"\t-F             force print\n"
35320267Sjoerg				"\t-P             prettier format\n"
35444386Sdavidn				"\t-a             print all users\n"
35544386Sdavidn				"\t-7             print in v7 format\n",
35630259Scharnier				"usage: pw usernext [switches]\n"
35744229Sdavidn				"\t-V etcdir      alternate /etc location\n"
35820267Sjoerg				"\t-C config      configuration file\n"
35920253Sjoerg			},
36020253Sjoerg			{
36130259Scharnier				"usage: pw groupadd [group|gid] [switches]\n"
36244229Sdavidn				"\t-V etcdir      alternate /etc location\n"
36320253Sjoerg				"\t-C config      configuration file\n"
36420253Sjoerg				"\t-q             quiet operation\n"
36520253Sjoerg				"\t-n group       group name\n"
36620253Sjoerg				"\t-g gid         group id\n"
36720267Sjoerg				"\t-M usr1,usr2   add users as group members\n"
36820267Sjoerg				"\t-o             duplicate gid ok\n"
36921330Sdavidn				"\t-Y             update NIS maps\n"
37020267Sjoerg				"\t-N             no update\n",
37130259Scharnier				"usage: pw groupdel [group|gid] [switches]\n"
37244229Sdavidn				"\t-V etcdir      alternate /etc location\n"
37320253Sjoerg				"\t-n name        group name\n"
37421330Sdavidn				"\t-g gid         group id\n"
37521330Sdavidn				"\t-Y             update NIS maps\n",
37630259Scharnier				"usage: pw groupmod [group|gid] [switches]\n"
37744229Sdavidn				"\t-V etcdir      alternate /etc location\n"
37820253Sjoerg				"\t-C config      configuration file\n"
37920253Sjoerg				"\t-q             quiet operation\n"
38020253Sjoerg				"\t-F             force add if not exists\n"
38120253Sjoerg				"\t-n name        group name\n"
38220253Sjoerg				"\t-g gid         group id\n"
38320267Sjoerg				"\t-M usr1,usr2   replaces users as group members\n"
38420267Sjoerg				"\t-m usr1,usr2   add users as group members\n"
38520267Sjoerg				"\t-l name        new group name\n"
38621330Sdavidn				"\t-Y             update NIS maps\n"
38720267Sjoerg				"\t-N             no update\n",
38830259Scharnier				"usage: pw groupshow [group|gid] [switches]\n"
38944229Sdavidn				"\t-V etcdir      alternate /etc location\n"
39020253Sjoerg				"\t-n name        group name\n"
39120253Sjoerg				"\t-g gid         group id\n"
39220253Sjoerg				"\t-F             force print\n"
39320267Sjoerg				"\t-P             prettier format\n"
39420267Sjoerg				"\t-a             print all accounting groups\n",
39530259Scharnier				"usage: pw groupnext [switches]\n"
39644229Sdavidn				"\t-V etcdir      alternate /etc location\n"
39720267Sjoerg				"\t-C config      configuration file\n"
39820253Sjoerg			}
39920253Sjoerg		};
40020253Sjoerg
40130259Scharnier		fprintf(stderr, help[which][mode]);
40220253Sjoerg	}
40320267Sjoerg	exit(EXIT_FAILURE);
40420253Sjoerg}
40520253Sjoerg
40620253Sjoergstruct carg    *
40720253Sjoerggetarg(struct cargs * _args, int ch)
40820253Sjoerg{
40920253Sjoerg	struct carg    *c = _args->lh_first;
41020253Sjoerg
41120253Sjoerg	while (c != NULL && c->ch != ch)
41220253Sjoerg		c = c->list.le_next;
41320253Sjoerg	return c;
41420253Sjoerg}
41520253Sjoerg
41620253Sjoergstruct carg    *
41720253Sjoergaddarg(struct cargs * _args, int ch, char *argstr)
41820253Sjoerg{
41920253Sjoerg	struct carg    *ca = malloc(sizeof(struct carg));
42020253Sjoerg
42120253Sjoerg	if (ca == NULL)
42230259Scharnier		errx(EX_OSERR, "out of memory");
42320253Sjoerg	ca->ch = ch;
42420253Sjoerg	ca->val = argstr;
42520253Sjoerg	LIST_INSERT_HEAD(_args, ca, list);
42620253Sjoerg	return ca;
42720253Sjoerg}
428