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.90 06/23/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], FALSE); 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], TRUE); 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 bool 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\n", 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 putline(buf, fp, bitset(M_FULLSMTP, m->m_flags)); 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 char obuf[MAXLINE]; 973 register char *obp; 974 bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 975 976 of_line = hvalue("original-from"); 977 for (h = e->e_header; h != NULL; h = h->h_link) 978 { 979 register char *p; 980 char *origfrom = e->e_origfrom; 981 bool nooutput; 982 983 nooutput = FALSE; 984 if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 985 nooutput = TRUE; 986 987 /* use From: line from message if generated is the same */ 988 if (strcmp(h->h_field, "from") == 0 && origfrom != NULL && 989 strcmp(m->m_from, "$f") == 0 && of_line == NULL) 990 { 991 p = origfrom; 992 origfrom = NULL; 993 } 994 else if (bitset(H_DEFAULT, h->h_flags)) 995 { 996 expand(h->h_value, buf, &buf[sizeof buf], e); 997 p = buf; 998 } 999 else if (bitset(H_ADDR, h->h_flags)) 1000 { 1001 register int opos; 1002 bool firstone = TRUE; 1003 1004 /* 1005 ** Output the address list translated by the 1006 ** mailer and with commas. 1007 */ 1008 1009 p = h->h_value; 1010 if (p == NULL || *p == '\0' || nooutput) 1011 continue; 1012 obp = obuf; 1013 sprintf(obp, "%s: ", capitalize(h->h_field)); 1014 opos = strlen(h->h_field) + 2; 1015 obp += opos; 1016 while (*p != '\0') 1017 { 1018 register char *name = p; 1019 extern char *remotename(); 1020 char savechar; 1021 1022 /* find the end of the name */ 1023 while (*p != '\0' && *p != ',') 1024 { 1025 extern bool isatword(); 1026 char *oldp; 1027 1028 if (!e->e_oldstyle || !isspace(*p)) 1029 { 1030 p++; 1031 continue; 1032 } 1033 oldp = p; 1034 while (*p != '\0' && isspace(*p)) 1035 p++; 1036 if (*p != '@' && !isatword(p)) 1037 { 1038 p = oldp; 1039 break; 1040 } 1041 p += *p == '@' ? 1 : 2; 1042 while (*p != '\0' && isspace(*p)) 1043 p++; 1044 } 1045 savechar = *p; 1046 *p = '\0'; 1047 1048 /* translate the name to be relative */ 1049 name = remotename(name, m, FALSE); 1050 if (*name == '\0') 1051 continue; 1052 1053 /* output the name with nice formatting */ 1054 opos += strlen(name); 1055 if (!firstone) 1056 opos += 2; 1057 if (opos > 78 && !firstone) 1058 { 1059 (void) sprintf(obp, ",\n"); 1060 putline(obuf, fp, fullsmtp); 1061 obp = obuf; 1062 (void) sprintf(obp, " "); 1063 obp += strlen(obp); 1064 opos = 8 + strlen(name); 1065 } 1066 else if (!firstone) 1067 { 1068 (void) sprintf(obp, ", "); 1069 obp += 2; 1070 } 1071 (void) sprintf(obp, "%s", name); 1072 obp += strlen(obp); 1073 firstone = FALSE; 1074 1075 /* clean up the source string */ 1076 *p = savechar; 1077 while (*p != '\0' && (isspace(*p) || *p == ',')) 1078 p++; 1079 } 1080 strcpy(obp, "\n"); 1081 putline(obuf, fp, fullsmtp); 1082 nooutput = TRUE; 1083 } 1084 else 1085 p = h->h_value; 1086 if (p == NULL || *p == '\0') 1087 continue; 1088 1089 /* hack, hack -- output Original-From field if different */ 1090 if (strcmp(h->h_field, "from") == 0 && origfrom != NULL) 1091 { 1092 /* output new Original-From line if needed */ 1093 if (of_line == NULL && !samefrom(p, origfrom)) 1094 { 1095 (void) sprintf(obuf, "Original-From: %s\n", origfrom); 1096 putline(obuf, fp, fullsmtp); 1097 } 1098 if (of_line != NULL && !nooutput && samefrom(p, of_line)) 1099 { 1100 /* delete Original-From: line if redundant */ 1101 p = of_line; 1102 of_line = NULL; 1103 } 1104 } 1105 else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL) 1106 nooutput = TRUE; 1107 1108 /* finally, output the header line */ 1109 if (!nooutput) 1110 { 1111 (void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p); 1112 putline(obuf, fp, fullsmtp); 1113 h->h_flags |= H_USED; 1114 } 1115 } 1116 } 1117 /* 1118 ** PUTBODY -- put the body of a message. 1119 ** 1120 ** Parameters: 1121 ** fp -- file to output onto. 1122 ** m -- a mailer descriptor. 1123 ** xdot -- if set, use SMTP hidden dot algorithm. 1124 ** 1125 ** Returns: 1126 ** none. 1127 ** 1128 ** Side Effects: 1129 ** The message is written onto fp. 1130 */ 1131 1132 putbody(fp, m, xdot) 1133 FILE *fp; 1134 struct mailer *m; 1135 bool xdot; 1136 { 1137 char buf[MAXLINE + 1]; 1138 bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 1139 1140 /* 1141 ** Output the body of the message 1142 */ 1143 1144 #ifdef lint 1145 /* m will be needed later for complete smtp emulation */ 1146 if (m == NULL) 1147 return; 1148 #endif lint 1149 1150 if (TempFile != NULL) 1151 { 1152 rewind(TempFile); 1153 buf[0] = '.'; 1154 while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL) 1155 putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp); 1156 1157 if (ferror(TempFile)) 1158 { 1159 syserr("putbody: read error"); 1160 ExitStat = EX_IOERR; 1161 } 1162 } 1163 1164 (void) fflush(fp); 1165 if (ferror(fp) && errno != EPIPE) 1166 { 1167 syserr("putbody: write error"); 1168 ExitStat = EX_IOERR; 1169 } 1170 errno = 0; 1171 } 1172 /* 1173 ** ISATWORD -- tell if the word we are pointing to is "at". 1174 ** 1175 ** Parameters: 1176 ** p -- word to check. 1177 ** 1178 ** Returns: 1179 ** TRUE -- if p is the word at. 1180 ** FALSE -- otherwise. 1181 ** 1182 ** Side Effects: 1183 ** none. 1184 */ 1185 1186 bool 1187 isatword(p) 1188 register char *p; 1189 { 1190 extern char lower(); 1191 1192 if (lower(p[0]) == 'a' && lower(p[1]) == 't' && 1193 p[2] != '\0' && isspace(p[2])) 1194 return (TRUE); 1195 return (FALSE); 1196 } 1197 /* 1198 ** REMOTENAME -- return the name relative to the current mailer 1199 ** 1200 ** Parameters: 1201 ** name -- the name to translate. 1202 ** force -- if set, forces rewriting even if the mailer 1203 ** does not request it. Used for rewriting 1204 ** sender addresses. 1205 ** 1206 ** Returns: 1207 ** the text string representing this address relative to 1208 ** the receiving mailer. 1209 ** 1210 ** Side Effects: 1211 ** none. 1212 ** 1213 ** Warnings: 1214 ** The text string returned is tucked away locally; 1215 ** copy it if you intend to save it. 1216 */ 1217 1218 char * 1219 remotename(name, m, force) 1220 char *name; 1221 struct mailer *m; 1222 bool force; 1223 { 1224 static char buf[MAXNAME]; 1225 char lbuf[MAXNAME]; 1226 extern char *macvalue(); 1227 char *oldf = macvalue('f'); 1228 char *oldx = macvalue('x'); 1229 char *oldg = macvalue('g'); 1230 extern char **prescan(); 1231 register char **pvp; 1232 extern char *getxpart(); 1233 extern ADDRESS *buildaddr(); 1234 1235 /* 1236 ** See if this mailer wants the name to be rewritten. There are 1237 ** many problems here, owing to the standards for doing replies. 1238 ** In general, these names should only be rewritten if we are 1239 ** sending to another host that runs sendmail. 1240 */ 1241 1242 if (!bitset(M_RELRCPT, m->m_flags) && !force) 1243 return (name); 1244 1245 /* 1246 ** Do general rewriting of name. 1247 ** This will also take care of doing global name translation. 1248 */ 1249 1250 define('x', getxpart(name)); 1251 pvp = prescan(name, '\0'); 1252 if (pvp == NULL) 1253 return (name); 1254 rewrite(pvp, 1); 1255 rewrite(pvp, 3); 1256 if (**pvp == CANONNET) 1257 { 1258 /* oops... resolved to something */ 1259 return (name); 1260 } 1261 cataddr(pvp, lbuf, sizeof lbuf); 1262 1263 /* make the name relative to the receiving mailer */ 1264 define('f', lbuf); 1265 expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv); 1266 1267 /* rewrite to get rid of garbage we added in the expand above */ 1268 pvp = prescan(buf, '\0'); 1269 if (pvp == NULL) 1270 return (name); 1271 rewrite(pvp, 2); 1272 cataddr(pvp, lbuf, sizeof lbuf); 1273 1274 /* now add any comment info we had before back */ 1275 define('g', lbuf); 1276 expand("$q", buf, &buf[sizeof buf - 1], CurEnv); 1277 1278 define('f', oldf); 1279 define('g', oldg); 1280 define('x', oldx); 1281 1282 # ifdef DEBUG 1283 if (Debug > 0) 1284 printf("remotename(%s) => `%s'\n", name, buf); 1285 # endif DEBUG 1286 return (buf); 1287 } 1288 /* 1289 ** SAMEFROM -- tell if two text addresses represent the same from address. 1290 ** 1291 ** Parameters: 1292 ** ifrom -- internally generated form of from address. 1293 ** efrom -- external form of from address. 1294 ** 1295 ** Returns: 1296 ** TRUE -- if they convey the same info. 1297 ** FALSE -- if any information has been lost. 1298 ** 1299 ** Side Effects: 1300 ** none. 1301 */ 1302 1303 bool 1304 samefrom(ifrom, efrom) 1305 char *ifrom; 1306 char *efrom; 1307 { 1308 register char *p; 1309 char buf[MAXNAME + 4]; 1310 1311 # ifdef DEBUG 1312 if (Debug > 7) 1313 printf("samefrom(%s,%s)-->", ifrom, efrom); 1314 # endif DEBUG 1315 if (strcmp(ifrom, efrom) == 0) 1316 goto success; 1317 p = index(ifrom, '@'); 1318 if (p == NULL) 1319 goto failure; 1320 *p = '\0'; 1321 (void) strcpy(buf, ifrom); 1322 (void) strcat(buf, " at "); 1323 *p++ = '@'; 1324 (void) strcat(buf, p); 1325 if (strcmp(buf, efrom) == 0) 1326 goto success; 1327 1328 failure: 1329 # ifdef DEBUG 1330 if (Debug > 7) 1331 printf("FALSE\n"); 1332 # endif DEBUG 1333 return (FALSE); 1334 1335 success: 1336 # ifdef DEBUG 1337 if (Debug > 7) 1338 printf("TRUE\n"); 1339 # endif DEBUG 1340 return (TRUE); 1341 } 1342 /* 1343 ** MAILFILE -- Send a message to a file. 1344 ** 1345 ** If the file has the setuid/setgid bits set, but NO execute 1346 ** bits, sendmail will try to become the owner of that file 1347 ** rather than the real user. Obviously, this only works if 1348 ** sendmail runs as root. 1349 ** 1350 ** Parameters: 1351 ** filename -- the name of the file to send to. 1352 ** ctladdr -- the controlling address header -- includes 1353 ** the userid/groupid to be when sending. 1354 ** 1355 ** Returns: 1356 ** The exit code associated with the operation. 1357 ** 1358 ** Side Effects: 1359 ** none. 1360 */ 1361 1362 mailfile(filename, ctladdr) 1363 char *filename; 1364 ADDRESS *ctladdr; 1365 { 1366 register FILE *f; 1367 register int pid; 1368 1369 /* 1370 ** Fork so we can change permissions here. 1371 ** Note that we MUST use fork, not vfork, because of 1372 ** the complications of calling subroutines, etc. 1373 */ 1374 1375 DOFORK(fork); 1376 1377 if (pid < 0) 1378 return (EX_OSERR); 1379 else if (pid == 0) 1380 { 1381 /* child -- actually write to file */ 1382 struct stat stb; 1383 1384 (void) signal(SIGINT, SIG_DFL); 1385 (void) signal(SIGHUP, SIG_DFL); 1386 (void) signal(SIGTERM, SIG_DFL); 1387 umask(OldUmask); 1388 if (stat(filename, &stb) < 0) 1389 stb.st_mode = 0666; 1390 if (bitset(0111, stb.st_mode)) 1391 exit(EX_CANTCREAT); 1392 if (ctladdr == NULL) 1393 ctladdr = &CurEnv->e_from; 1394 if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 1395 { 1396 if (ctladdr->q_uid == 0) 1397 (void) setgid(DefGid); 1398 else 1399 (void) setgid(ctladdr->q_gid); 1400 } 1401 if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 1402 { 1403 if (ctladdr->q_uid == 0) 1404 (void) setuid(DefUid); 1405 else 1406 (void) setuid(ctladdr->q_uid); 1407 } 1408 f = dfopen(filename, "a"); 1409 if (f == NULL) 1410 exit(EX_CANTCREAT); 1411 1412 putfromline(f, Mailer[1]); 1413 (*CurEnv->e_puthdr)(f, Mailer[1], CurEnv); 1414 fputs("\n", f); 1415 (*CurEnv->e_putbody)(f, Mailer[1], FALSE); 1416 fputs("\n", f); 1417 (void) fclose(f); 1418 (void) fflush(stdout); 1419 1420 /* reset ISUID & ISGID bits for paranoid systems */ 1421 (void) chmod(filename, (int) stb.st_mode); 1422 exit(EX_OK); 1423 /*NOTREACHED*/ 1424 } 1425 else 1426 { 1427 /* parent -- wait for exit status */ 1428 register int i; 1429 auto int stat; 1430 1431 while ((i = wait(&stat)) != pid) 1432 { 1433 if (i < 0) 1434 { 1435 stat = EX_OSERR << 8; 1436 break; 1437 } 1438 } 1439 if ((stat & 0377) != 0) 1440 stat = EX_UNAVAILABLE << 8; 1441 return ((stat >> 8) & 0377); 1442 } 1443 } 1444 /* 1445 ** SENDALL -- actually send all the messages. 1446 ** 1447 ** Parameters: 1448 ** e -- the envelope to send. 1449 ** verifyonly -- if set, only give verification messages. 1450 ** 1451 ** Returns: 1452 ** none. 1453 ** 1454 ** Side Effects: 1455 ** Scans the send lists and sends everything it finds. 1456 ** Delivers any appropriate error messages. 1457 */ 1458 1459 sendall(e, verifyonly) 1460 ENVELOPE *e; 1461 bool verifyonly; 1462 { 1463 register ADDRESS *q; 1464 1465 # ifdef DEBUG 1466 if (Debug > 1) 1467 { 1468 printf("\nSend Queue:\n"); 1469 printaddr(e->e_sendqueue, TRUE); 1470 } 1471 # endif DEBUG 1472 1473 /* 1474 ** Run through the list and send everything. 1475 */ 1476 1477 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1478 { 1479 if (verifyonly) 1480 { 1481 CurEnv->e_to = q->q_paddr; 1482 if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1483 message(Arpa_Info, "deliverable"); 1484 } 1485 else 1486 (void) deliver(q); 1487 } 1488 1489 /* 1490 ** Now run through and check for errors. 1491 */ 1492 1493 if (verifyonly) 1494 return; 1495 1496 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1497 { 1498 register ADDRESS *qq; 1499 1500 if (bitset(QQUEUEUP, q->q_flags)) 1501 e->e_queueup = TRUE; 1502 if (!bitset(QBADADDR, q->q_flags)) 1503 continue; 1504 1505 /* we have an address that failed -- find the parent */ 1506 for (qq = q; qq != NULL; qq = qq->q_alias) 1507 { 1508 char obuf[MAXNAME + 6]; 1509 extern char *aliaslookup(); 1510 1511 /* we can only have owners for local addresses */ 1512 if (!bitset(M_LOCAL, qq->q_mailer->m_flags)) 1513 continue; 1514 1515 /* see if the owner list exists */ 1516 (void) strcpy(obuf, "owner-"); 1517 if (strncmp(qq->q_user, "owner-", 6) == 0) 1518 (void) strcat(obuf, "owner"); 1519 else 1520 (void) strcat(obuf, qq->q_user); 1521 if (aliaslookup(obuf) == NULL) 1522 continue; 1523 1524 /* owner list exists -- add it to the error queue */ 1525 qq->q_flags &= ~QPRIMARY; 1526 sendto(obuf, 1, qq, &e->e_errorqueue); 1527 MailBack = TRUE; 1528 break; 1529 } 1530 1531 /* if we did not find an owner, send to the sender */ 1532 if (qq == NULL) 1533 sendto(e->e_from.q_paddr, 1, qq, &e->e_errorqueue); 1534 } 1535 } 1536 /* 1537 ** CHECKERRORS -- check a queue of addresses and process errors. 1538 ** 1539 ** Parameters: 1540 ** e -- the envelope to check. 1541 ** 1542 ** Returns: 1543 ** none. 1544 ** 1545 ** Side Effects: 1546 ** Arranges to queue all tempfailed messages in q 1547 ** or deliver error responses. 1548 */ 1549 1550 checkerrors(e) 1551 register ENVELOPE *e; 1552 { 1553 # ifdef DEBUG 1554 if (Debug > 0) 1555 { 1556 printf("\ncheckerrors: errorqueue:\n"); 1557 printaddr(e->e_errorqueue, TRUE); 1558 } 1559 # endif DEBUG 1560 1561 /* mail back the transcript on errors */ 1562 if (FatalErrors) 1563 savemail(); 1564 1565 /* queue up anything laying around */ 1566 if (e->e_queueup) 1567 { 1568 # ifdef QUEUE 1569 queueup(e, FALSE); 1570 # else QUEUE 1571 syserr("finis: trying to queue %s", e->e_df); 1572 # endif QUEUE 1573 } 1574 } 1575