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