1 /* $NetBSD: atrun.c,v 1.3 1996/08/30 20:24:51 thorpej Exp $ */ 2 3 /* 4 * atrun.c - run jobs queued by at; run with root privileges. 5 * Copyright (c) 1993 by Thomas Koenig 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. The name of the author(s) may not be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* System Headers */ 30 31 #include <sys/fcntl.h> 32 #include <sys/types.h> 33 #include <sys/stat.h> 34 #include <sys/wait.h> 35 #include <dirent.h> 36 #include <errno.h> 37 #include <pwd.h> 38 #include <signal.h> 39 #include <stddef.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <time.h> 44 #include <unistd.h> 45 #include <syslog.h> 46 47 #include <paths.h> 48 49 /* Local headers */ 50 51 #define MAIN 52 #include "privs.h" 53 #include "pathnames.h" 54 #include "atrun.h" 55 56 /* File scope variables */ 57 58 static char *namep; 59 static char rcsid[] = "$NetBSD: atrun.c,v 1.3 1996/08/30 20:24:51 thorpej Exp $"; 60 61 /* Local functions */ 62 static void 63 perr(a) 64 const char *a; 65 { 66 syslog(LOG_ERR, "%s: %m", a); 67 exit(EXIT_FAILURE); 68 } 69 70 static int 71 write_string(fd, a) 72 int fd; 73 const char *a; 74 { 75 return write(fd, a, strlen(a)); 76 } 77 78 static void 79 run_file(filename, uid) 80 const char *filename; 81 uid_t uid; 82 { 83 /* 84 * Run a file by by spawning off a process which redirects I/O, 85 * spawns a subshell, then waits for it to complete and spawns another 86 * process to send mail to the user. 87 */ 88 pid_t pid; 89 int fd_out, fd_in; 90 int queue; 91 char mailbuf[9]; 92 char *mailname = NULL; 93 FILE *stream; 94 int send_mail = 0; 95 struct stat buf; 96 off_t size; 97 struct passwd *pentry; 98 int fflags; 99 100 pid = fork(); 101 if (pid == -1) 102 perr("Cannot fork"); 103 else if (pid > 0) 104 return; 105 106 /* 107 * Let's see who we mail to. Hopefully, we can read it from the 108 * command file; if not, send it to the owner, or, failing that, to 109 * root. 110 */ 111 112 PRIV_START 113 114 stream = fopen(filename, "r"); 115 116 PRIV_END 117 118 pentry = getpwuid(uid); 119 if (pentry == NULL) 120 perr("UID not in password file!"); 121 122 if (stream == NULL) 123 perr("Cannot open input file"); 124 125 if ((fd_in = dup(fileno(stream))) < 0) 126 perr("Error duplicating input file descriptor"); 127 128 if ((fflags = fcntl(fd_in, F_GETFD)) < 0) 129 perr("Error in fcntl"); 130 131 fcntl(fd_in, F_SETFD, fflags & ~FD_CLOEXEC); 132 133 if (fscanf(stream, "#! /bin/sh\n# mail %8s %d", mailbuf, &send_mail) == 2) { 134 mailname = mailbuf; 135 } else { 136 mailname = pentry->pw_name; 137 } 138 fclose(stream); 139 if (chdir(_PATH_ATSPOOL) < 0) 140 perr("Cannot chdir to " _PATH_ATSPOOL); 141 142 /* 143 * Create a file to hold the output of the job we are about to 144 * run. Write the mail header. 145 */ 146 if ((fd_out = open(filename, 147 O_WRONLY | O_CREAT | O_EXCL, S_IWUSR | S_IRUSR)) < 0) 148 perr("Cannot create output file"); 149 150 write_string(fd_out, "Subject: Output from your job "); 151 write_string(fd_out, filename); 152 write_string(fd_out, "\n\n"); 153 fstat(fd_out, &buf); 154 size = buf.st_size; 155 156 close(STDIN_FILENO); 157 close(STDOUT_FILENO); 158 close(STDERR_FILENO); 159 160 pid = fork(); 161 if (pid < 0) 162 perr("Error in fork"); 163 else if (pid == 0) { 164 char *nul = NULL; 165 char **nenvp = &nul; 166 167 /* 168 * Set up things for the child; we want standard input from 169 * the input file, and standard output and error sent to 170 * our output file. 171 */ 172 173 if (lseek(fd_in, (off_t) 0, SEEK_SET) < 0) 174 perr("Error in lseek"); 175 176 if (dup(fd_in) != STDIN_FILENO) 177 perr("Error in I/O redirection"); 178 179 if (dup(fd_out) != STDOUT_FILENO) 180 perr("Error in I/O redirection"); 181 182 if (dup(fd_out) != STDERR_FILENO) 183 perr("Error in I/O redirection"); 184 185 close(fd_in); 186 close(fd_out); 187 if (chdir(_PATH_ATJOBS) < 0) 188 perr("Cannot chdir to " _PATH_ATJOBS); 189 190 queue = *filename; 191 192 PRIV_START 193 194 if (queue > 'b') 195 nice(queue - 'b'); 196 197 if (initgroups(pentry->pw_name, pentry->pw_gid) < 0) 198 perr("Cannot init group list"); 199 200 if (setgid(pentry->pw_gid) < 0) 201 perr("Cannot change primary group"); 202 203 if (setuid(uid) < 0) 204 perr("Cannot set user id"); 205 206 chdir("/"); 207 208 if (execle("/bin/sh", "sh", (char *) NULL, nenvp) != 0) 209 perr("Exec failed"); 210 211 PRIV_END 212 } 213 /* We're the parent. Let's wait. */ 214 close(fd_in); 215 close(fd_out); 216 waitpid(pid, (int *) NULL, 0); 217 218 stat(filename, &buf); 219 if ((buf.st_size != size) || send_mail) { 220 /* Fork off a child for sending mail */ 221 pid = fork(); 222 if (pid < 0) 223 perr("Fork failed"); 224 else if (pid == 0) { 225 if (open(filename, O_RDONLY) != STDIN_FILENO) 226 perr("Cannot reopen output file"); 227 228 execl(_PATH_SENDMAIL, _PATH_SENDMAIL, mailname, 229 (char *) NULL); 230 perr("Exec failed"); 231 } 232 waitpid(pid, (int *) NULL, 0); 233 } 234 unlink(filename); 235 exit(EXIT_SUCCESS); 236 } 237 238 /* Global functions */ 239 240 int 241 main(argc, argv) 242 int argc; 243 char *argv[]; 244 { 245 /* 246 * Browse through _PATH_ATJOBS, checking all the jobfiles wether 247 * they should be executed and or deleted. The queue is coded into 248 * the first byte of the job filename, the date (in minutes since 249 * Eon) as a hex number in the following eight bytes, followed by 250 * a dot and a serial number. A file which has not been executed 251 * yet is denoted by its execute - bit set. For those files which 252 * are to be executed, run_file() is called, which forks off a 253 * child which takes care of I/O redirection, forks off another 254 * child for execution and yet another one, optionally, for sending 255 * mail. Files which already have run are removed during the 256 * next invocation. 257 */ 258 DIR *spool; 259 struct dirent *dirent; 260 struct stat buf; 261 int older; 262 unsigned long ctm; 263 char queue; 264 265 /* 266 * We don't need root privileges all the time; running under uid 267 * and gid daemon is fine. 268 */ 269 270 RELINQUISH_PRIVS_ROOT(0) /* it's setuid root */ 271 openlog("atrun", LOG_PID, LOG_CRON); 272 273 namep = argv[0]; 274 if (chdir(_PATH_ATJOBS) != 0) 275 perr("Cannot change to " _PATH_ATJOBS); 276 277 /* 278 * Main loop. Open spool directory for reading and look over all 279 * the files in there. If the filename indicates that the job 280 * should be run and the x bit is set, fork off a child which sets 281 * its user and group id to that of the files and exec a /bin/sh 282 * which executes the shell script. Unlink older files if they 283 * should no longer be run. For deletion, their r bit has to be 284 * turned on. 285 */ 286 if ((spool = opendir(".")) == NULL) 287 perr("Cannot read " _PATH_ATJOBS); 288 289 while ((dirent = readdir(spool)) != NULL) { 290 double la; 291 292 if (stat(dirent->d_name, &buf) != 0) 293 perr("Cannot stat in " _PATH_ATJOBS); 294 295 /* We don't want directories */ 296 if (!S_ISREG(buf.st_mode)) 297 continue; 298 299 if (sscanf(dirent->d_name, "%c%8lx", &queue, &ctm) != 2) 300 continue; 301 302 if ((queue == 'b') && ((getloadavg(&la, 1) != 1) || 303 (la > ATRUN_MAXLOAD))) 304 continue; 305 306 older = (time_t) ctm *60 <= time(NULL); 307 308 /* The file is executable and old enough */ 309 if (older && (S_IXUSR & buf.st_mode)) { 310 /* 311 * Now we know we want to run the file, we can turn 312 * off the execute bit 313 */ 314 315 PRIV_START 316 317 if (chmod(dirent->d_name, S_IRUSR) != 0) 318 perr("Cannot change file permissions"); 319 320 PRIV_END 321 322 run_file(dirent->d_name, buf.st_uid); 323 } 324 /* Delete older files */ 325 if (older && !(S_IXUSR & buf.st_mode) && 326 (S_IRUSR & buf.st_mode)) 327 unlink(dirent->d_name); 328 } 329 closelog(); 330 exit(EXIT_SUCCESS); 331 } 332