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.8 (Berkeley) 01/13/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 to->q_flags |= QSENT; 498 } 499 500 /* 501 ** Restore state and return. 502 */ 503 504 errno = 0; 505 define('g', (char *) NULL, e); 506 define('<', (char *) NULL, e); 507 return (rcode); 508 } 509 /* 510 ** MARKFAILURE -- mark a failure on a specific address. 511 ** 512 ** Parameters: 513 ** e -- the envelope we are sending. 514 ** q -- the address to mark. 515 ** rcode -- the code signifying the particular failure. 516 ** 517 ** Returns: 518 ** none. 519 ** 520 ** Side Effects: 521 ** marks the address (and possibly the envelope) with the 522 ** failure so that an error will be returned or 523 ** the message will be queued, as appropriate. 524 */ 525 526 markfailure(e, q, rcode) 527 register ENVELOPE *e; 528 register ADDRESS *q; 529 int rcode; 530 { 531 if (rcode == EX_OK) 532 return; 533 else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR) 534 q->q_flags |= QBADADDR; 535 else if (curtime() > e->e_ctime + TimeOut) 536 { 537 extern char *pintvl(); 538 char buf[MAXLINE]; 539 540 if (!bitset(EF_TIMEOUT, e->e_flags)) 541 { 542 (void) sprintf(buf, "Cannot send message for %s", 543 pintvl(TimeOut, FALSE)); 544 if (e->e_message != NULL) 545 free(e->e_message); 546 e->e_message = newstr(buf); 547 message(Arpa_Info, buf); 548 } 549 q->q_flags |= QBADADDR; 550 e->e_flags |= EF_TIMEOUT; 551 } 552 else 553 q->q_flags |= QQUEUEUP; 554 } 555 /* 556 ** DOFORK -- do a fork, retrying a couple of times on failure. 557 ** 558 ** This MUST be a macro, since after a vfork we are running 559 ** two processes on the same stack!!! 560 ** 561 ** Parameters: 562 ** none. 563 ** 564 ** Returns: 565 ** From a macro??? You've got to be kidding! 566 ** 567 ** Side Effects: 568 ** Modifies the ==> LOCAL <== variable 'pid', leaving: 569 ** pid of child in parent, zero in child. 570 ** -1 on unrecoverable error. 571 ** 572 ** Notes: 573 ** I'm awfully sorry this looks so awful. That's 574 ** vfork for you..... 575 */ 576 577 # define NFORKTRIES 5 578 579 # ifndef FORK 580 # define FORK fork 581 # endif 582 583 # define DOFORK(fORKfN) \ 584 {\ 585 register int i;\ 586 \ 587 for (i = NFORKTRIES; --i >= 0; )\ 588 {\ 589 pid = fORKfN();\ 590 if (pid >= 0)\ 591 break;\ 592 if (i > 0)\ 593 sleep((unsigned) NFORKTRIES - i);\ 594 }\ 595 } 596 /* 597 ** DOFORK -- simple fork interface to DOFORK. 598 ** 599 ** Parameters: 600 ** none. 601 ** 602 ** Returns: 603 ** pid of child in parent. 604 ** zero in child. 605 ** -1 on error. 606 ** 607 ** Side Effects: 608 ** returns twice, once in parent and once in child. 609 */ 610 611 dofork() 612 { 613 register int pid; 614 615 DOFORK(fork); 616 return (pid); 617 } 618 /* 619 ** ENDMAILER -- Wait for mailer to terminate. 620 ** 621 ** We should never get fatal errors (e.g., segmentation 622 ** violation), so we report those specially. For other 623 ** errors, we choose a status message (into statmsg), 624 ** and if it represents an error, we print it. 625 ** 626 ** Parameters: 627 ** pid -- pid of mailer. 628 ** name -- name of mailer (for error messages). 629 ** 630 ** Returns: 631 ** exit code of mailer. 632 ** 633 ** Side Effects: 634 ** none. 635 */ 636 637 endmailer(mci, name) 638 register MCI *mci; 639 char *name; 640 { 641 int st; 642 643 /* close any connections */ 644 if (mci->mci_in != NULL) 645 (void) fclose(mci->mci_in); 646 if (mci->mci_out != NULL) 647 (void) fclose(mci->mci_out); 648 mci->mci_in = mci->mci_out = NULL; 649 mci->mci_state = MCIS_CLOSED; 650 651 /* in the IPC case there is nothing to wait for */ 652 if (mci->mci_pid == 0) 653 return (EX_OK); 654 655 /* wait for the mailer process to die and collect status */ 656 st = waitfor(mci->mci_pid); 657 if (st == -1) 658 { 659 syserr("endmailer %s: wait", name); 660 return (EX_SOFTWARE); 661 } 662 663 /* see if it died a horrid death */ 664 if ((st & 0377) != 0) 665 { 666 syserr("mailer %s died with signal %o", name, st); 667 ExitStat = EX_TEMPFAIL; 668 return (EX_TEMPFAIL); 669 } 670 671 /* normal death -- return status */ 672 st = (st >> 8) & 0377; 673 return (st); 674 } 675 /* 676 ** OPENMAILER -- open connection to mailer. 677 ** 678 ** Parameters: 679 ** m -- mailer descriptor. 680 ** pvp -- parameter vector to pass to mailer. 681 ** ctladdr -- controlling address for user. 682 ** clever -- create a full duplex connection. 683 ** 684 ** Returns: 685 ** The mail connection info struct for this connection. 686 ** NULL on failure. 687 ** 688 ** Side Effects: 689 ** creates a mailer in a subprocess. 690 */ 691 692 MCI * 693 openmailer(m, pvp, ctladdr, clever, e) 694 MAILER *m; 695 char **pvp; 696 ADDRESS *ctladdr; 697 bool clever; 698 ENVELOPE *e; 699 { 700 int pid; 701 register MCI *mci; 702 int mpvect[2]; 703 int rpvect[2]; 704 extern FILE *fdopen(); 705 706 if (tTd(11, 1)) 707 { 708 printf("openmailer:"); 709 printav(pvp); 710 } 711 errno = 0; 712 713 CurHostName = m->m_mailer; 714 715 /* 716 ** Deal with the special case of mail handled through an IPC 717 ** connection. 718 ** In this case we don't actually fork. We must be 719 ** running SMTP for this to work. We will return a 720 ** zero pid to indicate that we are running IPC. 721 ** We also handle a debug version that just talks to stdin/out. 722 */ 723 724 /* check for Local Person Communication -- not for mortals!!! */ 725 if (strcmp(m->m_mailer, "[LPC]") == 0) 726 { 727 mci = (MCI *) xalloc(sizeof *mci); 728 bzero((char *) mci, sizeof *mci); 729 mci->mci_in = stdin; 730 mci->mci_out = stdout; 731 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 732 mci->mci_mailer = m; 733 } 734 else if (strcmp(m->m_mailer, "[IPC]") == 0 || 735 strcmp(m->m_mailer, "[TCP]") == 0) 736 { 737 #ifdef DAEMON 738 register int i; 739 register u_short port; 740 char *curhost; 741 extern MCI *mci_get(); 742 extern char *hostsignature(); 743 744 CurHostName = pvp[1]; 745 curhost = hostsignature(m, pvp[1], e); 746 747 if (!clever) 748 syserr("non-clever IPC"); 749 if (pvp[2] != NULL) 750 port = atoi(pvp[2]); 751 else 752 port = 0; 753 while (*curhost != '\0') 754 { 755 register char *p; 756 char hostbuf[MAXNAME]; 757 758 /* pull the next host from the signature */ 759 p = strchr(curhost, ':'); 760 if (p == NULL) 761 p = &curhost[strlen(curhost)]; 762 strncpy(hostbuf, curhost, p - curhost); 763 hostbuf[p - curhost] = '\0'; 764 if (*p != '\0') 765 p++; 766 curhost = p; 767 768 /* see if we already know that this host is fried */ 769 CurHostName = hostbuf; 770 mci = mci_get(hostbuf, m); 771 if (mci->mci_state != MCIS_CLOSED) 772 { 773 if (tTd(11, 1)) 774 { 775 printf("openmailer: "); 776 mci_dump(mci); 777 } 778 return mci; 779 } 780 mci->mci_mailer = m; 781 if (mci->mci_exitstat != EX_OK) 782 continue; 783 784 /* try the connection */ 785 setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 786 message(Arpa_Info, "Connecting to %s (%s)...", 787 hostbuf, m->m_name); 788 i = makeconnection(hostbuf, port, mci, 789 bitnset(M_SECURE_PORT, m->m_flags)); 790 mci->mci_exitstat = i; 791 mci->mci_errno = errno; 792 if (i == EX_OK) 793 { 794 mci->mci_state = MCIS_OPENING; 795 mci_cache(mci); 796 break; 797 } 798 else if (tTd(11, 1)) 799 printf("openmailer: makeconnection => stat=%d, errno=%d\n", 800 i, errno); 801 802 803 /* enter status of this host */ 804 setstat(i); 805 } 806 mci->mci_pid = 0; 807 #else /* no DAEMON */ 808 syserr("openmailer: no IPC"); 809 if (tTd(11, 1)) 810 printf("openmailer: NULL\n"); 811 return NULL; 812 #endif /* DAEMON */ 813 } 814 else 815 { 816 /* create a pipe to shove the mail through */ 817 if (pipe(mpvect) < 0) 818 { 819 syserr("openmailer: pipe (to mailer)"); 820 if (tTd(11, 1)) 821 printf("openmailer: NULL\n"); 822 return NULL; 823 } 824 825 /* if this mailer speaks smtp, create a return pipe */ 826 if (clever && pipe(rpvect) < 0) 827 { 828 syserr("openmailer: pipe (from mailer)"); 829 (void) close(mpvect[0]); 830 (void) close(mpvect[1]); 831 if (tTd(11, 1)) 832 printf("openmailer: NULL\n"); 833 return NULL; 834 } 835 836 /* 837 ** Actually fork the mailer process. 838 ** DOFORK is clever about retrying. 839 ** 840 ** Dispose of SIGCHLD signal catchers that may be laying 841 ** around so that endmail will get it. 842 */ 843 844 if (e->e_xfp != NULL) 845 (void) fflush(e->e_xfp); /* for debugging */ 846 (void) fflush(stdout); 847 # ifdef SIGCHLD 848 (void) signal(SIGCHLD, SIG_DFL); 849 # endif /* SIGCHLD */ 850 DOFORK(FORK); 851 /* pid is set by DOFORK */ 852 if (pid < 0) 853 { 854 /* failure */ 855 syserr("openmailer: cannot fork"); 856 (void) close(mpvect[0]); 857 (void) close(mpvect[1]); 858 if (clever) 859 { 860 (void) close(rpvect[0]); 861 (void) close(rpvect[1]); 862 } 863 if (tTd(11, 1)) 864 printf("openmailer: NULL\n"); 865 return NULL; 866 } 867 else if (pid == 0) 868 { 869 int i; 870 int saveerrno; 871 char *env[2]; 872 extern int DtableSize; 873 874 /* child -- set up input & exec mailer */ 875 /* make diagnostic output be standard output */ 876 (void) signal(SIGINT, SIG_IGN); 877 (void) signal(SIGHUP, SIG_IGN); 878 (void) signal(SIGTERM, SIG_DFL); 879 880 /* arrange to filter std & diag output of command */ 881 if (clever) 882 { 883 (void) close(rpvect[0]); 884 (void) close(1); 885 (void) dup(rpvect[1]); 886 (void) close(rpvect[1]); 887 } 888 else if (OpMode == MD_SMTP || HoldErrs) 889 { 890 /* put mailer output in transcript */ 891 (void) close(1); 892 (void) dup(fileno(e->e_xfp)); 893 } 894 (void) close(2); 895 (void) dup(1); 896 897 /* arrange to get standard input */ 898 (void) close(mpvect[1]); 899 (void) close(0); 900 if (dup(mpvect[0]) < 0) 901 { 902 syserr("Cannot dup to zero!"); 903 _exit(EX_OSERR); 904 } 905 (void) close(mpvect[0]); 906 if (!bitnset(M_RESTR, m->m_flags)) 907 { 908 if (ctladdr == NULL || ctladdr->q_uid == 0) 909 { 910 (void) setgid(DefGid); 911 (void) initgroups(DefUser, DefGid); 912 (void) setuid(DefUid); 913 } 914 else 915 { 916 (void) setgid(ctladdr->q_gid); 917 (void) initgroups(ctladdr->q_ruser? 918 ctladdr->q_ruser: ctladdr->q_user, 919 ctladdr->q_gid); 920 (void) setuid(ctladdr->q_uid); 921 } 922 } 923 924 /* arrange for all the files to be closed */ 925 for (i = 3; i < DtableSize; i++) 926 { 927 register int j; 928 if ((j = fcntl(i, F_GETFD, 0)) != -1) 929 (void)fcntl(i, F_SETFD, j|1); 930 } 931 932 /* try to execute the mailer */ 933 env[0] = "AGENT=sendmail"; 934 env[1] = NULL; 935 execve(m->m_mailer, pvp, env); 936 saveerrno = errno; 937 syserr("Cannot exec %s", m->m_mailer); 938 if (m == LocalMailer) 939 _exit(EX_TEMPFAIL); 940 switch (saveerrno) 941 { 942 case EIO: 943 case EAGAIN: 944 case ENOMEM: 945 # ifdef EPROCLIM 946 case EPROCLIM: 947 # endif 948 _exit(EX_TEMPFAIL); 949 } 950 _exit(EX_UNAVAILABLE); 951 } 952 953 /* 954 ** Set up return value. 955 */ 956 957 mci = (MCI *) xalloc(sizeof *mci); 958 bzero((char *) mci, sizeof *mci); 959 mci->mci_mailer = m; 960 mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 961 mci->mci_pid = pid; 962 (void) close(mpvect[0]); 963 mci->mci_out = fdopen(mpvect[1], "w"); 964 if (clever) 965 { 966 (void) close(rpvect[1]); 967 mci->mci_in = fdopen(rpvect[0], "r"); 968 } 969 else 970 { 971 mci->mci_flags |= MCIF_TEMP; 972 mci->mci_in = NULL; 973 } 974 } 975 976 /* 977 ** If we are in SMTP opening state, send initial protocol. 978 */ 979 980 if (clever && mci->mci_state != MCIS_CLOSED) 981 { 982 smtpinit(m, mci, e); 983 } 984 if (tTd(11, 1)) 985 { 986 printf("openmailer: "); 987 mci_dump(mci); 988 } 989 990 return mci; 991 } 992 /* 993 ** GIVERESPONSE -- Interpret an error response from a mailer 994 ** 995 ** Parameters: 996 ** stat -- the status code from the mailer (high byte 997 ** only; core dumps must have been taken care of 998 ** already). 999 ** m -- the mailer descriptor for this mailer. 1000 ** 1001 ** Returns: 1002 ** none. 1003 ** 1004 ** Side Effects: 1005 ** Errors may be incremented. 1006 ** ExitStat may be set. 1007 */ 1008 1009 giveresponse(stat, m, e) 1010 int stat; 1011 register MAILER *m; 1012 ENVELOPE *e; 1013 { 1014 register char *statmsg; 1015 extern char *SysExMsg[]; 1016 register int i; 1017 extern int N_SysEx; 1018 #ifdef NAMED_BIND 1019 extern int h_errno; 1020 #endif 1021 char buf[MAXLINE]; 1022 1023 #ifdef lint 1024 if (m == NULL) 1025 return; 1026 #endif lint 1027 1028 /* 1029 ** Compute status message from code. 1030 */ 1031 1032 i = stat - EX__BASE; 1033 if (stat == 0) 1034 statmsg = "250 Sent"; 1035 else if (i < 0 || i > N_SysEx) 1036 { 1037 (void) sprintf(buf, "554 unknown mailer error %d", stat); 1038 stat = EX_UNAVAILABLE; 1039 statmsg = buf; 1040 } 1041 else if (stat == EX_TEMPFAIL) 1042 { 1043 (void) strcpy(buf, SysExMsg[i]); 1044 #ifdef NAMED_BIND 1045 if (h_errno == TRY_AGAIN) 1046 { 1047 extern char *errstring(); 1048 1049 statmsg = errstring(h_errno+MAX_ERRNO); 1050 } 1051 else 1052 #endif 1053 { 1054 if (errno != 0) 1055 { 1056 extern char *errstring(); 1057 1058 statmsg = errstring(errno); 1059 } 1060 else 1061 { 1062 #ifdef SMTP 1063 extern char SmtpError[]; 1064 1065 statmsg = SmtpError; 1066 #else /* SMTP */ 1067 statmsg = NULL; 1068 #endif /* SMTP */ 1069 } 1070 } 1071 if (statmsg != NULL && statmsg[0] != '\0') 1072 { 1073 (void) strcat(buf, ": "); 1074 (void) strcat(buf, statmsg); 1075 } 1076 statmsg = buf; 1077 } 1078 else 1079 { 1080 statmsg = SysExMsg[i]; 1081 } 1082 1083 /* 1084 ** Print the message as appropriate 1085 */ 1086 1087 if (stat == EX_OK || stat == EX_TEMPFAIL) 1088 message(Arpa_Info, &statmsg[4]); 1089 else 1090 { 1091 Errors++; 1092 usrerr(statmsg); 1093 } 1094 1095 /* 1096 ** Final cleanup. 1097 ** Log a record of the transaction. Compute the new 1098 ** ExitStat -- if we already had an error, stick with 1099 ** that. 1100 */ 1101 1102 if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2)) 1103 logdelivery(&statmsg[4], e); 1104 1105 if (stat != EX_TEMPFAIL) 1106 setstat(stat); 1107 if (stat != EX_OK) 1108 { 1109 if (e->e_message != NULL) 1110 free(e->e_message); 1111 e->e_message = newstr(&statmsg[4]); 1112 } 1113 errno = 0; 1114 #ifdef NAMED_BIND 1115 h_errno = 0; 1116 #endif 1117 } 1118 /* 1119 ** LOGDELIVERY -- log the delivery in the system log 1120 ** 1121 ** Parameters: 1122 ** stat -- the message to print for the status 1123 ** 1124 ** Returns: 1125 ** none 1126 ** 1127 ** Side Effects: 1128 ** none 1129 */ 1130 1131 logdelivery(stat, e) 1132 char *stat; 1133 register ENVELOPE *e; 1134 { 1135 extern char *pintvl(); 1136 1137 # ifdef LOG 1138 syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", e->e_id, 1139 e->e_to, pintvl(curtime() - e->e_ctime, TRUE), stat); 1140 # endif /* LOG */ 1141 } 1142 /* 1143 ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 1144 ** 1145 ** This can be made an arbitrary message separator by changing $l 1146 ** 1147 ** One of the ugliest hacks seen by human eyes is contained herein: 1148 ** UUCP wants those stupid "remote from <host>" lines. Why oh why 1149 ** does a well-meaning programmer such as myself have to deal with 1150 ** this kind of antique garbage???? 1151 ** 1152 ** Parameters: 1153 ** fp -- the file to output to. 1154 ** m -- the mailer describing this entry. 1155 ** 1156 ** Returns: 1157 ** none 1158 ** 1159 ** Side Effects: 1160 ** outputs some text to fp. 1161 */ 1162 1163 putfromline(fp, m, e) 1164 register FILE *fp; 1165 register MAILER *m; 1166 ENVELOPE *e; 1167 { 1168 char *template = "\001l\n"; 1169 char buf[MAXLINE]; 1170 1171 if (bitnset(M_NHDR, m->m_flags)) 1172 return; 1173 1174 # ifdef UGLYUUCP 1175 if (bitnset(M_UGLYUUCP, m->m_flags)) 1176 { 1177 char *bang; 1178 char xbuf[MAXLINE]; 1179 1180 expand("\001<", buf, &buf[sizeof buf - 1], e); 1181 bang = strchr(buf, '!'); 1182 if (bang == NULL) 1183 syserr("No ! in UUCP! (%s)", buf); 1184 else 1185 { 1186 *bang++ = '\0'; 1187 (void) sprintf(xbuf, "From %s \001d remote from %s\n", bang, buf); 1188 template = xbuf; 1189 } 1190 } 1191 # endif /* UGLYUUCP */ 1192 expand(template, buf, &buf[sizeof buf - 1], e); 1193 putline(buf, fp, m); 1194 } 1195 /* 1196 ** PUTBODY -- put the body of a message. 1197 ** 1198 ** Parameters: 1199 ** fp -- file to output onto. 1200 ** m -- a mailer descriptor to control output format. 1201 ** e -- the envelope to put out. 1202 ** 1203 ** Returns: 1204 ** none. 1205 ** 1206 ** Side Effects: 1207 ** The message is written onto fp. 1208 */ 1209 1210 putbody(fp, m, e) 1211 FILE *fp; 1212 MAILER *m; 1213 register ENVELOPE *e; 1214 { 1215 char buf[MAXLINE]; 1216 1217 /* 1218 ** Output the body of the message 1219 */ 1220 1221 if (e->e_dfp == NULL) 1222 { 1223 if (e->e_df != NULL) 1224 { 1225 e->e_dfp = fopen(e->e_df, "r"); 1226 if (e->e_dfp == NULL) 1227 syserr("putbody: Cannot open %s for %s from %s", 1228 e->e_df, e->e_to, e->e_from); 1229 } 1230 else 1231 putline("<<< No Message Collected >>>", fp, m); 1232 } 1233 if (e->e_dfp != NULL) 1234 { 1235 rewind(e->e_dfp); 1236 while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 1237 { 1238 if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1239 strncmp(buf, "From ", 5) == 0) 1240 (void) putc('>', fp); 1241 putline(buf, fp, m); 1242 } 1243 1244 if (ferror(e->e_dfp)) 1245 { 1246 syserr("putbody: read error"); 1247 ExitStat = EX_IOERR; 1248 } 1249 } 1250 1251 (void) fflush(fp); 1252 if (ferror(fp) && errno != EPIPE) 1253 { 1254 syserr("putbody: write error"); 1255 ExitStat = EX_IOERR; 1256 } 1257 errno = 0; 1258 } 1259 /* 1260 ** MAILFILE -- Send a message to a file. 1261 ** 1262 ** If the file has the setuid/setgid bits set, but NO execute 1263 ** bits, sendmail will try to become the owner of that file 1264 ** rather than the real user. Obviously, this only works if 1265 ** sendmail runs as root. 1266 ** 1267 ** This could be done as a subordinate mailer, except that it 1268 ** is used implicitly to save messages in ~/dead.letter. We 1269 ** view this as being sufficiently important as to include it 1270 ** here. For example, if the system is dying, we shouldn't have 1271 ** to create another process plus some pipes to save the message. 1272 ** 1273 ** Parameters: 1274 ** filename -- the name of the file to send to. 1275 ** ctladdr -- the controlling address header -- includes 1276 ** the userid/groupid to be when sending. 1277 ** 1278 ** Returns: 1279 ** The exit code associated with the operation. 1280 ** 1281 ** Side Effects: 1282 ** none. 1283 */ 1284 1285 mailfile(filename, ctladdr, e) 1286 char *filename; 1287 ADDRESS *ctladdr; 1288 register ENVELOPE *e; 1289 { 1290 register FILE *f; 1291 register int pid; 1292 int mode; 1293 1294 /* 1295 ** Fork so we can change permissions here. 1296 ** Note that we MUST use fork, not vfork, because of 1297 ** the complications of calling subroutines, etc. 1298 */ 1299 1300 DOFORK(fork); 1301 1302 if (pid < 0) 1303 return (EX_OSERR); 1304 else if (pid == 0) 1305 { 1306 /* child -- actually write to file */ 1307 struct stat stb; 1308 1309 (void) signal(SIGINT, SIG_DFL); 1310 (void) signal(SIGHUP, SIG_DFL); 1311 (void) signal(SIGTERM, SIG_DFL); 1312 (void) umask(OldUmask); 1313 1314 if (stat(filename, &stb) < 0) 1315 stb.st_mode = 0666; 1316 mode = stb.st_mode; 1317 1318 /* limit the errors to those actually caused in the child */ 1319 errno = 0; 1320 ExitStat = EX_OK; 1321 1322 if (bitset(0111, stb.st_mode)) 1323 exit(EX_CANTCREAT); 1324 if (ctladdr == NULL) 1325 ctladdr = &e->e_from; 1326 else 1327 { 1328 /* ignore setuid and setgid bits */ 1329 mode &= ~(S_ISGID|S_ISUID); 1330 } 1331 1332 /* we have to open the dfile BEFORE setuid */ 1333 if (e->e_dfp == NULL && e->e_df != NULL) 1334 { 1335 e->e_dfp = fopen(e->e_df, "r"); 1336 if (e->e_dfp == NULL) 1337 { 1338 syserr("mailfile: Cannot open %s for %s from %s", 1339 e->e_df, e->e_to, e->e_from); 1340 } 1341 } 1342 1343 if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 1344 { 1345 if (ctladdr->q_uid == 0) 1346 { 1347 (void) setgid(DefGid); 1348 (void) initgroups(DefUser, DefGid); 1349 } 1350 else 1351 { 1352 (void) setgid(ctladdr->q_gid); 1353 (void) initgroups(ctladdr->q_ruser ? 1354 ctladdr->q_ruser : ctladdr->q_user, 1355 ctladdr->q_gid); 1356 } 1357 } 1358 if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 1359 { 1360 if (ctladdr->q_uid == 0) 1361 (void) setuid(DefUid); 1362 else 1363 (void) setuid(ctladdr->q_uid); 1364 } 1365 FileName = filename; 1366 LineNumber = 0; 1367 f = dfopen(filename, "a"); 1368 if (f == NULL) 1369 { 1370 message("cannot open"); 1371 exit(EX_CANTCREAT); 1372 } 1373 1374 putfromline(f, ProgMailer, e); 1375 (*e->e_puthdr)(f, ProgMailer, e); 1376 putline("\n", f, ProgMailer); 1377 (*e->e_putbody)(f, ProgMailer, e); 1378 putline("\n", f, ProgMailer); 1379 if (ferror(f)) 1380 { 1381 message("I/O error"); 1382 setstat(EX_IOERR); 1383 } 1384 (void) fclose(f); 1385 (void) fflush(stdout); 1386 1387 /* reset ISUID & ISGID bits for paranoid systems */ 1388 (void) chmod(filename, (int) stb.st_mode); 1389 exit(ExitStat); 1390 /*NOTREACHED*/ 1391 } 1392 else 1393 { 1394 /* parent -- wait for exit status */ 1395 int st; 1396 1397 st = waitfor(pid); 1398 if ((st & 0377) != 0) 1399 return (EX_UNAVAILABLE); 1400 else 1401 return ((st >> 8) & 0377); 1402 /*NOTREACHED*/ 1403 } 1404 } 1405 /* 1406 ** SENDALL -- actually send all the messages. 1407 ** 1408 ** Parameters: 1409 ** e -- the envelope to send. 1410 ** mode -- the delivery mode to use. If SM_DEFAULT, use 1411 ** the current SendMode. 1412 ** 1413 ** Returns: 1414 ** none. 1415 ** 1416 ** Side Effects: 1417 ** Scans the send lists and sends everything it finds. 1418 ** Delivers any appropriate error messages. 1419 ** If we are running in a non-interactive mode, takes the 1420 ** appropriate action. 1421 */ 1422 1423 sendall(e, mode) 1424 ENVELOPE *e; 1425 char mode; 1426 { 1427 register ADDRESS *q; 1428 bool oldverbose; 1429 int pid; 1430 int nsent; 1431 # ifdef LOCKF 1432 struct flock lfd; 1433 # endif 1434 1435 /* determine actual delivery mode */ 1436 if (mode == SM_DEFAULT) 1437 { 1438 extern bool shouldqueue(); 1439 1440 if (shouldqueue(e->e_msgpriority, e->e_ctime)) 1441 mode = SM_QUEUE; 1442 else 1443 mode = SendMode; 1444 } 1445 1446 if (tTd(13, 1)) 1447 { 1448 printf("\nSENDALL: mode %c, sendqueue:\n", mode); 1449 printaddr(e->e_sendqueue, TRUE); 1450 } 1451 1452 /* 1453 ** Do any preprocessing necessary for the mode we are running. 1454 ** Check to make sure the hop count is reasonable. 1455 ** Delete sends to the sender in mailing lists. 1456 */ 1457 1458 CurEnv = e; 1459 1460 if (e->e_hopcount > MaxHopCount) 1461 { 1462 errno = 0; 1463 syserr("sendall: too many hops %d (%d max): from %s, to %s", 1464 e->e_hopcount, MaxHopCount, e->e_from.q_paddr, e->e_to); 1465 return; 1466 } 1467 1468 if (!MeToo) 1469 { 1470 extern ADDRESS *recipient(); 1471 1472 e->e_from.q_flags |= QDONTSEND; 1473 (void) recipient(&e->e_from, &e->e_sendqueue, e); 1474 } 1475 1476 # ifdef QUEUE 1477 if ((mode == SM_QUEUE || mode == SM_FORK || 1478 (mode != SM_VERIFY && SuperSafe)) && 1479 !bitset(EF_INQUEUE, e->e_flags)) 1480 queueup(e, TRUE, mode == SM_QUEUE); 1481 #endif /* QUEUE */ 1482 1483 oldverbose = Verbose; 1484 switch (mode) 1485 { 1486 case SM_VERIFY: 1487 Verbose = TRUE; 1488 break; 1489 1490 case SM_QUEUE: 1491 queueonly: 1492 e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 1493 return; 1494 1495 case SM_FORK: 1496 if (e->e_xfp != NULL) 1497 (void) fflush(e->e_xfp); 1498 1499 # ifdef LOCKF 1500 /* 1501 ** Since lockf has the interesting semantic that the 1502 ** lock is lost when we fork, we have to risk losing 1503 ** the lock here by closing before the fork, and then 1504 ** trying to get it back in the child. 1505 */ 1506 1507 if (e->e_lockfp != NULL) 1508 { 1509 (void) fclose(e->e_lockfp); 1510 e->e_lockfp = NULL; 1511 } 1512 # endif /* LOCKF */ 1513 1514 pid = fork(); 1515 if (pid < 0) 1516 { 1517 goto queueonly; 1518 } 1519 else if (pid > 0) 1520 { 1521 /* be sure we leave the temp files to our child */ 1522 e->e_id = e->e_df = NULL; 1523 # ifndef LOCKF 1524 if (e->e_lockfp != NULL) 1525 (void) fclose(e->e_lockfp); 1526 # endif 1527 return; 1528 } 1529 1530 /* double fork to avoid zombies */ 1531 if (fork() > 0) 1532 exit(EX_OK); 1533 1534 /* be sure we are immune from the terminal */ 1535 disconnect(FALSE); 1536 1537 # ifdef LOCKF 1538 /* 1539 ** Now try to get our lock back. 1540 */ 1541 1542 lfd.l_type = F_WRLCK; 1543 lfd.l_whence = lfd.l_start = lfd.l_len = 0; 1544 e->e_lockfp = fopen(queuename(e, 'q'), "r+"); 1545 if (e->e_lockfp == NULL || 1546 fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0) 1547 { 1548 /* oops.... lost it */ 1549 # ifdef LOG 1550 if (LogLevel > 5) 1551 syslog(LOG_NOTICE, "%s: lost lock: %m", 1552 e->e_id); 1553 # endif /* LOG */ 1554 exit(EX_OK); 1555 } 1556 # endif /* LOCKF */ 1557 1558 break; 1559 } 1560 1561 /* 1562 ** Run through the list and send everything. 1563 */ 1564 1565 nsent = 0; 1566 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1567 { 1568 if (mode == SM_VERIFY) 1569 { 1570 e->e_to = q->q_paddr; 1571 if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1572 message(Arpa_Info, "deliverable"); 1573 } 1574 else if (!bitset(QDONTSEND, q->q_flags)) 1575 { 1576 # ifdef QUEUE 1577 /* 1578 ** Checkpoint the send list every few addresses 1579 */ 1580 1581 if (nsent >= CheckpointInterval) 1582 { 1583 queueup(e, TRUE, FALSE); 1584 nsent = 0; 1585 } 1586 # endif /* QUEUE */ 1587 if (deliver(e, q) == EX_OK) 1588 nsent++; 1589 } 1590 } 1591 Verbose = oldverbose; 1592 1593 /* 1594 ** Now run through and check for errors. 1595 */ 1596 1597 if (mode == SM_VERIFY) 1598 return; 1599 1600 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1601 { 1602 register ADDRESS *qq; 1603 1604 if (tTd(13, 3)) 1605 { 1606 printf("Checking "); 1607 printaddr(q, FALSE); 1608 } 1609 1610 /* only send errors if the message failed */ 1611 if (!bitset(QBADADDR, q->q_flags)) 1612 continue; 1613 1614 /* we have an address that failed -- find the parent */ 1615 for (qq = q; qq != NULL; qq = qq->q_alias) 1616 { 1617 char obuf[MAXNAME + 6]; 1618 extern char *aliaslookup(); 1619 1620 /* we can only have owners for local addresses */ 1621 if (!bitnset(M_LOCAL, qq->q_mailer->m_flags)) 1622 continue; 1623 1624 /* see if the owner list exists */ 1625 (void) strcpy(obuf, "owner-"); 1626 if (strncmp(qq->q_user, "owner-", 6) == 0) 1627 (void) strcat(obuf, "owner"); 1628 else 1629 (void) strcat(obuf, qq->q_user); 1630 if (!bitnset(M_USR_UPPER, qq->q_mailer->m_flags)) 1631 makelower(obuf); 1632 if (aliaslookup(obuf) == NULL) 1633 continue; 1634 1635 if (tTd(13, 4)) 1636 printf("Errors to %s\n", obuf); 1637 1638 /* owner list exists -- add it to the error queue */ 1639 sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue, e); 1640 1641 /* and set the return path to point to it */ 1642 e->e_returnpath = newstr(obuf); 1643 1644 ErrorMode = EM_MAIL; 1645 break; 1646 } 1647 1648 /* if we did not find an owner, send to the sender */ 1649 if (qq == NULL && bitset(QBADADDR, q->q_flags)) 1650 sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue, e); 1651 } 1652 1653 if (mode == SM_FORK) 1654 finis(); 1655 } 1656 /* 1657 ** HOSTSIGNATURE -- return the "signature" for a host. 1658 ** 1659 ** The signature describes how we are going to send this -- it 1660 ** can be just the hostname (for non-Internet hosts) or can be 1661 ** an ordered list of MX hosts. 1662 ** 1663 ** Parameters: 1664 ** m -- the mailer describing this host. 1665 ** host -- the host name. 1666 ** e -- the current envelope. 1667 ** 1668 ** Returns: 1669 ** The signature for this host. 1670 ** 1671 ** Side Effects: 1672 ** Can tweak the symbol table. 1673 */ 1674 1675 char * 1676 hostsignature(m, host, e) 1677 register MAILER *m; 1678 char *host; 1679 ENVELOPE *e; 1680 { 1681 register char *p; 1682 register STAB *s; 1683 int i; 1684 int len; 1685 #ifdef NAMED_BIND 1686 int nmx; 1687 auto int rcode; 1688 char *mxhosts[MAXMXHOSTS + 1]; 1689 static char myhostbuf[MAXNAME]; 1690 #endif 1691 1692 /* 1693 ** Check to see if this uses IPC -- if not, it can't have MX records. 1694 */ 1695 1696 p = m->m_mailer; 1697 if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 1698 { 1699 /* just an ordinary mailer */ 1700 return host; 1701 } 1702 1703 /* 1704 ** If it is a numeric address, just return it. 1705 */ 1706 1707 if (host[0] == '[') 1708 return host; 1709 1710 /* 1711 ** Look it up in the symbol table. 1712 */ 1713 1714 s = stab(host, ST_HOSTSIG, ST_ENTER); 1715 if (s->s_hostsig != NULL) 1716 return s->s_hostsig; 1717 1718 /* 1719 ** Not already there -- create a signature. 1720 */ 1721 1722 #ifdef NAMED_BIND 1723 if (myhostbuf[0] == '\0') 1724 expand("\001j", myhostbuf, &myhostbuf[sizeof myhostbuf - 1], e); 1725 1726 nmx = getmxrr(host, mxhosts, myhostbuf, &rcode); 1727 if (nmx <= 0) 1728 { 1729 register MCI *mci; 1730 extern int errno; 1731 extern MCI *mci_get(); 1732 1733 /* update the connection info for this host */ 1734 mci = mci_get(host, m); 1735 mci->mci_exitstat = rcode; 1736 mci->mci_errno = errno; 1737 1738 /* and return the original host name as the signature */ 1739 s->s_hostsig = host; 1740 return host; 1741 } 1742 1743 len = 0; 1744 for (i = 0; i < nmx; i++) 1745 { 1746 len += strlen(mxhosts[i]) + 1; 1747 } 1748 s->s_hostsig = p = xalloc(len); 1749 for (i = 0; i < nmx; i++) 1750 { 1751 if (i != 0) 1752 *p++ = ':'; 1753 strcpy(p, mxhosts[i]); 1754 p += strlen(p); 1755 } 1756 makelower(s->s_hostsig); 1757 #else 1758 /* not using BIND -- the signature is just the host name */ 1759 s->s_hostsig = host; 1760 #endif 1761 if (tTd(17, 1)) 1762 printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 1763 return s->s_hostsig; 1764 } 1765