summaryrefslogtreecommitdiff
path: root/usr.sbin/smtpd/store.c
blob: 3c9f32ec531d9c4faf2d2f9133952ba7dad476fd (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
/*	$OpenBSD: store.c,v 1.25 2009/08/08 00:02:22 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 <ctype.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 *dest, FILE *src, struct path *path, enum action_type type, int session)
{
	char *buf, *lbuf;
	size_t len;
	char *escape;
	int inheaders = 1;

	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 we are NOT dealing with a mailer daemon copy, we have
		 * path set to the original recipient. In that case, we can
		 * add the Delivered-To header to help loop detection.
		 */
		if (!session && path != NULL && inheaders &&
		    strchr(buf, ':') == NULL && !isspace((int)*buf)) {
			if (fprintf(dest, "Delivered-To: %s@%s\n",
				path->user, path->domain) == -1)
				return 0;
			inheaders = 0;
		}

		if (!session && type == A_MBOX) {
			escape = buf;
			while (*escape == '>')
				++escape;
			if (strncmp("From ", escape, 5) == 0) {
				if (fprintf(dest, ">") != 1)
					return 0;
			}
		}

		/* "If first character of the line is a period, one
		 *  additional period is inserted at the beginning."
		 * [4.5.2]
		 */
		if (session && *buf == '.')
			if (fprintf(dest, ".") != 1)
				return 0;

		if (fprintf(dest, "%s%s", buf, session ? "\r\n" : "\n") !=
			(int)len + (session ? 2 : 1))
			return 0;
	}
	free(lbuf);

	if (!session && type == A_MBOX) {
		if (fprintf(dest, "\n") != 1)
			return 0;
	}

	return 1;
}

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 (! file_copy(mboxfp, messagefp, &messagep->session_rcpt,
		messagep->recipient.rule.r_action, 0))
		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)
{
	struct stat sb;

	if (fstat(batchp->sessionp->mboxfd, &sb) == -1)
		return 0;

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