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.

297 lines
8.9 KiB

  1. #include <windows.h>
  2. #include "crtdbg.h"
  3. #include <objbase.h>
  4. #include "ISAUsInf.h"
  5. PSID g_pSidEverybody = NULL;
  6. LONG g_pSidEverybodyLenght = 0;
  7. PSID g_pSidAdmins = NULL;
  8. LONG g_pSidAdminsLenght = 0;
  9. VARIANT_BOOL UserSidFound(PSID psidSAUser, LONG psidSAUserLength, PSID ppsidAAUsers[], LONG ppsidAAUsersLength[], DWORD dwNumAASids)
  10. {
  11. DWORD i;
  12. for(i=0; i<dwNumAASids; i++)
  13. {
  14. // if (EqualSid(psidSAUser, ppsidAAUsers[i]) == TRUE)
  15. if (psidSAUserLength && psidSAUserLength == ppsidAAUsersLength[i] && !memcmp(psidSAUser, ppsidAAUsers[i], ppsidAAUsersLength[i]))
  16. return VARIANT_TRUE;
  17. }
  18. return VARIANT_FALSE;
  19. }
  20. HRESULT GetUserList(ISAUserInfo *pSAUserInfo, BSTR **ppbstrSAUserNames,
  21. VARIANT_BOOL **ppvboolUserTypes, PSID **ppsidSAUsers, LONG **ppsidSAUsersLength, DWORD *pdwNumSAUsers)
  22. {
  23. VARIANT_BOOL vboolRes;
  24. VARIANT vUserNames, vUserTypes, vUserSids;
  25. SAFEARRAY *psaUserNames, *psaUserTypes, *psaUserSids;
  26. LONG lNumUsers, lCurrent;
  27. LONG lStartUserNames, lEndUserNames, lStartUserTypes, lEndUserTypes, lStartUserSids, lEndUserSids;
  28. HRESULT hr;
  29. *pdwNumSAUsers = 0;
  30. VariantInit(&vUserNames);
  31. VariantInit(&vUserTypes);
  32. VariantInit(&vUserSids);
  33. hr = pSAUserInfo->GetSAUsers(&vUserNames,
  34. &vUserTypes,
  35. &vUserSids,
  36. VARIANT_TRUE,
  37. &vboolRes);
  38. _ASSERTE(!FAILED(hr));
  39. if (FAILED(hr))
  40. return hr;
  41. psaUserNames = V_ARRAY(&vUserNames);
  42. psaUserTypes = V_ARRAY(&vUserTypes);
  43. psaUserSids = V_ARRAY(&vUserSids);
  44. _ASSERTE (V_VT(&vUserNames) == (VT_ARRAY | VT_VARIANT));
  45. if (V_VT(&vUserNames) != (VT_ARRAY | VT_VARIANT))
  46. return E_INVALIDARG;
  47. _ASSERTE(V_VT(&vUserTypes) == (VT_ARRAY | VT_VARIANT));
  48. if (V_VT(&vUserTypes) != (VT_ARRAY | VT_VARIANT))
  49. return E_INVALIDARG;
  50. _ASSERTE(V_VT(&vUserSids) == (VT_ARRAY | VT_VARIANT));
  51. if (V_VT(&vUserSids) != (VT_ARRAY | VT_VARIANT))
  52. return E_INVALIDARG;
  53. hr = SafeArrayGetLBound( psaUserNames, 1, &lStartUserNames );
  54. _ASSERTE(!FAILED(hr));
  55. if (FAILED(hr))
  56. return hr;
  57. hr = SafeArrayGetUBound( psaUserNames, 1, &lEndUserNames );
  58. _ASSERTE(!FAILED(hr));
  59. if (FAILED(hr))
  60. return hr;
  61. hr = SafeArrayGetLBound( psaUserTypes, 1, &lStartUserTypes );
  62. _ASSERTE(!FAILED(hr));
  63. if (FAILED(hr))
  64. return hr;
  65. hr = SafeArrayGetUBound( psaUserTypes, 1, &lEndUserTypes );
  66. _ASSERTE(!FAILED(hr));
  67. if (FAILED(hr))
  68. return hr;
  69. hr = SafeArrayGetLBound( psaUserSids, 1, &lStartUserSids );
  70. _ASSERTE(!FAILED(hr));
  71. if (FAILED(hr))
  72. return hr;
  73. hr = SafeArrayGetUBound( psaUserSids, 1, &lEndUserSids );
  74. _ASSERTE(!FAILED(hr));
  75. if (FAILED(hr))
  76. return hr;
  77. _ASSERTE(!( (lStartUserNames != lStartUserTypes) || (lEndUserNames != lEndUserNames) ));
  78. if ( (lStartUserNames != lStartUserTypes) || (lEndUserNames != lEndUserNames) )
  79. return E_FAIL;
  80. _ASSERTE(!( (lStartUserNames != lStartUserSids) || ((lEndUserNames+2) != lEndUserSids) ));
  81. if ( (lStartUserNames != lStartUserSids) || ((lEndUserNames+2) != lEndUserSids) )
  82. return E_FAIL;
  83. lNumUsers = lEndUserNames - lStartUserNames + 1;
  84. *pdwNumSAUsers = lNumUsers;
  85. *ppbstrSAUserNames = (BSTR *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lNumUsers * sizeof(BSTR *));
  86. _ASSERTE(*ppbstrSAUserNames);
  87. if ((*ppbstrSAUserNames) == NULL)
  88. return E_OUTOFMEMORY;
  89. *ppvboolUserTypes = (VARIANT_BOOL *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lNumUsers * sizeof(VARIANT_BOOL));
  90. _ASSERTE(*ppvboolUserTypes );
  91. if ((*ppvboolUserTypes ) == NULL)
  92. return E_OUTOFMEMORY;
  93. *ppsidSAUsers = (PSID *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (lNumUsers + 2) * sizeof(PSID));
  94. _ASSERTE(*ppsidSAUsers );
  95. if ((*ppsidSAUsers ) == NULL)
  96. return E_OUTOFMEMORY;
  97. *ppsidSAUsersLength = (LONG *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (lNumUsers + 2) * sizeof(LONG));
  98. _ASSERTE(*ppsidSAUsersLength );
  99. if ((*ppsidSAUsersLength ) == NULL)
  100. return E_OUTOFMEMORY;
  101. VARIANT vName;
  102. VARIANT vType;
  103. VARIANT vSid;
  104. VariantInit( &vName );
  105. VariantInit( &vType );
  106. VariantInit( &vSid );
  107. //
  108. // Process the array elements.
  109. //
  110. for ( lCurrent = lStartUserNames; lCurrent <= lEndUserNames; lCurrent++)
  111. {
  112. hr = SafeArrayGetElement( psaUserNames, &lCurrent, &vName );
  113. _ASSERTE( !FAILED(hr) );
  114. if( FAILED(hr) )
  115. return hr;
  116. hr = SafeArrayGetElement( psaUserTypes, &lCurrent, &vType );
  117. _ASSERTE( !FAILED(hr) );
  118. if( FAILED(hr) )
  119. return hr;
  120. hr = SafeArrayGetElement( psaUserSids, &lCurrent, &vSid );
  121. _ASSERTE( !FAILED(hr) );
  122. if( FAILED(hr) )
  123. return hr;
  124. _ASSERTE ( V_VT(&vName) == VT_BSTR );
  125. if ( V_VT(&vName) != VT_BSTR )
  126. return E_FAIL;
  127. _ASSERTE ( V_VT(&vType) == VT_BOOL );
  128. if ( V_VT(&vType) != VT_BOOL )
  129. return hr;
  130. (*ppbstrSAUserNames)[lCurrent] = SysAllocString(V_BSTR(&vName));
  131. (*ppvboolUserTypes)[lCurrent] = V_BOOL(&vType);
  132. hr = UnpackSidFromVariant(&vSid, &(*ppsidSAUsers)[lCurrent], &(*ppsidSAUsersLength)[lCurrent]);
  133. _ASSERTE( !FAILED(hr) );
  134. if (FAILED(hr))
  135. return hr;
  136. // _ASSERTE (IsValidSid((*ppsidSAUsers)[lCurrent]));
  137. // if (IsValidSid((*ppsidSAUsers)[lCurrent]) == FALSE)
  138. // return E_INVALIDARG;
  139. VariantClear( &vName );
  140. VariantClear( &vType );
  141. VariantClear( &vSid );
  142. }
  143. // Stash Administrators SID
  144. lCurrent = lEndUserSids - 1 ;
  145. hr = SafeArrayGetElement( psaUserSids, &lCurrent, &vSid );
  146. _ASSERTE( !FAILED(hr) );
  147. if( FAILED(hr) )
  148. return hr;
  149. hr = UnpackSidFromVariant(&vSid, &g_pSidAdmins, &g_pSidAdminsLenght);
  150. _ASSERTE( !FAILED(hr) );
  151. if (FAILED(hr))
  152. return hr;
  153. VariantClear( &vSid );
  154. // Stash Everyone SID
  155. lCurrent = lEndUserSids;
  156. hr = SafeArrayGetElement( psaUserSids, &lCurrent, &vSid );
  157. _ASSERTE( !FAILED(hr) );
  158. if( FAILED(hr) )
  159. return hr;
  160. hr = UnpackSidFromVariant(&vSid, &g_pSidEverybody, &g_pSidEverybodyLenght);
  161. _ASSERTE( !FAILED(hr) );
  162. if (FAILED(hr))
  163. return hr;
  164. VariantClear( &vSid );
  165. return S_OK;
  166. }
  167. HRESULT UnpackSidFromVariant(VARIANT *pvarSid, PSID *ppSid, LONG *plSidLength)
  168. {
  169. SAFEARRAY *psaSid;
  170. LONG lStart = 0L, lEnd = 0L,lCurrent;
  171. BYTE byteOfSid;
  172. HRESULT hr;
  173. _ASSERTE (V_VT(pvarSid) == (VT_ARRAY | VT_UI1));
  174. if (V_VT(pvarSid) != (VT_ARRAY | VT_UI1))
  175. return E_INVALIDARG;
  176. psaSid = V_ARRAY(pvarSid);
  177. hr = SafeArrayGetLBound( psaSid, 1, &lStart );
  178. _ASSERTE( !FAILED(hr) );
  179. if (FAILED(hr))
  180. return hr;
  181. hr = SafeArrayGetUBound( psaSid, 1, &lEnd );
  182. _ASSERTE( !FAILED(hr) );
  183. if (FAILED(hr))
  184. return hr;
  185. *plSidLength = lEnd - lStart + 1;
  186. *ppSid = (PSID)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *plSidLength);
  187. _ASSERTE(*ppSid);
  188. if ((*ppSid) == NULL)
  189. return E_OUTOFMEMORY;
  190. for(lCurrent = lStart; lCurrent<lEnd; lCurrent++)
  191. {
  192. hr = SafeArrayGetElement( psaSid, &lCurrent, &byteOfSid );
  193. _ASSERTE( !FAILED(hr) );
  194. if( FAILED(hr) )
  195. return hr;
  196. ((PBYTE)(*ppSid))[lCurrent] = byteOfSid;
  197. }
  198. return S_OK;
  199. }
  200. HRESULT PackSidInVariant(VARIANT **ppVarSid, PSID pSid, LONG lSidLength)
  201. {
  202. ULONG i;
  203. HRESULT hr;
  204. _ASSERTE (!((pSid == NULL) || (ppVarSid == NULL)));
  205. if ((pSid == NULL) || (ppVarSid == NULL))
  206. return E_INVALIDARG;
  207. *ppVarSid = (VARIANT *)HeapAlloc(GetProcessHeap(), 0, sizeof(VARIANT));
  208. _ASSERTE(*ppVarSid);
  209. if ((*ppVarSid) == NULL)
  210. return E_FAIL;
  211. VariantInit((*ppVarSid));
  212. V_VT((*ppVarSid)) = VT_ARRAY | VT_UI1;
  213. SAFEARRAYBOUND bounds;
  214. bounds.cElements = lSidLength;//GetLengthSid(pSid);
  215. bounds.lLbound = 0;
  216. SAFEARRAY *psaUserSid = NULL;
  217. psaUserSid = SafeArrayCreate(VT_UI1, 1, &bounds);
  218. _ASSERTE(psaUserSid);
  219. if (psaUserSid == NULL)
  220. return E_OUTOFMEMORY;
  221. for(i=0; i<bounds.cElements; i++)
  222. {
  223. hr = SafeArrayPutElement(psaUserSid, (LONG*)&i, (LPVOID)(((unsigned char *)pSid)+i*sizeof(unsigned char)));
  224. _ASSERTE( !FAILED(hr) );
  225. if (FAILED(hr))
  226. return hr;
  227. }
  228. V_ARRAY((*ppVarSid)) = psaUserSid;
  229. return S_OK;
  230. }