summaryrefslogtreecommitdiffstats
path: root/usr.sbin/nscd/parser.c
blob: a36821a75e7a8d6dd96814a6c25bf974bcabfce3 (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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*-
 * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/time.h>

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "debug.h"
#include "log.h"
#include "parser.h"

static void enable_cache(struct configuration *,const char *, int);
static struct configuration_entry *find_create_entry(struct configuration *,
	const char *);
static int get_number(const char *, int, int);
static enum cache_policy_t get_policy(const char *);
static int get_yesno(const char *);
static int check_cachename(const char *);
static void check_files(struct configuration *, const char *, int);
static void set_keep_hot_count(struct configuration *, const char *, int);
static void set_negative_policy(struct configuration *, const char *,
	enum cache_policy_t);
static void set_negative_time_to_live(struct configuration *,
	const char *, int);
static void set_positive_policy(struct configuration *, const char *,
	enum cache_policy_t);
static void set_perform_actual_lookups(struct configuration *, const char *,
	int);
static void set_positive_time_to_live(struct configuration *,
	const char *, int);
static void set_suggested_size(struct configuration *, const char *,
	int size);
static void set_threads_num(struct configuration *, int);
static int strbreak(char *, char **, int);

static int
strbreak(char *str, char **fields, int fields_size)
{
	char	*c = str;
	int	i, num;

	TRACE_IN(strbreak);
	num = 0;
	for (i = 0;
	     ((*fields =
	     	strsep(i < fields_size ? &c : NULL, "\n\t ")) != NULL);
	     ++i)
		if ((*(*fields)) != '\0') {
			++fields;
			++num;
		}

	TRACE_OUT(strbreak);
	return (num);
}

/*
 * Tries to find the configuration entry with the specified name. If search
 * fails, the new entry with the default parameters will be created.
 */
static struct configuration_entry *
find_create_entry(struct configuration *config,
	const char *entry_name)
{
	struct configuration_entry *entry = NULL;
	int res;

	TRACE_IN(find_create_entry);
	entry = configuration_find_entry(config, entry_name);
	if (entry == NULL) {
		entry = create_def_configuration_entry(entry_name);
		assert( entry != NULL);
		res = add_configuration_entry(config, entry);
		assert(res == 0);
	}

	TRACE_OUT(find_create_entry);
	return (entry);
}

/*
 * The vast majority of the functions below corresponds to the particular
 * keywords in the configuration file.
 */
static void
enable_cache(struct configuration *config, const char *entry_name, int flag)
{
	struct configuration_entry	*entry;

	TRACE_IN(enable_cache);
	entry = find_create_entry(config, entry_name);
	entry->enabled = flag;
	TRACE_OUT(enable_cache);
}

static void
set_positive_time_to_live(struct configuration *config,
	const char *entry_name, int ttl)
{
	struct configuration_entry *entry;
	struct timeval lifetime;

	TRACE_IN(set_positive_time_to_live);
	assert(ttl >= 0);
	assert(entry_name != NULL);
	memset(&lifetime, 0, sizeof(struct timeval));
	lifetime.tv_sec = ttl;

	entry = find_create_entry(config, entry_name);
	memcpy(&entry->positive_cache_params.max_lifetime,
		&lifetime, sizeof(struct timeval));
	memcpy(&entry->mp_cache_params.max_lifetime,
		&lifetime, sizeof(struct timeval));

	TRACE_OUT(set_positive_time_to_live);
}

static void
set_negative_time_to_live(struct configuration *config,
	const char *entry_name, int nttl)
{
	struct configuration_entry *entry;
	struct timeval lifetime;

	TRACE_IN(set_negative_time_to_live);
	assert(nttl > 0);
	assert(entry_name != NULL);
	memset(&lifetime, 0, sizeof(struct timeval));
	lifetime.tv_sec = nttl;

	entry = find_create_entry(config, entry_name);
	assert(entry != NULL);
	memcpy(&entry->negative_cache_params.max_lifetime,
		&lifetime, sizeof(struct timeval));

	TRACE_OUT(set_negative_time_to_live);
}

/*
 * Hot count is actually the elements size limit.
 */
static void
set_keep_hot_count(struct configuration *config,
	const char *entry_name, int count)
{
	struct configuration_entry *entry;

	TRACE_IN(set_keep_hot_count);
	assert(count >= 0);
	assert(entry_name != NULL);

	entry = find_create_entry(config, entry_name);
	assert(entry != NULL);
	entry->positive_cache_params.max_elemsize = count;

	entry = find_create_entry(config, entry_name);
	assert(entry != NULL);
	entry->negative_cache_params.max_elemsize = count;

	TRACE_OUT(set_keep_hot_count);
}

static void
set_positive_policy(struct configuration *config,
	const char *entry_name, enum cache_policy_t policy)
{
	struct configuration_entry *entry;

	TRACE_IN(set_positive_policy);
	assert(entry_name != NULL);

	entry = find_create_entry(config, entry_name);
	assert(entry != NULL);
	entry->positive_cache_params.policy = policy;

	TRACE_OUT(set_positive_policy);
}

static void
set_negative_policy(struct configuration *config,
	const char *entry_name, enum cache_policy_t policy)
{
	struct configuration_entry *entry;

	TRACE_IN(set_negative_policy);
	assert(entry_name != NULL);

	entry = find_create_entry(config, entry_name);
	assert(entry != NULL);
	entry->negative_cache_params.policy = policy;

	TRACE_OUT(set_negative_policy);
}

static void
set_perform_actual_lookups(struct configuration *config,
	const char *entry_name, int flag)
{
	struct configuration_entry *entry;

	TRACE_IN(set_perform_actual_lookups);
	assert(entry_name != NULL);

	entry = find_create_entry(config, entry_name);
	assert(entry != NULL);
	entry->perform_actual_lookups = flag;

	TRACE_OUT(set_perform_actual_lookups);
}

static void
set_suggested_size(struct configuration *config,
	const char *entry_name, int size)
{
	struct configuration_entry	*entry;

	TRACE_IN(set_suggested_size);
	assert(config != NULL);
	assert(entry_name != NULL);
	assert(size > 0);

	entry = find_create_entry(config, entry_name);
	assert(entry != NULL);
	entry->positive_cache_params.cache_entries_size = size;
	entry->negative_cache_params.cache_entries_size = size;

	TRACE_OUT(set_suggested_size);
}

static void
check_files(struct configuration *config, const char *entry_name, int flag)
{

	TRACE_IN(check_files);
	assert(entry_name != NULL);
	TRACE_OUT(check_files);
}

static int
get_yesno(const char *str)
{

	if (strcmp(str, "yes") == 0)
		return (1);
	else if (strcmp(str, "no") == 0)
		return (0);
	else
		return (-1);
}

static int
get_number(const char *str, int low, int max)
{

	char *end = NULL;
	int res = 0;

	if (str[0] == '\0')
		return (-1);

	res = strtol(str, &end, 10);
	if (*end != '\0')
		return (-1);
	else
		if (((res >= low) || (low == -1)) &&
			((res <= max) || (max == -1)))
			return (res);
		else
			return (-2);
}

static enum cache_policy_t
get_policy(const char *str)
{

	if (strcmp(str, "fifo") == 0)
		return (CPT_FIFO);
	else if (strcmp(str, "lru") == 0)
		return (CPT_LRU);
	else if (strcmp(str, "lfu") == 0)
		return (CPT_LFU);

	return (-1);
}

static int
check_cachename(const char *str)
{

	assert(str != NULL);
	return ((strlen(str) > 0) ? 0 : -1);
}

static void
set_threads_num(struct configuration *config, int value)
{

	assert(config != NULL);
	config->threads_num = value;
}

/*
 * The main configuration routine. Its implementation is hugely inspired by the
 * the same routine implementation in Solaris NSCD.
 */
int
parse_config_file(struct configuration *config,
	const char *fname, char const **error_str, int *error_line)
{
	FILE	*fin;
	char	buffer[255];
	char	*fields[128];
	int	field_count, line_num, value;
	int	res;

	TRACE_IN(parse_config_file);
	assert(config != NULL);
	assert(fname != NULL);

	fin = fopen(fname, "r");
	if (fin == NULL) {
		TRACE_OUT(parse_config_file);
		return (-1);
	}

	res = 0;
	line_num = 0;
	memset(buffer, 0, sizeof(buffer));
	while ((res == 0) && (fgets(buffer, sizeof(buffer) - 1, fin) != NULL)) {
		field_count = strbreak(buffer, fields, sizeof(fields));
		++line_num;

		if (field_count == 0)
			continue;

		switch (fields[0][0]) {
		case '#':
		case '\0':
			continue;
		case 'e':
			if ((field_count == 3) &&
			(strcmp(fields[0], "enable-cache") == 0) &&
			(check_cachename(fields[1]) == 0) &&
			((value = get_yesno(fields[2])) != -1)) {
				enable_cache(config, fields[1], value);
				continue;
			}
			break;
		case 'd':
			if ((field_count == 2) &&
			(strcmp(fields[0], "debug-level") == 0) &&
			((value = get_number(fields[1], 0, 10)) != -1)) {
				continue;
			}
			break;
		case 'p':
			if ((field_count == 3) &&
			(strcmp(fields[0], "positive-time-to-live") == 0) &&
			(check_cachename(fields[1]) == 0) &&
			((value = get_number(fields[2], 0, -1)) != -1)) {
				set_positive_time_to_live(config,
					fields[1], value);
				continue;
			} else if ((field_count == 3) &&
			(strcmp(fields[0], "positive-policy") == 0) &&
			(check_cachename(fields[1]) == 0) &&
			((value = get_policy(fields[2])) != -1)) {
				set_positive_policy(config, fields[1], value);
				continue;
			} else if ((field_count == 3) &&
			(strcmp(fields[0], "perform-actual-lookups") == 0) &&
			(check_cachename(fields[1]) == 0) &&
			((value = get_yesno(fields[2])) != -1)) {
				set_perform_actual_lookups(config, fields[1],
					value);
				continue;
			}
			break;
		case 'n':
			if ((field_count == 3) &&
			(strcmp(fields[0], "negative-time-to-live") == 0) &&
			(check_cachename(fields[1]) == 0) &&
			((value = get_number(fields[2], 0, -1)) != -1)) {
				set_negative_time_to_live(config,
					fields[1], value);
				continue;
			} else if ((field_count == 3) &&
			(strcmp(fields[0], "negative-policy") == 0) &&
			(check_cachename(fields[1]) == 0) &&
			((value = get_policy(fields[2])) != -1)) {
				set_negative_policy(config,
					fields[1], value);
				continue;
			}
			break;
		case 's':
			if ((field_count == 3) &&
			(strcmp(fields[0], "suggested-size") == 0) &&
			(check_cachename(fields[1]) == 0) &&
			((value = get_number(fields[2], 1, -1)) != -1)) {
				set_suggested_size(config, fields[1], value);
				continue;
			}
			break;
		case 't':
			if ((field_count == 2) &&
			(strcmp(fields[0], "threads") == 0) &&
			((value = get_number(fields[1], 1, -1)) != -1)) {
				set_threads_num(config, value);
				continue;
			}
			break;
		case 'k':
			if ((field_count == 3) &&
			(strcmp(fields[0], "keep-hot-count") == 0) &&
			(check_cachename(fields[1]) == 0) &&
			((value = get_number(fields[2], 0, -1)) != -1)) {
				set_keep_hot_count(config,
					fields[1], value);
				continue;
			}
			break;
		case 'c':
			if ((field_count == 3) &&
			(strcmp(fields[0], "check-files") == 0) &&
			(check_cachename(fields[1]) == 0) &&
			((value = get_yesno(fields[2])) != -1)) {
				check_files(config,
					fields[1], value);
				continue;
			}
			break;
		default:
			break;
		}

		LOG_ERR_2("config file parser", "error in file "
			"%s on line %d", fname, line_num);
		*error_str = "syntax error";
		*error_line = line_num;
		res = -1;
	}
	fclose(fin);

	TRACE_OUT(parse_config_file);
	return (res);
}
OpenPOWER on IntegriCloud