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