]> git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw.c
homedir can only be populate during useradd
[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 conf.fd = -1;
141
142 LIST_INIT(&arglist);
143
144 (void)setlocale(LC_ALL, "");
145
146 /*
147 * Break off the first couple of words to determine what exactly
148 * we're being asked to do
149 */
150 while (argc > 1) {
151 int tmp;
152
153 if (*argv[1] == '-') {
154 /*
155 * Special case, allow pw -V<dir> <operation> [args] for scripts etc.
156 */
157 arg = argv[1][1];
158 if (arg == 'V' || arg == 'R') {
159 if (relocated)
160 errx(EXIT_FAILURE, "Both '-R' and '-V' "
161 "specified, only one accepted");
162 relocated = true;
163 optarg = &argv[1][2];
164 if (*optarg == '\0') {
165 if (stat(argv[2], &st) != 0)
166 errx(EX_OSFILE, \
167 "no such directory `%s'",
168 argv[2]);
169 if (!S_ISDIR(st.st_mode))
170 errx(EX_OSFILE, "`%s' not a "
171 "directory", argv[2]);
172 optarg = argv[2];
173 ++argv;
174 --argc;
175 }
176 memcpy(&PWF, &VPWF, sizeof PWF);
177 if (arg == 'R') {
178 strlcpy(conf.rootdir, optarg,
179 sizeof(conf.rootdir));
180 PWF._altdir = PWF_ROOTDIR;
181 }
182 snprintf(conf.etcpath, sizeof(conf.etcpath),
183 "%s%s", optarg, arg == 'R' ? "/etc" : "");
184 } else
185 break;
186 }
187 else if (mode == -1 && (tmp = getindex(Modes, argv[1])) != -1)
188 mode = tmp;
189 else if (which == -1 && (tmp = getindex(Which, argv[1])) != -1)
190 which = tmp;
191 else if ((mode == -1 && which == -1) &&
192 ((tmp = getindex(Combo1, argv[1])) != -1 ||
193 (tmp = getindex(Combo2, argv[1])) != -1)) {
194 which = tmp / M_NUM;
195 mode = tmp % M_NUM;
196 } else if (strcmp(argv[1], "help") == 0 && argv[2] == NULL)
197 cmdhelp(mode, which);
198 else if (which != -1 && mode != -1) {
199 if (strspn(argv[1], "0123456789") == strlen(argv[1])) {
200 id = strtonum(argv[1], 0, LONG_MAX, &errstr);
201 if (errstr != NULL)
202 errx(EX_USAGE, "Bad id '%s': %s",
203 argv[1], errstr);
204 } else
205 name = argv[1];
206 } else
207 errx(EX_USAGE, "unknown keyword `%s'", argv[1]);
208 ++argv;
209 --argc;
210 }
211
212 /*
213 * Bail out unless the user is specific!
214 */
215 if (mode == -1 || which == -1)
216 cmdhelp(mode, which);
217
218 conf.which = which;
219 /*
220 * We know which mode we're in and what we're about to do, so now
221 * let's dispatch the remaining command line args in a genric way.
222 */
223 optarg = NULL;
224
225 while ((ch = getopt(argc, argv, opts[which][mode])) != -1) {
226 switch (ch) {
227 case '?':
228 errx(EX_USAGE, "unknown switch");
229 break;
230 case '7':
231 conf.v7 = true;
232 break;
233 case 'C':
234 conf.config = optarg;
235 config = conf.config;
236 break;
237 case 'F':
238 conf.force = true;
239 break;
240 case 'N':
241 conf.dryrun = true;
242 break;
243 case 'l':
244 if (strlen(optarg) >= MAXLOGNAME)
245 errx(EX_USAGE, "new name too long: %s", optarg);
246 conf.newname = optarg;
247 break;
248 case 'P':
249 conf.pretty = true;
250 break;
251 case 'Y':
252 nis = true;
253 break;
254 case 'a':
255 conf.all = true;
256 break;
257 case 'g':
258 if (which == 0) { /* for user* */
259 addarg(&arglist, 'g', optarg);
260 break;
261 }
262 if (strspn(optarg, "0123456789") != strlen(optarg))
263 errx(EX_USAGE, "-g expects a number");
264 id = strtonum(optarg, 0, LONG_MAX, &errstr);
265 if (errstr != NULL)
266 errx(EX_USAGE, "Bad id '%s': %s", optarg,
267 errstr);
268 break;
269 case 'u':
270 if (strspn(optarg, "0123456789,") != strlen(optarg))
271 errx(EX_USAGE, "-u expects a number");
272 if (strchr(optarg, ',') != NULL) {
273 addarg(&arglist, 'u', optarg);
274 break;
275 }
276 id = strtonum(optarg, 0, LONG_MAX, &errstr);
277 if (errstr != NULL)
278 errx(EX_USAGE, "Bad id '%s': %s", optarg,
279 errstr);
280 break;
281 case 'n':
282 if (strspn(optarg, "0123456789") != strlen(optarg)) {
283 name = optarg;
284 break;
285 }
286 id = strtonum(optarg, 0, LONG_MAX, &errstr);
287 if (errstr != NULL)
288 errx(EX_USAGE, "Bad id '%s': %s", optarg,
289 errstr);
290 break;
291 case 'H':
292 if (conf.fd != -1)
293 errx(EX_USAGE, "'-h' and '-H' are mutually "
294 "exclusive options");
295 conf.precrypted = true;
296 if (strspn(optarg, "0123456789") != strlen(optarg))
297 errx(EX_USAGE, "'-H' expects a file descriptor");
298
299 conf.fd = strtonum(optarg, 0, INT_MAX, &errstr);
300 if (errstr != NULL)
301 errx(EX_USAGE, "Bad file descriptor '%s': %s",
302 optarg, errstr);
303 break;
304 case 'h':
305 if (conf.fd != -1)
306 errx(EX_USAGE, "'-h' and '-H' are mutually "
307 "exclusive options");
308
309 if (strcmp(optarg, "-") == 0)
310 conf.fd = '-';
311 else if (strspn(optarg, "0123456789") == strlen(optarg)) {
312 conf.fd = strtonum(optarg, 0, INT_MAX, &errstr);
313 if (errstr != NULL)
314 errx(EX_USAGE, "'-h' expects a "
315 "file descriptor or '-'");
316 } else
317 errx(EX_USAGE, "'-h' expects a file "
318 "descriptor or '-'");
319 break;
320 case 'o':
321 conf.checkduplicate = true;
322 break;
323 case 'q':
324 conf.quiet = true;
325 break;
326 case 'r':
327 conf.deletehome = true;
328 break;
329 default:
330 addarg(&arglist, ch, optarg);
331 break;
332 }
333 optarg = NULL;
334 }
335
336 if (name != NULL && strlen(name) >= MAXLOGNAME)
337 errx(EX_USAGE, "name too long: %s", name);
338
339 /*
340 * Must be root to attempt an update
341 */
342 if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && !conf.dryrun)
343 errx(EX_NOPERM, "you must be root to run this program");
344
345 /*
346 * We should immediately look for the -q 'quiet' switch so that we
347 * don't bother with extraneous errors
348 */
349 if (conf.quiet)
350 freopen(_PATH_DEVNULL, "w", stderr);
351
352 /*
353 * Set our base working path if not overridden
354 */
355
356 if (config == NULL) { /* Only override config location if -C not specified */
357 asprintf(&config, "%s/pw.conf", conf.etcpath);
358 if (config == NULL)
359 errx(EX_OSERR, "out of memory");
360 }
361
362 /*
363 * Now, let's do the common initialisation
364 */
365 conf.userconf = read_userconfig(config);
366
367 ch = funcs[which] (mode, name, id, &arglist);
368
369 /*
370 * If everything went ok, and we've been asked to update
371 * the NIS maps, then do it now
372 */
373 if (ch == EXIT_SUCCESS && nis) {
374 pid_t pid;
375
376 fflush(NULL);
377 if (chdir(_PATH_YP) == -1)
378 warn("chdir(" _PATH_YP ")");
379 else if ((pid = fork()) == -1)
380 warn("fork()");
381 else if (pid == 0) {
382 /* Is make anywhere else? */
383 execlp("/usr/bin/make", "make", (char *)NULL);
384 _exit(1);
385 } else {
386 int i;
387 waitpid(pid, &i, 0);
388 if ((i = WEXITSTATUS(i)) != 0)
389 errx(ch, "make exited with status %d", i);
390 else
391 pw_log(conf.userconf, mode, which, "NIS maps updated");
392 }
393 }
394 return ch;
395 }
396
397
398 static int
399 getindex(const char *words[], const char *word)
400 {
401 int i = 0;
402
403 while (words[i]) {
404 if (strcmp(words[i], word) == 0)
405 return i;
406 i++;
407 }
408 return -1;
409 }
410
411
412 /*
413 * This is probably an overkill for a cmdline help system, but it reflects
414 * the complexity of the command line.
415 */
416
417 static void
418 cmdhelp(int mode, int which)
419 {
420 if (which == -1)
421 fprintf(stderr, "usage:\n pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n");
422 else if (mode == -1)
423 fprintf(stderr, "usage:\n pw %s [add|del|mod|show|next] [help|switches/values]\n", Which[which]);
424 else {
425
426 /*
427 * We need to give mode specific help
428 */
429 static const char *help[W_NUM][M_NUM] =
430 {
431 {
432 "usage: pw useradd [name] [switches]\n"
433 "\t-V etcdir alternate /etc location\n"
434 "\t-R rootir alternate root directory\n"
435 "\t-C config configuration file\n"
436 "\t-q quiet operation\n"
437 " Adding users:\n"
438 "\t-n name login name\n"
439 "\t-u uid user id\n"
440 "\t-c comment user name/comment\n"
441 "\t-d directory home directory\n"
442 "\t-e date account expiry date\n"
443 "\t-p date password expiry date\n"
444 "\t-g grp initial group\n"
445 "\t-G grp1,grp2 additional groups\n"
446 "\t-m [ -k dir ] create and set up home\n"
447 "\t-M mode home directory permissions\n"
448 "\t-s shell name of login shell\n"
449 "\t-o duplicate uid ok\n"
450 "\t-L class user class\n"
451 "\t-h fd read password on fd\n"
452 "\t-H fd read encrypted password on fd\n"
453 "\t-Y update NIS maps\n"
454 "\t-N no update\n"
455 " Setting defaults:\n"
456 "\t-V etcdir alternate /etc location\n"
457 "\t-R rootir alternate root directory\n"
458 "\t-D set user defaults\n"
459 "\t-b dir default home root dir\n"
460 "\t-e period default expiry period\n"
461 "\t-p period default password change period\n"
462 "\t-g group default group\n"
463 "\t-G grp1,grp2 additional groups\n"
464 "\t-L class default user class\n"
465 "\t-k dir default home skeleton\n"
466 "\t-M mode home directory permissions\n"
467 "\t-u min,max set min,max uids\n"
468 "\t-i min,max set min,max gids\n"
469 "\t-w method set default password method\n"
470 "\t-s shell default shell\n"
471 "\t-y path set NIS passwd file path\n",
472 "usage: pw userdel [uid|name] [switches]\n"
473 "\t-V etcdir alternate /etc location\n"
474 "\t-R rootir alternate root directory\n"
475 "\t-n name login name\n"
476 "\t-u uid user id\n"
477 "\t-Y update NIS maps\n"
478 "\t-r remove home & contents\n",
479 "usage: pw usermod [uid|name] [switches]\n"
480 "\t-V etcdir alternate /etc location\n"
481 "\t-R rootir alternate root directory\n"
482 "\t-C config configuration file\n"
483 "\t-q quiet operation\n"
484 "\t-F force add if no user\n"
485 "\t-n name login name\n"
486 "\t-u uid user id\n"
487 "\t-c comment user name/comment\n"
488 "\t-d directory home directory\n"
489 "\t-e date account expiry date\n"
490 "\t-p date password expiry date\n"
491 "\t-g grp initial group\n"
492 "\t-G grp1,grp2 additional groups\n"
493 "\t-l name new login name\n"
494 "\t-L class user class\n"
495 "\t-m [ -k dir ] create and set up home\n"
496 "\t-M mode home directory permissions\n"
497 "\t-s shell name of login shell\n"
498 "\t-w method set new password using method\n"
499 "\t-h fd read password on fd\n"
500 "\t-H fd read encrypted password on fd\n"
501 "\t-Y update NIS maps\n"
502 "\t-N no update\n",
503 "usage: pw usershow [uid|name] [switches]\n"
504 "\t-V etcdir alternate /etc location\n"
505 "\t-R rootir alternate root directory\n"
506 "\t-n name login name\n"
507 "\t-u uid user id\n"
508 "\t-F force print\n"
509 "\t-P prettier format\n"
510 "\t-a print all users\n"
511 "\t-7 print in v7 format\n",
512 "usage: pw usernext [switches]\n"
513 "\t-V etcdir alternate /etc location\n"
514 "\t-R rootir alternate root directory\n"
515 "\t-C config configuration file\n"
516 "\t-q quiet operation\n",
517 "usage pw: lock [switches]\n"
518 "\t-V etcdir alternate /etc locations\n"
519 "\t-C config configuration file\n"
520 "\t-q quiet operation\n",
521 "usage pw: unlock [switches]\n"
522 "\t-V etcdir alternate /etc locations\n"
523 "\t-C config configuration file\n"
524 "\t-q quiet operation\n"
525 },
526 {
527 "usage: pw groupadd [group|gid] [switches]\n"
528 "\t-V etcdir alternate /etc location\n"
529 "\t-R rootir alternate root directory\n"
530 "\t-C config configuration file\n"
531 "\t-q quiet operation\n"
532 "\t-n group group name\n"
533 "\t-g gid group id\n"
534 "\t-M usr1,usr2 add users as group members\n"
535 "\t-o duplicate gid ok\n"
536 "\t-Y update NIS maps\n"
537 "\t-N no update\n",
538 "usage: pw groupdel [group|gid] [switches]\n"
539 "\t-V etcdir alternate /etc location\n"
540 "\t-R rootir alternate root directory\n"
541 "\t-n name group name\n"
542 "\t-g gid group id\n"
543 "\t-Y update NIS maps\n",
544 "usage: pw groupmod [group|gid] [switches]\n"
545 "\t-V etcdir alternate /etc location\n"
546 "\t-R rootir alternate root directory\n"
547 "\t-C config configuration file\n"
548 "\t-q quiet operation\n"
549 "\t-F force add if not exists\n"
550 "\t-n name group name\n"
551 "\t-g gid group id\n"
552 "\t-M usr1,usr2 replaces users as group members\n"
553 "\t-m usr1,usr2 add users as group members\n"
554 "\t-d usr1,usr2 delete users as group members\n"
555 "\t-l name new group name\n"
556 "\t-Y update NIS maps\n"
557 "\t-N no update\n",
558 "usage: pw groupshow [group|gid] [switches]\n"
559 "\t-V etcdir alternate /etc location\n"
560 "\t-R rootir alternate root directory\n"
561 "\t-n name group name\n"
562 "\t-g gid group id\n"
563 "\t-F force print\n"
564 "\t-P prettier format\n"
565 "\t-a print all accounting groups\n",
566 "usage: pw groupnext [switches]\n"
567 "\t-V etcdir alternate /etc location\n"
568 "\t-R rootir alternate root directory\n"
569 "\t-C config configuration file\n"
570 "\t-q quiet operation\n"
571 }
572 };
573
574 fprintf(stderr, "%s", help[which][mode]);
575 }
576 exit(EXIT_FAILURE);
577 }
578
579 struct carg *
580 getarg(struct cargs * _args, int ch)
581 {
582 struct carg *c = LIST_FIRST(_args);
583
584 while (c != NULL && c->ch != ch)
585 c = LIST_NEXT(c, list);
586 return c;
587 }
588
589 struct carg *
590 addarg(struct cargs * _args, int ch, char *argstr)
591 {
592 struct carg *ca = malloc(sizeof(struct carg));
593
594 if (ca == NULL)
595 errx(EX_OSERR, "out of memory");
596 ca->ch = ch;
597 ca->val = argstr;
598 LIST_INSERT_HEAD(_args, ca, list);
599 return ca;
600 }