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.

438 lines
11 KiB

  1. #include "wizard.h"
  2. #define REGSTR_PATH_RUNONCE TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce")
  3. #if !defined (WIN16)
  4. #include <shlobj.h>
  5. static const TCHAR cszICW_StartFileName[] = TEXT("ICWStart.bat");
  6. static const TCHAR cszICW_StartCommand[] = TEXT("@start ");
  7. static const TCHAR cszICW_DummyWndName[] = TEXT("\"ICW\" ");
  8. static const TCHAR cszICW_ExitCommand[] = TEXT("\r\n@exit");
  9. static TCHAR g_cszAppName[257] = TEXT("inetwiz");
  10. //+----------------------------------------------------------------------------
  11. //
  12. // Function SetRunOnce
  13. //
  14. // Synopsis Before we reboot we have to make sure this
  15. // executable is automatically run after startup
  16. //
  17. // Arguments none
  18. //
  19. // Returns: DWORD - status
  20. //
  21. // History:
  22. // MKarki modified - for INETWIZ.EXE
  23. //
  24. //-----------------------------------------------------------------------------
  25. DWORD SetRunOnce (
  26. VOID
  27. )
  28. {
  29. TCHAR szTemp[MAX_PATH + MAX_PATH + 1];
  30. TCHAR szTemp2[MAX_PATH + 1];
  31. DWORD dwRet = ERROR_CANTREAD;
  32. HKEY hKey;
  33. LPTSTR lpszFilePart;
  34. //
  35. // get the name of the executable
  36. //
  37. if (GetModuleFileName(NULL,szTemp2,ARRAYSIZE(szTemp2)) != 0)
  38. {
  39. NULL_TERM_TCHARS(szTemp2);
  40. GetShortPathName (szTemp2, szTemp, ARRAYSIZE(szTemp));
  41. NULL_TERM_TCHARS(szTemp);
  42. //
  43. // Determine Version of the OS we are runningon
  44. //
  45. OSVERSIONINFO osvi;
  46. ZeroMemory(&osvi,sizeof(OSVERSIONINFO));
  47. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  48. if (!GetVersionEx(&osvi))
  49. {
  50. ZeroMemory(&osvi,sizeof(OSVERSIONINFO));
  51. }
  52. if (VER_PLATFORM_WIN32_NT == osvi.dwPlatformId)
  53. {
  54. //
  55. // if running on NT than copy the command into a
  56. // batch file to be run after reboot
  57. //
  58. dwRet = SetStartUpCommand (szTemp);
  59. }
  60. else
  61. {
  62. //
  63. // in case of Win95 we can safely put our path
  64. // in the RUNONCE registry key
  65. //
  66. dwRet = RegCreateKey (
  67. HKEY_LOCAL_MACHINE,
  68. REGSTR_PATH_RUNONCE,
  69. &hKey
  70. );
  71. if (ERROR_SUCCESS == dwRet)
  72. {
  73. dwRet = RegSetValueEx (
  74. hKey,
  75. g_cszAppName,
  76. 0L,
  77. REG_SZ,
  78. (LPBYTE)szTemp,
  79. sizeof(szTemp)
  80. );
  81. RegCloseKey (hKey);
  82. }
  83. }
  84. }
  85. return (dwRet);
  86. } // end of SetRunOnce function
  87. //+----------------------------------------------------------------------------
  88. //
  89. // Function SetStartUpCommand
  90. //
  91. // Synopsis On an NT machine the RunOnce method is not reliable. Therefore
  92. // we will restart the ICW by placing a .BAT file in the common
  93. // startup directory.
  94. //
  95. // Arguments lpCmd - command line used to restart the ICW
  96. //
  97. // Returns: BOOL - success/failure
  98. //
  99. //
  100. // History 1-10-97 ChrisK Created
  101. // 5/2/97 MKarki modified for INETWIZ
  102. //
  103. //-----------------------------------------------------------------------------
  104. BOOL
  105. SetStartUpCommand (
  106. LPTSTR lpCmd
  107. )
  108. {
  109. BOOL bRC = FALSE;
  110. HANDLE hFile = INVALID_HANDLE_VALUE ;
  111. DWORD dwWLen; // dummy variable used to make WriteFile happy
  112. TCHAR szCommandLine[MAX_PATH + 1];
  113. LPITEMIDLIST lpItemDList = NULL;
  114. HRESULT hr = ERROR_SUCCESS;
  115. BOOL bRetVal = FALSE;
  116. IMalloc *pMalloc = NULL;
  117. //
  118. // build full filename
  119. //
  120. hr = SHGetSpecialFolderLocation(NULL,CSIDL_COMMON_STARTUP,&lpItemDList);
  121. if (ERROR_SUCCESS != hr)
  122. goto SetStartUpCommandExit;
  123. if (FALSE == SHGetPathFromIDList(lpItemDList, szCommandLine))
  124. goto SetStartUpCommandExit;
  125. //
  126. // Free up the memory allocated for LPITEMIDLIST
  127. // because seems like we are clobberig something later
  128. // by not freeing this
  129. //
  130. hr = SHGetMalloc (&pMalloc);
  131. if (SUCCEEDED (hr))
  132. {
  133. pMalloc->Free (lpItemDList);
  134. pMalloc->Release ();
  135. }
  136. //
  137. // make sure there is a trailing \ character
  138. //
  139. if ('\\' != szCommandLine[lstrlen(szCommandLine)-1])
  140. lstrcat(szCommandLine,TEXT("\\"));
  141. lstrcat(szCommandLine,cszICW_StartFileName);
  142. //
  143. // Open file
  144. //
  145. hFile = CreateFile (
  146. szCommandLine,
  147. GENERIC_WRITE,
  148. 0,
  149. 0,
  150. CREATE_ALWAYS,
  151. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
  152. NULL
  153. );
  154. if (INVALID_HANDLE_VALUE == hFile)
  155. goto SetStartUpCommandExit;
  156. //
  157. // Write the restart commands to the file
  158. //
  159. bRetVal = WriteFile(
  160. hFile,
  161. cszICW_StartCommand,
  162. lstrlen(cszICW_StartCommand),
  163. &dwWLen,
  164. NULL
  165. );
  166. if (FALSE == bRetVal)
  167. goto SetStartUpCommandExit;
  168. //
  169. // 1/20/96 jmazner Normandy #13287
  170. // Start command considers the first thing it sees
  171. // in quotes to be a window title
  172. // So, since our path is in quotes, put in a fake window title
  173. //
  174. bRetVal = WriteFile (
  175. hFile,
  176. cszICW_DummyWndName,
  177. lstrlen(cszICW_DummyWndName),
  178. &dwWLen,NULL
  179. );
  180. if (FALSE == bRetVal)
  181. goto SetStartUpCommandExit;
  182. //
  183. // write the path name of the executable now
  184. //
  185. bRetVal = WriteFile (
  186. hFile,
  187. lpCmd,
  188. lstrlen(lpCmd),
  189. &dwWLen,
  190. NULL
  191. );
  192. if (FALSE == bRetVal)
  193. goto SetStartUpCommandExit;
  194. //
  195. // write the exit command in the next line
  196. //
  197. bRetVal = WriteFile (
  198. hFile,
  199. cszICW_ExitCommand,
  200. lstrlen (cszICW_ExitCommand),
  201. &dwWLen,
  202. NULL
  203. );
  204. if (FALSE == bRetVal)
  205. goto SetStartUpCommandExit;
  206. bRC = TRUE;
  207. SetStartUpCommandExit:
  208. //
  209. // Close handle and exit
  210. //
  211. if (INVALID_HANDLE_VALUE != hFile)
  212. CloseHandle(hFile);
  213. return bRC;
  214. } // end of SetStartUpCommand function
  215. //+----------------------------------------------------------------------------
  216. //
  217. // Function: DeleteStartUpCommand
  218. //
  219. // Synopsis: After restart the ICW we need to delete the .bat file from
  220. // the common startup directory
  221. //
  222. // Arguements: None
  223. //
  224. // Returns: None
  225. //
  226. // History: 1-10-97 ChrisK Created
  227. //
  228. //-----------------------------------------------------------------------------
  229. VOID DeleteStartUpCommand (
  230. VOID
  231. )
  232. {
  233. TCHAR szStartUpFile[MAX_PATH + 1];
  234. LPITEMIDLIST lpItemDList = NULL;
  235. HRESULT hr = ERROR_SUCCESS;
  236. IMalloc *pMalloc = NULL;
  237. //
  238. // Sleep for 10 seconds
  239. //
  240. // build full filename
  241. //
  242. hr = SHGetSpecialFolderLocation(NULL,CSIDL_COMMON_STARTUP,&lpItemDList);
  243. if (ERROR_SUCCESS != hr)
  244. goto DeleteStartUpCommandExit;
  245. if (FALSE == SHGetPathFromIDList(lpItemDList, szStartUpFile))
  246. goto DeleteStartUpCommandExit;
  247. //
  248. // Free up the memory allocated for LPITEMIDLIST
  249. // because seems like we are clobberig something later
  250. // by not freeing this
  251. //
  252. hr = SHGetMalloc (&pMalloc);
  253. if (SUCCEEDED (hr))
  254. {
  255. pMalloc->Free (lpItemDList);
  256. pMalloc->Release ();
  257. }
  258. //
  259. // make sure there is a trailing \ character
  260. //
  261. if ('\\' != szStartUpFile[lstrlen(szStartUpFile)-1])
  262. lstrcat(szStartUpFile,TEXT("\\"));
  263. lstrcat(szStartUpFile,cszICW_StartFileName);
  264. //
  265. // we dont care if the file does not exist
  266. //
  267. DeleteFile(szStartUpFile);
  268. DeleteStartUpCommandExit:
  269. return;
  270. } // end of DeleteStartUpCommand function
  271. #endif // !defined (WIN16)
  272. //+----------------------------------------------------------------------------
  273. //
  274. // Function: FGetSystemShutdownPrivledge
  275. //
  276. // Synopsis: For windows NT the process must explicitly ask for permission
  277. // to reboot the system.
  278. //
  279. // Arguements: none
  280. //
  281. // Return: TRUE - privledges granted
  282. // FALSE - DENIED
  283. //
  284. // History: 8/14/96 ChrisK Created
  285. //
  286. // Note: BUGBUG for Win95 we are going to have to softlink to these
  287. // entry points. Otherwise the app won't even load.
  288. // Also, this code was originally lifted out of MSDN July96
  289. // "Shutting down the system"
  290. //-----------------------------------------------------------------------------
  291. BOOL
  292. FGetSystemShutdownPrivledge (
  293. VOID
  294. )
  295. {
  296. HANDLE hToken = NULL;
  297. TOKEN_PRIVILEGES tkp;
  298. BOOL bRC = FALSE;
  299. if (IsNT())
  300. {
  301. //
  302. // Get the current process token handle
  303. // so we can get shutdown privilege.
  304. //
  305. if (!OpenProcessToken(GetCurrentProcess(),
  306. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  307. goto FGetSystemShutdownPrivledgeExit;
  308. //
  309. // Get the LUID for shutdown privilege.
  310. //
  311. ZeroMemory(&tkp,sizeof(tkp));
  312. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
  313. &tkp.Privileges[0].Luid);
  314. tkp.PrivilegeCount = 1; /* one privilege to set */
  315. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  316. //
  317. // Get shutdown privilege for this process.
  318. //
  319. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  320. (PTOKEN_PRIVILEGES) NULL, 0);
  321. if (ERROR_SUCCESS == GetLastError())
  322. bRC = TRUE;
  323. }
  324. else
  325. {
  326. bRC = TRUE;
  327. }
  328. FGetSystemShutdownPrivledgeExit:
  329. if (hToken) CloseHandle(hToken);
  330. return bRC;
  331. }
  332. //+-------------------------------------------------------------------
  333. //
  334. // Function: IsNT
  335. //
  336. // Synopsis: findout If we are running on NT
  337. //
  338. // Arguements: none
  339. //
  340. // Return: TRUE - Yes
  341. // FALSE - No
  342. //
  343. //--------------------------------------------------------------------
  344. BOOL
  345. IsNT (
  346. VOID
  347. )
  348. {
  349. OSVERSIONINFO OsVersionInfo;
  350. ZeroMemory(&OsVersionInfo, sizeof(OSVERSIONINFO));
  351. OsVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  352. GetVersionEx(&OsVersionInfo);
  353. return (VER_PLATFORM_WIN32_NT == OsVersionInfo.dwPlatformId);
  354. } //end of IsNT function call
  355. //+-------------------------------------------------------------------
  356. //
  357. // Function: IsNT5
  358. //
  359. // Synopsis: findout If we are running on NT5
  360. //
  361. // Arguements: none
  362. //
  363. // Return: TRUE - Yes
  364. // FALSE - No
  365. //
  366. //--------------------------------------------------------------------
  367. BOOL
  368. IsNT5 (
  369. VOID
  370. )
  371. {
  372. OSVERSIONINFO OsVersionInfo;
  373. ZeroMemory(&OsVersionInfo, sizeof(OSVERSIONINFO));
  374. OsVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  375. GetVersionEx(&OsVersionInfo);
  376. return ((VER_PLATFORM_WIN32_NT == OsVersionInfo.dwPlatformId) && (OsVersionInfo.dwMajorVersion >= 5));
  377. } //end of IsNT function call