ECConfigCheck.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <iostream>
  18. #include <algorithm>
  19. #include <sys/stat.h>
  20. #include <cstdlib>
  21. #include <cstdio>
  22. #include <cstring>
  23. #include <kopano/stringutil.h>
  24. using namespace std;
  25. #include "ECConfigCheck.h"
  26. ECConfigCheck::ECConfigCheck(const char *lpszName, const char *lpszConfigFile)
  27. {
  28. m_lpszName = lpszName;
  29. m_lpszConfigFile = lpszConfigFile;
  30. m_bDirty = false;
  31. m_bHosted = false;
  32. m_bMulti = false;
  33. readConfigFile(lpszConfigFile);
  34. }
  35. static string clearCharacters(string s, const string &whitespaces)
  36. {
  37. size_t pos = 0;
  38. /*
  39. * The line is build up like this:
  40. * config_name = bla bla
  41. *
  42. * Whe should clean it in such a way that it resolves to:
  43. * config_name=bla bla
  44. *
  45. * Be careful _not_ to remove any whitespace characters
  46. * within the configuration value itself.
  47. */
  48. pos = s.find_first_not_of(whitespaces);
  49. s.erase(0, pos);
  50. pos = s.find_last_not_of(whitespaces);
  51. if (pos != string::npos)
  52. s.erase(pos + 1, string::npos);
  53. return s;
  54. }
  55. void ECConfigCheck::readConfigFile(const char *lpszConfigFile)
  56. {
  57. FILE *fp = NULL;
  58. char cBuffer[1024];
  59. string strLine;
  60. string strName;
  61. string strValue;
  62. size_t pos;
  63. if (!lpszConfigFile) {
  64. m_bDirty = true;
  65. return;
  66. }
  67. if(!(fp = fopen(lpszConfigFile, "rt"))) {
  68. m_bDirty = true;
  69. return;
  70. }
  71. while (!feof(fp)) {
  72. memset(&cBuffer, 0, sizeof(cBuffer));
  73. if (!fgets(cBuffer, sizeof(cBuffer), fp))
  74. continue;
  75. strLine = string(cBuffer);
  76. /* Skip empty lines any lines which start with # */
  77. if (strLine.empty() || strLine[0] == '#')
  78. continue;
  79. /* Get setting name */
  80. pos = strLine.find('=');
  81. if (pos != string::npos) {
  82. strName = strLine.substr(0, pos);
  83. strValue = strLine.substr(pos + 1);
  84. } else
  85. continue;
  86. strName = clearCharacters(strName, " \t\r\n");
  87. strValue = clearCharacters(strValue, " \t\r\n");
  88. if(!strName.empty())
  89. m_mSettings[strName] = strValue;
  90. }
  91. fclose(fp);
  92. }
  93. bool ECConfigCheck::isDirty()
  94. {
  95. if (m_bDirty)
  96. cerr << "Validation of " << m_lpszName << " failed: file could not be read (" << m_lpszConfigFile << ")" << endl;
  97. return m_bDirty;
  98. }
  99. void ECConfigCheck::setHosted(bool hosted)
  100. {
  101. m_bHosted = hosted;
  102. }
  103. void ECConfigCheck::setMulti(bool multi)
  104. {
  105. m_bMulti = multi;
  106. }
  107. void ECConfigCheck::validate()
  108. {
  109. int warnings = 0;
  110. int errors = 0;
  111. cout << "Starting configuration validation of " << m_lpszName << endl;
  112. for (auto &c : m_lChecks) {
  113. c.hosted = m_bHosted;
  114. c.multi = m_bMulti;
  115. c.value1 = getSetting(c.option1);
  116. c.value2 = getSetting(c.option2);
  117. int retval = 0;
  118. if (c.check)
  119. retval = c.check(&c);
  120. warnings += (retval == CHECK_WARNING);
  121. errors += (retval == CHECK_ERROR);
  122. }
  123. cout << "Validation of " << m_lpszName << " ended with " << warnings << " warnings and " << errors << " errors" << endl;
  124. }
  125. int ECConfigCheck::testMandatory(const config_check_t *check)
  126. {
  127. if (!check->value1.empty())
  128. return CHECK_OK;
  129. printError(check->option1, "required option is empty");
  130. return CHECK_ERROR;
  131. }
  132. int ECConfigCheck::testDirectory(const config_check_t *check)
  133. {
  134. struct stat statfile;
  135. if (check->value1.empty())
  136. return CHECK_OK;
  137. // check if path exists, and is a directory (not a symlink)
  138. if (stat(check->value1.c_str(), &statfile) == 0 && S_ISDIR(statfile.st_mode))
  139. return CHECK_OK;
  140. printError(check->option1, "does not point to existing direcory: \"" + check->value1 + "\"");
  141. return CHECK_ERROR;
  142. }
  143. int ECConfigCheck::testFile(const config_check_t *check)
  144. {
  145. struct stat statfile;
  146. if (check->value1.empty())
  147. return CHECK_OK;
  148. // check if file exists, and is a normal file (not a symlink, or a directory
  149. if (stat(check->value1.c_str(), &statfile) == 0 && S_ISREG(statfile.st_mode))
  150. return CHECK_OK;
  151. printError(check->option1, "does not point to existing file: \"" + check->value1 + "\"");
  152. return CHECK_ERROR;
  153. }
  154. int ECConfigCheck::testUsedWithHosted(const config_check_t *check)
  155. {
  156. if (check->hosted)
  157. return CHECK_OK;
  158. if (check->value1.empty())
  159. return CHECK_OK;
  160. printWarning(check->option1, "Multi-company disabled: this option will be ignored");
  161. return CHECK_WARNING;
  162. }
  163. int ECConfigCheck::testUsedWithoutHosted(const config_check_t *check)
  164. {
  165. if (!check->hosted)
  166. return CHECK_OK;
  167. if (check->value1.empty())
  168. return CHECK_OK;
  169. printWarning(check->option1, "Multi-company enabled: this option will be ignored");
  170. return CHECK_WARNING;
  171. }
  172. int ECConfigCheck::testUsedWithMultiServer(const config_check_t *check)
  173. {
  174. if (check->multi)
  175. return CHECK_OK;
  176. if (check->value1.empty())
  177. return CHECK_OK;
  178. printWarning(check->option1, "Multi-server disabled: this option will be ignored");
  179. return CHECK_WARNING;
  180. }
  181. int ECConfigCheck::testUsedWithoutMultiServer(const config_check_t *check)
  182. {
  183. if (!check->multi)
  184. return CHECK_OK;
  185. if (check->value1.empty())
  186. return CHECK_OK;
  187. printWarning(check->option1, "Multi-server enabled: this option will be ignored");
  188. return CHECK_WARNING;
  189. }
  190. int ECConfigCheck::testCharset(const config_check_t *check)
  191. {
  192. FILE *fp = NULL;
  193. /* When grepping iconv output, all lines have '//' appended,
  194. * additionally all charsets are uppercase */
  195. auto v1 = strToUpper(check->value1);
  196. fp = popen(("iconv -l | grep -x \"" + v1 + "//\"").c_str(), "r");
  197. if (fp == nullptr) {
  198. printWarning(check->option1, "Failed to validate charset");
  199. return CHECK_WARNING;
  200. }
  201. char buffer[50];
  202. string output;
  203. memset(buffer, 0, sizeof(buffer));
  204. if (fgets(buffer, sizeof(buffer), fp) == nullptr) {
  205. printWarning(check->option1, "unable to validate charset: \"" + v1 + "\"");
  206. pclose(fp);
  207. return CHECK_WARNING;
  208. }
  209. output = buffer;
  210. pclose(fp);
  211. if (output.find(v1) == string::npos) {
  212. printError(check->option1, "contains unknown chartype \"" + v1 + "\"");
  213. return CHECK_ERROR;
  214. }
  215. return CHECK_OK;
  216. }
  217. int ECConfigCheck::testBoolean(const config_check_t *check)
  218. {
  219. auto v1 = strToLower(check->value1);
  220. if (v1.empty() || v1 == "true" || v1 == "false" || v1 == "yes" ||
  221. v1 == "no")
  222. return CHECK_OK;
  223. printError(check->option1, "does not contain boolean value: \"" + v1 + "\"");
  224. return CHECK_ERROR;
  225. }
  226. int ECConfigCheck::testNonZero(const config_check_t *check)
  227. {
  228. if (check->value1.empty() || atoi(check->value1.c_str()))
  229. return CHECK_OK;
  230. printError(check->option1, "must contain a positive (non-zero) value: " + check->value1);
  231. return CHECK_ERROR;
  232. }
  233. void ECConfigCheck::addCheck(const config_check_t &check, unsigned int flags)
  234. {
  235. if (flags & CONFIG_MANDATORY) {
  236. if (!check.option1.empty())
  237. addCheck(check.option1, 0, &testMandatory);
  238. if (!check.option2.empty())
  239. addCheck(check.option2, 0, &testMandatory);
  240. }
  241. if (flags & CONFIG_HOSTED_UNUSED) {
  242. if (!check.option1.empty())
  243. addCheck(check.option1, 0, &testUsedWithoutHosted);
  244. if (!check.option2.empty())
  245. addCheck(check.option2, 0, &testUsedWithoutHosted);
  246. }
  247. if (flags & CONFIG_HOSTED_USED) {
  248. if (!check.option1.empty())
  249. addCheck(check.option1, 0, &testUsedWithHosted);
  250. if (!check.option2.empty())
  251. addCheck(check.option2, 0, &testUsedWithHosted);
  252. }
  253. if (flags & CONFIG_MULTI_UNUSED) {
  254. if (!check.option1.empty())
  255. addCheck(check.option1, 0, &testUsedWithoutMultiServer);
  256. if (!check.option2.empty())
  257. addCheck(check.option2, 0, &testUsedWithoutMultiServer);
  258. }
  259. if (flags & CONFIG_MULTI_USED) {
  260. if (!check.option1.empty())
  261. addCheck(check.option1, 0, &testUsedWithMultiServer);
  262. if (!check.option2.empty())
  263. addCheck(check.option2, 0, &testUsedWithMultiServer);
  264. }
  265. m_lChecks.push_back(check);
  266. }
  267. void ECConfigCheck::addCheck(const std::string &option, unsigned int flags,
  268. int (*check)(const config_check_t *check))
  269. {
  270. config_check_t config_check;
  271. config_check.option1 = option;
  272. config_check.option2 = "";
  273. config_check.check = check;
  274. addCheck(config_check, flags);
  275. }
  276. void ECConfigCheck::addCheck(const std::string &option1,
  277. const std::string &option2, unsigned int flags,
  278. int (*check)(const config_check_t *check))
  279. {
  280. config_check_t config_check;
  281. config_check.option1 = option1;
  282. config_check.option2 = option2;
  283. config_check.check = check;
  284. addCheck(config_check, flags);
  285. }
  286. const std::string &ECConfigCheck::getSetting(const std::string &option)
  287. {
  288. return m_mSettings[option];
  289. }
  290. void ECConfigCheck::printError(const std::string &option,
  291. const std::string &message)
  292. {
  293. cerr << "[ERROR] " << option << ": " << message << endl;
  294. }
  295. void ECConfigCheck::printWarning(const std::string &option,
  296. const std::string &message)
  297. {
  298. cerr << "[WARNING] " << option << ": " << message << endl;
  299. }