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