1 /*
2  * Copyright (c) 2002-2014 Balabit
3  * Copyright (c) 1998-2014 Balázs Scheidler
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * As an additional exemption you are allowed to compile & link against the
20  * OpenSSL libraries as published by the OpenSSL project. See the file
21  * COPYING for details.
22  *
23  */
24 
25 #include "cfg.h"
26 
27 /*
28  * $(template <template-name> ...)
29  *
30  *   This template function looks up <template-name> in the configuration
31  *   and uses that format its result.  The template name can either be a
32  *   static or a dynamic reference.
33  *
34  *     * static: means that we perform lookup at configuration read time
35  *
36  *     * dynamic: the template name is actually a template (e.g.
37  *       ${foobar}), its result are taken as the name of the template and
38  *       that is looked up at runtime.
39  *
40  *   Dynamic references are slower, but more flexible.  syslog-ng uses the
41  *   following algorithm to determine which one to use:
42  *
43  *     * static: if the name of the template can be resolved at compile
44  *       time, the binding becomes static.
45  *
46  *     * dynamic: if static lookup fails and the template name contains at
47  *       least one '$' character to indicate that it is actually a template.
48  *
49  *   In case of dynamic we allow 2nd and further arguments which will be
50  *   used as content if the lookup fails.
51  *
52  *   Examples:
53  *      $(template foobar)     -> static binding
54  *      $(template ${foobar})  -> dynamic binding
55  *      $(template ${foobar} '$DATE $HOST $MSGHDR$MSG\n') -> dynamic binding with fallback
56  *
57  */
58 typedef struct _TFTemplateState
59 {
60   TFSimpleFuncState super;
61   GlobalConfig *cfg;
62   LogTemplate *invoked_template;
63 } TFTemplateState;
64 
65 static LogTemplate *
tf_template_lookup_invoked_template(TFTemplateState * state,GlobalConfig * cfg,const gchar * function_name,GError ** error)66 tf_template_lookup_invoked_template(TFTemplateState *state, GlobalConfig *cfg, const gchar *function_name,
67                                     GError **error)
68 {
69   return cfg_tree_lookup_template(&cfg->tree, function_name);
70 }
71 
72 static const gchar *
tf_template_extract_invoked_template_name_from_args(gint argc,gchar * argv[])73 tf_template_extract_invoked_template_name_from_args(gint argc, gchar *argv[])
74 {
75   if (argc >= 2 && strcmp(argv[0], "template") == 0)
76     return argv[1];
77   return NULL;
78 }
79 
80 static gboolean
tf_template_statically_bound(TFTemplateState * state)81 tf_template_statically_bound(TFTemplateState *state)
82 {
83   return state->invoked_template != NULL;
84 }
85 
86 static gboolean
tf_template_dynamically_bound(TFTemplateState * state)87 tf_template_dynamically_bound(TFTemplateState *state)
88 {
89   return !tf_template_statically_bound(state);
90 }
91 
92 static gboolean
tf_template_prepare(LogTemplateFunction * self,gpointer s,LogTemplate * parent,gint argc,gchar * argv[],GError ** error)93 tf_template_prepare(LogTemplateFunction *self, gpointer s, LogTemplate *parent, gint argc, gchar *argv[],
94                     GError **error)
95 {
96   TFTemplateState *state = (TFTemplateState *) s;
97   const gchar *invoked_template_name;
98 
99   g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
100   invoked_template_name = tf_template_extract_invoked_template_name_from_args(argc, argv);
101   if (!invoked_template_name)
102     {
103       g_set_error(error, LOG_TEMPLATE_ERROR, LOG_TEMPLATE_ERROR_COMPILE,
104                   "$(template) requires one argument, that specifies the template name to be invoked");
105       return FALSE;
106     }
107 
108   state->invoked_template = tf_template_lookup_invoked_template(state, parent->cfg, invoked_template_name, error);
109   if (tf_template_statically_bound(state))
110     return TRUE;
111 
112   /* compile time lookup failed, let's check if it's a dynamically bound invocation */
113   if (strchr(invoked_template_name, '$') == NULL)
114     {
115       /* our argument is not a template, no chance of being better at
116        * runtime, raise as an error */
117 
118       g_set_error(error, LOG_TEMPLATE_ERROR, LOG_TEMPLATE_ERROR_COMPILE,
119                   "$(template) Unknown template function or template \"%s\"",
120                   invoked_template_name);
121       return FALSE;
122     }
123 
124   state->cfg = parent->cfg;
125   return tf_simple_func_prepare(self, s, parent, argc, argv, error);
126 }
127 
128 static void
tf_template_eval(LogTemplateFunction * self,gpointer s,LogTemplateInvokeArgs * args)129 tf_template_eval(LogTemplateFunction *self, gpointer s, LogTemplateInvokeArgs *args)
130 {
131   TFTemplateState *state = (TFTemplateState *) s;
132 
133   if (tf_template_dynamically_bound(state))
134     tf_simple_func_eval(self, s, args);
135 }
136 
137 static LogTemplate *
tf_template_get_template_to_be_invoked(TFTemplateState * state,const LogTemplateInvokeArgs * args)138 tf_template_get_template_to_be_invoked(TFTemplateState *state, const LogTemplateInvokeArgs *args)
139 {
140   LogTemplate *invoked_template;
141 
142   if (tf_template_dynamically_bound(state))
143     {
144       const gchar *template_name = args->argv[0]->str;
145 
146       invoked_template = cfg_tree_lookup_template(&state->cfg->tree, template_name);
147 
148       msg_trace("$(template) dynamic template lookup result",
149                 evt_tag_str("template", template_name),
150                 evt_tag_int("found", invoked_template != NULL));
151     }
152   else
153     {
154       invoked_template = log_template_ref(state->invoked_template);
155     }
156   return invoked_template;
157 }
158 
159 static void
tf_template_call(LogTemplateFunction * self,gpointer s,const LogTemplateInvokeArgs * args,GString * result)160 tf_template_call(LogTemplateFunction *self, gpointer s, const LogTemplateInvokeArgs *args, GString *result)
161 {
162   TFTemplateState *state = (TFTemplateState *) s;
163   LogTemplate *invoked_template = NULL;
164 
165   invoked_template = tf_template_get_template_to_be_invoked(state, args);
166   if (!invoked_template)
167     {
168       _append_args_with_separator(state->super.argc - 1, (GString **) &args->argv[1], result, ' ');
169       return;
170     }
171 
172   log_template_append_format_with_context(invoked_template, args->messages, args->num_messages,
173                                           args->options, result);
174   log_template_unref(invoked_template);
175 }
176 
177 static void
tf_template_free_state(gpointer s)178 tf_template_free_state(gpointer s)
179 {
180   TFTemplateState *state = (TFTemplateState *) s;
181 
182   log_template_unref(state->invoked_template);
183   tf_simple_func_free_state(s);
184 }
185 
186 TEMPLATE_FUNCTION(TFTemplateState, tf_template,
187                   tf_template_prepare, tf_template_eval, tf_template_call, tf_template_free_state,
188                   NULL);
189