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.8 (Berkeley) 01/28/93 (with queueing)"; 14 #else 15 static char sccsid[] = "@(#)queue.c 6.8 (Berkeley) 01/28/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 # include <fcntl.h> 25 # ifdef SYSTEM5 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 # 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 ForceMail = TRUE; 430 431 # ifdef LOG 432 if (LogLevel > 11) 433 syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d", 434 QueueDir, getpid(), forkflag); 435 # endif /* LOG */ 436 437 /* 438 ** Release any resources used by the daemon code. 439 */ 440 441 # ifdef DAEMON 442 clrdaemon(); 443 # endif /* DAEMON */ 444 445 /* 446 ** Create ourselves an envelope 447 */ 448 449 CurEnv = &QueueEnvelope; 450 e = newenvelope(&QueueEnvelope); 451 e->e_flags = BlankEnvelope.e_flags; 452 453 /* 454 ** Make sure the alias database is open. 455 */ 456 457 initaliases(AliasFile, FALSE, e); 458 459 /* 460 ** Start making passes through the queue. 461 ** First, read and sort the entire queue. 462 ** Then, process the work in that order. 463 ** But if you take too long, start over. 464 */ 465 466 /* order the existing work requests */ 467 (void) orderq(FALSE); 468 469 /* process them once at a time */ 470 while (WorkQ != NULL) 471 { 472 WORK *w = WorkQ; 473 474 WorkQ = WorkQ->w_next; 475 dowork(w, e); 476 free(w->w_name); 477 free((char *) w); 478 } 479 480 /* exit without the usual cleanup */ 481 e->e_id = NULL; 482 finis(); 483 } 484 /* 485 ** ORDERQ -- order the work queue. 486 ** 487 ** Parameters: 488 ** doall -- if set, include everything in the queue (even 489 ** the jobs that cannot be run because the load 490 ** average is too high). Otherwise, exclude those 491 ** jobs. 492 ** 493 ** Returns: 494 ** The number of request in the queue (not necessarily 495 ** the number of requests in WorkQ however). 496 ** 497 ** Side Effects: 498 ** Sets WorkQ to the queue of available work, in order. 499 */ 500 501 # define NEED_P 001 502 # define NEED_T 002 503 504 orderq(doall) 505 bool doall; 506 { 507 register struct direct *d; 508 register WORK *w; 509 DIR *f; 510 register int i; 511 WORK wlist[QUEUESIZE+1]; 512 int wn = -1; 513 extern workcmpf(); 514 515 /* clear out old WorkQ */ 516 for (w = WorkQ; w != NULL; ) 517 { 518 register WORK *nw = w->w_next; 519 520 WorkQ = nw; 521 free(w->w_name); 522 free((char *) w); 523 w = nw; 524 } 525 526 /* open the queue directory */ 527 f = opendir("."); 528 if (f == NULL) 529 { 530 syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 531 return (0); 532 } 533 534 /* 535 ** Read the work directory. 536 */ 537 538 while ((d = readdir(f)) != NULL) 539 { 540 FILE *cf; 541 char lbuf[MAXNAME]; 542 extern bool shouldqueue(); 543 544 /* is this an interesting entry? */ 545 if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 546 continue; 547 548 /* yes -- open control file (if not too many files) */ 549 if (++wn >= QUEUESIZE) 550 continue; 551 cf = fopen(d->d_name, "r"); 552 if (cf == NULL) 553 { 554 /* this may be some random person sending hir msgs */ 555 /* syserr("orderq: cannot open %s", cbuf); */ 556 if (tTd(41, 2)) 557 printf("orderq: cannot open %s (%d)\n", 558 d->d_name, errno); 559 errno = 0; 560 wn--; 561 continue; 562 } 563 w = &wlist[wn]; 564 w->w_name = newstr(d->d_name); 565 566 /* make sure jobs in creation don't clog queue */ 567 w->w_pri = 0x7fffffff; 568 w->w_ctime = 0; 569 570 /* extract useful information */ 571 i = NEED_P | NEED_T; 572 while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 573 { 574 extern long atol(); 575 576 switch (lbuf[0]) 577 { 578 case 'P': 579 w->w_pri = atol(&lbuf[1]); 580 i &= ~NEED_P; 581 break; 582 583 case 'T': 584 w->w_ctime = atol(&lbuf[1]); 585 i &= ~NEED_T; 586 break; 587 } 588 } 589 (void) fclose(cf); 590 591 if (!doall && shouldqueue(w->w_pri, w->w_ctime)) 592 { 593 /* don't even bother sorting this job in */ 594 wn--; 595 } 596 } 597 (void) closedir(f); 598 wn++; 599 600 /* 601 ** Sort the work directory. 602 */ 603 604 qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 605 606 /* 607 ** Convert the work list into canonical form. 608 ** Should be turning it into a list of envelopes here perhaps. 609 */ 610 611 WorkQ = NULL; 612 for (i = min(wn, QUEUESIZE); --i >= 0; ) 613 { 614 w = (WORK *) xalloc(sizeof *w); 615 w->w_name = wlist[i].w_name; 616 w->w_pri = wlist[i].w_pri; 617 w->w_ctime = wlist[i].w_ctime; 618 w->w_next = WorkQ; 619 WorkQ = w; 620 } 621 622 if (tTd(40, 1)) 623 { 624 for (w = WorkQ; w != NULL; w = w->w_next) 625 printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 626 } 627 628 return (wn); 629 } 630 /* 631 ** WORKCMPF -- compare function for ordering work. 632 ** 633 ** Parameters: 634 ** a -- the first argument. 635 ** b -- the second argument. 636 ** 637 ** Returns: 638 ** -1 if a < b 639 ** 0 if a == b 640 ** +1 if a > b 641 ** 642 ** Side Effects: 643 ** none. 644 */ 645 646 workcmpf(a, b) 647 register WORK *a; 648 register WORK *b; 649 { 650 long pa = a->w_pri; 651 long pb = b->w_pri; 652 653 if (pa == pb) 654 return (0); 655 else if (pa > pb) 656 return (1); 657 else 658 return (-1); 659 } 660 /* 661 ** DOWORK -- do a work request. 662 ** 663 ** Parameters: 664 ** w -- the work request to be satisfied. 665 ** 666 ** Returns: 667 ** none. 668 ** 669 ** Side Effects: 670 ** The work request is satisfied if possible. 671 */ 672 673 dowork(w, e) 674 register WORK *w; 675 register ENVELOPE *e; 676 { 677 register int i; 678 extern bool shouldqueue(); 679 extern bool readqf(); 680 681 if (tTd(40, 1)) 682 printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 683 684 /* 685 ** Ignore jobs that are too expensive for the moment. 686 */ 687 688 if (shouldqueue(w->w_pri, w->w_ctime)) 689 { 690 if (Verbose) 691 printf("\nSkipping %s\n", w->w_name + 2); 692 return; 693 } 694 695 /* 696 ** Fork for work. 697 */ 698 699 if (ForkQueueRuns) 700 { 701 i = fork(); 702 if (i < 0) 703 { 704 syserr("dowork: cannot fork"); 705 return; 706 } 707 } 708 else 709 { 710 i = 0; 711 } 712 713 if (i == 0) 714 { 715 /* 716 ** CHILD 717 ** Lock the control file to avoid duplicate deliveries. 718 ** Then run the file as though we had just read it. 719 ** We save an idea of the temporary name so we 720 ** can recover on interrupt. 721 */ 722 723 /* set basic modes, etc. */ 724 (void) alarm(0); 725 clearenvelope(e, FALSE); 726 QueueRun = TRUE; 727 ErrorMode = EM_MAIL; 728 e->e_id = &w->w_name[2]; 729 # ifdef LOG 730 if (LogLevel > 12) 731 syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id, 732 getpid()); 733 # endif /* LOG */ 734 735 /* don't use the headers from sendmail.cf... */ 736 e->e_header = NULL; 737 738 /* read the queue control file -- return if locked */ 739 if (!readqf(e)) 740 { 741 if (ForkQueueRuns) 742 exit(EX_OK); 743 else 744 return; 745 } 746 747 e->e_flags |= EF_INQUEUE; 748 eatheader(e, TRUE); 749 750 /* do the delivery */ 751 if (!bitset(EF_FATALERRS, e->e_flags)) 752 sendall(e, SM_DELIVER); 753 754 /* finish up and exit */ 755 if (ForkQueueRuns) 756 finis(); 757 else 758 dropenvelope(e); 759 } 760 else 761 { 762 /* 763 ** Parent -- pick up results. 764 */ 765 766 errno = 0; 767 (void) waitfor(i); 768 } 769 } 770 /* 771 ** READQF -- read queue file and set up environment. 772 ** 773 ** Parameters: 774 ** e -- the envelope of the job to run. 775 ** 776 ** Returns: 777 ** TRUE if it successfully read the queue file. 778 ** FALSE otherwise. 779 ** 780 ** Side Effects: 781 ** The queue file is returned locked. 782 */ 783 784 bool 785 readqf(e) 786 register ENVELOPE *e; 787 { 788 char *qf; 789 register FILE *qfp; 790 ADDRESS *ctladdr; 791 struct stat st; 792 char *bp; 793 char buf[MAXLINE]; 794 extern char *fgetfolded(); 795 extern long atol(); 796 extern ADDRESS *setctluser(); 797 # ifdef LOCKF 798 struct flock lfd; 799 # endif 800 801 /* 802 ** Read and process the file. 803 */ 804 805 qf = queuename(e, 'q'); 806 qfp = fopen(qf, "r+"); 807 if (qfp == NULL) 808 { 809 if (errno != ENOENT) 810 syserr("readqf: no control file %s", qf); 811 return FALSE; 812 } 813 814 /* 815 ** Check the queue file for plausibility to avoid attacks. 816 */ 817 818 if (fstat(fileno(qfp), &st) < 0) 819 { 820 /* must have been being processed by someone else */ 821 fclose(qfp); 822 return FALSE; 823 } 824 825 if (st.st_uid != geteuid() || (st.st_mode & 07777) != FileMode) 826 { 827 # ifdef LOG 828 if (LogLevel > 0) 829 { 830 syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o", 831 e->e_id, st.st_uid, st.st_mode); 832 } 833 # endif /* LOG */ 834 fclose(qfp); 835 return FALSE; 836 } 837 838 # ifdef LOCKF 839 lfd.l_type = F_WRLCK; 840 lfd.l_whence = lfd.l_start = lfd.l_len = 0; 841 if (fcntl(fileno(qfp), F_SETLK, &lfd) < 0) 842 # else 843 if (flock(fileno(qfp), LOCK_EX|LOCK_NB) < 0) 844 # endif 845 { 846 /* being processed by another queuer */ 847 if (Verbose) 848 printf("%s: locked\n", e->e_id); 849 # ifdef LOG 850 if (LogLevel > 10) 851 syslog(LOG_DEBUG, "%s: locked", e->e_id); 852 # endif /* LOG */ 853 (void) fclose(qfp); 854 return FALSE; 855 } 856 857 /* save this lock */ 858 e->e_lockfp = qfp; 859 860 /* do basic system initialization */ 861 initsys(e); 862 863 FileName = qf; 864 LineNumber = 0; 865 if (Verbose) 866 printf("\nRunning %s\n", e->e_id); 867 ctladdr = NULL; 868 while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL) 869 { 870 struct stat st; 871 872 if (tTd(40, 4)) 873 printf("+++++ %s\n", bp); 874 switch (bp[0]) 875 { 876 case 'C': /* specify controlling user */ 877 ctladdr = setctluser(&bp[1]); 878 break; 879 880 case 'R': /* specify recipient */ 881 sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e); 882 break; 883 884 case 'E': /* specify error recipient */ 885 sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e); 886 break; 887 888 case 'H': /* header */ 889 (void) chompheader(&bp[1], FALSE, e); 890 break; 891 892 case 'M': /* message */ 893 e->e_message = newstr(&bp[1]); 894 break; 895 896 case 'S': /* sender */ 897 setsender(newstr(&bp[1]), e); 898 break; 899 900 case 'D': /* data file name */ 901 e->e_df = newstr(&bp[1]); 902 e->e_dfp = fopen(e->e_df, "r"); 903 if (e->e_dfp == NULL) 904 syserr("readqf: cannot open %s", e->e_df); 905 if (fstat(fileno(e->e_dfp), &st) >= 0) 906 e->e_msgsize = st.st_size; 907 break; 908 909 case 'T': /* init time */ 910 e->e_ctime = atol(&bp[1]); 911 break; 912 913 case 'P': /* message priority */ 914 e->e_msgpriority = atol(&bp[1]) + WkTimeFact; 915 break; 916 917 case '$': /* define macro */ 918 define(bp[1], newstr(&bp[2]), e); 919 break; 920 921 case '\0': /* blank line; ignore */ 922 break; 923 924 default: 925 syserr("readqf(%s:%d): bad line \"%s\"", e->e_id, 926 LineNumber, bp); 927 break; 928 } 929 930 if (bp != buf) 931 free(bp); 932 } 933 934 FileName = NULL; 935 936 /* 937 ** If we haven't read any lines, this queue file is empty. 938 ** Arrange to remove it without referencing any null pointers. 939 */ 940 941 if (LineNumber == 0) 942 { 943 errno = 0; 944 e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 945 } 946 return TRUE; 947 } 948 /* 949 ** PRINTQUEUE -- print out a representation of the mail queue 950 ** 951 ** Parameters: 952 ** none. 953 ** 954 ** Returns: 955 ** none. 956 ** 957 ** Side Effects: 958 ** Prints a listing of the mail queue on the standard output. 959 */ 960 961 printqueue() 962 { 963 register WORK *w; 964 FILE *f; 965 int nrequests; 966 char buf[MAXLINE]; 967 968 /* 969 ** Read and order the queue. 970 */ 971 972 nrequests = orderq(TRUE); 973 974 /* 975 ** Print the work list that we have read. 976 */ 977 978 /* first see if there is anything */ 979 if (nrequests <= 0) 980 { 981 printf("Mail queue is empty\n"); 982 return; 983 } 984 985 CurrentLA = getla(); /* get load average */ 986 987 printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 988 if (nrequests > QUEUESIZE) 989 printf(", only %d printed", QUEUESIZE); 990 if (Verbose) 991 printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 992 else 993 printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 994 for (w = WorkQ; w != NULL; w = w->w_next) 995 { 996 struct stat st; 997 auto time_t submittime = 0; 998 long dfsize = -1; 999 char message[MAXLINE]; 1000 # ifdef LOCKF 1001 struct flock lfd; 1002 # endif 1003 extern bool shouldqueue(); 1004 1005 f = fopen(w->w_name, "r"); 1006 if (f == NULL) 1007 { 1008 errno = 0; 1009 continue; 1010 } 1011 printf("%7s", w->w_name + 2); 1012 # ifdef LOCKF 1013 lfd.l_type = F_RDLCK; 1014 lfd.l_whence = lfd.l_start = lfd.l_len = 0; 1015 if (fcntl(fileno(f), F_GETLK, &lfd) < 0 || lfd.l_type != F_UNLCK) 1016 # else 1017 if (flock(fileno(f), LOCK_SH|LOCK_NB) < 0) 1018 # endif 1019 printf("*"); 1020 else if (shouldqueue(w->w_pri, w->w_ctime)) 1021 printf("X"); 1022 else 1023 printf(" "); 1024 errno = 0; 1025 1026 message[0] = '\0'; 1027 while (fgets(buf, sizeof buf, f) != NULL) 1028 { 1029 register int i; 1030 1031 fixcrlf(buf, TRUE); 1032 switch (buf[0]) 1033 { 1034 case 'M': /* error message */ 1035 if ((i = strlen(&buf[1])) >= sizeof message) 1036 i = sizeof message; 1037 bcopy(&buf[1], message, i); 1038 message[i] = '\0'; 1039 break; 1040 1041 case 'S': /* sender name */ 1042 if (Verbose) 1043 printf("%8ld %10ld %.12s %.38s", dfsize, 1044 w->w_pri, ctime(&submittime) + 4, 1045 &buf[1]); 1046 else 1047 printf("%8ld %.16s %.45s", dfsize, 1048 ctime(&submittime), &buf[1]); 1049 if (message[0] != '\0') 1050 printf("\n\t\t (%.60s)", message); 1051 break; 1052 1053 case 'C': /* controlling user */ 1054 if (Verbose) 1055 printf("\n\t\t\t\t (---%.34s---)", &buf[1]); 1056 break; 1057 1058 case 'R': /* recipient name */ 1059 if (Verbose) 1060 printf("\n\t\t\t\t\t %.38s", &buf[1]); 1061 else 1062 printf("\n\t\t\t\t %.45s", &buf[1]); 1063 break; 1064 1065 case 'T': /* creation time */ 1066 submittime = atol(&buf[1]); 1067 break; 1068 1069 case 'D': /* data file name */ 1070 if (stat(&buf[1], &st) >= 0) 1071 dfsize = st.st_size; 1072 break; 1073 } 1074 } 1075 if (submittime == (time_t) 0) 1076 printf(" (no control file)"); 1077 printf("\n"); 1078 (void) fclose(f); 1079 } 1080 } 1081 1082 # endif /* QUEUE */ 1083 /* 1084 ** QUEUENAME -- build a file name in the queue directory for this envelope. 1085 ** 1086 ** Assigns an id code if one does not already exist. 1087 ** This code is very careful to avoid trashing existing files 1088 ** under any circumstances. 1089 ** 1090 ** Parameters: 1091 ** e -- envelope to build it in/from. 1092 ** type -- the file type, used as the first character 1093 ** of the file name. 1094 ** 1095 ** Returns: 1096 ** a pointer to the new file name (in a static buffer). 1097 ** 1098 ** Side Effects: 1099 ** If no id code is already assigned, queuename will 1100 ** assign an id code, create a qf file, and leave a 1101 ** locked, open-for-write file pointer in the envelope. 1102 */ 1103 1104 char * 1105 queuename(e, type) 1106 register ENVELOPE *e; 1107 char type; 1108 { 1109 static char buf[MAXNAME]; 1110 static int pid = -1; 1111 char c1 = 'A'; 1112 char c2 = 'A'; 1113 1114 if (e->e_id == NULL) 1115 { 1116 char qf[20]; 1117 1118 /* find a unique id */ 1119 if (pid != getpid()) 1120 { 1121 /* new process -- start back at "AA" */ 1122 pid = getpid(); 1123 c1 = 'A'; 1124 c2 = 'A' - 1; 1125 } 1126 (void) sprintf(qf, "qfAA%05d", pid); 1127 1128 while (c1 < '~' || c2 < 'Z') 1129 { 1130 int i; 1131 # ifdef LOCKF 1132 struct flock lfd; 1133 # endif 1134 1135 if (c2 >= 'Z') 1136 { 1137 c1++; 1138 c2 = 'A' - 1; 1139 } 1140 qf[2] = c1; 1141 qf[3] = ++c2; 1142 if (tTd(7, 20)) 1143 printf("queuename: trying \"%s\"\n", qf); 1144 1145 i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 1146 if (i < 0) 1147 { 1148 if (errno == EEXIST) 1149 continue; 1150 syserr("queuename: Cannot create \"%s\" in \"%s\"", 1151 qf, QueueDir); 1152 exit(EX_UNAVAILABLE); 1153 } 1154 # ifdef LOCKF 1155 lfd.l_type = F_WRLCK; 1156 lfd.l_whence = lfd.l_start = lfd.l_len = 0; 1157 if (fcntl(i, F_SETLK, &lfd) >= 0) 1158 # else 1159 if (flock(i, LOCK_EX|LOCK_NB) >= 0) 1160 # endif 1161 { 1162 e->e_lockfp = fdopen(i, "w"); 1163 break; 1164 } 1165 1166 /* a reader got the file; abandon it and try again */ 1167 (void) close(i); 1168 } 1169 if (c1 >= '~' && c2 >= 'Z') 1170 { 1171 syserr("queuename: Cannot create \"%s\" in \"%s\"", 1172 qf, QueueDir); 1173 exit(EX_OSERR); 1174 } 1175 e->e_id = newstr(&qf[2]); 1176 define('i', e->e_id, e); 1177 if (tTd(7, 1)) 1178 printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 1179 # ifdef LOG 1180 if (LogLevel > 16) 1181 syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 1182 # endif /* LOG */ 1183 } 1184 1185 if (type == '\0') 1186 return (NULL); 1187 (void) sprintf(buf, "%cf%s", type, e->e_id); 1188 if (tTd(7, 2)) 1189 printf("queuename: %s\n", buf); 1190 return (buf); 1191 } 1192 /* 1193 ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 1194 ** 1195 ** Parameters: 1196 ** e -- the envelope to unlock. 1197 ** 1198 ** Returns: 1199 ** none 1200 ** 1201 ** Side Effects: 1202 ** unlocks the queue for `e'. 1203 */ 1204 1205 unlockqueue(e) 1206 ENVELOPE *e; 1207 { 1208 /* if there is a lock file in the envelope, close it */ 1209 if (e->e_lockfp != NULL) 1210 fclose(e->e_lockfp); 1211 e->e_lockfp = NULL; 1212 1213 /* remove the transcript */ 1214 # ifdef LOG 1215 if (LogLevel > 19) 1216 syslog(LOG_DEBUG, "%s: unlock", e->e_id); 1217 # endif /* LOG */ 1218 if (!tTd(51, 4)) 1219 xunlink(queuename(e, 'x')); 1220 1221 } 1222 /* 1223 ** SETCTLUSER -- create a controlling address 1224 ** 1225 ** Create a fake "address" given only a local login name; this is 1226 ** used as a "controlling user" for future recipient addresses. 1227 ** 1228 ** Parameters: 1229 ** user -- the user name of the controlling user. 1230 ** 1231 ** Returns: 1232 ** An address descriptor for the controlling user. 1233 ** 1234 ** Side Effects: 1235 ** none. 1236 */ 1237 1238 ADDRESS * 1239 setctluser(user) 1240 char *user; 1241 { 1242 register ADDRESS *a; 1243 struct passwd *pw; 1244 1245 /* 1246 ** See if this clears our concept of controlling user. 1247 */ 1248 1249 if (user == NULL || *user == '\0') 1250 return NULL; 1251 1252 /* 1253 ** Set up addr fields for controlling user. 1254 */ 1255 1256 a = (ADDRESS *) xalloc(sizeof *a); 1257 bzero((char *) a, sizeof *a); 1258 if ((pw = getpwnam(user)) != NULL) 1259 { 1260 a->q_home = newstr(pw->pw_dir); 1261 a->q_uid = pw->pw_uid; 1262 a->q_gid = pw->pw_gid; 1263 a->q_user = newstr(user); 1264 } 1265 else 1266 { 1267 a->q_uid = DefUid; 1268 a->q_gid = DefGid; 1269 a->q_user = newstr(DefUser); 1270 } 1271 1272 a->q_flags |= QGOODUID; /* flag as a "ctladdr" */ 1273 a->q_mailer = LocalMailer; 1274 return a; 1275 } 1276