From 35a6f4c07b58cb3ea1406d6a0c2e6a15b5645ea7 Mon Sep 17 00:00:00 2001 From: Balazs Scheidler Date: Wed, 25 Oct 2023 09:57:40 +0200 Subject: [PATCH] appmodel: add app-transform() parser Signed-off-by: Balazs Scheidler --- modules/appmodel/CMakeLists.txt | 1 + modules/appmodel/Makefile.am | 2 + modules/appmodel/app-parser-generator.h | 4 +- modules/appmodel/app-transform-generator.c | 122 +++++++++++++++++++++ modules/appmodel/app-transform-generator.h | 32 ++++++ modules/appmodel/appmodel-plugin.c | 13 +++ 6 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 modules/appmodel/app-transform-generator.c create mode 100644 modules/appmodel/app-transform-generator.h diff --git a/modules/appmodel/CMakeLists.txt b/modules/appmodel/CMakeLists.txt index 2bb5efe5d1..9365a9fc51 100644 --- a/modules/appmodel/CMakeLists.txt +++ b/modules/appmodel/CMakeLists.txt @@ -11,6 +11,7 @@ set (APPMODEL_SOURCES appmodel-context.c app-object-generator.c app-parser-generator.c + app-transform-generator.c application.c ) diff --git a/modules/appmodel/Makefile.am b/modules/appmodel/Makefile.am index ab3003fe45..5405ef2bb9 100644 --- a/modules/appmodel/Makefile.am +++ b/modules/appmodel/Makefile.am @@ -14,6 +14,8 @@ modules_appmodel_libappmodel_la_SOURCES = \ modules/appmodel/app-object-generator.h \ modules/appmodel/app-parser-generator.c \ modules/appmodel/app-parser-generator.h \ + modules/appmodel/app-transform-generator.c \ + modules/appmodel/app-transform-generator.h \ modules/appmodel/application.c \ modules/appmodel/application.h \ modules/appmodel/transformation.c \ diff --git a/modules/appmodel/app-parser-generator.h b/modules/appmodel/app-parser-generator.h index ea45d5e468..d3b78157be 100644 --- a/modules/appmodel/app-parser-generator.h +++ b/modules/appmodel/app-parser-generator.h @@ -22,8 +22,8 @@ * */ -#ifndef APPMODEL_APPPARSER_GENERATOR_H_INCLUDED -#define APPMODEL_APPPARSER_GENERATOR_H_INCLUDED +#ifndef APPMODEL_APP_PARSER_GENERATOR_H_INCLUDED +#define APPMODEL_APP_PARSER_GENERATOR_H_INCLUDED #include "plugin.h" diff --git a/modules/appmodel/app-transform-generator.c b/modules/appmodel/app-transform-generator.c new file mode 100644 index 0000000000..70db5a5997 --- /dev/null +++ b/modules/appmodel/app-transform-generator.c @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2024 Balazs Scheidler + * + * 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 "app-object-generator.h" +#include "appmodel.h" + +/* app-transform() */ + +typedef struct _AppTransformGenerator +{ + AppObjectGenerator super; + const gchar *flavour; + GString *block; +} AppTransformGenerator; + +static gboolean +_parse_transforms_arg(AppTransformGenerator *self, CfgArgs *args, const gchar *reference) +{ + self->flavour = cfg_args_get(args, "flavour"); + if (!self->flavour) + self->flavour = "default"; + return TRUE; +} + +static gboolean +app_transform_generator_parse_arguments(AppObjectGenerator *s, CfgArgs *args, const gchar *reference) +{ + AppTransformGenerator *self = (AppTransformGenerator *) s; + g_assert(args != NULL); + + if (!_parse_transforms_arg(self, args, reference)) + return FALSE; + + if (!app_object_generator_parse_arguments_method(&self->super, args, reference)) + return FALSE; + + return TRUE; +} + +static void +_generate_steps(AppTransformGenerator *self, GList *steps) +{ + for (GList *l = steps; l; l = l->next) + { + TransformationStep *step = l->data; + g_string_append_printf(self->block, " # step: %s", step->name); + g_string_append_printf(self->block, " %s\n", step->expr); + } +} + +static void +_generate_app_transform(Transformation *transformation, gpointer user_data) +{ + AppTransformGenerator *self = (AppTransformGenerator *) user_data; + + if (strcmp(transformation->super.instance, self->flavour) != 0) + return; + + if (!app_object_generator_is_application_included(&self->super, transformation->super.name)) + return; + + if (app_object_generator_is_application_excluded(&self->super, transformation->super.name)) + return; + + g_string_append_printf(self->block, "\n#Start Application %s\n", transformation->super.name); + g_string_append_printf(self->block, " if (meta.app_name == '%s') {\n", transformation->super.name); + for (GList *l = transformation->blocks; l; l = l->next) + { + TransformationBlock *block = l->data; + + _generate_steps(self, block->steps); + } + g_string_append(self->block, " };\n"); + g_string_append_printf(self->block, "\n#End Application %s\n", transformation->super.name); +} + + +static void +app_transform_generate_config(AppObjectGenerator *s, GlobalConfig *cfg, GString *result) +{ + AppTransformGenerator *self = (AppTransformGenerator *) s; + + self->block = result; + g_string_append_printf(result, "## app-transform(flavour(%s))\n" + "channel {\n" + " filterx {\n", self->flavour); + appmodel_iter_transformations(cfg, _generate_app_transform, self); + g_string_append(result, " };\n" + "}"); + self->block = NULL; +} + +CfgBlockGenerator * +app_transform_generator_new(gint context, const gchar *name) +{ + AppTransformGenerator *self = g_new0(AppTransformGenerator, 1); + + app_object_generator_init_instance(&self->super, context, name); + self->super.parse_arguments = app_transform_generator_parse_arguments; + self->super.generate_config = app_transform_generate_config; + return &self->super.super; +} diff --git a/modules/appmodel/app-transform-generator.h b/modules/appmodel/app-transform-generator.h new file mode 100644 index 0000000000..61db093465 --- /dev/null +++ b/modules/appmodel/app-transform-generator.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2017 Balabit + * Copyright (c) 2017 Balazs Scheidler + * + * 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. + * + */ + +#ifndef APPMODEL_APP_TRANSFORM_GENERATOR_H_INCLUDED +#define APPMODEL_APP_TRANSFORM_GENERATOR_H_INCLUDED + +#include "plugin.h" + +CfgBlockGenerator *app_transform_generator_new(gint context, const gchar *name); + +#endif diff --git a/modules/appmodel/appmodel-plugin.c b/modules/appmodel/appmodel-plugin.c index 03ed0d2f59..30ba1cbb47 100644 --- a/modules/appmodel/appmodel-plugin.c +++ b/modules/appmodel/appmodel-plugin.c @@ -24,6 +24,7 @@ #include "cfg-parser.h" #include "appmodel-parser.h" #include "app-parser-generator.h" +#include "app-transform-generator.h" #include "block-ref-parser.h" #include "plugin.h" #include "plugin-types.h" @@ -36,6 +37,12 @@ app_parser_construct(Plugin *p) return app_parser_generator_new(p->type, p->name); } +static gpointer +app_transform_construct(Plugin *p) +{ + return app_transform_generator_new(p->type, p->name); +} + static Plugin appmodel_plugins[] = { { @@ -53,6 +60,12 @@ static Plugin appmodel_plugins[] = .name = "app-parser", .construct = app_parser_construct, .parser = &block_ref_parser + }, + { + .type = LL_CONTEXT_PARSER | LL_CONTEXT_FLAG_GENERATOR, + .name = "app-transform", + .construct = app_transform_construct, + .parser = &block_ref_parser } };