Leaked source code of windows server 2003
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.

178 lines
7.0 KiB

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