3 * David L. Nugent. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 static const char rcsid[] =
40 #if !defined(_PATH_YP)
41 #define _PATH_YP "/var/yp/"
43 const char *Modes[] = {
44 "add", "del", "mod", "show", "next",
46 const char *Which[] = {"user", "group", NULL};
47 static const char *Combo1[] = {
48 "useradd", "userdel", "usermod", "usershow", "usernext",
50 "groupadd", "groupdel", "groupmod", "groupshow", "groupnext",
52 static const char *Combo2[] = {
53 "adduser", "deluser", "moduser", "showuser", "nextuser",
55 "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup",
90 static struct cargs arglist;
92 static int getindex(const char *words[], const char *word);
93 static void cmdhelp(int mode, int which);
97 main(int argc, char *argv[])
109 static const char *opts[W_NUM][M_NUM] =
112 "R:V:C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y",
114 "R:V:C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:FNPY",
121 "R:V:C:qn:g:h:H:M:opNPY",
123 "R:V:C:qn:d:g:l:h:H:FM:m:NPY",
129 static int (*funcs[W_NUM]) (int _mode, char *_name, long _id,
130 struct cargs * _args) =
131 { /* Request handlers */
137 relocated = nis = false;
138 memset(&conf, 0, sizeof(conf));
139 strlcpy(conf.etcpath, _PATH_PWD, sizeof(conf.etcpath));
140 conf.checkduplicate = true;
144 (void)setlocale(LC_ALL, "");
147 * Break off the first couple of words to determine what exactly
148 * we're being asked to do
153 if (*argv[1] == '-') {
155 * Special case, allow pw -V<dir> <operation> [args] for scripts etc.
158 if (arg == 'V' || arg == 'R') {
160 errx(EXIT_FAILURE, "Both '-R' and '-V' "
161 "specified, only one accepted");
163 optarg = &argv[1][2];
164 if (*optarg == '\0') {
165 if (stat(argv[2], &st) != 0)
167 "no such directory `%s'",
169 if (!S_ISDIR(st.st_mode))
170 errx(EX_OSFILE, "`%s' not a "
171 "directory", argv[2]);
176 memcpy(&PWF, &VPWF, sizeof PWF);
178 strlcpy(conf.rootdir, optarg,
179 sizeof(conf.rootdir));
180 PWF._altdir = PWF_ROOTDIR;
182 snprintf(conf.etcpath, sizeof(conf.etcpath),
183 "%s%s", optarg, arg == 'R' ? "/etc" : "");
187 else if (mode == -1 && (tmp = getindex(Modes, argv[1])) != -1)
189 else if (which == -1 && (tmp = getindex(Which, argv[1])) != -1)
191 else if ((mode == -1 && which == -1) &&
192 ((tmp = getindex(Combo1, argv[1])) != -1 ||
193 (tmp = getindex(Combo2, argv[1])) != -1)) {
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);
202 errx(EX_USAGE, "Bad id '%s': %s",
207 errx(EX_USAGE, "unknown keyword `%s'", argv[1]);
213 * Bail out unless the user is specific!
215 if (mode == -1 || which == -1)
216 cmdhelp(mode, which);
219 * We know which mode we're in and what we're about to do, so now
220 * let's dispatch the remaining command line args in a genric way.
224 while ((ch = getopt(argc, argv, opts[which][mode])) != -1) {
227 errx(EX_USAGE, "unknown switch");
233 conf.config = optarg;
234 config = conf.config;
240 if (strlen(optarg) >= MAXLOGNAME)
241 errx(EX_USAGE, "new name too long: %s", optarg);
242 conf.newname = optarg;
251 if (which == 0) { /* for user* */
252 addarg(&arglist, 'g', optarg);
255 if (strspn(optarg, "0123456789") != strlen(optarg))
256 errx(EX_USAGE, "-g expects a number");
257 id = strtonum(optarg, 0, LONG_MAX, &errstr);
259 errx(EX_USAGE, "Bad id '%s': %s", optarg,
263 if (strspn(optarg, "0123456789,") != strlen(optarg))
264 errx(EX_USAGE, "-u expects a number");
265 if (strchr(optarg, ',') != NULL) {
266 addarg(&arglist, 'u', optarg);
269 id = strtonum(optarg, 0, LONG_MAX, &errstr);
271 errx(EX_USAGE, "Bad id '%s': %s", optarg,
278 conf.checkduplicate = false;
281 addarg(&arglist, ch, optarg);
287 if (name != NULL && strlen(name) >= MAXLOGNAME)
288 errx(EX_USAGE, "name too long: %s", name);
291 * Must be root to attempt an update
293 if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && !conf.dryrun)
294 errx(EX_NOPERM, "you must be root to run this program");
297 * We should immediately look for the -q 'quiet' switch so that we
298 * don't bother with extraneous errors
300 if (getarg(&arglist, 'q') != NULL)
301 freopen(_PATH_DEVNULL, "w", stderr);
304 * Set our base working path if not overridden
307 if (config == NULL) { /* Only override config location if -C not specified */
308 asprintf(&config, "%s/pw.conf", conf.etcpath);
310 errx(EX_OSERR, "out of memory");
314 * Now, let's do the common initialisation
316 conf.userconf = read_userconfig(config);
318 ch = funcs[which] (mode, name, id, &arglist);
321 * If everything went ok, and we've been asked to update
322 * the NIS maps, then do it now
324 if (ch == EXIT_SUCCESS && nis) {
328 if (chdir(_PATH_YP) == -1)
329 warn("chdir(" _PATH_YP ")");
330 else if ((pid = fork()) == -1)
333 /* Is make anywhere else? */
334 execlp("/usr/bin/make", "make", (char *)NULL);
339 if ((i = WEXITSTATUS(i)) != 0)
340 errx(ch, "make exited with status %d", i);
342 pw_log(conf.userconf, mode, which, "NIS maps updated");
350 getindex(const char *words[], const char *word)
355 if (strcmp(words[i], word) == 0)
364 * This is probably an overkill for a cmdline help system, but it reflects
365 * the complexity of the command line.
369 cmdhelp(int mode, int which)
372 fprintf(stderr, "usage:\n pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n");
374 fprintf(stderr, "usage:\n pw %s [add|del|mod|show|next] [help|switches/values]\n", Which[which]);
378 * We need to give mode specific help
380 static const char *help[W_NUM][M_NUM] =
383 "usage: pw useradd [name] [switches]\n"
384 "\t-V etcdir alternate /etc location\n"
385 "\t-R rootir alternate root directory\n"
386 "\t-C config configuration file\n"
387 "\t-q quiet operation\n"
389 "\t-n name login name\n"
391 "\t-c comment user name/comment\n"
392 "\t-d directory home directory\n"
393 "\t-e date account expiry date\n"
394 "\t-p date password expiry date\n"
395 "\t-g grp initial group\n"
396 "\t-G grp1,grp2 additional groups\n"
397 "\t-m [ -k dir ] create and set up home\n"
398 "\t-M mode home directory permissions\n"
399 "\t-s shell name of login shell\n"
400 "\t-o duplicate uid ok\n"
401 "\t-L class user class\n"
402 "\t-h fd read password on fd\n"
403 "\t-H fd read encrypted password on fd\n"
404 "\t-Y update NIS maps\n"
406 " Setting defaults:\n"
407 "\t-V etcdir alternate /etc location\n"
408 "\t-R rootir alternate root directory\n"
409 "\t-D set user defaults\n"
410 "\t-b dir default home root dir\n"
411 "\t-e period default expiry period\n"
412 "\t-p period default password change period\n"
413 "\t-g group default group\n"
414 "\t-G grp1,grp2 additional groups\n"
415 "\t-L class default user class\n"
416 "\t-k dir default home skeleton\n"
417 "\t-M mode home directory permissions\n"
418 "\t-u min,max set min,max uids\n"
419 "\t-i min,max set min,max gids\n"
420 "\t-w method set default password method\n"
421 "\t-s shell default shell\n"
422 "\t-y path set NIS passwd file path\n",
423 "usage: pw userdel [uid|name] [switches]\n"
424 "\t-V etcdir alternate /etc location\n"
425 "\t-R rootir alternate root directory\n"
426 "\t-n name login name\n"
428 "\t-Y update NIS maps\n"
429 "\t-r remove home & contents\n",
430 "usage: pw usermod [uid|name] [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 no user\n"
436 "\t-n name login name\n"
438 "\t-c comment user name/comment\n"
439 "\t-d directory home directory\n"
440 "\t-e date account expiry date\n"
441 "\t-p date password expiry date\n"
442 "\t-g grp initial group\n"
443 "\t-G grp1,grp2 additional groups\n"
444 "\t-l name new login name\n"
445 "\t-L class user class\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-w method set new password using method\n"
450 "\t-h fd read password on fd\n"
451 "\t-H fd read encrypted password on fd\n"
452 "\t-Y update NIS maps\n"
454 "usage: pw usershow [uid|name] [switches]\n"
455 "\t-V etcdir alternate /etc location\n"
456 "\t-R rootir alternate root directory\n"
457 "\t-n name login name\n"
460 "\t-P prettier format\n"
461 "\t-a print all users\n"
462 "\t-7 print in v7 format\n",
463 "usage: pw usernext [switches]\n"
464 "\t-V etcdir alternate /etc location\n"
465 "\t-R rootir alternate root directory\n"
466 "\t-C config configuration file\n"
467 "\t-q quiet operation\n",
468 "usage pw: lock [switches]\n"
469 "\t-V etcdir alternate /etc locations\n"
470 "\t-C config configuration file\n"
471 "\t-q quiet operation\n",
472 "usage pw: unlock [switches]\n"
473 "\t-V etcdir alternate /etc locations\n"
474 "\t-C config configuration file\n"
475 "\t-q quiet operation\n"
478 "usage: pw groupadd [group|gid] [switches]\n"
479 "\t-V etcdir alternate /etc location\n"
480 "\t-R rootir alternate root directory\n"
481 "\t-C config configuration file\n"
482 "\t-q quiet operation\n"
483 "\t-n group group name\n"
484 "\t-g gid group id\n"
485 "\t-M usr1,usr2 add users as group members\n"
486 "\t-o duplicate gid ok\n"
487 "\t-Y update NIS maps\n"
489 "usage: pw groupdel [group|gid] [switches]\n"
490 "\t-V etcdir alternate /etc location\n"
491 "\t-R rootir alternate root directory\n"
492 "\t-n name group name\n"
493 "\t-g gid group id\n"
494 "\t-Y update NIS maps\n",
495 "usage: pw groupmod [group|gid] [switches]\n"
496 "\t-V etcdir alternate /etc location\n"
497 "\t-R rootir alternate root directory\n"
498 "\t-C config configuration file\n"
499 "\t-q quiet operation\n"
500 "\t-F force add if not exists\n"
501 "\t-n name group name\n"
502 "\t-g gid group id\n"
503 "\t-M usr1,usr2 replaces users as group members\n"
504 "\t-m usr1,usr2 add users as group members\n"
505 "\t-d usr1,usr2 delete users as group members\n"
506 "\t-l name new group name\n"
507 "\t-Y update NIS maps\n"
509 "usage: pw groupshow [group|gid] [switches]\n"
510 "\t-V etcdir alternate /etc location\n"
511 "\t-R rootir alternate root directory\n"
512 "\t-n name group name\n"
513 "\t-g gid group id\n"
515 "\t-P prettier format\n"
516 "\t-a print all accounting groups\n",
517 "usage: pw groupnext [switches]\n"
518 "\t-V etcdir alternate /etc location\n"
519 "\t-R rootir alternate root directory\n"
520 "\t-C config configuration file\n"
521 "\t-q quiet operation\n"
525 fprintf(stderr, "%s", help[which][mode]);
531 getarg(struct cargs * _args, int ch)
533 struct carg *c = LIST_FIRST(_args);
535 while (c != NULL && c->ch != ch)
536 c = LIST_NEXT(c, list);
541 addarg(struct cargs * _args, int ch, char *argstr)
543 struct carg *ca = malloc(sizeof(struct carg));
546 errx(EX_OSERR, "out of memory");
549 LIST_INSERT_HEAD(_args, ca, list);