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