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.

PostfixIntegrate.cpp 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. }
  95. OutputVerboseEnd();
  96. }
  97. void
  98. PostfixIntegrate::Unintegrate(FileBackup *SaveFile) {
  99. if (!IsIntegrated()) {
  100. return;
  101. }
  102. std::ifstream Input;
  103. if (Verbose()) {
  104. std::cout << "Remove integration in postfix file " << PostfixMainCfPath << "--\n";
  105. }
  106. if (!Explain()) {
  107. SaveFile->CreateBackupFile(PostfixMainCfPath); // Save any existing file.
  108. Input.open(PostfixMainCfPath.c_str()); // Read the contents.
  109. if (!Input) {
  110. std::string Temp;
  111. Temp = "Error opening the postfix configuration file " + PostfixMainCfPath;
  112. Temp += " for reading: ";
  113. Temp += strerror(errno);
  114. throw std::runtime_error(Temp);
  115. }
  116. std::string Content;
  117. std::string Line;
  118. while (getline(Input, Line)) {
  119. if (std::string::npos != Line.find(SnfMilterMainCfSearchString)) { // Check for integration line.
  120. if (Verbose()) {
  121. std::cout << " Remove '" << Line << "'...\n";
  122. }
  123. continue; // Do not copy this line.
  124. }
  125. Content += Line + "\n"; // Copy this line.
  126. }
  127. if (!Input.eof()) { // Should be at end-of-file.
  128. std::string Temp;
  129. Temp = "Error reading the postfix configuration file " + PostfixMainCfPath;
  130. Temp += ": ";
  131. Temp += strerror(errno);
  132. throw std::runtime_error(Temp);
  133. }
  134. Input.close();
  135. if (Input.bad()) {
  136. std::string Temp;
  137. Temp = "Error closing the postfix configuration file " + PostfixMainCfPath;
  138. Temp += " after reading: ";
  139. Temp += strerror(errno);
  140. throw std::runtime_error(Temp);
  141. }
  142. if (!Explain()) {
  143. std::ofstream Output; // Write the updated contents.
  144. Output.open(PostfixMainCfPath.c_str(), std::ios::trunc);
  145. if (!Output) {
  146. std::string Temp;
  147. Temp = "Error opening the postfix configuration file " + PostfixMainCfPath;
  148. Temp += " for writing: ";
  149. Temp += strerror(errno);
  150. throw std::runtime_error(Temp);
  151. }
  152. Output << Content;
  153. if (!Output) {
  154. std::string Temp;
  155. Temp = "Error writing the postfix configuration file " + PostfixMainCfPath;
  156. Temp += ": ";
  157. Temp += strerror(errno);
  158. throw std::runtime_error(Temp);
  159. }
  160. Output.close();
  161. if (!Output) {
  162. std::string Temp;
  163. Temp = "Error closing the postfix configuration file " + PostfixMainCfPath;
  164. Temp += " after writing: ";
  165. Temp += strerror(errno);
  166. throw std::runtime_error(Temp);
  167. }
  168. }
  169. }
  170. OutputVerboseEnd();
  171. }
  172. bool
  173. PostfixIntegrate::MtaIsRunningDetected() {
  174. if (Verbose()) {
  175. std::cout << "Checking whether postfix is detected to be running...";
  176. }
  177. bool IsRunningDetected;
  178. IsRunningDetected = (std::system(MtaIsRunningCommand.c_str()) == 0);
  179. if (Verbose()) {
  180. std::cout << (IsRunningDetected ? "yes" : "no");
  181. }
  182. OutputVerboseEnd();
  183. return IsRunningDetected;
  184. }
  185. bool
  186. PostfixIntegrate::ReloadMta() {
  187. if (Verbose()) {
  188. std::cout << "Reloading postfix...";
  189. }
  190. bool Succeeded;
  191. if (!Explain()) {
  192. Succeeded = (std::system(ReloadMtaCommand.c_str()) == 0);
  193. if (Verbose()) {
  194. std::cout << (Succeeded ? "succeeded" : "failed");
  195. }
  196. }
  197. OutputVerboseEnd();
  198. return Succeeded;
  199. }
  200. bool
  201. PostfixIntegrate::IsIntegrated() {
  202. if (!FileExists(PostfixMainCfPath)) {
  203. return false;
  204. }
  205. bool Integrated = false;
  206. if (Verbose()) {
  207. std::cout << "Checking for any SNFMilter integration in the postfix file " << PostfixMainCfPath << "--\n";
  208. }
  209. std::ifstream Input;
  210. Input.open(PostfixMainCfPath.c_str()); // Read the contents.
  211. if (!Input) {
  212. std::string Temp;
  213. Temp = "Error opening the postfix configuration file " + PostfixMainCfPath;
  214. Temp += " for reading: ";
  215. Temp += strerror(errno);
  216. throw std::runtime_error(Temp);
  217. }
  218. std::string Line;
  219. while (getline(Input, Line)) {
  220. if (std::string::npos != Line.find(SnfMilterMainCfSearchString)) { // Check for integration line.
  221. Integrated = true; // Found it.
  222. break;
  223. }
  224. }
  225. Input.close();
  226. if (Input.bad()) {
  227. std::string Temp;
  228. Temp = "Error closing the postfix configuration file " + PostfixMainCfPath;
  229. Temp += " after reading: ";
  230. Temp += strerror(errno);
  231. throw std::runtime_error(Temp);
  232. }
  233. OutputVerboseEnd();
  234. return Integrated;
  235. }