xref: /dragonfly/usr.sbin/pw/pw.c (revision b40e316c)
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  * $FreeBSD: src/usr.sbin/pw/pw.c,v 1.18.2.5 2001/07/19 01:46:55 kris Exp $
27  * $DragonFly: src/usr.sbin/pw/pw.c,v 1.3 2004/12/18 22:48:04 swildner Exp $
28  */
29 
30 #include <err.h>
31 #include <fcntl.h>
32 #include <locale.h>
33 #include <paths.h>
34 #include <sys/wait.h>
35 #include "pw.h"
36 
37 #if !defined(_PATH_YP)
38 #define	_PATH_YP	"/var/yp/"
39 #endif
40 const char     *Modes[] = {
41   "add", "del", "mod", "show", "next",
42   NULL};
43 const char     *Which[] = {"user", "group", NULL};
44 static const char *Combo1[] = {
45   "useradd", "userdel", "usermod", "usershow", "usernext",
46   "lock", "unlock",
47   "groupadd", "groupdel", "groupmod", "groupshow", "groupnext",
48   NULL};
49 static const char *Combo2[] = {
50   "adduser", "deluser", "moduser", "showuser", "nextuser",
51   "lock", "unlock",
52   "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup",
53   NULL};
54 
55 struct pwf PWF =
56 {
57 	0,
58 	setpwent,
59 	endpwent,
60 	getpwent,
61 	getpwuid,
62 	getpwnam,
63 	pwdb,
64 	setgrent,
65 	endgrent,
66 	getgrent,
67 	getgrgid,
68 	getgrnam,
69 	grdb
70 
71 };
72 struct pwf VPWF =
73 {
74 	1,
75 	vsetpwent,
76 	vendpwent,
77 	vgetpwent,
78 	vgetpwuid,
79 	vgetpwnam,
80 	vpwdb,
81 	vsetgrent,
82 	vendgrent,
83 	vgetgrent,
84 	vgetgrgid,
85 	vgetgrnam,
86 	vgrdb
87 };
88 
89 static struct cargs arglist;
90 
91 static int      getindex(const char *words[], const char *word);
92 static void     cmdhelp(int mode, int which);
93 
94 
95 int
96 main(int argc, char *argv[])
97 {
98 	int             ch;
99 	int             mode = -1;
100 	int             which = -1;
101 	char		*config = NULL;
102 	struct userconf *cnf;
103 
104 	static const char *opts[W_NUM][M_NUM] =
105 	{
106 		{ /* user */
107 			"V:C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db:NPy:Y",
108 			"V:C:qn:u:rY",
109 			"V:C:qn:u:c:d:e:p:g:G:ml:k:s:w:L:h:FNPY",
110 			"V:C:qn:u:FPa7",
111 			"V:C:q",
112 			"V:C:q",
113 			"V:C:q"
114 		},
115 		{ /* grp  */
116 			"V:C:qn:g:h:M:pNPY",
117 			"V:C:qn:g:Y",
118 			"V:C:qn:g:l:h:FM:m:NPY",
119 			"V:C:qn:g:FPa",
120 			"V:C:q"
121 		 }
122 	};
123 
124 	static int      (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) =
125 	{			/* Request handlers */
126 		pw_user,
127 		pw_group
128 	};
129 
130 	umask(0);		/* We wish to handle this manually */
131 	LIST_INIT(&arglist);
132 
133 	setlocale(LC_ALL, "");
134 
135 	/*
136 	 * Break off the first couple of words to determine what exactly
137 	 * we're being asked to do
138 	 */
139 	while (argc > 1) {
140 		int             tmp;
141 
142 		if (*argv[1] == '-') {
143 			/*
144 			 * Special case, allow pw -V<dir> <operation> [args] for scripts etc.
145 			 */
146 			if (argv[1][1] == 'V') {
147 				optarg = &argv[1][2];
148 				if (*optarg == '\0') {
149 					optarg = argv[2];
150 					++argv;
151 					--argc;
152 				}
153 				addarg(&arglist, 'V', optarg);
154 			} else
155 				break;
156 		}
157 		else if (mode == -1 && (tmp = getindex(Modes, argv[1])) != -1)
158 			mode = tmp;
159 		else if (which == -1 && (tmp = getindex(Which, argv[1])) != -1)
160 			which = tmp;
161 		else if ((mode == -1 && which == -1) &&
162 			 ((tmp = getindex(Combo1, argv[1])) != -1 ||
163 			  (tmp = getindex(Combo2, argv[1])) != -1)) {
164 			which = tmp / M_NUM;
165 			mode = tmp % M_NUM;
166 		} else if (strcmp(argv[1], "help") == 0 && argv[2] == NULL)
167 			cmdhelp(mode, which);
168 		else if (which != -1 && mode != -1)
169 			addarg(&arglist, 'n', argv[1]);
170 		else
171 			errx(EX_USAGE, "unknown keyword `%s'", argv[1]);
172 		++argv;
173 		--argc;
174 	}
175 
176 	/*
177 	 * Bail out unless the user is specific!
178 	 */
179 	if (mode == -1 || which == -1)
180 		cmdhelp(mode, which);
181 
182 	/*
183 	 * We know which mode we're in and what we're about to do, so now
184 	 * let's dispatch the remaining command line args in a genric way.
185 	 */
186 	optarg = NULL;
187 
188 	while ((ch = getopt(argc, argv, opts[which][mode])) != -1) {
189 		if (ch == '?')
190 			errx(EX_USAGE, "unknown switch");
191 		else
192 			addarg(&arglist, ch, optarg);
193 		optarg = NULL;
194 	}
195 
196 	/*
197 	 * Must be root to attempt an update
198 	 */
199 	if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && getarg(&arglist, 'N')==NULL)
200 		errx(EX_NOPERM, "you must be root to run this program");
201 
202 	/*
203 	 * We should immediately look for the -q 'quiet' switch so that we
204 	 * don't bother with extraneous errors
205 	 */
206 	if (getarg(&arglist, 'q') != NULL)
207 		freopen(_PATH_DEVNULL, "w", stderr);
208 
209 	/*
210 	 * Set our base working path if not overridden
211 	 */
212 
213 	config = getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL;
214 
215 	if (getarg(&arglist, 'V') != NULL) {
216 		char * etcpath = getarg(&arglist, 'V')->val;
217 		if (*etcpath) {
218 			if (config == NULL) {	/* Only override config location if -C not specified */
219 				config = malloc(MAXPATHLEN);
220 				snprintf(config, MAXPATHLEN, "%s/pw.conf", etcpath);
221 			}
222 			memcpy(&PWF, &VPWF, sizeof PWF);
223 			setpwdir(etcpath);
224 			setgrdir(etcpath);
225 		}
226 	}
227 
228 	/*
229 	 * Now, let's do the common initialisation
230 	 */
231 	cnf = read_userconfig(config);
232 
233 	ch = funcs[which] (cnf, mode, &arglist);
234 
235 	/*
236 	 * If everything went ok, and we've been asked to update
237 	 * the NIS maps, then do it now
238 	 */
239 	if (ch == EXIT_SUCCESS && getarg(&arglist, 'Y') != NULL) {
240 		pid_t	pid;
241 
242 		fflush(NULL);
243 		if (chdir(_PATH_YP) == -1)
244 			warn("chdir(" _PATH_YP ")");
245 		else if ((pid = fork()) == -1)
246 			warn("fork()");
247 		else if (pid == 0) {
248 			/* Is make anywhere else? */
249 			execlp("/usr/bin/make", "make", NULL);
250 			_exit(1);
251 		} else {
252 			int   i;
253 			waitpid(pid, &i, 0);
254 			if ((i = WEXITSTATUS(i)) != 0)
255 				errx(ch, "make exited with status %d", i);
256 			else
257 				pw_log(cnf, mode, which, "NIS maps updated");
258 		}
259 	}
260 	return ch;
261 }
262 
263 
264 static int
265 getindex(const char *words[], const char *word)
266 {
267 	int             i = 0;
268 
269 	while (words[i]) {
270 		if (strcmp(words[i], word) == 0)
271 			return i;
272 		i++;
273 	}
274 	return -1;
275 }
276 
277 
278 /*
279  * This is probably an overkill for a cmdline help system, but it reflects
280  * the complexity of the command line.
281  */
282 
283 static void
284 cmdhelp(int mode, int which)
285 {
286 	if (which == -1)
287 		fprintf(stderr, "usage:\n  pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n");
288 	else if (mode == -1)
289 		fprintf(stderr, "usage:\n  pw %s [add|del|mod|show|next] [help|switches/values]\n", Which[which]);
290 	else {
291 
292 		/*
293 		 * We need to give mode specific help
294 		 */
295 		static const char *help[W_NUM][M_NUM] =
296 		{
297 			{
298 				"usage: pw useradd [name] [switches]\n"
299 				"\t-V etcdir      alternate /etc location\n"
300 				"\t-C config      configuration file\n"
301 				"\t-q             quiet operation\n"
302 				"  Adding users:\n"
303 				"\t-n name        login name\n"
304 				"\t-u uid         user id\n"
305 				"\t-c comment     user name/comment\n"
306 				"\t-d directory   home directory\n"
307 				"\t-e date        account expiry date\n"
308 				"\t-p date        password expiry date\n"
309 				"\t-g grp         initial group\n"
310 				"\t-G grp1,grp2   additional groups\n"
311 				"\t-m [ -k dir ]  create and set up home\n"
312 				"\t-s shell       name of login shell\n"
313 				"\t-o             duplicate uid ok\n"
314 				"\t-L class       user class\n"
315 				"\t-h fd          read password on fd\n"
316 				"\t-Y             update NIS maps\n"
317 				"\t-N             no update\n"
318 				"  Setting defaults:\n"
319 				"\t-V etcdir      alternate /etc location\n"
320 			        "\t-D             set user defaults\n"
321 				"\t-b dir         default home root dir\n"
322 				"\t-e period      default expiry period\n"
323 				"\t-p period      default password change period\n"
324 				"\t-g group       default group\n"
325 				"\t-G grp1,grp2   additional groups\n"
326 				"\t-L class       default user class\n"
327 				"\t-k dir         default home skeleton\n"
328 				"\t-u min,max     set min,max uids\n"
329 				"\t-i min,max     set min,max gids\n"
330 				"\t-w method      set default password method\n"
331 				"\t-s shell       default shell\n"
332 				"\t-y path        set NIS passwd file path\n",
333 				"usage: pw userdel [uid|name] [switches]\n"
334 				"\t-V etcdir      alternate /etc location\n"
335 				"\t-n name        login name\n"
336 				"\t-u uid         user id\n"
337 				"\t-Y             update NIS maps\n"
338 				"\t-r             remove home & contents\n",
339 				"usage: pw usermod [uid|name] [switches]\n"
340 				"\t-V etcdir      alternate /etc location\n"
341 				"\t-C config      configuration file\n"
342 				"\t-q             quiet operation\n"
343 				"\t-F             force add if no user\n"
344 				"\t-n name        login name\n"
345 				"\t-u uid         user id\n"
346 				"\t-c comment     user name/comment\n"
347 				"\t-d directory   home directory\n"
348 				"\t-e date        account expiry date\n"
349 				"\t-p date        password expiry date\n"
350 				"\t-g grp         initial group\n"
351 				"\t-G grp1,grp2   additional groups\n"
352 				"\t-l name        new login name\n"
353 				"\t-L class       user class\n"
354 				"\t-m [ -k dir ]  create and set up home\n"
355 				"\t-s shell       name of login shell\n"
356 				"\t-w method      set new password using method\n"
357 				"\t-h fd          read password on fd\n"
358 				"\t-Y             update NIS maps\n"
359 				"\t-N             no update\n",
360 				"usage: pw usershow [uid|name] [switches]\n"
361 				"\t-V etcdir      alternate /etc location\n"
362 				"\t-n name        login name\n"
363 				"\t-u uid         user id\n"
364 				"\t-F             force print\n"
365 				"\t-P             prettier format\n"
366 				"\t-a             print all users\n"
367 				"\t-7             print in v7 format\n",
368 				"usage: pw usernext [switches]\n"
369 				"\t-V etcdir      alternate /etc location\n"
370 				"\t-C config      configuration file\n"
371 				"\t-q             quiet operation\n",
372 				"usage pw: lock [switches]\n"
373 				"\t-V etcdir      alternate /etc locations\n"
374 				"\t-C config      configuration file\n"
375 				"\t-q             quiet operation\n",
376 				"usage pw: unlock [switches]\n"
377 				"\t-V etcdir      alternate /etc locations\n"
378 				"\t-C config      configuration file\n"
379 				"\t-q             quiet operation\n"
380 			},
381 			{
382 				"usage: pw groupadd [group|gid] [switches]\n"
383 				"\t-V etcdir      alternate /etc location\n"
384 				"\t-C config      configuration file\n"
385 				"\t-q             quiet operation\n"
386 				"\t-n group       group name\n"
387 				"\t-g gid         group id\n"
388 				"\t-M usr1,usr2   add users as group members\n"
389 				"\t-o             duplicate gid ok\n"
390 				"\t-Y             update NIS maps\n"
391 				"\t-N             no update\n",
392 				"usage: pw groupdel [group|gid] [switches]\n"
393 				"\t-V etcdir      alternate /etc location\n"
394 				"\t-n name        group name\n"
395 				"\t-g gid         group id\n"
396 				"\t-Y             update NIS maps\n",
397 				"usage: pw groupmod [group|gid] [switches]\n"
398 				"\t-V etcdir      alternate /etc location\n"
399 				"\t-C config      configuration file\n"
400 				"\t-q             quiet operation\n"
401 				"\t-F             force add if not exists\n"
402 				"\t-n name        group name\n"
403 				"\t-g gid         group id\n"
404 				"\t-M usr1,usr2   replaces users as group members\n"
405 				"\t-m usr1,usr2   add users as group members\n"
406 				"\t-l name        new group name\n"
407 				"\t-Y             update NIS maps\n"
408 				"\t-N             no update\n",
409 				"usage: pw groupshow [group|gid] [switches]\n"
410 				"\t-V etcdir      alternate /etc location\n"
411 				"\t-n name        group name\n"
412 				"\t-g gid         group id\n"
413 				"\t-F             force print\n"
414 				"\t-P             prettier format\n"
415 				"\t-a             print all accounting groups\n",
416 				"usage: pw groupnext [switches]\n"
417 				"\t-V etcdir      alternate /etc location\n"
418 				"\t-C config      configuration file\n"
419 				"\t-q             quiet operation\n"
420 			}
421 		};
422 
423 		fprintf(stderr, "%s", help[which][mode]);
424 	}
425 	exit(EXIT_FAILURE);
426 }
427 
428 struct carg    *
429 getarg(struct cargs * _args, int ch)
430 {
431 	struct carg    *c = _args->lh_first;
432 
433 	while (c != NULL && c->ch != ch)
434 		c = c->list.le_next;
435 	return c;
436 }
437 
438 struct carg    *
439 addarg(struct cargs * _args, int ch, char *argstr)
440 {
441 	struct carg    *ca = malloc(sizeof(struct carg));
442 
443 	if (ca == NULL)
444 		errx(EX_OSERR, "out of memory");
445 	ca->ch = ch;
446 	ca->val = argstr;
447 	LIST_INSERT_HEAD(_args, ca, list);
448 	return ca;
449 }
450