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