Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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