1994-08-27 13:43:04 +00:00
|
|
|
/* Copyright 1988,1990,1993,1994 by Paul Vixie
|
|
|
|
* All rights reserved
|
|
|
|
*
|
|
|
|
* Distribute freely, except: don't remove my name from the source or
|
|
|
|
* documentation (don't take credit for my work), mark your changes (don't
|
|
|
|
* get me blamed for your possible bugs), don't alter or remove this
|
|
|
|
* notice. May be sold if buildable source is provided to buyer. No
|
|
|
|
* warrantee of any kind, express or implied, is included with this
|
|
|
|
* software; use at your own risk, responsibility for damages (if any) to
|
|
|
|
* anyone resulting from the use of this software rests entirely with the
|
|
|
|
* user.
|
|
|
|
*
|
|
|
|
* Send bug reports, bug fixes, enhancements, requests, flames, etc., and
|
|
|
|
* I'll try to keep a version up to date. I can be reached as follows:
|
|
|
|
* Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
|
1994-12-21 11:06:08 +00:00
|
|
|
* From Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp
|
1994-08-27 13:43:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if !defined(lint) && !defined(LINT)
|
1997-09-15 06:39:25 +00:00
|
|
|
static const char rcsid[] =
|
1999-08-28 01:35:59 +00:00
|
|
|
"$FreeBSD$";
|
1994-08-27 13:43:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* crontab - install and manage per-user crontab files
|
|
|
|
* vix 02may87 [RCS has the rest of the log]
|
|
|
|
* vix 26jan87 [original]
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAIN_PROGRAM
|
|
|
|
|
|
|
|
#include "cron.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#ifdef USE_UTIMES
|
|
|
|
# include <sys/time.h>
|
|
|
|
#else
|
|
|
|
# include <time.h>
|
|
|
|
# include <utime.h>
|
|
|
|
#endif
|
|
|
|
#if defined(POSIX)
|
|
|
|
# include <locale.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define NHEADER_LINES 3
|
|
|
|
|
|
|
|
|
|
|
|
enum opt_t { opt_unknown, opt_list, opt_delete, opt_edit, opt_replace };
|
|
|
|
|
|
|
|
#if DEBUGGING
|
|
|
|
static char *Options[] = { "???", "list", "delete", "edit", "replace" };
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static PID_T Pid;
|
|
|
|
static char User[MAX_UNAME], RealUser[MAX_UNAME];
|
|
|
|
static char Filename[MAX_FNAME];
|
|
|
|
static FILE *NewCrontab;
|
|
|
|
static int CheckErrorCount;
|
|
|
|
static enum opt_t Option;
|
|
|
|
static struct passwd *pw;
|
|
|
|
static void list_cmd __P((void)),
|
|
|
|
delete_cmd __P((void)),
|
|
|
|
edit_cmd __P((void)),
|
|
|
|
poke_daemon __P((void)),
|
|
|
|
check_error __P((char *)),
|
|
|
|
parse_args __P((int c, char *v[]));
|
|
|
|
static int replace_cmd __P((void));
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(msg)
|
|
|
|
char *msg;
|
|
|
|
{
|
1997-09-15 06:39:25 +00:00
|
|
|
fprintf(stderr, "crontab: usage error: %s\n", msg);
|
|
|
|
fprintf(stderr, "%s\n%s\n",
|
|
|
|
"usage: crontab [-u user] file",
|
|
|
|
" crontab [-u user] { -e | -l | -r }");
|
1994-08-27 13:43:04 +00:00
|
|
|
exit(ERROR_EXIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
int exitstatus;
|
|
|
|
|
|
|
|
Pid = getpid();
|
|
|
|
ProgramName = argv[0];
|
|
|
|
|
|
|
|
#if defined(POSIX)
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(BSD)
|
|
|
|
setlinebuf(stderr);
|
|
|
|
#endif
|
|
|
|
parse_args(argc, argv); /* sets many globals, opens a file */
|
|
|
|
set_cron_uid();
|
|
|
|
set_cron_cwd();
|
|
|
|
if (!allowed(User)) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warnx("you (%s) are not allowed to use this program", User);
|
1994-08-27 13:43:04 +00:00
|
|
|
log_it(RealUser, Pid, "AUTH", "crontab command not allowed");
|
|
|
|
exit(ERROR_EXIT);
|
|
|
|
}
|
|
|
|
exitstatus = OK_EXIT;
|
|
|
|
switch (Option) {
|
|
|
|
case opt_list: list_cmd();
|
|
|
|
break;
|
|
|
|
case opt_delete: delete_cmd();
|
|
|
|
break;
|
|
|
|
case opt_edit: edit_cmd();
|
|
|
|
break;
|
|
|
|
case opt_replace: if (replace_cmd() < 0)
|
|
|
|
exitstatus = ERROR_EXIT;
|
|
|
|
break;
|
1997-09-15 06:39:25 +00:00
|
|
|
case opt_unknown:
|
|
|
|
break;
|
1994-08-27 13:43:04 +00:00
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1995-05-30 03:57:47 +00:00
|
|
|
|
1994-08-27 13:43:04 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
parse_args(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
int argch;
|
|
|
|
|
1997-09-15 06:39:25 +00:00
|
|
|
if (!(pw = getpwuid(getuid())))
|
|
|
|
errx(ERROR_EXIT, "your UID isn't in the passwd file, bailing out");
|
1996-12-17 00:55:20 +00:00
|
|
|
(void) strncpy(User, pw->pw_name, (sizeof User)-1);
|
|
|
|
User[(sizeof User)-1] = '\0';
|
1994-08-27 13:43:04 +00:00
|
|
|
strcpy(RealUser, User);
|
|
|
|
Filename[0] = '\0';
|
|
|
|
Option = opt_unknown;
|
1997-03-31 05:11:47 +00:00
|
|
|
while ((argch = getopt(argc, argv, "u:lerx:")) != -1) {
|
1994-08-27 13:43:04 +00:00
|
|
|
switch (argch) {
|
|
|
|
case 'x':
|
|
|
|
if (!set_debug_flags(optarg))
|
|
|
|
usage("bad debug option");
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
if (getuid() != ROOT_UID)
|
1997-09-15 06:39:25 +00:00
|
|
|
errx(ERROR_EXIT, "must be privileged to use -u");
|
1994-08-27 13:43:04 +00:00
|
|
|
if (!(pw = getpwnam(optarg)))
|
1997-09-15 06:39:25 +00:00
|
|
|
errx(ERROR_EXIT, "user `%s' unknown", optarg);
|
1996-12-17 00:55:20 +00:00
|
|
|
(void) strncpy(User, pw->pw_name, (sizeof User)-1);
|
|
|
|
User[(sizeof User)-1] = '\0';
|
1994-08-27 13:43:04 +00:00
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
if (Option != opt_unknown)
|
|
|
|
usage("only one operation permitted");
|
|
|
|
Option = opt_list;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
if (Option != opt_unknown)
|
|
|
|
usage("only one operation permitted");
|
|
|
|
Option = opt_delete;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
if (Option != opt_unknown)
|
|
|
|
usage("only one operation permitted");
|
|
|
|
Option = opt_edit;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage("unrecognized option");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endpwent();
|
|
|
|
|
|
|
|
if (Option != opt_unknown) {
|
|
|
|
if (argv[optind] != NULL) {
|
|
|
|
usage("no arguments permitted after this option");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (argv[optind] != NULL) {
|
|
|
|
Option = opt_replace;
|
1996-12-17 00:55:20 +00:00
|
|
|
(void) strncpy (Filename, argv[optind], (sizeof Filename)-1);
|
|
|
|
Filename[(sizeof Filename)-1] = '\0';
|
|
|
|
|
1994-08-27 13:43:04 +00:00
|
|
|
} else {
|
|
|
|
usage("file name must be specified for replace");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Option == opt_replace) {
|
|
|
|
/* we have to open the file here because we're going to
|
|
|
|
* chdir(2) into /var/cron before we get around to
|
|
|
|
* reading the file.
|
|
|
|
*/
|
|
|
|
if (!strcmp(Filename, "-")) {
|
|
|
|
NewCrontab = stdin;
|
|
|
|
} else {
|
|
|
|
/* relinquish the setuid status of the binary during
|
|
|
|
* the open, lest nonroot users read files they should
|
|
|
|
* not be able to read. we can't use access() here
|
|
|
|
* since there's a race condition. thanks go out to
|
|
|
|
* Arnt Gulbrandsen <agulbra@pvv.unit.no> for spotting
|
|
|
|
* the race.
|
|
|
|
*/
|
|
|
|
|
1997-09-15 06:39:25 +00:00
|
|
|
if (swap_uids() < OK)
|
|
|
|
err(ERROR_EXIT, "swapping uids");
|
|
|
|
if (!(NewCrontab = fopen(Filename, "r")))
|
|
|
|
err(ERROR_EXIT, "%s", Filename);
|
|
|
|
if (swap_uids() < OK)
|
|
|
|
err(ERROR_EXIT, "swapping uids back");
|
1994-08-27 13:43:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug(DMISC, ("user=%s, file=%s, option=%s\n",
|
|
|
|
User, Filename, Options[(int)Option]))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
list_cmd() {
|
|
|
|
char n[MAX_FNAME];
|
|
|
|
FILE *f;
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
log_it(RealUser, Pid, "LIST", User);
|
|
|
|
(void) sprintf(n, CRON_TAB(User));
|
|
|
|
if (!(f = fopen(n, "r"))) {
|
|
|
|
if (errno == ENOENT)
|
1997-09-15 06:39:25 +00:00
|
|
|
errx(ERROR_EXIT, "no crontab for %s", User);
|
1994-08-27 13:43:04 +00:00
|
|
|
else
|
1997-09-15 06:39:25 +00:00
|
|
|
err(ERROR_EXIT, "%s", n);
|
1994-08-27 13:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* file is open. copy to stdout, close.
|
|
|
|
*/
|
|
|
|
Set_LineNum(1)
|
|
|
|
while (EOF != (ch = get_char(f)))
|
|
|
|
putchar(ch);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
delete_cmd() {
|
|
|
|
char n[MAX_FNAME];
|
2000-10-15 00:35:34 +00:00
|
|
|
int ch, first;
|
|
|
|
|
|
|
|
if (isatty(STDIN_FILENO)) {
|
|
|
|
(void)fprintf(stderr, "remove crontab for %s? ", User);
|
|
|
|
first = ch = getchar();
|
|
|
|
while (ch != '\n' && ch != EOF)
|
|
|
|
ch = getchar();
|
|
|
|
if (first != 'y' && first != 'Y')
|
|
|
|
return;
|
|
|
|
}
|
1994-08-27 13:43:04 +00:00
|
|
|
|
|
|
|
log_it(RealUser, Pid, "DELETE", User);
|
|
|
|
(void) sprintf(n, CRON_TAB(User));
|
|
|
|
if (unlink(n)) {
|
|
|
|
if (errno == ENOENT)
|
1997-09-15 06:39:25 +00:00
|
|
|
errx(ERROR_EXIT, "no crontab for %s", User);
|
1994-08-27 13:43:04 +00:00
|
|
|
else
|
1997-09-15 06:39:25 +00:00
|
|
|
err(ERROR_EXIT, "%s", n);
|
1994-08-27 13:43:04 +00:00
|
|
|
}
|
|
|
|
poke_daemon();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
check_error(msg)
|
|
|
|
char *msg;
|
|
|
|
{
|
|
|
|
CheckErrorCount++;
|
|
|
|
fprintf(stderr, "\"%s\":%d: %s\n", Filename, LineNumber-1, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
edit_cmd() {
|
|
|
|
char n[MAX_FNAME], q[MAX_TEMPSTR], *editor;
|
|
|
|
FILE *f;
|
|
|
|
int ch, t, x;
|
|
|
|
struct stat statbuf;
|
|
|
|
time_t mtime;
|
|
|
|
WAIT_T waiter;
|
|
|
|
PID_T pid, xpid;
|
1996-12-17 00:55:20 +00:00
|
|
|
mode_t um;
|
1994-08-27 13:43:04 +00:00
|
|
|
|
|
|
|
log_it(RealUser, Pid, "BEGIN EDIT", User);
|
|
|
|
(void) sprintf(n, CRON_TAB(User));
|
|
|
|
if (!(f = fopen(n, "r"))) {
|
1997-09-15 06:39:25 +00:00
|
|
|
if (errno != ENOENT)
|
|
|
|
err(ERROR_EXIT, "%s", n);
|
|
|
|
warnx("no crontab for %s - using an empty one", User);
|
|
|
|
if (!(f = fopen("/dev/null", "r")))
|
|
|
|
err(ERROR_EXIT, "/dev/null");
|
1994-08-27 13:43:04 +00:00
|
|
|
}
|
|
|
|
|
1996-12-17 00:55:20 +00:00
|
|
|
um = umask(077);
|
|
|
|
(void) sprintf(Filename, "/tmp/crontab.XXXXXXXXXX");
|
|
|
|
if ((t = mkstemp(Filename)) == -1) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warn("%s", Filename);
|
1996-12-17 00:55:20 +00:00
|
|
|
(void) umask(um);
|
1994-08-27 13:43:04 +00:00
|
|
|
goto fatal;
|
|
|
|
}
|
1996-12-17 00:55:20 +00:00
|
|
|
(void) umask(um);
|
1994-08-27 13:43:04 +00:00
|
|
|
#ifdef HAS_FCHOWN
|
|
|
|
if (fchown(t, getuid(), getgid()) < 0) {
|
|
|
|
#else
|
|
|
|
if (chown(Filename, getuid(), getgid()) < 0) {
|
|
|
|
#endif
|
1997-09-15 06:39:25 +00:00
|
|
|
warn("fchown");
|
1994-08-27 13:43:04 +00:00
|
|
|
goto fatal;
|
|
|
|
}
|
1994-12-21 11:06:08 +00:00
|
|
|
if (!(NewCrontab = fdopen(t, "w"))) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warn("fdopen");
|
1994-08-27 13:43:04 +00:00
|
|
|
goto fatal;
|
|
|
|
}
|
|
|
|
|
|
|
|
Set_LineNum(1)
|
|
|
|
|
|
|
|
/* ignore the top few comments since we probably put them there.
|
|
|
|
*/
|
|
|
|
for (x = 0; x < NHEADER_LINES; x++) {
|
|
|
|
ch = get_char(f);
|
|
|
|
if (EOF == ch)
|
|
|
|
break;
|
|
|
|
if ('#' != ch) {
|
|
|
|
putc(ch, NewCrontab);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (EOF != (ch = get_char(f)))
|
|
|
|
if (ch == '\n')
|
|
|
|
break;
|
|
|
|
if (EOF == ch)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy the rest of the crontab (if any) to the temp file.
|
|
|
|
*/
|
|
|
|
if (EOF != ch)
|
|
|
|
while (EOF != (ch = get_char(f)))
|
|
|
|
putc(ch, NewCrontab);
|
|
|
|
fclose(f);
|
1997-09-15 06:39:25 +00:00
|
|
|
if (fclose(NewCrontab))
|
|
|
|
err(ERROR_EXIT, "%s", Filename);
|
1994-08-27 13:43:04 +00:00
|
|
|
again:
|
1994-12-21 11:06:08 +00:00
|
|
|
if (stat(Filename, &statbuf) < 0) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warn("stat");
|
1994-08-27 13:43:04 +00:00
|
|
|
fatal: unlink(Filename);
|
|
|
|
exit(ERROR_EXIT);
|
|
|
|
}
|
|
|
|
mtime = statbuf.st_mtime;
|
|
|
|
|
|
|
|
if ((!(editor = getenv("VISUAL")))
|
|
|
|
&& (!(editor = getenv("EDITOR")))
|
|
|
|
) {
|
|
|
|
editor = EDITOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we still have the file open. editors will generally rewrite the
|
|
|
|
* original file rather than renaming/unlinking it and starting a
|
|
|
|
* new one; even backup files are supposed to be made by copying
|
|
|
|
* rather than by renaming. if some editor does not support this,
|
|
|
|
* then don't use it. the security problems are more severe if we
|
|
|
|
* close and reopen the file around the edit.
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (pid = fork()) {
|
|
|
|
case -1:
|
1997-09-15 06:39:25 +00:00
|
|
|
warn("fork");
|
1994-08-27 13:43:04 +00:00
|
|
|
goto fatal;
|
|
|
|
case 0:
|
|
|
|
/* child */
|
1997-09-15 06:39:25 +00:00
|
|
|
if (setuid(getuid()) < 0)
|
|
|
|
err(ERROR_EXIT, "setuid(getuid())");
|
|
|
|
if (chdir("/tmp") < 0)
|
|
|
|
err(ERROR_EXIT, "chdir(/tmp)");
|
|
|
|
if (strlen(editor) + strlen(Filename) + 2 >= MAX_TEMPSTR)
|
|
|
|
errx(ERROR_EXIT, "editor or filename too long");
|
1996-04-09 20:28:16 +00:00
|
|
|
execlp(editor, editor, Filename, NULL);
|
1997-09-15 06:39:25 +00:00
|
|
|
err(ERROR_EXIT, "%s", editor);
|
1994-08-27 13:43:04 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
default:
|
|
|
|
/* parent */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parent */
|
1996-04-09 20:28:16 +00:00
|
|
|
{
|
|
|
|
void (*f[4])();
|
|
|
|
f[0] = signal(SIGHUP, SIG_IGN);
|
|
|
|
f[1] = signal(SIGINT, SIG_IGN);
|
|
|
|
f[2] = signal(SIGTERM, SIG_IGN);
|
1994-08-27 13:43:04 +00:00
|
|
|
xpid = wait(&waiter);
|
1996-04-09 20:28:16 +00:00
|
|
|
signal(SIGHUP, f[0]);
|
|
|
|
signal(SIGINT, f[1]);
|
|
|
|
signal(SIGTERM, f[2]);
|
|
|
|
}
|
1994-08-27 13:43:04 +00:00
|
|
|
if (xpid != pid) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warnx("wrong PID (%d != %d) from \"%s\"", xpid, pid, editor);
|
1994-08-27 13:43:04 +00:00
|
|
|
goto fatal;
|
|
|
|
}
|
|
|
|
if (WIFEXITED(waiter) && WEXITSTATUS(waiter)) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warnx("\"%s\" exited with status %d", editor, WEXITSTATUS(waiter));
|
1994-08-27 13:43:04 +00:00
|
|
|
goto fatal;
|
|
|
|
}
|
|
|
|
if (WIFSIGNALED(waiter)) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warnx("\"%s\" killed; signal %d (%score dumped)",
|
|
|
|
editor, WTERMSIG(waiter), WCOREDUMP(waiter) ?"" :"no ");
|
1994-08-27 13:43:04 +00:00
|
|
|
goto fatal;
|
|
|
|
}
|
1994-12-21 11:06:08 +00:00
|
|
|
if (stat(Filename, &statbuf) < 0) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warn("stat");
|
1994-08-27 13:43:04 +00:00
|
|
|
goto fatal;
|
|
|
|
}
|
|
|
|
if (mtime == statbuf.st_mtime) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warnx("no changes made to crontab");
|
1994-08-27 13:43:04 +00:00
|
|
|
goto remove;
|
|
|
|
}
|
1997-09-15 06:39:25 +00:00
|
|
|
warnx("installing new crontab");
|
1994-12-21 11:06:08 +00:00
|
|
|
if (!(NewCrontab = fopen(Filename, "r"))) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warn("%s", Filename);
|
1994-12-21 11:06:08 +00:00
|
|
|
goto fatal;
|
|
|
|
}
|
1994-08-27 13:43:04 +00:00
|
|
|
switch (replace_cmd()) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
for (;;) {
|
|
|
|
printf("Do you want to retry the same edit? ");
|
|
|
|
fflush(stdout);
|
|
|
|
q[0] = '\0';
|
|
|
|
(void) fgets(q, sizeof q, stdin);
|
|
|
|
switch (islower(q[0]) ? q[0] : tolower(q[0])) {
|
|
|
|
case 'y':
|
|
|
|
goto again;
|
|
|
|
case 'n':
|
|
|
|
goto abandon;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Enter Y or N\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*NOTREACHED*/
|
|
|
|
case -2:
|
|
|
|
abandon:
|
1997-09-15 06:39:25 +00:00
|
|
|
warnx("edits left in %s", Filename);
|
1994-08-27 13:43:04 +00:00
|
|
|
goto done;
|
|
|
|
default:
|
1997-09-15 06:39:25 +00:00
|
|
|
warnx("panic: bad switch() in replace_cmd()");
|
1994-08-27 13:43:04 +00:00
|
|
|
goto fatal;
|
|
|
|
}
|
|
|
|
remove:
|
|
|
|
unlink(Filename);
|
|
|
|
done:
|
|
|
|
log_it(RealUser, Pid, "END EDIT", User);
|
|
|
|
}
|
1995-05-30 03:57:47 +00:00
|
|
|
|
1994-08-27 13:43:04 +00:00
|
|
|
|
|
|
|
/* returns 0 on success
|
|
|
|
* -1 on syntax error
|
|
|
|
* -2 on install error
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
replace_cmd() {
|
|
|
|
char n[MAX_FNAME], envstr[MAX_ENVSTR], tn[MAX_FNAME];
|
|
|
|
FILE *tmp;
|
|
|
|
int ch, eof;
|
|
|
|
entry *e;
|
|
|
|
time_t now = time(NULL);
|
|
|
|
char **envp = env_init();
|
|
|
|
|
1996-12-17 00:55:20 +00:00
|
|
|
if (envp == NULL) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warnx("cannot allocate memory");
|
1996-12-17 00:55:20 +00:00
|
|
|
return (-2);
|
|
|
|
}
|
|
|
|
|
1994-08-27 13:43:04 +00:00
|
|
|
(void) sprintf(n, "tmp.%d", Pid);
|
|
|
|
(void) sprintf(tn, CRON_TAB(n));
|
|
|
|
if (!(tmp = fopen(tn, "w+"))) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warn("%s", tn);
|
1994-08-27 13:43:04 +00:00
|
|
|
return (-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write a signature at the top of the file.
|
|
|
|
*
|
|
|
|
* VERY IMPORTANT: make sure NHEADER_LINES agrees with this code.
|
|
|
|
*/
|
|
|
|
fprintf(tmp, "# DO NOT EDIT THIS FILE - edit the master and reinstall.\n");
|
|
|
|
fprintf(tmp, "# (%s installed on %-24.24s)\n", Filename, ctime(&now));
|
|
|
|
fprintf(tmp, "# (Cron version -- %s)\n", rcsid);
|
|
|
|
|
|
|
|
/* copy the crontab to the tmp
|
|
|
|
*/
|
|
|
|
Set_LineNum(1)
|
|
|
|
while (EOF != (ch = get_char(NewCrontab)))
|
|
|
|
putc(ch, tmp);
|
1994-12-21 11:06:08 +00:00
|
|
|
fclose(NewCrontab);
|
1994-08-27 13:43:04 +00:00
|
|
|
ftruncate(fileno(tmp), ftell(tmp));
|
|
|
|
fflush(tmp); rewind(tmp);
|
|
|
|
|
|
|
|
if (ferror(tmp)) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warnx("error while writing new crontab to %s", tn);
|
1994-08-27 13:43:04 +00:00
|
|
|
fclose(tmp); unlink(tn);
|
|
|
|
return (-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check the syntax of the file being installed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BUG: was reporting errors after the EOF if there were any errors
|
|
|
|
* in the file proper -- kludged it by stopping after first error.
|
|
|
|
* vix 31mar87
|
|
|
|
*/
|
|
|
|
Set_LineNum(1 - NHEADER_LINES)
|
|
|
|
CheckErrorCount = 0; eof = FALSE;
|
|
|
|
while (!CheckErrorCount && !eof) {
|
|
|
|
switch (load_env(envstr, tmp)) {
|
|
|
|
case ERR:
|
|
|
|
eof = TRUE;
|
|
|
|
break;
|
|
|
|
case FALSE:
|
|
|
|
e = load_entry(tmp, check_error, pw, envp);
|
|
|
|
if (e)
|
|
|
|
free(e);
|
|
|
|
break;
|
|
|
|
case TRUE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CheckErrorCount != 0) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warnx("errors in crontab file, can't install");
|
1994-08-27 13:43:04 +00:00
|
|
|
fclose(tmp); unlink(tn);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAS_FCHOWN
|
|
|
|
if (fchown(fileno(tmp), ROOT_UID, -1) < OK)
|
|
|
|
#else
|
|
|
|
if (chown(tn, ROOT_UID, -1) < OK)
|
|
|
|
#endif
|
|
|
|
{
|
1997-09-15 06:39:25 +00:00
|
|
|
warn("chown");
|
1994-08-27 13:43:04 +00:00
|
|
|
fclose(tmp); unlink(tn);
|
|
|
|
return (-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAS_FCHMOD
|
|
|
|
if (fchmod(fileno(tmp), 0600) < OK)
|
|
|
|
#else
|
|
|
|
if (chmod(tn, 0600) < OK)
|
|
|
|
#endif
|
|
|
|
{
|
1997-09-15 06:39:25 +00:00
|
|
|
warn("chown");
|
1994-08-27 13:43:04 +00:00
|
|
|
fclose(tmp); unlink(tn);
|
|
|
|
return (-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fclose(tmp) == EOF) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warn("fclose");
|
1994-08-27 13:43:04 +00:00
|
|
|
unlink(tn);
|
|
|
|
return (-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) sprintf(n, CRON_TAB(User));
|
|
|
|
if (rename(tn, n)) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warn("error renaming %s to %s", tn, n);
|
1994-08-27 13:43:04 +00:00
|
|
|
unlink(tn);
|
|
|
|
return (-2);
|
|
|
|
}
|
|
|
|
log_it(RealUser, Pid, "REPLACE", User);
|
|
|
|
|
|
|
|
poke_daemon();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
poke_daemon() {
|
|
|
|
#ifdef USE_UTIMES
|
|
|
|
struct timeval tvs[2];
|
|
|
|
struct timezone tz;
|
|
|
|
|
|
|
|
(void) gettimeofday(&tvs[0], &tz);
|
|
|
|
tvs[1] = tvs[0];
|
|
|
|
if (utimes(SPOOL_DIR, tvs) < OK) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warn("can't update mtime on spooldir %s", SPOOL_DIR);
|
1994-08-27 13:43:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (utime(SPOOL_DIR, NULL) < OK) {
|
1997-09-15 06:39:25 +00:00
|
|
|
warn("can't update mtime on spooldir %s", SPOOL_DIR);
|
1994-08-27 13:43:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif /*USE_UTIMES*/
|
|
|
|
}
|