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.

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