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