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