]>
git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw.c
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * David L. Nugent. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 static const char rcsid
[] =
43 const char *Modes
[] = {
44 "add", "del", "mod", "show", "next",
46 const char *Which
[] = {"user", "group", NULL
};
47 static const char *Combo1
[] = {
48 "useradd", "userdel", "usermod", "usershow", "usernext",
50 "groupadd", "groupdel", "groupmod", "groupshow", "groupnext",
52 static const char *Combo2
[] = {
53 "adduser", "deluser", "moduser", "showuser", "nextuser",
55 "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup",
88 static int (*cmdfunc
[W_NUM
][M_NUM
])(int argc
, char **argv
, char *_name
) = {
109 static int getindex(const char *words
[], const char *word
);
110 static void cmdhelp(int mode
, int which
);
113 main(int argc
, char *argv
[])
115 int mode
= -1, which
= -1, tmp
;
121 relocated
= nis
= false;
122 memset(&conf
, 0, sizeof(conf
));
123 strlcpy(conf
.rootdir
, "/", sizeof(conf
.rootdir
));
124 strlcpy(conf
.etcpath
, _PATH_PWD
, sizeof(conf
.etcpath
));
126 conf
.checkduplicate
= true;
128 setlocale(LC_ALL
, "");
131 * Break off the first couple of words to determine what exactly
132 * we're being asked to do
135 if (*argv
[1] == '-') {
137 * Special case, allow pw -V<dir> <operation> [args] for scripts etc.
140 if (arg
== 'V' || arg
== 'R') {
142 errx(EXIT_FAILURE
, "Both '-R' and '-V' "
143 "specified, only one accepted");
145 optarg
= &argv
[1][2];
146 if (*optarg
== '\0') {
147 if (stat(argv
[2], &st
) != 0)
149 "no such directory `%s'",
151 if (!S_ISDIR(st
.st_mode
))
152 errx(EX_OSFILE
, "`%s' not a "
153 "directory", argv
[2]);
158 memcpy(&PWF
, &VPWF
, sizeof PWF
);
160 strlcpy(conf
.rootdir
, optarg
,
161 sizeof(conf
.rootdir
));
162 PWF
._altdir
= PWF_ROOTDIR
;
164 snprintf(conf
.etcpath
, sizeof(conf
.etcpath
),
165 "%s%s", optarg
, arg
== 'R' ?
170 else if (mode
== -1 && (tmp
= getindex(Modes
, argv
[1])) != -1)
172 else if (which
== -1 && (tmp
= getindex(Which
, argv
[1])) != -1)
174 else if ((mode
== -1 && which
== -1) &&
175 ((tmp
= getindex(Combo1
, argv
[1])) != -1 ||
176 (tmp
= getindex(Combo2
, argv
[1])) != -1)) {
179 } else if (strcmp(argv
[1], "help") == 0 && argv
[2] == NULL
)
180 cmdhelp(mode
, which
);
181 else if (which
!= -1 && mode
!= -1)
184 errx(EX_USAGE
, "unknown keyword `%s'", argv
[1]);
190 * Bail out unless the user is specific!
192 if (mode
== -1 || which
== -1)
193 cmdhelp(mode
, which
);
195 conf
.rootfd
= open(conf
.rootdir
, O_DIRECTORY
|O_CLOEXEC
);
196 if (conf
.rootfd
== -1)
197 errx(EXIT_FAILURE
, "Unable to open '%s'", conf
.rootdir
);
199 return (cmdfunc
[which
][mode
](argc
, argv
, arg1
));
204 getindex(const char *words
[], const char *word
)
209 if (strcmp(words
[i
], word
) == 0)
218 * This is probably an overkill for a cmdline help system, but it reflects
219 * the complexity of the command line.
223 cmdhelp(int mode
, int which
)
226 fprintf(stderr
, "usage:\n pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n");
228 fprintf(stderr
, "usage:\n pw %s [add|del|mod|show|next] [help|switches/values]\n", Which
[which
]);
232 * We need to give mode specific help
234 static const char *help
[W_NUM
][M_NUM
] =
237 "usage: pw useradd [name] [switches]\n"
238 "\t-V etcdir alternate /etc location\n"
239 "\t-R rootdir alternate root directory\n"
240 "\t-C config configuration file\n"
241 "\t-q quiet operation\n"
243 "\t-n name login name\n"
245 "\t-c comment user name/comment\n"
246 "\t-d directory home directory\n"
247 "\t-e date account expiry date\n"
248 "\t-p date password expiry date\n"
249 "\t-g grp initial group\n"
250 "\t-G grp1,grp2 additional groups\n"
251 "\t-m [ -k dir ] create and set up home\n"
252 "\t-M mode home directory permissions\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-H fd read encrypted password on fd\n"
258 "\t-Y update NIS maps\n"
260 " Setting defaults:\n"
261 "\t-V etcdir alternate /etc location\n"
262 "\t-R rootdir alternate root directory\n"
263 "\t-D set user defaults\n"
264 "\t-b dir default home root dir\n"
265 "\t-e period default expiry period\n"
266 "\t-p period default password change period\n"
267 "\t-g group default group\n"
268 "\t-G grp1,grp2 additional groups\n"
269 "\t-L class default user class\n"
270 "\t-k dir default home skeleton\n"
271 "\t-M mode home directory permissions\n"
272 "\t-u min,max set min,max uids\n"
273 "\t-i min,max set min,max gids\n"
274 "\t-w method set default password method\n"
275 "\t-s shell default shell\n"
276 "\t-y path set NIS passwd file path\n",
277 "usage: pw userdel [uid|name] [switches]\n"
278 "\t-V etcdir alternate /etc location\n"
279 "\t-R rootdir alternate root directory\n"
280 "\t-n name login name\n"
282 "\t-Y update NIS maps\n"
283 "\t-y path set NIS passwd file path\n"
284 "\t-r remove home & contents\n",
285 "usage: pw usermod [uid|name] [switches]\n"
286 "\t-V etcdir alternate /etc location\n"
287 "\t-R rootdir alternate root directory\n"
288 "\t-C config configuration file\n"
289 "\t-q quiet operation\n"
290 "\t-F force add if no user\n"
291 "\t-n name login name\n"
293 "\t-c comment user name/comment\n"
294 "\t-d directory home directory\n"
295 "\t-e date account expiry date\n"
296 "\t-p date password expiry date\n"
297 "\t-g grp initial group\n"
298 "\t-G grp1,grp2 additional groups\n"
299 "\t-l name new login name\n"
300 "\t-L class user class\n"
301 "\t-m [ -k dir ] create and set up home\n"
302 "\t-M mode home directory permissions\n"
303 "\t-s shell name of login shell\n"
304 "\t-w method set new password using method\n"
305 "\t-h fd read password on fd\n"
306 "\t-H fd read encrypted password on fd\n"
307 "\t-Y update NIS maps\n"
308 "\t-y path set NIS passwd file path\n"
310 "usage: pw usershow [uid|name] [switches]\n"
311 "\t-V etcdir alternate /etc location\n"
312 "\t-R rootdir alternate root directory\n"
313 "\t-n name login name\n"
316 "\t-P prettier format\n"
317 "\t-a print all users\n"
318 "\t-7 print in v7 format\n",
319 "usage: pw usernext [switches]\n"
320 "\t-V etcdir alternate /etc location\n"
321 "\t-R rootdir alternate root directory\n"
322 "\t-C config configuration file\n"
323 "\t-q quiet operation\n",
324 "usage pw: lock [switches]\n"
325 "\t-V etcdir alternate /etc locations\n"
326 "\t-C config configuration file\n"
327 "\t-q quiet operation\n",
328 "usage pw: unlock [switches]\n"
329 "\t-V etcdir alternate /etc locations\n"
330 "\t-C config configuration file\n"
331 "\t-q quiet operation\n"
334 "usage: pw groupadd [group|gid] [switches]\n"
335 "\t-V etcdir alternate /etc location\n"
336 "\t-R rootdir alternate root directory\n"
337 "\t-C config configuration file\n"
338 "\t-q quiet operation\n"
339 "\t-n group group name\n"
340 "\t-g gid group id\n"
341 "\t-M usr1,usr2 add users as group members\n"
342 "\t-o duplicate gid ok\n"
343 "\t-Y update NIS maps\n"
345 "usage: pw groupdel [group|gid] [switches]\n"
346 "\t-V etcdir alternate /etc location\n"
347 "\t-R rootdir alternate root directory\n"
348 "\t-n name group name\n"
349 "\t-g gid group id\n"
350 "\t-Y update NIS maps\n",
351 "usage: pw groupmod [group|gid] [switches]\n"
352 "\t-V etcdir alternate /etc location\n"
353 "\t-R rootdir alternate root directory\n"
354 "\t-C config configuration file\n"
355 "\t-q quiet operation\n"
356 "\t-F force add if not exists\n"
357 "\t-n name group name\n"
358 "\t-g gid group id\n"
359 "\t-M usr1,usr2 replaces users as group members\n"
360 "\t-m usr1,usr2 add users as group members\n"
361 "\t-d usr1,usr2 delete users as group members\n"
362 "\t-l name new group name\n"
363 "\t-Y update NIS maps\n"
365 "usage: pw groupshow [group|gid] [switches]\n"
366 "\t-V etcdir alternate /etc location\n"
367 "\t-R rootdir alternate root directory\n"
368 "\t-n name group name\n"
369 "\t-g gid group id\n"
371 "\t-P prettier format\n"
372 "\t-a print all accounting groups\n",
373 "usage: pw groupnext [switches]\n"
374 "\t-V etcdir alternate /etc location\n"
375 "\t-R rootdir alternate root directory\n"
376 "\t-C config configuration file\n"
377 "\t-q quiet operation\n"
381 fprintf(stderr
, "%s", help
[which
][mode
]);