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