]>
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
26 * $Id: pw.c,v 1.1.1.2 1996/12/09 23:55:20 joerg Exp $
31 static char *progname
= "pw";
33 const char *Modes
[] = {"add", "del", "mod", "show", "next", NULL
};
34 const char *Which
[] = {"user", "group", NULL
};
35 static const char *Combo1
[] = {
36 "useradd", "userdel", "usermod", "usershow", "usernext",
37 "groupadd", "groupdel", "groupmod", "groupshow", "groupnext",
39 static const char *Combo2
[] = {
40 "adduser", "deluser", "moduser", "showuser", "nextuser",
41 "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup",
44 static struct cargs arglist
;
46 static int getindex(const char *words
[], const char *word
);
47 static void cmdhelp(int mode
, int which
);
51 main(int argc
, char *argv
[])
58 static const char *opts
[W_NUM
][M_NUM
] =
61 "C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db:NP",
63 "C:qn:u:c:d:e:p:g:G:mk:s:w:L:h:FNP",
76 static int (*funcs
[W_NUM
]) (struct userconf
* _cnf
, int _mode
, struct cargs
* _args
) =
77 { /* Request handlers */
82 umask(0); /* We wish to handle this manually */
83 progname
= strrchr(argv
[0], '/');
92 * Break off the first couple of words to determine what exactly
93 * we're being asked to do
95 while (argc
> 1 && *argv
[1] != '-') {
98 if ((tmp
= getindex(Modes
, argv
[1])) != -1)
100 else if ((tmp
= getindex(Which
, argv
[1])) != -1)
102 else if ((tmp
= getindex(Combo1
, argv
[1])) != -1 || (tmp
= getindex(Combo2
, argv
[1])) != -1) {
105 } else if (strcmp(argv
[1], "help") == 0)
106 cmdhelp(mode
, which
);
107 else if (which
!= -1 && mode
!= -1 && arglist
.lh_first
== NULL
)
108 addarg(&arglist
, 'n', argv
[1]);
110 cmderr(EX_USAGE
, "Unknown keyword `%s'\n", argv
[1]);
116 * Bail out unless the user is specific!
118 if (mode
== -1 || which
== -1)
119 cmdhelp(mode
, which
);
122 * We know which mode we're in and what we're about to do, so now
123 * let's dispatch the remaining command line args in a genric way.
125 argv
[0] = progname
; /* Preserve this */
128 while ((ch
= getopt(argc
, argv
, opts
[which
][mode
])) != -1) {
130 cmderr(EX_USAGE
, NULL
);
132 addarg(&arglist
, ch
, optarg
);
137 * Must be root to attempt an update
139 if (getuid() != 0 && mode
!= M_PRINT
&& mode
!= M_NEXT
&& getarg(&arglist
, 'N')==NULL
)
140 cmderr(EX_NOPERM
, "you must be root to run this program\n");
143 * We should immediately look for the -q 'quiet' switch so that we
144 * don't bother with extraneous errors
146 if (getarg(&arglist
, 'q') != NULL
)
147 freopen("/dev/null", "w", stderr
);
150 * Now, let's do the common initialisation
152 cnf
= read_userconfig(getarg(&arglist
, 'C') ? getarg(&arglist
, 'C')->val
: NULL
);
153 return funcs
[which
] (cnf
, mode
, &arglist
);
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|next] [ help | switches/values ]\n", progname
);
202 fprintf(stderr
, "usage: %s %s [add|del|mod|show|next] [ 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"
229 " Setting defaults:\n"
230 "\t-D set user defaults\n"
231 "\t-b dir default home root dir\n"
232 "\t-e period default expiry period\n"
233 "\t-p period default password change period\n"
234 "\t-g group default group\n"
235 "\t-G grp1,grp2 additional groups\n"
236 "\t-L class default user class\n"
237 "\t-k dir default home skeleton\n"
238 "\t-u min,max set min,max uids\n"
239 "\t-i min,max set min,max gids\n"
240 "\t-w method set default password method\n"
241 "\t-s shell default shell\n",
242 "usage: %s userdel [uid|name] [switches]\n"
243 "\t-n name login name\n"
245 "\t-r remove home & contents\n",
246 "usage: %s usermod [uid|name] [switches]\n"
247 "\t-C config configuration file\n"
248 "\t-q quiet operation\n"
249 "\t-F force add if no user\n"
250 "\t-n name login name\n"
252 "\t-c comment user name/comment\n"
253 "\t-d directory home directory\n"
254 "\t-e date account expiry date\n"
255 "\t-p date password expiry date\n"
256 "\t-g grp initial group\n"
257 "\t-G grp1,grp2 additional groups\n"
258 "\t-l name new login name\n"
259 "\t-L class user class\n"
260 "\t-m [ -k dir ] create and set up home\n"
261 "\t-s shell name of login shell\n"
262 "\t-w method set new password using method\n"
263 "\t-h fd read password on fd\n"
265 "usage: %s usershow [uid|name] [switches]\n"
266 "\t-n name login name\n"
269 "\t-P prettier format\n"
270 "\t-a print all users\n",
271 "usage: %s usernext [switches]\n"
272 "\t-C config configuration file\n"
275 "usage: %s groupadd [group|gid] [switches]\n"
276 "\t-C config configuration file\n"
277 "\t-q quiet operation\n"
278 "\t-n group group name\n"
279 "\t-g gid group id\n"
280 "\t-M usr1,usr2 add users as group members\n"
281 "\t-o duplicate gid ok\n"
283 "usage: %s groupdel [group|gid] [switches]\n"
284 "\t-n name group name\n"
285 "\t-g gid group id\n",
286 "usage: %s groupmod [group|gid] [switches]\n"
287 "\t-C config configuration file\n"
288 "\t-q quiet operation\n"
289 "\t-F force add if not exists\n"
290 "\t-n name group name\n"
291 "\t-g gid group id\n"
292 "\t-M usr1,usr2 replaces users as group members\n"
293 "\t-m usr1,usr2 add users as group members\n"
294 "\t-l name new group name\n"
296 "usage: %s groupshow [group|gid] [switches]\n"
297 "\t-n name group name\n"
298 "\t-g gid group id\n"
300 "\t-P prettier format\n"
301 "\t-a print all accounting groups\n",
302 "usage: %s groupnext [switches]\n"
303 "\t-C config configuration file\n"
307 fprintf(stderr
, help
[which
][mode
], progname
);
313 getarg(struct cargs
* _args
, int ch
)
315 struct carg
*c
= _args
->lh_first
;
317 while (c
!= NULL
&& c
->ch
!= ch
)
323 addarg(struct cargs
* _args
, int ch
, char *argstr
)
325 struct carg
*ca
= malloc(sizeof(struct carg
));
328 cmderr(EX_OSERR
, "Abort - out of memory\n");
331 LIST_INSERT_HEAD(_args
, ca
, list
);