pw.c revision 20253
1/*- 2 * Copyright (c) 1996 by David L. Nugent <davidn@blaze.net.au>. 3 * 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 as 10 * the first lines of this file unmodified. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by David L. Nugent. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE DAVID L. NUGENT ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $Id$ 33 */ 34 35#include "pw.h" 36 37static char *progname = "pw"; 38 39const char *Modes[] = {"add", "del", "mod", "show", NULL}; 40const char *Which[] = {"user", "group", NULL}; 41static const char *Combo1[] = {"useradd", "userdel", "usermod", "usershow", 42 "groupadd", "groupdel", "groupmod", "groupshow", 43NULL}; 44static const char *Combo2[] = {"adduser", "deluser", "moduser", "showuser", 45 "addgroup", "delgroup", "modgroup", "showgroup", 46NULL}; 47 48static struct cargs arglist; 49 50static int getindex(const char *words[], const char *word); 51static void cmdhelp(int mode, int which); 52 53 54int 55main(int argc, char *argv[]) 56{ 57 int ch; 58 int mode = -1; 59 int which = -1; 60 struct userconf *cnf; 61 62 static const char *opts[W_NUM][M_NUM] = 63 { 64 /* user */ {"C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db", "C:qn:u:r", "C:qn:u:c:d:e:p:g:G:mk:s:L:h:F", "C:qn:u:Fpa"}, 65 /* grp */ {"C:qn:g:h:p", "C:qn:g:", "C:qn:g:l:h:F", "C:qn:g:Fpa"} 66 }; 67 68 static int (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) = 69 { /* Request handlers */ 70 pw_user, 71 pw_group 72 }; 73 74 umask(0); /* We wish to handle this manually */ 75 progname = strrchr(argv[0], '/'); 76 if (progname != NULL) 77 ++progname; 78 else 79 progname = argv[0]; 80 81 LIST_INIT(&arglist); 82 83 /* 84 * Break off the first couple of words to determine what exactly 85 * we're being asked to do 86 */ 87 while (argc > 1 && *argv[1] != '-') { 88 int tmp; 89 90 if ((tmp = getindex(Modes, argv[1])) != -1) 91 mode = tmp; 92 else if ((tmp = getindex(Which, argv[1])) != -1) 93 which = tmp; 94 else if ((tmp = getindex(Combo1, argv[1])) != -1 || (tmp = getindex(Combo2, argv[1])) != -1) { 95 which = tmp / M_NUM; 96 mode = tmp % M_NUM; 97 } else if (strcmp(argv[1], "help") == 0) 98 cmdhelp(mode, which); 99 else if (which != -1 && mode != -1 && arglist.lh_first == NULL) 100 addarg(&arglist, 'n', argv[1]); 101 else 102 cmderr(X_CMDERR, "Unknown keyword `%s'\n", argv[1]); 103 ++argv; 104 --argc; 105 } 106 107 /* 108 * Bail out unless the user is specific! 109 */ 110 if (mode == -1 || which == -1) 111 cmdhelp(mode, which); 112 113 /* 114 * Must be root to attempt an update 115 */ 116 if (getuid() != 0 && mode != M_PRINT) 117 cmderr(X_PERMERR, "you must be root to run this program\n"); 118 119 /* 120 * We know which mode we're in and what we're about to do, so now 121 * let's dispatch the remaining command line args in a genric way. 122 */ 123 argv[0] = progname; /* Preserve this */ 124 optarg = NULL; 125 126 while ((ch = getopt(argc, argv, opts[which][mode])) != -1) { 127 if (ch == '?') 128 cmderr(X_CMDERR, NULL); 129 else 130 addarg(&arglist, ch, optarg); 131 optarg = NULL; 132 } 133 134 /* 135 * We should immediately look for the -q 'quiet' switch so that we 136 * don't bother with extraneous errors 137 */ 138 if (getarg(&arglist, 'q') != NULL) 139 freopen("/dev/null", "w", stderr); 140 141 ch = X_CMDERR; 142 143 /* 144 * Now, let's do the common initialisation 145 */ 146 cnf = read_userconfig(getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL); 147 148 if (funcs[which]) 149 ch = funcs[which] (cnf, mode, &arglist); 150 else 151 fprintf(stderr, "%s: %s[%s] not yet implemented.\n", progname, Which[which], Modes[mode]); 152 153 return ch; 154} 155 156static int 157getindex(const char *words[], const char *word) 158{ 159 int i = 0; 160 161 while (words[i]) { 162 if (strcmp(words[i], word) == 0) 163 return i; 164 i++; 165 } 166 return -1; 167} 168 169 170/* 171 * This is probably an overkill for a cmdline help system, but it reflects 172 * the complexity of the command line. 173 */ 174 175static void 176banner(void) 177{ 178 fprintf(stderr, "%s: ", progname); 179} 180 181void 182cmderr(int ec, char const * fmt,...) 183{ 184 if (fmt != NULL) { 185 va_list argp; 186 187 banner(); 188 va_start(argp, fmt); 189 vfprintf(stderr, fmt, argp); 190 va_end(argp); 191 } 192 exit(ec); 193} 194 195static void 196cmdhelp(int mode, int which) 197{ 198 banner(); 199 if (which == -1) 200 fprintf(stderr, "usage: %s [user|group] [add|del|mod|show] [ help | switches/values ]\n", progname); 201 else if (mode == -1) 202 fprintf(stderr, "usage: %s %s [add|del|mod] [ help | switches/values ]\n", progname, Which[which]); 203 else { 204 205 /* 206 * We need to give mode specific help 207 */ 208 static const char *help[W_NUM][M_NUM] = 209 { 210 { 211 "usage: %s useradd [name] [switches]\n" 212 "\t-C config configuration file\n" 213 "\t-q quiet operation\n" 214 " Adding users:\n" 215 "\t-n name login name\n" 216 "\t-u uid user id\n" 217 "\t-c comment user name/comment\n" 218 "\t-d directory home directory\n" 219 "\t-e date account expiry date\n" 220 "\t-p date password expiry date\n" 221 "\t-g grp initial group\n" 222 "\t-G grp1,grp2 additional groups\n" 223 "\t-m [ -k dir ] create and set up home\n" 224 "\t-s shell name of login shell\n" 225 "\t-o duplicate uid ok\n" 226 "\t-L class user class\n" 227 "\t-h fd read password on fd\n" 228 " Setting defaults:\n" 229 "\t-D set user defaults\n" 230 "\t-b dir default home root dir\n" 231 "\t-e period default expiry period\n" 232 "\t-p period default password change period\n" 233 "\t-g group default group\n" 234 "\t-G grp1,grp2 additional groups\n" 235 "\t-L class default user class\n" 236 "\t-k dir default home skeleton\n" 237 "\t-u min,max set min,max uids\n" 238 "\t-i min,max set min,max gids\n" 239 "\t-w method set default password method\n" 240 "\t-s shell default shell\n", 241 "usage: %s userdel [uid|name] [switches]\n" 242 "\t-n name login name\n" 243 "\t-u uid user id\n" 244 "\t-r remove home & contents\n", 245 "usage: %s usermod [uid|name] [switches]\n" 246 "\t-C config configuration file\n" 247 "\t-q quiet operation\n" 248 "\t-F force add if no user\n" 249 "\t-n name login name\n" 250 "\t-u uid user id\n" 251 "\t-c comment user name/comment\n" 252 "\t-d directory home directory\n" 253 "\t-e date account expiry date\n" 254 "\t-p date password expiry date\n" 255 "\t-g grp initial group\n" 256 "\t-G grp1,grp2 additional groups\n" 257 "\t-l name new login name\n" 258 "\t-L class user class\n" 259 "\t-m [ -k dir ] create and set up home\n" 260 "\t-s shell name of login shell\n" 261 "\t-h fd read password on fd\n", 262 "usage: %s usershow [uid|name] [switches]\n" 263 "\t-n name login name\n" 264 "\t-u uid user id\n" 265 "\t-F force print\n" 266 "\t-p prettier format\n" 267 "\t-a print all users\n" 268 }, 269 { 270 "usage: %s groupadd [group|gid] [switches]\n" 271 "\t-C config configuration file\n" 272 "\t-q quiet operation\n" 273 "\t-n group group name\n" 274 "\t-g gid group id\n" 275 "\t-o duplicate gid ok\n", 276 "usage: %s groupdel [group|gid] [switches]\n" 277 "\t-n name group name\n" 278 "\t-g gid group id\n", 279 "usage: %s groupmod [group|gid] [switches]\n" 280 "\t-C config configuration file\n" 281 "\t-q quiet operation\n" 282 "\t-F force add if not exists\n" 283 "\t-n name group name\n" 284 "\t-g gid group id\n" 285 "\t-l name new group name\n", 286 "usage: %s groupshow [group|gid] [switches]\n" 287 "\t-n name group name\n" 288 "\t-g gid group id\n" 289 "\t-F force print\n" 290 "\t-p prettier format\n" 291 "\t-a print all accounting groups\n" 292 } 293 }; 294 295 fprintf(stderr, help[which][mode], progname); 296 } 297 exit(X_CMDERR); 298} 299 300struct carg * 301getarg(struct cargs * _args, int ch) 302{ 303 struct carg *c = _args->lh_first; 304 305 while (c != NULL && c->ch != ch) 306 c = c->list.le_next; 307 return c; 308} 309 310struct carg * 311addarg(struct cargs * _args, int ch, char *argstr) 312{ 313 struct carg *ca = malloc(sizeof(struct carg)); 314 315 if (ca == NULL) 316 cmderr(X_MEMERR, "Abort - out of memory\n"); 317 ca->ch = ch; 318 ca->val = argstr; 319 LIST_INSERT_HEAD(_args, ca, list); 320 return ca; 321} 322