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