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.91 06/26/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 register u_short port; 646 647 if (!clever) 648 syserr("non-clever IPC"); 649 if (pvp[2] != NULL) 650 port = atoi(pvp[2]); 651 else 652 port = 0; 653 i = makeconnection(pvp[1], port, pmfile, prfile); 654 if (i != EX_OK) 655 { 656 ExitStat = i; 657 return (-1); 658 } 659 else 660 return (0); 661 } 662 # endif DAEMON 663 664 /* create a pipe to shove the mail through */ 665 if (pipe(mpvect) < 0) 666 { 667 syserr("pipe (to mailer)"); 668 return (-1); 669 } 670 671 # ifdef SMTP 672 /* if this mailer speaks smtp, create a return pipe */ 673 if (clever && pipe(rpvect) < 0) 674 { 675 syserr("pipe (from mailer)"); 676 (void) close(mpvect[0]); 677 (void) close(mpvect[1]); 678 return (-1); 679 } 680 # endif SMTP 681 682 /* 683 ** Actually fork the mailer process. 684 ** DOFORK is clever about retrying. 685 */ 686 687 DOFORK(XFORK); 688 /* pid is set by DOFORK */ 689 if (pid < 0) 690 { 691 /* failure */ 692 syserr("Cannot fork"); 693 (void) close(mpvect[0]); 694 (void) close(mpvect[1]); 695 if (clever) 696 { 697 (void) close(rpvect[0]); 698 (void) close(rpvect[1]); 699 } 700 return (-1); 701 } 702 else if (pid == 0) 703 { 704 /* child -- set up input & exec mailer */ 705 /* make diagnostic output be standard output */ 706 (void) signal(SIGINT, SIG_IGN); 707 (void) signal(SIGHUP, SIG_IGN); 708 (void) signal(SIGTERM, SIG_DFL); 709 710 /* arrange to filter standard & diag output of command */ 711 if (clever) 712 { 713 (void) close(rpvect[0]); 714 (void) close(1); 715 (void) dup(rpvect[1]); 716 (void) close(rpvect[1]); 717 } 718 else if (OutChannel != stdout) 719 { 720 (void) close(1); 721 (void) dup(fileno(OutChannel)); 722 } 723 (void) close(2); 724 (void) dup(1); 725 726 /* arrange to get standard input */ 727 (void) close(mpvect[1]); 728 (void) close(0); 729 if (dup(mpvect[0]) < 0) 730 { 731 syserr("Cannot dup to zero!"); 732 _exit(EX_OSERR); 733 } 734 (void) close(mpvect[0]); 735 if (!bitset(M_RESTR, m->m_flags)) 736 { 737 if (ctladdr->q_uid == 0) 738 { 739 (void) setgid(DefGid); 740 (void) setuid(DefUid); 741 } 742 else 743 { 744 (void) setgid(ctladdr->q_gid); 745 (void) setuid(ctladdr->q_uid); 746 } 747 } 748 # ifndef VFORK 749 /* 750 ** We have to be careful with vfork - we can't mung up the 751 ** memory but we don't want the mailer to inherit any extra 752 ** open files. Chances are the mailer won't 753 ** care about an extra file, but then again you never know. 754 ** Actually, we would like to close(fileno(pwf)), but it's 755 ** declared static so we can't. But if we fclose(pwf), which 756 ** is what endpwent does, it closes it in the parent too and 757 ** the next getpwnam will be slower. If you have a weird 758 ** mailer that chokes on the extra file you should do the 759 ** endpwent(). 760 ** 761 ** Similar comments apply to log. However, openlog is 762 ** clever enough to set the FIOCLEX mode on the file, 763 ** so it will be closed automatically on the exec. 764 */ 765 766 endpwent(); 767 # ifdef LOG 768 closelog(); 769 # endif LOG 770 # endif VFORK 771 772 /* try to execute the mailer */ 773 execv(m->m_mailer, pvp); 774 775 /* syserr fails because log is closed */ 776 /* syserr("Cannot exec %s", m->m_mailer); */ 777 printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 778 (void) fflush(stdout); 779 _exit(EX_UNAVAILABLE); 780 } 781 782 /* 783 ** Set up return value. 784 */ 785 786 (void) close(mpvect[0]); 787 mfile = fdopen(mpvect[1], "w"); 788 if (clever) 789 { 790 (void) close(rpvect[1]); 791 rfile = fdopen(rpvect[0], "r"); 792 } 793 794 *pmfile = mfile; 795 *prfile = rfile; 796 797 return (pid); 798 } 799 /* 800 ** GIVERESPONSE -- Interpret an error response from a mailer 801 ** 802 ** Parameters: 803 ** stat -- the status code from the mailer (high byte 804 ** only; core dumps must have been taken care of 805 ** already). 806 ** force -- if set, force an error message output, even 807 ** if the mailer seems to like to print its own 808 ** messages. 809 ** m -- the mailer descriptor for this mailer. 810 ** 811 ** Returns: 812 ** none. 813 ** 814 ** Side Effects: 815 ** Errors may be incremented. 816 ** ExitStat may be set. 817 */ 818 819 giveresponse(stat, force, m) 820 int stat; 821 bool force; 822 register struct mailer *m; 823 { 824 register char *statmsg; 825 extern char *SysExMsg[]; 826 register int i; 827 extern int N_SysEx; 828 char buf[30]; 829 830 /* 831 ** Compute status message from code. 832 */ 833 834 i = stat - EX__BASE; 835 if (i < 0 || i > N_SysEx) 836 statmsg = NULL; 837 else 838 statmsg = SysExMsg[i]; 839 if (stat == 0) 840 { 841 if (bitset(M_LOCAL, m->m_flags)) 842 statmsg = "delivered"; 843 else 844 statmsg = "queued"; 845 message(Arpa_Info, statmsg); 846 } 847 # ifdef QUEUE 848 else if (stat == EX_TEMPFAIL) 849 { 850 message(Arpa_Info, "transmission deferred"); 851 } 852 # endif QUEUE 853 else 854 { 855 Errors++; 856 FatalErrors = TRUE; 857 if (statmsg == NULL && m->m_badstat != 0) 858 { 859 stat = m->m_badstat; 860 i = stat - EX__BASE; 861 # ifdef DEBUG 862 if (i < 0 || i >= N_SysEx) 863 syserr("Bad m_badstat %d", stat); 864 else 865 # endif DEBUG 866 statmsg = SysExMsg[i]; 867 } 868 if (statmsg == NULL) 869 usrerr("unknown mailer response %d", stat); 870 else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 871 usrerr("%s", statmsg); 872 } 873 874 /* 875 ** Final cleanup. 876 ** Log a record of the transaction. Compute the new 877 ** ExitStat -- if we already had an error, stick with 878 ** that. 879 */ 880 881 if (statmsg == NULL) 882 { 883 (void) sprintf(buf, "error %d", stat); 884 statmsg = buf; 885 } 886 887 # ifdef LOG 888 syslog(LOG_INFO, "%s->%s: %ld: %s", CurEnv->e_from.q_paddr, CurEnv->e_to, CurEnv->e_msgsize, statmsg); 889 # endif LOG 890 # ifdef QUEUE 891 if (stat != EX_TEMPFAIL) 892 # endif QUEUE 893 setstat(stat); 894 } 895 /* 896 ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 897 ** 898 ** This can be made an arbitrary message separator by changing $l 899 ** 900 ** One of the ugliest hacks seen by human eyes is 901 ** contained herein: UUCP wants those stupid 902 ** "remote from <host>" lines. Why oh why does a 903 ** well-meaning programmer such as myself have to 904 ** deal with this kind of antique garbage???? 905 ** 906 ** Parameters: 907 ** fp -- the file to output to. 908 ** m -- the mailer describing this entry. 909 ** 910 ** Returns: 911 ** none 912 ** 913 ** Side Effects: 914 ** outputs some text to fp. 915 */ 916 917 putfromline(fp, m) 918 register FILE *fp; 919 register MAILER *m; 920 { 921 char buf[MAXLINE]; 922 923 if (bitset(M_NHDR, m->m_flags)) 924 return; 925 926 # ifdef UGLYUUCP 927 if (bitset(M_UGLYUUCP, m->m_flags)) 928 { 929 extern char *macvalue(); 930 char *sys = macvalue('g'); 931 char *bang = index(sys, '!'); 932 933 if (bang == NULL) 934 syserr("No ! in UUCP! (%s)", sys); 935 else 936 *bang = '\0'; 937 expand("From $f $d remote from $g\n", buf, 938 &buf[sizeof buf - 1], CurEnv); 939 *bang = '!'; 940 } 941 else 942 # endif UGLYUUCP 943 expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv); 944 putline(buf, fp, bitset(M_FULLSMTP, m->m_flags)); 945 } 946 /* 947 ** PUTHEADER -- put the header part of a message from the in-core copy 948 ** 949 ** Parameters: 950 ** fp -- file to put it on. 951 ** m -- mailer to use. 952 ** e -- envelope to use. 953 ** 954 ** Returns: 955 ** none. 956 ** 957 ** Side Effects: 958 ** none. 959 */ 960 961 putheader(fp, m, e) 962 register FILE *fp; 963 register struct mailer *m; 964 register ENVELOPE *e; 965 { 966 char buf[BUFSIZ]; 967 register HDR *h; 968 extern char *arpadate(); 969 extern char *capitalize(); 970 extern char *hvalue(); 971 extern bool samefrom(); 972 char *of_line; 973 char obuf[MAXLINE]; 974 register char *obp; 975 bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 976 977 of_line = hvalue("original-from"); 978 for (h = e->e_header; h != NULL; h = h->h_link) 979 { 980 register char *p; 981 char *origfrom = e->e_origfrom; 982 bool nooutput; 983 984 nooutput = FALSE; 985 if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 986 nooutput = TRUE; 987 988 /* use From: line from message if generated is the same */ 989 if (strcmp(h->h_field, "from") == 0 && origfrom != NULL && 990 strcmp(m->m_from, "$f") == 0 && of_line == NULL) 991 { 992 p = origfrom; 993 origfrom = NULL; 994 } 995 else if (bitset(H_DEFAULT, h->h_flags)) 996 { 997 expand(h->h_value, buf, &buf[sizeof buf], e); 998 p = buf; 999 } 1000 else if (bitset(H_ADDR, h->h_flags)) 1001 { 1002 register int opos; 1003 bool firstone = TRUE; 1004 1005 /* 1006 ** Output the address list translated by the 1007 ** mailer and with commas. 1008 */ 1009 1010 p = h->h_value; 1011 if (p == NULL || *p == '\0' || nooutput) 1012 continue; 1013 obp = obuf; 1014 (void) sprintf(obp, "%s: ", capitalize(h->h_field)); 1015 opos = strlen(h->h_field) + 2; 1016 obp += opos; 1017 while (*p != '\0') 1018 { 1019 register char *name = p; 1020 extern char *remotename(); 1021 char savechar; 1022 1023 /* find the end of the name */ 1024 while (*p != '\0' && *p != ',') 1025 { 1026 extern bool isatword(); 1027 char *oldp; 1028 1029 if (!e->e_oldstyle || !isspace(*p)) 1030 { 1031 p++; 1032 continue; 1033 } 1034 oldp = p; 1035 while (*p != '\0' && isspace(*p)) 1036 p++; 1037 if (*p != '@' && !isatword(p)) 1038 { 1039 p = oldp; 1040 break; 1041 } 1042 p += *p == '@' ? 1 : 2; 1043 while (*p != '\0' && isspace(*p)) 1044 p++; 1045 } 1046 savechar = *p; 1047 *p = '\0'; 1048 1049 /* translate the name to be relative */ 1050 name = remotename(name, m, FALSE); 1051 if (*name == '\0') 1052 continue; 1053 1054 /* output the name with nice formatting */ 1055 opos += strlen(name); 1056 if (!firstone) 1057 opos += 2; 1058 if (opos > 78 && !firstone) 1059 { 1060 (void) sprintf(obp, ",\n"); 1061 putline(obuf, fp, fullsmtp); 1062 obp = obuf; 1063 (void) sprintf(obp, " "); 1064 obp += strlen(obp); 1065 opos = 8 + strlen(name); 1066 } 1067 else if (!firstone) 1068 { 1069 (void) sprintf(obp, ", "); 1070 obp += 2; 1071 } 1072 (void) sprintf(obp, "%s", name); 1073 obp += strlen(obp); 1074 firstone = FALSE; 1075 1076 /* clean up the source string */ 1077 *p = savechar; 1078 while (*p != '\0' && (isspace(*p) || *p == ',')) 1079 p++; 1080 } 1081 (void) strcpy(obp, "\n"); 1082 putline(obuf, fp, fullsmtp); 1083 nooutput = TRUE; 1084 } 1085 else 1086 p = h->h_value; 1087 if (p == NULL || *p == '\0') 1088 continue; 1089 1090 /* hack, hack -- output Original-From field if different */ 1091 if (strcmp(h->h_field, "from") == 0 && origfrom != NULL) 1092 { 1093 /* output new Original-From line if needed */ 1094 if (of_line == NULL && !samefrom(p, origfrom)) 1095 { 1096 (void) sprintf(obuf, "Original-From: %s\n", origfrom); 1097 putline(obuf, fp, fullsmtp); 1098 } 1099 if (of_line != NULL && !nooutput && samefrom(p, of_line)) 1100 { 1101 /* delete Original-From: line if redundant */ 1102 p = of_line; 1103 of_line = NULL; 1104 } 1105 } 1106 else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL) 1107 nooutput = TRUE; 1108 1109 /* finally, output the header line */ 1110 if (!nooutput) 1111 { 1112 (void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p); 1113 putline(obuf, fp, fullsmtp); 1114 h->h_flags |= H_USED; 1115 } 1116 } 1117 } 1118 /* 1119 ** PUTBODY -- put the body of a message. 1120 ** 1121 ** Parameters: 1122 ** fp -- file to output onto. 1123 ** m -- a mailer descriptor. 1124 ** xdot -- if set, use SMTP hidden dot algorithm. 1125 ** 1126 ** Returns: 1127 ** none. 1128 ** 1129 ** Side Effects: 1130 ** The message is written onto fp. 1131 */ 1132 1133 putbody(fp, m, xdot) 1134 FILE *fp; 1135 struct mailer *m; 1136 bool xdot; 1137 { 1138 char buf[MAXLINE + 1]; 1139 bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 1140 1141 /* 1142 ** Output the body of the message 1143 */ 1144 1145 #ifdef lint 1146 /* m will be needed later for complete smtp emulation */ 1147 if (m == NULL) 1148 return; 1149 #endif lint 1150 1151 if (TempFile != NULL) 1152 { 1153 rewind(TempFile); 1154 buf[0] = '.'; 1155 while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL) 1156 putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp); 1157 1158 if (ferror(TempFile)) 1159 { 1160 syserr("putbody: read error"); 1161 ExitStat = EX_IOERR; 1162 } 1163 } 1164 1165 (void) fflush(fp); 1166 if (ferror(fp) && errno != EPIPE) 1167 { 1168 syserr("putbody: write error"); 1169 ExitStat = EX_IOERR; 1170 } 1171 errno = 0; 1172 } 1173 /* 1174 ** ISATWORD -- tell if the word we are pointing to is "at". 1175 ** 1176 ** Parameters: 1177 ** p -- word to check. 1178 ** 1179 ** Returns: 1180 ** TRUE -- if p is the word at. 1181 ** FALSE -- otherwise. 1182 ** 1183 ** Side Effects: 1184 ** none. 1185 */ 1186 1187 bool 1188 isatword(p) 1189 register char *p; 1190 { 1191 extern char lower(); 1192 1193 if (lower(p[0]) == 'a' && lower(p[1]) == 't' && 1194 p[2] != '\0' && isspace(p[2])) 1195 return (TRUE); 1196 return (FALSE); 1197 } 1198 /* 1199 ** REMOTENAME -- return the name relative to the current mailer 1200 ** 1201 ** Parameters: 1202 ** name -- the name to translate. 1203 ** force -- if set, forces rewriting even if the mailer 1204 ** does not request it. Used for rewriting 1205 ** sender addresses. 1206 ** 1207 ** Returns: 1208 ** the text string representing this address relative to 1209 ** the receiving mailer. 1210 ** 1211 ** Side Effects: 1212 ** none. 1213 ** 1214 ** Warnings: 1215 ** The text string returned is tucked away locally; 1216 ** copy it if you intend to save it. 1217 */ 1218 1219 char * 1220 remotename(name, m, force) 1221 char *name; 1222 struct mailer *m; 1223 bool force; 1224 { 1225 static char buf[MAXNAME]; 1226 char lbuf[MAXNAME]; 1227 extern char *macvalue(); 1228 char *oldf = macvalue('f'); 1229 char *oldx = macvalue('x'); 1230 char *oldg = macvalue('g'); 1231 extern char **prescan(); 1232 register char **pvp; 1233 extern char *getxpart(); 1234 extern ADDRESS *buildaddr(); 1235 1236 /* 1237 ** See if this mailer wants the name to be rewritten. There are 1238 ** many problems here, owing to the standards for doing replies. 1239 ** In general, these names should only be rewritten if we are 1240 ** sending to another host that runs sendmail. 1241 */ 1242 1243 if (!bitset(M_RELRCPT, m->m_flags) && !force) 1244 return (name); 1245 1246 /* 1247 ** Do general rewriting of name. 1248 ** This will also take care of doing global name translation. 1249 */ 1250 1251 define('x', getxpart(name)); 1252 pvp = prescan(name, '\0'); 1253 if (pvp == NULL) 1254 return (name); 1255 rewrite(pvp, 1); 1256 rewrite(pvp, 3); 1257 if (**pvp == CANONNET) 1258 { 1259 /* oops... resolved to something */ 1260 return (name); 1261 } 1262 cataddr(pvp, lbuf, sizeof lbuf); 1263 1264 /* make the name relative to the receiving mailer */ 1265 define('f', lbuf); 1266 expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv); 1267 1268 /* rewrite to get rid of garbage we added in the expand above */ 1269 pvp = prescan(buf, '\0'); 1270 if (pvp == NULL) 1271 return (name); 1272 rewrite(pvp, 2); 1273 cataddr(pvp, lbuf, sizeof lbuf); 1274 1275 /* now add any comment info we had before back */ 1276 define('g', lbuf); 1277 expand("$q", buf, &buf[sizeof buf - 1], CurEnv); 1278 1279 define('f', oldf); 1280 define('g', oldg); 1281 define('x', oldx); 1282 1283 # ifdef DEBUG 1284 if (Debug > 0) 1285 printf("remotename(%s) => `%s'\n", name, buf); 1286 # endif DEBUG 1287 return (buf); 1288 } 1289 /* 1290 ** SAMEFROM -- tell if two text addresses represent the same from address. 1291 ** 1292 ** Parameters: 1293 ** ifrom -- internally generated form of from address. 1294 ** efrom -- external form of from address. 1295 ** 1296 ** Returns: 1297 ** TRUE -- if they convey the same info. 1298 ** FALSE -- if any information has been lost. 1299 ** 1300 ** Side Effects: 1301 ** none. 1302 */ 1303 1304 bool 1305 samefrom(ifrom, efrom) 1306 char *ifrom; 1307 char *efrom; 1308 { 1309 register char *p; 1310 char buf[MAXNAME + 4]; 1311 1312 # ifdef DEBUG 1313 if (Debug > 7) 1314 printf("samefrom(%s,%s)-->", ifrom, efrom); 1315 # endif DEBUG 1316 if (strcmp(ifrom, efrom) == 0) 1317 goto success; 1318 p = index(ifrom, '@'); 1319 if (p == NULL) 1320 goto failure; 1321 *p = '\0'; 1322 (void) strcpy(buf, ifrom); 1323 (void) strcat(buf, " at "); 1324 *p++ = '@'; 1325 (void) strcat(buf, p); 1326 if (strcmp(buf, efrom) == 0) 1327 goto success; 1328 1329 failure: 1330 # ifdef DEBUG 1331 if (Debug > 7) 1332 printf("FALSE\n"); 1333 # endif DEBUG 1334 return (FALSE); 1335 1336 success: 1337 # ifdef DEBUG 1338 if (Debug > 7) 1339 printf("TRUE\n"); 1340 # endif DEBUG 1341 return (TRUE); 1342 } 1343 /* 1344 ** MAILFILE -- Send a message to a file. 1345 ** 1346 ** If the file has the setuid/setgid bits set, but NO execute 1347 ** bits, sendmail will try to become the owner of that file 1348 ** rather than the real user. Obviously, this only works if 1349 ** sendmail runs as root. 1350 ** 1351 ** Parameters: 1352 ** filename -- the name of the file to send to. 1353 ** ctladdr -- the controlling address header -- includes 1354 ** the userid/groupid to be when sending. 1355 ** 1356 ** Returns: 1357 ** The exit code associated with the operation. 1358 ** 1359 ** Side Effects: 1360 ** none. 1361 */ 1362 1363 mailfile(filename, ctladdr) 1364 char *filename; 1365 ADDRESS *ctladdr; 1366 { 1367 register FILE *f; 1368 register int pid; 1369 1370 /* 1371 ** Fork so we can change permissions here. 1372 ** Note that we MUST use fork, not vfork, because of 1373 ** the complications of calling subroutines, etc. 1374 */ 1375 1376 DOFORK(fork); 1377 1378 if (pid < 0) 1379 return (EX_OSERR); 1380 else if (pid == 0) 1381 { 1382 /* child -- actually write to file */ 1383 struct stat stb; 1384 1385 (void) signal(SIGINT, SIG_DFL); 1386 (void) signal(SIGHUP, SIG_DFL); 1387 (void) signal(SIGTERM, SIG_DFL); 1388 umask(OldUmask); 1389 if (stat(filename, &stb) < 0) 1390 stb.st_mode = 0666; 1391 if (bitset(0111, stb.st_mode)) 1392 exit(EX_CANTCREAT); 1393 if (ctladdr == NULL) 1394 ctladdr = &CurEnv->e_from; 1395 if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 1396 { 1397 if (ctladdr->q_uid == 0) 1398 (void) setgid(DefGid); 1399 else 1400 (void) setgid(ctladdr->q_gid); 1401 } 1402 if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 1403 { 1404 if (ctladdr->q_uid == 0) 1405 (void) setuid(DefUid); 1406 else 1407 (void) setuid(ctladdr->q_uid); 1408 } 1409 f = dfopen(filename, "a"); 1410 if (f == NULL) 1411 exit(EX_CANTCREAT); 1412 1413 putfromline(f, Mailer[1]); 1414 (*CurEnv->e_puthdr)(f, Mailer[1], CurEnv); 1415 fputs("\n", f); 1416 (*CurEnv->e_putbody)(f, Mailer[1], FALSE); 1417 fputs("\n", f); 1418 (void) fclose(f); 1419 (void) fflush(stdout); 1420 1421 /* reset ISUID & ISGID bits for paranoid systems */ 1422 (void) chmod(filename, (int) stb.st_mode); 1423 exit(EX_OK); 1424 /*NOTREACHED*/ 1425 } 1426 else 1427 { 1428 /* parent -- wait for exit status */ 1429 register int i; 1430 auto int stat; 1431 1432 while ((i = wait(&stat)) != pid) 1433 { 1434 if (i < 0) 1435 { 1436 stat = EX_OSERR << 8; 1437 break; 1438 } 1439 } 1440 if ((stat & 0377) != 0) 1441 stat = EX_UNAVAILABLE << 8; 1442 return ((stat >> 8) & 0377); 1443 } 1444 } 1445 /* 1446 ** SENDALL -- actually send all the messages. 1447 ** 1448 ** Parameters: 1449 ** e -- the envelope to send. 1450 ** verifyonly -- if set, only give verification messages. 1451 ** 1452 ** Returns: 1453 ** none. 1454 ** 1455 ** Side Effects: 1456 ** Scans the send lists and sends everything it finds. 1457 ** Delivers any appropriate error messages. 1458 */ 1459 1460 sendall(e, verifyonly) 1461 ENVELOPE *e; 1462 bool verifyonly; 1463 { 1464 register ADDRESS *q; 1465 1466 # ifdef DEBUG 1467 if (Debug > 1) 1468 { 1469 printf("\nSend Queue:\n"); 1470 printaddr(e->e_sendqueue, TRUE); 1471 } 1472 # endif DEBUG 1473 1474 /* 1475 ** Run through the list and send everything. 1476 */ 1477 1478 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1479 { 1480 if (verifyonly) 1481 { 1482 CurEnv->e_to = q->q_paddr; 1483 if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1484 message(Arpa_Info, "deliverable"); 1485 } 1486 else 1487 (void) deliver(q); 1488 } 1489 1490 /* 1491 ** Now run through and check for errors. 1492 */ 1493 1494 if (verifyonly) 1495 return; 1496 1497 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1498 { 1499 register ADDRESS *qq; 1500 1501 if (bitset(QQUEUEUP, q->q_flags)) 1502 e->e_queueup = TRUE; 1503 if (!bitset(QBADADDR, q->q_flags)) 1504 continue; 1505 1506 /* we have an address that failed -- find the parent */ 1507 for (qq = q; qq != NULL; qq = qq->q_alias) 1508 { 1509 char obuf[MAXNAME + 6]; 1510 extern char *aliaslookup(); 1511 1512 /* we can only have owners for local addresses */ 1513 if (!bitset(M_LOCAL, qq->q_mailer->m_flags)) 1514 continue; 1515 1516 /* see if the owner list exists */ 1517 (void) strcpy(obuf, "owner-"); 1518 if (strncmp(qq->q_user, "owner-", 6) == 0) 1519 (void) strcat(obuf, "owner"); 1520 else 1521 (void) strcat(obuf, qq->q_user); 1522 if (aliaslookup(obuf) == NULL) 1523 continue; 1524 1525 /* owner list exists -- add it to the error queue */ 1526 qq->q_flags &= ~QPRIMARY; 1527 sendto(obuf, 1, qq, &e->e_errorqueue); 1528 MailBack = TRUE; 1529 break; 1530 } 1531 1532 /* if we did not find an owner, send to the sender */ 1533 if (qq == NULL) 1534 sendto(e->e_from.q_paddr, 1, qq, &e->e_errorqueue); 1535 } 1536 } 1537 /* 1538 ** CHECKERRORS -- check a queue of addresses and process errors. 1539 ** 1540 ** Parameters: 1541 ** e -- the envelope to check. 1542 ** 1543 ** Returns: 1544 ** none. 1545 ** 1546 ** Side Effects: 1547 ** Arranges to queue all tempfailed messages in q 1548 ** or deliver error responses. 1549 */ 1550 1551 checkerrors(e) 1552 register ENVELOPE *e; 1553 { 1554 # ifdef DEBUG 1555 if (Debug > 0) 1556 { 1557 printf("\ncheckerrors: errorqueue:\n"); 1558 printaddr(e->e_errorqueue, TRUE); 1559 } 1560 # endif DEBUG 1561 1562 /* mail back the transcript on errors */ 1563 if (FatalErrors) 1564 savemail(); 1565 1566 /* queue up anything laying around */ 1567 if (e->e_queueup) 1568 { 1569 # ifdef QUEUE 1570 queueup(e, FALSE); 1571 # else QUEUE 1572 syserr("finis: trying to queue %s", e->e_df); 1573 # endif QUEUE 1574 } 1575 } 1576