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.

248 lines
6.3 KiB

  1. /*++
  2. Copyright (c) 1997-2000 Microsoft Corporation
  3. Module Name:
  4. rndldap.h
  5. Abstract:
  6. Some ldap definitions and functions.
  7. --*/
  8. //
  9. // Some constants.
  10. //
  11. #ifndef __RNDLDAP_H_
  12. #define __RNDLDAP_H_
  13. #pragma once
  14. #include "rndcommc.h"
  15. const WCHAR DYNAMIC_USER_CN_FORMAT[] = L"%s]%hs";
  16. const WCHAR DYNAMIC_USER_DN_FORMAT[] = L"cn=%s,%s";
  17. const WCHAR DYNAMIC_CONTAINER[] = L"ou=dynamic,";
  18. const WCHAR DYNAMICOBJECT[] = L"DynamicObject";
  19. const WCHAR OBJECTCLASS[] = L"ObjectClass";
  20. const WCHAR USEROBJECT[] = L"userObject";
  21. const WCHAR NT_SECURITY_DESCRIPTOR[] = L"ntSecurityDescriptor";
  22. const WCHAR AT_CHARACTER = L'@';
  23. const WCHAR ANY_OBJECT_CLASS[] = L"ObjectClass=*";
  24. const WCHAR DEFAULT_NAMING_CONTEXT[] = L"defaultNamingContext";
  25. const WCHAR CNEQUALS[] = L"cn=";
  26. const WCHAR ENTRYTTL[] = L"EntryTTL";
  27. const WCHAR CLOSE_BRACKET_CHARACTER = L']';
  28. const WCHAR NULL_CHARACTER = L'\0';
  29. // decimal values for the following ports
  30. const WORD ILS_PORT = 1002;
  31. const WORD ILS_SSL_PORT = 637; // ZoltanS changed from 4999
  32. const WORD MINIMUM_TTL = 300;
  33. const DWORD REND_LDAP_TIMELIMIT = 60; // 60 seconds
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CLdapPtr is a smart pointer for a ldap connection.
  36. /////////////////////////////////////////////////////////////////////////////
  37. class CLdapPtr
  38. {
  39. public:
  40. CLdapPtr() : m_hLdap(NULL) {}
  41. CLdapPtr(LDAP *hLdap) : m_hLdap(hLdap) {}
  42. ~CLdapPtr() { if (m_hLdap) ldap_unbind(m_hLdap);}
  43. CLdapPtr &operator= (LDAP *hLdap) { m_hLdap = hLdap; return *this;}
  44. operator LDAP* () { return m_hLdap; }
  45. private:
  46. LDAP *m_hLdap;
  47. };
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CLdapMsgPtr is a smart pointer for a ldap message.
  50. /////////////////////////////////////////////////////////////////////////////
  51. class CLdapMsgPtr
  52. {
  53. public:
  54. CLdapMsgPtr() : m_pLdapMsg(NULL) {}
  55. CLdapMsgPtr(IN LDAPMessage *LdapMessage) : m_pLdapMsg(LdapMessage) {}
  56. ~CLdapMsgPtr() { ldap_msgfree(m_pLdapMsg); }
  57. LDAPMessage **operator& () { return &m_pLdapMsg; }
  58. operator LDAPMessage * () { return m_pLdapMsg; }
  59. CLdapMsgPtr& operator=(LDAPMessage *p) { m_pLdapMsg = p; return *this; }
  60. private:
  61. LDAPMessage *m_pLdapMsg;
  62. };
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CLdapValuePtr is a smart pointer for a ldap value.
  65. /////////////////////////////////////////////////////////////////////////////
  66. class CLdapValuePtr
  67. {
  68. public:
  69. CLdapValuePtr(IN TCHAR **Value) : m_Value(Value) {}
  70. ~CLdapValuePtr() { ldap_value_free(m_Value); }
  71. protected:
  72. TCHAR **m_Value;
  73. };
  74. /////////////////////////////////////////////////////////////////////////////
  75. // other functions
  76. /////////////////////////////////////////////////////////////////////////////
  77. inline HRESULT
  78. HResultFromErrorCodeWithoutLogging(IN long ErrorCode)
  79. {
  80. return ( 0x80070000 | (0xa000ffff & ErrorCode) );
  81. }
  82. inline HRESULT
  83. GetLdapHResult(
  84. IN ULONG LdapResult
  85. )
  86. {
  87. return HRESULT_FROM_ERROR_CODE((long)LdapMapErrorToWin32(LdapResult));
  88. }
  89. inline BOOL
  90. CompareLdapHResult(
  91. IN HRESULT hr,
  92. IN ULONG LdapErrorCode
  93. )
  94. {
  95. return ( hr == GetLdapHResult(LdapErrorCode));
  96. }
  97. #define BAIL_IF_LDAP_FAIL(Result, msg) \
  98. { \
  99. ULONG _res_ = Result; \
  100. if ( LDAP_SUCCESS != _res_ ) \
  101. { \
  102. LOG((MSP_ERROR, "%S - %d:%S", msg, _res_, ldap_err2string(_res_)));\
  103. return GetLdapHResult(_res_); \
  104. } \
  105. }
  106. // ZoltanS: For when we want to note an LDAP error and find the HR, but not bail.
  107. inline HRESULT
  108. LogAndGetLdapHResult(ULONG Result, TCHAR * msg)
  109. {
  110. BAIL_IF_LDAP_FAIL(Result, msg);
  111. return S_OK;
  112. }
  113. // ZoltanS: For when we want to find the HR, but not bail or log
  114. inline HRESULT
  115. GetLdapHResultIfFailed(ULONG Result)
  116. {
  117. if ( Result != LDAP_SUCCESS )
  118. {
  119. return HResultFromErrorCodeWithoutLogging(
  120. (long) LdapMapErrorToWin32( Result ) );
  121. }
  122. return S_OK;
  123. }
  124. inline WORD
  125. GetOtherPort(IN WORD CurrentPort)
  126. {
  127. switch (CurrentPort)
  128. {
  129. case LDAP_PORT: return LDAP_SSL_PORT;
  130. case LDAP_SSL_PORT: return LDAP_PORT;
  131. case ILS_PORT: return ILS_SSL_PORT;
  132. case ILS_SSL_PORT: return ILS_PORT;
  133. }
  134. // We don't support SSL unless the server is using a well-known
  135. // non-SSL port. Basically we would otherwise have to also publish
  136. // SSL ports in the DS in addition to non-SSL ports.
  137. _ASSERTE(FALSE);
  138. return CurrentPort; // was LDAP_PORT
  139. }
  140. HRESULT GetAttributeValue(
  141. IN LDAP * pLdap,
  142. IN LDAPMessage * pEntry,
  143. IN const WCHAR * pName,
  144. OUT BSTR * pValue
  145. );
  146. HRESULT GetAttributeValueBer(
  147. IN LDAP * pLdap,
  148. IN LDAPMessage * pEntry,
  149. IN const WCHAR * pName,
  150. OUT char ** pValue,
  151. OUT DWORD * pdwSize
  152. );
  153. HRESULT GetNamingContext(
  154. LDAP *hLdap,
  155. TCHAR **ppNamingContext
  156. );
  157. ULONG
  158. DoLdapSearch (
  159. LDAP *ld,
  160. PWCHAR base,
  161. ULONG scope,
  162. PWCHAR filter,
  163. PWCHAR attrs[],
  164. ULONG attrsonly,
  165. LDAPMessage **res,
  166. BOOL bSACL = TRUE
  167. );
  168. ULONG
  169. DoLdapAdd (
  170. LDAP *ld,
  171. PWCHAR dn,
  172. LDAPModW *attrs[]
  173. );
  174. ULONG
  175. DoLdapModify (
  176. BOOL fChase,
  177. LDAP *ld,
  178. PWCHAR dn,
  179. LDAPModW *attrs[],
  180. BOOL bSACL = TRUE
  181. );
  182. ULONG
  183. DoLdapDelete (
  184. LDAP *ld,
  185. PWCHAR dn
  186. );
  187. HRESULT SetTTL(
  188. IN LDAP * pLdap,
  189. IN const WCHAR * pDN,
  190. IN DWORD dwTTL
  191. );
  192. HRESULT UglyIPtoIP(
  193. BSTR pUglyIP,
  194. BSTR * pIP
  195. );
  196. HRESULT ParseUserName(
  197. BSTR pName,
  198. BSTR * ppAddress
  199. );
  200. #endif // __RNDLDAP_H_
  201. // eof