aboutsummaryrefslogtreecommitdiffstats
path: root/smtpd/mail.maildir.c
blob: fe6adba6028a8b417fca73c8ec798f11ae824ef7 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (c) 2017 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 "includes.h"

#ifndef nitems
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
#endif

#include <sys/types.h>
#include <sys/stat.h>

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sysexits.h>
#include <unistd.h>

#define	MAILADDR_ESCAPE		"!#$%&'*/?^`{|}~"

static int	maildir_subdir(const char *, char *, size_t);
static void	maildir_mkdirs(const char *);
static void	maildir_engine(const char *, int);
static int	mkdirs_component(const char *, mode_t);
static int	mkdirs(const char *, mode_t);

int
main(int argc, char *argv[])
{
	int	ch;
	int	junk = 0;

	if (! geteuid())
		errx(1, "mail.maildir: may not be executed as root");

	while ((ch = getopt(argc, argv, "j")) != -1) {
		switch (ch) {
		case 'j':
			junk = 1;
			break;
		default:
			break;
		}
	}
	argc -= optind;
	argv += optind;

	if (argc > 1)
		errx(1, "mail.maildir: only one maildir is allowed");

	maildir_engine(argv[0], junk);

	return (0);
}

static int
maildir_subdir(const char *extension, char *dest, size_t len)
{
	char		*sanitized;

	if (strlcpy(dest, extension, len) >= len)
		return 0;

	for (sanitized = dest; *sanitized; sanitized++)
		if (strchr(MAILADDR_ESCAPE, *sanitized))
			*sanitized = ':';

	return 1;
}

static void
maildir_mkdirs(const char *dirname)
{
	uint	i;
	int	ret;
	char	pathname[PATH_MAX];
	char	*subdirs[] = { "cur", "tmp", "new" };

	if (mkdirs(dirname, 0700) == -1 && errno != EEXIST) {
		if (errno == EINVAL || errno == ENAMETOOLONG)
			err(1, NULL);
		err(EX_TEMPFAIL, NULL);
	}

	for (i = 0; i < nitems(subdirs); ++i) {
		ret = snprintf(pathname, sizeof pathname, "%s/%s", dirname,
		    subdirs[i]);
		if (ret < 0 || (size_t)ret >= sizeof pathname)
			errc(1, ENAMETOOLONG, "%s/%s", dirname, subdirs[i]);
		if (mkdir(pathname, 0700) == -1 && errno != EEXIST)
			err(EX_TEMPFAIL, NULL);
	}
}

static void
maildir_engine(const char *dirname, int junk)
{
	char	rootpath[PATH_MAX];
	char	junkpath[PATH_MAX];
	char	extpath[PATH_MAX];
	char	subdir[PATH_MAX];
	char	filename[PATH_MAX];
	char	hostname[HOST_NAME_MAX+1];

	char	tmp[PATH_MAX];
	char	new[PATH_MAX];

	int	ret;

	int	fd;
	FILE    *fp;
	char	*line = NULL;
	size_t	linesize = 0;
	ssize_t	linelen;
	struct stat	sb;
	char	*home;
	char	*extension;

	int	is_junk = 0;
	int	in_hdr  = 1;

	if (dirname == NULL) {
		if ((home = getenv("HOME")) == NULL)
			err(1, NULL);
		ret = snprintf(rootpath, sizeof rootpath, "%s/Maildir", home);
		if (ret < 0 || (size_t)ret >= sizeof rootpath)
			errc(1, ENAMETOOLONG, "%s/Maildir", home);
		dirname = rootpath;
	}
	maildir_mkdirs(dirname);

	if (junk) {
		/* create Junk subdirectory */
		ret = snprintf(junkpath, sizeof junkpath, "%s/.Junk", dirname);
		if (ret < 0 || (size_t)ret >= sizeof junkpath)
			errc(1, ENAMETOOLONG, "%s/.Junk", dirname);
		maildir_mkdirs(junkpath);
	}

	if ((extension = getenv("EXTENSION")) != NULL) {
		if (maildir_subdir(extension, subdir, sizeof(subdir)) &&
		    subdir[0]) {
			ret = snprintf(extpath, sizeof extpath, "%s/.%s",
			    dirname, subdir);
			if (ret < 0 || (size_t)ret >= sizeof extpath)
				errc(1, ENAMETOOLONG, "%s/.%s",
				    dirname, subdir);
			if (stat(extpath, &sb) != -1) {
				dirname = extpath;
				maildir_mkdirs(dirname);
			}
		}
	}

	if (gethostname(hostname, sizeof hostname) != 0)
		(void)strlcpy(hostname, "localhost", sizeof hostname);

	(void)snprintf(filename, sizeof filename, "%lld.%08x.%s",
	    (long long int) time(NULL),
	    arc4random(),
	    hostname);

	(void)snprintf(tmp, sizeof tmp, "%s/tmp/%s", dirname, filename);

	fd = open(tmp, O_CREAT | O_EXCL | O_WRONLY, 0600);
	if (fd == -1)
		err(EX_TEMPFAIL, NULL);
	if ((fp = fdopen(fd, "w")) == NULL)
		err(EX_TEMPFAIL, NULL);

	while ((linelen = getline(&line, &linesize, stdin)) != -1) {
		line[strcspn(line, "\n")] = '\0';
		if (line[0] == '\0')
			in_hdr = 0;
		if (junk && in_hdr &&
		    (strcasecmp(line, "x-spam: yes") == 0 ||
			strcasecmp(line, "x-spam-flag: yes") == 0))
			is_junk = 1;
		fprintf(fp, "%s\n", line);
	}
	free(line);
	if (ferror(stdin))
		err(EX_TEMPFAIL, NULL);

	if (fflush(fp) == EOF ||
	    ferror(fp) ||
	    fsync(fd) == -1 ||
	    fclose(fp) == EOF)
		err(EX_TEMPFAIL, NULL);

	(void)snprintf(new, sizeof new, "%s/new/%s",
	    is_junk ? junkpath : dirname, filename);

	if (rename(tmp, new) == -1)
		err(EX_TEMPFAIL, NULL);

	exit(0);
}


static int
mkdirs_component(const char *path, mode_t mode)
{
	struct stat	sb;

	if (stat(path, &sb) == -1) {
		if (errno != ENOENT)
			return 0;
		if (mkdir(path, mode | S_IWUSR | S_IXUSR) == -1)
			return 0;
	}
	else if (!S_ISDIR(sb.st_mode)) {
		errno = ENOTDIR;
		return 0;
	}

	return 1;
}

static int
mkdirs(const char *path, mode_t mode)
{
	char	 buf[PATH_MAX];
	int	 i = 0;
	int	 done = 0;
	const char	*p;

	/* absolute path required */
	if (*path != '/') {
		errno = EINVAL;
		return 0;
	}

	/* make sure we don't exceed PATH_MAX */
	if (strlen(path) >= sizeof buf) {
		errno = ENAMETOOLONG;
		return 0;
	}

	memset(buf, 0, sizeof buf);
	for (p = path; *p; p++) {
		if (*p == '/') {
			if (buf[0] != '\0')
				if (!mkdirs_component(buf, mode))
					return 0;
			while (*p == '/')
				p++;
			buf[i++] = '/';
			buf[i++] = *p;
			if (*p == '\0' && ++done)
				break;
			continue;
		}
		buf[i++] = *p;
	}
	if (!done)
		if (!mkdirs_component(buf, mode))
			return 0;

	if (chmod(path, mode) == -1)
		return 0;

	return 1;
}