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