-
Notifications
You must be signed in to change notification settings - Fork 3
/
backend-qmail.c
222 lines (197 loc) · 5.93 KB
/
backend-qmail.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
215
216
217
218
219
220
221
222
#include <bglibs/sysdeps.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <bglibs/misc.h>
#include <bglibs/msg.h>
#include <bglibs/sig.h>
#include "mailfront.h"
#include "conf_qmail.c"
static RESPONSE(no_write,451,"4.3.0 Writing data to qmail-queue failed.");
static RESPONSE(no_pipe,451,"4.3.0 Could not open pipe to qmail-queue.");
static RESPONSE(no_fork,451,"4.3.0 Could not start qmail-queue.");
static RESPONSE(no_chdir,451,"4.3.0 Could not change to the qmail directory.");
static RESPONSE(qq_crashed,451,"4.3.0 qmail-queue crashed.");
static str buffer;
static unsigned long databytes;
static const char* qqargs[2] = { 0, 0 };
static int qqpid = -1;
static int qqepipe[2] = { -1, -1 };
static int qqmpipe[2] = { -1, -1 };
static void close_qqpipe(void)
{
if (qqepipe[0] != -1) close(qqepipe[0]);
if (qqepipe[1] != -1) close(qqepipe[1]);
if (qqmpipe[0] != -1) close(qqmpipe[0]);
if (qqmpipe[1] != -1) close(qqmpipe[1]);
qqepipe[0] = qqepipe[1] = qqmpipe[0] = qqmpipe[1] = -1;
}
static const response* reset(void)
{
close_qqpipe();
str_truncate(&buffer, 0);
return 0;
}
static const response* do_sender(str* sender, str* params)
{
if (!str_catc(&buffer, 'F') ||
!str_cat(&buffer, sender) ||
!str_catc(&buffer, 0)) return &resp_oom;
return 0;
(void)params;
}
static const response* do_recipient(str* recipient, str* params)
{
if (!str_catc(&buffer, 'T') ||
!str_cat(&buffer, recipient) ||
!str_catc(&buffer, 0)) return &resp_oom;
return 0;
(void)params;
}
static const response *start_qq(int msgfd, int envfd)
{
const char* qh;
qqargs[0] = session_getenv("QMAILQUEUE");
if (qqargs[0] == 0) qqargs[0] = "bin/qmail-queue";
if ((qh = session_getenv("QMAILHOME")) == 0)
qh = conf_qmail;
if (chdir(qh) == -1) return &resp_no_chdir;
if ((qqpid = fork()) == -1) {
close_qqpipe();
return &resp_no_fork;
}
if (qqpid == 0) {
if (!session_exportenv()) exit(51);
if (dup2(msgfd, 0) == -1) exit(120);
if (dup2(envfd, 1) == -1) exit(120);
close_qqpipe();
execvp(qqargs[0], (char**)qqargs);
exit(120);
}
return 0;
}
static const response* data_start(int fd)
{
const response* resp_qq;
sig_pipe_block();
if (pipe(qqepipe) == -1) return &resp_no_pipe;
if (fd < 0) {
if (pipe(qqmpipe) == -1) {
close_qqpipe();
return &resp_no_pipe;
}
resp_qq = start_qq(qqmpipe[0], qqepipe[0]);
if (resp_qq != 0) {
return resp_qq;
}
}
databytes = 0;
return 0;
(void)fd;
}
static int retry_write(int fd, const char* bytes, unsigned long len)
{
while (len) {
unsigned long written = write(fd, bytes, len);
if (written == (unsigned long)-1) return 0;
len -= written;
bytes += written;
}
return 1;
}
static const response* data_block(const char* bytes, unsigned long len)
{
if (qqmpipe[1] >= 0) {
if (!retry_write(qqmpipe[1], bytes, len))
return &resp_no_write;
databytes += len;
}
return 0;
}
static void parse_status(int status, response* resp)
{
char var[20];
const char* message;
resp->number = (status <= 40 && status >= 11) ? 554 : 451;
memcpy(var, "QQERRMSG_", 9);
strcpy(var+9, utoa(status));
if ((message = session_getenv(var)) == 0) {
switch (status) {
case 11: message = "5.1.3 Address too long."; break;
case 31: message = "5.3.0 Message refused."; break;
case 51: message = "4.3.0 Out of memory."; break;
case 52: message = "4.3.0 Timeout."; break;
case 53: message = "4.3.0 Write error (queue full?)."; break;
case 54: message = "4.3.0 Unable to read the message or envelope."; break;
case 55: message = "4.3.0 Unable to read a configuration file."; break;
case 56: message = "4.3.0 Network problem."; break;
case 61: message = "4.3.0 Problem with the qmail home directory."; break;
case 62: message = "4.3.0 Problem with the qmail queue directory."; break;
case 63: message = "4.3.0 Problem with queue/pid."; break;
case 64: message = "4.3.0 Problem with queue/mess."; break;
case 65: message = "4.3.0 Problem with queue/intd."; break;
case 66: message = "4.3.0 Problem with queue/todo."; break;
case 71: message = "4.3.0 Message refused by mail server."; break;
case 72: message = "4.3.0 Connection to mail server timed out."; break;
case 73: message = "4.3.0 Connection to mail server rejected."; break;
case 74: message = "4.3.0 Communication with mail server failed."; break;
case 81: message = "4.3.0 Internal qmail-queue bug."; break;
case 91: message = "4.3.0 Envelope format error."; break;
default:
message = (resp->number >= 500)
? "5.3.0 Message rejected by qmail-queue."
: "4.3.0 Temporary qmail-queue failure.";
}
}
resp->message = message;
}
static const response* message_end(int fd)
{
static response resp;
const response* resp_qq;
int status;
struct stat st;
if (fd < 0) {
close(qqmpipe[1]);
qqmpipe[1] = -1;
}
else {
if (lseek(fd, 0, SEEK_SET) != 0)
return &resp_internal;
if (fstat(fd, &st) != 0)
return &resp_internal;
databytes = st.st_size;
resp_qq = start_qq(fd, qqepipe[0]);
if (resp_qq != 0) {
return resp_qq;
}
}
if (!retry_write(qqepipe[1], buffer.s, buffer.len+1)) return &resp_no_write;
close_qqpipe();
if (waitpid(qqpid, &status, WUNTRACED) == -1) return &resp_qq_crashed;
if (!WIFEXITED(status)) return &resp_qq_crashed;
if ((status = WEXITSTATUS(status)) != 0)
parse_status(status, &resp);
else {
str_copys(&buffer, "2.6.0 Accepted message qp ");
str_catu(&buffer, qqpid);
str_cats(&buffer, " bytes ");
str_catu(&buffer, databytes);
msg1(buffer.s);
resp.number = 250;
resp.message = buffer.s;
}
return &resp;
(void)fd;
}
struct plugin backend = {
.version = PLUGIN_VERSION,
.reset = reset,
.sender = do_sender,
.recipient = do_recipient,
.data_start = data_start,
.data_block = data_block,
.message_end = message_end,
};