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 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
|
/*************************************************
* Exim Monitor *
*************************************************/
/* Copyright (c) University of Cambridge 1995 - 1998 */
/* See the file NOTICE for conditions of use and distribution. */
#include "em_hdr.h"
/* This module contains functions to do with scanning exim's
queue and displaying the data therefrom. */
/*************************************************
* Static variables *
*************************************************/
static int queue_total = 0; /* number of items in queue */
/* Table for turning base-62 numbers into binary */
static char tab62[] =
{0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0-9 */
0,10,11,12,13,14,15,16,17,18,19,20, /* A-K */
21,22,23,24,25,26,27,28,29,30,31,32, /* L-W */
33,34,35, 0, 0, 0, 0, 0, /* X-Z */
0,36,37,38,39,40,41,42,43,44,45,46, /* a-k */
47,48,49,50,51,52,53,54,55,56,57,58, /* l-w */
59,60,61}; /* x-z */
/* Index for quickly finding things in the ordered queue. */
static queue_item *queue_index[queue_index_size];
/*************************************************
* Find/Create/Delete a destination *
*************************************************/
/* If the action is dest_noop, then just return item or NULL;
if it is dest_add, then add if not present, and return item;
if it is dest_remove, remove if present and return NULL. The
address is lowercased to start with, unless it begins with
"*", which it does for error messages. */
dest_item *find_dest(queue_item *q, char *name, int action)
{
dest_item *dd;
dest_item **d = &(q->destinations);
char *s = name;
if (*s != '*') while (*s) { *s = tolower(*s); s++; }
while (*d != NULL)
{
if (strcmp(name, (*d)->address) == 0)
{
if (action != dest_remove) return *d;
dd = *d;
*d = dd->next;
free(dd);
return NULL;
}
d = &((*d)->next);
}
if (action != dest_add) return NULL;
dd = (dest_item *)malloc(sizeof(dest_item) + (int)strlen(name));
strcpy(dd->address, name);
dd->next = NULL;
dd->parent = NULL;
*d = dd;
return dd;
}
/*************************************************
* Clean up a dead queue item *
*************************************************/
static void clean_up(queue_item *p)
{
dest_item *dd = p->destinations;
while (dd != NULL)
{
dest_item *next = dd->next;
free(dd);
dd = next;
}
if (p->sender != NULL) free(p->sender);
free(p);
}
/*************************************************
* Set up new queue item *
*************************************************/
static queue_item *set_up(char *name, int dir_char)
{
int i, rc, save_errno;
struct stat statdata;
char *p;
queue_item *q = (queue_item *)malloc(sizeof(queue_item));
char buffer[256];
/* Initialize the block */
q->next = q->prev = NULL;
q->destinations = NULL;
strcpy(q->name, name);
q->seen = TRUE;
q->frozen = FALSE;
q->dir_char = dir_char;
q->sender = NULL;
q->size = 0;
/* Read the header file from the spool; if there is a failure it
might mean inaccessibility as a result of protections.
A successful read will have caused sender_address to get set and
the recipients fields to be initialized. If there's a format error
in the headers, we can still display info from the envelope. */
message_size = 0;
message_subdir[0] = dir_char;
sprintf(buffer, "%s-H", name);
rc = spool_read_header(buffer, FALSE, TRUE);
save_errno = errno;
/* If we failed to read the envelope, compute the input time by
interpreting the id as a base-62 number. */
if (rc != spool_read_OK && rc != spool_read_hdrerror)
{
int t = 0;
for (i = 0; i < 6; i++) t = t * 62 + tab62[name[i] - '0'];
q->update_time = q->input_time = t;
}
/* Envelope read; get input time and sender. */
else
{
q->update_time = q->input_time = received_time;
/* Remove qualify_domain from sender address, and then save it. */
if ((p = strstric(sender_address+1, qualify_domain, FALSE)) != NULL &&
*(--p) == '@') *p = 0;
}
/* If we didn't read the whole header successfully, generate an error
message. If the envelope was read, this appears as a first recipient;
otherwise it sets set up in the sender field. */
if (rc != spool_read_OK)
{
char *msg;
if (save_errno == ERRNO_SPOOLFORMAT)
{
struct stat statbuf;
sprintf(big_buffer, "%s/input/%s", spool_directory, buffer);
if (stat(big_buffer, &statbuf) == 0)
msg = string_sprintf("*** Format error in spool file: size = %d ***",
statbuf.st_size);
else msg = string_sprintf("*** Format error in spool file ***");
}
else msg = string_sprintf("*** Cannot read spool file ***");
if (rc == spool_read_hdrerror)
{
(void)find_dest(q, msg, dest_add);
free(msg);
}
else
{
deliver_freeze = FALSE;
sender_address = msg;
}
}
/* Now set up the remaining data */
q->frozen = deliver_freeze;
if (user_null_sender && sender_address != NULL && sender_address[0] != 0)
{
q->sender = malloc((int)strlen(sender_address) + 6);
sprintf(q->sender, "<> (%s)", sender_address);
free(sender_address);
}
else q->sender = sender_address;
sender_address = NULL;
sprintf(buffer, "%s/input/%s/%s-D", spool_directory, message_subdir, name);
if (stat(buffer, &statdata) == 0) q->size = message_size + statdata.st_size;
/* Scan and process the recipients list, skipping any that have already
been delivered, and removing visible names. In the nonrecipients list,
domains are lower-cased. */
if (recipients_list != NULL)
{
for (i = 0; i < recipients_count; i++)
{
BOOL delivered = TRUE;
char *r = recipients_list[i];
if (tree_search(tree_nonrecipients, r) == NULL)
{
char *rr = r;
while (*rr != 0 && *rr != '@') rr++;
while (*rr != 0) { *rr = tolower(*rr); rr++; }
if (tree_search(tree_nonrecipients, r) == NULL)
delivered = FALSE;
}
if (!delivered)
{
if ((p = strstric(r+1, qualify_domain, FALSE)) != NULL &&
*(--p) == '@') *p = 0;
(void)find_dest(q, r, dest_add);
}
free(r);
}
free(recipients_list);
}
tree_free(tree_nonrecipients);
return q;
}
/*************************************************
* Find/Create a queue item *
*************************************************/
/* The queue is kept as a doubly-linked list, sorted by name. However,
to speed up searches, an index into the list is used. This is maintained
by the scan_spool_input function when it goes down the list throwing
out entries that are no longer needed. When the action is "add" and
we don't need to add, mark the found item as seen. */
#ifdef never
static void debug_queue(void)
{
int i;
int count = 0;
queue_item *p;
printf("\nqueue_total=%d\n", queue_total);
for (i = 0; i < queue_index_size; i++)
printf("index %d = %d %s\n", i, (int)(queue_index[i]),
(queue_index[i])->name);
printf("Queue is:\n");
p = queue_index[0];
while (p != NULL)
{
count++;
for (i = 0; i < queue_index_size; i++)
{
if (queue_index[i] == p) printf("count=%d index=%d\n", count, (int)p);
}
printf("%d %d %d %s\n", (int)p, (int)p->next, (int)p->prev, p->name);
p = p->next;
}
}
#endif
queue_item *find_queue(char *name, int action, int dir_char)
{
int first = 0;
int last = queue_index_size - 1;
int middle = (first + last)/2;
queue_item *p, *q, *qq;
/* Handle the empty queue as a special case. */
if (queue_total == 0)
{
if (action != queue_add) return NULL;
if ((qq = set_up(name, dir_char)) != NULL)
{
int i;
for (i = 0; i < queue_index_size; i++) queue_index[i] = qq;
queue_total++;
return qq;
}
return NULL;
}
/* Also handle insertion at the start or end of the queue
as special cases. */
if (strcmp(name, (queue_index[0])->name) < 0)
{
if (action != queue_add) return NULL;
if ((qq = set_up(name, dir_char)) != NULL)
{
qq->next = queue_index[0];
(queue_index[0])->prev = qq;
queue_index[0] = qq;
queue_total++;
return qq;
}
return NULL;
}
if (strcmp(name, (queue_index[queue_index_size-1])->name) > 0)
{
if (action != queue_add) return NULL;
if ((qq = set_up(name, dir_char)) != NULL)
{
qq->prev = queue_index[queue_index_size-1];
(queue_index[queue_index_size-1])->next = qq;
queue_index[queue_index_size-1] = qq;
queue_total++;
return qq;
}
return NULL;
}
/* Use binary chopping on the index to get a range of the queue to search
when the name is somewhere in the middle, if present. */
while (middle > first)
{
if (strcmp(name, (queue_index[middle])->name) >= 0) first = middle;
else last = middle;
middle = (first + last)/2;
}
/* Now search down the part of the queue in which the item must
lie if it exists. Both end points are inclusive - though in fact
the bottom one can only be = if it is the original bottom. */
p = queue_index[first];
q = queue_index[last];
for (;;)
{
int c = strcmp(name, p->name);
/* Already on queue; mark seen if required. */
if (c == 0)
{
if (action == queue_add) p->seen = TRUE;
return p;
}
/* Not on the queue; add an entry if required. Note that set-up might
fail (the file might vanish under our feet). Note also that we know
there is always a previous item to p because the end points are
inclusive. */
else if (c < 0)
{
if (action == queue_add)
{
if ((qq = set_up(name, dir_char)) != NULL)
{
qq->next = p;
qq->prev = p->prev;
p->prev->next = qq;
p->prev = qq;
queue_total++;
return qq;
}
}
return NULL;
}
/* Control should not reach here if p == q, because the name
is supposed to be <= the name of the bottom item. */
if (p == q) return NULL;
/* Else might be further down the queue; continue */
p = p->next;
}
/* Control should never reach here. */
}
/*************************************************
* Scan the exim spool directory *
*************************************************/
/* If we discover that there are subdirectories, set a flag so that the menu
code knows to look for them. We count the entries to set the value for the
queue stripchart, and set up data for the queue display window if the "full"
option is given. */
void scan_spool_input(int full)
{
int i;
int subptr;
int subdir_max = 1;
int count = 0;
int indexptr = 1;
queue_item *p;
struct dirent *ent;
DIR *dd;
char input_dir[256];
char subdirs[64];
subdirs[0] = 0;
stripchart_total[0] = 0;
sprintf(input_dir, "%s/input", spool_directory);
subptr = (int)strlen(input_dir);
input_dir[subptr+2] = 0; /* terminator for lengthened name */
/* Loop for each spool file on the queue - searching any subdirectories that
may exist. When initializing eximon, every file will have to be read. To show
there is progress, output a dot for each one to the standard output. */
for (i = 0; i < subdir_max; i++)
{
int subdirchar = subdirs[i]; /* 0 for main directory */
if (subdirchar != 0)
{
input_dir[subptr] = '/';
input_dir[subptr+1] = subdirchar;
}
dd = opendir(input_dir);
if (dd == NULL) continue;
while ((ent = readdir(dd)) != NULL)
{
char *name = ent->d_name;
int len = (int)strlen(name);
/* If we find a single alphameric sub-directory on the first
pass, add it to the list for subsequent scans, and remember that
we are dealing with a split directory. */
if (i == 0 && len == 1 && isalnum(*name))
{
subdirs[subdir_max++] = *name;
spool_is_split = TRUE;
continue;
}
/* Otherwise, if it is a header spool file, add it to the list */
if (len == SPOOL_NAME_LENGTH &&
name[SPOOL_NAME_LENGTH - 2] == '-' &&
name[SPOOL_NAME_LENGTH - 1] == 'H')
{
char basename[SPOOL_NAME_LENGTH];
stripchart_total[0]++;
if (!eximon_initialized) { printf("."); fflush(stdout); }
strcpy(basename, name);
basename[SPOOL_NAME_LENGTH - 2] = 0;
if (full) find_queue(basename, queue_add, subdirchar);
}
}
closedir(dd);
}
/* If simply counting the number, we are done; same if there are no
items in the in-store queue. */
if (!full || queue_total == 0) return;
/* Now scan the queue and remove any items that were not in the directory. At
the same time, set up the index pointers into the queue. Because we are
removing items, the total that we are comparing against isn't actually correct,
but in a long queue it won't make much difference, and in a short queue it
doesn't matter anyway!*/
p = queue_index[0];
while (p != NULL)
{
if (!p->seen)
{
queue_item *next = p->next;
if (p->prev == NULL) queue_index[0] = next;
else p->prev->next = next;
if (next == NULL)
{
int i;
queue_item *q = queue_index[queue_index_size-1];
for (i = queue_index_size - 1; i >= 0; i--)
if (queue_index[i] == q) queue_index[i] = p->prev;
}
else next->prev = p->prev;
clean_up(p);
queue_total--;
p = next;
}
else
{
if (++count > (queue_total * indexptr)/(queue_index_size-1))
{
queue_index[indexptr++] = p;
}
p->seen = FALSE; /* for next time */
p = p->next;
}
}
/* If a lot of messages have been removed at the bottom, we may not
have got the index all filled in yet. Make sure all the pointers
are legal. */
while (indexptr < queue_index_size - 1)
{
queue_index[indexptr++] = queue_index[queue_index_size-1];
}
}
/*************************************************
* Update the recipients list for a message *
*************************************************/
/* We read the spool file only if its update time differs from last time,
or if there is a journal file in existence. */
/* First, a local subroutine to scan the non-recipients tree and
remove any of them from the address list */
static void
scan_tree(queue_item *p, tree_node *tn)
{
if (tn != NULL)
{
if (tn->left != NULL) scan_tree(p, tn->left);
if (tn->right != NULL) scan_tree(p, tn->right);
(void)find_dest(p, tn->name, dest_remove);
}
}
/* The main function */
static void update_recipients(queue_item *p)
{
int i;
FILE *jread;
struct stat statdata;
char buffer[1024];
message_subdir[0] = p->dir_char;
sprintf(buffer, "%s/input/%s/%s-J", spool_directory, message_subdir, p->name);
jread = fopen(buffer, "r");
if (jread == NULL)
{
sprintf(buffer, "%s/input/%s/%s-H", spool_directory, message_subdir, p->name);
if (stat(buffer, &statdata) < 0 || p->update_time == statdata.st_mtime)
return;
}
/* Get the contents of the header file; if any problem, just give up. */
sprintf(buffer, "%s-H", p->name);
if (spool_read_header(buffer, FALSE, TRUE) != spool_read_OK) return;
/* If there's a journal file, add its contents to the non-recipients tree */
if (jread != NULL)
{
while (fgets(big_buffer, big_buffer_size, jread) != NULL)
{
int n = (int)strlen(big_buffer);
big_buffer[n-1] = 0;
tree_add_nonrecipient(big_buffer+1, big_buffer[0] == 'Y');
}
fclose(jread);
}
/* Scan and process the recipients list, removing any that have already
been delivered, and removing visible names. In the nonrecipients tree,
domains are lower cased. */
if (recipients_list != NULL)
{
for (i = 0; i < recipients_count; i++)
{
char *pp;
char *r = recipients_list[i];
tree_node *node = tree_search(tree_nonrecipients, r);
if (node == NULL)
{
char temp[256];
char *rr = temp;
strcpy(temp, r);
while (*rr != 0 && *rr != '@') rr++;
while (*rr != 0) { *rr = tolower(*rr); rr++; }
node = tree_search(tree_nonrecipients, temp);
}
if ((pp = strstric(r+1, qualify_domain, FALSE)) != NULL &&
*(--pp) == '@') *pp = 0;
if (node == NULL)
(void)find_dest(p, r, dest_add);
else
(void)find_dest(p, r, dest_remove);
free(r);
}
free(recipients_list);
}
/* We also need to scan the tree of non-recipients, which might
contain child addresses that are not in the recipients list, but
which may have got onto the address list as a result of eximon
noticing an == line in the log. Then free the tree, remember the
update time, and we are done. */
scan_tree(p, tree_nonrecipients);
tree_free(tree_nonrecipients);
p->update_time = statdata.st_mtime;
}
/*************************************************
* Display queue data *
*************************************************/
/* The present implementation simple re-writes the entire information each
time. Take some care to keep the scrolled position as it previously was, but,
if it was at the bottom, keep it at the bottom. Take note of any hide list, and
time out the entries as appropriate. */
void
queue_display(void)
{
int now = (int)time(NULL);
queue_item *p = queue_index[0];
if (menu_is_up) return; /* Avoid nasty interactions */
text_empty(queue_widget);
while (p != NULL)
{
int count = 1;
dest_item *dd, *ddd;
char u = 'm';
int t = (now - p->input_time)/60; /* minutes on queue */
if (t > 90)
{
u = 'h';
t = (t + 30)/60;
if (t > 72)
{
u = 'd';
t = (t + 12)/24;
}
}
update_recipients(p); /* update destinations */
/* Can't set this earlier, as header data may change things. */
dd = p->destinations;
/* Check to see if this message is on the hide list; if any hide
item has timed out, remove it from the list. Hide if all destinations
are on the hide list. */
for (ddd = dd; ddd != NULL; ddd = ddd->next)
{
skip_item *sk;
skip_item **skp;
int len_address;
if (ddd->address[0] == '*') break;
len_address = (int)strlen(ddd->address);
for (skp = &queue_skip; ; skp = &(sk->next))
{
int len_skip;
sk = *skp;
while (sk != NULL && now >= sk->reveal)
{
*skp = sk->next;
free(sk);
sk = *skp;
if (queue_skip == NULL)
{
XtDestroyWidget(unhide_widget);
unhide_widget = NULL;
}
}
if (sk == NULL) break;
/* If this address matches the skip item, break (sk != NULL) */
len_skip = (int)strlen(sk->text);
if (len_skip <= len_address &&
strcmp(ddd->address + len_address - len_skip, sk->text) == 0)
break;
}
if (sk == NULL) break;
}
if (ddd != NULL)
{
text_showf(queue_widget, "%c%2d%c %s %s %-8s %s%s%s",
(p->frozen)? '*' : ' ',
t, u,
string_format_size(p->size, big_buffer),
p->name,
(p->sender == NULL)? " " :
(p->sender[0] == 0)? "<> " : p->sender,
(dd == NULL || dd->address[0] == '*')? "" : "<",
(dd == NULL)? "" : dd->address,
(dd == NULL || dd->address[0] == '*')? "" : ">");
if (dd != NULL && dd->parent != NULL && dd->parent->address[0] != '*')
text_showf(queue_widget, " parent <%s>", dd->parent->address);
text_show(queue_widget, "\n");
if (dd != NULL) dd = dd->next;
while (dd != NULL && count++ < queue_max_addresses)
{
text_showf(queue_widget, " <%s>",
dd->address);
if (dd->parent != NULL && dd->parent->address[0] != '*')
text_showf(queue_widget, " parent <%s>", dd->parent->address);
text_show(queue_widget, "\n");
dd = dd->next;
}
if (dd != NULL)
text_showf(queue_widget, " ...\n");
}
p = p->next;
}
text_position(queue_widget, 999999);
}
/* End of em_queue.c */
|