summaryrefslogtreecommitdiff
path: root/usr.sbin/smtpd/queue_backend.c
blob: 24ca057e283f5e808ca49b8ed5f2dacee5917990 (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*	$OpenBSD: queue_backend.c,v 1.3 2010/06/01 23:06:23 jacekm Exp $	*/

/*
 * Copyright (c) 2010 Jacek Masiulaniec <jacekm@dobremiasto.net>
 *
 * 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/stat.h>

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <fts.h>
#include <libgen.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "queue_backend.h"

static char path[PATH_MAX];

static char *idchars = "ABCDEFGHIJKLMNOPQRSTUVWYZabcdefghijklmnopqrstuvwxyz0123456789";

int
queue_be_content_create(u_int64_t *content_id)
{
	int c, fd;

	c = idchars[arc4random_uniform(61)];
	snprintf(path, sizeof path, "content/%c/%cXXXXXXX", c, c);
	fd = mkstemp(path);
	if (fd < 0) {
		if (errno != ENOENT)
			return -1;
		if (mkdir(dirname(path), 0700) < 0)
			return -1;
		fd = mkstemp(path);
		if (fd < 0)
			return -1;
	}
	close(fd);
	*content_id = queue_be_encode(path + 10);
	return 0;
}

int
queue_be_content_open(u_int64_t content_id, int wr)
{
	char *id;

	id = queue_be_decode(content_id);
	snprintf(path, sizeof path, "content/%c/%s", id[0], id);
	return open(path, wr ? O_RDWR|O_APPEND|O_EXLOCK : O_RDONLY|O_SHLOCK);
}

void
queue_be_content_delete(u_int64_t content_id)
{
	char *id;

	id = queue_be_decode(content_id);
	snprintf(path, sizeof path, "content/%c/%s", id[0], id);
	unlink(path);
}

int
queue_be_action_new(u_int64_t content_id, u_int64_t *action_id, char *aux)
{
	FILE *fp;
	char *id;
	int fd;

	id = queue_be_decode(content_id);
	snprintf(path, sizeof path, "action/%c/%s,XXXXXXXX", id[0], id);
	fd = mkstemp(path);
	if (fd < 0) {
		if (errno != ENOENT)
			return -1;
		if (mkdir(dirname(path), 0700) < 0)
			return -1;
		fd = mkstemp(path);
		if (fd < 0)
			return -1;
	}
	fp = fdopen(fd, "w+");
	if (fp == NULL) {
		unlink(path);
		return -1;
	}
	fprintf(fp, "%s\n", aux);
	if (fclose(fp) == EOF) {
		unlink(path);
		return -1;
	}
	*action_id = queue_be_encode(path + 18);
	return 0;
}

int
queue_be_action_read(struct action_be *a, u_int64_t content_id, u_int64_t action_id)
{
	static char status[2048];
	static char aux[2048];
	struct stat sb_status, sb_content;
	char *id;
	FILE *fp;

	bzero(a, sizeof *a);
	a->content_id = content_id;
	a->action_id = action_id;

	/*
	 * Auxillary params for mta and mda.
	 */
	id = queue_be_decode(content_id);
	snprintf(path, sizeof path, "action/%c/%s,", id[0], id);
	strlcat(path, queue_be_decode(action_id), sizeof path);
	fp = fopen(path, "r");
	if (fp == NULL)
		return -1;
	if (fgets(aux, sizeof aux, fp) == NULL) {
		fclose(fp);
		return -1;
	}
	fclose(fp);
	aux[strcspn(aux, "\n")] = '\0';
	a->aux = aux;

	/*
	 * Error status message.
	 */
	id = queue_be_decode(content_id);
	snprintf(path, sizeof path, "status/%c/%s,", id[0], id);
	strlcat(path, queue_be_decode(action_id), sizeof path);
	fp = fopen(path, "r");
	if (fp) {
		if (fgets(status, sizeof status, fp) != NULL)
			status[strcspn(status, "\n")] = '\0';
		else
			status[0] = '\0';
		if (fstat(fileno(fp), &sb_status) < 0) {
			fclose(fp);
			return -1;
		}
		fclose(fp);
	} else
		status[0] = '\0';
	a->status = status;

	/*
	 * Message birth time.
	 *
	 * For bounces, use mtime of the status file.
	 * For non-bounces, use mtime of the content file.
	 */
	id = queue_be_decode(content_id);
	snprintf(path, sizeof path, "content/%c/%s", id[0], id);
	if (stat(path, &sb_content) < 0)
		return -1;
	if (sb_content.st_mode & S_IWUSR)
		a->birth = 0;
	else if (status[0] == '5' || status[0] == '6')
		a->birth = sb_status.st_mtime;
	else
		a->birth = sb_content.st_mtime;

	return 0;
}

