pw_user.c revision 20590
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 * 2620590Sdavidn * $Id: pw_user.c,v 1.4 1996/12/17 01:43:30 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 *pw_checkname(char *name, int gecos); 5320253Sjoergstatic char *shell_path(char const * path, char *shells[], char *sh); 5420253Sjoergstatic void rmat(uid_t uid); 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{ 9020253Sjoerg char *p = NULL; 9120253Sjoerg struct carg *a_name; 9220253Sjoerg struct carg *a_uid; 9320253Sjoerg struct carg *arg; 9420253Sjoerg struct passwd *pwd = NULL; 9520253Sjoerg struct group *grp; 9620253Sjoerg struct stat st; 9720253Sjoerg char line[MAXPWLINE]; 9820253Sjoerg 9920253Sjoerg static struct passwd fakeuser = 10020253Sjoerg { 10120253Sjoerg NULL, 10220253Sjoerg "*", 10320253Sjoerg -1, 10420253Sjoerg -1, 10520253Sjoerg 0, 10620253Sjoerg "", 10720253Sjoerg "User &", 10820253Sjoerg "/bin/sh", 10920253Sjoerg 0, 11020253Sjoerg 0 11120253Sjoerg }; 11220253Sjoerg 11320253Sjoerg /* 11420267Sjoerg * With M_NEXT, we only need to return the 11520267Sjoerg * next uid to stdout 11620267Sjoerg */ 11720267Sjoerg if (mode == M_NEXT) 11820267Sjoerg { 11920267Sjoerg uid_t next = pw_uidpolicy(cnf, args); 12020267Sjoerg if (getarg(args, 'q')) 12120267Sjoerg return next; 12220267Sjoerg printf("%ld:", (long)next); 12320267Sjoerg pw_group(cnf, mode, args); 12420267Sjoerg return EXIT_SUCCESS; 12520267Sjoerg } 12620267Sjoerg 12720267Sjoerg /* 12820253Sjoerg * We can do all of the common legwork here 12920253Sjoerg */ 13020253Sjoerg 13120253Sjoerg if ((arg = getarg(args, 'b')) != NULL) { 13220267Sjoerg cnf->home = arg->val; 13320267Sjoerg if (stat(cnf->home, &st) == -1 || !S_ISDIR(st.st_mode)) 13420267Sjoerg cmderr(EX_OSFILE, "root home `%s' is not a directory or does not exist\n", cnf->home); 13520253Sjoerg } 13620253Sjoerg if ((arg = getarg(args, 'e')) != NULL) 13720253Sjoerg cnf->expire_days = atoi(arg->val); 13820253Sjoerg 13920253Sjoerg if ((arg = getarg(args, 'p')) != NULL && arg->val) 14020253Sjoerg cnf->password_days = atoi(arg->val); 14120253Sjoerg 14220253Sjoerg if ((arg = getarg(args, 'g')) != NULL) { 14320253Sjoerg p = arg->val; 14420253Sjoerg if ((grp = getgrnam(p)) == NULL) { 14520253Sjoerg if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL) 14620267Sjoerg cmderr(EX_NOUSER, "group `%s' does not exist\n", p); 14720253Sjoerg } 14820253Sjoerg cnf->default_group = newstr(grp->gr_name); 14920253Sjoerg } 15020253Sjoerg if ((arg = getarg(args, 'L')) != NULL) 15120253Sjoerg cnf->default_class = pw_checkname(arg->val, 0); 15220253Sjoerg 15320253Sjoerg if ((arg = getarg(args, 'G')) != NULL && arg->val) { 15420253Sjoerg int i = 0; 15520253Sjoerg 15620253Sjoerg for (p = strtok(arg->val, ", \t"); i < _UC_MAXGROUPS && p != NULL; p = strtok(NULL, ", \t")) { 15720253Sjoerg if ((grp = getgrnam(p)) == NULL) { 15820253Sjoerg if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL) 15920267Sjoerg cmderr(EX_NOUSER, "group `%s' does not exist\n", p); 16020253Sjoerg } 16120253Sjoerg cnf->groups[i++] = newstr(grp->gr_name); 16220253Sjoerg } 16320253Sjoerg while (i < _UC_MAXGROUPS) 16420253Sjoerg cnf->groups[i++] = NULL; 16520253Sjoerg } 16620253Sjoerg if ((arg = getarg(args, 'k')) != NULL) { 16720253Sjoerg if (stat(cnf->dotdir = arg->val, &st) == -1 || S_ISDIR(st.st_mode)) 16820267Sjoerg cmderr(EX_OSFILE, "skeleton `%s' is not a directory or does not exist\n", cnf->dotdir); 16920253Sjoerg } 17020253Sjoerg if ((arg = getarg(args, 's')) != NULL) 17120253Sjoerg cnf->shell_default = arg->val; 17220253Sjoerg 17320253Sjoerg if (mode == M_ADD && getarg(args, 'D')) { 17420253Sjoerg if (getarg(args, 'n') != NULL) 17520267Sjoerg cmderr(EX_DATAERR, "can't combine `-D' with `-n name'\n"); 17620253Sjoerg if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 17720253Sjoerg if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 17820253Sjoerg cnf->min_uid = 1000; 17920253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 18020253Sjoerg cnf->max_uid = 32000; 18120253Sjoerg } 18220253Sjoerg if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 18320253Sjoerg if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 18420253Sjoerg cnf->min_gid = 1000; 18520253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 18620253Sjoerg cnf->max_gid = 32000; 18720253Sjoerg } 18820253Sjoerg if ((arg = getarg(args, 'w')) != NULL) 18920253Sjoerg cnf->default_password = boolean_val(arg->val, cnf->default_password); 19020253Sjoerg 19120253Sjoerg arg = getarg(args, 'C'); 19220253Sjoerg if (write_userconfig(arg ? arg->val : NULL)) 19320267Sjoerg return EXIT_SUCCESS; 19420253Sjoerg perror("config update"); 19520267Sjoerg return EX_IOERR; 19620253Sjoerg } 19720253Sjoerg if (mode == M_PRINT && getarg(args, 'a')) { 19820267Sjoerg int pretty = getarg(args, 'P') != NULL; 19920253Sjoerg 20020253Sjoerg setpwent(); 20120253Sjoerg while ((pwd = getpwent()) != NULL) 20220253Sjoerg print_user(pwd, pretty); 20320253Sjoerg endpwent(); 20420267Sjoerg return EXIT_SUCCESS; 20520253Sjoerg } 20620253Sjoerg if ((a_name = getarg(args, 'n')) != NULL) 20720253Sjoerg pwd = getpwnam(pw_checkname(a_name->val, 0)); 20820253Sjoerg a_uid = getarg(args, 'u'); 20920253Sjoerg 21020253Sjoerg if (a_uid == NULL) { 21120253Sjoerg if (a_name == NULL) 21220267Sjoerg cmderr(EX_DATAERR, "user name or id required\n"); 21320253Sjoerg 21420253Sjoerg /* 21520253Sjoerg * Determine whether 'n' switch is name or uid - we don't 21620253Sjoerg * really don't really care which we have, but we need to 21720253Sjoerg * know. 21820253Sjoerg */ 21920253Sjoerg if (mode != M_ADD && pwd == NULL && isdigit(*a_name->val) && atoi(a_name->val) > 0) { /* Assume uid */ 22020253Sjoerg (a_uid = a_name)->ch = 'u'; 22120253Sjoerg a_name = NULL; 22220253Sjoerg } 22320253Sjoerg } 22420253Sjoerg /* 22520253Sjoerg * Update, delete & print require that the user exists 22620253Sjoerg */ 22720253Sjoerg if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) { 22820253Sjoerg if (a_name == NULL && pwd == NULL) /* Try harder */ 22920253Sjoerg pwd = getpwuid(atoi(a_uid->val)); 23020253Sjoerg 23120253Sjoerg if (pwd == NULL) { 23220253Sjoerg if (mode == M_PRINT && getarg(args, 'F')) { 23320253Sjoerg fakeuser.pw_name = a_name ? a_name->val : "nouser"; 23420253Sjoerg fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 23520267Sjoerg return print_user(&fakeuser, getarg(args, 'P') != NULL); 23620253Sjoerg } 23720253Sjoerg if (a_name == NULL) 23820267Sjoerg cmderr(EX_NOUSER, "no such uid `%s'\n", a_uid->val); 23920267Sjoerg cmderr(EX_NOUSER, "no such user `%s'\n", a_name->val); 24020253Sjoerg } 24120253Sjoerg if (a_name == NULL) /* May be needed later */ 24220253Sjoerg a_name = addarg(args, 'n', newstr(pwd->pw_name)); 24320253Sjoerg 24420253Sjoerg /* 24520253Sjoerg * Handle deletions now 24620253Sjoerg */ 24720253Sjoerg if (mode == M_DELETE) { 24820253Sjoerg char file[MAXPATHLEN]; 24920253Sjoerg char home[MAXPATHLEN]; 25020253Sjoerg uid_t uid = pwd->pw_uid; 25120253Sjoerg 25220253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 25320267Sjoerg cmderr(EX_DATAERR, "cannot remove user 'root'\n"); 25420253Sjoerg 25520253Sjoerg /* 25620253Sjoerg * Remove crontabs 25720253Sjoerg */ 25820253Sjoerg sprintf(file, "/var/cron/tabs/%s", pwd->pw_name); 25920253Sjoerg if (access(file, F_OK) == 0) { 26020253Sjoerg sprintf(file, "crontab -u %s -r", pwd->pw_name); 26120253Sjoerg system(file); 26220253Sjoerg } 26320253Sjoerg /* 26420253Sjoerg * Save these for later, since contents of pwd may be 26520253Sjoerg * invalidated by deletion 26620253Sjoerg */ 26720253Sjoerg sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 26820253Sjoerg strncpy(home, pwd->pw_dir, sizeof home); 26920253Sjoerg home[sizeof home - 1] = '\0'; 27020253Sjoerg 27120253Sjoerg if (!delpwent(pwd)) 27220267Sjoerg cmderr(EX_IOERR, "Error updating passwd file: %s\n", strerror(errno)); 27320253Sjoerg editgroups(a_name->val, NULL); 27420253Sjoerg 27520253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 27620253Sjoerg 27720253Sjoerg /* 27820253Sjoerg * Remove mail file 27920253Sjoerg */ 28020253Sjoerg remove(file); 28120253Sjoerg 28220253Sjoerg /* 28320253Sjoerg * Remove at jobs 28420253Sjoerg */ 28520253Sjoerg if (getpwuid(uid) == NULL) 28620253Sjoerg rmat(uid); 28720253Sjoerg 28820253Sjoerg /* 28920253Sjoerg * Remove home directory and contents 29020253Sjoerg */ 29120253Sjoerg if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 29220253Sjoerg if (stat(home, &st) != -1) { 29320253Sjoerg rm_r(home, uid); 29420253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 29520253Sjoerg a_name->val, (long) uid, home, 29620253Sjoerg stat(home, &st) == -1 ? "" : "not completely "); 29720253Sjoerg } 29820253Sjoerg } 29920267Sjoerg return EXIT_SUCCESS; 30020253Sjoerg } else if (mode == M_PRINT) 30120267Sjoerg return print_user(pwd, getarg(args, 'P') != NULL); 30220253Sjoerg 30320253Sjoerg /* 30420253Sjoerg * The rest is edit code 30520253Sjoerg */ 30620253Sjoerg if ((arg = getarg(args, 'l')) != NULL) { 30720253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 30820267Sjoerg cmderr(EX_DATAERR, "can't rename `root' account\n"); 30920253Sjoerg pwd->pw_name = pw_checkname(arg->val, 0); 31020253Sjoerg } 31120253Sjoerg if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) { 31220253Sjoerg pwd->pw_uid = (uid_t) atol(arg->val); 31320253Sjoerg if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 31420267Sjoerg cmderr(EX_DATAERR, "can't change uid of `root' account\n"); 31520253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 31620253Sjoerg fprintf(stderr, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd->pw_name); 31720253Sjoerg } 31820253Sjoerg if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) /* Already checked this */ 31920253Sjoerg pwd->pw_gid = (gid_t) getgrnam(cnf->default_group)->gr_gid; 32020253Sjoerg 32120253Sjoerg if ((arg = getarg(args, 'p')) != NULL) { 32220253Sjoerg if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) 32320253Sjoerg pwd->pw_change = 0; 32420253Sjoerg else { 32520253Sjoerg time_t now = time(NULL); 32620253Sjoerg time_t expire = parse_date(now, arg->val); 32720253Sjoerg 32820253Sjoerg if (now == expire) 32920267Sjoerg cmderr(EX_DATAERR, "Invalid password change date `%s'\n", arg->val); 33020253Sjoerg pwd->pw_change = expire; 33120253Sjoerg } 33220253Sjoerg } 33320267Sjoerg if ((arg = getarg(args, 'e')) != NULL) { 33420253Sjoerg if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) 33520253Sjoerg pwd->pw_expire = 0; 33620253Sjoerg else { 33720253Sjoerg time_t now = time(NULL); 33820253Sjoerg time_t expire = parse_date(now, arg->val); 33920253Sjoerg 34020253Sjoerg if (now == expire) 34120267Sjoerg cmderr(EX_DATAERR, "Invalid account expiry date `%s'\n", arg->val); 34220253Sjoerg pwd->pw_expire = expire; 34320253Sjoerg } 34420253Sjoerg } 34520253Sjoerg if ((arg = getarg(args, 's')) != NULL) 34620253Sjoerg pwd->pw_shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 34720253Sjoerg 34820253Sjoerg if (getarg(args, 'L')) 34920253Sjoerg pwd->pw_class = cnf->default_class; 35020253Sjoerg 35120267Sjoerg if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL) 35220267Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 35320267Sjoerg 35420253Sjoerg } else { 35520253Sjoerg if (a_name == NULL) /* Required */ 35620267Sjoerg cmderr(EX_DATAERR, "login name required\n"); 35720253Sjoerg else if ((pwd = getpwnam(a_name->val)) != NULL) /* Exists */ 35820267Sjoerg cmderr(EX_DATAERR, "login name `%s' already exists\n", a_name->val); 35920253Sjoerg 36020253Sjoerg /* 36120253Sjoerg * Now, set up defaults for a new user 36220253Sjoerg */ 36320253Sjoerg pwd = &fakeuser; 36420253Sjoerg pwd->pw_name = a_name->val; 36520253Sjoerg pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 36620253Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 36720253Sjoerg pwd->pw_uid = pw_uidpolicy(cnf, args); 36820253Sjoerg pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 36920253Sjoerg pwd->pw_change = pw_pwdpolicy(cnf, args); 37020253Sjoerg pwd->pw_expire = pw_exppolicy(cnf, args); 37120253Sjoerg pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 37220253Sjoerg pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 37320253Sjoerg 37420253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 37520253Sjoerg fprintf(stderr, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd->pw_name); 37620253Sjoerg } 37720253Sjoerg 37820253Sjoerg /* 37920253Sjoerg * Shared add/edit code 38020253Sjoerg */ 38120253Sjoerg if ((arg = getarg(args, 'c')) != NULL) 38220253Sjoerg pwd->pw_gecos = pw_checkname(arg->val, 1); 38320253Sjoerg 38420253Sjoerg if ((arg = getarg(args, 'h')) != NULL) { 38520253Sjoerg if (strcmp(arg->val, "-") == 0) 38620253Sjoerg pwd->pw_passwd = "*"; /* No access */ 38720253Sjoerg else { 38820253Sjoerg int fd = atoi(arg->val); 38920253Sjoerg int b; 39020253Sjoerg int istty = isatty(fd); 39120253Sjoerg struct termios t; 39220253Sjoerg 39320253Sjoerg if (istty) { 39420253Sjoerg if (tcgetattr(fd, &t) == -1) 39520253Sjoerg istty = 0; 39620253Sjoerg else { 39720253Sjoerg struct termios n = t; 39820253Sjoerg 39920253Sjoerg /* Disable echo */ 40020253Sjoerg n.c_lflag &= ~(ECHO); 40120253Sjoerg tcsetattr(fd, TCSANOW, &n); 40220253Sjoerg printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name); 40320253Sjoerg fflush(stdout); 40420253Sjoerg } 40520253Sjoerg } 40620253Sjoerg b = read(fd, line, sizeof(line) - 1); 40720253Sjoerg if (istty) { /* Restore state */ 40820253Sjoerg tcsetattr(fd, TCSANOW, &t); 40920253Sjoerg fputc('\n', stdout); 41020253Sjoerg fflush(stdout); 41120253Sjoerg } 41220253Sjoerg if (b < 0) { 41320253Sjoerg perror("-h file descriptor"); 41420267Sjoerg return EX_IOERR; 41520253Sjoerg } 41620253Sjoerg line[b] = '\0'; 41720253Sjoerg if ((p = strpbrk(line, " \t\r\n")) != NULL) 41820253Sjoerg *p = '\0'; 41920253Sjoerg if (!*line) 42020267Sjoerg cmderr(EX_DATAERR, "empty password read on file descriptor %d\n", fd); 42120253Sjoerg pwd->pw_passwd = pw_pwcrypt(line); 42220253Sjoerg } 42320253Sjoerg } 42420267Sjoerg 42520267Sjoerg /* 42620267Sjoerg * Special case: -N only displays & exits 42720267Sjoerg */ 42820267Sjoerg if (getarg(args, 'N') != NULL) 42920267Sjoerg return print_user(pwd, getarg(args, 'P') != NULL); 43020267Sjoerg 43120253Sjoerg if ((mode == M_ADD && !addpwent(pwd)) || 43220253Sjoerg (mode == M_UPDATE && !chgpwent(a_name->val, pwd))) { 43320253Sjoerg perror("password update"); 43420267Sjoerg return EX_IOERR; 43520253Sjoerg } 43620253Sjoerg /* 43720253Sjoerg * Ok, user is created or changed - now edit group file 43820253Sjoerg */ 43920253Sjoerg 44020253Sjoerg if (mode == M_ADD || getarg(args, 'G') != NULL) 44120253Sjoerg editgroups(pwd->pw_name, cnf->groups); 44220253Sjoerg 44320253Sjoerg /* pwd may have been invalidated */ 44420253Sjoerg if ((pwd = getpwnam(a_name->val)) == NULL) 44520267Sjoerg cmderr(EX_NOUSER, "user '%s' disappeared during update\n", a_name->val); 44620253Sjoerg 44720253Sjoerg grp = getgrgid(pwd->pw_gid); 44820253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s", 44920253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 45020253Sjoerg grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 45120253Sjoerg pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 45220253Sjoerg 45320253Sjoerg /* 45420253Sjoerg * If adding, let's touch and chown the user's mail file. This is not 45520253Sjoerg * strictly necessary under BSD with a 0755 maildir but it also 45620253Sjoerg * doesn't hurt anything to create the empty mailfile 45720253Sjoerg */ 45820253Sjoerg if (mode == M_ADD) { 45920253Sjoerg FILE *fp; 46020253Sjoerg 46120253Sjoerg sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 46220253Sjoerg close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 46320253Sjoerg * mtime */ 46420253Sjoerg chown(line, pwd->pw_uid, pwd->pw_gid); 46520253Sjoerg 46620253Sjoerg /* 46720253Sjoerg * Send mail to the new user as well, if we are asked to 46820253Sjoerg */ 46920253Sjoerg if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 47020253Sjoerg FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 47120253Sjoerg 47220253Sjoerg if (pfp == NULL) 47320253Sjoerg perror("sendmail"); 47420253Sjoerg else { 47520253Sjoerg fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 47620253Sjoerg while (fgets(line, sizeof(line), fp) != NULL) { 47720253Sjoerg /* Do substitutions? */ 47820253Sjoerg fputs(line, pfp); 47920253Sjoerg } 48020253Sjoerg pclose(pfp); 48120253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 48220253Sjoerg pwd->pw_name, (long) pwd->pw_uid); 48320253Sjoerg } 48420253Sjoerg fclose(fp); 48520253Sjoerg } 48620253Sjoerg } 48720253Sjoerg /* 48820253Sjoerg * Finally, let's create and populate the user's home directory. Note 48920253Sjoerg * that this also `works' for editing users if -m is used, but 49020253Sjoerg * existing files will *not* be overwritten. 49120253Sjoerg */ 49220253Sjoerg if (getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 49320253Sjoerg copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid); 49420253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 49520253Sjoerg pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 49620253Sjoerg } 49720267Sjoerg return EXIT_SUCCESS; 49820253Sjoerg} 49920253Sjoerg 50020253Sjoerg 50120253Sjoergstatic uid_t 50220253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args) 50320253Sjoerg{ 50420253Sjoerg struct passwd *pwd; 50520253Sjoerg uid_t uid = (uid_t) - 1; 50620253Sjoerg struct carg *a_uid = getarg(args, 'u'); 50720253Sjoerg 50820253Sjoerg /* 50920253Sjoerg * Check the given uid, if any 51020253Sjoerg */ 51120253Sjoerg if (a_uid != NULL) { 51220253Sjoerg uid = (uid_t) atol(a_uid->val); 51320253Sjoerg 51420253Sjoerg if ((pwd = getpwuid(uid)) != NULL && getarg(args, 'o') == NULL) 51520267Sjoerg cmderr(EX_DATAERR, "uid `%ld' has already been allocated\n", (long) pwd->pw_uid); 51620253Sjoerg } else { 51720253Sjoerg struct bitmap bm; 51820253Sjoerg 51920253Sjoerg /* 52020253Sjoerg * We need to allocate the next available uid under one of 52120253Sjoerg * two policies a) Grab the first unused uid b) Grab the 52220253Sjoerg * highest possible unused uid 52320253Sjoerg */ 52420253Sjoerg if (cnf->min_uid >= cnf->max_uid) { /* Sanity 52520253Sjoerg * claus^H^H^H^Hheck */ 52620253Sjoerg cnf->min_uid = 1000; 52720253Sjoerg cnf->max_uid = 32000; 52820253Sjoerg } 52920253Sjoerg bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 53020253Sjoerg 53120253Sjoerg /* 53220253Sjoerg * Now, let's fill the bitmap from the password file 53320253Sjoerg */ 53420253Sjoerg setpwent(); 53520253Sjoerg while ((pwd = getpwent()) != NULL) 53620253Sjoerg if (pwd->pw_uid >= (int) cnf->min_uid && pwd->pw_uid <= (int) cnf->max_uid) 53720253Sjoerg bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 53820253Sjoerg endpwent(); 53920253Sjoerg 54020253Sjoerg /* 54120253Sjoerg * Then apply the policy, with fallback to reuse if necessary 54220253Sjoerg */ 54320253Sjoerg if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 54420253Sjoerg uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 54520253Sjoerg 54620253Sjoerg /* 54720253Sjoerg * Another sanity check 54820253Sjoerg */ 54920253Sjoerg if (uid < cnf->min_uid || uid > cnf->max_uid) 55020267Sjoerg cmderr(EX_SOFTWARE, "unable to allocate a new uid - range fully used\n"); 55120253Sjoerg bm_dealloc(&bm); 55220253Sjoerg } 55320253Sjoerg return uid; 55420253Sjoerg} 55520253Sjoerg 55620253Sjoerg 55720253Sjoergstatic uid_t 55820253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 55920253Sjoerg{ 56020253Sjoerg struct group *grp; 56120253Sjoerg gid_t gid = (uid_t) - 1; 56220253Sjoerg struct carg *a_gid = getarg(args, 'g'); 56320253Sjoerg 56420253Sjoerg /* 56520253Sjoerg * If no arg given, see if default can help out 56620253Sjoerg */ 56720253Sjoerg if (a_gid == NULL && cnf->default_group && *cnf->default_group) 56820253Sjoerg a_gid = addarg(args, 'g', cnf->default_group); 56920253Sjoerg 57020253Sjoerg /* 57120253Sjoerg * Check the given gid, if any 57220253Sjoerg */ 57320267Sjoerg setgrent(); 57420253Sjoerg if (a_gid != NULL) { 57520253Sjoerg if ((grp = getgrnam(a_gid->val)) == NULL) { 57620253Sjoerg gid = (gid_t) atol(a_gid->val); 57720253Sjoerg if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = getgrgid(gid)) == NULL) 57820267Sjoerg cmderr(EX_NOUSER, "group `%s' is not defined\n", a_gid->val); 57920253Sjoerg } 58020253Sjoerg gid = grp->gr_gid; 58120267Sjoerg } else if ((grp = getgrnam(nam)) != NULL && grp->gr_mem[0] == NULL) { 58220267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 58320253Sjoerg } else { 58420253Sjoerg struct cargs grpargs; 58520253Sjoerg char tmp[32]; 58620253Sjoerg 58720253Sjoerg LIST_INIT(&grpargs); 58820253Sjoerg addarg(&grpargs, 'n', nam); 58920253Sjoerg 59020253Sjoerg /* 59120253Sjoerg * We need to auto-create a group with the user's name. We 59220253Sjoerg * can send all the appropriate output to our sister routine 59320253Sjoerg * bit first see if we can create a group with gid==uid so we 59420253Sjoerg * can keep the user and group ids in sync. We purposely do 59520253Sjoerg * NOT check the gid range if we can force the sync. If the 59620253Sjoerg * user's name dups an existing group, then the group add 59720253Sjoerg * function will happily handle that case for us and exit. 59820253Sjoerg */ 59920253Sjoerg if (getgrgid(prefer) == NULL) { 60020253Sjoerg sprintf(tmp, "%lu", (unsigned long) prefer); 60120253Sjoerg addarg(&grpargs, 'g', tmp); 60220253Sjoerg } 60320267Sjoerg if (getarg(args, 'N')) 60420267Sjoerg { 60520267Sjoerg addarg(&grpargs, 'N', NULL); 60620267Sjoerg addarg(&grpargs, 'q', NULL); 60720267Sjoerg gid = pw_group(cnf, M_NEXT, &grpargs); 60820267Sjoerg } 60920267Sjoerg else 61020267Sjoerg { 61120267Sjoerg pw_group(cnf, M_ADD, &grpargs); 61220267Sjoerg if ((grp = getgrnam(nam)) != NULL) 61320267Sjoerg gid = grp->gr_gid; 61420267Sjoerg } 61520253Sjoerg a_gid = grpargs.lh_first; 61620253Sjoerg while (a_gid != NULL) { 61720253Sjoerg struct carg *t = a_gid->list.le_next; 61820253Sjoerg LIST_REMOVE(a_gid, list); 61920253Sjoerg a_gid = t; 62020253Sjoerg } 62120253Sjoerg } 62220267Sjoerg endgrent(); 62320253Sjoerg return gid; 62420253Sjoerg} 62520253Sjoerg 62620253Sjoerg 62720253Sjoergstatic time_t 62820253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args) 62920253Sjoerg{ 63020253Sjoerg time_t result = 0; 63120253Sjoerg time_t now = time(NULL); 63220253Sjoerg struct carg *arg = getarg(args, 'e'); 63320253Sjoerg 63420253Sjoerg if (arg != NULL) { 63520253Sjoerg if ((result = parse_date(now, arg->val)) == now) 63620267Sjoerg cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val); 63720253Sjoerg } else if (cnf->password_days > 0) 63820253Sjoerg result = now + ((long) cnf->password_days * 86400L); 63920253Sjoerg return result; 64020253Sjoerg} 64120253Sjoerg 64220253Sjoerg 64320253Sjoergstatic time_t 64420253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args) 64520253Sjoerg{ 64620253Sjoerg time_t result = 0; 64720253Sjoerg time_t now = time(NULL); 64820253Sjoerg struct carg *arg = getarg(args, 'e'); 64920253Sjoerg 65020253Sjoerg if (arg != NULL) { 65120253Sjoerg if ((result = parse_date(now, arg->val)) == now) 65220267Sjoerg cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val); 65320253Sjoerg } else if (cnf->expire_days > 0) 65420253Sjoerg result = now + ((long) cnf->expire_days * 86400L); 65520253Sjoerg return result; 65620253Sjoerg} 65720253Sjoerg 65820253Sjoerg 65920253Sjoergstatic char * 66020253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 66120253Sjoerg{ 66220253Sjoerg struct carg *arg = getarg(args, 'd'); 66320253Sjoerg 66420253Sjoerg if (arg) 66520253Sjoerg return arg->val; 66620253Sjoerg else { 66720253Sjoerg static char home[128]; 66820253Sjoerg 66920253Sjoerg if (cnf->home == NULL || *cnf->home == '\0') 67020267Sjoerg cmderr(EX_CONFIG, "no base home directory set\n"); 67120253Sjoerg sprintf(home, "%s/%s", cnf->home, user); 67220253Sjoerg return home; 67320253Sjoerg } 67420253Sjoerg} 67520253Sjoerg 67620253Sjoergstatic char * 67720253Sjoergshell_path(char const * path, char *shells[], char *sh) 67820253Sjoerg{ 67920253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 68020253Sjoerg return sh; /* specified full path or forced none */ 68120253Sjoerg else { 68220253Sjoerg char *p; 68320253Sjoerg char paths[_UC_MAXLINE]; 68420253Sjoerg 68520253Sjoerg /* 68620253Sjoerg * We need to search paths 68720253Sjoerg */ 68820253Sjoerg strncpy(paths, path, sizeof paths); 68920253Sjoerg paths[sizeof paths - 1] = '\0'; 69020253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 69120253Sjoerg int i; 69220253Sjoerg static char shellpath[256]; 69320253Sjoerg 69420253Sjoerg if (sh != NULL) { 69520253Sjoerg sprintf(shellpath, "%s/%s", p, sh); 69620253Sjoerg if (access(shellpath, X_OK) == 0) 69720253Sjoerg return shellpath; 69820253Sjoerg } else 69920253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 70020253Sjoerg sprintf(shellpath, "%s/%s", p, shells[i]); 70120253Sjoerg if (access(shellpath, X_OK) == 0) 70220253Sjoerg return shellpath; 70320253Sjoerg } 70420253Sjoerg } 70520253Sjoerg if (sh == NULL) 70620267Sjoerg cmderr(EX_OSFILE, "can't find shell `%s' in shell paths\n", sh); 70720267Sjoerg cmderr(EX_CONFIG, "no default shell available or defined\n"); 70820253Sjoerg return NULL; 70920253Sjoerg } 71020253Sjoerg} 71120253Sjoerg 71220253Sjoerg 71320253Sjoergstatic char * 71420253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 71520253Sjoerg{ 71620253Sjoerg char *sh = newshell; 71720253Sjoerg struct carg *arg = getarg(args, 's'); 71820253Sjoerg 71920253Sjoerg if (newshell == NULL && arg != NULL) 72020253Sjoerg sh = arg->val; 72120253Sjoerg return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 72220253Sjoerg} 72320253Sjoerg 72420253Sjoergstatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."; 72520253Sjoerg 72620253Sjoergchar * 72720253Sjoergpw_pwcrypt(char *password) 72820253Sjoerg{ 72920253Sjoerg int i; 73020253Sjoerg char salt[12]; 73120253Sjoerg 73220253Sjoerg static char buf[256]; 73320253Sjoerg 73420253Sjoerg /* 73520253Sjoerg * Calculate a salt value 73620253Sjoerg */ 73720555Sdavidn srandom((unsigned) (time(NULL) ^ getpid())); 73820253Sjoerg for (i = 0; i < 8; i++) 73920253Sjoerg salt[i] = chars[random() % 63]; 74020253Sjoerg salt[i] = '\0'; 74120253Sjoerg 74220253Sjoerg return strcpy(buf, crypt(password, salt)); 74320253Sjoerg} 74420253Sjoerg 74520590Sdavidn#if defined(__FreeBSD__) 74620590Sdavidn 74720590Sdavidn#if defined(USE_MD5RAND) 74820555Sdavidnu_char * 74920590Sdavidnpw_getrand(u_char *buf, int len) /* cryptographically secure rng */ 75020555Sdavidn{ 75120590Sdavidn int i; 75220590Sdavidn for (i=0;i<len;i+=16) { 75320590Sdavidn u_char ubuf[16]; 75420590Sdavidn 75520590Sdavidn MD5_CTX md5_ctx; 75620590Sdavidn struct timeval tv, tvo; 75720590Sdavidn struct rusage ru; 75820590Sdavidn int n=0; 75920590Sdavidn int t; 76020590Sdavidn 76120590Sdavidn MD5Init (&md5_ctx); 76220590Sdavidn t=getpid(); 76320590Sdavidn MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 76420590Sdavidn t=getppid(); 76520590Sdavidn MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 76620590Sdavidn gettimeofday (&tvo, NULL); 76720590Sdavidn do { 76820590Sdavidn getrusage (RUSAGE_SELF, &ru); 76920590Sdavidn MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru); 77020590Sdavidn gettimeofday (&tv, NULL); 77120590Sdavidn MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv); 77220590Sdavidn } while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000); 77320590Sdavidn MD5Final (ubuf, &md5_ctx); 77420590Sdavidn memcpy(buf+i, ubuf, MIN(16, len-n)); 77520590Sdavidn } 77620590Sdavidn return buf; 77720555Sdavidn} 77820253Sjoerg 77920590Sdavidn#else /* Use random device (preferred) */ 78020590Sdavidn 78120555Sdavidnstatic u_char * 78220555Sdavidnpw_getrand(u_char *buf, int len) 78320555Sdavidn{ 78420555Sdavidn int fd; 78520555Sdavidn fd = open("/dev/urandom", O_RDONLY); 78620590Sdavidn if (fd==-1) 78720590Sdavidn cmderr(EX_OSFILE, "can't open /dev/urandom: %s\n", strerror(errno)); 78820590Sdavidn else if (read(fd, buf, len)!=len) 78920590Sdavidn cmderr(EX_IOERR, "read error on /dev/urandom\n"); 79020555Sdavidn close(fd); 79120555Sdavidn return buf; 79220555Sdavidn} 79320555Sdavidn 79420590Sdavidn#endif 79520590Sdavidn 79620590Sdavidn#else /* Portable version */ 79720590Sdavidn 79820590Sdavidnstatic u_char * 79920590Sdavidnpw_getrand(u_char *buf, int len) 80020590Sdavidn{ 80120590Sdavidn int i; 80220590Sdavidn 80320590Sdavidn for (i = 0; i < len; i++) { 80420590Sdavidn unsigned val = random(); 80520590Sdavidn /* Use all bits in the random value */ 80620590Sdavidn buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val); 80720590Sdavidn } 80820590Sdavidn return buf; 80920590Sdavidn} 81020590Sdavidn 81120590Sdavidn#endif 81220590Sdavidn 81320253Sjoergstatic char * 81420253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user) 81520253Sjoerg{ 81620253Sjoerg int i, l; 81720253Sjoerg char pwbuf[32]; 81820555Sdavidn u_char rndbuf[sizeof pwbuf]; 81920253Sjoerg 82020253Sjoerg switch (cnf->default_password) { 82120253Sjoerg case -1: /* Random password */ 82220555Sdavidn srandom((unsigned) (time(NULL) ^ getpid())); 82320253Sjoerg l = (random() % 8 + 8); /* 8 - 16 chars */ 82420555Sdavidn pw_getrand(rndbuf, l); 82520253Sjoerg for (i = 0; i < l; i++) 82620555Sdavidn pwbuf[i] = chars[rndbuf[i] % sizeof(chars)]; 82720253Sjoerg pwbuf[i] = '\0'; 82820253Sjoerg 82920253Sjoerg /* 83020253Sjoerg * We give this information back to the user 83120253Sjoerg */ 83220267Sjoerg if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) { 83320253Sjoerg if (isatty(0)) 83420253Sjoerg printf("Password is: "); 83520253Sjoerg printf("%s\n", pwbuf); 83620253Sjoerg fflush(stdout); 83720253Sjoerg } 83820253Sjoerg break; 83920253Sjoerg 84020253Sjoerg case -2: /* No password at all! */ 84120253Sjoerg return ""; 84220253Sjoerg 84320253Sjoerg case 0: /* No login - default */ 84420253Sjoerg default: 84520253Sjoerg return "*"; 84620253Sjoerg 84720253Sjoerg case 1: /* user's name */ 84820253Sjoerg strncpy(pwbuf, user, sizeof pwbuf); 84920253Sjoerg pwbuf[sizeof pwbuf - 1] = '\0'; 85020253Sjoerg break; 85120253Sjoerg } 85220253Sjoerg return pw_pwcrypt(pwbuf); 85320253Sjoerg} 85420253Sjoerg 85520253Sjoerg 85620253Sjoergstatic int 85720253Sjoergprint_user(struct passwd * pwd, int pretty) 85820253Sjoerg{ 85920253Sjoerg if (!pretty) { 86020253Sjoerg char buf[_UC_MAXLINE]; 86120253Sjoerg 86220253Sjoerg fmtpwent(buf, pwd); 86320253Sjoerg fputs(buf, stdout); 86420253Sjoerg } else { 86520267Sjoerg int j; 86620253Sjoerg char *p; 86720253Sjoerg struct group *grp = getgrgid(pwd->pw_gid); 86820253Sjoerg char uname[60] = "User &", office[60] = "[None]", 86920253Sjoerg wphone[60] = "[None]", hphone[60] = "[None]"; 87020590Sdavidn char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 87120590Sdavidn struct tm * tptr; 87220253Sjoerg 87320253Sjoerg if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 87420253Sjoerg strncpy(uname, p, sizeof uname); 87520253Sjoerg uname[sizeof uname - 1] = '\0'; 87620253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 87720253Sjoerg strncpy(office, p, sizeof office); 87820253Sjoerg office[sizeof office - 1] = '\0'; 87920253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 88020253Sjoerg strncpy(wphone, p, sizeof wphone); 88120253Sjoerg wphone[sizeof wphone - 1] = '\0'; 88220253Sjoerg if ((p = strtok(NULL, "")) != NULL) { 88320253Sjoerg strncpy(hphone, p, sizeof hphone); 88420253Sjoerg hphone[sizeof hphone - 1] = '\0'; 88520253Sjoerg } 88620253Sjoerg } 88720253Sjoerg } 88820253Sjoerg } 88920253Sjoerg /* 89020253Sjoerg * Handle '&' in gecos field 89120253Sjoerg */ 89220253Sjoerg if ((p = strchr(uname, '&')) != NULL) { 89320253Sjoerg int l = strlen(pwd->pw_name); 89420253Sjoerg int m = strlen(p); 89520253Sjoerg 89620253Sjoerg memmove(p + l, p + 1, m); 89720253Sjoerg memmove(p, pwd->pw_name, l); 89820253Sjoerg *p = (char) toupper(*p); 89920253Sjoerg } 90020590Sdavidn if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 90120590Sdavidn strftime(acexpire, sizeof acexpire, "%c", tptr); 90220590Sdavidn if (pwd->pw_change > (time_t)9 && (tptr = localtime(&pwd->pw_change)) != NULL) 90320590Sdavidn strftime(pwexpire, sizeof pwexpire, "%c", tptr); 90420253Sjoerg printf("Login Name : %-10s #%-22ld Group : %-10s #%ld\n" 90520253Sjoerg " Full Name : %s\n" 90620253Sjoerg " Home : %-32.32s Class : %s\n" 90720253Sjoerg " Shell : %-32.32s Office : %s\n" 90820590Sdavidn "Work Phone : %-32.32s Home Phone : %s\n" 90920590Sdavidn "Acc Expire : %-32.32s Pwd Expire : %s\n", 91020253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 91120253Sjoerg grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 91220253Sjoerg uname, pwd->pw_dir, pwd->pw_class, 91320590Sdavidn pwd->pw_shell, office, wphone, hphone, 91420590Sdavidn acexpire, pwexpire); 91520267Sjoerg setgrent(); 91620267Sjoerg j = 0; 91720267Sjoerg while ((grp=getgrent()) != NULL) 91820267Sjoerg { 91920267Sjoerg int i = 0; 92020267Sjoerg while (i < _UC_MAXGROUPS && grp->gr_mem[i] != NULL) 92120267Sjoerg { 92220267Sjoerg if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 92320267Sjoerg { 92420267Sjoerg printf(j++ == 0 ? " Groups : %s" : ",%s", grp->gr_name); 92520267Sjoerg break; 92620267Sjoerg } 92720267Sjoerg ++i; 92820267Sjoerg } 92920267Sjoerg } 93020267Sjoerg endgrent(); 93120267Sjoerg printf("%s\n", j ? "\n" : ""); 93220253Sjoerg } 93320267Sjoerg return EXIT_SUCCESS; 93420253Sjoerg} 93520253Sjoerg 93620253Sjoergstatic char * 93720253Sjoergpw_checkname(char *name, int gecos) 93820253Sjoerg{ 93920253Sjoerg int l = 0; 94020325Sjoerg char const *notch = gecos ? ":" : " ,\t:+-&#%$^()!@~*?<>=|\\/\""; 94120253Sjoerg 94220253Sjoerg while (name[l]) { 94320325Sjoerg if (strchr(notch, name[l]) != NULL || name[l] < ' ' || name[l] > 126) 94420325Sjoerg cmderr(EX_DATAERR, (name[l]<' ' || (unsigned char)name[l] > 126) 94520325Sjoerg ? "invalid character `%c' in field\n" 94620325Sjoerg : "invalid character 0x$02x in field\n", 94720325Sjoerg name[l]); 94820253Sjoerg ++l; 94920253Sjoerg } 95020325Sjoerg if (!gecos && l > MAXLOGNAME) 95120267Sjoerg cmderr(EX_DATAERR, "name too long `%s'\n", name); 95220253Sjoerg return name; 95320253Sjoerg} 95420253Sjoerg 95520253Sjoerg 95620253Sjoergstatic void 95720253Sjoergrmat(uid_t uid) 95820253Sjoerg{ 95920253Sjoerg DIR *d = opendir("/var/at/jobs"); 96020253Sjoerg 96120253Sjoerg if (d != NULL) { 96220253Sjoerg struct dirent *e; 96320253Sjoerg 96420253Sjoerg while ((e = readdir(d)) != NULL) { 96520253Sjoerg struct stat st; 96620253Sjoerg 96720253Sjoerg if (strncmp(e->d_name, ".lock", 5) != 0 && 96820253Sjoerg stat(e->d_name, &st) == 0 && 96920253Sjoerg !S_ISDIR(st.st_mode) && 97020253Sjoerg st.st_uid == uid) { 97120253Sjoerg char tmp[MAXPATHLEN]; 97220253Sjoerg 97320253Sjoerg sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 97420253Sjoerg system(tmp); 97520253Sjoerg } 97620253Sjoerg } 97720253Sjoerg closedir(d); 97820253Sjoerg } 97920253Sjoerg} 980