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.41 (Berkeley) 04/01/93 (with queueing)"; 14 #else 15 static char sccsid[] = "@(#)queue.c 6.41 (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 continue; 482 } 483 484 dowork(w->w_name + 2, ForkQueueRuns, e); 485 free(w->w_name); 486 free((char *) w); 487 } 488 489 /* exit without the usual cleanup */ 490 e->e_id = NULL; 491 finis(); 492 } 493 /* 494 ** ORDERQ -- order the work queue. 495 ** 496 ** Parameters: 497 ** doall -- if set, include everything in the queue (even 498 ** the jobs that cannot be run because the load 499 ** average is too high). Otherwise, exclude those 500 ** jobs. 501 ** 502 ** Returns: 503 ** The number of request in the queue (not necessarily 504 ** the number of requests in WorkQ however). 505 ** 506 ** Side Effects: 507 ** Sets WorkQ to the queue of available work, in order. 508 */ 509 510 # define NEED_P 001 511 # define NEED_T 002 512 # define NEED_R 004 513 # define NEED_S 010 514 515 orderq(doall) 516 bool doall; 517 { 518 register struct direct *d; 519 register WORK *w; 520 DIR *f; 521 register int i; 522 WORK wlist[QUEUESIZE+1]; 523 int wn = -1; 524 extern workcmpf(); 525 526 if (tTd(41, 1)) 527 { 528 printf("orderq:\n"); 529 if (QueueLimitId != NULL) 530 printf("\tQueueLimitId = %s\n", QueueLimitId); 531 if (QueueLimitSender != NULL) 532 printf("\tQueueLimitSender = %s\n", QueueLimitSender); 533 if (QueueLimitRecipient != NULL) 534 printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 535 } 536 537 /* clear out old WorkQ */ 538 for (w = WorkQ; w != NULL; ) 539 { 540 register WORK *nw = w->w_next; 541 542 WorkQ = nw; 543 free(w->w_name); 544 free((char *) w); 545 w = nw; 546 } 547 548 /* open the queue directory */ 549 f = opendir("."); 550 if (f == NULL) 551 { 552 syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 553 return (0); 554 } 555 556 /* 557 ** Read the work directory. 558 */ 559 560 while ((d = readdir(f)) != NULL) 561 { 562 FILE *cf; 563 char lbuf[MAXNAME]; 564 extern bool shouldqueue(); 565 extern bool strcontainedin(); 566 567 /* is this an interesting entry? */ 568 if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 569 continue; 570 571 if (QueueLimitId != NULL && 572 !strcontainedin(QueueLimitId, d->d_name)) 573 continue; 574 575 /* 576 ** Check queue name for plausibility. This handles 577 ** both old and new type ids. 578 */ 579 580 i = strlen(d->d_name); 581 if (i != 9 && i != 10) 582 { 583 if (Verbose) 584 printf("orderq: bogus qf name %s\n", d->d_name); 585 #ifdef LOG 586 if (LogLevel > 3) 587 syslog(LOG_NOTICE, "orderq: bogus qf name %s", 588 d->d_name); 589 #endif 590 if (strlen(d->d_name) >= MAXNAME) 591 d->d_name[MAXNAME - 1] = '\0'; 592 strcpy(lbuf, d->d_name); 593 lbuf[0] = 'Q'; 594 (void) rename(d->d_name, lbuf); 595 continue; 596 } 597 598 /* yes -- open control file (if not too many files) */ 599 if (++wn >= QUEUESIZE) 600 continue; 601 602 cf = fopen(d->d_name, "r"); 603 if (cf == NULL) 604 { 605 /* this may be some random person sending hir msgs */ 606 /* syserr("orderq: cannot open %s", cbuf); */ 607 if (tTd(41, 2)) 608 printf("orderq: cannot open %s (%d)\n", 609 d->d_name, errno); 610 errno = 0; 611 wn--; 612 continue; 613 } 614 w = &wlist[wn]; 615 w->w_name = newstr(d->d_name); 616 617 /* make sure jobs in creation don't clog queue */ 618 w->w_pri = 0x7fffffff; 619 w->w_ctime = 0; 620 621 /* extract useful information */ 622 i = NEED_P | NEED_T; 623 if (QueueLimitSender != NULL) 624 i |= NEED_S; 625 if (QueueLimitRecipient != NULL) 626 i |= NEED_R; 627 while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 628 { 629 extern long atol(); 630 extern bool strcontainedin(); 631 632 switch (lbuf[0]) 633 { 634 case 'P': 635 w->w_pri = atol(&lbuf[1]); 636 i &= ~NEED_P; 637 break; 638 639 case 'T': 640 w->w_ctime = atol(&lbuf[1]); 641 i &= ~NEED_T; 642 break; 643 644 case 'R': 645 if (QueueLimitRecipient != NULL && 646 strcontainedin(QueueLimitRecipient, &lbuf[1])) 647 i &= ~NEED_R; 648 break; 649 650 case 'S': 651 if (QueueLimitSender != NULL && 652 strcontainedin(QueueLimitSender, &lbuf[1])) 653 i &= ~NEED_S; 654 break; 655 } 656 } 657 (void) fclose(cf); 658 659 if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 660 bitset(NEED_R|NEED_S, i)) 661 { 662 /* don't even bother sorting this job in */ 663 wn--; 664 } 665 } 666 (void) closedir(f); 667 wn++; 668 669 /* 670 ** Sort the work directory. 671 */ 672 673 qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 674 675 /* 676 ** Convert the work list into canonical form. 677 ** Should be turning it into a list of envelopes here perhaps. 678 */ 679 680 WorkQ = NULL; 681 for (i = min(wn, QUEUESIZE); --i >= 0; ) 682 { 683 w = (WORK *) xalloc(sizeof *w); 684 w->w_name = wlist[i].w_name; 685 w->w_pri = wlist[i].w_pri; 686 w->w_ctime = wlist[i].w_ctime; 687 w->w_next = WorkQ; 688 WorkQ = w; 689 } 690 691 if (tTd(40, 1)) 692 { 693 for (w = WorkQ; w != NULL; w = w->w_next) 694 printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 695 } 696 697 return (wn); 698 } 699 /* 700 ** WORKCMPF -- compare function for ordering work. 701 ** 702 ** Parameters: 703 ** a -- the first argument. 704 ** b -- the second argument. 705 ** 706 ** Returns: 707 ** -1 if a < b 708 ** 0 if a == b 709 ** +1 if a > b 710 ** 711 ** Side Effects: 712 ** none. 713 */ 714 715 workcmpf(a, b) 716 register WORK *a; 717 register WORK *b; 718 { 719 long pa = a->w_pri; 720 long pb = b->w_pri; 721 722 if (pa == pb) 723 return (0); 724 else if (pa > pb) 725 return (1); 726 else 727 return (-1); 728 } 729 /* 730 ** DOWORK -- do a work request. 731 ** 732 ** Parameters: 733 ** id -- the ID of the job to run. 734 ** forkflag -- if set, run this in background. 735 ** e - the envelope in which to run it. 736 ** 737 ** Returns: 738 ** none. 739 ** 740 ** Side Effects: 741 ** The work request is satisfied if possible. 742 */ 743 744 dowork(id, forkflag, e) 745 char *id; 746 bool forkflag; 747 register ENVELOPE *e; 748 { 749 register int i; 750 extern bool readqf(); 751 752 if (tTd(40, 1)) 753 printf("dowork(%s)\n", id); 754 755 /* 756 ** Fork for work. 757 */ 758 759 if (forkflag) 760 { 761 i = fork(); 762 if (i < 0) 763 { 764 syserr("dowork: cannot fork"); 765 return; 766 } 767 } 768 else 769 { 770 i = 0; 771 } 772 773 if (i == 0) 774 { 775 /* 776 ** CHILD 777 ** Lock the control file to avoid duplicate deliveries. 778 ** Then run the file as though we had just read it. 779 ** We save an idea of the temporary name so we 780 ** can recover on interrupt. 781 */ 782 783 /* set basic modes, etc. */ 784 (void) alarm(0); 785 clearenvelope(e, FALSE); 786 e->e_flags |= EF_QUEUERUN; 787 e->e_errormode = EM_MAIL; 788 e->e_id = id; 789 # ifdef LOG 790 if (LogLevel > 76) 791 syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 792 getpid()); 793 # endif /* LOG */ 794 795 /* don't use the headers from sendmail.cf... */ 796 e->e_header = NULL; 797 798 /* read the queue control file -- return if locked */ 799 if (!readqf(e)) 800 { 801 if (tTd(40, 4)) 802 printf("readqf(%s) failed\n", e->e_id); 803 if (forkflag) 804 exit(EX_OK); 805 else 806 return; 807 } 808 809 e->e_flags |= EF_INQUEUE; 810 eatheader(e); 811 812 /* do the delivery */ 813 if (!bitset(EF_FATALERRS, e->e_flags)) 814 sendall(e, SM_DELIVER); 815 816 /* finish up and exit */ 817 if (forkflag) 818 finis(); 819 else 820 dropenvelope(e); 821 } 822 else 823 { 824 /* 825 ** Parent -- pick up results. 826 */ 827 828 errno = 0; 829 (void) waitfor(i); 830 } 831 } 832 /* 833 ** READQF -- read queue file and set up environment. 834 ** 835 ** Parameters: 836 ** e -- the envelope of the job to run. 837 ** 838 ** Returns: 839 ** TRUE if it successfully read the queue file. 840 ** FALSE otherwise. 841 ** 842 ** Side Effects: 843 ** The queue file is returned locked. 844 */ 845 846 bool 847 readqf(e) 848 register ENVELOPE *e; 849 { 850 char *qf; 851 register FILE *qfp; 852 ADDRESS *ctladdr; 853 struct stat st; 854 char *bp; 855 char buf[MAXLINE]; 856 extern char *fgetfolded(); 857 extern long atol(); 858 extern ADDRESS *setctluser(); 859 extern bool lockfile(); 860 861 /* 862 ** Read and process the file. 863 */ 864 865 qf = queuename(e, 'q'); 866 qfp = fopen(qf, "r+"); 867 if (qfp == NULL) 868 { 869 if (tTd(40, 8)) 870 printf("readqf(%s): fopen failure (%s)\n", 871 qf, errstring(errno)); 872 if (errno != ENOENT) 873 syserr("readqf: no control file %s", qf); 874 return FALSE; 875 } 876 877 /* 878 ** Check the queue file for plausibility to avoid attacks. 879 */ 880 881 if (fstat(fileno(qfp), &st) < 0) 882 { 883 /* must have been being processed by someone else */ 884 if (tTd(40, 8)) 885 printf("readqf(%s): fstat failure (%s)\n", 886 qf, errstring(errno)); 887 fclose(qfp); 888 return FALSE; 889 } 890 891 if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode) 892 { 893 # ifdef LOG 894 if (LogLevel > 0) 895 { 896 syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 897 e->e_id, st.st_uid, st.st_mode); 898 } 899 # endif /* LOG */ 900 if (tTd(40, 8)) 901 printf("readqf(%s): bogus file\n", qf); 902 fclose(qfp); 903 return FALSE; 904 } 905 906 if (!lockfile(fileno(qfp), qf, LOCK_EX|LOCK_NB)) 907 { 908 /* being processed by another queuer */ 909 if (tTd(40, 8)) 910 printf("readqf(%s): locked\n", qf); 911 if (Verbose) 912 printf("%s: locked\n", e->e_id); 913 # ifdef LOG 914 if (LogLevel > 19) 915 syslog(LOG_DEBUG, "%s: locked", e->e_id); 916 # endif /* LOG */ 917 (void) fclose(qfp); 918 return FALSE; 919 } 920 921 /* save this lock */ 922 e->e_lockfp = qfp; 923 924 /* do basic system initialization */ 925 initsys(e); 926 927 FileName = qf; 928 LineNumber = 0; 929 if (Verbose) 930 printf("\nRunning %s\n", e->e_id); 931 ctladdr = NULL; 932 while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 933 { 934 register char *p; 935 struct stat st; 936 937 if (tTd(40, 4)) 938 printf("+++++ %s\n", bp); 939 switch (bp[0]) 940 { 941 case 'C': /* specify controlling user */ 942 ctladdr = setctluser(&bp[1]); 943 break; 944 945 case 'R': /* specify recipient */ 946 (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 947 break; 948 949 case 'E': /* specify error recipient */ 950 (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 951 break; 952 953 case 'H': /* header */ 954 (void) chompheader(&bp[1], FALSE, e); 955 break; 956 957 case 'M': /* message */ 958 e->e_message = newstr(&bp[1]); 959 break; 960 961 case 'S': /* sender */ 962 setsender(newstr(&bp[1]), e, NULL, TRUE); 963 break; 964 965 case 'D': /* data file name */ 966 e->e_df = newstr(&bp[1]); 967 e->e_dfp = fopen(e->e_df, "r"); 968 if (e->e_dfp == NULL) 969 { 970 syserr("readqf: cannot open %s", e->e_df); 971 e->e_msgsize = -1; 972 } 973 else if (fstat(fileno(e->e_dfp), &st) >= 0) 974 e->e_msgsize = st.st_size; 975 break; 976 977 case 'T': /* init time */ 978 e->e_ctime = atol(&bp[1]); 979 break; 980 981 case 'P': /* message priority */ 982 e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 983 break; 984 985 case 'F': /* flag bits */ 986 for (p = &bp[1]; *p != '\0'; p++) 987 { 988 switch (*p) 989 { 990 case 'w': /* warning sent */ 991 e->e_flags |= EF_WARNING; 992 break; 993 994 case 'r': /* response */ 995 e->e_flags |= EF_RESPONSE; 996 break; 997 } 998 } 999 break; 1000 1001 case '$': /* define macro */ 1002 define(bp[1], newstr(&bp[2]), e); 1003 break; 1004 1005 case '\0': /* blank line; ignore */ 1006 break; 1007 1008 default: 1009 syserr("readqf(%s:%d): bad line \"%s\"", e->e_id, 1010 LineNumber, bp); 1011 break; 1012 } 1013 1014 if (bp != buf) 1015 free(bp); 1016 } 1017 1018 FileName = NULL; 1019 1020 /* 1021 ** If we haven't read any lines, this queue file is empty. 1022 ** Arrange to remove it without referencing any null pointers. 1023 */ 1024 1025 if (LineNumber == 0) 1026 { 1027 errno = 0; 1028 e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 1029 } 1030 return TRUE; 1031 } 1032 /* 1033 ** PRINTQUEUE -- print out a representation of the mail queue 1034 ** 1035 ** Parameters: 1036 ** none. 1037 ** 1038 ** Returns: 1039 ** none. 1040 ** 1041 ** Side Effects: 1042 ** Prints a listing of the mail queue on the standard output. 1043 */ 1044 1045 printqueue() 1046 { 1047 register WORK *w; 1048 FILE *f; 1049 int nrequests; 1050 char buf[MAXLINE]; 1051 1052 /* 1053 ** Check for permission to print the queue 1054 */ 1055 1056 if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && getuid() != 0) 1057 { 1058 struct stat st; 1059 # ifdef NGROUPS 1060 int n; 1061 int gidset[NGROUPS]; 1062 # endif 1063 1064 if (stat(QueueDir, &st) < 0) 1065 { 1066 syserr("Cannot stat %s", QueueDir); 1067 return; 1068 } 1069 # ifdef NGROUPS 1070 n = getgroups(NGROUPS, gidset); 1071 while (--n >= 0) 1072 { 1073 if (gidset[n] == st.st_gid) 1074 break; 1075 } 1076 if (n < 0) 1077 # else 1078 if (getgid() != st.st_gid) 1079 # endif 1080 { 1081 usrerr("510 You are not permitted to see the queue"); 1082 setstat(EX_NOPERM); 1083 return; 1084 } 1085 } 1086 1087 /* 1088 ** Read and order the queue. 1089 */ 1090 1091 nrequests = orderq(TRUE); 1092 1093 /* 1094 ** Print the work list that we have read. 1095 */ 1096 1097 /* first see if there is anything */ 1098 if (nrequests <= 0) 1099 { 1100 printf("Mail queue is empty\n"); 1101 return; 1102 } 1103 1104 CurrentLA = getla(); /* get load average */ 1105 1106 printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 1107 if (nrequests > QUEUESIZE) 1108 printf(", only %d printed", QUEUESIZE); 1109 if (Verbose) 1110 printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 1111 else 1112 printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 1113 for (w = WorkQ; w != NULL; w = w->w_next) 1114 { 1115 struct stat st; 1116 auto time_t submittime = 0; 1117 long dfsize = -1; 1118 int flags = 0; 1119 char message[MAXLINE]; 1120 extern bool shouldqueue(); 1121 extern bool lockfile(); 1122 1123 f = fopen(w->w_name, "r"); 1124 if (f == NULL) 1125 { 1126 errno = 0; 1127 continue; 1128 } 1129 printf("%8s", w->w_name + 2); 1130 if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB)) 1131 printf("*"); 1132 else if (shouldqueue(w->w_pri, w->w_ctime)) 1133 printf("X"); 1134 else 1135 printf(" "); 1136 errno = 0; 1137 1138 message[0] = '\0'; 1139 while (fgets(buf, sizeof buf, f) != NULL) 1140 { 1141 register int i; 1142 register char *p; 1143 1144 fixcrlf(buf, TRUE); 1145 switch (buf[0]) 1146 { 1147 case 'M': /* error message */ 1148 if ((i = strlen(&buf[1])) >= sizeof message) 1149 i = sizeof message - 1; 1150 bcopy(&buf[1], message, i); 1151 message[i] = '\0'; 1152 break; 1153 1154 case 'S': /* sender name */ 1155 if (Verbose) 1156 printf("%8ld %10ld%c%.12s %.38s", 1157 dfsize, 1158 w->w_pri, 1159 bitset(EF_WARNING, flags) ? '+' : ' ', 1160 ctime(&submittime) + 4, 1161 &buf[1]); 1162 else 1163 printf("%8ld %.16s %.45s", dfsize, 1164 ctime(&submittime), &buf[1]); 1165 if (message[0] != '\0') 1166 printf("\n\t\t (%.60s)", message); 1167 break; 1168 1169 case 'C': /* controlling user */ 1170 if (Verbose) 1171 printf("\n\t\t\t\t (---%.34s---)", 1172 &buf[1]); 1173 break; 1174 1175 case 'R': /* recipient name */ 1176 if (Verbose) 1177 printf("\n\t\t\t\t\t %.38s", &buf[1]); 1178 else 1179 printf("\n\t\t\t\t %.45s", &buf[1]); 1180 break; 1181 1182 case 'T': /* creation time */ 1183 submittime = atol(&buf[1]); 1184 break; 1185 1186 case 'D': /* data file name */ 1187 if (stat(&buf[1], &st) >= 0) 1188 dfsize = st.st_size; 1189 break; 1190 1191 case 'F': /* flag bits */ 1192 for (p = &buf[1]; *p != '\0'; p++) 1193 { 1194 switch (*p) 1195 { 1196 case 'w': 1197 flags |= EF_WARNING; 1198 break; 1199 } 1200 } 1201 } 1202 } 1203 if (submittime == (time_t) 0) 1204 printf(" (no control file)"); 1205 printf("\n"); 1206 (void) fclose(f); 1207 } 1208 } 1209 1210 # endif /* QUEUE */ 1211 /* 1212 ** QUEUENAME -- build a file name in the queue directory for this envelope. 1213 ** 1214 ** Assigns an id code if one does not already exist. 1215 ** This code is very careful to avoid trashing existing files 1216 ** under any circumstances. 1217 ** 1218 ** Parameters: 1219 ** e -- envelope to build it in/from. 1220 ** type -- the file type, used as the first character 1221 ** of the file name. 1222 ** 1223 ** Returns: 1224 ** a pointer to the new file name (in a static buffer). 1225 ** 1226 ** Side Effects: 1227 ** If no id code is already assigned, queuename will 1228 ** assign an id code, create a qf file, and leave a 1229 ** locked, open-for-write file pointer in the envelope. 1230 */ 1231 1232 char * 1233 queuename(e, type) 1234 register ENVELOPE *e; 1235 char type; 1236 { 1237 static int pid = -1; 1238 static char c0; 1239 static char c1; 1240 static char c2; 1241 time_t now; 1242 struct tm *tm; 1243 static char buf[MAXNAME]; 1244 extern bool lockfile(); 1245 1246 if (e->e_id == NULL) 1247 { 1248 char qf[20]; 1249 1250 /* find a unique id */ 1251 if (pid != getpid()) 1252 { 1253 /* new process -- start back at "AA" */ 1254 pid = getpid(); 1255 now = curtime(); 1256 tm = localtime(&now); 1257 c0 = 'A' + tm->tm_hour; 1258 c1 = 'A'; 1259 c2 = 'A' - 1; 1260 } 1261 (void) sprintf(qf, "qf%cAA%05d", c0, pid); 1262 1263 while (c1 < '~' || c2 < 'Z') 1264 { 1265 int i; 1266 1267 if (c2 >= 'Z') 1268 { 1269 c1++; 1270 c2 = 'A' - 1; 1271 } 1272 qf[3] = c1; 1273 qf[4] = ++c2; 1274 if (tTd(7, 20)) 1275 printf("queuename: trying \"%s\"\n", qf); 1276 1277 i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 1278 if (i < 0) 1279 { 1280 if (errno == EEXIST) 1281 continue; 1282 syserr("queuename: Cannot create \"%s\" in \"%s\"", 1283 qf, QueueDir); 1284 exit(EX_UNAVAILABLE); 1285 } 1286 if (lockfile(i, qf, LOCK_EX|LOCK_NB)) 1287 { 1288 e->e_lockfp = fdopen(i, "w"); 1289 break; 1290 } 1291 1292 /* a reader got the file; abandon it and try again */ 1293 (void) close(i); 1294 } 1295 if (c1 >= '~' && c2 >= 'Z') 1296 { 1297 syserr("queuename: Cannot create \"%s\" in \"%s\"", 1298 qf, QueueDir); 1299 exit(EX_OSERR); 1300 } 1301 e->e_id = newstr(&qf[2]); 1302 define('i', e->e_id, e); 1303 if (tTd(7, 1)) 1304 printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 1305 # ifdef LOG 1306 if (LogLevel > 93) 1307 syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 1308 # endif /* LOG */ 1309 } 1310 1311 if (type == '\0') 1312 return (NULL); 1313 (void) sprintf(buf, "%cf%s", type, e->e_id); 1314 if (tTd(7, 2)) 1315 printf("queuename: %s\n", buf); 1316 return (buf); 1317 } 1318 /* 1319 ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 1320 ** 1321 ** Parameters: 1322 ** e -- the envelope to unlock. 1323 ** 1324 ** Returns: 1325 ** none 1326 ** 1327 ** Side Effects: 1328 ** unlocks the queue for `e'. 1329 */ 1330 1331 unlockqueue(e) 1332 ENVELOPE *e; 1333 { 1334 if (tTd(51, 4)) 1335 printf("unlockqueue(%s)\n", e->e_id); 1336 1337 /* if there is a lock file in the envelope, close it */ 1338 if (e->e_lockfp != NULL) 1339 xfclose(e->e_lockfp, "unlockqueue", e->e_id); 1340 e->e_lockfp = NULL; 1341 1342 /* don't create a queue id if we don't already have one */ 1343 if (e->e_id == NULL) 1344 return; 1345 1346 /* remove the transcript */ 1347 # ifdef LOG 1348 if (LogLevel > 87) 1349 syslog(LOG_DEBUG, "%s: unlock", e->e_id); 1350 # endif /* LOG */ 1351 if (!tTd(51, 104)) 1352 xunlink(queuename(e, 'x')); 1353 1354 } 1355 /* 1356 ** SETCTLUSER -- create a controlling address 1357 ** 1358 ** Create a fake "address" given only a local login name; this is 1359 ** used as a "controlling user" for future recipient addresses. 1360 ** 1361 ** Parameters: 1362 ** user -- the user name of the controlling user. 1363 ** 1364 ** Returns: 1365 ** An address descriptor for the controlling user. 1366 ** 1367 ** Side Effects: 1368 ** none. 1369 */ 1370 1371 ADDRESS * 1372 setctluser(user) 1373 char *user; 1374 { 1375 register ADDRESS *a; 1376 struct passwd *pw; 1377 1378 /* 1379 ** See if this clears our concept of controlling user. 1380 */ 1381 1382 if (user == NULL || *user == '\0') 1383 user = DefUser; 1384 1385 /* 1386 ** Set up addr fields for controlling user. 1387 */ 1388 1389 a = (ADDRESS *) xalloc(sizeof *a); 1390 bzero((char *) a, sizeof *a); 1391 if ((pw = getpwnam(user)) != NULL) 1392 { 1393 a->q_home = newstr(pw->pw_dir); 1394 a->q_uid = pw->pw_uid; 1395 a->q_gid = pw->pw_gid; 1396 a->q_user = newstr(user); 1397 } 1398 else 1399 { 1400 a->q_uid = DefUid; 1401 a->q_gid = DefGid; 1402 a->q_user = newstr(DefUser); 1403 } 1404 1405 a->q_flags |= QGOODUID|QPRIMARY; /* flag as a "ctladdr" */ 1406 a->q_mailer = LocalMailer; 1407 return a; 1408 } 1409