summaryrefslogtreecommitdiff
path: root/libexec/atrun/atrun.c
blob: 02baf1d1364ad1ab1c125ffd6b3394ae5783718e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*	$OpenBSD: atrun.c,v 1.8 1999/08/06 20:41:05 deraadt Exp $	*/

/*
 *  atrun.c - run jobs queued by at; run with root privileges.
 *  Copyright (C) 1993, 1994 Thomas Koenig
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author(s) may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* System Headers */

#include <sys/fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <syslog.h>
#include <utmp.h>

#include <paths.h>

/* Local headers */

#define MAIN
#include "privs.h"
#include "pathnames.h"
#include "atrun.h"

/* File scope defines */

#if (MAXLOGNAME-1) > UT_NAMESIZE
#define LOGNAMESIZE UT_NAMESIZE
#else
#define LOGNAMESIZE (MAXLOGNAME-1)
#endif

/* File scope variables */

static char *namep;
static char rcsid[] = "$OpenBSD: atrun.c,v 1.8 1999/08/06 20:41:05 deraadt Exp $";
static int debug = 0;

/* Local functions */

static void
perr(a)
	const char *a;
{
	if (debug)
		perror(a);
	else
		syslog(LOG_ERR, "%s: %m", a);

	exit(EXIT_FAILURE);
}

static void
perr2(a, b)
	char *a, *b;
{
	if (debug) {
		(void)fputs(a, stderr);
		perror(b);
	} else
		syslog(LOG_ERR, "%s%s: %m", a, b);

	exit(EXIT_FAILURE);
}

static int
write_string(fd, a)
	int fd;
	const char *a;
{
	return(write(fd, a, strlen(a)));
}

