pw_user.c revision 181785
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 181785 2008-08-16 15:41:03Z ache $"; 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> 4620253Sjoerg#include "pw.h" 4720253Sjoerg#include "bitmap.h" 4820253Sjoerg 4923318Sache#if (MAXLOGNAME-1) > UT_NAMESIZE 5022394Sdavidn#define LOGNAMESIZE UT_NAMESIZE 5122394Sdavidn#else 5223318Sache#define LOGNAMESIZE (MAXLOGNAME-1) 5322394Sdavidn#endif 5422394Sdavidn 5552512Sdavidnstatic char locked_str[] = "*LOCKED*"; 5624214Sache 5744386Sdavidnstatic int print_user(struct passwd * pwd, int pretty, int v7); 5820253Sjoergstatic uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args); 5920253Sjoergstatic uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer); 6020253Sjoergstatic time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 6120253Sjoergstatic time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 6220253Sjoergstatic char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 6320253Sjoergstatic char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 6420253Sjoergstatic char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 6520253Sjoergstatic char *shell_path(char const * path, char *shells[], char *sh); 6620253Sjoergstatic void rmat(uid_t uid); 6785145Sachestatic void rmopie(char const * name); 6820253Sjoerg 6920253Sjoerg/*- 7020253Sjoerg * -C config configuration file 7120253Sjoerg * -q quiet operation 7220253Sjoerg * -n name login name 7320253Sjoerg * -u uid user id 7420253Sjoerg * -c comment user name/comment 7520253Sjoerg * -d directory home directory 7620253Sjoerg * -e date account expiry date 7720253Sjoerg * -p date password expiry date 7820253Sjoerg * -g grp primary group 7920253Sjoerg * -G grp1,grp2 additional groups 8020253Sjoerg * -m [ -k dir ] create and set up home 8120253Sjoerg * -s shell name of login shell 8220253Sjoerg * -o duplicate uid ok 8320253Sjoerg * -L class user class 8420253Sjoerg * -l name new login name 8520253Sjoerg * -h fd password filehandle 86124382Siedowse * -H fd encrypted password filehandle 8720253Sjoerg * -F force print or add 8820253Sjoerg * Setting defaults: 8920253Sjoerg * -D set user defaults 9020253Sjoerg * -b dir default home root dir 9120253Sjoerg * -e period default expiry period 9220253Sjoerg * -p period default password change period 9320253Sjoerg * -g group default group 9420253Sjoerg * -G grp1,grp2.. default additional groups 9520253Sjoerg * -L class default login class 9620253Sjoerg * -k dir default home skeleton 9720253Sjoerg * -s shell default shell 9820253Sjoerg * -w method default password method 9920253Sjoerg */ 10020253Sjoerg 10120253Sjoergint 10220253Sjoergpw_user(struct userconf * cnf, int mode, struct cargs * args) 10320253Sjoerg{ 10452527Sdavidn int rc, edited = 0; 10520253Sjoerg char *p = NULL; 10652512Sdavidn char *passtmp; 10720253Sjoerg struct carg *a_name; 10820253Sjoerg struct carg *a_uid; 10920253Sjoerg struct carg *arg; 11020253Sjoerg struct passwd *pwd = NULL; 11120253Sjoerg struct group *grp; 11220253Sjoerg struct stat st; 11320747Sdavidn char line[_PASSWORD_LEN+1]; 11482868Sdd FILE *fp; 115167919Sle mode_t dmode; 116167919Sle char *dmode_c; 117167919Sle void *set = NULL; 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 159167919Sle if ((arg = getarg(args, 'M')) != NULL) { 160167919Sle dmode_c = arg->val; 161167919Sle if ((set = setmode(dmode_c)) == NULL) 162167919Sle errx(EX_DATAERR, "invalid directory creation mode '%s'", 163167919Sle dmode_c); 164167919Sle dmode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO); 165167919Sle free(set); 166168044Sle cnf->homemode = dmode; 167168044Sle } 168167919Sle 16921052Sdavidn /* 17021052Sdavidn * If we'll need to use it or we're updating it, 17121052Sdavidn * then create the base home directory if necessary 17221052Sdavidn */ 17321052Sdavidn if (arg != NULL || getarg(args, 'm') != NULL) { 17421052Sdavidn int l = strlen(cnf->home); 17521052Sdavidn 17621052Sdavidn if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 17721052Sdavidn cnf->home[--l] = '\0'; 17821052Sdavidn 17921052Sdavidn if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 18030259Scharnier errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); 18121052Sdavidn 18221052Sdavidn if (stat(cnf->home, &st) == -1) { 18321052Sdavidn char dbuf[MAXPATHLEN]; 18421052Sdavidn 18521242Sdavidn /* 18621242Sdavidn * This is a kludge especially for Joerg :) 18721242Sdavidn * If the home directory would be created in the root partition, then 18821242Sdavidn * we really create it under /usr which is likely to have more space. 18921242Sdavidn * But we create a symlink from cnf->home -> "/usr" -> cnf->home 19021242Sdavidn */ 19121242Sdavidn if (strchr(cnf->home+1, '/') == NULL) { 19221242Sdavidn strcpy(dbuf, "/usr"); 19321242Sdavidn strncat(dbuf, cnf->home, MAXPATHLEN-5); 194168044Sle if (mkdir(dbuf, cnf->homemode) != -1 || errno == EEXIST) { 19521242Sdavidn chown(dbuf, 0, 0); 196148584Spjd /* 197148584Spjd * Skip first "/" and create symlink: 198148584Spjd * /home -> usr/home 199148584Spjd */ 200148584Spjd symlink(dbuf+1, cnf->home); 20121242Sdavidn } 20221242Sdavidn /* If this falls, fall back to old method */ 20321242Sdavidn } 204130633Srobert strlcpy(dbuf, cnf->home, sizeof(dbuf)); 205130633Srobert p = dbuf; 20621242Sdavidn if (stat(dbuf, &st) == -1) { 20721242Sdavidn while ((p = strchr(++p, '/')) != NULL) { 20821242Sdavidn *p = '\0'; 20921242Sdavidn if (stat(dbuf, &st) == -1) { 210168044Sle if (mkdir(dbuf, cnf->homemode) == -1) 21121242Sdavidn goto direrr; 21221242Sdavidn chown(dbuf, 0, 0); 21321242Sdavidn } else if (!S_ISDIR(st.st_mode)) 21430259Scharnier errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); 21521242Sdavidn *p = '/'; 21621242Sdavidn } 21721052Sdavidn } 21821242Sdavidn if (stat(dbuf, &st) == -1) { 219168044Sle if (mkdir(dbuf, cnf->homemode) == -1) { 22030259Scharnier direrr: err(EX_OSFILE, "mkdir '%s'", dbuf); 22121052Sdavidn } 22221052Sdavidn chown(dbuf, 0, 0); 22321052Sdavidn } 22421052Sdavidn } else if (!S_ISDIR(st.st_mode)) 22530259Scharnier errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); 22621052Sdavidn } 22721052Sdavidn 22820253Sjoerg if ((arg = getarg(args, 'e')) != NULL) 22920253Sjoerg cnf->expire_days = atoi(arg->val); 23020253Sjoerg 23121330Sdavidn if ((arg = getarg(args, 'y')) != NULL) 23221330Sdavidn cnf->nispasswd = arg->val; 23321330Sdavidn 23420253Sjoerg if ((arg = getarg(args, 'p')) != NULL && arg->val) 23520253Sjoerg cnf->password_days = atoi(arg->val); 23620253Sjoerg 23720253Sjoerg if ((arg = getarg(args, 'g')) != NULL) { 23863596Sdavidn if (!*(p = arg->val)) /* Handle empty group list specially */ 23963596Sdavidn cnf->default_group = ""; 24063596Sdavidn else { 24163596Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 24263596Sdavidn if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 24363596Sdavidn errx(EX_NOUSER, "group `%s' does not exist", p); 24463596Sdavidn } 24563596Sdavidn cnf->default_group = newstr(grp->gr_name); 24620253Sjoerg } 24720253Sjoerg } 24820253Sjoerg if ((arg = getarg(args, 'L')) != NULL) 24920679Sdavidn cnf->default_class = pw_checkname((u_char *)arg->val, 0); 25020253Sjoerg 25120253Sjoerg if ((arg = getarg(args, 'G')) != NULL && arg->val) { 25252527Sdavidn int i = 0; 25320253Sjoerg 25420747Sdavidn for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 25544229Sdavidn if ((grp = GETGRNAM(p)) == NULL) { 25661957Sache if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 25730259Scharnier errx(EX_NOUSER, "group `%s' does not exist", p); 25820253Sjoerg } 25920747Sdavidn if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 26020747Sdavidn cnf->groups[i++] = newstr(grp->gr_name); 26120253Sjoerg } 26220747Sdavidn while (i < cnf->numgroups) 26320253Sjoerg cnf->groups[i++] = NULL; 26420253Sjoerg } 26552527Sdavidn 26620253Sjoerg if ((arg = getarg(args, 'k')) != NULL) { 26726088Sdavidn if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode)) 26830259Scharnier errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir); 26920253Sjoerg } 27052527Sdavidn 27120253Sjoerg if ((arg = getarg(args, 's')) != NULL) 27220253Sjoerg cnf->shell_default = arg->val; 27320253Sjoerg 27463600Sdavidn if ((arg = getarg(args, 'w')) != NULL) 27563600Sdavidn cnf->default_password = boolean_val(arg->val, cnf->default_password); 27620253Sjoerg if (mode == M_ADD && getarg(args, 'D')) { 27720253Sjoerg if (getarg(args, 'n') != NULL) 27830259Scharnier errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 27920253Sjoerg if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 28020253Sjoerg if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 28120253Sjoerg cnf->min_uid = 1000; 28220253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 28320253Sjoerg cnf->max_uid = 32000; 28420253Sjoerg } 28520253Sjoerg if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 28620253Sjoerg if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 28720253Sjoerg cnf->min_gid = 1000; 28820253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 28920253Sjoerg cnf->max_gid = 32000; 29020253Sjoerg } 29120253Sjoerg 29220253Sjoerg arg = getarg(args, 'C'); 29320253Sjoerg if (write_userconfig(arg ? arg->val : NULL)) 29420267Sjoerg return EXIT_SUCCESS; 29530259Scharnier warn("config update"); 29620267Sjoerg return EX_IOERR; 29720253Sjoerg } 29852527Sdavidn 29920253Sjoerg if (mode == M_PRINT && getarg(args, 'a')) { 30020267Sjoerg int pretty = getarg(args, 'P') != NULL; 30144386Sdavidn int v7 = getarg(args, '7') != NULL; 30220253Sjoerg 30344229Sdavidn SETPWENT(); 30444229Sdavidn while ((pwd = GETPWENT()) != NULL) 30544386Sdavidn print_user(pwd, pretty, v7); 30644229Sdavidn ENDPWENT(); 30720267Sjoerg return EXIT_SUCCESS; 30820253Sjoerg } 30952527Sdavidn 31020253Sjoerg if ((a_name = getarg(args, 'n')) != NULL) 31144229Sdavidn pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0)); 31220253Sjoerg a_uid = getarg(args, 'u'); 31320253Sjoerg 31420253Sjoerg if (a_uid == NULL) { 31520253Sjoerg if (a_name == NULL) 31630259Scharnier errx(EX_DATAERR, "user name or id required"); 31720253Sjoerg 31820253Sjoerg /* 31920253Sjoerg * Determine whether 'n' switch is name or uid - we don't 32020253Sjoerg * really don't really care which we have, but we need to 32120253Sjoerg * know. 32220253Sjoerg */ 32343780Sdes if (mode != M_ADD && pwd == NULL 32443780Sdes && strspn(a_name->val, "0123456789") == strlen(a_name->val) 32543780Sdes && atoi(a_name->val) > 0) { /* Assume uid */ 32620253Sjoerg (a_uid = a_name)->ch = 'u'; 32720253Sjoerg a_name = NULL; 32820253Sjoerg } 32920253Sjoerg } 33052527Sdavidn 33120253Sjoerg /* 33220253Sjoerg * Update, delete & print require that the user exists 33320253Sjoerg */ 33452512Sdavidn if (mode == M_UPDATE || mode == M_DELETE || 33552512Sdavidn mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) { 33652527Sdavidn 33720253Sjoerg if (a_name == NULL && pwd == NULL) /* Try harder */ 33844229Sdavidn pwd = GETPWUID(atoi(a_uid->val)); 33920253Sjoerg 34020253Sjoerg if (pwd == NULL) { 34120253Sjoerg if (mode == M_PRINT && getarg(args, 'F')) { 34220253Sjoerg fakeuser.pw_name = a_name ? a_name->val : "nouser"; 34320253Sjoerg fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 34444386Sdavidn return print_user(&fakeuser, 34544386Sdavidn getarg(args, 'P') != NULL, 34644386Sdavidn getarg(args, '7') != NULL); 34720253Sjoerg } 34820253Sjoerg if (a_name == NULL) 34930259Scharnier errx(EX_NOUSER, "no such uid `%s'", a_uid->val); 35030259Scharnier errx(EX_NOUSER, "no such user `%s'", a_name->val); 35120253Sjoerg } 35252527Sdavidn 35320253Sjoerg if (a_name == NULL) /* May be needed later */ 35420253Sjoerg a_name = addarg(args, 'n', newstr(pwd->pw_name)); 35520253Sjoerg 35620253Sjoerg /* 35752512Sdavidn * The M_LOCK and M_UNLOCK functions simply add or remove 35852512Sdavidn * a "*LOCKED*" prefix from in front of the password to 35952512Sdavidn * prevent it decoding correctly, and therefore prevents 36052512Sdavidn * access. Of course, this only prevents access via 36152512Sdavidn * password authentication (not ssh, kerberos or any 36252512Sdavidn * other method that does not use the UNIX password) but 36352512Sdavidn * that is a known limitation. 36452512Sdavidn */ 36552512Sdavidn 36652512Sdavidn if (mode == M_LOCK) { 36752512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0) 36852512Sdavidn errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 36952512Sdavidn passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str)); 37052512Sdavidn if (passtmp == NULL) /* disaster */ 37152512Sdavidn errx(EX_UNAVAILABLE, "out of memory"); 37252512Sdavidn strcpy(passtmp, locked_str); 37352512Sdavidn strcat(passtmp, pwd->pw_passwd); 37452512Sdavidn pwd->pw_passwd = passtmp; 37552527Sdavidn edited = 1; 37652512Sdavidn } else if (mode == M_UNLOCK) { 37752512Sdavidn if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0) 37852512Sdavidn errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 37952512Sdavidn pwd->pw_passwd += sizeof(locked_str)-1; 38052527Sdavidn edited = 1; 38152527Sdavidn } else if (mode == M_DELETE) { 38252527Sdavidn /* 38352527Sdavidn * Handle deletions now 38452527Sdavidn */ 38520253Sjoerg char file[MAXPATHLEN]; 38620253Sjoerg char home[MAXPATHLEN]; 38720253Sjoerg uid_t uid = pwd->pw_uid; 38820253Sjoerg 38920253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 39030259Scharnier errx(EX_DATAERR, "cannot remove user 'root'"); 39120253Sjoerg 39244229Sdavidn if (!PWALTDIR()) { 39344229Sdavidn /* 39485145Sache * Remove opie record from /etc/opiekeys 39544229Sdavidn */ 39620747Sdavidn 39785145Sache rmopie(pwd->pw_name); 39820747Sdavidn 39944229Sdavidn /* 40044229Sdavidn * Remove crontabs 40144229Sdavidn */ 40244229Sdavidn sprintf(file, "/var/cron/tabs/%s", pwd->pw_name); 40344229Sdavidn if (access(file, F_OK) == 0) { 40444229Sdavidn sprintf(file, "crontab -u %s -r", pwd->pw_name); 40544229Sdavidn system(file); 40644229Sdavidn } 40720253Sjoerg } 40820253Sjoerg /* 40920253Sjoerg * Save these for later, since contents of pwd may be 41020253Sjoerg * invalidated by deletion 41120253Sjoerg */ 41220253Sjoerg sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 413130633Srobert strlcpy(home, pwd->pw_dir, sizeof(home)); 41420253Sjoerg 41552502Sdavidn rc = delpwent(pwd); 41652502Sdavidn if (rc == -1) 41752502Sdavidn err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 41852502Sdavidn else if (rc != 0) { 41956000Sdavidn warn("passwd update"); 42052502Sdavidn return EX_IOERR; 42152502Sdavidn } 42221330Sdavidn 42352502Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/') { 42452502Sdavidn rc = delnispwent(cnf->nispasswd, a_name->val); 42552502Sdavidn if (rc == -1) 42652502Sdavidn warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name); 42752502Sdavidn else if (rc != 0) 42856000Sdavidn warn("WARNING: NIS passwd update"); 42952502Sdavidn /* non-fatal */ 43052502Sdavidn } 43152502Sdavidn 43220253Sjoerg editgroups(a_name->val, NULL); 43320253Sjoerg 43420253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 43520253Sjoerg 43644229Sdavidn if (!PWALTDIR()) { 43744229Sdavidn /* 43844229Sdavidn * Remove mail file 43944229Sdavidn */ 44044229Sdavidn remove(file); 44120253Sjoerg 44244229Sdavidn /* 44344229Sdavidn * Remove at jobs 44444229Sdavidn */ 44544229Sdavidn if (getpwuid(uid) == NULL) 44644229Sdavidn rmat(uid); 44720253Sjoerg 44844229Sdavidn /* 44944229Sdavidn * Remove home directory and contents 45044229Sdavidn */ 45144229Sdavidn if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 45244229Sdavidn if (stat(home, &st) != -1) { 45344229Sdavidn rm_r(home, uid); 45444229Sdavidn pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 45544229Sdavidn a_name->val, (long) uid, home, 45644229Sdavidn stat(home, &st) == -1 ? "" : "not completely "); 45744229Sdavidn } 45820253Sjoerg } 45920253Sjoerg } 46020267Sjoerg return EXIT_SUCCESS; 46120253Sjoerg } else if (mode == M_PRINT) 46244386Sdavidn return print_user(pwd, 46344386Sdavidn getarg(args, 'P') != NULL, 46444386Sdavidn getarg(args, '7') != NULL); 46520253Sjoerg 46620253Sjoerg /* 46720253Sjoerg * The rest is edit code 46820253Sjoerg */ 46920253Sjoerg if ((arg = getarg(args, 'l')) != NULL) { 47020253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 47130259Scharnier errx(EX_DATAERR, "can't rename `root' account"); 47220679Sdavidn pwd->pw_name = pw_checkname((u_char *)arg->val, 0); 47352527Sdavidn edited = 1; 47420253Sjoerg } 47552527Sdavidn 47661957Sache if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) { 47720253Sjoerg pwd->pw_uid = (uid_t) atol(arg->val); 47852527Sdavidn edited = 1; 47920253Sjoerg if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 48030259Scharnier errx(EX_DATAERR, "can't change uid of `root' account"); 48120253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 48230259Scharnier warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 48320253Sjoerg } 48420253Sjoerg 48552527Sdavidn if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */ 48652527Sdavidn gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid; 48752527Sdavidn if (newgid != pwd->pw_gid) { 48852527Sdavidn edited = 1; 48961762Sdavidn pwd->pw_gid = newgid; 49052527Sdavidn } 49152527Sdavidn } 49252527Sdavidn 49320253Sjoerg if ((arg = getarg(args, 'p')) != NULL) { 49452527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 49552527Sdavidn if (pwd->pw_change != 0) { 49652527Sdavidn pwd->pw_change = 0; 49752527Sdavidn edited = 1; 49852527Sdavidn } 49952527Sdavidn } 50020253Sjoerg else { 50120253Sjoerg time_t now = time(NULL); 50220253Sjoerg time_t expire = parse_date(now, arg->val); 50320253Sjoerg 50420253Sjoerg if (now == expire) 50530259Scharnier errx(EX_DATAERR, "invalid password change date `%s'", arg->val); 50652527Sdavidn if (pwd->pw_change != expire) { 50752527Sdavidn pwd->pw_change = expire; 50852527Sdavidn edited = 1; 50952527Sdavidn } 51020253Sjoerg } 51120253Sjoerg } 51252527Sdavidn 51320267Sjoerg if ((arg = getarg(args, 'e')) != NULL) { 51452527Sdavidn if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 51552527Sdavidn if (pwd->pw_expire != 0) { 51652527Sdavidn pwd->pw_expire = 0; 51752527Sdavidn edited = 1; 51852527Sdavidn } 51952527Sdavidn } 52020253Sjoerg else { 52120253Sjoerg time_t now = time(NULL); 52220253Sjoerg time_t expire = parse_date(now, arg->val); 52320253Sjoerg 52420253Sjoerg if (now == expire) 52530259Scharnier errx(EX_DATAERR, "invalid account expiry date `%s'", arg->val); 52652527Sdavidn if (pwd->pw_expire != expire) { 52752527Sdavidn pwd->pw_expire = expire; 52852527Sdavidn edited = 1; 52952527Sdavidn } 53020253Sjoerg } 53120253Sjoerg } 53220253Sjoerg 53352527Sdavidn if ((arg = getarg(args, 's')) != NULL) { 53452527Sdavidn char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 53552527Sdavidn if (shell == NULL) 53652527Sdavidn shell = ""; 53752527Sdavidn if (strcmp(shell, pwd->pw_shell) != 0) { 53852527Sdavidn pwd->pw_shell = shell; 53952527Sdavidn edited = 1; 54052527Sdavidn } 54152527Sdavidn } 54220253Sjoerg 54352527Sdavidn if (getarg(args, 'L')) { 54452527Sdavidn if (cnf->default_class == NULL) 54552527Sdavidn cnf->default_class = ""; 54652527Sdavidn if (strcmp(pwd->pw_class, cnf->default_class) != 0) { 54752527Sdavidn pwd->pw_class = cnf->default_class; 54852527Sdavidn edited = 1; 54952527Sdavidn } 55052527Sdavidn } 55152527Sdavidn 55220747Sdavidn if ((arg = getarg(args, 'd')) != NULL) { 553130629Srobert if (strcmp(pwd->pw_dir, arg->val)) 554130629Srobert edited = 1; 55520747Sdavidn if (stat(pwd->pw_dir = arg->val, &st) == -1) { 55620747Sdavidn if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 55730259Scharnier warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 55820747Sdavidn } else if (!S_ISDIR(st.st_mode)) 55930259Scharnier warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 56020747Sdavidn } 56120747Sdavidn 562124382Siedowse if ((arg = getarg(args, 'w')) != NULL && 563124382Siedowse getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) { 56464918Sgreen login_cap_t *lc; 56564918Sgreen 56664918Sgreen lc = login_getpwclass(pwd); 56764918Sgreen if (lc == NULL || 56864918Sgreen login_setcryptfmt(lc, "md5", NULL) == NULL) 56964918Sgreen warn("setting crypt(3) format"); 57064918Sgreen login_close(lc); 57120267Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 57252527Sdavidn edited = 1; 57352527Sdavidn } 57420267Sjoerg 57520253Sjoerg } else { 57664918Sgreen login_cap_t *lc; 57752527Sdavidn 57852527Sdavidn /* 57952527Sdavidn * Add code 58052527Sdavidn */ 58152527Sdavidn 58220253Sjoerg if (a_name == NULL) /* Required */ 58330259Scharnier errx(EX_DATAERR, "login name required"); 58444229Sdavidn else if ((pwd = GETPWNAM(a_name->val)) != NULL) /* Exists */ 58530259Scharnier errx(EX_DATAERR, "login name `%s' already exists", a_name->val); 58620253Sjoerg 58720253Sjoerg /* 58820253Sjoerg * Now, set up defaults for a new user 58920253Sjoerg */ 59020253Sjoerg pwd = &fakeuser; 59120253Sjoerg pwd->pw_name = a_name->val; 59220253Sjoerg pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 59320253Sjoerg pwd->pw_uid = pw_uidpolicy(cnf, args); 59420253Sjoerg pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 59520253Sjoerg pwd->pw_change = pw_pwdpolicy(cnf, args); 59620253Sjoerg pwd->pw_expire = pw_exppolicy(cnf, args); 59720253Sjoerg pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 59820253Sjoerg pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 59964918Sgreen lc = login_getpwclass(pwd); 60064918Sgreen if (lc == NULL || login_setcryptfmt(lc, "md5", NULL) == NULL) 60164918Sgreen warn("setting crypt(3) format"); 60264918Sgreen login_close(lc); 60364918Sgreen pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 60452527Sdavidn edited = 1; 60520253Sjoerg 60620253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 60730259Scharnier warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 60820253Sjoerg } 60920253Sjoerg 61020253Sjoerg /* 61120253Sjoerg * Shared add/edit code 61220253Sjoerg */ 61352527Sdavidn if ((arg = getarg(args, 'c')) != NULL) { 61452527Sdavidn char *gecos = pw_checkname((u_char *)arg->val, 1); 61552527Sdavidn if (strcmp(pwd->pw_gecos, gecos) != 0) { 61652527Sdavidn pwd->pw_gecos = gecos; 61752527Sdavidn edited = 1; 61852527Sdavidn } 61952527Sdavidn } 62020253Sjoerg 621124382Siedowse if ((arg = getarg(args, 'h')) != NULL || 622124382Siedowse (arg = getarg(args, 'H')) != NULL) { 62363572Sdavidn if (strcmp(arg->val, "-") == 0) { 62463572Sdavidn if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 62563572Sdavidn pwd->pw_passwd = "*"; /* No access */ 62663572Sdavidn edited = 1; 62763572Sdavidn } 62863572Sdavidn } else { 62920253Sjoerg int fd = atoi(arg->val); 630124382Siedowse int precrypt = (arg->ch == 'H'); 63120253Sjoerg int b; 63220253Sjoerg int istty = isatty(fd); 63320253Sjoerg struct termios t; 63464918Sgreen login_cap_t *lc; 63520253Sjoerg 63620253Sjoerg if (istty) { 63720253Sjoerg if (tcgetattr(fd, &t) == -1) 63820253Sjoerg istty = 0; 63920253Sjoerg else { 64020253Sjoerg struct termios n = t; 64120253Sjoerg 64220253Sjoerg /* Disable echo */ 64320253Sjoerg n.c_lflag &= ~(ECHO); 64420253Sjoerg tcsetattr(fd, TCSANOW, &n); 645124382Siedowse printf("%s%spassword for user %s:", 646124382Siedowse (mode == M_UPDATE) ? "new " : "", 647124382Siedowse precrypt ? "encrypted " : "", 648124382Siedowse pwd->pw_name); 64920253Sjoerg fflush(stdout); 65020253Sjoerg } 65120253Sjoerg } 65220253Sjoerg b = read(fd, line, sizeof(line) - 1); 65320253Sjoerg if (istty) { /* Restore state */ 65420253Sjoerg tcsetattr(fd, TCSANOW, &t); 65520253Sjoerg fputc('\n', stdout); 65620253Sjoerg fflush(stdout); 65720253Sjoerg } 65820253Sjoerg if (b < 0) { 659124382Siedowse warn("-%c file descriptor", precrypt ? 'H' : 660124382Siedowse 'h'); 66120267Sjoerg return EX_IOERR; 66220253Sjoerg } 66320253Sjoerg line[b] = '\0'; 664168045Sle if ((p = strpbrk(line, "\r\n")) != NULL) 66520253Sjoerg *p = '\0'; 66620253Sjoerg if (!*line) 66730259Scharnier errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 668124382Siedowse if (precrypt) { 669124382Siedowse if (strchr(line, ':') != NULL) 670124382Siedowse return EX_DATAERR; 671124382Siedowse pwd->pw_passwd = line; 672124382Siedowse } else { 673124382Siedowse lc = login_getpwclass(pwd); 674124382Siedowse if (lc == NULL || 675124382Siedowse login_setcryptfmt(lc, "md5", NULL) == NULL) 676124382Siedowse warn("setting crypt(3) format"); 677124382Siedowse login_close(lc); 678124382Siedowse pwd->pw_passwd = pw_pwcrypt(line); 679124382Siedowse } 68052527Sdavidn edited = 1; 68120253Sjoerg } 68220253Sjoerg } 68320267Sjoerg 68420267Sjoerg /* 68520267Sjoerg * Special case: -N only displays & exits 68620267Sjoerg */ 68720267Sjoerg if (getarg(args, 'N') != NULL) 68844386Sdavidn return print_user(pwd, 68944386Sdavidn getarg(args, 'P') != NULL, 69044386Sdavidn getarg(args, '7') != NULL); 69120267Sjoerg 69221330Sdavidn if (mode == M_ADD) { 69352527Sdavidn edited = 1; /* Always */ 69452502Sdavidn rc = addpwent(pwd); 69552502Sdavidn if (rc == -1) { 69652502Sdavidn warnx("user '%s' already exists", pwd->pw_name); 69752502Sdavidn return EX_IOERR; 69852502Sdavidn } else if (rc != 0) { 69956000Sdavidn warn("passwd file update"); 70052502Sdavidn return EX_IOERR; 70152502Sdavidn } 70252502Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/') { 70352502Sdavidn rc = addnispwent(cnf->nispasswd, pwd); 70452502Sdavidn if (rc == -1) 70552502Sdavidn warnx("User '%s' already exists in NIS passwd", pwd->pw_name); 70652502Sdavidn else 70756000Sdavidn warn("NIS passwd update"); 70852502Sdavidn /* NOTE: we treat NIS-only update errors as non-fatal */ 70952502Sdavidn } 71052512Sdavidn } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) { 71152527Sdavidn if (edited) { /* Only updated this if required */ 71252527Sdavidn rc = chgpwent(a_name->val, pwd); 71352527Sdavidn if (rc == -1) { 71452527Sdavidn warnx("user '%s' does not exist (NIS?)", pwd->pw_name); 71552527Sdavidn return EX_IOERR; 71652527Sdavidn } else if (rc != 0) { 71756000Sdavidn warn("passwd file update"); 71852527Sdavidn return EX_IOERR; 71952527Sdavidn } 72052527Sdavidn if ( cnf->nispasswd && *cnf->nispasswd=='/') { 72152527Sdavidn rc = chgnispwent(cnf->nispasswd, a_name->val, pwd); 72252527Sdavidn if (rc == -1) 72352527Sdavidn warn("User '%s' not found in NIS passwd", pwd->pw_name); 72452527Sdavidn else 72556000Sdavidn warn("NIS passwd update"); 72652527Sdavidn /* NOTE: NIS-only update errors are not fatal */ 72752527Sdavidn } 72852502Sdavidn } 72921330Sdavidn } 73021330Sdavidn 73120253Sjoerg /* 73220253Sjoerg * Ok, user is created or changed - now edit group file 73320253Sjoerg */ 73420253Sjoerg 73520253Sjoerg if (mode == M_ADD || getarg(args, 'G') != NULL) 73620253Sjoerg editgroups(pwd->pw_name, cnf->groups); 73720253Sjoerg 73861759Sdavidn /* go get a current version of pwd */ 73961759Sdavidn pwd = GETPWNAM(a_name->val); 74061759Sdavidn if (pwd == NULL) { 74161759Sdavidn /* This will fail when we rename, so special case that */ 74261759Sdavidn if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) { 74361759Sdavidn a_name->val = arg->val; /* update new name */ 74461759Sdavidn pwd = GETPWNAM(a_name->val); /* refetch renamed rec */ 74561759Sdavidn } 74661759Sdavidn } 74761759Sdavidn if (pwd == NULL) /* can't go on without this */ 74830259Scharnier errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val); 74920253Sjoerg 75044229Sdavidn grp = GETGRGID(pwd->pw_gid); 75198744Sdwmalone pw_log(cnf, mode, W_USER, "%s(%ld):%s(%ld):%s:%s:%s", 75220253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 75320253Sjoerg grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 75420253Sjoerg pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 75520253Sjoerg 75620253Sjoerg /* 75720253Sjoerg * If adding, let's touch and chown the user's mail file. This is not 75820253Sjoerg * strictly necessary under BSD with a 0755 maildir but it also 75920253Sjoerg * doesn't hurt anything to create the empty mailfile 76020253Sjoerg */ 76120253Sjoerg if (mode == M_ADD) { 76244229Sdavidn if (!PWALTDIR()) { 76344229Sdavidn sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 76444229Sdavidn close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 76544229Sdavidn * mtime */ 76644229Sdavidn chown(line, pwd->pw_uid, pwd->pw_gid); 76720253Sjoerg } 76820253Sjoerg } 76952527Sdavidn 77020253Sjoerg /* 77182868Sdd * Let's create and populate the user's home directory. Note 77220253Sjoerg * that this also `works' for editing users if -m is used, but 77320253Sjoerg * existing files will *not* be overwritten. 77420253Sjoerg */ 77544229Sdavidn if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 776168044Sle copymkdir(pwd->pw_dir, cnf->dotdir, cnf->homemode, pwd->pw_uid, pwd->pw_gid); 77720253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 77820253Sjoerg pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 77920253Sjoerg } 78052527Sdavidn 78182868Sdd 78282868Sdd /* 78382868Sdd * Finally, send mail to the new user as well, if we are asked to 78482868Sdd */ 78582868Sdd if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 78682868Sdd FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 78782868Sdd 78882868Sdd if (pfp == NULL) 78982868Sdd warn("sendmail"); 79082868Sdd else { 79182868Sdd fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 79282868Sdd while (fgets(line, sizeof(line), fp) != NULL) { 79382868Sdd /* Do substitutions? */ 79482868Sdd fputs(line, pfp); 79582868Sdd } 79682868Sdd pclose(pfp); 79782868Sdd pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 79882868Sdd pwd->pw_name, (long) pwd->pw_uid); 79982868Sdd } 80082868Sdd fclose(fp); 80182868Sdd } 80282868Sdd 80320267Sjoerg return EXIT_SUCCESS; 80420253Sjoerg} 80520253Sjoerg 80620253Sjoerg 80720253Sjoergstatic uid_t 80820253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args) 80920253Sjoerg{ 81020253Sjoerg struct passwd *pwd; 81120253Sjoerg uid_t uid = (uid_t) - 1; 81220253Sjoerg struct carg *a_uid = getarg(args, 'u'); 81320253Sjoerg 81420253Sjoerg /* 81520253Sjoerg * Check the given uid, if any 81620253Sjoerg */ 81720253Sjoerg if (a_uid != NULL) { 81820253Sjoerg uid = (uid_t) atol(a_uid->val); 81920253Sjoerg 82044229Sdavidn if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL) 82130259Scharnier errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid); 82220253Sjoerg } else { 82320253Sjoerg struct bitmap bm; 82420253Sjoerg 82520253Sjoerg /* 82620253Sjoerg * We need to allocate the next available uid under one of 82720253Sjoerg * two policies a) Grab the first unused uid b) Grab the 82820253Sjoerg * highest possible unused uid 82920253Sjoerg */ 83020253Sjoerg if (cnf->min_uid >= cnf->max_uid) { /* Sanity 83120253Sjoerg * claus^H^H^H^Hheck */ 83220253Sjoerg cnf->min_uid = 1000; 83320253Sjoerg cnf->max_uid = 32000; 83420253Sjoerg } 83520253Sjoerg bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 83620253Sjoerg 83720253Sjoerg /* 83820253Sjoerg * Now, let's fill the bitmap from the password file 83920253Sjoerg */ 84044229Sdavidn SETPWENT(); 84144229Sdavidn while ((pwd = GETPWENT()) != NULL) 84244229Sdavidn if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 84320253Sjoerg bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 84444229Sdavidn ENDPWENT(); 84520253Sjoerg 84620253Sjoerg /* 84720253Sjoerg * Then apply the policy, with fallback to reuse if necessary 84820253Sjoerg */ 84920253Sjoerg if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 85020253Sjoerg uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 85120253Sjoerg 85220253Sjoerg /* 85320253Sjoerg * Another sanity check 85420253Sjoerg */ 85520253Sjoerg if (uid < cnf->min_uid || uid > cnf->max_uid) 85630259Scharnier errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 85720253Sjoerg bm_dealloc(&bm); 85820253Sjoerg } 85920253Sjoerg return uid; 86020253Sjoerg} 86120253Sjoerg 86220253Sjoerg 86320253Sjoergstatic uid_t 86420253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 86520253Sjoerg{ 86620253Sjoerg struct group *grp; 86720253Sjoerg gid_t gid = (uid_t) - 1; 86820253Sjoerg struct carg *a_gid = getarg(args, 'g'); 86920253Sjoerg 87020253Sjoerg /* 87120253Sjoerg * If no arg given, see if default can help out 87220253Sjoerg */ 87320253Sjoerg if (a_gid == NULL && cnf->default_group && *cnf->default_group) 87420253Sjoerg a_gid = addarg(args, 'g', cnf->default_group); 87520253Sjoerg 87620253Sjoerg /* 87720253Sjoerg * Check the given gid, if any 87820253Sjoerg */ 87944229Sdavidn SETGRENT(); 88020253Sjoerg if (a_gid != NULL) { 88144229Sdavidn if ((grp = GETGRNAM(a_gid->val)) == NULL) { 88220253Sjoerg gid = (gid_t) atol(a_gid->val); 88361957Sache if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL) 88430259Scharnier errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 88520253Sjoerg } 88620253Sjoerg gid = grp->gr_gid; 88744229Sdavidn } else if ((grp = GETGRNAM(nam)) != NULL && grp->gr_mem[0] == NULL) { 88820267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 88920253Sjoerg } else { 89020253Sjoerg struct cargs grpargs; 89120253Sjoerg char tmp[32]; 89220253Sjoerg 89320253Sjoerg LIST_INIT(&grpargs); 89420253Sjoerg addarg(&grpargs, 'n', nam); 89520253Sjoerg 89620253Sjoerg /* 89720253Sjoerg * We need to auto-create a group with the user's name. We 89820253Sjoerg * can send all the appropriate output to our sister routine 89920253Sjoerg * bit first see if we can create a group with gid==uid so we 90020253Sjoerg * can keep the user and group ids in sync. We purposely do 90120253Sjoerg * NOT check the gid range if we can force the sync. If the 90220253Sjoerg * user's name dups an existing group, then the group add 90320253Sjoerg * function will happily handle that case for us and exit. 90420253Sjoerg */ 90544229Sdavidn if (GETGRGID(prefer) == NULL) { 90620253Sjoerg sprintf(tmp, "%lu", (unsigned long) prefer); 90720253Sjoerg addarg(&grpargs, 'g', tmp); 90820253Sjoerg } 90920267Sjoerg if (getarg(args, 'N')) 91020267Sjoerg { 91120267Sjoerg addarg(&grpargs, 'N', NULL); 91220267Sjoerg addarg(&grpargs, 'q', NULL); 91320267Sjoerg gid = pw_group(cnf, M_NEXT, &grpargs); 91420267Sjoerg } 91520267Sjoerg else 91620267Sjoerg { 91720267Sjoerg pw_group(cnf, M_ADD, &grpargs); 91844229Sdavidn if ((grp = GETGRNAM(nam)) != NULL) 91920267Sjoerg gid = grp->gr_gid; 92020267Sjoerg } 92170486Sben a_gid = LIST_FIRST(&grpargs); 92220253Sjoerg while (a_gid != NULL) { 92370486Sben struct carg *t = LIST_NEXT(a_gid, list); 92420253Sjoerg LIST_REMOVE(a_gid, list); 92520253Sjoerg a_gid = t; 92620253Sjoerg } 92720253Sjoerg } 92844229Sdavidn ENDGRENT(); 92920253Sjoerg return gid; 93020253Sjoerg} 93120253Sjoerg 93220253Sjoerg 93320253Sjoergstatic time_t 93420253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args) 93520253Sjoerg{ 93620253Sjoerg time_t result = 0; 93720253Sjoerg time_t now = time(NULL); 93827831Sdavidn struct carg *arg = getarg(args, 'p'); 93920253Sjoerg 94020253Sjoerg if (arg != NULL) { 94120253Sjoerg if ((result = parse_date(now, arg->val)) == now) 94230259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 94320253Sjoerg } else if (cnf->password_days > 0) 94420253Sjoerg result = now + ((long) cnf->password_days * 86400L); 94520253Sjoerg return result; 94620253Sjoerg} 94720253Sjoerg 94820253Sjoerg 94920253Sjoergstatic time_t 95020253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args) 95120253Sjoerg{ 95220253Sjoerg time_t result = 0; 95320253Sjoerg time_t now = time(NULL); 95420253Sjoerg struct carg *arg = getarg(args, 'e'); 95520253Sjoerg 95620253Sjoerg if (arg != NULL) { 95720253Sjoerg if ((result = parse_date(now, arg->val)) == now) 95830259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 95920253Sjoerg } else if (cnf->expire_days > 0) 96020253Sjoerg result = now + ((long) cnf->expire_days * 86400L); 96120253Sjoerg return result; 96220253Sjoerg} 96320253Sjoerg 96420253Sjoerg 96520253Sjoergstatic char * 96620253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 96720253Sjoerg{ 96820253Sjoerg struct carg *arg = getarg(args, 'd'); 96920253Sjoerg 97020253Sjoerg if (arg) 97120253Sjoerg return arg->val; 97220253Sjoerg else { 97320253Sjoerg static char home[128]; 97420253Sjoerg 97520253Sjoerg if (cnf->home == NULL || *cnf->home == '\0') 97630259Scharnier errx(EX_CONFIG, "no base home directory set"); 97720253Sjoerg sprintf(home, "%s/%s", cnf->home, user); 97820253Sjoerg return home; 97920253Sjoerg } 98020253Sjoerg} 98120253Sjoerg 98220253Sjoergstatic char * 98320253Sjoergshell_path(char const * path, char *shells[], char *sh) 98420253Sjoerg{ 98520253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 98620253Sjoerg return sh; /* specified full path or forced none */ 98720253Sjoerg else { 98820253Sjoerg char *p; 98920253Sjoerg char paths[_UC_MAXLINE]; 99020253Sjoerg 99120253Sjoerg /* 99220253Sjoerg * We need to search paths 99320253Sjoerg */ 994130633Srobert strlcpy(paths, path, sizeof(paths)); 99520253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 99620253Sjoerg int i; 99720253Sjoerg static char shellpath[256]; 99820253Sjoerg 99920253Sjoerg if (sh != NULL) { 100020253Sjoerg sprintf(shellpath, "%s/%s", p, sh); 100120253Sjoerg if (access(shellpath, X_OK) == 0) 100220253Sjoerg return shellpath; 100320253Sjoerg } else 100420253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 100520253Sjoerg sprintf(shellpath, "%s/%s", p, shells[i]); 100620253Sjoerg if (access(shellpath, X_OK) == 0) 100720253Sjoerg return shellpath; 100820253Sjoerg } 100920253Sjoerg } 101020253Sjoerg if (sh == NULL) 101130259Scharnier errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 101230259Scharnier errx(EX_CONFIG, "no default shell available or defined"); 101320253Sjoerg return NULL; 101420253Sjoerg } 101520253Sjoerg} 101620253Sjoerg 101720253Sjoerg 101820253Sjoergstatic char * 101920253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 102020253Sjoerg{ 102120253Sjoerg char *sh = newshell; 102220253Sjoerg struct carg *arg = getarg(args, 's'); 102320253Sjoerg 102420253Sjoerg if (newshell == NULL && arg != NULL) 102520253Sjoerg sh = arg->val; 102620253Sjoerg return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 102720253Sjoerg} 102820253Sjoerg 1029179365Santoine#define SALTSIZE 32 103020253Sjoerg 1031179365Santoinestatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"; 1032179365Santoine 103320253Sjoergchar * 103420253Sjoergpw_pwcrypt(char *password) 103520253Sjoerg{ 103620253Sjoerg int i; 1037179365Santoine char salt[SALTSIZE + 1]; 103820253Sjoerg 103920253Sjoerg static char buf[256]; 104020253Sjoerg 104120253Sjoerg /* 104220253Sjoerg * Calculate a salt value 104320253Sjoerg */ 1044179365Santoine for (i = 0; i < SALTSIZE; i++) 1045181785Sache salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)]; 1046179365Santoine salt[SALTSIZE] = '\0'; 104720253Sjoerg 104820253Sjoerg return strcpy(buf, crypt(password, salt)); 104920253Sjoerg} 105020253Sjoerg 105120590Sdavidn 105220253Sjoergstatic char * 105320253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user) 105420253Sjoerg{ 105520253Sjoerg int i, l; 105620253Sjoerg char pwbuf[32]; 105720253Sjoerg 105820253Sjoerg switch (cnf->default_password) { 105920253Sjoerg case -1: /* Random password */ 106073563Skris l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 106120253Sjoerg for (i = 0; i < l; i++) 1062181785Sache pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)]; 106320253Sjoerg pwbuf[i] = '\0'; 106420253Sjoerg 106520253Sjoerg /* 106620253Sjoerg * We give this information back to the user 106720253Sjoerg */ 1068124382Siedowse if (getarg(args, 'h') == NULL && getarg(args, 'H') == NULL && 1069124382Siedowse getarg(args, 'N') == NULL) { 107061957Sache if (isatty(STDOUT_FILENO)) 107120712Sdavidn printf("Password for '%s' is: ", user); 107220253Sjoerg printf("%s\n", pwbuf); 107320253Sjoerg fflush(stdout); 107420253Sjoerg } 107520253Sjoerg break; 107620253Sjoerg 107720253Sjoerg case -2: /* No password at all! */ 107820253Sjoerg return ""; 107920253Sjoerg 108020253Sjoerg case 0: /* No login - default */ 108120253Sjoerg default: 108220253Sjoerg return "*"; 108320253Sjoerg 108420253Sjoerg case 1: /* user's name */ 1085130633Srobert strlcpy(pwbuf, user, sizeof(pwbuf)); 108620253Sjoerg break; 108720253Sjoerg } 108820253Sjoerg return pw_pwcrypt(pwbuf); 108920253Sjoerg} 109020253Sjoerg 109120253Sjoerg 109220253Sjoergstatic int 109344386Sdavidnprint_user(struct passwd * pwd, int pretty, int v7) 109420253Sjoerg{ 109520253Sjoerg if (!pretty) { 109620253Sjoerg char buf[_UC_MAXLINE]; 109720253Sjoerg 109844386Sdavidn fmtpwentry(buf, pwd, v7 ? PWF_PASSWD : PWF_STANDARD); 109920253Sjoerg fputs(buf, stdout); 110020253Sjoerg } else { 110120267Sjoerg int j; 110220253Sjoerg char *p; 110344229Sdavidn struct group *grp = GETGRGID(pwd->pw_gid); 110420253Sjoerg char uname[60] = "User &", office[60] = "[None]", 110520253Sjoerg wphone[60] = "[None]", hphone[60] = "[None]"; 110620590Sdavidn char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 110720590Sdavidn struct tm * tptr; 110820253Sjoerg 110920253Sjoerg if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 1110130633Srobert strlcpy(uname, p, sizeof(uname)); 111120253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1112130633Srobert strlcpy(office, p, sizeof(office)); 111320253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 1114130633Srobert strlcpy(wphone, p, sizeof(wphone)); 111520253Sjoerg if ((p = strtok(NULL, "")) != NULL) { 1116130633Srobert strlcpy(hphone, p, 1117130633Srobert sizeof(hphone)); 111820253Sjoerg } 111920253Sjoerg } 112020253Sjoerg } 112120253Sjoerg } 112220253Sjoerg /* 112320253Sjoerg * Handle '&' in gecos field 112420253Sjoerg */ 112520253Sjoerg if ((p = strchr(uname, '&')) != NULL) { 112620253Sjoerg int l = strlen(pwd->pw_name); 112720253Sjoerg int m = strlen(p); 112820253Sjoerg 112920253Sjoerg memmove(p + l, p + 1, m); 113020253Sjoerg memmove(p, pwd->pw_name, l); 113161957Sache *p = (char) toupper((unsigned char)*p); 113220253Sjoerg } 113320590Sdavidn if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 113474569Sache strftime(acexpire, sizeof acexpire, "%c", tptr); 113561957Sache if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 113674569Sache strftime(pwexpire, sizeof pwexpire, "%c", tptr); 113722394Sdavidn printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n" 113820747Sdavidn " Full Name: %s\n" 113920747Sdavidn " Home: %-26.26s Class: %s\n" 114020747Sdavidn " Shell: %-26.26s Office: %s\n" 114120747Sdavidn "Work Phone: %-26.26s Home Phone: %s\n" 114220747Sdavidn "Acc Expire: %-26.26s Pwd Expire: %s\n", 114320253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 114420253Sjoerg grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 114520253Sjoerg uname, pwd->pw_dir, pwd->pw_class, 114620590Sdavidn pwd->pw_shell, office, wphone, hphone, 114720590Sdavidn acexpire, pwexpire); 114844229Sdavidn SETGRENT(); 114920267Sjoerg j = 0; 115044229Sdavidn while ((grp=GETGRENT()) != NULL) 115120267Sjoerg { 115220267Sjoerg int i = 0; 115320747Sdavidn while (grp->gr_mem[i] != NULL) 115420267Sjoerg { 115520267Sjoerg if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 115620267Sjoerg { 115720747Sdavidn printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 115820267Sjoerg break; 115920267Sjoerg } 116020267Sjoerg ++i; 116120267Sjoerg } 116220267Sjoerg } 116344229Sdavidn ENDGRENT(); 116461957Sache printf("%s", j ? "\n" : ""); 116520253Sjoerg } 116620267Sjoerg return EXIT_SUCCESS; 116720253Sjoerg} 116820253Sjoerg 116920679Sdavidnchar * 117020679Sdavidnpw_checkname(u_char *name, int gecos) 117120253Sjoerg{ 1172109961Sgad char showch[8]; 1173109961Sgad u_char const *badchars, *ch, *showtype; 1174109961Sgad int reject; 117520253Sjoerg 1176109961Sgad ch = name; 1177109961Sgad reject = 0; 1178109961Sgad if (gecos) { 1179109961Sgad /* See if the name is valid as a gecos (comment) field. */ 1180109961Sgad badchars = ":!@"; 1181109961Sgad showtype = "gecos field"; 1182109961Sgad } else { 1183109961Sgad /* See if the name is valid as a userid or group. */ 1184109961Sgad badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\""; 1185109961Sgad showtype = "userid/group name"; 1186109961Sgad /* Userids and groups can not have a leading '-'. */ 1187109961Sgad if (*ch == '-') 1188109961Sgad reject = 1; 118920253Sjoerg } 1190109961Sgad if (!reject) { 1191109961Sgad while (*ch) { 1192109961Sgad if (strchr(badchars, *ch) != NULL || *ch < ' ' || 1193109961Sgad *ch == 127) { 1194109961Sgad reject = 1; 1195109961Sgad break; 1196109961Sgad } 1197109961Sgad /* 8-bit characters are only allowed in GECOS fields */ 1198109961Sgad if (!gecos && (*ch & 0x80)) { 1199109961Sgad reject = 1; 1200109961Sgad break; 1201109961Sgad } 1202109961Sgad ch++; 1203109961Sgad } 1204109961Sgad } 1205109961Sgad /* 1206109961Sgad * A `$' is allowed as the final character for userids and groups, 1207109961Sgad * mainly for the benefit of samba. 1208109961Sgad */ 1209109961Sgad if (reject && !gecos) { 1210109961Sgad if (*ch == '$' && *(ch + 1) == '\0') { 1211109961Sgad reject = 0; 1212109961Sgad ch++; 1213109961Sgad } 1214109961Sgad } 1215109961Sgad if (reject) { 1216109961Sgad snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127) 1217109961Sgad ? "`%c'" : "0x%02x", *ch); 1218109961Sgad errx(EX_DATAERR, "invalid character %s at position %d in %s", 1219109961Sgad showch, (ch - name), showtype); 1220109961Sgad } 1221109961Sgad if (!gecos && (ch - name) > LOGNAMESIZE) 1222109961Sgad errx(EX_DATAERR, "name too long `%s' (max is %d)", name, 1223109961Sgad LOGNAMESIZE); 122420679Sdavidn return (char *)name; 122520253Sjoerg} 122620253Sjoerg 122720253Sjoerg 122820253Sjoergstatic void 122920253Sjoergrmat(uid_t uid) 123020253Sjoerg{ 123120253Sjoerg DIR *d = opendir("/var/at/jobs"); 123220253Sjoerg 123320253Sjoerg if (d != NULL) { 123420253Sjoerg struct dirent *e; 123520253Sjoerg 123620253Sjoerg while ((e = readdir(d)) != NULL) { 123720253Sjoerg struct stat st; 123820253Sjoerg 123920253Sjoerg if (strncmp(e->d_name, ".lock", 5) != 0 && 124020253Sjoerg stat(e->d_name, &st) == 0 && 124120253Sjoerg !S_ISDIR(st.st_mode) && 124220253Sjoerg st.st_uid == uid) { 124320253Sjoerg char tmp[MAXPATHLEN]; 124420253Sjoerg 124520253Sjoerg sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 124620253Sjoerg system(tmp); 124720253Sjoerg } 124820253Sjoerg } 124920253Sjoerg closedir(d); 125020253Sjoerg } 125120253Sjoerg} 125220747Sdavidn 125320747Sdavidnstatic void 125485145Sachermopie(char const * name) 125520747Sdavidn{ 125685145Sache static const char etcopie[] = "/etc/opiekeys"; 125785145Sache FILE *fp = fopen(etcopie, "r+"); 125820747Sdavidn 125921052Sdavidn if (fp != NULL) { 126021052Sdavidn char tmp[1024]; 126121052Sdavidn off_t atofs = 0; 126221052Sdavidn int length = strlen(name); 126320747Sdavidn 126421052Sdavidn while (fgets(tmp, sizeof tmp, fp) != NULL) { 126521052Sdavidn if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 126621052Sdavidn if (fseek(fp, atofs, SEEK_SET) == 0) { 126721052Sdavidn fwrite("#", 1, 1, fp); /* Comment username out */ 126821052Sdavidn } 126921052Sdavidn break; 127020747Sdavidn } 127121052Sdavidn atofs = ftell(fp); 127220747Sdavidn } 127321052Sdavidn /* 127421052Sdavidn * If we got an error of any sort, don't update! 127521052Sdavidn */ 127621052Sdavidn fclose(fp); 127720747Sdavidn } 127820747Sdavidn} 127920747Sdavidn 1280