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
|
/* $OpenBSD: store.c,v 1.17 2009/03/15 19:15:25 gilles Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.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 <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <event.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "smtpd.h"
int file_copy(FILE *, FILE *, enum action_type);
int
file_copy(FILE *dest, FILE *src, enum action_type type)
{
char *buf, *lbuf;
size_t len;
char *escape;
lbuf = NULL;
while ((buf = fgetln(src, &len))) {
if (buf[len - 1] == '\n') {
buf[len - 1] = '\0';
len--;
}
else {
/* EOF without EOL, copy and add the NUL */
if ((lbuf = malloc(len + 1)) == NULL)
err(1, NULL);
memcpy(lbuf, buf, len);
lbuf[len] = '\0';
buf = lbuf;
}
if (type == A_MBOX) {
escape = buf;
while (*escape == '>')
++escape;
if (strncmp("From ", escape, 5) == 0) {
if (fprintf(dest, ">") != 1)
return 0;
}
}
if (fprintf(dest, "%s\n", buf) != (int)len + 1)
return 0;
}
free(lbuf);
if (type == A_MBOX) {
if (fprintf(dest, "\n") != 1)
return 0;
}
return 1;
}
int
store_write_header(struct batch *batchp, struct message *messagep, FILE *fp,
int finalize)
{
time_t tm;
void *p;
char addrbuf[INET6_ADDRSTRLEN];
tm = time(NULL);
if (messagep->session_ss.ss_family == PF_INET) {
struct sockaddr_in *ssin = (struct sockaddr_in *)&messagep->session_ss;
p = &ssin->sin_addr.s_addr;
}
if (messagep->session_ss.ss_family == PF_INET6) {
struct sockaddr_in6 *ssin6 = (struct sockaddr_in6 *)&messagep->session_ss;
p = &ssin6->sin6_addr.s6_addr;
}
bzero(addrbuf, sizeof (addrbuf));
inet_ntop(messagep->session_ss.ss_family, p, addrbuf, sizeof (addrbuf));
if (messagep->recipient.rule.r_action != A_MBOX) {
if (batchp->type & T_DAEMON_BATCH) {
if (fprintf(fp, "From %s@%s %s\n", "MAILER-DAEMON",
batchp->env->sc_hostname, time_to_text(tm)) == -1) {
return 0;
}
}
else {
if (fprintf(fp, "From %s@%s %s\n",
messagep->sender.user, messagep->sender.domain, time_to_text(tm)) == -1)
return 0;
}
}
if (fprintf(fp, "Received: from %s (%s [%s%s])\n"
"\tby %s with ESMTP id %s\n"
"\tfor <%s@%s>; %s\n%s",
messagep->session_helo, messagep->session_hostname,
messagep->session_ss.ss_family == PF_INET ? "" : "IPv6:", addrbuf,
batchp->env->sc_hostname, messagep->message_id,
messagep->sender.user, messagep->sender.domain, time_to_text(messagep->creation),
finalize ? "\n" : "") == -1) {
return 0;
}
return 1;
}
int
store_write_daemon(struct batch *batchp, struct message *messagep)
{
u_int32_t i;
struct message *recipient;
FILE *mboxfp;
FILE *messagefp;
mboxfp = fdopen(batchp->sessionp->mboxfd, "a");
if (mboxfp == NULL)
return 0;
messagefp = fdopen(batchp->sessionp->messagefd, "r");
if (messagefp == NULL)
goto bad;
if (! store_write_header(batchp, messagep, mboxfp, 1))
goto bad;
if (fprintf(mboxfp, "Hi !\n\n"
"This is the MAILER-DAEMON, please DO NOT REPLY to this e-mail it is\n"
"just a notification to let you know that an error has occurred.\n\n") == -1)
goto bad;
if ((batchp->status & S_BATCH_PERMFAILURE) && fprintf(mboxfp,
"You ran into a PERMANENT FAILURE, which means that the e-mail can't\n"
"be delivered to the remote host no matter how much I try.\n\n"
"Diagnostic:\n"
"%s\n\n", batchp->errorline) == -1)
goto bad;
if ((batchp->status & S_BATCH_TEMPFAILURE) && fprintf(mboxfp,
"You ran into a TEMPORARY FAILURE, which means that the e-mail can't\n"
"be delivered right now, but could be deliverable at a later time. I\n"
"will attempt until it succeeds for the next four days, then let you\n"
"know if it didn't work out.\n\n"
"Diagnostic:\n"
"%s\n\n", batchp->errorline) == -1)
goto bad;
if (! (batchp->status & S_BATCH_TEMPFAILURE)) {
/* First list the temporary failures */
i = 0;
TAILQ_FOREACH(recipient, &batchp->messages, entry) {
if (recipient->status & S_MESSAGE_TEMPFAILURE) {
if (i == 0) {
if (fprintf(mboxfp,
"The following recipients caused a temporary failure:\n") == -1)
goto bad;
++i;
}
if (fprintf(mboxfp,
"\t<%s@%s>:\n%s\n\n", recipient->recipient.user, recipient->recipient.domain,
recipient->session_errorline) == -1)
goto bad;
}
}
/* Then list the permanent failures */
i = 0;
TAILQ_FOREACH(recipient, &batchp->messages, entry) {
if (recipient->status & S_MESSAGE_PERMFAILURE) {
if (i == 0) {
if (fprintf(mboxfp,
"The following recipients caused a permanent failure:\n") == -1)
goto bad;
++i;
}
if (fprintf(mboxfp,
"\t<%s@%s>:\n%s\n\n", recipient->recipient.user, recipient->recipient.domain,
recipient->session_errorline) == -1)
goto bad;
}
}
}
if (fprintf(mboxfp, "Below is a copy of the original message:\n\n") == -1)
goto bad;
if (! file_copy(mboxfp, messagefp, messagep->recipient.rule.r_action))
goto bad;
fflush(mboxfp);
fsync(fileno(mboxfp));
fclose(mboxfp);
fclose(messagefp);
return 1;
bad:
if (mboxfp != NULL)
fclose(mboxfp);
if (messagefp != NULL)
fclose(messagefp);
return 0;
}
int
store_write_message(struct batch *batchp, struct message *messagep)
{
FILE *mboxfp;
FILE *messagefp;
mboxfp = fdopen(batchp->sessionp->mboxfd, "a");
if (mboxfp == NULL)
return 0;
messagefp = fdopen(batchp->sessionp->messagefd, "r");
if (messagefp == NULL)
goto bad;
if (! store_write_header(batchp, messagep, mboxfp, 0))
goto bad;
if (! file_copy(mboxfp, messagefp, messagep->recipient.rule.r_action))
goto bad;
fflush(mboxfp);
fsync(fileno(mboxfp));
fclose(mboxfp);
fclose(messagefp);
return 1;
bad:
if (mboxfp != NULL)
fclose(mboxfp);
if (messagefp != NULL)
fclose(messagefp);
return 0;
}
int
store_message(struct batch *batchp, struct message *messagep,
int (*writer)(struct batch *, struct message *))
{
struct stat sb;
if (fstat(batchp->sessionp->mboxfd, &sb) == -1)
return 0;
if (! writer(batchp, messagep)) {
if (S_ISREG(sb.st_mode)) {
ftruncate(batchp->sessionp->mboxfd, sb.st_size);
return 0;
}
return 0;
}
return 1;
}
|