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.

589 lines
16 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. //
  5. // File: misc.cpp
  6. //
  7. // Contents: Misc. routines
  8. //
  9. // History:
  10. //
  11. //---------------------------------------------------------------------------
  12. #include "pch.cpp"
  13. #include "globals.h"
  14. #include "misc.h"
  15. //------------------------------------------------------------
  16. PMHANDLE
  17. GenerateClientId()
  18. {
  19. return (PMHANDLE)ULongToPtr(GetCurrentThreadId());
  20. }
  21. //---------------------------------------------------------------------------
  22. void
  23. TlsLicenseRequestToPMLicenseRequest(
  24. DWORD dwLicenseType,
  25. PTLSLICENSEREQUEST pTlsRequest,
  26. LPTSTR pszMachineName,
  27. LPTSTR pszUserName,
  28. DWORD dwSupportFlags,
  29. PPMLICENSEREQUEST pPmRequest
  30. )
  31. /*++
  32. Private routine.
  33. ++*/
  34. {
  35. pPmRequest->dwLicenseType = dwLicenseType;
  36. pPmRequest->dwProductVersion = pTlsRequest->ProductInfo.dwVersion;
  37. pPmRequest->pszProductId = (LPTSTR)pTlsRequest->ProductInfo.pbProductID;
  38. pPmRequest->pszCompanyName = (LPTSTR) pTlsRequest->ProductInfo.pbCompanyName;
  39. pPmRequest->dwLanguageId = pTlsRequest->dwLanguageID;
  40. pPmRequest->dwPlatformId = pTlsRequest->dwPlatformID;
  41. pPmRequest->pszMachineName = pszMachineName;
  42. pPmRequest->pszUserName = pszUserName;
  43. pPmRequest->fTemporary = FALSE;
  44. pPmRequest->dwSupportFlags = dwSupportFlags;
  45. return;
  46. }
  47. //---------------------------------------------------------------------------
  48. BOOL
  49. TLSDBGetMaxKeyPackId(
  50. PTLSDbWorkSpace pDbWkSpace,
  51. DWORD* pdwKeyPackId
  52. )
  53. /*
  54. */
  55. {
  56. TLSLICENSEPACK keypack;
  57. SetLastError(ERROR_SUCCESS);
  58. //
  59. if(pDbWkSpace == NULL || pdwKeyPackId == NULL)
  60. {
  61. SetLastError(ERROR_INVALID_PARAMETER);
  62. return GetLastError();
  63. }
  64. LicPackTable& licpackTable = pDbWkSpace->m_LicPackTable;
  65. // use primary index - internal keypack id
  66. if( licpackTable.EnumBegin() == FALSE ||
  67. licpackTable.MoveToRecord(JET_MoveLast) == FALSE)
  68. {
  69. if(licpackTable.GetLastJetError() == JET_errNoCurrentRecord)
  70. {
  71. *pdwKeyPackId = 0;
  72. goto cleanup;
  73. }
  74. else
  75. {
  76. SetLastError(SET_JB_ERROR(licpackTable.GetLastJetError()));
  77. goto cleanup;
  78. }
  79. }
  80. if(licpackTable.FetchRecord(keypack) == FALSE)
  81. {
  82. SetLastError(SET_JB_ERROR(licpackTable.GetLastJetError()));
  83. goto cleanup;
  84. }
  85. //FreeTlsLicensePack(&keypack);
  86. *pdwKeyPackId = keypack.dwKeyPackId;
  87. cleanup:
  88. return GetLastError() == ERROR_SUCCESS;
  89. }
  90. //---------------------------------------------------------------------------
  91. BOOL
  92. TLSDBGetMaxLicenseId(
  93. PTLSDbWorkSpace pDbWkSpace,
  94. DWORD* pdwLicenseId
  95. )
  96. /*
  97. */
  98. {
  99. LICENSEDCLIENT licensed;
  100. SetLastError(ERROR_SUCCESS);
  101. //
  102. if(pDbWkSpace == NULL || pdwLicenseId == NULL)
  103. {
  104. SetLastError(ERROR_INVALID_PARAMETER);
  105. return GetLastError();
  106. }
  107. LicensedTable& licensedTable = pDbWkSpace->m_LicensedTable;
  108. // use primary index - internal keypack id
  109. if( licensedTable.EnumBegin() == FALSE ||
  110. licensedTable.MoveToRecord(JET_MoveLast) == FALSE)
  111. {
  112. if(licensedTable.GetLastJetError() == JET_errNoCurrentRecord)
  113. {
  114. *pdwLicenseId = 0;
  115. goto cleanup;
  116. }
  117. else
  118. {
  119. SetLastError(SET_JB_ERROR(licensedTable.GetLastJetError()));
  120. goto cleanup;
  121. }
  122. }
  123. if(licensedTable.FetchRecord(licensed) == FALSE)
  124. {
  125. SetLastError(SET_JB_ERROR(licensedTable.GetLastJetError()));
  126. goto cleanup;
  127. }
  128. *pdwLicenseId = licensed.dwLicenseId;
  129. cleanup:
  130. return GetLastError() == ERROR_SUCCESS;
  131. }
  132. //+------------------------------------------------------------------------
  133. // Function:
  134. // LSDBGetNextKeyPackId()
  135. //
  136. // Description:
  137. // Return next available KeyPackId to be used in KeyPack table
  138. //
  139. // Arguments:
  140. // None
  141. //
  142. // Returns:
  143. // Key Pack Id
  144. //
  145. // Notes:
  146. // Could use AUTO NUMBER column type but returning the value would be
  147. // more toublesome.
  148. //
  149. // History:
  150. //-------------------------------------------------------------------------
  151. DWORD
  152. TLSDBGetNextKeyPackId()
  153. {
  154. LONG nextkeypack = InterlockedExchangeAdd(&g_NextKeyPackId, 1);
  155. DBGPrintf(
  156. DBG_INFORMATION,
  157. DBG_FACILITY_ALLOCATELICENSE,
  158. DBGLEVEL_FUNCTION_TRACE,
  159. _TEXT("GetNextKeyPack returns %d\n"),
  160. nextkeypack
  161. );
  162. return nextkeypack;
  163. }
  164. //+------------------------------------------------------------------------
  165. // Function:
  166. // LSDBGetNextLicenseId()
  167. //
  168. // Abstract:
  169. // Return next available LicenseId to be used in License Table
  170. //
  171. // Arguments:
  172. // None.
  173. //
  174. // Returns:
  175. // Next available License Id
  176. //
  177. // Notes:
  178. //
  179. // History:
  180. //-------------------------------------------------------------------------
  181. DWORD
  182. TLSDBGetNextLicenseId()
  183. {
  184. LONG nextlicenseid = InterlockedExchangeAdd(&g_NextLicenseId, 1);
  185. DBGPrintf(
  186. DBG_INFORMATION,
  187. DBG_FACILITY_ALLOCATELICENSE,
  188. DBGLEVEL_FUNCTION_TRACE,
  189. _TEXT("GetNextLicenseId returns %d\n"),
  190. nextlicenseid
  191. );
  192. return nextlicenseid;
  193. }
  194. //---------------------------------------------------------------------------
  195. DWORD
  196. TLSFormDBRequest(
  197. PBYTE pbEncryptedHwid,
  198. DWORD cbEncryptedHwid,
  199. DWORD dwProductVersion,
  200. LPTSTR pszCompanyName,
  201. LPTSTR pszProductId,
  202. DWORD dwLanguageId,
  203. DWORD dwPlatformId,
  204. LPTSTR szClientMachine,
  205. LPTSTR szUserName,
  206. LPTLSDBLICENSEREQUEST pDbRequest
  207. )
  208. /*++
  209. ++*/
  210. {
  211. DWORD status;
  212. memset(pDbRequest, 0, sizeof(TLSDBLICENSEREQUEST));
  213. // Decrypt HWID
  214. if(pbEncryptedHwid)
  215. {
  216. status=LicenseDecryptHwid(
  217. &pDbRequest->hWid,
  218. cbEncryptedHwid,
  219. pbEncryptedHwid,
  220. g_cbSecretKey,
  221. g_pbSecretKey);
  222. if(status != LICENSE_STATUS_OK)
  223. {
  224. return status;
  225. }
  226. }
  227. //
  228. // NOTE : No allocation of memory here...
  229. //
  230. pDbRequest->dwProductVersion = dwProductVersion;
  231. pDbRequest->pszCompanyName = pszCompanyName;
  232. pDbRequest->pszProductId = pszProductId;
  233. pDbRequest->dwLanguageID = dwLanguageId;
  234. pDbRequest->dwPlatformID = dwPlatformId;
  235. pDbRequest->pbEncryptedHwid = pbEncryptedHwid;
  236. pDbRequest->cbEncryptedHwid = cbEncryptedHwid;
  237. if(szClientMachine)
  238. _tcscpy(pDbRequest->szMachineName, szClientMachine);
  239. if(szUserName)
  240. _tcscpy(pDbRequest->szUserName, szUserName);
  241. pDbRequest->clientCertRdn.type = LSCERT_CLIENT_INFO_TYPE;
  242. pDbRequest->clientCertRdn.ClientInfo.szUserName = pDbRequest->szUserName;
  243. pDbRequest->clientCertRdn.ClientInfo.szMachineName = pDbRequest->szMachineName;
  244. pDbRequest->clientCertRdn.ClientInfo.pClientID = &pDbRequest->hWid;
  245. return ERROR_SUCCESS;
  246. }
  247. //---------------------------------------------------------------------------
  248. DWORD
  249. TLSConvertRpcLicenseRequestToDbRequest(
  250. PBYTE pbEncryptedHwid,
  251. DWORD cbEncryptedHwid,
  252. TLSLICENSEREQUEST* pRequest,
  253. LPTSTR szClientMachine,
  254. LPTSTR szUserName,
  255. LPTLSDBLICENSEREQUEST pDbRequest
  256. )
  257. /*++
  258. ++*/
  259. {
  260. DWORD status;
  261. memset(pDbRequest, 0, sizeof(TLSDBLICENSEREQUEST));
  262. // Decrypt HWID
  263. if(pbEncryptedHwid)
  264. {
  265. status=LicenseDecryptHwid(
  266. &pDbRequest->hWid,
  267. cbEncryptedHwid,
  268. pbEncryptedHwid,
  269. g_cbSecretKey,
  270. g_pbSecretKey);
  271. if(status != LICENSE_STATUS_OK)
  272. {
  273. return status;
  274. }
  275. }
  276. //
  277. // NOTE : No allocation of memory here...
  278. //
  279. // pDbRequest->pProductInfo = &(pRequest->ProductInfo);
  280. pDbRequest->dwProductVersion = pRequest->ProductInfo.dwVersion;
  281. pDbRequest->pszCompanyName = (LPTSTR)pRequest->ProductInfo.pbCompanyName;
  282. pDbRequest->pszProductId = (LPTSTR)pRequest->ProductInfo.pbProductID;
  283. pDbRequest->dwLanguageID = pRequest->dwLanguageID;
  284. pDbRequest->dwPlatformID = pRequest->dwPlatformID;
  285. pDbRequest->pbEncryptedHwid = pRequest->pbEncryptedHwid;
  286. pDbRequest->cbEncryptedHwid = pRequest->cbEncryptedHwid;
  287. if(szClientMachine)
  288. _tcscpy(pDbRequest->szMachineName, szClientMachine);
  289. if(szUserName)
  290. _tcscpy(pDbRequest->szUserName, szUserName);
  291. pDbRequest->clientCertRdn.type = LSCERT_CLIENT_INFO_TYPE;
  292. pDbRequest->clientCertRdn.ClientInfo.szUserName = pDbRequest->szUserName;
  293. pDbRequest->clientCertRdn.ClientInfo.szMachineName = pDbRequest->szMachineName;
  294. pDbRequest->clientCertRdn.ClientInfo.pClientID = &pDbRequest->hWid;
  295. return ERROR_SUCCESS;
  296. }
  297. //---------------------------------------------------------------------------
  298. BOOL
  299. ConvertLsKeyPackToKeyPack(
  300. IN LPLSKeyPack lpLsKeyPack,
  301. IN OUT PTLSLICENSEPACK lpLicPack,
  302. IN OUT PLICPACKDESC lpLicPackDesc
  303. )
  304. /*
  305. Abstract:
  306. Convert LSKeyPack from client to internally use structure
  307. Parameter:
  308. lpLsKeyPack - source value.
  309. lpLicPack - Target license pack.
  310. lpLicPackDesc - target license pack description
  311. Return:
  312. None.
  313. */
  314. {
  315. if(lpLsKeyPack == NULL)
  316. {
  317. SetLastError(TLS_E_INVALID_DATA);
  318. return FALSE;
  319. }
  320. //
  321. // BUG 226875
  322. //
  323. DWORD dwBufSize;
  324. dwBufSize = sizeof(lpLsKeyPack->szCompanyName)/sizeof(lpLsKeyPack->szCompanyName[0]);
  325. lpLsKeyPack->szCompanyName[dwBufSize - 1] = _TEXT('\0');
  326. dwBufSize = sizeof(lpLsKeyPack->szKeyPackId)/sizeof(lpLsKeyPack->szKeyPackId[0]);
  327. lpLsKeyPack->szKeyPackId[dwBufSize - 1] = _TEXT('\0');
  328. dwBufSize = sizeof(lpLsKeyPack->szProductId)/sizeof(lpLsKeyPack->szProductId[0]);
  329. lpLsKeyPack->szProductId[dwBufSize - 1] = _TEXT('\0');
  330. dwBufSize = sizeof(lpLsKeyPack->szProductDesc)/sizeof(lpLsKeyPack->szProductDesc[0]);
  331. lpLsKeyPack->szProductDesc[dwBufSize - 1] = _TEXT('\0');
  332. dwBufSize = sizeof(lpLsKeyPack->szBeginSerialNumber)/sizeof(lpLsKeyPack->szBeginSerialNumber[0]);
  333. lpLsKeyPack->szBeginSerialNumber[dwBufSize - 1] = _TEXT('\0');
  334. dwBufSize = sizeof(lpLsKeyPack->szProductName)/sizeof(lpLsKeyPack->szProductName[0]);
  335. lpLsKeyPack->szProductName[dwBufSize - 1] = _TEXT('\0');
  336. if(lpLicPack)
  337. {
  338. memset(lpLicPack, 0, sizeof(TLSLICENSEPACK));
  339. lpLicPack->ucAgreementType = lpLsKeyPack->ucKeyPackType;
  340. SAFESTRCPY(lpLicPack->szCompanyName, lpLsKeyPack->szCompanyName);
  341. SAFESTRCPY(lpLicPack->szKeyPackId, lpLsKeyPack->szKeyPackId);
  342. SAFESTRCPY(lpLicPack->szProductId, lpLsKeyPack->szProductId);
  343. lpLicPack->wMajorVersion = lpLsKeyPack->wMajorVersion;
  344. lpLicPack->wMinorVersion = lpLsKeyPack->wMinorVersion;
  345. lpLicPack->dwPlatformType = lpLsKeyPack->dwPlatformType;
  346. lpLicPack->ucLicenseType = lpLsKeyPack->ucLicenseType;
  347. lpLicPack->ucChannelOfPurchase = lpLsKeyPack->ucChannelOfPurchase;
  348. SAFESTRCPY(lpLicPack->szBeginSerialNumber, lpLsKeyPack->szBeginSerialNumber);
  349. lpLicPack->dwTotalLicenseInKeyPack = lpLsKeyPack->dwTotalLicenseInKeyPack;
  350. lpLicPack->dwProductFlags = lpLsKeyPack->dwProductFlags;
  351. lpLicPack->dwKeyPackId = lpLsKeyPack->dwKeyPackId;
  352. lpLicPack->dwExpirationDate = lpLsKeyPack->dwExpirationDate;
  353. lpLicPack->dwKeyPackId = lpLsKeyPack->dwKeyPackId;
  354. lpLicPack->dwActivateDate = lpLsKeyPack->dwActivateDate;
  355. lpLicPack->dwExpirationDate = lpLsKeyPack->dwExpirationDate;
  356. lpLicPack->dwNumberOfLicenses = lpLsKeyPack->dwNumberOfLicenses;
  357. lpLicPack->ucKeyPackStatus = lpLsKeyPack->ucKeyPackStatus;
  358. }
  359. if(lpLicPackDesc)
  360. {
  361. lpLicPackDesc->dwKeyPackId = lpLsKeyPack->dwKeyPackId;
  362. lpLicPackDesc->dwLanguageId = lpLsKeyPack->dwLanguageId;
  363. SAFESTRCPY(lpLicPackDesc->szCompanyName, lpLsKeyPack->szCompanyName);
  364. SAFESTRCPY(lpLicPackDesc->szProductName, lpLsKeyPack->szProductName);
  365. SAFESTRCPY(lpLicPackDesc->szProductDesc, lpLsKeyPack->szProductDesc);
  366. }
  367. return TRUE;
  368. }
  369. //-----------------------------------------------------------
  370. void
  371. ConvertKeyPackToLsKeyPack(
  372. IN PTLSLICENSEPACK lpLicPack,
  373. IN PLICPACKDESC lpLicPackDesc,
  374. IN OUT LPLSKeyPack lpLsKeyPack
  375. )
  376. /*
  377. Abstract:
  378. Combine internally used license pack structure into one for
  379. return back to RPC client
  380. Parameter:
  381. lpLicPack - source
  382. lpLicPackStatus - source
  383. lpLicPackDesc - source
  384. lpLsKeyPack - target
  385. Return:
  386. None.
  387. */
  388. {
  389. if(lpLicPack)
  390. {
  391. lpLsKeyPack->ucKeyPackType = lpLicPack->ucAgreementType;
  392. SAFESTRCPY(lpLsKeyPack->szCompanyName, lpLicPack->szCompanyName);
  393. SAFESTRCPY(lpLsKeyPack->szKeyPackId, lpLicPack->szKeyPackId);
  394. SAFESTRCPY(lpLsKeyPack->szProductId, lpLicPack->szProductId);
  395. lpLsKeyPack->wMajorVersion = lpLicPack->wMajorVersion;
  396. lpLsKeyPack->wMinorVersion = lpLicPack->wMinorVersion;
  397. lpLsKeyPack->dwPlatformType = lpLicPack->dwPlatformType;
  398. lpLsKeyPack->ucLicenseType = lpLicPack->ucLicenseType;
  399. lpLsKeyPack->ucChannelOfPurchase = lpLicPack->ucChannelOfPurchase;
  400. SAFESTRCPY(lpLsKeyPack->szBeginSerialNumber, lpLicPack->szBeginSerialNumber);
  401. lpLsKeyPack->dwTotalLicenseInKeyPack = lpLicPack->dwTotalLicenseInKeyPack;
  402. lpLsKeyPack->dwProductFlags = lpLicPack->dwProductFlags;
  403. lpLsKeyPack->dwKeyPackId = lpLicPack->dwKeyPackId;
  404. lpLsKeyPack->ucKeyPackStatus = lpLicPack->ucKeyPackStatus;
  405. lpLsKeyPack->dwActivateDate = lpLicPack->dwActivateDate;
  406. lpLsKeyPack->dwExpirationDate = lpLicPack->dwExpirationDate;
  407. lpLsKeyPack->dwNumberOfLicenses = lpLicPack->dwNumberOfLicenses;
  408. }
  409. if(lpLicPackDesc)
  410. {
  411. lpLsKeyPack->dwKeyPackId = lpLicPackDesc->dwKeyPackId;
  412. lpLsKeyPack->dwLanguageId = lpLicPackDesc->dwLanguageId;
  413. SAFESTRCPY(lpLsKeyPack->szCompanyName, lpLicPackDesc->szCompanyName);
  414. SAFESTRCPY(lpLsKeyPack->szProductName, lpLicPackDesc->szProductName);
  415. SAFESTRCPY(lpLsKeyPack->szProductDesc, lpLicPackDesc->szProductDesc);
  416. }
  417. return;
  418. }
  419. //-----------------------------------------------------------------------
  420. void
  421. ConvertLSLicenseToLicense(
  422. LPLSLicense lplsLicense,
  423. LPLICENSEDCLIENT lpLicense
  424. )
  425. /*
  426. */
  427. {
  428. lpLicense->dwLicenseId = lplsLicense->dwLicenseId;
  429. lpLicense->dwKeyPackId = lplsLicense->dwKeyPackId;
  430. memset(lpLicense->szMachineName, 0, sizeof(lpLicense->szMachineName));
  431. memset(lpLicense->szUserName, 0, sizeof(lpLicense->szUserName));
  432. //SAFESTRCPY(lpLicense->szMachineName, lplsLicense->szMachineName);
  433. _tcsncpy(
  434. lpLicense->szMachineName,
  435. lplsLicense->szMachineName,
  436. sizeof(lpLicense->szMachineName)/sizeof(lpLicense->szMachineName[0]) - 1
  437. );
  438. //SAFESTRCPY(lpLicense->szUserName, lplsLicense->szUserName);
  439. _tcsncpy(
  440. lpLicense->szUserName,
  441. lplsLicense->szUserName,
  442. sizeof(lpLicense->szUserName)/sizeof(lpLicense->szUserName[0]) - 1
  443. );
  444. lpLicense->ftIssueDate = lplsLicense->ftIssueDate;
  445. lpLicense->ftExpireDate = lplsLicense->ftExpireDate;
  446. lpLicense->ucLicenseStatus = lplsLicense->ucLicenseStatus;
  447. //
  448. // not expose to client
  449. //
  450. lpLicense->dwNumLicenses = 0;
  451. return;
  452. }
  453. //-----------------------------------------------------------------------
  454. void
  455. ConvertLicenseToLSLicense(
  456. LPLICENSEDCLIENT lpLicense,
  457. LPLSLicense lplsLicense
  458. )
  459. /*
  460. */
  461. {
  462. lplsLicense->dwLicenseId = lpLicense->dwLicenseId;
  463. lplsLicense->dwKeyPackId = lpLicense->dwKeyPackId;
  464. SAFESTRCPY(lplsLicense->szMachineName, lpLicense->szMachineName);
  465. SAFESTRCPY(lplsLicense->szUserName, lpLicense->szUserName);
  466. lplsLicense->ftIssueDate = lpLicense->ftIssueDate;
  467. lplsLicense->ftExpireDate = lpLicense->ftExpireDate;
  468. lplsLicense->ucLicenseStatus = lpLicense->ucLicenseStatus;
  469. return;
  470. }
  471. //-----------------------------------------------------------------------
  472. void
  473. ConvertLicenseToLSLicenseEx(
  474. LPLICENSEDCLIENT lpLicense,
  475. LPLSLicenseEx lplsLicense
  476. )
  477. /*
  478. */
  479. {
  480. lplsLicense->dwLicenseId = lpLicense->dwLicenseId;
  481. lplsLicense->dwKeyPackId = lpLicense->dwKeyPackId;
  482. SAFESTRCPY(lplsLicense->szMachineName, lpLicense->szMachineName);
  483. SAFESTRCPY(lplsLicense->szUserName, lpLicense->szUserName);
  484. lplsLicense->ftIssueDate = lpLicense->ftIssueDate;
  485. lplsLicense->ftExpireDate = lpLicense->ftExpireDate;
  486. lplsLicense->ucLicenseStatus = lpLicense->ucLicenseStatus;
  487. lplsLicense->dwQuantity = lpLicense->dwNumLicenses;
  488. return;
  489. }