pw.c revision 30259
1/*-
2 * Copyright (C) 1996
3 *	David L. Nugent.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#ifndef lint
28static const char rcsid[] =
29	"$Id$";
30#endif /* not lint */
31
32#include "pw.h"
33#include <err.h>
34#include <paths.h>
35#include <sys/wait.h>
36
37const char     *Modes[] = {"add", "del", "mod", "show", "next", NULL};
38const char     *Which[] = {"user", "group", NULL};
39static const char *Combo1[] = {
40  "useradd", "userdel", "usermod", "usershow", "usernext",
41  "groupadd", "groupdel", "groupmod", "groupshow", "groupnext",
42  NULL};
43static const char *Combo2[] = {
44  "adduser", "deluser", "moduser", "showuser", "nextuser",
45  "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup",
46NULL};
47
48static struct cargs arglist;
49
50static int      getindex(const char *words[], const char *word);
51static void     cmdhelp(int mode, int which);
52
53
54int
55main(int argc, char *argv[])
56{
57	int             ch;
58	int             mode = -1;
59	int             which = -1;
60	struct userconf *cnf;
61
62	static const char *opts[W_NUM][M_NUM] =
63	{
64		{ /* user */
65			"C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db:NPy:Y",
66			"C:qn:u:rY",
67			"C:qn:u:c:d:e:p:g:G:ml:k:s:w:L:h:FNPY",
68			"C:qn:u:FPa",
69			"C:q"
70		},
71		{ /* grp  */
72			"C:qn:g:h:M:pNPY",
73			"C:qn:g:Y",
74			"C:qn:g:l:h:FM:m:NPY",
75			"C:qn:g:FPa",
76			"C:q"
77		 }
78	};
79
80	static int      (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) =
81	{			/* Request handlers */
82		pw_user,
83		pw_group
84	};
85
86	umask(0);		/* We wish to handle this manually */
87	LIST_INIT(&arglist);
88
89	/*
90	 * Break off the first couple of words to determine what exactly
91	 * we're being asked to do
92	 */
93	while (argc > 1 && *argv[1] != '-') {
94		int             tmp;
95
96		if ((tmp = getindex(Modes, argv[1])) != -1)
97			mode = tmp;
98		else if ((tmp = getindex(Which, argv[1])) != -1)
99			which = tmp;
100		else if ((tmp = getindex(Combo1, argv[1])) != -1 || (tmp = getindex(Combo2, argv[1])) != -1) {
101			which = tmp / M_NUM;
102			mode = tmp % M_NUM;
103		} else if (strcmp(argv[1], "help") == 0)
104			cmdhelp(mode, which);
105		else if (which != -1 && mode != -1 && arglist.lh_first == NULL)
106			addarg(&arglist, 'n', argv[1]);
107		else
108			errx(EX_USAGE, "unknown keyword `%s'", argv[1]);
109		++argv;
110		--argc;
111	}
112
113	/*
114	 * Bail out unless the user is specific!
115	 */
116	if (mode == -1 || which == -1)
117		cmdhelp(mode, which);
118
119	/*
120	 * We know which mode we're in and what we're about to do, so now
121	 * let's dispatch the remaining command line args in a genric way.
122	 */
123	optarg = NULL;
124
125	while ((ch = getopt(argc, argv, opts[which][mode])) != -1) {
126		if (ch == '?')
127			errx(EX_USAGE, NULL);
128		else
129			addarg(&arglist, ch, optarg);
130		optarg = NULL;
131	}
132
133	/*
134	 * Must be root to attempt an update
135	 */
136	if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && getarg(&arglist, 'N')==NULL)
137		errx(EX_NOPERM, "you must be root to run this program");
138
139	/*
140	 * We should immediately look for the -q 'quiet' switch so that we
141	 * don't bother with extraneous errors
142	 */
143	if (getarg(&arglist, 'q') != NULL)
144		freopen("/dev/null", "w", stderr);
145
146	/*
147	 * Now, let's do the common initialisation
148	 */
149	cnf = read_userconfig(getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL);
150	ch = funcs[which] (cnf, mode, &arglist);
151
152	/*
153	 * If everything went ok, and we've been asked to update
154	 * the NIS maps, then do it now
155	 */
156	if (ch == EXIT_SUCCESS && getarg(&arglist, 'Y') != NULL) {
157		pid_t	pid;
158
159		fflush(NULL);
160		if (chdir(_PATH_YP) == -1)
161			warn("chdir(" _PATH_YP ")");
162		else if ((pid = fork()) == -1)
163			warn("fork()");
164		else if (pid == 0) {
165			/* Is make anywhere else? */
166			execlp("/usr/bin/make", "make", NULL);
167			_exit(1);
168		} else {
169			int   i;
170			waitpid(pid, &i, 0);
171			if ((i = WEXITSTATUS(i)) != 0)
172				errx(ch, "make exited with status %d", i);
173			else
174				pw_log(cnf, mode, which, "NIS maps updated");
175		}
176	}
177	return ch;
178}
179
180static int
181getindex(const char *words[], const char *word)
182{
183	int             i = 0;
184
185	while (words[i]) {
186		if (strcmp(words[i], word) == 0)
187			return i;
188		i++;
189	}
190	return -1;
191}
192
193
194/*
195 * This is probably an overkill for a cmdline help system, but it reflects
196 * the complexity of the command line.
197 */
198
199static void
200cmdhelp(int mode, int which)
201{
202	if (which == -1)
203		fprintf(stderr, "usage: pw [user|group] [add|del|mod|show|next] [ help | switches/values ]\n");
204	else if (mode == -1)
205		fprintf(stderr, "usage: pw %s [add|del|mod|show|next] [ help | switches/values ]\n", Which[which]);
206	else {
207
208		/*
209		 * We need to give mode specific help
210		 */
211		static const char *help[W_NUM][M_NUM] =
212		{
213			{
214				"usage: pw useradd [name] [switches]\n"
215				"\t-C config      configuration file\n"
216				"\t-q             quiet operation\n"
217				"  Adding users:\n"
218				"\t-n name        login name\n"
219				"\t-u uid         user id\n"
220				"\t-c comment     user name/comment\n"
221				"\t-d directory   home directory\n"
222				"\t-e date        account expiry date\n"
223				"\t-p date        password expiry date\n"
224				"\t-g grp         initial group\n"
225				"\t-G grp1,grp2   additional groups\n"
226				"\t-m [ -k dir ]  create and set up home\n"
227				"\t-s shell       name of login shell\n"
228				"\t-o             duplicate uid ok\n"
229				"\t-L class       user class\n"
230				"\t-h fd          read password on fd\n"
231				"\t-Y             update NIS maps\n"
232				"\t-N             no update\n"
233				"  Setting defaults:\n"
234				"\t-D             set user defaults\n"
235				"\t-b dir         default home root dir\n"
236				"\t-e period      default expiry period\n"
237				"\t-p period      default password change period\n"
238				"\t-g group       default group\n"
239				"\t-G grp1,grp2   additional groups\n"
240				"\t-L class       default user class\n"
241				"\t-k dir         default home skeleton\n"
242				"\t-u min,max     set min,max uids\n"
243				"\t-i min,max     set min,max gids\n"
244				"\t-w method      set default password method\n"
245				"\t-s shell       default shell\n"
246				"\t-y path        set NIS passwd file path\n",
247				"usage: pw userdel [uid|name] [switches]\n"
248				"\t-n name        login name\n"
249				"\t-u uid         user id\n"
250				"\t-Y             update NIS maps\n"
251				"\t-r             remove home & contents\n",
252				"usage: pw usermod [uid|name] [switches]\n"
253				"\t-C config      configuration file\n"
254				"\t-q             quiet operation\n"
255				"\t-F             force add if no user\n"
256				"\t-n name        login name\n"
257				"\t-u uid         user id\n"
258				"\t-c comment     user name/comment\n"
259				"\t-d directory   home directory\n"
260				"\t-e date        account expiry date\n"
261				"\t-p date        password expiry date\n"
262				"\t-g grp         initial group\n"
263				"\t-G grp1,grp2   additional groups\n"
264				"\t-l name        new login name\n"
265				"\t-L class       user class\n"
266				"\t-m [ -k dir ]  create and set up home\n"
267				"\t-s shell       name of login shell\n"
268				"\t-w method      set new password using method\n"
269				"\t-h fd          read password on fd\n"
270				"\t-Y             update NIS maps\n"
271				"\t-N             no update\n",
272				"usage: pw usershow [uid|name] [switches]\n"
273				"\t-n name        login name\n"
274				"\t-u uid         user id\n"
275				"\t-F             force print\n"
276				"\t-P             prettier format\n"
277				"\t-a             print all users\n",
278				"usage: pw usernext [switches]\n"
279				"\t-C config      configuration file\n"
280			},
281			{
282				"usage: pw groupadd [group|gid] [switches]\n"
283				"\t-C config      configuration file\n"
284				"\t-q             quiet operation\n"
285				"\t-n group       group name\n"
286				"\t-g gid         group id\n"
287				"\t-M usr1,usr2   add users as group members\n"
288				"\t-o             duplicate gid ok\n"
289				"\t-Y             update NIS maps\n"
290				"\t-N             no update\n",
291				"usage: pw groupdel [group|gid] [switches]\n"
292				"\t-n name        group name\n"
293				"\t-g gid         group id\n"
294				"\t-Y             update NIS maps\n",
295				"usage: pw groupmod [group|gid] [switches]\n"
296				"\t-C config      configuration file\n"
297				"\t-q             quiet operation\n"
298				"\t-F             force add if not exists\n"
299				"\t-n name        group name\n"
300				"\t-g gid         group id\n"
301				"\t-M usr1,usr2   replaces users as group members\n"
302				"\t-m usr1,usr2   add users as group members\n"
303				"\t-l name        new group name\n"
304				"\t-Y             update NIS maps\n"
305				"\t-N             no update\n",
306				"usage: pw groupshow [group|gid] [switches]\n"
307				"\t-n name        group name\n"
308				"\t-g gid         group id\n"
309				"\t-F             force print\n"
310				"\t-P             prettier format\n"
311				"\t-a             print all accounting groups\n",
312				"usage: pw groupnext [switches]\n"
313				"\t-C config      configuration file\n"
314			}
315		};
316
317		fprintf(stderr, help[which][mode]);
318	}
319	exit(EXIT_FAILURE);
320}
321
322struct carg    *
323getarg(struct cargs * _args, int ch)
324{
325	struct carg    *c = _args->lh_first;
326
327	while (c != NULL && c->ch != ch)
328		c = c->list.le_next;
329	return c;
330}
331
332struct carg    *
333addarg(struct cargs * _args, int ch, char *argstr)
334{
335	struct carg    *ca = malloc(sizeof(struct carg));
336
337	if (ca == NULL)
338		errx(EX_OSERR, "out of memory");
339	ca->ch = ch;
340	ca->val = argstr;
341	LIST_INSERT_HEAD(_args, ca, list);
342	return ca;
343}
344