summaryrefslogtreecommitdiff
path: root/usr.sbin/smtpd/rfc2822.c
blob: 9aa3c2bdb3c6066a7e9ac9dddb3eb9827da658e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*	$OpenBSD: rfc2822.c,v 1.10 2017/02/04 19:25:24 guenther Exp $	*/

/*
 * Copyright (c) 2014 Gilles Chehade <gilles@poolp.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/tree.h>

#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "rfc2822.h"

/* default no-op callbacks */
static void hdr_dflt_cb(const struct rfc2822_header *hdr, void *arg) {}
static void body_dflt_cb(const char *line, void *arg) {}

static void
header_reset(struct rfc2822_header *hdr)
{
	struct rfc2822_line	*line;

	while ((line = TAILQ_FIRST(&hdr->lines))) {
		TAILQ_REMOVE(&hdr->lines, line, next);
		free(line);
	}
}

static void
header_callback(struct rfc2822_parser *rp)
{
	struct rfc2822_hdr_cb		*hdr_cb;
	struct rfc2822_hdr_miss_cb	*hdr_miss_cb;

	if (!rp->in_hdr)
		goto end;

	TAILQ_FOREACH(hdr_cb, &rp->hdr_cb, next)
	    if (strcasecmp(hdr_cb->name, rp->header.name) == 0) {
		    hdr_cb->func(&rp->header, hdr_cb->arg);
		    goto end;
	    }
	rp->hdr_dflt_cb.func(&rp->header, rp->hdr_dflt_cb.arg);

end:
	TAILQ_FOREACH(hdr_miss_cb, &rp->hdr_miss_cb, next)
	    if (strcasecmp(hdr_miss_cb->name, rp->header.name) == 0)
		    break;
	if (hdr_miss_cb)
		TAILQ_REMOVE(&rp->hdr_miss_cb, hdr_miss_cb, next);
	free(hdr_miss_cb);
	header_reset(&rp->header);
	rp->in_hdr = 0;
	return;
}

static void
missing_headers_callback(struct rfc2822_parser *rp)
{
	struct rfc2822_hdr_miss_cb	*hdr_miss_cb;

	while ((hdr_miss_cb = TAILQ_FIRST(&rp->hdr_miss_cb))) {
		hdr_miss_cb->func(hdr_miss_cb->name, hdr_miss_cb->arg);
		TAILQ_REMOVE(&rp->hdr_miss_cb, hdr_miss_cb, next);
		free(hdr_miss_cb);
	}
}

static void
body_callback(struct rfc2822_parser *rp, const char *line)
{
	rp->body_line_cb.func(line, rp->body_line_cb.arg);
}

static int
parser_feed_header(struct rfc2822_parser *rp, char *line)
{
	struct rfc2822_line	*hdrline;
	char			*pos;

	/* new header */
	if (!isspace((unsigned char)*line) && *line != '\0') {
		rp->in_hdr = 1;
		if ((pos = strchr(line, ':')) == NULL)
			return 0;
		memset(rp->header.name, 0, sizeof rp->header.name);
		(void)memcpy(rp->header.name, line, pos - line);
		if (isspace((unsigned char)pos[1]))
			return parser_feed_header(rp, pos + 1);
		else {
			*pos = ' ';
			return parser_feed_header(rp, pos);
		}
	}

	/* continuation */
	if (!rp->in_hdr)
		return 0;

	/* append line to header */
	if ((hdrline = calloc(1, sizeof *hdrline)) == NULL)
		return -1;
	(void)strlcpy(hdrline->buffer, line, sizeof hdrline->buffer);
	TAILQ_INSERT_TAIL(&rp->header.lines, hdrline, next);
	return 1;
}

static int
parser_feed_body(struct rfc2822_parser *rp, const char *line)
{
	/* for now, we only support per-line callbacks */
	body_callback(rp, line);
	return 1;
}


void
rfc2822_parser_init(struct rfc2822_parser *rp)
{
	memset(rp, 0, sizeof *rp);
	TAILQ_INIT(&rp->hdr_cb);
	TAILQ_INIT(&rp->hdr_miss_cb);
	TAILQ_INIT(&rp->header.lines);
	rfc2822_header_default_callback(rp, hdr_dflt_cb, NULL);
	rfc2822_body_callback(rp, body_dflt_cb, NULL);
	rfc2822_parser_reset(rp);
}

void
rfc2822_parser_flush(struct rfc2822_parser *rp)
{
	if (!rp->in_hdrs)
		return;

	header_callback(rp);

	missing_headers_callback(rp);
}

