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