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