pw.c (283809) | pw.c (283961) |
---|---|
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 "$FreeBSD: head/usr.sbin/pw/pw.c 283809 2015-05-31 10:02:01Z bapt $"; | 29 "$FreeBSD: head/usr.sbin/pw/pw.c 283961 2015-06-03 19:08:25Z bapt $"; |
30#endif /* not lint */ 31 32#include <err.h> 33#include <fcntl.h> 34#include <locale.h> 35#include <paths.h> 36#include <sys/wait.h> 37#include "pw.h" --- 13 unchanged lines hidden (view full) --- 51static const char *Combo2[] = { 52 "adduser", "deluser", "moduser", "showuser", "nextuser", 53 "lock", "unlock", 54 "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup", 55 NULL}; 56 57struct pwf PWF = 58{ | 30#endif /* not lint */ 31 32#include <err.h> 33#include <fcntl.h> 34#include <locale.h> 35#include <paths.h> 36#include <sys/wait.h> 37#include "pw.h" --- 13 unchanged lines hidden (view full) --- 51static const char *Combo2[] = { 52 "adduser", "deluser", "moduser", "showuser", "nextuser", 53 "lock", "unlock", 54 "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup", 55 NULL}; 56 57struct pwf PWF = 58{ |
59 0, | 59 PWF_REGULAR, |
60 setpwent, 61 endpwent, 62 getpwent, 63 getpwuid, 64 getpwnam, 65 setgrent, 66 endgrent, 67 getgrent, 68 getgrgid, 69 getgrnam, 70 71}; 72struct pwf VPWF = 73{ | 60 setpwent, 61 endpwent, 62 getpwent, 63 getpwuid, 64 getpwnam, 65 setgrent, 66 endgrent, 67 getgrent, 68 getgrgid, 69 getgrnam, 70 71}; 72struct pwf VPWF = 73{ |
74 1, | 74 PWF_ALT, |
75 vsetpwent, 76 vendpwent, 77 vgetpwent, 78 vgetpwuid, 79 vgetpwnam, 80 vsetgrent, 81 vendgrent, 82 vgetgrent, --- 11 unchanged lines hidden (view full) --- 94main(int argc, char *argv[]) 95{ 96 int ch; 97 int mode = -1; 98 int which = -1; 99 char *config = NULL; 100 struct userconf *cnf; 101 struct stat st; | 75 vsetpwent, 76 vendpwent, 77 vgetpwent, 78 vgetpwuid, 79 vgetpwnam, 80 vsetgrent, 81 vendgrent, 82 vgetgrent, --- 11 unchanged lines hidden (view full) --- 94main(int argc, char *argv[]) 95{ 96 int ch; 97 int mode = -1; 98 int which = -1; 99 char *config = NULL; 100 struct userconf *cnf; 101 struct stat st; |
102 char arg; 103 struct carg *carg; 104 char *etcpath = NULL; |
|
102 103 static const char *opts[W_NUM][M_NUM] = 104 { 105 { /* user */ | 105 106 static const char *opts[W_NUM][M_NUM] = 107 { 108 { /* user */ |
106 "V:C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y", 107 "V:C:qn:u:rY", 108 "V:C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:FNPY", 109 "V:C:qn:u:FPa7", 110 "V:C:q", 111 "V:C:q", 112 "V:C:q" | 109 "R:V:C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y", 110 "R:V:C:qn:u:rY", 111 "R:V:C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:FNPY", 112 "R:V:C:qn:u:FPa7", 113 "R:V:C:q", 114 "R:V:C:q", 115 "R:V:C:q" |
113 }, 114 { /* grp */ | 116 }, 117 { /* grp */ |
115 "V:C:qn:g:h:H:M:opNPY", 116 "V:C:qn:g:Y", 117 "V:C:qn:d:g:l:h:H:FM:m:NPY", 118 "V:C:qn:g:FPa", 119 "V:C:q" | 118 "R:V:C:qn:g:h:H:M:opNPY", 119 "R:V:C:qn:g:Y", 120 "R:V:C:qn:d:g:l:h:H:FM:m:NPY", 121 "R:V:C:qn:g:FPa", 122 "R:V:C:q" |
120 } 121 }; 122 123 static int (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) = 124 { /* Request handlers */ 125 pw_user, 126 pw_group 127 }; --- 8 unchanged lines hidden (view full) --- 136 */ 137 while (argc > 1) { 138 int tmp; 139 140 if (*argv[1] == '-') { 141 /* 142 * Special case, allow pw -V<dir> <operation> [args] for scripts etc. 143 */ | 123 } 124 }; 125 126 static int (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) = 127 { /* Request handlers */ 128 pw_user, 129 pw_group 130 }; --- 8 unchanged lines hidden (view full) --- 139 */ 140 while (argc > 1) { 141 int tmp; 142 143 if (*argv[1] == '-') { 144 /* 145 * Special case, allow pw -V<dir> <operation> [args] for scripts etc. 146 */ |
144 if (argv[1][1] == 'V') { | 147 arg = argv[1][1]; 148 if (arg == 'V' || arg == 'R') { |
145 optarg = &argv[1][2]; 146 if (*optarg == '\0') { 147 if (stat(argv[2], &st) != 0) 148 errx(EX_OSFILE, \ 149 "no such directory `%s'", 150 argv[2]); 151 if (!S_ISDIR(st.st_mode)) 152 errx(EX_OSFILE, "`%s' not a " 153 "directory", argv[2]); 154 optarg = argv[2]; 155 ++argv; 156 --argc; 157 } | 149 optarg = &argv[1][2]; 150 if (*optarg == '\0') { 151 if (stat(argv[2], &st) != 0) 152 errx(EX_OSFILE, \ 153 "no such directory `%s'", 154 argv[2]); 155 if (!S_ISDIR(st.st_mode)) 156 errx(EX_OSFILE, "`%s' not a " 157 "directory", argv[2]); 158 optarg = argv[2]; 159 ++argv; 160 --argc; 161 } |
158 addarg(&arglist, 'V', optarg); | 162 addarg(&arglist, arg, optarg); |
159 } else 160 break; 161 } 162 else if (mode == -1 && (tmp = getindex(Modes, argv[1])) != -1) 163 mode = tmp; 164 else if (which == -1 && (tmp = getindex(Which, argv[1])) != -1) 165 which = tmp; 166 else if ((mode == -1 && which == -1) && --- 45 unchanged lines hidden (view full) --- 212 freopen(_PATH_DEVNULL, "w", stderr); 213 214 /* 215 * Set our base working path if not overridden 216 */ 217 218 config = getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL; 219 | 163 } else 164 break; 165 } 166 else if (mode == -1 && (tmp = getindex(Modes, argv[1])) != -1) 167 mode = tmp; 168 else if (which == -1 && (tmp = getindex(Which, argv[1])) != -1) 169 which = tmp; 170 else if ((mode == -1 && which == -1) && --- 45 unchanged lines hidden (view full) --- 216 freopen(_PATH_DEVNULL, "w", stderr); 217 218 /* 219 * Set our base working path if not overridden 220 */ 221 222 config = getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL; 223 |
220 if (getarg(&arglist, 'V') != NULL) { 221 char * etcpath = getarg(&arglist, 'V')->val; 222 if (*etcpath) { 223 if (config == NULL) { /* Only override config location if -C not specified */ 224 asprintf(&config, "%s/pw.conf", etcpath); 225 if (config == NULL) 226 errx(EX_OSERR, "out of memory"); 227 } 228 memcpy(&PWF, &VPWF, sizeof PWF); 229 setpwdir(etcpath); 230 setgrdir(etcpath); | 224 if ((carg = getarg(&arglist, 'R')) != NULL) { 225 asprintf(&etcpath, "%s/etc", carg->val); 226 if (etcpath == NULL) 227 errx(EX_OSERR, "out of memory"); 228 } 229 if (etcpath == NULL && (carg = getarg(&arglist, 'V')) != NULL) { 230 etcpath = strdup(carg->val); 231 if (etcpath == NULL) 232 errx(EX_OSERR, "out of memory"); 233 } 234 if (etcpath && *etcpath) { 235 if (config == NULL) { /* Only override config location if -C not specified */ 236 asprintf(&config, "%s/pw.conf", etcpath); 237 if (config == NULL) 238 errx(EX_OSERR, "out of memory"); |
231 } | 239 } |
240 setpwdir(etcpath); 241 setgrdir(etcpath); 242 memcpy(&PWF, &VPWF, sizeof PWF); 243 if (getarg(&arglist, 'R')) 244 PWF._altdir = PWF_ROOTDIR; |
|
232 } | 245 } |
246 free(etcpath); |
|
233 234 /* 235 * Now, let's do the common initialisation 236 */ 237 cnf = read_userconfig(config); 238 239 ch = funcs[which] (cnf, mode, &arglist); 240 --- 57 unchanged lines hidden (view full) --- 298 /* 299 * We need to give mode specific help 300 */ 301 static const char *help[W_NUM][M_NUM] = 302 { 303 { 304 "usage: pw useradd [name] [switches]\n" 305 "\t-V etcdir alternate /etc location\n" | 247 248 /* 249 * Now, let's do the common initialisation 250 */ 251 cnf = read_userconfig(config); 252 253 ch = funcs[which] (cnf, mode, &arglist); 254 --- 57 unchanged lines hidden (view full) --- 312 /* 313 * We need to give mode specific help 314 */ 315 static const char *help[W_NUM][M_NUM] = 316 { 317 { 318 "usage: pw useradd [name] [switches]\n" 319 "\t-V etcdir alternate /etc location\n" |
320 "\t-R rootir alternate root directory\n" |
|
306 "\t-C config configuration file\n" 307 "\t-q quiet operation\n" 308 " Adding users:\n" 309 "\t-n name login name\n" 310 "\t-u uid user id\n" 311 "\t-c comment user name/comment\n" 312 "\t-d directory home directory\n" 313 "\t-e date account expiry date\n" --- 6 unchanged lines hidden (view full) --- 320 "\t-o duplicate uid ok\n" 321 "\t-L class user class\n" 322 "\t-h fd read password on fd\n" 323 "\t-H fd read encrypted password on fd\n" 324 "\t-Y update NIS maps\n" 325 "\t-N no update\n" 326 " Setting defaults:\n" 327 "\t-V etcdir alternate /etc location\n" | 321 "\t-C config configuration file\n" 322 "\t-q quiet operation\n" 323 " Adding users:\n" 324 "\t-n name login name\n" 325 "\t-u uid user id\n" 326 "\t-c comment user name/comment\n" 327 "\t-d directory home directory\n" 328 "\t-e date account expiry date\n" --- 6 unchanged lines hidden (view full) --- 335 "\t-o duplicate uid ok\n" 336 "\t-L class user class\n" 337 "\t-h fd read password on fd\n" 338 "\t-H fd read encrypted password on fd\n" 339 "\t-Y update NIS maps\n" 340 "\t-N no update\n" 341 " Setting defaults:\n" 342 "\t-V etcdir alternate /etc location\n" |
343 "\t-R rootir alternate root directory\n" |
|
328 "\t-D set user defaults\n" 329 "\t-b dir default home root dir\n" 330 "\t-e period default expiry period\n" 331 "\t-p period default password change period\n" 332 "\t-g group default group\n" 333 "\t-G grp1,grp2 additional groups\n" 334 "\t-L class default user class\n" 335 "\t-k dir default home skeleton\n" 336 "\t-M mode home directory permissions\n" 337 "\t-u min,max set min,max uids\n" 338 "\t-i min,max set min,max gids\n" 339 "\t-w method set default password method\n" 340 "\t-s shell default shell\n" 341 "\t-y path set NIS passwd file path\n", 342 "usage: pw userdel [uid|name] [switches]\n" 343 "\t-V etcdir alternate /etc location\n" | 344 "\t-D set user defaults\n" 345 "\t-b dir default home root dir\n" 346 "\t-e period default expiry period\n" 347 "\t-p period default password change period\n" 348 "\t-g group default group\n" 349 "\t-G grp1,grp2 additional groups\n" 350 "\t-L class default user class\n" 351 "\t-k dir default home skeleton\n" 352 "\t-M mode home directory permissions\n" 353 "\t-u min,max set min,max uids\n" 354 "\t-i min,max set min,max gids\n" 355 "\t-w method set default password method\n" 356 "\t-s shell default shell\n" 357 "\t-y path set NIS passwd file path\n", 358 "usage: pw userdel [uid|name] [switches]\n" 359 "\t-V etcdir alternate /etc location\n" |
360 "\t-R rootir alternate root directory\n" |
|
344 "\t-n name login name\n" 345 "\t-u uid user id\n" 346 "\t-Y update NIS maps\n" 347 "\t-r remove home & contents\n", 348 "usage: pw usermod [uid|name] [switches]\n" 349 "\t-V etcdir alternate /etc location\n" | 361 "\t-n name login name\n" 362 "\t-u uid user id\n" 363 "\t-Y update NIS maps\n" 364 "\t-r remove home & contents\n", 365 "usage: pw usermod [uid|name] [switches]\n" 366 "\t-V etcdir alternate /etc location\n" |
367 "\t-R rootir alternate root directory\n" |
|
350 "\t-C config configuration file\n" 351 "\t-q quiet operation\n" 352 "\t-F force add if no user\n" 353 "\t-n name login name\n" 354 "\t-u uid user id\n" 355 "\t-c comment user name/comment\n" 356 "\t-d directory home directory\n" 357 "\t-e date account expiry date\n" --- 7 unchanged lines hidden (view full) --- 365 "\t-s shell name of login shell\n" 366 "\t-w method set new password using method\n" 367 "\t-h fd read password on fd\n" 368 "\t-H fd read encrypted password on fd\n" 369 "\t-Y update NIS maps\n" 370 "\t-N no update\n", 371 "usage: pw usershow [uid|name] [switches]\n" 372 "\t-V etcdir alternate /etc location\n" | 368 "\t-C config configuration file\n" 369 "\t-q quiet operation\n" 370 "\t-F force add if no user\n" 371 "\t-n name login name\n" 372 "\t-u uid user id\n" 373 "\t-c comment user name/comment\n" 374 "\t-d directory home directory\n" 375 "\t-e date account expiry date\n" --- 7 unchanged lines hidden (view full) --- 383 "\t-s shell name of login shell\n" 384 "\t-w method set new password using method\n" 385 "\t-h fd read password on fd\n" 386 "\t-H fd read encrypted password on fd\n" 387 "\t-Y update NIS maps\n" 388 "\t-N no update\n", 389 "usage: pw usershow [uid|name] [switches]\n" 390 "\t-V etcdir alternate /etc location\n" |
391 "\t-R rootir alternate root directory\n" |
|
373 "\t-n name login name\n" 374 "\t-u uid user id\n" 375 "\t-F force print\n" 376 "\t-P prettier format\n" 377 "\t-a print all users\n" 378 "\t-7 print in v7 format\n", 379 "usage: pw usernext [switches]\n" 380 "\t-V etcdir alternate /etc location\n" | 392 "\t-n name login name\n" 393 "\t-u uid user id\n" 394 "\t-F force print\n" 395 "\t-P prettier format\n" 396 "\t-a print all users\n" 397 "\t-7 print in v7 format\n", 398 "usage: pw usernext [switches]\n" 399 "\t-V etcdir alternate /etc location\n" |
400 "\t-R rootir alternate root directory\n" |
|
381 "\t-C config configuration file\n" 382 "\t-q quiet operation\n", 383 "usage pw: lock [switches]\n" 384 "\t-V etcdir alternate /etc locations\n" 385 "\t-C config configuration file\n" 386 "\t-q quiet operation\n", 387 "usage pw: unlock [switches]\n" 388 "\t-V etcdir alternate /etc locations\n" 389 "\t-C config configuration file\n" 390 "\t-q quiet operation\n" 391 }, 392 { 393 "usage: pw groupadd [group|gid] [switches]\n" 394 "\t-V etcdir alternate /etc location\n" | 401 "\t-C config configuration file\n" 402 "\t-q quiet operation\n", 403 "usage pw: lock [switches]\n" 404 "\t-V etcdir alternate /etc locations\n" 405 "\t-C config configuration file\n" 406 "\t-q quiet operation\n", 407 "usage pw: unlock [switches]\n" 408 "\t-V etcdir alternate /etc locations\n" 409 "\t-C config configuration file\n" 410 "\t-q quiet operation\n" 411 }, 412 { 413 "usage: pw groupadd [group|gid] [switches]\n" 414 "\t-V etcdir alternate /etc location\n" |
415 "\t-R rootir alternate root directory\n" |
|
395 "\t-C config configuration file\n" 396 "\t-q quiet operation\n" 397 "\t-n group group name\n" 398 "\t-g gid group id\n" 399 "\t-M usr1,usr2 add users as group members\n" 400 "\t-o duplicate gid ok\n" 401 "\t-Y update NIS maps\n" 402 "\t-N no update\n", 403 "usage: pw groupdel [group|gid] [switches]\n" 404 "\t-V etcdir alternate /etc location\n" | 416 "\t-C config configuration file\n" 417 "\t-q quiet operation\n" 418 "\t-n group group name\n" 419 "\t-g gid group id\n" 420 "\t-M usr1,usr2 add users as group members\n" 421 "\t-o duplicate gid ok\n" 422 "\t-Y update NIS maps\n" 423 "\t-N no update\n", 424 "usage: pw groupdel [group|gid] [switches]\n" 425 "\t-V etcdir alternate /etc location\n" |
426 "\t-R rootir alternate root directory\n" |
|
405 "\t-n name group name\n" 406 "\t-g gid group id\n" 407 "\t-Y update NIS maps\n", 408 "usage: pw groupmod [group|gid] [switches]\n" 409 "\t-V etcdir alternate /etc location\n" | 427 "\t-n name group name\n" 428 "\t-g gid group id\n" 429 "\t-Y update NIS maps\n", 430 "usage: pw groupmod [group|gid] [switches]\n" 431 "\t-V etcdir alternate /etc location\n" |
432 "\t-R rootir alternate root directory\n" |
|
410 "\t-C config configuration file\n" 411 "\t-q quiet operation\n" 412 "\t-F force add if not exists\n" 413 "\t-n name group name\n" 414 "\t-g gid group id\n" 415 "\t-M usr1,usr2 replaces users as group members\n" 416 "\t-m usr1,usr2 add users as group members\n" 417 "\t-d usr1,usr2 delete users as group members\n" 418 "\t-l name new group name\n" 419 "\t-Y update NIS maps\n" 420 "\t-N no update\n", 421 "usage: pw groupshow [group|gid] [switches]\n" 422 "\t-V etcdir alternate /etc location\n" | 433 "\t-C config configuration file\n" 434 "\t-q quiet operation\n" 435 "\t-F force add if not exists\n" 436 "\t-n name group name\n" 437 "\t-g gid group id\n" 438 "\t-M usr1,usr2 replaces users as group members\n" 439 "\t-m usr1,usr2 add users as group members\n" 440 "\t-d usr1,usr2 delete users as group members\n" 441 "\t-l name new group name\n" 442 "\t-Y update NIS maps\n" 443 "\t-N no update\n", 444 "usage: pw groupshow [group|gid] [switches]\n" 445 "\t-V etcdir alternate /etc location\n" |
446 "\t-R rootir alternate root directory\n" |
|
423 "\t-n name group name\n" 424 "\t-g gid group id\n" 425 "\t-F force print\n" 426 "\t-P prettier format\n" 427 "\t-a print all accounting groups\n", 428 "usage: pw groupnext [switches]\n" 429 "\t-V etcdir alternate /etc location\n" | 447 "\t-n name group name\n" 448 "\t-g gid group id\n" 449 "\t-F force print\n" 450 "\t-P prettier format\n" 451 "\t-a print all accounting groups\n", 452 "usage: pw groupnext [switches]\n" 453 "\t-V etcdir alternate /etc location\n" |
454 "\t-R rootir alternate root directory\n" |
|
430 "\t-C config configuration file\n" 431 "\t-q quiet operation\n" 432 } 433 }; 434 435 fprintf(stderr, "%s", help[which][mode]); 436 } 437 exit(EXIT_FAILURE); --- 24 unchanged lines hidden --- | 455 "\t-C config configuration file\n" 456 "\t-q quiet operation\n" 457 } 458 }; 459 460 fprintf(stderr, "%s", help[which][mode]); 461 } 462 exit(EXIT_FAILURE); --- 24 unchanged lines hidden --- |