120253Sjoerg/*- 220302Sjoerg * Copyright (C) 1996 320302Sjoerg * David L. Nugent. All rights reserved. 420253Sjoerg * 520253Sjoerg * Redistribution and use in source and binary forms, with or without 620253Sjoerg * modification, are permitted provided that the following conditions 720253Sjoerg * are met: 820253Sjoerg * 1. Redistributions of source code must retain the above copyright 920302Sjoerg * notice, this list of conditions and the following disclaimer. 1020253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright 1120253Sjoerg * notice, this list of conditions and the following disclaimer in the 1220253Sjoerg * documentation and/or other materials provided with the distribution. 1320253Sjoerg * 1420302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 1520253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1620253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1720302Sjoerg * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 1820253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1920253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2020253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2120253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2220253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2320253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2420253Sjoerg * SUCH DAMAGE. 2520253Sjoerg */ 2620253Sjoerg 2730259Scharnier#ifndef lint 2830259Scharnierstatic const char rcsid[] = 2950479Speter "$FreeBSD$"; 3030259Scharnier#endif /* not lint */ 3130259Scharnier 3230259Scharnier#include <err.h> 3338112Snate#include <fcntl.h> 3461957Sache#include <locale.h> 3521330Sdavidn#include <paths.h> 3621330Sdavidn#include <sys/wait.h> 3744229Sdavidn#include "pw.h" 3820253Sjoerg 3956000Sdavidn#if !defined(_PATH_YP) 4056000Sdavidn#define _PATH_YP "/var/yp/" 4156000Sdavidn#endif 4252512Sdavidnconst char *Modes[] = { 4352512Sdavidn "add", "del", "mod", "show", "next", 4452512Sdavidn NULL}; 4520253Sjoergconst char *Which[] = {"user", "group", NULL}; 4620267Sjoergstatic const char *Combo1[] = { 4720267Sjoerg "useradd", "userdel", "usermod", "usershow", "usernext", 4852512Sdavidn "lock", "unlock", 4920267Sjoerg "groupadd", "groupdel", "groupmod", "groupshow", "groupnext", 5020267Sjoerg NULL}; 5120267Sjoergstatic const char *Combo2[] = { 5220267Sjoerg "adduser", "deluser", "moduser", "showuser", "nextuser", 5352512Sdavidn "lock", "unlock", 5420267Sjoerg "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup", 5552512Sdavidn NULL}; 5620253Sjoerg 5744229Sdavidnstruct pwf PWF = 5844229Sdavidn{ 5944229Sdavidn 0, 6044229Sdavidn setpwent, 6144229Sdavidn endpwent, 6244229Sdavidn getpwent, 6344229Sdavidn getpwuid, 6444229Sdavidn getpwnam, 6544229Sdavidn pwdb, 6644229Sdavidn setgrent, 6744229Sdavidn endgrent, 6844229Sdavidn getgrent, 6944229Sdavidn getgrgid, 7044229Sdavidn getgrnam, 7144229Sdavidn grdb 7244229Sdavidn 7344229Sdavidn}; 7444229Sdavidnstruct pwf VPWF = 7544229Sdavidn{ 7644229Sdavidn 1, 7744229Sdavidn vsetpwent, 7844229Sdavidn vendpwent, 7944229Sdavidn vgetpwent, 8044229Sdavidn vgetpwuid, 8144229Sdavidn vgetpwnam, 8244229Sdavidn vpwdb, 8344229Sdavidn vsetgrent, 8444229Sdavidn vendgrent, 8544229Sdavidn vgetgrent, 8644229Sdavidn vgetgrgid, 8744229Sdavidn vgetgrnam, 8844229Sdavidn vgrdb 8944229Sdavidn}; 9044229Sdavidn 9120253Sjoergstatic struct cargs arglist; 9220253Sjoerg 9320253Sjoergstatic int getindex(const char *words[], const char *word); 9420253Sjoergstatic void cmdhelp(int mode, int which); 9520253Sjoerg 9620253Sjoerg 9720253Sjoergint 9820253Sjoergmain(int argc, char *argv[]) 9920253Sjoerg{ 10020253Sjoerg int ch; 10120253Sjoerg int mode = -1; 10220253Sjoerg int which = -1; 10344229Sdavidn char *config = NULL; 10420253Sjoerg struct userconf *cnf; 10520253Sjoerg 10620253Sjoerg static const char *opts[W_NUM][M_NUM] = 10720253Sjoerg { 10820267Sjoerg { /* user */ 109167919Sle "V:C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y", 11044231Sdavidn "V:C:qn:u:rY", 111167919Sle "V:C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:FNPY", 11244386Sdavidn "V:C:qn:u:FPa7", 11352512Sdavidn "V:C:q", 11452512Sdavidn "V:C:q", 11544231Sdavidn "V:C:q" 11620267Sjoerg }, 11720267Sjoerg { /* grp */ 118162400Smaxim "V:C:qn:g:h:H:M:opNPY", 11944231Sdavidn "V:C:qn:g:Y", 120176474Sscf "V:C:qn:d:g:l:h:H:FM:m:NPY", 12144231Sdavidn "V:C:qn:g:FPa", 12244231Sdavidn "V:C:q" 12320267Sjoerg } 12420253Sjoerg }; 12520253Sjoerg 12620253Sjoerg static int (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) = 12720253Sjoerg { /* Request handlers */ 12820253Sjoerg pw_user, 12920253Sjoerg pw_group 13020253Sjoerg }; 13120253Sjoerg 13220253Sjoerg LIST_INIT(&arglist); 13320253Sjoerg 13462034Sache (void)setlocale(LC_ALL, ""); 13561957Sache 13620253Sjoerg /* 13720253Sjoerg * Break off the first couple of words to determine what exactly 13820253Sjoerg * we're being asked to do 13920253Sjoerg */ 14044229Sdavidn while (argc > 1) { 14120253Sjoerg int tmp; 14220253Sjoerg 14344229Sdavidn if (*argv[1] == '-') { 14444229Sdavidn /* 14544229Sdavidn * Special case, allow pw -V<dir> <operation> [args] for scripts etc. 14644229Sdavidn */ 14744229Sdavidn if (argv[1][1] == 'V') { 14844229Sdavidn optarg = &argv[1][2]; 14944229Sdavidn if (*optarg == '\0') { 15044229Sdavidn optarg = argv[2]; 15144229Sdavidn ++argv; 15244229Sdavidn --argc; 15344229Sdavidn } 15444229Sdavidn addarg(&arglist, 'V', optarg); 15544231Sdavidn } else 15644231Sdavidn break; 15744229Sdavidn } 15861760Sdavidn else if (mode == -1 && (tmp = getindex(Modes, argv[1])) != -1) 15920253Sjoerg mode = tmp; 16061760Sdavidn else if (which == -1 && (tmp = getindex(Which, argv[1])) != -1) 16120253Sjoerg which = tmp; 16261760Sdavidn else if ((mode == -1 && which == -1) && 16361760Sdavidn ((tmp = getindex(Combo1, argv[1])) != -1 || 16461760Sdavidn (tmp = getindex(Combo2, argv[1])) != -1)) { 16520253Sjoerg which = tmp / M_NUM; 16620253Sjoerg mode = tmp % M_NUM; 16761760Sdavidn } else if (strcmp(argv[1], "help") == 0 && argv[2] == NULL) 16820253Sjoerg cmdhelp(mode, which); 16944232Sdavidn else if (which != -1 && mode != -1) 17020253Sjoerg addarg(&arglist, 'n', argv[1]); 17120253Sjoerg else 17230259Scharnier errx(EX_USAGE, "unknown keyword `%s'", argv[1]); 17320253Sjoerg ++argv; 17420253Sjoerg --argc; 17520253Sjoerg } 17620253Sjoerg 17720253Sjoerg /* 17820253Sjoerg * Bail out unless the user is specific! 17920253Sjoerg */ 18020253Sjoerg if (mode == -1 || which == -1) 18120253Sjoerg cmdhelp(mode, which); 18220253Sjoerg 18320253Sjoerg /* 18420253Sjoerg * We know which mode we're in and what we're about to do, so now 18520253Sjoerg * let's dispatch the remaining command line args in a genric way. 18620253Sjoerg */ 18720253Sjoerg optarg = NULL; 18820253Sjoerg 18920253Sjoerg while ((ch = getopt(argc, argv, opts[which][mode])) != -1) { 19020253Sjoerg if (ch == '?') 19156000Sdavidn errx(EX_USAGE, "unknown switch"); 19220253Sjoerg else 19320253Sjoerg addarg(&arglist, ch, optarg); 19420253Sjoerg optarg = NULL; 19520253Sjoerg } 19620253Sjoerg 19720253Sjoerg /* 19820267Sjoerg * Must be root to attempt an update 19920267Sjoerg */ 20027474Sdavidn if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && getarg(&arglist, 'N')==NULL) 20130259Scharnier errx(EX_NOPERM, "you must be root to run this program"); 20220267Sjoerg 20320267Sjoerg /* 20420253Sjoerg * We should immediately look for the -q 'quiet' switch so that we 20520253Sjoerg * don't bother with extraneous errors 20620253Sjoerg */ 20720253Sjoerg if (getarg(&arglist, 'q') != NULL) 20869793Sobrien freopen(_PATH_DEVNULL, "w", stderr); 20920253Sjoerg 21020253Sjoerg /* 21144229Sdavidn * Set our base working path if not overridden 21244229Sdavidn */ 21344229Sdavidn 21444229Sdavidn config = getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL; 21544229Sdavidn 21644229Sdavidn if (getarg(&arglist, 'V') != NULL) { 21744229Sdavidn char * etcpath = getarg(&arglist, 'V')->val; 21844229Sdavidn if (*etcpath) { 21944229Sdavidn if (config == NULL) { /* Only override config location if -C not specified */ 22044229Sdavidn config = malloc(MAXPATHLEN); 22144229Sdavidn snprintf(config, MAXPATHLEN, "%s/pw.conf", etcpath); 22244229Sdavidn } 22344229Sdavidn memcpy(&PWF, &VPWF, sizeof PWF); 22444229Sdavidn setpwdir(etcpath); 22544229Sdavidn setgrdir(etcpath); 22644229Sdavidn } 22744229Sdavidn } 228176473Sscf 22944229Sdavidn /* 23020253Sjoerg * Now, let's do the common initialisation 23120253Sjoerg */ 23244229Sdavidn cnf = read_userconfig(config); 23338112Snate 23438110Snate ch = funcs[which] (cnf, mode, &arglist); 23521330Sdavidn 23621330Sdavidn /* 23721330Sdavidn * If everything went ok, and we've been asked to update 23821330Sdavidn * the NIS maps, then do it now 23921330Sdavidn */ 24021330Sdavidn if (ch == EXIT_SUCCESS && getarg(&arglist, 'Y') != NULL) { 24121330Sdavidn pid_t pid; 24221330Sdavidn 24321330Sdavidn fflush(NULL); 24421330Sdavidn if (chdir(_PATH_YP) == -1) 24530259Scharnier warn("chdir(" _PATH_YP ")"); 24621330Sdavidn else if ((pid = fork()) == -1) 24730259Scharnier warn("fork()"); 24821330Sdavidn else if (pid == 0) { 24921330Sdavidn /* Is make anywhere else? */ 25079452Sbrian execlp("/usr/bin/make", "make", (char *)NULL); 25121330Sdavidn _exit(1); 25221330Sdavidn } else { 25321330Sdavidn int i; 25421330Sdavidn waitpid(pid, &i, 0); 25521330Sdavidn if ((i = WEXITSTATUS(i)) != 0) 25630259Scharnier errx(ch, "make exited with status %d", i); 25721330Sdavidn else 25821330Sdavidn pw_log(cnf, mode, which, "NIS maps updated"); 25921330Sdavidn } 26021330Sdavidn } 26121330Sdavidn return ch; 26220253Sjoerg} 26320253Sjoerg 26438112Snate 26520253Sjoergstatic int 26620253Sjoerggetindex(const char *words[], const char *word) 26720253Sjoerg{ 26820253Sjoerg int i = 0; 26920253Sjoerg 27020253Sjoerg while (words[i]) { 27120253Sjoerg if (strcmp(words[i], word) == 0) 27220253Sjoerg return i; 27320253Sjoerg i++; 27420253Sjoerg } 27520253Sjoerg return -1; 27620253Sjoerg} 27720253Sjoerg 27820253Sjoerg 27920253Sjoerg/* 28020253Sjoerg * This is probably an overkill for a cmdline help system, but it reflects 28120253Sjoerg * the complexity of the command line. 28220253Sjoerg */ 28320253Sjoerg 28420253Sjoergstatic void 28520253Sjoergcmdhelp(int mode, int which) 28620253Sjoerg{ 28720253Sjoerg if (which == -1) 28852512Sdavidn fprintf(stderr, "usage:\n pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n"); 28920253Sjoerg else if (mode == -1) 29052512Sdavidn fprintf(stderr, "usage:\n pw %s [add|del|mod|show|next] [help|switches/values]\n", Which[which]); 29120253Sjoerg else { 29220253Sjoerg 29320253Sjoerg /* 29420253Sjoerg * We need to give mode specific help 29520253Sjoerg */ 29620253Sjoerg static const char *help[W_NUM][M_NUM] = 29720253Sjoerg { 29820253Sjoerg { 29930259Scharnier "usage: pw useradd [name] [switches]\n" 30044229Sdavidn "\t-V etcdir alternate /etc location\n" 30120253Sjoerg "\t-C config configuration file\n" 30220253Sjoerg "\t-q quiet operation\n" 30320253Sjoerg " Adding users:\n" 30420253Sjoerg "\t-n name login name\n" 30520253Sjoerg "\t-u uid user id\n" 30620253Sjoerg "\t-c comment user name/comment\n" 30720253Sjoerg "\t-d directory home directory\n" 30820253Sjoerg "\t-e date account expiry date\n" 30920253Sjoerg "\t-p date password expiry date\n" 31020253Sjoerg "\t-g grp initial group\n" 31120253Sjoerg "\t-G grp1,grp2 additional groups\n" 31220253Sjoerg "\t-m [ -k dir ] create and set up home\n" 313168043Sle "\t-M mode home directory permissions\n" 31420253Sjoerg "\t-s shell name of login shell\n" 31520253Sjoerg "\t-o duplicate uid ok\n" 31620253Sjoerg "\t-L class user class\n" 31720253Sjoerg "\t-h fd read password on fd\n" 318124382Siedowse "\t-H fd read encrypted password on fd\n" 31921330Sdavidn "\t-Y update NIS maps\n" 32020267Sjoerg "\t-N no update\n" 32120253Sjoerg " Setting defaults:\n" 32244229Sdavidn "\t-V etcdir alternate /etc location\n" 32344229Sdavidn "\t-D set user defaults\n" 32420253Sjoerg "\t-b dir default home root dir\n" 32520253Sjoerg "\t-e period default expiry period\n" 32620253Sjoerg "\t-p period default password change period\n" 32720253Sjoerg "\t-g group default group\n" 32820253Sjoerg "\t-G grp1,grp2 additional groups\n" 32920253Sjoerg "\t-L class default user class\n" 33020253Sjoerg "\t-k dir default home skeleton\n" 331168044Sle "\t-M mode home directory permissions\n" 33220253Sjoerg "\t-u min,max set min,max uids\n" 33320253Sjoerg "\t-i min,max set min,max gids\n" 33420253Sjoerg "\t-w method set default password method\n" 33521330Sdavidn "\t-s shell default shell\n" 33621330Sdavidn "\t-y path set NIS passwd file path\n", 33730259Scharnier "usage: pw userdel [uid|name] [switches]\n" 33844229Sdavidn "\t-V etcdir alternate /etc location\n" 33920253Sjoerg "\t-n name login name\n" 34020253Sjoerg "\t-u uid user id\n" 34121330Sdavidn "\t-Y update NIS maps\n" 34220253Sjoerg "\t-r remove home & contents\n", 34330259Scharnier "usage: pw usermod [uid|name] [switches]\n" 34444229Sdavidn "\t-V etcdir alternate /etc location\n" 34520253Sjoerg "\t-C config configuration file\n" 34620253Sjoerg "\t-q quiet operation\n" 34720253Sjoerg "\t-F force add if no user\n" 34820253Sjoerg "\t-n name login name\n" 34920253Sjoerg "\t-u uid user id\n" 35020253Sjoerg "\t-c comment user name/comment\n" 35120253Sjoerg "\t-d directory home directory\n" 35220253Sjoerg "\t-e date account expiry date\n" 35320253Sjoerg "\t-p date password expiry date\n" 35420253Sjoerg "\t-g grp initial group\n" 35520253Sjoerg "\t-G grp1,grp2 additional groups\n" 35620253Sjoerg "\t-l name new login name\n" 35720253Sjoerg "\t-L class user class\n" 35820253Sjoerg "\t-m [ -k dir ] create and set up home\n" 359168043Sle "\t-M mode home directory permissions\n" 36020253Sjoerg "\t-s shell name of login shell\n" 36120267Sjoerg "\t-w method set new password using method\n" 36220267Sjoerg "\t-h fd read password on fd\n" 363124382Siedowse "\t-H fd read encrypted password on fd\n" 36421330Sdavidn "\t-Y update NIS maps\n" 36520267Sjoerg "\t-N no update\n", 36630259Scharnier "usage: pw usershow [uid|name] [switches]\n" 36744229Sdavidn "\t-V etcdir alternate /etc location\n" 36820253Sjoerg "\t-n name login name\n" 36920253Sjoerg "\t-u uid user id\n" 37020253Sjoerg "\t-F force print\n" 37120267Sjoerg "\t-P prettier format\n" 37244386Sdavidn "\t-a print all users\n" 37344386Sdavidn "\t-7 print in v7 format\n", 37430259Scharnier "usage: pw usernext [switches]\n" 37544229Sdavidn "\t-V etcdir alternate /etc location\n" 37620267Sjoerg "\t-C config configuration file\n" 37774226Sdd "\t-q quiet operation\n", 37874226Sdd "usage pw: lock [switches]\n" 37974226Sdd "\t-V etcdir alternate /etc locations\n" 38074226Sdd "\t-C config configuration file\n" 38174226Sdd "\t-q quiet operation\n", 38274226Sdd "usage pw: unlock [switches]\n" 38374226Sdd "\t-V etcdir alternate /etc locations\n" 38474226Sdd "\t-C config configuration file\n" 38574226Sdd "\t-q quiet operation\n" 38620253Sjoerg }, 38720253Sjoerg { 38830259Scharnier "usage: pw groupadd [group|gid] [switches]\n" 38944229Sdavidn "\t-V etcdir alternate /etc location\n" 39020253Sjoerg "\t-C config configuration file\n" 39120253Sjoerg "\t-q quiet operation\n" 39220253Sjoerg "\t-n group group name\n" 39320253Sjoerg "\t-g gid group id\n" 39420267Sjoerg "\t-M usr1,usr2 add users as group members\n" 39520267Sjoerg "\t-o duplicate gid ok\n" 39621330Sdavidn "\t-Y update NIS maps\n" 39720267Sjoerg "\t-N no update\n", 39830259Scharnier "usage: pw groupdel [group|gid] [switches]\n" 39944229Sdavidn "\t-V etcdir alternate /etc location\n" 40020253Sjoerg "\t-n name group name\n" 40121330Sdavidn "\t-g gid group id\n" 40221330Sdavidn "\t-Y update NIS maps\n", 40330259Scharnier "usage: pw groupmod [group|gid] [switches]\n" 40444229Sdavidn "\t-V etcdir alternate /etc location\n" 40520253Sjoerg "\t-C config configuration file\n" 40620253Sjoerg "\t-q quiet operation\n" 40720253Sjoerg "\t-F force add if not exists\n" 40820253Sjoerg "\t-n name group name\n" 40920253Sjoerg "\t-g gid group id\n" 41020267Sjoerg "\t-M usr1,usr2 replaces users as group members\n" 41120267Sjoerg "\t-m usr1,usr2 add users as group members\n" 412176474Sscf "\t-d usr1,usr2 delete users as group members\n" 41320267Sjoerg "\t-l name new group name\n" 41421330Sdavidn "\t-Y update NIS maps\n" 41520267Sjoerg "\t-N no update\n", 41630259Scharnier "usage: pw groupshow [group|gid] [switches]\n" 41744229Sdavidn "\t-V etcdir alternate /etc location\n" 41820253Sjoerg "\t-n name group name\n" 41920253Sjoerg "\t-g gid group id\n" 42020253Sjoerg "\t-F force print\n" 42120267Sjoerg "\t-P prettier format\n" 42220267Sjoerg "\t-a print all accounting groups\n", 42330259Scharnier "usage: pw groupnext [switches]\n" 42444229Sdavidn "\t-V etcdir alternate /etc location\n" 42520267Sjoerg "\t-C config configuration file\n" 42674226Sdd "\t-q quiet operation\n" 42720253Sjoerg } 42820253Sjoerg }; 42920253Sjoerg 43079292Skris fprintf(stderr, "%s", help[which][mode]); 43120253Sjoerg } 43220267Sjoerg exit(EXIT_FAILURE); 43320253Sjoerg} 43420253Sjoerg 43520253Sjoergstruct carg * 43620253Sjoerggetarg(struct cargs * _args, int ch) 43720253Sjoerg{ 43870486Sben struct carg *c = LIST_FIRST(_args); 43920253Sjoerg 44020253Sjoerg while (c != NULL && c->ch != ch) 44170486Sben c = LIST_NEXT(c, list); 44220253Sjoerg return c; 44320253Sjoerg} 44420253Sjoerg 44520253Sjoergstruct carg * 44620253Sjoergaddarg(struct cargs * _args, int ch, char *argstr) 44720253Sjoerg{ 44820253Sjoerg struct carg *ca = malloc(sizeof(struct carg)); 44920253Sjoerg 45020253Sjoerg if (ca == NULL) 45130259Scharnier errx(EX_OSERR, "out of memory"); 45220253Sjoerg ca->ch = ch; 45320253Sjoerg ca->val = argstr; 45420253Sjoerg LIST_INSERT_HEAD(_args, ca, list); 45520253Sjoerg return ca; 45620253Sjoerg} 457