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