pw_user.c revision 20267
120253Sjoerg/*- 220253Sjoerg * Copyright (c) 1996 by David L. Nugent <davidn@blaze.net.au>. 320253Sjoerg * 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 920253Sjoerg * notice, this list of conditions and the following disclaimer as 1020253Sjoerg * the first lines of this file unmodified. 1120253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright 1220253Sjoerg * notice, this list of conditions and the following disclaimer in the 1320253Sjoerg * documentation and/or other materials provided with the distribution. 1420253Sjoerg * 3. All advertising materials mentioning features or use of this software 1520253Sjoerg * must display the following acknowledgement: 1620253Sjoerg * This product includes software developed by David L. Nugent. 1720253Sjoerg * 4. The name of the author may not be used to endorse or promote products 1820253Sjoerg * derived from this software without specific prior written permission. 1920253Sjoerg * 2020253Sjoerg * THIS SOFTWARE IS PROVIDED BY THE DAVID L. NUGENT ``AS IS'' AND 2120253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2220253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2320253Sjoerg * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT BE LIABLE 2420253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2520253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2620253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2720253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2820253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2920253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3020253Sjoerg * SUCH DAMAGE. 3120253Sjoerg * 3220253Sjoerg * $Id$ 3320253Sjoerg */ 3420253Sjoerg 3520253Sjoerg#include <unistd.h> 3620253Sjoerg#include <fcntl.h> 3720253Sjoerg#include <ctype.h> 3820253Sjoerg#include <paths.h> 3920253Sjoerg#include <sys/param.h> 4020253Sjoerg#include <dirent.h> 4120253Sjoerg#include <termios.h> 4220253Sjoerg#include "pw.h" 4320253Sjoerg#include "bitmap.h" 4420253Sjoerg#include "pwupd.h" 4520253Sjoerg 4620253Sjoergstatic int print_user(struct passwd * pwd, int pretty); 4720253Sjoergstatic uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args); 4820253Sjoergstatic uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer); 4920253Sjoergstatic time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 5020253Sjoergstatic time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 5120253Sjoergstatic char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 5220253Sjoergstatic char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 5320253Sjoergstatic char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 5420253Sjoergstatic char *pw_checkname(char *name, int gecos); 5520253Sjoergstatic char *shell_path(char const * path, char *shells[], char *sh); 5620253Sjoergstatic void rmat(uid_t uid); 5720253Sjoerg 5820253Sjoerg/*- 5920253Sjoerg * -C config configuration file 6020253Sjoerg * -q quiet operation 6120253Sjoerg * -n name login name 6220253Sjoerg * -u uid user id 6320253Sjoerg * -c comment user name/comment 6420253Sjoerg * -d directory home directory 6520253Sjoerg * -e date account expiry date 6620253Sjoerg * -p date password expiry date 6720253Sjoerg * -g grp primary group 6820253Sjoerg * -G grp1,grp2 additional groups 6920253Sjoerg * -m [ -k dir ] create and set up home 7020253Sjoerg * -s shell name of login shell 7120253Sjoerg * -o duplicate uid ok 7220253Sjoerg * -L class user class 7320253Sjoerg * -l name new login name 7420253Sjoerg * -h fd password filehandle 7520253Sjoerg * -F force print or add 7620253Sjoerg * Setting defaults: 7720253Sjoerg * -D set user defaults 7820253Sjoerg * -b dir default home root dir 7920253Sjoerg * -e period default expiry period 8020253Sjoerg * -p period default password change period 8120253Sjoerg * -g group default group 8220253Sjoerg * -G grp1,grp2.. default additional groups 8320253Sjoerg * -L class default login class 8420253Sjoerg * -k dir default home skeleton 8520253Sjoerg * -s shell default shell 8620253Sjoerg * -w method default password method 8720253Sjoerg */ 8820253Sjoerg 8920253Sjoergint 9020253Sjoergpw_user(struct userconf * cnf, int mode, struct cargs * args) 9120253Sjoerg{ 9220253Sjoerg char *p = NULL; 9320253Sjoerg struct carg *a_name; 9420253Sjoerg struct carg *a_uid; 9520253Sjoerg struct carg *arg; 9620253Sjoerg struct passwd *pwd = NULL; 9720253Sjoerg struct group *grp; 9820253Sjoerg struct stat st; 9920253Sjoerg char line[MAXPWLINE]; 10020253Sjoerg 10120253Sjoerg static struct passwd fakeuser = 10220253Sjoerg { 10320253Sjoerg NULL, 10420253Sjoerg "*", 10520253Sjoerg -1, 10620253Sjoerg -1, 10720253Sjoerg 0, 10820253Sjoerg "", 10920253Sjoerg "User &", 11020253Sjoerg "/bin/sh", 11120253Sjoerg 0, 11220253Sjoerg 0 11320253Sjoerg }; 11420253Sjoerg 11520253Sjoerg /* 11620267Sjoerg * With M_NEXT, we only need to return the 11720267Sjoerg * next uid to stdout 11820267Sjoerg */ 11920267Sjoerg if (mode == M_NEXT) 12020267Sjoerg { 12120267Sjoerg uid_t next = pw_uidpolicy(cnf, args); 12220267Sjoerg if (getarg(args, 'q')) 12320267Sjoerg return next; 12420267Sjoerg printf("%ld:", (long)next); 12520267Sjoerg pw_group(cnf, mode, args); 12620267Sjoerg return EXIT_SUCCESS; 12720267Sjoerg } 12820267Sjoerg 12920267Sjoerg /* 13020253Sjoerg * We can do all of the common legwork here 13120253Sjoerg */ 13220253Sjoerg 13320253Sjoerg if ((arg = getarg(args, 'b')) != NULL) { 13420267Sjoerg cnf->home = arg->val; 13520267Sjoerg if (stat(cnf->home, &st) == -1 || !S_ISDIR(st.st_mode)) 13620267Sjoerg cmderr(EX_OSFILE, "root home `%s' is not a directory or does not exist\n", cnf->home); 13720253Sjoerg } 13820253Sjoerg if ((arg = getarg(args, 'e')) != NULL) 13920253Sjoerg cnf->expire_days = atoi(arg->val); 14020253Sjoerg 14120253Sjoerg if ((arg = getarg(args, 'p')) != NULL && arg->val) 14220253Sjoerg cnf->password_days = atoi(arg->val); 14320253Sjoerg 14420253Sjoerg if ((arg = getarg(args, 'g')) != NULL) { 14520253Sjoerg p = arg->val; 14620253Sjoerg if ((grp = getgrnam(p)) == NULL) { 14720253Sjoerg if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL) 14820267Sjoerg cmderr(EX_NOUSER, "group `%s' does not exist\n", p); 14920253Sjoerg } 15020253Sjoerg cnf->default_group = newstr(grp->gr_name); 15120253Sjoerg } 15220253Sjoerg if ((arg = getarg(args, 'L')) != NULL) 15320253Sjoerg cnf->default_class = pw_checkname(arg->val, 0); 15420253Sjoerg 15520253Sjoerg if ((arg = getarg(args, 'G')) != NULL && arg->val) { 15620253Sjoerg int i = 0; 15720253Sjoerg 15820253Sjoerg for (p = strtok(arg->val, ", \t"); i < _UC_MAXGROUPS && p != NULL; p = strtok(NULL, ", \t")) { 15920253Sjoerg if ((grp = getgrnam(p)) == NULL) { 16020253Sjoerg if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL) 16120267Sjoerg cmderr(EX_NOUSER, "group `%s' does not exist\n", p); 16220253Sjoerg } 16320253Sjoerg cnf->groups[i++] = newstr(grp->gr_name); 16420253Sjoerg } 16520253Sjoerg while (i < _UC_MAXGROUPS) 16620253Sjoerg cnf->groups[i++] = NULL; 16720253Sjoerg } 16820253Sjoerg if ((arg = getarg(args, 'k')) != NULL) { 16920253Sjoerg if (stat(cnf->dotdir = arg->val, &st) == -1 || S_ISDIR(st.st_mode)) 17020267Sjoerg cmderr(EX_OSFILE, "skeleton `%s' is not a directory or does not exist\n", cnf->dotdir); 17120253Sjoerg } 17220253Sjoerg if ((arg = getarg(args, 's')) != NULL) 17320253Sjoerg cnf->shell_default = arg->val; 17420253Sjoerg 17520253Sjoerg if (mode == M_ADD && getarg(args, 'D')) { 17620253Sjoerg if (getarg(args, 'n') != NULL) 17720267Sjoerg cmderr(EX_DATAERR, "can't combine `-D' with `-n name'\n"); 17820253Sjoerg if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 17920253Sjoerg if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 18020253Sjoerg cnf->min_uid = 1000; 18120253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 18220253Sjoerg cnf->max_uid = 32000; 18320253Sjoerg } 18420253Sjoerg if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 18520253Sjoerg if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 18620253Sjoerg cnf->min_gid = 1000; 18720253Sjoerg if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 18820253Sjoerg cnf->max_gid = 32000; 18920253Sjoerg } 19020253Sjoerg if ((arg = getarg(args, 'w')) != NULL) 19120253Sjoerg cnf->default_password = boolean_val(arg->val, cnf->default_password); 19220253Sjoerg 19320253Sjoerg arg = getarg(args, 'C'); 19420253Sjoerg if (write_userconfig(arg ? arg->val : NULL)) 19520267Sjoerg return EXIT_SUCCESS; 19620253Sjoerg perror("config update"); 19720267Sjoerg return EX_IOERR; 19820253Sjoerg } 19920253Sjoerg if (mode == M_PRINT && getarg(args, 'a')) { 20020267Sjoerg int pretty = getarg(args, 'P') != NULL; 20120253Sjoerg 20220253Sjoerg setpwent(); 20320253Sjoerg while ((pwd = getpwent()) != NULL) 20420253Sjoerg print_user(pwd, pretty); 20520253Sjoerg endpwent(); 20620267Sjoerg return EXIT_SUCCESS; 20720253Sjoerg } 20820253Sjoerg if ((a_name = getarg(args, 'n')) != NULL) 20920253Sjoerg pwd = getpwnam(pw_checkname(a_name->val, 0)); 21020253Sjoerg a_uid = getarg(args, 'u'); 21120253Sjoerg 21220253Sjoerg if (a_uid == NULL) { 21320253Sjoerg if (a_name == NULL) 21420267Sjoerg cmderr(EX_DATAERR, "user name or id required\n"); 21520253Sjoerg 21620253Sjoerg /* 21720253Sjoerg * Determine whether 'n' switch is name or uid - we don't 21820253Sjoerg * really don't really care which we have, but we need to 21920253Sjoerg * know. 22020253Sjoerg */ 22120253Sjoerg if (mode != M_ADD && pwd == NULL && isdigit(*a_name->val) && atoi(a_name->val) > 0) { /* Assume uid */ 22220253Sjoerg (a_uid = a_name)->ch = 'u'; 22320253Sjoerg a_name = NULL; 22420253Sjoerg } 22520253Sjoerg } 22620253Sjoerg /* 22720253Sjoerg * Update, delete & print require that the user exists 22820253Sjoerg */ 22920253Sjoerg if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) { 23020253Sjoerg if (a_name == NULL && pwd == NULL) /* Try harder */ 23120253Sjoerg pwd = getpwuid(atoi(a_uid->val)); 23220253Sjoerg 23320253Sjoerg if (pwd == NULL) { 23420253Sjoerg if (mode == M_PRINT && getarg(args, 'F')) { 23520253Sjoerg fakeuser.pw_name = a_name ? a_name->val : "nouser"; 23620253Sjoerg fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 23720267Sjoerg return print_user(&fakeuser, getarg(args, 'P') != NULL); 23820253Sjoerg } 23920253Sjoerg if (a_name == NULL) 24020267Sjoerg cmderr(EX_NOUSER, "no such uid `%s'\n", a_uid->val); 24120267Sjoerg cmderr(EX_NOUSER, "no such user `%s'\n", a_name->val); 24220253Sjoerg } 24320253Sjoerg if (a_name == NULL) /* May be needed later */ 24420253Sjoerg a_name = addarg(args, 'n', newstr(pwd->pw_name)); 24520253Sjoerg 24620253Sjoerg /* 24720253Sjoerg * Handle deletions now 24820253Sjoerg */ 24920253Sjoerg if (mode == M_DELETE) { 25020253Sjoerg char file[MAXPATHLEN]; 25120253Sjoerg char home[MAXPATHLEN]; 25220253Sjoerg uid_t uid = pwd->pw_uid; 25320253Sjoerg 25420253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 25520267Sjoerg cmderr(EX_DATAERR, "cannot remove user 'root'\n"); 25620253Sjoerg 25720253Sjoerg /* 25820253Sjoerg * Remove crontabs 25920253Sjoerg */ 26020253Sjoerg sprintf(file, "/var/cron/tabs/%s", pwd->pw_name); 26120253Sjoerg if (access(file, F_OK) == 0) { 26220253Sjoerg sprintf(file, "crontab -u %s -r", pwd->pw_name); 26320253Sjoerg system(file); 26420253Sjoerg } 26520253Sjoerg /* 26620253Sjoerg * Save these for later, since contents of pwd may be 26720253Sjoerg * invalidated by deletion 26820253Sjoerg */ 26920253Sjoerg sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 27020253Sjoerg strncpy(home, pwd->pw_dir, sizeof home); 27120253Sjoerg home[sizeof home - 1] = '\0'; 27220253Sjoerg 27320253Sjoerg if (!delpwent(pwd)) 27420267Sjoerg cmderr(EX_IOERR, "Error updating passwd file: %s\n", strerror(errno)); 27520253Sjoerg editgroups(a_name->val, NULL); 27620253Sjoerg 27720253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 27820253Sjoerg 27920253Sjoerg /* 28020253Sjoerg * Remove mail file 28120253Sjoerg */ 28220253Sjoerg remove(file); 28320253Sjoerg 28420253Sjoerg /* 28520253Sjoerg * Remove at jobs 28620253Sjoerg */ 28720253Sjoerg if (getpwuid(uid) == NULL) 28820253Sjoerg rmat(uid); 28920253Sjoerg 29020253Sjoerg /* 29120253Sjoerg * Remove home directory and contents 29220253Sjoerg */ 29320253Sjoerg if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 29420253Sjoerg if (stat(home, &st) != -1) { 29520253Sjoerg rm_r(home, uid); 29620253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 29720253Sjoerg a_name->val, (long) uid, home, 29820253Sjoerg stat(home, &st) == -1 ? "" : "not completely "); 29920253Sjoerg } 30020253Sjoerg } 30120267Sjoerg return EXIT_SUCCESS; 30220253Sjoerg } else if (mode == M_PRINT) 30320267Sjoerg return print_user(pwd, getarg(args, 'P') != NULL); 30420253Sjoerg 30520253Sjoerg /* 30620253Sjoerg * The rest is edit code 30720253Sjoerg */ 30820253Sjoerg if ((arg = getarg(args, 'l')) != NULL) { 30920253Sjoerg if (strcmp(pwd->pw_name, "root") == 0) 31020267Sjoerg cmderr(EX_DATAERR, "can't rename `root' account\n"); 31120253Sjoerg pwd->pw_name = pw_checkname(arg->val, 0); 31220253Sjoerg } 31320253Sjoerg if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) { 31420253Sjoerg pwd->pw_uid = (uid_t) atol(arg->val); 31520253Sjoerg if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 31620267Sjoerg cmderr(EX_DATAERR, "can't change uid of `root' account\n"); 31720253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 31820253Sjoerg fprintf(stderr, "WARNING: account `%s' will have a uid of 0 (superuser access!)\n", pwd->pw_name); 31920253Sjoerg } 32020253Sjoerg if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) /* Already checked this */ 32120253Sjoerg pwd->pw_gid = (gid_t) getgrnam(cnf->default_group)->gr_gid; 32220253Sjoerg 32320253Sjoerg if ((arg = getarg(args, 'p')) != NULL) { 32420253Sjoerg if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) 32520253Sjoerg pwd->pw_change = 0; 32620253Sjoerg else { 32720253Sjoerg time_t now = time(NULL); 32820253Sjoerg time_t expire = parse_date(now, arg->val); 32920253Sjoerg 33020253Sjoerg if (now == expire) 33120267Sjoerg cmderr(EX_DATAERR, "Invalid password change date `%s'\n", arg->val); 33220253Sjoerg pwd->pw_change = expire; 33320253Sjoerg } 33420253Sjoerg } 33520267Sjoerg if ((arg = getarg(args, 'e')) != NULL) { 33620253Sjoerg if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) 33720253Sjoerg pwd->pw_expire = 0; 33820253Sjoerg else { 33920253Sjoerg time_t now = time(NULL); 34020253Sjoerg time_t expire = parse_date(now, arg->val); 34120253Sjoerg 34220253Sjoerg if (now == expire) 34320267Sjoerg cmderr(EX_DATAERR, "Invalid account expiry date `%s'\n", arg->val); 34420253Sjoerg pwd->pw_expire = expire; 34520253Sjoerg } 34620253Sjoerg } 34720253Sjoerg if ((arg = getarg(args, 's')) != NULL) 34820253Sjoerg pwd->pw_shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 34920253Sjoerg 35020253Sjoerg if (getarg(args, 'L')) 35120253Sjoerg pwd->pw_class = cnf->default_class; 35220253Sjoerg 35320267Sjoerg if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL) 35420267Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 35520267Sjoerg 35620253Sjoerg } else { 35720253Sjoerg if (a_name == NULL) /* Required */ 35820267Sjoerg cmderr(EX_DATAERR, "login name required\n"); 35920253Sjoerg else if ((pwd = getpwnam(a_name->val)) != NULL) /* Exists */ 36020267Sjoerg cmderr(EX_DATAERR, "login name `%s' already exists\n", a_name->val); 36120253Sjoerg 36220253Sjoerg /* 36320253Sjoerg * Now, set up defaults for a new user 36420253Sjoerg */ 36520253Sjoerg pwd = &fakeuser; 36620253Sjoerg pwd->pw_name = a_name->val; 36720253Sjoerg pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 36820253Sjoerg pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 36920253Sjoerg pwd->pw_uid = pw_uidpolicy(cnf, args); 37020253Sjoerg pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 37120253Sjoerg pwd->pw_change = pw_pwdpolicy(cnf, args); 37220253Sjoerg pwd->pw_expire = pw_exppolicy(cnf, args); 37320253Sjoerg pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 37420253Sjoerg pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 37520253Sjoerg 37620253Sjoerg if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 37720253Sjoerg fprintf(stderr, "WARNING: new account `%s' has a uid of 0 (superuser access!)\n", pwd->pw_name); 37820253Sjoerg } 37920253Sjoerg 38020253Sjoerg /* 38120253Sjoerg * Shared add/edit code 38220253Sjoerg */ 38320253Sjoerg if ((arg = getarg(args, 'c')) != NULL) 38420253Sjoerg pwd->pw_gecos = pw_checkname(arg->val, 1); 38520253Sjoerg 38620253Sjoerg if ((arg = getarg(args, 'h')) != NULL) { 38720253Sjoerg if (strcmp(arg->val, "-") == 0) 38820253Sjoerg pwd->pw_passwd = "*"; /* No access */ 38920253Sjoerg else { 39020253Sjoerg int fd = atoi(arg->val); 39120253Sjoerg int b; 39220253Sjoerg int istty = isatty(fd); 39320253Sjoerg struct termios t; 39420253Sjoerg 39520253Sjoerg if (istty) { 39620253Sjoerg if (tcgetattr(fd, &t) == -1) 39720253Sjoerg istty = 0; 39820253Sjoerg else { 39920253Sjoerg struct termios n = t; 40020253Sjoerg 40120253Sjoerg /* Disable echo */ 40220253Sjoerg n.c_lflag &= ~(ECHO); 40320253Sjoerg tcsetattr(fd, TCSANOW, &n); 40420253Sjoerg printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name); 40520253Sjoerg fflush(stdout); 40620253Sjoerg } 40720253Sjoerg } 40820253Sjoerg b = read(fd, line, sizeof(line) - 1); 40920253Sjoerg if (istty) { /* Restore state */ 41020253Sjoerg tcsetattr(fd, TCSANOW, &t); 41120253Sjoerg fputc('\n', stdout); 41220253Sjoerg fflush(stdout); 41320253Sjoerg } 41420253Sjoerg if (b < 0) { 41520253Sjoerg perror("-h file descriptor"); 41620267Sjoerg return EX_IOERR; 41720253Sjoerg } 41820253Sjoerg line[b] = '\0'; 41920253Sjoerg if ((p = strpbrk(line, " \t\r\n")) != NULL) 42020253Sjoerg *p = '\0'; 42120253Sjoerg if (!*line) 42220267Sjoerg cmderr(EX_DATAERR, "empty password read on file descriptor %d\n", fd); 42320253Sjoerg pwd->pw_passwd = pw_pwcrypt(line); 42420253Sjoerg } 42520253Sjoerg } 42620267Sjoerg 42720267Sjoerg /* 42820267Sjoerg * Special case: -N only displays & exits 42920267Sjoerg */ 43020267Sjoerg if (getarg(args, 'N') != NULL) 43120267Sjoerg return print_user(pwd, getarg(args, 'P') != NULL); 43220267Sjoerg 43320253Sjoerg if ((mode == M_ADD && !addpwent(pwd)) || 43420253Sjoerg (mode == M_UPDATE && !chgpwent(a_name->val, pwd))) { 43520253Sjoerg perror("password update"); 43620267Sjoerg return EX_IOERR; 43720253Sjoerg } 43820253Sjoerg /* 43920253Sjoerg * Ok, user is created or changed - now edit group file 44020253Sjoerg */ 44120253Sjoerg 44220253Sjoerg if (mode == M_ADD || getarg(args, 'G') != NULL) 44320253Sjoerg editgroups(pwd->pw_name, cnf->groups); 44420253Sjoerg 44520253Sjoerg /* pwd may have been invalidated */ 44620253Sjoerg if ((pwd = getpwnam(a_name->val)) == NULL) 44720267Sjoerg cmderr(EX_NOUSER, "user '%s' disappeared during update\n", a_name->val); 44820253Sjoerg 44920253Sjoerg grp = getgrgid(pwd->pw_gid); 45020253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s", 45120253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 45220253Sjoerg grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 45320253Sjoerg pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 45420253Sjoerg 45520253Sjoerg /* 45620253Sjoerg * If adding, let's touch and chown the user's mail file. This is not 45720253Sjoerg * strictly necessary under BSD with a 0755 maildir but it also 45820253Sjoerg * doesn't hurt anything to create the empty mailfile 45920253Sjoerg */ 46020253Sjoerg if (mode == M_ADD) { 46120253Sjoerg FILE *fp; 46220253Sjoerg 46320253Sjoerg sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 46420253Sjoerg close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 46520253Sjoerg * mtime */ 46620253Sjoerg chown(line, pwd->pw_uid, pwd->pw_gid); 46720253Sjoerg 46820253Sjoerg /* 46920253Sjoerg * Send mail to the new user as well, if we are asked to 47020253Sjoerg */ 47120253Sjoerg if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 47220253Sjoerg FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 47320253Sjoerg 47420253Sjoerg if (pfp == NULL) 47520253Sjoerg perror("sendmail"); 47620253Sjoerg else { 47720253Sjoerg fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 47820253Sjoerg while (fgets(line, sizeof(line), fp) != NULL) { 47920253Sjoerg /* Do substitutions? */ 48020253Sjoerg fputs(line, pfp); 48120253Sjoerg } 48220253Sjoerg pclose(pfp); 48320253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 48420253Sjoerg pwd->pw_name, (long) pwd->pw_uid); 48520253Sjoerg } 48620253Sjoerg fclose(fp); 48720253Sjoerg } 48820253Sjoerg } 48920253Sjoerg /* 49020253Sjoerg * Finally, let's create and populate the user's home directory. Note 49120253Sjoerg * that this also `works' for editing users if -m is used, but 49220253Sjoerg * existing files will *not* be overwritten. 49320253Sjoerg */ 49420253Sjoerg if (getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 49520253Sjoerg copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid); 49620253Sjoerg pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 49720253Sjoerg pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 49820253Sjoerg } 49920267Sjoerg return EXIT_SUCCESS; 50020253Sjoerg} 50120253Sjoerg 50220253Sjoerg 50320253Sjoergstatic uid_t 50420253Sjoergpw_uidpolicy(struct userconf * cnf, struct cargs * args) 50520253Sjoerg{ 50620253Sjoerg struct passwd *pwd; 50720253Sjoerg uid_t uid = (uid_t) - 1; 50820253Sjoerg struct carg *a_uid = getarg(args, 'u'); 50920253Sjoerg 51020253Sjoerg /* 51120253Sjoerg * Check the given uid, if any 51220253Sjoerg */ 51320253Sjoerg if (a_uid != NULL) { 51420253Sjoerg uid = (uid_t) atol(a_uid->val); 51520253Sjoerg 51620253Sjoerg if ((pwd = getpwuid(uid)) != NULL && getarg(args, 'o') == NULL) 51720267Sjoerg cmderr(EX_DATAERR, "uid `%ld' has already been allocated\n", (long) pwd->pw_uid); 51820253Sjoerg } else { 51920253Sjoerg struct bitmap bm; 52020253Sjoerg 52120253Sjoerg /* 52220253Sjoerg * We need to allocate the next available uid under one of 52320253Sjoerg * two policies a) Grab the first unused uid b) Grab the 52420253Sjoerg * highest possible unused uid 52520253Sjoerg */ 52620253Sjoerg if (cnf->min_uid >= cnf->max_uid) { /* Sanity 52720253Sjoerg * claus^H^H^H^Hheck */ 52820253Sjoerg cnf->min_uid = 1000; 52920253Sjoerg cnf->max_uid = 32000; 53020253Sjoerg } 53120253Sjoerg bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 53220253Sjoerg 53320253Sjoerg /* 53420253Sjoerg * Now, let's fill the bitmap from the password file 53520253Sjoerg */ 53620253Sjoerg setpwent(); 53720253Sjoerg while ((pwd = getpwent()) != NULL) 53820253Sjoerg if (pwd->pw_uid >= (int) cnf->min_uid && pwd->pw_uid <= (int) cnf->max_uid) 53920253Sjoerg bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 54020253Sjoerg endpwent(); 54120253Sjoerg 54220253Sjoerg /* 54320253Sjoerg * Then apply the policy, with fallback to reuse if necessary 54420253Sjoerg */ 54520253Sjoerg if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 54620253Sjoerg uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 54720253Sjoerg 54820253Sjoerg /* 54920253Sjoerg * Another sanity check 55020253Sjoerg */ 55120253Sjoerg if (uid < cnf->min_uid || uid > cnf->max_uid) 55220267Sjoerg cmderr(EX_SOFTWARE, "unable to allocate a new uid - range fully used\n"); 55320253Sjoerg bm_dealloc(&bm); 55420253Sjoerg } 55520253Sjoerg return uid; 55620253Sjoerg} 55720253Sjoerg 55820253Sjoerg 55920253Sjoergstatic uid_t 56020253Sjoergpw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 56120253Sjoerg{ 56220253Sjoerg struct group *grp; 56320253Sjoerg gid_t gid = (uid_t) - 1; 56420253Sjoerg struct carg *a_gid = getarg(args, 'g'); 56520253Sjoerg 56620253Sjoerg /* 56720253Sjoerg * If no arg given, see if default can help out 56820253Sjoerg */ 56920253Sjoerg if (a_gid == NULL && cnf->default_group && *cnf->default_group) 57020253Sjoerg a_gid = addarg(args, 'g', cnf->default_group); 57120253Sjoerg 57220253Sjoerg /* 57320253Sjoerg * Check the given gid, if any 57420253Sjoerg */ 57520267Sjoerg setgrent(); 57620253Sjoerg if (a_gid != NULL) { 57720253Sjoerg if ((grp = getgrnam(a_gid->val)) == NULL) { 57820253Sjoerg gid = (gid_t) atol(a_gid->val); 57920253Sjoerg if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = getgrgid(gid)) == NULL) 58020267Sjoerg cmderr(EX_NOUSER, "group `%s' is not defined\n", a_gid->val); 58120253Sjoerg } 58220253Sjoerg gid = grp->gr_gid; 58320267Sjoerg } else if ((grp = getgrnam(nam)) != NULL && grp->gr_mem[0] == NULL) { 58420267Sjoerg gid = grp->gr_gid; /* Already created? Use it anyway... */ 58520253Sjoerg } else { 58620253Sjoerg struct cargs grpargs; 58720253Sjoerg char tmp[32]; 58820253Sjoerg 58920253Sjoerg LIST_INIT(&grpargs); 59020253Sjoerg addarg(&grpargs, 'n', nam); 59120253Sjoerg 59220253Sjoerg /* 59320253Sjoerg * We need to auto-create a group with the user's name. We 59420253Sjoerg * can send all the appropriate output to our sister routine 59520253Sjoerg * bit first see if we can create a group with gid==uid so we 59620253Sjoerg * can keep the user and group ids in sync. We purposely do 59720253Sjoerg * NOT check the gid range if we can force the sync. If the 59820253Sjoerg * user's name dups an existing group, then the group add 59920253Sjoerg * function will happily handle that case for us and exit. 60020253Sjoerg */ 60120253Sjoerg if (getgrgid(prefer) == NULL) { 60220253Sjoerg sprintf(tmp, "%lu", (unsigned long) prefer); 60320253Sjoerg addarg(&grpargs, 'g', tmp); 60420253Sjoerg } 60520267Sjoerg if (getarg(args, 'N')) 60620267Sjoerg { 60720267Sjoerg addarg(&grpargs, 'N', NULL); 60820267Sjoerg addarg(&grpargs, 'q', NULL); 60920267Sjoerg gid = pw_group(cnf, M_NEXT, &grpargs); 61020267Sjoerg } 61120267Sjoerg else 61220267Sjoerg { 61320267Sjoerg pw_group(cnf, M_ADD, &grpargs); 61420267Sjoerg if ((grp = getgrnam(nam)) != NULL) 61520267Sjoerg gid = grp->gr_gid; 61620267Sjoerg } 61720253Sjoerg a_gid = grpargs.lh_first; 61820253Sjoerg while (a_gid != NULL) { 61920253Sjoerg struct carg *t = a_gid->list.le_next; 62020253Sjoerg LIST_REMOVE(a_gid, list); 62120253Sjoerg a_gid = t; 62220253Sjoerg } 62320253Sjoerg } 62420267Sjoerg endgrent(); 62520253Sjoerg return gid; 62620253Sjoerg} 62720253Sjoerg 62820253Sjoerg 62920253Sjoergstatic time_t 63020253Sjoergpw_pwdpolicy(struct userconf * cnf, struct cargs * args) 63120253Sjoerg{ 63220253Sjoerg time_t result = 0; 63320253Sjoerg time_t now = time(NULL); 63420253Sjoerg struct carg *arg = getarg(args, 'e'); 63520253Sjoerg 63620253Sjoerg if (arg != NULL) { 63720253Sjoerg if ((result = parse_date(now, arg->val)) == now) 63820267Sjoerg cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val); 63920253Sjoerg } else if (cnf->password_days > 0) 64020253Sjoerg result = now + ((long) cnf->password_days * 86400L); 64120253Sjoerg return result; 64220253Sjoerg} 64320253Sjoerg 64420253Sjoerg 64520253Sjoergstatic time_t 64620253Sjoergpw_exppolicy(struct userconf * cnf, struct cargs * args) 64720253Sjoerg{ 64820253Sjoerg time_t result = 0; 64920253Sjoerg time_t now = time(NULL); 65020253Sjoerg struct carg *arg = getarg(args, 'e'); 65120253Sjoerg 65220253Sjoerg if (arg != NULL) { 65320253Sjoerg if ((result = parse_date(now, arg->val)) == now) 65420267Sjoerg cmderr(EX_DATAERR, "invalid date/time `%s'\n", arg->val); 65520253Sjoerg } else if (cnf->expire_days > 0) 65620253Sjoerg result = now + ((long) cnf->expire_days * 86400L); 65720253Sjoerg return result; 65820253Sjoerg} 65920253Sjoerg 66020253Sjoerg 66120253Sjoergstatic char * 66220253Sjoergpw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 66320253Sjoerg{ 66420253Sjoerg struct carg *arg = getarg(args, 'd'); 66520253Sjoerg 66620253Sjoerg if (arg) 66720253Sjoerg return arg->val; 66820253Sjoerg else { 66920253Sjoerg static char home[128]; 67020253Sjoerg 67120253Sjoerg if (cnf->home == NULL || *cnf->home == '\0') 67220267Sjoerg cmderr(EX_CONFIG, "no base home directory set\n"); 67320253Sjoerg sprintf(home, "%s/%s", cnf->home, user); 67420253Sjoerg return home; 67520253Sjoerg } 67620253Sjoerg} 67720253Sjoerg 67820253Sjoergstatic char * 67920253Sjoergshell_path(char const * path, char *shells[], char *sh) 68020253Sjoerg{ 68120253Sjoerg if (sh != NULL && (*sh == '/' || *sh == '\0')) 68220253Sjoerg return sh; /* specified full path or forced none */ 68320253Sjoerg else { 68420253Sjoerg char *p; 68520253Sjoerg char paths[_UC_MAXLINE]; 68620253Sjoerg 68720253Sjoerg /* 68820253Sjoerg * We need to search paths 68920253Sjoerg */ 69020253Sjoerg strncpy(paths, path, sizeof paths); 69120253Sjoerg paths[sizeof paths - 1] = '\0'; 69220253Sjoerg for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 69320253Sjoerg int i; 69420253Sjoerg static char shellpath[256]; 69520253Sjoerg 69620253Sjoerg if (sh != NULL) { 69720253Sjoerg sprintf(shellpath, "%s/%s", p, sh); 69820253Sjoerg if (access(shellpath, X_OK) == 0) 69920253Sjoerg return shellpath; 70020253Sjoerg } else 70120253Sjoerg for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 70220253Sjoerg sprintf(shellpath, "%s/%s", p, shells[i]); 70320253Sjoerg if (access(shellpath, X_OK) == 0) 70420253Sjoerg return shellpath; 70520253Sjoerg } 70620253Sjoerg } 70720253Sjoerg if (sh == NULL) 70820267Sjoerg cmderr(EX_OSFILE, "can't find shell `%s' in shell paths\n", sh); 70920267Sjoerg cmderr(EX_CONFIG, "no default shell available or defined\n"); 71020253Sjoerg return NULL; 71120253Sjoerg } 71220253Sjoerg} 71320253Sjoerg 71420253Sjoerg 71520253Sjoergstatic char * 71620253Sjoergpw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 71720253Sjoerg{ 71820253Sjoerg char *sh = newshell; 71920253Sjoerg struct carg *arg = getarg(args, 's'); 72020253Sjoerg 72120253Sjoerg if (newshell == NULL && arg != NULL) 72220253Sjoerg sh = arg->val; 72320253Sjoerg return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 72420253Sjoerg} 72520253Sjoerg 72620253Sjoergstatic char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."; 72720253Sjoerg 72820253Sjoergchar * 72920253Sjoergpw_pwcrypt(char *password) 73020253Sjoerg{ 73120253Sjoerg int i; 73220253Sjoerg char salt[12]; 73320253Sjoerg 73420253Sjoerg static char buf[256]; 73520253Sjoerg 73620253Sjoerg /* 73720253Sjoerg * Calculate a salt value 73820253Sjoerg */ 73920253Sjoerg srandom((unsigned) (time(NULL) | getpid())); 74020253Sjoerg for (i = 0; i < 8; i++) 74120253Sjoerg salt[i] = chars[random() % 63]; 74220253Sjoerg salt[i] = '\0'; 74320253Sjoerg 74420253Sjoerg return strcpy(buf, crypt(password, salt)); 74520253Sjoerg} 74620253Sjoerg 74720253Sjoerg 74820253Sjoergstatic char * 74920253Sjoergpw_password(struct userconf * cnf, struct cargs * args, char const * user) 75020253Sjoerg{ 75120253Sjoerg int i, l; 75220253Sjoerg char pwbuf[32]; 75320253Sjoerg 75420253Sjoerg switch (cnf->default_password) { 75520253Sjoerg case -1: /* Random password */ 75620253Sjoerg srandom((unsigned) (time(NULL) | getpid())); 75720253Sjoerg l = (random() % 8 + 8); /* 8 - 16 chars */ 75820253Sjoerg for (i = 0; i < l; i++) 75920253Sjoerg pwbuf[i] = chars[random() % sizeof(chars)]; 76020253Sjoerg pwbuf[i] = '\0'; 76120253Sjoerg 76220253Sjoerg /* 76320253Sjoerg * We give this information back to the user 76420253Sjoerg */ 76520267Sjoerg if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) { 76620253Sjoerg if (isatty(0)) 76720253Sjoerg printf("Password is: "); 76820253Sjoerg printf("%s\n", pwbuf); 76920253Sjoerg fflush(stdout); 77020253Sjoerg } 77120253Sjoerg break; 77220253Sjoerg 77320253Sjoerg case -2: /* No password at all! */ 77420253Sjoerg return ""; 77520253Sjoerg 77620253Sjoerg case 0: /* No login - default */ 77720253Sjoerg default: 77820253Sjoerg return "*"; 77920253Sjoerg 78020253Sjoerg case 1: /* user's name */ 78120253Sjoerg strncpy(pwbuf, user, sizeof pwbuf); 78220253Sjoerg pwbuf[sizeof pwbuf - 1] = '\0'; 78320253Sjoerg break; 78420253Sjoerg } 78520253Sjoerg return pw_pwcrypt(pwbuf); 78620253Sjoerg} 78720253Sjoerg 78820253Sjoerg 78920253Sjoergstatic int 79020253Sjoergprint_user(struct passwd * pwd, int pretty) 79120253Sjoerg{ 79220253Sjoerg if (!pretty) { 79320253Sjoerg char buf[_UC_MAXLINE]; 79420253Sjoerg 79520253Sjoerg fmtpwent(buf, pwd); 79620253Sjoerg fputs(buf, stdout); 79720253Sjoerg } else { 79820267Sjoerg int j; 79920253Sjoerg char *p; 80020253Sjoerg struct group *grp = getgrgid(pwd->pw_gid); 80120253Sjoerg char uname[60] = "User &", office[60] = "[None]", 80220253Sjoerg wphone[60] = "[None]", hphone[60] = "[None]"; 80320253Sjoerg 80420253Sjoerg if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 80520253Sjoerg strncpy(uname, p, sizeof uname); 80620253Sjoerg uname[sizeof uname - 1] = '\0'; 80720253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 80820253Sjoerg strncpy(office, p, sizeof office); 80920253Sjoerg office[sizeof office - 1] = '\0'; 81020253Sjoerg if ((p = strtok(NULL, ",")) != NULL) { 81120253Sjoerg strncpy(wphone, p, sizeof wphone); 81220253Sjoerg wphone[sizeof wphone - 1] = '\0'; 81320253Sjoerg if ((p = strtok(NULL, "")) != NULL) { 81420253Sjoerg strncpy(hphone, p, sizeof hphone); 81520253Sjoerg hphone[sizeof hphone - 1] = '\0'; 81620253Sjoerg } 81720253Sjoerg } 81820253Sjoerg } 81920253Sjoerg } 82020253Sjoerg /* 82120253Sjoerg * Handle '&' in gecos field 82220253Sjoerg */ 82320253Sjoerg if ((p = strchr(uname, '&')) != NULL) { 82420253Sjoerg int l = strlen(pwd->pw_name); 82520253Sjoerg int m = strlen(p); 82620253Sjoerg 82720253Sjoerg memmove(p + l, p + 1, m); 82820253Sjoerg memmove(p, pwd->pw_name, l); 82920253Sjoerg *p = (char) toupper(*p); 83020253Sjoerg } 83120253Sjoerg printf("Login Name : %-10s #%-22ld Group : %-10s #%ld\n" 83220253Sjoerg " Full Name : %s\n" 83320253Sjoerg " Home : %-32.32s Class : %s\n" 83420253Sjoerg " Shell : %-32.32s Office : %s\n" 83520267Sjoerg "Work Phone : %-32.32s Home Phone : %s\n", 83620267Sjoerg 83720253Sjoerg pwd->pw_name, (long) pwd->pw_uid, 83820253Sjoerg grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 83920253Sjoerg uname, pwd->pw_dir, pwd->pw_class, 84020253Sjoerg pwd->pw_shell, office, wphone, hphone); 84120267Sjoerg setgrent(); 84220267Sjoerg j = 0; 84320267Sjoerg while ((grp=getgrent()) != NULL) 84420267Sjoerg { 84520267Sjoerg int i = 0; 84620267Sjoerg while (i < _UC_MAXGROUPS && grp->gr_mem[i] != NULL) 84720267Sjoerg { 84820267Sjoerg if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 84920267Sjoerg { 85020267Sjoerg printf(j++ == 0 ? " Groups : %s" : ",%s", grp->gr_name); 85120267Sjoerg break; 85220267Sjoerg } 85320267Sjoerg ++i; 85420267Sjoerg } 85520267Sjoerg } 85620267Sjoerg endgrent(); 85720267Sjoerg printf("%s\n", j ? "\n" : ""); 85820253Sjoerg } 85920267Sjoerg return EXIT_SUCCESS; 86020253Sjoerg} 86120253Sjoerg 86220253Sjoergstatic char * 86320253Sjoergpw_checkname(char *name, int gecos) 86420253Sjoerg{ 86520253Sjoerg int l = 0; 86620253Sjoerg char const *notch = gecos ? ":" : " :+-&#%$^()!@~*?<>=|\\/\""; 86720253Sjoerg 86820253Sjoerg while (name[l]) { 86920253Sjoerg if (strchr(notch, name[l]) != NULL || name[l] < ' ') 87020267Sjoerg cmderr(EX_DATAERR, "invalid character `%c' in field\n", name[l]); 87120253Sjoerg ++l; 87220253Sjoerg } 87320253Sjoerg if (!gecos && l > 8) 87420267Sjoerg cmderr(EX_DATAERR, "name too long `%s'\n", name); 87520253Sjoerg return name; 87620253Sjoerg} 87720253Sjoerg 87820253Sjoerg 87920253Sjoergstatic void 88020253Sjoergrmat(uid_t uid) 88120253Sjoerg{ 88220253Sjoerg DIR *d = opendir("/var/at/jobs"); 88320253Sjoerg 88420253Sjoerg if (d != NULL) { 88520253Sjoerg struct dirent *e; 88620253Sjoerg 88720253Sjoerg while ((e = readdir(d)) != NULL) { 88820253Sjoerg struct stat st; 88920253Sjoerg 89020253Sjoerg if (strncmp(e->d_name, ".lock", 5) != 0 && 89120253Sjoerg stat(e->d_name, &st) == 0 && 89220253Sjoerg !S_ISDIR(st.st_mode) && 89320253Sjoerg st.st_uid == uid) { 89420253Sjoerg char tmp[MAXPATHLEN]; 89520253Sjoerg 89620253Sjoerg sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 89720253Sjoerg system(tmp); 89820253Sjoerg } 89920253Sjoerg } 90020253Sjoerg closedir(d); 90120253Sjoerg } 90220253Sjoerg} 903