1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
5    Copyright (C) 2011-2012 Planets Communications B.V.
6    Copyright (C) 2013-2018 Bareos GmbH & Co. KG
7 
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12 
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    Affero General Public License for more details.
17 
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22 */
23 /*
24  * Kern Sibbald, January MM
25  */
26 #ifndef BAREOS_CORE_SRC_LIB_PARSE_CONF_H_
27 #define BAREOS_CORE_SRC_LIB_PARSE_CONF_H_
28 
29 
30 #include "include/bareos.h"
31 #include "include/bc_types.h"
32 #include "lib/parse_conf_callbacks.h"
33 #include "lib/s_password.h"
34 #include "lib/tls_conf.h"
35 #include "lib/parse_conf.h"
36 #include "lib/keyword_table_s.h"
37 #include "lib/message_destination_info.h"
38 
39 #include <functional>
40 #include <memory>
41 
42 struct ResourceItem;
43 class ConfigParserStateMachine;
44 class ConfigurationParser;
45 
46 /* For storing name_addr items in res_items table */
47 /* clang-format off */
48 #define ITEM(c, m) ((std::size_t)&c->m), reinterpret_cast<BareosResource**>(&c)
49 #define ITEMC(c)   0,   reinterpret_cast<BareosResource**>(&c)
50 /* clang-format on */
51 /*
52  * Master Resource configuration structure definition
53  * This is the structure that defines the resources that are available to
54  * this daemon.
55  */
56 struct ResourceTable {
57   const char* name;    /* Resource name */
58   ResourceItem* items; /* List of resource keywords */
59   uint32_t rcode;      /* Code if needed */
60   uint32_t size;       /* Size of resource */
61 
62   std::function<void()> ResourceSpecificInitializer; /* this allocates memory */
63   BareosResource** allocated_resource_;
64 };
65 
66 /*
67  * Common Resource definitions
68  */
69 #define MAX_RES_NAME_LENGTH \
70   (MAX_NAME_LENGTH - 1) /* maximum resource name length */
71 
72 /*
73  * Config item flags.
74  */
75 #define CFG_ITEM_REQUIRED 0x1   /* Item required */
76 #define CFG_ITEM_DEFAULT 0x2    /* Default supplied */
77 #define CFG_ITEM_NO_EQUALS 0x4  /* Don't scan = after name */
78 #define CFG_ITEM_DEPRECATED 0x8 /* Deprecated config option */
79 #define CFG_ITEM_ALIAS 0x10     /* Item is an alias for another */
80 
81 /*
82  * CFG_ITEM_DEFAULT_PLATFORM_SPECIFIC: the value may differ between different
83  * platforms (or configure settings). This information is used for the
84  * documentation.
85  */
86 #define CFG_ITEM_PLATFORM_SPECIFIC 0x20
87 
88 enum
89 {
90   /*
91    * Standard resource types. handlers in res.c
92    */
93   CFG_TYPE_STR = 1,           /* String */
94   CFG_TYPE_DIR = 2,           /* Directory */
95   CFG_TYPE_MD5PASSWORD = 3,   /* MD5 hashed Password */
96   CFG_TYPE_CLEARPASSWORD = 4, /* Clear text Password */
97   CFG_TYPE_AUTOPASSWORD =
98       5,             /* Password stored in clear when needed otherwise hashed */
99   CFG_TYPE_NAME = 6, /* Name */
100   CFG_TYPE_STRNAME = 7,             /* String Name */
101   CFG_TYPE_RES = 8,                 /* Resource */
102   CFG_TYPE_ALIST_RES = 9,           /* List of resources */
103   CFG_TYPE_ALIST_STR = 10,          /* List of strings */
104   CFG_TYPE_ALIST_DIR = 11,          /* List of dirs */
105   CFG_TYPE_INT16 = 12,              /* 16 bits Integer */
106   CFG_TYPE_PINT16 = 13,             /* Positive 16 bits Integer (unsigned) */
107   CFG_TYPE_INT32 = 14,              /* 32 bits Integer */
108   CFG_TYPE_PINT32 = 15,             /* Positive 32 bits Integer (unsigned) */
109   CFG_TYPE_MSGS = 16,               /* Message resource */
110   CFG_TYPE_INT64 = 17,              /* 64 bits Integer */
111   CFG_TYPE_BIT = 18,                /* Bitfield */
112   CFG_TYPE_BOOL = 19,               /* Boolean */
113   CFG_TYPE_TIME = 20,               /* Time value */
114   CFG_TYPE_SIZE64 = 21,             /* 64 bits file size */
115   CFG_TYPE_SIZE32 = 22,             /* 32 bits file size */
116   CFG_TYPE_SPEED = 23,              /* Speed limit */
117   CFG_TYPE_DEFS = 24,               /* Definition */
118   CFG_TYPE_LABEL = 25,              /* Label */
119   CFG_TYPE_ADDRESSES = 26,          /* List of ip addresses */
120   CFG_TYPE_ADDRESSES_ADDRESS = 27,  /* Ip address */
121   CFG_TYPE_ADDRESSES_PORT = 28,     /* Ip port */
122   CFG_TYPE_PLUGIN_NAMES = 29,       /* Plugin Name(s) */
123   CFG_TYPE_STDSTR = 30,             /* String as std::string */
124   CFG_TYPE_STDSTRDIR = 31,          /* Directory as std::string */
125   CFG_TYPE_STR_VECTOR = 32,         /* std::vector<std::string> of any string */
126   CFG_TYPE_STR_VECTOR_OF_DIRS = 33, /* std::vector<std::string> of directories*/
127 
128   /*
129    * Director resource types. handlers in dird_conf.
130    */
131   CFG_TYPE_ACL = 50,              /* User Access Control List */
132   CFG_TYPE_AUDIT = 51,            /* Auditing Command List */
133   CFG_TYPE_AUTHPROTOCOLTYPE = 52, /* Authentication Protocol */
134   CFG_TYPE_AUTHTYPE = 53,         /* Authentication Type */
135   CFG_TYPE_DEVICE = 54,           /* Device resource */
136   CFG_TYPE_JOBTYPE = 55,          /* Type of Job */
137   CFG_TYPE_PROTOCOLTYPE = 56,     /* Protocol */
138   CFG_TYPE_LEVEL = 57,            /* Backup Level */
139   CFG_TYPE_REPLACE = 58,          /* Replace option */
140   CFG_TYPE_SHRTRUNSCRIPT = 59,    /* Short Runscript definition */
141   CFG_TYPE_RUNSCRIPT = 60,        /* Runscript */
142   CFG_TYPE_RUNSCRIPT_CMD = 61,    /* Runscript Command */
143   CFG_TYPE_RUNSCRIPT_TARGET = 62, /* Runscript Target (Host) */
144   CFG_TYPE_RUNSCRIPT_BOOL = 63,   /* Runscript Boolean */
145   CFG_TYPE_RUNSCRIPT_WHEN = 64,   /* Runscript When expression */
146   CFG_TYPE_MIGTYPE = 65,          /* Migration Type */
147   CFG_TYPE_INCEXC = 66,           /* Include/Exclude item */
148   CFG_TYPE_RUN = 67,              /* Schedule Run Command */
149   CFG_TYPE_ACTIONONPURGE = 68,    /* Action to perform on Purge */
150   CFG_TYPE_POOLTYPE = 69,         /* Pool Type */
151 
152   /*
153    * Director fileset options. handlers in dird_conf.
154    */
155   CFG_TYPE_FNAME = 80,      /* Filename */
156   CFG_TYPE_PLUGINNAME = 81, /* Pluginname */
157   CFG_TYPE_EXCLUDEDIR = 82, /* Exclude directory */
158   CFG_TYPE_OPTIONS = 83,    /* Options block */
159   CFG_TYPE_OPTION = 84,     /* Option of Options block */
160   CFG_TYPE_REGEX = 85,      /* Regular Expression */
161   CFG_TYPE_BASE = 86,       /* Basejob Expression */
162   CFG_TYPE_WILD = 87,       /* Wildcard Expression */
163   CFG_TYPE_PLUGIN = 88,     /* Plugin definition */
164   CFG_TYPE_FSTYPE = 89,     /* FileSytem match criterium (UNIX)*/
165   CFG_TYPE_DRIVETYPE = 90,  /* DriveType match criterium (Windows) */
166   CFG_TYPE_META = 91,       /* Meta tag */
167 
168   /*
169    * Storage daemon resource types
170    */
171   CFG_TYPE_DEVTYPE = 201,      /* Device Type */
172   CFG_TYPE_MAXBLOCKSIZE = 202, /* Maximum Blocksize */
173   CFG_TYPE_IODIRECTION = 203,  /* IO Direction */
174   CFG_TYPE_CMPRSALGO = 204,    /* Compression Algorithm */
175 
176   /*
177    * File daemon resource types
178    */
179   CFG_TYPE_CIPHER = 301 /* Encryption Cipher */
180 };
181 
182 struct DatatypeName {
183   const int number;
184   const char* name;
185   const char* description;
186 };
187 
188 typedef void(INIT_RES_HANDLER)(ResourceItem* item, int pass);
189 typedef void(STORE_RES_HANDLER)(LEX* lc,
190                                 ResourceItem* item,
191                                 int index,
192                                 int pass);
193 typedef void(PRINT_RES_HANDLER)(ResourceItem* items,
194                                 int i,
195                                 PoolMem& cfg_str,
196                                 bool hide_sensitive_data,
197                                 bool inherited);
198 
199 class QualifiedResourceNameTypeConverter;
200 
201 class ConfigurationParser {
202   friend class ConfiguredTlsPolicyGetterPrivate;
203   friend class ConfigParserStateMachine;
204 
205  public:
206   std::string cf_;                    /* Config file parameter */
207   LEX_ERROR_HANDLER* scan_error_;     /* Error handler if non-null */
208   LEX_WARNING_HANDLER* scan_warning_; /* Warning handler if non-null */
209   INIT_RES_HANDLER*
210       init_res_; /* Init resource handler for non default types if non-null */
211   STORE_RES_HANDLER*
212       store_res_; /* Store resource handler for non default types if non-null */
213   PRINT_RES_HANDLER*
214       print_res_; /* Print resource handler for non default types if non-null */
215 
216   int32_t err_type_; /* The way to Terminate on failure */
217   // void* res_all_;        /* Pointer to res_all buffer */
218   // int32_t res_all_size_; /* Length of buffer */
219   bool omit_defaults_; /* Omit config variables with default values when dumping
220                           the config */
221 
222   int32_t r_first_;              /* First daemon resource type */
223   int32_t r_last_;               /* Last daemon resource type */
224   int32_t r_own_;                /* own resource type */
225   BareosResource* own_resource_; /* Pointer to own resource */
226   ResourceTable* resources_;     /* Pointer to table of permitted resources */
227   BareosResource** res_head_;    /* Pointer to defined resources */
228   mutable brwlock_t res_lock_;   /* Resource lock */
229 
230   SaveResourceCb_t SaveResourceCb_;
231   DumpResourceCb_t DumpResourceCb_;
232   FreeResourceCb_t FreeResourceCb_;
233 
234   ConfigurationParser();
235   ConfigurationParser(const char* cf,
236                       LEX_ERROR_HANDLER* ScanError,
237                       LEX_WARNING_HANDLER* scan_warning,
238                       INIT_RES_HANDLER* init_res,
239                       STORE_RES_HANDLER* StoreRes,
240                       PRINT_RES_HANDLER* print_res,
241                       int32_t err_type,
242                       int32_t r_first,
243                       int32_t r_last,
244                       ResourceTable* resources,
245                       BareosResource** res_head,
246                       const char* config_default_filename,
247                       const char* config_include_dir,
248                       void (*ParseConfigBeforeCb)(ConfigurationParser&),
249                       void (*ParseConfigReadyCb)(ConfigurationParser&),
250                       SaveResourceCb_t SaveResourceCb,
251                       DumpResourceCb_t DumpResourceCb,
252                       FreeResourceCb_t FreeResourceCb);
253 
254   ~ConfigurationParser();
255 
IsUsingConfigIncludeDir()256   bool IsUsingConfigIncludeDir() const { return use_config_include_dir_; }
257   bool ParseConfig();
258   bool ParseConfigFile(const char* config_file_name,
259                        void* caller_ctx,
260                        LEX_ERROR_HANDLER* scan_error = nullptr,
261                        LEX_WARNING_HANDLER* scan_warning = nullptr);
get_base_config_path()262   const std::string& get_base_config_path() const { return used_config_path_; }
263   void FreeResources();
264   BareosResource** SaveResources();
265   void InitResource(int rcode,
266                     ResourceItem items[],
267                     int pass,
268                     std::function<void()> ResourceSpecificInitializer);
269   bool AppendToResourcesChain(BareosResource* new_resource, int rcode);
270   bool RemoveResource(int rcode, const char* name);
271   void DumpResources(void sendit(void* sock, const char* fmt, ...),
272                      void* sock,
273                      bool hide_sensitive_data = false);
274   int GetResourceCode(const char* resource_type);
275   ResourceTable* GetResourceTable(int resource_type);
276   ResourceTable* GetResourceTable(const char* resource_type_name);
277   int GetResourceItemIndex(ResourceItem* res_table, const char* item);
278   ResourceItem* GetResourceItem(ResourceItem* res_table, const char* item);
279   bool GetPathOfResource(PoolMem& path,
280                          const char* component,
281                          const char* resourcetype,
282                          const char* name,
283                          bool set_wildcards = false);
284   bool GetPathOfNewResource(PoolMem& path,
285                             PoolMem& extramsg,
286                             const char* component,
287                             const char* resourcetype,
288                             const char* name,
289                             bool error_if_exits = false,
290                             bool create_directories = false);
291   BareosResource* GetNextRes(int rcode, BareosResource* res) const;
292   BareosResource* GetResWithName(int rcode,
293                                  const char* name,
294                                  bool lock = true) const;
295   void b_LockRes(const char* file, int line) const;
296   void b_UnlockRes(const char* file, int line) const;
297   const char* ResToStr(int rcode) const;
298   bool StoreResource(int rcode,
299                      LEX* lc,
300                      ResourceItem* item,
301                      int index,
302                      int pass);
303   void InitializeQualifiedResourceNameTypeConverter(
304       const std::map<int, std::string>&);
GetQualifiedResourceNameTypeConverter()305   QualifiedResourceNameTypeConverter* GetQualifiedResourceNameTypeConverter()
306       const
307   {
308     return qualified_resource_name_type_converter_.get();
309   }
310   static bool GetTlsPskByFullyQualifiedResourceName(
311       ConfigurationParser* config,
312       const char* fully_qualified_name,
313       std::string& psk);
314   bool GetConfiguredTlsPolicyFromCleartextHello(
315       const std::string& r_code,
316       const std::string& name,
317       TlsPolicy& tls_policy_out) const;
318   std::string CreateOwnQualifiedNameForNetworkDump() const;
319 
320  private:
321   ConfigurationParser(const ConfigurationParser&) = delete;
322   ConfigurationParser operator=(const ConfigurationParser&) = delete;
323 
324  private:
325   enum unit_type
326   {
327     STORE_SIZE,
328     STORE_SPEED
329   };
330 
331   std::string config_default_filename_; /* default config filename, that is
332                                            used, if no filename is given */
333   std::string config_dir_; /* base directory of configuration files */
334   std::string
335       config_include_dir_;      /* rel. path to the config include directory
336                                     (bareos-dir.d, bareos-sd.d, bareos-fd.d, ...) */
337   bool use_config_include_dir_; /* Use the config include directory */
338   std::string config_include_naming_format_; /* Format string for file paths of
339                                                 resources */
340   std::string used_config_path_;             /* Config file that is used. */
341   std::unique_ptr<QualifiedResourceNameTypeConverter>
342       qualified_resource_name_type_converter_;
343   ParseConfigBeforeCb_t ParseConfigBeforeCb_;
344   ParseConfigReadyCb_t ParseConfigReadyCb_;
345   bool parser_first_run_;
346 
347 
348   const char* GetDefaultConfigDir();
349   bool GetConfigFile(PoolMem& full_path,
350                      const char* config_dir,
351                      const char* config_filename);
352   bool GetConfigIncludePath(PoolMem& full_path, const char* config_dir);
353   bool FindConfigPath(PoolMem& full_path);
354   int GetResourceTableIndex(int resource_type);
355   void StoreMsgs(LEX* lc, ResourceItem* item, int index, int pass);
356   void StoreName(LEX* lc, ResourceItem* item, int index, int pass);
357   void StoreStrname(LEX* lc, ResourceItem* item, int index, int pass);
358   void StoreStr(LEX* lc, ResourceItem* item, int index, int pass);
359   void StoreStdstr(LEX* lc, ResourceItem* item, int index, int pass);
360   void StoreDir(LEX* lc, ResourceItem* item, int index, int pass);
361   void StoreStdstrdir(LEX* lc, ResourceItem* item, int index, int pass);
362   void StoreMd5Password(LEX* lc, ResourceItem* item, int index, int pass);
363   void StoreClearpassword(LEX* lc, ResourceItem* item, int index, int pass);
364   void StoreRes(LEX* lc, ResourceItem* item, int index, int pass);
365   void StoreAlistRes(LEX* lc, ResourceItem* item, int index, int pass);
366   void StoreAlistStr(LEX* lc, ResourceItem* item, int index, int pass);
367   void StoreStdVectorStr(LEX* lc, ResourceItem* item, int index, int pass);
368   void StoreAlistDir(LEX* lc, ResourceItem* item, int index, int pass);
369   void StorePluginNames(LEX* lc, ResourceItem* item, int index, int pass);
370   void StoreDefs(LEX* lc, ResourceItem* item, int index, int pass);
371   void store_int16(LEX* lc, ResourceItem* item, int index, int pass);
372   void store_int32(LEX* lc, ResourceItem* item, int index, int pass);
373   void store_pint16(LEX* lc, ResourceItem* item, int index, int pass);
374   void store_pint32(LEX* lc, ResourceItem* item, int index, int pass);
375   void store_int64(LEX* lc, ResourceItem* item, int index, int pass);
376   void store_int_unit(LEX* lc,
377                       ResourceItem* item,
378                       int index,
379                       int pass,
380                       bool size32,
381                       enum unit_type type);
382   void store_size32(LEX* lc, ResourceItem* item, int index, int pass);
383   void store_size64(LEX* lc, ResourceItem* item, int index, int pass);
384   void StoreSpeed(LEX* lc, ResourceItem* item, int index, int pass);
385   void StoreTime(LEX* lc, ResourceItem* item, int index, int pass);
386   void StoreBit(LEX* lc, ResourceItem* item, int index, int pass);
387   void StoreBool(LEX* lc, ResourceItem* item, int index, int pass);
388   void StoreLabel(LEX* lc, ResourceItem* item, int index, int pass);
389   void StoreAddresses(LEX* lc, ResourceItem* item, int index, int pass);
390   void StoreAddressesAddress(LEX* lc, ResourceItem* item, int index, int pass);
391   void StoreAddressesPort(LEX* lc, ResourceItem* item, int index, int pass);
392   void ScanTypes(LEX* lc,
393                  MessagesResource* msg,
394                  MessageDestinationCode dest_code,
395                  const std::string& where,
396                  const std::string& cmd,
397                  const std::string& timestamp_format);
398   void lex_error(const char* cf,
399                  LEX_ERROR_HANDLER* ScanError,
400                  LEX_WARNING_HANDLER* scan_warning) const;
401   void SetAllResourceDefaultsByParserPass(int rcode,
402                                           ResourceItem items[],
403                                           int pass);
404   void SetAllResourceDefaultsIterateOverItems(
405       int rcode,
406       ResourceItem items[],
407       std::function<void(ConfigurationParser&, ResourceItem*)> SetDefaults);
408   void SetResourceDefaultsParserPass1(ResourceItem* item);
409   void SetResourceDefaultsParserPass2(ResourceItem* item);
410 };
411 
412 void PrintMessage(void* sock, const char* fmt, ...);
413 bool IsTlsConfigured(TlsResource* tls_resource);
414 
415 /*
416  * Data type routines
417  */
418 DatatypeName* GetDatatype(int number);
419 const char* DatatypeToString(int type);
420 const char* DatatypeToDescription(int type);
421 
422 /*
423  * Resource routines
424  */
425 void IndentConfigItem(PoolMem& cfg_str,
426                       int level,
427                       const char* config_item,
428                       bool inherited = false);
429 
430 /*
431  * Loop through each resource of type, returning in var
432  */
433 #ifdef HAVE_TYPEOF
434 #define foreach_res(var, type)                                    \
435   for ((var) = NULL; ((var) = (typeof(var))my_config->GetNextRes( \
436                           (type), (BareosResource*)var));)
437 #else
438 #define foreach_res(var, type)                                        \
439   for (var = NULL; (*((void**)&(var)) = (void*)my_config->GetNextRes( \
440                         (type), (BareosResource*)var));)
441 #endif
442 
443 #define LockRes(x) (x)->b_LockRes(__FILE__, __LINE__)
444 #define UnlockRes(x) (x)->b_UnlockRes(__FILE__, __LINE__)
445 
446 #endif  // BAREOS_CORE_SRC_LIB_PARSE_CONF_H_
447