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