]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/configparser.cpp
Replace range() with min and max arguments on getInt().
[user/henk/code/inspircd.git] / src / configparser.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21 #include <fstream>
22 #include "configparser.h"
23
24 struct Parser
25 {
26         ParseStack& stack;
27         int flags;
28         FILE* const file;
29         fpos current;
30         fpos last_tag;
31         reference<ConfigTag> tag;
32         int ungot;
33         std::string mandatory_tag;
34
35         Parser(ParseStack& me, int myflags, FILE* conf, const std::string& name, const std::string& mandatorytag)
36                 : stack(me), flags(myflags), file(conf), current(name), last_tag(name), ungot(-1), mandatory_tag(mandatorytag)
37         { }
38
39         int next(bool eof_ok = false)
40         {
41                 if (ungot != -1)
42                 {
43                         int ch = ungot;
44                         ungot = -1;
45                         return ch;
46                 }
47                 int ch = fgetc(file);
48                 if (ch == EOF && !eof_ok)
49                 {
50                         throw CoreException("Unexpected end-of-file");
51                 }
52                 else if (ch == '\n')
53                 {
54                         current.line++;
55                         current.col = 0;
56                 }
57                 else
58                 {
59                         current.col++;
60                 }
61                 return ch;
62         }
63
64         void unget(int ch)
65         {
66                 if (ungot != -1)
67                         throw CoreException("INTERNAL ERROR: cannot unget twice");
68                 ungot = ch;
69         }
70
71         void comment()
72         {
73                 while (1)
74                 {
75                         int ch = next();
76                         if (ch == '\n')
77                                 return;
78                 }
79         }
80
81         void nextword(std::string& rv)
82         {
83                 int ch = next();
84                 while (isspace(ch))
85                         ch = next();
86                 while (isalnum(ch) || ch == '_'|| ch == '-')
87                 {
88                         rv.push_back(ch);
89                         ch = next();
90                 }
91                 unget(ch);
92         }
93
94         bool kv(std::vector<KeyVal>* items, std::set<std::string>& seen)
95         {
96                 std::string key;
97                 nextword(key);
98                 int ch = next();
99                 if (ch == '>' && key.empty())
100                 {
101                         return false;
102                 }
103                 else if (ch == '#' && key.empty())
104                 {
105                         comment();
106                         return true;
107                 }
108                 else if (ch != '=')
109                 {
110                         throw CoreException("Invalid character " + std::string(1, ch) + " in key (" + key + ")");
111                 }
112
113                 std::string value;
114                 ch = next();
115                 if (ch != '"')
116                 {
117                         throw CoreException("Invalid character in value of <" + tag->tag + ":" + key + ">");
118                 }
119                 while (1)
120                 {
121                         ch = next();
122                         if (ch == '&' && !(flags & FLAG_USE_COMPAT))
123                         {
124                                 std::string varname;
125                                 while (1)
126                                 {
127                                         ch = next();
128                                         if (isalnum(ch))
129                                                 varname.push_back(ch);
130                                         else if (ch == ';')
131                                                 break;
132                                         else
133                                         {
134                                                 stack.errstr << "Invalid XML entity name in value of <" + tag->tag + ":" + key + ">\n"
135                                                         << "To include an ampersand or quote, use &amp; or &quot;\n";
136                                                 throw CoreException("Parse error");
137                                         }
138                                 }
139                                 std::map<std::string, std::string>::iterator var = stack.vars.find(varname);
140                                 if (var == stack.vars.end())
141                                         throw CoreException("Undefined XML entity reference '&" + varname + ";'");
142                                 value.append(var->second);
143                         }
144                         else if (ch == '\\' && (flags & FLAG_USE_COMPAT))
145                         {
146                                 int esc = next();
147                                 if (esc == 'n')
148                                         value.push_back('\n');
149                                 else if (isalpha(esc))
150                                         throw CoreException("Unknown escape character \\" + std::string(1, esc));
151                                 else
152                                         value.push_back(esc);
153                         }
154                         else if (ch == '"')
155                                 break;
156                         else
157                                 value.push_back(ch);
158                 }
159
160                 if (!seen.insert(key).second)
161                         throw CoreException("Duplicate key '" + key + "' found");
162
163                 items->push_back(KeyVal(key, value));
164                 return true;
165         }
166
167         void dotag()
168         {
169                 last_tag = current;
170                 std::string name;
171                 nextword(name);
172
173                 int spc = next();
174                 if (spc == '>')
175                         unget(spc);
176                 else if (!isspace(spc))
177                         throw CoreException("Invalid character in tag name");
178
179                 if (name.empty())
180                         throw CoreException("Empty tag name");
181
182                 std::vector<KeyVal>* items;
183                 std::set<std::string> seen;
184                 tag = ConfigTag::create(name, current.filename, current.line, items);
185
186                 while (kv(items, seen))
187                 {
188                         // Do nothing here (silences a GCC warning).
189                 }
190
191                 if (name == mandatory_tag)
192                 {
193                         // Found the mandatory tag
194                         mandatory_tag.clear();
195                 }
196
197                 if (name == "include")
198                 {
199                         stack.DoInclude(tag, flags);
200                 }
201                 else if (name == "files")
202                 {
203                         for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
204                         {
205                                 stack.DoReadFile(i->first, i->second, flags, false);
206                         }
207                 }
208                 else if (name == "execfiles")
209                 {
210                         for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
211                         {
212                                 stack.DoReadFile(i->first, i->second, flags, true);
213                         }
214                 }
215                 else if (name == "define")
216                 {
217                         if (flags & FLAG_USE_COMPAT)
218                                 throw CoreException("<define> tags may only be used in XML-style config (add <config format=\"xml\">)");
219                         std::string varname = tag->getString("name");
220                         std::string value = tag->getString("value");
221                         if (varname.empty())
222                                 throw CoreException("Variable definition must include variable name");
223                         stack.vars[varname] = value;
224                 }
225                 else if (name == "config")
226                 {
227                         std::string format = tag->getString("format");
228                         if (format == "xml")
229                                 flags &= ~FLAG_USE_COMPAT;
230                         else if (format == "compat")
231                                 flags |= FLAG_USE_COMPAT;
232                         else if (!format.empty())
233                                 throw CoreException("Unknown configuration format " + format);
234                 }
235                 else
236                 {
237                         stack.output.insert(std::make_pair(name, tag));
238                 }
239                 // this is not a leak; reference<> takes care of the delete
240                 tag = NULL;
241         }
242
243         bool outer_parse()
244         {
245                 try
246                 {
247                         while (1)
248                         {
249                                 int ch = next(true);
250                                 switch (ch)
251                                 {
252                                         case EOF:
253                                                 // this is the one place where an EOF is not an error
254                                                 if (!mandatory_tag.empty())
255                                                         throw CoreException("Mandatory tag \"" + mandatory_tag + "\" not found");
256                                                 return true;
257                                         case '#':
258                                                 comment();
259                                                 break;
260                                         case '<':
261                                                 dotag();
262                                                 break;
263                                         case ' ':
264                                         case '\r':
265                                         case '\t':
266                                         case '\n':
267                                                 break;
268                                         case 0xFE:
269                                         case 0xFF:
270                                                 stack.errstr << "Do not save your files as UTF-16; use ASCII!\n";
271                                         default:
272                                                 throw CoreException("Syntax error - start of tag expected");
273                                 }
274                         }
275                 }
276                 catch (CoreException& err)
277                 {
278                         stack.errstr << err.GetReason() << " at " << current.str();
279                         if (tag)
280                                 stack.errstr << " (inside tag " << tag->tag << " at line " << tag->src_line << ")\n";
281                         else
282                                 stack.errstr << " (last tag was on line " << last_tag.line << ")\n";
283                 }
284                 return false;
285         }
286 };
287
288 void ParseStack::DoInclude(ConfigTag* tag, int flags)
289 {
290         if (flags & FLAG_NO_INC)
291                 throw CoreException("Invalid <include> tag in file included with noinclude=\"yes\"");
292
293         std::string mandatorytag;
294         tag->readString("mandatorytag", mandatorytag);
295
296         std::string name;
297         if (tag->readString("file", name))
298         {
299                 if (tag->getBool("noinclude", false))
300                         flags |= FLAG_NO_INC;
301                 if (tag->getBool("noexec", false))
302                         flags |= FLAG_NO_EXEC;
303                 if (!ParseFile(name, flags, mandatorytag))
304                         throw CoreException("Included");
305         }
306         else if (tag->readString("executable", name))
307         {
308                 if (flags & FLAG_NO_EXEC)
309                         throw CoreException("Invalid <include:executable> tag in file included with noexec=\"yes\"");
310                 if (tag->getBool("noinclude", false))
311                         flags |= FLAG_NO_INC;
312                 if (tag->getBool("noexec", true))
313                         flags |= FLAG_NO_EXEC;
314                 if (!ParseExec(name, flags, mandatorytag))
315                         throw CoreException("Included");
316         }
317 }
318
319 void ParseStack::DoReadFile(const std::string& key, const std::string& name, int flags, bool exec)
320 {
321         if (flags & FLAG_NO_INC)
322                 throw CoreException("Invalid <files> tag in file included with noinclude=\"yes\"");
323         if (exec && (flags & FLAG_NO_EXEC))
324                 throw CoreException("Invalid <execfiles> tag in file included with noexec=\"yes\"");
325
326         FileWrapper file(exec ? popen(name.c_str(), "r") : fopen(name.c_str(), "r"), exec);
327         if (!file)
328                 throw CoreException("Could not read \"" + name + "\" for \"" + key + "\" file");
329
330         file_cache& cache = FilesOutput[key];
331         cache.clear();
332
333         char linebuf[5120];
334         while (fgets(linebuf, sizeof(linebuf), file))
335         {
336                 size_t len = strlen(linebuf);
337                 if (len)
338                 {
339                         if (linebuf[len-1] == '\n')
340                                 len--;
341                         cache.push_back(std::string(linebuf, len));
342                 }
343         }
344 }
345
346 bool ParseStack::ParseFile(const std::string& name, int flags, const std::string& mandatory_tag)
347 {
348         ServerInstance->Logs->Log("CONFIG", LOG_DEBUG, "Reading file %s", name.c_str());
349         for (unsigned int t = 0; t < reading.size(); t++)
350         {
351                 if (std::string(name) == reading[t])
352                 {
353                         throw CoreException("File " + name + " is included recursively (looped inclusion)");
354                 }
355         }
356
357         /* It's not already included, add it to the list of files we've loaded */
358
359         FileWrapper file(fopen(name.c_str(), "r"));
360         if (!file)
361                 throw CoreException("Could not read \"" + name + "\" for include");
362
363         reading.push_back(name);
364         Parser p(*this, flags, file, name, mandatory_tag);
365         bool ok = p.outer_parse();
366         reading.pop_back();
367         return ok;
368 }
369
370 bool ParseStack::ParseExec(const std::string& name, int flags, const std::string& mandatory_tag)
371 {
372         ServerInstance->Logs->Log("CONFIG", LOG_DEBUG, "Reading executable %s", name.c_str());
373         for (unsigned int t = 0; t < reading.size(); t++)
374         {
375                 if (std::string(name) == reading[t])
376                 {
377                         throw CoreException("Executable " + name + " is included recursively (looped inclusion)");
378                 }
379         }
380
381         /* It's not already included, add it to the list of files we've loaded */
382
383         FileWrapper file(popen(name.c_str(), "r"), true);
384         if (!file)
385                 throw CoreException("Could not open executable \"" + name + "\" for include");
386
387         reading.push_back(name);
388         Parser p(*this, flags, file, name, mandatory_tag);
389         bool ok = p.outer_parse();
390         reading.pop_back();
391         return ok;
392 }
393
394 bool ConfigTag::readString(const std::string& key, std::string& value, bool allow_lf)
395 {
396         if (!this)
397                 return false;
398         for(std::vector<KeyVal>::iterator j = items.begin(); j != items.end(); ++j)
399         {
400                 if(j->first != key)
401                         continue;
402                 value = j->second;
403                 if (!allow_lf && (value.find('\n') != std::string::npos))
404                 {
405                         ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
406                                 " contains a linefeed, and linefeeds in this value are not permitted -- stripped to spaces.");
407                         for (std::string::iterator n = value.begin(); n != value.end(); n++)
408                                 if (*n == '\n')
409                                         *n = ' ';
410                 }
411                 return true;
412         }
413         return false;
414 }
415
416 std::string ConfigTag::getString(const std::string& key, const std::string& def)
417 {
418         std::string res = def;
419         readString(key, res);
420         return res;
421 }
422
423 long ConfigTag::getInt(const std::string &key, long def, long min, long max)
424 {
425         std::string result;
426         if(!readString(key, result))
427                 return def;
428
429         const char* res_cstr = result.c_str();
430         char* res_tail = NULL;
431         long res = strtol(res_cstr, &res_tail, 0);
432         if (res_tail == res_cstr)
433                 return def;
434         switch (toupper(*res_tail))
435         {
436                 case 'K':
437                         res = res * 1024;
438                         break;
439                 case 'M':
440                         res = res * 1024 * 1024;
441                         break;
442                 case 'G':
443                         res = res * 1024 * 1024 * 1024;
444                         break;
445         }
446         if (res < min || res > max)
447         {
448                 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "WARNING: <%s:%s> value of %ld is not between %ld and %ld; set to %ld.",
449                         tag.c_str(), key.c_str(), res, min, max, def);
450                 res = def;
451         }
452         return res;
453 }
454
455 double ConfigTag::getFloat(const std::string &key, double def)
456 {
457         std::string result;
458         if (!readString(key, result))
459                 return def;
460         return strtod(result.c_str(), NULL);
461 }
462
463 bool ConfigTag::getBool(const std::string &key, bool def)
464 {
465         std::string result;
466         if(!readString(key, result))
467                 return def;
468
469         if (result == "yes" || result == "true" || result == "1" || result == "on")
470                 return true;
471         if (result == "no" || result == "false" || result == "0" || result == "off")
472                 return false;
473
474         ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
475                 " is not valid, ignoring");
476         return def;
477 }
478
479 std::string ConfigTag::getTagLocation()
480 {
481         return src_name + ":" + ConvToStr(src_line);
482 }
483
484 ConfigTag* ConfigTag::create(const std::string& Tag, const std::string& file, int line, std::vector<KeyVal>*&items)
485 {
486         ConfigTag* rv = new ConfigTag(Tag, file, line);
487         items = &rv->items;
488         return rv;
489 }
490
491 ConfigTag::ConfigTag(const std::string& Tag, const std::string& file, int line)
492         : tag(Tag), src_name(file), src_line(line)
493 {
494 }
495
496 std::string OperInfo::getConfig(const std::string& key)
497 {
498         std::string rv;
499         if (type_block)
500                 type_block->readString(key, rv);
501         if (oper_block)
502                 oper_block->readString(key, rv);
503         return rv;
504 }