]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/configparser.cpp
Fix Windows build and a few more problems
[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         std::string path = ServerInstance->Config->Paths.PrependConfig(name);
327         FileWrapper file(exec ? popen(name.c_str(), "r") : fopen(path.c_str(), "r"), exec);
328         if (!file)
329                 throw CoreException("Could not read \"" + path + "\" for \"" + key + "\" file");
330
331         file_cache& cache = FilesOutput[key];
332         cache.clear();
333
334         char linebuf[5120];
335         while (fgets(linebuf, sizeof(linebuf), file))
336         {
337                 size_t len = strlen(linebuf);
338                 if (len)
339                 {
340                         if (linebuf[len-1] == '\n')
341                                 len--;
342                         cache.push_back(std::string(linebuf, len));
343                 }
344         }
345 }
346
347 bool ParseStack::ParseFile(const std::string& name, int flags, const std::string& mandatory_tag)
348 {
349         std::string path = ServerInstance->Config->Paths.PrependConfig(name);
350         ServerInstance->Logs->Log("CONFIG", LOG_DEBUG, "Reading file %s", path.c_str());
351         for (unsigned int t = 0; t < reading.size(); t++)
352         {
353                 if (std::string(name) == reading[t])
354                 {
355                         throw CoreException("File " + path + " is included recursively (looped inclusion)");
356                 }
357         }
358
359         /* It's not already included, add it to the list of files we've loaded */
360
361         FileWrapper file(fopen(path.c_str(), "r"));
362         if (!file)
363                 throw CoreException("Could not read \"" + path + "\" for include");
364
365         reading.push_back(path);
366         Parser p(*this, flags, file, path, mandatory_tag);
367         bool ok = p.outer_parse();
368         reading.pop_back();
369         return ok;
370 }
371
372 bool ParseStack::ParseExec(const std::string& name, int flags, const std::string& mandatory_tag)
373 {
374         ServerInstance->Logs->Log("CONFIG", LOG_DEBUG, "Reading executable %s", name.c_str());
375         for (unsigned int t = 0; t < reading.size(); t++)
376         {
377                 if (std::string(name) == reading[t])
378                 {
379                         throw CoreException("Executable " + name + " is included recursively (looped inclusion)");
380                 }
381         }
382
383         /* It's not already included, add it to the list of files we've loaded */
384
385         FileWrapper file(popen(name.c_str(), "r"), true);
386         if (!file)
387                 throw CoreException("Could not open executable \"" + name + "\" for include");
388
389         reading.push_back(name);
390         Parser p(*this, flags, file, name, mandatory_tag);
391         bool ok = p.outer_parse();
392         reading.pop_back();
393         return ok;
394 }
395
396 bool ConfigTag::readString(const std::string& key, std::string& value, bool allow_lf)
397 {
398         if (!this)
399                 return false;
400         for(std::vector<KeyVal>::iterator j = items.begin(); j != items.end(); ++j)
401         {
402                 if(j->first != key)
403                         continue;
404                 value = j->second;
405                 if (!allow_lf && (value.find('\n') != std::string::npos))
406                 {
407                         ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
408                                 " contains a linefeed, and linefeeds in this value are not permitted -- stripped to spaces.");
409                         for (std::string::iterator n = value.begin(); n != value.end(); n++)
410                                 if (*n == '\n')
411                                         *n = ' ';
412                 }
413                 return true;
414         }
415         return false;
416 }
417
418 std::string ConfigTag::getString(const std::string& key, const std::string& def)
419 {
420         std::string res = def;
421         readString(key, res);
422         return res;
423 }
424
425 long ConfigTag::getInt(const std::string &key, long def, long min, long max)
426 {
427         std::string result;
428         if(!readString(key, result))
429                 return def;
430
431         const char* res_cstr = result.c_str();
432         char* res_tail = NULL;
433         long res = strtol(res_cstr, &res_tail, 0);
434         if (res_tail == res_cstr)
435                 return def;
436         switch (toupper(*res_tail))
437         {
438                 case 'K':
439                         res = res * 1024;
440                         break;
441                 case 'M':
442                         res = res * 1024 * 1024;
443                         break;
444                 case 'G':
445                         res = res * 1024 * 1024 * 1024;
446                         break;
447         }
448
449         CheckRange(key, res, def, min, max);
450         return res;
451 }
452
453 void ConfigTag::CheckRange(const std::string& key, long& res, long def, long min, long max)
454 {
455         if (res < min || res > max)
456         {
457                 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "WARNING: <%s:%s> value of %ld is not between %ld and %ld; set to %ld.",
458                         tag.c_str(), key.c_str(), res, min, max, def);
459                 res = def;
460         }
461 }
462
463 long ConfigTag::getDuration(const std::string& key, long def, long min, long max)
464 {
465         std::string duration;
466         if (!readString(key, duration))
467                 return def;
468
469         long ret = InspIRCd::Duration(duration);
470         CheckRange(key, ret, def, min, max);
471         return ret;
472 }
473
474 double ConfigTag::getFloat(const std::string &key, double def)
475 {
476         std::string result;
477         if (!readString(key, result))
478                 return def;
479         return strtod(result.c_str(), NULL);
480 }
481
482 bool ConfigTag::getBool(const std::string &key, bool def)
483 {
484         std::string result;
485         if(!readString(key, result))
486                 return def;
487
488         if (result == "yes" || result == "true" || result == "1" || result == "on")
489                 return true;
490         if (result == "no" || result == "false" || result == "0" || result == "off")
491                 return false;
492
493         ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
494                 " is not valid, ignoring");
495         return def;
496 }
497
498 std::string ConfigTag::getTagLocation()
499 {
500         return src_name + ":" + ConvToStr(src_line);
501 }
502
503 ConfigTag* ConfigTag::create(const std::string& Tag, const std::string& file, int line, std::vector<KeyVal>*&items)
504 {
505         ConfigTag* rv = new ConfigTag(Tag, file, line);
506         items = &rv->items;
507         return rv;
508 }
509
510 ConfigTag::ConfigTag(const std::string& Tag, const std::string& file, int line)
511         : tag(Tag), src_name(file), src_line(line)
512 {
513 }
514
515 std::string OperInfo::getConfig(const std::string& key)
516 {
517         std::string rv;
518         if (type_block)
519                 type_block->readString(key, rv);
520         if (oper_block)
521                 oper_block->readString(key, rv);
522         return rv;
523 }