pw_user.c revision 82868
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 82868 2001-09-03 14:12:42Z dd $"; 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); 7020747Sdavidnstatic void rmskey(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 8920253Sjoerg * -F force print or add 9020253Sjoerg * Setting defaults: 9120253Sjoerg * -D set user defaults 9220253Sjoerg * -b dir default home root dir 9320253Sjoerg * -e period default expiry period 9420253Sjoerg * -p period default password change period 9520253Sjoerg * -g group default group 9620253Sjoerg * -G grp1,grp2.. default additional groups 9720253Sjoerg * -L class default login class 9820253Sjoerg * -k dir default home skeleton 9920253Sjoerg * -s shell default shell 10020253Sjoerg * -w method default password method 10120253Sjoerg */ 10220253Sjoerg 10320253Sjoergint 10420253Sjoergpw_user(struct userconf * cnf, int mode, struct cargs * args) 10520253Sjoerg{ 10652527Sdavidn int rc, edited = 0; 10720253Sjoerg char *p = NULL; 10852512Sdavidn char *passtmp; 10920253Sjoerg struct carg *a_name; 11020253Sjoerg struct carg *a_uid; 11120253Sjoerg struct carg *arg; 11220253Sjoerg struct passwd *pwd = NULL; 11320253Sjoerg struct group *grp; 11420253Sjoerg struct stat st; 11520747Sdavidn char line[_PASSWORD_LEN+1]; 11682868Sdd FILE *fp; 11720253Sjoerg 11820253Sjoerg static struct passwd fakeuser = 11920253Sjoerg { 12020253Sjoerg NULL, 12120253Sjoerg "*", 12220253Sjoerg -1, 12320253Sjoerg -1, 12420253Sjoerg 0, 12520253Sjoerg "", 12620253Sjoerg "User &", 12756000Sdavidn "/nonexistent", 12820253Sjoerg "/bin/sh", 12920253Sjoerg 0 13056000Sdavidn#if defined(__FreeBSD__) 13156000Sdavidn ,0 13256000Sdavidn#endif 13320253Sjoerg }; 13420253Sjoerg 13552512Sdavidn 13620253Sjoerg /* 13720267Sjoerg * With M_NEXT, we only need to return the 13820267Sjoerg * next uid to stdout 13920267Sjoerg */ 14020267Sjoerg if (mode == M_NEXT) 14120267Sjoerg { 14220267Sjoerg uid_t next = pw_uidpolicy(cnf, args); 14320267Sjoerg if (getarg(args, 'q')) 14420267Sjoerg return next; 14520267Sjoerg printf("%ld:", (long)next); 14620267Sjoerg pw_group(cnf, mode, args); 14720267Sjoerg return EXIT_SUCCESS; 14820267Sjoerg } 14920267Sjoerg 15020267Sjoerg /* 15120253Sjoerg * We can do all of the common legwork here 15220253Sjoerg */ 15320253Sjoerg 15420253Sjoerg if ((arg = getarg(args, 'b')) != NULL) { 15520267Sjoerg cnf->home = arg->val; 15620253Sjoerg } 15721052Sdavidn 15821052Sdavidn /* 15921052Sdavidn * If we'll need to use it or we're updating it, 16021052Sdavidn * then create the base home directory if necessary 16121052Sdavidn */ 16221052Sdavidn if (arg != NULL || getarg(args, 'm') != NULL) { 16321052Sdavidn int l = strlen(cnf->home); 16421052Sdavidn 16521052Sdavidn if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 16621052Sdavidn cnf->home[--l] = '\0'; 16721052Sdavidn 16821052Sdavidn if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 16930259Scharnier errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); 17021052Sdavidn 17121052Sdavidn if (stat(cnf->home, &st) == -1) { 17221052Sdavidn char dbuf[MAXPATHLEN]; 17321052Sdavidn 17421242Sdavidn /* 17521242Sdavidn * This is a kludge especially for Joerg :) 17621242Sdavidn * If the home directory would be created in the root partition, then 17721242Sdavidn * we really create it under /usr which is likely to have more space. 17821242Sdavidn * But we create a symlink from cnf->home -> "/usr" -> cnf->home 17921242Sdavidn */ 18021242Sdavidn if (strchr(cnf->home+1, '/') == NULL) { 18121242Sdavidn strcpy(dbuf, "/usr"); 18221242Sdavidn strncat(dbuf, cnf->home, MAXPATHLEN-5); 18321242Sdavidn if (mkdir(dbuf, 0755) != -1 || errno == EEXIST) { 18421242Sdavidn chown(dbuf, 0, 0); 18521242Sdavidn symlink(dbuf, cnf->home); 18621242Sdavidn } 18721242Sdavidn /* If this falls, fall back to old method */ 18821242Sdavidn } 18921052Sdavidn p = strncpy(dbuf, cnf->home, sizeof dbuf); 19021052Sdavidn dbuf[MAXPATHLEN-1] = '\0'; 19121242Sdavidn if (stat(dbuf, &st) == -1) { 19221242Sdavidn while ((p = strchr(++p, '/')) != NULL) { 19321242Sdavidn *p = '\0'; 19421242Sdavidn if (stat(dbuf, &st) == -1) { 19521242Sdavidn if (mkdir(dbuf, 0755) == -1) 19621242Sdavidn goto direrr; 19721242Sdavidn chown(dbuf, 0, 0); 19821242Sdavidn } else if (!S_ISDIR(st.st_mode)) 19930259Scharnier errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); 20021242Sdavidn *p = '/'; 20121242Sdavidn } 20221052Sdavidn } 20321242Sdavidn if (stat(dbuf, &st) == -1) { 20421052Sdavidn if (mkdir(dbuf, 0755) == -1) { 20530259Scharnier direrr: err(EX_OSFILE, "mkdir '%s'", dbuf); 20621052Sdavidn } 20721052Sdavidn chown(dbuf, 0, 0); 20821052Sdavidn } 20921052Sdavidn } else if (!S_ISDIR(st.st_mode)) 21030259Scharnier errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); 21121052Sdavidn } 21221052Sdavidn 21320253Sjoerg if ((arg = getarg(args, 'e')) != NULL) 21420253Sjoerg cnf->expire_days = atoi(arg->val); 21520253Sjoerg 21621330Sdavidn if ((arg = getarg(args, 'y')) != NULL) 21721330Sdavidn cnf->nispasswd = arg->val; 21821330Sdavidn 21920253Sjoerg if ((arg = getarg(args, 'p')) != NULL && arg->val) 22020253Sjoerg cnf->password_days = atoi(arg->val); 22120253Sjoerg 22220253Sjoerg if ((arg = getarg(args, 'g')) != NULL) { 22363596Sdavidn if (!*(p = arg->val)) /* Handle empty group list specially */ 22463596Sdavidn cnf->default_group = ""; 22563596Sdavidn else { 22663596Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 22763596Sdavidn if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 22863596Sdavidn errx(EX_NOUSER, "group `%s' does not exist", p); 22963596Sdavidn } 23063596Sdavidn cnf->default_group = newstr(grp->gr_name); 23120253Sjoerg } 23220253Sjoerg } 23320253Sjoerg if ((arg = getarg(args, 'L')) != NULL) 23420679Sdavidn cnf->default_class = pw_checkname((u_char *)arg->val, 0); 23520253Sjoerg 23620253Sjoerg if ((arg = getarg(args, 'G')) != NULL && arg->val) { 23752527Sdavidn int i = 0; 23820253Sjoerg 23920747Sdavidn for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 24044229Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 24161957Sache if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 24230259Scharnier errx(EX_NOUSER, "group `%s' does not exist", p); 24320253Sjoerg } 24420747Sdavidn if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 24520747Sdavidn cnf->groups[i++] = newstr(grp->gr_name); 24620253Sjoerg } 24720747Sdavidn while (i < cnf->numgroups) 24820253Sjoerg cnf->groups[i++] = NULL; 24920253Sjoerg } 25052527Sdavidn 25120253Sjoerg if ((arg = getarg(args, 'k')) != NULL) { 25226088Sdavidn if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode)) 25330259Scharnier errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir); 25420253Sjoerg } 25552527Sdavidn 25620253Sjoerg if ((arg = getarg(args, 's')) != NULL) 25720253Sjoerg cnf->shell_default = arg->val; 25820253Sjoerg 25963600Sdavidn if ((arg = getarg(args, 'w')) != NULL) 26063600Sdavidn cnf->default_password = boolean_val(arg->val, cnf->default_password); 26120253Sjoerg if (mode == M_ADD && getarg(args, 'D')) { 26220253Sjoerg if (getarg(args, 'n') != NULL) 26330259Scharnier errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 26420253Sjoerg if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 26520253Sjoerg if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 26620253Sjoerg cnf->min_uid = 1000; 26720253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 26820253Sjoerg cnf->max_uid = 32000; 26920253Sjoerg } 27020253Sjoerg if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 27120253Sjoerg if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 27220253Sjoerg cnf->min_gid = 1000; 27320253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 27420253Sjoerg cnf->max_gid = 32000; 27520253Sjoerg } 27620253Sjoerg 27720253Sjoerg arg = getarg(args, 'C'); 27820253Sjoerg if (write_userconfig(arg ? arg->val : NULL)) 27920267Sjoerg return EXIT_SUCCESS; 28030259Scharnier warn("config update"); 28120267Sjoerg return EX_IOERR; 28220253Sjoerg } 28352527Sdavidn 28420253Sjoerg if (mode == M_PRINT && getarg(args, 'a')) { 28520267Sjoerg int pretty = getarg(args, 'P') != NULL; 28644386Sdavidn int v7 = getarg(args, '7') != NULL; 28720253Sjoerg 28844229Sdavidn SETPWENT(); 28944229Sdavidn while ((pwd = GETPWENT()) != NULL) 29044386Sdavidn print_user(pwd, pretty, v7); 29144229Sdavidn ENDPWENT(); 29220267Sjoerg return EXIT_SUCCESS; 29320253Sjoerg } 29452527Sdavidn 29520253Sjoerg if ((a_name = getarg(args, 'n')) != NULL) 29644229Sdavidn pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0)); 29720253Sjoerg a_uid = getarg(args, 'u'); 29820253Sjoerg 29920253Sjoerg if (a_uid == NULL) { 30020253Sjoerg if (a_name == NULL) 30130259Scharnier errx(EX_DATAERR, "user name or id required"); 30220253Sjoerg 30320253Sjoerg /* 30420253Sjoerg * Determine whether 'n' switch is name or uid - we don't 30520253Sjoerg * really don't really care which we have, but we need to 30620253Sjoerg * know. 30720253Sjoerg */ 30843780Sdes if (mode != M_ADD && pwd == NULL 30943780Sdes && strspn(a_name->val, "0123456789") == strlen(a_name->val) 31043780Sdes && atoi(a_name->val) > 0) { /* Assume uid */ 31120253Sjoerg (a_uid = a_name)->ch = 'u'; 31220253Sjoerg a_name = NULL; 31320253Sjoerg } 31420253Sjoerg } 31552527Sdavidn 31620253Sjoerg /* 31720253Sjoerg * Update, delete & print require that the user exists 31820253Sjoerg */ 31952512Sdavidn if (mode == M_UPDATE || mode == M_DELETE || 32052512Sdavidn mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) { 32152527Sdavidn 32220253Sjoerg if (a_name == NULL && pwd == NULL) /* Try harder */ 32344229Sdavidn pwd = GETPWUID(atoi(a_uid->val)); 32420253Sjoerg 32520253Sjoerg if (pwd == NULL) { 32620253Sjoerg if (mode == M_PRINT && getarg(args, 'F')) { 32720253Sjoerg fakeuser.pw_name = a_name ? a_name->val : "nouser"; 32820253Sjoerg fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 32944386Sdavidn return print_user(&fakeuser, 33044386Sdavidn getarg(args, 'P') != NULL, 33144386Sdavidn getarg(args, '7') != NULL); 33220253Sjoerg } 33320253Sjoerg if (a_name == NULL) 33430259Scharnier errx(EX_NOUSER, "no such uid `%s'", a_uid->val); 33530259Scharnier errx(EX_NOUSER, "no such user `%s'", a_name->val); 33620253Sjoerg } 33752527Sdavidn 33820253Sjoerg if (a_name == NULL) /* May be needed later */ 33920253Sjoerg a_name = addarg(args, 'n', newstr(pwd->pw_name)); 34020253Sjoerg 34120253Sjoerg /* 34252512Sdavidn * The M_LOCK and M_UNLOCK functions simply add or remove 34352512Sdavidn * a "*LOCKED*" prefix from in front of the password to 34452512Sdavidn * prevent it decoding correctly, and therefore prevents 34552512Sdavidn * access. Of course, this only prevents access via 34652512Sdavidn * password authentication (not ssh, kerberos or any 34752512Sdavidn * other method that does not use the UNIX password) but 34852512Sdavidn * that is a known limitation. 34952512Sdavidn */ 35052512Sdavidn 35152512Sdavidn if (mode == M_LOCK) { 35252512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0) 35352512Sdavidn errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 35452512Sdavidn passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str)); 35552512Sdavidn if (passtmp == NULL) /* disaster */ 35652512Sdavidn errx(EX_UNAVAILABLE, "out of memory"); 35752512Sdavidn strcpy(passtmp, locked_str); 35852512Sdavidn strcat(passtmp, pwd->pw_passwd); 35952512Sdavidn pwd->pw_passwd = passtmp; 36052527Sdavidn edited = 1; 36152512Sdavidn } else if (mode == M_UNLOCK) { 36252512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0) 36352512Sdavidn errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 36452512Sdavidn pwd->pw_passwd += sizeof(locked_str)-1; 36552527Sdavidn edited = 1; 36652527Sdavidn } else if (mode == M_DELETE) { 36752527Sdavidn /* 36852527Sdavidn * Handle deletions now 36952527Sdavidn */ 37020253Sjoerg char file[MAXPATHLEN]; 37120253Sjoerg char home[MAXPATHLEN]; 37220253Sjoerg uid_t uid = pwd->pw_uid; 37320253Sjoerg 37420253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 37530259Scharnier errx(EX_DATAERR, "cannot remove user 'root'"); 37620253Sjoerg 37744229Sdavidn if (!PWALTDIR()) { 37844229Sdavidn /* 37944229Sdavidn * Remove skey record from /etc/skeykeys 38044229Sdavidn */ 38120747Sdavidn 38244229Sdavidn rmskey(pwd->pw_name); 38320747Sdavidn 38444229Sdavidn /* 38544229Sdavidn * Remove crontabs 38644229Sdavidn */ 38744229Sdavidn sprintf(file, "/var/cron/tabs/%s", pwd->pw_name); 38844229Sdavidn if (access(file, F_OK) == 0) { 38944229Sdavidn sprintf(file, "crontab -u %s -r", pwd->pw_name); 39044229Sdavidn system(file); 39144229Sdavidn } 39220253Sjoerg } 39320253Sjoerg /* 39420253Sjoerg * Save these for later, since contents of pwd may be 39520253Sjoerg * invalidated by deletion 39620253Sjoerg */ 39720253Sjoerg sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 39820253Sjoerg strncpy(home, pwd->pw_dir, sizeof home); 39920253Sjoerg home[sizeof home - 1] = '\0'; 40020253Sjoerg 40152502Sdavidn rc = delpwent(pwd); 40252502Sdavidn if (rc == -1) 40352502Sdavidn err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 40452502Sdavidn else if (rc != 0) { 40556000Sdavidn warn("passwd update"); 40652502Sdavidn return EX_IOERR; 40752502Sdavidn } 40821330Sdavidn 40952502Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/') { 41052502Sdavidn rc = delnispwent(cnf->nispasswd, a_name->val); 41152502Sdavidn if (rc == -1) 41252502Sdavidn warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name); 41352502Sdavidn else if (rc != 0) 41456000Sdavidn warn("WARNING: NIS passwd update"); 41552502Sdavidn /* non-fatal */ 41652502Sdavidn } 41752502Sdavidn 41820253Sjoerg editgroups(a_name->val, NULL); 41920253Sjoerg 42020253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 42120253Sjoerg 42244229Sdavidn if (!PWALTDIR()) { 42344229Sdavidn /* 42444229Sdavidn * Remove mail file 42544229Sdavidn */ 42644229Sdavidn remove(file); 42720253Sjoerg 42844229Sdavidn /* 42944229Sdavidn * Remove at jobs 43044229Sdavidn */ 43144229Sdavidn if (getpwuid(uid) == NULL) 43244229Sdavidn rmat(uid); 43320253Sjoerg 43444229Sdavidn /* 43544229Sdavidn * Remove home directory and contents 43644229Sdavidn */ 43744229Sdavidn if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 43844229Sdavidn if (stat(home, &st) != -1) { 43944229Sdavidn rm_r(home, uid); 44044229Sdavidn pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 44144229Sdavidn a_name->val, (long) uid, home, 44244229Sdavidn stat(home, &st) == -1 ? "" : "not completely "); 44344229Sdavidn } 44420253Sjoerg } 44520253Sjoerg } 44620267Sjoerg return EXIT_SUCCESS; 44720253Sjoerg } else if (mode == M_PRINT) 44844386Sdavidn return print_user(pwd, 44944386Sdavidn getarg(args, 'P') != NULL, 45044386Sdavidn getarg(args, '7') != NULL); 45120253Sjoerg 45220253Sjoerg /* 45320253Sjoerg * The rest is edit code 45420253Sjoerg */ 45520253Sjoerg if ((arg = getarg(args, 'l')) != NULL) { 45620253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 45730259Scharnier errx(EX_DATAERR, "can't rename `root' account"); 45820679Sdavidn pwd->pw_name = pw_checkname((u_char *)arg->val, 0); 45952527Sdavidn edited = 1; 46020253Sjoerg } 46152527Sdavidn 46261957Sache if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) { 46320253Sjoerg pwd->pw_uid = (uid_t) atol(arg->val); 46452527Sdavidn edited = 1; 46520253Sjoerg if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 46630259Scharnier errx(EX_DATAERR, "can't change uid of `root' account"); 46720253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 46830259Scharnier warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 46920253Sjoerg } 47020253Sjoerg 47152527Sdavidn if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */ 47252527Sdavidn gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid; 47352527Sdavidn if (newgid != pwd->pw_gid) { 47452527Sdavidn edited = 1; 47561762Sdavidn pwd->pw_gid = newgid; 47652527Sdavidn } 47752527Sdavidn } 47852527Sdavidn 47920253Sjoerg if ((arg = getarg(args, 'p')) != NULL) { 48052527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 48152527Sdavidn if (pwd->pw_change != 0) { 48252527Sdavidn pwd->pw_change = 0; 48352527Sdavidn edited = 1; 48452527Sdavidn } 48552527Sdavidn } 48620253Sjoerg else { 48720253Sjoerg time_t now = time(NULL); 48820253Sjoerg time_t expire = parse_date(now, arg->val); 48920253Sjoerg 49020253Sjoerg if (now == expire) 49130259Scharnier errx(EX_DATAERR, "invalid password change date `%s'", arg->val); 49252527Sdavidn if (pwd->pw_change != expire) { 49352527Sdavidn pwd->pw_change = expire; 49452527Sdavidn edited = 1; 49552527Sdavidn } 49620253Sjoerg } 49720253Sjoerg } 49852527Sdavidn 49920267Sjoerg if ((arg = getarg(args, 'e')) != NULL) { 50052527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 50152527Sdavidn if (pwd->pw_expire != 0) { 50252527Sdavidn pwd->pw_expire = 0; 50352527Sdavidn edited = 1; 50452527Sdavidn } 50552527Sdavidn } 50620253Sjoerg else { 50720253Sjoerg time_t now = time(NULL); 50820253Sjoerg time_t expire = parse_date(now, arg->val); 50920253Sjoerg 51020253Sjoerg if (now == expire) 51130259Scharnier errx(EX_DATAERR, "invalid account expiry date `%s'", arg->val); 51252527Sdavidn if (pwd->pw_expire != expire) { 51352527Sdavidn pwd->pw_expire = expire; 51452527Sdavidn edited = 1; 51552527Sdavidn } 51620253Sjoerg } 51720253Sjoerg } 51820253Sjoerg 51952527Sdavidn if ((arg = getarg(args, 's')) != NULL) { 52052527Sdavidn char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 52152527Sdavidn if (shell == NULL) 52252527Sdavidn shell = ""; 52352527Sdavidn if (strcmp(shell, pwd->pw_shell) != 0) { 52452527Sdavidn pwd->pw_shell = shell; 52552527Sdavidn edited = 1; 52652527Sdavidn } 52752527Sdavidn } 52820253Sjoerg 52952527Sdavidn if (getarg(args, 'L')) { 53052527Sdavidn if (cnf->default_class == NULL) 53152527Sdavidn cnf->default_class = ""; 53252527Sdavidn if (strcmp(pwd->pw_class, cnf->default_class) != 0) { 53352527Sdavidn pwd->pw_class = cnf->default_class; 53452527Sdavidn edited = 1; 53552527Sdavidn } 53652527Sdavidn } 53752527Sdavidn 53820747Sdavidn if ((arg = getarg(args, 'd')) != NULL) { 53962461Sdavidn edited = strcmp(pwd->pw_dir, arg->val) != 0; 54020747Sdavidn if (stat(pwd->pw_dir = arg->val, &st) == -1) { 54120747Sdavidn if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 54230259Scharnier warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 54320747Sdavidn } else if (!S_ISDIR(st.st_mode)) 54430259Scharnier warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 54520747Sdavidn } 54620747Sdavidn 54752527Sdavidn if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL) { 54864918Sgreen login_cap_t *lc; 54964918Sgreen 55064918Sgreen lc = login_getpwclass(pwd); 55164918Sgreen if (lc == NULL || 55264918Sgreen login_setcryptfmt(lc, "md5", NULL) == NULL) 55364918Sgreen warn("setting crypt(3) format"); 55464918Sgreen login_close(lc); 55520267Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 55652527Sdavidn edited = 1; 55752527Sdavidn } 55820267Sjoerg 55920253Sjoerg } else { 56064918Sgreen login_cap_t *lc; 56152527Sdavidn 56252527Sdavidn /* 56352527Sdavidn * Add code 56452527Sdavidn */ 56552527Sdavidn 56620253Sjoerg if (a_name == NULL) /* Required */ 56730259Scharnier errx(EX_DATAERR, "login name required"); 56844229Sdavidn else if ((pwd = GETPWNAM(a_name->val)) != NULL) /* Exists */ 56930259Scharnier errx(EX_DATAERR, "login name `%s' already exists", a_name->val); 57020253Sjoerg 57120253Sjoerg /* 57220253Sjoerg * Now, set up defaults for a new user 57320253Sjoerg */ 57420253Sjoerg pwd = &fakeuser; 57520253Sjoerg pwd->pw_name = a_name->val; 57620253Sjoerg pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 57720253Sjoerg pwd->pw_uid = pw_uidpolicy(cnf, args); 57820253Sjoerg pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 57920253Sjoerg pwd->pw_change = pw_pwdpolicy(cnf, args); 58020253Sjoerg pwd->pw_expire = pw_exppolicy(cnf, args); 58120253Sjoerg pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 58220253Sjoerg pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 58364918Sgreen lc = login_getpwclass(pwd); 58464918Sgreen if (lc == NULL || login_setcryptfmt(lc, "md5", NULL) == NULL) 58564918Sgreen warn("setting crypt(3) format"); 58664918Sgreen login_close(lc); 58764918Sgreen pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 58852527Sdavidn edited = 1; 58920253Sjoerg 59020253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 59130259Scharnier warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 59220253Sjoerg } 59320253Sjoerg 59420253Sjoerg /* 59520253Sjoerg * Shared add/edit code 59620253Sjoerg */ 59752527Sdavidn if ((arg = getarg(args, 'c')) != NULL) { 59852527Sdavidn char *gecos = pw_checkname((u_char *)arg->val, 1); 59952527Sdavidn if (strcmp(pwd->pw_gecos, gecos) != 0) { 60052527Sdavidn pwd->pw_gecos = gecos; 60152527Sdavidn edited = 1; 60252527Sdavidn } 60352527Sdavidn } 60420253Sjoerg 60520253Sjoerg if ((arg = getarg(args, 'h')) != NULL) { 60663572Sdavidn if (strcmp(arg->val, "-") == 0) { 60763572Sdavidn if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 60863572Sdavidn pwd->pw_passwd = "*"; /* No access */ 60963572Sdavidn edited = 1; 61063572Sdavidn } 61163572Sdavidn } else { 61220253Sjoerg int fd = atoi(arg->val); 61320253Sjoerg int b; 61420253Sjoerg int istty = isatty(fd); 61520253Sjoerg struct termios t; 61664918Sgreen login_cap_t *lc; 61720253Sjoerg 61820253Sjoerg if (istty) { 61920253Sjoerg if (tcgetattr(fd, &t) == -1) 62020253Sjoerg istty = 0; 62120253Sjoerg else { 62220253Sjoerg struct termios n = t; 62320253Sjoerg 62420253Sjoerg /* Disable echo */ 62520253Sjoerg n.c_lflag &= ~(ECHO); 62620253Sjoerg tcsetattr(fd, TCSANOW, &n); 62720253Sjoerg printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name); 62820253Sjoerg fflush(stdout); 62920253Sjoerg } 63020253Sjoerg } 63120253Sjoerg b = read(fd, line, sizeof(line) - 1); 63220253Sjoerg if (istty) { /* Restore state */ 63320253Sjoerg tcsetattr(fd, TCSANOW, &t); 63420253Sjoerg fputc('\n', stdout); 63520253Sjoerg fflush(stdout); 63620253Sjoerg } 63720253Sjoerg if (b < 0) { 63830259Scharnier warn("-h file descriptor"); 63920267Sjoerg return EX_IOERR; 64020253Sjoerg } 64120253Sjoerg line[b] = '\0'; 64220253Sjoerg if ((p = strpbrk(line, " \t\r\n")) != NULL) 64320253Sjoerg *p = '\0'; 64420253Sjoerg if (!*line) 64530259Scharnier errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 64664918Sgreen lc = login_getpwclass(pwd); 64764918Sgreen if (lc == NULL || 64864918Sgreen login_setcryptfmt(lc, "md5", NULL) == NULL) 64964918Sgreen warn("setting crypt(3) format"); 65064918Sgreen login_close(lc); 65120253Sjoerg pwd->pw_passwd = pw_pwcrypt(line); 65252527Sdavidn edited = 1; 65320253Sjoerg } 65420253Sjoerg } 65520267Sjoerg 65620267Sjoerg /* 65720267Sjoerg * Special case: -N only displays & exits 65820267Sjoerg */ 65920267Sjoerg if (getarg(args, 'N') != NULL) 66044386Sdavidn return print_user(pwd, 66144386Sdavidn getarg(args, 'P') != NULL, 66244386Sdavidn getarg(args, '7') != NULL); 66320267Sjoerg 66421330Sdavidn if (mode == M_ADD) { 66552527Sdavidn edited = 1; /* Always */ 66652502Sdavidn rc = addpwent(pwd); 66752502Sdavidn if (rc == -1) { 66852502Sdavidn warnx("user '%s' already exists", pwd->pw_name); 66952502Sdavidn return EX_IOERR; 67052502Sdavidn } else if (rc != 0) { 67156000Sdavidn warn("passwd file update"); 67252502Sdavidn return EX_IOERR; 67352502Sdavidn } 67452502Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/') { 67552502Sdavidn rc = addnispwent(cnf->nispasswd, pwd); 67652502Sdavidn if (rc == -1) 67752502Sdavidn warnx("User '%s' already exists in NIS passwd", pwd->pw_name); 67852502Sdavidn else 67956000Sdavidn warn("NIS passwd update"); 68052502Sdavidn /* NOTE: we treat NIS-only update errors as non-fatal */ 68152502Sdavidn } 68252512Sdavidn } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) { 68352527Sdavidn if (edited) { /* Only updated this if required */ 68452527Sdavidn rc = chgpwent(a_name->val, pwd); 68552527Sdavidn if (rc == -1) { 68652527Sdavidn warnx("user '%s' does not exist (NIS?)", pwd->pw_name); 68752527Sdavidn return EX_IOERR; 68852527Sdavidn } else if (rc != 0) { 68956000Sdavidn warn("passwd file update"); 69052527Sdavidn return EX_IOERR; 69152527Sdavidn } 69252527Sdavidn if ( cnf->nispasswd && *cnf->nispasswd=='/') { 69352527Sdavidn rc = chgnispwent(cnf->nispasswd, a_name->val, pwd); 69452527Sdavidn if (rc == -1) 69552527Sdavidn warn("User '%s' not found in NIS passwd", pwd->pw_name); 69652527Sdavidn else 69756000Sdavidn warn("NIS passwd update"); 69852527Sdavidn /* NOTE: NIS-only update errors are not fatal */ 69952527Sdavidn } 70052502Sdavidn } 70121330Sdavidn } 70221330Sdavidn 70320253Sjoerg /* 70420253Sjoerg * Ok, user is created or changed - now edit group file 70520253Sjoerg */ 70620253Sjoerg 70720253Sjoerg if (mode == M_ADD || getarg(args, 'G') != NULL) 70820253Sjoerg editgroups(pwd->pw_name, cnf->groups); 70920253Sjoerg 71061759Sdavidn /* go get a current version of pwd */ 71161759Sdavidn pwd = GETPWNAM(a_name->val); 71261759Sdavidn if (pwd == NULL) { 71361759Sdavidn /* This will fail when we rename, so special case that */ 71461759Sdavidn if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) { 71561759Sdavidn a_name->val = arg->val; /* update new name */ 71661759Sdavidn pwd = GETPWNAM(a_name->val); /* refetch renamed rec */ 71761759Sdavidn } 71861759Sdavidn } 71961759Sdavidn if (pwd == NULL) /* can't go on without this */ 72030259Scharnier errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val); 72120253Sjoerg 72244229Sdavidn grp = GETGRGID(pwd->pw_gid); 72320253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s", 72420253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 72520253Sjoerg grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 72620253Sjoerg pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 72720253Sjoerg 72820253Sjoerg /* 72920253Sjoerg * If adding, let's touch and chown the user's mail file. This is not 73020253Sjoerg * strictly necessary under BSD with a 0755 maildir but it also 73120253Sjoerg * doesn't hurt anything to create the empty mailfile 73220253Sjoerg */ 73320253Sjoerg if (mode == M_ADD) { 73444229Sdavidn if (!PWALTDIR()) { 73544229Sdavidn sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 73644229Sdavidn close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 73744229Sdavidn * mtime */ 73844229Sdavidn chown(line, pwd->pw_uid, pwd->pw_gid); 73920253Sjoerg } 74020253Sjoerg } 74152527Sdavidn 74220253Sjoerg /* 74382868Sdd * Let's create and populate the user's home directory. Note 74420253Sjoerg * that this also `works' for editing users if -m is used, but 74520253Sjoerg * existing files will *not* be overwritten. 74620253Sjoerg */ 74744229Sdavidn if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 74820253Sjoerg copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid); 74920253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 75020253Sjoerg pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 75120253Sjoerg } 75252527Sdavidn 75382868Sdd 75482868Sdd /* 75582868Sdd * Finally, send mail to the new user as well, if we are asked to 75682868Sdd */ 75782868Sdd if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 75882868Sdd FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 75982868Sdd 76082868Sdd if (pfp == NULL) 76182868Sdd warn("sendmail"); 76282868Sdd else { 76382868Sdd fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 76482868Sdd while (fgets(line, sizeof(line), fp) != NULL) { 76582868Sdd /* Do substitutions? */ 76682868Sdd fputs(line, pfp); 76782868Sdd } 76882868Sdd pclose(pfp); 76982868Sdd pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 77082868Sdd pwd->pw_name, (long) pwd->pw_uid); 77182868Sdd } 77282868Sdd fclose(fp); 77382868Sdd } 77482868Sdd 77520267Sjoerg return EXIT_SUCCESS; 77620253Sjoerg} 77720253Sjoerg 77820253Sjoerg 77920253Sjoergstatic uid_t 78020253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args) 78120253Sjoerg{ 78220253Sjoerg struct passwd *pwd; 78320253Sjoerg uid_t uid = (uid_t) - 1; 78420253Sjoerg struct carg *a_uid = getarg(args, 'u'); 78520253Sjoerg 78620253Sjoerg /* 78720253Sjoerg * Check the given uid, if any 78820253Sjoerg */ 78920253Sjoerg if (a_uid != NULL) { 79020253Sjoerg uid = (uid_t) atol(a_uid->val); 79120253Sjoerg 79244229Sdavidn if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL) 79330259Scharnier errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid); 79420253Sjoerg } else { 79520253Sjoerg struct bitmap bm; 79620253Sjoerg 79720253Sjoerg /* 79820253Sjoerg * We need to allocate the next available uid under one of 79920253Sjoerg * two policies a) Grab the first unused uid b) Grab the 80020253Sjoerg * highest possible unused uid 80120253Sjoerg */ 80220253Sjoerg if (cnf->min_uid >= cnf->max_uid) { /* Sanity 80320253Sjoerg * claus^H^H^H^Hheck */ 80420253Sjoerg cnf->min_uid = 1000; 80520253Sjoerg cnf->max_uid = 32000; 80620253Sjoerg } 80720253Sjoerg bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 80820253Sjoerg 80920253Sjoerg /* 81020253Sjoerg * Now, let's fill the bitmap from the password file 81120253Sjoerg */ 81244229Sdavidn SETPWENT(); 81344229Sdavidn while ((pwd = GETPWENT()) != NULL) 81444229Sdavidn if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 81520253Sjoerg bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 81644229Sdavidn ENDPWENT(); 81720253Sjoerg 81820253Sjoerg /* 81920253Sjoerg * Then apply the policy, with fallback to reuse if necessary 82020253Sjoerg */ 82120253Sjoerg if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 82220253Sjoerg uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 82320253Sjoerg 82420253Sjoerg /* 82520253Sjoerg * Another sanity check 82620253Sjoerg */ 82720253Sjoerg if (uid < cnf->min_uid || uid > cnf->max_uid) 82830259Scharnier errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 82920253Sjoerg bm_dealloc(&bm); 83020253Sjoerg } 83120253Sjoerg return uid; 83220253Sjoerg} 83320253Sjoerg 83420253Sjoerg 83520253Sjoergstatic uid_t 83620253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 83720253Sjoerg{ 83820253Sjoerg struct group *grp; 83920253Sjoerg gid_t gid = (uid_t) - 1; 84020253Sjoerg struct carg *a_gid = getarg(args, 'g'); 84120253Sjoerg 84220253Sjoerg /* 84320253Sjoerg * If no arg given, see if default can help out 84420253Sjoerg */ 84520253Sjoerg if (a_gid == NULL && cnf->default_group && *cnf->default_group) 84620253Sjoerg a_gid = addarg(args, 'g', cnf->default_group); 84720253Sjoerg 84820253Sjoerg /* 84920253Sjoerg * Check the given gid, if any 85020253Sjoerg */ 85144229Sdavidn SETGRENT(); 85220253Sjoerg if (a_gid != NULL) { 85344229Sdavidn if ((grp = GETGRNAM(a_gid->val)) == NULL) { 85420253Sjoerg gid = (gid_t) atol(a_gid->val); 85561957Sache if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL) 85630259Scharnier errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 85720253Sjoerg } 85820253Sjoerg gid = grp->gr_gid; 85944229Sdavidn } else if ((grp = GETGRNAM(nam)) != NULL && grp->gr_mem[0] == NULL) { 86020267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 86120253Sjoerg } else { 86220253Sjoerg struct cargs grpargs; 86320253Sjoerg char tmp[32]; 86420253Sjoerg 86520253Sjoerg LIST_INIT(&grpargs); 86620253Sjoerg addarg(&grpargs, 'n', nam); 86720253Sjoerg 86820253Sjoerg /* 86920253Sjoerg * We need to auto-create a group with the user's name. We 87020253Sjoerg * can send all the appropriate output to our sister routine 87120253Sjoerg * bit first see if we can create a group with gid==uid so we 87220253Sjoerg * can keep the user and group ids in sync. We purposely do 87320253Sjoerg * NOT check the gid range if we can force the sync. If the 87420253Sjoerg * user's name dups an existing group, then the group add 87520253Sjoerg * function will happily handle that case for us and exit. 87620253Sjoerg */ 87744229Sdavidn if (GETGRGID(prefer) == NULL) { 87820253Sjoerg sprintf(tmp, "%lu", (unsigned long) prefer); 87920253Sjoerg addarg(&grpargs, 'g', tmp); 88020253Sjoerg } 88120267Sjoerg if (getarg(args, 'N')) 88220267Sjoerg { 88320267Sjoerg addarg(&grpargs, 'N', NULL); 88420267Sjoerg addarg(&grpargs, 'q', NULL); 88520267Sjoerg gid = pw_group(cnf, M_NEXT, &grpargs); 88620267Sjoerg } 88720267Sjoerg else 88820267Sjoerg { 88920267Sjoerg pw_group(cnf, M_ADD, &grpargs); 89044229Sdavidn if ((grp = GETGRNAM(nam)) != NULL) 89120267Sjoerg gid = grp->gr_gid; 89220267Sjoerg } 89370486Sben a_gid = LIST_FIRST(&grpargs); 89420253Sjoerg while (a_gid != NULL) { 89570486Sben struct carg *t = LIST_NEXT(a_gid, list); 89620253Sjoerg LIST_REMOVE(a_gid, list); 89720253Sjoerg a_gid = t; 89820253Sjoerg } 89920253Sjoerg } 90044229Sdavidn ENDGRENT(); 90120253Sjoerg return gid; 90220253Sjoerg} 90320253Sjoerg 90420253Sjoerg 90520253Sjoergstatic time_t 90620253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args) 90720253Sjoerg{ 90820253Sjoerg time_t result = 0; 90920253Sjoerg time_t now = time(NULL); 91027831Sdavidn struct carg *arg = getarg(args, 'p'); 91120253Sjoerg 91220253Sjoerg if (arg != NULL) { 91320253Sjoerg if ((result = parse_date(now, arg->val)) == now) 91430259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 91520253Sjoerg } else if (cnf->password_days > 0) 91620253Sjoerg result = now + ((long) cnf->password_days * 86400L); 91720253Sjoerg return result; 91820253Sjoerg} 91920253Sjoerg 92020253Sjoerg 92120253Sjoergstatic time_t 92220253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args) 92320253Sjoerg{ 92420253Sjoerg time_t result = 0; 92520253Sjoerg time_t now = time(NULL); 92620253Sjoerg struct carg *arg = getarg(args, 'e'); 92720253Sjoerg 92820253Sjoerg if (arg != NULL) { 92920253Sjoerg if ((result = parse_date(now, arg->val)) == now) 93030259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 93120253Sjoerg } else if (cnf->expire_days > 0) 93220253Sjoerg result = now + ((long) cnf->expire_days * 86400L); 93320253Sjoerg return result; 93420253Sjoerg} 93520253Sjoerg 93620253Sjoerg 93720253Sjoergstatic char * 93820253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 93920253Sjoerg{ 94020253Sjoerg struct carg *arg = getarg(args, 'd'); 94120253Sjoerg 94220253Sjoerg if (arg) 94320253Sjoerg return arg->val; 94420253Sjoerg else { 94520253Sjoerg static char home[128]; 94620253Sjoerg 94720253Sjoerg if (cnf->home == NULL || *cnf->home == '\0') 94830259Scharnier errx(EX_CONFIG, "no base home directory set"); 94920253Sjoerg sprintf(home, "%s/%s", cnf->home, user); 95020253Sjoerg return home; 95120253Sjoerg } 95220253Sjoerg} 95320253Sjoerg 95420253Sjoergstatic char * 95520253Sjoergshell_path(char const * path, char *shells[], char *sh) 95620253Sjoerg{ 95720253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 95820253Sjoerg return sh; /* specified full path or forced none */ 95920253Sjoerg else { 96020253Sjoerg char *p; 96120253Sjoerg char paths[_UC_MAXLINE]; 96220253Sjoerg 96320253Sjoerg /* 96420253Sjoerg * We need to search paths 96520253Sjoerg */ 96620253Sjoerg strncpy(paths, path, sizeof paths); 96720253Sjoerg paths[sizeof paths - 1] = '\0'; 96820253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 96920253Sjoerg int i; 97020253Sjoerg static char shellpath[256]; 97120253Sjoerg 97220253Sjoerg if (sh != NULL) { 97320253Sjoerg sprintf(shellpath, "%s/%s", p, sh); 97420253Sjoerg if (access(shellpath, X_OK) == 0) 97520253Sjoerg return shellpath; 97620253Sjoerg } else 97720253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 97820253Sjoerg sprintf(shellpath, "%s/%s", p, shells[i]); 97920253Sjoerg if (access(shellpath, X_OK) == 0) 98020253Sjoerg return shellpath; 98120253Sjoerg } 98220253Sjoerg } 98320253Sjoerg if (sh == NULL) 98430259Scharnier errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 98530259Scharnier errx(EX_CONFIG, "no default shell available or defined"); 98620253Sjoerg return NULL; 98720253Sjoerg } 98820253Sjoerg} 98920253Sjoerg 99020253Sjoerg 99120253Sjoergstatic char * 99220253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 99320253Sjoerg{ 99420253Sjoerg char *sh = newshell; 99520253Sjoerg struct carg *arg = getarg(args, 's'); 99620253Sjoerg 99720253Sjoerg if (newshell == NULL && arg != NULL) 99820253Sjoerg sh = arg->val; 99920253Sjoerg return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 100020253Sjoerg} 100120253Sjoerg 100220253Sjoergstatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."; 100320253Sjoerg 100420253Sjoergchar * 100520253Sjoergpw_pwcrypt(char *password) 100620253Sjoerg{ 100720253Sjoerg int i; 100820253Sjoerg char salt[12]; 100920253Sjoerg 101020253Sjoerg static char buf[256]; 101120253Sjoerg 101220253Sjoerg /* 101320253Sjoerg * Calculate a salt value 101420253Sjoerg */ 101520253Sjoerg for (i = 0; i < 8; i++) 101673563Skris salt[i] = chars[arc4random() % 63]; 101720253Sjoerg salt[i] = '\0'; 101820253Sjoerg 101920253Sjoerg return strcpy(buf, crypt(password, salt)); 102020253Sjoerg} 102120253Sjoerg 102220590Sdavidn#if defined(USE_MD5RAND) 102320555Sdavidnu_char * 102420590Sdavidnpw_getrand(u_char *buf, int len) /* cryptographically secure rng */ 102520555Sdavidn{ 102620590Sdavidn int i; 102720590Sdavidn for (i=0;i<len;i+=16) { 102820590Sdavidn u_char ubuf[16]; 102920590Sdavidn 103020590Sdavidn MD5_CTX md5_ctx; 103120590Sdavidn struct timeval tv, tvo; 103220590Sdavidn struct rusage ru; 103320590Sdavidn int n=0; 103420590Sdavidn int t; 103520590Sdavidn 103620590Sdavidn MD5Init (&md5_ctx); 103720590Sdavidn t=getpid(); 103820590Sdavidn MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 103920590Sdavidn t=getppid(); 104020590Sdavidn MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 104120590Sdavidn gettimeofday (&tvo, NULL); 104220590Sdavidn do { 104320590Sdavidn getrusage (RUSAGE_SELF, &ru); 104420590Sdavidn MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru); 104520590Sdavidn gettimeofday (&tv, NULL); 104620590Sdavidn MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv); 104720590Sdavidn } while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000); 104820590Sdavidn MD5Final (ubuf, &md5_ctx); 104982047Sjoerg memcpy(buf+i, ubuf, MIN(16, len-i)); 105020590Sdavidn } 105120590Sdavidn return buf; 105220555Sdavidn} 105320253Sjoerg 105420590Sdavidn#else /* Portable version */ 105520590Sdavidn 105620590Sdavidnstatic u_char * 105720590Sdavidnpw_getrand(u_char *buf, int len) 105820590Sdavidn{ 105920590Sdavidn int i; 106020590Sdavidn 106182059Sjoerg srandomdev(); 106220590Sdavidn for (i = 0; i < len; i++) { 106324214Sache unsigned long val = random(); 106420590Sdavidn /* Use all bits in the random value */ 106520590Sdavidn buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val); 106620590Sdavidn } 106720590Sdavidn return buf; 106820590Sdavidn} 106920590Sdavidn 107020590Sdavidn#endif 107120590Sdavidn 107220253Sjoergstatic char * 107320253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user) 107420253Sjoerg{ 107520253Sjoerg int i, l; 107620253Sjoerg char pwbuf[32]; 107720555Sdavidn u_char rndbuf[sizeof pwbuf]; 107820253Sjoerg 107920253Sjoerg switch (cnf->default_password) { 108020253Sjoerg case -1: /* Random password */ 108173563Skris l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 108220555Sdavidn pw_getrand(rndbuf, l); 108320253Sjoerg for (i = 0; i < l; i++) 108442245Sbillf pwbuf[i] = chars[rndbuf[i] % (sizeof(chars)-1)]; 108520253Sjoerg pwbuf[i] = '\0'; 108620253Sjoerg 108720253Sjoerg /* 108820253Sjoerg * We give this information back to the user 108920253Sjoerg */ 109020267Sjoerg if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) { 109161957Sache if (isatty(STDOUT_FILENO)) 109220712Sdavidn printf("Password for '%s' is: ", user); 109320253Sjoerg printf("%s\n", pwbuf); 109420253Sjoerg fflush(stdout); 109520253Sjoerg } 109620253Sjoerg break; 109720253Sjoerg 109820253Sjoerg case -2: /* No password at all! */ 109920253Sjoerg return ""; 110020253Sjoerg 110120253Sjoerg case 0: /* No login - default */ 110220253Sjoerg default: 110320253Sjoerg return "*"; 110420253Sjoerg 110520253Sjoerg case 1: /* user's name */ 110620253Sjoerg strncpy(pwbuf, user, sizeof pwbuf); 110720253Sjoerg pwbuf[sizeof pwbuf - 1] = '\0'; 110820253Sjoerg break; 110920253Sjoerg } 111020253Sjoerg return pw_pwcrypt(pwbuf); 111120253Sjoerg} 111220253Sjoerg 111320253Sjoerg 111420253Sjoergstatic int 111544386Sdavidnprint_user(struct passwd * pwd, int pretty, int v7) 111620253Sjoerg{ 111720253Sjoerg if (!pretty) { 111820253Sjoerg char buf[_UC_MAXLINE]; 111920253Sjoerg 112044386Sdavidn fmtpwentry(buf, pwd, v7 ? PWF_PASSWD : PWF_STANDARD); 112120253Sjoerg fputs(buf, stdout); 112220253Sjoerg } else { 112320267Sjoerg int j; 112420253Sjoerg char *p; 112544229Sdavidn struct group *grp = GETGRGID(pwd->pw_gid); 112620253Sjoerg char uname[60] = "User &", office[60] = "[None]", 112720253Sjoerg wphone[60] = "[None]", hphone[60] = "[None]"; 112820590Sdavidn char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 112920590Sdavidn struct tm * tptr; 113020253Sjoerg 113120253Sjoerg if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 113220253Sjoerg strncpy(uname, p, sizeof uname); 113320253Sjoerg uname[sizeof uname - 1] = '\0'; 113420253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 113520253Sjoerg strncpy(office, p, sizeof office); 113620253Sjoerg office[sizeof office - 1] = '\0'; 113720253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 113820253Sjoerg strncpy(wphone, p, sizeof wphone); 113920253Sjoerg wphone[sizeof wphone - 1] = '\0'; 114020253Sjoerg if ((p = strtok(NULL, "")) != NULL) { 114120253Sjoerg strncpy(hphone, p, sizeof hphone); 114220253Sjoerg hphone[sizeof hphone - 1] = '\0'; 114320253Sjoerg } 114420253Sjoerg } 114520253Sjoerg } 114620253Sjoerg } 114720253Sjoerg /* 114820253Sjoerg * Handle '&' in gecos field 114920253Sjoerg */ 115020253Sjoerg if ((p = strchr(uname, '&')) != NULL) { 115120253Sjoerg int l = strlen(pwd->pw_name); 115220253Sjoerg int m = strlen(p); 115320253Sjoerg 115420253Sjoerg memmove(p + l, p + 1, m); 115520253Sjoerg memmove(p, pwd->pw_name, l); 115661957Sache *p = (char) toupper((unsigned char)*p); 115720253Sjoerg } 115820590Sdavidn if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 115974569Sache strftime(acexpire, sizeof acexpire, "%c", tptr); 116061957Sache if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 116174569Sache strftime(pwexpire, sizeof pwexpire, "%c", tptr); 116222394Sdavidn printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n" 116320747Sdavidn " Full Name: %s\n" 116420747Sdavidn " Home: %-26.26s Class: %s\n" 116520747Sdavidn " Shell: %-26.26s Office: %s\n" 116620747Sdavidn "Work Phone: %-26.26s Home Phone: %s\n" 116720747Sdavidn "Acc Expire: %-26.26s Pwd Expire: %s\n", 116820253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 116920253Sjoerg grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 117020253Sjoerg uname, pwd->pw_dir, pwd->pw_class, 117120590Sdavidn pwd->pw_shell, office, wphone, hphone, 117220590Sdavidn acexpire, pwexpire); 117344229Sdavidn SETGRENT(); 117420267Sjoerg j = 0; 117544229Sdavidn while ((grp=GETGRENT()) != NULL) 117620267Sjoerg { 117720267Sjoerg int i = 0; 117820747Sdavidn while (grp->gr_mem[i] != NULL) 117920267Sjoerg { 118020267Sjoerg if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 118120267Sjoerg { 118220747Sdavidn printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 118320267Sjoerg break; 118420267Sjoerg } 118520267Sjoerg ++i; 118620267Sjoerg } 118720267Sjoerg } 118844229Sdavidn ENDGRENT(); 118961957Sache printf("%s", j ? "\n" : ""); 119020253Sjoerg } 119120267Sjoerg return EXIT_SUCCESS; 119220253Sjoerg} 119320253Sjoerg 119420679Sdavidnchar * 119520679Sdavidnpw_checkname(u_char *name, int gecos) 119620253Sjoerg{ 119720253Sjoerg int l = 0; 119821052Sdavidn char const *notch = gecos ? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\""; 119920253Sjoerg 120020253Sjoerg while (name[l]) { 120121052Sdavidn if (strchr(notch, name[l]) != NULL || name[l] < ' ' || name[l] == 127 || 120221052Sdavidn (!gecos && l==0 && name[l] == '-') || /* leading '-' */ 120321052Sdavidn (!gecos && name[l] & 0x80)) /* 8-bit */ 120430259Scharnier errx(EX_DATAERR, (name[l] >= ' ' && name[l] < 127) 120530259Scharnier ? "invalid character `%c' in field" 120630259Scharnier : "invalid character 0x%02x in field", 120720325Sjoerg name[l]); 120820253Sjoerg ++l; 120920253Sjoerg } 121022394Sdavidn if (!gecos && l > LOGNAMESIZE) 121130259Scharnier errx(EX_DATAERR, "name too long `%s'", name); 121220679Sdavidn return (char *)name; 121320253Sjoerg} 121420253Sjoerg 121520253Sjoerg 121620253Sjoergstatic void 121720253Sjoergrmat(uid_t uid) 121820253Sjoerg{ 121920253Sjoerg DIR *d = opendir("/var/at/jobs"); 122020253Sjoerg 122120253Sjoerg if (d != NULL) { 122220253Sjoerg struct dirent *e; 122320253Sjoerg 122420253Sjoerg while ((e = readdir(d)) != NULL) { 122520253Sjoerg struct stat st; 122620253Sjoerg 122720253Sjoerg if (strncmp(e->d_name, ".lock", 5) != 0 && 122820253Sjoerg stat(e->d_name, &st) == 0 && 122920253Sjoerg !S_ISDIR(st.st_mode) && 123020253Sjoerg st.st_uid == uid) { 123120253Sjoerg char tmp[MAXPATHLEN]; 123220253Sjoerg 123320253Sjoerg sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 123420253Sjoerg system(tmp); 123520253Sjoerg } 123620253Sjoerg } 123720253Sjoerg closedir(d); 123820253Sjoerg } 123920253Sjoerg} 124020747Sdavidn 124120747Sdavidnstatic void 124220747Sdavidnrmskey(char const * name) 124320747Sdavidn{ 124420747Sdavidn static const char etcskey[] = "/etc/skeykeys"; 124521052Sdavidn FILE *fp = fopen(etcskey, "r+"); 124620747Sdavidn 124721052Sdavidn if (fp != NULL) { 124821052Sdavidn char tmp[1024]; 124921052Sdavidn off_t atofs = 0; 125021052Sdavidn int length = strlen(name); 125120747Sdavidn 125221052Sdavidn while (fgets(tmp, sizeof tmp, fp) != NULL) { 125321052Sdavidn if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 125421052Sdavidn if (fseek(fp, atofs, SEEK_SET) == 0) { 125521052Sdavidn fwrite("#", 1, 1, fp); /* Comment username out */ 125621052Sdavidn } 125721052Sdavidn break; 125820747Sdavidn } 125921052Sdavidn atofs = ftell(fp); 126020747Sdavidn } 126121052Sdavidn /* 126221052Sdavidn * If we got an error of any sort, don't update! 126321052Sdavidn */ 126421052Sdavidn fclose(fp); 126520747Sdavidn } 126620747Sdavidn} 126720747Sdavidn 1268