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.

195 lines
4.1 KiB

  1. /*++
  2. Copyright (C) 1998 - 2001 Microsoft Corporation
  3. Module Name:
  4. ldap.c
  5. Abstract:
  6. Handles the various functions for LDAP
  7. --*/
  8. #include "pch.h"
  9. #pragma hdrstop
  10. #include <netdom.h>
  11. DWORD
  12. NetDompLdapBind(
  13. IN LPWSTR DC,
  14. IN LPWSTR Domain,
  15. IN LPWSTR User,
  16. IN LPWSTR Password,
  17. IN ULONG BindType,
  18. OUT PLDAP *Ldap
  19. )
  20. /*++
  21. Routine Description:
  22. Binds to the named server using the given credentials
  23. Arguments:
  24. DC -- DC to connect to
  25. User -- User name to bind with
  26. Password -- Password to use for bind
  27. Ldap -- Where the connection handle is returned
  28. Returns:
  29. ERROR_SUCCESS -- Success
  30. --*/
  31. {
  32. DWORD Win32Err = ERROR_SUCCESS;
  33. SEC_WINNT_AUTH_IDENTITY_W AuthIdent = {0}, *AuthPtr = NULL;
  34. if ( User ) {
  35. AuthIdent.User = User;
  36. AuthIdent.UserLength = wcslen(User);
  37. AuthIdent.Domain = Domain;
  38. AuthIdent.DomainLength = (Domain) ? wcslen(Domain) : 0;
  39. AuthIdent.Password = Password;
  40. AuthIdent.PasswordLength = (Password) ? wcslen(Password) : 0;
  41. AuthIdent.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
  42. AuthPtr = &AuthIdent;
  43. }
  44. if ( Win32Err == ERROR_SUCCESS ) {
  45. LOG_VERBOSE(( MSG_VERBOSE_LDAP_BIND, DC ));
  46. //
  47. // Open a connection
  48. //
  49. *Ldap = ldap_open( DC, LDAP_PORT);
  50. if ( *Ldap ) {
  51. //
  52. // Do the bind
  53. //
  54. Win32Err = LdapMapErrorToWin32( ldap_bind_s( *Ldap,
  55. NULL,
  56. ( PWSTR )AuthPtr,
  57. BindType ) );
  58. } else {
  59. Win32Err = GetLastError();
  60. }
  61. }
  62. return( Win32Err );
  63. }
  64. DWORD
  65. NetDompLdapUnbind(
  66. IN PLDAP Ldap
  67. )
  68. /*++
  69. Routine Description:
  70. Unbinds a current ldap connection
  71. Arguments:
  72. Ldap -- Connection to be severed
  73. Returns:
  74. ERROR_SUCCESS -- Success
  75. --*/
  76. {
  77. DWORD Win32Err = ERROR_SUCCESS;
  78. if ( Ldap != NULL ) {
  79. LOG_VERBOSE(( MSG_VERBOSE_LDAP_UNBIND ));
  80. Win32Err = LdapMapErrorToWin32( ldap_unbind( Ldap ) );
  81. }
  82. return( Win32Err );
  83. }
  84. DWORD
  85. NetDompLdapReadOneAttribute(
  86. IN PLDAP Ldap,
  87. IN PWSTR ObjectPath,
  88. IN PWSTR Attribute,
  89. OUT PWSTR *ReadAttribute
  90. )
  91. {
  92. DWORD Win32Err = ERROR_SUCCESS;
  93. PWSTR Attrib[2] = {
  94. Attribute,
  95. NULL
  96. };
  97. PWSTR *Values = NULL;
  98. LDAPMessage *Message = NULL, *Entry;
  99. ULONG Items, i;
  100. Win32Err = LdapMapErrorToWin32( ldap_search_s( Ldap,
  101. ObjectPath,
  102. LDAP_SCOPE_BASE,
  103. L"(ObjectClass=*)",
  104. Attrib,
  105. 0,
  106. &Message ) );
  107. if ( Win32Err == ERROR_SUCCESS ) {
  108. Entry = ldap_first_entry( Ldap, Message );
  109. if ( Entry ) {
  110. //
  111. // Now, we'll have to get the values
  112. //
  113. Values = ldap_get_values( Ldap,
  114. Entry,
  115. Attrib[ 0 ] );
  116. if ( Values ) {
  117. Win32Err = NetApiBufferAllocate( ( wcslen( Values[ 0 ] ) + 1 ) * sizeof( WCHAR ),
  118. ( PVOID *)ReadAttribute );
  119. if ( Win32Err == ERROR_SUCCESS ) {
  120. wcscpy( *ReadAttribute, Values[ 0 ] );
  121. }
  122. ldap_value_free( Values );
  123. } else {
  124. Win32Err = LdapMapErrorToWin32( Ldap->ld_errno );
  125. }
  126. } else {
  127. Win32Err = LdapMapErrorToWin32( Ldap->ld_errno );
  128. }
  129. }
  130. ldap_msgfree( Message );
  131. return( Win32Err );
  132. }