int
queue_be_action_status(u_int64_t content_id, u_int64_t action_id, char *status)
{
	FILE *fp;
	char *id;

	id = queue_be_decode(content_id);
	snprintf(path, sizeof path, "status/%c/%s,", id[0], id);
	strlcat(path, queue_be_decode(action_id), sizeof path);
	fp = fopen(path, "w+");
	if (fp == NULL) {
		if (errno != ENOENT)
			return -1;
		mkdir(dirname(path), 0700);
		fp = fopen(path, "w+");
		if (fp == NULL)
			return -1;
	}
	if (fprintf(fp, "%s\n", status) == -1) {
		fclose(fp);
		return -1;
	}
	if (fclose(fp) == EOF)
		return -1;
	return 0;
}

void
queue_be_action_delete(u_int64_t content_id, u_int64_t action_id)
{
	char *id, *dir[] = { "action", "status" };
	u_int i;

	for (i = 0; i < 2; i++) {
		id = queue_be_decode(content_id);
		snprintf(path, sizeof path, "%s/%c/%s,", dir[i], id[0], id);
		id = queue_be_decode(action_id);
		strlcat(path, id, sizeof path);
		unlink(path);
	}
}

int
queue_be_commit(u_int64_t content_id)
{
	char *id;

	id = queue_be_decode(content_id);
	snprintf(path, sizeof path, "content/%c/%s", id[0], id);
	if (utimes(path, NULL) < 0 || chmod(path, 0400) < 0)
		return -1;
	return 0;
}

int
queue_be_getnext(struct action_be *a)
{
	static FTS	*fts;
	static FTSENT	*fe;
	char		*dir[] = { "action", NULL };

	if (fts == NULL) {
		fts = fts_open(dir, FTS_PHYSICAL|FTS_NOCHDIR, NULL);
		if (fts == NULL)
			return -1;
	}

	for (;;) {
		fe = fts_read(fts);
		if (fe == NULL) {
			if (errno) {
				fts_close(fts);
				return -1;
			} else {
				if (fts_close(fts) < 0)
					return -1;
				a->content_id = 0;
				a->action_id = 0;
				return 0;
			}
		}
		switch (fe->fts_info) {
		case FTS_F:
			break;
		case FTS_D:
		case FTS_DP:
			continue;
		default:
			fts_close(fts);
			return -1;
		}
		break;
	}

	if (fe->fts_namelen != 17 || fe->fts_name[8] != ',') {
		fts_close(fts);
		return -1;
	}
	a->content_id = queue_be_encode(fe->fts_name);
	a->action_id = queue_be_encode(fe->fts_name + 9);
	if (queue_be_action_read(a, a->content_id, a->action_id) < 0)
		return -2;

	return 0;
}

char *
queue_be_decode(u_int64_t id)
{
        static char txt[9];

	memcpy(txt, &id, sizeof id);
	txt[8] = '\0';
	return txt;
}

u_int64_t
queue_be_encode(const char *txt)
{
        u_int64_t id;

	if (strlen(txt) < sizeof id)
		id = INVALID_ID;
	else
		memcpy(&id, txt, sizeof id);

        return id;
}

int
queue_be_init(char *prefix, uid_t uid, gid_t gid)
{
	char *dir[] = { "action", "content", "status" };
	int i;

	for (i = 0; i < 3; i++) {
		snprintf(path, sizeof path, "%s/%s", prefix, dir[i]);
		if (mkdir(path, 0700) < 0 && errno != EEXIST)
			return -1;
		if (chmod(path, 0700) < 0)
			return -1;
		if (chown(path, uid, gid) < 0)
			return -1;
	}
	return 0;
}