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