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

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