pw.c revision 285092
1146329Skeramida/*-
2146329Skeramida * Copyright (C) 1996
3146329Skeramida *	David L. Nugent.  All rights reserved.
4146329Skeramida *
5146329Skeramida * Redistribution and use in source and binary forms, with or without
6146329Skeramida * modification, are permitted provided that the following conditions
7146329Skeramida * are met:
8146329Skeramida * 1. Redistributions of source code must retain the above copyright
9146329Skeramida *    notice, this list of conditions and the following disclaimer.
10146329Skeramida * 2. Redistributions in binary form must reproduce the above copyright
11146329Skeramida *    notice, this list of conditions and the following disclaimer in the
12146329Skeramida *    documentation and/or other materials provided with the distribution.
13146329Skeramida *
14146329Skeramida * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15146329Skeramida * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16146329Skeramida * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17146329Skeramida * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18146329Skeramida * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19146329Skeramida * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20146329Skeramida * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21146329Skeramida * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22146329Skeramida * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23146329Skeramida * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24146329Skeramida * SUCH DAMAGE.
25146329Skeramida */
26146329Skeramida
27290246Sngie#ifndef lint
28146329Skeramidastatic const char rcsid[] =
29146329Skeramida  "$FreeBSD: stable/10/usr.sbin/pw/pw.c 285092 2015-07-03 14:22:44Z bapt $";
30146329Skeramida#endif /* not lint */
31146329Skeramida
32146329Skeramida#include <err.h>
33146329Skeramida#include <fcntl.h>
34146329Skeramida#include <locale.h>
35146329Skeramida#include <paths.h>
36146329Skeramida#include <stdbool.h>
37146329Skeramida#include <sys/wait.h>
38146329Skeramida#include "pw.h"
39146329Skeramida
40146329Skeramida#if !defined(_PATH_YP)
41146329Skeramida#define	_PATH_YP	"/var/yp/"
42146329Skeramida#endif
43146329Skeramidaconst char     *Modes[] = {
44153430Siedowse  "add", "del", "mod", "show", "next",
45153430Siedowse  NULL};
46146329Skeramidaconst char     *Which[] = {"user", "group", NULL};
47146329Skeramidastatic const char *Combo1[] = {
48146329Skeramida  "useradd", "userdel", "usermod", "usershow", "usernext",
49146329Skeramida  "lock", "unlock",
50146329Skeramida  "groupadd", "groupdel", "groupmod", "groupshow", "groupnext",
51146329Skeramida  NULL};
52146329Skeramidastatic const char *Combo2[] = {
53146329Skeramida  "adduser", "deluser", "moduser", "showuser", "nextuser",
54147432Sru  "lock", "unlock",
55146329Skeramida  "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup",
56146329Skeramida  NULL};
57147432Sru
58146329Skeramidastruct pwf PWF =
59147432Sru{
60147432Sru	PWF_REGULAR,
61146329Skeramida	setpwent,
62147432Sru	endpwent,
63146329Skeramida	getpwent,
64146329Skeramida	getpwuid,
65146329Skeramida	getpwnam,
66146329Skeramida	setgrent,
67147432Sru	endgrent,
68146329Skeramida	getgrent,
69147432Sru	getgrgid,
70146329Skeramida	getgrnam,
71147432Sru
72146329Skeramida};
73146329Skeramidastruct pwf VPWF =
74147432Sru{
75146329Skeramida	PWF_ALT,
76147432Sru	vsetpwent,
77146329Skeramida	vendpwent,
78147432Sru	vgetpwent,
79146329Skeramida	vgetpwuid,
80147432Sru	vgetpwnam,
81146329Skeramida	vsetgrent,
82146329Skeramida	vendgrent,
83147432Sru	vgetgrent,
84146329Skeramida	vgetgrgid,
85147432Sru	vgetgrnam,
86146329Skeramida};
87147432Sru
88148580Skeramidastruct pwconf conf;
89147432Sru
90146329Skeramidastatic struct cargs arglist;
91146329Skeramida
92146329Skeramidastatic int      getindex(const char *words[], const char *word);
93147432Srustatic void     cmdhelp(int mode, int which);
94236278Sgjb
95147432Sru
96147432Sruint
97147432Srumain(int argc, char *argv[])
98146329Skeramida{
99146329Skeramida	int             ch;
100146329Skeramida	int             mode = -1;
101146329Skeramida	int             which = -1;
102146329Skeramida	long		id = -1;
103146329Skeramida	char		*config = NULL;
104146329Skeramida	struct stat	st;
105146329Skeramida	const char	*errstr;
106146329Skeramida	char		arg, *name;
107146329Skeramida	bool		relocated, nis;
108146329Skeramida
109146329Skeramida	static const char *opts[W_NUM][M_NUM] =
110146329Skeramida	{
111146329Skeramida		{ /* user */
112290246Sngie			"R:V:C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y",
113146329Skeramida			"R:V:C:qn:u:rY",
114290246Sngie			"R:V:C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:FNPY",
115146329Skeramida			"R:V:C:qn:u:FPa7",
116146329Skeramida			"R:V:C:q",
117146329Skeramida			"R:V:C:q",
118147432Sru			"R:V:C:q"
119146329Skeramida		},
120146329Skeramida		{ /* grp  */
121146329Skeramida			"R:V:C:qn:g:h:H:M:opNPY",
122146329Skeramida			"R:V:C:qn:g:Y",
123146329Skeramida			"R:V:C:qn:d:g:l:h:H:FM:m:NPY",
124146329Skeramida			"R:V:C:qn:g:FPa",
125147432Sru			"R:V:C:q"
126146329Skeramida		 }
127146329Skeramida	};
128146329Skeramida
129146329Skeramida	static int      (*funcs[W_NUM]) (int _mode, char *_name, long _id,
130146329Skeramida	    struct cargs * _args) =
131146329Skeramida	{			/* Request handlers */
132162404Sru		pw_user,
133146329Skeramida		pw_group
134146329Skeramida	};
135146329Skeramida
136146329Skeramida	name = NULL;
137147432Sru	relocated = nis = false;
138147432Sru	memset(&conf, 0, sizeof(conf));
139	strlcpy(conf.etcpath, _PATH_PWD, sizeof(conf.etcpath));
140
141	LIST_INIT(&arglist);
142
143	(void)setlocale(LC_ALL, "");
144
145	/*
146	 * Break off the first couple of words to determine what exactly
147	 * we're being asked to do
148	 */
149	while (argc > 1) {
150		int             tmp;
151
152		if (*argv[1] == '-') {
153			/*
154			 * Special case, allow pw -V<dir> <operation> [args] for scripts etc.
155			 */
156			arg = argv[1][1];
157			if (arg == 'V' || arg == 'R') {
158				if (relocated)
159					errx(EXIT_FAILURE, "Both '-R' and '-V' "
160					    "specified, only one accepted");
161				relocated = true;
162				optarg = &argv[1][2];
163				if (*optarg == '\0') {
164					if (stat(argv[2], &st) != 0)
165						errx(EX_OSFILE, \
166						    "no such directory `%s'",
167						    argv[2]);
168					if (!S_ISDIR(st.st_mode))
169						errx(EX_OSFILE, "`%s' not a "
170						    "directory", argv[2]);
171					optarg = argv[2];
172					++argv;
173					--argc;
174				}
175				memcpy(&PWF, &VPWF, sizeof PWF);
176				if (arg == 'R') {
177					strlcpy(conf.rootdir, optarg,
178					    sizeof(conf.rootdir));
179					PWF._altdir = PWF_ROOTDIR;
180				}
181				snprintf(conf.etcpath, sizeof(conf.etcpath),
182				    "%s%s", optarg, arg == 'R' ? "/etc" : "");
183			} else
184				break;
185		}
186		else if (mode == -1 && (tmp = getindex(Modes, argv[1])) != -1)
187			mode = tmp;
188		else if (which == -1 && (tmp = getindex(Which, argv[1])) != -1)
189			which = tmp;
190		else if ((mode == -1 && which == -1) &&
191			 ((tmp = getindex(Combo1, argv[1])) != -1 ||
192			  (tmp = getindex(Combo2, argv[1])) != -1)) {
193			which = tmp / M_NUM;
194			mode = tmp % M_NUM;
195		} else if (strcmp(argv[1], "help") == 0 && argv[2] == NULL)
196			cmdhelp(mode, which);
197		else if (which != -1 && mode != -1) {
198			if (strspn(argv[1], "0123456789") == strlen(argv[1])) {
199				id = strtonum(argv[1], 0, LONG_MAX, &errstr);
200				if (errstr != NULL)
201					errx(EX_USAGE, "Bad id '%s': %s",
202					    argv[1], errstr);
203			} else
204				name = argv[1];
205		} else
206			errx(EX_USAGE, "unknown keyword `%s'", argv[1]);
207		++argv;
208		--argc;
209	}
210
211	/*
212	 * Bail out unless the user is specific!
213	 */
214	if (mode == -1 || which == -1)
215		cmdhelp(mode, which);
216
217	/*
218	 * We know which mode we're in and what we're about to do, so now
219	 * let's dispatch the remaining command line args in a genric way.
220	 */
221	optarg = NULL;
222
223	while ((ch = getopt(argc, argv, opts[which][mode])) != -1) {
224		switch (ch) {
225		case '?':
226			errx(EX_USAGE, "unknown switch");
227			break;
228		case '7':
229			conf.v7 = true;
230			break;
231		case 'C':
232			conf.config = optarg;
233			config = conf.config;
234			break;
235		case 'N':
236			conf.dryrun = true;
237			break;
238		case 'l':
239			if (strlen(optarg) >= MAXLOGNAME)
240				errx(EX_USAGE, "new name too long: %s", optarg);
241			conf.newname = optarg;
242			break;
243		case 'P':
244			conf.pretty = true;
245			break;
246		case 'Y':
247			nis = true;
248			break;
249		case 'g':
250			if (which == 0) { /* for user* */
251				addarg(&arglist, 'g', optarg);
252				break;
253			}
254			if (strspn(optarg, "0123456789") != strlen(optarg))
255				errx(EX_USAGE, "-g expects a number");
256			id = strtonum(optarg, 0, LONG_MAX, &errstr);
257			if (errstr != NULL)
258				errx(EX_USAGE, "Bad id '%s': %s", optarg,
259				    errstr);
260			break;
261		case 'u':
262			if (strspn(optarg, "0123456789,") != strlen(optarg))
263				errx(EX_USAGE, "-u expects a number");
264			if (strchr(optarg, ',') != NULL) {
265				addarg(&arglist, 'u', optarg);
266				break;
267			}
268			id = strtonum(optarg, 0, LONG_MAX, &errstr);
269			if (errstr != NULL)
270				errx(EX_USAGE, "Bad id '%s': %s", optarg,
271				    errstr);
272			break;
273		case 'n':
274			if (strspn(optarg, "0123456789") != strlen(optarg)) {
275				name = optarg;
276				break;
277			}
278			id = strtonum(optarg, 0, LONG_MAX, &errstr);
279			if (errstr != NULL)
280				errx(EX_USAGE, "Bad id '%s': %s", optarg,
281				    errstr);
282			break;
283		case 'o':
284			conf.checkduplicate = true;
285			break;
286		default:
287			addarg(&arglist, ch, optarg);
288			break;
289		}
290		optarg = NULL;
291	}
292
293	if (name != NULL && strlen(name) >= MAXLOGNAME)
294		errx(EX_USAGE, "name too long: %s", name);
295
296	/*
297	 * Must be root to attempt an update
298	 */
299	if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && !conf.dryrun)
300		errx(EX_NOPERM, "you must be root to run this program");
301
302	/*
303	 * We should immediately look for the -q 'quiet' switch so that we
304	 * don't bother with extraneous errors
305	 */
306	if (getarg(&arglist, 'q') != NULL)
307		freopen(_PATH_DEVNULL, "w", stderr);
308
309	/*
310	 * Set our base working path if not overridden
311	 */
312
313	if (config == NULL) {	/* Only override config location if -C not specified */
314		asprintf(&config, "%s/pw.conf", conf.etcpath);
315		if (config == NULL)
316			errx(EX_OSERR, "out of memory");
317	}
318
319	/*
320	 * Now, let's do the common initialisation
321	 */
322	conf.userconf = read_userconfig(config);
323
324	ch = funcs[which] (mode, name, id, &arglist);
325
326	/*
327	 * If everything went ok, and we've been asked to update
328	 * the NIS maps, then do it now
329	 */
330	if (ch == EXIT_SUCCESS && nis) {
331		pid_t	pid;
332
333		fflush(NULL);
334		if (chdir(_PATH_YP) == -1)
335			warn("chdir(" _PATH_YP ")");
336		else if ((pid = fork()) == -1)
337			warn("fork()");
338		else if (pid == 0) {
339			/* Is make anywhere else? */
340			execlp("/usr/bin/make", "make", (char *)NULL);
341			_exit(1);
342		} else {
343			int   i;
344			waitpid(pid, &i, 0);
345			if ((i = WEXITSTATUS(i)) != 0)
346				errx(ch, "make exited with status %d", i);
347			else
348				pw_log(conf.userconf, mode, which, "NIS maps updated");
349		}
350	}
351	return ch;
352}
353
354
355static int
356getindex(const char *words[], const char *word)
357{
358	int             i = 0;
359
360	while (words[i]) {
361		if (strcmp(words[i], word) == 0)
362			return i;
363		i++;
364	}
365	return -1;
366}
367
368
369/*
370 * This is probably an overkill for a cmdline help system, but it reflects
371 * the complexity of the command line.
372 */
373
374static void
375cmdhelp(int mode, int which)
376{
377	if (which == -1)
378		fprintf(stderr, "usage:\n  pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n");
379	else if (mode == -1)
380		fprintf(stderr, "usage:\n  pw %s [add|del|mod|show|next] [help|switches/values]\n", Which[which]);
381	else {
382
383		/*
384		 * We need to give mode specific help
385		 */
386		static const char *help[W_NUM][M_NUM] =
387		{
388			{
389				"usage: pw useradd [name] [switches]\n"
390				"\t-V etcdir      alternate /etc location\n"
391				"\t-R rootir      alternate root directory\n"
392				"\t-C config      configuration file\n"
393				"\t-q             quiet operation\n"
394				"  Adding users:\n"
395				"\t-n name        login name\n"
396				"\t-u uid         user id\n"
397				"\t-c comment     user name/comment\n"
398				"\t-d directory   home directory\n"
399				"\t-e date        account expiry date\n"
400				"\t-p date        password expiry date\n"
401				"\t-g grp         initial group\n"
402				"\t-G grp1,grp2   additional groups\n"
403				"\t-m [ -k dir ]  create and set up home\n"
404				"\t-M mode        home directory permissions\n"
405				"\t-s shell       name of login shell\n"
406				"\t-o             duplicate uid ok\n"
407				"\t-L class       user class\n"
408				"\t-h fd          read password on fd\n"
409				"\t-H fd          read encrypted password on fd\n"
410				"\t-Y             update NIS maps\n"
411				"\t-N             no update\n"
412				"  Setting defaults:\n"
413				"\t-V etcdir      alternate /etc location\n"
414				"\t-R rootir      alternate root directory\n"
415			        "\t-D             set user defaults\n"
416				"\t-b dir         default home root dir\n"
417				"\t-e period      default expiry period\n"
418				"\t-p period      default password change period\n"
419				"\t-g group       default group\n"
420				"\t-G grp1,grp2   additional groups\n"
421				"\t-L class       default user class\n"
422				"\t-k dir         default home skeleton\n"
423				"\t-M mode        home directory permissions\n"
424				"\t-u min,max     set min,max uids\n"
425				"\t-i min,max     set min,max gids\n"
426				"\t-w method      set default password method\n"
427				"\t-s shell       default shell\n"
428				"\t-y path        set NIS passwd file path\n",
429				"usage: pw userdel [uid|name] [switches]\n"
430				"\t-V etcdir      alternate /etc location\n"
431				"\t-R rootir      alternate root directory\n"
432				"\t-n name        login name\n"
433				"\t-u uid         user id\n"
434				"\t-Y             update NIS maps\n"
435				"\t-r             remove home & contents\n",
436				"usage: pw usermod [uid|name] [switches]\n"
437				"\t-V etcdir      alternate /etc location\n"
438				"\t-R rootir      alternate root directory\n"
439				"\t-C config      configuration file\n"
440				"\t-q             quiet operation\n"
441				"\t-F             force add if no user\n"
442				"\t-n name        login name\n"
443				"\t-u uid         user id\n"
444				"\t-c comment     user name/comment\n"
445				"\t-d directory   home directory\n"
446				"\t-e date        account expiry date\n"
447				"\t-p date        password expiry date\n"
448				"\t-g grp         initial group\n"
449				"\t-G grp1,grp2   additional groups\n"
450				"\t-l name        new login name\n"
451				"\t-L class       user class\n"
452				"\t-m [ -k dir ]  create and set up home\n"
453				"\t-M mode        home directory permissions\n"
454				"\t-s shell       name of login shell\n"
455				"\t-w method      set new password using method\n"
456				"\t-h fd          read password on fd\n"
457				"\t-H fd          read encrypted password on fd\n"
458				"\t-Y             update NIS maps\n"
459				"\t-N             no update\n",
460				"usage: pw usershow [uid|name] [switches]\n"
461				"\t-V etcdir      alternate /etc location\n"
462				"\t-R rootir      alternate root directory\n"
463				"\t-n name        login name\n"
464				"\t-u uid         user id\n"
465				"\t-F             force print\n"
466				"\t-P             prettier format\n"
467				"\t-a             print all users\n"
468				"\t-7             print in v7 format\n",
469				"usage: pw usernext [switches]\n"
470				"\t-V etcdir      alternate /etc location\n"
471				"\t-R rootir      alternate root directory\n"
472				"\t-C config      configuration file\n"
473				"\t-q             quiet operation\n",
474				"usage pw: lock [switches]\n"
475				"\t-V etcdir      alternate /etc locations\n"
476				"\t-C config      configuration file\n"
477				"\t-q             quiet operation\n",
478				"usage pw: unlock [switches]\n"
479				"\t-V etcdir      alternate /etc locations\n"
480				"\t-C config      configuration file\n"
481				"\t-q             quiet operation\n"
482			},
483			{
484				"usage: pw groupadd [group|gid] [switches]\n"
485				"\t-V etcdir      alternate /etc location\n"
486				"\t-R rootir      alternate root directory\n"
487				"\t-C config      configuration file\n"
488				"\t-q             quiet operation\n"
489				"\t-n group       group name\n"
490				"\t-g gid         group id\n"
491				"\t-M usr1,usr2   add users as group members\n"
492				"\t-o             duplicate gid ok\n"
493				"\t-Y             update NIS maps\n"
494				"\t-N             no update\n",
495				"usage: pw groupdel [group|gid] [switches]\n"
496				"\t-V etcdir      alternate /etc location\n"
497				"\t-R rootir      alternate root directory\n"
498				"\t-n name        group name\n"
499				"\t-g gid         group id\n"
500				"\t-Y             update NIS maps\n",
501				"usage: pw groupmod [group|gid] [switches]\n"
502				"\t-V etcdir      alternate /etc location\n"
503				"\t-R rootir      alternate root directory\n"
504				"\t-C config      configuration file\n"
505				"\t-q             quiet operation\n"
506				"\t-F             force add if not exists\n"
507				"\t-n name        group name\n"
508				"\t-g gid         group id\n"
509				"\t-M usr1,usr2   replaces users as group members\n"
510				"\t-m usr1,usr2   add users as group members\n"
511				"\t-d usr1,usr2   delete users as group members\n"
512				"\t-l name        new group name\n"
513				"\t-Y             update NIS maps\n"
514				"\t-N             no update\n",
515				"usage: pw groupshow [group|gid] [switches]\n"
516				"\t-V etcdir      alternate /etc location\n"
517				"\t-R rootir      alternate root directory\n"
518				"\t-n name        group name\n"
519				"\t-g gid         group id\n"
520				"\t-F             force print\n"
521				"\t-P             prettier format\n"
522				"\t-a             print all accounting groups\n",
523				"usage: pw groupnext [switches]\n"
524				"\t-V etcdir      alternate /etc location\n"
525				"\t-R rootir      alternate root directory\n"
526				"\t-C config      configuration file\n"
527				"\t-q             quiet operation\n"
528			}
529		};
530
531		fprintf(stderr, "%s", help[which][mode]);
532	}
533	exit(EXIT_FAILURE);
534}
535
536struct carg    *
537getarg(struct cargs * _args, int ch)
538{
539	struct carg    *c = LIST_FIRST(_args);
540
541	while (c != NULL && c->ch != ch)
542		c = LIST_NEXT(c, list);
543	return c;
544}
545
546struct carg    *
547addarg(struct cargs * _args, int ch, char *argstr)
548{
549	struct carg    *ca = malloc(sizeof(struct carg));
550
551	if (ca == NULL)
552		errx(EX_OSERR, "out of memory");
553	ca->ch = ch;
554	ca->val = argstr;
555	LIST_INSERT_HEAD(_args, ca, list);
556	return ca;
557}
558