]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/configparser.cpp
Convert ConfigTag::CheckRange to a function template.
[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(ConfigItems* items)
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) || (varname.empty() && 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                                 if (varname.empty())
140                                         throw CoreException("Empty XML entity reference");
141                                 else if (varname[0] == '#' && (varname.size() == 1 || (varname.size() == 2 && varname[1] == 'x')))
142                                         throw CoreException("Empty numeric character reference");
143                                 else if (varname[0] == '#')
144                                 {
145                                         const char* cvarname = varname.c_str();
146                                         char* endptr;
147                                         unsigned long lvalue;
148                                         if (cvarname[1] == 'x')
149                                                 lvalue = strtoul(cvarname + 2, &endptr, 16);
150                                         else
151                                                 lvalue = strtoul(cvarname + 1, &endptr, 10);
152                                         if (*endptr != '\0' || lvalue > 255)
153                                                 throw CoreException("Invalid numeric character reference '&" + varname + ";'");
154                                         value.push_back(static_cast<char>(lvalue));
155                                 }
156                                 else
157                                 {
158                                         insp::flat_map<std::string, std::string>::iterator var = stack.vars.find(varname);
159                                         if (var == stack.vars.end())
160                                                 throw CoreException("Undefined XML entity reference '&" + varname + ";'");
161                                         value.append(var->second);
162                                 }
163                         }
164                         else if (ch == '\\' && (flags & FLAG_USE_COMPAT))
165                         {
166                                 int esc = next();
167                                 if (esc == 'n')
168                                         value.push_back('\n');
169                                 else if (isalpha(esc))
170                                         throw CoreException("Unknown escape character \\" + std::string(1, esc));
171                                 else
172                                         value.push_back(esc);
173                         }
174                         else if (ch == '"')
175                                 break;
176                         else if (ch != '\r')
177                                 value.push_back(ch);
178                 }
179
180                 if (items->find(key) != items->end())
181                         throw CoreException("Duplicate key '" + key + "' found");
182
183                 (*items)[key] = value;
184                 return true;
185         }
186
187         void dotag()
188         {
189                 last_tag = current;
190                 std::string name;
191                 nextword(name);
192
193                 int spc = next();
194                 if (spc == '>')
195                         unget(spc);
196                 else if (!isspace(spc))
197                         throw CoreException("Invalid character in tag name");
198
199                 if (name.empty())
200                         throw CoreException("Empty tag name");
201
202                 ConfigItems* items;
203                 tag = ConfigTag::create(name, current.filename, current.line, items);
204
205                 while (kv(items))
206                 {
207                         // Do nothing here (silences a GCC warning).
208                 }
209
210                 if (name == mandatory_tag)
211                 {
212                         // Found the mandatory tag
213                         mandatory_tag.clear();
214                 }
215
216                 if (name == "include")
217                 {
218                         stack.DoInclude(tag, flags);
219                 }
220                 else if (name == "files")
221                 {
222                         for(ConfigItems::iterator i = items->begin(); i != items->end(); i++)
223                         {
224                                 stack.DoReadFile(i->first, i->second, flags, false);
225                         }
226                 }
227                 else if (name == "execfiles")
228                 {
229                         for(ConfigItems::iterator i = items->begin(); i != items->end(); i++)
230                         {
231                                 stack.DoReadFile(i->first, i->second, flags, true);
232                         }
233                 }
234                 else if (name == "define")
235                 {
236                         if (flags & FLAG_USE_COMPAT)
237                                 throw CoreException("<define> tags may only be used in XML-style config (add <config format=\"xml\">)");
238                         std::string varname = tag->getString("name");
239                         std::string value = tag->getString("value");
240                         if (varname.empty())
241                                 throw CoreException("Variable definition must include variable name");
242                         stack.vars[varname] = value;
243                 }
244                 else if (name == "config")
245                 {
246                         std::string format = tag->getString("format");
247                         if (format == "xml")
248                                 flags &= ~FLAG_USE_COMPAT;
249                         else if (format == "compat")
250                                 flags |= FLAG_USE_COMPAT;
251                         else if (!format.empty())
252                                 throw CoreException("Unknown configuration format " + format);
253                 }
254                 else
255                 {
256                         stack.output.insert(std::make_pair(name, tag));
257                 }
258                 // this is not a leak; reference<> takes care of the delete
259                 tag = NULL;
260         }
261
262         bool outer_parse()
263         {
264                 try
265                 {
266                         while (1)
267                         {
268                                 int ch = next(true);
269                                 switch (ch)
270                                 {
271                                         case EOF:
272                                                 // this is the one place where an EOF is not an error
273                                                 if (!mandatory_tag.empty())
274                                                         throw CoreException("Mandatory tag \"" + mandatory_tag + "\" not found");
275                                                 return true;
276                                         case '#':
277                                                 comment();
278                                                 break;
279                                         case '<':
280                                                 dotag();
281                                                 break;
282                                         case ' ':
283                                         case '\r':
284                                         case '\t':
285                                         case '\n':
286                                                 break;
287                                         case 0xFE:
288                                         case 0xFF:
289                                                 stack.errstr << "Do not save your files as UTF-16; use ASCII!\n";
290                                         default:
291                                                 throw CoreException("Syntax error - start of tag expected");
292                                 }
293                         }
294                 }
295                 catch (CoreException& err)
296                 {
297                         stack.errstr << err.GetReason() << " at " << current.str();
298                         if (tag)
299                                 stack.errstr << " (inside tag " << tag->tag << " at line " << tag->src_line << ")\n";
300                         else
301                                 stack.errstr << " (last tag was on line " << last_tag.line << ")\n";
302                 }
303                 return false;
304         }
305 };
306
307 void ParseStack::DoInclude(ConfigTag* tag, int flags)
308 {
309         if (flags & FLAG_NO_INC)
310                 throw CoreException("Invalid <include> tag in file included with noinclude=\"yes\"");
311
312         std::string mandatorytag;
313         tag->readString("mandatorytag", mandatorytag);
314
315         std::string name;
316         if (tag->readString("file", name))
317         {
318                 if (tag->getBool("noinclude", false))
319                         flags |= FLAG_NO_INC;
320                 if (tag->getBool("noexec", false))
321                         flags |= FLAG_NO_EXEC;
322                 if (!ParseFile(ServerInstance->Config->Paths.PrependConfig(name), flags, mandatorytag))
323                         throw CoreException("Included");
324         }
325         else if (tag->readString("executable", name))
326         {
327                 if (flags & FLAG_NO_EXEC)
328                         throw CoreException("Invalid <include:executable> tag in file included with noexec=\"yes\"");
329                 if (tag->getBool("noinclude", false))
330                         flags |= FLAG_NO_INC;
331                 if (tag->getBool("noexec", true))
332                         flags |= FLAG_NO_EXEC;
333                 if (!ParseFile(name, flags, mandatorytag, true))
334                         throw CoreException("Included");
335         }
336 }
337
338 void ParseStack::DoReadFile(const std::string& key, const std::string& name, int flags, bool exec)
339 {
340         if (flags & FLAG_NO_INC)
341                 throw CoreException("Invalid <files> tag in file included with noinclude=\"yes\"");
342         if (exec && (flags & FLAG_NO_EXEC))
343                 throw CoreException("Invalid <execfiles> tag in file included with noexec=\"yes\"");
344
345         std::string path = ServerInstance->Config->Paths.PrependConfig(name);
346         FileWrapper file(exec ? popen(name.c_str(), "r") : fopen(path.c_str(), "r"), exec);
347         if (!file)
348                 throw CoreException("Could not read \"" + path + "\" for \"" + key + "\" file");
349
350         file_cache& cache = FilesOutput[key];
351         cache.clear();
352
353         char linebuf[5120];
354         while (fgets(linebuf, sizeof(linebuf), file))
355         {
356                 size_t len = strlen(linebuf);
357                 if (len)
358                 {
359                         if (linebuf[len-1] == '\n')
360                                 len--;
361                         cache.push_back(std::string(linebuf, len));
362                 }
363         }
364 }
365
366 bool ParseStack::ParseFile(const std::string& path, int flags, const std::string& mandatory_tag, bool isexec)
367 {
368         ServerInstance->Logs->Log("CONFIG", LOG_DEBUG, "Reading (isexec=%d) %s", isexec, path.c_str());
369         if (stdalgo::isin(reading, path))
370                 throw CoreException((isexec ? "Executable " : "File ") + path + " is included recursively (looped inclusion)");
371
372         /* It's not already included, add it to the list of files we've loaded */
373
374         FileWrapper file((isexec ? popen(path.c_str(), "r") : fopen(path.c_str(), "r")), isexec);
375         if (!file)
376                 throw CoreException("Could not read \"" + path + "\" for include");
377
378         reading.push_back(path);
379         Parser p(*this, flags, file, path, mandatory_tag);
380         bool ok = p.outer_parse();
381         reading.pop_back();
382         return ok;
383 }
384
385 bool ConfigTag::readString(const std::string& key, std::string& value, bool allow_lf)
386 {
387         for(ConfigItems::iterator j = items.begin(); j != items.end(); ++j)
388         {
389                 if(j->first != key)
390                         continue;
391                 value = j->second;
392                 if (!allow_lf && (value.find('\n') != std::string::npos))
393                 {
394                         ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
395                                 " contains a linefeed, and linefeeds in this value are not permitted -- stripped to spaces.");
396                         for (std::string::iterator n = value.begin(); n != value.end(); n++)
397                                 if (*n == '\n')
398                                         *n = ' ';
399                 }
400                 return true;
401         }
402         return false;
403 }
404
405 std::string ConfigTag::getString(const std::string& key, const std::string& def, size_t minlen, size_t maxlen)
406 {
407         std::string res;
408         if (!readString(key, res))
409                 return def;
410
411         if (res.length() < minlen || res.length() > maxlen)
412         {
413                 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "WARNING: The length of <%s:%s> is not between %ld and %ld; value set to %s.",
414                         tag.c_str(), key.c_str(), minlen, maxlen, def.c_str());
415                 return def;
416         }
417         return res;
418 }
419
420 namespace
421 {
422         /** Check for an out of range value. If the value falls outside the boundaries a warning is
423          * logged and the value is corrected (set to \p def).
424          * @param tag The tag name; used in the warning message.
425          * @param key The key name; used in the warning message.
426          * @param num The value to verify and modify if needed.
427          * @param def The default value, \p res will be set to this if (min <= res <= max) doesn't hold true.
428          * @param min Minimum accepted value for \p res.
429          * @param max Maximum accepted value for \p res.
430          */
431         template <typename Numeric>
432         void CheckRange(const std::string& tag, const std::string& key, Numeric& num, Numeric def, Numeric min, Numeric max)
433         {
434                 if (num >= min && num <= max)
435                         return;
436
437                 const std::string message = "WARNING: <" + tag + ":" + key + "> value of " + ConvToStr(num) + " is not between "
438                         + ConvToStr(min) + " and " + ConvToStr(max) + "; value set to " + ConvToStr(def) + ".";
439                 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, message);
440                 num = def;
441         }
442 }
443
444 long ConfigTag::getInt(const std::string &key, long def, long min, long max)
445 {
446         std::string result;
447         if(!readString(key, result))
448                 return def;
449
450         const char* res_cstr = result.c_str();
451         char* res_tail = NULL;
452         long res = strtol(res_cstr, &res_tail, 0);
453         if (res_tail == res_cstr)
454                 return def;
455         switch (toupper(*res_tail))
456         {
457                 case 'K':
458                         res = res * 1024;
459                         break;
460                 case 'M':
461                         res = res * 1024 * 1024;
462                         break;
463                 case 'G':
464                         res = res * 1024 * 1024 * 1024;
465                         break;
466         }
467
468         CheckRange(tag, key, res, def, min, max);
469         return res;
470 }
471
472 long ConfigTag::getDuration(const std::string& key, long def, long min, long max)
473 {
474         std::string duration;
475         if (!readString(key, duration))
476                 return def;
477
478         long ret = InspIRCd::Duration(duration);
479         CheckRange(tag, key, ret, def, min, max);
480         return ret;
481 }
482
483 double ConfigTag::getFloat(const std::string &key, double def)
484 {
485         std::string result;
486         if (!readString(key, result))
487                 return def;
488         return strtod(result.c_str(), NULL);
489 }
490
491 bool ConfigTag::getBool(const std::string &key, bool def)
492 {
493         std::string result;
494         if(!readString(key, result))
495                 return def;
496
497         if (result == "yes" || result == "true" || result == "1" || result == "on")
498                 return true;
499         if (result == "no" || result == "false" || result == "0" || result == "off")
500                 return false;
501
502         ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
503                 " is not valid, ignoring");
504         return def;
505 }
506
507 std::string ConfigTag::getTagLocation()
508 {
509         return src_name + ":" + ConvToStr(src_line);
510 }
511
512 ConfigTag* ConfigTag::create(const std::string& Tag, const std::string& file, int line, ConfigItems*& Items)
513 {
514         ConfigTag* rv = new ConfigTag(Tag, file, line);
515         Items = &rv->items;
516         return rv;
517 }
518
519 ConfigTag::ConfigTag(const std::string& Tag, const std::string& file, int line)
520         : tag(Tag), src_name(file), src_line(line)
521 {
522 }
523
524 OperInfo::OperInfo(const std::string& Name)
525         : name(Name)
526 {
527 }
528
529 std::string OperInfo::getConfig(const std::string& key)
530 {
531         std::string rv;
532         if (type_block)
533                 type_block->readString(key, rv);
534         if (oper_block)
535                 oper_block->readString(key, rv);
536         return rv;
537 }