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 ******************************************************************************/
31 #include "fetchmail.h"
39 const char *program_name = "rfc822";
47 #define HEADER_END(p) ((p)[0] == '\n' && ((p)[1] != ' ' && (p)[1] != '\t'))
49 #define BEFORE_EOL(s) (strcspn((s), "\r\n"))
52 char *buf /* header to be hacked */,
53 const char *host /* server hostname */,
55 /* hack message headers so replies will work properly */
57 char *from, *cp, last_nws = '\0', *parens_from = NULL;
58 int parendepth, state, has_bare_name_part, has_host_part;
63 if (strncasecmp("From:", buf, 5)
64 && strncasecmp("To:", buf, 3)
65 && strncasecmp("Reply-To:", buf, 9)
66 && strncasecmp("Return-Path:", buf, 12)
67 && strncasecmp("Cc:", buf, 3)
68 && strncasecmp("Bcc:", buf, 4)
69 && strncasecmp("Resent-From:", buf, 12)
70 && strncasecmp("Resent-To:", buf, 10)
71 && strncasecmp("Resent-Cc:", buf, 10)
72 && strncasecmp("Resent-Bcc:", buf, 11)
73 && strncasecmp("Apparently-From:", buf, 16)
74 && strncasecmp("Apparently-To:", buf, 14)
75 && strncasecmp("Sender:", buf, 7)
76 && strncasecmp("Resent-Sender:", buf, 14)
82 if (outlevel >= O_DEBUG) {
83 report_build(stdout, GT_("About to rewrite %s...\n"), (cp = sdump(buf, BEFORE_EOL(buf))));
87 /* make room to hack the address; buf must be malloced */
88 for (cp = buf; *cp; cp++)
89 if (*cp == ',' || isspace((unsigned char)*cp))
91 buf = (char *)xrealloc(buf, strlen(buf) + addresscount * (strlen(host) + 1) + 1);
95 * This is going to foo up on some ill-formed addresses.
96 * Note that we don't rewrite the fake address <> in order to
97 * avoid screwing up bounce suppression with a null Return-Path.
100 parendepth = state = 0;
101 has_host_part = has_bare_name_part = FALSE;
102 for (from = buf; *from; from++)
107 printf("state %d: %s", state, buf);
108 printf("%*s^\n", (int)(from - buf + 10), " ");
115 else if (*from == ')')
119 if (!parendepth && !has_host_part)
122 case 0: /* before header colon */
127 case 1: /* we've seen the colon, we're looking for addresses */
128 if (!isspace((unsigned char)*from))
132 else if (*from == '@' || *from == '!')
133 has_host_part = TRUE;
134 else if (*from == '"')
137 * Not expanding on last non-WS == ';' deals with groupnames,
138 * an obscure misfeature described in sections
139 * 6.1, 6.2.6, and A.1.5 of the RFC822 standard.
141 else if ((*from == ',' || HEADER_END(from))
142 && has_bare_name_part
152 while (isspace((unsigned char)*from) || (*from == ','))
155 hostlen = strlen(host);
156 for (cp = from + strlen(from); cp >= from; --cp)
159 memcpy(from, host, hostlen);
160 from = p + hostlen + 1;
161 has_host_part = TRUE;
163 else if (from[1] == '('
164 && has_bare_name_part
166 && last_nws != ';' && last_nws != ')')
170 else if (!isspace((unsigned char)*from))
171 has_bare_name_part = TRUE;
174 case 2: /* we're in a string */
181 for (bp = from - 1; *bp == '\\'; bp--)
188 case 3: /* we're in a <>-enclosed address */
189 if (*from == '@' || *from == '!')
190 has_host_part = TRUE;
191 else if (*from == '>' && (from > buf && from[-1] != '<'))
198 hostlen = strlen(host);
199 for (cp = from + strlen(from); cp >= from; --cp)
202 memcpy(from, host, hostlen);
204 has_host_part = TRUE;
211 * If we passed a comma, reset everything.
213 if ((from > buf && from[-1] == ',') && !parendepth) {
214 has_host_part = has_bare_name_part = FALSE;
220 if (outlevel >= O_DEBUG) {
221 report_complete(stdout, GT_("...rewritten version is %s.\n"),
222 (cp = sdump(buf, BEFORE_EOL(buf))));
227 *length = strlen(buf);
231 char *nxtaddr(const char *hdr /* header to be parsed, NUL to continue previous hdr */)
232 /* parse addresses in succession out of a specified RFC822 header */
234 static char address[BUFSIZ];
236 static const char *hp;
237 static int state, oldstate;
239 static const char *orighdr;
243 #define START_HDR 0 /* before header colon */
244 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
245 #define BARE_ADDRESS 2 /* collecting address without delimiters */
246 #define INSIDE_DQUOTE 3 /* inside double quotes */
247 #define INSIDE_PARENS 4 /* inside parentheses */
248 #define INSIDE_BRACKETS 5 /* inside bracketed address */
249 #define ENDIT_ALL 6 /* after last address */
251 #define NEXTTP() ((tp < sizeof(address)-1) ? tp++ : tp)
263 if (!hp) return NULL;
270 printf("state %d: %s", state, orighdr);
271 printf("%*s^\n", (int)(hp - orighdr + 10), " ");
275 if (state == ENDIT_ALL) /* after last address */
277 else if (HEADER_END(hp))
282 while (tp > 0 && isspace((unsigned char)address[tp - 1]))
290 else if (*hp == '\\') /* handle RFC822 escaping */
292 if (state != INSIDE_PARENS)
294 address[NEXTTP()] = *hp++; /* take the escape */
295 address[NEXTTP()] = *hp; /* take following unsigned char */
300 case START_HDR: /* before header colon */
305 case SKIP_JUNK: /* looking for address start */
306 if (*hp == '"') /* quoted string */
308 oldstate = SKIP_JUNK;
309 state = INSIDE_DQUOTE;
310 address[NEXTTP()] = *hp;
312 else if (*hp == '(') /* address comment -- ignore */
315 oldstate = SKIP_JUNK;
316 state = INSIDE_PARENS;
318 else if (*hp == '<') /* begin <address> */
320 state = INSIDE_BRACKETS;
323 else if (*hp != ',' && !isspace((unsigned char)*hp))
326 state = BARE_ADDRESS;
330 case BARE_ADDRESS: /* collecting address without delimiters */
331 if (*hp == ',') /* end of address */
335 address[NEXTTP()] = '\0';
341 else if (*hp == '(') /* beginning of comment */
344 oldstate = BARE_ADDRESS;
345 state = INSIDE_PARENS;
347 else if (*hp == '<') /* beginning of real address */
349 state = INSIDE_BRACKETS;
352 else if (*hp == '"') /* quoted word, copy verbatim */
355 state = INSIDE_DQUOTE;
356 address[NEXTTP()] = *hp;
358 else if (!isspace((unsigned char)*hp)) /* just take it, ignoring whitespace */
359 address[NEXTTP()] = *hp;
362 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
363 address[NEXTTP()] = *hp;
368 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
377 case INSIDE_BRACKETS: /* possible <>-enclosed address */
378 if (*hp == '>') /* end of address */
380 address[NEXTTP()] = '\0';
386 else if (*hp == '<') /* nested <> */
388 else if (*hp == '"') /* quoted address */
390 address[NEXTTP()] = *hp;
391 oldstate = INSIDE_BRACKETS;
392 state = INSIDE_DQUOTE;
394 else /* just copy address */
395 address[NEXTTP()] = *hp;
404 static void parsebuf(char *longbuf, int reply)
411 reply_hack(longbuf, "HOSTNAME.NET", &dummy);
412 printf("Rewritten buffer: %s", (char *)longbuf);
415 if ((cp = nxtaddr(longbuf)) != (char *)NULL)
417 printf("\t-> \"%s\"\n", (char *)cp);
419 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
424 int main(int argc, char *argv[])
426 char buf[BUFSIZ], longbuf[BUFSIZ];
429 verbose = reply = FALSE;
430 while ((ch = getopt(argc, argv, "rv")) != EOF)
442 while (fgets(buf, sizeof(buf)-1, stdin))
444 if (buf[0] == ' ' || buf[0] == '\t')
445 strlcat(longbuf, buf, sizeof(longbuf));
446 else if (!strncasecmp("From: ", buf, 6)
447 || !strncasecmp("To: ", buf, 4)
448 || !strncasecmp("Reply-", buf, 6)
449 || !strncasecmp("Cc: ", buf, 4)
450 || !strncasecmp("Bcc: ", buf, 5))
451 strlcpy(longbuf, buf, sizeof(longbuf));
455 fputs(longbuf, stdout);
456 parsebuf(longbuf, reply);
463 fputs(longbuf, stdout);
464 parsebuf(longbuf, reply);