]>
git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw.c
2 * Copyright (c) 1996 by David L. Nugent <davidn@blaze.net.au>.
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 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.
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
37 static char *progname
= "pw";
39 const char *Modes
[] = {"add", "del", "mod", "show", NULL
};
40 const char *Which
[] = {"user", "group", NULL
};
41 static const char *Combo1
[] = {"useradd", "userdel", "usermod", "usershow",
42 "groupadd", "groupdel", "groupmod", "groupshow",
44 static const char *Combo2
[] = {"adduser", "deluser", "moduser", "showuser",
45 "addgroup", "delgroup", "modgroup", "showgroup",
48 static struct cargs arglist
;
50 static int getindex(const char *words
[], const char *word
);
51 static void cmdhelp(int mode
, int which
);
55 main(int argc
, char *argv
[])
62 static const char *opts
[W_NUM
][M_NUM
] =
64 /* user */ {"C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db", "C:qn:u:r", "C:qn:u:c:d:e:p:g:G:mk:s:L:h:F", "C:qn:u:Fpa"},
65 /* grp */ {"C:qn:g:h:p", "C:qn:g:", "C:qn:g:l:h:F", "C:qn:g:Fpa"}
68 static int (*funcs
[W_NUM
]) (struct userconf
* _cnf
, int _mode
, struct cargs
* _args
) =
69 { /* Request handlers */
74 umask(0); /* We wish to handle this manually */
75 progname
= strrchr(argv
[0], '/');
84 * Break off the first couple of words to determine what exactly
85 * we're being asked to do
87 while (argc
> 1 && *argv
[1] != '-') {
90 if ((tmp
= getindex(Modes
, argv
[1])) != -1)
92 else if ((tmp
= getindex(Which
, argv
[1])) != -1)
94 else if ((tmp
= getindex(Combo1
, argv
[1])) != -1 || (tmp
= getindex(Combo2
, argv
[1])) != -1) {
97 } else if (strcmp(argv
[1], "help") == 0)
99 else if (which
!= -1 && mode
!= -1 && arglist
.lh_first
== NULL
)
100 addarg(&arglist
, 'n', argv
[1]);
102 cmderr(X_CMDERR
, "Unknown keyword `%s'\n", argv
[1]);
108 * Bail out unless the user is specific!
110 if (mode
== -1 || which
== -1)
111 cmdhelp(mode
, which
);
114 * Must be root to attempt an update
116 if (getuid() != 0 && mode
!= M_PRINT
)
117 cmderr(X_PERMERR
, "you must be root to run this program\n");
120 * We know which mode we're in and what we're about to do, so now
121 * let's dispatch the remaining command line args in a genric way.
123 argv
[0] = progname
; /* Preserve this */
126 while ((ch
= getopt(argc
, argv
, opts
[which
][mode
])) != -1) {
128 cmderr(X_CMDERR
, NULL
);
130 addarg(&arglist
, ch
, optarg
);
135 * We should immediately look for the -q 'quiet' switch so that we
136 * don't bother with extraneous errors
138 if (getarg(&arglist
, 'q') != NULL
)
139 freopen("/dev/null", "w", stderr
);
144 * Now, let's do the common initialisation
146 cnf
= read_userconfig(getarg(&arglist
, 'C') ? getarg(&arglist
, 'C')->val
: NULL
);
149 ch
= funcs
[which
] (cnf
, mode
, &arglist
);
151 fprintf(stderr
, "%s: %s[%s] not yet implemented.\n", progname
, Which
[which
], Modes
[mode
]);
157 getindex(const char *words
[], const char *word
)
162 if (strcmp(words
[i
], word
) == 0)
171 * This is probably an overkill for a cmdline help system, but it reflects
172 * the complexity of the command line.
178 fprintf(stderr
, "%s: ", progname
);
182 cmderr(int ec
, char const * fmt
,...)
189 vfprintf(stderr
, fmt
, argp
);
196 cmdhelp(int mode
, int which
)
200 fprintf(stderr
, "usage: %s [user|group] [add|del|mod|show] [ help | switches/values ]\n", progname
);
202 fprintf(stderr
, "usage: %s %s [add|del|mod] [ help | switches/values ]\n", progname
, Which
[which
]);
206 * We need to give mode specific help
208 static const char *help
[W_NUM
][M_NUM
] =
211 "usage: %s useradd [name] [switches]\n"
212 "\t-C config configuration file\n"
213 "\t-q quiet operation\n"
215 "\t-n name login name\n"
217 "\t-c comment user name/comment\n"
218 "\t-d directory home directory\n"
219 "\t-e date account expiry date\n"
220 "\t-p date password expiry date\n"
221 "\t-g grp initial group\n"
222 "\t-G grp1,grp2 additional groups\n"
223 "\t-m [ -k dir ] create and set up home\n"
224 "\t-s shell name of login shell\n"
225 "\t-o duplicate uid ok\n"
226 "\t-L class user class\n"
227 "\t-h fd read password on fd\n"
228 " Setting defaults:\n"
229 "\t-D set user defaults\n"
230 "\t-b dir default home root dir\n"
231 "\t-e period default expiry period\n"
232 "\t-p period default password change period\n"
233 "\t-g group default group\n"
234 "\t-G grp1,grp2 additional groups\n"
235 "\t-L class default user class\n"
236 "\t-k dir default home skeleton\n"
237 "\t-u min,max set min,max uids\n"
238 "\t-i min,max set min,max gids\n"
239 "\t-w method set default password method\n"
240 "\t-s shell default shell\n",
241 "usage: %s userdel [uid|name] [switches]\n"
242 "\t-n name login name\n"
244 "\t-r remove home & contents\n",
245 "usage: %s usermod [uid|name] [switches]\n"
246 "\t-C config configuration file\n"
247 "\t-q quiet operation\n"
248 "\t-F force add if no user\n"
249 "\t-n name login name\n"
251 "\t-c comment user name/comment\n"
252 "\t-d directory home directory\n"
253 "\t-e date account expiry date\n"
254 "\t-p date password expiry date\n"
255 "\t-g grp initial group\n"
256 "\t-G grp1,grp2 additional groups\n"
257 "\t-l name new login name\n"
258 "\t-L class user class\n"
259 "\t-m [ -k dir ] create and set up home\n"
260 "\t-s shell name of login shell\n"
261 "\t-h fd read password on fd\n",
262 "usage: %s usershow [uid|name] [switches]\n"
263 "\t-n name login name\n"
266 "\t-p prettier format\n"
267 "\t-a print all users\n"
270 "usage: %s groupadd [group|gid] [switches]\n"
271 "\t-C config configuration file\n"
272 "\t-q quiet operation\n"
273 "\t-n group group name\n"
274 "\t-g gid group id\n"
275 "\t-o duplicate gid ok\n",
276 "usage: %s groupdel [group|gid] [switches]\n"
277 "\t-n name group name\n"
278 "\t-g gid group id\n",
279 "usage: %s groupmod [group|gid] [switches]\n"
280 "\t-C config configuration file\n"
281 "\t-q quiet operation\n"
282 "\t-F force add if not exists\n"
283 "\t-n name group name\n"
284 "\t-g gid group id\n"
285 "\t-l name new group name\n",
286 "usage: %s groupshow [group|gid] [switches]\n"
287 "\t-n name group name\n"
288 "\t-g gid group id\n"
290 "\t-p prettier format\n"
291 "\t-a print all accounting groups\n"
295 fprintf(stderr
, help
[which
][mode
], progname
);
301 getarg(struct cargs
* _args
, int ch
)
303 struct carg
*c
= _args
->lh_first
;
305 while (c
!= NULL
&& c
->ch
!= ch
)
311 addarg(struct cargs
* _args
, int ch
, char *argstr
)
313 struct carg
*ca
= malloc(sizeof(struct carg
));
316 cmderr(X_MEMERR
, "Abort - out of memory\n");
319 LIST_INSERT_HEAD(_args
, ca
, list
);