ECUserManagement.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. #ifndef ECUSERMANAGEMENT_H
  18. #define ECUSERMANAGEMENT_H
  19. #include <kopano/zcdefs.h>
  20. #include <list>
  21. #include <map>
  22. #include <mutex>
  23. #include <ctime>
  24. #include <kopano/kcodes.h>
  25. #include <kopano/pcuser.hpp>
  26. #include <kopano/ECConfig.h>
  27. #include "ECSession.h"
  28. #include <kopano/ECLogger.h>
  29. #include <kopano/ECDefs.h>
  30. #include "plugin.h"
  31. struct soap;
  32. namespace KC {
  33. class localobjectdetails_t _kc_final : public objectdetails_t {
  34. public:
  35. localobjectdetails_t(void) = default;
  36. localobjectdetails_t(unsigned int id, objectclass_t objclass) : objectdetails_t(objclass), ulId(id) {};
  37. localobjectdetails_t(unsigned int id, const objectdetails_t &details) : objectdetails_t(details), ulId(id) {};
  38. bool operator==(const localobjectdetails_t &obj) const { return ulId == obj.ulId; };
  39. bool operator<(const localobjectdetails_t &obj) const { return ulId < obj.ulId; } ;
  40. unsigned int ulId = 0;
  41. };
  42. class usercount_t _kc_final {
  43. public:
  44. enum ucIndex {
  45. ucActiveUser = 0,
  46. ucNonActiveUser,
  47. ucRoom,
  48. ucEquipment,
  49. ucContact,
  50. ucNonActiveTotal, // Must be right before ucMAX
  51. ucMAX = ucNonActiveTotal // Must be very last
  52. };
  53. usercount_t(void)
  54. {
  55. memset(m_ulCounts, 0, sizeof(m_ulCounts));
  56. }
  57. usercount_t(unsigned int ulActiveUser, unsigned int ulNonActiveUser, unsigned int ulRoom, unsigned int ulEquipment, unsigned int ulContact): m_bValid(true) {
  58. m_ulCounts[ucActiveUser] = ulActiveUser;
  59. m_ulCounts[ucNonActiveUser] = ulNonActiveUser;
  60. m_ulCounts[ucRoom] = ulRoom;
  61. m_ulCounts[ucEquipment] = ulEquipment;
  62. m_ulCounts[ucContact] = ulContact;
  63. }
  64. usercount_t(const usercount_t &other): m_bValid(other.m_bValid) {
  65. memcpy(m_ulCounts, other.m_ulCounts, sizeof(m_ulCounts));
  66. }
  67. void swap(usercount_t &other) {
  68. std::swap(m_bValid, other.m_bValid);
  69. for (unsigned i = 0; i < ucMAX; ++i)
  70. std::swap(m_ulCounts[i], other.m_ulCounts[i]);
  71. }
  72. void assign(unsigned int ulActiveUser, unsigned int ulNonActiveUser, unsigned int ulRoom, unsigned int ulEquipment, unsigned int ulContact) {
  73. usercount_t tmp(ulActiveUser, ulNonActiveUser, ulRoom, ulEquipment, ulContact);
  74. swap(tmp);
  75. }
  76. void assign(const usercount_t &other) {
  77. if (&other != this) {
  78. usercount_t tmp(other);
  79. swap(tmp);
  80. }
  81. }
  82. usercount_t& operator=(const usercount_t &other) {
  83. assign(other);
  84. return *this;
  85. }
  86. bool isValid() const {
  87. return m_bValid;
  88. }
  89. void set(ucIndex index, unsigned int ulValue) {
  90. if (index != ucNonActiveTotal) {
  91. assert(index >= 0 && index < ucMAX);
  92. m_ulCounts[index] = ulValue;
  93. m_bValid = true;
  94. }
  95. }
  96. unsigned int operator[](ucIndex index) const {
  97. if (index == ucNonActiveTotal)
  98. return m_ulCounts[ucNonActiveUser] + m_ulCounts[ucRoom] + m_ulCounts[ucEquipment]; // Contacts don't count for non-active stores.
  99. assert(index >= 0 && index < ucMAX);
  100. return m_ulCounts[index];
  101. }
  102. private:
  103. bool m_bValid = false;
  104. unsigned int m_ulCounts[ucMAX];
  105. };
  106. // Use for ulFlags
  107. #define USERMANAGEMENT_IDS_ONLY 0x1 // Return only local userID (the ulId field). 'details' is undefined in this case
  108. #define USERMANAGEMENT_ADDRESSBOOK 0x2 // Return only objects which should be visible in the address book
  109. #define USERMANAGEMENT_FORCE_SYNC 0x4 // Force sync with external database
  110. #define USERMANAGEMENT_SHOW_HIDDEN 0x8 // Show hidden entries
  111. // Use for ulLicenseStatus in CheckUserLicense()
  112. #define USERMANAGEMENT_LIMIT_ACTIVE_USERS 0x1 /* Limit reached, but not yet exceeded */
  113. #define USERMANAGEMENT_LIMIT_NONACTIVE_USERS 0x2 /* Limit reached, but not yet exceeded */
  114. #define USERMANAGEMENT_EXCEED_ACTIVE_USERS 0x4 /* Limit exceeded */
  115. #define USERMANAGEMENT_EXCEED_NONACTIVE_USERS 0x8 /* Limit exceeded */
  116. #define USERMANAGEMENT_BLOCK_CREATE_ACTIVE_USER ( USERMANAGEMENT_LIMIT_ACTIVE_USERS | USERMANAGEMENT_EXCEED_ACTIVE_USERS )
  117. #define USERMANAGEMENT_BLOCK_CREATE_NONACTIVE_USER ( USERMANAGEMENT_LIMIT_NONACTIVE_USERS | USERMANAGEMENT_EXCEED_NONACTIVE_USERS )
  118. #define USERMANAGEMENT_USER_LICENSE_EXCEEDED ( USERMANAGEMENT_EXCEED_ACTIVE_USERS | USERMANAGEMENT_EXCEED_NONACTIVE_USERS )
  119. class _kc_export ECUserManagement _kc_final {
  120. public:
  121. _kc_hidden ECUserManagement(BTSession *, ECPluginFactory *, ECConfig *);
  122. _kc_hidden virtual ~ECUserManagement(void) _kc_impdtor;
  123. // Authenticate a user
  124. _kc_hidden virtual ECRESULT AuthUserAndSync(const char *user, const char *pass, unsigned int *user_id);
  125. // Get data for an object, with on-the-fly delete of the specified object id
  126. virtual ECRESULT GetObjectDetails(unsigned int obj_id, objectdetails_t *ret);
  127. // Get quota details for a user object
  128. _kc_hidden virtual ECRESULT GetQuotaDetailsAndSync(unsigned int obj_id, quotadetails_t *ret, bool get_user_default = false);
  129. // Set quota details for a user object
  130. _kc_hidden virtual ECRESULT SetQuotaDetailsAndSync(unsigned int obj_id, const quotadetails_t &);
  131. // Get (typed) objectlist for company, or list of all companies, with on-the-fly delete/create of users and groups
  132. _kc_hidden virtual ECRESULT GetCompanyObjectListAndSync(objectclass_t, unsigned int company_id, std::list<localobjectdetails_t> **objs, unsigned int flags = 0);
  133. // Get subobjects in an object, with on-the-fly delete of the specified parent object
  134. _kc_hidden virtual ECRESULT GetSubObjectsOfObjectAndSync(userobject_relation_t, unsigned int parent_id, std::list<localobjectdetails_t> **objs, unsigned int flags = 0);
  135. // Get parent to which an object belongs, with on-the-fly delete of the specified child object id
  136. _kc_hidden virtual ECRESULT GetParentObjectsOfObjectAndSync(userobject_relation_t, unsigned int child_id, std::list<localobjectdetails_t> **groups, unsigned int flags = 0);
  137. // Set data for a single user, with on-the-fly delete of the specified user id
  138. _kc_hidden virtual ECRESULT SetObjectDetailsAndSync(unsigned int obj_id, const objectdetails_t &, std::list<std::string> *remove_props);
  139. // Add a member to a group, with on-the-fly delete of the specified group id
  140. _kc_hidden virtual ECRESULT AddSubObjectToObjectAndSync(userobject_relation_t, unsigned int parent_id, unsigned int child_id);
  141. _kc_hidden virtual ECRESULT DeleteSubObjectFromObjectAndSync(userobject_relation_t, unsigned int parent_id, unsigned int child_id);
  142. // Resolve a user name to a user id, with on-the-fly create of the specified user
  143. _kc_hidden virtual ECRESULT ResolveObjectAndSync(objectclass_t, const char *name, unsigned int *obj_id);
  144. // Get a local object ID for a part of a name
  145. virtual ECRESULT SearchObjectAndSync(const char *search_string, unsigned int flags, unsigned int *id);
  146. // Create an object
  147. _kc_hidden virtual ECRESULT CreateObjectAndSync(const objectdetails_t &, unsigned int *id);
  148. // Delete an object
  149. _kc_hidden virtual ECRESULT DeleteObjectAndSync(unsigned int obj_id);
  150. // Either modify or create an object with a specific object id and type (used for synchronize)
  151. _kc_hidden virtual ECRESULT CreateOrModifyObject(const objectid_t &extern_id, const objectdetails_t &, unsigned int pref_id, std::list<std::string> *remove_props);
  152. // Get MAPI property data for a group or user/group/company id, with on-the-fly delete of the specified user/group/company
  153. _kc_hidden virtual ECRESULT GetProps(struct soap *, unsigned int obj_id, struct propTagArray *, struct propValArray *);
  154. _kc_hidden virtual ECRESULT GetContainerProps(struct soap *, unsigned int obj_id, struct propTagArray *, struct propValArray *);
  155. // Do the same for a whole set of items
  156. _kc_hidden virtual ECRESULT QueryContentsRowData(struct soap *, ECObjectTableList *rowlist, struct propTagArray *, struct rowSet **);
  157. _kc_hidden virtual ECRESULT QueryHierarchyRowData(struct soap *, ECObjectTableList *rowlist, struct propTagArray *, struct rowSet **);
  158. _kc_hidden virtual ECRESULT GetUserCount(unsigned int *active, unsigned int *inactive); // returns active users and non-active users (so you may get ulUsers=3, ulNonActives=5)
  159. _kc_hidden virtual ECRESULT GetUserCount(usercount_t *);
  160. _kc_hidden virtual ECRESULT GetCachedUserCount(usercount_t *);
  161. _kc_hidden virtual ECRESULT GetPublicStoreDetails(objectdetails_t *);
  162. virtual ECRESULT GetServerDetails(const std::string &server, serverdetails_t *);
  163. _kc_hidden virtual ECRESULT GetServerList(serverlist_t *);
  164. /* Check if the user license status */
  165. _kc_hidden ECRESULT CheckUserLicense(unsigned int *licstatus);
  166. // Returns true if ulId is an internal ID (so either SYSTEM or EVERYONE)
  167. bool IsInternalObject(unsigned int ulId);
  168. // Create a v1 based AB SourceKey
  169. _kc_hidden ECRESULT GetABSourceKeyV1(unsigned int user_id, SOURCEKEY *);
  170. // Get userinfo from cache
  171. _kc_hidden ECRESULT GetExternalId(unsigned int di, objectid_t *extern_id, unsigned int *company_id = nullptr, std::string *signature = nullptr);
  172. _kc_hidden ECRESULT GetLocalId(const objectid_t &extern_id, unsigned int *id, std::string *signature = nullptr);
  173. /* calls localid->externid and login->user/company conversions */
  174. _kc_hidden virtual ECRESULT UpdateUserDetailsFromClient(objectdetails_t *);
  175. /* Create an ABEID in version 1 or version 0 */
  176. _kc_hidden ECRESULT CreateABEntryID(struct soap *, unsigned int vers, unsigned int obj_id, unsigned int type, objectid_t *extern_id, gsoap_size_t *eid_size, ABEID **eid);
  177. /* Resync all objects from the plugin. */
  178. _kc_hidden ECRESULT SyncAllObjects(void);
  179. private:
  180. /* Convert a user loginname to username and companyname */
  181. _kc_hidden virtual ECRESULT ConvertLoginToUserAndCompany(objectdetails_t *);
  182. /* Convert username and companyname to loginname */
  183. _kc_hidden virtual ECRESULT ConvertUserAndCompanyToLogin(objectdetails_t *);
  184. /* convert extern IDs to local IDs */
  185. _kc_hidden virtual ECRESULT ConvertExternIDsToLocalIDs(objectdetails_t *);
  186. /* convert local IDs to extern IDs */
  187. _kc_hidden virtual ECRESULT ConvertLocalIDsToExternIDs(objectdetails_t *);
  188. /* calls externid->localid and user/company->login conversions */
  189. _kc_hidden virtual ECRESULT UpdateUserDetailsToClient(objectdetails_t *);
  190. _kc_hidden ECRESULT ComplementDefaultFeatures(objectdetails_t *);
  191. _kc_hidden ECRESULT RemoveDefaultFeatures(objectdetails_t *);
  192. _kc_hidden bool MustHide(/*const*/ ECSecurity &, unsigned int flags, const objectdetails_t &) const;
  193. // Get object details from list
  194. _kc_hidden ECRESULT GetLocalObjectListFromSignatures(const std::list<objectsignature_t> &signatures, const std::map<objectid_t, unsigned int> &extern_to_local, unsigned int flags, std::list<localobjectdetails_t> *);
  195. // Get local details
  196. _kc_hidden ECRESULT GetLocalObjectDetails(unsigned int id, objectdetails_t *);
  197. // Get remote details
  198. _kc_hidden ECRESULT GetExternalObjectDetails(unsigned int id, objectdetails_t *);
  199. // Get userid from usertable or create a new user/group if it doesn't exist yet
  200. _kc_hidden ECRESULT GetLocalObjectIdOrCreate(const objectsignature_t &signature, unsigned int *id);
  201. _kc_hidden ECRESULT GetLocalObjectsIdsOrCreate(const std::list<objectsignature_t> &signatures, map<objectid_t, unsigned int> *local_objids);
  202. // Get a list of local object IDs in the database plus any internal objects (SYSTEM, EVERYONE)
  203. _kc_hidden ECRESULT GetLocalObjectIdList(objectclass_t, unsigned int company_id, std::list<unsigned int> **objs);
  204. // Converts anonymous Object Detail to property. */
  205. _kc_hidden ECRESULT ConvertAnonymousObjectDetailToProp(struct soap *, objectdetails_t *, unsigned int tag, struct propVal *);
  206. // Converts the data in user/group/company details fields into property value array for content tables and MAPI_MAILUSER and MAPI_DISTLIST objects
  207. _kc_hidden ECRESULT ConvertObjectDetailsToProps(struct soap *, unsigned int id, objectdetails_t *, struct propTagArray *proptags, struct propValArray *propvals);
  208. // Converts the data in company/addresslist details fields into property value array for hierarchy tables and MAPI_ABCONT objects
  209. _kc_hidden ECRESULT ConvertContainerObjectDetailsToProps(struct soap *, unsigned int id, objectdetails_t *, struct propTagArray *proptags, struct propValArray *propvals);
  210. // Create GlobalAddressBook properties
  211. _kc_hidden ECRESULT ConvertABContainerToProps(struct soap *, unsigned int id, struct propTagArray *, struct propValArray *);
  212. _kc_hidden ECRESULT MoveOrCreateLocalObject(const objectsignature_t &signature, unsigned int *obj_id, bool *moved);
  213. _kc_hidden ECRESULT CreateLocalObjectSimple(const objectsignature_t &signature, unsigned int pref_id);
  214. _kc_hidden ECRESULT CreateLocalObject(const objectsignature_t &signature, unsigned int *obj_id);
  215. _kc_hidden ECRESULT MoveOrDeleteLocalObject(unsigned int obj_id, objectclass_t);
  216. _kc_hidden ECRESULT MoveLocalObject(unsigned int obj_id, objectclass_t, unsigned int company_id, const std::string &newusername);
  217. _kc_hidden ECRESULT DeleteLocalObject(unsigned int obj_id, objectclass_t);
  218. _kc_hidden ECRESULT UpdateObjectclassOrDelete(const objectid_t &extern_id, unsigned int *obj_id);
  219. _kc_hidden ECRESULT GetUserAndCompanyFromLoginName(const std::string &login, std::string *user, std::string *company);
  220. // Process the modification of a user-object
  221. _kc_hidden ECRESULT CheckObjectModified(unsigned int obj_id, const std::string &localsignature, const std::string &remotesignature);
  222. _kc_hidden ECRESULT ProcessModification(unsigned int id, const std::string &newsignature);
  223. _kc_hidden ECRESULT ResolveObject(objectclass_t, const std::string &name, const objectid_t &company, objectid_t *extern_id);
  224. _kc_hidden ECRESULT CreateABEntryID(struct soap *, const objectid_t &extern_id, struct propVal *);
  225. _kc_hidden ECRESULT CreateABEntryID(struct soap *, unsigned int obj_id, unsigned int type, struct propVal *);
  226. _kc_hidden ECRESULT GetSecurity(ECSecurity **);
  227. protected:
  228. ECPluginFactory *m_lpPluginFactory;
  229. BTSession *m_lpSession;
  230. ECConfig *m_lpConfig;
  231. private:
  232. std::recursive_mutex m_hMutex;
  233. usercount_t m_userCount;
  234. time_t m_usercount_ts = 0;
  235. };
  236. #define KOPANO_UID_EVERYONE 1
  237. #define KOPANO_UID_SYSTEM 2
  238. #define KOPANO_ACCOUNT_SYSTEM "SYSTEM"
  239. #define KOPANO_FULLNAME_SYSTEM "SYSTEM"
  240. #define KOPANO_ACCOUNT_EVERYONE "Everyone"
  241. #define KOPANO_FULLNAME_EVERYONE "Everyone"
  242. /*
  243. * Fixed addressbook containers
  244. * Only IDs 0, 1 and 2 are available for hardcoding
  245. * IDs for the fixed addressbook containers. This is because
  246. * those IDs are the only ones which will not conflict with
  247. * entries in the users table.
  248. *
  249. * The account name of the containers are used for the path
  250. * name of the container and is used in determine the exact
  251. * order of the containers and the subcontainers in the Address
  252. * Book. The fullname of the containers are used as display
  253. * name to the user.
  254. */
  255. #define KOPANO_UID_ADDRESS_BOOK 0
  256. #define KOPANO_UID_GLOBAL_ADDRESS_BOOK 1
  257. #define KOPANO_UID_GLOBAL_ADDRESS_LISTS 2
  258. #define KOPANO_ACCOUNT_ADDRESS_BOOK "Kopano Address Book"
  259. #define KOPANO_FULLNAME_ADDRESS_BOOK "Kopano Address Book"
  260. #define KOPANO_ACCOUNT_GLOBAL_ADDRESS_BOOK "Global Address Book"
  261. #define KOPANO_FULLNAME_GLOBAL_ADDRESS_BOOK "Global Address Book"
  262. #define KOPANO_ACCOUNT_GLOBAL_ADDRESS_LISTS "Global Address Lists"
  263. #define KOPANO_FULLNAME_GLOBAL_ADDRESS_LISTS "All Address Lists"
  264. } /* namespace */
  265. #endif