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