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