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.

742 lines
18 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996-1996
  5. //
  6. // File: license.c
  7. //
  8. // Contents:
  9. // Routine related to License Table
  10. //
  11. // History: 12-09-98 HueiWang Created
  12. //
  13. //---------------------------------------------------------------------------
  14. #include "pch.cpp"
  15. #include "clilic.h"
  16. #include "globals.h"
  17. void
  18. TLSDBLockLicenseTable()
  19. {
  20. DBGPrintf(
  21. DBG_INFORMATION,
  22. DBG_FACILITY_LOCK,
  23. DBGLEVEL_FUNCTION_TRACE,
  24. _TEXT("Locking table LicenseTable\n")
  25. );
  26. LicensedTable::LockTable();
  27. }
  28. void
  29. TLSDBUnlockLicenseTable()
  30. {
  31. DBGPrintf(
  32. DBG_INFORMATION,
  33. DBG_FACILITY_LOCK,
  34. DBGLEVEL_FUNCTION_TRACE,
  35. _TEXT("Unlocking table LicenseTable\n")
  36. );
  37. LicensedTable::UnlockTable();
  38. }
  39. /*************************************************************************
  40. Function:
  41. LSDBLicenseEnumBegin()
  42. Description:
  43. Begin a enumeration through license table based on search criterial
  44. Arguments:
  45. IN CSQLStmt* - SQL handle to bind input parameter
  46. IN bMatchAll - TRUE if match all search criterial, FALSE otherwise.
  47. IN dwSearchParm - which column in License table to bind
  48. IN LPLSLicenseSearchParm - search value
  49. Returns:
  50. ERROR_SUCCESS
  51. SQL error code.
  52. *************************************************************************/
  53. DWORD
  54. TLSDBLicenseFind(
  55. IN PTLSDbWorkSpace pDbWkSpace,
  56. IN BOOL bMatchAllParm,
  57. IN DWORD dwSearchParm,
  58. IN LPLICENSEDCLIENT lpSearch,
  59. IN OUT LPLICENSEDCLIENT lpFound
  60. )
  61. /*
  62. */
  63. {
  64. DWORD dwStatus=ERROR_SUCCESS;
  65. if(pDbWkSpace == NULL)
  66. {
  67. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  68. TLSASSERT(pDbWkSpace != NULL);
  69. return dwStatus;
  70. }
  71. LicensedTable& licenseTable = pDbWkSpace->m_LicensedTable;
  72. BOOL bSuccess;
  73. LICENSEDCLIENT found;
  74. bSuccess = licenseTable.FindRecord(
  75. bMatchAllParm,
  76. dwSearchParm,
  77. *lpSearch,
  78. (lpFound) ? *lpFound : found
  79. );
  80. if(bSuccess == FALSE)
  81. {
  82. if(licenseTable.GetLastJetError() == JET_errRecordNotFound)
  83. {
  84. SetLastError(dwStatus = TLS_E_RECORD_NOTFOUND);
  85. }
  86. else
  87. {
  88. LPTSTR pString = NULL;
  89. TLSGetESEError(licenseTable.GetLastJetError(), &pString);
  90. TLSLogEvent(
  91. EVENTLOG_ERROR_TYPE,
  92. TLS_E_DBGENERAL,
  93. TLS_E_JB_BASE,
  94. licenseTable.GetLastJetError(),
  95. (pString != NULL) ? pString : _TEXT("")
  96. );
  97. if(pString != NULL)
  98. {
  99. LocalFree(pString);
  100. }
  101. SetLastError(
  102. dwStatus = (SET_JB_ERROR(licenseTable.GetLastJetError()))
  103. );
  104. TLSASSERT(FALSE);
  105. }
  106. }
  107. return dwStatus;
  108. }
  109. //-----------------------------------------------------------------------
  110. DWORD
  111. TLSDBLicenseEnumBegin(
  112. IN PTLSDbWorkSpace pDbWkSpace,
  113. IN BOOL bMatchAll,
  114. IN DWORD dwSearchParm,
  115. IN LPLICENSEDCLIENT lpSearch
  116. )
  117. /*++
  118. --*/
  119. {
  120. return TLSDBLicenseEnumBeginEx(
  121. pDbWkSpace,
  122. bMatchAll,
  123. dwSearchParm,
  124. lpSearch,
  125. JET_bitSeekGE
  126. );
  127. }
  128. //-----------------------------------------------------------------------
  129. DWORD
  130. TLSDBLicenseEnumBeginEx(
  131. IN PTLSDbWorkSpace pDbWkSpace,
  132. IN BOOL bMatchAll,
  133. IN DWORD dwSearchParm,
  134. IN LPLICENSEDCLIENT lpSearch,
  135. IN JET_GRBIT jet_seek_grbit
  136. )
  137. /*++
  138. --*/
  139. {
  140. DWORD dwStatus=ERROR_SUCCESS;
  141. LicensedTable& licenseTable = pDbWkSpace->m_LicensedTable;
  142. BOOL bSuccess;
  143. bSuccess = licenseTable.EnumerateBegin(
  144. bMatchAll,
  145. dwSearchParm,
  146. lpSearch,
  147. jet_seek_grbit
  148. );
  149. if(bSuccess == FALSE)
  150. {
  151. LPTSTR pString = NULL;
  152. TLSGetESEError(licenseTable.GetLastJetError(), &pString);
  153. TLSLogEvent(
  154. EVENTLOG_ERROR_TYPE,
  155. TLS_E_DBGENERAL,
  156. TLS_E_JB_BASE,
  157. licenseTable.GetLastJetError(),
  158. (pString != NULL) ? pString : _TEXT("")
  159. );
  160. if(pString != NULL)
  161. {
  162. LocalFree(pString);
  163. }
  164. SetLastError(
  165. dwStatus = SET_JB_ERROR(licenseTable.GetLastJetError())
  166. );
  167. TLSASSERT(FALSE);
  168. }
  169. return dwStatus;
  170. }
  171. /*************************************************************************
  172. Function:
  173. LSDBLicenseEnumNext()
  174. Description:
  175. Retrieve next record that match search criterial, must have
  176. call LSDBLicenseEnumBegin() to establish search criterial.
  177. Arguments:
  178. IN CSQLStmt* - SQL handle to bind input parameter
  179. IN LPLSLicense - return record.
  180. IN LPLSHARDWARECHECKSUM - return hardware checksum value, see note
  181. Returns:
  182. ERROR_SUCCESS
  183. SQL error code.
  184. HLS_I_NO_MORE_DATA End of recordset.
  185. Note:
  186. Hardware checksum column is consider internal and not exposed across
  187. RPC layer.
  188. *************************************************************************/
  189. DWORD
  190. TLSDBLicenseEnumNext(
  191. IN PTLSDbWorkSpace pDbWkSpace,
  192. IN OUT LPLICENSEDCLIENT lplsLicense
  193. )
  194. /*
  195. */
  196. {
  197. return TLSDBLicenseEnumNextEx(
  198. pDbWkSpace,
  199. FALSE,
  200. FALSE,
  201. lplsLicense
  202. );
  203. }
  204. /*************************************************************************
  205. Function:
  206. LSDBLicenseEnumNext()
  207. Description:
  208. Retrieve next record that match search criterial, must have
  209. call LSDBLicenseEnumBegin() to establish search criterial.
  210. Arguments:
  211. IN pDbWkSpace - Workspace to search in
  212. IN bReverse - search in reverse order
  213. IN bAnyRecord - don't do equality comparison if true
  214. IN LPLSLicense - return record.
  215. Returns:
  216. ERROR_SUCCESS
  217. SQL error code.
  218. HLS_I_NO_MORE_DATA End of recordset.
  219. Note:
  220. Hardware checksum column is consider internal and not exposed across
  221. RPC layer.
  222. *************************************************************************/
  223. DWORD
  224. TLSDBLicenseEnumNextEx(
  225. IN PTLSDbWorkSpace pDbWkSpace,
  226. IN BOOL bReverse,
  227. IN BOOL bAnyRecord,
  228. IN OUT LPLICENSEDCLIENT lplsLicense
  229. )
  230. /*
  231. */
  232. {
  233. DWORD dwStatus=ERROR_SUCCESS;
  234. if(pDbWkSpace == NULL || lplsLicense == NULL)
  235. {
  236. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  237. TLSASSERT(FALSE);
  238. return dwStatus;
  239. }
  240. try {
  241. LicensedTable& licenseTable = pDbWkSpace->m_LicensedTable;
  242. BOOL bSuccess;
  243. switch(licenseTable.EnumerateNext(*lplsLicense,bReverse,bAnyRecord))
  244. {
  245. case RECORD_ENUM_ERROR:
  246. {
  247. LPTSTR pString = NULL;
  248. TLSGetESEError(licenseTable.GetLastJetError(), &pString);
  249. TLSLogEvent(
  250. EVENTLOG_ERROR_TYPE,
  251. TLS_E_DBGENERAL,
  252. TLS_E_JB_BASE,
  253. licenseTable.GetLastJetError(),
  254. (pString != NULL) ? pString : _TEXT("")
  255. );
  256. if(pString != NULL)
  257. {
  258. LocalFree(pString);
  259. }
  260. }
  261. dwStatus = SET_JB_ERROR(licenseTable.GetLastJetError());
  262. TLSASSERT(FALSE);
  263. break;
  264. case RECORD_ENUM_MORE_DATA:
  265. dwStatus = ERROR_SUCCESS;
  266. break;
  267. case RECORD_ENUM_END:
  268. dwStatus = TLS_I_NO_MORE_DATA;
  269. break;
  270. }
  271. }
  272. catch( SE_Exception e ) {
  273. dwStatus = e.getSeNumber();
  274. }
  275. catch(...) {
  276. dwStatus = TLS_E_INTERNAL;
  277. TLSASSERT(FALSE);
  278. }
  279. return dwStatus;
  280. }
  281. /*************************************************************************
  282. Function:
  283. LSDBLicenseEnumEnd()
  284. Description:
  285. Terminate a license table enumeration
  286. Arguments:
  287. IN CSQLStmt* - SQL handle
  288. Returns:
  289. None
  290. *************************************************************************/
  291. void
  292. TLSDBLicenseEnumEnd(
  293. IN PTLSDbWorkSpace pDbWkSpace
  294. )
  295. /*
  296. */
  297. {
  298. DWORD dwStatus=ERROR_SUCCESS;
  299. if(pDbWkSpace == NULL)
  300. {
  301. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  302. TLSASSERT(FALSE);
  303. return;
  304. }
  305. LicensedTable& licenseTable = pDbWkSpace->m_LicensedTable;
  306. licenseTable.EnumerateEnd();
  307. return;
  308. }
  309. //---------------------------------------------------------------------
  310. DWORD
  311. TLSDBLicenseAddEntry(
  312. IN PTLSDbWorkSpace pDbWkSpace,
  313. IN LPLICENSEDCLIENT pLicense
  314. )
  315. /*
  316. */
  317. {
  318. TLSASSERT(pDbWkSpace != NULL && pLicense != NULL);
  319. DWORD dwStatus=ERROR_SUCCESS;
  320. LicensedTable& licenseTable = pDbWkSpace->m_LicensedTable;
  321. BOOL bSuccess;
  322. TLSLicensedIndexMatchHwid dump(*pLicense);
  323. //
  324. // Check for duplicate entry - license ID
  325. //
  326. dwStatus = TLSDBLicenseFind(
  327. pDbWkSpace,
  328. TRUE,
  329. LSLICENSE_SEARCH_LICENSEID,
  330. pLicense,
  331. NULL
  332. );
  333. if(dwStatus == ERROR_SUCCESS)
  334. {
  335. SetLastError(dwStatus = TLS_E_DUPLICATE_RECORD);
  336. goto cleanup;
  337. }
  338. else if(dwStatus != TLS_E_RECORD_NOTFOUND)
  339. {
  340. goto cleanup;
  341. }
  342. dwStatus = ERROR_SUCCESS;
  343. pLicense->dbLowerBound = dump.dbLowerBound;
  344. GetSystemTimeAsFileTime(&(pLicense->ftLastModifyTime));
  345. bSuccess = licenseTable.InsertRecord(*pLicense);
  346. if(bSuccess = FALSE)
  347. {
  348. if(licenseTable.GetLastJetError() == JET_errKeyDuplicate)
  349. {
  350. SetLastError(dwStatus=TLS_E_DUPLICATE_RECORD);
  351. }
  352. else
  353. {
  354. LPTSTR pString = NULL;
  355. TLSGetESEError(licenseTable.GetLastJetError(), &pString);
  356. TLSLogEvent(
  357. EVENTLOG_ERROR_TYPE,
  358. TLS_E_DBGENERAL,
  359. TLS_E_JB_BASE,
  360. licenseTable.GetLastJetError(),
  361. (pString != NULL) ? pString : _TEXT("")
  362. );
  363. if(pString != NULL)
  364. {
  365. LocalFree(pString);
  366. }
  367. SetLastError(dwStatus = SET_JB_ERROR(licenseTable.GetLastJetError()));
  368. TLSASSERT(FALSE);
  369. }
  370. };
  371. cleanup:
  372. return dwStatus;
  373. }
  374. //---------------------------------------------------------------
  375. DWORD
  376. TLSDBLicenseDeleteEntry(
  377. IN PTLSDbWorkSpace pDbWkSpace,
  378. IN LPLICENSEDCLIENT pLicense,
  379. IN BOOL bPointerOnRecord
  380. )
  381. /*
  382. */
  383. {
  384. TLSASSERT(pDbWkSpace != NULL && pLicense != NULL);
  385. DWORD dwStatus=ERROR_SUCCESS;
  386. LicensedTable& licenseTable = pDbWkSpace->m_LicensedTable;
  387. BOOL bSuccess;
  388. bSuccess = licenseTable.DeleteAllRecord(
  389. TRUE,
  390. LSLICENSE_SEARCH_LICENSEID,
  391. *pLicense
  392. );
  393. if(bSuccess == FALSE)
  394. {
  395. SetLastError(dwStatus = SET_JB_ERROR(licenseTable.GetLastJetError()));
  396. if(licenseTable.GetLastJetError() != JET_errRecordNotFound)
  397. {
  398. LPTSTR pString = NULL;
  399. TLSGetESEError(licenseTable.GetLastJetError(), &pString);
  400. TLSLogEvent(
  401. EVENTLOG_ERROR_TYPE,
  402. TLS_E_DBGENERAL,
  403. TLS_E_JB_BASE,
  404. licenseTable.GetLastJetError(),
  405. (pString != NULL) ? pString : _TEXT("")
  406. );
  407. if(pString != NULL)
  408. {
  409. LocalFree(pString);
  410. }
  411. TLSASSERT(licenseTable.GetLastJetError() == JET_errRecordNotFound);
  412. }
  413. }
  414. return dwStatus;
  415. }
  416. DWORD
  417. TLSDBDeleteEnumeratedLicense(
  418. IN PTLSDbWorkSpace pDbWkSpace
  419. )
  420. {
  421. TLSASSERT(pDbWkSpace != NULL);
  422. DWORD dwStatus = ERROR_SUCCESS;
  423. LicensedTable& licenseTable = pDbWkSpace->m_LicensedTable;
  424. BOOL fSuccess;
  425. fSuccess = licenseTable.DeleteRecord();
  426. if (!fSuccess)
  427. {
  428. SetLastError(dwStatus = SET_JB_ERROR(licenseTable.GetLastJetError()));
  429. if(licenseTable.GetLastJetError() != JET_errRecordNotFound)
  430. {
  431. LPTSTR pString = NULL;
  432. TLSGetESEError(licenseTable.GetLastJetError(), &pString);
  433. TLSLogEvent(
  434. EVENTLOG_ERROR_TYPE,
  435. TLS_E_DBGENERAL,
  436. TLS_E_JB_BASE,
  437. licenseTable.GetLastJetError(),
  438. (pString != NULL) ? pString : _TEXT("")
  439. );
  440. if(pString != NULL)
  441. {
  442. LocalFree(pString);
  443. }
  444. TLSASSERT(licenseTable.GetLastJetError() == JET_errRecordNotFound);
  445. }
  446. }
  447. return dwStatus;
  448. }
  449. //----------------------------------------------------------------
  450. DWORD
  451. TLSDBLicenseUpdateEntry(
  452. IN PTLSDbWorkSpace pDbWkSpace,
  453. IN DWORD dwUpdateParm,
  454. IN LPLICENSEDCLIENT pLicense,
  455. IN BOOL bPointerOnRecord
  456. )
  457. /*
  458. */
  459. {
  460. TLSASSERT(pDbWkSpace != NULL && pLicense != NULL);
  461. DWORD dwStatus=ERROR_SUCCESS;
  462. LicensedTable& licenseTable = pDbWkSpace->m_LicensedTable;
  463. BOOL bSuccess;
  464. if(bPointerOnRecord == FALSE)
  465. {
  466. //
  467. // Check for duplicate entry - license ID, position pointer
  468. // to record and prepare for update.
  469. //
  470. dwStatus = TLSDBLicenseFind(
  471. pDbWkSpace,
  472. TRUE,
  473. LSLICENSE_SEARCH_LICENSEID,
  474. pLicense,
  475. NULL
  476. );
  477. if(dwStatus != ERROR_SUCCESS)
  478. {
  479. TLSASSERT(dwStatus == ERROR_SUCCESS);
  480. goto cleanup;
  481. }
  482. }
  483. GetSystemTimeAsFileTime(&(pLicense->ftLastModifyTime));
  484. bSuccess = licenseTable.UpdateRecord(
  485. *pLicense,
  486. (dwUpdateParm & ~LSLICENSE_SEARCH_LICENSEID) | LICENSE_PROCESS_LASTMODIFYTIME
  487. );
  488. if(bSuccess == FALSE)
  489. {
  490. LPTSTR pString = NULL;
  491. TLSGetESEError(licenseTable.GetLastJetError(), &pString);
  492. TLSLogEvent(
  493. EVENTLOG_ERROR_TYPE,
  494. TLS_E_DBGENERAL,
  495. TLS_E_JB_BASE,
  496. licenseTable.GetLastJetError(),
  497. (pString != NULL) ? pString : _TEXT("")
  498. );
  499. if(pString != NULL)
  500. {
  501. LocalFree(pString);
  502. }
  503. SetLastError(dwStatus = SET_JB_ERROR(licenseTable.GetLastJetError()));
  504. TLSASSERT(FALSE);
  505. }
  506. cleanup:
  507. return dwStatus;
  508. }
  509. //-----------------------------------------------------------------
  510. DWORD
  511. TLSDBLicenseSetValue(
  512. IN PTLSDbWorkSpace pDbWkSpace,
  513. IN DWORD dwSetParm,
  514. IN LPLICENSEDCLIENT lpLicense,
  515. IN BOOL bPointerOnRecord
  516. )
  517. /*
  518. */
  519. {
  520. DWORD dwStatus=ERROR_SUCCESS;
  521. BOOL bSuccess;
  522. if(pDbWkSpace == NULL || lpLicense == NULL)
  523. {
  524. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  525. TLSASSERT(FALSE);
  526. return dwStatus;
  527. }
  528. LicensedTable& licenseTable = pDbWkSpace->m_LicensedTable;
  529. TLSDBLockLicenseTable();
  530. if(lpLicense->ucLicenseStatus == LSLICENSESTATUS_DELETE)
  531. {
  532. dwStatus = TLSDBLicenseDeleteEntry(
  533. pDbWkSpace,
  534. lpLicense,
  535. bPointerOnRecord
  536. );
  537. }
  538. else
  539. {
  540. dwStatus = TLSDBLicenseUpdateEntry(
  541. pDbWkSpace,
  542. dwSetParm,
  543. lpLicense,
  544. bPointerOnRecord
  545. );
  546. }
  547. TLSDBUnlockLicenseTable();
  548. return dwStatus;
  549. }
  550. /*************************************************************************
  551. Function:
  552. LSDBLicenseGetCert()
  553. Description:
  554. Retrieve certificate issued to specific client
  555. Arguments:
  556. IN CSQLStmt* - SQL handle
  557. IN dwLicenseId - License Id
  558. OUT cbCert - size of certificate
  559. OUT pbCert - certificate issued to client
  560. Returns:
  561. ERROR_SUCCESS
  562. HLS_E_RECORD_NOTFOUND
  563. HLS_E_CORRUPT_DATABASE
  564. SQL error
  565. Note:
  566. Must have valid LicenseId.
  567. *************************************************************************/
  568. DWORD
  569. TLSDBLicenseGetCert(
  570. IN PTLSDbWorkSpace pDbWorkSpace,
  571. IN DWORD dwLicenseId,
  572. IN OUT PDWORD cbCert,
  573. IN OUT PBYTE pbCert
  574. )
  575. /*
  576. */
  577. {
  578. // unsupport function.
  579. TLSASSERT(FALSE);
  580. return TLS_E_INTERNAL;
  581. }
  582. /*************************************************************************
  583. Function:
  584. LSDBLicenseAdd()
  585. Description:
  586. Add an entry into license table
  587. Arguments:
  588. IN CSQLStmt* - SQL handle
  589. IN LSLicense* - value to be inserted
  590. IN PHWID - hardware ID.
  591. IN cbLicense - size of certificate
  592. IN pbLicense - Pointer to certificate
  593. Returns:
  594. ERROR_SUCCESS
  595. SQL error
  596. *************************************************************************/
  597. DWORD
  598. TLSDBLicenseAdd(
  599. IN PTLSDbWorkSpace pDbWorkSpace,
  600. LPLICENSEDCLIENT pLicense,
  601. DWORD cbLicense,
  602. PBYTE pbLicense
  603. )
  604. /*
  605. */
  606. {
  607. if(pDbWorkSpace == NULL || pLicense == NULL)
  608. {
  609. SetLastError(ERROR_INVALID_PARAMETER);
  610. TLSASSERT(FALSE);
  611. return ERROR_INVALID_PARAMETER;
  612. }
  613. return TLSDBLicenseAddEntry(
  614. pDbWorkSpace,
  615. pLicense
  616. );
  617. }