1 # include "sendmail.h" 2 # include <sys/stat.h> 3 # include <dir.h> 4 # include <signal.h> 5 # include <errno.h> 6 7 # ifndef QUEUE 8 SCCSID(@(#)queue.c 3.60 01/01/83 (no queueing)); 9 # else QUEUE 10 11 SCCSID(@(#)queue.c 3.60 01/01/83); 12 13 /* 14 ** Work queue. 15 */ 16 17 struct work 18 { 19 char *w_name; /* name of control file */ 20 long w_pri; /* priority of message, see below */ 21 struct work *w_next; /* next in queue */ 22 }; 23 24 typedef struct work WORK; 25 26 WORK *WorkQ; /* queue of things to be done */ 27 /* 28 ** QUEUEUP -- queue a message up for future transmission. 29 ** 30 ** Parameters: 31 ** e -- the envelope to queue up. 32 ** queueall -- if TRUE, queue all addresses, rather than 33 ** just those with the QQUEUEUP flag set. 34 ** announce -- if TRUE, tell when you are queueing up. 35 ** 36 ** Returns: 37 ** none. 38 ** 39 ** Side Effects: 40 ** The current request are saved in a control file. 41 */ 42 43 queueup(e, queueall, announce) 44 register ENVELOPE *e; 45 bool queueall; 46 bool announce; 47 { 48 char *tf; 49 char *qf; 50 char buf[MAXLINE]; 51 register FILE *tfp; 52 register HDR *h; 53 register ADDRESS *q; 54 55 /* 56 ** Create control file. 57 */ 58 59 tf = newstr(queuename(e, 't')); 60 tfp = fopen(tf, "w"); 61 if (tfp == NULL) 62 { 63 syserr("queueup: cannot create temp file %s", tf); 64 return; 65 } 66 (void) chmod(tf, FileMode); 67 68 # ifdef DEBUG 69 if (tTd(40, 1)) 70 printf("queueing in %s\n", tf); 71 # endif DEBUG 72 73 /* 74 ** If there is no data file yet, create one. 75 */ 76 77 if (e->e_df == NULL) 78 { 79 register FILE *dfp; 80 extern putbody(); 81 82 e->e_df = newstr(queuename(e, 'd')); 83 dfp = fopen(e->e_df, "w"); 84 if (dfp == NULL) 85 { 86 syserr("queueup: cannot create %s", e->e_df); 87 (void) fclose(tfp); 88 return; 89 } 90 (void) chmod(e->e_df, FileMode); 91 (*e->e_putbody)(dfp, ProgMailer, FALSE, e, FALSE); 92 (void) fclose(dfp); 93 e->e_putbody = putbody; 94 } 95 96 /* 97 ** Output future work requests. 98 ** Priority should be first, since it is read by orderq. 99 */ 100 101 /* output message priority */ 102 fprintf(tfp, "P%ld\n", e->e_msgpriority); 103 104 /* output creation time */ 105 fprintf(tfp, "T%ld\n", e->e_ctime); 106 107 /* output name of data file */ 108 fprintf(tfp, "D%s\n", e->e_df); 109 110 /* output name of sender */ 111 fprintf(tfp, "S%s\n", e->e_from.q_paddr); 112 113 /* output list of recipient addresses */ 114 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 115 { 116 if (queueall ? !bitset(QDONTSEND, q->q_flags) : 117 bitset(QQUEUEUP, q->q_flags)) 118 { 119 fprintf(tfp, "R%s\n", q->q_paddr); 120 if (announce) 121 { 122 e->e_to = q->q_paddr; 123 message(Arpa_Info, "queued"); 124 if (LogLevel > 4) 125 logdelivery("queued"); 126 e->e_to = NULL; 127 } 128 #ifdef DEBUG 129 if (tTd(40, 1)) 130 { 131 printf("queueing "); 132 printaddr(q, FALSE); 133 } 134 #endif DEBUG 135 } 136 } 137 138 /* 139 ** Output headers for this message. 140 ** Expand macros completely here. Queue run will deal with 141 ** everything as absolute headers. 142 ** All headers that must be relative to the recipient 143 ** can be cracked later. 144 */ 145 146 define('g', "$f", e); 147 for (h = e->e_header; h != NULL; h = h->h_link) 148 { 149 if (h->h_value == NULL || h->h_value[0] == '\0') 150 continue; 151 fprintf(tfp, "H"); 152 if (h->h_mflags != 0 && bitset(H_CHECK|H_ACHECK, h->h_flags)) 153 mfdecode(h->h_mflags, tfp); 154 if (bitset(H_DEFAULT, h->h_flags)) 155 { 156 (void) expand(h->h_value, buf, &buf[sizeof buf], e); 157 fprintf(tfp, "%s: %s\n", h->h_field, buf); 158 } 159 else if (bitset(H_FROM|H_RCPT, h->h_flags)) 160 { 161 commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags), 162 (MAILER *) NULL, FALSE); 163 } 164 else 165 fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 166 } 167 168 /* 169 ** Clean up. 170 */ 171 172 (void) fclose(tfp); 173 qf = queuename(e, 'q'); 174 holdsigs(); 175 (void) unlink(qf); 176 if (link(tf, qf) < 0) 177 syserr("cannot link(%s, %s), df=%s", tf, qf, e->e_df); 178 else 179 (void) unlink(tf); 180 rlsesigs(); 181 182 # ifdef LOG 183 /* save log info */ 184 if (LogLevel > 15) 185 syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 186 # endif LOG 187 } 188 /* 189 ** RUNQUEUE -- run the jobs in the queue. 190 ** 191 ** Gets the stuff out of the queue in some presumably logical 192 ** order and processes them. 193 ** 194 ** Parameters: 195 ** none. 196 ** 197 ** Returns: 198 ** none. 199 ** 200 ** Side Effects: 201 ** runs things in the mail queue. 202 */ 203 204 runqueue(forkflag) 205 bool forkflag; 206 { 207 /* 208 ** See if we want to go off and do other useful work. 209 */ 210 211 if (forkflag) 212 { 213 int pid; 214 215 pid = dofork(); 216 if (pid != 0) 217 { 218 /* parent -- pick up intermediate zombie */ 219 (void) waitfor(pid); 220 if (QueueIntvl != 0) 221 (void) setevent(QueueIntvl, runqueue, TRUE); 222 return; 223 } 224 /* child -- double fork */ 225 if (fork() != 0) 226 exit(EX_OK); 227 } 228 # ifdef LOG 229 if (LogLevel > 11) 230 syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid()); 231 # endif LOG 232 233 /* 234 ** Start making passes through the queue. 235 ** First, read and sort the entire queue. 236 ** Then, process the work in that order. 237 ** But if you take too long, start over. 238 */ 239 240 /* order the existing work requests */ 241 orderq(); 242 243 /* process them once at a time */ 244 while (WorkQ != NULL) 245 { 246 WORK *w = WorkQ; 247 248 WorkQ = WorkQ->w_next; 249 dowork(w); 250 free(w->w_name); 251 free((char *) w); 252 } 253 finis(); 254 } 255 /* 256 ** ORDERQ -- order the work queue. 257 ** 258 ** Parameters: 259 ** none. 260 ** 261 ** Returns: 262 ** none. 263 ** 264 ** Side Effects: 265 ** Sets WorkQ to the queue of available work, in order. 266 */ 267 268 # define WLSIZE 120 /* max size of worklist per sort */ 269 270 orderq() 271 { 272 register struct direct *d; 273 register WORK *w; 274 register WORK **wp; /* parent of w */ 275 DIR *f; 276 register int i; 277 WORK wlist[WLSIZE]; 278 int wn = 0; 279 extern workcmpf(); 280 281 /* clear out old WorkQ */ 282 for (w = WorkQ; w != NULL; ) 283 { 284 register WORK *nw = w->w_next; 285 286 WorkQ = nw; 287 free(w->w_name); 288 free((char *) w); 289 w = nw; 290 } 291 292 /* open the queue directory */ 293 f = opendir("."); 294 if (f == NULL) 295 { 296 syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 297 return; 298 } 299 300 /* 301 ** Read the work directory. 302 */ 303 304 while (wn < WLSIZE && (d = readdir(f)) != NULL) 305 { 306 FILE *cf; 307 char lbuf[MAXNAME]; 308 309 /* is this an interesting entry? */ 310 if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 311 continue; 312 313 /* yes -- open control file */ 314 cf = fopen(d->d_name, "r"); 315 if (cf == NULL) 316 { 317 /* this may be some random person sending hir msgs */ 318 /* syserr("orderq: cannot open %s", cbuf); */ 319 errno = 0; 320 continue; 321 } 322 wlist[wn].w_name = newstr(d->d_name); 323 324 /* extract useful information */ 325 while (fgets(lbuf, sizeof lbuf, cf) != NULL) 326 { 327 if (lbuf[0] == 'P') 328 { 329 (void) sscanf(&lbuf[1], "%ld", &wlist[wn].w_pri); 330 break; 331 } 332 } 333 wn++; 334 (void) fclose(cf); 335 } 336 (void) closedir(f); 337 338 /* 339 ** Sort the work directory. 340 */ 341 342 qsort(wlist, wn, sizeof *wlist, workcmpf); 343 344 /* 345 ** Convert the work list into canonical form. 346 ** Should be turning it into a list of envelopes here perhaps. 347 */ 348 349 wp = &WorkQ; 350 for (i = 0; i < wn; i++) 351 { 352 w = (WORK *) xalloc(sizeof *w); 353 w->w_name = wlist[i].w_name; 354 w->w_pri = wlist[i].w_pri; 355 w->w_next = NULL; 356 *wp = w; 357 wp = &w->w_next; 358 } 359 360 # ifdef DEBUG 361 if (tTd(40, 1)) 362 { 363 for (w = WorkQ; w != NULL; w = w->w_next) 364 printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 365 } 366 # endif DEBUG 367 } 368 /* 369 ** WORKCMPF -- compare function for ordering work. 370 ** 371 ** Parameters: 372 ** a -- the first argument. 373 ** b -- the second argument. 374 ** 375 ** Returns: 376 ** -1 if a < b 377 ** 0 if a == b 378 ** 1 if a > b 379 ** 380 ** Side Effects: 381 ** none. 382 */ 383 384 # define PRIFACT 1800 /* bytes each priority point is worth */ 385 386 workcmpf(a, b) 387 register WORK *a; 388 register WORK *b; 389 { 390 if (a->w_pri == b->w_pri) 391 return (0); 392 else if (a->w_pri > b->w_pri) 393 return (1); 394 else 395 return (-1); 396 } 397 /* 398 ** DOWORK -- do a work request. 399 ** 400 ** Parameters: 401 ** w -- the work request to be satisfied. 402 ** 403 ** Returns: 404 ** none. 405 ** 406 ** Side Effects: 407 ** The work request is satisfied if possible. 408 */ 409 410 dowork(w) 411 register WORK *w; 412 { 413 register int i; 414 415 # ifdef DEBUG 416 if (tTd(40, 1)) 417 printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 418 # endif DEBUG 419 420 /* 421 ** Fork for work. 422 */ 423 424 i = fork(); 425 if (i < 0) 426 { 427 syserr("dowork: cannot fork"); 428 return; 429 } 430 431 if (i == 0) 432 { 433 /* 434 ** CHILD 435 ** Lock the control file to avoid duplicate deliveries. 436 ** Then run the file as though we had just read it. 437 ** We save an idea of the temporary name so we 438 ** can recover on interrupt. 439 */ 440 441 /* set basic modes, etc. */ 442 (void) alarm(0); 443 CurEnv->e_flags &= ~EF_FATALERRS; 444 QueueRun = TRUE; 445 SendMode = SM_DELIVER; 446 ErrorMode = EM_MAIL; 447 CurEnv->e_id = &w->w_name[2]; 448 # ifdef LOG 449 if (LogLevel > 11) 450 syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id, 451 getpid()); 452 # endif LOG 453 454 /* don't use the headers from sendmail.cf... */ 455 CurEnv->e_header = NULL; 456 (void) chompheader("from: $q", TRUE); 457 458 /* create the link to the control file during processing */ 459 if (link(w->w_name, queuename(CurEnv, 'l')) < 0) 460 { 461 /* being processed by another queuer */ 462 # ifdef LOG 463 if (LogLevel > 4) 464 syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id); 465 # endif LOG 466 exit(EX_OK); 467 } 468 469 /* do basic system initialization */ 470 initsys(); 471 472 /* read the queue control file */ 473 readqf(CurEnv, TRUE); 474 CurEnv->e_flags |= EF_INQUEUE; 475 eatheader(CurEnv); 476 477 /* do the delivery */ 478 if (!bitset(EF_FATALERRS, CurEnv->e_flags)) 479 sendall(CurEnv, SM_DELIVER); 480 481 /* if still not sent, perhaps we should time out.... */ 482 # ifdef DEBUG 483 if (tTd(40, 3)) 484 printf("curtime=%ld, TimeOut=%ld\n", curtime(), 485 CurEnv->e_ctime + TimeOut); 486 # endif DEBUG 487 if (curtime() > CurEnv->e_ctime + TimeOut) 488 CurEnv->e_flags |= EF_TIMEOUT; 489 490 /* finish up and exit */ 491 finis(); 492 } 493 494 /* 495 ** Parent -- pick up results. 496 */ 497 498 errno = 0; 499 (void) waitfor(i); 500 } 501 /* 502 ** READQF -- read queue file and set up environment. 503 ** 504 ** Parameters: 505 ** e -- the envelope of the job to run. 506 ** full -- if set, read in all information. Otherwise just 507 ** read in info needed for a queue print. 508 ** 509 ** Returns: 510 ** none. 511 ** 512 ** Side Effects: 513 ** cf is read and created as the current job, as though 514 ** we had been invoked by argument. 515 */ 516 517 readqf(e, full) 518 register ENVELOPE *e; 519 bool full; 520 { 521 register FILE *f; 522 char buf[MAXFIELD]; 523 extern char *fgetfolded(); 524 register char *p; 525 526 /* 527 ** Open the file created by queueup. 528 */ 529 530 p = queuename(e, 'q'); 531 f = fopen(p, "r"); 532 if (f == NULL) 533 { 534 syserr("readqf: no control file %s", p); 535 return; 536 } 537 FileName = p; 538 LineNumber = 0; 539 540 /* 541 ** Read and process the file. 542 */ 543 544 if (Verbose && full) 545 printf("\nRunning %s\n", e->e_id); 546 while (fgetfolded(buf, sizeof buf, f) != NULL) 547 { 548 switch (buf[0]) 549 { 550 case 'R': /* specify recipient */ 551 sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue); 552 break; 553 554 case 'H': /* header */ 555 if (full) 556 (void) chompheader(&buf[1], FALSE); 557 break; 558 559 case 'S': /* sender */ 560 setsender(newstr(&buf[1])); 561 break; 562 563 case 'D': /* data file name */ 564 if (!full) 565 break; 566 e->e_df = newstr(&buf[1]); 567 e->e_dfp = fopen(e->e_df, "r"); 568 if (e->e_dfp == NULL) 569 syserr("readqf: cannot open %s", e->e_df); 570 break; 571 572 case 'T': /* init time */ 573 (void) sscanf(&buf[1], "%ld", &e->e_ctime); 574 break; 575 576 case 'P': /* message priority */ 577 (void) sscanf(&buf[1], "%ld", &e->e_msgpriority); 578 579 /* make sure that big things get sent eventually */ 580 e->e_msgpriority -= WKTIMEFACT; 581 break; 582 583 case 'M': /* define macro */ 584 if (full) 585 define(buf[1], newstr(&buf[2]), e); 586 break; 587 588 default: 589 syserr("readqf(%s): bad line \"%s\"", e->e_id, buf); 590 break; 591 } 592 } 593 594 FileName = NULL; 595 } 596 /* 597 ** TIMEOUT -- process timeout on queue file. 598 ** 599 ** Parameters: 600 ** e -- the envelope that timed out. 601 ** 602 ** Returns: 603 ** none. 604 ** 605 ** Side Effects: 606 ** Returns a message to the sender saying that this 607 ** message has timed out. 608 */ 609 610 timeout(e) 611 register ENVELOPE *e; 612 { 613 char buf[MAXLINE]; 614 extern char *pintvl(); 615 616 # ifdef DEBUG 617 if (tTd(40, 3)) 618 printf("timeout(%s)\n", e->e_id); 619 # endif DEBUG 620 e->e_to = NULL; 621 message(Arpa_Info, "Message has timed out"); 622 623 /* return message to sender */ 624 (void) sprintf(buf, "Cannot send mail for %s", pintvl(TimeOut, FALSE)); 625 (void) returntosender(buf, &e->e_from, TRUE); 626 627 /* arrange to remove files from queue */ 628 e->e_flags |= EF_CLRQUEUE; 629 } 630 /* 631 ** PRINTQUEUE -- print out a representation of the mail queue 632 ** 633 ** Parameters: 634 ** none. 635 ** 636 ** Returns: 637 ** none. 638 ** 639 ** Side Effects: 640 ** Prints a listing of the mail queue on the standard output. 641 */ 642 643 printqueue() 644 { 645 register WORK *w; 646 FILE *f; 647 char buf[MAXLINE]; 648 649 /* 650 ** Read and order the queue. 651 */ 652 653 orderq(); 654 655 /* 656 ** Print the work list that we have read. 657 */ 658 659 /* first see if there is anything */ 660 if (WorkQ == NULL) 661 { 662 printf("\nMail queue is empty\n"); 663 return; 664 } 665 666 printf("\n\t\tMail Queue\n"); 667 printf("--QID-- --Size-- -----Q Time----- --Sender/Recipient--\n"); 668 for (w = WorkQ; w != NULL; w = w->w_next) 669 { 670 struct stat st; 671 672 printf("%7s", w->w_name + 2); 673 f = fopen(w->w_name, "r"); 674 if (f == NULL) 675 { 676 printf(" (finished)\n"); 677 continue; 678 } 679 (void) fstat(fileno(f), &st); 680 printf(" %8ld", st.st_size); 681 while (fgets(buf, sizeof buf, f) != NULL) 682 { 683 auto long ti; 684 685 fixcrlf(buf, TRUE); 686 switch (buf[0]) 687 { 688 case 'S': /* sender name */ 689 printf(" %.20s", &buf[1]); 690 break; 691 692 case 'R': /* recipient name */ 693 printf("\n\t\t\t\t %.20s", &buf[1]); 694 break; 695 696 case 'T': /* creation time */ 697 sscanf(&buf[1], "%ld", &ti); 698 printf(" %.16s", ctime(&ti)); 699 break; 700 } 701 } 702 printf("\n"); 703 fclose(f); 704 } 705 } 706 707 # endif QUEUE 708