File: listener.c

package info (click to toggle)
dovecot 1%3A1.2.15-7
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 30,252 kB
  • ctags: 19,837
  • sloc: ansic: 191,438; sh: 21,091; makefile: 3,330; cpp: 526; perl: 108; xml: 44
file content (396 lines) | stat: -rw-r--r-- 9,838 bytes parent folder | download | duplicates (2)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* Copyright (c) 2002-2010 Dovecot authors, see the included COPYING file */

#include "common.h"
#include "array.h"
#include "ioloop.h"
#include "fd-close-on-exec.h"
#include "listener.h"

#include <stdlib.h>
#include <unistd.h>

static void resolve_ip(const char *set_name, const char *name,
		       struct ip_addr *ip, unsigned int *port)
{
	struct ip_addr *ip_list;
	const char *p;
	unsigned int ips_count;
	int ret;

	if (*name == '\0') {
                /* defaults to "*" or "[::]" */
		ip->family = 0;
		return;
	}

	if (name[0] == '[') {
		/* IPv6 address */
		p = strchr(name, ']');
		if (p == NULL) {
			i_fatal("%s: Missing ']' in address %s",
				set_name, name);
		}
		name = t_strdup_until(name+1, p);

		p++;
		if (*p == '\0')
			p = NULL;
		else if (*p != ':') {
			i_fatal("%s: Invalid data after ']' in address %s",
				set_name, name);
		}
#ifndef HAVE_IPV6
		i_fatal("%s: Compiled without IPv6 support, can't listen on %s",
			set_name, name);
#endif
	} else {
		p = strrchr(name, ':');
		if (p != NULL)
			name = t_strdup_until(name, p);
	}

	if (p != NULL) {
		if (!is_numeric(p+1, '\0')) {
			i_fatal("%s: Invalid port in address %s",
				set_name, name);
		}
		*port = atoi(p+1);
	}

	if (strcmp(name, "*") == 0) {
		/* IPv4 any */
		net_get_ip_any4(ip);
		return;
	}

	if (strcmp(name, "::") == 0) {
		/* IPv6 any */
		net_get_ip_any6(ip);
		return;
	}

	/* Return the first IP if there happens to be multiple. */
	ret = net_gethostbyname(name, &ip_list, &ips_count);
	if (ret != 0) {
		i_fatal("%s: Can't resolve address %s: %s",
			set_name, name, net_gethosterror(ret));
	}

	if (ips_count < 1)
		i_fatal("%s: No IPs for address: %s", set_name, name);

	*ip = ip_list[0];
}

static void
check_conflicts_set(const struct settings *set, const struct ip_addr *ip,
		    unsigned int port, const char *name1, const char *name2)
{
	const struct listener *listens = NULL;
	unsigned int i, count;

	if (array_is_created(&set->listens))
		listens = array_get(&set->listens, &count);
	else
		count = 0;
	for (i = 0; i < count; i++) {
		if (listens[i].fd <= 0 || listens[i].port != port ||
		    !net_ip_compare(&listens[i].ip, ip))
			continue;

		i_fatal("Protocols %s and %s are listening in same ip/port",
			name1, name2);
	}

	if (array_is_created(&set->ssl_listens))
		listens = array_get(&set->ssl_listens, &count);
	else
		count = 0;
	for (i = 0; i < count; i++) {
		if (listens[i].fd <= 0 || listens[i].port != port ||
		    !net_ip_compare(&listens[i].ip, ip))
			continue;

		i_fatal("Protocols %ss and %ss are listening in same ip/port",
			name1, name2);
	}
}

static void check_conflicts(const struct ip_addr *ip, unsigned int port,
			    const char *proto)
{
	struct server_settings *server;

	for (server = settings_root; server != NULL; server = server->next) {
		if (server->imap != NULL) {
			check_conflicts_set(server->imap, ip, port,
					    "imap", proto);
		}
		if (server->pop3 != NULL) {
			check_conflicts_set(server->pop3, ip, port,
					    "pop3", proto);
		}
		 if (server->managesieve != NULL) {
            check_conflicts_set(server->managesieve, ip, port,
                        "managesieve", proto);
        }
	}
}

