epclient.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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 <cstdint>
  19. #include <mapi.h>
  20. #include <mapispi.h>
  21. #include <mapiutil.h>
  22. #include <kopano/ECGetText.h>
  23. #include <kopano/memory.hpp>
  24. #include <memory>
  25. #include <string>
  26. #include <cassert>
  27. #include "kcore.hpp"
  28. #include "Mem.h"
  29. #include "DLLGlobal.h"
  30. #include "ECMSProviderSwitch.h"
  31. #include "ECXPProvider.h"
  32. #include "ECABProviderSwitch.h"
  33. #include <iostream>
  34. #include <kopano/ecversion.h>
  35. #include <kopano/ECDebug.h>
  36. #include <kopano/stringutil.h>
  37. #include <kopano/ECLogger.h>
  38. #include <kopano/ECGuid.h>
  39. #include <edkmdb.h>
  40. #include <edkguid.h>
  41. #include <kopano/mapi_ptr.h>
  42. #include "SSLUtil.h"
  43. #include "ClientUtil.h"
  44. #include "SymmetricCrypt.h"
  45. #include "EntryPoint.h"
  46. #include <kopano/charset/convstring.h>
  47. using namespace std;
  48. using namespace KCHL;
  49. struct initprov {
  50. IProviderAdmin *provadm;
  51. MAPIUID *provuid;
  52. IProfSect *profsect;
  53. WSTransport *transport;
  54. unsigned int count, eid_size, wrap_eid_size;
  55. SPropValue prop[6];
  56. EntryIdPtr eid;
  57. /* referenced from prop[n] */
  58. WStringPtr store_name;
  59. EntryIdPtr wrap_eid;
  60. memory_ptr<ABEID> abe_id;
  61. };
  62. static const uint32_t MAPI_S_SPECIAL_OK = MAKE_MAPI_S(0x900);
  63. // Client wide variable
  64. tstring g_strCommonFilesKopano;
  65. tstring g_strUserLocalAppDataKopano;
  66. tstring g_strKopanoDirectory;
  67. tstring g_strManufacturer;
  68. tstring g_strProductName;
  69. tstring g_strProductNameShort;
  70. bool g_isOEM;
  71. ULONG g_ulLoadsim;
  72. // Map of msprovider with Profilename as key
  73. ECMapProvider g_mapProviders;
  74. static HRESULT RemoveAllProviders(ECMapProvider *);
  75. class CKopanoApp {
  76. public:
  77. CKopanoApp() {
  78. ssl_threading_setup();
  79. g_strManufacturer = _T("Kopano");
  80. g_strProductName = _T("Kopano Core");
  81. g_isOEM = false;
  82. g_ulLoadsim = FALSE;
  83. // FIXME for offline
  84. // - g_strUserLocalAppDataKopano = ~/kopano ?
  85. // - g_strKopanoDirectory = /usr/bin/ ?
  86. }
  87. ~CKopanoApp() {
  88. ssl_threading_cleanup();
  89. RemoveAllProviders(&g_mapProviders);
  90. }
  91. };
  92. CKopanoApp theApp;
  93. static HRESULT RemoveAllProviders(ECMapProvider *mp)
  94. {
  95. if (mp == nullptr)
  96. return MAPI_E_INVALID_PARAMETER;
  97. for (const auto &p : *mp) {
  98. if (p.second.lpMSProviderOnline)
  99. p.second.lpMSProviderOnline->Release();
  100. if (p.second.lpABProviderOnline)
  101. p.second.lpABProviderOnline->Release();
  102. }
  103. return hrSuccess;
  104. }
  105. // entrypoints
  106. // Called by MAPI to return a MSProvider object when a user opens a store based on our service
  107. HRESULT __cdecl MSProviderInit(HINSTANCE hInstance, LPMALLOC pmalloc,
  108. LPALLOCATEBUFFER pfnAllocBuf, LPALLOCATEMORE pfnAllocMore,
  109. LPFREEBUFFER pfnFreeBuf, ULONG ulFlags, ULONG ulMAPIver,
  110. ULONG *lpulProviderVer, LPMSPROVIDER *ppmsp)
  111. {
  112. TRACE_MAPI(TRACE_ENTRY, "MSProviderInit", "flags=%08X", ulFlags);
  113. HRESULT hr = hrSuccess;
  114. object_ptr<ECMSProviderSwitch> lpMSProvider;
  115. // Check the interface version is ok
  116. if(ulMAPIver != CURRENT_SPI_VERSION) {
  117. hr = MAPI_E_VERSION;
  118. goto exit;
  119. }
  120. *lpulProviderVer = CURRENT_SPI_VERSION;
  121. // Save the pointers for later use
  122. _pmalloc = pmalloc;
  123. _pfnAllocBuf = pfnAllocBuf;
  124. _pfnAllocMore = pfnAllocMore;
  125. _pfnFreeBuf = pfnFreeBuf;
  126. _hInstance = hInstance;
  127. // This object is created for the lifetime of the DLL and destroyed when the
  128. // DLL is closed (same on linux, but then for the shared library);
  129. hr = ECMSProviderSwitch::Create(ulFlags, &~lpMSProvider);
  130. if(hr != hrSuccess)
  131. goto exit;
  132. hr = lpMSProvider->QueryInterface(IID_IMSProvider, (void **)ppmsp);
  133. exit:
  134. TRACE_MAPI(TRACE_RETURN, "MSProviderInit", "%s", GetMAPIErrorDescription(hr).c_str());
  135. return hr;
  136. }
  137. /**
  138. * Get the service name from the provider admin
  139. *
  140. * The service name is the string normally passed to CreateMsgService, like "ZARAFA6" or "MSEMS".
  141. *
  142. * @param lpProviderAdmin[in] The ProviderAdmin object passed to MSGServiceEntry
  143. * @param lpServiceName[out] The name of the message service
  144. */
  145. static HRESULT GetServiceName(IProviderAdmin *lpProviderAdmin,
  146. std::string *lpServiceName)
  147. {
  148. lpServiceName->assign("ZARAFA6");
  149. return hrSuccess;
  150. }
  151. static HRESULT
  152. initprov_storepub(struct initprov &d, const sGlobalProfileProps &profprop)
  153. {
  154. /* Get the public store */
  155. std::string redir_srv;
  156. HRESULT ret;
  157. if (profprop.ulProfileFlags & EC_PROFILE_FLAGS_NO_PUBLIC_STORE)
  158. /* skip over to the DeleteProvider part */
  159. ret = MAPI_E_INVALID_PARAMETER;
  160. else
  161. ret = d.transport->HrGetPublicStore(0, &d.eid_size, &~d.eid, &redir_srv);
  162. if (ret == MAPI_E_UNABLE_TO_COMPLETE) {
  163. d.transport->HrLogOff();
  164. auto new_props = profprop;
  165. new_props.strServerPath = redir_srv;
  166. ret = d.transport->HrLogon(new_props);
  167. if (ret == hrSuccess)
  168. ret = d.transport->HrGetPublicStore(0, &d.eid_size, &~d.eid);
  169. }
  170. if (ret == hrSuccess)
  171. return hrSuccess;
  172. if (d.provadm != NULL && d.provuid != NULL)
  173. d.provadm->DeleteProvider(d.provuid);
  174. /* Profile without public store */
  175. return MAPI_S_SPECIAL_OK;
  176. }
  177. static HRESULT
  178. initprov_service(struct initprov &d, const sGlobalProfileProps &profprop)
  179. {
  180. /* Get the default store for this user */
  181. std::string redir_srv;
  182. HRESULT ret = d.transport->HrGetStore(0, NULL, &d.eid_size, &~d.eid,
  183. 0, NULL, &redir_srv);
  184. if (ret == MAPI_E_NOT_FOUND) {
  185. ec_log_err("HrGetStore failed: No store present.");
  186. return ret;
  187. } else if (ret != MAPI_E_UNABLE_TO_COMPLETE) {
  188. return ret;
  189. }
  190. /* MAPI_E_UNABLE_TO_COMPLETE */
  191. d.transport->HrLogOff();
  192. auto new_props = profprop;
  193. new_props.strServerPath = redir_srv;
  194. ret = d.transport->HrLogon(new_props);
  195. if (ret != hrSuccess)
  196. return ret;
  197. ret = d.transport->HrGetStore(0, NULL, &d.eid_size, &~d.eid, 0, NULL);
  198. if (ret != hrSuccess)
  199. return ret;
  200. /* This should be a real URL */
  201. assert(redir_srv.compare(0, 9, "pseudo://") != 0);
  202. if (d.provadm == NULL || redir_srv.empty())
  203. return hrSuccess;
  204. /* Set/update the default store home server. */
  205. auto guid = reinterpret_cast<MAPIUID *>(const_cast<char *>(pbGlobalProfileSectionGuid));
  206. ProfSectPtr globprofsect;
  207. ret = d.provadm->OpenProfileSection(guid, nullptr, MAPI_MODIFY, &~globprofsect);
  208. if (ret != hrSuccess)
  209. return ret;
  210. SPropValue spv;
  211. spv.ulPropTag = PR_EC_PATH;
  212. spv.Value.lpszA = const_cast<char *>(redir_srv.c_str());
  213. return HrSetOneProp(globprofsect, &spv);
  214. }
  215. static HRESULT
  216. initprov_storedl(struct initprov &d, const sGlobalProfileProps &profprop)
  217. {
  218. /* PR_EC_USERNAME is the user we want to add ... */
  219. SPropValuePtr name;
  220. HRESULT ret = HrGetOneProp(d.profsect, PR_EC_USERNAME_W, &~name);
  221. if (ret != hrSuccess)
  222. ret = HrGetOneProp(d.profsect, PR_EC_USERNAME_A, &~name);
  223. if (ret != hrSuccess) {
  224. /*
  225. * This should probably be done in UpdateProviders. But
  226. * UpdateProviders does not know the type of the provider and it
  227. * should not just delete the provider for all types of
  228. * providers.
  229. */
  230. if (d.provadm != NULL && d.provuid != NULL)
  231. d.provadm->DeleteProvider(d.provuid);
  232. /* Invalid or empty delegate store */
  233. return MAPI_S_SPECIAL_OK;
  234. }
  235. std::string redir_srv;
  236. ret = d.transport->HrResolveUserStore(convstring::from_SPropValue(name),
  237. 0, NULL, &d.eid_size, &~d.eid, &redir_srv);
  238. if (ret != MAPI_E_UNABLE_TO_COMPLETE)
  239. return ret;
  240. d.transport->HrLogOff();
  241. auto new_props = profprop;
  242. new_props.strServerPath = redir_srv;
  243. ret = d.transport->HrLogon(new_props);
  244. if (ret != hrSuccess)
  245. return ret;
  246. return d.transport->HrResolveUserStore(convstring::from_SPropValue(name),
  247. 0, NULL, &d.eid_size, &~d.eid);
  248. }
  249. static HRESULT initprov_storearc(struct initprov &d)
  250. {
  251. // We need to get the username and the server name or url from the profsect.
  252. // That's enough information to get the entryid from the correct server. There's no redirect
  253. // available when resolving archive stores.
  254. SPropValuePtr name, server;
  255. HRESULT ret = HrGetOneProp(d.profsect, PR_EC_USERNAME_W, &~name);
  256. if (ret != hrSuccess)
  257. ret = HrGetOneProp(d.profsect, PR_EC_USERNAME_A, &~name);
  258. if (ret == hrSuccess) {
  259. ret = HrGetOneProp(d.profsect, PR_EC_SERVERNAME_W, &~server);
  260. if (ret != hrSuccess)
  261. ret = HrGetOneProp(d.profsect, PR_EC_SERVERNAME_A, &~server);
  262. if (ret != hrSuccess)
  263. return MAPI_E_UNCONFIGURED;
  264. }
  265. if (ret != hrSuccess) {
  266. /*
  267. * This should probably be done in UpdateProviders. But
  268. * UpdateProviders does not know the type of the provider and
  269. * it should not just delete the provider for all types of
  270. * providers.
  271. */
  272. if (d.provadm != NULL && d.provuid != NULL)
  273. d.provadm->DeleteProvider(d.provuid);
  274. /* Invalid or empty archive store */
  275. return MAPI_S_SPECIAL_OK;
  276. }
  277. WSTransport *alt_transport;
  278. ret = GetTransportToNamedServer(d.transport, server->Value.LPSZ,
  279. (PROP_TYPE(name->ulPropTag) == PT_STRING8 ? 0 : MAPI_UNICODE),
  280. &alt_transport);
  281. if (ret != hrSuccess)
  282. return ret;
  283. std::swap(d.transport, alt_transport);
  284. alt_transport->Release();
  285. alt_transport = NULL;
  286. return d.transport->HrResolveTypedStore(convstring::from_SPropValue(name),
  287. ECSTORE_TYPE_ARCHIVE, &d.eid_size, &~d.eid);
  288. }
  289. static HRESULT
  290. initprov_mapi_store(struct initprov &d, const sGlobalProfileProps &profprop)
  291. {
  292. SPropValuePtr mdb;
  293. HRESULT ret = HrGetOneProp(d.profsect, PR_MDB_PROVIDER, &~mdb);
  294. if (ret != hrSuccess)
  295. return ret;
  296. if (CompareMDBProvider(mdb->Value.bin.lpb, &KOPANO_STORE_PUBLIC_GUID)) {
  297. ret = initprov_storepub(d, profprop);
  298. if (ret != hrSuccess)
  299. return ret;
  300. } else if (CompareMDBProvider(mdb->Value.bin.lpb, &KOPANO_SERVICE_GUID)) {
  301. ret = initprov_service(d, profprop);
  302. if (ret != hrSuccess)
  303. return ret;
  304. } else if(CompareMDBProvider(mdb->Value.bin.lpb, &KOPANO_STORE_DELEGATE_GUID)) {
  305. ret = initprov_storedl(d, profprop);
  306. if (ret != hrSuccess)
  307. return ret;
  308. } else if(CompareMDBProvider(mdb->Value.bin.lpb, &KOPANO_STORE_ARCHIVE_GUID)) {
  309. ret = initprov_storearc(d);
  310. if (ret != hrSuccess)
  311. return ret;
  312. } else {
  313. assert(false); // unknown GUID?
  314. return hrSuccess;
  315. }
  316. ret = d.transport->HrGetStoreName(d.eid_size, d.eid, MAPI_UNICODE,
  317. static_cast<LPTSTR *>(&~d.store_name));
  318. if (ret != hrSuccess)
  319. return ret;
  320. ret = WrapStoreEntryID(0, reinterpret_cast<LPTSTR>(const_cast<char *>(WCLIENT_DLL_NAME)),
  321. d.eid_size, d.eid, &d.wrap_eid_size, &~d.wrap_eid);
  322. if (ret != hrSuccess)
  323. return ret;
  324. d.prop[d.count].ulPropTag = PR_ENTRYID;
  325. d.prop[d.count].Value.bin.cb = d.wrap_eid_size;
  326. d.prop[d.count++].Value.bin.lpb = reinterpret_cast<BYTE *>(d.wrap_eid.get());
  327. d.prop[d.count].ulPropTag = PR_RECORD_KEY;
  328. d.prop[d.count].Value.bin.cb = sizeof(MAPIUID);
  329. d.prop[d.count++].Value.bin.lpb = (LPBYTE)&reinterpret_cast<PEID>(d.eid.get())->guid; //@FIXME validate guid
  330. d.prop[d.count].ulPropTag = PR_DISPLAY_NAME_W;
  331. d.prop[d.count++].Value.lpszW = reinterpret_cast<wchar_t *>(d.store_name.get());
  332. d.prop[d.count].ulPropTag = PR_EC_PATH;
  333. d.prop[d.count++].Value.lpszA = const_cast<char *>("Server");
  334. d.prop[d.count].ulPropTag = PR_PROVIDER_DLL_NAME_A;
  335. d.prop[d.count++].Value.lpszA = const_cast<char *>(WCLIENT_DLL_NAME);
  336. return hrSuccess;
  337. }
  338. static HRESULT initprov_addrbook(struct initprov &d)
  339. {
  340. ABEID *eidptr;
  341. size_t abe_size = CbNewABEID("");
  342. HRESULT ret = MAPIAllocateBuffer(abe_size, reinterpret_cast<void **>(&eidptr));
  343. if (ret != hrSuccess)
  344. return ret;
  345. d.abe_id.reset(eidptr);
  346. memset(eidptr, 0, abe_size);
  347. memcpy(&d.abe_id->guid, &MUIDECSAB, sizeof(GUID));
  348. d.abe_id->ulType = MAPI_ABCONT;
  349. d.prop[d.count].ulPropTag = PR_ENTRYID;
  350. d.prop[d.count].Value.bin.cb = abe_size;
  351. d.prop[d.count++].Value.bin.lpb = reinterpret_cast<BYTE *>(eidptr);
  352. d.prop[d.count].ulPropTag = PR_RECORD_KEY;
  353. d.prop[d.count].Value.bin.cb = sizeof(MAPIUID);
  354. d.prop[d.count++].Value.bin.lpb = reinterpret_cast<BYTE *>(const_cast<GUID *>(&MUIDECSAB));
  355. d.prop[d.count].ulPropTag = PR_DISPLAY_NAME_A;
  356. d.prop[d.count++].Value.lpszA = const_cast<char *>("Kopano Addressbook");
  357. d.prop[d.count].ulPropTag = PR_EC_PATH;
  358. d.prop[d.count++].Value.lpszA = const_cast<char *>("Kopano Addressbook");
  359. d.prop[d.count].ulPropTag = PR_PROVIDER_DLL_NAME_A;
  360. d.prop[d.count++].Value.lpszA = const_cast<char *>(WCLIENT_DLL_NAME);
  361. return hrSuccess;
  362. }
  363. /**
  364. * Initialize one service provider
  365. *
  366. * @param lpAdminProvider[in] Pointer to provider admin.
  367. * @param lpProfSect[in] Pointer to provider profile section.
  368. * @param sProfileProps[in] Global profile properties
  369. * @param lpcStoreID[out] Size of lppStoreID
  370. * @param lppStoreID[out] Entryid of the store of the provider
  371. *
  372. * @return MAPI error codes
  373. */
  374. HRESULT InitializeProvider(LPPROVIDERADMIN lpAdminProvider,
  375. IProfSect *lpProfSect, const sGlobalProfileProps &sProfileProps,
  376. ULONG *lpcStoreID, LPENTRYID *lppStoreID, WSTransport *transport)
  377. {
  378. HRESULT hr = hrSuccess;
  379. SPropValuePtr ptrPropValueResourceType;
  380. SPropValuePtr ptrPropValueProviderUid;
  381. std::string strServiceName;
  382. ULONG ulResourceType=0;
  383. struct initprov d;
  384. d.provadm = lpAdminProvider;
  385. d.profsect = lpProfSect;
  386. d.count = d.eid_size = 0;
  387. d.transport = NULL;
  388. if (d.provadm != NULL) {
  389. hr = GetServiceName(d.provadm, &strServiceName);
  390. if (hr != hrSuccess)
  391. goto exit;
  392. } else {
  393. SPropValuePtr psn;
  394. hr = HrGetOneProp(d.profsect, PR_SERVICE_NAME_A, &~psn);
  395. if(hr == hrSuccess)
  396. strServiceName = psn->Value.lpszA;
  397. hr = hrSuccess;
  398. }
  399. hr = HrGetOneProp(d.profsect, PR_RESOURCE_TYPE, &~ptrPropValueResourceType);
  400. if(hr != hrSuccess) {
  401. // Ignore this provider; apparently it has no resource type, so just skip it
  402. hr = hrSuccess;
  403. goto exit;
  404. }
  405. if (HrGetOneProp(d.profsect, PR_PROVIDER_UID, &~ptrPropValueProviderUid) == hrSuccess &&
  406. ptrPropValueProviderUid != nullptr)
  407. d.provuid = reinterpret_cast<MAPIUID *>(ptrPropValueProviderUid.get()->Value.bin.lpb);
  408. else
  409. d.provuid = nullptr;
  410. ulResourceType = ptrPropValueResourceType->Value.l;
  411. TRACE_MAPI(TRACE_INFO, "InitializeProvider", "Resource type=%s", ResourceTypeToString(ulResourceType) );
  412. if (transport != NULL) {
  413. d.transport = transport;
  414. } else {
  415. hr = WSTransport::Create(0, &d.transport);
  416. if (hr != hrSuccess)
  417. goto exit;
  418. }
  419. hr = d.transport->HrLogon(sProfileProps);
  420. if(hr != hrSuccess)
  421. goto exit;
  422. if(ulResourceType == MAPI_STORE_PROVIDER)
  423. {
  424. hr = initprov_mapi_store(d, sProfileProps);
  425. if (hr != hrSuccess)
  426. goto exit;
  427. } else if(ulResourceType == MAPI_AB_PROVIDER) {
  428. hr = initprov_addrbook(d);
  429. if (hr != hrSuccess)
  430. goto exit;
  431. } else {
  432. if (ulResourceType != MAPI_TRANSPORT_PROVIDER)
  433. assert(false);
  434. goto exit;
  435. }
  436. hr = d.profsect->SetProps(d.count, d.prop, NULL);
  437. if(hr != hrSuccess)
  438. goto exit;
  439. hr = d.profsect->SaveChanges(0);
  440. if(hr != hrSuccess)
  441. goto exit;
  442. if (lpcStoreID && lppStoreID) {
  443. *lpcStoreID = d.eid_size;
  444. hr = MAPIAllocateBuffer(d.eid_size, reinterpret_cast<void **>(lppStoreID));
  445. if(hr != hrSuccess)
  446. goto exit;
  447. memcpy(*lppStoreID, d.eid, d.eid_size);
  448. }
  449. exit:
  450. //Free allocated memory
  451. if (d.transport != NULL && d.transport != transport)
  452. d.transport->Release(); /* implies logoff */
  453. else if (d.transport != NULL)
  454. d.transport->logoff_nd();
  455. if (hr == MAPI_S_SPECIAL_OK)
  456. return hrSuccess;
  457. return hr;
  458. }
  459. static HRESULT UpdateProviders(LPPROVIDERADMIN lpAdminProviders,
  460. const sGlobalProfileProps &sProfileProps, WSTransport *transport)
  461. {
  462. HRESULT hr;
  463. ProfSectPtr ptrProfSect;
  464. MAPITablePtr ptrTable;
  465. SRowSetPtr ptrRows;
  466. // Get the provider table
  467. hr = lpAdminProviders->GetProviderTable(0, &~ptrTable);
  468. if(hr != hrSuccess)
  469. return hr;
  470. // Get the rows
  471. hr = ptrTable->QueryRows(0xFF, 0, &ptrRows);
  472. if(hr != hrSuccess)
  473. return hr;
  474. //Check if exist one or more rows
  475. if (ptrRows.size() == 0)
  476. return MAPI_E_NOT_FOUND;
  477. // Scan the rows for message stores
  478. for (ULONG curRow = 0; curRow < ptrRows.size(); ++curRow) {
  479. //Get de UID of the provider to open the profile section
  480. auto lpsProviderUID = PCpropFindProp(ptrRows[curRow].lpProps, ptrRows[curRow].cValues, PR_PROVIDER_UID);
  481. if(lpsProviderUID == NULL || lpsProviderUID->Value.bin.cb == 0) {
  482. // Provider without a provider uid, just move to the next
  483. assert(false);
  484. continue;
  485. }
  486. hr = lpAdminProviders->OpenProfileSection((MAPIUID *)lpsProviderUID->Value.bin.lpb, nullptr, MAPI_MODIFY, &~ptrProfSect);
  487. if(hr != hrSuccess)
  488. return hr;
  489. // Set already PR_PROVIDER_UID, ignore error
  490. HrSetOneProp(ptrProfSect, lpsProviderUID);
  491. hr = InitializeProvider(lpAdminProviders, ptrProfSect, sProfileProps, NULL, NULL, transport);
  492. if (hr != hrSuccess)
  493. return hr;
  494. }
  495. return hrSuccess;
  496. }
  497. static std::string GetServerTypeFromPath(const char *szPath)
  498. {
  499. std::string path = szPath;
  500. size_t pos;
  501. pos = path.find("://");
  502. if (pos != std::string::npos)
  503. return path.substr(0, pos);
  504. return std::string();
  505. }
  506. // Called by MAPI to configure, or create a service
  507. extern "C" HRESULT __stdcall MSGServiceEntry(HINSTANCE hInst,
  508. LPMALLOC lpMalloc, LPMAPISUP psup, ULONG ulUIParam, ULONG ulFlags,
  509. ULONG ulContext, ULONG cvals, LPSPropValue pvals,
  510. LPPROVIDERADMIN lpAdminProviders, MAPIERROR **lppMapiError)
  511. {
  512. TRACE_MAPI(TRACE_ENTRY, "MSGServiceEntry", "flags=0x%08X, context=%s", ulFlags, MsgServiceContextToString(ulContext));
  513. HRESULT hr = erSuccess;
  514. std::string strServerName;
  515. std::wstring strUserName;
  516. std::wstring strUserPassword;
  517. std::string strServerPort;
  518. std::string strDefaultOfflinePath;
  519. std::string strType;
  520. std::string strDefStoreServer;
  521. sGlobalProfileProps sProfileProps;
  522. std::basic_string<TCHAR> strError;
  523. ProfSectPtr ptrGlobalProfSect;
  524. ProfSectPtr ptrProfSect;
  525. MAPISessionPtr ptrSession;
  526. object_ptr<WSTransport> lpTransport;
  527. memory_ptr<SPropValue> lpsPropValue;
  528. ULONG cValues = 0;
  529. bool bShowDialog = false;
  530. MAPIERROR *lpMapiError = NULL;
  531. memory_ptr<BYTE> lpDelegateStores;
  532. ULONG cDelegateStores = 0;
  533. ULONG cValueIndex = 0;
  534. convert_context converter;
  535. bool bGlobalProfileUpdate = false;
  536. bool bUpdatedPageConnection = false;
  537. bool bInitStores = true;
  538. _hInstance = hInst;
  539. if (psup) {
  540. hr = psup->GetMemAllocRoutines(&_pfnAllocBuf, &_pfnAllocMore, &_pfnFreeBuf);
  541. if (hr != hrSuccess)
  542. assert(false);
  543. } else {
  544. // Support object not available on linux at this time... TODO: fix mapi4linux?
  545. _pfnAllocBuf = MAPIAllocateBuffer;
  546. _pfnAllocMore = MAPIAllocateMore;
  547. _pfnFreeBuf = MAPIFreeBuffer;
  548. }
  549. // Logon defaults
  550. strType = "http";
  551. strServerName = "";
  552. strServerPort ="236";
  553. switch(ulContext) {
  554. case MSG_SERVICE_INSTALL:
  555. hr = hrSuccess;
  556. break;
  557. case MSG_SERVICE_UNINSTALL:
  558. hr = hrSuccess;
  559. break;
  560. case MSG_SERVICE_DELETE:
  561. hr = hrSuccess;
  562. break;
  563. case MSG_SERVICE_PROVIDER_CREATE: {
  564. if (cvals == 0 || pvals == nullptr)
  565. break;
  566. const SPropValue *lpsPropName = NULL;
  567. auto lpsPropValueFind = PCpropFindProp(pvals, cvals, PR_PROVIDER_UID);
  568. if (lpsPropValueFind == NULL || lpsPropValueFind->Value.bin.cb == 0)
  569. {
  570. //FIXME: give the right error?
  571. hr = MAPI_E_UNCONFIGURED;
  572. goto exit;
  573. }
  574. // PR_EC_USERNAME is the user we're adding ...
  575. lpsPropName = PCpropFindProp(pvals, cvals, CHANGE_PROP_TYPE(PR_EC_USERNAME_A, PT_UNSPECIFIED));
  576. if (lpsPropName == NULL || lpsPropName->Value.bin.cb == 0)
  577. {
  578. hr = MAPI_E_UNCONFIGURED;
  579. goto exit;
  580. }
  581. //Open profile section
  582. hr = lpAdminProviders->OpenProfileSection((MAPIUID *)lpsPropValueFind->Value.bin.lpb, nullptr, MAPI_MODIFY, &~ptrProfSect);
  583. if (hr != hrSuccess)
  584. goto exit;
  585. hr = HrSetOneProp(ptrProfSect, lpsPropName);
  586. if (hr != hrSuccess)
  587. goto exit;
  588. hr = lpAdminProviders->OpenProfileSection((LPMAPIUID)pbGlobalProfileSectionGuid, nullptr, MAPI_MODIFY, &~ptrGlobalProfSect);
  589. if (hr != hrSuccess)
  590. goto exit;
  591. // Get username/pass settings
  592. hr = ClientUtil::GetGlobalProfileProperties(ptrGlobalProfSect, &sProfileProps);
  593. if (hr != hrSuccess)
  594. goto exit;
  595. if (sProfileProps.strUserName.empty() || sProfileProps.strServerPath.empty()) {
  596. hr = MAPI_E_UNCONFIGURED; // @todo: check if this is the right error
  597. goto exit;
  598. }
  599. hr = InitializeProvider(lpAdminProviders, ptrProfSect, sProfileProps, NULL, NULL, NULL);
  600. if (hr != hrSuccess)
  601. goto exit;
  602. break;
  603. }
  604. case MSG_SERVICE_PROVIDER_DELETE:
  605. hr = hrSuccess;
  606. //FIXME: delete Offline database
  607. break;
  608. case MSG_SERVICE_CONFIGURE:
  609. //bShowAllSettingsPages = true;
  610. // Do not break here
  611. case MSG_SERVICE_CREATE:
  612. //Open global profile, add the store.(for show list, delete etc)
  613. hr = lpAdminProviders->OpenProfileSection((LPMAPIUID)pbGlobalProfileSectionGuid, nullptr, MAPI_MODIFY , &~ptrGlobalProfSect);
  614. if(hr != hrSuccess)
  615. goto exit;
  616. if(cvals) {
  617. hr = ptrGlobalProfSect->SetProps(cvals, pvals, NULL);
  618. if(hr != hrSuccess)
  619. goto exit;
  620. }
  621. hr = ClientUtil::GetGlobalProfileProperties(ptrGlobalProfSect, &sProfileProps);
  622. if (sProfileProps.strServerPath.empty() ||
  623. sProfileProps.strUserName.empty() ||
  624. (sProfileProps.strPassword.empty() &&
  625. sProfileProps.strSSLKeyFile.empty()))
  626. bShowDialog = true;
  627. //FIXME: check here offline path with the flags
  628. if(!sProfileProps.strServerPath.empty()) {
  629. strServerName = GetServerNameFromPath(sProfileProps.strServerPath.c_str());
  630. strServerPort = GetServerPortFromPath(sProfileProps.strServerPath.c_str());
  631. strType = GetServerTypeFromPath(sProfileProps.strServerPath.c_str());
  632. }
  633. // Get deligate stores, Ignore error
  634. ClientUtil::GetGlobalProfileDelegateStoresProp(ptrGlobalProfSect, &cDelegateStores, &~lpDelegateStores);
  635. // init defaults
  636. hr = WSTransport::Create(ulFlags & SERVICE_UI_ALLOWED ? 0 : MDB_NO_DIALOG, &~lpTransport);
  637. if(hr != hrSuccess)
  638. goto exit;
  639. // Check the path, username and password
  640. while(1)
  641. {
  642. bGlobalProfileUpdate = false;
  643. bUpdatedPageConnection = false;
  644. if ((bShowDialog && ulFlags & SERVICE_UI_ALLOWED) || ulFlags & SERVICE_UI_ALWAYS)
  645. hr = MAPI_E_USER_CANCEL;
  646. if(!(ulFlags & SERVICE_UI_ALLOWED || ulFlags & SERVICE_UI_ALWAYS) && (strServerName.empty() || sProfileProps.strUserName.empty())){
  647. hr = MAPI_E_UNCONFIGURED;
  648. goto exit;
  649. }else if(!strServerName.empty() && !sProfileProps.strUserName.empty()) {
  650. //Logon the server
  651. hr = lpTransport->HrLogon(sProfileProps);
  652. }else{
  653. hr = MAPI_E_LOGON_FAILED;
  654. }
  655. if(hr == MAPI_E_LOGON_FAILED || hr == MAPI_E_NETWORK_ERROR || hr == MAPI_E_VERSION || hr == MAPI_E_INVALID_PARAMETER) {
  656. bShowDialog = true;
  657. } else if(hr != erSuccess){ // Big error?
  658. bShowDialog = true;
  659. assert(false);
  660. }else {
  661. //Update global profile
  662. if( bGlobalProfileUpdate == true) {
  663. cValues = 12;
  664. cValueIndex = 0;
  665. hr = MAPIAllocateBuffer(sizeof(SPropValue) * cValues, &~lpsPropValue);
  666. if(hr != hrSuccess)
  667. goto exit;
  668. lpsPropValue[cValueIndex].ulPropTag = PR_EC_PATH;
  669. lpsPropValue[cValueIndex++].Value.lpszA = (char *)sProfileProps.strServerPath.c_str();
  670. lpsPropValue[cValueIndex].ulPropTag = PR_EC_USERNAME_W;
  671. lpsPropValue[cValueIndex++].Value.lpszW = (wchar_t *)sProfileProps.strUserName.c_str();
  672. lpsPropValue[cValueIndex].ulPropTag = PR_EC_USERPASSWORD_W;
  673. lpsPropValue[cValueIndex++].Value.lpszW = (wchar_t *)sProfileProps.strPassword.c_str();
  674. lpsPropValue[cValueIndex].ulPropTag = PR_EC_FLAGS;
  675. lpsPropValue[cValueIndex++].Value.ul = sProfileProps.ulProfileFlags;
  676. lpsPropValue[cValueIndex].ulPropTag = PR_EC_STATS_SESSION_CLIENT_APPLICATION_VERSION;
  677. lpsPropValue[cValueIndex++].Value.lpszA = (char *)sProfileProps.strClientAppVersion.c_str();
  678. lpsPropValue[cValueIndex].ulPropTag = PR_EC_STATS_SESSION_CLIENT_APPLICATION_MISC;
  679. lpsPropValue[cValueIndex++].Value.lpszA = (char *)sProfileProps.strClientAppMisc.c_str();
  680. if (bUpdatedPageConnection == true)
  681. {
  682. lpsPropValue[cValueIndex].ulPropTag = PR_EC_CONNECTION_TIMEOUT;
  683. lpsPropValue[cValueIndex++].Value.ul = sProfileProps.ulConnectionTimeOut;
  684. // Proxy settings
  685. lpsPropValue[cValueIndex].ulPropTag = PR_EC_PROXY_FLAGS;
  686. lpsPropValue[cValueIndex++].Value.ul = sProfileProps.ulProxyFlags;
  687. lpsPropValue[cValueIndex].ulPropTag = PR_EC_PROXY_PORT;
  688. lpsPropValue[cValueIndex++].Value.ul = sProfileProps.ulProxyPort;
  689. lpsPropValue[cValueIndex].ulPropTag = PR_EC_PROXY_HOST;
  690. lpsPropValue[cValueIndex++].Value.lpszA = (char *)sProfileProps.strProxyHost.c_str();
  691. lpsPropValue[cValueIndex].ulPropTag = PR_EC_PROXY_USERNAME;
  692. lpsPropValue[cValueIndex++].Value.lpszA = (char *)sProfileProps.strProxyUserName.c_str();
  693. lpsPropValue[cValueIndex].ulPropTag = PR_EC_PROXY_PASSWORD;
  694. lpsPropValue[cValueIndex++].Value.lpszA = (char *)sProfileProps.strProxyPassword.c_str();
  695. }
  696. hr = ptrGlobalProfSect->SetProps(cValueIndex, lpsPropValue, NULL);
  697. if(hr != hrSuccess)
  698. goto exit;
  699. }
  700. break; // Everything is oke
  701. }
  702. // On incorrect password, and UI allowed, show incorrect password error
  703. if((ulFlags & SERVICE_UI_ALLOWED || ulFlags & SERVICE_UI_ALWAYS)) {
  704. // what do we do on linux?
  705. cout << "Access Denied: Incorrect username and/or password." << endl;
  706. hr = MAPI_E_UNCONFIGURED;
  707. goto exit;
  708. }else if(!(ulFlags & SERVICE_UI_ALLOWED || ulFlags & SERVICE_UI_ALWAYS)){
  709. // Do not reset the logon error from HrLogon()
  710. // The DAgent uses this value to determain if the delivery is fatal or not
  711. //
  712. // Although this error is not in the online spec from MS, it should not really matter .... right?
  713. // hr = MAPI_E_UNCONFIGURED;
  714. goto exit;
  715. }
  716. }// while(1)
  717. if(bInitStores) {
  718. hr = UpdateProviders(lpAdminProviders, sProfileProps, lpTransport);
  719. if(hr != hrSuccess)
  720. goto exit;
  721. }
  722. break;
  723. } // switch(ulContext)
  724. exit:
  725. if (lppMapiError) {
  726. *lppMapiError = NULL;
  727. if(hr != hrSuccess) {
  728. memory_ptr<TCHAR> lpszErrorMsg;
  729. if (Util::HrMAPIErrorToText(hr, &~lpszErrorMsg) == hrSuccess) {
  730. // Set Error
  731. strError = _T("EntryPoint: ");
  732. strError += lpszErrorMsg;
  733. // Some outlook 2007 clients can't allocate memory so check it
  734. if(MAPIAllocateBuffer(sizeof(MAPIERROR), (void**)&lpMapiError) == hrSuccess) {
  735. memset(lpMapiError, 0, sizeof(MAPIERROR));
  736. if (ulFlags & MAPI_UNICODE) {
  737. std::wstring wstrErrorMsg = convert_to<std::wstring>(strError);
  738. std::wstring wstrCompName = convert_to<std::wstring>(g_strProductName.c_str());
  739. if ((hr = MAPIAllocateMore(sizeof(std::wstring::value_type) * (wstrErrorMsg.size() + 1), lpMapiError, (void**)&lpMapiError->lpszError)) != hrSuccess)
  740. goto exit;
  741. wcscpy((wchar_t*)lpMapiError->lpszError, wstrErrorMsg.c_str());
  742. if ((hr = MAPIAllocateMore(sizeof(std::wstring::value_type) * (wstrCompName.size() + 1), lpMapiError, (void**)&lpMapiError->lpszComponent)) != hrSuccess)
  743. goto exit;
  744. wcscpy((wchar_t*)lpMapiError->lpszComponent, wstrCompName.c_str());
  745. } else {
  746. std::string strErrorMsg = convert_to<std::string>(strError);
  747. std::string strCompName = convert_to<std::string>(g_strProductName.c_str());
  748. if ((hr = MAPIAllocateMore(strErrorMsg.size() + 1, lpMapiError, (void**)&lpMapiError->lpszError)) != hrSuccess)
  749. goto exit;
  750. strcpy((char*)lpMapiError->lpszError, strErrorMsg.c_str());
  751. if ((hr = MAPIAllocateMore(strCompName.size() + 1, lpMapiError, (void**)&lpMapiError->lpszComponent)) != hrSuccess)
  752. goto exit;
  753. strcpy((char*)lpMapiError->lpszComponent, strCompName.c_str());
  754. }
  755. lpMapiError->ulVersion = 0;
  756. lpMapiError->ulLowLevelError = 0;
  757. lpMapiError->ulContext = 0;
  758. *lppMapiError = lpMapiError;
  759. }
  760. }
  761. }
  762. }
  763. TRACE_MAPI(TRACE_RETURN, "MSGServiceEntry", "%s", GetMAPIErrorDescription(hr).c_str());
  764. return hr;
  765. }
  766. HRESULT __cdecl XPProviderInit(HINSTANCE hInstance, LPMALLOC lpMalloc,
  767. LPALLOCATEBUFFER lpAllocateBuffer, LPALLOCATEMORE lpAllocateMore,
  768. LPFREEBUFFER lpFreeBuffer, ULONG ulFlags, ULONG ulMAPIVer,
  769. ULONG *lpulProviderVer, LPXPPROVIDER *lppXPProvider)
  770. {
  771. TRACE_MAPI(TRACE_ENTRY, "XPProviderInit", "");
  772. HRESULT hr = hrSuccess;
  773. object_ptr<ECXPProvider> pXPProvider;
  774. if (ulMAPIVer < CURRENT_SPI_VERSION)
  775. return MAPI_E_VERSION;
  776. *lpulProviderVer = CURRENT_SPI_VERSION;
  777. // Save the pointer to the allocation routines in global variables
  778. _pmalloc = lpMalloc;
  779. _pfnAllocBuf = lpAllocateBuffer;
  780. _pfnAllocMore = lpAllocateMore;
  781. _pfnFreeBuf = lpFreeBuffer;
  782. _hInstance = hInstance;
  783. hr = ECXPProvider::Create(&~pXPProvider);
  784. if(hr != hrSuccess)
  785. return hr;
  786. return pXPProvider->QueryInterface(IID_IXPProvider,
  787. reinterpret_cast<void **>(lppXPProvider));
  788. }
  789. HRESULT __cdecl ABProviderInit(HINSTANCE hInstance, LPMALLOC lpMalloc,
  790. LPALLOCATEBUFFER lpAllocateBuffer, LPALLOCATEMORE lpAllocateMore,
  791. LPFREEBUFFER lpFreeBuffer, ULONG ulFlags, ULONG ulMAPIVer,
  792. ULONG *lpulProviderVer, LPABPROVIDER *lppABProvider)
  793. {
  794. TRACE_MAPI(TRACE_ENTRY, "ABProviderInit", "");
  795. if (ulMAPIVer < CURRENT_SPI_VERSION)
  796. return MAPI_E_VERSION;
  797. *lpulProviderVer = CURRENT_SPI_VERSION;
  798. // Save the pointer to the allocation routines in global variables
  799. _pmalloc = lpMalloc;
  800. _pfnAllocBuf = lpAllocateBuffer;
  801. _pfnAllocMore = lpAllocateMore;
  802. _pfnFreeBuf = lpFreeBuffer;
  803. _hInstance = hInstance;
  804. object_ptr<ECABProviderSwitch> lpABProvider;
  805. HRESULT hr = ECABProviderSwitch::Create(&~lpABProvider);
  806. if (hr == hrSuccess)
  807. hr = lpABProvider->QueryInterface(IID_IABProvider,
  808. reinterpret_cast<void **>(lppABProvider));
  809. return hr;
  810. }