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.

382 lines
9.8 KiB

  1. //#define STRICT
  2. // Include Files
  3. //
  4. #include <tchar.h>
  5. #include <windows.h>
  6. #include <windowsx.h>
  7. #include <stdio.h>
  8. //#include <wininet.h>
  9. #include <ATKInternet.h>
  10. #include "rwpost.h"
  11. #include "rw_common.h"
  12. #ifdef _DEBUG
  13. #include <iostream.h>
  14. #endif
  15. static void _stdcall myCallback(
  16. IN HINTERNET hInternet,
  17. IN DWORD_PTR dwContext,
  18. IN DWORD dwInternetStatus,
  19. IN LPVOID lpvStatusInformation OPTIONAL,
  20. IN DWORD dwStatusInformationLength
  21. );
  22. //
  23. // returns
  24. // #define RWZ_SITE_CONNECTED 1
  25. // #define RWZ_FAIL_TOCONNECTTOSITE 0
  26. // #define RWZ_SITE_REQUIRES_AUTHENTICATION 2
  27. //
  28. DWORD ChkSiteAvailability( HWND hwndProxyParent, LPCTSTR szIISServer,DWORD dwTimeOut,LPTSTR szProxyServer ,LPTSTR szUserName,
  29. LPTSTR szPassword)
  30. {
  31. DWORD dwRet ;
  32. DWORD dwLen=1024;
  33. DWORD dwAccessType ;
  34. HANDLE hEvent;
  35. HINTERNET hSession;
  36. HINTERNET hConnect;
  37. HINTERNET hHttpFile;
  38. INTERNET_STATUS_CALLBACK hI;
  39. char bufQuery[32]="" ;
  40. DWORD dwbufQuery;
  41. DWORD dwLengthBufQuery;
  42. BOOL bQuery;
  43. // Date : 2/4/98
  44. // The below var are added for incorporatinf IE Auth Dlg
  45. // We are changing the Connectivity Check Via Proxy using
  46. // INTERNET_OPEN_PRECONFIG
  47. DWORD dwProxyAuthError; // Store error returned by InternetErrorDlg
  48. LPTSTR pUserName;
  49. LPTSTR pPassword;
  50. LPTSTR pProxyServerName;
  51. pUserName =_T("");
  52. pPassword =_T("");
  53. pProxyServerName =NULL;
  54. if(_tcscmp(szProxyServer,_T(""))){
  55. dwAccessType = INTERNET_OPEN_TYPE_PRECONFIG;
  56. }else{
  57. dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
  58. }
  59. #ifdef _DEBUG
  60. RW_DEBUG << "\nServer is :" << ConvertToANSIString(szIISServer) <<"\n" <<flush;
  61. RW_DEBUG << "\nTimeout is :" << dwTimeOut <<"\n" <<flush;
  62. if(_tcscmp(szProxyServer,_T(""))){
  63. RW_DEBUG << "Proxy is :" << ConvertToANSIString(szProxyServer) <<"\n" <<flush;
  64. }else{
  65. RW_DEBUG << "Proxy is : NULL" <<"\n" <<flush;
  66. }
  67. #endif
  68. hSession = ATK_InternetOpen ( _T("Registration Wizard"),
  69. dwAccessType,
  70. pProxyServerName,
  71. NULL, // List of Addesss to ByPass
  72. INTERNET_FLAG_ASYNC
  73. );
  74. if (hSession == NULL){
  75. dwRet = RWZ_FAIL_TOCONNECTTOSITE;
  76. #ifdef _DEBUG
  77. RW_DEBUG << "InternetOpen Failed\n" << flush;
  78. #endif
  79. return dwRet;
  80. }
  81. hConnect = ATK_InternetConnect( hSession,
  82. szIISServer,
  83. INTERNET_INVALID_PORT_NUMBER,
  84. pUserName,
  85. pPassword,
  86. INTERNET_SERVICE_HTTP,
  87. 0,
  88. 1
  89. );
  90. if(hConnect == NULL){
  91. ::InternetCloseHandle(hSession);
  92. #ifdef _DEBUG
  93. RW_DEBUG << "InternetConnect Failed\n" << flush;
  94. #endif
  95. dwRet = RWZ_FAIL_TOCONNECTTOSITE;
  96. return dwRet;
  97. }
  98. hEvent = CreateEvent(NULL, FALSE,FALSE,_T("HTTPConnect"));
  99. hHttpFile = ATK_HttpOpenRequest( hConnect,
  100. _T("GET"),
  101. _T("/register.asp"),
  102. HTTP_VERSION,
  103. NULL,
  104. 0,
  105. INTERNET_FLAG_DONT_CACHE,
  106. (DWORD_PTR)hEvent
  107. );
  108. if (hHttpFile == NULL){
  109. #ifdef _DEBUG
  110. RW_DEBUG << "HttpOpenRequest Failed\n" << flush;
  111. #endif
  112. dwRet = RWZ_FAIL_TOCONNECTTOSITE;
  113. CloseHandle(hEvent);
  114. ::InternetCloseHandle(hConnect);
  115. ::InternetCloseHandle(hSession);
  116. return dwRet;
  117. }
  118. //
  119. // Http Open is Success so continue further
  120. hI = InternetSetStatusCallback(hHttpFile,myCallback);
  121. BOOL bSendRequest = ATK_HttpSendRequest(hHttpFile, NULL, 0, 0, 0);
  122. DWORD dwWaitStatus = WaitForSingleObject(hEvent,dwTimeOut) ;
  123. // Time out Error
  124. if( dwWaitStatus == WAIT_TIMEOUT ){
  125. #ifdef _DEBUG
  126. RW_DEBUG << "Timeout Happened\n" << flush;
  127. #endif
  128. RW_DEBUG << "Timeout Happened\n" << flush;
  129. dwRet = RWZ_FAIL_TOCONNECTTOSITE;
  130. goto FinishFn;
  131. }
  132. if(dwWaitStatus == WAIT_FAILED){
  133. #ifdef _DEBUG
  134. RW_DEBUG << "WAIT_FAILED returned from WaitForSingleObject\n" << flush;
  135. #endif
  136. dwRet = RWZ_FAIL_TOCONNECTTOSITE;
  137. goto FinishFn;
  138. }
  139. dwLengthBufQuery = sizeof (dwbufQuery);
  140. //DWORD dwLengthBufQuery = sizeof ( bufQuery);
  141. bQuery = ATK_HttpQueryInfo( hHttpFile,
  142. HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
  143. //HTTP_QUERY_CONTENT_DESCRIPTION ,
  144. //HTTP_QUERY_CONTENT_LENGTH ,
  145. &dwbufQuery,
  146. //bufQuery ,
  147. &dwLengthBufQuery,
  148. NULL) ;
  149. RW_DEBUG << "\nChecking Connection To Site via Proxy Returns[" << dwbufQuery << "]" <<flush;
  150. switch(dwbufQuery) {
  151. case HTTP_STATUS_OK:
  152. case HTTP_STATUS_SERVICE_UNAVAIL:
  153. dwRet = RWZ_SITE_CONNECTED;
  154. break;
  155. case HTTP_STATUS_PROXY_AUTH_REQ:
  156. // Invoking the IE Proxy Server Authentication Dialog
  157. dwProxyAuthError = InternetErrorDlg(hwndProxyParent,hHttpFile,
  158. ERROR_INTERNET_INCORRECT_PASSWORD,
  159. FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS,
  160. NULL);
  161. RW_DEBUG << "\nInetErrorDialog REturns " << dwProxyAuthError << flush;
  162. dwRet = RWZ_SITE_REQUIRES_AUTHENTICATION;
  163. //
  164. // We are not checking for the return value because
  165. // connection has to be reestablished to really check
  166. // the info provided is valid
  167. /**
  168. 02/07/98
  169. switch (dwProxyAuthError) {
  170. case ERROR_SUCCESS:
  171. dwRet = RWZ_SITE_CONNECTED;
  172. break;
  173. case ERROR_CANCELLED :
  174. case ERROR_INTERNET_FORCE_RETRY:
  175. dwRet = RWZ_SITE_REQUIRES_AUTHENTICATION;
  176. default:
  177. break;
  178. }
  179. **/
  180. break; // End of Proxy Auth
  181. case 0:
  182. case HTTP_STATUS_SERVER_ERROR:
  183. dwRet = RWZ_FAIL_TOCONNECTTOSITE;
  184. RW_DEBUG << "\n Problem in Proxy Server With connection Via Proxy " << dwbufQuery << flush;
  185. break;
  186. default:
  187. dwRet = RWZ_SITE_CONNECTED;
  188. break;
  189. }
  190. FinishFn :
  191. CloseHandle(hEvent);
  192. ::InternetCloseHandle(hHttpFile);
  193. ::InternetCloseHandle(hConnect);
  194. ::InternetCloseHandle(hSession);
  195. return dwRet;
  196. }
  197. void _stdcall myCallback(
  198. IN HINTERNET hInternet,
  199. IN DWORD_PTR dwContext,
  200. IN DWORD dwInternetStatus,
  201. IN LPVOID lpvStatusInformation OPTIONAL,
  202. IN DWORD dwStatusInformationLength
  203. )
  204. {
  205. switch(dwInternetStatus)
  206. {
  207. case INTERNET_STATUS_RESOLVING_NAME :
  208. #ifdef _DEBUG
  209. RW_DEBUG << "\nCallback: RESOLVING_NAME\n" << flush;
  210. #endif //_DEBUG
  211. break;
  212. case INTERNET_STATUS_NAME_RESOLVED :
  213. #ifdef _DEBUG
  214. RW_DEBUG << "\nCallback: INTERNET_STATUS_NAME_RESOLVED\n" << flush;
  215. #endif //_DEBUG
  216. break;
  217. case INTERNET_STATUS_CONNECTING_TO_SERVER :
  218. #ifdef _DEBUG
  219. RW_DEBUG << "\nCallback: INTERNET_STATUS_CONNECTING_TO_SERVER\n" << flush;
  220. #endif //_DEBUG
  221. break;
  222. case INTERNET_STATUS_CONNECTED_TO_SERVER :
  223. #ifdef _DEBUG
  224. RW_DEBUG << "\nCallback: INTERNET_STATUS_CONNECTED_TO_SERVER\n" << flush;
  225. #endif //_DEBUG
  226. //::InternetCloseHandle(hInternet);
  227. break;
  228. case INTERNET_STATUS_SENDING_REQUEST :
  229. #ifdef _DEBUG
  230. RW_DEBUG << "\nCallback: INTERNET_STATUS_SENDING_REQUEST\n" << flush;
  231. #endif //_DEBUG
  232. break;
  233. case INTERNET_STATUS_REQUEST_SENT :
  234. #ifdef _DEBUG
  235. RW_DEBUG << "\nCallback: INTERNET_STATUS_REQUEST_SENT\n" << flush;
  236. #endif //_DEBUG
  237. break;
  238. case INTERNET_STATUS_RECEIVING_RESPONSE :
  239. #ifdef _DEBUG
  240. RW_DEBUG << "\nCallback: INTERNET_STATUS_RECEIVING_RESPONSE\n" << flush;
  241. #endif //_DEBUG
  242. break;
  243. case INTERNET_STATUS_RESPONSE_RECEIVED :
  244. #ifdef _DEBUG
  245. RW_DEBUG << "\nCallback: INTERNET_STATUS_RESPONSE_RECEIVED\n" << flush;
  246. #endif //_DEBUG
  247. break;
  248. case INTERNET_STATUS_REDIRECT :
  249. #ifdef _DEBUG
  250. RW_DEBUG << "\nCallback: INTERNET_STATUS_REDIRECT\n" << flush;
  251. #endif //_DEBUG
  252. break;
  253. case INTERNET_STATUS_CLOSING_CONNECTION :
  254. #ifdef _DEBUG
  255. RW_DEBUG << "\nCallback: INTERNET_STATUS_CLOSING_CONNECTION\n" << flush;
  256. #endif //_DEBUG
  257. break;
  258. case INTERNET_STATUS_CONNECTION_CLOSED :
  259. #ifdef _DEBUG
  260. RW_DEBUG << "\nCallback: INTERNET_STATUS_CONNECTION_CLOSED\n" << flush;
  261. #endif //_DEBUG
  262. break;
  263. case INTERNET_STATUS_HANDLE_CREATED :
  264. #ifdef _DEBUG
  265. RW_DEBUG << "\nCallback: INTERNET_STATUS_HANDLE_CREATED\n" << flush;
  266. #endif //_DEBUG
  267. break;
  268. case INTERNET_STATUS_REQUEST_COMPLETE :
  269. #ifdef _DEBUG
  270. RW_DEBUG << "\nCallback: INTERNET_STATUS_REQUEST_COMPLETE\n\n" << flush;
  271. INTERNET_ASYNC_RESULT *x;
  272. x = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
  273. if(x->dwError == ERROR_SUCCESS)
  274. {
  275. RW_DEBUG << "\nERROR_SUCCESS\n" << flush;
  276. }
  277. else
  278. {
  279. RW_DEBUG << "\nERROR \ndwResult: " << (DWORD)x->dwResult <<flush;
  280. }
  281. if(dwStatusInformationLength == ERROR_INTERNET_EXTENDED_ERROR)
  282. {
  283. RW_DEBUG << "\n-NO EXTENDED_ERROR\n" << flush;
  284. }
  285. DWORD dwError;
  286. DWORD dwErrorLen;
  287. _TCHAR szErrorInfo[256];
  288. dwErrorLen = 256;
  289. ATK_InternetGetLastResponseInfo(&dwError,szErrorInfo,&dwErrorLen) ;
  290. RW_DEBUG << "\nErrorNo:" << dwError ;
  291. RW_DEBUG << "\nErrorInfo:"<<szErrorInfo<<flush;
  292. RW_DEBUG << "\n\n" << flush;
  293. #endif //_DEBUG
  294. SetEvent((HANDLE)dwContext);
  295. break;
  296. case INTERNET_STATUS_HANDLE_CLOSING :
  297. #ifdef _DEBUG
  298. RW_DEBUG << "\nCallback: INTERNET_STATUS_HANDLE_CLOSING\n" << flush;
  299. #endif //_DEBUG
  300. SetEvent((HANDLE)dwContext);
  301. break;
  302. #ifdef _DEBUG
  303. default :
  304. RW_DEBUG <<"\n Callback : default " << flush;
  305. #endif //_DEBUG
  306. }
  307. }
  308. #if 0
  309. void main(int argc , char ** argv)
  310. {
  311. if(argc < 2 )
  312. {
  313. RW_DEBUG << "\n\n\nThe Executable Requires the following parameters : \n" << endl;
  314. RW_DEBUG << " \t\tServer Name \n\t\tTimeout value \n\t\tProxyServer name\n\t\tUserName \n\t\tPassword\n\n" << endl;
  315. RW_DEBUG << "Following Parameters are mandatory\n\t\tServer Name \n\t\tTimeout value \n" << endl;
  316. return;
  317. }
  318. BOOL bRet = ChkSiteAvailability( argv[1],atol(argv[2]),
  319. argv[3],argv[4],argv[5] );
  320. if(bRet == TRUE)
  321. {
  322. RW_DEBUG << "Returned TRUE" << flush;
  323. }
  324. else
  325. {
  326. RW_DEBUG << "Returned FALSE" << flush;
  327. }
  328. }
  329. #endif