1 /*****************************************************************************
4 rfc822.c -- code for slicing and dicing RFC822 mail headers
7 nextaddr() -- parse the next address out of an RFC822 header
8 reply_hack() -- append hostname to local header addresses
11 How to parse RFC822 headers in C. This is not a fully conformant
12 implementation of RFC822 or RFC2822, but it has been in production use
13 in a widely-deployed MTA (fetcmail) since 1996 without complaints.
14 Really perverse combinations of quoting and commenting could break it.
17 Eric S. Raymond <esr@thyrsus.com>, 1997. This source code example
18 is part of fetchmail and the Unix Cookbook, and are released under the
19 MIT license. Compile with -DMAIN to build the demonstrator.
21 ******************************************************************************/
27 #include "fetchmail.h"
34 const char *program_name = "rfc822";
42 #define HEADER_END(p) ((p)[0] == '\n' && ((p)[1] != ' ' && (p)[1] != '\t'))
44 #define BEFORE_EOL(s) strcspn((s), "\r\n"))
47 char *buf /* header to be hacked */,
48 const char *host /* server hostname */,
50 /* hack message headers so replies will work properly */
52 char *from, *cp, last_nws = '\0', *parens_from = NULL;
53 int parendepth, state, has_bare_name_part, has_host_part;
58 if (strncasecmp("From:", buf, 5)
59 && strncasecmp("To:", buf, 3)
60 && strncasecmp("Reply-To:", buf, 9)
61 && strncasecmp("Return-Path:", buf, 12)
62 && strncasecmp("Cc:", buf, 3)
63 && strncasecmp("Bcc:", buf, 4)
64 && strncasecmp("Resent-From:", buf, 12)
65 && strncasecmp("Resent-To:", buf, 10)
66 && strncasecmp("Resent-Cc:", buf, 10)
67 && strncasecmp("Resent-Bcc:", buf, 11)
68 && strncasecmp("Apparently-From:", buf, 16)
69 && strncasecmp("Apparently-To:", buf, 14)
70 && strncasecmp("Sender:", buf, 7)
71 && strncasecmp("Resent-Sender:", buf, 14)
77 if (outlevel >= O_DEBUG)
78 report_build(stdout, GT_("About to rewrite %.*s...\n"),
79 (int)BEFORE_EOL(buf), buf);
81 /* make room to hack the address; buf must be malloced */
82 for (cp = buf; *cp; cp++)
83 if (*cp == ',' || isspace((unsigned char)*cp))
85 buf = (char *)xrealloc(buf, strlen(buf) + addresscount * (strlen(host) + 1) + 1);
89 * This is going to foo up on some ill-formed addresses.
90 * Note that we don't rewrite the fake address <> in order to
91 * avoid screwing up bounce suppression with a null Return-Path.
94 parendepth = state = 0;
95 has_host_part = has_bare_name_part = FALSE;
96 for (from = buf; *from; from++)
101 printf("state %d: %s", state, buf);
102 printf("%*s^\n", (int)(from - buf + 10), " ");
109 else if (*from == ')')
113 if (!parendepth && !has_host_part)
116 case 0: /* before header colon */
121 case 1: /* we've seen the colon, we're looking for addresses */
122 if (!isspace((unsigned char)*from))
126 else if (*from == '@' || *from == '!')
127 has_host_part = TRUE;
128 else if (*from == '"')
131 * Not expanding on last non-WS == ';' deals with groupnames,
132 * an obscure misfeature described in sections
133 * 6.1, 6.2.6, and A.1.5 of the RFC822 standard.
135 else if ((*from == ',' || HEADER_END(from))
136 && has_bare_name_part
146 while (isspace((unsigned char)*from) || (*from == ','))
149 hostlen = strlen(host);
150 for (cp = from + strlen(from); cp >= from; --cp)
153 memcpy(from, host, hostlen);
154 from = p + hostlen + 1;
155 has_host_part = TRUE;
157 else if (from[1] == '('
158 && has_bare_name_part
160 && last_nws != ';' && last_nws != ')')
164 else if (!isspace((unsigned char)*from))
165 has_bare_name_part = TRUE;
168 case 2: /* we're in a string */
175 for (bp = from - 1; *bp == '\\'; bp--)
182 case 3: /* we're in a <>-enclosed address */
183 if (*from == '@' || *from == '!')
184 has_host_part = TRUE;
185 else if (*from == '>' && (from > buf && from[-1] != '<'))
192 hostlen = strlen(host);
193 for (cp = from + strlen(from); cp >= from; --cp)
196 memcpy(from, host, hostlen);
198 has_host_part = TRUE;
205 * If we passed a comma, reset everything.
207 if ((from > buf && from[-1] == ',') && !parendepth) {
208 has_host_part = has_bare_name_part = FALSE;
214 if (outlevel >= O_DEBUG)
215 report_complete(stdout, GT_("...rewritten version is %.*s.\n"),
216 (int)BEFORE_EOL(buf), buf);
218 *length = strlen(buf);
222 char *nxtaddr(const char *hdr /* header to be parsed, NUL to continue previous hdr */)
223 /* parse addresses in succession out of a specified RFC822 header */
225 static char address[BUFSIZ];
227 static const char *hp;
228 static int state, oldstate;
230 static const char *orighdr;
234 #define START_HDR 0 /* before header colon */
235 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
236 #define BARE_ADDRESS 2 /* collecting address without delimiters */
237 #define INSIDE_DQUOTE 3 /* inside double quotes */
238 #define INSIDE_PARENS 4 /* inside parentheses */
239 #define INSIDE_BRACKETS 5 /* inside bracketed address */
240 #define ENDIT_ALL 6 /* after last address */
242 #define NEXTTP() ((tp < sizeof(address)-1) ? tp++ : tp)
254 if (!hp) return NULL;
261 printf("state %d: %s", state, orighdr);
262 printf("%*s^\n", (int)(hp - orighdr + 10), " ");
266 if (state == ENDIT_ALL) /* after last address */
268 else if (HEADER_END(hp))
273 while (tp > 0 && isspace((unsigned char)address[tp - 1]))
281 else if (*hp == '\\') /* handle RFC822 escaping */
283 if (state != INSIDE_PARENS)
285 address[NEXTTP()] = *hp++; /* take the escape */
286 address[NEXTTP()] = *hp; /* take following unsigned char */
291 case START_HDR: /* before header colon */
296 case SKIP_JUNK: /* looking for address start */
297 if (*hp == '"') /* quoted string */
299 oldstate = SKIP_JUNK;
300 state = INSIDE_DQUOTE;
301 address[NEXTTP()] = *hp;
303 else if (*hp == '(') /* address comment -- ignore */
306 oldstate = SKIP_JUNK;
307 state = INSIDE_PARENS;
309 else if (*hp == '<') /* begin <address> */
311 state = INSIDE_BRACKETS;
314 else if (*hp != ',' && !isspace((unsigned char)*hp))
317 state = BARE_ADDRESS;
321 case BARE_ADDRESS: /* collecting address without delimiters */
322 if (*hp == ',') /* end of address */
326 address[NEXTTP()] = '\0';
332 else if (*hp == '(') /* beginning of comment */
335 oldstate = BARE_ADDRESS;
336 state = INSIDE_PARENS;
338 else if (*hp == '<') /* beginning of real address */
340 state = INSIDE_BRACKETS;
343 else if (*hp == '"') /* quoted word, copy verbatim */
346 state = INSIDE_DQUOTE;
347 address[NEXTTP()] = *hp;
349 else if (!isspace((unsigned char)*hp)) /* just take it, ignoring whitespace */
350 address[NEXTTP()] = *hp;
353 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
354 address[NEXTTP()] = *hp;
359 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
368 case INSIDE_BRACKETS: /* possible <>-enclosed address */
369 if (*hp == '>') /* end of address */
371 address[NEXTTP()] = '\0';
377 else if (*hp == '<') /* nested <> */
379 else if (*hp == '"') /* quoted address */
381 address[NEXTTP()] = *hp;
382 oldstate = INSIDE_BRACKETS;
383 state = INSIDE_DQUOTE;
385 else /* just copy address */
386 address[NEXTTP()] = *hp;
395 static void parsebuf(char *longbuf, int reply)
402 reply_hack(longbuf, "HOSTNAME.NET", &dummy);
403 printf("Rewritten buffer: %s", (char *)longbuf);
406 if ((cp = nxtaddr(longbuf)) != (char *)NULL)
408 printf("\t-> \"%s\"\n", (char *)cp);
410 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
415 int main(int argc, char *argv[])
417 char buf[BUFSIZ], longbuf[BUFSIZ];
420 verbose = reply = FALSE;
421 while ((ch = getopt(argc, argv, "rv")) != EOF)
433 while (fgets(buf, sizeof(buf)-1, stdin))
435 if (buf[0] == ' ' || buf[0] == '\t')
436 strlcat(longbuf, buf, sizeof(longbuf));
437 else if (!strncasecmp("From: ", buf, 6)
438 || !strncasecmp("To: ", buf, 4)
439 || !strncasecmp("Reply-", buf, 6)
440 || !strncasecmp("Cc: ", buf, 4)
441 || !strncasecmp("Bcc: ", buf, 5))
442 strlcpy(longbuf, buf, sizeof(longbuf));
446 fputs(longbuf, stdout);
447 parsebuf(longbuf, reply);
454 fputs(longbuf, stdout);
455 parsebuf(longbuf, reply);