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