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