]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/configparser.cpp
Merge pull request #933 from SaberUK/insp20+fix-llvm34
[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_XML))
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_XML))
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                 if (name == mandatory_tag)
189                 {
190                         // Found the mandatory tag
191                         mandatory_tag.clear();
192                 }
193
194                 if (name == "include")
195                 {
196                         stack.DoInclude(tag, flags);
197                 }
198                 else if (name == "files")
199                 {
200                         for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
201                         {
202                                 stack.DoReadFile(i->first, i->second, flags, false);
203                         }
204                 }
205                 else if (name == "execfiles")
206                 {
207                         for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
208                         {
209                                 stack.DoReadFile(i->first, i->second, flags, true);
210                         }
211                 }
212                 else if (name == "define")
213                 {
214                         if (!(flags & FLAG_USE_XML))
215                                 throw CoreException("<define> tags may only be used in XML-style config (add <config format=\"xml\">)");
216                         std::string varname = tag->getString("name");
217                         std::string value = tag->getString("value");
218                         if (varname.empty())
219                                 throw CoreException("Variable definition must include variable name");
220                         stack.vars[varname] = value;
221                 }
222                 else if (name == "config")
223                 {
224                         std::string format = tag->getString("format");
225                         if (format == "xml")
226                                 flags |= FLAG_USE_XML;
227                         else if (format == "compat")
228                                 flags &= ~FLAG_USE_XML;
229                         else if (!format.empty())
230                                 throw CoreException("Unknown configuration format " + format);
231                 }
232                 else
233                 {
234                         stack.output.insert(std::make_pair(name, tag));
235                 }
236                 // this is not a leak; reference<> takes care of the delete
237                 tag = NULL;
238         }
239
240         bool outer_parse()
241         {
242                 try
243                 {
244                         while (1)
245                         {
246                                 int ch = next(true);
247                                 switch (ch)
248                                 {
249                                         case EOF:
250                                                 // this is the one place where an EOF is not an error
251                                                 if (!mandatory_tag.empty())
252                                                         throw CoreException("Mandatory tag \"" + mandatory_tag + "\" not found");
253                                                 return true;
254                                         case '#':
255                                                 comment();
256                                                 break;
257                                         case '<':
258                                                 dotag();
259                                                 break;
260                                         case ' ':
261                                         case '\r':
262                                         case '\t':
263                                         case '\n':
264                                                 break;
265                                         case 0xFE:
266                                         case 0xFF:
267                                                 stack.errstr << "Do not save your files as UTF-16; use ASCII!\n";
268                                         default:
269                                                 throw CoreException("Syntax error - start of tag expected");
270                                 }
271                         }
272                 }
273                 catch (CoreException& err)
274                 {
275                         stack.errstr << err.GetReason() << " at " << current.str();
276                         if (tag)
277                                 stack.errstr << " (inside tag " << tag->tag << " at line " << tag->src_line << ")\n";
278                         else
279                                 stack.errstr << " (last tag was on line " << last_tag.line << ")\n";
280                 }
281                 return false;
282         }
283 };
284
285 void ParseStack::DoInclude(ConfigTag* tag, int flags)
286 {
287         if (flags & FLAG_NO_INC)
288                 throw CoreException("Invalid <include> tag in file included with noinclude=\"yes\"");
289
290         std::string mandatorytag;
291         tag->readString("mandatorytag", mandatorytag);
292
293         std::string name;
294         if (tag->readString("file", name))
295         {
296                 if (tag->getBool("noinclude", false))
297                         flags |= FLAG_NO_INC;
298                 if (tag->getBool("noexec", false))
299                         flags |= FLAG_NO_EXEC;
300                 if (!ParseFile(name, flags, mandatorytag))
301                         throw CoreException("Included");
302         }
303         else if (tag->readString("executable", name))
304         {
305                 if (flags & FLAG_NO_EXEC)
306                         throw CoreException("Invalid <include:executable> tag in file included with noexec=\"yes\"");
307                 if (tag->getBool("noinclude", false))
308                         flags |= FLAG_NO_INC;
309                 if (tag->getBool("noexec", true))
310                         flags |= FLAG_NO_EXEC;
311                 if (!ParseExec(name, flags, mandatorytag))
312                         throw CoreException("Included");
313         }
314 }
315
316 void ParseStack::DoReadFile(const std::string& key, const std::string& name, int flags, bool exec)
317 {
318         if (flags & FLAG_NO_INC)
319                 throw CoreException("Invalid <files> tag in file included with noinclude=\"yes\"");
320         if (exec && (flags & FLAG_NO_EXEC))
321                 throw CoreException("Invalid <execfiles> tag in file included with noexec=\"yes\"");
322
323         FileWrapper file(exec ? popen(name.c_str(), "r") : fopen(name.c_str(), "r"), exec);
324         if (!file)
325                 throw CoreException("Could not read \"" + name + "\" for \"" + key + "\" file");
326
327         file_cache& cache = FilesOutput[key];
328         cache.clear();
329
330         char linebuf[MAXBUF*10];
331         while (fgets(linebuf, sizeof(linebuf), file))
332         {
333                 size_t len = strlen(linebuf);
334                 if (len)
335                 {
336                         if (linebuf[len-1] == '\n')
337                                 len--;
338                         cache.push_back(std::string(linebuf, len));
339                 }
340         }
341 }
342
343 bool ParseStack::ParseFile(const std::string& name, int flags, const std::string& mandatory_tag)
344 {
345         ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading file %s", name.c_str());
346         for (unsigned int t = 0; t < reading.size(); t++)
347         {
348                 if (std::string(name) == reading[t])
349                 {
350                         throw CoreException("File " + name + " is included recursively (looped inclusion)");
351                 }
352         }
353
354         /* It's not already included, add it to the list of files we've loaded */
355
356         FileWrapper file(fopen(name.c_str(), "r"));
357         if (!file)
358                 throw CoreException("Could not read \"" + name + "\" for include");
359
360         reading.push_back(name);
361         Parser p(*this, flags, file, name, mandatory_tag);
362         bool ok = p.outer_parse();
363         reading.pop_back();
364         return ok;
365 }
366
367 bool ParseStack::ParseExec(const std::string& name, int flags, const std::string& mandatory_tag)
368 {
369         ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading executable %s", name.c_str());
370         for (unsigned int t = 0; t < reading.size(); t++)
371         {
372                 if (std::string(name) == reading[t])
373                 {
374                         throw CoreException("Executable " + name + " is included recursively (looped inclusion)");
375                 }
376         }
377
378         /* It's not already included, add it to the list of files we've loaded */
379
380         FileWrapper file(popen(name.c_str(), "r"), true);
381         if (!file)
382                 throw CoreException("Could not open executable \"" + name + "\" for include");
383
384         reading.push_back(name);
385         Parser p(*this, flags, file, name, mandatory_tag);
386         bool ok = p.outer_parse();
387         reading.pop_back();
388         return ok;
389 }
390
391 bool ConfigTag::readString(const std::string& key, std::string& value, bool allow_lf)
392 {
393 #ifdef __clang__
394 # pragma clang diagnostic push
395 # pragma clang diagnostic ignored "-Wunknown-pragmas"
396 # pragma clang diagnostic ignored "-Wundefined-bool-conversion"
397 #endif
398         // TODO: this is undefined behaviour but changing the API is too risky for 2.0.
399         if (!this)
400                 return false;
401 #ifdef __clang__
402 # pragma clang diagnostic pop
403 #endif
404         for(std::vector<KeyVal>::iterator j = items.begin(); j != items.end(); ++j)
405         {
406                 if(j->first != key)
407                         continue;
408                 value = j->second;
409                 if (!allow_lf && (value.find('\n') != std::string::npos))
410                 {
411                         ServerInstance->Logs->Log("CONFIG",DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
412                                 " contains a linefeed, and linefeeds in this value are not permitted -- stripped to spaces.");
413                         for (std::string::iterator n = value.begin(); n != value.end(); n++)
414                                 if (*n == '\n')
415                                         *n = ' ';
416                 }
417                 return true;
418         }
419         return false;
420 }
421
422 std::string ConfigTag::getString(const std::string& key, const std::string& def)
423 {
424         std::string res = def;
425         readString(key, res);
426         return res;
427 }
428
429 long ConfigTag::getInt(const std::string &key, long def)
430 {
431         std::string result;
432         if(!readString(key, result))
433                 return def;
434
435         const char* res_cstr = result.c_str();
436         char* res_tail = NULL;
437         long res = strtol(res_cstr, &res_tail, 0);
438         if (res_tail == res_cstr)
439                 return def;
440         switch (toupper(*res_tail))
441         {
442                 case 'K':
443                         res= res* 1024;
444                         break;
445                 case 'M':
446                         res= res* 1024 * 1024;
447                         break;
448                 case 'G':
449                         res= res* 1024 * 1024 * 1024;
450                         break;
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",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 }