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