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