ECServerEntrypoint.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/zcdefs.h>
  18. #include <kopano/platform.h>
  19. #include <mutex>
  20. #include <pthread.h>
  21. #include <kopano/ECLogger.h>
  22. #include <kopano/lockhelper.hpp>
  23. #include "ECDatabase.h"
  24. #include "ECSessionManager.h"
  25. #include "ECStatsCollector.h"
  26. #include "ECDatabaseFactory.h"
  27. #include "ECServerEntrypoint.h"
  28. #include "ECS3Attachment.h"
  29. namespace KC {
  30. pthread_key_t database_key;
  31. pthread_key_t plugin_key;
  32. _kc_export ECSessionManager *g_lpSessionManager;
  33. _kc_export ECStatsCollector *g_lpStatsCollector;
  34. static std::set<ECDatabase *> g_lpDBObjectList;
  35. static std::mutex g_hMutexDBObjectList;
  36. static bool g_bInitLib = false;
  37. void AddDatabaseObject(ECDatabase* lpDatabase)
  38. {
  39. scoped_lock lk(g_hMutexDBObjectList);
  40. g_lpDBObjectList.insert(std::set<ECDatabase*>::value_type(lpDatabase));
  41. }
  42. static void database_destroy(void *lpParam)
  43. {
  44. auto lpDatabase = static_cast<ECDatabase *>(lpParam);
  45. ulock_normal l_obj(g_hMutexDBObjectList);
  46. g_lpDBObjectList.erase(std::set<ECDatabase*>::key_type(lpDatabase));
  47. l_obj.unlock();
  48. lpDatabase->ThreadEnd();
  49. delete lpDatabase;
  50. }
  51. static void plugin_destroy(void *lpParam)
  52. {
  53. delete static_cast<UserPlugin *>(lpParam);
  54. }
  55. ECRESULT kopano_initlibrary(const char *lpDatabaseDir, const char *lpConfigFile)
  56. {
  57. ECRESULT er;
  58. if (g_bInitLib == true)
  59. return KCERR_CALL_FAILED;
  60. // This is a global key that we can reference from each thread with a different value. The
  61. // database_destroy routine is called when the thread terminates.
  62. pthread_key_create(&database_key, database_destroy);
  63. pthread_key_create(&plugin_key, plugin_destroy); // same goes for the userDB-plugin
  64. // Init mutex for database object list
  65. er = ECDatabase::InitLibrary(lpDatabaseDir, lpConfigFile);
  66. g_lpStatsCollector = new ECStatsCollector();
  67. //TODO: with an error remove all variables and g_bInitLib = false
  68. g_bInitLib = true;
  69. return er;
  70. }
  71. ECRESULT kopano_unloadlibrary(void)
  72. {
  73. if (!g_bInitLib)
  74. return KCERR_NOT_INITIALIZED;
  75. // Delete the global key,
  76. // on this position, there are zero or more threads exist.
  77. // As you delete the keys, the function database_destroy and plugin_destroy will never called
  78. //
  79. pthread_key_delete(database_key);
  80. pthread_key_delete(plugin_key);
  81. // Remove all exist database objects
  82. ulock_normal l_obj(g_hMutexDBObjectList);
  83. auto iterDBObject = g_lpDBObjectList.cbegin();
  84. while (iterDBObject != g_lpDBObjectList.cend())
  85. {
  86. auto iNext = iterDBObject;
  87. ++iNext;
  88. delete (*iterDBObject);
  89. g_lpDBObjectList.erase(iterDBObject);
  90. iterDBObject = iNext;
  91. }
  92. l_obj.unlock();
  93. // remove mutex for database object list
  94. ECDatabase::UnloadLibrary();
  95. g_bInitLib = false;
  96. return erSuccess;
  97. }
  98. ECRESULT kopano_init(ECConfig *lpConfig, ECLogger *lpAudit, bool bHostedKopano, bool bDistributedKopano)
  99. {
  100. ECRESULT er;
  101. if (!g_bInitLib)
  102. return KCERR_NOT_INITIALIZED;
  103. g_lpSessionManager = new ECSessionManager(lpConfig, lpAudit, bHostedKopano, bDistributedKopano);
  104. er = g_lpSessionManager->LoadSettings();
  105. if(er != erSuccess)
  106. return er;
  107. er = g_lpSessionManager->CheckUserLicense();
  108. if (er != erSuccess)
  109. return er;
  110. #ifdef HAVE_LIBS3_H
  111. if (strcmp(lpConfig->GetSetting("attachment_storage"), "s3") == 0)
  112. ECS3Attachment::StaticInit(lpConfig);
  113. #endif
  114. return erSuccess;
  115. }
  116. void kopano_removeallsessions()
  117. {
  118. if (g_lpSessionManager != nullptr)
  119. g_lpSessionManager->RemoveAllSessions();
  120. }
  121. ECRESULT kopano_exit()
  122. {
  123. if (!g_bInitLib)
  124. return KCERR_NOT_INITIALIZED;
  125. #ifdef HAVE_LIBS3_H
  126. if (g_lpSessionManager && strcmp(g_lpSessionManager->GetConfig()->GetSetting("attachment_storage"), "s3") == 0)
  127. ECS3Attachment::StaticDeinit();
  128. #endif
  129. // delete our plugin of the mainthread: requires ECPluginFactory to be alive, because that holds the dlopen() result
  130. plugin_destroy(pthread_getspecific(plugin_key));
  131. delete g_lpSessionManager;
  132. g_lpSessionManager = NULL;
  133. delete g_lpStatsCollector;
  134. g_lpStatsCollector = NULL;
  135. // Close all database connections
  136. scoped_lock l_obj(g_hMutexDBObjectList);
  137. for (auto dbobjp : g_lpDBObjectList)
  138. dbobjp->Close();
  139. return erSuccess;
  140. }
  141. /**
  142. * Called for each HTTP header in a request, handles the proxy header
  143. * and marks the connection as using the proxy if it is found. The value
  144. * of the header is ignored. The special value '*' for proxy_header is
  145. * not searched for here, but it is used in GetBestServerPath()
  146. *
  147. * We use the soap->user->fparsehdr to daisy chain the request to, which
  148. * is the original gSoap header parsing code. This is needed to decode
  149. * normal headers like content-type, etc.
  150. *
  151. * @param[in] soap Soap structure of the incoming call
  152. * @param[in] key Key part of the header (left of the :)
  153. * @param[in] vak Value part of the header (right of the :)
  154. * @return SOAP_OK or soap error
  155. */
  156. static int kopano_fparsehdr(struct soap *soap, const char *key,
  157. const char *val)
  158. {
  159. const char *szProxy = g_lpSessionManager->GetConfig()->GetSetting("proxy_header");
  160. if (strlen(szProxy) > 0 && strcasecmp(key, szProxy) == 0)
  161. soap_info(soap)->bProxy = true;
  162. return soap_info(soap)->fparsehdr(soap, key, val);
  163. }
  164. // Called just after a new soap connection is established
  165. void kopano_new_soap_connection(CONNECTION_TYPE ulType, struct soap *soap)
  166. {
  167. const char *szProxy = g_lpSessionManager->GetConfig()->GetSetting("proxy_header");
  168. auto lpInfo = new SOAPINFO;
  169. lpInfo->ulConnectionType = ulType;
  170. lpInfo->bProxy = false;
  171. soap->user = (void *)lpInfo;
  172. if (szProxy[0] == '\0')
  173. return;
  174. if (strcmp(szProxy, "*") == 0) {
  175. // Assume everything is proxied
  176. lpInfo->bProxy = true;
  177. return;
  178. }
  179. // Parse headers to determine if the connection is proxied
  180. lpInfo->fparsehdr = soap->fparsehdr; // daisy-chain the existing code
  181. soap->fparsehdr = kopano_fparsehdr;
  182. }
  183. void kopano_end_soap_connection(struct soap *soap)
  184. {
  185. delete soap_info(soap);
  186. }
  187. void kopano_new_soap_listener(CONNECTION_TYPE ulType, struct soap *soap)
  188. {
  189. auto lpInfo = new SOAPINFO;
  190. lpInfo->ulConnectionType = ulType;
  191. lpInfo->bProxy = false;
  192. soap->user = (void *)lpInfo;
  193. }
  194. void kopano_end_soap_listener(struct soap *soap)
  195. {
  196. delete soap_info(soap);
  197. }
  198. // Called just before the socket is reset, with the server-side socket still
  199. // open
  200. void kopano_disconnect_soap_connection(struct soap *soap)
  201. {
  202. if (SOAP_CONNECTION_TYPE_NAMED_PIPE(soap))
  203. // Mark the persistent session as exited
  204. g_lpSessionManager->RemoveSessionPersistentConnection((unsigned int)soap->socket);
  205. }
  206. // Export functions
  207. ECRESULT GetDatabaseObject(ECDatabase **lppDatabase)
  208. {
  209. if (g_lpSessionManager == NULL)
  210. return KCERR_UNKNOWN;
  211. if (lppDatabase == NULL)
  212. return KCERR_INVALID_PARAMETER;
  213. ECDatabaseFactory db(g_lpSessionManager->GetConfig());
  214. return GetThreadLocalDatabase(&db, lppDatabase);
  215. }
  216. } /* namespace */