fsck-main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include <iostream>
  19. #include <set>
  20. #include <list>
  21. #include <kopano/CommonUtil.h>
  22. #include <kopano/mapiext.h>
  23. #include <kopano/mapiguidext.h>
  24. #include <kopano/memory.hpp>
  25. #include <mapiutil.h>
  26. #include <mapix.h>
  27. #include <kopano/stringutil.h>
  28. #include "fsck.h"
  29. using namespace KCHL;
  30. static bool ReadYesNoMessage(const std::string &strMessage,
  31. const std::string &strAuto)
  32. {
  33. string strReply;
  34. cout << strMessage << " [yes/no]: ";
  35. if (strAuto.empty())
  36. getline(cin, strReply);
  37. else {
  38. cout << strAuto << endl;
  39. strReply = strAuto;
  40. }
  41. return (strReply[0] == 'y' || strReply[0] == 'Y');
  42. }
  43. static HRESULT DeleteEntry(LPMAPIFOLDER lpFolder,
  44. const SPropValue *lpItemProperty)
  45. {
  46. memory_ptr<ENTRYLIST> lpEntryList;
  47. HRESULT hr = MAPIAllocateBuffer(sizeof(ENTRYLIST), &~lpEntryList);
  48. if (hr != hrSuccess)
  49. goto exit;
  50. hr = MAPIAllocateMore(sizeof(SBinary), lpEntryList, (void**)&lpEntryList->lpbin);
  51. if (hr != hrSuccess)
  52. goto exit;
  53. lpEntryList->cValues = 1;
  54. lpEntryList->lpbin[0].cb = lpItemProperty->Value.bin.cb;
  55. lpEntryList->lpbin[0].lpb = lpItemProperty->Value.bin.lpb;
  56. hr = lpFolder->DeleteMessages(lpEntryList, 0, NULL, 0);
  57. exit:
  58. if (hr == hrSuccess)
  59. cout << "Item deleted." << endl;
  60. else
  61. cout << "Failed to delete entry." << endl;
  62. return hr;
  63. }
  64. static HRESULT FixProperty(LPMESSAGE lpMessage, const std::string &strName,
  65. ULONG ulTag, __UPV Value)
  66. {
  67. HRESULT hr;
  68. SPropValue ErrorProp;
  69. ErrorProp.ulPropTag = ulTag;
  70. ErrorProp.Value = Value;
  71. /* NOTE: Named properties don't have the PT value set by default,
  72. The caller of this function should have taken care of this. */
  73. if (PROP_ID(ulTag) == 0 || PROP_TYPE(ulTag) == 0) {
  74. cout << "Invalid property tag: " << stringify(ulTag, true) << endl;
  75. return MAPI_E_INVALID_PARAMETER;
  76. }
  77. hr = lpMessage->SetProps(1, &ErrorProp, NULL);
  78. if (hr != hrSuccess) {
  79. cout << "Failed to fix broken property." << endl;
  80. return hr;
  81. }
  82. hr = lpMessage->SaveChanges(KEEP_OPEN_READWRITE);
  83. if (hr != hrSuccess)
  84. cout << "Failed to save changes to fix broken property";
  85. return hr;
  86. }
  87. static HRESULT DetectFolderEntryDetails(LPMESSAGE lpMessage, string *lpName,
  88. string *lpClass)
  89. {
  90. HRESULT hr = hrSuccess;
  91. memory_ptr<SPropValue> lpPropertyArray;
  92. ULONG ulPropertyCount;
  93. static constexpr const SizedSPropTagArray(3, PropertyTagArray) =
  94. {3, {PR_SUBJECT_A, PR_NORMALIZED_SUBJECT_A, PR_MESSAGE_CLASS_A}};
  95. hr = lpMessage->GetProps(PropertyTagArray, 0, &ulPropertyCount,
  96. &~lpPropertyArray);
  97. if (FAILED(hr)) {
  98. cout << "Failed to obtain all properties." << endl;
  99. return hr;
  100. }
  101. for (ULONG i = 0; i < ulPropertyCount; ++i) {
  102. if (PROP_TYPE(lpPropertyArray[i].ulPropTag) == PT_ERROR)
  103. continue;
  104. else if (lpPropertyArray[i].ulPropTag == PR_SUBJECT_A)
  105. *lpName = lpPropertyArray[i].Value.lpszA;
  106. else if (lpPropertyArray[i].ulPropTag == PR_NORMALIZED_SUBJECT_A)
  107. *lpName = lpPropertyArray[i].Value.lpszA;
  108. else if (lpPropertyArray[i].ulPropTag == PR_MESSAGE_CLASS_A)
  109. *lpClass = lpPropertyArray[i].Value.lpszA;
  110. }
  111. /*
  112. * The name is allowed to be empty, the class however not.
  113. */
  114. if (lpClass->empty())
  115. cout << "Unable to detect message class.";
  116. else
  117. hr = hrSuccess;
  118. return hr;
  119. }
  120. static HRESULT ProcessFolderEntry(Fsck *lpFsck, LPMAPIFOLDER lpFolder,
  121. LPSRow lpRow)
  122. {
  123. HRESULT hr = hrSuccess;
  124. object_ptr<IMessage> lpMessage;
  125. ULONG ulObjectType = 0;
  126. string strName;
  127. string strClass;
  128. auto lpItemProperty = PCpropFindProp(lpRow->lpProps, lpRow->cValues, PR_ENTRYID);
  129. if (!lpItemProperty) {
  130. cout << "Row does not contain an EntryID." << endl;
  131. goto exit;
  132. }
  133. hr = lpFolder->OpenEntry(lpItemProperty->Value.bin.cb,
  134. reinterpret_cast<ENTRYID *>(lpItemProperty->Value.bin.lpb),
  135. &IID_IMessage, MAPI_MODIFY, &ulObjectType, &~lpMessage);
  136. if (hr != hrSuccess) {
  137. cout << "Failed to open EntryID." << endl;
  138. goto exit;
  139. }
  140. hr = DetectFolderEntryDetails(lpMessage, &strName, &strClass);
  141. if (hr != hrSuccess)
  142. goto exit;
  143. hr = lpFsck->ValidateMessage(lpMessage, strName, strClass);
  144. if (hr != hrSuccess)
  145. goto exit;
  146. exit:
  147. if (hr != hrSuccess)
  148. hr = lpFsck->DeleteMessage(lpFolder, lpItemProperty);
  149. return hr;
  150. }
  151. static HRESULT ProcessFolder(Fsck *lpFsck, LPMAPIFOLDER lpFolder,
  152. const std::string &strName)
  153. {
  154. object_ptr<IMAPITable> lpTable;
  155. ULONG ulCount;
  156. HRESULT hr = lpFolder->GetContentsTable(0, &~lpTable);
  157. if(hr != hrSuccess) {
  158. cout << "Failed to open Folder table." << endl;
  159. return hr;
  160. }
  161. /*
  162. * Check if we have found at least *something*.
  163. */
  164. hr = lpTable->GetRowCount(0, &ulCount);
  165. if(hr != hrSuccess) {
  166. cout << "Failed to count number of rows." << endl;
  167. return hr;
  168. } else if (!ulCount) {
  169. cout << "No entries inside folder." << endl;
  170. return hr;
  171. }
  172. /*
  173. * Loop through each row/entry and validate.
  174. */
  175. while (true) {
  176. rowset_ptr lpRows;
  177. hr = lpTable->QueryRows(20, 0, &~lpRows);
  178. if (hr != hrSuccess)
  179. return hr;
  180. if (lpRows->cRows == 0)
  181. break;
  182. for (ULONG i = 0; i < lpRows->cRows; ++i) {
  183. hr = ProcessFolderEntry(lpFsck, lpFolder, &lpRows->aRow[i]);
  184. if (hr != hrSuccess)
  185. // Move along, nothing to see.
  186. cout << "Failed to validate entry." << endl;
  187. }
  188. }
  189. return hrSuccess;
  190. }
  191. /*
  192. * Fsck implementation.
  193. */
  194. HRESULT Fsck::ValidateMessage(LPMESSAGE lpMessage,
  195. const std::string &strName, const std::string &strClass)
  196. {
  197. cout << "Validating entry: \"" << strName << "\"" << endl;
  198. ++this->ulEntries;
  199. HRESULT hr = this->ValidateItem(lpMessage, strClass);
  200. cout << "Validating of entry \"" << strName << "\" ended" << endl;
  201. return hr;
  202. }
  203. HRESULT Fsck::ValidateFolder(LPMAPIFOLDER lpFolder,
  204. const std::string &strName)
  205. {
  206. cout << "Validating folder \"" << strName << "\"" << endl;
  207. ++this->ulFolders;
  208. HRESULT hr = ProcessFolder(this, lpFolder, strName);
  209. cout << "Validating of folder \"" << strName << "\" ended" << endl;
  210. return hr;
  211. }
  212. HRESULT Fsck::AddMissingProperty(LPMESSAGE lpMessage,
  213. const std::string &strName, ULONG ulTag, __UPV Value)
  214. {
  215. HRESULT hr = hrSuccess;
  216. cout << "Missing property " << strName << endl;
  217. ++this->ulProblems;
  218. if (ReadYesNoMessage("Add missing property?", auto_fix)) {
  219. hr = FixProperty(lpMessage, strName, ulTag, Value);
  220. if (hr == hrSuccess)
  221. ++this->ulFixed;
  222. }
  223. return hr;
  224. }
  225. HRESULT Fsck::ReplaceProperty(LPMESSAGE lpMessage,
  226. const std::string &strName, ULONG ulTag, const std::string &strError,
  227. __UPV Value)
  228. {
  229. HRESULT hr = hrSuccess;
  230. cout << "Invalid property " << strName << " - " << strError << endl;
  231. ++this->ulProblems;
  232. if (ReadYesNoMessage("Fix broken property?", auto_fix)) {
  233. hr = FixProperty(lpMessage, strName, ulTag, Value);
  234. if (hr == hrSuccess)
  235. ++this->ulFixed;
  236. }
  237. return hr;
  238. }
  239. HRESULT Fsck::DeleteRecipientList(LPMESSAGE lpMessage, std::list<unsigned int> &mapiReciptDel, bool &bChanged)
  240. {
  241. ++this->ulProblems;
  242. cout << mapiReciptDel.size() << " duplicate or invalid recipients found. " << endl;
  243. if (!ReadYesNoMessage("Remove duplicate or invalid recipients?", auto_fix))
  244. return hrSuccess;
  245. memory_ptr<ADRLIST> lpMods;
  246. HRESULT hr = MAPIAllocateBuffer(CbNewADRLIST(mapiReciptDel.size()), &~lpMods);
  247. if (hr != hrSuccess)
  248. return hr;
  249. lpMods->cEntries = 0;
  250. for (const auto &recip : mapiReciptDel) {
  251. auto &ent = lpMods->aEntries[lpMods->cEntries];
  252. ent.cValues = 1;
  253. hr = MAPIAllocateMore(sizeof(SPropValue), lpMods, reinterpret_cast<void **>(&ent.rgPropVals));
  254. if (hr != hrSuccess)
  255. return hr;
  256. ent.rgPropVals->ulPropTag = PR_ROWID;
  257. ent.rgPropVals->Value.ul = recip;
  258. }
  259. hr = lpMessage->ModifyRecipients(MODRECIP_REMOVE, lpMods.get());
  260. if (hr != hrSuccess)
  261. return hr;
  262. hr = lpMessage->SaveChanges(KEEP_OPEN_READWRITE);
  263. if (hr != hrSuccess)
  264. return hr;
  265. bChanged = true;
  266. ++this->ulFixed;
  267. return hrSuccess;
  268. }
  269. HRESULT Fsck::DeleteMessage(LPMAPIFOLDER lpFolder,
  270. const SPropValue *lpItemProperty)
  271. {
  272. HRESULT hr = hrSuccess;
  273. if (!ReadYesNoMessage("Delete message?", auto_del))
  274. return hr;
  275. hr = DeleteEntry(lpFolder, lpItemProperty);
  276. if (hr == hrSuccess)
  277. ++this->ulDeleted;
  278. return hr;
  279. }
  280. HRESULT Fsck::ValidateRecursiveDuplicateRecipients(LPMESSAGE lpMessage, bool &bChanged)
  281. {
  282. HRESULT hr = hrSuccess;
  283. bool bSubChanged = false;
  284. object_ptr<IMAPITable> lpTable;
  285. ULONG cRows = 0;
  286. static constexpr const SizedSPropTagArray(2, sptaProps) =
  287. {2, {PR_ATTACH_NUM, PR_ATTACH_METHOD}};
  288. hr = lpMessage->GetAttachmentTable(0, &~lpTable);
  289. if (hr != hrSuccess)
  290. return hr;
  291. hr = lpTable->GetRowCount(0, &cRows);
  292. if (hr != hrSuccess)
  293. return hr;
  294. if (cRows == 0)
  295. goto message;
  296. hr = lpTable->SetColumns(sptaProps, 0);
  297. if (hr != hrSuccess)
  298. return hr;
  299. while (true) {
  300. rowset_ptr pRows;
  301. hr = lpTable->QueryRows(50, 0, &~pRows);
  302. if (hr != hrSuccess)
  303. return hr;
  304. if (pRows->cRows == 0)
  305. break;
  306. for (unsigned int i = 0; i < pRows->cRows; ++i) {
  307. if (pRows->aRow[i].lpProps[1].ulPropTag != PR_ATTACH_METHOD ||
  308. pRows->aRow[i].lpProps[1].Value.ul != ATTACH_EMBEDDED_MSG)
  309. continue;
  310. object_ptr<IAttach> lpAttach;
  311. object_ptr<IMessage> lpSubMessage;
  312. bSubChanged = false;
  313. hr = lpMessage->OpenAttach(pRows->aRow[i].lpProps[0].Value.ul, nullptr, MAPI_BEST_ACCESS, &~lpAttach);
  314. if (hr != hrSuccess)
  315. return hr;
  316. hr = lpAttach->OpenProperty(PR_ATTACH_DATA_OBJ, &IID_IMessage, 0, MAPI_MODIFY, &~lpSubMessage);
  317. if (hr != hrSuccess)
  318. return hr;
  319. hr = ValidateRecursiveDuplicateRecipients(lpSubMessage, bSubChanged);
  320. if (hr != hrSuccess)
  321. return hr;
  322. if (bSubChanged) {
  323. hr = lpAttach->SaveChanges(KEEP_OPEN_READWRITE);
  324. if (hr != hrSuccess)
  325. return hr;
  326. bChanged = bSubChanged;
  327. }
  328. }
  329. }
  330. message:
  331. hr = ValidateDuplicateRecipients(lpMessage, bChanged);
  332. if (hr != hrSuccess)
  333. return hr;
  334. if (bChanged)
  335. lpMessage->SaveChanges(KEEP_OPEN_READWRITE);
  336. return hrSuccess;
  337. }
  338. HRESULT Fsck::ValidateDuplicateRecipients(LPMESSAGE lpMessage, bool &bChanged)
  339. {
  340. object_ptr<IMAPITable> lpTable;
  341. ULONG cRows = 0;
  342. std::set<std::string> mapRecip;
  343. std::list<unsigned int> mapiReciptDel;
  344. std::string strData;
  345. unsigned int i = 0;
  346. static constexpr const SizedSPropTagArray(5, sptaProps) =
  347. {5, {PR_ROWID, PR_DISPLAY_NAME_A, PR_EMAIL_ADDRESS_A,
  348. PR_RECIPIENT_TYPE, PR_ENTRYID}};
  349. HRESULT hr = lpMessage->GetRecipientTable(0, &~lpTable);
  350. if (hr != hrSuccess)
  351. return hr;
  352. hr = lpTable->GetRowCount(0, &cRows);
  353. if (hr != hrSuccess)
  354. return hr;
  355. if (cRows < 1)
  356. // 0 or 1 row not needed to check
  357. return hr;
  358. hr = lpTable->SetColumns(sptaProps, 0);
  359. if (hr != hrSuccess)
  360. return hr;
  361. while (true) {
  362. rowset_ptr pRows;
  363. hr = lpTable->QueryRows(50, 0, &~pRows);
  364. if (hr != hrSuccess)
  365. return hr;
  366. if (pRows->cRows == 0)
  367. break;
  368. for (i = 0; i < pRows->cRows; ++i) {
  369. if (pRows->aRow[i].lpProps[1].ulPropTag != PR_DISPLAY_NAME_A && pRows->aRow[i].lpProps[2].ulPropTag != PR_EMAIL_ADDRESS_A) {
  370. mapiReciptDel.push_back(pRows->aRow[i].lpProps[0].Value.ul);
  371. continue;
  372. }
  373. // Invalid or missing entryid
  374. if (pRows->aRow[i].lpProps[4].ulPropTag != PR_ENTRYID || pRows->aRow[i].lpProps[4].Value.bin.cb == 0) {
  375. mapiReciptDel.push_back(pRows->aRow[i].lpProps[0].Value.ul);
  376. continue;
  377. }
  378. strData.clear();
  379. if (pRows->aRow[i].lpProps[1].ulPropTag == PR_DISPLAY_NAME_A) strData += pRows->aRow[i].lpProps[1].Value.lpszA;
  380. if (pRows->aRow[i].lpProps[2].ulPropTag == PR_EMAIL_ADDRESS_A) strData += pRows->aRow[i].lpProps[2].Value.lpszA;
  381. if (pRows->aRow[i].lpProps[3].ulPropTag == PR_RECIPIENT_TYPE) strData += stringify(pRows->aRow[i].lpProps[3].Value.ul);
  382. auto res = mapRecip.insert(strData);
  383. if (res.second == false)
  384. mapiReciptDel.push_back(pRows->aRow[i].lpProps[0].Value.ul);
  385. }
  386. }
  387. // modify
  388. if (!mapiReciptDel.empty())
  389. hr = DeleteRecipientList(lpMessage, mapiReciptDel, bChanged);
  390. return hr;
  391. }
  392. void Fsck::PrintStatistics(const std::string &title)
  393. {
  394. cout << title << endl;
  395. cout << "\tFolders:\t" << ulFolders << endl;
  396. cout << "\tEntries:\t" << ulEntries << endl;
  397. cout << "\tProblems:\t" << ulProblems << endl;
  398. cout << "\tFixed:\t\t" << ulFixed << endl;
  399. cout << "\tDeleted:\t" << ulDeleted << endl;
  400. }