Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PostfixIntegrate.cpp 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 <errno.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/types.h>
  15. #include <pwd.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <unistd.h>
  19. #include <iostream>
  20. #include <exception>
  21. #include <stdexcept>
  22. #include <sstream>
  23. #include <fstream>
  24. #include <vector>
  25. #include "PostfixIntegrate.hpp"
  26. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
  28. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  29. const std::string SnfMilterMainCfSearchString("Added by PostfixIntegrate");
  30. const std::string SnfMilterMainCfIntegrationString("smtpd_milters = unix:/var/snf-milter/socket $smtpd_milters # Added by PostfixIntegrate");
  31. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  32. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  33. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. void
  35. PostfixIntegrate::SetOperatingSystem(std::string OperatingSystemType) {
  36. if ("OpenBSD" == OperatingSystemType) {
  37. PostfixMainCfPath = "/usr/local/etc/postfix/main.cf";
  38. PostfixMasterCfPath = "/usr/local/etc/postfix/master.cf";
  39. } else if ("FreeBSD" == OperatingSystemType) {
  40. PostfixMainCfPath = "/etc/postfix/main.cf";
  41. PostfixMasterCfPath = "/etc/postfix/master.cf";
  42. } else if ("Ubuntu" == OperatingSystemType) {
  43. PostfixMainCfPath = "/etc/postfix/main.cf";
  44. PostfixMasterCfPath = "/etc/postfix/master.cf";
  45. } else if ("RedHat" == OperatingSystemType) {
  46. PostfixMainCfPath = "/etc/postfix/main.cf";
  47. PostfixMasterCfPath = "/etc/postfix/master.cf";
  48. } else if ("Suse" == OperatingSystemType) {
  49. PostfixMainCfPath = "/etc/postfix/main.cf";
  50. PostfixMasterCfPath = "/etc/postfix/master.cf";
  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. std::ofstream Output; // Append the configuration.
  69. Output.open(PostfixMainCfPath.c_str(), std::ios::app);
  70. if (!Output) {
  71. std::string Temp;
  72. Temp = "Error opening the postfix configuration file " + PostfixMainCfPath;
  73. Temp += " for writing: ";
  74. Temp += strerror(errno);
  75. throw std::runtime_error(Temp);
  76. }
  77. Output << SnfMilterMainCfIntegrationString << "\n";
  78. if (!Output) {
  79. std::string Temp;
  80. Temp = "Error appending to the postfix configuration file " + PostfixMainCfPath;
  81. Temp += ": ";
  82. Temp += strerror(errno);
  83. throw std::runtime_error(Temp);
  84. }
  85. Output.close();
  86. if (!Output) {
  87. std::string Temp;
  88. Temp = "Error closing the postfix configuration file " + PostfixMainCfPath;
  89. Temp += " after appending: ";
  90. Temp += strerror(errno);
  91. throw std::runtime_error(Temp);
  92. }
  93. }
  94. OutputVerboseEnd();
  95. }
  96. void
  97. PostfixIntegrate::Unintegrate(FileBackup *SaveFile) {
  98. if (!IsIntegrated()) {
  99. return;
  100. }
  101. std::ifstream Input;
  102. if (Verbose()) {
  103. std::cout << "Remove integration in postfix file " << PostfixMainCfPath << "--\n";
  104. }
  105. Input.open(PostfixMainCfPath.c_str()); // Read the contents.
  106. if (!Input) {
  107. std::string Temp;
  108. Temp = "Error opening the postfix configuration file " + PostfixMainCfPath;
  109. Temp += " for reading: ";
  110. Temp += strerror(errno);
  111. throw std::runtime_error(Temp);
  112. }
  113. std::string Content;
  114. std::string Line;
  115. while (getline(Input, Line)) {
  116. if (std::string::npos != Line.find(SnfMilterMainCfSearchString)) { // Check for integration line.
  117. if (Verbose()) {
  118. std::cout << " Remove '" << Line << "'...\n";
  119. }
  120. continue; // Do not copy this line.
  121. }
  122. Content += Line + "\n"; // Copy this line.
  123. }
  124. if (!Input.eof()) { // Should be at end-of-file.
  125. std::string Temp;
  126. Temp = "Error reading the postfix configuration file " + PostfixMainCfPath;
  127. Temp += ": ";
  128. Temp += strerror(errno);
  129. throw std::runtime_error(Temp);
  130. }
  131. Input.close();
  132. if (Input.bad()) {
  133. std::string Temp;
  134. Temp = "Error closing the postfix configuration file " + PostfixMainCfPath;
  135. Temp += " after reading: ";
  136. Temp += strerror(errno);
  137. throw std::runtime_error(Temp);
  138. }
  139. if (!Explain()) {
  140. std::ofstream Output; // Write the updated contents.
  141. Output.open(PostfixMainCfPath.c_str(), std::ios::trunc);
  142. if (!Output) {
  143. std::string Temp;
  144. Temp = "Error opening the postfix configuration file " + PostfixMainCfPath;
  145. Temp += " for writing: ";
  146. Temp += strerror(errno);
  147. throw std::runtime_error(Temp);
  148. }
  149. Output << Content;
  150. if (!Output) {
  151. std::string Temp;
  152. Temp = "Error writing the postfix configuration file " + PostfixMainCfPath;
  153. Temp += ": ";
  154. Temp += strerror(errno);
  155. throw std::runtime_error(Temp);
  156. }
  157. Output.close();
  158. if (!Output) {
  159. std::string Temp;
  160. Temp = "Error closing the postfix configuration file " + PostfixMainCfPath;
  161. Temp += " after writing: ";
  162. Temp += strerror(errno);
  163. throw std::runtime_error(Temp);
  164. }
  165. }
  166. OutputVerboseEnd();
  167. }
  168. bool
  169. PostfixIntegrate::IsIntegrated() {
  170. if (!FileExists(PostfixMainCfPath)) {
  171. return false;
  172. }
  173. bool Integrated = false;
  174. if (Verbose()) {
  175. std::cout << "Checking for any SNFMilter integration in the postfix file " << PostfixMainCfPath << "--\n";
  176. }
  177. std::ifstream Input;
  178. Input.open(PostfixMainCfPath.c_str()); // Read the contents.
  179. if (!Input) {
  180. std::string Temp;
  181. Temp = "Error opening the postfix configuration file " + PostfixMainCfPath;
  182. Temp += " for reading: ";
  183. Temp += strerror(errno);
  184. throw std::runtime_error(Temp);
  185. }
  186. std::string Line;
  187. while (getline(Input, Line)) {
  188. if (std::string::npos != Line.find(SnfMilterMainCfSearchString)) { // Check for integration line.
  189. Integrated = true; // Found it.
  190. break;
  191. }
  192. }
  193. Input.close();
  194. if (Input.bad()) {
  195. std::string Temp;
  196. Temp = "Error closing the postfix configuration file " + PostfixMainCfPath;
  197. Temp += " after reading: ";
  198. Temp += strerror(errno);
  199. throw std::runtime_error(Temp);
  200. }
  201. OutputVerboseEnd();
  202. return Integrated;
  203. }