ECSession.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. // ECSession.h: interface for the ECSession class.
  18. //
  19. //////////////////////////////////////////////////////////////////////
  20. #ifndef ECSESSION
  21. #define ECSESSION
  22. #include <kopano/zcdefs.h>
  23. #include <condition_variable>
  24. #include <list>
  25. #include <map>
  26. #include <mutex>
  27. #include <pthread.h>
  28. #include "soapH.h"
  29. #include <kopano/kcodes.h>
  30. #include "ECNotification.h"
  31. #include "ECTableManager.h"
  32. #include <kopano/ECConfig.h>
  33. #include <kopano/ECLogger.h>
  34. #include "ECDatabaseFactory.h"
  35. #include "ECPluginFactory.h"
  36. #include "ECSessionGroup.h"
  37. #include "ECLockManager.h"
  38. #include "kcore.hpp"
  39. #ifdef HAVE_GSSAPI
  40. #include <gssapi/gssapi.h>
  41. #endif
  42. struct soap;
  43. namespace KC {
  44. class ECSecurity;
  45. class ECUserManagement;
  46. class SOURCEKEY;
  47. void CreateSessionID(unsigned int ulCapabilities, ECSESSIONID *lpSessionId);
  48. enum { SESSION_STATE_PROCESSING, SESSION_STATE_SENDING };
  49. struct BUSYSTATE {
  50. const char *fname;
  51. struct timespec threadstart;
  52. double start;
  53. pthread_t threadid;
  54. int state;
  55. };
  56. /*
  57. BaseType session
  58. */
  59. class _kc_export BTSession {
  60. public:
  61. _kc_hidden BTSession(const char *addr, ECSESSIONID, ECDatabaseFactory *, ECSessionManager *, unsigned int caps);
  62. _kc_hidden virtual ~BTSession(void) _kc_impdtor;
  63. _kc_hidden virtual ECRESULT Shutdown(unsigned int timeout);
  64. _kc_hidden virtual ECRESULT ValidateOriginator(struct soap *);
  65. _kc_hidden virtual ECSESSIONID GetSessionId(void) const { return m_sessionID; }
  66. _kc_hidden virtual time_t GetSessionTime(void) const { return m_sessionTime + m_ulSessionTimeout; }
  67. _kc_hidden virtual void UpdateSessionTime(void);
  68. _kc_hidden virtual unsigned int GetCapabilities(void) const { return m_ulClientCapabilities; }
  69. _kc_hidden virtual ECSessionManager *GetSessionManager(void) const { return m_lpSessionManager; }
  70. _kc_hidden virtual ECUserManagement *GetUserManagement(void) const { return m_lpUserManagement; }
  71. virtual ECRESULT GetDatabase(ECDatabase **);
  72. _kc_hidden virtual ECRESULT GetAdditionalDatabase(ECDatabase **);
  73. _kc_hidden ECRESULT GetServerGUID(GUID *);
  74. _kc_hidden ECRESULT GetNewSourceKey(SOURCEKEY *);
  75. _kc_hidden virtual void SetClientMeta(const char *cl_vers, const char *cl_misc);
  76. _kc_hidden virtual void GetClientApplicationVersion(std::string *);
  77. _kc_hidden virtual void GetClientApplicationMisc(std::string *);
  78. virtual void Lock(void);
  79. virtual void Unlock(void);
  80. _kc_hidden virtual bool IsLocked(void) const { return m_ulRefCount > 0; }
  81. _kc_hidden virtual void RecordRequest(struct soap *);
  82. _kc_hidden virtual unsigned int GetRequests(void);
  83. _kc_hidden virtual void GetClientPort(unsigned int *);
  84. _kc_hidden virtual void GetRequestURL(std::string *);
  85. _kc_hidden virtual void GetProxyHost(std::string *);
  86. _kc_hidden size_t GetInternalObjectSize(void);
  87. _kc_hidden virtual size_t GetObjectSize(void) = 0;
  88. _kc_hidden time_t GetIdleTime(void);
  89. _kc_hidden const std::string &GetSourceAddr(void) const { return m_strSourceAddr; }
  90. enum AUTHMETHOD {
  91. METHOD_NONE, METHOD_USERPASSWORD, METHOD_SOCKET, METHOD_SSO, METHOD_SSL_CERT
  92. };
  93. protected:
  94. unsigned int m_ulRefCount;
  95. std::string m_strSourceAddr;
  96. ECSESSIONID m_sessionID;
  97. bool m_bCheckIP;
  98. time_t m_sessionTime;
  99. unsigned int m_ulSessionTimeout;
  100. ECDatabaseFactory *m_lpDatabaseFactory;
  101. ECSessionManager *m_lpSessionManager;
  102. ECUserManagement *m_lpUserManagement;
  103. unsigned int m_ulClientCapabilities;
  104. /*
  105. * Protects the object from deleting while a thread is running on a
  106. * method in this object.
  107. */
  108. std::condition_variable m_hThreadReleased;
  109. std::mutex m_hThreadReleasedMutex;
  110. std::mutex m_hRequestStats;
  111. unsigned int m_ulRequests;
  112. std::string m_strLastRequestURL;
  113. std::string m_strProxyHost;
  114. unsigned int m_ulLastRequestPort;
  115. std::string m_strClientApplicationVersion, m_strClientApplicationMisc;
  116. };
  117. /*
  118. Normal session
  119. */
  120. class _kc_export_dycast ECSession _kc_final : public BTSession {
  121. public:
  122. _kc_hidden ECSession(const char *addr, ECSESSIONID, ECSESSIONGROUPID, ECDatabaseFactory *, ECSessionManager *, unsigned int caps, AUTHMETHOD, int pid, const std::string &cl_vers, const std::string &cl_app, const std::string &cl_app_ver, const std::string &cl_app_misc);
  123. _kc_hidden virtual ECSESSIONGROUPID GetSessionGroupId(void) const { return m_ecSessionGroupId; }
  124. _kc_hidden virtual int GetConnectingPid(void) const { return m_ulConnectingPid; }
  125. _kc_hidden virtual ~ECSession(void);
  126. _kc_hidden virtual ECRESULT Shutdown(unsigned int timeout);
  127. /* Notification functions all wrap directly to SessionGroup */
  128. _kc_hidden ECRESULT AddAdvise(unsigned int conn, unsigned int key, unsigned int event_mask);
  129. _kc_hidden ECRESULT AddChangeAdvise(unsigned int conn, notifySyncState *);
  130. _kc_hidden ECRESULT DelAdvise(unsigned int conn);
  131. _kc_hidden ECRESULT AddNotificationTable(unsigned int type, unsigned int obj_type, unsigned int table, sObjectTableKey *child_row, sObjectTableKey *prev_row, struct propValArray *row);
  132. _kc_hidden ECRESULT GetNotifyItems(struct soap *, struct notifyResponse *notifications);
  133. _kc_hidden ECTableManager *GetTableManager(void) const { return m_lpTableManager; }
  134. _kc_hidden ECSecurity *GetSecurity(void) const { return m_lpEcSecurity; }
  135. _kc_hidden ECRESULT GetObjectFromEntryId(const entryId *, unsigned int *obj_id, unsigned int *eid_flags = nullptr);
  136. _kc_hidden ECRESULT LockObject(unsigned int obj_id);
  137. _kc_hidden ECRESULT UnlockObject(unsigned int obj_id);
  138. /* for ECStatsSessionTable */
  139. _kc_hidden void AddBusyState(pthread_t, const char *state, const struct timespec &threadstart, double start);
  140. _kc_hidden void UpdateBusyState(pthread_t, int state);
  141. _kc_hidden void RemoveBusyState(pthread_t);
  142. _kc_hidden void GetBusyStates(std::list<BUSYSTATE> *);
  143. _kc_hidden void AddClocks(double user, double system, double real);
  144. _kc_hidden void GetClocks(double *user, double *system, double *real);
  145. _kc_hidden void GetClientVersion(std::string *version);
  146. _kc_hidden void GetClientApp(std::string *client_app);
  147. _kc_hidden size_t GetObjectSize(void);
  148. _kc_hidden unsigned int ClientVersion(void) const { return m_ulClientVersion; }
  149. _kc_hidden AUTHMETHOD GetAuthMethod(void) const { return m_ulAuthMethod; }
  150. private:
  151. ECTableManager *m_lpTableManager;
  152. ECSessionGroup *m_lpSessionGroup;
  153. ECSecurity *m_lpEcSecurity = nullptr;
  154. std::mutex m_hStateLock;
  155. typedef std::map<pthread_t, BUSYSTATE> BusyStateMap;
  156. BusyStateMap m_mapBusyStates; /* which thread does what function */
  157. double m_dblUser = 0, m_dblSystem = 0, m_dblReal = 0;
  158. AUTHMETHOD m_ulAuthMethod;
  159. int m_ulConnectingPid;
  160. ECSESSIONGROUPID m_ecSessionGroupId;
  161. std::string m_strClientVersion;
  162. unsigned int m_ulClientVersion;
  163. std::string m_strClientApp;
  164. std::string m_strUsername;
  165. typedef std::map<unsigned int, ECObjectLock> LockMap;
  166. std::mutex m_hLocksLock;
  167. LockMap m_mapLocks;
  168. };
  169. /*
  170. Authentication session
  171. */
  172. class _kc_export_dycast ECAuthSession _kc_final : public BTSession {
  173. public:
  174. _kc_hidden ECAuthSession(const char *addr, ECSESSIONID, ECDatabaseFactory *, ECSessionManager *, unsigned int caps);
  175. _kc_hidden virtual ~ECAuthSession(void);
  176. _kc_hidden ECRESULT ValidateUserLogon(const char *name, const char *pass, const char *imp_user);
  177. _kc_hidden ECRESULT ValidateUserSocket(int socket, const char *name, const char *imp_user);
  178. _kc_hidden ECRESULT ValidateUserCertificate(struct soap *, const char *name, const char *imp_user);
  179. _kc_hidden ECRESULT ValidateSSOData(struct soap *, const char *name, const char *imp_user, const char *cl_ver, const char *cl_app, const char *cl_app_ver, const char *cl_app_misc, const struct xsd__base64Binary *input, struct xsd__base64Binary **output);
  180. _kc_hidden virtual ECRESULT CreateECSession(ECSESSIONGROUPID, const std::string &cl_ver, const std::string &cl_app, const std::string &cl_app_ver, const std::string &cl_app_misc, ECSESSIONID *retid, ECSession **ret);
  181. _kc_hidden size_t GetObjectSize(void);
  182. protected:
  183. unsigned int m_ulUserID = 0;
  184. unsigned int m_ulImpersonatorID = 0; // The ID of the user who's credentials were used to login when using impersonation
  185. bool m_bValidated = false;
  186. AUTHMETHOD m_ulValidationMethod = METHOD_NONE;
  187. int m_ulConnectingPid = 0;
  188. private:
  189. /* SSO */
  190. _kc_hidden ECRESULT ValidateSSOData_NTLM(struct soap *, const char *name, const char *cl_ver, const char *cl_app, const char *cl_app_ver, const char *cl_app_misc, const struct xsd__base64Binary *input, struct xsd__base64Binary **out);
  191. _kc_hidden ECRESULT ValidateSSOData_KRB5(struct soap *, const char *name, const char *cl_ver, const char *cl_app, const char *cl_app_ver, const char *cl_app_misc, const struct xsd__base64Binary *input, struct xsd__base64Binary **out);
  192. #ifdef HAVE_GSSAPI
  193. _kc_hidden ECRESULT LogKRB5Error(const char *msg, OM_uint32 major, OM_uint32 minor);
  194. #endif
  195. _kc_hidden ECRESULT ProcessImpersonation(const char *imp_user);
  196. /* NTLM */
  197. pid_t m_NTLM_pid = -1;
  198. int m_NTLM_stdin[2], m_NTLM_stdout[2], m_NTLM_stderr[2];
  199. int m_stdin = -1, m_stdout = -1, m_stderr = -1; /* shortcuts to the above */
  200. #ifdef HAVE_GSSAPI
  201. /* KRB5 */
  202. gss_cred_id_t m_gssServerCreds;
  203. gss_ctx_id_t m_gssContext;
  204. #endif
  205. };
  206. } /* namespace */
  207. #endif // #ifndef ECSESSION