Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
6.2 KiB

  1. #ifndef EXLDAP_H
  2. #define EXLDAP_H
  3. #include <winldap.h>
  4. #include "EaLen.hpp"
  5. #include "Common.hpp"
  6. #include "UString.hpp"
  7. typedef WINLDAPAPI LDAP * LDAPAPI LDAP_OPEN( PWCHAR HostName, ULONG PortNumber );
  8. typedef WINLDAPAPI LDAP * LDAPAPI LDAP_INIT( PWCHAR HostName, ULONG PortNumber );
  9. typedef WINLDAPAPI ULONG LDAPAPI LDAPMAPERRORTOWIN32( ULONG LdapError );
  10. typedef WINLDAPAPI ULONG LDAPAPI LDAP_UNBIND( LDAP *ld );
  11. typedef WINLDAPAPI ULONG LDAPAPI LDAP_BIND( LDAP *ld, PWCHAR dn, PWCHAR cred, ULONG method );
  12. typedef WINLDAPAPI ULONG LDAPAPI LDAPGETLASTERROR( VOID );
  13. typedef WINLDAPAPI ULONG LDAPAPI LDAP_MODIFY_S( LDAP *ld, PWCHAR dn, LDAPModW *mods[] );
  14. typedef WINLDAPAPI ULONG LDAPAPI LDAP_MSGFREE( LDAPMessage *res );
  15. typedef WINLDAPAPI ULONG LDAPAPI LDAP_COUNT_ENTRIES( LDAP *ld, LDAPMessage *res );
  16. typedef WINLDAPAPI ULONG LDAPAPI LDAP_SEARCH_EXT_S(
  17. LDAP *ld,
  18. PWCHAR base,
  19. ULONG scope,
  20. PWCHAR filter,
  21. PWCHAR attrs[],
  22. ULONG attrsonly,
  23. PLDAPControlW *ServerControls,
  24. PLDAPControlW *ClientControls,
  25. struct l_timeval *timeout,
  26. ULONG SizeLimit,
  27. LDAPMessage **res
  28. );
  29. typedef WINLDAPAPI ULONG LDAPAPI LDAP_CREATE_PAGE_CONTROL(
  30. PLDAP ExternalHandle,
  31. ULONG PageSize,
  32. struct berval *Cookie,
  33. UCHAR IsCritical,
  34. PLDAPControlW *Control
  35. );
  36. typedef WINLDAPAPI ULONG LDAPAPI LDAP_PARSE_PAGE_CONTROL (
  37. PLDAP ExternalHandle,
  38. PLDAPControlW *ServerControls,
  39. ULONG *TotalCount,
  40. struct berval **Cookie // Use ber_bvfree to free
  41. );
  42. typedef WINLDAPAPI PWCHAR *LDAPAPI LDAP_GET_VALUES(
  43. LDAP *ld,
  44. LDAPMessage *entry,
  45. PWCHAR attr
  46. );
  47. typedef WINLDAPAPI ULONG LDAPAPI LDAP_VALUE_FREE( PWCHAR *vals );
  48. typedef WINLDAPAPI LDAPMessage *LDAPAPI LDAP_NEXT_ENTRY( LDAP *ld, LDAPMessage *entry );
  49. typedef WINLDAPAPI LDAPMessage *LDAPAPI LDAP_FIRST_ENTRY( LDAP *ld, LDAPMessage *res );
  50. typedef WINLDAPAPI VOID LDAPAPI BER_BVFREE( struct berval *bv );
  51. typedef WINLDAPAPI ULONG LDAPAPI LDAP_CONTROLS_FREE (
  52. LDAPControlW **Control
  53. );
  54. typedef WINLDAPAPI ULONG LDAPAPI LDAP_PARSE_RESULT (
  55. LDAP *Connection,
  56. LDAPMessage *ResultMessage,
  57. ULONG *ReturnCode OPTIONAL, // returned by server
  58. PWCHAR *MatchedDNs OPTIONAL, // free with ldap_memfree
  59. PWCHAR *ErrorMessage OPTIONAL, // free with ldap_memfree
  60. PWCHAR **Referrals OPTIONAL, // free with ldap_value_freeW
  61. PLDAPControlW **ServerControls OPTIONAL, // free with ldap_free_controlsW
  62. BOOLEAN Freeit
  63. );
  64. typedef WINLDAPAPI ULONG LDAPAPI LDAP_GET_OPTION( LDAP *ld, int option, void *outvalue );
  65. typedef WINLDAPAPI ULONG LDAPAPI LDAP_SET_OPTION( LDAP *ld, int option, void *invalue );
  66. class CLdapConnection
  67. {
  68. WCHAR m_exchServer[LEN_Computer];
  69. LDAP * m_LD;
  70. ULONG m_port;
  71. HMODULE m_hDll;
  72. BOOL m_bUseSSL;
  73. WCHAR m_credentials[300];
  74. WCHAR m_password[100];
  75. public:
  76. LDAP_PARSE_RESULT * ldap_parse_result;
  77. LDAP_PARSE_PAGE_CONTROL * ldap_parse_page_control;
  78. LDAP_CONTROLS_FREE * ldap_controls_free;
  79. BER_BVFREE * ber_bvfree;
  80. LDAP_FIRST_ENTRY * ldap_first_entry;
  81. LDAP_NEXT_ENTRY * ldap_next_entry;
  82. LDAP_VALUE_FREE * ldap_value_free;
  83. LDAP_GET_VALUES * ldap_get_values;
  84. LDAP_CREATE_PAGE_CONTROL * ldap_create_page_control;
  85. LDAP_SEARCH_EXT_S * ldap_search_ext_s;
  86. LDAP_COUNT_ENTRIES * ldap_count_entries;
  87. LDAP_MSGFREE * ldap_msgfree;
  88. LDAP_MODIFY_S * ldap_modify_s;
  89. LDAPGETLASTERROR * LdapGetLastError;
  90. LDAP_BIND * ldap_bind_sW;
  91. LDAP_UNBIND * ldap_unbind;
  92. LDAPMAPERRORTOWIN32 * LdapMapErrorToWin32;
  93. LDAP_OPEN * ldap_open;
  94. LDAP_GET_OPTION * ldap_get_option;
  95. LDAP_SET_OPTION * ldap_set_option;
  96. LDAP_INIT * ldap_init;
  97. public:
  98. CLdapConnection();
  99. ~CLdapConnection();
  100. void SetCredentials(WCHAR const * cred,WCHAR const * pwd) { safecopy(m_credentials,cred); safecopy(m_password,pwd); }
  101. LDAP * GetHandle() { return m_LD; }
  102. DWORD Connect(WCHAR const * server,ULONG port);
  103. DWORD UpdateSimpleStringValue(WCHAR const * dn, WCHAR const * property, WCHAR const * value);
  104. void Close();
  105. BOOL StringToBytes(WCHAR const * pString,BYTE * pBytes);
  106. BOOL BytesToString(BYTE * pBytes,WCHAR * sidString,DWORD numBytes);
  107. protected:
  108. // helper functions
  109. BYTE HexValue(WCHAR value);
  110. void AddByteToString(WCHAR ** string,BYTE value);
  111. };
  112. class CLdapEnum
  113. {
  114. BOOL m_bOpen;
  115. ULONG m_nReturned;
  116. ULONG m_nCurrent;
  117. ULONG m_totalCount;
  118. LDAPMessage * m_message;
  119. LDAPMessage * m_currMsg;
  120. WCHAR m_query[1000];
  121. WCHAR m_basepoint[LEN_DistName];
  122. int m_scope;
  123. long m_pageSize;
  124. int m_nAttributes;
  125. WCHAR ** m_AttrNames;
  126. public:
  127. CLdapConnection m_connection;
  128. public:
  129. CLdapEnum() { m_bOpen = FALSE; m_nReturned = 0; m_nCurrent = 0; m_totalCount = 0; m_message = NULL;
  130. m_query[0] = 0; m_basepoint[0] = 0; m_scope = 2; m_pageSize = 100; m_currMsg = NULL;
  131. m_nAttributes = 0; m_AttrNames =NULL;}
  132. ~CLdapEnum();
  133. DWORD InitConnection(WCHAR const * server,ULONG port) { return m_connection.Connect(server,port); }
  134. DWORD Open(WCHAR const * query,WCHAR const * basePoint,short scope, long pageSize,int numAttributes,
  135. WCHAR ** attrs);
  136. DWORD Next(PWCHAR ** ppAttrs);
  137. void FreeData(PWCHAR* pValues);
  138. protected:
  139. DWORD GetNextEntry(PWCHAR ** ppAttrs);
  140. DWORD GetNextPage();
  141. };
  142. #endif // EXLDAP_H