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