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