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