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 ******************************************************************************/
28 #include "fetchmail.h"
32 char *program_name = "rfc822";
40 #define HEADER_END(p) ((p)[0] == '\n' && ((p)[1] != ' ' && (p)[1] != '\t'))
42 unsigned char *reply_hack(buf, host)
43 /* hack message headers so replies will work properly */
44 unsigned char *buf; /* header to be hacked */
45 const unsigned char *host; /* server hostname */
47 unsigned char *from, *cp, last_nws = '\0', *parens_from = NULL;
48 int parendepth, state, has_bare_name_part, has_host_part;
53 if (strncasecmp("From:", buf, 5)
54 && strncasecmp("To:", buf, 3)
55 && strncasecmp("Reply-To:", buf, 9)
56 && strncasecmp("Return-Path:", buf, 12)
57 && strncasecmp("Cc:", buf, 3)
58 && strncasecmp("Bcc:", buf, 4)
59 && strncasecmp("Resent-From:", buf, 12)
60 && strncasecmp("Resent-To:", buf, 10)
61 && strncasecmp("Resent-Cc:", buf, 10)
62 && strncasecmp("Resent-Bcc:", buf, 11)
63 && strncasecmp("Apparently-From:", buf, 16)
64 && strncasecmp("Apparently-To:", buf, 14)
65 && strncasecmp("Sender:", buf, 7)
66 && strncasecmp("Resent-Sender:", buf, 14)
72 if (outlevel >= O_DEBUG)
73 report_build(stdout, GT_("About to rewrite %s"), buf);
75 /* make room to hack the address; buf must be malloced */
76 for (cp = buf; *cp; cp++)
77 if (*cp == ',' || isspace(*cp))
79 buf = (unsigned char *)xrealloc(buf, strlen(buf) + addresscount * (strlen(host) + 1) + 1);
83 * This is going to foo up on some ill-formed addresses.
84 * Note that we don't rewrite the fake address <> in order to
85 * avoid screwing up bounce suppression with a null Return-Path.
88 parendepth = state = 0;
89 has_host_part = has_bare_name_part = FALSE;
90 for (from = buf; *from; from++)
95 printf("state %d: %s", state, buf);
96 printf("%*s^\n", from - buf + 10, " ");
103 else if (*from == ')')
107 if (!parendepth && !has_host_part)
110 case 0: /* before header colon */
115 case 1: /* we've seen the colon, we're looking for addresses */
120 else if (*from == '@' || *from == '!')
121 has_host_part = TRUE;
122 else if (*from == '"')
125 * Not expanding on last non-WS == ';' deals with groupnames,
126 * an obscure misfeature described in sections
127 * 6.1, 6.2.6, and A.1.5 of the RFC822 standard.
129 else if ((*from == ',' || HEADER_END(from))
130 && has_bare_name_part
140 while (isspace(*from) || (*from == ','))
143 hostlen = strlen(host);
144 for (cp = from + strlen(from); cp >= from; --cp)
147 memcpy(from, host, hostlen);
148 from = p + hostlen + 1;
149 has_host_part = TRUE;
151 else if (from[1] == '('
152 && has_bare_name_part
154 && last_nws != ';' && last_nws != ')')
158 else if (!isspace(*from))
159 has_bare_name_part = TRUE;
162 case 2: /* we're in a string */
169 for (bp = from - 1; *bp == '\\'; bp--)
176 case 3: /* we're in a <>-enclosed address */
177 if (*from == '@' || *from == '!')
178 has_host_part = TRUE;
179 else if (*from == '>' && from[-1] != '<')
186 hostlen = strlen(host);
187 for (cp = from + strlen(from); cp >= from; --cp)
190 memcpy(from, host, hostlen);
192 has_host_part = TRUE;
199 * If we passed a comma, reset everything.
201 if (from[-1] == ',' && !parendepth) {
202 has_host_part = has_bare_name_part = FALSE;
208 if (outlevel >= O_DEBUG)
209 report_complete(stdout, GT_("Rewritten version is %s\n"), buf);
214 unsigned char *nxtaddr(hdr)
215 /* parse addresses in succession out of a specified RFC822 header */
216 const unsigned char *hdr; /* header to be parsed, NUL to continue previous hdr */
218 static unsigned char address[BUFSIZ];
220 static const unsigned char *hp;
221 static int state, oldstate;
223 static const unsigned char *orighdr;
227 #define START_HDR 0 /* before header colon */
228 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
229 #define BARE_ADDRESS 2 /* collecting address without delimiters */
230 #define INSIDE_DQUOTE 3 /* inside double quotes */
231 #define INSIDE_PARENS 4 /* inside parentheses */
232 #define INSIDE_BRACKETS 5 /* inside bracketed address */
233 #define ENDIT_ALL 6 /* after last address */
235 #define NEXTTP() ((tp < sizeof(address)-1) ? tp++ : tp)
252 printf("state %d: %s", state, orighdr);
253 printf("%*s^\n", hp - orighdr + 10, " ");
257 if (state == ENDIT_ALL) /* after last address */
259 else if (HEADER_END(hp))
264 while (isspace(address[--tp]))
266 address[++tp] = '\0';
270 return((unsigned char *)NULL);
272 else if (*hp == '\\') /* handle RFC822 escaping */
274 if (state != INSIDE_PARENS)
276 address[NEXTTP()] = *hp++; /* take the escape */
277 address[NEXTTP()] = *hp; /* take following unsigned char */
282 case START_HDR: /* before header colon */
287 case SKIP_JUNK: /* looking for address start */
288 if (*hp == '"') /* quoted string */
290 oldstate = SKIP_JUNK;
291 state = INSIDE_DQUOTE;
292 address[NEXTTP()] = *hp;
294 else if (*hp == '(') /* address comment -- ignore */
297 oldstate = SKIP_JUNK;
298 state = INSIDE_PARENS;
300 else if (*hp == '<') /* begin <address> */
302 state = INSIDE_BRACKETS;
305 else if (*hp != ',' && !isspace(*hp))
308 state = BARE_ADDRESS;
312 case BARE_ADDRESS: /* collecting address without delimiters */
313 if (*hp == ',') /* end of address */
317 address[NEXTTP()] = '\0';
323 else if (*hp == '(') /* beginning of comment */
326 oldstate = BARE_ADDRESS;
327 state = INSIDE_PARENS;
329 else if (*hp == '<') /* beginning of real address */
331 state = INSIDE_BRACKETS;
334 else if (*hp == '"') /* quoted word, copy verbatim */
337 state = INSIDE_DQUOTE;
338 address[NEXTTP()] = *hp;
340 else if (!isspace(*hp)) /* just take it, ignoring whitespace */
341 address[NEXTTP()] = *hp;
344 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
346 address[NEXTTP()] = *hp;
349 address[NEXTTP()] = *hp;
354 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
363 case INSIDE_BRACKETS: /* possible <>-enclosed address */
364 if (*hp == '>') /* end of address */
366 address[NEXTTP()] = '\0';
372 else if (*hp == '<') /* nested <> */
374 else if (*hp == '"') /* quoted address */
376 address[NEXTTP()] = *hp;
377 oldstate = INSIDE_BRACKETS;
378 state = INSIDE_DQUOTE;
380 else /* just copy address */
381 address[NEXTTP()] = *hp;
390 static void parsebuf(unsigned char *longbuf, int reply)
396 reply_hack(longbuf, "HOSTNAME.NET");
397 printf("Rewritten buffer: %s", longbuf);
400 if ((cp = nxtaddr(longbuf)) != (unsigned char *)NULL)
402 printf("\t-> \"%s\"\n", cp);
404 ((cp = nxtaddr((unsigned char *)NULL)) != (unsigned char *)NULL);
409 main(int argc, char *argv[])
411 unsigned char buf[BUFSIZ], longbuf[BUFSIZ];
414 verbose = reply = FALSE;
415 while ((ch = getopt(argc, argv, "rv")) != EOF)
427 while (fgets(buf, sizeof(buf)-1, stdin))
429 if (buf[0] == ' ' || buf[0] == '\t')
430 strcat(longbuf, buf);
431 else if (!strncasecmp("From: ", buf, 6)
432 || !strncasecmp("To: ", buf, 4)
433 || !strncasecmp("Reply-", buf, 6)
434 || !strncasecmp("Cc: ", buf, 4)
435 || !strncasecmp("Bcc: ", buf, 5))
436 strcpy(longbuf, buf);
440 fputs(longbuf, stdout);
441 parsebuf(longbuf, reply);
448 fputs(longbuf, stdout);
449 parsebuf(longbuf, reply);