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.79.1.1 05/29/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 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 return (0); 354 } 355 356 /* print out messages as full list */ 357 CurEnv->e_to = tobuf; 358 359 /* 360 ** Fill out any parameters after the $u parameter. 361 */ 362 363 while (!clever && *++mvp != NULL) 364 { 365 expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv); 366 *pvp++ = newstr(buf); 367 if (pvp >= &pv[MAXPV]) 368 syserr("deliver: pv overflow after $u for %s", pv[0]); 369 } 370 *pvp++ = NULL; 371 372 /* 373 ** Call the mailer. 374 ** The argument vector gets built, pipes 375 ** are created as necessary, and we fork & exec as 376 ** appropriate. 377 ** If we are running SMTP, we just need to clean up. 378 */ 379 380 if (ctladdr == NULL) 381 ctladdr = &CurEnv->e_from; 382 # ifdef SMTP 383 if (clever) 384 { 385 i = smtpfinish(m, CurEnv); 386 smtpquit(pv[0]); 387 } 388 else 389 # endif SMTP 390 i = sendoff(m, pv, ctladdr); 391 392 /* 393 ** If we got a temporary failure, arrange to queue the 394 ** addressees. 395 */ 396 397 # ifdef QUEUE 398 if (i == EX_TEMPFAIL) 399 { 400 CurEnv->e_queueup = TRUE; 401 for (to = tochain; to != NULL; to = to->q_tchain) 402 to->q_flags |= QQUEUEUP; 403 } 404 # endif QUEUE 405 406 errno = 0; 407 return (i); 408 } 409 /* 410 ** DOFORK -- do a fork, retrying a couple of times on failure. 411 ** 412 ** This MUST be a macro, since after a vfork we are running 413 ** two processes on the same stack!!! 414 ** 415 ** Parameters: 416 ** none. 417 ** 418 ** Returns: 419 ** From a macro??? You've got to be kidding! 420 ** 421 ** Side Effects: 422 ** Modifies the ==> LOCAL <== variable 'pid', leaving: 423 ** pid of child in parent, zero in child. 424 ** -1 on unrecoverable error. 425 ** 426 ** Notes: 427 ** I'm awfully sorry this looks so awful. That's 428 ** vfork for you..... 429 */ 430 431 # define NFORKTRIES 5 432 # ifdef VFORK 433 # define XFORK vfork 434 # else VFORK 435 # define XFORK fork 436 # endif VFORK 437 438 # define DOFORK(fORKfN) \ 439 {\ 440 register int i;\ 441 \ 442 for (i = NFORKTRIES; i-- > 0; )\ 443 {\ 444 pid = fORKfN();\ 445 if (pid >= 0)\ 446 break;\ 447 sleep((unsigned) NFORKTRIES - i);\ 448 }\ 449 } 450 /* 451 ** DOFORK -- simple fork interface to DOFORK. 452 ** 453 ** Parameters: 454 ** none. 455 ** 456 ** Returns: 457 ** pid of child in parent. 458 ** zero in child. 459 ** -1 on error. 460 ** 461 ** Side Effects: 462 ** returns twice, once in parent and once in child. 463 */ 464 465 dofork() 466 { 467 register int pid; 468 469 DOFORK(fork); 470 return (pid); 471 } 472 /* 473 ** SENDOFF -- send off call to mailer & collect response. 474 ** 475 ** Parameters: 476 ** m -- mailer descriptor. 477 ** pvp -- parameter vector to send to it. 478 ** ctladdr -- an address pointer controlling the 479 ** user/groupid etc. of the mailer. 480 ** 481 ** Returns: 482 ** exit status of mailer. 483 ** 484 ** Side Effects: 485 ** none. 486 */ 487 488 sendoff(m, pvp, ctladdr) 489 struct mailer *m; 490 char **pvp; 491 ADDRESS *ctladdr; 492 { 493 auto FILE *mfile; 494 auto FILE *rfile; 495 register int i; 496 int pid; 497 498 /* 499 ** Create connection to mailer. 500 */ 501 502 pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile); 503 if (pid < 0) 504 return (-1); 505 506 /* 507 ** Format and send message. 508 */ 509 510 (void) signal(SIGPIPE, SIG_IGN); 511 putfromline(mfile, m); 512 (*CurEnv->e_puthdr)(mfile, m, CurEnv); 513 fprintf(mfile, "\n"); 514 (*CurEnv->e_putbody)(mfile, m, FALSE); 515 (void) fclose(mfile); 516 517 i = endmailer(pid, pvp[0]); 518 giveresponse(i, TRUE, m); 519 520 /* arrange a return receipt if requested */ 521 if (CurEnv->e_retreceipt && bitset(M_LOCAL, m->m_flags) && i == EX_OK) 522 { 523 CurEnv->e_sendreceipt = TRUE; 524 fprintf(Xscript, "%s... successfully delivered\n", CurEnv->e_to); 525 /* do we want to send back more info? */ 526 } 527 528 return (i); 529 } 530 /* 531 ** ENDMAILER -- Wait for mailer to terminate. 532 ** 533 ** We should never get fatal errors (e.g., segmentation 534 ** violation), so we report those specially. For other 535 ** errors, we choose a status message (into statmsg), 536 ** and if it represents an error, we print it. 537 ** 538 ** Parameters: 539 ** pid -- pid of mailer. 540 ** name -- name of mailer (for error messages). 541 ** 542 ** Returns: 543 ** exit code of mailer. 544 ** 545 ** Side Effects: 546 ** none. 547 */ 548 549 endmailer(pid, name) 550 int pid; 551 char *name; 552 { 553 register int i; 554 auto int st; 555 556 /* in the IPC case there is nothing to wait for */ 557 if (pid == 0) 558 return (EX_OK); 559 560 /* wait for the mailer process to die and collect status */ 561 while ((i = wait(&st)) > 0 && i != pid) 562 continue; 563 if (i < 0) 564 { 565 syserr("wait"); 566 return (-1); 567 } 568 569 /* see if it died a horrid death */ 570 if ((st & 0377) != 0) 571 { 572 syserr("%s: stat %o", name, st); 573 ExitStat = EX_UNAVAILABLE; 574 return (-1); 575 } 576 577 /* normal death -- return status */ 578 i = (st >> 8) & 0377; 579 return (i); 580 } 581 /* 582 ** OPENMAILER -- open connection to mailer. 583 ** 584 ** Parameters: 585 ** m -- mailer descriptor. 586 ** pvp -- parameter vector to pass to mailer. 587 ** ctladdr -- controlling address for user. 588 ** clever -- create a full duplex connection. 589 ** pmfile -- pointer to mfile (to mailer) connection. 590 ** prfile -- pointer to rfile (from mailer) connection. 591 ** 592 ** Returns: 593 ** pid of mailer ( > 0 ). 594 ** -1 on error. 595 ** zero on an IPC connection. 596 ** 597 ** Side Effects: 598 ** creates a mailer in a subprocess. 599 */ 600 601 openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 602 struct mailer *m; 603 char **pvp; 604 ADDRESS *ctladdr; 605 bool clever; 606 FILE **pmfile; 607 FILE **prfile; 608 { 609 int pid; 610 int mpvect[2]; 611 int rpvect[2]; 612 FILE *mfile; 613 FILE *rfile; 614 extern FILE *fdopen(); 615 616 # ifdef DEBUG 617 if (Debug) 618 { 619 printf("openmailer:\n"); 620 printav(pvp); 621 } 622 # endif DEBUG 623 errno = 0; 624 625 # ifdef DAEMON 626 /* 627 ** Deal with the special case of mail handled through an IPC 628 ** connection. 629 ** In this case we don't actually fork. We must be 630 ** running SMTP for this to work. We will return a 631 ** zero pid to indicate that we are running IPC. 632 */ 633 634 if (strcmp(m->m_mailer, "[IPC]") == 0) 635 { 636 register int i; 637 638 if (!clever) 639 syserr("non-clever IPC"); 640 if (pvp[2] != NULL) 641 i = atoi(pvp[2]); 642 else 643 i = 0; 644 i = makeconnection(pvp[1], i, pmfile, prfile); 645 if (i != EX_OK) 646 { 647 ExitStat = i; 648 return (-1); 649 } 650 else 651 return (0); 652 } 653 # endif DAEMON 654 655 /* create a pipe to shove the mail through */ 656 if (pipe(mpvect) < 0) 657 { 658 syserr("pipe (to mailer)"); 659 return (-1); 660 } 661 662 # ifdef SMTP 663 /* if this mailer speaks smtp, create a return pipe */ 664 if (clever && pipe(rpvect) < 0) 665 { 666 syserr("pipe (from mailer)"); 667 (void) close(mpvect[0]); 668 (void) close(mpvect[1]); 669 return (-1); 670 } 671 # endif SMTP 672 673 /* 674 ** Actually fork the mailer process. 675 ** DOFORK is clever about retrying. 676 */ 677 678 DOFORK(XFORK); 679 /* pid is set by DOFORK */ 680 if (pid < 0) 681 { 682 /* failure */ 683 syserr("Cannot fork"); 684 (void) close(mpvect[0]); 685 (void) close(mpvect[1]); 686 if (clever) 687 { 688 (void) close(rpvect[0]); 689 (void) close(rpvect[1]); 690 } 691 return (-1); 692 } 693 else if (pid == 0) 694 { 695 /* child -- set up input & exec mailer */ 696 /* make diagnostic output be standard output */ 697 (void) signal(SIGINT, SIG_IGN); 698 (void) signal(SIGHUP, SIG_IGN); 699 (void) signal(SIGTERM, SIG_DFL); 700 701 /* arrange to filter standard & diag output of command */ 702 if (clever) 703 { 704 (void) close(rpvect[0]); 705 (void) close(1); 706 (void) dup(rpvect[1]); 707 (void) close(rpvect[1]); 708 } 709 else if (OutChannel != stdout) 710 { 711 (void) close(1); 712 (void) dup(fileno(OutChannel)); 713 } 714 (void) close(2); 715 (void) dup(1); 716 717 /* arrange to get standard input */ 718 (void) close(mpvect[1]); 719 (void) close(0); 720 if (dup(mpvect[0]) < 0) 721 { 722 syserr("Cannot dup to zero!"); 723 _exit(EX_OSERR); 724 } 725 (void) close(mpvect[0]); 726 if (!bitset(M_RESTR, m->m_flags)) 727 { 728 if (ctladdr->q_uid == 0) 729 { 730 (void) setgid(DefGid); 731 (void) setuid(DefUid); 732 } 733 else 734 { 735 (void) setgid(ctladdr->q_gid); 736 (void) setuid(ctladdr->q_uid); 737 } 738 } 739 # ifndef VFORK 740 /* 741 ** We have to be careful with vfork - we can't mung up the 742 ** memory but we don't want the mailer to inherit any extra 743 ** open files. Chances are the mailer won't 744 ** care about an extra file, but then again you never know. 745 ** Actually, we would like to close(fileno(pwf)), but it's 746 ** declared static so we can't. But if we fclose(pwf), which 747 ** is what endpwent does, it closes it in the parent too and 748 ** the next getpwnam will be slower. If you have a weird 749 ** mailer that chokes on the extra file you should do the 750 ** endpwent(). 751 ** 752 ** Similar comments apply to log. However, openlog is 753 ** clever enough to set the FIOCLEX mode on the file, 754 ** so it will be closed automatically on the exec. 755 */ 756 757 endpwent(); 758 # ifdef LOG 759 closelog(); 760 # endif LOG 761 # endif VFORK 762 763 /* try to execute the mailer */ 764 execv(m->m_mailer, pvp); 765 766 /* syserr fails because log is closed */ 767 /* syserr("Cannot exec %s", m->m_mailer); */ 768 printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 769 (void) fflush(stdout); 770 _exit(EX_UNAVAILABLE); 771 } 772 773 /* 774 ** Set up return value. 775 */ 776 777 (void) close(mpvect[0]); 778 mfile = fdopen(mpvect[1], "w"); 779 if (clever) 780 { 781 (void) close(rpvect[1]); 782 rfile = fdopen(rpvect[0], "r"); 783 } 784 785 *pmfile = mfile; 786 *prfile = rfile; 787 788 return (pid); 789 } 790 /* 791 ** GIVERESPONSE -- Interpret an error response from a mailer 792 ** 793 ** Parameters: 794 ** stat -- the status code from the mailer (high byte 795 ** only; core dumps must have been taken care of 796 ** already). 797 ** force -- if set, force an error message output, even 798 ** if the mailer seems to like to print its own 799 ** messages. 800 ** m -- the mailer descriptor for this mailer. 801 ** 802 ** Returns: 803 ** none. 804 ** 805 ** Side Effects: 806 ** Errors may be incremented. 807 ** ExitStat may be set. 808 */ 809 810 giveresponse(stat, force, m) 811 int stat; 812 int force; 813 register struct mailer *m; 814 { 815 register char *statmsg; 816 extern char *SysExMsg[]; 817 register int i; 818 extern int N_SysEx; 819 char buf[30]; 820 821 /* 822 ** Compute status message from code. 823 */ 824 825 i = stat - EX__BASE; 826 if (i < 0 || i > N_SysEx) 827 statmsg = NULL; 828 else 829 statmsg = SysExMsg[i]; 830 if (stat == 0) 831 { 832 if (bitset(M_LOCAL, m->m_flags)) 833 statmsg = "delivered"; 834 else 835 statmsg = "queued"; 836 if (Verbose) 837 message(Arpa_Info, statmsg); 838 } 839 # ifdef QUEUE 840 else if (stat == EX_TEMPFAIL) 841 { 842 if (Verbose) 843 message(Arpa_Info, "transmission deferred"); 844 } 845 # endif QUEUE 846 else 847 { 848 Errors++; 849 FatalErrors = TRUE; 850 if (statmsg == NULL && m->m_badstat != 0) 851 { 852 stat = m->m_badstat; 853 i = stat - EX__BASE; 854 # ifdef DEBUG 855 if (i < 0 || i >= N_SysEx) 856 syserr("Bad m_badstat %d", stat); 857 else 858 # endif DEBUG 859 statmsg = SysExMsg[i]; 860 } 861 if (statmsg == NULL) 862 usrerr("unknown mailer response %d", stat); 863 else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 864 usrerr("%s", statmsg); 865 } 866 867 /* 868 ** Final cleanup. 869 ** Log a record of the transaction. Compute the new 870 ** ExitStat -- if we already had an error, stick with 871 ** that. 872 */ 873 874 if (statmsg == NULL) 875 { 876 (void) sprintf(buf, "error %d", stat); 877 statmsg = buf; 878 } 879 880 # ifdef LOG 881 syslog(LOG_INFO, "%s->%s: %ld: %s", CurEnv->e_from.q_paddr, CurEnv->e_to, CurEnv->e_msgsize, statmsg); 882 # endif LOG 883 # ifdef QUEUE 884 if (stat != EX_TEMPFAIL) 885 # endif QUEUE 886 setstat(stat); 887 } 888 /* 889 ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 890 ** 891 ** This can be made an arbitrary message separator by changing $l 892 ** 893 ** One of the ugliest hacks seen by human eyes is 894 ** contained herein: UUCP wants those stupid 895 ** "remote from <host>" lines. Why oh why does a 896 ** well-meaning programmer such as myself have to 897 ** deal with this kind of antique garbage???? 898 ** 899 ** Parameters: 900 ** fp -- the file to output to. 901 ** m -- the mailer describing this entry. 902 ** 903 ** Returns: 904 ** none 905 ** 906 ** Side Effects: 907 ** outputs some text to fp. 908 */ 909 910 putfromline(fp, m) 911 register FILE *fp; 912 register MAILER *m; 913 { 914 char buf[MAXLINE]; 915 916 if (bitset(M_NHDR, m->m_flags)) 917 return; 918 919 # ifdef UGLYUUCP 920 if (bitset(M_UGLYUUCP, m->m_flags)) 921 { 922 extern char *macvalue(); 923 char *sys = macvalue('g'); 924 char *bang = index(sys, '!'); 925 926 if (bang == NULL) 927 syserr("No ! in UUCP! (%s)", sys); 928 else 929 *bang = '\0'; 930 expand("From $f $d remote from $g", buf, 931 &buf[sizeof buf - 1], CurEnv); 932 *bang = '!'; 933 } 934 else 935 # endif UGLYUUCP 936 expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv); 937 fputs(buf, fp); 938 } 939 /* 940 ** PUTHEADER -- put the header part of a message from the in-core copy 941 ** 942 ** Parameters: 943 ** fp -- file to put it on. 944 ** m -- mailer to use. 945 ** e -- envelope to use. 946 ** 947 ** Returns: 948 ** none. 949 ** 950 ** Side Effects: 951 ** none. 952 */ 953 954 putheader(fp, m, e) 955 register FILE *fp; 956 register struct mailer *m; 957 register ENVELOPE *e; 958 { 959 char buf[BUFSIZ]; 960 register HDR *h; 961 extern char *arpadate(); 962 extern char *capitalize(); 963 extern char *hvalue(); 964 extern bool samefrom(); 965 char *of_line; 966 967 of_line = hvalue("original-from"); 968 for (h = e->e_header; h != NULL; h = h->h_link) 969 { 970 register char *p; 971 char *origfrom = e->e_origfrom; 972 bool nooutput; 973 974 nooutput = FALSE; 975 if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 976 nooutput = TRUE; 977 978 /* use From: line from message if generated is the same */ 979 if (strcmp(h->h_field, "from") == 0 && origfrom != NULL && 980 strcmp(m->m_from, "$f") == 0 && of_line == NULL) 981 { 982 p = origfrom; 983 origfrom = NULL; 984 } 985 else if (bitset(H_DEFAULT, h->h_flags)) 986 { 987 expand(h->h_value, buf, &buf[sizeof buf], e); 988 p = buf; 989 } 990 else if (bitset(H_ADDR, h->h_flags)) 991 { 992 register int opos; 993 bool firstone = TRUE; 994 995 /* 996 ** Output the address list translated by the 997 ** mailer and with commas. 998 */ 999 1000 p = h->h_value; 1001 if (p == NULL || *p == '\0' || nooutput) 1002 continue; 1003 fprintf(fp, "%s: ", capitalize(h->h_field)); 1004 opos = strlen(h->h_field) + 2; 1005 while (*p != '\0') 1006 { 1007 register char *name = p; 1008 extern char *remotename(); 1009 char savechar; 1010 1011 /* find the end of the name */ 1012 while (*p != '\0' && *p != ',') 1013 { 1014 extern bool isatword(); 1015 char *oldp; 1016 1017 if (!e->e_oldstyle || !isspace(*p)) 1018 { 1019 p++; 1020 continue; 1021 } 1022 oldp = p; 1023 while (*p != '\0' && isspace(*p)) 1024 p++; 1025 if (*p != '@' && !isatword(p)) 1026 { 1027 p = oldp; 1028 break; 1029 } 1030 p += *p == '@' ? 1 : 2; 1031 while (*p != '\0' && isspace(*p)) 1032 p++; 1033 } 1034 savechar = *p; 1035 *p = '\0'; 1036 1037 /* translate the name to be relative */ 1038 name = remotename(name, m, FALSE); 1039 if (*name == '\0') 1040 continue; 1041 1042 /* output the name with nice formatting */ 1043 opos += strlen(name); 1044 if (!firstone) 1045 opos += 2; 1046 if (opos > 78 && !firstone) 1047 { 1048 fprintf(fp, ",\n "); 1049 opos = 8 + strlen(name); 1050 } 1051 else if (!firstone) 1052 fprintf(fp, ", "); 1053 fprintf(fp, "%s", name); 1054 firstone = FALSE; 1055 1056 /* clean up the source string */ 1057 *p = savechar; 1058 while (*p != '\0' && (isspace(*p) || *p == ',')) 1059 p++; 1060 } 1061 fprintf(fp, "\n"); 1062 nooutput = TRUE; 1063 } 1064 else 1065 p = h->h_value; 1066 if (p == NULL || *p == '\0') 1067 continue; 1068 1069 /* hack, hack -- output Original-From field if different */ 1070 if (strcmp(h->h_field, "from") == 0 && origfrom != NULL) 1071 { 1072 /* output new Original-From line if needed */ 1073 if (of_line == NULL && !samefrom(p, origfrom)) 1074 fprintf(fp, "Original-From: %s\n", origfrom); 1075 if (of_line != NULL && !nooutput && samefrom(p, of_line)) 1076 { 1077 /* delete Original-From: line if redundant */ 1078 p = of_line; 1079 of_line = NULL; 1080 } 1081 } 1082 else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL) 1083 nooutput = TRUE; 1084 1085 /* finally, output the header line */ 1086 if (!nooutput) 1087 { 1088 fprintf(fp, "%s: %s\n", capitalize(h->h_field), p); 1089 h->h_flags |= H_USED; 1090 } 1091 } 1092 } 1093 /* 1094 ** PUTBODY -- put the body of a message. 1095 ** 1096 ** Parameters: 1097 ** fp -- file to output onto. 1098 ** m -- a mailer descriptor. 1099 ** xdot -- if set, use SMTP hidden dot algorithm. 1100 ** 1101 ** Returns: 1102 ** none. 1103 ** 1104 ** Side Effects: 1105 ** The message is written onto fp. 1106 */ 1107 1108 putbody(fp, m, xdot) 1109 FILE *fp; 1110 struct mailer *m; 1111 bool xdot; 1112 { 1113 char buf[MAXLINE + 1]; 1114 1115 /* 1116 ** Output the body of the message 1117 */ 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 1203 /* 1204 ** See if this mailer wants the name to be rewritten. There are 1205 ** many problems here, owing to the standards for doing replies. 1206 ** In general, these names should only be rewritten if we are 1207 ** sending to another host that runs sendmail. 1208 */ 1209 1210 if (!bitset(M_RELRCPT, m->m_flags) && !force) 1211 return; 1212 1213 /* 1214 ** Do general rewriting of name. 1215 ** This will also take care of doing global name translation. 1216 */ 1217 1218 define('x', getxpart(name)); 1219 pvp = prescan(name, '\0'); 1220 for (;;) 1221 { 1222 rewrite(pvp, 1); 1223 rewrite(pvp, 3); 1224 if (**pvp == CANONNET) 1225 { 1226 auto ADDRESS a; 1227 register char *p; 1228 extern char *hostalias(); 1229 1230 /* oops... resolved to something */ 1231 if (buildaddr(pvp, &a) == NULL) 1232 return (name); 1233 p = hostalias(&a); 1234 if (p == NULL) 1235 return (name); 1236 pvp = prescan(p, '\0'); 1237 } 1238 else 1239 { 1240 cataddr(pvp, lbuf, sizeof lbuf); 1241 break; 1242 } 1243 } 1244 1245 /* make the name relative to the receiving mailer */ 1246 define('f', lbuf); 1247 expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv); 1248 1249 /* rewrite to get rid of garbage we added in the expand above */ 1250 pvp = prescan(buf, '\0'); 1251 rewrite(pvp, 2); 1252 cataddr(pvp, lbuf, sizeof lbuf); 1253 1254 /* now add any comment info we had before back */ 1255 define('g', lbuf); 1256 expand("$q", buf, &buf[sizeof buf - 1], CurEnv); 1257 1258 define('f', oldf); 1259 define('g', oldg); 1260 define('x', oldx); 1261 1262 # ifdef DEBUG 1263 if (Debug > 0) 1264 printf("remotename(%s) => `%s'\n", name, buf); 1265 # endif DEBUG 1266 return (buf); 1267 } 1268 /* 1269 ** SAMEFROM -- tell if two text addresses represent the same from address. 1270 ** 1271 ** Parameters: 1272 ** ifrom -- internally generated form of from address. 1273 ** efrom -- external form of from address. 1274 ** 1275 ** Returns: 1276 ** TRUE -- if they convey the same info. 1277 ** FALSE -- if any information has been lost. 1278 ** 1279 ** Side Effects: 1280 ** none. 1281 */ 1282 1283 bool 1284 samefrom(ifrom, efrom) 1285 char *ifrom; 1286 char *efrom; 1287 { 1288 register char *p; 1289 char buf[MAXNAME + 4]; 1290 1291 # ifdef DEBUG 1292 if (Debug > 7) 1293 printf("samefrom(%s,%s)-->", ifrom, efrom); 1294 # endif DEBUG 1295 if (strcmp(ifrom, efrom) == 0) 1296 goto success; 1297 p = index(ifrom, '@'); 1298 if (p == NULL) 1299 goto failure; 1300 *p = '\0'; 1301 strcpy(buf, ifrom); 1302 strcat(buf, " at "); 1303 *p++ = '@'; 1304 strcat(buf, p); 1305 if (strcmp(buf, efrom) == 0) 1306 goto success; 1307 1308 failure: 1309 # ifdef DEBUG 1310 if (Debug > 7) 1311 printf("FALSE\n"); 1312 # endif DEBUG 1313 return (FALSE); 1314 1315 success: 1316 # ifdef DEBUG 1317 if (Debug > 7) 1318 printf("TRUE\n"); 1319 # endif DEBUG 1320 return (TRUE); 1321 } 1322 /* 1323 ** MAILFILE -- Send a message to a file. 1324 ** 1325 ** If the file has the setuid/setgid bits set, but NO execute 1326 ** bits, sendmail will try to become the owner of that file 1327 ** rather than the real user. Obviously, this only works if 1328 ** sendmail runs as root. 1329 ** 1330 ** Parameters: 1331 ** filename -- the name of the file to send to. 1332 ** ctladdr -- the controlling address header -- includes 1333 ** the userid/groupid to be when sending. 1334 ** 1335 ** Returns: 1336 ** The exit code associated with the operation. 1337 ** 1338 ** Side Effects: 1339 ** none. 1340 */ 1341 1342 mailfile(filename, ctladdr) 1343 char *filename; 1344 ADDRESS *ctladdr; 1345 { 1346 register FILE *f; 1347 register int pid; 1348 1349 /* 1350 ** Fork so we can change permissions here. 1351 ** Note that we MUST use fork, not vfork, because of 1352 ** the complications of calling subroutines, etc. 1353 */ 1354 1355 DOFORK(fork); 1356 1357 if (pid < 0) 1358 return (EX_OSERR); 1359 else if (pid == 0) 1360 { 1361 /* child -- actually write to file */ 1362 struct stat stb; 1363 1364 (void) signal(SIGINT, SIG_DFL); 1365 (void) signal(SIGHUP, SIG_DFL); 1366 (void) signal(SIGTERM, SIG_DFL); 1367 umask(OldUmask); 1368 if (stat(filename, &stb) < 0) 1369 stb.st_mode = 0666; 1370 if (bitset(0111, stb.st_mode)) 1371 exit(EX_CANTCREAT); 1372 if (ctladdr == NULL) 1373 ctladdr = &CurEnv->e_from; 1374 if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 1375 { 1376 if (ctladdr->q_uid == 0) 1377 (void) setgid(DefGid); 1378 else 1379 (void) setgid(ctladdr->q_gid); 1380 } 1381 if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 1382 { 1383 if (ctladdr->q_uid == 0) 1384 (void) setuid(DefUid); 1385 else 1386 (void) setuid(ctladdr->q_uid); 1387 } 1388 f = dfopen(filename, "a"); 1389 if (f == NULL) 1390 exit(EX_CANTCREAT); 1391 1392 putfromline(f, Mailer[1]); 1393 (*CurEnv->e_puthdr)(f, Mailer[1], CurEnv); 1394 fprintf(f, "\n"); 1395 (*CurEnv->e_putbody)(f, Mailer[1], FALSE); 1396 fputs("\n", f); 1397 (void) fclose(f); 1398 (void) fflush(stdout); 1399 1400 /* reset ISUID & ISGID bits for paranoid systems */ 1401 (void) chmod(filename, (int) stb.st_mode); 1402 exit(EX_OK); 1403 /*NOTREACHED*/ 1404 } 1405 else 1406 { 1407 /* parent -- wait for exit status */ 1408 register int i; 1409 auto int stat; 1410 1411 while ((i = wait(&stat)) != pid) 1412 { 1413 if (i < 0) 1414 { 1415 stat = EX_OSERR << 8; 1416 break; 1417 } 1418 } 1419 if ((stat & 0377) != 0) 1420 stat = EX_UNAVAILABLE << 8; 1421 return ((stat >> 8) & 0377); 1422 } 1423 } 1424 /* 1425 ** SENDALL -- actually send all the messages. 1426 ** 1427 ** Parameters: 1428 ** verifyonly -- if set, only give verification messages. 1429 ** 1430 ** Returns: 1431 ** none. 1432 ** 1433 ** Side Effects: 1434 ** Scans the send lists and sends everything it finds. 1435 */ 1436 1437 sendall(verifyonly) 1438 bool verifyonly; 1439 { 1440 register ADDRESS *q; 1441 typedef int (*fnptr)(); 1442 1443 # ifdef DEBUG 1444 if (Debug > 1) 1445 { 1446 printf("\nSend Queue:\n"); 1447 printaddr(CurEnv->e_sendqueue, TRUE); 1448 } 1449 # endif DEBUG 1450 1451 for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next) 1452 { 1453 if (verifyonly) 1454 { 1455 CurEnv->e_to = q->q_paddr; 1456 if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1457 { 1458 if (bitset(M_LOCAL, q->q_mailer->m_flags)) 1459 message(Arpa_Info, "deliverable"); 1460 else 1461 message(Arpa_Info, "queueable"); 1462 } 1463 } 1464 else 1465 (void) deliver(q); 1466 } 1467 } 1468