]>
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
[] =
39 #if !defined(_PATH_YP)
40 #define _PATH_YP "/var/yp/"
42 const char *Modes
[] = {
43 "add", "del", "mod", "show", "next",
45 const char *Which
[] = {"user", "group", NULL
};
46 static const char *Combo1
[] = {
47 "useradd", "userdel", "usermod", "usershow", "usernext",
49 "groupadd", "groupdel", "groupmod", "groupshow", "groupnext",
51 static const char *Combo2
[] = {
52 "adduser", "deluser", "moduser", "showuser", "nextuser",
54 "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup",
87 static struct cargs arglist
;
89 static int getindex(const char *words
[], const char *word
);
90 static void cmdhelp(int mode
, int which
);
94 main(int argc
, char *argv
[])
100 struct userconf
*cnf
;
103 static const char *opts
[W_NUM
][M_NUM
] =
106 "V:C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y",
108 "V:C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:FNPY",
115 "V:C:qn:g:h:H:M:opNPY",
117 "V:C:qn:d:g:l:h:H:FM:m:NPY",
123 static int (*funcs
[W_NUM
]) (struct userconf
* _cnf
, int _mode
, struct cargs
* _args
) =
124 { /* Request handlers */
131 (void)setlocale(LC_ALL
, "");
134 * Break off the first couple of words to determine what exactly
135 * we're being asked to do
140 if (*argv
[1] == '-') {
142 * Special case, allow pw -V<dir> <operation> [args] for scripts etc.
144 if (argv
[1][1] == 'V') {
145 optarg
= &argv
[1][2];
146 if (*optarg
== '\0') {
147 if (stat(argv
[2], &st
) != 0)
149 "no such directory `%s'",
151 if (!S_ISDIR(st
.st_mode
))
152 errx(EX_OSFILE
, "`%s' not a "
153 "directory", argv
[2]);
158 addarg(&arglist
, 'V', optarg
);
162 else if (mode
== -1 && (tmp
= getindex(Modes
, argv
[1])) != -1)
164 else if (which
== -1 && (tmp
= getindex(Which
, argv
[1])) != -1)
166 else if ((mode
== -1 && which
== -1) &&
167 ((tmp
= getindex(Combo1
, argv
[1])) != -1 ||
168 (tmp
= getindex(Combo2
, argv
[1])) != -1)) {
171 } else if (strcmp(argv
[1], "help") == 0 && argv
[2] == NULL
)
172 cmdhelp(mode
, which
);
173 else if (which
!= -1 && mode
!= -1)
174 addarg(&arglist
, 'n', argv
[1]);
176 errx(EX_USAGE
, "unknown keyword `%s'", argv
[1]);
182 * Bail out unless the user is specific!
184 if (mode
== -1 || which
== -1)
185 cmdhelp(mode
, which
);
188 * We know which mode we're in and what we're about to do, so now
189 * let's dispatch the remaining command line args in a genric way.
193 while ((ch
= getopt(argc
, argv
, opts
[which
][mode
])) != -1) {
195 errx(EX_USAGE
, "unknown switch");
197 addarg(&arglist
, ch
, optarg
);
202 * Must be root to attempt an update
204 if (geteuid() != 0 && mode
!= M_PRINT
&& mode
!= M_NEXT
&& getarg(&arglist
, 'N')==NULL
)
205 errx(EX_NOPERM
, "you must be root to run this program");
208 * We should immediately look for the -q 'quiet' switch so that we
209 * don't bother with extraneous errors
211 if (getarg(&arglist
, 'q') != NULL
)
212 freopen(_PATH_DEVNULL
, "w", stderr
);
215 * Set our base working path if not overridden
218 config
= getarg(&arglist
, 'C') ? getarg(&arglist
, 'C')->val
: NULL
;
220 if (getarg(&arglist
, 'V') != NULL
) {
221 char * etcpath
= getarg(&arglist
, 'V')->val
;
223 if (config
== NULL
) { /* Only override config location if -C not specified */
224 config
= malloc(MAXPATHLEN
);
225 snprintf(config
, MAXPATHLEN
, "%s/pw.conf", etcpath
);
227 memcpy(&PWF
, &VPWF
, sizeof PWF
);
234 * Now, let's do the common initialisation
236 cnf
= read_userconfig(config
);
238 ch
= funcs
[which
] (cnf
, mode
, &arglist
);
241 * If everything went ok, and we've been asked to update
242 * the NIS maps, then do it now
244 if (ch
== EXIT_SUCCESS
&& getarg(&arglist
, 'Y') != NULL
) {
248 if (chdir(_PATH_YP
) == -1)
249 warn("chdir(" _PATH_YP
")");
250 else if ((pid
= fork()) == -1)
253 /* Is make anywhere else? */
254 execlp("/usr/bin/make", "make", (char *)NULL
);
259 if ((i
= WEXITSTATUS(i
)) != 0)
260 errx(ch
, "make exited with status %d", i
);
262 pw_log(cnf
, mode
, which
, "NIS maps updated");
270 getindex(const char *words
[], const char *word
)
275 if (strcmp(words
[i
], word
) == 0)
284 * This is probably an overkill for a cmdline help system, but it reflects
285 * the complexity of the command line.
289 cmdhelp(int mode
, int which
)
292 fprintf(stderr
, "usage:\n pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n");
294 fprintf(stderr
, "usage:\n pw %s [add|del|mod|show|next] [help|switches/values]\n", Which
[which
]);
298 * We need to give mode specific help
300 static const char *help
[W_NUM
][M_NUM
] =
303 "usage: pw useradd [name] [switches]\n"
304 "\t-V etcdir alternate /etc location\n"
305 "\t-C config configuration file\n"
306 "\t-q quiet operation\n"
308 "\t-n name login name\n"
310 "\t-c comment user name/comment\n"
311 "\t-d directory home directory\n"
312 "\t-e date account expiry date\n"
313 "\t-p date password expiry date\n"
314 "\t-g grp initial group\n"
315 "\t-G grp1,grp2 additional groups\n"
316 "\t-m [ -k dir ] create and set up home\n"
317 "\t-M mode home directory permissions\n"
318 "\t-s shell name of login shell\n"
319 "\t-o duplicate uid ok\n"
320 "\t-L class user class\n"
321 "\t-h fd read password on fd\n"
322 "\t-H fd read encrypted password on fd\n"
323 "\t-Y update NIS maps\n"
325 " Setting defaults:\n"
326 "\t-V etcdir alternate /etc location\n"
327 "\t-D set user defaults\n"
328 "\t-b dir default home root dir\n"
329 "\t-e period default expiry period\n"
330 "\t-p period default password change period\n"
331 "\t-g group default group\n"
332 "\t-G grp1,grp2 additional groups\n"
333 "\t-L class default user class\n"
334 "\t-k dir default home skeleton\n"
335 "\t-M mode home directory permissions\n"
336 "\t-u min,max set min,max uids\n"
337 "\t-i min,max set min,max gids\n"
338 "\t-w method set default password method\n"
339 "\t-s shell default shell\n"
340 "\t-y path set NIS passwd file path\n",
341 "usage: pw userdel [uid|name] [switches]\n"
342 "\t-V etcdir alternate /etc location\n"
343 "\t-n name login name\n"
345 "\t-Y update NIS maps\n"
346 "\t-r remove home & contents\n",
347 "usage: pw usermod [uid|name] [switches]\n"
348 "\t-V etcdir alternate /etc location\n"
349 "\t-C config configuration file\n"
350 "\t-q quiet operation\n"
351 "\t-F force add if no user\n"
352 "\t-n name login name\n"
354 "\t-c comment user name/comment\n"
355 "\t-d directory home directory\n"
356 "\t-e date account expiry date\n"
357 "\t-p date password expiry date\n"
358 "\t-g grp initial group\n"
359 "\t-G grp1,grp2 additional groups\n"
360 "\t-l name new login name\n"
361 "\t-L class user class\n"
362 "\t-m [ -k dir ] create and set up home\n"
363 "\t-M mode home directory permissions\n"
364 "\t-s shell name of login shell\n"
365 "\t-w method set new password using method\n"
366 "\t-h fd read password on fd\n"
367 "\t-H fd read encrypted password on fd\n"
368 "\t-Y update NIS maps\n"
370 "usage: pw usershow [uid|name] [switches]\n"
371 "\t-V etcdir alternate /etc location\n"
372 "\t-n name login name\n"
375 "\t-P prettier format\n"
376 "\t-a print all users\n"
377 "\t-7 print in v7 format\n",
378 "usage: pw usernext [switches]\n"
379 "\t-V etcdir alternate /etc location\n"
380 "\t-C config configuration file\n"
381 "\t-q quiet operation\n",
382 "usage pw: lock [switches]\n"
383 "\t-V etcdir alternate /etc locations\n"
384 "\t-C config configuration file\n"
385 "\t-q quiet operation\n",
386 "usage pw: unlock [switches]\n"
387 "\t-V etcdir alternate /etc locations\n"
388 "\t-C config configuration file\n"
389 "\t-q quiet operation\n"
392 "usage: pw groupadd [group|gid] [switches]\n"
393 "\t-V etcdir alternate /etc location\n"
394 "\t-C config configuration file\n"
395 "\t-q quiet operation\n"
396 "\t-n group group name\n"
397 "\t-g gid group id\n"
398 "\t-M usr1,usr2 add users as group members\n"
399 "\t-o duplicate gid ok\n"
400 "\t-Y update NIS maps\n"
402 "usage: pw groupdel [group|gid] [switches]\n"
403 "\t-V etcdir alternate /etc location\n"
404 "\t-n name group name\n"
405 "\t-g gid group id\n"
406 "\t-Y update NIS maps\n",
407 "usage: pw groupmod [group|gid] [switches]\n"
408 "\t-V etcdir alternate /etc location\n"
409 "\t-C config configuration file\n"
410 "\t-q quiet operation\n"
411 "\t-F force add if not exists\n"
412 "\t-n name group name\n"
413 "\t-g gid group id\n"
414 "\t-M usr1,usr2 replaces users as group members\n"
415 "\t-m usr1,usr2 add users as group members\n"
416 "\t-d usr1,usr2 delete users as group members\n"
417 "\t-l name new group name\n"
418 "\t-Y update NIS maps\n"
420 "usage: pw groupshow [group|gid] [switches]\n"
421 "\t-V etcdir alternate /etc location\n"
422 "\t-n name group name\n"
423 "\t-g gid group id\n"
425 "\t-P prettier format\n"
426 "\t-a print all accounting groups\n",
427 "usage: pw groupnext [switches]\n"
428 "\t-V etcdir alternate /etc location\n"
429 "\t-C config configuration file\n"
430 "\t-q quiet operation\n"
434 fprintf(stderr
, "%s", help
[which
][mode
]);
440 getarg(struct cargs
* _args
, int ch
)
442 struct carg
*c
= LIST_FIRST(_args
);
444 while (c
!= NULL
&& c
->ch
!= ch
)
445 c
= LIST_NEXT(c
, list
);
450 addarg(struct cargs
* _args
, int ch
, char *argstr
)
452 struct carg
*ca
= malloc(sizeof(struct carg
));
455 errx(EX_OSERR
, "out of memory");
458 LIST_INSERT_HEAD(_args
, ca
, list
);