-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.cc
164 lines (137 loc) · 5.69 KB
/
config.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
// Copyright (c) 2010-2023 David Caldwell <david@porkrind.org>
// Licenced under the GPL 3.0 or any later version. See LICENSE file for details.
#include "config.h"
#include "strprintf.h"
#include "uniq.h"
#include "stringutil.h"
#include "log.h"
#include "foreach.h"
#include <grp.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
static string remove_comment(string in) { return trim(in.substr(0, min(in.length(), in.find('#')))); }
template<class T>
std::vector<T> concat(std::vector<T> a, std::vector<T> b)
{
std::vector<T> list = a;
list.insert(list.end(), b.begin(), b.end());
return list;
}
struct master_config parse_master_config(string path)
{
ifstream in(path.c_str(), ifstream::in);
struct master_config config;
map<string,vector<string> > *section = NULL;
bool settings_section = false;
int n=0;
string line;
while (in.good()) {
getline(in, line);
n++;
line = remove_comment(line);
if (line.empty()) continue;
if (line[0] == '[') {
size_t end = line.find(']');
if (end == line.npos) throw_str("%s:%d missing ']' from section header", path.c_str(), n);
string sect = line.substr(1, end-1);
if (trim(line.substr(end+1)).length()) throw_str("%s:%d Junk at end of section header line", path.c_str(), n);
if (sect == "runs_as") {
sect = "can_run_as";
log(LOG_WARNING, "%s:%d [runs_as] is now called [can_run_as]. Please fix this, it will break eventually.\n", path.c_str(), n);
}
settings_section = false;
if (sect == "can_run_as") section = &config.can_run_as;
else if (sect == "manages") section = &config.manages;
else if (sect == "settings") settings_section = true;
else throw_str("%s:%d Illegal section \"%s\"", path.c_str(), n, sect.c_str());
continue;
}
if (!section && !settings_section) throw_str("%s:%d Line before section header", path.c_str(), n);
if (settings_section) {
size_t sep;
string key = trim(line.substr(0, sep=line.find('=')));
if (sep == line.npos) throw_str("%s:%d Missing '=' after key", path.c_str(), n);
config.settings[key] = trim(line.substr(sep+1));
continue;
}
size_t sep;
string key = trim(line.substr(0, sep=line.find(':')));
vector<string> list;
if (sep != line.npos)
split(list, trim(line.substr(sep+1)), string(","));
// Expand @group in list to individual user names.
for (auto it = list.begin(); it != list.end(); it++)
if ((*it)[0] == '@') {
string group = it->substr(1);
struct group *g = getgrnam(group.c_str());
list.erase(it);
if (!g)
log(LOG_ERR, "%s:%d Couldn't find group \"%s\". Ignoring.\n", path.c_str(), n, group.c_str());
else
for (int i=0; g->gr_mem[i]; i++)
list.push_back(string(g->gr_mem[i]));
it = list.begin(); // start over--our iterator is invalidated by the erase().
}
// Expand @group in the key to individual user names by duplicating the list
if (key[0] == '@') {
string group = key.substr(1);
struct group *g = getgrnam(group.c_str());
if (!g)
log(LOG_ERR, "%s:%d Couldn't find group \"%s\". Ignoring line.\n", path.c_str(), n, group.c_str());
else
for (int i=0; g->gr_mem[i]; i++)
(*section)[g->gr_mem[i]] = uniq(concat((*section)[g->gr_mem[i]], list));
} else
(*section)[key] = uniq(concat((*section)[key], list));
}
return config;
}
struct daemon_config parse_daemon_config(string path)
{
ifstream in(path.c_str(), ifstream::in);
struct daemon_config config;
int n=0;
string line;
while (in.good()) {
getline(in, line);
n++;
line = remove_comment(line);
if (line.empty()) continue;
size_t sep;
map<string,string> *section = &config.config;
if (trim(line.substr(0, sep=line.find(' '))) == "export") {
line = line.substr(sep+1);
section = &config.env;
}
string key = trim(line.substr(0, sep=line.find('=')));
if (sep == line.npos) throw_str("%s:%d Missing '=' after key", path.c_str(), n);
(*section)[key] = trim(line.substr(sep+1));
}
return config;
}
static list<string> validate_keys_inner(map<string,string> cfg, const string &file, const vector<string> &valid_keys, bool error)
{
std::list<string> whine_list;
typedef pair<string,string> str_pair;
foreach(str_pair c, cfg) {
foreach(const string &key, valid_keys)
if (c.first == key) goto found;
{ string warning = strprintf("Unknown key \"%s\" in config file \"%s\"\n", c.first.c_str(), file.c_str());
log(error ? LOG_ERR : LOG_WARNING, "%s", warning.c_str());
whine_list.push_back("Warning: "+warning);
} // C++ doesn't like "warning" being instantiated midsteram unless it goes out of scope before the goto label.
found:;
}
return whine_list;
}
void validate_keys_pedantically(map<string,string> cfg, const string &file, const vector<string> &valid_keys)
{
if (validate_keys_inner(cfg, file, valid_keys, true).size() > 0)
throw_str("%s contains unknown keys", file.c_str());
}
list<string> validate_keys(map<string,string> cfg, const string &file, const vector<string> &valid_keys)
{
return validate_keys_inner(cfg, file, valid_keys, false);
}