pw.c revision 37711
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 28static const char rcsid[] = 29 "$Id: pw.c,v 1.7 1997/10/10 06:23:34 charnier Exp $"; 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> 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 49static struct cargs arglist; 50 51static int getindex(const char *words[], const char *word); 52static void cmdhelp(int mode, int which); 53static int filelock(const char *filename); 54 55 56int 57main(int argc, char *argv[]) 58{ 59 int ch; 60 int mode = -1; 61 int which = -1; 62 struct userconf *cnf; 63 64 static const char *opts[W_NUM][M_NUM] = 65 { 66 { /* user */ 67 "C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db:NPy:Y", 68 "C:qn:u:rY", 69 "C:qn:u:c:d:e:p:g:G:ml:k:s:w:L:h:FNPY", 70 "C:qn:u:FPa", 71 "C:q" 72 }, 73 { /* grp */ 74 "C:qn:g:h:M:pNPY", 75 "C:qn:g:Y", 76 "C:qn:g:l:h:FM:m:NPY", 77 "C:qn:g:FPa", 78 "C:q" 79 } 80 }; 81 82 static int (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) = 83 { /* Request handlers */ 84 pw_user, 85 pw_group 86 }; 87 88 umask(0); /* We wish to handle this manually */ 89 LIST_INIT(&arglist); 90 91 /* 92 * Break off the first couple of words to determine what exactly 93 * we're being asked to do 94 */ 95 while (argc > 1 && *argv[1] != '-') { 96 int tmp; 97 98 if ((tmp = getindex(Modes, argv[1])) != -1) 99 mode = tmp; 100 else if ((tmp = getindex(Which, argv[1])) != -1) 101 which = tmp; 102 else if ((tmp = getindex(Combo1, argv[1])) != -1 || (tmp = getindex(Combo2, argv[1])) != -1) { 103 which = tmp / M_NUM; 104 mode = tmp % M_NUM; 105 } else if (strcmp(argv[1], "help") == 0) 106 cmdhelp(mode, which); 107 else if (which != -1 && mode != -1 && arglist.lh_first == NULL) 108 addarg(&arglist, 'n', argv[1]); 109 else 110 errx(EX_USAGE, "unknown keyword `%s'", argv[1]); 111 ++argv; 112 --argc; 113 } 114 115 /* 116 * Bail out unless the user is specific! 117 */ 118 if (mode == -1 || which == -1) 119 cmdhelp(mode, which); 120 121 /* 122 * We know which mode we're in and what we're about to do, so now 123 * let's dispatch the remaining command line args in a genric way. 124 */ 125 optarg = NULL; 126 127 while ((ch = getopt(argc, argv, opts[which][mode])) != -1) { 128 if (ch == '?') 129 errx(EX_USAGE, NULL); 130 else 131 addarg(&arglist, ch, optarg); 132 optarg = NULL; 133 } 134 135 /* 136 * Must be root to attempt an update 137 */ 138 if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && getarg(&arglist, 'N')==NULL) 139 errx(EX_NOPERM, "you must be root to run this program"); 140 141 /* 142 * We should immediately look for the -q 'quiet' switch so that we 143 * don't bother with extraneous errors 144 */ 145 if (getarg(&arglist, 'q') != NULL) 146 freopen("/dev/null", "w", stderr); 147 148 /* 149 * Now, let's do the common initialisation 150 */ 151 cnf = read_userconfig(getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL); 152 153 154 /* 155 * Be pessimistic and lock the master passowrd and group 156 * files right away. Keep it locked for the duration. 157 */ 158 if (-1 == filelock(_PATH_GROUP) || -1 == filelock(_PATH_MASTERPASSWD)) 159 { 160 ch = EX_IOERR; 161 } 162 else 163 { 164 ch = funcs[which] (cnf, mode, &arglist); 165 } 166 167 /* 168 * If everything went ok, and we've been asked to update 169 * the NIS maps, then do it now 170 */ 171 if (ch == EXIT_SUCCESS && getarg(&arglist, 'Y') != NULL) { 172 pid_t pid; 173 174 fflush(NULL); 175 if (chdir(_PATH_YP) == -1) 176 warn("chdir(" _PATH_YP ")"); 177 else if ((pid = fork()) == -1) 178 warn("fork()"); 179 else if (pid == 0) { 180 /* Is make anywhere else? */ 181 execlp("/usr/bin/make", "make", NULL); 182 _exit(1); 183 } else { 184 int i; 185 waitpid(pid, &i, 0); 186 if ((i = WEXITSTATUS(i)) != 0) 187 errx(ch, "make exited with status %d", i); 188 else 189 pw_log(cnf, mode, which, "NIS maps updated"); 190 } 191 } 192 return ch; 193} 194 195static int 196filelock(const char *filename) 197{ 198 return open(filename, O_RDONLY | O_EXLOCK, 0); 199} 200 201static int 202getindex(const char *words[], const char *word) 203{ 204 int i = 0; 205 206 while (words[i]) { 207 if (strcmp(words[i], word) == 0) 208 return i; 209 i++; 210 } 211 return -1; 212} 213 214 215/* 216 * This is probably an overkill for a cmdline help system, but it reflects 217 * the complexity of the command line. 218 */ 219 220static void 221cmdhelp(int mode, int which) 222{ 223 if (which == -1) 224 fprintf(stderr, "usage: pw [user|group] [add|del|mod|show|next] [ help | switches/values ]\n"); 225 else if (mode == -1) 226 fprintf(stderr, "usage: pw %s [add|del|mod|show|next] [ help | switches/values ]\n", Which[which]); 227 else { 228 229 /* 230 * We need to give mode specific help 231 */ 232 static const char *help[W_NUM][M_NUM] = 233 { 234 { 235 "usage: pw useradd [name] [switches]\n" 236 "\t-C config configuration file\n" 237 "\t-q quiet operation\n" 238 " Adding users:\n" 239 "\t-n name login name\n" 240 "\t-u uid user id\n" 241 "\t-c comment user name/comment\n" 242 "\t-d directory home directory\n" 243 "\t-e date account expiry date\n" 244 "\t-p date password expiry date\n" 245 "\t-g grp initial group\n" 246 "\t-G grp1,grp2 additional groups\n" 247 "\t-m [ -k dir ] create and set up home\n" 248 "\t-s shell name of login shell\n" 249 "\t-o duplicate uid ok\n" 250 "\t-L class user class\n" 251 "\t-h fd read password on fd\n" 252 "\t-Y update NIS maps\n" 253 "\t-N no update\n" 254 " Setting defaults:\n" 255 "\t-D set user defaults\n" 256 "\t-b dir default home root dir\n" 257 "\t-e period default expiry period\n" 258 "\t-p period default password change period\n" 259 "\t-g group default group\n" 260 "\t-G grp1,grp2 additional groups\n" 261 "\t-L class default user class\n" 262 "\t-k dir default home skeleton\n" 263 "\t-u min,max set min,max uids\n" 264 "\t-i min,max set min,max gids\n" 265 "\t-w method set default password method\n" 266 "\t-s shell default shell\n" 267 "\t-y path set NIS passwd file path\n", 268 "usage: pw userdel [uid|name] [switches]\n" 269 "\t-n name login name\n" 270 "\t-u uid user id\n" 271 "\t-Y update NIS maps\n" 272 "\t-r remove home & contents\n", 273 "usage: pw usermod [uid|name] [switches]\n" 274 "\t-C config configuration file\n" 275 "\t-q quiet operation\n" 276 "\t-F force add if no user\n" 277 "\t-n name login name\n" 278 "\t-u uid user id\n" 279 "\t-c comment user name/comment\n" 280 "\t-d directory home directory\n" 281 "\t-e date account expiry date\n" 282 "\t-p date password expiry date\n" 283 "\t-g grp initial group\n" 284 "\t-G grp1,grp2 additional groups\n" 285 "\t-l name new login name\n" 286 "\t-L class user class\n" 287 "\t-m [ -k dir ] create and set up home\n" 288 "\t-s shell name of login shell\n" 289 "\t-w method set new password using method\n" 290 "\t-h fd read password on fd\n" 291 "\t-Y update NIS maps\n" 292 "\t-N no update\n", 293 "usage: pw usershow [uid|name] [switches]\n" 294 "\t-n name login name\n" 295 "\t-u uid user id\n" 296 "\t-F force print\n" 297 "\t-P prettier format\n" 298 "\t-a print all users\n", 299 "usage: pw usernext [switches]\n" 300 "\t-C config configuration file\n" 301 }, 302 { 303 "usage: pw groupadd [group|gid] [switches]\n" 304 "\t-C config configuration file\n" 305 "\t-q quiet operation\n" 306 "\t-n group group name\n" 307 "\t-g gid group id\n" 308 "\t-M usr1,usr2 add users as group members\n" 309 "\t-o duplicate gid ok\n" 310 "\t-Y update NIS maps\n" 311 "\t-N no update\n", 312 "usage: pw groupdel [group|gid] [switches]\n" 313 "\t-n name group name\n" 314 "\t-g gid group id\n" 315 "\t-Y update NIS maps\n", 316 "usage: pw groupmod [group|gid] [switches]\n" 317 "\t-C config configuration file\n" 318 "\t-q quiet operation\n" 319 "\t-F force add if not exists\n" 320 "\t-n name group name\n" 321 "\t-g gid group id\n" 322 "\t-M usr1,usr2 replaces users as group members\n" 323 "\t-m usr1,usr2 add users as group members\n" 324 "\t-l name new group name\n" 325 "\t-Y update NIS maps\n" 326 "\t-N no update\n", 327 "usage: pw groupshow [group|gid] [switches]\n" 328 "\t-n name group name\n" 329 "\t-g gid group id\n" 330 "\t-F force print\n" 331 "\t-P prettier format\n" 332 "\t-a print all accounting groups\n", 333 "usage: pw groupnext [switches]\n" 334 "\t-C config configuration file\n" 335 } 336 }; 337 338 fprintf(stderr, help[which][mode]); 339 } 340 exit(EXIT_FAILURE); 341} 342 343struct carg * 344getarg(struct cargs * _args, int ch) 345{ 346 struct carg *c = _args->lh_first; 347 348 while (c != NULL && c->ch != ch) 349 c = c->list.le_next; 350 return c; 351} 352 353struct carg * 354addarg(struct cargs * _args, int ch, char *argstr) 355{ 356 struct carg *ca = malloc(sizeof(struct carg)); 357 358 if (ca == NULL) 359 errx(EX_OSERR, "out of memory"); 360 ca->ch = ch; 361 ca->val = argstr; 362 LIST_INSERT_HEAD(_args, ca, list); 363 return ca; 364} 365