pw_user.c revision 130629
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 130629 2004-06-17 10:29:12Z robert $"; 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; 11820253Sjoerg 11920253Sjoerg static struct passwd fakeuser = 12020253Sjoerg { 12120253Sjoerg NULL, 12220253Sjoerg "*", 12320253Sjoerg -1, 12420253Sjoerg -1, 12520253Sjoerg 0, 12620253Sjoerg "", 12720253Sjoerg "User &", 12856000Sdavidn "/nonexistent", 12920253Sjoerg "/bin/sh", 13020253Sjoerg 0 13156000Sdavidn#if defined(__FreeBSD__) 13256000Sdavidn ,0 13356000Sdavidn#endif 13420253Sjoerg }; 13520253Sjoerg 13652512Sdavidn 13720253Sjoerg /* 13820267Sjoerg * With M_NEXT, we only need to return the 13920267Sjoerg * next uid to stdout 14020267Sjoerg */ 14120267Sjoerg if (mode == M_NEXT) 14220267Sjoerg { 14320267Sjoerg uid_t next = pw_uidpolicy(cnf, args); 14420267Sjoerg if (getarg(args, 'q')) 14520267Sjoerg return next; 14620267Sjoerg printf("%ld:", (long)next); 14720267Sjoerg pw_group(cnf, mode, args); 14820267Sjoerg return EXIT_SUCCESS; 14920267Sjoerg } 15020267Sjoerg 15120267Sjoerg /* 15220253Sjoerg * We can do all of the common legwork here 15320253Sjoerg */ 15420253Sjoerg 15520253Sjoerg if ((arg = getarg(args, 'b')) != NULL) { 15620267Sjoerg cnf->home = arg->val; 15720253Sjoerg } 15821052Sdavidn 15921052Sdavidn /* 16021052Sdavidn * If we'll need to use it or we're updating it, 16121052Sdavidn * then create the base home directory if necessary 16221052Sdavidn */ 16321052Sdavidn if (arg != NULL || getarg(args, 'm') != NULL) { 16421052Sdavidn int l = strlen(cnf->home); 16521052Sdavidn 16621052Sdavidn if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 16721052Sdavidn cnf->home[--l] = '\0'; 16821052Sdavidn 16921052Sdavidn if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 17030259Scharnier errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); 17121052Sdavidn 17221052Sdavidn if (stat(cnf->home, &st) == -1) { 17321052Sdavidn char dbuf[MAXPATHLEN]; 17421052Sdavidn 17521242Sdavidn /* 17621242Sdavidn * This is a kludge especially for Joerg :) 17721242Sdavidn * If the home directory would be created in the root partition, then 17821242Sdavidn * we really create it under /usr which is likely to have more space. 17921242Sdavidn * But we create a symlink from cnf->home -> "/usr" -> cnf->home 18021242Sdavidn */ 18121242Sdavidn if (strchr(cnf->home+1, '/') == NULL) { 18221242Sdavidn strcpy(dbuf, "/usr"); 18321242Sdavidn strncat(dbuf, cnf->home, MAXPATHLEN-5); 18421242Sdavidn if (mkdir(dbuf, 0755) != -1 || errno == EEXIST) { 18521242Sdavidn chown(dbuf, 0, 0); 18621242Sdavidn symlink(dbuf, cnf->home); 18721242Sdavidn } 18821242Sdavidn /* If this falls, fall back to old method */ 18921242Sdavidn } 19021052Sdavidn p = strncpy(dbuf, cnf->home, sizeof dbuf); 19121052Sdavidn dbuf[MAXPATHLEN-1] = '\0'; 19221242Sdavidn if (stat(dbuf, &st) == -1) { 19321242Sdavidn while ((p = strchr(++p, '/')) != NULL) { 19421242Sdavidn *p = '\0'; 19521242Sdavidn if (stat(dbuf, &st) == -1) { 19621242Sdavidn if (mkdir(dbuf, 0755) == -1) 19721242Sdavidn goto direrr; 19821242Sdavidn chown(dbuf, 0, 0); 19921242Sdavidn } else if (!S_ISDIR(st.st_mode)) 20030259Scharnier errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); 20121242Sdavidn *p = '/'; 20221242Sdavidn } 20321052Sdavidn } 20421242Sdavidn if (stat(dbuf, &st) == -1) { 20521052Sdavidn if (mkdir(dbuf, 0755) == -1) { 20630259Scharnier direrr: err(EX_OSFILE, "mkdir '%s'", dbuf); 20721052Sdavidn } 20821052Sdavidn chown(dbuf, 0, 0); 20921052Sdavidn } 21021052Sdavidn } else if (!S_ISDIR(st.st_mode)) 21130259Scharnier errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); 21221052Sdavidn } 21321052Sdavidn 21420253Sjoerg if ((arg = getarg(args, 'e')) != NULL) 21520253Sjoerg cnf->expire_days = atoi(arg->val); 21620253Sjoerg 21721330Sdavidn if ((arg = getarg(args, 'y')) != NULL) 21821330Sdavidn cnf->nispasswd = arg->val; 21921330Sdavidn 22020253Sjoerg if ((arg = getarg(args, 'p')) != NULL && arg->val) 22120253Sjoerg cnf->password_days = atoi(arg->val); 22220253Sjoerg 22320253Sjoerg if ((arg = getarg(args, 'g')) != NULL) { 22463596Sdavidn if (!*(p = arg->val)) /* Handle empty group list specially */ 22563596Sdavidn cnf->default_group = ""; 22663596Sdavidn else { 22763596Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 22863596Sdavidn if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 22963596Sdavidn errx(EX_NOUSER, "group `%s' does not exist", p); 23063596Sdavidn } 23163596Sdavidn cnf->default_group = newstr(grp->gr_name); 23220253Sjoerg } 23320253Sjoerg } 23420253Sjoerg if ((arg = getarg(args, 'L')) != NULL) 23520679Sdavidn cnf->default_class = pw_checkname((u_char *)arg->val, 0); 23620253Sjoerg 23720253Sjoerg if ((arg = getarg(args, 'G')) != NULL && arg->val) { 23852527Sdavidn int i = 0; 23920253Sjoerg 24020747Sdavidn for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 24144229Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 24261957Sache if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 24330259Scharnier errx(EX_NOUSER, "group `%s' does not exist", p); 24420253Sjoerg } 24520747Sdavidn if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 24620747Sdavidn cnf->groups[i++] = newstr(grp->gr_name); 24720253Sjoerg } 24820747Sdavidn while (i < cnf->numgroups) 24920253Sjoerg cnf->groups[i++] = NULL; 25020253Sjoerg } 25152527Sdavidn 25220253Sjoerg if ((arg = getarg(args, 'k')) != NULL) { 25326088Sdavidn if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode)) 25430259Scharnier errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir); 25520253Sjoerg } 25652527Sdavidn 25720253Sjoerg if ((arg = getarg(args, 's')) != NULL) 25820253Sjoerg cnf->shell_default = arg->val; 25920253Sjoerg 26063600Sdavidn if ((arg = getarg(args, 'w')) != NULL) 26163600Sdavidn cnf->default_password = boolean_val(arg->val, cnf->default_password); 26220253Sjoerg if (mode == M_ADD && getarg(args, 'D')) { 26320253Sjoerg if (getarg(args, 'n') != NULL) 26430259Scharnier errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 26520253Sjoerg if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 26620253Sjoerg if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 26720253Sjoerg cnf->min_uid = 1000; 26820253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 26920253Sjoerg cnf->max_uid = 32000; 27020253Sjoerg } 27120253Sjoerg if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 27220253Sjoerg if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 27320253Sjoerg cnf->min_gid = 1000; 27420253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 27520253Sjoerg cnf->max_gid = 32000; 27620253Sjoerg } 27720253Sjoerg 27820253Sjoerg arg = getarg(args, 'C'); 27920253Sjoerg if (write_userconfig(arg ? arg->val : NULL)) 28020267Sjoerg return EXIT_SUCCESS; 28130259Scharnier warn("config update"); 28220267Sjoerg return EX_IOERR; 28320253Sjoerg } 28452527Sdavidn 28520253Sjoerg if (mode == M_PRINT && getarg(args, 'a')) { 28620267Sjoerg int pretty = getarg(args, 'P') != NULL; 28744386Sdavidn int v7 = getarg(args, '7') != NULL; 28820253Sjoerg 28944229Sdavidn SETPWENT(); 29044229Sdavidn while ((pwd = GETPWENT()) != NULL) 29144386Sdavidn print_user(pwd, pretty, v7); 29244229Sdavidn ENDPWENT(); 29320267Sjoerg return EXIT_SUCCESS; 29420253Sjoerg } 29552527Sdavidn 29620253Sjoerg if ((a_name = getarg(args, 'n')) != NULL) 29744229Sdavidn pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0)); 29820253Sjoerg a_uid = getarg(args, 'u'); 29920253Sjoerg 30020253Sjoerg if (a_uid == NULL) { 30120253Sjoerg if (a_name == NULL) 30230259Scharnier errx(EX_DATAERR, "user name or id required"); 30320253Sjoerg 30420253Sjoerg /* 30520253Sjoerg * Determine whether 'n' switch is name or uid - we don't 30620253Sjoerg * really don't really care which we have, but we need to 30720253Sjoerg * know. 30820253Sjoerg */ 30943780Sdes if (mode != M_ADD && pwd == NULL 31043780Sdes && strspn(a_name->val, "0123456789") == strlen(a_name->val) 31143780Sdes && atoi(a_name->val) > 0) { /* Assume uid */ 31220253Sjoerg (a_uid = a_name)->ch = 'u'; 31320253Sjoerg a_name = NULL; 31420253Sjoerg } 31520253Sjoerg } 31652527Sdavidn 31720253Sjoerg /* 31820253Sjoerg * Update, delete & print require that the user exists 31920253Sjoerg */ 32052512Sdavidn if (mode == M_UPDATE || mode == M_DELETE || 32152512Sdavidn mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) { 32252527Sdavidn 32320253Sjoerg if (a_name == NULL && pwd == NULL) /* Try harder */ 32444229Sdavidn pwd = GETPWUID(atoi(a_uid->val)); 32520253Sjoerg 32620253Sjoerg if (pwd == NULL) { 32720253Sjoerg if (mode == M_PRINT && getarg(args, 'F')) { 32820253Sjoerg fakeuser.pw_name = a_name ? a_name->val : "nouser"; 32920253Sjoerg fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 33044386Sdavidn return print_user(&fakeuser, 33144386Sdavidn getarg(args, 'P') != NULL, 33244386Sdavidn getarg(args, '7') != NULL); 33320253Sjoerg } 33420253Sjoerg if (a_name == NULL) 33530259Scharnier errx(EX_NOUSER, "no such uid `%s'", a_uid->val); 33630259Scharnier errx(EX_NOUSER, "no such user `%s'", a_name->val); 33720253Sjoerg } 33852527Sdavidn 33920253Sjoerg if (a_name == NULL) /* May be needed later */ 34020253Sjoerg a_name = addarg(args, 'n', newstr(pwd->pw_name)); 34120253Sjoerg 34220253Sjoerg /* 34352512Sdavidn * The M_LOCK and M_UNLOCK functions simply add or remove 34452512Sdavidn * a "*LOCKED*" prefix from in front of the password to 34552512Sdavidn * prevent it decoding correctly, and therefore prevents 34652512Sdavidn * access. Of course, this only prevents access via 34752512Sdavidn * password authentication (not ssh, kerberos or any 34852512Sdavidn * other method that does not use the UNIX password) but 34952512Sdavidn * that is a known limitation. 35052512Sdavidn */ 35152512Sdavidn 35252512Sdavidn if (mode == M_LOCK) { 35352512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0) 35452512Sdavidn errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 35552512Sdavidn passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str)); 35652512Sdavidn if (passtmp == NULL) /* disaster */ 35752512Sdavidn errx(EX_UNAVAILABLE, "out of memory"); 35852512Sdavidn strcpy(passtmp, locked_str); 35952512Sdavidn strcat(passtmp, pwd->pw_passwd); 36052512Sdavidn pwd->pw_passwd = passtmp; 36152527Sdavidn edited = 1; 36252512Sdavidn } else if (mode == M_UNLOCK) { 36352512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0) 36452512Sdavidn errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 36552512Sdavidn pwd->pw_passwd += sizeof(locked_str)-1; 36652527Sdavidn edited = 1; 36752527Sdavidn } else if (mode == M_DELETE) { 36852527Sdavidn /* 36952527Sdavidn * Handle deletions now 37052527Sdavidn */ 37120253Sjoerg char file[MAXPATHLEN]; 37220253Sjoerg char home[MAXPATHLEN]; 37320253Sjoerg uid_t uid = pwd->pw_uid; 37420253Sjoerg 37520253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 37630259Scharnier errx(EX_DATAERR, "cannot remove user 'root'"); 37720253Sjoerg 37844229Sdavidn if (!PWALTDIR()) { 37944229Sdavidn /* 38085145Sache * Remove opie record from /etc/opiekeys 38144229Sdavidn */ 38220747Sdavidn 38385145Sache rmopie(pwd->pw_name); 38420747Sdavidn 38544229Sdavidn /* 38644229Sdavidn * Remove crontabs 38744229Sdavidn */ 38844229Sdavidn sprintf(file, "/var/cron/tabs/%s", pwd->pw_name); 38944229Sdavidn if (access(file, F_OK) == 0) { 39044229Sdavidn sprintf(file, "crontab -u %s -r", pwd->pw_name); 39144229Sdavidn system(file); 39244229Sdavidn } 39320253Sjoerg } 39420253Sjoerg /* 39520253Sjoerg * Save these for later, since contents of pwd may be 39620253Sjoerg * invalidated by deletion 39720253Sjoerg */ 39820253Sjoerg sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 39920253Sjoerg strncpy(home, pwd->pw_dir, sizeof home); 40020253Sjoerg home[sizeof home - 1] = '\0'; 40120253Sjoerg 40252502Sdavidn rc = delpwent(pwd); 40352502Sdavidn if (rc == -1) 40452502Sdavidn err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 40552502Sdavidn else if (rc != 0) { 40656000Sdavidn warn("passwd update"); 40752502Sdavidn return EX_IOERR; 40852502Sdavidn } 40921330Sdavidn 41052502Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/') { 41152502Sdavidn rc = delnispwent(cnf->nispasswd, a_name->val); 41252502Sdavidn if (rc == -1) 41352502Sdavidn warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name); 41452502Sdavidn else if (rc != 0) 41556000Sdavidn warn("WARNING: NIS passwd update"); 41652502Sdavidn /* non-fatal */ 41752502Sdavidn } 41852502Sdavidn 41920253Sjoerg editgroups(a_name->val, NULL); 42020253Sjoerg 42120253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 42220253Sjoerg 42344229Sdavidn if (!PWALTDIR()) { 42444229Sdavidn /* 42544229Sdavidn * Remove mail file 42644229Sdavidn */ 42744229Sdavidn remove(file); 42820253Sjoerg 42944229Sdavidn /* 43044229Sdavidn * Remove at jobs 43144229Sdavidn */ 43244229Sdavidn if (getpwuid(uid) == NULL) 43344229Sdavidn rmat(uid); 43420253Sjoerg 43544229Sdavidn /* 43644229Sdavidn * Remove home directory and contents 43744229Sdavidn */ 43844229Sdavidn if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 43944229Sdavidn if (stat(home, &st) != -1) { 44044229Sdavidn rm_r(home, uid); 44144229Sdavidn pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 44244229Sdavidn a_name->val, (long) uid, home, 44344229Sdavidn stat(home, &st) == -1 ? "" : "not completely "); 44444229Sdavidn } 44520253Sjoerg } 44620253Sjoerg } 44720267Sjoerg return EXIT_SUCCESS; 44820253Sjoerg } else if (mode == M_PRINT) 44944386Sdavidn return print_user(pwd, 45044386Sdavidn getarg(args, 'P') != NULL, 45144386Sdavidn getarg(args, '7') != NULL); 45220253Sjoerg 45320253Sjoerg /* 45420253Sjoerg * The rest is edit code 45520253Sjoerg */ 45620253Sjoerg if ((arg = getarg(args, 'l')) != NULL) { 45720253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 45830259Scharnier errx(EX_DATAERR, "can't rename `root' account"); 45920679Sdavidn pwd->pw_name = pw_checkname((u_char *)arg->val, 0); 46052527Sdavidn edited = 1; 46120253Sjoerg } 46252527Sdavidn 46361957Sache if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) { 46420253Sjoerg pwd->pw_uid = (uid_t) atol(arg->val); 46552527Sdavidn edited = 1; 46620253Sjoerg if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 46730259Scharnier errx(EX_DATAERR, "can't change uid of `root' account"); 46820253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 46930259Scharnier warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 47020253Sjoerg } 47120253Sjoerg 47252527Sdavidn if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */ 47352527Sdavidn gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid; 47452527Sdavidn if (newgid != pwd->pw_gid) { 47552527Sdavidn edited = 1; 47661762Sdavidn pwd->pw_gid = newgid; 47752527Sdavidn } 47852527Sdavidn } 47952527Sdavidn 48020253Sjoerg if ((arg = getarg(args, 'p')) != NULL) { 48152527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 48252527Sdavidn if (pwd->pw_change != 0) { 48352527Sdavidn pwd->pw_change = 0; 48452527Sdavidn edited = 1; 48552527Sdavidn } 48652527Sdavidn } 48720253Sjoerg else { 48820253Sjoerg time_t now = time(NULL); 48920253Sjoerg time_t expire = parse_date(now, arg->val); 49020253Sjoerg 49120253Sjoerg if (now == expire) 49230259Scharnier errx(EX_DATAERR, "invalid password change date `%s'", arg->val); 49352527Sdavidn if (pwd->pw_change != expire) { 49452527Sdavidn pwd->pw_change = expire; 49552527Sdavidn edited = 1; 49652527Sdavidn } 49720253Sjoerg } 49820253Sjoerg } 49952527Sdavidn 50020267Sjoerg if ((arg = getarg(args, 'e')) != NULL) { 50152527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 50252527Sdavidn if (pwd->pw_expire != 0) { 50352527Sdavidn pwd->pw_expire = 0; 50452527Sdavidn edited = 1; 50552527Sdavidn } 50652527Sdavidn } 50720253Sjoerg else { 50820253Sjoerg time_t now = time(NULL); 50920253Sjoerg time_t expire = parse_date(now, arg->val); 51020253Sjoerg 51120253Sjoerg if (now == expire) 51230259Scharnier errx(EX_DATAERR, "invalid account expiry date `%s'", arg->val); 51352527Sdavidn if (pwd->pw_expire != expire) { 51452527Sdavidn pwd->pw_expire = expire; 51552527Sdavidn edited = 1; 51652527Sdavidn } 51720253Sjoerg } 51820253Sjoerg } 51920253Sjoerg 52052527Sdavidn if ((arg = getarg(args, 's')) != NULL) { 52152527Sdavidn char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 52252527Sdavidn if (shell == NULL) 52352527Sdavidn shell = ""; 52452527Sdavidn if (strcmp(shell, pwd->pw_shell) != 0) { 52552527Sdavidn pwd->pw_shell = shell; 52652527Sdavidn edited = 1; 52752527Sdavidn } 52852527Sdavidn } 52920253Sjoerg 53052527Sdavidn if (getarg(args, 'L')) { 53152527Sdavidn if (cnf->default_class == NULL) 53252527Sdavidn cnf->default_class = ""; 53352527Sdavidn if (strcmp(pwd->pw_class, cnf->default_class) != 0) { 53452527Sdavidn pwd->pw_class = cnf->default_class; 53552527Sdavidn edited = 1; 53652527Sdavidn } 53752527Sdavidn } 53852527Sdavidn 53920747Sdavidn if ((arg = getarg(args, 'd')) != NULL) { 540130629Srobert if (strcmp(pwd->pw_dir, arg->val)) 541130629Srobert edited = 1; 54220747Sdavidn if (stat(pwd->pw_dir = arg->val, &st) == -1) { 54320747Sdavidn if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 54430259Scharnier warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 54520747Sdavidn } else if (!S_ISDIR(st.st_mode)) 54630259Scharnier warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 54720747Sdavidn } 54820747Sdavidn 549124382Siedowse if ((arg = getarg(args, 'w')) != NULL && 550124382Siedowse getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) { 55164918Sgreen login_cap_t *lc; 55264918Sgreen 55364918Sgreen lc = login_getpwclass(pwd); 55464918Sgreen if (lc == NULL || 55564918Sgreen login_setcryptfmt(lc, "md5", NULL) == NULL) 55664918Sgreen warn("setting crypt(3) format"); 55764918Sgreen login_close(lc); 55820267Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 55952527Sdavidn edited = 1; 56052527Sdavidn } 56120267Sjoerg 56220253Sjoerg } else { 56364918Sgreen login_cap_t *lc; 56452527Sdavidn 56552527Sdavidn /* 56652527Sdavidn * Add code 56752527Sdavidn */ 56852527Sdavidn 56920253Sjoerg if (a_name == NULL) /* Required */ 57030259Scharnier errx(EX_DATAERR, "login name required"); 57144229Sdavidn else if ((pwd = GETPWNAM(a_name->val)) != NULL) /* Exists */ 57230259Scharnier errx(EX_DATAERR, "login name `%s' already exists", a_name->val); 57320253Sjoerg 57420253Sjoerg /* 57520253Sjoerg * Now, set up defaults for a new user 57620253Sjoerg */ 57720253Sjoerg pwd = &fakeuser; 57820253Sjoerg pwd->pw_name = a_name->val; 57920253Sjoerg pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 58020253Sjoerg pwd->pw_uid = pw_uidpolicy(cnf, args); 58120253Sjoerg pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 58220253Sjoerg pwd->pw_change = pw_pwdpolicy(cnf, args); 58320253Sjoerg pwd->pw_expire = pw_exppolicy(cnf, args); 58420253Sjoerg pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 58520253Sjoerg pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 58664918Sgreen lc = login_getpwclass(pwd); 58764918Sgreen if (lc == NULL || login_setcryptfmt(lc, "md5", NULL) == NULL) 58864918Sgreen warn("setting crypt(3) format"); 58964918Sgreen login_close(lc); 59064918Sgreen pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 59152527Sdavidn edited = 1; 59220253Sjoerg 59320253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 59430259Scharnier warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 59520253Sjoerg } 59620253Sjoerg 59720253Sjoerg /* 59820253Sjoerg * Shared add/edit code 59920253Sjoerg */ 60052527Sdavidn if ((arg = getarg(args, 'c')) != NULL) { 60152527Sdavidn char *gecos = pw_checkname((u_char *)arg->val, 1); 60252527Sdavidn if (strcmp(pwd->pw_gecos, gecos) != 0) { 60352527Sdavidn pwd->pw_gecos = gecos; 60452527Sdavidn edited = 1; 60552527Sdavidn } 60652527Sdavidn } 60720253Sjoerg 608124382Siedowse if ((arg = getarg(args, 'h')) != NULL || 609124382Siedowse (arg = getarg(args, 'H')) != NULL) { 61063572Sdavidn if (strcmp(arg->val, "-") == 0) { 61163572Sdavidn if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 61263572Sdavidn pwd->pw_passwd = "*"; /* No access */ 61363572Sdavidn edited = 1; 61463572Sdavidn } 61563572Sdavidn } else { 61620253Sjoerg int fd = atoi(arg->val); 617124382Siedowse int precrypt = (arg->ch == 'H'); 61820253Sjoerg int b; 61920253Sjoerg int istty = isatty(fd); 62020253Sjoerg struct termios t; 62164918Sgreen login_cap_t *lc; 62220253Sjoerg 62320253Sjoerg if (istty) { 62420253Sjoerg if (tcgetattr(fd, &t) == -1) 62520253Sjoerg istty = 0; 62620253Sjoerg else { 62720253Sjoerg struct termios n = t; 62820253Sjoerg 62920253Sjoerg /* Disable echo */ 63020253Sjoerg n.c_lflag &= ~(ECHO); 63120253Sjoerg tcsetattr(fd, TCSANOW, &n); 632124382Siedowse printf("%s%spassword for user %s:", 633124382Siedowse (mode == M_UPDATE) ? "new " : "", 634124382Siedowse precrypt ? "encrypted " : "", 635124382Siedowse pwd->pw_name); 63620253Sjoerg fflush(stdout); 63720253Sjoerg } 63820253Sjoerg } 63920253Sjoerg b = read(fd, line, sizeof(line) - 1); 64020253Sjoerg if (istty) { /* Restore state */ 64120253Sjoerg tcsetattr(fd, TCSANOW, &t); 64220253Sjoerg fputc('\n', stdout); 64320253Sjoerg fflush(stdout); 64420253Sjoerg } 64520253Sjoerg if (b < 0) { 646124382Siedowse warn("-%c file descriptor", precrypt ? 'H' : 647124382Siedowse 'h'); 64820267Sjoerg return EX_IOERR; 64920253Sjoerg } 65020253Sjoerg line[b] = '\0'; 65120253Sjoerg if ((p = strpbrk(line, " \t\r\n")) != NULL) 65220253Sjoerg *p = '\0'; 65320253Sjoerg if (!*line) 65430259Scharnier errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 655124382Siedowse if (precrypt) { 656124382Siedowse if (strchr(line, ':') != NULL) 657124382Siedowse return EX_DATAERR; 658124382Siedowse pwd->pw_passwd = line; 659124382Siedowse } else { 660124382Siedowse lc = login_getpwclass(pwd); 661124382Siedowse if (lc == NULL || 662124382Siedowse login_setcryptfmt(lc, "md5", NULL) == NULL) 663124382Siedowse warn("setting crypt(3) format"); 664124382Siedowse login_close(lc); 665124382Siedowse pwd->pw_passwd = pw_pwcrypt(line); 666124382Siedowse } 66752527Sdavidn edited = 1; 66820253Sjoerg } 66920253Sjoerg } 67020267Sjoerg 67120267Sjoerg /* 67220267Sjoerg * Special case: -N only displays & exits 67320267Sjoerg */ 67420267Sjoerg if (getarg(args, 'N') != NULL) 67544386Sdavidn return print_user(pwd, 67644386Sdavidn getarg(args, 'P') != NULL, 67744386Sdavidn getarg(args, '7') != NULL); 67820267Sjoerg 67921330Sdavidn if (mode == M_ADD) { 68052527Sdavidn edited = 1; /* Always */ 68152502Sdavidn rc = addpwent(pwd); 68252502Sdavidn if (rc == -1) { 68352502Sdavidn warnx("user '%s' already exists", pwd->pw_name); 68452502Sdavidn return EX_IOERR; 68552502Sdavidn } else if (rc != 0) { 68656000Sdavidn warn("passwd file update"); 68752502Sdavidn return EX_IOERR; 68852502Sdavidn } 68952502Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/') { 69052502Sdavidn rc = addnispwent(cnf->nispasswd, pwd); 69152502Sdavidn if (rc == -1) 69252502Sdavidn warnx("User '%s' already exists in NIS passwd", pwd->pw_name); 69352502Sdavidn else 69456000Sdavidn warn("NIS passwd update"); 69552502Sdavidn /* NOTE: we treat NIS-only update errors as non-fatal */ 69652502Sdavidn } 69752512Sdavidn } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) { 69852527Sdavidn if (edited) { /* Only updated this if required */ 69952527Sdavidn rc = chgpwent(a_name->val, pwd); 70052527Sdavidn if (rc == -1) { 70152527Sdavidn warnx("user '%s' does not exist (NIS?)", pwd->pw_name); 70252527Sdavidn return EX_IOERR; 70352527Sdavidn } else if (rc != 0) { 70456000Sdavidn warn("passwd file update"); 70552527Sdavidn return EX_IOERR; 70652527Sdavidn } 70752527Sdavidn if ( cnf->nispasswd && *cnf->nispasswd=='/') { 70852527Sdavidn rc = chgnispwent(cnf->nispasswd, a_name->val, pwd); 70952527Sdavidn if (rc == -1) 71052527Sdavidn warn("User '%s' not found in NIS passwd", pwd->pw_name); 71152527Sdavidn else 71256000Sdavidn warn("NIS passwd update"); 71352527Sdavidn /* NOTE: NIS-only update errors are not fatal */ 71452527Sdavidn } 71552502Sdavidn } 71621330Sdavidn } 71721330Sdavidn 71820253Sjoerg /* 71920253Sjoerg * Ok, user is created or changed - now edit group file 72020253Sjoerg */ 72120253Sjoerg 72220253Sjoerg if (mode == M_ADD || getarg(args, 'G') != NULL) 72320253Sjoerg editgroups(pwd->pw_name, cnf->groups); 72420253Sjoerg 72561759Sdavidn /* go get a current version of pwd */ 72661759Sdavidn pwd = GETPWNAM(a_name->val); 72761759Sdavidn if (pwd == NULL) { 72861759Sdavidn /* This will fail when we rename, so special case that */ 72961759Sdavidn if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) { 73061759Sdavidn a_name->val = arg->val; /* update new name */ 73161759Sdavidn pwd = GETPWNAM(a_name->val); /* refetch renamed rec */ 73261759Sdavidn } 73361759Sdavidn } 73461759Sdavidn if (pwd == NULL) /* can't go on without this */ 73530259Scharnier errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val); 73620253Sjoerg 73744229Sdavidn grp = GETGRGID(pwd->pw_gid); 73898744Sdwmalone pw_log(cnf, mode, W_USER, "%s(%ld):%s(%ld):%s:%s:%s", 73920253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 74020253Sjoerg grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 74120253Sjoerg pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 74220253Sjoerg 74320253Sjoerg /* 74420253Sjoerg * If adding, let's touch and chown the user's mail file. This is not 74520253Sjoerg * strictly necessary under BSD with a 0755 maildir but it also 74620253Sjoerg * doesn't hurt anything to create the empty mailfile 74720253Sjoerg */ 74820253Sjoerg if (mode == M_ADD) { 74944229Sdavidn if (!PWALTDIR()) { 75044229Sdavidn sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 75144229Sdavidn close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 75244229Sdavidn * mtime */ 75344229Sdavidn chown(line, pwd->pw_uid, pwd->pw_gid); 75420253Sjoerg } 75520253Sjoerg } 75652527Sdavidn 75720253Sjoerg /* 75882868Sdd * Let's create and populate the user's home directory. Note 75920253Sjoerg * that this also `works' for editing users if -m is used, but 76020253Sjoerg * existing files will *not* be overwritten. 76120253Sjoerg */ 76244229Sdavidn if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 76320253Sjoerg copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid); 76420253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 76520253Sjoerg pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 76620253Sjoerg } 76752527Sdavidn 76882868Sdd 76982868Sdd /* 77082868Sdd * Finally, send mail to the new user as well, if we are asked to 77182868Sdd */ 77282868Sdd if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 77382868Sdd FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 77482868Sdd 77582868Sdd if (pfp == NULL) 77682868Sdd warn("sendmail"); 77782868Sdd else { 77882868Sdd fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 77982868Sdd while (fgets(line, sizeof(line), fp) != NULL) { 78082868Sdd /* Do substitutions? */ 78182868Sdd fputs(line, pfp); 78282868Sdd } 78382868Sdd pclose(pfp); 78482868Sdd pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 78582868Sdd pwd->pw_name, (long) pwd->pw_uid); 78682868Sdd } 78782868Sdd fclose(fp); 78882868Sdd } 78982868Sdd 79020267Sjoerg return EXIT_SUCCESS; 79120253Sjoerg} 79220253Sjoerg 79320253Sjoerg 79420253Sjoergstatic uid_t 79520253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args) 79620253Sjoerg{ 79720253Sjoerg struct passwd *pwd; 79820253Sjoerg uid_t uid = (uid_t) - 1; 79920253Sjoerg struct carg *a_uid = getarg(args, 'u'); 80020253Sjoerg 80120253Sjoerg /* 80220253Sjoerg * Check the given uid, if any 80320253Sjoerg */ 80420253Sjoerg if (a_uid != NULL) { 80520253Sjoerg uid = (uid_t) atol(a_uid->val); 80620253Sjoerg 80744229Sdavidn if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL) 80830259Scharnier errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid); 80920253Sjoerg } else { 81020253Sjoerg struct bitmap bm; 81120253Sjoerg 81220253Sjoerg /* 81320253Sjoerg * We need to allocate the next available uid under one of 81420253Sjoerg * two policies a) Grab the first unused uid b) Grab the 81520253Sjoerg * highest possible unused uid 81620253Sjoerg */ 81720253Sjoerg if (cnf->min_uid >= cnf->max_uid) { /* Sanity 81820253Sjoerg * claus^H^H^H^Hheck */ 81920253Sjoerg cnf->min_uid = 1000; 82020253Sjoerg cnf->max_uid = 32000; 82120253Sjoerg } 82220253Sjoerg bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 82320253Sjoerg 82420253Sjoerg /* 82520253Sjoerg * Now, let's fill the bitmap from the password file 82620253Sjoerg */ 82744229Sdavidn SETPWENT(); 82844229Sdavidn while ((pwd = GETPWENT()) != NULL) 82944229Sdavidn if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 83020253Sjoerg bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 83144229Sdavidn ENDPWENT(); 83220253Sjoerg 83320253Sjoerg /* 83420253Sjoerg * Then apply the policy, with fallback to reuse if necessary 83520253Sjoerg */ 83620253Sjoerg if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 83720253Sjoerg uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 83820253Sjoerg 83920253Sjoerg /* 84020253Sjoerg * Another sanity check 84120253Sjoerg */ 84220253Sjoerg if (uid < cnf->min_uid || uid > cnf->max_uid) 84330259Scharnier errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 84420253Sjoerg bm_dealloc(&bm); 84520253Sjoerg } 84620253Sjoerg return uid; 84720253Sjoerg} 84820253Sjoerg 84920253Sjoerg 85020253Sjoergstatic uid_t 85120253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 85220253Sjoerg{ 85320253Sjoerg struct group *grp; 85420253Sjoerg gid_t gid = (uid_t) - 1; 85520253Sjoerg struct carg *a_gid = getarg(args, 'g'); 85620253Sjoerg 85720253Sjoerg /* 85820253Sjoerg * If no arg given, see if default can help out 85920253Sjoerg */ 86020253Sjoerg if (a_gid == NULL && cnf->default_group && *cnf->default_group) 86120253Sjoerg a_gid = addarg(args, 'g', cnf->default_group); 86220253Sjoerg 86320253Sjoerg /* 86420253Sjoerg * Check the given gid, if any 86520253Sjoerg */ 86644229Sdavidn SETGRENT(); 86720253Sjoerg if (a_gid != NULL) { 86844229Sdavidn if ((grp = GETGRNAM(a_gid->val)) == NULL) { 86920253Sjoerg gid = (gid_t) atol(a_gid->val); 87061957Sache if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL) 87130259Scharnier errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 87220253Sjoerg } 87320253Sjoerg gid = grp->gr_gid; 87444229Sdavidn } else if ((grp = GETGRNAM(nam)) != NULL && grp->gr_mem[0] == NULL) { 87520267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 87620253Sjoerg } else { 87720253Sjoerg struct cargs grpargs; 87820253Sjoerg char tmp[32]; 87920253Sjoerg 88020253Sjoerg LIST_INIT(&grpargs); 88120253Sjoerg addarg(&grpargs, 'n', nam); 88220253Sjoerg 88320253Sjoerg /* 88420253Sjoerg * We need to auto-create a group with the user's name. We 88520253Sjoerg * can send all the appropriate output to our sister routine 88620253Sjoerg * bit first see if we can create a group with gid==uid so we 88720253Sjoerg * can keep the user and group ids in sync. We purposely do 88820253Sjoerg * NOT check the gid range if we can force the sync. If the 88920253Sjoerg * user's name dups an existing group, then the group add 89020253Sjoerg * function will happily handle that case for us and exit. 89120253Sjoerg */ 89244229Sdavidn if (GETGRGID(prefer) == NULL) { 89320253Sjoerg sprintf(tmp, "%lu", (unsigned long) prefer); 89420253Sjoerg addarg(&grpargs, 'g', tmp); 89520253Sjoerg } 89620267Sjoerg if (getarg(args, 'N')) 89720267Sjoerg { 89820267Sjoerg addarg(&grpargs, 'N', NULL); 89920267Sjoerg addarg(&grpargs, 'q', NULL); 90020267Sjoerg gid = pw_group(cnf, M_NEXT, &grpargs); 90120267Sjoerg } 90220267Sjoerg else 90320267Sjoerg { 90420267Sjoerg pw_group(cnf, M_ADD, &grpargs); 90544229Sdavidn if ((grp = GETGRNAM(nam)) != NULL) 90620267Sjoerg gid = grp->gr_gid; 90720267Sjoerg } 90870486Sben a_gid = LIST_FIRST(&grpargs); 90920253Sjoerg while (a_gid != NULL) { 91070486Sben struct carg *t = LIST_NEXT(a_gid, list); 91120253Sjoerg LIST_REMOVE(a_gid, list); 91220253Sjoerg a_gid = t; 91320253Sjoerg } 91420253Sjoerg } 91544229Sdavidn ENDGRENT(); 91620253Sjoerg return gid; 91720253Sjoerg} 91820253Sjoerg 91920253Sjoerg 92020253Sjoergstatic time_t 92120253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args) 92220253Sjoerg{ 92320253Sjoerg time_t result = 0; 92420253Sjoerg time_t now = time(NULL); 92527831Sdavidn struct carg *arg = getarg(args, 'p'); 92620253Sjoerg 92720253Sjoerg if (arg != NULL) { 92820253Sjoerg if ((result = parse_date(now, arg->val)) == now) 92930259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 93020253Sjoerg } else if (cnf->password_days > 0) 93120253Sjoerg result = now + ((long) cnf->password_days * 86400L); 93220253Sjoerg return result; 93320253Sjoerg} 93420253Sjoerg 93520253Sjoerg 93620253Sjoergstatic time_t 93720253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args) 93820253Sjoerg{ 93920253Sjoerg time_t result = 0; 94020253Sjoerg time_t now = time(NULL); 94120253Sjoerg struct carg *arg = getarg(args, 'e'); 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->expire_days > 0) 94720253Sjoerg result = now + ((long) cnf->expire_days * 86400L); 94820253Sjoerg return result; 94920253Sjoerg} 95020253Sjoerg 95120253Sjoerg 95220253Sjoergstatic char * 95320253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 95420253Sjoerg{ 95520253Sjoerg struct carg *arg = getarg(args, 'd'); 95620253Sjoerg 95720253Sjoerg if (arg) 95820253Sjoerg return arg->val; 95920253Sjoerg else { 96020253Sjoerg static char home[128]; 96120253Sjoerg 96220253Sjoerg if (cnf->home == NULL || *cnf->home == '\0') 96330259Scharnier errx(EX_CONFIG, "no base home directory set"); 96420253Sjoerg sprintf(home, "%s/%s", cnf->home, user); 96520253Sjoerg return home; 96620253Sjoerg } 96720253Sjoerg} 96820253Sjoerg 96920253Sjoergstatic char * 97020253Sjoergshell_path(char const * path, char *shells[], char *sh) 97120253Sjoerg{ 97220253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 97320253Sjoerg return sh; /* specified full path or forced none */ 97420253Sjoerg else { 97520253Sjoerg char *p; 97620253Sjoerg char paths[_UC_MAXLINE]; 97720253Sjoerg 97820253Sjoerg /* 97920253Sjoerg * We need to search paths 98020253Sjoerg */ 98120253Sjoerg strncpy(paths, path, sizeof paths); 98220253Sjoerg paths[sizeof paths - 1] = '\0'; 98320253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 98420253Sjoerg int i; 98520253Sjoerg static char shellpath[256]; 98620253Sjoerg 98720253Sjoerg if (sh != NULL) { 98820253Sjoerg sprintf(shellpath, "%s/%s", p, sh); 98920253Sjoerg if (access(shellpath, X_OK) == 0) 99020253Sjoerg return shellpath; 99120253Sjoerg } else 99220253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 99320253Sjoerg sprintf(shellpath, "%s/%s", p, shells[i]); 99420253Sjoerg if (access(shellpath, X_OK) == 0) 99520253Sjoerg return shellpath; 99620253Sjoerg } 99720253Sjoerg } 99820253Sjoerg if (sh == NULL) 99930259Scharnier errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 100030259Scharnier errx(EX_CONFIG, "no default shell available or defined"); 100120253Sjoerg return NULL; 100220253Sjoerg } 100320253Sjoerg} 100420253Sjoerg 100520253Sjoerg 100620253Sjoergstatic char * 100720253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 100820253Sjoerg{ 100920253Sjoerg char *sh = newshell; 101020253Sjoerg struct carg *arg = getarg(args, 's'); 101120253Sjoerg 101220253Sjoerg if (newshell == NULL && arg != NULL) 101320253Sjoerg sh = arg->val; 101420253Sjoerg return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 101520253Sjoerg} 101620253Sjoerg 101720253Sjoergstatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."; 101820253Sjoerg 101920253Sjoergchar * 102020253Sjoergpw_pwcrypt(char *password) 102120253Sjoerg{ 102220253Sjoerg int i; 102320253Sjoerg char salt[12]; 102420253Sjoerg 102520253Sjoerg static char buf[256]; 102620253Sjoerg 102720253Sjoerg /* 102820253Sjoerg * Calculate a salt value 102920253Sjoerg */ 103020253Sjoerg for (i = 0; i < 8; i++) 103173563Skris salt[i] = chars[arc4random() % 63]; 103220253Sjoerg salt[i] = '\0'; 103320253Sjoerg 103420253Sjoerg return strcpy(buf, crypt(password, salt)); 103520253Sjoerg} 103620253Sjoerg 103720590Sdavidn#if defined(USE_MD5RAND) 103820555Sdavidnu_char * 103920590Sdavidnpw_getrand(u_char *buf, int len) /* cryptographically secure rng */ 104020555Sdavidn{ 104120590Sdavidn int i; 104220590Sdavidn for (i=0;i<len;i+=16) { 104320590Sdavidn u_char ubuf[16]; 104420590Sdavidn 104520590Sdavidn MD5_CTX md5_ctx; 104620590Sdavidn struct timeval tv, tvo; 104720590Sdavidn struct rusage ru; 104820590Sdavidn int n=0; 104920590Sdavidn int t; 105020590Sdavidn 105120590Sdavidn MD5Init (&md5_ctx); 105220590Sdavidn t=getpid(); 105320590Sdavidn MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 105420590Sdavidn t=getppid(); 105520590Sdavidn MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 105620590Sdavidn gettimeofday (&tvo, NULL); 105720590Sdavidn do { 105820590Sdavidn getrusage (RUSAGE_SELF, &ru); 105920590Sdavidn MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru); 106020590Sdavidn gettimeofday (&tv, NULL); 106120590Sdavidn MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv); 106220590Sdavidn } while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000); 106320590Sdavidn MD5Final (ubuf, &md5_ctx); 106482047Sjoerg memcpy(buf+i, ubuf, MIN(16, len-i)); 106520590Sdavidn } 106620590Sdavidn return buf; 106720555Sdavidn} 106820253Sjoerg 106920590Sdavidn#else /* Portable version */ 107020590Sdavidn 107120590Sdavidnstatic u_char * 107220590Sdavidnpw_getrand(u_char *buf, int len) 107320590Sdavidn{ 107420590Sdavidn int i; 107520590Sdavidn 107620590Sdavidn for (i = 0; i < len; i++) { 1077110667Sache unsigned long val = arc4random(); 107820590Sdavidn /* Use all bits in the random value */ 107920590Sdavidn buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val); 108020590Sdavidn } 108120590Sdavidn return buf; 108220590Sdavidn} 108320590Sdavidn 108420590Sdavidn#endif 108520590Sdavidn 108620253Sjoergstatic char * 108720253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user) 108820253Sjoerg{ 108920253Sjoerg int i, l; 109020253Sjoerg char pwbuf[32]; 109120555Sdavidn u_char rndbuf[sizeof pwbuf]; 109220253Sjoerg 109320253Sjoerg switch (cnf->default_password) { 109420253Sjoerg case -1: /* Random password */ 109573563Skris l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 109620555Sdavidn pw_getrand(rndbuf, l); 109720253Sjoerg for (i = 0; i < l; i++) 109842245Sbillf pwbuf[i] = chars[rndbuf[i] % (sizeof(chars)-1)]; 109920253Sjoerg pwbuf[i] = '\0'; 110020253Sjoerg 110120253Sjoerg /* 110220253Sjoerg * We give this information back to the user 110320253Sjoerg */ 1104124382Siedowse if (getarg(args, 'h') == NULL && getarg(args, 'H') == NULL && 1105124382Siedowse getarg(args, 'N') == NULL) { 110661957Sache if (isatty(STDOUT_FILENO)) 110720712Sdavidn printf("Password for '%s' is: ", user); 110820253Sjoerg printf("%s\n", pwbuf); 110920253Sjoerg fflush(stdout); 111020253Sjoerg } 111120253Sjoerg break; 111220253Sjoerg 111320253Sjoerg case -2: /* No password at all! */ 111420253Sjoerg return ""; 111520253Sjoerg 111620253Sjoerg case 0: /* No login - default */ 111720253Sjoerg default: 111820253Sjoerg return "*"; 111920253Sjoerg 112020253Sjoerg case 1: /* user's name */ 112120253Sjoerg strncpy(pwbuf, user, sizeof pwbuf); 112220253Sjoerg pwbuf[sizeof pwbuf - 1] = '\0'; 112320253Sjoerg break; 112420253Sjoerg } 112520253Sjoerg return pw_pwcrypt(pwbuf); 112620253Sjoerg} 112720253Sjoerg 112820253Sjoerg 112920253Sjoergstatic int 113044386Sdavidnprint_user(struct passwd * pwd, int pretty, int v7) 113120253Sjoerg{ 113220253Sjoerg if (!pretty) { 113320253Sjoerg char buf[_UC_MAXLINE]; 113420253Sjoerg 113544386Sdavidn fmtpwentry(buf, pwd, v7 ? PWF_PASSWD : PWF_STANDARD); 113620253Sjoerg fputs(buf, stdout); 113720253Sjoerg } else { 113820267Sjoerg int j; 113920253Sjoerg char *p; 114044229Sdavidn struct group *grp = GETGRGID(pwd->pw_gid); 114120253Sjoerg char uname[60] = "User &", office[60] = "[None]", 114220253Sjoerg wphone[60] = "[None]", hphone[60] = "[None]"; 114320590Sdavidn char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 114420590Sdavidn struct tm * tptr; 114520253Sjoerg 114620253Sjoerg if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 114720253Sjoerg strncpy(uname, p, sizeof uname); 114820253Sjoerg uname[sizeof uname - 1] = '\0'; 114920253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 115020253Sjoerg strncpy(office, p, sizeof office); 115120253Sjoerg office[sizeof office - 1] = '\0'; 115220253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 115320253Sjoerg strncpy(wphone, p, sizeof wphone); 115420253Sjoerg wphone[sizeof wphone - 1] = '\0'; 115520253Sjoerg if ((p = strtok(NULL, "")) != NULL) { 115620253Sjoerg strncpy(hphone, p, sizeof hphone); 115720253Sjoerg hphone[sizeof hphone - 1] = '\0'; 115820253Sjoerg } 115920253Sjoerg } 116020253Sjoerg } 116120253Sjoerg } 116220253Sjoerg /* 116320253Sjoerg * Handle '&' in gecos field 116420253Sjoerg */ 116520253Sjoerg if ((p = strchr(uname, '&')) != NULL) { 116620253Sjoerg int l = strlen(pwd->pw_name); 116720253Sjoerg int m = strlen(p); 116820253Sjoerg 116920253Sjoerg memmove(p + l, p + 1, m); 117020253Sjoerg memmove(p, pwd->pw_name, l); 117161957Sache *p = (char) toupper((unsigned char)*p); 117220253Sjoerg } 117320590Sdavidn if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 117474569Sache strftime(acexpire, sizeof acexpire, "%c", tptr); 117561957Sache if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 117674569Sache strftime(pwexpire, sizeof pwexpire, "%c", tptr); 117722394Sdavidn printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n" 117820747Sdavidn " Full Name: %s\n" 117920747Sdavidn " Home: %-26.26s Class: %s\n" 118020747Sdavidn " Shell: %-26.26s Office: %s\n" 118120747Sdavidn "Work Phone: %-26.26s Home Phone: %s\n" 118220747Sdavidn "Acc Expire: %-26.26s Pwd Expire: %s\n", 118320253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 118420253Sjoerg grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 118520253Sjoerg uname, pwd->pw_dir, pwd->pw_class, 118620590Sdavidn pwd->pw_shell, office, wphone, hphone, 118720590Sdavidn acexpire, pwexpire); 118844229Sdavidn SETGRENT(); 118920267Sjoerg j = 0; 119044229Sdavidn while ((grp=GETGRENT()) != NULL) 119120267Sjoerg { 119220267Sjoerg int i = 0; 119320747Sdavidn while (grp->gr_mem[i] != NULL) 119420267Sjoerg { 119520267Sjoerg if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 119620267Sjoerg { 119720747Sdavidn printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 119820267Sjoerg break; 119920267Sjoerg } 120020267Sjoerg ++i; 120120267Sjoerg } 120220267Sjoerg } 120344229Sdavidn ENDGRENT(); 120461957Sache printf("%s", j ? "\n" : ""); 120520253Sjoerg } 120620267Sjoerg return EXIT_SUCCESS; 120720253Sjoerg} 120820253Sjoerg 120920679Sdavidnchar * 121020679Sdavidnpw_checkname(u_char *name, int gecos) 121120253Sjoerg{ 1212109961Sgad char showch[8]; 1213109961Sgad u_char const *badchars, *ch, *showtype; 1214109961Sgad int reject; 121520253Sjoerg 1216109961Sgad ch = name; 1217109961Sgad reject = 0; 1218109961Sgad if (gecos) { 1219109961Sgad /* See if the name is valid as a gecos (comment) field. */ 1220109961Sgad badchars = ":!@"; 1221109961Sgad showtype = "gecos field"; 1222109961Sgad } else { 1223109961Sgad /* See if the name is valid as a userid or group. */ 1224109961Sgad badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\""; 1225109961Sgad showtype = "userid/group name"; 1226109961Sgad /* Userids and groups can not have a leading '-'. */ 1227109961Sgad if (*ch == '-') 1228109961Sgad reject = 1; 122920253Sjoerg } 1230109961Sgad if (!reject) { 1231109961Sgad while (*ch) { 1232109961Sgad if (strchr(badchars, *ch) != NULL || *ch < ' ' || 1233109961Sgad *ch == 127) { 1234109961Sgad reject = 1; 1235109961Sgad break; 1236109961Sgad } 1237109961Sgad /* 8-bit characters are only allowed in GECOS fields */ 1238109961Sgad if (!gecos && (*ch & 0x80)) { 1239109961Sgad reject = 1; 1240109961Sgad break; 1241109961Sgad } 1242109961Sgad ch++; 1243109961Sgad } 1244109961Sgad } 1245109961Sgad /* 1246109961Sgad * A `$' is allowed as the final character for userids and groups, 1247109961Sgad * mainly for the benefit of samba. 1248109961Sgad */ 1249109961Sgad if (reject && !gecos) { 1250109961Sgad if (*ch == '$' && *(ch + 1) == '\0') { 1251109961Sgad reject = 0; 1252109961Sgad ch++; 1253109961Sgad } 1254109961Sgad } 1255109961Sgad if (reject) { 1256109961Sgad snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127) 1257109961Sgad ? "`%c'" : "0x%02x", *ch); 1258109961Sgad errx(EX_DATAERR, "invalid character %s at position %d in %s", 1259109961Sgad showch, (ch - name), showtype); 1260109961Sgad } 1261109961Sgad if (!gecos && (ch - name) > LOGNAMESIZE) 1262109961Sgad errx(EX_DATAERR, "name too long `%s' (max is %d)", name, 1263109961Sgad LOGNAMESIZE); 126420679Sdavidn return (char *)name; 126520253Sjoerg} 126620253Sjoerg 126720253Sjoerg 126820253Sjoergstatic void 126920253Sjoergrmat(uid_t uid) 127020253Sjoerg{ 127120253Sjoerg DIR *d = opendir("/var/at/jobs"); 127220253Sjoerg 127320253Sjoerg if (d != NULL) { 127420253Sjoerg struct dirent *e; 127520253Sjoerg 127620253Sjoerg while ((e = readdir(d)) != NULL) { 127720253Sjoerg struct stat st; 127820253Sjoerg 127920253Sjoerg if (strncmp(e->d_name, ".lock", 5) != 0 && 128020253Sjoerg stat(e->d_name, &st) == 0 && 128120253Sjoerg !S_ISDIR(st.st_mode) && 128220253Sjoerg st.st_uid == uid) { 128320253Sjoerg char tmp[MAXPATHLEN]; 128420253Sjoerg 128520253Sjoerg sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 128620253Sjoerg system(tmp); 128720253Sjoerg } 128820253Sjoerg } 128920253Sjoerg closedir(d); 129020253Sjoerg } 129120253Sjoerg} 129220747Sdavidn 129320747Sdavidnstatic void 129485145Sachermopie(char const * name) 129520747Sdavidn{ 129685145Sache static const char etcopie[] = "/etc/opiekeys"; 129785145Sache FILE *fp = fopen(etcopie, "r+"); 129820747Sdavidn 129921052Sdavidn if (fp != NULL) { 130021052Sdavidn char tmp[1024]; 130121052Sdavidn off_t atofs = 0; 130221052Sdavidn int length = strlen(name); 130320747Sdavidn 130421052Sdavidn while (fgets(tmp, sizeof tmp, fp) != NULL) { 130521052Sdavidn if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 130621052Sdavidn if (fseek(fp, atofs, SEEK_SET) == 0) { 130721052Sdavidn fwrite("#", 1, 1, fp); /* Comment username out */ 130821052Sdavidn } 130921052Sdavidn break; 131020747Sdavidn } 131121052Sdavidn atofs = ftell(fp); 131220747Sdavidn } 131321052Sdavidn /* 131421052Sdavidn * If we got an error of any sort, don't update! 131521052Sdavidn */ 131621052Sdavidn fclose(fp); 131720747Sdavidn } 131820747Sdavidn} 131920747Sdavidn 1320