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