-
Notifications
You must be signed in to change notification settings - Fork 6
/
Configuration.cpp
303 lines (255 loc) · 6.79 KB
/
Configuration.cpp
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
/*
* This file is part of evQueue
*
* evQueue 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 3 of the License, or
* (at your option) any later version.
*
* evQueue 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 evQueue. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Thibault Kummer <bob@coldsource.net>
*/
#include <Configuration/Configuration.h>
#include <Exception/Exception.h>
#include <regex>
#include <string.h>
#include <stdlib.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
using namespace std;
Configuration *Configuration::instance=0;
Configuration::Configuration(void)
{
}
Configuration::Configuration(const map<string,string> &entries)
{
this->entries = entries;
}
Configuration::~Configuration(void)
{
for(int i=0;i<configs.size();i++)
delete configs[i];
instance = 0;
}
Configuration *Configuration::GetInstance()
{
if(instance==0)
instance = new Configuration();
return instance;
}
bool Configuration::RegisterConfig(Configuration *config)
{
configs.push_back(config);
return true;
}
void Configuration::Merge()
{
for(int i=0;i<configs.size();i++)
{
for(auto it = configs[i]->entries.begin(); it!=configs[i]->entries.end(); ++it)
{
if(entries.find(it->first)!=entries.end())
throw Exception("Configuration", "Duplicated configuration entry : "+it->first);
entries[it->first] = it->second;
}
}
}
void Configuration::Split()
{
for(int i=0;i<configs.size();i++)
{
for(auto it = configs[i]->entries.begin(); it!=configs[i]->entries.end(); ++it)
configs[i]->entries[it->first] = Get(it->first);
}
}
void Configuration::Substitute(void)
{
for(auto it=entries.begin();it!=entries.end();it++)
{
regex var_regex ("(\\{[a-zA-Z_]+\\})");
string entry_value = it->second;
auto words_begin = sregex_iterator(entry_value.begin(), entry_value.end(), var_regex);
auto words_end = sregex_iterator();
for(auto i=words_begin;i!=words_end;++i)
{
string var = i->str();
string var_name = var.substr(1,var.length()-2);
const char *value = getenv(var_name.c_str());
if(value)
{
size_t start_pos = it->second.find(var);
it->second.replace(start_pos,var.length(),string(value));
}
}
}
}
void Configuration::CheckAll()
{
for(int i=0;i<configs.size();i++)
configs[i]->Check();
}
bool Configuration::Set(const string &entry,const string &value)
{
if(entries.count(entry)==0)
return false;
entries[entry] = value;
return true;
}
const string &Configuration::Get(const string &entry) const
{
map<string,string>::const_iterator it = entries.find(entry);
if(it==entries.end())
throw Exception("Configuration","Unknown configuration entry: "+entry);
return it->second;
}
int Configuration::GetInt(const string &entry) const
{
const string value = Get(entry);
if(value.substr(0,2)=="0x")
return strtol(value.c_str(),0,16);
return strtol(value.c_str(),0,10);
}
int Configuration::GetSize(const string &entry) const
{
const string value = Get(entry);
if(value.substr(value.length()-1,1)=="K")
return strtol(value.c_str(),0,10)*1024;
else if(value.substr(value.length()-1,1)=="M")
return strtol(value.c_str(),0,10)*1024*1024;
else if(value.substr(value.length()-1,1)=="G")
return strtol(value.c_str(),0,10)*1024*1024*1024;
else
return strtol(value.c_str(),0,10);
}
bool Configuration::GetBool(const string &entry) const
{
const string value = Get(entry);
if(value=="yes" || value=="true" || value=="1")
return true;
return false;
}
int Configuration::GetUID(const string &entry) const
{
try
{
return std::stoi(Get("core.uid"));
}
catch(const std::invalid_argument& excpt)
{
struct passwd *user_entry = getpwnam(Get("core.uid").c_str());
if(!user_entry)
throw Exception("core","Unable to find user");
return user_entry->pw_uid;
}
catch(const std::out_of_range & excpt)
{
throw Exception("core","Invalid UID");
}
}
int Configuration::GetGID(const string &entry) const
{
try
{
return std::stoi(Get("core.gid"));
}
catch(const std::invalid_argument& excpt)
{
struct group *group_entry = getgrnam(Get("core.gid").c_str());
if(!group_entry)
throw Exception("core","Unable to find group");
return group_entry->gr_gid;
}
catch(const std::out_of_range & excpt)
{
throw Exception("core","Invalid GID");
}
}
void Configuration::check_f_is_exec(const string &filename)
{
uid_t uid = geteuid();
gid_t gid = getegid();
// Special check for root
if(uid==0)
return;
struct stat ste;
if(stat(filename.c_str(),&ste)!=0)
throw Exception("Configuration","File not found : "+filename);
if(!S_ISREG(ste.st_mode))
throw Exception("Configuration",filename+" is not a regular file");
if(uid==ste.st_uid && (ste.st_mode & S_IXUSR))
return;
else if(gid==ste.st_gid && (ste.st_mode & S_IXGRP))
return;
else if(ste.st_mode & S_IXOTH)
return;
throw Exception("Configuration","File is not executable : "+filename);
}
void Configuration::check_d_is_writeable(const string &path)
{
uid_t uid = geteuid();
gid_t gid = getegid();
struct stat ste;
if(stat(path.c_str(),&ste)!=0)
throw Exception("Configuration","Directory not found : "+path);
if(!S_ISDIR(ste.st_mode))
throw Exception("Configuration",path+" is not a directory");
if(uid==ste.st_uid && (ste.st_mode & S_IWUSR))
return;
else if(gid==ste.st_gid && (ste.st_mode & S_IWGRP))
return;
else if(ste.st_mode & S_IWOTH)
return;
throw Exception("Configuration","Directory is not writeable : "+path);
}
void Configuration::check_bool_entry(const string &name)
{
if(entries[name]=="yes" || entries[name]=="true" || entries[name]=="1")
return;
if(entries[name]=="no" || entries[name]=="false" || entries[name]=="0")
return;
throw Exception("Configuration",name+": invalid boolean value '"+entries[name]+"'");
}
void Configuration::check_int_entry(const string &name, bool signed_int)
{
try
{
size_t l;
int val = stoi(entries[name],&l);
if(l!=entries[name].length())
throw 1;
if(!signed_int && val<0)
throw 1;
}
catch(...)
{
throw Exception("Configuration", name+": invalid integer value '"+entries[name]+"'");
}
}
void Configuration::check_size_entry(const string &name)
{
try
{
size_t l;
stoi(entries[name],&l);
if(l==entries[name].length())
return;
string unit = entries[name].substr(l);
if(unit!="K" && unit!="M" && unit!="G")
throw 1;
}
catch(...)
{
throw Exception("Configuration", name+": invalid size value '"+entries[name]+"'");
}
}