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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. // /file PostfixIntegrate.cpp
  2. //
  3. // Copyright (C) 2012, 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. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  24. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  25. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  26. void
  27. PostfixIntegrate::SetOperatingSystem(std::string OperatingSystemType) {
  28. MtaIsRunningCommand = "ps axl | grep -v grep | grep -q 'postfix/master'";
  29. PostfixDefaultIsChrooted = false; // Overwritten if postfix is
  30. SnfSnifferDirName = "/usr/sbin"; // by default chrooted.
  31. SnfSnifferFileName = SnfSnifferDirName + "/snfSniffer";
  32. SnfSnifferSampleFileName = SnfSnifferFileName + ".sample";
  33. ContentFilterLine = " -o content_filter=snfilter:dummy\n";
  34. ContentFilterSpec = "snfilter unix - n n - 10 pipe\n";
  35. if ("OpenBSD" == OperatingSystemType) {
  36. PostfixDefaultIsChrooted = true;
  37. SnfSnifferDirName = "/var/spool/postfix/snf-server";
  38. SnfSnifferFileName = SnfSnifferDirName + "/snfSniffer";
  39. SnfSnifferSampleFileName = "/usr/local/sbin/snfSniffer.sample";
  40. ContentFilterSpec += " flags=Rq user=snfuser argv=/snfSniffer\n";
  41. PostfixMainCfPath = "/etc/postfix/main.cf";
  42. PostfixMasterCfPath = "/etc/postfix/master.cf";
  43. ReloadMtaCommand = "/usr/local/sbin/postfix reload";
  44. } else if ("FreeBSD" == OperatingSystemType) {
  45. ContentFilterSpec += " flags=Rq user=snfuser argv=/usr/sbin/snfSniffer\n";
  46. PostfixMainCfPath = "/usr/local/etc/postfix/main.cf";
  47. PostfixMasterCfPath = "/usr/local/etc/postfix/master.cf";
  48. ReloadMtaCommand = "/usr/local/sbin/postfix reload";
  49. } else if ("Ubuntu" == OperatingSystemType) {
  50. PostfixDefaultIsChrooted = true;
  51. SnfSnifferDirName = "/var/spool/postfix";
  52. SnfSnifferFileName = SnfSnifferDirName + "/snfSniffer";
  53. SnfSnifferSampleFileName = "/usr/local/sbin/snfSniffer.sample";
  54. ContentFilterSpec += " flags=Rq user=snfuser argv=/snfSniffer\n";
  55. PostfixMainCfPath = "/etc/postfix/main.cf";
  56. PostfixMasterCfPath = "/etc/postfix/master.cf";
  57. ReloadMtaCommand = "/usr/sbin/postfix reload";
  58. } else if ("RedHat" == OperatingSystemType) {
  59. ContentFilterSpec += " flags=Rq user=snfuser argv=/usr/sbin/snfSniffer\n";
  60. PostfixMainCfPath = "/etc/postfix/main.cf";
  61. PostfixMasterCfPath = "/etc/postfix/master.cf";
  62. ReloadMtaCommand = "/usr/sbin/postfix reload";
  63. } else if ("Suse" == OperatingSystemType) {
  64. ContentFilterSpec += " flags=Rq user=snfuser argv=/usr/sbin/snfSniffer\n";
  65. PostfixMainCfPath = "/etc/postfix/main.cf";
  66. PostfixMasterCfPath = "/etc/postfix/master.cf";
  67. ReloadMtaCommand = "/usr/sbin/postfix reload";
  68. } else {
  69. std::ostringstream Temp;
  70. Temp << "***Error from PostfixIntegrate::SetOperatingSystem: Invalid value of OperatingSystemType: "
  71. << OperatingSystemType;
  72. throw std::runtime_error(Temp.str());
  73. }
  74. ContentFilterSpec += " -f ${sender} -- ${recipient}\n";
  75. }
  76. void
  77. PostfixIntegrate::Integrate(FileBackup *SaveFile) {
  78. if (IsIntegrated()) {
  79. return;
  80. }
  81. // Check whether the chroot configuration is as expected.
  82. bool IsChrooted;
  83. IsChrooted = MtaConfigurationIsChrooted();
  84. if (IsChrooted != PostfixDefaultIsChrooted) {
  85. std::string Temp;
  86. Temp = "Error--postfix must be configured to run ";
  87. Temp += (PostfixDefaultIsChrooted ? "" : "not ");
  88. Temp += "chrooted, which is the default for this operating system. ";
  89. Temp += "postfix was detected to be configured to run ";
  90. Temp += (IsChrooted ? "" : "not ");
  91. Temp += "chrooted.";
  92. Temp += strerror(errno);
  93. throw std::runtime_error(Temp);
  94. }
  95. std::ifstream Input;
  96. if (Verbose()) {
  97. std::cout << "Integrate with postfix...\n";
  98. }
  99. std::string Content;
  100. if (!Explain()) {
  101. if (!FileExists(SnfSnifferFileName)) { // Create SnfSniffer script
  102. // if it doesn't exist.
  103. SaveFile->CreateBackupFile(SnfSnifferFileName);
  104. MkDir(SnfSnifferDirName);
  105. SetMode(SnfSnifferDirName, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  106. SetOwnerGroup(SnfSnifferDirName);
  107. Copy(SnfSnifferSampleFileName, SnfSnifferFileName);
  108. SetMode(SnfSnifferFileName, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  109. SetOwnerGroup(SnfSnifferFileName);
  110. }
  111. SaveFile->CreateBackupFile(PostfixMasterCfPath);
  112. Input.open(PostfixMasterCfPath.c_str()); // Read the contents.
  113. if (!Input) {
  114. std::string Temp;
  115. Temp = "Error opening the postfix configuration file " + PostfixMasterCfPath;
  116. Temp += " for reading: ";
  117. Temp += strerror(errno);
  118. throw std::runtime_error(Temp);
  119. }
  120. std::string Line;
  121. while (getline(Input, Line)) {
  122. Content += Line + "\n"; // Copy this line.
  123. if (Line.find("smtp") == 0) {
  124. if (Verbose()) {
  125. std::cout << " Add\n\n " << ContentFilterLine << "\n\n after 'smtp' line in "
  126. << PostfixMasterCfPath << "...\n";
  127. }
  128. Content += ContentFilterLine + "\n";
  129. }
  130. }
  131. if (!Input.eof()) { // Should be at end-of-file.
  132. std::string Temp;
  133. Temp = "Error reading the postfix configuration file " + PostfixMasterCfPath;
  134. Temp += ": ";
  135. Temp += strerror(errno);
  136. throw std::runtime_error(Temp);
  137. }
  138. Input.close();
  139. if (Input.bad()) {
  140. std::string Temp;
  141. Temp = "Error closing the postfix configuration file " + PostfixMasterCfPath;
  142. Temp += " after reading: ";
  143. Temp += strerror(errno);
  144. throw std::runtime_error(Temp);
  145. }
  146. if (Verbose()) {
  147. std::cout << " Add\n\n " << ContentFilterSpec << "\n\n to the end of "
  148. << PostfixMasterCfPath << "...\n";
  149. }
  150. Content += ContentFilterSpec;
  151. if (!Explain()) {
  152. std::ofstream Output; // Write the updated contents.
  153. Output.open(PostfixMasterCfPath.c_str(), std::ios::trunc);
  154. if (!Output) {
  155. std::string Temp;
  156. Temp = "Error opening the postfix configuration file " + PostfixMasterCfPath;
  157. Temp += " for writing: ";
  158. Temp += strerror(errno);
  159. throw std::runtime_error(Temp);
  160. }
  161. Output << Content;
  162. if (!Output) {
  163. std::string Temp;
  164. Temp = "Error writing the postfix configuration file " + PostfixMasterCfPath;
  165. Temp += ": ";
  166. Temp += strerror(errno);
  167. throw std::runtime_error(Temp);
  168. }
  169. Output.close();
  170. if (!Output) {
  171. std::string Temp;
  172. Temp = "Error closing the postfix configuration file " + PostfixMasterCfPath;
  173. Temp += " after writing: ";
  174. Temp += strerror(errno);
  175. throw std::runtime_error(Temp);
  176. }
  177. }
  178. }
  179. OutputVerboseEnd();
  180. if (!ReloadMta()) {
  181. std::cerr << "Unable to reload the postfix configuration. Please run "
  182. << "'postfix reload' for the integration with SNFServer to take effect.";
  183. }
  184. }
  185. void
  186. PostfixIntegrate::Unintegrate(FileBackup *SaveFile) {
  187. if (!IsIntegrated()) {
  188. return;
  189. }
  190. std::ifstream Input;
  191. if (Verbose()) {
  192. std::cout << "Remove integration in postfix file " << PostfixMasterCfPath << "--\n";
  193. }
  194. if (!Explain()) {
  195. SaveFile->CreateBackupFile(PostfixMasterCfPath); // Save any existing file.
  196. Input.open(PostfixMasterCfPath.c_str()); // Read the contents.
  197. if (!Input) {
  198. std::string Temp;
  199. Temp = "Error opening the postfix configuration file " + PostfixMasterCfPath;
  200. Temp += " for reading: ";
  201. Temp += strerror(errno);
  202. throw std::runtime_error(Temp);
  203. }
  204. std::ostringstream ContentStream;
  205. ContentStream << Input.rdbuf();
  206. Input.close();
  207. if (!Input) {
  208. std::string Temp;
  209. Temp = "Error closing the postfix configuration file " + PostfixMasterCfPath;
  210. Temp += ": ";
  211. Temp += strerror(errno);
  212. throw std::runtime_error(Temp);
  213. }
  214. std::string Content;
  215. Content = ContentStream.str();
  216. if (Verbose()) {
  217. std::cout << " Remove all occurances of\n\n" << ContentFilterLine << "\n\n"
  218. << " from" << PostfixMasterCfPath << "...\n";
  219. }
  220. std::string::size_type IntegrationBegin = std::string::npos;
  221. while ((IntegrationBegin = Content.find(ContentFilterLine)) != std::string::npos) {
  222. Content.erase(IntegrationBegin, ContentFilterLine.length());
  223. }
  224. if (Verbose()) {
  225. std::cout << " Remove all occurances of\n\n" << ContentFilterSpec << "\n\n"
  226. << " from" << PostfixMasterCfPath << "...\n";
  227. }
  228. IntegrationBegin = std::string::npos;
  229. while ((IntegrationBegin = Content.find(ContentFilterSpec)) != std::string::npos) {
  230. Content.erase(IntegrationBegin, ContentFilterSpec.length());
  231. }
  232. }
  233. OutputVerboseEnd();
  234. if (!ReloadMta()) {
  235. std::cerr << "Unable to reload the postfix configuration. Please run "
  236. << "'postfix reload' for the integration with SNFServer to take effect.";
  237. }
  238. }
  239. bool
  240. PostfixIntegrate::MtaIsRunningDetected() {
  241. if (Verbose()) {
  242. std::cout << "Checking whether postfix is detected to be running...";
  243. }
  244. bool IsRunningDetected;
  245. IsRunningDetected = (std::system(MtaIsRunningCommand.c_str()) == 0);
  246. if (Verbose()) {
  247. std::cout << (IsRunningDetected ? "yes..." : "no...");
  248. }
  249. OutputVerboseEnd();
  250. return IsRunningDetected;
  251. }
  252. bool
  253. PostfixIntegrate::ReloadMta() {
  254. if (!MtaIsRunningDetected()) {
  255. return true;
  256. }
  257. if (Verbose()) {
  258. std::cout << "Reloading postfix...\n";
  259. std::cout.flush();
  260. }
  261. bool Succeeded;
  262. if (!Explain()) {
  263. Succeeded = (std::system(ReloadMtaCommand.c_str()) == 0);
  264. if (Verbose()) {
  265. std::cout << (Succeeded ? "succeeded..." : "failed...");
  266. }
  267. }
  268. OutputVerboseEnd();
  269. return Succeeded;
  270. }
  271. bool
  272. PostfixIntegrate::IsIntegrated() {
  273. if (Verbose()) {
  274. std::cout << "Checking for any SNFServer integration in the postfix file " << PostfixMasterCfPath << "...";
  275. }
  276. if (!FileExists(PostfixMasterCfPath)) {
  277. if (Verbose()) {
  278. std::cout << "file doesn't exist; postfix is not integrated...";
  279. }
  280. OutputVerboseEnd();
  281. return false;
  282. }
  283. std::ifstream Input;
  284. Input.open(PostfixMasterCfPath.c_str()); // Read the contents.
  285. if (!Input) {
  286. std::string Temp;
  287. Temp = "Error opening the postfix configuration file " + PostfixMasterCfPath;
  288. Temp += " for reading: ";
  289. Temp += strerror(errno);
  290. throw std::runtime_error(Temp);
  291. }
  292. std::ostringstream ContentStream;
  293. ContentStream << Input.rdbuf();
  294. Input.close();
  295. if (!Input) {
  296. std::string Temp;
  297. Temp = "Error closing the postfix configuration file " + PostfixMasterCfPath;
  298. Temp += ": ";
  299. Temp += strerror(errno);
  300. throw std::runtime_error(Temp);
  301. }
  302. std::string Content;
  303. Content = ContentStream.str();
  304. bool FoundContentFilterLine = (Content.find(ContentFilterLine) != std::string::npos);
  305. bool FoundContentFilterSpec = (Content.find(ContentFilterSpec) != std::string::npos);
  306. bool Integrated = (FoundContentFilterLine || FoundContentFilterSpec);
  307. if (Verbose()) {
  308. if (FoundContentFilterLine) {
  309. std::cout << "found\n\n" << ContentFilterLine << "\n\n";
  310. }
  311. if (FoundContentFilterSpec) {
  312. std::cout << "found\n\n" << ContentFilterSpec << "\n\n";
  313. }
  314. if (!Integrated) {
  315. } else {
  316. std::cout << "none found...";
  317. }
  318. }
  319. OutputVerboseEnd();
  320. return Integrated;
  321. }
  322. bool
  323. PostfixIntegrate::DefaultIsChrooted() {
  324. return PostfixDefaultIsChrooted;
  325. }
  326. bool
  327. PostfixIntegrate::MtaConfigurationIsChrooted() {
  328. std::string File;
  329. std::ifstream Input;
  330. File = PostfixMasterCfPath;
  331. Input.open(File.c_str());
  332. if (!Input) {
  333. std::string Temp;
  334. Temp = "Error opening postfix configuration file " + File;
  335. Temp += " for reading: ";
  336. Temp += strerror(errno);
  337. throw std::runtime_error(Temp);
  338. }
  339. std::string Line;
  340. bool ConfigurationIsChrooted = false;
  341. while (getline(Input, Line)) {
  342. if (CheckForString(Line, "smtp")) { // Check for smtp line.
  343. std::istringstream Buffer(Line); // Parse buffer line.
  344. std::string Token[8];
  345. for (unsigned int iToken = 0; iToken < 8; iToken++) {
  346. Buffer >> Token[iToken];
  347. }
  348. if ( ("y" == Token[4]) || ("-" == Token[4]) ) {
  349. Input.close();
  350. if (Input.bad()) {
  351. std::string Temp;
  352. Temp = "Error closing the postfix configuration file " + File;
  353. Temp += " after reading: ";
  354. Temp += strerror(errno);
  355. throw std::runtime_error(Temp);
  356. }
  357. return true;
  358. }
  359. }
  360. }
  361. Input.close();
  362. if (Input.bad()) {
  363. std::string Temp;
  364. Temp = "Error closing the rulebase download script file " + File;
  365. Temp += " after reading: ";
  366. Temp += strerror(errno);
  367. throw std::runtime_error(Temp);
  368. }
  369. return false;
  370. }