-
Notifications
You must be signed in to change notification settings - Fork 271
/
istream-ext-filter.c
214 lines (183 loc) · 5.59 KB
/
istream-ext-filter.c
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
/* Copyright (c) 2013-2017 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "str.h"
#include "net.h"
#include "eacces-error.h"
#include "fd-set-nonblock.h"
#include "ostream.h"
#include "istream-private.h"
#include "istream-ext-filter.h"
#include <unistd.h>
struct mail_filter_istream {
struct istream_private istream;
int fd;
struct istream *ext_in;
struct ostream *ext_out;
size_t prev_ret;
};
static void
i_stream_mail_filter_close(struct iostream_private *stream, bool close_parent)
{
struct mail_filter_istream *mstream =
(struct mail_filter_istream *)stream;
if (mstream->ext_in != NULL)
i_stream_destroy(&mstream->ext_in);
if (mstream->ext_out != NULL)
o_stream_destroy(&mstream->ext_out);
if (mstream->fd != -1) {
if (close(mstream->fd) < 0)
i_error("ext-filter: close() failed: %m");
mstream->fd = -1;
}
if (close_parent)
i_stream_close(mstream->istream.parent);
}
static ssize_t
i_stream_read_copy_from(struct istream *istream, struct istream *source)
{
struct istream_private *stream = istream->real_stream;
size_t pos;
ssize_t ret;
stream->pos -= stream->skip;
stream->skip = 0;
stream->buffer = i_stream_get_data(source, &pos);
if (pos > stream->pos)
ret = 0;
else do {
if ((ret = i_stream_read(source)) == -2)
return -2;
stream->istream.stream_errno = source->stream_errno;
stream->istream.eof = source->eof;
stream->buffer = i_stream_get_data(source, &pos);
/* check again, in case the source stream had been seeked
backwards and the previous read() didn't get us far
enough. */
} while (pos <= stream->pos && ret > 0);
ret = pos > stream->pos ? (ssize_t)(pos - stream->pos) :
(ret == 0 ? 0 : -1);
stream->pos = pos;
i_assert(ret != -1 || stream->istream.eof ||
stream->istream.stream_errno != 0);
return ret;
}
static ssize_t
i_stream_mail_filter_read_once(struct mail_filter_istream *mstream)
{
struct istream_private *stream = &mstream->istream;
ssize_t ret;
if (mstream->ext_out != NULL) {
/* we haven't sent everything yet */
(void)o_stream_send_istream(mstream->ext_out, stream->parent);
if (mstream->ext_out->stream_errno != 0) {
stream->istream.stream_errno =
mstream->ext_out->stream_errno;
return -1;
}
if (i_stream_is_eof(stream->parent)) {
o_stream_destroy(&mstream->ext_out);
/* if we wanted to be a blocking stream,
from now on the rest of the reads are */
if (stream->istream.blocking)
net_set_nonblock(mstream->fd, FALSE);
if (shutdown(mstream->fd, SHUT_WR) < 0)
i_error("ext-filter: shutdown() failed: %m");
}
}
i_stream_skip(mstream->ext_in, mstream->prev_ret);
ret = i_stream_read_copy_from(&stream->istream, mstream->ext_in);
mstream->prev_ret = ret < 0 ? 0 : ret;
return ret;
}
static ssize_t i_stream_mail_filter_read(struct istream_private *stream)
{
struct mail_filter_istream *mstream =
(struct mail_filter_istream *)stream;
ssize_t ret;
if (mstream->ext_in == NULL) {
stream->istream.stream_errno = EIO;
return -1;
}
while ((ret = i_stream_mail_filter_read_once(mstream)) == 0) {
if (!stream->istream.blocking)
break;
}
if (ret == -1 && !i_stream_have_bytes_left(&stream->istream) &&
stream->istream.v_offset == 0) {
/* EOF without any input -> assume the script is repoting
failure. pretty ugly way, but currently there's no error
reporting channel. */
stream->istream.stream_errno = EIO;
}
return ret;
}
static int
i_stream_mail_filter_stat(struct istream_private *stream, bool exact)
{
const struct stat *st;
i_assert(!exact);
if (i_stream_stat(stream->parent, exact, &st) < 0) {
stream->istream.stream_errno = stream->parent->stream_errno;
return -1;
}
stream->statbuf = *st;
return 0;
}
static int filter_connect(struct mail_filter_istream *mstream,
const char *socket_path, const char *args)
{
const char **argv;
string_t *str;
ssize_t ret;
int fd;
argv = t_strsplit(args, " ");
if ((fd = net_connect_unix_with_retries(socket_path, 1000)) < 0) {
if (errno == EACCES) {
i_error("ext-filter: %s",
eacces_error_get("net_connect_unix",
socket_path));
} else {
i_error("ext-filter: net_connect_unix(%s) failed: %m",
socket_path);
}
return -1;
}
if (mstream->istream.istream.blocking)
net_set_nonblock(fd, FALSE);
mstream->fd = fd;
mstream->ext_in =
i_stream_create_fd(fd, mstream->istream.max_buffer_size, FALSE);
mstream->ext_out = o_stream_create_fd(fd, 0, FALSE);
str = t_str_new(256);
str_append(str, "VERSION\tscript\t4\t0\nnoreply\n");
for (; *argv != NULL; argv++) {
str_append(str, *argv);
str_append_c(str, '\n');
}
str_append_c(str, '\n');
ret = o_stream_send(mstream->ext_out, str_data(str), str_len(str));
if (ret < 0) {
i_error("ext-filter: write(%s) failed: %s", socket_path,
o_stream_get_error(mstream->ext_out));
i_stream_mail_filter_close(&mstream->istream.iostream, FALSE);
return -1;
}
i_assert((size_t)ret == str_len(str));
return 0;
}
struct istream *
i_stream_create_ext_filter(struct istream *input, const char *socket_path,
const char *args)
{
struct mail_filter_istream *mstream;
mstream = i_new(struct mail_filter_istream, 1);
mstream->istream.iostream.close = i_stream_mail_filter_close;
mstream->istream.max_buffer_size = input->real_stream->max_buffer_size;
mstream->istream.read = i_stream_mail_filter_read;
mstream->istream.stat = i_stream_mail_filter_stat;
mstream->istream.istream.readable_fd = FALSE;
mstream->istream.istream.blocking = input->blocking;
mstream->istream.istream.seekable = FALSE;
mstream->fd = -1;
(void)filter_connect(mstream, socket_path, args);
return i_stream_create(&mstream->istream, input, mstream->fd);
}