static void
listener_init(const char *set_name, const char *listen_list,
	      unsigned int default_port, ARRAY_TYPE(listener) *listens_arr)
{
	const char *const *tmp;
	struct listener l, *listens;
	unsigned int i, count;

	if (!array_is_created(listens_arr))
		i_array_init(listens_arr, 4);

	listens = array_get_modifiable(listens_arr, &count);
	for (i = 0; i < count; i++)
		listens[i].wanted = FALSE;

	memset(&l, 0, sizeof(l));
	l.fd = -1;
	l.wanted = TRUE;

	for (tmp = t_strsplit_spaces(listen_list, ", "); *tmp != NULL; tmp++) {
		l.port = default_port;
		resolve_ip(set_name, *tmp, &l.ip, &l.port);

		/* see if it already exists */
		for (i = 0; i < count; i++) {
			if (listens[i].port == l.port &&
			    net_ip_compare(&listens[i].ip, &l.ip)) {
				listens[i].wanted = TRUE;
				break;
			}
		}

		if (i == count) {
			array_append(listens_arr, &l, 1);
			listens = array_get_modifiable(listens_arr, &count);
		}
	}

	/* close unwanted fds */
	for (i = 0; i < count; ) {
		if (listens[i].wanted)
			i++;
		else {
			if (listens[i].fd > 0) {
				if (close(listens[i].fd) < 0)
					i_error("close(listener) failed: %m");
			}
			array_delete(listens_arr, i, 1);
			listens = array_get_modifiable(listens_arr, &count);
		}
	}
}

static void listener_close_fds(ARRAY_TYPE(listener) *listens_arr)
{
	const struct listener *listens;
	unsigned int i, count;

	if (!array_is_created(listens_arr))
		return;

	listens = array_get(listens_arr, &count);
	for (i = 0; i < count; i++) {
		if (listens[i].fd > 0) {
			if (close(listens[i].fd) < 0)
				i_error("close(listener) failed: %m");
		}
	}
	array_free(listens_arr);
}

static void listen_parse_and_close_unneeded(struct settings *set)
{
	const char *const *proto;
	unsigned int default_port;
	bool nonssl_listen = FALSE, ssl_listen = FALSE;

	if (set == NULL)
		return;

	/* register wanted protocols */
        proto = t_strsplit_spaces(set->protocols, " ");
	for (; *proto != NULL; proto++) {
		if (strcasecmp(*proto, "imap") == 0) {
			if (set->protocol == MAIL_PROTOCOL_IMAP)
				nonssl_listen = TRUE;
		} else if (strcasecmp(*proto, "imaps") == 0) {
			if (set->protocol == MAIL_PROTOCOL_IMAP &&
			    strcmp(set->ssl, "no") != 0)
				ssl_listen = TRUE;
		} else if (strcasecmp(*proto, "pop3") == 0) {
			if (set->protocol == MAIL_PROTOCOL_POP3)
				nonssl_listen = TRUE;
		} else if (strcasecmp(*proto, "pop3s") == 0) {
			if (set->protocol == MAIL_PROTOCOL_POP3 &&
			    strcmp(set->ssl, "no") != 0)
				ssl_listen = TRUE;
		} else if (strcasecmp(*proto, "managesieve") == 0) {
			if (set->protocol == MAIL_PROTOCOL_MANAGESIEVE)
				nonssl_listen = TRUE;
		}
	}

	if (!nonssl_listen)
		listener_close_fds(&set->listens);
	else {
		switch (set->protocol) {
		case MAIL_PROTOCOL_IMAP:
			default_port = 143;
			break;
		case MAIL_PROTOCOL_POP3:
			default_port = 110;
			break;
		case MAIL_PROTOCOL_MANAGESIEVE:
			default_port = 2000;
			break;
		default:
			i_unreached();
		}

		listener_init("listen", set->listen, default_port,
			      &set->listens);
	}
	if (!ssl_listen)
		listener_close_fds(&set->ssl_listens);
	else {
		default_port = set->protocol == MAIL_PROTOCOL_IMAP ? 993 : 995;
		listener_init("ssl_listen", *set->ssl_listen != '\0' ?
			      set->ssl_listen : set->listen, default_port,
			      &set->ssl_listens);
	}
}

