]>
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
[])
107 static const char *opts
[W_NUM
][M_NUM
] =
110 "R:V:C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y",
112 "R:V:C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:FNPY",
119 "R:V:C:qn:g:h:H:M:opNPY",
121 "R:V:C:qn:d:g:l:h:H:FM:m:NPY",
127 static int (*funcs
[W_NUM
]) (int _mode
, struct cargs
* _args
) =
128 { /* Request handlers */
133 relocated
= nis
= false;
134 conf
.rootdir
[0] = '\0';
136 strlcpy(conf
.etcpath
, _PATH_PWD
, sizeof(conf
.etcpath
));
140 (void)setlocale(LC_ALL
, "");
143 * Break off the first couple of words to determine what exactly
144 * we're being asked to do
149 if (*argv
[1] == '-') {
151 * Special case, allow pw -V<dir> <operation> [args] for scripts etc.
154 if (arg
== 'V' || arg
== 'R') {
156 errx(EXIT_FAILURE
, "Both '-R' and '-V' "
157 "specified, only one accepted");
159 optarg
= &argv
[1][2];
160 if (*optarg
== '\0') {
161 if (stat(argv
[2], &st
) != 0)
163 "no such directory `%s'",
165 if (!S_ISDIR(st
.st_mode
))
166 errx(EX_OSFILE
, "`%s' not a "
167 "directory", argv
[2]);
172 memcpy(&PWF
, &VPWF
, sizeof PWF
);
174 strlcpy(conf
.rootdir
, optarg
,
175 sizeof(conf
.rootdir
));
176 PWF
._altdir
= PWF_ROOTDIR
;
178 snprintf(conf
.etcpath
, sizeof(conf
.etcpath
),
179 "%s%s", optarg
, arg
== 'R' ? "/etc" : "");
183 else if (mode
== -1 && (tmp
= getindex(Modes
, argv
[1])) != -1)
185 else if (which
== -1 && (tmp
= getindex(Which
, argv
[1])) != -1)
187 else if ((mode
== -1 && which
== -1) &&
188 ((tmp
= getindex(Combo1
, argv
[1])) != -1 ||
189 (tmp
= getindex(Combo2
, argv
[1])) != -1)) {
192 } else if (strcmp(argv
[1], "help") == 0 && argv
[2] == NULL
)
193 cmdhelp(mode
, which
);
194 else if (which
!= -1 && mode
!= -1)
195 addarg(&arglist
, 'n', argv
[1]);
197 errx(EX_USAGE
, "unknown keyword `%s'", argv
[1]);
203 * Bail out unless the user is specific!
205 if (mode
== -1 || which
== -1)
206 cmdhelp(mode
, which
);
209 * We know which mode we're in and what we're about to do, so now
210 * let's dispatch the remaining command line args in a genric way.
214 while ((ch
= getopt(argc
, argv
, opts
[which
][mode
])) != -1) {
217 errx(EX_USAGE
, "unknown switch");
229 addarg(&arglist
, ch
, optarg
);
236 * Must be root to attempt an update
238 if (geteuid() != 0 && mode
!= M_PRINT
&& mode
!= M_NEXT
&& !conf
.dryrun
)
239 errx(EX_NOPERM
, "you must be root to run this program");
242 * We should immediately look for the -q 'quiet' switch so that we
243 * don't bother with extraneous errors
245 if (getarg(&arglist
, 'q') != NULL
)
246 freopen(_PATH_DEVNULL
, "w", stderr
);
249 * Set our base working path if not overridden
252 if (config
== NULL
) { /* Only override config location if -C not specified */
253 asprintf(&config
, "%s/pw.conf", conf
.etcpath
);
255 errx(EX_OSERR
, "out of memory");
259 * Now, let's do the common initialisation
261 conf
.userconf
= read_userconfig(config
);
263 ch
= funcs
[which
] (mode
, &arglist
);
266 * If everything went ok, and we've been asked to update
267 * the NIS maps, then do it now
269 if (ch
== EXIT_SUCCESS
&& nis
) {
273 if (chdir(_PATH_YP
) == -1)
274 warn("chdir(" _PATH_YP
")");
275 else if ((pid
= fork()) == -1)
278 /* Is make anywhere else? */
279 execlp("/usr/bin/make", "make", (char *)NULL
);
284 if ((i
= WEXITSTATUS(i
)) != 0)
285 errx(ch
, "make exited with status %d", i
);
287 pw_log(conf
.userconf
, mode
, which
, "NIS maps updated");
295 getindex(const char *words
[], const char *word
)
300 if (strcmp(words
[i
], word
) == 0)
309 * This is probably an overkill for a cmdline help system, but it reflects
310 * the complexity of the command line.
314 cmdhelp(int mode
, int which
)
317 fprintf(stderr
, "usage:\n pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n");
319 fprintf(stderr
, "usage:\n pw %s [add|del|mod|show|next] [help|switches/values]\n", Which
[which
]);
323 * We need to give mode specific help
325 static const char *help
[W_NUM
][M_NUM
] =
328 "usage: pw useradd [name] [switches]\n"
329 "\t-V etcdir alternate /etc location\n"
330 "\t-R rootir alternate root directory\n"
331 "\t-C config configuration file\n"
332 "\t-q quiet operation\n"
334 "\t-n name login name\n"
336 "\t-c comment user name/comment\n"
337 "\t-d directory home directory\n"
338 "\t-e date account expiry date\n"
339 "\t-p date password expiry date\n"
340 "\t-g grp initial group\n"
341 "\t-G grp1,grp2 additional groups\n"
342 "\t-m [ -k dir ] create and set up home\n"
343 "\t-M mode home directory permissions\n"
344 "\t-s shell name of login shell\n"
345 "\t-o duplicate uid ok\n"
346 "\t-L class user class\n"
347 "\t-h fd read password on fd\n"
348 "\t-H fd read encrypted password on fd\n"
349 "\t-Y update NIS maps\n"
351 " Setting defaults:\n"
352 "\t-V etcdir alternate /etc location\n"
353 "\t-R rootir alternate root directory\n"
354 "\t-D set user defaults\n"
355 "\t-b dir default home root dir\n"
356 "\t-e period default expiry period\n"
357 "\t-p period default password change period\n"
358 "\t-g group default group\n"
359 "\t-G grp1,grp2 additional groups\n"
360 "\t-L class default user class\n"
361 "\t-k dir default home skeleton\n"
362 "\t-M mode home directory permissions\n"
363 "\t-u min,max set min,max uids\n"
364 "\t-i min,max set min,max gids\n"
365 "\t-w method set default password method\n"
366 "\t-s shell default shell\n"
367 "\t-y path set NIS passwd file path\n",
368 "usage: pw userdel [uid|name] [switches]\n"
369 "\t-V etcdir alternate /etc location\n"
370 "\t-R rootir alternate root directory\n"
371 "\t-n name login name\n"
373 "\t-Y update NIS maps\n"
374 "\t-r remove home & contents\n",
375 "usage: pw usermod [uid|name] [switches]\n"
376 "\t-V etcdir alternate /etc location\n"
377 "\t-R rootir alternate root directory\n"
378 "\t-C config configuration file\n"
379 "\t-q quiet operation\n"
380 "\t-F force add if no user\n"
381 "\t-n name login name\n"
383 "\t-c comment user name/comment\n"
384 "\t-d directory home directory\n"
385 "\t-e date account expiry date\n"
386 "\t-p date password expiry date\n"
387 "\t-g grp initial group\n"
388 "\t-G grp1,grp2 additional groups\n"
389 "\t-l name new login name\n"
390 "\t-L class user class\n"
391 "\t-m [ -k dir ] create and set up home\n"
392 "\t-M mode home directory permissions\n"
393 "\t-s shell name of login shell\n"
394 "\t-w method set new password using method\n"
395 "\t-h fd read password on fd\n"
396 "\t-H fd read encrypted password on fd\n"
397 "\t-Y update NIS maps\n"
399 "usage: pw usershow [uid|name] [switches]\n"
400 "\t-V etcdir alternate /etc location\n"
401 "\t-R rootir alternate root directory\n"
402 "\t-n name login name\n"
405 "\t-P prettier format\n"
406 "\t-a print all users\n"
407 "\t-7 print in v7 format\n",
408 "usage: pw usernext [switches]\n"
409 "\t-V etcdir alternate /etc location\n"
410 "\t-R rootir alternate root directory\n"
411 "\t-C config configuration file\n"
412 "\t-q quiet operation\n",
413 "usage pw: lock [switches]\n"
414 "\t-V etcdir alternate /etc locations\n"
415 "\t-C config configuration file\n"
416 "\t-q quiet operation\n",
417 "usage pw: unlock [switches]\n"
418 "\t-V etcdir alternate /etc locations\n"
419 "\t-C config configuration file\n"
420 "\t-q quiet operation\n"
423 "usage: pw groupadd [group|gid] [switches]\n"
424 "\t-V etcdir alternate /etc location\n"
425 "\t-R rootir alternate root directory\n"
426 "\t-C config configuration file\n"
427 "\t-q quiet operation\n"
428 "\t-n group group name\n"
429 "\t-g gid group id\n"
430 "\t-M usr1,usr2 add users as group members\n"
431 "\t-o duplicate gid ok\n"
432 "\t-Y update NIS maps\n"
434 "usage: pw groupdel [group|gid] [switches]\n"
435 "\t-V etcdir alternate /etc location\n"
436 "\t-R rootir alternate root directory\n"
437 "\t-n name group name\n"
438 "\t-g gid group id\n"
439 "\t-Y update NIS maps\n",
440 "usage: pw groupmod [group|gid] [switches]\n"
441 "\t-V etcdir alternate /etc location\n"
442 "\t-R rootir alternate root directory\n"
443 "\t-C config configuration file\n"
444 "\t-q quiet operation\n"
445 "\t-F force add if not exists\n"
446 "\t-n name group name\n"
447 "\t-g gid group id\n"
448 "\t-M usr1,usr2 replaces users as group members\n"
449 "\t-m usr1,usr2 add users as group members\n"
450 "\t-d usr1,usr2 delete users as group members\n"
451 "\t-l name new group name\n"
452 "\t-Y update NIS maps\n"
454 "usage: pw groupshow [group|gid] [switches]\n"
455 "\t-V etcdir alternate /etc location\n"
456 "\t-R rootir alternate root directory\n"
457 "\t-n name group name\n"
458 "\t-g gid group id\n"
460 "\t-P prettier format\n"
461 "\t-a print all accounting groups\n",
462 "usage: pw groupnext [switches]\n"
463 "\t-V etcdir alternate /etc location\n"
464 "\t-R rootir alternate root directory\n"
465 "\t-C config configuration file\n"
466 "\t-q quiet operation\n"
470 fprintf(stderr
, "%s", help
[which
][mode
]);
476 getarg(struct cargs
* _args
, int ch
)
478 struct carg
*c
= LIST_FIRST(_args
);
480 while (c
!= NULL
&& c
->ch
!= ch
)
481 c
= LIST_NEXT(c
, list
);
486 addarg(struct cargs
* _args
, int ch
, char *argstr
)
488 struct carg
*ca
= malloc(sizeof(struct carg
));
491 errx(EX_OSERR
, "out of memory");
494 LIST_INSERT_HEAD(_args
, ca
, list
);