pw_user.c revision 284118
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 284118 2015-06-07 14:34:38Z bapt $"; 3130259Scharnier#endif /* not lint */ 3230259Scharnier 3330259Scharnier#include <ctype.h> 3430259Scharnier#include <err.h> 3520253Sjoerg#include <fcntl.h> 3620253Sjoerg#include <sys/param.h> 3720253Sjoerg#include <dirent.h> 3830259Scharnier#include <paths.h> 3920253Sjoerg#include <termios.h> 4020555Sdavidn#include <sys/types.h> 4120555Sdavidn#include <sys/time.h> 4220555Sdavidn#include <sys/resource.h> 4364918Sgreen#include <login_cap.h> 44242349Sbapt#include <pwd.h> 45242349Sbapt#include <grp.h> 46242349Sbapt#include <libutil.h> 4720253Sjoerg#include "pw.h" 4820253Sjoerg#include "bitmap.h" 4920253Sjoerg 5023318Sache#define LOGNAMESIZE (MAXLOGNAME-1) 5122394Sdavidn 5252512Sdavidnstatic char locked_str[] = "*LOCKED*"; 5324214Sache 54284111Sbaptstatic int delete_user(struct userconf *cnf, struct passwd *pwd, 55284111Sbapt struct carg *a_name, int delete, int mode); 5644386Sdavidnstatic int print_user(struct passwd * pwd, int pretty, int v7); 5720253Sjoergstatic uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args); 58284118Sbaptstatic uid_t pw_gidpolicy(struct cargs * args, char *nam, gid_t prefer); 5920253Sjoergstatic time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 6020253Sjoergstatic time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 6120253Sjoergstatic char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 6220253Sjoergstatic char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 6320253Sjoergstatic char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 6420253Sjoergstatic char *shell_path(char const * path, char *shells[], char *sh); 6520253Sjoergstatic void rmat(uid_t uid); 6685145Sachestatic void rmopie(char const * name); 6720253Sjoerg 68283961Sbaptstatic void 69284118Sbaptcreate_and_populate_homedir(int mode, struct passwd *pwd) 70283961Sbapt{ 71283961Sbapt char *homedir, *dotdir; 72284118Sbapt struct userconf *cnf = conf.userconf; 73283961Sbapt 74283961Sbapt homedir = dotdir = NULL; 75283961Sbapt 76284118Sbapt if (conf.rootdir[0] != '\0') { 77284118Sbapt asprintf(&homedir, "%s/%s", conf.rootdir, pwd->pw_dir); 78283961Sbapt if (homedir == NULL) 79283961Sbapt errx(EX_OSERR, "out of memory"); 80284118Sbapt asprintf(&dotdir, "%s/%s", conf.rootdir, cnf->dotdir); 81283961Sbapt } 82283961Sbapt 83283961Sbapt copymkdir(homedir ? homedir : pwd->pw_dir, dotdir ? dotdir: cnf->dotdir, 84283961Sbapt cnf->homemode, pwd->pw_uid, pwd->pw_gid); 85283961Sbapt pw_log(cnf, mode, W_USER, "%s(%u) home %s made", pwd->pw_name, 86283961Sbapt pwd->pw_uid, pwd->pw_dir); 87283961Sbapt} 88283961Sbapt 8920253Sjoerg/*- 9020253Sjoerg * -C config configuration file 9120253Sjoerg * -q quiet operation 9220253Sjoerg * -n name login name 9320253Sjoerg * -u uid user id 9420253Sjoerg * -c comment user name/comment 9520253Sjoerg * -d directory home directory 9620253Sjoerg * -e date account expiry date 9720253Sjoerg * -p date password expiry date 9820253Sjoerg * -g grp primary group 9920253Sjoerg * -G grp1,grp2 additional groups 10020253Sjoerg * -m [ -k dir ] create and set up home 10120253Sjoerg * -s shell name of login shell 10220253Sjoerg * -o duplicate uid ok 10320253Sjoerg * -L class user class 10420253Sjoerg * -l name new login name 10520253Sjoerg * -h fd password filehandle 106124382Siedowse * -H fd encrypted password filehandle 10720253Sjoerg * -F force print or add 10820253Sjoerg * Setting defaults: 10920253Sjoerg * -D set user defaults 11020253Sjoerg * -b dir default home root dir 11120253Sjoerg * -e period default expiry period 11220253Sjoerg * -p period default password change period 11320253Sjoerg * -g group default group 11420253Sjoerg * -G grp1,grp2.. default additional groups 11520253Sjoerg * -L class default login class 11620253Sjoerg * -k dir default home skeleton 11720253Sjoerg * -s shell default shell 11820253Sjoerg * -w method default password method 11920253Sjoerg */ 12020253Sjoerg 12120253Sjoergint 122284118Sbaptpw_user(int mode, struct cargs * args) 12320253Sjoerg{ 12452527Sdavidn int rc, edited = 0; 12520253Sjoerg char *p = NULL; 12652512Sdavidn char *passtmp; 12720253Sjoerg struct carg *a_name; 12820253Sjoerg struct carg *a_uid; 12920253Sjoerg struct carg *arg; 13020253Sjoerg struct passwd *pwd = NULL; 13120253Sjoerg struct group *grp; 13220253Sjoerg struct stat st; 133284118Sbapt struct userconf *cnf; 13420747Sdavidn char line[_PASSWORD_LEN+1]; 135283961Sbapt char path[MAXPATHLEN]; 13682868Sdd FILE *fp; 137167919Sle char *dmode_c; 138167919Sle void *set = NULL; 13920253Sjoerg 14020253Sjoerg static struct passwd fakeuser = 14120253Sjoerg { 14220253Sjoerg NULL, 14320253Sjoerg "*", 14420253Sjoerg -1, 14520253Sjoerg -1, 14620253Sjoerg 0, 14720253Sjoerg "", 14820253Sjoerg "User &", 14956000Sdavidn "/nonexistent", 15020253Sjoerg "/bin/sh", 15120253Sjoerg 0 15256000Sdavidn#if defined(__FreeBSD__) 15356000Sdavidn ,0 15456000Sdavidn#endif 15520253Sjoerg }; 15620253Sjoerg 157284118Sbapt cnf = conf.userconf; 15852512Sdavidn 15920253Sjoerg /* 16020267Sjoerg * With M_NEXT, we only need to return the 16120267Sjoerg * next uid to stdout 16220267Sjoerg */ 16320267Sjoerg if (mode == M_NEXT) 16420267Sjoerg { 16520267Sjoerg uid_t next = pw_uidpolicy(cnf, args); 16620267Sjoerg if (getarg(args, 'q')) 16720267Sjoerg return next; 168283842Sbapt printf("%u:", next); 169284118Sbapt pw_group(mode, args); 17020267Sjoerg return EXIT_SUCCESS; 17120267Sjoerg } 17220267Sjoerg 17320267Sjoerg /* 17420253Sjoerg * We can do all of the common legwork here 17520253Sjoerg */ 17620253Sjoerg 17720253Sjoerg if ((arg = getarg(args, 'b')) != NULL) { 17820267Sjoerg cnf->home = arg->val; 17920253Sjoerg } 18021052Sdavidn 181167919Sle if ((arg = getarg(args, 'M')) != NULL) { 182167919Sle dmode_c = arg->val; 183167919Sle if ((set = setmode(dmode_c)) == NULL) 184167919Sle errx(EX_DATAERR, "invalid directory creation mode '%s'", 185167919Sle dmode_c); 186219408Sjkim cnf->homemode = getmode(set, _DEF_DIRMODE); 187167919Sle free(set); 188168044Sle } 189167919Sle 19021052Sdavidn /* 19121052Sdavidn * If we'll need to use it or we're updating it, 19221052Sdavidn * then create the base home directory if necessary 19321052Sdavidn */ 194224535Sdelphij if (arg != NULL || getarg(args, 'm') != NULL) { 19521052Sdavidn int l = strlen(cnf->home); 19621052Sdavidn 19721052Sdavidn if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 19821052Sdavidn cnf->home[--l] = '\0'; 19921052Sdavidn 20021052Sdavidn if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 20130259Scharnier errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); 20221052Sdavidn 20321052Sdavidn if (stat(cnf->home, &st) == -1) { 20421052Sdavidn char dbuf[MAXPATHLEN]; 20521052Sdavidn 20621242Sdavidn /* 20721242Sdavidn * This is a kludge especially for Joerg :) 20821242Sdavidn * If the home directory would be created in the root partition, then 20921242Sdavidn * we really create it under /usr which is likely to have more space. 21021242Sdavidn * But we create a symlink from cnf->home -> "/usr" -> cnf->home 21121242Sdavidn */ 21221242Sdavidn if (strchr(cnf->home+1, '/') == NULL) { 213282683Sbapt snprintf(dbuf, MAXPATHLEN, "/usr%s", cnf->home); 214219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) { 21521242Sdavidn chown(dbuf, 0, 0); 216148584Spjd /* 217148584Spjd * Skip first "/" and create symlink: 218148584Spjd * /home -> usr/home 219148584Spjd */ 220148584Spjd symlink(dbuf+1, cnf->home); 22121242Sdavidn } 22221242Sdavidn /* If this falls, fall back to old method */ 22321242Sdavidn } 224130633Srobert strlcpy(dbuf, cnf->home, sizeof(dbuf)); 225130633Srobert p = dbuf; 22621242Sdavidn if (stat(dbuf, &st) == -1) { 227252377Skientzle while ((p = strchr(p + 1, '/')) != NULL) { 22821242Sdavidn *p = '\0'; 22921242Sdavidn if (stat(dbuf, &st) == -1) { 230219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) == -1) 23121242Sdavidn goto direrr; 23221242Sdavidn chown(dbuf, 0, 0); 23321242Sdavidn } else if (!S_ISDIR(st.st_mode)) 23430259Scharnier errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); 23521242Sdavidn *p = '/'; 23621242Sdavidn } 23721052Sdavidn } 23821242Sdavidn if (stat(dbuf, &st) == -1) { 239219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) == -1) { 24030259Scharnier direrr: err(EX_OSFILE, "mkdir '%s'", dbuf); 24121052Sdavidn } 24221052Sdavidn chown(dbuf, 0, 0); 24321052Sdavidn } 24421052Sdavidn } else if (!S_ISDIR(st.st_mode)) 24530259Scharnier errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); 24621052Sdavidn } 24721052Sdavidn 24820253Sjoerg if ((arg = getarg(args, 'e')) != NULL) 24920253Sjoerg cnf->expire_days = atoi(arg->val); 25020253Sjoerg 25121330Sdavidn if ((arg = getarg(args, 'y')) != NULL) 25221330Sdavidn cnf->nispasswd = arg->val; 25321330Sdavidn 25420253Sjoerg if ((arg = getarg(args, 'p')) != NULL && arg->val) 25520253Sjoerg cnf->password_days = atoi(arg->val); 25620253Sjoerg 25720253Sjoerg if ((arg = getarg(args, 'g')) != NULL) { 25863596Sdavidn if (!*(p = arg->val)) /* Handle empty group list specially */ 25963596Sdavidn cnf->default_group = ""; 26063596Sdavidn else { 26163596Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 26263596Sdavidn if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 26363596Sdavidn errx(EX_NOUSER, "group `%s' does not exist", p); 26463596Sdavidn } 26563596Sdavidn cnf->default_group = newstr(grp->gr_name); 26620253Sjoerg } 26720253Sjoerg } 26820253Sjoerg if ((arg = getarg(args, 'L')) != NULL) 269284110Sbapt cnf->default_class = pw_checkname(arg->val, 0); 27020253Sjoerg 27120253Sjoerg if ((arg = getarg(args, 'G')) != NULL && arg->val) { 27252527Sdavidn int i = 0; 27320253Sjoerg 27420747Sdavidn for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 27544229Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 27661957Sache if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 27730259Scharnier errx(EX_NOUSER, "group `%s' does not exist", p); 27820253Sjoerg } 27920747Sdavidn if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 28020747Sdavidn cnf->groups[i++] = newstr(grp->gr_name); 28120253Sjoerg } 28220747Sdavidn while (i < cnf->numgroups) 28320253Sjoerg cnf->groups[i++] = NULL; 28420253Sjoerg } 28552527Sdavidn 28620253Sjoerg if ((arg = getarg(args, 'k')) != NULL) { 28726088Sdavidn if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode)) 28830259Scharnier errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir); 28920253Sjoerg } 29052527Sdavidn 29120253Sjoerg if ((arg = getarg(args, 's')) != NULL) 29220253Sjoerg cnf->shell_default = arg->val; 29320253Sjoerg 29463600Sdavidn if ((arg = getarg(args, 'w')) != NULL) 29563600Sdavidn cnf->default_password = boolean_val(arg->val, cnf->default_password); 29620253Sjoerg if (mode == M_ADD && getarg(args, 'D')) { 29720253Sjoerg if (getarg(args, 'n') != NULL) 29830259Scharnier errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 29920253Sjoerg if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 30020253Sjoerg if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 30120253Sjoerg cnf->min_uid = 1000; 30220253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 30320253Sjoerg cnf->max_uid = 32000; 30420253Sjoerg } 30520253Sjoerg if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 30620253Sjoerg if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 30720253Sjoerg cnf->min_gid = 1000; 30820253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 30920253Sjoerg cnf->max_gid = 32000; 31020253Sjoerg } 31120253Sjoerg 31220253Sjoerg arg = getarg(args, 'C'); 31320253Sjoerg if (write_userconfig(arg ? arg->val : NULL)) 31420267Sjoerg return EXIT_SUCCESS; 315283814Sbapt err(EX_IOERR, "config udpate"); 31620253Sjoerg } 31752527Sdavidn 31820253Sjoerg if (mode == M_PRINT && getarg(args, 'a')) { 31920267Sjoerg int pretty = getarg(args, 'P') != NULL; 32044386Sdavidn int v7 = getarg(args, '7') != NULL; 32144229Sdavidn SETPWENT(); 32244229Sdavidn while ((pwd = GETPWENT()) != NULL) 32344386Sdavidn print_user(pwd, pretty, v7); 32444229Sdavidn ENDPWENT(); 32520267Sjoerg return EXIT_SUCCESS; 32620253Sjoerg } 32752527Sdavidn 32820253Sjoerg if ((a_name = getarg(args, 'n')) != NULL) 329284110Sbapt pwd = GETPWNAM(pw_checkname(a_name->val, 0)); 33020253Sjoerg a_uid = getarg(args, 'u'); 33120253Sjoerg 33220253Sjoerg if (a_uid == NULL) { 33320253Sjoerg if (a_name == NULL) 33430259Scharnier errx(EX_DATAERR, "user name or id required"); 33520253Sjoerg 33620253Sjoerg /* 33720253Sjoerg * Determine whether 'n' switch is name or uid - we don't 33820253Sjoerg * really don't really care which we have, but we need to 33920253Sjoerg * know. 34020253Sjoerg */ 34143780Sdes if (mode != M_ADD && pwd == NULL 342241108Sbapt && strspn(a_name->val, "0123456789") == strlen(a_name->val) 343241108Sbapt && *a_name->val) { 34420253Sjoerg (a_uid = a_name)->ch = 'u'; 34520253Sjoerg a_name = NULL; 34620253Sjoerg } 347273787Sbapt } else { 348277764Sbapt if (strspn(a_uid->val, "0123456789") != strlen(a_uid->val)) 349273787Sbapt errx(EX_USAGE, "-u expects a number"); 35020253Sjoerg } 35152527Sdavidn 35220253Sjoerg /* 35320253Sjoerg * Update, delete & print require that the user exists 35420253Sjoerg */ 35552512Sdavidn if (mode == M_UPDATE || mode == M_DELETE || 35652512Sdavidn mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) { 35752527Sdavidn 35820253Sjoerg if (a_name == NULL && pwd == NULL) /* Try harder */ 35944229Sdavidn pwd = GETPWUID(atoi(a_uid->val)); 36020253Sjoerg 36120253Sjoerg if (pwd == NULL) { 36220253Sjoerg if (mode == M_PRINT && getarg(args, 'F')) { 36320253Sjoerg fakeuser.pw_name = a_name ? a_name->val : "nouser"; 364283841Sbapt fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : (uid_t) -1; 36544386Sdavidn return print_user(&fakeuser, 36644386Sdavidn getarg(args, 'P') != NULL, 36744386Sdavidn getarg(args, '7') != NULL); 36820253Sjoerg } 36920253Sjoerg if (a_name == NULL) 37030259Scharnier errx(EX_NOUSER, "no such uid `%s'", a_uid->val); 37130259Scharnier errx(EX_NOUSER, "no such user `%s'", a_name->val); 37220253Sjoerg } 37352527Sdavidn 37420253Sjoerg if (a_name == NULL) /* May be needed later */ 37520253Sjoerg a_name = addarg(args, 'n', newstr(pwd->pw_name)); 37620253Sjoerg 37720253Sjoerg /* 37852512Sdavidn * The M_LOCK and M_UNLOCK functions simply add or remove 37952512Sdavidn * a "*LOCKED*" prefix from in front of the password to 38052512Sdavidn * prevent it decoding correctly, and therefore prevents 38152512Sdavidn * access. Of course, this only prevents access via 38252512Sdavidn * password authentication (not ssh, kerberos or any 38352512Sdavidn * other method that does not use the UNIX password) but 38452512Sdavidn * that is a known limitation. 38552512Sdavidn */ 38652512Sdavidn 38752512Sdavidn if (mode == M_LOCK) { 38852512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0) 38952512Sdavidn errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 390282685Sbapt asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd); 39152512Sdavidn if (passtmp == NULL) /* disaster */ 39252512Sdavidn errx(EX_UNAVAILABLE, "out of memory"); 39352512Sdavidn pwd->pw_passwd = passtmp; 39452527Sdavidn edited = 1; 39552512Sdavidn } else if (mode == M_UNLOCK) { 39652512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0) 39752512Sdavidn errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 39852512Sdavidn pwd->pw_passwd += sizeof(locked_str)-1; 39952527Sdavidn edited = 1; 400284111Sbapt } else if (mode == M_DELETE) 401284111Sbapt return (delete_user(cnf, pwd, a_name, 402284111Sbapt getarg(args, 'r') != NULL, mode)); 403284111Sbapt else if (mode == M_PRINT) 40444386Sdavidn return print_user(pwd, 40544386Sdavidn getarg(args, 'P') != NULL, 40644386Sdavidn getarg(args, '7') != NULL); 40720253Sjoerg 40820253Sjoerg /* 40920253Sjoerg * The rest is edit code 41020253Sjoerg */ 41120253Sjoerg if ((arg = getarg(args, 'l')) != NULL) { 41220253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 41330259Scharnier errx(EX_DATAERR, "can't rename `root' account"); 414284110Sbapt pwd->pw_name = pw_checkname(arg->val, 0); 41552527Sdavidn edited = 1; 41620253Sjoerg } 41752527Sdavidn 41861957Sache if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) { 41920253Sjoerg pwd->pw_uid = (uid_t) atol(arg->val); 42052527Sdavidn edited = 1; 42120253Sjoerg if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 42230259Scharnier errx(EX_DATAERR, "can't change uid of `root' account"); 42320253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 42430259Scharnier warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 42520253Sjoerg } 42620253Sjoerg 42752527Sdavidn if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */ 42852527Sdavidn gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid; 42952527Sdavidn if (newgid != pwd->pw_gid) { 43052527Sdavidn edited = 1; 43161762Sdavidn pwd->pw_gid = newgid; 43252527Sdavidn } 43352527Sdavidn } 43452527Sdavidn 43520253Sjoerg if ((arg = getarg(args, 'p')) != NULL) { 43652527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 43752527Sdavidn if (pwd->pw_change != 0) { 43852527Sdavidn pwd->pw_change = 0; 43952527Sdavidn edited = 1; 44052527Sdavidn } 44152527Sdavidn } 44220253Sjoerg else { 44320253Sjoerg time_t now = time(NULL); 44420253Sjoerg time_t expire = parse_date(now, arg->val); 44520253Sjoerg 44652527Sdavidn if (pwd->pw_change != expire) { 44752527Sdavidn pwd->pw_change = expire; 44852527Sdavidn edited = 1; 44952527Sdavidn } 45020253Sjoerg } 45120253Sjoerg } 45252527Sdavidn 45320267Sjoerg if ((arg = getarg(args, 'e')) != NULL) { 45452527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 45552527Sdavidn if (pwd->pw_expire != 0) { 45652527Sdavidn pwd->pw_expire = 0; 45752527Sdavidn edited = 1; 45852527Sdavidn } 45952527Sdavidn } 46020253Sjoerg else { 46120253Sjoerg time_t now = time(NULL); 46220253Sjoerg time_t expire = parse_date(now, arg->val); 46320253Sjoerg 46452527Sdavidn if (pwd->pw_expire != expire) { 46552527Sdavidn pwd->pw_expire = expire; 46652527Sdavidn edited = 1; 46752527Sdavidn } 46820253Sjoerg } 46920253Sjoerg } 47020253Sjoerg 47152527Sdavidn if ((arg = getarg(args, 's')) != NULL) { 47252527Sdavidn char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 47352527Sdavidn if (shell == NULL) 47452527Sdavidn shell = ""; 47552527Sdavidn if (strcmp(shell, pwd->pw_shell) != 0) { 47652527Sdavidn pwd->pw_shell = shell; 47752527Sdavidn edited = 1; 47852527Sdavidn } 47952527Sdavidn } 48020253Sjoerg 48152527Sdavidn if (getarg(args, 'L')) { 48252527Sdavidn if (cnf->default_class == NULL) 48352527Sdavidn cnf->default_class = ""; 48452527Sdavidn if (strcmp(pwd->pw_class, cnf->default_class) != 0) { 48552527Sdavidn pwd->pw_class = cnf->default_class; 48652527Sdavidn edited = 1; 48752527Sdavidn } 48852527Sdavidn } 48952527Sdavidn 49020747Sdavidn if ((arg = getarg(args, 'd')) != NULL) { 491130629Srobert if (strcmp(pwd->pw_dir, arg->val)) 492130629Srobert edited = 1; 49320747Sdavidn if (stat(pwd->pw_dir = arg->val, &st) == -1) { 49420747Sdavidn if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 49530259Scharnier warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 49620747Sdavidn } else if (!S_ISDIR(st.st_mode)) 49730259Scharnier warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 49820747Sdavidn } 49920747Sdavidn 500124382Siedowse if ((arg = getarg(args, 'w')) != NULL && 501124382Siedowse getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) { 50264918Sgreen login_cap_t *lc; 50364918Sgreen 50464918Sgreen lc = login_getpwclass(pwd); 50564918Sgreen if (lc == NULL || 506252688Sdes login_setcryptfmt(lc, "sha512", NULL) == NULL) 50764918Sgreen warn("setting crypt(3) format"); 50864918Sgreen login_close(lc); 50920267Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 51052527Sdavidn edited = 1; 51152527Sdavidn } 51220267Sjoerg 51320253Sjoerg } else { 51464918Sgreen login_cap_t *lc; 51552527Sdavidn 51652527Sdavidn /* 51752527Sdavidn * Add code 51852527Sdavidn */ 51952527Sdavidn 52020253Sjoerg if (a_name == NULL) /* Required */ 52130259Scharnier errx(EX_DATAERR, "login name required"); 52244229Sdavidn else if ((pwd = GETPWNAM(a_name->val)) != NULL) /* Exists */ 52330259Scharnier errx(EX_DATAERR, "login name `%s' already exists", a_name->val); 52420253Sjoerg 52520253Sjoerg /* 52620253Sjoerg * Now, set up defaults for a new user 52720253Sjoerg */ 52820253Sjoerg pwd = &fakeuser; 52920253Sjoerg pwd->pw_name = a_name->val; 53020253Sjoerg pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 53120253Sjoerg pwd->pw_uid = pw_uidpolicy(cnf, args); 532284118Sbapt pwd->pw_gid = pw_gidpolicy(args, pwd->pw_name, (gid_t) pwd->pw_uid); 53320253Sjoerg pwd->pw_change = pw_pwdpolicy(cnf, args); 53420253Sjoerg pwd->pw_expire = pw_exppolicy(cnf, args); 53520253Sjoerg pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 53620253Sjoerg pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 53764918Sgreen lc = login_getpwclass(pwd); 538272833Sdes if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) 53964918Sgreen warn("setting crypt(3) format"); 54064918Sgreen login_close(lc); 54164918Sgreen pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 54252527Sdavidn edited = 1; 54320253Sjoerg 54420253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 54530259Scharnier warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 54620253Sjoerg } 54720253Sjoerg 54820253Sjoerg /* 54920253Sjoerg * Shared add/edit code 55020253Sjoerg */ 55152527Sdavidn if ((arg = getarg(args, 'c')) != NULL) { 552284110Sbapt char *gecos = pw_checkname(arg->val, 1); 55352527Sdavidn if (strcmp(pwd->pw_gecos, gecos) != 0) { 55452527Sdavidn pwd->pw_gecos = gecos; 55552527Sdavidn edited = 1; 55652527Sdavidn } 55752527Sdavidn } 55820253Sjoerg 559124382Siedowse if ((arg = getarg(args, 'h')) != NULL || 560124382Siedowse (arg = getarg(args, 'H')) != NULL) { 56163572Sdavidn if (strcmp(arg->val, "-") == 0) { 56263572Sdavidn if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 56363572Sdavidn pwd->pw_passwd = "*"; /* No access */ 56463572Sdavidn edited = 1; 56563572Sdavidn } 56663572Sdavidn } else { 56720253Sjoerg int fd = atoi(arg->val); 568124382Siedowse int precrypt = (arg->ch == 'H'); 56920253Sjoerg int b; 57020253Sjoerg int istty = isatty(fd); 57120253Sjoerg struct termios t; 57264918Sgreen login_cap_t *lc; 57320253Sjoerg 57420253Sjoerg if (istty) { 57520253Sjoerg if (tcgetattr(fd, &t) == -1) 57620253Sjoerg istty = 0; 57720253Sjoerg else { 57820253Sjoerg struct termios n = t; 57920253Sjoerg 58020253Sjoerg /* Disable echo */ 58120253Sjoerg n.c_lflag &= ~(ECHO); 58220253Sjoerg tcsetattr(fd, TCSANOW, &n); 583124382Siedowse printf("%s%spassword for user %s:", 584124382Siedowse (mode == M_UPDATE) ? "new " : "", 585124382Siedowse precrypt ? "encrypted " : "", 586124382Siedowse pwd->pw_name); 58720253Sjoerg fflush(stdout); 58820253Sjoerg } 58920253Sjoerg } 59020253Sjoerg b = read(fd, line, sizeof(line) - 1); 59120253Sjoerg if (istty) { /* Restore state */ 59220253Sjoerg tcsetattr(fd, TCSANOW, &t); 59320253Sjoerg fputc('\n', stdout); 59420253Sjoerg fflush(stdout); 59520253Sjoerg } 596283814Sbapt if (b < 0) 597283814Sbapt err(EX_IOERR, "-%c file descriptor", 598283814Sbapt precrypt ? 'H' : 'h'); 59920253Sjoerg line[b] = '\0'; 600168045Sle if ((p = strpbrk(line, "\r\n")) != NULL) 60120253Sjoerg *p = '\0'; 60220253Sjoerg if (!*line) 60330259Scharnier errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 604124382Siedowse if (precrypt) { 605124382Siedowse if (strchr(line, ':') != NULL) 606124382Siedowse return EX_DATAERR; 607124382Siedowse pwd->pw_passwd = line; 608124382Siedowse } else { 609124382Siedowse lc = login_getpwclass(pwd); 610124382Siedowse if (lc == NULL || 611272833Sdes login_setcryptfmt(lc, "sha512", NULL) == NULL) 612124382Siedowse warn("setting crypt(3) format"); 613124382Siedowse login_close(lc); 614124382Siedowse pwd->pw_passwd = pw_pwcrypt(line); 615124382Siedowse } 61652527Sdavidn edited = 1; 61720253Sjoerg } 61820253Sjoerg } 61920267Sjoerg 62020267Sjoerg /* 62120267Sjoerg * Special case: -N only displays & exits 62220267Sjoerg */ 62320267Sjoerg if (getarg(args, 'N') != NULL) 62444386Sdavidn return print_user(pwd, 62544386Sdavidn getarg(args, 'P') != NULL, 62644386Sdavidn getarg(args, '7') != NULL); 62720267Sjoerg 62821330Sdavidn if (mode == M_ADD) { 62952527Sdavidn edited = 1; /* Always */ 63052502Sdavidn rc = addpwent(pwd); 631283814Sbapt if (rc == -1) 632283814Sbapt errx(EX_IOERR, "user '%s' already exists", 633283814Sbapt pwd->pw_name); 634283814Sbapt else if (rc != 0) 635283814Sbapt err(EX_IOERR, "passwd file update"); 63652502Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/') { 63752502Sdavidn rc = addnispwent(cnf->nispasswd, pwd); 63852502Sdavidn if (rc == -1) 63952502Sdavidn warnx("User '%s' already exists in NIS passwd", pwd->pw_name); 64052502Sdavidn else 64156000Sdavidn warn("NIS passwd update"); 64252502Sdavidn /* NOTE: we treat NIS-only update errors as non-fatal */ 64352502Sdavidn } 64452512Sdavidn } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) { 64552527Sdavidn if (edited) { /* Only updated this if required */ 64652527Sdavidn rc = chgpwent(a_name->val, pwd); 647283814Sbapt if (rc == -1) 648283814Sbapt errx(EX_IOERR, "user '%s' does not exist (NIS?)", pwd->pw_name); 649283814Sbapt else if (rc != 0) 650283814Sbapt err(EX_IOERR, "passwd file update"); 65152527Sdavidn if ( cnf->nispasswd && *cnf->nispasswd=='/') { 65252527Sdavidn rc = chgnispwent(cnf->nispasswd, a_name->val, pwd); 65352527Sdavidn if (rc == -1) 65452527Sdavidn warn("User '%s' not found in NIS passwd", pwd->pw_name); 65552527Sdavidn else 65656000Sdavidn warn("NIS passwd update"); 65752527Sdavidn /* NOTE: NIS-only update errors are not fatal */ 65852527Sdavidn } 65952502Sdavidn } 66021330Sdavidn } 66121330Sdavidn 66220253Sjoerg /* 66320253Sjoerg * Ok, user is created or changed - now edit group file 66420253Sjoerg */ 66520253Sjoerg 666242349Sbapt if (mode == M_ADD || getarg(args, 'G') != NULL) { 667273779Sbapt int i, j; 668273779Sbapt /* First remove the user from all group */ 669273779Sbapt SETGRENT(); 670273779Sbapt while ((grp = GETGRENT()) != NULL) { 671273779Sbapt char group[MAXLOGNAME]; 672273779Sbapt if (grp->gr_mem == NULL) 673273779Sbapt continue; 674273779Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 675273779Sbapt if (strcmp(grp->gr_mem[i] , pwd->pw_name) != 0) 676273779Sbapt continue; 677273779Sbapt for (j = i; grp->gr_mem[j] != NULL ; j++) 678273779Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 679273779Sbapt strlcpy(group, grp->gr_name, MAXLOGNAME); 680273779Sbapt chggrent(group, grp); 681273779Sbapt } 682273779Sbapt } 683273779Sbapt ENDGRENT(); 684273779Sbapt 685273779Sbapt /* now add to group where needed */ 686242349Sbapt for (i = 0; cnf->groups[i] != NULL; i++) { 687242349Sbapt grp = GETGRNAM(cnf->groups[i]); 688244737Sbapt grp = gr_add(grp, pwd->pw_name); 689244737Sbapt /* 690244737Sbapt * grp can only be NULL in 2 cases: 691244737Sbapt * - the new member is already a member 692244737Sbapt * - a problem with memory occurs 693244737Sbapt * in both cases we want to skip now. 694244737Sbapt */ 695244737Sbapt if (grp == NULL) 696242349Sbapt continue; 697242349Sbapt chggrent(cnf->groups[i], grp); 698245114Smjg free(grp); 699242349Sbapt } 700242349Sbapt } 701242349Sbapt 702242349Sbapt 70361759Sdavidn /* go get a current version of pwd */ 70461759Sdavidn pwd = GETPWNAM(a_name->val); 70561759Sdavidn if (pwd == NULL) { 70661759Sdavidn /* This will fail when we rename, so special case that */ 70761759Sdavidn if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) { 70861759Sdavidn a_name->val = arg->val; /* update new name */ 70961759Sdavidn pwd = GETPWNAM(a_name->val); /* refetch renamed rec */ 71061759Sdavidn } 71161759Sdavidn } 71261759Sdavidn if (pwd == NULL) /* can't go on without this */ 71330259Scharnier errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val); 71420253Sjoerg 71544229Sdavidn grp = GETGRGID(pwd->pw_gid); 716283842Sbapt pw_log(cnf, mode, W_USER, "%s(%u):%s(%u):%s:%s:%s", 717283842Sbapt pwd->pw_name, pwd->pw_uid, 718283842Sbapt grp ? grp->gr_name : "unknown", (grp ? grp->gr_gid : (uid_t)-1), 71920253Sjoerg pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 72020253Sjoerg 72120253Sjoerg /* 72220253Sjoerg * If adding, let's touch and chown the user's mail file. This is not 72320253Sjoerg * strictly necessary under BSD with a 0755 maildir but it also 72420253Sjoerg * doesn't hurt anything to create the empty mailfile 72520253Sjoerg */ 72620253Sjoerg if (mode == M_ADD) { 727283961Sbapt if (PWALTDIR() != PWF_ALT) { 728283961Sbapt arg = getarg(args, 'R'); 729283961Sbapt snprintf(path, sizeof(path), "%s%s/%s", 730283961Sbapt arg ? arg->val : "", _PATH_MAILDIR, pwd->pw_name); 731283961Sbapt close(open(path, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 73244229Sdavidn * mtime */ 733283961Sbapt chown(path, pwd->pw_uid, pwd->pw_gid); 73420253Sjoerg } 73520253Sjoerg } 73652527Sdavidn 73720253Sjoerg /* 73882868Sdd * Let's create and populate the user's home directory. Note 73920253Sjoerg * that this also `works' for editing users if -m is used, but 74020253Sjoerg * existing files will *not* be overwritten. 74120253Sjoerg */ 742283961Sbapt if (PWALTDIR() != PWF_ALT && getarg(args, 'm') != NULL && pwd->pw_dir && 743283961Sbapt *pwd->pw_dir == '/' && pwd->pw_dir[1]) 744284118Sbapt create_and_populate_homedir(mode, pwd); 74552527Sdavidn 74682868Sdd /* 74782868Sdd * Finally, send mail to the new user as well, if we are asked to 74882868Sdd */ 74982868Sdd if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 75082868Sdd FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 75182868Sdd 75282868Sdd if (pfp == NULL) 75382868Sdd warn("sendmail"); 75482868Sdd else { 75582868Sdd fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 75682868Sdd while (fgets(line, sizeof(line), fp) != NULL) { 75782868Sdd /* Do substitutions? */ 75882868Sdd fputs(line, pfp); 75982868Sdd } 76082868Sdd pclose(pfp); 761283842Sbapt pw_log(cnf, mode, W_USER, "%s(%u) new user mail sent", 762283842Sbapt pwd->pw_name, pwd->pw_uid); 76382868Sdd } 76482868Sdd fclose(fp); 76582868Sdd } 76682868Sdd 76720267Sjoerg return EXIT_SUCCESS; 76820253Sjoerg} 76920253Sjoerg 77020253Sjoerg 77120253Sjoergstatic uid_t 77220253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args) 77320253Sjoerg{ 77420253Sjoerg struct passwd *pwd; 77520253Sjoerg uid_t uid = (uid_t) - 1; 77620253Sjoerg struct carg *a_uid = getarg(args, 'u'); 77720253Sjoerg 77820253Sjoerg /* 77920253Sjoerg * Check the given uid, if any 78020253Sjoerg */ 78120253Sjoerg if (a_uid != NULL) { 78220253Sjoerg uid = (uid_t) atol(a_uid->val); 78320253Sjoerg 78444229Sdavidn if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL) 785283842Sbapt errx(EX_DATAERR, "uid `%u' has already been allocated", pwd->pw_uid); 78620253Sjoerg } else { 78720253Sjoerg struct bitmap bm; 78820253Sjoerg 78920253Sjoerg /* 79020253Sjoerg * We need to allocate the next available uid under one of 79120253Sjoerg * two policies a) Grab the first unused uid b) Grab the 79220253Sjoerg * highest possible unused uid 79320253Sjoerg */ 79420253Sjoerg if (cnf->min_uid >= cnf->max_uid) { /* Sanity 79520253Sjoerg * claus^H^H^H^Hheck */ 79620253Sjoerg cnf->min_uid = 1000; 79720253Sjoerg cnf->max_uid = 32000; 79820253Sjoerg } 79920253Sjoerg bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 80020253Sjoerg 80120253Sjoerg /* 80220253Sjoerg * Now, let's fill the bitmap from the password file 80320253Sjoerg */ 80444229Sdavidn SETPWENT(); 80544229Sdavidn while ((pwd = GETPWENT()) != NULL) 80644229Sdavidn if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 80720253Sjoerg bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 80844229Sdavidn ENDPWENT(); 80920253Sjoerg 81020253Sjoerg /* 81120253Sjoerg * Then apply the policy, with fallback to reuse if necessary 81220253Sjoerg */ 81320253Sjoerg if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 81420253Sjoerg uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 81520253Sjoerg 81620253Sjoerg /* 81720253Sjoerg * Another sanity check 81820253Sjoerg */ 81920253Sjoerg if (uid < cnf->min_uid || uid > cnf->max_uid) 82030259Scharnier errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 82120253Sjoerg bm_dealloc(&bm); 82220253Sjoerg } 82320253Sjoerg return uid; 82420253Sjoerg} 82520253Sjoerg 82620253Sjoerg 82720253Sjoergstatic uid_t 828284118Sbaptpw_gidpolicy(struct cargs * args, char *nam, gid_t prefer) 82920253Sjoerg{ 83020253Sjoerg struct group *grp; 83120253Sjoerg gid_t gid = (uid_t) - 1; 83220253Sjoerg struct carg *a_gid = getarg(args, 'g'); 833284118Sbapt struct userconf *cnf = conf.userconf; 83420253Sjoerg 83520253Sjoerg /* 83620253Sjoerg * If no arg given, see if default can help out 83720253Sjoerg */ 83820253Sjoerg if (a_gid == NULL && cnf->default_group && *cnf->default_group) 83920253Sjoerg a_gid = addarg(args, 'g', cnf->default_group); 84020253Sjoerg 84120253Sjoerg /* 84220253Sjoerg * Check the given gid, if any 84320253Sjoerg */ 84444229Sdavidn SETGRENT(); 84520253Sjoerg if (a_gid != NULL) { 84644229Sdavidn if ((grp = GETGRNAM(a_gid->val)) == NULL) { 84720253Sjoerg gid = (gid_t) atol(a_gid->val); 84861957Sache if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL) 84930259Scharnier errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 85020253Sjoerg } 85120253Sjoerg gid = grp->gr_gid; 852262865Sjulian } else if ((grp = GETGRNAM(nam)) != NULL && 853262865Sjulian (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) { 85420267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 85520253Sjoerg } else { 85620253Sjoerg struct cargs grpargs; 85720253Sjoerg char tmp[32]; 85820253Sjoerg 85920253Sjoerg LIST_INIT(&grpargs); 86020253Sjoerg addarg(&grpargs, 'n', nam); 86120253Sjoerg 86220253Sjoerg /* 86320253Sjoerg * We need to auto-create a group with the user's name. We 86420253Sjoerg * can send all the appropriate output to our sister routine 86520253Sjoerg * bit first see if we can create a group with gid==uid so we 86620253Sjoerg * can keep the user and group ids in sync. We purposely do 86720253Sjoerg * NOT check the gid range if we can force the sync. If the 86820253Sjoerg * user's name dups an existing group, then the group add 86920253Sjoerg * function will happily handle that case for us and exit. 87020253Sjoerg */ 87144229Sdavidn if (GETGRGID(prefer) == NULL) { 872282700Sbapt snprintf(tmp, sizeof(tmp), "%u", prefer); 87320253Sjoerg addarg(&grpargs, 'g', tmp); 87420253Sjoerg } 87520267Sjoerg if (getarg(args, 'N')) 87620267Sjoerg { 87720267Sjoerg addarg(&grpargs, 'N', NULL); 87820267Sjoerg addarg(&grpargs, 'q', NULL); 879284118Sbapt gid = pw_group(M_NEXT, &grpargs); 88020267Sjoerg } 88120267Sjoerg else 88220267Sjoerg { 883284118Sbapt pw_group(M_ADD, &grpargs); 88444229Sdavidn if ((grp = GETGRNAM(nam)) != NULL) 88520267Sjoerg gid = grp->gr_gid; 88620267Sjoerg } 88770486Sben a_gid = LIST_FIRST(&grpargs); 88820253Sjoerg while (a_gid != NULL) { 88970486Sben struct carg *t = LIST_NEXT(a_gid, list); 89020253Sjoerg LIST_REMOVE(a_gid, list); 89120253Sjoerg a_gid = t; 89220253Sjoerg } 89320253Sjoerg } 89444229Sdavidn ENDGRENT(); 89520253Sjoerg return gid; 89620253Sjoerg} 89720253Sjoerg 89820253Sjoerg 89920253Sjoergstatic time_t 90020253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args) 90120253Sjoerg{ 90220253Sjoerg time_t result = 0; 90320253Sjoerg time_t now = time(NULL); 90427831Sdavidn struct carg *arg = getarg(args, 'p'); 90520253Sjoerg 90620253Sjoerg if (arg != NULL) { 90720253Sjoerg if ((result = parse_date(now, arg->val)) == now) 90830259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 90920253Sjoerg } else if (cnf->password_days > 0) 91020253Sjoerg result = now + ((long) cnf->password_days * 86400L); 91120253Sjoerg return result; 91220253Sjoerg} 91320253Sjoerg 91420253Sjoerg 91520253Sjoergstatic time_t 91620253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args) 91720253Sjoerg{ 91820253Sjoerg time_t result = 0; 91920253Sjoerg time_t now = time(NULL); 92020253Sjoerg struct carg *arg = getarg(args, 'e'); 92120253Sjoerg 92220253Sjoerg if (arg != NULL) { 92320253Sjoerg if ((result = parse_date(now, arg->val)) == now) 92430259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 92520253Sjoerg } else if (cnf->expire_days > 0) 92620253Sjoerg result = now + ((long) cnf->expire_days * 86400L); 92720253Sjoerg return result; 92820253Sjoerg} 92920253Sjoerg 93020253Sjoerg 93120253Sjoergstatic char * 93220253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 93320253Sjoerg{ 93420253Sjoerg struct carg *arg = getarg(args, 'd'); 935282699Sbapt static char home[128]; 93620253Sjoerg 93720253Sjoerg if (arg) 938282699Sbapt return (arg->val); 93920253Sjoerg 940282699Sbapt if (cnf->home == NULL || *cnf->home == '\0') 941282699Sbapt errx(EX_CONFIG, "no base home directory set"); 942282699Sbapt snprintf(home, sizeof(home), "%s/%s", cnf->home, user); 943282699Sbapt 944282699Sbapt return (home); 94520253Sjoerg} 94620253Sjoerg 94720253Sjoergstatic char * 94820253Sjoergshell_path(char const * path, char *shells[], char *sh) 94920253Sjoerg{ 95020253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 95120253Sjoerg return sh; /* specified full path or forced none */ 95220253Sjoerg else { 95320253Sjoerg char *p; 95420253Sjoerg char paths[_UC_MAXLINE]; 95520253Sjoerg 95620253Sjoerg /* 95720253Sjoerg * We need to search paths 95820253Sjoerg */ 959130633Srobert strlcpy(paths, path, sizeof(paths)); 96020253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 96120253Sjoerg int i; 96220253Sjoerg static char shellpath[256]; 96320253Sjoerg 96420253Sjoerg if (sh != NULL) { 965282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh); 96620253Sjoerg if (access(shellpath, X_OK) == 0) 96720253Sjoerg return shellpath; 96820253Sjoerg } else 96920253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 970282700Sbapt snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]); 97120253Sjoerg if (access(shellpath, X_OK) == 0) 97220253Sjoerg return shellpath; 97320253Sjoerg } 97420253Sjoerg } 97520253Sjoerg if (sh == NULL) 97630259Scharnier errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 97730259Scharnier errx(EX_CONFIG, "no default shell available or defined"); 97820253Sjoerg return NULL; 97920253Sjoerg } 98020253Sjoerg} 98120253Sjoerg 98220253Sjoerg 98320253Sjoergstatic char * 98420253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 98520253Sjoerg{ 98620253Sjoerg char *sh = newshell; 98720253Sjoerg struct carg *arg = getarg(args, 's'); 98820253Sjoerg 98920253Sjoerg if (newshell == NULL && arg != NULL) 99020253Sjoerg sh = arg->val; 99120253Sjoerg return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 99220253Sjoerg} 99320253Sjoerg 994179365Santoine#define SALTSIZE 32 99520253Sjoerg 996179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"; 997179365Santoine 99820253Sjoergchar * 99920253Sjoergpw_pwcrypt(char *password) 100020253Sjoerg{ 100120253Sjoerg int i; 1002179365Santoine char salt[SALTSIZE + 1]; 1003231994Skevlo char *cryptpw; 100420253Sjoerg 100520253Sjoerg static char buf[256]; 100620253Sjoerg 100720253Sjoerg /* 100820253Sjoerg * Calculate a salt value 100920253Sjoerg */ 1010179365Santoine for (i = 0; i < SALTSIZE; i++) 1011181785Sache salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)]; 1012179365Santoine salt[SALTSIZE] = '\0'; 101320253Sjoerg 1014231994Skevlo cryptpw = crypt(password, salt); 1015231994Skevlo if (cryptpw == NULL) 1016231994Skevlo errx(EX_CONFIG, "crypt(3) failure"); 1017231994Skevlo return strcpy(buf, cryptpw); 101820253Sjoerg} 101920253Sjoerg 102020590Sdavidn 102120253Sjoergstatic char * 102220253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user) 102320253Sjoerg{ 102420253Sjoerg int i, l; 102520253Sjoerg char pwbuf[32]; 102620253Sjoerg 102720253Sjoerg switch (cnf->default_password) { 102820253Sjoerg case -1: /* Random password */ 102973563Skris l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 103020253Sjoerg for (i = 0; i < l; i++) 1031181785Sache pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)]; 103220253Sjoerg pwbuf[i] = '\0'; 103320253Sjoerg 103420253Sjoerg /* 103520253Sjoerg * We give this information back to the user 103620253Sjoerg */ 1037124382Siedowse if (getarg(args, 'h') == NULL && getarg(args, 'H') == NULL && 1038124382Siedowse getarg(args, 'N') == NULL) { 103961957Sache if (isatty(STDOUT_FILENO)) 104020712Sdavidn printf("Password for '%s' is: ", user); 104120253Sjoerg printf("%s\n", pwbuf); 104220253Sjoerg fflush(stdout); 104320253Sjoerg } 104420253Sjoerg break; 104520253Sjoerg 104620253Sjoerg case -2: /* No password at all! */ 104720253Sjoerg return ""; 104820253Sjoerg 104920253Sjoerg case 0: /* No login - default */ 105020253Sjoerg default: 105120253Sjoerg return "*"; 105220253Sjoerg 105320253Sjoerg case 1: /* user's name */ 1054130633Srobert strlcpy(pwbuf, user, sizeof(pwbuf)); 105520253Sjoerg break; 105620253Sjoerg } 105720253Sjoerg return pw_pwcrypt(pwbuf); 105820253Sjoerg} 105920253Sjoerg 1060284111Sbaptstatic int 1061284111Sbaptdelete_user(struct userconf *cnf, struct passwd *pwd, struct carg *a_name, 1062284111Sbapt int delete, int mode) 1063284111Sbapt{ 1064284111Sbapt char file[MAXPATHLEN]; 1065284111Sbapt char home[MAXPATHLEN]; 1066284111Sbapt uid_t uid = pwd->pw_uid; 1067284111Sbapt struct group *gr, *grp; 1068284111Sbapt char grname[LOGNAMESIZE]; 1069284111Sbapt int rc; 1070284111Sbapt struct stat st; 107120253Sjoerg 1072284111Sbapt if (strcmp(pwd->pw_name, "root") == 0) 1073284111Sbapt errx(EX_DATAERR, "cannot remove user 'root'"); 1074284111Sbapt 1075284111Sbapt if (!PWALTDIR()) { 1076284111Sbapt /* 1077284111Sbapt * Remove opie record from /etc/opiekeys 1078284111Sbapt */ 1079284111Sbapt 1080284111Sbapt rmopie(pwd->pw_name); 1081284111Sbapt 1082284111Sbapt /* 1083284111Sbapt * Remove crontabs 1084284111Sbapt */ 1085284111Sbapt snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name); 1086284111Sbapt if (access(file, F_OK) == 0) { 1087284111Sbapt snprintf(file, sizeof(file), "crontab -u %s -r", pwd->pw_name); 1088284111Sbapt system(file); 1089284111Sbapt } 1090284111Sbapt } 1091284111Sbapt /* 1092284111Sbapt * Save these for later, since contents of pwd may be 1093284111Sbapt * invalidated by deletion 1094284111Sbapt */ 1095284111Sbapt snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name); 1096284111Sbapt strlcpy(home, pwd->pw_dir, sizeof(home)); 1097284111Sbapt gr = GETGRGID(pwd->pw_gid); 1098284111Sbapt if (gr != NULL) 1099284111Sbapt strlcpy(grname, gr->gr_name, LOGNAMESIZE); 1100284111Sbapt else 1101284111Sbapt grname[0] = '\0'; 1102284111Sbapt 1103284111Sbapt rc = delpwent(pwd); 1104284111Sbapt if (rc == -1) 1105284111Sbapt err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 1106284111Sbapt else if (rc != 0) 1107284111Sbapt err(EX_IOERR, "passwd update"); 1108284111Sbapt 1109284111Sbapt if (cnf->nispasswd && *cnf->nispasswd=='/') { 1110284111Sbapt rc = delnispwent(cnf->nispasswd, a_name->val); 1111284111Sbapt if (rc == -1) 1112284111Sbapt warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name); 1113284111Sbapt else if (rc != 0) 1114284111Sbapt warn("WARNING: NIS passwd update"); 1115284111Sbapt /* non-fatal */ 1116284111Sbapt } 1117284111Sbapt 1118284111Sbapt grp = GETGRNAM(a_name->val); 1119284111Sbapt if (grp != NULL && 1120284111Sbapt (grp->gr_mem == NULL || *grp->gr_mem == NULL) && 1121284111Sbapt strcmp(a_name->val, grname) == 0) 1122284111Sbapt delgrent(GETGRNAM(a_name->val)); 1123284111Sbapt SETGRENT(); 1124284111Sbapt while ((grp = GETGRENT()) != NULL) { 1125284111Sbapt int i, j; 1126284111Sbapt char group[MAXLOGNAME]; 1127284113Sbapt if (grp->gr_mem == NULL) 1128284113Sbapt continue; 1129284113Sbapt 1130284113Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 1131284114Sbapt if (strcmp(grp->gr_mem[i], a_name->val) != 0) 1132284113Sbapt continue; 1133284113Sbapt 1134284113Sbapt for (j = i; grp->gr_mem[j] != NULL; j++) 1135284113Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 1136284113Sbapt strlcpy(group, grp->gr_name, MAXLOGNAME); 1137284113Sbapt chggrent(group, grp); 1138284111Sbapt } 1139284111Sbapt } 1140284111Sbapt ENDGRENT(); 1141284111Sbapt 1142284111Sbapt pw_log(cnf, mode, W_USER, "%s(%u) account removed", a_name->val, uid); 1143284111Sbapt 1144284117Sbapt if (!PWALTDIR()) { 1145284111Sbapt /* 1146284111Sbapt * Remove mail file 1147284111Sbapt */ 1148284111Sbapt remove(file); 1149284111Sbapt 1150284111Sbapt /* 1151284111Sbapt * Remove at jobs 1152284111Sbapt */ 1153284111Sbapt if (getpwuid(uid) == NULL) 1154284111Sbapt rmat(uid); 1155284111Sbapt 1156284111Sbapt /* 1157284111Sbapt * Remove home directory and contents 1158284111Sbapt */ 1159284112Sbapt if (delete && *home == '/' && getpwuid(uid) == NULL && 1160284112Sbapt stat(home, &st) != -1) { 1161284112Sbapt rm_r(home, uid); 1162284112Sbapt pw_log(cnf, mode, W_USER, "%s(%u) home '%s' %sremoved", 1163284112Sbapt a_name->val, uid, home, 1164284112Sbapt stat(home, &st) == -1 ? "" : "not completely "); 1165284111Sbapt } 1166284111Sbapt } 1167284111Sbapt 1168284111Sbapt return (EXIT_SUCCESS); 1169284111Sbapt} 1170284111Sbapt 117120253Sjoergstatic int 117244386Sdavidnprint_user(struct passwd * pwd, int pretty, int v7) 117320253Sjoerg{ 117420253Sjoerg if (!pretty) { 1175242349Sbapt char *buf; 117620253Sjoerg 1177242349Sbapt if (!v7) 1178242349Sbapt pwd->pw_passwd = (pwd->pw_passwd == NULL) ? "" : "*"; 1179242349Sbapt 1180242349Sbapt buf = v7 ? pw_make_v7(pwd) : pw_make(pwd); 1181242349Sbapt printf("%s\n", buf); 1182242349Sbapt free(buf); 118320253Sjoerg } else { 118420267Sjoerg int j; 118520253Sjoerg char *p; 118644229Sdavidn struct group *grp = GETGRGID(pwd->pw_gid); 118720253Sjoerg char uname[60] = "User &", office[60] = "[None]", 118820253Sjoerg wphone[60] = "[None]", hphone[60] = "[None]"; 118920590Sdavidn char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 119020590Sdavidn struct tm * tptr; 119120253Sjoerg 119220253Sjoerg if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 1193130633Srobert strlcpy(uname, p, sizeof(uname)); 119420253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1195130633Srobert strlcpy(office, p, sizeof(office)); 119620253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1197130633Srobert strlcpy(wphone, p, sizeof(wphone)); 119820253Sjoerg if ((p = strtok(NULL, "")) != NULL) { 1199130633Srobert strlcpy(hphone, p, 1200130633Srobert sizeof(hphone)); 120120253Sjoerg } 120220253Sjoerg } 120320253Sjoerg } 120420253Sjoerg } 120520253Sjoerg /* 120620253Sjoerg * Handle '&' in gecos field 120720253Sjoerg */ 120820253Sjoerg if ((p = strchr(uname, '&')) != NULL) { 120920253Sjoerg int l = strlen(pwd->pw_name); 121020253Sjoerg int m = strlen(p); 121120253Sjoerg 121220253Sjoerg memmove(p + l, p + 1, m); 121320253Sjoerg memmove(p, pwd->pw_name, l); 121461957Sache *p = (char) toupper((unsigned char)*p); 121520253Sjoerg } 121620590Sdavidn if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 121774569Sache strftime(acexpire, sizeof acexpire, "%c", tptr); 121861957Sache if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 121974569Sache strftime(pwexpire, sizeof pwexpire, "%c", tptr); 1220283842Sbapt printf("Login Name: %-15s #%-12u Group: %-15s #%u\n" 122120747Sdavidn " Full Name: %s\n" 122220747Sdavidn " Home: %-26.26s Class: %s\n" 122320747Sdavidn " Shell: %-26.26s Office: %s\n" 122420747Sdavidn "Work Phone: %-26.26s Home Phone: %s\n" 122520747Sdavidn "Acc Expire: %-26.26s Pwd Expire: %s\n", 1226283842Sbapt pwd->pw_name, pwd->pw_uid, 1227283842Sbapt grp ? grp->gr_name : "(invalid)", pwd->pw_gid, 122820253Sjoerg uname, pwd->pw_dir, pwd->pw_class, 122920590Sdavidn pwd->pw_shell, office, wphone, hphone, 123020590Sdavidn acexpire, pwexpire); 123144229Sdavidn SETGRENT(); 123220267Sjoerg j = 0; 123344229Sdavidn while ((grp=GETGRENT()) != NULL) 123420267Sjoerg { 123520267Sjoerg int i = 0; 1236262865Sjulian if (grp->gr_mem != NULL) { 1237262865Sjulian while (grp->gr_mem[i] != NULL) 123820267Sjoerg { 1239262865Sjulian if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 1240262865Sjulian { 1241262865Sjulian printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 1242262865Sjulian break; 1243262865Sjulian } 1244262865Sjulian ++i; 124520267Sjoerg } 124620267Sjoerg } 124720267Sjoerg } 124844229Sdavidn ENDGRENT(); 124961957Sache printf("%s", j ? "\n" : ""); 125020253Sjoerg } 125120267Sjoerg return EXIT_SUCCESS; 125220253Sjoerg} 125320253Sjoerg 1254284110Sbaptchar * 1255284110Sbaptpw_checkname(char *name, int gecos) 125620253Sjoerg{ 1257109961Sgad char showch[8]; 1258284110Sbapt const char *badchars, *ch, *showtype; 1259109961Sgad int reject; 126020253Sjoerg 1261109961Sgad ch = name; 1262109961Sgad reject = 0; 1263109961Sgad if (gecos) { 1264109961Sgad /* See if the name is valid as a gecos (comment) field. */ 1265109961Sgad badchars = ":!@"; 1266109961Sgad showtype = "gecos field"; 1267109961Sgad } else { 1268109961Sgad /* See if the name is valid as a userid or group. */ 1269109961Sgad badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\""; 1270109961Sgad showtype = "userid/group name"; 1271109961Sgad /* Userids and groups can not have a leading '-'. */ 1272109961Sgad if (*ch == '-') 1273109961Sgad reject = 1; 127420253Sjoerg } 1275109961Sgad if (!reject) { 1276109961Sgad while (*ch) { 1277109961Sgad if (strchr(badchars, *ch) != NULL || *ch < ' ' || 1278109961Sgad *ch == 127) { 1279109961Sgad reject = 1; 1280109961Sgad break; 1281109961Sgad } 1282109961Sgad /* 8-bit characters are only allowed in GECOS fields */ 1283109961Sgad if (!gecos && (*ch & 0x80)) { 1284109961Sgad reject = 1; 1285109961Sgad break; 1286109961Sgad } 1287109961Sgad ch++; 1288109961Sgad } 1289109961Sgad } 1290109961Sgad /* 1291109961Sgad * A `$' is allowed as the final character for userids and groups, 1292109961Sgad * mainly for the benefit of samba. 1293109961Sgad */ 1294109961Sgad if (reject && !gecos) { 1295109961Sgad if (*ch == '$' && *(ch + 1) == '\0') { 1296109961Sgad reject = 0; 1297109961Sgad ch++; 1298109961Sgad } 1299109961Sgad } 1300109961Sgad if (reject) { 1301109961Sgad snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127) 1302109961Sgad ? "`%c'" : "0x%02x", *ch); 1303228673Sdim errx(EX_DATAERR, "invalid character %s at position %td in %s", 1304109961Sgad showch, (ch - name), showtype); 1305109961Sgad } 1306109961Sgad if (!gecos && (ch - name) > LOGNAMESIZE) 1307109961Sgad errx(EX_DATAERR, "name too long `%s' (max is %d)", name, 1308109961Sgad LOGNAMESIZE); 1309284110Sbapt 1310284110Sbapt return (name); 131120253Sjoerg} 131220253Sjoerg 131320253Sjoerg 131420253Sjoergstatic void 131520253Sjoergrmat(uid_t uid) 131620253Sjoerg{ 131720253Sjoerg DIR *d = opendir("/var/at/jobs"); 131820253Sjoerg 131920253Sjoerg if (d != NULL) { 132020253Sjoerg struct dirent *e; 132120253Sjoerg 132220253Sjoerg while ((e = readdir(d)) != NULL) { 132320253Sjoerg struct stat st; 132420253Sjoerg 132520253Sjoerg if (strncmp(e->d_name, ".lock", 5) != 0 && 132620253Sjoerg stat(e->d_name, &st) == 0 && 132720253Sjoerg !S_ISDIR(st.st_mode) && 132820253Sjoerg st.st_uid == uid) { 132920253Sjoerg char tmp[MAXPATHLEN]; 133020253Sjoerg 1331282700Sbapt snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s", e->d_name); 133220253Sjoerg system(tmp); 133320253Sjoerg } 133420253Sjoerg } 133520253Sjoerg closedir(d); 133620253Sjoerg } 133720253Sjoerg} 133820747Sdavidn 133920747Sdavidnstatic void 134085145Sachermopie(char const * name) 134120747Sdavidn{ 134285145Sache static const char etcopie[] = "/etc/opiekeys"; 134385145Sache FILE *fp = fopen(etcopie, "r+"); 134420747Sdavidn 134521052Sdavidn if (fp != NULL) { 134621052Sdavidn char tmp[1024]; 134721052Sdavidn off_t atofs = 0; 134821052Sdavidn int length = strlen(name); 134920747Sdavidn 135021052Sdavidn while (fgets(tmp, sizeof tmp, fp) != NULL) { 135121052Sdavidn if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 135221052Sdavidn if (fseek(fp, atofs, SEEK_SET) == 0) { 135321052Sdavidn fwrite("#", 1, 1, fp); /* Comment username out */ 135421052Sdavidn } 135521052Sdavidn break; 135620747Sdavidn } 135721052Sdavidn atofs = ftell(fp); 135820747Sdavidn } 135921052Sdavidn /* 136021052Sdavidn * If we got an error of any sort, don't update! 136121052Sdavidn */ 136221052Sdavidn fclose(fp); 136320747Sdavidn } 136420747Sdavidn} 136520747Sdavidn 1366