]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/configparser.cpp
Add support for including directories containing .conf files.
[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 enum ParseFlags
25 {
26         // Legacy config parsing should be used.
27         FLAG_USE_COMPAT = 1,
28
29         // Executable includes are disabled.
30         FLAG_NO_EXEC = 2,
31
32         // All includes are disabled.
33         FLAG_NO_INC = 4
34 };
35
36 // Represents the position within a config file.
37 struct FilePosition
38 {
39         // The name of the file which is being read.
40         std::string name;
41
42         // The line of the file that this position points to.
43         unsigned int line;
44
45         // The column of the file that this position points to.
46         unsigned int column;
47
48         FilePosition(const std::string& Name)
49                 : name(Name)
50                 , line(1)
51                 , column(1)
52         {
53         }
54
55         /** Returns a string that represents this file position. */
56         std::string str()
57         {
58                 return name + ":" + ConvToStr(line) + ":" + ConvToStr(column);
59         }
60 };
61
62 // RAII wrapper for FILE* which closes the file when it goes out of scope.
63 class FileWrapper
64 {
65  private:
66         // Whether this file handle should be closed with pclose.
67         bool close_with_pclose;
68
69         // The file handle which is being wrapped.
70         FILE* const file;
71
72  public:
73         FileWrapper(FILE* File, bool CloseWithPClose = false)
74                 : close_with_pclose(CloseWithPClose)
75                 , file(File)
76         {
77         }
78
79         // Operator which determines whether the file is open.
80         operator bool() { return (file != NULL); }
81
82         // Operator which retrieves the underlying FILE pointer.
83         operator FILE*() { return file; }
84
85         ~FileWrapper()
86         {
87                 if (!file)
88                         return;
89
90                 if (close_with_pclose)
91                         pclose(file);
92                 else
93                         fclose(file);
94         }
95 };
96
97
98 struct Parser
99 {
100         ParseStack& stack;
101         int flags;
102         FILE* const file;
103         FilePosition current;
104         FilePosition last_tag;
105         reference<ConfigTag> tag;
106         int ungot;
107         std::string mandatory_tag;
108
109         Parser(ParseStack& me, int myflags, FILE* conf, const std::string& name, const std::string& mandatorytag)
110                 : stack(me), flags(myflags), file(conf), current(name), last_tag(name), ungot(-1), mandatory_tag(mandatorytag)
111         { }
112
113         int next(bool eof_ok = false)
114         {
115                 if (ungot != -1)
116                 {
117                         int ch = ungot;
118                         ungot = -1;
119                         return ch;
120                 }
121                 int ch = fgetc(file);
122                 if (ch == EOF && !eof_ok)
123                 {
124                         throw CoreException("Unexpected end-of-file");
125                 }
126                 else if (ch == '\n')
127                 {
128                         current.line++;
129                         current.column = 0;
130                 }
131                 else
132                 {
133                         current.column++;
134                 }
135                 return ch;
136         }
137
138         void unget(int ch)
139         {
140                 if (ungot != -1)
141                         throw CoreException("INTERNAL ERROR: cannot unget twice");
142                 ungot = ch;
143         }
144
145         void comment()
146         {
147                 while (1)
148                 {
149                         int ch = next();
150                         if (ch == '\n')
151                                 return;
152                 }
153         }
154
155         void nextword(std::string& rv)
156         {
157                 int ch = next();
158                 while (isspace(ch))
159                         ch = next();
160                 while (isalnum(ch) || ch == '_'|| ch == '-')
161                 {
162                         rv.push_back(ch);
163                         ch = next();
164                 }
165                 unget(ch);
166         }
167
168         bool kv(ConfigItems* items)
169         {
170                 std::string key;
171                 nextword(key);
172                 int ch = next();
173                 if (ch == '>' && key.empty())
174                 {
175                         return false;
176                 }
177                 else if (ch == '#' && key.empty())
178                 {
179                         comment();
180                         return true;
181                 }
182                 else if (ch != '=')
183                 {
184                         throw CoreException("Invalid character " + std::string(1, ch) + " in key (" + key + ")");
185                 }
186
187                 std::string value;
188                 ch = next();
189                 if (ch != '"')
190                 {
191                         throw CoreException("Invalid character in value of <" + tag->tag + ":" + key + ">");
192                 }
193                 while (1)
194                 {
195                         ch = next();
196                         if (ch == '&' && !(flags & FLAG_USE_COMPAT))
197                         {
198                                 std::string varname;
199                                 while (1)
200                                 {
201                                         ch = next();
202                                         if (isalnum(ch) || (varname.empty() && ch == '#'))
203                                                 varname.push_back(ch);
204                                         else if (ch == ';')
205                                                 break;
206                                         else
207                                         {
208                                                 stack.errstr << "Invalid XML entity name in value of <" + tag->tag + ":" + key + ">\n"
209                                                         << "To include an ampersand or quote, use &amp; or &quot;\n";
210                                                 throw CoreException("Parse error");
211                                         }
212                                 }
213                                 if (varname.empty())
214                                         throw CoreException("Empty XML entity reference");
215                                 else if (varname[0] == '#' && (varname.size() == 1 || (varname.size() == 2 && varname[1] == 'x')))
216                                         throw CoreException("Empty numeric character reference");
217                                 else if (varname[0] == '#')
218                                 {
219                                         const char* cvarname = varname.c_str();
220                                         char* endptr;
221                                         unsigned long lvalue;
222                                         if (cvarname[1] == 'x')
223                                                 lvalue = strtoul(cvarname + 2, &endptr, 16);
224                                         else
225                                                 lvalue = strtoul(cvarname + 1, &endptr, 10);
226                                         if (*endptr != '\0' || lvalue > 255)
227                                                 throw CoreException("Invalid numeric character reference '&" + varname + ";'");
228                                         value.push_back(static_cast<char>(lvalue));
229                                 }
230                                 else
231                                 {
232                                         insp::flat_map<std::string, std::string>::iterator var = stack.vars.find(varname);
233                                         if (var == stack.vars.end())
234                                                 throw CoreException("Undefined XML entity reference '&" + varname + ";'");
235                                         value.append(var->second);
236                                 }
237                         }
238                         else if (ch == '\\' && (flags & FLAG_USE_COMPAT))
239                         {
240                                 int esc = next();
241                                 if (esc == 'n')
242                                         value.push_back('\n');
243                                 else if (isalpha(esc))
244                                         throw CoreException("Unknown escape character \\" + std::string(1, esc));
245                                 else
246                                         value.push_back(esc);
247                         }
248                         else if (ch == '"')
249                                 break;
250                         else if (ch != '\r')
251                                 value.push_back(ch);
252                 }
253
254                 if (items->find(key) != items->end())
255                         throw CoreException("Duplicate key '" + key + "' found");
256
257                 (*items)[key] = value;
258                 return true;
259         }
260
261         void dotag()
262         {
263                 last_tag = current;
264                 std::string name;
265                 nextword(name);
266
267                 int spc = next();
268                 if (spc == '>')
269                         unget(spc);
270                 else if (!isspace(spc))
271                         throw CoreException("Invalid character in tag name");
272
273                 if (name.empty())
274                         throw CoreException("Empty tag name");
275
276                 ConfigItems* items;
277                 tag = ConfigTag::create(name, current.name, current.line, items);
278
279                 while (kv(items))
280                 {
281                         // Do nothing here (silences a GCC warning).
282                 }
283
284                 if (name == mandatory_tag)
285                 {
286                         // Found the mandatory tag
287                         mandatory_tag.clear();
288                 }
289
290                 if (stdalgo::string::equalsci(name, "include"))
291                 {
292                         stack.DoInclude(tag, flags);
293                 }
294                 else if (stdalgo::string::equalsci(name, "files"))
295                 {
296                         for(ConfigItems::iterator i = items->begin(); i != items->end(); i++)
297                         {
298                                 stack.DoReadFile(i->first, i->second, flags, false);
299                         }
300                 }
301                 else if (stdalgo::string::equalsci(name, "execfiles"))
302                 {
303                         for(ConfigItems::iterator i = items->begin(); i != items->end(); i++)
304                         {
305                                 stack.DoReadFile(i->first, i->second, flags, true);
306                         }
307                 }
308                 else if (stdalgo::string::equalsci(name, "define"))
309                 {
310                         if (flags & FLAG_USE_COMPAT)
311                                 throw CoreException("<define> tags may only be used in XML-style config (add <config format=\"xml\">)");
312                         std::string varname = tag->getString("name");
313                         std::string value = tag->getString("value");
314                         if (varname.empty())
315                                 throw CoreException("Variable definition must include variable name");
316                         stack.vars[varname] = value;
317                 }
318                 else if (stdalgo::string::equalsci(name, "config"))
319                 {
320                         std::string format = tag->getString("format");
321                         if (stdalgo::string::equalsci(format, "xml"))
322                                 flags &= ~FLAG_USE_COMPAT;
323                         else if (stdalgo::string::equalsci(format, "compat"))
324                                 flags |= FLAG_USE_COMPAT;
325                         else if (!format.empty())
326                                 throw CoreException("Unknown configuration format " + format);
327                 }
328                 else
329                 {
330                         stack.output.insert(std::make_pair(name, tag));
331                 }
332                 // this is not a leak; reference<> takes care of the delete
333                 tag = NULL;
334         }
335
336         bool outer_parse()
337         {
338                 try
339                 {
340                         while (1)
341                         {
342                                 int ch = next(true);
343                                 switch (ch)
344                                 {
345                                         case EOF:
346                                                 // this is the one place where an EOF is not an error
347                                                 if (!mandatory_tag.empty())
348                                                         throw CoreException("Mandatory tag \"" + mandatory_tag + "\" not found");
349                                                 return true;
350                                         case '#':
351                                                 comment();
352                                                 break;
353                                         case '<':
354                                                 dotag();
355                                                 break;
356                                         case ' ':
357                                         case '\r':
358                                         case '\t':
359                                         case '\n':
360                                                 break;
361                                         case 0xFE:
362                                         case 0xFF:
363                                                 stack.errstr << "Do not save your files as UTF-16 or UTF-32, use UTF-8!\n";
364                                                 /*@fallthrough@*/
365                                         default:
366                                                 throw CoreException("Syntax error - start of tag expected");
367                                 }
368                         }
369                 }
370                 catch (CoreException& err)
371                 {
372                         stack.errstr << err.GetReason() << " at " << current.str();
373                         if (tag)
374                                 stack.errstr << " (inside tag " << tag->tag << " at line " << tag->src_line << ")\n";
375                         else
376                                 stack.errstr << " (last tag was on line " << last_tag.line << ")\n";
377                 }
378                 return false;
379         }
380 };
381
382 void ParseStack::DoInclude(ConfigTag* tag, int flags)
383 {
384         if (flags & FLAG_NO_INC)
385                 throw CoreException("Invalid <include> tag in file included with noinclude=\"yes\"");
386
387         std::string mandatorytag;
388         tag->readString("mandatorytag", mandatorytag);
389
390         std::string name;
391         if (tag->readString("file", name))
392         {
393                 if (tag->getBool("noinclude", false))
394                         flags |= FLAG_NO_INC;
395                 if (tag->getBool("noexec", false))
396                         flags |= FLAG_NO_EXEC;
397
398                 if (!ParseFile(ServerInstance->Config->Paths.PrependConfig(name), flags, mandatorytag))
399                         throw CoreException("Included");
400         }
401         else if (tag->readString("directory", name))
402         {
403                 if (tag->getBool("noinclude", false))
404                         flags |= FLAG_NO_INC;
405                 if (tag->getBool("noexec", false))
406                         flags |= FLAG_NO_EXEC;
407
408                 const std::string includedir = ServerInstance->Config->Paths.PrependConfig(name);
409                 std::vector<std::string> files;
410                 if (!FileSystem::GetFileList(includedir, files, "*.conf"))
411                         throw CoreException("Unable to read directory for include: " + includedir);
412
413                 std::sort(files.begin(), files.end()); 
414                 for (std::vector<std::string>::const_iterator iter = files.begin(); iter != files.end(); ++iter)
415                 {
416                         const std::string path = includedir + '/' + *iter;
417                         if (!ParseFile(path, flags, mandatorytag))
418                                 throw CoreException("Included");
419                 }
420         }
421         else if (tag->readString("executable", name))
422         {
423                 if (flags & FLAG_NO_EXEC)
424                         throw CoreException("Invalid <include:executable> tag in file included with noexec=\"yes\"");
425                 if (tag->getBool("noinclude", false))
426                         flags |= FLAG_NO_INC;
427                 if (tag->getBool("noexec", true))
428                         flags |= FLAG_NO_EXEC;
429
430                 if (!ParseFile(name, flags, mandatorytag, true))
431                         throw CoreException("Included");
432         }
433 }
434
435 void ParseStack::DoReadFile(const std::string& key, const std::string& name, int flags, bool exec)
436 {
437         if (flags & FLAG_NO_INC)
438                 throw CoreException("Invalid <files> tag in file included with noinclude=\"yes\"");
439         if (exec && (flags & FLAG_NO_EXEC))
440                 throw CoreException("Invalid <execfiles> tag in file included with noexec=\"yes\"");
441
442         std::string path = ServerInstance->Config->Paths.PrependConfig(name);
443         FileWrapper file(exec ? popen(name.c_str(), "r") : fopen(path.c_str(), "r"), exec);
444         if (!file)
445                 throw CoreException("Could not read \"" + path + "\" for \"" + key + "\" file");
446
447         file_cache& cache = FilesOutput[key];
448         cache.clear();
449
450         char linebuf[5120];
451         while (fgets(linebuf, sizeof(linebuf), file))
452         {
453                 size_t len = strlen(linebuf);
454                 if (len)
455                 {
456                         if (linebuf[len-1] == '\n')
457                                 len--;
458                         cache.push_back(std::string(linebuf, len));
459                 }
460         }
461 }
462
463 bool ParseStack::ParseFile(const std::string& path, int flags, const std::string& mandatory_tag, bool isexec)
464 {
465         ServerInstance->Logs->Log("CONFIG", LOG_DEBUG, "Reading (isexec=%d) %s", isexec, path.c_str());
466         if (stdalgo::isin(reading, path))
467                 throw CoreException((isexec ? "Executable " : "File ") + path + " is included recursively (looped inclusion)");
468
469         /* It's not already included, add it to the list of files we've loaded */
470
471         FileWrapper file((isexec ? popen(path.c_str(), "r") : fopen(path.c_str(), "r")), isexec);
472         if (!file)
473                 throw CoreException("Could not read \"" + path + "\" for include");
474
475         reading.push_back(path);
476         Parser p(*this, flags, file, path, mandatory_tag);
477         bool ok = p.outer_parse();
478         reading.pop_back();
479         return ok;
480 }
481
482 bool ConfigTag::readString(const std::string& key, std::string& value, bool allow_lf)
483 {
484         for(ConfigItems::iterator j = items.begin(); j != items.end(); ++j)
485         {
486                 if(j->first != key)
487                         continue;
488                 value = j->second;
489                 if (!allow_lf && (value.find('\n') != std::string::npos))
490                 {
491                         ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
492                                 " contains a linefeed, and linefeeds in this value are not permitted -- stripped to spaces.");
493                         for (std::string::iterator n = value.begin(); n != value.end(); n++)
494                                 if (*n == '\n')
495                                         *n = ' ';
496                 }
497                 return true;
498         }
499         return false;
500 }
501
502 std::string ConfigTag::getString(const std::string& key, const std::string& def, const TR1NS::function<bool(const std::string&)>& validator)
503 {
504         std::string res;
505         if (!readString(key, res))
506                 return def;
507
508         if (!validator(res))
509         {
510                 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "WARNING: The value of <%s:%s> is not valid; value set to %s.",
511                         tag.c_str(), key.c_str(), def.c_str());
512                 return def;
513         }
514         return res;
515 }
516
517 std::string ConfigTag::getString(const std::string& key, const std::string& def, size_t minlen, size_t maxlen)
518 {
519         std::string res;
520         if (!readString(key, res))
521                 return def;
522
523         if (res.length() < minlen || res.length() > maxlen)
524         {
525                 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "WARNING: The length of <%s:%s> is not between %ld and %ld; value set to %s.",
526                         tag.c_str(), key.c_str(), minlen, maxlen, def.c_str());
527                 return def;
528         }
529         return res;
530 }
531
532 namespace
533 {
534         /** Check for an invalid magnitude specifier. If one is found a warning is logged and the
535          * value is corrected (set to \p def).
536          * @param tag The tag name; used in the warning message.
537          * @param key The key name; used in the warning message.
538          * @param val The full value set in the config as a string.
539          * @param num The value to verify and modify if needed.
540          * @param def The default value, \p res will be set to this if \p tail does not contain a.
541          *            valid magnitude specifier.
542          * @param tail The location in the config value at which the magnifier is located.
543          */
544         template <typename Numeric>
545         void CheckMagnitude(const std::string& tag, const std::string& key, const std::string& val, Numeric& num, Numeric def, const char* tail)
546         {
547                 // If this is NULL then no magnitude specifier was given.
548                 if (!*tail)
549                         return;
550
551                 switch (toupper(*tail))
552                 {
553                         case 'K':
554                                 num *= 1024;
555                                 return;
556
557                         case 'M':
558                                 num *= 1024 * 1024;
559                                 return;
560
561                         case 'G':
562                                 num *= 1024 * 1024 * 1024;
563                                 return;
564                 }
565
566                 const std::string message = "WARNING: <" + tag + ":" + key + "> value of " + val + " contains an invalid magnitude specifier '"
567                         + tail + "'; value set to " + ConvToStr(def) + ".";
568                 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, message);
569                 num = def;
570         }
571
572         /** Check for an out of range value. If the value falls outside the boundaries a warning is
573          * logged and the value is corrected (set to \p def).
574          * @param tag The tag name; used in the warning message.
575          * @param key The key name; used in the warning message.
576          * @param num The value to verify and modify if needed.
577          * @param def The default value, \p res will be set to this if (min <= res <= max) doesn't hold true.
578          * @param min Minimum accepted value for \p res.
579          * @param max Maximum accepted value for \p res.
580          */
581         template <typename Numeric>
582         void CheckRange(const std::string& tag, const std::string& key, Numeric& num, Numeric def, Numeric min, Numeric max)
583         {
584                 if (num >= min && num <= max)
585                         return;
586
587                 const std::string message = "WARNING: <" + tag + ":" + key + "> value of " + ConvToStr(num) + " is not between "
588                         + ConvToStr(min) + " and " + ConvToStr(max) + "; value set to " + ConvToStr(def) + ".";
589                 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, message);
590                 num = def;
591         }
592 }
593
594 long ConfigTag::getInt(const std::string &key, long def, long min, long max)
595 {
596         std::string result;
597         if(!readString(key, result))
598                 return def;
599
600         const char* res_cstr = result.c_str();
601         char* res_tail = NULL;
602         long res = strtol(res_cstr, &res_tail, 0);
603         if (res_tail == res_cstr)
604                 return def;
605
606         CheckMagnitude(tag, key, result, res, def, res_tail);
607         CheckRange(tag, key, res, def, min, max);
608         return res;
609 }
610
611 unsigned long ConfigTag::getUInt(const std::string& key, unsigned long def, unsigned long min, unsigned long max)
612 {
613         std::string result;
614         if (!readString(key, result))
615                 return def;
616
617         const char* res_cstr = result.c_str();
618         char* res_tail = NULL;
619         unsigned long res = strtoul(res_cstr, &res_tail, 0);
620         if (res_tail == res_cstr)
621                 return def;
622
623         CheckMagnitude(tag, key, result, res, def, res_tail);
624         CheckRange(tag, key, res, def, min, max);
625         return res;
626 }
627
628 unsigned long ConfigTag::getDuration(const std::string& key, unsigned long def, unsigned long min, unsigned long max)
629 {
630         std::string duration;
631         if (!readString(key, duration))
632                 return def;
633
634         unsigned long ret;
635         if (!InspIRCd::Duration(duration, ret))
636         {
637                 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
638                         " is not a duration; value set to " + ConvToStr(def) + ".");
639                 return def;
640         }
641
642         CheckRange(tag, key, ret, def, min, max);
643         return ret;
644 }
645
646 double ConfigTag::getFloat(const std::string& key, double def, double min, double max)
647 {
648         std::string result;
649         if (!readString(key, result))
650                 return def;
651
652         double res = strtod(result.c_str(), NULL);
653         CheckRange(tag, key, res, def, min, max);
654         return res;
655 }
656
657 bool ConfigTag::getBool(const std::string &key, bool def)
658 {
659         std::string result;
660         if(!readString(key, result))
661                 return def;
662
663         if (result == "yes" || result == "true" || result == "1" || result == "on")
664                 return true;
665         if (result == "no" || result == "false" || result == "0" || result == "off")
666                 return false;
667
668         ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
669                 " is not valid, ignoring");
670         return def;
671 }
672
673 std::string ConfigTag::getTagLocation()
674 {
675         return src_name + ":" + ConvToStr(src_line);
676 }
677
678 ConfigTag* ConfigTag::create(const std::string& Tag, const std::string& file, int line, ConfigItems*& Items)
679 {
680         ConfigTag* rv = new ConfigTag(Tag, file, line);
681         Items = &rv->items;
682         return rv;
683 }
684
685 ConfigTag::ConfigTag(const std::string& Tag, const std::string& file, int line)
686         : tag(Tag), src_name(file), src_line(line)
687 {
688 }
689
690 OperInfo::OperInfo(const std::string& Name)
691         : name(Name)
692 {
693 }
694
695 std::string OperInfo::getConfig(const std::string& key)
696 {
697         std::string rv;
698         if (type_block)
699                 type_block->readString(key, rv);
700         if (oper_block)
701                 oper_block->readString(key, rv);
702         return rv;
703 }