forked from gdestuynder/audisp-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-config.c
294 lines (263 loc) · 6.02 KB
/
json-config.c
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
/* json-config.c --
* Copyright (c) 2014 Mozilla Corporation.
* Copyright 2008 Red Hat Inc., Durham, North Carolina.
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors:
* Steve Grubb <[email protected]>
* Guillaume Destuynder <[email protected]>
*
*/
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <syslog.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include "json-config.h"
struct nv_pair
{
const char *name;
const char *value;
const char *option;
};
struct kw_pair
{
const char *name;
int (*parser)(struct nv_pair *, int, json_conf_t *);
int max_options;
};
struct nv_list
{
const char *name;
int option;
};
static char *get_line(FILE *f, char *buf);
static int nv_split(char *buf, struct nv_pair *nv);
static const struct kw_pair *kw_lookup(const char *val);
static int prepend_parser(struct nv_pair *nv, int line,
json_conf_t *config);
static int postpend_parser(struct nv_pair *nv, int line,
json_conf_t *config);
static const struct kw_pair keywords[] =
{
{"prepend_msg", prepend_parser, 0},
{"postpend_msg", postpend_parser, 0},
{NULL}
};
/*
* Set everything to its default value
*/
void clear_config(json_conf_t *config)
{
config->prepend_msg= NULL;
config->postpend_msg = NULL;
}
int load_config(json_conf_t *config, const char *file)
{
int fd, rc, mode, lineno = 1;
struct stat st;
FILE *f;
char buf[128];
clear_config(config);
/* open the file */
mode = O_RDONLY;
rc = open(file, mode);
if (rc < 0) {
if (errno != ENOENT) {
syslog(LOG_ERR, "Error opening %s (%s)", file,
strerror(errno));
return 1;
}
syslog(LOG_WARNING,
"Config file %s doesn't exist", file);
return 1;
}
fd = rc;
/* check the file's permissions: owned by root, not world writable,
* not symlink.
*/
if (fstat(fd, &st) < 0) {
syslog(LOG_ERR, "Error fstat'ing config file (%s)",
strerror(errno));
close(fd);
return 1;
}
if ((st.st_mode & S_IWOTH) == S_IWOTH) {
syslog(LOG_ERR, "Error - %s is world writable",
file);
close(fd);
return 1;
}
if (!S_ISREG(st.st_mode)) {
syslog(LOG_ERR, "Error - %s is not a regular file",
file);
close(fd);
return 1;
}
/* it's ok, read line by line */
f = fdopen(fd, "r");
if (f == NULL) {
syslog(LOG_ERR, "Error - fdopen failed (%s)",
strerror(errno));
close(fd);
return 1;
}
while (get_line(f, buf)) {
// convert line into name-value pair
const struct kw_pair *kw;
struct nv_pair nv;
rc = nv_split(buf, &nv);
switch (rc) {
case 0: // fine
break;
case 1: // not the right number of tokens.
syslog(LOG_ERR,
"Wrong number of arguments for line %d in %s",
lineno, file);
break;
case 2: // no '=' sign
syslog(LOG_ERR,
"Missing equal sign for line %d in %s",
lineno, file);
break;
default: // something else went wrong...
syslog(LOG_ERR,
"Unknown error for line %d in %s",
lineno, file);
break;
}
if (nv.name == NULL) {
lineno++;
continue;
}
if (nv.value == NULL) {
fclose(f);
return 1;
}
/* identify keyword or error */
kw = kw_lookup(nv.name);
if (kw->name == NULL) {
syslog(LOG_ERR,
"Unknown keyword \"%s\" in line %d of %s",
nv.name, lineno, file);
fclose(f);
return 1;
}
/* Check number of options */
if (kw->max_options == 0 && nv.option != NULL) {
syslog(LOG_ERR,
"Keyword \"%s\" has invalid option "
"\"%s\" in line %d of %s",
nv.name, nv.option, lineno, file);
fclose(f);
return 1;
}
/* dispatch to keyword's local parser */
rc = kw->parser(&nv, lineno, config);
if (rc != 0) {
fclose(f);
return 1; // local parser puts message out
}
lineno++;
}
fclose(f);
return 0;
}
static char *get_line(FILE *f, char *buf)
{
if (fgets_unlocked(buf, 128, f)) {
/* remove newline */
char *ptr = strchr(buf, 0x0a);
if (ptr)
*ptr = 0;
return buf;
}
return NULL;
}
static int nv_split(char *buf, struct nv_pair *nv)
{
/* Get the name part */
char *ptr;
nv->name = NULL;
nv->value = NULL;
nv->option = NULL;
ptr = strtok(buf, " ");
if (ptr == NULL)
return 0; /* If there's nothing, go to next line */
if (ptr[0] == '#')
return 0; /* If there's a comment, go to next line */
nv->name = ptr;
/* Check for a '=' */
ptr = strtok(NULL, " ");
if (ptr == NULL)
return 1;
if (strcmp(ptr, "=") != 0)
return 2;
/* get the value */
ptr = strtok(NULL, " ");
if (ptr == NULL)
return 1;
nv->value = ptr;
/* See if there's an option */
ptr = strtok(NULL, " ");
if (ptr) {
nv->option = ptr;
/* Make sure there's nothing else */
ptr = strtok(NULL, " ");
if (ptr)
return 1;
}
/* Everything is OK */
return 0;
}
static const struct kw_pair *kw_lookup(const char *val)
{
int i = 0;
while (keywords[i].name != NULL) {
if (strcasecmp(keywords[i].name, val) == 0)
break;
i++;
}
return &keywords[i];
}
static int prepend_parser(struct nv_pair *nv, int line,
json_conf_t *config)
{
if (nv->value)
config->prepend_msg = strdup(nv->value);
else
config->prepend_msg = NULL;
return 0;
}
static int postpend_parser(struct nv_pair *nv, int line,
json_conf_t *config)
{
if (nv->value)
config->postpend_msg = strdup(nv->value);
else
config->postpend_msg = NULL;
return 0;
}
void free_config(json_conf_t *config)
{
free((void *)config->prepend_msg);
free((void *)config->postpend_msg);
}