pw.c revision 56000
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 56000 2000-01-15 00:20:22Z davidn $";
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
3856000Sdavidn#if !defined(_PATH_YP)
3956000Sdavidn#define	_PATH_YP	"/var/yp/"
4056000Sdavidn#endif
4152512Sdavidnconst char     *Modes[] = {
4252512Sdavidn  "add", "del", "mod", "show", "next",
4352512Sdavidn  NULL};
4420253Sjoergconst char     *Which[] = {"user", "group", NULL};
4520267Sjoergstatic const char *Combo1[] = {
4620267Sjoerg  "useradd", "userdel", "usermod", "usershow", "usernext",
4752512Sdavidn  "lock", "unlock",
4820267Sjoerg  "groupadd", "groupdel", "groupmod", "groupshow", "groupnext",
4920267Sjoerg  NULL};
5020267Sjoergstatic const char *Combo2[] = {
5120267Sjoerg  "adduser", "deluser", "moduser", "showuser", "nextuser",
5252512Sdavidn  "lock", "unlock",
5320267Sjoerg  "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup",
5452512Sdavidn  NULL};
5520253Sjoerg
5644229Sdavidnstruct pwf PWF =
5744229Sdavidn{
5844229Sdavidn	0,
5944229Sdavidn	setpwent,
6044229Sdavidn	endpwent,
6144229Sdavidn	getpwent,
6244229Sdavidn	getpwuid,
6344229Sdavidn	getpwnam,
6444229Sdavidn	pwdb,
6544229Sdavidn	setgrent,
6644229Sdavidn	endgrent,
6744229Sdavidn	getgrent,
6844229Sdavidn	getgrgid,
6944229Sdavidn	getgrnam,
7044229Sdavidn	grdb
7144229Sdavidn
7244229Sdavidn};
7344229Sdavidnstruct pwf VPWF =
7444229Sdavidn{
7544229Sdavidn	1,
7644229Sdavidn	vsetpwent,
7744229Sdavidn	vendpwent,
7844229Sdavidn	vgetpwent,
7944229Sdavidn	vgetpwuid,
8044229Sdavidn	vgetpwnam,
8144229Sdavidn	vpwdb,
8244229Sdavidn	vsetgrent,
8344229Sdavidn	vendgrent,
8444229Sdavidn	vgetgrent,
8544229Sdavidn	vgetgrgid,
8644229Sdavidn	vgetgrnam,
8744229Sdavidn	vgrdb
8844229Sdavidn};
8944229Sdavidn
9020253Sjoergstatic struct cargs arglist;
9120253Sjoerg
9220253Sjoergstatic int      getindex(const char *words[], const char *word);
9320253Sjoergstatic void     cmdhelp(int mode, int which);
9420253Sjoerg
9520253Sjoerg
9620253Sjoergint
9720253Sjoergmain(int argc, char *argv[])
9820253Sjoerg{
9920253Sjoerg	int             ch;
10020253Sjoerg	int             mode = -1;
10120253Sjoerg	int             which = -1;
10244229Sdavidn	char		*config = NULL;
10320253Sjoerg	struct userconf *cnf;
10420253Sjoerg
10520253Sjoerg	static const char *opts[W_NUM][M_NUM] =
10620253Sjoerg	{
10720267Sjoerg		{ /* user */
10844231Sdavidn			"V:C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db:NPy:Y",
10944231Sdavidn			"V:C:qn:u:rY",
11044231Sdavidn			"V:C:qn:u:c:d:e:p:g:G:ml:k:s:w:L:h:FNPY",
11144386Sdavidn			"V:C:qn:u:FPa7",
11252512Sdavidn			"V:C:q",
11352512Sdavidn			"V:C:q",
11444231Sdavidn			"V:C:q"
11520267Sjoerg		},
11620267Sjoerg		{ /* grp  */
11744231Sdavidn			"V:C:qn:g:h:M:pNPY",
11844231Sdavidn			"V:C:qn:g:Y",
11944231Sdavidn			"V:C:qn:g:l:h:FM:m:NPY",
12044231Sdavidn			"V:C:qn:g:FPa",
12144231Sdavidn			"V:C:q"
12220267Sjoerg		 }
12320253Sjoerg	};
12420253Sjoerg
12520253Sjoerg	static int      (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) =
12620253Sjoerg	{			/* Request handlers */
12720253Sjoerg		pw_user,
12820253Sjoerg		pw_group
12920253Sjoerg	};
13020253Sjoerg
13120253Sjoerg	umask(0);		/* We wish to handle this manually */
13220253Sjoerg	LIST_INIT(&arglist);
13320253Sjoerg
13420253Sjoerg	/*
13520253Sjoerg	 * Break off the first couple of words to determine what exactly
13620253Sjoerg	 * we're being asked to do
13720253Sjoerg	 */
13844229Sdavidn	while (argc > 1) {
13920253Sjoerg		int             tmp;
14020253Sjoerg
14144229Sdavidn		if (*argv[1] == '-') {
14244229Sdavidn			/*
14344229Sdavidn			 * Special case, allow pw -V<dir> <operation> [args] for scripts etc.
14444229Sdavidn			 */
14544229Sdavidn			if (argv[1][1] == 'V') {
14644229Sdavidn				optarg = &argv[1][2];
14744229Sdavidn				if (*optarg == '\0') {
14844229Sdavidn					optarg = argv[2];
14944229Sdavidn					++argv;
15044229Sdavidn					--argc;
15144229Sdavidn				}
15244229Sdavidn				addarg(&arglist, 'V', optarg);
15344231Sdavidn			} else
15444231Sdavidn				break;
15544229Sdavidn		}
15644229Sdavidn		else if ((tmp = getindex(Modes, argv[1])) != -1)
15720253Sjoerg			mode = tmp;
15820253Sjoerg		else if ((tmp = getindex(Which, argv[1])) != -1)
15920253Sjoerg			which = tmp;
16020253Sjoerg		else if ((tmp = getindex(Combo1, argv[1])) != -1 || (tmp = getindex(Combo2, argv[1])) != -1) {
16120253Sjoerg			which = tmp / M_NUM;
16220253Sjoerg			mode = tmp % M_NUM;
16320253Sjoerg		} else if (strcmp(argv[1], "help") == 0)
16420253Sjoerg			cmdhelp(mode, which);
16544232Sdavidn		else if (which != -1 && mode != -1)
16620253Sjoerg			addarg(&arglist, 'n', argv[1]);
16720253Sjoerg		else
16830259Scharnier			errx(EX_USAGE, "unknown keyword `%s'", argv[1]);
16920253Sjoerg		++argv;
17020253Sjoerg		--argc;
17120253Sjoerg	}
17220253Sjoerg
17320253Sjoerg	/*
17420253Sjoerg	 * Bail out unless the user is specific!
17520253Sjoerg	 */
17620253Sjoerg	if (mode == -1 || which == -1)
17720253Sjoerg		cmdhelp(mode, which);
17820253Sjoerg
17920253Sjoerg	/*
18020253Sjoerg	 * We know which mode we're in and what we're about to do, so now
18120253Sjoerg	 * let's dispatch the remaining command line args in a genric way.
18220253Sjoerg	 */
18320253Sjoerg	optarg = NULL;
18420253Sjoerg
18520253Sjoerg	while ((ch = getopt(argc, argv, opts[which][mode])) != -1) {
18620253Sjoerg		if (ch == '?')
18756000Sdavidn			errx(EX_USAGE, "unknown switch");
18820253Sjoerg		else
18920253Sjoerg			addarg(&arglist, ch, optarg);
19020253Sjoerg		optarg = NULL;
19120253Sjoerg	}
19220253Sjoerg
19320253Sjoerg	/*
19420267Sjoerg	 * Must be root to attempt an update
19520267Sjoerg	 */
19627474Sdavidn	if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && getarg(&arglist, 'N')==NULL)
19730259Scharnier		errx(EX_NOPERM, "you must be root to run this program");
19820267Sjoerg
19920267Sjoerg	/*
20020253Sjoerg	 * We should immediately look for the -q 'quiet' switch so that we
20120253Sjoerg	 * don't bother with extraneous errors
20220253Sjoerg	 */
20320253Sjoerg	if (getarg(&arglist, 'q') != NULL)
20420253Sjoerg		freopen("/dev/null", "w", stderr);
20520253Sjoerg
20620253Sjoerg	/*
20744229Sdavidn	 * Set our base working path if not overridden
20844229Sdavidn	 */
20944229Sdavidn
21044229Sdavidn	config = getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL;
21144229Sdavidn
21244229Sdavidn	if (getarg(&arglist, 'V') != NULL) {
21344229Sdavidn		char * etcpath = getarg(&arglist, 'V')->val;
21444229Sdavidn		if (*etcpath) {
21544229Sdavidn			if (config == NULL) {	/* Only override config location if -C not specified */
21644229Sdavidn				config = malloc(MAXPATHLEN);
21744229Sdavidn				snprintf(config, MAXPATHLEN, "%s/pw.conf", etcpath);
21844229Sdavidn			}
21944229Sdavidn			memcpy(&PWF, &VPWF, sizeof PWF);
22044229Sdavidn			setpwdir(etcpath);
22144229Sdavidn			setgrdir(etcpath);
22244229Sdavidn		}
22344229Sdavidn	}
22444229Sdavidn
22544229Sdavidn	/*
22620253Sjoerg	 * Now, let's do the common initialisation
22720253Sjoerg	 */
22844229Sdavidn	cnf = read_userconfig(config);
22938112Snate
23038110Snate	ch = funcs[which] (cnf, mode, &arglist);
23121330Sdavidn
23221330Sdavidn	/*
23321330Sdavidn	 * If everything went ok, and we've been asked to update
23421330Sdavidn	 * the NIS maps, then do it now
23521330Sdavidn	 */
23621330Sdavidn	if (ch == EXIT_SUCCESS && getarg(&arglist, 'Y') != NULL) {
23721330Sdavidn		pid_t	pid;
23821330Sdavidn
23921330Sdavidn		fflush(NULL);
24021330Sdavidn		if (chdir(_PATH_YP) == -1)
24130259Scharnier			warn("chdir(" _PATH_YP ")");
24221330Sdavidn		else if ((pid = fork()) == -1)
24330259Scharnier			warn("fork()");
24421330Sdavidn		else if (pid == 0) {
24521330Sdavidn			/* Is make anywhere else? */
24621330Sdavidn			execlp("/usr/bin/make", "make", NULL);
24721330Sdavidn			_exit(1);
24821330Sdavidn		} else {
24921330Sdavidn			int   i;
25021330Sdavidn			waitpid(pid, &i, 0);
25121330Sdavidn			if ((i = WEXITSTATUS(i)) != 0)
25230259Scharnier				errx(ch, "make exited with status %d", i);
25321330Sdavidn			else
25421330Sdavidn				pw_log(cnf, mode, which, "NIS maps updated");
25521330Sdavidn		}
25621330Sdavidn	}
25721330Sdavidn	return ch;
25820253Sjoerg}
25920253Sjoerg
26038112Snate
26120253Sjoergstatic int
26220253Sjoerggetindex(const char *words[], const char *word)
26320253Sjoerg{
26420253Sjoerg	int             i = 0;
26520253Sjoerg
26620253Sjoerg	while (words[i]) {
26720253Sjoerg		if (strcmp(words[i], word) == 0)
26820253Sjoerg			return i;
26920253Sjoerg		i++;
27020253Sjoerg	}
27120253Sjoerg	return -1;
27220253Sjoerg}
27320253Sjoerg
27420253Sjoerg
27520253Sjoerg/*
27620253Sjoerg * This is probably an overkill for a cmdline help system, but it reflects
27720253Sjoerg * the complexity of the command line.
27820253Sjoerg */
27920253Sjoerg
28020253Sjoergstatic void
28120253Sjoergcmdhelp(int mode, int which)
28220253Sjoerg{
28320253Sjoerg	if (which == -1)
28452512Sdavidn		fprintf(stderr, "usage:\n  pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n");
28520253Sjoerg	else if (mode == -1)
28652512Sdavidn		fprintf(stderr, "usage:\n  pw %s [add|del|mod|show|next] [help|switches/values]\n", Which[which]);
28720253Sjoerg	else {
28820253Sjoerg
28920253Sjoerg		/*
29020253Sjoerg		 * We need to give mode specific help
29120253Sjoerg		 */
29220253Sjoerg		static const char *help[W_NUM][M_NUM] =
29320253Sjoerg		{
29420253Sjoerg			{
29530259Scharnier				"usage: pw useradd [name] [switches]\n"
29644229Sdavidn				"\t-V etcdir      alternate /etc location\n"
29720253Sjoerg				"\t-C config      configuration file\n"
29820253Sjoerg				"\t-q             quiet operation\n"
29920253Sjoerg				"  Adding users:\n"
30020253Sjoerg				"\t-n name        login name\n"
30120253Sjoerg				"\t-u uid         user id\n"
30220253Sjoerg				"\t-c comment     user name/comment\n"
30320253Sjoerg				"\t-d directory   home directory\n"
30420253Sjoerg				"\t-e date        account expiry date\n"
30520253Sjoerg				"\t-p date        password expiry date\n"
30620253Sjoerg				"\t-g grp         initial group\n"
30720253Sjoerg				"\t-G grp1,grp2   additional groups\n"
30820253Sjoerg				"\t-m [ -k dir ]  create and set up home\n"
30920253Sjoerg				"\t-s shell       name of login shell\n"
31020253Sjoerg				"\t-o             duplicate uid ok\n"
31120253Sjoerg				"\t-L class       user class\n"
31220253Sjoerg				"\t-h fd          read password on fd\n"
31321330Sdavidn				"\t-Y             update NIS maps\n"
31420267Sjoerg				"\t-N             no update\n"
31520253Sjoerg				"  Setting defaults:\n"
31644229Sdavidn				"\t-V etcdir      alternate /etc location\n"
31744229Sdavidn			        "\t-D             set user defaults\n"
31820253Sjoerg				"\t-b dir         default home root dir\n"
31920253Sjoerg				"\t-e period      default expiry period\n"
32020253Sjoerg				"\t-p period      default password change period\n"
32120253Sjoerg				"\t-g group       default group\n"
32220253Sjoerg				"\t-G grp1,grp2   additional groups\n"
32320253Sjoerg				"\t-L class       default user class\n"
32420253Sjoerg				"\t-k dir         default home skeleton\n"
32520253Sjoerg				"\t-u min,max     set min,max uids\n"
32620253Sjoerg				"\t-i min,max     set min,max gids\n"
32720253Sjoerg				"\t-w method      set default password method\n"
32821330Sdavidn				"\t-s shell       default shell\n"
32921330Sdavidn				"\t-y path        set NIS passwd file path\n",
33030259Scharnier				"usage: pw userdel [uid|name] [switches]\n"
33144229Sdavidn				"\t-V etcdir      alternate /etc location\n"
33220253Sjoerg				"\t-n name        login name\n"
33320253Sjoerg				"\t-u uid         user id\n"
33421330Sdavidn				"\t-Y             update NIS maps\n"
33520253Sjoerg				"\t-r             remove home & contents\n",
33630259Scharnier				"usage: pw usermod [uid|name] [switches]\n"
33744229Sdavidn				"\t-V etcdir      alternate /etc location\n"
33820253Sjoerg				"\t-C config      configuration file\n"
33920253Sjoerg				"\t-q             quiet operation\n"
34020253Sjoerg				"\t-F             force add if no user\n"
34120253Sjoerg				"\t-n name        login name\n"
34220253Sjoerg				"\t-u uid         user id\n"
34320253Sjoerg				"\t-c comment     user name/comment\n"
34420253Sjoerg				"\t-d directory   home directory\n"
34520253Sjoerg				"\t-e date        account expiry date\n"
34620253Sjoerg				"\t-p date        password expiry date\n"
34720253Sjoerg				"\t-g grp         initial group\n"
34820253Sjoerg				"\t-G grp1,grp2   additional groups\n"
34920253Sjoerg				"\t-l name        new login name\n"
35020253Sjoerg				"\t-L class       user class\n"
35120253Sjoerg				"\t-m [ -k dir ]  create and set up home\n"
35220253Sjoerg				"\t-s shell       name of login shell\n"
35320267Sjoerg				"\t-w method      set new password using method\n"
35420267Sjoerg				"\t-h fd          read password on fd\n"
35521330Sdavidn				"\t-Y             update NIS maps\n"
35620267Sjoerg				"\t-N             no update\n",
35730259Scharnier				"usage: pw usershow [uid|name] [switches]\n"
35844229Sdavidn				"\t-V etcdir      alternate /etc location\n"
35920253Sjoerg				"\t-n name        login name\n"
36020253Sjoerg				"\t-u uid         user id\n"
36120253Sjoerg				"\t-F             force print\n"
36220267Sjoerg				"\t-P             prettier format\n"
36344386Sdavidn				"\t-a             print all users\n"
36444386Sdavidn				"\t-7             print in v7 format\n",
36530259Scharnier				"usage: pw usernext [switches]\n"
36644229Sdavidn				"\t-V etcdir      alternate /etc location\n"
36720267Sjoerg				"\t-C config      configuration file\n"
36820253Sjoerg			},
36920253Sjoerg			{
37030259Scharnier				"usage: pw groupadd [group|gid] [switches]\n"
37144229Sdavidn				"\t-V etcdir      alternate /etc location\n"
37220253Sjoerg				"\t-C config      configuration file\n"
37320253Sjoerg				"\t-q             quiet operation\n"
37420253Sjoerg				"\t-n group       group name\n"
37520253Sjoerg				"\t-g gid         group id\n"
37620267Sjoerg				"\t-M usr1,usr2   add users as group members\n"
37720267Sjoerg				"\t-o             duplicate gid ok\n"
37821330Sdavidn				"\t-Y             update NIS maps\n"
37920267Sjoerg				"\t-N             no update\n",
38030259Scharnier				"usage: pw groupdel [group|gid] [switches]\n"
38144229Sdavidn				"\t-V etcdir      alternate /etc location\n"
38220253Sjoerg				"\t-n name        group name\n"
38321330Sdavidn				"\t-g gid         group id\n"
38421330Sdavidn				"\t-Y             update NIS maps\n",
38530259Scharnier				"usage: pw groupmod [group|gid] [switches]\n"
38644229Sdavidn				"\t-V etcdir      alternate /etc location\n"
38720253Sjoerg				"\t-C config      configuration file\n"
38820253Sjoerg				"\t-q             quiet operation\n"
38920253Sjoerg				"\t-F             force add if not exists\n"
39020253Sjoerg				"\t-n name        group name\n"
39120253Sjoerg				"\t-g gid         group id\n"
39220267Sjoerg				"\t-M usr1,usr2   replaces users as group members\n"
39320267Sjoerg				"\t-m usr1,usr2   add users as group members\n"
39420267Sjoerg				"\t-l name        new group name\n"
39521330Sdavidn				"\t-Y             update NIS maps\n"
39620267Sjoerg				"\t-N             no update\n",
39730259Scharnier				"usage: pw groupshow [group|gid] [switches]\n"
39844229Sdavidn				"\t-V etcdir      alternate /etc location\n"
39920253Sjoerg				"\t-n name        group name\n"
40020253Sjoerg				"\t-g gid         group id\n"
40120253Sjoerg				"\t-F             force print\n"
40220267Sjoerg				"\t-P             prettier format\n"
40320267Sjoerg				"\t-a             print all accounting groups\n",
40430259Scharnier				"usage: pw groupnext [switches]\n"
40544229Sdavidn				"\t-V etcdir      alternate /etc location\n"
40620267Sjoerg				"\t-C config      configuration file\n"
40720253Sjoerg			}
40820253Sjoerg		};
40920253Sjoerg
41030259Scharnier		fprintf(stderr, help[which][mode]);
41120253Sjoerg	}
41220267Sjoerg	exit(EXIT_FAILURE);
41320253Sjoerg}
41420253Sjoerg
41520253Sjoergstruct carg    *
41620253Sjoerggetarg(struct cargs * _args, int ch)
41720253Sjoerg{
41820253Sjoerg	struct carg    *c = _args->lh_first;
41920253Sjoerg
42020253Sjoerg	while (c != NULL && c->ch != ch)
42120253Sjoerg		c = c->list.le_next;
42220253Sjoerg	return c;
42320253Sjoerg}
42420253Sjoerg
42520253Sjoergstruct carg    *
42620253Sjoergaddarg(struct cargs * _args, int ch, char *argstr)
42720253Sjoerg{
42820253Sjoerg	struct carg    *ca = malloc(sizeof(struct carg));
42920253Sjoerg
43020253Sjoerg	if (ca == NULL)
43130259Scharnier		errx(EX_OSERR, "out of memory");
43220253Sjoerg	ca->ch = ch;
43320253Sjoerg	ca->val = argstr;
43420253Sjoerg	LIST_INSERT_HEAD(_args, ca, list);
43520253Sjoerg	return ca;
43620253Sjoerg}
437