-
Notifications
You must be signed in to change notification settings - Fork 256
/
parse_conf.cc
722 lines (633 loc) · 21.2 KB
/
parse_conf.cc
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program 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
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Master Configuration routines.
*
* This file contains the common parts of the BAREOS configuration routines.
*
* Note, the configuration file parser consists of four parts
*
* 1. The generic lexical scanner in lib/lex.c and lib/lex.h
*
* 2. The generic config scanner in lib/parse_conf.c and lib/parse_conf.h.
* These files contain the parser code, some utility routines,
*
* 3. The generic resource functions in lib/res.c
* Which form the common store routines (name, int, string, time,
* int64, size, ...).
*
* 4. The daemon specific file, which contains the Resource definitions
* as well as any specific store routines for the resource records.
*
* N.B. This is a two pass parser, so if you malloc() a string in a "store"
* routine, you must ensure to do it during only one of the two passes, or to
* free it between.
*
* Also, note that the resource record is malloced and saved in SaveResource()
* during pass 1. Anything that you want saved after pass two (e.g. resource
* pointers) must explicitly be done in SaveResource. Take a look at the Job
* resource in src/dird/dird_conf.c to see how it is done.
*
* Kern Sibbald, January MM
*/
#include "include/bareos.h"
#include "include/jcr.h"
#include "lib/address_conf.h"
#include "lib/edit.h"
#include "lib/parse_conf.h"
#include "lib/parse_conf_state_machine.h"
#include "lib/qualified_resource_name_type_converter.h"
#include "lib/bstringlist.h"
#include "lib/ascii_control_characters.h"
#include "lib/messages_resource.h"
#include "lib/resource_item.h"
#include "lib/berrno.h"
#include "lib/util.h"
#include <algorithm>
#if defined(HAVE_WIN32)
# include "shlobj.h"
#else
# define MAX_PATH 1024
#endif
bool PrintMessage(void* sock, const char* fmt, ...)
{
va_list arg_ptr;
va_start(arg_ptr, fmt);
vfprintf(stdout, fmt, arg_ptr);
va_end(arg_ptr);
return true;
}
ConfigurationParser::ConfigurationParser()
: scan_error_(nullptr)
, scan_warning_(nullptr)
, init_res_(nullptr)
, store_res_(nullptr)
, print_res_(nullptr)
, err_type_(0)
, omit_defaults_(false)
, r_first_(0)
, r_last_(0)
, r_own_(0)
, own_resource_(nullptr)
, resources_(0)
, res_head_(nullptr)
, SaveResourceCb_(nullptr)
, DumpResourceCb_(nullptr)
, FreeResourceCb_(nullptr)
, use_config_include_dir_(false)
, ParseConfigReadyCb_(nullptr)
, parser_first_run_(true)
{
return;
}
ConfigurationParser::ConfigurationParser(
const char* cf,
LEX_ERROR_HANDLER* ScanError,
LEX_WARNING_HANDLER* scan_warning,
INIT_RES_HANDLER* init_res,
STORE_RES_HANDLER* StoreRes,
PRINT_RES_HANDLER* print_res,
int32_t err_type,
int32_t r_first,
int32_t r_last,
ResourceTable* resources,
BareosResource** res_head,
const char* config_default_filename,
const char* config_include_dir,
void (*ParseConfigBeforeCb)(ConfigurationParser&),
void (*ParseConfigReadyCb)(ConfigurationParser&),
SaveResourceCb_t SaveResourceCb,
DumpResourceCb_t DumpResourceCb,
FreeResourceCb_t FreeResourceCb)
: ConfigurationParser()
{
cf_ = cf == nullptr ? "" : cf;
use_config_include_dir_ = false;
config_include_naming_format_ = "%s/%s/%s.conf";
scan_error_ = ScanError;
scan_warning_ = scan_warning;
init_res_ = init_res;
store_res_ = StoreRes;
print_res_ = print_res;
err_type_ = err_type;
r_first_ = r_first;
r_last_ = r_last;
resources_ = resources;
res_head_ = res_head;
config_default_filename_
= config_default_filename == nullptr ? "" : config_default_filename;
config_include_dir_ = config_include_dir == nullptr ? "" : config_include_dir;
ParseConfigBeforeCb_ = ParseConfigBeforeCb;
ParseConfigReadyCb_ = ParseConfigReadyCb;
ASSERT(SaveResourceCb);
ASSERT(DumpResourceCb);
ASSERT(FreeResourceCb);
SaveResourceCb_ = SaveResourceCb;
DumpResourceCb_ = DumpResourceCb;
FreeResourceCb_ = FreeResourceCb;
}
ConfigurationParser::~ConfigurationParser()
{
if (res_head_) {
for (int i = r_first_; i <= r_last_; i++) {
if (res_head_[i - r_first_]) {
FreeResourceCb_(res_head_[i - r_first_], i);
}
res_head_[i - r_first_] = nullptr;
}
}
}
void ConfigurationParser::InitializeQualifiedResourceNameTypeConverter(
const std::map<int, std::string>& map)
{
qualified_resource_name_type_converter_.reset(
new QualifiedResourceNameTypeConverter(map));
}
std::string ConfigurationParser::CreateOwnQualifiedNameForNetworkDump() const
{
std::string qualified_name;
if (own_resource_ && qualified_resource_name_type_converter_) {
if (qualified_resource_name_type_converter_->ResourceToString(
own_resource_->resource_name_, own_resource_->rcode_,
"::", qualified_name)) {
return qualified_name;
}
}
return qualified_name;
}
bool ConfigurationParser::ParseConfig()
{
int errstat;
PoolMem config_path;
if (ParseConfigBeforeCb_) ParseConfigBeforeCb_(*this);
if (parser_first_run_ && (errstat = RwlInit(&res_lock_)) != 0) {
BErrNo be;
Jmsg1(nullptr, M_ABORT, 0,
_("Unable to initialize resource lock. ERR=%s\n"),
be.bstrerror(errstat));
}
parser_first_run_ = false;
if (!FindConfigPath(config_path)) {
Jmsg0(nullptr, M_ERROR_TERM, 0, _("Failed to find config filename.\n"));
}
used_config_path_ = config_path.c_str();
Dmsg1(100, "config file = %s\n", used_config_path_.c_str());
bool success = ParseConfigFile(config_path.c_str(), nullptr, scan_error_,
scan_warning_);
if (success && ParseConfigReadyCb_) { ParseConfigReadyCb_(*this); }
return success;
}
void ConfigurationParser::lex_error(const char* cf,
LEX_ERROR_HANDLER* ScanError,
LEX_WARNING_HANDLER* scan_warning) const
{
// We must create a lex packet to print the error
LEX* lexical_parser_ = (LEX*)malloc(sizeof(LEX));
memset(lexical_parser_, 0, sizeof(LEX));
if (ScanError) {
lexical_parser_->ScanError = ScanError;
} else {
LexSetDefaultErrorHandler(lexical_parser_);
}
if (scan_warning) {
lexical_parser_->scan_warning = scan_warning;
} else {
LexSetDefaultWarningHandler(lexical_parser_);
}
LexSetErrorHandlerErrorType(lexical_parser_, err_type_);
BErrNo be;
scan_err2(lexical_parser_, _("Cannot open config file \"%s\": %s\n"), cf,
be.bstrerror());
free(lexical_parser_);
}
bool ConfigurationParser::ParseConfigFile(const char* config_file_name,
void* caller_ctx,
LEX_ERROR_HANDLER* scan_error,
LEX_WARNING_HANDLER* scan_warning)
{
ConfigParserStateMachine state_machine(config_file_name, caller_ctx,
scan_error, scan_warning, *this);
Dmsg1(900, "Enter ParseConfigFile(%s)\n", config_file_name);
do {
if (!state_machine.InitParserPass()) { return false; }
if (!state_machine.ParseAllTokens()) {
scan_err0(state_machine.lexical_parser_, _("ParseAllTokens failed."));
return false;
}
switch (state_machine.GetParseError()) {
case ConfigParserStateMachine::ParserError::kResourceIncomplete:
scan_err0(state_machine.lexical_parser_,
_("End of conf file reached with unclosed resource."));
return false;
case ConfigParserStateMachine::ParserError::kParserError:
scan_err0(state_machine.lexical_parser_, _("Parser Error occurred."));
return false;
case ConfigParserStateMachine::ParserError::kNoError:
break;
}
} while (!state_machine.Finished());
state_machine.DumpResourcesAfterSecondPass();
Dmsg0(900, "Leave ParseConfigFile()\n");
return true;
}
bool ConfigurationParser::AppendToResourcesChain(BareosResource* new_resource,
int rcode)
{
int rindex = rcode - r_first_;
if (!new_resource->resource_name_) {
Emsg1(M_ERROR, 0,
_("Name item is required in %s resource, but not found.\n"),
resources_[rindex].name);
return false;
}
if (!res_head_[rindex]) {
res_head_[rindex] = new_resource;
Dmsg3(900, "Inserting first %s res: %s index=%d\n", ResToStr(rcode),
new_resource->resource_name_, rindex);
} else { // append
BareosResource* last = nullptr;
BareosResource* current = res_head_[rindex];
do {
if (bstrcmp(current->resource_name_, new_resource->resource_name_)) {
Emsg2(M_ERROR, 0,
_("Attempt to define second %s resource named \"%s\" is not "
"permitted.\n"),
resources_[rindex].name, new_resource->resource_name_);
return false;
}
last = current;
current = last->next_;
} while (current);
last->next_ = new_resource;
Dmsg3(900, _("Inserting %s res: %s index=%d\n"), ResToStr(rcode),
new_resource->resource_name_, rindex);
}
return true;
}
int ConfigurationParser::GetResourceTableIndex(const char* resource_type_name)
{
for (int i = 0; resources_[i].name; i++) {
if (Bstrcasecmp(resources_[i].name, resource_type_name)) { return i; }
}
return -1;
}
int ConfigurationParser::GetResourceCode(const char* resource_type_name)
{
for (int i = 0; resources_[i].name; i++) {
if (Bstrcasecmp(resources_[i].name, resource_type_name)) {
return resources_[i].rcode;
}
}
return 0;
}
ResourceTable* ConfigurationParser::GetResourceTable(
const char* resource_type_name)
{
int res_table_index = GetResourceTableIndex(resource_type_name);
return &resources_[res_table_index];
}
int ConfigurationParser::GetResourceItemIndex(ResourceItem* resource_items_,
const char* item)
{
int result = -1;
int i;
for (i = 0; resource_items_[i].name; i++) {
if (Bstrcasecmp(resource_items_[i].name, item)) {
result = i;
break;
}
}
return result;
}
ResourceItem* ConfigurationParser::GetResourceItem(
ResourceItem* resource_items_,
const char* item)
{
ResourceItem* result = nullptr;
int i = -1;
if (resource_items_) {
i = GetResourceItemIndex(resource_items_, item);
if (i >= 0) { result = &resource_items_[i]; }
}
return result;
}
const char* ConfigurationParser::GetDefaultConfigDir()
{
#if defined(HAVE_WIN32)
HRESULT hr;
static char szConfigDir[MAX_PATH + 1] = {0};
if (!p_SHGetFolderPath) {
bstrncpy(szConfigDir, DEFAULT_CONFIGDIR, sizeof(szConfigDir));
return szConfigDir;
}
if (szConfigDir[0] == '\0') {
hr = p_SHGetFolderPath(nullptr, CSIDL_COMMON_APPDATA, nullptr, 0,
szConfigDir);
if (SUCCEEDED(hr)) {
bstrncat(szConfigDir, "\\Bareos", sizeof(szConfigDir));
} else {
bstrncpy(szConfigDir, DEFAULT_CONFIGDIR, sizeof(szConfigDir));
}
}
return szConfigDir;
#else
return CONFDIR;
#endif
}
bool ConfigurationParser::GetConfigFile(PoolMem& full_path,
const char* config_dir,
const char* config_filename)
{
bool found = false;
if (!PathIsDirectory(config_dir)) { return false; }
if (config_filename) {
full_path.strcpy(config_dir);
if (PathAppend(full_path, config_filename)) {
if (PathExists(full_path)) {
config_dir_ = config_dir;
found = true;
}
}
}
return found;
}
bool ConfigurationParser::GetConfigIncludePath(PoolMem& full_path,
const char* config_dir)
{
bool found = false;
if (!config_include_dir_.empty()) {
/*
* Set full_path to the initial part of the include path,
* so it can be used as result, even on errors.
* On success, full_path will be overwritten with the full path.
*/
full_path.strcpy(config_dir);
PathAppend(full_path, config_include_dir_.c_str());
if (PathIsDirectory(full_path)) {
config_dir_ = config_dir;
// Set full_path to wildcard path.
if (GetPathOfResource(full_path, nullptr, nullptr, nullptr, true)) {
use_config_include_dir_ = true;
found = true;
}
}
}
return found;
}
/*
* Returns false on error
* true on OK, with full_path set to where config file should be
*/
bool ConfigurationParser::FindConfigPath(PoolMem& full_path)
{
bool found = false;
PoolMem config_dir;
PoolMem config_path_file;
if (cf_.empty()) {
// No path is given, so use the defaults.
found = GetConfigFile(full_path, GetDefaultConfigDir(),
config_default_filename_.c_str());
if (!found) {
config_path_file.strcpy(full_path);
found = GetConfigIncludePath(full_path, GetDefaultConfigDir());
}
if (!found) {
Jmsg2(nullptr, M_ERROR, 0,
_("Failed to read config file at the default locations "
"\"%s\" (config file path) and \"%s\" (config include "
"directory).\n"),
config_path_file.c_str(), full_path.c_str());
}
} else if (PathExists(cf_.c_str())) {
// Path is given and exists.
if (PathIsDirectory(cf_.c_str())) {
found = GetConfigFile(full_path, cf_.c_str(),
config_default_filename_.c_str());
if (!found) {
config_path_file.strcpy(full_path);
found = GetConfigIncludePath(full_path, cf_.c_str());
}
if (!found) {
Jmsg3(nullptr, M_ERROR, 0,
_("Failed to find configuration files under directory \"%s\". "
"Did look for \"%s\" (config file path) and \"%s\" (config "
"include directory).\n"),
cf_.c_str(), config_path_file.c_str(), full_path.c_str());
}
} else {
full_path.strcpy(cf_.c_str());
PathGetDirectory(config_dir, full_path);
config_dir_ = config_dir.c_str();
found = true;
}
} else if (config_default_filename_.empty()) {
/*
* Compatibility with older versions.
* If config_default_filename_ is not set,
* cf_ may contain what is expected in config_default_filename_.
*/
found = GetConfigFile(full_path, GetDefaultConfigDir(), cf_.c_str());
if (!found) {
Jmsg2(nullptr, M_ERROR, 0,
_("Failed to find configuration files at \"%s\" and \"%s\".\n"),
cf_.c_str(), full_path.c_str());
}
} else {
Jmsg1(nullptr, M_ERROR, 0, _("Failed to read config file \"%s\"\n"),
cf_.c_str());
}
return found;
}
BareosResource** ConfigurationParser::SaveResources()
{
int num = r_last_ - r_first_ + 1;
BareosResource** res
= (BareosResource**)malloc(num * sizeof(BareosResource*));
for (int i = 0; i < num; i++) {
res[i] = res_head_[i];
res_head_[i] = nullptr;
}
return res;
}
bool ConfigurationParser::RemoveResource(int rcode, const char* name)
{
int rindex = rcode - r_first_;
BareosResource* last;
/*
* Remove resource from list.
*
* Note: this is intended for removing a resource that has just been added,
* but proven to be incorrect (added by console command "configure add").
* For a general approach, a check if this resource is referenced by other
* resources must be added. If it is referenced, don't remove it.
*/
last = nullptr;
for (BareosResource* res = res_head_[rindex]; res; res = res->next_) {
if (bstrcmp(res->resource_name_, name)) {
if (!last) {
Dmsg2(900,
_("removing resource %s, name=%s (first resource in list)\n"),
ResToStr(rcode), name);
res_head_[rindex] = res->next_;
} else {
Dmsg2(900, _("removing resource %s, name=%s\n"), ResToStr(rcode), name);
last->next_ = res->next_;
}
res->next_ = nullptr;
FreeResourceCb_(res, rcode);
return true;
}
last = res;
}
// Resource with this name not found
return false;
}
bool ConfigurationParser::DumpResources(bool sendit(void* sock,
const char* fmt,
...),
void* sock,
const std::string& res_type_name,
const std::string& res_name,
bool hide_sensitive_data)
{
bool result = false;
if (res_type_name.empty()) {
DumpResources(sendit, sock, hide_sensitive_data);
result = true;
} else {
int res_type = GetResourceCode(res_type_name.c_str());
if (res_type > 0) {
BareosResource* res = nullptr;
if (res_name.empty()) {
// No name, dump all resources of specified type
res = GetNextRes(res_type, nullptr);
} else {
// Dump a single resource with specified name
res = GetResWithName(res_type, res_name.c_str());
res_type = -res_type;
}
if (res) { result = true; }
DumpResourceCb_(res_type, res, sendit, sock, hide_sensitive_data, false);
}
}
return result;
}
void ConfigurationParser::DumpResources(bool sendit(void* sock,
const char* fmt,
...),
void* sock,
bool hide_sensitive_data)
{
for (int i = r_first_; i <= r_last_; i++) {
if (res_head_[i - r_first_]) {
DumpResourceCb_(i, res_head_[i - r_first_], sendit, sock,
hide_sensitive_data, false);
}
}
}
bool ConfigurationParser::GetPathOfResource(PoolMem& path,
const char* component,
const char* resourcetype,
const char* name,
bool set_wildcards)
{
PoolMem rel_path(PM_FNAME);
PoolMem directory(PM_FNAME);
PoolMem resourcetype_lowercase(resourcetype);
resourcetype_lowercase.toLower();
if (!component) {
if (!config_include_dir_.empty()) {
component = config_include_dir_.c_str();
} else {
return false;
}
}
if (resourcetype_lowercase.strlen() <= 0) {
if (set_wildcards) {
resourcetype_lowercase.strcpy("*");
} else {
return false;
}
}
if (!name) {
if (set_wildcards) {
name = "*";
} else {
return false;
}
}
path.strcpy(config_dir_.c_str());
rel_path.bsprintf(config_include_naming_format_.c_str(), component,
resourcetype_lowercase.c_str(), name);
PathAppend(path, rel_path);
return true;
}
bool ConfigurationParser::GetPathOfNewResource(PoolMem& path,
PoolMem& extramsg,
const char* component,
const char* resourcetype,
const char* name,
bool error_if_exists,
bool create_directories)
{
PoolMem rel_path(PM_FNAME);
PoolMem directory(PM_FNAME);
PoolMem resourcetype_lowercase(resourcetype);
resourcetype_lowercase.toLower();
if (!GetPathOfResource(path, component, resourcetype, name, false)) {
return false;
}
PathGetDirectory(directory, path);
if (create_directories) { PathCreate(directory); }
if (!PathExists(directory)) {
extramsg.bsprintf("Resource config directory \"%s\" does not exist.\n",
directory.c_str());
return false;
}
/*
* Store name for temporary file in extramsg.
* Can be used, if result is true.
* Otherwise it contains an error message.
*/
extramsg.bsprintf("%s.tmp", path.c_str());
if (!error_if_exists) { return true; }
// File should not exists, as it is going to be created.
if (PathExists(path)) {
extramsg.bsprintf("Resource config file \"%s\" already exists.\n",
path.c_str());
return false;
}
if (PathExists(extramsg)) {
extramsg.bsprintf(
"Temporary resource config file \"%s.tmp\" already exists.\n",
path.c_str());
return false;
}
return true;
}
void ConfigurationParser::AddWarning(const std::string& warning)
{
warnings_ << warning;
}
void ConfigurationParser::ClearWarnings() { warnings_.clear(); }
bool ConfigurationParser::HasWarnings() const { return !warnings_.empty(); }
const BStringList& ConfigurationParser::GetWarnings() const
{
return warnings_;
}