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