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