pw_user.c revision 285133
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. 2544229Sdavidn * 2620253Sjoerg */ 2720253Sjoerg 2830259Scharnier#ifndef lint 2930259Scharnierstatic const char rcsid[] = 3050479Speter "$FreeBSD: head/usr.sbin/pw/pw_user.c 285133 2015-07-04 15:27:04Z bapt $"; 3130259Scharnier#endif /* not lint */ 3230259Scharnier 3330259Scharnier#include <ctype.h> 3430259Scharnier#include <err.h> 3520253Sjoerg#include <fcntl.h> 3620253Sjoerg#include <sys/param.h> 3720253Sjoerg#include <dirent.h> 3830259Scharnier#include <paths.h> 3920253Sjoerg#include <termios.h> 4020555Sdavidn#include <sys/types.h> 4120555Sdavidn#include <sys/time.h> 4220555Sdavidn#include <sys/resource.h> 4364918Sgreen#include <login_cap.h> 44242349Sbapt#include <pwd.h> 45242349Sbapt#include <grp.h> 46242349Sbapt#include <libutil.h> 4720253Sjoerg#include "pw.h" 4820253Sjoerg#include "bitmap.h" 4920253Sjoerg 5023318Sache#define LOGNAMESIZE (MAXLOGNAME-1) 5122394Sdavidn 5252512Sdavidnstatic char locked_str[] = "*LOCKED*"; 5324214Sache 54284111Sbaptstatic int delete_user(struct userconf *cnf, struct passwd *pwd, 55284128Sbapt char *name, int delete, int mode); 56284124Sbaptstatic int print_user(struct passwd * pwd); 57284133Sbaptstatic uid_t pw_uidpolicy(struct userconf * cnf, long id); 58284118Sbaptstatic uid_t pw_gidpolicy(struct cargs * args, char *nam, gid_t prefer); 5920253Sjoergstatic time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 6020253Sjoergstatic time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 6120253Sjoergstatic char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 6220253Sjoergstatic char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 6320253Sjoergstatic char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 6420253Sjoergstatic char *shell_path(char const * path, char *shells[], char *sh); 6520253Sjoergstatic void rmat(uid_t uid); 6685145Sachestatic void rmopie(char const * name); 6720253Sjoerg 68283961Sbaptstatic void 69284118Sbaptcreate_and_populate_homedir(int mode, struct passwd *pwd) 70283961Sbapt{ 71283961Sbapt char *homedir, *dotdir; 72284118Sbapt struct userconf *cnf = conf.userconf; 73283961Sbapt 74283961Sbapt homedir = dotdir = NULL; 75283961Sbapt 76284118Sbapt if (conf.rootdir[0] != '\0') { 77284118Sbapt asprintf(&homedir, "%s/%s", conf.rootdir, pwd->pw_dir); 78283961Sbapt if (homedir == NULL) 79283961Sbapt errx(EX_OSERR, "out of memory"); 80284118Sbapt asprintf(&dotdir, "%s/%s", conf.rootdir, cnf->dotdir); 81283961Sbapt } 82283961Sbapt 83283961Sbapt copymkdir(homedir ? homedir : pwd->pw_dir, dotdir ? dotdir: cnf->dotdir, 84283961Sbapt cnf->homemode, pwd->pw_uid, pwd->pw_gid); 85283961Sbapt pw_log(cnf, mode, W_USER, "%s(%u) home %s made", pwd->pw_name, 86283961Sbapt pwd->pw_uid, pwd->pw_dir); 87283961Sbapt} 88283961Sbapt 89285133Sbaptstatic int 90285133Sbaptset_passwd(struct passwd *pwd, struct carg *arg, bool update) 91285133Sbapt{ 92285133Sbapt int b, istty; 93285133Sbapt struct termios t, n; 94285133Sbapt login_cap_t *lc; 95285133Sbapt char line[_PASSWORD_LEN+1]; 96285133Sbapt char *p; 97285133Sbapt 98285133Sbapt if (conf.fd == '-') { 99285133Sbapt if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 100285133Sbapt pwd->pw_passwd = "*"; /* No access */ 101285133Sbapt return (1); 102285133Sbapt } 103285133Sbapt return (0); 104285133Sbapt } 105285133Sbapt 106285133Sbapt if ((istty = isatty(conf.fd))) { 107285133Sbapt if (tcgetattr(conf.fd, &t) == -1) 108285133Sbapt istty = 0; 109285133Sbapt else { 110285133Sbapt n.c_lflag &= ~(ECHO); 111285133Sbapt tcsetattr(conf.fd, TCSANOW, &n); 112285133Sbapt printf("%s%spassword for user %s:", 113285133Sbapt update ? "new " : "", 114285133Sbapt conf.precrypted ? "encrypted " : "", 115285133Sbapt pwd->pw_name); 116285133Sbapt fflush(stdout); 117285133Sbapt } 118285133Sbapt } 119285133Sbapt b = read(conf.fd, line, sizeof(line) - 1); 120285133Sbapt if (istty) { /* Restore state */ 121285133Sbapt tcsetattr(conf.fd, TCSANOW, &t); 122285133Sbapt fputc('\n', stdout); 123285133Sbapt fflush(stdout); 124285133Sbapt } 125285133Sbapt 126285133Sbapt if (b < 0) 127285133Sbapt err(EX_IOERR, "-%c file descriptor", 128285133Sbapt conf.precrypted ? 'H' : 'h'); 129285133Sbapt line[b] = '\0'; 130285133Sbapt if ((p = strpbrk(line, "\r\n")) != NULL) 131285133Sbapt *p = '\0'; 132285133Sbapt if (!*line) 133285133Sbapt errx(EX_DATAERR, "empty password read on file descriptor %d", 134285133Sbapt conf.fd); 135285133Sbapt if (conf.precrypted) { 136285133Sbapt if (strchr(line, ':') != NULL) 137285133Sbapt return EX_DATAERR; 138285133Sbapt pwd->pw_passwd = line; 139285133Sbapt } else { 140285133Sbapt lc = login_getpwclass(pwd); 141285133Sbapt if (lc == NULL || 142285133Sbapt login_setcryptfmt(lc, "sha512", NULL) == NULL) 143285133Sbapt warn("setting crypt(3) format"); 144285133Sbapt login_close(lc); 145285133Sbapt pwd->pw_passwd = pw_pwcrypt(line); 146285133Sbapt } 147285133Sbapt return (1); 148285133Sbapt} 149285133Sbapt 15020253Sjoerg/*- 15120253Sjoerg * -C config configuration file 15220253Sjoerg * -q quiet operation 15320253Sjoerg * -n name login name 15420253Sjoerg * -u uid user id 15520253Sjoerg * -c comment user name/comment 15620253Sjoerg * -d directory home directory 15720253Sjoerg * -e date account expiry date 15820253Sjoerg * -p date password expiry date 15920253Sjoerg * -g grp primary group 16020253Sjoerg * -G grp1,grp2 additional groups 16120253Sjoerg * -m [ -k dir ] create and set up home 16220253Sjoerg * -s shell name of login shell 16320253Sjoerg * -o duplicate uid ok 16420253Sjoerg * -L class user class 16520253Sjoerg * -l name new login name 16620253Sjoerg * -h fd password filehandle 167124382Siedowse * -H fd encrypted password filehandle 16820253Sjoerg * -F force print or add 16920253Sjoerg * Setting defaults: 17020253Sjoerg * -D set user defaults 17120253Sjoerg * -b dir default home root dir 17220253Sjoerg * -e period default expiry period 17320253Sjoerg * -p period default password change period 17420253Sjoerg * -g group default group 17520253Sjoerg * -G grp1,grp2.. default additional groups 17620253Sjoerg * -L class default login class 17720253Sjoerg * -k dir default home skeleton 17820253Sjoerg * -s shell default shell 17920253Sjoerg * -w method default password method 18020253Sjoerg */ 18120253Sjoerg 18220253Sjoergint 183284128Sbaptpw_user(int mode, char *name, long id, struct cargs * args) 18420253Sjoerg{ 18552527Sdavidn int rc, edited = 0; 18620253Sjoerg char *p = NULL; 18752512Sdavidn char *passtmp; 18820253Sjoerg struct carg *arg; 18920253Sjoerg struct passwd *pwd = NULL; 19020253Sjoerg struct group *grp; 19120253Sjoerg struct stat st; 192284118Sbapt struct userconf *cnf; 19320747Sdavidn char line[_PASSWORD_LEN+1]; 194283961Sbapt char path[MAXPATHLEN]; 19582868Sdd FILE *fp; 196167919Sle char *dmode_c; 197167919Sle void *set = NULL; 19820253Sjoerg 19920253Sjoerg static struct passwd fakeuser = 20020253Sjoerg { 20120253Sjoerg NULL, 20220253Sjoerg "*", 20320253Sjoerg -1, 20420253Sjoerg -1, 20520253Sjoerg 0, 20620253Sjoerg "", 20720253Sjoerg "User &", 20856000Sdavidn "/nonexistent", 20920253Sjoerg "/bin/sh", 21020253Sjoerg 0 21156000Sdavidn#if defined(__FreeBSD__) 21256000Sdavidn ,0 21356000Sdavidn#endif 21420253Sjoerg }; 21520253Sjoerg 216284118Sbapt cnf = conf.userconf; 21752512Sdavidn 21820253Sjoerg /* 21920267Sjoerg * With M_NEXT, we only need to return the 22020267Sjoerg * next uid to stdout 22120267Sjoerg */ 222284149Sbapt if (mode == M_NEXT) 223284149Sbapt { 224284149Sbapt uid_t next = pw_uidpolicy(cnf, id); 225284149Sbapt if (getarg(args, 'q')) 226284149Sbapt return next; 227284149Sbapt printf("%u:", next); 228284128Sbapt pw_group(mode, name, -1, args); 229284149Sbapt return EXIT_SUCCESS; 23020267Sjoerg } 23120267Sjoerg 23220267Sjoerg /* 23320253Sjoerg * We can do all of the common legwork here 23420253Sjoerg */ 23520253Sjoerg 23620253Sjoerg if ((arg = getarg(args, 'b')) != NULL) { 23720267Sjoerg cnf->home = arg->val; 23820253Sjoerg } 23921052Sdavidn 240167919Sle if ((arg = getarg(args, 'M')) != NULL) { 241167919Sle dmode_c = arg->val; 242167919Sle if ((set = setmode(dmode_c)) == NULL) 243167919Sle errx(EX_DATAERR, "invalid directory creation mode '%s'", 244167919Sle dmode_c); 245219408Sjkim cnf->homemode = getmode(set, _DEF_DIRMODE); 246167919Sle free(set); 247168044Sle } 248167919Sle 24921052Sdavidn /* 25021052Sdavidn * If we'll need to use it or we're updating it, 25121052Sdavidn * then create the base home directory if necessary 25221052Sdavidn */ 253224535Sdelphij if (arg != NULL || getarg(args, 'm') != NULL) { 25421052Sdavidn int l = strlen(cnf->home); 25521052Sdavidn 25621052Sdavidn if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 25721052Sdavidn cnf->home[--l] = '\0'; 25821052Sdavidn 25921052Sdavidn if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 26030259Scharnier errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); 26121052Sdavidn 26221052Sdavidn if (stat(cnf->home, &st) == -1) { 26321052Sdavidn char dbuf[MAXPATHLEN]; 26421052Sdavidn 26521242Sdavidn /* 26621242Sdavidn * This is a kludge especially for Joerg :) 26721242Sdavidn * If the home directory would be created in the root partition, then 26821242Sdavidn * we really create it under /usr which is likely to have more space. 26921242Sdavidn * But we create a symlink from cnf->home -> "/usr" -> cnf->home 27021242Sdavidn */ 27121242Sdavidn if (strchr(cnf->home+1, '/') == NULL) { 272282683Sbapt snprintf(dbuf, MAXPATHLEN, "/usr%s", cnf->home); 273219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) { 27421242Sdavidn chown(dbuf, 0, 0); 275148584Spjd /* 276148584Spjd * Skip first "/" and create symlink: 277148584Spjd * /home -> usr/home 278148584Spjd */ 279148584Spjd symlink(dbuf+1, cnf->home); 28021242Sdavidn } 28121242Sdavidn /* If this falls, fall back to old method */ 28221242Sdavidn } 283130633Srobert strlcpy(dbuf, cnf->home, sizeof(dbuf)); 284130633Srobert p = dbuf; 28521242Sdavidn if (stat(dbuf, &st) == -1) { 286252377Skientzle while ((p = strchr(p + 1, '/')) != NULL) { 28721242Sdavidn *p = '\0'; 28821242Sdavidn if (stat(dbuf, &st) == -1) { 289219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) == -1) 29021242Sdavidn goto direrr; 29121242Sdavidn chown(dbuf, 0, 0); 29221242Sdavidn } else if (!S_ISDIR(st.st_mode)) 29330259Scharnier errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); 29421242Sdavidn *p = '/'; 29521242Sdavidn } 29621052Sdavidn } 29721242Sdavidn if (stat(dbuf, &st) == -1) { 298219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) == -1) { 29930259Scharnier direrr: err(EX_OSFILE, "mkdir '%s'", dbuf); 30021052Sdavidn } 30121052Sdavidn chown(dbuf, 0, 0); 30221052Sdavidn } 30321052Sdavidn } else if (!S_ISDIR(st.st_mode)) 30430259Scharnier errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); 30521052Sdavidn } 30621052Sdavidn 30720253Sjoerg if ((arg = getarg(args, 'e')) != NULL) 30820253Sjoerg cnf->expire_days = atoi(arg->val); 30920253Sjoerg 31021330Sdavidn if ((arg = getarg(args, 'y')) != NULL) 31121330Sdavidn cnf->nispasswd = arg->val; 31221330Sdavidn 31320253Sjoerg if ((arg = getarg(args, 'p')) != NULL && arg->val) 31420253Sjoerg cnf->password_days = atoi(arg->val); 31520253Sjoerg 31620253Sjoerg if ((arg = getarg(args, 'g')) != NULL) { 31763596Sdavidn if (!*(p = arg->val)) /* Handle empty group list specially */ 31863596Sdavidn cnf->default_group = ""; 31963596Sdavidn else { 32063596Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 32163596Sdavidn if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 32263596Sdavidn errx(EX_NOUSER, "group `%s' does not exist", p); 32363596Sdavidn } 32463596Sdavidn cnf->default_group = newstr(grp->gr_name); 32520253Sjoerg } 32620253Sjoerg } 32720253Sjoerg if ((arg = getarg(args, 'L')) != NULL) 328284110Sbapt cnf->default_class = pw_checkname(arg->val, 0); 32920253Sjoerg 33020253Sjoerg if ((arg = getarg(args, 'G')) != NULL && arg->val) { 33152527Sdavidn int i = 0; 33220253Sjoerg 33320747Sdavidn for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 33444229Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 33561957Sache if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 33630259Scharnier errx(EX_NOUSER, "group `%s' does not exist", p); 33720253Sjoerg } 33820747Sdavidn if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 33920747Sdavidn cnf->groups[i++] = newstr(grp->gr_name); 34020253Sjoerg } 34120747Sdavidn while (i < cnf->numgroups) 34220253Sjoerg cnf->groups[i++] = NULL; 34320253Sjoerg } 34452527Sdavidn 34520253Sjoerg if ((arg = getarg(args, 'k')) != NULL) { 34626088Sdavidn if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode)) 34730259Scharnier errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir); 34820253Sjoerg } 34952527Sdavidn 35020253Sjoerg if ((arg = getarg(args, 's')) != NULL) 35120253Sjoerg cnf->shell_default = arg->val; 35220253Sjoerg 35363600Sdavidn if ((arg = getarg(args, 'w')) != NULL) 35463600Sdavidn cnf->default_password = boolean_val(arg->val, cnf->default_password); 35520253Sjoerg if (mode == M_ADD && getarg(args, 'D')) { 356284135Sbapt if (name != NULL) 35730259Scharnier errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 35820253Sjoerg if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 35920253Sjoerg if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 36020253Sjoerg cnf->min_uid = 1000; 36120253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 36220253Sjoerg cnf->max_uid = 32000; 36320253Sjoerg } 36420253Sjoerg if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 36520253Sjoerg if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 36620253Sjoerg cnf->min_gid = 1000; 36720253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 36820253Sjoerg cnf->max_gid = 32000; 36920253Sjoerg } 37020253Sjoerg 371284135Sbapt if (write_userconfig(conf.config)) 372284135Sbapt return (EXIT_SUCCESS); 373283814Sbapt err(EX_IOERR, "config udpate"); 37420253Sjoerg } 37552527Sdavidn 37620253Sjoerg if (mode == M_PRINT && getarg(args, 'a')) { 37744229Sdavidn SETPWENT(); 37844229Sdavidn while ((pwd = GETPWENT()) != NULL) 379284124Sbapt print_user(pwd); 38044229Sdavidn ENDPWENT(); 38120267Sjoerg return EXIT_SUCCESS; 38220253Sjoerg } 38352527Sdavidn 384284128Sbapt if (name != NULL) 385284128Sbapt pwd = GETPWNAM(pw_checkname(name, 0)); 38620253Sjoerg 387284128Sbapt if (id < 0 && name == NULL) 388284128Sbapt errx(EX_DATAERR, "user name or id required"); 38920253Sjoerg 39020253Sjoerg /* 39120253Sjoerg * Update, delete & print require that the user exists 39220253Sjoerg */ 39352512Sdavidn if (mode == M_UPDATE || mode == M_DELETE || 39452512Sdavidn mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) { 39552527Sdavidn 396284128Sbapt if (name == NULL && pwd == NULL) /* Try harder */ 397284128Sbapt pwd = GETPWUID(id); 39820253Sjoerg 39920253Sjoerg if (pwd == NULL) { 40020253Sjoerg if (mode == M_PRINT && getarg(args, 'F')) { 401284128Sbapt fakeuser.pw_name = name ? name : "nouser"; 402284128Sbapt fakeuser.pw_uid = (uid_t) id; 403284126Sbapt return print_user(&fakeuser); 40420253Sjoerg } 405284128Sbapt if (name == NULL) 406284128Sbapt errx(EX_NOUSER, "no such uid `%ld'", id); 407284128Sbapt errx(EX_NOUSER, "no such user `%s'", name); 40820253Sjoerg } 40952527Sdavidn 410284128Sbapt if (name == NULL) 411284128Sbapt name = pwd->pw_name; 41220253Sjoerg 41320253Sjoerg /* 41452512Sdavidn * The M_LOCK and M_UNLOCK functions simply add or remove 41552512Sdavidn * a "*LOCKED*" prefix from in front of the password to 41652512Sdavidn * prevent it decoding correctly, and therefore prevents 41752512Sdavidn * access. Of course, this only prevents access via 41852512Sdavidn * password authentication (not ssh, kerberos or any 41952512Sdavidn * other method that does not use the UNIX password) but 42052512Sdavidn * that is a known limitation. 42152512Sdavidn */ 42252512Sdavidn 42352512Sdavidn if (mode == M_LOCK) { 42452512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0) 42552512Sdavidn errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 426282685Sbapt asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd); 42752512Sdavidn if (passtmp == NULL) /* disaster */ 42852512Sdavidn errx(EX_UNAVAILABLE, "out of memory"); 42952512Sdavidn pwd->pw_passwd = passtmp; 43052527Sdavidn edited = 1; 43152512Sdavidn } else if (mode == M_UNLOCK) { 43252512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0) 43352512Sdavidn errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 43452512Sdavidn pwd->pw_passwd += sizeof(locked_str)-1; 43552527Sdavidn edited = 1; 436284111Sbapt } else if (mode == M_DELETE) 437284128Sbapt return (delete_user(cnf, pwd, name, 438284111Sbapt getarg(args, 'r') != NULL, mode)); 439284111Sbapt else if (mode == M_PRINT) 440284126Sbapt return print_user(pwd); 44120253Sjoerg 44220253Sjoerg /* 44320253Sjoerg * The rest is edit code 44420253Sjoerg */ 445284129Sbapt if (conf.newname != NULL) { 44620253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 44730259Scharnier errx(EX_DATAERR, "can't rename `root' account"); 448284129Sbapt pwd->pw_name = pw_checkname(conf.newname, 0); 44952527Sdavidn edited = 1; 45020253Sjoerg } 45152527Sdavidn 452284133Sbapt if (id > 0 && isdigit((unsigned char)*arg->val)) { 453284133Sbapt pwd->pw_uid = (uid_t)id; 45452527Sdavidn edited = 1; 45520253Sjoerg if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 45630259Scharnier errx(EX_DATAERR, "can't change uid of `root' account"); 45720253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 45830259Scharnier warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 45920253Sjoerg } 46020253Sjoerg 46152527Sdavidn if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */ 46252527Sdavidn gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid; 46352527Sdavidn if (newgid != pwd->pw_gid) { 46452527Sdavidn edited = 1; 46561762Sdavidn pwd->pw_gid = newgid; 46652527Sdavidn } 46752527Sdavidn } 46852527Sdavidn 46920253Sjoerg if ((arg = getarg(args, 'p')) != NULL) { 47052527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 47152527Sdavidn if (pwd->pw_change != 0) { 47252527Sdavidn pwd->pw_change = 0; 47352527Sdavidn edited = 1; 47452527Sdavidn } 47552527Sdavidn } 47620253Sjoerg else { 47720253Sjoerg time_t now = time(NULL); 47820253Sjoerg time_t expire = parse_date(now, arg->val); 47920253Sjoerg 48052527Sdavidn if (pwd->pw_change != expire) { 48152527Sdavidn pwd->pw_change = expire; 48252527Sdavidn edited = 1; 48352527Sdavidn } 48420253Sjoerg } 48520253Sjoerg } 48652527Sdavidn 48720267Sjoerg if ((arg = getarg(args, 'e')) != NULL) { 48852527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 48952527Sdavidn if (pwd->pw_expire != 0) { 49052527Sdavidn pwd->pw_expire = 0; 49152527Sdavidn edited = 1; 49252527Sdavidn } 49352527Sdavidn } 49420253Sjoerg else { 49520253Sjoerg time_t now = time(NULL); 49620253Sjoerg time_t expire = parse_date(now, arg->val); 49720253Sjoerg 49852527Sdavidn if (pwd->pw_expire != expire) { 49952527Sdavidn pwd->pw_expire = expire; 50052527Sdavidn edited = 1; 50152527Sdavidn } 50220253Sjoerg } 50320253Sjoerg } 50420253Sjoerg 50552527Sdavidn if ((arg = getarg(args, 's')) != NULL) { 50652527Sdavidn char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 50752527Sdavidn if (shell == NULL) 50852527Sdavidn shell = ""; 50952527Sdavidn if (strcmp(shell, pwd->pw_shell) != 0) { 51052527Sdavidn pwd->pw_shell = shell; 51152527Sdavidn edited = 1; 51252527Sdavidn } 51352527Sdavidn } 51420253Sjoerg 51552527Sdavidn if (getarg(args, 'L')) { 51652527Sdavidn if (cnf->default_class == NULL) 51752527Sdavidn cnf->default_class = ""; 51852527Sdavidn if (strcmp(pwd->pw_class, cnf->default_class) != 0) { 51952527Sdavidn pwd->pw_class = cnf->default_class; 52052527Sdavidn edited = 1; 52152527Sdavidn } 52252527Sdavidn } 52352527Sdavidn 52420747Sdavidn if ((arg = getarg(args, 'd')) != NULL) { 525130629Srobert if (strcmp(pwd->pw_dir, arg->val)) 526130629Srobert edited = 1; 52720747Sdavidn if (stat(pwd->pw_dir = arg->val, &st) == -1) { 52820747Sdavidn if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 52930259Scharnier warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 53020747Sdavidn } else if (!S_ISDIR(st.st_mode)) 53130259Scharnier warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 53220747Sdavidn } 53320747Sdavidn 534124382Siedowse if ((arg = getarg(args, 'w')) != NULL && 535124382Siedowse getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) { 53664918Sgreen login_cap_t *lc; 53764918Sgreen 53864918Sgreen lc = login_getpwclass(pwd); 53964918Sgreen if (lc == NULL || 540252688Sdes login_setcryptfmt(lc, "sha512", NULL) == NULL) 54164918Sgreen warn("setting crypt(3) format"); 54264918Sgreen login_close(lc); 54320267Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 54452527Sdavidn edited = 1; 54552527Sdavidn } 54620267Sjoerg 54720253Sjoerg } else { 54864918Sgreen login_cap_t *lc; 54952527Sdavidn 55052527Sdavidn /* 55152527Sdavidn * Add code 55252527Sdavidn */ 55352527Sdavidn 554284128Sbapt if (name == NULL) /* Required */ 55530259Scharnier errx(EX_DATAERR, "login name required"); 556284128Sbapt else if ((pwd = GETPWNAM(name)) != NULL) /* Exists */ 557284128Sbapt errx(EX_DATAERR, "login name `%s' already exists", name); 55820253Sjoerg 55920253Sjoerg /* 56020253Sjoerg * Now, set up defaults for a new user 56120253Sjoerg */ 56220253Sjoerg pwd = &fakeuser; 563284128Sbapt pwd->pw_name = name; 56420253Sjoerg pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 565284133Sbapt pwd->pw_uid = pw_uidpolicy(cnf, id); 566284118Sbapt pwd->pw_gid = pw_gidpolicy(args, pwd->pw_name, (gid_t) pwd->pw_uid); 56720253Sjoerg pwd->pw_change = pw_pwdpolicy(cnf, args); 56820253Sjoerg pwd->pw_expire = pw_exppolicy(cnf, args); 56920253Sjoerg pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 57020253Sjoerg pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 57164918Sgreen lc = login_getpwclass(pwd); 572272833Sdes if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) 57364918Sgreen warn("setting crypt(3) format"); 57464918Sgreen login_close(lc); 57564918Sgreen pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 57652527Sdavidn edited = 1; 57720253Sjoerg 57820253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 57930259Scharnier warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 58020253Sjoerg } 58120253Sjoerg 58220253Sjoerg /* 58320253Sjoerg * Shared add/edit code 58420253Sjoerg */ 58552527Sdavidn if ((arg = getarg(args, 'c')) != NULL) { 586284110Sbapt char *gecos = pw_checkname(arg->val, 1); 58752527Sdavidn if (strcmp(pwd->pw_gecos, gecos) != 0) { 58852527Sdavidn pwd->pw_gecos = gecos; 58952527Sdavidn edited = 1; 59052527Sdavidn } 59152527Sdavidn } 59220253Sjoerg 593285133Sbapt if (conf.fd != -1) 594285133Sbapt edited = set_passwd(pwd, arg, mode == M_UPDATE); 59520253Sjoerg 59620267Sjoerg /* 59720267Sjoerg * Special case: -N only displays & exits 59820267Sjoerg */ 599284121Sbapt if (conf.dryrun) 600284126Sbapt return print_user(pwd); 60120267Sjoerg 60221330Sdavidn if (mode == M_ADD) { 60352527Sdavidn edited = 1; /* Always */ 60452502Sdavidn rc = addpwent(pwd); 605283814Sbapt if (rc == -1) 606283814Sbapt errx(EX_IOERR, "user '%s' already exists", 607283814Sbapt pwd->pw_name); 608283814Sbapt else if (rc != 0) 609283814Sbapt err(EX_IOERR, "passwd file update"); 61052502Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/') { 61152502Sdavidn rc = addnispwent(cnf->nispasswd, pwd); 61252502Sdavidn if (rc == -1) 61352502Sdavidn warnx("User '%s' already exists in NIS passwd", pwd->pw_name); 61452502Sdavidn else 61556000Sdavidn warn("NIS passwd update"); 61652502Sdavidn /* NOTE: we treat NIS-only update errors as non-fatal */ 61752502Sdavidn } 61852512Sdavidn } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) { 61952527Sdavidn if (edited) { /* Only updated this if required */ 620284128Sbapt rc = chgpwent(name, pwd); 621283814Sbapt if (rc == -1) 622283814Sbapt errx(EX_IOERR, "user '%s' does not exist (NIS?)", pwd->pw_name); 623283814Sbapt else if (rc != 0) 624283814Sbapt err(EX_IOERR, "passwd file update"); 62552527Sdavidn if ( cnf->nispasswd && *cnf->nispasswd=='/') { 626284128Sbapt rc = chgnispwent(cnf->nispasswd, name, pwd); 62752527Sdavidn if (rc == -1) 62852527Sdavidn warn("User '%s' not found in NIS passwd", pwd->pw_name); 62952527Sdavidn else 63056000Sdavidn warn("NIS passwd update"); 63152527Sdavidn /* NOTE: NIS-only update errors are not fatal */ 63252527Sdavidn } 63352502Sdavidn } 63421330Sdavidn } 63521330Sdavidn 63620253Sjoerg /* 63720253Sjoerg * Ok, user is created or changed - now edit group file 63820253Sjoerg */ 63920253Sjoerg 640242349Sbapt if (mode == M_ADD || getarg(args, 'G') != NULL) { 641273779Sbapt int i, j; 642273779Sbapt /* First remove the user from all group */ 643273779Sbapt SETGRENT(); 644273779Sbapt while ((grp = GETGRENT()) != NULL) { 645273779Sbapt char group[MAXLOGNAME]; 646273779Sbapt if (grp->gr_mem == NULL) 647273779Sbapt continue; 648273779Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 649273779Sbapt if (strcmp(grp->gr_mem[i] , pwd->pw_name) != 0) 650273779Sbapt continue; 651273779Sbapt for (j = i; grp->gr_mem[j] != NULL ; j++) 652273779Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 653273779Sbapt strlcpy(group, grp->gr_name, MAXLOGNAME); 654273779Sbapt chggrent(group, grp); 655273779Sbapt } 656273779Sbapt } 657273779Sbapt ENDGRENT(); 658273779Sbapt 659273779Sbapt /* now add to group where needed */ 660242349Sbapt for (i = 0; cnf->groups[i] != NULL; i++) { 661242349Sbapt grp = GETGRNAM(cnf->groups[i]); 662244737Sbapt grp = gr_add(grp, pwd->pw_name); 663244737Sbapt /* 664244737Sbapt * grp can only be NULL in 2 cases: 665244737Sbapt * - the new member is already a member 666244737Sbapt * - a problem with memory occurs 667244737Sbapt * in both cases we want to skip now. 668244737Sbapt */ 669244737Sbapt if (grp == NULL) 670242349Sbapt continue; 671242349Sbapt chggrent(cnf->groups[i], grp); 672245114Smjg free(grp); 673242349Sbapt } 674242349Sbapt } 675242349Sbapt 676242349Sbapt 67761759Sdavidn /* go get a current version of pwd */ 678284128Sbapt pwd = GETPWNAM(name); 67961759Sdavidn if (pwd == NULL) { 68061759Sdavidn /* This will fail when we rename, so special case that */ 681284129Sbapt if (mode == M_UPDATE && conf.newname != NULL) { 682284129Sbapt name = conf.newname; /* update new name */ 683284128Sbapt pwd = GETPWNAM(name); /* refetch renamed rec */ 68461759Sdavidn } 68561759Sdavidn } 68661759Sdavidn if (pwd == NULL) /* can't go on without this */ 687284128Sbapt errx(EX_NOUSER, "user '%s' disappeared during update", name); 68820253Sjoerg 68944229Sdavidn grp = GETGRGID(pwd->pw_gid); 690283842Sbapt pw_log(cnf, mode, W_USER, "%s(%u):%s(%u):%s:%s:%s", 691283842Sbapt pwd->pw_name, pwd->pw_uid, 692283842Sbapt grp ? grp->gr_name : "unknown", (grp ? grp->gr_gid : (uid_t)-1), 69320253Sjoerg pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 69420253Sjoerg 69520253Sjoerg /* 69620253Sjoerg * If adding, let's touch and chown the user's mail file. This is not 69720253Sjoerg * strictly necessary under BSD with a 0755 maildir but it also 69820253Sjoerg * doesn't hurt anything to create the empty mailfile 69920253Sjoerg */ 70020253Sjoerg if (mode == M_ADD) { 701283961Sbapt if (PWALTDIR() != PWF_ALT) { 702283961Sbapt arg = getarg(args, 'R'); 703283961Sbapt snprintf(path, sizeof(path), "%s%s/%s", 704283961Sbapt arg ? arg->val : "", _PATH_MAILDIR, pwd->pw_name); 705283961Sbapt close(open(path, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 70644229Sdavidn * mtime */ 707283961Sbapt chown(path, pwd->pw_uid, pwd->pw_gid); 70820253Sjoerg } 70920253Sjoerg } 71052527Sdavidn 71120253Sjoerg /* 71282868Sdd * Let's create and populate the user's home directory. Note 71320253Sjoerg * that this also `works' for editing users if -m is used, but 71420253Sjoerg * existing files will *not* be overwritten. 71520253Sjoerg */ 716283961Sbapt if (PWALTDIR() != PWF_ALT && getarg(args, 'm') != NULL && pwd->pw_dir && 717283961Sbapt *pwd->pw_dir == '/' && pwd->pw_dir[1]) 718284118Sbapt create_and_populate_homedir(mode, pwd); 71952527Sdavidn 72082868Sdd /* 72182868Sdd * Finally, send mail to the new user as well, if we are asked to 72282868Sdd */ 72382868Sdd if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 72482868Sdd FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 72582868Sdd 72682868Sdd if (pfp == NULL) 72782868Sdd warn("sendmail"); 72882868Sdd else { 72982868Sdd fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 73082868Sdd while (fgets(line, sizeof(line), fp) != NULL) { 73182868Sdd /* Do substitutions? */ 73282868Sdd fputs(line, pfp); 73382868Sdd } 73482868Sdd pclose(pfp); 735283842Sbapt pw_log(cnf, mode, W_USER, "%s(%u) new user mail sent", 736283842Sbapt pwd->pw_name, pwd->pw_uid); 73782868Sdd } 73882868Sdd fclose(fp); 73982868Sdd } 74082868Sdd 74120267Sjoerg return EXIT_SUCCESS; 74220253Sjoerg} 74320253Sjoerg 74420253Sjoerg 74520253Sjoergstatic uid_t 746284133Sbaptpw_uidpolicy(struct userconf * cnf, long id) 74720253Sjoerg{ 74820253Sjoerg struct passwd *pwd; 74920253Sjoerg uid_t uid = (uid_t) - 1; 75020253Sjoerg 75120253Sjoerg /* 75220253Sjoerg * Check the given uid, if any 75320253Sjoerg */ 754284133Sbapt if (id > 0) { 755284133Sbapt uid = (uid_t) id; 75620253Sjoerg 757284133Sbapt if ((pwd = GETPWUID(uid)) != NULL && conf.checkduplicate) 758283842Sbapt errx(EX_DATAERR, "uid `%u' has already been allocated", pwd->pw_uid); 75920253Sjoerg } else { 76020253Sjoerg struct bitmap bm; 76120253Sjoerg 76220253Sjoerg /* 76320253Sjoerg * We need to allocate the next available uid under one of 76420253Sjoerg * two policies a) Grab the first unused uid b) Grab the 76520253Sjoerg * highest possible unused uid 76620253Sjoerg */ 76720253Sjoerg if (cnf->min_uid >= cnf->max_uid) { /* Sanity 76820253Sjoerg * claus^H^H^H^Hheck */ 76920253Sjoerg cnf->min_uid = 1000; 77020253Sjoerg cnf->max_uid = 32000; 77120253Sjoerg } 77220253Sjoerg bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 77320253Sjoerg 77420253Sjoerg /* 77520253Sjoerg * Now, let's fill the bitmap from the password file 77620253Sjoerg */ 77744229Sdavidn SETPWENT(); 77844229Sdavidn while ((pwd = GETPWENT()) != NULL) 77944229Sdavidn if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 78020253Sjoerg bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 78144229Sdavidn ENDPWENT(); 78220253Sjoerg 78320253Sjoerg /* 78420253Sjoerg * Then apply the policy, with fallback to reuse if necessary 78520253Sjoerg */ 78620253Sjoerg if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 78720253Sjoerg uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 78820253Sjoerg 78920253Sjoerg /* 79020253Sjoerg * Another sanity check 79120253Sjoerg */ 79220253Sjoerg if (uid < cnf->min_uid || uid > cnf->max_uid) 79330259Scharnier errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 79420253Sjoerg bm_dealloc(&bm); 79520253Sjoerg } 79620253Sjoerg return uid; 79720253Sjoerg} 79820253Sjoerg 79920253Sjoerg 80020253Sjoergstatic uid_t 801284118Sbaptpw_gidpolicy(struct cargs * args, char *nam, gid_t prefer) 80220253Sjoerg{ 80320253Sjoerg struct group *grp; 80420253Sjoerg gid_t gid = (uid_t) - 1; 80520253Sjoerg struct carg *a_gid = getarg(args, 'g'); 806284118Sbapt struct userconf *cnf = conf.userconf; 80720253Sjoerg 80820253Sjoerg /* 80920253Sjoerg * If no arg given, see if default can help out 81020253Sjoerg */ 81120253Sjoerg if (a_gid == NULL && cnf->default_group && *cnf->default_group) 81220253Sjoerg a_gid = addarg(args, 'g', cnf->default_group); 81320253Sjoerg 81420253Sjoerg /* 81520253Sjoerg * Check the given gid, if any 81620253Sjoerg */ 81744229Sdavidn SETGRENT(); 81820253Sjoerg if (a_gid != NULL) { 81944229Sdavidn if ((grp = GETGRNAM(a_gid->val)) == NULL) { 82020253Sjoerg gid = (gid_t) atol(a_gid->val); 82161957Sache if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL) 82230259Scharnier errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 82320253Sjoerg } 82420253Sjoerg gid = grp->gr_gid; 825262865Sjulian } else if ((grp = GETGRNAM(nam)) != NULL && 826262865Sjulian (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) { 82720267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 82820253Sjoerg } else { 82920253Sjoerg struct cargs grpargs; 83020253Sjoerg char tmp[32]; 83120253Sjoerg 83220253Sjoerg LIST_INIT(&grpargs); 83320253Sjoerg 83420253Sjoerg /* 83520253Sjoerg * We need to auto-create a group with the user's name. We 83620253Sjoerg * can send all the appropriate output to our sister routine 83720253Sjoerg * bit first see if we can create a group with gid==uid so we 83820253Sjoerg * can keep the user and group ids in sync. We purposely do 83920253Sjoerg * NOT check the gid range if we can force the sync. If the 84020253Sjoerg * user's name dups an existing group, then the group add 84120253Sjoerg * function will happily handle that case for us and exit. 84220253Sjoerg */ 84344229Sdavidn if (GETGRGID(prefer) == NULL) { 844282700Sbapt snprintf(tmp, sizeof(tmp), "%u", prefer); 84520253Sjoerg addarg(&grpargs, 'g', tmp); 84620253Sjoerg } 847284121Sbapt if (conf.dryrun) { 84820267Sjoerg addarg(&grpargs, 'q', NULL); 849284128Sbapt gid = pw_group(M_NEXT, nam, -1, &grpargs); 85020267Sjoerg } 85120267Sjoerg else 85220267Sjoerg { 853284128Sbapt pw_group(M_ADD, nam, -1, &grpargs); 85444229Sdavidn if ((grp = GETGRNAM(nam)) != NULL) 85520267Sjoerg gid = grp->gr_gid; 85620267Sjoerg } 85770486Sben a_gid = LIST_FIRST(&grpargs); 85820253Sjoerg while (a_gid != NULL) { 85970486Sben struct carg *t = LIST_NEXT(a_gid, list); 86020253Sjoerg LIST_REMOVE(a_gid, list); 86120253Sjoerg a_gid = t; 86220253Sjoerg } 86320253Sjoerg } 86444229Sdavidn ENDGRENT(); 86520253Sjoerg return gid; 86620253Sjoerg} 86720253Sjoerg 86820253Sjoerg 86920253Sjoergstatic time_t 87020253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args) 87120253Sjoerg{ 87220253Sjoerg time_t result = 0; 87320253Sjoerg time_t now = time(NULL); 87427831Sdavidn struct carg *arg = getarg(args, 'p'); 87520253Sjoerg 87620253Sjoerg if (arg != NULL) { 87720253Sjoerg if ((result = parse_date(now, arg->val)) == now) 87830259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 87920253Sjoerg } else if (cnf->password_days > 0) 88020253Sjoerg result = now + ((long) cnf->password_days * 86400L); 88120253Sjoerg return result; 88220253Sjoerg} 88320253Sjoerg 88420253Sjoerg 88520253Sjoergstatic time_t 88620253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args) 88720253Sjoerg{ 88820253Sjoerg time_t result = 0; 88920253Sjoerg time_t now = time(NULL); 89020253Sjoerg struct carg *arg = getarg(args, 'e'); 89120253Sjoerg 89220253Sjoerg if (arg != NULL) { 89320253Sjoerg if ((result = parse_date(now, arg->val)) == now) 89430259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 89520253Sjoerg } else if (cnf->expire_days > 0) 89620253Sjoerg result = now + ((long) cnf->expire_days * 86400L); 89720253Sjoerg return result; 89820253Sjoerg} 89920253Sjoerg 90020253Sjoerg 90120253Sjoergstatic char * 90220253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 90320253Sjoerg{ 90420253Sjoerg struct carg *arg = getarg(args, 'd'); 905282699Sbapt static char home[128]; 90620253Sjoerg 90720253Sjoerg if (arg) 908282699Sbapt return (arg->val); 90920253Sjoerg 910282699Sbapt if (cnf->home == NULL || *cnf->home == '\0') 911282699Sbapt errx(EX_CONFIG, "no base home directory set"); 912282699Sbapt snprintf(home, sizeof(home), "%s/%s", cnf->home, user); 913282699Sbapt 914282699Sbapt return (home); 91520253Sjoerg} 91620253Sjoerg 91720253Sjoergstatic char * 91820253Sjoergshell_path(char const * path, char *shells[], char *sh) 91920253Sjoerg{ 92020253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 92120253Sjoerg return sh; /* specified full path or forced none */ 92220253Sjoerg else { 92320253Sjoerg char *p; 92420253Sjoerg char paths[_UC_MAXLINE]; 92520253Sjoerg 92620253Sjoerg /* 92720253Sjoerg * We need to search paths 92820253Sjoerg */ 929130633Srobert strlcpy(paths, path, sizeof(paths)); 93020253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 93120253Sjoerg int i; 93220253Sjoerg static char shellpath[256]; 93320253Sjoerg 93420253Sjoerg if (sh != NULL) { 935282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh); 93620253Sjoerg if (access(shellpath, X_OK) == 0) 93720253Sjoerg return shellpath; 93820253Sjoerg } else 93920253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 940282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]); 94120253Sjoerg if (access(shellpath, X_OK) == 0) 94220253Sjoerg return shellpath; 94320253Sjoerg } 94420253Sjoerg } 94520253Sjoerg if (sh == NULL) 94630259Scharnier errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 94730259Scharnier errx(EX_CONFIG, "no default shell available or defined"); 94820253Sjoerg return NULL; 94920253Sjoerg } 95020253Sjoerg} 95120253Sjoerg 95220253Sjoerg 95320253Sjoergstatic char * 95420253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 95520253Sjoerg{ 95620253Sjoerg char *sh = newshell; 95720253Sjoerg struct carg *arg = getarg(args, 's'); 95820253Sjoerg 95920253Sjoerg if (newshell == NULL && arg != NULL) 96020253Sjoerg sh = arg->val; 96120253Sjoerg return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 96220253Sjoerg} 96320253Sjoerg 964179365Santoine#define SALTSIZE 32 96520253Sjoerg 966179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"; 967179365Santoine 96820253Sjoergchar * 96920253Sjoergpw_pwcrypt(char *password) 97020253Sjoerg{ 97120253Sjoerg int i; 972179365Santoine char salt[SALTSIZE + 1]; 973231994Skevlo char *cryptpw; 97420253Sjoerg 97520253Sjoerg static char buf[256]; 97620253Sjoerg 97720253Sjoerg /* 97820253Sjoerg * Calculate a salt value 97920253Sjoerg */ 980179365Santoine for (i = 0; i < SALTSIZE; i++) 981181785Sache salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)]; 982179365Santoine salt[SALTSIZE] = '\0'; 98320253Sjoerg 984231994Skevlo cryptpw = crypt(password, salt); 985231994Skevlo if (cryptpw == NULL) 986231994Skevlo errx(EX_CONFIG, "crypt(3) failure"); 987231994Skevlo return strcpy(buf, cryptpw); 98820253Sjoerg} 98920253Sjoerg 99020590Sdavidn 99120253Sjoergstatic char * 99220253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user) 99320253Sjoerg{ 99420253Sjoerg int i, l; 99520253Sjoerg char pwbuf[32]; 99620253Sjoerg 99720253Sjoerg switch (cnf->default_password) { 99820253Sjoerg case -1: /* Random password */ 99973563Skris l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 100020253Sjoerg for (i = 0; i < l; i++) 1001181785Sache pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)]; 100220253Sjoerg pwbuf[i] = '\0'; 100320253Sjoerg 100420253Sjoerg /* 100520253Sjoerg * We give this information back to the user 100620253Sjoerg */ 1007124382Siedowse if (getarg(args, 'h') == NULL && getarg(args, 'H') == NULL && 1008284121Sbapt !conf.dryrun) { 100961957Sache if (isatty(STDOUT_FILENO)) 101020712Sdavidn printf("Password for '%s' is: ", user); 101120253Sjoerg printf("%s\n", pwbuf); 101220253Sjoerg fflush(stdout); 101320253Sjoerg } 101420253Sjoerg break; 101520253Sjoerg 101620253Sjoerg case -2: /* No password at all! */ 101720253Sjoerg return ""; 101820253Sjoerg 101920253Sjoerg case 0: /* No login - default */ 102020253Sjoerg default: 102120253Sjoerg return "*"; 102220253Sjoerg 102320253Sjoerg case 1: /* user's name */ 1024130633Srobert strlcpy(pwbuf, user, sizeof(pwbuf)); 102520253Sjoerg break; 102620253Sjoerg } 102720253Sjoerg return pw_pwcrypt(pwbuf); 102820253Sjoerg} 102920253Sjoerg 1030284111Sbaptstatic int 1031284128Sbaptdelete_user(struct userconf *cnf, struct passwd *pwd, char *name, 1032284111Sbapt int delete, int mode) 1033284111Sbapt{ 1034284111Sbapt char file[MAXPATHLEN]; 1035284111Sbapt char home[MAXPATHLEN]; 1036284111Sbapt uid_t uid = pwd->pw_uid; 1037284111Sbapt struct group *gr, *grp; 1038284111Sbapt char grname[LOGNAMESIZE]; 1039284111Sbapt int rc; 1040284111Sbapt struct stat st; 104120253Sjoerg 1042284111Sbapt if (strcmp(pwd->pw_name, "root") == 0) 1043284111Sbapt errx(EX_DATAERR, "cannot remove user 'root'"); 1044284111Sbapt 1045284111Sbapt if (!PWALTDIR()) { 1046284111Sbapt /* 1047284111Sbapt * Remove opie record from /etc/opiekeys 1048284111Sbapt */ 1049284111Sbapt 1050284111Sbapt rmopie(pwd->pw_name); 1051284111Sbapt 1052284111Sbapt /* 1053284111Sbapt * Remove crontabs 1054284111Sbapt */ 1055284111Sbapt snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name); 1056284111Sbapt if (access(file, F_OK) == 0) { 1057284111Sbapt snprintf(file, sizeof(file), "crontab -u %s -r", pwd->pw_name); 1058284111Sbapt system(file); 1059284111Sbapt } 1060284111Sbapt } 1061284111Sbapt /* 1062284111Sbapt * Save these for later, since contents of pwd may be 1063284111Sbapt * invalidated by deletion 1064284111Sbapt */ 1065284111Sbapt snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name); 1066284111Sbapt strlcpy(home, pwd->pw_dir, sizeof(home)); 1067284111Sbapt gr = GETGRGID(pwd->pw_gid); 1068284111Sbapt if (gr != NULL) 1069284111Sbapt strlcpy(grname, gr->gr_name, LOGNAMESIZE); 1070284111Sbapt else 1071284111Sbapt grname[0] = '\0'; 1072284111Sbapt 1073284111Sbapt rc = delpwent(pwd); 1074284111Sbapt if (rc == -1) 1075284111Sbapt err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 1076284111Sbapt else if (rc != 0) 1077284111Sbapt err(EX_IOERR, "passwd update"); 1078284111Sbapt 1079284111Sbapt if (cnf->nispasswd && *cnf->nispasswd=='/') { 1080284128Sbapt rc = delnispwent(cnf->nispasswd, name); 1081284111Sbapt if (rc == -1) 1082284111Sbapt warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name); 1083284111Sbapt else if (rc != 0) 1084284111Sbapt warn("WARNING: NIS passwd update"); 1085284111Sbapt /* non-fatal */ 1086284111Sbapt } 1087284111Sbapt 1088284128Sbapt grp = GETGRNAM(name); 1089284111Sbapt if (grp != NULL && 1090284111Sbapt (grp->gr_mem == NULL || *grp->gr_mem == NULL) && 1091284128Sbapt strcmp(name, grname) == 0) 1092284128Sbapt delgrent(GETGRNAM(name)); 1093284111Sbapt SETGRENT(); 1094284111Sbapt while ((grp = GETGRENT()) != NULL) { 1095284111Sbapt int i, j; 1096284111Sbapt char group[MAXLOGNAME]; 1097284113Sbapt if (grp->gr_mem == NULL) 1098284113Sbapt continue; 1099284113Sbapt 1100284113Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 1101284128Sbapt if (strcmp(grp->gr_mem[i], name) != 0) 1102284113Sbapt continue; 1103284113Sbapt 1104284113Sbapt for (j = i; grp->gr_mem[j] != NULL; j++) 1105284113Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 1106284113Sbapt strlcpy(group, grp->gr_name, MAXLOGNAME); 1107284113Sbapt chggrent(group, grp); 1108284111Sbapt } 1109284111Sbapt } 1110284111Sbapt ENDGRENT(); 1111284111Sbapt 1112284128Sbapt pw_log(cnf, mode, W_USER, "%s(%u) account removed", name, uid); 1113284111Sbapt 1114284117Sbapt if (!PWALTDIR()) { 1115284111Sbapt /* 1116284111Sbapt * Remove mail file 1117284111Sbapt */ 1118284111Sbapt remove(file); 1119284111Sbapt 1120284111Sbapt /* 1121284111Sbapt * Remove at jobs 1122284111Sbapt */ 1123284111Sbapt if (getpwuid(uid) == NULL) 1124284111Sbapt rmat(uid); 1125284111Sbapt 1126284111Sbapt /* 1127284111Sbapt * Remove home directory and contents 1128284111Sbapt */ 1129284112Sbapt if (delete && *home == '/' && getpwuid(uid) == NULL && 1130284112Sbapt stat(home, &st) != -1) { 1131284112Sbapt rm_r(home, uid); 1132284112Sbapt pw_log(cnf, mode, W_USER, "%s(%u) home '%s' %sremoved", 1133284128Sbapt name, uid, home, 1134284112Sbapt stat(home, &st) == -1 ? "" : "not completely "); 1135284111Sbapt } 1136284111Sbapt } 1137284111Sbapt 1138284111Sbapt return (EXIT_SUCCESS); 1139284111Sbapt} 1140284111Sbapt 114120253Sjoergstatic int 1142284124Sbaptprint_user(struct passwd * pwd) 114320253Sjoerg{ 1144284122Sbapt if (!conf.pretty) { 1145242349Sbapt char *buf; 114620253Sjoerg 1147284124Sbapt buf = conf.v7 ? pw_make_v7(pwd) : pw_make(pwd); 1148242349Sbapt printf("%s\n", buf); 1149242349Sbapt free(buf); 115020253Sjoerg } else { 115120267Sjoerg int j; 115220253Sjoerg char *p; 115344229Sdavidn struct group *grp = GETGRGID(pwd->pw_gid); 115420253Sjoerg char uname[60] = "User &", office[60] = "[None]", 115520253Sjoerg wphone[60] = "[None]", hphone[60] = "[None]"; 115620590Sdavidn char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 115720590Sdavidn struct tm * tptr; 115820253Sjoerg 115920253Sjoerg if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 1160130633Srobert strlcpy(uname, p, sizeof(uname)); 116120253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1162130633Srobert strlcpy(office, p, sizeof(office)); 116320253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1164130633Srobert strlcpy(wphone, p, sizeof(wphone)); 116520253Sjoerg if ((p = strtok(NULL, "")) != NULL) { 1166130633Srobert strlcpy(hphone, p, 1167130633Srobert sizeof(hphone)); 116820253Sjoerg } 116920253Sjoerg } 117020253Sjoerg } 117120253Sjoerg } 117220253Sjoerg /* 117320253Sjoerg * Handle '&' in gecos field 117420253Sjoerg */ 117520253Sjoerg if ((p = strchr(uname, '&')) != NULL) { 117620253Sjoerg int l = strlen(pwd->pw_name); 117720253Sjoerg int m = strlen(p); 117820253Sjoerg 117920253Sjoerg memmove(p + l, p + 1, m); 118020253Sjoerg memmove(p, pwd->pw_name, l); 118161957Sache *p = (char) toupper((unsigned char)*p); 118220253Sjoerg } 118320590Sdavidn if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 118474569Sache strftime(acexpire, sizeof acexpire, "%c", tptr); 118561957Sache if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 118674569Sache strftime(pwexpire, sizeof pwexpire, "%c", tptr); 1187283842Sbapt printf("Login Name: %-15s #%-12u Group: %-15s #%u\n" 118820747Sdavidn " Full Name: %s\n" 118920747Sdavidn " Home: %-26.26s Class: %s\n" 119020747Sdavidn " Shell: %-26.26s Office: %s\n" 119120747Sdavidn "Work Phone: %-26.26s Home Phone: %s\n" 119220747Sdavidn "Acc Expire: %-26.26s Pwd Expire: %s\n", 1193283842Sbapt pwd->pw_name, pwd->pw_uid, 1194283842Sbapt grp ? grp->gr_name : "(invalid)", pwd->pw_gid, 119520253Sjoerg uname, pwd->pw_dir, pwd->pw_class, 119620590Sdavidn pwd->pw_shell, office, wphone, hphone, 119720590Sdavidn acexpire, pwexpire); 119844229Sdavidn SETGRENT(); 119920267Sjoerg j = 0; 120044229Sdavidn while ((grp=GETGRENT()) != NULL) 120120267Sjoerg { 120220267Sjoerg int i = 0; 1203262865Sjulian if (grp->gr_mem != NULL) { 1204262865Sjulian while (grp->gr_mem[i] != NULL) 120520267Sjoerg { 1206262865Sjulian if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 1207262865Sjulian { 1208262865Sjulian printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 1209262865Sjulian break; 1210262865Sjulian } 1211262865Sjulian ++i; 121220267Sjoerg } 121320267Sjoerg } 121420267Sjoerg } 121544229Sdavidn ENDGRENT(); 121661957Sache printf("%s", j ? "\n" : ""); 121720253Sjoerg } 121820267Sjoerg return EXIT_SUCCESS; 121920253Sjoerg} 122020253Sjoerg 1221284110Sbaptchar * 1222284110Sbaptpw_checkname(char *name, int gecos) 122320253Sjoerg{ 1224109961Sgad char showch[8]; 1225284110Sbapt const char *badchars, *ch, *showtype; 1226109961Sgad int reject; 122720253Sjoerg 1228109961Sgad ch = name; 1229109961Sgad reject = 0; 1230109961Sgad if (gecos) { 1231109961Sgad /* See if the name is valid as a gecos (comment) field. */ 1232109961Sgad badchars = ":!@"; 1233109961Sgad showtype = "gecos field"; 1234109961Sgad } else { 1235109961Sgad /* See if the name is valid as a userid or group. */ 1236109961Sgad badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\""; 1237109961Sgad showtype = "userid/group name"; 1238109961Sgad /* Userids and groups can not have a leading '-'. */ 1239109961Sgad if (*ch == '-') 1240109961Sgad reject = 1; 124120253Sjoerg } 1242109961Sgad if (!reject) { 1243109961Sgad while (*ch) { 1244109961Sgad if (strchr(badchars, *ch) != NULL || *ch < ' ' || 1245109961Sgad *ch == 127) { 1246109961Sgad reject = 1; 1247109961Sgad break; 1248109961Sgad } 1249109961Sgad /* 8-bit characters are only allowed in GECOS fields */ 1250109961Sgad if (!gecos && (*ch & 0x80)) { 1251109961Sgad reject = 1; 1252109961Sgad break; 1253109961Sgad } 1254109961Sgad ch++; 1255109961Sgad } 1256109961Sgad } 1257109961Sgad /* 1258109961Sgad * A `$' is allowed as the final character for userids and groups, 1259109961Sgad * mainly for the benefit of samba. 1260109961Sgad */ 1261109961Sgad if (reject && !gecos) { 1262109961Sgad if (*ch == '$' && *(ch + 1) == '\0') { 1263109961Sgad reject = 0; 1264109961Sgad ch++; 1265109961Sgad } 1266109961Sgad } 1267109961Sgad if (reject) { 1268109961Sgad snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127) 1269109961Sgad ? "`%c'" : "0x%02x", *ch); 1270228673Sdim errx(EX_DATAERR, "invalid character %s at position %td in %s", 1271109961Sgad showch, (ch - name), showtype); 1272109961Sgad } 1273109961Sgad if (!gecos && (ch - name) > LOGNAMESIZE) 1274109961Sgad errx(EX_DATAERR, "name too long `%s' (max is %d)", name, 1275109961Sgad LOGNAMESIZE); 1276284110Sbapt 1277284110Sbapt return (name); 127820253Sjoerg} 127920253Sjoerg 128020253Sjoerg 128120253Sjoergstatic void 128220253Sjoergrmat(uid_t uid) 128320253Sjoerg{ 128420253Sjoerg DIR *d = opendir("/var/at/jobs"); 128520253Sjoerg 128620253Sjoerg if (d != NULL) { 128720253Sjoerg struct dirent *e; 128820253Sjoerg 128920253Sjoerg while ((e = readdir(d)) != NULL) { 129020253Sjoerg struct stat st; 129120253Sjoerg 129220253Sjoerg if (strncmp(e->d_name, ".lock", 5) != 0 && 129320253Sjoerg stat(e->d_name, &st) == 0 && 129420253Sjoerg !S_ISDIR(st.st_mode) && 129520253Sjoerg st.st_uid == uid) { 129620253Sjoerg char tmp[MAXPATHLEN]; 129720253Sjoerg 1298282700Sbapt snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s", e->d_name); 129920253Sjoerg system(tmp); 130020253Sjoerg } 130120253Sjoerg } 130220253Sjoerg closedir(d); 130320253Sjoerg } 130420253Sjoerg} 130520747Sdavidn 130620747Sdavidnstatic void 130785145Sachermopie(char const * name) 130820747Sdavidn{ 130985145Sache static const char etcopie[] = "/etc/opiekeys"; 131085145Sache FILE *fp = fopen(etcopie, "r+"); 131120747Sdavidn 131221052Sdavidn if (fp != NULL) { 131321052Sdavidn char tmp[1024]; 131421052Sdavidn off_t atofs = 0; 131521052Sdavidn int length = strlen(name); 131620747Sdavidn 131721052Sdavidn while (fgets(tmp, sizeof tmp, fp) != NULL) { 131821052Sdavidn if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 131921052Sdavidn if (fseek(fp, atofs, SEEK_SET) == 0) { 132021052Sdavidn fwrite("#", 1, 1, fp); /* Comment username out */ 132121052Sdavidn } 132221052Sdavidn break; 132320747Sdavidn } 132421052Sdavidn atofs = ftell(fp); 132520747Sdavidn } 132621052Sdavidn /* 132721052Sdavidn * If we got an error of any sort, don't update! 132821052Sdavidn */ 132921052Sdavidn fclose(fp); 133020747Sdavidn } 133120747Sdavidn} 133220747Sdavidn 1333