xref: /freebsd/usr.sbin/pw/pw.c (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 1996
5  *	David L. Nugent.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef lint
30 static const char rcsid[] =
31   "$FreeBSD$";
32 #endif /* not lint */
33 
34 #include <err.h>
35 #include <fcntl.h>
36 #include <locale.h>
37 #include <string.h>
38 #include <sysexits.h>
39 #include <unistd.h>
40 
41 #include "pw.h"
42 
43 const char     *Modes[] = {
44   "add", "del", "mod", "show", "next",
45   NULL};
46 const char     *Which[] = {"user", "group", NULL};
47 static const char *Combo1[] = {
48   "useradd", "userdel", "usermod", "usershow", "usernext",
49   "lock", "unlock",
50   "groupadd", "groupdel", "groupmod", "groupshow", "groupnext",
51   NULL};
52 static const char *Combo2[] = {
53   "adduser", "deluser", "moduser", "showuser", "nextuser",
54   "lock", "unlock",
55   "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup",
56   NULL};
57 
58 struct pwf PWF =
59 {
60 	PWF_REGULAR,
61 	setpwent,
62 	endpwent,
63 	getpwent,
64 	getpwuid,
65 	getpwnam,
66 	setgrent,
67 	endgrent,
68 	getgrent,
69 	getgrgid,
70 	getgrnam,
71 
72 };
73 struct pwf VPWF =
74 {
75 	PWF_ALT,
76 	vsetpwent,
77 	vendpwent,
78 	vgetpwent,
79 	vgetpwuid,
80 	vgetpwnam,
81 	vsetgrent,
82 	vendgrent,
83 	vgetgrent,
84 	vgetgrgid,
85 	vgetgrnam,
86 };
87 
88 static int (*cmdfunc[W_NUM][M_NUM])(int argc, char **argv, char *_name) = {
89 	{ /* user */
90 		pw_user_add,
91 		pw_user_del,
92 		pw_user_mod,
93 		pw_user_show,
94 		pw_user_next,
95 		pw_user_lock,
96 		pw_user_unlock,
97 	},
98 	{ /* group */
99 		pw_group_add,
100 		pw_group_del,
101 		pw_group_mod,
102 		pw_group_show,
103 		pw_group_next,
104 	}
105 };
106 
107 struct pwconf conf;
108 
109 static int	getindex(const char *words[], const char *word);
110 static void	cmdhelp(int mode, int which);
111 
112 int
113 main(int argc, char *argv[])
114 {
115 	int		mode = -1, which = -1, tmp;
116 	struct stat	st;
117 	char		arg, *arg1;
118 	bool		relocated, nis;
119 
120 	arg1 = NULL;
121 	relocated = nis = false;
122 	memset(&conf, 0, sizeof(conf));
123 	strlcpy(conf.rootdir, "/", sizeof(conf.rootdir));
124 	strlcpy(conf.etcpath, _PATH_PWD, sizeof(conf.etcpath));
125 	conf.fd = -1;
126 	conf.checkduplicate = true;
127 
128 	setlocale(LC_ALL, "");
129 
130 	/*
131 	 * Break off the first couple of words to determine what exactly
132 	 * we're being asked to do
133 	 */
134 	while (argc > 1) {
135 		if (*argv[1] == '-') {
136 			/*
137 			 * Special case, allow pw -V<dir> <operation> [args] for scripts etc.
138 			 */
139 			arg = argv[1][1];
140 			if (arg == 'V' || arg == 'R') {
141 				if (relocated)
142 					errx(EXIT_FAILURE, "Both '-R' and '-V' "
143 					    "specified, only one accepted");
144 				relocated = true;
145 				optarg = &argv[1][2];
146 				if (*optarg == '\0') {
147 					if (stat(argv[2], &st) != 0)
148 						errx(EX_OSFILE, \
149 						    "no such directory `%s'",
150 						    argv[2]);
151 					if (!S_ISDIR(st.st_mode))
152 						errx(EX_OSFILE, "`%s' not a "
153 						    "directory", argv[2]);
154 					optarg = argv[2];
155 					++argv;
156 					--argc;
157 				}
158 				memcpy(&PWF, &VPWF, sizeof PWF);
159 				if (arg == 'R') {
160 					strlcpy(conf.rootdir, optarg,
161 					    sizeof(conf.rootdir));
162 					PWF._altdir = PWF_ROOTDIR;
163 				}
164 				snprintf(conf.etcpath, sizeof(conf.etcpath),
165 				    "%s%s", optarg, arg == 'R' ? "/etc" : "");
166 			} else
167 				break;
168 		}
169 		else if (mode == -1 && (tmp = getindex(Modes, argv[1])) != -1)
170 			mode = tmp;
171 		else if (which == -1 && (tmp = getindex(Which, argv[1])) != -1)
172 			which = tmp;
173 		else if ((mode == -1 && which == -1) &&
174 			 ((tmp = getindex(Combo1, argv[1])) != -1 ||
175 			  (tmp = getindex(Combo2, argv[1])) != -1)) {
176 			which = tmp / M_NUM;
177 			mode = tmp % M_NUM;
178 		} else if (strcmp(argv[1], "help") == 0 && argv[2] == NULL)
179 			cmdhelp(mode, which);
180 		else if (which != -1 && mode != -1)
181 				arg1 = argv[1];
182 		else
183 			errx(EX_USAGE, "unknown keyword `%s'", argv[1]);
184 		++argv;
185 		--argc;
186 	}
187 
188 	/*
189 	 * Bail out unless the user is specific!
190 	 */
191 	if (mode == -1 || which == -1)
192 		cmdhelp(mode, which);
193 
194 	conf.rootfd = open(conf.rootdir, O_DIRECTORY|O_CLOEXEC);
195 	if (conf.rootfd == -1)
196 		errx(EXIT_FAILURE, "Unable to open '%s'", conf.rootdir);
197 
198 	return (cmdfunc[which][mode](argc, argv, arg1));
199 }
200 
201 
202 static int
203 getindex(const char *words[], const char *word)
204 {
205 	int	i = 0;
206 
207 	while (words[i]) {
208 		if (strcmp(words[i], word) == 0)
209 			return (i);
210 		i++;
211 	}
212 	return (-1);
213 }
214 
215 
216 /*
217  * This is probably an overkill for a cmdline help system, but it reflects
218  * the complexity of the command line.
219  */
220 
221 static void
222 cmdhelp(int mode, int which)
223 {
224 	if (which == -1)
225 		fprintf(stderr, "usage:\n  pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n");
226 	else if (mode == -1)
227 		fprintf(stderr, "usage:\n  pw %s [add|del|mod|show|next] [help|switches/values]\n", Which[which]);
228 	else {
229 
230 		/*
231 		 * We need to give mode specific help
232 		 */
233 		static const char *help[W_NUM][M_NUM] =
234 		{
235 			{
236 				"usage: pw useradd [name] [switches]\n"
237 				"\t-V etcdir      alternate /etc location\n"
238 				"\t-R rootdir     alternate root directory\n"
239 				"\t-C config      configuration file\n"
240 				"\t-q             quiet operation\n"
241 				"  Adding users:\n"
242 				"\t-n name        login name\n"
243 				"\t-u uid         user id\n"
244 				"\t-c comment     user name/comment\n"
245 				"\t-d directory   home directory\n"
246 				"\t-e date        account expiry date\n"
247 				"\t-p date        password expiry date\n"
248 				"\t-g grp         initial group\n"
249 				"\t-G grp1,grp2   additional groups\n"
250 				"\t-m [ -k dir ]  create and set up home\n"
251 				"\t-M mode        home directory permissions\n"
252 				"\t-s shell       name of login shell\n"
253 				"\t-o             duplicate uid ok\n"
254 				"\t-L class       user class\n"
255 				"\t-h fd          read password on fd\n"
256 				"\t-H fd          read encrypted password on fd\n"
257 				"\t-Y             update NIS maps\n"
258 				"\t-N             no update\n"
259 				"  Setting defaults:\n"
260 				"\t-V etcdir      alternate /etc location\n"
261 				"\t-R rootdir     alternate root directory\n"
262 				"\t-D             set user defaults\n"
263 				"\t-b dir         default home root dir\n"
264 				"\t-e period      default expiry period\n"
265 				"\t-p period      default password change period\n"
266 				"\t-g group       default group\n"
267 				"\t-G grp1,grp2   additional groups\n"
268 				"\t-L class       default user class\n"
269 				"\t-k dir         default home skeleton\n"
270 				"\t-M mode        home directory permissions\n"
271 				"\t-u min,max     set min,max uids\n"
272 				"\t-i min,max     set min,max gids\n"
273 				"\t-w method      set default password method\n"
274 				"\t-s shell       default shell\n"
275 				"\t-y path        set NIS passwd file path\n",
276 				"usage: pw userdel [uid|name] [switches]\n"
277 				"\t-V etcdir      alternate /etc location\n"
278 				"\t-R rootdir     alternate root directory\n"
279 				"\t-n name        login name\n"
280 				"\t-u uid         user id\n"
281 				"\t-Y             update NIS maps\n"
282 				"\t-y path        set NIS passwd file path\n"
283 				"\t-r             remove home & contents\n",
284 				"usage: pw usermod [uid|name] [switches]\n"
285 				"\t-V etcdir      alternate /etc location\n"
286 				"\t-R rootdir     alternate root directory\n"
287 				"\t-C config      configuration file\n"
288 				"\t-q             quiet operation\n"
289 				"\t-F             force add if no user\n"
290 				"\t-n name        login name\n"
291 				"\t-u uid         user id\n"
292 				"\t-c comment     user name/comment\n"
293 				"\t-d directory   home directory\n"
294 				"\t-e date        account expiry date\n"
295 				"\t-p date        password expiry date\n"
296 				"\t-g grp         initial group\n"
297 				"\t-G grp1,grp2   additional groups\n"
298 				"\t-l name        new login name\n"
299 				"\t-L class       user class\n"
300 				"\t-m [ -k dir ]  create and set up home\n"
301 				"\t-M mode        home directory permissions\n"
302 				"\t-s shell       name of login shell\n"
303 				"\t-w method      set new password using method\n"
304 				"\t-h fd          read password on fd\n"
305 				"\t-H fd          read encrypted password on fd\n"
306 				"\t-Y             update NIS maps\n"
307 				"\t-y path        set NIS passwd file path\n"
308 				"\t-N             no update\n",
309 				"usage: pw usershow [uid|name] [switches]\n"
310 				"\t-V etcdir      alternate /etc location\n"
311 				"\t-R rootdir     alternate root directory\n"
312 				"\t-n name        login name\n"
313 				"\t-u uid         user id\n"
314 				"\t-F             force print\n"
315 				"\t-P             prettier format\n"
316 				"\t-a             print all users\n"
317 				"\t-7             print in v7 format\n",
318 				"usage: pw usernext [switches]\n"
319 				"\t-V etcdir      alternate /etc location\n"
320 				"\t-R rootdir     alternate root directory\n"
321 				"\t-C config      configuration file\n"
322 				"\t-q             quiet operation\n",
323 				"usage pw: lock [switches]\n"
324 				"\t-V etcdir      alternate /etc locations\n"
325 				"\t-C config      configuration file\n"
326 				"\t-q             quiet operation\n",
327 				"usage pw: unlock [switches]\n"
328 				"\t-V etcdir      alternate /etc locations\n"
329 				"\t-C config      configuration file\n"
330 				"\t-q             quiet operation\n"
331 			},
332 			{
333 				"usage: pw groupadd [group|gid] [switches]\n"
334 				"\t-V etcdir      alternate /etc location\n"
335 				"\t-R rootdir     alternate root directory\n"
336 				"\t-C config      configuration file\n"
337 				"\t-q             quiet operation\n"
338 				"\t-n group       group name\n"
339 				"\t-g gid         group id\n"
340 				"\t-M usr1,usr2   add users as group members\n"
341 				"\t-o             duplicate gid ok\n"
342 				"\t-Y             update NIS maps\n"
343 				"\t-N             no update\n",
344 				"usage: pw groupdel [group|gid] [switches]\n"
345 				"\t-V etcdir      alternate /etc location\n"
346 				"\t-R rootdir     alternate root directory\n"
347 				"\t-n name        group name\n"
348 				"\t-g gid         group id\n"
349 				"\t-Y             update NIS maps\n",
350 				"usage: pw groupmod [group|gid] [switches]\n"
351 				"\t-V etcdir      alternate /etc location\n"
352 				"\t-R rootdir     alternate root directory\n"
353 				"\t-C config      configuration file\n"
354 				"\t-q             quiet operation\n"
355 				"\t-F             force add if not exists\n"
356 				"\t-n name        group name\n"
357 				"\t-g gid         group id\n"
358 				"\t-M usr1,usr2   replaces users as group members\n"
359 				"\t-m usr1,usr2   add users as group members\n"
360 				"\t-d usr1,usr2   delete users as group members\n"
361 				"\t-l name        new group name\n"
362 				"\t-Y             update NIS maps\n"
363 				"\t-N             no update\n",
364 				"usage: pw groupshow [group|gid] [switches]\n"
365 				"\t-V etcdir      alternate /etc location\n"
366 				"\t-R rootdir     alternate root directory\n"
367 				"\t-n name        group name\n"
368 				"\t-g gid         group id\n"
369 				"\t-F             force print\n"
370 				"\t-P             prettier format\n"
371 				"\t-a             print all accounting groups\n",
372 				"usage: pw groupnext [switches]\n"
373 				"\t-V etcdir      alternate /etc location\n"
374 				"\t-R rootdir     alternate root directory\n"
375 				"\t-C config      configuration file\n"
376 				"\t-q             quiet operation\n"
377 			}
378 		};
379 
380 		fprintf(stderr, "%s", help[which][mode]);
381 	}
382 	exit(EXIT_FAILURE);
383 }
384