1 /*
2  * Copyright (c) 2002-2013 Balabit
3  * Copyright (c) 1998-2012 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 #ifndef CFG_H_INCLUDED
26 #define CFG_H_INCLUDED
27 
28 #include "syslog-ng.h"
29 #include "cfg-tree.h"
30 #include "cfg-lexer.h"
31 #include "cfg-parser.h"
32 #include "plugin.h"
33 #include "persist-state.h"
34 #include "template/templates.h"
35 #include "host-resolve.h"
36 #include "type-hinting.h"
37 #include "stats/stats.h"
38 #include "dnscache.h"
39 #include "file-perms.h"
40 
41 #include <sys/types.h>
42 #include <regex.h>
43 
44 /* destination mark modes */
45 enum
46 {
47   MM_INTERNAL = 1,
48   MM_DST_IDLE,
49   MM_HOST_IDLE,
50   MM_PERIODICAL,
51   MM_NONE,
52   MM_GLOBAL,
53 };
54 
55 /* configuration data kept between configuration reloads */
56 typedef struct _PersistConfig PersistConfig;
57 
58 /* configuration data as loaded from the config file */
59 struct _GlobalConfig
60 {
61   /* version number specified by the user, set _after_ parsing is complete */
62   /* hex-encoded syslog-ng major/minor, e.g. 0x0201 is syslog-ng 2.1 format */
63   gint user_version;
64 
65   const gchar *filename;
66   PluginContext plugin_context;
67   gboolean use_plugin_discovery;
68   gboolean enable_forced_modules;
69   CfgLexer *lexer;
70   CfgArgs *globals;
71 
72   StatsOptions stats_options;
73   gint mark_freq;
74   gint flush_lines;
75   gint mark_mode;
76   gboolean threaded;
77   gboolean pass_unix_credentials;
78   gboolean chain_hostnames;
79   gboolean keep_hostname;
80   gboolean check_hostname;
81   gboolean bad_hostname_compiled;
82   regex_t bad_hostname;
83   gchar *bad_hostname_re;
84   gchar *custom_domain;
85   DNSCacheOptions dns_cache_options;
86   gint time_reopen;
87   gint time_reap;
88   gint suppress;
89   gint type_cast_strictness;
90 
91   gint log_fifo_size;
92   gint log_msg_size;
93   gboolean trim_large_messages;
94 
95   gboolean create_dirs;
96   FilePermOptions file_perm_options;
97   GList *source_mangle_callback_list;
98   gboolean use_uniqid;
99 
100   gboolean keep_timestamp;
101 
102   gchar *recv_time_zone;
103   LogTemplateOptions template_options;
104   HostResolveOptions host_resolve_options;
105 
106   gchar *file_template_name;
107   gchar *proto_template_name;
108 
109   LogTemplate *file_template;
110   LogTemplate *proto_template;
111 
112   guint min_iw_size_per_reader;
113 
114   PersistConfig *persist;
115   PersistState *state;
116   GHashTable *module_config;
117 
118   CfgTree tree;
119 
120   GString *preprocess_config;
121   GString *original_config;
122 
123   GList *file_list;
124 };
125 
126 gboolean cfg_load_module_with_args(GlobalConfig *cfg, const gchar *module_name, CfgArgs *args);
127 gboolean cfg_load_module(GlobalConfig *cfg, const gchar *module_name);
128 gboolean cfg_is_module_available(GlobalConfig *self, const gchar *module_name);
129 void cfg_discover_candidate_modules(GlobalConfig *self);
130 
131 Plugin *cfg_find_plugin(GlobalConfig *cfg, gint plugin_type, const gchar *plugin_name);
132 gpointer cfg_parse_plugin(GlobalConfig *cfg, Plugin *plugin, CFG_LTYPE *yylloc, gpointer arg);
133 
134 gboolean cfg_allow_config_dups(GlobalConfig *self);
135 
136 void cfg_bad_hostname_set(GlobalConfig *self, gchar *bad_hostname_re);
137 gint cfg_lookup_mark_mode(const gchar *mark_mode);
138 void cfg_set_mark_mode(GlobalConfig *self, const gchar *mark_mode);
139 
140 gint cfg_tz_convert_value(gchar *convert);
141 gint cfg_ts_format_value(gchar *format);
142 
143 void cfg_set_version_without_validation(GlobalConfig *self, gint version);
144 gboolean cfg_set_version(GlobalConfig *self, gint version);
145 gboolean cfg_set_current_version(GlobalConfig *self);
146 
147 void cfg_set_global_paths(GlobalConfig *self);
148 
149 GlobalConfig *cfg_new(gint version);
150 GlobalConfig *cfg_new_snippet(void);
151 GlobalConfig *cfg_new_subordinate(GlobalConfig *master);
152 gboolean cfg_run_parser(GlobalConfig *self, CfgLexer *lexer, CfgParser *parser, gpointer *result, gpointer arg);
153 gboolean cfg_run_parser_with_main_context(GlobalConfig *self, CfgLexer *lexer, CfgParser *parser, gpointer *result,
154                                           gpointer arg, const gchar *desc);
155 gboolean cfg_read_config(GlobalConfig *cfg, const gchar *fname, gchar *preprocess_into);
156 void cfg_load_forced_modules(GlobalConfig *self);
157 void cfg_shutdown(GlobalConfig *self);
158 gboolean cfg_is_shutting_down(GlobalConfig *cfg);
159 void cfg_free(GlobalConfig *self);
160 gboolean cfg_init(GlobalConfig *cfg);
161 gboolean cfg_deinit(GlobalConfig *cfg);
162 
163 PersistConfig *persist_config_new(void);
164 void persist_config_free(PersistConfig *self);
165 void cfg_persist_config_move(GlobalConfig *src, GlobalConfig *dest);
166 void cfg_persist_config_add(GlobalConfig *cfg, const gchar *name, gpointer value, GDestroyNotify destroy,
167                             gboolean force);
168 gpointer cfg_persist_config_fetch(GlobalConfig *cfg, const gchar *name);
169 
170 typedef gboolean(* mangle_callback)(GlobalConfig *cfg, LogMessage *msg, gpointer user_data);
171 
172 void register_source_mangle_callback(GlobalConfig *src, mangle_callback cb);
173 void uregister_source_mangle_callback(GlobalConfig *src, mangle_callback cb);
174 
175 static inline gboolean
__cfg_is_config_version_older(GlobalConfig * cfg,gint req)176 __cfg_is_config_version_older(GlobalConfig *cfg, gint req)
177 {
178   if (!cfg)
179     return FALSE;
180   if (version_convert_from_user(cfg->user_version) >= req)
181     return FALSE;
182   return TRUE;
183 }
184 
185 /* VERSION_VALUE_LAST_SEMANTIC_CHANGE needs to be bumped in versioning.h whenever
186  * we add a conditional on the config version anywhere in the codebase.  The
187  * G_STATIC_ASSERT checks that we indeed did that */
188 
189 #define cfg_is_config_version_older(__cfg, __req) \
190   ({ \
191     /* check that VERSION_VALUE_LAST_SEMANTIC_CHANGE is set correctly */ G_STATIC_ASSERT((__req) <= VERSION_VALUE_LAST_SEMANTIC_CHANGE); \
192     __cfg_is_config_version_older(__cfg, __req); \
193   })
194 
195 static inline void
cfg_set_use_uniqid(gboolean flag)196 cfg_set_use_uniqid(gboolean flag)
197 {
198   configuration->use_uniqid = !!flag;
199 }
200 
201 gint cfg_get_user_version(const GlobalConfig *cfg);
202 guint cfg_get_parsed_version(const GlobalConfig *cfg);
203 const gchar *cfg_get_filename(const GlobalConfig *cfg);
204 
205 static inline EVTTAG *
cfg_format_version_tag(const gchar * tag_name,gint version)206 cfg_format_version_tag(const gchar *tag_name, gint version)
207 {
208   return evt_tag_printf(tag_name, "%d.%d", (version & 0xFF00) >> 8, version & 0xFF);
209 }
210 
211 static inline EVTTAG *
cfg_format_config_version_tag(GlobalConfig * self)212 cfg_format_config_version_tag(GlobalConfig *self)
213 {
214   return cfg_format_version_tag("config-version", self->user_version);
215 }
216 
217 
218 #endif
219