1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #ifndef lint 10 static char sccsid[] = "@(#)headers.c 8.51 (Berkeley) 03/21/95"; 11 #endif /* not lint */ 12 13 # include <errno.h> 14 # include "sendmail.h" 15 16 /* 17 ** CHOMPHEADER -- process and save a header line. 18 ** 19 ** Called by collect and by readcf to deal with header lines. 20 ** 21 ** Parameters: 22 ** line -- header as a text line. 23 ** def -- if set, this is a default value. 24 ** e -- the envelope including this header. 25 ** 26 ** Returns: 27 ** flags for this header. 28 ** 29 ** Side Effects: 30 ** The header is saved on the header list. 31 ** Contents of 'line' are destroyed. 32 */ 33 34 chompheader(line, def, e) 35 char *line; 36 bool def; 37 register ENVELOPE *e; 38 { 39 register char *p; 40 register HDR *h; 41 HDR **hp; 42 char *fname; 43 char *fvalue; 44 struct hdrinfo *hi; 45 bool cond = FALSE; 46 BITMAP mopts; 47 char buf[MAXNAME + 1]; 48 49 if (tTd(31, 6)) 50 printf("chompheader: %s\n", line); 51 52 /* strip off options */ 53 clrbitmap(mopts); 54 p = line; 55 if (*p == '?') 56 { 57 /* have some */ 58 register char *q = strchr(p + 1, *p); 59 60 if (q != NULL) 61 { 62 *q++ = '\0'; 63 while (*++p != '\0') 64 setbitn(*p, mopts); 65 p = q; 66 } 67 else 68 usrerr("553 header syntax error, line \"%s\"", line); 69 cond = TRUE; 70 } 71 72 /* find canonical name */ 73 fname = p; 74 while (isascii(*p) && isgraph(*p) && *p != ':') 75 p++; 76 fvalue = p; 77 while (isascii(*p) && isspace(*p)) 78 p++; 79 if (*p++ != ':' || fname == fvalue) 80 { 81 syserr("553 header syntax error, line \"%s\"", line); 82 return (0); 83 } 84 *fvalue = '\0'; 85 fvalue = p; 86 87 /* strip field value on front */ 88 if (*fvalue == ' ') 89 fvalue++; 90 91 /* see if it is a known type */ 92 for (hi = HdrInfo; hi->hi_field != NULL; hi++) 93 { 94 if (strcasecmp(hi->hi_field, fname) == 0) 95 break; 96 } 97 98 if (tTd(31, 9)) 99 { 100 if (hi->hi_field == NULL) 101 printf("no header match\n"); 102 else 103 printf("header match, hi_flags=%x\n", hi->hi_flags); 104 } 105 106 /* see if this is a resent message */ 107 if (!def && bitset(H_RESENT, hi->hi_flags)) 108 e->e_flags |= EF_RESENT; 109 110 /* if this is an Errors-To: header keep track of it now */ 111 if (UseErrorsTo && !def && bitset(H_ERRORSTO, hi->hi_flags)) 112 (void) sendtolist(fvalue, NULLADDR, &e->e_errorqueue, 0, e); 113 114 /* if this means "end of header" quit now */ 115 if (bitset(H_EOH, hi->hi_flags)) 116 return (hi->hi_flags); 117 118 /* 119 ** Drop explicit From: if same as what we would generate. 120 ** This is to make MH (which doesn't always give a full name) 121 ** insert the full name information in all circumstances. 122 */ 123 124 p = "resent-from"; 125 if (!bitset(EF_RESENT, e->e_flags)) 126 p += 7; 127 if (!def && !bitset(EF_QUEUERUN, e->e_flags) && strcasecmp(fname, p) == 0) 128 { 129 if (tTd(31, 2)) 130 { 131 printf("comparing header from (%s) against default (%s or %s)\n", 132 fvalue, e->e_from.q_paddr, e->e_from.q_user); 133 } 134 if (e->e_from.q_paddr != NULL && 135 (strcmp(fvalue, e->e_from.q_paddr) == 0 || 136 strcmp(fvalue, e->e_from.q_user) == 0)) 137 return (hi->hi_flags); 138 #ifdef MAYBENEXTRELEASE /* XXX UNTESTED XXX UNTESTED XXX UNTESTED XXX */ 139 #ifdef USERDB 140 else 141 { 142 auto ADDRESS a; 143 char *fancy; 144 bool oldSuprErrs = SuprErrs; 145 extern char *crackaddr(); 146 extern char *udbsender(); 147 148 /* 149 ** Try doing USERDB rewriting even on fully commented 150 ** names; this saves the "comment" information (such 151 ** as full name) and rewrites the electronic part. 152 ** 153 ** XXX This code doesn't belong here -- parsing should 154 ** XXX not be done during collect() phase because 155 ** XXX error messages can confuse the SMTP phase. 156 ** XXX Setting SuprErrs is a crude hack around this 157 ** XXX problem. 158 */ 159 160 if (OpMode == MD_SMTP || OpMode == MD_ARPAFTP) 161 SuprErrs = TRUE; 162 fancy = crackaddr(fvalue); 163 if (parseaddr(fvalue, &a, RF_COPYNONE, '\0', NULL, e) != NULL && 164 bitnset(M_CHECKUDB, a.q_mailer->m_flags) && 165 (p = udbsender(a.q_user)) != NULL) 166 { 167 char *oldg = macvalue('g', e); 168 169 define('g', p, e); 170 expand(fancy, buf, sizeof buf, e); 171 define('g', oldg, e); 172 fvalue = buf; 173 } 174 SuprErrs = oldSuprErrs; 175 } 176 #endif 177 #endif 178 } 179 180 /* delete default value for this header */ 181 for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 182 { 183 if (strcasecmp(fname, h->h_field) == 0 && 184 bitset(H_DEFAULT, h->h_flags) && 185 !bitset(H_FORCE, h->h_flags)) 186 h->h_value = NULL; 187 } 188 189 /* create a new node */ 190 h = (HDR *) xalloc(sizeof *h); 191 h->h_field = newstr(fname); 192 h->h_value = newstr(fvalue); 193 h->h_link = NULL; 194 bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts); 195 *hp = h; 196 h->h_flags = hi->hi_flags; 197 if (def) 198 h->h_flags |= H_DEFAULT; 199 if (cond) 200 h->h_flags |= H_CHECK; 201 202 /* hack to see if this is a new format message */ 203 if (!def && bitset(H_RCPT|H_FROM, h->h_flags) && 204 (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL || 205 strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL)) 206 { 207 e->e_flags &= ~EF_OLDSTYLE; 208 } 209 210 return (h->h_flags); 211 } 212 /* 213 ** ADDHEADER -- add a header entry to the end of the queue. 214 ** 215 ** This bypasses the special checking of chompheader. 216 ** 217 ** Parameters: 218 ** field -- the name of the header field. 219 ** value -- the value of the field. 220 ** hp -- an indirect pointer to the header structure list. 221 ** 222 ** Returns: 223 ** none. 224 ** 225 ** Side Effects: 226 ** adds the field on the list of headers for this envelope. 227 */ 228 229 addheader(field, value, hdrlist) 230 char *field; 231 char *value; 232 HDR **hdrlist; 233 { 234 register HDR *h; 235 register struct hdrinfo *hi; 236 HDR **hp; 237 238 /* find info struct */ 239 for (hi = HdrInfo; hi->hi_field != NULL; hi++) 240 { 241 if (strcasecmp(field, hi->hi_field) == 0) 242 break; 243 } 244 245 /* find current place in list -- keep back pointer? */ 246 for (hp = hdrlist; (h = *hp) != NULL; hp = &h->h_link) 247 { 248 if (strcasecmp(field, h->h_field) == 0) 249 break; 250 } 251 252 /* allocate space for new header */ 253 h = (HDR *) xalloc(sizeof *h); 254 h->h_field = field; 255 h->h_value = newstr(value); 256 h->h_link = *hp; 257 h->h_flags = hi->hi_flags | H_DEFAULT; 258 clrbitmap(h->h_mflags); 259 *hp = h; 260 } 261 /* 262 ** HVALUE -- return value of a header. 263 ** 264 ** Only "real" fields (i.e., ones that have not been supplied 265 ** as a default) are used. 266 ** 267 ** Parameters: 268 ** field -- the field name. 269 ** header -- the header list. 270 ** 271 ** Returns: 272 ** pointer to the value part. 273 ** NULL if not found. 274 ** 275 ** Side Effects: 276 ** none. 277 */ 278 279 char * 280 hvalue(field, header) 281 char *field; 282 HDR *header; 283 { 284 register HDR *h; 285 286 for (h = header; h != NULL; h = h->h_link) 287 { 288 if (!bitset(H_DEFAULT, h->h_flags) && 289 strcasecmp(h->h_field, field) == 0) 290 return (h->h_value); 291 } 292 return (NULL); 293 } 294 /* 295 ** ISHEADER -- predicate telling if argument is a header. 296 ** 297 ** A line is a header if it has a single word followed by 298 ** optional white space followed by a colon. 299 ** 300 ** Parameters: 301 ** s -- string to check for possible headerness. 302 ** 303 ** Returns: 304 ** TRUE if s is a header. 305 ** FALSE otherwise. 306 ** 307 ** Side Effects: 308 ** none. 309 */ 310 311 bool 312 isheader(s) 313 register char *s; 314 { 315 while (*s > ' ' && *s != ':' && *s != '\0') 316 s++; 317 318 /* following technically violates RFC822 */ 319 while (isascii(*s) && isspace(*s)) 320 s++; 321 322 return (*s == ':'); 323 } 324 /* 325 ** EATHEADER -- run through the stored header and extract info. 326 ** 327 ** Parameters: 328 ** e -- the envelope to process. 329 ** full -- if set, do full processing (e.g., compute 330 ** message priority). 331 ** 332 ** Returns: 333 ** none. 334 ** 335 ** Side Effects: 336 ** Sets a bunch of global variables from information 337 ** in the collected header. 338 ** Aborts the message if the hop count is exceeded. 339 */ 340 341 eatheader(e, full) 342 register ENVELOPE *e; 343 bool full; 344 { 345 register HDR *h; 346 register char *p; 347 int hopcnt = 0; 348 char *msgid; 349 char buf[MAXLINE]; 350 351 /* 352 ** Set up macros for possible expansion in headers. 353 */ 354 355 define('f', e->e_sender, e); 356 define('g', e->e_sender, e); 357 if (e->e_origrcpt != NULL && *e->e_origrcpt != '\0') 358 define('u', e->e_origrcpt, e); 359 else 360 define('u', NULL, e); 361 362 /* full name of from person */ 363 p = hvalue("full-name", e->e_header); 364 if (p != NULL) 365 define('x', p, e); 366 367 if (tTd(32, 1)) 368 printf("----- collected header -----\n"); 369 msgid = "<none>"; 370 for (h = e->e_header; h != NULL; h = h->h_link) 371 { 372 if (h->h_value == NULL) 373 { 374 if (tTd(32, 1)) 375 printf("%s: <NULL>\n", h->h_field); 376 continue; 377 } 378 379 /* do early binding */ 380 if (bitset(H_DEFAULT, h->h_flags)) 381 { 382 expand(h->h_value, buf, sizeof buf, e); 383 if (buf[0] != '\0') 384 { 385 h->h_value = newstr(buf); 386 h->h_flags &= ~H_DEFAULT; 387 } 388 } 389 390 if (tTd(32, 1)) 391 { 392 printf("%s: ", h->h_field); 393 xputs(h->h_value); 394 printf("\n"); 395 } 396 397 /* count the number of times it has been processed */ 398 if (bitset(H_TRACE, h->h_flags)) 399 hopcnt++; 400 401 /* send to this person if we so desire */ 402 if (GrabTo && bitset(H_RCPT, h->h_flags) && 403 !bitset(H_DEFAULT, h->h_flags) && 404 (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags))) 405 { 406 int saveflags = e->e_flags; 407 408 (void) sendtolist(h->h_value, NULLADDR, 409 &e->e_sendqueue, 0, e); 410 411 /* delete fatal errors generated by this address */ 412 if (!GrabTo && !bitset(EF_FATALERRS, saveflags)) 413 e->e_flags &= ~EF_FATALERRS; 414 } 415 416 /* save the message-id for logging */ 417 if (full && strcasecmp(h->h_field, "message-id") == 0) 418 { 419 msgid = h->h_value; 420 while (isascii(*msgid) && isspace(*msgid)) 421 msgid++; 422 } 423 424 /* see if this is a return-receipt header */ 425 if (bitset(H_RECEIPTTO, h->h_flags)) 426 e->e_receiptto = h->h_value; 427 } 428 if (tTd(32, 1)) 429 printf("----------------------------\n"); 430 431 /* if we are just verifying (that is, sendmail -t -bv), drop out now */ 432 if (OpMode == MD_VERIFY) 433 return; 434 435 /* store hop count */ 436 if (hopcnt > e->e_hopcount) 437 e->e_hopcount = hopcnt; 438 439 /* message priority */ 440 p = hvalue("precedence", e->e_header); 441 if (p != NULL) 442 e->e_class = priencode(p); 443 if (full) 444 { 445 e->e_msgpriority = e->e_msgsize 446 - e->e_class * WkClassFact 447 + e->e_nrcpts * WkRecipFact; 448 if (e->e_class < 0) 449 e->e_timeoutclass = TOC_NONURGENT; 450 else if (e->e_class > 0) 451 e->e_timeoutclass = TOC_URGENT; 452 } 453 454 /* message timeout priority */ 455 p = hvalue("priority", e->e_header); 456 if (full && p != NULL) 457 { 458 /* (this should be in the configuration file) */ 459 if (strcasecmp(p, "urgent")) 460 e->e_timeoutclass = TOC_URGENT; 461 else if (strcasecmp(p, "normal")) 462 e->e_timeoutclass = TOC_NORMAL; 463 else if (strcasecmp(p, "non-urgent")) 464 e->e_timeoutclass = TOC_NONURGENT; 465 } 466 467 /* date message originated */ 468 p = hvalue("posted-date", e->e_header); 469 if (p == NULL) 470 p = hvalue("date", e->e_header); 471 if (p != NULL) 472 define('a', p, e); 473 474 /* 475 ** From person in antiquated ARPANET mode 476 ** required by UK Grey Book e-mail gateways (sigh) 477 */ 478 479 if (OpMode == MD_ARPAFTP) 480 { 481 register struct hdrinfo *hi; 482 483 for (hi = HdrInfo; hi->hi_field != NULL; hi++) 484 { 485 if (bitset(H_FROM, hi->hi_flags) && 486 (!bitset(H_RESENT, hi->hi_flags) || 487 bitset(EF_RESENT, e->e_flags)) && 488 (p = hvalue(hi->hi_field, e->e_header)) != NULL) 489 break; 490 } 491 if (hi->hi_field != NULL) 492 { 493 if (tTd(32, 2)) 494 printf("eatheader: setsender(*%s == %s)\n", 495 hi->hi_field, p); 496 setsender(p, e, NULL, TRUE); 497 } 498 } 499 500 /* 501 ** Log collection information. 502 */ 503 504 # ifdef LOG 505 if (bitset(EF_LOGSENDER, e->e_flags) && LogLevel > 4) 506 logsender(e, msgid); 507 # endif /* LOG */ 508 e->e_flags &= ~EF_LOGSENDER; 509 } 510 /* 511 ** LOGSENDER -- log sender information 512 ** 513 ** Parameters: 514 ** e -- the envelope to log 515 ** msgid -- the message id 516 ** 517 ** Returns: 518 ** none 519 */ 520 521 logsender(e, msgid) 522 register ENVELOPE *e; 523 char *msgid; 524 { 525 # ifdef LOG 526 char *name; 527 register char *sbp; 528 register char *p; 529 int l; 530 char hbuf[MAXNAME + 1]; 531 char sbuf[MAXLINE + 1]; 532 char mbuf[MAXNAME + 1]; 533 534 /* don't allow newlines in the message-id */ 535 if (msgid != NULL) 536 { 537 l = strlen(msgid); 538 if (l > sizeof mbuf - 1) 539 l = sizeof mbuf - 1; 540 bcopy(msgid, mbuf, l); 541 mbuf[l] = '\0'; 542 p = mbuf; 543 while ((p = strchr(p, '\n')) != NULL) 544 *p++ = ' '; 545 } 546 547 if (bitset(EF_RESPONSE, e->e_flags)) 548 name = "[RESPONSE]"; 549 else if ((name = macvalue('_', e)) != NULL) 550 ; 551 else if (RealHostName == NULL) 552 name = "localhost"; 553 else if (RealHostName[0] == '[') 554 name = RealHostName; 555 else 556 { 557 name = hbuf; 558 (void) sprintf(hbuf, "%.80s", RealHostName); 559 if (RealHostAddr.sa.sa_family != 0) 560 { 561 p = &hbuf[strlen(hbuf)]; 562 (void) sprintf(p, " (%s)", 563 anynet_ntoa(&RealHostAddr)); 564 } 565 } 566 567 /* some versions of syslog only take 5 printf args */ 568 # if (SYSLOG_BUFSIZE) >= 256 569 sbp = sbuf; 570 sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d", 571 e->e_from.q_paddr == NULL ? "<NONE>" : e->e_from.q_paddr, 572 e->e_msgsize, e->e_class, e->e_msgpriority, e->e_nrcpts); 573 sbp += strlen(sbp); 574 if (msgid != NULL) 575 { 576 sprintf(sbp, ", msgid=%.100s", mbuf); 577 sbp += strlen(sbp); 578 } 579 if (e->e_bodytype != NULL) 580 { 581 (void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype); 582 sbp += strlen(sbp); 583 } 584 p = macvalue('r', e); 585 if (p != NULL) 586 (void) sprintf(sbp, ", proto=%.20s", p); 587 syslog(LOG_INFO, "%s: %s, relay=%s", 588 e->e_id, sbuf, name); 589 590 # else /* short syslog buffer */ 591 592 syslog(LOG_INFO, "%s: from=%s", 593 e->e_id, e->e_from.q_paddr == NULL ? "<NONE>" : 594 shortenstring(e->e_from.q_paddr, 83)); 595 syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d", 596 e->e_id, e->e_msgsize, e->e_class, 597 e->e_msgpriority, e->e_nrcpts); 598 if (msgid != NULL) 599 syslog(LOG_INFO, "%s: msgid=%s", e->e_id, mbuf); 600 sbp = sbuf; 601 sprintf(sbp, "%s:", e->e_id); 602 sbp += strlen(sbp); 603 if (e->e_bodytype != NULL) 604 { 605 sprintf(sbp, " bodytype=%s,", e->e_bodytype); 606 sbp += strlen(sbp); 607 } 608 p = macvalue('r', e); 609 if (p != NULL) 610 { 611 sprintf(sbp, " proto=%s,", p); 612 sbp += strlen(sbp); 613 } 614 syslog(LOG_INFO, "%s relay=%s", sbuf, name); 615 # endif 616 # endif 617 } 618 /* 619 ** PRIENCODE -- encode external priority names into internal values. 620 ** 621 ** Parameters: 622 ** p -- priority in ascii. 623 ** 624 ** Returns: 625 ** priority as a numeric level. 626 ** 627 ** Side Effects: 628 ** none. 629 */ 630 631 priencode(p) 632 char *p; 633 { 634 register int i; 635 636 for (i = 0; i < NumPriorities; i++) 637 { 638 if (!strcasecmp(p, Priorities[i].pri_name)) 639 return (Priorities[i].pri_val); 640 } 641 642 /* unknown priority */ 643 return (0); 644 } 645 /* 646 ** CRACKADDR -- parse an address and turn it into a macro 647 ** 648 ** This doesn't actually parse the address -- it just extracts 649 ** it and replaces it with "$g". The parse is totally ad hoc 650 ** and isn't even guaranteed to leave something syntactically 651 ** identical to what it started with. However, it does leave 652 ** something semantically identical. 653 ** 654 ** This algorithm has been cleaned up to handle a wider range 655 ** of cases -- notably quoted and backslash escaped strings. 656 ** This modification makes it substantially better at preserving 657 ** the original syntax. 658 ** 659 ** Parameters: 660 ** addr -- the address to be cracked. 661 ** 662 ** Returns: 663 ** a pointer to the new version. 664 ** 665 ** Side Effects: 666 ** none. 667 ** 668 ** Warning: 669 ** The return value is saved in local storage and should 670 ** be copied if it is to be reused. 671 */ 672 673 char * 674 crackaddr(addr) 675 register char *addr; 676 { 677 register char *p; 678 register char c; 679 int cmtlev; 680 int realcmtlev; 681 int anglelev, realanglelev; 682 int copylev; 683 bool qmode; 684 bool realqmode; 685 bool skipping; 686 bool putgmac = FALSE; 687 bool quoteit = FALSE; 688 bool gotangle = FALSE; 689 register char *bp; 690 char *buflim; 691 static char buf[MAXNAME + 1]; 692 693 if (tTd(33, 1)) 694 printf("crackaddr(%s)\n", addr); 695 696 /* strip leading spaces */ 697 while (*addr != '\0' && isascii(*addr) && isspace(*addr)) 698 addr++; 699 700 /* 701 ** Start by assuming we have no angle brackets. This will be 702 ** adjusted later if we find them. 703 */ 704 705 bp = buf; 706 buflim = &buf[sizeof buf - 5]; 707 p = addr; 708 copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0; 709 qmode = realqmode = FALSE; 710 711 while ((c = *p++) != '\0') 712 { 713 /* 714 ** If the buffer is overful, go into a special "skipping" 715 ** mode that tries to keep legal syntax but doesn't actually 716 ** output things. 717 */ 718 719 skipping = bp >= buflim; 720 721 if (copylev > 0 && !skipping) 722 *bp++ = c; 723 724 /* check for backslash escapes */ 725 if (c == '\\') 726 { 727 /* arrange to quote the address */ 728 if (cmtlev <= 0 && !qmode) 729 quoteit = TRUE; 730 731 if ((c = *p++) == '\0') 732 { 733 /* too far */ 734 p--; 735 goto putg; 736 } 737 if (copylev > 0 && !skipping) 738 *bp++ = c; 739 goto putg; 740 } 741 742 /* check for quoted strings */ 743 if (c == '"' && cmtlev <= 0) 744 { 745 qmode = !qmode; 746 if (copylev > 0 && !skipping) 747 realqmode = !realqmode; 748 continue; 749 } 750 if (qmode) 751 goto putg; 752 753 /* check for comments */ 754 if (c == '(') 755 { 756 cmtlev++; 757 758 /* allow space for closing paren */ 759 if (!skipping) 760 { 761 buflim--; 762 realcmtlev++; 763 if (copylev++ <= 0) 764 { 765 *bp++ = ' '; 766 *bp++ = c; 767 } 768 } 769 } 770 if (cmtlev > 0) 771 { 772 if (c == ')') 773 { 774 cmtlev--; 775 copylev--; 776 if (!skipping) 777 { 778 realcmtlev--; 779 buflim++; 780 } 781 } 782 continue; 783 } 784 else if (c == ')') 785 { 786 /* syntax error: unmatched ) */ 787 if (copylev > 0 && !skipping) 788 bp--; 789 } 790 791 /* check for characters that may have to be quoted */ 792 if (strchr(".'@,;:\\()[]", c) != NULL) 793 { 794 /* 795 ** If these occur as the phrase part of a <> 796 ** construct, but are not inside of () or already 797 ** quoted, they will have to be quoted. Note that 798 ** now (but don't actually do the quoting). 799 */ 800 801 if (cmtlev <= 0 && !qmode) 802 quoteit = TRUE; 803 } 804 805 /* check for angle brackets */ 806 if (c == '<') 807 { 808 register char *q; 809 810 /* assume first of two angles is bogus */ 811 if (gotangle) 812 quoteit = TRUE; 813 gotangle = TRUE; 814 815 /* oops -- have to change our mind */ 816 anglelev = 1; 817 if (!skipping) 818 realanglelev = 1; 819 820 bp = buf; 821 if (quoteit) 822 { 823 *bp++ = '"'; 824 825 /* back up over the '<' and any spaces */ 826 --p; 827 while (isascii(*--p) && isspace(*p)) 828 continue; 829 p++; 830 } 831 for (q = addr; q < p; ) 832 { 833 c = *q++; 834 if (bp < buflim) 835 { 836 if (quoteit && c == '"') 837 *bp++ = '\\'; 838 *bp++ = c; 839 } 840 } 841 if (quoteit) 842 { 843 if (bp == &buf[1]) 844 bp--; 845 else 846 *bp++ = '"'; 847 while ((c = *p++) != '<') 848 { 849 if (bp < buflim) 850 *bp++ = c; 851 } 852 *bp++ = c; 853 } 854 copylev = 0; 855 putgmac = quoteit = FALSE; 856 continue; 857 } 858 859 if (c == '>') 860 { 861 if (anglelev > 0) 862 { 863 anglelev--; 864 if (!skipping) 865 { 866 realanglelev--; 867 buflim++; 868 } 869 } 870 else if (!skipping) 871 { 872 /* syntax error: unmatched > */ 873 if (copylev > 0) 874 bp--; 875 quoteit = TRUE; 876 continue; 877 } 878 if (copylev++ <= 0) 879 *bp++ = c; 880 continue; 881 } 882 883 /* must be a real address character */ 884 putg: 885 if (copylev <= 0 && !putgmac) 886 { 887 *bp++ = MACROEXPAND; 888 *bp++ = 'g'; 889 putgmac = TRUE; 890 } 891 } 892 893 /* repair any syntactic damage */ 894 if (realqmode) 895 *bp++ = '"'; 896 while (realcmtlev-- > 0) 897 *bp++ = ')'; 898 while (realanglelev-- > 0) 899 *bp++ = '>'; 900 *bp++ = '\0'; 901 902 if (tTd(33, 1)) 903 printf("crackaddr=>`%s'\n", buf); 904 905 return (buf); 906 } 907 /* 908 ** PUTHEADER -- put the header part of a message from the in-core copy 909 ** 910 ** Parameters: 911 ** mci -- the connection information. 912 ** h -- the header to put. 913 ** e -- envelope to use. 914 ** 915 ** Returns: 916 ** none. 917 ** 918 ** Side Effects: 919 ** none. 920 */ 921 922 /* 923 * Macro for fast max (not available in e.g. DG/UX, 386/ix). 924 */ 925 #ifndef MAX 926 # define MAX(a,b) (((a)>(b))?(a):(b)) 927 #endif 928 929 putheader(mci, h, e) 930 register MCI *mci; 931 register HDR *h; 932 register ENVELOPE *e; 933 { 934 char buf[MAX(MAXLINE,BUFSIZ)]; 935 char obuf[MAXLINE]; 936 937 if (tTd(34, 1)) 938 printf("--- putheader, mailer = %s ---\n", 939 mci->mci_mailer->m_name); 940 941 mci->mci_flags |= MCIF_INHEADER; 942 for (; h != NULL; h = h->h_link) 943 { 944 register char *p; 945 extern bool bitintersect(); 946 947 if (tTd(34, 11)) 948 { 949 printf(" %s: ", h->h_field); 950 xputs(h->h_value); 951 } 952 953 if (bitset(H_CHECK|H_ACHECK, h->h_flags) && 954 !bitintersect(h->h_mflags, mci->mci_mailer->m_flags)) 955 { 956 if (tTd(34, 11)) 957 printf(" (skipped)\n"); 958 continue; 959 } 960 961 /* handle Resent-... headers specially */ 962 if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 963 { 964 if (tTd(34, 11)) 965 printf(" (skipped (resent))\n"); 966 continue; 967 } 968 969 /* suppress return receipts if requested */ 970 if (bitset(H_RECEIPTTO, h->h_flags) && 971 bitset(EF_NORECEIPT, e->e_flags)) 972 { 973 if (tTd(34, 11)) 974 printf(" (skipped (receipt))\n"); 975 continue; 976 } 977 978 /* suppress Content-Transfer-Encoding: if we are MIMEing */ 979 if (bitset(H_CTE, h->h_flags) && 980 bitset(MCIF_CVT8TO7, mci->mci_flags)) 981 { 982 if (tTd(34, 11)) 983 printf(" (skipped (content-transfer-encoding))\n"); 984 continue; 985 } 986 987 /* macro expand value if generated internally */ 988 p = h->h_value; 989 if (bitset(H_DEFAULT, h->h_flags)) 990 { 991 expand(p, buf, sizeof buf, e); 992 p = buf; 993 if (p == NULL || *p == '\0') 994 { 995 if (tTd(34, 11)) 996 printf(" (skipped -- null value)\n"); 997 continue; 998 } 999 } 1000 1001 if (tTd(34, 11)) 1002 printf("\n"); 1003 1004 if (bitset(H_STRIPVAL, h->h_flags)) 1005 { 1006 /* empty field */ 1007 (void) sprintf(obuf, "%s:", h->h_field); 1008 putline(obuf, mci); 1009 } 1010 else if (bitset(H_FROM|H_RCPT, h->h_flags)) 1011 { 1012 /* address field */ 1013 bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 1014 1015 if (bitset(H_FROM, h->h_flags)) 1016 oldstyle = FALSE; 1017 commaize(h, p, oldstyle, mci, e); 1018 } 1019 else 1020 { 1021 /* vanilla header line */ 1022 register char *nlp; 1023 1024 (void) sprintf(obuf, "%s: ", h->h_field); 1025 while ((nlp = strchr(p, '\n')) != NULL) 1026 { 1027 *nlp = '\0'; 1028 (void) strcat(obuf, p); 1029 *nlp = '\n'; 1030 putline(obuf, mci); 1031 p = ++nlp; 1032 obuf[0] = '\0'; 1033 } 1034 (void) strcat(obuf, p); 1035 putline(obuf, mci); 1036 } 1037 } 1038 1039 /* 1040 ** If we are converting this to a MIME message, add the 1041 ** MIME headers. 1042 */ 1043 1044 if (bitset(MM_MIME8BIT, MimeMode) && 1045 bitset(EF_HAS8BIT, e->e_flags) && 1046 !bitnset(M_8BITS, mci->mci_mailer->m_flags) && 1047 !bitset(MCIF_CVT8TO7, mci->mci_flags)) 1048 { 1049 if (hvalue("MIME-Version", e->e_header) == NULL) 1050 putline("MIME-Version: 1.0", mci); 1051 if (hvalue("Content-Type", e->e_header) == NULL) 1052 { 1053 sprintf(obuf, "Content-Type: text/plain; charset=%s", 1054 defcharset(e)); 1055 putline(obuf, mci); 1056 } 1057 if (hvalue("Content-Transfer-Encoding", e->e_header) == NULL) 1058 putline("Content-Transfer-Encoding: 8bit", mci); 1059 } 1060 } 1061 /* 1062 ** COMMAIZE -- output a header field, making a comma-translated list. 1063 ** 1064 ** Parameters: 1065 ** h -- the header field to output. 1066 ** p -- the value to put in it. 1067 ** oldstyle -- TRUE if this is an old style header. 1068 ** mci -- the connection information. 1069 ** e -- the envelope containing the message. 1070 ** 1071 ** Returns: 1072 ** none. 1073 ** 1074 ** Side Effects: 1075 ** outputs "p" to file "fp". 1076 */ 1077 1078 void 1079 commaize(h, p, oldstyle, mci, e) 1080 register HDR *h; 1081 register char *p; 1082 bool oldstyle; 1083 register MCI *mci; 1084 register ENVELOPE *e; 1085 { 1086 register char *obp; 1087 int opos; 1088 int omax; 1089 bool firstone = TRUE; 1090 char obuf[MAXLINE + 3]; 1091 1092 /* 1093 ** Output the address list translated by the 1094 ** mailer and with commas. 1095 */ 1096 1097 if (tTd(14, 2)) 1098 printf("commaize(%s: %s)\n", h->h_field, p); 1099 1100 obp = obuf; 1101 (void) sprintf(obp, "%s: ", h->h_field); 1102 opos = strlen(h->h_field) + 2; 1103 obp += opos; 1104 omax = mci->mci_mailer->m_linelimit - 2; 1105 if (omax < 0 || omax > 78) 1106 omax = 78; 1107 1108 /* 1109 ** Run through the list of values. 1110 */ 1111 1112 while (*p != '\0') 1113 { 1114 register char *name; 1115 register int c; 1116 char savechar; 1117 int flags; 1118 auto int stat; 1119 1120 /* 1121 ** Find the end of the name. New style names 1122 ** end with a comma, old style names end with 1123 ** a space character. However, spaces do not 1124 ** necessarily delimit an old-style name -- at 1125 ** signs mean keep going. 1126 */ 1127 1128 /* find end of name */ 1129 while ((isascii(*p) && isspace(*p)) || *p == ',') 1130 p++; 1131 name = p; 1132 for (;;) 1133 { 1134 auto char *oldp; 1135 char pvpbuf[PSBUFSIZE]; 1136 1137 (void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, 1138 sizeof pvpbuf, &oldp); 1139 p = oldp; 1140 1141 /* look to see if we have an at sign */ 1142 while (*p != '\0' && isascii(*p) && isspace(*p)) 1143 p++; 1144 1145 if (*p != '@') 1146 { 1147 p = oldp; 1148 break; 1149 } 1150 p += *p == '@' ? 1 : 2; 1151 while (*p != '\0' && isascii(*p) && isspace(*p)) 1152 p++; 1153 } 1154 /* at the end of one complete name */ 1155 1156 /* strip off trailing white space */ 1157 while (p >= name && 1158 ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0')) 1159 p--; 1160 if (++p == name) 1161 continue; 1162 savechar = *p; 1163 *p = '\0'; 1164 1165 /* translate the name to be relative */ 1166 flags = RF_HEADERADDR|RF_ADDDOMAIN; 1167 if (bitset(H_FROM, h->h_flags)) 1168 flags |= RF_SENDERADDR; 1169 #ifdef USERDB 1170 else if (e->e_from.q_mailer != NULL && 1171 bitnset(M_UDBRECIPIENT, e->e_from.q_mailer->m_flags)) 1172 { 1173 extern char *udbsender(); 1174 1175 name = udbsender(name); 1176 } 1177 #endif 1178 stat = EX_OK; 1179 name = remotename(name, mci->mci_mailer, flags, &stat, e); 1180 if (*name == '\0') 1181 { 1182 *p = savechar; 1183 continue; 1184 } 1185 1186 /* output the name with nice formatting */ 1187 opos += strlen(name); 1188 if (!firstone) 1189 opos += 2; 1190 if (opos > omax && !firstone) 1191 { 1192 (void) strcpy(obp, ",\n"); 1193 putline(obuf, mci); 1194 obp = obuf; 1195 (void) strcpy(obp, " "); 1196 opos = strlen(obp); 1197 obp += opos; 1198 opos += strlen(name); 1199 } 1200 else if (!firstone) 1201 { 1202 (void) strcpy(obp, ", "); 1203 obp += 2; 1204 } 1205 1206 while ((c = *name++) != '\0' && obp < &obuf[MAXLINE]) 1207 *obp++ = c; 1208 firstone = FALSE; 1209 *p = savechar; 1210 } 1211 (void) strcpy(obp, "\n"); 1212 putline(obuf, mci); 1213 } 1214 /* 1215 ** COPYHEADER -- copy header list 1216 ** 1217 ** This routine is the equivalent of newstr for header lists 1218 ** 1219 ** Parameters: 1220 ** header -- list of header structures to copy. 1221 ** 1222 ** Returns: 1223 ** a copy of 'header'. 1224 ** 1225 ** Side Effects: 1226 ** none. 1227 */ 1228 1229 HDR * 1230 copyheader(header) 1231 register HDR *header; 1232 { 1233 register HDR *newhdr; 1234 HDR *ret; 1235 register HDR **tail = &ret; 1236 1237 while (header != NULL) 1238 { 1239 newhdr = (HDR *) xalloc(sizeof(HDR)); 1240 STRUCTCOPY(*header, *newhdr); 1241 *tail = newhdr; 1242 tail = &newhdr->h_link; 1243 header = header->h_link; 1244 } 1245 *tail = NULL; 1246 1247 return ret; 1248 } 1249