]> git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw.c
Refactor input validation
[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 #if !defined(_PATH_YP)
41 #define _PATH_YP "/var/yp/"
42 #endif
43 const char *Modes[] = {
44 "add", "del", "mod", "show", "next",
45 NULL};
46 const char *Which[] = {"user", "group", NULL};
47 static const char *Combo1[] = {
48 "useradd", "userdel", "usermod", "usershow", "usernext",
49 "lock", "unlock",
50 "groupadd", "groupdel", "groupmod", "groupshow", "groupnext",
51 NULL};
52 static const char *Combo2[] = {
53 "adduser", "deluser", "moduser", "showuser", "nextuser",
54 "lock", "unlock",
55 "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup",
56 NULL};
57
58 struct pwf PWF =
59 {
60 PWF_REGULAR,
61 setpwent,
62 endpwent,
63 getpwent,
64 getpwuid,
65 getpwnam,
66 setgrent,
67 endgrent,
68 getgrent,
69 getgrgid,
70 getgrnam,
71
72 };
73 struct pwf VPWF =
74 {
75 PWF_ALT,
76 vsetpwent,
77 vendpwent,
78 vgetpwent,
79 vgetpwuid,
80 vgetpwnam,
81 vsetgrent,
82 vendgrent,
83 vgetgrent,
84 vgetgrgid,
85 vgetgrnam,
86 };
87
88 struct pwconf conf;
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 long id = -1;
103 char *config = NULL;
104 struct stat st;
105 const char *errstr;
106 char arg, *name;
107 bool relocated, nis;
108
109 static const char *opts[W_NUM][M_NUM] =
110 {
111 { /* user */
112 "R:V:C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y",
113 "R:V:C:qn:u:rY",
114 "R:V:C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:FNPY",
115 "R:V:C:qn:u:FPa7",
116 "R:V:C:q",
117 "R:V:C:q",
118 "R:V:C:q"
119 },
120 { /* grp */
121 "R:V:C:qn:g:h:H:M:opNPY",
122 "R:V:C:qn:g:Y",
123 "R:V:C:qn:d:g:l:h:H:FM:m:NPY",
124 "R:V:C:qn:g:FPa",
125 "R:V:C:q"
126 }
127 };
128
129 static int (*funcs[W_NUM]) (int _mode, char *_name, long _id,
130 struct cargs * _args) =
131 { /* Request handlers */
132 pw_user,
133 pw_group
134 };
135
136 name = NULL;
137 relocated = nis = false;
138 memset(&conf, 0, sizeof(conf));
139 strlcpy(conf.etcpath, _PATH_PWD, sizeof(conf.etcpath));
140
141 LIST_INIT(&arglist);
142
143 (void)setlocale(LC_ALL, "");
144
145 /*
146 * Break off the first couple of words to determine what exactly
147 * we're being asked to do
148 */
149 while (argc > 1) {
150 int tmp;
151
152 if (*argv[1] == '-') {
153 /*
154 * Special case, allow pw -V<dir> <operation> [args] for scripts etc.
155 */
156 arg = argv[1][1];
157 if (arg == 'V' || arg == 'R') {
158 if (relocated)
159 errx(EXIT_FAILURE, "Both '-R' and '-V' "
160 "specified, only one accepted");
161 relocated = true;
162 optarg = &argv[1][2];
163 if (*optarg == '\0') {
164 if (stat(argv[2], &st) != 0)
165 errx(EX_OSFILE, \
166 "no such directory `%s'",
167 argv[2]);
168 if (!S_ISDIR(st.st_mode))
169 errx(EX_OSFILE, "`%s' not a "
170 "directory", argv[2]);
171 optarg = argv[2];
172 ++argv;
173 --argc;
174 }
175 memcpy(&PWF, &VPWF, sizeof PWF);
176 if (arg == 'R') {
177 strlcpy(conf.rootdir, optarg,
178 sizeof(conf.rootdir));
179 PWF._altdir = PWF_ROOTDIR;
180 }
181 snprintf(conf.etcpath, sizeof(conf.etcpath),
182 "%s%s", optarg, arg == 'R' ? "/etc" : "");
183 } else
184 break;
185 }
186 else if (mode == -1 && (tmp = getindex(Modes, argv[1])) != -1)
187 mode = tmp;
188 else if (which == -1 && (tmp = getindex(Which, argv[1])) != -1)
189 which = tmp;
190 else if ((mode == -1 && which == -1) &&
191 ((tmp = getindex(Combo1, argv[1])) != -1 ||
192 (tmp = getindex(Combo2, argv[1])) != -1)) {
193 which = tmp / M_NUM;
194 mode = tmp % M_NUM;
195 } else if (strcmp(argv[1], "help") == 0 && argv[2] == NULL)
196 cmdhelp(mode, which);
197 else if (which != -1 && mode != -1) {
198 if (strspn(argv[1], "0123456789") == strlen(argv[1])) {
199 id = strtonum(argv[1], 0, LONG_MAX, &errstr);
200 if (errstr != NULL)
201 errx(EX_USAGE, "Bad id '%s': %s",
202 argv[1], errstr);
203 } else
204 name = argv[1];
205 } else
206 errx(EX_USAGE, "unknown keyword `%s'", argv[1]);
207 ++argv;
208 --argc;
209 }
210
211 /*
212 * Bail out unless the user is specific!
213 */
214 if (mode == -1 || which == -1)
215 cmdhelp(mode, which);
216
217 /*
218 * We know which mode we're in and what we're about to do, so now
219 * let's dispatch the remaining command line args in a genric way.
220 */
221 optarg = NULL;
222
223 while ((ch = getopt(argc, argv, opts[which][mode])) != -1) {
224 switch (ch) {
225 case '?':
226 errx(EX_USAGE, "unknown switch");
227 break;
228 case '7':
229 conf.v7 = true;
230 break;
231 case 'C':
232 config = optarg;
233 break;
234 case 'N':
235 conf.dryrun = true;
236 break;
237 case 'P':
238 conf.pretty = true;
239 break;
240 case 'Y':
241 nis = true;
242 break;
243 case 'g':
244 if (which == 0) { /* for user* */
245 addarg(&arglist, 'g', optarg);
246 break;
247 }
248 /* FALLTHROUGH */
249 case 'u':
250 if (strspn(optarg, "0123456789") != strlen(optarg))
251 errx(EX_USAGE, "%s expects a number",
252 which == 1 ? "-g" : "-u" );
253 id = strtonum(optarg, 0, LONG_MAX, &errstr);
254 if (errstr != NULL)
255 errx(EX_USAGE, "Bad id '%s': %s", optarg,
256 errstr);
257 break;
258 case 'n':
259 if (strspn(optarg, "0123456789") != strlen(optarg)) {
260 name = optarg;
261 break;
262 }
263 id = strtonum(optarg, 0, LONG_MAX, &errstr);
264 if (errstr != NULL)
265 errx(EX_USAGE, "Bad id '%s': %s", optarg,
266 errstr);
267 default:
268 addarg(&arglist, ch, optarg);
269 break;
270 }
271 optarg = NULL;
272 }
273
274 if (name != NULL && strlen(name) >= MAXLOGNAME)
275 errx(EX_USAGE, "name too long: %s", name);
276
277 /*
278 * Must be root to attempt an update
279 */
280 if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && !conf.dryrun)
281 errx(EX_NOPERM, "you must be root to run this program");
282
283 /*
284 * We should immediately look for the -q 'quiet' switch so that we
285 * don't bother with extraneous errors
286 */
287 if (getarg(&arglist, 'q') != NULL)
288 freopen(_PATH_DEVNULL, "w", stderr);
289
290 /*
291 * Set our base working path if not overridden
292 */
293
294 if (config == NULL) { /* Only override config location if -C not specified */
295 asprintf(&config, "%s/pw.conf", conf.etcpath);
296 if (config == NULL)
297 errx(EX_OSERR, "out of memory");
298 }
299
300 /*
301 * Now, let's do the common initialisation
302 */
303 conf.userconf = read_userconfig(config);
304
305 ch = funcs[which] (mode, name, id, &arglist);
306
307 /*
308 * If everything went ok, and we've been asked to update
309 * the NIS maps, then do it now
310 */
311 if (ch == EXIT_SUCCESS && nis) {
312 pid_t pid;
313
314 fflush(NULL);
315 if (chdir(_PATH_YP) == -1)
316 warn("chdir(" _PATH_YP ")");
317 else if ((pid = fork()) == -1)
318 warn("fork()");
319 else if (pid == 0) {
320 /* Is make anywhere else? */
321 execlp("/usr/bin/make", "make", (char *)NULL);
322 _exit(1);
323 } else {
324 int i;
325 waitpid(pid, &i, 0);
326 if ((i = WEXITSTATUS(i)) != 0)
327 errx(ch, "make exited with status %d", i);
328 else
329 pw_log(conf.userconf, mode, which, "NIS maps updated");
330 }
331 }
332 return ch;
333 }
334
335
336 static int
337 getindex(const char *words[], const char *word)
338 {
339 int i = 0;
340
341 while (words[i]) {
342 if (strcmp(words[i], word) == 0)
343 return i;
344 i++;
345 }
346 return -1;
347 }
348
349
350 /*
351 * This is probably an overkill for a cmdline help system, but it reflects
352 * the complexity of the command line.
353 */
354
355 static void
356 cmdhelp(int mode, int which)
357 {
358 if (which == -1)
359 fprintf(stderr, "usage:\n pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n");
360 else if (mode == -1)
361 fprintf(stderr, "usage:\n pw %s [add|del|mod|show|next] [help|switches/values]\n", Which[which]);
362 else {
363
364 /*
365 * We need to give mode specific help
366 */
367 static const char *help[W_NUM][M_NUM] =
368 {
369 {
370 "usage: pw useradd [name] [switches]\n"
371 "\t-V etcdir alternate /etc location\n"
372 "\t-R rootir alternate root directory\n"
373 "\t-C config configuration file\n"
374 "\t-q quiet operation\n"
375 " Adding users:\n"
376 "\t-n name login name\n"
377 "\t-u uid user id\n"
378 "\t-c comment user name/comment\n"
379 "\t-d directory home directory\n"
380 "\t-e date account expiry date\n"
381 "\t-p date password expiry date\n"
382 "\t-g grp initial group\n"
383 "\t-G grp1,grp2 additional groups\n"
384 "\t-m [ -k dir ] create and set up home\n"
385 "\t-M mode home directory permissions\n"
386 "\t-s shell name of login shell\n"
387 "\t-o duplicate uid ok\n"
388 "\t-L class user class\n"
389 "\t-h fd read password on fd\n"
390 "\t-H fd read encrypted password on fd\n"
391 "\t-Y update NIS maps\n"
392 "\t-N no update\n"
393 " Setting defaults:\n"
394 "\t-V etcdir alternate /etc location\n"
395 "\t-R rootir alternate root directory\n"
396 "\t-D set user defaults\n"
397 "\t-b dir default home root dir\n"
398 "\t-e period default expiry period\n"
399 "\t-p period default password change period\n"
400 "\t-g group default group\n"
401 "\t-G grp1,grp2 additional groups\n"
402 "\t-L class default user class\n"
403 "\t-k dir default home skeleton\n"
404 "\t-M mode home directory permissions\n"
405 "\t-u min,max set min,max uids\n"
406 "\t-i min,max set min,max gids\n"
407 "\t-w method set default password method\n"
408 "\t-s shell default shell\n"
409 "\t-y path set NIS passwd file path\n",
410 "usage: pw userdel [uid|name] [switches]\n"
411 "\t-V etcdir alternate /etc location\n"
412 "\t-R rootir alternate root directory\n"
413 "\t-n name login name\n"
414 "\t-u uid user id\n"
415 "\t-Y update NIS maps\n"
416 "\t-r remove home & contents\n",
417 "usage: pw usermod [uid|name] [switches]\n"
418 "\t-V etcdir alternate /etc location\n"
419 "\t-R rootir alternate root directory\n"
420 "\t-C config configuration file\n"
421 "\t-q quiet operation\n"
422 "\t-F force add if no user\n"
423 "\t-n name login name\n"
424 "\t-u uid user id\n"
425 "\t-c comment user name/comment\n"
426 "\t-d directory home directory\n"
427 "\t-e date account expiry date\n"
428 "\t-p date password expiry date\n"
429 "\t-g grp initial group\n"
430 "\t-G grp1,grp2 additional groups\n"
431 "\t-l name new login name\n"
432 "\t-L class user class\n"
433 "\t-m [ -k dir ] create and set up home\n"
434 "\t-M mode home directory permissions\n"
435 "\t-s shell name of login shell\n"
436 "\t-w method set new password using method\n"
437 "\t-h fd read password on fd\n"
438 "\t-H fd read encrypted password on fd\n"
439 "\t-Y update NIS maps\n"
440 "\t-N no update\n",
441 "usage: pw usershow [uid|name] [switches]\n"
442 "\t-V etcdir alternate /etc location\n"
443 "\t-R rootir alternate root directory\n"
444 "\t-n name login name\n"
445 "\t-u uid user id\n"
446 "\t-F force print\n"
447 "\t-P prettier format\n"
448 "\t-a print all users\n"
449 "\t-7 print in v7 format\n",
450 "usage: pw usernext [switches]\n"
451 "\t-V etcdir alternate /etc location\n"
452 "\t-R rootir alternate root directory\n"
453 "\t-C config configuration file\n"
454 "\t-q quiet operation\n",
455 "usage pw: lock [switches]\n"
456 "\t-V etcdir alternate /etc locations\n"
457 "\t-C config configuration file\n"
458 "\t-q quiet operation\n",
459 "usage pw: unlock [switches]\n"
460 "\t-V etcdir alternate /etc locations\n"
461 "\t-C config configuration file\n"
462 "\t-q quiet operation\n"
463 },
464 {
465 "usage: pw groupadd [group|gid] [switches]\n"
466 "\t-V etcdir alternate /etc location\n"
467 "\t-R rootir alternate root directory\n"
468 "\t-C config configuration file\n"
469 "\t-q quiet operation\n"
470 "\t-n group group name\n"
471 "\t-g gid group id\n"
472 "\t-M usr1,usr2 add users as group members\n"
473 "\t-o duplicate gid ok\n"
474 "\t-Y update NIS maps\n"
475 "\t-N no update\n",
476 "usage: pw groupdel [group|gid] [switches]\n"
477 "\t-V etcdir alternate /etc location\n"
478 "\t-R rootir alternate root directory\n"
479 "\t-n name group name\n"
480 "\t-g gid group id\n"
481 "\t-Y update NIS maps\n",
482 "usage: pw groupmod [group|gid] [switches]\n"
483 "\t-V etcdir alternate /etc location\n"
484 "\t-R rootir alternate root directory\n"
485 "\t-C config configuration file\n"
486 "\t-q quiet operation\n"
487 "\t-F force add if not exists\n"
488 "\t-n name group name\n"
489 "\t-g gid group id\n"
490 "\t-M usr1,usr2 replaces users as group members\n"
491 "\t-m usr1,usr2 add users as group members\n"
492 "\t-d usr1,usr2 delete users as group members\n"
493 "\t-l name new group name\n"
494 "\t-Y update NIS maps\n"
495 "\t-N no update\n",
496 "usage: pw groupshow [group|gid] [switches]\n"
497 "\t-V etcdir alternate /etc location\n"
498 "\t-R rootir alternate root directory\n"
499 "\t-n name group name\n"
500 "\t-g gid group id\n"
501 "\t-F force print\n"
502 "\t-P prettier format\n"
503 "\t-a print all accounting groups\n",
504 "usage: pw groupnext [switches]\n"
505 "\t-V etcdir alternate /etc location\n"
506 "\t-R rootir alternate root directory\n"
507 "\t-C config configuration file\n"
508 "\t-q quiet operation\n"
509 }
510 };
511
512 fprintf(stderr, "%s", help[which][mode]);
513 }
514 exit(EXIT_FAILURE);
515 }
516
517 struct carg *
518 getarg(struct cargs * _args, int ch)
519 {
520 struct carg *c = LIST_FIRST(_args);
521
522 while (c != NULL && c->ch != ch)
523 c = LIST_NEXT(c, list);
524 return c;
525 }
526
527 struct carg *
528 addarg(struct cargs * _args, int ch, char *argstr)
529 {
530 struct carg *ca = malloc(sizeof(struct carg));
531
532 if (ca == NULL)
533 errx(EX_OSERR, "out of memory");
534 ca->ch = ch;
535 ca->val = argstr;
536 LIST_INSERT_HEAD(_args, ca, list);
537 return ca;
538 }