summaryrefslogtreecommitdiff
path: root/common/config-parser.c
blob: fbb9c704c3d16621e7e6e0feda098059a774d4f9 (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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
 * Copyright (c) 2005, Stefan Walter
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above
 *       copyright notice, this list of conditions and the
 *       following disclaimer.
 *     * 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.
 *     * The names of contributors to this software may not be
 *       used to endorse or promote products derived from this
 *       software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
 * COPYRIGHT OWNER 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.
 *
 *
 * CONTRIBUTORS
 *  Stef Walter <stef@memberwebs.com>
 */

#include "usuals.h"

#include "config-parser.h"

#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <stdarg.h>
#include <dirent.h>
#include <sys/stat.h>

#include <bsnmp/asn1.h>
#include <bsnmp/snmp.h>

static void
errmsg(const char* filename, void* data, const char* msg, ...)
{
    #define MAX_MSGLEN  1024
    char buf[MAX_MSGLEN];
    va_list ap;

    va_start(ap, msg);
    vsnprintf(buf, MAX_MSGLEN, msg, ap);
    buf[MAX_MSGLEN - 1] = 0;
    cfg_error(filename, buf, data);
    va_end(ap);
}

/* -----------------------------------------------------------------------------
 * CONFIG PARSER
 */

static char*
read_config_file(const char** configfile, void* data)
{
    char* config = NULL;
    char* newfilename;
    FILE* f = NULL;
    long len;
    int flen;

    ASSERT(configfile);

    f = fopen(*configfile, "r");
    if(f == NULL)
    {
        errmsg(*configfile, data, "couldn't open config file: %s", *configfile);
        return NULL;
    }

    /* Figure out size */
    if(fseek(f, 0, SEEK_END) == -1 || (len = ftell(f)) == -1 || fseek(f, 0, SEEK_SET) == -1)
    {
        errmsg(*configfile, data, "couldn't seek config file: %s", *configfile);
        return NULL;
    }

    flen = strlen(*configfile);
    if((config = (char*)malloc(len + 4 + flen)) == NULL)
    {
        errmsg(*configfile, data, "out of memory");
        return NULL;
    }

    /* And read in one block */
    if(fread(config, 1, len, f) != len)
    {
        errmsg(*configfile, data, "couldn't read config file: %s", *configfile);
        return NULL;
    }

    fclose(f);

    /* Null terminate the data */
    config[len] = '\n';
    config[len + 1] = 0;

    /* Remove nasty dos line endings */
    strcln(config, '\r');

    /* Persistent allocation for filename */
    newfilename = config + len + 2;
    strcpy(newfilename, *configfile);
    *configfile = newfilename;

    return config;
}

int
cfg_parse_file(const char* filename, void* data, char** memory)
{
    char* name = NULL;
    char* value = NULL;
    char* config;
    char* next;
    char* header;
    int ret = -1;
    char* p;
    char* t;

    ASSERT(filename);

    config = read_config_file(&filename, data);
    if(!config)
        goto finally;

    next = config;

    /* Go through lines and process them */
    while((t = strchr(next, '\n')) != NULL)
    {
        *t = 0;
        p = next; /* Do this before cleaning below */
        next = t + 1;

        t = strbtrim(p);

        /* Continuation line (had spaces at start) */
        if(p < t && *t)
        {
            if(!value)
            {
                errmsg(filename, data, "%s: invalid continuation in config: %s",
                       filename, p);
                goto finally;
            }

            /* Calculate the end of the current value */
            t = value + strlen(value);
            ASSERT(t < p);

            /* Continuations are separated by spaces */
            *t = ' ';
            t++;

            continue;
        }

        /* No continuation hand off value if necessary */
        if(name && value)
        {
            if(cfg_value(filename, header, name, strtrim(value), data) == -1)
                goto finally;
        }

        name = NULL;
        value = NULL;

        /* Empty lines / comments at start / comments without continuation */
        if(!*t || *p == '#')
            continue;

        /* A header */
        if(*p == '[')
        {
            t = p + strcspn(p, "]");
            if(!*t || t == p + 1)
            {
                errmsg(filename, data, "%s: invalid config header: %s",
                       filename, p);
                goto finally;
            }

            *t = 0;
            header = strtrim(p + 1);
            continue;
        }

        /* Look for the break between name = value on the same line */
        t = p + strcspn(p, ":=");
        if(!*t)
        {
            errmsg(filename, data, "%s: invalid config line: %s",
                   filename, p);
            goto finally;
        }

        /* Null terminate and split value part */
        *t = 0;
        t++;

        name = strtrim(p);
        value = strtrim(t);
    }

    if(name && value)
    {
        if(cfg_value(filename, header, name, value, data) == -1)
            goto finally;
    }

    ret = 0;


finally:

    if(!memory || ret != 0)
        free(config);
    else if(memory)
        *memory = config;

    return ret;
}

static int
parse_dir_internal(const char* subdir, void* data)
{
    char path[MAXPATHLEN];
    struct dirent* dire;
    int is_dir, is_reg, is_lnk;
    struct stat st;
    char* memory;
    DIR* dir;
    int r;

    /* Open specified or current directory */
    dir = opendir(subdir ? subdir : ".");
    if(!dir)
    {
        errmsg(NULL, data, "couldn't list config directory: %s",
               subdir ? subdir : ".");
        return -1;
    }

    while((dire = readdir(dir)) != NULL)
    {
        /* Build a file path to this entry */
        if(subdir)
        {
            strlcpy(path, subdir, MAXPATHLEN);
            strlcat(path, "/", MAXPATHLEN);
            strlcat(path, dire->d_name, MAXPATHLEN);
        }
        else
            strlcpy(path, dire->d_name, MAXPATHLEN);

        is_dir = is_reg = is_lnk = 0;
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
        if(dire->d_type != DT_UNKNOWN)
        {
            is_dir = (dire->d_type == DT_DIR);
            is_reg = (dire->d_type == DT_REG);
            is_lnk = (dire->d_type == DT_LNK);
        }
        else
#endif
        {
            if(stat(path, &st) < 0)
            {
                errmsg(NULL, data, "couldn't stat path: %s", path);
                return -1;
            }

            if(S_ISREG(st.st_mode))
                is_reg = 1;
            else if(S_ISDIR(st.st_mode))
                is_dir = 1;
            else if(S_ISLNK(st.st_mode))
                is_lnk = 1;
        }

        /* Descend into each sub directory */
        if(is_dir)
        {
            /* No hidden or dot directories */
            if(dire->d_name[0] == '.')
                continue;

            r = parse_dir_internal(path, data);
            if(r < 0)
                return r;

            continue;
        }

        if(!is_reg && !is_lnk)
            continue;

        /* Build a happy path name */
        cfg_parse_file(path, data, &memory);

        /* We call it with blanks after files */
        if(cfg_value(path, NULL, NULL, NULL, data) == -1)
            break;

        /* Keep the memory around */
        if(memory)
            atexitv(free, memory);
    }

    closedir(dir);

    return 0;
}

int
cfg_parse_dir(const char* dirname, void* data)
{
    char olddir[MAXPATHLEN];
    int ret;

    ASSERT(dirname != NULL);

    if(!getcwd(olddir, MAXPATHLEN))
        olddir[0] = 0;

    if(chdir(dirname) == -1)
    {
        errmsg(NULL, data, "couldn't list config directory: %s", dirname);
        return -1;
    }

    ret = parse_dir_internal(NULL, data);

    if(olddir[0])
        chdir(olddir);

    return ret;
}

const char*
cfg_parse_uri (char *uri, char** scheme, char** host, char** user,
               char** path, char** query)
{
    char* t;

    *scheme = NULL;
    *host = NULL;
    *user = NULL;
    *path = NULL;
    *query = NULL;

    *scheme = strsep(&uri, ":");
    if(uri == NULL || (uri[0] != '/' && uri[1] != '/'))
        return "invalid uri";

    uri += 2;
    *host = strsep(&uri, "/");
    if(*host[0])
    {
        /* Parse the community out from the host */
        t = strchr(*host, '@');
        if(t)
        {
            *t = 0;
            *user = *host;
            *host = t + 1;
        }
    }

    if(!*host[0])
        return "invalid uri: no host name found";

    if(!uri || !uri[0])
        return "invalid uri: no path found";

    *path = uri;

    while((*path)[0] == '/')
        (*path)++;

    *query = strchr(*path, '?');
    if(*query)
    {
        *(*query) = 0;
        (*query)++;
    }

    return NULL;
}

#define CONFIG_SNMP "snmp"
#define CONFIG_SNMP2 "snmp2"
#define CONFIG_SNMP2C "snmp2c"

/* Parsing snmp, snmp2 snmp2c etc... */
const char*
cfg_parse_scheme(const char *str, enum snmp_version *scheme)
{
    /* Currently we only support SNMP pollers */
    if(strcmp(str, CONFIG_SNMP) == 0)
        *scheme = SNMP_V1;
    else if(strcmp(str, CONFIG_SNMP2) == 0)
        *scheme = SNMP_V2c;
    else if(strcmp(str, CONFIG_SNMP2C) == 0)
        *scheme = SNMP_V2c;
    else
        return "invalid scheme";
    return NULL;
}

const char*
cfg_parse_query (char *query, char **name, char **value, char **remainder)
{
	const char *msg;
	char *x;

	*remainder = NULL;

	if (*query == '&' || *query == '?')
		query++;

	/* Only use the first argument */
	x = strchr (query, '&');
	if (x) {
		*x = 0;
		*remainder = x + 1;
	}

	/* Parse into argument and value */
	*value = strchr (query, '=');
	if (*value) {
		*(*value) = 0;
		(*value)++;
		msg = cfg_parse_url_decode (*value);
		if (msg)
			return msg;
	}

	*name = query;
	return NULL;
}

const static char HEX_CHARS[] = "0123456789abcdef";

const char*
cfg_parse_url_decode (char *value)
{
	char *p, *a, *b;

	/* Change all plusses to spaces */
	for (p = strchr (value, '+'); p; p = strchr (p, '+'))
		*p = ' ';

	/* Now loop through looking for escapes */
	p = value;
	while (*value) {
		/*
		 * A percent sign followed by two hex digits means
		 * that the digits represent an escaped character.
		 */
		if (*value == '%') {
			value++;
			a = strchr (HEX_CHARS, tolower(value[0]));
			b = strchr (HEX_CHARS, tolower(value[1]));
			if (a && b) {
				*p = (a - HEX_CHARS) << 4;
				*(p++) |= (b - HEX_CHARS);
				value += 2;
				continue;
			}
		}

		*(p++) = *(value++);
	}

	*p = 0;
	return NULL;
}