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