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