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.33 08/17/82 (no queueing)); 9 # else QUEUE 10 11 SCCSID(@(#)queue.c 3.33 08/17/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 cf[MAXNAME]; 37 char buf[MAXLINE]; 38 register FILE *cfp; 39 register HDR *h; 40 register ADDRESS *q; 41 extern char *mktemp(); 42 register int i; 43 44 /* 45 ** Create control file. 46 */ 47 48 (void) strcpy(cf, QueueDir); 49 (void) strcat(cf, "/tfXXXXXX"); 50 (void) mktemp(cf); 51 cfp = fopen(cf, "w"); 52 if (cfp == NULL) 53 { 54 syserr("queueup: cannot create control file %s", cf); 55 return; 56 } 57 (void) chmod(cf, 0600); 58 59 # ifdef DEBUG 60 if (tTd(40, 1)) 61 printf("queueing in %s\n", cf); 62 # endif DEBUG 63 64 /* 65 ** If there is no data file yet, create one. 66 */ 67 68 if (e->e_df == NULL) 69 { 70 register FILE *dfp; 71 72 (void) strcpy(buf, QueueDir); 73 (void) strcat(buf, "/dfXXXXXX"); 74 e->e_df = newstr(mktemp(buf)); 75 dfp = fopen(e->e_df, "w"); 76 if (dfp == NULL) 77 { 78 syserr("queueup: cannot create %s", e->e_df); 79 (void) fclose(cfp); 80 return; 81 } 82 (void) chmod(e->e_df, 0600); 83 (*e->e_putbody)(dfp, Mailer[1], FALSE); 84 (void) fclose(dfp); 85 } 86 87 /* 88 ** Output future work requests. 89 */ 90 91 /* output name of data file */ 92 fprintf(cfp, "D%s\n", e->e_df); 93 94 /* output name of sender */ 95 fprintf(cfp, "S%s\n", e->e_from.q_paddr); 96 97 /* output timeout */ 98 fprintf(cfp, "T%ld\n", TimeOut); 99 100 /* output message priority */ 101 fprintf(cfp, "P%ld\n", e->e_msgpriority); 102 103 /* output message class */ 104 fprintf(cfp, "C%d\n", e->e_class); 105 106 /* output macro definitions */ 107 /* I don't think this is needed any more..... 108 for (i = 0; i < 128; i++) 109 { 110 register char *p = e->e_macro[i]; 111 112 if (p != NULL && i != (int) 'b') 113 fprintf(cfp, "M%c%s\n", i, p); 114 } 115 ..... */ 116 117 /* output list of recipient addresses */ 118 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 119 { 120 # ifdef DEBUG 121 if (tTd(40, 1)) 122 { 123 printf("queueing "); 124 printaddr(q, FALSE); 125 } 126 # endif DEBUG 127 if (queueall ? !bitset(QDONTSEND, q->q_flags) : 128 bitset(QQUEUEUP, q->q_flags)) 129 fprintf(cfp, "R%s\n", q->q_paddr); 130 } 131 132 /* output headers for this message */ 133 define('g', "$f"); 134 for (h = e->e_header; h != NULL; h = h->h_link) 135 { 136 if (h->h_value == NULL || h->h_value[0] == '\0') 137 continue; 138 fprintf(cfp, "H"); 139 if (h->h_mflags != 0 && bitset(H_CHECK|H_ACHECK, h->h_flags)) 140 mfdecode(h->h_mflags, cfp); 141 fprintf(cfp, "%s: ", h->h_field); 142 if (bitset(H_DEFAULT, h->h_flags)) 143 { 144 (void) expand(h->h_value, buf, &buf[sizeof buf], e); 145 fprintf(cfp, "%s\n", buf); 146 } 147 else 148 fprintf(cfp, "%s\n", h->h_value); 149 } 150 151 /* 152 ** Clean up. 153 */ 154 155 (void) fclose(cfp); 156 (void) strcpy(buf, QueueDir); 157 (void) strcat(buf, "/cfXXXXXX"); 158 (void) mktemp(buf); 159 if (link(cf, buf) < 0) 160 syserr("cannot link(%s, %s), df=%s", cf, buf, e->e_df); 161 else 162 (void) unlink(cf); 163 164 # ifdef LOG 165 /* save log info */ 166 if (LogLevel > 9) 167 syslog(LOG_INFO, "%s queueup: cf=%s, df=%s\n", MsgId, buf, e->e_df); 168 # endif LOG 169 170 /* disconnect this temp file from the job */ 171 e->e_df = NULL; 172 } 173 /* 174 ** RUNQUEUE -- run the jobs in the queue. 175 ** 176 ** Gets the stuff out of the queue in some presumably logical 177 ** order and processes them. 178 ** 179 ** Parameters: 180 ** none. 181 ** 182 ** Returns: 183 ** none. 184 ** 185 ** Side Effects: 186 ** runs things in the mail queue. 187 */ 188 189 bool ReorderQueue; /* if set, reorder the send queue */ 190 int QueuePid; /* pid of child running queue */ 191 192 runqueue(forkflag) 193 bool forkflag; 194 { 195 extern reordersig(); 196 197 /* 198 ** See if we want to go off and do other useful work. 199 */ 200 201 if (forkflag) 202 { 203 QueuePid = dofork(); 204 if (QueuePid > 0) 205 { 206 /* parent */ 207 if (QueueIntvl != 0) 208 setevent(QueueIntvl, reordersig, TRUE); 209 return; 210 } 211 } 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] != 'c' || 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 517 /* don't use the headers from sendmail.cf... */ 518 CurEnv->e_header = NULL; 519 chompheader("from: $q", TRUE); 520 521 /* create the link to the control file during processing */ 522 (void) strcpy(buf, QueueDir); 523 (void) strcat(buf, "/tfXXXXXX"); 524 (void) mktemp(buf); 525 if (link(w->w_name, buf) < 0) 526 { 527 /* this can happen normally; another queuer sneaks in */ 528 /* syserr("dowork: link(%s, %s)", w->w_name, buf); */ 529 exit(EX_OK); 530 } 531 ControlFile = newstr(buf); 532 (void) unlink(w->w_name); 533 534 /* create ourselves a transcript file */ 535 openxscrpt(); 536 537 /* do basic system initialization */ 538 initsys(); 539 540 /* read the queue control file */ 541 readqf(buf); 542 eatheader(); 543 544 /* do the delivery */ 545 if (!FatalErrors) 546 sendall(CurEnv, FALSE); 547 548 /* if still not sent, perhaps we should time out.... */ 549 # ifdef DEBUG 550 if (tTd(40, 3)) 551 printf("CurTime=%ld, TimeOut=%ld\n", CurTime, TimeOut); 552 # endif DEBUG 553 if (CurEnv->e_queueup && CurTime > TimeOut) 554 timeout(w); 555 556 /* get rid of the temporary file -- a new cf will be made */ 557 ControlFile = NULL; 558 (void) unlink(buf); 559 560 /* finish up and exit */ 561 finis(); 562 } 563 564 /* 565 ** Parent -- pick up results. 566 */ 567 568 errno = 0; 569 while ((i = wait(&xstat)) > 0 && errno != EINTR) 570 { 571 if (errno == EINTR) 572 { 573 errno = 0; 574 } 575 } 576 } 577 /* 578 ** READQF -- read queue file and set up environment. 579 ** 580 ** Parameters: 581 ** cf -- name of queue control file. 582 ** 583 ** Returns: 584 ** none. 585 ** 586 ** Side Effects: 587 ** cf is read and created as the current job, as though 588 ** we had been invoked by argument. 589 */ 590 591 readqf(cf) 592 char *cf; 593 { 594 register FILE *f; 595 char buf[MAXFIELD]; 596 register char *p; 597 register int i; 598 599 /* 600 ** Open the file created by queueup. 601 */ 602 603 f = fopen(cf, "r"); 604 if (f == NULL) 605 { 606 syserr("readqf: no cf file %s", cf); 607 return; 608 } 609 610 /* 611 ** Read and process the file. 612 */ 613 614 if (Verbose) 615 printf("\nRunning %s\n", cf); 616 while (fgetfolded(buf, sizeof buf, f) != NULL) 617 { 618 switch (buf[0]) 619 { 620 case 'R': /* specify recipient */ 621 sendto(&buf[1], 1, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 622 break; 623 624 case 'H': /* header */ 625 (void) chompheader(&buf[1], FALSE); 626 break; 627 628 case 'S': /* sender */ 629 setsender(newstr(&buf[1])); 630 break; 631 632 case 'D': /* data file name */ 633 CurEnv->e_df = newstr(&buf[1]); 634 TempFile = fopen(CurEnv->e_df, "r"); 635 if (TempFile == NULL) 636 syserr("readqf: cannot open %s", CurEnv->e_df); 637 break; 638 639 case 'T': /* timeout */ 640 (void) sscanf(&buf[1], "%ld", &TimeOut); 641 break; 642 643 case 'P': /* message priority */ 644 (void) sscanf(&buf[1], "%ld", &CurEnv->e_msgpriority); 645 646 /* make sure that big things get sent eventually */ 647 CurEnv->e_msgpriority -= WKTIMEFACT; 648 break; 649 650 case 'C': /* message class */ 651 (void) sscanf(&buf[1], "%hd", &CurEnv->e_class); 652 break; 653 654 case 'M': /* define macro */ 655 define(buf[1], newstr(&buf[2])); 656 break; 657 658 default: 659 syserr("readqf(%s): bad line \"%s\"", cf, buf); 660 break; 661 } 662 } 663 } 664 /* 665 ** TIMEOUT -- process timeout on queue file. 666 ** 667 ** Parameters: 668 ** w -- pointer to work request that timed out. 669 ** 670 ** Returns: 671 ** none. 672 ** 673 ** Side Effects: 674 ** Returns a message to the sender saying that this 675 ** message has timed out. 676 */ 677 678 timeout(w) 679 register WORK *w; 680 { 681 char buf[MAXLINE]; 682 extern char *TextTimeOut; 683 684 # ifdef DEBUG 685 if (tTd(40, 3)) 686 printf("timeout(%s)\n", w->w_name); 687 # endif DEBUG 688 message(Arpa_Info, "Message has timed out"); 689 690 /* return message to sender */ 691 (void) sprintf(buf, "Cannot send mail for %s", TextTimeOut); 692 (void) returntosender(buf, &CurEnv->e_from, TRUE); 693 694 /* arrange to remove files from queue */ 695 CurEnv->e_queueup = FALSE; 696 } 697 698 # endif QUEUE 699