static void
run_file(filename, uid, gid)
	const char *filename;
	uid_t uid;
	gid_t gid;
{
	/*
	 * Run a file by by spawning off a process which redirects I/O,
	 * spawns a subshell, then waits for it to complete and spawns another
	 * process to send mail to the user.
	 */
	pid_t pid;
	int fd_out, fd_in;
	int queue;
	char mailbuf[LOGNAMESIZE + 1], fmt[49];
	char *mailname = NULL;
	FILE *stream;
	int send_mail = 0;
	struct stat buf, lbuf;
	off_t size;
	struct passwd *pentry;
	int fflags;
	uid_t nuid;
	gid_t ngid;

	PRIV_START

	if (chmod(filename, S_IRUSR) != 0)
		perr("Cannot change file permissions");

	PRIV_END

	pid = fork();
	if (pid == -1)
		perr("Cannot fork");
	else if (pid != 0)
		return;

	/*
	 * Let's see who we mail to.  Hopefully, we can read it from the
	 * command file; if not, send it to the owner, or, failing that, to
	 * root.
	 */

	pentry = getpwuid(uid);
	if (pentry == NULL) {
		syslog(LOG_ERR,"Userid %u not found - aborting job %s",
		    uid, filename);
		exit(EXIT_FAILURE);
	}
	PRIV_START

	stream = fopen(filename, "r");

	PRIV_END

	if (pentry->pw_expire && time(NULL) >= pentry->pw_expire) {
		syslog(LOG_ERR, "Userid %u has expired - aborting job %s",
		    uid, filename);
		exit(EXIT_FAILURE);
	}

	if (stream == NULL)
		perr("Cannot open input file");

	if ((fd_in = dup(fileno(stream))) < 0)
		perr("Error duplicating input file descriptor");

	if (fstat(fd_in, &buf) == -1)
		perr("Error in fstat of input file descriptor");

	PRIV_START

	if (lstat(filename, &lbuf) == -1)
		perr("Error in lstat of input file");

	PRIV_END

	if (S_ISLNK(lbuf.st_mode)) {
		syslog(LOG_ERR, "Symbolic link encountered in job %s - aborting",
		    filename);
		exit(EXIT_FAILURE);
	}
	if ((lbuf.st_dev != buf.st_dev) || (lbuf.st_ino != buf.st_ino) ||
	    (lbuf.st_uid != buf.st_uid) || (lbuf.st_gid != buf.st_gid) ||
	    (lbuf.st_size!=buf.st_size)) {
		syslog(LOG_ERR, "Somebody changed files from under us for job %s - aborting", filename);
		exit(EXIT_FAILURE);
	}
	if (buf.st_nlink > 1) {
		syslog(LOG_ERR, "Somebody is trying to run a linked script for job %s",
		    filename);
		exit(EXIT_FAILURE);
	}
	if ((fflags = fcntl(fd_in, F_GETFD)) < 0)
		perr("Error in fcntl");

	(void)fcntl(fd_in, F_SETFD, fflags & ~FD_CLOEXEC);

	(void)snprintf(fmt, sizeof(fmt),
	    "#!/bin/sh\n# atrun uid=%%u gid=%%u\n# mail %%%ds %%d",
	    LOGNAMESIZE);
	if (fscanf(stream, fmt, &nuid, &ngid, mailbuf, &send_mail) != 4) {
		syslog(LOG_ERR, "File %s is in wrong format - aborting",
		    filename);
		exit(EXIT_FAILURE);
	}
	if (mailbuf[0] == '-') {
		syslog(LOG_ERR, "illegal mail name %s in %s", mailbuf, filename);
		exit(EXIT_FAILURE);
	}
	mailname = mailbuf;
	if (nuid != uid) {
		syslog(LOG_ERR, "Job %s - userid %u does not match file uid %u",
		    filename, nuid, uid);
		exit(EXIT_FAILURE);
	}
	if (ngid != gid) {
		syslog(LOG_ERR, "Job %s - groupid %u does not match file gid %u",
		    filename, ngid, gid);
		exit(EXIT_FAILURE);
	}
	(void)fclose(stream);

	PRIV_START

	if (chdir(_PATH_ATSPOOL) < 0)
		perr2("Cannot chdir to ", _PATH_ATSPOOL);

	/*
	 * Create a file to hold the output of the job we are  about to
	 * run. Write the mail header.
	 */

	if ((fd_out = open(filename,
		    O_WRONLY | O_CREAT | O_EXCL, S_IWUSR | S_IRUSR)) < 0)
		perr("Cannot create output file");

	PRIV_END

	write_string(fd_out, "To: ");
	write_string(fd_out, mailname);
	write_string(fd_out, "\nSubject: Output from your job ");
	write_string(fd_out, filename);
	write_string(fd_out, "\n\n");
	if (fstat(fd_out, &buf) == -1)
		perr("Error in fstat of output file descriptor");
	size = buf.st_size;

	(void)close(STDIN_FILENO);
	(void)close(STDOUT_FILENO);
	(void)close(STDERR_FILENO);

	pid = fork();
	if (pid < 0)
		perr("Error in fork");
	else if (pid == 0) {
		char *nul = NULL;
		char **nenvp = &nul;

		/*
		 * Set up things for the child; we want standard input from
		 * the input file, and standard output and error sent to
		 * our output file.
		 */
		if (lseek(fd_in, (off_t) 0, SEEK_SET) < 0)
			perr("Error in lseek");

		if (dup(fd_in) != STDIN_FILENO)
			perr("Error in I/O redirection");

		if (dup(fd_out) != STDOUT_FILENO)
			perr("Error in I/O redirection");

		if (dup(fd_out) != STDERR_FILENO)
			perr("Error in I/O redirection");

		(void)close(fd_in);
		(void)close(fd_out);

		PRIV_START

		if (chdir(_PATH_ATJOBS) < 0)
			perr2("Cannot chdir to ", _PATH_ATJOBS);

		queue = *filename;

		if (queue > 'b')
		    nice(queue - 'b');

		if (initgroups(pentry->pw_name, pentry->pw_gid) < 0)
			perr("Cannot init group list");

		if (setegid(pentry->pw_gid) < 0 || setgid(pentry->pw_gid) < 0)
			perr("Cannot change primary group");

		if (setlogin(pentry->pw_name) < 0)
			perr("Cannot set login name");

		if (seteuid(uid) < 0 || setuid(uid) < 0)
			perr("Cannot set user id");

		if (chdir(pentry->pw_dir) < 0)
			chdir("/");

		if (execle("/bin/sh", "sh", (char *)NULL, nenvp) != 0)
			perr("Exec failed for /bin/sh");

		PRIV_END
	}
	/* We're the parent.  Let's wait. */
	(void)close(fd_in);
	(void)close(fd_out);
	waitpid(pid, (int *)NULL, 0);

	/*
	 * Send mail.  Unlink the output file first, so it is deleted
	 * after the run.
	 */
	PRIV_START

	if (stat(filename, &buf) == -1)
		perr("Error in stat of output file");
	if (open(filename, O_RDONLY) != STDIN_FILENO)
		perr("Open of jobfile failed");

	(void)unlink(filename);

	PRIV_END

	if ((buf.st_size != size) || send_mail) {
		/* Fork off a child for sending mail */

		PRIV_START

		if (initgroups(pentry->pw_name, pentry->pw_gid))
			perr("Cannot init group list");

		if (setegid(gid) < 0 || setgid(gid) < 0)
			perr("Cannot change primary group");

		if (setlogin(pentry->pw_name) < 0)
			perr("Cannot set login name");

		if (seteuid(uid) < 0 || setuid(uid) < 0)
			perr("Cannot set user id");

		if (chdir(pentry->pw_dir))
			chdir("/");

		execl(_PATH_SENDMAIL, "sendmail", "-F", "Atrun Service",
		    "-odi", "-oem", "-t", (char *) NULL);
		perr("Exec failed for mail command");

		PRIV_END
	}
	exit(EXIT_SUCCESS);
}

