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 <paths.h> 35 #include <sys/wait.h> 36 #include "pw.h" 37 38 const char *Modes[] = { 39 "add", "del", "mod", "show", "next", 40 NULL}; 41 const char *Which[] = {"user", "group", NULL}; 42 static const char *Combo1[] = { 43 "useradd", "userdel", "usermod", "usershow", "usernext", 44 "lock", "unlock", 45 "groupadd", "groupdel", "groupmod", "groupshow", "groupnext", 46 NULL}; 47 static const char *Combo2[] = { 48 "adduser", "deluser", "moduser", "showuser", "nextuser", 49 "lock", "unlock", 50 "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup", 51 NULL}; 52 53 struct pwf PWF = 54 { 55 0, 56 setpwent, 57 endpwent, 58 getpwent, 59 getpwuid, 60 getpwnam, 61 pwdb, 62 setgrent, 63 endgrent, 64 getgrent, 65 getgrgid, 66 getgrnam, 67 grdb 68 69 }; 70 struct pwf VPWF = 71 { 72 1, 73 vsetpwent, 74 vendpwent, 75 vgetpwent, 76 vgetpwuid, 77 vgetpwnam, 78 vpwdb, 79 vsetgrent, 80 vendgrent, 81 vgetgrent, 82 vgetgrgid, 83 vgetgrnam, 84 vgrdb 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 102 static const char *opts[W_NUM][M_NUM] = 103 { 104 { /* user */ 105 "V:C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db:NPy:Y", 106 "V:C:qn:u:rY", 107 "V:C:qn:u:c:d:e:p:g:G:ml:k:s:w:L:h:FNPY", 108 "V:C:qn:u:FPa7", 109 "V:C:q", 110 "V:C:q", 111 "V:C:q" 112 }, 113 { /* grp */ 114 "V:C:qn:g:h:M:pNPY", 115 "V:C:qn:g:Y", 116 "V:C:qn:g:l:h:FM:m:NPY", 117 "V:C:qn:g:FPa", 118 "V:C:q" 119 } 120 }; 121 122 static int (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) = 123 { /* Request handlers */ 124 pw_user, 125 pw_group 126 }; 127 128 umask(0); /* We wish to handle this manually */ 129 LIST_INIT(&arglist); 130 131 /* 132 * Break off the first couple of words to determine what exactly 133 * we're being asked to do 134 */ 135 while (argc > 1) { 136 int tmp; 137 138 if (*argv[1] == '-') { 139 /* 140 * Special case, allow pw -V<dir> <operation> [args] for scripts etc. 141 */ 142 if (argv[1][1] == 'V') { 143 optarg = &argv[1][2]; 144 if (*optarg == '\0') { 145 optarg = argv[2]; 146 ++argv; 147 --argc; 148 } 149 addarg(&arglist, 'V', optarg); 150 } else 151 break; 152 } 153 else if ((tmp = getindex(Modes, argv[1])) != -1) 154 mode = tmp; 155 else if ((tmp = getindex(Which, argv[1])) != -1) 156 which = tmp; 157 else if ((tmp = getindex(Combo1, argv[1])) != -1 || (tmp = getindex(Combo2, argv[1])) != -1) { 158 which = tmp / M_NUM; 159 mode = tmp % M_NUM; 160 } else if (strcmp(argv[1], "help") == 0) 161 cmdhelp(mode, which); 162 else if (which != -1 && mode != -1) 163 addarg(&arglist, 'n', argv[1]); 164 else 165 errx(EX_USAGE, "unknown keyword `%s'", argv[1]); 166 ++argv; 167 --argc; 168 } 169 170 /* 171 * Bail out unless the user is specific! 172 */ 173 if (mode == -1 || which == -1) 174 cmdhelp(mode, which); 175 176 /* 177 * We know which mode we're in and what we're about to do, so now 178 * let's dispatch the remaining command line args in a genric way. 179 */ 180 optarg = NULL; 181 182 while ((ch = getopt(argc, argv, opts[which][mode])) != -1) { 183 if (ch == '?') 184 errx(EX_USAGE, NULL); 185 else 186 addarg(&arglist, ch, optarg); 187 optarg = NULL; 188 } 189 190 /* 191 * Must be root to attempt an update 192 */ 193 if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && getarg(&arglist, 'N')==NULL) 194 errx(EX_NOPERM, "you must be root to run this program"); 195 196 /* 197 * We should immediately look for the -q 'quiet' switch so that we 198 * don't bother with extraneous errors 199 */ 200 if (getarg(&arglist, 'q') != NULL) 201 freopen("/dev/null", "w", stderr); 202 203 /* 204 * Set our base working path if not overridden 205 */ 206 207 config = getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL; 208 209 if (getarg(&arglist, 'V') != NULL) { 210 char * etcpath = getarg(&arglist, 'V')->val; 211 if (*etcpath) { 212 if (config == NULL) { /* Only override config location if -C not specified */ 213 config = malloc(MAXPATHLEN); 214 snprintf(config, MAXPATHLEN, "%s/pw.conf", etcpath); 215 } 216 memcpy(&PWF, &VPWF, sizeof PWF); 217 setpwdir(etcpath); 218 setgrdir(etcpath); 219 } 220 } 221 222 /* 223 * Now, let's do the common initialisation 224 */ 225 cnf = read_userconfig(config); 226 227 ch = funcs[which] (cnf, mode, &arglist); 228 229 /* 230 * If everything went ok, and we've been asked to update 231 * the NIS maps, then do it now 232 */ 233 if (ch == EXIT_SUCCESS && getarg(&arglist, 'Y') != NULL) { 234 pid_t pid; 235 236 fflush(NULL); 237 if (chdir(_PATH_YP) == -1) 238 warn("chdir(" _PATH_YP ")"); 239 else if ((pid = fork()) == -1) 240 warn("fork()"); 241 else if (pid == 0) { 242 /* Is make anywhere else? */ 243 execlp("/usr/bin/make", "make", NULL); 244 _exit(1); 245 } else { 246 int i; 247 waitpid(pid, &i, 0); 248 if ((i = WEXITSTATUS(i)) != 0) 249 errx(ch, "make exited with status %d", i); 250 else 251 pw_log(cnf, mode, which, "NIS maps updated"); 252 } 253 } 254 return ch; 255 } 256 257 258 static int 259 getindex(const char *words[], const char *word) 260 { 261 int i = 0; 262 263 while (words[i]) { 264 if (strcmp(words[i], word) == 0) 265 return i; 266 i++; 267 } 268 return -1; 269 } 270 271 272 /* 273 * This is probably an overkill for a cmdline help system, but it reflects 274 * the complexity of the command line. 275 */ 276 277 static void 278 cmdhelp(int mode, int which) 279 { 280 if (which == -1) 281 fprintf(stderr, "usage:\n pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n"); 282 else if (mode == -1) 283 fprintf(stderr, "usage:\n pw %s [add|del|mod|show|next] [help|switches/values]\n", Which[which]); 284 else { 285 286 /* 287 * We need to give mode specific help 288 */ 289 static const char *help[W_NUM][M_NUM] = 290 { 291 { 292 "usage: pw useradd [name] [switches]\n" 293 "\t-V etcdir alternate /etc location\n" 294 "\t-C config configuration file\n" 295 "\t-q quiet operation\n" 296 " Adding users:\n" 297 "\t-n name login name\n" 298 "\t-u uid user id\n" 299 "\t-c comment user name/comment\n" 300 "\t-d directory home directory\n" 301 "\t-e date account expiry date\n" 302 "\t-p date password expiry date\n" 303 "\t-g grp initial group\n" 304 "\t-G grp1,grp2 additional groups\n" 305 "\t-m [ -k dir ] create and set up home\n" 306 "\t-s shell name of login shell\n" 307 "\t-o duplicate uid ok\n" 308 "\t-L class user class\n" 309 "\t-h fd read password on fd\n" 310 "\t-Y update NIS maps\n" 311 "\t-N no update\n" 312 " Setting defaults:\n" 313 "\t-V etcdir alternate /etc location\n" 314 "\t-D set user defaults\n" 315 "\t-b dir default home root dir\n" 316 "\t-e period default expiry period\n" 317 "\t-p period default password change period\n" 318 "\t-g group default group\n" 319 "\t-G grp1,grp2 additional groups\n" 320 "\t-L class default user class\n" 321 "\t-k dir default home skeleton\n" 322 "\t-u min,max set min,max uids\n" 323 "\t-i min,max set min,max gids\n" 324 "\t-w method set default password method\n" 325 "\t-s shell default shell\n" 326 "\t-y path set NIS passwd file path\n", 327 "usage: pw userdel [uid|name] [switches]\n" 328 "\t-V etcdir alternate /etc location\n" 329 "\t-n name login name\n" 330 "\t-u uid user id\n" 331 "\t-Y update NIS maps\n" 332 "\t-r remove home & contents\n", 333 "usage: pw usermod [uid|name] [switches]\n" 334 "\t-V etcdir alternate /etc location\n" 335 "\t-C config configuration file\n" 336 "\t-q quiet operation\n" 337 "\t-F force add if no user\n" 338 "\t-n name login name\n" 339 "\t-u uid user id\n" 340 "\t-c comment user name/comment\n" 341 "\t-d directory home directory\n" 342 "\t-e date account expiry date\n" 343 "\t-p date password expiry date\n" 344 "\t-g grp initial group\n" 345 "\t-G grp1,grp2 additional groups\n" 346 "\t-l name new login name\n" 347 "\t-L class user class\n" 348 "\t-m [ -k dir ] create and set up home\n" 349 "\t-s shell name of login shell\n" 350 "\t-w method set new password using method\n" 351 "\t-h fd read password on fd\n" 352 "\t-Y update NIS maps\n" 353 "\t-N no update\n", 354 "usage: pw usershow [uid|name] [switches]\n" 355 "\t-V etcdir alternate /etc location\n" 356 "\t-n name login name\n" 357 "\t-u uid user id\n" 358 "\t-F force print\n" 359 "\t-P prettier format\n" 360 "\t-a print all users\n" 361 "\t-7 print in v7 format\n", 362 "usage: pw usernext [switches]\n" 363 "\t-V etcdir alternate /etc location\n" 364 "\t-C config configuration file\n" 365 }, 366 { 367 "usage: pw groupadd [group|gid] [switches]\n" 368 "\t-V etcdir alternate /etc location\n" 369 "\t-C config configuration file\n" 370 "\t-q quiet operation\n" 371 "\t-n group group name\n" 372 "\t-g gid group id\n" 373 "\t-M usr1,usr2 add users as group members\n" 374 "\t-o duplicate gid ok\n" 375 "\t-Y update NIS maps\n" 376 "\t-N no update\n", 377 "usage: pw groupdel [group|gid] [switches]\n" 378 "\t-V etcdir alternate /etc location\n" 379 "\t-n name group name\n" 380 "\t-g gid group id\n" 381 "\t-Y update NIS maps\n", 382 "usage: pw groupmod [group|gid] [switches]\n" 383 "\t-V etcdir alternate /etc location\n" 384 "\t-C config configuration file\n" 385 "\t-q quiet operation\n" 386 "\t-F force add if not exists\n" 387 "\t-n name group name\n" 388 "\t-g gid group id\n" 389 "\t-M usr1,usr2 replaces users as group members\n" 390 "\t-m usr1,usr2 add users as group members\n" 391 "\t-l name new group name\n" 392 "\t-Y update NIS maps\n" 393 "\t-N no update\n", 394 "usage: pw groupshow [group|gid] [switches]\n" 395 "\t-V etcdir alternate /etc location\n" 396 "\t-n name group name\n" 397 "\t-g gid group id\n" 398 "\t-F force print\n" 399 "\t-P prettier format\n" 400 "\t-a print all accounting groups\n", 401 "usage: pw groupnext [switches]\n" 402 "\t-V etcdir alternate /etc location\n" 403 "\t-C config configuration file\n" 404 } 405 }; 406 407 fprintf(stderr, help[which][mode]); 408 } 409 exit(EXIT_FAILURE); 410 } 411 412 struct carg * 413 getarg(struct cargs * _args, int ch) 414 { 415 struct carg *c = _args->lh_first; 416 417 while (c != NULL && c->ch != ch) 418 c = c->list.le_next; 419 return c; 420 } 421 422 struct carg * 423 addarg(struct cargs * _args, int ch, char *argstr) 424 { 425 struct carg *ca = malloc(sizeof(struct carg)); 426 427 if (ca == NULL) 428 errx(EX_OSERR, "out of memory"); 429 ca->ch = ch; 430 ca->val = argstr; 431 LIST_INSERT_HEAD(_args, ca, list); 432 return ca; 433 } 434