pw_user.c revision 23318
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 * 2623299Sache * $Id: pw_user.c,v 1.15 1997/02/22 16:12:30 peter Exp $ 2720253Sjoerg */ 2820253Sjoerg 2920253Sjoerg#include <unistd.h> 3020253Sjoerg#include <fcntl.h> 3120253Sjoerg#include <ctype.h> 3220253Sjoerg#include <paths.h> 3320253Sjoerg#include <sys/param.h> 3420253Sjoerg#include <dirent.h> 3520253Sjoerg#include <termios.h> 3620555Sdavidn#include <sys/types.h> 3720555Sdavidn#include <sys/time.h> 3820555Sdavidn#include <sys/resource.h> 3922394Sdavidn#include <utmp.h> 4022394Sdavidn#if defined(USE_MD5RAND) 4120555Sdavidn#include <md5.h> 4222394Sdavidn#endif 4320253Sjoerg#include "pw.h" 4420253Sjoerg#include "bitmap.h" 4520253Sjoerg#include "pwupd.h" 4620253Sjoerg 4723318Sache#if (MAXLOGNAME-1) > UT_NAMESIZE 4822394Sdavidn#define LOGNAMESIZE UT_NAMESIZE 4922394Sdavidn#else 5023318Sache#define LOGNAMESIZE (MAXLOGNAME-1) 5122394Sdavidn#endif 5222394Sdavidn 5320253Sjoergstatic int print_user(struct passwd * pwd, int pretty); 5420253Sjoergstatic uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args); 5520253Sjoergstatic uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer); 5620253Sjoergstatic time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 5720253Sjoergstatic time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 5820253Sjoergstatic char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 5920253Sjoergstatic char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 6020253Sjoergstatic char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 6120253Sjoergstatic char *shell_path(char const * path, char *shells[], char *sh); 6220253Sjoergstatic void rmat(uid_t uid); 6320747Sdavidnstatic void rmskey(char const * name); 6420253Sjoerg 6520253Sjoerg/*- 6620253Sjoerg * -C config configuration file 6720253Sjoerg * -q quiet operation 6820253Sjoerg * -n name login name 6920253Sjoerg * -u uid user id 7020253Sjoerg * -c comment user name/comment 7120253Sjoerg * -d directory home directory 7220253Sjoerg * -e date account expiry date 7320253Sjoerg * -p date password expiry date 7420253Sjoerg * -g grp primary group 7520253Sjoerg * -G grp1,grp2 additional groups 7620253Sjoerg * -m [ -k dir ] create and set up home 7720253Sjoerg * -s shell name of login shell 7820253Sjoerg * -o duplicate uid ok 7920253Sjoerg * -L class user class 8020253Sjoerg * -l name new login name 8120253Sjoerg * -h fd password filehandle 8220253Sjoerg * -F force print or add 8320253Sjoerg * Setting defaults: 8420253Sjoerg * -D set user defaults 8520253Sjoerg * -b dir default home root dir 8620253Sjoerg * -e period default expiry period 8720253Sjoerg * -p period default password change period 8820253Sjoerg * -g group default group 8920253Sjoerg * -G grp1,grp2.. default additional groups 9020253Sjoerg * -L class default login class 9120253Sjoerg * -k dir default home skeleton 9220253Sjoerg * -s shell default shell 9320253Sjoerg * -w method default password method 9420253Sjoerg */ 9520253Sjoerg 9620253Sjoergint 9720253Sjoergpw_user(struct userconf * cnf, int mode, struct cargs * args) 9820253Sjoerg{ 9921330Sdavidn int r, r1; 10020253Sjoerg char *p = NULL; 10120253Sjoerg struct carg *a_name; 10220253Sjoerg struct carg *a_uid; 10320253Sjoerg struct carg *arg; 10420253Sjoerg struct passwd *pwd = NULL; 10520253Sjoerg struct group *grp; 10620253Sjoerg struct stat st; 10720747Sdavidn char line[_PASSWORD_LEN+1]; 10820253Sjoerg 10920253Sjoerg static struct passwd fakeuser = 11020253Sjoerg { 11120253Sjoerg NULL, 11220253Sjoerg "*", 11320253Sjoerg -1, 11420253Sjoerg -1, 11520253Sjoerg 0, 11620253Sjoerg "", 11720253Sjoerg "User &", 11820253Sjoerg "/bin/sh", 11920253Sjoerg 0, 12020253Sjoerg 0 12120253Sjoerg }; 12220253Sjoerg 12320253Sjoerg /* 12420267Sjoerg * With M_NEXT, we only need to return the 12520267Sjoerg * next uid to stdout 12620267Sjoerg */ 12720267Sjoerg if (mode == M_NEXT) 12820267Sjoerg { 12920267Sjoerg uid_t next = pw_uidpolicy(cnf, args); 13020267Sjoerg if (getarg(args, 'q')) 13120267Sjoerg return next; 13220267Sjoerg printf("%ld:", (long)next); 13320267Sjoerg pw_group(cnf, mode, args); 13420267Sjoerg return EXIT_SUCCESS; 13520267Sjoerg } 13620267Sjoerg 13720267Sjoerg /* 13820253Sjoerg * We can do all of the common legwork here 13920253Sjoerg */ 14020253Sjoerg 14120253Sjoerg if ((arg = getarg(args, 'b')) != NULL) { 14220267Sjoerg cnf->home = arg->val; 14320253Sjoerg } 14421052Sdavidn 14521052Sdavidn /* 14621052Sdavidn * If we'll need to use it or we're updating it, 14721052Sdavidn * then create the base home directory if necessary 14821052Sdavidn */ 14921052Sdavidn if (arg != NULL || getarg(args, 'm') != NULL) { 15021052Sdavidn int l = strlen(cnf->home); 15121052Sdavidn 15221052Sdavidn if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 15321052Sdavidn cnf->home[--l] = '\0'; 15421052Sdavidn 15521052Sdavidn if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 15621052Sdavidn cmderr(EX_DATAERR, "invalid base directory for home '%s'\n", cnf->home); 15721052Sdavidn 15821052Sdavidn if (stat(cnf->home, &st) == -1) { 15921052Sdavidn char dbuf[MAXPATHLEN]; 16021052Sdavidn 16121242Sdavidn /* 16221242Sdavidn * This is a kludge especially for Joerg :) 16321242Sdavidn * If the home directory would be created in the root partition, then 16421242Sdavidn * we really create it under /usr which is likely to have more space. 16521242Sdavidn * But we create a symlink from cnf->home -> "/usr" -> cnf->home 16621242Sdavidn */ 16721242Sdavidn if (strchr(cnf->home+1, '/') == NULL) { 16821242Sdavidn strcpy(dbuf, "/usr"); 16921242Sdavidn strncat(dbuf, cnf->home, MAXPATHLEN-5); 17021242Sdavidn if (mkdir(dbuf, 0755) != -1 || errno == EEXIST) { 17121242Sdavidn chown(dbuf, 0, 0); 17221242Sdavidn symlink(dbuf, cnf->home); 17321242Sdavidn } 17421242Sdavidn /* If this falls, fall back to old method */ 17521242Sdavidn } 17621052Sdavidn p = strncpy(dbuf, cnf->home, sizeof dbuf); 17721052Sdavidn dbuf[MAXPATHLEN-1] = '\0'; 17821242Sdavidn if (stat(dbuf, &st) == -1) { 17921242Sdavidn while ((p = strchr(++p, '/')) != NULL) { 18021242Sdavidn *p = '\0'; 18121242Sdavidn if (stat(dbuf, &st) == -1) { 18221242Sdavidn if (mkdir(dbuf, 0755) == -1) 18321242Sdavidn goto direrr; 18421242Sdavidn chown(dbuf, 0, 0); 18521242Sdavidn } else if (!S_ISDIR(st.st_mode)) 18621242Sdavidn cmderr(EX_OSFILE, "'%s' (root home parent) is not a directory\n", dbuf); 18721242Sdavidn *p = '/'; 18821242Sdavidn } 18921052Sdavidn } 19021242Sdavidn if (stat(dbuf, &st) == -1) { 19121052Sdavidn if (mkdir(dbuf, 0755) == -1) { 19221052Sdavidn direrr: cmderr(EX_OSFILE, "mkdir '%s': %s\n", dbuf, strerror(errno)); 19321052Sdavidn } 19421052Sdavidn chown(dbuf, 0, 0); 19521052Sdavidn } 19621052Sdavidn } else if (!S_ISDIR(st.st_mode)) 19721052Sdavidn cmderr(EX_OSFILE, "root home `%s' is not a directory\n", cnf->home); 19821052Sdavidn } 19921052Sdavidn 20021052Sdavidn 20120253Sjoerg if ((arg = getarg(args, 'e')) != NULL) 20220253Sjoerg cnf->expire_days = atoi(arg->val); 20320253Sjoerg 20421330Sdavidn if ((arg = getarg(args, 'y')) != NULL) 20521330Sdavidn cnf->nispasswd = arg->val; 20621330Sdavidn 20720253Sjoerg if ((arg = getarg(args, 'p')) != NULL && arg->val) 20820253Sjoerg cnf->password_days = atoi(arg->val); 20920253Sjoerg 21020253Sjoerg if ((arg = getarg(args, 'g')) != NULL) { 21120253Sjoerg p = arg->val; 21220253Sjoerg if ((grp = getgrnam(p)) == NULL) { 21320253Sjoerg if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL) 21420267Sjoerg cmderr(EX_NOUSER, "group `%s' does not exist\n", p); 21520253Sjoerg } 21620253Sjoerg cnf->default_group = newstr(grp->gr_name); 21720253Sjoerg } 21820253Sjoerg if ((arg = getarg(args, 'L')) != NULL) 21920679Sdavidn cnf->default_class = pw_checkname((u_char *)arg->val, 0); 22020253Sjoerg 22120253Sjoerg if ((arg = getarg(args, 'G')) != NULL && arg->val) { 22220253Sjoerg int i = 0; 22320253Sjoerg 22420747Sdavidn for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 22520253Sjoerg if ((grp = getgrnam(p)) == NULL) { 22620253Sjoerg if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL) 22720267Sjoerg cmderr(EX_NOUSER, "group `%s' does not exist\n", p); 22820253Sjoerg } 22920747Sdavidn if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 23020747Sdavidn cnf->groups[i++] = newstr(grp->gr_name); 23120253Sjoerg } 23220747Sdavidn while (i < cnf->numgroups) 23320253Sjoerg cnf->groups[i++] = NULL; 23420253Sjoerg } 23520253Sjoerg if ((arg = getarg(args, 'k')) != NULL) { 23620253Sjoerg if (stat(cnf->dotdir = arg->val, &st) == -1 || S_ISDIR(st.st_mode)) 23720267Sjoerg cmderr(EX_OSFILE, "skeleton `%s' is not a directory or does not exist\n", cnf->dotdir); 23820253Sjoerg } 23920253Sjoerg if ((arg = getarg(args, 's')) != NULL) 24020253Sjoerg cnf->shell_default = arg->val; 24120253Sjoerg 24220253Sjoerg if (mode == M_ADD && getarg(args, 'D')) { 24320253Sjoerg if (getarg(args, 'n') != NULL) 24420267Sjoerg cmderr(EX_DATAERR, "can't combine `-D' with `-n name'\n"); 24520253Sjoerg if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 24620253Sjoerg if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 24720253Sjoerg cnf->min_uid = 1000; 24820253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 24920253Sjoerg cnf->max_uid = 32000; 25020253Sjoerg } 25120253Sjoerg if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 25220253Sjoerg if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 25320253Sjoerg cnf->min_gid = 1000; 25420253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 25520253Sjoerg cnf->max_gid = 32000; 25620253Sjoerg } 25720253Sjoerg if ((arg = getarg(args, 'w')) != NULL) 25820253Sjoerg cnf->default_password = boolean_val(arg->val, cnf->default_password); 25920253Sjoerg 26020253Sjoerg arg = getarg(args, 'C'); 26120253Sjoerg if (write_userconfig(arg ? arg->val : NULL)) 26220267Sjoerg return EXIT_SUCCESS; 26320253Sjoerg perror("config update"); 26420267Sjoerg return EX_IOERR; 26520253Sjoerg } 26620253Sjoerg if (mode == M_PRINT && getarg(args, 'a')) { 26720267Sjoerg int pretty = getarg(args, 'P') != NULL; 26820253Sjoerg 26920253Sjoerg setpwent(); 27020253Sjoerg while ((pwd = getpwent()) != NULL) 27120253Sjoerg print_user(pwd, pretty); 27220253Sjoerg endpwent(); 27320267Sjoerg return EXIT_SUCCESS; 27420253Sjoerg } 27520253Sjoerg if ((a_name = getarg(args, 'n')) != NULL) 27620679Sdavidn pwd = getpwnam(pw_checkname((u_char *)a_name->val, 0)); 27720253Sjoerg a_uid = getarg(args, 'u'); 27820253Sjoerg 27920253Sjoerg if (a_uid == NULL) { 28020253Sjoerg if (a_name == NULL) 28120267Sjoerg cmderr(EX_DATAERR, "user name or id required\n"); 28220253Sjoerg 28320253Sjoerg /* 28420253Sjoerg * Determine whether 'n' switch is name or uid - we don't 28520253Sjoerg * really don't really care which we have, but we need to 28620253Sjoerg * know. 28720253Sjoerg */ 28820253Sjoerg if (mode != M_ADD && pwd == NULL && isdigit(*a_name->val) && atoi(a_name->val) > 0) { /* Assume uid */ 28920253Sjoerg (a_uid = a_name)->ch = 'u'; 29020253Sjoerg a_name = NULL; 29120253Sjoerg } 29220253Sjoerg } 29320253Sjoerg /* 29420253Sjoerg * Update, delete & print require that the user exists 29520253Sjoerg */ 29620253Sjoerg if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) { 29720253Sjoerg if (a_name == NULL && pwd == NULL) /* Try harder */ 29820253Sjoerg pwd = getpwuid(atoi(a_uid->val)); 29920253Sjoerg 30020253Sjoerg if (pwd == NULL) { 30120253Sjoerg if (mode == M_PRINT && getarg(args, 'F')) { 30220253Sjoerg fakeuser.pw_name = a_name ? a_name->val : "nouser"; 30320253Sjoerg fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 30420267Sjoerg return print_user(&fakeuser, getarg(args, 'P') != NULL); 30520253Sjoerg } 30620253Sjoerg if (a_name == NULL) 30720267Sjoerg cmderr(EX_NOUSER, "no such uid `%s'\n", a_uid->val); 30820267Sjoerg cmderr(EX_NOUSER, "no such user `%s'\n", a_name->val); 30920253Sjoerg } 31020253Sjoerg if (a_name == NULL) /* May be needed later */ 31120253Sjoerg a_name = addarg(args, 'n', newstr(pwd->pw_name)); 31220253Sjoerg 31320253Sjoerg /* 31420253Sjoerg * Handle deletions now 31520253Sjoerg */ 31620253Sjoerg if (mode == M_DELETE) { 31720253Sjoerg char file[MAXPATHLEN]; 31820253Sjoerg char home[MAXPATHLEN]; 31920253Sjoerg uid_t uid = pwd->pw_uid; 32020253Sjoerg 32120253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 32220267Sjoerg cmderr(EX_DATAERR, "cannot remove user 'root'\n"); 32320253Sjoerg 32420253Sjoerg /* 32520747Sdavidn * Remove skey record from /etc/skeykeys 32620747Sdavidn */ 32720747Sdavidn 32820747Sdavidn rmskey(pwd->pw_name); 32920747Sdavidn 33020747Sdavidn /* 33120253Sjoerg * Remove crontabs 33220253Sjoerg */ 33320253Sjoerg sprintf(file, "/var/cron/tabs/%s", pwd->pw_name); 33420253Sjoerg if (access(file, F_OK) == 0) { 33520253Sjoerg sprintf(file, "crontab -u %s -r", pwd->pw_name); 33620253Sjoerg system(file); 33720253Sjoerg } 33820253Sjoerg /* 33920253Sjoerg * Save these for later, since contents of pwd may be 34020253Sjoerg * invalidated by deletion 34120253Sjoerg */ 34220253Sjoerg sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 34320253Sjoerg strncpy(home, pwd->pw_dir, sizeof home); 34420253Sjoerg home[sizeof home - 1] = '\0'; 34520253Sjoerg 34620253Sjoerg if (!delpwent(pwd)) 34720267Sjoerg cmderr(EX_IOERR, "Error updating passwd file: %s\n", strerror(errno)); 34821330Sdavidn 34921330Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/' && !delnispwent(cnf->nispasswd, a_name->val)) 35021330Sdavidn perror("WARNING: NIS passwd update"); 35121330Sdavidn 35220253Sjoerg editgroups(a_name->val, NULL); 35320253Sjoerg 35420253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 35520253Sjoerg 35620253Sjoerg /* 35720253Sjoerg * Remove mail file 35820253Sjoerg */ 35920253Sjoerg remove(file); 36020253Sjoerg 36120253Sjoerg /* 36220253Sjoerg * Remove at jobs 36320253Sjoerg */ 36420253Sjoerg if (getpwuid(uid) == NULL) 36520253Sjoerg rmat(uid); 36620253Sjoerg 36720253Sjoerg /* 36820253Sjoerg * Remove home directory and contents 36920253Sjoerg */ 37020253Sjoerg if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 37120253Sjoerg if (stat(home, &st) != -1) { 37220253Sjoerg rm_r(home, uid); 37320253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 37420253Sjoerg a_name->val, (long) uid, home, 37520253Sjoerg stat(home, &st) == -1 ? "" : "not completely "); 37620253Sjoerg } 37720253Sjoerg } 37820267Sjoerg return EXIT_SUCCESS; 37920253Sjoerg } else if (mode == M_PRINT) 38020267Sjoerg return print_user(pwd, getarg(args, 'P') != NULL); 38120253Sjoerg 38220253Sjoerg /* 38320253Sjoerg * The rest is edit code 38420253Sjoerg */ 38520253Sjoerg if ((arg = getarg(args, 'l')) != NULL) { 38620253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 38720267Sjoerg cmderr(EX_DATAERR, "can't rename `root' account\n"); 38820679Sdavidn pwd->pw_name = pw_checkname((u_char *)arg->val, 0); 38920253Sjoerg } 39020253Sjoerg if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) { 39120253Sjoerg pwd->pw_uid = (uid_t) atol(arg->val); 39220253Sjoerg if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 39320267Sjoerg cmderr(EX_DATAERR, "can't change uid of `root' account\n"); 39420253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 39520253Sjoerg fprintf(stderr, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd->pw_name); 39620253Sjoerg } 39720253Sjoerg if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) /* Already checked this */ 39820253Sjoerg pwd->pw_gid = (gid_t) getgrnam(cnf->default_group)->gr_gid; 39920253Sjoerg 40020253Sjoerg if ((arg = getarg(args, 'p')) != NULL) { 40120253Sjoerg if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) 40220253Sjoerg pwd->pw_change = 0; 40320253Sjoerg else { 40420253Sjoerg time_t now = time(NULL); 40520253Sjoerg time_t expire = parse_date(now, arg->val); 40620253Sjoerg 40720253Sjoerg if (now == expire) 40820267Sjoerg cmderr(EX_DATAERR, "Invalid password change date `%s'\n", arg->val); 40920253Sjoerg pwd->pw_change = expire; 41020253Sjoerg } 41120253Sjoerg } 41220267Sjoerg if ((arg = getarg(args, 'e')) != NULL) { 41320253Sjoerg if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) 41420253Sjoerg pwd->pw_expire = 0; 41520253Sjoerg else { 41620253Sjoerg time_t now = time(NULL); 41720253Sjoerg time_t expire = parse_date(now, arg->val); 41820253Sjoerg 41920253Sjoerg if (now == expire) 42020267Sjoerg cmderr(EX_DATAERR, "Invalid account expiry date `%s'\n", arg->val); 42120253Sjoerg pwd->pw_expire = expire; 42220253Sjoerg } 42320253Sjoerg } 42420253Sjoerg if ((arg = getarg(args, 's')) != NULL) 42520253Sjoerg pwd->pw_shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 42620253Sjoerg 42720253Sjoerg if (getarg(args, 'L')) 42820253Sjoerg pwd->pw_class = cnf->default_class; 42920253Sjoerg 43020747Sdavidn if ((arg = getarg(args, 'd')) != NULL) { 43120747Sdavidn if (stat(pwd->pw_dir = arg->val, &st) == -1) { 43220747Sdavidn if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 43320747Sdavidn fprintf(stderr, "WARNING: home `%s' does not exist\n", pwd->pw_dir); 43420747Sdavidn } else if (!S_ISDIR(st.st_mode)) 43520747Sdavidn fprintf(stderr, "WARNING: home `%s' is not a directory\n", pwd->pw_dir); 43620747Sdavidn } 43720747Sdavidn 43820267Sjoerg if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL) 43920267Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 44020267Sjoerg 44120253Sjoerg } else { 44220253Sjoerg if (a_name == NULL) /* Required */ 44320267Sjoerg cmderr(EX_DATAERR, "login name required\n"); 44420253Sjoerg else if ((pwd = getpwnam(a_name->val)) != NULL) /* Exists */ 44520267Sjoerg cmderr(EX_DATAERR, "login name `%s' already exists\n", a_name->val); 44620253Sjoerg 44720253Sjoerg /* 44820253Sjoerg * Now, set up defaults for a new user 44920253Sjoerg */ 45020253Sjoerg pwd = &fakeuser; 45120253Sjoerg pwd->pw_name = a_name->val; 45220253Sjoerg pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 45320253Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 45420253Sjoerg pwd->pw_uid = pw_uidpolicy(cnf, args); 45520253Sjoerg pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 45620253Sjoerg pwd->pw_change = pw_pwdpolicy(cnf, args); 45720253Sjoerg pwd->pw_expire = pw_exppolicy(cnf, args); 45820253Sjoerg pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 45920253Sjoerg pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 46020253Sjoerg 46120253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 46220253Sjoerg fprintf(stderr, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd->pw_name); 46320253Sjoerg } 46420253Sjoerg 46520253Sjoerg /* 46620253Sjoerg * Shared add/edit code 46720253Sjoerg */ 46820253Sjoerg if ((arg = getarg(args, 'c')) != NULL) 46920679Sdavidn pwd->pw_gecos = pw_checkname((u_char *)arg->val, 1); 47020253Sjoerg 47120253Sjoerg if ((arg = getarg(args, 'h')) != NULL) { 47220253Sjoerg if (strcmp(arg->val, "-") == 0) 47320253Sjoerg pwd->pw_passwd = "*"; /* No access */ 47420253Sjoerg else { 47520253Sjoerg int fd = atoi(arg->val); 47620253Sjoerg int b; 47720253Sjoerg int istty = isatty(fd); 47820253Sjoerg struct termios t; 47920253Sjoerg 48020253Sjoerg if (istty) { 48120253Sjoerg if (tcgetattr(fd, &t) == -1) 48220253Sjoerg istty = 0; 48320253Sjoerg else { 48420253Sjoerg struct termios n = t; 48520253Sjoerg 48620253Sjoerg /* Disable echo */ 48720253Sjoerg n.c_lflag &= ~(ECHO); 48820253Sjoerg tcsetattr(fd, TCSANOW, &n); 48920253Sjoerg printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name); 49020253Sjoerg fflush(stdout); 49120253Sjoerg } 49220253Sjoerg } 49320253Sjoerg b = read(fd, line, sizeof(line) - 1); 49420253Sjoerg if (istty) { /* Restore state */ 49520253Sjoerg tcsetattr(fd, TCSANOW, &t); 49620253Sjoerg fputc('\n', stdout); 49720253Sjoerg fflush(stdout); 49820253Sjoerg } 49920253Sjoerg if (b < 0) { 50020253Sjoerg perror("-h file descriptor"); 50120267Sjoerg return EX_IOERR; 50220253Sjoerg } 50320253Sjoerg line[b] = '\0'; 50420253Sjoerg if ((p = strpbrk(line, " \t\r\n")) != NULL) 50520253Sjoerg *p = '\0'; 50620253Sjoerg if (!*line) 50720267Sjoerg cmderr(EX_DATAERR, "empty password read on file descriptor %d\n", fd); 50820253Sjoerg pwd->pw_passwd = pw_pwcrypt(line); 50920253Sjoerg } 51020253Sjoerg } 51120267Sjoerg 51220267Sjoerg /* 51320267Sjoerg * Special case: -N only displays & exits 51420267Sjoerg */ 51520267Sjoerg if (getarg(args, 'N') != NULL) 51620267Sjoerg return print_user(pwd, getarg(args, 'P') != NULL); 51720267Sjoerg 51821330Sdavidn r = r1 = 1; 51921330Sdavidn if (mode == M_ADD) { 52021330Sdavidn r = addpwent(pwd); 52121330Sdavidn if (r && cnf->nispasswd && *cnf->nispasswd=='/') 52221330Sdavidn r1 = addnispwent(cnf->nispasswd, pwd); 52321330Sdavidn } else if (mode == M_UPDATE) { 52421330Sdavidn r = chgpwent(a_name->val, pwd); 52521330Sdavidn if (r && cnf->nispasswd && *cnf->nispasswd=='/') 52621330Sdavidn r1 = chgnispwent(cnf->nispasswd, a_name->val, pwd); 52721330Sdavidn } 52821330Sdavidn 52921330Sdavidn if (!r) { 53020253Sjoerg perror("password update"); 53120267Sjoerg return EX_IOERR; 53221330Sdavidn } else if (!r1) { 53321330Sdavidn perror("WARNING: NIS password update"); 53421330Sdavidn /* Keep on trucking */ 53520253Sjoerg } 53621330Sdavidn 53720253Sjoerg /* 53820253Sjoerg * Ok, user is created or changed - now edit group file 53920253Sjoerg */ 54020253Sjoerg 54120253Sjoerg if (mode == M_ADD || getarg(args, 'G') != NULL) 54220253Sjoerg editgroups(pwd->pw_name, cnf->groups); 54320253Sjoerg 54420253Sjoerg /* pwd may have been invalidated */ 54520253Sjoerg if ((pwd = getpwnam(a_name->val)) == NULL) 54620267Sjoerg cmderr(EX_NOUSER, "user '%s' disappeared during update\n", a_name->val); 54720253Sjoerg 54820253Sjoerg grp = getgrgid(pwd->pw_gid); 54920253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s", 55020253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 55120253Sjoerg grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 55220253Sjoerg pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 55320253Sjoerg 55420253Sjoerg /* 55520253Sjoerg * If adding, let's touch and chown the user's mail file. This is not 55620253Sjoerg * strictly necessary under BSD with a 0755 maildir but it also 55720253Sjoerg * doesn't hurt anything to create the empty mailfile 55820253Sjoerg */ 55920253Sjoerg if (mode == M_ADD) { 56020253Sjoerg FILE *fp; 56120253Sjoerg 56220253Sjoerg sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 56320253Sjoerg close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 56420253Sjoerg * mtime */ 56520253Sjoerg chown(line, pwd->pw_uid, pwd->pw_gid); 56620253Sjoerg 56720253Sjoerg /* 56820253Sjoerg * Send mail to the new user as well, if we are asked to 56920253Sjoerg */ 57020253Sjoerg if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 57120253Sjoerg FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 57220253Sjoerg 57320253Sjoerg if (pfp == NULL) 57420253Sjoerg perror("sendmail"); 57520253Sjoerg else { 57620253Sjoerg fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 57720253Sjoerg while (fgets(line, sizeof(line), fp) != NULL) { 57820253Sjoerg /* Do substitutions? */ 57920253Sjoerg fputs(line, pfp); 58020253Sjoerg } 58120253Sjoerg pclose(pfp); 58220253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 58320253Sjoerg pwd->pw_name, (long) pwd->pw_uid); 58420253Sjoerg } 58520253Sjoerg fclose(fp); 58620253Sjoerg } 58720253Sjoerg } 58820253Sjoerg /* 58920253Sjoerg * Finally, let's create and populate the user's home directory. Note 59020253Sjoerg * that this also `works' for editing users if -m is used, but 59120253Sjoerg * existing files will *not* be overwritten. 59220253Sjoerg */ 59320253Sjoerg if (getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 59420253Sjoerg copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid); 59520253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 59620253Sjoerg pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 59720253Sjoerg } 59820267Sjoerg return EXIT_SUCCESS; 59920253Sjoerg} 60020253Sjoerg 60120253Sjoerg 60220253Sjoergstatic uid_t 60320253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args) 60420253Sjoerg{ 60520253Sjoerg struct passwd *pwd; 60620253Sjoerg uid_t uid = (uid_t) - 1; 60720253Sjoerg struct carg *a_uid = getarg(args, 'u'); 60820253Sjoerg 60920253Sjoerg /* 61020253Sjoerg * Check the given uid, if any 61120253Sjoerg */ 61220253Sjoerg if (a_uid != NULL) { 61320253Sjoerg uid = (uid_t) atol(a_uid->val); 61420253Sjoerg 61520253Sjoerg if ((pwd = getpwuid(uid)) != NULL && getarg(args, 'o') == NULL) 61620267Sjoerg cmderr(EX_DATAERR, "uid `%ld' has already been allocated\n", (long) pwd->pw_uid); 61720253Sjoerg } else { 61820253Sjoerg struct bitmap bm; 61920253Sjoerg 62020253Sjoerg /* 62120253Sjoerg * We need to allocate the next available uid under one of 62220253Sjoerg * two policies a) Grab the first unused uid b) Grab the 62320253Sjoerg * highest possible unused uid 62420253Sjoerg */ 62520253Sjoerg if (cnf->min_uid >= cnf->max_uid) { /* Sanity 62620253Sjoerg * claus^H^H^H^Hheck */ 62720253Sjoerg cnf->min_uid = 1000; 62820253Sjoerg cnf->max_uid = 32000; 62920253Sjoerg } 63020253Sjoerg bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 63120253Sjoerg 63220253Sjoerg /* 63320253Sjoerg * Now, let's fill the bitmap from the password file 63420253Sjoerg */ 63520253Sjoerg setpwent(); 63620253Sjoerg while ((pwd = getpwent()) != NULL) 63720253Sjoerg if (pwd->pw_uid >= (int) cnf->min_uid && pwd->pw_uid <= (int) cnf->max_uid) 63820253Sjoerg bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 63920253Sjoerg endpwent(); 64020253Sjoerg 64120253Sjoerg /* 64220253Sjoerg * Then apply the policy, with fallback to reuse if necessary 64320253Sjoerg */ 64420253Sjoerg if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 64520253Sjoerg uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 64620253Sjoerg 64720253Sjoerg /* 64820253Sjoerg * Another sanity check 64920253Sjoerg */ 65020253Sjoerg if (uid < cnf->min_uid || uid > cnf->max_uid) 65120267Sjoerg cmderr(EX_SOFTWARE, "unable to allocate a new uid - range fully used\n"); 65220253Sjoerg bm_dealloc(&bm); 65320253Sjoerg } 65420253Sjoerg return uid; 65520253Sjoerg} 65620253Sjoerg 65720253Sjoerg 65820253Sjoergstatic uid_t 65920253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 66020253Sjoerg{ 66120253Sjoerg struct group *grp; 66220253Sjoerg gid_t gid = (uid_t) - 1; 66320253Sjoerg struct carg *a_gid = getarg(args, 'g'); 66420253Sjoerg 66520253Sjoerg /* 66620253Sjoerg * If no arg given, see if default can help out 66720253Sjoerg */ 66820253Sjoerg if (a_gid == NULL && cnf->default_group && *cnf->default_group) 66920253Sjoerg a_gid = addarg(args, 'g', cnf->default_group); 67020253Sjoerg 67120253Sjoerg /* 67220253Sjoerg * Check the given gid, if any 67320253Sjoerg */ 67420267Sjoerg setgrent(); 67520253Sjoerg if (a_gid != NULL) { 67620253Sjoerg if ((grp = getgrnam(a_gid->val)) == NULL) { 67720253Sjoerg gid = (gid_t) atol(a_gid->val); 67820253Sjoerg if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = getgrgid(gid)) == NULL) 67920267Sjoerg cmderr(EX_NOUSER, "group `%s' is not defined\n", a_gid->val); 68020253Sjoerg } 68120253Sjoerg gid = grp->gr_gid; 68220267Sjoerg } else if ((grp = getgrnam(nam)) != NULL && grp->gr_mem[0] == NULL) { 68320267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 68420253Sjoerg } else { 68520253Sjoerg struct cargs grpargs; 68620253Sjoerg char tmp[32]; 68720253Sjoerg 68820253Sjoerg LIST_INIT(&grpargs); 68920253Sjoerg addarg(&grpargs, 'n', nam); 69020253Sjoerg 69120253Sjoerg /* 69220253Sjoerg * We need to auto-create a group with the user's name. We 69320253Sjoerg * can send all the appropriate output to our sister routine 69420253Sjoerg * bit first see if we can create a group with gid==uid so we 69520253Sjoerg * can keep the user and group ids in sync. We purposely do 69620253Sjoerg * NOT check the gid range if we can force the sync. If the 69720253Sjoerg * user's name dups an existing group, then the group add 69820253Sjoerg * function will happily handle that case for us and exit. 69920253Sjoerg */ 70020253Sjoerg if (getgrgid(prefer) == NULL) { 70120253Sjoerg sprintf(tmp, "%lu", (unsigned long) prefer); 70220253Sjoerg addarg(&grpargs, 'g', tmp); 70320253Sjoerg } 70420267Sjoerg if (getarg(args, 'N')) 70520267Sjoerg { 70620267Sjoerg addarg(&grpargs, 'N', NULL); 70720267Sjoerg addarg(&grpargs, 'q', NULL); 70820267Sjoerg gid = pw_group(cnf, M_NEXT, &grpargs); 70920267Sjoerg } 71020267Sjoerg else 71120267Sjoerg { 71220267Sjoerg pw_group(cnf, M_ADD, &grpargs); 71320267Sjoerg if ((grp = getgrnam(nam)) != NULL) 71420267Sjoerg gid = grp->gr_gid; 71520267Sjoerg } 71620253Sjoerg a_gid = grpargs.lh_first; 71720253Sjoerg while (a_gid != NULL) { 71820253Sjoerg struct carg *t = a_gid->list.le_next; 71920253Sjoerg LIST_REMOVE(a_gid, list); 72020253Sjoerg a_gid = t; 72120253Sjoerg } 72220253Sjoerg } 72320267Sjoerg endgrent(); 72420253Sjoerg return gid; 72520253Sjoerg} 72620253Sjoerg 72720253Sjoerg 72820253Sjoergstatic time_t 72920253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args) 73020253Sjoerg{ 73120253Sjoerg time_t result = 0; 73220253Sjoerg time_t now = time(NULL); 73320253Sjoerg struct carg *arg = getarg(args, 'e'); 73420253Sjoerg 73520253Sjoerg if (arg != NULL) { 73620253Sjoerg if ((result = parse_date(now, arg->val)) == now) 73720267Sjoerg cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val); 73820253Sjoerg } else if (cnf->password_days > 0) 73920253Sjoerg result = now + ((long) cnf->password_days * 86400L); 74020253Sjoerg return result; 74120253Sjoerg} 74220253Sjoerg 74320253Sjoerg 74420253Sjoergstatic time_t 74520253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args) 74620253Sjoerg{ 74720253Sjoerg time_t result = 0; 74820253Sjoerg time_t now = time(NULL); 74920253Sjoerg struct carg *arg = getarg(args, 'e'); 75020253Sjoerg 75120253Sjoerg if (arg != NULL) { 75220253Sjoerg if ((result = parse_date(now, arg->val)) == now) 75320267Sjoerg cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val); 75420253Sjoerg } else if (cnf->expire_days > 0) 75520253Sjoerg result = now + ((long) cnf->expire_days * 86400L); 75620253Sjoerg return result; 75720253Sjoerg} 75820253Sjoerg 75920253Sjoerg 76020253Sjoergstatic char * 76120253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 76220253Sjoerg{ 76320253Sjoerg struct carg *arg = getarg(args, 'd'); 76420253Sjoerg 76520253Sjoerg if (arg) 76620253Sjoerg return arg->val; 76720253Sjoerg else { 76820253Sjoerg static char home[128]; 76920253Sjoerg 77020253Sjoerg if (cnf->home == NULL || *cnf->home == '\0') 77120267Sjoerg cmderr(EX_CONFIG, "no base home directory set\n"); 77220253Sjoerg sprintf(home, "%s/%s", cnf->home, user); 77320253Sjoerg return home; 77420253Sjoerg } 77520253Sjoerg} 77620253Sjoerg 77720253Sjoergstatic char * 77820253Sjoergshell_path(char const * path, char *shells[], char *sh) 77920253Sjoerg{ 78020253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 78120253Sjoerg return sh; /* specified full path or forced none */ 78220253Sjoerg else { 78320253Sjoerg char *p; 78420253Sjoerg char paths[_UC_MAXLINE]; 78520253Sjoerg 78620253Sjoerg /* 78720253Sjoerg * We need to search paths 78820253Sjoerg */ 78920253Sjoerg strncpy(paths, path, sizeof paths); 79020253Sjoerg paths[sizeof paths - 1] = '\0'; 79120253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 79220253Sjoerg int i; 79320253Sjoerg static char shellpath[256]; 79420253Sjoerg 79520253Sjoerg if (sh != NULL) { 79620253Sjoerg sprintf(shellpath, "%s/%s", p, sh); 79720253Sjoerg if (access(shellpath, X_OK) == 0) 79820253Sjoerg return shellpath; 79920253Sjoerg } else 80020253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 80120253Sjoerg sprintf(shellpath, "%s/%s", p, shells[i]); 80220253Sjoerg if (access(shellpath, X_OK) == 0) 80320253Sjoerg return shellpath; 80420253Sjoerg } 80520253Sjoerg } 80620253Sjoerg if (sh == NULL) 80720267Sjoerg cmderr(EX_OSFILE, "can't find shell `%s' in shell paths\n", sh); 80820267Sjoerg cmderr(EX_CONFIG, "no default shell available or defined\n"); 80920253Sjoerg return NULL; 81020253Sjoerg } 81120253Sjoerg} 81220253Sjoerg 81320253Sjoerg 81420253Sjoergstatic char * 81520253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 81620253Sjoerg{ 81720253Sjoerg char *sh = newshell; 81820253Sjoerg struct carg *arg = getarg(args, 's'); 81920253Sjoerg 82020253Sjoerg if (newshell == NULL && arg != NULL) 82120253Sjoerg sh = arg->val; 82220253Sjoerg return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 82320253Sjoerg} 82420253Sjoerg 82520253Sjoergstatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."; 82620253Sjoerg 82720253Sjoergchar * 82820253Sjoergpw_pwcrypt(char *password) 82920253Sjoerg{ 83020253Sjoerg int i; 83120253Sjoerg char salt[12]; 83220253Sjoerg 83320253Sjoerg static char buf[256]; 83420253Sjoerg 83520253Sjoerg /* 83620253Sjoerg * Calculate a salt value 83720253Sjoerg */ 83820555Sdavidn srandom((unsigned) (time(NULL) ^ getpid())); 83920253Sjoerg for (i = 0; i < 8; i++) 84020253Sjoerg salt[i] = chars[random() % 63]; 84120253Sjoerg salt[i] = '\0'; 84220253Sjoerg 84320253Sjoerg return strcpy(buf, crypt(password, salt)); 84420253Sjoerg} 84520253Sjoerg 84620590Sdavidn#if defined(__FreeBSD__) 84720590Sdavidn 84820590Sdavidn#if defined(USE_MD5RAND) 84920555Sdavidnu_char * 85020590Sdavidnpw_getrand(u_char *buf, int len) /* cryptographically secure rng */ 85120555Sdavidn{ 85220590Sdavidn int i; 85320590Sdavidn for (i=0;i<len;i+=16) { 85420590Sdavidn u_char ubuf[16]; 85520590Sdavidn 85620590Sdavidn MD5_CTX md5_ctx; 85720590Sdavidn struct timeval tv, tvo; 85820590Sdavidn struct rusage ru; 85920590Sdavidn int n=0; 86020590Sdavidn int t; 86120590Sdavidn 86220590Sdavidn MD5Init (&md5_ctx); 86320590Sdavidn t=getpid(); 86420590Sdavidn MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 86520590Sdavidn t=getppid(); 86620590Sdavidn MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 86720590Sdavidn gettimeofday (&tvo, NULL); 86820590Sdavidn do { 86920590Sdavidn getrusage (RUSAGE_SELF, &ru); 87020590Sdavidn MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru); 87120590Sdavidn gettimeofday (&tv, NULL); 87220590Sdavidn MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv); 87320590Sdavidn } while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000); 87420590Sdavidn MD5Final (ubuf, &md5_ctx); 87520590Sdavidn memcpy(buf+i, ubuf, MIN(16, len-n)); 87620590Sdavidn } 87720590Sdavidn return buf; 87820555Sdavidn} 87920253Sjoerg 88020590Sdavidn#else /* Use random device (preferred) */ 88120590Sdavidn 88220555Sdavidnstatic u_char * 88320555Sdavidnpw_getrand(u_char *buf, int len) 88420555Sdavidn{ 88520555Sdavidn int fd; 88620555Sdavidn fd = open("/dev/urandom", O_RDONLY); 88720590Sdavidn if (fd==-1) 88820590Sdavidn cmderr(EX_OSFILE, "can't open /dev/urandom: %s\n", strerror(errno)); 88920590Sdavidn else if (read(fd, buf, len)!=len) 89020590Sdavidn cmderr(EX_IOERR, "read error on /dev/urandom\n"); 89120555Sdavidn close(fd); 89220555Sdavidn return buf; 89320555Sdavidn} 89420555Sdavidn 89520590Sdavidn#endif 89620590Sdavidn 89720590Sdavidn#else /* Portable version */ 89820590Sdavidn 89920590Sdavidnstatic u_char * 90020590Sdavidnpw_getrand(u_char *buf, int len) 90120590Sdavidn{ 90220590Sdavidn int i; 90320590Sdavidn 90420590Sdavidn for (i = 0; i < len; i++) { 90520590Sdavidn unsigned val = random(); 90620590Sdavidn /* Use all bits in the random value */ 90720590Sdavidn buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val); 90820590Sdavidn } 90920590Sdavidn return buf; 91020590Sdavidn} 91120590Sdavidn 91220590Sdavidn#endif 91320590Sdavidn 91420253Sjoergstatic char * 91520253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user) 91620253Sjoerg{ 91720253Sjoerg int i, l; 91820253Sjoerg char pwbuf[32]; 91920555Sdavidn u_char rndbuf[sizeof pwbuf]; 92020253Sjoerg 92120253Sjoerg switch (cnf->default_password) { 92220253Sjoerg case -1: /* Random password */ 92320555Sdavidn srandom((unsigned) (time(NULL) ^ getpid())); 92420253Sjoerg l = (random() % 8 + 8); /* 8 - 16 chars */ 92520555Sdavidn pw_getrand(rndbuf, l); 92620253Sjoerg for (i = 0; i < l; i++) 92720555Sdavidn pwbuf[i] = chars[rndbuf[i] % sizeof(chars)]; 92820253Sjoerg pwbuf[i] = '\0'; 92920253Sjoerg 93020253Sjoerg /* 93120253Sjoerg * We give this information back to the user 93220253Sjoerg */ 93320267Sjoerg if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) { 93420712Sdavidn if (isatty(1)) 93520712Sdavidn printf("Password for '%s' is: ", user); 93620253Sjoerg printf("%s\n", pwbuf); 93720253Sjoerg fflush(stdout); 93820253Sjoerg } 93920253Sjoerg break; 94020253Sjoerg 94120253Sjoerg case -2: /* No password at all! */ 94220253Sjoerg return ""; 94320253Sjoerg 94420253Sjoerg case 0: /* No login - default */ 94520253Sjoerg default: 94620253Sjoerg return "*"; 94720253Sjoerg 94820253Sjoerg case 1: /* user's name */ 94920253Sjoerg strncpy(pwbuf, user, sizeof pwbuf); 95020253Sjoerg pwbuf[sizeof pwbuf - 1] = '\0'; 95120253Sjoerg break; 95220253Sjoerg } 95320253Sjoerg return pw_pwcrypt(pwbuf); 95420253Sjoerg} 95520253Sjoerg 95620253Sjoerg 95720253Sjoergstatic int 95820253Sjoergprint_user(struct passwd * pwd, int pretty) 95920253Sjoerg{ 96020253Sjoerg if (!pretty) { 96120253Sjoerg char buf[_UC_MAXLINE]; 96220253Sjoerg 96320253Sjoerg fmtpwent(buf, pwd); 96420253Sjoerg fputs(buf, stdout); 96520253Sjoerg } else { 96620267Sjoerg int j; 96720253Sjoerg char *p; 96820253Sjoerg struct group *grp = getgrgid(pwd->pw_gid); 96920253Sjoerg char uname[60] = "User &", office[60] = "[None]", 97020253Sjoerg wphone[60] = "[None]", hphone[60] = "[None]"; 97120590Sdavidn char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 97220590Sdavidn struct tm * tptr; 97320253Sjoerg 97420253Sjoerg if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 97520253Sjoerg strncpy(uname, p, sizeof uname); 97620253Sjoerg uname[sizeof uname - 1] = '\0'; 97720253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 97820253Sjoerg strncpy(office, p, sizeof office); 97920253Sjoerg office[sizeof office - 1] = '\0'; 98020253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 98120253Sjoerg strncpy(wphone, p, sizeof wphone); 98220253Sjoerg wphone[sizeof wphone - 1] = '\0'; 98320253Sjoerg if ((p = strtok(NULL, "")) != NULL) { 98420253Sjoerg strncpy(hphone, p, sizeof hphone); 98520253Sjoerg hphone[sizeof hphone - 1] = '\0'; 98620253Sjoerg } 98720253Sjoerg } 98820253Sjoerg } 98920253Sjoerg } 99020253Sjoerg /* 99120253Sjoerg * Handle '&' in gecos field 99220253Sjoerg */ 99320253Sjoerg if ((p = strchr(uname, '&')) != NULL) { 99420253Sjoerg int l = strlen(pwd->pw_name); 99520253Sjoerg int m = strlen(p); 99620253Sjoerg 99720253Sjoerg memmove(p + l, p + 1, m); 99820253Sjoerg memmove(p, pwd->pw_name, l); 99920253Sjoerg *p = (char) toupper(*p); 100020253Sjoerg } 100120590Sdavidn if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 100220590Sdavidn strftime(acexpire, sizeof acexpire, "%c", tptr); 100320590Sdavidn if (pwd->pw_change > (time_t)9 && (tptr = localtime(&pwd->pw_change)) != NULL) 100420590Sdavidn strftime(pwexpire, sizeof pwexpire, "%c", tptr); 100522394Sdavidn printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n" 100620747Sdavidn " Full Name: %s\n" 100720747Sdavidn " Home: %-26.26s Class: %s\n" 100820747Sdavidn " Shell: %-26.26s Office: %s\n" 100920747Sdavidn "Work Phone: %-26.26s Home Phone: %s\n" 101020747Sdavidn "Acc Expire: %-26.26s Pwd Expire: %s\n", 101120253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 101220253Sjoerg grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 101320253Sjoerg uname, pwd->pw_dir, pwd->pw_class, 101420590Sdavidn pwd->pw_shell, office, wphone, hphone, 101520590Sdavidn acexpire, pwexpire); 101620267Sjoerg setgrent(); 101720267Sjoerg j = 0; 101820267Sjoerg while ((grp=getgrent()) != NULL) 101920267Sjoerg { 102020267Sjoerg int i = 0; 102120747Sdavidn while (grp->gr_mem[i] != NULL) 102220267Sjoerg { 102320267Sjoerg if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 102420267Sjoerg { 102520747Sdavidn printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 102620267Sjoerg break; 102720267Sjoerg } 102820267Sjoerg ++i; 102920267Sjoerg } 103020267Sjoerg } 103120267Sjoerg endgrent(); 103220267Sjoerg printf("%s\n", j ? "\n" : ""); 103320253Sjoerg } 103420267Sjoerg return EXIT_SUCCESS; 103520253Sjoerg} 103620253Sjoerg 103720679Sdavidnchar * 103820679Sdavidnpw_checkname(u_char *name, int gecos) 103920253Sjoerg{ 104020253Sjoerg int l = 0; 104121052Sdavidn char const *notch = gecos ? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\""; 104220253Sjoerg 104320253Sjoerg while (name[l]) { 104421052Sdavidn if (strchr(notch, name[l]) != NULL || name[l] < ' ' || name[l] == 127 || 104521052Sdavidn (!gecos && l==0 && name[l] == '-') || /* leading '-' */ 104621052Sdavidn (!gecos && name[l] & 0x80)) /* 8-bit */ 104721052Sdavidn cmderr(EX_DATAERR, (name[l] >= ' ' && name[l] < 127) 104820325Sjoerg ? "invalid character `%c' in field\n" 104921052Sdavidn : "invalid character 0x%02x in field\n", 105020325Sjoerg name[l]); 105120253Sjoerg ++l; 105220253Sjoerg } 105322394Sdavidn if (!gecos && l > LOGNAMESIZE) 105420267Sjoerg cmderr(EX_DATAERR, "name too long `%s'\n", name); 105520679Sdavidn return (char *)name; 105620253Sjoerg} 105720253Sjoerg 105820253Sjoerg 105920253Sjoergstatic void 106020253Sjoergrmat(uid_t uid) 106120253Sjoerg{ 106220253Sjoerg DIR *d = opendir("/var/at/jobs"); 106320253Sjoerg 106420253Sjoerg if (d != NULL) { 106520253Sjoerg struct dirent *e; 106620253Sjoerg 106720253Sjoerg while ((e = readdir(d)) != NULL) { 106820253Sjoerg struct stat st; 106920253Sjoerg 107020253Sjoerg if (strncmp(e->d_name, ".lock", 5) != 0 && 107120253Sjoerg stat(e->d_name, &st) == 0 && 107220253Sjoerg !S_ISDIR(st.st_mode) && 107320253Sjoerg st.st_uid == uid) { 107420253Sjoerg char tmp[MAXPATHLEN]; 107520253Sjoerg 107620253Sjoerg sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 107720253Sjoerg system(tmp); 107820253Sjoerg } 107920253Sjoerg } 108020253Sjoerg closedir(d); 108120253Sjoerg } 108220253Sjoerg} 108320747Sdavidn 108420747Sdavidnstatic void 108520747Sdavidnrmskey(char const * name) 108620747Sdavidn{ 108720747Sdavidn static const char etcskey[] = "/etc/skeykeys"; 108821052Sdavidn FILE *fp = fopen(etcskey, "r+"); 108920747Sdavidn 109021052Sdavidn if (fp != NULL) { 109121052Sdavidn char tmp[1024]; 109221052Sdavidn off_t atofs = 0; 109321052Sdavidn int length = strlen(name); 109420747Sdavidn 109521052Sdavidn while (fgets(tmp, sizeof tmp, fp) != NULL) { 109621052Sdavidn if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 109721052Sdavidn if (fseek(fp, atofs, SEEK_SET) == 0) { 109821052Sdavidn fwrite("#", 1, 1, fp); /* Comment username out */ 109921052Sdavidn } 110021052Sdavidn break; 110120747Sdavidn } 110221052Sdavidn atofs = ftell(fp); 110320747Sdavidn } 110421052Sdavidn /* 110521052Sdavidn * If we got an error of any sort, don't update! 110621052Sdavidn */ 110721052Sdavidn fclose(fp); 110820747Sdavidn } 110920747Sdavidn} 111020747Sdavidn 1111