summaryrefslogtreecommitdiffstats
path: root/src/lib-master/master-service-settings-cache.c
blob: 11dd66bb5a20597926ba3ee6baeb8cf8bace454a (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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "wildcard-match.h"
#include "hash.h"
#include "llist.h"
#include "settings-parser.h"
#include "dns-util.h"
#include "strescape.h"
#include "master-service-private.h"
#include "master-service-settings.h"
#include "master-service-settings-cache.h"

/* we start with just a guess. it's updated later. */
#define CACHE_INITIAL_ENTRY_POOL_SIZE (1024*16)
#define CACHE_ADD_ENTRY_POOL_SIZE 1024

struct config_filter {
	struct config_filter *prev, *next;

	const char *local_name;
	struct ip_addr local_ip, remote_ip;
	unsigned int local_bits, remote_bits;
};

struct settings_entry {
	struct settings_entry *prev, *next;

	pool_t pool;
	const char *local_name;
	struct ip_addr local_ip;

	struct setting_parser_context *parser;
};

struct master_service_settings_cache {
	pool_t pool;

	struct master_service *service;
	const char *module;
	const char *service_name;
	size_t max_cache_size;

	/* global settings for this service (after they've been read) */
	struct setting_parser_context *global_parser;

	/* cache for other settings (local_ip/local_name set) */
	struct settings_entry *oldest, *newest;
	/* separate list for entries whose parser=global_parser */
	struct settings_entry *oldest_global, *newest_global;
	/* local_name, local_ip => struct settings_entry */
	HASH_TABLE(char *, struct settings_entry *) local_name_hash;
	HASH_TABLE(struct ip_addr *, struct settings_entry *) local_ip_hash;

	struct config_filter *filters;

	/* Initial size for new settings entry pools */
	size_t approx_entry_pool_size;
	/* number of bytes malloced by cached settings entries
	   (doesn't count memory used by hash table or global sets) */
	size_t cache_malloc_size;

	bool done_initial_lookup:1;
	bool service_uses_local:1;
	bool service_uses_remote:1;
};

struct master_service_settings_cache *
master_service_settings_cache_init(struct master_service *service,
				   const char *module, const char *service_name)
{
	struct master_service_settings_cache *cache;
	pool_t pool;

	pool = pool_alloconly_create(MEMPOOL_GROWING"master service settings cache",
				     1024*12);
	cache = p_new(pool, struct master_service_settings_cache, 1);
	cache->pool = pool;
	cache->service = service;
	cache->module = p_strdup(pool, module);
	cache->service_name = p_strdup(pool, service_name);
	cache->max_cache_size = SIZE_MAX;
	return cache;
}

int master_service_settings_cache_init_filter(struct master_service_settings_cache *cache)
{
	const char *const *filters;
	const char *error;

	if (cache->filters != NULL)
		return 0;
	if (master_service_settings_get_filters(cache->service, &filters, &error) < 0) {
		i_error("master-service: cannot get filters: %s", error);
		return -1;
	}

	/* parse filters */
	while(*filters != NULL) {
		const char *const *keys = t_strsplit_tabescaped(*filters);
		struct config_filter *filter =
			p_new(cache->pool, struct config_filter, 1);
		while(*keys != NULL) {
			if (str_begins(*keys, "local-net=")) {
				(void)net_parse_range((*keys)+10,
					&filter->local_ip, &filter->local_bits);
			} else if (str_begins(*keys, "remote-net=")) {
				(void)net_parse_range((*keys)+11,
					&filter->remote_ip, &filter->remote_bits);
			} else if (str_begins(*keys, "local-name=")) {
				filter->local_name = p_strdup(cache->pool, (*keys)+11);
			}
			keys++;
		}
		DLLIST_PREPEND(&cache->filters, filter);
		filters++;
	}
	return 0;
}

static bool
match_local_name(const char *local_name,
		 const char *filter_local_name)
{
	/* Handle multiple names separated by spaces in local_name
	   * Ex: local_name "mail.domain.tld domain.tld mx.domain.tld" { ... } */
	const char *ptr;
	while((ptr = strchr(filter_local_name, ' ')) != NULL) {
		if (dns_match_wildcard(local_name,
		    t_strdup_until(filter_local_name, ptr)) == 0)
			return TRUE;
		filter_local_name = ptr+1;
	}
	return dns_match_wildcard(local_name, filter_local_name) == 0;
}

/* Remove any elements which there is no filter for */
static void
master_service_settings_cache_fix_input(struct master_service_settings_cache *cache,
				        const struct master_service_settings_input *input,
					struct master_service_settings_input *new_input)
{
	bool found_lip, found_rip, found_local_name;

	found_lip = found_rip = found_local_name = FALSE;

	struct config_filter *filter = cache->filters;
	while(filter != NULL) {
		if (filter->local_bits > 0 &&
		    net_is_in_network(&input->local_ip, &filter->local_ip,
				      filter->local_bits))
			found_lip = TRUE;
		if (filter->remote_bits > 0 &&
		    net_is_in_network(&input->remote_ip, &filter->remote_ip,
				      filter->remote_bits))
			found_rip = TRUE;
		if (input->local_name != NULL && filter->local_name != NULL &&
		    match_local_name(input->local_name, filter->local_name))
			found_local_name = TRUE;
		filter = filter->next;
	};

	*new_input = *input;

	if (!found_lip)
		i_zero(&new_input->local_ip);
	if (!found_rip)
		i_zero(&new_input->remote_ip);
	if (!found_local_name)
		new_input->local_name = NULL;
}


void master_service_settings_cache_deinit(struct master_service_settings_cache **_cache)
{
	struct master_service_settings_cache *cache = *_cache;
	struct settings_entry *entry, *next;

	/* parsers need to be deinitialized, because they reference the pool */
	for (entry = cache->oldest_global; entry != NULL; entry = next) {
		next = entry->next;
		i_assert(entry->parser == cache->global_parser);
		pool_unref(&entry->pool);
	}
	for (entry = cache->oldest; entry != NULL; entry = next) {
		next = entry->next;
		i_assert(entry->parser != cache->global_parser);
		settings_parser_deinit(&entry->parser);
		pool_unref(&entry->pool);
	}
	hash_table_destroy(&cache->local_name_hash);
	hash_table_destroy(&cache->local_ip_hash);
	if (cache->global_parser != NULL)
		settings_parser_deinit(&cache->global_parser);
	pool_unref(&cache->pool);
}

static bool
cache_can_return_global(struct master_service_settings_cache *cache,
			const struct master_service_settings_input *input)
{
	if (cache->service_uses_local) {
		if (input->local_name != NULL || input->local_ip.family != 0)
			return FALSE;
	}
	if (cache->service_uses_remote) {
		if (input->remote_ip.family != 0)
			return FALSE;
	}
	return TRUE;
}

static bool
cache_find(struct master_service_settings_cache *cache,
	   const struct master_service_settings_input *input,
	   const struct setting_parser_context **parser_r)
{
	struct settings_entry *entry = NULL;

	if (!cache->done_initial_lookup)
		return FALSE;

	if (cache_can_return_global(cache, input)) {
		if (cache->global_parser != NULL) {
			*parser_r = cache->global_parser;
			return TRUE;
		}
		return FALSE;
	}

	if (cache->service_uses_remote)
		return FALSE;

	/* see if we have it already in cache. if local_name is specified,
	   don't even try to use local_ip (even though we have it), because
	   there may be different settings specifically for local_name */
	if (input->local_name != NULL) {
		if (hash_table_is_created(cache->local_name_hash)) {
			entry = hash_table_lookup(cache->local_name_hash,
						  input->local_name);
		}
	} else if (hash_table_is_created(cache->local_ip_hash) &&
		   input->local_ip.family != 0) {
		entry = hash_table_lookup(cache->local_ip_hash,
					  &input->local_ip);
	}

	if (entry != NULL) {
		if (entry->parser != cache->global_parser) {
			DLLIST2_REMOVE(&cache->oldest, &cache->newest, entry);
			DLLIST2_APPEND(&cache->oldest, &cache->newest, entry);
		}
		*parser_r = entry->parser;
		return TRUE;
	}
	return FALSE;
}

static void
setting_entry_detach(struct master_service_settings_cache *cache,
		     struct settings_entry *entry)
{
	DLLIST2_REMOVE(&cache->oldest, &cache->newest, entry);
	cache->cache_malloc_size -=
		pool_alloconly_get_total_alloc_size(entry->pool);

	if (entry->local_name != NULL)
		hash_table_remove(cache->local_name_hash, entry->local_name);
	else if (entry->local_ip.family != 0)
		hash_table_remove(cache->local_ip_hash, &entry->local_ip);
	settings_parser_deinit(&entry->parser);
}

static struct setting_parser_context *
cache_add(struct master_service_settings_cache *cache,
	  const struct master_service_settings_input *input,
	  const struct master_service_settings_output *output,
	  struct setting_parser_context *parser)
{
	struct settings_entry *entry;
	pool_t pool;
	size_t pool_size;
	char *entry_local_name;

	if (!output->used_local && !output->used_remote) {
		/* these are same as global settings */
		if (cache->global_parser == NULL) {
			cache->global_parser =
				settings_parser_dup(parser, cache->pool);
		}
	}
	if (cache->service_uses_remote) {
		/* for now we don't try to handle caching remote IPs */
		return parser;
	}

	if (input->local_name == NULL && input->local_ip.family == 0)
		return parser;

	if (!output->used_local) {
		/* use global settings, but add local_ip/host to hash tables
		   so we'll find them */
		pool = pool_alloconly_create("settings global entry", 256);
	} else if (cache->cache_malloc_size >= cache->max_cache_size) {
		/* free the oldest and reuse its pool */
		pool = cache->oldest->pool;
		setting_entry_detach(cache, cache->oldest);
		p_clear(pool); /* note: frees also entry */
	} else {
		pool_size = cache->approx_entry_pool_size != 0 ?
			cache->approx_entry_pool_size :
			CACHE_INITIAL_ENTRY_POOL_SIZE;
		pool = pool_alloconly_create("settings entry", pool_size);
	}
	entry = p_new(pool, struct settings_entry, 1);
	entry->pool = pool;
	entry_local_name = p_strdup(pool, input->local_name);
	entry->local_name = entry_local_name;
	entry->local_ip = input->local_ip;
	if (!output->used_local) {
		entry->parser = cache->global_parser;
		DLLIST2_APPEND(&cache->oldest_global, &cache->newest_global,
			       entry);
	} else {
		entry->parser = settings_parser_dup(parser, entry->pool);
		DLLIST2_APPEND(&cache->oldest, &cache->newest, entry);

		pool_size = pool_alloconly_get_total_used_size(pool);
		if (pool_size > cache->approx_entry_pool_size) {
			cache->approx_entry_pool_size = pool_size +
				CACHE_ADD_ENTRY_POOL_SIZE;
		}
	}
	cache->cache_malloc_size += pool_alloconly_get_total_alloc_size(pool);

	if (input->local_name != NULL) {
		if (!hash_table_is_created(cache->local_name_hash)) {
			hash_table_create(&cache->local_name_hash,
					  cache->pool, 0, str_hash, strcmp);
		}
		i_assert(hash_table_lookup(cache->local_name_hash,
					   entry_local_name) == NULL);
		hash_table_insert(cache->local_name_hash,
				  entry_local_name, entry);
	} else if (input->local_ip.family != 0) {
		if (!hash_table_is_created(cache->local_ip_hash)) {
			hash_table_create(&cache->local_ip_hash, cache->pool, 0,
					  net_ip_hash, net_ip_cmp);
		}
		i_assert(hash_table_lookup(cache->local_ip_hash,
					   &entry->local_ip) == NULL);
		hash_table_insert(cache->local_ip_hash,
				  &entry->local_ip, entry);
	}
	return entry->parser;
}

int master_service_settings_cache_read(struct master_service_settings_cache *cache,
				       const struct master_service_settings_input *input,
				       const struct dynamic_settings_parser *dyn_parsers,
				       const struct setting_parser_context **parser_r,
				       const char **error_r)
{
	struct master_service_settings_output output;
	struct master_service_settings_input new_input;
	const struct master_service_settings *set;

	i_assert(null_strcmp(input->module, cache->module) == 0);
	i_assert(null_strcmp(input->service, cache->service_name) == 0);

	if (cache_find(cache, input, parser_r))
		return 0;

	new_input = *input;
	if (cache->filters != NULL) {
		master_service_settings_cache_fix_input(cache, input, &new_input);
		if (cache_find(cache, &new_input, parser_r))
			return 0;
	}

	if (dyn_parsers != NULL) {
		settings_parser_dyn_update(cache->pool, &new_input.roots,
					   dyn_parsers);
	}
	if (master_service_settings_read(cache->service, &new_input,
					 &output, error_r) < 0)
		return -1;

	if (!cache->done_initial_lookup) {
		cache->done_initial_lookup = TRUE;
		cache->service_uses_local = output.service_uses_local;
		cache->service_uses_remote = output.service_uses_remote;

		set = master_service_settings_get(cache->service);
		cache->max_cache_size = set->config_cache_size;
	}

	if (output.used_local && !cache->service_uses_local) {
		*error_r = "BUG: config unexpectedly returned local settings";
		return -1;
	}
	if (output.used_remote && !cache->service_uses_remote) {
		*error_r = "BUG: config unexpectedly returned remote settings";
		return -1;
	}

	*parser_r = cache_add(cache, &new_input, &output,
			      cache->service->set_parser);
	return 0;
}