1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
|
/*
Anacron - run commands periodically
Copyright (C) 1998 Itai Tzur <itzur@actcom.co.il>
Copyright (C) 1999 Sean 'Shaleh' Perry <shaleh@debian.org>
Copyright (C) 2004 Pascal Hakim <pasc@redellipse.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
The GNU General Public License can also be found in the file
`COPYING' that comes with the Anacron source distribution.
*/
/* /etc/anacrontab parsing, and job sorting
*/
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <obstack.h>
#include <limits.h>
#include <fnmatch.h>
#include <unistd.h>
#include <signal.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include "global.h"
#include "matchrx.h"
static struct obstack input_o; /* holds input line */
static struct obstack tab_o; /* holds processed data read from anacrontab */
static FILE *tab;
job_rec **job_array;
int njobs; /* number of jobs to run */
static int jobs_read; /* number of jobs read */
static int line_num; /* current line in anacrontab */
static job_rec *last_job_rec; /* last job stored in memory, at the moment */
static env_rec *last_env_rec; /* last environment assignment stored */
extern job_rec sen_job_rec; /* sentinel job, indicates end of list */
/* internal 'errno' lookup table */
static int err_val = 0;
#define ERR_OUT_OF_RANGE 1
#define ERR_BAD_HASH 2
#define ERR_UNKNOWN_NAMED 3
#define ERR_INVALID_SYNTAX 4
static const char * err_msgs[] = {
"No error",
"%s: number out of range on line %d, skipping",
"%s: invalid hash information on line %d, skipping. Use -H to generate:\n"
"$ anacron -H /usr/sbin/anacron\n"
"Hash for anacrontab:\n"
"AAAAAAABKCDX8ts7Ik86X-Q2VTTTxku_iT9TKb6r1pjTxl-5OWTQHS91c3Ivc2Jpbi9hbmFjcm9u",
"%s: Unknown named period on line %d, skipping",
"Invalid syntax in %s on line %d - skipping this line",
};
static char data[4096] = {0};
/* some definitions for the obstack macros */
#define obstack_chunk_alloc xmalloc
#define obstack_chunk_free free
static void
setup_hash_index(char *path, job_rec *jr, char *id)
{
jr->ident = id;
jr->path_to_hash = path;
}
static void *
xmalloc (size_t size)
/* Just like standard malloc(), only never returns NULL. */
{
void * ptr;
ptr = malloc(size);
if (ptr == NULL)
die("Memory exhausted");
return ptr;
}
static int
conv2int(const char *s)
/* Return the int or -1 on over/under-flow
*/
{
long l;
errno = 0;
l = strtol(s, NULL, 10);
/* we use negative as error, so I am really returning unsigned int */
if (errno == ERANGE || l < 0 || l > INT_MAX) return - 1;
return l;
}
static char *
read_tab_line ()
/* Read one line and return a pointer to it.
Return NULL if no more lines.
*/
{
int c, prev=0;
if (feof(tab)) return NULL;
while (1)
{
c = getc(tab);
if ((c == '\n' && prev != '\\') || c == EOF)
{
if (0 != prev) obstack_1grow(&input_o, prev);
break;
}
if ('\\' != prev && 0 != prev && '\n' != prev) obstack_1grow(&input_o, prev);
else if ('\n' == prev) obstack_1grow(&input_o, ' ');
prev = c;
}
if (ferror(tab)) die_e("Error reading %s", anacrontab);
obstack_1grow(&input_o, '\0');
return obstack_finish(&input_o);
}
static int
job_arg_num(const char *ident)
/* Return the command-line-argument number referring to this job-identifier.
* If it isn't specified, return -1.
*/
{
int i, r;
for (i = 0; i < nargs; i++)
{
r = fnmatch(args[i], ident, 0);
if (r == 0) return i;
if (r != FNM_NOMATCH) die("fnmatch() error");
}
return - 1;
}
static void
register_env(const char *env_var, const char *value)
/* Store the environment assignment "env_var"="value" */
{
env_rec *er;
int var_len, val_len;
var_len = strlen(env_var);
val_len = strlen(value);
er = obstack_alloc(&tab_o, sizeof(env_rec));
er->assign = obstack_alloc(&tab_o, var_len + 1 + val_len + 1);
strcpy(er->assign, env_var);
er->assign[var_len] = '=';
strcpy(er->assign + var_len + 1, value);
er->assign[var_len + 1 + val_len] = 0;
if (last_env_rec != NULL) last_env_rec->next = er;
else first_env_rec = er;
last_env_rec = er;
xdebug("on line %d: %s", line_num, er->assign);
}
off_t
parse_hash_info(char *base64, unsigned char *hash, char **path)
{
uint64_t hash_file_size = 0;
err_val = 0;
if(base64 == NULL || path == NULL || hash == NULL)
{
xdebug("No hash information on line %d", line_num);
return 0;
}
/* Max base64 decode size is length * 3 / 4 - padding bytes */
int max_b64_len = strlen(base64) * 3 / 4;
if(max_b64_len <= MIN_HASH_INFO)
{
err_val = ERR_BAD_HASH;
return -1;
}
*path = obstack_alloc(&tab_o, max_b64_len);
int decoded_size = b64_pton(base64, (unsigned char *)*path, max_b64_len);
if (decoded_size == -1 || decoded_size > max_b64_len)
{
err_val = ERR_BAD_HASH;
return -1;
}
memcpy(&hash_file_size, *path, sizeof(uint64_t));
hash_file_size = be64toh(hash_file_size);
memcpy(hash, *path + sizeof(uint64_t), HASH_SIZE);
memmove(*path,
*path + sizeof(uint64_t) + HASH_SIZE,
decoded_size - (sizeof(uint64_t) + HASH_SIZE));
(*path)[decoded_size - (sizeof(uint64_t) + HASH_SIZE)] = '\0';
return hash_file_size;
}
static job_rec *
register_job_internal(int period, int named_period, int delay, char *ident,
char *command, unsigned char *hash, size_t hash_file_size, char *path)
{
job_rec *jr = obstack_alloc(&tab_o, sizeof(job_rec));
jr->period = period;
jr->named_period = named_period;
jr->delay = delay;
jr->tab_line = line_num;
jr->ident = ident;
jr->command = command;
jr->job_pid = jr->mailer_pid = 0;
if (last_job_rec != NULL) last_job_rec->next = jr;
else first_job_rec = jr;
last_job_rec = jr;
jr->prev_env_rec = last_env_rec;
jr->next = &sen_job_rec;
jr->path_to_hash = NULL;
jr->mail_header_size = 0;
if(hash_file_size > 0)
{
jr->hash = obstack_alloc(&tab_o, HASH_SIZE);
memcpy(jr->hash, hash, HASH_SIZE);
jr->hash_file_size = hash_file_size;
if(path != NULL)
{
jr->path_to_hash = path;
}
}
return jr;
}
static void
register_job(const char *periods, const char *delays,
const char *ident, char *command, char *base64)
/* Store a job definition */
{
int period, delay;
job_rec *jr;
int ident_len, command_len;
unsigned char hash[HASH_SIZE] = {0};
off_t hash_file_size = 0;
char *path = NULL;
ident_len = strlen(ident);
command_len = strlen(command);
jobs_read++;
period = conv2int(periods);
delay = conv2int(delays);
if (period < 0 || delay < 0)
{
err_val = ERR_OUT_OF_RANGE;
return;
}
hash_file_size = parse_hash_info(base64, hash, &path);
if(hash_file_size == -1)
{
return;
}
jr = register_job_internal(period, 0, delay,
obstack_alloc(&tab_o, ident_len + 1),
obstack_alloc(&tab_o, command_len + 1),
hash, hash_file_size, path);
strcpy(jr->ident, ident);
jr->arg_num = job_arg_num(ident);
strcpy(jr->command, command);
xdebug("Read job - period=%d, delay=%d, ident=%s, command=%s",
jr->period, jr->delay, jr->ident, jr->command);
}
static void
register_period_job(const char *periods, const char *delays,
const char *ident, char *command, char *base64)
/* Store a job definition with a named period */
{
int delay, named_period;
job_rec *jr = NULL;
int ident_len, command_len;
unsigned char hash[HASH_SIZE] ={0};
off_t hash_file_size = 0;
char *path = NULL;
ident_len = strlen(ident);
command_len = strlen(command);
jobs_read++;
delay = conv2int(delays);
if (delay < 0)
{
err_val = ERR_OUT_OF_RANGE;
return;
}
hash_file_size = parse_hash_info(base64, hash, &path);
if(hash_file_size == -1)
{
return;
}
if (!strncmp ("@monthly", periods, 7)) {
named_period = 1;
} else if (!strncmp("@yearly", periods, 7)) {
named_period = 2;
} else if (!strncmp("@weekly", periods, 7)) {
named_period = 3;
} else if (!strncmp("@daily", periods, 7)) {
named_period = 4;
} else {
err_val = ERR_UNKNOWN_NAMED;
return;
}
jr = register_job_internal(0, named_period, delay,
obstack_alloc(&tab_o, ident_len + 1),
obstack_alloc(&tab_o, command_len + 1),
hash, hash_file_size, path);
strcpy(jr->ident, ident);
jr->arg_num = job_arg_num(ident);
strcpy(jr->command, command);
xdebug("Read job - period %d, delay=%d, ident=%s, command=%s",
jr->named_period, jr->delay, jr->ident, jr->command);
}
static void
parse_tab_line(char *line)
{
int r;
char *env_var;
char *value;
char *periods;
char *delays;
char *ident;
char *hash=NULL;
char *command;
char *wrapper;
err_val = 0;
/* an empty line? */
r = match_rx("^[ \t]*($|#)", line, 0);
if (r == -1) goto reg_err;
if (r)
{
xdebug("line %d empty", line_num);
return;
}
/* an environment assignment? */
r = match_rx("^[ \t]*([^ \t=]+)[ \t]*=(.*)$", line, 2,
&env_var, &value);
if (r == -1) goto reg_err;
if (r)
{
register_env(env_var, value);
return;
}
/* a job? */
r = match_rx("^[ \t]*([[:digit:]]+)[ \t]+([[:digit:]]+)[ \t]+"
"([^ \t/]+)(\\/([-0-9A-Za-z_=]+))?[ \t]+([^ \t].*)$",
line, 6, &periods, &delays, &ident, &wrapper, &hash, &command);
if (r == -1) goto reg_err;
if (r)
{
register_job(periods, delays, ident, command, hash);
return;
}
/* A period job? */
r = match_rx("^[ \t]*(@[^ \t]+)[ \t]+([[:digit:]]+)[ \t]+"
"([^ \t/]+)(\\/([-0-9A-Za-z_=]+))?[ \t]+([^ \t].*)$",
line, 6, &periods, &delays, &ident, &wrapper, &hash, &command);
if (r == -1) goto reg_err;
if (r)
{
register_period_job(periods, delays, ident, command, hash);
return;
}
err_val = ERR_INVALID_SYNTAX;
return;
reg_err:
die("Regex error reading %s", anacrontab);
}
static env_rec *fr = NULL;
static char name[PATH_MAX];
static int
parse_line(char *line, char **path, unsigned char *hash)
{
off_t r;
err_val = 0;
r = parse_hash_info(line, hash, path);
return r;
}
static void
store_hash(job_rec *jr, char *assign, unsigned char *hash, int size)
{
if(strlen(assign) == size)
{
env_rec *er;
int var_len = strlen(assign);
er = obstack_alloc(&tab_o, sizeof(env_rec));
er->assign = assign;
er->assign[size] = '=';
memcpy(er->assign + var_len + 1, hash, HASH_SIZE);
er->assign[var_len + 1 + HASH_SIZE] = 0;
if (last_env_rec != NULL) last_env_rec->next = er;
else first_env_rec = er;
last_env_rec = er;
}
else
{
if (last_job_rec != NULL) last_job_rec->next = jr;
else first_job_rec = jr;
last_job_rec = jr;
jr->next = &sen_job_rec;
jr->hash = obstack_alloc(&tab_o, HASH_SIZE);
memcpy(jr->hash, hash, HASH_SIZE);
}
}
static void
env_hash_line(job_rec *jr, char *env)
{
unsigned char hash[HASH_SIZE] = {0};
int r =0;
char *assign = NULL;
/* loop through env and check each line for info */
if(env == NULL) return;
char *line = env;
do
{
if ((r = parse_line(line, &assign, hash)) == -1 && err_val > 0)
{
snprintf(data, sizeof(data), err_msgs[err_val], anacrontab, line_num);
xdebug("Invalid env %s", data);
}
line = strchr(line, '\n');
if(line != NULL) line++;
}
while(r == -1 && line != NULL);
if(r > 0)
{
store_hash(jr, assign, hash, r);
}
}
static void
check_hash_env(void *nx, char *ind, int *init)
{
context *ctx = (context *)nx;
int n = 0;
int delay = xinit(ctx, init);
job_rec *jr = obstack_alloc(&tab_o, sizeof(job_rec));
if(jr == NULL) return;
memset(jr, 0, sizeof(job_rec));
/* Check for hash file */
setup_hash_index(ind, jr, "");
jr->hash_file_size = DEFAULT_SIZE;
if (check_exists(jr) != 1)
{
goto done;
}
env_hash_line(jr, data);
job_rec *next = jr->next;
if (next == NULL || jr->path_to_hash == NULL)
{
goto done;
}
n++;
jr->next = nx;
jr->mail_header_size = 0;
*next = *jr;
done:
xreset(ctx, delay, n);
first_job_rec = &sen_job_rec;
last_job_rec = NULL;
first_env_rec = NULL;
fr = NULL;
}
static void
check_default_env(char *name, int *n)
{
if (fr != NULL)
{
check_hash_env(fr, name, n);
}
}
static void
setup_tab(int cwd)
{
/* Open the anacrontab file */
if (fchdir (cwd)) die_e("Can't fchdir to %s", cwd);
tab = fopen(anacrontab, "r");
if (chdir(spooldir)) die_e("Can't chdir to %s", spooldir);
if (tab == NULL) die_e("Error opening %s", anacrontab);
/* Initialize the obstacks */
obstack_init(&input_o);
obstack_init(&tab_o);
fr = first_env_rec;
}
void
read_tab(int cwd)
/* Read the anacrontab file into memory */
{
char *tab_line;
/* Initialize temp file names */
if (-1 == init_temp_file(name, sizeof(name))) die_e("Can't init temp_file state");
/* Open anancrontab and setup for parsing */
setup_tab(cwd);
first_job_rec = &sen_job_rec;
last_job_rec = NULL;
first_env_rec = last_env_rec = NULL;
jobs_read = 0;
line_num = 0;
/* Check for default env */
check_default_env(name, &njobs);
while ((tab_line = read_tab_line()) != NULL)
{
line_num++;
parse_tab_line(tab_line);
if(err_val > 0) complain(err_msgs[err_val], anacrontab, line_num);
obstack_free(&input_o, tab_line);
}
if (fclose(tab)) die_e("Error closing %s", anacrontab);
}
static int
execution_order(const job_rec **job1, const job_rec **job2)
/* Comparison function for sorting the jobs.
*/
{
int d;
d = (*job1)->arg_num - (*job2)->arg_num;
if (d != 0 && now) return d;
d = (*job1)->delay - (*job2)->delay;
if (d != 0) return d;
d = (*job1)->tab_line - (*job2)->tab_line;
return d;
}
int
check_exists(const job_rec *jr)
{
struct stat st;
if(stat(jr->path_to_hash, &st) != 0)
{
explain("Job `%s' hash file %s does not exist - skipping",
jr->ident, jr->path_to_hash);
return -1;
}
if(st.st_size != jr->hash_file_size)
{
explain("Job `%s' hash file %s size does not match expected - skipping",
jr->ident, jr->path_to_hash);
return -1;
}
return 1;
}
static int
valid_job(job_rec * jr)
{
if(consider_job(jr) && (jr->path_to_hash == NULL || check_exists(jr) == 1))
{
return 1;
}
return 0;
}
void
arrange_jobs()
/* Make an array of pointers to jobs that are going to be executed,
* and arrange them in the order of execution.
* Also lock these jobs.
*/
{
int nsort = 0;
job_rec *j;
j = first_job_rec;
while (j != &sen_job_rec)
{
if (j->arg_num != -1 && (update_only || testing_only || list_only || valid_job(j)))
{
njobs++;
obstack_grow(&tab_o, &j, sizeof(j));
}
j = j->next;
}
nsort = obstack_object_size(&tab_o)/sizeof(j);
/* add sentinel to indicate end of list */
obstack_grow(&tab_o, &j, sizeof(j));
job_array = obstack_finish(&tab_o);
/* sort the jobs */
qsort(job_array, nsort, sizeof(*job_array),
(int (*)(const void *, const void *))execution_order);
}
static int
ident_order(const job_rec **job1, const job_rec **job2)
{
int d;
d = strcmp((*job1)->ident, (*job2)->ident);
return d;
}
void
list_obsolete_jobs()
{
DIR *dir;
struct dirent *ent;
struct stat statbuf;
job_rec key;
job_rec *keyp;
job_rec *res;
/* sort the jobs */
qsort(job_array, njobs, sizeof(*job_array),
(int (*)(const void *, const void *))ident_order);
dir = opendir(".");
if (!dir) die_e("Can't opendir %s", spooldir);
errno = 0;
keyp = &key;
while ((ent = readdir(dir)) != NULL)
{
if (stat(ent->d_name, &statbuf))
{
complain_e("Can't stat %s", ent->d_name);
errno = 0;
continue;
}
if (!S_ISREG(statbuf.st_mode)) continue;
key.ident = ent->d_name;
res = bsearch(&keyp, job_array, njobs, sizeof(*job_array),
(int (*)(const void *, const void *))ident_order);
if (!res) puts(ent->d_name);
}
if (errno) die_e("Error reading %s", spooldir);
closedir(dir);
}
|