pw_user.c revision 43780
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. 2520253Sjoerg */ 2620253Sjoerg 2730259Scharnier#ifndef lint 2830259Scharnierstatic const char rcsid[] = 2943780Sdes "$Id: pw_user.c,v 1.25 1999/01/04 14:07:53 billf Exp $"; 3030259Scharnier#endif /* not lint */ 3130259Scharnier 3230259Scharnier#include <ctype.h> 3330259Scharnier#include <err.h> 3420253Sjoerg#include <fcntl.h> 3520253Sjoerg#include <sys/param.h> 3620253Sjoerg#include <dirent.h> 3730259Scharnier#include <paths.h> 3820253Sjoerg#include <termios.h> 3920555Sdavidn#include <sys/types.h> 4020555Sdavidn#include <sys/time.h> 4120555Sdavidn#include <sys/resource.h> 4230259Scharnier#include <unistd.h> 4322394Sdavidn#include <utmp.h> 4422394Sdavidn#if defined(USE_MD5RAND) 4520555Sdavidn#include <md5.h> 4622394Sdavidn#endif 4720253Sjoerg#include "pw.h" 4820253Sjoerg#include "bitmap.h" 4920253Sjoerg#include "pwupd.h" 5020253Sjoerg 5123318Sache#if (MAXLOGNAME-1) > UT_NAMESIZE 5222394Sdavidn#define LOGNAMESIZE UT_NAMESIZE 5322394Sdavidn#else 5423318Sache#define LOGNAMESIZE (MAXLOGNAME-1) 5522394Sdavidn#endif 5622394Sdavidn 5724214Sachestatic randinit; 5824214Sache 5920253Sjoergstatic int print_user(struct passwd * pwd, int pretty); 6020253Sjoergstatic uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args); 6120253Sjoergstatic uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer); 6220253Sjoergstatic time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 6320253Sjoergstatic time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 6420253Sjoergstatic char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 6520253Sjoergstatic char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 6620253Sjoergstatic char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 6720253Sjoergstatic char *shell_path(char const * path, char *shells[], char *sh); 6820253Sjoergstatic void rmat(uid_t uid); 6920747Sdavidnstatic void rmskey(char const * name); 7020253Sjoerg 7120253Sjoerg/*- 7220253Sjoerg * -C config configuration file 7320253Sjoerg * -q quiet operation 7420253Sjoerg * -n name login name 7520253Sjoerg * -u uid user id 7620253Sjoerg * -c comment user name/comment 7720253Sjoerg * -d directory home directory 7820253Sjoerg * -e date account expiry date 7920253Sjoerg * -p date password expiry date 8020253Sjoerg * -g grp primary group 8120253Sjoerg * -G grp1,grp2 additional groups 8220253Sjoerg * -m [ -k dir ] create and set up home 8320253Sjoerg * -s shell name of login shell 8420253Sjoerg * -o duplicate uid ok 8520253Sjoerg * -L class user class 8620253Sjoerg * -l name new login name 8720253Sjoerg * -h fd password filehandle 8820253Sjoerg * -F force print or add 8920253Sjoerg * Setting defaults: 9020253Sjoerg * -D set user defaults 9120253Sjoerg * -b dir default home root dir 9220253Sjoerg * -e period default expiry period 9320253Sjoerg * -p period default password change period 9420253Sjoerg * -g group default group 9520253Sjoerg * -G grp1,grp2.. default additional groups 9620253Sjoerg * -L class default login class 9720253Sjoerg * -k dir default home skeleton 9820253Sjoerg * -s shell default shell 9920253Sjoerg * -w method default password method 10020253Sjoerg */ 10120253Sjoerg 10220253Sjoergint 10320253Sjoergpw_user(struct userconf * cnf, int mode, struct cargs * args) 10420253Sjoerg{ 10521330Sdavidn int r, r1; 10620253Sjoerg char *p = NULL; 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]; 11420253Sjoerg 11520253Sjoerg static struct passwd fakeuser = 11620253Sjoerg { 11720253Sjoerg NULL, 11820253Sjoerg "*", 11920253Sjoerg -1, 12020253Sjoerg -1, 12120253Sjoerg 0, 12220253Sjoerg "", 12320253Sjoerg "User &", 12420253Sjoerg "/bin/sh", 12520253Sjoerg 0, 12620253Sjoerg 0 12720253Sjoerg }; 12820253Sjoerg 12920253Sjoerg /* 13020267Sjoerg * With M_NEXT, we only need to return the 13120267Sjoerg * next uid to stdout 13220267Sjoerg */ 13320267Sjoerg if (mode == M_NEXT) 13420267Sjoerg { 13520267Sjoerg uid_t next = pw_uidpolicy(cnf, args); 13620267Sjoerg if (getarg(args, 'q')) 13720267Sjoerg return next; 13820267Sjoerg printf("%ld:", (long)next); 13920267Sjoerg pw_group(cnf, mode, args); 14020267Sjoerg return EXIT_SUCCESS; 14120267Sjoerg } 14220267Sjoerg 14320267Sjoerg /* 14420253Sjoerg * We can do all of the common legwork here 14520253Sjoerg */ 14620253Sjoerg 14720253Sjoerg if ((arg = getarg(args, 'b')) != NULL) { 14820267Sjoerg cnf->home = arg->val; 14920253Sjoerg } 15021052Sdavidn 15121052Sdavidn /* 15221052Sdavidn * If we'll need to use it or we're updating it, 15321052Sdavidn * then create the base home directory if necessary 15421052Sdavidn */ 15521052Sdavidn if (arg != NULL || getarg(args, 'm') != NULL) { 15621052Sdavidn int l = strlen(cnf->home); 15721052Sdavidn 15821052Sdavidn if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 15921052Sdavidn cnf->home[--l] = '\0'; 16021052Sdavidn 16121052Sdavidn if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 16230259Scharnier errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); 16321052Sdavidn 16421052Sdavidn if (stat(cnf->home, &st) == -1) { 16521052Sdavidn char dbuf[MAXPATHLEN]; 16621052Sdavidn 16721242Sdavidn /* 16821242Sdavidn * This is a kludge especially for Joerg :) 16921242Sdavidn * If the home directory would be created in the root partition, then 17021242Sdavidn * we really create it under /usr which is likely to have more space. 17121242Sdavidn * But we create a symlink from cnf->home -> "/usr" -> cnf->home 17221242Sdavidn */ 17321242Sdavidn if (strchr(cnf->home+1, '/') == NULL) { 17421242Sdavidn strcpy(dbuf, "/usr"); 17521242Sdavidn strncat(dbuf, cnf->home, MAXPATHLEN-5); 17621242Sdavidn if (mkdir(dbuf, 0755) != -1 || errno == EEXIST) { 17721242Sdavidn chown(dbuf, 0, 0); 17821242Sdavidn symlink(dbuf, cnf->home); 17921242Sdavidn } 18021242Sdavidn /* If this falls, fall back to old method */ 18121242Sdavidn } 18221052Sdavidn p = strncpy(dbuf, cnf->home, sizeof dbuf); 18321052Sdavidn dbuf[MAXPATHLEN-1] = '\0'; 18421242Sdavidn if (stat(dbuf, &st) == -1) { 18521242Sdavidn while ((p = strchr(++p, '/')) != NULL) { 18621242Sdavidn *p = '\0'; 18721242Sdavidn if (stat(dbuf, &st) == -1) { 18821242Sdavidn if (mkdir(dbuf, 0755) == -1) 18921242Sdavidn goto direrr; 19021242Sdavidn chown(dbuf, 0, 0); 19121242Sdavidn } else if (!S_ISDIR(st.st_mode)) 19230259Scharnier errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); 19321242Sdavidn *p = '/'; 19421242Sdavidn } 19521052Sdavidn } 19621242Sdavidn if (stat(dbuf, &st) == -1) { 19721052Sdavidn if (mkdir(dbuf, 0755) == -1) { 19830259Scharnier direrr: err(EX_OSFILE, "mkdir '%s'", dbuf); 19921052Sdavidn } 20021052Sdavidn chown(dbuf, 0, 0); 20121052Sdavidn } 20221052Sdavidn } else if (!S_ISDIR(st.st_mode)) 20330259Scharnier errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); 20421052Sdavidn } 20521052Sdavidn 20621052Sdavidn 20720253Sjoerg if ((arg = getarg(args, 'e')) != NULL) 20820253Sjoerg cnf->expire_days = atoi(arg->val); 20920253Sjoerg 21021330Sdavidn if ((arg = getarg(args, 'y')) != NULL) 21121330Sdavidn cnf->nispasswd = arg->val; 21221330Sdavidn 21320253Sjoerg if ((arg = getarg(args, 'p')) != NULL && arg->val) 21420253Sjoerg cnf->password_days = atoi(arg->val); 21520253Sjoerg 21620253Sjoerg if ((arg = getarg(args, 'g')) != NULL) { 21720253Sjoerg p = arg->val; 21820253Sjoerg if ((grp = getgrnam(p)) == NULL) { 21920253Sjoerg if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL) 22030259Scharnier errx(EX_NOUSER, "group `%s' does not exist", p); 22120253Sjoerg } 22220253Sjoerg cnf->default_group = newstr(grp->gr_name); 22320253Sjoerg } 22420253Sjoerg if ((arg = getarg(args, 'L')) != NULL) 22520679Sdavidn cnf->default_class = pw_checkname((u_char *)arg->val, 0); 22620253Sjoerg 22720253Sjoerg if ((arg = getarg(args, 'G')) != NULL && arg->val) { 22820253Sjoerg int i = 0; 22920253Sjoerg 23020747Sdavidn for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 23120253Sjoerg if ((grp = getgrnam(p)) == NULL) { 23220253Sjoerg if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL) 23330259Scharnier errx(EX_NOUSER, "group `%s' does not exist", p); 23420253Sjoerg } 23520747Sdavidn if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 23620747Sdavidn cnf->groups[i++] = newstr(grp->gr_name); 23720253Sjoerg } 23820747Sdavidn while (i < cnf->numgroups) 23920253Sjoerg cnf->groups[i++] = NULL; 24020253Sjoerg } 24120253Sjoerg if ((arg = getarg(args, 'k')) != NULL) { 24226088Sdavidn if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode)) 24330259Scharnier errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir); 24420253Sjoerg } 24520253Sjoerg if ((arg = getarg(args, 's')) != NULL) 24620253Sjoerg cnf->shell_default = arg->val; 24720253Sjoerg 24820253Sjoerg if (mode == M_ADD && getarg(args, 'D')) { 24920253Sjoerg if (getarg(args, 'n') != NULL) 25030259Scharnier errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 25120253Sjoerg if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 25220253Sjoerg if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 25320253Sjoerg cnf->min_uid = 1000; 25420253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 25520253Sjoerg cnf->max_uid = 32000; 25620253Sjoerg } 25720253Sjoerg if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 25820253Sjoerg if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 25920253Sjoerg cnf->min_gid = 1000; 26020253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 26120253Sjoerg cnf->max_gid = 32000; 26220253Sjoerg } 26320253Sjoerg if ((arg = getarg(args, 'w')) != NULL) 26420253Sjoerg cnf->default_password = boolean_val(arg->val, cnf->default_password); 26520253Sjoerg 26620253Sjoerg arg = getarg(args, 'C'); 26720253Sjoerg if (write_userconfig(arg ? arg->val : NULL)) 26820267Sjoerg return EXIT_SUCCESS; 26930259Scharnier warn("config update"); 27020267Sjoerg return EX_IOERR; 27120253Sjoerg } 27220253Sjoerg if (mode == M_PRINT && getarg(args, 'a')) { 27320267Sjoerg int pretty = getarg(args, 'P') != NULL; 27420253Sjoerg 27520253Sjoerg setpwent(); 27620253Sjoerg while ((pwd = getpwent()) != NULL) 27720253Sjoerg print_user(pwd, pretty); 27820253Sjoerg endpwent(); 27920267Sjoerg return EXIT_SUCCESS; 28020253Sjoerg } 28120253Sjoerg if ((a_name = getarg(args, 'n')) != NULL) 28220679Sdavidn pwd = getpwnam(pw_checkname((u_char *)a_name->val, 0)); 28320253Sjoerg a_uid = getarg(args, 'u'); 28420253Sjoerg 28520253Sjoerg if (a_uid == NULL) { 28620253Sjoerg if (a_name == NULL) 28730259Scharnier errx(EX_DATAERR, "user name or id required"); 28820253Sjoerg 28920253Sjoerg /* 29020253Sjoerg * Determine whether 'n' switch is name or uid - we don't 29120253Sjoerg * really don't really care which we have, but we need to 29220253Sjoerg * know. 29320253Sjoerg */ 29443780Sdes if (mode != M_ADD && pwd == NULL 29543780Sdes && strspn(a_name->val, "0123456789") == strlen(a_name->val) 29643780Sdes && atoi(a_name->val) > 0) { /* Assume uid */ 29720253Sjoerg (a_uid = a_name)->ch = 'u'; 29820253Sjoerg a_name = NULL; 29920253Sjoerg } 30020253Sjoerg } 30120253Sjoerg /* 30220253Sjoerg * Update, delete & print require that the user exists 30320253Sjoerg */ 30420253Sjoerg if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) { 30520253Sjoerg if (a_name == NULL && pwd == NULL) /* Try harder */ 30620253Sjoerg pwd = getpwuid(atoi(a_uid->val)); 30720253Sjoerg 30820253Sjoerg if (pwd == NULL) { 30920253Sjoerg if (mode == M_PRINT && getarg(args, 'F')) { 31020253Sjoerg fakeuser.pw_name = a_name ? a_name->val : "nouser"; 31120253Sjoerg fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 31220267Sjoerg return print_user(&fakeuser, getarg(args, 'P') != NULL); 31320253Sjoerg } 31420253Sjoerg if (a_name == NULL) 31530259Scharnier errx(EX_NOUSER, "no such uid `%s'", a_uid->val); 31630259Scharnier errx(EX_NOUSER, "no such user `%s'", a_name->val); 31720253Sjoerg } 31820253Sjoerg if (a_name == NULL) /* May be needed later */ 31920253Sjoerg a_name = addarg(args, 'n', newstr(pwd->pw_name)); 32020253Sjoerg 32120253Sjoerg /* 32220253Sjoerg * Handle deletions now 32320253Sjoerg */ 32420253Sjoerg if (mode == M_DELETE) { 32520253Sjoerg char file[MAXPATHLEN]; 32620253Sjoerg char home[MAXPATHLEN]; 32720253Sjoerg uid_t uid = pwd->pw_uid; 32820253Sjoerg 32920253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 33030259Scharnier errx(EX_DATAERR, "cannot remove user 'root'"); 33120253Sjoerg 33220253Sjoerg /* 33320747Sdavidn * Remove skey record from /etc/skeykeys 33420747Sdavidn */ 33520747Sdavidn 33620747Sdavidn rmskey(pwd->pw_name); 33720747Sdavidn 33820747Sdavidn /* 33920253Sjoerg * Remove crontabs 34020253Sjoerg */ 34120253Sjoerg sprintf(file, "/var/cron/tabs/%s", pwd->pw_name); 34220253Sjoerg if (access(file, F_OK) == 0) { 34320253Sjoerg sprintf(file, "crontab -u %s -r", pwd->pw_name); 34420253Sjoerg system(file); 34520253Sjoerg } 34620253Sjoerg /* 34720253Sjoerg * Save these for later, since contents of pwd may be 34820253Sjoerg * invalidated by deletion 34920253Sjoerg */ 35020253Sjoerg sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 35120253Sjoerg strncpy(home, pwd->pw_dir, sizeof home); 35220253Sjoerg home[sizeof home - 1] = '\0'; 35320253Sjoerg 35420253Sjoerg if (!delpwent(pwd)) 35530259Scharnier err(EX_IOERR, "error updating passwd file"); 35621330Sdavidn 35721330Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/' && !delnispwent(cnf->nispasswd, a_name->val)) 35830259Scharnier warn("WARNING: NIS passwd update"); 35921330Sdavidn 36020253Sjoerg editgroups(a_name->val, NULL); 36120253Sjoerg 36220253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 36320253Sjoerg 36420253Sjoerg /* 36520253Sjoerg * Remove mail file 36620253Sjoerg */ 36720253Sjoerg remove(file); 36820253Sjoerg 36920253Sjoerg /* 37020253Sjoerg * Remove at jobs 37120253Sjoerg */ 37220253Sjoerg if (getpwuid(uid) == NULL) 37320253Sjoerg rmat(uid); 37420253Sjoerg 37520253Sjoerg /* 37620253Sjoerg * Remove home directory and contents 37720253Sjoerg */ 37820253Sjoerg if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 37920253Sjoerg if (stat(home, &st) != -1) { 38020253Sjoerg rm_r(home, uid); 38120253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 38220253Sjoerg a_name->val, (long) uid, home, 38320253Sjoerg stat(home, &st) == -1 ? "" : "not completely "); 38420253Sjoerg } 38520253Sjoerg } 38620267Sjoerg return EXIT_SUCCESS; 38720253Sjoerg } else if (mode == M_PRINT) 38820267Sjoerg return print_user(pwd, getarg(args, 'P') != NULL); 38920253Sjoerg 39020253Sjoerg /* 39120253Sjoerg * The rest is edit code 39220253Sjoerg */ 39320253Sjoerg if ((arg = getarg(args, 'l')) != NULL) { 39420253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 39530259Scharnier errx(EX_DATAERR, "can't rename `root' account"); 39620679Sdavidn pwd->pw_name = pw_checkname((u_char *)arg->val, 0); 39720253Sjoerg } 39820253Sjoerg if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) { 39920253Sjoerg pwd->pw_uid = (uid_t) atol(arg->val); 40020253Sjoerg if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 40130259Scharnier errx(EX_DATAERR, "can't change uid of `root' account"); 40220253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 40330259Scharnier warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 40420253Sjoerg } 40520253Sjoerg if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) /* Already checked this */ 40620253Sjoerg pwd->pw_gid = (gid_t) getgrnam(cnf->default_group)->gr_gid; 40720253Sjoerg 40820253Sjoerg if ((arg = getarg(args, 'p')) != NULL) { 40920253Sjoerg if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) 41020253Sjoerg pwd->pw_change = 0; 41120253Sjoerg else { 41220253Sjoerg time_t now = time(NULL); 41320253Sjoerg time_t expire = parse_date(now, arg->val); 41420253Sjoerg 41520253Sjoerg if (now == expire) 41630259Scharnier errx(EX_DATAERR, "invalid password change date `%s'", arg->val); 41720253Sjoerg pwd->pw_change = expire; 41820253Sjoerg } 41920253Sjoerg } 42020267Sjoerg if ((arg = getarg(args, 'e')) != NULL) { 42120253Sjoerg if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) 42220253Sjoerg pwd->pw_expire = 0; 42320253Sjoerg else { 42420253Sjoerg time_t now = time(NULL); 42520253Sjoerg time_t expire = parse_date(now, arg->val); 42620253Sjoerg 42720253Sjoerg if (now == expire) 42830259Scharnier errx(EX_DATAERR, "invalid account expiry date `%s'", arg->val); 42920253Sjoerg pwd->pw_expire = expire; 43020253Sjoerg } 43120253Sjoerg } 43220253Sjoerg if ((arg = getarg(args, 's')) != NULL) 43320253Sjoerg pwd->pw_shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 43420253Sjoerg 43520253Sjoerg if (getarg(args, 'L')) 43620253Sjoerg pwd->pw_class = cnf->default_class; 43720253Sjoerg 43820747Sdavidn if ((arg = getarg(args, 'd')) != NULL) { 43920747Sdavidn if (stat(pwd->pw_dir = arg->val, &st) == -1) { 44020747Sdavidn if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 44130259Scharnier warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 44220747Sdavidn } else if (!S_ISDIR(st.st_mode)) 44330259Scharnier warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 44420747Sdavidn } 44520747Sdavidn 44620267Sjoerg if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL) 44720267Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 44820267Sjoerg 44920253Sjoerg } else { 45020253Sjoerg if (a_name == NULL) /* Required */ 45130259Scharnier errx(EX_DATAERR, "login name required"); 45220253Sjoerg else if ((pwd = getpwnam(a_name->val)) != NULL) /* Exists */ 45330259Scharnier errx(EX_DATAERR, "login name `%s' already exists", a_name->val); 45420253Sjoerg 45520253Sjoerg /* 45620253Sjoerg * Now, set up defaults for a new user 45720253Sjoerg */ 45820253Sjoerg pwd = &fakeuser; 45920253Sjoerg pwd->pw_name = a_name->val; 46020253Sjoerg pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 46120253Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 46220253Sjoerg pwd->pw_uid = pw_uidpolicy(cnf, args); 46320253Sjoerg pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 46420253Sjoerg pwd->pw_change = pw_pwdpolicy(cnf, args); 46520253Sjoerg pwd->pw_expire = pw_exppolicy(cnf, args); 46620253Sjoerg pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 46720253Sjoerg pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 46820253Sjoerg 46920253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 47030259Scharnier warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 47120253Sjoerg } 47220253Sjoerg 47320253Sjoerg /* 47420253Sjoerg * Shared add/edit code 47520253Sjoerg */ 47620253Sjoerg if ((arg = getarg(args, 'c')) != NULL) 47720679Sdavidn pwd->pw_gecos = pw_checkname((u_char *)arg->val, 1); 47820253Sjoerg 47920253Sjoerg if ((arg = getarg(args, 'h')) != NULL) { 48020253Sjoerg if (strcmp(arg->val, "-") == 0) 48120253Sjoerg pwd->pw_passwd = "*"; /* No access */ 48220253Sjoerg else { 48320253Sjoerg int fd = atoi(arg->val); 48420253Sjoerg int b; 48520253Sjoerg int istty = isatty(fd); 48620253Sjoerg struct termios t; 48720253Sjoerg 48820253Sjoerg if (istty) { 48920253Sjoerg if (tcgetattr(fd, &t) == -1) 49020253Sjoerg istty = 0; 49120253Sjoerg else { 49220253Sjoerg struct termios n = t; 49320253Sjoerg 49420253Sjoerg /* Disable echo */ 49520253Sjoerg n.c_lflag &= ~(ECHO); 49620253Sjoerg tcsetattr(fd, TCSANOW, &n); 49720253Sjoerg printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name); 49820253Sjoerg fflush(stdout); 49920253Sjoerg } 50020253Sjoerg } 50120253Sjoerg b = read(fd, line, sizeof(line) - 1); 50220253Sjoerg if (istty) { /* Restore state */ 50320253Sjoerg tcsetattr(fd, TCSANOW, &t); 50420253Sjoerg fputc('\n', stdout); 50520253Sjoerg fflush(stdout); 50620253Sjoerg } 50720253Sjoerg if (b < 0) { 50830259Scharnier warn("-h file descriptor"); 50920267Sjoerg return EX_IOERR; 51020253Sjoerg } 51120253Sjoerg line[b] = '\0'; 51220253Sjoerg if ((p = strpbrk(line, " \t\r\n")) != NULL) 51320253Sjoerg *p = '\0'; 51420253Sjoerg if (!*line) 51530259Scharnier errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 51620253Sjoerg pwd->pw_passwd = pw_pwcrypt(line); 51720253Sjoerg } 51820253Sjoerg } 51920267Sjoerg 52020267Sjoerg /* 52120267Sjoerg * Special case: -N only displays & exits 52220267Sjoerg */ 52320267Sjoerg if (getarg(args, 'N') != NULL) 52420267Sjoerg return print_user(pwd, getarg(args, 'P') != NULL); 52520267Sjoerg 52621330Sdavidn r = r1 = 1; 52721330Sdavidn if (mode == M_ADD) { 52821330Sdavidn r = addpwent(pwd); 52921330Sdavidn if (r && cnf->nispasswd && *cnf->nispasswd=='/') 53021330Sdavidn r1 = addnispwent(cnf->nispasswd, pwd); 53121330Sdavidn } else if (mode == M_UPDATE) { 53221330Sdavidn r = chgpwent(a_name->val, pwd); 53321330Sdavidn if (r && cnf->nispasswd && *cnf->nispasswd=='/') 53421330Sdavidn r1 = chgnispwent(cnf->nispasswd, a_name->val, pwd); 53521330Sdavidn } 53621330Sdavidn 53721330Sdavidn if (!r) { 53830259Scharnier warn("password update"); 53920267Sjoerg return EX_IOERR; 54021330Sdavidn } else if (!r1) { 54130259Scharnier warn("WARNING: NIS password update"); 54221330Sdavidn /* Keep on trucking */ 54320253Sjoerg } 54421330Sdavidn 54520253Sjoerg /* 54620253Sjoerg * Ok, user is created or changed - now edit group file 54720253Sjoerg */ 54820253Sjoerg 54920253Sjoerg if (mode == M_ADD || getarg(args, 'G') != NULL) 55020253Sjoerg editgroups(pwd->pw_name, cnf->groups); 55120253Sjoerg 55220253Sjoerg /* pwd may have been invalidated */ 55320253Sjoerg if ((pwd = getpwnam(a_name->val)) == NULL) 55430259Scharnier errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val); 55520253Sjoerg 55620253Sjoerg grp = getgrgid(pwd->pw_gid); 55720253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s", 55820253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 55920253Sjoerg grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 56020253Sjoerg pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 56120253Sjoerg 56220253Sjoerg /* 56320253Sjoerg * If adding, let's touch and chown the user's mail file. This is not 56420253Sjoerg * strictly necessary under BSD with a 0755 maildir but it also 56520253Sjoerg * doesn't hurt anything to create the empty mailfile 56620253Sjoerg */ 56720253Sjoerg if (mode == M_ADD) { 56820253Sjoerg FILE *fp; 56920253Sjoerg 57020253Sjoerg sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 57120253Sjoerg close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 57220253Sjoerg * mtime */ 57320253Sjoerg chown(line, pwd->pw_uid, pwd->pw_gid); 57420253Sjoerg 57520253Sjoerg /* 57620253Sjoerg * Send mail to the new user as well, if we are asked to 57720253Sjoerg */ 57820253Sjoerg if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 57920253Sjoerg FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 58020253Sjoerg 58120253Sjoerg if (pfp == NULL) 58230259Scharnier warn("sendmail"); 58320253Sjoerg else { 58420253Sjoerg fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 58520253Sjoerg while (fgets(line, sizeof(line), fp) != NULL) { 58620253Sjoerg /* Do substitutions? */ 58720253Sjoerg fputs(line, pfp); 58820253Sjoerg } 58920253Sjoerg pclose(pfp); 59020253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 59120253Sjoerg pwd->pw_name, (long) pwd->pw_uid); 59220253Sjoerg } 59320253Sjoerg fclose(fp); 59420253Sjoerg } 59520253Sjoerg } 59620253Sjoerg /* 59720253Sjoerg * Finally, let's create and populate the user's home directory. Note 59820253Sjoerg * that this also `works' for editing users if -m is used, but 59920253Sjoerg * existing files will *not* be overwritten. 60020253Sjoerg */ 60120253Sjoerg if (getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 60220253Sjoerg copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid); 60320253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 60420253Sjoerg pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 60520253Sjoerg } 60620267Sjoerg return EXIT_SUCCESS; 60720253Sjoerg} 60820253Sjoerg 60920253Sjoerg 61020253Sjoergstatic uid_t 61120253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args) 61220253Sjoerg{ 61320253Sjoerg struct passwd *pwd; 61420253Sjoerg uid_t uid = (uid_t) - 1; 61520253Sjoerg struct carg *a_uid = getarg(args, 'u'); 61620253Sjoerg 61720253Sjoerg /* 61820253Sjoerg * Check the given uid, if any 61920253Sjoerg */ 62020253Sjoerg if (a_uid != NULL) { 62120253Sjoerg uid = (uid_t) atol(a_uid->val); 62220253Sjoerg 62320253Sjoerg if ((pwd = getpwuid(uid)) != NULL && getarg(args, 'o') == NULL) 62430259Scharnier errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid); 62520253Sjoerg } else { 62620253Sjoerg struct bitmap bm; 62720253Sjoerg 62820253Sjoerg /* 62920253Sjoerg * We need to allocate the next available uid under one of 63020253Sjoerg * two policies a) Grab the first unused uid b) Grab the 63120253Sjoerg * highest possible unused uid 63220253Sjoerg */ 63320253Sjoerg if (cnf->min_uid >= cnf->max_uid) { /* Sanity 63420253Sjoerg * claus^H^H^H^Hheck */ 63520253Sjoerg cnf->min_uid = 1000; 63620253Sjoerg cnf->max_uid = 32000; 63720253Sjoerg } 63820253Sjoerg bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 63920253Sjoerg 64020253Sjoerg /* 64120253Sjoerg * Now, let's fill the bitmap from the password file 64220253Sjoerg */ 64320253Sjoerg setpwent(); 64420253Sjoerg while ((pwd = getpwent()) != NULL) 64520253Sjoerg if (pwd->pw_uid >= (int) cnf->min_uid && pwd->pw_uid <= (int) cnf->max_uid) 64620253Sjoerg bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 64720253Sjoerg endpwent(); 64820253Sjoerg 64920253Sjoerg /* 65020253Sjoerg * Then apply the policy, with fallback to reuse if necessary 65120253Sjoerg */ 65220253Sjoerg if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 65320253Sjoerg uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 65420253Sjoerg 65520253Sjoerg /* 65620253Sjoerg * Another sanity check 65720253Sjoerg */ 65820253Sjoerg if (uid < cnf->min_uid || uid > cnf->max_uid) 65930259Scharnier errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 66020253Sjoerg bm_dealloc(&bm); 66120253Sjoerg } 66220253Sjoerg return uid; 66320253Sjoerg} 66420253Sjoerg 66520253Sjoerg 66620253Sjoergstatic uid_t 66720253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 66820253Sjoerg{ 66920253Sjoerg struct group *grp; 67020253Sjoerg gid_t gid = (uid_t) - 1; 67120253Sjoerg struct carg *a_gid = getarg(args, 'g'); 67220253Sjoerg 67320253Sjoerg /* 67420253Sjoerg * If no arg given, see if default can help out 67520253Sjoerg */ 67620253Sjoerg if (a_gid == NULL && cnf->default_group && *cnf->default_group) 67720253Sjoerg a_gid = addarg(args, 'g', cnf->default_group); 67820253Sjoerg 67920253Sjoerg /* 68020253Sjoerg * Check the given gid, if any 68120253Sjoerg */ 68220267Sjoerg setgrent(); 68320253Sjoerg if (a_gid != NULL) { 68420253Sjoerg if ((grp = getgrnam(a_gid->val)) == NULL) { 68520253Sjoerg gid = (gid_t) atol(a_gid->val); 68620253Sjoerg if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = getgrgid(gid)) == NULL) 68730259Scharnier errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 68820253Sjoerg } 68920253Sjoerg gid = grp->gr_gid; 69020267Sjoerg } else if ((grp = getgrnam(nam)) != NULL && grp->gr_mem[0] == NULL) { 69120267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 69220253Sjoerg } else { 69320253Sjoerg struct cargs grpargs; 69420253Sjoerg char tmp[32]; 69520253Sjoerg 69620253Sjoerg LIST_INIT(&grpargs); 69720253Sjoerg addarg(&grpargs, 'n', nam); 69820253Sjoerg 69920253Sjoerg /* 70020253Sjoerg * We need to auto-create a group with the user's name. We 70120253Sjoerg * can send all the appropriate output to our sister routine 70220253Sjoerg * bit first see if we can create a group with gid==uid so we 70320253Sjoerg * can keep the user and group ids in sync. We purposely do 70420253Sjoerg * NOT check the gid range if we can force the sync. If the 70520253Sjoerg * user's name dups an existing group, then the group add 70620253Sjoerg * function will happily handle that case for us and exit. 70720253Sjoerg */ 70820253Sjoerg if (getgrgid(prefer) == NULL) { 70920253Sjoerg sprintf(tmp, "%lu", (unsigned long) prefer); 71020253Sjoerg addarg(&grpargs, 'g', tmp); 71120253Sjoerg } 71220267Sjoerg if (getarg(args, 'N')) 71320267Sjoerg { 71420267Sjoerg addarg(&grpargs, 'N', NULL); 71520267Sjoerg addarg(&grpargs, 'q', NULL); 71620267Sjoerg gid = pw_group(cnf, M_NEXT, &grpargs); 71720267Sjoerg } 71820267Sjoerg else 71920267Sjoerg { 72020267Sjoerg pw_group(cnf, M_ADD, &grpargs); 72120267Sjoerg if ((grp = getgrnam(nam)) != NULL) 72220267Sjoerg gid = grp->gr_gid; 72320267Sjoerg } 72420253Sjoerg a_gid = grpargs.lh_first; 72520253Sjoerg while (a_gid != NULL) { 72620253Sjoerg struct carg *t = a_gid->list.le_next; 72720253Sjoerg LIST_REMOVE(a_gid, list); 72820253Sjoerg a_gid = t; 72920253Sjoerg } 73020253Sjoerg } 73120267Sjoerg endgrent(); 73220253Sjoerg return gid; 73320253Sjoerg} 73420253Sjoerg 73520253Sjoerg 73620253Sjoergstatic time_t 73720253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args) 73820253Sjoerg{ 73920253Sjoerg time_t result = 0; 74020253Sjoerg time_t now = time(NULL); 74127831Sdavidn struct carg *arg = getarg(args, 'p'); 74220253Sjoerg 74320253Sjoerg if (arg != NULL) { 74420253Sjoerg if ((result = parse_date(now, arg->val)) == now) 74530259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 74620253Sjoerg } else if (cnf->password_days > 0) 74720253Sjoerg result = now + ((long) cnf->password_days * 86400L); 74820253Sjoerg return result; 74920253Sjoerg} 75020253Sjoerg 75120253Sjoerg 75220253Sjoergstatic time_t 75320253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args) 75420253Sjoerg{ 75520253Sjoerg time_t result = 0; 75620253Sjoerg time_t now = time(NULL); 75720253Sjoerg struct carg *arg = getarg(args, 'e'); 75820253Sjoerg 75920253Sjoerg if (arg != NULL) { 76020253Sjoerg if ((result = parse_date(now, arg->val)) == now) 76130259Scharnier errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 76220253Sjoerg } else if (cnf->expire_days > 0) 76320253Sjoerg result = now + ((long) cnf->expire_days * 86400L); 76420253Sjoerg return result; 76520253Sjoerg} 76620253Sjoerg 76720253Sjoerg 76820253Sjoergstatic char * 76920253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 77020253Sjoerg{ 77120253Sjoerg struct carg *arg = getarg(args, 'd'); 77220253Sjoerg 77320253Sjoerg if (arg) 77420253Sjoerg return arg->val; 77520253Sjoerg else { 77620253Sjoerg static char home[128]; 77720253Sjoerg 77820253Sjoerg if (cnf->home == NULL || *cnf->home == '\0') 77930259Scharnier errx(EX_CONFIG, "no base home directory set"); 78020253Sjoerg sprintf(home, "%s/%s", cnf->home, user); 78120253Sjoerg return home; 78220253Sjoerg } 78320253Sjoerg} 78420253Sjoerg 78520253Sjoergstatic char * 78620253Sjoergshell_path(char const * path, char *shells[], char *sh) 78720253Sjoerg{ 78820253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 78920253Sjoerg return sh; /* specified full path or forced none */ 79020253Sjoerg else { 79120253Sjoerg char *p; 79220253Sjoerg char paths[_UC_MAXLINE]; 79320253Sjoerg 79420253Sjoerg /* 79520253Sjoerg * We need to search paths 79620253Sjoerg */ 79720253Sjoerg strncpy(paths, path, sizeof paths); 79820253Sjoerg paths[sizeof paths - 1] = '\0'; 79920253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 80020253Sjoerg int i; 80120253Sjoerg static char shellpath[256]; 80220253Sjoerg 80320253Sjoerg if (sh != NULL) { 80420253Sjoerg sprintf(shellpath, "%s/%s", p, sh); 80520253Sjoerg if (access(shellpath, X_OK) == 0) 80620253Sjoerg return shellpath; 80720253Sjoerg } else 80820253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 80920253Sjoerg sprintf(shellpath, "%s/%s", p, shells[i]); 81020253Sjoerg if (access(shellpath, X_OK) == 0) 81120253Sjoerg return shellpath; 81220253Sjoerg } 81320253Sjoerg } 81420253Sjoerg if (sh == NULL) 81530259Scharnier errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 81630259Scharnier errx(EX_CONFIG, "no default shell available or defined"); 81720253Sjoerg return NULL; 81820253Sjoerg } 81920253Sjoerg} 82020253Sjoerg 82120253Sjoerg 82220253Sjoergstatic char * 82320253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 82420253Sjoerg{ 82520253Sjoerg char *sh = newshell; 82620253Sjoerg struct carg *arg = getarg(args, 's'); 82720253Sjoerg 82820253Sjoerg if (newshell == NULL && arg != NULL) 82920253Sjoerg sh = arg->val; 83020253Sjoerg return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 83120253Sjoerg} 83220253Sjoerg 83320253Sjoergstatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."; 83420253Sjoerg 83520253Sjoergchar * 83620253Sjoergpw_pwcrypt(char *password) 83720253Sjoerg{ 83820253Sjoerg int i; 83920253Sjoerg char salt[12]; 84020253Sjoerg 84120253Sjoerg static char buf[256]; 84220253Sjoerg 84320253Sjoerg /* 84420253Sjoerg * Calculate a salt value 84520253Sjoerg */ 84624214Sache if (!randinit) { 84724214Sache randinit = 1; 84824214Sache#ifdef __FreeBSD__ 84926626Sache srandomdev(); 85026626Sache#else 85126626Sache srandom((unsigned long) (time(NULL) ^ getpid())); 85224214Sache#endif 85324214Sache } 85420253Sjoerg for (i = 0; i < 8; i++) 85520253Sjoerg salt[i] = chars[random() % 63]; 85620253Sjoerg salt[i] = '\0'; 85720253Sjoerg 85820253Sjoerg return strcpy(buf, crypt(password, salt)); 85920253Sjoerg} 86020253Sjoerg 86120590Sdavidn#if defined(USE_MD5RAND) 86220555Sdavidnu_char * 86320590Sdavidnpw_getrand(u_char *buf, int len) /* cryptographically secure rng */ 86420555Sdavidn{ 86520590Sdavidn int i; 86620590Sdavidn for (i=0;i<len;i+=16) { 86720590Sdavidn u_char ubuf[16]; 86820590Sdavidn 86920590Sdavidn MD5_CTX md5_ctx; 87020590Sdavidn struct timeval tv, tvo; 87120590Sdavidn struct rusage ru; 87220590Sdavidn int n=0; 87320590Sdavidn int t; 87420590Sdavidn 87520590Sdavidn MD5Init (&md5_ctx); 87620590Sdavidn t=getpid(); 87720590Sdavidn MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 87820590Sdavidn t=getppid(); 87920590Sdavidn MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 88020590Sdavidn gettimeofday (&tvo, NULL); 88120590Sdavidn do { 88220590Sdavidn getrusage (RUSAGE_SELF, &ru); 88320590Sdavidn MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru); 88420590Sdavidn gettimeofday (&tv, NULL); 88520590Sdavidn MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv); 88620590Sdavidn } while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000); 88720590Sdavidn MD5Final (ubuf, &md5_ctx); 88820590Sdavidn memcpy(buf+i, ubuf, MIN(16, len-n)); 88920590Sdavidn } 89020590Sdavidn return buf; 89120555Sdavidn} 89220253Sjoerg 89320590Sdavidn#else /* Portable version */ 89420590Sdavidn 89520590Sdavidnstatic u_char * 89620590Sdavidnpw_getrand(u_char *buf, int len) 89720590Sdavidn{ 89820590Sdavidn int i; 89920590Sdavidn 90020590Sdavidn for (i = 0; i < len; i++) { 90124214Sache unsigned long val = random(); 90220590Sdavidn /* Use all bits in the random value */ 90320590Sdavidn buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val); 90420590Sdavidn } 90520590Sdavidn return buf; 90620590Sdavidn} 90720590Sdavidn 90820590Sdavidn#endif 90920590Sdavidn 91020253Sjoergstatic char * 91120253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user) 91220253Sjoerg{ 91320253Sjoerg int i, l; 91420253Sjoerg char pwbuf[32]; 91520555Sdavidn u_char rndbuf[sizeof pwbuf]; 91620253Sjoerg 91720253Sjoerg switch (cnf->default_password) { 91820253Sjoerg case -1: /* Random password */ 91924214Sache if (!randinit) { 92024214Sache randinit = 1; 92124214Sache#ifdef __FreeBSD__ 92226626Sache srandomdev(); 92326626Sache#else 92426626Sache srandom((unsigned long) (time(NULL) ^ getpid())); 92524214Sache#endif 92624214Sache } 92720253Sjoerg l = (random() % 8 + 8); /* 8 - 16 chars */ 92820555Sdavidn pw_getrand(rndbuf, l); 92920253Sjoerg for (i = 0; i < l; i++) 93042245Sbillf pwbuf[i] = chars[rndbuf[i] % (sizeof(chars)-1)]; 93120253Sjoerg pwbuf[i] = '\0'; 93220253Sjoerg 93320253Sjoerg /* 93420253Sjoerg * We give this information back to the user 93520253Sjoerg */ 93620267Sjoerg if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) { 93720712Sdavidn if (isatty(1)) 93820712Sdavidn printf("Password for '%s' is: ", user); 93920253Sjoerg printf("%s\n", pwbuf); 94020253Sjoerg fflush(stdout); 94120253Sjoerg } 94220253Sjoerg break; 94320253Sjoerg 94420253Sjoerg case -2: /* No password at all! */ 94520253Sjoerg return ""; 94620253Sjoerg 94720253Sjoerg case 0: /* No login - default */ 94820253Sjoerg default: 94920253Sjoerg return "*"; 95020253Sjoerg 95120253Sjoerg case 1: /* user's name */ 95220253Sjoerg strncpy(pwbuf, user, sizeof pwbuf); 95320253Sjoerg pwbuf[sizeof pwbuf - 1] = '\0'; 95420253Sjoerg break; 95520253Sjoerg } 95620253Sjoerg return pw_pwcrypt(pwbuf); 95720253Sjoerg} 95820253Sjoerg 95920253Sjoerg 96020253Sjoergstatic int 96120253Sjoergprint_user(struct passwd * pwd, int pretty) 96220253Sjoerg{ 96320253Sjoerg if (!pretty) { 96420253Sjoerg char buf[_UC_MAXLINE]; 96520253Sjoerg 96620253Sjoerg fmtpwent(buf, pwd); 96720253Sjoerg fputs(buf, stdout); 96820253Sjoerg } else { 96920267Sjoerg int j; 97020253Sjoerg char *p; 97120253Sjoerg struct group *grp = getgrgid(pwd->pw_gid); 97220253Sjoerg char uname[60] = "User &", office[60] = "[None]", 97320253Sjoerg wphone[60] = "[None]", hphone[60] = "[None]"; 97420590Sdavidn char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 97520590Sdavidn struct tm * tptr; 97620253Sjoerg 97720253Sjoerg if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 97820253Sjoerg strncpy(uname, p, sizeof uname); 97920253Sjoerg uname[sizeof uname - 1] = '\0'; 98020253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 98120253Sjoerg strncpy(office, p, sizeof office); 98220253Sjoerg office[sizeof office - 1] = '\0'; 98320253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 98420253Sjoerg strncpy(wphone, p, sizeof wphone); 98520253Sjoerg wphone[sizeof wphone - 1] = '\0'; 98620253Sjoerg if ((p = strtok(NULL, "")) != NULL) { 98720253Sjoerg strncpy(hphone, p, sizeof hphone); 98820253Sjoerg hphone[sizeof hphone - 1] = '\0'; 98920253Sjoerg } 99020253Sjoerg } 99120253Sjoerg } 99220253Sjoerg } 99320253Sjoerg /* 99420253Sjoerg * Handle '&' in gecos field 99520253Sjoerg */ 99620253Sjoerg if ((p = strchr(uname, '&')) != NULL) { 99720253Sjoerg int l = strlen(pwd->pw_name); 99820253Sjoerg int m = strlen(p); 99920253Sjoerg 100020253Sjoerg memmove(p + l, p + 1, m); 100120253Sjoerg memmove(p, pwd->pw_name, l); 100220253Sjoerg *p = (char) toupper(*p); 100320253Sjoerg } 100420590Sdavidn if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 100520590Sdavidn strftime(acexpire, sizeof acexpire, "%c", tptr); 100620590Sdavidn if (pwd->pw_change > (time_t)9 && (tptr = localtime(&pwd->pw_change)) != NULL) 100720590Sdavidn strftime(pwexpire, sizeof pwexpire, "%c", tptr); 100822394Sdavidn printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n" 100920747Sdavidn " Full Name: %s\n" 101020747Sdavidn " Home: %-26.26s Class: %s\n" 101120747Sdavidn " Shell: %-26.26s Office: %s\n" 101220747Sdavidn "Work Phone: %-26.26s Home Phone: %s\n" 101320747Sdavidn "Acc Expire: %-26.26s Pwd Expire: %s\n", 101420253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 101520253Sjoerg grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 101620253Sjoerg uname, pwd->pw_dir, pwd->pw_class, 101720590Sdavidn pwd->pw_shell, office, wphone, hphone, 101820590Sdavidn acexpire, pwexpire); 101920267Sjoerg setgrent(); 102020267Sjoerg j = 0; 102120267Sjoerg while ((grp=getgrent()) != NULL) 102220267Sjoerg { 102320267Sjoerg int i = 0; 102420747Sdavidn while (grp->gr_mem[i] != NULL) 102520267Sjoerg { 102620267Sjoerg if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 102720267Sjoerg { 102820747Sdavidn printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 102920267Sjoerg break; 103020267Sjoerg } 103120267Sjoerg ++i; 103220267Sjoerg } 103320267Sjoerg } 103420267Sjoerg endgrent(); 103520267Sjoerg printf("%s\n", j ? "\n" : ""); 103620253Sjoerg } 103720267Sjoerg return EXIT_SUCCESS; 103820253Sjoerg} 103920253Sjoerg 104020679Sdavidnchar * 104120679Sdavidnpw_checkname(u_char *name, int gecos) 104220253Sjoerg{ 104320253Sjoerg int l = 0; 104421052Sdavidn char const *notch = gecos ? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\""; 104520253Sjoerg 104620253Sjoerg while (name[l]) { 104721052Sdavidn if (strchr(notch, name[l]) != NULL || name[l] < ' ' || name[l] == 127 || 104821052Sdavidn (!gecos && l==0 && name[l] == '-') || /* leading '-' */ 104921052Sdavidn (!gecos && name[l] & 0x80)) /* 8-bit */ 105030259Scharnier errx(EX_DATAERR, (name[l] >= ' ' && name[l] < 127) 105130259Scharnier ? "invalid character `%c' in field" 105230259Scharnier : "invalid character 0x%02x in field", 105320325Sjoerg name[l]); 105420253Sjoerg ++l; 105520253Sjoerg } 105622394Sdavidn if (!gecos && l > LOGNAMESIZE) 105730259Scharnier errx(EX_DATAERR, "name too long `%s'", name); 105820679Sdavidn return (char *)name; 105920253Sjoerg} 106020253Sjoerg 106120253Sjoerg 106220253Sjoergstatic void 106320253Sjoergrmat(uid_t uid) 106420253Sjoerg{ 106520253Sjoerg DIR *d = opendir("/var/at/jobs"); 106620253Sjoerg 106720253Sjoerg if (d != NULL) { 106820253Sjoerg struct dirent *e; 106920253Sjoerg 107020253Sjoerg while ((e = readdir(d)) != NULL) { 107120253Sjoerg struct stat st; 107220253Sjoerg 107320253Sjoerg if (strncmp(e->d_name, ".lock", 5) != 0 && 107420253Sjoerg stat(e->d_name, &st) == 0 && 107520253Sjoerg !S_ISDIR(st.st_mode) && 107620253Sjoerg st.st_uid == uid) { 107720253Sjoerg char tmp[MAXPATHLEN]; 107820253Sjoerg 107920253Sjoerg sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 108020253Sjoerg system(tmp); 108120253Sjoerg } 108220253Sjoerg } 108320253Sjoerg closedir(d); 108420253Sjoerg } 108520253Sjoerg} 108620747Sdavidn 108720747Sdavidnstatic void 108820747Sdavidnrmskey(char const * name) 108920747Sdavidn{ 109020747Sdavidn static const char etcskey[] = "/etc/skeykeys"; 109121052Sdavidn FILE *fp = fopen(etcskey, "r+"); 109220747Sdavidn 109321052Sdavidn if (fp != NULL) { 109421052Sdavidn char tmp[1024]; 109521052Sdavidn off_t atofs = 0; 109621052Sdavidn int length = strlen(name); 109720747Sdavidn 109821052Sdavidn while (fgets(tmp, sizeof tmp, fp) != NULL) { 109921052Sdavidn if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 110021052Sdavidn if (fseek(fp, atofs, SEEK_SET) == 0) { 110121052Sdavidn fwrite("#", 1, 1, fp); /* Comment username out */ 110221052Sdavidn } 110321052Sdavidn break; 110420747Sdavidn } 110521052Sdavidn atofs = ftell(fp); 110620747Sdavidn } 110721052Sdavidn /* 110821052Sdavidn * If we got an error of any sort, don't update! 110921052Sdavidn */ 111021052Sdavidn fclose(fp); 111120747Sdavidn } 111220747Sdavidn} 111320747Sdavidn 1114