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