#if !defined(lint) && !defined(LINT)
static const char rcsid[] =
"$FreeBSD: src/usr.sbin/cron/lib/entry.c,v 1.12 2001/06/13 05:49:37 dd Exp $";
#endif
#include "cron.h"
#include <grp.h>
#ifdef LOGIN_CAP
#include <login_cap.h>
#endif
typedef enum ecode {
e_none, e_minute, e_hour, e_dom, e_month, e_dow,
e_cmd, e_timespec, e_username, e_group, e_mem
#ifdef LOGIN_CAP
, e_class
#endif
} ecode_e;
static char get_list __P((bitstr_t *, int, int, char *[], int, FILE *)),
get_range __P((bitstr_t *, int, int, char *[], int, FILE *)),
get_number __P((int *, int, char *[], int, FILE *));
static int set_element __P((bitstr_t *, int, int, int));
static char *ecodes[] =
{
"no error",
"bad minute",
"bad hour",
"bad day-of-month",
"bad month",
"bad day-of-week",
"bad command",
"bad time specifier",
"bad username",
"bad group name",
"out of memory",
#ifdef LOGIN_CAP
"bad class name",
#endif
};
void
free_entry(e)
entry *e;
{
#ifdef LOGIN_CAP
if (e->class != NULL)
free(e->class);
#endif
if( e->cmd != NULL )
free(e->cmd);
if( e->envp != NULL)
env_free(e->envp);
free(e);
}
entry *
load_entry(file, error_func, uname, envp)
FILE *file;
void (*error_func)();
char *uname;
char **envp;
{
ecode_e ecode = e_none;
entry *e;
int ch;
char cmd[MAX_COMMAND];
char envstr[MAX_ENVSTR];
char **prev_env;
Debug(DPARS, ("load_entry()...about to eat comments\n"))
skip_comments(file);
ch = get_char(file);
if (ch == EOF)
return NULL;
e = (entry *) calloc(sizeof(entry), sizeof(char));
if (e == NULL) {
warn("load_entry: calloc failed");
return NULL;
}
if (ch == '@') {
Debug(DPARS, ("load_entry()...about to test shortcuts\n"))
ch = get_string(cmd, MAX_COMMAND, file, " \t\n");
if (!strcmp("reboot", cmd)) {
Debug(DPARS, ("load_entry()...reboot shortcut\n"))
e->flags |= WHEN_REBOOT;
} else if (!strcmp("yearly", cmd) || !strcmp("annually", cmd)){
Debug(DPARS, ("load_entry()...yearly shortcut\n"))
bit_set(e->minute, 0);
bit_set(e->hour, 0);
bit_set(e->dom, 0);
bit_set(e->month, 0);
bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
e->flags |= DOW_STAR;
} else if (!strcmp("monthly", cmd)) {
Debug(DPARS, ("load_entry()...monthly shortcut\n"))
bit_set(e->minute, 0);
bit_set(e->hour, 0);
bit_set(e->dom, 0);
bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
e->flags |= DOW_STAR;
} else if (!strcmp("weekly", cmd)) {
Debug(DPARS, ("load_entry()...weekly shortcut\n"))
bit_set(e->minute, 0);
bit_set(e->hour, 0);
bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
bit_set(e->dow, 0);
} else if (!strcmp("daily", cmd) || !strcmp("midnight", cmd)) {
Debug(DPARS, ("load_entry()...daily shortcut\n"))
bit_set(e->minute, 0);
bit_set(e->hour, 0);
bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
} else if (!strcmp("hourly", cmd)) {
Debug(DPARS, ("load_entry()...hourly shortcut\n"))
bit_set(e->minute, 0);
bit_nset(e->hour, 0, (LAST_HOUR-FIRST_HOUR+1));
bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
} else {
ecode = e_timespec;
goto eof;
}
Skip_Blanks(ch, file);
if (ch == EOF) {
ecode = e_cmd;
goto eof;
}
} else {
Debug(DPARS, ("load_entry()...about to parse numerics\n"))
ch = get_list(e->minute, FIRST_MINUTE, LAST_MINUTE,
PPC_NULL, ch, file);
if (ch == EOF) {
ecode = e_minute;
goto eof;
}
ch = get_list(e->hour, FIRST_HOUR, LAST_HOUR,
PPC_NULL, ch, file);
if (ch == EOF) {
ecode = e_hour;
goto eof;
}
if (ch == '*')
e->flags |= DOM_STAR;
ch = get_list(e->dom, FIRST_DOM, LAST_DOM,
PPC_NULL, ch, file);
if (ch == EOF) {
ecode = e_dom;
goto eof;
}
ch = get_list(e->month, FIRST_MONTH, LAST_MONTH,
MonthNames, ch, file);
if (ch == EOF) {
ecode = e_month;
goto eof;
}
if (ch == '*')
e->flags |= DOW_STAR;
ch = get_list(e->dow, FIRST_DOW, LAST_DOW,
DowNames, ch, file);
if (ch == EOF) {
ecode = e_dow;
goto eof;
}
}
if (bit_test(e->dow, 0) || bit_test(e->dow, 7)) {
bit_set(e->dow, 0);
bit_set(e->dow, 7);
}
unget_char(ch, file);
if (!uname) {
char *username = cmd;
char *s;
#ifdef LOGIN_CAP
login_cap_t *lc;
#endif
Debug(DPARS, ("load_entry()...about to parse username\n"))
ch = get_string(username, MAX_COMMAND, file, " \t");
Debug(DPARS, ("load_entry()...got %s\n",username))
if (ch == EOF) {
ecode = e_cmd;
goto eof;
}
#ifdef LOGIN_CAP
if ((s = strrchr(username, '/')) != NULL) {
*s = '\0';
e->class = strdup(s + 1);
if (e->class == NULL)
warn("strdup(\"%s\")", s + 1);
} else {
e->class = strdup(RESOURCE_RC);
if (e->class == NULL)
warn("strdup(\"%s\")", RESOURCE_RC);
}
if (e->class == NULL) {
ecode = e_mem;
goto eof;
}
if ((lc = login_getclass(e->class)) == NULL) {
ecode = e_class;
goto eof;
}
login_close(lc);
#endif
if ((s = strrchr(username, ':')) != NULL) {
*s = '\0';
strcpy(e->gname, s + 1);
}
strcpy(e->uname, username);
} else {
strcpy(e->uname, uname);
strcpy(e->gname, "");
}
Debug(DPARS, ("load_entry()...user: %s group:\n",e->uname,e->gname))
#ifdef LOGIN_CAP
Debug(DPARS, ("load_entry()...class %s\n",e->class))
#endif
e->envp = env_copy(envp);
if (e->envp == NULL) {
warn("env_copy");
ecode = e_mem;
goto eof;
}
if (!env_get("SHELL", e->envp)) {
prev_env = e->envp;
sprintf(envstr, "SHELL=%s", _PATH_BSHELL);
e->envp = env_set(e->envp, envstr);
if (e->envp == NULL) {
warn("env_set(%s)", envstr);
env_free(prev_env);
ecode = e_mem;
goto eof;
}
}
prev_env = e->envp;
if (!env_get("PATH", e->envp)) {
prev_env = e->envp;
sprintf(envstr, "PATH=%s", _PATH_DEFPATH);
e->envp = env_set(e->envp, envstr);
if (e->envp == NULL) {
warn("env_set(%s)", envstr);
env_free(prev_env);
ecode = e_mem;
goto eof;
}
}
prev_env = e->envp;
sprintf(envstr, "%s=%s", "LOGNAME", uname);
e->envp = env_set(e->envp, envstr);
if (e->envp == NULL) {
warn("env_set(%s)", envstr);
env_free(prev_env);
ecode = e_mem;
goto eof;
}
#if defined(BSD)
prev_env = e->envp;
sprintf(envstr, "%s=%s", "USER", uname);
e->envp = env_set(e->envp, envstr);
if (e->envp == NULL) {
warn("env_set(%s)", envstr);
env_free(prev_env);
ecode = e_mem;
goto eof;
}
#endif
Debug(DPARS, ("load_entry()...about to parse command\n"))
ch = get_string(cmd, MAX_COMMAND, file, "\n");
if (ch == EOF) {
ecode = e_cmd;
goto eof;
}
e->cmd = strdup(cmd);
if (e->cmd == NULL) {
warn("strdup(\"%s\")", cmd);
ecode = e_mem;
goto eof;
}
if( e->cmd[0] == '@' ) {
if( strncmp(e->cmd+1, "AppleNotOnBattery", 17) == 0 ) {
e->flags |= NOT_BATTERY;
e->cmd += 18;
for( ; isspace(e->cmd[0]); e->cmd++ );
}
}
Debug(DPARS, ("load_entry()...returning successfully\n"))
return e;
eof:
free_entry(e);
if (ecode != e_none && error_func)
(*error_func)(ecodes[(int)ecode]);
while (ch != EOF && ch != '\n')
ch = get_char(file);
return NULL;
}
static char
get_list(bits, low, high, names, ch, file)
bitstr_t *bits;
int low, high;
char *names[];
int ch;
FILE *file;
{
register int done;
Debug(DPARS|DEXT, ("get_list()...entered\n"))
bit_nclear(bits, 0, (high-low+1));
done = FALSE;
while (!done) {
ch = get_range(bits, low, high, names, ch, file);
if (ch == ',')
ch = get_char(file);
else
done = TRUE;
}
Skip_Nonblanks(ch, file)
Skip_Blanks(ch, file)
Debug(DPARS|DEXT, ("get_list()...exiting w/ %02x\n", ch))
return ch;
}
static char
get_range(bits, low, high, names, ch, file)
bitstr_t *bits;
int low, high;
char *names[];
int ch;
FILE *file;
{
register int i;
auto int num1, num2, num3;
Debug(DPARS|DEXT, ("get_range()...entering, exit won't show\n"))
if (ch == '*') {
num1 = low;
num2 = high;
ch = get_char(file);
if (ch == EOF)
return EOF;
} else {
if (EOF == (ch = get_number(&num1, low, names, ch, file)))
return EOF;
if (ch != '-') {
if (EOF == set_element(bits, low, high, num1))
return EOF;
return ch;
} else {
ch = get_char(file);
if (ch == EOF)
return EOF;
ch = get_number(&num2, low, names, ch, file);
if (ch == EOF)
return EOF;
}
}
if (ch == '/') {
ch = get_char(file);
if (ch == EOF)
return EOF;
ch = get_number(&num3, 0, PPC_NULL, ch, file);
if (ch == EOF)
return EOF;
} else {
num3 = 1;
}
for (i = num1; i <= num2; i += num3)
if (EOF == set_element(bits, low, high, i))
return EOF;
return ch;
}
static char
get_number(numptr, low, names, ch, file)
int *numptr;
int low;
char *names[];
int ch;
FILE *file;
{
char temp[MAX_TEMPSTR], *pc;
int len, i, all_digits;
pc = temp;
len = 0;
all_digits = TRUE;
while (isalnum(ch)) {
if (++len >= MAX_TEMPSTR)
return EOF;
*pc++ = ch;
if (!isdigit(ch))
all_digits = FALSE;
ch = get_char(file);
}
*pc = '\0';
if (names) {
for (i = 0; names[i] != NULL; i++) {
Debug(DPARS|DEXT,
("get_num, compare(%s,%s)\n", names[i], temp))
if (!strcasecmp(names[i], temp)) {
*numptr = i+low;
return ch;
}
}
}
if (all_digits) {
*numptr = atoi(temp);
return ch;
}
return EOF;
}
static int
set_element(bits, low, high, number)
bitstr_t *bits;
int low;
int high;
int number;
{
Debug(DPARS|DEXT, ("set_element(?,%d,%d,%d)\n", low, high, number))
if (number < low || number > high)
return EOF;
bit_set(bits, (number-low));
return OK;
}