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.

441 lines
9.7 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2001.
  5. //
  6. // File: dsstore.c
  7. //
  8. // Contents: Policy management for directory
  9. //
  10. //
  11. // History: TaroonM
  12. // 10/30/01
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "precomp.h"
  16. LPWSTR PolicyDNAttributes[] = {
  17. L"msieee80211-ID",
  18. L"description",
  19. L"msieee80211-DataType",
  20. L"msieee80211-Data",
  21. L"cn",
  22. L"distinguishedName",
  23. L"whenChanged",
  24. NULL
  25. };
  26. DWORD
  27. OpenDirectoryServerHandle(
  28. LPWSTR pszDomainName,
  29. DWORD dwPortNumber,
  30. HLDAP * phLdapBindHandle
  31. )
  32. {
  33. DWORD dwError = 0;
  34. *phLdapBindHandle = NULL;
  35. dwError = LdapOpen(
  36. pszDomainName,
  37. dwPortNumber,
  38. phLdapBindHandle
  39. );
  40. BAIL_ON_WIN32_ERROR(dwError);
  41. dwError = LdapBind(
  42. *phLdapBindHandle
  43. );
  44. BAIL_ON_WIN32_ERROR(dwError);
  45. return(dwError);
  46. error:
  47. if (*phLdapBindHandle) {
  48. CloseDirectoryServerHandle(
  49. *phLdapBindHandle
  50. );
  51. *phLdapBindHandle = NULL;
  52. }
  53. return(dwError);
  54. }
  55. DWORD
  56. CloseDirectoryServerHandle(
  57. HLDAP hLdapBindHandle
  58. )
  59. {
  60. int ldaperr = 0;
  61. if (hLdapBindHandle) {
  62. ldaperr = ldap_unbind(hLdapBindHandle);
  63. }
  64. return(0);
  65. }
  66. DWORD
  67. ReadPolicyObjectFromDirectory(
  68. HLDAP hLdapBindHandle,
  69. LPWSTR pszPolicyDN,
  70. PWIRELESS_POLICY_OBJECT * ppWirelessPolicyObject
  71. )
  72. {
  73. LDAPMessage *res = NULL;
  74. LDAPMessage *e = NULL;
  75. LPWSTR szFilterString = L"(objectClass=*)";
  76. DWORD dwError = 0;
  77. PWIRELESS_POLICY_OBJECT pWirelessPolicyObject = NULL;
  78. dwError = LdapSearchST(
  79. hLdapBindHandle,
  80. pszPolicyDN,
  81. LDAP_SCOPE_BASE,
  82. szFilterString,
  83. PolicyDNAttributes,
  84. 0,
  85. NULL,
  86. &res
  87. );
  88. BAIL_ON_WIN32_ERROR(dwError);
  89. dwError = UnMarshallPolicyObject(
  90. hLdapBindHandle,
  91. pszPolicyDN,
  92. &pWirelessPolicyObject,
  93. res
  94. );
  95. BAIL_ON_WIN32_ERROR(dwError);
  96. *ppWirelessPolicyObject = pWirelessPolicyObject;
  97. cleanup:
  98. if (res) {
  99. LdapMsgFree(res);
  100. }
  101. return(dwError);
  102. error:
  103. if (pWirelessPolicyObject) {
  104. FreeWirelessPolicyObject(
  105. pWirelessPolicyObject
  106. );
  107. }
  108. *ppWirelessPolicyObject = NULL;
  109. goto cleanup;
  110. }
  111. DWORD
  112. UnMarshallPolicyObject(
  113. HLDAP hLdapBindHandle,
  114. LPWSTR pszPolicyDN,
  115. PWIRELESS_POLICY_OBJECT * ppWirelessPolicyObject,
  116. LDAPMessage *res
  117. )
  118. {
  119. PWIRELESS_POLICY_OBJECT pWirelessPolicyObject = NULL;
  120. DWORD dwCount = 0;
  121. DWORD dwLen = 0;
  122. LPBYTE pBuffer = NULL;
  123. DWORD i = 0;
  124. DWORD dwError = 0;
  125. LDAPMessage *e = NULL;
  126. WCHAR **strvalues = NULL;
  127. struct berval ** bvalues = NULL;
  128. LPWSTR * ppszTemp = NULL;
  129. pWirelessPolicyObject = (PWIRELESS_POLICY_OBJECT)AllocPolMem(
  130. sizeof(WIRELESS_POLICY_OBJECT)
  131. );
  132. if (!pWirelessPolicyObject) {
  133. dwError = ERROR_OUTOFMEMORY;
  134. BAIL_ON_WIN32_ERROR(dwError);
  135. }
  136. dwError = LdapFirstEntry(
  137. hLdapBindHandle,
  138. res,
  139. &e
  140. );
  141. BAIL_ON_WIN32_ERROR(dwError);
  142. /*
  143. strvalues = NULL;
  144. dwError = LdapGetValues(
  145. hLdapBindHandle,
  146. e,
  147. L"distinguishedName",
  148. (WCHAR ***)&strvalues,
  149. (int *)&dwCount
  150. );
  151. BAIL_ON_WIN32_ERROR(dwError);
  152. */
  153. pWirelessPolicyObject->pszWirelessOwnersReference = AllocPolStr(
  154. pszPolicyDN
  155. );
  156. if (!pWirelessPolicyObject->pszWirelessOwnersReference) {
  157. dwError = ERROR_OUTOFMEMORY;
  158. BAIL_ON_WIN32_ERROR(dwError);
  159. }
  160. strvalues = NULL;
  161. dwError = LdapGetValues(
  162. hLdapBindHandle,
  163. e,
  164. L"cn",
  165. (WCHAR ***)&strvalues,
  166. (int *)&dwCount
  167. );
  168. BAIL_ON_WIN32_ERROR(dwError);
  169. pWirelessPolicyObject->pszWirelessName = AllocPolStr(
  170. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  171. );
  172. if (!pWirelessPolicyObject->pszWirelessName) {
  173. dwError = ERROR_OUTOFMEMORY;
  174. BAIL_ON_WIN32_ERROR(dwError);
  175. }
  176. LdapValueFree(strvalues);
  177. strvalues = NULL;
  178. dwError = LdapGetValues(
  179. hLdapBindHandle,
  180. e,
  181. L"description",
  182. (WCHAR ***)&strvalues,
  183. (int *)&dwCount
  184. );
  185. // BAIL_ON_WIN32_ERROR(dwError);
  186. if (strvalues && LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)) {
  187. pWirelessPolicyObject->pszDescription = AllocPolStr(
  188. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  189. );
  190. if (!pWirelessPolicyObject->pszDescription) {
  191. dwError = ERROR_OUTOFMEMORY;
  192. BAIL_ON_WIN32_ERROR(dwError);
  193. }
  194. LdapValueFree(strvalues);
  195. } else {
  196. pWirelessPolicyObject->pszDescription = NULL;
  197. }
  198. strvalues = NULL;
  199. dwError = LdapGetValues(
  200. hLdapBindHandle,
  201. e,
  202. L"msieee80211-ID",
  203. (WCHAR ***)&strvalues,
  204. (int *)&dwCount
  205. );
  206. BAIL_ON_WIN32_ERROR(dwError);
  207. pWirelessPolicyObject->pszWirelessID = AllocPolStr(
  208. LDAPOBJECT_STRING((PLDAPOBJECT)strvalues)
  209. );
  210. if (!pWirelessPolicyObject->pszWirelessID) {
  211. dwError = ERROR_OUTOFMEMORY;
  212. BAIL_ON_WIN32_ERROR(dwError);
  213. }
  214. LdapValueFree(strvalues);
  215. strvalues = NULL;
  216. dwError = LdapGetValues(
  217. hLdapBindHandle,
  218. e,
  219. L"msieee80211-DataType",
  220. (WCHAR ***)&strvalues,
  221. (int *)&dwCount
  222. );
  223. BAIL_ON_WIN32_ERROR(dwError);
  224. pWirelessPolicyObject->dwWirelessDataType = _wtol(LDAPOBJECT_STRING((PLDAPOBJECT)strvalues));
  225. LdapValueFree(strvalues);
  226. strvalues = NULL;
  227. dwError = LdapGetValues(
  228. hLdapBindHandle,
  229. e,
  230. L"whenChanged",
  231. (WCHAR ***)&strvalues,
  232. (int *)&dwCount
  233. );
  234. BAIL_ON_WIN32_ERROR(dwError);
  235. pWirelessPolicyObject->dwWhenChanged = _wtol(LDAPOBJECT_STRING((PLDAPOBJECT)strvalues));
  236. LdapValueFree(strvalues);
  237. //
  238. // unmarshall the msieee80211-Data blob
  239. //
  240. dwError = LdapGetValuesLen(
  241. hLdapBindHandle,
  242. e,
  243. L"msieee80211-Data",
  244. (struct berval ***)&bvalues,
  245. (int *)&dwCount
  246. );
  247. BAIL_ON_WIN32_ERROR(dwError);
  248. dwLen = LDAPOBJECT_BERVAL_LEN((PLDAPOBJECT)bvalues);
  249. pBuffer = (LPBYTE)AllocPolMem(dwLen);
  250. if (!pBuffer) {
  251. dwError = ERROR_OUTOFMEMORY;
  252. BAIL_ON_WIN32_ERROR(dwError);
  253. }
  254. memcpy( pBuffer, LDAPOBJECT_BERVAL_VAL((PLDAPOBJECT)bvalues), dwLen );
  255. pWirelessPolicyObject->pWirelessData = pBuffer;
  256. pWirelessPolicyObject->dwWirelessDataLen = dwLen;
  257. LdapValueFreeLen(bvalues);
  258. *ppWirelessPolicyObject = pWirelessPolicyObject;
  259. return(dwError);
  260. error:
  261. if (pWirelessPolicyObject) {
  262. FreeWirelessPolicyObject(pWirelessPolicyObject);
  263. }
  264. *ppWirelessPolicyObject = NULL;
  265. return(dwError);
  266. }
  267. DWORD
  268. ComputePrelimCN(
  269. LPWSTR szDN,
  270. LPWSTR szCommonName
  271. )
  272. {
  273. LPWSTR pszComma = NULL;
  274. pszComma = wcschr(szDN, L',');
  275. if (!pszComma) {
  276. return (ERROR_INVALID_DATA);
  277. }
  278. *pszComma = L'\0';
  279. wcscpy(szCommonName, szDN);
  280. *pszComma = L',';
  281. return(0);
  282. }
  283. DWORD
  284. ComputePolicyContainerDN(
  285. LPWSTR pszPolicyDN,
  286. LPWSTR * ppszPolicyContainerDN
  287. )
  288. {
  289. LPWSTR pszComma = NULL;
  290. LPWSTR pszPolicyContainer = NULL;
  291. DWORD dwError = 0;
  292. *ppszPolicyContainerDN = NULL;
  293. pszComma = wcschr(pszPolicyDN, L',');
  294. pszPolicyContainer = AllocPolStr(
  295. pszComma + 1
  296. );
  297. if (!pszPolicyContainer) {
  298. dwError = ERROR_OUTOFMEMORY;
  299. BAIL_ON_WIN32_ERROR(dwError);
  300. }
  301. *ppszPolicyContainerDN = pszPolicyContainer;
  302. error:
  303. return(dwError);
  304. }
  305. DWORD
  306. ComputeDefaultDirectory(
  307. LPWSTR * ppszDefaultDirectory
  308. )
  309. {
  310. PDOMAIN_CONTROLLER_INFOW pDomainControllerInfo = NULL;
  311. DWORD dwError = 0;
  312. DWORD Flags = DS_DIRECTORY_SERVICE_REQUIRED | DS_RETURN_DNS_NAME;
  313. LPWSTR pszDefaultDirectory = NULL;
  314. *ppszDefaultDirectory = NULL;
  315. dwError = DsGetDcNameW(
  316. NULL,
  317. NULL,
  318. NULL,
  319. NULL,
  320. Flags,
  321. &pDomainControllerInfo
  322. ) ;
  323. BAIL_ON_WIN32_ERROR(dwError);
  324. pszDefaultDirectory = AllocPolStr(
  325. pDomainControllerInfo->DomainName
  326. );
  327. if (!pszDefaultDirectory) {
  328. dwError = ERROR_OUTOFMEMORY;
  329. BAIL_ON_WIN32_ERROR(dwError);
  330. }
  331. *ppszDefaultDirectory = pszDefaultDirectory;
  332. error:
  333. if (pDomainControllerInfo) {
  334. (void) NetApiBufferFree(pDomainControllerInfo) ;
  335. }
  336. return(dwError);
  337. }