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.

454 lines
12 KiB

  1. #include "stdafx.h"
  2. #include "common.h"
  3. #include "coauth.h"
  4. #ifdef _DEBUG
  5. #undef THIS_FILE
  6. static char BASED_CODE THIS_FILE[] = __FILE__;
  7. #endif
  8. #define new DEBUG_NEW
  9. BOOL
  10. EqualAuthInfo(
  11. COAUTHINFO* pAuthInfo,
  12. COAUTHINFO* pAuthInfoOther)
  13. {
  14. if ( pAuthInfo && pAuthInfoOther )
  15. {
  16. if ( (pAuthInfo->dwAuthnSvc != pAuthInfoOther->dwAuthnSvc) ||
  17. (pAuthInfo->dwAuthzSvc != pAuthInfoOther->dwAuthzSvc) ||
  18. (pAuthInfo->dwAuthnLevel != pAuthInfoOther->dwAuthnLevel) ||
  19. (pAuthInfo->dwImpersonationLevel != pAuthInfoOther->dwImpersonationLevel) ||
  20. (pAuthInfo->dwCapabilities != pAuthInfoOther->dwCapabilities) )
  21. {
  22. return FALSE;
  23. }
  24. // only compare pwszServerPrincName's if they're both specified
  25. if (pAuthInfo->pwszServerPrincName && pAuthInfoOther->pwszServerPrincName)
  26. {
  27. if ( lstrcmpW(pAuthInfo->pwszServerPrincName,
  28. pAuthInfoOther->pwszServerPrincName) != 0 )
  29. {
  30. return FALSE;
  31. }
  32. }
  33. else
  34. {
  35. // if one was NULL, both should be NULL for equality
  36. if (pAuthInfo->pwszServerPrincName != pAuthInfoOther->pwszServerPrincName)
  37. {
  38. return FALSE;
  39. }
  40. }
  41. // we never cache authid, so one of them must be NULL
  42. ASSERT(!(pAuthInfo->pAuthIdentityData && pAuthInfoOther->pAuthIdentityData));
  43. if (pAuthInfo->pAuthIdentityData || pAuthInfoOther->pAuthIdentityData)
  44. {
  45. return FALSE;
  46. }
  47. }
  48. else
  49. {
  50. if ( pAuthInfo != pAuthInfoOther )
  51. {
  52. return FALSE;
  53. }
  54. }
  55. return TRUE;
  56. }
  57. /*
  58. HRESULT
  59. CopyAuthIdentity(
  60. IN COAUTHIDENTITY * pAuthIdentSrc,
  61. IN COAUTHIDENTITY ** ppAuthIdentDest
  62. )
  63. {
  64. HRESULT hr = E_OUTOFMEMORY;
  65. ULONG ulCharLen = 1;
  66. COAUTHIDENTITY *pAuthIdentTemp = NULL;
  67. *ppAuthIdentDest = NULL;
  68. // Guard against both being set, although presumably this would have
  69. // caused grief before we got to this point.
  70. if ((pAuthIdentSrc->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE) &&
  71. (pAuthIdentSrc->Flags & SEC_WINNT_AUTH_IDENTITY_ANSI))
  72. {
  73. ASSERT(0 && "Both string type flags were set!");
  74. hr = E_UNEXPECTED;
  75. goto Cleanup;
  76. }
  77. if (pAuthIdentSrc->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
  78. {
  79. ulCharLen = sizeof(WCHAR);
  80. }
  81. else if (pAuthIdentSrc->Flags & SEC_WINNT_AUTH_IDENTITY_ANSI)
  82. {
  83. ulCharLen = sizeof(CHAR);
  84. }
  85. else
  86. {
  87. // The user didn't specify either string bit? How did we get here?
  88. ASSERT(0 && "String type flag was not set!");
  89. hr = E_UNEXPECTED;
  90. goto Cleanup;
  91. }
  92. pAuthIdentTemp = (COAUTHIDENTITY*) AllocMem(sizeof(COAUTHIDENTITY));
  93. if (!pAuthIdentTemp)
  94. goto Cleanup;
  95. CopyMemory(pAuthIdentTemp, pAuthIdentSrc, sizeof(COAUTHIDENTITY));
  96. // Strings need to be allocated individually and copied
  97. pAuthIdentTemp->User = pAuthIdentTemp->Domain = pAuthIdentTemp->Password = NULL;
  98. if (pAuthIdentSrc->User)
  99. {
  100. pAuthIdentTemp->User = (USHORT *)AllocMem((pAuthIdentTemp->UserLength+1) * ulCharLen);
  101. if (!pAuthIdentTemp->User)
  102. goto Cleanup;
  103. CopyMemory(pAuthIdentTemp->User, pAuthIdentSrc->User, (pAuthIdentTemp->UserLength+1) * ulCharLen);
  104. }
  105. if (pAuthIdentSrc->Domain)
  106. {
  107. pAuthIdentTemp->Domain = (USHORT *)AllocMem((pAuthIdentTemp->DomainLength+1) * ulCharLen);
  108. if (!pAuthIdentTemp->Domain)
  109. goto Cleanup;
  110. CopyMemory(pAuthIdentTemp->Domain, pAuthIdentSrc->Domain, (pAuthIdentTemp->DomainLength+1) * ulCharLen);
  111. }
  112. if (pAuthIdentSrc->Password)
  113. {
  114. pAuthIdentTemp->Password = (USHORT *)AllocMem((pAuthIdentTemp->PasswordLength+1) * ulCharLen);
  115. if (!pAuthIdentTemp->Password)
  116. goto Cleanup;
  117. CopyMemory(pAuthIdentTemp->Password, pAuthIdentSrc->Password, (pAuthIdentTemp->PasswordLength+1) * ulCharLen);
  118. }
  119. hr = S_OK;
  120. Cleanup:
  121. if (SUCCEEDED(hr))
  122. {
  123. *ppAuthIdentDest = pAuthIdentTemp;
  124. }
  125. else
  126. {
  127. if (pAuthIdentTemp)
  128. {
  129. FreeMem(pAuthIdentTemp);
  130. }
  131. }
  132. return hr;
  133. }
  134. HRESULT
  135. CopyAuthInfo(
  136. IN COAUTHINFO * pAuthInfoSrc,
  137. IN COAUTHINFO ** ppAuthInfoDest
  138. )
  139. {
  140. HRESULT hr = E_OUTOFMEMORY;
  141. COAUTHINFO *pAuthInfoTemp = NULL;
  142. *ppAuthInfoDest = NULL;
  143. if (pAuthInfoSrc == NULL)
  144. {
  145. return S_OK;
  146. }
  147. pAuthInfoTemp = (COAUTHINFO*)AllocMem(sizeof(COAUTHINFO));
  148. if (!pAuthInfoTemp)
  149. goto Cleanup;
  150. CopyMemory(pAuthInfoTemp, pAuthInfoSrc, sizeof(COAUTHINFO));
  151. // We need to allocate these fields and make a copy
  152. pAuthInfoTemp->pwszServerPrincName = NULL;
  153. pAuthInfoTemp->pAuthIdentityData = NULL;
  154. // only alloc space for pwszServerPrincName if its non-null
  155. if (pAuthInfoSrc->pwszServerPrincName)
  156. {
  157. pAuthInfoTemp->pwszServerPrincName =
  158. (LPWSTR) AllocMem((lstrlenW(pAuthInfoSrc->pwszServerPrincName) + 1) * sizeof(WCHAR));
  159. if (!pAuthInfoTemp->pwszServerPrincName)
  160. goto Cleanup;
  161. lstrcpyW(pAuthInfoTemp->pwszServerPrincName, pAuthInfoSrc->pwszServerPrincName);
  162. }
  163. // copy the AuthIdentity if its non-null
  164. if (pAuthInfoSrc->pAuthIdentityData)
  165. {
  166. hr = CopyAuthIdentity(pAuthInfoSrc->pAuthIdentityData, &pAuthInfoTemp->pAuthIdentityData);
  167. if (FAILED(hr))
  168. goto Cleanup;
  169. }
  170. hr = S_OK;
  171. Cleanup:
  172. if (SUCCEEDED(hr))
  173. {
  174. *ppAuthInfoDest = pAuthInfoTemp;
  175. }
  176. else if (pAuthInfoTemp)
  177. {
  178. FreeMem(pAuthInfoTemp);
  179. }
  180. return hr;
  181. }
  182. HRESULT
  183. CopyServerInfo(
  184. IN COSERVERINFO * pServerInfoSrc,
  185. IN COSERVERINFO ** ppServerInfoDest
  186. )
  187. {
  188. HRESULT hr = E_OUTOFMEMORY;
  189. COSERVERINFO *pServerInfoTemp = NULL;
  190. *ppServerInfoDest = NULL;
  191. if (pServerInfoSrc == NULL)
  192. {
  193. return S_OK;
  194. }
  195. pServerInfoTemp = (COSERVERINFO*)AllocMem(sizeof(COSERVERINFO));
  196. if (!pServerInfoTemp)
  197. goto Cleanup;
  198. CopyMemory(pServerInfoTemp, pServerInfoSrc, sizeof(COSERVERINFO));
  199. // We need to allocate these fields and make a copy
  200. pServerInfoTemp->pwszName = NULL;
  201. // only alloc space for pwszServerPrincName if its non-null
  202. if (pServerInfoSrc->pwszName)
  203. {
  204. pServerInfoTemp->pwszName =
  205. (LPWSTR) AllocMem((lstrlenW(pServerInfoSrc->pwszName) + 1) * sizeof(WCHAR));
  206. if (!pServerInfoTemp->pwszName)
  207. goto Cleanup;
  208. lstrcpyW(pServerInfoTemp->pwszName, pServerInfoSrc->pwszName);
  209. }
  210. pServerInfoTemp->pAuthInfo = NULL;
  211. // copy the AuthIdentity if its non-null
  212. if (pServerInfoSrc->pAuthInfo)
  213. {
  214. hr = CopyAuthInfo(pServerInfoSrc->pAuthInfo, &pServerInfoTemp->pAuthInfo);
  215. if (FAILED(hr))
  216. goto Cleanup;
  217. }
  218. hr = S_OK;
  219. Cleanup:
  220. if (SUCCEEDED(hr))
  221. {
  222. *ppServerInfoDest = pServerInfoTemp;
  223. }
  224. else if (pServerInfoTemp)
  225. {
  226. FreeMem(pServerInfoTemp);
  227. }
  228. return hr;
  229. }
  230. */
  231. HRESULT
  232. CopyServerInfoStruct(
  233. IN COSERVERINFO * pServerInfoSrc,
  234. IN COSERVERINFO * pServerInfoDest
  235. )
  236. {
  237. HRESULT hr = E_OUTOFMEMORY;
  238. if (pServerInfoSrc == NULL)
  239. {
  240. return S_OK;
  241. }
  242. if (pServerInfoDest == NULL)
  243. {
  244. return E_POINTER;
  245. }
  246. CopyMemory(pServerInfoDest, pServerInfoSrc, sizeof(COSERVERINFO));
  247. // We need to allocate these fields and make a copy
  248. pServerInfoDest->pwszName = NULL;
  249. // only alloc space for pwszServerPrincName if its non-null
  250. if (pServerInfoSrc->pwszName)
  251. {
  252. pServerInfoDest->pwszName =
  253. (LPWSTR) AllocMem((lstrlenW(pServerInfoSrc->pwszName) + 1) * sizeof(WCHAR));
  254. if (!pServerInfoDest->pwszName)
  255. goto Cleanup;
  256. lstrcpyW(pServerInfoDest->pwszName, pServerInfoSrc->pwszName);
  257. }
  258. pServerInfoDest->pAuthInfo = NULL;
  259. hr = S_OK;
  260. Cleanup:
  261. return hr;
  262. }
  263. HRESULT
  264. CopyAuthInfoStruct(
  265. IN COAUTHINFO * pAuthInfoSrc,
  266. IN COAUTHINFO * pAuthInfoDest
  267. )
  268. {
  269. HRESULT hr = E_OUTOFMEMORY;
  270. if (pAuthInfoSrc == NULL)
  271. {
  272. return S_OK;
  273. }
  274. if (pAuthInfoDest == NULL)
  275. {
  276. return E_POINTER;
  277. }
  278. CopyMemory(pAuthInfoDest, pAuthInfoSrc, sizeof(COAUTHINFO));
  279. // We need to allocate these fields and make a copy
  280. pAuthInfoDest->pwszServerPrincName = NULL;
  281. pAuthInfoDest->pAuthIdentityData = NULL;
  282. // only alloc space for pwszServerPrincName if its non-null
  283. if (pAuthInfoSrc->pwszServerPrincName)
  284. {
  285. pAuthInfoDest->pwszServerPrincName =
  286. (LPWSTR) AllocMem((lstrlenW(pAuthInfoSrc->pwszServerPrincName) + 1) * sizeof(WCHAR));
  287. if (!pAuthInfoDest->pwszServerPrincName)
  288. goto Cleanup;
  289. lstrcpyW(pAuthInfoDest->pwszServerPrincName, pAuthInfoSrc->pwszServerPrincName);
  290. }
  291. pAuthInfoDest->pAuthIdentityData = NULL;
  292. hr = S_OK;
  293. Cleanup:
  294. return hr;
  295. }
  296. HRESULT
  297. CopyAuthIdentityStruct(
  298. IN COAUTHIDENTITY * pAuthIdentSrc,
  299. IN COAUTHIDENTITY * pAuthIdentDest
  300. )
  301. {
  302. HRESULT hr = E_OUTOFMEMORY;
  303. ULONG ulCharLen = 1;
  304. if (pAuthIdentSrc == NULL)
  305. {
  306. hr = E_POINTER;
  307. goto Cleanup;
  308. }
  309. if (pAuthIdentDest == NULL)
  310. {
  311. hr = E_POINTER;
  312. goto Cleanup;
  313. }
  314. // Guard against both being set, although presumably this would have
  315. // caused grief before we got to this point.
  316. if ((pAuthIdentSrc->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE) &&
  317. (pAuthIdentSrc->Flags & SEC_WINNT_AUTH_IDENTITY_ANSI))
  318. {
  319. ASSERT(0 && "Both string type flags were set!");
  320. hr = E_UNEXPECTED;
  321. goto Cleanup;
  322. }
  323. if (pAuthIdentSrc->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
  324. {
  325. ulCharLen = sizeof(WCHAR);
  326. }
  327. else if (pAuthIdentSrc->Flags & SEC_WINNT_AUTH_IDENTITY_ANSI)
  328. {
  329. ulCharLen = sizeof(CHAR);
  330. }
  331. else
  332. {
  333. // The user didn't specify either string bit? How did we get here?
  334. ASSERT(0 && "String type flag was not set!");
  335. hr = E_UNEXPECTED;
  336. goto Cleanup;
  337. }
  338. CopyMemory(pAuthIdentDest, pAuthIdentSrc, sizeof(COAUTHIDENTITY));
  339. // Strings need to be allocated individually and copied
  340. pAuthIdentDest->User = pAuthIdentDest->Domain = pAuthIdentDest->Password = NULL;
  341. if (pAuthIdentSrc->User)
  342. {
  343. pAuthIdentDest->User = (USHORT *)AllocMem((pAuthIdentDest->UserLength+1) * ulCharLen);
  344. if (!pAuthIdentDest->User)
  345. goto Cleanup;
  346. CopyMemory(pAuthIdentDest->User, pAuthIdentSrc->User, (pAuthIdentDest->UserLength+1) * ulCharLen);
  347. }
  348. if (pAuthIdentSrc->Domain)
  349. {
  350. pAuthIdentDest->Domain = (USHORT *)AllocMem((pAuthIdentDest->DomainLength+1) * ulCharLen);
  351. if (!pAuthIdentDest->Domain)
  352. goto Cleanup;
  353. CopyMemory(pAuthIdentDest->Domain, pAuthIdentSrc->Domain, (pAuthIdentDest->DomainLength+1) * ulCharLen);
  354. }
  355. if (pAuthIdentSrc->Password)
  356. {
  357. pAuthIdentDest->Password = (USHORT *)AllocMem((pAuthIdentDest->PasswordLength+1) * ulCharLen);
  358. if (!pAuthIdentDest->Password)
  359. goto Cleanup;
  360. CopyMemory(pAuthIdentDest->Password, pAuthIdentSrc->Password, (pAuthIdentDest->PasswordLength+1) * ulCharLen);
  361. }
  362. hr = S_OK;
  363. Cleanup:
  364. return hr;
  365. }