1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (C) 1996 5 * David L. Nugent. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 */ 28 29 #ifndef lint 30 static const char rcsid[] = 31 "$FreeBSD$"; 32 #endif /* not lint */ 33 34 #include <err.h> 35 #include <fcntl.h> 36 #include <locale.h> 37 #include <string.h> 38 #include <sysexits.h> 39 #include <unistd.h> 40 41 #include "pw.h" 42 43 const char *Modes[] = { 44 "add", "del", "mod", "show", "next", 45 NULL}; 46 const char *Which[] = {"user", "group", NULL}; 47 static const char *Combo1[] = { 48 "useradd", "userdel", "usermod", "usershow", "usernext", 49 "lock", "unlock", 50 "groupadd", "groupdel", "groupmod", "groupshow", "groupnext", 51 NULL}; 52 static const char *Combo2[] = { 53 "adduser", "deluser", "moduser", "showuser", "nextuser", 54 "lock", "unlock", 55 "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup", 56 NULL}; 57 58 struct pwf PWF = 59 { 60 PWF_REGULAR, 61 setpwent, 62 endpwent, 63 getpwent, 64 getpwuid, 65 getpwnam, 66 setgrent, 67 endgrent, 68 getgrent, 69 getgrgid, 70 getgrnam, 71 72 }; 73 struct pwf VPWF = 74 { 75 PWF_ALT, 76 vsetpwent, 77 vendpwent, 78 vgetpwent, 79 vgetpwuid, 80 vgetpwnam, 81 vsetgrent, 82 vendgrent, 83 vgetgrent, 84 vgetgrgid, 85 vgetgrnam, 86 }; 87 88 static int (*cmdfunc[W_NUM][M_NUM])(int argc, char **argv, char *_name) = { 89 { /* user */ 90 pw_user_add, 91 pw_user_del, 92 pw_user_mod, 93 pw_user_show, 94 pw_user_next, 95 pw_user_lock, 96 pw_user_unlock, 97 }, 98 { /* group */ 99 pw_group_add, 100 pw_group_del, 101 pw_group_mod, 102 pw_group_show, 103 pw_group_next, 104 } 105 }; 106 107 struct pwconf conf; 108 109 static int getindex(const char *words[], const char *word); 110 static void cmdhelp(int mode, int which); 111 112 int 113 main(int argc, char *argv[]) 114 { 115 int mode = -1, which = -1, tmp; 116 struct stat st; 117 char arg, *arg1; 118 bool relocated, nis; 119 120 arg1 = NULL; 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)); 125 conf.fd = -1; 126 conf.checkduplicate = true; 127 128 setlocale(LC_ALL, ""); 129 130 /* 131 * Break off the first couple of words to determine what exactly 132 * we're being asked to do 133 */ 134 while (argc > 1) { 135 if (*argv[1] == '-') { 136 /* 137 * Special case, allow pw -V<dir> <operation> [args] for scripts etc. 138 */ 139 arg = argv[1][1]; 140 if (arg == 'V' || arg == 'R') { 141 if (relocated) 142 errx(EXIT_FAILURE, "Both '-R' and '-V' " 143 "specified, only one accepted"); 144 relocated = true; 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 memcpy(&PWF, &VPWF, sizeof PWF); 159 if (arg == 'R') { 160 strlcpy(conf.rootdir, optarg, 161 sizeof(conf.rootdir)); 162 PWF._altdir = PWF_ROOTDIR; 163 } 164 snprintf(conf.etcpath, sizeof(conf.etcpath), 165 "%s%s", optarg, arg == 'R' ? 166 _PATH_PWD : ""); 167 } else 168 break; 169 } 170 else if (mode == -1 && (tmp = getindex(Modes, argv[1])) != -1) 171 mode = tmp; 172 else if (which == -1 && (tmp = getindex(Which, argv[1])) != -1) 173 which = tmp; 174 else if ((mode == -1 && which == -1) && 175 ((tmp = getindex(Combo1, argv[1])) != -1 || 176 (tmp = getindex(Combo2, argv[1])) != -1)) { 177 which = tmp / M_NUM; 178 mode = tmp % M_NUM; 179 } else if (strcmp(argv[1], "help") == 0 && argv[2] == NULL) 180 cmdhelp(mode, which); 181 else if (which != -1 && mode != -1) 182 arg1 = argv[1]; 183 else 184 errx(EX_USAGE, "unknown keyword `%s'", argv[1]); 185 ++argv; 186 --argc; 187 } 188 189 /* 190 * Bail out unless the user is specific! 191 */ 192 if (mode == -1 || which == -1) 193 cmdhelp(mode, which); 194 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); 198 199 return (cmdfunc[which][mode](argc, argv, arg1)); 200 } 201 202 203 static int 204 getindex(const char *words[], const char *word) 205 { 206 int i = 0; 207 208 while (words[i]) { 209 if (strcmp(words[i], word) == 0) 210 return (i); 211 i++; 212 } 213 return (-1); 214 } 215 216 217 /* 218 * This is probably an overkill for a cmdline help system, but it reflects 219 * the complexity of the command line. 220 */ 221 222 static void 223 cmdhelp(int mode, int which) 224 { 225 if (which == -1) 226 fprintf(stderr, "usage:\n pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n"); 227 else if (mode == -1) 228 fprintf(stderr, "usage:\n pw %s [add|del|mod|show|next] [help|switches/values]\n", Which[which]); 229 else { 230 231 /* 232 * We need to give mode specific help 233 */ 234 static const char *help[W_NUM][M_NUM] = 235 { 236 { 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" 242 " Adding users:\n" 243 "\t-n name login name\n" 244 "\t-u uid user id\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" 259 "\t-N no update\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" 281 "\t-u uid user id\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" 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-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" 309 "\t-N no update\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" 314 "\t-u uid user id\n" 315 "\t-F force print\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" 332 }, 333 { 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" 344 "\t-N no update\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" 364 "\t-N no update\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" 370 "\t-F force print\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" 378 } 379 }; 380 381 fprintf(stderr, "%s", help[which][mode]); 382 } 383 exit(EXIT_FAILURE); 384 } 385