]>
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
[] =
41 const char *Modes
[] = {
42 "add", "del", "mod", "show", "next",
44 const char *Which
[] = {"user", "group", NULL
};
45 static const char *Combo1
[] = {
46 "useradd", "userdel", "usermod", "usershow", "usernext",
48 "groupadd", "groupdel", "groupmod", "groupshow", "groupnext",
50 static const char *Combo2
[] = {
51 "adduser", "deluser", "moduser", "showuser", "nextuser",
53 "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup",
86 static int (*cmdfunc
[W_NUM
][M_NUM
])(int argc
, char **argv
, char *_name
) = {
107 static int getindex(const char *words
[], const char *word
);
108 static void cmdhelp(int mode
, int which
);
111 main(int argc
, char *argv
[])
113 int mode
= -1, which
= -1, tmp
;
119 relocated
= nis
= false;
120 memset(&conf
, 0, sizeof(conf
));
121 strlcpy(conf
.rootdir
, "/", sizeof(conf
.rootdir
));
122 strlcpy(conf
.etcpath
, _PATH_PWD
, sizeof(conf
.etcpath
));
124 conf
.checkduplicate
= true;
126 setlocale(LC_ALL
, "");
129 * Break off the first couple of words to determine what exactly
130 * we're being asked to do
133 if (*argv
[1] == '-') {
135 * Special case, allow pw -V<dir> <operation> [args] for scripts etc.
138 if (arg
== 'V' || arg
== 'R') {
140 errx(EXIT_FAILURE
, "Both '-R' and '-V' "
141 "specified, only one accepted");
143 optarg
= &argv
[1][2];
144 if (*optarg
== '\0') {
145 if (stat(argv
[2], &st
) != 0)
147 "no such directory `%s'",
149 if (!S_ISDIR(st
.st_mode
))
150 errx(EX_OSFILE
, "`%s' not a "
151 "directory", argv
[2]);
156 memcpy(&PWF
, &VPWF
, sizeof PWF
);
158 strlcpy(conf
.rootdir
, optarg
,
159 sizeof(conf
.rootdir
));
160 PWF
._altdir
= PWF_ROOTDIR
;
162 snprintf(conf
.etcpath
, sizeof(conf
.etcpath
),
163 "%s%s", optarg
, arg
== 'R' ? "/etc" : "");
167 else if (mode
== -1 && (tmp
= getindex(Modes
, argv
[1])) != -1)
169 else if (which
== -1 && (tmp
= getindex(Which
, argv
[1])) != -1)
171 else if ((mode
== -1 && which
== -1) &&
172 ((tmp
= getindex(Combo1
, argv
[1])) != -1 ||
173 (tmp
= getindex(Combo2
, argv
[1])) != -1)) {
176 } else if (strcmp(argv
[1], "help") == 0 && argv
[2] == NULL
)
177 cmdhelp(mode
, which
);
178 else if (which
!= -1 && mode
!= -1)
181 errx(EX_USAGE
, "unknown keyword `%s'", argv
[1]);
187 * Bail out unless the user is specific!
189 if (mode
== -1 || which
== -1)
190 cmdhelp(mode
, which
);
192 conf
.rootfd
= open(conf
.rootdir
, O_DIRECTORY
|O_CLOEXEC
);
193 if (conf
.rootfd
== -1)
194 errx(EXIT_FAILURE
, "Unable to open '%s'", conf
.rootdir
);
196 return (cmdfunc
[which
][mode
](argc
, argv
, arg1
));
201 getindex(const char *words
[], const char *word
)
206 if (strcmp(words
[i
], word
) == 0)
215 * This is probably an overkill for a cmdline help system, but it reflects
216 * the complexity of the command line.
220 cmdhelp(int mode
, int which
)
223 fprintf(stderr
, "usage:\n pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n");
225 fprintf(stderr
, "usage:\n pw %s [add|del|mod|show|next] [help|switches/values]\n", Which
[which
]);
229 * We need to give mode specific help
231 static const char *help
[W_NUM
][M_NUM
] =
234 "usage: pw useradd [name] [switches]\n"
235 "\t-V etcdir alternate /etc location\n"
236 "\t-R rootir alternate root directory\n"
237 "\t-C config configuration file\n"
238 "\t-q quiet operation\n"
240 "\t-n name login name\n"
242 "\t-c comment user name/comment\n"
243 "\t-d directory home directory\n"
244 "\t-e date account expiry date\n"
245 "\t-p date password expiry date\n"
246 "\t-g grp initial group\n"
247 "\t-G grp1,grp2 additional groups\n"
248 "\t-m [ -k dir ] create and set up home\n"
249 "\t-M mode home directory permissions\n"
250 "\t-s shell name of login shell\n"
251 "\t-o duplicate uid ok\n"
252 "\t-L class user class\n"
253 "\t-h fd read password on fd\n"
254 "\t-H fd read encrypted password on fd\n"
255 "\t-Y update NIS maps\n"
257 " Setting defaults:\n"
258 "\t-V etcdir alternate /etc location\n"
259 "\t-R rootir alternate root directory\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-M mode home directory permissions\n"
269 "\t-u min,max set min,max uids\n"
270 "\t-i min,max set min,max gids\n"
271 "\t-w method set default password method\n"
272 "\t-s shell default shell\n"
273 "\t-y path set NIS passwd file path\n",
274 "usage: pw userdel [uid|name] [switches]\n"
275 "\t-V etcdir alternate /etc location\n"
276 "\t-R rootir alternate root directory\n"
277 "\t-n name login name\n"
279 "\t-Y update NIS maps\n"
280 "\t-y path set NIS passwd file path\n"
281 "\t-r remove home & contents\n",
282 "usage: pw usermod [uid|name] [switches]\n"
283 "\t-V etcdir alternate /etc location\n"
284 "\t-R rootir alternate root directory\n"
285 "\t-C config configuration file\n"
286 "\t-q quiet operation\n"
287 "\t-F force add if no user\n"
288 "\t-n name login name\n"
290 "\t-c comment user name/comment\n"
291 "\t-d directory home directory\n"
292 "\t-e date account expiry date\n"
293 "\t-p date password expiry date\n"
294 "\t-g grp initial group\n"
295 "\t-G grp1,grp2 additional groups\n"
296 "\t-l name new login name\n"
297 "\t-L class user class\n"
298 "\t-m [ -k dir ] create and set up home\n"
299 "\t-M mode home directory permissions\n"
300 "\t-s shell name of login shell\n"
301 "\t-w method set new password using method\n"
302 "\t-h fd read password on fd\n"
303 "\t-H fd read encrypted password on fd\n"
304 "\t-Y update NIS maps\n"
305 "\t-y path set NIS passwd file path\n"
307 "usage: pw usershow [uid|name] [switches]\n"
308 "\t-V etcdir alternate /etc location\n"
309 "\t-R rootir alternate root directory\n"
310 "\t-n name login name\n"
313 "\t-P prettier format\n"
314 "\t-a print all users\n"
315 "\t-7 print in v7 format\n",
316 "usage: pw usernext [switches]\n"
317 "\t-V etcdir alternate /etc location\n"
318 "\t-R rootir alternate root directory\n"
319 "\t-C config configuration file\n"
320 "\t-q quiet operation\n",
321 "usage pw: lock [switches]\n"
322 "\t-V etcdir alternate /etc locations\n"
323 "\t-C config configuration file\n"
324 "\t-q quiet operation\n",
325 "usage pw: unlock [switches]\n"
326 "\t-V etcdir alternate /etc locations\n"
327 "\t-C config configuration file\n"
328 "\t-q quiet operation\n"
331 "usage: pw groupadd [group|gid] [switches]\n"
332 "\t-V etcdir alternate /etc location\n"
333 "\t-R rootir alternate root directory\n"
334 "\t-C config configuration file\n"
335 "\t-q quiet operation\n"
336 "\t-n group group name\n"
337 "\t-g gid group id\n"
338 "\t-M usr1,usr2 add users as group members\n"
339 "\t-o duplicate gid ok\n"
340 "\t-Y update NIS maps\n"
342 "usage: pw groupdel [group|gid] [switches]\n"
343 "\t-V etcdir alternate /etc location\n"
344 "\t-R rootir alternate root directory\n"
345 "\t-n name group name\n"
346 "\t-g gid group id\n"
347 "\t-Y update NIS maps\n",
348 "usage: pw groupmod [group|gid] [switches]\n"
349 "\t-V etcdir alternate /etc location\n"
350 "\t-R rootir alternate root directory\n"
351 "\t-C config configuration file\n"
352 "\t-q quiet operation\n"
353 "\t-F force add if not exists\n"
354 "\t-n name group name\n"
355 "\t-g gid group id\n"
356 "\t-M usr1,usr2 replaces users as group members\n"
357 "\t-m usr1,usr2 add users as group members\n"
358 "\t-d usr1,usr2 delete users as group members\n"
359 "\t-l name new group name\n"
360 "\t-Y update NIS maps\n"
362 "usage: pw groupshow [group|gid] [switches]\n"
363 "\t-V etcdir alternate /etc location\n"
364 "\t-R rootir alternate root directory\n"
365 "\t-n name group name\n"
366 "\t-g gid group id\n"
368 "\t-P prettier format\n"
369 "\t-a print all accounting groups\n",
370 "usage: pw groupnext [switches]\n"
371 "\t-V etcdir alternate /etc location\n"
372 "\t-R rootir alternate root directory\n"
373 "\t-C config configuration file\n"
374 "\t-q quiet operation\n"
378 fprintf(stderr
, "%s", help
[which
][mode
]);