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.70 (Berkeley) 06/19/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
chompheader(line,def,hdrp,e)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
addheader(field,value,hdrlist)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 *
hvalue(field,header)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
isheader(h)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
eatheader(e,full)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 else 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 else if ((p = hvalue("Content-Type", e->e_header)) != NULL)
531 {
532 /* this may be an RFC 1049 message */
533 p = strpbrk(p, ";/");
534 if (p == NULL || *p == ';')
535 {
536 /* yep, it is */
537 e->e_flags |= EF_DONT_MIME;
538 }
539 }
540
541 /*
542 ** From person in antiquated ARPANET mode
543 ** required by UK Grey Book e-mail gateways (sigh)
544 */
545
546 if (OpMode == MD_ARPAFTP)
547 {
548 register struct hdrinfo *hi;
549
550 for (hi = HdrInfo; hi->hi_field != NULL; hi++)
551 {
552 if (bitset(H_FROM, hi->hi_flags) &&
553 (!bitset(H_RESENT, hi->hi_flags) ||
554 bitset(EF_RESENT, e->e_flags)) &&
555 (p = hvalue(hi->hi_field, e->e_header)) != NULL)
556 break;
557 }
558 if (hi->hi_field != NULL)
559 {
560 if (tTd(32, 2))
561 printf("eatheader: setsender(*%s == %s)\n",
562 hi->hi_field, p);
563 setsender(p, e, NULL, TRUE);
564 }
565 }
566
567 /*
568 ** Log collection information.
569 */
570
571 # ifdef LOG
572 if (bitset(EF_LOGSENDER, e->e_flags) && LogLevel > 4)
573 logsender(e, msgid);
574 # endif /* LOG */
575 e->e_flags &= ~EF_LOGSENDER;
576 }
577 /*
578 ** LOGSENDER -- log sender information
579 **
580 ** Parameters:
581 ** e -- the envelope to log
582 ** msgid -- the message id
583 **
584 ** Returns:
585 ** none
586 */
587
588 void
logsender(e,msgid)589 logsender(e, msgid)
590 register ENVELOPE *e;
591 char *msgid;
592 {
593 # ifdef LOG
594 char *name;
595 register char *sbp;
596 register char *p;
597 int l;
598 char hbuf[MAXNAME + 1];
599 char sbuf[MAXLINE + 1];
600 char mbuf[MAXNAME + 1];
601
602 /* don't allow newlines in the message-id */
603 if (msgid != NULL)
604 {
605 l = strlen(msgid);
606 if (l > sizeof mbuf - 1)
607 l = sizeof mbuf - 1;
608 bcopy(msgid, mbuf, l);
609 mbuf[l] = '\0';
610 p = mbuf;
611 while ((p = strchr(p, '\n')) != NULL)
612 *p++ = ' ';
613 }
614
615 if (bitset(EF_RESPONSE, e->e_flags))
616 name = "[RESPONSE]";
617 else if ((name = macvalue('_', e)) != NULL)
618 ;
619 else if (RealHostName == NULL)
620 name = "localhost";
621 else if (RealHostName[0] == '[')
622 name = RealHostName;
623 else
624 {
625 name = hbuf;
626 (void) sprintf(hbuf, "%.80s", RealHostName);
627 if (RealHostAddr.sa.sa_family != 0)
628 {
629 p = &hbuf[strlen(hbuf)];
630 (void) sprintf(p, " (%s)",
631 anynet_ntoa(&RealHostAddr));
632 }
633 }
634
635 /* some versions of syslog only take 5 printf args */
636 # if (SYSLOG_BUFSIZE) >= 256
637 sbp = sbuf;
638 sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d",
639 e->e_from.q_paddr == NULL ? "<NONE>" : e->e_from.q_paddr,
640 e->e_msgsize, e->e_class, e->e_msgpriority, e->e_nrcpts);
641 sbp += strlen(sbp);
642 if (msgid != NULL)
643 {
644 sprintf(sbp, ", msgid=%.100s", mbuf);
645 sbp += strlen(sbp);
646 }
647 if (e->e_bodytype != NULL)
648 {
649 (void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype);
650 sbp += strlen(sbp);
651 }
652 p = macvalue('r', e);
653 if (p != NULL)
654 (void) sprintf(sbp, ", proto=%.20s", p);
655 syslog(LOG_INFO, "%s: %s, relay=%s",
656 e->e_id, sbuf, name);
657
658 # else /* short syslog buffer */
659
660 syslog(LOG_INFO, "%s: from=%s",
661 e->e_id, e->e_from.q_paddr == NULL ? "<NONE>" :
662 shortenstring(e->e_from.q_paddr, 83));
663 syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d",
664 e->e_id, e->e_msgsize, e->e_class,
665 e->e_msgpriority, e->e_nrcpts);
666 if (msgid != NULL)
667 syslog(LOG_INFO, "%s: msgid=%s", e->e_id, mbuf);
668 sbp = sbuf;
669 sprintf(sbp, "%s:", e->e_id);
670 sbp += strlen(sbp);
671 if (e->e_bodytype != NULL)
672 {
673 sprintf(sbp, " bodytype=%s,", e->e_bodytype);
674 sbp += strlen(sbp);
675 }
676 p = macvalue('r', e);
677 if (p != NULL)
678 {
679 sprintf(sbp, " proto=%s,", p);
680 sbp += strlen(sbp);
681 }
682 syslog(LOG_INFO, "%s relay=%s", sbuf, name);
683 # endif
684 # endif
685 }
686 /*
687 ** PRIENCODE -- encode external priority names into internal values.
688 **
689 ** Parameters:
690 ** p -- priority in ascii.
691 **
692 ** Returns:
693 ** priority as a numeric level.
694 **
695 ** Side Effects:
696 ** none.
697 */
698
699 int
priencode(p)700 priencode(p)
701 char *p;
702 {
703 register int i;
704
705 for (i = 0; i < NumPriorities; i++)
706 {
707 if (!strcasecmp(p, Priorities[i].pri_name))
708 return (Priorities[i].pri_val);
709 }
710
711 /* unknown priority */
712 return (0);
713 }
714 /*
715 ** CRACKADDR -- parse an address and turn it into a macro
716 **
717 ** This doesn't actually parse the address -- it just extracts
718 ** it and replaces it with "$g". The parse is totally ad hoc
719 ** and isn't even guaranteed to leave something syntactically
720 ** identical to what it started with. However, it does leave
721 ** something semantically identical.
722 **
723 ** This algorithm has been cleaned up to handle a wider range
724 ** of cases -- notably quoted and backslash escaped strings.
725 ** This modification makes it substantially better at preserving
726 ** the original syntax.
727 **
728 ** Parameters:
729 ** addr -- the address to be cracked.
730 **
731 ** Returns:
732 ** a pointer to the new version.
733 **
734 ** Side Effects:
735 ** none.
736 **
737 ** Warning:
738 ** The return value is saved in local storage and should
739 ** be copied if it is to be reused.
740 */
741
742 char *
crackaddr(addr)743 crackaddr(addr)
744 register char *addr;
745 {
746 register char *p;
747 register char c;
748 int cmtlev;
749 int realcmtlev;
750 int anglelev, realanglelev;
751 int copylev;
752 bool qmode;
753 bool realqmode;
754 bool skipping;
755 bool putgmac = FALSE;
756 bool quoteit = FALSE;
757 bool gotangle = FALSE;
758 bool gotcolon = FALSE;
759 register char *bp;
760 char *buflim;
761 char *bufhead;
762 char *addrhead;
763 static char buf[MAXNAME + 1];
764
765 if (tTd(33, 1))
766 printf("crackaddr(%s)\n", addr);
767
768 /* strip leading spaces */
769 while (*addr != '\0' && isascii(*addr) && isspace(*addr))
770 addr++;
771
772 /*
773 ** Start by assuming we have no angle brackets. This will be
774 ** adjusted later if we find them.
775 */
776
777 bp = bufhead = buf;
778 buflim = &buf[sizeof buf - 5];
779 p = addrhead = addr;
780 copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
781 qmode = realqmode = FALSE;
782
783 while ((c = *p++) != '\0')
784 {
785 /*
786 ** If the buffer is overful, go into a special "skipping"
787 ** mode that tries to keep legal syntax but doesn't actually
788 ** output things.
789 */
790
791 skipping = bp >= buflim;
792
793 if (copylev > 0 && !skipping)
794 *bp++ = c;
795
796 /* check for backslash escapes */
797 if (c == '\\')
798 {
799 /* arrange to quote the address */
800 if (cmtlev <= 0 && !qmode)
801 quoteit = TRUE;
802
803 if ((c = *p++) == '\0')
804 {
805 /* too far */
806 p--;
807 goto putg;
808 }
809 if (copylev > 0 && !skipping)
810 *bp++ = c;
811 goto putg;
812 }
813
814 /* check for quoted strings */
815 if (c == '"' && cmtlev <= 0)
816 {
817 qmode = !qmode;
818 if (copylev > 0 && !skipping)
819 realqmode = !realqmode;
820 continue;
821 }
822 if (qmode)
823 goto putg;
824
825 /* check for comments */
826 if (c == '(')
827 {
828 cmtlev++;
829
830 /* allow space for closing paren */
831 if (!skipping)
832 {
833 buflim--;
834 realcmtlev++;
835 if (copylev++ <= 0)
836 {
837 *bp++ = ' ';
838 *bp++ = c;
839 }
840 }
841 }
842 if (cmtlev > 0)
843 {
844 if (c == ')')
845 {
846 cmtlev--;
847 copylev--;
848 if (!skipping)
849 {
850 realcmtlev--;
851 buflim++;
852 }
853 }
854 continue;
855 }
856 else if (c == ')')
857 {
858 /* syntax error: unmatched ) */
859 if (copylev > 0 && !skipping)
860 bp--;
861 }
862
863 /* check for group: list; syntax */
864 if (c == ':' && anglelev <= 0 && !gotcolon && !ColonOkInAddr)
865 {
866 register char *q;
867
868 if (*p == ':')
869 {
870 /* special case -- :: syntax */
871 if (cmtlev <= 0 && !qmode)
872 quoteit = TRUE;
873 if (copylev > 0 && !skipping)
874 {
875 *bp++ = c;
876 *bp++ = c;
877 }
878 p++;
879 goto putg;
880 }
881
882 gotcolon = TRUE;
883
884 bp = bufhead;
885 if (quoteit)
886 {
887 *bp++ = '"';
888
889 /* back up over the ':' and any spaces */
890 --p;
891 while (isascii(*--p) && isspace(*p))
892 continue;
893 p++;
894 }
895 for (q = addrhead; q < p; )
896 {
897 c = *q++;
898 if (bp < buflim)
899 {
900 if (quoteit && c == '"')
901 *bp++ = '\\';
902 *bp++ = c;
903 }
904 }
905 if (quoteit)
906 {
907 if (bp == &bufhead[1])
908 bp--;
909 else
910 *bp++ = '"';
911 while ((c = *p++) != ':')
912 {
913 if (bp < buflim)
914 *bp++ = c;
915 }
916 *bp++ = c;
917 }
918
919 /* any trailing white space is part of group: */
920 while (isascii(*p) && isspace(*p) && bp < buflim)
921 *bp++ = *p++;
922 copylev = 0;
923 putgmac = quoteit = FALSE;
924 bufhead = bp;
925 addrhead = p;
926 continue;
927 }
928
929 if (c == ';' && copylev <= 0 && !ColonOkInAddr)
930 {
931 if (bp < buflim)
932 *bp++ = c;
933 }
934
935 /* check for characters that may have to be quoted */
936 if (strchr(".'@,;:\\()[]", c) != NULL)
937 {
938 /*
939 ** If these occur as the phrase part of a <>
940 ** construct, but are not inside of () or already
941 ** quoted, they will have to be quoted. Note that
942 ** now (but don't actually do the quoting).
943 */
944
945 if (cmtlev <= 0 && !qmode)
946 quoteit = TRUE;
947 }
948
949 /* check for angle brackets */
950 if (c == '<')
951 {
952 register char *q;
953
954 /* assume first of two angles is bogus */
955 if (gotangle)
956 quoteit = TRUE;
957 gotangle = TRUE;
958
959 /* oops -- have to change our mind */
960 anglelev = 1;
961 if (!skipping)
962 realanglelev = 1;
963
964 bp = bufhead;
965 if (quoteit)
966 {
967 *bp++ = '"';
968
969 /* back up over the '<' and any spaces */
970 --p;
971 while (isascii(*--p) && isspace(*p))
972 continue;
973 p++;
974 }
975 for (q = addrhead; q < p; )
976 {
977 c = *q++;
978 if (bp < buflim)
979 {
980 if (quoteit && c == '"')
981 *bp++ = '\\';
982 *bp++ = c;
983 }
984 }
985 if (quoteit)
986 {
987 if (bp == &buf[1])
988 bp--;
989 else
990 *bp++ = '"';
991 while ((c = *p++) != '<')
992 {
993 if (bp < buflim)
994 *bp++ = c;
995 }
996 *bp++ = c;
997 }
998 copylev = 0;
999 putgmac = quoteit = FALSE;
1000 continue;
1001 }
1002
1003 if (c == '>')
1004 {
1005 if (anglelev > 0)
1006 {
1007 anglelev--;
1008 if (!skipping)
1009 {
1010 realanglelev--;
1011 buflim++;
1012 }
1013 }
1014 else if (!skipping)
1015 {
1016 /* syntax error: unmatched > */
1017 if (copylev > 0)
1018 bp--;
1019 quoteit = TRUE;
1020 continue;
1021 }
1022 if (copylev++ <= 0)
1023 *bp++ = c;
1024 continue;
1025 }
1026
1027 /* must be a real address character */
1028 putg:
1029 if (copylev <= 0 && !putgmac)
1030 {
1031 *bp++ = MACROEXPAND;
1032 *bp++ = 'g';
1033 putgmac = TRUE;
1034 }
1035 }
1036
1037 /* repair any syntactic damage */
1038 if (realqmode)
1039 *bp++ = '"';
1040 while (realcmtlev-- > 0)
1041 *bp++ = ')';
1042 while (realanglelev-- > 0)
1043 *bp++ = '>';
1044 *bp++ = '\0';
1045
1046 if (tTd(33, 1))
1047 printf("crackaddr=>`%s'\n", buf);
1048
1049 return (buf);
1050 }
1051 /*
1052 ** PUTHEADER -- put the header part of a message from the in-core copy
1053 **
1054 ** Parameters:
1055 ** mci -- the connection information.
1056 ** h -- the header to put.
1057 ** e -- envelope to use.
1058 **
1059 ** Returns:
1060 ** none.
1061 **
1062 ** Side Effects:
1063 ** none.
1064 */
1065
1066 /*
1067 * Macro for fast max (not available in e.g. DG/UX, 386/ix).
1068 */
1069 #ifndef MAX
1070 # define MAX(a,b) (((a)>(b))?(a):(b))
1071 #endif
1072
1073 void
putheader(mci,h,e)1074 putheader(mci, h, e)
1075 register MCI *mci;
1076 register HDR *h;
1077 register ENVELOPE *e;
1078 {
1079 char buf[MAX(MAXLINE,BUFSIZ)];
1080 char obuf[MAXLINE];
1081
1082 if (tTd(34, 1))
1083 printf("--- putheader, mailer = %s ---\n",
1084 mci->mci_mailer->m_name);
1085
1086 mci->mci_flags |= MCIF_INHEADER;
1087 for (; h != NULL; h = h->h_link)
1088 {
1089 register char *p = h->h_value;
1090 extern bool bitintersect();
1091
1092 if (tTd(34, 11))
1093 {
1094 printf(" %s: ", h->h_field);
1095 xputs(p);
1096 }
1097
1098 /* suppress Content-Transfer-Encoding: if we are MIMEing */
1099 if (bitset(H_CTE, h->h_flags) &&
1100 bitset(MCIF_CVT8TO7|MCIF_INMIME, mci->mci_flags))
1101 {
1102 if (tTd(34, 11))
1103 printf(" (skipped (content-transfer-encoding))\n");
1104 continue;
1105 }
1106
1107 if (bitset(MCIF_INMIME, mci->mci_flags))
1108 goto vanilla;
1109
1110 if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
1111 !bitintersect(h->h_mflags, mci->mci_mailer->m_flags))
1112 {
1113 if (tTd(34, 11))
1114 printf(" (skipped)\n");
1115 continue;
1116 }
1117
1118 /* handle Resent-... headers specially */
1119 if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
1120 {
1121 if (tTd(34, 11))
1122 printf(" (skipped (resent))\n");
1123 continue;
1124 }
1125
1126 /* suppress return receipts if requested */
1127 if (bitset(H_RECEIPTTO, h->h_flags) &&
1128 bitset(EF_NORECEIPT, e->e_flags))
1129 {
1130 if (tTd(34, 11))
1131 printf(" (skipped (receipt))\n");
1132 continue;
1133 }
1134
1135 /* macro expand value if generated internally */
1136 if (bitset(H_DEFAULT, h->h_flags))
1137 {
1138 expand(p, buf, sizeof buf, e);
1139 p = buf;
1140 if (p == NULL || *p == '\0')
1141 {
1142 if (tTd(34, 11))
1143 printf(" (skipped -- null value)\n");
1144 continue;
1145 }
1146 }
1147
1148 if (tTd(34, 11))
1149 printf("\n");
1150
1151 if (bitset(H_STRIPVAL, h->h_flags))
1152 {
1153 /* empty field */
1154 (void) sprintf(obuf, "%s:", h->h_field);
1155 putline(obuf, mci);
1156 }
1157 else if (bitset(H_FROM|H_RCPT, h->h_flags))
1158 {
1159 /* address field */
1160 bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
1161
1162 if (bitset(H_FROM, h->h_flags))
1163 oldstyle = FALSE;
1164 commaize(h, p, oldstyle, mci, e);
1165 }
1166 else
1167 {
1168 /* vanilla header line */
1169 register char *nlp;
1170
1171 vanilla:
1172 (void) sprintf(obuf, "%s: ", h->h_field);
1173 while ((nlp = strchr(p, '\n')) != NULL)
1174 {
1175 *nlp = '\0';
1176 (void) strcat(obuf, p);
1177 *nlp = '\n';
1178 putline(obuf, mci);
1179 p = ++nlp;
1180 obuf[0] = '\0';
1181 }
1182 (void) strcat(obuf, p);
1183 putline(obuf, mci);
1184 }
1185 }
1186
1187 /*
1188 ** If we are converting this to a MIME message, add the
1189 ** MIME headers.
1190 */
1191
1192 #if MIME8TO7
1193 if (bitset(MM_MIME8BIT, MimeMode) &&
1194 bitset(EF_HAS8BIT, e->e_flags) &&
1195 !bitset(EF_DONT_MIME, e->e_flags) &&
1196 !bitnset(M_8BITS, mci->mci_mailer->m_flags) &&
1197 !bitset(MCIF_CVT8TO7, mci->mci_flags))
1198 {
1199 if (hvalue("MIME-Version", e->e_header) == NULL)
1200 putline("MIME-Version: 1.0", mci);
1201 if (hvalue("Content-Type", e->e_header) == NULL)
1202 {
1203 sprintf(obuf, "Content-Type: text/plain; charset=%s",
1204 defcharset(e));
1205 putline(obuf, mci);
1206 }
1207 if (hvalue("Content-Transfer-Encoding", e->e_header) == NULL)
1208 putline("Content-Transfer-Encoding: 8bit", mci);
1209 }
1210 #endif
1211 }
1212 /*
1213 ** COMMAIZE -- output a header field, making a comma-translated list.
1214 **
1215 ** Parameters:
1216 ** h -- the header field to output.
1217 ** p -- the value to put in it.
1218 ** oldstyle -- TRUE if this is an old style header.
1219 ** mci -- the connection information.
1220 ** e -- the envelope containing the message.
1221 **
1222 ** Returns:
1223 ** none.
1224 **
1225 ** Side Effects:
1226 ** outputs "p" to file "fp".
1227 */
1228
1229 void
commaize(h,p,oldstyle,mci,e)1230 commaize(h, p, oldstyle, mci, e)
1231 register HDR *h;
1232 register char *p;
1233 bool oldstyle;
1234 register MCI *mci;
1235 register ENVELOPE *e;
1236 {
1237 register char *obp;
1238 int opos;
1239 int omax;
1240 bool firstone = TRUE;
1241 char obuf[MAXLINE + 3];
1242
1243 /*
1244 ** Output the address list translated by the
1245 ** mailer and with commas.
1246 */
1247
1248 if (tTd(14, 2))
1249 printf("commaize(%s: %s)\n", h->h_field, p);
1250
1251 obp = obuf;
1252 (void) sprintf(obp, "%s: ", h->h_field);
1253 opos = strlen(h->h_field) + 2;
1254 obp += opos;
1255 omax = mci->mci_mailer->m_linelimit - 2;
1256 if (omax < 0 || omax > 78)
1257 omax = 78;
1258
1259 /*
1260 ** Run through the list of values.
1261 */
1262
1263 while (*p != '\0')
1264 {
1265 register char *name;
1266 register int c;
1267 char savechar;
1268 int flags;
1269 auto int stat;
1270
1271 /*
1272 ** Find the end of the name. New style names
1273 ** end with a comma, old style names end with
1274 ** a space character. However, spaces do not
1275 ** necessarily delimit an old-style name -- at
1276 ** signs mean keep going.
1277 */
1278
1279 /* find end of name */
1280 while ((isascii(*p) && isspace(*p)) || *p == ',')
1281 p++;
1282 name = p;
1283 for (;;)
1284 {
1285 auto char *oldp;
1286 char pvpbuf[PSBUFSIZE];
1287
1288 (void) prescan(p, oldstyle ? ' ' : ',', pvpbuf,
1289 sizeof pvpbuf, &oldp, NULL);
1290 p = oldp;
1291
1292 /* look to see if we have an at sign */
1293 while (*p != '\0' && isascii(*p) && isspace(*p))
1294 p++;
1295
1296 if (*p != '@')
1297 {
1298 p = oldp;
1299 break;
1300 }
1301 p += *p == '@' ? 1 : 2;
1302 while (*p != '\0' && isascii(*p) && isspace(*p))
1303 p++;
1304 }
1305 /* at the end of one complete name */
1306
1307 /* strip off trailing white space */
1308 while (p >= name &&
1309 ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
1310 p--;
1311 if (++p == name)
1312 continue;
1313 savechar = *p;
1314 *p = '\0';
1315
1316 /* translate the name to be relative */
1317 flags = RF_HEADERADDR|RF_ADDDOMAIN;
1318 if (bitset(H_FROM, h->h_flags))
1319 flags |= RF_SENDERADDR;
1320 #if USERDB
1321 else if (e->e_from.q_mailer != NULL &&
1322 bitnset(M_UDBRECIPIENT, e->e_from.q_mailer->m_flags))
1323 {
1324 extern char *udbsender();
1325
1326 name = udbsender(name);
1327 }
1328 #endif
1329 stat = EX_OK;
1330 name = remotename(name, mci->mci_mailer, flags, &stat, e);
1331 if (*name == '\0')
1332 {
1333 *p = savechar;
1334 continue;
1335 }
1336
1337 /* output the name with nice formatting */
1338 opos += strlen(name);
1339 if (!firstone)
1340 opos += 2;
1341 if (opos > omax && !firstone)
1342 {
1343 (void) strcpy(obp, ",\n");
1344 putline(obuf, mci);
1345 obp = obuf;
1346 (void) strcpy(obp, " ");
1347 opos = strlen(obp);
1348 obp += opos;
1349 opos += strlen(name);
1350 }
1351 else if (!firstone)
1352 {
1353 (void) strcpy(obp, ", ");
1354 obp += 2;
1355 }
1356
1357 while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
1358 *obp++ = c;
1359 firstone = FALSE;
1360 *p = savechar;
1361 }
1362 (void) strcpy(obp, "\n");
1363 putline(obuf, mci);
1364 }
1365 /*
1366 ** COPYHEADER -- copy header list
1367 **
1368 ** This routine is the equivalent of newstr for header lists
1369 **
1370 ** Parameters:
1371 ** header -- list of header structures to copy.
1372 **
1373 ** Returns:
1374 ** a copy of 'header'.
1375 **
1376 ** Side Effects:
1377 ** none.
1378 */
1379
1380 HDR *
copyheader(header)1381 copyheader(header)
1382 register HDR *header;
1383 {
1384 register HDR *newhdr;
1385 HDR *ret;
1386 register HDR **tail = &ret;
1387
1388 while (header != NULL)
1389 {
1390 newhdr = (HDR *) xalloc(sizeof(HDR));
1391 STRUCTCOPY(*header, *newhdr);
1392 *tail = newhdr;
1393 tail = &newhdr->h_link;
1394 header = header->h_link;
1395 }
1396 *tail = NULL;
1397
1398 return ret;
1399 }
1400