2017-11-27 15:25:02 +00:00
|
|
|
/*-
|
1995-04-12 02:52:21 +00:00
|
|
|
* atrun.c - run jobs queued by at; run with root privileges.
|
2017-11-27 15:25:02 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*
|
1995-04-12 02:52:21 +00:00
|
|
|
* Copyright (C) 1993, 1994 Thomas Koenig
|
1994-01-05 01:03:02 +00: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.
|
1995-08-21 12:34:18 +00:00
|
|
|
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
1994-01-05 01:03:02 +00: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.
|
|
|
|
*/
|
|
|
|
|
1997-11-20 07:21:55 +00:00
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1997-11-20 07:21:55 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1994-01-05 01:03:02 +00:00
|
|
|
/* System Headers */
|
|
|
|
|
|
|
|
#include <sys/fcntl.h>
|
2013-06-11 18:43:27 +00:00
|
|
|
#include <sys/file.h>
|
1994-01-05 01:03:02 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2012-10-30 19:46:00 +00:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
1994-01-05 01:03:02 +00:00
|
|
|
#include <sys/wait.h>
|
1997-04-12 01:04:50 +00:00
|
|
|
#include <sys/param.h>
|
1995-04-12 02:52:21 +00:00
|
|
|
#include <ctype.h>
|
1994-01-05 01:03:02 +00:00
|
|
|
#include <dirent.h>
|
1997-11-20 07:21:55 +00:00
|
|
|
#include <err.h>
|
1995-04-12 02:52:21 +00:00
|
|
|
#include <grp.h>
|
1997-11-20 07:21:55 +00:00
|
|
|
#include <pwd.h>
|
1994-01-05 01:03:02 +00:00
|
|
|
#include <signal.h>
|
2007-06-15 10:10:40 +00:00
|
|
|
#include <stdarg.h>
|
1994-01-05 01:03:02 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1997-11-20 07:21:55 +00:00
|
|
|
#include <syslog.h>
|
1994-01-05 01:03:02 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
1995-08-21 12:34:18 +00:00
|
|
|
#ifdef __FreeBSD__
|
1994-01-05 01:03:02 +00:00
|
|
|
#include <paths.h>
|
1995-08-21 12:34:18 +00:00
|
|
|
#else
|
|
|
|
#include <getopt.h>
|
1995-04-12 02:52:21 +00:00
|
|
|
#endif
|
2007-06-14 22:16:21 +00:00
|
|
|
#ifdef LOGIN_CAP
|
|
|
|
#include <login_cap.h>
|
|
|
|
#endif
|
2007-06-15 12:02:16 +00:00
|
|
|
#ifdef PAM
|
|
|
|
#include <security/pam_appl.h>
|
|
|
|
#include <security/openpam.h>
|
|
|
|
#endif
|
1994-01-05 01:03:02 +00:00
|
|
|
|
|
|
|
/* Local headers */
|
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
#include "gloadavg.h"
|
1994-01-05 01:03:02 +00:00
|
|
|
#define MAIN
|
|
|
|
#include "privs.h"
|
1995-04-12 02:52:21 +00:00
|
|
|
|
|
|
|
/* Macros */
|
|
|
|
|
|
|
|
#ifndef ATJOB_DIR
|
|
|
|
#define ATJOB_DIR "/usr/spool/atjobs/"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ATSPOOL_DIR
|
|
|
|
#define ATSPOOL_DIR "/usr/spool/atspool/"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef LOADAVG_MX
|
|
|
|
#define LOADAVG_MX 1.5
|
|
|
|
#endif
|
1994-01-05 01:03:02 +00:00
|
|
|
|
|
|
|
/* File scope variables */
|
|
|
|
|
2007-06-15 12:02:16 +00:00
|
|
|
static const char * const atrun = "atrun"; /* service name for syslog etc. */
|
2004-07-11 17:37:33 +00:00
|
|
|
static int debug = 0;
|
1994-01-05 01:03:02 +00:00
|
|
|
|
2007-06-15 10:10:40 +00:00
|
|
|
void perr(const char *fmt, ...);
|
|
|
|
void perrx(const char *fmt, ...);
|
2002-02-03 15:53:02 +00:00
|
|
|
static void usage(void);
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1995-08-21 12:34:18 +00:00
|
|
|
/* Local functions */
|
1994-01-05 01:03:02 +00:00
|
|
|
static int
|
1995-04-12 02:52:21 +00:00
|
|
|
write_string(int fd, const char* a)
|
1994-01-05 01:03:02 +00:00
|
|
|
{
|
1995-04-12 02:52:21 +00:00
|
|
|
return write(fd, a, strlen(a));
|
1994-01-05 01:03:02 +00:00
|
|
|
}
|
|
|
|
|
1995-08-21 12:34:18 +00:00
|
|
|
#undef DEBUG_FORK
|
1995-04-12 02:52:21 +00:00
|
|
|
#ifdef DEBUG_FORK
|
|
|
|
static pid_t
|
2002-02-03 15:53:02 +00:00
|
|
|
myfork(void)
|
1994-01-05 01:03:02 +00:00
|
|
|
{
|
1995-04-12 02:52:21 +00:00
|
|
|
pid_t res;
|
|
|
|
res = fork();
|
|
|
|
if (res == 0)
|
|
|
|
kill(getpid(),SIGSTOP);
|
|
|
|
return res;
|
|
|
|
}
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
#define fork myfork
|
|
|
|
#endif
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
static void
|
|
|
|
run_file(const char *filename, uid_t uid, gid_t gid)
|
|
|
|
{
|
2001-07-23 11:00:31 +00:00
|
|
|
/* Run a file by spawning off a process which redirects I/O,
|
1995-04-12 02:52:21 +00:00
|
|
|
* spawns a subshell, then waits for it to complete and sends
|
|
|
|
* mail to the user.
|
|
|
|
*/
|
|
|
|
pid_t pid;
|
|
|
|
int fd_out, fd_in;
|
|
|
|
int queue;
|
2012-09-26 20:47:39 +00:00
|
|
|
char mailbuf[MAXLOGNAME], fmt[64];
|
1995-04-12 02:52:21 +00:00
|
|
|
char *mailname = NULL;
|
|
|
|
FILE *stream;
|
|
|
|
int send_mail = 0;
|
1995-08-21 12:34:18 +00:00
|
|
|
struct stat buf, lbuf;
|
1995-04-12 02:52:21 +00:00
|
|
|
off_t size;
|
|
|
|
struct passwd *pentry;
|
|
|
|
int fflags;
|
1995-08-21 12:34:18 +00:00
|
|
|
long nuid;
|
|
|
|
long ngid;
|
2007-06-15 12:02:16 +00:00
|
|
|
#ifdef PAM
|
|
|
|
pam_handle_t *pamh = NULL;
|
|
|
|
int pam_err;
|
|
|
|
struct pam_conv pamc = {
|
|
|
|
.conv = openpam_nullconv,
|
|
|
|
.appdata_ptr = NULL
|
|
|
|
};
|
|
|
|
#endif
|
1995-04-12 02:52:21 +00:00
|
|
|
|
|
|
|
PRIV_START
|
|
|
|
|
|
|
|
if (chmod(filename, S_IRUSR) != 0)
|
|
|
|
{
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("cannot change file permissions");
|
1995-04-12 02:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PRIV_END
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if (pid == -1)
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("cannot fork");
|
1995-08-21 12:34:18 +00:00
|
|
|
|
|
|
|
else if (pid != 0)
|
1995-04-12 02:52:21 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pentry = getpwuid(uid);
|
|
|
|
if (pentry == NULL)
|
2007-06-15 10:34:36 +00:00
|
|
|
perrx("Userid %lu not found - aborting job %s",
|
|
|
|
(unsigned long) uid, filename);
|
|
|
|
|
2007-06-15 12:02:16 +00:00
|
|
|
#ifdef PAM
|
1995-04-12 02:52:21 +00:00
|
|
|
PRIV_START
|
1994-01-05 01:03:02 +00:00
|
|
|
|
2007-06-15 12:02:16 +00:00
|
|
|
pam_err = pam_start(atrun, pentry->pw_name, &pamc, &pamh);
|
|
|
|
if (pam_err != PAM_SUCCESS)
|
|
|
|
perrx("cannot start PAM: %s", pam_strerror(pamh, pam_err));
|
|
|
|
|
|
|
|
pam_err = pam_acct_mgmt(pamh, PAM_SILENT);
|
|
|
|
/* Expired password shouldn't prevent the job from running. */
|
|
|
|
if (pam_err != PAM_SUCCESS && pam_err != PAM_NEW_AUTHTOK_REQD)
|
|
|
|
perrx("Account %s (userid %lu) unavailable for job %s: %s",
|
|
|
|
pentry->pw_name, (unsigned long)uid,
|
|
|
|
filename, pam_strerror(pamh, pam_err));
|
|
|
|
|
|
|
|
pam_end(pamh, pam_err);
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
PRIV_END
|
2007-06-15 12:02:16 +00:00
|
|
|
#endif /* PAM */
|
1994-01-05 01:03:02 +00:00
|
|
|
|
2007-06-15 12:02:16 +00:00
|
|
|
PRIV_START
|
|
|
|
|
|
|
|
stream=fopen(filename, "r");
|
|
|
|
|
|
|
|
PRIV_END
|
1995-08-28 21:30:59 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
if (stream == NULL)
|
2013-06-11 18:46:46 +00:00
|
|
|
perr("cannot open input file %s", filename);
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
if ((fd_in = dup(fileno(stream))) <0)
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("error duplicating input file descriptor");
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1995-08-21 12:34:18 +00:00
|
|
|
if (fstat(fd_in, &buf) == -1)
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("error in fstat of input file descriptor");
|
1995-08-21 12:34:18 +00:00
|
|
|
|
|
|
|
if (lstat(filename, &lbuf) == -1)
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("error in fstat of input file");
|
1995-08-21 12:34:18 +00:00
|
|
|
|
2007-06-15 10:34:36 +00:00
|
|
|
if (S_ISLNK(lbuf.st_mode))
|
|
|
|
perrx("Symbolic link encountered in job %s - aborting", filename);
|
|
|
|
|
1995-08-21 12:34:18 +00: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) ||
|
2007-06-15 10:34:36 +00:00
|
|
|
(lbuf.st_size!=buf.st_size))
|
|
|
|
perrx("Somebody changed files from under us for job %s - aborting",
|
1995-08-21 12:34:18 +00:00
|
|
|
filename);
|
2007-06-15 10:34:36 +00:00
|
|
|
|
|
|
|
if (buf.st_nlink > 1)
|
|
|
|
perrx("Somebody is trying to run a linked script for job %s", filename);
|
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
if ((fflags = fcntl(fd_in, F_GETFD)) <0)
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("error in fcntl");
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
fcntl(fd_in, F_SETFD, fflags & ~FD_CLOEXEC);
|
1994-01-05 01:03:02 +00:00
|
|
|
|
2000-11-26 10:05:06 +00:00
|
|
|
snprintf(fmt, sizeof(fmt),
|
|
|
|
"#!/bin/sh\n# atrun uid=%%ld gid=%%ld\n# mail %%%ds %%d",
|
2009-12-25 10:30:54 +00:00
|
|
|
MAXLOGNAME - 1);
|
2007-06-15 10:34:36 +00: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);
|
|
|
|
|
1995-08-21 12:34:18 +00:00
|
|
|
mailname = mailbuf;
|
2007-06-15 10:34:36 +00:00
|
|
|
|
|
|
|
if (nuid != uid)
|
|
|
|
perrx("Job %s - userid %ld does not match file uid %lu",
|
1998-08-02 16:44:18 +00:00
|
|
|
filename, nuid, (unsigned long)uid);
|
2007-06-15 10:34:36 +00:00
|
|
|
|
|
|
|
if (ngid != gid)
|
|
|
|
perrx("Job %s - groupid %ld does not match file gid %lu",
|
1998-08-02 16:44:18 +00:00
|
|
|
filename, ngid, (unsigned long)gid);
|
2007-06-15 10:34:36 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
fclose(stream);
|
2007-06-15 10:34:36 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
if (chdir(ATSPOOL_DIR) < 0)
|
2007-06-15 10:34:36 +00:00
|
|
|
perr("cannot chdir to %s", ATSPOOL_DIR);
|
1995-08-21 12:34:18 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
/* Create a file to hold the output of the job we are about to run.
|
|
|
|
* Write the mail header.
|
1995-08-21 12:34:18 +00:00
|
|
|
*/
|
1995-04-12 02:52:21 +00:00
|
|
|
if((fd_out=open(filename,
|
|
|
|
O_WRONLY | O_CREAT | O_EXCL, S_IWUSR | S_IRUSR)) < 0)
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("cannot create output file");
|
1995-04-12 02:52:21 +00:00
|
|
|
|
|
|
|
write_string(fd_out, "Subject: Output from your job ");
|
|
|
|
write_string(fd_out, filename);
|
|
|
|
write_string(fd_out, "\n\n");
|
|
|
|
fstat(fd_out, &buf);
|
|
|
|
size = buf.st_size;
|
|
|
|
|
|
|
|
close(STDIN_FILENO);
|
|
|
|
close(STDOUT_FILENO);
|
|
|
|
close(STDERR_FILENO);
|
1995-08-21 12:34:18 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
pid = fork();
|
|
|
|
if (pid < 0)
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("error in fork");
|
1995-04-12 02:52:21 +00:00
|
|
|
|
|
|
|
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.
|
1994-01-05 01:03:02 +00:00
|
|
|
*/
|
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
if (lseek(fd_in, (off_t) 0, SEEK_SET) < 0)
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("error in lseek");
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
if (dup(fd_in) != STDIN_FILENO)
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("error in I/O redirection");
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
if (dup(fd_out) != STDOUT_FILENO)
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("error in I/O redirection");
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
if (dup(fd_out) != STDERR_FILENO)
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("error in I/O redirection");
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
close(fd_in);
|
|
|
|
close(fd_out);
|
|
|
|
if (chdir(ATJOB_DIR) < 0)
|
2007-06-15 10:34:36 +00:00
|
|
|
perr("cannot chdir to %s", ATJOB_DIR);
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
queue = *filename;
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
PRIV_START
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
nice(tolower(queue) - 'a');
|
1995-08-21 12:34:18 +00:00
|
|
|
|
2007-06-14 22:16:21 +00:00
|
|
|
#ifdef LOGIN_CAP
|
|
|
|
/*
|
|
|
|
* For simplicity and safety, set all aspects of the user context
|
|
|
|
* except for a selected subset: Don't set priority, which was
|
|
|
|
* set based on the queue file name according to the tradition.
|
|
|
|
* Don't bother to set environment, including path vars, either
|
|
|
|
* because it will be discarded anyway. Although the job file
|
|
|
|
* should set umask, preset it here just in case.
|
|
|
|
*/
|
|
|
|
if (setusercontext(NULL, pentry, uid, LOGIN_SETALL &
|
|
|
|
~(LOGIN_SETPRIORITY | LOGIN_SETPATH | LOGIN_SETENV)) != 0)
|
|
|
|
exit(EXIT_FAILURE); /* setusercontext() logged the error */
|
|
|
|
#else /* LOGIN_CAP */
|
1995-04-12 02:52:21 +00:00
|
|
|
if (initgroups(pentry->pw_name,pentry->pw_gid))
|
2007-06-14 15:18:44 +00:00
|
|
|
perr("cannot init group access list");
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1997-09-08 23:39:48 +00:00
|
|
|
if (setgid(gid) < 0 || setegid(pentry->pw_gid) < 0)
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("cannot change group");
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1997-09-08 23:39:48 +00:00
|
|
|
if (setlogin(pentry->pw_name))
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("cannot set login name");
|
1997-09-08 23:39:48 +00:00
|
|
|
|
|
|
|
if (setuid(uid) < 0 || seteuid(uid) < 0)
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("cannot set user id");
|
2007-06-14 22:16:21 +00:00
|
|
|
#endif /* LOGIN_CAP */
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1997-09-08 23:39:48 +00:00
|
|
|
if (chdir(pentry->pw_dir))
|
|
|
|
chdir("/");
|
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
if(execle("/bin/sh","sh",(char *) NULL, nenvp) != 0)
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("exec failed for /bin/sh");
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
PRIV_END
|
|
|
|
}
|
|
|
|
/* We're the parent. Let's wait.
|
|
|
|
*/
|
|
|
|
close(fd_in);
|
|
|
|
close(fd_out);
|
|
|
|
waitpid(pid, (int *) NULL, 0);
|
|
|
|
|
|
|
|
/* Send mail. Unlink the output file first, so it is deleted after
|
|
|
|
* the run.
|
|
|
|
*/
|
|
|
|
stat(filename, &buf);
|
|
|
|
if (open(filename, O_RDONLY) != STDIN_FILENO)
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("open of jobfile failed");
|
1995-04-12 02:52:21 +00:00
|
|
|
|
|
|
|
unlink(filename);
|
|
|
|
if ((buf.st_size != size) || send_mail)
|
1995-08-21 12:34:18 +00:00
|
|
|
{
|
|
|
|
PRIV_START
|
|
|
|
|
2007-06-14 22:16:21 +00:00
|
|
|
#ifdef LOGIN_CAP
|
|
|
|
/*
|
|
|
|
* This time set full context to run the mailer.
|
|
|
|
*/
|
|
|
|
if (setusercontext(NULL, pentry, uid, LOGIN_SETALL) != 0)
|
|
|
|
exit(EXIT_FAILURE); /* setusercontext() logged the error */
|
|
|
|
#else /* LOGIN_CAP */
|
1995-08-21 12:34:18 +00:00
|
|
|
if (initgroups(pentry->pw_name,pentry->pw_gid))
|
2007-06-14 21:24:05 +00:00
|
|
|
perr("cannot init group access list");
|
1995-08-21 12:34:18 +00:00
|
|
|
|
1997-09-08 23:39:48 +00:00
|
|
|
if (setgid(gid) < 0 || setegid(pentry->pw_gid) < 0)
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("cannot change group");
|
1995-08-21 12:34:18 +00:00
|
|
|
|
1997-09-08 23:39:48 +00:00
|
|
|
if (setlogin(pentry->pw_name))
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("cannot set login name");
|
1997-09-08 23:39:48 +00:00
|
|
|
|
|
|
|
if (setuid(uid) < 0 || seteuid(uid) < 0)
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("cannot set user id");
|
2007-06-14 22:16:21 +00:00
|
|
|
#endif /* LOGIN_CAP */
|
1995-08-21 12:34:18 +00:00
|
|
|
|
1997-09-08 23:39:48 +00:00
|
|
|
if (chdir(pentry->pw_dir))
|
|
|
|
chdir("/");
|
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
execl(_PATH_SENDMAIL, "sendmail", "-F", "Atrun Service",
|
1995-04-12 19:21:43 +00:00
|
|
|
"-odi", "-oem",
|
1995-04-12 02:52:21 +00:00
|
|
|
mailname, (char *) NULL);
|
|
|
|
#else
|
|
|
|
execl(MAIL_CMD, MAIL_CMD, mailname, (char *) NULL);
|
|
|
|
#endif
|
1997-11-20 07:21:55 +00:00
|
|
|
perr("exec failed for mail command");
|
1995-08-21 12:34:18 +00:00
|
|
|
|
|
|
|
PRIV_END
|
1995-04-12 02:52:21 +00:00
|
|
|
}
|
|
|
|
exit(EXIT_SUCCESS);
|
1994-01-05 01:03:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Global functions */
|
|
|
|
|
1995-08-21 12:34:18 +00:00
|
|
|
/* Needed in gloadavg.c */
|
|
|
|
void
|
2007-06-15 10:10:40 +00:00
|
|
|
perr(const char *fmt, ...)
|
1995-08-21 12:34:18 +00:00
|
|
|
{
|
2007-06-15 10:10:40 +00:00
|
|
|
const char * const fmtadd = ": %m";
|
|
|
|
char nfmt[strlen(fmt) + strlen(fmtadd) + 1];
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
1995-08-21 12:34:18 +00:00
|
|
|
if (debug)
|
|
|
|
{
|
2007-06-15 10:10:40 +00:00
|
|
|
vwarn(fmt, ap);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf(nfmt, sizeof(nfmt), "%s%s", fmt, fmtadd);
|
|
|
|
vsyslog(LOG_ERR, nfmt, ap);
|
1995-08-21 12:34:18 +00:00
|
|
|
}
|
2007-06-15 10:10:40 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
perrx(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
if (debug)
|
|
|
|
vwarnx(fmt, ap);
|
1995-08-21 12:34:18 +00:00
|
|
|
else
|
2007-06-15 10:10:40 +00:00
|
|
|
vsyslog(LOG_ERR, fmt, ap);
|
|
|
|
va_end(ap);
|
1995-08-21 12:34:18 +00:00
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
1994-01-05 01:03:02 +00:00
|
|
|
int
|
1995-04-12 02:52:21 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-01-05 01:03:02 +00:00
|
|
|
{
|
1995-04-12 02:52:21 +00:00
|
|
|
/* Browse through ATJOB_DIR, 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;
|
1995-08-21 12:34:18 +00:00
|
|
|
unsigned long jobno;
|
1995-04-12 02:52:21 +00:00
|
|
|
char queue;
|
|
|
|
time_t now, run_time;
|
|
|
|
char batch_name[] = "Z2345678901234";
|
|
|
|
uid_t batch_uid;
|
|
|
|
gid_t batch_gid;
|
|
|
|
int c;
|
|
|
|
int run_batch;
|
2012-10-30 19:46:00 +00:00
|
|
|
#ifdef __FreeBSD__
|
2016-01-30 17:54:18 +00:00
|
|
|
size_t ncpusz;
|
2012-10-30 19:46:00 +00:00
|
|
|
double load_avg = -1;
|
2016-01-30 17:54:18 +00:00
|
|
|
int ncpu;
|
2012-10-30 19:46:00 +00:00
|
|
|
#else
|
1995-04-12 02:52:21 +00:00
|
|
|
double load_avg = LOADAVG_MX;
|
2012-10-30 19:46:00 +00:00
|
|
|
#endif
|
1995-04-12 02:52:21 +00:00
|
|
|
|
|
|
|
/* We don't need root privileges all the time; running under uid and gid daemon
|
|
|
|
* is fine.
|
|
|
|
*/
|
1994-01-05 01:03:02 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
RELINQUISH_PRIVS_ROOT(DAEMON_UID, DAEMON_GID)
|
|
|
|
|
2007-06-15 12:02:16 +00:00
|
|
|
openlog(atrun, LOG_PID, LOG_CRON);
|
1995-04-12 02:52:21 +00:00
|
|
|
|
|
|
|
opterr = 0;
|
1997-03-28 15:48:21 +00:00
|
|
|
while((c=getopt(argc, argv, "dl:"))!= -1)
|
1995-04-12 02:52:21 +00:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
1995-08-21 12:34:18 +00:00
|
|
|
case 'l':
|
1995-04-12 02:52:21 +00:00
|
|
|
if (sscanf(optarg, "%lf", &load_avg) != 1)
|
|
|
|
perr("garbled option -l");
|
2012-10-30 19:46:00 +00:00
|
|
|
#ifndef __FreeBSD__
|
1995-04-12 02:52:21 +00:00
|
|
|
if (load_avg <= 0.)
|
|
|
|
load_avg = LOADAVG_MX;
|
2012-10-30 19:46:00 +00:00
|
|
|
#endif
|
1995-04-12 02:52:21 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
debug ++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
1997-11-20 07:21:55 +00:00
|
|
|
usage();
|
1995-04-12 02:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chdir(ATJOB_DIR) != 0)
|
2007-06-15 10:34:36 +00:00
|
|
|
perr("cannot change to %s", ATJOB_DIR);
|
1995-04-12 02:52:21 +00:00
|
|
|
|
2012-10-30 19:46:00 +00:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
if (load_avg <= 0.) {
|
|
|
|
ncpusz = sizeof(size_t);
|
|
|
|
if (sysctlbyname("hw.ncpu", &ncpu, &ncpusz, NULL, 0) < 0)
|
|
|
|
ncpu = 1;
|
|
|
|
load_avg = LOADAVG_MX * ncpu;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1995-04-12 02:52:21 +00: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.
|
|
|
|
*
|
|
|
|
* Also, pick the oldest batch job to run, at most one per invocation of
|
|
|
|
* atrun.
|
|
|
|
*/
|
|
|
|
if ((spool = opendir(".")) == NULL)
|
2007-06-15 10:34:36 +00:00
|
|
|
perr("cannot read %s", ATJOB_DIR);
|
1995-04-12 02:52:21 +00:00
|
|
|
|
2013-06-11 18:43:27 +00:00
|
|
|
if (flock(dirfd(spool), LOCK_EX) == -1)
|
|
|
|
perr("cannot lock %s", ATJOB_DIR);
|
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
now = time(NULL);
|
|
|
|
run_batch = 0;
|
|
|
|
batch_uid = (uid_t) -1;
|
|
|
|
batch_gid = (gid_t) -1;
|
|
|
|
|
1995-08-21 12:34:18 +00:00
|
|
|
while ((dirent = readdir(spool)) != NULL) {
|
1995-04-12 02:52:21 +00:00
|
|
|
if (stat(dirent->d_name,&buf) != 0)
|
2007-06-15 10:34:36 +00:00
|
|
|
perr("cannot stat in %s", ATJOB_DIR);
|
1995-04-12 02:52:21 +00:00
|
|
|
|
|
|
|
/* We don't want directories
|
1994-01-05 01:03:02 +00:00
|
|
|
*/
|
1995-08-21 12:34:18 +00:00
|
|
|
if (!S_ISREG(buf.st_mode))
|
1995-04-12 02:52:21 +00:00
|
|
|
continue;
|
|
|
|
|
1995-08-21 12:34:18 +00:00
|
|
|
if (sscanf(dirent->d_name,"%c%5lx%8lx",&queue,&jobno,&ctm) != 3)
|
1995-04-12 02:52:21 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
run_time = (time_t) ctm*60;
|
|
|
|
|
1995-08-21 12:34:18 +00:00
|
|
|
if ((S_IXUSR & buf.st_mode) && (run_time <=now)) {
|
|
|
|
if (isupper(queue) && (strcmp(batch_name,dirent->d_name) > 0)) {
|
1995-04-12 02:52:21 +00:00
|
|
|
run_batch = 1;
|
2007-06-14 14:44:04 +00:00
|
|
|
strlcpy(batch_name, dirent->d_name, sizeof(batch_name));
|
1995-04-12 02:52:21 +00:00
|
|
|
batch_uid = buf.st_uid;
|
|
|
|
batch_gid = buf.st_gid;
|
|
|
|
}
|
1995-08-21 12:34:18 +00:00
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
/* The file is executable and old enough
|
|
|
|
*/
|
|
|
|
if (islower(queue))
|
|
|
|
run_file(dirent->d_name, buf.st_uid, buf.st_gid);
|
1994-01-05 01:03:02 +00:00
|
|
|
}
|
1995-04-12 02:52:21 +00:00
|
|
|
/* Delete older files
|
|
|
|
*/
|
|
|
|
if ((run_time < now) && !(S_IXUSR & buf.st_mode) && (S_IRUSR & buf.st_mode))
|
|
|
|
unlink(dirent->d_name);
|
|
|
|
}
|
|
|
|
/* run the single batch file, if any
|
|
|
|
*/
|
1995-08-21 12:34:18 +00:00
|
|
|
if (run_batch && (gloadavg() < load_avg))
|
|
|
|
run_file(batch_name, batch_uid, batch_gid);
|
1995-04-12 02:52:21 +00:00
|
|
|
|
2014-04-17 16:00:26 +00:00
|
|
|
if (flock(dirfd(spool), LOCK_UN) == -1)
|
|
|
|
perr("cannot unlock %s", ATJOB_DIR);
|
|
|
|
|
|
|
|
if (closedir(spool) == -1)
|
|
|
|
perr("cannot closedir %s", ATJOB_DIR);
|
|
|
|
|
1995-04-12 02:52:21 +00:00
|
|
|
closelog();
|
|
|
|
exit(EXIT_SUCCESS);
|
1994-01-05 01:03:02 +00:00
|
|
|
}
|
1997-11-20 07:21:55 +00:00
|
|
|
|
|
|
|
static void
|
2002-02-03 15:53:02 +00:00
|
|
|
usage(void)
|
1997-11-20 07:21:55 +00:00
|
|
|
{
|
|
|
|
if (debug)
|
|
|
|
fprintf(stderr, "usage: atrun [-l load_avg] [-d]\n");
|
|
|
|
else
|
|
|
|
syslog(LOG_ERR, "usage: atrun [-l load_avg] [-d]");
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|