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.

363 lines
9.2 KiB

  1. //+-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) Microsoft Corporation
  6. //
  7. // File: kthttp.cxx
  8. //
  9. // Contents: Kerberos Tunneller, http communication routines
  10. //
  11. // History: 28-Jun-2001 t-ryanj Created
  12. //
  13. //------------------------------------------------------------------------
  14. #include "ktdebug.h"
  15. #include "kthttp.h"
  16. #include "ktcontrol.h"
  17. #include "ktkerb.h"
  18. #if 0 /* Unneeded for now since http is running sync */
  19. VOID CALLBACK KtHttpCallback(
  20. IN HINTERNET hInternet,
  21. IN DWORD_PTR dwContext,
  22. IN DWORD dwInternetStatus,
  23. IN LPVOID lpvStatusInformation,
  24. IN DWORD dwStatusInformationLength
  25. );
  26. #endif
  27. //#define KT_HTTP_AGENT_STRING TEXT("Kerbtunnel")
  28. //#define KT_KERBPROXY_LOCATION TEXT("/kerberos-KDC/kerbproxy.dll")
  29. TCHAR KT_HTTP_AGENT_STRING[] = TEXT("Kerbtunnel");
  30. TCHAR KT_KERBPROXY_LOCATION[] = TEXT("/kerberos-KDC/kerbproxy.dll");
  31. LPCTSTR KT_MIMETYPES_ACCEPTED[] = { TEXT("*/*"), NULL };
  32. HINTERNET KtInternet = NULL;
  33. //+-------------------------------------------------------------------------
  34. //
  35. // Function: KtInitHttp
  36. //
  37. // Synopsis: Performs necessary initialization for use of http
  38. //
  39. // Effects:
  40. //
  41. // Arguments:
  42. //
  43. // Requires:
  44. //
  45. // Returns: Success value. If FALSE, GetLastError() for details.
  46. //
  47. // Notes:
  48. //
  49. //--------------------------------------------------------------------------
  50. BOOL
  51. KtInitHttp(
  52. VOID
  53. )
  54. {
  55. BOOL fSuccess = TRUE;
  56. DsysAssert(KtInternet == NULL );
  57. KtInternet = InternetOpen( KT_HTTP_AGENT_STRING,
  58. INTERNET_OPEN_TYPE_PRECONFIG,
  59. NULL,
  60. NULL,
  61. 0 );
  62. if( KtInternet == NULL )
  63. {
  64. DebugLog( DEB_ERROR, "%s(%d): Error initializing internet routines: 0x%x.\n", __FILE__, __LINE__, GetLastError() );
  65. goto Error;
  66. }
  67. Cleanup:
  68. return fSuccess;
  69. Error:
  70. KtCleanupHttp();
  71. fSuccess = FALSE;
  72. goto Cleanup;
  73. }
  74. //+-------------------------------------------------------------------------
  75. //
  76. // Function: KtCleanupHttp
  77. //
  78. // Synopsis: Performs necessary cleanup after use of http
  79. //
  80. // Effects:
  81. //
  82. // Arguments:
  83. //
  84. // Requires:
  85. //
  86. // Returns:
  87. //
  88. // Notes:
  89. //
  90. //--------------------------------------------------------------------------
  91. VOID
  92. KtCleanupHttp(
  93. VOID
  94. )
  95. {
  96. if( KtInternet )
  97. {
  98. InternetCloseHandle(KtInternet);
  99. KtInternet = NULL;
  100. }
  101. }
  102. //+-------------------------------------------------------------------------
  103. //
  104. // Function: KtHttpWrite
  105. //
  106. // Synopsis: Opens a connection to the kerbproxy server, creates a POST
  107. // request, and writes the contents of the context buffer as
  108. // the request body.
  109. //
  110. // Effects:
  111. //
  112. // Arguments: pContext - A context.
  113. //
  114. // Requires:
  115. //
  116. // Returns: Success value. If FALSE, GetLastError() for details.
  117. //
  118. // Notes:
  119. //
  120. //--------------------------------------------------------------------------
  121. BOOL
  122. KtHttpWrite(
  123. PKTCONTEXT pContext
  124. )
  125. {
  126. HINTERNET hConnect = NULL;
  127. HINTERNET hRequest = NULL;
  128. BOOL SendSuccess;
  129. BOOL IocpSuccess;
  130. BOOL fRet = TRUE;
  131. INTERNET_PORT InternetPort = INTERNET_DEFAULT_HTTP_PORT;
  132. DebugLog( DEB_TRACE, "%s(%d): Sending %d bytes over http.\n", __FILE__, __LINE__, pContext->buffers->bytesused );
  133. #if 0
  134. //
  135. // Use SSL for AS-REQUEST
  136. //
  137. if( KtIsAsRequest( pContext ) )
  138. InternetPort = INTERNET_DEFAULT_HTTPS_PORT;
  139. #endif
  140. //
  141. // TODO: Add logic so that it tries all the servers in the list.
  142. //
  143. //
  144. // Open a connection to the kerbproxy server and store it in the context.
  145. //
  146. hConnect = InternetConnect( KtInternet,
  147. (LPCWSTR)pContext->pbProxies,
  148. InternetPort,
  149. NULL,
  150. NULL,
  151. INTERNET_SERVICE_HTTP,
  152. 0,
  153. INTERNET_FLAG_ASYNC );
  154. if( !hConnect )
  155. {
  156. DebugLog( DEB_ERROR, "%s(%d): Error connecting to server: 0x%x.\n", __FILE__, __LINE__, GetLastError() );
  157. goto Error;
  158. }
  159. pContext->hConnect = hConnect;
  160. hConnect = NULL;
  161. //
  162. // Open a POST request to the kerbproxy object and store it in the context.
  163. //
  164. hRequest = HttpOpenRequest( pContext->hConnect,
  165. TEXT("POST"),
  166. KT_KERBPROXY_LOCATION,
  167. NULL, /* version */
  168. NULL, /* referrer */
  169. KT_MIMETYPES_ACCEPTED,
  170. 0,
  171. 0 );
  172. if( !hRequest )
  173. {
  174. DebugLog(DEB_ERROR, "%s(%d): Error opening %ws%ws: 0x%x.\n", __FILE__, __LINE__, pContext->pbProxies, KT_KERBPROXY_LOCATION, GetLastError() );
  175. goto Error;
  176. }
  177. pContext->hRequest = hRequest;
  178. hRequest = NULL;
  179. //
  180. // Send the results from reading off the user socket as the request body.
  181. //
  182. #if 0 /* unnecessary, as synchronous ops are being used now. */
  183. InternetSetStatusCallback( hConnect,
  184. KtHttpCallback );
  185. #endif
  186. pContext->Status = KT_HTTP_WRITE;
  187. SendSuccess = HttpSendRequest( pContext->hRequest,
  188. NULL,
  189. 0,
  190. pContext->buffers->buffer,
  191. pContext->buffers->bytesused );
  192. if( !SendSuccess /*&& (GetLastError() != ERROR_IO_PENDING)*/ )
  193. {
  194. DebugLog(DEB_ERROR, "%s(%d): Error from sendrequest: 0x%x.\n", __FILE__, __LINE__, GetLastError() );
  195. goto Error;
  196. }
  197. //
  198. // Since this is being done synchronously right now, we need to post to
  199. // the iocp in order to progress to the next step.
  200. //
  201. IocpSuccess = PostQueuedCompletionStatus( KtIocp,
  202. 0,
  203. KTCK_CHECK_CONTEXT,
  204. &(pContext->ol) );
  205. if( !IocpSuccess )
  206. {
  207. DebugLog(DEB_ERROR, "%s(%d): Error posting to completion port: 0x%x.\n", __FILE__, __LINE__, GetLastError() );
  208. goto Error;
  209. }
  210. Cleanup:
  211. return fRet;
  212. Error:
  213. fRet = FALSE;
  214. goto Cleanup;
  215. }
  216. //+-------------------------------------------------------------------------
  217. //
  218. // Function: KtHttpRead
  219. //
  220. // Synopsis: Reads the response to the request sent by KtHttpWrite into
  221. // the context buffer.
  222. //
  223. // Effects:
  224. //
  225. // Arguments: pContext - A context.
  226. //
  227. // Requires:
  228. //
  229. // Returns: Success value. If FALSE, GetLastError() for details.
  230. //
  231. // Notes:
  232. //
  233. //--------------------------------------------------------------------------
  234. BOOL
  235. KtHttpRead(
  236. PKTCONTEXT pContext
  237. )
  238. {
  239. BOOL EndSuccess;
  240. BOOL ReadSuccess;
  241. BOOL fRet = TRUE;
  242. BOOL IocpSuccess;
  243. DebugLog( DEB_PEDANTIC, "%s(%d): Reading up to %d bytes from http.\n", __FILE__, __LINE__, pContext->emptybuf->buflen );
  244. //
  245. // Read the response from the kerbproxy server.
  246. //
  247. pContext->Status = KT_HTTP_READ;
  248. ReadSuccess = InternetReadFile( pContext->hRequest,
  249. pContext->emptybuf->buffer,
  250. pContext->emptybuf->buflen,
  251. &(pContext->emptybuf->bytesused) );
  252. if( !ReadSuccess )
  253. {
  254. DebugLog(DEB_ERROR, "%s(%d): Error from readfile: 0x%x.\n", __FILE__, __LINE__, GetLastError() );
  255. goto Error;
  256. }
  257. //
  258. // Since we're doing this synchronously, we need to post to the iocp
  259. // manually to move on.
  260. //
  261. IocpSuccess = PostQueuedCompletionStatus( KtIocp,
  262. 0,
  263. KTCK_CHECK_CONTEXT,
  264. &(pContext->ol) );
  265. if( !IocpSuccess )
  266. {
  267. DebugLog( DEB_ERROR, "%s(%d): Error for PQCS: 0x%x\n", __FILE__, __LINE__, GetLastError() );
  268. goto Error;
  269. }
  270. Cleanup:
  271. return fRet;
  272. Error:
  273. fRet = FALSE;
  274. goto Cleanup;
  275. }
  276. #if 0 /* Unneccessary because using synchronous calls for now */
  277. //+-------------------------------------------------------------------------
  278. //
  279. // Function: KtHttpCallback
  280. //
  281. // Synopsis: Callback function for asynchronous http functions.
  282. //
  283. // Effects:
  284. //
  285. // Arguments: hInternet - connection appropriate to this callback
  286. // pContext - context supplied at the time of the async call
  287. // dwInternetStatus - why the callback was called
  288. // lpvStatusInformation - more detailed info
  289. // dwStatusInformationLength - length of *lpvStatusInformation
  290. //
  291. // Requires:
  292. //
  293. // Returns:
  294. //
  295. // Notes: Not being used right now, as synchronous wininet calls are
  296. // being used, since it is not entirely clear how to do the
  297. // asynchronous calls in all cases.
  298. //
  299. //--------------------------------------------------------------------------
  300. VOID CALLBACK KtHttpCallback(
  301. IN HINTERNET hInternet,
  302. IN DWORD_PTR pContext,
  303. IN DWORD dwInternetStatus,
  304. IN LPVOID lpvStatusInformation,
  305. IN DWORD dwStatusInformationLength
  306. )
  307. {
  308. BOOL IocpSuccess;
  309. if( dwInternetStatus == INTERNET_STATUS_REQUEST_COMPLETE )
  310. {
  311. IocpSuccess = PostQueuedCompletionStatus( KtIocp,
  312. 0,
  313. KTCK_CHECK_CONTEXT,
  314. &(((PKPCONTEXT)pContext)->ol) );
  315. if( !IocpSuccess )
  316. {
  317. DebugLog( DEB_ERROR, "%s(%d): Error in PQCS: 0x%x.\n", __FILE__, __LINE__, GetLastError() );
  318. KtReleaseContext( (PKTCONTEXT)pContext );
  319. }
  320. }
  321. else
  322. {
  323. DebugLog( DEB_TRACE, "This httpcallback status: 0x%x.\n", dwInternetStatus );
  324. }
  325. }
  326. #endif