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.

324 lines
7.5 KiB

  1. /*++
  2. Copyright (c) 1993 Microsoft Corporation
  3. Module Name:
  4. ccache.c
  5. Abstract:
  6. This module contains the code to keep a cache of the user
  7. credentials. The cache is used mainly for a user browsing from
  8. winfile.
  9. Author:
  10. Chuck Y Chan (chuckc) 4-Dec-93
  11. Revision History:
  12. chuckc Created
  13. --*/
  14. #include <nwclient.h>
  15. #include <nwcanon.h>
  16. #include <nwapi.h>
  17. //-------------------------------------------------------------------//
  18. // //
  19. // Local Function Prototypes //
  20. // //
  21. //-------------------------------------------------------------------//
  22. DWORD
  23. ExtractServerName(
  24. IN LPWSTR RemoteName,
  25. OUT LPWSTR ServerName,
  26. IN DWORD ServerNameSize
  27. ) ;
  28. //-------------------------------------------------------------------//
  29. // //
  30. // Global variables //
  31. // //
  32. //-------------------------------------------------------------------//
  33. static WCHAR CachedPassword[NW_MAX_PASSWORD_LEN+1] ;
  34. static WCHAR CachedUserName[NW_MAX_USERNAME_LEN+1] ;
  35. static WCHAR CachedServerName[NW_MAX_SERVER_LEN+1] ;
  36. static DWORD CachedCredentialsTime ;
  37. static UNICODE_STRING CachedPasswordUnicodeStr ;
  38. static UCHAR EncodeSeed = 0 ;
  39. //-------------------------------------------------------------------//
  40. // //
  41. // Function Bodies //
  42. // //
  43. //-------------------------------------------------------------------//
  44. DWORD
  45. NwpCacheCredentials(
  46. IN LPWSTR RemoteName,
  47. IN LPWSTR UserName,
  48. IN LPWSTR Password
  49. )
  50. /*++
  51. Routine Description:
  52. This function caches the user credentials for the particular
  53. server.
  54. Arguments:
  55. RemoteName - path containg the server we are accessing. only the
  56. server component is of interest.
  57. UserName - user name to remember
  58. Password - password to remember
  59. Return Value:
  60. NO_ERROR - Successfully cached the credentials
  61. Win32 error code otherwise.
  62. --*/
  63. {
  64. DWORD status ;
  65. //
  66. // various paramter checks
  67. //
  68. if (!RemoteName || !UserName || !Password)
  69. {
  70. status = ERROR_INVALID_PARAMETER ;
  71. goto ExitPoint ;
  72. }
  73. if (wcslen(UserName) >= sizeof(CachedUserName)/sizeof(CachedUserName[0]))
  74. {
  75. status = ERROR_INVALID_PARAMETER ;
  76. goto ExitPoint ;
  77. }
  78. if (wcslen(Password) >= sizeof(CachedPassword)/sizeof(CachedPassword[0]))
  79. {
  80. status = ERROR_INVALID_PARAMETER ;
  81. goto ExitPoint ;
  82. }
  83. //
  84. // extract the server portion of the path
  85. //
  86. status = ExtractServerName(
  87. RemoteName,
  88. CachedServerName,
  89. sizeof(CachedServerName)/sizeof(CachedServerName[0])) ;
  90. if (status != NO_ERROR)
  91. {
  92. goto ExitPoint ;
  93. }
  94. //
  95. // save away the credentials
  96. //
  97. wcscpy(CachedUserName, UserName) ;
  98. wcscpy(CachedPassword, Password) ;
  99. //
  100. // encode it since it is in page pool
  101. //
  102. RtlInitUnicodeString(&CachedPasswordUnicodeStr, CachedPassword) ;
  103. RtlRunEncodeUnicodeString(&EncodeSeed, &CachedPasswordUnicodeStr) ;
  104. //
  105. // mark the time this happened
  106. //
  107. CachedCredentialsTime = GetTickCount() ;
  108. return NO_ERROR ;
  109. ExitPoint:
  110. CachedServerName[0] = 0 ;
  111. return status ;
  112. }
  113. BOOL
  114. NwpRetrieveCachedCredentials(
  115. IN LPWSTR RemoteName,
  116. OUT LPWSTR *UserName,
  117. OUT LPWSTR *Password
  118. )
  119. /*++
  120. Routine Description:
  121. This function retrieves the cached user credentials for the particular
  122. server.
  123. Arguments:
  124. RemoteName - path containg the server we are accessing. only the
  125. server component is of interest.
  126. UserName - used to return user name
  127. Password - used to return password
  128. Return Value:
  129. NO_ERROR - Successfully returned at least one entry.
  130. Win32 error code otherwise.
  131. --*/
  132. {
  133. DWORD status ;
  134. DWORD CurrentTime ;
  135. WCHAR ServerName[NW_MAX_SERVER_LEN+1] ;
  136. *UserName = NULL ;
  137. *Password = NULL ;
  138. CurrentTime = GetTickCount() ;
  139. if (!RemoteName)
  140. {
  141. return FALSE ;
  142. }
  143. //
  144. // if too old, bag out
  145. //
  146. if (((CurrentTime > CachedCredentialsTime) &&
  147. (CurrentTime - CachedCredentialsTime) > 60000) ||
  148. ((CurrentTime < CachedCredentialsTime) &&
  149. (CurrentTime + (MAXULONG - CachedCredentialsTime)) >= 60000))
  150. {
  151. CachedServerName[0] = 0 ; // reset to nothing
  152. return FALSE ;
  153. }
  154. status = ExtractServerName(
  155. RemoteName,
  156. ServerName,
  157. sizeof(ServerName)/sizeof(ServerName[0])) ;
  158. if (status != NO_ERROR)
  159. {
  160. return FALSE ;
  161. }
  162. //
  163. // if dont compare, bag out
  164. //
  165. if (_wcsicmp(ServerName, CachedServerName) != 0)
  166. {
  167. return FALSE ;
  168. }
  169. //
  170. // allocate memory to return data
  171. //
  172. if (!(*UserName = (LPWSTR) LocalAlloc(
  173. LPTR,
  174. (wcslen(CachedUserName)+1) * sizeof(WCHAR))))
  175. {
  176. return FALSE ;
  177. }
  178. if (!(*Password = (LPWSTR) LocalAlloc(
  179. LPTR,
  180. (wcslen(CachedPassword)+1) * sizeof(WCHAR))))
  181. {
  182. LocalFree((HLOCAL)*UserName) ;
  183. *UserName = NULL ;
  184. return FALSE ;
  185. }
  186. //
  187. // decode the string,copy it and then reencode it
  188. //
  189. RtlRunDecodeUnicodeString(EncodeSeed, &CachedPasswordUnicodeStr) ;
  190. wcscpy(*Password, CachedPassword) ;
  191. RtlRunEncodeUnicodeString(&EncodeSeed, &CachedPasswordUnicodeStr) ;
  192. wcscpy(*UserName, CachedUserName) ;
  193. //
  194. // update the tick count
  195. //
  196. CachedCredentialsTime = GetTickCount() ;
  197. return TRUE ;
  198. }
  199. DWORD
  200. ExtractServerName(
  201. IN LPWSTR RemoteName,
  202. OUT LPWSTR ServerName,
  203. IN DWORD ServerNameSize
  204. )
  205. /*++
  206. Routine Description:
  207. This function extracts the server name out of a remote name
  208. Arguments:
  209. RemoteName - the input string to extract the server name from.
  210. ServerName - the return buffer for the server string
  211. ServerNameSize - size o f buffer in chars
  212. Return Value:
  213. NO_ERROR - Successfully cached the credentials
  214. Win32 error code otherwise.
  215. --*/
  216. {
  217. LPWSTR ServerStart ;
  218. LPWSTR ServerEnd ;
  219. //
  220. // skip initial backslashes, then find next one delimiting the server name
  221. //
  222. ServerStart = RemoteName ;
  223. while (*ServerStart == L'\\')
  224. ServerStart++ ;
  225. ServerEnd = wcschr(ServerStart, L'\\') ;
  226. if (ServerEnd)
  227. *ServerEnd = 0 ;
  228. //
  229. // make sure we can fit
  230. //
  231. if (wcslen(ServerStart) >= ServerNameSize)
  232. {
  233. if (ServerEnd)
  234. *ServerEnd = L'\\' ;
  235. return ERROR_INVALID_PARAMETER ;
  236. }
  237. //
  238. // copy and restore the backslash
  239. //
  240. wcscpy(ServerName, ServerStart) ;
  241. if (ServerEnd)
  242. *ServerEnd = L'\\' ;
  243. return NO_ERROR ;
  244. }