pw.c revision 79452
1139749Simp/*- 291398Stmm * Copyright (C) 1996 391398Stmm * David L. Nugent. All rights reserved. 491398Stmm * 591398Stmm * Redistribution and use in source and binary forms, with or without 691398Stmm * modification, are permitted provided that the following conditions 791398Stmm * are met: 891398Stmm * 1. Redistributions of source code must retain the above copyright 991398Stmm * notice, this list of conditions and the following disclaimer. 1091398Stmm * 2. Redistributions in binary form must reproduce the above copyright 1191398Stmm * notice, this list of conditions and the following disclaimer in the 1291398Stmm * documentation and/or other materials provided with the distribution. 1391398Stmm * 1491398Stmm * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 1591398Stmm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1691398Stmm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1791398Stmm * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 1891398Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1991398Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2091398Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2191398Stmm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2291398Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2391398Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2491398Stmm * SUCH DAMAGE. 2591398Stmm */ 2691398Stmm 27177560Smarius#ifndef lint 2891398Stmmstatic const char rcsid[] = 2991398Stmm "$FreeBSD: head/usr.sbin/pw/pw.c 79452 2001-07-09 09:24:06Z brian $"; 3091398Stmm#endif /* not lint */ 3191398Stmm 3291398Stmm#include <err.h> 3391398Stmm#include <fcntl.h> 3491398Stmm#include <locale.h> 35194763Smarius#include <paths.h> 3691398Stmm#include <sys/wait.h> 37177560Smarius#include "pw.h" 38223944Smarius 39177560Smarius#if !defined(_PATH_YP) 40177560Smarius#define _PATH_YP "/var/yp/" 41177560Smarius#endif 4291398Stmmconst char *Modes[] = { 4391398Stmm "add", "del", "mod", "show", "next", 4491398Stmm NULL}; 45174987Smariusconst char *Which[] = {"user", "group", NULL}; 4691398Stmmstatic const char *Combo1[] = { 4791398Stmm "useradd", "userdel", "usermod", "usershow", "usernext", 4891398Stmm "lock", "unlock", 49177560Smarius "groupadd", "groupdel", "groupmod", "groupshow", "groupnext", 5091398Stmm NULL}; 51194763Smariusstatic const char *Combo2[] = { 52194763Smarius "adduser", "deluser", "moduser", "showuser", "nextuser", 5391398Stmm "lock", "unlock", 5491398Stmm "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup", 55194763Smarius NULL}; 56194763Smarius 57194763Smariusstruct pwf PWF = 58194763Smarius{ 59172334Smarius 0, 60194763Smarius setpwent, 61172334Smarius endpwent, 62172334Smarius getpwent, 63194763Smarius getpwuid, 6491398Stmm getpwnam, 6591398Stmm pwdb, 6691398Stmm setgrent, 6791398Stmm endgrent, 6891398Stmm getgrent, 69194763Smarius getgrgid, 70194763Smarius getgrnam, 7191398Stmm grdb 72172334Smarius 73174987Smarius}; 74172334Smariusstruct pwf VPWF = 75172334Smarius{ 76194763Smarius 1, 77194763Smarius vsetpwent, 78194763Smarius vendpwent, 79194763Smarius vgetpwent, 80194763Smarius vgetpwuid, 81194763Smarius vgetpwnam, 82194763Smarius vpwdb, 83194763Smarius vsetgrent, 84194763Smarius vendgrent, 85194763Smarius vgetgrent, 86194763Smarius vgetgrgid, 87194763Smarius vgetgrnam, 88194763Smarius vgrdb 89172334Smarius}; 9091398Stmm 9191398Stmmstatic struct cargs arglist; 92172334Smarius 93172334Smariusstatic int getindex(const char *words[], const char *word); 9491398Stmmstatic void cmdhelp(int mode, int which); 95194763Smarius 96223944Smarius 97194763Smariusint 98194763Smariusmain(int argc, char *argv[]) 99194763Smarius{ 100194763Smarius int ch; 101194763Smarius int mode = -1; 102194763Smarius int which = -1; 103194763Smarius char *config = NULL; 104194763Smarius struct userconf *cnf; 105194763Smarius 106194763Smarius static const char *opts[W_NUM][M_NUM] = 10791398Stmm { 108194763Smarius { /* user */ 109194763Smarius "V:C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db:NPy:Y", 110194763Smarius "V:C:qn:u:rY", 111194763Smarius "V:C:qn:u:c:d:e:p:g:G:ml:k:s:w:L:h:FNPY", 11291398Stmm "V:C:qn:u:FPa7", 113194763Smarius "V:C:q", 11491398Stmm "V:C:q", 115194763Smarius "V:C:q" 116194763Smarius }, 117194763Smarius { /* grp */ 118194763Smarius "V:C:qn:g:h:M:pNPY", 119194763Smarius "V:C:qn:g:Y", 120194763Smarius "V:C:qn:g:l:h:FM:m:NPY", 121194763Smarius "V:C:qn:g:FPa", 122194763Smarius "V:C:q" 123194763Smarius } 124194763Smarius }; 125194763Smarius 126194763Smarius static int (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) = 127172334Smarius { /* Request handlers */ 12891398Stmm pw_user, 129194763Smarius pw_group 130194763Smarius }; 131223944Smarius 13291398Stmm umask(0); /* We wish to handle this manually */ 133194763Smarius LIST_INIT(&arglist); 134194763Smarius 135194763Smarius (void)setlocale(LC_ALL, ""); 136194763Smarius 137194763Smarius /* 138194763Smarius * Break off the first couple of words to determine what exactly 139194763Smarius * we're being asked to do 140194763Smarius */ 141194763Smarius while (argc > 1) { 142194763Smarius int tmp; 143194763Smarius 144194763Smarius if (*argv[1] == '-') { 145194763Smarius /* 14691398Stmm * Special case, allow pw -V<dir> <operation> [args] for scripts etc. 147194763Smarius */ 148194763Smarius if (argv[1][1] == 'V') { 149194763Smarius optarg = &argv[1][2]; 150223944Smarius if (*optarg == '\0') { 151223944Smarius optarg = argv[2]; 15291398Stmm ++argv; 153194763Smarius --argc; 15491398Stmm } 155194763Smarius addarg(&arglist, 'V', optarg); 15691398Stmm } else 15791398Stmm break; 15891398Stmm } 15991398Stmm else if (mode == -1 && (tmp = getindex(Modes, argv[1])) != -1) 16091398Stmm mode = tmp; 16191398Stmm else if (which == -1 && (tmp = getindex(Which, argv[1])) != -1) 16291398Stmm which = tmp; 16391398Stmm else if ((mode == -1 && which == -1) && 16491398Stmm ((tmp = getindex(Combo1, argv[1])) != -1 || 16591398Stmm (tmp = getindex(Combo2, argv[1])) != -1)) { 16691398Stmm which = tmp / M_NUM; 16791398Stmm mode = tmp % M_NUM; 16891398Stmm } else if (strcmp(argv[1], "help") == 0 && argv[2] == NULL) 16991398Stmm cmdhelp(mode, which); 17091398Stmm else if (which != -1 && mode != -1) 17191398Stmm addarg(&arglist, 'n', argv[1]); 17291398Stmm else 17391398Stmm errx(EX_USAGE, "unknown keyword `%s'", argv[1]); 17491398Stmm ++argv; 17591398Stmm --argc; 17691398Stmm } 17791398Stmm 17891398Stmm /* 17991398Stmm * Bail out unless the user is specific! 180174987Smarius */ 18191398Stmm if (mode == -1 || which == -1) 18291398Stmm cmdhelp(mode, which); 18391398Stmm 18491398Stmm /* 18591398Stmm * We know which mode we're in and what we're about to do, so now 18691398Stmm * let's dispatch the remaining command line args in a genric way. 18791398Stmm */ 18891398Stmm optarg = NULL; 18991398Stmm 19091398Stmm while ((ch = getopt(argc, argv, opts[which][mode])) != -1) { 19191398Stmm if (ch == '?') 19291398Stmm errx(EX_USAGE, "unknown switch"); 19391398Stmm else 19491398Stmm addarg(&arglist, ch, optarg); 19591398Stmm optarg = NULL; 19691398Stmm } 19791398Stmm 19891398Stmm /* 199194763Smarius * Must be root to attempt an update 20091398Stmm */ 20191398Stmm if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && getarg(&arglist, 'N')==NULL) 20291398Stmm errx(EX_NOPERM, "you must be root to run this program"); 20391398Stmm 20491398Stmm /* 20591398Stmm * We should immediately look for the -q 'quiet' switch so that we 20691398Stmm * don't bother with extraneous errors 20791398Stmm */ 20891398Stmm if (getarg(&arglist, 'q') != NULL) 20991398Stmm freopen(_PATH_DEVNULL, "w", stderr); 21091398Stmm 21191398Stmm /* 21291398Stmm * Set our base working path if not overridden 21391398Stmm */ 21491398Stmm 21591398Stmm config = getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL; 21691398Stmm 21791398Stmm if (getarg(&arglist, 'V') != NULL) { 21891398Stmm char * etcpath = getarg(&arglist, 'V')->val; 21991398Stmm if (*etcpath) { 22091398Stmm if (config == NULL) { /* Only override config location if -C not specified */ 22191398Stmm config = malloc(MAXPATHLEN); 22291398Stmm snprintf(config, MAXPATHLEN, "%s/pw.conf", etcpath); 22391398Stmm } 22491398Stmm memcpy(&PWF, &VPWF, sizeof PWF); 22591398Stmm setpwdir(etcpath); 226174987Smarius setgrdir(etcpath); 22791398Stmm } 22891398Stmm } 22991398Stmm 23091398Stmm /* 231172334Smarius * Now, let's do the common initialisation 23291398Stmm */ 23391398Stmm cnf = read_userconfig(config); 23491398Stmm 23591398Stmm ch = funcs[which] (cnf, mode, &arglist); 23691398Stmm 23791398Stmm /* 23891398Stmm * If everything went ok, and we've been asked to update 23991398Stmm * the NIS maps, then do it now 24091398Stmm */ 24191398Stmm if (ch == EXIT_SUCCESS && getarg(&arglist, 'Y') != NULL) { 24291398Stmm pid_t pid; 24391398Stmm 24491398Stmm fflush(NULL); 24591398Stmm if (chdir(_PATH_YP) == -1) 24691398Stmm warn("chdir(" _PATH_YP ")"); 24799726Sbenno else if ((pid = fork()) == -1) 24891398Stmm warn("fork()"); 24991398Stmm else if (pid == 0) { 25091398Stmm /* Is make anywhere else? */ 25199726Sbenno execlp("/usr/bin/make", "make", (char *)NULL); 25299726Sbenno _exit(1); 25399726Sbenno } else { 25491398Stmm int i; 25591398Stmm waitpid(pid, &i, 0); 25691398Stmm if ((i = WEXITSTATUS(i)) != 0) 25791398Stmm errx(ch, "make exited with status %d", i); 25891398Stmm else 25991398Stmm pw_log(cnf, mode, which, "NIS maps updated"); 26091398Stmm } 26191398Stmm } 26291398Stmm return ch; 26391398Stmm} 26491398Stmm 26591398Stmm 26691398Stmmstatic int 26791398Stmmgetindex(const char *words[], const char *word) 26891398Stmm{ 26991398Stmm int i = 0; 27091398Stmm 27191398Stmm while (words[i]) { 272172334Smarius if (strcmp(words[i], word) == 0) 27391398Stmm return i; 27491398Stmm i++; 27591398Stmm } 27691398Stmm return -1; 27791398Stmm} 27891398Stmm 27991398Stmm 28091398Stmm/* 28191398Stmm * This is probably an overkill for a cmdline help system, but it reflects 28291398Stmm * the complexity of the command line. 28391398Stmm */ 28491398Stmm 28591398Stmmstatic void 28691398Stmmcmdhelp(int mode, int which) 28791398Stmm{ 28891398Stmm if (which == -1) 28991398Stmm fprintf(stderr, "usage:\n pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n"); 29091398Stmm else if (mode == -1) 29191398Stmm fprintf(stderr, "usage:\n pw %s [add|del|mod|show|next] [help|switches/values]\n", Which[which]); 29291398Stmm else { 29391398Stmm 29491398Stmm /* 29591398Stmm * We need to give mode specific help 29691398Stmm */ 29791398Stmm static const char *help[W_NUM][M_NUM] = 29891398Stmm { 29991398Stmm { 30091398Stmm "usage: pw useradd [name] [switches]\n" 30191398Stmm "\t-V etcdir alternate /etc location\n" 30291398Stmm "\t-C config configuration file\n" 30391398Stmm "\t-q quiet operation\n" 30491398Stmm " Adding users:\n" 30591398Stmm "\t-n name login name\n" 30691398Stmm "\t-u uid user id\n" 30791398Stmm "\t-c comment user name/comment\n" 30891398Stmm "\t-d directory home directory\n" 30991398Stmm "\t-e date account expiry date\n" 31091398Stmm "\t-p date password expiry date\n" 31191398Stmm "\t-g grp initial group\n" 31291398Stmm "\t-G grp1,grp2 additional groups\n" 31391398Stmm "\t-m [ -k dir ] create and set up home\n" 31491398Stmm "\t-s shell name of login shell\n" 31591398Stmm "\t-o duplicate uid ok\n" 31691398Stmm "\t-L class user class\n" 31791398Stmm "\t-h fd read password on fd\n" 31891398Stmm "\t-Y update NIS maps\n" 31991398Stmm "\t-N no update\n" 32091398Stmm " Setting defaults:\n" 32191398Stmm "\t-V etcdir alternate /etc location\n" 32291398Stmm "\t-D set user defaults\n" 32391398Stmm "\t-b dir default home root dir\n" 32491398Stmm "\t-e period default expiry period\n" 325172334Smarius "\t-p period default password change period\n" 32691398Stmm "\t-g group default group\n" 32791398Stmm "\t-G grp1,grp2 additional groups\n" 32891398Stmm "\t-L class default user class\n" 32991398Stmm "\t-k dir default home skeleton\n" 33091398Stmm "\t-u min,max set min,max uids\n" 33191398Stmm "\t-i min,max set min,max gids\n" 33291398Stmm "\t-w method set default password method\n" 33391398Stmm "\t-s shell default shell\n" 33491398Stmm "\t-y path set NIS passwd file path\n", 33591398Stmm "usage: pw userdel [uid|name] [switches]\n" 33691398Stmm "\t-V etcdir alternate /etc location\n" 33791398Stmm "\t-n name login name\n" 33891398Stmm "\t-u uid user id\n" 33991398Stmm "\t-Y update NIS maps\n" 34091398Stmm "\t-r remove home & contents\n", 34191398Stmm "usage: pw usermod [uid|name] [switches]\n" 34291398Stmm "\t-V etcdir alternate /etc location\n" 34391398Stmm "\t-C config configuration file\n" 34491398Stmm "\t-q quiet operation\n" 34591398Stmm "\t-F force add if no user\n" 34691398Stmm "\t-n name login name\n" 34791398Stmm "\t-u uid user id\n" 34891398Stmm "\t-c comment user name/comment\n" 34991398Stmm "\t-d directory home directory\n" 35091398Stmm "\t-e date account expiry date\n" 35191398Stmm "\t-p date password expiry date\n" 35291398Stmm "\t-g grp initial group\n" 35391398Stmm "\t-G grp1,grp2 additional groups\n" 35491398Stmm "\t-l name new login name\n" 355172334Smarius "\t-L class user class\n" 356172334Smarius "\t-m [ -k dir ] create and set up home\n" 35791398Stmm "\t-s shell name of login shell\n" 358172334Smarius "\t-w method set new password using method\n" 359172334Smarius "\t-h fd read password on fd\n" 360172334Smarius "\t-Y update NIS maps\n" 361172334Smarius "\t-N no update\n", 36291398Stmm "usage: pw usershow [uid|name] [switches]\n" 36391398Stmm "\t-V etcdir alternate /etc location\n" 36491398Stmm "\t-n name login name\n" 36591398Stmm "\t-u uid user id\n" 36699726Sbenno "\t-F force print\n" 36791398Stmm "\t-P prettier format\n" 36891398Stmm "\t-a print all users\n" 36991398Stmm "\t-7 print in v7 format\n", 370172334Smarius "usage: pw usernext [switches]\n" 371172334Smarius "\t-V etcdir alternate /etc location\n" 372172334Smarius "\t-C config configuration file\n" 37391398Stmm "\t-q quiet operation\n", 374172334Smarius "usage pw: lock [switches]\n" 375172334Smarius "\t-V etcdir alternate /etc locations\n" 376172334Smarius "\t-C config configuration file\n" 377172334Smarius "\t-q quiet operation\n", 378172334Smarius "usage pw: unlock [switches]\n" 379172334Smarius "\t-V etcdir alternate /etc locations\n" 380172334Smarius "\t-C config configuration file\n" 381172334Smarius "\t-q quiet operation\n" 38291398Stmm }, 38391398Stmm { 38491398Stmm "usage: pw groupadd [group|gid] [switches]\n" 385172334Smarius "\t-V etcdir alternate /etc location\n" 386174987Smarius "\t-C config configuration file\n" 38791398Stmm "\t-q quiet operation\n" 38891398Stmm "\t-n group group name\n" 38991398Stmm "\t-g gid group id\n" 39091398Stmm "\t-M usr1,usr2 add users as group members\n" 39191398Stmm "\t-o duplicate gid ok\n" 39291398Stmm "\t-Y update NIS maps\n" 393194763Smarius "\t-N no update\n", 394172334Smarius "usage: pw groupdel [group|gid] [switches]\n" 395172334Smarius "\t-V etcdir alternate /etc location\n" 396172334Smarius "\t-n name group name\n" 397172334Smarius "\t-g gid group id\n" 398172334Smarius "\t-Y update NIS maps\n", 39991398Stmm "usage: pw groupmod [group|gid] [switches]\n" 40091398Stmm "\t-V etcdir alternate /etc location\n" 40191398Stmm "\t-C config configuration file\n" 40291398Stmm "\t-q quiet operation\n" 40391398Stmm "\t-F force add if not exists\n" 40491398Stmm "\t-n name group name\n" 40591398Stmm "\t-g gid group id\n" 40691398Stmm "\t-M usr1,usr2 replaces users as group members\n" 40791398Stmm "\t-m usr1,usr2 add users as group members\n" 40891398Stmm "\t-l name new group name\n" 40991398Stmm "\t-Y update NIS maps\n" 41091398Stmm "\t-N no update\n", 41191398Stmm "usage: pw groupshow [group|gid] [switches]\n" 41291398Stmm "\t-V etcdir alternate /etc location\n" 41391398Stmm "\t-n name group name\n" 414172334Smarius "\t-g gid group id\n" 415172334Smarius "\t-F force print\n" 416172334Smarius "\t-P prettier format\n" 417172334Smarius "\t-a print all accounting groups\n", 41891398Stmm "usage: pw groupnext [switches]\n" 41991398Stmm "\t-V etcdir alternate /etc location\n" 42091398Stmm "\t-C config configuration file\n" 42191398Stmm "\t-q quiet operation\n" 42291398Stmm } 423172334Smarius }; 42491398Stmm 425194763Smarius fprintf(stderr, "%s", help[which][mode]); 426194763Smarius } 427194763Smarius exit(EXIT_FAILURE); 428194763Smarius} 42991398Stmm 43091398Stmmstruct carg * 43191398Stmmgetarg(struct cargs * _args, int ch) 43291398Stmm{ 43391398Stmm struct carg *c = LIST_FIRST(_args); 434194763Smarius 435194763Smarius while (c != NULL && c->ch != ch) 43691398Stmm c = LIST_NEXT(c, list); 43791398Stmm return c; 43891398Stmm} 43991398Stmm 440194763Smariusstruct carg * 441194763Smariusaddarg(struct cargs * _args, int ch, char *argstr) 44291398Stmm{ 443194763Smarius struct carg *ca = malloc(sizeof(struct carg)); 44491398Stmm 44591398Stmm if (ca == NULL) 44691398Stmm errx(EX_OSERR, "out of memory"); 44791398Stmm ca->ch = ch; 44891398Stmm ca->val = argstr; 44991398Stmm LIST_INSERT_HEAD(_args, ca, list); 45091398Stmm return ca; 45191398Stmm} 45291398Stmm