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.

373 lines
6.7 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. creden.cxx
  5. Abstract:
  6. This module abstracts user credentials for the multiple credential support.
  7. Author:
  8. Krishna Ganugapati (KrishnaG) 03-Aug-1996
  9. Revision History:
  10. --*/
  11. extern "C" {
  12. #include <nt.h>
  13. #include <ntrtl.h>
  14. #include <nturtl.h>
  15. #include <windows.h>
  16. #include <imagehlp.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include "memory.h"
  20. #include <wincrypt.h>
  21. }
  22. #include <basetyps.h>
  23. typedef long HRESULT;
  24. #include "misc.hxx"
  25. #include "creden.hxx"
  26. CCredentials::CCredentials():
  27. _lpszUserName(NULL),
  28. _lpszPassword(NULL),
  29. _dwAuthFlags(0),
  30. _dwMaxLen(0)
  31. {
  32. }
  33. CCredentials::CCredentials(
  34. LPWSTR lpszUserName,
  35. LPWSTR lpszPassword,
  36. DWORD dwAuthFlags
  37. ):
  38. _lpszUserName(NULL),
  39. _lpszPassword(NULL),
  40. _dwAuthFlags(0),
  41. _dwMaxLen(0)
  42. {
  43. if (lpszUserName) {
  44. _lpszUserName = AllocADsStr(
  45. lpszUserName
  46. );
  47. }else {
  48. _lpszUserName = NULL;
  49. }
  50. if (lpszPassword) {
  51. SetPassword(lpszPassword);
  52. }
  53. else
  54. {
  55. _lpszPassword = NULL;
  56. _dwMaxLen = 0;
  57. }
  58. _dwAuthFlags = dwAuthFlags;
  59. }
  60. CCredentials::~CCredentials()
  61. {
  62. if (_lpszUserName) {
  63. FreeADsStr(_lpszUserName);
  64. }
  65. if (_lpszPassword) {
  66. FreeADsMem(_lpszPassword);
  67. }
  68. }
  69. HRESULT
  70. CCredentials::GetUserName(
  71. LPWSTR *lppszUserName
  72. )
  73. {
  74. if (!lppszUserName) {
  75. RRETURN(E_FAIL);
  76. }
  77. if (!_lpszUserName) {
  78. *lppszUserName = NULL;
  79. }else {
  80. *lppszUserName = AllocADsStr(_lpszUserName);
  81. if (!*lppszUserName) {
  82. RRETURN(E_OUTOFMEMORY);
  83. }
  84. }
  85. RRETURN(S_OK);
  86. }
  87. HRESULT
  88. CCredentials::GetPassword(
  89. LPWSTR * lppszPassword
  90. ) const
  91. {
  92. if (!lppszPassword) {
  93. RRETURN(E_FAIL);
  94. }
  95. if (!_lpszPassword) {
  96. *lppszPassword = NULL;
  97. }
  98. else
  99. {
  100. LPWSTR lpszTemp = (LPWSTR)AllocADsMem(_dwMaxLen);
  101. if (!lpszTemp)
  102. {
  103. RRETURN(E_FAIL);
  104. }
  105. memcpy(lpszTemp, _lpszPassword, _dwMaxLen);
  106. CryptUnprotectMemory(lpszTemp, _dwMaxLen, CRYPTPROTECTMEMORY_SAME_PROCESS);
  107. *lppszPassword = lpszTemp;
  108. }
  109. RRETURN(S_OK);
  110. }
  111. HRESULT
  112. CCredentials::SetUserName(
  113. LPWSTR lpszUserName
  114. )
  115. {
  116. if (_lpszUserName) {
  117. FreeADsStr(_lpszUserName);
  118. }
  119. if (!lpszUserName) {
  120. _lpszUserName = NULL;
  121. RRETURN(S_OK);
  122. }
  123. _lpszUserName = AllocADsStr(
  124. lpszUserName
  125. );
  126. if(!_lpszUserName) {
  127. RRETURN(E_FAIL);
  128. }
  129. RRETURN(S_OK);
  130. }
  131. HRESULT
  132. CCredentials::SetPassword(
  133. LPWSTR lpszPassword
  134. )
  135. {
  136. if (_lpszPassword) {
  137. FreeADsMem(_lpszPassword);
  138. }
  139. if (!lpszPassword) {
  140. _lpszPassword = NULL;
  141. _dwMaxLen = 0;
  142. RRETURN(S_OK);
  143. }
  144. DWORD dwLen = (wcslen(lpszPassword) + 1) * sizeof(WCHAR);
  145. DWORD dwPadLen = CRYPTPROTECTMEMORY_BLOCK_SIZE - (dwLen % CRYPTPROTECTMEMORY_BLOCK_SIZE);
  146. if( dwPadLen == CRYPTPROTECTMEMORY_BLOCK_SIZE )
  147. {
  148. dwPadLen = 0;
  149. }
  150. _dwMaxLen = dwLen + dwPadLen;
  151. _lpszPassword = (LPWSTR)AllocADsMem(_dwMaxLen);
  152. if(!_lpszPassword) {
  153. _dwMaxLen = 0;
  154. RRETURN(E_FAIL);
  155. }
  156. wcscpy(_lpszPassword, lpszPassword);
  157. BOOL bOK = CryptProtectMemory(_lpszPassword, _dwMaxLen, CRYPTPROTECTMEMORY_SAME_PROCESS);
  158. if (bOK == TRUE)
  159. {
  160. RRETURN(S_OK);
  161. }
  162. else
  163. {
  164. RRETURN(E_FAIL);
  165. }
  166. }
  167. CCredentials::CCredentials(
  168. const CCredentials& Credentials
  169. )
  170. {
  171. LPWSTR pTempPass;
  172. _lpszUserName = NULL;
  173. _lpszPassword = NULL;
  174. _lpszUserName = AllocADsStr(
  175. Credentials._lpszUserName
  176. );
  177. Credentials.GetPassword(&pTempPass);
  178. if (pTempPass)
  179. {
  180. SetPassword(pTempPass);
  181. SecureZeroMemory(pTempPass, wcslen(pTempPass)*sizeof(WCHAR));
  182. FreeADsMem(pTempPass);
  183. }
  184. _dwAuthFlags = Credentials._dwAuthFlags;
  185. }
  186. void
  187. CCredentials::operator=(
  188. const CCredentials& other
  189. )
  190. {
  191. LPWSTR pTempPass;
  192. if ( &other == this) {
  193. return;
  194. }
  195. if (_lpszUserName) {
  196. FreeADsStr(_lpszUserName);
  197. }
  198. if (_lpszPassword) {
  199. FreeADsMem(_lpszPassword);
  200. }
  201. _lpszUserName = AllocADsStr(
  202. other._lpszUserName
  203. );
  204. other.GetPassword(&pTempPass);
  205. if (pTempPass)
  206. {
  207. SetPassword(pTempPass);
  208. SecureZeroMemory(pTempPass, wcslen(pTempPass)*sizeof(WCHAR));
  209. FreeADsMem(pTempPass);
  210. }
  211. _dwAuthFlags = other._dwAuthFlags;
  212. return;
  213. }
  214. BOOL
  215. operator==(
  216. CCredentials& x,
  217. CCredentials& y
  218. )
  219. {
  220. BOOL bEqualUser = FALSE;
  221. BOOL bEqualPassword = FALSE;
  222. BOOL bEqualFlags = FALSE;
  223. LPWSTR lpszXPassword = NULL;
  224. LPWSTR lpszYPassword = NULL;
  225. BOOL bReturnCode = FALSE;
  226. HRESULT hr = S_OK;
  227. if (x._lpszUserName && y._lpszUserName) {
  228. bEqualUser = !(wcscmp(x._lpszUserName, y._lpszUserName));
  229. }else if (!x._lpszUserName && !y._lpszUserName){
  230. bEqualUser = TRUE;
  231. }
  232. hr = x.GetPassword(&lpszXPassword);
  233. if (FAILED(hr)) {
  234. goto error;
  235. }
  236. hr = y.GetPassword(&lpszYPassword);
  237. if (FAILED(hr)) {
  238. goto error;
  239. }
  240. if ((lpszXPassword && lpszYPassword)) {
  241. bEqualPassword = !(wcscmp(lpszXPassword, lpszYPassword));
  242. }else if (!lpszXPassword && !lpszYPassword) {
  243. bEqualPassword = TRUE;
  244. }
  245. if (x._dwAuthFlags == y._dwAuthFlags) {
  246. bEqualFlags = TRUE;
  247. }
  248. if (bEqualUser && bEqualPassword && bEqualFlags) {
  249. bReturnCode = TRUE;
  250. }
  251. error:
  252. if (lpszXPassword) {
  253. FreeADsMem(lpszXPassword);
  254. }
  255. if (lpszYPassword) {
  256. FreeADsMem(lpszYPassword);
  257. }
  258. return(bReturnCode);
  259. }
  260. BOOL
  261. CCredentials::IsNullCredentials(
  262. )
  263. {
  264. // The function will return true even if the flags are set
  265. // this is because we want to try and get the default credentials
  266. // even if the flags were set
  267. if (!_lpszUserName && !_lpszPassword) {
  268. return(TRUE);
  269. }else {
  270. return(FALSE);
  271. }
  272. }
  273. DWORD
  274. CCredentials::GetAuthFlags()
  275. {
  276. return(_dwAuthFlags);
  277. }
  278. void
  279. CCredentials::SetAuthFlags(
  280. DWORD dwAuthFlags
  281. )
  282. {
  283. _dwAuthFlags = dwAuthFlags;
  284. }