summaryrefslogtreecommitdiff
path: root/usr.sbin/smtpd/queue_ram.c
blob: ab80811435e3a2fd0137e9b262eb3a4b6dafc2e0 (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*	$OpenBSD: queue_ram.c,v 1.2 2013/01/31 18:34:43 eric Exp $	*/

/*
 * Copyright (c) 2012 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 <sys/types.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <imsg.h>
#include <inttypes.h>
#include <libgen.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "smtpd.h"
#include "log.h"

static int queue_ram_init(int);
static int queue_ram_message(enum queue_op, uint32_t *);
static int queue_ram_envelope(enum queue_op , uint64_t *, char *, size_t);

struct queue_backend	queue_backend_ram = {
	queue_ram_init,
	queue_ram_message,
	queue_ram_envelope,
};

struct qr_envelope {
	char		*buf;
	size_t		 len;
};

struct qr_message {
	int		 hasfile;
	char		*buf;
	size_t		 len;
	struct tree	 envelopes;
};

static struct tree messages;

static int
queue_ram_init(int server)
{
	tree_init(&messages);

	return (1);
}

static int
queue_ram_message(enum queue_op qop, uint32_t *msgid)
{
	char			 path[MAXPATHLEN];
	uint64_t		 evpid;
	struct qr_envelope	*evp;
	struct qr_message	*msg;
	int			 fd, fd2, ret;
	struct stat		 sb;
	FILE			*f;
	size_t			 n;

	switch (qop) {
	case QOP_CREATE:
		msg = xcalloc(1, sizeof *msg, "queue_ram_message");
		tree_init(&msg->envelopes);
		do {
			*msgid = queue_generate_msgid();
		} while (tree_check(&messages, *msgid));
		tree_xset(&messages, *msgid, msg);
		return (1);

	case QOP_DELETE:
		if ((msg = tree_pop(&messages, *msgid)) == NULL) {
			log_warnx("warn: queue_ram_message: not found");
			return (0);
		}
		while (tree_poproot(&messages, &evpid, (void**)&evp)) {
			stat_decrement("queue.ram.envelope.size", evp->len);
			free(evp->buf);
			free(evp);
		}
		stat_decrement("queue.ram.message.size", msg->len);
		free(msg->buf);
		if (msg->hasfile) {
			queue_message_incoming_path(*msgid, path, sizeof(path));
			if (unlink(path) == -1)
				log_warn("warn: queue_ram_message: unlink");
		}
		free(msg);
		return (1);

	case QOP_COMMIT:
		if ((msg = tree_get(&messages, *msgid)) == NULL) {
			log_warnx("warn: queue_ram_message: not found");
			return (0);
		}
		queue_message_incoming_path(*msgid, path, sizeof(path));
		f = fopen(path, "rb");
		if (f == NULL) {
			log_warn("warn: queue_ram: fopen");
			return (0);
		}
		if (fstat(fileno(f), &sb) == -1) {
			log_warn("warn: queue_ram_message: fstat");
			fclose(f);
			return (0);
		}

		msg->len = sb.st_size;
		msg->buf = xmalloc(msg->len, "queue_ram_message");
		ret = 0;
		n = fread(msg->buf, 1, msg->len, f);
		if (ferror(f))
			log_warn("warn: queue_ram_message: fread");
		else if ((off_t)n != sb.st_size)
			log_warnx("warn: queue_ram_message: bad read");
		else {
			ret = 1;
			stat_increment("queue.ram.message.size", msg->len);
		}
		fclose(f);
		if (unlink(path) == -1)
			log_warn("warn: queue_ram_message: unlink");
		msg->hasfile = 0;
		return (ret);

	case QOP_FD_R:
		if ((msg = tree_get(&messages, *msgid)) == NULL) {
			log_warnx("warn: queue_ram_message: not found");
			return (-1);
		}
		fd = mktmpfile();
		if (fd == -1) {
			log_warn("warn: queue_ram_message: mktmpfile");
			return (-1);
		}
		fd2 = dup(fd);
		if (fd2 == -1) {
			log_warn("warn: queue_ram_message: dup");
			close(fd);
			return (-1);
		}
		f = fdopen(fd2, "w");
		if (f == NULL) {
			log_warn("warn: queue_ram_message: fdopen");
			close(fd);
			close(fd2);
			return (-1);
		}
		n = fwrite(msg->buf, 1, msg->len, f);
		if (n != msg->len) {
			log_warn("warn: queue_ram_message: write");
			close(fd);
			fclose(f);
			return (-1);
		}
		fclose(f);
		lseek(fd, 0, SEEK_SET);
		return (fd);

	case QOP_FD_RW:
		if ((msg = tree_get(&messages, *msgid)) == NULL) {
			log_warnx("warn: queue_ram_message: not found");
			return (-1);
		}
		queue_message_incoming_path(*msgid, path, sizeof(path));
		fd = open(path, O_RDWR | O_CREAT | O_EXCL, 0600);
		if (fd == -1) {
			log_warn("warn: queue_ram_message: open");
			return (-1);
		}
		msg->hasfile = 1;
		return (fd);

	case QOP_CORRUPT:
		return (queue_ram_message(QOP_DELETE, msgid));

	default:
		fatalx("queue_ram_message: unsupported operation.");
	}

	return (0);
}

static int
queue_ram_envelope(enum queue_op qop, uint64_t *evpid, char *buf, size_t len)
{
	struct qr_envelope	*evp;
	struct qr_message	*msg;
	uint32_t		 msgid;
	char			*tmp;

	if (qop == QOP_WALK)
		return (-1);

	msgid = evpid_to_msgid(*evpid);
	msg = tree_get(&messages, msgid);
	if (msg == NULL) {
		log_warn("warn: queue_ram_envelope: message not found");
		return (0);
	}

	switch (qop) {
	case QOP_CREATE:
		do {
			*evpid = queue_generate_evpid(msgid);
		} while (tree_check(&msg->envelopes, *evpid));
		evp = calloc(1, sizeof *evp);
		if (evp == NULL) {
			log_warn("warn: queue_ram_envelope: calloc");
			return (0);
		}
		evp->len = len;
		evp->buf = malloc(len);
		if (evp->buf == NULL) {
			log_warn("warn: queue_ram_envelope: malloc");
			free(evp);
			return (0);
		}
		memmove(evp->buf, buf, len);
		tree_xset(&msg->envelopes, *evpid, evp);
		stat_increment("queue.ram.envelope.size", len);
		return (1);

	case QOP_DELETE:
		if ((evp = tree_pop(&msg->envelopes, *evpid)) == NULL) {
			log_warnx("warn: queue_ram_envelope: not found");
			return (0);
		}
		stat_decrement("queue.ram.envelope.size", evp->len);
		free(evp->buf);
		free(evp);
		if (tree_empty(&msg->envelopes)) {
			tree_xpop(&messages, msgid);
			stat_decrement("queue.ram.message.size", msg->len);
			free(msg->buf);
			free(msg);
		}
		return (1);

	case QOP_LOAD:
		if ((evp = tree_get(&msg->envelopes, *evpid)) == NULL) {
			log_warn("warn: queue_ram_envelope: not found");
			return (0);
		}
		if (len < evp->len) {
			log_warnx("warn: queue_ram_envelope: buffer too small");
			return (0);
		}
		memmove(buf, evp->buf, evp->len);
		return (evp->len);

	case QOP_UPDATE:
		if ((evp = tree_get(&msg->envelopes, *evpid)) == NULL) {
			log_warn("warn: queue_ram_envelope: not found");
			return (0);
		}
		tmp = malloc(len);
		if (tmp == NULL) {
			log_warn("warn: queue_ram_envelope: malloc");
			return (0);
		}
		memmove(tmp, buf, len);
		free(evp->buf);
		evp->len = len;
		evp->buf = tmp;
		stat_decrement("queue.ram.envelope.size", evp->len);
		stat_increment("queue.ram.envelope.size", len);
		return (1);

	default:
		fatalx("queue_ram_envelope: unsupported operation.");
	}

	return (0);
}