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