pw_user.c revision 285430
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 285430 2015-07-12 20:29:51Z 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); 62285409Sbaptstatic char *pw_password(struct userconf * cnf, 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 68285403Sbaptcreate_and_populate_homedir(struct passwd *pwd) 69283961Sbapt{ 70284118Sbapt struct userconf *cnf = conf.userconf; 71285430Sbapt const char *skeldir; 72285430Sbapt int skelfd = -1; 73283961Sbapt 74285430Sbapt skeldir = cnf->dotdir; 75283961Sbapt 76285430Sbapt if (skeldir != NULL && *skeldir != '\0') { 77285430Sbapt skelfd = openat(conf.rootfd, cnf->dotdir, 78285430Sbapt O_DIRECTORY|O_CLOEXEC); 79283961Sbapt } 80283961Sbapt 81285430Sbapt copymkdir(conf.rootfd, pwd->pw_dir, skelfd, cnf->homemode, pwd->pw_uid, 82285430Sbapt pwd->pw_gid, 0); 83285403Sbapt pw_log(cnf, M_ADD, W_USER, "%s(%u) home %s made", pwd->pw_name, 84283961Sbapt pwd->pw_uid, pwd->pw_dir); 85283961Sbapt} 86283961Sbapt 87285133Sbaptstatic int 88285137Sbaptset_passwd(struct passwd *pwd, bool update) 89285133Sbapt{ 90285133Sbapt int b, istty; 91285133Sbapt struct termios t, n; 92285133Sbapt login_cap_t *lc; 93285133Sbapt char line[_PASSWORD_LEN+1]; 94285133Sbapt char *p; 95285133Sbapt 96285133Sbapt if (conf.fd == '-') { 97285133Sbapt if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 98285133Sbapt pwd->pw_passwd = "*"; /* No access */ 99285133Sbapt return (1); 100285133Sbapt } 101285133Sbapt return (0); 102285133Sbapt } 103285133Sbapt 104285133Sbapt if ((istty = isatty(conf.fd))) { 105285133Sbapt if (tcgetattr(conf.fd, &t) == -1) 106285133Sbapt istty = 0; 107285133Sbapt else { 108285137Sbapt n = t; 109285133Sbapt n.c_lflag &= ~(ECHO); 110285133Sbapt tcsetattr(conf.fd, TCSANOW, &n); 111285133Sbapt printf("%s%spassword for user %s:", 112285133Sbapt update ? "new " : "", 113285133Sbapt conf.precrypted ? "encrypted " : "", 114285133Sbapt pwd->pw_name); 115285133Sbapt fflush(stdout); 116285133Sbapt } 117285133Sbapt } 118285133Sbapt b = read(conf.fd, line, sizeof(line) - 1); 119285133Sbapt if (istty) { /* Restore state */ 120285133Sbapt tcsetattr(conf.fd, TCSANOW, &t); 121285133Sbapt fputc('\n', stdout); 122285133Sbapt fflush(stdout); 123285133Sbapt } 124285133Sbapt 125285133Sbapt if (b < 0) 126285133Sbapt err(EX_IOERR, "-%c file descriptor", 127285133Sbapt conf.precrypted ? 'H' : 'h'); 128285133Sbapt line[b] = '\0'; 129285133Sbapt if ((p = strpbrk(line, "\r\n")) != NULL) 130285133Sbapt *p = '\0'; 131285133Sbapt if (!*line) 132285133Sbapt errx(EX_DATAERR, "empty password read on file descriptor %d", 133285133Sbapt conf.fd); 134285133Sbapt if (conf.precrypted) { 135285133Sbapt if (strchr(line, ':') != NULL) 136285137Sbapt errx(EX_DATAERR, "bad encrypted password"); 137285133Sbapt pwd->pw_passwd = line; 138285133Sbapt } else { 139285133Sbapt lc = login_getpwclass(pwd); 140285133Sbapt if (lc == NULL || 141285133Sbapt login_setcryptfmt(lc, "sha512", NULL) == NULL) 142285133Sbapt warn("setting crypt(3) format"); 143285133Sbapt login_close(lc); 144285133Sbapt pwd->pw_passwd = pw_pwcrypt(line); 145285133Sbapt } 146285133Sbapt return (1); 147285133Sbapt} 148285133Sbapt 149285395Sbaptint 150285395Sbaptpw_usernext(struct userconf *cnf, bool quiet) 151285395Sbapt{ 152285395Sbapt uid_t next = pw_uidpolicy(cnf, -1); 153285395Sbapt 154285395Sbapt if (quiet) 155285395Sbapt return (next); 156285395Sbapt 157285395Sbapt printf("%u:", next); 158285395Sbapt pw_groupnext(cnf, quiet); 159285395Sbapt 160285395Sbapt return (EXIT_SUCCESS); 161285395Sbapt} 162285395Sbapt 163285398Sbaptstatic int 164285398Sbaptpw_usershow(char *name, long id, struct passwd *fakeuser) 165285398Sbapt{ 166285398Sbapt struct passwd *pwd = NULL; 167285398Sbapt 168285398Sbapt if (id < 0 && name == NULL && !conf.all) 169285398Sbapt errx(EX_DATAERR, "username or id or '-a' required"); 170285398Sbapt 171285398Sbapt if (conf.all) { 172285398Sbapt SETPWENT(); 173285398Sbapt while ((pwd = GETPWENT()) != NULL) 174285398Sbapt print_user(pwd); 175285398Sbapt ENDPWENT(); 176285398Sbapt return (EXIT_SUCCESS); 177285398Sbapt } 178285398Sbapt 179285398Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 180285398Sbapt if (pwd == NULL) { 181285398Sbapt if (conf.force) { 182285398Sbapt pwd = fakeuser; 183285398Sbapt } else { 184285398Sbapt if (name == NULL) 185285398Sbapt errx(EX_NOUSER, "no such uid `%ld'", id); 186285398Sbapt errx(EX_NOUSER, "no such user `%s'", name); 187285398Sbapt } 188285398Sbapt } 189285398Sbapt 190285398Sbapt return (print_user(pwd)); 191285398Sbapt} 192285398Sbapt 193285405Sbaptstatic void 194285405Sbaptperform_chgpwent(const char *name, struct passwd *pwd) 195285405Sbapt{ 196285405Sbapt int rc; 197285405Sbapt 198285405Sbapt rc = chgpwent(name, pwd); 199285405Sbapt if (rc == -1) 200285405Sbapt errx(EX_IOERR, "user '%s' does not exist (NIS?)", pwd->pw_name); 201285405Sbapt else if (rc != 0) 202285405Sbapt err(EX_IOERR, "passwd file update"); 203285405Sbapt 204285405Sbapt if (conf.userconf->nispasswd && *conf.userconf->nispasswd == '/') { 205285405Sbapt rc = chgnispwent(conf.userconf->nispasswd, name, pwd); 206285405Sbapt if (rc == -1) 207285405Sbapt warn("User '%s' not found in NIS passwd", pwd->pw_name); 208285405Sbapt else 209285405Sbapt warn("NIS passwd update"); 210285405Sbapt /* NOTE: NIS-only update errors are not fatal */ 211285405Sbapt } 212285405Sbapt} 213285405Sbapt 214285405Sbapt/* 215285405Sbapt * The M_LOCK and M_UNLOCK functions simply add or remove 216285405Sbapt * a "*LOCKED*" prefix from in front of the password to 217285405Sbapt * prevent it decoding correctly, and therefore prevents 218285405Sbapt * access. Of course, this only prevents access via 219285405Sbapt * password authentication (not ssh, kerberos or any 220285405Sbapt * other method that does not use the UNIX password) but 221285405Sbapt * that is a known limitation. 222285405Sbapt */ 223285405Sbaptstatic int 224285405Sbaptpw_userlock(char *name, long id, int mode) 225285405Sbapt{ 226285405Sbapt struct passwd *pwd = NULL; 227285405Sbapt char *passtmp = NULL; 228285405Sbapt bool locked = false; 229285405Sbapt 230285405Sbapt if (id < 0 && name == NULL) 231285405Sbapt errx(EX_DATAERR, "username or id required"); 232285405Sbapt 233285405Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 234285405Sbapt if (pwd == NULL) { 235285405Sbapt if (name == NULL) 236285405Sbapt errx(EX_NOUSER, "no such uid `%ld'", id); 237285405Sbapt errx(EX_NOUSER, "no such user `%s'", name); 238285405Sbapt } 239285405Sbapt 240285405Sbapt if (name == NULL) 241285405Sbapt name = pwd->pw_name; 242285405Sbapt 243285405Sbapt if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str) -1) == 0) 244285405Sbapt locked = true; 245285405Sbapt if (mode == M_LOCK && locked) 246285405Sbapt errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 247285405Sbapt if (mode == M_UNLOCK && !locked) 248285405Sbapt errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 249285405Sbapt 250285405Sbapt if (mode == M_LOCK) { 251285405Sbapt asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd); 252285405Sbapt if (passtmp == NULL) /* disaster */ 253285405Sbapt errx(EX_UNAVAILABLE, "out of memory"); 254285405Sbapt pwd->pw_passwd = passtmp; 255285405Sbapt } else { 256285405Sbapt pwd->pw_passwd += sizeof(locked_str)-1; 257285405Sbapt } 258285405Sbapt 259285405Sbapt perform_chgpwent(name, pwd); 260285405Sbapt free(passtmp); 261285405Sbapt 262285405Sbapt return (EXIT_SUCCESS); 263285405Sbapt} 264285405Sbapt 26520253Sjoerg/*- 26620253Sjoerg * -C config configuration file 26720253Sjoerg * -q quiet operation 26820253Sjoerg * -n name login name 26920253Sjoerg * -u uid user id 27020253Sjoerg * -c comment user name/comment 27120253Sjoerg * -d directory home directory 27220253Sjoerg * -e date account expiry date 27320253Sjoerg * -p date password expiry date 27420253Sjoerg * -g grp primary group 27520253Sjoerg * -G grp1,grp2 additional groups 27620253Sjoerg * -m [ -k dir ] create and set up home 27720253Sjoerg * -s shell name of login shell 27820253Sjoerg * -o duplicate uid ok 27920253Sjoerg * -L class user class 28020253Sjoerg * -l name new login name 28120253Sjoerg * -h fd password filehandle 282124382Siedowse * -H fd encrypted password filehandle 28320253Sjoerg * -F force print or add 28420253Sjoerg * Setting defaults: 28520253Sjoerg * -D set user defaults 28620253Sjoerg * -b dir default home root dir 28720253Sjoerg * -e period default expiry period 28820253Sjoerg * -p period default password change period 28920253Sjoerg * -g group default group 29020253Sjoerg * -G grp1,grp2.. default additional groups 29120253Sjoerg * -L class default login class 29220253Sjoerg * -k dir default home skeleton 29320253Sjoerg * -s shell default shell 29420253Sjoerg * -w method default password method 29520253Sjoerg */ 29620253Sjoerg 29720253Sjoergint 298284128Sbaptpw_user(int mode, char *name, long id, struct cargs * args) 29920253Sjoerg{ 30052527Sdavidn int rc, edited = 0; 30120253Sjoerg char *p = NULL; 30220253Sjoerg struct carg *arg; 30320253Sjoerg struct passwd *pwd = NULL; 30420253Sjoerg struct group *grp; 30520253Sjoerg struct stat st; 306284118Sbapt struct userconf *cnf; 30720747Sdavidn char line[_PASSWORD_LEN+1]; 308283961Sbapt char path[MAXPATHLEN]; 30982868Sdd FILE *fp; 310167919Sle char *dmode_c; 311167919Sle void *set = NULL; 31220253Sjoerg 31320253Sjoerg static struct passwd fakeuser = 31420253Sjoerg { 315285398Sbapt "nouser", 31620253Sjoerg "*", 31720253Sjoerg -1, 31820253Sjoerg -1, 31920253Sjoerg 0, 32020253Sjoerg "", 32120253Sjoerg "User &", 32256000Sdavidn "/nonexistent", 32320253Sjoerg "/bin/sh", 32420253Sjoerg 0 32556000Sdavidn#if defined(__FreeBSD__) 32656000Sdavidn ,0 32756000Sdavidn#endif 32820253Sjoerg }; 32920253Sjoerg 330284118Sbapt cnf = conf.userconf; 33152512Sdavidn 332284149Sbapt if (mode == M_NEXT) 333285396Sbapt return (pw_usernext(cnf, conf.quiet)); 33420267Sjoerg 335285398Sbapt if (mode == M_PRINT) 336285398Sbapt return (pw_usershow(name, id, &fakeuser)); 337285398Sbapt 338285401Sbapt if (mode == M_DELETE) 339285401Sbapt return (pw_userdel(name, id)); 340285401Sbapt 341285405Sbapt if (mode == M_LOCK || mode == M_UNLOCK) 342285405Sbapt return (pw_userlock(name, id, mode)); 343285405Sbapt 34420267Sjoerg /* 34520253Sjoerg * We can do all of the common legwork here 34620253Sjoerg */ 34720253Sjoerg 34820253Sjoerg if ((arg = getarg(args, 'b')) != NULL) { 34920267Sjoerg cnf->home = arg->val; 35020253Sjoerg } 35121052Sdavidn 352167919Sle if ((arg = getarg(args, 'M')) != NULL) { 353167919Sle dmode_c = arg->val; 354167919Sle if ((set = setmode(dmode_c)) == NULL) 355167919Sle errx(EX_DATAERR, "invalid directory creation mode '%s'", 356167919Sle dmode_c); 357219408Sjkim cnf->homemode = getmode(set, _DEF_DIRMODE); 358167919Sle free(set); 359168044Sle } 360167919Sle 36121052Sdavidn /* 36221052Sdavidn * If we'll need to use it or we're updating it, 36321052Sdavidn * then create the base home directory if necessary 36421052Sdavidn */ 365224535Sdelphij if (arg != NULL || getarg(args, 'm') != NULL) { 36621052Sdavidn int l = strlen(cnf->home); 36721052Sdavidn 36821052Sdavidn if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 36921052Sdavidn cnf->home[--l] = '\0'; 37021052Sdavidn 37121052Sdavidn if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 37230259Scharnier errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); 37321052Sdavidn 37421052Sdavidn if (stat(cnf->home, &st) == -1) { 37521052Sdavidn char dbuf[MAXPATHLEN]; 37621052Sdavidn 37721242Sdavidn /* 37821242Sdavidn * This is a kludge especially for Joerg :) 37921242Sdavidn * If the home directory would be created in the root partition, then 38021242Sdavidn * we really create it under /usr which is likely to have more space. 38121242Sdavidn * But we create a symlink from cnf->home -> "/usr" -> cnf->home 38221242Sdavidn */ 38321242Sdavidn if (strchr(cnf->home+1, '/') == NULL) { 384282683Sbapt snprintf(dbuf, MAXPATHLEN, "/usr%s", cnf->home); 385219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) { 38621242Sdavidn chown(dbuf, 0, 0); 387148584Spjd /* 388148584Spjd * Skip first "/" and create symlink: 389148584Spjd * /home -> usr/home 390148584Spjd */ 391148584Spjd symlink(dbuf+1, cnf->home); 39221242Sdavidn } 39321242Sdavidn /* If this falls, fall back to old method */ 39421242Sdavidn } 395130633Srobert strlcpy(dbuf, cnf->home, sizeof(dbuf)); 396130633Srobert p = dbuf; 39721242Sdavidn if (stat(dbuf, &st) == -1) { 398252377Skientzle while ((p = strchr(p + 1, '/')) != NULL) { 39921242Sdavidn *p = '\0'; 40021242Sdavidn if (stat(dbuf, &st) == -1) { 401219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) == -1) 402285406Sbapt err(EX_OSFILE, "mkdir '%s'", dbuf); 40321242Sdavidn chown(dbuf, 0, 0); 40421242Sdavidn } else if (!S_ISDIR(st.st_mode)) 40530259Scharnier errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); 40621242Sdavidn *p = '/'; 40721242Sdavidn } 40821052Sdavidn } 40921242Sdavidn if (stat(dbuf, &st) == -1) { 410285406Sbapt if (mkdir(dbuf, _DEF_DIRMODE) == -1) 411285406Sbapt err(EX_OSFILE, "mkdir '%s'", dbuf); 41221052Sdavidn chown(dbuf, 0, 0); 41321052Sdavidn } 41421052Sdavidn } else if (!S_ISDIR(st.st_mode)) 41530259Scharnier errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); 41621052Sdavidn } 41721052Sdavidn 41820253Sjoerg if ((arg = getarg(args, 'e')) != NULL) 41920253Sjoerg cnf->expire_days = atoi(arg->val); 42020253Sjoerg 42121330Sdavidn if ((arg = getarg(args, 'y')) != NULL) 42221330Sdavidn cnf->nispasswd = arg->val; 42321330Sdavidn 42420253Sjoerg if ((arg = getarg(args, 'p')) != NULL && arg->val) 42520253Sjoerg cnf->password_days = atoi(arg->val); 42620253Sjoerg 42720253Sjoerg if ((arg = getarg(args, 'g')) != NULL) { 42863596Sdavidn if (!*(p = arg->val)) /* Handle empty group list specially */ 42963596Sdavidn cnf->default_group = ""; 43063596Sdavidn else { 43163596Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 43263596Sdavidn if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 43363596Sdavidn errx(EX_NOUSER, "group `%s' does not exist", p); 43463596Sdavidn } 43563596Sdavidn cnf->default_group = newstr(grp->gr_name); 43620253Sjoerg } 43720253Sjoerg } 43820253Sjoerg if ((arg = getarg(args, 'L')) != NULL) 439284110Sbapt cnf->default_class = pw_checkname(arg->val, 0); 44020253Sjoerg 44120253Sjoerg if ((arg = getarg(args, 'G')) != NULL && arg->val) { 44220747Sdavidn for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 44344229Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 44461957Sache if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 44530259Scharnier errx(EX_NOUSER, "group `%s' does not exist", p); 44620253Sjoerg } 447285412Sbapt sl_add(cnf->groups, newstr(grp->gr_name)); 44820253Sjoerg } 44920253Sjoerg } 45052527Sdavidn 45120253Sjoerg if ((arg = getarg(args, 'k')) != NULL) { 45226088Sdavidn if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode)) 45330259Scharnier errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir); 45420253Sjoerg } 45552527Sdavidn 45620253Sjoerg if ((arg = getarg(args, 's')) != NULL) 45720253Sjoerg cnf->shell_default = arg->val; 45820253Sjoerg 45963600Sdavidn if ((arg = getarg(args, 'w')) != NULL) 46063600Sdavidn cnf->default_password = boolean_val(arg->val, cnf->default_password); 46120253Sjoerg if (mode == M_ADD && getarg(args, 'D')) { 462284135Sbapt if (name != NULL) 46330259Scharnier errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 46420253Sjoerg if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 46520253Sjoerg if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 46620253Sjoerg cnf->min_uid = 1000; 46720253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 46820253Sjoerg cnf->max_uid = 32000; 46920253Sjoerg } 47020253Sjoerg if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 47120253Sjoerg if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 47220253Sjoerg cnf->min_gid = 1000; 47320253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 47420253Sjoerg cnf->max_gid = 32000; 47520253Sjoerg } 47620253Sjoerg 477284135Sbapt if (write_userconfig(conf.config)) 478284135Sbapt return (EXIT_SUCCESS); 479283814Sbapt err(EX_IOERR, "config udpate"); 48020253Sjoerg } 48152527Sdavidn 482284128Sbapt if (name != NULL) 483284128Sbapt pwd = GETPWNAM(pw_checkname(name, 0)); 48420253Sjoerg 485284128Sbapt if (id < 0 && name == NULL) 486284128Sbapt errx(EX_DATAERR, "user name or id required"); 48720253Sjoerg 48820253Sjoerg /* 489285401Sbapt * Update require that the user exists 49020253Sjoerg */ 491285405Sbapt if (mode == M_UPDATE) { 49252527Sdavidn 493284128Sbapt if (name == NULL && pwd == NULL) /* Try harder */ 494284128Sbapt pwd = GETPWUID(id); 49520253Sjoerg 49620253Sjoerg if (pwd == NULL) { 497284128Sbapt if (name == NULL) 498284128Sbapt errx(EX_NOUSER, "no such uid `%ld'", id); 499284128Sbapt errx(EX_NOUSER, "no such user `%s'", name); 50020253Sjoerg } 50152527Sdavidn 502284128Sbapt if (name == NULL) 503284128Sbapt name = pwd->pw_name; 50420253Sjoerg 50520253Sjoerg /* 50620253Sjoerg * The rest is edit code 50720253Sjoerg */ 508284129Sbapt if (conf.newname != NULL) { 50920253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 51030259Scharnier errx(EX_DATAERR, "can't rename `root' account"); 511284129Sbapt pwd->pw_name = pw_checkname(conf.newname, 0); 51252527Sdavidn edited = 1; 51320253Sjoerg } 51452527Sdavidn 515284133Sbapt if (id > 0 && isdigit((unsigned char)*arg->val)) { 516284133Sbapt pwd->pw_uid = (uid_t)id; 51752527Sdavidn edited = 1; 51820253Sjoerg if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 51930259Scharnier errx(EX_DATAERR, "can't change uid of `root' account"); 52020253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 52130259Scharnier warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 52220253Sjoerg } 52320253Sjoerg 52452527Sdavidn if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */ 52552527Sdavidn gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid; 52652527Sdavidn if (newgid != pwd->pw_gid) { 52752527Sdavidn edited = 1; 52861762Sdavidn pwd->pw_gid = newgid; 52952527Sdavidn } 53052527Sdavidn } 53152527Sdavidn 53220253Sjoerg if ((arg = getarg(args, 'p')) != NULL) { 53352527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 53452527Sdavidn if (pwd->pw_change != 0) { 53552527Sdavidn pwd->pw_change = 0; 53652527Sdavidn edited = 1; 53752527Sdavidn } 53852527Sdavidn } 53920253Sjoerg else { 54020253Sjoerg time_t now = time(NULL); 54120253Sjoerg time_t expire = parse_date(now, arg->val); 54220253Sjoerg 54352527Sdavidn if (pwd->pw_change != expire) { 54452527Sdavidn pwd->pw_change = expire; 54552527Sdavidn edited = 1; 54652527Sdavidn } 54720253Sjoerg } 54820253Sjoerg } 54952527Sdavidn 55020267Sjoerg if ((arg = getarg(args, 'e')) != NULL) { 55152527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 55252527Sdavidn if (pwd->pw_expire != 0) { 55352527Sdavidn pwd->pw_expire = 0; 55452527Sdavidn edited = 1; 55552527Sdavidn } 55652527Sdavidn } 55720253Sjoerg else { 55820253Sjoerg time_t now = time(NULL); 55920253Sjoerg time_t expire = parse_date(now, arg->val); 56020253Sjoerg 56152527Sdavidn if (pwd->pw_expire != expire) { 56252527Sdavidn pwd->pw_expire = expire; 56352527Sdavidn edited = 1; 56452527Sdavidn } 56520253Sjoerg } 56620253Sjoerg } 56720253Sjoerg 56852527Sdavidn if ((arg = getarg(args, 's')) != NULL) { 56952527Sdavidn char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 57052527Sdavidn if (shell == NULL) 57152527Sdavidn shell = ""; 57252527Sdavidn if (strcmp(shell, pwd->pw_shell) != 0) { 57352527Sdavidn pwd->pw_shell = shell; 57452527Sdavidn edited = 1; 57552527Sdavidn } 57652527Sdavidn } 57720253Sjoerg 57852527Sdavidn if (getarg(args, 'L')) { 57952527Sdavidn if (cnf->default_class == NULL) 58052527Sdavidn cnf->default_class = ""; 58152527Sdavidn if (strcmp(pwd->pw_class, cnf->default_class) != 0) { 58252527Sdavidn pwd->pw_class = cnf->default_class; 58352527Sdavidn edited = 1; 58452527Sdavidn } 58552527Sdavidn } 58652527Sdavidn 58720747Sdavidn if ((arg = getarg(args, 'd')) != NULL) { 588130629Srobert if (strcmp(pwd->pw_dir, arg->val)) 589130629Srobert edited = 1; 59020747Sdavidn if (stat(pwd->pw_dir = arg->val, &st) == -1) { 59120747Sdavidn if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 59230259Scharnier warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 59320747Sdavidn } else if (!S_ISDIR(st.st_mode)) 59430259Scharnier warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 59520747Sdavidn } 59620747Sdavidn 597285137Sbapt if ((arg = getarg(args, 'w')) != NULL && conf.fd == -1) { 59864918Sgreen login_cap_t *lc; 59964918Sgreen 60064918Sgreen lc = login_getpwclass(pwd); 60164918Sgreen if (lc == NULL || 602252688Sdes login_setcryptfmt(lc, "sha512", NULL) == NULL) 60364918Sgreen warn("setting crypt(3) format"); 60464918Sgreen login_close(lc); 605285409Sbapt pwd->pw_passwd = pw_password(cnf, pwd->pw_name); 60652527Sdavidn edited = 1; 60752527Sdavidn } 60820267Sjoerg 60920253Sjoerg } else { 61064918Sgreen login_cap_t *lc; 61152527Sdavidn 61252527Sdavidn /* 61352527Sdavidn * Add code 61452527Sdavidn */ 61552527Sdavidn 616284128Sbapt if (name == NULL) /* Required */ 61730259Scharnier errx(EX_DATAERR, "login name required"); 618284128Sbapt else if ((pwd = GETPWNAM(name)) != NULL) /* Exists */ 619284128Sbapt errx(EX_DATAERR, "login name `%s' already exists", name); 62020253Sjoerg 62120253Sjoerg /* 62220253Sjoerg * Now, set up defaults for a new user 62320253Sjoerg */ 62420253Sjoerg pwd = &fakeuser; 625284128Sbapt pwd->pw_name = name; 62620253Sjoerg pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 627284133Sbapt pwd->pw_uid = pw_uidpolicy(cnf, id); 628284118Sbapt pwd->pw_gid = pw_gidpolicy(args, pwd->pw_name, (gid_t) pwd->pw_uid); 62920253Sjoerg pwd->pw_change = pw_pwdpolicy(cnf, args); 63020253Sjoerg pwd->pw_expire = pw_exppolicy(cnf, args); 63120253Sjoerg pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 63220253Sjoerg pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 63364918Sgreen lc = login_getpwclass(pwd); 634272833Sdes if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) 63564918Sgreen warn("setting crypt(3) format"); 63664918Sgreen login_close(lc); 637285409Sbapt pwd->pw_passwd = pw_password(cnf, pwd->pw_name); 63852527Sdavidn edited = 1; 63920253Sjoerg 64020253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 64130259Scharnier warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 64220253Sjoerg } 64320253Sjoerg 64420253Sjoerg /* 64520253Sjoerg * Shared add/edit code 64620253Sjoerg */ 647285408Sbapt if (conf.gecos != NULL) { 648285408Sbapt if (strcmp(pwd->pw_gecos, conf.gecos) != 0) { 649285408Sbapt pwd->pw_gecos = conf.gecos; 65052527Sdavidn edited = 1; 65152527Sdavidn } 65252527Sdavidn } 65320253Sjoerg 654285133Sbapt if (conf.fd != -1) 655285137Sbapt edited = set_passwd(pwd, mode == M_UPDATE); 65620253Sjoerg 65720267Sjoerg /* 65820267Sjoerg * Special case: -N only displays & exits 65920267Sjoerg */ 660284121Sbapt if (conf.dryrun) 661284126Sbapt return print_user(pwd); 66220267Sjoerg 66321330Sdavidn if (mode == M_ADD) { 66452527Sdavidn edited = 1; /* Always */ 66552502Sdavidn rc = addpwent(pwd); 666283814Sbapt if (rc == -1) 667283814Sbapt errx(EX_IOERR, "user '%s' already exists", 668283814Sbapt pwd->pw_name); 669283814Sbapt else if (rc != 0) 670283814Sbapt err(EX_IOERR, "passwd file update"); 67152502Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/') { 67252502Sdavidn rc = addnispwent(cnf->nispasswd, pwd); 67352502Sdavidn if (rc == -1) 67452502Sdavidn warnx("User '%s' already exists in NIS passwd", pwd->pw_name); 67552502Sdavidn else 67656000Sdavidn warn("NIS passwd update"); 67752502Sdavidn /* NOTE: we treat NIS-only update errors as non-fatal */ 67852502Sdavidn } 679285405Sbapt } else if (mode == M_UPDATE && edited) /* Only updated this if required */ 680285405Sbapt perform_chgpwent(name, pwd); 68121330Sdavidn 68220253Sjoerg /* 68320253Sjoerg * Ok, user is created or changed - now edit group file 68420253Sjoerg */ 68520253Sjoerg 686242349Sbapt if (mode == M_ADD || getarg(args, 'G') != NULL) { 687285412Sbapt int j; 688285412Sbapt size_t i; 689273779Sbapt /* First remove the user from all group */ 690273779Sbapt SETGRENT(); 691273779Sbapt while ((grp = GETGRENT()) != NULL) { 692273779Sbapt char group[MAXLOGNAME]; 693273779Sbapt if (grp->gr_mem == NULL) 694273779Sbapt continue; 695273779Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 696273779Sbapt if (strcmp(grp->gr_mem[i] , pwd->pw_name) != 0) 697273779Sbapt continue; 698273779Sbapt for (j = i; grp->gr_mem[j] != NULL ; j++) 699273779Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 700273779Sbapt strlcpy(group, grp->gr_name, MAXLOGNAME); 701273779Sbapt chggrent(group, grp); 702273779Sbapt } 703273779Sbapt } 704273779Sbapt ENDGRENT(); 705273779Sbapt 706273779Sbapt /* now add to group where needed */ 707285412Sbapt for (i = 0; i < cnf->groups->sl_cur; i++) { 708285412Sbapt grp = GETGRNAM(cnf->groups->sl_str[i]); 709244737Sbapt grp = gr_add(grp, pwd->pw_name); 710244737Sbapt /* 711244737Sbapt * grp can only be NULL in 2 cases: 712244737Sbapt * - the new member is already a member 713244737Sbapt * - a problem with memory occurs 714244737Sbapt * in both cases we want to skip now. 715244737Sbapt */ 716244737Sbapt if (grp == NULL) 717242349Sbapt continue; 718285412Sbapt chggrent(grp->gr_name, grp); 719245114Smjg free(grp); 720242349Sbapt } 721242349Sbapt } 722242349Sbapt 723242349Sbapt 72461759Sdavidn /* go get a current version of pwd */ 725284128Sbapt pwd = GETPWNAM(name); 72661759Sdavidn if (pwd == NULL) { 72761759Sdavidn /* This will fail when we rename, so special case that */ 728284129Sbapt if (mode == M_UPDATE && conf.newname != NULL) { 729284129Sbapt name = conf.newname; /* update new name */ 730284128Sbapt pwd = GETPWNAM(name); /* refetch renamed rec */ 73161759Sdavidn } 73261759Sdavidn } 73361759Sdavidn if (pwd == NULL) /* can't go on without this */ 734284128Sbapt errx(EX_NOUSER, "user '%s' disappeared during update", name); 73520253Sjoerg 73644229Sdavidn grp = GETGRGID(pwd->pw_gid); 737283842Sbapt pw_log(cnf, mode, W_USER, "%s(%u):%s(%u):%s:%s:%s", 738283842Sbapt pwd->pw_name, pwd->pw_uid, 739283842Sbapt grp ? grp->gr_name : "unknown", (grp ? grp->gr_gid : (uid_t)-1), 74020253Sjoerg pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 74120253Sjoerg 74220253Sjoerg /* 74320253Sjoerg * If adding, let's touch and chown the user's mail file. This is not 74420253Sjoerg * strictly necessary under BSD with a 0755 maildir but it also 74520253Sjoerg * doesn't hurt anything to create the empty mailfile 74620253Sjoerg */ 74720253Sjoerg if (mode == M_ADD) { 748283961Sbapt if (PWALTDIR() != PWF_ALT) { 749283961Sbapt arg = getarg(args, 'R'); 750283961Sbapt snprintf(path, sizeof(path), "%s%s/%s", 751283961Sbapt arg ? arg->val : "", _PATH_MAILDIR, pwd->pw_name); 752283961Sbapt close(open(path, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 75344229Sdavidn * mtime */ 754283961Sbapt chown(path, pwd->pw_uid, pwd->pw_gid); 75520253Sjoerg } 75620253Sjoerg } 75752527Sdavidn 75820253Sjoerg /* 75982868Sdd * Let's create and populate the user's home directory. Note 76020253Sjoerg * that this also `works' for editing users if -m is used, but 76120253Sjoerg * existing files will *not* be overwritten. 76220253Sjoerg */ 763283961Sbapt if (PWALTDIR() != PWF_ALT && getarg(args, 'm') != NULL && pwd->pw_dir && 764283961Sbapt *pwd->pw_dir == '/' && pwd->pw_dir[1]) 765285403Sbapt create_and_populate_homedir(pwd); 76652527Sdavidn 76782868Sdd /* 76882868Sdd * Finally, send mail to the new user as well, if we are asked to 76982868Sdd */ 77082868Sdd if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 77182868Sdd FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 77282868Sdd 77382868Sdd if (pfp == NULL) 77482868Sdd warn("sendmail"); 77582868Sdd else { 77682868Sdd fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 77782868Sdd while (fgets(line, sizeof(line), fp) != NULL) { 77882868Sdd /* Do substitutions? */ 77982868Sdd fputs(line, pfp); 78082868Sdd } 78182868Sdd pclose(pfp); 782283842Sbapt pw_log(cnf, mode, W_USER, "%s(%u) new user mail sent", 783283842Sbapt pwd->pw_name, pwd->pw_uid); 78482868Sdd } 78582868Sdd fclose(fp); 78682868Sdd } 78782868Sdd 78820267Sjoerg return EXIT_SUCCESS; 78920253Sjoerg} 79020253Sjoerg 79120253Sjoerg 79220253Sjoergstatic uid_t 793284133Sbaptpw_uidpolicy(struct userconf * cnf, long id) 79420253Sjoerg{ 79520253Sjoerg struct passwd *pwd; 79620253Sjoerg uid_t uid = (uid_t) - 1; 79720253Sjoerg 79820253Sjoerg /* 79920253Sjoerg * Check the given uid, if any 80020253Sjoerg */ 801284133Sbapt if (id > 0) { 802284133Sbapt uid = (uid_t) id; 80320253Sjoerg 804284133Sbapt if ((pwd = GETPWUID(uid)) != NULL && conf.checkduplicate) 805283842Sbapt errx(EX_DATAERR, "uid `%u' has already been allocated", pwd->pw_uid); 80620253Sjoerg } else { 80720253Sjoerg struct bitmap bm; 80820253Sjoerg 80920253Sjoerg /* 81020253Sjoerg * We need to allocate the next available uid under one of 81120253Sjoerg * two policies a) Grab the first unused uid b) Grab the 81220253Sjoerg * highest possible unused uid 81320253Sjoerg */ 81420253Sjoerg if (cnf->min_uid >= cnf->max_uid) { /* Sanity 81520253Sjoerg * claus^H^H^H^Hheck */ 81620253Sjoerg cnf->min_uid = 1000; 81720253Sjoerg cnf->max_uid = 32000; 81820253Sjoerg } 81920253Sjoerg bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 82020253Sjoerg 82120253Sjoerg /* 82220253Sjoerg * Now, let's fill the bitmap from the password file 82320253Sjoerg */ 82444229Sdavidn SETPWENT(); 82544229Sdavidn while ((pwd = GETPWENT()) != NULL) 82644229Sdavidn if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 82720253Sjoerg bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 82844229Sdavidn ENDPWENT(); 82920253Sjoerg 83020253Sjoerg /* 83120253Sjoerg * Then apply the policy, with fallback to reuse if necessary 83220253Sjoerg */ 83320253Sjoerg if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 83420253Sjoerg uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 83520253Sjoerg 83620253Sjoerg /* 83720253Sjoerg * Another sanity check 83820253Sjoerg */ 83920253Sjoerg if (uid < cnf->min_uid || uid > cnf->max_uid) 84030259Scharnier errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 84120253Sjoerg bm_dealloc(&bm); 84220253Sjoerg } 84320253Sjoerg return uid; 84420253Sjoerg} 84520253Sjoerg 84620253Sjoerg 84720253Sjoergstatic uid_t 848284118Sbaptpw_gidpolicy(struct cargs * args, char *nam, gid_t prefer) 84920253Sjoerg{ 85020253Sjoerg struct group *grp; 85120253Sjoerg gid_t gid = (uid_t) - 1; 85220253Sjoerg struct carg *a_gid = getarg(args, 'g'); 853284118Sbapt struct userconf *cnf = conf.userconf; 85420253Sjoerg 85520253Sjoerg /* 85620253Sjoerg * If no arg given, see if default can help out 85720253Sjoerg */ 85820253Sjoerg if (a_gid == NULL && cnf->default_group && *cnf->default_group) 85920253Sjoerg a_gid = addarg(args, 'g', cnf->default_group); 86020253Sjoerg 86120253Sjoerg /* 86220253Sjoerg * Check the given gid, if any 86320253Sjoerg */ 86444229Sdavidn SETGRENT(); 86520253Sjoerg if (a_gid != NULL) { 86644229Sdavidn if ((grp = GETGRNAM(a_gid->val)) == NULL) { 86720253Sjoerg gid = (gid_t) atol(a_gid->val); 86861957Sache if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL) 86930259Scharnier errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 87020253Sjoerg } 87120253Sjoerg gid = grp->gr_gid; 872262865Sjulian } else if ((grp = GETGRNAM(nam)) != NULL && 873262865Sjulian (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) { 87420267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 87520253Sjoerg } else { 876285414Sbapt gid_t grid = -1; 87720253Sjoerg 87820253Sjoerg /* 87920253Sjoerg * We need to auto-create a group with the user's name. We 88020253Sjoerg * can send all the appropriate output to our sister routine 88120253Sjoerg * bit first see if we can create a group with gid==uid so we 88220253Sjoerg * can keep the user and group ids in sync. We purposely do 88320253Sjoerg * NOT check the gid range if we can force the sync. If the 88420253Sjoerg * user's name dups an existing group, then the group add 88520253Sjoerg * function will happily handle that case for us and exit. 88620253Sjoerg */ 887285414Sbapt if (GETGRGID(prefer) == NULL) 888285414Sbapt grid = prefer; 889284121Sbapt if (conf.dryrun) { 890285395Sbapt gid = pw_groupnext(cnf, true); 891285395Sbapt } else { 892285415Sbapt pw_group(M_ADD, nam, grid, NULL); 89344229Sdavidn if ((grp = GETGRNAM(nam)) != NULL) 89420267Sjoerg gid = grp->gr_gid; 89520267Sjoerg } 89620253Sjoerg } 89744229Sdavidn ENDGRENT(); 89820253Sjoerg return gid; 89920253Sjoerg} 90020253Sjoerg 90120253Sjoerg 90220253Sjoergstatic time_t 90320253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args) 90420253Sjoerg{ 90520253Sjoerg time_t result = 0; 90620253Sjoerg time_t now = time(NULL); 90727831Sdavidn struct carg *arg = getarg(args, 'p'); 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->password_days > 0) 91320253Sjoerg result = now + ((long) cnf->password_days * 86400L); 91420253Sjoerg return result; 91520253Sjoerg} 91620253Sjoerg 91720253Sjoerg 91820253Sjoergstatic time_t 91920253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args) 92020253Sjoerg{ 92120253Sjoerg time_t result = 0; 92220253Sjoerg time_t now = time(NULL); 92320253Sjoerg struct carg *arg = getarg(args, 'e'); 92420253Sjoerg 92520253Sjoerg if (arg != NULL) { 92620253Sjoerg if ((result = parse_date(now, arg->val)) == now) 92730259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 92820253Sjoerg } else if (cnf->expire_days > 0) 92920253Sjoerg result = now + ((long) cnf->expire_days * 86400L); 93020253Sjoerg return result; 93120253Sjoerg} 93220253Sjoerg 93320253Sjoerg 93420253Sjoergstatic char * 93520253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 93620253Sjoerg{ 93720253Sjoerg struct carg *arg = getarg(args, 'd'); 938282699Sbapt static char home[128]; 93920253Sjoerg 94020253Sjoerg if (arg) 941282699Sbapt return (arg->val); 94220253Sjoerg 943282699Sbapt if (cnf->home == NULL || *cnf->home == '\0') 944282699Sbapt errx(EX_CONFIG, "no base home directory set"); 945282699Sbapt snprintf(home, sizeof(home), "%s/%s", cnf->home, user); 946282699Sbapt 947282699Sbapt return (home); 94820253Sjoerg} 94920253Sjoerg 95020253Sjoergstatic char * 95120253Sjoergshell_path(char const * path, char *shells[], char *sh) 95220253Sjoerg{ 95320253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 95420253Sjoerg return sh; /* specified full path or forced none */ 95520253Sjoerg else { 95620253Sjoerg char *p; 95720253Sjoerg char paths[_UC_MAXLINE]; 95820253Sjoerg 95920253Sjoerg /* 96020253Sjoerg * We need to search paths 96120253Sjoerg */ 962130633Srobert strlcpy(paths, path, sizeof(paths)); 96320253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 96420253Sjoerg int i; 96520253Sjoerg static char shellpath[256]; 96620253Sjoerg 96720253Sjoerg if (sh != NULL) { 968282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh); 96920253Sjoerg if (access(shellpath, X_OK) == 0) 97020253Sjoerg return shellpath; 97120253Sjoerg } else 97220253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 973282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]); 97420253Sjoerg if (access(shellpath, X_OK) == 0) 97520253Sjoerg return shellpath; 97620253Sjoerg } 97720253Sjoerg } 97820253Sjoerg if (sh == NULL) 97930259Scharnier errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 98030259Scharnier errx(EX_CONFIG, "no default shell available or defined"); 98120253Sjoerg return NULL; 98220253Sjoerg } 98320253Sjoerg} 98420253Sjoerg 98520253Sjoerg 98620253Sjoergstatic char * 98720253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 98820253Sjoerg{ 98920253Sjoerg char *sh = newshell; 99020253Sjoerg struct carg *arg = getarg(args, 's'); 99120253Sjoerg 99220253Sjoerg if (newshell == NULL && arg != NULL) 99320253Sjoerg sh = arg->val; 99420253Sjoerg return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 99520253Sjoerg} 99620253Sjoerg 997179365Santoine#define SALTSIZE 32 99820253Sjoerg 999179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"; 1000179365Santoine 100120253Sjoergchar * 100220253Sjoergpw_pwcrypt(char *password) 100320253Sjoerg{ 100420253Sjoerg int i; 1005179365Santoine char salt[SALTSIZE + 1]; 1006231994Skevlo char *cryptpw; 100720253Sjoerg 100820253Sjoerg static char buf[256]; 100920253Sjoerg 101020253Sjoerg /* 101120253Sjoerg * Calculate a salt value 101220253Sjoerg */ 1013179365Santoine for (i = 0; i < SALTSIZE; i++) 1014181785Sache salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)]; 1015179365Santoine salt[SALTSIZE] = '\0'; 101620253Sjoerg 1017231994Skevlo cryptpw = crypt(password, salt); 1018231994Skevlo if (cryptpw == NULL) 1019231994Skevlo errx(EX_CONFIG, "crypt(3) failure"); 1020231994Skevlo return strcpy(buf, cryptpw); 102120253Sjoerg} 102220253Sjoerg 102320590Sdavidn 102420253Sjoergstatic char * 1025285409Sbaptpw_password(struct userconf * cnf, char const * user) 102620253Sjoerg{ 102720253Sjoerg int i, l; 102820253Sjoerg char pwbuf[32]; 102920253Sjoerg 103020253Sjoerg switch (cnf->default_password) { 103120253Sjoerg case -1: /* Random password */ 103273563Skris l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 103320253Sjoerg for (i = 0; i < l; i++) 1034181785Sache pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)]; 103520253Sjoerg pwbuf[i] = '\0'; 103620253Sjoerg 103720253Sjoerg /* 103820253Sjoerg * We give this information back to the user 103920253Sjoerg */ 1040285137Sbapt if (conf.fd == -1 && !conf.dryrun) { 104161957Sache if (isatty(STDOUT_FILENO)) 104220712Sdavidn printf("Password for '%s' is: ", user); 104320253Sjoerg printf("%s\n", pwbuf); 104420253Sjoerg fflush(stdout); 104520253Sjoerg } 104620253Sjoerg break; 104720253Sjoerg 104820253Sjoerg case -2: /* No password at all! */ 104920253Sjoerg return ""; 105020253Sjoerg 105120253Sjoerg case 0: /* No login - default */ 105220253Sjoerg default: 105320253Sjoerg return "*"; 105420253Sjoerg 105520253Sjoerg case 1: /* user's name */ 1056130633Srobert strlcpy(pwbuf, user, sizeof(pwbuf)); 105720253Sjoerg break; 105820253Sjoerg } 105920253Sjoerg return pw_pwcrypt(pwbuf); 106020253Sjoerg} 106120253Sjoerg 1062284111Sbaptstatic int 1063285401Sbaptpw_userdel(char *name, long id) 1064284111Sbapt{ 1065285401Sbapt struct passwd *pwd = NULL; 1066284111Sbapt char file[MAXPATHLEN]; 1067284111Sbapt char home[MAXPATHLEN]; 1068285401Sbapt uid_t uid; 1069284111Sbapt struct group *gr, *grp; 1070284111Sbapt char grname[LOGNAMESIZE]; 1071284111Sbapt int rc; 1072284111Sbapt struct stat st; 107320253Sjoerg 1074285401Sbapt if (id < 0 && name == NULL) 1075285401Sbapt errx(EX_DATAERR, "username or id required"); 1076285401Sbapt 1077285401Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 1078285401Sbapt if (pwd == NULL) { 1079285401Sbapt if (name == NULL) 1080285401Sbapt errx(EX_NOUSER, "no such uid `%ld'", id); 1081285401Sbapt errx(EX_NOUSER, "no such user `%s'", name); 1082285401Sbapt } 1083285401Sbapt uid = pwd->pw_uid; 1084285401Sbapt if (name == NULL) 1085285401Sbapt name = pwd->pw_name; 1086285401Sbapt 1087284111Sbapt if (strcmp(pwd->pw_name, "root") == 0) 1088284111Sbapt errx(EX_DATAERR, "cannot remove user 'root'"); 1089284111Sbapt 1090284111Sbapt if (!PWALTDIR()) { 1091284111Sbapt /* 1092284111Sbapt * Remove opie record from /etc/opiekeys 1093284111Sbapt */ 1094284111Sbapt 1095284111Sbapt rmopie(pwd->pw_name); 1096284111Sbapt 1097284111Sbapt /* 1098284111Sbapt * Remove crontabs 1099284111Sbapt */ 1100284111Sbapt snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name); 1101284111Sbapt if (access(file, F_OK) == 0) { 1102284111Sbapt snprintf(file, sizeof(file), "crontab -u %s -r", pwd->pw_name); 1103284111Sbapt system(file); 1104284111Sbapt } 1105284111Sbapt } 1106284111Sbapt /* 1107284111Sbapt * Save these for later, since contents of pwd may be 1108284111Sbapt * invalidated by deletion 1109284111Sbapt */ 1110284111Sbapt snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name); 1111284111Sbapt strlcpy(home, pwd->pw_dir, sizeof(home)); 1112284111Sbapt gr = GETGRGID(pwd->pw_gid); 1113284111Sbapt if (gr != NULL) 1114284111Sbapt strlcpy(grname, gr->gr_name, LOGNAMESIZE); 1115284111Sbapt else 1116284111Sbapt grname[0] = '\0'; 1117284111Sbapt 1118284111Sbapt rc = delpwent(pwd); 1119284111Sbapt if (rc == -1) 1120284111Sbapt err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 1121284111Sbapt else if (rc != 0) 1122284111Sbapt err(EX_IOERR, "passwd update"); 1123284111Sbapt 1124285401Sbapt if (conf.userconf->nispasswd && *conf.userconf->nispasswd=='/') { 1125285401Sbapt rc = delnispwent(conf.userconf->nispasswd, name); 1126284111Sbapt if (rc == -1) 1127285401Sbapt warnx("WARNING: user '%s' does not exist in NIS passwd", 1128285401Sbapt pwd->pw_name); 1129284111Sbapt else if (rc != 0) 1130284111Sbapt warn("WARNING: NIS passwd update"); 1131284111Sbapt /* non-fatal */ 1132284111Sbapt } 1133284111Sbapt 1134284128Sbapt grp = GETGRNAM(name); 1135284111Sbapt if (grp != NULL && 1136284111Sbapt (grp->gr_mem == NULL || *grp->gr_mem == NULL) && 1137284128Sbapt strcmp(name, grname) == 0) 1138284128Sbapt delgrent(GETGRNAM(name)); 1139284111Sbapt SETGRENT(); 1140284111Sbapt while ((grp = GETGRENT()) != NULL) { 1141284111Sbapt int i, j; 1142284111Sbapt char group[MAXLOGNAME]; 1143284113Sbapt if (grp->gr_mem == NULL) 1144284113Sbapt continue; 1145284113Sbapt 1146284113Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 1147284128Sbapt if (strcmp(grp->gr_mem[i], name) != 0) 1148284113Sbapt continue; 1149284113Sbapt 1150284113Sbapt for (j = i; grp->gr_mem[j] != NULL; j++) 1151284113Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 1152284113Sbapt strlcpy(group, grp->gr_name, MAXLOGNAME); 1153284113Sbapt chggrent(group, grp); 1154284111Sbapt } 1155284111Sbapt } 1156284111Sbapt ENDGRENT(); 1157284111Sbapt 1158285401Sbapt pw_log(conf.userconf, M_DELETE, W_USER, "%s(%u) account removed", name, 1159285401Sbapt uid); 1160284111Sbapt 1161284117Sbapt if (!PWALTDIR()) { 1162284111Sbapt /* 1163284111Sbapt * Remove mail file 1164284111Sbapt */ 1165284111Sbapt remove(file); 1166284111Sbapt 1167284111Sbapt /* 1168284111Sbapt * Remove at jobs 1169284111Sbapt */ 1170284111Sbapt if (getpwuid(uid) == NULL) 1171284111Sbapt rmat(uid); 1172284111Sbapt 1173284111Sbapt /* 1174284111Sbapt * Remove home directory and contents 1175284111Sbapt */ 1176285401Sbapt if (conf.deletehome && *home == '/' && getpwuid(uid) == NULL && 1177284112Sbapt stat(home, &st) != -1) { 1178284112Sbapt rm_r(home, uid); 1179285401Sbapt pw_log(conf.userconf, M_DELETE, W_USER, "%s(%u) home '%s' %sremoved", 1180284128Sbapt name, uid, home, 1181284112Sbapt stat(home, &st) == -1 ? "" : "not completely "); 1182284111Sbapt } 1183284111Sbapt } 1184284111Sbapt 1185284111Sbapt return (EXIT_SUCCESS); 1186284111Sbapt} 1187284111Sbapt 118820253Sjoergstatic int 1189284124Sbaptprint_user(struct passwd * pwd) 119020253Sjoerg{ 1191284122Sbapt if (!conf.pretty) { 1192242349Sbapt char *buf; 119320253Sjoerg 1194284124Sbapt buf = conf.v7 ? pw_make_v7(pwd) : pw_make(pwd); 1195242349Sbapt printf("%s\n", buf); 1196242349Sbapt free(buf); 119720253Sjoerg } else { 119820267Sjoerg int j; 119920253Sjoerg char *p; 120044229Sdavidn struct group *grp = GETGRGID(pwd->pw_gid); 120120253Sjoerg char uname[60] = "User &", office[60] = "[None]", 120220253Sjoerg wphone[60] = "[None]", hphone[60] = "[None]"; 120320590Sdavidn char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 120420590Sdavidn struct tm * tptr; 120520253Sjoerg 120620253Sjoerg if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 1207130633Srobert strlcpy(uname, p, sizeof(uname)); 120820253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1209130633Srobert strlcpy(office, p, sizeof(office)); 121020253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1211130633Srobert strlcpy(wphone, p, sizeof(wphone)); 121220253Sjoerg if ((p = strtok(NULL, "")) != NULL) { 1213130633Srobert strlcpy(hphone, p, 1214130633Srobert sizeof(hphone)); 121520253Sjoerg } 121620253Sjoerg } 121720253Sjoerg } 121820253Sjoerg } 121920253Sjoerg /* 122020253Sjoerg * Handle '&' in gecos field 122120253Sjoerg */ 122220253Sjoerg if ((p = strchr(uname, '&')) != NULL) { 122320253Sjoerg int l = strlen(pwd->pw_name); 122420253Sjoerg int m = strlen(p); 122520253Sjoerg 122620253Sjoerg memmove(p + l, p + 1, m); 122720253Sjoerg memmove(p, pwd->pw_name, l); 122861957Sache *p = (char) toupper((unsigned char)*p); 122920253Sjoerg } 123020590Sdavidn if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 123174569Sache strftime(acexpire, sizeof acexpire, "%c", tptr); 123261957Sache if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 123374569Sache strftime(pwexpire, sizeof pwexpire, "%c", tptr); 1234283842Sbapt printf("Login Name: %-15s #%-12u Group: %-15s #%u\n" 123520747Sdavidn " Full Name: %s\n" 123620747Sdavidn " Home: %-26.26s Class: %s\n" 123720747Sdavidn " Shell: %-26.26s Office: %s\n" 123820747Sdavidn "Work Phone: %-26.26s Home Phone: %s\n" 123920747Sdavidn "Acc Expire: %-26.26s Pwd Expire: %s\n", 1240283842Sbapt pwd->pw_name, pwd->pw_uid, 1241283842Sbapt grp ? grp->gr_name : "(invalid)", pwd->pw_gid, 124220253Sjoerg uname, pwd->pw_dir, pwd->pw_class, 124320590Sdavidn pwd->pw_shell, office, wphone, hphone, 124420590Sdavidn acexpire, pwexpire); 124544229Sdavidn SETGRENT(); 124620267Sjoerg j = 0; 124744229Sdavidn while ((grp=GETGRENT()) != NULL) 124820267Sjoerg { 124920267Sjoerg int i = 0; 1250262865Sjulian if (grp->gr_mem != NULL) { 1251262865Sjulian while (grp->gr_mem[i] != NULL) 125220267Sjoerg { 1253262865Sjulian if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 1254262865Sjulian { 1255262865Sjulian printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 1256262865Sjulian break; 1257262865Sjulian } 1258262865Sjulian ++i; 125920267Sjoerg } 126020267Sjoerg } 126120267Sjoerg } 126244229Sdavidn ENDGRENT(); 126361957Sache printf("%s", j ? "\n" : ""); 126420253Sjoerg } 126520267Sjoerg return EXIT_SUCCESS; 126620253Sjoerg} 126720253Sjoerg 1268284110Sbaptchar * 1269284110Sbaptpw_checkname(char *name, int gecos) 127020253Sjoerg{ 1271109961Sgad char showch[8]; 1272284110Sbapt const char *badchars, *ch, *showtype; 1273109961Sgad int reject; 127420253Sjoerg 1275109961Sgad ch = name; 1276109961Sgad reject = 0; 1277109961Sgad if (gecos) { 1278109961Sgad /* See if the name is valid as a gecos (comment) field. */ 1279109961Sgad badchars = ":!@"; 1280109961Sgad showtype = "gecos field"; 1281109961Sgad } else { 1282109961Sgad /* See if the name is valid as a userid or group. */ 1283109961Sgad badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\""; 1284109961Sgad showtype = "userid/group name"; 1285109961Sgad /* Userids and groups can not have a leading '-'. */ 1286109961Sgad if (*ch == '-') 1287109961Sgad reject = 1; 128820253Sjoerg } 1289109961Sgad if (!reject) { 1290109961Sgad while (*ch) { 1291109961Sgad if (strchr(badchars, *ch) != NULL || *ch < ' ' || 1292109961Sgad *ch == 127) { 1293109961Sgad reject = 1; 1294109961Sgad break; 1295109961Sgad } 1296109961Sgad /* 8-bit characters are only allowed in GECOS fields */ 1297109961Sgad if (!gecos && (*ch & 0x80)) { 1298109961Sgad reject = 1; 1299109961Sgad break; 1300109961Sgad } 1301109961Sgad ch++; 1302109961Sgad } 1303109961Sgad } 1304109961Sgad /* 1305109961Sgad * A `$' is allowed as the final character for userids and groups, 1306109961Sgad * mainly for the benefit of samba. 1307109961Sgad */ 1308109961Sgad if (reject && !gecos) { 1309109961Sgad if (*ch == '$' && *(ch + 1) == '\0') { 1310109961Sgad reject = 0; 1311109961Sgad ch++; 1312109961Sgad } 1313109961Sgad } 1314109961Sgad if (reject) { 1315109961Sgad snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127) 1316109961Sgad ? "`%c'" : "0x%02x", *ch); 1317228673Sdim errx(EX_DATAERR, "invalid character %s at position %td in %s", 1318109961Sgad showch, (ch - name), showtype); 1319109961Sgad } 1320109961Sgad if (!gecos && (ch - name) > LOGNAMESIZE) 1321109961Sgad errx(EX_DATAERR, "name too long `%s' (max is %d)", name, 1322109961Sgad LOGNAMESIZE); 1323284110Sbapt 1324284110Sbapt return (name); 132520253Sjoerg} 132620253Sjoerg 132720253Sjoerg 132820253Sjoergstatic void 132920253Sjoergrmat(uid_t uid) 133020253Sjoerg{ 133120253Sjoerg DIR *d = opendir("/var/at/jobs"); 133220253Sjoerg 133320253Sjoerg if (d != NULL) { 133420253Sjoerg struct dirent *e; 133520253Sjoerg 133620253Sjoerg while ((e = readdir(d)) != NULL) { 133720253Sjoerg struct stat st; 133820253Sjoerg 133920253Sjoerg if (strncmp(e->d_name, ".lock", 5) != 0 && 134020253Sjoerg stat(e->d_name, &st) == 0 && 134120253Sjoerg !S_ISDIR(st.st_mode) && 134220253Sjoerg st.st_uid == uid) { 134320253Sjoerg char tmp[MAXPATHLEN]; 134420253Sjoerg 1345282700Sbapt snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s", e->d_name); 134620253Sjoerg system(tmp); 134720253Sjoerg } 134820253Sjoerg } 134920253Sjoerg closedir(d); 135020253Sjoerg } 135120253Sjoerg} 135220747Sdavidn 135320747Sdavidnstatic void 135485145Sachermopie(char const * name) 135520747Sdavidn{ 135685145Sache static const char etcopie[] = "/etc/opiekeys"; 135785145Sache FILE *fp = fopen(etcopie, "r+"); 135820747Sdavidn 135921052Sdavidn if (fp != NULL) { 136021052Sdavidn char tmp[1024]; 136121052Sdavidn off_t atofs = 0; 136221052Sdavidn int length = strlen(name); 136320747Sdavidn 136421052Sdavidn while (fgets(tmp, sizeof tmp, fp) != NULL) { 136521052Sdavidn if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 136621052Sdavidn if (fseek(fp, atofs, SEEK_SET) == 0) { 136721052Sdavidn fwrite("#", 1, 1, fp); /* Comment username out */ 136821052Sdavidn } 136921052Sdavidn break; 137020747Sdavidn } 137121052Sdavidn atofs = ftell(fp); 137220747Sdavidn } 137321052Sdavidn /* 137421052Sdavidn * If we got an error of any sort, don't update! 137521052Sdavidn */ 137621052Sdavidn fclose(fp); 137720747Sdavidn } 137820747Sdavidn} 137920747Sdavidn 1380