atrun.c revision 1.1 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.1 cgd static char rcsid[] = "$Id: atrun.c,v 1.1 1993/12/05 11:36:38 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.1 cgd run_file(filename, uid, gid)
78 1.1 cgd const char *filename;
79 1.1 cgd uid_t uid;
80 1.1 cgd gid_t gid;
81 1.1 cgd {
82 1.1 cgd /*
83 1.1 cgd * Run a file by by spawning off a process which redirects I/O,
84 1.1 cgd * spawns a subshell, then waits for it to complete and spawns another
85 1.1 cgd * process to send mail to the user.
86 1.1 cgd */
87 1.1 cgd pid_t pid;
88 1.1 cgd int fd_out, fd_in;
89 1.1 cgd int queue;
90 1.1 cgd char mailbuf[9];
91 1.1 cgd char *mailname = NULL;
92 1.1 cgd FILE *stream;
93 1.1 cgd int send_mail = 0;
94 1.1 cgd struct stat buf;
95 1.1 cgd off_t size;
96 1.1 cgd struct passwd *pentry;
97 1.1 cgd int fflags;
98 1.1 cgd
99 1.1 cgd pid = fork();
100 1.1 cgd if (pid == -1)
101 1.1 cgd perr("Cannot fork");
102 1.1 cgd else if (pid > 0)
103 1.1 cgd return;
104 1.1 cgd
105 1.1 cgd /*
106 1.1 cgd * Let's see who we mail to. Hopefully, we can read it from the
107 1.1 cgd * command file; if not, send it to the owner, or, failing that, to
108 1.1 cgd * root.
109 1.1 cgd */
110 1.1 cgd
111 1.1 cgd PRIV_START
112 1.1 cgd
113 1.1 cgd stream = fopen(filename, "r");
114 1.1 cgd
115 1.1 cgd PRIV_END
116 1.1 cgd
117 1.1 cgd if (stream == NULL)
118 1.1 cgd perr("Cannot open input file");
119 1.1 cgd
120 1.1 cgd if ((fd_in = dup(fileno(stream))) < 0)
121 1.1 cgd perr("Error duplicating input file descriptor");
122 1.1 cgd
123 1.1 cgd if ((fflags = fcntl(fd_in, F_GETFD)) < 0)
124 1.1 cgd perr("Error in fcntl");
125 1.1 cgd
126 1.1 cgd fcntl(fd_in, F_SETFD, fflags & ~FD_CLOEXEC);
127 1.1 cgd
128 1.1 cgd if (fscanf(stream, "#! /bin/sh\n# mail %8s %d", mailbuf, &send_mail) == 2) {
129 1.1 cgd mailname = mailbuf;
130 1.1 cgd } else {
131 1.1 cgd pentry = getpwuid(uid);
132 1.1 cgd if (pentry == NULL)
133 1.1 cgd mailname = "root";
134 1.1 cgd else
135 1.1 cgd mailname = pentry->pw_name;
136 1.1 cgd }
137 1.1 cgd fclose(stream);
138 1.1 cgd if (chdir(_PATH_ATSPOOL) < 0)
139 1.1 cgd perr("Cannot chdir to " _PATH_ATSPOOL);
140 1.1 cgd
141 1.1 cgd /*
142 1.1 cgd * Create a file to hold the output of the job we are about to
143 1.1 cgd * run. Write the mail header.
144 1.1 cgd */
145 1.1 cgd if ((fd_out = open(filename,
146 1.1 cgd O_WRONLY | O_CREAT | O_EXCL, S_IWUSR | S_IRUSR)) < 0)
147 1.1 cgd perr("Cannot create output file");
148 1.1 cgd
149 1.1 cgd write_string(fd_out, "Subject: Output from your job ");
150 1.1 cgd write_string(fd_out, filename);
151 1.1 cgd write_string(fd_out, "\n\n");
152 1.1 cgd fstat(fd_out, &buf);
153 1.1 cgd size = buf.st_size;
154 1.1 cgd
155 1.1 cgd close(STDIN_FILENO);
156 1.1 cgd close(STDOUT_FILENO);
157 1.1 cgd close(STDERR_FILENO);
158 1.1 cgd
159 1.1 cgd pid = fork();
160 1.1 cgd if (pid < 0)
161 1.1 cgd perr("Error in fork");
162 1.1 cgd else if (pid == 0) {
163 1.1 cgd char *nul = NULL;
164 1.1 cgd char **nenvp = &nul;
165 1.1 cgd
166 1.1 cgd /*
167 1.1 cgd * Set up things for the child; we want standard input from
168 1.1 cgd * the input file, and standard output and error sent to
169 1.1 cgd * our output file.
170 1.1 cgd */
171 1.1 cgd
172 1.1 cgd if (lseek(fd_in, (off_t) 0, SEEK_SET) < 0)
173 1.1 cgd perr("Error in lseek");
174 1.1 cgd
175 1.1 cgd if (dup(fd_in) != STDIN_FILENO)
176 1.1 cgd perr("Error in I/O redirection");
177 1.1 cgd
178 1.1 cgd if (dup(fd_out) != STDOUT_FILENO)
179 1.1 cgd perr("Error in I/O redirection");
180 1.1 cgd
181 1.1 cgd if (dup(fd_out) != STDERR_FILENO)
182 1.1 cgd perr("Error in I/O redirection");
183 1.1 cgd
184 1.1 cgd close(fd_in);
185 1.1 cgd close(fd_out);
186 1.1 cgd if (chdir(_PATH_ATJOBS) < 0)
187 1.1 cgd perr("Cannot chdir to " _PATH_ATJOBS);
188 1.1 cgd
189 1.1 cgd queue = *filename;
190 1.1 cgd
191 1.1 cgd PRIV_START
192 1.1 cgd
193 1.1 cgd if (queue > 'b')
194 1.1 cgd nice(queue - 'b');
195 1.1 cgd
196 1.1 cgd if (setgid(gid) < 0)
197 1.1 cgd perr("Cannot change group");
198 1.1 cgd
199 1.1 cgd if (setuid(uid) < 0)
200 1.1 cgd perr("Cannot set user id");
201 1.1 cgd
202 1.1 cgd chdir("/");
203 1.1 cgd
204 1.1 cgd if (execle("/bin/sh", "sh", (char *) NULL, nenvp) != 0)
205 1.1 cgd perr("Exec failed");
206 1.1 cgd
207 1.1 cgd PRIV_END
208 1.1 cgd }
209 1.1 cgd /* We're the parent. Let's wait. */
210 1.1 cgd close(fd_in);
211 1.1 cgd close(fd_out);
212 1.1 cgd waitpid(pid, (int *) NULL, 0);
213 1.1 cgd
214 1.1 cgd stat(filename, &buf);
215 1.1 cgd if ((buf.st_size != size) || send_mail) {
216 1.1 cgd /* Fork off a child for sending mail */
217 1.1 cgd pid = fork();
218 1.1 cgd if (pid < 0)
219 1.1 cgd perr("Fork failed");
220 1.1 cgd else if (pid == 0) {
221 1.1 cgd if (open(filename, O_RDONLY) != STDIN_FILENO)
222 1.1 cgd perr("Cannot reopen output file");
223 1.1 cgd
224 1.1 cgd execl(_PATH_SENDMAIL, _PATH_SENDMAIL, mailname,
225 1.1 cgd (char *) NULL);
226 1.1 cgd perr("Exec failed");
227 1.1 cgd }
228 1.1 cgd waitpid(pid, (int *) NULL, 0);
229 1.1 cgd }
230 1.1 cgd unlink(filename);
231 1.1 cgd exit(EXIT_SUCCESS);
232 1.1 cgd }
233 1.1 cgd
234 1.1 cgd /* Global functions */
235 1.1 cgd
236 1.1 cgd int
237 1.1 cgd main(argc, argv)
238 1.1 cgd int argc;
239 1.1 cgd char *argv[];
240 1.1 cgd {
241 1.1 cgd /*
242 1.1 cgd * Browse through _PATH_ATJOBS, checking all the jobfiles wether
243 1.1 cgd * they should be executed and or deleted. The queue is coded into
244 1.1 cgd * the first byte of the job filename, the date (in minutes since
245 1.1 cgd * Eon) as a hex number in the following eight bytes, followed by
246 1.1 cgd * a dot and a serial number. A file which has not been executed
247 1.1 cgd * yet is denoted by its execute - bit set. For those files which
248 1.1 cgd * are to be executed, run_file() is called, which forks off a
249 1.1 cgd * child which takes care of I/O redirection, forks off another
250 1.1 cgd * child for execution and yet another one, optionally, for sending
251 1.1 cgd * mail. Files which already have run are removed during the
252 1.1 cgd * next invocation.
253 1.1 cgd */
254 1.1 cgd DIR *spool;
255 1.1 cgd struct dirent *dirent;
256 1.1 cgd struct stat buf;
257 1.1 cgd int older;
258 1.1 cgd unsigned long ctm;
259 1.1 cgd char queue;
260 1.1 cgd
261 1.1 cgd /*
262 1.1 cgd * We don't need root privileges all the time; running under uid
263 1.1 cgd * and gid daemon is fine.
264 1.1 cgd */
265 1.1 cgd
266 1.1 cgd RELINQUISH_PRIVS_ROOT(0) /* it's setuid root */
267 1.1 cgd openlog("atrun", LOG_PID, LOG_CRON);
268 1.1 cgd
269 1.1 cgd namep = argv[0];
270 1.1 cgd if (chdir(_PATH_ATJOBS) != 0)
271 1.1 cgd perr("Cannot change to " _PATH_ATJOBS);
272 1.1 cgd
273 1.1 cgd /*
274 1.1 cgd * Main loop. Open spool directory for reading and look over all
275 1.1 cgd * the files in there. If the filename indicates that the job
276 1.1 cgd * should be run and the x bit is set, fork off a child which sets
277 1.1 cgd * its user and group id to that of the files and exec a /bin/sh
278 1.1 cgd * which executes the shell script. Unlink older files if they
279 1.1 cgd * should no longer be run. For deletion, their r bit has to be
280 1.1 cgd * turned on.
281 1.1 cgd */
282 1.1 cgd if ((spool = opendir(".")) == NULL)
283 1.1 cgd perr("Cannot read " _PATH_ATJOBS);
284 1.1 cgd
285 1.1 cgd while ((dirent = readdir(spool)) != NULL) {
286 1.1 cgd double la;
287 1.1 cgd
288 1.1 cgd if (stat(dirent->d_name, &buf) != 0)
289 1.1 cgd perr("Cannot stat in " _PATH_ATJOBS);
290 1.1 cgd
291 1.1 cgd /* We don't want directories */
292 1.1 cgd if (!S_ISREG(buf.st_mode))
293 1.1 cgd continue;
294 1.1 cgd
295 1.1 cgd if (sscanf(dirent->d_name, "%c%8lx", &queue, &ctm) != 2)
296 1.1 cgd continue;
297 1.1 cgd
298 1.1 cgd if ((queue == 'b') && ((getloadavg(&la, 1) != 1) ||
299 1.1 cgd (la > ATRUN_MAXLOAD)))
300 1.1 cgd continue;
301 1.1 cgd
302 1.1 cgd older = (time_t) ctm *60 <= time(NULL);
303 1.1 cgd
304 1.1 cgd /* The file is executable and old enough */
305 1.1 cgd if (older && (S_IXUSR & buf.st_mode)) {
306 1.1 cgd /*
307 1.1 cgd * Now we know we want to run the file, we can turn
308 1.1 cgd * off the execute bit
309 1.1 cgd */
310 1.1 cgd
311 1.1 cgd PRIV_START
312 1.1 cgd
313 1.1 cgd if (chmod(dirent->d_name, S_IRUSR) != 0)
314 1.1 cgd perr("Cannot change file permissions");
315 1.1 cgd
316 1.1 cgd PRIV_END
317 1.1 cgd
318 1.1 cgd run_file(dirent->d_name, buf.st_uid, buf.st_gid);
319 1.1 cgd }
320 1.1 cgd /* Delete older files */
321 1.1 cgd if (older && !(S_IXUSR & buf.st_mode) &&
322 1.1 cgd (S_IRUSR & buf.st_mode))
323 1.1 cgd unlink(dirent->d_name);
324 1.1 cgd }
325 1.1 cgd closelog();
326 1.1 cgd exit(EXIT_SUCCESS);
327 1.1 cgd }
328