ProtocolBase.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 <kopano/memory.hpp>
  19. #include "ProtocolBase.h"
  20. #include <kopano/stringutil.h>
  21. #include <kopano/CommonUtil.h>
  22. #include "CalDavUtil.h"
  23. #include <kopano/mapi_ptr.h>
  24. using namespace std;
  25. using namespace KCHL;
  26. ProtocolBase::ProtocolBase(Http *lpRequest, IMAPISession *lpSession,
  27. const std::string &strSrvTz, const std::string &strCharset) :
  28. m_lpRequest(lpRequest), m_lpSession(lpSession), m_strSrvTz(strSrvTz),
  29. m_strCharset(strCharset)
  30. {
  31. }
  32. ProtocolBase::~ProtocolBase()
  33. {
  34. if (m_lpLoginUser)
  35. m_lpLoginUser->Release();
  36. if (m_lpActiveUser)
  37. m_lpActiveUser->Release();
  38. if (m_lpIPMSubtree)
  39. m_lpIPMSubtree->Release();
  40. if (m_lpUsrFld)
  41. m_lpUsrFld->Release();
  42. if (m_lpAddrBook)
  43. m_lpAddrBook->Release();
  44. if (m_lpDefStore)
  45. m_lpDefStore->Release();
  46. if (m_lpActiveStore)
  47. m_lpActiveStore->Release();
  48. }
  49. /**
  50. * Opens the store and folders required for the Request. Also checks
  51. * if DELETE or RENAME actions are allowed on the folder.
  52. *
  53. * @return MAPI error code
  54. * @retval MAPI_E_NO_ACCESS Not enough permissions to open the folder or store
  55. * @retval MAPI_E_NOT_FOUND Folder requested by client not found
  56. */
  57. HRESULT ProtocolBase::HrInitializeClass()
  58. {
  59. HRESULT hr = hrSuccess;
  60. std::string strUrl;
  61. std::string strMethod;
  62. string strFldOwner;
  63. string strFldName;
  64. memory_ptr<SPropValue> lpDefaultProp, lpFldProp;
  65. SPropValuePtr lpEntryID;
  66. ULONG ulRes = 0;
  67. bool bIsPublic = false;
  68. ULONG ulType = 0;
  69. MAPIFolderPtr lpRoot;
  70. /* URLs
  71. *
  72. * /caldav/ | depth: 0 results in root props, depth: 1 results in 0 + calendar FOLDER list current user. eg: the same happens /caldav/self/
  73. * /caldav/self/ | depth: 0 results in root props, depth: 1 results in 0 + calendar FOLDER list current user. see ^^
  74. * /caldav/self/Calendar/ | depth: 0 results in root props, depth: 1 results in 0 + _GIVEN_ calendar CONTENTS list current user.
  75. * /caldav/self/_NAMED_FOLDER_ID_/ (evo has date? still??) (we also do hexed entryids?)
  76. * /caldav/other/[...]
  77. * /caldav/public/[...]
  78. *
  79. * /caldav/user/folder/folder/ | subfolders are not allowed!
  80. * /caldav/user/folder/id.ics | a message (note: not ending in /)
  81. */
  82. m_lpRequest->HrGetUrl(&strUrl);
  83. m_lpRequest->HrGetUser(&m_wstrUser);
  84. m_lpRequest->HrGetMethod(&strMethod);
  85. HrParseURL(strUrl, &m_ulUrlFlag, &strFldOwner, &strFldName);
  86. m_wstrFldOwner = U2W(strFldOwner);
  87. m_wstrFldName = U2W(strFldName);
  88. bIsPublic = m_ulUrlFlag & REQ_PUBLIC;
  89. if (m_wstrFldOwner.empty())
  90. m_wstrFldOwner = m_wstrUser;
  91. hr = m_lpSession->OpenAddressBook(0, NULL, 0, &m_lpAddrBook);
  92. if(hr != hrSuccess)
  93. {
  94. ec_log_err("Error opening addressbook, error code: 0x%08X", hr);
  95. return hr;
  96. }
  97. // default store required for various actions (delete, freebusy, ...)
  98. hr = HrOpenDefaultStore(m_lpSession, &m_lpDefStore);
  99. if(hr != hrSuccess)
  100. {
  101. ec_log_err("Error opening default store of user %ls, error code: 0x%08X", m_wstrUser.c_str(), hr);
  102. return hr;
  103. }
  104. hr = HrGetOwner(m_lpSession, m_lpDefStore, &m_lpLoginUser);
  105. if(hr != hrSuccess)
  106. return hr;
  107. /*
  108. * Set m_lpActiveStore
  109. */
  110. if (bIsPublic)
  111. {
  112. // open public
  113. hr = HrOpenECPublicStore(m_lpSession, &m_lpActiveStore);
  114. if (hr != hrSuccess) {
  115. ec_log_err("Unable to open public store with user %ls, error code: 0x%08X", m_wstrUser.c_str(), hr);
  116. return hr;
  117. }
  118. } else if (wcscasecmp(m_wstrUser.c_str(), m_wstrFldOwner.c_str())) {
  119. // open shared store
  120. hr = HrOpenUserMsgStore(m_lpSession, (WCHAR*)m_wstrFldOwner.c_str(), &m_lpActiveStore);
  121. if (hr != hrSuccess) {
  122. ec_log_err("Unable to open store of user %ls with user %ls, error code: 0x%08X", m_wstrFldOwner.c_str(), m_wstrUser.c_str(), hr);
  123. return hr;
  124. }
  125. m_ulFolderFlag |= SHARED_FOLDER;
  126. } else {
  127. // @todo, make auto pointers
  128. hr = m_lpDefStore->QueryInterface(IID_IMsgStore, (void**)&m_lpActiveStore);
  129. if (hr != hrSuccess)
  130. return hr;
  131. }
  132. // Retrieve named properties
  133. hr = HrLookupNames(m_lpActiveStore, &~m_lpNamedProps);
  134. if (hr != hrSuccess)
  135. return hr;
  136. // get active user info
  137. if (bIsPublic)
  138. hr = m_lpLoginUser->QueryInterface(IID_IMailUser, (void**)&m_lpActiveUser);
  139. else
  140. hr = HrGetOwner(m_lpSession, m_lpActiveStore, &m_lpActiveUser);
  141. if(hr != hrSuccess)
  142. return hr;
  143. /*
  144. * Set m_lpIPMSubtree, used for CopyFolder, CreateFolder, DeleteFolder
  145. */
  146. hr = OpenSubFolder(m_lpActiveStore, NULL, '/', bIsPublic, false, &m_lpIPMSubtree);
  147. if(hr != hrSuccess)
  148. {
  149. ec_log_err("Error opening IPM SUBTREE, using user %ls, error code: 0x%08X", m_wstrUser.c_str(), hr);
  150. return hr;
  151. }
  152. // Get active store default calendar to prevent delete action on this folder
  153. hr = m_lpActiveStore->OpenEntry(0, nullptr, nullptr, 0, &ulType, &~lpRoot);
  154. if(hr != hrSuccess)
  155. {
  156. ec_log_err("Error opening root container, using user %ls, error code: 0x%08X", m_wstrUser.c_str(), hr);
  157. return hr;
  158. }
  159. if (!bIsPublic) {
  160. // get default calendar entryid for non-public stores
  161. hr = HrGetOneProp(lpRoot, PR_IPM_APPOINTMENT_ENTRYID, &~lpDefaultProp);
  162. if(hr != hrSuccess)
  163. {
  164. ec_log_err("Error retrieving Entry id of Default calendar for user %ls, error code: 0x%08X", m_wstrUser.c_str(), hr);
  165. return hr;
  166. }
  167. }
  168. /*
  169. * Set m_lpUsrFld
  170. */
  171. if (strMethod.compare("MKCALENDAR") == 0 && (m_ulUrlFlag & SERVICE_CALDAV))
  172. {
  173. // created in the IPM_SUBTREE
  174. hr = OpenSubFolder(m_lpActiveStore, NULL, '/', bIsPublic,
  175. false, &m_lpUsrFld);
  176. if(hr != hrSuccess)
  177. {
  178. ec_log_err("Error opening IPM_SUBTREE folder of user %ls, error code: 0x%08X", m_wstrUser.c_str(), hr);
  179. return hr;
  180. }
  181. }
  182. else if(!m_wstrFldName.empty())
  183. {
  184. // @note, caldav allows creation of calendars for non-existing urls, but since this can also use IDs, I am not sure we want to.
  185. hr = HrFindFolder(m_lpActiveStore, m_lpIPMSubtree, m_lpNamedProps, m_wstrFldName, &m_lpUsrFld);
  186. if(hr != hrSuccess)
  187. {
  188. ec_log_err("Error opening named folder of user %ls, folder %ls, error code: 0x%08X", m_wstrUser.c_str(), m_wstrFldName.c_str(), hr);
  189. return hr;
  190. }
  191. m_ulFolderFlag |= SINGLE_FOLDER;
  192. // check if this is the default calendar folder to enable freebusy publishing
  193. if (lpDefaultProp &&
  194. HrGetOneProp(m_lpUsrFld, PR_ENTRYID, &~lpEntryID) == hrSuccess &&
  195. m_lpActiveStore->CompareEntryIDs(lpEntryID->Value.bin.cb, (LPENTRYID)lpEntryID->Value.bin.lpb,
  196. lpDefaultProp->Value.bin.cb, (LPENTRYID)lpDefaultProp->Value.bin.lpb, 0, &ulRes) == hrSuccess &&
  197. ulRes == TRUE)
  198. {
  199. // disable delete default folder, and enable fb publish
  200. m_blFolderAccess = false;
  201. m_ulFolderFlag |= DEFAULT_FOLDER;
  202. }
  203. }
  204. // default calendar
  205. else if (bIsPublic) {
  206. hr = m_lpIPMSubtree->QueryInterface(IID_IMAPIFolder, (void**)&m_lpUsrFld);
  207. if (hr != hrSuccess)
  208. return hr;
  209. } else {
  210. // open default calendar
  211. hr = m_lpActiveStore->OpenEntry(lpDefaultProp->Value.bin.cb, (LPENTRYID)lpDefaultProp->Value.bin.lpb, NULL, MAPI_BEST_ACCESS, &ulType, (LPUNKNOWN*)&m_lpUsrFld);
  212. if (hr != hrSuccess)
  213. {
  214. ec_log_err("Unable to open default calendar for user %ls, error code: 0x%08X", m_wstrUser.c_str(), hr);
  215. return hr;
  216. }
  217. // we already know we don't want to delete this folder
  218. m_blFolderAccess = false;
  219. m_ulFolderFlag |= DEFAULT_FOLDER;
  220. }
  221. /*
  222. * Workaround for old users with sunbird / lightning on old url base.
  223. */
  224. {
  225. vector<string> parts;
  226. parts = tokenize(strUrl, '/', true);
  227. m_lpRequest->HrGetHeaderValue("User-Agent", &strAgent);
  228. // /caldav/
  229. // /caldav/username/ (which we return in XML data! (and shouldn't)), since this isn't a calendar, but /caldav/username/Calendar/ is.
  230. if ((strAgent.find("Sunbird/1") != string::npos || strAgent.find("Lightning/1") != string::npos) && parts.size() <= 2) {
  231. // Mozilla Sunbird / Lightning doesn't handle listing of calendars, only contents.
  232. // We therefore redirect them to the default calendar url.
  233. SPropValuePtr ptrDisplayName;
  234. string strLocation = "/caldav/" + urlEncode(m_wstrFldOwner, "utf-8");
  235. if (HrGetOneProp(m_lpUsrFld, PR_DISPLAY_NAME_W, &~ptrDisplayName) == hrSuccess) {
  236. std::string part = urlEncode(ptrDisplayName->Value.lpszW, "UTF-8");
  237. strLocation += "/" + part + "/";
  238. } else {
  239. // return 404 ?
  240. strLocation += "/Calendar/";
  241. }
  242. m_lpRequest->HrResponseHeader(301, "Moved Permanently");
  243. m_lpRequest->HrResponseHeader("Location", m_converter.convert_to<string>(strLocation));
  244. return MAPI_E_NOT_ME;
  245. }
  246. }
  247. /*
  248. * Check delete / rename access on folder, if not already blocked.
  249. */
  250. if (m_blFolderAccess &&
  251. // lpDefaultProp already should contain PR_IPM_APPOINTMENT_ENTRYID
  252. lpDefaultProp != nullptr) {
  253. ULONG ulCmp;
  254. hr = HrGetOneProp(m_lpUsrFld, PR_ENTRYID, &~lpFldProp);
  255. if (hr != hrSuccess)
  256. return hr;
  257. hr = m_lpSession->CompareEntryIDs(lpDefaultProp->Value.bin.cb, (LPENTRYID)lpDefaultProp->Value.bin.lpb,
  258. lpFldProp->Value.bin.cb, (LPENTRYID)lpFldProp->Value.bin.lpb, 0, &ulCmp);
  259. if (hr != hrSuccess || ulCmp == TRUE)
  260. m_blFolderAccess = false;
  261. }
  262. if (m_blFolderAccess) {
  263. hr = HrGetOneProp(m_lpUsrFld, PR_SUBFOLDERS, &~lpFldProp);
  264. if(hr != hrSuccess)
  265. return hr;
  266. if(lpFldProp->Value.b == (unsigned short)true && !strMethod.compare("DELETE"))
  267. m_blFolderAccess = false;
  268. }
  269. return hr;
  270. }
  271. /**
  272. * converts widechar to utf-8 string
  273. * @param[in] strWinChar source string(windows-1252) to be converted
  274. * @return string converted string (utf-8)
  275. */
  276. std::string ProtocolBase::W2U(const std::wstring &strWideChar)
  277. {
  278. return m_converter.convert_to<string>(m_strCharset.c_str(), strWideChar, rawsize(strWideChar), CHARSET_WCHAR);
  279. }
  280. /**
  281. * converts widechar to utf-8 string
  282. * @param[in] strWinChar source string(windows-1252) to be converted
  283. * @return string converted string (utf-8)
  284. */
  285. std::string ProtocolBase::W2U(const WCHAR* lpwWideChar)
  286. {
  287. return m_converter.convert_to<string>(m_strCharset.c_str(), lpwWideChar, rawsize(lpwWideChar), CHARSET_WCHAR);
  288. }
  289. /**
  290. * converts utf-8 string to widechar
  291. * @param[in] strUtfChar source string(utf-8) to be converted
  292. * @return wstring converted wide string
  293. */
  294. std::wstring ProtocolBase::U2W(const std::string &strUtfChar)
  295. {
  296. return m_converter.convert_to<wstring>(strUtfChar, rawsize(strUtfChar), m_strCharset.c_str());
  297. }
  298. /**
  299. * Convert SPropValue to string
  300. *
  301. * @param[in] lpSprop SPropValue to be converted
  302. * @return string
  303. */
  304. std::string ProtocolBase::SPropValToString(const SPropValue *lpSprop)
  305. {
  306. time_t tmUnixTime;
  307. std::string strRetVal;
  308. if (lpSprop == NULL)
  309. return std::string();
  310. if (PROP_TYPE(lpSprop->ulPropTag) == PT_SYSTIME)
  311. {
  312. FileTimeToUnixTime(lpSprop->Value.ft, &tmUnixTime);
  313. strRetVal = stringify_int64(tmUnixTime, false);
  314. }
  315. else if (PROP_TYPE(lpSprop->ulPropTag) == PT_STRING8)
  316. {
  317. strRetVal = lpSprop->Value.lpszA;
  318. }
  319. else if (PROP_TYPE(lpSprop->ulPropTag) == PT_UNICODE)
  320. {
  321. strRetVal = W2U(lpSprop->Value.lpszW);
  322. }
  323. //Global UID
  324. else if (PROP_TYPE(lpSprop->ulPropTag) == PT_BINARY)
  325. {
  326. HrGetICalUidFromBinUid(lpSprop->Value.bin, &strRetVal);
  327. }
  328. else if (PROP_TYPE(lpSprop->ulPropTag) == PT_LONG)
  329. {
  330. strRetVal = stringify(lpSprop->Value.ul, false);
  331. }
  332. return strRetVal;
  333. }