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