1 # include <signal.h> 2 # include <errno.h> 3 # include "sendmail.h" 4 # include <sys/stat.h> 5 # ifdef LOG 6 # include <syslog.h> 7 # endif LOG 8 9 SCCSID(@(#)deliver.c 3.81 05/31/82); 10 11 /* 12 ** DELIVER -- Deliver a message to a list of addresses. 13 ** 14 ** This routine delivers to everyone on the same host as the 15 ** user on the head of the list. It is clever about mailers 16 ** that don't handle multiple users. It is NOT guaranteed 17 ** that it will deliver to all these addresses however -- so 18 ** deliver should be called once for each address on the 19 ** list. 20 ** 21 ** Parameters: 22 ** firstto -- head of the address list to deliver to. 23 ** 24 ** Returns: 25 ** zero -- successfully delivered. 26 ** else -- some failure, see ExitStat for more info. 27 ** 28 ** Side Effects: 29 ** The standard input is passed off to someone. 30 */ 31 32 deliver(firstto) 33 ADDRESS *firstto; 34 { 35 char *host; /* host being sent to */ 36 char *user; /* user being sent to */ 37 char **pvp; 38 register char **mvp; 39 register char *p; 40 register struct mailer *m; /* mailer for this recipient */ 41 register int i; 42 extern bool checkcompat(); 43 char *pv[MAXPV+1]; 44 char tobuf[MAXLINE]; /* text line of to people */ 45 char buf[MAXNAME]; 46 ADDRESS *ctladdr; 47 extern ADDRESS *getctladdr(); 48 char tfrombuf[MAXNAME]; /* translated from person */ 49 extern char **prescan(); 50 register ADDRESS *to = firstto; 51 bool clever = FALSE; /* running user smtp to this mailer */ 52 bool tempfail = FALSE; 53 ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 54 55 errno = 0; 56 if (!ForceMail && bitset(QDONTSEND, to->q_flags)) 57 return (0); 58 59 m = to->q_mailer; 60 host = to->q_host; 61 62 # ifdef DEBUG 63 if (Debug) 64 printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 65 m->m_mno, host, to->q_user); 66 # endif DEBUG 67 if (Verbose) 68 message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name); 69 70 /* 71 ** If this mailer is expensive, and if we don't want to make 72 ** connections now, just mark these addresses and return. 73 ** This is useful if we want to batch connections to 74 ** reduce load. This will cause the messages to be 75 ** queued up, and a daemon will come along to send the 76 ** messages later. 77 ** This should be on a per-mailer basis. 78 */ 79 80 if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags)) 81 { 82 CurEnv->e_queueup = TRUE; 83 for (; to != NULL; to = to->q_next) 84 if (!bitset(QDONTSEND, to->q_flags)) 85 to->q_flags |= QQUEUEUP|QDONTSEND; 86 return (0); 87 } 88 89 /* 90 ** Do initial argv setup. 91 ** Insert the mailer name. Notice that $x expansion is 92 ** NOT done on the mailer name. Then, if the mailer has 93 ** a picky -f flag, we insert it as appropriate. This 94 ** code does not check for 'pv' overflow; this places a 95 ** manifest lower limit of 4 for MAXPV. 96 ** We rewrite the from address here, being careful 97 ** to also rewrite it again using ruleset 2 to 98 ** eliminate redundancies. 99 */ 100 101 /* rewrite from address, using rewriting rules */ 102 expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv); 103 mvp = prescan(buf, '\0'); 104 if (mvp == NULL) 105 { 106 syserr("bad mailer from translate \"%s\"", buf); 107 return (EX_SOFTWARE); 108 } 109 rewrite(mvp, 2); 110 cataddr(mvp, tfrombuf, sizeof tfrombuf); 111 112 define('g', tfrombuf); /* translated sender address */ 113 define('h', host); /* 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], CurEnv); 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], CurEnv); 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 167 /* send the initial SMTP protocol */ 168 i = smtpinit(m, pv, (ADDRESS *) NULL); 169 # ifdef QUEUE 170 if (i == EX_TEMPFAIL) 171 { 172 CurEnv->e_queueup = TRUE; 173 tempfail = TRUE; 174 } 175 # endif QUEUE 176 # else SMTP 177 /* oops! we don't implement SMTP */ 178 syserr("SMTP style mailer"); 179 return (EX_SOFTWARE); 180 # endif SMTP 181 } 182 183 /* 184 ** At this point *mvp points to the argument with $u. We 185 ** run through our address list and append all the addresses 186 ** we can. If we run out of space, do not fret! We can 187 ** always send another copy later. 188 */ 189 190 tobuf[0] = '\0'; 191 CurEnv->e_to = tobuf; 192 ctladdr = NULL; 193 for (; to != NULL; to = to->q_next) 194 { 195 /* avoid sending multiple recipients to dumb mailers */ 196 if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags)) 197 break; 198 199 /* if already sent or not for this host, don't send */ 200 if ((!ForceMail && bitset(QDONTSEND, to->q_flags)) || 201 strcmp(to->q_host, host) != 0 || to->q_mailer != firstto->q_mailer) 202 continue; 203 204 # ifdef DEBUG 205 if (Debug) 206 { 207 printf("\nsend to "); 208 printaddr(to, FALSE); 209 } 210 # endif DEBUG 211 212 /* compute effective uid/gid when sending */ 213 if (to->q_mailer == ProgMailer) 214 ctladdr = getctladdr(to); 215 216 user = to->q_user; 217 CurEnv->e_to = to->q_paddr; 218 to->q_flags |= QDONTSEND; 219 if (tempfail) 220 { 221 to->q_flags |= QQUEUEUP; 222 continue; 223 } 224 225 /* 226 ** Check to see that these people are allowed to 227 ** talk to each other. 228 */ 229 230 if (!checkcompat(to)) 231 { 232 giveresponse(EX_UNAVAILABLE, TRUE, m); 233 continue; 234 } 235 236 /* 237 ** Strip quote bits from names if the mailer is dumb 238 ** about them. 239 */ 240 241 if (bitset(M_STRIPQ, m->m_flags)) 242 { 243 stripquotes(user, TRUE); 244 stripquotes(host, TRUE); 245 } 246 else 247 { 248 stripquotes(user, FALSE); 249 stripquotes(host, FALSE); 250 } 251 252 /* 253 ** Pass it to the other host if we are running SMTP. 254 */ 255 256 if (clever) 257 { 258 # ifdef SMTP 259 i = smtprcpt(to); 260 if (i != EX_OK) 261 { 262 # ifdef QUEUE 263 if (i == EX_TEMPFAIL) 264 { 265 CurEnv->e_queueup = TRUE; 266 to->q_flags |= QQUEUEUP; 267 } 268 else 269 # endif QUEUE 270 { 271 to->q_flags |= QBADADDR; 272 giveresponse(i, TRUE, m); 273 } 274 } 275 # else SMTP 276 syserr("trying to be clever"); 277 # endif SMTP 278 } 279 280 /* 281 ** If an error message has already been given, don't 282 ** bother to send to this address. 283 ** 284 ** >>>>>>>>>> This clause assumes that the local mailer 285 ** >> NOTE >> cannot do any further aliasing; that 286 ** >>>>>>>>>> function is subsumed by sendmail. 287 */ 288 289 if (bitset(QBADADDR, to->q_flags)) 290 continue; 291 292 /* save statistics.... */ 293 Stat.stat_nt[to->q_mailer->m_mno]++; 294 Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize); 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 i = mailfile(user, getctladdr(to)); 309 giveresponse(i, TRUE, m); 310 continue; 311 } 312 } 313 314 /* 315 ** Address is verified -- add this user to mailer 316 ** argv, and add it to the print list of recipients. 317 */ 318 319 /* link together the chain of recipients */ 320 to->q_tchain = tochain; 321 tochain = to; 322 323 /* create list of users for error messages */ 324 if (tobuf[0] != '\0') 325 (void) strcat(tobuf, ","); 326 (void) strcat(tobuf, to->q_paddr); 327 define('u', user); /* to user */ 328 define('z', to->q_home); /* user's home */ 329 330 /* 331 ** Expand out this user into argument list. 332 */ 333 334 if (!clever) 335 { 336 expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 337 *pvp++ = newstr(buf); 338 if (pvp >= &pv[MAXPV - 2]) 339 { 340 /* allow some space for trailing parms */ 341 break; 342 } 343 } 344 } 345 346 /* see if any addresses still exist */ 347 if (tobuf[0] == '\0') 348 { 349 # ifdef SMTP 350 if (clever) 351 smtpquit(pv[0]); 352 # endif SMTP 353 define('g', (char *) NULL); 354 return (0); 355 } 356 357 /* print out messages as full list */ 358 CurEnv->e_to = tobuf; 359 360 /* 361 ** Fill out any parameters after the $u parameter. 362 */ 363 364 while (!clever && *++mvp != NULL) 365 { 366 expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 367 *pvp++ = newstr(buf); 368 if (pvp >= &pv[MAXPV]) 369 syserr("deliver: pv overflow after $u for %s", pv[0]); 370 } 371 *pvp++ = NULL; 372 373 /* 374 ** Call the mailer. 375 ** The argument vector gets built, pipes 376 ** are created as necessary, and we fork & exec as 377 ** appropriate. 378 ** If we are running SMTP, we just need to clean up. 379 */ 380 381 if (ctladdr == NULL) 382 ctladdr = &CurEnv->e_from; 383 # ifdef SMTP 384 if (clever) 385 { 386 i = smtpfinish(m, CurEnv); 387 smtpquit(pv[0]); 388 } 389 else 390 # endif SMTP 391 i = sendoff(m, pv, ctladdr); 392 393 /* 394 ** If we got a temporary failure, arrange to queue the 395 ** addressees. 396 */ 397 398 # ifdef QUEUE 399 if (i == EX_TEMPFAIL) 400 { 401 CurEnv->e_queueup = TRUE; 402 for (to = tochain; to != NULL; to = to->q_tchain) 403 to->q_flags |= QQUEUEUP; 404 } 405 # endif QUEUE 406 407 errno = 0; 408 define('g', (char *) NULL); 409 return (i); 410 } 411 /* 412 ** DOFORK -- do a fork, retrying a couple of times on failure. 413 ** 414 ** This MUST be a macro, since after a vfork we are running 415 ** two processes on the same stack!!! 416 ** 417 ** Parameters: 418 ** none. 419 ** 420 ** Returns: 421 ** From a macro??? You've got to be kidding! 422 ** 423 ** Side Effects: 424 ** Modifies the ==> LOCAL <== variable 'pid', leaving: 425 ** pid of child in parent, zero in child. 426 ** -1 on unrecoverable error. 427 ** 428 ** Notes: 429 ** I'm awfully sorry this looks so awful. That's 430 ** vfork for you..... 431 */ 432 433 # define NFORKTRIES 5 434 # ifdef VFORK 435 # define XFORK vfork 436 # else VFORK 437 # define XFORK fork 438 # endif VFORK 439 440 # define DOFORK(fORKfN) \ 441 {\ 442 register int i;\ 443 \ 444 for (i = NFORKTRIES; i-- > 0; )\ 445 {\ 446 pid = fORKfN();\ 447 if (pid >= 0)\ 448 break;\ 449 sleep(NFORKTRIES - i);\ 450 }\ 451 } 452 /* 453 ** DOFORK -- simple fork interface to DOFORK. 454 ** 455 ** Parameters: 456 ** none. 457 ** 458 ** Returns: 459 ** pid of child in parent. 460 ** zero in child. 461 ** -1 on error. 462 ** 463 ** Side Effects: 464 ** returns twice, once in parent and once in child. 465 */ 466 467 dofork() 468 { 469 register int pid; 470 471 DOFORK(fork); 472 return (pid); 473 } 474 /* 475 ** SENDOFF -- send off call to mailer & collect response. 476 ** 477 ** Parameters: 478 ** m -- mailer descriptor. 479 ** pvp -- parameter vector to send to it. 480 ** ctladdr -- an address pointer controlling the 481 ** user/groupid etc. of the mailer. 482 ** 483 ** Returns: 484 ** exit status of mailer. 485 ** 486 ** Side Effects: 487 ** none. 488 */ 489 490 sendoff(m, pvp, ctladdr) 491 struct mailer *m; 492 char **pvp; 493 ADDRESS *ctladdr; 494 { 495 auto FILE *mfile; 496 auto FILE *rfile; 497 register int i; 498 int pid; 499 500 /* 501 ** Create connection to mailer. 502 */ 503 504 pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile); 505 if (pid < 0) 506 return (-1); 507 508 /* 509 ** Format and send message. 510 */ 511 512 (void) signal(SIGPIPE, SIG_IGN); 513 putfromline(mfile, m); 514 (*CurEnv->e_puthdr)(mfile, m, CurEnv); 515 fprintf(mfile, "\n"); 516 (*CurEnv->e_putbody)(mfile, m, FALSE); 517 (void) fclose(mfile); 518 519 i = endmailer(pid, pvp[0]); 520 giveresponse(i, TRUE, m); 521 522 /* arrange a return receipt if requested */ 523 if (CurEnv->e_retreceipt && bitset(M_LOCAL, m->m_flags) && i == EX_OK) 524 { 525 CurEnv->e_sendreceipt = TRUE; 526 fprintf(Xscript, "%s... successfully delivered\n", CurEnv->e_to); 527 /* do we want to send back more info? */ 528 } 529 530 return (i); 531 } 532 /* 533 ** ENDMAILER -- Wait for mailer to terminate. 534 ** 535 ** We should never get fatal errors (e.g., segmentation 536 ** violation), so we report those specially. For other 537 ** errors, we choose a status message (into statmsg), 538 ** and if it represents an error, we print it. 539 ** 540 ** Parameters: 541 ** pid -- pid of mailer. 542 ** name -- name of mailer (for error messages). 543 ** 544 ** Returns: 545 ** exit code of mailer. 546 ** 547 ** Side Effects: 548 ** none. 549 */ 550 551 endmailer(pid, name) 552 int pid; 553 char *name; 554 { 555 register int i; 556 auto int st; 557 558 /* in the IPC case there is nothing to wait for */ 559 if (pid == 0) 560 return (EX_OK); 561 562 /* wait for the mailer process to die and collect status */ 563 while ((i = wait(&st)) > 0 && i != pid) 564 continue; 565 if (i < 0) 566 { 567 syserr("wait"); 568 return (-1); 569 } 570 571 /* see if it died a horrid death */ 572 if ((st & 0377) != 0) 573 { 574 syserr("%s: stat %o", name, st); 575 ExitStat = EX_UNAVAILABLE; 576 return (-1); 577 } 578 579 /* normal death -- return status */ 580 i = (st >> 8) & 0377; 581 return (i); 582 } 583 /* 584 ** OPENMAILER -- open connection to mailer. 585 ** 586 ** Parameters: 587 ** m -- mailer descriptor. 588 ** pvp -- parameter vector to pass to mailer. 589 ** ctladdr -- controlling address for user. 590 ** clever -- create a full duplex connection. 591 ** pmfile -- pointer to mfile (to mailer) connection. 592 ** prfile -- pointer to rfile (from mailer) connection. 593 ** 594 ** Returns: 595 ** pid of mailer ( > 0 ). 596 ** -1 on error. 597 ** zero on an IPC connection. 598 ** 599 ** Side Effects: 600 ** creates a mailer in a subprocess. 601 */ 602 603 openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 604 struct mailer *m; 605 char **pvp; 606 ADDRESS *ctladdr; 607 bool clever; 608 FILE **pmfile; 609 FILE **prfile; 610 { 611 int pid; 612 int mpvect[2]; 613 int rpvect[2]; 614 FILE *mfile; 615 FILE *rfile; 616 extern FILE *fdopen(); 617 618 # ifdef DEBUG 619 if (Debug) 620 { 621 printf("openmailer:\n"); 622 printav(pvp); 623 } 624 # endif DEBUG 625 errno = 0; 626 627 # ifdef DAEMON 628 /* 629 ** Deal with the special case of mail handled through an IPC 630 ** connection. 631 ** In this case we don't actually fork. We must be 632 ** running SMTP for this to work. We will return a 633 ** zero pid to indicate that we are running IPC. 634 */ 635 636 if (strcmp(m->m_mailer, "[IPC]") == 0) 637 { 638 register int i; 639 640 if (!clever) 641 syserr("non-clever IPC"); 642 if (pvp[2] != NULL) 643 i = atoi(pvp[2]); 644 else 645 i = 0; 646 i = makeconnection(pvp[1], i, pmfile, prfile); 647 if (i != EX_OK) 648 { 649 ExitStat = i; 650 return (-1); 651 } 652 else 653 return (0); 654 } 655 # endif DAEMON 656 657 /* create a pipe to shove the mail through */ 658 if (pipe(mpvect) < 0) 659 { 660 syserr("pipe (to mailer)"); 661 return (-1); 662 } 663 664 # ifdef SMTP 665 /* if this mailer speaks smtp, create a return pipe */ 666 if (clever && pipe(rpvect) < 0) 667 { 668 syserr("pipe (from mailer)"); 669 (void) close(mpvect[0]); 670 (void) close(mpvect[1]); 671 return (-1); 672 } 673 # endif SMTP 674 675 /* 676 ** Actually fork the mailer process. 677 ** DOFORK is clever about retrying. 678 */ 679 680 DOFORK(XFORK); 681 /* pid is set by DOFORK */ 682 if (pid < 0) 683 { 684 /* failure */ 685 syserr("Cannot fork"); 686 (void) close(mpvect[0]); 687 (void) close(mpvect[1]); 688 if (clever) 689 { 690 (void) close(rpvect[0]); 691 (void) close(rpvect[1]); 692 } 693 return (-1); 694 } 695 else if (pid == 0) 696 { 697 /* child -- set up input & exec mailer */ 698 /* make diagnostic output be standard output */ 699 (void) signal(SIGINT, SIG_IGN); 700 (void) signal(SIGHUP, SIG_IGN); 701 (void) signal(SIGTERM, SIG_DFL); 702 703 /* arrange to filter standard & diag output of command */ 704 if (clever) 705 { 706 (void) close(rpvect[0]); 707 (void) close(1); 708 (void) dup(rpvect[1]); 709 (void) close(rpvect[1]); 710 } 711 else if (OutChannel != stdout) 712 { 713 (void) close(1); 714 (void) dup(fileno(OutChannel)); 715 } 716 (void) close(2); 717 (void) dup(1); 718 719 /* arrange to get standard input */ 720 (void) close(mpvect[1]); 721 (void) close(0); 722 if (dup(mpvect[0]) < 0) 723 { 724 syserr("Cannot dup to zero!"); 725 _exit(EX_OSERR); 726 } 727 (void) close(mpvect[0]); 728 if (!bitset(M_RESTR, m->m_flags)) 729 { 730 if (ctladdr->q_uid == 0) 731 { 732 (void) setgid(DefGid); 733 (void) setuid(DefUid); 734 } 735 else 736 { 737 (void) setgid(ctladdr->q_gid); 738 (void) setuid(ctladdr->q_uid); 739 } 740 } 741 # ifndef VFORK 742 /* 743 ** We have to be careful with vfork - we can't mung up the 744 ** memory but we don't want the mailer to inherit any extra 745 ** open files. Chances are the mailer won't 746 ** care about an extra file, but then again you never know. 747 ** Actually, we would like to close(fileno(pwf)), but it's 748 ** declared static so we can't. But if we fclose(pwf), which 749 ** is what endpwent does, it closes it in the parent too and 750 ** the next getpwnam will be slower. If you have a weird 751 ** mailer that chokes on the extra file you should do the 752 ** endpwent(). 753 ** 754 ** Similar comments apply to log. However, openlog is 755 ** clever enough to set the FIOCLEX mode on the file, 756 ** so it will be closed automatically on the exec. 757 */ 758 759 endpwent(); 760 # ifdef LOG 761 closelog(); 762 # endif LOG 763 # endif VFORK 764 765 /* try to execute the mailer */ 766 execv(m->m_mailer, pvp); 767 768 /* syserr fails because log is closed */ 769 /* syserr("Cannot exec %s", m->m_mailer); */ 770 printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 771 (void) fflush(stdout); 772 _exit(EX_UNAVAILABLE); 773 } 774 775 /* 776 ** Set up return value. 777 */ 778 779 (void) close(mpvect[0]); 780 mfile = fdopen(mpvect[1], "w"); 781 if (clever) 782 { 783 (void) close(rpvect[1]); 784 rfile = fdopen(rpvect[0], "r"); 785 } 786 787 *pmfile = mfile; 788 *prfile = rfile; 789 790 return (pid); 791 } 792 /* 793 ** GIVERESPONSE -- Interpret an error response from a mailer 794 ** 795 ** Parameters: 796 ** stat -- the status code from the mailer (high byte 797 ** only; core dumps must have been taken care of 798 ** already). 799 ** force -- if set, force an error message output, even 800 ** if the mailer seems to like to print its own 801 ** messages. 802 ** m -- the mailer descriptor for this mailer. 803 ** 804 ** Returns: 805 ** none. 806 ** 807 ** Side Effects: 808 ** Errors may be incremented. 809 ** ExitStat may be set. 810 */ 811 812 giveresponse(stat, force, m) 813 int stat; 814 int force; 815 register struct mailer *m; 816 { 817 register char *statmsg; 818 extern char *SysExMsg[]; 819 register int i; 820 extern int N_SysEx; 821 char buf[30]; 822 823 /* 824 ** Compute status message from code. 825 */ 826 827 i = stat - EX__BASE; 828 if (i < 0 || i > N_SysEx) 829 statmsg = NULL; 830 else 831 statmsg = SysExMsg[i]; 832 if (stat == 0) 833 { 834 if (bitset(M_LOCAL, m->m_flags)) 835 statmsg = "delivered"; 836 else 837 statmsg = "queued"; 838 if (Verbose) 839 message(Arpa_Info, statmsg); 840 } 841 # ifdef QUEUE 842 else if (stat == EX_TEMPFAIL) 843 { 844 if (Verbose) 845 message(Arpa_Info, "transmission deferred"); 846 } 847 # endif QUEUE 848 else 849 { 850 Errors++; 851 FatalErrors = TRUE; 852 if (statmsg == NULL && m->m_badstat != 0) 853 { 854 stat = m->m_badstat; 855 i = stat - EX__BASE; 856 # ifdef DEBUG 857 if (i < 0 || i >= N_SysEx) 858 syserr("Bad m_badstat %d", stat); 859 else 860 # endif DEBUG 861 statmsg = SysExMsg[i]; 862 } 863 if (statmsg == NULL) 864 usrerr("unknown mailer response %d", stat); 865 else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 866 usrerr("%s", statmsg); 867 } 868 869 /* 870 ** Final cleanup. 871 ** Log a record of the transaction. Compute the new 872 ** ExitStat -- if we already had an error, stick with 873 ** that. 874 */ 875 876 if (statmsg == NULL) 877 { 878 (void) sprintf(buf, "error %d", stat); 879 statmsg = buf; 880 } 881 882 # ifdef LOG 883 syslog(LOG_INFO, "%s->%s: %ld: %s", CurEnv->e_from.q_paddr, CurEnv->e_to, CurEnv->e_msgsize, statmsg); 884 # endif LOG 885 # ifdef QUEUE 886 if (stat != EX_TEMPFAIL) 887 # endif QUEUE 888 setstat(stat); 889 } 890 /* 891 ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 892 ** 893 ** This can be made an arbitrary message separator by changing $l 894 ** 895 ** One of the ugliest hacks seen by human eyes is 896 ** contained herein: UUCP wants those stupid 897 ** "remote from <host>" lines. Why oh why does a 898 ** well-meaning programmer such as myself have to 899 ** deal with this kind of antique garbage???? 900 ** 901 ** Parameters: 902 ** fp -- the file to output to. 903 ** m -- the mailer describing this entry. 904 ** 905 ** Returns: 906 ** none 907 ** 908 ** Side Effects: 909 ** outputs some text to fp. 910 */ 911 912 putfromline(fp, m) 913 register FILE *fp; 914 register MAILER *m; 915 { 916 char buf[MAXLINE]; 917 918 if (bitset(M_NHDR, m->m_flags)) 919 return; 920 921 # ifdef UGLYUUCP 922 if (bitset(M_UGLYUUCP, m->m_flags)) 923 { 924 extern char *macvalue(); 925 char *sys = macvalue('g'); 926 char *bang = index(sys, '!'); 927 928 if (bang == NULL) 929 syserr("No ! in UUCP! (%s)", sys); 930 else 931 *bang = '\0'; 932 expand("From $f $d remote from $g", buf, 933 &buf[sizeof buf - 1], CurEnv); 934 *bang = '!'; 935 } 936 else 937 # endif UGLYUUCP 938 expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv); 939 fputs(buf, fp); 940 } 941 /* 942 ** PUTHEADER -- put the header part of a message from the in-core copy 943 ** 944 ** Parameters: 945 ** fp -- file to put it on. 946 ** m -- mailer to use. 947 ** e -- envelope to use. 948 ** 949 ** Returns: 950 ** none. 951 ** 952 ** Side Effects: 953 ** none. 954 */ 955 956 putheader(fp, m, e) 957 register FILE *fp; 958 register struct mailer *m; 959 register ENVELOPE *e; 960 { 961 char buf[BUFSIZ]; 962 register HDR *h; 963 extern char *arpadate(); 964 extern char *capitalize(); 965 extern char *hvalue(); 966 extern bool samefrom(); 967 char *of_line; 968 969 of_line = hvalue("original-from"); 970 for (h = e->e_header; h != NULL; h = h->h_link) 971 { 972 register char *p; 973 char *origfrom = e->e_origfrom; 974 bool nooutput; 975 976 nooutput = FALSE; 977 if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 978 nooutput = TRUE; 979 980 /* use From: line from message if generated is the same */ 981 if (strcmp(h->h_field, "from") == 0 && origfrom != NULL && 982 strcmp(m->m_from, "$f") == 0 && of_line == NULL) 983 { 984 p = origfrom; 985 origfrom = NULL; 986 } 987 else if (bitset(H_DEFAULT, h->h_flags)) 988 { 989 expand(h->h_value, buf, &buf[sizeof buf], e); 990 p = buf; 991 } 992 else if (bitset(H_ADDR, h->h_flags)) 993 { 994 register int opos; 995 bool firstone = TRUE; 996 997 /* 998 ** Output the address list translated by the 999 ** mailer and with commas. 1000 */ 1001 1002 p = h->h_value; 1003 if (p == NULL || *p == '\0' || nooutput) 1004 continue; 1005 fprintf(fp, "%s: ", capitalize(h->h_field)); 1006 opos = strlen(h->h_field) + 2; 1007 while (*p != '\0') 1008 { 1009 register char *name = p; 1010 extern char *remotename(); 1011 char savechar; 1012 1013 /* find the end of the name */ 1014 while (*p != '\0' && *p != ',') 1015 { 1016 extern bool isatword(); 1017 char *oldp; 1018 1019 if (!e->e_oldstyle || !isspace(*p)) 1020 { 1021 p++; 1022 continue; 1023 } 1024 oldp = p; 1025 while (*p != '\0' && isspace(*p)) 1026 p++; 1027 if (*p != '@' && !isatword(p)) 1028 { 1029 p = oldp; 1030 break; 1031 } 1032 p += *p == '@' ? 1 : 2; 1033 while (*p != '\0' && isspace(*p)) 1034 p++; 1035 } 1036 savechar = *p; 1037 *p = '\0'; 1038 1039 /* translate the name to be relative */ 1040 name = remotename(name, m, FALSE); 1041 if (*name == '\0') 1042 continue; 1043 1044 /* output the name with nice formatting */ 1045 opos += strlen(name); 1046 if (!firstone) 1047 opos += 2; 1048 if (opos > 78 && !firstone) 1049 { 1050 fprintf(fp, ",\n "); 1051 opos = 8 + strlen(name); 1052 } 1053 else if (!firstone) 1054 fprintf(fp, ", "); 1055 fprintf(fp, "%s", name); 1056 firstone = FALSE; 1057 1058 /* clean up the source string */ 1059 *p = savechar; 1060 while (*p != '\0' && (isspace(*p) || *p == ',')) 1061 p++; 1062 } 1063 fprintf(fp, "\n"); 1064 nooutput = TRUE; 1065 } 1066 else 1067 p = h->h_value; 1068 if (p == NULL || *p == '\0') 1069 continue; 1070 1071 /* hack, hack -- output Original-From field if different */ 1072 if (strcmp(h->h_field, "from") == 0 && origfrom != NULL) 1073 { 1074 /* output new Original-From line if needed */ 1075 if (of_line == NULL && !samefrom(p, origfrom)) 1076 fprintf(fp, "Original-From: %s\n", origfrom); 1077 if (of_line != NULL && !nooutput && samefrom(p, of_line)) 1078 { 1079 /* delete Original-From: line if redundant */ 1080 p = of_line; 1081 of_line = NULL; 1082 } 1083 } 1084 else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL) 1085 nooutput = TRUE; 1086 1087 /* finally, output the header line */ 1088 if (!nooutput) 1089 { 1090 fprintf(fp, "%s: %s\n", capitalize(h->h_field), p); 1091 h->h_flags |= H_USED; 1092 } 1093 } 1094 } 1095 /* 1096 ** PUTBODY -- put the body of a message. 1097 ** 1098 ** Parameters: 1099 ** fp -- file to output onto. 1100 ** m -- a mailer descriptor. 1101 ** xdot -- if set, use SMTP hidden dot algorithm. 1102 ** 1103 ** Returns: 1104 ** none. 1105 ** 1106 ** Side Effects: 1107 ** The message is written onto fp. 1108 */ 1109 1110 putbody(fp, m, xdot) 1111 FILE *fp; 1112 struct mailer *m; 1113 bool xdot; 1114 { 1115 char buf[MAXLINE + 1]; 1116 1117 /* 1118 ** Output the body of the message 1119 */ 1120 1121 #ifdef lint 1122 /* m will be needed later for complete smtp emulation */ 1123 if (m == NULL) 1124 return; 1125 #endif lint 1126 1127 if (TempFile != NULL) 1128 { 1129 rewind(TempFile); 1130 buf[0] = '.'; 1131 while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL) 1132 fputs((xdot && buf[1] == '.') ? buf : &buf[1], fp); 1133 1134 if (ferror(TempFile)) 1135 { 1136 syserr("putbody: read error"); 1137 ExitStat = EX_IOERR; 1138 } 1139 } 1140 1141 (void) fflush(fp); 1142 if (ferror(fp) && errno != EPIPE) 1143 { 1144 syserr("putbody: write error"); 1145 ExitStat = EX_IOERR; 1146 } 1147 errno = 0; 1148 } 1149 /* 1150 ** ISATWORD -- tell if the word we are pointing to is "at". 1151 ** 1152 ** Parameters: 1153 ** p -- word to check. 1154 ** 1155 ** Returns: 1156 ** TRUE -- if p is the word at. 1157 ** FALSE -- otherwise. 1158 ** 1159 ** Side Effects: 1160 ** none. 1161 */ 1162 1163 bool 1164 isatword(p) 1165 register char *p; 1166 { 1167 extern char lower(); 1168 1169 if (lower(p[0]) == 'a' && lower(p[1]) == 't' && 1170 p[2] != '\0' && isspace(p[2])) 1171 return (TRUE); 1172 return (FALSE); 1173 } 1174 /* 1175 ** REMOTENAME -- return the name relative to the current mailer 1176 ** 1177 ** Parameters: 1178 ** name -- the name to translate. 1179 ** force -- if set, forces rewriting even if the mailer 1180 ** does not request it. Used for rewriting 1181 ** sender addresses. 1182 ** 1183 ** Returns: 1184 ** the text string representing this address relative to 1185 ** the receiving mailer. 1186 ** 1187 ** Side Effects: 1188 ** none. 1189 ** 1190 ** Warnings: 1191 ** The text string returned is tucked away locally; 1192 ** copy it if you intend to save it. 1193 */ 1194 1195 char * 1196 remotename(name, m, force) 1197 char *name; 1198 struct mailer *m; 1199 bool force; 1200 { 1201 static char buf[MAXNAME]; 1202 char lbuf[MAXNAME]; 1203 extern char *macvalue(); 1204 char *oldf = macvalue('f'); 1205 char *oldx = macvalue('x'); 1206 char *oldg = macvalue('g'); 1207 extern char **prescan(); 1208 register char **pvp; 1209 extern char *getxpart(); 1210 extern ADDRESS *buildaddr(); 1211 1212 /* 1213 ** See if this mailer wants the name to be rewritten. There are 1214 ** many problems here, owing to the standards for doing replies. 1215 ** In general, these names should only be rewritten if we are 1216 ** sending to another host that runs sendmail. 1217 */ 1218 1219 if (!bitset(M_RELRCPT, m->m_flags) && !force) 1220 return (name); 1221 1222 /* 1223 ** Do general rewriting of name. 1224 ** This will also take care of doing global name translation. 1225 */ 1226 1227 define('x', getxpart(name)); 1228 pvp = prescan(name, '\0'); 1229 for (;;) 1230 { 1231 rewrite(pvp, 1); 1232 rewrite(pvp, 3); 1233 if (**pvp == CANONNET) 1234 { 1235 auto ADDRESS a; 1236 register char *p; 1237 extern char *hostalias(); 1238 1239 /* oops... resolved to something */ 1240 if (buildaddr(pvp, &a) == NULL) 1241 return (name); 1242 p = hostalias(&a); 1243 if (p == NULL) 1244 return (name); 1245 pvp = prescan(p, '\0'); 1246 } 1247 else 1248 { 1249 cataddr(pvp, lbuf, sizeof lbuf); 1250 break; 1251 } 1252 } 1253 1254 /* make the name relative to the receiving mailer */ 1255 define('f', lbuf); 1256 expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv); 1257 1258 /* rewrite to get rid of garbage we added in the expand above */ 1259 pvp = prescan(buf, '\0'); 1260 rewrite(pvp, 2); 1261 cataddr(pvp, lbuf, sizeof lbuf); 1262 1263 /* now add any comment info we had before back */ 1264 define('g', lbuf); 1265 expand("$q", buf, &buf[sizeof buf - 1], CurEnv); 1266 1267 define('f', oldf); 1268 define('g', oldg); 1269 define('x', oldx); 1270 1271 # ifdef DEBUG 1272 if (Debug > 0) 1273 printf("remotename(%s) => `%s'\n", name, buf); 1274 # endif DEBUG 1275 return (buf); 1276 } 1277 /* 1278 ** SAMEFROM -- tell if two text addresses represent the same from address. 1279 ** 1280 ** Parameters: 1281 ** ifrom -- internally generated form of from address. 1282 ** efrom -- external form of from address. 1283 ** 1284 ** Returns: 1285 ** TRUE -- if they convey the same info. 1286 ** FALSE -- if any information has been lost. 1287 ** 1288 ** Side Effects: 1289 ** none. 1290 */ 1291 1292 bool 1293 samefrom(ifrom, efrom) 1294 char *ifrom; 1295 char *efrom; 1296 { 1297 register char *p; 1298 char buf[MAXNAME + 4]; 1299 1300 # ifdef DEBUG 1301 if (Debug > 7) 1302 printf("samefrom(%s,%s)-->", ifrom, efrom); 1303 # endif DEBUG 1304 if (strcmp(ifrom, efrom) == 0) 1305 goto success; 1306 p = index(ifrom, '@'); 1307 if (p == NULL) 1308 goto failure; 1309 *p = '\0'; 1310 (void) strcpy(buf, ifrom); 1311 (void) strcat(buf, " at "); 1312 *p++ = '@'; 1313 (void) strcat(buf, p); 1314 if (strcmp(buf, efrom) == 0) 1315 goto success; 1316 1317 failure: 1318 # ifdef DEBUG 1319 if (Debug > 7) 1320 printf("FALSE\n"); 1321 # endif DEBUG 1322 return (FALSE); 1323 1324 success: 1325 # ifdef DEBUG 1326 if (Debug > 7) 1327 printf("TRUE\n"); 1328 # endif DEBUG 1329 return (TRUE); 1330 } 1331 /* 1332 ** MAILFILE -- Send a message to a file. 1333 ** 1334 ** If the file has the setuid/setgid bits set, but NO execute 1335 ** bits, sendmail will try to become the owner of that file 1336 ** rather than the real user. Obviously, this only works if 1337 ** sendmail runs as root. 1338 ** 1339 ** Parameters: 1340 ** filename -- the name of the file to send to. 1341 ** ctladdr -- the controlling address header -- includes 1342 ** the userid/groupid to be when sending. 1343 ** 1344 ** Returns: 1345 ** The exit code associated with the operation. 1346 ** 1347 ** Side Effects: 1348 ** none. 1349 */ 1350 1351 mailfile(filename, ctladdr) 1352 char *filename; 1353 ADDRESS *ctladdr; 1354 { 1355 register FILE *f; 1356 register int pid; 1357 1358 /* 1359 ** Fork so we can change permissions here. 1360 ** Note that we MUST use fork, not vfork, because of 1361 ** the complications of calling subroutines, etc. 1362 */ 1363 1364 DOFORK(fork); 1365 1366 if (pid < 0) 1367 return (EX_OSERR); 1368 else if (pid == 0) 1369 { 1370 /* child -- actually write to file */ 1371 struct stat stb; 1372 1373 (void) signal(SIGINT, SIG_DFL); 1374 (void) signal(SIGHUP, SIG_DFL); 1375 (void) signal(SIGTERM, SIG_DFL); 1376 umask(OldUmask); 1377 if (stat(filename, &stb) < 0) 1378 stb.st_mode = 0666; 1379 if (bitset(0111, stb.st_mode)) 1380 exit(EX_CANTCREAT); 1381 if (ctladdr == NULL) 1382 ctladdr = &CurEnv->e_from; 1383 if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 1384 { 1385 if (ctladdr->q_uid == 0) 1386 (void) setgid(DefGid); 1387 else 1388 (void) setgid(ctladdr->q_gid); 1389 } 1390 if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 1391 { 1392 if (ctladdr->q_uid == 0) 1393 (void) setuid(DefUid); 1394 else 1395 (void) setuid(ctladdr->q_uid); 1396 } 1397 f = dfopen(filename, "a"); 1398 if (f == NULL) 1399 exit(EX_CANTCREAT); 1400 1401 putfromline(f, Mailer[1]); 1402 (*CurEnv->e_puthdr)(f, Mailer[1], CurEnv); 1403 fprintf(f, "\n"); 1404 (*CurEnv->e_putbody)(f, Mailer[1], FALSE); 1405 fputs("\n", f); 1406 (void) fclose(f); 1407 (void) fflush(stdout); 1408 1409 /* reset ISUID & ISGID bits for paranoid systems */ 1410 (void) chmod(filename, (int) stb.st_mode); 1411 exit(EX_OK); 1412 /*NOTREACHED*/ 1413 } 1414 else 1415 { 1416 /* parent -- wait for exit status */ 1417 register int i; 1418 auto int stat; 1419 1420 while ((i = wait(&stat)) != pid) 1421 { 1422 if (i < 0) 1423 { 1424 stat = EX_OSERR << 8; 1425 break; 1426 } 1427 } 1428 if ((stat & 0377) != 0) 1429 stat = EX_UNAVAILABLE << 8; 1430 return ((stat >> 8) & 0377); 1431 } 1432 } 1433 /* 1434 ** SENDALL -- actually send all the messages. 1435 ** 1436 ** Parameters: 1437 ** verifyonly -- if set, only give verification messages. 1438 ** 1439 ** Returns: 1440 ** none. 1441 ** 1442 ** Side Effects: 1443 ** Scans the send lists and sends everything it finds. 1444 */ 1445 1446 sendall(verifyonly) 1447 bool verifyonly; 1448 { 1449 register ADDRESS *q; 1450 typedef int (*fnptr)(); 1451 1452 # ifdef DEBUG 1453 if (Debug > 1) 1454 { 1455 printf("\nSend Queue:\n"); 1456 printaddr(CurEnv->e_sendqueue, TRUE); 1457 } 1458 # endif DEBUG 1459 1460 for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next) 1461 { 1462 if (verifyonly) 1463 { 1464 CurEnv->e_to = q->q_paddr; 1465 if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1466 { 1467 if (bitset(M_LOCAL, q->q_mailer->m_flags)) 1468 message(Arpa_Info, "deliverable"); 1469 else 1470 message(Arpa_Info, "queueable"); 1471 } 1472 } 1473 else 1474 (void) deliver(q); 1475 } 1476 } 1477