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