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 mode = -1;
105 static int which = -1;
106
107 static int getindex(const char *words[], const char *word);
108 static void cmdhelp(int mode, int which);
109
110 int
main(int argc,char * argv[])111 main(int argc, char *argv[])
112 {
113 int 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' ?
164 _PATH_PWD : "");
165 } else
166 break;
167 }
168 else if (mode == -1 && (tmp = getindex(Modes, argv[1])) != -1)
169 mode = tmp;
170 else if (which == -1 && (tmp = getindex(Which, argv[1])) != -1)
171 which = tmp;
172 else if ((mode == -1 && which == -1) &&
173 ((tmp = getindex(Combo1, argv[1])) != -1 ||
174 (tmp = getindex(Combo2, argv[1])) != -1)) {
175 which = tmp / M_NUM;
176 mode = tmp % M_NUM;
177 } else if (strcmp(argv[1], "help") == 0 && argv[2] == NULL)
178 cmdhelp(mode, which);
179 else if (which != -1 && mode != -1)
180 arg1 = argv[1];
181 else
182 errx(EX_USAGE, "unknown keyword `%s'", argv[1]);
183 ++argv;
184 --argc;
185 }
186
187 /*
188 * Bail out unless the user is specific!
189 */
190 if (mode == -1 || which == -1)
191 cmdhelp(mode, which);
192
193 conf.rootfd = open(conf.rootdir, O_DIRECTORY|O_CLOEXEC);
194 if (conf.rootfd == -1)
195 errx(EXIT_FAILURE, "Unable to open '%s'", conf.rootdir);
196
197 return (cmdfunc[which][mode](argc, argv, arg1));
198 }
199
200
201 static int
getindex(const char * words[],const char * word)202 getindex(const char *words[], const char *word)
203 {
204 int i = 0;
205
206 while (words[i]) {
207 if (strcmp(words[i], word) == 0)
208 return (i);
209 i++;
210 }
211 return (-1);
212 }
213
214
215 /*
216 * This is probably an overkill for a cmdline help system, but it reflects
217 * the complexity of the command line.
218 */
219
220 static void
cmdhelp(int mode,int which)221 cmdhelp(int mode, int which)
222 {
223 if (which == -1)
224 fprintf(stderr, "usage:\n pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n");
225 else if (mode == -1)
226 fprintf(stderr, "usage:\n pw %s [add|del|mod|show|next] [help|switches/values]\n", Which[which]);
227 else {
228
229 /*
230 * We need to give mode specific help
231 */
232 static const char *help[W_NUM][M_NUM] =
233 {
234 {
235 "usage: pw useradd [name] [switches]\n"
236 "\t-V etcdir alternate /etc location\n"
237 "\t-R rootdir alternate root directory\n"
238 "\t-C config configuration file\n"
239 "\t-q quiet operation\n"
240 " Adding users:\n"
241 "\t-n name login name\n"
242 "\t-u uid user id\n"
243 "\t-c comment user name/comment\n"
244 "\t-d directory home directory\n"
245 "\t-e date account expiry date\n"
246 "\t-p date password expiry date\n"
247 "\t-g grp initial group\n"
248 "\t-G grp1,grp2 additional groups\n"
249 "\t-m [ -k dir ] create and set up home\n"
250 "\t-M mode home directory permissions\n"
251 "\t-s shell name of login shell\n"
252 "\t-o duplicate uid ok\n"
253 "\t-L class user class\n"
254 "\t-h fd read password on fd\n"
255 "\t-H fd read encrypted password on fd\n"
256 "\t-Y update NIS maps\n"
257 "\t-N no update\n"
258 " Setting defaults:\n"
259 "\t-V etcdir alternate /etc location\n"
260 "\t-R rootdir alternate root directory\n"
261 "\t-D set user defaults\n"
262 "\t-b dir default home root dir\n"
263 "\t-e period default expiry period\n"
264 "\t-p period default password change period\n"
265 "\t-g group default group\n"
266 "\t-G grp1,grp2 additional groups\n"
267 "\t-L class default user class\n"
268 "\t-k dir default home skeleton\n"
269 "\t-M mode home directory permissions\n"
270 "\t-u min,max set min,max uids\n"
271 "\t-i min,max set min,max gids\n"
272 "\t-w method set default password method\n"
273 "\t-s shell default shell\n"
274 "\t-y path set NIS passwd file path\n",
275 "usage: pw userdel [uid|name] [switches]\n"
276 "\t-V etcdir alternate /etc location\n"
277 "\t-R rootdir alternate root directory\n"
278 "\t-n name login name\n"
279 "\t-u uid user id\n"
280 "\t-Y update NIS maps\n"
281 "\t-y path set NIS passwd file path\n"
282 "\t-r remove home & contents\n",
283 "usage: pw usermod [uid|name] [switches]\n"
284 "\t-V etcdir alternate /etc location\n"
285 "\t-R rootdir alternate root directory\n"
286 "\t-C config configuration file\n"
287 "\t-q quiet operation\n"
288 "\t-F force add if no user\n"
289 "\t-n name login name\n"
290 "\t-u uid user id\n"
291 "\t-c comment user name/comment\n"
292 "\t-d directory home directory\n"
293 "\t-e date account expiry date\n"
294 "\t-p date password expiry date\n"
295 "\t-g grp initial group\n"
296 "\t-G grp1,grp2 additional groups\n"
297 "\t-l name new login name\n"
298 "\t-L class user class\n"
299 "\t-m [ -k dir ] create and set up home\n"
300 "\t-M mode home directory permissions\n"
301 "\t-s shell name of login shell\n"
302 "\t-w method set new password using method\n"
303 "\t-h fd read password on fd\n"
304 "\t-H fd read encrypted password on fd\n"
305 "\t-Y update NIS maps\n"
306 "\t-y path set NIS passwd file path\n"
307 "\t-N no update\n",
308 "usage: pw usershow [uid|name] [switches]\n"
309 "\t-V etcdir alternate /etc location\n"
310 "\t-R rootdir alternate root directory\n"
311 "\t-n name login name\n"
312 "\t-u uid user id\n"
313 "\t-F force print\n"
314 "\t-P prettier format\n"
315 "\t-a print all users\n"
316 "\t-7 print in v7 format\n",
317 "usage: pw usernext [switches]\n"
318 "\t-V etcdir alternate /etc location\n"
319 "\t-R rootdir alternate root directory\n"
320 "\t-C config configuration file\n"
321 "\t-q quiet operation\n",
322 "usage pw: lock [switches]\n"
323 "\t-V etcdir alternate /etc locations\n"
324 "\t-C config configuration file\n"
325 "\t-q quiet operation\n",
326 "usage pw: unlock [switches]\n"
327 "\t-V etcdir alternate /etc locations\n"
328 "\t-C config configuration file\n"
329 "\t-q quiet operation\n"
330 },
331 {
332 "usage: pw groupadd [group|gid] [switches]\n"
333 "\t-V etcdir alternate /etc location\n"
334 "\t-R rootdir alternate root directory\n"
335 "\t-C config configuration file\n"
336 "\t-q quiet operation\n"
337 "\t-n group group name\n"
338 "\t-g gid group id\n"
339 "\t-M usr1,usr2 add users as group members\n"
340 "\t-o duplicate gid ok\n"
341 "\t-Y update NIS maps\n"
342 "\t-N no update\n",
343 "usage: pw groupdel [group|gid] [switches]\n"
344 "\t-V etcdir alternate /etc location\n"
345 "\t-R rootdir alternate root directory\n"
346 "\t-n name group name\n"
347 "\t-g gid group id\n"
348 "\t-Y update NIS maps\n",
349 "usage: pw groupmod [group|gid] [switches]\n"
350 "\t-V etcdir alternate /etc location\n"
351 "\t-R rootdir alternate root directory\n"
352 "\t-C config configuration file\n"
353 "\t-q quiet operation\n"
354 "\t-F force add if not exists\n"
355 "\t-n name group name\n"
356 "\t-g gid group id\n"
357 "\t-M usr1,usr2 replaces users as group members\n"
358 "\t-m usr1,usr2 add users as group members\n"
359 "\t-d usr1,usr2 delete users as group members\n"
360 "\t-l name new group name\n"
361 "\t-Y update NIS maps\n"
362 "\t-N no update\n",
363 "usage: pw groupshow [group|gid] [switches]\n"
364 "\t-V etcdir alternate /etc location\n"
365 "\t-R rootdir alternate root directory\n"
366 "\t-n name group name\n"
367 "\t-g gid group id\n"
368 "\t-F force print\n"
369 "\t-P prettier format\n"
370 "\t-a print all accounting groups\n",
371 "usage: pw groupnext [switches]\n"
372 "\t-V etcdir alternate /etc location\n"
373 "\t-R rootdir alternate root directory\n"
374 "\t-C config configuration file\n"
375 "\t-q quiet operation\n"
376 }
377 };
378
379 fprintf(stderr, "%s", help[which][mode]);
380 }
381 exit(EX_USAGE);
382 }
383
384 void
usage(void)385 usage(void)
386 {
387 cmdhelp(mode, which);
388 }
389