/* Global functions */

int
main(argc, argv)
	int argc;
	char *argv[];
{
	/*
	 * Browse through  _PATH_ATJOBS, checking all the jobfiles wether
	 * they should be executed and or deleted. The queue is coded into
	 * the first byte of the job filename, the date (in minutes since
	 * Eon) as a hex number in the following eight bytes, followed by
	 * a dot and a serial number.  A file which has not been executed
	 * yet is denoted by its execute - bit set.  For those files which
	 * are to be executed, run_file() is called, which forks off a
	 * child which takes care of I/O redirection, forks off another
	 * child for execution and yet another one, optionally, for sending
	 * mail.  Files which already have run are removed during the
	 * next invocation.
	 */
	DIR *spool;
	struct dirent *dirent;
	struct stat buf;
	unsigned long ctm;
	int jobno;
	char queue;
	time_t now, run_time;
	char batch_name[] = "Z2345678901234";
	uid_t batch_uid;
	gid_t batch_gid;
	int c;
	int run_batch;
	double la, load_avg = ATRUN_MAXLOAD;

	/*
	 * We don't need root privileges all the time; running under uid
	 * and gid nobody is fine except for priviledged operations.
	 */
	RELINQUISH_PRIVS_ROOT(NOBODY_UID, NOBODY_GID)

	openlog("atrun", LOG_PID, LOG_CRON);

	opterr = 0;
	errno = 0;
	while ((c = getopt(argc, argv, "dl:")) != -1) {
		switch (c) {
		case 'l':
			if (sscanf(optarg, "%lf", &load_avg) != 1)
				perr("garbled option -l");
			if (load_avg <= 0.)
				load_avg = ATRUN_MAXLOAD;
			break;

		case 'd':
			debug++;
			break;

		case '?':
			perr("unknown option");
			break;

		default:
			perr("idiotic option - aborted");
			break;
		}
	}

	namep = argv[0];

	PRIV_START

	if (chdir(_PATH_ATJOBS) != 0)
		perr2("Cannot change to ", _PATH_ATJOBS);


	/*
	 * Main loop. Open spool directory for reading and look over all
	 * the files in there. If the filename indicates that the job
	 * should be run and the x bit is set, fork off a child which sets
	 * its user and group id to that of the files and exec a /bin/sh
	 * which executes the shell script. Unlink older files if they
	 * should no longer be run.  For deletion, their r bit has to be
	 * turned on.
	 *
	 * Also, pick the oldest batch job to run, at most one per
	 * invocation of atrun.
	 */
	if ((spool = opendir(".")) == NULL)
		perr2("Cannot read ", _PATH_ATJOBS);

	PRIV_END

	now = time(NULL);
	run_batch = 0;
	batch_uid = (uid_t) -1;
	batch_gid = (gid_t) -1;

	while ((dirent = readdir(spool)) != NULL) {
		PRIV_START

		if (stat(dirent->d_name, &buf) != 0)
			perr2("Cannot stat in ", _PATH_ATJOBS);

		PRIV_END

		/* We don't want directories */
		if (!S_ISREG(buf.st_mode))
			continue;

		if (sscanf(dirent->d_name, "%c%5x%8lx", &queue, &jobno, &ctm) != 3)
			continue;

		run_time = (time_t) ctm * 60;

		if ((S_IXUSR & buf.st_mode) && (run_time <= now)) {
			if (isupper(queue) &&
			    (strcmp(batch_name, dirent->d_name) > 0)) {
				run_batch = 1;
				(void)strncpy(batch_name, dirent->d_name,
				    sizeof(batch_name));
				batch_uid = buf.st_uid;
				batch_gid = buf.st_gid;
			}

			/* The file is executable and old enough */
			if (islower(queue))
				run_file(dirent->d_name, buf.st_uid, buf.st_gid);
		}

		/* Delete older files */
		if ((run_time < now) && !(S_IXUSR & buf.st_mode) &&
		    (S_IRUSR & buf.st_mode)) {
			PRIV_START

			(void)unlink(dirent->d_name);

			PRIV_END
		}
	}

	/* Run the single batch file, if any */
	if (run_batch && ((getloadavg(&la, 1) == 1) && la < load_avg))
		run_file(batch_name, batch_uid, batch_gid);

	closelog();
	exit(EXIT_SUCCESS);
}