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 ******************************************************************************/
23 #define _XOPEN_SOURCE 600
33 #include "fetchmail.h"
41 const char *program_name = "rfc822";
49 #define HEADER_END(p) ((p)[0] == '\n' && ((p)[1] != ' ' && (p)[1] != '\t'))
51 #define BEFORE_EOL(s) (strcspn((s), "\r\n"))
54 char *buf /* header to be hacked */,
55 const char *host /* server hostname */,
57 /* hack message headers so replies will work properly */
59 char *from, *cp, last_nws = '\0', *parens_from = NULL;
60 int parendepth, state, has_bare_name_part, has_host_part;
65 if (strncasecmp("From:", buf, 5)
66 && strncasecmp("To:", buf, 3)
67 && strncasecmp("Reply-To:", buf, 9)
68 && strncasecmp("Return-Path:", buf, 12)
69 && strncasecmp("Cc:", buf, 3)
70 && strncasecmp("Bcc:", buf, 4)
71 && strncasecmp("Resent-From:", buf, 12)
72 && strncasecmp("Resent-To:", buf, 10)
73 && strncasecmp("Resent-Cc:", buf, 10)
74 && strncasecmp("Resent-Bcc:", buf, 11)
75 && strncasecmp("Apparently-From:", buf, 16)
76 && strncasecmp("Apparently-To:", buf, 14)
77 && strncasecmp("Sender:", buf, 7)
78 && strncasecmp("Resent-Sender:", buf, 14)
84 if (outlevel >= O_DEBUG) {
85 report_build(stdout, GT_("About to rewrite %s...\n"), (cp = sdump(buf, BEFORE_EOL(buf))));
89 /* make room to hack the address; buf must be malloced */
90 for (cp = buf; *cp; cp++)
91 if (*cp == ',' || isspace((unsigned char)*cp))
93 buf = (char *)xrealloc(buf, strlen(buf) + addresscount * (strlen(host) + 1) + 1);
97 * This is going to foo up on some ill-formed addresses.
98 * Note that we don't rewrite the fake address <> in order to
99 * avoid screwing up bounce suppression with a null Return-Path.
102 parendepth = state = 0;
103 has_host_part = has_bare_name_part = FALSE;
104 for (from = buf; *from; from++)
109 printf("state %d: %s", state, buf);
110 printf("%*s^\n", (int)(from - buf + 10), " ");
117 else if (*from == ')')
121 if (!parendepth && !has_host_part)
124 case 0: /* before header colon */
129 case 1: /* we've seen the colon, we're looking for addresses */
130 if (!isspace((unsigned char)*from))
134 else if (*from == '@' || *from == '!')
135 has_host_part = TRUE;
136 else if (*from == '"')
139 * Not expanding on last non-WS == ';' deals with groupnames,
140 * an obscure misfeature described in sections
141 * 6.1, 6.2.6, and A.1.5 of the RFC822 standard.
143 else if ((*from == ',' || HEADER_END(from))
144 && has_bare_name_part
154 while (isspace((unsigned char)*from) || (*from == ','))
157 hostlen = strlen(host);
158 for (cp = from + strlen(from); cp >= from; --cp)
161 memcpy(from, host, hostlen);
162 from = p + hostlen + 1;
163 has_host_part = TRUE;
165 else if (from[1] == '('
166 && has_bare_name_part
168 && last_nws != ';' && last_nws != ')')
172 else if (!isspace((unsigned char)*from))
173 has_bare_name_part = TRUE;
176 case 2: /* we're in a string */
183 for (bp = from - 1; *bp == '\\'; bp--)
190 case 3: /* we're in a <>-enclosed address */
191 if (*from == '@' || *from == '!')
192 has_host_part = TRUE;
193 else if (*from == '>' && (from > buf && from[-1] != '<'))
200 hostlen = strlen(host);
201 for (cp = from + strlen(from); cp >= from; --cp)
204 memcpy(from, host, hostlen);
206 has_host_part = TRUE;
213 * If we passed a comma, reset everything.
215 if ((from > buf && from[-1] == ',') && !parendepth) {
216 has_host_part = has_bare_name_part = FALSE;
222 if (outlevel >= O_DEBUG) {
223 report_complete(stdout, GT_("...rewritten version is %s.\n"),
224 (cp = sdump(buf, BEFORE_EOL(buf))));
229 *length = strlen(buf);
233 char *nxtaddr(const char *hdr /* header to be parsed, NUL to continue previous hdr */)
234 /* parse addresses in succession out of a specified RFC822 header */
236 static char address[BUFSIZ];
238 static const char *hp;
239 static int state, oldstate;
241 static const char *orighdr;
245 #define START_HDR 0 /* before header colon */
246 #define SKIP_JUNK 1 /* skip whitespace, \n, and junk */
247 #define BARE_ADDRESS 2 /* collecting address without delimiters */
248 #define INSIDE_DQUOTE 3 /* inside double quotes */
249 #define INSIDE_PARENS 4 /* inside parentheses */
250 #define INSIDE_BRACKETS 5 /* inside bracketed address */
251 #define ENDIT_ALL 6 /* after last address */
253 #define NEXTTP() ((tp < sizeof(address)-1) ? tp++ : tp)
265 if (!hp) return NULL;
272 printf("state %d: %s", state, orighdr);
273 printf("%*s^\n", (int)(hp - orighdr + 10), " ");
277 if (state == ENDIT_ALL) /* after last address */
279 else if (HEADER_END(hp))
284 while (tp > 0 && isspace((unsigned char)address[tp - 1]))
292 else if (*hp == '\\') /* handle RFC822 escaping */
294 if (state != INSIDE_PARENS)
296 address[NEXTTP()] = *hp++; /* take the escape */
297 address[NEXTTP()] = *hp; /* take following unsigned char */
302 case START_HDR: /* before header colon */
307 case SKIP_JUNK: /* looking for address start */
308 if (*hp == '"') /* quoted string */
310 oldstate = SKIP_JUNK;
311 state = INSIDE_DQUOTE;
312 address[NEXTTP()] = *hp;
314 else if (*hp == '(') /* address comment -- ignore */
317 oldstate = SKIP_JUNK;
318 state = INSIDE_PARENS;
320 else if (*hp == '<') /* begin <address> */
322 state = INSIDE_BRACKETS;
325 else if (*hp != ',' && !isspace((unsigned char)*hp))
328 state = BARE_ADDRESS;
332 case BARE_ADDRESS: /* collecting address without delimiters */
333 if (*hp == ',') /* end of address */
337 address[NEXTTP()] = '\0';
343 else if (*hp == '(') /* beginning of comment */
346 oldstate = BARE_ADDRESS;
347 state = INSIDE_PARENS;
349 else if (*hp == '<') /* beginning of real address */
351 state = INSIDE_BRACKETS;
354 else if (*hp == '"') /* quoted word, copy verbatim */
357 state = INSIDE_DQUOTE;
358 address[NEXTTP()] = *hp;
360 else if (!isspace((unsigned char)*hp)) /* just take it, ignoring whitespace */
361 address[NEXTTP()] = *hp;
364 case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */
365 address[NEXTTP()] = *hp;
370 case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */
379 case INSIDE_BRACKETS: /* possible <>-enclosed address */
380 if (*hp == '>') /* end of address */
382 address[NEXTTP()] = '\0';
388 else if (*hp == '<') /* nested <> */
390 else if (*hp == '"') /* quoted address */
392 address[NEXTTP()] = *hp;
393 oldstate = INSIDE_BRACKETS;
394 state = INSIDE_DQUOTE;
396 else /* just copy address */
397 address[NEXTTP()] = *hp;
406 static void parsebuf(char *longbuf, int reply)
413 reply_hack(longbuf, "HOSTNAME.NET", &dummy);
414 printf("Rewritten buffer: %s", (char *)longbuf);
417 if ((cp = nxtaddr(longbuf)) != (char *)NULL)
419 printf("\t-> \"%s\"\n", (char *)cp);
421 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
426 int main(int argc, char *argv[])
428 char buf[BUFSIZ], longbuf[BUFSIZ];
431 verbose = reply = FALSE;
432 while ((ch = getopt(argc, argv, "rv")) != EOF)
446 while (fgets(buf, sizeof(buf)-1, stdin))
448 if (buf[0] == ' ' || buf[0] == '\t')
449 strlcat(longbuf, buf, sizeof(longbuf));
450 else if (!strncasecmp("From: ", buf, 6)
451 || !strncasecmp("To: ", buf, 4)
452 || !strncasecmp("Reply-", buf, 6)
453 || !strncasecmp("Cc: ", buf, 4)
454 || !strncasecmp("Bcc: ", buf, 5))
455 strlcpy(longbuf, buf, sizeof(longbuf));
459 fputs(longbuf, stdout);
460 parsebuf(longbuf, reply);
467 fputs(longbuf, stdout);
468 parsebuf(longbuf, reply);