Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PostfixIntegrate.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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/snf-server";
  52. SnfSnifferFileName = SnfSnifferDirName + "/snfSniffer";
  53. SnfSnifferSampleFileName = "/usr/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. if (!FileExists(SnfSnifferDirName)) {
  105. MkDir(SnfSnifferDirName);
  106. }
  107. SetMode(SnfSnifferDirName, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  108. SetOwnerGroup(SnfSnifferDirName);
  109. Copy(SnfSnifferSampleFileName, SnfSnifferFileName);
  110. SetMode(SnfSnifferFileName, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  111. SetOwnerGroup(SnfSnifferFileName);
  112. }
  113. SaveFile->CreateBackupFile(PostfixMasterCfPath);
  114. Input.open(PostfixMasterCfPath.c_str()); // Read the contents.
  115. if (!Input) {
  116. std::string Temp;
  117. Temp = "Error opening the postfix configuration file " + PostfixMasterCfPath;
  118. Temp += " for reading: ";
  119. Temp += strerror(errno);
  120. throw std::runtime_error(Temp);
  121. }
  122. std::string Line;
  123. while (getline(Input, Line)) {
  124. Content += Line + "\n"; // Copy this line.
  125. if ( (Line.find("smtp") == 0) && (Line.find("inet") != std::string::npos) ) {
  126. if (Verbose()) {
  127. std::cout << " Add\n\n "
  128. << ContentFilterLine
  129. << "\n\n after\n\n"
  130. << Line
  131. << "\n\n in "
  132. << PostfixMasterCfPath << "...\n";
  133. }
  134. Content += ContentFilterLine;
  135. }
  136. }
  137. if (!Input.eof()) { // Should be at end-of-file.
  138. std::string Temp;
  139. Temp = "Error reading the postfix configuration file " + PostfixMasterCfPath;
  140. Temp += ": ";
  141. Temp += strerror(errno);
  142. throw std::runtime_error(Temp);
  143. }
  144. Input.close();
  145. if (Input.bad()) {
  146. std::string Temp;
  147. Temp = "Error closing the postfix configuration file " + PostfixMasterCfPath;
  148. Temp += " after reading: ";
  149. Temp += strerror(errno);
  150. throw std::runtime_error(Temp);
  151. }
  152. if (Verbose()) {
  153. std::cout << " Add\n\n" << ContentFilterSpec << "\n\n to the end of "
  154. << PostfixMasterCfPath << "...\n";
  155. }
  156. Content += ContentFilterSpec;
  157. if (!Explain()) {
  158. std::ofstream Output; // Write the updated contents.
  159. Output.open(PostfixMasterCfPath.c_str(), std::ios::trunc);
  160. if (!Output) {
  161. std::string Temp;
  162. Temp = "Error opening the postfix configuration file " + PostfixMasterCfPath;
  163. Temp += " for writing: ";
  164. Temp += strerror(errno);
  165. throw std::runtime_error(Temp);
  166. }
  167. Output << Content;
  168. if (!Output) {
  169. std::string Temp;
  170. Temp = "Error writing the postfix configuration file " + PostfixMasterCfPath;
  171. Temp += ": ";
  172. Temp += strerror(errno);
  173. throw std::runtime_error(Temp);
  174. }
  175. Output.close();
  176. if (!Output) {
  177. std::string Temp;
  178. Temp = "Error closing the postfix configuration file " + PostfixMasterCfPath;
  179. Temp += " after writing: ";
  180. Temp += strerror(errno);
  181. throw std::runtime_error(Temp);
  182. }
  183. }
  184. }
  185. OutputVerboseEnd();
  186. if (!ReloadMta()) {
  187. std::cerr << "Unable to reload the postfix configuration. Please run "
  188. << "'postfix reload' for the integration with SNFServer to take effect.";
  189. }
  190. }
  191. void
  192. PostfixIntegrate::Unintegrate(FileBackup *SaveFile) {
  193. if (!IsIntegrated()) {
  194. return;
  195. }
  196. std::ifstream Input;
  197. if (Verbose()) {
  198. std::cout << "Remove integration in postfix file " << PostfixMasterCfPath << "--\n";
  199. }
  200. if (!Explain()) {
  201. SaveFile->CreateBackupFile(PostfixMasterCfPath); // Save any existing file.
  202. Input.open(PostfixMasterCfPath.c_str()); // Read the contents.
  203. if (!Input) {
  204. std::string Temp;
  205. Temp = "Error opening the postfix configuration file " + PostfixMasterCfPath;
  206. Temp += " for reading: ";
  207. Temp += strerror(errno);
  208. throw std::runtime_error(Temp);
  209. }
  210. std::ostringstream ContentStream;
  211. ContentStream << Input.rdbuf();
  212. Input.close();
  213. if (!Input) {
  214. std::string Temp;
  215. Temp = "Error closing the postfix configuration file " + PostfixMasterCfPath;
  216. Temp += ": ";
  217. Temp += strerror(errno);
  218. throw std::runtime_error(Temp);
  219. }
  220. std::string Content;
  221. Content = ContentStream.str();
  222. if (Verbose()) {
  223. std::cout << " Remove all occurances of\n\n" << ContentFilterLine << "\n\n"
  224. << " from" << PostfixMasterCfPath << "...\n";
  225. }
  226. std::string::size_type IntegrationBegin = std::string::npos;
  227. while ((IntegrationBegin = Content.find(ContentFilterLine)) != std::string::npos) {
  228. Content.erase(IntegrationBegin, ContentFilterLine.length());
  229. }
  230. if (Verbose()) {
  231. std::cout << " Remove all occurances of\n\n" << ContentFilterSpec << "\n\n"
  232. << " from" << PostfixMasterCfPath << "...\n";
  233. }
  234. IntegrationBegin = std::string::npos;
  235. while ((IntegrationBegin = Content.find(ContentFilterSpec)) != std::string::npos) {
  236. Content.erase(IntegrationBegin, ContentFilterSpec.length());
  237. }
  238. std::ofstream Output; // Write the updated contents.
  239. Output.open(PostfixMasterCfPath.c_str(), std::ios::trunc);
  240. if (!Output) {
  241. std::string Temp;
  242. Temp = "Error opening the postfix configuration file " + PostfixMasterCfPath;
  243. Temp += " for writing: ";
  244. Temp += strerror(errno);
  245. throw std::runtime_error(Temp);
  246. }
  247. Output << Content;
  248. if (!Output) {
  249. std::string Temp;
  250. Temp = "Error writing the postfix configuration file " + PostfixMasterCfPath;
  251. Temp += ": ";
  252. Temp += strerror(errno);
  253. throw std::runtime_error(Temp);
  254. }
  255. Output.close();
  256. if (!Output) {
  257. std::string Temp;
  258. Temp = "Error closing the postfix configuration file " + PostfixMasterCfPath;
  259. Temp += " after writing: ";
  260. Temp += strerror(errno);
  261. throw std::runtime_error(Temp);
  262. }
  263. }
  264. OutputVerboseEnd();
  265. if (!ReloadMta()) {
  266. std::cerr << "Unable to reload the postfix configuration. Please run "
  267. << "'postfix reload' for the integration with SNFServer to take effect.";
  268. }
  269. }
  270. bool
  271. PostfixIntegrate::MtaIsRunningDetected() {
  272. if (Verbose()) {
  273. std::cout << "Checking whether postfix is detected to be running...";
  274. }
  275. bool IsRunningDetected;
  276. IsRunningDetected = (std::system(MtaIsRunningCommand.c_str()) == 0);
  277. if (Verbose()) {
  278. std::cout << (IsRunningDetected ? "yes..." : "no...");
  279. }
  280. OutputVerboseEnd();
  281. return IsRunningDetected;
  282. }
  283. bool
  284. PostfixIntegrate::ReloadMta() {
  285. if (!MtaIsRunningDetected()) {
  286. return true;
  287. }
  288. if (Verbose()) {
  289. std::cout << "Reloading postfix...\n";
  290. std::cout.flush();
  291. }
  292. bool Succeeded;
  293. if (!Explain()) {
  294. Succeeded = (std::system(ReloadMtaCommand.c_str()) == 0);
  295. if (Verbose()) {
  296. std::cout << (Succeeded ? "succeeded..." : "failed...");
  297. }
  298. }
  299. OutputVerboseEnd();
  300. return Succeeded;
  301. }
  302. bool
  303. PostfixIntegrate::IsIntegrated() {
  304. if (Verbose()) {
  305. std::cout << "Checking for any SNFServer integration in the postfix file " << PostfixMasterCfPath << "...";
  306. }
  307. if (!FileExists(PostfixMasterCfPath)) {
  308. if (Verbose()) {
  309. std::cout << "file doesn't exist; postfix is not integrated...";
  310. }
  311. OutputVerboseEnd();
  312. return false;
  313. }
  314. std::ifstream Input;
  315. Input.open(PostfixMasterCfPath.c_str()); // Read the contents.
  316. if (!Input) {
  317. std::string Temp;
  318. Temp = "Error opening the postfix configuration file " + PostfixMasterCfPath;
  319. Temp += " for reading: ";
  320. Temp += strerror(errno);
  321. throw std::runtime_error(Temp);
  322. }
  323. std::ostringstream ContentStream;
  324. ContentStream << Input.rdbuf();
  325. Input.close();
  326. if (!Input) {
  327. std::string Temp;
  328. Temp = "Error closing the postfix configuration file " + PostfixMasterCfPath;
  329. Temp += ": ";
  330. Temp += strerror(errno);
  331. throw std::runtime_error(Temp);
  332. }
  333. std::string Content;
  334. Content = ContentStream.str();
  335. bool FoundContentFilterLine = (Content.find(ContentFilterLine) != std::string::npos);
  336. bool FoundContentFilterSpec = (Content.find(ContentFilterSpec) != std::string::npos);
  337. bool Integrated = (FoundContentFilterLine || FoundContentFilterSpec);
  338. if (Verbose()) {
  339. if (FoundContentFilterLine) {
  340. std::cout << "found\n\n" << ContentFilterLine << "\n\n";
  341. }
  342. if (FoundContentFilterSpec) {
  343. std::cout << "found\n\n" << ContentFilterSpec << "\n\n";
  344. }
  345. if (!Integrated) {
  346. std::cout << "none found...";
  347. }
  348. }
  349. OutputVerboseEnd();
  350. return Integrated;
  351. }
  352. bool
  353. PostfixIntegrate::DefaultIsChrooted() {
  354. return PostfixDefaultIsChrooted;
  355. }
  356. bool
  357. PostfixIntegrate::MtaConfigurationIsChrooted() {
  358. std::string File;
  359. std::ifstream Input;
  360. File = PostfixMasterCfPath;
  361. Input.open(File.c_str());
  362. if (!Input) {
  363. std::string Temp;
  364. Temp = "Error opening postfix configuration file " + File;
  365. Temp += " for reading: ";
  366. Temp += strerror(errno);
  367. throw std::runtime_error(Temp);
  368. }
  369. std::string Line;
  370. bool ConfigurationIsChrooted = false;
  371. while (getline(Input, Line)) {
  372. if (CheckForString(Line, "smtp")) { // Check for smtp line.
  373. std::istringstream Buffer(Line); // Parse buffer line.
  374. std::string Token[8];
  375. for (unsigned int iToken = 0; iToken < 8; iToken++) {
  376. Buffer >> Token[iToken];
  377. }
  378. if ( ("y" == Token[4]) || ("-" == Token[4]) ) {
  379. Input.close();
  380. if (Input.bad()) {
  381. std::string Temp;
  382. Temp = "Error closing the postfix configuration file " + File;
  383. Temp += " after reading: ";
  384. Temp += strerror(errno);
  385. throw std::runtime_error(Temp);
  386. }
  387. return true;
  388. }
  389. }
  390. }
  391. Input.close();
  392. if (Input.bad()) {
  393. std::string Temp;
  394. Temp = "Error closing the rulebase download script file " + File;
  395. Temp += " after reading: ";
  396. Temp += strerror(errno);
  397. throw std::runtime_error(Temp);
  398. }
  399. return false;
  400. }