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.

346 lines
7.7 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: secutil.cxx
  7. //
  8. // Contents: Helper routines for conversion - LDAP specific
  9. //
  10. // Functions:
  11. //
  12. // History: 09-27-98 by splitting ldap\var2sec.cxx
  13. // and distributing between ldapc and router - AjayR
  14. //
  15. //----------------------------------------------------------------------------
  16. #include "ldapc.hxx"
  17. #pragma hdrstop
  18. //
  19. // Definition need as this is not a part of the headers
  20. //
  21. extern "C" {
  22. HRESULT
  23. ADsEncodeBinaryData (
  24. PBYTE pbSrcData,
  25. DWORD dwSrcLen,
  26. LPWSTR * ppszDestData
  27. );
  28. }
  29. HRESULT
  30. ConvertSidToString(
  31. PSID pSid,
  32. LPWSTR String
  33. )
  34. /*++
  35. Routine Description:
  36. This function generates a printable unicode string representation
  37. of a SID.
  38. The resulting string will take one of two forms. If the
  39. IdentifierAuthority value is not greater than 2^32, then
  40. the SID will be in the form:
  41. S-1-281736-12-72-9-110
  42. ^ ^^ ^^ ^ ^^^
  43. | | | | |
  44. +-----+--+-+--+---- Decimal
  45. Otherwise it will take the form:
  46. S-1-0x173495281736-12-72-9-110
  47. ^^^^^^^^^^^^^^ ^^ ^^ ^ ^^^
  48. Hexidecimal | | | |
  49. +--+-+--+---- Decimal
  50. Arguments:
  51. pSid - opaque pointer that supplies the SID that is to be
  52. converted to Unicode.
  53. Return Value:
  54. If the Sid is successfully converted to a Unicode string, a
  55. pointer to the Unicode string is returned, else NULL is
  56. returned.
  57. --*/
  58. {
  59. WCHAR Buffer[256];
  60. UCHAR i;
  61. ULONG Tmp;
  62. HRESULT hr = S_OK;
  63. SID_IDENTIFIER_AUTHORITY *pSidIdentifierAuthority;
  64. PUCHAR pSidSubAuthorityCount;
  65. if (!IsValidSid( pSid )) {
  66. *String= L'\0';
  67. hr = HRESULT_FROM_WIN32(ERROR_INVALID_SID);
  68. RRETURN(hr);
  69. }
  70. wsprintf(Buffer, L"S-%u-", (USHORT)(((PISID)pSid)->Revision ));
  71. wcscpy(String, Buffer);
  72. pSidIdentifierAuthority = GetSidIdentifierAuthority(pSid);
  73. if ( (pSidIdentifierAuthority->Value[0] != 0) ||
  74. (pSidIdentifierAuthority->Value[1] != 0) ){
  75. wsprintf(Buffer, L"0x%02hx%02hx%02hx%02hx%02hx%02hx",
  76. (USHORT)pSidIdentifierAuthority->Value[0],
  77. (USHORT)pSidIdentifierAuthority->Value[1],
  78. (USHORT)pSidIdentifierAuthority->Value[2],
  79. (USHORT)pSidIdentifierAuthority->Value[3],
  80. (USHORT)pSidIdentifierAuthority->Value[4],
  81. (USHORT)pSidIdentifierAuthority->Value[5] );
  82. wcscat(String, Buffer);
  83. } else {
  84. Tmp = (ULONG)pSidIdentifierAuthority->Value[5] +
  85. (ULONG)(pSidIdentifierAuthority->Value[4] << 8) +
  86. (ULONG)(pSidIdentifierAuthority->Value[3] << 16) +
  87. (ULONG)(pSidIdentifierAuthority->Value[2] << 24);
  88. wsprintf(Buffer, L"%lu", Tmp);
  89. wcscat(String, Buffer);
  90. }
  91. pSidSubAuthorityCount = GetSidSubAuthorityCount(pSid);
  92. for (i=0;i< *(pSidSubAuthorityCount);i++ ) {
  93. wsprintf(Buffer, L"-%lu", *(GetSidSubAuthority(pSid, i)));
  94. wcscat(String, Buffer);
  95. }
  96. RRETURN(S_OK);
  97. }
  98. HRESULT
  99. ConvertU2TrusteeToSid(
  100. LPWSTR pszServerName,
  101. CCredentials& Credentials,
  102. LPWSTR pszTrustee,
  103. LPBYTE Sid,
  104. PDWORD pdwSidSize
  105. )
  106. {
  107. PADSLDP pLdapHandle = NULL;
  108. HRESULT hr = S_OK;
  109. LPWSTR *SidAttribute = NULL;
  110. DWORD nCount = 0;
  111. DWORD dwStatus = 0;
  112. struct berval **ppBerValue = NULL;
  113. LPWSTR Attributes[2];
  114. LDAPMessage *res = NULL;
  115. LDAPMessage *entry = NULL;
  116. DWORD dwNumberOfEntries = 0;
  117. DWORD dwSidLength = 0;
  118. LPBYTE lpByte = NULL;
  119. WCHAR szSid[MAX_PATH];
  120. Attributes[0] = L"Sid";
  121. Attributes[1] = NULL;
  122. ConvertSidToString( Sid, szSid);
  123. dwStatus = LdapOpenObject(
  124. pszServerName,
  125. pszTrustee,
  126. &pLdapHandle,
  127. Credentials,
  128. FALSE
  129. );
  130. if (dwStatus) {
  131. hr = HRESULT_FROM_WIN32(dwStatus);
  132. BAIL_ON_FAILURE(hr);
  133. }
  134. dwStatus = LdapSearchS(
  135. pLdapHandle,
  136. pszTrustee,
  137. LDAP_SCOPE_BASE,
  138. L"(objectClass=*)",
  139. Attributes,
  140. 0,
  141. &res
  142. );
  143. if (dwStatus) {
  144. hr = HRESULT_FROM_WIN32(dwStatus);
  145. BAIL_ON_FAILURE(hr);
  146. }
  147. dwNumberOfEntries = LdapCountEntries( pLdapHandle, res );
  148. if ( dwNumberOfEntries == 0 )
  149. RRETURN(S_OK);
  150. dwStatus = LdapFirstEntry( pLdapHandle, res, &entry );
  151. if (dwStatus) {
  152. hr = HRESULT_FROM_WIN32(dwStatus);
  153. BAIL_ON_FAILURE(hr);
  154. }
  155. dwStatus = LdapGetValuesLen(
  156. pLdapHandle,
  157. entry,
  158. L"Sid",
  159. &ppBerValue,
  160. (int *)&nCount
  161. );
  162. if (dwStatus) {
  163. hr = HRESULT_FROM_WIN32(dwStatus);
  164. BAIL_ON_FAILURE(hr);
  165. }
  166. dwSidLength = ((struct berval **)ppBerValue)[0]->bv_len;
  167. lpByte = (LPBYTE)((struct berval **) ppBerValue)[0]->bv_val;
  168. memcpy( Sid, lpByte, dwSidLength);
  169. *pdwSidSize = dwSidLength;
  170. error:
  171. if (res) {
  172. LdapMsgFree( res );
  173. }
  174. RRETURN(hr);
  175. }
  176. HRESULT
  177. ConvertSidToU2Trustee(
  178. LPWSTR pszServerName,
  179. CCredentials& Credentials,
  180. PSID pSid,
  181. LPWSTR szTrustee
  182. )
  183. {
  184. HRESULT hr = S_OK;
  185. PUCHAR pSidAuthorityCount = NULL;
  186. LPWSTR pszQueryString = NULL;
  187. DWORD dwSidLength = 0;
  188. LDAPMessage *res = NULL;
  189. LPWSTR pszDN = NULL;
  190. LDAPMessage *entry = NULL;
  191. DWORD dwStatus = 0;
  192. DWORD dwNumberOfEntries = 0;
  193. WCHAR szSearchExp[MAX_PATH];
  194. PADSLDP pLdapHandle = NULL;
  195. LPWSTR Attributes[] = {L"Sid", NULL};
  196. WCHAR szSid[MAX_PATH];
  197. ConvertSidToString( pSid, szSid);
  198. pSidAuthorityCount = GetSidSubAuthorityCount(pSid);
  199. if (!pSidAuthorityCount) {
  200. RRETURN(E_FAIL);
  201. }
  202. dwSidLength = GetSidLengthRequired(*pSidAuthorityCount);
  203. hr = ADsEncodeBinaryData (
  204. (LPBYTE)pSid,
  205. dwSidLength,
  206. &pszQueryString
  207. );
  208. BAIL_ON_FAILURE(hr);
  209. dwStatus = LdapOpenObject(
  210. pszServerName,
  211. NULL,
  212. &pLdapHandle,
  213. Credentials,
  214. FALSE
  215. );
  216. if (dwStatus) {
  217. hr = HRESULT_FROM_WIN32(dwStatus);
  218. BAIL_ON_FAILURE(hr);
  219. }
  220. wcscpy(szSearchExp,L"(Sid=");
  221. wcscat(szSearchExp, pszQueryString);
  222. wcscat(szSearchExp, L")");
  223. dwStatus = LdapSearchS(
  224. pLdapHandle,
  225. NULL,
  226. LDAP_SCOPE_SUBTREE,
  227. szSearchExp,
  228. Attributes,
  229. 0,
  230. &res
  231. );
  232. if (dwStatus) {
  233. hr = HRESULT_FROM_WIN32(dwStatus);
  234. BAIL_ON_FAILURE(hr);
  235. }
  236. dwNumberOfEntries = LdapCountEntries( pLdapHandle, res );
  237. if ( dwNumberOfEntries == 0 ){
  238. hr = E_FAIL;
  239. BAIL_ON_FAILURE(hr);
  240. }
  241. dwStatus = LdapFirstEntry( pLdapHandle, res, &entry );
  242. if (dwStatus) {
  243. hr = HRESULT_FROM_WIN32(dwStatus);
  244. BAIL_ON_FAILURE(hr);
  245. }
  246. dwStatus = LdapGetDn( pLdapHandle, entry, &pszDN);
  247. if (dwStatus) {
  248. hr = HRESULT_FROM_WIN32(dwStatus);
  249. BAIL_ON_FAILURE(hr);
  250. }
  251. wcscpy(szTrustee, pszDN);
  252. error:
  253. if (pszQueryString) {
  254. FreeADsStr(pszQueryString);
  255. }
  256. if (pszDN) {
  257. LdapMemFree(pszDN);
  258. }
  259. if (res) {
  260. LdapMsgFree( res );
  261. }
  262. if (pLdapHandle) {
  263. LdapCloseObject( pLdapHandle);
  264. }
  265. RRETURN(hr);
  266. }
  267.