atrun.c revision 1.11 1 1.11 christos /* $NetBSD: atrun.c,v 1.11 2002/07/29 00:36:44 christos Exp $ */
2 1.3 thorpej
3 1.1 cgd /*
4 1.5 christos * atrun.c - run jobs queued by at; run with root privileges.
5 1.5 christos * Copyright (C) 1993, 1994 Thomas Koenig
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. The name of the author(s) may not be used to endorse or promote
13 1.1 cgd * products derived from this software without specific prior written
14 1.1 cgd * permission.
15 1.1 cgd *
16 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.5 christos * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 cgd * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 cgd */
27 1.1 cgd
28 1.1 cgd /* System Headers */
29 1.4 mrg #include <sys/types.h>
30 1.1 cgd #include <sys/wait.h>
31 1.10 christos
32 1.5 christos #include <ctype.h>
33 1.10 christos #include <errno.h>
34 1.1 cgd #include <dirent.h>
35 1.6 kleink #include <fcntl.h>
36 1.1 cgd #include <stdio.h>
37 1.1 cgd #include <stdlib.h>
38 1.1 cgd #include <string.h>
39 1.10 christos #include <limits.h>
40 1.1 cgd #include <time.h>
41 1.1 cgd #include <unistd.h>
42 1.1 cgd #include <syslog.h>
43 1.10 christos #include <pwd.h>
44 1.10 christos #include <err.h>
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.5 christos #if 0
58 1.5 christos static char rcsid[] = "$OpenBSD: atrun.c,v 1.7 1997/09/08 22:12:10 millert Exp $";
59 1.5 christos #else
60 1.11 christos __RCSID("$NetBSD: atrun.c,v 1.11 2002/07/29 00:36:44 christos Exp $");
61 1.4 mrg #endif
62 1.1 cgd
63 1.5 christos static int debug = 0;
64 1.5 christos
65 1.1 cgd /* Local functions */
66 1.5 christos static void perr __P((const char *));
67 1.5 christos static void perr2 __P((char *, char *));
68 1.5 christos static int write_string __P((int, const char *));
69 1.5 christos static void run_file __P((const char *, uid_t, gid_t));
70 1.5 christos static void become_user __P((struct passwd *, uid_t));
71 1.5 christos
72 1.5 christos int main __P((int, char *[]));
73 1.5 christos
74 1.1 cgd static void
75 1.1 cgd perr(a)
76 1.1 cgd const char *a;
77 1.1 cgd {
78 1.5 christos if (debug)
79 1.11 christos warn("%s", a);
80 1.5 christos else
81 1.5 christos syslog(LOG_ERR, "%s: %m", a);
82 1.5 christos
83 1.5 christos exit(EXIT_FAILURE);
84 1.5 christos }
85 1.5 christos
86 1.5 christos static void
87 1.5 christos perr2(a, b)
88 1.5 christos char *a, *b;
89 1.5 christos {
90 1.5 christos if (debug) {
91 1.10 christos warn("%s%s", a, b);
92 1.5 christos } else
93 1.5 christos syslog(LOG_ERR, "%s%s: %m", a, b);
94 1.5 christos
95 1.1 cgd exit(EXIT_FAILURE);
96 1.1 cgd }
97 1.1 cgd
98 1.1 cgd static int
99 1.1 cgd write_string(fd, a)
100 1.1 cgd int fd;
101 1.1 cgd const char *a;
102 1.1 cgd {
103 1.5 christos return(write(fd, a, strlen(a)));
104 1.5 christos }
105 1.5 christos
106 1.5 christos static void
107 1.5 christos become_user(pentry, uid)
108 1.5 christos struct passwd *pentry;
109 1.5 christos uid_t uid;
110 1.5 christos {
111 1.5 christos if (initgroups(pentry->pw_name, pentry->pw_gid) < 0)
112 1.5 christos perr("Cannot init group list");
113 1.5 christos
114 1.5 christos if (setegid(pentry->pw_gid) < 0 || setgid(pentry->pw_gid) < 0)
115 1.5 christos perr("Cannot change primary group");
116 1.5 christos
117 1.5 christos if (setlogin(pentry->pw_name) < 0)
118 1.5 christos perr("Cannot set login name");
119 1.5 christos
120 1.5 christos if (setuid(uid) < 0 || seteuid(uid) < 0)
121 1.5 christos perr("Cannot set user id");
122 1.5 christos
123 1.5 christos if (chdir(pentry->pw_dir) < 0)
124 1.5 christos chdir("/");
125 1.1 cgd }
126 1.1 cgd
127 1.1 cgd static void
128 1.5 christos run_file(filename, uid, gid)
129 1.1 cgd const char *filename;
130 1.1 cgd uid_t uid;
131 1.5 christos gid_t gid;
132 1.1 cgd {
133 1.1 cgd /*
134 1.1 cgd * Run a file by by spawning off a process which redirects I/O,
135 1.1 cgd * spawns a subshell, then waits for it to complete and spawns another
136 1.1 cgd * process to send mail to the user.
137 1.1 cgd */
138 1.1 cgd pid_t pid;
139 1.1 cgd int fd_out, fd_in;
140 1.1 cgd int queue;
141 1.8 simonb char mailbuf[LOGIN_NAME_MAX], fmt[49];
142 1.1 cgd char *mailname = NULL;
143 1.1 cgd FILE *stream;
144 1.1 cgd int send_mail = 0;
145 1.5 christos struct stat buf, lbuf;
146 1.1 cgd off_t size;
147 1.1 cgd struct passwd *pentry;
148 1.1 cgd int fflags;
149 1.9 martin uint nuid;
150 1.9 martin uint ngid;
151 1.5 christos
152 1.5 christos PRIV_START
153 1.5 christos
154 1.5 christos if (chmod(filename, S_IRUSR) != 0)
155 1.5 christos perr("Cannot change file permissions");
156 1.5 christos
157 1.5 christos PRIV_END
158 1.1 cgd
159 1.1 cgd pid = fork();
160 1.1 cgd if (pid == -1)
161 1.1 cgd perr("Cannot fork");
162 1.5 christos else if (pid != 0)
163 1.1 cgd return;
164 1.1 cgd
165 1.1 cgd /*
166 1.1 cgd * Let's see who we mail to. Hopefully, we can read it from the
167 1.1 cgd * command file; if not, send it to the owner, or, failing that, to
168 1.1 cgd * root.
169 1.1 cgd */
170 1.1 cgd
171 1.5 christos pentry = getpwuid(uid);
172 1.5 christos if (pentry == NULL) {
173 1.5 christos syslog(LOG_ERR,"Userid %u not found - aborting job %s",
174 1.5 christos uid, filename);
175 1.5 christos exit(EXIT_FAILURE);
176 1.5 christos }
177 1.1 cgd PRIV_START
178 1.1 cgd
179 1.5 christos stream = fopen(filename, "r");
180 1.1 cgd
181 1.1 cgd PRIV_END
182 1.1 cgd
183 1.5 christos if (pentry->pw_expire && time(NULL) >= pentry->pw_expire) {
184 1.5 christos syslog(LOG_ERR, "Userid %u has expired - aborting job %s",
185 1.5 christos uid, filename);
186 1.5 christos exit(EXIT_FAILURE);
187 1.5 christos }
188 1.2 cgd
189 1.1 cgd if (stream == NULL)
190 1.1 cgd perr("Cannot open input file");
191 1.1 cgd
192 1.1 cgd if ((fd_in = dup(fileno(stream))) < 0)
193 1.1 cgd perr("Error duplicating input file descriptor");
194 1.1 cgd
195 1.5 christos if (fstat(fd_in, &buf) == -1)
196 1.5 christos perr("Error in fstat of input file descriptor");
197 1.5 christos
198 1.5 christos PRIV_START
199 1.5 christos
200 1.5 christos if (lstat(filename, &lbuf) == -1)
201 1.5 christos perr("Error in lstat of input file");
202 1.5 christos
203 1.5 christos PRIV_END
204 1.5 christos
205 1.5 christos if (S_ISLNK(lbuf.st_mode)) {
206 1.5 christos syslog(LOG_ERR, "Symbolic link encountered in job %s - aborting",
207 1.5 christos filename);
208 1.5 christos exit(EXIT_FAILURE);
209 1.5 christos }
210 1.5 christos if ((lbuf.st_dev != buf.st_dev) || (lbuf.st_ino != buf.st_ino) ||
211 1.5 christos (lbuf.st_uid != buf.st_uid) || (lbuf.st_gid != buf.st_gid) ||
212 1.5 christos (lbuf.st_size!=buf.st_size)) {
213 1.5 christos syslog(LOG_ERR, "Somebody changed files from under us for job %s - aborting", filename);
214 1.5 christos exit(EXIT_FAILURE);
215 1.5 christos }
216 1.5 christos if (buf.st_nlink > 1) {
217 1.5 christos syslog(LOG_ERR, "Somebody is trying to run a linked script for job %s",
218 1.5 christos filename);
219 1.5 christos exit(EXIT_FAILURE);
220 1.5 christos }
221 1.1 cgd if ((fflags = fcntl(fd_in, F_GETFD)) < 0)
222 1.1 cgd perr("Error in fcntl");
223 1.1 cgd
224 1.5 christos (void)fcntl(fd_in, F_SETFD, fflags & ~FD_CLOEXEC);
225 1.1 cgd
226 1.5 christos (void)snprintf(fmt, sizeof(fmt),
227 1.9 martin "#!/bin/sh\n# atrun uid=%%u gid=%%u\n# mail %%%ds %%d",
228 1.8 simonb LOGIN_NAME_MAX);
229 1.5 christos if (fscanf(stream, fmt, &nuid, &ngid, mailbuf, &send_mail) != 4) {
230 1.5 christos syslog(LOG_ERR, "File %s is in wrong format - aborting",
231 1.5 christos filename);
232 1.5 christos exit(EXIT_FAILURE);
233 1.5 christos }
234 1.5 christos if (mailbuf[0] == '-') {
235 1.5 christos syslog(LOG_ERR, "illegal mail name %s in %s", mailbuf, filename);
236 1.5 christos exit(EXIT_FAILURE);
237 1.5 christos }
238 1.5 christos mailname = mailbuf;
239 1.5 christos if (nuid != uid) {
240 1.5 christos syslog(LOG_ERR, "Job %s - userid %u does not match file uid %u",
241 1.5 christos filename, nuid, uid);
242 1.5 christos exit(EXIT_FAILURE);
243 1.1 cgd }
244 1.5 christos if (ngid != gid) {
245 1.5 christos syslog(LOG_ERR, "Job %s - groupid %u does not match file gid %u",
246 1.5 christos filename, ngid, gid);
247 1.5 christos exit(EXIT_FAILURE);
248 1.5 christos }
249 1.5 christos (void)fclose(stream);
250 1.5 christos
251 1.5 christos PRIV_START
252 1.5 christos
253 1.1 cgd if (chdir(_PATH_ATSPOOL) < 0)
254 1.5 christos perr2("Cannot chdir to ", _PATH_ATSPOOL);
255 1.1 cgd
256 1.1 cgd /*
257 1.1 cgd * Create a file to hold the output of the job we are about to
258 1.1 cgd * run. Write the mail header.
259 1.1 cgd */
260 1.5 christos
261 1.1 cgd if ((fd_out = open(filename,
262 1.1 cgd O_WRONLY | O_CREAT | O_EXCL, S_IWUSR | S_IRUSR)) < 0)
263 1.1 cgd perr("Cannot create output file");
264 1.1 cgd
265 1.5 christos PRIV_END
266 1.5 christos
267 1.5 christos write_string(fd_out, "To: ");
268 1.5 christos write_string(fd_out, mailname);
269 1.5 christos write_string(fd_out, "\nSubject: Output from your job ");
270 1.1 cgd write_string(fd_out, filename);
271 1.1 cgd write_string(fd_out, "\n\n");
272 1.5 christos if (fstat(fd_out, &buf) == -1)
273 1.5 christos perr("Error in fstat of output file descriptor");
274 1.1 cgd size = buf.st_size;
275 1.1 cgd
276 1.5 christos (void)close(STDIN_FILENO);
277 1.5 christos (void)close(STDOUT_FILENO);
278 1.5 christos (void)close(STDERR_FILENO);
279 1.1 cgd
280 1.1 cgd pid = fork();
281 1.1 cgd if (pid < 0)
282 1.1 cgd perr("Error in fork");
283 1.1 cgd else if (pid == 0) {
284 1.1 cgd char *nul = NULL;
285 1.1 cgd char **nenvp = &nul;
286 1.1 cgd
287 1.1 cgd /*
288 1.1 cgd * Set up things for the child; we want standard input from
289 1.1 cgd * the input file, and standard output and error sent to
290 1.1 cgd * our output file.
291 1.1 cgd */
292 1.1 cgd if (lseek(fd_in, (off_t) 0, SEEK_SET) < 0)
293 1.1 cgd perr("Error in lseek");
294 1.1 cgd
295 1.1 cgd if (dup(fd_in) != STDIN_FILENO)
296 1.1 cgd perr("Error in I/O redirection");
297 1.1 cgd
298 1.1 cgd if (dup(fd_out) != STDOUT_FILENO)
299 1.1 cgd perr("Error in I/O redirection");
300 1.1 cgd
301 1.1 cgd if (dup(fd_out) != STDERR_FILENO)
302 1.1 cgd perr("Error in I/O redirection");
303 1.1 cgd
304 1.5 christos (void)close(fd_in);
305 1.5 christos (void)close(fd_out);
306 1.5 christos
307 1.5 christos PRIV_START
308 1.5 christos
309 1.1 cgd if (chdir(_PATH_ATJOBS) < 0)
310 1.5 christos perr2("Cannot chdir to ", _PATH_ATJOBS);
311 1.1 cgd
312 1.1 cgd queue = *filename;
313 1.1 cgd
314 1.5 christos if (queue > 'b')
315 1.5 christos nice(queue - 'b');
316 1.1 cgd
317 1.5 christos become_user(pentry, uid);
318 1.1 cgd
319 1.5 christos if (execle("/bin/sh", "sh", (char *)NULL, nenvp) != 0)
320 1.5 christos perr("Exec failed for /bin/sh");
321 1.2 cgd
322 1.5 christos PRIV_END
323 1.5 christos }
324 1.5 christos /* We're the parent. Let's wait. */
325 1.5 christos (void)close(fd_in);
326 1.5 christos (void)close(fd_out);
327 1.5 christos waitpid(pid, (int *)NULL, 0);
328 1.1 cgd
329 1.5 christos /*
330 1.5 christos * Send mail. Unlink the output file first, so it is deleted
331 1.5 christos * after the run.
332 1.5 christos */
333 1.5 christos PRIV_START
334 1.1 cgd
335 1.5 christos if (stat(filename, &buf) == -1)
336 1.5 christos perr("Error in stat of output file");
337 1.5 christos if (open(filename, O_RDONLY) != STDIN_FILENO)
338 1.5 christos perr("Open of jobfile failed");
339 1.1 cgd
340 1.5 christos (void)unlink(filename);
341 1.1 cgd
342 1.5 christos PRIV_END
343 1.1 cgd
344 1.1 cgd if ((buf.st_size != size) || send_mail) {
345 1.1 cgd /* Fork off a child for sending mail */
346 1.5 christos
347 1.5 christos PRIV_START
348 1.5 christos
349 1.5 christos become_user(pentry, uid);
350 1.5 christos
351 1.5 christos execl(_PATH_SENDMAIL, "sendmail", "-F", "Atrun Service",
352 1.5 christos "-odi", "-oem", "-t", (char *) NULL);
353 1.5 christos perr("Exec failed for mail command");
354 1.5 christos
355 1.5 christos PRIV_END
356 1.1 cgd }
357 1.1 cgd exit(EXIT_SUCCESS);
358 1.1 cgd }
359 1.1 cgd
360 1.1 cgd /* Global functions */
361 1.1 cgd
362 1.1 cgd int
363 1.1 cgd main(argc, argv)
364 1.1 cgd int argc;
365 1.1 cgd char *argv[];
366 1.1 cgd {
367 1.1 cgd /*
368 1.1 cgd * Browse through _PATH_ATJOBS, checking all the jobfiles wether
369 1.1 cgd * they should be executed and or deleted. The queue is coded into
370 1.1 cgd * the first byte of the job filename, the date (in minutes since
371 1.1 cgd * Eon) as a hex number in the following eight bytes, followed by
372 1.1 cgd * a dot and a serial number. A file which has not been executed
373 1.1 cgd * yet is denoted by its execute - bit set. For those files which
374 1.1 cgd * are to be executed, run_file() is called, which forks off a
375 1.1 cgd * child which takes care of I/O redirection, forks off another
376 1.1 cgd * child for execution and yet another one, optionally, for sending
377 1.1 cgd * mail. Files which already have run are removed during the
378 1.1 cgd * next invocation.
379 1.1 cgd */
380 1.1 cgd DIR *spool;
381 1.1 cgd struct dirent *dirent;
382 1.1 cgd struct stat buf;
383 1.1 cgd unsigned long ctm;
384 1.5 christos int jobno;
385 1.1 cgd char queue;
386 1.5 christos time_t now, run_time;
387 1.5 christos char batch_name[] = "Z2345678901234";
388 1.5 christos uid_t batch_uid;
389 1.5 christos gid_t batch_gid;
390 1.5 christos int c;
391 1.5 christos int run_batch;
392 1.5 christos double la, load_avg = ATRUN_MAXLOAD;
393 1.1 cgd
394 1.1 cgd /*
395 1.1 cgd * We don't need root privileges all the time; running under uid
396 1.7 simonb * and gid nobody is fine except for privileged operations.
397 1.1 cgd */
398 1.5 christos RELINQUISH_PRIVS_ROOT(NOBODY_UID, NOBODY_GID)
399 1.1 cgd
400 1.1 cgd openlog("atrun", LOG_PID, LOG_CRON);
401 1.1 cgd
402 1.5 christos opterr = 0;
403 1.5 christos errno = 0;
404 1.5 christos while ((c = getopt(argc, argv, "dl:")) != -1) {
405 1.5 christos switch (c) {
406 1.5 christos case 'l':
407 1.5 christos if (sscanf(optarg, "%lf", &load_avg) != 1)
408 1.5 christos perr("garbled option -l");
409 1.5 christos if (load_avg <= 0.)
410 1.5 christos load_avg = ATRUN_MAXLOAD;
411 1.5 christos break;
412 1.5 christos
413 1.5 christos case 'd':
414 1.5 christos debug++;
415 1.5 christos break;
416 1.5 christos
417 1.5 christos case '?':
418 1.5 christos perr("unknown option");
419 1.5 christos break;
420 1.5 christos
421 1.5 christos default:
422 1.5 christos perr("idiotic option - aborted");
423 1.5 christos break;
424 1.5 christos }
425 1.5 christos }
426 1.5 christos
427 1.1 cgd namep = argv[0];
428 1.5 christos
429 1.5 christos PRIV_START
430 1.5 christos
431 1.1 cgd if (chdir(_PATH_ATJOBS) != 0)
432 1.5 christos perr2("Cannot change to ", _PATH_ATJOBS);
433 1.5 christos
434 1.1 cgd
435 1.1 cgd /*
436 1.1 cgd * Main loop. Open spool directory for reading and look over all
437 1.1 cgd * the files in there. If the filename indicates that the job
438 1.1 cgd * should be run and the x bit is set, fork off a child which sets
439 1.1 cgd * its user and group id to that of the files and exec a /bin/sh
440 1.1 cgd * which executes the shell script. Unlink older files if they
441 1.1 cgd * should no longer be run. For deletion, their r bit has to be
442 1.1 cgd * turned on.
443 1.5 christos *
444 1.5 christos * Also, pick the oldest batch job to run, at most one per
445 1.5 christos * invocation of atrun.
446 1.1 cgd */
447 1.1 cgd if ((spool = opendir(".")) == NULL)
448 1.5 christos perr2("Cannot read ", _PATH_ATJOBS);
449 1.5 christos
450 1.5 christos PRIV_END
451 1.5 christos
452 1.5 christos now = time(NULL);
453 1.5 christos run_batch = 0;
454 1.5 christos batch_uid = (uid_t) -1;
455 1.5 christos batch_gid = (gid_t) -1;
456 1.1 cgd
457 1.1 cgd while ((dirent = readdir(spool)) != NULL) {
458 1.5 christos PRIV_START
459 1.1 cgd
460 1.1 cgd if (stat(dirent->d_name, &buf) != 0)
461 1.5 christos perr2("Cannot stat in ", _PATH_ATJOBS);
462 1.5 christos
463 1.5 christos PRIV_END
464 1.1 cgd
465 1.1 cgd /* We don't want directories */
466 1.1 cgd if (!S_ISREG(buf.st_mode))
467 1.1 cgd continue;
468 1.1 cgd
469 1.5 christos if (sscanf(dirent->d_name, "%c%5x%8lx", &queue, &jobno, &ctm) != 3)
470 1.1 cgd continue;
471 1.1 cgd
472 1.5 christos run_time = (time_t) ctm * 60;
473 1.1 cgd
474 1.5 christos if ((S_IXUSR & buf.st_mode) && (run_time <= now)) {
475 1.5 christos if (isupper(queue) &&
476 1.5 christos (strcmp(batch_name, dirent->d_name) > 0)) {
477 1.5 christos run_batch = 1;
478 1.5 christos (void)strncpy(batch_name, dirent->d_name,
479 1.5 christos sizeof(batch_name));
480 1.5 christos batch_uid = buf.st_uid;
481 1.5 christos batch_gid = buf.st_gid;
482 1.5 christos }
483 1.5 christos
484 1.5 christos /* The file is executable and old enough */
485 1.5 christos if (islower(queue))
486 1.5 christos run_file(dirent->d_name, buf.st_uid, buf.st_gid);
487 1.5 christos }
488 1.1 cgd
489 1.5 christos /* Delete older files */
490 1.5 christos if ((run_time < now) && !(S_IXUSR & buf.st_mode) &&
491 1.5 christos (S_IRUSR & buf.st_mode)) {
492 1.1 cgd PRIV_START
493 1.1 cgd
494 1.5 christos (void)unlink(dirent->d_name);
495 1.1 cgd
496 1.1 cgd PRIV_END
497 1.1 cgd }
498 1.1 cgd }
499 1.5 christos
500 1.5 christos /* Run the single batch file, if any */
501 1.5 christos if (run_batch && ((getloadavg(&la, 1) == 1) && la < load_avg))
502 1.5 christos run_file(batch_name, batch_uid, batch_gid);
503 1.5 christos
504 1.1 cgd closelog();
505 1.1 cgd exit(EXIT_SUCCESS);
506 1.1 cgd }
507