You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

93 lines
2.7 KiB

  1. /*
  2. * This file is part of PowerDNS or dnsdist.
  3. * Copyright -- PowerDNS.COM B.V. and its contributors
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * In addition, for the avoidance of any doubt, permission is granted to
  10. * link this program with OpenSSL and to (re)distribute the binaries
  11. * produced as the result of such linking.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <fstream>
  23. #include <stdexcept>
  24. #include "dnsname.hh"
  25. #include "logger.hh"
  26. #include "misc.hh"
  27. #include "pubsuffix.hh"
  28. extern const char* g_pubsuffix[];
  29. std::vector<std::vector<std::string>> g_pubs;
  30. void initPublicSuffixList(const std::string& file)
  31. {
  32. std::vector<std::vector<std::string>> pbList;
  33. bool loaded = false;
  34. if (!file.empty()) {
  35. try {
  36. Regex reg("^[.0-9a-z-]*$");
  37. std::ifstream suffixFile(file);
  38. if (!suffixFile.is_open()) {
  39. throw std::runtime_error("Error opening the public suffix list file");
  40. }
  41. std::string line;
  42. while (std::getline(suffixFile, line)) {
  43. if (line.empty() || (line.rfind("//", 0) == 0)) {
  44. /* skip empty and commented lines */
  45. continue;
  46. }
  47. try {
  48. line = toLower(line);
  49. if (!reg.match(line)) {
  50. continue;
  51. }
  52. DNSName name(toLower(line));
  53. if (name.countLabels() < 2) {
  54. continue;
  55. }
  56. pbList.push_back(name.labelReverse().getRawLabels());
  57. }
  58. catch (...) {
  59. /* not a DNS name, ignoring */
  60. }
  61. }
  62. g_log << Logger::Info << "Loaded the Public Suffix List from '" << file << "'" << endl;
  63. loaded = true;
  64. }
  65. catch (const std::exception& e) {
  66. g_log << Logger::Warning << "Error while loading the Public Suffix List from '" << file << "', falling back to the built-in list: " << e.what() << endl;
  67. }
  68. }
  69. if (!loaded) {
  70. pbList.clear();
  71. for (const char** p = g_pubsuffix; *p; ++p) {
  72. string low = toLower(*p);
  73. vector<string> parts;
  74. stringtok(parts, low, ".");
  75. reverse(parts.begin(), parts.end());
  76. pbList.push_back(parts);
  77. }
  78. }
  79. sort(pbList.begin(), pbList.end());
  80. g_pubs = std::move(pbList);
  81. }