pw_user.c revision 21330
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 * 2621330Sdavidn * $Id: pw_user.c,v 1.11 1997/01/03 04:42:18 davidn 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> 3920555Sdavidn#include <md5.h> 4020253Sjoerg#include "pw.h" 4120253Sjoerg#include "bitmap.h" 4220253Sjoerg#include "pwupd.h" 4320253Sjoerg 4420253Sjoergstatic int print_user(struct passwd * pwd, int pretty); 4520253Sjoergstatic uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args); 4620253Sjoergstatic uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer); 4720253Sjoergstatic time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 4820253Sjoergstatic time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 4920253Sjoergstatic char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 5020253Sjoergstatic char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 5120253Sjoergstatic char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 5220253Sjoergstatic char *shell_path(char const * path, char *shells[], char *sh); 5320253Sjoergstatic void rmat(uid_t uid); 5420747Sdavidnstatic void rmskey(char const * name); 5520253Sjoerg 5620253Sjoerg/*- 5720253Sjoerg * -C config configuration file 5820253Sjoerg * -q quiet operation 5920253Sjoerg * -n name login name 6020253Sjoerg * -u uid user id 6120253Sjoerg * -c comment user name/comment 6220253Sjoerg * -d directory home directory 6320253Sjoerg * -e date account expiry date 6420253Sjoerg * -p date password expiry date 6520253Sjoerg * -g grp primary group 6620253Sjoerg * -G grp1,grp2 additional groups 6720253Sjoerg * -m [ -k dir ] create and set up home 6820253Sjoerg * -s shell name of login shell 6920253Sjoerg * -o duplicate uid ok 7020253Sjoerg * -L class user class 7120253Sjoerg * -l name new login name 7220253Sjoerg * -h fd password filehandle 7320253Sjoerg * -F force print or add 7420253Sjoerg * Setting defaults: 7520253Sjoerg * -D set user defaults 7620253Sjoerg * -b dir default home root dir 7720253Sjoerg * -e period default expiry period 7820253Sjoerg * -p period default password change period 7920253Sjoerg * -g group default group 8020253Sjoerg * -G grp1,grp2.. default additional groups 8120253Sjoerg * -L class default login class 8220253Sjoerg * -k dir default home skeleton 8320253Sjoerg * -s shell default shell 8420253Sjoerg * -w method default password method 8520253Sjoerg */ 8620253Sjoerg 8720253Sjoergint 8820253Sjoergpw_user(struct userconf * cnf, int mode, struct cargs * args) 8920253Sjoerg{ 9021330Sdavidn int r, r1; 9120253Sjoerg char *p = NULL; 9220253Sjoerg struct carg *a_name; 9320253Sjoerg struct carg *a_uid; 9420253Sjoerg struct carg *arg; 9520253Sjoerg struct passwd *pwd = NULL; 9620253Sjoerg struct group *grp; 9720253Sjoerg struct stat st; 9820747Sdavidn char line[_PASSWORD_LEN+1]; 9920253Sjoerg 10020253Sjoerg static struct passwd fakeuser = 10120253Sjoerg { 10220253Sjoerg NULL, 10320253Sjoerg "*", 10420253Sjoerg -1, 10520253Sjoerg -1, 10620253Sjoerg 0, 10720253Sjoerg "", 10820253Sjoerg "User &", 10920253Sjoerg "/bin/sh", 11020253Sjoerg 0, 11120253Sjoerg 0 11220253Sjoerg }; 11320253Sjoerg 11420253Sjoerg /* 11520267Sjoerg * With M_NEXT, we only need to return the 11620267Sjoerg * next uid to stdout 11720267Sjoerg */ 11820267Sjoerg if (mode == M_NEXT) 11920267Sjoerg { 12020267Sjoerg uid_t next = pw_uidpolicy(cnf, args); 12120267Sjoerg if (getarg(args, 'q')) 12220267Sjoerg return next; 12320267Sjoerg printf("%ld:", (long)next); 12420267Sjoerg pw_group(cnf, mode, args); 12520267Sjoerg return EXIT_SUCCESS; 12620267Sjoerg } 12720267Sjoerg 12820267Sjoerg /* 12920253Sjoerg * We can do all of the common legwork here 13020253Sjoerg */ 13120253Sjoerg 13220253Sjoerg if ((arg = getarg(args, 'b')) != NULL) { 13320267Sjoerg cnf->home = arg->val; 13420253Sjoerg } 13521052Sdavidn 13621052Sdavidn /* 13721052Sdavidn * If we'll need to use it or we're updating it, 13821052Sdavidn * then create the base home directory if necessary 13921052Sdavidn */ 14021052Sdavidn if (arg != NULL || getarg(args, 'm') != NULL) { 14121052Sdavidn int l = strlen(cnf->home); 14221052Sdavidn 14321052Sdavidn if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 14421052Sdavidn cnf->home[--l] = '\0'; 14521052Sdavidn 14621052Sdavidn if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 14721052Sdavidn cmderr(EX_DATAERR, "invalid base directory for home '%s'\n", cnf->home); 14821052Sdavidn 14921052Sdavidn if (stat(cnf->home, &st) == -1) { 15021052Sdavidn char dbuf[MAXPATHLEN]; 15121052Sdavidn 15221242Sdavidn /* 15321242Sdavidn * This is a kludge especially for Joerg :) 15421242Sdavidn * If the home directory would be created in the root partition, then 15521242Sdavidn * we really create it under /usr which is likely to have more space. 15621242Sdavidn * But we create a symlink from cnf->home -> "/usr" -> cnf->home 15721242Sdavidn */ 15821242Sdavidn if (strchr(cnf->home+1, '/') == NULL) { 15921242Sdavidn strcpy(dbuf, "/usr"); 16021242Sdavidn strncat(dbuf, cnf->home, MAXPATHLEN-5); 16121242Sdavidn if (mkdir(dbuf, 0755) != -1 || errno == EEXIST) { 16221242Sdavidn chown(dbuf, 0, 0); 16321242Sdavidn symlink(dbuf, cnf->home); 16421242Sdavidn } 16521242Sdavidn /* If this falls, fall back to old method */ 16621242Sdavidn } 16721052Sdavidn p = strncpy(dbuf, cnf->home, sizeof dbuf); 16821052Sdavidn dbuf[MAXPATHLEN-1] = '\0'; 16921242Sdavidn if (stat(dbuf, &st) == -1) { 17021242Sdavidn while ((p = strchr(++p, '/')) != NULL) { 17121242Sdavidn *p = '\0'; 17221242Sdavidn if (stat(dbuf, &st) == -1) { 17321242Sdavidn if (mkdir(dbuf, 0755) == -1) 17421242Sdavidn goto direrr; 17521242Sdavidn chown(dbuf, 0, 0); 17621242Sdavidn } else if (!S_ISDIR(st.st_mode)) 17721242Sdavidn cmderr(EX_OSFILE, "'%s' (root home parent) is not a directory\n", dbuf); 17821242Sdavidn *p = '/'; 17921242Sdavidn } 18021052Sdavidn } 18121242Sdavidn if (stat(dbuf, &st) == -1) { 18221052Sdavidn if (mkdir(dbuf, 0755) == -1) { 18321052Sdavidn direrr: cmderr(EX_OSFILE, "mkdir '%s': %s\n", dbuf, strerror(errno)); 18421052Sdavidn } 18521052Sdavidn chown(dbuf, 0, 0); 18621052Sdavidn } 18721052Sdavidn } else if (!S_ISDIR(st.st_mode)) 18821052Sdavidn cmderr(EX_OSFILE, "root home `%s' is not a directory\n", cnf->home); 18921052Sdavidn } 19021052Sdavidn 19121052Sdavidn 19220253Sjoerg if ((arg = getarg(args, 'e')) != NULL) 19320253Sjoerg cnf->expire_days = atoi(arg->val); 19420253Sjoerg 19521330Sdavidn if ((arg = getarg(args, 'y')) != NULL) 19621330Sdavidn cnf->nispasswd = arg->val; 19721330Sdavidn 19820253Sjoerg if ((arg = getarg(args, 'p')) != NULL && arg->val) 19920253Sjoerg cnf->password_days = atoi(arg->val); 20020253Sjoerg 20120253Sjoerg if ((arg = getarg(args, 'g')) != NULL) { 20220253Sjoerg p = arg->val; 20320253Sjoerg if ((grp = getgrnam(p)) == NULL) { 20420253Sjoerg if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL) 20520267Sjoerg cmderr(EX_NOUSER, "group `%s' does not exist\n", p); 20620253Sjoerg } 20720253Sjoerg cnf->default_group = newstr(grp->gr_name); 20820253Sjoerg } 20920253Sjoerg if ((arg = getarg(args, 'L')) != NULL) 21020679Sdavidn cnf->default_class = pw_checkname((u_char *)arg->val, 0); 21120253Sjoerg 21220253Sjoerg if ((arg = getarg(args, 'G')) != NULL && arg->val) { 21320253Sjoerg int i = 0; 21420253Sjoerg 21520747Sdavidn for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 21620253Sjoerg if ((grp = getgrnam(p)) == NULL) { 21720253Sjoerg if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL) 21820267Sjoerg cmderr(EX_NOUSER, "group `%s' does not exist\n", p); 21920253Sjoerg } 22020747Sdavidn if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 22120747Sdavidn cnf->groups[i++] = newstr(grp->gr_name); 22220253Sjoerg } 22320747Sdavidn while (i < cnf->numgroups) 22420253Sjoerg cnf->groups[i++] = NULL; 22520253Sjoerg } 22620253Sjoerg if ((arg = getarg(args, 'k')) != NULL) { 22720253Sjoerg if (stat(cnf->dotdir = arg->val, &st) == -1 || S_ISDIR(st.st_mode)) 22820267Sjoerg cmderr(EX_OSFILE, "skeleton `%s' is not a directory or does not exist\n", cnf->dotdir); 22920253Sjoerg } 23020253Sjoerg if ((arg = getarg(args, 's')) != NULL) 23120253Sjoerg cnf->shell_default = arg->val; 23220253Sjoerg 23320253Sjoerg if (mode == M_ADD && getarg(args, 'D')) { 23420253Sjoerg if (getarg(args, 'n') != NULL) 23520267Sjoerg cmderr(EX_DATAERR, "can't combine `-D' with `-n name'\n"); 23620253Sjoerg if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 23720253Sjoerg if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 23820253Sjoerg cnf->min_uid = 1000; 23920253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 24020253Sjoerg cnf->max_uid = 32000; 24120253Sjoerg } 24220253Sjoerg if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 24320253Sjoerg if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 24420253Sjoerg cnf->min_gid = 1000; 24520253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 24620253Sjoerg cnf->max_gid = 32000; 24720253Sjoerg } 24820253Sjoerg if ((arg = getarg(args, 'w')) != NULL) 24920253Sjoerg cnf->default_password = boolean_val(arg->val, cnf->default_password); 25020253Sjoerg 25120253Sjoerg arg = getarg(args, 'C'); 25220253Sjoerg if (write_userconfig(arg ? arg->val : NULL)) 25320267Sjoerg return EXIT_SUCCESS; 25420253Sjoerg perror("config update"); 25520267Sjoerg return EX_IOERR; 25620253Sjoerg } 25720253Sjoerg if (mode == M_PRINT && getarg(args, 'a')) { 25820267Sjoerg int pretty = getarg(args, 'P') != NULL; 25920253Sjoerg 26020253Sjoerg setpwent(); 26120253Sjoerg while ((pwd = getpwent()) != NULL) 26220253Sjoerg print_user(pwd, pretty); 26320253Sjoerg endpwent(); 26420267Sjoerg return EXIT_SUCCESS; 26520253Sjoerg } 26620253Sjoerg if ((a_name = getarg(args, 'n')) != NULL) 26720679Sdavidn pwd = getpwnam(pw_checkname((u_char *)a_name->val, 0)); 26820253Sjoerg a_uid = getarg(args, 'u'); 26920253Sjoerg 27020253Sjoerg if (a_uid == NULL) { 27120253Sjoerg if (a_name == NULL) 27220267Sjoerg cmderr(EX_DATAERR, "user name or id required\n"); 27320253Sjoerg 27420253Sjoerg /* 27520253Sjoerg * Determine whether 'n' switch is name or uid - we don't 27620253Sjoerg * really don't really care which we have, but we need to 27720253Sjoerg * know. 27820253Sjoerg */ 27920253Sjoerg if (mode != M_ADD && pwd == NULL && isdigit(*a_name->val) && atoi(a_name->val) > 0) { /* Assume uid */ 28020253Sjoerg (a_uid = a_name)->ch = 'u'; 28120253Sjoerg a_name = NULL; 28220253Sjoerg } 28320253Sjoerg } 28420253Sjoerg /* 28520253Sjoerg * Update, delete & print require that the user exists 28620253Sjoerg */ 28720253Sjoerg if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) { 28820253Sjoerg if (a_name == NULL && pwd == NULL) /* Try harder */ 28920253Sjoerg pwd = getpwuid(atoi(a_uid->val)); 29020253Sjoerg 29120253Sjoerg if (pwd == NULL) { 29220253Sjoerg if (mode == M_PRINT && getarg(args, 'F')) { 29320253Sjoerg fakeuser.pw_name = a_name ? a_name->val : "nouser"; 29420253Sjoerg fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 29520267Sjoerg return print_user(&fakeuser, getarg(args, 'P') != NULL); 29620253Sjoerg } 29720253Sjoerg if (a_name == NULL) 29820267Sjoerg cmderr(EX_NOUSER, "no such uid `%s'\n", a_uid->val); 29920267Sjoerg cmderr(EX_NOUSER, "no such user `%s'\n", a_name->val); 30020253Sjoerg } 30120253Sjoerg if (a_name == NULL) /* May be needed later */ 30220253Sjoerg a_name = addarg(args, 'n', newstr(pwd->pw_name)); 30320253Sjoerg 30420253Sjoerg /* 30520253Sjoerg * Handle deletions now 30620253Sjoerg */ 30720253Sjoerg if (mode == M_DELETE) { 30820253Sjoerg char file[MAXPATHLEN]; 30920253Sjoerg char home[MAXPATHLEN]; 31020253Sjoerg uid_t uid = pwd->pw_uid; 31120253Sjoerg 31220253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 31320267Sjoerg cmderr(EX_DATAERR, "cannot remove user 'root'\n"); 31420253Sjoerg 31520253Sjoerg /* 31620747Sdavidn * Remove skey record from /etc/skeykeys 31720747Sdavidn */ 31820747Sdavidn 31920747Sdavidn rmskey(pwd->pw_name); 32020747Sdavidn 32120747Sdavidn /* 32220253Sjoerg * Remove crontabs 32320253Sjoerg */ 32420253Sjoerg sprintf(file, "/var/cron/tabs/%s", pwd->pw_name); 32520253Sjoerg if (access(file, F_OK) == 0) { 32620253Sjoerg sprintf(file, "crontab -u %s -r", pwd->pw_name); 32720253Sjoerg system(file); 32820253Sjoerg } 32920253Sjoerg /* 33020253Sjoerg * Save these for later, since contents of pwd may be 33120253Sjoerg * invalidated by deletion 33220253Sjoerg */ 33320253Sjoerg sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 33420253Sjoerg strncpy(home, pwd->pw_dir, sizeof home); 33520253Sjoerg home[sizeof home - 1] = '\0'; 33620253Sjoerg 33720253Sjoerg if (!delpwent(pwd)) 33820267Sjoerg cmderr(EX_IOERR, "Error updating passwd file: %s\n", strerror(errno)); 33921330Sdavidn 34021330Sdavidn if (cnf->nispasswd && *cnf->nispasswd=='/' && !delnispwent(cnf->nispasswd, a_name->val)) 34121330Sdavidn perror("WARNING: NIS passwd update"); 34221330Sdavidn 34320253Sjoerg editgroups(a_name->val, NULL); 34420253Sjoerg 34520253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 34620253Sjoerg 34720253Sjoerg /* 34820253Sjoerg * Remove mail file 34920253Sjoerg */ 35020253Sjoerg remove(file); 35120253Sjoerg 35220253Sjoerg /* 35320253Sjoerg * Remove at jobs 35420253Sjoerg */ 35520253Sjoerg if (getpwuid(uid) == NULL) 35620253Sjoerg rmat(uid); 35720253Sjoerg 35820253Sjoerg /* 35920253Sjoerg * Remove home directory and contents 36020253Sjoerg */ 36120253Sjoerg if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 36220253Sjoerg if (stat(home, &st) != -1) { 36320253Sjoerg rm_r(home, uid); 36420253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 36520253Sjoerg a_name->val, (long) uid, home, 36620253Sjoerg stat(home, &st) == -1 ? "" : "not completely "); 36720253Sjoerg } 36820253Sjoerg } 36920267Sjoerg return EXIT_SUCCESS; 37020253Sjoerg } else if (mode == M_PRINT) 37120267Sjoerg return print_user(pwd, getarg(args, 'P') != NULL); 37220253Sjoerg 37320253Sjoerg /* 37420253Sjoerg * The rest is edit code 37520253Sjoerg */ 37620253Sjoerg if ((arg = getarg(args, 'l')) != NULL) { 37720253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 37820267Sjoerg cmderr(EX_DATAERR, "can't rename `root' account\n"); 37920679Sdavidn pwd->pw_name = pw_checkname((u_char *)arg->val, 0); 38020253Sjoerg } 38120253Sjoerg if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) { 38220253Sjoerg pwd->pw_uid = (uid_t) atol(arg->val); 38320253Sjoerg if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 38420267Sjoerg cmderr(EX_DATAERR, "can't change uid of `root' account\n"); 38520253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 38620253Sjoerg fprintf(stderr, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd->pw_name); 38720253Sjoerg } 38820253Sjoerg if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) /* Already checked this */ 38920253Sjoerg pwd->pw_gid = (gid_t) getgrnam(cnf->default_group)->gr_gid; 39020253Sjoerg 39120253Sjoerg if ((arg = getarg(args, 'p')) != NULL) { 39220253Sjoerg if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) 39320253Sjoerg pwd->pw_change = 0; 39420253Sjoerg else { 39520253Sjoerg time_t now = time(NULL); 39620253Sjoerg time_t expire = parse_date(now, arg->val); 39720253Sjoerg 39820253Sjoerg if (now == expire) 39920267Sjoerg cmderr(EX_DATAERR, "Invalid password change date `%s'\n", arg->val); 40020253Sjoerg pwd->pw_change = expire; 40120253Sjoerg } 40220253Sjoerg } 40320267Sjoerg if ((arg = getarg(args, 'e')) != NULL) { 40420253Sjoerg if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) 40520253Sjoerg pwd->pw_expire = 0; 40620253Sjoerg else { 40720253Sjoerg time_t now = time(NULL); 40820253Sjoerg time_t expire = parse_date(now, arg->val); 40920253Sjoerg 41020253Sjoerg if (now == expire) 41120267Sjoerg cmderr(EX_DATAERR, "Invalid account expiry date `%s'\n", arg->val); 41220253Sjoerg pwd->pw_expire = expire; 41320253Sjoerg } 41420253Sjoerg } 41520253Sjoerg if ((arg = getarg(args, 's')) != NULL) 41620253Sjoerg pwd->pw_shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 41720253Sjoerg 41820253Sjoerg if (getarg(args, 'L')) 41920253Sjoerg pwd->pw_class = cnf->default_class; 42020253Sjoerg 42120747Sdavidn if ((arg = getarg(args, 'd')) != NULL) { 42220747Sdavidn if (stat(pwd->pw_dir = arg->val, &st) == -1) { 42320747Sdavidn if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 42420747Sdavidn fprintf(stderr, "WARNING: home `%s' does not exist\n", pwd->pw_dir); 42520747Sdavidn } else if (!S_ISDIR(st.st_mode)) 42620747Sdavidn fprintf(stderr, "WARNING: home `%s' is not a directory\n", pwd->pw_dir); 42720747Sdavidn } 42820747Sdavidn 42920267Sjoerg if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL) 43020267Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 43120267Sjoerg 43220253Sjoerg } else { 43320253Sjoerg if (a_name == NULL) /* Required */ 43420267Sjoerg cmderr(EX_DATAERR, "login name required\n"); 43520253Sjoerg else if ((pwd = getpwnam(a_name->val)) != NULL) /* Exists */ 43620267Sjoerg cmderr(EX_DATAERR, "login name `%s' already exists\n", a_name->val); 43720253Sjoerg 43820253Sjoerg /* 43920253Sjoerg * Now, set up defaults for a new user 44020253Sjoerg */ 44120253Sjoerg pwd = &fakeuser; 44220253Sjoerg pwd->pw_name = a_name->val; 44320253Sjoerg pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 44420253Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 44520253Sjoerg pwd->pw_uid = pw_uidpolicy(cnf, args); 44620253Sjoerg pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 44720253Sjoerg pwd->pw_change = pw_pwdpolicy(cnf, args); 44820253Sjoerg pwd->pw_expire = pw_exppolicy(cnf, args); 44920253Sjoerg pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 45020253Sjoerg pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 45120253Sjoerg 45220253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 45320253Sjoerg fprintf(stderr, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd->pw_name); 45420253Sjoerg } 45520253Sjoerg 45620253Sjoerg /* 45720253Sjoerg * Shared add/edit code 45820253Sjoerg */ 45920253Sjoerg if ((arg = getarg(args, 'c')) != NULL) 46020679Sdavidn pwd->pw_gecos = pw_checkname((u_char *)arg->val, 1); 46120253Sjoerg 46220253Sjoerg if ((arg = getarg(args, 'h')) != NULL) { 46320253Sjoerg if (strcmp(arg->val, "-") == 0) 46420253Sjoerg pwd->pw_passwd = "*"; /* No access */ 46520253Sjoerg else { 46620253Sjoerg int fd = atoi(arg->val); 46720253Sjoerg int b; 46820253Sjoerg int istty = isatty(fd); 46920253Sjoerg struct termios t; 47020253Sjoerg 47120253Sjoerg if (istty) { 47220253Sjoerg if (tcgetattr(fd, &t) == -1) 47320253Sjoerg istty = 0; 47420253Sjoerg else { 47520253Sjoerg struct termios n = t; 47620253Sjoerg 47720253Sjoerg /* Disable echo */ 47820253Sjoerg n.c_lflag &= ~(ECHO); 47920253Sjoerg tcsetattr(fd, TCSANOW, &n); 48020253Sjoerg printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name); 48120253Sjoerg fflush(stdout); 48220253Sjoerg } 48320253Sjoerg } 48420253Sjoerg b = read(fd, line, sizeof(line) - 1); 48520253Sjoerg if (istty) { /* Restore state */ 48620253Sjoerg tcsetattr(fd, TCSANOW, &t); 48720253Sjoerg fputc('\n', stdout); 48820253Sjoerg fflush(stdout); 48920253Sjoerg } 49020253Sjoerg if (b < 0) { 49120253Sjoerg perror("-h file descriptor"); 49220267Sjoerg return EX_IOERR; 49320253Sjoerg } 49420253Sjoerg line[b] = '\0'; 49520253Sjoerg if ((p = strpbrk(line, " \t\r\n")) != NULL) 49620253Sjoerg *p = '\0'; 49720253Sjoerg if (!*line) 49820267Sjoerg cmderr(EX_DATAERR, "empty password read on file descriptor %d\n", fd); 49920253Sjoerg pwd->pw_passwd = pw_pwcrypt(line); 50020253Sjoerg } 50120253Sjoerg } 50220267Sjoerg 50320267Sjoerg /* 50420267Sjoerg * Special case: -N only displays & exits 50520267Sjoerg */ 50620267Sjoerg if (getarg(args, 'N') != NULL) 50720267Sjoerg return print_user(pwd, getarg(args, 'P') != NULL); 50820267Sjoerg 50921330Sdavidn r = r1 = 1; 51021330Sdavidn if (mode == M_ADD) { 51121330Sdavidn r = addpwent(pwd); 51221330Sdavidn if (r && cnf->nispasswd && *cnf->nispasswd=='/') 51321330Sdavidn r1 = addnispwent(cnf->nispasswd, pwd); 51421330Sdavidn } else if (mode == M_UPDATE) { 51521330Sdavidn r = chgpwent(a_name->val, pwd); 51621330Sdavidn if (r && cnf->nispasswd && *cnf->nispasswd=='/') 51721330Sdavidn r1 = chgnispwent(cnf->nispasswd, a_name->val, pwd); 51821330Sdavidn } 51921330Sdavidn 52021330Sdavidn if (!r) { 52120253Sjoerg perror("password update"); 52220267Sjoerg return EX_IOERR; 52321330Sdavidn } else if (!r1) { 52421330Sdavidn perror("WARNING: NIS password update"); 52521330Sdavidn /* Keep on trucking */ 52620253Sjoerg } 52721330Sdavidn 52820253Sjoerg /* 52920253Sjoerg * Ok, user is created or changed - now edit group file 53020253Sjoerg */ 53120253Sjoerg 53220253Sjoerg if (mode == M_ADD || getarg(args, 'G') != NULL) 53320253Sjoerg editgroups(pwd->pw_name, cnf->groups); 53420253Sjoerg 53520253Sjoerg /* pwd may have been invalidated */ 53620253Sjoerg if ((pwd = getpwnam(a_name->val)) == NULL) 53720267Sjoerg cmderr(EX_NOUSER, "user '%s' disappeared during update\n", a_name->val); 53820253Sjoerg 53920253Sjoerg grp = getgrgid(pwd->pw_gid); 54020253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s", 54120253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 54220253Sjoerg grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 54320253Sjoerg pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 54420253Sjoerg 54520253Sjoerg /* 54620253Sjoerg * If adding, let's touch and chown the user's mail file. This is not 54720253Sjoerg * strictly necessary under BSD with a 0755 maildir but it also 54820253Sjoerg * doesn't hurt anything to create the empty mailfile 54920253Sjoerg */ 55020253Sjoerg if (mode == M_ADD) { 55120253Sjoerg FILE *fp; 55220253Sjoerg 55320253Sjoerg sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 55420253Sjoerg close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 55520253Sjoerg * mtime */ 55620253Sjoerg chown(line, pwd->pw_uid, pwd->pw_gid); 55720253Sjoerg 55820253Sjoerg /* 55920253Sjoerg * Send mail to the new user as well, if we are asked to 56020253Sjoerg */ 56120253Sjoerg if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 56220253Sjoerg FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 56320253Sjoerg 56420253Sjoerg if (pfp == NULL) 56520253Sjoerg perror("sendmail"); 56620253Sjoerg else { 56720253Sjoerg fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 56820253Sjoerg while (fgets(line, sizeof(line), fp) != NULL) { 56920253Sjoerg /* Do substitutions? */ 57020253Sjoerg fputs(line, pfp); 57120253Sjoerg } 57220253Sjoerg pclose(pfp); 57320253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 57420253Sjoerg pwd->pw_name, (long) pwd->pw_uid); 57520253Sjoerg } 57620253Sjoerg fclose(fp); 57720253Sjoerg } 57820253Sjoerg } 57920253Sjoerg /* 58020253Sjoerg * Finally, let's create and populate the user's home directory. Note 58120253Sjoerg * that this also `works' for editing users if -m is used, but 58220253Sjoerg * existing files will *not* be overwritten. 58320253Sjoerg */ 58420253Sjoerg if (getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 58520253Sjoerg copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid); 58620253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 58720253Sjoerg pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 58820253Sjoerg } 58920267Sjoerg return EXIT_SUCCESS; 59020253Sjoerg} 59120253Sjoerg 59220253Sjoerg 59320253Sjoergstatic uid_t 59420253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args) 59520253Sjoerg{ 59620253Sjoerg struct passwd *pwd; 59720253Sjoerg uid_t uid = (uid_t) - 1; 59820253Sjoerg struct carg *a_uid = getarg(args, 'u'); 59920253Sjoerg 60020253Sjoerg /* 60120253Sjoerg * Check the given uid, if any 60220253Sjoerg */ 60320253Sjoerg if (a_uid != NULL) { 60420253Sjoerg uid = (uid_t) atol(a_uid->val); 60520253Sjoerg 60620253Sjoerg if ((pwd = getpwuid(uid)) != NULL && getarg(args, 'o') == NULL) 60720267Sjoerg cmderr(EX_DATAERR, "uid `%ld' has already been allocated\n", (long) pwd->pw_uid); 60820253Sjoerg } else { 60920253Sjoerg struct bitmap bm; 61020253Sjoerg 61120253Sjoerg /* 61220253Sjoerg * We need to allocate the next available uid under one of 61320253Sjoerg * two policies a) Grab the first unused uid b) Grab the 61420253Sjoerg * highest possible unused uid 61520253Sjoerg */ 61620253Sjoerg if (cnf->min_uid >= cnf->max_uid) { /* Sanity 61720253Sjoerg * claus^H^H^H^Hheck */ 61820253Sjoerg cnf->min_uid = 1000; 61920253Sjoerg cnf->max_uid = 32000; 62020253Sjoerg } 62120253Sjoerg bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 62220253Sjoerg 62320253Sjoerg /* 62420253Sjoerg * Now, let's fill the bitmap from the password file 62520253Sjoerg */ 62620253Sjoerg setpwent(); 62720253Sjoerg while ((pwd = getpwent()) != NULL) 62820253Sjoerg if (pwd->pw_uid >= (int) cnf->min_uid && pwd->pw_uid <= (int) cnf->max_uid) 62920253Sjoerg bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 63020253Sjoerg endpwent(); 63120253Sjoerg 63220253Sjoerg /* 63320253Sjoerg * Then apply the policy, with fallback to reuse if necessary 63420253Sjoerg */ 63520253Sjoerg if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 63620253Sjoerg uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 63720253Sjoerg 63820253Sjoerg /* 63920253Sjoerg * Another sanity check 64020253Sjoerg */ 64120253Sjoerg if (uid < cnf->min_uid || uid > cnf->max_uid) 64220267Sjoerg cmderr(EX_SOFTWARE, "unable to allocate a new uid - range fully used\n"); 64320253Sjoerg bm_dealloc(&bm); 64420253Sjoerg } 64520253Sjoerg return uid; 64620253Sjoerg} 64720253Sjoerg 64820253Sjoerg 64920253Sjoergstatic uid_t 65020253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 65120253Sjoerg{ 65220253Sjoerg struct group *grp; 65320253Sjoerg gid_t gid = (uid_t) - 1; 65420253Sjoerg struct carg *a_gid = getarg(args, 'g'); 65520253Sjoerg 65620253Sjoerg /* 65720253Sjoerg * If no arg given, see if default can help out 65820253Sjoerg */ 65920253Sjoerg if (a_gid == NULL && cnf->default_group && *cnf->default_group) 66020253Sjoerg a_gid = addarg(args, 'g', cnf->default_group); 66120253Sjoerg 66220253Sjoerg /* 66320253Sjoerg * Check the given gid, if any 66420253Sjoerg */ 66520267Sjoerg setgrent(); 66620253Sjoerg if (a_gid != NULL) { 66720253Sjoerg if ((grp = getgrnam(a_gid->val)) == NULL) { 66820253Sjoerg gid = (gid_t) atol(a_gid->val); 66920253Sjoerg if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = getgrgid(gid)) == NULL) 67020267Sjoerg cmderr(EX_NOUSER, "group `%s' is not defined\n", a_gid->val); 67120253Sjoerg } 67220253Sjoerg gid = grp->gr_gid; 67320267Sjoerg } else if ((grp = getgrnam(nam)) != NULL && grp->gr_mem[0] == NULL) { 67420267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 67520253Sjoerg } else { 67620253Sjoerg struct cargs grpargs; 67720253Sjoerg char tmp[32]; 67820253Sjoerg 67920253Sjoerg LIST_INIT(&grpargs); 68020253Sjoerg addarg(&grpargs, 'n', nam); 68120253Sjoerg 68220253Sjoerg /* 68320253Sjoerg * We need to auto-create a group with the user's name. We 68420253Sjoerg * can send all the appropriate output to our sister routine 68520253Sjoerg * bit first see if we can create a group with gid==uid so we 68620253Sjoerg * can keep the user and group ids in sync. We purposely do 68720253Sjoerg * NOT check the gid range if we can force the sync. If the 68820253Sjoerg * user's name dups an existing group, then the group add 68920253Sjoerg * function will happily handle that case for us and exit. 69020253Sjoerg */ 69120253Sjoerg if (getgrgid(prefer) == NULL) { 69220253Sjoerg sprintf(tmp, "%lu", (unsigned long) prefer); 69320253Sjoerg addarg(&grpargs, 'g', tmp); 69420253Sjoerg } 69520267Sjoerg if (getarg(args, 'N')) 69620267Sjoerg { 69720267Sjoerg addarg(&grpargs, 'N', NULL); 69820267Sjoerg addarg(&grpargs, 'q', NULL); 69920267Sjoerg gid = pw_group(cnf, M_NEXT, &grpargs); 70020267Sjoerg } 70120267Sjoerg else 70220267Sjoerg { 70320267Sjoerg pw_group(cnf, M_ADD, &grpargs); 70420267Sjoerg if ((grp = getgrnam(nam)) != NULL) 70520267Sjoerg gid = grp->gr_gid; 70620267Sjoerg } 70720253Sjoerg a_gid = grpargs.lh_first; 70820253Sjoerg while (a_gid != NULL) { 70920253Sjoerg struct carg *t = a_gid->list.le_next; 71020253Sjoerg LIST_REMOVE(a_gid, list); 71120253Sjoerg a_gid = t; 71220253Sjoerg } 71320253Sjoerg } 71420267Sjoerg endgrent(); 71520253Sjoerg return gid; 71620253Sjoerg} 71720253Sjoerg 71820253Sjoerg 71920253Sjoergstatic time_t 72020253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args) 72120253Sjoerg{ 72220253Sjoerg time_t result = 0; 72320253Sjoerg time_t now = time(NULL); 72420253Sjoerg struct carg *arg = getarg(args, 'e'); 72520253Sjoerg 72620253Sjoerg if (arg != NULL) { 72720253Sjoerg if ((result = parse_date(now, arg->val)) == now) 72820267Sjoerg cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val); 72920253Sjoerg } else if (cnf->password_days > 0) 73020253Sjoerg result = now + ((long) cnf->password_days * 86400L); 73120253Sjoerg return result; 73220253Sjoerg} 73320253Sjoerg 73420253Sjoerg 73520253Sjoergstatic time_t 73620253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args) 73720253Sjoerg{ 73820253Sjoerg time_t result = 0; 73920253Sjoerg time_t now = time(NULL); 74020253Sjoerg struct carg *arg = getarg(args, 'e'); 74120253Sjoerg 74220253Sjoerg if (arg != NULL) { 74320253Sjoerg if ((result = parse_date(now, arg->val)) == now) 74420267Sjoerg cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val); 74520253Sjoerg } else if (cnf->expire_days > 0) 74620253Sjoerg result = now + ((long) cnf->expire_days * 86400L); 74720253Sjoerg return result; 74820253Sjoerg} 74920253Sjoerg 75020253Sjoerg 75120253Sjoergstatic char * 75220253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 75320253Sjoerg{ 75420253Sjoerg struct carg *arg = getarg(args, 'd'); 75520253Sjoerg 75620253Sjoerg if (arg) 75720253Sjoerg return arg->val; 75820253Sjoerg else { 75920253Sjoerg static char home[128]; 76020253Sjoerg 76120253Sjoerg if (cnf->home == NULL || *cnf->home == '\0') 76220267Sjoerg cmderr(EX_CONFIG, "no base home directory set\n"); 76320253Sjoerg sprintf(home, "%s/%s", cnf->home, user); 76420253Sjoerg return home; 76520253Sjoerg } 76620253Sjoerg} 76720253Sjoerg 76820253Sjoergstatic char * 76920253Sjoergshell_path(char const * path, char *shells[], char *sh) 77020253Sjoerg{ 77120253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 77220253Sjoerg return sh; /* specified full path or forced none */ 77320253Sjoerg else { 77420253Sjoerg char *p; 77520253Sjoerg char paths[_UC_MAXLINE]; 77620253Sjoerg 77720253Sjoerg /* 77820253Sjoerg * We need to search paths 77920253Sjoerg */ 78020253Sjoerg strncpy(paths, path, sizeof paths); 78120253Sjoerg paths[sizeof paths - 1] = '\0'; 78220253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 78320253Sjoerg int i; 78420253Sjoerg static char shellpath[256]; 78520253Sjoerg 78620253Sjoerg if (sh != NULL) { 78720253Sjoerg sprintf(shellpath, "%s/%s", p, sh); 78820253Sjoerg if (access(shellpath, X_OK) == 0) 78920253Sjoerg return shellpath; 79020253Sjoerg } else 79120253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 79220253Sjoerg sprintf(shellpath, "%s/%s", p, shells[i]); 79320253Sjoerg if (access(shellpath, X_OK) == 0) 79420253Sjoerg return shellpath; 79520253Sjoerg } 79620253Sjoerg } 79720253Sjoerg if (sh == NULL) 79820267Sjoerg cmderr(EX_OSFILE, "can't find shell `%s' in shell paths\n", sh); 79920267Sjoerg cmderr(EX_CONFIG, "no default shell available or defined\n"); 80020253Sjoerg return NULL; 80120253Sjoerg } 80220253Sjoerg} 80320253Sjoerg 80420253Sjoerg 80520253Sjoergstatic char * 80620253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 80720253Sjoerg{ 80820253Sjoerg char *sh = newshell; 80920253Sjoerg struct carg *arg = getarg(args, 's'); 81020253Sjoerg 81120253Sjoerg if (newshell == NULL && arg != NULL) 81220253Sjoerg sh = arg->val; 81320253Sjoerg return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 81420253Sjoerg} 81520253Sjoerg 81620253Sjoergstatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."; 81720253Sjoerg 81820253Sjoergchar * 81920253Sjoergpw_pwcrypt(char *password) 82020253Sjoerg{ 82120253Sjoerg int i; 82220253Sjoerg char salt[12]; 82320253Sjoerg 82420253Sjoerg static char buf[256]; 82520253Sjoerg 82620253Sjoerg /* 82720253Sjoerg * Calculate a salt value 82820253Sjoerg */ 82920555Sdavidn srandom((unsigned) (time(NULL) ^ getpid())); 83020253Sjoerg for (i = 0; i < 8; i++) 83120253Sjoerg salt[i] = chars[random() % 63]; 83220253Sjoerg salt[i] = '\0'; 83320253Sjoerg 83420253Sjoerg return strcpy(buf, crypt(password, salt)); 83520253Sjoerg} 83620253Sjoerg 83720590Sdavidn#if defined(__FreeBSD__) 83820590Sdavidn 83920590Sdavidn#if defined(USE_MD5RAND) 84020555Sdavidnu_char * 84120590Sdavidnpw_getrand(u_char *buf, int len) /* cryptographically secure rng */ 84220555Sdavidn{ 84320590Sdavidn int i; 84420590Sdavidn for (i=0;i<len;i+=16) { 84520590Sdavidn u_char ubuf[16]; 84620590Sdavidn 84720590Sdavidn MD5_CTX md5_ctx; 84820590Sdavidn struct timeval tv, tvo; 84920590Sdavidn struct rusage ru; 85020590Sdavidn int n=0; 85120590Sdavidn int t; 85220590Sdavidn 85320590Sdavidn MD5Init (&md5_ctx); 85420590Sdavidn t=getpid(); 85520590Sdavidn MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 85620590Sdavidn t=getppid(); 85720590Sdavidn MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 85820590Sdavidn gettimeofday (&tvo, NULL); 85920590Sdavidn do { 86020590Sdavidn getrusage (RUSAGE_SELF, &ru); 86120590Sdavidn MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru); 86220590Sdavidn gettimeofday (&tv, NULL); 86320590Sdavidn MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv); 86420590Sdavidn } while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000); 86520590Sdavidn MD5Final (ubuf, &md5_ctx); 86620590Sdavidn memcpy(buf+i, ubuf, MIN(16, len-n)); 86720590Sdavidn } 86820590Sdavidn return buf; 86920555Sdavidn} 87020253Sjoerg 87120590Sdavidn#else /* Use random device (preferred) */ 87220590Sdavidn 87320555Sdavidnstatic u_char * 87420555Sdavidnpw_getrand(u_char *buf, int len) 87520555Sdavidn{ 87620555Sdavidn int fd; 87720555Sdavidn fd = open("/dev/urandom", O_RDONLY); 87820590Sdavidn if (fd==-1) 87920590Sdavidn cmderr(EX_OSFILE, "can't open /dev/urandom: %s\n", strerror(errno)); 88020590Sdavidn else if (read(fd, buf, len)!=len) 88120590Sdavidn cmderr(EX_IOERR, "read error on /dev/urandom\n"); 88220555Sdavidn close(fd); 88320555Sdavidn return buf; 88420555Sdavidn} 88520555Sdavidn 88620590Sdavidn#endif 88720590Sdavidn 88820590Sdavidn#else /* Portable version */ 88920590Sdavidn 89020590Sdavidnstatic u_char * 89120590Sdavidnpw_getrand(u_char *buf, int len) 89220590Sdavidn{ 89320590Sdavidn int i; 89420590Sdavidn 89520590Sdavidn for (i = 0; i < len; i++) { 89620590Sdavidn unsigned val = random(); 89720590Sdavidn /* Use all bits in the random value */ 89820590Sdavidn buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val); 89920590Sdavidn } 90020590Sdavidn return buf; 90120590Sdavidn} 90220590Sdavidn 90320590Sdavidn#endif 90420590Sdavidn 90520253Sjoergstatic char * 90620253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user) 90720253Sjoerg{ 90820253Sjoerg int i, l; 90920253Sjoerg char pwbuf[32]; 91020555Sdavidn u_char rndbuf[sizeof pwbuf]; 91120253Sjoerg 91220253Sjoerg switch (cnf->default_password) { 91320253Sjoerg case -1: /* Random password */ 91420555Sdavidn srandom((unsigned) (time(NULL) ^ getpid())); 91520253Sjoerg l = (random() % 8 + 8); /* 8 - 16 chars */ 91620555Sdavidn pw_getrand(rndbuf, l); 91720253Sjoerg for (i = 0; i < l; i++) 91820555Sdavidn pwbuf[i] = chars[rndbuf[i] % sizeof(chars)]; 91920253Sjoerg pwbuf[i] = '\0'; 92020253Sjoerg 92120253Sjoerg /* 92220253Sjoerg * We give this information back to the user 92320253Sjoerg */ 92420267Sjoerg if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) { 92520712Sdavidn if (isatty(1)) 92620712Sdavidn printf("Password for '%s' is: ", user); 92720253Sjoerg printf("%s\n", pwbuf); 92820253Sjoerg fflush(stdout); 92920253Sjoerg } 93020253Sjoerg break; 93120253Sjoerg 93220253Sjoerg case -2: /* No password at all! */ 93320253Sjoerg return ""; 93420253Sjoerg 93520253Sjoerg case 0: /* No login - default */ 93620253Sjoerg default: 93720253Sjoerg return "*"; 93820253Sjoerg 93920253Sjoerg case 1: /* user's name */ 94020253Sjoerg strncpy(pwbuf, user, sizeof pwbuf); 94120253Sjoerg pwbuf[sizeof pwbuf - 1] = '\0'; 94220253Sjoerg break; 94320253Sjoerg } 94420253Sjoerg return pw_pwcrypt(pwbuf); 94520253Sjoerg} 94620253Sjoerg 94720253Sjoerg 94820253Sjoergstatic int 94920253Sjoergprint_user(struct passwd * pwd, int pretty) 95020253Sjoerg{ 95120253Sjoerg if (!pretty) { 95220253Sjoerg char buf[_UC_MAXLINE]; 95320253Sjoerg 95420253Sjoerg fmtpwent(buf, pwd); 95520253Sjoerg fputs(buf, stdout); 95620253Sjoerg } else { 95720267Sjoerg int j; 95820253Sjoerg char *p; 95920253Sjoerg struct group *grp = getgrgid(pwd->pw_gid); 96020253Sjoerg char uname[60] = "User &", office[60] = "[None]", 96120253Sjoerg wphone[60] = "[None]", hphone[60] = "[None]"; 96220590Sdavidn char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 96320590Sdavidn struct tm * tptr; 96420253Sjoerg 96520253Sjoerg if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 96620253Sjoerg strncpy(uname, p, sizeof uname); 96720253Sjoerg uname[sizeof uname - 1] = '\0'; 96820253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 96920253Sjoerg strncpy(office, p, sizeof office); 97020253Sjoerg office[sizeof office - 1] = '\0'; 97120253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 97220253Sjoerg strncpy(wphone, p, sizeof wphone); 97320253Sjoerg wphone[sizeof wphone - 1] = '\0'; 97420253Sjoerg if ((p = strtok(NULL, "")) != NULL) { 97520253Sjoerg strncpy(hphone, p, sizeof hphone); 97620253Sjoerg hphone[sizeof hphone - 1] = '\0'; 97720253Sjoerg } 97820253Sjoerg } 97920253Sjoerg } 98020253Sjoerg } 98120253Sjoerg /* 98220253Sjoerg * Handle '&' in gecos field 98320253Sjoerg */ 98420253Sjoerg if ((p = strchr(uname, '&')) != NULL) { 98520253Sjoerg int l = strlen(pwd->pw_name); 98620253Sjoerg int m = strlen(p); 98720253Sjoerg 98820253Sjoerg memmove(p + l, p + 1, m); 98920253Sjoerg memmove(p, pwd->pw_name, l); 99020253Sjoerg *p = (char) toupper(*p); 99120253Sjoerg } 99220590Sdavidn if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 99320590Sdavidn strftime(acexpire, sizeof acexpire, "%c", tptr); 99420590Sdavidn if (pwd->pw_change > (time_t)9 && (tptr = localtime(&pwd->pw_change)) != NULL) 99520590Sdavidn strftime(pwexpire, sizeof pwexpire, "%c", tptr); 99620747Sdavidn printf("Login Name: %-10s #%-16ld Group: %-10s #%ld\n" 99720747Sdavidn " Full Name: %s\n" 99820747Sdavidn " Home: %-26.26s Class: %s\n" 99920747Sdavidn " Shell: %-26.26s Office: %s\n" 100020747Sdavidn "Work Phone: %-26.26s Home Phone: %s\n" 100120747Sdavidn "Acc Expire: %-26.26s Pwd Expire: %s\n", 100220253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 100320253Sjoerg grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 100420253Sjoerg uname, pwd->pw_dir, pwd->pw_class, 100520590Sdavidn pwd->pw_shell, office, wphone, hphone, 100620590Sdavidn acexpire, pwexpire); 100720267Sjoerg setgrent(); 100820267Sjoerg j = 0; 100920267Sjoerg while ((grp=getgrent()) != NULL) 101020267Sjoerg { 101120267Sjoerg int i = 0; 101220747Sdavidn while (grp->gr_mem[i] != NULL) 101320267Sjoerg { 101420267Sjoerg if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 101520267Sjoerg { 101620747Sdavidn printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 101720267Sjoerg break; 101820267Sjoerg } 101920267Sjoerg ++i; 102020267Sjoerg } 102120267Sjoerg } 102220267Sjoerg endgrent(); 102320267Sjoerg printf("%s\n", j ? "\n" : ""); 102420253Sjoerg } 102520267Sjoerg return EXIT_SUCCESS; 102620253Sjoerg} 102720253Sjoerg 102820679Sdavidnchar * 102920679Sdavidnpw_checkname(u_char *name, int gecos) 103020253Sjoerg{ 103120253Sjoerg int l = 0; 103221052Sdavidn char const *notch = gecos ? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\""; 103320253Sjoerg 103420253Sjoerg while (name[l]) { 103521052Sdavidn if (strchr(notch, name[l]) != NULL || name[l] < ' ' || name[l] == 127 || 103621052Sdavidn (!gecos && l==0 && name[l] == '-') || /* leading '-' */ 103721052Sdavidn (!gecos && name[l] & 0x80)) /* 8-bit */ 103821052Sdavidn cmderr(EX_DATAERR, (name[l] >= ' ' && name[l] < 127) 103920325Sjoerg ? "invalid character `%c' in field\n" 104021052Sdavidn : "invalid character 0x%02x in field\n", 104120325Sjoerg name[l]); 104220253Sjoerg ++l; 104320253Sjoerg } 104420325Sjoerg if (!gecos && l > MAXLOGNAME) 104520267Sjoerg cmderr(EX_DATAERR, "name too long `%s'\n", name); 104620679Sdavidn return (char *)name; 104720253Sjoerg} 104820253Sjoerg 104920253Sjoerg 105020253Sjoergstatic void 105120253Sjoergrmat(uid_t uid) 105220253Sjoerg{ 105320253Sjoerg DIR *d = opendir("/var/at/jobs"); 105420253Sjoerg 105520253Sjoerg if (d != NULL) { 105620253Sjoerg struct dirent *e; 105720253Sjoerg 105820253Sjoerg while ((e = readdir(d)) != NULL) { 105920253Sjoerg struct stat st; 106020253Sjoerg 106120253Sjoerg if (strncmp(e->d_name, ".lock", 5) != 0 && 106220253Sjoerg stat(e->d_name, &st) == 0 && 106320253Sjoerg !S_ISDIR(st.st_mode) && 106420253Sjoerg st.st_uid == uid) { 106520253Sjoerg char tmp[MAXPATHLEN]; 106620253Sjoerg 106720253Sjoerg sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 106820253Sjoerg system(tmp); 106920253Sjoerg } 107020253Sjoerg } 107120253Sjoerg closedir(d); 107220253Sjoerg } 107320253Sjoerg} 107420747Sdavidn 107520747Sdavidnstatic void 107620747Sdavidnrmskey(char const * name) 107720747Sdavidn{ 107820747Sdavidn static const char etcskey[] = "/etc/skeykeys"; 107921052Sdavidn FILE *fp = fopen(etcskey, "r+"); 108020747Sdavidn 108121052Sdavidn if (fp != NULL) { 108221052Sdavidn char tmp[1024]; 108321052Sdavidn off_t atofs = 0; 108421052Sdavidn int length = strlen(name); 108520747Sdavidn 108621052Sdavidn while (fgets(tmp, sizeof tmp, fp) != NULL) { 108721052Sdavidn if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 108821052Sdavidn if (fseek(fp, atofs, SEEK_SET) == 0) { 108921052Sdavidn fwrite("#", 1, 1, fp); /* Comment username out */ 109021052Sdavidn } 109121052Sdavidn break; 109220747Sdavidn } 109321052Sdavidn atofs = ftell(fp); 109420747Sdavidn } 109521052Sdavidn /* 109621052Sdavidn * If we got an error of any sort, don't update! 109721052Sdavidn */ 109821052Sdavidn fclose(fp); 109920747Sdavidn } 110020747Sdavidn} 110120747Sdavidn 1102