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.

705 lines
19 KiB

  1. /*-----------------------------------------------------------------------------
  2. autodial.cpp
  3. Main entry point for autodial hook.
  4. Copyright (C) 1996 Microsoft Corporation
  5. All rights reserved.
  6. Authors:
  7. ChrisK ChrisKauffman
  8. History:
  9. 7/22/96 ChrisK Cleaned and formatted
  10. -----------------------------------------------------------------------------*/
  11. #include "pch.hpp"
  12. #include "resource.h"
  13. #include "semaphor.h"
  14. UINT g_cDialAttempts = 0;
  15. UINT g_cHangupDelay = 0;
  16. TCHAR g_szPassword[PWLEN + 1] = TEXT("");
  17. TCHAR g_szEntryName[RAS_MaxEntryName + 1] = TEXT("");
  18. HINSTANCE g_hInstance = NULL;
  19. static LPRASDIALPARAMS lpDialParams = NULL;
  20. // 4/2/97 ChrisK Olympus 296
  21. HANDLE g_hRNAZapperThread = INVALID_HANDLE_VALUE;
  22. typedef struct tagIcwDialShare
  23. {
  24. TCHAR szISPFile[MAX_PATH + 1];
  25. TCHAR szCurrentDUNFile[MAX_PATH + 1];
  26. BYTE fFlags;
  27. BYTE bMask;
  28. DWORD dwCountryID;
  29. WORD wState;
  30. GATHEREDINFO gi;
  31. DWORD dwPlatform;
  32. } ICWDIALSHARE, *PICWDIALSHARE;
  33. static PICWDIALSHARE pDynShare;
  34. LPCTSTR GetISPFile()
  35. {
  36. return pDynShare->szISPFile;
  37. }
  38. void SetCurrentDUNFile(LPCTSTR szDUNFile)
  39. {
  40. lstrcpyn(
  41. pDynShare->szCurrentDUNFile,
  42. szDUNFile,
  43. SIZEOF_TCHAR_BUFFER(pDynShare->szCurrentDUNFile));
  44. }
  45. DWORD GetPlatform()
  46. {
  47. return pDynShare->dwPlatform;
  48. }
  49. LPCTSTR GIGetAppDir()
  50. {
  51. return pDynShare->gi.szAppDir;
  52. }
  53. /********************************************************************
  54. NAME: LibShareEntry
  55. SYNOPSIS: Initialize or uninitialize shared memory of this DLL
  56. NOTES: The share memory replaces the shared section
  57. *********************************************************************/
  58. BOOL LibShareEntry(BOOL fInit)
  59. {
  60. static TCHAR szSharedMemName[] = TEXT("ICWDIAL_SHAREMEMORY");
  61. static HANDLE hSharedMem = 0;
  62. BOOL retval = FALSE;
  63. if (fInit)
  64. {
  65. DWORD dwErr = ERROR_SUCCESS;
  66. SetLastError(0);
  67. hSharedMem = CreateFileMapping(
  68. INVALID_HANDLE_VALUE,
  69. NULL,
  70. PAGE_READWRITE,
  71. 0,
  72. sizeof(ICWDIALSHARE),
  73. szSharedMemName);
  74. dwErr = GetLastError();
  75. switch (dwErr)
  76. {
  77. case ERROR_ALREADY_EXISTS:
  78. case ERROR_SUCCESS:
  79. pDynShare = (PICWDIALSHARE) MapViewOfFile(
  80. hSharedMem,
  81. FILE_MAP_WRITE,
  82. 0,
  83. 0,
  84. 0);
  85. if (pDynShare != NULL)
  86. {
  87. if (dwErr == ERROR_SUCCESS)
  88. {
  89. pDynShare->szISPFile[0] = (TCHAR) 0;
  90. pDynShare->szCurrentDUNFile[0] = (TCHAR) 0;
  91. pDynShare->fFlags = 0;
  92. pDynShare->bMask = 0;
  93. pDynShare->dwCountryID = 0;
  94. pDynShare->wState = 0;
  95. pDynShare->dwPlatform = 0xffffffff;
  96. }
  97. else // dwErr == ERROR_ALREADY_EXISTS
  98. {
  99. // NO initialization needed
  100. }
  101. retval = TRUE;
  102. }
  103. else
  104. {
  105. TraceMsg(TF_ERROR, TEXT("MapViewOfFile failed: 0x%08lx"),
  106. GetLastError());
  107. CloseHandle(hSharedMem);
  108. hSharedMem = 0;
  109. retval = FALSE;
  110. }
  111. break;
  112. default:
  113. TraceMsg(TF_ERROR, TEXT("CreateFileMapping failed: 0x08lx"), dwErr);
  114. hSharedMem = 0;
  115. retval = FALSE;
  116. }
  117. }
  118. else
  119. {
  120. if (pDynShare)
  121. {
  122. UnmapViewOfFile(pDynShare);
  123. pDynShare = NULL;
  124. }
  125. if (hSharedMem)
  126. {
  127. CloseHandle(hSharedMem);
  128. hSharedMem = NULL;
  129. }
  130. retval = TRUE;
  131. }
  132. return retval;
  133. }
  134. //static const CHAR szBrowserClass1[] = "IExplorer_Frame";
  135. //static const CHAR szBrowserClass2[] = "Internet Explorer_Frame";
  136. //static const CHAR szBrowserClass3[] = "IEFrame";
  137. //
  138. // 8/5/97 jmazner Olympus 11215
  139. // Isignup window caption/title is IDS_APP_TITLE in isign32\strings.inc
  140. // IDS_APP_TITLE should be in synch with IDS_TITLE in icwdial.rc
  141. //
  142. static const TCHAR cszIsignupWndClassName[] = TEXT("Internet Signup\0");
  143. static DWORD AutoDialConnect(HWND hDlg, LPRASDIALPARAMS lpDialParams);
  144. static BOOL AutoDialEvent(HWND hDlg, RASCONNSTATE state, LPDWORD lpdwError);
  145. static VOID SetDialogTitle(HWND hDlg, LPCTSTR pszConnectoidName);
  146. static HWND FindBrowser(void);
  147. static UINT RetryMessage(HWND hDlg, DWORD dwError);
  148. #define MAX_RETIES 3
  149. #define irgMaxSzs 5
  150. TCHAR szStrTable[irgMaxSzs][256];
  151. int iSzTable;
  152. /*******************************************************************
  153. NAME: DllEntryPoint
  154. SYNOPSIS: Entry point for DLL.
  155. NOTES: Initializes thunk layer to WIZ16.DLL
  156. ********************************************************************/
  157. extern "C" BOOL _stdcall DllEntryPoint(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpReserved)
  158. {
  159. BOOL retval = TRUE;
  160. TraceMsg(TF_GENERAL, "ICWDIAL :DllEntryPoint()\n");
  161. if( fdwReason == DLL_PROCESS_ATTACH ) {
  162. //
  163. // ChrisK Olympus 6373 6/13/97
  164. // Disable thread attach calls in order to avoid race condition
  165. // on Win95 golden
  166. //
  167. DisableThreadLibraryCalls(hInstDll);
  168. g_hInstance = hInstDll;
  169. retval = LibShareEntry(TRUE);
  170. if (0xFFFFFFFF == pDynShare->dwPlatform)
  171. {
  172. OSVERSIONINFO osver;
  173. ZeroMemory(&osver,sizeof(osver));
  174. osver.dwOSVersionInfoSize = sizeof(osver);
  175. if (GetVersionEx(&osver))
  176. {
  177. pDynShare->dwPlatform = osver.dwPlatformId;
  178. }
  179. }
  180. }
  181. else if (fdwReason == DLL_PROCESS_DETACH)
  182. {
  183. retval = LibShareEntry(FALSE);
  184. }
  185. else if (fdwReason == DLL_THREAD_DETACH)
  186. {
  187. //
  188. // ChrisK 6/3/97 296
  189. // Broaden window to close this thread
  190. //
  191. if (INVALID_HANDLE_VALUE != g_hRNAZapperThread)
  192. {
  193. StopRNAReestablishZapper(g_hRNAZapperThread);
  194. }
  195. }
  196. return retval;
  197. }
  198. // ############################################################################
  199. HRESULT ReleaseBold(HWND hwnd)
  200. {
  201. HFONT hfont = NULL;
  202. hfont = (HFONT)SendMessage(hwnd,WM_GETFONT,0,0);
  203. if (hfont) DeleteObject(hfont);
  204. return ERROR_SUCCESS;
  205. }
  206. // ############################################################################
  207. HRESULT MakeBold (HWND hwnd, BOOL fSize, LONG lfWeight)
  208. {
  209. HRESULT hr = ERROR_SUCCESS;
  210. HFONT hfont = NULL;
  211. HFONT hnewfont = NULL;
  212. LOGFONT* plogfont = NULL;
  213. if (!hwnd) goto MakeBoldExit;
  214. hfont = (HFONT)SendMessage(hwnd,WM_GETFONT,0,0);
  215. if (!hfont)
  216. {
  217. hr = GetLastError();
  218. goto MakeBoldExit;
  219. }
  220. plogfont = (LOGFONT*)GlobalAlloc(GPTR,sizeof(LOGFONT));
  221. if (!plogfont)
  222. {
  223. hr = GetLastError();
  224. goto MakeBoldExit;
  225. }
  226. if (!GetObject(hfont,sizeof(LOGFONT),(LPVOID)plogfont))
  227. {
  228. hr = GetLastError();
  229. goto MakeBoldExit;
  230. }
  231. if (abs(plogfont->lfHeight) < 24 && fSize)
  232. {
  233. plogfont->lfHeight = plogfont->lfHeight + (plogfont->lfHeight / 4);
  234. }
  235. plogfont->lfWeight = lfWeight;
  236. if (!(hnewfont = CreateFontIndirect(plogfont)))
  237. {
  238. hr = GetLastError();
  239. goto MakeBoldExit;
  240. }
  241. SendMessage(hwnd,WM_SETFONT,(WPARAM)hnewfont,MAKELPARAM(TRUE,0));
  242. MakeBoldExit:
  243. if (plogfont) GlobalFree(plogfont);
  244. plogfont = NULL;
  245. // if (hfont) DeleteObject(hfont);
  246. // BUG:? Do I need to delete hnewfont at some time?
  247. return hr;
  248. }
  249. // ############################################################################
  250. // NAME: GetSz
  251. //
  252. // Load strings from resources
  253. //
  254. // Created 1/28/96, Chris Kauffman
  255. // ############################################################################
  256. PTSTR GetSz(WORD wszID)
  257. {
  258. PTSTR psz = szStrTable[iSzTable];
  259. iSzTable++;
  260. if (iSzTable >= irgMaxSzs)
  261. iSzTable = 0;
  262. if (!LoadString(g_hInstance, wszID, psz, 256))
  263. {
  264. TraceMsg(TF_GENERAL, "Autodial:LoadString failed %d\n", (DWORD) wszID);
  265. *psz = 0;
  266. }
  267. return (psz);
  268. }
  269. //+----------------------------------------------------------------------------
  270. //
  271. // Function: IsISignupRunning
  272. //
  273. // Synopsis: Check if ISIGNUP is running
  274. //
  275. // Arguments: none
  276. //
  277. // Returns: TRUE - ISIGNUP is already running
  278. //
  279. // History: 7/24/97 ChrisK fixed part of 8445
  280. //
  281. //-----------------------------------------------------------------------------
  282. BOOL IsISignupRunning()
  283. {
  284. //
  285. // IE 8445 ChrisK 7/24/97
  286. // As part of fixing IE 8445, the ICW was inappropriately deleting the
  287. // isp signup connectoid because it thought isignup was not running.
  288. //
  289. HANDLE hSemaphore;
  290. BOOL bRC = FALSE;
  291. //
  292. // Check the GetLastError value immediately after the CreateSemaphore to
  293. // make sure nothing else changes the error value
  294. //
  295. hSemaphore = CreateSemaphore(NULL, 1, 1, ICW_ELSE_SEMAPHORE);
  296. if( ERROR_ALREADY_EXISTS == GetLastError() )
  297. {
  298. bRC = TRUE;
  299. }
  300. //
  301. // 8/3/97 jmazner Olympus #11206
  302. // Even if the semaphore already exists, we still get back a handle
  303. // reference to it, which means that we need to close that handle
  304. // or else the semaphore will never get destroyed.
  305. //
  306. if( hSemaphore && (hSemaphore != INVALID_HANDLE_VALUE) )
  307. {
  308. CloseHandle(hSemaphore);
  309. hSemaphore = INVALID_HANDLE_VALUE;
  310. }
  311. return bRC;
  312. }
  313. TCHAR szDialogBoxClass[] = TEXT("#32770"); // hard coded dialog class name
  314. // check if ICWCONN1 is running
  315. BOOL IsICWCONN1Running()
  316. {
  317. return (FindWindow(szDialogBoxClass, GetSz(IDS_TITLE)) != NULL);
  318. }
  319. // ############################################################################
  320. typedef HRESULT (WINAPI *PFNINETSETAUTODIAL)(BOOL,LPCTSTR);
  321. void RemoveAutodialer()
  322. {
  323. HINSTANCE hinst = NULL;
  324. FARPROC fp = NULL;
  325. hinst = LoadLibrary(TEXT("INETCFG.DLL"));
  326. if (hinst)
  327. {
  328. if(fp = GetProcAddress(hinst,"InetSetAutodial"))
  329. {
  330. ((PFNINETSETAUTODIAL)fp)(FALSE, TEXT(""));
  331. }
  332. FreeLibrary(hinst);
  333. }
  334. }
  335. // ############################################################################
  336. BOOL WINAPI AutoDialHandler(
  337. HWND hwndParent,
  338. LPCTSTR lpszEntry,
  339. DWORD dwFlags,
  340. LPDWORD pdwRetCode
  341. )
  342. {
  343. HRESULT hr;
  344. INT cRetry;
  345. TCHAR szToDebugOrNot[2];
  346. DWORD dwSize;
  347. RNAAPI *pcRNA = NULL;
  348. PDIALDLGDATA pDD = NULL;
  349. PERRORDLGDATA pDE = NULL;
  350. if(!IsISignupRunning())
  351. {
  352. //
  353. // 7/30/97 ChrisK IE 8445
  354. // In ICW 1.1 icwconn1 is left alive the whole time, so we should not
  355. // care whether or not it is around when we go to dial.
  356. //
  357. //// in some *really* weird circs we can be called while ICWCONN1 is running
  358. //// if so just return failure
  359. //if(IsICWCONN1Running())
  360. //{
  361. // *pdwRetCode = ERROR_CANCELLED;
  362. // return TRUE;
  363. //}
  364. OutputDebugString(TEXT("Someome didn't cleanup ICWDIAL correctly\r\n"));
  365. // clean it up now! delete connectoid
  366. pcRNA = new RNAAPI;
  367. if (pcRNA)
  368. {
  369. pcRNA->RasDeleteEntry(NULL, (LPTSTR)lpszEntry);
  370. delete pcRNA;
  371. pcRNA = NULL;
  372. }
  373. // remove autodial-hook. No clue who to restore, though
  374. RemoveAutodialer();
  375. // return FALSE so someone else will dial
  376. return FALSE;
  377. }
  378. #ifdef _DEBUG
  379. // This is how we can break into the debugger when this DLL is called as
  380. // part of the autodialer sequence
  381. //
  382. lstrcpyn(szToDebugOrNot,TEXT("0"),2);
  383. dwSize = sizeof(szToDebugOrNot);
  384. RegQueryValue(HKEY_LOCAL_MACHINE,TEXT("SOFTWARE\\MICROSOFT\\ISIGNUP\\DEBUG"),szToDebugOrNot,(PLONG)&dwSize);
  385. if (szToDebugOrNot[0] == '1')
  386. DebugBreak();
  387. #endif
  388. // Keep track of EntryName for later
  389. //
  390. lstrcpyn(g_szEntryName, lpszEntry, RAS_MaxEntryName);
  391. if (lstrlen(pDynShare->szISPFile)==0)
  392. {
  393. // if ((*pdwRetCode = LoadInitSettingFromRegistry()) != ERROR_SUCCESS)
  394. // return TRUE;
  395. LoadInitSettingFromRegistry();
  396. }
  397. // g_pdevice = (PMYDEVICE)GlobalAlloc(GPTR,sizeof(MYDEVICE));
  398. // if (!g_pdevice)
  399. // {
  400. // *pdwRetCode = ERROR_NOT_ENOUGH_MEMORY;
  401. // return TRUE;
  402. // }
  403. TryDial:
  404. cRetry = 0;
  405. TryRedial:
  406. if (pDD)
  407. {
  408. GlobalFree(pDD);
  409. pDD = NULL;
  410. }
  411. pDD = (PDIALDLGDATA)GlobalAlloc(GPTR,sizeof(DIALDLGDATA));
  412. if (pDD)
  413. {
  414. pDD->dwSize = sizeof(DIALDLGDATA);
  415. StrDup(&pDD->pszMessage,GetSz(IDS_DIALMESSAGE));
  416. StrDup(&pDD->pszRasEntryName,lpszEntry);
  417. pDD->pfnStatusCallback = StatusMessageCallback;
  418. pDD->hInst = g_hInstance;
  419. } else {
  420. MessageBox(NULL,GetSz(IDS_OUTOFMEMORY),GetSz(IDS_TITLE),MB_MYERROR);
  421. }
  422. // Dial ISP
  423. //
  424. hr = DialingDownloadDialog(pDD);
  425. cRetry++;
  426. // Check if we should automatically redial
  427. //
  428. if ((cRetry < MAX_RETIES) && (FShouldRetry(hr)))
  429. goto TryRedial;
  430. if (hr != ERROR_USERNEXT)
  431. {
  432. pDE = (PERRORDLGDATA)GlobalAlloc(GPTR,sizeof(ERRORDLGDATA));
  433. if (!pDE)
  434. {
  435. MessageBox(NULL,GetSz(IDS_OUTOFMEMORY),GetSz(IDS_TITLE),MB_MYERROR);
  436. } else {
  437. pDE->dwSize = sizeof (ERRORDLGDATA);
  438. StrDup(&pDE->pszMessage,GetSz(RasErrorToIDS(hr)));
  439. StrDup(&pDE->pszRasEntryName,lpszEntry);
  440. pDE->pdwCountryID = &(pDynShare->dwCountryID);
  441. pDE->pwStateID = &(pDynShare->wState);
  442. pDE->bType = pDynShare->fFlags;
  443. pDE->bMask = pDynShare->bMask;
  444. StrDup(&pDE->pszHelpFile,AUTODIAL_HELPFILE);
  445. pDE->dwHelpID = icw_trb;
  446. pDE->hInst = g_hInstance;
  447. pDE->hParentHwnd = NULL;
  448. hr = DialingErrorDialog(pDE);
  449. if (hr == ERROR_USERNEXT)
  450. goto TryDial;
  451. else
  452. hr = ERROR_USERCANCEL;
  453. }
  454. }
  455. GlobalFree(pDD);
  456. pDD = NULL;
  457. if (hr == ERROR_SUCCESS)
  458. *pdwRetCode = ERROR_SUCCESS;
  459. else if (hr == ERROR_USERCANCEL)
  460. *pdwRetCode = ERROR_CANCELLED;
  461. if (ERROR_SUCCESS != *pdwRetCode)
  462. {
  463. HWND hwndIsignup = NULL;
  464. //
  465. // 8/5/97 jmazner Olympus 11215
  466. // For ICW 1.1 and IE 4, looking for the browser won't work
  467. // Instead, look for isignup and send it a special quit message.
  468. //
  469. //hwndBrowser = FindBrowser();
  470. hwndIsignup = FindWindow(cszIsignupWndClassName, GetSz(IDS_TITLE));
  471. if (NULL != hwndIsignup)
  472. {
  473. PostMessage(hwndIsignup, WM_CLOSE, 0, 0);
  474. }
  475. }
  476. return TRUE;
  477. }
  478. // ############################################################################
  479. HRESULT LoadInitSettingFromRegistry()
  480. {
  481. HRESULT hr = ERROR_SUCCESS;
  482. HKEY hKey = NULL;
  483. DWORD dwType, dwSize;
  484. hr = RegOpenKey(HKEY_LOCAL_MACHINE,SIGNUPKEY,&hKey);
  485. if (hr != ERROR_SUCCESS)
  486. {
  487. TraceMsg(TF_ERROR, TEXT("Failed RegOpenKey: %s 0x%08lx"), SIGNUPKEY, hr);
  488. goto LoadInitSettingFromRegistryExit;
  489. }
  490. dwType = REG_BINARY;
  491. dwSize = sizeof(pDynShare->gi);
  492. ZeroMemory(&(pDynShare->gi),sizeof(pDynShare->gi));
  493. hr = RegQueryValueEx(
  494. hKey,
  495. GATHERINFOVALUENAME,
  496. 0,
  497. &dwType,
  498. (LPBYTE) &(pDynShare->gi),
  499. &dwSize);
  500. if (hr != ERROR_SUCCESS)
  501. {
  502. TraceMsg(TF_ERROR, TEXT("Failed RegQueryValueEx: %s 0x%08lx"),
  503. GATHERINFOVALUENAME, hr);
  504. goto LoadInitSettingFromRegistryExit;
  505. }
  506. AutoDialInit(
  507. pDynShare->gi.szISPFile,
  508. pDynShare->gi.fType,
  509. pDynShare->gi.bMask,
  510. pDynShare->gi.dwCountry,
  511. pDynShare->gi.wState);
  512. SetCurrentDirectory(pDynShare->gi.szAppDir);
  513. // Get the name of the DUN file
  514. pDynShare->szCurrentDUNFile[0] = 0;
  515. dwSize = SIZEOF_TCHAR_BUFFER(pDynShare->szCurrentDUNFile);
  516. ReadSignUpReg(
  517. (LPBYTE)pDynShare->szCurrentDUNFile,
  518. &dwSize,
  519. REG_SZ,
  520. DUNFILEVALUENAME);
  521. LoadInitSettingFromRegistryExit:
  522. if (hKey) RegCloseKey(hKey);
  523. return hr;
  524. }
  525. // ############################################################################
  526. /******
  527. *
  528. * 8/5/97 jmazner Olympus 11215
  529. * This function is no longer required
  530. *
  531. static HWND FindBrowser(void)
  532. {
  533. HWND hwnd;
  534. //look for all the microsoft browsers under the sun
  535. if ((hwnd = FindWindow(szBrowserClass1, NULL)) == NULL)
  536. {
  537. if ((hwnd = FindWindow(szBrowserClass2, NULL)) == NULL)
  538. {
  539. hwnd = FindWindow(szBrowserClass3, NULL);
  540. }
  541. }
  542. return hwnd;
  543. }
  544. ****/
  545. // ############################################################################
  546. HRESULT AutoDialInit(LPTSTR lpszISPFile, BYTE fFlags, BYTE bMask, DWORD dwCountry, WORD wState)
  547. {
  548. TraceMsg(TF_GENERAL, "AUTODIAL:AutoDialInit()\n");
  549. if (lpszISPFile) lstrcpyn(pDynShare->szISPFile, lpszISPFile, MAX_PATH);
  550. pDynShare->fFlags = fFlags;
  551. pDynShare->bMask = bMask;
  552. pDynShare->dwCountryID = dwCountry;
  553. pDynShare->wState = wState;
  554. return ERROR_SUCCESS;
  555. }
  556. // ############################################################################
  557. BOOL FShouldRetry(HRESULT hrErr)
  558. {
  559. BOOL bRC;
  560. if (hrErr == ERROR_LINE_BUSY ||
  561. hrErr == ERROR_VOICE_ANSWER ||
  562. hrErr == ERROR_NO_ANSWER ||
  563. hrErr == ERROR_NO_CARRIER ||
  564. hrErr == ERROR_AUTHENTICATION_FAILURE ||
  565. hrErr == ERROR_PPP_TIMEOUT ||
  566. hrErr == ERROR_REMOTE_DISCONNECTION ||
  567. hrErr == ERROR_AUTH_INTERNAL ||
  568. hrErr == ERROR_PROTOCOL_NOT_CONFIGURED ||
  569. hrErr == ERROR_PPP_NO_PROTOCOLS_CONFIGURED)
  570. {
  571. bRC = TRUE;
  572. } else {
  573. bRC = FALSE;
  574. }
  575. return bRC;
  576. }