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