pw_user.c revision 285406
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 285406 2015-07-11 20:18:34Z 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 68285403Sbaptcreate_and_populate_homedir(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); 84285403Sbapt pw_log(cnf, M_ADD, 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 194285405Sbaptstatic void 195285405Sbaptperform_chgpwent(const char *name, struct passwd *pwd) 196285405Sbapt{ 197285405Sbapt int rc; 198285405Sbapt 199285405Sbapt rc = chgpwent(name, pwd); 200285405Sbapt if (rc == -1) 201285405Sbapt errx(EX_IOERR, "user '%s' does not exist (NIS?)", pwd->pw_name); 202285405Sbapt else if (rc != 0) 203285405Sbapt err(EX_IOERR, "passwd file update"); 204285405Sbapt 205285405Sbapt if (conf.userconf->nispasswd && *conf.userconf->nispasswd == '/') { 206285405Sbapt rc = chgnispwent(conf.userconf->nispasswd, name, pwd); 207285405Sbapt if (rc == -1) 208285405Sbapt warn("User '%s' not found in NIS passwd", pwd->pw_name); 209285405Sbapt else 210285405Sbapt warn("NIS passwd update"); 211285405Sbapt /* NOTE: NIS-only update errors are not fatal */ 212285405Sbapt } 213285405Sbapt} 214285405Sbapt 215285405Sbapt/* 216285405Sbapt * The M_LOCK and M_UNLOCK functions simply add or remove 217285405Sbapt * a "*LOCKED*" prefix from in front of the password to 218285405Sbapt * prevent it decoding correctly, and therefore prevents 219285405Sbapt * access. Of course, this only prevents access via 220285405Sbapt * password authentication (not ssh, kerberos or any 221285405Sbapt * other method that does not use the UNIX password) but 222285405Sbapt * that is a known limitation. 223285405Sbapt */ 224285405Sbaptstatic int 225285405Sbaptpw_userlock(char *name, long id, int mode) 226285405Sbapt{ 227285405Sbapt struct passwd *pwd = NULL; 228285405Sbapt char *passtmp = NULL; 229285405Sbapt bool locked = false; 230285405Sbapt 231285405Sbapt if (id < 0 && name == NULL) 232285405Sbapt errx(EX_DATAERR, "username or id required"); 233285405Sbapt 234285405Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 235285405Sbapt if (pwd == NULL) { 236285405Sbapt if (name == NULL) 237285405Sbapt errx(EX_NOUSER, "no such uid `%ld'", id); 238285405Sbapt errx(EX_NOUSER, "no such user `%s'", name); 239285405Sbapt } 240285405Sbapt 241285405Sbapt if (name == NULL) 242285405Sbapt name = pwd->pw_name; 243285405Sbapt 244285405Sbapt if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str) -1) == 0) 245285405Sbapt locked = true; 246285405Sbapt if (mode == M_LOCK && locked) 247285405Sbapt errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 248285405Sbapt if (mode == M_UNLOCK && !locked) 249285405Sbapt errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 250285405Sbapt 251285405Sbapt if (mode == M_LOCK) { 252285405Sbapt asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd); 253285405Sbapt if (passtmp == NULL) /* disaster */ 254285405Sbapt errx(EX_UNAVAILABLE, "out of memory"); 255285405Sbapt pwd->pw_passwd = passtmp; 256285405Sbapt } else { 257285405Sbapt pwd->pw_passwd += sizeof(locked_str)-1; 258285405Sbapt } 259285405Sbapt 260285405Sbapt perform_chgpwent(name, pwd); 261285405Sbapt free(passtmp); 262285405Sbapt 263285405Sbapt return (EXIT_SUCCESS); 264285405Sbapt} 265285405Sbapt 26620253Sjoerg/*- 26720253Sjoerg * -C config configuration file 26820253Sjoerg * -q quiet operation 26920253Sjoerg * -n name login name 27020253Sjoerg * -u uid user id 27120253Sjoerg * -c comment user name/comment 27220253Sjoerg * -d directory home directory 27320253Sjoerg * -e date account expiry date 27420253Sjoerg * -p date password expiry date 27520253Sjoerg * -g grp primary group 27620253Sjoerg * -G grp1,grp2 additional groups 27720253Sjoerg * -m [ -k dir ] create and set up home 27820253Sjoerg * -s shell name of login shell 27920253Sjoerg * -o duplicate uid ok 28020253Sjoerg * -L class user class 28120253Sjoerg * -l name new login name 28220253Sjoerg * -h fd password filehandle 283124382Siedowse * -H fd encrypted password filehandle 28420253Sjoerg * -F force print or add 28520253Sjoerg * Setting defaults: 28620253Sjoerg * -D set user defaults 28720253Sjoerg * -b dir default home root dir 28820253Sjoerg * -e period default expiry period 28920253Sjoerg * -p period default password change period 29020253Sjoerg * -g group default group 29120253Sjoerg * -G grp1,grp2.. default additional groups 29220253Sjoerg * -L class default login class 29320253Sjoerg * -k dir default home skeleton 29420253Sjoerg * -s shell default shell 29520253Sjoerg * -w method default password method 29620253Sjoerg */ 29720253Sjoerg 29820253Sjoergint 299284128Sbaptpw_user(int mode, char *name, long id, struct cargs * args) 30020253Sjoerg{ 30152527Sdavidn int rc, edited = 0; 30220253Sjoerg char *p = NULL; 30320253Sjoerg struct carg *arg; 30420253Sjoerg struct passwd *pwd = NULL; 30520253Sjoerg struct group *grp; 30620253Sjoerg struct stat st; 307284118Sbapt struct userconf *cnf; 30820747Sdavidn char line[_PASSWORD_LEN+1]; 309283961Sbapt char path[MAXPATHLEN]; 31082868Sdd FILE *fp; 311167919Sle char *dmode_c; 312167919Sle void *set = NULL; 31320253Sjoerg 31420253Sjoerg static struct passwd fakeuser = 31520253Sjoerg { 316285398Sbapt "nouser", 31720253Sjoerg "*", 31820253Sjoerg -1, 31920253Sjoerg -1, 32020253Sjoerg 0, 32120253Sjoerg "", 32220253Sjoerg "User &", 32356000Sdavidn "/nonexistent", 32420253Sjoerg "/bin/sh", 32520253Sjoerg 0 32656000Sdavidn#if defined(__FreeBSD__) 32756000Sdavidn ,0 32856000Sdavidn#endif 32920253Sjoerg }; 33020253Sjoerg 331284118Sbapt cnf = conf.userconf; 33252512Sdavidn 333284149Sbapt if (mode == M_NEXT) 334285396Sbapt return (pw_usernext(cnf, conf.quiet)); 33520267Sjoerg 336285398Sbapt if (mode == M_PRINT) 337285398Sbapt return (pw_usershow(name, id, &fakeuser)); 338285398Sbapt 339285401Sbapt if (mode == M_DELETE) 340285401Sbapt return (pw_userdel(name, id)); 341285401Sbapt 342285405Sbapt if (mode == M_LOCK || mode == M_UNLOCK) 343285405Sbapt return (pw_userlock(name, id, mode)); 344285405Sbapt 34520267Sjoerg /* 34620253Sjoerg * We can do all of the common legwork here 34720253Sjoerg */ 34820253Sjoerg 34920253Sjoerg if ((arg = getarg(args, 'b')) != NULL) { 35020267Sjoerg cnf->home = arg->val; 35120253Sjoerg } 35221052Sdavidn 353167919Sle if ((arg = getarg(args, 'M')) != NULL) { 354167919Sle dmode_c = arg->val; 355167919Sle if ((set = setmode(dmode_c)) == NULL) 356167919Sle errx(EX_DATAERR, "invalid directory creation mode '%s'", 357167919Sle dmode_c); 358219408Sjkim cnf->homemode = getmode(set, _DEF_DIRMODE); 359167919Sle free(set); 360168044Sle } 361167919Sle 36221052Sdavidn /* 36321052Sdavidn * If we'll need to use it or we're updating it, 36421052Sdavidn * then create the base home directory if necessary 36521052Sdavidn */ 366224535Sdelphij if (arg != NULL || getarg(args, 'm') != NULL) { 36721052Sdavidn int l = strlen(cnf->home); 36821052Sdavidn 36921052Sdavidn if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 37021052Sdavidn cnf->home[--l] = '\0'; 37121052Sdavidn 37221052Sdavidn if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 37330259Scharnier errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); 37421052Sdavidn 37521052Sdavidn if (stat(cnf->home, &st) == -1) { 37621052Sdavidn char dbuf[MAXPATHLEN]; 37721052Sdavidn 37821242Sdavidn /* 37921242Sdavidn * This is a kludge especially for Joerg :) 38021242Sdavidn * If the home directory would be created in the root partition, then 38121242Sdavidn * we really create it under /usr which is likely to have more space. 38221242Sdavidn * But we create a symlink from cnf->home -> "/usr" -> cnf->home 38321242Sdavidn */ 38421242Sdavidn if (strchr(cnf->home+1, '/') == NULL) { 385282683Sbapt snprintf(dbuf, MAXPATHLEN, "/usr%s", cnf->home); 386219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) { 38721242Sdavidn chown(dbuf, 0, 0); 388148584Spjd /* 389148584Spjd * Skip first "/" and create symlink: 390148584Spjd * /home -> usr/home 391148584Spjd */ 392148584Spjd symlink(dbuf+1, cnf->home); 39321242Sdavidn } 39421242Sdavidn /* If this falls, fall back to old method */ 39521242Sdavidn } 396130633Srobert strlcpy(dbuf, cnf->home, sizeof(dbuf)); 397130633Srobert p = dbuf; 39821242Sdavidn if (stat(dbuf, &st) == -1) { 399252377Skientzle while ((p = strchr(p + 1, '/')) != NULL) { 40021242Sdavidn *p = '\0'; 40121242Sdavidn if (stat(dbuf, &st) == -1) { 402219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) == -1) 403285406Sbapt err(EX_OSFILE, "mkdir '%s'", dbuf); 40421242Sdavidn chown(dbuf, 0, 0); 40521242Sdavidn } else if (!S_ISDIR(st.st_mode)) 40630259Scharnier errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); 40721242Sdavidn *p = '/'; 40821242Sdavidn } 40921052Sdavidn } 41021242Sdavidn if (stat(dbuf, &st) == -1) { 411285406Sbapt if (mkdir(dbuf, _DEF_DIRMODE) == -1) 412285406Sbapt err(EX_OSFILE, "mkdir '%s'", dbuf); 41321052Sdavidn chown(dbuf, 0, 0); 41421052Sdavidn } 41521052Sdavidn } else if (!S_ISDIR(st.st_mode)) 41630259Scharnier errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); 41721052Sdavidn } 41821052Sdavidn 41920253Sjoerg if ((arg = getarg(args, 'e')) != NULL) 42020253Sjoerg cnf->expire_days = atoi(arg->val); 42120253Sjoerg 42221330Sdavidn if ((arg = getarg(args, 'y')) != NULL) 42321330Sdavidn cnf->nispasswd = arg->val; 42421330Sdavidn 42520253Sjoerg if ((arg = getarg(args, 'p')) != NULL && arg->val) 42620253Sjoerg cnf->password_days = atoi(arg->val); 42720253Sjoerg 42820253Sjoerg if ((arg = getarg(args, 'g')) != NULL) { 42963596Sdavidn if (!*(p = arg->val)) /* Handle empty group list specially */ 43063596Sdavidn cnf->default_group = ""; 43163596Sdavidn else { 43263596Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 43363596Sdavidn if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 43463596Sdavidn errx(EX_NOUSER, "group `%s' does not exist", p); 43563596Sdavidn } 43663596Sdavidn cnf->default_group = newstr(grp->gr_name); 43720253Sjoerg } 43820253Sjoerg } 43920253Sjoerg if ((arg = getarg(args, 'L')) != NULL) 440284110Sbapt cnf->default_class = pw_checkname(arg->val, 0); 44120253Sjoerg 44220253Sjoerg if ((arg = getarg(args, 'G')) != NULL && arg->val) { 44352527Sdavidn int i = 0; 44420253Sjoerg 44520747Sdavidn for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 44644229Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 44761957Sache if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 44830259Scharnier errx(EX_NOUSER, "group `%s' does not exist", p); 44920253Sjoerg } 45020747Sdavidn if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 45120747Sdavidn cnf->groups[i++] = newstr(grp->gr_name); 45220253Sjoerg } 45320747Sdavidn while (i < cnf->numgroups) 45420253Sjoerg cnf->groups[i++] = NULL; 45520253Sjoerg } 45652527Sdavidn 45720253Sjoerg if ((arg = getarg(args, 'k')) != NULL) { 45826088Sdavidn if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode)) 45930259Scharnier errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir); 46020253Sjoerg } 46152527Sdavidn 46220253Sjoerg if ((arg = getarg(args, 's')) != NULL) 46320253Sjoerg cnf->shell_default = arg->val; 46420253Sjoerg 46563600Sdavidn if ((arg = getarg(args, 'w')) != NULL) 46663600Sdavidn cnf->default_password = boolean_val(arg->val, cnf->default_password); 46720253Sjoerg if (mode == M_ADD && getarg(args, 'D')) { 468284135Sbapt if (name != NULL) 46930259Scharnier errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 47020253Sjoerg if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 47120253Sjoerg if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 47220253Sjoerg cnf->min_uid = 1000; 47320253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 47420253Sjoerg cnf->max_uid = 32000; 47520253Sjoerg } 47620253Sjoerg if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 47720253Sjoerg if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 47820253Sjoerg cnf->min_gid = 1000; 47920253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 48020253Sjoerg cnf->max_gid = 32000; 48120253Sjoerg } 48220253Sjoerg 483284135Sbapt if (write_userconfig(conf.config)) 484284135Sbapt return (EXIT_SUCCESS); 485283814Sbapt err(EX_IOERR, "config udpate"); 48620253Sjoerg } 48752527Sdavidn 488284128Sbapt if (name != NULL) 489284128Sbapt pwd = GETPWNAM(pw_checkname(name, 0)); 49020253Sjoerg 491284128Sbapt if (id < 0 && name == NULL) 492284128Sbapt errx(EX_DATAERR, "user name or id required"); 49320253Sjoerg 49420253Sjoerg /* 495285401Sbapt * Update require that the user exists 49620253Sjoerg */ 497285405Sbapt if (mode == M_UPDATE) { 49852527Sdavidn 499284128Sbapt if (name == NULL && pwd == NULL) /* Try harder */ 500284128Sbapt pwd = GETPWUID(id); 50120253Sjoerg 50220253Sjoerg if (pwd == NULL) { 503284128Sbapt if (name == NULL) 504284128Sbapt errx(EX_NOUSER, "no such uid `%ld'", id); 505284128Sbapt errx(EX_NOUSER, "no such user `%s'", name); 50620253Sjoerg } 50752527Sdavidn 508284128Sbapt if (name == NULL) 509284128Sbapt name = pwd->pw_name; 51020253Sjoerg 51120253Sjoerg /* 51220253Sjoerg * The rest is edit code 51320253Sjoerg */ 514284129Sbapt if (conf.newname != NULL) { 51520253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 51630259Scharnier errx(EX_DATAERR, "can't rename `root' account"); 517284129Sbapt pwd->pw_name = pw_checkname(conf.newname, 0); 51852527Sdavidn edited = 1; 51920253Sjoerg } 52052527Sdavidn 521284133Sbapt if (id > 0 && isdigit((unsigned char)*arg->val)) { 522284133Sbapt pwd->pw_uid = (uid_t)id; 52352527Sdavidn edited = 1; 52420253Sjoerg if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 52530259Scharnier errx(EX_DATAERR, "can't change uid of `root' account"); 52620253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 52730259Scharnier warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 52820253Sjoerg } 52920253Sjoerg 53052527Sdavidn if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */ 53152527Sdavidn gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid; 53252527Sdavidn if (newgid != pwd->pw_gid) { 53352527Sdavidn edited = 1; 53461762Sdavidn pwd->pw_gid = newgid; 53552527Sdavidn } 53652527Sdavidn } 53752527Sdavidn 53820253Sjoerg if ((arg = getarg(args, 'p')) != NULL) { 53952527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 54052527Sdavidn if (pwd->pw_change != 0) { 54152527Sdavidn pwd->pw_change = 0; 54252527Sdavidn edited = 1; 54352527Sdavidn } 54452527Sdavidn } 54520253Sjoerg else { 54620253Sjoerg time_t now = time(NULL); 54720253Sjoerg time_t expire = parse_date(now, arg->val); 54820253Sjoerg 54952527Sdavidn if (pwd->pw_change != expire) { 55052527Sdavidn pwd->pw_change = expire; 55152527Sdavidn edited = 1; 55252527Sdavidn } 55320253Sjoerg } 55420253Sjoerg } 55552527Sdavidn 55620267Sjoerg if ((arg = getarg(args, 'e')) != NULL) { 55752527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 55852527Sdavidn if (pwd->pw_expire != 0) { 55952527Sdavidn pwd->pw_expire = 0; 56052527Sdavidn edited = 1; 56152527Sdavidn } 56252527Sdavidn } 56320253Sjoerg else { 56420253Sjoerg time_t now = time(NULL); 56520253Sjoerg time_t expire = parse_date(now, arg->val); 56620253Sjoerg 56752527Sdavidn if (pwd->pw_expire != expire) { 56852527Sdavidn pwd->pw_expire = expire; 56952527Sdavidn edited = 1; 57052527Sdavidn } 57120253Sjoerg } 57220253Sjoerg } 57320253Sjoerg 57452527Sdavidn if ((arg = getarg(args, 's')) != NULL) { 57552527Sdavidn char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 57652527Sdavidn if (shell == NULL) 57752527Sdavidn shell = ""; 57852527Sdavidn if (strcmp(shell, pwd->pw_shell) != 0) { 57952527Sdavidn pwd->pw_shell = shell; 58052527Sdavidn edited = 1; 58152527Sdavidn } 58252527Sdavidn } 58320253Sjoerg 58452527Sdavidn if (getarg(args, 'L')) { 58552527Sdavidn if (cnf->default_class == NULL) 58652527Sdavidn cnf->default_class = ""; 58752527Sdavidn if (strcmp(pwd->pw_class, cnf->default_class) != 0) { 58852527Sdavidn pwd->pw_class = cnf->default_class; 58952527Sdavidn edited = 1; 59052527Sdavidn } 59152527Sdavidn } 59252527Sdavidn 59320747Sdavidn if ((arg = getarg(args, 'd')) != NULL) { 594130629Srobert if (strcmp(pwd->pw_dir, arg->val)) 595130629Srobert edited = 1; 59620747Sdavidn if (stat(pwd->pw_dir = arg->val, &st) == -1) { 59720747Sdavidn if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 59830259Scharnier warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 59920747Sdavidn } else if (!S_ISDIR(st.st_mode)) 60030259Scharnier warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 60120747Sdavidn } 60220747Sdavidn 603285137Sbapt if ((arg = getarg(args, 'w')) != NULL && conf.fd == -1) { 60464918Sgreen login_cap_t *lc; 60564918Sgreen 60664918Sgreen lc = login_getpwclass(pwd); 60764918Sgreen if (lc == NULL || 608252688Sdes login_setcryptfmt(lc, "sha512", NULL) == NULL) 60964918Sgreen warn("setting crypt(3) format"); 61064918Sgreen login_close(lc); 61120267Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 61252527Sdavidn edited = 1; 61352527Sdavidn } 61420267Sjoerg 61520253Sjoerg } else { 61664918Sgreen login_cap_t *lc; 61752527Sdavidn 61852527Sdavidn /* 61952527Sdavidn * Add code 62052527Sdavidn */ 62152527Sdavidn 622284128Sbapt if (name == NULL) /* Required */ 62330259Scharnier errx(EX_DATAERR, "login name required"); 624284128Sbapt else if ((pwd = GETPWNAM(name)) != NULL) /* Exists */ 625284128Sbapt errx(EX_DATAERR, "login name `%s' already exists", name); 62620253Sjoerg 62720253Sjoerg /* 62820253Sjoerg * Now, set up defaults for a new user 62920253Sjoerg */ 63020253Sjoerg pwd = &fakeuser; 631284128Sbapt pwd->pw_name = name; 63220253Sjoerg pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 633284133Sbapt pwd->pw_uid = pw_uidpolicy(cnf, id); 634284118Sbapt pwd->pw_gid = pw_gidpolicy(args, pwd->pw_name, (gid_t) pwd->pw_uid); 63520253Sjoerg pwd->pw_change = pw_pwdpolicy(cnf, args); 63620253Sjoerg pwd->pw_expire = pw_exppolicy(cnf, args); 63720253Sjoerg pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 63820253Sjoerg pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 63964918Sgreen lc = login_getpwclass(pwd); 640272833Sdes if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) 64164918Sgreen warn("setting crypt(3) format"); 64264918Sgreen login_close(lc); 64364918Sgreen pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 64452527Sdavidn edited = 1; 64520253Sjoerg 64620253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 64730259Scharnier warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 64820253Sjoerg } 64920253Sjoerg 65020253Sjoerg /* 65120253Sjoerg * Shared add/edit code 65220253Sjoerg */ 65352527Sdavidn if ((arg = getarg(args, 'c')) != NULL) { 654284110Sbapt char *gecos = pw_checkname(arg->val, 1); 65552527Sdavidn if (strcmp(pwd->pw_gecos, gecos) != 0) { 65652527Sdavidn pwd->pw_gecos = gecos; 65752527Sdavidn edited = 1; 65852527Sdavidn } 65952527Sdavidn } 66020253Sjoerg 661285133Sbapt if (conf.fd != -1) 662285137Sbapt edited = set_passwd(pwd, mode == M_UPDATE); 66320253Sjoerg 66420267Sjoerg /* 66520267Sjoerg * Special case: -N only displays & exits 66620267Sjoerg */ 667284121Sbapt if (conf.dryrun) 668284126Sbapt return print_user(pwd); 66920267Sjoerg 67021330Sdavidn if (mode == M_ADD) { 67152527Sdavidn edited = 1; /* Always */ 67252502Sdavidn rc = addpwent(pwd); 673283814Sbapt if (rc == -1) 674283814Sbapt errx(EX_IOERR, "user '%s' already exists", 675283814Sbapt pwd->pw_name); 676283814Sbapt else if (rc != 0) 677283814Sbapt err(EX_IOERR, "passwd file update"); 67852502Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/') { 67952502Sdavidn rc = addnispwent(cnf->nispasswd, pwd); 68052502Sdavidn if (rc == -1) 68152502Sdavidn warnx("User '%s' already exists in NIS passwd", pwd->pw_name); 68252502Sdavidn else 68356000Sdavidn warn("NIS passwd update"); 68452502Sdavidn /* NOTE: we treat NIS-only update errors as non-fatal */ 68552502Sdavidn } 686285405Sbapt } else if (mode == M_UPDATE && edited) /* Only updated this if required */ 687285405Sbapt perform_chgpwent(name, pwd); 68821330Sdavidn 68920253Sjoerg /* 69020253Sjoerg * Ok, user is created or changed - now edit group file 69120253Sjoerg */ 69220253Sjoerg 693242349Sbapt if (mode == M_ADD || getarg(args, 'G') != NULL) { 694273779Sbapt int i, j; 695273779Sbapt /* First remove the user from all group */ 696273779Sbapt SETGRENT(); 697273779Sbapt while ((grp = GETGRENT()) != NULL) { 698273779Sbapt char group[MAXLOGNAME]; 699273779Sbapt if (grp->gr_mem == NULL) 700273779Sbapt continue; 701273779Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 702273779Sbapt if (strcmp(grp->gr_mem[i] , pwd->pw_name) != 0) 703273779Sbapt continue; 704273779Sbapt for (j = i; grp->gr_mem[j] != NULL ; j++) 705273779Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 706273779Sbapt strlcpy(group, grp->gr_name, MAXLOGNAME); 707273779Sbapt chggrent(group, grp); 708273779Sbapt } 709273779Sbapt } 710273779Sbapt ENDGRENT(); 711273779Sbapt 712273779Sbapt /* now add to group where needed */ 713242349Sbapt for (i = 0; cnf->groups[i] != NULL; i++) { 714242349Sbapt grp = GETGRNAM(cnf->groups[i]); 715244737Sbapt grp = gr_add(grp, pwd->pw_name); 716244737Sbapt /* 717244737Sbapt * grp can only be NULL in 2 cases: 718244737Sbapt * - the new member is already a member 719244737Sbapt * - a problem with memory occurs 720244737Sbapt * in both cases we want to skip now. 721244737Sbapt */ 722244737Sbapt if (grp == NULL) 723242349Sbapt continue; 724242349Sbapt chggrent(cnf->groups[i], grp); 725245114Smjg free(grp); 726242349Sbapt } 727242349Sbapt } 728242349Sbapt 729242349Sbapt 73061759Sdavidn /* go get a current version of pwd */ 731284128Sbapt pwd = GETPWNAM(name); 73261759Sdavidn if (pwd == NULL) { 73361759Sdavidn /* This will fail when we rename, so special case that */ 734284129Sbapt if (mode == M_UPDATE && conf.newname != NULL) { 735284129Sbapt name = conf.newname; /* update new name */ 736284128Sbapt pwd = GETPWNAM(name); /* refetch renamed rec */ 73761759Sdavidn } 73861759Sdavidn } 73961759Sdavidn if (pwd == NULL) /* can't go on without this */ 740284128Sbapt errx(EX_NOUSER, "user '%s' disappeared during update", name); 74120253Sjoerg 74244229Sdavidn grp = GETGRGID(pwd->pw_gid); 743283842Sbapt pw_log(cnf, mode, W_USER, "%s(%u):%s(%u):%s:%s:%s", 744283842Sbapt pwd->pw_name, pwd->pw_uid, 745283842Sbapt grp ? grp->gr_name : "unknown", (grp ? grp->gr_gid : (uid_t)-1), 74620253Sjoerg pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 74720253Sjoerg 74820253Sjoerg /* 74920253Sjoerg * If adding, let's touch and chown the user's mail file. This is not 75020253Sjoerg * strictly necessary under BSD with a 0755 maildir but it also 75120253Sjoerg * doesn't hurt anything to create the empty mailfile 75220253Sjoerg */ 75320253Sjoerg if (mode == M_ADD) { 754283961Sbapt if (PWALTDIR() != PWF_ALT) { 755283961Sbapt arg = getarg(args, 'R'); 756283961Sbapt snprintf(path, sizeof(path), "%s%s/%s", 757283961Sbapt arg ? arg->val : "", _PATH_MAILDIR, pwd->pw_name); 758283961Sbapt close(open(path, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 75944229Sdavidn * mtime */ 760283961Sbapt chown(path, pwd->pw_uid, pwd->pw_gid); 76120253Sjoerg } 76220253Sjoerg } 76352527Sdavidn 76420253Sjoerg /* 76582868Sdd * Let's create and populate the user's home directory. Note 76620253Sjoerg * that this also `works' for editing users if -m is used, but 76720253Sjoerg * existing files will *not* be overwritten. 76820253Sjoerg */ 769283961Sbapt if (PWALTDIR() != PWF_ALT && getarg(args, 'm') != NULL && pwd->pw_dir && 770283961Sbapt *pwd->pw_dir == '/' && pwd->pw_dir[1]) 771285403Sbapt create_and_populate_homedir(pwd); 77252527Sdavidn 77382868Sdd /* 77482868Sdd * Finally, send mail to the new user as well, if we are asked to 77582868Sdd */ 77682868Sdd if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 77782868Sdd FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 77882868Sdd 77982868Sdd if (pfp == NULL) 78082868Sdd warn("sendmail"); 78182868Sdd else { 78282868Sdd fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 78382868Sdd while (fgets(line, sizeof(line), fp) != NULL) { 78482868Sdd /* Do substitutions? */ 78582868Sdd fputs(line, pfp); 78682868Sdd } 78782868Sdd pclose(pfp); 788283842Sbapt pw_log(cnf, mode, W_USER, "%s(%u) new user mail sent", 789283842Sbapt pwd->pw_name, pwd->pw_uid); 79082868Sdd } 79182868Sdd fclose(fp); 79282868Sdd } 79382868Sdd 79420267Sjoerg return EXIT_SUCCESS; 79520253Sjoerg} 79620253Sjoerg 79720253Sjoerg 79820253Sjoergstatic uid_t 799284133Sbaptpw_uidpolicy(struct userconf * cnf, long id) 80020253Sjoerg{ 80120253Sjoerg struct passwd *pwd; 80220253Sjoerg uid_t uid = (uid_t) - 1; 80320253Sjoerg 80420253Sjoerg /* 80520253Sjoerg * Check the given uid, if any 80620253Sjoerg */ 807284133Sbapt if (id > 0) { 808284133Sbapt uid = (uid_t) id; 80920253Sjoerg 810284133Sbapt if ((pwd = GETPWUID(uid)) != NULL && conf.checkduplicate) 811283842Sbapt errx(EX_DATAERR, "uid `%u' has already been allocated", pwd->pw_uid); 81220253Sjoerg } else { 81320253Sjoerg struct bitmap bm; 81420253Sjoerg 81520253Sjoerg /* 81620253Sjoerg * We need to allocate the next available uid under one of 81720253Sjoerg * two policies a) Grab the first unused uid b) Grab the 81820253Sjoerg * highest possible unused uid 81920253Sjoerg */ 82020253Sjoerg if (cnf->min_uid >= cnf->max_uid) { /* Sanity 82120253Sjoerg * claus^H^H^H^Hheck */ 82220253Sjoerg cnf->min_uid = 1000; 82320253Sjoerg cnf->max_uid = 32000; 82420253Sjoerg } 82520253Sjoerg bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 82620253Sjoerg 82720253Sjoerg /* 82820253Sjoerg * Now, let's fill the bitmap from the password file 82920253Sjoerg */ 83044229Sdavidn SETPWENT(); 83144229Sdavidn while ((pwd = GETPWENT()) != NULL) 83244229Sdavidn if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 83320253Sjoerg bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 83444229Sdavidn ENDPWENT(); 83520253Sjoerg 83620253Sjoerg /* 83720253Sjoerg * Then apply the policy, with fallback to reuse if necessary 83820253Sjoerg */ 83920253Sjoerg if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 84020253Sjoerg uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 84120253Sjoerg 84220253Sjoerg /* 84320253Sjoerg * Another sanity check 84420253Sjoerg */ 84520253Sjoerg if (uid < cnf->min_uid || uid > cnf->max_uid) 84630259Scharnier errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 84720253Sjoerg bm_dealloc(&bm); 84820253Sjoerg } 84920253Sjoerg return uid; 85020253Sjoerg} 85120253Sjoerg 85220253Sjoerg 85320253Sjoergstatic uid_t 854284118Sbaptpw_gidpolicy(struct cargs * args, char *nam, gid_t prefer) 85520253Sjoerg{ 85620253Sjoerg struct group *grp; 85720253Sjoerg gid_t gid = (uid_t) - 1; 85820253Sjoerg struct carg *a_gid = getarg(args, 'g'); 859284118Sbapt struct userconf *cnf = conf.userconf; 86020253Sjoerg 86120253Sjoerg /* 86220253Sjoerg * If no arg given, see if default can help out 86320253Sjoerg */ 86420253Sjoerg if (a_gid == NULL && cnf->default_group && *cnf->default_group) 86520253Sjoerg a_gid = addarg(args, 'g', cnf->default_group); 86620253Sjoerg 86720253Sjoerg /* 86820253Sjoerg * Check the given gid, if any 86920253Sjoerg */ 87044229Sdavidn SETGRENT(); 87120253Sjoerg if (a_gid != NULL) { 87244229Sdavidn if ((grp = GETGRNAM(a_gid->val)) == NULL) { 87320253Sjoerg gid = (gid_t) atol(a_gid->val); 87461957Sache if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL) 87530259Scharnier errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 87620253Sjoerg } 87720253Sjoerg gid = grp->gr_gid; 878262865Sjulian } else if ((grp = GETGRNAM(nam)) != NULL && 879262865Sjulian (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) { 88020267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 88120253Sjoerg } else { 88220253Sjoerg struct cargs grpargs; 88320253Sjoerg char tmp[32]; 88420253Sjoerg 88520253Sjoerg LIST_INIT(&grpargs); 88620253Sjoerg 88720253Sjoerg /* 88820253Sjoerg * We need to auto-create a group with the user's name. We 88920253Sjoerg * can send all the appropriate output to our sister routine 89020253Sjoerg * bit first see if we can create a group with gid==uid so we 89120253Sjoerg * can keep the user and group ids in sync. We purposely do 89220253Sjoerg * NOT check the gid range if we can force the sync. If the 89320253Sjoerg * user's name dups an existing group, then the group add 89420253Sjoerg * function will happily handle that case for us and exit. 89520253Sjoerg */ 89644229Sdavidn if (GETGRGID(prefer) == NULL) { 897282700Sbapt snprintf(tmp, sizeof(tmp), "%u", prefer); 89820253Sjoerg addarg(&grpargs, 'g', tmp); 89920253Sjoerg } 900284121Sbapt if (conf.dryrun) { 901285395Sbapt gid = pw_groupnext(cnf, true); 902285395Sbapt } else { 903284128Sbapt pw_group(M_ADD, nam, -1, &grpargs); 90444229Sdavidn if ((grp = GETGRNAM(nam)) != NULL) 90520267Sjoerg gid = grp->gr_gid; 90620267Sjoerg } 90770486Sben a_gid = LIST_FIRST(&grpargs); 90820253Sjoerg while (a_gid != NULL) { 90970486Sben struct carg *t = LIST_NEXT(a_gid, list); 91020253Sjoerg LIST_REMOVE(a_gid, list); 91120253Sjoerg a_gid = t; 91220253Sjoerg } 91320253Sjoerg } 91444229Sdavidn ENDGRENT(); 91520253Sjoerg return gid; 91620253Sjoerg} 91720253Sjoerg 91820253Sjoerg 91920253Sjoergstatic time_t 92020253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args) 92120253Sjoerg{ 92220253Sjoerg time_t result = 0; 92320253Sjoerg time_t now = time(NULL); 92427831Sdavidn struct carg *arg = getarg(args, 'p'); 92520253Sjoerg 92620253Sjoerg if (arg != NULL) { 92720253Sjoerg if ((result = parse_date(now, arg->val)) == now) 92830259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 92920253Sjoerg } else if (cnf->password_days > 0) 93020253Sjoerg result = now + ((long) cnf->password_days * 86400L); 93120253Sjoerg return result; 93220253Sjoerg} 93320253Sjoerg 93420253Sjoerg 93520253Sjoergstatic time_t 93620253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args) 93720253Sjoerg{ 93820253Sjoerg time_t result = 0; 93920253Sjoerg time_t now = time(NULL); 94020253Sjoerg struct carg *arg = getarg(args, 'e'); 94120253Sjoerg 94220253Sjoerg if (arg != NULL) { 94320253Sjoerg if ((result = parse_date(now, arg->val)) == now) 94430259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 94520253Sjoerg } else if (cnf->expire_days > 0) 94620253Sjoerg result = now + ((long) cnf->expire_days * 86400L); 94720253Sjoerg return result; 94820253Sjoerg} 94920253Sjoerg 95020253Sjoerg 95120253Sjoergstatic char * 95220253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 95320253Sjoerg{ 95420253Sjoerg struct carg *arg = getarg(args, 'd'); 955282699Sbapt static char home[128]; 95620253Sjoerg 95720253Sjoerg if (arg) 958282699Sbapt return (arg->val); 95920253Sjoerg 960282699Sbapt if (cnf->home == NULL || *cnf->home == '\0') 961282699Sbapt errx(EX_CONFIG, "no base home directory set"); 962282699Sbapt snprintf(home, sizeof(home), "%s/%s", cnf->home, user); 963282699Sbapt 964282699Sbapt return (home); 96520253Sjoerg} 96620253Sjoerg 96720253Sjoergstatic char * 96820253Sjoergshell_path(char const * path, char *shells[], char *sh) 96920253Sjoerg{ 97020253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 97120253Sjoerg return sh; /* specified full path or forced none */ 97220253Sjoerg else { 97320253Sjoerg char *p; 97420253Sjoerg char paths[_UC_MAXLINE]; 97520253Sjoerg 97620253Sjoerg /* 97720253Sjoerg * We need to search paths 97820253Sjoerg */ 979130633Srobert strlcpy(paths, path, sizeof(paths)); 98020253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 98120253Sjoerg int i; 98220253Sjoerg static char shellpath[256]; 98320253Sjoerg 98420253Sjoerg if (sh != NULL) { 985282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh); 98620253Sjoerg if (access(shellpath, X_OK) == 0) 98720253Sjoerg return shellpath; 98820253Sjoerg } else 98920253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 990282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]); 99120253Sjoerg if (access(shellpath, X_OK) == 0) 99220253Sjoerg return shellpath; 99320253Sjoerg } 99420253Sjoerg } 99520253Sjoerg if (sh == NULL) 99630259Scharnier errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 99730259Scharnier errx(EX_CONFIG, "no default shell available or defined"); 99820253Sjoerg return NULL; 99920253Sjoerg } 100020253Sjoerg} 100120253Sjoerg 100220253Sjoerg 100320253Sjoergstatic char * 100420253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 100520253Sjoerg{ 100620253Sjoerg char *sh = newshell; 100720253Sjoerg struct carg *arg = getarg(args, 's'); 100820253Sjoerg 100920253Sjoerg if (newshell == NULL && arg != NULL) 101020253Sjoerg sh = arg->val; 101120253Sjoerg return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 101220253Sjoerg} 101320253Sjoerg 1014179365Santoine#define SALTSIZE 32 101520253Sjoerg 1016179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"; 1017179365Santoine 101820253Sjoergchar * 101920253Sjoergpw_pwcrypt(char *password) 102020253Sjoerg{ 102120253Sjoerg int i; 1022179365Santoine char salt[SALTSIZE + 1]; 1023231994Skevlo char *cryptpw; 102420253Sjoerg 102520253Sjoerg static char buf[256]; 102620253Sjoerg 102720253Sjoerg /* 102820253Sjoerg * Calculate a salt value 102920253Sjoerg */ 1030179365Santoine for (i = 0; i < SALTSIZE; i++) 1031181785Sache salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)]; 1032179365Santoine salt[SALTSIZE] = '\0'; 103320253Sjoerg 1034231994Skevlo cryptpw = crypt(password, salt); 1035231994Skevlo if (cryptpw == NULL) 1036231994Skevlo errx(EX_CONFIG, "crypt(3) failure"); 1037231994Skevlo return strcpy(buf, cryptpw); 103820253Sjoerg} 103920253Sjoerg 104020590Sdavidn 104120253Sjoergstatic char * 104220253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user) 104320253Sjoerg{ 104420253Sjoerg int i, l; 104520253Sjoerg char pwbuf[32]; 104620253Sjoerg 104720253Sjoerg switch (cnf->default_password) { 104820253Sjoerg case -1: /* Random password */ 104973563Skris l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 105020253Sjoerg for (i = 0; i < l; i++) 1051181785Sache pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)]; 105220253Sjoerg pwbuf[i] = '\0'; 105320253Sjoerg 105420253Sjoerg /* 105520253Sjoerg * We give this information back to the user 105620253Sjoerg */ 1057285137Sbapt if (conf.fd == -1 && !conf.dryrun) { 105861957Sache if (isatty(STDOUT_FILENO)) 105920712Sdavidn printf("Password for '%s' is: ", user); 106020253Sjoerg printf("%s\n", pwbuf); 106120253Sjoerg fflush(stdout); 106220253Sjoerg } 106320253Sjoerg break; 106420253Sjoerg 106520253Sjoerg case -2: /* No password at all! */ 106620253Sjoerg return ""; 106720253Sjoerg 106820253Sjoerg case 0: /* No login - default */ 106920253Sjoerg default: 107020253Sjoerg return "*"; 107120253Sjoerg 107220253Sjoerg case 1: /* user's name */ 1073130633Srobert strlcpy(pwbuf, user, sizeof(pwbuf)); 107420253Sjoerg break; 107520253Sjoerg } 107620253Sjoerg return pw_pwcrypt(pwbuf); 107720253Sjoerg} 107820253Sjoerg 1079284111Sbaptstatic int 1080285401Sbaptpw_userdel(char *name, long id) 1081284111Sbapt{ 1082285401Sbapt struct passwd *pwd = NULL; 1083284111Sbapt char file[MAXPATHLEN]; 1084284111Sbapt char home[MAXPATHLEN]; 1085285401Sbapt uid_t uid; 1086284111Sbapt struct group *gr, *grp; 1087284111Sbapt char grname[LOGNAMESIZE]; 1088284111Sbapt int rc; 1089284111Sbapt struct stat st; 109020253Sjoerg 1091285401Sbapt if (id < 0 && name == NULL) 1092285401Sbapt errx(EX_DATAERR, "username or id required"); 1093285401Sbapt 1094285401Sbapt pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 1095285401Sbapt if (pwd == NULL) { 1096285401Sbapt if (name == NULL) 1097285401Sbapt errx(EX_NOUSER, "no such uid `%ld'", id); 1098285401Sbapt errx(EX_NOUSER, "no such user `%s'", name); 1099285401Sbapt } 1100285401Sbapt uid = pwd->pw_uid; 1101285401Sbapt if (name == NULL) 1102285401Sbapt name = pwd->pw_name; 1103285401Sbapt 1104284111Sbapt if (strcmp(pwd->pw_name, "root") == 0) 1105284111Sbapt errx(EX_DATAERR, "cannot remove user 'root'"); 1106284111Sbapt 1107284111Sbapt if (!PWALTDIR()) { 1108284111Sbapt /* 1109284111Sbapt * Remove opie record from /etc/opiekeys 1110284111Sbapt */ 1111284111Sbapt 1112284111Sbapt rmopie(pwd->pw_name); 1113284111Sbapt 1114284111Sbapt /* 1115284111Sbapt * Remove crontabs 1116284111Sbapt */ 1117284111Sbapt snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name); 1118284111Sbapt if (access(file, F_OK) == 0) { 1119284111Sbapt snprintf(file, sizeof(file), "crontab -u %s -r", pwd->pw_name); 1120284111Sbapt system(file); 1121284111Sbapt } 1122284111Sbapt } 1123284111Sbapt /* 1124284111Sbapt * Save these for later, since contents of pwd may be 1125284111Sbapt * invalidated by deletion 1126284111Sbapt */ 1127284111Sbapt snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name); 1128284111Sbapt strlcpy(home, pwd->pw_dir, sizeof(home)); 1129284111Sbapt gr = GETGRGID(pwd->pw_gid); 1130284111Sbapt if (gr != NULL) 1131284111Sbapt strlcpy(grname, gr->gr_name, LOGNAMESIZE); 1132284111Sbapt else 1133284111Sbapt grname[0] = '\0'; 1134284111Sbapt 1135284111Sbapt rc = delpwent(pwd); 1136284111Sbapt if (rc == -1) 1137284111Sbapt err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 1138284111Sbapt else if (rc != 0) 1139284111Sbapt err(EX_IOERR, "passwd update"); 1140284111Sbapt 1141285401Sbapt if (conf.userconf->nispasswd && *conf.userconf->nispasswd=='/') { 1142285401Sbapt rc = delnispwent(conf.userconf->nispasswd, name); 1143284111Sbapt if (rc == -1) 1144285401Sbapt warnx("WARNING: user '%s' does not exist in NIS passwd", 1145285401Sbapt pwd->pw_name); 1146284111Sbapt else if (rc != 0) 1147284111Sbapt warn("WARNING: NIS passwd update"); 1148284111Sbapt /* non-fatal */ 1149284111Sbapt } 1150284111Sbapt 1151284128Sbapt grp = GETGRNAM(name); 1152284111Sbapt if (grp != NULL && 1153284111Sbapt (grp->gr_mem == NULL || *grp->gr_mem == NULL) && 1154284128Sbapt strcmp(name, grname) == 0) 1155284128Sbapt delgrent(GETGRNAM(name)); 1156284111Sbapt SETGRENT(); 1157284111Sbapt while ((grp = GETGRENT()) != NULL) { 1158284111Sbapt int i, j; 1159284111Sbapt char group[MAXLOGNAME]; 1160284113Sbapt if (grp->gr_mem == NULL) 1161284113Sbapt continue; 1162284113Sbapt 1163284113Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 1164284128Sbapt if (strcmp(grp->gr_mem[i], name) != 0) 1165284113Sbapt continue; 1166284113Sbapt 1167284113Sbapt for (j = i; grp->gr_mem[j] != NULL; j++) 1168284113Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 1169284113Sbapt strlcpy(group, grp->gr_name, MAXLOGNAME); 1170284113Sbapt chggrent(group, grp); 1171284111Sbapt } 1172284111Sbapt } 1173284111Sbapt ENDGRENT(); 1174284111Sbapt 1175285401Sbapt pw_log(conf.userconf, M_DELETE, W_USER, "%s(%u) account removed", name, 1176285401Sbapt uid); 1177284111Sbapt 1178284117Sbapt if (!PWALTDIR()) { 1179284111Sbapt /* 1180284111Sbapt * Remove mail file 1181284111Sbapt */ 1182284111Sbapt remove(file); 1183284111Sbapt 1184284111Sbapt /* 1185284111Sbapt * Remove at jobs 1186284111Sbapt */ 1187284111Sbapt if (getpwuid(uid) == NULL) 1188284111Sbapt rmat(uid); 1189284111Sbapt 1190284111Sbapt /* 1191284111Sbapt * Remove home directory and contents 1192284111Sbapt */ 1193285401Sbapt if (conf.deletehome && *home == '/' && getpwuid(uid) == NULL && 1194284112Sbapt stat(home, &st) != -1) { 1195284112Sbapt rm_r(home, uid); 1196285401Sbapt pw_log(conf.userconf, M_DELETE, W_USER, "%s(%u) home '%s' %sremoved", 1197284128Sbapt name, uid, home, 1198284112Sbapt stat(home, &st) == -1 ? "" : "not completely "); 1199284111Sbapt } 1200284111Sbapt } 1201284111Sbapt 1202284111Sbapt return (EXIT_SUCCESS); 1203284111Sbapt} 1204284111Sbapt 120520253Sjoergstatic int 1206284124Sbaptprint_user(struct passwd * pwd) 120720253Sjoerg{ 1208284122Sbapt if (!conf.pretty) { 1209242349Sbapt char *buf; 121020253Sjoerg 1211284124Sbapt buf = conf.v7 ? pw_make_v7(pwd) : pw_make(pwd); 1212242349Sbapt printf("%s\n", buf); 1213242349Sbapt free(buf); 121420253Sjoerg } else { 121520267Sjoerg int j; 121620253Sjoerg char *p; 121744229Sdavidn struct group *grp = GETGRGID(pwd->pw_gid); 121820253Sjoerg char uname[60] = "User &", office[60] = "[None]", 121920253Sjoerg wphone[60] = "[None]", hphone[60] = "[None]"; 122020590Sdavidn char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 122120590Sdavidn struct tm * tptr; 122220253Sjoerg 122320253Sjoerg if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 1224130633Srobert strlcpy(uname, p, sizeof(uname)); 122520253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1226130633Srobert strlcpy(office, p, sizeof(office)); 122720253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1228130633Srobert strlcpy(wphone, p, sizeof(wphone)); 122920253Sjoerg if ((p = strtok(NULL, "")) != NULL) { 1230130633Srobert strlcpy(hphone, p, 1231130633Srobert sizeof(hphone)); 123220253Sjoerg } 123320253Sjoerg } 123420253Sjoerg } 123520253Sjoerg } 123620253Sjoerg /* 123720253Sjoerg * Handle '&' in gecos field 123820253Sjoerg */ 123920253Sjoerg if ((p = strchr(uname, '&')) != NULL) { 124020253Sjoerg int l = strlen(pwd->pw_name); 124120253Sjoerg int m = strlen(p); 124220253Sjoerg 124320253Sjoerg memmove(p + l, p + 1, m); 124420253Sjoerg memmove(p, pwd->pw_name, l); 124561957Sache *p = (char) toupper((unsigned char)*p); 124620253Sjoerg } 124720590Sdavidn if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 124874569Sache strftime(acexpire, sizeof acexpire, "%c", tptr); 124961957Sache if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 125074569Sache strftime(pwexpire, sizeof pwexpire, "%c", tptr); 1251283842Sbapt printf("Login Name: %-15s #%-12u Group: %-15s #%u\n" 125220747Sdavidn " Full Name: %s\n" 125320747Sdavidn " Home: %-26.26s Class: %s\n" 125420747Sdavidn " Shell: %-26.26s Office: %s\n" 125520747Sdavidn "Work Phone: %-26.26s Home Phone: %s\n" 125620747Sdavidn "Acc Expire: %-26.26s Pwd Expire: %s\n", 1257283842Sbapt pwd->pw_name, pwd->pw_uid, 1258283842Sbapt grp ? grp->gr_name : "(invalid)", pwd->pw_gid, 125920253Sjoerg uname, pwd->pw_dir, pwd->pw_class, 126020590Sdavidn pwd->pw_shell, office, wphone, hphone, 126120590Sdavidn acexpire, pwexpire); 126244229Sdavidn SETGRENT(); 126320267Sjoerg j = 0; 126444229Sdavidn while ((grp=GETGRENT()) != NULL) 126520267Sjoerg { 126620267Sjoerg int i = 0; 1267262865Sjulian if (grp->gr_mem != NULL) { 1268262865Sjulian while (grp->gr_mem[i] != NULL) 126920267Sjoerg { 1270262865Sjulian if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 1271262865Sjulian { 1272262865Sjulian printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 1273262865Sjulian break; 1274262865Sjulian } 1275262865Sjulian ++i; 127620267Sjoerg } 127720267Sjoerg } 127820267Sjoerg } 127944229Sdavidn ENDGRENT(); 128061957Sache printf("%s", j ? "\n" : ""); 128120253Sjoerg } 128220267Sjoerg return EXIT_SUCCESS; 128320253Sjoerg} 128420253Sjoerg 1285284110Sbaptchar * 1286284110Sbaptpw_checkname(char *name, int gecos) 128720253Sjoerg{ 1288109961Sgad char showch[8]; 1289284110Sbapt const char *badchars, *ch, *showtype; 1290109961Sgad int reject; 129120253Sjoerg 1292109961Sgad ch = name; 1293109961Sgad reject = 0; 1294109961Sgad if (gecos) { 1295109961Sgad /* See if the name is valid as a gecos (comment) field. */ 1296109961Sgad badchars = ":!@"; 1297109961Sgad showtype = "gecos field"; 1298109961Sgad } else { 1299109961Sgad /* See if the name is valid as a userid or group. */ 1300109961Sgad badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\""; 1301109961Sgad showtype = "userid/group name"; 1302109961Sgad /* Userids and groups can not have a leading '-'. */ 1303109961Sgad if (*ch == '-') 1304109961Sgad reject = 1; 130520253Sjoerg } 1306109961Sgad if (!reject) { 1307109961Sgad while (*ch) { 1308109961Sgad if (strchr(badchars, *ch) != NULL || *ch < ' ' || 1309109961Sgad *ch == 127) { 1310109961Sgad reject = 1; 1311109961Sgad break; 1312109961Sgad } 1313109961Sgad /* 8-bit characters are only allowed in GECOS fields */ 1314109961Sgad if (!gecos && (*ch & 0x80)) { 1315109961Sgad reject = 1; 1316109961Sgad break; 1317109961Sgad } 1318109961Sgad ch++; 1319109961Sgad } 1320109961Sgad } 1321109961Sgad /* 1322109961Sgad * A `$' is allowed as the final character for userids and groups, 1323109961Sgad * mainly for the benefit of samba. 1324109961Sgad */ 1325109961Sgad if (reject && !gecos) { 1326109961Sgad if (*ch == '$' && *(ch + 1) == '\0') { 1327109961Sgad reject = 0; 1328109961Sgad ch++; 1329109961Sgad } 1330109961Sgad } 1331109961Sgad if (reject) { 1332109961Sgad snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127) 1333109961Sgad ? "`%c'" : "0x%02x", *ch); 1334228673Sdim errx(EX_DATAERR, "invalid character %s at position %td in %s", 1335109961Sgad showch, (ch - name), showtype); 1336109961Sgad } 1337109961Sgad if (!gecos && (ch - name) > LOGNAMESIZE) 1338109961Sgad errx(EX_DATAERR, "name too long `%s' (max is %d)", name, 1339109961Sgad LOGNAMESIZE); 1340284110Sbapt 1341284110Sbapt return (name); 134220253Sjoerg} 134320253Sjoerg 134420253Sjoerg 134520253Sjoergstatic void 134620253Sjoergrmat(uid_t uid) 134720253Sjoerg{ 134820253Sjoerg DIR *d = opendir("/var/at/jobs"); 134920253Sjoerg 135020253Sjoerg if (d != NULL) { 135120253Sjoerg struct dirent *e; 135220253Sjoerg 135320253Sjoerg while ((e = readdir(d)) != NULL) { 135420253Sjoerg struct stat st; 135520253Sjoerg 135620253Sjoerg if (strncmp(e->d_name, ".lock", 5) != 0 && 135720253Sjoerg stat(e->d_name, &st) == 0 && 135820253Sjoerg !S_ISDIR(st.st_mode) && 135920253Sjoerg st.st_uid == uid) { 136020253Sjoerg char tmp[MAXPATHLEN]; 136120253Sjoerg 1362282700Sbapt snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s", e->d_name); 136320253Sjoerg system(tmp); 136420253Sjoerg } 136520253Sjoerg } 136620253Sjoerg closedir(d); 136720253Sjoerg } 136820253Sjoerg} 136920747Sdavidn 137020747Sdavidnstatic void 137185145Sachermopie(char const * name) 137220747Sdavidn{ 137385145Sache static const char etcopie[] = "/etc/opiekeys"; 137485145Sache FILE *fp = fopen(etcopie, "r+"); 137520747Sdavidn 137621052Sdavidn if (fp != NULL) { 137721052Sdavidn char tmp[1024]; 137821052Sdavidn off_t atofs = 0; 137921052Sdavidn int length = strlen(name); 138020747Sdavidn 138121052Sdavidn while (fgets(tmp, sizeof tmp, fp) != NULL) { 138221052Sdavidn if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 138321052Sdavidn if (fseek(fp, atofs, SEEK_SET) == 0) { 138421052Sdavidn fwrite("#", 1, 1, fp); /* Comment username out */ 138521052Sdavidn } 138621052Sdavidn break; 138720747Sdavidn } 138821052Sdavidn atofs = ftell(fp); 138920747Sdavidn } 139021052Sdavidn /* 139121052Sdavidn * If we got an error of any sort, don't update! 139221052Sdavidn */ 139321052Sdavidn fclose(fp); 139420747Sdavidn } 139520747Sdavidn} 139620747Sdavidn 1397