pw_user.c revision 282699
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 282699 2015-05-09 22:43:44Z 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 5444386Sdavidnstatic int print_user(struct passwd * pwd, int pretty, int v7); 5520253Sjoergstatic uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args); 5620253Sjoergstatic uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer); 5720253Sjoergstatic time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 5820253Sjoergstatic time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 5920253Sjoergstatic char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 6020253Sjoergstatic char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 6120253Sjoergstatic char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 6220253Sjoergstatic char *shell_path(char const * path, char *shells[], char *sh); 6320253Sjoergstatic void rmat(uid_t uid); 6485145Sachestatic void rmopie(char const * name); 6520253Sjoerg 6620253Sjoerg/*- 6720253Sjoerg * -C config configuration file 6820253Sjoerg * -q quiet operation 6920253Sjoerg * -n name login name 7020253Sjoerg * -u uid user id 7120253Sjoerg * -c comment user name/comment 7220253Sjoerg * -d directory home directory 7320253Sjoerg * -e date account expiry date 7420253Sjoerg * -p date password expiry date 7520253Sjoerg * -g grp primary group 7620253Sjoerg * -G grp1,grp2 additional groups 7720253Sjoerg * -m [ -k dir ] create and set up home 7820253Sjoerg * -s shell name of login shell 7920253Sjoerg * -o duplicate uid ok 8020253Sjoerg * -L class user class 8120253Sjoerg * -l name new login name 8220253Sjoerg * -h fd password filehandle 83124382Siedowse * -H fd encrypted password filehandle 8420253Sjoerg * -F force print or add 8520253Sjoerg * Setting defaults: 8620253Sjoerg * -D set user defaults 8720253Sjoerg * -b dir default home root dir 8820253Sjoerg * -e period default expiry period 8920253Sjoerg * -p period default password change period 9020253Sjoerg * -g group default group 9120253Sjoerg * -G grp1,grp2.. default additional groups 9220253Sjoerg * -L class default login class 9320253Sjoerg * -k dir default home skeleton 9420253Sjoerg * -s shell default shell 9520253Sjoerg * -w method default password method 9620253Sjoerg */ 9720253Sjoerg 9820253Sjoergint 9920253Sjoergpw_user(struct userconf * cnf, int mode, struct cargs * args) 10020253Sjoerg{ 10152527Sdavidn int rc, edited = 0; 10220253Sjoerg char *p = NULL; 10352512Sdavidn char *passtmp; 10420253Sjoerg struct carg *a_name; 10520253Sjoerg struct carg *a_uid; 10620253Sjoerg struct carg *arg; 10720253Sjoerg struct passwd *pwd = NULL; 10820253Sjoerg struct group *grp; 10920253Sjoerg struct stat st; 11020747Sdavidn char line[_PASSWORD_LEN+1]; 11182868Sdd FILE *fp; 112167919Sle char *dmode_c; 113167919Sle void *set = NULL; 11420253Sjoerg 11520253Sjoerg static struct passwd fakeuser = 11620253Sjoerg { 11720253Sjoerg NULL, 11820253Sjoerg "*", 11920253Sjoerg -1, 12020253Sjoerg -1, 12120253Sjoerg 0, 12220253Sjoerg "", 12320253Sjoerg "User &", 12456000Sdavidn "/nonexistent", 12520253Sjoerg "/bin/sh", 12620253Sjoerg 0 12756000Sdavidn#if defined(__FreeBSD__) 12856000Sdavidn ,0 12956000Sdavidn#endif 13020253Sjoerg }; 13120253Sjoerg 13252512Sdavidn 13320253Sjoerg /* 13420267Sjoerg * With M_NEXT, we only need to return the 13520267Sjoerg * next uid to stdout 13620267Sjoerg */ 13720267Sjoerg if (mode == M_NEXT) 13820267Sjoerg { 13920267Sjoerg uid_t next = pw_uidpolicy(cnf, args); 14020267Sjoerg if (getarg(args, 'q')) 14120267Sjoerg return next; 14220267Sjoerg printf("%ld:", (long)next); 14320267Sjoerg pw_group(cnf, mode, args); 14420267Sjoerg return EXIT_SUCCESS; 14520267Sjoerg } 14620267Sjoerg 14720267Sjoerg /* 14820253Sjoerg * We can do all of the common legwork here 14920253Sjoerg */ 15020253Sjoerg 15120253Sjoerg if ((arg = getarg(args, 'b')) != NULL) { 15220267Sjoerg cnf->home = arg->val; 15320253Sjoerg } 15421052Sdavidn 155167919Sle if ((arg = getarg(args, 'M')) != NULL) { 156167919Sle dmode_c = arg->val; 157167919Sle if ((set = setmode(dmode_c)) == NULL) 158167919Sle errx(EX_DATAERR, "invalid directory creation mode '%s'", 159167919Sle dmode_c); 160219408Sjkim cnf->homemode = getmode(set, _DEF_DIRMODE); 161167919Sle free(set); 162168044Sle } 163167919Sle 16421052Sdavidn /* 16521052Sdavidn * If we'll need to use it or we're updating it, 16621052Sdavidn * then create the base home directory if necessary 16721052Sdavidn */ 168224535Sdelphij if (arg != NULL || getarg(args, 'm') != NULL) { 16921052Sdavidn int l = strlen(cnf->home); 17021052Sdavidn 17121052Sdavidn if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 17221052Sdavidn cnf->home[--l] = '\0'; 17321052Sdavidn 17421052Sdavidn if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 17530259Scharnier errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); 17621052Sdavidn 17721052Sdavidn if (stat(cnf->home, &st) == -1) { 17821052Sdavidn char dbuf[MAXPATHLEN]; 17921052Sdavidn 18021242Sdavidn /* 18121242Sdavidn * This is a kludge especially for Joerg :) 18221242Sdavidn * If the home directory would be created in the root partition, then 18321242Sdavidn * we really create it under /usr which is likely to have more space. 18421242Sdavidn * But we create a symlink from cnf->home -> "/usr" -> cnf->home 18521242Sdavidn */ 18621242Sdavidn if (strchr(cnf->home+1, '/') == NULL) { 187282683Sbapt snprintf(dbuf, MAXPATHLEN, "/usr%s", cnf->home); 188219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) { 18921242Sdavidn chown(dbuf, 0, 0); 190148584Spjd /* 191148584Spjd * Skip first "/" and create symlink: 192148584Spjd * /home -> usr/home 193148584Spjd */ 194148584Spjd symlink(dbuf+1, cnf->home); 19521242Sdavidn } 19621242Sdavidn /* If this falls, fall back to old method */ 19721242Sdavidn } 198130633Srobert strlcpy(dbuf, cnf->home, sizeof(dbuf)); 199130633Srobert p = dbuf; 20021242Sdavidn if (stat(dbuf, &st) == -1) { 201252377Skientzle while ((p = strchr(p + 1, '/')) != NULL) { 20221242Sdavidn *p = '\0'; 20321242Sdavidn if (stat(dbuf, &st) == -1) { 204219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) == -1) 20521242Sdavidn goto direrr; 20621242Sdavidn chown(dbuf, 0, 0); 20721242Sdavidn } else if (!S_ISDIR(st.st_mode)) 20830259Scharnier errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); 20921242Sdavidn *p = '/'; 21021242Sdavidn } 21121052Sdavidn } 21221242Sdavidn if (stat(dbuf, &st) == -1) { 213219408Sjkim if (mkdir(dbuf, _DEF_DIRMODE) == -1) { 21430259Scharnier direrr: err(EX_OSFILE, "mkdir '%s'", dbuf); 21521052Sdavidn } 21621052Sdavidn chown(dbuf, 0, 0); 21721052Sdavidn } 21821052Sdavidn } else if (!S_ISDIR(st.st_mode)) 21930259Scharnier errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); 22021052Sdavidn } 22121052Sdavidn 22220253Sjoerg if ((arg = getarg(args, 'e')) != NULL) 22320253Sjoerg cnf->expire_days = atoi(arg->val); 22420253Sjoerg 22521330Sdavidn if ((arg = getarg(args, 'y')) != NULL) 22621330Sdavidn cnf->nispasswd = arg->val; 22721330Sdavidn 22820253Sjoerg if ((arg = getarg(args, 'p')) != NULL && arg->val) 22920253Sjoerg cnf->password_days = atoi(arg->val); 23020253Sjoerg 23120253Sjoerg if ((arg = getarg(args, 'g')) != NULL) { 23263596Sdavidn if (!*(p = arg->val)) /* Handle empty group list specially */ 23363596Sdavidn cnf->default_group = ""; 23463596Sdavidn else { 23563596Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 23663596Sdavidn if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 23763596Sdavidn errx(EX_NOUSER, "group `%s' does not exist", p); 23863596Sdavidn } 23963596Sdavidn cnf->default_group = newstr(grp->gr_name); 24020253Sjoerg } 24120253Sjoerg } 24220253Sjoerg if ((arg = getarg(args, 'L')) != NULL) 24320679Sdavidn cnf->default_class = pw_checkname((u_char *)arg->val, 0); 24420253Sjoerg 24520253Sjoerg if ((arg = getarg(args, 'G')) != NULL && arg->val) { 24652527Sdavidn int i = 0; 24720253Sjoerg 24820747Sdavidn for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 24944229Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 25061957Sache if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 25130259Scharnier errx(EX_NOUSER, "group `%s' does not exist", p); 25220253Sjoerg } 25320747Sdavidn if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 25420747Sdavidn cnf->groups[i++] = newstr(grp->gr_name); 25520253Sjoerg } 25620747Sdavidn while (i < cnf->numgroups) 25720253Sjoerg cnf->groups[i++] = NULL; 25820253Sjoerg } 25952527Sdavidn 26020253Sjoerg if ((arg = getarg(args, 'k')) != NULL) { 26126088Sdavidn if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode)) 26230259Scharnier errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir); 26320253Sjoerg } 26452527Sdavidn 26520253Sjoerg if ((arg = getarg(args, 's')) != NULL) 26620253Sjoerg cnf->shell_default = arg->val; 26720253Sjoerg 26863600Sdavidn if ((arg = getarg(args, 'w')) != NULL) 26963600Sdavidn cnf->default_password = boolean_val(arg->val, cnf->default_password); 27020253Sjoerg if (mode == M_ADD && getarg(args, 'D')) { 27120253Sjoerg if (getarg(args, 'n') != NULL) 27230259Scharnier errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 27320253Sjoerg if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 27420253Sjoerg if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 27520253Sjoerg cnf->min_uid = 1000; 27620253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 27720253Sjoerg cnf->max_uid = 32000; 27820253Sjoerg } 27920253Sjoerg if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 28020253Sjoerg if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 28120253Sjoerg cnf->min_gid = 1000; 28220253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 28320253Sjoerg cnf->max_gid = 32000; 28420253Sjoerg } 28520253Sjoerg 28620253Sjoerg arg = getarg(args, 'C'); 28720253Sjoerg if (write_userconfig(arg ? arg->val : NULL)) 28820267Sjoerg return EXIT_SUCCESS; 28930259Scharnier warn("config update"); 29020267Sjoerg return EX_IOERR; 29120253Sjoerg } 29252527Sdavidn 29320253Sjoerg if (mode == M_PRINT && getarg(args, 'a')) { 29420267Sjoerg int pretty = getarg(args, 'P') != NULL; 29544386Sdavidn int v7 = getarg(args, '7') != NULL; 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 317241108Sbapt && strspn(a_name->val, "0123456789") == strlen(a_name->val) 318241108Sbapt && *a_name->val) { 31920253Sjoerg (a_uid = a_name)->ch = 'u'; 32020253Sjoerg a_name = NULL; 32120253Sjoerg } 322273787Sbapt } else { 323277764Sbapt if (strspn(a_uid->val, "0123456789") != strlen(a_uid->val)) 324273787Sbapt errx(EX_USAGE, "-u expects a number"); 32520253Sjoerg } 32652527Sdavidn 32720253Sjoerg /* 32820253Sjoerg * Update, delete & print require that the user exists 32920253Sjoerg */ 33052512Sdavidn if (mode == M_UPDATE || mode == M_DELETE || 33152512Sdavidn mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) { 33252527Sdavidn 33320253Sjoerg if (a_name == NULL && pwd == NULL) /* Try harder */ 33444229Sdavidn pwd = GETPWUID(atoi(a_uid->val)); 33520253Sjoerg 33620253Sjoerg if (pwd == NULL) { 33720253Sjoerg if (mode == M_PRINT && getarg(args, 'F')) { 33820253Sjoerg fakeuser.pw_name = a_name ? a_name->val : "nouser"; 33920253Sjoerg fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 34044386Sdavidn return print_user(&fakeuser, 34144386Sdavidn getarg(args, 'P') != NULL, 34244386Sdavidn getarg(args, '7') != NULL); 34320253Sjoerg } 34420253Sjoerg if (a_name == NULL) 34530259Scharnier errx(EX_NOUSER, "no such uid `%s'", a_uid->val); 34630259Scharnier errx(EX_NOUSER, "no such user `%s'", a_name->val); 34720253Sjoerg } 34852527Sdavidn 34920253Sjoerg if (a_name == NULL) /* May be needed later */ 35020253Sjoerg a_name = addarg(args, 'n', newstr(pwd->pw_name)); 35120253Sjoerg 35220253Sjoerg /* 35352512Sdavidn * The M_LOCK and M_UNLOCK functions simply add or remove 35452512Sdavidn * a "*LOCKED*" prefix from in front of the password to 35552512Sdavidn * prevent it decoding correctly, and therefore prevents 35652512Sdavidn * access. Of course, this only prevents access via 35752512Sdavidn * password authentication (not ssh, kerberos or any 35852512Sdavidn * other method that does not use the UNIX password) but 35952512Sdavidn * that is a known limitation. 36052512Sdavidn */ 36152512Sdavidn 36252512Sdavidn if (mode == M_LOCK) { 36352512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0) 36452512Sdavidn errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 365282685Sbapt asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd); 36652512Sdavidn if (passtmp == NULL) /* disaster */ 36752512Sdavidn errx(EX_UNAVAILABLE, "out of memory"); 36852512Sdavidn pwd->pw_passwd = passtmp; 36952527Sdavidn edited = 1; 37052512Sdavidn } else if (mode == M_UNLOCK) { 37152512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0) 37252512Sdavidn errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 37352512Sdavidn pwd->pw_passwd += sizeof(locked_str)-1; 37452527Sdavidn edited = 1; 37552527Sdavidn } else if (mode == M_DELETE) { 37652527Sdavidn /* 37752527Sdavidn * Handle deletions now 37852527Sdavidn */ 37920253Sjoerg char file[MAXPATHLEN]; 38020253Sjoerg char home[MAXPATHLEN]; 38120253Sjoerg uid_t uid = pwd->pw_uid; 382263114Sdteske struct group *gr; 383263114Sdteske char grname[LOGNAMESIZE]; 38420253Sjoerg 38520253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 38630259Scharnier errx(EX_DATAERR, "cannot remove user 'root'"); 38720253Sjoerg 38844229Sdavidn if (!PWALTDIR()) { 38944229Sdavidn /* 39085145Sache * Remove opie record from /etc/opiekeys 39144229Sdavidn */ 39220747Sdavidn 39385145Sache rmopie(pwd->pw_name); 39420747Sdavidn 39544229Sdavidn /* 39644229Sdavidn * Remove crontabs 39744229Sdavidn */ 398243895Seadler snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name); 39944229Sdavidn if (access(file, F_OK) == 0) { 40044229Sdavidn sprintf(file, "crontab -u %s -r", pwd->pw_name); 40144229Sdavidn system(file); 40244229Sdavidn } 40320253Sjoerg } 40420253Sjoerg /* 40520253Sjoerg * Save these for later, since contents of pwd may be 40620253Sjoerg * invalidated by deletion 40720253Sjoerg */ 40820253Sjoerg sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 409130633Srobert strlcpy(home, pwd->pw_dir, sizeof(home)); 410263114Sdteske gr = GETGRGID(pwd->pw_gid); 411263114Sdteske if (gr != NULL) 412263114Sdteske strlcpy(grname, gr->gr_name, LOGNAMESIZE); 413263114Sdteske else 414263114Sdteske grname[0] = '\0'; 41520253Sjoerg 41652502Sdavidn rc = delpwent(pwd); 41752502Sdavidn if (rc == -1) 41852502Sdavidn err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 41952502Sdavidn else if (rc != 0) { 42056000Sdavidn warn("passwd update"); 42152502Sdavidn return EX_IOERR; 42252502Sdavidn } 42321330Sdavidn 42452502Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/') { 42552502Sdavidn rc = delnispwent(cnf->nispasswd, a_name->val); 42652502Sdavidn if (rc == -1) 42752502Sdavidn warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name); 42852502Sdavidn else if (rc != 0) 42956000Sdavidn warn("WARNING: NIS passwd update"); 43052502Sdavidn /* non-fatal */ 43152502Sdavidn } 43252502Sdavidn 433242349Sbapt grp = GETGRNAM(a_name->val); 434262865Sjulian if (grp != NULL && 435263114Sdteske (grp->gr_mem == NULL || *grp->gr_mem == NULL) && 436263114Sdteske strcmp(a_name->val, grname) == 0) 437242349Sbapt delgrent(GETGRNAM(a_name->val)); 438242349Sbapt SETGRENT(); 439242349Sbapt while ((grp = GETGRENT()) != NULL) { 440267970Smjg int i, j; 441242349Sbapt char group[MAXLOGNAME]; 442262865Sjulian if (grp->gr_mem != NULL) { 443262865Sjulian for (i = 0; grp->gr_mem[i] != NULL; i++) { 444262865Sjulian if (!strcmp(grp->gr_mem[i], a_name->val)) { 445267970Smjg for (j = i; grp->gr_mem[j] != NULL; j++) 446267970Smjg grp->gr_mem[j] = grp->gr_mem[j+1]; 447262865Sjulian strlcpy(group, grp->gr_name, MAXLOGNAME); 448262865Sjulian chggrent(group, grp); 449262865Sjulian } 450242349Sbapt } 451242349Sbapt } 452242349Sbapt } 453242349Sbapt ENDGRENT(); 45420253Sjoerg 45520253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 45620253Sjoerg 45744229Sdavidn if (!PWALTDIR()) { 45844229Sdavidn /* 45944229Sdavidn * Remove mail file 46044229Sdavidn */ 46144229Sdavidn remove(file); 46220253Sjoerg 46344229Sdavidn /* 46444229Sdavidn * Remove at jobs 46544229Sdavidn */ 46644229Sdavidn if (getpwuid(uid) == NULL) 46744229Sdavidn rmat(uid); 46820253Sjoerg 46944229Sdavidn /* 47044229Sdavidn * Remove home directory and contents 47144229Sdavidn */ 47244229Sdavidn if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 47344229Sdavidn if (stat(home, &st) != -1) { 47444229Sdavidn rm_r(home, uid); 47544229Sdavidn pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 47644229Sdavidn a_name->val, (long) uid, home, 47744229Sdavidn stat(home, &st) == -1 ? "" : "not completely "); 47844229Sdavidn } 47920253Sjoerg } 48020253Sjoerg } 48120267Sjoerg return EXIT_SUCCESS; 48220253Sjoerg } else if (mode == M_PRINT) 48344386Sdavidn return print_user(pwd, 48444386Sdavidn getarg(args, 'P') != NULL, 48544386Sdavidn getarg(args, '7') != NULL); 48620253Sjoerg 48720253Sjoerg /* 48820253Sjoerg * The rest is edit code 48920253Sjoerg */ 49020253Sjoerg if ((arg = getarg(args, 'l')) != NULL) { 49120253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 49230259Scharnier errx(EX_DATAERR, "can't rename `root' account"); 49320679Sdavidn pwd->pw_name = pw_checkname((u_char *)arg->val, 0); 49452527Sdavidn edited = 1; 49520253Sjoerg } 49652527Sdavidn 49761957Sache if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) { 49820253Sjoerg pwd->pw_uid = (uid_t) atol(arg->val); 49952527Sdavidn edited = 1; 50020253Sjoerg if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 50130259Scharnier errx(EX_DATAERR, "can't change uid of `root' account"); 50220253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 50330259Scharnier warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 50420253Sjoerg } 50520253Sjoerg 50652527Sdavidn if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */ 50752527Sdavidn gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid; 50852527Sdavidn if (newgid != pwd->pw_gid) { 50952527Sdavidn edited = 1; 51061762Sdavidn pwd->pw_gid = newgid; 51152527Sdavidn } 51252527Sdavidn } 51352527Sdavidn 51420253Sjoerg if ((arg = getarg(args, 'p')) != NULL) { 51552527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 51652527Sdavidn if (pwd->pw_change != 0) { 51752527Sdavidn pwd->pw_change = 0; 51852527Sdavidn edited = 1; 51952527Sdavidn } 52052527Sdavidn } 52120253Sjoerg else { 52220253Sjoerg time_t now = time(NULL); 52320253Sjoerg time_t expire = parse_date(now, arg->val); 52420253Sjoerg 52552527Sdavidn if (pwd->pw_change != expire) { 52652527Sdavidn pwd->pw_change = expire; 52752527Sdavidn edited = 1; 52852527Sdavidn } 52920253Sjoerg } 53020253Sjoerg } 53152527Sdavidn 53220267Sjoerg if ((arg = getarg(args, 'e')) != NULL) { 53352527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 53452527Sdavidn if (pwd->pw_expire != 0) { 53552527Sdavidn pwd->pw_expire = 0; 53652527Sdavidn edited = 1; 53752527Sdavidn } 53852527Sdavidn } 53920253Sjoerg else { 54020253Sjoerg time_t now = time(NULL); 54120253Sjoerg time_t expire = parse_date(now, arg->val); 54220253Sjoerg 54352527Sdavidn if (pwd->pw_expire != expire) { 54452527Sdavidn pwd->pw_expire = expire; 54552527Sdavidn edited = 1; 54652527Sdavidn } 54720253Sjoerg } 54820253Sjoerg } 54920253Sjoerg 55052527Sdavidn if ((arg = getarg(args, 's')) != NULL) { 55152527Sdavidn char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 55252527Sdavidn if (shell == NULL) 55352527Sdavidn shell = ""; 55452527Sdavidn if (strcmp(shell, pwd->pw_shell) != 0) { 55552527Sdavidn pwd->pw_shell = shell; 55652527Sdavidn edited = 1; 55752527Sdavidn } 55852527Sdavidn } 55920253Sjoerg 56052527Sdavidn if (getarg(args, 'L')) { 56152527Sdavidn if (cnf->default_class == NULL) 56252527Sdavidn cnf->default_class = ""; 56352527Sdavidn if (strcmp(pwd->pw_class, cnf->default_class) != 0) { 56452527Sdavidn pwd->pw_class = cnf->default_class; 56552527Sdavidn edited = 1; 56652527Sdavidn } 56752527Sdavidn } 56852527Sdavidn 56920747Sdavidn if ((arg = getarg(args, 'd')) != NULL) { 570130629Srobert if (strcmp(pwd->pw_dir, arg->val)) 571130629Srobert edited = 1; 57220747Sdavidn if (stat(pwd->pw_dir = arg->val, &st) == -1) { 57320747Sdavidn if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 57430259Scharnier warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 57520747Sdavidn } else if (!S_ISDIR(st.st_mode)) 57630259Scharnier warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 57720747Sdavidn } 57820747Sdavidn 579124382Siedowse if ((arg = getarg(args, 'w')) != NULL && 580124382Siedowse getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) { 58164918Sgreen login_cap_t *lc; 58264918Sgreen 58364918Sgreen lc = login_getpwclass(pwd); 58464918Sgreen if (lc == NULL || 585252688Sdes login_setcryptfmt(lc, "sha512", NULL) == NULL) 58664918Sgreen warn("setting crypt(3) format"); 58764918Sgreen login_close(lc); 58820267Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 58952527Sdavidn edited = 1; 59052527Sdavidn } 59120267Sjoerg 59220253Sjoerg } else { 59364918Sgreen login_cap_t *lc; 59452527Sdavidn 59552527Sdavidn /* 59652527Sdavidn * Add code 59752527Sdavidn */ 59852527Sdavidn 59920253Sjoerg if (a_name == NULL) /* Required */ 60030259Scharnier errx(EX_DATAERR, "login name required"); 60144229Sdavidn else if ((pwd = GETPWNAM(a_name->val)) != NULL) /* Exists */ 60230259Scharnier errx(EX_DATAERR, "login name `%s' already exists", a_name->val); 60320253Sjoerg 60420253Sjoerg /* 60520253Sjoerg * Now, set up defaults for a new user 60620253Sjoerg */ 60720253Sjoerg pwd = &fakeuser; 60820253Sjoerg pwd->pw_name = a_name->val; 60920253Sjoerg pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 61020253Sjoerg pwd->pw_uid = pw_uidpolicy(cnf, args); 61120253Sjoerg pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 61220253Sjoerg pwd->pw_change = pw_pwdpolicy(cnf, args); 61320253Sjoerg pwd->pw_expire = pw_exppolicy(cnf, args); 61420253Sjoerg pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 61520253Sjoerg pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 61664918Sgreen lc = login_getpwclass(pwd); 617272833Sdes if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) 61864918Sgreen warn("setting crypt(3) format"); 61964918Sgreen login_close(lc); 62064918Sgreen pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 62152527Sdavidn edited = 1; 62220253Sjoerg 62320253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 62430259Scharnier warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 62520253Sjoerg } 62620253Sjoerg 62720253Sjoerg /* 62820253Sjoerg * Shared add/edit code 62920253Sjoerg */ 63052527Sdavidn if ((arg = getarg(args, 'c')) != NULL) { 63152527Sdavidn char *gecos = pw_checkname((u_char *)arg->val, 1); 63252527Sdavidn if (strcmp(pwd->pw_gecos, gecos) != 0) { 63352527Sdavidn pwd->pw_gecos = gecos; 63452527Sdavidn edited = 1; 63552527Sdavidn } 63652527Sdavidn } 63720253Sjoerg 638124382Siedowse if ((arg = getarg(args, 'h')) != NULL || 639124382Siedowse (arg = getarg(args, 'H')) != NULL) { 64063572Sdavidn if (strcmp(arg->val, "-") == 0) { 64163572Sdavidn if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 64263572Sdavidn pwd->pw_passwd = "*"; /* No access */ 64363572Sdavidn edited = 1; 64463572Sdavidn } 64563572Sdavidn } else { 64620253Sjoerg int fd = atoi(arg->val); 647124382Siedowse int precrypt = (arg->ch == 'H'); 64820253Sjoerg int b; 64920253Sjoerg int istty = isatty(fd); 65020253Sjoerg struct termios t; 65164918Sgreen login_cap_t *lc; 65220253Sjoerg 65320253Sjoerg if (istty) { 65420253Sjoerg if (tcgetattr(fd, &t) == -1) 65520253Sjoerg istty = 0; 65620253Sjoerg else { 65720253Sjoerg struct termios n = t; 65820253Sjoerg 65920253Sjoerg /* Disable echo */ 66020253Sjoerg n.c_lflag &= ~(ECHO); 66120253Sjoerg tcsetattr(fd, TCSANOW, &n); 662124382Siedowse printf("%s%spassword for user %s:", 663124382Siedowse (mode == M_UPDATE) ? "new " : "", 664124382Siedowse precrypt ? "encrypted " : "", 665124382Siedowse pwd->pw_name); 66620253Sjoerg fflush(stdout); 66720253Sjoerg } 66820253Sjoerg } 66920253Sjoerg b = read(fd, line, sizeof(line) - 1); 67020253Sjoerg if (istty) { /* Restore state */ 67120253Sjoerg tcsetattr(fd, TCSANOW, &t); 67220253Sjoerg fputc('\n', stdout); 67320253Sjoerg fflush(stdout); 67420253Sjoerg } 67520253Sjoerg if (b < 0) { 676124382Siedowse warn("-%c file descriptor", precrypt ? 'H' : 677124382Siedowse 'h'); 67820267Sjoerg return EX_IOERR; 67920253Sjoerg } 68020253Sjoerg line[b] = '\0'; 681168045Sle if ((p = strpbrk(line, "\r\n")) != NULL) 68220253Sjoerg *p = '\0'; 68320253Sjoerg if (!*line) 68430259Scharnier errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 685124382Siedowse if (precrypt) { 686124382Siedowse if (strchr(line, ':') != NULL) 687124382Siedowse return EX_DATAERR; 688124382Siedowse pwd->pw_passwd = line; 689124382Siedowse } else { 690124382Siedowse lc = login_getpwclass(pwd); 691124382Siedowse if (lc == NULL || 692272833Sdes login_setcryptfmt(lc, "sha512", NULL) == NULL) 693124382Siedowse warn("setting crypt(3) format"); 694124382Siedowse login_close(lc); 695124382Siedowse pwd->pw_passwd = pw_pwcrypt(line); 696124382Siedowse } 69752527Sdavidn edited = 1; 69820253Sjoerg } 69920253Sjoerg } 70020267Sjoerg 70120267Sjoerg /* 70220267Sjoerg * Special case: -N only displays & exits 70320267Sjoerg */ 70420267Sjoerg if (getarg(args, 'N') != NULL) 70544386Sdavidn return print_user(pwd, 70644386Sdavidn getarg(args, 'P') != NULL, 70744386Sdavidn getarg(args, '7') != NULL); 70820267Sjoerg 70921330Sdavidn if (mode == M_ADD) { 71052527Sdavidn edited = 1; /* Always */ 71152502Sdavidn rc = addpwent(pwd); 71252502Sdavidn if (rc == -1) { 71352502Sdavidn warnx("user '%s' already exists", pwd->pw_name); 71452502Sdavidn return EX_IOERR; 71552502Sdavidn } else if (rc != 0) { 71656000Sdavidn warn("passwd file update"); 71752502Sdavidn return EX_IOERR; 71852502Sdavidn } 71952502Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/') { 72052502Sdavidn rc = addnispwent(cnf->nispasswd, pwd); 72152502Sdavidn if (rc == -1) 72252502Sdavidn warnx("User '%s' already exists in NIS passwd", pwd->pw_name); 72352502Sdavidn else 72456000Sdavidn warn("NIS passwd update"); 72552502Sdavidn /* NOTE: we treat NIS-only update errors as non-fatal */ 72652502Sdavidn } 72752512Sdavidn } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) { 72852527Sdavidn if (edited) { /* Only updated this if required */ 72952527Sdavidn rc = chgpwent(a_name->val, pwd); 73052527Sdavidn if (rc == -1) { 73152527Sdavidn warnx("user '%s' does not exist (NIS?)", pwd->pw_name); 73252527Sdavidn return EX_IOERR; 73352527Sdavidn } else if (rc != 0) { 73456000Sdavidn warn("passwd file update"); 73552527Sdavidn return EX_IOERR; 73652527Sdavidn } 73752527Sdavidn if ( cnf->nispasswd && *cnf->nispasswd=='/') { 73852527Sdavidn rc = chgnispwent(cnf->nispasswd, a_name->val, pwd); 73952527Sdavidn if (rc == -1) 74052527Sdavidn warn("User '%s' not found in NIS passwd", pwd->pw_name); 74152527Sdavidn else 74256000Sdavidn warn("NIS passwd update"); 74352527Sdavidn /* NOTE: NIS-only update errors are not fatal */ 74452527Sdavidn } 74552502Sdavidn } 74621330Sdavidn } 74721330Sdavidn 74820253Sjoerg /* 74920253Sjoerg * Ok, user is created or changed - now edit group file 75020253Sjoerg */ 75120253Sjoerg 752242349Sbapt if (mode == M_ADD || getarg(args, 'G') != NULL) { 753273779Sbapt int i, j; 754273779Sbapt /* First remove the user from all group */ 755273779Sbapt SETGRENT(); 756273779Sbapt while ((grp = GETGRENT()) != NULL) { 757273779Sbapt char group[MAXLOGNAME]; 758273779Sbapt if (grp->gr_mem == NULL) 759273779Sbapt continue; 760273779Sbapt for (i = 0; grp->gr_mem[i] != NULL; i++) { 761273779Sbapt if (strcmp(grp->gr_mem[i] , pwd->pw_name) != 0) 762273779Sbapt continue; 763273779Sbapt for (j = i; grp->gr_mem[j] != NULL ; j++) 764273779Sbapt grp->gr_mem[j] = grp->gr_mem[j+1]; 765273779Sbapt strlcpy(group, grp->gr_name, MAXLOGNAME); 766273779Sbapt chggrent(group, grp); 767273779Sbapt } 768273779Sbapt } 769273779Sbapt ENDGRENT(); 770273779Sbapt 771273779Sbapt /* now add to group where needed */ 772242349Sbapt for (i = 0; cnf->groups[i] != NULL; i++) { 773242349Sbapt grp = GETGRNAM(cnf->groups[i]); 774244737Sbapt grp = gr_add(grp, pwd->pw_name); 775244737Sbapt /* 776244737Sbapt * grp can only be NULL in 2 cases: 777244737Sbapt * - the new member is already a member 778244737Sbapt * - a problem with memory occurs 779244737Sbapt * in both cases we want to skip now. 780244737Sbapt */ 781244737Sbapt if (grp == NULL) 782242349Sbapt continue; 783242349Sbapt chggrent(cnf->groups[i], grp); 784245114Smjg free(grp); 785242349Sbapt } 786242349Sbapt } 787242349Sbapt 788242349Sbapt 78961759Sdavidn /* go get a current version of pwd */ 79061759Sdavidn pwd = GETPWNAM(a_name->val); 79161759Sdavidn if (pwd == NULL) { 79261759Sdavidn /* This will fail when we rename, so special case that */ 79361759Sdavidn if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) { 79461759Sdavidn a_name->val = arg->val; /* update new name */ 79561759Sdavidn pwd = GETPWNAM(a_name->val); /* refetch renamed rec */ 79661759Sdavidn } 79761759Sdavidn } 79861759Sdavidn if (pwd == NULL) /* can't go on without this */ 79930259Scharnier errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val); 80020253Sjoerg 80144229Sdavidn grp = GETGRGID(pwd->pw_gid); 80298744Sdwmalone pw_log(cnf, mode, W_USER, "%s(%ld):%s(%ld):%s:%s:%s", 80320253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 80420253Sjoerg grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 80520253Sjoerg pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 80620253Sjoerg 80720253Sjoerg /* 80820253Sjoerg * If adding, let's touch and chown the user's mail file. This is not 80920253Sjoerg * strictly necessary under BSD with a 0755 maildir but it also 81020253Sjoerg * doesn't hurt anything to create the empty mailfile 81120253Sjoerg */ 81220253Sjoerg if (mode == M_ADD) { 81344229Sdavidn if (!PWALTDIR()) { 81444229Sdavidn sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 81544229Sdavidn close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 81644229Sdavidn * mtime */ 81744229Sdavidn chown(line, pwd->pw_uid, pwd->pw_gid); 81820253Sjoerg } 81920253Sjoerg } 82052527Sdavidn 82120253Sjoerg /* 82282868Sdd * Let's create and populate the user's home directory. Note 82320253Sjoerg * that this also `works' for editing users if -m is used, but 82420253Sjoerg * existing files will *not* be overwritten. 82520253Sjoerg */ 82644229Sdavidn if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 827168044Sle copymkdir(pwd->pw_dir, cnf->dotdir, cnf->homemode, pwd->pw_uid, pwd->pw_gid); 82820253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 82920253Sjoerg pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 83020253Sjoerg } 83152527Sdavidn 83282868Sdd 83382868Sdd /* 83482868Sdd * Finally, send mail to the new user as well, if we are asked to 83582868Sdd */ 83682868Sdd if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 83782868Sdd FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 83882868Sdd 83982868Sdd if (pfp == NULL) 84082868Sdd warn("sendmail"); 84182868Sdd else { 84282868Sdd fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 84382868Sdd while (fgets(line, sizeof(line), fp) != NULL) { 84482868Sdd /* Do substitutions? */ 84582868Sdd fputs(line, pfp); 84682868Sdd } 84782868Sdd pclose(pfp); 84882868Sdd pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 84982868Sdd pwd->pw_name, (long) pwd->pw_uid); 85082868Sdd } 85182868Sdd fclose(fp); 85282868Sdd } 85382868Sdd 85420267Sjoerg return EXIT_SUCCESS; 85520253Sjoerg} 85620253Sjoerg 85720253Sjoerg 85820253Sjoergstatic uid_t 85920253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args) 86020253Sjoerg{ 86120253Sjoerg struct passwd *pwd; 86220253Sjoerg uid_t uid = (uid_t) - 1; 86320253Sjoerg struct carg *a_uid = getarg(args, 'u'); 86420253Sjoerg 86520253Sjoerg /* 86620253Sjoerg * Check the given uid, if any 86720253Sjoerg */ 86820253Sjoerg if (a_uid != NULL) { 86920253Sjoerg uid = (uid_t) atol(a_uid->val); 87020253Sjoerg 87144229Sdavidn if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL) 87230259Scharnier errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid); 87320253Sjoerg } else { 87420253Sjoerg struct bitmap bm; 87520253Sjoerg 87620253Sjoerg /* 87720253Sjoerg * We need to allocate the next available uid under one of 87820253Sjoerg * two policies a) Grab the first unused uid b) Grab the 87920253Sjoerg * highest possible unused uid 88020253Sjoerg */ 88120253Sjoerg if (cnf->min_uid >= cnf->max_uid) { /* Sanity 88220253Sjoerg * claus^H^H^H^Hheck */ 88320253Sjoerg cnf->min_uid = 1000; 88420253Sjoerg cnf->max_uid = 32000; 88520253Sjoerg } 88620253Sjoerg bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 88720253Sjoerg 88820253Sjoerg /* 88920253Sjoerg * Now, let's fill the bitmap from the password file 89020253Sjoerg */ 89144229Sdavidn SETPWENT(); 89244229Sdavidn while ((pwd = GETPWENT()) != NULL) 89344229Sdavidn if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 89420253Sjoerg bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 89544229Sdavidn ENDPWENT(); 89620253Sjoerg 89720253Sjoerg /* 89820253Sjoerg * Then apply the policy, with fallback to reuse if necessary 89920253Sjoerg */ 90020253Sjoerg if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 90120253Sjoerg uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 90220253Sjoerg 90320253Sjoerg /* 90420253Sjoerg * Another sanity check 90520253Sjoerg */ 90620253Sjoerg if (uid < cnf->min_uid || uid > cnf->max_uid) 90730259Scharnier errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 90820253Sjoerg bm_dealloc(&bm); 90920253Sjoerg } 91020253Sjoerg return uid; 91120253Sjoerg} 91220253Sjoerg 91320253Sjoerg 91420253Sjoergstatic uid_t 91520253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 91620253Sjoerg{ 91720253Sjoerg struct group *grp; 91820253Sjoerg gid_t gid = (uid_t) - 1; 91920253Sjoerg struct carg *a_gid = getarg(args, 'g'); 92020253Sjoerg 92120253Sjoerg /* 92220253Sjoerg * If no arg given, see if default can help out 92320253Sjoerg */ 92420253Sjoerg if (a_gid == NULL && cnf->default_group && *cnf->default_group) 92520253Sjoerg a_gid = addarg(args, 'g', cnf->default_group); 92620253Sjoerg 92720253Sjoerg /* 92820253Sjoerg * Check the given gid, if any 92920253Sjoerg */ 93044229Sdavidn SETGRENT(); 93120253Sjoerg if (a_gid != NULL) { 93244229Sdavidn if ((grp = GETGRNAM(a_gid->val)) == NULL) { 93320253Sjoerg gid = (gid_t) atol(a_gid->val); 93461957Sache if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL) 93530259Scharnier errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 93620253Sjoerg } 93720253Sjoerg gid = grp->gr_gid; 938262865Sjulian } else if ((grp = GETGRNAM(nam)) != NULL && 939262865Sjulian (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) { 94020267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 94120253Sjoerg } else { 94220253Sjoerg struct cargs grpargs; 94320253Sjoerg char tmp[32]; 94420253Sjoerg 94520253Sjoerg LIST_INIT(&grpargs); 94620253Sjoerg addarg(&grpargs, 'n', nam); 94720253Sjoerg 94820253Sjoerg /* 94920253Sjoerg * We need to auto-create a group with the user's name. We 95020253Sjoerg * can send all the appropriate output to our sister routine 95120253Sjoerg * bit first see if we can create a group with gid==uid so we 95220253Sjoerg * can keep the user and group ids in sync. We purposely do 95320253Sjoerg * NOT check the gid range if we can force the sync. If the 95420253Sjoerg * user's name dups an existing group, then the group add 95520253Sjoerg * function will happily handle that case for us and exit. 95620253Sjoerg */ 95744229Sdavidn if (GETGRGID(prefer) == NULL) { 95820253Sjoerg sprintf(tmp, "%lu", (unsigned long) prefer); 95920253Sjoerg addarg(&grpargs, 'g', tmp); 96020253Sjoerg } 96120267Sjoerg if (getarg(args, 'N')) 96220267Sjoerg { 96320267Sjoerg addarg(&grpargs, 'N', NULL); 96420267Sjoerg addarg(&grpargs, 'q', NULL); 96520267Sjoerg gid = pw_group(cnf, M_NEXT, &grpargs); 96620267Sjoerg } 96720267Sjoerg else 96820267Sjoerg { 96920267Sjoerg pw_group(cnf, M_ADD, &grpargs); 97044229Sdavidn if ((grp = GETGRNAM(nam)) != NULL) 97120267Sjoerg gid = grp->gr_gid; 97220267Sjoerg } 97370486Sben a_gid = LIST_FIRST(&grpargs); 97420253Sjoerg while (a_gid != NULL) { 97570486Sben struct carg *t = LIST_NEXT(a_gid, list); 97620253Sjoerg LIST_REMOVE(a_gid, list); 97720253Sjoerg a_gid = t; 97820253Sjoerg } 97920253Sjoerg } 98044229Sdavidn ENDGRENT(); 98120253Sjoerg return gid; 98220253Sjoerg} 98320253Sjoerg 98420253Sjoerg 98520253Sjoergstatic time_t 98620253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args) 98720253Sjoerg{ 98820253Sjoerg time_t result = 0; 98920253Sjoerg time_t now = time(NULL); 99027831Sdavidn struct carg *arg = getarg(args, 'p'); 99120253Sjoerg 99220253Sjoerg if (arg != NULL) { 99320253Sjoerg if ((result = parse_date(now, arg->val)) == now) 99430259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 99520253Sjoerg } else if (cnf->password_days > 0) 99620253Sjoerg result = now + ((long) cnf->password_days * 86400L); 99720253Sjoerg return result; 99820253Sjoerg} 99920253Sjoerg 100020253Sjoerg 100120253Sjoergstatic time_t 100220253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args) 100320253Sjoerg{ 100420253Sjoerg time_t result = 0; 100520253Sjoerg time_t now = time(NULL); 100620253Sjoerg struct carg *arg = getarg(args, 'e'); 100720253Sjoerg 100820253Sjoerg if (arg != NULL) { 100920253Sjoerg if ((result = parse_date(now, arg->val)) == now) 101030259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 101120253Sjoerg } else if (cnf->expire_days > 0) 101220253Sjoerg result = now + ((long) cnf->expire_days * 86400L); 101320253Sjoerg return result; 101420253Sjoerg} 101520253Sjoerg 101620253Sjoerg 101720253Sjoergstatic char * 101820253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 101920253Sjoerg{ 102020253Sjoerg struct carg *arg = getarg(args, 'd'); 1021282699Sbapt static char home[128]; 102220253Sjoerg 102320253Sjoerg if (arg) 1024282699Sbapt return (arg->val); 102520253Sjoerg 1026282699Sbapt if (cnf->home == NULL || *cnf->home == '\0') 1027282699Sbapt errx(EX_CONFIG, "no base home directory set"); 1028282699Sbapt snprintf(home, sizeof(home), "%s/%s", cnf->home, user); 1029282699Sbapt 1030282699Sbapt return (home); 103120253Sjoerg} 103220253Sjoerg 103320253Sjoergstatic char * 103420253Sjoergshell_path(char const * path, char *shells[], char *sh) 103520253Sjoerg{ 103620253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 103720253Sjoerg return sh; /* specified full path or forced none */ 103820253Sjoerg else { 103920253Sjoerg char *p; 104020253Sjoerg char paths[_UC_MAXLINE]; 104120253Sjoerg 104220253Sjoerg /* 104320253Sjoerg * We need to search paths 104420253Sjoerg */ 1045130633Srobert strlcpy(paths, path, sizeof(paths)); 104620253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 104720253Sjoerg int i; 104820253Sjoerg static char shellpath[256]; 104920253Sjoerg 105020253Sjoerg if (sh != NULL) { 105120253Sjoerg sprintf(shellpath, "%s/%s", p, sh); 105220253Sjoerg if (access(shellpath, X_OK) == 0) 105320253Sjoerg return shellpath; 105420253Sjoerg } else 105520253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 105620253Sjoerg sprintf(shellpath, "%s/%s", p, shells[i]); 105720253Sjoerg if (access(shellpath, X_OK) == 0) 105820253Sjoerg return shellpath; 105920253Sjoerg } 106020253Sjoerg } 106120253Sjoerg if (sh == NULL) 106230259Scharnier errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 106330259Scharnier errx(EX_CONFIG, "no default shell available or defined"); 106420253Sjoerg return NULL; 106520253Sjoerg } 106620253Sjoerg} 106720253Sjoerg 106820253Sjoerg 106920253Sjoergstatic char * 107020253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 107120253Sjoerg{ 107220253Sjoerg char *sh = newshell; 107320253Sjoerg struct carg *arg = getarg(args, 's'); 107420253Sjoerg 107520253Sjoerg if (newshell == NULL && arg != NULL) 107620253Sjoerg sh = arg->val; 107720253Sjoerg return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 107820253Sjoerg} 107920253Sjoerg 1080179365Santoine#define SALTSIZE 32 108120253Sjoerg 1082179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"; 1083179365Santoine 108420253Sjoergchar * 108520253Sjoergpw_pwcrypt(char *password) 108620253Sjoerg{ 108720253Sjoerg int i; 1088179365Santoine char salt[SALTSIZE + 1]; 1089231994Skevlo char *cryptpw; 109020253Sjoerg 109120253Sjoerg static char buf[256]; 109220253Sjoerg 109320253Sjoerg /* 109420253Sjoerg * Calculate a salt value 109520253Sjoerg */ 1096179365Santoine for (i = 0; i < SALTSIZE; i++) 1097181785Sache salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)]; 1098179365Santoine salt[SALTSIZE] = '\0'; 109920253Sjoerg 1100231994Skevlo cryptpw = crypt(password, salt); 1101231994Skevlo if (cryptpw == NULL) 1102231994Skevlo errx(EX_CONFIG, "crypt(3) failure"); 1103231994Skevlo return strcpy(buf, cryptpw); 110420253Sjoerg} 110520253Sjoerg 110620590Sdavidn 110720253Sjoergstatic char * 110820253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user) 110920253Sjoerg{ 111020253Sjoerg int i, l; 111120253Sjoerg char pwbuf[32]; 111220253Sjoerg 111320253Sjoerg switch (cnf->default_password) { 111420253Sjoerg case -1: /* Random password */ 111573563Skris l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 111620253Sjoerg for (i = 0; i < l; i++) 1117181785Sache pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)]; 111820253Sjoerg pwbuf[i] = '\0'; 111920253Sjoerg 112020253Sjoerg /* 112120253Sjoerg * We give this information back to the user 112220253Sjoerg */ 1123124382Siedowse if (getarg(args, 'h') == NULL && getarg(args, 'H') == NULL && 1124124382Siedowse getarg(args, 'N') == NULL) { 112561957Sache if (isatty(STDOUT_FILENO)) 112620712Sdavidn printf("Password for '%s' is: ", user); 112720253Sjoerg printf("%s\n", pwbuf); 112820253Sjoerg fflush(stdout); 112920253Sjoerg } 113020253Sjoerg break; 113120253Sjoerg 113220253Sjoerg case -2: /* No password at all! */ 113320253Sjoerg return ""; 113420253Sjoerg 113520253Sjoerg case 0: /* No login - default */ 113620253Sjoerg default: 113720253Sjoerg return "*"; 113820253Sjoerg 113920253Sjoerg case 1: /* user's name */ 1140130633Srobert strlcpy(pwbuf, user, sizeof(pwbuf)); 114120253Sjoerg break; 114220253Sjoerg } 114320253Sjoerg return pw_pwcrypt(pwbuf); 114420253Sjoerg} 114520253Sjoerg 114620253Sjoerg 114720253Sjoergstatic int 114844386Sdavidnprint_user(struct passwd * pwd, int pretty, int v7) 114920253Sjoerg{ 115020253Sjoerg if (!pretty) { 1151242349Sbapt char *buf; 115220253Sjoerg 1153242349Sbapt if (!v7) 1154242349Sbapt pwd->pw_passwd = (pwd->pw_passwd == NULL) ? "" : "*"; 1155242349Sbapt 1156242349Sbapt buf = v7 ? pw_make_v7(pwd) : pw_make(pwd); 1157242349Sbapt printf("%s\n", buf); 1158242349Sbapt free(buf); 115920253Sjoerg } else { 116020267Sjoerg int j; 116120253Sjoerg char *p; 116244229Sdavidn struct group *grp = GETGRGID(pwd->pw_gid); 116320253Sjoerg char uname[60] = "User &", office[60] = "[None]", 116420253Sjoerg wphone[60] = "[None]", hphone[60] = "[None]"; 116520590Sdavidn char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 116620590Sdavidn struct tm * tptr; 116720253Sjoerg 116820253Sjoerg if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 1169130633Srobert strlcpy(uname, p, sizeof(uname)); 117020253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1171130633Srobert strlcpy(office, p, sizeof(office)); 117220253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1173130633Srobert strlcpy(wphone, p, sizeof(wphone)); 117420253Sjoerg if ((p = strtok(NULL, "")) != NULL) { 1175130633Srobert strlcpy(hphone, p, 1176130633Srobert sizeof(hphone)); 117720253Sjoerg } 117820253Sjoerg } 117920253Sjoerg } 118020253Sjoerg } 118120253Sjoerg /* 118220253Sjoerg * Handle '&' in gecos field 118320253Sjoerg */ 118420253Sjoerg if ((p = strchr(uname, '&')) != NULL) { 118520253Sjoerg int l = strlen(pwd->pw_name); 118620253Sjoerg int m = strlen(p); 118720253Sjoerg 118820253Sjoerg memmove(p + l, p + 1, m); 118920253Sjoerg memmove(p, pwd->pw_name, l); 119061957Sache *p = (char) toupper((unsigned char)*p); 119120253Sjoerg } 119220590Sdavidn if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 119374569Sache strftime(acexpire, sizeof acexpire, "%c", tptr); 119461957Sache if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 119574569Sache strftime(pwexpire, sizeof pwexpire, "%c", tptr); 119622394Sdavidn printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n" 119720747Sdavidn " Full Name: %s\n" 119820747Sdavidn " Home: %-26.26s Class: %s\n" 119920747Sdavidn " Shell: %-26.26s Office: %s\n" 120020747Sdavidn "Work Phone: %-26.26s Home Phone: %s\n" 120120747Sdavidn "Acc Expire: %-26.26s Pwd Expire: %s\n", 120220253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 120320253Sjoerg grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 120420253Sjoerg uname, pwd->pw_dir, pwd->pw_class, 120520590Sdavidn pwd->pw_shell, office, wphone, hphone, 120620590Sdavidn acexpire, pwexpire); 120744229Sdavidn SETGRENT(); 120820267Sjoerg j = 0; 120944229Sdavidn while ((grp=GETGRENT()) != NULL) 121020267Sjoerg { 121120267Sjoerg int i = 0; 1212262865Sjulian if (grp->gr_mem != NULL) { 1213262865Sjulian while (grp->gr_mem[i] != NULL) 121420267Sjoerg { 1215262865Sjulian if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 1216262865Sjulian { 1217262865Sjulian printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 1218262865Sjulian break; 1219262865Sjulian } 1220262865Sjulian ++i; 122120267Sjoerg } 122220267Sjoerg } 122320267Sjoerg } 122444229Sdavidn ENDGRENT(); 122561957Sache printf("%s", j ? "\n" : ""); 122620253Sjoerg } 122720267Sjoerg return EXIT_SUCCESS; 122820253Sjoerg} 122920253Sjoerg 123020679Sdavidnchar * 123120679Sdavidnpw_checkname(u_char *name, int gecos) 123220253Sjoerg{ 1233109961Sgad char showch[8]; 1234109961Sgad u_char const *badchars, *ch, *showtype; 1235109961Sgad int reject; 123620253Sjoerg 1237109961Sgad ch = name; 1238109961Sgad reject = 0; 1239109961Sgad if (gecos) { 1240109961Sgad /* See if the name is valid as a gecos (comment) field. */ 1241109961Sgad badchars = ":!@"; 1242109961Sgad showtype = "gecos field"; 1243109961Sgad } else { 1244109961Sgad /* See if the name is valid as a userid or group. */ 1245109961Sgad badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\""; 1246109961Sgad showtype = "userid/group name"; 1247109961Sgad /* Userids and groups can not have a leading '-'. */ 1248109961Sgad if (*ch == '-') 1249109961Sgad reject = 1; 125020253Sjoerg } 1251109961Sgad if (!reject) { 1252109961Sgad while (*ch) { 1253109961Sgad if (strchr(badchars, *ch) != NULL || *ch < ' ' || 1254109961Sgad *ch == 127) { 1255109961Sgad reject = 1; 1256109961Sgad break; 1257109961Sgad } 1258109961Sgad /* 8-bit characters are only allowed in GECOS fields */ 1259109961Sgad if (!gecos && (*ch & 0x80)) { 1260109961Sgad reject = 1; 1261109961Sgad break; 1262109961Sgad } 1263109961Sgad ch++; 1264109961Sgad } 1265109961Sgad } 1266109961Sgad /* 1267109961Sgad * A `$' is allowed as the final character for userids and groups, 1268109961Sgad * mainly for the benefit of samba. 1269109961Sgad */ 1270109961Sgad if (reject && !gecos) { 1271109961Sgad if (*ch == '$' && *(ch + 1) == '\0') { 1272109961Sgad reject = 0; 1273109961Sgad ch++; 1274109961Sgad } 1275109961Sgad } 1276109961Sgad if (reject) { 1277109961Sgad snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127) 1278109961Sgad ? "`%c'" : "0x%02x", *ch); 1279228673Sdim errx(EX_DATAERR, "invalid character %s at position %td in %s", 1280109961Sgad showch, (ch - name), showtype); 1281109961Sgad } 1282109961Sgad if (!gecos && (ch - name) > LOGNAMESIZE) 1283109961Sgad errx(EX_DATAERR, "name too long `%s' (max is %d)", name, 1284109961Sgad LOGNAMESIZE); 128520679Sdavidn return (char *)name; 128620253Sjoerg} 128720253Sjoerg 128820253Sjoerg 128920253Sjoergstatic void 129020253Sjoergrmat(uid_t uid) 129120253Sjoerg{ 129220253Sjoerg DIR *d = opendir("/var/at/jobs"); 129320253Sjoerg 129420253Sjoerg if (d != NULL) { 129520253Sjoerg struct dirent *e; 129620253Sjoerg 129720253Sjoerg while ((e = readdir(d)) != NULL) { 129820253Sjoerg struct stat st; 129920253Sjoerg 130020253Sjoerg if (strncmp(e->d_name, ".lock", 5) != 0 && 130120253Sjoerg stat(e->d_name, &st) == 0 && 130220253Sjoerg !S_ISDIR(st.st_mode) && 130320253Sjoerg st.st_uid == uid) { 130420253Sjoerg char tmp[MAXPATHLEN]; 130520253Sjoerg 130620253Sjoerg sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 130720253Sjoerg system(tmp); 130820253Sjoerg } 130920253Sjoerg } 131020253Sjoerg closedir(d); 131120253Sjoerg } 131220253Sjoerg} 131320747Sdavidn 131420747Sdavidnstatic void 131585145Sachermopie(char const * name) 131620747Sdavidn{ 131785145Sache static const char etcopie[] = "/etc/opiekeys"; 131885145Sache FILE *fp = fopen(etcopie, "r+"); 131920747Sdavidn 132021052Sdavidn if (fp != NULL) { 132121052Sdavidn char tmp[1024]; 132221052Sdavidn off_t atofs = 0; 132321052Sdavidn int length = strlen(name); 132420747Sdavidn 132521052Sdavidn while (fgets(tmp, sizeof tmp, fp) != NULL) { 132621052Sdavidn if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 132721052Sdavidn if (fseek(fp, atofs, SEEK_SET) == 0) { 132821052Sdavidn fwrite("#", 1, 1, fp); /* Comment username out */ 132921052Sdavidn } 133021052Sdavidn break; 133120747Sdavidn } 133221052Sdavidn atofs = ftell(fp); 133320747Sdavidn } 133421052Sdavidn /* 133521052Sdavidn * If we got an error of any sort, don't update! 133621052Sdavidn */ 133721052Sdavidn fclose(fp); 133820747Sdavidn } 133920747Sdavidn} 134020747Sdavidn 1341