]>
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
33 static char *progname
= "pw";
35 const char *Modes
[] = {"add", "del", "mod", "show", "next", NULL
};
36 const char *Which
[] = {"user", "group", NULL
};
37 static const char *Combo1
[] = {
38 "useradd", "userdel", "usermod", "usershow", "usernext",
39 "groupadd", "groupdel", "groupmod", "groupshow", "groupnext",
41 static const char *Combo2
[] = {
42 "adduser", "deluser", "moduser", "showuser", "nextuser",
43 "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup",
46 static struct cargs arglist
;
48 static int getindex(const char *words
[], const char *word
);
49 static void cmdhelp(int mode
, int which
);
53 main(int argc
, char *argv
[])
60 static const char *opts
[W_NUM
][M_NUM
] =
63 "C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db:NPy:Y",
65 "C:qn:u:c:d:e:p:g:G:ml:k:s:w:L:h:FNPY",
72 "C:qn:g:l:h:FM:m:NPY",
78 static int (*funcs
[W_NUM
]) (struct userconf
* _cnf
, int _mode
, struct cargs
* _args
) =
79 { /* Request handlers */
84 umask(0); /* We wish to handle this manually */
85 progname
= strrchr(argv
[0], '/');
94 * Break off the first couple of words to determine what exactly
95 * we're being asked to do
97 while (argc
> 1 && *argv
[1] != '-') {
100 if ((tmp
= getindex(Modes
, argv
[1])) != -1)
102 else if ((tmp
= getindex(Which
, argv
[1])) != -1)
104 else if ((tmp
= getindex(Combo1
, argv
[1])) != -1 || (tmp
= getindex(Combo2
, argv
[1])) != -1) {
107 } else if (strcmp(argv
[1], "help") == 0)
108 cmdhelp(mode
, which
);
109 else if (which
!= -1 && mode
!= -1 && arglist
.lh_first
== NULL
)
110 addarg(&arglist
, 'n', argv
[1]);
112 cmderr(EX_USAGE
, "Unknown keyword `%s'\n", argv
[1]);
118 * Bail out unless the user is specific!
120 if (mode
== -1 || which
== -1)
121 cmdhelp(mode
, which
);
124 * We know which mode we're in and what we're about to do, so now
125 * let's dispatch the remaining command line args in a genric way.
127 argv
[0] = progname
; /* Preserve this */
130 while ((ch
= getopt(argc
, argv
, opts
[which
][mode
])) != -1) {
132 cmderr(EX_USAGE
, NULL
);
134 addarg(&arglist
, ch
, optarg
);
139 * Must be root to attempt an update
141 if (getuid() != 0 && mode
!= M_PRINT
&& mode
!= M_NEXT
&& getarg(&arglist
, 'N')==NULL
)
142 cmderr(EX_NOPERM
, "you must be root to run this program\n");
145 * We should immediately look for the -q 'quiet' switch so that we
146 * don't bother with extraneous errors
148 if (getarg(&arglist
, 'q') != NULL
)
149 freopen("/dev/null", "w", stderr
);
152 * Now, let's do the common initialisation
154 cnf
= read_userconfig(getarg(&arglist
, 'C') ? getarg(&arglist
, 'C')->val
: NULL
);
155 ch
= funcs
[which
] (cnf
, mode
, &arglist
);
158 * If everything went ok, and we've been asked to update
159 * the NIS maps, then do it now
161 if (ch
== EXIT_SUCCESS
&& getarg(&arglist
, 'Y') != NULL
) {
165 if (chdir(_PATH_YP
) == -1)
166 perror("chdir(" _PATH_YP
")");
167 else if ((pid
= fork()) == -1)
170 /* Is make anywhere else? */
171 execlp("/usr/bin/make", "make", NULL
);
176 if ((i
= WEXITSTATUS(i
)) != 0)
177 cmderr(ch
, "warning: make exited with status %d\n", i
);
179 pw_log(cnf
, mode
, which
, "NIS maps updated");
186 getindex(const char *words
[], const char *word
)
191 if (strcmp(words
[i
], word
) == 0)
200 * This is probably an overkill for a cmdline help system, but it reflects
201 * the complexity of the command line.
207 fprintf(stderr
, "%s: ", progname
);
211 cmderr(int ec
, char const * fmt
,...)
218 vfprintf(stderr
, fmt
, argp
);
225 cmdhelp(int mode
, int which
)
229 fprintf(stderr
, "usage: %s [user|group] [add|del|mod|show|next] [ help | switches/values ]\n", progname
);
231 fprintf(stderr
, "usage: %s %s [add|del|mod|show|next] [ help | switches/values ]\n", progname
, Which
[which
]);
235 * We need to give mode specific help
237 static const char *help
[W_NUM
][M_NUM
] =
240 "usage: %s useradd [name] [switches]\n"
241 "\t-C config configuration file\n"
242 "\t-q quiet operation\n"
244 "\t-n name login name\n"
246 "\t-c comment user name/comment\n"
247 "\t-d directory home directory\n"
248 "\t-e date account expiry date\n"
249 "\t-p date password expiry date\n"
250 "\t-g grp initial group\n"
251 "\t-G grp1,grp2 additional groups\n"
252 "\t-m [ -k dir ] create and set up home\n"
253 "\t-s shell name of login shell\n"
254 "\t-o duplicate uid ok\n"
255 "\t-L class user class\n"
256 "\t-h fd read password on fd\n"
257 "\t-Y update NIS maps\n"
259 " Setting defaults:\n"
260 "\t-D set user defaults\n"
261 "\t-b dir default home root dir\n"
262 "\t-e period default expiry period\n"
263 "\t-p period default password change period\n"
264 "\t-g group default group\n"
265 "\t-G grp1,grp2 additional groups\n"
266 "\t-L class default user class\n"
267 "\t-k dir default home skeleton\n"
268 "\t-u min,max set min,max uids\n"
269 "\t-i min,max set min,max gids\n"
270 "\t-w method set default password method\n"
271 "\t-s shell default shell\n"
272 "\t-y path set NIS passwd file path\n",
273 "usage: %s userdel [uid|name] [switches]\n"
274 "\t-n name login name\n"
276 "\t-Y update NIS maps\n"
277 "\t-r remove home & contents\n",
278 "usage: %s usermod [uid|name] [switches]\n"
279 "\t-C config configuration file\n"
280 "\t-q quiet operation\n"
281 "\t-F force add if no user\n"
282 "\t-n name login name\n"
284 "\t-c comment user name/comment\n"
285 "\t-d directory home directory\n"
286 "\t-e date account expiry date\n"
287 "\t-p date password expiry date\n"
288 "\t-g grp initial group\n"
289 "\t-G grp1,grp2 additional groups\n"
290 "\t-l name new login name\n"
291 "\t-L class user class\n"
292 "\t-m [ -k dir ] create and set up home\n"
293 "\t-s shell name of login shell\n"
294 "\t-w method set new password using method\n"
295 "\t-h fd read password on fd\n"
296 "\t-Y update NIS maps\n"
298 "usage: %s usershow [uid|name] [switches]\n"
299 "\t-n name login name\n"
302 "\t-P prettier format\n"
303 "\t-a print all users\n",
304 "usage: %s usernext [switches]\n"
305 "\t-C config configuration file\n"
308 "usage: %s groupadd [group|gid] [switches]\n"
309 "\t-C config configuration file\n"
310 "\t-q quiet operation\n"
311 "\t-n group group name\n"
312 "\t-g gid group id\n"
313 "\t-M usr1,usr2 add users as group members\n"
314 "\t-o duplicate gid ok\n"
315 "\t-Y update NIS maps\n"
317 "usage: %s groupdel [group|gid] [switches]\n"
318 "\t-n name group name\n"
319 "\t-g gid group id\n"
320 "\t-Y update NIS maps\n",
321 "usage: %s groupmod [group|gid] [switches]\n"
322 "\t-C config configuration file\n"
323 "\t-q quiet operation\n"
324 "\t-F force add if not exists\n"
325 "\t-n name group name\n"
326 "\t-g gid group id\n"
327 "\t-M usr1,usr2 replaces users as group members\n"
328 "\t-m usr1,usr2 add users as group members\n"
329 "\t-l name new group name\n"
330 "\t-Y update NIS maps\n"
332 "usage: %s groupshow [group|gid] [switches]\n"
333 "\t-n name group name\n"
334 "\t-g gid group id\n"
336 "\t-P prettier format\n"
337 "\t-a print all accounting groups\n",
338 "usage: %s groupnext [switches]\n"
339 "\t-C config configuration file\n"
343 fprintf(stderr
, help
[which
][mode
], progname
);
349 getarg(struct cargs
* _args
, int ch
)
351 struct carg
*c
= _args
->lh_first
;
353 while (c
!= NULL
&& c
->ch
!= ch
)
359 addarg(struct cargs
* _args
, int ch
, char *argstr
)
361 struct carg
*ca
= malloc(sizeof(struct carg
));
364 cmderr(EX_OSERR
, "Abort - out of memory\n");
367 LIST_INSERT_HEAD(_args
, ca
, list
);