]> git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw.c
Upgrade from the author, reflecting all my wishes resulting out of the
[pw-darwin.git] / pw / pw.c
1 /*-
2 * Copyright (c) 1996 by David L. Nugent <davidn@blaze.net.au>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer as
10 * the first lines of this file unmodified.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by David L. Nugent.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE DAVID L. NUGENT ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $Id$
33 */
34
35 #include "pw.h"
36
37 static char *progname = "pw";
38
39 const char *Modes[] = {"add", "del", "mod", "show", "next", NULL};
40 const char *Which[] = {"user", "group", NULL};
41 static const char *Combo1[] = {
42 "useradd", "userdel", "usermod", "usershow", "usernext",
43 "groupadd", "groupdel", "groupmod", "groupshow", "groupnext",
44 NULL};
45 static const char *Combo2[] = {
46 "adduser", "deluser", "moduser", "showuser", "nextuser",
47 "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup",
48 NULL};
49
50 static struct cargs arglist;
51
52 static int getindex(const char *words[], const char *word);
53 static void cmdhelp(int mode, int which);
54
55
56 int
57 main(int argc, char *argv[])
58 {
59 int ch;
60 int mode = -1;
61 int which = -1;
62 struct userconf *cnf;
63
64 static const char *opts[W_NUM][M_NUM] =
65 {
66 { /* user */
67 "C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db:NP",
68 "C:qn:u:r",
69 "C:qn:u:c:d:e:p:g:G:mk:s:w:L:h:FNP",
70 "C:qn:u:FPa",
71 "C:q"
72 },
73 { /* grp */
74 "C:qn:g:h:M:pNP",
75 "C:qn:g:",
76 "C:qn:g:l:h:FM:m:NP",
77 "C:qn:g:FPa",
78 "C:q"
79 }
80 };
81
82 static int (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) =
83 { /* Request handlers */
84 pw_user,
85 pw_group
86 };
87
88 umask(0); /* We wish to handle this manually */
89 progname = strrchr(argv[0], '/');
90 if (progname != NULL)
91 ++progname;
92 else
93 progname = argv[0];
94
95 LIST_INIT(&arglist);
96
97 /*
98 * Break off the first couple of words to determine what exactly
99 * we're being asked to do
100 */
101 while (argc > 1 && *argv[1] != '-') {
102 int tmp;
103
104 if ((tmp = getindex(Modes, argv[1])) != -1)
105 mode = tmp;
106 else if ((tmp = getindex(Which, argv[1])) != -1)
107 which = tmp;
108 else if ((tmp = getindex(Combo1, argv[1])) != -1 || (tmp = getindex(Combo2, argv[1])) != -1) {
109 which = tmp / M_NUM;
110 mode = tmp % M_NUM;
111 } else if (strcmp(argv[1], "help") == 0)
112 cmdhelp(mode, which);
113 else if (which != -1 && mode != -1 && arglist.lh_first == NULL)
114 addarg(&arglist, 'n', argv[1]);
115 else
116 cmderr(EX_USAGE, "Unknown keyword `%s'\n", argv[1]);
117 ++argv;
118 --argc;
119 }
120
121 /*
122 * Bail out unless the user is specific!
123 */
124 if (mode == -1 || which == -1)
125 cmdhelp(mode, which);
126
127 /*
128 * We know which mode we're in and what we're about to do, so now
129 * let's dispatch the remaining command line args in a genric way.
130 */
131 argv[0] = progname; /* Preserve this */
132 optarg = NULL;
133
134 while ((ch = getopt(argc, argv, opts[which][mode])) != -1) {
135 if (ch == '?')
136 cmderr(EX_USAGE, NULL);
137 else
138 addarg(&arglist, ch, optarg);
139 optarg = NULL;
140 }
141
142 /*
143 * Must be root to attempt an update
144 */
145 if (getuid() != 0 && mode != M_PRINT && mode != M_NEXT && getarg(&arglist, 'N')==NULL)
146 cmderr(EX_NOPERM, "you must be root to run this program\n");
147
148 /*
149 * We should immediately look for the -q 'quiet' switch so that we
150 * don't bother with extraneous errors
151 */
152 if (getarg(&arglist, 'q') != NULL)
153 freopen("/dev/null", "w", stderr);
154
155 /*
156 * Now, let's do the common initialisation
157 */
158 cnf = read_userconfig(getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL);
159 return funcs[which] (cnf, mode, &arglist);
160 }
161
162 static int
163 getindex(const char *words[], const char *word)
164 {
165 int i = 0;
166
167 while (words[i]) {
168 if (strcmp(words[i], word) == 0)
169 return i;
170 i++;
171 }
172 return -1;
173 }
174
175
176 /*
177 * This is probably an overkill for a cmdline help system, but it reflects
178 * the complexity of the command line.
179 */
180
181 static void
182 banner(void)
183 {
184 fprintf(stderr, "%s: ", progname);
185 }
186
187 void
188 cmderr(int ec, char const * fmt,...)
189 {
190 if (fmt != NULL) {
191 va_list argp;
192
193 banner();
194 va_start(argp, fmt);
195 vfprintf(stderr, fmt, argp);
196 va_end(argp);
197 }
198 exit(ec);
199 }
200
201 static void
202 cmdhelp(int mode, int which)
203 {
204 banner();
205 if (which == -1)
206 fprintf(stderr, "usage: %s [user|group] [add|del|mod|show|next] [ help | switches/values ]\n", progname);
207 else if (mode == -1)
208 fprintf(stderr, "usage: %s %s [add|del|mod|show|next] [ help | switches/values ]\n", progname, Which[which]);
209 else {
210
211 /*
212 * We need to give mode specific help
213 */
214 static const char *help[W_NUM][M_NUM] =
215 {
216 {
217 "usage: %s useradd [name] [switches]\n"
218 "\t-C config configuration file\n"
219 "\t-q quiet operation\n"
220 " Adding users:\n"
221 "\t-n name login name\n"
222 "\t-u uid user id\n"
223 "\t-c comment user name/comment\n"
224 "\t-d directory home directory\n"
225 "\t-e date account expiry date\n"
226 "\t-p date password expiry date\n"
227 "\t-g grp initial group\n"
228 "\t-G grp1,grp2 additional groups\n"
229 "\t-m [ -k dir ] create and set up home\n"
230 "\t-s shell name of login shell\n"
231 "\t-o duplicate uid ok\n"
232 "\t-L class user class\n"
233 "\t-h fd read password on fd\n"
234 "\t-N no update\n"
235 " Setting defaults:\n"
236 "\t-D set user defaults\n"
237 "\t-b dir default home root dir\n"
238 "\t-e period default expiry period\n"
239 "\t-p period default password change period\n"
240 "\t-g group default group\n"
241 "\t-G grp1,grp2 additional groups\n"
242 "\t-L class default user class\n"
243 "\t-k dir default home skeleton\n"
244 "\t-u min,max set min,max uids\n"
245 "\t-i min,max set min,max gids\n"
246 "\t-w method set default password method\n"
247 "\t-s shell default shell\n",
248 "usage: %s userdel [uid|name] [switches]\n"
249 "\t-n name login name\n"
250 "\t-u uid user id\n"
251 "\t-r remove home & contents\n",
252 "usage: %s usermod [uid|name] [switches]\n"
253 "\t-C config configuration file\n"
254 "\t-q quiet operation\n"
255 "\t-F force add if no user\n"
256 "\t-n name login name\n"
257 "\t-u uid user id\n"
258 "\t-c comment user name/comment\n"
259 "\t-d directory home directory\n"
260 "\t-e date account expiry date\n"
261 "\t-p date password expiry date\n"
262 "\t-g grp initial group\n"
263 "\t-G grp1,grp2 additional groups\n"
264 "\t-l name new login name\n"
265 "\t-L class user class\n"
266 "\t-m [ -k dir ] create and set up home\n"
267 "\t-s shell name of login shell\n"
268 "\t-w method set new password using method\n"
269 "\t-h fd read password on fd\n"
270 "\t-N no update\n",
271 "usage: %s usershow [uid|name] [switches]\n"
272 "\t-n name login name\n"
273 "\t-u uid user id\n"
274 "\t-F force print\n"
275 "\t-P prettier format\n"
276 "\t-a print all users\n",
277 "usage: %s usernext [switches]\n"
278 "\t-C config configuration file\n"
279 },
280 {
281 "usage: %s groupadd [group|gid] [switches]\n"
282 "\t-C config configuration file\n"
283 "\t-q quiet operation\n"
284 "\t-n group group name\n"
285 "\t-g gid group id\n"
286 "\t-M usr1,usr2 add users as group members\n"
287 "\t-o duplicate gid ok\n"
288 "\t-N no update\n",
289 "usage: %s groupdel [group|gid] [switches]\n"
290 "\t-n name group name\n"
291 "\t-g gid group id\n",
292 "usage: %s groupmod [group|gid] [switches]\n"
293 "\t-C config configuration file\n"
294 "\t-q quiet operation\n"
295 "\t-F force add if not exists\n"
296 "\t-n name group name\n"
297 "\t-g gid group id\n"
298 "\t-M usr1,usr2 replaces users as group members\n"
299 "\t-m usr1,usr2 add users as group members\n"
300 "\t-l name new group name\n"
301 "\t-N no update\n",
302 "usage: %s groupshow [group|gid] [switches]\n"
303 "\t-n name group name\n"
304 "\t-g gid group id\n"
305 "\t-F force print\n"
306 "\t-P prettier format\n"
307 "\t-a print all accounting groups\n",
308 "usage: %s groupnext [switches]\n"
309 "\t-C config configuration file\n"
310 }
311 };
312
313 fprintf(stderr, help[which][mode], progname);
314 }
315 exit(EXIT_FAILURE);
316 }
317
318 struct carg *
319 getarg(struct cargs * _args, int ch)
320 {
321 struct carg *c = _args->lh_first;
322
323 while (c != NULL && c->ch != ch)
324 c = c->list.le_next;
325 return c;
326 }
327
328 struct carg *
329 addarg(struct cargs * _args, int ch, char *argstr)
330 {
331 struct carg *ca = malloc(sizeof(struct carg));
332
333 if (ca == NULL)
334 cmderr(EX_OSERR, "Abort - out of memory\n");
335 ca->ch = ch;
336 ca->val = argstr;
337 LIST_INSERT_HEAD(_args, ca, list);
338 return ca;
339 }