2011-09-16 20:13:16 +04:00
|
|
|
/* $NetBSD: atrun.c,v 1.21 2011/09/16 16:13:17 plunky Exp $ */
|
1996-08-31 00:15:06 +04:00
|
|
|
|
1993-12-05 14:34:40 +03:00
|
|
|
/*
|
1998-06-28 01:15:42 +04:00
|
|
|
* atrun.c - run jobs queued by at; run with root privileges.
|
|
|
|
* Copyright (C) 1993, 1994 Thomas Koenig
|
1993-12-05 14:34:40 +03:00
|
|
|
*
|
|
|
|
* 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.
|
1998-06-28 01:15:42 +04:00
|
|
|
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
1993-12-05 14:34:40 +03:00
|
|
|
* 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 */
|
2007-12-15 19:32:05 +03:00
|
|
|
#include <sys/cdefs.h>
|
1998-06-28 01:15:42 +04:00
|
|
|
#include <sys/types.h>
|
1993-12-05 14:34:40 +03:00
|
|
|
#include <sys/wait.h>
|
2002-07-29 04:35:49 +04:00
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
#include <ctype.h>
|
1993-12-05 14:34:40 +03:00
|
|
|
#include <errno.h>
|
2002-07-29 04:35:49 +04:00
|
|
|
#include <dirent.h>
|
1999-02-12 18:04:00 +03:00
|
|
|
#include <fcntl.h>
|
1993-12-05 14:34:40 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2002-07-29 04:35:49 +04:00
|
|
|
#include <limits.h>
|
1993-12-05 14:34:40 +03:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <syslog.h>
|
2002-07-29 04:35:49 +04:00
|
|
|
#include <pwd.h>
|
2005-10-31 18:46:50 +03:00
|
|
|
#include <grp.h>
|
2002-07-29 04:35:49 +04:00
|
|
|
#include <err.h>
|
1993-12-05 14:34:40 +03:00
|
|
|
#include <paths.h>
|
2005-10-31 19:15:42 +03:00
|
|
|
#include <stdarg.h>
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
/* Local headers */
|
|
|
|
|
|
|
|
#define MAIN
|
|
|
|
#include "privs.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
#include "atrun.h"
|
|
|
|
|
|
|
|
/* File scope variables */
|
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
#if 0
|
|
|
|
static char rcsid[] = "$OpenBSD: atrun.c,v 1.7 1997/09/08 22:12:10 millert Exp $";
|
|
|
|
#else
|
2011-09-16 20:13:16 +04:00
|
|
|
__RCSID("$NetBSD: atrun.c,v 1.21 2011/09/16 16:13:17 plunky Exp $");
|
1997-10-07 14:49:16 +04:00
|
|
|
#endif
|
1993-12-05 14:34:40 +03:00
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
static int debug = 0;
|
|
|
|
|
1993-12-05 14:34:40 +03:00
|
|
|
/* Local functions */
|
2007-12-15 22:44:37 +03:00
|
|
|
static void perr(const char *, ...) __dead;
|
|
|
|
static void perrx(const char *, ...) __dead;
|
2005-10-31 19:15:42 +03:00
|
|
|
static int write_string(int, const char *);
|
|
|
|
static void run_file(const char *, uid_t, gid_t);
|
|
|
|
static void become_user(struct passwd *, uid_t);
|
1998-06-28 01:15:42 +04:00
|
|
|
|
2005-10-31 18:46:50 +03:00
|
|
|
static const char nobody[] = "nobody";
|
|
|
|
|
1993-12-05 14:34:40 +03:00
|
|
|
static void
|
2005-10-31 19:15:42 +03:00
|
|
|
perr(const char *fmt, ...)
|
1993-12-05 14:34:40 +03:00
|
|
|
{
|
2005-10-31 19:15:42 +03:00
|
|
|
char buf[2048];
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
(void)vsnprintf(buf, sizeof(buf), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
if (debug)
|
2005-10-31 19:15:42 +03:00
|
|
|
warn("%s", buf);
|
1998-06-28 01:15:42 +04:00
|
|
|
else
|
2005-10-31 19:15:42 +03:00
|
|
|
syslog(LOG_ERR, "%s: %m", buf);
|
1998-06-28 01:15:42 +04:00
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-10-31 19:15:42 +03:00
|
|
|
perrx(const char *fmt, ...)
|
1998-06-28 01:15:42 +04:00
|
|
|
{
|
2005-10-31 19:15:42 +03:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
vwarnx(fmt, ap);
|
|
|
|
else
|
|
|
|
vsyslog(LOG_ERR, fmt, ap);
|
|
|
|
|
|
|
|
va_end(ap);
|
1998-06-28 01:15:42 +04:00
|
|
|
|
1993-12-05 14:34:40 +03:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-10-31 19:15:42 +03:00
|
|
|
write_string(int fd, const char *a)
|
1993-12-05 14:34:40 +03:00
|
|
|
{
|
2005-10-31 19:15:42 +03:00
|
|
|
return write(fd, a, strlen(a));
|
1998-06-28 01:15:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-10-31 19:15:42 +03:00
|
|
|
become_user(struct passwd *pentry, uid_t uid)
|
1998-06-28 01:15:42 +04:00
|
|
|
{
|
2005-10-31 19:15:42 +03:00
|
|
|
if (initgroups(pentry->pw_name, pentry->pw_gid) == -1)
|
|
|
|
perr("Cannot init group list for `%s'", pentry->pw_name);
|
1998-06-28 01:15:42 +04:00
|
|
|
|
2005-10-31 19:15:42 +03:00
|
|
|
if (setegid(pentry->pw_gid) == -1 || setgid(pentry->pw_gid) == -1)
|
|
|
|
perr("Cannot change primary group to %lu",
|
|
|
|
(unsigned long)pentry->pw_gid);
|
1998-06-28 01:15:42 +04:00
|
|
|
|
2005-10-31 19:15:42 +03:00
|
|
|
if (setsid() == -1)
|
2003-03-03 20:14:36 +03:00
|
|
|
perr("Cannot create a session");
|
|
|
|
|
2005-10-31 19:15:42 +03:00
|
|
|
if (setlogin(pentry->pw_name) == -1)
|
|
|
|
perr("Cannot set login name to `%s'", pentry->pw_name);
|
1998-06-28 01:15:42 +04:00
|
|
|
|
2005-10-31 19:15:42 +03:00
|
|
|
if (setuid(uid) == -1 || seteuid(uid) == -1)
|
|
|
|
perr("Cannot set user id to %lu", (unsigned long)uid);
|
1998-06-28 01:15:42 +04:00
|
|
|
|
2005-10-31 19:15:42 +03:00
|
|
|
if (chdir(pentry->pw_dir) == -1)
|
|
|
|
(void)chdir("/");
|
1993-12-05 14:34:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-10-31 19:15:42 +03:00
|
|
|
run_file(const char *filename, uid_t uid, gid_t gid)
|
1993-12-05 14:34:40 +03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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;
|
2000-06-25 17:32:34 +04:00
|
|
|
char mailbuf[LOGIN_NAME_MAX], fmt[49];
|
1993-12-05 14:34:40 +03:00
|
|
|
char *mailname = NULL;
|
|
|
|
FILE *stream;
|
|
|
|
int send_mail = 0;
|
1998-06-28 01:15:42 +04:00
|
|
|
struct stat buf, lbuf;
|
1993-12-05 14:34:40 +03:00
|
|
|
off_t size;
|
|
|
|
struct passwd *pentry;
|
|
|
|
int fflags;
|
2005-10-31 19:15:42 +03:00
|
|
|
uid_t nuid;
|
|
|
|
gid_t ngid;
|
|
|
|
int serrno;
|
1998-06-28 01:15:42 +04:00
|
|
|
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_START;
|
1998-06-28 01:15:42 +04:00
|
|
|
|
2005-10-31 19:15:42 +03:00
|
|
|
if (chmod(filename, S_IRUSR) == -1)
|
|
|
|
perr("Cannot change file permissions to `%s'", filename);
|
1998-06-28 01:15:42 +04:00
|
|
|
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_END;
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if (pid == -1)
|
|
|
|
perr("Cannot fork");
|
1998-06-28 01:15:42 +04:00
|
|
|
else if (pid != 0)
|
1993-12-05 14:34:40 +03:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
pentry = getpwuid(uid);
|
2005-10-31 19:15:42 +03:00
|
|
|
if (pentry == NULL)
|
|
|
|
perrx("Userid %lu not found - aborting job `%s'",
|
|
|
|
(unsigned long)uid, filename);
|
|
|
|
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_START;
|
1993-12-05 14:34:40 +03:00
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
stream = fopen(filename, "r");
|
2005-10-31 19:15:42 +03:00
|
|
|
serrno = errno;
|
1993-12-05 14:34:40 +03:00
|
|
|
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_END;
|
1993-12-05 14:34:40 +03:00
|
|
|
|
2005-10-31 19:15:42 +03:00
|
|
|
if (stream == NULL) {
|
|
|
|
errno = serrno;
|
|
|
|
perr("Cannot open input file");
|
1998-06-28 01:15:42 +04:00
|
|
|
}
|
1995-03-03 01:06:06 +03:00
|
|
|
|
2005-10-31 19:15:42 +03:00
|
|
|
if (pentry->pw_expire && time(NULL) >= pentry->pw_expire)
|
|
|
|
perrx("Userid %lu has expired - aborting job `%s'",
|
|
|
|
(unsigned long)uid, filename);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
2005-10-31 19:15:42 +03:00
|
|
|
if ((fd_in = dup(fileno(stream))) == -1)
|
1993-12-05 14:34:40 +03:00
|
|
|
perr("Error duplicating input file descriptor");
|
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
if (fstat(fd_in, &buf) == -1)
|
|
|
|
perr("Error in fstat of input file descriptor");
|
|
|
|
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_START;
|
1998-06-28 01:15:42 +04:00
|
|
|
|
|
|
|
if (lstat(filename, &lbuf) == -1)
|
2005-10-31 19:15:42 +03:00
|
|
|
perr("Error in lstat of `%s'", filename);
|
1998-06-28 01:15:42 +04:00
|
|
|
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_END;
|
1998-06-28 01:15:42 +04:00
|
|
|
|
2005-10-31 19:15:42 +03:00
|
|
|
if (S_ISLNK(lbuf.st_mode))
|
|
|
|
perrx("Symbolic link encountered in job `%s' - aborting",
|
1998-06-28 01:15:42 +04:00
|
|
|
filename);
|
2005-10-31 19:15:42 +03:00
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
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) ||
|
2005-10-31 19:15:42 +03:00
|
|
|
(lbuf.st_size!=buf.st_size))
|
|
|
|
perrx("Somebody changed files from under us for job `%s' "
|
|
|
|
"- aborting", filename);
|
|
|
|
|
|
|
|
if (buf.st_nlink > 1)
|
|
|
|
perrx("Somebody is trying to run a linked script for job `%s'",
|
1998-06-28 01:15:42 +04:00
|
|
|
filename);
|
2005-10-31 19:15:42 +03:00
|
|
|
|
1993-12-05 14:34:40 +03:00
|
|
|
if ((fflags = fcntl(fd_in, F_GETFD)) < 0)
|
|
|
|
perr("Error in fcntl");
|
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
(void)fcntl(fd_in, F_SETFD, fflags & ~FD_CLOEXEC);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
(void)snprintf(fmt, sizeof(fmt),
|
2002-06-06 13:29:31 +04:00
|
|
|
"#!/bin/sh\n# atrun uid=%%u gid=%%u\n# mail %%%ds %%d",
|
2000-06-25 17:32:34 +04:00
|
|
|
LOGIN_NAME_MAX);
|
2005-10-31 19:15:42 +03:00
|
|
|
|
|
|
|
if (fscanf(stream, fmt, &nuid, &ngid, mailbuf, &send_mail) != 4)
|
|
|
|
perrx("File `%s' is in wrong format - aborting", filename);
|
|
|
|
|
|
|
|
if (mailbuf[0] == '-')
|
|
|
|
perrx("Illegal mail name `%s' in `%s'", mailbuf, filename);
|
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
mailname = mailbuf;
|
2005-10-31 19:15:42 +03:00
|
|
|
if (nuid != uid)
|
|
|
|
perrx("Job `%s' - userid %lu does not match file uid %lu",
|
|
|
|
filename, (unsigned long)nuid, (unsigned long)uid);
|
|
|
|
|
|
|
|
if (ngid != gid)
|
|
|
|
perrx("Job `%s' - groupid %lu does not match file gid %lu",
|
|
|
|
filename, (unsigned long)ngid, (unsigned long)gid);
|
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
(void)fclose(stream);
|
|
|
|
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_START;
|
1998-06-28 01:15:42 +04:00
|
|
|
|
2005-10-31 19:15:42 +03:00
|
|
|
if (chdir(_PATH_ATSPOOL) == -1)
|
|
|
|
perr("Cannot chdir to `%s'", _PATH_ATSPOOL);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a file to hold the output of the job we are about to
|
|
|
|
* run. Write the mail header.
|
|
|
|
*/
|
1998-06-28 01:15:42 +04:00
|
|
|
|
1993-12-05 14:34:40 +03:00
|
|
|
if ((fd_out = open(filename,
|
2005-10-31 19:15:42 +03:00
|
|
|
O_WRONLY | O_CREAT | O_EXCL, S_IWUSR | S_IRUSR)) == -1)
|
|
|
|
perr("Cannot create output file `%s'", filename);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_END;
|
1998-06-28 01:15:42 +04:00
|
|
|
|
|
|
|
write_string(fd_out, "To: ");
|
|
|
|
write_string(fd_out, mailname);
|
|
|
|
write_string(fd_out, "\nSubject: Output from your job ");
|
1993-12-05 14:34:40 +03:00
|
|
|
write_string(fd_out, filename);
|
|
|
|
write_string(fd_out, "\n\n");
|
1998-06-28 01:15:42 +04:00
|
|
|
if (fstat(fd_out, &buf) == -1)
|
|
|
|
perr("Error in fstat of output file descriptor");
|
1993-12-05 14:34:40 +03:00
|
|
|
size = buf.st_size;
|
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
(void)close(STDIN_FILENO);
|
|
|
|
(void)close(STDOUT_FILENO);
|
|
|
|
(void)close(STDERR_FILENO);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2005-10-31 19:15:42 +03:00
|
|
|
if (lseek(fd_in, (off_t) 0, SEEK_SET) == (off_t)-1)
|
1993-12-05 14:34:40 +03:00
|
|
|
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");
|
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
(void)close(fd_in);
|
|
|
|
(void)close(fd_out);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_START;
|
1993-12-05 14:34:40 +03:00
|
|
|
|
2005-10-31 19:15:42 +03:00
|
|
|
if (chdir(_PATH_ATJOBS) == -1)
|
|
|
|
perr("Cannot chdir to `%s'", _PATH_ATJOBS);
|
1995-03-03 01:06:06 +03:00
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
queue = *filename;
|
1993-12-05 14:34:40 +03:00
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
if (queue > 'b')
|
|
|
|
nice(queue - 'b');
|
1993-12-05 14:34:40 +03:00
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
become_user(pentry, uid);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
2005-10-31 19:15:42 +03:00
|
|
|
(void)execle("/bin/sh", "sh", (char *)NULL, nenvp);
|
|
|
|
perr("Exec failed for /bin/sh");
|
1993-12-05 14:34:40 +03:00
|
|
|
}
|
|
|
|
/* We're the parent. Let's wait. */
|
1998-06-28 01:15:42 +04:00
|
|
|
(void)close(fd_in);
|
|
|
|
(void)close(fd_out);
|
2011-09-16 20:13:16 +04:00
|
|
|
(void)waitpid(pid, NULL, 0);
|
1998-06-28 01:15:42 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Send mail. Unlink the output file first, so it is deleted
|
|
|
|
* after the run.
|
|
|
|
*/
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_START;
|
1998-06-28 01:15:42 +04:00
|
|
|
|
|
|
|
if (stat(filename, &buf) == -1)
|
2005-10-31 19:15:42 +03:00
|
|
|
perr("Error in stat of output file `%s'", filename);
|
1998-06-28 01:15:42 +04:00
|
|
|
if (open(filename, O_RDONLY) != STDIN_FILENO)
|
2005-10-31 19:15:42 +03:00
|
|
|
perr("Open of jobfile `%s' failed", filename);
|
1998-06-28 01:15:42 +04:00
|
|
|
|
|
|
|
(void)unlink(filename);
|
|
|
|
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_END;
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
if ((buf.st_size != size) || send_mail) {
|
|
|
|
/* Fork off a child for sending mail */
|
1998-06-28 01:15:42 +04:00
|
|
|
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_START;
|
1998-06-28 01:15:42 +04:00
|
|
|
|
|
|
|
become_user(pentry, uid);
|
|
|
|
|
|
|
|
execl(_PATH_SENDMAIL, "sendmail", "-F", "Atrun Service",
|
|
|
|
"-odi", "-oem", "-t", (char *) NULL);
|
2005-10-31 19:15:42 +03:00
|
|
|
perr("Exec failed for mail command `%s'", _PATH_SENDMAIL);
|
1998-06-28 01:15:42 +04:00
|
|
|
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_END;
|
1993-12-05 14:34:40 +03:00
|
|
|
}
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Global functions */
|
|
|
|
|
|
|
|
int
|
2005-10-31 19:15:42 +03:00
|
|
|
main(int argc, char *argv[])
|
1993-12-05 14:34:40 +03:00
|
|
|
{
|
|
|
|
/*
|
2011-08-01 16:28:53 +04:00
|
|
|
* Browse through _PATH_ATJOBS, checking all the jobfiles whether
|
1993-12-05 14:34:40 +03:00
|
|
|
* 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;
|
1998-06-28 01:15:42 +04:00
|
|
|
int jobno;
|
1993-12-05 14:34:40 +03:00
|
|
|
char queue;
|
1998-06-28 01:15:42 +04:00
|
|
|
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;
|
2005-10-31 18:46:50 +03:00
|
|
|
struct group *grp;
|
|
|
|
struct passwd *pwd;
|
|
|
|
|
2005-10-31 19:15:42 +03:00
|
|
|
openlog("atrun", LOG_PID, LOG_CRON);
|
|
|
|
|
2005-10-31 18:46:50 +03:00
|
|
|
if ((grp = getgrnam(nobody)) == NULL)
|
2005-10-31 19:15:42 +03:00
|
|
|
perrx("Cannot get gid for `%s'", nobody);
|
2005-10-31 18:46:50 +03:00
|
|
|
|
|
|
|
if ((pwd = getpwnam(nobody)) == NULL)
|
2005-10-31 19:15:42 +03:00
|
|
|
perrx("Cannot get uid for `%s'", nobody);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't need root privileges all the time; running under uid
|
1999-08-16 06:59:22 +04:00
|
|
|
* and gid nobody is fine except for privileged operations.
|
1993-12-05 14:34:40 +03:00
|
|
|
*/
|
2008-04-06 00:17:37 +04:00
|
|
|
RELINQUISH_PRIVS_ROOT(pwd->pw_uid, grp->gr_gid);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
opterr = 0;
|
|
|
|
errno = 0;
|
|
|
|
while ((c = getopt(argc, argv, "dl:")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'l':
|
|
|
|
if (sscanf(optarg, "%lf", &load_avg) != 1)
|
2005-10-31 19:15:42 +03:00
|
|
|
perrx("Bad argument to option -l: ", optarg);
|
|
|
|
if (load_avg <= 0)
|
1998-06-28 01:15:42 +04:00
|
|
|
load_avg = ATRUN_MAXLOAD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
debug++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
2005-10-31 19:15:42 +03:00
|
|
|
perrx("Usage: %s [-l <loadav>] [-d]", getprogname());
|
|
|
|
/*NOTREACHED*/
|
1998-06-28 01:15:42 +04:00
|
|
|
|
|
|
|
default:
|
2005-10-31 19:15:42 +03:00
|
|
|
perrx("Invalid option: %c", c);
|
|
|
|
/*NOTREACHED*/
|
1998-06-28 01:15:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_START;
|
1998-06-28 01:15:42 +04:00
|
|
|
|
2005-10-31 19:15:42 +03:00
|
|
|
if (chdir(_PATH_ATJOBS) == -1)
|
|
|
|
perr("Cannot change directory to `%s'", _PATH_ATJOBS);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
1998-06-28 01:15:42 +04:00
|
|
|
*
|
|
|
|
* Also, pick the oldest batch job to run, at most one per
|
|
|
|
* invocation of atrun.
|
1993-12-05 14:34:40 +03:00
|
|
|
*/
|
|
|
|
if ((spool = opendir(".")) == NULL)
|
2005-10-31 19:15:42 +03:00
|
|
|
perr("Cannot open `%s'", _PATH_ATJOBS);
|
1998-06-28 01:15:42 +04:00
|
|
|
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_END;
|
1998-06-28 01:15:42 +04:00
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
run_batch = 0;
|
|
|
|
batch_uid = (uid_t) -1;
|
|
|
|
batch_gid = (gid_t) -1;
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
while ((dirent = readdir(spool)) != NULL) {
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_START;
|
1993-12-05 14:34:40 +03:00
|
|
|
|
2005-10-31 19:15:42 +03:00
|
|
|
if (stat(dirent->d_name, &buf) == -1)
|
|
|
|
perr("Cannot stat `%s' in `%s'", dirent->d_name,
|
|
|
|
_PATH_ATJOBS);
|
1998-06-28 01:15:42 +04:00
|
|
|
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_END;
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
/* We don't want directories */
|
|
|
|
if (!S_ISREG(buf.st_mode))
|
|
|
|
continue;
|
|
|
|
|
2005-10-31 19:15:42 +03:00
|
|
|
if (sscanf(dirent->d_name, "%c%5x%8lx", &queue, &jobno,
|
|
|
|
&ctm) != 3)
|
1993-12-05 14:34:40 +03:00
|
|
|
continue;
|
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
run_time = (time_t) ctm * 60;
|
|
|
|
|
|
|
|
if ((S_IXUSR & buf.st_mode) && (run_time <= now)) {
|
2004-11-06 00:30:31 +03:00
|
|
|
if (isupper((unsigned char)queue) &&
|
1998-06-28 01:15:42 +04:00
|
|
|
(strcmp(batch_name, dirent->d_name) > 0)) {
|
|
|
|
run_batch = 1;
|
2003-05-18 02:57:30 +04:00
|
|
|
(void)strlcpy(batch_name, dirent->d_name,
|
1998-06-28 01:15:42 +04:00
|
|
|
sizeof(batch_name));
|
|
|
|
batch_uid = buf.st_uid;
|
|
|
|
batch_gid = buf.st_gid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The file is executable and old enough */
|
2004-11-06 00:30:31 +03:00
|
|
|
if (islower((unsigned char)queue))
|
2005-10-31 19:15:42 +03:00
|
|
|
run_file(dirent->d_name, buf.st_uid,
|
|
|
|
buf.st_gid);
|
1998-06-28 01:15:42 +04:00
|
|
|
}
|
1993-12-05 14:34:40 +03:00
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
/* Delete older files */
|
|
|
|
if ((run_time < now) && !(S_IXUSR & buf.st_mode) &&
|
|
|
|
(S_IRUSR & buf.st_mode)) {
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_START;
|
1993-12-05 14:34:40 +03:00
|
|
|
|
1998-06-28 01:15:42 +04:00
|
|
|
(void)unlink(dirent->d_name);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
2008-04-06 00:17:37 +04:00
|
|
|
PRIV_END;
|
1993-12-05 14:34:40 +03:00
|
|
|
}
|
|
|
|
}
|
1998-06-28 01:15:42 +04:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
1993-12-05 14:34:40 +03:00
|
|
|
closelog();
|
2005-10-31 19:15:42 +03:00
|
|
|
return EXIT_SUCCESS;
|
1993-12-05 14:34:40 +03:00
|
|
|
}
|