Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PostfixIntegrate.cpp 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // /file PostfixIntegrate.cpp
  2. //
  3. // Copyright (C) 2011, ARM Research Labs, LLC.
  4. // See www.armresearch.com for the copyright terms.
  5. //
  6. // This file contains the functions for PostfixIntegrate.
  7. //
  8. // $Id$
  9. //
  10. ///////////////////////////////////////////////////////////////////////////////////////////////////
  11. #include <cstdlib>
  12. #include <cerrno>
  13. #include <cstring>
  14. #include <iostream>
  15. #include <exception>
  16. #include <stdexcept>
  17. #include <sstream>
  18. #include <fstream>
  19. #include "PostfixIntegrate.hpp"
  20. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  21. // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
  22. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  23. const std::string SnfMilterMainCfSearchString("Added by PostfixIntegrate");
  24. const std::string SnfMilterMainCfIntegrationString("smtpd_milters = unix:/var/snf-milter/socket $smtpd_milters # Added by PostfixIntegrate");
  25. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  26. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  27. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  28. void
  29. PostfixIntegrate::SetOperatingSystem(std::string OperatingSystemType) {
  30. MtaIsRunningCommand = "ps -axl root | grep -v grep | grep -q 'postfix/master'";
  31. if ("OpenBSD" == OperatingSystemType) {
  32. PostfixMainCfPath = "/usr/local/etc/postfix/main.cf";
  33. PostfixMasterCfPath = "/usr/local/etc/postfix/master.cf";
  34. ReloadMtaCommand = "/usr/local/sbin/postfix reload";
  35. } else if ("FreeBSD" == OperatingSystemType) {
  36. PostfixMainCfPath = "/etc/postfix/main.cf";
  37. PostfixMasterCfPath = "/etc/postfix/master.cf";
  38. ReloadMtaCommand = "/usr/local/sbin/postfix reload";
  39. } else if ("Ubuntu" == OperatingSystemType) {
  40. PostfixMainCfPath = "/etc/postfix/main.cf";
  41. PostfixMasterCfPath = "/etc/postfix/master.cf";
  42. ReloadMtaCommand = "/usr/sbin/postfix reload";
  43. } else if ("RedHat" == OperatingSystemType) {
  44. PostfixMainCfPath = "/etc/postfix/main.cf";
  45. PostfixMasterCfPath = "/etc/postfix/master.cf";
  46. ReloadMtaCommand = "/usr/sbin/postfix reload";
  47. } else if ("Suse" == OperatingSystemType) {
  48. PostfixMainCfPath = "/etc/postfix/main.cf";
  49. PostfixMasterCfPath = "/etc/postfix/master.cf";
  50. ReloadMtaCommand = "/usr/sbin/postfix reload";
  51. } else {
  52. std::ostringstream Temp;
  53. Temp << "***Error from PostfixIntegrate::SetOperatingSystem: Invalid value of OperatingSystemType: "
  54. << OperatingSystemType;
  55. throw std::runtime_error(Temp.str());
  56. }
  57. }
  58. void
  59. PostfixIntegrate::Integrate(FileBackup *SaveFile) {
  60. if (IsIntegrated()) {
  61. return;
  62. }
  63. if (Verbose()) {
  64. std::cout << "Add to postfix file " << PostfixMainCfPath << ": '"
  65. << SnfMilterMainCfIntegrationString << "'...";
  66. }
  67. if (!Explain()) {
  68. SaveFile->CreateBackupFile(PostfixMainCfPath); // Save any existing file.
  69. std::ofstream Output; // Append the configuration.
  70. Output.open(PostfixMainCfPath.c_str(), std::ios::app);
  71. if (!Output) {
  72. std::string Temp;
  73. Temp = "Error opening the postfix configuration file " + PostfixMainCfPath;
  74. Temp += " for writing: ";
  75. Temp += strerror(errno);
  76. throw std::runtime_error(Temp);
  77. }
  78. Output << SnfMilterMainCfIntegrationString << "\n";
  79. if (!Output) {
  80. std::string Temp;
  81. Temp = "Error appending to the postfix configuration file " + PostfixMainCfPath;
  82. Temp += ": ";
  83. Temp += strerror(errno);
  84. throw std::runtime_error(Temp);
  85. }
  86. Output.close();
  87. if (!Output) {
  88. std::string Temp;
  89. Temp = "Error closing the postfix configuration file " + PostfixMainCfPath;
  90. Temp += " after appending: ";
  91. Temp += strerror(errno);
  92. throw std::runtime_error(Temp);
  93. }
  94. if (MtaIsRunningDetected()) {
  95. ReloadMta();
  96. }
  97. }
  98. OutputVerboseEnd();
  99. }
  100. void
  101. PostfixIntegrate::Unintegrate(FileBackup *SaveFile) {
  102. if (!IsIntegrated()) {
  103. return;
  104. }
  105. std::ifstream Input;
  106. if (Verbose()) {
  107. std::cout << "Remove integration in postfix file " << PostfixMainCfPath << "--\n";
  108. }
  109. if (!Explain()) {
  110. SaveFile->CreateBackupFile(PostfixMainCfPath); // Save any existing file.
  111. Input.open(PostfixMainCfPath.c_str()); // Read the contents.
  112. if (!Input) {
  113. std::string Temp;
  114. Temp = "Error opening the postfix configuration file " + PostfixMainCfPath;
  115. Temp += " for reading: ";
  116. Temp += strerror(errno);
  117. throw std::runtime_error(Temp);
  118. }
  119. std::string Content;
  120. std::string Line;
  121. while (getline(Input, Line)) {
  122. if (std::string::npos != Line.find(SnfMilterMainCfSearchString)) { // Check for integration line.
  123. if (Verbose()) {
  124. std::cout << " Remove '" << Line << "'...\n";
  125. }
  126. continue; // Do not copy this line.
  127. }
  128. Content += Line + "\n"; // Copy this line.
  129. }
  130. if (!Input.eof()) { // Should be at end-of-file.
  131. std::string Temp;
  132. Temp = "Error reading the postfix configuration file " + PostfixMainCfPath;
  133. Temp += ": ";
  134. Temp += strerror(errno);
  135. throw std::runtime_error(Temp);
  136. }
  137. Input.close();
  138. if (Input.bad()) {
  139. std::string Temp;
  140. Temp = "Error closing the postfix configuration file " + PostfixMainCfPath;
  141. Temp += " after reading: ";
  142. Temp += strerror(errno);
  143. throw std::runtime_error(Temp);
  144. }
  145. if (!Explain()) {
  146. std::ofstream Output; // Write the updated contents.
  147. Output.open(PostfixMainCfPath.c_str(), std::ios::trunc);
  148. if (!Output) {
  149. std::string Temp;
  150. Temp = "Error opening the postfix configuration file " + PostfixMainCfPath;
  151. Temp += " for writing: ";
  152. Temp += strerror(errno);
  153. throw std::runtime_error(Temp);
  154. }
  155. Output << Content;
  156. if (!Output) {
  157. std::string Temp;
  158. Temp = "Error writing the postfix configuration file " + PostfixMainCfPath;
  159. Temp += ": ";
  160. Temp += strerror(errno);
  161. throw std::runtime_error(Temp);
  162. }
  163. Output.close();
  164. if (!Output) {
  165. std::string Temp;
  166. Temp = "Error closing the postfix configuration file " + PostfixMainCfPath;
  167. Temp += " after writing: ";
  168. Temp += strerror(errno);
  169. throw std::runtime_error(Temp);
  170. }
  171. }
  172. if (MtaIsRunningDetected()) {
  173. ReloadMta();
  174. }
  175. }
  176. OutputVerboseEnd();
  177. }
  178. bool
  179. PostfixIntegrate::MtaIsRunningDetected() {
  180. if (Verbose()) {
  181. std::cout << "Checking whether postfix is detected to be running...";
  182. }
  183. bool IsRunningDetected;
  184. IsRunningDetected = (std::system(MtaIsRunningCommand.c_str()) == 0);
  185. if (Verbose()) {
  186. std::cout << (IsRunningDetected ? "yes" : "no");
  187. }
  188. OutputVerboseEnd();
  189. return IsRunningDetected;
  190. }
  191. bool
  192. PostfixIntegrate::ReloadMta() {
  193. if (Verbose()) {
  194. std::cout << "Reloading postfix...";
  195. }
  196. bool Failed;
  197. if (!Explain()) {
  198. Failed = (std::system(ReloadMtaCommand.c_str()) == 0);
  199. if (Verbose()) {
  200. std::cout << (Failed ? "failed..." : "succeeded...");
  201. }
  202. }
  203. OutputVerboseEnd();
  204. return !Failed;
  205. }
  206. bool
  207. PostfixIntegrate::IsIntegrated() {
  208. if (Verbose()) {
  209. std::cout << "Checking for any SNFMilter integration in the postfix file " << PostfixMainCfPath << "...";
  210. }
  211. if (!FileExists(PostfixMainCfPath)) {
  212. if (Verbose()) {
  213. std::cout << "file doesn't exist; postfix is not integrated...";
  214. }
  215. OutputVerboseEnd();
  216. return false;
  217. }
  218. bool Integrated = false;
  219. std::ifstream Input;
  220. Input.open(PostfixMainCfPath.c_str()); // Read the contents.
  221. if (!Input) {
  222. std::string Temp;
  223. Temp = "Error opening the postfix configuration file " + PostfixMainCfPath;
  224. Temp += " for reading: ";
  225. Temp += strerror(errno);
  226. throw std::runtime_error(Temp);
  227. }
  228. std::string Line;
  229. while (getline(Input, Line)) {
  230. if (std::string::npos != Line.find(SnfMilterMainCfSearchString)) { // Check for integration line.
  231. Integrated = true; // Found it.
  232. if (Verbose()) {
  233. std::cout << "found '" << Line << "'...";
  234. }
  235. break;
  236. }
  237. }
  238. Input.close();
  239. if (Input.bad()) {
  240. std::string Temp;
  241. Temp = "Error closing the postfix configuration file " + PostfixMainCfPath;
  242. Temp += " after reading: ";
  243. Temp += strerror(errno);
  244. throw std::runtime_error(Temp);
  245. }
  246. OutputVerboseEnd();
  247. return Integrated;
  248. }