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