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