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 char *program_name = "rfc822";
42 #define HEADER_END(p) ((p)[0] == '\n' && ((p)[1] != ' ' && (p)[1] != '\t'))
44 unsigned char *reply_hack(
45 unsigned char *buf /* header to be hacked */,
46 const unsigned char *host /* server hostname */,
48 /* hack message headers so replies will work properly */
50 unsigned char *from, *cp, last_nws = '\0', *parens_from = NULL;
51 int parendepth, state, has_bare_name_part, has_host_part;
56 if (strncasecmp("From:", buf, 5)
57 && strncasecmp("To:", buf, 3)
58 && strncasecmp("Reply-To:", buf, 9)
59 && strncasecmp("Return-Path:", buf, 12)
60 && strncasecmp("Cc:", buf, 3)
61 && strncasecmp("Bcc:", buf, 4)
62 && strncasecmp("Resent-From:", buf, 12)
63 && strncasecmp("Resent-To:", buf, 10)
64 && strncasecmp("Resent-Cc:", buf, 10)
65 && strncasecmp("Resent-Bcc:", buf, 11)
66 && strncasecmp("Apparently-From:", buf, 16)
67 && strncasecmp("Apparently-To:", buf, 14)
68 && strncasecmp("Sender:", buf, 7)
69 && strncasecmp("Resent-Sender:", buf, 14)
75 if (outlevel >= O_DEBUG)
76 report_build(stdout, GT_("About to rewrite %s"), buf);
78 /* make room to hack the address; buf must be malloced */
79 for (cp = buf; *cp; cp++)
80 if (*cp == ',' || isspace(*cp))
82 buf = (unsigned char *)xrealloc(buf, strlen(buf) + addresscount * (strlen(host) + 1) + 1);
86 * This is going to foo up on some ill-formed addresses.
87 * Note that we don't rewrite the fake address <> in order to
88 * avoid screwing up bounce suppression with a null Return-Path.
91 parendepth = state = 0;
92 has_host_part = has_bare_name_part = FALSE;
93 for (from = buf; *from; from++)
98 printf("state %d: %s", state, (char *)buf);
99 printf("%*s^\n", from - buf + 10, " ");
106 else if (*from == ')')
110 if (!parendepth && !has_host_part)
113 case 0: /* before header colon */
118 case 1: /* we've seen the colon, we're looking for addresses */
123 else if (*from == '@' || *from == '!')
124 has_host_part = TRUE;
125 else if (*from == '"')
128 * Not expanding on last non-WS == ';' deals with groupnames,
129 * an obscure misfeature described in sections
130 * 6.1, 6.2.6, and A.1.5 of the RFC822 standard.
132 else if ((*from == ',' || HEADER_END(from))
133 && has_bare_name_part
143 while (isspace(*from) || (*from == ','))
146 hostlen = strlen(host);
147 for (cp = from + strlen(from); cp >= from; --cp)
150 memcpy(from, host, hostlen);
151 from = p + hostlen + 1;
152 has_host_part = TRUE;
154 else if (from[1] == '('
155 && has_bare_name_part
157 && last_nws != ';' && last_nws != ')')
161 else if (!isspace(*from))
162 has_bare_name_part = TRUE;
165 case 2: /* we're in a string */
172 for (bp = from - 1; *bp == '\\'; bp--)
179 case 3: /* we're in a <>-enclosed address */
180 if (*from == '@' || *from == '!')
181 has_host_part = TRUE;
182 else if (*from == '>' && (from > buf && from[-1] != '<'))
189 hostlen = strlen(host);
190 for (cp = from + strlen(from); cp >= from; --cp)
193 memcpy(from, host, hostlen);
195 has_host_part = TRUE;
202 * If we passed a comma, reset everything.
204 if ((from > buf && from[-1] == ',') && !parendepth) {
205 has_host_part = has_bare_name_part = FALSE;
211 if (outlevel >= O_DEBUG)
212 report_complete(stdout, GT_("Rewritten version is %s\n"), buf);
214 *length = strlen(buf);
218 unsigned char *nxtaddr(const unsigned char *hdr /* header to be parsed, NUL to continue previous hdr */)
219 /* parse addresses in succession out of a specified RFC822 header */
221 static unsigned char address[BUFSIZ];
223 static const unsigned char *hp;
224 static int state, oldstate;
226 static const unsigned char *orighdr;
230 #define START_HDR 0 /* before header colon */
231 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
232 #define BARE_ADDRESS 2 /* collecting address without delimiters */
233 #define INSIDE_DQUOTE 3 /* inside double quotes */
234 #define INSIDE_PARENS 4 /* inside parentheses */
235 #define INSIDE_BRACKETS 5 /* inside bracketed address */
236 #define ENDIT_ALL 6 /* after last address */
238 #define NEXTTP() ((tp < sizeof(address)-1) ? tp++ : tp)
255 printf("state %d: %s", state, (char *)orighdr);
256 printf("%*s^\n", hp - orighdr + 10, " ");
260 if (state == ENDIT_ALL) /* after last address */
262 else if (HEADER_END(hp))
268 while (tp >= 0 && isspace(address[tp--]))
270 address[++tp] = '\0';
274 return((unsigned char *)NULL);
276 else if (*hp == '\\') /* handle RFC822 escaping */
278 if (state != INSIDE_PARENS)
280 address[NEXTTP()] = *hp++; /* take the escape */
281 address[NEXTTP()] = *hp; /* take following unsigned char */
286 case START_HDR: /* before header colon */
291 case SKIP_JUNK: /* looking for address start */
292 if (*hp == '"') /* quoted string */
294 oldstate = SKIP_JUNK;
295 state = INSIDE_DQUOTE;
296 address[NEXTTP()] = *hp;
298 else if (*hp == '(') /* address comment -- ignore */
301 oldstate = SKIP_JUNK;
302 state = INSIDE_PARENS;
304 else if (*hp == '<') /* begin <address> */
306 state = INSIDE_BRACKETS;
309 else if (*hp != ',' && !isspace(*hp))
312 state = BARE_ADDRESS;
316 case BARE_ADDRESS: /* collecting address without delimiters */
317 if (*hp == ',') /* end of address */
321 address[NEXTTP()] = '\0';
327 else if (*hp == '(') /* beginning of comment */
330 oldstate = BARE_ADDRESS;
331 state = INSIDE_PARENS;
333 else if (*hp == '<') /* beginning of real address */
335 state = INSIDE_BRACKETS;
338 else if (*hp == '"') /* quoted word, copy verbatim */
341 state = INSIDE_DQUOTE;
342 address[NEXTTP()] = *hp;
344 else if (!isspace(*hp)) /* just take it, ignoring whitespace */
345 address[NEXTTP()] = *hp;
348 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
350 address[NEXTTP()] = *hp;
353 address[NEXTTP()] = *hp;
358 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
367 case INSIDE_BRACKETS: /* possible <>-enclosed address */
368 if (*hp == '>') /* end of address */
370 address[NEXTTP()] = '\0';
376 else if (*hp == '<') /* nested <> */
378 else if (*hp == '"') /* quoted address */
380 address[NEXTTP()] = *hp;
381 oldstate = INSIDE_BRACKETS;
382 state = INSIDE_DQUOTE;
384 else /* just copy address */
385 address[NEXTTP()] = *hp;
394 static void parsebuf(unsigned char *longbuf, int reply)
401 reply_hack(longbuf, "HOSTNAME.NET", &dummy);
402 printf("Rewritten buffer: %s", (char *)longbuf);
405 if ((cp = nxtaddr(longbuf)) != (unsigned char *)NULL)
407 printf("\t-> \"%s\"\n", (char *)cp);
409 ((cp = nxtaddr((unsigned char *)NULL)) != (unsigned char *)NULL);
414 int main(int argc, char *argv[])
416 unsigned char buf[BUFSIZ], longbuf[BUFSIZ];
419 verbose = reply = FALSE;
420 while ((ch = getopt(argc, argv, "rv")) != EOF)
432 while (fgets(buf, sizeof(buf)-1, stdin))
434 if (buf[0] == ' ' || buf[0] == '\t')
435 strcat(longbuf, buf);
436 else if (!strncasecmp("From: ", buf, 6)
437 || !strncasecmp("To: ", buf, 4)
438 || !strncasecmp("Reply-", buf, 6)
439 || !strncasecmp("Cc: ", buf, 4)
440 || !strncasecmp("Bcc: ", buf, 5))
441 strcpy(longbuf, buf);
445 fputs(longbuf, stdout);
446 parsebuf(longbuf, reply);
453 fputs(longbuf, stdout);
454 parsebuf(longbuf, reply);