pw_user.c revision 179365
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 179365 2008-05-27 19:04:31Z antoine $"; 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> 4422394Sdavidn#include <utmp.h> 4564918Sgreen#include <login_cap.h> 4622394Sdavidn#if defined(USE_MD5RAND) 4720555Sdavidn#include <md5.h> 4822394Sdavidn#endif 4920253Sjoerg#include "pw.h" 5020253Sjoerg#include "bitmap.h" 5120253Sjoerg 5223318Sache#if (MAXLOGNAME-1) > UT_NAMESIZE 5322394Sdavidn#define LOGNAMESIZE UT_NAMESIZE 5422394Sdavidn#else 5523318Sache#define LOGNAMESIZE (MAXLOGNAME-1) 5622394Sdavidn#endif 5722394Sdavidn 5852512Sdavidnstatic char locked_str[] = "*LOCKED*"; 5924214Sache 6044386Sdavidnstatic int print_user(struct passwd * pwd, int pretty, int v7); 6120253Sjoergstatic uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args); 6220253Sjoergstatic uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer); 6320253Sjoergstatic time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 6420253Sjoergstatic time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 6520253Sjoergstatic char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 6620253Sjoergstatic char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 6720253Sjoergstatic char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 6820253Sjoergstatic char *shell_path(char const * path, char *shells[], char *sh); 6920253Sjoergstatic void rmat(uid_t uid); 7085145Sachestatic void rmopie(char const * name); 7120253Sjoerg 7220253Sjoerg/*- 7320253Sjoerg * -C config configuration file 7420253Sjoerg * -q quiet operation 7520253Sjoerg * -n name login name 7620253Sjoerg * -u uid user id 7720253Sjoerg * -c comment user name/comment 7820253Sjoerg * -d directory home directory 7920253Sjoerg * -e date account expiry date 8020253Sjoerg * -p date password expiry date 8120253Sjoerg * -g grp primary group 8220253Sjoerg * -G grp1,grp2 additional groups 8320253Sjoerg * -m [ -k dir ] create and set up home 8420253Sjoerg * -s shell name of login shell 8520253Sjoerg * -o duplicate uid ok 8620253Sjoerg * -L class user class 8720253Sjoerg * -l name new login name 8820253Sjoerg * -h fd password filehandle 89124382Siedowse * -H fd encrypted password filehandle 9020253Sjoerg * -F force print or add 9120253Sjoerg * Setting defaults: 9220253Sjoerg * -D set user defaults 9320253Sjoerg * -b dir default home root dir 9420253Sjoerg * -e period default expiry period 9520253Sjoerg * -p period default password change period 9620253Sjoerg * -g group default group 9720253Sjoerg * -G grp1,grp2.. default additional groups 9820253Sjoerg * -L class default login class 9920253Sjoerg * -k dir default home skeleton 10020253Sjoerg * -s shell default shell 10120253Sjoerg * -w method default password method 10220253Sjoerg */ 10320253Sjoerg 10420253Sjoergint 10520253Sjoergpw_user(struct userconf * cnf, int mode, struct cargs * args) 10620253Sjoerg{ 10752527Sdavidn int rc, edited = 0; 10820253Sjoerg char *p = NULL; 10952512Sdavidn char *passtmp; 11020253Sjoerg struct carg *a_name; 11120253Sjoerg struct carg *a_uid; 11220253Sjoerg struct carg *arg; 11320253Sjoerg struct passwd *pwd = NULL; 11420253Sjoerg struct group *grp; 11520253Sjoerg struct stat st; 11620747Sdavidn char line[_PASSWORD_LEN+1]; 11782868Sdd FILE *fp; 118167919Sle mode_t dmode; 119167919Sle char *dmode_c; 120167919Sle void *set = NULL; 12120253Sjoerg 12220253Sjoerg static struct passwd fakeuser = 12320253Sjoerg { 12420253Sjoerg NULL, 12520253Sjoerg "*", 12620253Sjoerg -1, 12720253Sjoerg -1, 12820253Sjoerg 0, 12920253Sjoerg "", 13020253Sjoerg "User &", 13156000Sdavidn "/nonexistent", 13220253Sjoerg "/bin/sh", 13320253Sjoerg 0 13456000Sdavidn#if defined(__FreeBSD__) 13556000Sdavidn ,0 13656000Sdavidn#endif 13720253Sjoerg }; 13820253Sjoerg 13952512Sdavidn 14020253Sjoerg /* 14120267Sjoerg * With M_NEXT, we only need to return the 14220267Sjoerg * next uid to stdout 14320267Sjoerg */ 14420267Sjoerg if (mode == M_NEXT) 14520267Sjoerg { 14620267Sjoerg uid_t next = pw_uidpolicy(cnf, args); 14720267Sjoerg if (getarg(args, 'q')) 14820267Sjoerg return next; 14920267Sjoerg printf("%ld:", (long)next); 15020267Sjoerg pw_group(cnf, mode, args); 15120267Sjoerg return EXIT_SUCCESS; 15220267Sjoerg } 15320267Sjoerg 15420267Sjoerg /* 15520253Sjoerg * We can do all of the common legwork here 15620253Sjoerg */ 15720253Sjoerg 15820253Sjoerg if ((arg = getarg(args, 'b')) != NULL) { 15920267Sjoerg cnf->home = arg->val; 16020253Sjoerg } 16121052Sdavidn 162167919Sle if ((arg = getarg(args, 'M')) != NULL) { 163167919Sle dmode_c = arg->val; 164167919Sle if ((set = setmode(dmode_c)) == NULL) 165167919Sle errx(EX_DATAERR, "invalid directory creation mode '%s'", 166167919Sle dmode_c); 167167919Sle dmode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO); 168167919Sle free(set); 169168044Sle cnf->homemode = dmode; 170168044Sle } 171167919Sle 17221052Sdavidn /* 17321052Sdavidn * If we'll need to use it or we're updating it, 17421052Sdavidn * then create the base home directory if necessary 17521052Sdavidn */ 17621052Sdavidn if (arg != NULL || getarg(args, 'm') != NULL) { 17721052Sdavidn int l = strlen(cnf->home); 17821052Sdavidn 17921052Sdavidn if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 18021052Sdavidn cnf->home[--l] = '\0'; 18121052Sdavidn 18221052Sdavidn if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 18330259Scharnier errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); 18421052Sdavidn 18521052Sdavidn if (stat(cnf->home, &st) == -1) { 18621052Sdavidn char dbuf[MAXPATHLEN]; 18721052Sdavidn 18821242Sdavidn /* 18921242Sdavidn * This is a kludge especially for Joerg :) 19021242Sdavidn * If the home directory would be created in the root partition, then 19121242Sdavidn * we really create it under /usr which is likely to have more space. 19221242Sdavidn * But we create a symlink from cnf->home -> "/usr" -> cnf->home 19321242Sdavidn */ 19421242Sdavidn if (strchr(cnf->home+1, '/') == NULL) { 19521242Sdavidn strcpy(dbuf, "/usr"); 19621242Sdavidn strncat(dbuf, cnf->home, MAXPATHLEN-5); 197168044Sle if (mkdir(dbuf, cnf->homemode) != -1 || errno == EEXIST) { 19821242Sdavidn chown(dbuf, 0, 0); 199148584Spjd /* 200148584Spjd * Skip first "/" and create symlink: 201148584Spjd * /home -> usr/home 202148584Spjd */ 203148584Spjd symlink(dbuf+1, cnf->home); 20421242Sdavidn } 20521242Sdavidn /* If this falls, fall back to old method */ 20621242Sdavidn } 207130633Srobert strlcpy(dbuf, cnf->home, sizeof(dbuf)); 208130633Srobert p = dbuf; 20921242Sdavidn if (stat(dbuf, &st) == -1) { 21021242Sdavidn while ((p = strchr(++p, '/')) != NULL) { 21121242Sdavidn *p = '\0'; 21221242Sdavidn if (stat(dbuf, &st) == -1) { 213168044Sle if (mkdir(dbuf, cnf->homemode) == -1) 21421242Sdavidn goto direrr; 21521242Sdavidn chown(dbuf, 0, 0); 21621242Sdavidn } else if (!S_ISDIR(st.st_mode)) 21730259Scharnier errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); 21821242Sdavidn *p = '/'; 21921242Sdavidn } 22021052Sdavidn } 22121242Sdavidn if (stat(dbuf, &st) == -1) { 222168044Sle if (mkdir(dbuf, cnf->homemode) == -1) { 22330259Scharnier direrr: err(EX_OSFILE, "mkdir '%s'", dbuf); 22421052Sdavidn } 22521052Sdavidn chown(dbuf, 0, 0); 22621052Sdavidn } 22721052Sdavidn } else if (!S_ISDIR(st.st_mode)) 22830259Scharnier errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); 22921052Sdavidn } 23021052Sdavidn 23120253Sjoerg if ((arg = getarg(args, 'e')) != NULL) 23220253Sjoerg cnf->expire_days = atoi(arg->val); 23320253Sjoerg 23421330Sdavidn if ((arg = getarg(args, 'y')) != NULL) 23521330Sdavidn cnf->nispasswd = arg->val; 23621330Sdavidn 23720253Sjoerg if ((arg = getarg(args, 'p')) != NULL && arg->val) 23820253Sjoerg cnf->password_days = atoi(arg->val); 23920253Sjoerg 24020253Sjoerg if ((arg = getarg(args, 'g')) != NULL) { 24163596Sdavidn if (!*(p = arg->val)) /* Handle empty group list specially */ 24263596Sdavidn cnf->default_group = ""; 24363596Sdavidn else { 24463596Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 24563596Sdavidn if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 24663596Sdavidn errx(EX_NOUSER, "group `%s' does not exist", p); 24763596Sdavidn } 24863596Sdavidn cnf->default_group = newstr(grp->gr_name); 24920253Sjoerg } 25020253Sjoerg } 25120253Sjoerg if ((arg = getarg(args, 'L')) != NULL) 25220679Sdavidn cnf->default_class = pw_checkname((u_char *)arg->val, 0); 25320253Sjoerg 25420253Sjoerg if ((arg = getarg(args, 'G')) != NULL && arg->val) { 25552527Sdavidn int i = 0; 25620253Sjoerg 25720747Sdavidn for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 25844229Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 25961957Sache if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 26030259Scharnier errx(EX_NOUSER, "group `%s' does not exist", p); 26120253Sjoerg } 26220747Sdavidn if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 26320747Sdavidn cnf->groups[i++] = newstr(grp->gr_name); 26420253Sjoerg } 26520747Sdavidn while (i < cnf->numgroups) 26620253Sjoerg cnf->groups[i++] = NULL; 26720253Sjoerg } 26852527Sdavidn 26920253Sjoerg if ((arg = getarg(args, 'k')) != NULL) { 27026088Sdavidn if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode)) 27130259Scharnier errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir); 27220253Sjoerg } 27352527Sdavidn 27420253Sjoerg if ((arg = getarg(args, 's')) != NULL) 27520253Sjoerg cnf->shell_default = arg->val; 27620253Sjoerg 27763600Sdavidn if ((arg = getarg(args, 'w')) != NULL) 27863600Sdavidn cnf->default_password = boolean_val(arg->val, cnf->default_password); 27920253Sjoerg if (mode == M_ADD && getarg(args, 'D')) { 28020253Sjoerg if (getarg(args, 'n') != NULL) 28130259Scharnier errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 28220253Sjoerg if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 28320253Sjoerg if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 28420253Sjoerg cnf->min_uid = 1000; 28520253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 28620253Sjoerg cnf->max_uid = 32000; 28720253Sjoerg } 28820253Sjoerg if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 28920253Sjoerg if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 29020253Sjoerg cnf->min_gid = 1000; 29120253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 29220253Sjoerg cnf->max_gid = 32000; 29320253Sjoerg } 29420253Sjoerg 29520253Sjoerg arg = getarg(args, 'C'); 29620253Sjoerg if (write_userconfig(arg ? arg->val : NULL)) 29720267Sjoerg return EXIT_SUCCESS; 29830259Scharnier warn("config update"); 29920267Sjoerg return EX_IOERR; 30020253Sjoerg } 30152527Sdavidn 30220253Sjoerg if (mode == M_PRINT && getarg(args, 'a')) { 30320267Sjoerg int pretty = getarg(args, 'P') != NULL; 30444386Sdavidn int v7 = getarg(args, '7') != NULL; 30520253Sjoerg 30644229Sdavidn SETPWENT(); 30744229Sdavidn while ((pwd = GETPWENT()) != NULL) 30844386Sdavidn print_user(pwd, pretty, v7); 30944229Sdavidn ENDPWENT(); 31020267Sjoerg return EXIT_SUCCESS; 31120253Sjoerg } 31252527Sdavidn 31320253Sjoerg if ((a_name = getarg(args, 'n')) != NULL) 31444229Sdavidn pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0)); 31520253Sjoerg a_uid = getarg(args, 'u'); 31620253Sjoerg 31720253Sjoerg if (a_uid == NULL) { 31820253Sjoerg if (a_name == NULL) 31930259Scharnier errx(EX_DATAERR, "user name or id required"); 32020253Sjoerg 32120253Sjoerg /* 32220253Sjoerg * Determine whether 'n' switch is name or uid - we don't 32320253Sjoerg * really don't really care which we have, but we need to 32420253Sjoerg * know. 32520253Sjoerg */ 32643780Sdes if (mode != M_ADD && pwd == NULL 32743780Sdes && strspn(a_name->val, "0123456789") == strlen(a_name->val) 32843780Sdes && atoi(a_name->val) > 0) { /* Assume uid */ 32920253Sjoerg (a_uid = a_name)->ch = 'u'; 33020253Sjoerg a_name = NULL; 33120253Sjoerg } 33220253Sjoerg } 33352527Sdavidn 33420253Sjoerg /* 33520253Sjoerg * Update, delete & print require that the user exists 33620253Sjoerg */ 33752512Sdavidn if (mode == M_UPDATE || mode == M_DELETE || 33852512Sdavidn mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) { 33952527Sdavidn 34020253Sjoerg if (a_name == NULL && pwd == NULL) /* Try harder */ 34144229Sdavidn pwd = GETPWUID(atoi(a_uid->val)); 34220253Sjoerg 34320253Sjoerg if (pwd == NULL) { 34420253Sjoerg if (mode == M_PRINT && getarg(args, 'F')) { 34520253Sjoerg fakeuser.pw_name = a_name ? a_name->val : "nouser"; 34620253Sjoerg fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 34744386Sdavidn return print_user(&fakeuser, 34844386Sdavidn getarg(args, 'P') != NULL, 34944386Sdavidn getarg(args, '7') != NULL); 35020253Sjoerg } 35120253Sjoerg if (a_name == NULL) 35230259Scharnier errx(EX_NOUSER, "no such uid `%s'", a_uid->val); 35330259Scharnier errx(EX_NOUSER, "no such user `%s'", a_name->val); 35420253Sjoerg } 35552527Sdavidn 35620253Sjoerg if (a_name == NULL) /* May be needed later */ 35720253Sjoerg a_name = addarg(args, 'n', newstr(pwd->pw_name)); 35820253Sjoerg 35920253Sjoerg /* 36052512Sdavidn * The M_LOCK and M_UNLOCK functions simply add or remove 36152512Sdavidn * a "*LOCKED*" prefix from in front of the password to 36252512Sdavidn * prevent it decoding correctly, and therefore prevents 36352512Sdavidn * access. Of course, this only prevents access via 36452512Sdavidn * password authentication (not ssh, kerberos or any 36552512Sdavidn * other method that does not use the UNIX password) but 36652512Sdavidn * that is a known limitation. 36752512Sdavidn */ 36852512Sdavidn 36952512Sdavidn if (mode == M_LOCK) { 37052512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0) 37152512Sdavidn errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 37252512Sdavidn passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str)); 37352512Sdavidn if (passtmp == NULL) /* disaster */ 37452512Sdavidn errx(EX_UNAVAILABLE, "out of memory"); 37552512Sdavidn strcpy(passtmp, locked_str); 37652512Sdavidn strcat(passtmp, pwd->pw_passwd); 37752512Sdavidn pwd->pw_passwd = passtmp; 37852527Sdavidn edited = 1; 37952512Sdavidn } else if (mode == M_UNLOCK) { 38052512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0) 38152512Sdavidn errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 38252512Sdavidn pwd->pw_passwd += sizeof(locked_str)-1; 38352527Sdavidn edited = 1; 38452527Sdavidn } else if (mode == M_DELETE) { 38552527Sdavidn /* 38652527Sdavidn * Handle deletions now 38752527Sdavidn */ 38820253Sjoerg char file[MAXPATHLEN]; 38920253Sjoerg char home[MAXPATHLEN]; 39020253Sjoerg uid_t uid = pwd->pw_uid; 39120253Sjoerg 39220253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 39330259Scharnier errx(EX_DATAERR, "cannot remove user 'root'"); 39420253Sjoerg 39544229Sdavidn if (!PWALTDIR()) { 39644229Sdavidn /* 39785145Sache * Remove opie record from /etc/opiekeys 39844229Sdavidn */ 39920747Sdavidn 40085145Sache rmopie(pwd->pw_name); 40120747Sdavidn 40244229Sdavidn /* 40344229Sdavidn * Remove crontabs 40444229Sdavidn */ 40544229Sdavidn sprintf(file, "/var/cron/tabs/%s", pwd->pw_name); 40644229Sdavidn if (access(file, F_OK) == 0) { 40744229Sdavidn sprintf(file, "crontab -u %s -r", pwd->pw_name); 40844229Sdavidn system(file); 40944229Sdavidn } 41020253Sjoerg } 41120253Sjoerg /* 41220253Sjoerg * Save these for later, since contents of pwd may be 41320253Sjoerg * invalidated by deletion 41420253Sjoerg */ 41520253Sjoerg sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 416130633Srobert strlcpy(home, pwd->pw_dir, sizeof(home)); 41720253Sjoerg 41852502Sdavidn rc = delpwent(pwd); 41952502Sdavidn if (rc == -1) 42052502Sdavidn err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 42152502Sdavidn else if (rc != 0) { 42256000Sdavidn warn("passwd update"); 42352502Sdavidn return EX_IOERR; 42452502Sdavidn } 42521330Sdavidn 42652502Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/') { 42752502Sdavidn rc = delnispwent(cnf->nispasswd, a_name->val); 42852502Sdavidn if (rc == -1) 42952502Sdavidn warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name); 43052502Sdavidn else if (rc != 0) 43156000Sdavidn warn("WARNING: NIS passwd update"); 43252502Sdavidn /* non-fatal */ 43352502Sdavidn } 43452502Sdavidn 43520253Sjoerg editgroups(a_name->val, NULL); 43620253Sjoerg 43720253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 43820253Sjoerg 43944229Sdavidn if (!PWALTDIR()) { 44044229Sdavidn /* 44144229Sdavidn * Remove mail file 44244229Sdavidn */ 44344229Sdavidn remove(file); 44420253Sjoerg 44544229Sdavidn /* 44644229Sdavidn * Remove at jobs 44744229Sdavidn */ 44844229Sdavidn if (getpwuid(uid) == NULL) 44944229Sdavidn rmat(uid); 45020253Sjoerg 45144229Sdavidn /* 45244229Sdavidn * Remove home directory and contents 45344229Sdavidn */ 45444229Sdavidn if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 45544229Sdavidn if (stat(home, &st) != -1) { 45644229Sdavidn rm_r(home, uid); 45744229Sdavidn pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 45844229Sdavidn a_name->val, (long) uid, home, 45944229Sdavidn stat(home, &st) == -1 ? "" : "not completely "); 46044229Sdavidn } 46120253Sjoerg } 46220253Sjoerg } 46320267Sjoerg return EXIT_SUCCESS; 46420253Sjoerg } else if (mode == M_PRINT) 46544386Sdavidn return print_user(pwd, 46644386Sdavidn getarg(args, 'P') != NULL, 46744386Sdavidn getarg(args, '7') != NULL); 46820253Sjoerg 46920253Sjoerg /* 47020253Sjoerg * The rest is edit code 47120253Sjoerg */ 47220253Sjoerg if ((arg = getarg(args, 'l')) != NULL) { 47320253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 47430259Scharnier errx(EX_DATAERR, "can't rename `root' account"); 47520679Sdavidn pwd->pw_name = pw_checkname((u_char *)arg->val, 0); 47652527Sdavidn edited = 1; 47720253Sjoerg } 47852527Sdavidn 47961957Sache if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) { 48020253Sjoerg pwd->pw_uid = (uid_t) atol(arg->val); 48152527Sdavidn edited = 1; 48220253Sjoerg if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 48330259Scharnier errx(EX_DATAERR, "can't change uid of `root' account"); 48420253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 48530259Scharnier warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 48620253Sjoerg } 48720253Sjoerg 48852527Sdavidn if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */ 48952527Sdavidn gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid; 49052527Sdavidn if (newgid != pwd->pw_gid) { 49152527Sdavidn edited = 1; 49261762Sdavidn pwd->pw_gid = newgid; 49352527Sdavidn } 49452527Sdavidn } 49552527Sdavidn 49620253Sjoerg if ((arg = getarg(args, 'p')) != NULL) { 49752527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 49852527Sdavidn if (pwd->pw_change != 0) { 49952527Sdavidn pwd->pw_change = 0; 50052527Sdavidn edited = 1; 50152527Sdavidn } 50252527Sdavidn } 50320253Sjoerg else { 50420253Sjoerg time_t now = time(NULL); 50520253Sjoerg time_t expire = parse_date(now, arg->val); 50620253Sjoerg 50720253Sjoerg if (now == expire) 50830259Scharnier errx(EX_DATAERR, "invalid password change date `%s'", arg->val); 50952527Sdavidn if (pwd->pw_change != expire) { 51052527Sdavidn pwd->pw_change = expire; 51152527Sdavidn edited = 1; 51252527Sdavidn } 51320253Sjoerg } 51420253Sjoerg } 51552527Sdavidn 51620267Sjoerg if ((arg = getarg(args, 'e')) != NULL) { 51752527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 51852527Sdavidn if (pwd->pw_expire != 0) { 51952527Sdavidn pwd->pw_expire = 0; 52052527Sdavidn edited = 1; 52152527Sdavidn } 52252527Sdavidn } 52320253Sjoerg else { 52420253Sjoerg time_t now = time(NULL); 52520253Sjoerg time_t expire = parse_date(now, arg->val); 52620253Sjoerg 52720253Sjoerg if (now == expire) 52830259Scharnier errx(EX_DATAERR, "invalid account expiry date `%s'", arg->val); 52952527Sdavidn if (pwd->pw_expire != expire) { 53052527Sdavidn pwd->pw_expire = expire; 53152527Sdavidn edited = 1; 53252527Sdavidn } 53320253Sjoerg } 53420253Sjoerg } 53520253Sjoerg 53652527Sdavidn if ((arg = getarg(args, 's')) != NULL) { 53752527Sdavidn char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 53852527Sdavidn if (shell == NULL) 53952527Sdavidn shell = ""; 54052527Sdavidn if (strcmp(shell, pwd->pw_shell) != 0) { 54152527Sdavidn pwd->pw_shell = shell; 54252527Sdavidn edited = 1; 54352527Sdavidn } 54452527Sdavidn } 54520253Sjoerg 54652527Sdavidn if (getarg(args, 'L')) { 54752527Sdavidn if (cnf->default_class == NULL) 54852527Sdavidn cnf->default_class = ""; 54952527Sdavidn if (strcmp(pwd->pw_class, cnf->default_class) != 0) { 55052527Sdavidn pwd->pw_class = cnf->default_class; 55152527Sdavidn edited = 1; 55252527Sdavidn } 55352527Sdavidn } 55452527Sdavidn 55520747Sdavidn if ((arg = getarg(args, 'd')) != NULL) { 556130629Srobert if (strcmp(pwd->pw_dir, arg->val)) 557130629Srobert edited = 1; 55820747Sdavidn if (stat(pwd->pw_dir = arg->val, &st) == -1) { 55920747Sdavidn if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 56030259Scharnier warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 56120747Sdavidn } else if (!S_ISDIR(st.st_mode)) 56230259Scharnier warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 56320747Sdavidn } 56420747Sdavidn 565124382Siedowse if ((arg = getarg(args, 'w')) != NULL && 566124382Siedowse getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) { 56764918Sgreen login_cap_t *lc; 56864918Sgreen 56964918Sgreen lc = login_getpwclass(pwd); 57064918Sgreen if (lc == NULL || 57164918Sgreen login_setcryptfmt(lc, "md5", NULL) == NULL) 57264918Sgreen warn("setting crypt(3) format"); 57364918Sgreen login_close(lc); 57420267Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 57552527Sdavidn edited = 1; 57652527Sdavidn } 57720267Sjoerg 57820253Sjoerg } else { 57964918Sgreen login_cap_t *lc; 58052527Sdavidn 58152527Sdavidn /* 58252527Sdavidn * Add code 58352527Sdavidn */ 58452527Sdavidn 58520253Sjoerg if (a_name == NULL) /* Required */ 58630259Scharnier errx(EX_DATAERR, "login name required"); 58744229Sdavidn else if ((pwd = GETPWNAM(a_name->val)) != NULL) /* Exists */ 58830259Scharnier errx(EX_DATAERR, "login name `%s' already exists", a_name->val); 58920253Sjoerg 59020253Sjoerg /* 59120253Sjoerg * Now, set up defaults for a new user 59220253Sjoerg */ 59320253Sjoerg pwd = &fakeuser; 59420253Sjoerg pwd->pw_name = a_name->val; 59520253Sjoerg pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 59620253Sjoerg pwd->pw_uid = pw_uidpolicy(cnf, args); 59720253Sjoerg pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 59820253Sjoerg pwd->pw_change = pw_pwdpolicy(cnf, args); 59920253Sjoerg pwd->pw_expire = pw_exppolicy(cnf, args); 60020253Sjoerg pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 60120253Sjoerg pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 60264918Sgreen lc = login_getpwclass(pwd); 60364918Sgreen if (lc == NULL || login_setcryptfmt(lc, "md5", NULL) == NULL) 60464918Sgreen warn("setting crypt(3) format"); 60564918Sgreen login_close(lc); 60664918Sgreen pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 60752527Sdavidn edited = 1; 60820253Sjoerg 60920253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 61030259Scharnier warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 61120253Sjoerg } 61220253Sjoerg 61320253Sjoerg /* 61420253Sjoerg * Shared add/edit code 61520253Sjoerg */ 61652527Sdavidn if ((arg = getarg(args, 'c')) != NULL) { 61752527Sdavidn char *gecos = pw_checkname((u_char *)arg->val, 1); 61852527Sdavidn if (strcmp(pwd->pw_gecos, gecos) != 0) { 61952527Sdavidn pwd->pw_gecos = gecos; 62052527Sdavidn edited = 1; 62152527Sdavidn } 62252527Sdavidn } 62320253Sjoerg 624124382Siedowse if ((arg = getarg(args, 'h')) != NULL || 625124382Siedowse (arg = getarg(args, 'H')) != NULL) { 62663572Sdavidn if (strcmp(arg->val, "-") == 0) { 62763572Sdavidn if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 62863572Sdavidn pwd->pw_passwd = "*"; /* No access */ 62963572Sdavidn edited = 1; 63063572Sdavidn } 63163572Sdavidn } else { 63220253Sjoerg int fd = atoi(arg->val); 633124382Siedowse int precrypt = (arg->ch == 'H'); 63420253Sjoerg int b; 63520253Sjoerg int istty = isatty(fd); 63620253Sjoerg struct termios t; 63764918Sgreen login_cap_t *lc; 63820253Sjoerg 63920253Sjoerg if (istty) { 64020253Sjoerg if (tcgetattr(fd, &t) == -1) 64120253Sjoerg istty = 0; 64220253Sjoerg else { 64320253Sjoerg struct termios n = t; 64420253Sjoerg 64520253Sjoerg /* Disable echo */ 64620253Sjoerg n.c_lflag &= ~(ECHO); 64720253Sjoerg tcsetattr(fd, TCSANOW, &n); 648124382Siedowse printf("%s%spassword for user %s:", 649124382Siedowse (mode == M_UPDATE) ? "new " : "", 650124382Siedowse precrypt ? "encrypted " : "", 651124382Siedowse pwd->pw_name); 65220253Sjoerg fflush(stdout); 65320253Sjoerg } 65420253Sjoerg } 65520253Sjoerg b = read(fd, line, sizeof(line) - 1); 65620253Sjoerg if (istty) { /* Restore state */ 65720253Sjoerg tcsetattr(fd, TCSANOW, &t); 65820253Sjoerg fputc('\n', stdout); 65920253Sjoerg fflush(stdout); 66020253Sjoerg } 66120253Sjoerg if (b < 0) { 662124382Siedowse warn("-%c file descriptor", precrypt ? 'H' : 663124382Siedowse 'h'); 66420267Sjoerg return EX_IOERR; 66520253Sjoerg } 66620253Sjoerg line[b] = '\0'; 667168045Sle if ((p = strpbrk(line, "\r\n")) != NULL) 66820253Sjoerg *p = '\0'; 66920253Sjoerg if (!*line) 67030259Scharnier errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 671124382Siedowse if (precrypt) { 672124382Siedowse if (strchr(line, ':') != NULL) 673124382Siedowse return EX_DATAERR; 674124382Siedowse pwd->pw_passwd = line; 675124382Siedowse } else { 676124382Siedowse lc = login_getpwclass(pwd); 677124382Siedowse if (lc == NULL || 678124382Siedowse login_setcryptfmt(lc, "md5", NULL) == NULL) 679124382Siedowse warn("setting crypt(3) format"); 680124382Siedowse login_close(lc); 681124382Siedowse pwd->pw_passwd = pw_pwcrypt(line); 682124382Siedowse } 68352527Sdavidn edited = 1; 68420253Sjoerg } 68520253Sjoerg } 68620267Sjoerg 68720267Sjoerg /* 68820267Sjoerg * Special case: -N only displays & exits 68920267Sjoerg */ 69020267Sjoerg if (getarg(args, 'N') != NULL) 69144386Sdavidn return print_user(pwd, 69244386Sdavidn getarg(args, 'P') != NULL, 69344386Sdavidn getarg(args, '7') != NULL); 69420267Sjoerg 69521330Sdavidn if (mode == M_ADD) { 69652527Sdavidn edited = 1; /* Always */ 69752502Sdavidn rc = addpwent(pwd); 69852502Sdavidn if (rc == -1) { 69952502Sdavidn warnx("user '%s' already exists", pwd->pw_name); 70052502Sdavidn return EX_IOERR; 70152502Sdavidn } else if (rc != 0) { 70256000Sdavidn warn("passwd file update"); 70352502Sdavidn return EX_IOERR; 70452502Sdavidn } 70552502Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/') { 70652502Sdavidn rc = addnispwent(cnf->nispasswd, pwd); 70752502Sdavidn if (rc == -1) 70852502Sdavidn warnx("User '%s' already exists in NIS passwd", pwd->pw_name); 70952502Sdavidn else 71056000Sdavidn warn("NIS passwd update"); 71152502Sdavidn /* NOTE: we treat NIS-only update errors as non-fatal */ 71252502Sdavidn } 71352512Sdavidn } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) { 71452527Sdavidn if (edited) { /* Only updated this if required */ 71552527Sdavidn rc = chgpwent(a_name->val, pwd); 71652527Sdavidn if (rc == -1) { 71752527Sdavidn warnx("user '%s' does not exist (NIS?)", pwd->pw_name); 71852527Sdavidn return EX_IOERR; 71952527Sdavidn } else if (rc != 0) { 72056000Sdavidn warn("passwd file update"); 72152527Sdavidn return EX_IOERR; 72252527Sdavidn } 72352527Sdavidn if ( cnf->nispasswd && *cnf->nispasswd=='/') { 72452527Sdavidn rc = chgnispwent(cnf->nispasswd, a_name->val, pwd); 72552527Sdavidn if (rc == -1) 72652527Sdavidn warn("User '%s' not found in NIS passwd", pwd->pw_name); 72752527Sdavidn else 72856000Sdavidn warn("NIS passwd update"); 72952527Sdavidn /* NOTE: NIS-only update errors are not fatal */ 73052527Sdavidn } 73152502Sdavidn } 73221330Sdavidn } 73321330Sdavidn 73420253Sjoerg /* 73520253Sjoerg * Ok, user is created or changed - now edit group file 73620253Sjoerg */ 73720253Sjoerg 73820253Sjoerg if (mode == M_ADD || getarg(args, 'G') != NULL) 73920253Sjoerg editgroups(pwd->pw_name, cnf->groups); 74020253Sjoerg 74161759Sdavidn /* go get a current version of pwd */ 74261759Sdavidn pwd = GETPWNAM(a_name->val); 74361759Sdavidn if (pwd == NULL) { 74461759Sdavidn /* This will fail when we rename, so special case that */ 74561759Sdavidn if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) { 74661759Sdavidn a_name->val = arg->val; /* update new name */ 74761759Sdavidn pwd = GETPWNAM(a_name->val); /* refetch renamed rec */ 74861759Sdavidn } 74961759Sdavidn } 75061759Sdavidn if (pwd == NULL) /* can't go on without this */ 75130259Scharnier errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val); 75220253Sjoerg 75344229Sdavidn grp = GETGRGID(pwd->pw_gid); 75498744Sdwmalone pw_log(cnf, mode, W_USER, "%s(%ld):%s(%ld):%s:%s:%s", 75520253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 75620253Sjoerg grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 75720253Sjoerg pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 75820253Sjoerg 75920253Sjoerg /* 76020253Sjoerg * If adding, let's touch and chown the user's mail file. This is not 76120253Sjoerg * strictly necessary under BSD with a 0755 maildir but it also 76220253Sjoerg * doesn't hurt anything to create the empty mailfile 76320253Sjoerg */ 76420253Sjoerg if (mode == M_ADD) { 76544229Sdavidn if (!PWALTDIR()) { 76644229Sdavidn sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 76744229Sdavidn close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 76844229Sdavidn * mtime */ 76944229Sdavidn chown(line, pwd->pw_uid, pwd->pw_gid); 77020253Sjoerg } 77120253Sjoerg } 77252527Sdavidn 77320253Sjoerg /* 77482868Sdd * Let's create and populate the user's home directory. Note 77520253Sjoerg * that this also `works' for editing users if -m is used, but 77620253Sjoerg * existing files will *not* be overwritten. 77720253Sjoerg */ 77844229Sdavidn if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 779168044Sle copymkdir(pwd->pw_dir, cnf->dotdir, cnf->homemode, pwd->pw_uid, pwd->pw_gid); 78020253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 78120253Sjoerg pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 78220253Sjoerg } 78352527Sdavidn 78482868Sdd 78582868Sdd /* 78682868Sdd * Finally, send mail to the new user as well, if we are asked to 78782868Sdd */ 78882868Sdd if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 78982868Sdd FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 79082868Sdd 79182868Sdd if (pfp == NULL) 79282868Sdd warn("sendmail"); 79382868Sdd else { 79482868Sdd fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 79582868Sdd while (fgets(line, sizeof(line), fp) != NULL) { 79682868Sdd /* Do substitutions? */ 79782868Sdd fputs(line, pfp); 79882868Sdd } 79982868Sdd pclose(pfp); 80082868Sdd pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 80182868Sdd pwd->pw_name, (long) pwd->pw_uid); 80282868Sdd } 80382868Sdd fclose(fp); 80482868Sdd } 80582868Sdd 80620267Sjoerg return EXIT_SUCCESS; 80720253Sjoerg} 80820253Sjoerg 80920253Sjoerg 81020253Sjoergstatic uid_t 81120253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args) 81220253Sjoerg{ 81320253Sjoerg struct passwd *pwd; 81420253Sjoerg uid_t uid = (uid_t) - 1; 81520253Sjoerg struct carg *a_uid = getarg(args, 'u'); 81620253Sjoerg 81720253Sjoerg /* 81820253Sjoerg * Check the given uid, if any 81920253Sjoerg */ 82020253Sjoerg if (a_uid != NULL) { 82120253Sjoerg uid = (uid_t) atol(a_uid->val); 82220253Sjoerg 82344229Sdavidn if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL) 82430259Scharnier errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid); 82520253Sjoerg } else { 82620253Sjoerg struct bitmap bm; 82720253Sjoerg 82820253Sjoerg /* 82920253Sjoerg * We need to allocate the next available uid under one of 83020253Sjoerg * two policies a) Grab the first unused uid b) Grab the 83120253Sjoerg * highest possible unused uid 83220253Sjoerg */ 83320253Sjoerg if (cnf->min_uid >= cnf->max_uid) { /* Sanity 83420253Sjoerg * claus^H^H^H^Hheck */ 83520253Sjoerg cnf->min_uid = 1000; 83620253Sjoerg cnf->max_uid = 32000; 83720253Sjoerg } 83820253Sjoerg bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 83920253Sjoerg 84020253Sjoerg /* 84120253Sjoerg * Now, let's fill the bitmap from the password file 84220253Sjoerg */ 84344229Sdavidn SETPWENT(); 84444229Sdavidn while ((pwd = GETPWENT()) != NULL) 84544229Sdavidn if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 84620253Sjoerg bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 84744229Sdavidn ENDPWENT(); 84820253Sjoerg 84920253Sjoerg /* 85020253Sjoerg * Then apply the policy, with fallback to reuse if necessary 85120253Sjoerg */ 85220253Sjoerg if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 85320253Sjoerg uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 85420253Sjoerg 85520253Sjoerg /* 85620253Sjoerg * Another sanity check 85720253Sjoerg */ 85820253Sjoerg if (uid < cnf->min_uid || uid > cnf->max_uid) 85930259Scharnier errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 86020253Sjoerg bm_dealloc(&bm); 86120253Sjoerg } 86220253Sjoerg return uid; 86320253Sjoerg} 86420253Sjoerg 86520253Sjoerg 86620253Sjoergstatic uid_t 86720253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 86820253Sjoerg{ 86920253Sjoerg struct group *grp; 87020253Sjoerg gid_t gid = (uid_t) - 1; 87120253Sjoerg struct carg *a_gid = getarg(args, 'g'); 87220253Sjoerg 87320253Sjoerg /* 87420253Sjoerg * If no arg given, see if default can help out 87520253Sjoerg */ 87620253Sjoerg if (a_gid == NULL && cnf->default_group && *cnf->default_group) 87720253Sjoerg a_gid = addarg(args, 'g', cnf->default_group); 87820253Sjoerg 87920253Sjoerg /* 88020253Sjoerg * Check the given gid, if any 88120253Sjoerg */ 88244229Sdavidn SETGRENT(); 88320253Sjoerg if (a_gid != NULL) { 88444229Sdavidn if ((grp = GETGRNAM(a_gid->val)) == NULL) { 88520253Sjoerg gid = (gid_t) atol(a_gid->val); 88661957Sache if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL) 88730259Scharnier errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 88820253Sjoerg } 88920253Sjoerg gid = grp->gr_gid; 89044229Sdavidn } else if ((grp = GETGRNAM(nam)) != NULL && grp->gr_mem[0] == NULL) { 89120267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 89220253Sjoerg } else { 89320253Sjoerg struct cargs grpargs; 89420253Sjoerg char tmp[32]; 89520253Sjoerg 89620253Sjoerg LIST_INIT(&grpargs); 89720253Sjoerg addarg(&grpargs, 'n', nam); 89820253Sjoerg 89920253Sjoerg /* 90020253Sjoerg * We need to auto-create a group with the user's name. We 90120253Sjoerg * can send all the appropriate output to our sister routine 90220253Sjoerg * bit first see if we can create a group with gid==uid so we 90320253Sjoerg * can keep the user and group ids in sync. We purposely do 90420253Sjoerg * NOT check the gid range if we can force the sync. If the 90520253Sjoerg * user's name dups an existing group, then the group add 90620253Sjoerg * function will happily handle that case for us and exit. 90720253Sjoerg */ 90844229Sdavidn if (GETGRGID(prefer) == NULL) { 90920253Sjoerg sprintf(tmp, "%lu", (unsigned long) prefer); 91020253Sjoerg addarg(&grpargs, 'g', tmp); 91120253Sjoerg } 91220267Sjoerg if (getarg(args, 'N')) 91320267Sjoerg { 91420267Sjoerg addarg(&grpargs, 'N', NULL); 91520267Sjoerg addarg(&grpargs, 'q', NULL); 91620267Sjoerg gid = pw_group(cnf, M_NEXT, &grpargs); 91720267Sjoerg } 91820267Sjoerg else 91920267Sjoerg { 92020267Sjoerg pw_group(cnf, M_ADD, &grpargs); 92144229Sdavidn if ((grp = GETGRNAM(nam)) != NULL) 92220267Sjoerg gid = grp->gr_gid; 92320267Sjoerg } 92470486Sben a_gid = LIST_FIRST(&grpargs); 92520253Sjoerg while (a_gid != NULL) { 92670486Sben struct carg *t = LIST_NEXT(a_gid, list); 92720253Sjoerg LIST_REMOVE(a_gid, list); 92820253Sjoerg a_gid = t; 92920253Sjoerg } 93020253Sjoerg } 93144229Sdavidn ENDGRENT(); 93220253Sjoerg return gid; 93320253Sjoerg} 93420253Sjoerg 93520253Sjoerg 93620253Sjoergstatic time_t 93720253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args) 93820253Sjoerg{ 93920253Sjoerg time_t result = 0; 94020253Sjoerg time_t now = time(NULL); 94127831Sdavidn struct carg *arg = getarg(args, 'p'); 94220253Sjoerg 94320253Sjoerg if (arg != NULL) { 94420253Sjoerg if ((result = parse_date(now, arg->val)) == now) 94530259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 94620253Sjoerg } else if (cnf->password_days > 0) 94720253Sjoerg result = now + ((long) cnf->password_days * 86400L); 94820253Sjoerg return result; 94920253Sjoerg} 95020253Sjoerg 95120253Sjoerg 95220253Sjoergstatic time_t 95320253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args) 95420253Sjoerg{ 95520253Sjoerg time_t result = 0; 95620253Sjoerg time_t now = time(NULL); 95720253Sjoerg struct carg *arg = getarg(args, 'e'); 95820253Sjoerg 95920253Sjoerg if (arg != NULL) { 96020253Sjoerg if ((result = parse_date(now, arg->val)) == now) 96130259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 96220253Sjoerg } else if (cnf->expire_days > 0) 96320253Sjoerg result = now + ((long) cnf->expire_days * 86400L); 96420253Sjoerg return result; 96520253Sjoerg} 96620253Sjoerg 96720253Sjoerg 96820253Sjoergstatic char * 96920253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 97020253Sjoerg{ 97120253Sjoerg struct carg *arg = getarg(args, 'd'); 97220253Sjoerg 97320253Sjoerg if (arg) 97420253Sjoerg return arg->val; 97520253Sjoerg else { 97620253Sjoerg static char home[128]; 97720253Sjoerg 97820253Sjoerg if (cnf->home == NULL || *cnf->home == '\0') 97930259Scharnier errx(EX_CONFIG, "no base home directory set"); 98020253Sjoerg sprintf(home, "%s/%s", cnf->home, user); 98120253Sjoerg return home; 98220253Sjoerg } 98320253Sjoerg} 98420253Sjoerg 98520253Sjoergstatic char * 98620253Sjoergshell_path(char const * path, char *shells[], char *sh) 98720253Sjoerg{ 98820253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 98920253Sjoerg return sh; /* specified full path or forced none */ 99020253Sjoerg else { 99120253Sjoerg char *p; 99220253Sjoerg char paths[_UC_MAXLINE]; 99320253Sjoerg 99420253Sjoerg /* 99520253Sjoerg * We need to search paths 99620253Sjoerg */ 997130633Srobert strlcpy(paths, path, sizeof(paths)); 99820253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 99920253Sjoerg int i; 100020253Sjoerg static char shellpath[256]; 100120253Sjoerg 100220253Sjoerg if (sh != NULL) { 100320253Sjoerg sprintf(shellpath, "%s/%s", p, sh); 100420253Sjoerg if (access(shellpath, X_OK) == 0) 100520253Sjoerg return shellpath; 100620253Sjoerg } else 100720253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 100820253Sjoerg sprintf(shellpath, "%s/%s", p, shells[i]); 100920253Sjoerg if (access(shellpath, X_OK) == 0) 101020253Sjoerg return shellpath; 101120253Sjoerg } 101220253Sjoerg } 101320253Sjoerg if (sh == NULL) 101430259Scharnier errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 101530259Scharnier errx(EX_CONFIG, "no default shell available or defined"); 101620253Sjoerg return NULL; 101720253Sjoerg } 101820253Sjoerg} 101920253Sjoerg 102020253Sjoerg 102120253Sjoergstatic char * 102220253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 102320253Sjoerg{ 102420253Sjoerg char *sh = newshell; 102520253Sjoerg struct carg *arg = getarg(args, 's'); 102620253Sjoerg 102720253Sjoerg if (newshell == NULL && arg != NULL) 102820253Sjoerg sh = arg->val; 102920253Sjoerg return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 103020253Sjoerg} 103120253Sjoerg 1032179365Santoine#define SALTSIZE 32 103320253Sjoerg 1034179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"; 1035179365Santoine 103620253Sjoergchar * 103720253Sjoergpw_pwcrypt(char *password) 103820253Sjoerg{ 103920253Sjoerg int i; 1040179365Santoine char salt[SALTSIZE + 1]; 104120253Sjoerg 104220253Sjoerg static char buf[256]; 104320253Sjoerg 104420253Sjoerg /* 104520253Sjoerg * Calculate a salt value 104620253Sjoerg */ 1047179365Santoine for (i = 0; i < SALTSIZE; i++) 1048179365Santoine salt[i] = chars[arc4random() % (sizeof(chars) - 1)]; 1049179365Santoine salt[SALTSIZE] = '\0'; 105020253Sjoerg 105120253Sjoerg return strcpy(buf, crypt(password, salt)); 105220253Sjoerg} 105320253Sjoerg 105420590Sdavidn#if defined(USE_MD5RAND) 105520555Sdavidnu_char * 105620590Sdavidnpw_getrand(u_char *buf, int len) /* cryptographically secure rng */ 105720555Sdavidn{ 105820590Sdavidn int i; 105920590Sdavidn for (i=0;i<len;i+=16) { 106020590Sdavidn u_char ubuf[16]; 106120590Sdavidn 106220590Sdavidn MD5_CTX md5_ctx; 106320590Sdavidn struct timeval tv, tvo; 106420590Sdavidn struct rusage ru; 106520590Sdavidn int n=0; 106620590Sdavidn int t; 106720590Sdavidn 106820590Sdavidn MD5Init (&md5_ctx); 106920590Sdavidn t=getpid(); 107020590Sdavidn MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 107120590Sdavidn t=getppid(); 107220590Sdavidn MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 107320590Sdavidn gettimeofday (&tvo, NULL); 107420590Sdavidn do { 107520590Sdavidn getrusage (RUSAGE_SELF, &ru); 107620590Sdavidn MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru); 107720590Sdavidn gettimeofday (&tv, NULL); 107820590Sdavidn MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv); 107920590Sdavidn } while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000); 108020590Sdavidn MD5Final (ubuf, &md5_ctx); 108182047Sjoerg memcpy(buf+i, ubuf, MIN(16, len-i)); 108220590Sdavidn } 108320590Sdavidn return buf; 108420555Sdavidn} 108520253Sjoerg 108620590Sdavidn#else /* Portable version */ 108720590Sdavidn 108820590Sdavidnstatic u_char * 108920590Sdavidnpw_getrand(u_char *buf, int len) 109020590Sdavidn{ 109120590Sdavidn int i; 109220590Sdavidn 109320590Sdavidn for (i = 0; i < len; i++) { 1094110667Sache unsigned long val = arc4random(); 109520590Sdavidn /* Use all bits in the random value */ 109620590Sdavidn buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val); 109720590Sdavidn } 109820590Sdavidn return buf; 109920590Sdavidn} 110020590Sdavidn 110120590Sdavidn#endif 110220590Sdavidn 110320253Sjoergstatic char * 110420253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user) 110520253Sjoerg{ 110620253Sjoerg int i, l; 110720253Sjoerg char pwbuf[32]; 110820555Sdavidn u_char rndbuf[sizeof pwbuf]; 110920253Sjoerg 111020253Sjoerg switch (cnf->default_password) { 111120253Sjoerg case -1: /* Random password */ 111273563Skris l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 111320555Sdavidn pw_getrand(rndbuf, l); 111420253Sjoerg for (i = 0; i < l; i++) 111542245Sbillf pwbuf[i] = chars[rndbuf[i] % (sizeof(chars)-1)]; 111620253Sjoerg pwbuf[i] = '\0'; 111720253Sjoerg 111820253Sjoerg /* 111920253Sjoerg * We give this information back to the user 112020253Sjoerg */ 1121124382Siedowse if (getarg(args, 'h') == NULL && getarg(args, 'H') == NULL && 1122124382Siedowse getarg(args, 'N') == NULL) { 112361957Sache if (isatty(STDOUT_FILENO)) 112420712Sdavidn printf("Password for '%s' is: ", user); 112520253Sjoerg printf("%s\n", pwbuf); 112620253Sjoerg fflush(stdout); 112720253Sjoerg } 112820253Sjoerg break; 112920253Sjoerg 113020253Sjoerg case -2: /* No password at all! */ 113120253Sjoerg return ""; 113220253Sjoerg 113320253Sjoerg case 0: /* No login - default */ 113420253Sjoerg default: 113520253Sjoerg return "*"; 113620253Sjoerg 113720253Sjoerg case 1: /* user's name */ 1138130633Srobert strlcpy(pwbuf, user, sizeof(pwbuf)); 113920253Sjoerg break; 114020253Sjoerg } 114120253Sjoerg return pw_pwcrypt(pwbuf); 114220253Sjoerg} 114320253Sjoerg 114420253Sjoerg 114520253Sjoergstatic int 114644386Sdavidnprint_user(struct passwd * pwd, int pretty, int v7) 114720253Sjoerg{ 114820253Sjoerg if (!pretty) { 114920253Sjoerg char buf[_UC_MAXLINE]; 115020253Sjoerg 115144386Sdavidn fmtpwentry(buf, pwd, v7 ? PWF_PASSWD : PWF_STANDARD); 115220253Sjoerg fputs(buf, stdout); 115320253Sjoerg } else { 115420267Sjoerg int j; 115520253Sjoerg char *p; 115644229Sdavidn struct group *grp = GETGRGID(pwd->pw_gid); 115720253Sjoerg char uname[60] = "User &", office[60] = "[None]", 115820253Sjoerg wphone[60] = "[None]", hphone[60] = "[None]"; 115920590Sdavidn char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 116020590Sdavidn struct tm * tptr; 116120253Sjoerg 116220253Sjoerg if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 1163130633Srobert strlcpy(uname, p, sizeof(uname)); 116420253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1165130633Srobert strlcpy(office, p, sizeof(office)); 116620253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1167130633Srobert strlcpy(wphone, p, sizeof(wphone)); 116820253Sjoerg if ((p = strtok(NULL, "")) != NULL) { 1169130633Srobert strlcpy(hphone, p, 1170130633Srobert sizeof(hphone)); 117120253Sjoerg } 117220253Sjoerg } 117320253Sjoerg } 117420253Sjoerg } 117520253Sjoerg /* 117620253Sjoerg * Handle '&' in gecos field 117720253Sjoerg */ 117820253Sjoerg if ((p = strchr(uname, '&')) != NULL) { 117920253Sjoerg int l = strlen(pwd->pw_name); 118020253Sjoerg int m = strlen(p); 118120253Sjoerg 118220253Sjoerg memmove(p + l, p + 1, m); 118320253Sjoerg memmove(p, pwd->pw_name, l); 118461957Sache *p = (char) toupper((unsigned char)*p); 118520253Sjoerg } 118620590Sdavidn if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 118774569Sache strftime(acexpire, sizeof acexpire, "%c", tptr); 118861957Sache if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 118974569Sache strftime(pwexpire, sizeof pwexpire, "%c", tptr); 119022394Sdavidn printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n" 119120747Sdavidn " Full Name: %s\n" 119220747Sdavidn " Home: %-26.26s Class: %s\n" 119320747Sdavidn " Shell: %-26.26s Office: %s\n" 119420747Sdavidn "Work Phone: %-26.26s Home Phone: %s\n" 119520747Sdavidn "Acc Expire: %-26.26s Pwd Expire: %s\n", 119620253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 119720253Sjoerg grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 119820253Sjoerg uname, pwd->pw_dir, pwd->pw_class, 119920590Sdavidn pwd->pw_shell, office, wphone, hphone, 120020590Sdavidn acexpire, pwexpire); 120144229Sdavidn SETGRENT(); 120220267Sjoerg j = 0; 120344229Sdavidn while ((grp=GETGRENT()) != NULL) 120420267Sjoerg { 120520267Sjoerg int i = 0; 120620747Sdavidn while (grp->gr_mem[i] != NULL) 120720267Sjoerg { 120820267Sjoerg if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 120920267Sjoerg { 121020747Sdavidn printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 121120267Sjoerg break; 121220267Sjoerg } 121320267Sjoerg ++i; 121420267Sjoerg } 121520267Sjoerg } 121644229Sdavidn ENDGRENT(); 121761957Sache printf("%s", j ? "\n" : ""); 121820253Sjoerg } 121920267Sjoerg return EXIT_SUCCESS; 122020253Sjoerg} 122120253Sjoerg 122220679Sdavidnchar * 122320679Sdavidnpw_checkname(u_char *name, int gecos) 122420253Sjoerg{ 1225109961Sgad char showch[8]; 1226109961Sgad u_char const *badchars, *ch, *showtype; 1227109961Sgad int reject; 122820253Sjoerg 1229109961Sgad ch = name; 1230109961Sgad reject = 0; 1231109961Sgad if (gecos) { 1232109961Sgad /* See if the name is valid as a gecos (comment) field. */ 1233109961Sgad badchars = ":!@"; 1234109961Sgad showtype = "gecos field"; 1235109961Sgad } else { 1236109961Sgad /* See if the name is valid as a userid or group. */ 1237109961Sgad badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\""; 1238109961Sgad showtype = "userid/group name"; 1239109961Sgad /* Userids and groups can not have a leading '-'. */ 1240109961Sgad if (*ch == '-') 1241109961Sgad reject = 1; 124220253Sjoerg } 1243109961Sgad if (!reject) { 1244109961Sgad while (*ch) { 1245109961Sgad if (strchr(badchars, *ch) != NULL || *ch < ' ' || 1246109961Sgad *ch == 127) { 1247109961Sgad reject = 1; 1248109961Sgad break; 1249109961Sgad } 1250109961Sgad /* 8-bit characters are only allowed in GECOS fields */ 1251109961Sgad if (!gecos && (*ch & 0x80)) { 1252109961Sgad reject = 1; 1253109961Sgad break; 1254109961Sgad } 1255109961Sgad ch++; 1256109961Sgad } 1257109961Sgad } 1258109961Sgad /* 1259109961Sgad * A `$' is allowed as the final character for userids and groups, 1260109961Sgad * mainly for the benefit of samba. 1261109961Sgad */ 1262109961Sgad if (reject && !gecos) { 1263109961Sgad if (*ch == '$' && *(ch + 1) == '\0') { 1264109961Sgad reject = 0; 1265109961Sgad ch++; 1266109961Sgad } 1267109961Sgad } 1268109961Sgad if (reject) { 1269109961Sgad snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127) 1270109961Sgad ? "`%c'" : "0x%02x", *ch); 1271109961Sgad errx(EX_DATAERR, "invalid character %s at position %d in %s", 1272109961Sgad showch, (ch - name), showtype); 1273109961Sgad } 1274109961Sgad if (!gecos && (ch - name) > LOGNAMESIZE) 1275109961Sgad errx(EX_DATAERR, "name too long `%s' (max is %d)", name, 1276109961Sgad LOGNAMESIZE); 127720679Sdavidn return (char *)name; 127820253Sjoerg} 127920253Sjoerg 128020253Sjoerg 128120253Sjoergstatic void 128220253Sjoergrmat(uid_t uid) 128320253Sjoerg{ 128420253Sjoerg DIR *d = opendir("/var/at/jobs"); 128520253Sjoerg 128620253Sjoerg if (d != NULL) { 128720253Sjoerg struct dirent *e; 128820253Sjoerg 128920253Sjoerg while ((e = readdir(d)) != NULL) { 129020253Sjoerg struct stat st; 129120253Sjoerg 129220253Sjoerg if (strncmp(e->d_name, ".lock", 5) != 0 && 129320253Sjoerg stat(e->d_name, &st) == 0 && 129420253Sjoerg !S_ISDIR(st.st_mode) && 129520253Sjoerg st.st_uid == uid) { 129620253Sjoerg char tmp[MAXPATHLEN]; 129720253Sjoerg 129820253Sjoerg sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 129920253Sjoerg system(tmp); 130020253Sjoerg } 130120253Sjoerg } 130220253Sjoerg closedir(d); 130320253Sjoerg } 130420253Sjoerg} 130520747Sdavidn 130620747Sdavidnstatic void 130785145Sachermopie(char const * name) 130820747Sdavidn{ 130985145Sache static const char etcopie[] = "/etc/opiekeys"; 131085145Sache FILE *fp = fopen(etcopie, "r+"); 131120747Sdavidn 131221052Sdavidn if (fp != NULL) { 131321052Sdavidn char tmp[1024]; 131421052Sdavidn off_t atofs = 0; 131521052Sdavidn int length = strlen(name); 131620747Sdavidn 131721052Sdavidn while (fgets(tmp, sizeof tmp, fp) != NULL) { 131821052Sdavidn if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 131921052Sdavidn if (fseek(fp, atofs, SEEK_SET) == 0) { 132021052Sdavidn fwrite("#", 1, 1, fp); /* Comment username out */ 132121052Sdavidn } 132221052Sdavidn break; 132320747Sdavidn } 132421052Sdavidn atofs = ftell(fp); 132520747Sdavidn } 132621052Sdavidn /* 132721052Sdavidn * If we got an error of any sort, don't update! 132821052Sdavidn */ 132921052Sdavidn fclose(fp); 133020747Sdavidn } 133120747Sdavidn} 133220747Sdavidn 1333