Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PostfixIntegrate.cpp 16KB

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