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 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27 #ifndef lint 28 static const char rcsid[] = 29 "$FreeBSD$"; 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" 38 39 #if !defined(_PATH_YP) 40 #define _PATH_YP "/var/yp/" 41 #endif 42 const char *Modes[] = { 43 "add", "del", "mod", "show", "next", 44 NULL}; 45 const char *Which[] = {"user", "group", NULL}; 46 static const char *Combo1[] = { 47 "useradd", "userdel", "usermod", "usershow", "usernext", 48 "lock", "unlock", 49 "groupadd", "groupdel", "groupmod", "groupshow", "groupnext", 50 NULL}; 51 static const char *Combo2[] = { 52 "adduser", "deluser", "moduser", "showuser", "nextuser", 53 "lock", "unlock", 54 "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup", 55 NULL}; 56 57 struct pwf PWF = 58 { 59 0, 60 setpwent, 61 endpwent, 62 getpwent, 63 getpwuid, 64 getpwnam, 65 setgrent, 66 endgrent, 67 getgrent, 68 getgrgid, 69 getgrnam, 70 71 }; 72 struct pwf VPWF = 73 { 74 1, 75 vsetpwent, 76 vendpwent, 77 vgetpwent, 78 vgetpwuid, 79 vgetpwnam, 80 vsetgrent, 81 vendgrent, 82 vgetgrent, 83 vgetgrgid, 84 vgetgrnam, 85 }; 86 87 static struct cargs arglist; 88 89 static int getindex(const char *words[], const char *word); 90 static void cmdhelp(int mode, int which); 91 92 93 int 94 main(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 103 static const char *opts[W_NUM][M_NUM] = 104 { 105 { /* 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" 113 }, 114 { /* 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" 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 }; 128 129 LIST_INIT(&arglist); 130 131 (void)setlocale(LC_ALL, ""); 132 133 /* 134 * Break off the first couple of words to determine what exactly 135 * we're being asked to do 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 */ 144 if (argv[1][1] == 'V') { 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 } 158 addarg(&arglist, 'V', 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) && 167 ((tmp = getindex(Combo1, argv[1])) != -1 || 168 (tmp = getindex(Combo2, argv[1])) != -1)) { 169 which = tmp / M_NUM; 170 mode = tmp % M_NUM; 171 } else if (strcmp(argv[1], "help") == 0 && argv[2] == NULL) 172 cmdhelp(mode, which); 173 else if (which != -1 && mode != -1) 174 addarg(&arglist, 'n', argv[1]); 175 else 176 errx(EX_USAGE, "unknown keyword `%s'", argv[1]); 177 ++argv; 178 --argc; 179 } 180 181 /* 182 * Bail out unless the user is specific! 183 */ 184 if (mode == -1 || which == -1) 185 cmdhelp(mode, which); 186 187 /* 188 * We know which mode we're in and what we're about to do, so now 189 * let's dispatch the remaining command line args in a genric way. 190 */ 191 optarg = NULL; 192 193 while ((ch = getopt(argc, argv, opts[which][mode])) != -1) { 194 if (ch == '?') 195 errx(EX_USAGE, "unknown switch"); 196 else 197 addarg(&arglist, ch, optarg); 198 optarg = NULL; 199 } 200 201 /* 202 * Must be root to attempt an update 203 */ 204 if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && getarg(&arglist, 'N')==NULL) 205 errx(EX_NOPERM, "you must be root to run this program"); 206 207 /* 208 * We should immediately look for the -q 'quiet' switch so that we 209 * don't bother with extraneous errors 210 */ 211 if (getarg(&arglist, 'q') != NULL) 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 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); 231 } 232 } 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 241 /* 242 * If everything went ok, and we've been asked to update 243 * the NIS maps, then do it now 244 */ 245 if (ch == EXIT_SUCCESS && getarg(&arglist, 'Y') != NULL) { 246 pid_t pid; 247 248 fflush(NULL); 249 if (chdir(_PATH_YP) == -1) 250 warn("chdir(" _PATH_YP ")"); 251 else if ((pid = fork()) == -1) 252 warn("fork()"); 253 else if (pid == 0) { 254 /* Is make anywhere else? */ 255 execlp("/usr/bin/make", "make", (char *)NULL); 256 _exit(1); 257 } else { 258 int i; 259 waitpid(pid, &i, 0); 260 if ((i = WEXITSTATUS(i)) != 0) 261 errx(ch, "make exited with status %d", i); 262 else 263 pw_log(cnf, mode, which, "NIS maps updated"); 264 } 265 } 266 return ch; 267 } 268 269 270 static int 271 getindex(const char *words[], const char *word) 272 { 273 int i = 0; 274 275 while (words[i]) { 276 if (strcmp(words[i], word) == 0) 277 return i; 278 i++; 279 } 280 return -1; 281 } 282 283 284 /* 285 * This is probably an overkill for a cmdline help system, but it reflects 286 * the complexity of the command line. 287 */ 288 289 static void 290 cmdhelp(int mode, int which) 291 { 292 if (which == -1) 293 fprintf(stderr, "usage:\n pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n"); 294 else if (mode == -1) 295 fprintf(stderr, "usage:\n pw %s [add|del|mod|show|next] [help|switches/values]\n", Which[which]); 296 else { 297 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" 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" 314 "\t-p date password expiry date\n" 315 "\t-g grp initial group\n" 316 "\t-G grp1,grp2 additional groups\n" 317 "\t-m [ -k dir ] create and set up home\n" 318 "\t-M mode home directory permissions\n" 319 "\t-s shell name of login shell\n" 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" 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-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" 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" 358 "\t-p date password expiry date\n" 359 "\t-g grp initial group\n" 360 "\t-G grp1,grp2 additional groups\n" 361 "\t-l name new login name\n" 362 "\t-L class user class\n" 363 "\t-m [ -k dir ] create and set up home\n" 364 "\t-M mode home directory permissions\n" 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" 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" 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" 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" 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" 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" 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" 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); 438 } 439 440 struct carg * 441 getarg(struct cargs * _args, int ch) 442 { 443 struct carg *c = LIST_FIRST(_args); 444 445 while (c != NULL && c->ch != ch) 446 c = LIST_NEXT(c, list); 447 return c; 448 } 449 450 struct carg * 451 addarg(struct cargs * _args, int ch, char *argstr) 452 { 453 struct carg *ca = malloc(sizeof(struct carg)); 454 455 if (ca == NULL) 456 errx(EX_OSERR, "out of memory"); 457 ca->ch = ch; 458 ca->val = argstr; 459 LIST_INSERT_HEAD(_args, ca, list); 460 return ca; 461 } 462