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.

734 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. LicensedTable& licenseTable = pDbWkSpace->m_LicensedTable;
  241. BOOL bSuccess;
  242. switch(licenseTable.EnumerateNext(*lplsLicense,bReverse,bAnyRecord))
  243. {
  244. case RECORD_ENUM_ERROR:
  245. {
  246. LPTSTR pString = NULL;
  247. TLSGetESEError(licenseTable.GetLastJetError(), &pString);
  248. TLSLogEvent(
  249. EVENTLOG_ERROR_TYPE,
  250. TLS_E_DBGENERAL,
  251. TLS_E_JB_BASE,
  252. licenseTable.GetLastJetError(),
  253. (pString != NULL) ? pString : _TEXT("")
  254. );
  255. if(pString != NULL)
  256. {
  257. LocalFree(pString);
  258. }
  259. }
  260. dwStatus = SET_JB_ERROR(licenseTable.GetLastJetError());
  261. TLSASSERT(FALSE);
  262. break;
  263. case RECORD_ENUM_MORE_DATA:
  264. dwStatus = ERROR_SUCCESS;
  265. break;
  266. case RECORD_ENUM_END:
  267. dwStatus = TLS_I_NO_MORE_DATA;
  268. break;
  269. }
  270. return dwStatus;
  271. }
  272. /*************************************************************************
  273. Function:
  274. LSDBLicenseEnumEnd()
  275. Description:
  276. Terminate a license table enumeration
  277. Arguments:
  278. IN CSQLStmt* - SQL handle
  279. Returns:
  280. None
  281. *************************************************************************/
  282. void
  283. TLSDBLicenseEnumEnd(
  284. IN PTLSDbWorkSpace pDbWkSpace
  285. )
  286. /*
  287. */
  288. {
  289. DWORD dwStatus=ERROR_SUCCESS;
  290. if(pDbWkSpace == NULL)
  291. {
  292. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  293. TLSASSERT(FALSE);
  294. return;
  295. }
  296. LicensedTable& licenseTable = pDbWkSpace->m_LicensedTable;
  297. licenseTable.EnumerateEnd();
  298. return;
  299. }
  300. //---------------------------------------------------------------------
  301. DWORD
  302. TLSDBLicenseAddEntry(
  303. IN PTLSDbWorkSpace pDbWkSpace,
  304. IN LPLICENSEDCLIENT pLicense
  305. )
  306. /*
  307. */
  308. {
  309. TLSASSERT(pDbWkSpace != NULL && pLicense != NULL);
  310. DWORD dwStatus=ERROR_SUCCESS;
  311. LicensedTable& licenseTable = pDbWkSpace->m_LicensedTable;
  312. BOOL bSuccess;
  313. TLSLicensedIndexMatchHwid dump(*pLicense);
  314. //
  315. // Check for duplicate entry - license ID
  316. //
  317. dwStatus = TLSDBLicenseFind(
  318. pDbWkSpace,
  319. TRUE,
  320. LSLICENSE_SEARCH_LICENSEID,
  321. pLicense,
  322. NULL
  323. );
  324. if(dwStatus == ERROR_SUCCESS)
  325. {
  326. SetLastError(dwStatus = TLS_E_DUPLICATE_RECORD);
  327. goto cleanup;
  328. }
  329. else if(dwStatus != TLS_E_RECORD_NOTFOUND)
  330. {
  331. goto cleanup;
  332. }
  333. dwStatus = ERROR_SUCCESS;
  334. pLicense->dbLowerBound = dump.dbLowerBound;
  335. GetSystemTimeAsFileTime(&(pLicense->ftLastModifyTime));
  336. bSuccess = licenseTable.InsertRecord(*pLicense);
  337. if(bSuccess == FALSE)
  338. {
  339. if(licenseTable.GetLastJetError() == JET_errKeyDuplicate)
  340. {
  341. SetLastError(dwStatus=TLS_E_DUPLICATE_RECORD);
  342. }
  343. else
  344. {
  345. LPTSTR pString = NULL;
  346. TLSGetESEError(licenseTable.GetLastJetError(), &pString);
  347. TLSLogEvent(
  348. EVENTLOG_ERROR_TYPE,
  349. TLS_E_DBGENERAL,
  350. TLS_E_JB_BASE,
  351. licenseTable.GetLastJetError(),
  352. (pString != NULL) ? pString : _TEXT("")
  353. );
  354. if(pString != NULL)
  355. {
  356. LocalFree(pString);
  357. }
  358. SetLastError(dwStatus = SET_JB_ERROR(licenseTable.GetLastJetError()));
  359. TLSASSERT(FALSE);
  360. }
  361. };
  362. cleanup:
  363. return dwStatus;
  364. }
  365. //---------------------------------------------------------------
  366. DWORD
  367. TLSDBLicenseDeleteEntry(
  368. IN PTLSDbWorkSpace pDbWkSpace,
  369. IN LPLICENSEDCLIENT pLicense,
  370. IN BOOL bPointerOnRecord
  371. )
  372. /*
  373. */
  374. {
  375. TLSASSERT(pDbWkSpace != NULL && pLicense != NULL);
  376. DWORD dwStatus=ERROR_SUCCESS;
  377. LicensedTable& licenseTable = pDbWkSpace->m_LicensedTable;
  378. BOOL bSuccess;
  379. bSuccess = licenseTable.DeleteAllRecord(
  380. TRUE,
  381. LSLICENSE_SEARCH_LICENSEID,
  382. *pLicense
  383. );
  384. if(bSuccess == FALSE)
  385. {
  386. SetLastError(dwStatus = SET_JB_ERROR(licenseTable.GetLastJetError()));
  387. if(licenseTable.GetLastJetError() != JET_errRecordNotFound)
  388. {
  389. LPTSTR pString = NULL;
  390. TLSGetESEError(licenseTable.GetLastJetError(), &pString);
  391. TLSLogEvent(
  392. EVENTLOG_ERROR_TYPE,
  393. TLS_E_DBGENERAL,
  394. TLS_E_JB_BASE,
  395. licenseTable.GetLastJetError(),
  396. (pString != NULL) ? pString : _TEXT("")
  397. );
  398. if(pString != NULL)
  399. {
  400. LocalFree(pString);
  401. }
  402. TLSASSERT(licenseTable.GetLastJetError() == JET_errRecordNotFound);
  403. }
  404. }
  405. return dwStatus;
  406. }
  407. DWORD
  408. TLSDBDeleteEnumeratedLicense(
  409. IN PTLSDbWorkSpace pDbWkSpace
  410. )
  411. {
  412. TLSASSERT(pDbWkSpace != NULL);
  413. DWORD dwStatus = ERROR_SUCCESS;
  414. LicensedTable& licenseTable = pDbWkSpace->m_LicensedTable;
  415. BOOL fSuccess;
  416. fSuccess = licenseTable.DeleteRecord();
  417. if (!fSuccess)
  418. {
  419. SetLastError(dwStatus = SET_JB_ERROR(licenseTable.GetLastJetError()));
  420. if(licenseTable.GetLastJetError() != JET_errRecordNotFound)
  421. {
  422. LPTSTR pString = NULL;
  423. TLSGetESEError(licenseTable.GetLastJetError(), &pString);
  424. TLSLogEvent(
  425. EVENTLOG_ERROR_TYPE,
  426. TLS_E_DBGENERAL,
  427. TLS_E_JB_BASE,
  428. licenseTable.GetLastJetError(),
  429. (pString != NULL) ? pString : _TEXT("")
  430. );
  431. if(pString != NULL)
  432. {
  433. LocalFree(pString);
  434. }
  435. TLSASSERT(licenseTable.GetLastJetError() == JET_errRecordNotFound);
  436. }
  437. }
  438. return dwStatus;
  439. }
  440. //----------------------------------------------------------------
  441. DWORD
  442. TLSDBLicenseUpdateEntry(
  443. IN PTLSDbWorkSpace pDbWkSpace,
  444. IN DWORD dwUpdateParm,
  445. IN LPLICENSEDCLIENT pLicense,
  446. IN BOOL bPointerOnRecord
  447. )
  448. /*
  449. */
  450. {
  451. TLSASSERT(pDbWkSpace != NULL && pLicense != NULL);
  452. DWORD dwStatus=ERROR_SUCCESS;
  453. LicensedTable& licenseTable = pDbWkSpace->m_LicensedTable;
  454. BOOL bSuccess;
  455. if(bPointerOnRecord == FALSE)
  456. {
  457. //
  458. // Check for duplicate entry - license ID, position pointer
  459. // to record and prepare for update.
  460. //
  461. dwStatus = TLSDBLicenseFind(
  462. pDbWkSpace,
  463. TRUE,
  464. LSLICENSE_SEARCH_LICENSEID,
  465. pLicense,
  466. NULL
  467. );
  468. if(dwStatus != ERROR_SUCCESS)
  469. {
  470. TLSASSERT(dwStatus == ERROR_SUCCESS);
  471. goto cleanup;
  472. }
  473. }
  474. GetSystemTimeAsFileTime(&(pLicense->ftLastModifyTime));
  475. bSuccess = licenseTable.UpdateRecord(
  476. *pLicense,
  477. (dwUpdateParm & ~LSLICENSE_SEARCH_LICENSEID) | LICENSE_PROCESS_LASTMODIFYTIME
  478. );
  479. if(bSuccess == FALSE)
  480. {
  481. LPTSTR pString = NULL;
  482. TLSGetESEError(licenseTable.GetLastJetError(), &pString);
  483. TLSLogEvent(
  484. EVENTLOG_ERROR_TYPE,
  485. TLS_E_DBGENERAL,
  486. TLS_E_JB_BASE,
  487. licenseTable.GetLastJetError(),
  488. (pString != NULL) ? pString : _TEXT("")
  489. );
  490. if(pString != NULL)
  491. {
  492. LocalFree(pString);
  493. }
  494. SetLastError(dwStatus = SET_JB_ERROR(licenseTable.GetLastJetError()));
  495. TLSASSERT(FALSE);
  496. }
  497. cleanup:
  498. return dwStatus;
  499. }
  500. //-----------------------------------------------------------------
  501. DWORD
  502. TLSDBLicenseSetValue(
  503. IN PTLSDbWorkSpace pDbWkSpace,
  504. IN DWORD dwSetParm,
  505. IN LPLICENSEDCLIENT lpLicense,
  506. IN BOOL bPointerOnRecord
  507. )
  508. /*
  509. */
  510. {
  511. DWORD dwStatus=ERROR_SUCCESS;
  512. BOOL bSuccess;
  513. if(pDbWkSpace == NULL || lpLicense == NULL)
  514. {
  515. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  516. TLSASSERT(FALSE);
  517. return dwStatus;
  518. }
  519. LicensedTable& licenseTable = pDbWkSpace->m_LicensedTable;
  520. TLSDBLockLicenseTable();
  521. if(lpLicense->ucLicenseStatus == LSLICENSESTATUS_DELETE)
  522. {
  523. dwStatus = TLSDBLicenseDeleteEntry(
  524. pDbWkSpace,
  525. lpLicense,
  526. bPointerOnRecord
  527. );
  528. }
  529. else
  530. {
  531. dwStatus = TLSDBLicenseUpdateEntry(
  532. pDbWkSpace,
  533. dwSetParm,
  534. lpLicense,
  535. bPointerOnRecord
  536. );
  537. }
  538. TLSDBUnlockLicenseTable();
  539. return dwStatus;
  540. }
  541. /*************************************************************************
  542. Function:
  543. LSDBLicenseGetCert()
  544. Description:
  545. Retrieve certificate issued to specific client
  546. Arguments:
  547. IN CSQLStmt* - SQL handle
  548. IN dwLicenseId - License Id
  549. OUT cbCert - size of certificate
  550. OUT pbCert - certificate issued to client
  551. Returns:
  552. ERROR_SUCCESS
  553. HLS_E_RECORD_NOTFOUND
  554. HLS_E_CORRUPT_DATABASE
  555. SQL error
  556. Note:
  557. Must have valid LicenseId.
  558. *************************************************************************/
  559. DWORD
  560. TLSDBLicenseGetCert(
  561. IN PTLSDbWorkSpace pDbWorkSpace,
  562. IN DWORD dwLicenseId,
  563. IN OUT PDWORD cbCert,
  564. IN OUT PBYTE pbCert
  565. )
  566. /*
  567. */
  568. {
  569. // unsupport function.
  570. TLSASSERT(FALSE);
  571. return TLS_E_INTERNAL;
  572. }
  573. /*************************************************************************
  574. Function:
  575. LSDBLicenseAdd()
  576. Description:
  577. Add an entry into license table
  578. Arguments:
  579. IN CSQLStmt* - SQL handle
  580. IN LSLicense* - value to be inserted
  581. IN PHWID - hardware ID.
  582. IN cbLicense - size of certificate
  583. IN pbLicense - Pointer to certificate
  584. Returns:
  585. ERROR_SUCCESS
  586. SQL error
  587. *************************************************************************/
  588. DWORD
  589. TLSDBLicenseAdd(
  590. IN PTLSDbWorkSpace pDbWorkSpace,
  591. LPLICENSEDCLIENT pLicense,
  592. DWORD cbLicense,
  593. PBYTE pbLicense
  594. )
  595. /*
  596. */
  597. {
  598. if(pDbWorkSpace == NULL || pLicense == NULL)
  599. {
  600. SetLastError(ERROR_INVALID_PARAMETER);
  601. TLSASSERT(FALSE);
  602. return ERROR_INVALID_PARAMETER;
  603. }
  604. return TLSDBLicenseAddEntry(
  605. pDbWorkSpace,
  606. pLicense
  607. );
  608. }