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