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 #ifndef lint 10 static char sccsid[] = "@(#)deliver.c 6.10 (Berkeley) 01/21/93"; 11 #endif /* not lint */ 12 13 #include "sendmail.h" 14 #include <sys/signal.h> 15 #include <sys/stat.h> 16 #include <netdb.h> 17 #include <fcntl.h> 18 #include <errno.h> 19 #ifdef NAMED_BIND 20 #include <arpa/nameser.h> 21 #include <resolv.h> 22 #endif 23 24 /* 25 ** DELIVER -- Deliver a message to a list of addresses. 26 ** 27 ** This routine delivers to everyone on the same host as the 28 ** user on the head of the list. It is clever about mailers 29 ** that don't handle multiple users. It is NOT guaranteed 30 ** that it will deliver to all these addresses however -- so 31 ** deliver should be called once for each address on the 32 ** list. 33 ** 34 ** Parameters: 35 ** e -- the envelope to deliver. 36 ** firstto -- head of the address list to deliver to. 37 ** 38 ** Returns: 39 ** zero -- successfully delivered. 40 ** else -- some failure, see ExitStat for more info. 41 ** 42 ** Side Effects: 43 ** The standard input is passed off to someone. 44 */ 45 46 deliver(e, firstto) 47 register ENVELOPE *e; 48 ADDRESS *firstto; 49 { 50 char *host; /* host being sent to */ 51 char *user; /* user being sent to */ 52 char **pvp; 53 register char **mvp; 54 register char *p; 55 register MAILER *m; /* mailer for this recipient */ 56 ADDRESS *ctladdr; 57 register MCI *mci; 58 register ADDRESS *to = firstto; 59 bool clever = FALSE; /* running user smtp to this mailer */ 60 ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 61 int rcode; /* response code */ 62 char *from; /* pointer to from person */ 63 char *firstsig; /* signature of firstto */ 64 char *pv[MAXPV+1]; 65 char tobuf[MAXLINE-50]; /* text line of to people */ 66 char buf[MAXNAME]; 67 char tfrombuf[MAXNAME]; /* translated from person */ 68 char rpathbuf[MAXNAME]; /* translated return path */ 69 extern int checkcompat(); 70 extern ADDRESS *getctladdr(); 71 extern char *remotename(); 72 extern MCI *openmailer(); 73 extern char *hostsignature(); 74 75 errno = 0; 76 if (bitset(QDONTSEND, to->q_flags)) 77 return (0); 78 79 #ifdef NAMED_BIND 80 /* unless interactive, try twice, over a minute */ 81 if (OpMode == MD_DAEMON || OpMode == MD_SMTP) { 82 _res.retrans = 30; 83 _res.retry = 2; 84 } 85 #endif 86 87 m = to->q_mailer; 88 host = to->q_host; 89 90 if (tTd(10, 1)) 91 printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 92 m->m_mno, host, to->q_user); 93 94 /* 95 ** If this mailer is expensive, and if we don't want to make 96 ** connections now, just mark these addresses and return. 97 ** This is useful if we want to batch connections to 98 ** reduce load. This will cause the messages to be 99 ** queued up, and a daemon will come along to send the 100 ** messages later. 101 ** This should be on a per-mailer basis. 102 */ 103 104 if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) && 105 !Verbose) 106 { 107 for (; to != NULL; to = to->q_next) 108 { 109 if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m) 110 continue; 111 to->q_flags |= QQUEUEUP|QDONTSEND; 112 e->e_to = to->q_paddr; 113 message(Arpa_Info, "queued"); 114 if (LogLevel > 4) 115 logdelivery("queued", e); 116 } 117 e->e_to = NULL; 118 return (0); 119 } 120 121 /* 122 ** Do initial argv setup. 123 ** Insert the mailer name. Notice that $x expansion is 124 ** NOT done on the mailer name. Then, if the mailer has 125 ** a picky -f flag, we insert it as appropriate. This 126 ** code does not check for 'pv' overflow; this places a 127 ** manifest lower limit of 4 for MAXPV. 128 ** The from address rewrite is expected to make 129 ** the address relative to the other end. 130 */ 131 132 /* rewrite from address, using rewriting rules */ 133 (void) strcpy(rpathbuf, remotename(e->e_returnpath, m, TRUE, TRUE, e)); 134 if (e->e_returnpath == e->e_sender) 135 { 136 from = rpathbuf; 137 } 138 else 139 { 140 (void) strcpy(tfrombuf, remotename(e->e_sender, m, TRUE, TRUE, e)); 141 from = tfrombuf; 142 } 143 144 define('f', e->e_returnpath, e); /* raw return path */ 145 define('<', rpathbuf, e); /* translated return path */ 146 define('g', from, e); /* translated sender */ 147 define('h', host, e); /* to host */ 148 Errors = 0; 149 pvp = pv; 150 *pvp++ = m->m_argv[0]; 151 152 /* insert -f or -r flag as appropriate */ 153 if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 154 { 155 if (bitnset(M_FOPT, m->m_flags)) 156 *pvp++ = "-f"; 157 else 158 *pvp++ = "-r"; 159 *pvp++ = newstr(rpathbuf); 160 } 161 162 /* 163 ** Append the other fixed parts of the argv. These run 164 ** up to the first entry containing "$u". There can only 165 ** be one of these, and there are only a few more slots 166 ** in the pv after it. 167 */ 168 169 for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 170 { 171 while ((p = strchr(p, '\001')) != NULL) 172 if (*++p == 'u') 173 break; 174 if (p != NULL) 175 break; 176 177 /* this entry is safe -- go ahead and process it */ 178 expand(*mvp, buf, &buf[sizeof buf - 1], e); 179 *pvp++ = newstr(buf); 180 if (pvp >= &pv[MAXPV - 3]) 181 { 182 syserr("Too many parameters to %s before $u", pv[0]); 183 return (-1); 184 } 185 } 186 187 /* 188 ** If we have no substitution for the user name in the argument 189 ** list, we know that we must supply the names otherwise -- and 190 ** SMTP is the answer!! 191 */ 192 193 if (*mvp == NULL) 194 { 195 /* running SMTP */ 196 # ifdef SMTP 197 clever = TRUE; 198 *pvp = NULL; 199 # else /* SMTP */ 200 /* oops! we don't implement SMTP */ 201 syserr("SMTP style mailer"); 202 return (EX_SOFTWARE); 203 # endif /* SMTP */ 204 } 205 206 /* 207 ** At this point *mvp points to the argument with $u. We 208 ** run through our address list and append all the addresses 209 ** we can. If we run out of space, do not fret! We can 210 ** always send another copy later. 211 */ 212 213 tobuf[0] = '\0'; 214 e->e_to = tobuf; 215 ctladdr = NULL; 216 firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e); 217 for (; to != NULL; to = to->q_next) 218 { 219 /* avoid sending multiple recipients to dumb mailers */ 220 if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 221 break; 222 223 /* if already sent or not for this host, don't send */ 224 if (bitset(QDONTSEND, to->q_flags) || 225 to->q_mailer != firstto->q_mailer || 226 strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0) 227 continue; 228 229 /* avoid overflowing tobuf */ 230 if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 231 break; 232 233 if (tTd(10, 1)) 234 { 235 printf("\nsend to "); 236 printaddr(to, FALSE); 237 } 238 239 /* compute effective uid/gid when sending */ 240 if (to->q_mailer == ProgMailer) 241 ctladdr = getctladdr(to); 242 243 user = to->q_user; 244 e->e_to = to->q_paddr; 245 to->q_flags |= QDONTSEND; 246 247 /* 248 ** Check to see that these people are allowed to 249 ** talk to each other. 250 */ 251 252 if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 253 { 254 NoReturn = TRUE; 255 usrerr("Message is too large; %ld bytes max", m->m_maxsize); 256 giveresponse(EX_UNAVAILABLE, m, e); 257 continue; 258 } 259 rcode = checkcompat(to, e); 260 if (rcode != EX_OK) 261 { 262 giveresponse(rcode, m, e); 263 continue; 264 } 265 266 /* 267 ** Strip quote bits from names if the mailer is dumb 268 ** about them. 269 */ 270 271 if (bitnset(M_STRIPQ, m->m_flags)) 272 { 273 stripquotes(user); 274 stripquotes(host); 275 } 276 277 /* hack attack -- delivermail compatibility */ 278 if (m == ProgMailer && *user == '|') 279 user++; 280 281 /* 282 ** If an error message has already been given, don't 283 ** bother to send to this address. 284 ** 285 ** >>>>>>>>>> This clause assumes that the local mailer 286 ** >> NOTE >> cannot do any further aliasing; that 287 ** >>>>>>>>>> function is subsumed by sendmail. 288 */ 289 290 if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 291 continue; 292 293 /* save statistics.... */ 294 markstats(e, to); 295 296 /* 297 ** See if this user name is "special". 298 ** If the user name has a slash in it, assume that this 299 ** is a file -- send it off without further ado. Note 300 ** that this type of addresses is not processed along 301 ** with the others, so we fudge on the To person. 302 */ 303 304 if (m == FileMailer) 305 { 306 rcode = mailfile(user, getctladdr(to), e); 307 giveresponse(rcode, m, e); 308 if (rcode == EX_OK) 309 to->q_flags |= QSENT; 310 continue; 311 } 312 313 /* 314 ** Address is verified -- add this user to mailer 315 ** argv, and add it to the print list of recipients. 316 */ 317 318 /* link together the chain of recipients */ 319 to->q_tchain = tochain; 320 tochain = to; 321 322 /* create list of users for error messages */ 323 (void) strcat(tobuf, ","); 324 (void) strcat(tobuf, to->q_paddr); 325 define('u', user, e); /* to user */ 326 define('z', to->q_home, e); /* user's home */ 327 328 /* 329 ** Expand out this user into argument list. 330 */ 331 332 if (!clever) 333 { 334 expand(*mvp, buf, &buf[sizeof buf - 1], e); 335 *pvp++ = newstr(buf); 336 if (pvp >= &pv[MAXPV - 2]) 337 { 338 /* allow some space for trailing parms */ 339 break; 340 } 341 } 342 } 343 344 /* see if any addresses still exist */ 345 if (tobuf[0] == '\0') 346 { 347 define('g', (char *) NULL, e); 348 define('<', (char *) NULL, e); 349 return (0); 350 } 351 352 /* print out messages as full list */ 353 e->e_to = tobuf + 1; 354 355 /* 356 ** Fill out any parameters after the $u parameter. 357 */ 358 359 while (!clever && *++mvp != NULL) 360 { 361 expand(*mvp, buf, &buf[sizeof buf - 1], e); 362 *pvp++ = newstr(buf); 363 if (pvp >= &pv[MAXPV]) 364 syserr("deliver: pv overflow after $u for %s", pv[0]); 365 } 366 *pvp++ = NULL; 367 368 /* 369 ** Call the mailer. 370 ** The argument vector gets built, pipes 371 ** are created as necessary, and we fork & exec as 372 ** appropriate. 373 ** If we are running SMTP, we just need to clean up. 374 */ 375 376 if (ctladdr == NULL) 377 ctladdr = &e->e_from; 378 #ifdef NAMED_BIND 379 if (ConfigLevel < 2) 380 _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 381 #endif 382 mci = openmailer(m, pv, ctladdr, clever, e); 383 if (mci == NULL) 384 { 385 /* catastrophic error */ 386 rcode = -1; 387 goto give_up; 388 } 389 else if (mci->mci_state != MCIS_OPEN) 390 { 391 /* couldn't open the mailer */ 392 rcode = mci->mci_exitstat; 393 errno = mci->mci_errno; 394 if (rcode == EX_OK) 395 { 396 /* shouldn't happen */ 397 rcode = EX_SOFTWARE; 398 } 399 } 400 else if (!clever) 401 { 402 /* 403 ** Format and send message. 404 */ 405 406 putfromline(mci->mci_out, m, e); 407 (*e->e_puthdr)(mci->mci_out, m, e); 408 putline("\n", mci->mci_out, m); 409 (*e->e_putbody)(mci->mci_out, m, e); 410 411 /* get the exit status */ 412 rcode = endmailer(mci, pv[0]); 413 } 414 else 415 #ifdef SMTP 416 { 417 /* 418 ** Send the MAIL FROM: protocol 419 */ 420 421 rcode = smtpmailfrom(m, mci, e); 422 if (rcode == EX_OK) 423 { 424 register char *t = tobuf; 425 register int i; 426 427 /* send the recipient list */ 428 tobuf[0] = '\0'; 429 for (to = tochain; to != NULL; to = to->q_tchain) 430 { 431 e->e_to = to->q_paddr; 432 if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 433 { 434 markfailure(e, to, i); 435 giveresponse(i, m, e); 436 } 437 else 438 { 439 *t++ = ','; 440 for (p = to->q_paddr; *p; *t++ = *p++) 441 continue; 442 } 443 } 444 445 /* now send the data */ 446 if (tobuf[0] == '\0') 447 { 448 e->e_to = NULL; 449 if (bitset(MCIF_CACHED, mci->mci_flags)) 450 smtprset(m, mci, e); 451 } 452 else 453 { 454 e->e_to = tobuf + 1; 455 rcode = smtpdata(m, mci, e); 456 } 457 458 /* now close the connection */ 459 if (!bitset(MCIF_CACHED, mci->mci_flags)) 460 smtpquit(m, mci, e); 461 } 462 } 463 #else /* not SMTP */ 464 { 465 syserr("deliver: need SMTP compiled to use clever mailer"); 466 rcode = -1; 467 goto give_up; 468 } 469 #endif /* SMTP */ 470 #ifdef NAMED_BIND 471 if (ConfigLevel < 2) 472 _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 473 #endif 474 475 /* arrange a return receipt if requested */ 476 if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags)) 477 { 478 e->e_flags |= EF_SENDRECEIPT; 479 /* do we want to send back more info? */ 480 } 481 482 /* 483 ** Do final status disposal. 484 ** We check for something in tobuf for the SMTP case. 485 ** If we got a temporary failure, arrange to queue the 486 ** addressees. 487 */ 488 489 give_up: 490 if (tobuf[0] != '\0') 491 giveresponse(rcode, m, e); 492 for (to = tochain; to != NULL; to = to->q_tchain) 493 { 494 if (rcode != EX_OK) 495 markfailure(e, to, rcode); 496 else 497 { 498 to->q_flags |= QSENT; 499 e->e_nsent++; 500 } 501 } 502 503 /* 504 ** Restore state and return. 505 */ 506 507 errno = 0; 508 define('g', (char *) NULL, e); 509 define('<', (char *) NULL, e); 510 return (rcode); 511 } 512 /* 513 ** MARKFAILURE -- mark a failure on a specific address. 514 ** 515 ** Parameters: 516 ** e -- the envelope we are sending. 517 ** q -- the address to mark. 518 ** rcode -- the code signifying the particular failure. 519 ** 520 ** Returns: 521 ** none. 522 ** 523 ** Side Effects: 524 ** marks the address (and possibly the envelope) with the 525 ** failure so that an error will be returned or 526 ** the message will be queued, as appropriate. 527 */ 528 529 markfailure(e, q, rcode) 530 register ENVELOPE *e; 531 register ADDRESS *q; 532 int rcode; 533 { 534 if (rcode == EX_OK) 535 return; 536 else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR) 537 q->q_flags |= QBADADDR; 538 else if (curtime() > e->e_ctime + TimeOut) 539 { 540 extern char *pintvl(); 541 char buf[MAXLINE]; 542 543 if (!bitset(EF_TIMEOUT, e->e_flags)) 544 { 545 (void) sprintf(buf, "Cannot send message for %s", 546 pintvl(TimeOut, FALSE)); 547 if (e->e_message != NULL) 548 free(e->e_message); 549 e->e_message = newstr(buf); 550 message(Arpa_Info, buf); 551 } 552 q->q_flags |= QBADADDR; 553 e->e_flags |= EF_TIMEOUT; 554 fprintf(e->e_xfp, "421 %s... Message timed out\n", q->q_paddr); 555 } 556 else 557 q->q_flags |= QQUEUEUP; 558 } 559 /* 560 ** DOFORK -- do a fork, retrying a couple of times on failure. 561 ** 562 ** This MUST be a macro, since after a vfork we are running 563 ** two processes on the same stack!!! 564 ** 565 ** Parameters: 566 ** none. 567 ** 568 ** Returns: 569 ** From a macro??? You've got to be kidding! 570 ** 571 ** Side Effects: 572 ** Modifies the ==> LOCAL <== variable 'pid', leaving: 573 ** pid of child in parent, zero in child. 574 ** -1 on unrecoverable error. 575 ** 576 ** Notes: 577 ** I'm awfully sorry this looks so awful. That's 578 ** vfork for you..... 579 */ 580 581 # define NFORKTRIES 5 582 583 # ifndef FORK 584 # define FORK fork 585 # endif 586 587 # define DOFORK(fORKfN) \ 588 {\ 589 register int i;\ 590 \ 591 for (i = NFORKTRIES; --i >= 0; )\ 592 {\ 593 pid = fORKfN();\ 594 if (pid >= 0)\ 595 break;\ 596 if (i > 0)\ 597 sleep((unsigned) NFORKTRIES - i);\ 598 }\ 599 } 600 /* 601 ** DOFORK -- simple fork interface to DOFORK. 602 ** 603 ** Parameters: 604 ** none. 605 ** 606 ** Returns: 607 ** pid of child in parent. 608 ** zero in child. 609 ** -1 on error. 610 ** 611 ** Side Effects: 612 ** returns twice, once in parent and once in child. 613 */ 614 615 dofork() 616 { 617 register int pid; 618 619 DOFORK(fork); 620 return (pid); 621 } 622 /* 623 ** ENDMAILER -- Wait for mailer to terminate. 624 ** 625 ** We should never get fatal errors (e.g., segmentation 626 ** violation), so we report those specially. For other 627 ** errors, we choose a status message (into statmsg), 628 ** and if it represents an error, we print it. 629 ** 630 ** Parameters: 631 ** pid -- pid of mailer. 632 ** name -- name of mailer (for error messages). 633 ** 634 ** Returns: 635 ** exit code of mailer. 636 ** 637 ** Side Effects: 638 ** none. 639 */ 640 641 endmailer(mci, name) 642 register MCI *mci; 643 char *name; 644 { 645 int st; 646 647 /* close any connections */ 648 if (mci->mci_in != NULL) 649 (void) fclose(mci->mci_in); 650 if (mci->mci_out != NULL) 651 (void) fclose(mci->mci_out); 652 mci->mci_in = mci->mci_out = NULL; 653 mci->mci_state = MCIS_CLOSED; 654 655 /* in the IPC case there is nothing to wait for */ 656 if (mci->mci_pid == 0) 657 return (EX_OK); 658 659 /* wait for the mailer process to die and collect status */ 660 st = waitfor(mci->mci_pid); 661 if (st == -1) 662 { 663 syserr("endmailer %s: wait", name); 664 return (EX_SOFTWARE); 665 } 666 667 /* see if it died a horrid death */ 668 if ((st & 0377) != 0) 669 { 670 syserr("mailer %s died with signal %o", name, st); 671 ExitStat = EX_TEMPFAIL; 672 return (EX_TEMPFAIL); 673 } 674 675 /* normal death -- return status */ 676 st = (st >> 8) & 0377; 677 return (st); 678 } 679 /* 680 ** OPENMAILER -- open connection to mailer. 681 ** 682 ** Parameters: 683 ** m -- mailer descriptor. 684 ** pvp -- parameter vector to pass to mailer. 685 ** ctladdr -- controlling address for user. 686 ** clever -- create a full duplex connection. 687 ** 688 ** Returns: 689 ** The mail connection info struct for this connection. 690 ** NULL on failure. 691 ** 692 ** Side Effects: 693 ** creates a mailer in a subprocess. 694 */ 695 696 MCI * 697 openmailer(m, pvp, ctladdr, clever, e) 698 MAILER *m; 699 char **pvp; 700 ADDRESS *ctladdr; 701 bool clever; 702 ENVELOPE *e; 703 { 704 int pid; 705 register MCI *mci; 706 int mpvect[2]; 707 int rpvect[2]; 708 extern FILE *fdopen(); 709 710 if (tTd(11, 1)) 711 { 712 printf("openmailer:"); 713 printav(pvp); 714 } 715 errno = 0; 716 717 CurHostName = m->m_mailer; 718 719 /* 720 ** Deal with the special case of mail handled through an IPC 721 ** connection. 722 ** In this case we don't actually fork. We must be 723 ** running SMTP for this to work. We will return a 724 ** zero pid to indicate that we are running IPC. 725 ** We also handle a debug version that just talks to stdin/out. 726 */ 727 728 /* check for Local Person Communication -- not for mortals!!! */ 729 if (strcmp(m->m_mailer, "[LPC]") == 0) 730 { 731 mci = (MCI *) xalloc(sizeof *mci); 732 bzero((char *) mci, sizeof *mci); 733 mci->mci_in = stdin; 734 mci->mci_out = stdout; 735 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 736 mci->mci_mailer = m; 737 } 738 else if (strcmp(m->m_mailer, "[IPC]") == 0 || 739 strcmp(m->m_mailer, "[TCP]") == 0) 740 { 741 #ifdef DAEMON 742 register int i; 743 register u_short port; 744 char *curhost; 745 extern MCI *mci_get(); 746 extern char *hostsignature(); 747 748 CurHostName = pvp[1]; 749 curhost = hostsignature(m, pvp[1], e); 750 751 if (!clever) 752 syserr("non-clever IPC"); 753 if (pvp[2] != NULL) 754 port = atoi(pvp[2]); 755 else 756 port = 0; 757 while (*curhost != '\0') 758 { 759 register char *p; 760 char hostbuf[MAXNAME]; 761 762 /* pull the next host from the signature */ 763 p = strchr(curhost, ':'); 764 if (p == NULL) 765 p = &curhost[strlen(curhost)]; 766 strncpy(hostbuf, curhost, p - curhost); 767 hostbuf[p - curhost] = '\0'; 768 if (*p != '\0') 769 p++; 770 curhost = p; 771 772 /* see if we already know that this host is fried */ 773 CurHostName = hostbuf; 774 mci = mci_get(hostbuf, m); 775 if (mci->mci_state != MCIS_CLOSED) 776 { 777 if (tTd(11, 1)) 778 { 779 printf("openmailer: "); 780 mci_dump(mci); 781 } 782 return mci; 783 } 784 mci->mci_mailer = m; 785 if (mci->mci_exitstat != EX_OK) 786 continue; 787 788 /* try the connection */ 789 setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 790 message(Arpa_Info, "Connecting to %s (%s)...", 791 hostbuf, m->m_name); 792 i = makeconnection(hostbuf, port, mci, 793 bitnset(M_SECURE_PORT, m->m_flags)); 794 mci->mci_exitstat = i; 795 mci->mci_errno = errno; 796 if (i == EX_OK) 797 { 798 mci->mci_state = MCIS_OPENING; 799 mci_cache(mci); 800 break; 801 } 802 else if (tTd(11, 1)) 803 printf("openmailer: makeconnection => stat=%d, errno=%d\n", 804 i, errno); 805 806 807 /* enter status of this host */ 808 setstat(i); 809 } 810 mci->mci_pid = 0; 811 #else /* no DAEMON */ 812 syserr("openmailer: no IPC"); 813 if (tTd(11, 1)) 814 printf("openmailer: NULL\n"); 815 return NULL; 816 #endif /* DAEMON */ 817 } 818 else 819 { 820 /* create a pipe to shove the mail through */ 821 if (pipe(mpvect) < 0) 822 { 823 syserr("openmailer: pipe (to mailer)"); 824 if (tTd(11, 1)) 825 printf("openmailer: NULL\n"); 826 return NULL; 827 } 828 829 /* if this mailer speaks smtp, create a return pipe */ 830 if (clever && pipe(rpvect) < 0) 831 { 832 syserr("openmailer: pipe (from mailer)"); 833 (void) close(mpvect[0]); 834 (void) close(mpvect[1]); 835 if (tTd(11, 1)) 836 printf("openmailer: NULL\n"); 837 return NULL; 838 } 839 840 /* 841 ** Actually fork the mailer process. 842 ** DOFORK is clever about retrying. 843 ** 844 ** Dispose of SIGCHLD signal catchers that may be laying 845 ** around so that endmail will get it. 846 */ 847 848 if (e->e_xfp != NULL) 849 (void) fflush(e->e_xfp); /* for debugging */ 850 (void) fflush(stdout); 851 # ifdef SIGCHLD 852 (void) signal(SIGCHLD, SIG_DFL); 853 # endif /* SIGCHLD */ 854 DOFORK(FORK); 855 /* pid is set by DOFORK */ 856 if (pid < 0) 857 { 858 /* failure */ 859 syserr("openmailer: cannot fork"); 860 (void) close(mpvect[0]); 861 (void) close(mpvect[1]); 862 if (clever) 863 { 864 (void) close(rpvect[0]); 865 (void) close(rpvect[1]); 866 } 867 if (tTd(11, 1)) 868 printf("openmailer: NULL\n"); 869 return NULL; 870 } 871 else if (pid == 0) 872 { 873 int i; 874 int saveerrno; 875 char *env[2]; 876 extern int DtableSize; 877 878 /* child -- set up input & exec mailer */ 879 /* make diagnostic output be standard output */ 880 (void) signal(SIGINT, SIG_IGN); 881 (void) signal(SIGHUP, SIG_IGN); 882 (void) signal(SIGTERM, SIG_DFL); 883 884 /* arrange to filter std & diag output of command */ 885 if (clever) 886 { 887 (void) close(rpvect[0]); 888 (void) close(1); 889 (void) dup(rpvect[1]); 890 (void) close(rpvect[1]); 891 } 892 else if (OpMode == MD_SMTP || HoldErrs) 893 { 894 /* put mailer output in transcript */ 895 (void) close(1); 896 (void) dup(fileno(e->e_xfp)); 897 } 898 (void) close(2); 899 (void) dup(1); 900 901 /* arrange to get standard input */ 902 (void) close(mpvect[1]); 903 (void) close(0); 904 if (dup(mpvect[0]) < 0) 905 { 906 syserr("Cannot dup to zero!"); 907 _exit(EX_OSERR); 908 } 909 (void) close(mpvect[0]); 910 if (!bitnset(M_RESTR, m->m_flags)) 911 { 912 if (ctladdr == NULL || ctladdr->q_uid == 0) 913 { 914 (void) setgid(DefGid); 915 (void) initgroups(DefUser, DefGid); 916 (void) setuid(DefUid); 917 } 918 else 919 { 920 (void) setgid(ctladdr->q_gid); 921 (void) initgroups(ctladdr->q_ruser? 922 ctladdr->q_ruser: ctladdr->q_user, 923 ctladdr->q_gid); 924 (void) setuid(ctladdr->q_uid); 925 } 926 } 927 928 /* arrange for all the files to be closed */ 929 for (i = 3; i < DtableSize; i++) 930 { 931 register int j; 932 if ((j = fcntl(i, F_GETFD, 0)) != -1) 933 (void)fcntl(i, F_SETFD, j|1); 934 } 935 936 /* try to execute the mailer */ 937 env[0] = "AGENT=sendmail"; 938 env[1] = NULL; 939 execve(m->m_mailer, pvp, env); 940 saveerrno = errno; 941 syserr("Cannot exec %s", m->m_mailer); 942 if (m == LocalMailer) 943 _exit(EX_TEMPFAIL); 944 switch (saveerrno) 945 { 946 case EIO: 947 case EAGAIN: 948 case ENOMEM: 949 # ifdef EPROCLIM 950 case EPROCLIM: 951 # endif 952 _exit(EX_TEMPFAIL); 953 } 954 _exit(EX_UNAVAILABLE); 955 } 956 957 /* 958 ** Set up return value. 959 */ 960 961 mci = (MCI *) xalloc(sizeof *mci); 962 bzero((char *) mci, sizeof *mci); 963 mci->mci_mailer = m; 964 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 965 mci->mci_pid = pid; 966 (void) close(mpvect[0]); 967 mci->mci_out = fdopen(mpvect[1], "w"); 968 if (clever) 969 { 970 (void) close(rpvect[1]); 971 mci->mci_in = fdopen(rpvect[0], "r"); 972 } 973 else 974 { 975 mci->mci_flags |= MCIF_TEMP; 976 mci->mci_in = NULL; 977 } 978 } 979 980 /* 981 ** If we are in SMTP opening state, send initial protocol. 982 */ 983 984 if (clever && mci->mci_state != MCIS_CLOSED) 985 { 986 smtpinit(m, mci, e); 987 } 988 if (tTd(11, 1)) 989 { 990 printf("openmailer: "); 991 mci_dump(mci); 992 } 993 994 return mci; 995 } 996 /* 997 ** GIVERESPONSE -- Interpret an error response from a mailer 998 ** 999 ** Parameters: 1000 ** stat -- the status code from the mailer (high byte 1001 ** only; core dumps must have been taken care of 1002 ** already). 1003 ** m -- the mailer descriptor for this mailer. 1004 ** 1005 ** Returns: 1006 ** none. 1007 ** 1008 ** Side Effects: 1009 ** Errors may be incremented. 1010 ** ExitStat may be set. 1011 */ 1012 1013 giveresponse(stat, m, e) 1014 int stat; 1015 register MAILER *m; 1016 ENVELOPE *e; 1017 { 1018 register char *statmsg; 1019 extern char *SysExMsg[]; 1020 register int i; 1021 extern int N_SysEx; 1022 #ifdef NAMED_BIND 1023 extern int h_errno; 1024 #endif 1025 char buf[MAXLINE]; 1026 1027 #ifdef lint 1028 if (m == NULL) 1029 return; 1030 #endif lint 1031 1032 /* 1033 ** Compute status message from code. 1034 */ 1035 1036 i = stat - EX__BASE; 1037 if (stat == 0) 1038 statmsg = "250 Sent"; 1039 else if (i < 0 || i > N_SysEx) 1040 { 1041 (void) sprintf(buf, "554 unknown mailer error %d", stat); 1042 stat = EX_UNAVAILABLE; 1043 statmsg = buf; 1044 } 1045 else if (stat == EX_TEMPFAIL) 1046 { 1047 (void) strcpy(buf, SysExMsg[i]); 1048 #ifdef NAMED_BIND 1049 if (h_errno == TRY_AGAIN) 1050 { 1051 extern char *errstring(); 1052 1053 statmsg = errstring(h_errno+MAX_ERRNO); 1054 } 1055 else 1056 #endif 1057 { 1058 if (errno != 0) 1059 { 1060 extern char *errstring(); 1061 1062 statmsg = errstring(errno); 1063 } 1064 else 1065 { 1066 #ifdef SMTP 1067 extern char SmtpError[]; 1068 1069 statmsg = SmtpError; 1070 #else /* SMTP */ 1071 statmsg = NULL; 1072 #endif /* SMTP */ 1073 } 1074 } 1075 if (statmsg != NULL && statmsg[0] != '\0') 1076 { 1077 (void) strcat(buf, ": "); 1078 (void) strcat(buf, statmsg); 1079 } 1080 statmsg = buf; 1081 } 1082 else 1083 { 1084 statmsg = SysExMsg[i]; 1085 } 1086 1087 /* 1088 ** Print the message as appropriate 1089 */ 1090 1091 if (stat == EX_OK || stat == EX_TEMPFAIL) 1092 message(Arpa_Info, &statmsg[4]); 1093 else 1094 { 1095 Errors++; 1096 usrerr(statmsg); 1097 } 1098 1099 /* 1100 ** Final cleanup. 1101 ** Log a record of the transaction. Compute the new 1102 ** ExitStat -- if we already had an error, stick with 1103 ** that. 1104 */ 1105 1106 if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2)) 1107 logdelivery(&statmsg[4], e); 1108 1109 if (stat != EX_TEMPFAIL) 1110 setstat(stat); 1111 if (stat != EX_OK) 1112 { 1113 if (e->e_message != NULL) 1114 free(e->e_message); 1115 e->e_message = newstr(&statmsg[4]); 1116 } 1117 errno = 0; 1118 #ifdef NAMED_BIND 1119 h_errno = 0; 1120 #endif 1121 } 1122 /* 1123 ** LOGDELIVERY -- log the delivery in the system log 1124 ** 1125 ** Parameters: 1126 ** stat -- the message to print for the status 1127 ** 1128 ** Returns: 1129 ** none 1130 ** 1131 ** Side Effects: 1132 ** none 1133 */ 1134 1135 logdelivery(stat, e) 1136 char *stat; 1137 register ENVELOPE *e; 1138 { 1139 extern char *pintvl(); 1140 1141 # ifdef LOG 1142 syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", e->e_id, 1143 e->e_to, pintvl(curtime() - e->e_ctime, TRUE), stat); 1144 # endif /* LOG */ 1145 } 1146 /* 1147 ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 1148 ** 1149 ** This can be made an arbitrary message separator by changing $l 1150 ** 1151 ** One of the ugliest hacks seen by human eyes is contained herein: 1152 ** UUCP wants those stupid "remote from <host>" lines. Why oh why 1153 ** does a well-meaning programmer such as myself have to deal with 1154 ** this kind of antique garbage???? 1155 ** 1156 ** Parameters: 1157 ** fp -- the file to output to. 1158 ** m -- the mailer describing this entry. 1159 ** 1160 ** Returns: 1161 ** none 1162 ** 1163 ** Side Effects: 1164 ** outputs some text to fp. 1165 */ 1166 1167 putfromline(fp, m, e) 1168 register FILE *fp; 1169 register MAILER *m; 1170 ENVELOPE *e; 1171 { 1172 char *template = "\001l\n"; 1173 char buf[MAXLINE]; 1174 1175 if (bitnset(M_NHDR, m->m_flags)) 1176 return; 1177 1178 # ifdef UGLYUUCP 1179 if (bitnset(M_UGLYUUCP, m->m_flags)) 1180 { 1181 char *bang; 1182 char xbuf[MAXLINE]; 1183 1184 expand("\001<", buf, &buf[sizeof buf - 1], e); 1185 bang = strchr(buf, '!'); 1186 if (bang == NULL) 1187 syserr("No ! in UUCP! (%s)", buf); 1188 else 1189 { 1190 *bang++ = '\0'; 1191 (void) sprintf(xbuf, "From %s \001d remote from %s\n", bang, buf); 1192 template = xbuf; 1193 } 1194 } 1195 # endif /* UGLYUUCP */ 1196 expand(template, buf, &buf[sizeof buf - 1], e); 1197 putline(buf, fp, m); 1198 } 1199 /* 1200 ** PUTBODY -- put the body of a message. 1201 ** 1202 ** Parameters: 1203 ** fp -- file to output onto. 1204 ** m -- a mailer descriptor to control output format. 1205 ** e -- the envelope to put out. 1206 ** 1207 ** Returns: 1208 ** none. 1209 ** 1210 ** Side Effects: 1211 ** The message is written onto fp. 1212 */ 1213 1214 putbody(fp, m, e) 1215 FILE *fp; 1216 MAILER *m; 1217 register ENVELOPE *e; 1218 { 1219 char buf[MAXLINE]; 1220 1221 /* 1222 ** Output the body of the message 1223 */ 1224 1225 if (e->e_dfp == NULL) 1226 { 1227 if (e->e_df != NULL) 1228 { 1229 e->e_dfp = fopen(e->e_df, "r"); 1230 if (e->e_dfp == NULL) 1231 syserr("putbody: Cannot open %s for %s from %s", 1232 e->e_df, e->e_to, e->e_from); 1233 } 1234 else 1235 putline("<<< No Message Collected >>>", fp, m); 1236 } 1237 if (e->e_dfp != NULL) 1238 { 1239 rewind(e->e_dfp); 1240 while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 1241 { 1242 if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1243 strncmp(buf, "From ", 5) == 0) 1244 (void) putc('>', fp); 1245 putline(buf, fp, m); 1246 } 1247 1248 if (ferror(e->e_dfp)) 1249 { 1250 syserr("putbody: read error"); 1251 ExitStat = EX_IOERR; 1252 } 1253 } 1254 1255 (void) fflush(fp); 1256 if (ferror(fp) && errno != EPIPE) 1257 { 1258 syserr("putbody: write error"); 1259 ExitStat = EX_IOERR; 1260 } 1261 errno = 0; 1262 } 1263 /* 1264 ** MAILFILE -- Send a message to a file. 1265 ** 1266 ** If the file has the setuid/setgid bits set, but NO execute 1267 ** bits, sendmail will try to become the owner of that file 1268 ** rather than the real user. Obviously, this only works if 1269 ** sendmail runs as root. 1270 ** 1271 ** This could be done as a subordinate mailer, except that it 1272 ** is used implicitly to save messages in ~/dead.letter. We 1273 ** view this as being sufficiently important as to include it 1274 ** here. For example, if the system is dying, we shouldn't have 1275 ** to create another process plus some pipes to save the message. 1276 ** 1277 ** Parameters: 1278 ** filename -- the name of the file to send to. 1279 ** ctladdr -- the controlling address header -- includes 1280 ** the userid/groupid to be when sending. 1281 ** 1282 ** Returns: 1283 ** The exit code associated with the operation. 1284 ** 1285 ** Side Effects: 1286 ** none. 1287 */ 1288 1289 mailfile(filename, ctladdr, e) 1290 char *filename; 1291 ADDRESS *ctladdr; 1292 register ENVELOPE *e; 1293 { 1294 register FILE *f; 1295 register int pid; 1296 int mode; 1297 1298 /* 1299 ** Fork so we can change permissions here. 1300 ** Note that we MUST use fork, not vfork, because of 1301 ** the complications of calling subroutines, etc. 1302 */ 1303 1304 DOFORK(fork); 1305 1306 if (pid < 0) 1307 return (EX_OSERR); 1308 else if (pid == 0) 1309 { 1310 /* child -- actually write to file */ 1311 struct stat stb; 1312 1313 (void) signal(SIGINT, SIG_DFL); 1314 (void) signal(SIGHUP, SIG_DFL); 1315 (void) signal(SIGTERM, SIG_DFL); 1316 (void) umask(OldUmask); 1317 1318 if (stat(filename, &stb) < 0) 1319 stb.st_mode = 0666; 1320 mode = stb.st_mode; 1321 1322 /* limit the errors to those actually caused in the child */ 1323 errno = 0; 1324 ExitStat = EX_OK; 1325 1326 if (bitset(0111, stb.st_mode)) 1327 exit(EX_CANTCREAT); 1328 if (ctladdr == NULL) 1329 ctladdr = &e->e_from; 1330 else 1331 { 1332 /* ignore setuid and setgid bits */ 1333 mode &= ~(S_ISGID|S_ISUID); 1334 } 1335 1336 /* we have to open the dfile BEFORE setuid */ 1337 if (e->e_dfp == NULL && e->e_df != NULL) 1338 { 1339 e->e_dfp = fopen(e->e_df, "r"); 1340 if (e->e_dfp == NULL) 1341 { 1342 syserr("mailfile: Cannot open %s for %s from %s", 1343 e->e_df, e->e_to, e->e_from); 1344 } 1345 } 1346 1347 if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 1348 { 1349 if (ctladdr->q_uid == 0) 1350 { 1351 (void) setgid(DefGid); 1352 (void) initgroups(DefUser, DefGid); 1353 } 1354 else 1355 { 1356 (void) setgid(ctladdr->q_gid); 1357 (void) initgroups(ctladdr->q_ruser ? 1358 ctladdr->q_ruser : ctladdr->q_user, 1359 ctladdr->q_gid); 1360 } 1361 } 1362 if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 1363 { 1364 if (ctladdr->q_uid == 0) 1365 (void) setuid(DefUid); 1366 else 1367 (void) setuid(ctladdr->q_uid); 1368 } 1369 FileName = filename; 1370 LineNumber = 0; 1371 f = dfopen(filename, "a"); 1372 if (f == NULL) 1373 { 1374 message("cannot open"); 1375 exit(EX_CANTCREAT); 1376 } 1377 1378 putfromline(f, ProgMailer, e); 1379 (*e->e_puthdr)(f, ProgMailer, e); 1380 putline("\n", f, ProgMailer); 1381 (*e->e_putbody)(f, ProgMailer, e); 1382 putline("\n", f, ProgMailer); 1383 if (ferror(f)) 1384 { 1385 message("I/O error"); 1386 setstat(EX_IOERR); 1387 } 1388 (void) fclose(f); 1389 (void) fflush(stdout); 1390 1391 /* reset ISUID & ISGID bits for paranoid systems */ 1392 (void) chmod(filename, (int) stb.st_mode); 1393 exit(ExitStat); 1394 /*NOTREACHED*/ 1395 } 1396 else 1397 { 1398 /* parent -- wait for exit status */ 1399 int st; 1400 1401 st = waitfor(pid); 1402 if ((st & 0377) != 0) 1403 return (EX_UNAVAILABLE); 1404 else 1405 return ((st >> 8) & 0377); 1406 /*NOTREACHED*/ 1407 } 1408 } 1409 /* 1410 ** SENDALL -- actually send all the messages. 1411 ** 1412 ** Parameters: 1413 ** e -- the envelope to send. 1414 ** mode -- the delivery mode to use. If SM_DEFAULT, use 1415 ** the current SendMode. 1416 ** 1417 ** Returns: 1418 ** none. 1419 ** 1420 ** Side Effects: 1421 ** Scans the send lists and sends everything it finds. 1422 ** Delivers any appropriate error messages. 1423 ** If we are running in a non-interactive mode, takes the 1424 ** appropriate action. 1425 */ 1426 1427 sendall(e, mode) 1428 ENVELOPE *e; 1429 char mode; 1430 { 1431 register ADDRESS *q; 1432 bool oldverbose; 1433 int pid; 1434 # ifdef LOCKF 1435 struct flock lfd; 1436 # endif 1437 1438 /* determine actual delivery mode */ 1439 if (mode == SM_DEFAULT) 1440 { 1441 extern bool shouldqueue(); 1442 1443 if (shouldqueue(e->e_msgpriority, e->e_ctime)) 1444 mode = SM_QUEUE; 1445 else 1446 mode = SendMode; 1447 } 1448 1449 if (tTd(13, 1)) 1450 { 1451 printf("\nSENDALL: mode %c, sendqueue:\n", mode); 1452 printaddr(e->e_sendqueue, TRUE); 1453 } 1454 1455 /* 1456 ** Do any preprocessing necessary for the mode we are running. 1457 ** Check to make sure the hop count is reasonable. 1458 ** Delete sends to the sender in mailing lists. 1459 */ 1460 1461 CurEnv = e; 1462 1463 if (e->e_hopcount > MaxHopCount) 1464 { 1465 errno = 0; 1466 syserr("sendall: too many hops %d (%d max): from %s, to %s", 1467 e->e_hopcount, MaxHopCount, e->e_from.q_paddr, 1468 e->e_sendqueue->q_paddr); 1469 return; 1470 } 1471 1472 if (!MeToo) 1473 { 1474 extern ADDRESS *recipient(); 1475 1476 e->e_from.q_flags |= QDONTSEND; 1477 (void) recipient(&e->e_from, &e->e_sendqueue, e); 1478 } 1479 1480 # ifdef QUEUE 1481 if ((mode == SM_QUEUE || mode == SM_FORK || 1482 (mode != SM_VERIFY && SuperSafe)) && 1483 !bitset(EF_INQUEUE, e->e_flags)) 1484 queueup(e, TRUE, mode == SM_QUEUE); 1485 #endif /* QUEUE */ 1486 1487 oldverbose = Verbose; 1488 switch (mode) 1489 { 1490 case SM_VERIFY: 1491 Verbose = TRUE; 1492 break; 1493 1494 case SM_QUEUE: 1495 queueonly: 1496 e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 1497 return; 1498 1499 case SM_FORK: 1500 if (e->e_xfp != NULL) 1501 (void) fflush(e->e_xfp); 1502 1503 # ifdef LOCKF 1504 /* 1505 ** Since lockf has the interesting semantic that the 1506 ** lock is lost when we fork, we have to risk losing 1507 ** the lock here by closing before the fork, and then 1508 ** trying to get it back in the child. 1509 */ 1510 1511 if (e->e_lockfp != NULL) 1512 { 1513 (void) fclose(e->e_lockfp); 1514 e->e_lockfp = NULL; 1515 } 1516 # endif /* LOCKF */ 1517 1518 pid = fork(); 1519 if (pid < 0) 1520 { 1521 goto queueonly; 1522 } 1523 else if (pid > 0) 1524 { 1525 /* be sure we leave the temp files to our child */ 1526 e->e_id = e->e_df = NULL; 1527 # ifndef LOCKF 1528 if (e->e_lockfp != NULL) 1529 (void) fclose(e->e_lockfp); 1530 # endif 1531 return; 1532 } 1533 1534 /* double fork to avoid zombies */ 1535 if (fork() > 0) 1536 exit(EX_OK); 1537 1538 /* be sure we are immune from the terminal */ 1539 disconnect(FALSE); 1540 1541 # ifdef LOCKF 1542 /* 1543 ** Now try to get our lock back. 1544 */ 1545 1546 lfd.l_type = F_WRLCK; 1547 lfd.l_whence = lfd.l_start = lfd.l_len = 0; 1548 e->e_lockfp = fopen(queuename(e, 'q'), "r+"); 1549 if (e->e_lockfp == NULL || 1550 fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0) 1551 { 1552 /* oops.... lost it */ 1553 # ifdef LOG 1554 if (LogLevel > 5) 1555 syslog(LOG_NOTICE, "%s: lost lock: %m", 1556 e->e_id); 1557 # endif /* LOG */ 1558 exit(EX_OK); 1559 } 1560 # endif /* LOCKF */ 1561 1562 break; 1563 } 1564 1565 /* 1566 ** Run through the list and send everything. 1567 */ 1568 1569 e->e_nsent = 0; 1570 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1571 { 1572 if (mode == SM_VERIFY) 1573 { 1574 e->e_to = q->q_paddr; 1575 if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1576 message(Arpa_Info, "deliverable"); 1577 } 1578 else if (!bitset(QDONTSEND, q->q_flags)) 1579 { 1580 # ifdef QUEUE 1581 /* 1582 ** Checkpoint the send list every few addresses 1583 */ 1584 1585 if (e->e_nsent >= CheckpointInterval) 1586 { 1587 queueup(e, TRUE, FALSE); 1588 e->e_nsent = 0; 1589 } 1590 # endif /* QUEUE */ 1591 (void) deliver(e, q); 1592 } 1593 } 1594 Verbose = oldverbose; 1595 1596 /* 1597 ** Now run through and check for errors. 1598 */ 1599 1600 if (mode == SM_VERIFY) 1601 return; 1602 1603 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1604 { 1605 register ADDRESS *qq; 1606 1607 if (tTd(13, 3)) 1608 { 1609 printf("Checking "); 1610 printaddr(q, FALSE); 1611 } 1612 1613 /* only send errors if the message failed */ 1614 if (!bitset(QBADADDR, q->q_flags)) 1615 continue; 1616 1617 /* we have an address that failed -- find the parent */ 1618 for (qq = q; qq != NULL; qq = qq->q_alias) 1619 { 1620 char obuf[MAXNAME + 6]; 1621 extern char *aliaslookup(); 1622 1623 /* we can only have owners for local addresses */ 1624 if (!bitnset(M_LOCAL, qq->q_mailer->m_flags)) 1625 continue; 1626 1627 /* see if the owner list exists */ 1628 (void) strcpy(obuf, "owner-"); 1629 if (strncmp(qq->q_user, "owner-", 6) == 0) 1630 (void) strcat(obuf, "owner"); 1631 else 1632 (void) strcat(obuf, qq->q_user); 1633 if (!bitnset(M_USR_UPPER, qq->q_mailer->m_flags)) 1634 makelower(obuf); 1635 if (aliaslookup(obuf) == NULL) 1636 continue; 1637 1638 if (tTd(13, 4)) 1639 printf("Errors to %s\n", obuf); 1640 1641 /* owner list exists -- add it to the error queue */ 1642 sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue, e); 1643 1644 /* and set the return path to point to it */ 1645 e->e_returnpath = newstr(obuf); 1646 1647 ErrorMode = EM_MAIL; 1648 break; 1649 } 1650 1651 /* if we did not find an owner, send to the sender */ 1652 if (qq == NULL && bitset(QBADADDR, q->q_flags)) 1653 sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue, e); 1654 } 1655 1656 if (mode == SM_FORK) 1657 finis(); 1658 } 1659 /* 1660 ** HOSTSIGNATURE -- return the "signature" for a host. 1661 ** 1662 ** The signature describes how we are going to send this -- it 1663 ** can be just the hostname (for non-Internet hosts) or can be 1664 ** an ordered list of MX hosts. 1665 ** 1666 ** Parameters: 1667 ** m -- the mailer describing this host. 1668 ** host -- the host name. 1669 ** e -- the current envelope. 1670 ** 1671 ** Returns: 1672 ** The signature for this host. 1673 ** 1674 ** Side Effects: 1675 ** Can tweak the symbol table. 1676 */ 1677 1678 char * 1679 hostsignature(m, host, e) 1680 register MAILER *m; 1681 char *host; 1682 ENVELOPE *e; 1683 { 1684 register char *p; 1685 register STAB *s; 1686 int i; 1687 int len; 1688 #ifdef NAMED_BIND 1689 int nmx; 1690 auto int rcode; 1691 char *mxhosts[MAXMXHOSTS + 1]; 1692 static char myhostbuf[MAXNAME]; 1693 #endif 1694 1695 /* 1696 ** Check to see if this uses IPC -- if not, it can't have MX records. 1697 */ 1698 1699 p = m->m_mailer; 1700 if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 1701 { 1702 /* just an ordinary mailer */ 1703 return host; 1704 } 1705 1706 /* 1707 ** If it is a numeric address, just return it. 1708 */ 1709 1710 if (host[0] == '[') 1711 return host; 1712 1713 /* 1714 ** Look it up in the symbol table. 1715 */ 1716 1717 s = stab(host, ST_HOSTSIG, ST_ENTER); 1718 if (s->s_hostsig != NULL) 1719 return s->s_hostsig; 1720 1721 /* 1722 ** Not already there -- create a signature. 1723 */ 1724 1725 #ifdef NAMED_BIND 1726 if (myhostbuf[0] == '\0') 1727 expand("\001j", myhostbuf, &myhostbuf[sizeof myhostbuf - 1], e); 1728 1729 nmx = getmxrr(host, mxhosts, myhostbuf, &rcode); 1730 if (nmx <= 0) 1731 { 1732 register MCI *mci; 1733 extern int errno; 1734 extern MCI *mci_get(); 1735 1736 /* update the connection info for this host */ 1737 mci = mci_get(host, m); 1738 mci->mci_exitstat = rcode; 1739 mci->mci_errno = errno; 1740 1741 /* and return the original host name as the signature */ 1742 s->s_hostsig = host; 1743 return host; 1744 } 1745 1746 len = 0; 1747 for (i = 0; i < nmx; i++) 1748 { 1749 len += strlen(mxhosts[i]) + 1; 1750 } 1751 s->s_hostsig = p = xalloc(len); 1752 for (i = 0; i < nmx; i++) 1753 { 1754 if (i != 0) 1755 *p++ = ':'; 1756 strcpy(p, mxhosts[i]); 1757 p += strlen(p); 1758 } 1759 makelower(s->s_hostsig); 1760 #else 1761 /* not using BIND -- the signature is just the host name */ 1762 s->s_hostsig = host; 1763 #endif 1764 if (tTd(17, 1)) 1765 printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 1766 return s->s_hostsig; 1767 } 1768