2004-10-30 20:55:06 +04:00
|
|
|
/* $NetBSD: at.c,v 1.21 2004/10/30 16:55:06 dsl Exp $ */
|
1995-03-25 21:04:51 +03:00
|
|
|
|
1993-12-05 14:34:40 +03:00
|
|
|
/*
|
1998-06-28 01:15:07 +04:00
|
|
|
* at.c : Put file into atrun queue
|
|
|
|
* Copyright (C) 1993, 1994 Thomas Koenig
|
1993-12-05 14:34:40 +03:00
|
|
|
*
|
1998-06-28 01:15:07 +04:00
|
|
|
* Atrun & Atq modifications
|
|
|
|
* Copyright (C) 1993 David Parsons
|
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:07 +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 */
|
|
|
|
#include <sys/types.h>
|
1998-06-28 01:15:07 +04:00
|
|
|
#include <sys/param.h>
|
1993-12-05 14:34:40 +03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
2000-10-04 23:14:53 +04:00
|
|
|
#include <err.h>
|
1993-12-05 14:34:40 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
1998-06-28 01:15:07 +04:00
|
|
|
#include <locale.h>
|
|
|
|
|
1993-12-05 14:34:40 +03:00
|
|
|
/* Local headers */
|
|
|
|
#include "at.h"
|
|
|
|
#include "panic.h"
|
|
|
|
#include "parsetime.h"
|
1998-06-28 01:15:07 +04:00
|
|
|
#include "perm.h"
|
1993-12-05 14:34:40 +03:00
|
|
|
#include "pathnames.h"
|
2000-10-15 18:51:14 +04:00
|
|
|
#include "stime.h"
|
1993-12-05 14:34:40 +03:00
|
|
|
#define MAIN
|
|
|
|
#include "privs.h"
|
|
|
|
|
|
|
|
/* Macros */
|
|
|
|
#define ALARMC 10 /* Number of seconds to wait for timeout */
|
|
|
|
|
|
|
|
#define TIMESIZE 50
|
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
enum { ATQ, ATRM, AT, BATCH, CAT }; /* what program we want to run */
|
|
|
|
|
1993-12-05 14:34:40 +03:00
|
|
|
/* File scope variables */
|
1995-03-25 21:04:51 +03:00
|
|
|
#ifndef lint
|
1998-06-28 01:15:07 +04:00
|
|
|
#if 0
|
|
|
|
static char rcsid[] = "$OpenBSD: at.c,v 1.15 1998/06/03 16:20:26 deraadt Exp $";
|
|
|
|
#else
|
2004-10-30 20:55:06 +04:00
|
|
|
__RCSID("$NetBSD: at.c,v 1.21 2004/10/30 16:55:06 dsl Exp $");
|
1998-06-28 01:15:07 +04:00
|
|
|
#endif
|
1995-03-25 21:04:51 +03:00
|
|
|
#endif
|
|
|
|
|
1993-12-05 14:34:40 +03:00
|
|
|
char *no_export[] =
|
|
|
|
{
|
|
|
|
"TERM", "TERMCAP", "DISPLAY", "_"
|
|
|
|
};
|
1998-03-30 06:19:02 +04:00
|
|
|
static int send_mail = 0;
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
/* External variables */
|
1998-06-28 01:15:07 +04:00
|
|
|
|
1993-12-05 14:34:40 +03:00
|
|
|
extern char **environ;
|
|
|
|
int fcreated;
|
|
|
|
char *namep;
|
|
|
|
char atfile[FILENAME_MAX];
|
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
char *atinput = (char *)0; /* where to get input from */
|
2004-10-30 20:55:06 +04:00
|
|
|
unsigned char atqueue = 0; /* which queue to examine for jobs (atq) */
|
1993-12-05 14:34:40 +03:00
|
|
|
char atverify = 0; /* verify time instead of queuing job */
|
|
|
|
|
|
|
|
/* Function declarations */
|
1998-06-28 01:15:07 +04:00
|
|
|
|
2000-10-04 23:24:59 +04:00
|
|
|
static void sigc (int);
|
|
|
|
static void alarmc (int);
|
|
|
|
static char *cwdname (void);
|
|
|
|
static int nextjob (void);
|
2004-10-30 20:55:06 +04:00
|
|
|
static void writefile (time_t, unsigned char);
|
2000-10-04 23:24:59 +04:00
|
|
|
static void list_jobs (void);
|
|
|
|
static void process_jobs (int, char **, int);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
/* Signal catching functions */
|
|
|
|
|
2000-06-25 17:35:48 +04:00
|
|
|
/*ARGSUSED*/
|
1993-12-05 14:34:40 +03:00
|
|
|
static void
|
2000-10-04 23:24:59 +04:00
|
|
|
sigc(int signo)
|
1993-12-05 14:34:40 +03:00
|
|
|
{
|
1998-06-28 01:15:07 +04:00
|
|
|
/* If the user presses ^C, remove the spool file and exit. */
|
1993-12-05 14:34:40 +03:00
|
|
|
if (fcreated) {
|
|
|
|
PRIV_START
|
1998-06-28 01:15:07 +04:00
|
|
|
(void)unlink(atfile);
|
1993-12-05 14:34:40 +03:00
|
|
|
PRIV_END
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2000-06-25 17:35:48 +04:00
|
|
|
/*ARGSUSED*/
|
1993-12-05 14:34:40 +03:00
|
|
|
static void
|
2000-10-04 23:24:59 +04:00
|
|
|
alarmc(int signo)
|
1993-12-05 14:34:40 +03:00
|
|
|
{
|
1998-06-28 01:15:07 +04:00
|
|
|
/* Time out after some seconds. */
|
1993-12-05 14:34:40 +03:00
|
|
|
panic("File locking timed out");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Local functions */
|
|
|
|
|
|
|
|
static char *
|
2000-10-04 23:24:59 +04:00
|
|
|
cwdname(void)
|
1993-12-05 14:34:40 +03:00
|
|
|
{
|
1998-06-28 01:15:07 +04:00
|
|
|
/*
|
|
|
|
* Read in the current directory; the name will be overwritten on
|
|
|
|
* subsequent calls.
|
|
|
|
*/
|
|
|
|
static char path[MAXPATHLEN];
|
1993-12-05 14:34:40 +03:00
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
return (getcwd(path, sizeof(path)));
|
|
|
|
}
|
1993-12-05 14:34:40 +03:00
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
static int
|
2000-10-04 23:24:59 +04:00
|
|
|
nextjob(void)
|
1998-06-28 01:15:07 +04:00
|
|
|
{
|
|
|
|
int jobno;
|
|
|
|
FILE *fid;
|
|
|
|
|
|
|
|
if ((fid = fopen(_PATH_SEQFILE, "r+")) != NULL) {
|
|
|
|
if (fscanf(fid, "%5x", &jobno) == 1) {
|
|
|
|
(void)rewind(fid);
|
|
|
|
jobno = (1+jobno) % 0xfffff; /* 2^20 jobs enough? */
|
|
|
|
(void)fprintf(fid, "%05x\n", jobno);
|
|
|
|
} else
|
|
|
|
jobno = EOF;
|
|
|
|
(void)fclose(fid);
|
|
|
|
return (jobno);
|
|
|
|
} else if ((fid = fopen(_PATH_SEQFILE, "w")) != NULL) {
|
|
|
|
(void)fprintf(fid, "%05x\n", jobno = 1);
|
|
|
|
(void)fclose(fid);
|
|
|
|
return (1);
|
1993-12-05 14:34:40 +03:00
|
|
|
}
|
1998-06-28 01:15:07 +04:00
|
|
|
return (EOF);
|
1993-12-05 14:34:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-10-30 20:55:06 +04:00
|
|
|
writefile(time_t runtimer, unsigned char queue)
|
1993-12-05 14:34:40 +03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This does most of the work if at or batch are invoked for
|
|
|
|
* writing a job.
|
|
|
|
*/
|
1998-06-28 01:15:07 +04:00
|
|
|
int jobno;
|
1998-07-27 11:41:31 +04:00
|
|
|
char *ap, *ppos;
|
|
|
|
const char *mailname;
|
1993-12-05 14:34:40 +03:00
|
|
|
struct passwd *pass_entry;
|
|
|
|
struct stat statbuf;
|
|
|
|
int fdes, lockdes, fd2;
|
|
|
|
FILE *fp, *fpin;
|
|
|
|
struct sigaction act;
|
|
|
|
char **atenv;
|
|
|
|
int ch;
|
|
|
|
mode_t cmask;
|
|
|
|
struct flock lock;
|
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
(void)setlocale(LC_TIME, "");
|
|
|
|
|
1993-12-05 14:34:40 +03:00
|
|
|
/*
|
|
|
|
* Install the signal handler for SIGINT; terminate after removing the
|
|
|
|
* spool file if necessary
|
|
|
|
*/
|
1998-06-28 01:15:07 +04:00
|
|
|
memset(&act, 0, sizeof act);
|
1993-12-05 14:34:40 +03:00
|
|
|
act.sa_handler = sigc;
|
|
|
|
sigemptyset(&(act.sa_mask));
|
|
|
|
act.sa_flags = 0;
|
|
|
|
|
|
|
|
sigaction(SIGINT, &act, NULL);
|
|
|
|
|
2002-11-16 07:31:15 +03:00
|
|
|
(void)strlcpy(atfile, _PATH_ATJOBS, sizeof(atfile));
|
1998-06-28 01:15:07 +04:00
|
|
|
ppos = atfile + strlen(atfile);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop over all possible file names for running something at this
|
1998-06-28 01:15:07 +04:00
|
|
|
* particular time, see if a file is there; the first empty slot at
|
|
|
|
* any particular time is used. Lock the file _PATH_LOCKFILE first
|
|
|
|
* to make sure we're alone when doing this.
|
1993-12-05 14:34:40 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
PRIV_START
|
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
if ((lockdes = open(_PATH_LOCKFILE, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR)) < 0)
|
1993-12-05 14:34:40 +03:00
|
|
|
perr2("Cannot open lockfile ", _PATH_LOCKFILE);
|
|
|
|
|
1993-12-06 07:10:42 +03:00
|
|
|
lock.l_type = F_WRLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
lock.l_start = 0;
|
|
|
|
lock.l_len = 0;
|
|
|
|
|
1993-12-05 14:34:40 +03:00
|
|
|
act.sa_handler = alarmc;
|
|
|
|
sigemptyset(&(act.sa_mask));
|
|
|
|
act.sa_flags = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set an alarm so a timeout occurs after ALARMC seconds, in case
|
|
|
|
* something is seriously broken.
|
|
|
|
*/
|
|
|
|
sigaction(SIGALRM, &act, NULL);
|
|
|
|
alarm(ALARMC);
|
|
|
|
fcntl(lockdes, F_SETLKW, &lock);
|
|
|
|
alarm(0);
|
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
if ((jobno = nextjob()) == EOF)
|
|
|
|
perr("Cannot generate job number");
|
|
|
|
|
|
|
|
(void)snprintf(ppos, sizeof(atfile) - (ppos - atfile),
|
|
|
|
"%c%5x%8lx", queue, jobno, (unsigned long) (runtimer/60));
|
1993-12-05 14:34:40 +03:00
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
for (ap = ppos; *ap != '\0'; ap++)
|
|
|
|
if (*ap == ' ')
|
|
|
|
*ap = '0';
|
|
|
|
|
|
|
|
if (stat(atfile, &statbuf) != 0)
|
|
|
|
if (errno != ENOENT)
|
|
|
|
perr2("Cannot access ", _PATH_ATJOBS);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the file. The x bit is only going to be set after it has
|
|
|
|
* been completely written out, to make sure it is not executed in
|
|
|
|
* the meantime. To make sure they do not get deleted, turn off
|
|
|
|
* their r bit. Yes, this is a kluge.
|
|
|
|
*/
|
|
|
|
cmask = umask(S_IRUSR | S_IWUSR | S_IXUSR);
|
1998-06-28 01:15:07 +04:00
|
|
|
if ((fdes = open(atfile, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR)) == -1)
|
1993-12-05 14:34:40 +03:00
|
|
|
perr("Cannot create atjob file");
|
|
|
|
|
|
|
|
if ((fd2 = dup(fdes)) < 0)
|
|
|
|
perr("Error in dup() of job file");
|
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
if (fchown(fd2, real_uid, real_gid) != 0)
|
1993-12-05 14:34:40 +03:00
|
|
|
perr("Cannot give away file");
|
|
|
|
|
|
|
|
PRIV_END
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We've successfully created the file; let's set the flag so it
|
|
|
|
* gets removed in case of an interrupt or error.
|
|
|
|
*/
|
|
|
|
fcreated = 1;
|
|
|
|
|
|
|
|
/* Now we can release the lock, so other people can access it */
|
|
|
|
lock.l_type = F_UNLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
lock.l_start = 0;
|
|
|
|
lock.l_len = 0;
|
1998-06-28 01:15:07 +04:00
|
|
|
(void)fcntl(lockdes, F_SETLKW, &lock);
|
|
|
|
(void)close(lockdes);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
if ((fp = fdopen(fdes, "w")) == NULL)
|
|
|
|
panic("Cannot reopen atjob file");
|
|
|
|
|
|
|
|
/*
|
1998-06-28 01:15:07 +04:00
|
|
|
* Get the userid to mail to, first by trying getlogin(), which reads
|
|
|
|
* /etc/utmp, then from $LOGNAME or $USER, finally from getpwuid().
|
1993-12-05 14:34:40 +03:00
|
|
|
*/
|
|
|
|
mailname = getlogin();
|
1998-06-28 01:15:07 +04:00
|
|
|
if (mailname == NULL && (mailname = getenv("LOGNAME")) == NULL)
|
|
|
|
mailname = getenv("USER");
|
1993-12-05 14:34:40 +03:00
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
if ((mailname == NULL) || (mailname[0] == '\0') ||
|
2000-06-25 17:35:48 +04:00
|
|
|
(strlen(mailname) > LOGIN_NAME_MAX) || (getpwnam(mailname) == NULL)) {
|
1998-06-28 01:15:07 +04:00
|
|
|
pass_entry = getpwuid(real_uid);
|
1993-12-05 14:34:40 +03:00
|
|
|
if (pass_entry != NULL)
|
|
|
|
mailname = pass_entry->pw_name;
|
|
|
|
}
|
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
if (atinput != NULL) {
|
1993-12-05 14:34:40 +03:00
|
|
|
fpin = freopen(atinput, "r", stdin);
|
|
|
|
if (fpin == NULL)
|
|
|
|
perr("Cannot open input file");
|
|
|
|
}
|
2000-06-25 17:35:48 +04:00
|
|
|
(void)fprintf(fp, "#!/bin/sh\n# atrun uid=%u gid=%u\n# mail %s %d\n",
|
|
|
|
real_uid, real_gid, mailname, send_mail);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
/* Write out the umask at the time of invocation */
|
1998-06-28 01:15:07 +04:00
|
|
|
(void)fprintf(fp, "umask %o\n", cmask);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Write out the environment. Anything that may look like a special
|
|
|
|
* character to the shell is quoted, except for \n, which is done
|
|
|
|
* with a pair of "'s. Dont't export the no_export list (such as
|
|
|
|
* TERM or DISPLAY) because we don't want these.
|
|
|
|
*/
|
|
|
|
for (atenv = environ; *atenv != NULL; atenv++) {
|
|
|
|
int export = 1;
|
|
|
|
char *eqp;
|
|
|
|
|
|
|
|
eqp = strchr(*atenv, '=');
|
2000-04-23 22:11:21 +04:00
|
|
|
if (eqp == NULL)
|
1993-12-05 14:34:40 +03:00
|
|
|
eqp = *atenv;
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0;i < sizeof(no_export) /
|
|
|
|
sizeof(no_export[0]); i++) {
|
|
|
|
export = export
|
|
|
|
&& (strncmp(*atenv, no_export[i],
|
|
|
|
(size_t) (eqp - *atenv)) != 0);
|
|
|
|
}
|
|
|
|
eqp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (export) {
|
1998-06-28 01:15:07 +04:00
|
|
|
(void)fwrite(*atenv, sizeof(char), eqp - *atenv, fp);
|
1993-12-05 14:34:40 +03:00
|
|
|
for (ap = eqp; *ap != '\0'; ap++) {
|
|
|
|
if (*ap == '\n')
|
1998-06-28 01:15:07 +04:00
|
|
|
(void)fprintf(fp, "\"\n\"");
|
1993-12-05 14:34:40 +03:00
|
|
|
else {
|
2004-10-30 20:55:06 +04:00
|
|
|
if (!isalnum((unsigned char)*ap)) {
|
1998-06-28 01:15:07 +04:00
|
|
|
switch (*ap) {
|
|
|
|
case '%': case '/': case '{':
|
|
|
|
case '[': case ']': case '=':
|
|
|
|
case '}': case '@': case '+':
|
|
|
|
case '#': case ',': case '.':
|
|
|
|
case ':': case '-': case '_':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
(void)fputc('\\', fp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(void)fputc(*ap, fp);
|
1993-12-05 14:34:40 +03:00
|
|
|
}
|
|
|
|
}
|
1998-06-28 01:15:07 +04:00
|
|
|
(void)fputs("; export ", fp);
|
|
|
|
(void)fwrite(*atenv, sizeof(char), eqp - *atenv - 1, fp);
|
|
|
|
(void)fputc('\n', fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Cd to the directory at the time and write out all the
|
|
|
|
* commands the user supplies from stdin.
|
|
|
|
*/
|
|
|
|
(void)fputs("cd ", fp);
|
|
|
|
for (ap = cwdname(); *ap != '\0'; ap++) {
|
|
|
|
if (*ap == '\n')
|
2000-06-25 17:35:48 +04:00
|
|
|
(void)fprintf(fp, "\"\n\"");
|
1998-06-28 01:15:07 +04:00
|
|
|
else {
|
2004-10-30 20:55:06 +04:00
|
|
|
if (*ap != '/' && !isalnum((unsigned char)*ap))
|
1998-06-28 01:15:07 +04:00
|
|
|
(void)fputc('\\', fp);
|
1993-12-05 14:34:40 +03:00
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
(void)fputc(*ap, fp);
|
1993-12-05 14:34:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
1998-06-28 01:15:07 +04:00
|
|
|
* Test cd's exit status: die if the original directory has been
|
|
|
|
* removed, become unreadable or whatever.
|
1993-12-05 14:34:40 +03:00
|
|
|
*/
|
1998-06-28 01:15:07 +04:00
|
|
|
(void)fprintf(fp, " || {\n\t echo 'Execution directory inaccessible' >&2\n\t exit 1\n}\n");
|
1993-12-05 14:34:40 +03:00
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
if ((ch = getchar()) == EOF)
|
|
|
|
panic("Input error");
|
1993-12-05 14:34:40 +03:00
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
do {
|
|
|
|
(void)fputc(ch, fp);
|
|
|
|
} while ((ch = getchar()) != EOF);
|
|
|
|
|
|
|
|
(void)fprintf(fp, "\n");
|
1993-12-05 14:34:40 +03:00
|
|
|
if (ferror(fp))
|
|
|
|
panic("Output error");
|
|
|
|
|
|
|
|
if (ferror(stdin))
|
|
|
|
panic("Input error");
|
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
(void)fclose(fp);
|
|
|
|
|
|
|
|
|
|
|
|
PRIV_START
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the x bit so that we're ready to start executing
|
|
|
|
*/
|
|
|
|
if (fchmod(fd2, S_IRUSR | S_IWUSR | S_IXUSR) < 0)
|
|
|
|
perr("Cannot give away file");
|
|
|
|
|
1998-06-26 12:28:20 +04:00
|
|
|
PRIV_END
|
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
(void)close(fd2);
|
|
|
|
(void)fprintf(stderr, "Job %d will be executed using /bin/sh\n", jobno);
|
1993-12-05 14:34:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-10-04 23:24:59 +04:00
|
|
|
list_jobs(void)
|
1993-12-05 14:34:40 +03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* List all a user's jobs in the queue, by looping through
|
|
|
|
* _PATH_ATJOBS, or everybody's if we are root
|
|
|
|
*/
|
|
|
|
struct passwd *pw;
|
|
|
|
DIR *spool;
|
|
|
|
struct dirent *dirent;
|
|
|
|
struct stat buf;
|
|
|
|
struct tm runtime;
|
|
|
|
unsigned long ctm;
|
2004-10-30 20:55:06 +04:00
|
|
|
unsigned char queue;
|
1998-06-28 01:15:07 +04:00
|
|
|
int jobno;
|
1993-12-05 14:34:40 +03:00
|
|
|
time_t runtimer;
|
|
|
|
char timestr[TIMESIZE];
|
|
|
|
int first = 1;
|
|
|
|
|
|
|
|
PRIV_START
|
|
|
|
|
1998-06-26 12:28:20 +04:00
|
|
|
if (chdir(_PATH_ATJOBS) != 0)
|
1993-12-05 14:34:40 +03:00
|
|
|
perr2("Cannot change to ", _PATH_ATJOBS);
|
|
|
|
|
|
|
|
if ((spool = opendir(".")) == NULL)
|
|
|
|
perr2("Cannot open ", _PATH_ATJOBS);
|
|
|
|
|
|
|
|
/* Loop over every file in the directory */
|
|
|
|
while ((dirent = readdir(spool)) != NULL) {
|
|
|
|
if (stat(dirent->d_name, &buf) != 0)
|
|
|
|
perr2("Cannot stat in ", _PATH_ATJOBS);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See it's a regular file and has its x bit turned on and
|
|
|
|
* is the user's
|
|
|
|
*/
|
|
|
|
if (!S_ISREG(buf.st_mode)
|
|
|
|
|| ((buf.st_uid != real_uid) && !(real_uid == 0))
|
|
|
|
|| !(S_IXUSR & buf.st_mode || atverify))
|
|
|
|
continue;
|
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
if (sscanf(dirent->d_name, "%c%5x%8lx", &queue, &jobno, &ctm) != 3)
|
1993-12-05 14:34:40 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (atqueue && (queue != atqueue))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
runtimer = 60 * (time_t) ctm;
|
|
|
|
runtime = *localtime(&runtimer);
|
|
|
|
strftime(timestr, TIMESIZE, "%X %x", &runtime);
|
|
|
|
if (first) {
|
2000-06-25 17:35:48 +04:00
|
|
|
(void)printf("%-*s %-*s %-*s %s\n",
|
|
|
|
(int)strlen(timestr), "Date",
|
|
|
|
LOGIN_NAME_MAX, "Owner",
|
|
|
|
7, "Queue",
|
|
|
|
"Job");
|
1993-12-05 14:34:40 +03:00
|
|
|
first = 0;
|
|
|
|
}
|
|
|
|
pw = getpwuid(buf.st_uid);
|
|
|
|
|
2000-06-25 17:35:48 +04:00
|
|
|
(void)printf("%s %-*s %c%-*s %d\n",
|
1993-12-05 14:34:40 +03:00
|
|
|
timestr,
|
2000-06-25 17:35:48 +04:00
|
|
|
LOGIN_NAME_MAX, pw ? pw->pw_name : "???",
|
1993-12-05 14:34:40 +03:00
|
|
|
queue,
|
2000-06-25 17:35:48 +04:00
|
|
|
6, (S_IXUSR & buf.st_mode) ? "" : "(done)",
|
1998-06-28 01:15:07 +04:00
|
|
|
jobno);
|
1993-12-05 14:34:40 +03:00
|
|
|
}
|
|
|
|
PRIV_END
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-10-04 23:24:59 +04:00
|
|
|
process_jobs(int argc, char **argv, int what)
|
1993-12-05 14:34:40 +03:00
|
|
|
{
|
|
|
|
/* Delete every argument (job - ID) given */
|
|
|
|
int i;
|
|
|
|
struct stat buf;
|
1998-06-28 01:15:07 +04:00
|
|
|
DIR *spool;
|
|
|
|
struct dirent *dirent;
|
|
|
|
unsigned long ctm;
|
2004-10-30 20:55:06 +04:00
|
|
|
unsigned char queue;
|
1998-06-28 01:15:07 +04:00
|
|
|
int jobno;
|
1993-12-05 14:34:40 +03:00
|
|
|
|
|
|
|
PRIV_START
|
|
|
|
|
1998-06-26 12:28:20 +04:00
|
|
|
if (chdir(_PATH_ATJOBS) != 0)
|
1993-12-05 14:34:40 +03:00
|
|
|
perr2("Cannot change to ", _PATH_ATJOBS);
|
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
if ((spool = opendir(".")) == NULL)
|
|
|
|
perr2("Cannot open ", _PATH_ATJOBS);
|
|
|
|
|
|
|
|
PRIV_END
|
|
|
|
|
|
|
|
/* Loop over every file in the directory */
|
|
|
|
while((dirent = readdir(spool)) != NULL) {
|
|
|
|
|
|
|
|
PRIV_START
|
|
|
|
if (stat(dirent->d_name, &buf) != 0)
|
|
|
|
perr2("Cannot stat in ", _PATH_ATJOBS);
|
|
|
|
PRIV_END
|
|
|
|
|
|
|
|
if (sscanf(dirent->d_name, "%c%5x%8lx", &queue, &jobno, &ctm) !=3)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (i = optind; i < argc; i++) {
|
|
|
|
if (atoi(argv[i]) == jobno) {
|
|
|
|
if ((buf.st_uid != real_uid) && !(real_uid == 0)) {
|
2000-10-04 23:14:53 +04:00
|
|
|
errx(EXIT_FAILURE, "%s: Not owner", argv[i]);
|
1998-06-28 01:15:07 +04:00
|
|
|
}
|
|
|
|
switch (what) {
|
|
|
|
case ATRM:
|
|
|
|
PRIV_START
|
|
|
|
|
|
|
|
if (unlink(dirent->d_name) != 0)
|
|
|
|
perr(dirent->d_name);
|
|
|
|
|
|
|
|
PRIV_END
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CAT:
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
PRIV_START
|
|
|
|
|
|
|
|
fp = fopen(dirent->d_name, "r");
|
|
|
|
|
|
|
|
PRIV_END
|
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
perr("Cannot open file");
|
|
|
|
|
|
|
|
while((ch = getc(fp)) != EOF)
|
|
|
|
putchar(ch);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2000-10-04 23:14:53 +04:00
|
|
|
errx(EXIT_FAILURE, "Internal error, process_jobs = %d",
|
|
|
|
what);
|
1998-06-28 01:15:07 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1993-12-05 14:34:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} /* delete_jobs */
|
|
|
|
|
|
|
|
/* Global functions */
|
|
|
|
|
|
|
|
int
|
2000-10-04 23:24:59 +04:00
|
|
|
main(int argc, char **argv)
|
1993-12-05 14:34:40 +03:00
|
|
|
{
|
|
|
|
int c;
|
2004-10-30 20:55:06 +04:00
|
|
|
unsigned char queue = DEFAULT_AT_QUEUE;
|
1998-06-28 01:15:07 +04:00
|
|
|
char queue_set = 0;
|
2000-10-15 18:51:14 +04:00
|
|
|
char time_set = 0;
|
1993-12-05 14:34:40 +03:00
|
|
|
char *pgm;
|
|
|
|
|
2000-10-15 18:51:14 +04:00
|
|
|
int program = AT; /* our default program */
|
|
|
|
char *options = "q:f:t:mvldbrVc"; /* default options for at */
|
1998-06-28 01:15:07 +04:00
|
|
|
int disp_version = 0;
|
1993-12-05 14:34:40 +03:00
|
|
|
time_t timer;
|
|
|
|
|
|
|
|
RELINQUISH_PRIVS
|
|
|
|
|
|
|
|
/* Eat any leading paths */
|
|
|
|
if ((pgm = strrchr(argv[0], '/')) == NULL)
|
|
|
|
pgm = argv[0];
|
|
|
|
else
|
|
|
|
pgm++;
|
|
|
|
|
|
|
|
namep = pgm;
|
|
|
|
|
|
|
|
/* find out what this program is supposed to do */
|
|
|
|
if (strcmp(pgm, "atq") == 0) {
|
|
|
|
program = ATQ;
|
1998-06-28 01:15:07 +04:00
|
|
|
options = "q:vV";
|
1993-12-05 14:34:40 +03:00
|
|
|
} else if (strcmp(pgm, "atrm") == 0) {
|
|
|
|
program = ATRM;
|
1998-06-28 01:15:07 +04:00
|
|
|
options = "V";
|
1993-12-05 14:34:40 +03:00
|
|
|
} else if (strcmp(pgm, "batch") == 0) {
|
|
|
|
program = BATCH;
|
2000-10-15 18:51:14 +04:00
|
|
|
options = "f:q:t:mvV";
|
1993-12-05 14:34:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* process whatever options we can process */
|
|
|
|
opterr = 1;
|
1997-10-18 16:23:21 +04:00
|
|
|
while ((c = getopt(argc, argv, options)) != -1)
|
1993-12-05 14:34:40 +03:00
|
|
|
switch (c) {
|
|
|
|
case 'v': /* verify time settings */
|
|
|
|
atverify = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm': /* send mail when job is complete */
|
|
|
|
send_mail = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
atinput = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'q': /* specify queue */
|
|
|
|
if (strlen(optarg) > 1)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
atqueue = queue = *optarg;
|
1998-06-28 01:15:07 +04:00
|
|
|
if (!(islower(queue) || isupper(queue)))
|
1993-12-05 14:34:40 +03:00
|
|
|
usage();
|
1998-06-28 01:15:07 +04:00
|
|
|
|
|
|
|
queue_set = 1;
|
|
|
|
break;
|
2000-10-15 18:51:14 +04:00
|
|
|
case 't': /* touch(1) date format */
|
|
|
|
timer = stime(optarg);
|
|
|
|
time_set = 1;
|
|
|
|
break;
|
1998-06-28 01:15:07 +04:00
|
|
|
|
|
|
|
case 'd':
|
1999-05-22 09:05:11 +04:00
|
|
|
case 'r':
|
1998-06-28 01:15:07 +04:00
|
|
|
if (program != AT)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
program = ATRM;
|
|
|
|
options = "V";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
if (program != AT)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
program = ATQ;
|
|
|
|
options = "q:vV";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
if (program != AT)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
program = BATCH;
|
|
|
|
options = "f:q:mvV";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'V':
|
|
|
|
disp_version = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
program = CAT;
|
|
|
|
options = "";
|
1993-12-05 14:34:40 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* end of options eating */
|
|
|
|
|
1998-06-28 01:15:07 +04:00
|
|
|
if (disp_version)
|
|
|
|
(void)fprintf(stderr, "%s version %.1f\n", namep, AT_VERSION);
|
|
|
|
|
|
|
|
if (!check_permission()) {
|
2000-10-04 23:14:53 +04:00
|
|
|
errx(EXIT_FAILURE, "You do not have permission to use %s.", namep);
|
1998-06-28 01:15:07 +04:00
|
|
|
}
|
|
|
|
|
1993-12-05 14:34:40 +03:00
|
|
|
/* select our program */
|
|
|
|
switch (program) {
|
|
|
|
case ATQ:
|
1998-06-28 01:15:07 +04:00
|
|
|
if (optind != argc)
|
|
|
|
usage();
|
1993-12-05 14:34:40 +03:00
|
|
|
list_jobs();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ATRM:
|
1998-06-28 01:15:07 +04:00
|
|
|
case CAT:
|
|
|
|
if (optind == argc)
|
|
|
|
usage();
|
|
|
|
process_jobs(argc, argv, program);
|
1993-12-05 14:34:40 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AT:
|
2000-10-15 18:51:14 +04:00
|
|
|
if (argc > optind) {
|
|
|
|
/* -t and timespec argument are mutually exclusive */
|
|
|
|
if (time_set) {
|
|
|
|
usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
} else {
|
|
|
|
timer = parsetime(argc, argv);
|
|
|
|
time_set = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-12-05 14:34:40 +03:00
|
|
|
if (atverify) {
|
|
|
|
struct tm *tm = localtime(&timer);
|
1998-06-28 01:15:07 +04:00
|
|
|
(void)fprintf(stderr, "%s\n", asctime(tm));
|
1993-12-05 14:34:40 +03:00
|
|
|
}
|
|
|
|
writefile(timer, queue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BATCH:
|
1998-06-28 01:15:07 +04:00
|
|
|
if (queue_set)
|
|
|
|
queue = toupper(queue);
|
|
|
|
else
|
|
|
|
queue = DEFAULT_BATCH_QUEUE;
|
|
|
|
|
2000-10-15 18:51:14 +04:00
|
|
|
if (argc > optind) {
|
|
|
|
/* -t and timespec argument are mutually exclusive */
|
|
|
|
if (time_set) {
|
|
|
|
usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
} else {
|
|
|
|
timer = parsetime(argc, argv);
|
|
|
|
time_set = 1;
|
|
|
|
}
|
|
|
|
} else if (!time_set) {
|
1998-06-28 01:15:07 +04:00
|
|
|
timer = time(NULL);
|
2000-10-15 18:51:14 +04:00
|
|
|
}
|
1998-06-28 01:15:07 +04:00
|
|
|
|
|
|
|
if (atverify) {
|
|
|
|
struct tm *tm = localtime(&timer);
|
|
|
|
(void)fprintf(stderr, "%s\n", asctime(tm));
|
|
|
|
}
|
|
|
|
|
|
|
|
writefile(timer, queue);
|
1993-12-05 14:34:40 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic("Internal error");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
exit(EXIT_SUCCESS);
|
2000-06-25 17:35:48 +04:00
|
|
|
/*NOTREACHED*/
|
1993-12-05 14:34:40 +03:00
|
|
|
}
|