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.

673 lines
18 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. //
  5. // File: forward.cpp
  6. //
  7. // Contents: Forward license request.
  8. //
  9. // History:
  10. //
  11. //---------------------------------------------------------------------------
  12. #include "pch.cpp"
  13. #include "globals.h"
  14. #include "srvlist.h"
  15. #include "forward.h"
  16. #include "keypack.h"
  17. #include "postjob.h"
  18. //-----------------------------------------------------------------------
  19. CopyRpcTLSRequestToRequest(
  20. TLSLICENSEREQUEST* lpRpcRequest ,
  21. LICENSEREQUEST* lpRequest
  22. )
  23. /*++
  24. --*/
  25. {
  26. lpRequest->cbEncryptedHwid = lpRpcRequest->cbEncryptedHwid;
  27. lpRequest->pbEncryptedHwid = lpRpcRequest->pbEncryptedHwid;
  28. lpRequest->dwLanguageID = lpRpcRequest->dwLanguageID;
  29. lpRequest->dwPlatformID = lpRpcRequest->dwPlatformID;
  30. lpRequest->pProductInfo->dwVersion = lpRpcRequest->ProductInfo.dwVersion;
  31. lpRequest->pProductInfo->cbCompanyName = lpRpcRequest->ProductInfo.cbCompanyName;
  32. lpRequest->pProductInfo->pbCompanyName = lpRpcRequest->ProductInfo.pbCompanyName;
  33. lpRequest->pProductInfo->cbProductID = lpRpcRequest->ProductInfo.cbProductID;
  34. lpRequest->pProductInfo->pbProductID = lpRpcRequest->ProductInfo.pbProductID;
  35. return ERROR_SUCCESS;
  36. }
  37. //-----------------------------------------------------------------------
  38. DWORD
  39. ForwardUpgradeLicenseRequest(
  40. IN LPTSTR pszServerSetupId,
  41. IN OUT DWORD *pdwSupportFlags,
  42. IN TLSLICENSEREQUEST* pRequest,
  43. IN CHALLENGE_CONTEXT ChallengeContext,
  44. IN DWORD cbChallengeResponse,
  45. IN PBYTE pbChallengeResponse,
  46. IN DWORD cbOldLicense,
  47. IN PBYTE pbOldLicense,
  48. OUT PDWORD pcbNewLicense,
  49. OUT PBYTE* ppbNewLicense,
  50. OUT PDWORD pdwErrCode
  51. )
  52. /*++
  53. --*/
  54. {
  55. TLS_HANDLE hHandle = NULL;
  56. DWORD dwStatus;
  57. LICENSEREQUEST LicenseRequest;
  58. BYTE pbEncryptedHwid[1024]; // encrypted HWID can't be more than 1024
  59. TCHAR szCompanyName[LSERVER_MAX_STRING_SIZE+2];
  60. TCHAR szProductId[LSERVER_MAX_STRING_SIZE+2];
  61. Product_Info ProductInfo;
  62. TLServerInfo ServerInfo;
  63. DBGPrintf(
  64. DBG_INFORMATION,
  65. DBG_FACILITY_RPC,
  66. DBGLEVEL_FUNCTION_TRACE,
  67. _TEXT("ForwardUpgradeLicenseRequest() ...\n")
  68. );
  69. dwStatus = TLSLookupRegisteredServer(
  70. pszServerSetupId,
  71. NULL,
  72. NULL,
  73. &ServerInfo
  74. );
  75. if(dwStatus != ERROR_SUCCESS)
  76. {
  77. goto cleanup;
  78. }
  79. if(TLSCanForwardRequest(
  80. TLS_CURRENT_VERSION,
  81. ServerInfo.GetServerVersion()
  82. ) == FALSE)
  83. {
  84. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  85. goto cleanup;
  86. }
  87. DBGPrintf(
  88. DBG_INFORMATION,
  89. DBG_FACILITY_RPC,
  90. DBGLEVEL_FUNCTION_TRACE,
  91. _TEXT("ForwardUpgradeLicenseRequest() to %s %s\n"),
  92. pszServerSetupId,
  93. ServerInfo.GetServerName()
  94. );
  95. hHandle = TLSConnectToServerWithServerId(pszServerSetupId);
  96. if(hHandle == NULL)
  97. {
  98. //
  99. // server not available
  100. //
  101. dwStatus = GetLastError();
  102. goto cleanup;
  103. }
  104. //
  105. // RPC AC if pass in the original address
  106. // TlsRequestToRequest(pTlsRequest, &LicenseRequest);
  107. if(pRequest->cbEncryptedHwid >= sizeof(pbEncryptedHwid))
  108. {
  109. TLSASSERT(FALSE);
  110. SetLastError(dwStatus = ERROR_INVALID_DATA);
  111. goto cleanup;
  112. }
  113. if(pRequest->ProductInfo.cbCompanyName >= sizeof(szCompanyName) ||
  114. pRequest->ProductInfo.cbProductID >= sizeof(szProductId) )
  115. {
  116. TLSASSERT(FALSE);
  117. SetLastError(dwStatus = ERROR_INVALID_DATA);
  118. goto cleanup;
  119. }
  120. memset(&LicenseRequest, 0, sizeof(LicenseRequest));
  121. memcpy(
  122. pbEncryptedHwid,
  123. pRequest->pbEncryptedHwid,
  124. pRequest->cbEncryptedHwid
  125. );
  126. LicenseRequest.pbEncryptedHwid = pbEncryptedHwid;
  127. LicenseRequest.cbEncryptedHwid = pRequest->cbEncryptedHwid;
  128. LicenseRequest.dwLanguageID = pRequest->dwLanguageID;
  129. LicenseRequest.dwPlatformID = pRequest->dwPlatformID;
  130. LicenseRequest.pProductInfo = &ProductInfo;
  131. ProductInfo.dwVersion = pRequest->ProductInfo.dwVersion;
  132. ProductInfo.pbCompanyName = (PBYTE)szCompanyName;
  133. ProductInfo.pbProductID = (PBYTE)szProductId;
  134. ProductInfo.cbCompanyName = pRequest->ProductInfo.cbCompanyName;
  135. ProductInfo.cbProductID = pRequest->ProductInfo.cbProductID;
  136. memset(szCompanyName, 0, sizeof(szCompanyName));
  137. memset(szProductId, 0, sizeof(szProductId));
  138. memcpy(
  139. szCompanyName,
  140. pRequest->ProductInfo.pbCompanyName,
  141. pRequest->ProductInfo.cbCompanyName
  142. );
  143. memcpy(
  144. szProductId,
  145. pRequest->ProductInfo.pbProductID,
  146. pRequest->ProductInfo.cbProductID
  147. );
  148. if(IsServiceShuttingdown() == TRUE)
  149. {
  150. dwStatus = TLS_I_SERVICE_STOP;
  151. }
  152. else
  153. {
  154. dwStatus = TLSUpgradeLicenseEx(
  155. hHandle,
  156. pdwSupportFlags,
  157. &LicenseRequest,
  158. ChallengeContext,
  159. cbChallengeResponse,
  160. pbChallengeResponse,
  161. cbOldLicense,
  162. pbOldLicense,
  163. 1, // dwQuantity
  164. pcbNewLicense,
  165. ppbNewLicense,
  166. pdwErrCode
  167. );
  168. }
  169. cleanup:
  170. if(hHandle)
  171. {
  172. TLSDisconnectFromServer(hHandle);
  173. }
  174. return dwStatus;
  175. }
  176. //-----------------------------------------------------------------------
  177. DWORD
  178. ForwardNewLicenseRequest(
  179. IN LPTSTR pszServerSetupId,
  180. IN OUT DWORD *pdwSupportFlags,
  181. IN CHALLENGE_CONTEXT ChallengeContext,
  182. IN PTLSLICENSEREQUEST pRequest,
  183. IN LPTSTR pszMachineName,
  184. IN LPTSTR pszUserName,
  185. IN DWORD cbChallengeResponse,
  186. IN PBYTE pbChallengeResponse,
  187. IN BOOL bAcceptTemporaryLicense,
  188. IN BOOL bAcceptFewerLicenses,
  189. IN OUT DWORD *pdwQuantity,
  190. OUT PDWORD pcbLicense,
  191. OUT PBYTE *ppbLicense,
  192. IN OUT PDWORD pdwErrCode
  193. )
  194. /*++
  195. ++*/
  196. {
  197. TLS_HANDLE hHandle = NULL;
  198. DWORD dwStatus;
  199. LICENSEREQUEST LicenseRequest;
  200. BYTE pbEncryptedHwid[1024]; // encrypted HWID can't be more than 1024
  201. TCHAR szCompanyName[LSERVER_MAX_STRING_SIZE+2];
  202. TCHAR szProductId[LSERVER_MAX_STRING_SIZE+2];
  203. Product_Info ProductInfo;
  204. TLServerInfo ServerInfo;
  205. DBGPrintf(
  206. DBG_INFORMATION,
  207. DBG_FACILITY_RPC,
  208. DBGLEVEL_FUNCTION_TRACE,
  209. _TEXT("ForwardNewLicenseRequest() ...\n")
  210. );
  211. dwStatus = TLSLookupRegisteredServer(
  212. pszServerSetupId,
  213. NULL,
  214. NULL,
  215. &ServerInfo
  216. );
  217. if(dwStatus != ERROR_SUCCESS)
  218. {
  219. goto cleanup;
  220. }
  221. if(TLSCanForwardRequest(
  222. TLS_CURRENT_VERSION,
  223. ServerInfo.GetServerVersion()
  224. ) == FALSE)
  225. {
  226. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  227. goto cleanup;
  228. }
  229. DBGPrintf(
  230. DBG_INFORMATION,
  231. DBG_FACILITY_RPC,
  232. DBGLEVEL_FUNCTION_TRACE,
  233. _TEXT("ForwardNewLicenseRequest() to %s %s\n"),
  234. pszServerSetupId,
  235. ServerInfo.GetServerName()
  236. );
  237. hHandle = TLSConnectToServerWithServerId(pszServerSetupId);
  238. if(hHandle == NULL)
  239. {
  240. //
  241. // server not available
  242. //
  243. dwStatus = GetLastError();
  244. goto cleanup;
  245. }
  246. //TlsRequestToRequest(pRequest, &LicenseRequest);
  247. if(pRequest->cbEncryptedHwid >= sizeof(pbEncryptedHwid))
  248. {
  249. TLSASSERT(FALSE);
  250. SetLastError(dwStatus = ERROR_INVALID_DATA);
  251. goto cleanup;
  252. }
  253. if(pRequest->ProductInfo.cbCompanyName >= sizeof(szCompanyName) ||
  254. pRequest->ProductInfo.cbProductID >= sizeof(szProductId) )
  255. {
  256. TLSASSERT(FALSE);
  257. SetLastError(dwStatus = ERROR_INVALID_DATA);
  258. goto cleanup;
  259. }
  260. memset(&LicenseRequest, 0, sizeof(LicenseRequest));
  261. memcpy(
  262. pbEncryptedHwid,
  263. pRequest->pbEncryptedHwid,
  264. pRequest->cbEncryptedHwid
  265. );
  266. LicenseRequest.pbEncryptedHwid = pbEncryptedHwid;
  267. LicenseRequest.cbEncryptedHwid = pRequest->cbEncryptedHwid;
  268. LicenseRequest.dwLanguageID = pRequest->dwLanguageID;
  269. LicenseRequest.dwPlatformID = pRequest->dwPlatformID;
  270. LicenseRequest.pProductInfo = &ProductInfo;
  271. ProductInfo.dwVersion = pRequest->ProductInfo.dwVersion;
  272. ProductInfo.pbCompanyName = (PBYTE)szCompanyName;
  273. ProductInfo.pbProductID = (PBYTE)szProductId;
  274. ProductInfo.cbCompanyName = pRequest->ProductInfo.cbCompanyName;
  275. ProductInfo.cbProductID = pRequest->ProductInfo.cbProductID;
  276. memset(szCompanyName, 0, sizeof(szCompanyName));
  277. memset(szProductId, 0, sizeof(szProductId));
  278. memcpy(
  279. szCompanyName,
  280. pRequest->ProductInfo.pbCompanyName,
  281. pRequest->ProductInfo.cbCompanyName
  282. );
  283. memcpy(
  284. szProductId,
  285. pRequest->ProductInfo.pbProductID,
  286. pRequest->ProductInfo.cbProductID
  287. );
  288. if(IsServiceShuttingdown() == TRUE)
  289. {
  290. dwStatus = TLS_I_SERVICE_STOP;
  291. }
  292. else
  293. {
  294. dwStatus = TLSIssueNewLicenseExEx(
  295. hHandle,
  296. pdwSupportFlags,
  297. ChallengeContext,
  298. &LicenseRequest,
  299. pszMachineName,
  300. pszUserName,
  301. cbChallengeResponse,
  302. pbChallengeResponse,
  303. bAcceptTemporaryLicense,
  304. bAcceptFewerLicenses,
  305. pdwQuantity,
  306. pcbLicense,
  307. ppbLicense,
  308. pdwErrCode
  309. );
  310. }
  311. cleanup:
  312. if(hHandle)
  313. {
  314. TLSDisconnectFromServer(hHandle);
  315. }
  316. return dwStatus;
  317. }
  318. //---------------------------------------------------------
  319. DWORD
  320. TLSForwardUpgradeRequest(
  321. IN PTLSForwardUpgradeLicenseRequest pForward,
  322. IN OUT DWORD *pdwSupportFlags,
  323. IN PTLSDBLICENSEREQUEST pRequest,
  324. OUT PDWORD pcbLicense,
  325. OUT PBYTE* ppbLicense
  326. )
  327. /*++
  328. ++*/
  329. {
  330. DWORD dwStatus = ERROR_SUCCESS;
  331. DWORD dwErrCode = ERROR_SUCCESS;
  332. PTLSDbWorkSpace pDbWkSpace = NULL;
  333. TLServerInfo ServerInfo;
  334. TLSLICENSEPACK search;
  335. TLSLICENSEPACK found;
  336. DWORD dwSupportFlagsTemp;
  337. SAFESTRCPY(search.szProductId,pRequest->pszProductId);
  338. pDbWkSpace = AllocateWorkSpace(g_EnumDbTimeout);
  339. if(pDbWkSpace == NULL)
  340. {
  341. dwStatus=TLS_E_ALLOCATE_HANDLE;
  342. goto cleanup;
  343. }
  344. try {
  345. dwStatus = TLSDBKeyPackEnumBegin(
  346. pDbWkSpace,
  347. TRUE,
  348. LSKEYPACK_SEARCH_PRODUCTID,
  349. &search
  350. );
  351. if(dwStatus != ERROR_SUCCESS)
  352. {
  353. goto cleanup;
  354. }
  355. while( (dwStatus = TLSDBKeyPackEnumNext(pDbWkSpace, &found)) == ERROR_SUCCESS )
  356. {
  357. if(IsServiceShuttingdown() == TRUE)
  358. {
  359. SetLastError(dwStatus = TLS_I_SERVICE_STOP);
  360. break;
  361. }
  362. #if 0
  363. if(!(found.ucAgreementType & LSKEYPACK_REMOTE_TYPE))
  364. {
  365. continue;
  366. }
  367. #endif
  368. if(!(found.ucKeyPackStatus & LSKEYPACKSTATUS_REMOTE))
  369. {
  370. continue;
  371. }
  372. if(found.wMajorVersion < HIWORD(pRequest->dwProductVersion))
  373. {
  374. continue;
  375. }
  376. if(found.wMinorVersion < LOWORD(pRequest->dwProductVersion))
  377. {
  378. continue;
  379. }
  380. if((found.dwPlatformType & ~LSKEYPACK_PLATFORM_REMOTE) != pRequest->dwPlatformID)
  381. {
  382. continue;
  383. }
  384. if(_tcsicmp(found.szCompanyName, pRequest->pszCompanyName) != 0)
  385. {
  386. continue;
  387. }
  388. //
  389. // Save the original Support flags; they can be changed
  390. //
  391. dwSupportFlagsTemp = *pdwSupportFlags;
  392. //
  393. // make call to remote server
  394. //
  395. dwStatus = ForwardUpgradeLicenseRequest(
  396. found.szInstallId,
  397. &dwSupportFlagsTemp,
  398. pForward->m_pRequest,
  399. pForward->m_ChallengeContext,
  400. pForward->m_cbChallengeResponse,
  401. pForward->m_pbChallengeResponse,
  402. pForward->m_cbOldLicense,
  403. pForward->m_pbOldLicense,
  404. pcbLicense,
  405. ppbLicense,
  406. &dwErrCode
  407. );
  408. if (dwStatus == ERROR_SUCCESS &&
  409. dwErrCode == LSERVER_S_SUCCESS)
  410. {
  411. *pdwSupportFlags = dwSupportFlagsTemp;
  412. break;
  413. }
  414. if (dwStatus == TLS_I_SERVICE_STOP)
  415. {
  416. break;
  417. }
  418. // try next server
  419. dwStatus = ERROR_SUCCESS;
  420. }
  421. TLSDBKeyPackEnumEnd(pDbWkSpace);
  422. }
  423. catch( SE_Exception e ) {
  424. dwStatus = e.getSeNumber();
  425. }
  426. catch( ... ) {
  427. dwStatus = TLS_E_INTERNAL;
  428. }
  429. cleanup:
  430. if(pDbWkSpace != NULL)
  431. {
  432. ReleaseWorkSpace(&pDbWkSpace);
  433. }
  434. return dwStatus;
  435. }
  436. //-----------------------------------------------------------------------
  437. DWORD
  438. TLSForwardLicenseRequest(
  439. IN PTLSForwardNewLicenseRequest pForward,
  440. IN OUT DWORD *pdwSupportFlags,
  441. IN PTLSDBLICENSEREQUEST pRequest,
  442. IN BOOL bAcceptFewerLicenses,
  443. IN OUT DWORD *pdwQuantity,
  444. OUT PDWORD pcbLicense,
  445. OUT PBYTE* ppbLicense
  446. )
  447. /*++
  448. ++*/
  449. {
  450. DWORD dwStatus = ERROR_SUCCESS;
  451. DWORD dwErrCode = ERROR_SUCCESS;
  452. PTLSDbWorkSpace pDbWkSpace = NULL;
  453. TLServerInfo ServerInfo;
  454. TLSLICENSEPACK search;
  455. TLSLICENSEPACK found;
  456. DWORD dwSupportFlagsTemp;
  457. DWORD dwQuantityTemp;
  458. SAFESTRCPY(search.szProductId,pRequest->pszProductId);
  459. pDbWkSpace = AllocateWorkSpace(g_EnumDbTimeout);
  460. if(pDbWkSpace == NULL)
  461. {
  462. dwStatus=TLS_E_ALLOCATE_HANDLE;
  463. goto cleanup;
  464. }
  465. try {
  466. dwStatus = TLSDBKeyPackEnumBegin(
  467. pDbWkSpace,
  468. TRUE,
  469. LSKEYPACK_SEARCH_PRODUCTID,
  470. &search
  471. );
  472. if(dwStatus != ERROR_SUCCESS)
  473. {
  474. goto cleanup;
  475. }
  476. while( (dwStatus = TLSDBKeyPackEnumNext(pDbWkSpace, &found)) == ERROR_SUCCESS )
  477. {
  478. if(IsServiceShuttingdown() == TRUE)
  479. {
  480. SetLastError(dwStatus = TLS_I_SERVICE_STOP);
  481. break;
  482. }
  483. #if 0
  484. if(!(found.ucAgreementType & LSKEYPACK_REMOTE_TYPE))
  485. {
  486. continue;
  487. }
  488. #endif
  489. if(!(found.ucKeyPackStatus & LSKEYPACKSTATUS_REMOTE))
  490. {
  491. continue;
  492. }
  493. if(found.wMajorVersion < HIWORD(pRequest->dwProductVersion))
  494. {
  495. continue;
  496. }
  497. if(found.wMinorVersion < LOWORD(pRequest->dwProductVersion))
  498. {
  499. continue;
  500. }
  501. if((found.dwPlatformType & ~LSKEYPACK_PLATFORM_REMOTE) != pRequest->dwPlatformID)
  502. {
  503. continue;
  504. }
  505. if(_tcsicmp(found.szCompanyName, pRequest->pszCompanyName) != 0)
  506. {
  507. continue;
  508. }
  509. //
  510. // Save the original support flags and quantity;
  511. // they can be changed
  512. //
  513. dwSupportFlagsTemp = *pdwSupportFlags;
  514. dwQuantityTemp = *pdwQuantity;
  515. //
  516. // make call to remote server
  517. //
  518. dwStatus = ForwardNewLicenseRequest(
  519. found.szInstallId,
  520. &dwSupportFlagsTemp,
  521. pForward->m_ChallengeContext,
  522. pForward->m_pRequest,
  523. pForward->m_szMachineName,
  524. pForward->m_szUserName,
  525. pForward->m_cbChallengeResponse,
  526. pForward->m_pbChallengeResponse,
  527. FALSE, // bAcceptTemporaryLicense
  528. bAcceptFewerLicenses,
  529. &dwQuantityTemp,
  530. pcbLicense,
  531. ppbLicense,
  532. &dwErrCode
  533. );
  534. if (dwStatus == ERROR_SUCCESS &&
  535. dwErrCode == LSERVER_S_SUCCESS)
  536. {
  537. *pdwSupportFlags = dwSupportFlagsTemp;
  538. *pdwQuantity = dwQuantityTemp;
  539. break;
  540. }
  541. if (dwStatus == TLS_I_SERVICE_STOP)
  542. {
  543. break;
  544. }
  545. // try next server
  546. dwStatus = ERROR_SUCCESS;
  547. }
  548. TLSDBKeyPackEnumEnd(pDbWkSpace);
  549. }
  550. catch( SE_Exception e ) {
  551. dwStatus = e.getSeNumber();
  552. }
  553. catch( ... ) {
  554. dwStatus = TLS_E_INTERNAL;
  555. }
  556. cleanup:
  557. if(pDbWkSpace != NULL)
  558. {
  559. ReleaseWorkSpace(&pDbWkSpace);
  560. }
  561. return dwStatus;
  562. }