ECMSProvider.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 <new>
  18. #include <kopano/platform.h>
  19. #include <mapi.h>
  20. #include <mapiutil.h>
  21. #include <mapispi.h>
  22. #include <kopano/ECGetText.h>
  23. #include <kopano/memory.hpp>
  24. #include "Mem.h"
  25. #include <kopano/ECGuid.h>
  26. #include <kopano/ECInterfaceDefs.h>
  27. #include "ECMSProvider.h"
  28. #include "ECMsgStore.h"
  29. #include "ECABProvider.h"
  30. #include <kopano/ECDebug.h>
  31. #include "ClientUtil.h"
  32. #include "EntryPoint.h"
  33. #include "WSUtil.h"
  34. #include "pcutil.hpp"
  35. #include "ProviderUtil.h"
  36. #include <kopano/stringutil.h>
  37. #include <edkguid.h>
  38. #include <cwchar>
  39. #include <kopano/charset/convert.h>
  40. #include <kopano/charset/utf8string.h>
  41. using namespace std;
  42. using namespace KCHL;
  43. typedef KCHL::memory_ptr<ECUSER> ECUserPtr;
  44. ECMSProvider::ECMSProvider(ULONG ulFlags, const char *szClassName) :
  45. ECUnknown(szClassName), m_ulFlags(ulFlags)
  46. {
  47. TRACE_MAPI(TRACE_ENTRY, "ECMSProvider::ECMSProvider","");
  48. }
  49. ECMSProvider::~ECMSProvider()
  50. {
  51. TRACE_MAPI(TRACE_ENTRY, "ECMSProvider::~ECMSProvider","");
  52. }
  53. HRESULT ECMSProvider::Create(ULONG ulFlags, ECMSProvider **lppECMSProvider) {
  54. auto lpECMSProvider = new(std::nothrow) ECMSProvider(ulFlags, "IMSProvider");
  55. if (lpECMSProvider == nullptr)
  56. return MAPI_E_NOT_ENOUGH_MEMORY;
  57. auto ret = lpECMSProvider->QueryInterface(IID_ECMSProvider,
  58. reinterpret_cast<void **>(lppECMSProvider));
  59. if (ret != hrSuccess)
  60. delete lpECMSProvider;
  61. return ret;
  62. }
  63. HRESULT ECMSProvider::QueryInterface(REFIID refiid, void **lppInterface)
  64. {
  65. REGISTER_INTERFACE2(ECMSProvider, this);
  66. REGISTER_INTERFACE2(IMSProvider, &this->m_xMSProvider);
  67. REGISTER_INTERFACE3(ISelectUnicode, IUnknown, &this->m_xUnknown);
  68. return MAPI_E_INTERFACE_NOT_SUPPORTED;
  69. }
  70. HRESULT ECMSProvider::Shutdown(ULONG * lpulFlags)
  71. {
  72. return hrSuccess;
  73. }
  74. HRESULT ECMSProvider::Logon(LPMAPISUP lpMAPISup, ULONG ulUIParam, LPTSTR lpszProfileName, ULONG cbEntryID, LPENTRYID lpEntryID, ULONG ulFlags, LPCIID lpInterface, ULONG *lpcbSpoolSecurity, LPBYTE *lppbSpoolSecurity, LPMAPIERROR *lppMAPIError, LPMSLOGON *lppMSLogon, LPMDB *lppMDB)
  75. {
  76. HRESULT hr = hrSuccess;
  77. object_ptr<WSTransport> lpTransport;
  78. object_ptr<ECMsgStore> lpECMsgStore;
  79. object_ptr<ECMSLogon> lpECMSLogon;
  80. object_ptr<IProfSect> lpProfSect;
  81. ULONG cValues = 0;
  82. memory_ptr<SPropValue> lpsPropArray;
  83. BOOL fIsDefaultStore = FALSE;
  84. ULONG ulStoreType = 0;
  85. MAPIUID guidMDBProvider;
  86. BOOL bOfflineStore = FALSE;
  87. sGlobalProfileProps sProfileProps;
  88. // Always suppress UI when running in a service
  89. if(m_ulFlags & MAPI_NT_SERVICE)
  90. ulFlags |= MDB_NO_DIALOG;
  91. // If the EntryID is not configured, return MAPI_E_UNCONFIGURED, this will
  92. // cause MAPI to call our configuration entry point (MSGServiceEntry)
  93. if (lpEntryID == nullptr)
  94. return MAPI_E_UNCONFIGURED;
  95. if(lpcbSpoolSecurity)
  96. *lpcbSpoolSecurity = 0;
  97. if(lppbSpoolSecurity)
  98. *lppbSpoolSecurity = NULL;
  99. // Get the username and password from the profile settings
  100. hr = ClientUtil::GetGlobalProfileProperties(lpMAPISup, &sProfileProps);
  101. if(hr != hrSuccess)
  102. return hr;
  103. // Open profile settings
  104. hr = lpMAPISup->OpenProfileSection(nullptr, MAPI_MODIFY, &~lpProfSect);
  105. if(hr != hrSuccess)
  106. return hr;
  107. static constexpr const SizedSPropTagArray(2, proptags) =
  108. {2, {PR_MDB_PROVIDER, PR_RESOURCE_FLAGS}};
  109. hr = lpProfSect->GetProps(proptags, 0, &cValues, &~lpsPropArray);
  110. if (FAILED(hr))
  111. return hr;
  112. TRACE_MAPI(TRACE_ENTRY, "ECMSProvider::Logon::MDB", "PR_MDB_PROVIDER = %s", lpsPropArray[0].ulPropTag == PR_MDB_PROVIDER ? DBGGUIDToString(*(IID*)lpsPropArray[0].Value.bin.lpb).c_str() : "<Unknown>");
  113. if (lpsPropArray[1].ulPropTag == PR_RESOURCE_FLAGS &&
  114. lpsPropArray[1].Value.ul & STATUS_DEFAULT_STORE)
  115. fIsDefaultStore = TRUE;
  116. // Create a transport for this message store
  117. hr = WSTransport::Create(ulFlags, &~lpTransport);
  118. if(hr != hrSuccess)
  119. return hr;
  120. hr = LogonByEntryID(&+lpTransport, &sProfileProps, cbEntryID, lpEntryID);
  121. if (lpsPropArray[0].ulPropTag == PR_MDB_PROVIDER) {
  122. memcpy(&guidMDBProvider, lpsPropArray[0].Value.bin.lpb, sizeof(MAPIUID));
  123. } else if (fIsDefaultStore == FALSE){
  124. // also fallback to private store when logon failed (hr, do not change)
  125. if (hr != hrSuccess || lpTransport->HrGetStoreType(cbEntryID, lpEntryID, &ulStoreType) != hrSuccess)
  126. // Maintain backward-compat: if connecting to a server that does not support the storetype
  127. // call, assume private store, which is what happened before this call was introduced
  128. ulStoreType = ECSTORE_TYPE_PRIVATE;
  129. if (ulStoreType == ECSTORE_TYPE_PRIVATE)
  130. memcpy(&guidMDBProvider, &KOPANO_STORE_DELEGATE_GUID, sizeof(MAPIUID));
  131. else if (ulStoreType == ECSTORE_TYPE_PUBLIC)
  132. memcpy(&guidMDBProvider, &KOPANO_STORE_PUBLIC_GUID, sizeof(MAPIUID));
  133. else if (ulStoreType == ECSTORE_TYPE_ARCHIVE)
  134. memcpy(&guidMDBProvider, &KOPANO_STORE_ARCHIVE_GUID, sizeof(MAPIUID));
  135. else {
  136. assert(false);
  137. return MAPI_E_NO_SUPPORT;
  138. }
  139. } else {
  140. memcpy(&guidMDBProvider, &KOPANO_SERVICE_GUID, sizeof(MAPIUID));
  141. }
  142. TRACE_MAPI(TRACE_ENTRY, "ECMSProvider::Logon::MDB", "PR_MDB_PROVIDER = %s", DBGGUIDToString(*(IID*)&guidMDBProvider).c_str());
  143. if(hr != hrSuccess)
  144. return hr;
  145. // Get a message store object
  146. hr = CreateMsgStoreObject((LPSTR)sProfileProps.strProfileName.c_str(), lpMAPISup, cbEntryID, lpEntryID, ulFlags, sProfileProps.ulProfileFlags, lpTransport,
  147. &guidMDBProvider, false, fIsDefaultStore, bOfflineStore, &~lpECMsgStore);
  148. if(hr != hrSuccess)
  149. return hr;
  150. // Register ourselves with mapisupport
  151. //hr = lpMAPISup->SetProviderUID((MAPIUID *)&lpMsgStore->GetStoreGuid(), 0);
  152. //if(hr != hrSuccess)
  153. // return hr;
  154. // Return the variables
  155. if(lppMDB) {
  156. hr = lpECMsgStore->QueryInterface(IID_IMsgStore, (void **)lppMDB);
  157. if(hr != hrSuccess)
  158. return hr;
  159. }
  160. // We don't count lpMSLogon as a child, because its lifetime is coupled to lpMsgStore
  161. if(lppMSLogon) {
  162. hr = ECMSLogon::Create(lpECMsgStore, &~lpECMSLogon);
  163. if(hr != hrSuccess)
  164. return hr;
  165. hr = lpECMSLogon->QueryInterface(IID_IMSLogon, (void **)lppMSLogon);
  166. if(hr != hrSuccess)
  167. return hr;
  168. }
  169. return hrSuccess;
  170. }
  171. //FIXME: What todo with offline??
  172. //TODO: online/offline state???
  173. HRESULT ECMSProvider::SpoolerLogon(LPMAPISUP lpMAPISup, ULONG ulUIParam, LPTSTR lpszProfileName, ULONG cbEntryID, LPENTRYID lpEntryID, ULONG ulFlags, LPCIID lpInterface, ULONG cbSpoolSecurity, LPBYTE lpbSpoolSecurity, LPMAPIERROR *lppMAPIError, LPMSLOGON *lppMSLogon, LPMDB *lppMDB)
  174. {
  175. HRESULT hr = hrSuccess;
  176. object_ptr<WSTransport> lpTransport;
  177. object_ptr<ECMsgStore> lpMsgStore;
  178. object_ptr<ECMSLogon> lpLogon;
  179. MAPIUID guidMDBProvider;
  180. object_ptr<IProfSect> lpProfSect;
  181. ULONG cValues = 0;
  182. LPSPropValue lpsPropArray = NULL;
  183. bool bOfflineStore = false;
  184. sGlobalProfileProps sProfileProps;
  185. wchar_t *strSep = NULL;
  186. if (lpEntryID == nullptr)
  187. return MAPI_E_UNCONFIGURED;
  188. if (cbSpoolSecurity == 0 || lpbSpoolSecurity == nullptr)
  189. return MAPI_E_NO_ACCESS;
  190. // Get Global profile settings
  191. hr = ClientUtil::GetGlobalProfileProperties(lpMAPISup, &sProfileProps);
  192. if(hr != hrSuccess)
  193. return hr;
  194. // Open profile settings
  195. hr = lpMAPISup->OpenProfileSection(nullptr, MAPI_MODIFY, &~lpProfSect);
  196. if(hr != hrSuccess)
  197. return hr;
  198. static constexpr const SizedSPropTagArray(2, proptags) =
  199. {2, {PR_MDB_PROVIDER, PR_RESOURCE_FLAGS}};
  200. // Get the MDBProvider from the profile settings
  201. hr = lpProfSect->GetProps(proptags, 0, &cValues, &lpsPropArray);
  202. if(hr == hrSuccess || hr == MAPI_W_ERRORS_RETURNED)
  203. {
  204. if (lpsPropArray[0].ulPropTag == PR_MDB_PROVIDER)
  205. memcpy(&guidMDBProvider, lpsPropArray[0].Value.bin.lpb, sizeof(MAPIUID));
  206. if (lpsPropArray[1].ulPropTag == PR_RESOURCE_FLAGS &&
  207. !(lpsPropArray[1].Value.ul & STATUS_DEFAULT_STORE))
  208. /* Deny spooler logon to any store that is not the default store */
  209. return MAPI_E_NOT_FOUND;
  210. }
  211. if (cbSpoolSecurity % sizeof(wchar_t) != 0)
  212. return MAPI_E_INVALID_PARAMETER;
  213. strSep = (wchar_t*)wmemchr((wchar_t*)lpbSpoolSecurity, 0, cbSpoolSecurity / sizeof(wchar_t));
  214. if (strSep == NULL)
  215. return MAPI_E_NO_ACCESS;
  216. ++strSep;
  217. sProfileProps.strUserName = (wchar_t*)lpbSpoolSecurity;
  218. sProfileProps.strPassword = strSep;
  219. // Create a transport for this message store
  220. hr = WSTransport::Create(ulFlags, &~lpTransport);
  221. if(hr != hrSuccess)
  222. return hr;
  223. hr = LogonByEntryID(&+lpTransport, &sProfileProps, cbEntryID, lpEntryID);
  224. if(hr != hrSuccess) {
  225. if (ulFlags & MDB_NO_DIALOG)
  226. return MAPI_E_FAILONEPROVIDER;
  227. return MAPI_E_UNCONFIGURED;
  228. }
  229. // Get a message store object
  230. hr = CreateMsgStoreObject((LPSTR)sProfileProps.strProfileName.c_str(), lpMAPISup, cbEntryID, lpEntryID, ulFlags, sProfileProps.ulProfileFlags, lpTransport,
  231. &guidMDBProvider, true, true, bOfflineStore, &~lpMsgStore);
  232. if(hr != hrSuccess)
  233. return hr;
  234. // Register ourselves with mapisupport
  235. //guidStore = lpMsgStore->GetStoreGuid();
  236. //hr = lpMAPISup->SetProviderUID((MAPIUID *)&guidStore, 0);
  237. //if(hr != hrSuccess)
  238. // goto exit;
  239. // Return the variables
  240. if(lppMDB) {
  241. hr = lpMsgStore->QueryInterface(IID_IMsgStore, (void **)lppMDB);
  242. if(hr != hrSuccess)
  243. return hr;
  244. }
  245. if(lppMSLogon) {
  246. hr = ECMSLogon::Create(lpMsgStore, &~lpLogon);
  247. if(hr != hrSuccess)
  248. return hr;
  249. hr = lpLogon->QueryInterface(IID_IMSLogon, (void **)lppMSLogon);
  250. if(hr != hrSuccess)
  251. return hr;
  252. }
  253. return hrSuccess;
  254. }
  255. HRESULT ECMSProvider::CompareStoreIDs(ULONG cbEntryID1, LPENTRYID lpEntryID1, ULONG cbEntryID2, LPENTRYID lpEntryID2, ULONG ulFlags, ULONG *lpulResult)
  256. {
  257. return ::CompareStoreIDs(cbEntryID1, lpEntryID1, cbEntryID2, lpEntryID2, ulFlags, lpulResult);
  258. }
  259. /**
  260. * Log a WSTransport object on to a server based on the URL found in the provided store entryid.
  261. *
  262. * @param[in,out] lppTransport Double pointer to the WSTransport object that is to be logged on. This
  263. * is a double pointer because a new WSTransport object will be returned if
  264. * the entryid contains a pseudo URL that is resolved to another server than
  265. * the server specified in the profile.
  266. * @param[in,out] lpsProfileProps The profile properties used to connect to logon to the server. If the
  267. * entryid doesn't contain a pseudo URL, the serverpath in the profile properties
  268. * will be updated to contain the path extracted from the entryid.
  269. * @param[in] cbEntryID The length of the passed entryid in bytes.
  270. * @param[in] lpEntryID Pointer to the store entryid from which the server path will be extracted.
  271. *
  272. * @retval MAPI_E_FAILONEPROVIDER Returned when the extraction of the URL failed.
  273. */
  274. HRESULT ECMSProvider::LogonByEntryID(WSTransport **lppTransport, sGlobalProfileProps *lpsProfileProps, ULONG cbEntryID, LPENTRYID lpEntryID)
  275. {
  276. HRESULT hr;
  277. string extractedServerPath; // The extracted server path
  278. bool bIsPseudoUrl = false;
  279. WSTransport *lpTransport = NULL;
  280. assert(lppTransport != NULL && *lppTransport != NULL);
  281. lpTransport = *lppTransport;
  282. hr = HrGetServerURLFromStoreEntryId(cbEntryID, lpEntryID, extractedServerPath, &bIsPseudoUrl);
  283. if (hr != hrSuccess)
  284. return MAPI_E_FAILONEPROVIDER;
  285. // Log on the transport to the server
  286. if (!bIsPseudoUrl) {
  287. sGlobalProfileProps sOtherProps = *lpsProfileProps;
  288. sOtherProps.strServerPath = extractedServerPath;
  289. hr = lpTransport->HrLogon(sOtherProps);
  290. if (hr != hrSuccess)
  291. // If we failed to open a non-pseudo-URL, fallback to using the server from the global
  292. // profile section. We need this because some older versions wrote a non-pseudo URL, which
  293. // we should still support - even when the hostname of the server changes for example.
  294. hr = lpTransport->HrLogon(*lpsProfileProps);
  295. } else {
  296. string strServerPath; // The resolved server path
  297. bool bIsPeer;
  298. WSTransport *lpAltTransport = NULL;
  299. hr = lpTransport->HrLogon(*lpsProfileProps);
  300. if (hr != hrSuccess)
  301. return hr;
  302. hr = HrResolvePseudoUrl(lpTransport, extractedServerPath.c_str(), strServerPath, &bIsPeer);
  303. if (hr != hrSuccess)
  304. return hr;
  305. if (!bIsPeer) {
  306. hr = lpTransport->CreateAndLogonAlternate(strServerPath.c_str(), &lpAltTransport);
  307. if (hr != hrSuccess)
  308. return hr;
  309. lpTransport->HrLogOff();
  310. lpTransport->Release();
  311. *lppTransport = lpAltTransport;
  312. }
  313. }
  314. return hrSuccess;
  315. }
  316. DEF_ULONGMETHOD1(TRACE_MAPI, ECMSProvider, MSProvider, AddRef, (void))
  317. DEF_ULONGMETHOD1(TRACE_MAPI, ECMSProvider, MSProvider, Release, (void))
  318. DEF_HRMETHOD1(TRACE_MAPI, ECMSProvider, MSProvider, QueryInterface, (REFIID, refiid), (void **, lppInterface))
  319. DEF_HRMETHOD1(TRACE_MAPI, ECMSProvider, MSProvider, Shutdown, (ULONG *, lpulFlags))
  320. /* has 12 args, no macro deals with it atm */
  321. HRESULT ECMSProvider::xMSProvider::Logon(LPMAPISUP lpMAPISup, ULONG ulUIParam, LPTSTR lpszProfileName, ULONG cbEntryID, LPENTRYID lpEntryID, ULONG ulFlags, LPCIID lpInterface, ULONG *lpcbSpoolSecurity, LPBYTE *lppbSpoolSecurity, LPMAPIERROR *lppMAPIError, LPMSLOGON *lppMSLogon, LPMDB *lppMDB)
  322. {
  323. TRACE_MAPI(TRACE_ENTRY, "IMSProvider::Logon", "flags=%x, cbEntryID=%d, lpEntryid=%s", ulFlags, cbEntryID, lpEntryID ? bin2hex(cbEntryID, (LPBYTE)lpEntryID).c_str() : "NULL");
  324. METHOD_PROLOGUE_(ECMSProvider, MSProvider);
  325. HRESULT hr = pThis->Logon(lpMAPISup, ulUIParam, lpszProfileName, cbEntryID, lpEntryID,ulFlags, lpInterface, lpcbSpoolSecurity, lppbSpoolSecurity, lppMAPIError, lppMSLogon, lppMDB);
  326. TRACE_MAPI(TRACE_RETURN, "IMSProvider::Logon", "%s", GetMAPIErrorDescription(hr).c_str());
  327. return hr;
  328. }
  329. HRESULT ECMSProvider::xMSProvider::SpoolerLogon(LPMAPISUP lpMAPISup, ULONG ulUIParam, LPTSTR lpszProfileName, ULONG cbEntryID, LPENTRYID lpEntryID, ULONG ulFlags, LPCIID lpInterface, ULONG lpcbSpoolSecurity, LPBYTE lppbSpoolSecurity, LPMAPIERROR *lppMAPIError, LPMSLOGON *lppMSLogon, LPMDB *lppMDB)
  330. {
  331. TRACE_MAPI(TRACE_ENTRY, "IMSProvider::SpoolerLogon", "flags=%x", ulFlags);
  332. METHOD_PROLOGUE_(ECMSProvider, MSProvider);
  333. HRESULT hr = pThis->SpoolerLogon(lpMAPISup, ulUIParam, lpszProfileName, cbEntryID, lpEntryID,ulFlags, lpInterface, lpcbSpoolSecurity, lppbSpoolSecurity, lppMAPIError, lppMSLogon, lppMDB);
  334. TRACE_MAPI(TRACE_RETURN, "IMSProvider::SpoolerLogon", "%s", GetMAPIErrorDescription(hr).c_str());
  335. return hr;
  336. }
  337. DEF_HRMETHOD1(TRACE_MAPI, ECMSProvider, MSProvider, CompareStoreIDs, (ULONG, cbEntryID1), (LPENTRYID, lpEntryID1), (ULONG, cbEntryID2), (LPENTRYID, lpEntryID2), (ULONG, ulFlags), (ULONG *, lpulResult))