static void listen_copy_old(struct settings *old_set, struct settings *new_set)
{
	if (old_set == NULL || new_set == NULL) {
		if (old_set != NULL) {
			listener_close_fds(&old_set->listens);
			listener_close_fds(&old_set->ssl_listens);
		}
		return;
	}

	i_assert(!array_is_created(&new_set->listens));
	i_assert(!array_is_created(&new_set->ssl_listens));

	new_set->listens = old_set->listens;
	new_set->ssl_listens = old_set->ssl_listens;

	old_set->listens.arr.buffer = NULL;
	old_set->ssl_listens.arr.buffer = NULL;
}

static void
listener_array_listen_missing(const char *proto,
			      ARRAY_TYPE(listener) *listens_arr, bool retry)
{
	struct listener *listens;
	unsigned int i, j, count;

	if (!array_is_created(listens_arr))
		return;

	listens = array_get_modifiable(listens_arr, &count);
	for (i = 0; i < count; i++) {
		if (listens[i].fd > 0)
			continue;

		for (j = 0; j < 10; j++) {
			listens[i].fd = net_listen(&listens[i].ip,
						   &listens[i].port, 128);
			if (listens[i].fd != -1)
				break;

			if (errno == EADDRINUSE) {
				/* retry */
			} else if (errno == EINTR &&
				   io_loop_is_running(ioloop)) {
				/* SIGHUPing sometimes gets us here.
				   we don't want to die. */
			} else {
				/* error */
				break;
			}

			check_conflicts(&listens[i].ip, listens[i].port, proto);
			if (!retry)
				break;

			/* wait a while and try again. we're SIGHUPing
			   so we most likely just closed it ourself.. */
			sleep(1);
		}

		if (listens[i].fd == -1) {
			i_fatal("listen(%s, %d) failed: %m",
				net_ip2addr(&listens[i].ip), listens[i].port);
		}
		net_set_nonblock(listens[i].fd, TRUE);
		fd_close_on_exec(listens[i].fd, TRUE);
	}
}

static void
listener_listen_missing(struct settings *set, const char *proto, bool retry)
{
	listener_array_listen_missing(proto, &set->listens, retry);
	listener_array_listen_missing(t_strconcat(proto, "s", NULL),
				      &set->ssl_listens, retry);
}

void listeners_open_fds(struct server_settings *old_set, bool retry)
{
	struct server_settings *server;

	for (server = settings_root; server != NULL; server = server->next) {
		if (old_set != NULL) {
			listen_copy_old(old_set->imap, server->imap);
			listen_copy_old(old_set->pop3, server->pop3);
			listen_copy_old(old_set->managesieve, server->managesieve);
		}
		listen_parse_and_close_unneeded(server->imap);
		listen_parse_and_close_unneeded(server->pop3);
		listen_parse_and_close_unneeded(server->managesieve);

		if (old_set != NULL)
			old_set = old_set->next;
	}

	for (server = settings_root; server != NULL; server = server->next) {
		if (server->imap != NULL)
			listener_listen_missing(server->imap, "imap", retry);
		if (server->pop3 != NULL)
			listener_listen_missing(server->pop3, "pop3", retry);
		if (server->managesieve != NULL)
			listener_listen_missing(server->managesieve, "managesieve", retry);
	}
}

void listeners_close_fds(void)
{
	struct server_settings *server;

	for (server = settings_root; server != NULL; server = server->next) {
		if (server->imap != NULL) {
			listener_close_fds(&server->imap->listens);
			listener_close_fds(&server->imap->ssl_listens);
		}
		if (server->pop3 != NULL) {
			listener_close_fds(&server->pop3->listens);
			listener_close_fds(&server->pop3->ssl_listens);
		}
		if (server->managesieve != NULL) {
			listener_close_fds(&server->managesieve->listens);
		}
	}
}