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.47 (Berkeley) 04/15/93 (with queueing)"; 14 #else 15 static char sccsid[] = "@(#)queue.c 6.47 (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 /* save this lock */ 932 e->e_lockfp = qfp; 933 934 /* do basic system initialization */ 935 initsys(e); 936 937 FileName = qf; 938 LineNumber = 0; 939 if (Verbose) 940 printf("\nRunning %s\n", e->e_id); 941 ctladdr = NULL; 942 while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 943 { 944 register char *p; 945 struct stat st; 946 947 if (tTd(40, 4)) 948 printf("+++++ %s\n", bp); 949 switch (bp[0]) 950 { 951 case 'C': /* specify controlling user */ 952 ctladdr = setctluser(&bp[1]); 953 break; 954 955 case 'R': /* specify recipient */ 956 (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 957 break; 958 959 case 'E': /* specify error recipient */ 960 (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 961 break; 962 963 case 'H': /* header */ 964 (void) chompheader(&bp[1], FALSE, e); 965 break; 966 967 case 'M': /* message */ 968 e->e_message = newstr(&bp[1]); 969 break; 970 971 case 'S': /* sender */ 972 setsender(newstr(&bp[1]), e, NULL, TRUE); 973 break; 974 975 case 'B': /* body type */ 976 e->e_bodytype = newstr(&bp[1]); 977 break; 978 979 case 'D': /* data file name */ 980 e->e_df = newstr(&bp[1]); 981 e->e_dfp = fopen(e->e_df, "r"); 982 if (e->e_dfp == NULL) 983 { 984 syserr("readqf: cannot open %s", e->e_df); 985 e->e_msgsize = -1; 986 } 987 else if (fstat(fileno(e->e_dfp), &st) >= 0) 988 e->e_msgsize = st.st_size; 989 break; 990 991 case 'T': /* init time */ 992 e->e_ctime = atol(&bp[1]); 993 break; 994 995 case 'P': /* message priority */ 996 e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 997 break; 998 999 case 'F': /* flag bits */ 1000 for (p = &bp[1]; *p != '\0'; p++) 1001 { 1002 switch (*p) 1003 { 1004 case 'w': /* warning sent */ 1005 e->e_flags |= EF_WARNING; 1006 break; 1007 1008 case 'r': /* response */ 1009 e->e_flags |= EF_RESPONSE; 1010 break; 1011 } 1012 } 1013 break; 1014 1015 case '$': /* define macro */ 1016 define(bp[1], newstr(&bp[2]), e); 1017 break; 1018 1019 case '\0': /* blank line; ignore */ 1020 break; 1021 1022 default: 1023 syserr("readqf(%s:%d): bad line \"%s\"", e->e_id, 1024 LineNumber, bp); 1025 break; 1026 } 1027 1028 if (bp != buf) 1029 free(bp); 1030 } 1031 1032 FileName = NULL; 1033 1034 /* 1035 ** If we haven't read any lines, this queue file is empty. 1036 ** Arrange to remove it without referencing any null pointers. 1037 */ 1038 1039 if (LineNumber == 0) 1040 { 1041 errno = 0; 1042 e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 1043 } 1044 return TRUE; 1045 } 1046 /* 1047 ** PRINTQUEUE -- print out a representation of the mail queue 1048 ** 1049 ** Parameters: 1050 ** none. 1051 ** 1052 ** Returns: 1053 ** none. 1054 ** 1055 ** Side Effects: 1056 ** Prints a listing of the mail queue on the standard output. 1057 */ 1058 1059 printqueue() 1060 { 1061 register WORK *w; 1062 FILE *f; 1063 int nrequests; 1064 char buf[MAXLINE]; 1065 1066 /* 1067 ** Check for permission to print the queue 1068 */ 1069 1070 if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0) 1071 { 1072 struct stat st; 1073 # ifdef NGROUPS 1074 int n; 1075 int gidset[NGROUPS]; 1076 # endif 1077 1078 if (stat(QueueDir, &st) < 0) 1079 { 1080 syserr("Cannot stat %s", QueueDir); 1081 return; 1082 } 1083 # ifdef NGROUPS 1084 n = getgroups(NGROUPS, gidset); 1085 while (--n >= 0) 1086 { 1087 if (gidset[n] == st.st_gid) 1088 break; 1089 } 1090 if (n < 0) 1091 # else 1092 if (getgid() != st.st_gid) 1093 # endif 1094 { 1095 usrerr("510 You are not permitted to see the queue"); 1096 setstat(EX_NOPERM); 1097 return; 1098 } 1099 } 1100 1101 /* 1102 ** Read and order the queue. 1103 */ 1104 1105 nrequests = orderq(TRUE); 1106 1107 /* 1108 ** Print the work list that we have read. 1109 */ 1110 1111 /* first see if there is anything */ 1112 if (nrequests <= 0) 1113 { 1114 printf("Mail queue is empty\n"); 1115 return; 1116 } 1117 1118 CurrentLA = getla(); /* get load average */ 1119 1120 printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 1121 if (nrequests > QUEUESIZE) 1122 printf(", only %d printed", QUEUESIZE); 1123 if (Verbose) 1124 printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 1125 else 1126 printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 1127 for (w = WorkQ; w != NULL; w = w->w_next) 1128 { 1129 struct stat st; 1130 auto time_t submittime = 0; 1131 long dfsize = -1; 1132 int flags = 0; 1133 char message[MAXLINE]; 1134 char bodytype[MAXNAME]; 1135 extern bool shouldqueue(); 1136 extern bool lockfile(); 1137 1138 f = fopen(w->w_name, "r"); 1139 if (f == NULL) 1140 { 1141 errno = 0; 1142 continue; 1143 } 1144 printf("%8s", w->w_name + 2); 1145 if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB)) 1146 printf("*"); 1147 else if (shouldqueue(w->w_pri, w->w_ctime)) 1148 printf("X"); 1149 else 1150 printf(" "); 1151 errno = 0; 1152 1153 message[0] = bodytype[0] = '\0'; 1154 while (fgets(buf, sizeof buf, f) != NULL) 1155 { 1156 register int i; 1157 register char *p; 1158 1159 fixcrlf(buf, TRUE); 1160 switch (buf[0]) 1161 { 1162 case 'M': /* error message */ 1163 if ((i = strlen(&buf[1])) >= sizeof message) 1164 i = sizeof message - 1; 1165 bcopy(&buf[1], message, i); 1166 message[i] = '\0'; 1167 break; 1168 1169 case 'B': /* body type */ 1170 if ((i = strlen(&buf[1])) >= sizeof bodytype) 1171 i = sizeof bodytype - 1; 1172 bcopy(&buf[1], bodytype, i); 1173 bodytype[i] = '\0'; 1174 break; 1175 1176 case 'S': /* sender name */ 1177 if (Verbose) 1178 printf("%8ld %10ld%c%.12s %.38s", 1179 dfsize, 1180 w->w_pri, 1181 bitset(EF_WARNING, flags) ? '+' : ' ', 1182 ctime(&submittime) + 4, 1183 &buf[1]); 1184 else 1185 printf("%8ld %.16s %.45s", dfsize, 1186 ctime(&submittime), &buf[1]); 1187 if (message[0] != '\0' || bodytype[0] != '\0') 1188 { 1189 printf("\n %10.10s", bodytype); 1190 if (message[0] != '\0') 1191 printf(" (%.60s)", message); 1192 } 1193 break; 1194 1195 case 'C': /* controlling user */ 1196 if (Verbose) 1197 printf("\n\t\t\t\t (---%.34s---)", 1198 &buf[1]); 1199 break; 1200 1201 case 'R': /* recipient name */ 1202 if (Verbose) 1203 printf("\n\t\t\t\t\t %.38s", &buf[1]); 1204 else 1205 printf("\n\t\t\t\t %.45s", &buf[1]); 1206 break; 1207 1208 case 'T': /* creation time */ 1209 submittime = atol(&buf[1]); 1210 break; 1211 1212 case 'D': /* data file name */ 1213 if (stat(&buf[1], &st) >= 0) 1214 dfsize = st.st_size; 1215 break; 1216 1217 case 'F': /* flag bits */ 1218 for (p = &buf[1]; *p != '\0'; p++) 1219 { 1220 switch (*p) 1221 { 1222 case 'w': 1223 flags |= EF_WARNING; 1224 break; 1225 } 1226 } 1227 } 1228 } 1229 if (submittime == (time_t) 0) 1230 printf(" (no control file)"); 1231 printf("\n"); 1232 (void) fclose(f); 1233 } 1234 } 1235 1236 # endif /* QUEUE */ 1237 /* 1238 ** QUEUENAME -- build a file name in the queue directory for this envelope. 1239 ** 1240 ** Assigns an id code if one does not already exist. 1241 ** This code is very careful to avoid trashing existing files 1242 ** under any circumstances. 1243 ** 1244 ** Parameters: 1245 ** e -- envelope to build it in/from. 1246 ** type -- the file type, used as the first character 1247 ** of the file name. 1248 ** 1249 ** Returns: 1250 ** a pointer to the new file name (in a static buffer). 1251 ** 1252 ** Side Effects: 1253 ** If no id code is already assigned, queuename will 1254 ** assign an id code, create a qf file, and leave a 1255 ** locked, open-for-write file pointer in the envelope. 1256 */ 1257 1258 char * 1259 queuename(e, type) 1260 register ENVELOPE *e; 1261 char type; 1262 { 1263 static int pid = -1; 1264 static char c0; 1265 static char c1; 1266 static char c2; 1267 time_t now; 1268 struct tm *tm; 1269 static char buf[MAXNAME]; 1270 extern bool lockfile(); 1271 1272 if (e->e_id == NULL) 1273 { 1274 char qf[20]; 1275 1276 /* find a unique id */ 1277 if (pid != getpid()) 1278 { 1279 /* new process -- start back at "AA" */ 1280 pid = getpid(); 1281 now = curtime(); 1282 tm = localtime(&now); 1283 c0 = 'A' + tm->tm_hour; 1284 c1 = 'A'; 1285 c2 = 'A' - 1; 1286 } 1287 (void) sprintf(qf, "qf%cAA%05d", c0, pid); 1288 1289 while (c1 < '~' || c2 < 'Z') 1290 { 1291 int i; 1292 1293 if (c2 >= 'Z') 1294 { 1295 c1++; 1296 c2 = 'A' - 1; 1297 } 1298 qf[3] = c1; 1299 qf[4] = ++c2; 1300 if (tTd(7, 20)) 1301 printf("queuename: trying \"%s\"\n", qf); 1302 1303 i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 1304 if (i < 0) 1305 { 1306 if (errno == EEXIST) 1307 continue; 1308 syserr("queuename: Cannot create \"%s\" in \"%s\"", 1309 qf, QueueDir); 1310 exit(EX_UNAVAILABLE); 1311 } 1312 if (lockfile(i, qf, LOCK_EX|LOCK_NB)) 1313 { 1314 e->e_lockfp = fdopen(i, "w"); 1315 break; 1316 } 1317 1318 /* a reader got the file; abandon it and try again */ 1319 (void) close(i); 1320 } 1321 if (c1 >= '~' && c2 >= 'Z') 1322 { 1323 syserr("queuename: Cannot create \"%s\" in \"%s\"", 1324 qf, QueueDir); 1325 exit(EX_OSERR); 1326 } 1327 e->e_id = newstr(&qf[2]); 1328 define('i', e->e_id, e); 1329 if (tTd(7, 1)) 1330 printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 1331 # ifdef LOG 1332 if (LogLevel > 93) 1333 syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 1334 # endif /* LOG */ 1335 } 1336 1337 if (type == '\0') 1338 return (NULL); 1339 (void) sprintf(buf, "%cf%s", type, e->e_id); 1340 if (tTd(7, 2)) 1341 printf("queuename: %s\n", buf); 1342 return (buf); 1343 } 1344 /* 1345 ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 1346 ** 1347 ** Parameters: 1348 ** e -- the envelope to unlock. 1349 ** 1350 ** Returns: 1351 ** none 1352 ** 1353 ** Side Effects: 1354 ** unlocks the queue for `e'. 1355 */ 1356 1357 unlockqueue(e) 1358 ENVELOPE *e; 1359 { 1360 if (tTd(51, 4)) 1361 printf("unlockqueue(%s)\n", e->e_id); 1362 1363 /* if there is a lock file in the envelope, close it */ 1364 if (e->e_lockfp != NULL) 1365 xfclose(e->e_lockfp, "unlockqueue", e->e_id); 1366 e->e_lockfp = NULL; 1367 1368 /* don't create a queue id if we don't already have one */ 1369 if (e->e_id == NULL) 1370 return; 1371 1372 /* remove the transcript */ 1373 # ifdef LOG 1374 if (LogLevel > 87) 1375 syslog(LOG_DEBUG, "%s: unlock", e->e_id); 1376 # endif /* LOG */ 1377 if (!tTd(51, 104)) 1378 xunlink(queuename(e, 'x')); 1379 1380 } 1381 /* 1382 ** SETCTLUSER -- create a controlling address 1383 ** 1384 ** Create a fake "address" given only a local login name; this is 1385 ** used as a "controlling user" for future recipient addresses. 1386 ** 1387 ** Parameters: 1388 ** user -- the user name of the controlling user. 1389 ** 1390 ** Returns: 1391 ** An address descriptor for the controlling user. 1392 ** 1393 ** Side Effects: 1394 ** none. 1395 */ 1396 1397 ADDRESS * 1398 setctluser(user) 1399 char *user; 1400 { 1401 register ADDRESS *a; 1402 struct passwd *pw; 1403 1404 /* 1405 ** See if this clears our concept of controlling user. 1406 */ 1407 1408 if (user == NULL || *user == '\0') 1409 user = DefUser; 1410 1411 /* 1412 ** Set up addr fields for controlling user. 1413 */ 1414 1415 a = (ADDRESS *) xalloc(sizeof *a); 1416 bzero((char *) a, sizeof *a); 1417 if ((pw = getpwnam(user)) != NULL) 1418 { 1419 a->q_home = newstr(pw->pw_dir); 1420 a->q_uid = pw->pw_uid; 1421 a->q_gid = pw->pw_gid; 1422 a->q_user = newstr(user); 1423 } 1424 else 1425 { 1426 a->q_uid = DefUid; 1427 a->q_gid = DefGid; 1428 a->q_user = newstr(DefUser); 1429 } 1430 1431 a->q_flags |= QGOODUID|QPRIMARY; /* flag as a "ctladdr" */ 1432 a->q_mailer = LocalMailer; 1433 return a; 1434 } 1435