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