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 setgrent, 6644229Sdavidn endgrent, 6744229Sdavidn getgrent, 6844229Sdavidn getgrgid, 6944229Sdavidn getgrnam, 7044229Sdavidn 7144229Sdavidn}; 7244229Sdavidnstruct pwf VPWF = 7344229Sdavidn{ 7444229Sdavidn 1, 7544229Sdavidn vsetpwent, 7644229Sdavidn vendpwent, 7744229Sdavidn vgetpwent, 7844229Sdavidn vgetpwuid, 7944229Sdavidn vgetpwnam, 8044229Sdavidn vsetgrent, 8144229Sdavidn vendgrent, 8244229Sdavidn vgetgrent, 8344229Sdavidn vgetgrgid, 8444229Sdavidn vgetgrnam, 8544229Sdavidn}; 8644229Sdavidn 8720253Sjoergstatic struct cargs arglist; 8820253Sjoerg 8920253Sjoergstatic int getindex(const char *words[], const char *word); 9020253Sjoergstatic void cmdhelp(int mode, int which); 9120253Sjoerg 9220253Sjoerg 9320253Sjoergint 9420253Sjoergmain(int argc, char *argv[]) 9520253Sjoerg{ 9620253Sjoerg int ch; 9720253Sjoerg int mode = -1; 9820253Sjoerg int which = -1; 9944229Sdavidn char *config = NULL; 10020253Sjoerg struct userconf *cnf; 10120253Sjoerg 10220253Sjoerg static const char *opts[W_NUM][M_NUM] = 10320253Sjoerg { 10420267Sjoerg { /* user */ 105167919Sle "V:C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y", 10644231Sdavidn "V:C:qn:u:rY", 107167919Sle "V:C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:FNPY", 10844386Sdavidn "V:C:qn:u:FPa7", 10952512Sdavidn "V:C:q", 11052512Sdavidn "V:C:q", 11144231Sdavidn "V:C:q" 11220267Sjoerg }, 11320267Sjoerg { /* grp */ 114162400Smaxim "V:C:qn:g:h:H:M:opNPY", 11544231Sdavidn "V:C:qn:g:Y", 116176474Sscf "V:C:qn:d:g:l:h:H:FM:m:NPY", 11744231Sdavidn "V:C:qn:g:FPa", 11844231Sdavidn "V:C:q" 11920267Sjoerg } 12020253Sjoerg }; 12120253Sjoerg 12220253Sjoerg static int (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) = 12320253Sjoerg { /* Request handlers */ 12420253Sjoerg pw_user, 12520253Sjoerg pw_group 12620253Sjoerg }; 12720253Sjoerg 12820253Sjoerg LIST_INIT(&arglist); 12920253Sjoerg 13062034Sache (void)setlocale(LC_ALL, ""); 13161957Sache 13220253Sjoerg /* 13320253Sjoerg * Break off the first couple of words to determine what exactly 13420253Sjoerg * we're being asked to do 13520253Sjoerg */ 13644229Sdavidn while (argc > 1) { 13720253Sjoerg int tmp; 13820253Sjoerg 13944229Sdavidn if (*argv[1] == '-') { 14044229Sdavidn /* 14144229Sdavidn * Special case, allow pw -V<dir> <operation> [args] for scripts etc. 14244229Sdavidn */ 14344229Sdavidn if (argv[1][1] == 'V') { 14444229Sdavidn optarg = &argv[1][2]; 14544229Sdavidn if (*optarg == '\0') { 14644229Sdavidn optarg = argv[2]; 14744229Sdavidn ++argv; 14844229Sdavidn --argc; 14944229Sdavidn } 15044229Sdavidn addarg(&arglist, 'V', optarg); 15144231Sdavidn } else 15244231Sdavidn break; 15344229Sdavidn } 15461760Sdavidn else if (mode == -1 && (tmp = getindex(Modes, argv[1])) != -1) 15520253Sjoerg mode = tmp; 15661760Sdavidn else if (which == -1 && (tmp = getindex(Which, argv[1])) != -1) 15720253Sjoerg which = tmp; 15861760Sdavidn else if ((mode == -1 && which == -1) && 15961760Sdavidn ((tmp = getindex(Combo1, argv[1])) != -1 || 16061760Sdavidn (tmp = getindex(Combo2, argv[1])) != -1)) { 16120253Sjoerg which = tmp / M_NUM; 16220253Sjoerg mode = tmp % M_NUM; 16361760Sdavidn } else if (strcmp(argv[1], "help") == 0 && argv[2] == NULL) 16420253Sjoerg cmdhelp(mode, which); 16544232Sdavidn else if (which != -1 && mode != -1) 16620253Sjoerg addarg(&arglist, 'n', argv[1]); 16720253Sjoerg else 16830259Scharnier errx(EX_USAGE, "unknown keyword `%s'", argv[1]); 16920253Sjoerg ++argv; 17020253Sjoerg --argc; 17120253Sjoerg } 17220253Sjoerg 17320253Sjoerg /* 17420253Sjoerg * Bail out unless the user is specific! 17520253Sjoerg */ 17620253Sjoerg if (mode == -1 || which == -1) 17720253Sjoerg cmdhelp(mode, which); 17820253Sjoerg 17920253Sjoerg /* 18020253Sjoerg * We know which mode we're in and what we're about to do, so now 18120253Sjoerg * let's dispatch the remaining command line args in a genric way. 18220253Sjoerg */ 18320253Sjoerg optarg = NULL; 18420253Sjoerg 18520253Sjoerg while ((ch = getopt(argc, argv, opts[which][mode])) != -1) { 18620253Sjoerg if (ch == '?') 18756000Sdavidn errx(EX_USAGE, "unknown switch"); 18820253Sjoerg else 18920253Sjoerg addarg(&arglist, ch, optarg); 19020253Sjoerg optarg = NULL; 19120253Sjoerg } 19220253Sjoerg 19320253Sjoerg /* 19420267Sjoerg * Must be root to attempt an update 19520267Sjoerg */ 19627474Sdavidn if (geteuid() != 0 && mode != M_PRINT && mode != M_NEXT && getarg(&arglist, 'N')==NULL) 19730259Scharnier errx(EX_NOPERM, "you must be root to run this program"); 19820267Sjoerg 19920267Sjoerg /* 20020253Sjoerg * We should immediately look for the -q 'quiet' switch so that we 20120253Sjoerg * don't bother with extraneous errors 20220253Sjoerg */ 20320253Sjoerg if (getarg(&arglist, 'q') != NULL) 20469793Sobrien freopen(_PATH_DEVNULL, "w", stderr); 20520253Sjoerg 20620253Sjoerg /* 20744229Sdavidn * Set our base working path if not overridden 20844229Sdavidn */ 20944229Sdavidn 21044229Sdavidn config = getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL; 21144229Sdavidn 21244229Sdavidn if (getarg(&arglist, 'V') != NULL) { 21344229Sdavidn char * etcpath = getarg(&arglist, 'V')->val; 21444229Sdavidn if (*etcpath) { 21544229Sdavidn if (config == NULL) { /* Only override config location if -C not specified */ 21644229Sdavidn config = malloc(MAXPATHLEN); 21744229Sdavidn snprintf(config, MAXPATHLEN, "%s/pw.conf", etcpath); 21844229Sdavidn } 21944229Sdavidn memcpy(&PWF, &VPWF, sizeof PWF); 22044229Sdavidn setpwdir(etcpath); 22144229Sdavidn setgrdir(etcpath); 22244229Sdavidn } 22344229Sdavidn } 224176473Sscf 22544229Sdavidn /* 22620253Sjoerg * Now, let's do the common initialisation 22720253Sjoerg */ 22844229Sdavidn cnf = read_userconfig(config); 22938112Snate 23038110Snate ch = funcs[which] (cnf, mode, &arglist); 23121330Sdavidn 23221330Sdavidn /* 23321330Sdavidn * If everything went ok, and we've been asked to update 23421330Sdavidn * the NIS maps, then do it now 23521330Sdavidn */ 23621330Sdavidn if (ch == EXIT_SUCCESS && getarg(&arglist, 'Y') != NULL) { 23721330Sdavidn pid_t pid; 23821330Sdavidn 23921330Sdavidn fflush(NULL); 24021330Sdavidn if (chdir(_PATH_YP) == -1) 24130259Scharnier warn("chdir(" _PATH_YP ")"); 24221330Sdavidn else if ((pid = fork()) == -1) 24330259Scharnier warn("fork()"); 24421330Sdavidn else if (pid == 0) { 24521330Sdavidn /* Is make anywhere else? */ 24679452Sbrian execlp("/usr/bin/make", "make", (char *)NULL); 24721330Sdavidn _exit(1); 24821330Sdavidn } else { 24921330Sdavidn int i; 25021330Sdavidn waitpid(pid, &i, 0); 25121330Sdavidn if ((i = WEXITSTATUS(i)) != 0) 25230259Scharnier errx(ch, "make exited with status %d", i); 25321330Sdavidn else 25421330Sdavidn pw_log(cnf, mode, which, "NIS maps updated"); 25521330Sdavidn } 25621330Sdavidn } 25721330Sdavidn return ch; 25820253Sjoerg} 25920253Sjoerg 26038112Snate 26120253Sjoergstatic int 26220253Sjoerggetindex(const char *words[], const char *word) 26320253Sjoerg{ 26420253Sjoerg int i = 0; 26520253Sjoerg 26620253Sjoerg while (words[i]) { 26720253Sjoerg if (strcmp(words[i], word) == 0) 26820253Sjoerg return i; 26920253Sjoerg i++; 27020253Sjoerg } 27120253Sjoerg return -1; 27220253Sjoerg} 27320253Sjoerg 27420253Sjoerg 27520253Sjoerg/* 27620253Sjoerg * This is probably an overkill for a cmdline help system, but it reflects 27720253Sjoerg * the complexity of the command line. 27820253Sjoerg */ 27920253Sjoerg 28020253Sjoergstatic void 28120253Sjoergcmdhelp(int mode, int which) 28220253Sjoerg{ 28320253Sjoerg if (which == -1) 28452512Sdavidn fprintf(stderr, "usage:\n pw [user|group|lock|unlock] [add|del|mod|show|next] [help|switches/values]\n"); 28520253Sjoerg else if (mode == -1) 28652512Sdavidn fprintf(stderr, "usage:\n pw %s [add|del|mod|show|next] [help|switches/values]\n", Which[which]); 28720253Sjoerg else { 28820253Sjoerg 28920253Sjoerg /* 29020253Sjoerg * We need to give mode specific help 29120253Sjoerg */ 29220253Sjoerg static const char *help[W_NUM][M_NUM] = 29320253Sjoerg { 29420253Sjoerg { 29530259Scharnier "usage: pw useradd [name] [switches]\n" 29644229Sdavidn "\t-V etcdir alternate /etc location\n" 29720253Sjoerg "\t-C config configuration file\n" 29820253Sjoerg "\t-q quiet operation\n" 29920253Sjoerg " Adding users:\n" 30020253Sjoerg "\t-n name login name\n" 30120253Sjoerg "\t-u uid user id\n" 30220253Sjoerg "\t-c comment user name/comment\n" 30320253Sjoerg "\t-d directory home directory\n" 30420253Sjoerg "\t-e date account expiry date\n" 30520253Sjoerg "\t-p date password expiry date\n" 30620253Sjoerg "\t-g grp initial group\n" 30720253Sjoerg "\t-G grp1,grp2 additional groups\n" 30820253Sjoerg "\t-m [ -k dir ] create and set up home\n" 309168043Sle "\t-M mode home directory permissions\n" 31020253Sjoerg "\t-s shell name of login shell\n" 31120253Sjoerg "\t-o duplicate uid ok\n" 31220253Sjoerg "\t-L class user class\n" 31320253Sjoerg "\t-h fd read password on fd\n" 314124382Siedowse "\t-H fd read encrypted password on fd\n" 31521330Sdavidn "\t-Y update NIS maps\n" 31620267Sjoerg "\t-N no update\n" 31720253Sjoerg " Setting defaults:\n" 31844229Sdavidn "\t-V etcdir alternate /etc location\n" 31944229Sdavidn "\t-D set user defaults\n" 32020253Sjoerg "\t-b dir default home root dir\n" 32120253Sjoerg "\t-e period default expiry period\n" 32220253Sjoerg "\t-p period default password change period\n" 32320253Sjoerg "\t-g group default group\n" 32420253Sjoerg "\t-G grp1,grp2 additional groups\n" 32520253Sjoerg "\t-L class default user class\n" 32620253Sjoerg "\t-k dir default home skeleton\n" 327168044Sle "\t-M mode home directory permissions\n" 32820253Sjoerg "\t-u min,max set min,max uids\n" 32920253Sjoerg "\t-i min,max set min,max gids\n" 33020253Sjoerg "\t-w method set default password method\n" 33121330Sdavidn "\t-s shell default shell\n" 33221330Sdavidn "\t-y path set NIS passwd file path\n", 33330259Scharnier "usage: pw userdel [uid|name] [switches]\n" 33444229Sdavidn "\t-V etcdir alternate /etc location\n" 33520253Sjoerg "\t-n name login name\n" 33620253Sjoerg "\t-u uid user id\n" 33721330Sdavidn "\t-Y update NIS maps\n" 33820253Sjoerg "\t-r remove home & contents\n", 33930259Scharnier "usage: pw usermod [uid|name] [switches]\n" 34044229Sdavidn "\t-V etcdir alternate /etc location\n" 34120253Sjoerg "\t-C config configuration file\n" 34220253Sjoerg "\t-q quiet operation\n" 34320253Sjoerg "\t-F force add if no user\n" 34420253Sjoerg "\t-n name login name\n" 34520253Sjoerg "\t-u uid user id\n" 34620253Sjoerg "\t-c comment user name/comment\n" 34720253Sjoerg "\t-d directory home directory\n" 34820253Sjoerg "\t-e date account expiry date\n" 34920253Sjoerg "\t-p date password expiry date\n" 35020253Sjoerg "\t-g grp initial group\n" 35120253Sjoerg "\t-G grp1,grp2 additional groups\n" 35220253Sjoerg "\t-l name new login name\n" 35320253Sjoerg "\t-L class user class\n" 35420253Sjoerg "\t-m [ -k dir ] create and set up home\n" 355168043Sle "\t-M mode home directory permissions\n" 35620253Sjoerg "\t-s shell name of login shell\n" 35720267Sjoerg "\t-w method set new password using method\n" 35820267Sjoerg "\t-h fd read password on fd\n" 359124382Siedowse "\t-H fd read encrypted password on fd\n" 36021330Sdavidn "\t-Y update NIS maps\n" 36120267Sjoerg "\t-N no update\n", 36230259Scharnier "usage: pw usershow [uid|name] [switches]\n" 36344229Sdavidn "\t-V etcdir alternate /etc location\n" 36420253Sjoerg "\t-n name login name\n" 36520253Sjoerg "\t-u uid user id\n" 36620253Sjoerg "\t-F force print\n" 36720267Sjoerg "\t-P prettier format\n" 36844386Sdavidn "\t-a print all users\n" 36944386Sdavidn "\t-7 print in v7 format\n", 37030259Scharnier "usage: pw usernext [switches]\n" 37144229Sdavidn "\t-V etcdir alternate /etc location\n" 37220267Sjoerg "\t-C config configuration file\n" 37374226Sdd "\t-q quiet operation\n", 37474226Sdd "usage pw: lock [switches]\n" 37574226Sdd "\t-V etcdir alternate /etc locations\n" 37674226Sdd "\t-C config configuration file\n" 37774226Sdd "\t-q quiet operation\n", 37874226Sdd "usage pw: unlock [switches]\n" 37974226Sdd "\t-V etcdir alternate /etc locations\n" 38074226Sdd "\t-C config configuration file\n" 38174226Sdd "\t-q quiet operation\n" 38220253Sjoerg }, 38320253Sjoerg { 38430259Scharnier "usage: pw groupadd [group|gid] [switches]\n" 38544229Sdavidn "\t-V etcdir alternate /etc location\n" 38620253Sjoerg "\t-C config configuration file\n" 38720253Sjoerg "\t-q quiet operation\n" 38820253Sjoerg "\t-n group group name\n" 38920253Sjoerg "\t-g gid group id\n" 39020267Sjoerg "\t-M usr1,usr2 add users as group members\n" 39120267Sjoerg "\t-o duplicate gid ok\n" 39221330Sdavidn "\t-Y update NIS maps\n" 39320267Sjoerg "\t-N no update\n", 39430259Scharnier "usage: pw groupdel [group|gid] [switches]\n" 39544229Sdavidn "\t-V etcdir alternate /etc location\n" 39620253Sjoerg "\t-n name group name\n" 39721330Sdavidn "\t-g gid group id\n" 39821330Sdavidn "\t-Y update NIS maps\n", 39930259Scharnier "usage: pw groupmod [group|gid] [switches]\n" 40044229Sdavidn "\t-V etcdir alternate /etc location\n" 40120253Sjoerg "\t-C config configuration file\n" 40220253Sjoerg "\t-q quiet operation\n" 40320253Sjoerg "\t-F force add if not exists\n" 40420253Sjoerg "\t-n name group name\n" 40520253Sjoerg "\t-g gid group id\n" 40620267Sjoerg "\t-M usr1,usr2 replaces users as group members\n" 40720267Sjoerg "\t-m usr1,usr2 add users as group members\n" 408176474Sscf "\t-d usr1,usr2 delete users as group members\n" 40920267Sjoerg "\t-l name new group name\n" 41021330Sdavidn "\t-Y update NIS maps\n" 41120267Sjoerg "\t-N no update\n", 41230259Scharnier "usage: pw groupshow [group|gid] [switches]\n" 41344229Sdavidn "\t-V etcdir alternate /etc location\n" 41420253Sjoerg "\t-n name group name\n" 41520253Sjoerg "\t-g gid group id\n" 41620253Sjoerg "\t-F force print\n" 41720267Sjoerg "\t-P prettier format\n" 41820267Sjoerg "\t-a print all accounting groups\n", 41930259Scharnier "usage: pw groupnext [switches]\n" 42044229Sdavidn "\t-V etcdir alternate /etc location\n" 42120267Sjoerg "\t-C config configuration file\n" 42274226Sdd "\t-q quiet operation\n" 42320253Sjoerg } 42420253Sjoerg }; 42520253Sjoerg 42679292Skris fprintf(stderr, "%s", help[which][mode]); 42720253Sjoerg } 42820267Sjoerg exit(EXIT_FAILURE); 42920253Sjoerg} 43020253Sjoerg 43120253Sjoergstruct carg * 43220253Sjoerggetarg(struct cargs * _args, int ch) 43320253Sjoerg{ 43470486Sben struct carg *c = LIST_FIRST(_args); 43520253Sjoerg 43620253Sjoerg while (c != NULL && c->ch != ch) 43770486Sben c = LIST_NEXT(c, list); 43820253Sjoerg return c; 43920253Sjoerg} 44020253Sjoerg 44120253Sjoergstruct carg * 44220253Sjoergaddarg(struct cargs * _args, int ch, char *argstr) 44320253Sjoerg{ 44420253Sjoerg struct carg *ca = malloc(sizeof(struct carg)); 44520253Sjoerg 44620253Sjoerg if (ca == NULL) 44730259Scharnier errx(EX_OSERR, "out of memory"); 44820253Sjoerg ca->ch = ch; 44920253Sjoerg ca->val = argstr; 45020253Sjoerg LIST_INSERT_HEAD(_args, ca, list); 45120253Sjoerg return ca; 45220253Sjoerg} 453