ECTableManager.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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 <kopano/memory.hpp>
  20. #include <mapidefs.h>
  21. #include <mapitags.h>
  22. #include <kopano/lockhelper.hpp>
  23. #include "ECMAPI.h"
  24. #include "ECGenericObjectTable.h"
  25. #include "ECSearchObjectTable.h"
  26. #include "ECConvenientDepthObjectTable.h"
  27. #include "ECStoreObjectTable.h"
  28. #include "ECMultiStoreTable.h"
  29. #include "ECUserStoreTable.h"
  30. #include "ECABObjectTable.h"
  31. #include "ECStatsTables.h"
  32. #include "ECConvenientDepthABObjectTable.h"
  33. #include "ECTableManager.h"
  34. #include "ECSessionManager.h"
  35. #include "ECSession.h"
  36. #include "ECDatabaseUtils.h"
  37. #include "ECSecurity.h"
  38. #include "ECSubRestriction.h"
  39. #include <kopano/ECDefs.h>
  40. #include "SOAPUtils.h"
  41. #include <kopano/stringutil.h>
  42. #include <kopano/Trace.h>
  43. #include "ECServerEntrypoint.h"
  44. #include "ECMailBoxTable.h"
  45. #include <kopano/mapiext.h>
  46. #include <edkmdb.h>
  47. using namespace KCHL;
  48. namespace KC {
  49. void FreeRowSet(struct rowSet *lpRowSet, bool bBasePointerDel);
  50. static const unsigned int sContentsProps[] = {
  51. PR_ENTRYID, PR_DISPLAY_NAME, PR_MESSAGE_FLAGS, PR_SUBJECT,
  52. PR_STORE_ENTRYID, PR_STORE_RECORD_KEY, PR_STORE_SUPPORT_MASK,
  53. PR_INSTANCE_KEY, PR_RECORD_KEY, PR_ACCESS, PR_ACCESS_LEVEL,
  54. };
  55. static const unsigned int sHierarchyProps[] = {
  56. PR_ENTRYID, PR_DISPLAY_NAME, PR_CONTENT_COUNT, PR_CONTENT_UNREAD,
  57. PR_STORE_ENTRYID, PR_STORE_RECORD_KEY, PR_STORE_SUPPORT_MASK,
  58. PR_INSTANCE_KEY, PR_RECORD_KEY, PR_ACCESS, PR_ACCESS_LEVEL,
  59. };
  60. static const unsigned int sABContentsProps[] = {
  61. PR_ENTRYID, PR_DISPLAY_NAME, PR_INSTANCE_KEY, PR_ADDRTYPE,
  62. PR_DISPLAY_TYPE, PR_DISPLAY_TYPE_EX, PR_EMAIL_ADDRESS, PR_SMTP_ADDRESS,
  63. PR_OBJECT_TYPE, PR_RECORD_KEY, PR_SEARCH_KEY, PR_DEPARTMENT_NAME,
  64. PR_OFFICE_TELEPHONE_NUMBER, PR_OFFICE_LOCATION, PR_PRIMARY_FAX_NUMBER,
  65. };
  66. static const unsigned int sABHierarchyProps[] = {
  67. PR_ENTRYID, PR_DISPLAY_NAME, PR_INSTANCE_KEY, PR_DISPLAY_TYPE,
  68. PR_OBJECT_TYPE, PR_RECORD_KEY, PR_SEARCH_KEY,
  69. };
  70. static const unsigned int sUserStoresProps[] = {
  71. PR_EC_USERNAME, PR_EC_STOREGUID, PR_EC_STORETYPE, PR_DISPLAY_NAME,
  72. PR_EC_COMPANYID, PR_EC_COMPANY_NAME, PR_STORE_ENTRYID,
  73. PR_LAST_MODIFICATION_TIME, PR_MESSAGE_SIZE_EXTENDED,
  74. };
  75. // stats tables
  76. static const unsigned int sSystemStatsProps[] = {
  77. PR_DISPLAY_NAME, PR_EC_STATS_SYSTEM_DESCRIPTION,
  78. PR_EC_STATS_SYSTEM_VALUE,
  79. };
  80. static const unsigned int sSessionStatsProps[] = {
  81. PR_EC_STATS_SESSION_ID, PR_EC_STATS_SESSION_GROUP_ID,
  82. PR_EC_STATS_SESSION_IPADDRESS, PR_EC_STATS_SESSION_IDLETIME,
  83. PR_EC_STATS_SESSION_CAPABILITY, PR_EC_STATS_SESSION_LOCKED,
  84. PR_EC_USERNAME, PR_EC_STATS_SESSION_BUSYSTATES,
  85. PR_EC_STATS_SESSION_PROCSTATES, PR_EC_STATS_SESSION_CPU_USER,
  86. PR_EC_STATS_SESSION_CPU_SYSTEM, PR_EC_STATS_SESSION_CPU_REAL,
  87. PR_EC_STATS_SESSION_PEER_PID, PR_EC_STATS_SESSION_CLIENT_VERSION,
  88. PR_EC_STATS_SESSION_CLIENT_APPLICATION, PR_EC_STATS_SESSION_REQUESTS,
  89. PR_EC_STATS_SESSION_PORT, PR_EC_STATS_SESSION_PROXY,
  90. PR_EC_STATS_SESSION_URL, PR_EC_STATS_SESSION_CLIENT_APPLICATION_VERSION,
  91. PR_EC_STATS_SESSION_CLIENT_APPLICATION_MISC,
  92. };
  93. static const unsigned int sUserStatsProps[] = {
  94. PR_EC_COMPANY_NAME, PR_EC_USERNAME, PR_DISPLAY_NAME, PR_SMTP_ADDRESS,
  95. PR_EC_NONACTIVE, PR_EC_ADMINISTRATOR, PR_EC_HOMESERVER_NAME,
  96. PR_MESSAGE_SIZE_EXTENDED, PR_QUOTA_WARNING_THRESHOLD,
  97. PR_QUOTA_SEND_THRESHOLD, PR_QUOTA_RECEIVE_THRESHOLD,
  98. PR_EC_QUOTA_MAIL_TIME, PR_EC_OUTOFOFFICE, PR_LAST_LOGON_TIME,
  99. PR_LAST_LOGOFF_TIME,
  100. };
  101. static const unsigned int sCompanyStatsProps[] = {
  102. PR_EC_COMPANY_NAME, PR_EC_COMPANY_ADMIN, PR_MESSAGE_SIZE_EXTENDED,
  103. PR_QUOTA_WARNING_THRESHOLD, PR_QUOTA_SEND_THRESHOLD,
  104. PR_QUOTA_RECEIVE_THRESHOLD, PR_EC_QUOTA_MAIL_TIME,
  105. };
  106. static const unsigned int sServerStatsProps[] = {
  107. PR_EC_STATS_SERVER_NAME, PR_EC_STATS_SERVER_HOST,
  108. PR_EC_STATS_SERVER_HTTPPORT, PR_EC_STATS_SERVER_SSLPORT,
  109. PR_EC_STATS_SERVER_PROXYURL, PR_EC_STATS_SERVER_HTTPURL,
  110. PR_EC_STATS_SERVER_HTTPSURL, PR_EC_STATS_SERVER_FILEURL,
  111. };
  112. static const struct propTagArray sPropTagArrayContents =
  113. {const_cast<unsigned int *>(sContentsProps), ARRAY_SIZE(sContentsProps)};
  114. static const struct propTagArray sPropTagArrayHierarchy =
  115. {const_cast<unsigned int *>(sHierarchyProps), ARRAY_SIZE(sHierarchyProps)};
  116. static const struct propTagArray sPropTagArrayABContents =
  117. {const_cast<unsigned int *>(sABContentsProps), ARRAY_SIZE(sABContentsProps)};
  118. static const struct propTagArray sPropTagArrayABHierarchy =
  119. {const_cast<unsigned int *>(sABHierarchyProps), ARRAY_SIZE(sABHierarchyProps)};
  120. static const struct propTagArray sPropTagArrayUserStores =
  121. {const_cast<unsigned int *>(sUserStoresProps), ARRAY_SIZE(sUserStoresProps)};
  122. static const struct propTagArray sPropTagArraySystemStats =
  123. {const_cast<unsigned int *>(sSystemStatsProps), ARRAY_SIZE(sSystemStatsProps)};
  124. static const struct propTagArray sPropTagArraySessionStats =
  125. {const_cast<unsigned int *>(sSessionStatsProps), ARRAY_SIZE(sSessionStatsProps)};
  126. static const struct propTagArray sPropTagArrayUserStats =
  127. {const_cast<unsigned int *>(sUserStatsProps), ARRAY_SIZE(sUserStatsProps)};
  128. static const struct propTagArray sPropTagArrayCompanyStats =
  129. {const_cast<unsigned int *>(sCompanyStatsProps), ARRAY_SIZE(sCompanyStatsProps)};
  130. static const struct propTagArray sPropTagArrayServerStats =
  131. {const_cast<unsigned int *>(sServerStatsProps), ARRAY_SIZE(sServerStatsProps)};
  132. ECTableManager::ECTableManager(ECSession *lpSession)
  133. {
  134. this->lpSession = lpSession;
  135. }
  136. ECTableManager::~ECTableManager()
  137. {
  138. scoped_rlock lock(hListMutex);
  139. auto iterTables = mapTable.cbegin();
  140. // Clean up tables, if CloseTable(..) isn't called
  141. while (iterTables != mapTable.cend()) {
  142. auto iterNext = iterTables;
  143. ++iterNext;
  144. CloseTable(iterTables->first);
  145. iterTables = iterNext;
  146. }
  147. mapTable.clear();
  148. }
  149. void ECTableManager::AddTableEntry(TABLE_ENTRY *lpEntry, unsigned int *lpulTableId)
  150. {
  151. scoped_rlock lock(hListMutex);
  152. mapTable[ulNextTableId] = lpEntry;
  153. lpEntry->lpTable->AddRef();
  154. *lpulTableId = ulNextTableId;
  155. lpEntry->lpTable->SetTableId(*lpulTableId);
  156. // Subscribe to events for this table, if needed
  157. switch(lpEntry->ulTableType) {
  158. case TABLE_ENTRY::TABLE_TYPE_GENERIC:
  159. lpSession->GetSessionManager()->SubscribeTableEvents(lpEntry->ulTableType,
  160. lpEntry->sTable.sGeneric.ulParentId, lpEntry->sTable.sGeneric.ulObjectType,
  161. lpEntry->sTable.sGeneric.ulObjectFlags, lpSession->GetSessionId());
  162. break;
  163. case TABLE_ENTRY::TABLE_TYPE_OUTGOINGQUEUE:
  164. lpSession->GetSessionManager()->SubscribeTableEvents(lpEntry->ulTableType,
  165. lpEntry->sTable.sOutgoingQueue.ulFlags & EC_SUBMIT_MASTER ? 0 : lpEntry->sTable.sOutgoingQueue.ulStoreId,
  166. MAPI_MESSAGE, lpEntry->sTable.sOutgoingQueue.ulFlags, lpSession->GetSessionId());
  167. break;
  168. default:
  169. // Other table types don't need updates from other sessions
  170. break;
  171. }
  172. ++ulNextTableId;
  173. }
  174. ECRESULT ECTableManager::OpenOutgoingQueueTable(unsigned int ulStoreId, unsigned int *lpulTableId)
  175. {
  176. ECRESULT er = erSuccess;
  177. object_ptr<ECStoreObjectTable> lpTable;
  178. std::unique_ptr<TABLE_ENTRY> lpEntry;
  179. DB_RESULT lpDBResult;
  180. DB_ROW lpDBRow = NULL;
  181. std::string strQuery;
  182. struct propTagArray *lpsPropTags = NULL;
  183. GUID sGuid;
  184. sObjectTableKey sRowItem;
  185. const ECLocale locale = lpSession->GetSessionManager()->GetSortLocale(ulStoreId);
  186. ECDatabase *lpDatabase = NULL;
  187. er = lpSession->GetDatabase(&lpDatabase);
  188. if (er != erSuccess)
  189. goto exit;
  190. if(ulStoreId) {
  191. er = lpSession->GetSessionManager()->GetCacheManager()->GetStore(ulStoreId, &ulStoreId, &sGuid);
  192. if(er != erSuccess)
  193. goto exit;
  194. er = ECStoreObjectTable::Create(lpSession, ulStoreId, &sGuid, 0, MAPI_MESSAGE, 0, 0, locale, &~lpTable);
  195. } else {
  196. // FIXME check permissions for master outgoing table
  197. // Master outgoing table has different STORE_ENTRYID and GUID per row
  198. er = ECStoreObjectTable::Create(lpSession, 0, NULL, 0, MAPI_MESSAGE, 0, 0, locale, &~lpTable);
  199. }
  200. if(er != erSuccess)
  201. goto exit;
  202. // Select outgoingqueue
  203. strQuery = "SELECT o.hierarchy_id, i.hierarchyid FROM outgoingqueue AS o LEFT JOIN indexedproperties AS i ON i.hierarchyid=o.hierarchy_id and i.tag=0xFFF";
  204. if(ulStoreId)
  205. strQuery += " WHERE o.flags & 1 =" + stringify(EC_SUBMIT_LOCAL) + " AND o.store_id=" + stringify(ulStoreId);
  206. else
  207. strQuery += " WHERE o.flags & 1 =" + stringify(EC_SUBMIT_MASTER);
  208. er = lpDatabase->DoSelect(strQuery, &lpDBResult);
  209. if(er != erSuccess)
  210. goto exit;
  211. while( (lpDBRow = lpDatabase->FetchRow(lpDBResult)) != NULL) {
  212. if(lpDBRow[0] == NULL)
  213. continue;
  214. // Item found without entryid, item already deleted, so delete the item from the queue
  215. if (lpDBRow[1] == NULL) {
  216. ec_log_err("Removing stray object \"%s\" from outgoing table", lpDBRow[0]);
  217. strQuery = "DELETE FROM outgoingqueue WHERE hierarchy_id=" + string(lpDBRow[0]);
  218. lpDatabase->DoDelete(strQuery); //ignore errors
  219. continue;
  220. }
  221. lpTable->UpdateRow(ECKeyTable::TABLE_ROW_ADD, atoi(lpDBRow[0]), 0);
  222. }
  223. lpEntry.reset(new TABLE_ENTRY);
  224. // Add the open table to the list of current tables
  225. lpEntry->lpTable = lpTable;
  226. lpEntry->ulTableType = TABLE_ENTRY::TABLE_TYPE_OUTGOINGQUEUE;
  227. lpEntry->sTable.sOutgoingQueue.ulStoreId = ulStoreId;
  228. lpEntry->sTable.sOutgoingQueue.ulFlags = ulStoreId ? EC_SUBMIT_LOCAL : EC_SUBMIT_MASTER;
  229. AddTableEntry(lpEntry.release(), lpulTableId);
  230. if (lpTable->GetColumns(NULL, TBL_ALL_COLUMNS, &lpsPropTags) == erSuccess)
  231. lpTable->SetColumns(lpsPropTags, false);
  232. lpTable->SeekRow(BOOKMARK_BEGINNING, 0, NULL);
  233. exit:
  234. if(lpsPropTags)
  235. FreePropTagArray(lpsPropTags);
  236. return er;
  237. }
  238. ECRESULT ECTableManager::OpenUserStoresTable(unsigned int ulFlags, unsigned int *lpulTableId)
  239. {
  240. ECRESULT er = erSuccess;
  241. object_ptr<ECUserStoreTable> lpTable;
  242. const char *lpszLocaleId = lpSession->GetSessionManager()->GetConfig()->GetSetting("default_sort_locale_id");
  243. er = ECUserStoreTable::Create(lpSession, ulFlags, createLocaleFromName(lpszLocaleId), &~lpTable);
  244. if (er != erSuccess)
  245. return er;
  246. std::unique_ptr<TABLE_ENTRY> lpEntry(new(std::nothrow) TABLE_ENTRY);
  247. if (lpEntry == nullptr)
  248. return KCERR_NOT_ENOUGH_MEMORY;
  249. // Add the open table to the list of current tables
  250. lpEntry->lpTable = lpTable;
  251. lpEntry->ulTableType = TABLE_ENTRY::TABLE_TYPE_USERSTORES;
  252. memset(&lpEntry->sTable, 0, sizeof(TABLE_ENTRY::__sTable));
  253. er = lpTable->SetColumns(&sPropTagArrayUserStores, true);
  254. if (er != erSuccess)
  255. return er;
  256. AddTableEntry(lpEntry.release(), lpulTableId);
  257. return erSuccess;
  258. }
  259. ECRESULT ECTableManager::OpenMultiStoreTable(unsigned int ulObjType, unsigned int ulFlags, unsigned int *lpulTableId)
  260. {
  261. ECRESULT er = erSuccess;
  262. object_ptr<ECMultiStoreTable> lpTable;
  263. const char *lpszLocaleId = lpSession->GetSessionManager()->GetConfig()->GetSetting("default_sort_locale_id");
  264. // Open an empty table. Contents will be provided by client in a later call.
  265. er = ECMultiStoreTable::Create(lpSession, ulObjType, ulFlags, createLocaleFromName(lpszLocaleId), &~lpTable);
  266. if (er != erSuccess)
  267. return er;
  268. std::unique_ptr<TABLE_ENTRY> lpEntry(new(std::nothrow) TABLE_ENTRY);
  269. if (lpEntry == nullptr)
  270. return KCERR_NOT_ENOUGH_MEMORY;
  271. // Add the open table to the list of current tables
  272. lpEntry->lpTable = lpTable;
  273. lpEntry->ulTableType = TABLE_ENTRY::TABLE_TYPE_MULTISTORE;
  274. memset(&lpEntry->sTable, 0, sizeof(TABLE_ENTRY::__sTable));
  275. AddTableEntry(lpEntry.release(), lpulTableId);
  276. return er;
  277. }
  278. ECRESULT ECTableManager::OpenGenericTable(unsigned int ulParent, unsigned int ulObjType, unsigned int ulFlags, unsigned int *lpulTableId, bool fLoad)
  279. {
  280. ECRESULT er = erSuccess;
  281. std::string strQuery;
  282. object_ptr<ECStoreObjectTable> lpTable;
  283. std::unique_ptr<TABLE_ENTRY> lpEntry;
  284. unsigned int ulStoreId = 0;
  285. GUID sGuid;
  286. ECLocale locale;
  287. ECDatabase *lpDatabase = NULL;
  288. er = lpSession->GetDatabase(&lpDatabase);
  289. if (er != erSuccess)
  290. return er;
  291. er = lpSession->GetSessionManager()->GetCacheManager()->GetStore(ulParent, &ulStoreId, &sGuid);
  292. if(er != erSuccess)
  293. return er;
  294. locale = lpSession->GetSessionManager()->GetSortLocale(ulStoreId);
  295. if(lpSession->GetSessionManager()->GetSearchFolders()->IsSearchFolder(ulStoreId, ulParent) == erSuccess) {
  296. if (ulFlags & (MSGFLAG_DELETED | MAPI_ASSOCIATED))
  297. return KCERR_NO_SUPPORT;
  298. er = lpSession->GetSecurity()->CheckPermission(ulParent, ecSecurityFolderVisible);
  299. if(er != erSuccess)
  300. return er;
  301. else
  302. er = ECSearchObjectTable::Create(lpSession, ulStoreId, &sGuid, ulParent, ulObjType, ulFlags, locale, &~lpTable);
  303. } else if(ulObjType == MAPI_FOLDER && (ulFlags & CONVENIENT_DEPTH))
  304. er = ECConvenientDepthObjectTable::Create(lpSession, ulStoreId, &sGuid, ulParent, ulObjType, ulFlags, locale, &~lpTable);
  305. else
  306. er = ECStoreObjectTable::Create(lpSession, ulStoreId, &sGuid, ulParent, ulObjType, ulFlags, 0, locale, &~lpTable);
  307. if (er != erSuccess)
  308. return er;
  309. lpEntry.reset(new(std::nothrow) TABLE_ENTRY);
  310. if (lpEntry == nullptr)
  311. return KCERR_NOT_ENOUGH_MEMORY;
  312. // Add the open table to the list of current tables
  313. lpEntry->lpTable = lpTable;
  314. lpEntry->ulTableType = TABLE_ENTRY::TABLE_TYPE_GENERIC;
  315. lpEntry->sTable.sGeneric.ulObjectFlags = ulFlags & (MAPI_ASSOCIATED | MSGFLAG_DELETED); // MSGFLAG_DELETED because of conversion in ns__tableOpen
  316. lpEntry->sTable.sGeneric.ulObjectType = ulObjType;
  317. lpEntry->sTable.sGeneric.ulParentId = ulParent;
  318. // First, add table to internal list of tables. This means we can already start
  319. // receiving notifications on this table
  320. AddTableEntry(lpEntry.release(), lpulTableId);
  321. // Load a default column set
  322. if (ulObjType == MAPI_MESSAGE)
  323. lpTable->SetColumns(&sPropTagArrayContents, true);
  324. else
  325. lpTable->SetColumns(&sPropTagArrayHierarchy, true);
  326. return erSuccess;
  327. }
  328. static void AuditStatsAccess(ECSession *lpSession, const char *access, const char *table)
  329. {
  330. if (!lpSession->GetSessionManager()->GetAudit())
  331. return;
  332. std::string strUsername;
  333. std::string strImpersonator;
  334. lpSession->GetSecurity()->GetUsername(&strUsername);
  335. if (lpSession->GetSecurity()->GetImpersonator(&strImpersonator) == erSuccess)
  336. ZLOG_AUDIT(lpSession->GetSessionManager()->GetAudit(), "access %s table='%s stats' username=%s impersonator=%s", access, table, strUsername.c_str(), strImpersonator.c_str());
  337. else
  338. ZLOG_AUDIT(lpSession->GetSessionManager()->GetAudit(), "access %s table='%s stats' username=%s", access, table, strUsername.c_str());
  339. }
  340. ECRESULT ECTableManager::OpenStatsTable(unsigned int ulTableType, unsigned int ulFlags, unsigned int *lpulTableId)
  341. {
  342. ECRESULT er = erSuccess;
  343. object_ptr<ECGenericObjectTable> lpTable;
  344. std::unique_ptr<TABLE_ENTRY> lpEntry;
  345. int adminlevel = lpSession->GetSecurity()->GetAdminLevel();
  346. bool hosted = lpSession->GetSessionManager()->IsHostedSupported();
  347. const char *lpszLocaleId = lpSession->GetSessionManager()->GetConfig()->GetSetting("default_sort_locale_id");
  348. // TABLETYPE_STATS_SYSTEM: only for (sys)admins
  349. // TABLETYPE_STATS_SESSIONS: only for (sys)admins
  350. // TABLETYPE_STATS_USERS: full list: only for (sys)admins, company list: only for admins
  351. lpEntry.reset(new(std::nothrow) TABLE_ENTRY);
  352. if (lpEntry == nullptr)
  353. return KCERR_NOT_ENOUGH_MEMORY;
  354. switch (ulTableType) {
  355. case TABLETYPE_STATS_SYSTEM:
  356. if ((hosted && adminlevel < ADMIN_LEVEL_SYSADMIN) || (!hosted && adminlevel < ADMIN_LEVEL_ADMIN)) {
  357. AuditStatsAccess(lpSession, "denied", "system");
  358. return KCERR_NO_ACCESS;
  359. }
  360. er = ECSystemStatsTable::Create(lpSession, ulFlags, createLocaleFromName(lpszLocaleId), &~lpTable);
  361. if (er != erSuccess)
  362. return er;
  363. lpEntry->ulTableType = TABLE_ENTRY::TABLE_TYPE_SYSTEMSTATS;
  364. er = lpTable->SetColumns(&sPropTagArraySystemStats, true);
  365. break;
  366. case TABLETYPE_STATS_SESSIONS:
  367. if ((hosted && adminlevel < ADMIN_LEVEL_SYSADMIN) || (!hosted && adminlevel < ADMIN_LEVEL_ADMIN)) {
  368. AuditStatsAccess(lpSession, "denied", "session");
  369. return KCERR_NO_ACCESS;
  370. }
  371. er = ECSessionStatsTable::Create(lpSession, ulFlags, createLocaleFromName(lpszLocaleId), &~lpTable);
  372. if (er != erSuccess)
  373. return er;
  374. lpEntry->ulTableType = TABLE_ENTRY::TABLE_TYPE_SESSIONSTATS;
  375. er = lpTable->SetColumns(&sPropTagArraySessionStats, true);
  376. break;
  377. case TABLETYPE_STATS_USERS:
  378. if (adminlevel < ADMIN_LEVEL_ADMIN) {
  379. AuditStatsAccess(lpSession, "denied", "user");
  380. return KCERR_NO_ACCESS;
  381. }
  382. er = ECUserStatsTable::Create(lpSession, ulFlags, createLocaleFromName(lpszLocaleId), &~lpTable);
  383. if (er != erSuccess)
  384. return er;
  385. lpEntry->ulTableType = TABLE_ENTRY::TABLE_TYPE_USERSTATS;
  386. er = lpTable->SetColumns(&sPropTagArrayUserStats, true);
  387. break;
  388. case TABLETYPE_STATS_COMPANY:
  389. if (!hosted)
  390. return KCERR_NOT_FOUND;
  391. if (adminlevel < ADMIN_LEVEL_SYSADMIN) {
  392. AuditStatsAccess(lpSession, "denied", "company");
  393. return KCERR_NO_ACCESS;
  394. }
  395. er = ECCompanyStatsTable::Create(lpSession, ulFlags, createLocaleFromName(lpszLocaleId), &~lpTable);
  396. if (er != erSuccess)
  397. return er;
  398. lpEntry->ulTableType = TABLE_ENTRY::TABLE_TYPE_COMPANYSTATS;
  399. er = lpTable->SetColumns(&sPropTagArrayCompanyStats, true);
  400. break;
  401. case TABLETYPE_STATS_SERVERS:
  402. if (adminlevel < ADMIN_LEVEL_SYSADMIN) {
  403. AuditStatsAccess(lpSession, "denied", "company");
  404. return KCERR_NO_ACCESS;
  405. }
  406. er = ECServerStatsTable::Create(lpSession, ulFlags, createLocaleFromName(lpszLocaleId), &~lpTable);
  407. if (er != erSuccess)
  408. return er;
  409. lpEntry->ulTableType = TABLE_ENTRY::TABLE_TYPE_SERVERSTATS;
  410. er = lpTable->SetColumns(&sPropTagArrayServerStats, true);
  411. break;
  412. default:
  413. er = KCERR_UNKNOWN;
  414. break;
  415. }
  416. if (er != erSuccess)
  417. return er;
  418. // Add the open table to the list of current tables
  419. lpEntry->lpTable = lpTable;
  420. memset(&lpEntry->sTable, 0, sizeof(TABLE_ENTRY::__sTable));
  421. AddTableEntry(lpEntry.release(), lpulTableId);
  422. return erSuccess;
  423. }
  424. ECRESULT ECTableManager::OpenMailBoxTable(unsigned int ulflags, unsigned int *lpulTableId)
  425. {
  426. ECRESULT er = erSuccess;
  427. object_ptr<ECMailBoxTable> lpTable;
  428. const char *lpszLocaleId = lpSession->GetSessionManager()->GetConfig()->GetSetting("default_sort_locale_id");
  429. er = ECMailBoxTable::Create(lpSession, ulflags, createLocaleFromName(lpszLocaleId), &~lpTable);
  430. if (er != erSuccess)
  431. return er;
  432. std::unique_ptr<TABLE_ENTRY> lpEntry(new(std::nothrow) TABLE_ENTRY);
  433. if (lpEntry == nullptr)
  434. return KCERR_NOT_ENOUGH_MEMORY;
  435. // Add the open table to the list of current tables
  436. lpEntry->lpTable = lpTable;
  437. lpEntry->ulTableType = TABLE_ENTRY::TABLE_TYPE_MAILBOX;
  438. memset(&lpEntry->sTable, 0, sizeof(TABLE_ENTRY::__sTable));
  439. //@todo check this list!!!
  440. er = lpTable->SetColumns(&sPropTagArrayUserStores, true);
  441. if (er != erSuccess)
  442. return er;
  443. AddTableEntry(lpEntry.release(), lpulTableId);
  444. return erSuccess;
  445. }
  446. /*
  447. Open an addressbook table
  448. */
  449. ECRESULT ECTableManager::OpenABTable(unsigned int ulParent, unsigned int ulParentType, unsigned int ulObjType, unsigned int ulFlags, unsigned int *lpulTableId)
  450. {
  451. ECRESULT er = erSuccess;
  452. object_ptr<ECABObjectTable> lpTable;
  453. std::unique_ptr<TABLE_ENTRY> lpEntry;
  454. const char *lpszLocaleId = lpSession->GetSessionManager()->GetConfig()->GetSetting("default_sort_locale_id");
  455. // Open first container
  456. if (ulFlags & CONVENIENT_DEPTH)
  457. er = ECConvenientDepthABObjectTable::Create(lpSession, 1, ulObjType, ulParent, ulParentType, ulFlags, createLocaleFromName(lpszLocaleId), &~lpTable);
  458. else
  459. er = ECABObjectTable::Create(lpSession, 1, ulObjType, ulParent, ulParentType, ulFlags, createLocaleFromName(lpszLocaleId), &~lpTable);
  460. if (er != erSuccess)
  461. return er;
  462. lpEntry.reset(new TABLE_ENTRY);
  463. // Add the open table to the list of current tables
  464. lpEntry->lpTable = lpTable;
  465. lpEntry->ulTableType = TABLE_ENTRY::TABLE_TYPE_GENERIC;
  466. lpEntry->sTable.sGeneric.ulObjectFlags = ulFlags & (MAPI_ASSOCIATED | MSGFLAG_DELETED); // MSGFLAG_DELETED because of conversion in ns__tableOpen
  467. lpEntry->sTable.sGeneric.ulObjectType = ulObjType;
  468. lpEntry->sTable.sGeneric.ulParentId = ulParent;
  469. AddTableEntry(lpEntry.release(), lpulTableId);
  470. if (ulObjType == MAPI_ABCONT || ulObjType == MAPI_DISTLIST)
  471. lpTable->SetColumns(&sPropTagArrayABHierarchy, true);
  472. else
  473. lpTable->SetColumns(&sPropTagArrayABContents, true);
  474. return erSuccess;
  475. }
  476. ECRESULT ECTableManager::GetTable(unsigned int ulTableId, ECGenericObjectTable **lppTable)
  477. {
  478. scoped_rlock lock(hListMutex);
  479. auto iterTables = mapTable.find(ulTableId);
  480. if (iterTables == mapTable.cend())
  481. return KCERR_NOT_FOUND;
  482. ECGenericObjectTable *lpTable = iterTables->second->lpTable;
  483. if (lpTable == NULL)
  484. return KCERR_NOT_FOUND;
  485. lpTable->AddRef();
  486. *lppTable = lpTable;
  487. return erSuccess;
  488. }
  489. ECRESULT ECTableManager::CloseTable(unsigned int ulTableId)
  490. {
  491. ECRESULT er = erSuccess;
  492. TABLE_ENTRY *lpEntry = NULL;
  493. ulock_rec lk(hListMutex);
  494. auto iterTables = mapTable.find(ulTableId);
  495. if (iterTables != mapTable.cend()) {
  496. // Remember the table entry struct
  497. lpEntry = iterTables->second;
  498. // Unsubscribe if needed
  499. switch(lpEntry->ulTableType) {
  500. case TABLE_ENTRY::TABLE_TYPE_GENERIC:
  501. lpSession->GetSessionManager()->UnsubscribeTableEvents(lpEntry->ulTableType,
  502. lpEntry->sTable.sGeneric.ulParentId, lpEntry->sTable.sGeneric.ulObjectType,
  503. lpEntry->sTable.sGeneric.ulObjectFlags, lpSession->GetSessionId());
  504. break;
  505. case TABLE_ENTRY::TABLE_TYPE_OUTGOINGQUEUE:
  506. lpSession->GetSessionManager()->UnsubscribeTableEvents(lpEntry->ulTableType,
  507. lpEntry->sTable.sOutgoingQueue.ulFlags & EC_SUBMIT_MASTER ? 0 : lpEntry->sTable.sOutgoingQueue.ulStoreId,
  508. MAPI_MESSAGE, lpEntry->sTable.sOutgoingQueue.ulFlags, lpSession->GetSessionId());
  509. break;
  510. default:
  511. break;
  512. }
  513. // Now, remove the table from the open table list
  514. mapTable.erase(ulTableId);
  515. // Unlock the table now as the search thread may not be able to exit without a hListMutex lock
  516. lk.unlock();
  517. // Free table data and threads running
  518. lpEntry->lpTable->Release();
  519. delete lpEntry;
  520. } else {
  521. lk.unlock();
  522. }
  523. return er;
  524. }
  525. ECRESULT ECTableManager::UpdateOutgoingTables(ECKeyTable::UpdateType ulType, unsigned ulStoreId, std::list<unsigned int> &lstObjId, unsigned int ulFlags, unsigned int ulObjType)
  526. {
  527. ECRESULT er = erSuccess;
  528. sObjectTableKey sRow;
  529. scoped_rlock lock(hListMutex);
  530. for (const auto &t : mapTable) {
  531. bool k = t.second->ulTableType == TABLE_ENTRY::TABLE_TYPE_OUTGOINGQUEUE &&
  532. (t.second->sTable.sOutgoingQueue.ulStoreId == ulStoreId ||
  533. t.second->sTable.sOutgoingQueue.ulStoreId == 0) &&
  534. t.second->sTable.sOutgoingQueue.ulFlags == (ulFlags & EC_SUBMIT_MASTER);
  535. if (!k)
  536. continue;
  537. er = t.second->lpTable->UpdateRows(ulType, &lstObjId, OBJECTTABLE_NOTIFY, false);
  538. // ignore errors from the update
  539. er = erSuccess;
  540. }
  541. return er;
  542. }
  543. ECRESULT ECTableManager::UpdateTables(ECKeyTable::UpdateType ulType, unsigned int ulFlags, unsigned int ulObjId, std::list<unsigned int> &lstChildId, unsigned int ulObjType)
  544. {
  545. ECRESULT er = erSuccess;
  546. sObjectTableKey sRow;
  547. scoped_rlock lock(hListMutex);
  548. // This is called when a table has changed, so we have to see if the table in question is actually loaded by this table
  549. // manager, and then update the row if required.
  550. // First, do all the actual contents tables and hierarchy tables
  551. for (const auto &t : mapTable) {
  552. bool k = t.second->ulTableType == TABLE_ENTRY::TABLE_TYPE_GENERIC &&
  553. t.second->sTable.sGeneric.ulParentId == ulObjId &&
  554. t.second->sTable.sGeneric.ulObjectFlags == ulFlags &&
  555. t.second->sTable.sGeneric.ulObjectType == ulObjType;
  556. if (!k)
  557. continue;
  558. er = t.second->lpTable->UpdateRows(ulType, &lstChildId, OBJECTTABLE_NOTIFY, false);
  559. // ignore errors from the update
  560. er = erSuccess;
  561. }
  562. return er;
  563. }
  564. /**
  565. * Get table statistics
  566. *
  567. * @param[out] lpulTables The amount of open tables
  568. * @param[out] lpulObjectSize The total memory usages of the tables
  569. *
  570. */
  571. ECRESULT ECTableManager::GetStats(unsigned int *lpulTables, unsigned int *lpulObjectSize)
  572. {
  573. unsigned int ulSize = 0;
  574. unsigned int ulTables = 0;
  575. scoped_rlock lock(hListMutex);
  576. ulTables = mapTable.size();
  577. ulSize = MEMORY_USAGE_MAP(ulTables, TABLEENTRYMAP);
  578. for (const auto &e : mapTable)
  579. if (e.second->ulTableType != TABLE_ENTRY::TABLE_TYPE_SYSTEMSTATS)
  580. /* Skip system stats since it would recursively include itself */
  581. ulSize += e.second->lpTable->GetObjectSize();
  582. *lpulTables = ulTables;
  583. *lpulObjectSize = ulSize;
  584. return erSuccess;
  585. }
  586. } /* namespace */