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