NetBSD/usr.sbin/cron/crontab.c

704 lines
16 KiB
C
Raw Normal View History

/* $NetBSD: crontab.c,v 1.19 2003/06/23 22:36:02 hubertf Exp $ */
1997-03-13 09:19:07 +03:00
1994-01-12 21:35:59 +03:00
/* Copyright 1988,1990,1993,1994 by Paul Vixie
1994-01-05 23:40:12 +03:00
* 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
*/
#include <sys/cdefs.h>
1994-01-05 23:40:12 +03:00
#if !defined(lint) && !defined(LINT)
#if 0
static char rcsid[] = "Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp";
#else
__RCSID("$NetBSD: crontab.c,v 1.19 2003/06/23 22:36:02 hubertf Exp $");
#endif
1994-01-05 23:40:12 +03:00
#endif
/* crontab - install and manage per-user crontab files
* vix 02may87 [RCS has the rest of the log]
* vix 26jan87 [original]
*/
#define MAXCRONTABSIZE (1024*256) /* max crontab size == 256 KB */
1994-01-05 23:40:12 +03:00
#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 <utime.h>
#endif
#if defined(POSIX)
# include <locale.h>
#endif
#include <time.h>
#include <signal.h>
1994-01-05 23:40:12 +03:00
1994-01-20 05:46:53 +03:00
#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
1994-01-12 21:35:59 +03:00
static PID_T Pid;
1994-01-11 22:10:45 +03:00
static char User[MAX_UNAME], RealUser[MAX_UNAME];
static char Filename[MAX_FNAME];
static FILE *NewCrontab;
static int CheckErrorCount;
1994-01-20 05:46:53 +03:00
static enum opt_t Option;
1994-01-11 22:10:45 +03:00
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((const char *)),
1998-02-22 15:17:31 +03:00
parse_args __P((int c, char *v[])),
skip_header __P((int *, FILE *)),
1998-02-22 15:17:31 +03:00
usage __P((char *));
1994-01-20 05:46:53 +03:00
static int replace_cmd __P((void));
1994-01-05 23:40:12 +03:00
1998-02-22 15:17:31 +03:00
int main __P((int, char *[]));
1994-01-05 23:40:12 +03:00
static void
usage(msg)
char *msg;
{
fprintf(stderr, "%s: usage error: %s\n", ProgramName, msg);
fprintf(stderr, "usage:\t%s [-u user] file\n", ProgramName);
fprintf(stderr, "\t%s [-u user] [ -e | -l | -r ]\n", ProgramName);
1994-01-05 23:40:12 +03:00
fprintf(stderr, "\t\t(default operation is replace, per 1003.2)\n");
fprintf(stderr, "\t-e\t(edit user's crontab)\n");
fprintf(stderr, "\t-l\t(list user's crontab)\n");
fprintf(stderr, "\t-r\t(delete user's crontab)\n");
exit(ERROR_EXIT);
}
int
main(argc, argv)
int argc;
char *argv[];
{
1994-01-20 05:46:53 +03:00
int exitstatus;
1994-01-05 23:40:12 +03:00
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)) {
fprintf(stderr,
"You (%s) are not allowed to use this program (%s)\n",
User, ProgramName);
fprintf(stderr, "See crontab(1) for more information\n");
log_it(RealUser, Pid, "AUTH", "crontab command not allowed");
exit(ERROR_EXIT);
}
1994-01-20 05:46:53 +03:00
exitstatus = OK_EXIT;
switch (Option) {
1994-01-05 23:40:12 +03:00
case opt_list: list_cmd();
break;
case opt_delete: delete_cmd();
break;
case opt_edit: edit_cmd();
break;
1994-01-20 05:46:53 +03:00
case opt_replace: if (replace_cmd() < 0)
exitstatus = ERROR_EXIT;
1994-01-05 23:40:12 +03:00
break;
1998-02-22 15:17:31 +03:00
case opt_unknown:
usage("unrecognized option");
break;
1994-01-05 23:40:12 +03:00
}
exit(0);
/*NOTREACHED*/
}
static void
parse_args(argc, argv)
int argc;
char *argv[];
{
int argch;
if (!(pw = getpwuid(getuid()))) {
fprintf(stderr, "%s: your UID isn't in the passwd file.\n",
ProgramName);
fprintf(stderr, "bailing out.\n");
exit(ERROR_EXIT);
}
2002-11-16 07:34:13 +03:00
strlcpy(User, pw->pw_name, sizeof(User));
strlcpy(RealUser, User, sizeof(RealUser));
1994-01-05 23:40:12 +03:00
Filename[0] = '\0';
Option = opt_unknown;
1997-10-17 04:10:42 +04:00
while (-1 != (argch = getopt(argc, argv, "u:lerx:"))) {
1994-01-05 23:40:12 +03:00
switch (argch) {
case 'x':
if (!set_debug_flags(optarg))
usage("bad debug option");
break;
case 'u':
if (getuid() != ROOT_UID)
{
fprintf(stderr,
"must be privileged to use -u\n");
exit(ERROR_EXIT);
}
1994-01-11 22:10:45 +03:00
if (!(pw = getpwnam(optarg)))
1994-01-05 23:40:12 +03:00
{
fprintf(stderr, "%s: user `%s' unknown\n",
ProgramName, optarg);
exit(ERROR_EXIT);
}
2002-11-16 07:34:13 +03:00
(void) strlcpy(User, optarg, sizeof(User));
1994-01-05 23:40:12 +03: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;
2002-11-16 07:34:13 +03:00
(void) strlcpy(Filename, argv[optind],
sizeof(Filename));
1994-01-05 23:40:12 +03: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.
*/
if (swap_uids() < OK) {
perror("swapping uids");
exit(ERROR_EXIT);
}
if (!(NewCrontab = fopen(Filename, "r"))) {
perror(Filename);
exit(ERROR_EXIT);
}
if (swap_uids() < OK) {
perror("swapping uids back");
exit(ERROR_EXIT);
}
}
}
Debug(DMISC, ("user=%s, file=%s, option=%s\n",
User, Filename, Options[(int)Option]))
}
static void
skip_header(pch, f)
int *pch;
FILE *f;
{
int ch;
int x;
/* 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;
}
*pch = ch;
}
1994-01-05 23:40:12 +03:00
static void
list_cmd() {
char n[MAX_FNAME];
FILE *f;
int ch;
log_it(RealUser, Pid, "LIST", User);
(void) snprintf(n, sizeof(n), CRON_TAB(User));
1994-01-05 23:40:12 +03:00
if (!(f = fopen(n, "r"))) {
if (errno == ENOENT)
fprintf(stderr, "no crontab for %s\n", User);
else
perror(n);
exit(ERROR_EXIT);
}
/* file is open. copy to stdout, close.
*/
Set_LineNum(1)
skip_header(&ch, f);
1994-01-05 23:40:12 +03:00
while (EOF != (ch = get_char(f)))
putchar(ch);
fclose(f);
}
static void
delete_cmd() {
char n[MAX_FNAME];
log_it(RealUser, Pid, "DELETE", User);
(void) snprintf(n, sizeof(n), CRON_TAB(User));
1994-01-05 23:40:12 +03:00
if (unlink(n)) {
if (errno == ENOENT)
fprintf(stderr, "no crontab for %s\n", User);
else
perror(n);
exit(ERROR_EXIT);
}
poke_daemon();
}
static void
check_error(msg)
const char *msg;
1994-01-05 23:40:12 +03:00
{
1994-01-20 05:46:53 +03:00
CheckErrorCount++;
fprintf(stderr, "\"%s\":%d: %s\n", Filename, LineNumber-1, msg);
1994-01-05 23:40:12 +03:00
}
static void
edit_cmd() {
1998-07-28 23:27:39 +04:00
char n[MAX_FNAME], q[MAX_TEMPSTR];
char *editor;
1994-01-05 23:40:12 +03:00
FILE *f;
int ch, t;
1994-01-05 23:40:12 +03:00
struct stat statbuf;
time_t mtime;
long mtimensec;
1994-01-05 23:40:12 +03:00
WAIT_T waiter;
1994-01-12 21:35:59 +03:00
PID_T pid, xpid;
sig_t oint, oabrt;
char *argv[3];
1994-01-05 23:40:12 +03:00
log_it(RealUser, Pid, "BEGIN EDIT", User);
(void) snprintf(n, sizeof(n), CRON_TAB(User));
1994-01-05 23:40:12 +03:00
if (!(f = fopen(n, "r"))) {
if (errno != ENOENT) {
perror(n);
exit(ERROR_EXIT);
}
fprintf(stderr, "no crontab for %s - using an empty one\n",
User);
if (!(f = fopen("/dev/null", "r"))) {
perror("/dev/null");
exit(ERROR_EXIT);
}
}
(void) snprintf(Filename, sizeof(Filename), "/tmp/crontab.%d", Pid);
1994-01-20 05:46:53 +03:00
if (-1 == (t = open(Filename, O_CREAT|O_EXCL|O_RDWR, 0600))) {
1994-01-05 23:40:12 +03:00
perror(Filename);
goto fatal;
}
#ifdef HAS_FCHOWN
if (fchown(t, getuid(), getgid()) < 0) {
#else
if (chown(Filename, getuid(), getgid()) < 0) {
#endif
perror("fchown");
goto fatal;
}
if (fcntl(t, F_SETFD, FD_CLOEXEC) == -1) {
perror("fcntl");
goto fatal;
}
1994-01-05 23:40:12 +03:00
if (!(NewCrontab = fdopen(t, "r+"))) {
perror("fdopen");
goto fatal;
}
Set_LineNum(1)
skip_header(&ch, f);
1994-01-05 23:40:12 +03:00
/* 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);
1994-01-20 05:46:53 +03:00
if (fflush(NewCrontab) < OK) {
perror(Filename);
exit(ERROR_EXIT);
}
again:
rewind(NewCrontab);
1994-01-05 23:40:12 +03:00
if (ferror(NewCrontab)) {
fprintf(stderr, "%s: error while writing new crontab to %s\n",
ProgramName, Filename);
1994-01-20 05:46:53 +03:00
fatal: unlink(Filename);
1994-01-05 23:40:12 +03:00
exit(ERROR_EXIT);
}
if (fstat(t, &statbuf) < 0) {
perror("fstat");
goto fatal;
}
mtime = statbuf.st_mtime;
mtimensec = statbuf.st_mtimensec;
1994-01-05 23:40:12 +03:00
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.
*/
oint = signal(SIGINT, SIG_IGN);
oabrt = signal(SIGABRT, SIG_IGN);
1994-01-05 23:40:12 +03:00
switch (pid = fork()) {
case -1:
perror("fork");
goto fatal;
case 0:
/* child */
if (setuid(getuid()) < 0) {
perror("setuid(getuid())");
exit(ERROR_EXIT);
}
if (chdir("/tmp") < 0) {
perror("chdir(/tmp)");
exit(ERROR_EXIT);
}
argv[0] = editor;
argv[1] = Filename;
argv[2] = NULL;
execvp(editor, argv);
1994-01-05 23:40:12 +03:00
perror(editor);
exit(ERROR_EXIT);
/*NOTREACHED*/
default:
/* parent */
break;
}
/* parent */
1994-01-12 21:35:59 +03:00
xpid = wait(&waiter);
if (xpid != pid) {
1994-01-05 23:40:12 +03:00
fprintf(stderr, "%s: wrong PID (%d != %d) from \"%s\"\n",
1994-01-12 21:35:59 +03:00
ProgramName, xpid, pid, editor);
1994-01-05 23:40:12 +03:00
goto fatal;
}
(void)signal(SIGINT, oint);
(void)signal(SIGABRT, oabrt);
1994-01-05 23:40:12 +03:00
if (WIFEXITED(waiter) && WEXITSTATUS(waiter)) {
fprintf(stderr, "%s: \"%s\" exited with status %d\n",
ProgramName, editor, WEXITSTATUS(waiter));
goto fatal;
}
if (WIFSIGNALED(waiter)) {
fprintf(stderr,
"%s: \"%s\" killed; signal %d (%score dumped)\n",
ProgramName, editor, WTERMSIG(waiter),
WCOREDUMP(waiter) ?"" :"no ");
goto fatal;
}
if (fstat(t, &statbuf) < 0) {
perror("fstat");
goto fatal;
}
if (mtime == statbuf.st_mtime && mtimensec == statbuf.st_mtimensec) {
1994-01-05 23:40:12 +03:00
fprintf(stderr, "%s: no changes made to crontab\n",
ProgramName);
1994-01-20 05:46:53 +03:00
goto remove;
1994-01-05 23:40:12 +03:00
}
1994-01-20 05:46:53 +03:00
fprintf(stderr, "%s: installing new crontab\n", ProgramName);
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:
fprintf(stderr, "%s: edits left in %s\n",
ProgramName, Filename);
goto done;
default:
1998-02-22 15:17:31 +03:00
fprintf(stderr, "%s: panic: bad switch() in replace_cmd()\n",
ProgramName);
1994-01-20 05:46:53 +03:00
goto fatal;
}
remove:
unlink(Filename);
done:
1994-01-05 23:40:12 +03:00
log_it(RealUser, Pid, "END EDIT", User);
}
1994-01-20 05:46:53 +03:00
/* returns 0 on success
* -1 on syntax error
* -2 on install error
*/
static int
1994-01-05 23:40:12 +03:00
replace_cmd() {
char n[MAX_FNAME], n2[MAX_FNAME], envstr[MAX_ENVSTR], tn[MAX_FNAME];
FILE *tmp, *fmaxtabsize;
1994-01-05 23:40:12 +03:00
int ch, eof;
entry *e;
time_t now = time(NULL);
1994-01-11 22:10:45 +03:00
char **envp = env_init();
size_t maxtabsize;
struct stat statbuf;
1994-01-05 23:40:12 +03:00
(void) snprintf(n, sizeof(n), "tmp.%d", Pid);
(void) snprintf(tn, sizeof(tn), CRON_TAB(n));
1994-01-05 23:40:12 +03:00
if (!(tmp = fopen(tn, "w+"))) {
perror(tn);
1994-01-20 05:46:53 +03:00
return (-2);
1994-01-05 23:40:12 +03:00
}
/* Make sure that the crontab is not an unreasonable size.
*
* XXX This is subject to a race condition--the user could
* add stuff to the file after we've checked the size but
* before we slurp it in and write it out. We can't just move
* the test to test the temp file we later create, because by
* that time we've already filled up the crontab disk. Probably
* the right thing to do is to do a bytecount in the copy loop
* rather than stating the file we're about to read.
*/
(void) snprintf(n2, sizeof(n), "%s/%s", CRONDIR, MAXTABSIZE_FILE);
if ((fmaxtabsize = fopen(n2, "r"))) {
if (fgets(n2, sizeof(n2), fmaxtabsize) == NULL) {
maxtabsize = 0;
} else {
maxtabsize = atoi(n2);
}
fclose(fmaxtabsize);
} else {
maxtabsize = MAXTABSIZE_DEFAULT;
}
if (fstat(fileno(NewCrontab), &statbuf)) {
fprintf(stderr, "%s: error stat'ing crontab input: %s\n",
ProgramName, strerror(errno));
return(-2);
}
if (statbuf.st_size > maxtabsize) {
fprintf(stderr,
"%s: %ld bytes is larger than the maximum size of %ld bytes\n",
ProgramName, (long) statbuf.st_size, (long) maxtabsize);
return(-1);
}
1994-01-05 23:40:12 +03:00
/* 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",
"$NetBSD: crontab.c,v 1.19 2003/06/23 22:36:02 hubertf Exp $");
1994-01-05 23:40:12 +03:00
/* copy the crontab to the tmp
*/
1994-01-20 05:46:53 +03:00
rewind(NewCrontab);
1994-01-05 23:40:12 +03:00
Set_LineNum(1)
while (EOF != (ch = get_char(NewCrontab)))
putc(ch, tmp);
ftruncate(fileno(tmp), ftell(tmp));
fflush(tmp); rewind(tmp);
if (ferror(tmp)) {
fprintf(stderr, "%s: error while writing new crontab to %s\n",
ProgramName, tn);
fclose(tmp); unlink(tn);
1994-01-20 05:46:53 +03:00
return (-2);
1994-01-05 23:40:12 +03:00
}
/* 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
*/
1994-01-20 05:46:53 +03:00
Set_LineNum(1 - NHEADER_LINES)
1994-01-05 23:40:12 +03:00
CheckErrorCount = 0; eof = FALSE;
while (!CheckErrorCount && !eof) {
switch (load_env(envstr, tmp)) {
case ERR:
eof = TRUE;
break;
case FALSE:
1994-01-11 22:10:45 +03:00
e = load_entry(tmp, check_error, pw, envp);
1994-01-20 05:46:53 +03:00
if (e)
free(e);
1994-01-05 23:40:12 +03:00
break;
case TRUE:
break;
}
}
if (CheckErrorCount != 0) {
fprintf(stderr, "errors in crontab file, can't install.\n");
fclose(tmp); unlink(tn);
1994-01-20 05:46:53 +03:00
return (-1);
1994-01-05 23:40:12 +03:00
}
#ifdef HAS_FCHOWN
1994-01-20 05:46:53 +03:00
if (fchown(fileno(tmp), ROOT_UID, -1) < OK)
1994-01-05 23:40:12 +03:00
#else
1994-01-20 05:46:53 +03:00
if (chown(tn, ROOT_UID, -1) < OK)
1994-01-05 23:40:12 +03:00
#endif
1994-01-20 05:46:53 +03:00
{
1994-01-05 23:40:12 +03:00
perror("chown");
fclose(tmp); unlink(tn);
1994-01-20 05:46:53 +03:00
return (-2);
1994-01-05 23:40:12 +03:00
}
#ifdef HAS_FCHMOD
1994-01-20 05:46:53 +03:00
if (fchmod(fileno(tmp), 0600) < OK)
1994-01-05 23:40:12 +03:00
#else
1994-01-20 05:46:53 +03:00
if (chmod(tn, 0600) < OK)
1994-01-05 23:40:12 +03:00
#endif
1994-01-20 05:46:53 +03:00
{
1994-01-05 23:40:12 +03:00
perror("chown");
fclose(tmp); unlink(tn);
1994-01-20 05:46:53 +03:00
return (-2);
1994-01-05 23:40:12 +03:00
}
if (fclose(tmp) == EOF) {
perror("fclose");
unlink(tn);
1994-01-20 05:46:53 +03:00
return (-2);
1994-01-05 23:40:12 +03:00
}
(void) snprintf(n, sizeof(n), CRON_TAB(User));
1994-01-05 23:40:12 +03:00
if (rename(tn, n)) {
fprintf(stderr, "%s: error renaming %s to %s\n",
ProgramName, tn, n);
perror("rename");
unlink(tn);
1994-01-20 05:46:53 +03:00
return (-2);
1994-01-05 23:40:12 +03:00
}
log_it(RealUser, Pid, "REPLACE", User);
poke_daemon();
1994-01-20 05:46:53 +03:00
return (0);
1994-01-05 23:40:12 +03:00
}
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) {
fprintf(stderr, "crontab: can't update mtime on spooldir\n");
perror(SPOOL_DIR);
return;
}
#else
if (utime(SPOOL_DIR, NULL) < OK) {
fprintf(stderr, "crontab: can't update mtime on spooldir\n");
perror(SPOOL_DIR);
return;
}
#endif /*USE_UTIMES*/
}