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.61 01/05/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 ** editfcn -- if non-NULL, we want to call this function 24 ** to output the letter (instead of just out- 25 ** putting it raw). 26 ** 27 ** Returns: 28 ** zero -- successfully delivered. 29 ** else -- some failure, see ExitStat for more info. 30 ** 31 ** Side Effects: 32 ** The standard input is passed off to someone. 33 */ 34 35 deliver(firstto, editfcn) 36 ADDRESS *firstto; 37 int (*editfcn)(); 38 { 39 char *host; /* host being sent to */ 40 char *user; /* user being sent to */ 41 char **pvp; 42 register char **mvp; 43 register char *p; 44 register struct mailer *m; /* mailer for this recipient */ 45 register int i; 46 extern putmessage(); 47 extern bool checkcompat(); 48 char *pv[MAXPV+1]; 49 char tobuf[MAXLINE]; /* text line of to people */ 50 char buf[MAXNAME]; 51 ADDRESS *ctladdr; 52 extern ADDRESS *getctladdr(); 53 char tfrombuf[MAXNAME]; /* translated from person */ 54 extern char **prescan(); 55 register ADDRESS *to = firstto; 56 bool clever = FALSE; /* running user smtp to this mailer */ 57 bool tempfail = FALSE; 58 ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 59 60 errno = 0; 61 if (!ForceMail && bitset(QDONTSEND, to->q_flags)) 62 return (0); 63 64 # ifdef DEBUG 65 if (Debug) 66 printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 67 to->q_mailer->m_mno, to->q_host, to->q_user); 68 # endif DEBUG 69 70 /* 71 ** Do initial argv setup. 72 ** Insert the mailer name. Notice that $x expansion is 73 ** NOT done on the mailer name. Then, if the mailer has 74 ** a picky -f flag, we insert it as appropriate. This 75 ** code does not check for 'pv' overflow; this places a 76 ** manifest lower limit of 4 for MAXPV. 77 */ 78 79 m = to->q_mailer; 80 host = to->q_host; 81 82 /* rewrite from address, using rewriting rules */ 83 (void) expand(m->m_from, buf, &buf[sizeof buf - 1]); 84 mvp = prescan(buf, '\0'); 85 if (mvp == NULL) 86 { 87 syserr("bad mailer from translate \"%s\"", buf); 88 return (EX_SOFTWARE); 89 } 90 rewrite(mvp, 2); 91 cataddr(mvp, tfrombuf, sizeof tfrombuf); 92 93 define('g', tfrombuf); /* translated sender address */ 94 define('h', host); /* to host */ 95 Errors = 0; 96 pvp = pv; 97 *pvp++ = m->m_argv[0]; 98 99 /* insert -f or -r flag as appropriate */ 100 if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag) 101 { 102 if (bitset(M_FOPT, m->m_flags)) 103 *pvp++ = "-f"; 104 else 105 *pvp++ = "-r"; 106 (void) expand("$g", buf, &buf[sizeof buf - 1]); 107 *pvp++ = newstr(buf); 108 } 109 110 /* 111 ** Append the other fixed parts of the argv. These run 112 ** up to the first entry containing "$u". There can only 113 ** be one of these, and there are only a few more slots 114 ** in the pv after it. 115 */ 116 117 for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 118 { 119 while ((p = index(p, '$')) != NULL) 120 if (*++p == 'u') 121 break; 122 if (p != NULL) 123 break; 124 125 /* this entry is safe -- go ahead and process it */ 126 (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 127 *pvp++ = newstr(buf); 128 if (pvp >= &pv[MAXPV - 3]) 129 { 130 syserr("Too many parameters to %s before $u", pv[0]); 131 return (-1); 132 } 133 } 134 135 if (*mvp == NULL) 136 { 137 /* running SMTP */ 138 # ifdef SMTP 139 clever = TRUE; 140 *pvp = NULL; 141 i = smtpinit(m, pv, (ADDRESS *) NULL); 142 giveresponse(i, TRUE, m); 143 # ifdef QUEUE 144 if (i == EX_TEMPFAIL) 145 { 146 QueueUp = TRUE; 147 tempfail = TRUE; 148 } 149 # endif QUEUE 150 # else SMTP 151 syserr("SMTP style mailer"); 152 return (EX_SOFTWARE); 153 # endif SMTP 154 } 155 156 /* 157 ** At this point *mvp points to the argument with $u. We 158 ** run through our address list and append all the addresses 159 ** we can. If we run out of space, do not fret! We can 160 ** always send another copy later. 161 */ 162 163 tobuf[0] = '\0'; 164 To = tobuf; 165 ctladdr = NULL; 166 for (; to != NULL; to = to->q_next) 167 { 168 /* avoid sending multiple recipients to dumb mailers */ 169 if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags)) 170 break; 171 172 /* if already sent or not for this host, don't send */ 173 if ((!ForceMail && bitset(QDONTSEND, to->q_flags)) || 174 strcmp(to->q_host, host) != 0 || to->q_mailer != firstto->q_mailer) 175 continue; 176 177 # ifdef DEBUG 178 if (Debug) 179 { 180 printf("\nsend to "); 181 printaddr(to, FALSE); 182 } 183 # endif DEBUG 184 185 /* link together the chain of recipients */ 186 if (!bitset(QDONTSEND, to->q_flags)) 187 { 188 to->q_tchain = tochain; 189 tochain = to; 190 } 191 192 /* compute effective uid/gid when sending */ 193 if (to->q_mailer == ProgMailer) 194 ctladdr = getctladdr(to); 195 196 user = to->q_user; 197 To = to->q_paddr; 198 to->q_flags |= QDONTSEND; 199 if (tempfail) 200 { 201 to->q_flags |= QQUEUEUP; 202 continue; 203 } 204 205 /* 206 ** Check to see that these people are allowed to 207 ** talk to each other. 208 */ 209 210 if (!checkcompat(to)) 211 { 212 giveresponse(EX_UNAVAILABLE, TRUE, m); 213 continue; 214 } 215 216 /* 217 ** Strip quote bits from names if the mailer is dumb 218 ** about them. 219 */ 220 221 if (bitset(M_STRIPQ, m->m_flags)) 222 { 223 stripquotes(user, TRUE); 224 stripquotes(host, TRUE); 225 } 226 else 227 { 228 stripquotes(user, FALSE); 229 stripquotes(host, FALSE); 230 } 231 232 /* 233 ** If an error message has already been given, don't 234 ** bother to send to this address. 235 ** 236 ** >>>>>>>>>> This clause assumes that the local mailer 237 ** >> NOTE >> cannot do any further aliasing; that 238 ** >>>>>>>>>> function is subsumed by sendmail. 239 */ 240 241 if (bitset(QBADADDR, to->q_flags)) 242 continue; 243 244 /* save statistics.... */ 245 Stat.stat_nt[to->q_mailer->m_mno]++; 246 Stat.stat_bt[to->q_mailer->m_mno] += kbytes(MsgSize); 247 248 /* 249 ** See if this user name is "special". 250 ** If the user name has a slash in it, assume that this 251 ** is a file -- send it off without further ado. 252 ** Note that this means that editfcn's will not 253 ** be applied to the message. Also note that 254 ** this type of addresses is not processed along 255 ** with the others, so we fudge on the To person. 256 */ 257 258 if (m == LocalMailer) 259 { 260 if (index(user, '/') != NULL) 261 { 262 i = mailfile(user, getctladdr(to)); 263 giveresponse(i, TRUE, m); 264 continue; 265 } 266 } 267 268 /* 269 ** Address is verified -- add this user to mailer 270 ** argv, and add it to the print list of recipients. 271 */ 272 273 /* create list of users for error messages */ 274 if (tobuf[0] != '\0') 275 (void) strcat(tobuf, ","); 276 (void) strcat(tobuf, to->q_paddr); 277 define('u', user); /* to user */ 278 define('z', to->q_home); /* user's home */ 279 280 /* 281 ** Expand out this user into argument list or 282 ** send it to our SMTP server. 283 */ 284 285 if (clever) 286 { 287 # ifdef SMTP 288 i = smtprcpt(to); 289 if (i != EX_OK) 290 { 291 # ifdef QUEUE 292 if (i == EX_TEMPFAIL) 293 { 294 QueueUp = TRUE; 295 to->q_flags |= QQUEUEUP; 296 } 297 else 298 # endif QUEUE 299 { 300 to->q_flags |= QBADADDR; 301 giveresponse(i, TRUE, m); 302 } 303 } 304 # else SMTP 305 syserr("trying to be clever"); 306 # endif SMTP 307 } 308 else 309 { 310 (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 311 *pvp++ = newstr(buf); 312 if (pvp >= &pv[MAXPV - 2]) 313 { 314 /* allow some space for trailing parms */ 315 break; 316 } 317 } 318 } 319 320 /* see if any addresses still exist */ 321 if (tobuf[0] == '\0') 322 { 323 # ifdef SMTP 324 if (clever) 325 smtpquit(pv[0]); 326 # endif SMTP 327 return (0); 328 } 329 330 /* print out messages as full list */ 331 To = tobuf; 332 333 /* 334 ** Fill out any parameters after the $u parameter. 335 */ 336 337 while (!clever && *++mvp != NULL) 338 { 339 (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 340 *pvp++ = newstr(buf); 341 if (pvp >= &pv[MAXPV]) 342 syserr("deliver: pv overflow after $u for %s", pv[0]); 343 } 344 *pvp++ = NULL; 345 346 /* 347 ** Call the mailer. 348 ** The argument vector gets built, pipes 349 ** are created as necessary, and we fork & exec as 350 ** appropriate. 351 ** If we are running SMTP, we just need to clean up. 352 */ 353 354 if (editfcn == NULL) 355 editfcn = putmessage; 356 if (ctladdr == NULL) 357 ctladdr = &From; 358 # ifdef SMTP 359 if (clever) 360 { 361 i = smtpfinish(m, editfcn); 362 smtpquit(pv[0]); 363 } 364 else 365 # endif SMTP 366 i = sendoff(m, pv, editfcn, ctladdr); 367 368 /* 369 ** If we got a temporary failure, arrange to queue the 370 ** addressees. 371 */ 372 373 # ifdef QUEUE 374 if (i == EX_TEMPFAIL) 375 { 376 QueueUp = TRUE; 377 for (to = tochain; to != NULL; to = to->q_tchain) 378 to->q_flags |= QQUEUEUP; 379 } 380 # endif QUEUE 381 382 errno = 0; 383 return (i); 384 } 385 /* 386 ** DOFORK -- do a fork, retrying a couple of times on failure. 387 ** 388 ** This MUST be a macro, since after a vfork we are running 389 ** two processes on the same stack!!! 390 ** 391 ** Parameters: 392 ** none. 393 ** 394 ** Returns: 395 ** From a macro??? You've got to be kidding! 396 ** 397 ** Side Effects: 398 ** Modifies the ==> LOCAL <== variable 'pid', leaving: 399 ** pid of child in parent, zero in child. 400 ** -1 on unrecoverable error. 401 ** 402 ** Notes: 403 ** I'm awfully sorry this looks so awful. That's 404 ** vfork for you..... 405 */ 406 407 # define NFORKTRIES 5 408 # ifdef VFORK 409 # define XFORK vfork 410 # else VFORK 411 # define XFORK fork 412 # endif VFORK 413 414 # define DOFORK(fORKfN) \ 415 {\ 416 register int i;\ 417 \ 418 for (i = NFORKTRIES; i-- > 0; )\ 419 {\ 420 pid = fORKfN();\ 421 if (pid >= 0)\ 422 break;\ 423 sleep((unsigned) NFORKTRIES - i);\ 424 }\ 425 } 426 /* 427 ** DOFORK -- simple fork interface to DOFORK. 428 ** 429 ** Parameters: 430 ** none. 431 ** 432 ** Returns: 433 ** pid of child in parent. 434 ** zero in child. 435 ** -1 on error. 436 ** 437 ** Side Effects: 438 ** returns twice, once in parent and once in child. 439 */ 440 441 dofork() 442 { 443 register int pid; 444 445 DOFORK(fork); 446 return (pid); 447 } 448 /* 449 ** SENDOFF -- send off call to mailer & collect response. 450 ** 451 ** Parameters: 452 ** m -- mailer descriptor. 453 ** pvp -- parameter vector to send to it. 454 ** editfcn -- function to pipe it through. 455 ** ctladdr -- an address pointer controlling the 456 ** user/groupid etc. of the mailer. 457 ** 458 ** Returns: 459 ** exit status of mailer. 460 ** 461 ** Side Effects: 462 ** none. 463 */ 464 465 sendoff(m, pvp, editfcn, ctladdr) 466 struct mailer *m; 467 char **pvp; 468 int (*editfcn)(); 469 ADDRESS *ctladdr; 470 { 471 auto FILE *mfile; 472 auto FILE *rfile; 473 register int i; 474 extern putmessage(); 475 int pid; 476 477 /* 478 ** Create connection to mailer. 479 */ 480 481 pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile); 482 if (pid < 0) 483 return (-1); 484 485 /* 486 ** Format and send message. 487 */ 488 489 (void) signal(SIGPIPE, SIG_IGN); 490 if (editfcn == NULL) 491 editfcn = putmessage; 492 493 (*editfcn)(mfile, m, FALSE); 494 (void) fclose(mfile); 495 496 i = endmailer(pid, pvp[0]); 497 giveresponse(i, TRUE, m); 498 return (i); 499 } 500 /* 501 ** ENDMAILER -- Wait for mailer to terminate. 502 ** 503 ** We should never get fatal errors (e.g., segmentation 504 ** violation), so we report those specially. For other 505 ** errors, we choose a status message (into statmsg), 506 ** and if it represents an error, we print it. 507 ** 508 ** Parameters: 509 ** pid -- pid of mailer. 510 ** name -- name of mailer (for error messages). 511 ** 512 ** Returns: 513 ** exit code of mailer. 514 ** 515 ** Side Effects: 516 ** none. 517 */ 518 519 endmailer(pid, name) 520 int pid; 521 char *name; 522 { 523 register int i; 524 auto int st; 525 526 while ((i = wait(&st)) > 0 && i != pid) 527 continue; 528 if (i < 0) 529 { 530 syserr("wait"); 531 return (-1); 532 } 533 if ((st & 0377) != 0) 534 { 535 syserr("%s: stat %o", name, st); 536 ExitStat = EX_UNAVAILABLE; 537 return (-1); 538 } 539 i = (st >> 8) & 0377; 540 return (i); 541 } 542 /* 543 ** OPENMAILER -- open connection to mailer. 544 ** 545 ** Parameters: 546 ** m -- mailer descriptor. 547 ** pvp -- parameter vector to pass to mailer. 548 ** ctladdr -- controlling address for user. 549 ** clever -- create a full duplex connection. 550 ** pmfile -- pointer to mfile (to mailer) connection. 551 ** prfile -- pointer to rfile (from mailer) connection. 552 ** 553 ** Returns: 554 ** pid of mailer. 555 ** -1 on error. 556 ** 557 ** Side Effects: 558 ** creates a mailer in a subprocess. 559 */ 560 561 openmailer(m, pvp, ctladdr, clever, pmfile, prfile) 562 struct mailer *m; 563 char **pvp; 564 ADDRESS *ctladdr; 565 bool clever; 566 FILE **pmfile; 567 FILE **prfile; 568 { 569 int pid; 570 int mpvect[2]; 571 int rpvect[2]; 572 FILE *mfile; 573 FILE *rfile; 574 extern FILE *fdopen(); 575 576 # ifdef DEBUG 577 if (Debug) 578 { 579 printf("openmailer:\n"); 580 printav(pvp); 581 } 582 # endif DEBUG 583 errno = 0; 584 585 /* create a pipe to shove the mail through */ 586 if (pipe(mpvect) < 0) 587 { 588 syserr("pipe (to mailer)"); 589 return (-1); 590 } 591 592 # ifdef SMTP 593 /* if this mailer speaks smtp, create a return pipe */ 594 if (clever && pipe(rpvect) < 0) 595 { 596 syserr("pipe (from mailer)"); 597 (void) close(mpvect[0]); 598 (void) close(mpvect[1]); 599 return (-1); 600 } 601 # endif SMTP 602 603 DOFORK(XFORK); 604 /* pid is set by DOFORK */ 605 if (pid < 0) 606 { 607 syserr("Cannot fork"); 608 (void) close(mpvect[0]); 609 (void) close(mpvect[1]); 610 if (clever) 611 { 612 (void) close(rpvect[0]); 613 (void) close(rpvect[1]); 614 } 615 return (-1); 616 } 617 else if (pid == 0) 618 { 619 /* child -- set up input & exec mailer */ 620 /* make diagnostic output be standard output */ 621 (void) signal(SIGINT, SIG_IGN); 622 (void) signal(SIGHUP, SIG_IGN); 623 (void) signal(SIGTERM, SIG_DFL); 624 625 /* arrange to filter standard & diag output of command */ 626 if (clever) 627 { 628 (void) close(rpvect[0]); 629 (void) close(1); 630 (void) dup(rpvect[1]); 631 (void) close(rpvect[1]); 632 } 633 else if (OutChannel != stdout) 634 { 635 (void) close(1); 636 (void) dup(fileno(OutChannel)); 637 } 638 (void) close(2); 639 (void) dup(1); 640 641 /* arrange to get standard input */ 642 (void) close(mpvect[1]); 643 (void) close(0); 644 if (dup(mpvect[0]) < 0) 645 { 646 syserr("Cannot dup to zero!"); 647 _exit(EX_OSERR); 648 } 649 (void) close(mpvect[0]); 650 if (!bitset(M_RESTR, m->m_flags)) 651 { 652 if (ctladdr->q_uid == 0) 653 { 654 extern int DefUid, DefGid; 655 656 (void) setgid(DefGid); 657 (void) setuid(DefUid); 658 } 659 else 660 { 661 (void) setgid(ctladdr->q_gid); 662 (void) setuid(ctladdr->q_uid); 663 } 664 } 665 # ifndef VFORK 666 /* 667 ** We have to be careful with vfork - we can't mung up the 668 ** memory but we don't want the mailer to inherit any extra 669 ** open files. Chances are the mailer won't 670 ** care about an extra file, but then again you never know. 671 ** Actually, we would like to close(fileno(pwf)), but it's 672 ** declared static so we can't. But if we fclose(pwf), which 673 ** is what endpwent does, it closes it in the parent too and 674 ** the next getpwnam will be slower. If you have a weird 675 ** mailer that chokes on the extra file you should do the 676 ** endpwent(). 677 ** 678 ** Similar comments apply to log. However, openlog is 679 ** clever enough to set the FIOCLEX mode on the file, 680 ** so it will be closed automatically on the exec. 681 */ 682 683 endpwent(); 684 # ifdef LOG 685 closelog(); 686 # endif LOG 687 # endif VFORK 688 execv(m->m_mailer, pvp); 689 /* syserr fails because log is closed */ 690 /* syserr("Cannot exec %s", m->m_mailer); */ 691 printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno); 692 (void) fflush(stdout); 693 _exit(EX_UNAVAILABLE); 694 } 695 696 /* 697 ** Set up return value. 698 */ 699 700 (void) close(mpvect[0]); 701 mfile = fdopen(mpvect[1], "w"); 702 if (clever) 703 { 704 (void) close(rpvect[1]); 705 rfile = fdopen(rpvect[0], "r"); 706 } 707 708 *pmfile = mfile; 709 *prfile = rfile; 710 711 return (pid); 712 } 713 /* 714 ** GIVERESPONSE -- Interpret an error response from a mailer 715 ** 716 ** Parameters: 717 ** stat -- the status code from the mailer (high byte 718 ** only; core dumps must have been taken care of 719 ** already). 720 ** force -- if set, force an error message output, even 721 ** if the mailer seems to like to print its own 722 ** messages. 723 ** m -- the mailer descriptor for this mailer. 724 ** 725 ** Returns: 726 ** none. 727 ** 728 ** Side Effects: 729 ** Errors may be incremented. 730 ** ExitStat may be set. 731 */ 732 733 giveresponse(stat, force, m) 734 int stat; 735 int force; 736 register struct mailer *m; 737 { 738 register char *statmsg; 739 extern char *SysExMsg[]; 740 register int i; 741 extern int N_SysEx; 742 char buf[30]; 743 744 /* 745 ** Compute status message from code. 746 */ 747 748 i = stat - EX__BASE; 749 if (i < 0 || i > N_SysEx) 750 statmsg = NULL; 751 else 752 statmsg = SysExMsg[i]; 753 if (stat == 0) 754 { 755 if (bitset(M_LOCAL, m->m_flags)) 756 statmsg = "delivered"; 757 else 758 statmsg = "queued"; 759 if (Verbose) 760 message(Arpa_Info, statmsg); 761 } 762 # ifdef QUEUE 763 else if (stat == EX_TEMPFAIL) 764 { 765 if (Verbose) 766 message(Arpa_Info, "transmission deferred"); 767 } 768 # endif QUEUE 769 else 770 { 771 Errors++; 772 if (statmsg == NULL && m->m_badstat != 0) 773 { 774 stat = m->m_badstat; 775 i = stat - EX__BASE; 776 # ifdef DEBUG 777 if (i < 0 || i >= N_SysEx) 778 syserr("Bad m_badstat %d", stat); 779 else 780 # endif DEBUG 781 statmsg = SysExMsg[i]; 782 } 783 if (statmsg == NULL) 784 usrerr("unknown mailer response %d", stat); 785 else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 786 usrerr("%s", statmsg); 787 } 788 789 /* 790 ** Final cleanup. 791 ** Log a record of the transaction. Compute the new 792 ** ExitStat -- if we already had an error, stick with 793 ** that. 794 */ 795 796 if (statmsg == NULL) 797 { 798 (void) sprintf(buf, "error %d", stat); 799 statmsg = buf; 800 } 801 802 # ifdef LOG 803 syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg); 804 # endif LOG 805 # ifdef QUEUE 806 if (stat != EX_TEMPFAIL) 807 # endif QUEUE 808 setstat(stat); 809 } 810 /* 811 ** PUTMESSAGE -- output a message to the final mailer. 812 ** 813 ** This routine takes care of recreating the header from the 814 ** in-core copy, etc. 815 ** 816 ** Parameters: 817 ** fp -- file to output onto. 818 ** m -- a mailer descriptor. 819 ** xdot -- if set, hide lines beginning with dot. 820 ** 821 ** Returns: 822 ** none. 823 ** 824 ** Side Effects: 825 ** The message is written onto fp. 826 */ 827 828 putmessage(fp, m, xdot) 829 FILE *fp; 830 struct mailer *m; 831 bool xdot; 832 { 833 char buf[BUFSIZ]; 834 register HDR *h; 835 extern char *arpadate(); 836 bool anyheader = FALSE; 837 extern char *capitalize(); 838 extern char *hvalue(); 839 extern bool samefrom(); 840 char *of_line; 841 842 /* 843 ** Output "From" line unless supressed 844 ** 845 ** >>>>>>>>>> One of the ugliest hacks seen by human eyes is 846 ** >>>>>>>>>> contained herein: UUCP wants those stupid 847 ** >>>>>>>>>> "remote from <host>" lines. Why oh why does a 848 ** >> NOTE >> well-meaning programmer such as myself have to 849 ** >>>>>>>>>> deal with this kind of antique garbage???? 850 ** >>>>>>>>>> This even depends on the local UUCP host name 851 ** >>>>>>>>>> being in the $U macro!!!! 852 */ 853 854 if (!bitset(M_NHDR, m->m_flags)) 855 { 856 # ifdef UGLYUUCP 857 char *p = rindex(m->m_mailer, '/'); 858 859 if (p != NULL && strcmp(p, "/uux") == 0 && 860 strcmp(m->m_name, "uucp") == 0) 861 (void) expand("From $f $d remote from $U", buf, 862 &buf[sizeof buf - 1]); 863 else 864 # endif UGLYUUCP 865 (void) expand("$l", buf, &buf[sizeof buf - 1]); 866 fprintf(fp, "%s\n", buf); 867 } 868 869 /* 870 ** Output all header lines 871 */ 872 873 of_line = hvalue("original-from"); 874 for (h = Header; h != NULL; h = h->h_link) 875 { 876 register char *p; 877 char *origfrom = OrigFrom; 878 bool nooutput; 879 880 nooutput = FALSE; 881 if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 882 { 883 p = ")><("; /* can't happen (I hope) */ 884 nooutput = TRUE; 885 } 886 887 /* use From: line from message if generated is the same */ 888 if (strcmp(h->h_field, "from") == 0 && origfrom != NULL && 889 strcmp(m->m_from, "$f") == 0 && of_line == NULL) 890 { 891 p = origfrom; 892 origfrom = NULL; 893 } 894 else if (bitset(H_DEFAULT, h->h_flags)) 895 { 896 (void) expand(h->h_value, buf, &buf[sizeof buf]); 897 p = buf; 898 } 899 else 900 p = h->h_value; 901 if (p == NULL || *p == '\0') 902 continue; 903 904 /* hack, hack -- output Original-From field if different */ 905 if (strcmp(h->h_field, "from") == 0 && origfrom != NULL) 906 { 907 /* output new Original-From line if needed */ 908 if (of_line == NULL && !samefrom(p, origfrom)) 909 { 910 fprintf(fp, "Original-From: %s\n", origfrom); 911 anyheader = TRUE; 912 } 913 if (of_line != NULL && !nooutput && samefrom(p, of_line)) 914 { 915 /* delete Original-From: line if redundant */ 916 p = of_line; 917 of_line = NULL; 918 } 919 } 920 else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL) 921 nooutput = TRUE; 922 923 /* finally, output the header line */ 924 if (!nooutput) 925 { 926 fprintf(fp, "%s: %s\n", capitalize(h->h_field), p); 927 h->h_flags |= H_USED; 928 anyheader = TRUE; 929 } 930 } 931 if (anyheader) 932 fprintf(fp, "\n"); 933 934 /* 935 ** Output the body of the message 936 */ 937 938 if (TempFile != NULL) 939 { 940 rewind(TempFile); 941 while (!ferror(fp) && fgets(buf, sizeof buf, TempFile) != NULL) 942 fprintf(fp, "%s%s", xdot && buf[0] == '.' ? "." : "", buf); 943 944 if (ferror(TempFile)) 945 { 946 syserr("putmessage: read error"); 947 setstat(EX_IOERR); 948 } 949 } 950 951 (void) fflush(fp); 952 if (ferror(fp) && errno != EPIPE) 953 { 954 syserr("putmessage: write error"); 955 setstat(EX_IOERR); 956 } 957 errno = 0; 958 } 959 /* 960 ** SAMEFROM -- tell if two text addresses represent the same from address. 961 ** 962 ** Parameters: 963 ** ifrom -- internally generated form of from address. 964 ** efrom -- external form of from address. 965 ** 966 ** Returns: 967 ** TRUE -- if they convey the same info. 968 ** FALSE -- if any information has been lost. 969 ** 970 ** Side Effects: 971 ** none. 972 */ 973 974 bool 975 samefrom(ifrom, efrom) 976 char *ifrom; 977 char *efrom; 978 { 979 register char *p; 980 char buf[MAXNAME + 4]; 981 982 # ifdef DEBUG 983 if (Debug > 7) 984 printf("samefrom(%s,%s)-->", ifrom, efrom); 985 # endif DEBUG 986 if (strcmp(ifrom, efrom) == 0) 987 goto success; 988 p = index(ifrom, '@'); 989 if (p == NULL) 990 goto failure; 991 *p = '\0'; 992 strcpy(buf, ifrom); 993 strcat(buf, " at "); 994 *p++ = '@'; 995 strcat(buf, p); 996 if (strcmp(buf, efrom) == 0) 997 goto success; 998 999 failure: 1000 # ifdef DEBUG 1001 if (Debug > 7) 1002 printf("FALSE\n"); 1003 # endif DEBUG 1004 return (FALSE); 1005 1006 success: 1007 # ifdef DEBUG 1008 if (Debug > 7) 1009 printf("TRUE\n"); 1010 # endif DEBUG 1011 return (TRUE); 1012 } 1013 /* 1014 ** MAILFILE -- Send a message to a file. 1015 ** 1016 ** If the file has the setuid/setgid bits set, but NO execute 1017 ** bits, sendmail will try to become the owner of that file 1018 ** rather than the real user. Obviously, this only works if 1019 ** sendmail runs as root. 1020 ** 1021 ** Parameters: 1022 ** filename -- the name of the file to send to. 1023 ** ctladdr -- the controlling address header -- includes 1024 ** the userid/groupid to be when sending. 1025 ** 1026 ** Returns: 1027 ** The exit code associated with the operation. 1028 ** 1029 ** Side Effects: 1030 ** none. 1031 */ 1032 1033 mailfile(filename, ctladdr) 1034 char *filename; 1035 ADDRESS *ctladdr; 1036 { 1037 register FILE *f; 1038 register int pid; 1039 1040 /* 1041 ** Fork so we can change permissions here. 1042 ** Note that we MUST use fork, not vfork, because of 1043 ** the complications of calling subroutines, etc. 1044 */ 1045 1046 DOFORK(fork); 1047 1048 if (pid < 0) 1049 return (EX_OSERR); 1050 else if (pid == 0) 1051 { 1052 /* child -- actually write to file */ 1053 struct stat stb; 1054 extern int DefUid, DefGid; 1055 1056 (void) signal(SIGINT, SIG_DFL); 1057 (void) signal(SIGHUP, SIG_DFL); 1058 (void) signal(SIGTERM, SIG_DFL); 1059 umask(OldUmask); 1060 if (stat(filename, &stb) < 0) 1061 stb.st_mode = 0666; 1062 if (bitset(0111, stb.st_mode)) 1063 exit(EX_CANTCREAT); 1064 if (ctladdr == NULL) 1065 ctladdr = &From; 1066 if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0) 1067 { 1068 if (ctladdr->q_uid == 0) 1069 (void) setgid(DefGid); 1070 else 1071 (void) setgid(ctladdr->q_gid); 1072 } 1073 if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0) 1074 { 1075 if (ctladdr->q_uid == 0) 1076 (void) setuid(DefUid); 1077 else 1078 (void) setuid(ctladdr->q_uid); 1079 } 1080 f = fopen(filename, "a"); 1081 if (f == NULL) 1082 exit(EX_CANTCREAT); 1083 1084 putmessage(f, Mailer[1], FALSE); 1085 fputs("\n", f); 1086 (void) fclose(f); 1087 (void) fflush(stdout); 1088 1089 /* reset ISUID & ISGID bits */ 1090 (void) chmod(filename, (int) stb.st_mode); 1091 exit(EX_OK); 1092 /*NOTREACHED*/ 1093 } 1094 else 1095 { 1096 /* parent -- wait for exit status */ 1097 register int i; 1098 auto int stat; 1099 1100 while ((i = wait(&stat)) != pid) 1101 { 1102 if (i < 0) 1103 { 1104 stat = EX_OSERR << 8; 1105 break; 1106 } 1107 } 1108 if ((stat & 0377) != 0) 1109 stat = EX_UNAVAILABLE << 8; 1110 return ((stat >> 8) & 0377); 1111 } 1112 } 1113 /* 1114 ** SENDALL -- actually send all the messages. 1115 ** 1116 ** Parameters: 1117 ** verifyonly -- if set, only give verification messages. 1118 ** 1119 ** Returns: 1120 ** none. 1121 ** 1122 ** Side Effects: 1123 ** Scans the send lists and sends everything it finds. 1124 */ 1125 1126 sendall(verifyonly) 1127 bool verifyonly; 1128 { 1129 register ADDRESS *q; 1130 typedef int (*fnptr)(); 1131 1132 # ifdef DEBUG 1133 if (Debug > 1) 1134 { 1135 printf("\nSendQueue:\n"); 1136 printaddr(SendQueue, TRUE); 1137 } 1138 # endif DEBUG 1139 1140 for (q = SendQueue; q != NULL; q = q->q_next) 1141 { 1142 if (verifyonly) 1143 { 1144 To = q->q_paddr; 1145 if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 1146 { 1147 if (bitset(M_LOCAL, q->q_mailer->m_flags)) 1148 message(Arpa_Info, "deliverable"); 1149 else 1150 message(Arpa_Info, "queueable"); 1151 } 1152 } 1153 else 1154 (void) deliver(q, (fnptr) NULL); 1155 } 1156 } 1157