]> git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw.c
Be a little more strict about handling command line args. This allows user and
[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 <paths.h>
35 #include <sys/wait.h>
36 #include "pw.h"
37
38 #if !defined(_PATH_YP)
39 #define _PATH_YP "/var/yp/"
40 #endif
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 0,
59 setpwent,
60 endpwent,
61 getpwent,
62 getpwuid,
63 getpwnam,
64 pwdb,
65 setgrent,
66 endgrent,
67 getgrent,
68 getgrgid,
69 getgrnam,
70 grdb
71
72 };
73 struct pwf VPWF =
74 {
75 1,
76 vsetpwent,
77 vendpwent,
78 vgetpwent,
79 vgetpwuid,
80 vgetpwnam,
81 vpwdb,
82 vsetgrent,
83 vendgrent,
84 vgetgrent,
85 vgetgrgid,
86 vgetgrnam,
87 vgrdb
88 };
89
90 static struct cargs arglist;
91
92 static int getindex(const char *words[], const char *word);
93 static void cmdhelp(int mode, int which);
94
95
96 int
97 main(int argc, char *argv[])
98 {
99 int ch;
100 int mode = -1;
101 int which = -1;
102 char *config = NULL;
103 struct userconf *cnf;
104
105 static const char *opts[W_NUM][M_NUM] =
106 {
107 { /* user */
108 "V:C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db:NPy:Y",
109 "V:C:qn:u:rY",
110 "V:C:qn:u:c:d:e:p:g:G:ml:k:s:w:L:h:FNPY",
111 "V:C:qn:u:FPa7",
112 "V:C:q",
113 "V:C:q",
114 "V:C:q"
115 },
116 { /* grp */
117 "V:C:qn:g:h:M:pNPY",
118 "V:C:qn:g:Y",
119 "V:C:qn:g:l:h:FM:m:NPY",
120 "V:C:qn:g:FPa",
121 "V:C:q"
122 }
123 };
124
125 static int (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) =
126 { /* Request handlers */
127 pw_user,
128 pw_group
129 };
130
131 umask(0); /* We wish to handle this manually */
132 LIST_INIT(&arglist);
133
134 /*
135 * Break off the first couple of words to determine what exactly
136 * we're being asked to do
137 */
138 while (argc > 1) {
139 int tmp;
140
141 if (*argv[1] == '-') {
142 /*
143 * Special case, allow pw -V<dir> <operation> [args] for scripts etc.
144 */
145 if (argv[1][1] == 'V') {
146 optarg = &argv[1][2];
147 if (*optarg == '\0') {
148 optarg = argv[2];
149 ++argv;
150 --argc;
151 }
152 addarg(&arglist, 'V', optarg);
153 } else
154 break;
155 }
156 else if (mode == -1 && (tmp = getindex(Modes, argv[1])) != -1)
157 mode = tmp;
158 else if (which == -1 && (tmp = getindex(Which, argv[1])) != -1)
159 which = tmp;
160 else if ((mode == -1 && which == -1) &&
161 ((tmp = getindex(Combo1, argv[1])) != -1 ||
162 (tmp = getindex(Combo2, argv[1])) != -1)) {
163 which = tmp / M_NUM;
164 mode = tmp % M_NUM;
165 } else if (strcmp(argv[1], "help") == 0 && argv[2] == NULL)
166 cmdhelp(mode, which);
167 else if (which != -1 && mode != -1)
168 addarg(&arglist, 'n', argv[1]);
169 else
170 errx(EX_USAGE, "unknown keyword `%s'", argv[1]);
171 ++argv;
172 --argc;
173 }
174
175 /*
176 * Bail out unless the user is specific!
177 */
178 if (mode == -1 || which == -1)
179 cmdhelp(mode, which);
180
181 /*
182 * We know which mode we're in and what we're about to do, so now
183 * let's dispatch the remaining command line args in a genric way.
184 */
185 optarg = NULL;
186
187 while ((ch = getopt(argc, argv, opts[which][mode])) != -1) {
188 if (ch == '?')
189 errx(EX_USAGE, "unknown switch");
190 else
191 addarg(&arglist, ch, optarg);
192 optarg = NULL;
193 }
194
195 /*
196 * Must be root to attempt an update
197 */
198 if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && getarg(&arglist, 'N')==NULL)
199 errx(EX_NOPERM, "you must be root to run this program");
200
201 /*
202 * We should immediately look for the -q 'quiet' switch so that we
203 * don't bother with extraneous errors
204 */
205 if (getarg(&arglist, 'q') != NULL)
206 freopen("/dev/null", "w", stderr);
207
208 /*
209 * Set our base working path if not overridden
210 */
211
212 config = getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL;
213
214 if (getarg(&arglist, 'V') != NULL) {
215 char * etcpath = getarg(&arglist, 'V')->val;
216 if (*etcpath) {
217 if (config == NULL) { /* Only override config location if -C not specified */
218 config = malloc(MAXPATHLEN);
219 snprintf(config, MAXPATHLEN, "%s/pw.conf", etcpath);
220 }
221 memcpy(&PWF, &VPWF, sizeof PWF);
222 setpwdir(etcpath);
223 setgrdir(etcpath);
224 }
225 }
226
227 /*
228 * Now, let's do the common initialisation
229 */
230 cnf = read_userconfig(config);
231
232 ch = funcs[which] (cnf, mode, &arglist);
233
234 /*
235 * If everything went ok, and we've been asked to update
236 * the NIS maps, then do it now
237 */
238 if (ch == EXIT_SUCCESS && getarg(&arglist, 'Y') != NULL) {
239 pid_t pid;
240
241 fflush(NULL);
242 if (chdir(_PATH_YP) == -1)
243 warn("chdir(" _PATH_YP ")");
244 else if ((pid = fork()) == -1)
245 warn("fork()");
246 else if (pid == 0) {
247 /* Is make anywhere else? */
248 execlp("/usr/bin/make", "make", NULL);
249 _exit(1);
250 } else {
251 int i;
252 waitpid(pid, &i, 0);
253 if ((i = WEXITSTATUS(i)) != 0)
254 errx(ch, "make exited with status %d", i);
255 else
256 pw_log(cnf, mode, which, "NIS maps updated");
257 }
258 }
259 return ch;
260 }
261
262
263 static int
264 getindex(const char *words[], const char *word)
265 {
266 int i = 0;
267
268 while (words[i]) {
269 if (strcmp(words[i], word) == 0)
270 return i;
271 i++;
272 }
273 return -1;
274 }
275
276
277 /*
278 * This is probably an overkill for a cmdline help system, but it reflects
279 * the complexity of the command line.
280 */
281
282 static void
283 cmdhelp(int mode, int which)
284 {
285 if (which == -1)
286 fprintf(stderr, "usage:\n pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n");
287 else if (mode == -1)
288 fprintf(stderr, "usage:\n pw %s [add|del|mod|show|next] [help|switches/values]\n", Which[which]);
289 else {
290
291 /*
292 * We need to give mode specific help
293 */
294 static const char *help[W_NUM][M_NUM] =
295 {
296 {
297 "usage: pw useradd [name] [switches]\n"
298 "\t-V etcdir alternate /etc location\n"
299 "\t-C config configuration file\n"
300 "\t-q quiet operation\n"
301 " Adding users:\n"
302 "\t-n name login name\n"
303 "\t-u uid user id\n"
304 "\t-c comment user name/comment\n"
305 "\t-d directory home directory\n"
306 "\t-e date account expiry date\n"
307 "\t-p date password expiry date\n"
308 "\t-g grp initial group\n"
309 "\t-G grp1,grp2 additional groups\n"
310 "\t-m [ -k dir ] create and set up home\n"
311 "\t-s shell name of login shell\n"
312 "\t-o duplicate uid ok\n"
313 "\t-L class user class\n"
314 "\t-h fd read password on fd\n"
315 "\t-Y update NIS maps\n"
316 "\t-N no update\n"
317 " Setting defaults:\n"
318 "\t-V etcdir alternate /etc location\n"
319 "\t-D set user defaults\n"
320 "\t-b dir default home root dir\n"
321 "\t-e period default expiry period\n"
322 "\t-p period default password change period\n"
323 "\t-g group default group\n"
324 "\t-G grp1,grp2 additional groups\n"
325 "\t-L class default user class\n"
326 "\t-k dir default home skeleton\n"
327 "\t-u min,max set min,max uids\n"
328 "\t-i min,max set min,max gids\n"
329 "\t-w method set default password method\n"
330 "\t-s shell default shell\n"
331 "\t-y path set NIS passwd file path\n",
332 "usage: pw userdel [uid|name] [switches]\n"
333 "\t-V etcdir alternate /etc location\n"
334 "\t-n name login name\n"
335 "\t-u uid user id\n"
336 "\t-Y update NIS maps\n"
337 "\t-r remove home & contents\n",
338 "usage: pw usermod [uid|name] [switches]\n"
339 "\t-V etcdir alternate /etc location\n"
340 "\t-C config configuration file\n"
341 "\t-q quiet operation\n"
342 "\t-F force add if no user\n"
343 "\t-n name login name\n"
344 "\t-u uid user id\n"
345 "\t-c comment user name/comment\n"
346 "\t-d directory home directory\n"
347 "\t-e date account expiry date\n"
348 "\t-p date password expiry date\n"
349 "\t-g grp initial group\n"
350 "\t-G grp1,grp2 additional groups\n"
351 "\t-l name new login name\n"
352 "\t-L class user class\n"
353 "\t-m [ -k dir ] create and set up home\n"
354 "\t-s shell name of login shell\n"
355 "\t-w method set new password using method\n"
356 "\t-h fd read password on fd\n"
357 "\t-Y update NIS maps\n"
358 "\t-N no update\n",
359 "usage: pw usershow [uid|name] [switches]\n"
360 "\t-V etcdir alternate /etc location\n"
361 "\t-n name login name\n"
362 "\t-u uid user id\n"
363 "\t-F force print\n"
364 "\t-P prettier format\n"
365 "\t-a print all users\n"
366 "\t-7 print in v7 format\n",
367 "usage: pw usernext [switches]\n"
368 "\t-V etcdir alternate /etc location\n"
369 "\t-C config configuration file\n"
370 },
371 {
372 "usage: pw groupadd [group|gid] [switches]\n"
373 "\t-V etcdir alternate /etc location\n"
374 "\t-C config configuration file\n"
375 "\t-q quiet operation\n"
376 "\t-n group group name\n"
377 "\t-g gid group id\n"
378 "\t-M usr1,usr2 add users as group members\n"
379 "\t-o duplicate gid ok\n"
380 "\t-Y update NIS maps\n"
381 "\t-N no update\n",
382 "usage: pw groupdel [group|gid] [switches]\n"
383 "\t-V etcdir alternate /etc location\n"
384 "\t-n name group name\n"
385 "\t-g gid group id\n"
386 "\t-Y update NIS maps\n",
387 "usage: pw groupmod [group|gid] [switches]\n"
388 "\t-V etcdir alternate /etc location\n"
389 "\t-C config configuration file\n"
390 "\t-q quiet operation\n"
391 "\t-F force add if not exists\n"
392 "\t-n name group name\n"
393 "\t-g gid group id\n"
394 "\t-M usr1,usr2 replaces users as group members\n"
395 "\t-m usr1,usr2 add users as group members\n"
396 "\t-l name new group name\n"
397 "\t-Y update NIS maps\n"
398 "\t-N no update\n",
399 "usage: pw groupshow [group|gid] [switches]\n"
400 "\t-V etcdir alternate /etc location\n"
401 "\t-n name group name\n"
402 "\t-g gid group id\n"
403 "\t-F force print\n"
404 "\t-P prettier format\n"
405 "\t-a print all accounting groups\n",
406 "usage: pw groupnext [switches]\n"
407 "\t-V etcdir alternate /etc location\n"
408 "\t-C config configuration file\n"
409 }
410 };
411
412 fprintf(stderr, help[which][mode]);
413 }
414 exit(EXIT_FAILURE);
415 }
416
417 struct carg *
418 getarg(struct cargs * _args, int ch)
419 {
420 struct carg *c = _args->lh_first;
421
422 while (c != NULL && c->ch != ch)
423 c = c->list.le_next;
424 return c;
425 }
426
427 struct carg *
428 addarg(struct cargs * _args, int ch, char *argstr)
429 {
430 struct carg *ca = malloc(sizeof(struct carg));
431
432 if (ca == NULL)
433 errx(EX_OSERR, "out of memory");
434 ca->ch = ch;
435 ca->val = argstr;
436 LIST_INSERT_HEAD(_args, ca, list);
437 return ca;
438 }