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.4 (Berkeley) 07/16/93 (with queueing)"; 14 #else 15 static char sccsid[] = "@(#)queue.c 8.4 (Berkeley) 07/16/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 || 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 343 /* if a is an alias, use that for printing */ 344 if (a->q_alias != NULL) 345 a = a->q_alias; 346 347 /* check to see if this is the same as last time */ 348 if (lastctladdr != NULL && uid == lastuid && 349 strcmp(lastctladdr->q_paddr, a->q_paddr) == 0) 350 return; 351 lastuid = uid; 352 lastctladdr = a; 353 354 if (uid == 0 || (pw = getpwuid(uid)) == NULL) 355 uname = ""; 356 else 357 uname = pw->pw_name; 358 359 fprintf(tfp, "C%s:%s\n", uname, a->q_paddr); 360 } 361 362 /* 363 ** RUNQUEUE -- run the jobs in the queue. 364 ** 365 ** Gets the stuff out of the queue in some presumably logical 366 ** order and processes them. 367 ** 368 ** Parameters: 369 ** forkflag -- TRUE if the queue scanning should be done in 370 ** a child process. We double-fork so it is not our 371 ** child and we don't have to clean up after it. 372 ** 373 ** Returns: 374 ** none. 375 ** 376 ** Side Effects: 377 ** runs things in the mail queue. 378 */ 379 380 ENVELOPE QueueEnvelope; /* the queue run envelope */ 381 382 runqueue(forkflag) 383 bool forkflag; 384 { 385 register ENVELOPE *e; 386 extern ENVELOPE BlankEnvelope; 387 388 /* 389 ** If no work will ever be selected, don't even bother reading 390 ** the queue. 391 */ 392 393 CurrentLA = getla(); /* get load average */ 394 395 if (shouldqueue(0L, curtime())) 396 { 397 if (Verbose) 398 printf("Skipping queue run -- load average too high\n"); 399 if (forkflag && QueueIntvl != 0) 400 (void) setevent(QueueIntvl, runqueue, TRUE); 401 return; 402 } 403 404 /* 405 ** See if we want to go off and do other useful work. 406 */ 407 408 if (forkflag) 409 { 410 int pid; 411 412 pid = dofork(); 413 if (pid != 0) 414 { 415 extern void reapchild(); 416 417 /* parent -- pick up intermediate zombie */ 418 #ifndef SIGCHLD 419 (void) waitfor(pid); 420 #else /* SIGCHLD */ 421 (void) signal(SIGCHLD, reapchild); 422 #endif /* SIGCHLD */ 423 if (QueueIntvl != 0) 424 (void) setevent(QueueIntvl, runqueue, TRUE); 425 return; 426 } 427 /* child -- double fork */ 428 #ifndef SIGCHLD 429 if (fork() != 0) 430 exit(EX_OK); 431 #else /* SIGCHLD */ 432 (void) signal(SIGCHLD, SIG_DFL); 433 #endif /* SIGCHLD */ 434 } 435 436 setproctitle("running queue: %s", QueueDir); 437 438 # ifdef LOG 439 if (LogLevel > 69) 440 syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 441 QueueDir, getpid(), forkflag); 442 # endif /* LOG */ 443 444 /* 445 ** Release any resources used by the daemon code. 446 */ 447 448 # ifdef DAEMON 449 clrdaemon(); 450 # endif /* DAEMON */ 451 452 /* 453 ** Create ourselves an envelope 454 */ 455 456 CurEnv = &QueueEnvelope; 457 e = newenvelope(&QueueEnvelope, CurEnv); 458 e->e_flags = BlankEnvelope.e_flags; 459 460 /* 461 ** Make sure the alias database is open. 462 */ 463 464 initmaps(FALSE, e); 465 466 /* 467 ** Start making passes through the queue. 468 ** First, read and sort the entire queue. 469 ** Then, process the work in that order. 470 ** But if you take too long, start over. 471 */ 472 473 /* order the existing work requests */ 474 (void) orderq(FALSE); 475 476 /* process them once at a time */ 477 while (WorkQ != NULL) 478 { 479 WORK *w = WorkQ; 480 481 WorkQ = WorkQ->w_next; 482 483 /* 484 ** Ignore jobs that are too expensive for the moment. 485 */ 486 487 if (shouldqueue(w->w_pri, w->w_ctime)) 488 { 489 if (Verbose) 490 printf("\nSkipping %s\n", w->w_name + 2); 491 } 492 else 493 { 494 dowork(w->w_name + 2, ForkQueueRuns, FALSE, e); 495 } 496 free(w->w_name); 497 free((char *) w); 498 } 499 500 /* exit without the usual cleanup */ 501 e->e_id = NULL; 502 finis(); 503 } 504 /* 505 ** ORDERQ -- order the work queue. 506 ** 507 ** Parameters: 508 ** doall -- if set, include everything in the queue (even 509 ** the jobs that cannot be run because the load 510 ** average is too high). Otherwise, exclude those 511 ** jobs. 512 ** 513 ** Returns: 514 ** The number of request in the queue (not necessarily 515 ** the number of requests in WorkQ however). 516 ** 517 ** Side Effects: 518 ** Sets WorkQ to the queue of available work, in order. 519 */ 520 521 # define NEED_P 001 522 # define NEED_T 002 523 # define NEED_R 004 524 # define NEED_S 010 525 526 orderq(doall) 527 bool doall; 528 { 529 register struct dirent *d; 530 register WORK *w; 531 DIR *f; 532 register int i; 533 WORK wlist[QUEUESIZE+1]; 534 int wn = -1; 535 extern workcmpf(); 536 537 if (tTd(41, 1)) 538 { 539 printf("orderq:\n"); 540 if (QueueLimitId != NULL) 541 printf("\tQueueLimitId = %s\n", QueueLimitId); 542 if (QueueLimitSender != NULL) 543 printf("\tQueueLimitSender = %s\n", QueueLimitSender); 544 if (QueueLimitRecipient != NULL) 545 printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient); 546 } 547 548 /* clear out old WorkQ */ 549 for (w = WorkQ; w != NULL; ) 550 { 551 register WORK *nw = w->w_next; 552 553 WorkQ = nw; 554 free(w->w_name); 555 free((char *) w); 556 w = nw; 557 } 558 559 /* open the queue directory */ 560 f = opendir("."); 561 if (f == NULL) 562 { 563 syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 564 return (0); 565 } 566 567 /* 568 ** Read the work directory. 569 */ 570 571 while ((d = readdir(f)) != NULL) 572 { 573 FILE *cf; 574 char lbuf[MAXNAME]; 575 extern bool strcontainedin(); 576 577 /* is this an interesting entry? */ 578 if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 579 continue; 580 581 if (QueueLimitId != NULL && 582 !strcontainedin(QueueLimitId, d->d_name)) 583 continue; 584 585 /* 586 ** Check queue name for plausibility. This handles 587 ** both old and new type ids. 588 */ 589 590 i = strlen(d->d_name); 591 if (i != 9 && i != 10) 592 { 593 if (Verbose) 594 printf("orderq: bogus qf name %s\n", d->d_name); 595 #ifdef LOG 596 if (LogLevel > 3) 597 syslog(LOG_CRIT, "orderq: bogus qf name %s", 598 d->d_name); 599 #endif 600 if (strlen(d->d_name) >= MAXNAME) 601 d->d_name[MAXNAME - 1] = '\0'; 602 strcpy(lbuf, d->d_name); 603 lbuf[0] = 'Q'; 604 (void) rename(d->d_name, lbuf); 605 continue; 606 } 607 608 /* yes -- open control file (if not too many files) */ 609 if (++wn >= QUEUESIZE) 610 continue; 611 612 cf = fopen(d->d_name, "r"); 613 if (cf == NULL) 614 { 615 /* this may be some random person sending hir msgs */ 616 /* syserr("orderq: cannot open %s", cbuf); */ 617 if (tTd(41, 2)) 618 printf("orderq: cannot open %s (%d)\n", 619 d->d_name, errno); 620 errno = 0; 621 wn--; 622 continue; 623 } 624 w = &wlist[wn]; 625 w->w_name = newstr(d->d_name); 626 627 /* make sure jobs in creation don't clog queue */ 628 w->w_pri = 0x7fffffff; 629 w->w_ctime = 0; 630 631 /* extract useful information */ 632 i = NEED_P | NEED_T; 633 if (QueueLimitSender != NULL) 634 i |= NEED_S; 635 if (QueueLimitRecipient != NULL) 636 i |= NEED_R; 637 while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 638 { 639 extern long atol(); 640 extern bool strcontainedin(); 641 642 switch (lbuf[0]) 643 { 644 case 'P': 645 w->w_pri = atol(&lbuf[1]); 646 i &= ~NEED_P; 647 break; 648 649 case 'T': 650 w->w_ctime = atol(&lbuf[1]); 651 i &= ~NEED_T; 652 break; 653 654 case 'R': 655 if (QueueLimitRecipient != NULL && 656 strcontainedin(QueueLimitRecipient, &lbuf[1])) 657 i &= ~NEED_R; 658 break; 659 660 case 'S': 661 if (QueueLimitSender != NULL && 662 strcontainedin(QueueLimitSender, &lbuf[1])) 663 i &= ~NEED_S; 664 break; 665 } 666 } 667 (void) fclose(cf); 668 669 if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) || 670 bitset(NEED_R|NEED_S, i)) 671 { 672 /* don't even bother sorting this job in */ 673 wn--; 674 } 675 } 676 (void) closedir(f); 677 wn++; 678 679 /* 680 ** Sort the work directory. 681 */ 682 683 qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 684 685 /* 686 ** Convert the work list into canonical form. 687 ** Should be turning it into a list of envelopes here perhaps. 688 */ 689 690 WorkQ = NULL; 691 for (i = min(wn, QUEUESIZE); --i >= 0; ) 692 { 693 w = (WORK *) xalloc(sizeof *w); 694 w->w_name = wlist[i].w_name; 695 w->w_pri = wlist[i].w_pri; 696 w->w_ctime = wlist[i].w_ctime; 697 w->w_next = WorkQ; 698 WorkQ = w; 699 } 700 701 if (tTd(40, 1)) 702 { 703 for (w = WorkQ; w != NULL; w = w->w_next) 704 printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 705 } 706 707 return (wn); 708 } 709 /* 710 ** WORKCMPF -- compare function for ordering work. 711 ** 712 ** Parameters: 713 ** a -- the first argument. 714 ** b -- the second argument. 715 ** 716 ** Returns: 717 ** -1 if a < b 718 ** 0 if a == b 719 ** +1 if a > b 720 ** 721 ** Side Effects: 722 ** none. 723 */ 724 725 workcmpf(a, b) 726 register WORK *a; 727 register WORK *b; 728 { 729 long pa = a->w_pri; 730 long pb = b->w_pri; 731 732 if (pa == pb) 733 return (0); 734 else if (pa > pb) 735 return (1); 736 else 737 return (-1); 738 } 739 /* 740 ** DOWORK -- do a work request. 741 ** 742 ** Parameters: 743 ** id -- the ID of the job to run. 744 ** forkflag -- if set, run this in background. 745 ** requeueflag -- if set, reinstantiate the queue quickly. 746 ** This is used when expanding aliases in the queue. 747 ** If forkflag is also set, it doesn't wait for the 748 ** child. 749 ** e - the envelope in which to run it. 750 ** 751 ** Returns: 752 ** none. 753 ** 754 ** Side Effects: 755 ** The work request is satisfied if possible. 756 */ 757 758 dowork(id, forkflag, requeueflag, e) 759 char *id; 760 bool forkflag; 761 bool requeueflag; 762 register ENVELOPE *e; 763 { 764 register int i; 765 extern bool readqf(); 766 767 if (tTd(40, 1)) 768 printf("dowork(%s)\n", id); 769 770 /* 771 ** Fork for work. 772 */ 773 774 if (forkflag) 775 { 776 i = fork(); 777 if (i < 0) 778 { 779 syserr("dowork: cannot fork"); 780 return; 781 } 782 } 783 else 784 { 785 i = 0; 786 } 787 788 if (i == 0) 789 { 790 /* 791 ** CHILD 792 ** Lock the control file to avoid duplicate deliveries. 793 ** Then run the file as though we had just read it. 794 ** We save an idea of the temporary name so we 795 ** can recover on interrupt. 796 */ 797 798 /* set basic modes, etc. */ 799 (void) alarm(0); 800 clearenvelope(e, FALSE); 801 e->e_flags |= EF_QUEUERUN; 802 e->e_errormode = EM_MAIL; 803 e->e_id = id; 804 if (forkflag) 805 disconnect(0, e); 806 # ifdef LOG 807 if (LogLevel > 76) 808 syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 809 getpid()); 810 # endif /* LOG */ 811 812 /* don't use the headers from sendmail.cf... */ 813 e->e_header = NULL; 814 815 /* read the queue control file -- return if locked */ 816 if (!readqf(e)) 817 { 818 if (tTd(40, 4)) 819 printf("readqf(%s) failed\n", e->e_id); 820 if (forkflag) 821 exit(EX_OK); 822 else 823 return; 824 } 825 826 e->e_flags |= EF_INQUEUE; 827 eatheader(e, requeueflag); 828 829 if (requeueflag) 830 queueup(e, TRUE, FALSE); 831 832 /* do the delivery */ 833 sendall(e, SM_DELIVER); 834 835 /* finish up and exit */ 836 if (forkflag) 837 finis(); 838 else 839 dropenvelope(e); 840 } 841 else if (!requeueflag) 842 { 843 /* 844 ** Parent -- pick up results. 845 */ 846 847 errno = 0; 848 (void) waitfor(i); 849 } 850 } 851 /* 852 ** READQF -- read queue file and set up environment. 853 ** 854 ** Parameters: 855 ** e -- the envelope of the job to run. 856 ** 857 ** Returns: 858 ** TRUE if it successfully read the queue file. 859 ** FALSE otherwise. 860 ** 861 ** Side Effects: 862 ** The queue file is returned locked. 863 */ 864 865 bool 866 readqf(e) 867 register ENVELOPE *e; 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 FileName = qf; 954 LineNumber = 0; 955 if (Verbose) 956 printf("\nRunning %s\n", e->e_id); 957 ctladdr = NULL; 958 while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 959 { 960 register char *p; 961 struct stat st; 962 963 if (tTd(40, 4)) 964 printf("+++++ %s\n", bp); 965 switch (bp[0]) 966 { 967 case 'C': /* specify controlling user */ 968 ctladdr = setctluser(&bp[1]); 969 break; 970 971 case 'R': /* specify recipient */ 972 (void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 973 break; 974 975 case 'E': /* specify error recipient */ 976 (void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 977 break; 978 979 case 'H': /* header */ 980 (void) chompheader(&bp[1], FALSE, e); 981 break; 982 983 case 'M': /* message */ 984 e->e_message = newstr(&bp[1]); 985 break; 986 987 case 'S': /* sender */ 988 setsender(newstr(&bp[1]), e, NULL, TRUE); 989 break; 990 991 case 'B': /* body type */ 992 e->e_bodytype = newstr(&bp[1]); 993 break; 994 995 case 'D': /* data file name */ 996 e->e_df = newstr(&bp[1]); 997 e->e_dfp = fopen(e->e_df, "r"); 998 if (e->e_dfp == NULL) 999 { 1000 syserr("readqf: cannot open %s", e->e_df); 1001 e->e_msgsize = -1; 1002 } 1003 else if (fstat(fileno(e->e_dfp), &st) >= 0) 1004 e->e_msgsize = st.st_size; 1005 break; 1006 1007 case 'T': /* init time */ 1008 e->e_ctime = atol(&bp[1]); 1009 break; 1010 1011 case 'P': /* message priority */ 1012 e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 1013 break; 1014 1015 case 'F': /* flag bits */ 1016 for (p = &bp[1]; *p != '\0'; p++) 1017 { 1018 switch (*p) 1019 { 1020 case 'w': /* warning sent */ 1021 e->e_flags |= EF_WARNING; 1022 break; 1023 1024 case 'r': /* response */ 1025 e->e_flags |= EF_RESPONSE; 1026 break; 1027 } 1028 } 1029 break; 1030 1031 case '$': /* define macro */ 1032 define(bp[1], newstr(&bp[2]), e); 1033 break; 1034 1035 case '\0': /* blank line; ignore */ 1036 break; 1037 1038 default: 1039 syserr("readqf: bad line \"%s\"", e->e_id, 1040 LineNumber, bp); 1041 fclose(qfp); 1042 rename(qf, queuename(e, 'Q')); 1043 return FALSE; 1044 } 1045 1046 if (bp != buf) 1047 free(bp); 1048 } 1049 1050 FileName = NULL; 1051 1052 /* 1053 ** If we haven't read any lines, this queue file is empty. 1054 ** Arrange to remove it without referencing any null pointers. 1055 */ 1056 1057 if (LineNumber == 0) 1058 { 1059 errno = 0; 1060 e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 1061 } 1062 return TRUE; 1063 } 1064 /* 1065 ** PRINTQUEUE -- print out a representation of the mail queue 1066 ** 1067 ** Parameters: 1068 ** none. 1069 ** 1070 ** Returns: 1071 ** none. 1072 ** 1073 ** Side Effects: 1074 ** Prints a listing of the mail queue on the standard output. 1075 */ 1076 1077 printqueue() 1078 { 1079 register WORK *w; 1080 FILE *f; 1081 int nrequests; 1082 char buf[MAXLINE]; 1083 1084 /* 1085 ** Check for permission to print the queue 1086 */ 1087 1088 if (bitset(PRIV_RESTRMAILQ, PrivacyFlags) && RealUid != 0) 1089 { 1090 struct stat st; 1091 # ifdef NGROUPS 1092 int n; 1093 int gidset[NGROUPS]; 1094 # endif 1095 1096 if (stat(QueueDir, &st) < 0) 1097 { 1098 syserr("Cannot stat %s", QueueDir); 1099 return; 1100 } 1101 # ifdef NGROUPS 1102 n = getgroups(NGROUPS, gidset); 1103 while (--n >= 0) 1104 { 1105 if (gidset[n] == st.st_gid) 1106 break; 1107 } 1108 if (n < 0) 1109 # else 1110 if (RealGid != st.st_gid) 1111 # endif 1112 { 1113 usrerr("510 You are not permitted to see the queue"); 1114 setstat(EX_NOPERM); 1115 return; 1116 } 1117 } 1118 1119 /* 1120 ** Read and order the queue. 1121 */ 1122 1123 nrequests = orderq(TRUE); 1124 1125 /* 1126 ** Print the work list that we have read. 1127 */ 1128 1129 /* first see if there is anything */ 1130 if (nrequests <= 0) 1131 { 1132 printf("Mail queue is empty\n"); 1133 return; 1134 } 1135 1136 CurrentLA = getla(); /* get load average */ 1137 1138 printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 1139 if (nrequests > QUEUESIZE) 1140 printf(", only %d printed", QUEUESIZE); 1141 if (Verbose) 1142 printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 1143 else 1144 printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 1145 for (w = WorkQ; w != NULL; w = w->w_next) 1146 { 1147 struct stat st; 1148 auto time_t submittime = 0; 1149 long dfsize = -1; 1150 int flags = 0; 1151 char message[MAXLINE]; 1152 char bodytype[MAXNAME]; 1153 1154 f = fopen(w->w_name, "r"); 1155 if (f == NULL) 1156 { 1157 errno = 0; 1158 continue; 1159 } 1160 printf("%8s", w->w_name + 2); 1161 if (!lockfile(fileno(f), w->w_name, LOCK_SH|LOCK_NB)) 1162 printf("*"); 1163 else if (shouldqueue(w->w_pri, w->w_ctime)) 1164 printf("X"); 1165 else 1166 printf(" "); 1167 errno = 0; 1168 1169 message[0] = bodytype[0] = '\0'; 1170 while (fgets(buf, sizeof buf, f) != NULL) 1171 { 1172 register int i; 1173 register char *p; 1174 1175 fixcrlf(buf, TRUE); 1176 switch (buf[0]) 1177 { 1178 case 'M': /* error message */ 1179 if ((i = strlen(&buf[1])) >= sizeof message) 1180 i = sizeof message - 1; 1181 bcopy(&buf[1], message, i); 1182 message[i] = '\0'; 1183 break; 1184 1185 case 'B': /* body type */ 1186 if ((i = strlen(&buf[1])) >= sizeof bodytype) 1187 i = sizeof bodytype - 1; 1188 bcopy(&buf[1], bodytype, i); 1189 bodytype[i] = '\0'; 1190 break; 1191 1192 case 'S': /* sender name */ 1193 if (Verbose) 1194 printf("%8ld %10ld%c%.12s %.38s", 1195 dfsize, 1196 w->w_pri, 1197 bitset(EF_WARNING, flags) ? '+' : ' ', 1198 ctime(&submittime) + 4, 1199 &buf[1]); 1200 else 1201 printf("%8ld %.16s %.45s", dfsize, 1202 ctime(&submittime), &buf[1]); 1203 if (message[0] != '\0' || bodytype[0] != '\0') 1204 { 1205 printf("\n %10.10s", bodytype); 1206 if (message[0] != '\0') 1207 printf(" (%.60s)", message); 1208 } 1209 break; 1210 1211 case 'C': /* controlling user */ 1212 if (Verbose) 1213 printf("\n\t\t\t\t (---%.34s---)", 1214 &buf[1]); 1215 break; 1216 1217 case 'R': /* recipient name */ 1218 if (Verbose) 1219 printf("\n\t\t\t\t\t %.38s", &buf[1]); 1220 else 1221 printf("\n\t\t\t\t %.45s", &buf[1]); 1222 break; 1223 1224 case 'T': /* creation time */ 1225 submittime = atol(&buf[1]); 1226 break; 1227 1228 case 'D': /* data file name */ 1229 if (stat(&buf[1], &st) >= 0) 1230 dfsize = st.st_size; 1231 break; 1232 1233 case 'F': /* flag bits */ 1234 for (p = &buf[1]; *p != '\0'; p++) 1235 { 1236 switch (*p) 1237 { 1238 case 'w': 1239 flags |= EF_WARNING; 1240 break; 1241 } 1242 } 1243 } 1244 } 1245 if (submittime == (time_t) 0) 1246 printf(" (no control file)"); 1247 printf("\n"); 1248 (void) fclose(f); 1249 } 1250 } 1251 1252 # endif /* QUEUE */ 1253 /* 1254 ** QUEUENAME -- build a file name in the queue directory for this envelope. 1255 ** 1256 ** Assigns an id code if one does not already exist. 1257 ** This code is very careful to avoid trashing existing files 1258 ** under any circumstances. 1259 ** 1260 ** Parameters: 1261 ** e -- envelope to build it in/from. 1262 ** type -- the file type, used as the first character 1263 ** of the file name. 1264 ** 1265 ** Returns: 1266 ** a pointer to the new file name (in a static buffer). 1267 ** 1268 ** Side Effects: 1269 ** If no id code is already assigned, queuename will 1270 ** assign an id code, create a qf file, and leave a 1271 ** locked, open-for-write file pointer in the envelope. 1272 */ 1273 1274 char * 1275 queuename(e, type) 1276 register ENVELOPE *e; 1277 int type; 1278 { 1279 static int pid = -1; 1280 static char c0; 1281 static char c1; 1282 static char c2; 1283 time_t now; 1284 struct tm *tm; 1285 static char buf[MAXNAME]; 1286 1287 if (e->e_id == NULL) 1288 { 1289 char qf[20]; 1290 1291 /* find a unique id */ 1292 if (pid != getpid()) 1293 { 1294 /* new process -- start back at "AA" */ 1295 pid = getpid(); 1296 now = curtime(); 1297 tm = localtime(&now); 1298 c0 = 'A' + tm->tm_hour; 1299 c1 = 'A'; 1300 c2 = 'A' - 1; 1301 } 1302 (void) sprintf(qf, "qf%cAA%05d", c0, pid); 1303 1304 while (c1 < '~' || c2 < 'Z') 1305 { 1306 int i; 1307 1308 if (c2 >= 'Z') 1309 { 1310 c1++; 1311 c2 = 'A' - 1; 1312 } 1313 qf[3] = c1; 1314 qf[4] = ++c2; 1315 if (tTd(7, 20)) 1316 printf("queuename: trying \"%s\"\n", qf); 1317 1318 i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 1319 if (i < 0) 1320 { 1321 if (errno == EEXIST) 1322 continue; 1323 syserr("queuename: Cannot create \"%s\" in \"%s\"", 1324 qf, QueueDir); 1325 exit(EX_UNAVAILABLE); 1326 } 1327 if (lockfile(i, qf, LOCK_EX|LOCK_NB)) 1328 { 1329 e->e_lockfp = fdopen(i, "w"); 1330 break; 1331 } 1332 1333 /* a reader got the file; abandon it and try again */ 1334 (void) close(i); 1335 } 1336 if (c1 >= '~' && c2 >= 'Z') 1337 { 1338 syserr("queuename: Cannot create \"%s\" in \"%s\"", 1339 qf, QueueDir); 1340 exit(EX_OSERR); 1341 } 1342 e->e_id = newstr(&qf[2]); 1343 define('i', e->e_id, e); 1344 if (tTd(7, 1)) 1345 printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 1346 # ifdef LOG 1347 if (LogLevel > 93) 1348 syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 1349 # endif /* LOG */ 1350 } 1351 1352 if (type == '\0') 1353 return (NULL); 1354 (void) sprintf(buf, "%cf%s", type, e->e_id); 1355 if (tTd(7, 2)) 1356 printf("queuename: %s\n", buf); 1357 return (buf); 1358 } 1359 /* 1360 ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 1361 ** 1362 ** Parameters: 1363 ** e -- the envelope to unlock. 1364 ** 1365 ** Returns: 1366 ** none 1367 ** 1368 ** Side Effects: 1369 ** unlocks the queue for `e'. 1370 */ 1371 1372 unlockqueue(e) 1373 ENVELOPE *e; 1374 { 1375 if (tTd(51, 4)) 1376 printf("unlockqueue(%s)\n", e->e_id); 1377 1378 /* if there is a lock file in the envelope, close it */ 1379 if (e->e_lockfp != NULL) 1380 xfclose(e->e_lockfp, "unlockqueue", e->e_id); 1381 e->e_lockfp = NULL; 1382 1383 /* don't create a queue id if we don't already have one */ 1384 if (e->e_id == NULL) 1385 return; 1386 1387 /* remove the transcript */ 1388 # ifdef LOG 1389 if (LogLevel > 87) 1390 syslog(LOG_DEBUG, "%s: unlock", e->e_id); 1391 # endif /* LOG */ 1392 if (!tTd(51, 104)) 1393 xunlink(queuename(e, 'x')); 1394 1395 } 1396 /* 1397 ** SETCTLUSER -- create a controlling address 1398 ** 1399 ** Create a fake "address" given only a local login name; this is 1400 ** used as a "controlling user" for future recipient addresses. 1401 ** 1402 ** Parameters: 1403 ** user -- the user name of the controlling user. 1404 ** 1405 ** Returns: 1406 ** An address descriptor for the controlling user. 1407 ** 1408 ** Side Effects: 1409 ** none. 1410 */ 1411 1412 ADDRESS * 1413 setctluser(user) 1414 char *user; 1415 { 1416 register ADDRESS *a; 1417 struct passwd *pw; 1418 char *p; 1419 1420 /* 1421 ** See if this clears our concept of controlling user. 1422 */ 1423 1424 if (user == NULL) 1425 user = ""; 1426 1427 /* 1428 ** Set up addr fields for controlling user. 1429 */ 1430 1431 a = (ADDRESS *) xalloc(sizeof *a); 1432 bzero((char *) a, sizeof *a); 1433 1434 p = strchr(user, ':'); 1435 if (p != NULL) 1436 *p++ = '\0'; 1437 if (*user != '\0' && (pw = getpwnam(user)) != NULL) 1438 { 1439 a->q_home = newstr(pw->pw_dir); 1440 a->q_uid = pw->pw_uid; 1441 a->q_gid = pw->pw_gid; 1442 a->q_user = newstr(user); 1443 a->q_flags |= QGOODUID; 1444 } 1445 else 1446 { 1447 a->q_user = newstr(DefUser); 1448 } 1449 1450 a->q_flags |= QPRIMARY; /* flag as a "ctladdr" */ 1451 a->q_mailer = LocalMailer; 1452 if (p == NULL) 1453 a->q_paddr = a->q_user; 1454 else 1455 a->q_paddr = newstr(p); 1456 return a; 1457 } 1458