aboutsummaryrefslogtreecommitdiffstats
path: root/smtpd/rfc5322.c
blob: 0af667728249ce35994c894542ca51dbe95d3650 (plain) (blame)
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
/*	$OpenBSD: rfc5322.c,v 1.2 2018/10/24 18:59:29 gilles Exp $	*/

/*
 * Copyright (c) 2018 Eric Faurot <eric@openbsd.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 "includes.h"

#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>

#include "rfc5322.h"

struct buf {
	char	*buf;
	size_t	 bufsz;
	size_t	 buflen;
	size_t	 bufmax;
};

static int buf_alloc(struct buf *, size_t);
static int buf_grow(struct buf *, size_t);
static int buf_cat(struct buf *, const char *);

struct rfc5322_parser {
	const char	*line;
	int		 state;		/* last parser state */
	int		 next;		/* parser needs data */
	int		 unfold;
	const char	*currhdr;
	struct buf	 hdr;
	struct buf	 val;
};

struct rfc5322_parser *
rfc5322_parser_new(void)
{
	struct rfc5322_parser *parser;

	parser = calloc(1, sizeof(*parser));
	if (parser == NULL)
		return NULL;

	rfc5322_clear(parser);
	parser->hdr.bufmax = 1024;
	parser->val.bufmax = 65536;

	return parser;
}

void
rfc5322_free(struct rfc5322_parser *parser)
{
	free(parser->hdr.buf);
	free(parser->val.buf);
	free(parser);
}

void
rfc5322_clear(struct rfc5322_parser *parser)
{
	parser->line = NULL;
	parser->state = RFC5322_NONE;
	parser->next = 0;
	parser->hdr.buflen = 0;
	parser->val.buflen = 0;
}

int
rfc5322_push(struct rfc5322_parser *parser, const char *line)
{
	if (parser->line) {
		errno = EALREADY;
		return -1;
	}

	parser->line = line;
	parser->next = 0;

	return 0;
}

int
rfc5322_unfold_header(struct rfc5322_parser *parser)
{
	if (parser->unfold) {
		errno = EALREADY;
		return -1;
	}

	if (parser->currhdr == NULL) {
		errno = EOPNOTSUPP;
		return -1;
	}

	if (buf_cat(&parser->val, parser->currhdr) == -1)
		return -1;

	parser->currhdr = NULL;
	parser->unfold = 1;

	return 0;
}

static int
_rfc5322_next(struct rfc5322_parser *parser, struct rfc5322_result *res)
{
	size_t len;
	const char *pos, *line;

	line = parser->line;

	switch(parser->state) {

	case RFC5322_HEADER_START:
	case RFC5322_HEADER_CONT:
		res->hdr = parser->hdr.buf;

		if (line && (line[0] == ' ' || line[0] == '\t')) {
			parser->line = NULL;
			parser->next = 1;
			if (parser->unfold) {
				if (buf_cat(&parser->val, "\n") == -1 ||
				    buf_cat(&parser->val, line) == -1)
					return -1;
			}
			res->value = line;
			return RFC5322_HEADER_CONT;
		}

		if (parser->unfold) {
			parser->val.buflen = 0;
			parser->unfold = 0;
			res->value = parser->val.buf;
		}
		return RFC5322_HEADER_END;

	case RFC5322_NONE:
	case RFC5322_HEADER_END:
		if (line && (pos = strchr(line, ':'))) {
			len = pos - line;
			if (buf_grow(&parser->hdr, len + 1) == -1)
				return -1;
			(void)memcpy(parser->hdr.buf, line, len);
			parser->hdr.buf[len] = '\0';
			parser->hdr.buflen = len + 1;
			parser->line = NULL;
			parser->next = 1;
			parser->currhdr = pos + 1;
			res->hdr = parser->hdr.buf;
			res->value = pos + 1;
			return RFC5322_HEADER_START;
		}

		return RFC5322_END_OF_HEADERS;

	case RFC5322_END_OF_HEADERS:
		if (line == NULL)
			return RFC5322_END_OF_MESSAGE;

		if (line[0] == '\0') {
			parser->line = NULL;
			parser->next = 1;
			res->value = line;
			return RFC5322_BODY_START;
		}

		errno = EINVAL;
		return -1;

	case RFC5322_BODY_START:
	case RFC5322_BODY:
		if (line == NULL)
			return RFC5322_END_OF_MESSAGE;

		parser->line = NULL;
		parser->next = 1;
		res->value = line;
		return RFC5322_BODY;

	case RFC5322_END_OF_MESSAGE:
		errno = ENOMSG;
		return -1;

	default:
		errno = EINVAL;
		return -1;
	}
}

int
rfc5322_next(struct rfc5322_parser *parser, struct rfc5322_result *res)
{
	memset(res, 0, sizeof(*res));

	if (parser->next)
		return RFC5322_NONE;

	return (parser->state = _rfc5322_next(parser, res));
}

static int
buf_alloc(struct buf *b, size_t need)
{
	char *buf;
	size_t alloc;

	if (b->buf && b->bufsz >= need)
		return 0;

	if (need >= b->bufmax) {
		errno = ERANGE;
		return -1;
	}

#define N 256
	alloc = N * (need / N) + ((need % N) ? N : 0);
#undef N
	buf = reallocarray(b->buf, alloc, 1);
	if (buf == NULL)
		return -1;

	b->buf = buf;
	b->bufsz = alloc;

	return 0;
}

static int
buf_grow(struct buf *b, size_t sz)
{
	if (SIZE_T_MAX - b->buflen <= sz) {
		errno = ERANGE;
		return -1;
	}

	return buf_alloc(b, b->buflen + sz);
}

static int
buf_cat(struct buf *b, const char *s)
{
	size_t len = strlen(s);

	if (buf_grow(b, len + 1) == -1)
		return -1;

	(void)memmove(b->buf + b->buflen, s, len + 1);
	b->buflen += len;
	return 0;
}