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
|
/*
* Copyright (c) 2014 Balabit
* Copyright (c) 2014 Viktor Juhász <viktor.juhasz@balabit.com>
* Copyright (c) 2014 Viktor Tusa <viktor.tusa@balabit.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "config_parse_lib.h"
#include "cfg.h"
#include "cfg-parser.h"
#include "plugin.h"
static gchar **
split_plugin_name_and_config(const gchar *config_to_parse)
{
gchar **delimited = NULL;
if (!config_to_parse || strlen(config_to_parse) == 0)
return NULL;
delimited = g_strsplit(config_to_parse, "(", 2);
if (!delimited)
return NULL;
if (g_strv_length(delimited) == 2)
{
gchar *tmp = delimited[1];
delimited[1] = g_strdup_printf("(%s", tmp);
g_free(tmp);
return delimited;
}
g_strfreev(delimited);
return NULL;
}
static gpointer
parse_plugin_config(const gchar *config_to_parse, gint context, gpointer arg)
{
gpointer result = NULL;
CfgLexer *old_lexer = configuration->lexer;
CfgLexer *lexer = NULL;
CFG_LTYPE *yylloc = NULL;
Plugin *plugin = NULL;
gchar **delimited;
delimited = split_plugin_name_and_config(config_to_parse);
if (!delimited)
{
fprintf(stderr, "Error parsing expression\n");
return NULL;
}
lexer = cfg_lexer_new_buffer(configuration, delimited[1], strlen(delimited[1]));
if (!lexer)
{
fprintf(stderr, "Error parsing expression\n");
goto finish;
}
yylloc = g_new0(CFG_LTYPE, 1);
yylloc->first_column = 1;
yylloc->first_line = 1;
yylloc->last_column = 1;
yylloc->last_line = 1;
plugin = cfg_find_plugin(configuration, context, delimited[0]);
if (!plugin)
{
fprintf(stderr, "Error parsing expression\n");
goto finish;
}
configuration->lexer = lexer;
cfg_set_global_paths(configuration);
cfg_lexer_push_context(lexer, main_parser.context, main_parser.keywords, main_parser.name);
result = cfg_parse_plugin(configuration, plugin, yylloc, arg);
cfg_lexer_pop_context(lexer);
if (!result)
{
fprintf(stderr, "Error parsing expression\n");
goto finish;
}
finish:
configuration->lexer = old_lexer;
if (lexer)
cfg_lexer_free(lexer);
g_free(yylloc);
g_strfreev(delimited);
return result;
}
static gboolean
parse_general_config(const gchar *config_to_parse, gint context, gpointer arg)
{
gpointer result = NULL;
CfgLexer *lexer = cfg_lexer_new_buffer(configuration, config_to_parse, strlen(config_to_parse));
if (!cfg_run_parser(configuration, lexer, &main_parser, &result, arg))
{
fprintf(stderr, "Error parsing expression\n");
return FALSE;
}
return TRUE;
}
gboolean
parse_config(const gchar *config_to_parse, gint context, gpointer arg, gpointer *result)
{
if (!config_to_parse || strlen(config_to_parse) == 0)
return FALSE;
if (result)
{
*result = parse_plugin_config(config_to_parse, context, arg);
if (*result)
return TRUE;
}
if (main_parser.context != context)
return FALSE;
return parse_general_config(config_to_parse, context, arg);
}
|