pw_user.c revision 285401
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 285401 2015-07-11 19:07:47Z 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 54285401Sbaptstatic int pw_userdel(char *name, long id); 55284124Sbaptstatic int print_user(struct passwd * pwd); 56284133Sbaptstatic uid_t pw_uidpolicy(struct userconf * cnf, long id); 57284118Sbaptstatic uid_t pw_gidpolicy(struct cargs * args, char *nam, gid_t prefer); 5820253Sjoergstatic time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 5920253Sjoergstatic time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 6020253Sjoergstatic char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 6120253Sjoergstatic char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 6220253Sjoergstatic char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 6320253Sjoergstatic char *shell_path(char const * path, char *shells[], char *sh); 6420253Sjoergstatic void rmat(uid_t uid); 6585145Sachestatic void rmopie(char const * name); 6620253Sjoerg 67283961Sbaptstatic void 68284118Sbaptcreate_and_populate_homedir(int mode, struct passwd *pwd) 69283961Sbapt{ 70283961Sbapt char *homedir, *dotdir; 71284118Sbapt struct userconf *cnf = conf.userconf; 72283961Sbapt 73283961Sbapt homedir = dotdir = NULL; 74283961Sbapt 75284118Sbapt if (conf.rootdir[0] != '\0') { 76284118Sbapt asprintf(&homedir, "%s/%s", conf.rootdir, pwd->pw_dir); 77283961Sbapt if (homedir == NULL) 78283961Sbapt errx(EX_OSERR, "out of memory"); 79284118Sbapt asprintf(&dotdir, "%s/%s", conf.rootdir, cnf->dotdir); 80283961Sbapt } 81283961Sbapt 82283961Sbapt copymkdir(homedir ? homedir : pwd->pw_dir, dotdir ? dotdir: cnf->dotdir, 83283961Sbapt cnf->homemode, pwd->pw_uid, pwd->pw_gid); 84283961Sbapt pw_log(cnf, mode, W_USER, "%s(%u) home %s made", pwd->pw_name, 85283961Sbapt pwd->pw_uid, pwd->pw_dir); 86283961Sbapt} 87283961Sbapt 88285133Sbaptstatic int 89285137Sbaptset_passwd(struct passwd *pwd, bool update) 90285133Sbapt{ 91285133Sbapt int b, istty; 92285133Sbapt struct termios t, n; 93285133Sbapt login_cap_t *lc; 94285133Sbapt char line[_PASSWORD_LEN+1]; 95285133Sbapt char *p; 96285133Sbapt 97285133Sbapt if (conf.fd == '-') { 98285133Sbapt if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 99285133Sbapt pwd->pw_passwd = "*"; /* No access */ 100285133Sbapt return (1); 101285133Sbapt } 102285133Sbapt return (0); 103285133Sbapt } 104285133Sbapt 105285133Sbapt if ((istty = isatty(conf.fd))) { 106285133Sbapt if (tcgetattr(conf.fd, &t) == -1) 107285133Sbapt istty = 0; 108285133Sbapt else { 109285137Sbapt n = t; 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) 137285137Sbapt errx(EX_DATAERR, "bad encrypted password"); 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 150285395Sbaptint 151285395Sbaptpw_usernext(struct userconf *cnf, bool quiet) 152285395Sbapt{ 153285395Sbapt uid_t next = pw_uidpolicy(cnf, -1); 154285395Sbapt 155285395Sbapt if (quiet) 156285395Sbapt return (next); 157285395Sbapt 158285395Sbapt printf("%u:", next); 159285395Sbapt pw_groupnext(cnf, quiet); 160285395Sbapt 161285395Sbapt return (EXIT_SUCCESS); 162285395Sbapt} 163285395Sbapt 164285398Sbaptstatic int 165285398Sbaptpw_usershow(char *name, long id, struct passwd *fakeuser) 166285398Sbapt{ 167285398Sbapt struct passwd *pwd = NULL; 168285398Sbapt 169285398Sbapt if (id < 0 && name == NULL && !conf.all) 170285398Sbapt errx(EX_DATAERR, "username or id or '-a' required"); 171285398Sbapt 172285398Sbapt if (conf.all) { 173285398Sbapt SETPWENT(); 174285398Sbapt while ((pwd = GETPWENT()) != NULL) 175285398Sbapt print_user(pwd); 176285398Sbapt ENDPWENT(); 177285398Sbapt return (EXIT_SUCCESS); 178285398Sbapt } 179285398Sbapt 180285398Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 181285398Sbapt if (pwd == NULL) { 182285398Sbapt if (conf.force) { 183285398Sbapt pwd = fakeuser; 184285398Sbapt } else { 185285398Sbapt if (name == NULL) 186285398Sbapt errx(EX_NOUSER, "no such uid `%ld'", id); 187285398Sbapt errx(EX_NOUSER, "no such user `%s'", name); 188285398Sbapt } 189285398Sbapt } 190285398Sbapt 191285398Sbapt return (print_user(pwd)); 192285398Sbapt} 193285398Sbapt 19420253Sjoerg/*- 19520253Sjoerg * -C config configuration file 19620253Sjoerg * -q quiet operation 19720253Sjoerg * -n name login name 19820253Sjoerg * -u uid user id 19920253Sjoerg * -c comment user name/comment 20020253Sjoerg * -d directory home directory 20120253Sjoerg * -e date account expiry date 20220253Sjoerg * -p date password expiry date 20320253Sjoerg * -g grp primary group 20420253Sjoerg * -G grp1,grp2 additional groups 20520253Sjoerg * -m [ -k dir ] create and set up home 20620253Sjoerg * -s shell name of login shell 20720253Sjoerg * -o duplicate uid ok 20820253Sjoerg * -L class user class 20920253Sjoerg * -l name new login name 21020253Sjoerg * -h fd password filehandle 211124382Siedowse * -H fd encrypted password filehandle 21220253Sjoerg * -F force print or add 21320253Sjoerg * Setting defaults: 21420253Sjoerg * -D set user defaults 21520253Sjoerg * -b dir default home root dir 21620253Sjoerg * -e period default expiry period 21720253Sjoerg * -p period default password change period 21820253Sjoerg * -g group default group 21920253Sjoerg * -G grp1,grp2.. default additional groups 22020253Sjoerg * -L class default login class 22120253Sjoerg * -k dir default home skeleton 22220253Sjoerg * -s shell default shell 22320253Sjoerg * -w method default password method 22420253Sjoerg */ 22520253Sjoerg 22620253Sjoergint 227284128Sbaptpw_user(int mode, char *name, long id, struct cargs * args) 22820253Sjoerg{ 22952527Sdavidn int rc, edited = 0; 23020253Sjoerg char *p = NULL; 23152512Sdavidn char *passtmp; 23220253Sjoerg struct carg *arg; 23320253Sjoerg struct passwd *pwd = NULL; 23420253Sjoerg struct group *grp; 23520253Sjoerg struct stat st; 236284118Sbapt struct userconf *cnf; 23720747Sdavidn char line[_PASSWORD_LEN+1]; 238283961Sbapt char path[MAXPATHLEN]; 23982868Sdd FILE *fp; 240167919Sle char *dmode_c; 241167919Sle void *set = NULL; 24220253Sjoerg 24320253Sjoerg static struct passwd fakeuser = 24420253Sjoerg { 245285398Sbapt "nouser", 24620253Sjoerg "*", 24720253Sjoerg -1, 24820253Sjoerg -1, 24920253Sjoerg 0, 25020253Sjoerg "", 25120253Sjoerg "User &", 25256000Sdavidn "/nonexistent", 25320253Sjoerg "/bin/sh", 25420253Sjoerg 0 25556000Sdavidn#if defined(__FreeBSD__) 25656000Sdavidn ,0 25756000Sdavidn#endif 25820253Sjoerg }; 25920253Sjoerg 260284118Sbapt cnf = conf.userconf; 26152512Sdavidn 262284149Sbapt if (mode == M_NEXT) 263285396Sbapt return (pw_usernext(cnf, conf.quiet)); 26420267Sjoerg 265285398Sbapt if (mode == M_PRINT) 266285398Sbapt return (pw_usershow(name, id, &fakeuser)); 267285398Sbapt 268285401Sbapt if (mode == M_DELETE) 269285401Sbapt return (pw_userdel(name, id)); 270285401Sbapt 27120267Sjoerg /* 27220253Sjoerg * We can do all of the common legwork here 27320253Sjoerg */ 27420253Sjoerg 27520253Sjoerg if ((arg = getarg(args, 'b')) != NULL) { 27620267Sjoerg cnf->home = arg->val; 27720253Sjoerg } 27821052Sdavidn 279167919Sle if ((arg = getarg(args, 'M')) != NULL) { 280167919Sle dmode_c = arg->val; 281167919Sle if ((set = setmode(dmode_c)) == NULL) 282167919Sle errx(EX_DATAERR, "invalid directory creation mode '%s'", 283167919Sle dmode_c); 284219408Sjkim cnf->homemode = getmode(set, _DEF_DIRMODE); 285167919Sle free(set); 286168044Sle } 287167919Sle 28821052Sdavidn /* 28921052Sdavidn * If we'll need to use it or we're updating it, 29021052Sdavidn * then create the base home directory if necessary 29121052Sdavidn */ 292224535Sdelphij if (arg != NULL || getarg(args, 'm') != NULL) { 29321052Sdavidn int l = strlen(cnf->home); 29421052Sdavidn 29521052Sdavidn if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 29621052Sdavidn cnf->home[--l] = '\0'; 29721052Sdavidn 29821052Sdavidn if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 29930259Scharnier errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); 30021052Sdavidn 30121052Sdavidn if (stat(cnf->home, &st) == -1) { 30221052Sdavidn char dbuf[MAXPATHLEN]; 30321052Sdavidn 30421242Sdavidn /* 30521242Sdavidn * This is a kludge especially for Joerg :) 30621242Sdavidn * If the home directory would be created in the root partition, then 30721242Sdavidn * we really create it under /usr which is likely to have more space. 30821242Sdavidn * But we create a symlink from cnf->home -> "/usr" -> cnf->home 30921242Sdavidn */ 31021242Sdavidn if (strchr(cnf->home+1, '/') == NULL) { 311282683Sbapt snprintf(dbuf, MAXPATHLEN, "/usr%s", cnf->home); 312219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) { 31321242Sdavidn chown(dbuf, 0, 0); 314148584Spjd /* 315148584Spjd * Skip first "/" and create symlink: 316148584Spjd * /home -> usr/home 317148584Spjd */ 318148584Spjd symlink(dbuf+1, cnf->home); 31921242Sdavidn } 32021242Sdavidn /* If this falls, fall back to old method */ 32121242Sdavidn } 322130633Srobert strlcpy(dbuf, cnf->home, sizeof(dbuf)); 323130633Srobert p = dbuf; 32421242Sdavidn if (stat(dbuf, &st) == -1) { 325252377Skientzle while ((p = strchr(p + 1, '/')) != NULL) { 32621242Sdavidn *p = '\0'; 32721242Sdavidn if (stat(dbuf, &st) == -1) { 328219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) == -1) 32921242Sdavidn goto direrr; 33021242Sdavidn chown(dbuf, 0, 0); 33121242Sdavidn } else if (!S_ISDIR(st.st_mode)) 33230259Scharnier errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); 33321242Sdavidn *p = '/'; 33421242Sdavidn } 33521052Sdavidn } 33621242Sdavidn if (stat(dbuf, &st) == -1) { 337219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) == -1) { 33830259Scharnier direrr: err(EX_OSFILE, "mkdir '%s'", dbuf); 33921052Sdavidn } 34021052Sdavidn chown(dbuf, 0, 0); 34121052Sdavidn } 34221052Sdavidn } else if (!S_ISDIR(st.st_mode)) 34330259Scharnier errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); 34421052Sdavidn } 34521052Sdavidn 34620253Sjoerg if ((arg = getarg(args, 'e')) != NULL) 34720253Sjoerg cnf->expire_days = atoi(arg->val); 34820253Sjoerg 34921330Sdavidn if ((arg = getarg(args, 'y')) != NULL) 35021330Sdavidn cnf->nispasswd = arg->val; 35121330Sdavidn 35220253Sjoerg if ((arg = getarg(args, 'p')) != NULL && arg->val) 35320253Sjoerg cnf->password_days = atoi(arg->val); 35420253Sjoerg 35520253Sjoerg if ((arg = getarg(args, 'g')) != NULL) { 35663596Sdavidn if (!*(p = arg->val)) /* Handle empty group list specially */ 35763596Sdavidn cnf->default_group = ""; 35863596Sdavidn else { 35963596Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 36063596Sdavidn if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 36163596Sdavidn errx(EX_NOUSER, "group `%s' does not exist", p); 36263596Sdavidn } 36363596Sdavidn cnf->default_group = newstr(grp->gr_name); 36420253Sjoerg } 36520253Sjoerg } 36620253Sjoerg if ((arg = getarg(args, 'L')) != NULL) 367284110Sbapt cnf->default_class = pw_checkname(arg->val, 0); 36820253Sjoerg 36920253Sjoerg if ((arg = getarg(args, 'G')) != NULL && arg->val) { 37052527Sdavidn int i = 0; 37120253Sjoerg 37220747Sdavidn for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 37344229Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 37461957Sache if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 37530259Scharnier errx(EX_NOUSER, "group `%s' does not exist", p); 37620253Sjoerg } 37720747Sdavidn if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 37820747Sdavidn cnf->groups[i++] = newstr(grp->gr_name); 37920253Sjoerg } 38020747Sdavidn while (i < cnf->numgroups) 38120253Sjoerg cnf->groups[i++] = NULL; 38220253Sjoerg } 38352527Sdavidn 38420253Sjoerg if ((arg = getarg(args, 'k')) != NULL) { 38526088Sdavidn if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode)) 38630259Scharnier errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir); 38720253Sjoerg } 38852527Sdavidn 38920253Sjoerg if ((arg = getarg(args, 's')) != NULL) 39020253Sjoerg cnf->shell_default = arg->val; 39120253Sjoerg 39263600Sdavidn if ((arg = getarg(args, 'w')) != NULL) 39363600Sdavidn cnf->default_password = boolean_val(arg->val, cnf->default_password); 39420253Sjoerg if (mode == M_ADD && getarg(args, 'D')) { 395284135Sbapt if (name != NULL) 39630259Scharnier errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 39720253Sjoerg if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 39820253Sjoerg if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 39920253Sjoerg cnf->min_uid = 1000; 40020253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 40120253Sjoerg cnf->max_uid = 32000; 40220253Sjoerg } 40320253Sjoerg if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 40420253Sjoerg if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 40520253Sjoerg cnf->min_gid = 1000; 40620253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 40720253Sjoerg cnf->max_gid = 32000; 40820253Sjoerg } 40920253Sjoerg 410284135Sbapt if (write_userconfig(conf.config)) 411284135Sbapt return (EXIT_SUCCESS); 412283814Sbapt err(EX_IOERR, "config udpate"); 41320253Sjoerg } 41452527Sdavidn 415284128Sbapt if (name != NULL) 416284128Sbapt pwd = GETPWNAM(pw_checkname(name, 0)); 41720253Sjoerg 418284128Sbapt if (id < 0 && name == NULL) 419284128Sbapt errx(EX_DATAERR, "user name or id required"); 42020253Sjoerg 42120253Sjoerg /* 422285401Sbapt * Update require that the user exists 42320253Sjoerg */ 424285401Sbapt if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) { 42552527Sdavidn 426284128Sbapt if (name == NULL && pwd == NULL) /* Try harder */ 427284128Sbapt pwd = GETPWUID(id); 42820253Sjoerg 42920253Sjoerg if (pwd == NULL) { 430284128Sbapt if (name == NULL) 431284128Sbapt errx(EX_NOUSER, "no such uid `%ld'", id); 432284128Sbapt errx(EX_NOUSER, "no such user `%s'", name); 43320253Sjoerg } 43452527Sdavidn 435284128Sbapt if (name == NULL) 436284128Sbapt name = pwd->pw_name; 43720253Sjoerg 43820253Sjoerg /* 43952512Sdavidn * The M_LOCK and M_UNLOCK functions simply add or remove 44052512Sdavidn * a "*LOCKED*" prefix from in front of the password to 44152512Sdavidn * prevent it decoding correctly, and therefore prevents 44252512Sdavidn * access. Of course, this only prevents access via 44352512Sdavidn * password authentication (not ssh, kerberos or any 44452512Sdavidn * other method that does not use the UNIX password) but 44552512Sdavidn * that is a known limitation. 44652512Sdavidn */ 44752512Sdavidn 44852512Sdavidn if (mode == M_LOCK) { 44952512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0) 45052512Sdavidn errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 451282685Sbapt asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd); 45252512Sdavidn if (passtmp == NULL) /* disaster */ 45352512Sdavidn errx(EX_UNAVAILABLE, "out of memory"); 45452512Sdavidn pwd->pw_passwd = passtmp; 45552527Sdavidn edited = 1; 45652512Sdavidn } else if (mode == M_UNLOCK) { 45752512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0) 45852512Sdavidn errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 45952512Sdavidn pwd->pw_passwd += sizeof(locked_str)-1; 46052527Sdavidn edited = 1; 461285401Sbapt } 46220253Sjoerg 46320253Sjoerg /* 46420253Sjoerg * The rest is edit code 46520253Sjoerg */ 466284129Sbapt if (conf.newname != NULL) { 46720253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 46830259Scharnier errx(EX_DATAERR, "can't rename `root' account"); 469284129Sbapt pwd->pw_name = pw_checkname(conf.newname, 0); 47052527Sdavidn edited = 1; 47120253Sjoerg } 47252527Sdavidn 473284133Sbapt if (id > 0 && isdigit((unsigned char)*arg->val)) { 474284133Sbapt pwd->pw_uid = (uid_t)id; 47552527Sdavidn edited = 1; 47620253Sjoerg if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 47730259Scharnier errx(EX_DATAERR, "can't change uid of `root' account"); 47820253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 47930259Scharnier warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 48020253Sjoerg } 48120253Sjoerg 48252527Sdavidn if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */ 48352527Sdavidn gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid; 48452527Sdavidn if (newgid != pwd->pw_gid) { 48552527Sdavidn edited = 1; 48661762Sdavidn pwd->pw_gid = newgid; 48752527Sdavidn } 48852527Sdavidn } 48952527Sdavidn 49020253Sjoerg if ((arg = getarg(args, 'p')) != NULL) { 49152527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 49252527Sdavidn if (pwd->pw_change != 0) { 49352527Sdavidn pwd->pw_change = 0; 49452527Sdavidn edited = 1; 49552527Sdavidn } 49652527Sdavidn } 49720253Sjoerg else { 49820253Sjoerg time_t now = time(NULL); 49920253Sjoerg time_t expire = parse_date(now, arg->val); 50020253Sjoerg 50152527Sdavidn if (pwd->pw_change != expire) { 50252527Sdavidn pwd->pw_change = expire; 50352527Sdavidn edited = 1; 50452527Sdavidn } 50520253Sjoerg } 50620253Sjoerg } 50752527Sdavidn 50820267Sjoerg if ((arg = getarg(args, 'e')) != NULL) { 50952527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 51052527Sdavidn if (pwd->pw_expire != 0) { 51152527Sdavidn pwd->pw_expire = 0; 51252527Sdavidn edited = 1; 51352527Sdavidn } 51452527Sdavidn } 51520253Sjoerg else { 51620253Sjoerg time_t now = time(NULL); 51720253Sjoerg time_t expire = parse_date(now, arg->val); 51820253Sjoerg 51952527Sdavidn if (pwd->pw_expire != expire) { 52052527Sdavidn pwd->pw_expire = expire; 52152527Sdavidn edited = 1; 52252527Sdavidn } 52320253Sjoerg } 52420253Sjoerg } 52520253Sjoerg 52652527Sdavidn if ((arg = getarg(args, 's')) != NULL) { 52752527Sdavidn char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 52852527Sdavidn if (shell == NULL) 52952527Sdavidn shell = ""; 53052527Sdavidn if (strcmp(shell, pwd->pw_shell) != 0) { 53152527Sdavidn pwd->pw_shell = shell; 53252527Sdavidn edited = 1; 53352527Sdavidn } 53452527Sdavidn } 53520253Sjoerg 53652527Sdavidn if (getarg(args, 'L')) { 53752527Sdavidn if (cnf->default_class == NULL) 53852527Sdavidn cnf->default_class = ""; 53952527Sdavidn if (strcmp(pwd->pw_class, cnf->default_class) != 0) { 54052527Sdavidn pwd->pw_class = cnf->default_class; 54152527Sdavidn edited = 1; 54252527Sdavidn } 54352527Sdavidn } 54452527Sdavidn 54520747Sdavidn if ((arg = getarg(args, 'd')) != NULL) { 546130629Srobert if (strcmp(pwd->pw_dir, arg->val)) 547130629Srobert edited = 1; 54820747Sdavidn if (stat(pwd->pw_dir = arg->val, &st) == -1) { 54920747Sdavidn if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 55030259Scharnier warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 55120747Sdavidn } else if (!S_ISDIR(st.st_mode)) 55230259Scharnier warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 55320747Sdavidn } 55420747Sdavidn 555285137Sbapt if ((arg = getarg(args, 'w')) != NULL && conf.fd == -1) { 55664918Sgreen login_cap_t *lc; 55764918Sgreen 55864918Sgreen lc = login_getpwclass(pwd); 55964918Sgreen if (lc == NULL || 560252688Sdes login_setcryptfmt(lc, "sha512", NULL) == NULL) 56164918Sgreen warn("setting crypt(3) format"); 56264918Sgreen login_close(lc); 56320267Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 56452527Sdavidn edited = 1; 56552527Sdavidn } 56620267Sjoerg 56720253Sjoerg } else { 56864918Sgreen login_cap_t *lc; 56952527Sdavidn 57052527Sdavidn /* 57152527Sdavidn * Add code 57252527Sdavidn */ 57352527Sdavidn 574284128Sbapt if (name == NULL) /* Required */ 57530259Scharnier errx(EX_DATAERR, "login name required"); 576284128Sbapt else if ((pwd = GETPWNAM(name)) != NULL) /* Exists */ 577284128Sbapt errx(EX_DATAERR, "login name `%s' already exists", name); 57820253Sjoerg 57920253Sjoerg /* 58020253Sjoerg * Now, set up defaults for a new user 58120253Sjoerg */ 58220253Sjoerg pwd = &fakeuser; 583284128Sbapt pwd->pw_name = name; 58420253Sjoerg pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 585284133Sbapt pwd->pw_uid = pw_uidpolicy(cnf, id); 586284118Sbapt pwd->pw_gid = pw_gidpolicy(args, pwd->pw_name, (gid_t) pwd->pw_uid); 58720253Sjoerg pwd->pw_change = pw_pwdpolicy(cnf, args); 58820253Sjoerg pwd->pw_expire = pw_exppolicy(cnf, args); 58920253Sjoerg pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 59020253Sjoerg pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 59164918Sgreen lc = login_getpwclass(pwd); 592272833Sdes if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) 59364918Sgreen warn("setting crypt(3) format"); 59464918Sgreen login_close(lc); 59564918Sgreen pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 59652527Sdavidn edited = 1; 59720253Sjoerg 59820253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 59930259Scharnier warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 60020253Sjoerg } 60120253Sjoerg 60220253Sjoerg /* 60320253Sjoerg * Shared add/edit code 60420253Sjoerg */ 60552527Sdavidn if ((arg = getarg(args, 'c')) != NULL) { 606284110Sbapt char *gecos = pw_checkname(arg->val, 1); 60752527Sdavidn if (strcmp(pwd->pw_gecos, gecos) != 0) { 60852527Sdavidn pwd->pw_gecos = gecos; 60952527Sdavidn edited = 1; 61052527Sdavidn } 61152527Sdavidn } 61220253Sjoerg 613285133Sbapt if (conf.fd != -1) 614285137Sbapt edited = set_passwd(pwd, mode == M_UPDATE); 61520253Sjoerg 61620267Sjoerg /* 61720267Sjoerg * Special case: -N only displays & exits 61820267Sjoerg */ 619284121Sbapt if (conf.dryrun) 620284126Sbapt return print_user(pwd); 62120267Sjoerg 62221330Sdavidn if (mode == M_ADD) { 62352527Sdavidn edited = 1; /* Always */ 62452502Sdavidn rc = addpwent(pwd); 625283814Sbapt if (rc == -1) 626283814Sbapt errx(EX_IOERR, "user '%s' already exists", 627283814Sbapt pwd->pw_name); 628283814Sbapt else if (rc != 0) 629283814Sbapt err(EX_IOERR, "passwd file update"); 63052502Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/') { 63152502Sdavidn rc = addnispwent(cnf->nispasswd, pwd); 63252502Sdavidn if (rc == -1) 63352502Sdavidn warnx("User '%s' already exists in NIS passwd", pwd->pw_name); 63452502Sdavidn else 63556000Sdavidn warn("NIS passwd update"); 63652502Sdavidn /* NOTE: we treat NIS-only update errors as non-fatal */ 63752502Sdavidn } 63852512Sdavidn } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) { 63952527Sdavidn if (edited) { /* Only updated this if required */ 640284128Sbapt rc = chgpwent(name, pwd); 641283814Sbapt if (rc == -1) 642283814Sbapt errx(EX_IOERR, "user '%s' does not exist (NIS?)", pwd->pw_name); 643283814Sbapt else if (rc != 0) 644283814Sbapt err(EX_IOERR, "passwd file update"); 64552527Sdavidn if ( cnf->nispasswd && *cnf->nispasswd=='/') { 646284128Sbapt rc = chgnispwent(cnf->nispasswd, name, pwd); 64752527Sdavidn if (rc == -1) 64852527Sdavidn warn("User '%s' not found in NIS passwd", pwd->pw_name); 64952527Sdavidn else 65056000Sdavidn warn("NIS passwd update"); 65152527Sdavidn /* NOTE: NIS-only update errors are not fatal */ 65252527Sdavidn } 65352502Sdavidn } 65421330Sdavidn } 65521330Sdavidn 65620253Sjoerg /* 65720253Sjoerg * Ok, user is created or changed - now edit group file 65820253Sjoerg */ 65920253Sjoerg 660242349Sbapt if (mode == M_ADD || getarg(args, 'G') != NULL) { 661273779Sbapt int i, j; 662273779Sbapt /* First remove the user from all group */ 663273779Sbapt SETGRENT(); 664273779Sbapt while ((grp = GETGRENT()) != NULL) { 665273779Sbapt char group[MAXLOGNAME]; 666273779Sbapt if (grp->gr_mem == NULL) 667273779Sbapt continue; 668273779Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 669273779Sbapt if (strcmp(grp->gr_mem[i] , pwd->pw_name) != 0) 670273779Sbapt continue; 671273779Sbapt for (j = i; grp->gr_mem[j] != NULL ; j++) 672273779Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 673273779Sbapt strlcpy(group, grp->gr_name, MAXLOGNAME); 674273779Sbapt chggrent(group, grp); 675273779Sbapt } 676273779Sbapt } 677273779Sbapt ENDGRENT(); 678273779Sbapt 679273779Sbapt /* now add to group where needed */ 680242349Sbapt for (i = 0; cnf->groups[i] != NULL; i++) { 681242349Sbapt grp = GETGRNAM(cnf->groups[i]); 682244737Sbapt grp = gr_add(grp, pwd->pw_name); 683244737Sbapt /* 684244737Sbapt * grp can only be NULL in 2 cases: 685244737Sbapt * - the new member is already a member 686244737Sbapt * - a problem with memory occurs 687244737Sbapt * in both cases we want to skip now. 688244737Sbapt */ 689244737Sbapt if (grp == NULL) 690242349Sbapt continue; 691242349Sbapt chggrent(cnf->groups[i], grp); 692245114Smjg free(grp); 693242349Sbapt } 694242349Sbapt } 695242349Sbapt 696242349Sbapt 69761759Sdavidn /* go get a current version of pwd */ 698284128Sbapt pwd = GETPWNAM(name); 69961759Sdavidn if (pwd == NULL) { 70061759Sdavidn /* This will fail when we rename, so special case that */ 701284129Sbapt if (mode == M_UPDATE && conf.newname != NULL) { 702284129Sbapt name = conf.newname; /* update new name */ 703284128Sbapt pwd = GETPWNAM(name); /* refetch renamed rec */ 70461759Sdavidn } 70561759Sdavidn } 70661759Sdavidn if (pwd == NULL) /* can't go on without this */ 707284128Sbapt errx(EX_NOUSER, "user '%s' disappeared during update", name); 70820253Sjoerg 70944229Sdavidn grp = GETGRGID(pwd->pw_gid); 710283842Sbapt pw_log(cnf, mode, W_USER, "%s(%u):%s(%u):%s:%s:%s", 711283842Sbapt pwd->pw_name, pwd->pw_uid, 712283842Sbapt grp ? grp->gr_name : "unknown", (grp ? grp->gr_gid : (uid_t)-1), 71320253Sjoerg pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 71420253Sjoerg 71520253Sjoerg /* 71620253Sjoerg * If adding, let's touch and chown the user's mail file. This is not 71720253Sjoerg * strictly necessary under BSD with a 0755 maildir but it also 71820253Sjoerg * doesn't hurt anything to create the empty mailfile 71920253Sjoerg */ 72020253Sjoerg if (mode == M_ADD) { 721283961Sbapt if (PWALTDIR() != PWF_ALT) { 722283961Sbapt arg = getarg(args, 'R'); 723283961Sbapt snprintf(path, sizeof(path), "%s%s/%s", 724283961Sbapt arg ? arg->val : "", _PATH_MAILDIR, pwd->pw_name); 725283961Sbapt close(open(path, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 72644229Sdavidn * mtime */ 727283961Sbapt chown(path, pwd->pw_uid, pwd->pw_gid); 72820253Sjoerg } 72920253Sjoerg } 73052527Sdavidn 73120253Sjoerg /* 73282868Sdd * Let's create and populate the user's home directory. Note 73320253Sjoerg * that this also `works' for editing users if -m is used, but 73420253Sjoerg * existing files will *not* be overwritten. 73520253Sjoerg */ 736283961Sbapt if (PWALTDIR() != PWF_ALT && getarg(args, 'm') != NULL && pwd->pw_dir && 737283961Sbapt *pwd->pw_dir == '/' && pwd->pw_dir[1]) 738284118Sbapt create_and_populate_homedir(mode, pwd); 73952527Sdavidn 74082868Sdd /* 74182868Sdd * Finally, send mail to the new user as well, if we are asked to 74282868Sdd */ 74382868Sdd if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 74482868Sdd FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 74582868Sdd 74682868Sdd if (pfp == NULL) 74782868Sdd warn("sendmail"); 74882868Sdd else { 74982868Sdd fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 75082868Sdd while (fgets(line, sizeof(line), fp) != NULL) { 75182868Sdd /* Do substitutions? */ 75282868Sdd fputs(line, pfp); 75382868Sdd } 75482868Sdd pclose(pfp); 755283842Sbapt pw_log(cnf, mode, W_USER, "%s(%u) new user mail sent", 756283842Sbapt pwd->pw_name, pwd->pw_uid); 75782868Sdd } 75882868Sdd fclose(fp); 75982868Sdd } 76082868Sdd 76120267Sjoerg return EXIT_SUCCESS; 76220253Sjoerg} 76320253Sjoerg 76420253Sjoerg 76520253Sjoergstatic uid_t 766284133Sbaptpw_uidpolicy(struct userconf * cnf, long id) 76720253Sjoerg{ 76820253Sjoerg struct passwd *pwd; 76920253Sjoerg uid_t uid = (uid_t) - 1; 77020253Sjoerg 77120253Sjoerg /* 77220253Sjoerg * Check the given uid, if any 77320253Sjoerg */ 774284133Sbapt if (id > 0) { 775284133Sbapt uid = (uid_t) id; 77620253Sjoerg 777284133Sbapt if ((pwd = GETPWUID(uid)) != NULL && conf.checkduplicate) 778283842Sbapt errx(EX_DATAERR, "uid `%u' has already been allocated", pwd->pw_uid); 77920253Sjoerg } else { 78020253Sjoerg struct bitmap bm; 78120253Sjoerg 78220253Sjoerg /* 78320253Sjoerg * We need to allocate the next available uid under one of 78420253Sjoerg * two policies a) Grab the first unused uid b) Grab the 78520253Sjoerg * highest possible unused uid 78620253Sjoerg */ 78720253Sjoerg if (cnf->min_uid >= cnf->max_uid) { /* Sanity 78820253Sjoerg * claus^H^H^H^Hheck */ 78920253Sjoerg cnf->min_uid = 1000; 79020253Sjoerg cnf->max_uid = 32000; 79120253Sjoerg } 79220253Sjoerg bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 79320253Sjoerg 79420253Sjoerg /* 79520253Sjoerg * Now, let's fill the bitmap from the password file 79620253Sjoerg */ 79744229Sdavidn SETPWENT(); 79844229Sdavidn while ((pwd = GETPWENT()) != NULL) 79944229Sdavidn if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 80020253Sjoerg bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 80144229Sdavidn ENDPWENT(); 80220253Sjoerg 80320253Sjoerg /* 80420253Sjoerg * Then apply the policy, with fallback to reuse if necessary 80520253Sjoerg */ 80620253Sjoerg if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 80720253Sjoerg uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 80820253Sjoerg 80920253Sjoerg /* 81020253Sjoerg * Another sanity check 81120253Sjoerg */ 81220253Sjoerg if (uid < cnf->min_uid || uid > cnf->max_uid) 81330259Scharnier errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 81420253Sjoerg bm_dealloc(&bm); 81520253Sjoerg } 81620253Sjoerg return uid; 81720253Sjoerg} 81820253Sjoerg 81920253Sjoerg 82020253Sjoergstatic uid_t 821284118Sbaptpw_gidpolicy(struct cargs * args, char *nam, gid_t prefer) 82220253Sjoerg{ 82320253Sjoerg struct group *grp; 82420253Sjoerg gid_t gid = (uid_t) - 1; 82520253Sjoerg struct carg *a_gid = getarg(args, 'g'); 826284118Sbapt struct userconf *cnf = conf.userconf; 82720253Sjoerg 82820253Sjoerg /* 82920253Sjoerg * If no arg given, see if default can help out 83020253Sjoerg */ 83120253Sjoerg if (a_gid == NULL && cnf->default_group && *cnf->default_group) 83220253Sjoerg a_gid = addarg(args, 'g', cnf->default_group); 83320253Sjoerg 83420253Sjoerg /* 83520253Sjoerg * Check the given gid, if any 83620253Sjoerg */ 83744229Sdavidn SETGRENT(); 83820253Sjoerg if (a_gid != NULL) { 83944229Sdavidn if ((grp = GETGRNAM(a_gid->val)) == NULL) { 84020253Sjoerg gid = (gid_t) atol(a_gid->val); 84161957Sache if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL) 84230259Scharnier errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 84320253Sjoerg } 84420253Sjoerg gid = grp->gr_gid; 845262865Sjulian } else if ((grp = GETGRNAM(nam)) != NULL && 846262865Sjulian (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) { 84720267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 84820253Sjoerg } else { 84920253Sjoerg struct cargs grpargs; 85020253Sjoerg char tmp[32]; 85120253Sjoerg 85220253Sjoerg LIST_INIT(&grpargs); 85320253Sjoerg 85420253Sjoerg /* 85520253Sjoerg * We need to auto-create a group with the user's name. We 85620253Sjoerg * can send all the appropriate output to our sister routine 85720253Sjoerg * bit first see if we can create a group with gid==uid so we 85820253Sjoerg * can keep the user and group ids in sync. We purposely do 85920253Sjoerg * NOT check the gid range if we can force the sync. If the 86020253Sjoerg * user's name dups an existing group, then the group add 86120253Sjoerg * function will happily handle that case for us and exit. 86220253Sjoerg */ 86344229Sdavidn if (GETGRGID(prefer) == NULL) { 864282700Sbapt snprintf(tmp, sizeof(tmp), "%u", prefer); 86520253Sjoerg addarg(&grpargs, 'g', tmp); 86620253Sjoerg } 867284121Sbapt if (conf.dryrun) { 868285395Sbapt gid = pw_groupnext(cnf, true); 869285395Sbapt } else { 870284128Sbapt pw_group(M_ADD, nam, -1, &grpargs); 87144229Sdavidn if ((grp = GETGRNAM(nam)) != NULL) 87220267Sjoerg gid = grp->gr_gid; 87320267Sjoerg } 87470486Sben a_gid = LIST_FIRST(&grpargs); 87520253Sjoerg while (a_gid != NULL) { 87670486Sben struct carg *t = LIST_NEXT(a_gid, list); 87720253Sjoerg LIST_REMOVE(a_gid, list); 87820253Sjoerg a_gid = t; 87920253Sjoerg } 88020253Sjoerg } 88144229Sdavidn ENDGRENT(); 88220253Sjoerg return gid; 88320253Sjoerg} 88420253Sjoerg 88520253Sjoerg 88620253Sjoergstatic time_t 88720253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args) 88820253Sjoerg{ 88920253Sjoerg time_t result = 0; 89020253Sjoerg time_t now = time(NULL); 89127831Sdavidn struct carg *arg = getarg(args, 'p'); 89220253Sjoerg 89320253Sjoerg if (arg != NULL) { 89420253Sjoerg if ((result = parse_date(now, arg->val)) == now) 89530259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 89620253Sjoerg } else if (cnf->password_days > 0) 89720253Sjoerg result = now + ((long) cnf->password_days * 86400L); 89820253Sjoerg return result; 89920253Sjoerg} 90020253Sjoerg 90120253Sjoerg 90220253Sjoergstatic time_t 90320253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args) 90420253Sjoerg{ 90520253Sjoerg time_t result = 0; 90620253Sjoerg time_t now = time(NULL); 90720253Sjoerg struct carg *arg = getarg(args, 'e'); 90820253Sjoerg 90920253Sjoerg if (arg != NULL) { 91020253Sjoerg if ((result = parse_date(now, arg->val)) == now) 91130259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 91220253Sjoerg } else if (cnf->expire_days > 0) 91320253Sjoerg result = now + ((long) cnf->expire_days * 86400L); 91420253Sjoerg return result; 91520253Sjoerg} 91620253Sjoerg 91720253Sjoerg 91820253Sjoergstatic char * 91920253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 92020253Sjoerg{ 92120253Sjoerg struct carg *arg = getarg(args, 'd'); 922282699Sbapt static char home[128]; 92320253Sjoerg 92420253Sjoerg if (arg) 925282699Sbapt return (arg->val); 92620253Sjoerg 927282699Sbapt if (cnf->home == NULL || *cnf->home == '\0') 928282699Sbapt errx(EX_CONFIG, "no base home directory set"); 929282699Sbapt snprintf(home, sizeof(home), "%s/%s", cnf->home, user); 930282699Sbapt 931282699Sbapt return (home); 93220253Sjoerg} 93320253Sjoerg 93420253Sjoergstatic char * 93520253Sjoergshell_path(char const * path, char *shells[], char *sh) 93620253Sjoerg{ 93720253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 93820253Sjoerg return sh; /* specified full path or forced none */ 93920253Sjoerg else { 94020253Sjoerg char *p; 94120253Sjoerg char paths[_UC_MAXLINE]; 94220253Sjoerg 94320253Sjoerg /* 94420253Sjoerg * We need to search paths 94520253Sjoerg */ 946130633Srobert strlcpy(paths, path, sizeof(paths)); 94720253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 94820253Sjoerg int i; 94920253Sjoerg static char shellpath[256]; 95020253Sjoerg 95120253Sjoerg if (sh != NULL) { 952282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh); 95320253Sjoerg if (access(shellpath, X_OK) == 0) 95420253Sjoerg return shellpath; 95520253Sjoerg } else 95620253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 957282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]); 95820253Sjoerg if (access(shellpath, X_OK) == 0) 95920253Sjoerg return shellpath; 96020253Sjoerg } 96120253Sjoerg } 96220253Sjoerg if (sh == NULL) 96330259Scharnier errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 96430259Scharnier errx(EX_CONFIG, "no default shell available or defined"); 96520253Sjoerg return NULL; 96620253Sjoerg } 96720253Sjoerg} 96820253Sjoerg 96920253Sjoerg 97020253Sjoergstatic char * 97120253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 97220253Sjoerg{ 97320253Sjoerg char *sh = newshell; 97420253Sjoerg struct carg *arg = getarg(args, 's'); 97520253Sjoerg 97620253Sjoerg if (newshell == NULL && arg != NULL) 97720253Sjoerg sh = arg->val; 97820253Sjoerg return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 97920253Sjoerg} 98020253Sjoerg 981179365Santoine#define SALTSIZE 32 98220253Sjoerg 983179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"; 984179365Santoine 98520253Sjoergchar * 98620253Sjoergpw_pwcrypt(char *password) 98720253Sjoerg{ 98820253Sjoerg int i; 989179365Santoine char salt[SALTSIZE + 1]; 990231994Skevlo char *cryptpw; 99120253Sjoerg 99220253Sjoerg static char buf[256]; 99320253Sjoerg 99420253Sjoerg /* 99520253Sjoerg * Calculate a salt value 99620253Sjoerg */ 997179365Santoine for (i = 0; i < SALTSIZE; i++) 998181785Sache salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)]; 999179365Santoine salt[SALTSIZE] = '\0'; 100020253Sjoerg 1001231994Skevlo cryptpw = crypt(password, salt); 1002231994Skevlo if (cryptpw == NULL) 1003231994Skevlo errx(EX_CONFIG, "crypt(3) failure"); 1004231994Skevlo return strcpy(buf, cryptpw); 100520253Sjoerg} 100620253Sjoerg 100720590Sdavidn 100820253Sjoergstatic char * 100920253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user) 101020253Sjoerg{ 101120253Sjoerg int i, l; 101220253Sjoerg char pwbuf[32]; 101320253Sjoerg 101420253Sjoerg switch (cnf->default_password) { 101520253Sjoerg case -1: /* Random password */ 101673563Skris l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 101720253Sjoerg for (i = 0; i < l; i++) 1018181785Sache pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)]; 101920253Sjoerg pwbuf[i] = '\0'; 102020253Sjoerg 102120253Sjoerg /* 102220253Sjoerg * We give this information back to the user 102320253Sjoerg */ 1024285137Sbapt if (conf.fd == -1 && !conf.dryrun) { 102561957Sache if (isatty(STDOUT_FILENO)) 102620712Sdavidn printf("Password for '%s' is: ", user); 102720253Sjoerg printf("%s\n", pwbuf); 102820253Sjoerg fflush(stdout); 102920253Sjoerg } 103020253Sjoerg break; 103120253Sjoerg 103220253Sjoerg case -2: /* No password at all! */ 103320253Sjoerg return ""; 103420253Sjoerg 103520253Sjoerg case 0: /* No login - default */ 103620253Sjoerg default: 103720253Sjoerg return "*"; 103820253Sjoerg 103920253Sjoerg case 1: /* user's name */ 1040130633Srobert strlcpy(pwbuf, user, sizeof(pwbuf)); 104120253Sjoerg break; 104220253Sjoerg } 104320253Sjoerg return pw_pwcrypt(pwbuf); 104420253Sjoerg} 104520253Sjoerg 1046284111Sbaptstatic int 1047285401Sbaptpw_userdel(char *name, long id) 1048284111Sbapt{ 1049285401Sbapt struct passwd *pwd = NULL; 1050284111Sbapt char file[MAXPATHLEN]; 1051284111Sbapt char home[MAXPATHLEN]; 1052285401Sbapt uid_t uid; 1053284111Sbapt struct group *gr, *grp; 1054284111Sbapt char grname[LOGNAMESIZE]; 1055284111Sbapt int rc; 1056284111Sbapt struct stat st; 105720253Sjoerg 1058285401Sbapt if (id < 0 && name == NULL) 1059285401Sbapt errx(EX_DATAERR, "username or id required"); 1060285401Sbapt 1061285401Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 1062285401Sbapt if (pwd == NULL) { 1063285401Sbapt if (name == NULL) 1064285401Sbapt errx(EX_NOUSER, "no such uid `%ld'", id); 1065285401Sbapt errx(EX_NOUSER, "no such user `%s'", name); 1066285401Sbapt } 1067285401Sbapt uid = pwd->pw_uid; 1068285401Sbapt if (name == NULL) 1069285401Sbapt name = pwd->pw_name; 1070285401Sbapt 1071284111Sbapt if (strcmp(pwd->pw_name, "root") == 0) 1072284111Sbapt errx(EX_DATAERR, "cannot remove user 'root'"); 1073284111Sbapt 1074284111Sbapt if (!PWALTDIR()) { 1075284111Sbapt /* 1076284111Sbapt * Remove opie record from /etc/opiekeys 1077284111Sbapt */ 1078284111Sbapt 1079284111Sbapt rmopie(pwd->pw_name); 1080284111Sbapt 1081284111Sbapt /* 1082284111Sbapt * Remove crontabs 1083284111Sbapt */ 1084284111Sbapt snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name); 1085284111Sbapt if (access(file, F_OK) == 0) { 1086284111Sbapt snprintf(file, sizeof(file), "crontab -u %s -r", pwd->pw_name); 1087284111Sbapt system(file); 1088284111Sbapt } 1089284111Sbapt } 1090284111Sbapt /* 1091284111Sbapt * Save these for later, since contents of pwd may be 1092284111Sbapt * invalidated by deletion 1093284111Sbapt */ 1094284111Sbapt snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name); 1095284111Sbapt strlcpy(home, pwd->pw_dir, sizeof(home)); 1096284111Sbapt gr = GETGRGID(pwd->pw_gid); 1097284111Sbapt if (gr != NULL) 1098284111Sbapt strlcpy(grname, gr->gr_name, LOGNAMESIZE); 1099284111Sbapt else 1100284111Sbapt grname[0] = '\0'; 1101284111Sbapt 1102284111Sbapt rc = delpwent(pwd); 1103284111Sbapt if (rc == -1) 1104284111Sbapt err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 1105284111Sbapt else if (rc != 0) 1106284111Sbapt err(EX_IOERR, "passwd update"); 1107284111Sbapt 1108285401Sbapt if (conf.userconf->nispasswd && *conf.userconf->nispasswd=='/') { 1109285401Sbapt rc = delnispwent(conf.userconf->nispasswd, name); 1110284111Sbapt if (rc == -1) 1111285401Sbapt warnx("WARNING: user '%s' does not exist in NIS passwd", 1112285401Sbapt pwd->pw_name); 1113284111Sbapt else if (rc != 0) 1114284111Sbapt warn("WARNING: NIS passwd update"); 1115284111Sbapt /* non-fatal */ 1116284111Sbapt } 1117284111Sbapt 1118284128Sbapt grp = GETGRNAM(name); 1119284111Sbapt if (grp != NULL && 1120284111Sbapt (grp->gr_mem == NULL || *grp->gr_mem == NULL) && 1121284128Sbapt strcmp(name, grname) == 0) 1122284128Sbapt delgrent(GETGRNAM(name)); 1123284111Sbapt SETGRENT(); 1124284111Sbapt while ((grp = GETGRENT()) != NULL) { 1125284111Sbapt int i, j; 1126284111Sbapt char group[MAXLOGNAME]; 1127284113Sbapt if (grp->gr_mem == NULL) 1128284113Sbapt continue; 1129284113Sbapt 1130284113Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 1131284128Sbapt if (strcmp(grp->gr_mem[i], name) != 0) 1132284113Sbapt continue; 1133284113Sbapt 1134284113Sbapt for (j = i; grp->gr_mem[j] != NULL; j++) 1135284113Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 1136284113Sbapt strlcpy(group, grp->gr_name, MAXLOGNAME); 1137284113Sbapt chggrent(group, grp); 1138284111Sbapt } 1139284111Sbapt } 1140284111Sbapt ENDGRENT(); 1141284111Sbapt 1142285401Sbapt pw_log(conf.userconf, M_DELETE, W_USER, "%s(%u) account removed", name, 1143285401Sbapt uid); 1144284111Sbapt 1145284117Sbapt if (!PWALTDIR()) { 1146284111Sbapt /* 1147284111Sbapt * Remove mail file 1148284111Sbapt */ 1149284111Sbapt remove(file); 1150284111Sbapt 1151284111Sbapt /* 1152284111Sbapt * Remove at jobs 1153284111Sbapt */ 1154284111Sbapt if (getpwuid(uid) == NULL) 1155284111Sbapt rmat(uid); 1156284111Sbapt 1157284111Sbapt /* 1158284111Sbapt * Remove home directory and contents 1159284111Sbapt */ 1160285401Sbapt if (conf.deletehome && *home == '/' && getpwuid(uid) == NULL && 1161284112Sbapt stat(home, &st) != -1) { 1162284112Sbapt rm_r(home, uid); 1163285401Sbapt pw_log(conf.userconf, M_DELETE, W_USER, "%s(%u) home '%s' %sremoved", 1164284128Sbapt name, uid, home, 1165284112Sbapt stat(home, &st) == -1 ? "" : "not completely "); 1166284111Sbapt } 1167284111Sbapt } 1168284111Sbapt 1169284111Sbapt return (EXIT_SUCCESS); 1170284111Sbapt} 1171284111Sbapt 117220253Sjoergstatic int 1173284124Sbaptprint_user(struct passwd * pwd) 117420253Sjoerg{ 1175284122Sbapt if (!conf.pretty) { 1176242349Sbapt char *buf; 117720253Sjoerg 1178284124Sbapt buf = conf.v7 ? pw_make_v7(pwd) : pw_make(pwd); 1179242349Sbapt printf("%s\n", buf); 1180242349Sbapt free(buf); 118120253Sjoerg } else { 118220267Sjoerg int j; 118320253Sjoerg char *p; 118444229Sdavidn struct group *grp = GETGRGID(pwd->pw_gid); 118520253Sjoerg char uname[60] = "User &", office[60] = "[None]", 118620253Sjoerg wphone[60] = "[None]", hphone[60] = "[None]"; 118720590Sdavidn char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 118820590Sdavidn struct tm * tptr; 118920253Sjoerg 119020253Sjoerg if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 1191130633Srobert strlcpy(uname, p, sizeof(uname)); 119220253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1193130633Srobert strlcpy(office, p, sizeof(office)); 119420253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1195130633Srobert strlcpy(wphone, p, sizeof(wphone)); 119620253Sjoerg if ((p = strtok(NULL, "")) != NULL) { 1197130633Srobert strlcpy(hphone, p, 1198130633Srobert sizeof(hphone)); 119920253Sjoerg } 120020253Sjoerg } 120120253Sjoerg } 120220253Sjoerg } 120320253Sjoerg /* 120420253Sjoerg * Handle '&' in gecos field 120520253Sjoerg */ 120620253Sjoerg if ((p = strchr(uname, '&')) != NULL) { 120720253Sjoerg int l = strlen(pwd->pw_name); 120820253Sjoerg int m = strlen(p); 120920253Sjoerg 121020253Sjoerg memmove(p + l, p + 1, m); 121120253Sjoerg memmove(p, pwd->pw_name, l); 121261957Sache *p = (char) toupper((unsigned char)*p); 121320253Sjoerg } 121420590Sdavidn if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 121574569Sache strftime(acexpire, sizeof acexpire, "%c", tptr); 121661957Sache if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 121774569Sache strftime(pwexpire, sizeof pwexpire, "%c", tptr); 1218283842Sbapt printf("Login Name: %-15s #%-12u Group: %-15s #%u\n" 121920747Sdavidn " Full Name: %s\n" 122020747Sdavidn " Home: %-26.26s Class: %s\n" 122120747Sdavidn " Shell: %-26.26s Office: %s\n" 122220747Sdavidn "Work Phone: %-26.26s Home Phone: %s\n" 122320747Sdavidn "Acc Expire: %-26.26s Pwd Expire: %s\n", 1224283842Sbapt pwd->pw_name, pwd->pw_uid, 1225283842Sbapt grp ? grp->gr_name : "(invalid)", pwd->pw_gid, 122620253Sjoerg uname, pwd->pw_dir, pwd->pw_class, 122720590Sdavidn pwd->pw_shell, office, wphone, hphone, 122820590Sdavidn acexpire, pwexpire); 122944229Sdavidn SETGRENT(); 123020267Sjoerg j = 0; 123144229Sdavidn while ((grp=GETGRENT()) != NULL) 123220267Sjoerg { 123320267Sjoerg int i = 0; 1234262865Sjulian if (grp->gr_mem != NULL) { 1235262865Sjulian while (grp->gr_mem[i] != NULL) 123620267Sjoerg { 1237262865Sjulian if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 1238262865Sjulian { 1239262865Sjulian printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 1240262865Sjulian break; 1241262865Sjulian } 1242262865Sjulian ++i; 124320267Sjoerg } 124420267Sjoerg } 124520267Sjoerg } 124644229Sdavidn ENDGRENT(); 124761957Sache printf("%s", j ? "\n" : ""); 124820253Sjoerg } 124920267Sjoerg return EXIT_SUCCESS; 125020253Sjoerg} 125120253Sjoerg 1252284110Sbaptchar * 1253284110Sbaptpw_checkname(char *name, int gecos) 125420253Sjoerg{ 1255109961Sgad char showch[8]; 1256284110Sbapt const char *badchars, *ch, *showtype; 1257109961Sgad int reject; 125820253Sjoerg 1259109961Sgad ch = name; 1260109961Sgad reject = 0; 1261109961Sgad if (gecos) { 1262109961Sgad /* See if the name is valid as a gecos (comment) field. */ 1263109961Sgad badchars = ":!@"; 1264109961Sgad showtype = "gecos field"; 1265109961Sgad } else { 1266109961Sgad /* See if the name is valid as a userid or group. */ 1267109961Sgad badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\""; 1268109961Sgad showtype = "userid/group name"; 1269109961Sgad /* Userids and groups can not have a leading '-'. */ 1270109961Sgad if (*ch == '-') 1271109961Sgad reject = 1; 127220253Sjoerg } 1273109961Sgad if (!reject) { 1274109961Sgad while (*ch) { 1275109961Sgad if (strchr(badchars, *ch) != NULL || *ch < ' ' || 1276109961Sgad *ch == 127) { 1277109961Sgad reject = 1; 1278109961Sgad break; 1279109961Sgad } 1280109961Sgad /* 8-bit characters are only allowed in GECOS fields */ 1281109961Sgad if (!gecos && (*ch & 0x80)) { 1282109961Sgad reject = 1; 1283109961Sgad break; 1284109961Sgad } 1285109961Sgad ch++; 1286109961Sgad } 1287109961Sgad } 1288109961Sgad /* 1289109961Sgad * A `$' is allowed as the final character for userids and groups, 1290109961Sgad * mainly for the benefit of samba. 1291109961Sgad */ 1292109961Sgad if (reject && !gecos) { 1293109961Sgad if (*ch == '$' && *(ch + 1) == '\0') { 1294109961Sgad reject = 0; 1295109961Sgad ch++; 1296109961Sgad } 1297109961Sgad } 1298109961Sgad if (reject) { 1299109961Sgad snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127) 1300109961Sgad ? "`%c'" : "0x%02x", *ch); 1301228673Sdim errx(EX_DATAERR, "invalid character %s at position %td in %s", 1302109961Sgad showch, (ch - name), showtype); 1303109961Sgad } 1304109961Sgad if (!gecos && (ch - name) > LOGNAMESIZE) 1305109961Sgad errx(EX_DATAERR, "name too long `%s' (max is %d)", name, 1306109961Sgad LOGNAMESIZE); 1307284110Sbapt 1308284110Sbapt return (name); 130920253Sjoerg} 131020253Sjoerg 131120253Sjoerg 131220253Sjoergstatic void 131320253Sjoergrmat(uid_t uid) 131420253Sjoerg{ 131520253Sjoerg DIR *d = opendir("/var/at/jobs"); 131620253Sjoerg 131720253Sjoerg if (d != NULL) { 131820253Sjoerg struct dirent *e; 131920253Sjoerg 132020253Sjoerg while ((e = readdir(d)) != NULL) { 132120253Sjoerg struct stat st; 132220253Sjoerg 132320253Sjoerg if (strncmp(e->d_name, ".lock", 5) != 0 && 132420253Sjoerg stat(e->d_name, &st) == 0 && 132520253Sjoerg !S_ISDIR(st.st_mode) && 132620253Sjoerg st.st_uid == uid) { 132720253Sjoerg char tmp[MAXPATHLEN]; 132820253Sjoerg 1329282700Sbapt snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s", e->d_name); 133020253Sjoerg system(tmp); 133120253Sjoerg } 133220253Sjoerg } 133320253Sjoerg closedir(d); 133420253Sjoerg } 133520253Sjoerg} 133620747Sdavidn 133720747Sdavidnstatic void 133885145Sachermopie(char const * name) 133920747Sdavidn{ 134085145Sache static const char etcopie[] = "/etc/opiekeys"; 134185145Sache FILE *fp = fopen(etcopie, "r+"); 134220747Sdavidn 134321052Sdavidn if (fp != NULL) { 134421052Sdavidn char tmp[1024]; 134521052Sdavidn off_t atofs = 0; 134621052Sdavidn int length = strlen(name); 134720747Sdavidn 134821052Sdavidn while (fgets(tmp, sizeof tmp, fp) != NULL) { 134921052Sdavidn if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 135021052Sdavidn if (fseek(fp, atofs, SEEK_SET) == 0) { 135121052Sdavidn fwrite("#", 1, 1, fp); /* Comment username out */ 135221052Sdavidn } 135321052Sdavidn break; 135420747Sdavidn } 135521052Sdavidn atofs = ftell(fp); 135620747Sdavidn } 135721052Sdavidn /* 135821052Sdavidn * If we got an error of any sort, don't update! 135921052Sdavidn */ 136021052Sdavidn fclose(fp); 136120747Sdavidn } 136220747Sdavidn} 136320747Sdavidn 1364