summaryrefslogtreecommitdiff
path: root/usr.sbin/smtpd/store.c
blob: f51550cb317266d5222f513c6459eaf2ff0e873e (plain)
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
288
289
290
291
292
293
294
295
296
297
298
299
/*	$OpenBSD: store.c,v 1.9 2009/01/01 16:15:47 jacekm 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 <errno.h>
#include <event.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "smtpd.h"

int file_copy(FILE *, FILE *, size_t);
int file_append(FILE *, FILE *);

int
file_copy(FILE *dest, FILE *src, size_t len)
{
	char buffer[8192];
	size_t rlen;

	for (; len;) {

		rlen = len < sizeof(buffer) ? len : sizeof(buffer);

		if (fread(buffer, 1, rlen, src) != rlen)
			return 0;

		if (fwrite(buffer, 1, rlen, dest) != rlen)
			return 0;

		len -= rlen;
	}

	return 1;
}

int
file_append(FILE *dest, FILE *src)
{
	struct stat sb;
	size_t srcsz;

	if (fstat(fileno(src), &sb) == -1)
		return 0;
	srcsz = sb.st_size;

	if (! file_copy(dest, src, srcsz))
		return 0;

	return 1;
}

int
store_write_header(struct batch *batchp, struct message *messagep, FILE *fp)
{
	time_t tm;
	char timebuf[26];	/* current time	 */
	char ctimebuf[26];	/* creation time */
	void *p;
	char addrbuf[INET6_ADDRSTRLEN];

	tm = time(NULL);
	ctime_r(&tm, timebuf);
	timebuf[strcspn(timebuf, "\n")] = '\0';

	tm = time(&messagep->creation);
	ctime_r(&tm, ctimebuf);
	ctimebuf[strcspn(ctimebuf, "\n")] = '\0';

	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 (batchp->type & T_DAEMON_BATCH) {

		if (fprintf(fp, "From %s@%s %s\n", "MAILER-DAEMON",
			batchp->env->sc_hostname, timebuf) == -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\n",
			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, ctimebuf) == -1) {
			return 0;
		}
		return 1;
	}

	if (fprintf(fp, "From %s@%s %s\n"
		"Received: from %s (%s [%s%s])\n"
		"\tby %s with ESMTP id %s\n"
		"\tfor <%s@%s>; %s\n",
		messagep->sender.user, messagep->sender.domain, timebuf,
		messagep->session_helo, messagep->session_hostname,
		messagep->session_ss.ss_family == PF_INET ? "" : "IPv6:", addrbuf,
		batchp->env->sc_hostname, batchp->message_id,
		messagep->recipient.user, messagep->recipient.domain, ctimebuf) == -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(messagep->mboxfd, "a");
	if (mboxfp == NULL)
		return 0;

	messagefp = fdopen(messagep->messagefd, "r");
	if (messagefp == NULL)
		goto bad;

	if (! store_write_header(batchp, messagep, mboxfp))
		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_append(mboxfp, messagefp))
		goto bad;

	if (fprintf(mboxfp, "\n") == -1)
		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(messagep->mboxfd, "a");
	if (mboxfp == NULL)
		return 0;

	messagefp = fdopen(messagep->messagefd, "r");
	if (messagefp == NULL)
		goto bad;

	if (! store_write_header(batchp, messagep, mboxfp))
		goto bad;

	if (! file_append(mboxfp, messagefp))
		goto bad;

	if (fprintf(mboxfp, "\n") == -1)
		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(messagep->mboxfd, &sb) == -1)
		return 0;

	if (! writer(batchp, messagep)) {
		if (S_ISREG(sb.st_mode)) {
			ftruncate(messagep->mboxfd, sb.st_size);
			return 0;
		}
		return 0;
	}
	return 1;
}