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.

348 lines
9.5 KiB

  1. //*********************************************************************
  2. //* Microsoft Windows **
  3. //* Copyright(c) Microsoft Corp., 1994 **
  4. //*********************************************************************
  5. //
  6. // INIT.C - WinMain and initialization code for Internet setup/signup wizard
  7. //
  8. // HISTORY:
  9. //
  10. // 11/20/94 jeremys Created.
  11. // 96/03/26 markdu Put #ifdef __cplusplus around extern "C"
  12. //
  13. #include "wizard.h"
  14. #ifdef WIN32
  15. #include "..\inc\semaphor.h"
  16. #endif
  17. #define IEAK_RESTRICTION_REGKEY TEXT("Software\\Policies\\Microsoft\\Internet Explorer\\Control Panel")
  18. #define IEAK_RESTRICTION_REGKEY_VALUE TEXT("Connwiz Admin Lock")
  19. // superceded by definition in semaphor.h
  20. //#define SEMAPHORE_NAME "Internet Connection Wizard INETWIZ.EXE"
  21. HINSTANCE ghInstance=NULL;
  22. LPTSTR LoadSz(UINT idString,LPTSTR lpszBuf,UINT cbBuf);
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif // __cplusplus
  27. VOID WINAPI LaunchSignupWizard(LPTSTR lpCmdLine,int nCmdShow, PBOOL pReboot);
  28. #ifdef __cplusplus
  29. }
  30. #endif // __cplusplus
  31. BOOL DoesUserHaveAdminPrivleges(HINSTANCE hInstance)
  32. {
  33. HKEY hKey = NULL;
  34. BOOL bRet = FALSE;
  35. if (!IsNT())
  36. return TRUE;
  37. // BUGBUG: We should allow NT5 to run in all user groups
  38. // except normal users.
  39. if (IsNT5())
  40. return TRUE;
  41. //
  42. // Ensure caller is an administrator on this machine.
  43. //
  44. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  45. (TCHAR*)TEXT("SYSTEM\\CurrentControlSet"),
  46. 0,
  47. KEY_ALL_ACCESS,
  48. &hKey))
  49. {
  50. bRet = TRUE;
  51. CloseHandle(hKey);
  52. }
  53. else
  54. {
  55. TCHAR szAdminDenied [MAX_PATH] = TEXT("\0");
  56. TCHAR szAdminDeniedTitle [MAX_PATH] = TEXT("\0");
  57. LoadString(hInstance, IDS_ADMIN_ACCESS_DENIED, szAdminDenied, MAX_PATH);
  58. LoadString(hInstance, IDS_ADMIN_ACCESS_DENIED_TITLE, szAdminDeniedTitle, MAX_PATH);
  59. MessageBox(NULL, szAdminDenied, szAdminDeniedTitle, MB_OK | MB_ICONSTOP);
  60. }
  61. return bRet;
  62. }
  63. BOOL CheckForIEAKRestriction(HINSTANCE hInstance)
  64. {
  65. HKEY hkey = NULL;
  66. BOOL bRC = FALSE;
  67. DWORD dwType = 0;
  68. DWORD dwSize = 0;
  69. DWORD dwData = 0;
  70. if (ERROR_SUCCESS == RegOpenKey(HKEY_CURRENT_USER,
  71. IEAK_RESTRICTION_REGKEY,&hkey))
  72. {
  73. dwSize = sizeof(dwData);
  74. if (ERROR_SUCCESS == RegQueryValueEx(hkey,IEAK_RESTRICTION_REGKEY_VALUE,0,&dwType,
  75. (LPBYTE)&dwData,&dwSize))
  76. {
  77. if (dwData)
  78. {
  79. TCHAR szIEAKDenied[MAX_PATH];
  80. TCHAR szIEAKDeniedTitle[MAX_PATH];
  81. LoadString(hInstance, IDS_IEAK_ACCESS_DENIED, szIEAKDenied, MAX_PATH);
  82. LoadString(hInstance, IDS_IEAK_ACCESS_DENIED_TITLE, szIEAKDeniedTitle, MAX_PATH);
  83. MessageBox(NULL, szIEAKDenied, szIEAKDeniedTitle, MB_OK | MB_ICONSTOP);
  84. bRC = TRUE;
  85. }
  86. }
  87. }
  88. if (hkey)
  89. RegCloseKey(hkey);
  90. return bRC;
  91. }
  92. /*******************************************************************
  93. NAME: WinMain
  94. SYNOPSIS: App entry point
  95. ********************************************************************/
  96. int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
  97. LPSTR lpCmdLine,int nCmdShow)
  98. {
  99. BOOL bReboot = FALSE;
  100. #ifdef UNICODE
  101. // Initialize the C runtime locale to the system locale.
  102. setlocale(LC_ALL, "");
  103. #endif
  104. // only allow one instance of wizard
  105. // note: hPrevInstance is always NULL for Win32 apps, so need to look
  106. // for existing window
  107. HANDLE hSemaphore = NULL;
  108. hSemaphore = CreateSemaphore(NULL, 1, 1, ICW_ELSE_SEMAPHORE);
  109. DWORD dwErr = GetLastError();
  110. if ( ERROR_ALREADY_EXISTS == dwErr )
  111. {
  112. IsAnotherComponentRunning32( FALSE );
  113. // every instance should close its own semaphore handle
  114. goto WinMainExit;
  115. }
  116. else
  117. {
  118. if( IsAnotherComponentRunning32( TRUE ) )
  119. goto WinMainExit;
  120. }
  121. //
  122. // remove the batch files if we are running of NT and
  123. // started of 1
  124. //
  125. if (IsNT ())
  126. DeleteStartUpCommand();
  127. if (!DoesUserHaveAdminPrivleges(hInstance))
  128. //no, bail.
  129. goto WinMainExit;
  130. if (CheckForIEAKRestriction(hInstance))
  131. //yes, bail.
  132. goto WinMainExit;
  133. //
  134. // call our DLL to run the wizard
  135. //
  136. #ifdef UNICODE
  137. TCHAR szCmdLine[MAX_PATH+1];
  138. mbstowcs(szCmdLine, lpCmdLine, MAX_PATH+1);
  139. LaunchSignupWizard(szCmdLine, nCmdShow, &bReboot);
  140. #else
  141. LaunchSignupWizard(lpCmdLine, nCmdShow, &bReboot);
  142. #endif
  143. //
  144. // we should reboot, if the flag is set
  145. //
  146. if (TRUE == bReboot)
  147. {
  148. //
  149. // as the user if we would like to reboot at this time
  150. //
  151. TCHAR szMessage[256];
  152. TCHAR szTitle[256];
  153. LoadSz (IDS_WANTTOREBOOT, szMessage, sizeof (szMessage));
  154. LoadSz (IDS_WIZ_WINDOW_NAME, szTitle, sizeof (szTitle));
  155. if (IDYES == MessageBox(NULL, szMessage, szTitle, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2))
  156. {
  157. SetRunOnce ();
  158. if (!FGetSystemShutdownPrivledge() ||
  159. !ExitWindowsEx(EWX_REBOOT,0))
  160. {
  161. TCHAR szFailMessage[256];
  162. LoadSz (IDS_EXITFAILED, szFailMessage, sizeof (szFailMessage));
  163. MessageBox(NULL, szFailMessage, szTitle, MB_ICONERROR | MB_OK);
  164. }
  165. //
  166. // ChrisK Olympus 4212, Allow Inetwiz to exit on restart
  167. //
  168. //else
  169. //{
  170. // //
  171. // // We will wait for the System to release all
  172. // // resources, 5 minutes should be more than
  173. // // enough for this
  174. // // - MKarki (4/22/97) Fix for Bug #3109
  175. // //
  176. // Sleep (300000);
  177. //}
  178. }
  179. }
  180. WinMainExit:
  181. if( hSemaphore )
  182. CloseHandle(hSemaphore);
  183. return 0;
  184. }
  185. /*******************************************************************
  186. NAME: LoadSz
  187. SYNOPSIS: Loads specified string resource into buffer
  188. EXIT: returns a pointer to the passed-in buffer
  189. NOTES: If this function fails (most likely due to low
  190. memory), the returned buffer will have a leading NULL
  191. so it is generally safe to use this without checking for
  192. failure.
  193. ********************************************************************/
  194. LPTSTR LoadSz(UINT idString,LPTSTR lpszBuf,UINT cbBuf)
  195. {
  196. ASSERT(lpszBuf);
  197. // Clear the buffer and load the string
  198. if ( lpszBuf )
  199. {
  200. *lpszBuf = '\0';
  201. LoadString( ghInstance, idString, lpszBuf, cbBuf );
  202. }
  203. return lpszBuf;
  204. }
  205. #ifdef WIN32
  206. //+---------------------------------------------------------------------------
  207. //
  208. // Function: IsAnotherComponentRunning32()
  209. //
  210. // Synopsis: Checks if there's another ICW component already
  211. // running. If so, it will set focus to that component's window.
  212. //
  213. // This functionality is needed by all of our .exe's. However,
  214. // the actual components to check for differ between .exe's.
  215. // The comment COMPONENT SPECIFIC designates lines of code
  216. // that vary between components' source code.
  217. //
  218. // For ICWCONN1, we return FALSE if the following are already running:
  219. // another instance of ICWCONN1, any other component
  220. //
  221. // Arguments: bCreatedSemaphore --
  222. // -- if TRUE, then this component has successfully created
  223. // it's semaphore, and we want to check whether other
  224. // components are running
  225. // -- if FALSE, then this component could not create
  226. // it's semaphore, and we want to find the already
  227. // running instance of this component
  228. //
  229. // Returns: TRUE if another component is already running
  230. // FALSE otherwise
  231. //
  232. // History: 12/3/96 jmazner Created, with help from IsAnotherInstanceRunning
  233. // in icwconn1\connmain.cpp
  234. //
  235. //----------------------------------------------------------------------------
  236. BOOL IsAnotherComponentRunning32(BOOL bCreatedSemaphore)
  237. {
  238. HWND hWnd = NULL;
  239. HANDLE hSemaphore = NULL;
  240. DWORD dwErr = 0;
  241. TCHAR szWindowName[SMALL_BUF_LEN+1];
  242. if( !bCreatedSemaphore )
  243. {
  244. // something other than conn1 is running,
  245. // let's find it!
  246. // COMPONENT SPECIFIC
  247. LoadSz(IDS_WIZ_WINDOW_NAME,szWindowName,sizeof(szWindowName));
  248. hWnd = FindWindow(DIALOG_CLASS_NAME, szWindowName);
  249. if( hWnd )
  250. {
  251. SetFocus(hWnd);
  252. SetForegroundWindow(hWnd);
  253. }
  254. // regardless of whether we found the window, return TRUE
  255. // since bCreatedSemaphore tells us this component was already running.
  256. return TRUE;
  257. }
  258. else
  259. {
  260. // check whether CONN1 is running
  261. // The particular semaphores we look for are
  262. // COMPONENT SPECIFIC
  263. // is conn1 running?
  264. hSemaphore = CreateSemaphore(NULL, 1, 1, ICWCONN1_SEMAPHORE);
  265. dwErr = GetLastError();
  266. // close the semaphore right away since we have no use for it.
  267. if( hSemaphore )
  268. CloseHandle(hSemaphore);
  269. if( ERROR_ALREADY_EXISTS == dwErr )
  270. {
  271. // conn1 is running.
  272. // Bring the running instance's window to the foreground
  273. LoadSz(IDS_WIZ_WINDOW_NAME,szWindowName,sizeof(szWindowName));
  274. hWnd = FindWindow(DIALOG_CLASS_NAME, szWindowName);
  275. if( hWnd )
  276. {
  277. SetFocus(hWnd);
  278. SetForegroundWindow(hWnd);
  279. }
  280. return( TRUE );
  281. }
  282. // didn't find any other running components!
  283. return( FALSE );
  284. }
  285. }
  286. #endif