aboutsummaryrefslogtreecommitdiffstats
path: root/smtpd/config.c
blob: 05e2542c780d8bc534253e7ff72ebf726c98505c (plain) (blame)
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
/*	$OpenBSD: config.c,v 1.17 2012/09/27 17:47:49 chl Exp $	*/

/*
 * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@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 "includes.h"

#include <sys/types.h>
#include "sys-queue.h"
#include "sys-tree.h"
#include <sys/param.h>
#include <sys/socket.h>

#include <event.h>
#include <imsg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "smtpd.h"
#include "log.h"

static int is_peer(struct peer *, enum smtp_proc_type, uint);

static int
is_peer(struct peer *p, enum smtp_proc_type peer, uint peercount)
{
	uint	i;

	for (i = 0; i < peercount; i++)
		if (p[i].id == peer)
			return (1);
	return (0);
}

void
unconfigure(void)
{
}

void
configure(void)
{
}

void
purge_config(uint8_t what)
{
	struct listener	*l;
	struct map	*m;
	struct rule	*r;
	struct ssl	*s;
	struct mapel	*me;

	if (what & PURGE_LISTENERS) {
		while ((l = TAILQ_FIRST(env->sc_listeners)) != NULL) {
			TAILQ_REMOVE(env->sc_listeners, l, entry);
			free(l);
		}
		free(env->sc_listeners);
		env->sc_listeners = NULL;
	}
	if (what & PURGE_MAPS) {
		while ((m = TAILQ_FIRST(env->sc_maps)) != NULL) {
			TAILQ_REMOVE(env->sc_maps, m, m_entry);
			while ((me = TAILQ_FIRST(&m->m_contents))) {
				TAILQ_REMOVE(&m->m_contents, me, me_entry);
				free(me);
			}
			free(m);
		}
		free(env->sc_maps);
		env->sc_maps = NULL;
	}
	if (what & PURGE_RULES) {
		while ((r = TAILQ_FIRST(env->sc_rules)) != NULL) {
			TAILQ_REMOVE(env->sc_rules, r, r_entry);
			free(r);
		}
		free(env->sc_rules);
		env->sc_rules = NULL;
	}
	if (what & PURGE_SSL) {
		while ((s = SPLAY_ROOT(env->sc_ssl)) != NULL) {
			SPLAY_REMOVE(ssltree, env->sc_ssl, s);
			free(s->ssl_cert);
			free(s->ssl_key);
			free(s);
		}
		free(env->sc_ssl);
		env->sc_ssl = NULL;
	}
}

void
init_pipes(void)
{
	int	 i;
	int	 j;
	int	 count;
	int	 sockpair[2];

	for (i = 0; i < PROC_COUNT; i++)
		for (j = 0; j < PROC_COUNT; j++) {
			/*
			 * find out how many instances of this peer there are.
			 */
			if (i >= j || env->sc_instances[i] == 0||
			   env->sc_instances[j] == 0)
				continue;

			if (env->sc_instances[i] > 1 &&
			    env->sc_instances[j] > 1)
				fatalx("N:N peering not supported");

			count = env->sc_instances[i] * env->sc_instances[j];

			env->sc_pipes[i][j] = xcalloc(count, sizeof(int),
			    "init_pipes");
			env->sc_pipes[j][i] = xcalloc(count, sizeof(int),
			    "init_pipes");

			while (--count >= 0) {
				if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC,
				    sockpair) == -1)
					fatal("socketpair");
				env->sc_pipes[i][j][count] = sockpair[0];
				env->sc_pipes[j][i][count] = sockpair[1];
				session_socket_blockmode(
				    env->sc_pipes[i][j][count],
				    BM_NONBLOCK);
				session_socket_blockmode(
				    env->sc_pipes[j][i][count],
				    BM_NONBLOCK);
			}
		}
}

void
config_pipes(struct peer *p, uint peercount)
{
	uint	i;
	uint	j;
	int	count;

	/*
	 * close pipes
	 */
	for (i = 0; i < PROC_COUNT; i++) {
		for (j = 0; j < PROC_COUNT; j++) {
			if (i == j ||
			    env->sc_instances[i] == 0 ||
			    env->sc_instances[j] == 0)
				continue;

			for (count = 0;
			    count < env->sc_instances[i]*env->sc_instances[j];
			    count++) {
				if (i == smtpd_process &&
				    is_peer(p, j, peercount) &&
				    count == env->sc_instance)
					continue;
				if (i == smtpd_process &&
				    is_peer(p, j, peercount) &&
				    env->sc_instances[i] == 1)
					continue;
				close(env->sc_pipes[i][j][count]);
				env->sc_pipes[i][j][count] = -1;
			}
		}
	}
}

void
config_peers(struct peer *p, uint peercount)
{
	int	count;
	uint	src;
	uint	dst;
	uint	i;
	/*
	 * listen on appropriate pipes
	 */
	for (i = 0; i < peercount; i++) {

		src = smtpd_process;
		dst = p[i].id;

		if (dst == smtpd_process)
			fatal("config_peers: cannot peer with oneself");
		
		env->sc_ievs[dst] = xcalloc(env->sc_instances[dst],
		    sizeof(struct imsgev), "config_peers");

		for (count = 0; count < env->sc_instances[dst]; count++) {
			imsg_init(&(env->sc_ievs[dst][count].ibuf),
			    env->sc_pipes[src][dst][count]);
			env->sc_ievs[dst][count].handler =  p[i].cb;
			env->sc_ievs[dst][count].events = EV_READ;
			env->sc_ievs[dst][count].proc = dst;
			env->sc_ievs[dst][count].data = &env->sc_ievs[dst][count];

			event_set(&(env->sc_ievs[dst][count].ev),
			    env->sc_ievs[dst][count].ibuf.fd,
			    env->sc_ievs[dst][count].events,
			    env->sc_ievs[dst][count].handler,
			    env->sc_ievs[dst][count].data);
			event_add(&(env->sc_ievs[dst][count].ev), NULL);
		}
	}
}

#ifdef VALGRIND
void free_peers(void)
{
	u_int	i;

	for (i = 0; i < PROC_COUNT; i++)
		if (env->sc_ievs[i])
			free(env->sc_ievs[i]);
}

void free_pipes(void)
{
	u_int	i;
	u_int	j;

	for (i = 0; i < PROC_COUNT; i++)
		for (j = 0; j < PROC_COUNT; j++) {

			if (i >= j || env->sc_instances[i] == 0 ||
			   env->sc_instances[j] == 0)
				continue;

			free(env->sc_pipes[i][j]);
			free(env->sc_pipes[j][i]);

		}
}
#endif