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