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