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$"; 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> 4330259Scharnier#include <unistd.h> 4464918Sgreen#include <login_cap.h> 4520253Sjoerg#include "pw.h" 4620253Sjoerg#include "bitmap.h" 4720253Sjoerg 4823318Sache#define LOGNAMESIZE (MAXLOGNAME-1) 4922394Sdavidn 5052512Sdavidnstatic char locked_str[] = "*LOCKED*"; 5124214Sache 5244386Sdavidnstatic int print_user(struct passwd * pwd, int pretty, int v7); 5320253Sjoergstatic uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args); 5420253Sjoergstatic uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer); 5520253Sjoergstatic time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 5620253Sjoergstatic time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 5720253Sjoergstatic char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 5820253Sjoergstatic char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 5920253Sjoergstatic char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 6020253Sjoergstatic char *shell_path(char const * path, char *shells[], char *sh); 6120253Sjoergstatic void rmat(uid_t uid); 6285145Sachestatic void rmopie(char const * name); 6320253Sjoerg 6420253Sjoerg/*- 6520253Sjoerg * -C config configuration file 6620253Sjoerg * -q quiet operation 6720253Sjoerg * -n name login name 6820253Sjoerg * -u uid user id 6920253Sjoerg * -c comment user name/comment 7020253Sjoerg * -d directory home directory 7120253Sjoerg * -e date account expiry date 7220253Sjoerg * -p date password expiry date 7320253Sjoerg * -g grp primary group 7420253Sjoerg * -G grp1,grp2 additional groups 7520253Sjoerg * -m [ -k dir ] create and set up home 7620253Sjoerg * -s shell name of login shell 7720253Sjoerg * -o duplicate uid ok 7820253Sjoerg * -L class user class 7920253Sjoerg * -l name new login name 8020253Sjoerg * -h fd password filehandle 81124382Siedowse * -H fd encrypted password filehandle 8220253Sjoerg * -F force print or add 8320253Sjoerg * Setting defaults: 8420253Sjoerg * -D set user defaults 8520253Sjoerg * -b dir default home root dir 8620253Sjoerg * -e period default expiry period 8720253Sjoerg * -p period default password change period 8820253Sjoerg * -g group default group 8920253Sjoerg * -G grp1,grp2.. default additional groups 9020253Sjoerg * -L class default login class 9120253Sjoerg * -k dir default home skeleton 9220253Sjoerg * -s shell default shell 9320253Sjoerg * -w method default password method 9420253Sjoerg */ 9520253Sjoerg 9620253Sjoergint 9720253Sjoergpw_user(struct userconf * cnf, int mode, struct cargs * args) 9820253Sjoerg{ 9952527Sdavidn int rc, edited = 0; 10020253Sjoerg char *p = NULL; 10152512Sdavidn char *passtmp; 10220253Sjoerg struct carg *a_name; 10320253Sjoerg struct carg *a_uid; 10420253Sjoerg struct carg *arg; 10520253Sjoerg struct passwd *pwd = NULL; 10620253Sjoerg struct group *grp; 10720253Sjoerg struct stat st; 10820747Sdavidn char line[_PASSWORD_LEN+1]; 10982868Sdd FILE *fp; 110167919Sle char *dmode_c; 111167919Sle void *set = NULL; 11220253Sjoerg 11320253Sjoerg static struct passwd fakeuser = 11420253Sjoerg { 11520253Sjoerg NULL, 11620253Sjoerg "*", 11720253Sjoerg -1, 11820253Sjoerg -1, 11920253Sjoerg 0, 12020253Sjoerg "", 12120253Sjoerg "User &", 12256000Sdavidn "/nonexistent", 12320253Sjoerg "/bin/sh", 12420253Sjoerg 0 12556000Sdavidn#if defined(__FreeBSD__) 12656000Sdavidn ,0 12756000Sdavidn#endif 12820253Sjoerg }; 12920253Sjoerg 13052512Sdavidn 13120253Sjoerg /* 13220267Sjoerg * With M_NEXT, we only need to return the 13320267Sjoerg * next uid to stdout 13420267Sjoerg */ 13520267Sjoerg if (mode == M_NEXT) 13620267Sjoerg { 13720267Sjoerg uid_t next = pw_uidpolicy(cnf, args); 13820267Sjoerg if (getarg(args, 'q')) 13920267Sjoerg return next; 14020267Sjoerg printf("%ld:", (long)next); 14120267Sjoerg pw_group(cnf, mode, args); 14220267Sjoerg return EXIT_SUCCESS; 14320267Sjoerg } 14420267Sjoerg 14520267Sjoerg /* 14620253Sjoerg * We can do all of the common legwork here 14720253Sjoerg */ 14820253Sjoerg 14920253Sjoerg if ((arg = getarg(args, 'b')) != NULL) { 15020267Sjoerg cnf->home = arg->val; 15120253Sjoerg } 15221052Sdavidn 153167919Sle if ((arg = getarg(args, 'M')) != NULL) { 154167919Sle dmode_c = arg->val; 155167919Sle if ((set = setmode(dmode_c)) == NULL) 156167919Sle errx(EX_DATAERR, "invalid directory creation mode '%s'", 157167919Sle dmode_c); 158219408Sjkim cnf->homemode = getmode(set, _DEF_DIRMODE); 159167919Sle free(set); 160168044Sle } 161167919Sle 16221052Sdavidn /* 16321052Sdavidn * If we'll need to use it or we're updating it, 16421052Sdavidn * then create the base home directory if necessary 16521052Sdavidn */ 166224535Sdelphij if (arg != NULL || getarg(args, 'm') != NULL) { 16721052Sdavidn int l = strlen(cnf->home); 16821052Sdavidn 16921052Sdavidn if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 17021052Sdavidn cnf->home[--l] = '\0'; 17121052Sdavidn 17221052Sdavidn if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 17330259Scharnier errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); 17421052Sdavidn 17521052Sdavidn if (stat(cnf->home, &st) == -1) { 17621052Sdavidn char dbuf[MAXPATHLEN]; 17721052Sdavidn 17821242Sdavidn /* 17921242Sdavidn * This is a kludge especially for Joerg :) 18021242Sdavidn * If the home directory would be created in the root partition, then 18121242Sdavidn * we really create it under /usr which is likely to have more space. 18221242Sdavidn * But we create a symlink from cnf->home -> "/usr" -> cnf->home 18321242Sdavidn */ 18421242Sdavidn if (strchr(cnf->home+1, '/') == NULL) { 18521242Sdavidn strcpy(dbuf, "/usr"); 18621242Sdavidn strncat(dbuf, cnf->home, MAXPATHLEN-5); 187219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) { 18821242Sdavidn chown(dbuf, 0, 0); 189148584Spjd /* 190148584Spjd * Skip first "/" and create symlink: 191148584Spjd * /home -> usr/home 192148584Spjd */ 193148584Spjd symlink(dbuf+1, cnf->home); 19421242Sdavidn } 19521242Sdavidn /* If this falls, fall back to old method */ 19621242Sdavidn } 197130633Srobert strlcpy(dbuf, cnf->home, sizeof(dbuf)); 198130633Srobert p = dbuf; 19921242Sdavidn if (stat(dbuf, &st) == -1) { 20021242Sdavidn while ((p = strchr(++p, '/')) != NULL) { 20121242Sdavidn *p = '\0'; 20221242Sdavidn if (stat(dbuf, &st) == -1) { 203219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) == -1) 20421242Sdavidn goto direrr; 20521242Sdavidn chown(dbuf, 0, 0); 20621242Sdavidn } else if (!S_ISDIR(st.st_mode)) 20730259Scharnier errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); 20821242Sdavidn *p = '/'; 20921242Sdavidn } 21021052Sdavidn } 21121242Sdavidn if (stat(dbuf, &st) == -1) { 212219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) == -1) { 21330259Scharnier direrr: err(EX_OSFILE, "mkdir '%s'", dbuf); 21421052Sdavidn } 21521052Sdavidn chown(dbuf, 0, 0); 21621052Sdavidn } 21721052Sdavidn } else if (!S_ISDIR(st.st_mode)) 21830259Scharnier errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); 21921052Sdavidn } 22021052Sdavidn 22120253Sjoerg if ((arg = getarg(args, 'e')) != NULL) 22220253Sjoerg cnf->expire_days = atoi(arg->val); 22320253Sjoerg 22421330Sdavidn if ((arg = getarg(args, 'y')) != NULL) 22521330Sdavidn cnf->nispasswd = arg->val; 22621330Sdavidn 22720253Sjoerg if ((arg = getarg(args, 'p')) != NULL && arg->val) 22820253Sjoerg cnf->password_days = atoi(arg->val); 22920253Sjoerg 23020253Sjoerg if ((arg = getarg(args, 'g')) != NULL) { 23163596Sdavidn if (!*(p = arg->val)) /* Handle empty group list specially */ 23263596Sdavidn cnf->default_group = ""; 23363596Sdavidn else { 23463596Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 23563596Sdavidn if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 23663596Sdavidn errx(EX_NOUSER, "group `%s' does not exist", p); 23763596Sdavidn } 23863596Sdavidn cnf->default_group = newstr(grp->gr_name); 23920253Sjoerg } 24020253Sjoerg } 24120253Sjoerg if ((arg = getarg(args, 'L')) != NULL) 24220679Sdavidn cnf->default_class = pw_checkname((u_char *)arg->val, 0); 24320253Sjoerg 24420253Sjoerg if ((arg = getarg(args, 'G')) != NULL && arg->val) { 24552527Sdavidn int i = 0; 24620253Sjoerg 24720747Sdavidn for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 24844229Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 24961957Sache if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 25030259Scharnier errx(EX_NOUSER, "group `%s' does not exist", p); 25120253Sjoerg } 25220747Sdavidn if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 25320747Sdavidn cnf->groups[i++] = newstr(grp->gr_name); 25420253Sjoerg } 25520747Sdavidn while (i < cnf->numgroups) 25620253Sjoerg cnf->groups[i++] = NULL; 25720253Sjoerg } 25852527Sdavidn 25920253Sjoerg if ((arg = getarg(args, 'k')) != NULL) { 26026088Sdavidn if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode)) 26130259Scharnier errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir); 26220253Sjoerg } 26352527Sdavidn 26420253Sjoerg if ((arg = getarg(args, 's')) != NULL) 26520253Sjoerg cnf->shell_default = arg->val; 26620253Sjoerg 26763600Sdavidn if ((arg = getarg(args, 'w')) != NULL) 26863600Sdavidn cnf->default_password = boolean_val(arg->val, cnf->default_password); 26920253Sjoerg if (mode == M_ADD && getarg(args, 'D')) { 27020253Sjoerg if (getarg(args, 'n') != NULL) 27130259Scharnier errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 27220253Sjoerg if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 27320253Sjoerg if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 27420253Sjoerg cnf->min_uid = 1000; 27520253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 27620253Sjoerg cnf->max_uid = 32000; 27720253Sjoerg } 27820253Sjoerg if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 27920253Sjoerg if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 28020253Sjoerg cnf->min_gid = 1000; 28120253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 28220253Sjoerg cnf->max_gid = 32000; 28320253Sjoerg } 28420253Sjoerg 28520253Sjoerg arg = getarg(args, 'C'); 28620253Sjoerg if (write_userconfig(arg ? arg->val : NULL)) 28720267Sjoerg return EXIT_SUCCESS; 28830259Scharnier warn("config update"); 28920267Sjoerg return EX_IOERR; 29020253Sjoerg } 29152527Sdavidn 29220253Sjoerg if (mode == M_PRINT && getarg(args, 'a')) { 29320267Sjoerg int pretty = getarg(args, 'P') != NULL; 29444386Sdavidn int v7 = getarg(args, '7') != NULL; 29520253Sjoerg 29644229Sdavidn SETPWENT(); 29744229Sdavidn while ((pwd = GETPWENT()) != NULL) 29844386Sdavidn print_user(pwd, pretty, v7); 29944229Sdavidn ENDPWENT(); 30020267Sjoerg return EXIT_SUCCESS; 30120253Sjoerg } 30252527Sdavidn 30320253Sjoerg if ((a_name = getarg(args, 'n')) != NULL) 30444229Sdavidn pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0)); 30520253Sjoerg a_uid = getarg(args, 'u'); 30620253Sjoerg 30720253Sjoerg if (a_uid == NULL) { 30820253Sjoerg if (a_name == NULL) 30930259Scharnier errx(EX_DATAERR, "user name or id required"); 31020253Sjoerg 31120253Sjoerg /* 31220253Sjoerg * Determine whether 'n' switch is name or uid - we don't 31320253Sjoerg * really don't really care which we have, but we need to 31420253Sjoerg * know. 31520253Sjoerg */ 31643780Sdes if (mode != M_ADD && pwd == NULL 31743780Sdes && strspn(a_name->val, "0123456789") == strlen(a_name->val) 318242916Sbapt && *a_name->val) { 31920253Sjoerg (a_uid = a_name)->ch = 'u'; 32020253Sjoerg a_name = NULL; 32120253Sjoerg } 32220253Sjoerg } 32352527Sdavidn 32420253Sjoerg /* 32520253Sjoerg * Update, delete & print require that the user exists 32620253Sjoerg */ 32752512Sdavidn if (mode == M_UPDATE || mode == M_DELETE || 32852512Sdavidn mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) { 32952527Sdavidn 33020253Sjoerg if (a_name == NULL && pwd == NULL) /* Try harder */ 33144229Sdavidn pwd = GETPWUID(atoi(a_uid->val)); 33220253Sjoerg 33320253Sjoerg if (pwd == NULL) { 33420253Sjoerg if (mode == M_PRINT && getarg(args, 'F')) { 33520253Sjoerg fakeuser.pw_name = a_name ? a_name->val : "nouser"; 33620253Sjoerg fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 33744386Sdavidn return print_user(&fakeuser, 33844386Sdavidn getarg(args, 'P') != NULL, 33944386Sdavidn getarg(args, '7') != NULL); 34020253Sjoerg } 34120253Sjoerg if (a_name == NULL) 34230259Scharnier errx(EX_NOUSER, "no such uid `%s'", a_uid->val); 34330259Scharnier errx(EX_NOUSER, "no such user `%s'", a_name->val); 34420253Sjoerg } 34552527Sdavidn 34620253Sjoerg if (a_name == NULL) /* May be needed later */ 34720253Sjoerg a_name = addarg(args, 'n', newstr(pwd->pw_name)); 34820253Sjoerg 34920253Sjoerg /* 35052512Sdavidn * The M_LOCK and M_UNLOCK functions simply add or remove 35152512Sdavidn * a "*LOCKED*" prefix from in front of the password to 35252512Sdavidn * prevent it decoding correctly, and therefore prevents 35352512Sdavidn * access. Of course, this only prevents access via 35452512Sdavidn * password authentication (not ssh, kerberos or any 35552512Sdavidn * other method that does not use the UNIX password) but 35652512Sdavidn * that is a known limitation. 35752512Sdavidn */ 35852512Sdavidn 35952512Sdavidn if (mode == M_LOCK) { 36052512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0) 36152512Sdavidn errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 36252512Sdavidn passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str)); 36352512Sdavidn if (passtmp == NULL) /* disaster */ 36452512Sdavidn errx(EX_UNAVAILABLE, "out of memory"); 36552512Sdavidn strcpy(passtmp, locked_str); 36652512Sdavidn strcat(passtmp, pwd->pw_passwd); 36752512Sdavidn pwd->pw_passwd = passtmp; 36852527Sdavidn edited = 1; 36952512Sdavidn } else if (mode == M_UNLOCK) { 37052512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0) 37152512Sdavidn errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 37252512Sdavidn pwd->pw_passwd += sizeof(locked_str)-1; 37352527Sdavidn edited = 1; 37452527Sdavidn } else if (mode == M_DELETE) { 37552527Sdavidn /* 37652527Sdavidn * Handle deletions now 37752527Sdavidn */ 37820253Sjoerg char file[MAXPATHLEN]; 37920253Sjoerg char home[MAXPATHLEN]; 38020253Sjoerg uid_t uid = pwd->pw_uid; 38120253Sjoerg 38220253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 38330259Scharnier errx(EX_DATAERR, "cannot remove user 'root'"); 38420253Sjoerg 38544229Sdavidn if (!PWALTDIR()) { 38644229Sdavidn /* 38785145Sache * Remove opie record from /etc/opiekeys 38844229Sdavidn */ 38920747Sdavidn 39085145Sache rmopie(pwd->pw_name); 39120747Sdavidn 39244229Sdavidn /* 39344229Sdavidn * Remove crontabs 39444229Sdavidn */ 39544229Sdavidn sprintf(file, "/var/cron/tabs/%s", pwd->pw_name); 39644229Sdavidn if (access(file, F_OK) == 0) { 39744229Sdavidn sprintf(file, "crontab -u %s -r", pwd->pw_name); 39844229Sdavidn system(file); 39944229Sdavidn } 40020253Sjoerg } 40120253Sjoerg /* 40220253Sjoerg * Save these for later, since contents of pwd may be 40320253Sjoerg * invalidated by deletion 40420253Sjoerg */ 40520253Sjoerg sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 406130633Srobert strlcpy(home, pwd->pw_dir, sizeof(home)); 40720253Sjoerg 40852502Sdavidn rc = delpwent(pwd); 40952502Sdavidn if (rc == -1) 41052502Sdavidn err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 41152502Sdavidn else if (rc != 0) { 41256000Sdavidn warn("passwd update"); 41352502Sdavidn return EX_IOERR; 41452502Sdavidn } 41521330Sdavidn 41652502Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/') { 41752502Sdavidn rc = delnispwent(cnf->nispasswd, a_name->val); 41852502Sdavidn if (rc == -1) 41952502Sdavidn warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name); 42052502Sdavidn else if (rc != 0) 42156000Sdavidn warn("WARNING: NIS passwd update"); 42252502Sdavidn /* non-fatal */ 42352502Sdavidn } 42452502Sdavidn 42520253Sjoerg editgroups(a_name->val, NULL); 42620253Sjoerg 42720253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 42820253Sjoerg 42944229Sdavidn if (!PWALTDIR()) { 43044229Sdavidn /* 43144229Sdavidn * Remove mail file 43244229Sdavidn */ 43344229Sdavidn remove(file); 43420253Sjoerg 43544229Sdavidn /* 43644229Sdavidn * Remove at jobs 43744229Sdavidn */ 43844229Sdavidn if (getpwuid(uid) == NULL) 43944229Sdavidn rmat(uid); 44020253Sjoerg 44144229Sdavidn /* 44244229Sdavidn * Remove home directory and contents 44344229Sdavidn */ 44444229Sdavidn if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 44544229Sdavidn if (stat(home, &st) != -1) { 44644229Sdavidn rm_r(home, uid); 44744229Sdavidn pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 44844229Sdavidn a_name->val, (long) uid, home, 44944229Sdavidn stat(home, &st) == -1 ? "" : "not completely "); 45044229Sdavidn } 45120253Sjoerg } 45220253Sjoerg } 45320267Sjoerg return EXIT_SUCCESS; 45420253Sjoerg } else if (mode == M_PRINT) 45544386Sdavidn return print_user(pwd, 45644386Sdavidn getarg(args, 'P') != NULL, 45744386Sdavidn getarg(args, '7') != NULL); 45820253Sjoerg 45920253Sjoerg /* 46020253Sjoerg * The rest is edit code 46120253Sjoerg */ 46220253Sjoerg if ((arg = getarg(args, 'l')) != NULL) { 46320253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 46430259Scharnier errx(EX_DATAERR, "can't rename `root' account"); 46520679Sdavidn pwd->pw_name = pw_checkname((u_char *)arg->val, 0); 46652527Sdavidn edited = 1; 46720253Sjoerg } 46852527Sdavidn 46961957Sache if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) { 47020253Sjoerg pwd->pw_uid = (uid_t) atol(arg->val); 47152527Sdavidn edited = 1; 47220253Sjoerg if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 47330259Scharnier errx(EX_DATAERR, "can't change uid of `root' account"); 47420253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 47530259Scharnier warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 47620253Sjoerg } 47720253Sjoerg 47852527Sdavidn if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */ 47952527Sdavidn gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid; 48052527Sdavidn if (newgid != pwd->pw_gid) { 48152527Sdavidn edited = 1; 48261762Sdavidn pwd->pw_gid = newgid; 48352527Sdavidn } 48452527Sdavidn } 48552527Sdavidn 48620253Sjoerg if ((arg = getarg(args, 'p')) != NULL) { 48752527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 48852527Sdavidn if (pwd->pw_change != 0) { 48952527Sdavidn pwd->pw_change = 0; 49052527Sdavidn edited = 1; 49152527Sdavidn } 49252527Sdavidn } 49320253Sjoerg else { 49420253Sjoerg time_t now = time(NULL); 49520253Sjoerg time_t expire = parse_date(now, arg->val); 49620253Sjoerg 49752527Sdavidn if (pwd->pw_change != expire) { 49852527Sdavidn pwd->pw_change = expire; 49952527Sdavidn edited = 1; 50052527Sdavidn } 50120253Sjoerg } 50220253Sjoerg } 50352527Sdavidn 50420267Sjoerg if ((arg = getarg(args, 'e')) != NULL) { 50552527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 50652527Sdavidn if (pwd->pw_expire != 0) { 50752527Sdavidn pwd->pw_expire = 0; 50852527Sdavidn edited = 1; 50952527Sdavidn } 51052527Sdavidn } 51120253Sjoerg else { 51220253Sjoerg time_t now = time(NULL); 51320253Sjoerg time_t expire = parse_date(now, arg->val); 51420253Sjoerg 51552527Sdavidn if (pwd->pw_expire != expire) { 51652527Sdavidn pwd->pw_expire = expire; 51752527Sdavidn edited = 1; 51852527Sdavidn } 51920253Sjoerg } 52020253Sjoerg } 52120253Sjoerg 52252527Sdavidn if ((arg = getarg(args, 's')) != NULL) { 52352527Sdavidn char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 52452527Sdavidn if (shell == NULL) 52552527Sdavidn shell = ""; 52652527Sdavidn if (strcmp(shell, pwd->pw_shell) != 0) { 52752527Sdavidn pwd->pw_shell = shell; 52852527Sdavidn edited = 1; 52952527Sdavidn } 53052527Sdavidn } 53120253Sjoerg 53252527Sdavidn if (getarg(args, 'L')) { 53352527Sdavidn if (cnf->default_class == NULL) 53452527Sdavidn cnf->default_class = ""; 53552527Sdavidn if (strcmp(pwd->pw_class, cnf->default_class) != 0) { 53652527Sdavidn pwd->pw_class = cnf->default_class; 53752527Sdavidn edited = 1; 53852527Sdavidn } 53952527Sdavidn } 54052527Sdavidn 54120747Sdavidn if ((arg = getarg(args, 'd')) != NULL) { 542130629Srobert if (strcmp(pwd->pw_dir, arg->val)) 543130629Srobert edited = 1; 54420747Sdavidn if (stat(pwd->pw_dir = arg->val, &st) == -1) { 54520747Sdavidn if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 54630259Scharnier warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 54720747Sdavidn } else if (!S_ISDIR(st.st_mode)) 54830259Scharnier warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 54920747Sdavidn } 55020747Sdavidn 551124382Siedowse if ((arg = getarg(args, 'w')) != NULL && 552124382Siedowse getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) { 55364918Sgreen login_cap_t *lc; 55464918Sgreen 55564918Sgreen lc = login_getpwclass(pwd); 55664918Sgreen if (lc == NULL || 557253157Sdes login_setcryptfmt(lc, "sha512", NULL) == NULL) 55864918Sgreen warn("setting crypt(3) format"); 55964918Sgreen login_close(lc); 56020267Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 56152527Sdavidn edited = 1; 56252527Sdavidn } 56320267Sjoerg 56420253Sjoerg } else { 56564918Sgreen login_cap_t *lc; 56652527Sdavidn 56752527Sdavidn /* 56852527Sdavidn * Add code 56952527Sdavidn */ 57052527Sdavidn 57120253Sjoerg if (a_name == NULL) /* Required */ 57230259Scharnier errx(EX_DATAERR, "login name required"); 57344229Sdavidn else if ((pwd = GETPWNAM(a_name->val)) != NULL) /* Exists */ 57430259Scharnier errx(EX_DATAERR, "login name `%s' already exists", a_name->val); 57520253Sjoerg 57620253Sjoerg /* 57720253Sjoerg * Now, set up defaults for a new user 57820253Sjoerg */ 57920253Sjoerg pwd = &fakeuser; 58020253Sjoerg pwd->pw_name = a_name->val; 58120253Sjoerg pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 58220253Sjoerg pwd->pw_uid = pw_uidpolicy(cnf, args); 58320253Sjoerg pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 58420253Sjoerg pwd->pw_change = pw_pwdpolicy(cnf, args); 58520253Sjoerg pwd->pw_expire = pw_exppolicy(cnf, args); 58620253Sjoerg pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 58720253Sjoerg pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 58864918Sgreen lc = login_getpwclass(pwd); 58964918Sgreen if (lc == NULL || login_setcryptfmt(lc, "md5", NULL) == NULL) 59064918Sgreen warn("setting crypt(3) format"); 59164918Sgreen login_close(lc); 59264918Sgreen pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 59352527Sdavidn edited = 1; 59420253Sjoerg 59520253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 59630259Scharnier warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 59720253Sjoerg } 59820253Sjoerg 59920253Sjoerg /* 60020253Sjoerg * Shared add/edit code 60120253Sjoerg */ 60252527Sdavidn if ((arg = getarg(args, 'c')) != NULL) { 60352527Sdavidn char *gecos = pw_checkname((u_char *)arg->val, 1); 60452527Sdavidn if (strcmp(pwd->pw_gecos, gecos) != 0) { 60552527Sdavidn pwd->pw_gecos = gecos; 60652527Sdavidn edited = 1; 60752527Sdavidn } 60852527Sdavidn } 60920253Sjoerg 610124382Siedowse if ((arg = getarg(args, 'h')) != NULL || 611124382Siedowse (arg = getarg(args, 'H')) != NULL) { 61263572Sdavidn if (strcmp(arg->val, "-") == 0) { 61363572Sdavidn if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 61463572Sdavidn pwd->pw_passwd = "*"; /* No access */ 61563572Sdavidn edited = 1; 61663572Sdavidn } 61763572Sdavidn } else { 61820253Sjoerg int fd = atoi(arg->val); 619124382Siedowse int precrypt = (arg->ch == 'H'); 62020253Sjoerg int b; 62120253Sjoerg int istty = isatty(fd); 62220253Sjoerg struct termios t; 62364918Sgreen login_cap_t *lc; 62420253Sjoerg 62520253Sjoerg if (istty) { 62620253Sjoerg if (tcgetattr(fd, &t) == -1) 62720253Sjoerg istty = 0; 62820253Sjoerg else { 62920253Sjoerg struct termios n = t; 63020253Sjoerg 63120253Sjoerg /* Disable echo */ 63220253Sjoerg n.c_lflag &= ~(ECHO); 63320253Sjoerg tcsetattr(fd, TCSANOW, &n); 634124382Siedowse printf("%s%spassword for user %s:", 635124382Siedowse (mode == M_UPDATE) ? "new " : "", 636124382Siedowse precrypt ? "encrypted " : "", 637124382Siedowse pwd->pw_name); 63820253Sjoerg fflush(stdout); 63920253Sjoerg } 64020253Sjoerg } 64120253Sjoerg b = read(fd, line, sizeof(line) - 1); 64220253Sjoerg if (istty) { /* Restore state */ 64320253Sjoerg tcsetattr(fd, TCSANOW, &t); 64420253Sjoerg fputc('\n', stdout); 64520253Sjoerg fflush(stdout); 64620253Sjoerg } 64720253Sjoerg if (b < 0) { 648124382Siedowse warn("-%c file descriptor", precrypt ? 'H' : 649124382Siedowse 'h'); 65020267Sjoerg return EX_IOERR; 65120253Sjoerg } 65220253Sjoerg line[b] = '\0'; 653168045Sle if ((p = strpbrk(line, "\r\n")) != NULL) 65420253Sjoerg *p = '\0'; 65520253Sjoerg if (!*line) 65630259Scharnier errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 657124382Siedowse if (precrypt) { 658124382Siedowse if (strchr(line, ':') != NULL) 659124382Siedowse return EX_DATAERR; 660124382Siedowse pwd->pw_passwd = line; 661124382Siedowse } else { 662124382Siedowse lc = login_getpwclass(pwd); 663124382Siedowse if (lc == NULL || 664124382Siedowse login_setcryptfmt(lc, "md5", NULL) == NULL) 665124382Siedowse warn("setting crypt(3) format"); 666124382Siedowse login_close(lc); 667124382Siedowse pwd->pw_passwd = pw_pwcrypt(line); 668124382Siedowse } 66952527Sdavidn edited = 1; 67020253Sjoerg } 67120253Sjoerg } 67220267Sjoerg 67320267Sjoerg /* 67420267Sjoerg * Special case: -N only displays & exits 67520267Sjoerg */ 67620267Sjoerg if (getarg(args, 'N') != NULL) 67744386Sdavidn return print_user(pwd, 67844386Sdavidn getarg(args, 'P') != NULL, 67944386Sdavidn getarg(args, '7') != NULL); 68020267Sjoerg 68121330Sdavidn if (mode == M_ADD) { 68252527Sdavidn edited = 1; /* Always */ 68352502Sdavidn rc = addpwent(pwd); 68452502Sdavidn if (rc == -1) { 68552502Sdavidn warnx("user '%s' already exists", pwd->pw_name); 68652502Sdavidn return EX_IOERR; 68752502Sdavidn } else if (rc != 0) { 68856000Sdavidn warn("passwd file update"); 68952502Sdavidn return EX_IOERR; 69052502Sdavidn } 69152502Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/') { 69252502Sdavidn rc = addnispwent(cnf->nispasswd, pwd); 69352502Sdavidn if (rc == -1) 69452502Sdavidn warnx("User '%s' already exists in NIS passwd", pwd->pw_name); 69552502Sdavidn else 69656000Sdavidn warn("NIS passwd update"); 69752502Sdavidn /* NOTE: we treat NIS-only update errors as non-fatal */ 69852502Sdavidn } 69952512Sdavidn } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) { 70052527Sdavidn if (edited) { /* Only updated this if required */ 70152527Sdavidn rc = chgpwent(a_name->val, pwd); 70252527Sdavidn if (rc == -1) { 70352527Sdavidn warnx("user '%s' does not exist (NIS?)", pwd->pw_name); 70452527Sdavidn return EX_IOERR; 70552527Sdavidn } else if (rc != 0) { 70656000Sdavidn warn("passwd file update"); 70752527Sdavidn return EX_IOERR; 70852527Sdavidn } 70952527Sdavidn if ( cnf->nispasswd && *cnf->nispasswd=='/') { 71052527Sdavidn rc = chgnispwent(cnf->nispasswd, a_name->val, pwd); 71152527Sdavidn if (rc == -1) 71252527Sdavidn warn("User '%s' not found in NIS passwd", pwd->pw_name); 71352527Sdavidn else 71456000Sdavidn warn("NIS passwd update"); 71552527Sdavidn /* NOTE: NIS-only update errors are not fatal */ 71652527Sdavidn } 71752502Sdavidn } 71821330Sdavidn } 71921330Sdavidn 72020253Sjoerg /* 72120253Sjoerg * Ok, user is created or changed - now edit group file 72220253Sjoerg */ 72320253Sjoerg 72420253Sjoerg if (mode == M_ADD || getarg(args, 'G') != NULL) 72520253Sjoerg editgroups(pwd->pw_name, cnf->groups); 72620253Sjoerg 72761759Sdavidn /* go get a current version of pwd */ 72861759Sdavidn pwd = GETPWNAM(a_name->val); 72961759Sdavidn if (pwd == NULL) { 73061759Sdavidn /* This will fail when we rename, so special case that */ 73161759Sdavidn if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) { 73261759Sdavidn a_name->val = arg->val; /* update new name */ 73361759Sdavidn pwd = GETPWNAM(a_name->val); /* refetch renamed rec */ 73461759Sdavidn } 73561759Sdavidn } 73661759Sdavidn if (pwd == NULL) /* can't go on without this */ 73730259Scharnier errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val); 73820253Sjoerg 73944229Sdavidn grp = GETGRGID(pwd->pw_gid); 74098744Sdwmalone pw_log(cnf, mode, W_USER, "%s(%ld):%s(%ld):%s:%s:%s", 74120253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 74220253Sjoerg grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 74320253Sjoerg pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 74420253Sjoerg 74520253Sjoerg /* 74620253Sjoerg * If adding, let's touch and chown the user's mail file. This is not 74720253Sjoerg * strictly necessary under BSD with a 0755 maildir but it also 74820253Sjoerg * doesn't hurt anything to create the empty mailfile 74920253Sjoerg */ 75020253Sjoerg if (mode == M_ADD) { 75144229Sdavidn if (!PWALTDIR()) { 75244229Sdavidn sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 75344229Sdavidn close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 75444229Sdavidn * mtime */ 75544229Sdavidn chown(line, pwd->pw_uid, pwd->pw_gid); 75620253Sjoerg } 75720253Sjoerg } 75852527Sdavidn 75920253Sjoerg /* 76082868Sdd * Let's create and populate the user's home directory. Note 76120253Sjoerg * that this also `works' for editing users if -m is used, but 76220253Sjoerg * existing files will *not* be overwritten. 76320253Sjoerg */ 76444229Sdavidn if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 765168044Sle copymkdir(pwd->pw_dir, cnf->dotdir, cnf->homemode, pwd->pw_uid, pwd->pw_gid); 76620253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 76720253Sjoerg pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 76820253Sjoerg } 76952527Sdavidn 77082868Sdd 77182868Sdd /* 77282868Sdd * Finally, send mail to the new user as well, if we are asked to 77382868Sdd */ 77482868Sdd if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 77582868Sdd FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 77682868Sdd 77782868Sdd if (pfp == NULL) 77882868Sdd warn("sendmail"); 77982868Sdd else { 78082868Sdd fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 78182868Sdd while (fgets(line, sizeof(line), fp) != NULL) { 78282868Sdd /* Do substitutions? */ 78382868Sdd fputs(line, pfp); 78482868Sdd } 78582868Sdd pclose(pfp); 78682868Sdd pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 78782868Sdd pwd->pw_name, (long) pwd->pw_uid); 78882868Sdd } 78982868Sdd fclose(fp); 79082868Sdd } 79182868Sdd 79220267Sjoerg return EXIT_SUCCESS; 79320253Sjoerg} 79420253Sjoerg 79520253Sjoerg 79620253Sjoergstatic uid_t 79720253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args) 79820253Sjoerg{ 79920253Sjoerg struct passwd *pwd; 80020253Sjoerg uid_t uid = (uid_t) - 1; 80120253Sjoerg struct carg *a_uid = getarg(args, 'u'); 80220253Sjoerg 80320253Sjoerg /* 80420253Sjoerg * Check the given uid, if any 80520253Sjoerg */ 80620253Sjoerg if (a_uid != NULL) { 80720253Sjoerg uid = (uid_t) atol(a_uid->val); 80820253Sjoerg 80944229Sdavidn if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL) 81030259Scharnier errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid); 81120253Sjoerg } else { 81220253Sjoerg struct bitmap bm; 81320253Sjoerg 81420253Sjoerg /* 81520253Sjoerg * We need to allocate the next available uid under one of 81620253Sjoerg * two policies a) Grab the first unused uid b) Grab the 81720253Sjoerg * highest possible unused uid 81820253Sjoerg */ 81920253Sjoerg if (cnf->min_uid >= cnf->max_uid) { /* Sanity 82020253Sjoerg * claus^H^H^H^Hheck */ 82120253Sjoerg cnf->min_uid = 1000; 82220253Sjoerg cnf->max_uid = 32000; 82320253Sjoerg } 82420253Sjoerg bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 82520253Sjoerg 82620253Sjoerg /* 82720253Sjoerg * Now, let's fill the bitmap from the password file 82820253Sjoerg */ 82944229Sdavidn SETPWENT(); 83044229Sdavidn while ((pwd = GETPWENT()) != NULL) 83144229Sdavidn if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 83220253Sjoerg bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 83344229Sdavidn ENDPWENT(); 83420253Sjoerg 83520253Sjoerg /* 83620253Sjoerg * Then apply the policy, with fallback to reuse if necessary 83720253Sjoerg */ 83820253Sjoerg if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 83920253Sjoerg uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 84020253Sjoerg 84120253Sjoerg /* 84220253Sjoerg * Another sanity check 84320253Sjoerg */ 84420253Sjoerg if (uid < cnf->min_uid || uid > cnf->max_uid) 84530259Scharnier errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 84620253Sjoerg bm_dealloc(&bm); 84720253Sjoerg } 84820253Sjoerg return uid; 84920253Sjoerg} 85020253Sjoerg 85120253Sjoerg 85220253Sjoergstatic uid_t 85320253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 85420253Sjoerg{ 85520253Sjoerg struct group *grp; 85620253Sjoerg gid_t gid = (uid_t) - 1; 85720253Sjoerg struct carg *a_gid = getarg(args, 'g'); 85820253Sjoerg 85920253Sjoerg /* 86020253Sjoerg * If no arg given, see if default can help out 86120253Sjoerg */ 86220253Sjoerg if (a_gid == NULL && cnf->default_group && *cnf->default_group) 86320253Sjoerg a_gid = addarg(args, 'g', cnf->default_group); 86420253Sjoerg 86520253Sjoerg /* 86620253Sjoerg * Check the given gid, if any 86720253Sjoerg */ 86844229Sdavidn SETGRENT(); 86920253Sjoerg if (a_gid != NULL) { 87044229Sdavidn if ((grp = GETGRNAM(a_gid->val)) == NULL) { 87120253Sjoerg gid = (gid_t) atol(a_gid->val); 87261957Sache if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL) 87330259Scharnier errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 87420253Sjoerg } 87520253Sjoerg gid = grp->gr_gid; 87644229Sdavidn } else if ((grp = GETGRNAM(nam)) != NULL && grp->gr_mem[0] == NULL) { 87720267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 87820253Sjoerg } else { 87920253Sjoerg struct cargs grpargs; 88020253Sjoerg char tmp[32]; 88120253Sjoerg 88220253Sjoerg LIST_INIT(&grpargs); 88320253Sjoerg addarg(&grpargs, 'n', nam); 88420253Sjoerg 88520253Sjoerg /* 88620253Sjoerg * We need to auto-create a group with the user's name. We 88720253Sjoerg * can send all the appropriate output to our sister routine 88820253Sjoerg * bit first see if we can create a group with gid==uid so we 88920253Sjoerg * can keep the user and group ids in sync. We purposely do 89020253Sjoerg * NOT check the gid range if we can force the sync. If the 89120253Sjoerg * user's name dups an existing group, then the group add 89220253Sjoerg * function will happily handle that case for us and exit. 89320253Sjoerg */ 89444229Sdavidn if (GETGRGID(prefer) == NULL) { 89520253Sjoerg sprintf(tmp, "%lu", (unsigned long) prefer); 89620253Sjoerg addarg(&grpargs, 'g', tmp); 89720253Sjoerg } 89820267Sjoerg if (getarg(args, 'N')) 89920267Sjoerg { 90020267Sjoerg addarg(&grpargs, 'N', NULL); 90120267Sjoerg addarg(&grpargs, 'q', NULL); 90220267Sjoerg gid = pw_group(cnf, M_NEXT, &grpargs); 90320267Sjoerg } 90420267Sjoerg else 90520267Sjoerg { 90620267Sjoerg pw_group(cnf, M_ADD, &grpargs); 90744229Sdavidn if ((grp = GETGRNAM(nam)) != NULL) 90820267Sjoerg gid = grp->gr_gid; 90920267Sjoerg } 91070486Sben a_gid = LIST_FIRST(&grpargs); 91120253Sjoerg while (a_gid != NULL) { 91270486Sben struct carg *t = LIST_NEXT(a_gid, list); 91320253Sjoerg LIST_REMOVE(a_gid, list); 91420253Sjoerg a_gid = t; 91520253Sjoerg } 91620253Sjoerg } 91744229Sdavidn ENDGRENT(); 91820253Sjoerg return gid; 91920253Sjoerg} 92020253Sjoerg 92120253Sjoerg 92220253Sjoergstatic time_t 92320253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args) 92420253Sjoerg{ 92520253Sjoerg time_t result = 0; 92620253Sjoerg time_t now = time(NULL); 92727831Sdavidn struct carg *arg = getarg(args, 'p'); 92820253Sjoerg 92920253Sjoerg if (arg != NULL) { 93020253Sjoerg if ((result = parse_date(now, arg->val)) == now) 93130259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 93220253Sjoerg } else if (cnf->password_days > 0) 93320253Sjoerg result = now + ((long) cnf->password_days * 86400L); 93420253Sjoerg return result; 93520253Sjoerg} 93620253Sjoerg 93720253Sjoerg 93820253Sjoergstatic time_t 93920253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args) 94020253Sjoerg{ 94120253Sjoerg time_t result = 0; 94220253Sjoerg time_t now = time(NULL); 94320253Sjoerg struct carg *arg = getarg(args, 'e'); 94420253Sjoerg 94520253Sjoerg if (arg != NULL) { 94620253Sjoerg if ((result = parse_date(now, arg->val)) == now) 94730259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 94820253Sjoerg } else if (cnf->expire_days > 0) 94920253Sjoerg result = now + ((long) cnf->expire_days * 86400L); 95020253Sjoerg return result; 95120253Sjoerg} 95220253Sjoerg 95320253Sjoerg 95420253Sjoergstatic char * 95520253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 95620253Sjoerg{ 95720253Sjoerg struct carg *arg = getarg(args, 'd'); 95820253Sjoerg 95920253Sjoerg if (arg) 96020253Sjoerg return arg->val; 96120253Sjoerg else { 96220253Sjoerg static char home[128]; 96320253Sjoerg 96420253Sjoerg if (cnf->home == NULL || *cnf->home == '\0') 96530259Scharnier errx(EX_CONFIG, "no base home directory set"); 96620253Sjoerg sprintf(home, "%s/%s", cnf->home, user); 96720253Sjoerg return home; 96820253Sjoerg } 96920253Sjoerg} 97020253Sjoerg 97120253Sjoergstatic char * 97220253Sjoergshell_path(char const * path, char *shells[], char *sh) 97320253Sjoerg{ 97420253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 97520253Sjoerg return sh; /* specified full path or forced none */ 97620253Sjoerg else { 97720253Sjoerg char *p; 97820253Sjoerg char paths[_UC_MAXLINE]; 97920253Sjoerg 98020253Sjoerg /* 98120253Sjoerg * We need to search paths 98220253Sjoerg */ 983130633Srobert strlcpy(paths, path, sizeof(paths)); 98420253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 98520253Sjoerg int i; 98620253Sjoerg static char shellpath[256]; 98720253Sjoerg 98820253Sjoerg if (sh != NULL) { 98920253Sjoerg sprintf(shellpath, "%s/%s", p, sh); 99020253Sjoerg if (access(shellpath, X_OK) == 0) 99120253Sjoerg return shellpath; 99220253Sjoerg } else 99320253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 99420253Sjoerg sprintf(shellpath, "%s/%s", p, shells[i]); 99520253Sjoerg if (access(shellpath, X_OK) == 0) 99620253Sjoerg return shellpath; 99720253Sjoerg } 99820253Sjoerg } 99920253Sjoerg if (sh == NULL) 100030259Scharnier errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 100130259Scharnier errx(EX_CONFIG, "no default shell available or defined"); 100220253Sjoerg return NULL; 100320253Sjoerg } 100420253Sjoerg} 100520253Sjoerg 100620253Sjoerg 100720253Sjoergstatic char * 100820253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 100920253Sjoerg{ 101020253Sjoerg char *sh = newshell; 101120253Sjoerg struct carg *arg = getarg(args, 's'); 101220253Sjoerg 101320253Sjoerg if (newshell == NULL && arg != NULL) 101420253Sjoerg sh = arg->val; 101520253Sjoerg return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 101620253Sjoerg} 101720253Sjoerg 1018179365Santoine#define SALTSIZE 32 101920253Sjoerg 1020179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"; 1021179365Santoine 102220253Sjoergchar * 102320253Sjoergpw_pwcrypt(char *password) 102420253Sjoerg{ 102520253Sjoerg int i; 1026179365Santoine char salt[SALTSIZE + 1]; 102720253Sjoerg 102820253Sjoerg static char buf[256]; 102920253Sjoerg 103020253Sjoerg /* 103120253Sjoerg * Calculate a salt value 103220253Sjoerg */ 1033179365Santoine for (i = 0; i < SALTSIZE; i++) 1034181785Sache salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)]; 1035179365Santoine salt[SALTSIZE] = '\0'; 103620253Sjoerg 103720253Sjoerg return strcpy(buf, crypt(password, salt)); 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 */ 1057124382Siedowse if (getarg(args, 'h') == NULL && getarg(args, 'H') == NULL && 1058124382Siedowse getarg(args, 'N') == NULL) { 105961957Sache if (isatty(STDOUT_FILENO)) 106020712Sdavidn printf("Password for '%s' is: ", user); 106120253Sjoerg printf("%s\n", pwbuf); 106220253Sjoerg fflush(stdout); 106320253Sjoerg } 106420253Sjoerg break; 106520253Sjoerg 106620253Sjoerg case -2: /* No password at all! */ 106720253Sjoerg return ""; 106820253Sjoerg 106920253Sjoerg case 0: /* No login - default */ 107020253Sjoerg default: 107120253Sjoerg return "*"; 107220253Sjoerg 107320253Sjoerg case 1: /* user's name */ 1074130633Srobert strlcpy(pwbuf, user, sizeof(pwbuf)); 107520253Sjoerg break; 107620253Sjoerg } 107720253Sjoerg return pw_pwcrypt(pwbuf); 107820253Sjoerg} 107920253Sjoerg 108020253Sjoerg 108120253Sjoergstatic int 108244386Sdavidnprint_user(struct passwd * pwd, int pretty, int v7) 108320253Sjoerg{ 108420253Sjoerg if (!pretty) { 108520253Sjoerg char buf[_UC_MAXLINE]; 108620253Sjoerg 108744386Sdavidn fmtpwentry(buf, pwd, v7 ? PWF_PASSWD : PWF_STANDARD); 108820253Sjoerg fputs(buf, stdout); 108920253Sjoerg } else { 109020267Sjoerg int j; 109120253Sjoerg char *p; 109244229Sdavidn struct group *grp = GETGRGID(pwd->pw_gid); 109320253Sjoerg char uname[60] = "User &", office[60] = "[None]", 109420253Sjoerg wphone[60] = "[None]", hphone[60] = "[None]"; 109520590Sdavidn char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 109620590Sdavidn struct tm * tptr; 109720253Sjoerg 109820253Sjoerg if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 1099130633Srobert strlcpy(uname, p, sizeof(uname)); 110020253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1101130633Srobert strlcpy(office, p, sizeof(office)); 110220253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1103130633Srobert strlcpy(wphone, p, sizeof(wphone)); 110420253Sjoerg if ((p = strtok(NULL, "")) != NULL) { 1105130633Srobert strlcpy(hphone, p, 1106130633Srobert sizeof(hphone)); 110720253Sjoerg } 110820253Sjoerg } 110920253Sjoerg } 111020253Sjoerg } 111120253Sjoerg /* 111220253Sjoerg * Handle '&' in gecos field 111320253Sjoerg */ 111420253Sjoerg if ((p = strchr(uname, '&')) != NULL) { 111520253Sjoerg int l = strlen(pwd->pw_name); 111620253Sjoerg int m = strlen(p); 111720253Sjoerg 111820253Sjoerg memmove(p + l, p + 1, m); 111920253Sjoerg memmove(p, pwd->pw_name, l); 112061957Sache *p = (char) toupper((unsigned char)*p); 112120253Sjoerg } 112220590Sdavidn if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 112374569Sache strftime(acexpire, sizeof acexpire, "%c", tptr); 112461957Sache if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 112574569Sache strftime(pwexpire, sizeof pwexpire, "%c", tptr); 112622394Sdavidn printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n" 112720747Sdavidn " Full Name: %s\n" 112820747Sdavidn " Home: %-26.26s Class: %s\n" 112920747Sdavidn " Shell: %-26.26s Office: %s\n" 113020747Sdavidn "Work Phone: %-26.26s Home Phone: %s\n" 113120747Sdavidn "Acc Expire: %-26.26s Pwd Expire: %s\n", 113220253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 113320253Sjoerg grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 113420253Sjoerg uname, pwd->pw_dir, pwd->pw_class, 113520590Sdavidn pwd->pw_shell, office, wphone, hphone, 113620590Sdavidn acexpire, pwexpire); 113744229Sdavidn SETGRENT(); 113820267Sjoerg j = 0; 113944229Sdavidn while ((grp=GETGRENT()) != NULL) 114020267Sjoerg { 114120267Sjoerg int i = 0; 114220747Sdavidn while (grp->gr_mem[i] != NULL) 114320267Sjoerg { 114420267Sjoerg if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 114520267Sjoerg { 114620747Sdavidn printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 114720267Sjoerg break; 114820267Sjoerg } 114920267Sjoerg ++i; 115020267Sjoerg } 115120267Sjoerg } 115244229Sdavidn ENDGRENT(); 115361957Sache printf("%s", j ? "\n" : ""); 115420253Sjoerg } 115520267Sjoerg return EXIT_SUCCESS; 115620253Sjoerg} 115720253Sjoerg 115820679Sdavidnchar * 115920679Sdavidnpw_checkname(u_char *name, int gecos) 116020253Sjoerg{ 1161109961Sgad char showch[8]; 1162109961Sgad u_char const *badchars, *ch, *showtype; 1163109961Sgad int reject; 116420253Sjoerg 1165109961Sgad ch = name; 1166109961Sgad reject = 0; 1167109961Sgad if (gecos) { 1168109961Sgad /* See if the name is valid as a gecos (comment) field. */ 1169109961Sgad badchars = ":!@"; 1170109961Sgad showtype = "gecos field"; 1171109961Sgad } else { 1172109961Sgad /* See if the name is valid as a userid or group. */ 1173109961Sgad badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\""; 1174109961Sgad showtype = "userid/group name"; 1175109961Sgad /* Userids and groups can not have a leading '-'. */ 1176109961Sgad if (*ch == '-') 1177109961Sgad reject = 1; 117820253Sjoerg } 1179109961Sgad if (!reject) { 1180109961Sgad while (*ch) { 1181109961Sgad if (strchr(badchars, *ch) != NULL || *ch < ' ' || 1182109961Sgad *ch == 127) { 1183109961Sgad reject = 1; 1184109961Sgad break; 1185109961Sgad } 1186109961Sgad /* 8-bit characters are only allowed in GECOS fields */ 1187109961Sgad if (!gecos && (*ch & 0x80)) { 1188109961Sgad reject = 1; 1189109961Sgad break; 1190109961Sgad } 1191109961Sgad ch++; 1192109961Sgad } 1193109961Sgad } 1194109961Sgad /* 1195109961Sgad * A `$' is allowed as the final character for userids and groups, 1196109961Sgad * mainly for the benefit of samba. 1197109961Sgad */ 1198109961Sgad if (reject && !gecos) { 1199109961Sgad if (*ch == '$' && *(ch + 1) == '\0') { 1200109961Sgad reject = 0; 1201109961Sgad ch++; 1202109961Sgad } 1203109961Sgad } 1204109961Sgad if (reject) { 1205109961Sgad snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127) 1206109961Sgad ? "`%c'" : "0x%02x", *ch); 1207229237Sdim errx(EX_DATAERR, "invalid character %s at position %td in %s", 1208109961Sgad showch, (ch - name), showtype); 1209109961Sgad } 1210109961Sgad if (!gecos && (ch - name) > LOGNAMESIZE) 1211109961Sgad errx(EX_DATAERR, "name too long `%s' (max is %d)", name, 1212109961Sgad LOGNAMESIZE); 121320679Sdavidn return (char *)name; 121420253Sjoerg} 121520253Sjoerg 121620253Sjoerg 121720253Sjoergstatic void 121820253Sjoergrmat(uid_t uid) 121920253Sjoerg{ 122020253Sjoerg DIR *d = opendir("/var/at/jobs"); 122120253Sjoerg 122220253Sjoerg if (d != NULL) { 122320253Sjoerg struct dirent *e; 122420253Sjoerg 122520253Sjoerg while ((e = readdir(d)) != NULL) { 122620253Sjoerg struct stat st; 122720253Sjoerg 122820253Sjoerg if (strncmp(e->d_name, ".lock", 5) != 0 && 122920253Sjoerg stat(e->d_name, &st) == 0 && 123020253Sjoerg !S_ISDIR(st.st_mode) && 123120253Sjoerg st.st_uid == uid) { 123220253Sjoerg char tmp[MAXPATHLEN]; 123320253Sjoerg 123420253Sjoerg sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 123520253Sjoerg system(tmp); 123620253Sjoerg } 123720253Sjoerg } 123820253Sjoerg closedir(d); 123920253Sjoerg } 124020253Sjoerg} 124120747Sdavidn 124220747Sdavidnstatic void 124385145Sachermopie(char const * name) 124420747Sdavidn{ 124585145Sache static const char etcopie[] = "/etc/opiekeys"; 124685145Sache FILE *fp = fopen(etcopie, "r+"); 124720747Sdavidn 124821052Sdavidn if (fp != NULL) { 124921052Sdavidn char tmp[1024]; 125021052Sdavidn off_t atofs = 0; 125121052Sdavidn int length = strlen(name); 125220747Sdavidn 125321052Sdavidn while (fgets(tmp, sizeof tmp, fp) != NULL) { 125421052Sdavidn if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 125521052Sdavidn if (fseek(fp, atofs, SEEK_SET) == 0) { 125621052Sdavidn fwrite("#", 1, 1, fp); /* Comment username out */ 125721052Sdavidn } 125821052Sdavidn break; 125920747Sdavidn } 126021052Sdavidn atofs = ftell(fp); 126120747Sdavidn } 126221052Sdavidn /* 126321052Sdavidn * If we got an error of any sort, don't update! 126421052Sdavidn */ 126521052Sdavidn fclose(fp); 126620747Sdavidn } 126720747Sdavidn} 126820747Sdavidn 1269