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.

HostsConfig.cpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // Created by robin on 8/8/15.
  3. //
  4. #include <json/json.h>
  5. #include <fstream>
  6. #include <memory>
  7. #include <DBO/Actions/Action.h>
  8. #include <DBO/Actions/ActionAddHost.h>
  9. #include <DBO/Actions/ActionDelHost.h>
  10. #include <DBO/Actions/ActionAddDomain.h>
  11. #include <DBO/Actions/ActionDelDomain.h>
  12. #include "HostsConfig.h"
  13. #define SET_VALUE(VALUE, METHOD, TYPE) do { \
  14. if (action) \
  15. { \
  16. auto v = readValue(act, defaults, VALUE); \
  17. if (!v || !v.is##TYPE()) \
  18. { \
  19. res.addWarning("Could not find value " \
  20. VALUE " for item " + std::to_string(i));\
  21. action = nullptr; \
  22. } \
  23. else \
  24. { \
  25. action->METHOD(v.as##TYPE()); \
  26. } \
  27. } \
  28. } while (0)
  29. #define SET_VALUE_STRING(VALUE, METHOD) SET_VALUE(VALUE, METHOD, String)
  30. #define SET_VALUE_BOOL(VALUE, METHOD) SET_VALUE(VALUE, METHOD, Bool)
  31. #define SET_VALUE_INT(VALUE, METHOD) SET_VALUE(VALUE, METHOD, Int)
  32. HostsConfig::HostsConfig(const std::string &filePath)
  33. : _filePath(filePath)
  34. {
  35. }
  36. Result<Actions> HostsConfig::readConfig()
  37. {
  38. Result<Actions> res;
  39. std::ifstream file(_filePath);
  40. if (!file)
  41. return res.error("Could not open file");
  42. Json::Value root;
  43. try
  44. {
  45. file >> root;
  46. file.close();
  47. }
  48. catch (...)
  49. {
  50. file.close();
  51. return res.error("Could not parse JSON");
  52. }
  53. auto defaults = root["defaults"];
  54. if (!defaults || !defaults.isObject())
  55. return res.error("Could not find defaults object");
  56. auto acts = root["actions"];
  57. if (!acts || !acts.isArray())
  58. return res.error("Could not find actions array");
  59. std::vector<std::shared_ptr<Action>> actions;
  60. for (Json::ArrayIndex i = 0; i < acts.size(); ++i)
  61. {
  62. auto act = acts[i];
  63. auto action_type = readValue(act, defaults, "action");
  64. if (!action_type || !action_type.isString())
  65. res.addWarning("Could not find action value for item " + std::to_string(i));
  66. else
  67. {
  68. auto action_str = action_type.asString();
  69. if (action_str == "add_domain")
  70. {
  71. auto del = readDelDomain(act, defaults, i, res);
  72. auto add = readAddDomain(act, defaults, i, res);
  73. if (del && add)
  74. {
  75. actions.push_back(del);
  76. actions.push_back(add);
  77. }
  78. }
  79. else if (action_str == "del_domain")
  80. {
  81. auto del = readDelDomain(act, defaults, i, res);
  82. if (del)
  83. actions.push_back(del);
  84. }
  85. else if (action_str == "add_host")
  86. {
  87. auto del = readDelHost(act, defaults, i, res);
  88. auto add = readAddHost(act, defaults, i, res);
  89. if (del && add)
  90. {
  91. actions.push_back(del);
  92. actions.push_back(add);
  93. }
  94. }
  95. else if (action_str == "del_host")
  96. {
  97. auto del = readDelHost(act, defaults, i, res);
  98. if (del)
  99. actions.push_back(del);
  100. }
  101. else
  102. res.addWarning("Unknown action " + action_str);
  103. }
  104. }
  105. return res.ok(actions);
  106. }
  107. Json::Value HostsConfig::readValue(const Json::Value &value, const Json::Value &defaults, const std::string &name) const
  108. {
  109. auto v = value[name];
  110. if (!v)
  111. return defaults[name];
  112. return v;
  113. }
  114. std::shared_ptr<Action> HostsConfig::readAddHost(const Json::Value &act, const Json::Value &defaults,
  115. int i, Result<Actions>& res) const
  116. {
  117. auto action = std::make_shared<ActionAddHost>();
  118. SET_VALUE_STRING("domain", setDomain);
  119. SET_VALUE_STRING("host", setHost);
  120. SET_VALUE_STRING("record_value", setRecordValue);
  121. SET_VALUE_STRING("record_type", setRecordType);
  122. SET_VALUE_STRING("dhcp_mac", setDhcpMac);
  123. SET_VALUE_STRING("reverse_domain", setReverseDomain);
  124. SET_VALUE_BOOL("reverse_enabled", setReverseEnabled);
  125. SET_VALUE_INT("record_ttl", setTtl);
  126. return action;
  127. }
  128. std::shared_ptr<Action> HostsConfig::readDelHost(const Json::Value &act, const Json::Value &defaults, int i,
  129. Result<Actions> &res) const
  130. {
  131. auto action = std::make_shared<ActionDelHost>();
  132. SET_VALUE_STRING("domain", setDomain);
  133. SET_VALUE_STRING("host", setHost);
  134. return action;
  135. }
  136. std::shared_ptr<Action> HostsConfig::readAddDomain(const Json::Value &act, const Json::Value &defaults, int i,
  137. Result<Actions> &res) const
  138. {
  139. auto action = std::make_shared<ActionAddDomain>();
  140. SET_VALUE_STRING("domain", setDomain);
  141. SET_VALUE_STRING("soa_ns", setSoaNs);
  142. SET_VALUE_STRING("soa_mail", setSoaMail);
  143. SET_VALUE_INT("soa_refresh", setSoaRefresh);
  144. SET_VALUE_INT("soa_retry", setSoaRetry);
  145. SET_VALUE_INT("soa_expire", setSoaExpire);
  146. SET_VALUE_INT("soa_ttl", setSoaTtl);
  147. SET_VALUE_INT("record_ttl", setTtl);
  148. return action;
  149. }
  150. std::shared_ptr<Action> HostsConfig::readDelDomain(const Json::Value &act, const Json::Value &defaults, int i,
  151. Result<Actions> &res) const
  152. {
  153. auto action = std::make_shared<ActionDelDomain>();
  154. SET_VALUE_STRING("domain", setDomain);
  155. return action;
  156. }