ECStatsTables.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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 <memory>
  19. #include <new>
  20. #include <kopano/tie.hpp>
  21. #include "ECStatsTables.h"
  22. #include "SOAPUtils.h"
  23. #include "ECSession.h"
  24. #include "ECSessionManager.h"
  25. #include "ECUserManagement.h"
  26. #include "ECSecurity.h"
  27. #include <mapidefs.h>
  28. #include <mapicode.h>
  29. #include <mapitags.h>
  30. #include <kopano/mapiext.h>
  31. #include <edkmdb.h>
  32. #include <kopano/ECTags.h>
  33. #include <kopano/stringutil.h>
  34. #include "ECStatsCollector.h"
  35. #if defined(HAVE_GPERFTOOLS_MALLOC_EXTENSION_H)
  36. # include <gperftools/malloc_extension_c.h>
  37. # define HAVE_TCMALLOC 1
  38. #elif defined(HAVE_GOOGLE_MALLOC_EXTENSION_H)
  39. # include <google/malloc_extension_c.h>
  40. # define HAVE_TCMALLOC 1
  41. #endif
  42. #ifdef HAVE_MALLOC_H
  43. # include <malloc.h>
  44. #endif
  45. /*
  46. System stats
  47. - cache stats
  48. - license
  49. */
  50. using namespace KCHL;
  51. namespace KC {
  52. void (*kopano_get_server_stats)(unsigned int *qlen, double *qage, unsigned int *nthr, unsigned int *idlthr) = [](unsigned int *, double *, unsigned int *, unsigned int *) {};
  53. ECSystemStatsTable::ECSystemStatsTable(ECSession *lpSession, unsigned int ulFlags, const ECLocale &locale) : ECGenericObjectTable(lpSession, MAPI_STATUS, ulFlags, locale)
  54. {
  55. m_lpfnQueryRowData = QueryRowData;
  56. id = 0;
  57. }
  58. ECRESULT ECSystemStatsTable::Create(ECSession *lpSession, unsigned int ulFlags,
  59. const ECLocale &locale, ECGenericObjectTable **lppTable)
  60. {
  61. *lppTable = new(std::nothrow) ECSystemStatsTable(lpSession, ulFlags, locale);
  62. if (*lppTable == nullptr)
  63. return KCERR_NOT_ENOUGH_MEMORY;
  64. (*lppTable)->AddRef();
  65. return erSuccess;
  66. }
  67. void ECSystemStatsTable::load_tcmalloc(void)
  68. {
  69. #ifdef HAVE_TCMALLOC
  70. size_t value = 0;
  71. auto gnp = reinterpret_cast<decltype(MallocExtension_GetNumericProperty) *>(dlsym(NULL, "MallocExtension_GetNumericProperty"));
  72. if (gnp == NULL)
  73. return;
  74. gnp("generic.current_allocated_bytes", &value);
  75. GetStatsCollectorData("tc_allocated", "Current allocated memory by TCMalloc", stringify_int64(value), this); // Bytes in use by application
  76. value = 0;
  77. gnp("generic.heap_size", &value);
  78. GetStatsCollectorData("tc_reserved", "Bytes of system memory reserved by TCMalloc", stringify_int64(value), this);
  79. value = 0;
  80. gnp("tcmalloc.pageheap_free_bytes", &value);
  81. GetStatsCollectorData("tc_page_map_free", "Number of bytes in free, mapped pages in page heap", stringify_int64(value), this);
  82. value = 0;
  83. gnp("tcmalloc.pageheap_unmapped_bytes", &value);
  84. GetStatsCollectorData("tc_page_unmap_free", "Number of bytes in free, unmapped pages in page heap (released to OS)", stringify_int64(value), this);
  85. value = 0;
  86. gnp("tcmalloc.max_total_thread_cache_bytes", &value);
  87. GetStatsCollectorData("tc_threadcache_max", "A limit to how much memory TCMalloc dedicates for small objects", stringify_int64(value), this);
  88. value = 0;
  89. gnp("tcmalloc.current_total_thread_cache_bytes", &value);
  90. GetStatsCollectorData("tc_threadcache_cur", "Current allocated memory in bytes for thread cache", stringify_int64(value), this);
  91. #ifdef DEBUG
  92. char test[2048] = {0};
  93. auto getstat = reinterpret_cast<decltype(MallocExtension_GetStats) *>(dlsym(NULL, "MallocExtension_GetStats"));
  94. if (getstat != NULL) {
  95. getstat(test, sizeof(test));
  96. GetStatsCollectorData("tc_stats_string", "TCMalloc memory debug data", test, this);
  97. }
  98. #endif
  99. #endif
  100. }
  101. ECRESULT ECSystemStatsTable::Load()
  102. {
  103. ECRESULT er = erSuccess;
  104. sObjectTableKey sRowItem;
  105. unsigned int i;
  106. unsigned int ulQueueLen = 0;
  107. double dblAge = 0;
  108. unsigned int ulThreads = 0;
  109. unsigned int ulIdleThreads = 0;
  110. unsigned int ulLicensedUsers = 0;
  111. usercount_t userCount;
  112. id = 0;
  113. g_lpStatsCollector->ForEachString(this->GetStatsCollectorData, (void*)this);
  114. g_lpStatsCollector->ForEachStat(this->GetStatsCollectorData, (void*)this);
  115. lpSession->GetSessionManager()->GetCacheManager()->ForEachCacheItem(this->GetStatsCollectorData, (void*)this);
  116. // Receive session stats
  117. lpSession->GetSessionManager()->GetStats(this->GetStatsCollectorData, (void*)this);
  118. kopano_get_server_stats(&ulQueueLen, &dblAge, &ulThreads, &ulIdleThreads);
  119. GetStatsCollectorData("queuelen", "Current queue length", stringify(ulQueueLen), this);
  120. GetStatsCollectorData("queueage", "Age of the front queue item", stringify_double(dblAge,3), this);
  121. GetStatsCollectorData("threads", "Number of threads running to process items", stringify(ulThreads), this);
  122. GetStatsCollectorData("threads_idle", "Number of idle threads", stringify(ulIdleThreads), this);
  123. lpSession->GetSessionManager()->GetLicensedUsers(0 /*SERVICE_TYPE_ZCP*/, &ulLicensedUsers);
  124. lpSession->GetUserManagement()->GetCachedUserCount(&userCount);
  125. GetStatsCollectorData("usercnt_licensed", "Number of allowed users", stringify(ulLicensedUsers), this);
  126. GetStatsCollectorData("usercnt_active", "Number of active users", stringify(userCount[usercount_t::ucActiveUser]), this);
  127. GetStatsCollectorData("usercnt_nonactive", "Number of total non-active objects", stringify(userCount[usercount_t::ucNonActiveTotal]), this);
  128. GetStatsCollectorData("usercnt_na_user", "Number of non-active users", stringify(userCount[usercount_t::ucNonActiveUser]), this);
  129. GetStatsCollectorData("usercnt_room", "Number of rooms", stringify(userCount[usercount_t::ucRoom]), this);
  130. GetStatsCollectorData("usercnt_equipment", "Number of equipment", stringify(userCount[usercount_t::ucEquipment]), this);
  131. GetStatsCollectorData("usercnt_contact", "Number of contacts", stringify(userCount[usercount_t::ucContact]), this);
  132. // @todo report licensed archived users and current archieved users
  133. //lpSession->GetSessionManager()->GetLicensedUsers(1/*SERVICE_TYPE_ARCHIVE*/, &ulLicensedArchivedUsers);
  134. //GetStatsCollectorData("??????????", "Number of allowed archive users", stringify(ulLicensedArchivedUsers), this);
  135. load_tcmalloc();
  136. #ifdef HAVE_MALLINFO
  137. /* parallel threaded allocator */
  138. struct mallinfo malloc_info = mallinfo();
  139. GetStatsCollectorData("pt_allocated", "Current allocated memory by libc ptmalloc, in bytes", stringify_int64(malloc_info.uordblks), this);
  140. #endif
  141. /* Configuration data */
  142. auto lpConfig = lpSession->GetSessionManager()->GetConfig();
  143. GetStatsCollectorData("userplugin", "User plugin used", lpConfig->GetSetting("user_plugin"), this);
  144. // add all items to the keytable
  145. for (i = 0; i < id; ++i)
  146. // Use MAPI_STATUS as ulObjType for the key table .. this param may be removed in the future..?
  147. UpdateRow(ECKeyTable::TABLE_ROW_ADD, i, 0);
  148. return er;
  149. }
  150. void ECSystemStatsTable::GetStatsCollectorData(const std::string &name, const std::string &description, const std::string &value, void *obj)
  151. {
  152. auto lpThis = static_cast<ECSystemStatsTable *>(obj);
  153. statstrings ss;
  154. ss.name = name;
  155. ss.description = description;
  156. ss.value = value;
  157. lpThis->m_mapStatData[lpThis->id] = ss;
  158. ++lpThis->id;
  159. }
  160. ECRESULT ECSystemStatsTable::QueryRowData(ECGenericObjectTable *lpGenericThis, struct soap *soap, ECSession *lpSession, ECObjectTableList *lpRowList, struct propTagArray *lpsPropTagArray, void *lpObjectData, struct rowSet **lppRowSet, bool bCacheTableData, bool bTableLimit)
  161. {
  162. struct rowSet *lpsRowSet = NULL;
  163. auto lpThis = static_cast<ECSystemStatsTable *>(lpGenericThis);
  164. gsoap_size_t i;
  165. lpsRowSet = s_alloc<rowSet>(soap);
  166. lpsRowSet->__size = 0;
  167. lpsRowSet->__ptr = NULL;
  168. if (lpRowList->empty()) {
  169. *lppRowSet = lpsRowSet;
  170. return erSuccess;
  171. }
  172. // We return a square array with all the values
  173. lpsRowSet->__size = lpRowList->size();
  174. lpsRowSet->__ptr = s_alloc<propValArray>(soap, lpsRowSet->__size);
  175. memset(lpsRowSet->__ptr, 0, sizeof(propValArray) * lpsRowSet->__size);
  176. // Allocate memory for all rows
  177. for (i = 0; i < lpsRowSet->__size; ++i) {
  178. lpsRowSet->__ptr[i].__size = lpsPropTagArray->__size;
  179. lpsRowSet->__ptr[i].__ptr = s_alloc<propVal>(soap, lpsPropTagArray->__size);
  180. memset(lpsRowSet->__ptr[i].__ptr, 0, sizeof(propVal) * lpsPropTagArray->__size);
  181. }
  182. i = 0;
  183. for (const auto &row : *lpRowList) {
  184. auto iterSD = lpThis->m_mapStatData.find(row.ulObjId);
  185. for (gsoap_size_t k = 0; k < lpsPropTagArray->__size; ++k) {
  186. if (iterSD == lpThis->m_mapStatData.cend())
  187. continue; // broken .. should never happen
  188. // default is error prop
  189. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = PROP_TAG(PROP_TYPE(PT_ERROR), PROP_ID(lpsPropTagArray->__ptr[k]));
  190. lpsRowSet->__ptr[i].__ptr[k].Value.ul = KCERR_NOT_FOUND;
  191. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_ul;
  192. switch (PROP_ID(lpsPropTagArray->__ptr[k])) {
  193. case PROP_ID(PR_INSTANCE_KEY):
  194. // generate key
  195. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_bin;
  196. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  197. lpsRowSet->__ptr[i].__ptr[k].Value.bin = s_alloc<xsd__base64Binary>(soap);
  198. lpsRowSet->__ptr[i].__ptr[k].Value.bin->__size = sizeof(sObjectTableKey);
  199. lpsRowSet->__ptr[i].__ptr[k].Value.bin->__ptr = s_alloc<unsigned char>(soap, sizeof(sObjectTableKey));
  200. memcpy(lpsRowSet->__ptr[i].__ptr[k].Value.bin->__ptr, &row, sizeof(sObjectTableKey));
  201. break;
  202. case PROP_ID(PR_DISPLAY_NAME):
  203. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  204. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  205. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_alloc<char>(soap, iterSD->second.name.length()+1);
  206. strcpy(lpsRowSet->__ptr[i].__ptr[k].Value.lpszA, iterSD->second.name.c_str());
  207. break;
  208. case PROP_ID(PR_EC_STATS_SYSTEM_DESCRIPTION):
  209. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  210. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  211. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_alloc<char>(soap, iterSD->second.description.length()+1);
  212. strcpy(lpsRowSet->__ptr[i].__ptr[k].Value.lpszA, iterSD->second.description.c_str());
  213. break;
  214. case PROP_ID(PR_EC_STATS_SYSTEM_VALUE):
  215. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  216. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  217. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_alloc<char>(soap, iterSD->second.value.length()+1);
  218. strcpy(lpsRowSet->__ptr[i].__ptr[k].Value.lpszA, iterSD->second.value.c_str());
  219. break;
  220. }
  221. }
  222. ++i;
  223. }
  224. *lppRowSet = lpsRowSet;
  225. return erSuccess;
  226. }
  227. /*
  228. Session stats
  229. - session object:
  230. - ipaddress
  231. - session (idle) time
  232. - capabilities (compression enabled)
  233. - session lock
  234. - ecsecurity object:
  235. - username
  236. - busystates
  237. */
  238. ECSessionStatsTable::ECSessionStatsTable(ECSession *lpSession, unsigned int ulFlags, const ECLocale &locale) : ECGenericObjectTable(lpSession, MAPI_STATUS, ulFlags, locale)
  239. {
  240. m_lpfnQueryRowData = QueryRowData;
  241. id = 0;
  242. }
  243. ECRESULT ECSessionStatsTable::Create(ECSession *lpSession, unsigned int ulFlags,
  244. const ECLocale &locale, ECGenericObjectTable **lppTable)
  245. {
  246. *lppTable = new(std::nothrow) ECSessionStatsTable(lpSession, ulFlags, locale);
  247. if (*lppTable == nullptr)
  248. return KCERR_NOT_ENOUGH_MEMORY;
  249. (*lppTable)->AddRef();
  250. return erSuccess;
  251. }
  252. ECRESULT ECSessionStatsTable::Load()
  253. {
  254. ECRESULT er = erSuccess;
  255. sObjectTableKey sRowItem;
  256. ECSessionManager *lpSessionManager = lpSession->GetSessionManager();
  257. unsigned int i;
  258. id = 0;
  259. // get all data items available
  260. // since the table is too volatile, collect all the data at once, and not in QueryRowData
  261. lpSessionManager->ForEachSession(this->GetSessionData, (void*)this);
  262. // add all items to the keytable
  263. for (i = 0; i < id; ++i)
  264. // Use MAPI_STATUS as ulObjType for the key table .. this param may be removed in the future..?
  265. UpdateRow(ECKeyTable::TABLE_ROW_ADD, i, 0);
  266. return er;
  267. }
  268. void ECSessionStatsTable::GetSessionData(ECSession *lpSession, void *obj)
  269. {
  270. auto lpThis = static_cast<ECSessionStatsTable *>(obj);
  271. sessiondata sd;
  272. if (lpSession == nullptr)
  273. // dynamic_cast failed
  274. return;
  275. sd.sessionid = lpSession->GetSessionId();
  276. sd.sessiongroupid = lpSession->GetSessionGroupId();
  277. sd.peerpid = lpSession->GetConnectingPid();
  278. sd.srcaddress = lpSession->GetSourceAddr();
  279. sd.idletime = lpSession->GetIdleTime();
  280. sd.capability = lpSession->GetCapabilities();
  281. sd.locked = lpSession->IsLocked();
  282. lpSession->GetClocks(&sd.dblUser, &sd.dblSystem, &sd.dblReal);
  283. lpSession->GetSecurity()->GetUsername(&sd.username);
  284. lpSession->GetBusyStates(&sd.busystates);
  285. lpSession->GetClientVersion(&sd.version);
  286. lpSession->GetClientApp(&sd.clientapp);
  287. lpSession->GetClientPort(&sd.port);
  288. lpSession->GetRequestURL(&sd.url);
  289. lpSession->GetProxyHost(&sd.proxyhost);
  290. lpSession->GetClientApplicationVersion(&sd.client_application_version);
  291. lpSession->GetClientApplicationMisc(&sd.client_application_misc);
  292. sd.requests = lpSession->GetRequests();
  293. // To get up-to-date CPU stats, check each of the active threads on the session
  294. // for their CPU usage, and add that to the already-logged time on the session
  295. for (const auto &bs : sd.busystates) {
  296. clockid_t clock;
  297. struct timespec now;
  298. if (pthread_getcpuclockid(bs.threadid, &clock) != 0)
  299. continue;
  300. clock_gettime(clock, &now);
  301. sd.dblUser += timespec2dbl(now) - timespec2dbl(bs.threadstart);
  302. sd.dblReal += GetTimeOfDay() - bs.start;
  303. }
  304. lpThis->m_mapSessionData[lpThis->id] = sd;
  305. ++lpThis->id;
  306. }
  307. ECRESULT ECSessionStatsTable::QueryRowData(ECGenericObjectTable *lpGenericThis, struct soap *soap, ECSession *lpSession, ECObjectTableList *lpRowList, struct propTagArray *lpsPropTagArray, void *lpObjectData, struct rowSet **lppRowSet, bool bCacheTableData, bool bTableLimit)
  308. {
  309. struct rowSet *lpsRowSet = NULL;
  310. auto lpThis = static_cast<ECSessionStatsTable *>(lpGenericThis);
  311. gsoap_size_t i;
  312. std::string strTemp;
  313. lpsRowSet = s_alloc<rowSet>(soap);
  314. lpsRowSet->__size = 0;
  315. lpsRowSet->__ptr = NULL;
  316. if (lpRowList->empty()) {
  317. *lppRowSet = lpsRowSet;
  318. return erSuccess;
  319. }
  320. // We return a square array with all the values
  321. lpsRowSet->__size = lpRowList->size();
  322. lpsRowSet->__ptr = s_alloc<propValArray>(soap, lpsRowSet->__size);
  323. memset(lpsRowSet->__ptr, 0, sizeof(propValArray) * lpsRowSet->__size);
  324. // Allocate memory for all rows
  325. for (i = 0; i < lpsRowSet->__size; ++i) {
  326. lpsRowSet->__ptr[i].__size = lpsPropTagArray->__size;
  327. lpsRowSet->__ptr[i].__ptr = s_alloc<propVal>(soap, lpsPropTagArray->__size);
  328. memset(lpsRowSet->__ptr[i].__ptr, 0, sizeof(propVal) * lpsPropTagArray->__size);
  329. }
  330. i = 0;
  331. for (const auto &row : *lpRowList) {
  332. auto iterSD = lpThis->m_mapSessionData.find(row.ulObjId);
  333. for (gsoap_size_t k = 0; k < lpsPropTagArray->__size; ++k) {
  334. gsoap_size_t j;
  335. // default is error prop
  336. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = PROP_TAG(PROP_TYPE(PT_ERROR), PROP_ID(lpsPropTagArray->__ptr[k]));
  337. lpsRowSet->__ptr[i].__ptr[k].Value.ul = KCERR_NOT_FOUND;
  338. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_ul;
  339. if (iterSD == lpThis->m_mapSessionData.cend())
  340. continue; // broken; should never happen
  341. switch (PROP_ID(lpsPropTagArray->__ptr[k])) {
  342. case PROP_ID(PR_INSTANCE_KEY):
  343. // generate key
  344. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_bin;
  345. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  346. lpsRowSet->__ptr[i].__ptr[k].Value.bin = s_alloc<xsd__base64Binary>(soap);
  347. lpsRowSet->__ptr[i].__ptr[k].Value.bin->__size = sizeof(sObjectTableKey);
  348. lpsRowSet->__ptr[i].__ptr[k].Value.bin->__ptr = s_alloc<unsigned char>(soap, sizeof(sObjectTableKey));
  349. memcpy(lpsRowSet->__ptr[i].__ptr[k].Value.bin->__ptr, &row, sizeof(sObjectTableKey));
  350. break;
  351. case PROP_ID(PR_EC_USERNAME):
  352. strTemp = iterSD->second.username;
  353. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  354. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  355. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_alloc<char>(soap, strTemp.length()+1);
  356. strcpy(lpsRowSet->__ptr[i].__ptr[k].Value.lpszA, strTemp.c_str());
  357. break;
  358. case PROP_ID(PR_EC_STATS_SESSION_ID):
  359. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_li;
  360. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  361. lpsRowSet->__ptr[i].__ptr[k].Value.li = iterSD->second.sessionid;
  362. break;
  363. case PROP_ID(PR_EC_STATS_SESSION_GROUP_ID):
  364. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_li;
  365. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  366. lpsRowSet->__ptr[i].__ptr[k].Value.li = iterSD->second.sessiongroupid;
  367. break;
  368. case PROP_ID(PR_EC_STATS_SESSION_PEER_PID):
  369. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_ul;
  370. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  371. lpsRowSet->__ptr[i].__ptr[k].Value.ul = iterSD->second.peerpid;
  372. break;
  373. case PROP_ID(PR_EC_STATS_SESSION_CLIENT_VERSION):
  374. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  375. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  376. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_strcpy(soap, iterSD->second.version.c_str());
  377. break;
  378. case PROP_ID(PR_EC_STATS_SESSION_CLIENT_APPLICATION):
  379. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  380. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  381. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_strcpy(soap, iterSD->second.clientapp.c_str());
  382. break;
  383. case PROP_ID(PR_EC_STATS_SESSION_IPADDRESS):
  384. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  385. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  386. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_strcpy(soap, iterSD->second.srcaddress.c_str());
  387. break;
  388. case PROP_ID(PR_EC_STATS_SESSION_PORT):
  389. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_ul;
  390. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  391. lpsRowSet->__ptr[i].__ptr[k].Value.ul = iterSD->second.port;
  392. break;
  393. case PROP_ID(PR_EC_STATS_SESSION_URL):
  394. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  395. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  396. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_strcpy(soap, iterSD->second.url.c_str());
  397. break;
  398. case PROP_ID(PR_EC_STATS_SESSION_PROXY):
  399. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  400. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  401. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_strcpy(soap, iterSD->second.proxyhost.c_str());
  402. break;
  403. case PROP_ID(PR_EC_STATS_SESSION_IDLETIME):
  404. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_ul;
  405. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  406. lpsRowSet->__ptr[i].__ptr[k].Value.ul = iterSD->second.idletime;
  407. break;
  408. case PROP_ID(PR_EC_STATS_SESSION_CAPABILITY):
  409. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_ul;
  410. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  411. lpsRowSet->__ptr[i].__ptr[k].Value.ul = iterSD->second.capability;
  412. break;
  413. case PROP_ID(PR_EC_STATS_SESSION_LOCKED):
  414. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_b;
  415. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  416. lpsRowSet->__ptr[i].__ptr[k].Value.b = iterSD->second.locked;
  417. break;
  418. case PROP_ID(PR_EC_STATS_SESSION_CPU_USER):
  419. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_dbl;
  420. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  421. lpsRowSet->__ptr[i].__ptr[k].Value.dbl = iterSD->second.dblUser;
  422. break;
  423. case PROP_ID(PR_EC_STATS_SESSION_CPU_SYSTEM):
  424. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_dbl;
  425. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  426. lpsRowSet->__ptr[i].__ptr[k].Value.dbl = iterSD->second.dblSystem;
  427. break;
  428. case PROP_ID(PR_EC_STATS_SESSION_CPU_REAL):
  429. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_dbl;
  430. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  431. lpsRowSet->__ptr[i].__ptr[k].Value.dbl = iterSD->second.dblReal;
  432. break;
  433. case PROP_ID(PR_EC_STATS_SESSION_BUSYSTATES):
  434. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_mvszA;
  435. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  436. lpsRowSet->__ptr[i].__ptr[k].Value.mvszA.__size = iterSD->second.busystates.size();
  437. lpsRowSet->__ptr[i].__ptr[k].Value.mvszA.__ptr = s_alloc<char*>(soap, iterSD->second.busystates.size());
  438. j = 0;
  439. for (const auto &bs : iterSD->second.busystates)
  440. lpsRowSet->__ptr[i].__ptr[k].Value.mvszA.__ptr[j++] = s_strcpy(soap, bs.fname);
  441. break;
  442. case PROP_ID(PR_EC_STATS_SESSION_PROCSTATES):
  443. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_mvszA;
  444. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  445. lpsRowSet->__ptr[i].__ptr[k].Value.mvszA.__size = iterSD->second.busystates.size();
  446. lpsRowSet->__ptr[i].__ptr[k].Value.mvszA.__ptr = s_alloc<char*>(soap, iterSD->second.busystates.size());
  447. j = 0;
  448. for (const auto &bs : iterSD->second.busystates) {
  449. const char *szState = "";
  450. if (bs.state == SESSION_STATE_PROCESSING)
  451. szState = "P";
  452. else if (bs.state == SESSION_STATE_SENDING)
  453. szState = "S";
  454. else
  455. assert(false);
  456. lpsRowSet->__ptr[i].__ptr[k].Value.mvszA.__ptr[j++] = s_strcpy(soap, szState);
  457. }
  458. break;
  459. case PROP_ID(PR_EC_STATS_SESSION_REQUESTS):
  460. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_ul;
  461. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  462. lpsRowSet->__ptr[i].__ptr[k].Value.ul = iterSD->second.requests;
  463. break;
  464. case PROP_ID(PR_EC_STATS_SESSION_CLIENT_APPLICATION_VERSION):
  465. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  466. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  467. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_strcpy(soap, iterSD->second.client_application_version.c_str());
  468. break;
  469. case PROP_ID(PR_EC_STATS_SESSION_CLIENT_APPLICATION_MISC):
  470. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  471. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  472. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_strcpy(soap, iterSD->second.client_application_misc.c_str());
  473. break;
  474. }
  475. }
  476. ++i;
  477. }
  478. *lppRowSet = lpsRowSet;
  479. return erSuccess;
  480. }
  481. /*
  482. User stats
  483. */
  484. ECUserStatsTable::ECUserStatsTable(ECSession *lpSession, unsigned int ulFlags, const ECLocale &locale) : ECGenericObjectTable(lpSession, MAPI_STATUS, ulFlags, locale)
  485. {
  486. m_lpfnQueryRowData = QueryRowData;
  487. }
  488. ECRESULT ECUserStatsTable::Create(ECSession *lpSession, unsigned int ulFlags,
  489. const ECLocale &locale, ECGenericObjectTable **lppTable)
  490. {
  491. *lppTable = new(std::nothrow) ECUserStatsTable(lpSession, ulFlags, locale);
  492. if (*lppTable == nullptr)
  493. return KCERR_NOT_ENOUGH_MEMORY;
  494. (*lppTable)->AddRef();
  495. return erSuccess;
  496. }
  497. ECRESULT ECUserStatsTable::Load()
  498. {
  499. ECRESULT er = erSuccess;
  500. std::unique_ptr<std::list<localobjectdetails_t> > lpCompanies;
  501. // load all active and non-active users
  502. // FIXME: group/company quota already possible?
  503. // get company list if hosted and is sysadmin
  504. er = lpSession->GetSecurity()->GetViewableCompanyIds(0, &unique_tie(lpCompanies));
  505. if (er != erSuccess)
  506. return er;
  507. if (lpCompanies->empty()) {
  508. er = LoadCompanyUsers(0);
  509. if (er != erSuccess)
  510. return er;
  511. } else {
  512. for (const auto &com : *lpCompanies) {
  513. er = LoadCompanyUsers(com.ulId);
  514. if (er != erSuccess)
  515. return er;
  516. }
  517. }
  518. return erSuccess;
  519. }
  520. ECRESULT ECUserStatsTable::LoadCompanyUsers(ULONG ulCompanyId)
  521. {
  522. ECRESULT er = erSuccess;
  523. std::unique_ptr<std::list<localobjectdetails_t> > lpObjects;
  524. sObjectTableKey sRowItem;
  525. ECUserManagement *lpUserManagement = lpSession->GetUserManagement();
  526. bool bDistrib = lpSession->GetSessionManager()->IsDistributedSupported();
  527. const char* server = lpSession->GetSessionManager()->GetConfig()->GetSetting("server_name");
  528. std::list<unsigned int> lstObjId;
  529. er = lpUserManagement->GetCompanyObjectListAndSync(OBJECTCLASS_USER,
  530. ulCompanyId, &unique_tie(lpObjects), 0);
  531. if (FAILED(er))
  532. return er;
  533. for (const auto &obj : *lpObjects) {
  534. // we only return users present on this server
  535. if (bDistrib && obj.GetPropString(OB_PROP_S_SERVERNAME).compare(server) != 0)
  536. continue;
  537. lstObjId.push_back(obj.ulId);
  538. }
  539. UpdateRows(ECKeyTable::TABLE_ROW_ADD, &lstObjId, 0, false);
  540. return erSuccess;
  541. }
  542. ECRESULT ECUserStatsTable::QueryRowData(ECGenericObjectTable *lpThis, struct soap *soap, ECSession *lpSession, ECObjectTableList *lpRowList, struct propTagArray *lpsPropTagArray, void *lpObjectData, struct rowSet **lppRowSet, bool bCacheTableData, bool bTableLimit)
  543. {
  544. ECRESULT er;
  545. gsoap_size_t i;
  546. struct rowSet *lpsRowSet = NULL;
  547. ECUserManagement *lpUserManagement = lpSession->GetUserManagement();
  548. ECDatabase *lpDatabase = NULL;
  549. long long llStoreSize = 0;
  550. objectdetails_t objectDetails;
  551. objectdetails_t companyDetails;
  552. quotadetails_t quotaDetails;
  553. bool bNoObjectDetails = false;
  554. bool bNoQuotaDetails = false;
  555. std::string strData;
  556. DB_ROW lpDBRow = NULL;
  557. DB_RESULT lpDBResult;
  558. std::string strQuery;
  559. er = lpSession->GetDatabase(&lpDatabase);
  560. if (er != erSuccess)
  561. return er;
  562. lpsRowSet = s_alloc<rowSet>(soap);
  563. lpsRowSet->__size = 0;
  564. lpsRowSet->__ptr = NULL;
  565. if (lpRowList->empty()) {
  566. *lppRowSet = lpsRowSet;
  567. return erSuccess;
  568. }
  569. // We return a square array with all the values
  570. lpsRowSet->__size = lpRowList->size();
  571. lpsRowSet->__ptr = s_alloc<propValArray>(soap, lpsRowSet->__size);
  572. memset(lpsRowSet->__ptr, 0, sizeof(propValArray) * lpsRowSet->__size);
  573. // Allocate memory for all rows
  574. for (i = 0; i < lpsRowSet->__size; ++i) {
  575. lpsRowSet->__ptr[i].__size = lpsPropTagArray->__size;
  576. lpsRowSet->__ptr[i].__ptr = s_alloc<propVal>(soap, lpsPropTagArray->__size);
  577. memset(lpsRowSet->__ptr[i].__ptr, 0, sizeof(propVal) * lpsPropTagArray->__size);
  578. }
  579. i = 0;
  580. for (const auto &row : *lpRowList) {
  581. bNoObjectDetails = bNoQuotaDetails = false;
  582. if (lpUserManagement->GetObjectDetails(row.ulObjId, &objectDetails) != erSuccess)
  583. // user gone missing since first list, all props should be set to ignore
  584. bNoObjectDetails = bNoQuotaDetails = true;
  585. else if (lpSession->GetSecurity()->GetUserQuota(row.ulObjId, false, &quotaDetails) != erSuccess)
  586. // user gone missing since last call, all quota props should be set to ignore
  587. bNoQuotaDetails = true;
  588. if (lpSession->GetSecurity()->GetUserSize(row.ulObjId, &llStoreSize) != erSuccess)
  589. llStoreSize = 0;
  590. for (gsoap_size_t k = 0; k < lpsPropTagArray->__size; ++k) {
  591. // default is error prop
  592. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = PROP_TAG(PROP_TYPE(PT_ERROR), PROP_ID(lpsPropTagArray->__ptr[k]));
  593. lpsRowSet->__ptr[i].__ptr[k].Value.ul = KCERR_NOT_FOUND;
  594. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_ul;
  595. switch (PROP_ID(lpsPropTagArray->__ptr[k])) {
  596. case PROP_ID(PR_INSTANCE_KEY):
  597. // generate key
  598. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_bin;
  599. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  600. lpsRowSet->__ptr[i].__ptr[k].Value.bin = s_alloc<xsd__base64Binary>(soap);
  601. lpsRowSet->__ptr[i].__ptr[k].Value.bin->__size = sizeof(sObjectTableKey);
  602. lpsRowSet->__ptr[i].__ptr[k].Value.bin->__ptr = s_alloc<unsigned char>(soap, sizeof(sObjectTableKey));
  603. memcpy(lpsRowSet->__ptr[i].__ptr[k].Value.bin->__ptr, &row, sizeof(sObjectTableKey));
  604. break;
  605. case PROP_ID(PR_EC_COMPANY_NAME):
  606. if (!bNoObjectDetails && lpUserManagement->GetObjectDetails(objectDetails.GetPropInt(OB_PROP_I_COMPANYID), &companyDetails) == erSuccess) {
  607. strData = companyDetails.GetPropString(OB_PROP_S_FULLNAME);
  608. // do we have a default copy function??
  609. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  610. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  611. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_alloc<char>(soap, strData.length()+1);
  612. memcpy(lpsRowSet->__ptr[i].__ptr[k].Value.lpszA, strData.data(), strData.length()+1);
  613. }
  614. break;
  615. case PROP_ID(PR_EC_USERNAME):
  616. if (!bNoObjectDetails) {
  617. strData = objectDetails.GetPropString(OB_PROP_S_LOGIN);
  618. // do we have a default copy function??
  619. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  620. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  621. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_alloc<char>(soap, strData.length()+1);
  622. memcpy(lpsRowSet->__ptr[i].__ptr[k].Value.lpszA, strData.data(), strData.length()+1);
  623. }
  624. break;
  625. case PROP_ID(PR_DISPLAY_NAME):
  626. if (!bNoObjectDetails) {
  627. strData = objectDetails.GetPropString(OB_PROP_S_FULLNAME);
  628. // do we have a default copy function??
  629. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  630. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  631. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_alloc<char>(soap, strData.length()+1);
  632. memcpy(lpsRowSet->__ptr[i].__ptr[k].Value.lpszA, strData.data(), strData.length()+1);
  633. }
  634. break;
  635. case PROP_ID(PR_SMTP_ADDRESS):
  636. if (!bNoObjectDetails) {
  637. strData = objectDetails.GetPropString(OB_PROP_S_EMAIL);
  638. // do we have a default copy function??
  639. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  640. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  641. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_alloc<char>(soap, strData.length()+1);
  642. memcpy(lpsRowSet->__ptr[i].__ptr[k].Value.lpszA, strData.data(), strData.length()+1);
  643. }
  644. break;
  645. case PROP_ID(PR_EC_NONACTIVE):
  646. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_b;
  647. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  648. lpsRowSet->__ptr[i].__ptr[k].Value.b =
  649. (OBJECTCLASS_TYPE(objectDetails.GetClass()) == OBJECTTYPE_MAILUSER) &&
  650. (objectDetails.GetClass() != ACTIVE_USER);
  651. break;
  652. case PROP_ID(PR_EC_ADMINISTRATOR):
  653. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_ul;
  654. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  655. lpsRowSet->__ptr[i].__ptr[k].Value.ul = objectDetails.GetPropInt(OB_PROP_I_ADMINLEVEL);
  656. break;
  657. case PROP_ID(PR_EC_HOMESERVER_NAME):
  658. // should always be this servername, see ::Load()
  659. if (!bNoObjectDetails && lpSession->GetSessionManager()->IsDistributedSupported()) {
  660. strData = objectDetails.GetPropString(OB_PROP_S_SERVERNAME);
  661. // do we have a default copy function??
  662. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  663. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  664. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_alloc<char>(soap, strData.length()+1);
  665. memcpy(lpsRowSet->__ptr[i].__ptr[k].Value.lpszA, strData.data(), strData.length()+1);
  666. }
  667. break;
  668. case PROP_ID(PR_MESSAGE_SIZE_EXTENDED):
  669. if (llStoreSize > 0) {
  670. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_li;
  671. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  672. lpsRowSet->__ptr[i].__ptr[k].Value.li = llStoreSize;
  673. }
  674. break;
  675. case PROP_ID(PR_QUOTA_WARNING_THRESHOLD):
  676. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_ul;
  677. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  678. lpsRowSet->__ptr[i].__ptr[k].Value.ul = quotaDetails.llWarnSize / 1024;
  679. break;
  680. case PROP_ID(PR_QUOTA_SEND_THRESHOLD):
  681. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_ul;
  682. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  683. lpsRowSet->__ptr[i].__ptr[k].Value.ul = quotaDetails.llSoftSize / 1024;
  684. break;
  685. case PROP_ID(PR_QUOTA_RECEIVE_THRESHOLD):
  686. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_ul;
  687. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  688. lpsRowSet->__ptr[i].__ptr[k].Value.ul = quotaDetails.llHardSize / 1024;
  689. break;
  690. case PROP_ID(PR_LAST_LOGON_TIME):
  691. case PROP_ID(PR_LAST_LOGOFF_TIME):
  692. case PROP_ID(PR_EC_QUOTA_MAIL_TIME):
  693. // last mail time ... property in the store of the user...
  694. strQuery = "SELECT val_hi, val_lo FROM properties JOIN hierarchy ON properties.hierarchyid=hierarchy.id JOIN stores ON hierarchy.id=stores.hierarchy_id WHERE stores.user_id=" +
  695. stringify(row.ulObjId) + " AND properties.tag=" +
  696. stringify(PROP_ID(lpsPropTagArray->__ptr[k])) +
  697. " AND properties.type=" +
  698. stringify(PROP_TYPE(lpsPropTagArray->__ptr[k]));
  699. er = lpDatabase->DoSelect(strQuery, &lpDBResult);
  700. if (er != erSuccess) {
  701. // database error .. ignore for now
  702. er = erSuccess;
  703. break;
  704. }
  705. if (lpDatabase->GetNumRows(lpDBResult) > 0) {
  706. lpDBRow = lpDatabase->FetchRow(lpDBResult);
  707. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_hilo;
  708. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  709. lpsRowSet->__ptr[i].__ptr[k].Value.hilo = s_alloc<hiloLong>(soap);
  710. lpsRowSet->__ptr[i].__ptr[k].Value.hilo->hi = atoi(lpDBRow[0]);
  711. lpsRowSet->__ptr[i].__ptr[k].Value.hilo->lo = atoi(lpDBRow[1]);
  712. }
  713. break;
  714. case PROP_ID(PR_EC_OUTOFOFFICE):
  715. strQuery = "SELECT val_ulong FROM properties JOIN stores ON properties.hierarchyid=stores.hierarchy_id WHERE stores.user_id=" +
  716. stringify(row.ulObjId) +
  717. " AND properties.tag=" +
  718. stringify(PROP_ID(PR_EC_OUTOFOFFICE)) +
  719. " AND properties.type=" +
  720. stringify(PROP_TYPE(PR_EC_OUTOFOFFICE));
  721. er = lpDatabase->DoSelect(strQuery, &lpDBResult);
  722. if (er != erSuccess) {
  723. // database error .. ignore for now
  724. er = erSuccess;
  725. break;
  726. }
  727. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_b;
  728. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  729. if (lpDatabase->GetNumRows(lpDBResult) > 0) {
  730. lpDBRow = lpDatabase->FetchRow(lpDBResult);
  731. lpsRowSet->__ptr[i].__ptr[k].Value.b = atoi(lpDBRow[0]);
  732. } else {
  733. lpsRowSet->__ptr[i].__ptr[k].Value.b = 0;
  734. }
  735. break;
  736. };
  737. }
  738. ++i;
  739. }
  740. *lppRowSet = lpsRowSet;
  741. return er;
  742. }
  743. ECCompanyStatsTable::ECCompanyStatsTable(ECSession *lpSession, unsigned int ulFlags, const ECLocale &locale) : ECGenericObjectTable(lpSession, MAPI_STATUS, ulFlags, locale)
  744. {
  745. m_lpfnQueryRowData = QueryRowData;
  746. m_lpObjectData = this;
  747. }
  748. ECRESULT ECCompanyStatsTable::Create(ECSession *lpSession, unsigned int ulFlags,
  749. const ECLocale &locale, ECGenericObjectTable **lppTable)
  750. {
  751. *lppTable = new(std::nothrow) ECCompanyStatsTable(lpSession, ulFlags, locale);
  752. if (*lppTable == nullptr)
  753. return KCERR_NOT_ENOUGH_MEMORY;
  754. (*lppTable)->AddRef();
  755. return erSuccess;
  756. }
  757. ECRESULT ECCompanyStatsTable::Load()
  758. {
  759. ECRESULT er = erSuccess;
  760. std::unique_ptr<std::list<localobjectdetails_t> > lpCompanies;
  761. sObjectTableKey sRowItem;
  762. er = lpSession->GetSecurity()->GetViewableCompanyIds(0, &unique_tie(lpCompanies));
  763. if (er != erSuccess)
  764. return er;
  765. for (const auto &com : *lpCompanies)
  766. UpdateRow(ECKeyTable::TABLE_ROW_ADD, com.ulId, 0);
  767. return erSuccess;
  768. }
  769. ECRESULT ECCompanyStatsTable::QueryRowData(ECGenericObjectTable *lpThis, struct soap *soap, ECSession *lpSession, ECObjectTableList* lpRowList, struct propTagArray *lpsPropTagArray, void* lpObjectData, struct rowSet **lppRowSet, bool bCacheTableData, bool bTableLimit)
  770. {
  771. ECRESULT er;
  772. gsoap_size_t i;
  773. struct rowSet *lpsRowSet = NULL;
  774. ECUserManagement *lpUserManagement = lpSession->GetUserManagement();
  775. ECDatabase *lpDatabase = NULL;
  776. long long llStoreSize = 0;
  777. objectdetails_t companyDetails;
  778. quotadetails_t quotaDetails;
  779. bool bNoCompanyDetails = false;
  780. bool bNoQuotaDetails = false;
  781. std::string strData;
  782. DB_ROW lpDBRow = NULL;
  783. DB_RESULT lpDBResult;
  784. std::string strQuery;
  785. er = lpSession->GetDatabase(&lpDatabase);
  786. if (er != erSuccess)
  787. return er;
  788. lpsRowSet = s_alloc<rowSet>(soap);
  789. lpsRowSet->__size = 0;
  790. lpsRowSet->__ptr = NULL;
  791. if (lpRowList->empty()) {
  792. *lppRowSet = lpsRowSet;
  793. return erSuccess;
  794. }
  795. // We return a square array with all the values
  796. lpsRowSet->__size = lpRowList->size();
  797. lpsRowSet->__ptr = s_alloc<propValArray>(soap, lpsRowSet->__size);
  798. memset(lpsRowSet->__ptr, 0, sizeof(propValArray) * lpsRowSet->__size);
  799. // Allocate memory for all rows
  800. for (i = 0; i < lpsRowSet->__size; ++i) {
  801. lpsRowSet->__ptr[i].__size = lpsPropTagArray->__size;
  802. lpsRowSet->__ptr[i].__ptr = s_alloc<propVal>(soap, lpsPropTagArray->__size);
  803. memset(lpsRowSet->__ptr[i].__ptr, 0, sizeof(propVal) * lpsPropTagArray->__size);
  804. }
  805. i = 0;
  806. for (const auto &row : *lpRowList) {
  807. bNoCompanyDetails = bNoQuotaDetails = false;
  808. if (lpUserManagement->GetObjectDetails(row.ulObjId, &companyDetails) != erSuccess)
  809. bNoCompanyDetails = true;
  810. else if (lpUserManagement->GetQuotaDetailsAndSync(row.ulObjId, &quotaDetails) != erSuccess)
  811. // company gone missing since last call, all quota props should be set to ignore
  812. bNoQuotaDetails = true;
  813. if (lpSession->GetSecurity()->GetUserSize(row.ulObjId, &llStoreSize) != erSuccess)
  814. llStoreSize = 0;
  815. for (gsoap_size_t k = 0; k < lpsPropTagArray->__size; ++k) {
  816. // default is error prop
  817. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = PROP_TAG(PROP_TYPE(PT_ERROR), PROP_ID(lpsPropTagArray->__ptr[k]));
  818. lpsRowSet->__ptr[i].__ptr[k].Value.ul = KCERR_NOT_FOUND;
  819. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_ul;
  820. switch (PROP_ID(lpsPropTagArray->__ptr[k])) {
  821. case PROP_ID(PR_INSTANCE_KEY):
  822. // generate key
  823. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_bin;
  824. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  825. lpsRowSet->__ptr[i].__ptr[k].Value.bin = s_alloc<xsd__base64Binary>(soap);
  826. lpsRowSet->__ptr[i].__ptr[k].Value.bin->__size = sizeof(sObjectTableKey);
  827. lpsRowSet->__ptr[i].__ptr[k].Value.bin->__ptr = s_alloc<unsigned char>(soap, sizeof(sObjectTableKey));
  828. memcpy(lpsRowSet->__ptr[i].__ptr[k].Value.bin->__ptr, &row, sizeof(sObjectTableKey));
  829. break;
  830. case PROP_ID(PR_EC_COMPANY_NAME):
  831. if (!bNoCompanyDetails) {
  832. strData = companyDetails.GetPropString(OB_PROP_S_FULLNAME);
  833. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  834. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  835. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_strcpy(soap, strData.data());
  836. }
  837. break;
  838. case PROP_ID(PR_EC_COMPANY_ADMIN):
  839. strData = companyDetails.GetPropObject(OB_PROP_O_SYSADMIN).id;
  840. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  841. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  842. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_strcpy(soap, strData.data());
  843. break;
  844. case PROP_ID(PR_MESSAGE_SIZE_EXTENDED):
  845. if (llStoreSize > 0) {
  846. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_li;
  847. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  848. lpsRowSet->__ptr[i].__ptr[k].Value.li = llStoreSize;
  849. }
  850. break;
  851. case PROP_ID(PR_QUOTA_WARNING_THRESHOLD):
  852. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_li;
  853. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k]; // set type to I8 ?
  854. lpsRowSet->__ptr[i].__ptr[k].Value.li = quotaDetails.llWarnSize;
  855. break;
  856. case PROP_ID(PR_QUOTA_SEND_THRESHOLD):
  857. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_li;
  858. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  859. lpsRowSet->__ptr[i].__ptr[k].Value.li = quotaDetails.llSoftSize;
  860. break;
  861. case PROP_ID(PR_QUOTA_RECEIVE_THRESHOLD):
  862. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_li;
  863. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  864. lpsRowSet->__ptr[i].__ptr[k].Value.li = quotaDetails.llHardSize;
  865. break;
  866. case PROP_ID(PR_EC_QUOTA_MAIL_TIME):
  867. // last mail time ... property in the store of the company (=public)...
  868. strQuery = "SELECT val_hi, val_lo FROM properties JOIN hierarchy ON properties.hierarchyid=hierarchy.id JOIN stores ON hierarchy.id=stores.hierarchy_id WHERE stores.user_id=" +
  869. stringify(row.ulObjId) +
  870. " AND properties.tag=" +
  871. stringify(PROP_ID(PR_EC_QUOTA_MAIL_TIME)) +
  872. " AND properties.type=" +
  873. stringify(PROP_TYPE(PR_EC_QUOTA_MAIL_TIME));
  874. er = lpDatabase->DoSelect(strQuery, &lpDBResult);
  875. if (er != erSuccess) {
  876. // database error .. ignore for now
  877. er = erSuccess;
  878. break;
  879. }
  880. if (lpDatabase->GetNumRows(lpDBResult) > 0) {
  881. lpDBRow = lpDatabase->FetchRow(lpDBResult);
  882. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_hilo;
  883. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  884. lpsRowSet->__ptr[i].__ptr[k].Value.hilo = s_alloc<hiloLong>(soap);
  885. lpsRowSet->__ptr[i].__ptr[k].Value.hilo->hi = atoi(lpDBRow[0]);
  886. lpsRowSet->__ptr[i].__ptr[k].Value.hilo->lo = atoi(lpDBRow[1]);
  887. }
  888. break;
  889. };
  890. }
  891. ++i;
  892. }
  893. *lppRowSet = lpsRowSet;
  894. return er;
  895. }
  896. ECServerStatsTable::ECServerStatsTable(ECSession *lpSession, unsigned int ulFlags, const ECLocale &locale) : ECGenericObjectTable(lpSession, MAPI_STATUS, ulFlags, locale)
  897. {
  898. m_lpfnQueryRowData = QueryRowData;
  899. m_lpObjectData = this;
  900. }
  901. ECRESULT ECServerStatsTable::Create(ECSession *lpSession, unsigned int ulFlags,
  902. const ECLocale &locale, ECGenericObjectTable **lppTable)
  903. {
  904. *lppTable = new(std::nothrow) ECServerStatsTable(lpSession, ulFlags, locale);
  905. if (*lppTable == nullptr)
  906. return KCERR_NOT_ENOUGH_MEMORY;
  907. (*lppTable)->AddRef();
  908. return erSuccess;
  909. }
  910. ECRESULT ECServerStatsTable::Load()
  911. {
  912. ECRESULT er;
  913. sObjectTableKey sRowItem;
  914. serverlist_t servers;
  915. unsigned int i = 1;
  916. er = lpSession->GetUserManagement()->GetServerList(&servers);
  917. if (er != erSuccess)
  918. return er;
  919. // Assign an ID to each server which is usable from QueryRowData
  920. for (const auto &srv : servers) {
  921. m_mapServers.insert(std::make_pair(i, srv));
  922. // For each server, add a row in the table
  923. UpdateRow(ECKeyTable::TABLE_ROW_ADD, i, 0);
  924. ++i;
  925. }
  926. return erSuccess;
  927. }
  928. ECRESULT ECServerStatsTable::QueryRowData(ECGenericObjectTable *lpThis, struct soap *soap, ECSession *lpSession, ECObjectTableList* lpRowList, struct propTagArray *lpsPropTagArray, void* lpObjectData, struct rowSet **lppRowSet, bool bCacheTableData, bool bTableLimit)
  929. {
  930. gsoap_size_t i;
  931. struct rowSet *lpsRowSet = NULL;
  932. ECUserManagement *lpUserManagement = lpSession->GetUserManagement();
  933. serverdetails_t details;
  934. auto lpStats = static_cast<ECServerStatsTable *>(lpThis);
  935. lpsRowSet = s_alloc<rowSet>(soap);
  936. lpsRowSet->__size = 0;
  937. lpsRowSet->__ptr = NULL;
  938. if (lpRowList->empty()) {
  939. *lppRowSet = lpsRowSet;
  940. return erSuccess;
  941. }
  942. // We return a square array with all the values
  943. lpsRowSet->__size = lpRowList->size();
  944. lpsRowSet->__ptr = s_alloc<propValArray>(soap, lpsRowSet->__size);
  945. memset(lpsRowSet->__ptr, 0, sizeof(propValArray) * lpsRowSet->__size);
  946. // Allocate memory for all rows
  947. for (i = 0; i < lpsRowSet->__size; ++i) {
  948. lpsRowSet->__ptr[i].__size = lpsPropTagArray->__size;
  949. lpsRowSet->__ptr[i].__ptr = s_alloc<propVal>(soap, lpsPropTagArray->__size);
  950. memset(lpsRowSet->__ptr[i].__ptr, 0, sizeof(propVal) * lpsPropTagArray->__size);
  951. }
  952. i = 0;
  953. for (const auto &row : *lpRowList) {
  954. if (lpUserManagement->GetServerDetails(lpStats->m_mapServers[row.ulObjId], &details) != erSuccess)
  955. details = serverdetails_t();
  956. for (gsoap_size_t k = 0; k < lpsPropTagArray->__size; ++k) {
  957. // default is error prop
  958. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = PROP_TAG(PROP_TYPE(PT_ERROR), PROP_ID(lpsPropTagArray->__ptr[k]));
  959. lpsRowSet->__ptr[i].__ptr[k].Value.ul = KCERR_NOT_FOUND;
  960. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_ul;
  961. switch (PROP_ID(lpsPropTagArray->__ptr[k])) {
  962. case PROP_ID(PR_INSTANCE_KEY):
  963. // generate key
  964. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_bin;
  965. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  966. lpsRowSet->__ptr[i].__ptr[k].Value.bin = s_alloc<xsd__base64Binary>(soap);
  967. lpsRowSet->__ptr[i].__ptr[k].Value.bin->__size = sizeof(sObjectTableKey);
  968. lpsRowSet->__ptr[i].__ptr[k].Value.bin->__ptr = s_alloc<unsigned char>(soap, sizeof(sObjectTableKey));
  969. memcpy(lpsRowSet->__ptr[i].__ptr[k].Value.bin->__ptr, &row, sizeof(sObjectTableKey));
  970. break;
  971. case PROP_ID(PR_EC_STATS_SERVER_NAME):
  972. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  973. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  974. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_strcpy(soap, lpStats->m_mapServers[row.ulObjId].c_str());
  975. break;
  976. case PROP_ID(PR_EC_STATS_SERVER_HTTPPORT):
  977. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_ul;
  978. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  979. lpsRowSet->__ptr[i].__ptr[k].Value.ul = details.GetHttpPort();
  980. break;
  981. case PROP_ID(PR_EC_STATS_SERVER_SSLPORT):
  982. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_ul;
  983. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  984. lpsRowSet->__ptr[i].__ptr[k].Value.ul = details.GetSslPort();
  985. break;
  986. case PROP_ID(PR_EC_STATS_SERVER_HOST):
  987. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  988. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  989. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_strcpy(soap, details.GetHostAddress().c_str());
  990. break;
  991. case PROP_ID(PR_EC_STATS_SERVER_PROXYURL):
  992. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  993. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  994. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_strcpy(soap, details.GetProxyPath().c_str());
  995. break;
  996. case PROP_ID(PR_EC_STATS_SERVER_HTTPURL):
  997. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  998. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  999. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_strcpy(soap, details.GetHttpPath().c_str());
  1000. break;
  1001. case PROP_ID(PR_EC_STATS_SERVER_HTTPSURL):
  1002. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  1003. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  1004. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_strcpy(soap, details.GetSslPath().c_str());
  1005. break;
  1006. case PROP_ID(PR_EC_STATS_SERVER_FILEURL):
  1007. lpsRowSet->__ptr[i].__ptr[k].__union = SOAP_UNION_propValData_lpszA;
  1008. lpsRowSet->__ptr[i].__ptr[k].ulPropTag = lpsPropTagArray->__ptr[k];
  1009. lpsRowSet->__ptr[i].__ptr[k].Value.lpszA = s_strcpy(soap, details.GetFilePath().c_str());
  1010. break;
  1011. };
  1012. }
  1013. ++i;
  1014. }
  1015. *lppRowSet = lpsRowSet;
  1016. return erSuccess;
  1017. }
  1018. } /* namespace */