Newer
Older
/*
* Asterisk -- A telephony toolkit for Linux.
*
* Configuration File Parser
*
Mark Spencer
committed
* Copyright (C) 1999-2004, Digium, Inc.
Mark Spencer
committed
* Mark Spencer <markster@digium.com>
*
* This program is free software, distributed under the terms of
* the GNU General Public License
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <asterisk/config_pvt.h>
#include <asterisk/cli.h>
#include <asterisk/lock.h>
#include <asterisk/options.h>
#include <asterisk/logger.h>
static int ast_cust_config=0;
struct ast_config *(*global_load_func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int
#ifdef PRESERVE_COMMENTS
,struct ast_comment_struct *
Mark Spencer
committed
#endif
);
AST_MUTEX_DEFINE_STATIC(ast_cust_config_lock);
static struct ast_config_reg *ast_cust_config_list;
static char *config_conf_file = "extconfig.conf";
static char *strip(char *buf)
{
char *start;
/* Strip off trailing whitespace, returns, etc */
while (!ast_strlen_zero(buf) && (buf[strlen(buf)-1]<33))
buf[strlen(buf)-1] = '\0';
start = buf;
/* Strip off leading whitespace, returns, etc */
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
static void free_comments(struct ast_comment *com)
{
struct ast_comment *l;
while (com) {
l = com;
com = com->next;
free(l);
}
}
Mark Spencer
committed
#endif
void ast_destroy(struct ast_config *ast)
{
struct ast_category *cat, *catn;
struct ast_variable *v, *vn;
cat = ast->root;
while(cat) {
v = cat->root;
while(v) {
vn = v;
free(v->name);
free(v->value);
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
free_comments(v->precomments);
free_comments(v->sameline);
Mark Spencer
committed
#endif
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
free_comments(cat->precomments);
free_comments(cat->sameline);
Mark Spencer
committed
#endif
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
Mark Spencer
committed
#endif
free(ast);
}
int ast_true(char *s)
{
/* Determine if this is a true value */
if (!strcasecmp(s, "yes") ||
!strcasecmp(s, "true") ||
!strcasecmp(s, "y") ||
!strcasecmp(s, "t") ||
!strcasecmp(s, "1") ||
!strcasecmp(s, "on"))
return 0;
}
int ast_false(char *s)
{
if (!s)
return 0;
/* Determine if this is a false value */
if (!strcasecmp(s, "no") ||
!strcasecmp(s, "false") ||
!strcasecmp(s, "n") ||
!strcasecmp(s, "f") ||
!strcasecmp(s, "0") ||
!strcasecmp(s, "off"))
return 0;
}
struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
{
struct ast_category *cat;
cat = config->root;
while(cat) {
if (cat->name == category)
return cat->root;
cat = cat->next;
}
cat = config->root;
while(cat) {
if (!strcasecmp(cat->name, category))
return cat->root;
cat = cat->next;
}
return NULL;
}
char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
{
struct ast_variable *v;
v = ast_variable_browse(config, category);
while (v) {
if (value == v->name)
return v->value;
v=v->next;
}
v = ast_variable_browse(config, category);
while (v) {
if (!strcasecmp(value, v->name))
return v->value;
v=v->next;
}
} else {
struct ast_category *cat;
cat = config->root;
while(cat) {
v = cat->root;
while (v) {
if (!strcasecmp(value, v->name))
return v->value;
v=v->next;
}
cat = cat->next;
}
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
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
int ast_variable_delete(struct ast_config *cfg, char *category, char *variable, char *value)
{
struct ast_variable *v, *pv, *bv, *bpv;
struct ast_category *cat;
cat = cfg->root;
while(cat) {
if (cat->name == category) {
break;
}
cat = cat->next;
}
if (!cat) {
cat = cfg->root;
while(cat) {
if (!strcasecmp(cat->name, category)) {
break;
}
cat = cat->next;
}
}
if (!cat)
return -1;
v = cat->root;
pv = NULL;
while (v) {
if ((variable == v->name) && (!value || !strcmp(v->value, value)))
break;
pv = v;
v=v->next;
}
if (!v) {
/* Get the last one that looks like it */
bv = NULL;
bpv = NULL;
v = cat->root;
pv = NULL;
while (v) {
if (!strcasecmp(variable, v->name) && (!value || !strcmp(v->value, value))) {
bv = v;
bpv = pv;
}
pv = v;
v=v->next;
}
v = bv;
}
if (v) {
/* Unlink from original position */
if (pv)
pv->next = v->next;
else
cat->root = v->next;
v->next = NULL;
free(v->name);
if (v->value)
free(v->value);
free_comments(v->sameline);
free_comments(v->precomments);
return 0;
}
return -1;
}
int ast_category_delete(struct ast_config *cfg, char *category)
{
struct ast_variable *v, *pv;
struct ast_category *cat, *cprev;
cat = cfg->root;
cprev = NULL;
while(cat) {
if (cat->name == category) {
break;
}
cprev = cat;
cat = cat->next;
}
if (!cat) {
cat = cfg->root;
cprev = NULL;
while(cat) {
if (!strcasecmp(cat->name, category)) {
break;
}
cprev = cat;
cat = cat->next;
}
}
if (!cat)
return -1;
/* Unlink it */
if (cprev)
cprev->next = cat->next;
else
cfg->root = cat->next;
v = cat->root;
while (v) {
pv = v;
v=v->next;
if (pv->value)
free(pv->value);
if (pv->name)
free(pv->name);
free_comments(pv->sameline);
free_comments(pv->precomments);
free(pv);
}
free_comments(cat->sameline);
free_comments(cat->precomments);
free(cat);
return 0;
}
struct ast_variable *ast_variable_append_modify(struct ast_config *config, char *category, char *variable, char *value, int newcat, int newvar, int move)
{
struct ast_category *cat, *pcat;
cat = config->root;
if (!newcat) {
while(cat) {
if (cat->name == category) {
break;
}
cat = cat->next;
}
if (!cat) {
cat = config->root;
while(cat) {
if (!strcasecmp(cat->name, category)) {
break;
}
cat = cat->next;
}
}
}
if (!cat) {
cat = malloc(sizeof(struct ast_category));
if (!cat)
return NULL;
memset(cat, 0, sizeof(struct ast_category));
strncpy(cat->name, category, sizeof(cat->name) - 1);
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
if (config->root) {
/* Put us at the end */
pcat = config->root;
while(pcat->next)
pcat = pcat->next;
pcat->next = cat;
} else {
/* We're the first one */
config->root = cat;
}
}
if (!newvar) {
v = cat->root;
pv = NULL;
while (v) {
if (variable == v->name)
break;
pv = v;
v=v->next;
}
if (!v) {
/* Get the last one that looks like it */
bv = NULL;
bpv = NULL;
v = cat->root;
pv = NULL;
while (v) {
if (!strcasecmp(variable, v->name)) {
bv = v;
bpv = pv;
}
pv = v;
v=v->next;
}
v = bv;
}
} else v = NULL;
if (v && move) {
/* Unlink from original position */
if (pv)
pv->next = v->next;
else
cat->root = v->next;
v->next = NULL;
}
if (!v) {
v = malloc(sizeof(struct ast_variable));
if (!v)
return NULL;
memset(v, 0, sizeof(struct ast_variable));
v->name = strdup(variable);
move = 1;
}
if (v->value)
free(v->value);
if (value)
v->value = strdup(value);
else
v->value = strdup("");
if (move) {
if (cat->root) {
pv = cat->root;
while (pv->next)
pv = pv->next;
pv->next = v;
} else {
cat->root = v;
}
}
return v;
}
Mark Spencer
committed
#endif
int ast_category_exist(struct ast_config *config, char *category_name)
{
struct ast_category *category = NULL;
category = config->root;
while(category) {
if (!strcasecmp(category->name,category_name))
return 1;
category = category->next;
}
return 0;
}
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
static struct ast_comment *build_comment(char *cmt)
{
struct ast_comment *c;
int len = strlen(cmt) + 1;
c = malloc(sizeof(struct ast_comment) + len);
memset(c, 0, sizeof(struct ast_comment));
Mark Spencer
committed
#endif
static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel
#ifdef PRESERVE_COMMENTS
, struct ast_comment_struct *acs
#endif
);
Mark Spencer
committed
static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, char *buf, int lineno, char *configfile, int includelevel
#ifdef PRESERVE_COMMENTS
,struct ast_comment_struct *acs
#endif
)
char *arg=NULL;
struct ast_config_reg *reg=NULL;
struct ast_config *(*load_func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int
#ifdef PRESERVE_COMMENTS
,struct ast_comment_struct *
#endif
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
Mark Spencer
committed
#endif
/* Strip off lines using ; as comment */
c = strchr(buf, ';');
while (c) {
if ((c == buf) || (*(c-1) != '\\')) {
*c = '\0';
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
c++;
if (*c != '!')
com = build_comment(c);
Mark Spencer
committed
#endif
} else {
*(c-1) = ';';
memmove(c, c + 1, strlen(c + 1));
}
c = strchr(c + 1, ';');
/* Actually parse the entry */
if (cur[0] == '[') {
/* A category header */
c = strchr(cur, ']');
if (c) {
*c = 0;
*_tmpc = malloc(sizeof(struct ast_category));
if (!*_tmpc) {
ast_destroy(tmp);
ast_log(LOG_WARNING,
"Out of memory, line %d\n", lineno);
return -1;
}
strncpy((*_tmpc)->name, cur+1, sizeof((*_tmpc)->name) - 1);
(*_tmpc)->root = NULL;
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
(*_tmpc)->precomments = acs->root;
(*_tmpc)->sameline = com;
Mark Spencer
committed
#endif
if (!tmp->prev)
tmp->root = *_tmpc;
else
tmp->prev->next = *_tmpc;
tmp->prev = *_tmpc;
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
acs->root = NULL;
acs->prev = NULL;
Mark Spencer
committed
#endif
*_last = NULL;
} else {
ast_log(LOG_WARNING,
"parse error: no closing ']', line %d of %s\n", lineno, configfile);
}
} else if (cur[0] == '#') {
/* A directive */
cur++;
c = cur;
while(*c && (*c > 32)) c++;
if (*c) {
*c = '\0';
c++;
/* Find real argument */
while(*c && (*c < 33)) c++;
if (!*c)
c = NULL;
} else
c = NULL;
if (!strcasecmp(cur, "include")) {
/* A #include */
if (c) {
while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
/* Get rid of leading mess */
cur = c;
c = cur + strlen(cur) - 1;
if ((*c == '>') || (*c == '<') || (*c == '\"'))
*c = '\0';
else
break;
}
if((c = strchr(cur,':'))) {
*c = '\0';
if(arg && cur) {
load_func = NULL;
if(ast_cust_config_list)
reg = get_ast_cust_config(cur);
if(reg && reg->func)
load_func = reg->func;
if(load_func) {
ast_log(LOG_NOTICE,"External Include '%s' via '%s' config engine\n",arg,cur);
Anthony Minessale II
committed
load_func(arg,tmp, _tmpc, _last, includelevel
#ifdef PRESERVE_COMMENTS
,&acs
#endif
);
}
else
ast_log(LOG_WARNING,"Cant Find Registered Config Engine [%s] for [%s]\n",cur,arg);
}
else {
__ast_load(cur, tmp, _tmpc, _last, includelevel + 1
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
Mark Spencer
committed
#endif
ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
} else
ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
/* Strip off leading and trailing "'s and <>'s */
ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
} else {
/* Just a line (variable = value) */
if (!*_tmpc) {
ast_log(LOG_WARNING,
"parse error: No category context for line %d of %s\n", lineno, configfile);
}
c = strchr(cur, '=');
if (c) {
*c = 0;
c++;
/* Ignore > in => */
v = malloc(sizeof(struct ast_variable));
if (v) {
v->next = NULL;
v->name = strdup(strip(cur));
v->value = strdup(strip(c));
v->lineno = lineno;
v->object = object;
/* Put and reset comments */
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
Mark Spencer
committed
v->sameline = com;
acs->prev = NULL;
acs->root = NULL;
Mark Spencer
committed
#endif
v->blanklines = 0;
(*_last)->next = v;
else
(*_tmpc)->root = v;
*_last = v;
} else {
ast_destroy(tmp);
ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
return -1;
}
} else {
ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
}
}
} else {
/* store any comments if there are any */
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
if (com) {
if (acs->prev)
acs->prev->next = com;
else
acs->root = com;
acs->prev = com;
} else {
Mark Spencer
committed
#endif
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
static void dump_comments(FILE *f, struct ast_comment *comment)
{
while (comment) {
Mark Spencer
committed
#endif
int ast_save(char *configfile, struct ast_config *cfg, char *generator)
{
FILE *f;
char fn[256];
char date[256]="";
time_t t;
struct ast_variable *var;
struct ast_category *cat;
int blanklines = 0;
if (configfile[0] == '/') {
strncpy(fn, configfile, sizeof(fn)-1);
} else {
snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
}
time(&t);
strncpy(date, ctime(&t), sizeof(date) - 1);
if ((f = fopen(fn, "w"))) {
if ((option_verbose > 1) && !option_debug)
ast_verbose( VERBOSE_PREFIX_2 "Saving '%s': ", fn);
fprintf(f, ";!\n");
fprintf(f, ";! Automatically generated configuration file\n");
fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
fprintf(f, ";! Generator: %s\n", generator);
fprintf(f, ";! Creation Date: %s", date);
fprintf(f, ";!\n");
cat = cfg->root;
while(cat) {
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
/* Dump any precomments */
dump_comments(f, cat->precomments);
Mark Spencer
committed
#endif
/* Dump section with any appropriate comment */
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
fprintf(f, "[%s] ; %s\n", cat->name, cat->sameline->cmt);
Mark Spencer
committed
#endif
fprintf(f, "[%s]\n", cat->name);
var = cat->root;
while(var) {
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
Mark Spencer
committed
#endif
fprintf(f, "%s %s %s ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
else
fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
if (var->blanklines) {
blanklines = var->blanklines;
while (blanklines) {
fprintf(f, "\n");
blanklines--;
}
}
var = var->next;
}
#if 0
/* Put an empty line */
fprintf(f, "\n");
#endif
cat = cat->next;
}
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
dump_comments(f, cfg->trailingcomments);
Mark Spencer
committed
#endif
} else {
if (option_debug)
printf("Unable to open for writing: %s\n", fn);
else if (option_verbose > 1)
printf( "Unable to write (%s)", strerror(errno));
return -1;
}
fclose(f);
return 0;
}
Mark Spencer
committed
static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel
#ifdef PRESERVE_COMMENTS
, struct ast_comment_struct *acs
#endif
)
char buf[8192];
struct ast_config_reg *reg=NULL;
struct ast_config *(*load_func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int
#ifdef PRESERVE_COMMENTS
,struct ast_comment_struct *
#endif
);
load_func=NULL;
if (strcmp(configfile,config_conf_file) && strcmp(configfile,"asterisk.conf") && ast_cust_config_list) {
if (global_load_func) {
load_func = global_load_func;
} else {
reg = get_ast_cust_config_keyword(configfile);
if (reg && reg->func) {
load_func = reg->func;
} else {
reg = get_ast_cust_config_keyword("global");
if (reg && reg->func)
global_load_func = load_func = reg->func;
}
}
if (load_func) {
ast_log(LOG_NOTICE,"Loading Config %s via %s engine\n",configfile,reg && reg->name ? reg->name : "global");
tmp = load_func(configfile,tmp, _tmpc, _last, includelevel
#ifdef PRESERVE_COMMENTS
,&acs
#endif
);
snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, configfile);
}
if ((option_verbose > 1) && !option_debug) {
ast_verbose( VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
fflush(stdout);
}
if ((f = fopen(fn, "r"))) {
if (option_debug)
ast_log(LOG_DEBUG, "Parsing %s\n", fn);
else if (option_verbose > 1)
ast_verbose( "Found\n");
if (!tmp) {
tmp = malloc(sizeof(struct ast_config));
if (tmp)
memset(tmp, 0, sizeof(struct ast_config));
if (!tmp) {
ast_log(LOG_WARNING, "Out of memory\n");
fclose(f);
return NULL;
}
while(!feof(f)) {
lineno++;
Mark Spencer
committed
if (fgets(buf, sizeof(buf), f)) {
Mark Spencer
committed
if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel
#ifdef PRESERVE_COMMENTS
, acs
#endif
)) {
}
}
}
fclose(f);
} else {
if (option_debug)
ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
else if (option_verbose > 1)
ast_verbose( "Not found (%s)\n", strerror(errno));
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
if (master) {
/* Keep trailing comments */
tmp->trailingcomments = acs->root;
acs->root = NULL;
acs->prev = NULL;
}
Mark Spencer
committed
#endif
struct ast_config_reg *get_ast_cust_config_keyword(char *name)
{
struct ast_config_reg *reg,*ret=NULL;
int x=0;
ast_mutex_lock(&ast_cust_config_lock);
for (reg=ast_cust_config_list;reg && !ret;reg=reg->next) {
for (x=0;x<reg->keycount && !ret ;x++) {
if (!strcmp(reg->keywords[x],name))
ast_mutex_unlock(&ast_cust_config_lock);
return ret;
}
struct ast_config_reg *get_ast_cust_config(char *name)
{
struct ast_config_reg *ptr=NULL;
ast_mutex_lock(&ast_cust_config_lock);
for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
if (!strcmp(name,ptr->name))
break;
}
ast_mutex_unlock(&ast_cust_config_lock);
return ptr;
}
struct ast_config_reg *key;
ast_mutex_lock(&ast_cust_config_lock);
for (key=ast_cust_config_list;key;key=key->next) {
ast_config_deregister(key);
}
ast_cust_config_list = NULL;
ast_mutex_unlock(&ast_cust_config_lock);
}
struct ast_config_reg *get_config_registrations(void)
{
int ast_config_register(struct ast_config_reg *new)
{
struct ast_config_reg *ptr;
ast_mutex_lock(&ast_cust_config_lock);
new->keycount = 0;
for(ptr=ast_cust_config_list;ptr->next;ptr=ptr->next);
}
ast_mutex_unlock(&ast_cust_config_lock);
ast_log(LOG_NOTICE,"Registered Config Engine %s\n",new->name);
return 1;
}
int ast_config_deregister(struct ast_config_reg *del)
{
struct ast_config_reg *ptr=NULL,*last=NULL;
ast_mutex_lock(&ast_cust_config_lock);
for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
if (ptr == del) {
if (last && ptr->next) {
last->next = ptr->next;
} else if (last && ! ptr->next) {
last->next = NULL;
} else if (!last && ptr->next) {
ast_cust_config_list = ptr->next;
} else if (!last && !ptr->next) {
ast_cust_config_list = NULL;
}
}
last = ptr;
}
ast_mutex_unlock(&ast_cust_config_lock);
return 0;
}
int ast_cust_config_active(void) {
return (ast_cust_config >0) ? 1 : 0;
}
struct ast_config *ast_load(char *configfile)
{
struct ast_category *tmpc=NULL;
struct ast_variable *last = NULL;
Mark Spencer
committed
#ifdef PRESERVE_COMMENTS
struct ast_comment_struct acs = { NULL, NULL };
Mark Spencer
committed
#endif
Mark Spencer
committed
return __ast_load(configfile, NULL, &tmpc, &last, 0
#ifdef PRESERVE_COMMENTS
,&acs
#endif
);
char *ast_category_browse(struct ast_config *config, char *prev)
{
struct ast_category *cat;
if (!prev) {
if (config->root)
return config->root->name;
else
return NULL;
}
cat = config->root;
while(cat) {
if (cat->name == prev) {
if (cat->next)
return cat->next->name;
else
return NULL;
}
cat = cat->next;
}
cat = config->root;
while(cat) {
if (!strcasecmp(cat->name, prev)) {
if (cat->next)
return cat->next->name;
else
return NULL;
}
cat = cat->next;
}
return NULL;
}
struct ast_config *ast_new_config(void)
{
struct ast_config *config;
config = malloc(sizeof(struct ast_config));
memset(config,0,sizeof(struct ast_config));
return config;
}
struct ast_category *ast_new_category(char *name)
{
struct ast_category *category;
category = malloc(sizeof(struct ast_category));
memset(category,0,sizeof(struct ast_category));
strncpy(category->name,name,sizeof(category->name) - 1);
struct ast_variable *ast_new_variable(char *name, char *value)
{
struct ast_variable *variable;
variable = malloc(sizeof(struct ast_variable));
memset(variable,0,sizeof(struct ast_variable));
variable->object=0;
variable->name = malloc(strlen(name)+1);