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