ECSessionManager.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. // ECSessionManager.h: interface for the ECSessionManager class.
  18. //
  19. //////////////////////////////////////////////////////////////////////
  20. #ifndef ECSESSIONMANAGER
  21. #define ECSESSIONMANAGER
  22. #include <kopano/zcdefs.h>
  23. #include "ECSession.h"
  24. #include <condition_variable>
  25. #include <list>
  26. #include <map>
  27. #include <mutex>
  28. #include <set>
  29. #include <pthread.h>
  30. #include <kopano/lockhelper.hpp>
  31. #include "ECUserManagement.h"
  32. #include "ECSearchFolders.h"
  33. #include "ECDatabaseFactory.h"
  34. #include "ECCacheManager.h"
  35. #include "ECPluginFactory.h"
  36. #include "ECServerEntrypoint.h"
  37. #include "ECSessionGroup.h"
  38. #include "ECNotificationManager.h"
  39. #include "ECLockManager.h"
  40. struct soap;
  41. namespace KC {
  42. class ECLogger;
  43. class ECTPropsPurge;
  44. using namespace std;
  45. typedef hash_map<ECSESSIONGROUPID, ECSessionGroup*>::Type EC_SESSIONGROUPMAP;
  46. typedef hash_map<ECSESSIONID, BTSession*>::Type SESSIONMAP;
  47. typedef hash_map<ECSESSIONID, unsigned int>::Type PERSISTENTBYSESSION;
  48. typedef hash_map<unsigned int, ECSESSIONID>::Type PERSISTENTBYCONNECTION;
  49. typedef std::multimap<unsigned int, ECSESSIONGROUPID> OBJECTSUBSCRIPTIONSMULTIMAP;
  50. struct TABLESUBSCRIPTION {
  51. TABLE_ENTRY::TABLE_TYPE ulType;
  52. unsigned int ulRootObjectId;
  53. unsigned int ulObjectType;
  54. unsigned int ulObjectFlags;
  55. bool operator==(const TABLESUBSCRIPTION &b) const { return memcmp(this, &b, sizeof(*this)) == 0; }
  56. bool operator<(const TABLESUBSCRIPTION &b) const { return memcmp(this, &b, sizeof(*this)) < 0; }
  57. };
  58. typedef std::multimap<TABLESUBSCRIPTION, ECSESSIONID> TABLESUBSCRIPTIONMULTIMAP;
  59. struct sSessionManagerStats {
  60. struct {
  61. ULONG ulItems;
  62. ULONG ulLocked;
  63. ULONG ulOpenTables;
  64. ULONGLONG ullSize;
  65. ULONGLONG ulTableSize;
  66. }session;
  67. struct {
  68. ULONG ulItems;
  69. ULONGLONG ullSize;
  70. } group;
  71. ULONG ulPersistentByConnection;
  72. ULONG ulPersistentByConnectionSize;
  73. ULONG ulPersistentBySession;
  74. ULONG ulPersistentBySessionSize;
  75. ULONG ulTableSubscriptions;
  76. ULONG ulObjectSubscriptions;
  77. ULONG ulTableSubscriptionSize;
  78. ULONG ulObjectSubscriptionSize;
  79. };
  80. class SOURCEKEY;
  81. class _kc_export ECSessionManager _kc_final {
  82. public:
  83. _kc_hidden ECSessionManager(ECConfig *, ECLogger *audit, bool hosted, bool distributed);
  84. _kc_hidden virtual ~ECSessionManager(void);
  85. _kc_hidden virtual ECRESULT CreateAuthSession(struct soap *, unsigned int caps, ECSESSIONID *, ECAuthSession **, bool register_ses, bool lock_ses);
  86. // Creates a session based on passed credentials
  87. _kc_hidden virtual ECRESULT CreateSession(struct soap *, const char *name, const char *pass, const char *imp_user, const char *cl_vers, const char *cl_app, const char *cl_app_ver, const char *cl_app_misc, unsigned int caps, ECSESSIONGROUPID, ECSESSIONID *, ECSession **, bool lock_ses, bool allow_uid_auth);
  88. // Creates a session without credential checking (caller must check credentials)
  89. _kc_hidden virtual ECRESULT RegisterSession(ECAuthSession *, ECSESSIONGROUPID, const char *cl_ver, const char *cl_app, const char *cl_app_ver, const char *cl_app_misc, ECSESSIONID *, ECSession **, bool lock_ses);
  90. virtual ECRESULT CreateSessionInternal(ECSession **, unsigned int user_id = KOPANO_UID_SYSTEM);
  91. _kc_hidden virtual ECRESULT RemoveSession(ECSESSIONID);
  92. virtual void RemoveSessionInternal(ECSession *);
  93. // Persistent connections: sessions with persistent connections (named pipes) are guaranteed not to timeout
  94. // between calls to SetSessionPersistentConnection() and RemoveSessionPersistentConnection. The persistent connection ID
  95. // is implementation-specific, but must be unique for each session.
  96. _kc_hidden virtual ECRESULT SetSessionPersistentConnection(ECSESSIONID, unsigned int conn_id);
  97. _kc_hidden virtual ECRESULT RemoveSessionPersistentConnection(unsigned int conn_id);
  98. _kc_hidden virtual ECRESULT GetSessionGroup(ECSESSIONGROUPID, ECSession *, ECSessionGroup **);
  99. _kc_hidden virtual ECRESULT DeleteIfOrphaned(ECSessionGroup *);
  100. _kc_hidden ECRESULT RemoveAllSessions(void);
  101. _kc_hidden ECRESULT CancelAllSessions(ECSESSIONID except = 0);
  102. _kc_hidden ECRESULT ForEachSession(void (*cb)(ECSession *, void *), void *obj);
  103. _kc_hidden ECRESULT LoadSettings(void);
  104. _kc_hidden ECRESULT CheckUserLicense(void);
  105. _kc_hidden ECRESULT UpdateTables(ECKeyTable::UpdateType, unsigned int flags, unsigned int obj_id, unsigned int child_id, unsigned int obj_type);
  106. _kc_hidden ECRESULT UpdateTables(ECKeyTable::UpdateType, unsigned int flags, unsigned int obj_id, std::list<unsigned int> &objects, unsigned int obj_type);
  107. _kc_hidden ECRESULT UpdateOutgoingTables(ECKeyTable::UpdateType, unsigned int store_id, unsigned int obj_id, unsigned int flags, unsigned int obj_type);
  108. _kc_hidden ECRESULT NotificationModified(unsigned int obj_type, unsigned int obj_id, unsigned int parent_id = 0);
  109. _kc_hidden ECRESULT NotificationCreated(unsigned int obj_type, unsigned int obj_id, unsigned int parent_id);
  110. _kc_hidden ECRESULT NotificationMoved(unsigned int obj_type, unsigned int obj_id, unsigned int parent_id, unsigned int old_parent_id, entryId *old_eid = nullptr);
  111. _kc_hidden ECRESULT NotificationCopied(unsigned int obj_type, unsigned int obj_id, unsigned int parent_id, unsigned int old_obj_id, unsigned int old_parent_id);
  112. _kc_hidden ECRESULT NotificationDeleted(unsigned int obj_type, unsigned int obj_id, unsigned int store_id, entryId *eid, unsigned int folder_id, unsigned int flags);
  113. _kc_hidden ECRESULT NotificationSearchComplete(unsigned int obj_id, unsigned int store_id);
  114. _kc_hidden ECRESULT NotificationChange(const std::set<unsigned int> &sync_ids, unsigned int change_id, unsigned int change_type);
  115. _kc_hidden ECRESULT ValidateSession(struct soap *, ECSESSIONID, ECAuthSession **, bool lock_ses = false);
  116. _kc_hidden ECRESULT ValidateSession(struct soap *, ECSESSIONID, ECSession **, bool lock_ses = false);
  117. _kc_hidden ECRESULT AddSessionClocks(ECSESSIONID, double user, double system, double real);
  118. ECRESULT RemoveBusyState(ECSESSIONID ecSessionID, pthread_t thread);
  119. _kc_hidden static void *SessionCleaner(void *tmp_ses_mgr);
  120. _kc_hidden ECRESULT AddNotification(notification *item, unsigned int key, unsigned int store_id = 0, unsigned int folder_id = 0, unsigned int flags = 0);
  121. _kc_hidden ECRESULT DeferNotificationProcessing(ECSESSIONID, struct soap *);
  122. _kc_hidden ECRESULT NotifyNotificationReady(ECSESSIONID);
  123. _kc_hidden void GetStats(void (*cb)(const std::string &, const std::string &, const std::string &, void *), void *obj);
  124. _kc_hidden void GetStats(sSessionManagerStats &);
  125. _kc_hidden ECRESULT DumpStats(void);
  126. _kc_hidden bool IsHostedSupported(void) { return m_bHostedKopano; }
  127. _kc_hidden bool IsDistributedSupported(void) { return m_bDistributedKopano; }
  128. _kc_hidden ECRESULT GetLicensedUsers(unsigned int svc_type, unsigned int *licusers);
  129. _kc_hidden ECRESULT GetServerGUID(GUID *);
  130. _kc_hidden ECRESULT GetNewSourceKey(SOURCEKEY *);
  131. // Requests that table change events of a specific table are sent to a
  132. // session. Events are published to the 'UpdateTables()' function or
  133. // 'UpdateOutgoingTables()' function of the session.
  134. _kc_hidden ECRESULT SubscribeTableEvents(TABLE_ENTRY::TABLE_TYPE, unsigned int tbl_root_obj_id, unsigned int obj_type, unsigned int obj_flags, ECSESSIONID);
  135. _kc_hidden ECRESULT UnsubscribeTableEvents(TABLE_ENTRY::TABLE_TYPE, unsigned int tbl_root_obj_id, unsigned int obj_type, unsigned int obj_flags, ECSESSIONID);
  136. // Requests that object notifications for a certain store are dispatched to a sessiongroup. Events
  137. // are published to the 'AddNotification()' function for the session's sessiongroup.
  138. _kc_hidden ECRESULT SubscribeObjectEvents(unsigned int store_id, ECSESSIONGROUPID);
  139. _kc_hidden ECRESULT UnsubscribeObjectEvents(unsigned int store_id, ECSESSIONGROUPID);
  140. enum SEQUENCE { SEQ_IMAP };
  141. _kc_hidden ECRESULT GetNewSequence(SEQUENCE, unsigned long long *seq_id);
  142. _kc_hidden ECRESULT CreateDatabaseConnection(void);
  143. _kc_hidden ECRESULT GetStoreSortLCID(ULONG store_id, ULONG *id);
  144. _kc_hidden LPCSTR GetDefaultSortLocaleID(void);
  145. _kc_hidden ULONG GetSortLCID(ULONG store_id);
  146. _kc_hidden ECLocale GetSortLocale(ULONG store_id);
  147. _kc_hidden ECCacheManager *GetCacheManager(void) { return m_lpECCacheManager; }
  148. _kc_hidden ECSearchFolders *GetSearchFolders(void) { return m_lpSearchFolders; }
  149. _kc_hidden ECConfig *GetConfig(void) { return m_lpConfig; }
  150. _kc_hidden ECLogger *GetAudit(void) { return m_lpAudit; }
  151. _kc_hidden ECPluginFactory *GetPluginFactory(void) { return m_lpPluginFactory; }
  152. _kc_hidden ECLockManager *GetLockManager(void) { return m_ptrLockManager.get(); }
  153. protected:
  154. _kc_hidden BTSession *GetSession(ECSESSIONID, bool lock_ses = false);
  155. _kc_hidden ECRESULT ValidateBTSession(struct soap *, ECSESSIONID, BTSession **, bool lock_ses = false);
  156. _kc_hidden BOOL IsSessionPersistent(ECSESSIONID );
  157. _kc_hidden ECRESULT UpdateSubscribedTables(ECKeyTable::UpdateType, TABLESUBSCRIPTION, std::list<unsigned int> &child_id);
  158. _kc_hidden ECRESULT SaveSourceKeyAutoIncrement(unsigned long long new_src_key_autoincr);
  159. EC_SESSIONGROUPMAP m_mapSessionGroups; ///< map of all the session groups
  160. SESSIONMAP m_mapSessions; ///< map of all the sessions
  161. KC::shared_mutex m_hCacheRWLock; ///< locking of the sessionMap
  162. KC::shared_mutex m_hGroupLock; ///< locking of session group map and lonely list
  163. std::mutex m_hExitMutex; /* Mutex needed for the release signal */
  164. std::condition_variable m_hExitSignal; /* Signal that should be send to the sessionncleaner when to exit */
  165. pthread_t m_hSessionCleanerThread;///< Thread that is used for the sessioncleaner
  166. bool m_bTerminateThread;
  167. ECConfig* m_lpConfig;
  168. bool bExit = false;
  169. ECCacheManager* m_lpECCacheManager;
  170. ECLogger* m_lpAudit;
  171. ECDatabaseFactory* m_lpDatabaseFactory;
  172. ECPluginFactory* m_lpPluginFactory;
  173. ECSearchFolders* m_lpSearchFolders;
  174. bool m_bHostedKopano;
  175. bool m_bDistributedKopano;
  176. GUID *m_lpServerGuid = nullptr;
  177. unsigned long long m_ullSourceKeyAutoIncrement = 0;
  178. unsigned int m_ulSourceKeyQueue = 0;
  179. std::mutex m_hSourceKeyAutoIncrementMutex;
  180. ECDatabase *m_lpDatabase = nullptr;
  181. std::mutex m_mutexPersistent;
  182. PERSISTENTBYSESSION m_mapPersistentBySession; ///< map of all persistent sessions mapped to their connection id
  183. PERSISTENTBYCONNECTION m_mapPersistentByConnection; ///< map of all persistent connections mapped to their sessions
  184. std::mutex m_mutexTableSubscriptions;
  185. unsigned int m_ulTableSubscriptionId;
  186. TABLESUBSCRIPTIONMULTIMAP m_mapTableSubscriptions; ///< Maps a table subscription to the subscriber
  187. std::mutex m_mutexObjectSubscriptions;
  188. OBJECTSUBSCRIPTIONSMULTIMAP m_mapObjectSubscriptions; ///< Maps an object notification subscription (store id) to the subscriber
  189. ECNotificationManager *m_lpNotificationManager;
  190. ECTPropsPurge *m_lpTPropsPurge;
  191. ECLockManagerPtr m_ptrLockManager;
  192. // Sequences
  193. std::mutex m_hSeqMutex;
  194. unsigned long long m_ulSeqIMAP = 0;
  195. unsigned int m_ulSeqIMAPQueue = 0;
  196. };
  197. extern _kc_export ECSessionManager *g_lpSessionManager;
  198. } /* namespace */
  199. #endif // #ifndef ECSESSIONMANAGER