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