GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/smtpd/smtpd/../delivery_maildir.c Lines: 0 64 0.0 %
Date: 2017-11-13 Branches: 0 72 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: delivery_maildir.c,v 1.18 2016/08/31 10:18:08 gilles Exp $	*/
2
3
/*
4
 * Copyright (c) 2011 Gilles Chehade <gilles@poolp.org>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <sys/types.h>
20
#include <sys/queue.h>
21
#include <sys/tree.h>
22
#include <sys/socket.h>
23
#include <sys/stat.h>
24
25
#include <ctype.h>
26
#include <err.h>
27
#include <errno.h>
28
#include <event.h>
29
#include <fcntl.h>
30
#include <imsg.h>
31
#include <paths.h>
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <string.h>
35
#include <time.h>
36
#include <unistd.h>
37
#include <limits.h>
38
39
#include "smtpd.h"
40
#include "log.h"
41
42
extern char	**environ;
43
44
/* maildir backend */
45
static void delivery_maildir_open(struct deliver *);
46
static int mailaddr_tag(const struct mailaddr *, char *, size_t);
47
48
struct delivery_backend delivery_backend_maildir = {
49
	1, delivery_maildir_open
50
};
51
52
static int
53
mailaddr_tag(const struct mailaddr *maddr, char *dest, size_t len)
54
{
55
	char		*tag;
56
	char		*sanitized;
57
58
	if ((tag = strchr(maddr->user, *env->sc_subaddressing_delim))) {
59
		tag++;
60
		while (*tag == '.')
61
			tag++;
62
	}
63
	if (tag == NULL)
64
		return 1;
65
66
	if (strlcpy(dest, tag, len) >= len)
67
		return 0;
68
	for (sanitized = dest; *sanitized; sanitized++)
69
		if (strchr(MAILADDR_ESCAPE, *sanitized))
70
			*sanitized = ':';
71
	return 1;
72
}
73
74
static void
75
delivery_maildir_open(struct deliver *deliver)
76
{
77
	char	 tmp[PATH_MAX], new[PATH_MAX], tag[PATH_MAX];
78
	int	 ch, fd;
79
	FILE	*fp;
80
	char	*msg;
81
	int	 n;
82
	const char	*chd;
83
	struct mailaddr	maddr;
84
	struct stat	sb;
85
86
#define error(m)	{ msg = m; goto err; }
87
#define error2(m)	{ msg = m; goto err2; }
88
89
	setproctitle("maildir delivery");
90
91
	memset(&maddr, 0, sizeof maddr);
92
	if (!text_to_mailaddr(&maddr, deliver->dest))
93
		error("cannot parse destination address");
94
95
	memset(tag, 0, sizeof tag);
96
	if (!mailaddr_tag(&maddr, tag, sizeof tag))
97
		error("cannot extract tag from destination address");
98
99
	if (mkdirs(deliver->to, 0700) < 0 && errno != EEXIST)
100
		error("cannot mkdir maildir");
101
	chd = deliver->to;
102
103
	if (tag[0]) {
104
		(void)snprintf(tmp, sizeof tmp, "%s/.%s", deliver->to, tag);
105
		if (stat(tmp, &sb) != -1)
106
			chd = tmp;
107
	}
108
109
	if (chdir(chd) < 0)
110
		error("cannot cd to maildir");
111
	if (mkdir("cur", 0700) < 0 && errno != EEXIST)
112
		error("mkdir cur failed");
113
	if (mkdir("tmp", 0700) < 0 && errno != EEXIST)
114
		error("mkdir tmp failed");
115
	if (mkdir("new", 0700) < 0 && errno != EEXIST)
116
		error("mkdir new failed");
117
	(void)snprintf(tmp, sizeof tmp, "tmp/%lld.%d.%s",
118
	    (long long int) time(NULL),
119
	    getpid(), env->sc_hostname);
120
	fd = open(tmp, O_CREAT | O_EXCL | O_WRONLY, 0600);
121
	if (fd < 0)
122
		error("cannot open tmp file");
123
	fp = fdopen(fd, "w");
124
	if (fp == NULL)
125
		error2("fdopen");
126
	while ((ch = getc(stdin)) != EOF)
127
		if (putc(ch, fp) == EOF)
128
			break;
129
	if (ferror(stdin))
130
		error2("read error");
131
	if (fflush(fp) == EOF || ferror(fp))
132
		error2("write error");
133
	if (fsync(fd) < 0)
134
		error2("fsync");
135
	if (fclose(fp) == EOF)
136
		error2("fclose");
137
	(void)snprintf(new, sizeof new, "new/%s", tmp + 4);
138
	if (rename(tmp, new) < 0)
139
		error2("cannot rename tmp->new");
140
	_exit(0);
141
142
err2:
143
	n = errno;
144
	unlink(tmp);
145
	errno = n;
146
err:
147
	perror(msg);
148
	_exit(1);
149
}