pw.c revision 38112
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: pw.c,v 1.8 1998/07/16 17:18:25 nate Exp $";
30#endif /* not lint */
31
32#include "pw.h"
33#include <err.h>
34#include <fcntl.h>
35#include <paths.h>
36#include <sys/wait.h>
37
38const char     *Modes[] = {"add", "del", "mod", "show", "next", NULL};
39const char     *Which[] = {"user", "group", NULL};
40static const char *Combo1[] = {
41  "useradd", "userdel", "usermod", "usershow", "usernext",
42  "groupadd", "groupdel", "groupmod", "groupshow", "groupnext",
43  NULL};
44static const char *Combo2[] = {
45  "adduser", "deluser", "moduser", "showuser", "nextuser",
46  "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup",
47NULL};
48
49static struct cargs arglist;
50
51static int      getindex(const char *words[], const char *word);
52static void     cmdhelp(int mode, int which);
53static void     filelock(const char *filename);
54
55
56int
57main(int argc, char *argv[])
58{
59	int             ch;
60	int             mode = -1;
61	int             which = -1;
62	struct userconf *cnf;
63
64	static const char *opts[W_NUM][M_NUM] =
65	{
66		{ /* user */
67			"C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db:NPy:Y",
68			"C:qn:u:rY",
69			"C:qn:u:c:d:e:p:g:G:ml:k:s:w:L:h:FNPY",
70			"C:qn:u:FPa",
71			"C:q"
72		},
73		{ /* grp  */
74			"C:qn:g:h:M:pNPY",
75			"C:qn:g:Y",
76			"C:qn:g:l:h:FM:m:NPY",
77			"C:qn:g:FPa",
78			"C:q"
79		 }
80	};
81
82	static int      (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) =
83	{			/* Request handlers */
84		pw_user,
85		pw_group
86	};
87
88	umask(0);		/* We wish to handle this manually */
89	LIST_INIT(&arglist);
90
91	/*
92	 * Break off the first couple of words to determine what exactly
93	 * we're being asked to do
94	 */
95	while (argc > 1 && *argv[1] != '-') {
96		int             tmp;
97
98		if ((tmp = getindex(Modes, argv[1])) != -1)
99			mode = tmp;
100		else if ((tmp = getindex(Which, argv[1])) != -1)
101			which = tmp;
102		else if ((tmp = getindex(Combo1, argv[1])) != -1 || (tmp = getindex(Combo2, argv[1])) != -1) {
103			which = tmp / M_NUM;
104			mode = tmp % M_NUM;
105		} else if (strcmp(argv[1], "help") == 0)
106			cmdhelp(mode, which);
107		else if (which != -1 && mode != -1 && arglist.lh_first == NULL)
108			addarg(&arglist, 'n', argv[1]);
109		else
110			errx(EX_USAGE, "unknown keyword `%s'", argv[1]);
111		++argv;
112		--argc;
113	}
114
115	/*
116	 * Bail out unless the user is specific!
117	 */
118	if (mode == -1 || which == -1)
119		cmdhelp(mode, which);
120
121	/*
122	 * We know which mode we're in and what we're about to do, so now
123	 * let's dispatch the remaining command line args in a genric way.
124	 */
125	optarg = NULL;
126
127	while ((ch = getopt(argc, argv, opts[which][mode])) != -1) {
128		if (ch == '?')
129			errx(EX_USAGE, NULL);
130		else
131			addarg(&arglist, ch, optarg);
132		optarg = NULL;
133	}
134
135	/*
136	 * Must be root to attempt an update
137	 */
138	if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && getarg(&arglist, 'N')==NULL)
139		errx(EX_NOPERM, "you must be root to run this program");
140
141	/*
142	 * We should immediately look for the -q 'quiet' switch so that we
143	 * don't bother with extraneous errors
144	 */
145	if (getarg(&arglist, 'q') != NULL)
146		freopen("/dev/null", "w", stderr);
147
148	/*
149	 * Now, let's do the common initialisation
150	 */
151	cnf = read_userconfig(getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL);
152
153	/*
154	 * Try to lock the master passowrd and group files right away (we
155	 * don't care if it works, since this is just advisory locking.
156	 */
157	filelock(_PATH_GROUP);
158	filelock(_PATH_MASTERPASSWD);
159	ch = funcs[which] (cnf, mode, &arglist);
160
161	/*
162	 * If everything went ok, and we've been asked to update
163	 * the NIS maps, then do it now
164	 */
165	if (ch == EXIT_SUCCESS && getarg(&arglist, 'Y') != NULL) {
166		pid_t	pid;
167
168		fflush(NULL);
169		if (chdir(_PATH_YP) == -1)
170			warn("chdir(" _PATH_YP ")");
171		else if ((pid = fork()) == -1)
172			warn("fork()");
173		else if (pid == 0) {
174			/* Is make anywhere else? */
175			execlp("/usr/bin/make", "make", NULL);
176			_exit(1);
177		} else {
178			int   i;
179			waitpid(pid, &i, 0);
180			if ((i = WEXITSTATUS(i)) != 0)
181				errx(ch, "make exited with status %d", i);
182			else
183				pw_log(cnf, mode, which, "NIS maps updated");
184		}
185	}
186	return ch;
187}
188
189static void
190filelock(const char *filename)
191{
192	open(filename, O_RDONLY | O_EXLOCK, 0);
193}
194
195static int
196getindex(const char *words[], const char *word)
197{
198	int             i = 0;
199
200	while (words[i]) {
201		if (strcmp(words[i], word) == 0)
202			return i;
203		i++;
204	}
205	return -1;
206}
207
208
209/*
210 * This is probably an overkill for a cmdline help system, but it reflects
211 * the complexity of the command line.
212 */
213
214static void
215cmdhelp(int mode, int which)
216{
217	if (which == -1)
218		fprintf(stderr, "usage: pw [user|group] [add|del|mod|show|next] [ help | switches/values ]\n");
219	else if (mode == -1)
220		fprintf(stderr, "usage: pw %s [add|del|mod|show|next] [ help | switches/values ]\n", Which[which]);
221	else {
222
223		/*
224		 * We need to give mode specific help
225		 */
226		static const char *help[W_NUM][M_NUM] =
227		{
228			{
229				"usage: pw useradd [name] [switches]\n"
230				"\t-C config      configuration file\n"
231				"\t-q             quiet operation\n"
232				"  Adding users:\n"
233				"\t-n name        login name\n"
234				"\t-u uid         user id\n"
235				"\t-c comment     user name/comment\n"
236				"\t-d directory   home directory\n"
237				"\t-e date        account expiry date\n"
238				"\t-p date        password expiry date\n"
239				"\t-g grp         initial group\n"
240				"\t-G grp1,grp2   additional groups\n"
241				"\t-m [ -k dir ]  create and set up home\n"
242				"\t-s shell       name of login shell\n"
243				"\t-o             duplicate uid ok\n"
244				"\t-L class       user class\n"
245				"\t-h fd          read password on fd\n"
246				"\t-Y             update NIS maps\n"
247				"\t-N             no update\n"
248				"  Setting defaults:\n"
249				"\t-D             set user defaults\n"
250				"\t-b dir         default home root dir\n"
251				"\t-e period      default expiry period\n"
252				"\t-p period      default password change period\n"
253				"\t-g group       default group\n"
254				"\t-G grp1,grp2   additional groups\n"
255				"\t-L class       default user class\n"
256				"\t-k dir         default home skeleton\n"
257				"\t-u min,max     set min,max uids\n"
258				"\t-i min,max     set min,max gids\n"
259				"\t-w method      set default password method\n"
260				"\t-s shell       default shell\n"
261				"\t-y path        set NIS passwd file path\n",
262				"usage: pw userdel [uid|name] [switches]\n"
263				"\t-n name        login name\n"
264				"\t-u uid         user id\n"
265				"\t-Y             update NIS maps\n"
266				"\t-r             remove home & contents\n",
267				"usage: pw usermod [uid|name] [switches]\n"
268				"\t-C config      configuration file\n"
269				"\t-q             quiet operation\n"
270				"\t-F             force add if no user\n"
271				"\t-n name        login name\n"
272				"\t-u uid         user id\n"
273				"\t-c comment     user name/comment\n"
274				"\t-d directory   home directory\n"
275				"\t-e date        account expiry date\n"
276				"\t-p date        password expiry date\n"
277				"\t-g grp         initial group\n"
278				"\t-G grp1,grp2   additional groups\n"
279				"\t-l name        new login name\n"
280				"\t-L class       user class\n"
281				"\t-m [ -k dir ]  create and set up home\n"
282				"\t-s shell       name of login shell\n"
283				"\t-w method      set new password using method\n"
284				"\t-h fd          read password on fd\n"
285				"\t-Y             update NIS maps\n"
286				"\t-N             no update\n",
287				"usage: pw usershow [uid|name] [switches]\n"
288				"\t-n name        login name\n"
289				"\t-u uid         user id\n"
290				"\t-F             force print\n"
291				"\t-P             prettier format\n"
292				"\t-a             print all users\n",
293				"usage: pw usernext [switches]\n"
294				"\t-C config      configuration file\n"
295			},
296			{
297				"usage: pw groupadd [group|gid] [switches]\n"
298				"\t-C config      configuration file\n"
299				"\t-q             quiet operation\n"
300				"\t-n group       group name\n"
301				"\t-g gid         group id\n"
302				"\t-M usr1,usr2   add users as group members\n"
303				"\t-o             duplicate gid ok\n"
304				"\t-Y             update NIS maps\n"
305				"\t-N             no update\n",
306				"usage: pw groupdel [group|gid] [switches]\n"
307				"\t-n name        group name\n"
308				"\t-g gid         group id\n"
309				"\t-Y             update NIS maps\n",
310				"usage: pw groupmod [group|gid] [switches]\n"
311				"\t-C config      configuration file\n"
312				"\t-q             quiet operation\n"
313				"\t-F             force add if not exists\n"
314				"\t-n name        group name\n"
315				"\t-g gid         group id\n"
316				"\t-M usr1,usr2   replaces users as group members\n"
317				"\t-m usr1,usr2   add users as group members\n"
318				"\t-l name        new group name\n"
319				"\t-Y             update NIS maps\n"
320				"\t-N             no update\n",
321				"usage: pw groupshow [group|gid] [switches]\n"
322				"\t-n name        group name\n"
323				"\t-g gid         group id\n"
324				"\t-F             force print\n"
325				"\t-P             prettier format\n"
326				"\t-a             print all accounting groups\n",
327				"usage: pw groupnext [switches]\n"
328				"\t-C config      configuration file\n"
329			}
330		};
331
332		fprintf(stderr, help[which][mode]);
333	}
334	exit(EXIT_FAILURE);
335}
336
337struct carg    *
338getarg(struct cargs * _args, int ch)
339{
340	struct carg    *c = _args->lh_first;
341
342	while (c != NULL && c->ch != ch)
343		c = c->list.le_next;
344	return c;
345}
346
347struct carg    *
348addarg(struct cargs * _args, int ch, char *argstr)
349{
350	struct carg    *ca = malloc(sizeof(struct carg));
351
352	if (ca == NULL)
353		errx(EX_OSERR, "out of memory");
354	ca->ch = ch;
355	ca->val = argstr;
356	LIST_INSERT_HEAD(_args, ca, list);
357	return ca;
358}
359