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