1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 # include "sendmail.h" 10 11 #ifndef lint 12 #ifdef QUEUE 13 static char sccsid[] = "@(#)queue.c 8.11 (Berkeley) 08/16/93 (with queueing)"; 14 #else 15 static char sccsid[] = "@(#)queue.c 8.11 (Berkeley) 08/16/93 (without queueing)"; 16 #endif 17 #endif /* not lint */ 18 19 # include <errno.h> 20 # include <pwd.h> 21 # include <dirent.h> 22 23 # ifdef QUEUE 24 25 /* 26 ** Work queue. 27 */ 28 29 struct work 30 { 31 char *w_name; /* name of control file */ 32 long w_pri; /* priority of message, see below */ 33 time_t w_ctime; /* creation time of message */ 34 struct work *w_next; /* next in queue */ 35 }; 36 37 typedef struct work WORK; 38 39 WORK *WorkQ; /* queue of things to be done */ 40 /* 41 ** QUEUEUP -- queue a message up for future transmission. 42 ** 43 ** Parameters: 44 ** e -- the envelope to queue up. 45 ** queueall -- if TRUE, queue all addresses, rather than 46 ** just those with the QQUEUEUP flag set. 47 ** announce -- if TRUE, tell when you are queueing up. 48 ** 49 ** Returns: 50 ** none. 51 ** 52 ** Side Effects: 53 ** The current request are saved in a control file. 54 ** The queue file is left locked. 55 */ 56 57 queueup(e, queueall, announce) 58 register ENVELOPE *e; 59 bool queueall; 60 bool announce; 61 { 62 char *qf; 63 register FILE *tfp; 64 register HDR *h; 65 register ADDRESS *q; 66 int fd; 67 int i; 68 bool newid; 69 register char *p; 70 MAILER nullmailer; 71 char buf[MAXLINE], tf[MAXLINE]; 72 73 /* 74 ** Create control file. 75 */ 76 77 newid = (e->e_id == NULL); 78 79 /* if newid, queuename will create a locked qf file in e->lockfp */ 80 strcpy(tf, queuename(e, 't')); 81 tfp = e->e_lockfp; 82 if (tfp == NULL) 83 newid = FALSE; 84 85 /* if newid, just write the qf file directly (instead of tf file) */ 86 if (newid) 87 { 88 tfp = e->e_lockfp; 89 } 90 else 91 { 92 /* get a locked tf file */ 93 for (i = 0; i < 128; i++) 94 { 95 fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode); 96 if (fd < 0) 97 { 98 if (errno != EEXIST) 99 break; 100 #ifdef LOG 101 if (LogLevel > 0 && (i % 32) == 0) 102 syslog(LOG_ALERT, "queueup: cannot create %s: %s", 103 tf, errstring(errno)); 104 #endif 105 continue; 106 } 107 108 if (lockfile(fd, tf, LOCK_EX|LOCK_NB)) 109 break; 110 #ifdef LOG 111 else if (LogLevel > 0 && (i % 32) == 0) 112 syslog(LOG_ALERT, "queueup: cannot lock %s: %s", 113 tf, errstring(errno)); 114 #endif 115 116 close(fd); 117 118 if ((i % 32) == 31) 119 { 120 /* save the old temp file away */ 121 (void) rename(tf, queuename(e, 'T')); 122 } 123 else 124 sleep(i % 32); 125 } 126 if (fd < 0) 127 syserr("!queueup: cannot create temp file %s", tf); 128 129 tfp = fdopen(fd, "w"); 130 } 131 132 if (tTd(40, 1)) 133 printf("queueing %s\n", e->e_id); 134 135 /* 136 ** If there is no data file yet, create one. 137 */ 138 139 if (e->e_df == NULL) 140 { 141 register FILE *dfp; 142 extern putbody(); 143 144 e->e_df = queuename(e, 'd'); 145 e->e_df = newstr(e->e_df); 146 fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode); 147 if (fd < 0) 148 syserr("!queueup: cannot create %s", e->e_df); 149 dfp = fdopen(fd, "w"); 150 (*e->e_putbody)(dfp, FileMailer, e, NULL); 151 (void) xfclose(dfp, "queueup dfp", e->e_id); 152 e->e_putbody = putbody; 153 } 154 155 /* 156 ** Output future work requests. 157 ** Priority and creation time should be first, since 158 ** they are required by orderq. 159 */ 160 161 /* output message priority */ 162 fprintf(tfp, "P%ld\n", e->e_msgpriority); 163 164 /* output creation time */ 165 fprintf(tfp, "T%ld\n", e->e_ctime); 166 167 /* output type and name of data file */ 168 if (e->e_bodytype != NULL) 169 fprintf(tfp, "B%s\n", e->e_bodytype); 170 fprintf(tfp, "D%s\n", e->e_df); 171 172 /* message from envelope, if it exists */ 173 if (e->e_message != NULL) 174 fprintf(tfp, "M%s\n", e->e_message); 175 176 /* send various flag bits through */ 177 p = buf; 178 if (bitset(EF_WARNING, e->e_flags)) 179 *p++ = 'w'; 180 if (bitset(EF_RESPONSE, e->e_flags)) 181 *p++ = 'r'; 182 *p++ = '\0'; 183 if (buf[0] != '\0') 184 fprintf(tfp, "F%s\n", buf); 185 186 /* $r and $s and $_ macro values */ 187 if ((p = macvalue('r', e)) != NULL) 188 fprintf(tfp, "$r%s\n", p); 189 if ((p = macvalue('s', e)) != NULL) 190 fprintf(tfp, "$s%s\n", p); 191 if ((p = macvalue('_', e)) != NULL) 192 fprintf(tfp, "$_%s\n", p); 193 194 /* output name of sender */ 195 fprintf(tfp, "S%s\n", e->e_from.q_paddr); 196 197 /* output list of error recipients */ 198 printctladdr(NULL, NULL); 199 for (q = e->e_errorqueue; q != NULL; q = q->q_next) 200 { 201 if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 202 { 203 printctladdr(q, tfp); 204 fprintf(tfp, "E%s\n", q->q_paddr); 205 } 206 } 207 208 /* output list of recipient addresses */ 209 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 210 { 211 if (bitset(QQUEUEUP, q->q_flags) || 212 (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags))) 213 { 214 printctladdr(q, tfp); 215 fprintf(tfp, "R%s\n", q->q_paddr); 216 if (announce) 217 { 218 e->e_to = q->q_paddr; 219 message("queued"); 220 if (LogLevel > 8) 221 logdelivery(NULL, NULL, "queued", e); 222 e->e_to = NULL; 223 } 224 if (tTd(40, 1)) 225 { 226 printf("queueing "); 227 printaddr(q, FALSE); 228 } 229 } 230 } 231 232 /* 233 ** Output headers for this message. 234 ** Expand macros completely here. Queue run will deal with 235 ** everything as absolute headers. 236 ** All headers that must be relative to the recipient 237 ** can be cracked later. 238 ** We set up a "null mailer" -- i.e., a mailer that will have 239 ** no effect on the addresses as they are output. 240 */ 241 242 bzero((char *) &nullmailer, sizeof nullmailer); 243 nullmailer.m_re_rwset = nullmailer.m_rh_rwset = 244 nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1; 245 nullmailer.m_eol = "\n"; 246 247 define('g', "\201f", e); 248 for (h = e->e_header; h != NULL; h = h->h_link) 249 { 250 extern bool bitzerop(); 251 252 /* don't output null headers */ 253 if (h->h_value == NULL || h->h_value[0] == '\0') 254 continue; 255 256 /* don't output resent headers on non-resent messages */ 257 if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 258 continue; 259 260 /* output this header */ 261 fprintf(tfp, "H"); 262 263 /* if conditional, output the set of conditions */ 264 if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 265 { 266 int j; 267 268 (void) putc('?', tfp); 269 for (j = '\0'; j <= '\177'; j++) 270 if (bitnset(j, h->h_mflags)) 271 (void) putc(j, tfp); 272 (void) putc('?', tfp); 273 } 274 275 /* output the header: expand macros, convert addresses */ 276 if (bitset(H_DEFAULT, h->h_flags)) 277 { 278 (void) expand(h->h_value, buf, &buf[sizeof buf], e); 279 fprintf(tfp, "%s: %s\n", h->h_field, buf); 280 } 281 else if (bitset(H_FROM|H_RCPT, h->h_flags)) 282 { 283 bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 284 FILE *savetrace = TrafficLogFile; 285 286 TrafficLogFile = NULL; 287 288 if (bitset(H_FROM, h->h_flags)) 289 oldstyle = FALSE; 290 291 commaize(h, h->h_value, tfp, oldstyle, 292 &nullmailer, e); 293 294 TrafficLogFile = savetrace; 295 } 296 else 297 fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 298 } 299 300 /* 301 ** Clean up. 302 */ 303 304 fflush(tfp); 305 fsync(fileno(tfp)); 306 if (ferror(tfp)) 307 { 308 if (newid) 309 syserr("!552 Error writing control file %s", tf); 310 else 311 syserr("!452 Error writing control file %s", tf); 312 } 313 314 if (!newid) 315 { 316 /* rename (locked) tf to be (locked) qf */ 317 qf = queuename(e, 'q'); 318 if (rename(tf, qf) < 0) 319 syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df); 320 321 /* close and unlock old (locked) qf */ 322 if (e->e_lockfp != NULL) 323 (void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id); 324 e->e_lockfp = tfp; 325 } 326 else 327 qf = tf; 328 errno = 0; 329 330 # ifdef LOG 331 /* save log info */ 332 if (LogLevel > 79) 333 syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 334 # endif /* LOG */ 335 return; 336 } 337 338 printctladdr(a, tfp) 339 register ADDRESS *a; 340 FILE *tfp; 341 { 342 char *uname; 343 register struct passwd *pw; 344 register ADDRESS *q; 345 uid_t uid; 346 static ADDRESS *lastctladdr; 347 static uid_t lastuid; 348 349 /* initialization */ 350 if (a == NULL || a->q_alias == NULL || tfp == NULL) 351 { 352 if (lastctladdr != NULL && tfp != NULL) 353 fprintf(tfp, "C\n"); 354 lastctladdr = NULL; 355 lastuid = 0; 356 return; 357 } 358 359 /* find the active uid */ 360 q = getctladdr(a); 361 if (q == NULL) 362 uid = 0; 363 else 364 uid = q->q_uid; 365 a = a->q_alias; 366 367 /* check to see if this is the same as last time */ 368 if (lastctladdr != NULL && uid == lastuid && 369 strcmp(lastctladdr->q_paddr, a->q_paddr) == 0) 370 return; 371 lastuid = uid; 372 lastctladdr = a; 373 374 if (uid == 0 || (pw = getpwuid(uid)) == NULL) 375 uname = ""; 376 else 377 uname = pw->pw_name; 378 379 fprintf(tfp, "C%s:%s\n", uname, a->q_paddr); 380 } 381 382 /* 383 ** RUNQUEUE -- run the jobs in the queue. 384 ** 385 ** Gets the stuff out of the queue in some presumably logical 386 ** order and processes them. 387 ** 388 ** Parameters: 389 ** forkflag -- TRUE if the queue scanning should be done in 390 ** a child process. We double-fork so it is not our 391 ** child and we don't have to clean up after it. 392 ** 393 ** Returns: 394 ** none. 395 ** 396 ** Side Effects: 397 ** runs things in the mail queue. 398 */ 399 400 ENVELOPE QueueEnvelope; /* the queue run envelope */ 401 402 runqueue(forkflag) 403 bool forkflag; 404 { 405 register ENVELOPE *e; 406 extern ENVELOPE BlankEnvelope; 407 408 /* 409 ** If no work will ever be selected, don't even bother reading 410 ** the queue. 411 */ 412 413 CurrentLA = getla(); /* get load average */ 414 415 if (shouldqueue(0L, curtime())) 416 { 417 if (Verbose) 418 printf("Skipping queue run -- load average too high\n"); 419 if (forkflag && QueueIntvl != 0) 420 (void) setevent(QueueIntvl, runqueue, TRUE); 421 return; 422 } 423 424 /* 425 ** See if we want to go off and do other useful work. 426 */ 427 428 if (forkflag) 429 { 430 int pid; 431 432 pid = dofork(); 433 if (pid != 0) 434 { 435 extern void reapchild(); 436 437 /* parent -- pick up intermediate zombie */ 438 #ifndef SIGCHLD 439 (void) waitfor(pid); 440 #else /* SIGCHLD */ 441 (void) setsignal(SIGCHLD, reapchild); 442 #endif /* SIGCHLD */ 443 if (QueueIntvl != 0) 444 (void) setevent(QueueIntvl, runqueue, TRUE); 445 return; 446 } 447 /* child -- double fork */ 448 #ifndef SIGCHLD 449 if (fork() != 0) 450 exit(EX_OK); 451 #else /* SIGCHLD */ 452 (void) setsignal(SIGCHLD, SIG_DFL); 453 #endif /* SIGCHLD */ 454 } 455 456 setproctitle("running queue: %s", QueueDir); 457 458 # ifdef LOG 459 if (LogLevel > 69) 460 syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 461 QueueDir, getpid(), forkflag); 462 # endif /* LOG */ 463 464 /* 465 ** Release any resources used by the daemon code. 466 */ 467 468 # ifdef DAEMON 469 clrdaemon(); 470 # endif /* DAEMON */ 471 472 /* 473 ** Create ourselves an envelope 474 */ 475 476 CurEnv = &QueueEnvelope; 477 e = newenvelope(&QueueEnvelope, CurEnv); 478 e->e_flags = BlankEnvelope.e_flags; 479 480 /* 481 ** Make sure the alias database is open. 482 */ 483 484 initmaps(FALSE, e); 485 486 /* 487 ** Start making passes through the queue. 488 ** First, read and sort the entire queue. 489 ** Then, process the work in that order. 490 ** But if you take too long, start over. 491 */ 492 493 /* order the existing work requests */ 494 (void) orderq(FALSE); 495 496 /* process them once at a time */ 497 while (WorkQ != NULL) 498 { 499 WORK *w = WorkQ; 500 501 WorkQ = WorkQ->w_next; 502 503 /* 504 ** Ignore jobs that are too expensive for the moment. 505 */ 506 507 if (shouldqueue(w->w_pri, w->w_ctime)) 508 { 509 if (Verbose) 510 printf("\nSkipping %s\n", w->w_name + 2); 511 } 512 else 513 { 514 dowork(w->w_name + 2, ForkQueueRuns, FALSE, e); 515 } 516 free(w->w_name); 517 free((char *) w); 518 } 519 520 /* exit without the usual cleanup */ 521 e->e_id = NULL; 522 finis(); 523 } 524 /* 525 ** ORDERQ -- order the work queue. 526 ** 527 ** Parameters: 528 ** doall -- if set, include everything in the queue (even 529 ** the jobs that cannot be run because the load 530 ** average is too high). Otherwise, exclude those 531 ** jobs. 532 ** 533 ** Returns: 534 ** The number of request in the queue (not necessarily 535 ** the number of requests in WorkQ however). 536 ** 537 ** Side Effects: 538 ** Sets WorkQ to the queue of available work, in order. 539 */ 540 541 # define NEED_P 001 542 # define NEED_T 002 543 # define NEED_R 004 544 # define NEED_S 010 545 546 orderq(doall) 547 bool doall; 548 { 549 register struct dirent *d; 550 register WORK *w; 551 DIR *f; 552 register int i; 553 WORK wlist[QUEUESIZE+1]; 554 int wn = -1; 555 extern workcmpf(); 556 557 if (tTd(41, 1)) 558 { 559 printf("orderq:\n"); 560 if (QueueLimitId != NULL) 561 printf("\tQueueLimitId = %s\n", QueueLimitId); 562 if (QueueLimitSender != NULL) 563 printf("\tQueueLimitSender = %s\n", QueueLimitSender); 564 if (QueueLimitRecipient != NULL) 565 printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 566 } 567 568 /* clear out old WorkQ */ 569 for (w = WorkQ; w != NULL; ) 570 { 571 register WORK *nw = w->w_next; 572 573 WorkQ = nw; 574 free(w->w_name); 575 free((char *) w); 576 w = nw; 577 } 578 579 /* open the queue directory */ 580 f = opendir("."); 581 if (f == NULL) 582 { 583 syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 584 return (0); 585 } 586 587 /* 588 ** Read the work directory. 589 */ 590 591 while ((d = readdir(f)) != NULL) 592 { 593 FILE *cf; 594 char lbuf[MAXNAME]; 595 extern bool strcontainedin(); 596 597 /* is this an interesting entry? */ 598 if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 599 continue; 600 601 if (QueueLimitId != NULL && 602 !strcontainedin(QueueLimitId, d->d_name)) 603 continue; 604 605 /* 606 ** Check queue name for plausibility. This handles 607 ** both old and new type ids. 608 */ 609 610 i = strlen(d->d_name); 611 if (i != 9 && i != 10) 612 { 613 if (Verbose) 614 printf("orderq: bogus qf name %s\n", d->d_name); 615 #ifdef LOG 616 if (LogLevel > 3) 617 syslog(LOG_CRIT, "orderq: bogus qf name %s", 618 d->d_name); 619 #endif 620 if (strlen(d->d_name) >= MAXNAME) 621 d->d_name[MAXNAME - 1] = '\0'; 622 strcpy(lbuf, d->d_name); 623 lbuf[0] = 'Q'; 624 (void) rename(d->d_name, lbuf); 625 continue; 626 } 627 628 /* yes -- open control file (if not too many files) */ 629 if (++wn >= QUEUESIZE) 630 continue; 631 632 cf = fopen(d->d_name, "r"); 633 if (cf == NULL) 634 { 635 /* this may be some random person sending hir msgs */ 636 /* syserr("orderq: cannot open %s", cbuf); */ 637 if (tTd(41, 2)) 638 printf("orderq: cannot open %s (%d)\n", 639 d->d_name, errno); 640 errno = 0; 641 wn--; 642 continue; 643 } 644 w = &wlist[wn]; 645 w->w_name = newstr(d->d_name); 646 647 /* make sure jobs in creation don't clog queue */ 648 w->w_pri = 0x7fffffff; 649 w->w_ctime = 0; 650 651 /* extract useful information */ 652 i = NEED_P | NEED_T; 653 if (QueueLimitSender != NULL) 654 i |= NEED_S; 655 if (QueueLimitRecipient != NULL) 656 i |= NEED_R; 657 while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 658 { 659 extern long atol(); 660 extern bool strcontainedin(); 661 662 switch (lbuf[0]) 663 { 664 case 'P': 665 w->w_pri = atol(&lbuf[1]); 666 i &= ~NEED_P; 667 break; 668 669 case 'T': 670 w->w_ctime = atol(&lbuf[1]); 671 i &= ~NEED_T; 672 break; 673 674 case 'R': 675 if (QueueLimitRecipient != NULL && 676 strcontainedin(QueueLimitRecipient, &lbuf[1])) 677 i &= ~NEED_R; 678 break; 679 680 case 'S': 681 if (QueueLimitSender != NULL && 682 strcontainedin(QueueLimitSender, &lbuf[1])) 683 i &= ~NEED_S; 684 break; 685 } 686 } 687 (void) fclose(cf); 688 689 if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 690 bitset(NEED_R|NEED_S, i)) 691 { 692 /* don't even bother sorting this job in */ 693 wn--; 694 } 695 } 696 (void) closedir(f); 697 wn++; 698 699 /* 700 ** Sort the work directory. 701 */ 702 703 qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 704 705 /* 706 ** Convert the work list into canonical form. 707 ** Should be turning it into a list of envelopes here perhaps. 708 */ 709 710 WorkQ = NULL; 711 for (i = min(wn, QUEUESIZE); --i >= 0; ) 712 { 713 w = (WORK *) xalloc(sizeof *w); 714 w->w_name = wlist[i].w_name; 715 w->w_pri = wlist[i].w_pri; 716 w->w_ctime = wlist[i].w_ctime; 717 w->w_next = WorkQ; 718 WorkQ = w; 719 } 720 721 if (tTd(40, 1)) 722 { 723 for (w = WorkQ; w != NULL; w = w->w_next) 724 printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 725 } 726 727 return (wn); 728 } 729 /* 730 ** WORKCMPF -- compare function for ordering work. 731 ** 732 ** Parameters: 733 ** a -- the first argument. 734 ** b -- the second argument. 735 ** 736 ** Returns: 737 ** -1 if a < b 738 ** 0 if a == b 739 ** +1 if a > b 740 ** 741 ** Side Effects: 742 ** none. 743 */ 744 745 workcmpf(a, b) 746 register WORK *a; 747 register WORK *b; 748 { 749 long pa = a->w_pri; 750 long pb = b->w_pri; 751 752 if (pa == pb) 753 return (0); 754 else if (pa > pb) 755 return (1); 756 else 757 return (-1); 758 } 759 /* 760 ** DOWORK -- do a work request. 761 ** 762 ** Parameters: 763 ** id -- the ID of the job to run. 764 ** forkflag -- if set, run this in background. 765 ** requeueflag -- if set, reinstantiate the queue quickly. 766 ** This is used when expanding aliases in the queue. 767 ** If forkflag is also set, it doesn't wait for the 768 ** child. 769 ** e - the envelope in which to run it. 770 ** 771 ** Returns: 772 ** none. 773 ** 774 ** Side Effects: 775 ** The work request is satisfied if possible. 776 */ 777 778 dowork(id, forkflag, requeueflag, e) 779 char *id; 780 bool forkflag; 781 bool requeueflag; 782 register ENVELOPE *e; 783 { 784 register int i; 785 extern bool readqf(); 786 787 if (tTd(40, 1)) 788 printf("dowork(%s)\n", id); 789 790 /* 791 ** Fork for work. 792 */ 793 794 if (forkflag) 795 { 796 i = fork(); 797 if (i < 0) 798 { 799 syserr("dowork: cannot fork"); 800 return; 801 } 802 } 803 else 804 { 805 i = 0; 806 } 807 808 if (i == 0) 809 { 810 /* 811 ** CHILD 812 ** Lock the control file to avoid duplicate deliveries. 813 ** Then run the file as though we had just read it. 814 ** We save an idea of the temporary name so we 815 ** can recover on interrupt. 816 */ 817 818 /* set basic modes, etc. */ 819 (void) alarm(0); 820 clearenvelope(e, FALSE); 821 e->e_flags |= EF_QUEUERUN; 822 e->e_errormode = EM_MAIL; 823 e->e_id = id; 824 if (forkflag) 825 disconnect(0, e); 826 # ifdef LOG 827 if (LogLevel > 76) 828 syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 829 getpid()); 830 # endif /* LOG */ 831 832 /* don't use the headers from sendmail.cf... */ 833 e->e_header = NULL; 834 835 /* read the queue control file -- return if locked */ 836 if (!readqf(e, !requeueflag)) 837 { 838 if (tTd(40, 4)) 839 printf("readqf(%s) failed\n", e->e_id); 840 if (forkflag) 841 exit(EX_OK); 842 else 843 return; 844 } 845 846 e->e_flags |= EF_INQUEUE; 847 eatheader(e, requeueflag); 848 849 if (requeueflag) 850 queueup(e, TRUE, FALSE); 851 852 /* do the delivery */ 853 sendall(e, SM_DELIVER); 854 855 /* finish up and exit */ 856 if (forkflag) 857 finis(); 858 else 859 dropenvelope(e); 860 } 861 else if (!requeueflag) 862 { 863 /* 864 ** Parent -- pick up results. 865 */ 866 867 errno = 0; 868 (void) waitfor(i); 869 } 870 } 871 /* 872 ** READQF -- read queue file and set up environment. 873 ** 874 ** Parameters: 875 ** e -- the envelope of the job to run. 876 ** announcefile -- if set, announce the name of the queue 877 ** file in error messages. 878 ** 879 ** Returns: 880 ** TRUE if it successfully read the queue file. 881 ** FALSE otherwise. 882 ** 883 ** Side Effects: 884 ** The queue file is returned locked. 885 */ 886 887 bool 888 readqf(e, announcefile) 889 register ENVELOPE *e; 890 bool announcefile; 891 { 892 register FILE *qfp; 893 ADDRESS *ctladdr; 894 struct stat st; 895 char *bp; 896 char qf[20]; 897 char buf[MAXLINE]; 898 extern long atol(); 899 extern ADDRESS *setctluser(); 900 901 /* 902 ** Read and process the file. 903 */ 904 905 strcpy(qf, queuename(e, 'q')); 906 qfp = fopen(qf, "r+"); 907 if (qfp == NULL) 908 { 909 if (tTd(40, 8)) 910 printf("readqf(%s): fopen failure (%s)\n", 911 qf, errstring(errno)); 912 if (errno != ENOENT) 913 syserr("readqf: no control file %s", qf); 914 return FALSE; 915 } 916 917 if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB)) 918 { 919 /* being processed by another queuer */ 920 if (tTd(40, 8)) 921 printf("readqf(%s): locked\n", qf); 922 if (Verbose) 923 printf("%s: locked\n", e->e_id); 924 # ifdef LOG 925 if (LogLevel > 19) 926 syslog(LOG_DEBUG, "%s: locked", e->e_id); 927 # endif /* LOG */ 928 (void) fclose(qfp); 929 return FALSE; 930 } 931 932 /* 933 ** Check the queue file for plausibility to avoid attacks. 934 */ 935 936 if (fstat(fileno(qfp), &st) < 0) 937 { 938 /* must have been being processed by someone else */ 939 if (tTd(40, 8)) 940 printf("readqf(%s): fstat failure (%s)\n", 941 qf, errstring(errno)); 942 fclose(qfp); 943 return FALSE; 944 } 945 946 if (st.st_uid != geteuid()) 947 { 948 # ifdef LOG 949 if (LogLevel > 0) 950 { 951 syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 952 e->e_id, st.st_uid, st.st_mode); 953 } 954 # endif /* LOG */ 955 if (tTd(40, 8)) 956 printf("readqf(%s): bogus file\n", qf); 957 rename(qf, queuename(e, 'Q')); 958 fclose(qfp); 959 return FALSE; 960 } 961 962 if (st.st_size == 0) 963 { 964 /* must be a bogus file -- just remove it */ 965 (void) unlink(qf); 966 fclose(qfp); 967 return FALSE; 968 } 969 970 if (st.st_nlink == 0) 971 { 972 /* 973 ** Race condition -- we got a file just as it was being 974 ** unlinked. Just assume it is zero length. 975 */ 976 977 fclose(qfp); 978 return FALSE; 979 } 980 981 /* good file -- save this lock */ 982 e->e_lockfp = qfp; 983 984 /* do basic system initialization */ 985 initsys(e); 986 987 if (announcefile) 988 FileName = qf; 989 LineNumber = 0; 990 e->e_flags |= EF_GLOBALERRS; 991 OpMode = MD_DELIVER; 992 if (Verbose) 993 printf("\nRunning %s\n", e->e_id); 994 ctladdr = NULL; 995 while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 996 { 997 register char *p; 998 struct stat st; 999 1000 if (tTd(40, 4)) 1001 printf("+++++ %s\n", bp); 1002 switch (bp[0]) 1003 { 1004 case 'C': /* specify controlling user */ 1005 ctladdr = setctluser(&bp[1]); 1006 break; 1007 1008 case 'R': /* specify recipient */ 1009 (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 1010 break; 1011 1012 case 'E': /* specify error recipient */ 1013 (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 1014 break; 1015 1016 case 'H': /* header */ 1017 (void) chompheader(&bp[1], FALSE, e); 1018 break; 1019 1020 case 'M': /* message */ 1021 e->e_message = newstr(&bp[1]); 1022 break; 1023 1024 case 'S': /* sender */ 1025 setsender(newstr(&bp[1]), e, NULL, TRUE); 1026 break; 1027 1028 case 'B': /* body type */ 1029 e->e_bodytype = newstr(&bp[1]); 1030 break; 1031 1032 case 'D': /* data file name */ 1033 e->e_df = newstr(&bp[1]); 1034 e->e_dfp = fopen(e->e_df, "r"); 1035 if (e->e_dfp == NULL) 1036 { 1037 syserr("readqf: cannot open %s", e->e_df); 1038 e->e_msgsize = -1; 1039 } 1040 else if (fstat(fileno(e->e_dfp), &st) >= 0) 1041 e->e_msgsize = st.st_size; 1042 break; 1043 1044 case 'T': /* init time */ 1045 e->e_ctime = atol(&bp[1]); 1046 break; 1047 1048 case 'P': /* message priority */ 1049 e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 1050 break; 1051 1052 case 'F': /* flag bits */ 1053 for (p = &bp[1]; *p != '\0'; p++) 1054 { 1055 switch (*p) 1056 { 1057 case 'w': /* warning sent */ 1058 e->e_flags |= EF_WARNING; 1059 break; 1060 1061 case 'r': /* response */ 1062 e->e_flags |= EF_RESPONSE; 1063 break; 1064 } 1065 } 1066 break; 1067 1068 case '$': /* define macro */ 1069 define(bp[1], newstr(&bp[2]), e); 1070 break; 1071 1072 case '\0': /* blank line; ignore */ 1073 break; 1074 1075 default: 1076 syserr("readqf: bad line \"%s\"", e->e_id, 1077 LineNumber, bp); 1078 fclose(qfp); 1079 rename(qf, queuename(e, 'Q')); 1080 return FALSE; 1081 } 1082 1083 if (bp != buf) 1084 free(bp); 1085 } 1086 1087 FileName = NULL; 1088 1089 /* 1090 ** If we haven't read any lines, this queue file is empty. 1091 ** Arrange to remove it without referencing any null pointers. 1092 */ 1093 1094 if (LineNumber == 0) 1095 { 1096 errno = 0; 1097 e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 1098 } 1099 return TRUE; 1100 } 1101 /* 1102 ** PRINTQUEUE -- print out a representation of the mail queue 1103 ** 1104 ** Parameters: 1105 ** none. 1106 ** 1107 ** Returns: 1108 ** none. 1109 ** 1110 ** Side Effects: 1111 ** Prints a listing of the mail queue on the standard output. 1112 */ 1113 1114 printqueue() 1115 { 1116 register WORK *w; 1117 FILE *f; 1118 int nrequests; 1119 char buf[MAXLINE]; 1120 1121 /* 1122 ** Check for permission to print the queue 1123 */ 1124 1125 if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && RealUid != 0) 1126 { 1127 struct stat st; 1128 # ifdef NGROUPS 1129 int n; 1130 GIDSET_T gidset[NGROUPS]; 1131 # endif 1132 1133 if (stat(QueueDir, &st) < 0) 1134 { 1135 syserr("Cannot stat %s", QueueDir); 1136 return; 1137 } 1138 # ifdef NGROUPS 1139 n = getgroups(NGROUPS, gidset); 1140 while (--n >= 0) 1141 { 1142 if (gidset[n] == st.st_gid) 1143 break; 1144 } 1145 if (n < 0) 1146 # else 1147 if (RealGid != st.st_gid) 1148 # endif 1149 { 1150 usrerr("510 You are not permitted to see the queue"); 1151 setstat(EX_NOPERM); 1152 return; 1153 } 1154 } 1155 1156 /* 1157 ** Read and order the queue. 1158 */ 1159 1160 nrequests = orderq(TRUE); 1161 1162 /* 1163 ** Print the work list that we have read. 1164 */ 1165 1166 /* first see if there is anything */ 1167 if (nrequests <= 0) 1168 { 1169 printf("Mail queue is empty\n"); 1170 return; 1171 } 1172 1173 CurrentLA = getla(); /* get load average */ 1174 1175 printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 1176 if (nrequests > QUEUESIZE) 1177 printf(", only %d printed", QUEUESIZE); 1178 if (Verbose) 1179 printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 1180 else 1181 printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 1182 for (w = WorkQ; w != NULL; w = w->w_next) 1183 { 1184 struct stat st; 1185 auto time_t submittime = 0; 1186 long dfsize = -1; 1187 int flags = 0; 1188 char message[MAXLINE]; 1189 char bodytype[MAXNAME]; 1190 1191 f = fopen(w->w_name, "r"); 1192 if (f == NULL) 1193 { 1194 errno = 0; 1195 continue; 1196 } 1197 printf("%8s", w->w_name + 2); 1198 if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB)) 1199 printf("*"); 1200 else if (shouldqueue(w->w_pri, w->w_ctime)) 1201 printf("X"); 1202 else 1203 printf(" "); 1204 errno = 0; 1205 1206 message[0] = bodytype[0] = '\0'; 1207 while (fgets(buf, sizeof buf, f) != NULL) 1208 { 1209 register int i; 1210 register char *p; 1211 1212 fixcrlf(buf, TRUE); 1213 switch (buf[0]) 1214 { 1215 case 'M': /* error message */ 1216 if ((i = strlen(&buf[1])) >= sizeof message) 1217 i = sizeof message - 1; 1218 bcopy(&buf[1], message, i); 1219 message[i] = '\0'; 1220 break; 1221 1222 case 'B': /* body type */ 1223 if ((i = strlen(&buf[1])) >= sizeof bodytype) 1224 i = sizeof bodytype - 1; 1225 bcopy(&buf[1], bodytype, i); 1226 bodytype[i] = '\0'; 1227 break; 1228 1229 case 'S': /* sender name */ 1230 if (Verbose) 1231 printf("%8ld %10ld%c%.12s %.38s", 1232 dfsize, 1233 w->w_pri, 1234 bitset(EF_WARNING, flags) ? '+' : ' ', 1235 ctime(&submittime) + 4, 1236 &buf[1]); 1237 else 1238 printf("%8ld %.16s %.45s", dfsize, 1239 ctime(&submittime), &buf[1]); 1240 if (message[0] != '\0' || bodytype[0] != '\0') 1241 { 1242 printf("\n %10.10s", bodytype); 1243 if (message[0] != '\0') 1244 printf(" (%.60s)", message); 1245 } 1246 break; 1247 1248 case 'C': /* controlling user */ 1249 if (Verbose) 1250 printf("\n\t\t\t\t (---%.34s---)", 1251 &buf[1]); 1252 break; 1253 1254 case 'R': /* recipient name */ 1255 if (Verbose) 1256 printf("\n\t\t\t\t\t %.38s", &buf[1]); 1257 else 1258 printf("\n\t\t\t\t %.45s", &buf[1]); 1259 break; 1260 1261 case 'T': /* creation time */ 1262 submittime = atol(&buf[1]); 1263 break; 1264 1265 case 'D': /* data file name */ 1266 if (stat(&buf[1], &st) >= 0) 1267 dfsize = st.st_size; 1268 break; 1269 1270 case 'F': /* flag bits */ 1271 for (p = &buf[1]; *p != '\0'; p++) 1272 { 1273 switch (*p) 1274 { 1275 case 'w': 1276 flags |= EF_WARNING; 1277 break; 1278 } 1279 } 1280 } 1281 } 1282 if (submittime == (time_t) 0) 1283 printf(" (no control file)"); 1284 printf("\n"); 1285 (void) fclose(f); 1286 } 1287 } 1288 1289 # endif /* QUEUE */ 1290 /* 1291 ** QUEUENAME -- build a file name in the queue directory for this envelope. 1292 ** 1293 ** Assigns an id code if one does not already exist. 1294 ** This code is very careful to avoid trashing existing files 1295 ** under any circumstances. 1296 ** 1297 ** Parameters: 1298 ** e -- envelope to build it in/from. 1299 ** type -- the file type, used as the first character 1300 ** of the file name. 1301 ** 1302 ** Returns: 1303 ** a pointer to the new file name (in a static buffer). 1304 ** 1305 ** Side Effects: 1306 ** If no id code is already assigned, queuename will 1307 ** assign an id code, create a qf file, and leave a 1308 ** locked, open-for-write file pointer in the envelope. 1309 */ 1310 1311 char * 1312 queuename(e, type) 1313 register ENVELOPE *e; 1314 int type; 1315 { 1316 static int pid = -1; 1317 static char c0; 1318 static char c1; 1319 static char c2; 1320 time_t now; 1321 struct tm *tm; 1322 static char buf[MAXNAME]; 1323 1324 if (e->e_id == NULL) 1325 { 1326 char qf[20]; 1327 1328 /* find a unique id */ 1329 if (pid != getpid()) 1330 { 1331 /* new process -- start back at "AA" */ 1332 pid = getpid(); 1333 now = curtime(); 1334 tm = localtime(&now); 1335 c0 = 'A' + tm->tm_hour; 1336 c1 = 'A'; 1337 c2 = 'A' - 1; 1338 } 1339 (void) sprintf(qf, "qf%cAA%05d", c0, pid); 1340 1341 while (c1 < '~' || c2 < 'Z') 1342 { 1343 int i; 1344 1345 if (c2 >= 'Z') 1346 { 1347 c1++; 1348 c2 = 'A' - 1; 1349 } 1350 qf[3] = c1; 1351 qf[4] = ++c2; 1352 if (tTd(7, 20)) 1353 printf("queuename: trying \"%s\"\n", qf); 1354 1355 i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 1356 if (i < 0) 1357 { 1358 if (errno == EEXIST) 1359 continue; 1360 syserr("queuename: Cannot create \"%s\" in \"%s\"", 1361 qf, QueueDir); 1362 exit(EX_UNAVAILABLE); 1363 } 1364 if (lockfile(i, qf, LOCK_EX|LOCK_NB)) 1365 { 1366 e->e_lockfp = fdopen(i, "w"); 1367 break; 1368 } 1369 1370 /* a reader got the file; abandon it and try again */ 1371 (void) close(i); 1372 } 1373 if (c1 >= '~' && c2 >= 'Z') 1374 { 1375 syserr("queuename: Cannot create \"%s\" in \"%s\"", 1376 qf, QueueDir); 1377 exit(EX_OSERR); 1378 } 1379 e->e_id = newstr(&qf[2]); 1380 define('i', e->e_id, e); 1381 if (tTd(7, 1)) 1382 printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 1383 # ifdef LOG 1384 if (LogLevel > 93) 1385 syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 1386 # endif /* LOG */ 1387 } 1388 1389 if (type == '\0') 1390 return (NULL); 1391 (void) sprintf(buf, "%cf%s", type, e->e_id); 1392 if (tTd(7, 2)) 1393 printf("queuename: %s\n", buf); 1394 return (buf); 1395 } 1396 /* 1397 ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 1398 ** 1399 ** Parameters: 1400 ** e -- the envelope to unlock. 1401 ** 1402 ** Returns: 1403 ** none 1404 ** 1405 ** Side Effects: 1406 ** unlocks the queue for `e'. 1407 */ 1408 1409 unlockqueue(e) 1410 ENVELOPE *e; 1411 { 1412 if (tTd(51, 4)) 1413 printf("unlockqueue(%s)\n", e->e_id); 1414 1415 /* if there is a lock file in the envelope, close it */ 1416 if (e->e_lockfp != NULL) 1417 xfclose(e->e_lockfp, "unlockqueue", e->e_id); 1418 e->e_lockfp = NULL; 1419 1420 /* don't create a queue id if we don't already have one */ 1421 if (e->e_id == NULL) 1422 return; 1423 1424 /* remove the transcript */ 1425 # ifdef LOG 1426 if (LogLevel > 87) 1427 syslog(LOG_DEBUG, "%s: unlock", e->e_id); 1428 # endif /* LOG */ 1429 if (!tTd(51, 104)) 1430 xunlink(queuename(e, 'x')); 1431 1432 } 1433 /* 1434 ** SETCTLUSER -- create a controlling address 1435 ** 1436 ** Create a fake "address" given only a local login name; this is 1437 ** used as a "controlling user" for future recipient addresses. 1438 ** 1439 ** Parameters: 1440 ** user -- the user name of the controlling user. 1441 ** 1442 ** Returns: 1443 ** An address descriptor for the controlling user. 1444 ** 1445 ** Side Effects: 1446 ** none. 1447 */ 1448 1449 ADDRESS * 1450 setctluser(user) 1451 char *user; 1452 { 1453 register ADDRESS *a; 1454 struct passwd *pw; 1455 char *p; 1456 1457 /* 1458 ** See if this clears our concept of controlling user. 1459 */ 1460 1461 if (user == NULL || *user == '\0') 1462 return NULL; 1463 1464 /* 1465 ** Set up addr fields for controlling user. 1466 */ 1467 1468 a = (ADDRESS *) xalloc(sizeof *a); 1469 bzero((char *) a, sizeof *a); 1470 1471 p = strchr(user, ':'); 1472 if (p != NULL) 1473 *p++ = '\0'; 1474 if (*user != '\0' && (pw = getpwnam(user)) != NULL) 1475 { 1476 a->q_home = newstr(pw->pw_dir); 1477 a->q_uid = pw->pw_uid; 1478 a->q_gid = pw->pw_gid; 1479 a->q_user = newstr(user); 1480 a->q_flags |= QGOODUID; 1481 } 1482 else 1483 { 1484 a->q_user = newstr(DefUser); 1485 } 1486 1487 a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 1488 a->q_mailer = LocalMailer; 1489 if (p == NULL) 1490 a->q_paddr = a->q_user; 1491 else 1492 a->q_paddr = newstr(p); 1493 return a; 1494 } 1495