config.c revision 194096
1/*-
2 * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/usr.sbin/nscd/config.c 194096 2009-06-13 00:56:17Z des $");
30
31#include <sys/stat.h>
32#include <sys/time.h>
33
34#include <assert.h>
35#include <math.h>
36#include <nsswitch.h>
37#include <pthread.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41
42#include "config.h"
43#include "debug.h"
44#include "log.h"
45
46/*
47 * Default entries, which always exist in the configuration
48 */
49const char *c_default_entries[6] = {
50	NSDB_PASSWD,
51	NSDB_GROUP,
52	NSDB_HOSTS,
53	NSDB_SERVICES,
54	NSDB_PROTOCOLS,
55	NSDB_RPC
56	};
57
58static int configuration_entry_cmp(const void *, const void *);
59static int configuration_entry_sort_cmp(const void *, const void *);
60static int configuration_entry_cache_mp_sort_cmp(const void *, const void *);
61static int configuration_entry_cache_mp_cmp(const void *, const void *);
62static int configuration_entry_cache_mp_part_cmp(const void *, const void *);
63static struct configuration_entry *create_configuration_entry(const char *,
64	struct timeval const *, struct timeval const *,
65	struct common_cache_entry_params const *,
66	struct common_cache_entry_params const *,
67	struct mp_cache_entry_params const *);
68
69static int
70configuration_entry_sort_cmp(const void *e1, const void *e2)
71{
72	return (strcmp((*((struct configuration_entry **)e1))->name,
73		(*((struct configuration_entry **)e2))->name
74		));
75}
76
77static int
78configuration_entry_cmp(const void *e1, const void *e2)
79{
80	return (strcmp((const char *)e1,
81		(*((struct configuration_entry **)e2))->name
82		));
83}
84
85static int
86configuration_entry_cache_mp_sort_cmp(const void *e1, const void *e2)
87{
88	return (strcmp((*((cache_entry *)e1))->params->entry_name,
89		(*((cache_entry *)e2))->params->entry_name
90		));
91}
92
93static int
94configuration_entry_cache_mp_cmp(const void *e1, const void *e2)
95{
96	return (strcmp((const char *)e1,
97		(*((cache_entry *)e2))->params->entry_name
98		));
99}
100
101static int
102configuration_entry_cache_mp_part_cmp(const void *e1, const void *e2)
103{
104	return (strncmp((const char *)e1,
105		(*((cache_entry *)e2))->params->entry_name,
106		strlen((const char *)e1)
107		));
108}
109
110static struct configuration_entry *
111create_configuration_entry(const char *name,
112	struct timeval const *common_timeout,
113	struct timeval const *mp_timeout,
114	struct common_cache_entry_params const *positive_params,
115	struct common_cache_entry_params const *negative_params,
116	struct mp_cache_entry_params const *mp_params)
117{
118	struct configuration_entry *retval;
119	size_t	size;
120	int res;
121
122	TRACE_IN(create_configuration_entry);
123	assert(name != NULL);
124	assert(positive_params != NULL);
125	assert(negative_params != NULL);
126	assert(mp_params != NULL);
127
128	retval = (struct configuration_entry *)calloc(1,
129		sizeof(struct configuration_entry));
130	assert(retval != NULL);
131
132	res = pthread_mutex_init(&retval->positive_cache_lock, NULL);
133	if (res != 0) {
134		free(retval);
135		LOG_ERR_2("create_configuration_entry",
136			"can't create positive cache lock");
137		TRACE_OUT(create_configuration_entry);
138		return (NULL);
139	}
140
141	res = pthread_mutex_init(&retval->negative_cache_lock, NULL);
142	if (res != 0) {
143		pthread_mutex_destroy(&retval->positive_cache_lock);
144		free(retval);
145		LOG_ERR_2("create_configuration_entry",
146			"can't create negative cache lock");
147		TRACE_OUT(create_configuration_entry);
148		return (NULL);
149	}
150
151	res = pthread_mutex_init(&retval->mp_cache_lock, NULL);
152	if (res != 0) {
153		pthread_mutex_destroy(&retval->positive_cache_lock);
154		pthread_mutex_destroy(&retval->negative_cache_lock);
155		free(retval);
156		LOG_ERR_2("create_configuration_entry",
157			"can't create negative cache lock");
158		TRACE_OUT(create_configuration_entry);
159		return (NULL);
160	}
161
162	memcpy(&retval->positive_cache_params, positive_params,
163		sizeof(struct common_cache_entry_params));
164	memcpy(&retval->negative_cache_params, negative_params,
165		sizeof(struct common_cache_entry_params));
166	memcpy(&retval->mp_cache_params, mp_params,
167		sizeof(struct mp_cache_entry_params));
168
169	size = strlen(name);
170	retval->name = (char *)calloc(1, size + 1);
171	assert(retval->name != NULL);
172	memcpy(retval->name, name, size);
173
174	memcpy(&retval->common_query_timeout, common_timeout,
175		sizeof(struct timeval));
176	memcpy(&retval->mp_query_timeout, mp_timeout,
177		sizeof(struct timeval));
178
179	asprintf(&retval->positive_cache_params.entry_name, "%s+", name);
180	assert(retval->positive_cache_params.entry_name != NULL);
181
182	asprintf(&retval->negative_cache_params.entry_name, "%s-", name);
183	assert(retval->negative_cache_params.entry_name != NULL);
184
185	asprintf(&retval->mp_cache_params.entry_name, "%s*", name);
186	assert(retval->mp_cache_params.entry_name != NULL);
187
188	TRACE_OUT(create_configuration_entry);
189	return (retval);
190}
191
192/*
193 * Creates configuration entry and fills it with default values
194 */
195struct configuration_entry *
196create_def_configuration_entry(const char *name)
197{
198	struct common_cache_entry_params positive_params, negative_params;
199	struct mp_cache_entry_params mp_params;
200	struct timeval default_common_timeout, default_mp_timeout;
201
202	struct configuration_entry *res = NULL;
203
204	TRACE_IN(create_def_configuration_entry);
205	memset(&positive_params, 0,
206		sizeof(struct common_cache_entry_params));
207	positive_params.entry_type = CET_COMMON;
208	positive_params.cache_entries_size = DEFAULT_CACHE_HT_SIZE;
209	positive_params.max_elemsize = DEFAULT_POSITIVE_ELEMENTS_SIZE;
210	positive_params.satisf_elemsize = DEFAULT_POSITIVE_ELEMENTS_SIZE / 2;
211	positive_params.max_lifetime.tv_sec = DEFAULT_POSITIVE_LIFETIME;
212	positive_params.policy = CPT_LRU;
213
214	memcpy(&negative_params, &positive_params,
215		sizeof(struct common_cache_entry_params));
216	negative_params.max_elemsize = DEFAULT_NEGATIVE_ELEMENTS_SIZE;
217	negative_params.satisf_elemsize = DEFAULT_NEGATIVE_ELEMENTS_SIZE / 2;
218	negative_params.max_lifetime.tv_sec = DEFAULT_NEGATIVE_LIFETIME;
219	negative_params.policy = CPT_FIFO;
220
221	memset(&default_common_timeout, 0, sizeof(struct timeval));
222	default_common_timeout.tv_sec = DEFAULT_COMMON_ENTRY_TIMEOUT;
223
224	memset(&default_mp_timeout, 0, sizeof(struct timeval));
225	default_mp_timeout.tv_sec = DEFAULT_MP_ENTRY_TIMEOUT;
226
227	memset(&mp_params, 0,
228		sizeof(struct mp_cache_entry_params));
229	mp_params.entry_type = CET_MULTIPART;
230	mp_params.max_elemsize = DEFAULT_MULTIPART_ELEMENTS_SIZE;
231	mp_params.max_sessions = DEFAULT_MULITPART_SESSIONS_SIZE;
232	mp_params.max_lifetime.tv_sec = DEFAULT_MULITPART_LIFETIME;
233
234	res = create_configuration_entry(name, &default_common_timeout,
235		&default_mp_timeout, &positive_params, &negative_params,
236		&mp_params);
237
238	TRACE_OUT(create_def_configuration_entry);
239	return (res);
240}
241
242void
243destroy_configuration_entry(struct configuration_entry *entry)
244{
245	TRACE_IN(destroy_configuration_entry);
246	assert(entry != NULL);
247	pthread_mutex_destroy(&entry->positive_cache_lock);
248	pthread_mutex_destroy(&entry->negative_cache_lock);
249	pthread_mutex_destroy(&entry->mp_cache_lock);
250	free(entry->name);
251	free(entry->positive_cache_params.entry_name);
252	free(entry->negative_cache_params.entry_name);
253	free(entry->mp_cache_params.entry_name);
254	free(entry->mp_cache_entries);
255	free(entry);
256	TRACE_OUT(destroy_configuration_entry);
257}
258
259int
260add_configuration_entry(struct configuration *config,
261	struct configuration_entry *entry)
262{
263	TRACE_IN(add_configuration_entry);
264	assert(entry != NULL);
265	assert(entry->name != NULL);
266	if (configuration_find_entry(config, entry->name) != NULL) {
267		TRACE_OUT(add_configuration_entry);
268		return (-1);
269	}
270
271	if (config->entries_size == config->entries_capacity) {
272		struct configuration_entry **new_entries;
273
274		config->entries_capacity *= 2;
275		new_entries = (struct configuration_entry **)calloc(1,
276			sizeof(struct configuration_entry *) *
277			config->entries_capacity);
278		assert(new_entries != NULL);
279		memcpy(new_entries, config->entries,
280			sizeof(struct configuration_entry *) *
281		        config->entries_size);
282
283		free(config->entries);
284		config->entries = new_entries;
285	}
286
287	config->entries[config->entries_size++] = entry;
288	qsort(config->entries, config->entries_size,
289		sizeof(struct configuration_entry *),
290		configuration_entry_sort_cmp);
291
292	TRACE_OUT(add_configuration_entry);
293	return (0);
294}
295
296size_t
297configuration_get_entries_size(struct configuration *config)
298{
299	TRACE_IN(configuration_get_entries_size);
300	assert(config != NULL);
301	TRACE_OUT(configuration_get_entries_size);
302	return (config->entries_size);
303}
304
305struct configuration_entry *
306configuration_get_entry(struct configuration *config, size_t index)
307{
308	TRACE_IN(configuration_get_entry);
309	assert(config != NULL);
310	assert(index < config->entries_size);
311	TRACE_OUT(configuration_get_entry);
312	return (config->entries[index]);
313}
314
315struct configuration_entry *
316configuration_find_entry(struct configuration *config,
317	const char *name)
318{
319	struct configuration_entry	**retval;
320
321	TRACE_IN(configuration_find_entry);
322
323	retval = bsearch(name, config->entries, config->entries_size,
324		sizeof(struct configuration_entry *), configuration_entry_cmp);
325	TRACE_OUT(configuration_find_entry);
326
327	return ((retval != NULL) ? *retval : NULL);
328}
329
330/*
331 * All multipart cache entries are stored in the configuration_entry in the
332 * sorted array (sorted by names). The 3 functions below manage this array.
333 */
334
335int
336configuration_entry_add_mp_cache_entry(struct configuration_entry *config_entry,
337	cache_entry c_entry)
338{
339	cache_entry *new_mp_entries, *old_mp_entries;
340
341	TRACE_IN(configuration_entry_add_mp_cache_entry);
342	++config_entry->mp_cache_entries_size;
343	new_mp_entries = (cache_entry *)malloc(sizeof(cache_entry) *
344		config_entry->mp_cache_entries_size);
345	assert(new_mp_entries != NULL);
346	new_mp_entries[0] = c_entry;
347
348	if (config_entry->mp_cache_entries_size - 1 > 0) {
349		memcpy(new_mp_entries + 1,
350		    config_entry->mp_cache_entries,
351		    (config_entry->mp_cache_entries_size - 1) *
352		    sizeof(cache_entry));
353	}
354
355	old_mp_entries = config_entry->mp_cache_entries;
356	config_entry->mp_cache_entries = new_mp_entries;
357	free(old_mp_entries);
358
359	qsort(config_entry->mp_cache_entries,
360		config_entry->mp_cache_entries_size,
361		sizeof(cache_entry),
362		configuration_entry_cache_mp_sort_cmp);
363
364	TRACE_OUT(configuration_entry_add_mp_cache_entry);
365	return (0);
366}
367
368cache_entry
369configuration_entry_find_mp_cache_entry(
370	struct configuration_entry *config_entry, const char *mp_name)
371{
372	cache_entry *result;
373
374	TRACE_IN(configuration_entry_find_mp_cache_entry);
375	result = bsearch(mp_name, config_entry->mp_cache_entries,
376		config_entry->mp_cache_entries_size,
377		sizeof(cache_entry), configuration_entry_cache_mp_cmp);
378
379	if (result == NULL) {
380		TRACE_OUT(configuration_entry_find_mp_cache_entry);
381		return (NULL);
382	} else {
383		TRACE_OUT(configuration_entry_find_mp_cache_entry);
384		return (*result);
385	}
386}
387
388/*
389 * Searches for all multipart entries with names starting with mp_name.
390 * Needed for cache flushing.
391 */
392int
393configuration_entry_find_mp_cache_entries(
394	struct configuration_entry *config_entry, const char *mp_name,
395	cache_entry **start, cache_entry **finish)
396{
397	cache_entry *result;
398
399	TRACE_IN(configuration_entry_find_mp_cache_entries);
400	result = bsearch(mp_name, config_entry->mp_cache_entries,
401		config_entry->mp_cache_entries_size,
402		sizeof(cache_entry), configuration_entry_cache_mp_part_cmp);
403
404	if (result == NULL) {
405		TRACE_OUT(configuration_entry_find_mp_cache_entries);
406		return (-1);
407	}
408
409	*start = result;
410	*finish = result + 1;
411
412	while (*start != config_entry->mp_cache_entries) {
413	    if (configuration_entry_cache_mp_part_cmp(mp_name, *start - 1) == 0)
414		*start = *start - 1;
415	    else
416		break;
417	}
418
419	while (*finish != config_entry->mp_cache_entries +
420		config_entry->mp_cache_entries_size) {
421
422	    if (configuration_entry_cache_mp_part_cmp(
423		mp_name, *finish) == 0)
424	    	*finish = *finish + 1;
425	    else
426		break;
427	}
428
429	TRACE_OUT(configuration_entry_find_mp_cache_entries);
430	return (0);
431}
432
433/*
434 * Configuration entry uses rwlock to handle access to its fields.
435 */
436void
437configuration_lock_rdlock(struct configuration *config)
438{
439    TRACE_IN(configuration_lock_rdlock);
440    pthread_rwlock_rdlock(&config->rwlock);
441    TRACE_OUT(configuration_lock_rdlock);
442}
443
444void
445configuration_lock_wrlock(struct configuration *config)
446{
447    TRACE_IN(configuration_lock_wrlock);
448    pthread_rwlock_wrlock(&config->rwlock);
449    TRACE_OUT(configuration_lock_wrlock);
450}
451
452void
453configuration_unlock(struct configuration *config)
454{
455    TRACE_IN(configuration_unlock);
456    pthread_rwlock_unlock(&config->rwlock);
457    TRACE_OUT(configuration_unlock);
458}
459
460/*
461 * Configuration entry uses 3 mutexes to handle cache operations. They are
462 * acquired by configuration_lock_entry and configuration_unlock_entry
463 * functions.
464 */
465void
466configuration_lock_entry(struct configuration_entry *entry,
467	enum config_entry_lock_type lock_type)
468{
469	TRACE_IN(configuration_lock_entry);
470	assert(entry != NULL);
471
472	switch (lock_type) {
473	case CELT_POSITIVE:
474		pthread_mutex_lock(&entry->positive_cache_lock);
475		break;
476	case CELT_NEGATIVE:
477		pthread_mutex_lock(&entry->negative_cache_lock);
478		break;
479	case CELT_MULTIPART:
480		pthread_mutex_lock(&entry->mp_cache_lock);
481		break;
482	default:
483		/* should be unreachable */
484		break;
485	}
486	TRACE_OUT(configuration_lock_entry);
487}
488
489void
490configuration_unlock_entry(struct configuration_entry *entry,
491	enum config_entry_lock_type lock_type)
492{
493	TRACE_IN(configuration_unlock_entry);
494	assert(entry != NULL);
495
496	switch (lock_type) {
497	case CELT_POSITIVE:
498		pthread_mutex_unlock(&entry->positive_cache_lock);
499		break;
500	case CELT_NEGATIVE:
501		pthread_mutex_unlock(&entry->negative_cache_lock);
502		break;
503	case CELT_MULTIPART:
504		pthread_mutex_unlock(&entry->mp_cache_lock);
505		break;
506	default:
507		/* should be unreachable */
508		break;
509	}
510	TRACE_OUT(configuration_unlock_entry);
511}
512
513struct configuration *
514init_configuration(void)
515{
516	struct configuration	*retval;
517
518	TRACE_IN(init_configuration);
519	retval = (struct configuration *)calloc(1, sizeof(struct configuration));
520	assert(retval != NULL);
521
522	retval->entries_capacity = INITIAL_ENTRIES_CAPACITY;
523	retval->entries = (struct configuration_entry **)calloc(1,
524		sizeof(struct configuration_entry *) *
525		retval->entries_capacity);
526	assert(retval->entries != NULL);
527
528	pthread_rwlock_init(&retval->rwlock, NULL);
529
530	TRACE_OUT(init_configuration);
531	return (retval);
532}
533
534void
535fill_configuration_defaults(struct configuration *config)
536{
537	size_t	len, i;
538
539	TRACE_IN(fill_configuration_defaults);
540	assert(config != NULL);
541
542	if (config->socket_path != NULL)
543		free(config->socket_path);
544
545	len = strlen(DEFAULT_SOCKET_PATH);
546	config->socket_path = (char *)calloc(1, len + 1);
547	assert(config->socket_path != NULL);
548	memcpy(config->socket_path, DEFAULT_SOCKET_PATH, len);
549
550	len = strlen(DEFAULT_PIDFILE_PATH);
551	config->pidfile_path = (char *)calloc(1, len + 1);
552	assert(config->pidfile_path != NULL);
553	memcpy(config->pidfile_path, DEFAULT_PIDFILE_PATH, len);
554
555	config->socket_mode =  S_IFSOCK | S_IRUSR | S_IWUSR |
556		S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
557	config->force_unlink = 1;
558
559	config->query_timeout = DEFAULT_QUERY_TIMEOUT;
560	config->threads_num = DEFAULT_THREADS_NUM;
561
562	for (i = 0; i < config->entries_size; ++i)
563		destroy_configuration_entry(config->entries[i]);
564	config->entries_size = 0;
565
566	TRACE_OUT(fill_configuration_defaults);
567}
568
569void
570destroy_configuration(struct configuration *config)
571{
572	unsigned int i;
573
574	TRACE_IN(destroy_configuration);
575	assert(config != NULL);
576	free(config->pidfile_path);
577	free(config->socket_path);
578
579	for (i = 0; i < config->entries_size; ++i)
580		destroy_configuration_entry(config->entries[i]);
581	free(config->entries);
582
583	pthread_rwlock_destroy(&config->rwlock);
584	free(config);
585	TRACE_OUT(destroy_configuration);
586}
587