void
rfc2822_parser_reset(struct rfc2822_parser *rp)
{
	header_reset(&rp->header);
	rp->in_hdrs = 1;
}

void
rfc2822_parser_release(struct rfc2822_parser *rp)
{
	struct rfc2822_hdr_cb		*cb;
	struct rfc2822_hdr_miss_cb	*mcb;

	rfc2822_parser_reset(rp);
	while ((cb = TAILQ_FIRST(&rp->hdr_cb))) {
		TAILQ_REMOVE(&rp->hdr_cb, cb, next);
		free(cb);
	}
	while ((mcb = TAILQ_FIRST(&rp->hdr_miss_cb))) {
		TAILQ_REMOVE(&rp->hdr_miss_cb, mcb, next);
		free(mcb);
	}
}

int
rfc2822_parser_feed(struct rfc2822_parser *rp, const char *line)
{
	char			buffer[RFC2822_MAX_LINE_SIZE+1];

	/* in header and line is not a continuation, execute callback */
	if (rp->in_hdr && (*line == '\0' || !isspace((unsigned char)*line)))
		header_callback(rp);

	/* no longer in headers */
	if (*line == '\0') {
		if (rp->in_hdrs)
			missing_headers_callback(rp);
		rp->in_hdrs = 0;
	}

	if (rp->in_hdrs) {
		/* line exceeds RFC maximum size requirement */
		if (strlcpy(buffer, line, sizeof buffer) >= sizeof buffer)
			return 0;
		return parser_feed_header(rp, buffer);
	}

	/* don't enforce line max length on content, too many MUA break */
	return parser_feed_body(rp, line);
}

int
rfc2822_header_callback(struct rfc2822_parser *rp, const char *header,
    void (*func)(const struct rfc2822_header *, void *), void *arg)
{
	struct rfc2822_hdr_cb  *cb;
	struct rfc2822_hdr_cb  *cb_tmp;
	char			buffer[RFC2822_MAX_LINE_SIZE+1];

	/* line exceeds RFC maximum size requirement */
	if (strlcpy(buffer, header, sizeof buffer) >= sizeof buffer)
		return 0;

	TAILQ_FOREACH_SAFE(cb, &rp->hdr_cb, next, cb_tmp) {
		if (strcasecmp(cb->name, buffer) == 0) {
			TAILQ_REMOVE(&rp->hdr_cb, cb, next);
			free(cb);
		}
	}

	if ((cb = calloc(1, sizeof *cb)) == NULL)
		return -1;
	(void)strlcpy(cb->name, buffer, sizeof cb->name);
	cb->func = func;
	cb->arg  = arg;
	TAILQ_INSERT_TAIL(&rp->hdr_cb, cb, next);
	return 1;
}

int
rfc2822_missing_header_callback(struct rfc2822_parser *rp, const char *header,
    void (*func)(const char *, void *), void *arg)
{
	struct rfc2822_hdr_miss_cb  *cb;
	struct rfc2822_hdr_miss_cb  *cb_tmp;
	char			buffer[RFC2822_MAX_LINE_SIZE+1];

	/* line exceeds RFC maximum size requirement */
	if (strlcpy(buffer, header, sizeof buffer) >= sizeof buffer)
		return 0;

	TAILQ_FOREACH_SAFE(cb, &rp->hdr_miss_cb, next, cb_tmp) {
		if (strcasecmp(cb->name, buffer) == 0) {
			TAILQ_REMOVE(&rp->hdr_miss_cb, cb, next);
			free(cb);
		}
	}

	if ((cb = calloc(1, sizeof *cb)) == NULL)
		return -1;
	(void)strlcpy(cb->name, buffer, sizeof cb->name);
	cb->func = func;
	cb->arg  = arg;
	TAILQ_INSERT_TAIL(&rp->hdr_miss_cb, cb, next);
	return 1;
}

void
rfc2822_header_default_callback(struct rfc2822_parser *rp,
    void (*func)(const struct rfc2822_header *, void *), void *arg)
{
	struct rfc2822_hdr_cb	*cb;

	cb = &rp->hdr_dflt_cb;
	cb->func = func;
	cb->arg  = arg;
}

void
rfc2822_body_callback(struct rfc2822_parser *rp,
    void (*func)(const char *, void *), void *arg)
{
	struct rfc2822_line_cb	*cb;

	cb = &rp->body_line_cb;
	cb->func = func;
	cb->arg  = arg;
}