Source code of Windows XP (NT5)
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.

626 lines
18 KiB

  1. #include <windows.h>
  2. #include <shlobj.h>
  3. #include <shellapi.h>
  4. #include <comctrlp.h>
  5. #include <shlwapi.h>
  6. #include <shlwapip.h>
  7. #include <limits.h>
  8. #include <mstask.h>
  9. extern "C"
  10. {
  11. #include <winsta.h>
  12. #include <syslib.h>
  13. }
  14. #include "appdefs.h"
  15. #include "util.h"
  16. #include "msg.h"
  17. #include <Wininet.h>
  18. #include <limits.h>
  19. #include <activation.h>
  20. #include <licdll.h>
  21. #include <eslerr.h>
  22. #include <LAModes.h>
  23. #include <cherror.h>
  24. #include <ldefines.h>
  25. #define REGSTR_PATH_SYSTEMSETUPKEY L"System\\Setup"
  26. #define REGSTR_VALUE_CMDLINE L"CmdLine"
  27. ICOMLicenseAgent* m_pLicenseAgent;
  28. TCHAR File[MAX_PATH];
  29. TCHAR msg[1024];
  30. BOOL SaveProxySettings(LPTSTR ProxySave);
  31. BOOL RestoreProxySettings(LPTSTR ProxySave);
  32. BOOL ApplyProxySettings(LPTSTR ProxyPath);
  33. void RemoveCmdline(HINSTANCE hInstance);
  34. void OpenLogFile();
  35. void WriteToLog(LPWSTR pszFormatString, ...);
  36. void CloseLogFile();
  37. void RemoveActivationShortCut()
  38. {
  39. HINF hinf;
  40. hinf = SetupOpenInfFile(L"syssetup.inf",NULL,INF_STYLE_WIN4,NULL);
  41. if(hinf != INVALID_HANDLE_VALUE)
  42. {
  43. if (SetupInstallFromInfSection(NULL,
  44. hinf,
  45. L"DEL_OOBE_ACTIVATE",
  46. SPINST_PROFILEITEMS , //SPINST_ALL,
  47. NULL,
  48. NULL,
  49. 0,
  50. NULL,
  51. NULL,
  52. NULL,
  53. NULL) != 0)
  54. {
  55. // Success
  56. WriteToLog(L"Remove Activation shortcut succeeded\r\n");
  57. }
  58. else
  59. {
  60. // Failure
  61. WriteToLog(L"Remove Activation shortcut failed. GetLastError=%1!ld!\r\n",GetLastError());
  62. }
  63. SetupCloseInfFile(hinf);
  64. }
  65. }
  66. HRESULT InitActivation()
  67. {
  68. HRESULT hr = E_FAIL;
  69. //CoCreate LicenseAgent
  70. hr = CoCreateInstance(CLSID_COMLicenseAgent,
  71. NULL,
  72. CLSCTX_INPROC_SERVER,
  73. IID_ICOMLicenseAgent,
  74. (LPVOID*)&m_pLicenseAgent);
  75. return hr;
  76. }
  77. HRESULT ActivationHandShake()
  78. {
  79. DWORD Status;
  80. DWORD dwType;
  81. DWORD Data;
  82. DWORD cbData = sizeof(Data);
  83. MYASSERT( m_pLicenseAgent );
  84. if ( !m_pLicenseAgent ) {
  85. TRACE( L"License Agent is inaccessible" );
  86. WriteToLog(L"License Agent is inaccessible\r\n" );
  87. return ERR_ACT_INTERNAL_WINDOWS_ERR;
  88. }
  89. m_pLicenseAgent->Initialize(
  90. WINDOWSBPC,
  91. LA_MODE_ONLINE_CH,
  92. NULL,
  93. &Status
  94. );
  95. if ( Status != ERROR_SUCCESS ) {
  96. TRACE1( L"m_pLicenseAgent->Initialize() failed. Error = %d", Status );
  97. WriteToLog(L"m_pLicenseAgent->Initialize() failed. Error = %1!ld!\r\n", Status);
  98. return Status;
  99. }
  100. Status = m_pLicenseAgent->SetIsoLanguage( GetSystemDefaultLCID() );
  101. if ( Status != ERROR_SUCCESS ) {
  102. TRACE1( L"m_pLicenseAgent->SetIsoLanguage() failed. Error = %d", Status );
  103. WriteToLog( L"m_pLicenseAgent->SetIsoLanguage() failed. Error = %1!ld!\r\n", Status );
  104. return Status;
  105. }
  106. Status = m_pLicenseAgent->AsyncProcessHandshakeRequest( FALSE );
  107. return Status;
  108. }
  109. HRESULT ActivationLicenseRequest(BOOL bReissueLicense)
  110. {
  111. DWORD Status;
  112. if (bReissueLicense)
  113. {
  114. Status = m_pLicenseAgent->AsyncProcessReissueLicenseRequest();
  115. }
  116. else
  117. {
  118. Status = m_pLicenseAgent->AsyncProcessNewLicenseRequest();
  119. }
  120. if ( Status != ERROR_SUCCESS ) {
  121. TRACE1( L"m_pLicenseAgent->AsyncProcessXxxLicenseRequest() failed. Error = %d", Status );
  122. WriteToLog(L"m_pLicenseAgent->AsyncProcessXxxLicenseRequest() failed. Error = %1!ld!\r\n", Status);
  123. }
  124. return Status;
  125. }
  126. HRESULT WaitForActivationPhase()
  127. {
  128. DWORD Status;
  129. MSG msg;
  130. do
  131. {
  132. m_pLicenseAgent->GetAsyncProcessReturnCode( &Status );
  133. if( LA_ERR_REQUEST_IN_PROGRESS == Status )
  134. {
  135. Sleep(1000);
  136. }
  137. } while (LA_ERR_REQUEST_IN_PROGRESS == Status);
  138. return Status;
  139. }
  140. HRESULT DoActivationEx()
  141. {
  142. static const WCHAR OOBE_HTTP_AGENT_NAME[] =
  143. L"Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1)";
  144. HRESULT hr = E_FAIL;
  145. DWORD WPADaysLeft;
  146. DWORD EvalDaysLeft;
  147. DWORD dwValue = 1;
  148. BOOL bReissueLicense = FALSE;
  149. DWORD time = GetTickCount();
  150. HINTERNET hInternet = NULL;
  151. TRACE(L"DoActivation.");
  152. WriteToLog(L"DoActivation.\r\n");
  153. hInternet = InternetOpen(
  154. OOBE_HTTP_AGENT_NAME,
  155. PRE_CONFIG_INTERNET_ACCESS,
  156. NULL,
  157. NULL,
  158. 0);
  159. if (!hInternet)
  160. {
  161. TRACE(L"InternetOpen Failed.");
  162. WriteToLog(L"InternetOpen Failed.\r\n");
  163. }
  164. else
  165. {
  166. if (!InternetSetOption(
  167. hInternet,
  168. INTERNET_OPTION_DISABLE_AUTODIAL,
  169. &dwValue,
  170. sizeof(DWORD)))
  171. {
  172. dwValue = GetLastError();
  173. TRACE1(L"InternetSetOption INTERNET_OPTION_DISABLE_AUTODIAL Failed. GetLastError=%1!lx!", dwValue);
  174. WriteToLog(L"InternetSetOption INTERNET_OPTION_DISABLE_AUTODIAL Failed. GetLastError=%1!lx!\r\n", dwValue);
  175. }
  176. else
  177. {
  178. hr = S_OK;
  179. }
  180. }
  181. if (hr == S_OK)
  182. {
  183. hr = InitActivation();
  184. }
  185. else
  186. {
  187. TRACE1(L"InitActivation failed with error code:%d.", hr);
  188. WriteToLog(L"InitActivation failed with error code:%1!ld!.\r\n", hr);
  189. }
  190. if (hr == S_OK)
  191. {
  192. hr = m_pLicenseAgent->GetExpirationInfo(
  193. &WPADaysLeft,
  194. &EvalDaysLeft
  195. );
  196. if ( hr == S_OK && WPADaysLeft != INT_MAX )
  197. {
  198. hr = ActivationHandShake();
  199. if (hr == S_OK)
  200. {
  201. hr = WaitForActivationPhase();
  202. TRACE1(L"ActivationHandShake returned with :%d.", hr);
  203. WriteToLog(L"ActivationHandShake returned with :%1!ld!.\r\n", hr);
  204. if ( hr == ESL_ERR_NEW_LICENSE ) {
  205. bReissueLicense = FALSE;
  206. hr = S_OK;
  207. } else if ( hr == ESL_ERR_ORW_REISSUE ) {
  208. bReissueLicense = TRUE;
  209. hr = S_OK;
  210. }
  211. if (hr == S_OK)
  212. {
  213. hr = ActivationLicenseRequest(bReissueLicense);
  214. if (hr == S_OK)
  215. {
  216. hr = WaitForActivationPhase();
  217. TRACE1(L"ActivationLicenseRequest returned with :%d.", hr);
  218. WriteToLog(L"ActivationLicenseRequest returned with :%1!ld!.\r\n", hr);
  219. if (hr == S_OK)
  220. {
  221. RemoveActivationShortCut();
  222. }
  223. }
  224. else
  225. {
  226. TRACE1(L"ActivationLicenseRequest failed with error code:%d.", hr);
  227. WriteToLog(L"ActivationLicenseRequest failed with error code:%1!ld!.\r\n", hr);
  228. }
  229. }
  230. }
  231. else
  232. {
  233. TRACE1(L"ActivationHandShake failed with error code:%d.", hr);
  234. WriteToLog(L"ActivationHandShake failed with error code:%1!ld!.\r\n", hr);
  235. }
  236. }
  237. else
  238. {
  239. TRACE(L"Product already activated.");
  240. WriteToLog(L"Product already activated.\r\n");
  241. }
  242. }
  243. TRACE(L"DoActivation is done.");
  244. WriteToLog(L"DoActivation is done.\r\n");
  245. time = GetTickCount() - time;
  246. WriteToLog(L"Activation took %1!ld! msec.\r\n", time);
  247. if (hInternet)
  248. {
  249. InternetCloseHandle(hInternet);
  250. }
  251. return hr;
  252. }
  253. HRESULT DoActivation(HINSTANCE hInstance)
  254. {
  255. HRESULT hr = E_FAIL;
  256. WCHAR WinntPath[MAX_PATH];
  257. WCHAR ProxyPath[MAX_PATH];
  258. WCHAR ProxySave[MAX_PATH];
  259. WCHAR Answer[50];
  260. BOOL bSaveRestoreProxy = FALSE;
  261. // Need to see if we need to remove the CmdLine reg entry
  262. OpenLogFile();
  263. if(GetCanonicalizedPath(WinntPath, WINNT_INF_FILENAME))
  264. {
  265. // See if we should autoactivate
  266. if( GetPrivateProfileString( TEXT("Unattended"),
  267. TEXT("AutoActivate"),
  268. TEXT(""),
  269. Answer,
  270. sizeof(Answer)/sizeof(TCHAR),
  271. WinntPath ) && !lstrcmpi(Answer, YES_ANSWER))
  272. {
  273. // Check if there is a proxy section specified
  274. if( GetPrivateProfileString( TEXT("Unattended"),
  275. TEXT("ActivateProxy"),
  276. TEXT(""),
  277. Answer,
  278. sizeof(Answer)/sizeof(TCHAR),
  279. WinntPath ) )
  280. {
  281. // We have a section
  282. // Now we should create a temp file with the entries from that section
  283. // and pass them to iedkcs32.dll so that they can get applied to the registry
  284. // Before calling iedkcs32.dll save the registry
  285. if (GetOOBEPath(ProxyPath))
  286. {
  287. WCHAR section[1024];
  288. lstrcpy(ProxySave, ProxyPath);
  289. lstrcat(ProxySave, L"\\oobeact.pry");
  290. lstrcat(ProxyPath, L"\\oobeact.prx");
  291. DeleteFile(ProxyPath);
  292. // Read the proxy section
  293. GetPrivateProfileSection(Answer, section, 1024, WinntPath);
  294. // Write it to the temp file under section [Proxy]
  295. WritePrivateProfileSection(TEXT("Proxy"), section, ProxyPath);
  296. bSaveRestoreProxy = TRUE;
  297. // Save the internet setting registry key
  298. SaveProxySettings(ProxySave);
  299. // Apply the settings
  300. ApplyProxySettings(ProxyPath);
  301. // Don't need the temp file for iedkcs32.dll any more.
  302. DeleteFile(ProxyPath);
  303. }
  304. else
  305. {
  306. WriteToLog(L"Cannot the path for OOBE\r\n");
  307. }
  308. }
  309. hr = DoActivationEx();
  310. if (bSaveRestoreProxy)
  311. {
  312. // Restore the internet settings registry key
  313. RestoreProxySettings(ProxySave);
  314. // Don;t need the file
  315. DeleteFile(ProxySave);
  316. }
  317. }
  318. else
  319. {
  320. WriteToLog(L"No AutoActivate in %1\r\n",WinntPath);
  321. }
  322. }
  323. else
  324. {
  325. WriteToLog(L"Cannot get the location for %1\r\n",WINNT_INF_FILENAME);
  326. }
  327. RemoveCmdline(hInstance);
  328. CloseLogFile();
  329. return hr;
  330. }
  331. BOOL SaveProxySettings(LPTSTR ProxySave)
  332. {
  333. BOOL bRet = FALSE;
  334. HKEY hkey = NULL;
  335. HANDLE Token;
  336. LUID Luid;
  337. TOKEN_PRIVILEGES NewPrivileges;
  338. // Make sure the file does not exist.
  339. DeleteFile(ProxySave);
  340. if(OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&Token))
  341. {
  342. if(LookupPrivilegeValue(NULL,SE_BACKUP_NAME,&Luid))
  343. {
  344. NewPrivileges.PrivilegeCount = 1;
  345. NewPrivileges.Privileges[0].Luid = Luid;
  346. NewPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  347. AdjustTokenPrivileges(Token,FALSE,&NewPrivileges,0,NULL,NULL);
  348. }
  349. }
  350. if (RegOpenKeyEx(HKEY_CURRENT_USER,
  351. TEXT("Software\\Microsoft\\windows\\currentVersion\\Internet Settings"),
  352. 0,
  353. KEY_ALL_ACCESS,
  354. &hkey) == ERROR_SUCCESS)
  355. {
  356. bRet = (RegSaveKey(hkey,ProxySave, NULL) == ERROR_SUCCESS);
  357. RegCloseKey(hkey);
  358. }
  359. WriteToLog(L"SaveProxySettings returned with :%1!ld!.\r\n", bRet);
  360. return bRet;
  361. }
  362. BOOL RestoreProxySettings(LPTSTR ProxySave)
  363. {
  364. BOOL bRet = FALSE;
  365. HKEY hkey = NULL;
  366. HANDLE Token;
  367. LUID Luid;
  368. TOKEN_PRIVILEGES NewPrivileges;
  369. if(OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&Token))
  370. {
  371. if(LookupPrivilegeValue(NULL,SE_RESTORE_NAME,&Luid))
  372. {
  373. NewPrivileges.PrivilegeCount = 1;
  374. NewPrivileges.Privileges[0].Luid = Luid;
  375. NewPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  376. AdjustTokenPrivileges(Token,FALSE,&NewPrivileges,0,NULL,NULL);
  377. }
  378. }
  379. if (RegOpenKeyEx(HKEY_CURRENT_USER,
  380. TEXT("Software\\Microsoft\\windows\\currentVersion\\Internet Settings"),
  381. 0,
  382. KEY_ALL_ACCESS,
  383. &hkey) == ERROR_SUCCESS)
  384. {
  385. bRet = (RegRestoreKey(hkey,
  386. ProxySave,
  387. REG_FORCE_RESTORE) == ERROR_SUCCESS);
  388. RegCloseKey(hkey);
  389. }
  390. WriteToLog(L"RestoreProxySettings returned with :%1!ld!.\r\n", bRet);
  391. return bRet;
  392. }
  393. BOOL ApplyProxySettings(LPTSTR ProxyPath)
  394. {
  395. typedef BOOL (*BRANDINTRAPROC) ( LPCSTR );
  396. // typedef BOOL (*BRANDCLEANSTUBPROC) (HWND, HINSTANCE, LPCSTR, int);
  397. HMODULE IedkHandle = NULL;
  398. BRANDINTRAPROC BrandIntraProc;
  399. // BRANDCLEANSTUBPROC BrandCleanStubProc;
  400. CHAR BrandingFileA[MAX_PATH];
  401. BOOL bRet = FALSE;
  402. __try {
  403. if( IedkHandle = LoadLibrary(L"IEDKCS32") )
  404. {
  405. // BrandCleanStubProc = (BRANDCLEANSTUBPROC) GetProcAddress(IedkHandle,"BrandCleanInstallStubs");
  406. BrandIntraProc = (BRANDINTRAPROC) GetProcAddress(IedkHandle,"BrandIntra");
  407. if( BrandIntraProc )
  408. {
  409. if (!WideCharToMultiByte(
  410. CP_ACP,
  411. 0,
  412. ProxyPath,
  413. -1,
  414. BrandingFileA,
  415. sizeof(BrandingFileA),
  416. NULL,
  417. NULL
  418. ))
  419. {
  420. bRet = FALSE;
  421. }
  422. else
  423. {
  424. // bRet = BrandCleanStubProc( NULL, NULL, "", 0);
  425. // if( bRet )
  426. {
  427. bRet = BrandIntraProc( BrandingFileA );
  428. }
  429. }
  430. }
  431. else
  432. {
  433. bRet = FALSE;
  434. }
  435. FreeLibrary(IedkHandle);
  436. }
  437. else
  438. {
  439. bRet = FALSE;
  440. }
  441. }
  442. __except(EXCEPTION_EXECUTE_HANDLER)
  443. {
  444. bRet = FALSE;
  445. }
  446. WriteToLog(L"ApplyProxySettings returned with :%1!ld!.\r\n", bRet);
  447. return bRet;
  448. }
  449. HANDLE hLogFile = INVALID_HANDLE_VALUE;
  450. void CloseLogFile()
  451. {
  452. if (hLogFile != INVALID_HANDLE_VALUE)
  453. {
  454. CloseHandle(hLogFile);
  455. hLogFile = INVALID_HANDLE_VALUE;
  456. }
  457. }
  458. void OpenLogFile()
  459. {
  460. WCHAR File[MAX_PATH];
  461. DWORD Result;
  462. if (hLogFile == INVALID_HANDLE_VALUE)
  463. {
  464. Result = GetWindowsDirectory( File, MAX_PATH );
  465. if(Result == 0)
  466. {
  467. return;
  468. }
  469. lstrcat(File,TEXT("\\oobeact.log"));
  470. hLogFile = CreateFile(
  471. File,
  472. GENERIC_READ | GENERIC_WRITE,
  473. FILE_SHARE_READ,
  474. NULL,
  475. CREATE_ALWAYS,
  476. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
  477. NULL
  478. );
  479. }
  480. return;
  481. }
  482. void WriteToLog(LPWSTR pszFormatString, ...)
  483. {
  484. va_list args;
  485. LPWSTR pszFullErrMsg = NULL;
  486. DWORD dwBytesWritten;
  487. if (hLogFile != INVALID_HANDLE_VALUE)
  488. {
  489. va_start(args, pszFormatString);
  490. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_STRING,
  491. (LPCVOID) pszFormatString, 0, 0, (LPTSTR) &pszFullErrMsg, 0, &args);
  492. if (pszFullErrMsg)
  493. {
  494. PSTR str;
  495. ULONG Bytes;
  496. Bytes = (wcslen( pszFullErrMsg )*2) + 4;
  497. str = (PSTR)malloc( Bytes );
  498. if (str)
  499. {
  500. WideCharToMultiByte(
  501. CP_ACP,
  502. 0,
  503. pszFullErrMsg,
  504. -1,
  505. str,
  506. Bytes,
  507. NULL,
  508. NULL
  509. );
  510. WriteFile(hLogFile, str, lstrlenA(str), &dwBytesWritten, NULL);
  511. free(str);
  512. }
  513. LocalFree(pszFullErrMsg);
  514. }
  515. }
  516. }
  517. void RemoveCmdline(HINSTANCE hInstance)
  518. {
  519. HKEY hkey;
  520. LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  521. REGSTR_PATH_SYSTEMSETUPKEY,
  522. 0,
  523. KEY_ALL_ACCESS,
  524. &hkey
  525. );
  526. if (lRet == ERROR_SUCCESS)
  527. {
  528. WCHAR rgchCommandLine[MAX_PATH + 1];
  529. DWORD dwSize = sizeof(rgchCommandLine);
  530. LRESULT lResult = RegQueryValueEx(
  531. hkey,
  532. REGSTR_VALUE_CMDLINE,
  533. 0,
  534. NULL,
  535. (LPBYTE)rgchCommandLine,
  536. &dwSize
  537. );
  538. if (ERROR_SUCCESS == lResult)
  539. {
  540. WCHAR file[MAX_PATH];
  541. if (GetModuleFileName(hInstance, file, MAX_PATH) == 0)
  542. {
  543. lstrcpy(file, L"OOBEBALN");
  544. }
  545. // Check if oobebaln is on the cmd line
  546. if (StrStrI(rgchCommandLine, file) != NULL)
  547. {
  548. // Remove the entry
  549. RegDeleteValue(hkey,REGSTR_VALUE_CMDLINE);
  550. }
  551. }
  552. RegCloseKey(hkey);
  553. }
  554. }