]>
git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw.c
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
.rootdir
, "/", sizeof(conf
.rootdir
));
140 strlcpy(conf
.etcpath
, _PATH_PWD
, sizeof(conf
.etcpath
));
142 conf
.checkduplicate
= true;
146 (void)setlocale(LC_ALL
, "");
149 * Break off the first couple of words to determine what exactly
150 * we're being asked to do
155 if (*argv
[1] == '-') {
157 * Special case, allow pw -V<dir> <operation> [args] for scripts etc.
160 if (arg
== 'V' || arg
== 'R') {
162 errx(EXIT_FAILURE
, "Both '-R' and '-V' "
163 "specified, only one accepted");
165 optarg
= &argv
[1][2];
166 if (*optarg
== '\0') {
167 if (stat(argv
[2], &st
) != 0)
169 "no such directory `%s'",
171 if (!S_ISDIR(st
.st_mode
))
172 errx(EX_OSFILE
, "`%s' not a "
173 "directory", argv
[2]);
178 memcpy(&PWF
, &VPWF
, sizeof PWF
);
180 strlcpy(conf
.rootdir
, optarg
,
181 sizeof(conf
.rootdir
));
182 PWF
._altdir
= PWF_ROOTDIR
;
184 snprintf(conf
.etcpath
, sizeof(conf
.etcpath
),
185 "%s%s", optarg
, arg
== 'R' ? "/etc" : "");
189 else if (mode
== -1 && (tmp
= getindex(Modes
, argv
[1])) != -1)
191 else if (which
== -1 && (tmp
= getindex(Which
, argv
[1])) != -1)
193 else if ((mode
== -1 && which
== -1) &&
194 ((tmp
= getindex(Combo1
, argv
[1])) != -1 ||
195 (tmp
= getindex(Combo2
, argv
[1])) != -1)) {
198 } else if (strcmp(argv
[1], "help") == 0 && argv
[2] == NULL
)
199 cmdhelp(mode
, which
);
200 else if (which
!= -1 && mode
!= -1) {
201 if (strspn(argv
[1], "0123456789") == strlen(argv
[1])) {
202 id
= strtounum(argv
[1], 0, UID_MAX
, &errstr
);
204 errx(EX_USAGE
, "Bad id '%s': %s",
209 errx(EX_USAGE
, "unknown keyword `%s'", argv
[1]);
215 * Bail out unless the user is specific!
217 if (mode
== -1 || which
== -1)
218 cmdhelp(mode
, which
);
220 conf
.rootfd
= open(conf
.rootdir
, O_DIRECTORY
|O_CLOEXEC
);
221 if (conf
.rootfd
== -1)
222 errx(EXIT_FAILURE
, "Unable to open '%s'", conf
.rootdir
);
225 * We know which mode we're in and what we're about to do, so now
226 * let's dispatch the remaining command line args in a genric way.
230 while ((ch
= getopt(argc
, argv
, opts
[which
][mode
])) != -1) {
233 errx(EX_USAGE
, "unknown switch");
239 conf
.config
= optarg
;
240 config
= conf
.config
;
249 if (strlen(optarg
) >= MAXLOGNAME
)
250 errx(EX_USAGE
, "new name too long: %s", optarg
);
251 conf
.newname
= optarg
;
263 conf
.gecos
= pw_checkname(optarg
, 1);
266 conf
.expire_days
= strtonum(optarg
, 0, INT_MAX
, &errstr
);
268 errx(EX_USAGE
, "Invalid expired days: %s", optarg
);
271 if (which
== 0) { /* for user* */
272 addarg(&arglist
, 'g', optarg
);
275 if (strspn(optarg
, "0123456789") != strlen(optarg
))
276 errx(EX_USAGE
, "-g expects a number");
277 id
= strtounum(optarg
, 0, GID_MAX
, &errstr
);
279 errx(EX_USAGE
, "Bad id '%s': %s", optarg
,
283 if (strspn(optarg
, "0123456789,") != strlen(optarg
))
284 errx(EX_USAGE
, "-u expects a number");
285 if (strchr(optarg
, ',') != NULL
) {
286 addarg(&arglist
, 'u', optarg
);
289 id
= strtounum(optarg
, 0, UID_MAX
, &errstr
);
291 errx(EX_USAGE
, "Bad id '%s': %s", optarg
,
299 errx(EX_USAGE
, "'-h' and '-H' are mutually "
300 "exclusive options");
301 conf
.precrypted
= true;
302 if (strspn(optarg
, "0123456789") != strlen(optarg
))
303 errx(EX_USAGE
, "'-H' expects a file descriptor");
305 conf
.fd
= strtonum(optarg
, 0, INT_MAX
, &errstr
);
307 errx(EX_USAGE
, "Bad file descriptor '%s': %s",
312 errx(EX_USAGE
, "'-h' and '-H' are mutually "
313 "exclusive options");
315 if (strcmp(optarg
, "-") == 0)
317 else if (strspn(optarg
, "0123456789") == strlen(optarg
)) {
318 conf
.fd
= strtonum(optarg
, 0, INT_MAX
, &errstr
);
320 errx(EX_USAGE
, "'-h' expects a "
321 "file descriptor or '-'");
323 errx(EX_USAGE
, "'-h' expects a file "
324 "descriptor or '-'");
327 conf
.checkduplicate
= false;
330 conf
.password_days
= strtonum(optarg
, 0, INT_MAX
, &errstr
);
332 errx(EX_USAGE
, "Invalid password days: %s", optarg
);
338 conf
.deletehome
= true;
341 addarg(&arglist
, ch
, optarg
);
347 if (name
!= NULL
&& strlen(name
) >= MAXLOGNAME
)
348 errx(EX_USAGE
, "name too long: %s", name
);
351 * Must be root to attempt an update
353 if (geteuid() != 0 && mode
!= M_PRINT
&& mode
!= M_NEXT
&& !conf
.dryrun
)
354 errx(EX_NOPERM
, "you must be root to run this program");
357 * We should immediately look for the -q 'quiet' switch so that we
358 * don't bother with extraneous errors
361 freopen(_PATH_DEVNULL
, "w", stderr
);
364 * Set our base working path if not overridden
367 if (config
== NULL
) { /* Only override config location if -C not specified */
368 asprintf(&config
, "%s/pw.conf", conf
.etcpath
);
370 errx(EX_OSERR
, "out of memory");
374 * Now, let's do the common initialisation
376 conf
.userconf
= read_userconfig(config
);
378 ch
= funcs
[which
] (mode
, name
, id
, &arglist
);
381 * If everything went ok, and we've been asked to update
382 * the NIS maps, then do it now
384 if (ch
== EXIT_SUCCESS
&& nis
) {
388 if (chdir(_PATH_YP
) == -1)
389 warn("chdir(" _PATH_YP
")");
390 else if ((pid
= fork()) == -1)
393 /* Is make anywhere else? */
394 execlp("/usr/bin/make", "make", (char *)NULL
);
399 if ((i
= WEXITSTATUS(i
)) != 0)
400 errx(ch
, "make exited with status %d", i
);
402 pw_log(conf
.userconf
, mode
, which
, "NIS maps updated");
410 getindex(const char *words
[], const char *word
)
415 if (strcmp(words
[i
], word
) == 0)
424 * This is probably an overkill for a cmdline help system, but it reflects
425 * the complexity of the command line.
429 cmdhelp(int mode
, int which
)
432 fprintf(stderr
, "usage:\n pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n");
434 fprintf(stderr
, "usage:\n pw %s [add|del|mod|show|next] [help|switches/values]\n", Which
[which
]);
438 * We need to give mode specific help
440 static const char *help
[W_NUM
][M_NUM
] =
443 "usage: pw useradd [name] [switches]\n"
444 "\t-V etcdir alternate /etc location\n"
445 "\t-R rootir alternate root directory\n"
446 "\t-C config configuration file\n"
447 "\t-q quiet operation\n"
449 "\t-n name login name\n"
451 "\t-c comment user name/comment\n"
452 "\t-d directory home directory\n"
453 "\t-e date account expiry date\n"
454 "\t-p date password expiry date\n"
455 "\t-g grp initial group\n"
456 "\t-G grp1,grp2 additional groups\n"
457 "\t-m [ -k dir ] create and set up home\n"
458 "\t-M mode home directory permissions\n"
459 "\t-s shell name of login shell\n"
460 "\t-o duplicate uid ok\n"
461 "\t-L class user class\n"
462 "\t-h fd read password on fd\n"
463 "\t-H fd read encrypted password on fd\n"
464 "\t-Y update NIS maps\n"
466 " Setting defaults:\n"
467 "\t-V etcdir alternate /etc location\n"
468 "\t-R rootir alternate root directory\n"
469 "\t-D set user defaults\n"
470 "\t-b dir default home root dir\n"
471 "\t-e period default expiry period\n"
472 "\t-p period default password change period\n"
473 "\t-g group default group\n"
474 "\t-G grp1,grp2 additional groups\n"
475 "\t-L class default user class\n"
476 "\t-k dir default home skeleton\n"
477 "\t-M mode home directory permissions\n"
478 "\t-u min,max set min,max uids\n"
479 "\t-i min,max set min,max gids\n"
480 "\t-w method set default password method\n"
481 "\t-s shell default shell\n"
482 "\t-y path set NIS passwd file path\n",
483 "usage: pw userdel [uid|name] [switches]\n"
484 "\t-V etcdir alternate /etc location\n"
485 "\t-R rootir alternate root directory\n"
486 "\t-n name login name\n"
488 "\t-Y update NIS maps\n"
489 "\t-r remove home & contents\n",
490 "usage: pw usermod [uid|name] [switches]\n"
491 "\t-V etcdir alternate /etc location\n"
492 "\t-R rootir alternate root directory\n"
493 "\t-C config configuration file\n"
494 "\t-q quiet operation\n"
495 "\t-F force add if no user\n"
496 "\t-n name login name\n"
498 "\t-c comment user name/comment\n"
499 "\t-d directory home directory\n"
500 "\t-e date account expiry date\n"
501 "\t-p date password expiry date\n"
502 "\t-g grp initial group\n"
503 "\t-G grp1,grp2 additional groups\n"
504 "\t-l name new login name\n"
505 "\t-L class user class\n"
506 "\t-m [ -k dir ] create and set up home\n"
507 "\t-M mode home directory permissions\n"
508 "\t-s shell name of login shell\n"
509 "\t-w method set new password using method\n"
510 "\t-h fd read password on fd\n"
511 "\t-H fd read encrypted password on fd\n"
512 "\t-Y update NIS maps\n"
514 "usage: pw usershow [uid|name] [switches]\n"
515 "\t-V etcdir alternate /etc location\n"
516 "\t-R rootir alternate root directory\n"
517 "\t-n name login name\n"
520 "\t-P prettier format\n"
521 "\t-a print all users\n"
522 "\t-7 print in v7 format\n",
523 "usage: pw usernext [switches]\n"
524 "\t-V etcdir alternate /etc location\n"
525 "\t-R rootir alternate root directory\n"
526 "\t-C config configuration file\n"
527 "\t-q quiet operation\n",
528 "usage pw: lock [switches]\n"
529 "\t-V etcdir alternate /etc locations\n"
530 "\t-C config configuration file\n"
531 "\t-q quiet operation\n",
532 "usage pw: unlock [switches]\n"
533 "\t-V etcdir alternate /etc locations\n"
534 "\t-C config configuration file\n"
535 "\t-q quiet operation\n"
538 "usage: pw groupadd [group|gid] [switches]\n"
539 "\t-V etcdir alternate /etc location\n"
540 "\t-R rootir alternate root directory\n"
541 "\t-C config configuration file\n"
542 "\t-q quiet operation\n"
543 "\t-n group group name\n"
544 "\t-g gid group id\n"
545 "\t-M usr1,usr2 add users as group members\n"
546 "\t-o duplicate gid ok\n"
547 "\t-Y update NIS maps\n"
549 "usage: pw groupdel [group|gid] [switches]\n"
550 "\t-V etcdir alternate /etc location\n"
551 "\t-R rootir alternate root directory\n"
552 "\t-n name group name\n"
553 "\t-g gid group id\n"
554 "\t-Y update NIS maps\n",
555 "usage: pw groupmod [group|gid] [switches]\n"
556 "\t-V etcdir alternate /etc location\n"
557 "\t-R rootir alternate root directory\n"
558 "\t-C config configuration file\n"
559 "\t-q quiet operation\n"
560 "\t-F force add if not exists\n"
561 "\t-n name group name\n"
562 "\t-g gid group id\n"
563 "\t-M usr1,usr2 replaces users as group members\n"
564 "\t-m usr1,usr2 add users as group members\n"
565 "\t-d usr1,usr2 delete users as group members\n"
566 "\t-l name new group name\n"
567 "\t-Y update NIS maps\n"
569 "usage: pw groupshow [group|gid] [switches]\n"
570 "\t-V etcdir alternate /etc location\n"
571 "\t-R rootir alternate root directory\n"
572 "\t-n name group name\n"
573 "\t-g gid group id\n"
575 "\t-P prettier format\n"
576 "\t-a print all accounting groups\n",
577 "usage: pw groupnext [switches]\n"
578 "\t-V etcdir alternate /etc location\n"
579 "\t-R rootir alternate root directory\n"
580 "\t-C config configuration file\n"
581 "\t-q quiet operation\n"
585 fprintf(stderr
, "%s", help
[which
][mode
]);
591 getarg(struct cargs
* _args
, int ch
)
598 c
= LIST_FIRST(_args
);
600 while (c
!= NULL
&& c
->ch
!= ch
)
601 c
= LIST_NEXT(c
, list
);
606 addarg(struct cargs
* _args
, int ch
, char *argstr
)
608 struct carg
*ca
= malloc(sizeof(struct carg
));
611 errx(EX_OSERR
, "out of memory");
614 LIST_INSERT_HEAD(_args
, ca
, list
);