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