ECABLogon.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. #include <new>
  17. #include <kopano/platform.h>
  18. #include <kopano/ECInterfaceDefs.h>
  19. #include <mapiutil.h>
  20. #include "kcore.hpp"
  21. #include <kopano/ECGuid.h>
  22. #include <kopano/memory.hpp>
  23. #include <edkguid.h>
  24. #include "ECABLogon.h"
  25. #include "ECABContainer.h"
  26. #include "ECMailUser.h"
  27. #include "ECDistList.h"
  28. #include <kopano/ECDebug.h>
  29. #include "WSTransport.h"
  30. #include <kopano/Util.h>
  31. #include "Mem.h"
  32. #include <kopano/stringutil.h>
  33. #include "pcutil.hpp"
  34. using namespace KCHL;
  35. ECABLogon::ECABLogon(LPMAPISUP lpMAPISup, WSTransport *lpTransport,
  36. ULONG ulProfileFlags, GUID *lpGUID) :
  37. ECUnknown("IABLogon"), m_lpMAPISup(lpMAPISup),
  38. m_lpTransport(lpTransport)
  39. {
  40. // The 'legacy' guid used normally (all AB entryIDs have this GUID)
  41. m_guid = MUIDECSAB;
  42. // The specific GUID for *this* addressbook provider, if available
  43. m_ABPGuid = (lpGUID != nullptr) ? *lpGUID : GUID_NULL;
  44. if(m_lpTransport)
  45. m_lpTransport->AddRef();
  46. if(m_lpMAPISup)
  47. m_lpMAPISup->AddRef();
  48. if (! (ulProfileFlags & EC_PROFILE_FLAGS_NO_NOTIFICATIONS))
  49. ECNotifyClient::Create(MAPI_ADDRBOOK, this, ulProfileFlags, lpMAPISup, &m_lpNotifyClient);
  50. }
  51. ECABLogon::~ECABLogon()
  52. {
  53. if(m_lpTransport)
  54. m_lpTransport->HrLogOff();
  55. // Disable all advises
  56. if(m_lpNotifyClient)
  57. m_lpNotifyClient->ReleaseAll();
  58. if(m_lpNotifyClient)
  59. m_lpNotifyClient->Release();
  60. if (m_lpMAPISup != nullptr)
  61. m_lpMAPISup->Release();
  62. if(m_lpTransport)
  63. m_lpTransport->Release();
  64. }
  65. HRESULT ECABLogon::Create(LPMAPISUP lpMAPISup, WSTransport* lpTransport, ULONG ulProfileFlags, GUID *lpGuid, ECABLogon **lppECABLogon)
  66. {
  67. HRESULT hr = hrSuccess;
  68. auto lpABLogon = new ECABLogon(lpMAPISup, lpTransport, ulProfileFlags, lpGuid);
  69. if (lpABLogon == nullptr)
  70. return MAPI_E_NOT_ENOUGH_MEMORY;
  71. hr = lpABLogon->QueryInterface(IID_ECABLogon, (void **)lppECABLogon);
  72. if(hr != hrSuccess)
  73. delete lpABLogon;
  74. return hr;
  75. }
  76. HRESULT ECABLogon::QueryInterface(REFIID refiid, void **lppInterface)
  77. {
  78. REGISTER_INTERFACE2(ECABLogon, this);
  79. REGISTER_INTERFACE2(ECUnknown, this);
  80. REGISTER_INTERFACE2(IABLogon, &this->m_xABLogon);
  81. REGISTER_INTERFACE2(IUnknown, &this->m_xABLogon);
  82. REGISTER_INTERFACE3(ISelectUnicode, IUnknown, &this->m_xUnknown);
  83. return MAPI_E_INTERFACE_NOT_SUPPORTED;
  84. }
  85. HRESULT ECABLogon::GetLastError(HRESULT hResult, ULONG ulFlags, LPMAPIERROR *lppMAPIError)
  86. {
  87. return MAPI_E_CALL_FAILED;
  88. }
  89. HRESULT ECABLogon::Logoff(ULONG ulFlags)
  90. {
  91. HRESULT hr = hrSuccess;
  92. //FIXME: Release all Other open objects ?
  93. //Releases all open objects, such as any subobjects or the status object.
  94. //Releases the provider's support object.
  95. if(m_lpMAPISup)
  96. {
  97. m_lpMAPISup->Release();
  98. m_lpMAPISup = NULL;
  99. }
  100. return hr;
  101. }
  102. HRESULT ECABLogon::OpenEntry(ULONG cbEntryID, LPENTRYID lpEntryID, LPCIID lpInterface, ULONG ulFlags, ULONG *lpulObjType, LPUNKNOWN *lppUnk)
  103. {
  104. HRESULT hr = hrSuccess;
  105. object_ptr<ECABContainer> lpABContainer;
  106. BOOL fModifyObject = FALSE;
  107. ABEID eidRoot = ABEID(MAPI_ABCONT, MUIDECSAB, 0);
  108. ABEID *lpABeid = NULL;
  109. object_ptr<IECPropStorage> lpPropStorage;
  110. object_ptr<ECMailUser> lpMailUser;
  111. object_ptr<ECDistList> lpDistList;
  112. memory_ptr<ENTRYID> lpEntryIDServer;
  113. // Check input/output variables
  114. if (lpulObjType == nullptr || lppUnk == nullptr)
  115. return MAPI_E_INVALID_PARAMETER;
  116. /*if(ulFlags & MAPI_MODIFY) {
  117. if (!fModify)
  118. return MAPI_E_NO_ACCESS;
  119. else
  120. fModifyObject = TRUE;
  121. }
  122. if(ulFlags & MAPI_BEST_ACCESS)
  123. fModifyObject = fModify;
  124. */
  125. if(cbEntryID == 0 && lpEntryID == NULL) {
  126. lpABeid = &eidRoot;
  127. cbEntryID = CbABEID(lpABeid);
  128. lpEntryID = (LPENTRYID)lpABeid;
  129. } else {
  130. if (cbEntryID == 0 || lpEntryID == nullptr)
  131. return MAPI_E_UNKNOWN_ENTRYID;
  132. hr = MAPIAllocateBuffer(cbEntryID, &~lpEntryIDServer);
  133. if(hr != hrSuccess)
  134. return hr;
  135. memcpy(lpEntryIDServer, lpEntryID, cbEntryID);
  136. lpEntryID = lpEntryIDServer;
  137. lpABeid = reinterpret_cast<ABEID *>(lpEntryID);
  138. // Check sane entryid
  139. if (lpABeid->ulType != MAPI_ABCONT && lpABeid->ulType != MAPI_MAILUSER && lpABeid->ulType != MAPI_DISTLIST)
  140. return MAPI_E_UNKNOWN_ENTRYID;
  141. // Check entryid GUID, must be either MUIDECSAB or m_ABPGuid
  142. if (memcmp(&lpABeid->guid, &MUIDECSAB, sizeof(MAPIUID)) != 0 &&
  143. memcmp(&lpABeid->guid, &m_ABPGuid, sizeof(MAPIUID)) != 0)
  144. return MAPI_E_UNKNOWN_ENTRYID;
  145. memcpy(&lpABeid->guid, &MUIDECSAB, sizeof(MAPIUID));
  146. }
  147. //TODO: check entryid serverside?
  148. switch(lpABeid->ulType) {
  149. case MAPI_ABCONT:
  150. hr = ECABContainer::Create(this, MAPI_ABCONT, fModifyObject, &~lpABContainer);
  151. if (hr != hrSuccess)
  152. return hr;
  153. hr = lpABContainer->SetEntryId(cbEntryID, lpEntryID);
  154. if (hr != hrSuccess)
  155. return hr;
  156. AddChild(lpABContainer);
  157. hr = m_lpTransport->HrOpenABPropStorage(cbEntryID, lpEntryID, &~lpPropStorage);
  158. if (hr != hrSuccess)
  159. return hr;
  160. hr = lpABContainer->HrSetPropStorage(lpPropStorage, TRUE);
  161. if (hr != hrSuccess)
  162. return hr;
  163. if (lpInterface)
  164. hr = lpABContainer->QueryInterface(*lpInterface,(void **)lppUnk);
  165. else
  166. hr = lpABContainer->QueryInterface(IID_IABContainer, (void **)lppUnk);
  167. if (hr != hrSuccess)
  168. return hr;
  169. break;
  170. case MAPI_MAILUSER:
  171. hr = ECMailUser::Create(this, fModifyObject, &~lpMailUser);
  172. if (hr != hrSuccess)
  173. return hr;
  174. hr = lpMailUser->SetEntryId(cbEntryID, lpEntryID);
  175. if (hr != hrSuccess)
  176. return hr;
  177. AddChild(lpMailUser);
  178. hr = m_lpTransport->HrOpenABPropStorage(cbEntryID, lpEntryID, &~lpPropStorage);
  179. if (hr != hrSuccess)
  180. return hr;
  181. hr = lpMailUser->HrSetPropStorage(lpPropStorage, TRUE);
  182. if (hr != hrSuccess)
  183. return hr;
  184. if (lpInterface)
  185. hr = lpMailUser->QueryInterface(*lpInterface,(void **)lppUnk);
  186. else
  187. hr = lpMailUser->QueryInterface(IID_IMailUser, (void **)lppUnk);
  188. if (hr != hrSuccess)
  189. return hr;
  190. break;
  191. case MAPI_DISTLIST:
  192. hr = ECDistList::Create(this, fModifyObject, &~lpDistList);
  193. if (hr != hrSuccess)
  194. return hr;
  195. hr = lpDistList->SetEntryId(cbEntryID, lpEntryID);
  196. if (hr != hrSuccess)
  197. return hr;
  198. AddChild(lpDistList);
  199. hr = m_lpTransport->HrOpenABPropStorage(cbEntryID, lpEntryID, &~lpPropStorage);
  200. if (hr != hrSuccess)
  201. return hr;
  202. hr = lpDistList->HrSetPropStorage(lpPropStorage, TRUE);
  203. if (hr != hrSuccess)
  204. return hr;
  205. if (lpInterface)
  206. hr = lpDistList->QueryInterface(*lpInterface, (void **)lppUnk);
  207. else
  208. hr = lpDistList->QueryInterface(IID_IDistList, (void **)lppUnk);
  209. if (hr != hrSuccess)
  210. return hr;
  211. break;
  212. default:
  213. return MAPI_E_NOT_FOUND;
  214. }
  215. if(lpulObjType)
  216. *lpulObjType = lpABeid->ulType;
  217. return hrSuccess;
  218. }
  219. HRESULT ECABLogon::CompareEntryIDs(ULONG cbEntryID1, LPENTRYID lpEntryID1, ULONG cbEntryID2, LPENTRYID lpEntryID2, ULONG ulFlags, ULONG *lpulResult)
  220. {
  221. if(lpulResult)
  222. *lpulResult = (CompareABEID(cbEntryID1, lpEntryID1, cbEntryID2, lpEntryID2) ? TRUE : FALSE);
  223. return hrSuccess;
  224. }
  225. HRESULT ECABLogon::Advise(ULONG cbEntryID, LPENTRYID lpEntryID, ULONG ulEventMask, LPMAPIADVISESINK lpAdviseSink, ULONG *lpulConnection)
  226. {
  227. HRESULT hr = hrSuccess;
  228. if (lpAdviseSink == NULL || lpulConnection == NULL)
  229. return MAPI_E_INVALID_PARAMETER;
  230. if (lpEntryID == NULL)
  231. //NOTE: Normal you must give the entryid of the addressbook toplevel
  232. return MAPI_E_INVALID_PARAMETER;
  233. assert(m_lpNotifyClient != NULL && (lpEntryID != NULL || true));
  234. if(m_lpNotifyClient->Advise(cbEntryID, (LPBYTE)lpEntryID, ulEventMask, lpAdviseSink, lpulConnection) != S_OK)
  235. hr = MAPI_E_NO_SUPPORT;
  236. return hr;
  237. }
  238. HRESULT ECABLogon::Unadvise(ULONG ulConnection)
  239. {
  240. assert(m_lpNotifyClient != NULL);
  241. m_lpNotifyClient->Unadvise(ulConnection);
  242. return hrSuccess;
  243. }
  244. HRESULT ECABLogon::OpenStatusEntry(LPCIID lpInterface, ULONG ulFlags, ULONG *lpulObjType, LPMAPISTATUS * lppMAPIStatus)
  245. {
  246. return MAPI_E_NO_SUPPORT;
  247. }
  248. HRESULT ECABLogon::OpenTemplateID(ULONG cbTemplateID, LPENTRYID lpTemplateID, ULONG ulTemplateFlags, LPMAPIPROP lpMAPIPropData, LPCIID lpInterface, LPMAPIPROP * lppMAPIPropNew, LPMAPIPROP lpMAPIPropSibling)
  249. {
  250. return MAPI_E_NO_SUPPORT;
  251. }
  252. HRESULT ECABLogon::GetOneOffTable(ULONG ulFlags, LPMAPITABLE * lppTable)
  253. {
  254. //return m_lpMAPISup->GetOneOffTable(ulFlags, lppTable);
  255. return MAPI_E_NO_SUPPORT;
  256. }
  257. HRESULT ECABLogon::PrepareRecips(ULONG ulFlags,
  258. const SPropTagArray *lpPropTagArray, LPADRLIST lpRecipList)
  259. {
  260. HRESULT hr = hrSuccess;
  261. ULONG cPropsRecip;
  262. LPSPropValue rgpropvalsRecip;
  263. ABEID *lpABeid = NULL;
  264. ULONG cbABeid;
  265. ULONG cValues;
  266. ecmem_ptr<SPropValue> lpPropArray, lpNewPropArray;
  267. unsigned int j;
  268. ULONG ulObjType;
  269. if(lpPropTagArray == NULL || lpPropTagArray->cValues == 0) // There is no work to do.
  270. return hrSuccess;
  271. for (unsigned int i = 0; i < lpRecipList->cEntries; ++i) {
  272. rgpropvalsRecip = lpRecipList->aEntries[i].rgPropVals;
  273. cPropsRecip = lpRecipList->aEntries[i].cValues;
  274. // For each recipient, find its entryid
  275. auto lpPropVal = PCpropFindProp(rgpropvalsRecip, cPropsRecip, PR_ENTRYID);
  276. if(!lpPropVal)
  277. continue; // no
  278. lpABeid = reinterpret_cast<ABEID *>(lpPropVal->Value.bin.lpb);
  279. cbABeid = lpPropVal->Value.bin.cb;
  280. /* Is it one of ours? */
  281. if ( cbABeid < CbNewABEID("") || lpABeid == NULL)
  282. continue; // no
  283. if ( memcmp( &(lpABeid->guid), &this->m_guid, sizeof(MAPIUID) ) != 0)
  284. continue; // no
  285. object_ptr<IMailUser> lpIMailUser;
  286. hr = OpenEntry(cbABeid, reinterpret_cast<ENTRYID *>(lpABeid), nullptr, 0, &ulObjType, &~lpIMailUser);
  287. if(hr != hrSuccess)
  288. continue; // no
  289. hr = lpIMailUser->GetProps(lpPropTagArray, 0, &cValues, &~lpPropArray);
  290. if(FAILED(hr) != hrSuccess)
  291. continue; // no
  292. // merge the properties
  293. hr = ECAllocateBuffer((cValues + cPropsRecip) * sizeof(SPropValue), &~lpNewPropArray);
  294. if (hr != hrSuccess)
  295. return hr;
  296. for (j = 0; j < cValues; ++j) {
  297. lpPropVal = NULL;
  298. if(PROP_TYPE(lpPropArray[j].ulPropTag) == PT_ERROR)
  299. lpPropVal = PCpropFindProp(rgpropvalsRecip, cPropsRecip, lpPropTagArray->aulPropTag[j]);
  300. if(lpPropVal == NULL)
  301. lpPropVal = &lpPropArray[j];
  302. hr = Util::HrCopyProperty(lpNewPropArray + j, lpPropVal, lpNewPropArray);
  303. if(hr != hrSuccess)
  304. return hr;
  305. }
  306. for (j = 0; j < cPropsRecip; ++j) {
  307. if (PCpropFindProp(lpNewPropArray, cValues, rgpropvalsRecip[j].ulPropTag) ||
  308. PROP_TYPE( rgpropvalsRecip[j].ulPropTag ) == PT_ERROR )
  309. continue;
  310. hr = Util::HrCopyProperty(lpNewPropArray + cValues, &rgpropvalsRecip[j], lpNewPropArray);
  311. if(hr != hrSuccess)
  312. return hr;
  313. ++cValues;
  314. }
  315. lpRecipList->aEntries[i].rgPropVals = lpNewPropArray.release();
  316. lpRecipList->aEntries[i].cValues = cValues;
  317. if(rgpropvalsRecip) {
  318. ECFreeBuffer(rgpropvalsRecip);
  319. rgpropvalsRecip = NULL;
  320. }
  321. }
  322. // Always succeeded on this point
  323. return hrSuccess;
  324. }
  325. DEF_HRMETHOD1(TRACE_MAPI, ECABLogon, ABLogon, QueryInterface, (REFIID, refiid), (void **, lppInterface))
  326. DEF_ULONGMETHOD1(TRACE_MAPI, ECABLogon, ABLogon, AddRef, (void))
  327. DEF_ULONGMETHOD1(TRACE_MAPI, ECABLogon, ABLogon, Release, (void))
  328. DEF_HRMETHOD1(TRACE_MAPI, ECABLogon, ABLogon, GetLastError, (HRESULT, hResult), (ULONG, ulFlags), (LPMAPIERROR *, lppMAPIError))
  329. DEF_HRMETHOD1(TRACE_MAPI, ECABLogon, ABLogon, Logoff, (ULONG, ulFlags))
  330. DEF_HRMETHOD1(TRACE_MAPI, ECABLogon, ABLogon, OpenEntry, (ULONG, cbEntryID), (LPENTRYID, lpEntryID), (LPCIID, lpInterface), (ULONG, ulFlags), (ULONG *, lpulObjType), (LPUNKNOWN *, lppUnk))
  331. DEF_HRMETHOD1(TRACE_MAPI, ECABLogon, ABLogon, CompareEntryIDs, (ULONG, cbEntryID1), (LPENTRYID, lpEntryID1), (ULONG, cbEntryID2), (LPENTRYID, lpEntryID2), (ULONG, ulFlags), (ULONG *, lpulResult))
  332. DEF_HRMETHOD1(TRACE_MAPI, ECABLogon, ABLogon, Advise, (ULONG, cbEntryID), (LPENTRYID, lpEntryID), (ULONG, ulEventMask), (LPMAPIADVISESINK, lpAdviseSink), (ULONG *, lpulConnection))
  333. DEF_HRMETHOD1(TRACE_MAPI, ECABLogon, ABLogon, Unadvise, (ULONG, ulConnection))
  334. DEF_HRMETHOD1(TRACE_MAPI, ECABLogon, ABLogon, OpenStatusEntry, (LPCIID, lpInterface), (ULONG, ulFlags), (ULONG *, lpulObjType), (LPMAPISTATUS *, lppMAPIStatus))
  335. DEF_HRMETHOD1(TRACE_MAPI, ECABLogon, ABLogon, OpenTemplateID, (ULONG, cbTemplateID), (LPENTRYID, lpTemplateID), (ULONG, ulTemplateFlags), (LPMAPIPROP, lpMAPIPropData), (LPCIID, lpInterface), (LPMAPIPROP *, lppMAPIPropNew), (LPMAPIPROP, lpMAPIPropSibling))
  336. DEF_HRMETHOD1(TRACE_MAPI, ECABLogon, ABLogon, GetOneOffTable, (ULONG, ulFlags), (LPMAPITABLE *, lppTable))
  337. DEF_HRMETHOD1(TRACE_MAPI, ECABLogon, ABLogon, PrepareRecips, (ULONG, ulFlags), (const SPropTagArray *, lpPropTagArray), (LPADRLIST, lpRecipList))