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