pw.c (42394) | pw.c (44229) |
---|---|
1/*- 2 * Copyright (C) 1996 3 * David L. Nugent. 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 --- 12 unchanged lines hidden (view full) --- 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 24 * SUCH DAMAGE. 25 */ 26 27#ifndef lint 28static const char rcsid[] = | 1/*- 2 * Copyright (C) 1996 3 * David L. Nugent. 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 --- 12 unchanged lines hidden (view full) --- 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 24 * SUCH DAMAGE. 25 */ 26 27#ifndef lint 28static const char rcsid[] = |
29 "$Id: pw.c,v 1.10 1998/08/04 22:31:26 nate Exp $"; | 29 "$Id: pw.c,v 1.11 1999/01/08 10:52:38 davidn Exp $"; |
30#endif /* not lint */ 31 | 30#endif /* not lint */ 31 |
32#include "pw.h" | |
33#include <err.h> 34#include <fcntl.h> 35#include <paths.h> 36#include <sys/wait.h> | 32#include <err.h> 33#include <fcntl.h> 34#include <paths.h> 35#include <sys/wait.h> |
36#include "pw.h" |
|
37 38const char *Modes[] = {"add", "del", "mod", "show", "next", NULL}; 39const char *Which[] = {"user", "group", NULL}; 40static const char *Combo1[] = { 41 "useradd", "userdel", "usermod", "usershow", "usernext", 42 "groupadd", "groupdel", "groupmod", "groupshow", "groupnext", 43 NULL}; 44static const char *Combo2[] = { 45 "adduser", "deluser", "moduser", "showuser", "nextuser", 46 "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup", 47NULL}; 48 | 37 38const char *Modes[] = {"add", "del", "mod", "show", "next", NULL}; 39const char *Which[] = {"user", "group", NULL}; 40static const char *Combo1[] = { 41 "useradd", "userdel", "usermod", "usershow", "usernext", 42 "groupadd", "groupdel", "groupmod", "groupshow", "groupnext", 43 NULL}; 44static const char *Combo2[] = { 45 "adduser", "deluser", "moduser", "showuser", "nextuser", 46 "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup", 47NULL}; 48 |
49struct pwf PWF = 50{ 51 0, 52 setpwent, 53 endpwent, 54 getpwent, 55 getpwuid, 56 getpwnam, 57 pwdb, 58 setgrent, 59 endgrent, 60 getgrent, 61 getgrgid, 62 getgrnam, 63 grdb 64 65}; 66struct pwf VPWF = 67{ 68 1, 69 vsetpwent, 70 vendpwent, 71 vgetpwent, 72 vgetpwuid, 73 vgetpwnam, 74 vpwdb, 75 vsetgrent, 76 vendgrent, 77 vgetgrent, 78 vgetgrgid, 79 vgetgrnam, 80 vgrdb 81}; 82 |
|
49static struct cargs arglist; 50 51static int getindex(const char *words[], const char *word); 52static void cmdhelp(int mode, int which); 53 54 55int 56main(int argc, char *argv[]) 57{ 58 int ch; 59 int mode = -1; 60 int which = -1; | 83static struct cargs arglist; 84 85static int getindex(const char *words[], const char *word); 86static void cmdhelp(int mode, int which); 87 88 89int 90main(int argc, char *argv[]) 91{ 92 int ch; 93 int mode = -1; 94 int which = -1; |
95 char *config = NULL; |
|
61 struct userconf *cnf; 62 63 static const char *opts[W_NUM][M_NUM] = 64 { 65 { /* user */ | 96 struct userconf *cnf; 97 98 static const char *opts[W_NUM][M_NUM] = 99 { 100 { /* user */ |
66 "C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db:NPy:Y", 67 "C:qn:u:rY", 68 "C:qn:u:c:d:e:p:g:G:ml:k:s:w:L:h:FNPY", 69 "C:qn:u:FPa", 70 "C:q" | 101 "VC:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db:NPy:Y", 102 "VC:qn:u:rY", 103 "VC:qn:u:c:d:e:p:g:G:ml:k:s:w:L:h:FNPY", 104 "VC:qn:u:FPa", 105 "VC:q" |
71 }, 72 { /* grp */ | 106 }, 107 { /* grp */ |
73 "C:qn:g:h:M:pNPY", 74 "C:qn:g:Y", 75 "C:qn:g:l:h:FM:m:NPY", 76 "C:qn:g:FPa", 77 "C:q" | 108 "VC:qn:g:h:M:pNPY", 109 "VC:qn:g:Y", 110 "VC:qn:g:l:h:FM:m:NPY", 111 "VC:qn:g:FPa", 112 "VC:q" |
78 } 79 }; 80 81 static int (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) = 82 { /* Request handlers */ 83 pw_user, 84 pw_group 85 }; 86 87 umask(0); /* We wish to handle this manually */ 88 LIST_INIT(&arglist); 89 90 /* 91 * Break off the first couple of words to determine what exactly 92 * we're being asked to do 93 */ | 113 } 114 }; 115 116 static int (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) = 117 { /* Request handlers */ 118 pw_user, 119 pw_group 120 }; 121 122 umask(0); /* We wish to handle this manually */ 123 LIST_INIT(&arglist); 124 125 /* 126 * Break off the first couple of words to determine what exactly 127 * we're being asked to do 128 */ |
94 while (argc > 1 && *argv[1] != '-') { | 129 while (argc > 1) { |
95 int tmp; 96 | 130 int tmp; 131 |
97 if ((tmp = getindex(Modes, argv[1])) != -1) | 132 if (*argv[1] == '-') { 133 /* 134 * Special case, allow pw -V<dir> <operation> [args] for scripts etc. 135 */ 136 if (argv[1][1] == 'V') { 137 optarg = &argv[1][2]; 138 if (*optarg == '\0') { 139 optarg = argv[2]; 140 ++argv; 141 --argc; 142 } 143 addarg(&arglist, 'V', optarg); 144 } 145 break; 146 } 147 else if ((tmp = getindex(Modes, argv[1])) != -1) |
98 mode = tmp; 99 else if ((tmp = getindex(Which, argv[1])) != -1) 100 which = tmp; 101 else if ((tmp = getindex(Combo1, argv[1])) != -1 || (tmp = getindex(Combo2, argv[1])) != -1) { 102 which = tmp / M_NUM; 103 mode = tmp % M_NUM; 104 } else if (strcmp(argv[1], "help") == 0) 105 cmdhelp(mode, which); --- 34 unchanged lines hidden (view full) --- 140 /* 141 * We should immediately look for the -q 'quiet' switch so that we 142 * don't bother with extraneous errors 143 */ 144 if (getarg(&arglist, 'q') != NULL) 145 freopen("/dev/null", "w", stderr); 146 147 /* | 148 mode = tmp; 149 else if ((tmp = getindex(Which, argv[1])) != -1) 150 which = tmp; 151 else if ((tmp = getindex(Combo1, argv[1])) != -1 || (tmp = getindex(Combo2, argv[1])) != -1) { 152 which = tmp / M_NUM; 153 mode = tmp % M_NUM; 154 } else if (strcmp(argv[1], "help") == 0) 155 cmdhelp(mode, which); --- 34 unchanged lines hidden (view full) --- 190 /* 191 * We should immediately look for the -q 'quiet' switch so that we 192 * don't bother with extraneous errors 193 */ 194 if (getarg(&arglist, 'q') != NULL) 195 freopen("/dev/null", "w", stderr); 196 197 /* |
198 * Set our base working path if not overridden 199 */ 200 201 config = getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL; 202 203 if (getarg(&arglist, 'V') != NULL) { 204 char * etcpath = getarg(&arglist, 'V')->val; 205 if (*etcpath) { 206 if (config == NULL) { /* Only override config location if -C not specified */ 207 config = malloc(MAXPATHLEN); 208 snprintf(config, MAXPATHLEN, "%s/pw.conf", etcpath); 209 } 210 memcpy(&PWF, &VPWF, sizeof PWF); 211 setpwdir(etcpath); 212 setgrdir(etcpath); 213 } 214 } 215 216 /* |
|
148 * Now, let's do the common initialisation 149 */ | 217 * Now, let's do the common initialisation 218 */ |
150 cnf = read_userconfig(getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL); | 219 cnf = read_userconfig(config); |
151 152 ch = funcs[which] (cnf, mode, &arglist); 153 154 /* 155 * If everything went ok, and we've been asked to update 156 * the NIS maps, then do it now 157 */ 158 if (ch == EXIT_SUCCESS && getarg(&arglist, 'Y') != NULL) { --- 51 unchanged lines hidden (view full) --- 210 211 /* 212 * We need to give mode specific help 213 */ 214 static const char *help[W_NUM][M_NUM] = 215 { 216 { 217 "usage: pw useradd [name] [switches]\n" | 220 221 ch = funcs[which] (cnf, mode, &arglist); 222 223 /* 224 * If everything went ok, and we've been asked to update 225 * the NIS maps, then do it now 226 */ 227 if (ch == EXIT_SUCCESS && getarg(&arglist, 'Y') != NULL) { --- 51 unchanged lines hidden (view full) --- 279 280 /* 281 * We need to give mode specific help 282 */ 283 static const char *help[W_NUM][M_NUM] = 284 { 285 { 286 "usage: pw useradd [name] [switches]\n" |
287 "\t-V etcdir alternate /etc location\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-Y update NIS maps\n" 235 "\t-N no update\n" 236 " Setting defaults:\n" | 288 "\t-C config configuration file\n" 289 "\t-q quiet operation\n" 290 " Adding users:\n" 291 "\t-n name login name\n" 292 "\t-u uid user id\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-m [ -k dir ] create and set up home\n" 300 "\t-s shell name of login shell\n" 301 "\t-o duplicate uid ok\n" 302 "\t-L class user class\n" 303 "\t-h fd read password on fd\n" 304 "\t-Y update NIS maps\n" 305 "\t-N no update\n" 306 " Setting defaults:\n" |
237 "\t-D set user defaults\n" | 307 "\t-V etcdir alternate /etc location\n" 308 "\t-D set user defaults\n" |
238 "\t-b dir default home root dir\n" 239 "\t-e period default expiry period\n" 240 "\t-p period default password change period\n" 241 "\t-g group default group\n" 242 "\t-G grp1,grp2 additional groups\n" 243 "\t-L class default user class\n" 244 "\t-k dir default home skeleton\n" 245 "\t-u min,max set min,max uids\n" 246 "\t-i min,max set min,max gids\n" 247 "\t-w method set default password method\n" 248 "\t-s shell default shell\n" 249 "\t-y path set NIS passwd file path\n", 250 "usage: pw userdel [uid|name] [switches]\n" | 309 "\t-b dir default home root dir\n" 310 "\t-e period default expiry period\n" 311 "\t-p period default password change period\n" 312 "\t-g group default group\n" 313 "\t-G grp1,grp2 additional groups\n" 314 "\t-L class default user class\n" 315 "\t-k dir default home skeleton\n" 316 "\t-u min,max set min,max uids\n" 317 "\t-i min,max set min,max gids\n" 318 "\t-w method set default password method\n" 319 "\t-s shell default shell\n" 320 "\t-y path set NIS passwd file path\n", 321 "usage: pw userdel [uid|name] [switches]\n" |
322 "\t-V etcdir alternate /etc location\n" |
|
251 "\t-n name login name\n" 252 "\t-u uid user id\n" 253 "\t-Y update NIS maps\n" 254 "\t-r remove home & contents\n", 255 "usage: pw usermod [uid|name] [switches]\n" | 323 "\t-n name login name\n" 324 "\t-u uid user id\n" 325 "\t-Y update NIS maps\n" 326 "\t-r remove home & contents\n", 327 "usage: pw usermod [uid|name] [switches]\n" |
328 "\t-V etcdir alternate /etc location\n" |
|
256 "\t-C config configuration file\n" 257 "\t-q quiet operation\n" 258 "\t-F force add if no user\n" 259 "\t-n name login name\n" 260 "\t-u uid user id\n" 261 "\t-c comment user name/comment\n" 262 "\t-d directory home directory\n" 263 "\t-e date account expiry date\n" --- 4 unchanged lines hidden (view full) --- 268 "\t-L class user class\n" 269 "\t-m [ -k dir ] create and set up home\n" 270 "\t-s shell name of login shell\n" 271 "\t-w method set new password using method\n" 272 "\t-h fd read password on fd\n" 273 "\t-Y update NIS maps\n" 274 "\t-N no update\n", 275 "usage: pw usershow [uid|name] [switches]\n" | 329 "\t-C config configuration file\n" 330 "\t-q quiet operation\n" 331 "\t-F force add if no user\n" 332 "\t-n name login name\n" 333 "\t-u uid user id\n" 334 "\t-c comment user name/comment\n" 335 "\t-d directory home directory\n" 336 "\t-e date account expiry date\n" --- 4 unchanged lines hidden (view full) --- 341 "\t-L class user class\n" 342 "\t-m [ -k dir ] create and set up home\n" 343 "\t-s shell name of login shell\n" 344 "\t-w method set new password using method\n" 345 "\t-h fd read password on fd\n" 346 "\t-Y update NIS maps\n" 347 "\t-N no update\n", 348 "usage: pw usershow [uid|name] [switches]\n" |
349 "\t-V etcdir alternate /etc location\n" |
|
276 "\t-n name login name\n" 277 "\t-u uid user id\n" 278 "\t-F force print\n" 279 "\t-P prettier format\n" 280 "\t-a print all users\n", 281 "usage: pw usernext [switches]\n" | 350 "\t-n name login name\n" 351 "\t-u uid user id\n" 352 "\t-F force print\n" 353 "\t-P prettier format\n" 354 "\t-a print all users\n", 355 "usage: pw usernext [switches]\n" |
356 "\t-V etcdir alternate /etc location\n" |
|
282 "\t-C config configuration file\n" 283 }, 284 { 285 "usage: pw groupadd [group|gid] [switches]\n" | 357 "\t-C config configuration file\n" 358 }, 359 { 360 "usage: pw groupadd [group|gid] [switches]\n" |
361 "\t-V etcdir alternate /etc location\n" |
|
286 "\t-C config configuration file\n" 287 "\t-q quiet operation\n" 288 "\t-n group group name\n" 289 "\t-g gid group id\n" 290 "\t-M usr1,usr2 add users as group members\n" 291 "\t-o duplicate gid ok\n" 292 "\t-Y update NIS maps\n" 293 "\t-N no update\n", 294 "usage: pw groupdel [group|gid] [switches]\n" | 362 "\t-C config configuration file\n" 363 "\t-q quiet operation\n" 364 "\t-n group group name\n" 365 "\t-g gid group id\n" 366 "\t-M usr1,usr2 add users as group members\n" 367 "\t-o duplicate gid ok\n" 368 "\t-Y update NIS maps\n" 369 "\t-N no update\n", 370 "usage: pw groupdel [group|gid] [switches]\n" |
371 "\t-V etcdir alternate /etc location\n" |
|
295 "\t-n name group name\n" 296 "\t-g gid group id\n" 297 "\t-Y update NIS maps\n", 298 "usage: pw groupmod [group|gid] [switches]\n" | 372 "\t-n name group name\n" 373 "\t-g gid group id\n" 374 "\t-Y update NIS maps\n", 375 "usage: pw groupmod [group|gid] [switches]\n" |
376 "\t-V etcdir alternate /etc location\n" |
|
299 "\t-C config configuration file\n" 300 "\t-q quiet operation\n" 301 "\t-F force add if not exists\n" 302 "\t-n name group name\n" 303 "\t-g gid group id\n" 304 "\t-M usr1,usr2 replaces users as group members\n" 305 "\t-m usr1,usr2 add users as group members\n" 306 "\t-l name new group name\n" 307 "\t-Y update NIS maps\n" 308 "\t-N no update\n", 309 "usage: pw groupshow [group|gid] [switches]\n" | 377 "\t-C config configuration file\n" 378 "\t-q quiet operation\n" 379 "\t-F force add if not exists\n" 380 "\t-n name group name\n" 381 "\t-g gid group id\n" 382 "\t-M usr1,usr2 replaces users as group members\n" 383 "\t-m usr1,usr2 add users as group members\n" 384 "\t-l name new group name\n" 385 "\t-Y update NIS maps\n" 386 "\t-N no update\n", 387 "usage: pw groupshow [group|gid] [switches]\n" |
388 "\t-V etcdir alternate /etc location\n" |
|
310 "\t-n name group name\n" 311 "\t-g gid group id\n" 312 "\t-F force print\n" 313 "\t-P prettier format\n" 314 "\t-a print all accounting groups\n", 315 "usage: pw groupnext [switches]\n" | 389 "\t-n name group name\n" 390 "\t-g gid group id\n" 391 "\t-F force print\n" 392 "\t-P prettier format\n" 393 "\t-a print all accounting groups\n", 394 "usage: pw groupnext [switches]\n" |
395 "\t-V etcdir alternate /etc location\n" |
|
316 "\t-C config configuration file\n" 317 } 318 }; 319 320 fprintf(stderr, help[which][mode]); 321 } 322 exit(EXIT_FAILURE); 323} --- 23 unchanged lines hidden --- | 396 "\t-C config configuration file\n" 397 } 398 }; 399 400 fprintf(stderr, help[which][mode]); 401 } 402 exit(EXIT_FAILURE); 403} --- 23 unchanged lines hidden --- |