forked from kismetwireless/kismet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configfile.cc
693 lines (519 loc) · 19.6 KB
/
configfile.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
/*
This file is part of Kismet
Kismet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kismet 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Kismet; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <string>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <stdexcept>
#include <glob.h>
#include <sys/stat.h>
#include "util.h"
#include "configfile.h"
#include "messagebus.h"
#ifndef GLOB_TILDE_CHECK
#define GLOB_TILDE_CHECK GLOB_TILDE
#endif
// uclibc is missing GLOB_TILDE entirely so just disable it
#ifndef GLOB_TILDE
#define GLOB_TILDE 0
#endif
config_file::config_file() {
config_locker.set_name("configfile_locker");
checksum = 0;
}
config_file::~config_file() {
}
int config_file::parse_config(const char *in_fname) {
int r;
r = parse_config(in_fname, config_map, config_map_dirty);
if (r < 0)
return r;
for (auto f : config_override_file_list) {
r = parse_opt_override(f);
if (r < 0)
break;
}
config_override_file_list.clear();
if (final_override.length() > 0)
parse_opt_override(final_override);
return r;
}
int config_file::parse_config(const char *in_fname,
std::map<std::string, std::vector<config_entity> > &target_map,
std::map<std::string, int> &target_map_dirty) {
kis_lock_guard<kis_mutex> lk(config_locker, "configfile parse_config");
FILE *configf;
char confline[8192];
std::stringstream sstream;
if ((configf = fopen(in_fname, "r")) == NULL) {
sstream << "Error reading config file '" << in_fname <<
"': " << kis_strerror_r(errno);
_MSG(sstream.str(), MSGFLAG_ERROR);
return -1;
}
filename = in_fname;
int lineno = 0;
while (!feof(configf)) {
if (fgets(confline, 8192, configf) == NULL || feof(configf))
break;
lineno++;
// It's easier to parse this using C++ functions
std::string parsestr = str_strip(confline);
std::string directive, value;
bool append = false;
if (parsestr.length() == 0)
continue;
if (parsestr[0] == '#')
continue;
unsigned int eq;
if ((eq = parsestr.find("=")) > parsestr.length() || eq == 0) {
directive = parsestr;
value = "";
} else {
if (parsestr[eq - 1] == '+') {
append = true;
directive = str_strip(parsestr.substr(0, eq - 1));
value = str_strip(parsestr.substr(eq + 1, parsestr.length()));
} else {
directive = str_strip(parsestr.substr(0, eq));
value = str_strip(parsestr.substr(eq+1, parsestr.length()));
}
if (value == "") {
sstream << "Illegal config option in '" << in_fname << "' line " <<
lineno << ": " << parsestr;
_MSG(sstream.str(), MSGFLAG_ERROR);
sstream.str("");
continue;
}
if (directive == "include") {
value = expand_log_path(value, "", "", 0, 1);
sstream << "Including sub-config file: " << value;
_MSG(sstream.str(), MSGFLAG_INFO);
sstream.str("");
if (parse_config(value.c_str(), target_map, target_map_dirty) < 0) {
fclose(configf);
return -1;
}
} else if (directive == "opt_include") {
if (parse_opt_include(expand_log_path(value, "", "", 0, 1), target_map,
target_map_dirty) < 0)
return -1;
} else if (directive == "opt_override") {
// Store the override for parsing at the end
config_override_file_list.push_back(expand_log_path(value, "", "", 0, 1));
} else {
config_entity e(value, in_fname, append);
target_map[str_lower(directive)].push_back(e);
target_map_dirty[str_lower(directive)] = 1;
}
}
}
fclose(configf);
return 1;
}
int config_file::parse_opt_include(const std::string path,
std::map<std::string, std::vector<config_entity> > &target_map,
std::map<std::string, int> &target_map_dirty) {
glob_t globbed;
size_t i;
struct stat st;
std::stringstream sstream;
if (glob(path.c_str(), GLOB_TILDE_CHECK, NULL, &globbed) == 0) {
for(i=0; i<globbed.gl_pathc; i++) {
if (stat(globbed.gl_pathv[i], &st) != 0) {
continue;
}
if (!S_ISREG(st.st_mode)) {
continue;
}
sstream << "Loading optional sub-config file: " << globbed.gl_pathv[i];
_MSG(sstream.str(), MSGFLAG_INFO);
sstream.str("");
if (parse_config(globbed.gl_pathv[i], target_map, target_map_dirty) < 0) {
sstream << "Parsing failed for optional sub-config file: " <<
globbed.gl_pathv[i];
_MSG(sstream.str(), MSGFLAG_ERROR);
sstream.str("");
return -1;
}
}
} else {
sstream << "Optional sub-config file not present: " << path;
_MSG(sstream.str(), MSGFLAG_INFO);
sstream.str("");
return 0;
}
globfree(&globbed);
return 1;
}
int config_file::parse_opt_override(const std::string path) {
std::map<std::string, std::vector<config_entity> > override_config_map;
std::map<std::string, int> override_config_map_dirty;
int r;
_MSG("Loading config override file '" + path + "'", MSGFLAG_INFO);
// Parse into our submaps
r = parse_opt_include(path, override_config_map, override_config_map_dirty);
// If we hit a legit error or a missing file, bail
if (r <= 0)
return r;
// Clobber or append existing values
for (auto v : override_config_map) {
bool appendonly = true;
for (const auto& uv : v.second) {
if (!uv.append)
appendonly = false;
}
// If we only use append +=, concat to an existing value, otherwise
// override per usual since we have a '=' and a '+='
if (appendonly) {
for (const auto& uv : v.second) {
config_map[v.first].push_back(uv);
}
} else {
config_map[v.first] = v.second;
}
}
for (auto d : override_config_map_dirty)
config_map_dirty[d.first] = d.second;
return 1;
}
int config_file::save_config(const char *in_fname) {
kis_lock_guard<kis_mutex> lk(config_locker, "config_file save_config");
FILE *wf = NULL;
if ((wf = fopen(in_fname, "w")) == NULL) {
_MSG_ERROR("Could not write config file {} - {}", in_fname, kis_strerror_r(errno));
return -1;
}
for (auto x = config_map.begin(); x != config_map.end(); ++x) {
for (unsigned int y = 0; y < x->second.size(); y++) {
fprintf(wf, "%s=%s\n", x->first.c_str(), x->second[y].value.c_str());
}
}
fclose(wf);
return 1;
}
std::string config_file::fetch_opt(std::string in_key) {
kis_lock_guard<kis_mutex> lk(config_locker, "config_file fetch_opt");
auto cmitr = config_map.find(str_lower(in_key));
// No such key
if (cmitr == config_map.end())
return "";
// Get a single key if we can
if (cmitr->second.size() == 0)
return "";
std::string val = cmitr->second[0].value;
return val;
}
std::string config_file::fetch_opt_dfl(std::string in_key, std::string in_dfl) {
std::string r = fetch_opt(in_key);
if (r.length() == 0)
return in_dfl;
return r;
}
std::vector<std::string> config_file::fetch_opt_vec(std::string in_key) {
kis_lock_guard<kis_mutex> lk(config_locker, "config_file fetch_opt_vec");
// Empty vec to return
std::vector<std::string> eretvec;
auto cmitr = config_map.find(str_lower(in_key));
// No such key
if (cmitr == config_map.end())
return eretvec;
for (unsigned int i = 0; i < cmitr->second.size(); i++) {
eretvec.push_back(cmitr->second[i].value);
}
return eretvec;
}
int config_file::fetch_opt_bool(std::string in_key, int dvalue) {
// Don't lock, we're locked in fetchopt
// local_locker lock(&config_locker);
std::string v = str_lower(fetch_opt(in_key));
int r;
r = string_to_bool(v);
if (r == -1)
return dvalue;
return r;
}
header_value_config config_file::fetch_opt_complex(const std::string& in_key) {
return header_value_config(fetch_opt(in_key));
}
std::string config_file::fetch_opt_path(const std::string& in_key, const std::string& in_dfl) {
auto p = fetch_opt_dfl(in_key, in_dfl);
return expand_log_path(p, "", "", 0, 1);
}
int config_file::fetch_opt_int(const std::string& in_key, int dvalue) {
return fetch_opt_as<int>(in_key, dvalue);
}
unsigned int config_file::fetch_opt_uint(const std::string& in_key, unsigned int dvalue) {
return fetch_opt_as<unsigned int>(in_key, dvalue);
}
unsigned long config_file::fetch_opt_ulong(const std::string& in_key, unsigned long dvalue) {
return fetch_opt_as<unsigned long>(in_key, dvalue);
}
int config_file::fetch_opt_dirty(const std::string& in_key) {
kis_lock_guard<kis_mutex> lk(config_locker, "config_file fetch_opt_dirty");
if (config_map_dirty.find(str_lower(in_key)) == config_map_dirty.end())
return 0;
return config_map_dirty[str_lower(in_key)];
}
void config_file::set_opt_dirty(const std::string& in_key, int in_dirty) {
kis_lock_guard<kis_mutex> lk(config_locker, "config_file set_opt_dirty");
config_map_dirty[str_lower(in_key)] = in_dirty;
}
void config_file::set_opt(const std::string& in_key, const std::string& in_val, int in_dirty) {
kis_lock_guard<kis_mutex> lk(config_locker, "config_file set_opt");
std::vector<config_entity> v;
config_entity e(in_val, "::dynamic::");
v.push_back(e);
config_map[str_lower(in_key)] = v;
set_opt_dirty(in_key, in_dirty);
}
void config_file::set_opt_vec(const std::string& in_key, const std::vector<std::string>& in_val,
int in_dirty) {
kis_lock_guard<kis_mutex> lk(config_locker, "config_file set_opt_vec");
std::vector<config_entity> cev;
for (unsigned int x = 0; x < in_val.size(); x++) {
config_entity ce(in_val[x], "::dynamic::");
cev.push_back(ce);
}
config_map[str_lower(in_key)] = cev;
set_opt_dirty(in_key, in_dirty);
}
std::string config_file::process_log_template(const std::string& path, const std::string& logname,
const std::string& type, unsigned int iteration) {
std::string logtemplate;
logtemplate = path;
for (unsigned int nl = logtemplate.find("%"); nl < logtemplate.length(); nl = logtemplate.find("%", nl)) {
char op = logtemplate[nl+1];
logtemplate.erase(nl, 2);
if (op == 'n')
logtemplate.insert(nl, logname);
else if (op == 'd') {
time_t tnow;
struct tm now;
tnow = Globalreg::globalreg->start_time;
gmtime_r(&tnow, &now);
char datestr[24];
strftime(datestr, 24, "%b-%d-%Y", &now);
logtemplate.insert(nl, datestr);
}
else if (op == 'D') {
time_t tnow;
struct tm now;
tnow = Globalreg::globalreg->start_time;
gmtime_r(&tnow, &now);
char datestr[24];
strftime(datestr, 24, "%Y%m%d", &now);
logtemplate.insert(nl, datestr);
} else if (op == 't') {
time_t tnow;
struct tm now;
tnow = Globalreg::globalreg->start_time;
gmtime_r(&tnow, &now);
char timestr[12];
strftime(timestr, 12, "%H-%M-%S", &now);
logtemplate.insert(nl, timestr);
} else if (op == 'T') {
time_t tnow;
struct tm now;
// tnow = time(0);
tnow = Globalreg::globalreg->start_time;
gmtime_r(&tnow, &now);
char timestr[12];
strftime(timestr, 12, "%H%M%S", &now);
logtemplate.insert(nl, timestr);
} else if (op == 'l') {
logtemplate.insert(nl, type.c_str());
} else if (op == 'i') {
logtemplate.insert(nl, fmt::format("{}", iteration));
} else if (op == 'I') {
logtemplate.insert(nl, fmt::format("{:06}", iteration));
} else if (op == 'h') {
if (Globalreg::globalreg->homepath == "") {
char *pwbuf;
ssize_t pwbuf_sz;
struct passwd pw, *pw_result = NULL;
if ((pwbuf_sz = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1) {
pwbuf_sz = 8192;
}
pwbuf = new char[pwbuf_sz];
if (getpwuid_r(getuid(), &pw, pwbuf, pwbuf_sz, &pw_result) != 0 ||
pw_result == NULL) {
fprintf(stderr, "ERROR: Could not explode home directory path, getpwuid() failed.\n");
exit(1);
} else {
logtemplate.insert(nl, pw_result->pw_dir);
}
delete[] pwbuf;
} else {
logtemplate.insert(nl, Globalreg::globalreg->homepath);
}
} else if (op == 'p') {
std::string pfx = Globalreg::globalreg->log_prefix;
if (pfx == "")
pfx = fetch_opt_dfl("log_prefix", "./");
if (pfx != "")
if (pfx[pfx.length() - 1] != '/')
pfx += "/";
logtemplate.insert(nl, pfx);
} else if (op == 'S') {
logtemplate.insert(nl, Globalreg::globalreg->data_dir);
} else if (op == 'E') {
logtemplate.insert(nl, Globalreg::globalreg->etc_dir);
} else if (op == 'B') {
logtemplate.insert(nl, BIN_LOC);
}
}
return logtemplate;
}
std::string config_file::expand_log_path(const std::string& path, const std::string& logname,
const std::string& type, int start, int overwrite) {
kis_lock_guard<kis_mutex> lk(config_locker, "config_file expand_log_path");
auto have_incremental = path.find("%i") != std::string::npos || path.find("%I") != std::string::npos;
struct stat filstat;
if (have_incremental) {
// If we don't have a number we want to use, find the next free
for (unsigned int i = start; i < 10000; i++) {
auto logfile = process_log_template(path, logname, type, i);
if (overwrite)
return logfile;
if (stat(logfile.c_str(), &filstat) == 0)
continue;
auto lfgz = logfile + ".gz";
if (stat(lfgz.c_str(), &filstat) == 0)
continue;
auto lfbz = logfile + ".bz2";
if (stat(lfgz.c_str(), &filstat) == 0)
continue;
return logfile;
}
_MSG_ERROR("Could not allocate file for {} ({}) within a reasonable search, try moving "
"similarly named log files out of the logging directory?", logname, type);
return "";
}
auto logfile = process_log_template(path, logname, type, 0);
if (overwrite)
return logfile;
if (stat(logfile.c_str(), &filstat) == 0)
return "";
auto lfgz = logfile + ".gz";
if (stat(lfgz.c_str(), &filstat) == 0)
return "";
auto lfbz = logfile + ".bz2";
if (stat(lfgz.c_str(), &filstat) == 0)
return "";
return logfile;
}
uint32_t config_file::fetch_file_checksum() {
kis_lock_guard<kis_mutex> lk(config_locker, "config_file fetch_file_checksum");
if (checksum == 0)
calculate_file_checksum();
return checksum;
}
void config_file::calculate_file_checksum() {
kis_lock_guard<kis_mutex> lk(config_locker, "config_file calculate_file_checksum");
std::string cks;
for (auto x = config_map.begin(); x != config_map.end(); ++x) {
cks += x->first;
for (unsigned int y = 0; y < x->second.size(); y++) {
cks += x->second[y].value;
}
}
checksum = adler32_checksum(cks.c_str(), cks.length());
}
header_value_config::header_value_config(const std::string& in_confline) {
parse_line(in_confline);
}
header_value_config::header_value_config() {
}
void header_value_config::parse_line(const std::string& in_confline) {
kis_lock_guard<kis_mutex> lk(mutex, "header_value_config parse_line");
auto cpos = in_confline.find(":");
content_map.clear();
if (cpos == std::string::npos) {
header = in_confline;
} else {
header = in_confline.substr(0, cpos);
raw = in_confline.substr(cpos + 1, in_confline.length() - (cpos + 1));
std::vector<opt_pair> opt_vec;
string_to_opts(in_confline.substr(cpos + 1, in_confline.size() - cpos), ",", &opt_vec);
for (auto oi : opt_vec)
content_map[oi.opt] = oi.val;
}
}
std::string header_value_config::get_header() {
kis_lock_guard<kis_mutex> lk(mutex, "header_value_config get_header");
return header;
}
void header_value_config::set_header(const std::string& in_str) {
kis_lock_guard<kis_mutex> lk(mutex, "header_value_config set_header");
header = in_str;
}
bool header_value_config::has_key(const std::string& in_str) {
kis_lock_guard<kis_mutex> lk(mutex, "header_value_config has_key");
return (content_map.find(in_str) != content_map.end());
}
std::string header_value_config::get_value(const std::string& in_str) {
kis_lock_guard<kis_mutex> lk(mutex, "header_value_config get_value");
auto vi = content_map.find(in_str);
if (vi == content_map.end())
throw std::runtime_error(fmt::format("no such key in content map: {}", in_str));
return vi->second;
}
std::string header_value_config::get_value(const std::string& in_str, const std::string& in_defl) {
kis_lock_guard<kis_mutex> lk(mutex, "header_value_config get_value");
auto vi = content_map.find(in_str);
if (vi == content_map.end())
return in_defl;
return vi->second;
}
void header_value_config::erase_key(const std::string& in_key) {
kis_lock_guard<kis_mutex> lk(mutex, "header_value_config erase_key");
auto vi = content_map.find(in_key);
if (vi == content_map.end())
return;
content_map.erase(vi);
}
std::string header_value_config::to_string() {
std::stringstream ss;
ss << header << ":";
bool add_comma = false;
for (auto kv : content_map) {
if (add_comma)
ss << ",";
add_comma = true;
ss << kv.first << "=\"" << kv.second << "\"";
}
return ss.str();
}
std::ostream& operator<<(std::ostream& os, const header_value_config& h) {
os << h.header << ":";
bool add_comma = false;
for (auto kv : h.content_map) {
if (add_comma)
os << ",";
add_comma = true;
os << kv.first << "=\"" << kv.second << "\"";
}
return os;
}
std::istream& operator>>(std::istream& is, header_value_config& h) {
std::string sline;
std::getline(is, sline);
h.parse_line(sline);
return is;
}