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.

7584 lines
207 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996-1996
  5. //
  6. // File: tlsrpc.c
  7. //
  8. // Contents: Various RPC function to accept client request
  9. //
  10. // History: 12-09-98 HueiWang Created
  11. //
  12. //---------------------------------------------------------------------------
  13. #include "pch.cpp"
  14. #include "server.h"
  15. #include "gencert.h"
  16. #include "kp.h"
  17. #include "keypack.h"
  18. #include "clilic.h"
  19. #include "postjob.h"
  20. #include "srvlist.h"
  21. #include "utils.h"
  22. #include "misc.h"
  23. #include "licreq.h"
  24. #include "conlic.h"
  25. #include "globals.h"
  26. #include "db.h"
  27. #include "tlscert.h"
  28. #include "permlic.h"
  29. #include "remotedb.h"
  30. CCMutex g_AdminLock;
  31. CCMutex g_RpcLock;
  32. CCEvent g_ServerShutDown(TRUE, FALSE);
  33. BOOL
  34. VerifyLicenseRequest(
  35. PTLSLICENSEREQUEST pLicenseRequest
  36. )
  37. /*++
  38. --*/
  39. {
  40. BOOL bValid = FALSE;
  41. if(pLicenseRequest == NULL)
  42. {
  43. DBGPrintf(
  44. DBG_INFORMATION,
  45. DBG_FACILITY_RPC,
  46. DBGLEVEL_FUNCTION_TRACE,
  47. _TEXT("VerifyLicenseRequest() invalid input\n")
  48. );
  49. goto cleanup;
  50. }
  51. if( pLicenseRequest->cbEncryptedHwid == 0 ||
  52. pLicenseRequest->pbEncryptedHwid == NULL)
  53. {
  54. DBGPrintf(
  55. DBG_INFORMATION,
  56. DBG_FACILITY_RPC,
  57. DBGLEVEL_FUNCTION_TRACE,
  58. _TEXT("VerifyLicenseRequest() invalid HWID\n")
  59. );
  60. goto cleanup;
  61. }
  62. if( pLicenseRequest->ProductInfo.cbCompanyName == 0 ||
  63. pLicenseRequest->ProductInfo.pbCompanyName == NULL )
  64. {
  65. DBGPrintf(
  66. DBG_INFORMATION,
  67. DBG_FACILITY_RPC,
  68. DBGLEVEL_FUNCTION_TRACE,
  69. _TEXT("VerifyLicenseRequest() invalid company name\n")
  70. );
  71. goto cleanup;
  72. }
  73. if( pLicenseRequest->ProductInfo.cbProductID == 0 ||
  74. pLicenseRequest->ProductInfo.pbProductID == NULL )
  75. {
  76. DBGPrintf(
  77. DBG_INFORMATION,
  78. DBG_FACILITY_RPC,
  79. DBGLEVEL_FUNCTION_TRACE,
  80. _TEXT("VerifyLicenseRequest() invalid product id\n")
  81. );
  82. goto cleanup;
  83. }
  84. bValid = TRUE;
  85. cleanup:
  86. return bValid;
  87. }
  88. /////////////////////////////////////////////////////////////////////////////
  89. BOOL
  90. WaitForMyTurnOrShutdown(
  91. HANDLE hHandle,
  92. DWORD dwWaitTime
  93. )
  94. /*
  95. */
  96. {
  97. //
  98. // Shutdown event is first one in the wait list
  99. // reason is when service thread signal shutdow, at the same time,
  100. // there might be a RPC call entering WaitForMultipleObjects() call and
  101. // it will return WAIT_OBJECT_0 and continue on, this is not desirable
  102. // since we want it to return can't get handle and exit RPC call immediately
  103. //
  104. HANDLE waitHandles[2]={g_ServerShutDown.hEvent, hHandle};
  105. DWORD dwStatus;
  106. //
  107. // Could be return shutting down...
  108. //
  109. dwStatus=WaitForMultipleObjects(
  110. sizeof(waitHandles)/sizeof(waitHandles[0]),
  111. waitHandles,
  112. FALSE,
  113. dwWaitTime
  114. );
  115. return (dwStatus == WAIT_OBJECT_0 + 1) || (dwStatus == WAIT_ABANDONED_0 + 1);
  116. }
  117. //////////////////////////////////////////////////////
  118. HANDLE
  119. GetServiceShutdownHandle()
  120. {
  121. return g_ServerShutDown.hEvent;
  122. }
  123. void
  124. ServiceSignalShutdown()
  125. {
  126. g_ServerShutDown.SetEvent();
  127. }
  128. void
  129. ServiceResetShutdownEvent()
  130. {
  131. g_ServerShutDown.ResetEvent();
  132. }
  133. BOOL
  134. IsServiceShuttingdown()
  135. {
  136. return (WaitForSingleObject(g_ServerShutDown.hEvent, 0) == WAIT_OBJECT_0);
  137. }
  138. //////////////////////////////////////////////////////
  139. BOOL
  140. AcquireRPCExclusiveLock(
  141. IN DWORD dwWaitTime
  142. )
  143. /*++
  144. Abstract:
  145. Acquire exclusive lock for RPC interface.
  146. Parameter:
  147. dwWaitTime : Wait time.
  148. Return:
  149. TRUE/FALSE
  150. --*/
  151. {
  152. return WaitForMyTurnOrShutdown(
  153. g_RpcLock.hMutex,
  154. dwWaitTime
  155. );
  156. }
  157. //////////////////////////////////////////////////////
  158. void
  159. ReleaseRPCExclusiveLock()
  160. {
  161. g_RpcLock.Unlock();
  162. }
  163. //////////////////////////////////////////////////////
  164. BOOL
  165. AcquireAdministrativeLock(
  166. IN DWORD dwWaitTime
  167. )
  168. /*++
  169. Abstract:
  170. Acquire lock for administrative action.
  171. Parameter:
  172. dwWaitTime : Time to wait for the lock.
  173. Returns:
  174. TRUE/FALSE.
  175. --*/
  176. {
  177. return WaitForMyTurnOrShutdown(
  178. g_AdminLock.hMutex,
  179. dwWaitTime
  180. );
  181. }
  182. //////////////////////////////////////////////////////
  183. void
  184. ReleaseAdministrativeLock()
  185. /*++
  186. --*/
  187. {
  188. g_AdminLock.Unlock();
  189. }
  190. //-----------------------------------------------------------------------
  191. DWORD
  192. TLSVerifyHydraCertificate(
  193. PBYTE pHSCert,
  194. DWORD cbHSCert
  195. )
  196. /*
  197. */
  198. {
  199. DWORD dwStatus;
  200. dwStatus = TLSVerifyProprietyChainedCertificate(
  201. g_hCryptProv,
  202. pHSCert,
  203. cbHSCert
  204. );
  205. if(dwStatus != ERROR_SUCCESS)
  206. {
  207. Hydra_Server_Cert hCert;
  208. memset(&hCert, 0, sizeof(Hydra_Server_Cert));
  209. dwStatus=UnpackHydraServerCertificate(pHSCert, cbHSCert, &hCert);
  210. if(dwStatus == LICENSE_STATUS_OK)
  211. {
  212. dwStatus=LicenseVerifyServerCert(&hCert);
  213. if(hCert.PublicKeyData.pBlob)
  214. free(hCert.PublicKeyData.pBlob);
  215. if(hCert.SignatureBlob.pBlob)
  216. free(hCert.SignatureBlob.pBlob);
  217. }
  218. }
  219. return dwStatus;
  220. }
  221. //-------------------------------------------------------------------------
  222. //
  223. // General RPC routines
  224. //
  225. void * __RPC_USER
  226. MIDL_user_allocate(size_t size)
  227. {
  228. void* ptr=AllocateMemory(size);
  229. // DBGPrintf(0xFFFFFFFF, _TEXT("Allocate 0x%08x, size %d\n"), ptr, size);
  230. return ptr;
  231. }
  232. void __RPC_USER
  233. MIDL_user_free(void *pointer)
  234. {
  235. FreeMemory(pointer);
  236. }
  237. //-------------------------------------------------------------------------
  238. BOOL
  239. ValidContextHandle(
  240. IN PCONTEXT_HANDLE phContext
  241. )
  242. /*++
  243. Description:
  244. Verify client context handle.
  245. Arguments:
  246. phContext - client context handle return from TLSRpcConnect().
  247. Return:
  248. TRUE/FALSE
  249. ++*/
  250. {
  251. #if DBG
  252. BOOL bValid;
  253. LPCLIENTCONTEXT lpClientContext = (LPCLIENTCONTEXT)phContext;
  254. bValid = (lpClientContext->m_PreDbg[0] == 0xcdcdcdcd && lpClientContext->m_PreDbg[1] == 0xcdcdcdcd &&
  255. lpClientContext->m_PostDbg[0] == 0xcdcdcdcd && lpClientContext->m_PostDbg[1] == 0xcdcdcdcd);
  256. if(!bValid)
  257. {
  258. DBGPrintf(
  259. DBG_ERROR,
  260. DBG_FACILITY_RPC,
  261. DBGLEVEL_FUNCTION_TRACE,
  262. _TEXT("ValidContextHandle : Bad client context\n")
  263. );
  264. TLSASSERT(FALSE);
  265. }
  266. return bValid;
  267. #else
  268. return TRUE;
  269. #endif
  270. }
  271. //-------------------------------------------------------------------------
  272. void
  273. __RPC_USER PCONTEXT_HANDLE_rundown(
  274. PCONTEXT_HANDLE phContext
  275. )
  276. /*++
  277. Description:
  278. Client context handle cleanup, called when client disconnect normally
  279. or abnormally, see context handle rundown routine help on RPC
  280. Argument:
  281. phContext - client context handle.
  282. Returns:
  283. None
  284. ++*/
  285. {
  286. DBGPrintf(
  287. DBG_INFORMATION,
  288. DBG_FACILITY_RPC,
  289. DBGLEVEL_FUNCTION_TRACE,
  290. _TEXT("PCONTEXT_HANDLE_rundown...\n")
  291. );
  292. TLSASSERT(phContext != NULL);
  293. try {
  294. //
  295. // If service is shutting down, exit right away without freeing up memory,
  296. //
  297. // Durning shutdown, RPC wait until all call completed but it does not wait
  298. // until all open connection has 'rundown' if client is still in enumeration,
  299. // this will cause ReleaseWorkSpace() to assert. Instead of using one more
  300. // HANDLE to wait until all open connection has been rundown, we return right
  301. // away to speed up shutdown time
  302. //
  303. if( phContext && ValidContextHandle(phContext) )
  304. {
  305. LPCLIENTCONTEXT lpClientContext = (LPCLIENTCONTEXT)phContext;
  306. DBGPrintf(
  307. DBG_INFORMATION,
  308. DBG_FACILITY_RPC,
  309. DBGLEVEL_FUNCTION_TRACE,
  310. _TEXT("Disconnect from %s\n"),
  311. lpClientContext->m_Client
  312. );
  313. assert(lpClientContext->m_RefCount == 0);
  314. if( IsServiceShuttingdown() == FALSE )
  315. {
  316. switch(lpClientContext->m_ContextType)
  317. {
  318. case CONTEXTHANDLE_LICENSE_ENUM_TYPE:
  319. {
  320. PTLSDbWorkSpace pDbWkSpace = (PTLSDbWorkSpace)lpClientContext->m_ContextHandle;
  321. if( IsValidAllocatedWorkspace(pDbWkSpace) == TRUE )
  322. {
  323. ReleaseWorkSpace(&pDbWkSpace);
  324. }
  325. }
  326. break;
  327. case CONTEXTHANDLE_KEYPACK_ENUM_TYPE:
  328. {
  329. LPENUMHANDLE hEnum=(LPENUMHANDLE)lpClientContext->m_ContextHandle;
  330. if( IsValidAllocatedWorkspace(hEnum->pbWorkSpace) == TRUE )
  331. {
  332. TLSDBLicenseKeyPackEnumEnd(hEnum);
  333. }
  334. lpClientContext->m_ContextType = CONTEXTHANDLE_EMPTY_TYPE;
  335. lpClientContext->m_ContextHandle=NULL;
  336. }
  337. break;
  338. case CONTEXTHANDLE_HYDRA_REQUESTCERT_TYPE:
  339. {
  340. LPTERMSERVCERTREQHANDLE lpHandle=(LPTERMSERVCERTREQHANDLE)lpClientContext->m_ContextHandle;
  341. midl_user_free(lpHandle->pCertRequest);
  342. midl_user_free(lpHandle->pbChallengeData);
  343. FreeMemory(lpHandle);
  344. }
  345. break;
  346. case CONTEXTHANDLE_CHALLENGE_SERVER_TYPE:
  347. case CONTEXTHANDLE_CHALLENGE_LRWIZ_TYPE:
  348. case CONTEXTHANDLE_CHALLENGE_TERMSRV_TYPE:
  349. {
  350. PTLSCHALLENGEDATA pChallengeData = (PTLSCHALLENGEDATA) lpClientContext->m_ContextHandle;
  351. if(pChallengeData)
  352. {
  353. FreeMemory(pChallengeData->pbChallengeData);
  354. FreeMemory(pChallengeData);
  355. }
  356. }
  357. }
  358. }
  359. else
  360. {
  361. DBGPrintf(
  362. DBG_INFORMATION,
  363. DBG_FACILITY_RPC,
  364. DBGLEVEL_FUNCTION_TRACE,
  365. _TEXT("PCONTEXT_HANDLE_rundown while shutting down...\n")
  366. );
  367. }
  368. if( lpClientContext->m_Client )
  369. {
  370. FreeMemory(lpClientContext->m_Client);
  371. }
  372. midl_user_free(lpClientContext);
  373. }
  374. }
  375. catch(...) {
  376. SetLastError(TLS_E_INTERNAL);
  377. }
  378. return;
  379. }
  380. //----------------------------------------------------------------------------------
  381. DWORD
  382. GetClientPrivilege(
  383. IN handle_t hRpcBinding
  384. )
  385. /*++
  386. Description:
  387. Return client's privilege level
  388. Arguments:
  389. hRpcBinding - Client's RPC binding handle.
  390. Return:
  391. Client's privilege level
  392. ++*/
  393. {
  394. DWORD dwStatus = CLIENT_ACCESS_USER;
  395. BOOL bAdmin=FALSE;
  396. RPC_STATUS rpc_status;
  397. // If a value of zero is specified, the server impersonates the client that
  398. // is being served by this server thread
  399. rpc_status = RpcImpersonateClient(hRpcBinding);
  400. if(rpc_status == RPC_S_OK)
  401. {
  402. IsAdmin(&bAdmin);
  403. dwStatus = (bAdmin) ? CLIENT_ACCESS_ADMIN : CLIENT_ACCESS_USER;
  404. RpcRevertToSelfEx(hRpcBinding);
  405. }
  406. return dwStatus;
  407. }
  408. //-------------------------------------------------------------------------------
  409. error_status_t
  410. TLSRpcConnect(
  411. /* [in] */ handle_t binding,
  412. /* [out] */ PCONTEXT_HANDLE __RPC_FAR *pphContext
  413. )
  414. /*++
  415. Description:
  416. Connect client and allocate/return client context handle.
  417. Arguments:
  418. hRPCBinding - RPC binding handle
  419. pphContext - client context handle.
  420. Returns via dwErrCode.
  421. RPC_S_ACCESS_DENIED or LSERVER_S_SUCCESS.
  422. ++*/
  423. {
  424. DWORD status=ERROR_SUCCESS;
  425. DWORD dwPriv;
  426. RPC_BINDING_HANDLE hClient=NULL;
  427. WCHAR * pszRpcStrBinding=NULL;
  428. LPTSTR pszClient=NULL;
  429. if(RpcBindingServerFromClient(binding, &hClient) == RPC_S_OK)
  430. {
  431. status = RpcBindingToStringBinding( hClient, &pszRpcStrBinding );
  432. RpcBindingFree(&hClient);
  433. if (status != RPC_S_OK)
  434. {
  435. goto cleanup;
  436. }
  437. }
  438. //
  439. // need to load from resource file
  440. //
  441. pszClient = (LPTSTR)AllocateMemory(
  442. (_tcslen((pszRpcStrBinding) ? pszRpcStrBinding : _TEXT("Unknown")) + 1) * sizeof(TCHAR)
  443. );
  444. if(pszClient == NULL)
  445. {
  446. status = ERROR_OUTOFMEMORY;
  447. goto cleanup;
  448. }
  449. _tcscpy(pszClient,
  450. (pszRpcStrBinding) ? pszRpcStrBinding : _TEXT("Unknown")
  451. );
  452. if(pszRpcStrBinding)
  453. {
  454. RpcStringFree(&pszRpcStrBinding);
  455. }
  456. DBGPrintf(
  457. DBG_INFORMATION,
  458. DBG_FACILITY_RPC,
  459. DBGLEVEL_FUNCTION_TRACE,
  460. _TEXT("Connect from client %s\n"),
  461. pszClient
  462. );
  463. dwPriv=GetClientPrivilege(binding);
  464. LPCLIENTCONTEXT lpContext;
  465. lpContext=(LPCLIENTCONTEXT)midl_user_allocate(sizeof(CLIENTCONTEXT));
  466. if(lpContext == NULL)
  467. {
  468. status = ERROR_OUTOFMEMORY;
  469. goto cleanup;
  470. }
  471. #if DBG
  472. lpContext->m_LastCall = RPC_CALL_CONNECT;
  473. lpContext->m_PreDbg[0] = 0xcdcdcdcd;
  474. lpContext->m_PreDbg[1] = 0xcdcdcdcd;
  475. lpContext->m_PostDbg[0] = 0xcdcdcdcd;
  476. lpContext->m_PostDbg[1] = 0xcdcdcdcd;
  477. #endif
  478. lpContext->m_Client = pszClient;
  479. lpContext->m_RefCount = 0;
  480. *pphContext=lpContext;
  481. lpContext->m_ContextType = CONTEXTHANDLE_EMPTY_TYPE;
  482. lpContext->m_ClientFlags = dwPriv;
  483. cleanup:
  484. if(status != ERROR_SUCCESS)
  485. {
  486. FreeMemory(pszClient);
  487. }
  488. return TLSMapReturnCode(status);
  489. }
  490. //-------------------------------------------------------------------------------
  491. error_status_t
  492. TLSRpcDisconnect(
  493. /* [out][in] */ PPCONTEXT_HANDLE pphContext
  494. )
  495. /*++
  496. Description:
  497. Disconnect client and FreeMemory all memory allocated on the behalf of client
  498. Arguments:
  499. pphContext - pointer to client context handle
  500. Returns:
  501. LSERVER_S_SUCCESS or ERROR_INVALID_HANDLE
  502. ++*/
  503. {
  504. DWORD Status=ERROR_SUCCESS;
  505. try {
  506. if(!ValidContextHandle(*pphContext) || *pphContext == NULL)
  507. {
  508. Status = ERROR_INVALID_HANDLE;
  509. }
  510. else
  511. {
  512. PCONTEXT_HANDLE_rundown(*pphContext);
  513. *pphContext = NULL;
  514. }
  515. }
  516. catch(...) {
  517. Status = TLS_E_INTERNAL;
  518. }
  519. return TLSMapReturnCode(Status);
  520. }
  521. //-------------------------------------------------------------------------------
  522. error_status_t
  523. TLSRpcGetVersion(
  524. /* [in] */ PCONTEXT_HANDLE phContext,
  525. /* [ref][out][in] */ PDWORD pdwVersion
  526. )
  527. /*++
  528. ++*/
  529. {
  530. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  531. InterlockedIncrement( &lpContext->m_RefCount );
  532. DBGPrintf(
  533. DBG_INFORMATION,
  534. DBG_FACILITY_RPC,
  535. DBGLEVEL_FUNCTION_TRACE,
  536. _TEXT("%s : TLSRpcGetVersion\n"),
  537. lpContext->m_Client
  538. );
  539. if(TLSIsBetaNTServer() == TRUE)
  540. {
  541. *pdwVersion = TLS_CURRENT_VERSION;
  542. }
  543. else
  544. {
  545. *pdwVersion = TLS_CURRENT_VERSION_RTM;
  546. }
  547. if(g_SrvRole & TLSERVER_ENTERPRISE_SERVER)
  548. {
  549. *pdwVersion |= TLS_VERSION_ENTERPRISE_BIT;
  550. }
  551. DBGPrintf(
  552. DBG_INFORMATION,
  553. DBG_FACILITY_RPC,
  554. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  555. _TEXT("%s : TLSRpcGetVersion return 0x%08x\n"),
  556. lpContext->m_Client,
  557. *pdwVersion
  558. );
  559. InterlockedDecrement( &lpContext->m_RefCount );
  560. return RPC_S_OK;
  561. }
  562. //-------------------------------------------------------------------------------
  563. error_status_t
  564. TLSRpcGetSupportFlags(
  565. /* [in] */ PCONTEXT_HANDLE phContext,
  566. /* [ref][out] */ DWORD *pdwSupportFlags
  567. )
  568. /*++
  569. ++*/
  570. {
  571. error_status_t status = RPC_S_OK;
  572. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  573. InterlockedIncrement( &lpContext->m_RefCount );
  574. DBGPrintf(
  575. DBG_INFORMATION,
  576. DBG_FACILITY_RPC,
  577. DBGLEVEL_FUNCTION_TRACE,
  578. _TEXT("%s : TLSRpcGetSupportFlags\n"),
  579. lpContext->m_Client
  580. );
  581. if (NULL != pdwSupportFlags)
  582. {
  583. *pdwSupportFlags = ALL_KNOWN_SUPPORT_FLAGS;
  584. }
  585. else
  586. {
  587. status = ERROR_INVALID_PARAMETER;
  588. }
  589. InterlockedDecrement( &lpContext->m_RefCount );
  590. return status;
  591. }
  592. //-------------------------------------------------------------------------------
  593. error_status_t
  594. TLSRpcSendServerCertificate(
  595. /* [in] */ PCONTEXT_HANDLE phContext,
  596. /* [in] */ DWORD cbCert,
  597. /* [size_is][in] */ PBYTE pbCert,
  598. /* [ref][out][in] */ PDWORD dwErrCode
  599. )
  600. /*++
  601. Description:
  602. This routine is for License Server to identify hydra server, hydra server
  603. need to send its certificate in order to gain certificate request privilege.
  604. Arguments:
  605. phContext - client context handle.
  606. cbCert - size of hydra server certificate.
  607. pbCert - hydra server's self-created certificate.
  608. dwErrCode - return code.
  609. Returns via dwErrCode
  610. LSERVER_E_INVALID_DATA.
  611. ++*/
  612. {
  613. DWORD status=ERROR_SUCCESS;
  614. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  615. InterlockedIncrement( &lpContext->m_RefCount );
  616. DBGPrintf(
  617. DBG_INFORMATION,
  618. DBG_FACILITY_RPC,
  619. DBGLEVEL_FUNCTION_TRACE,
  620. _TEXT("%s : TLSRpcSendServerCertificate\n"),
  621. lpContext->m_Client
  622. );
  623. try {
  624. if(pbCert == NULL || cbCert == 0 ||
  625. TLSVerifyHydraCertificate(pbCert, cbCert) != LICENSE_STATUS_OK)
  626. {
  627. DBGPrintf(
  628. DBG_WARNING,
  629. DBG_FACILITY_RPC,
  630. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  631. _TEXT("TLSRpcSendServerCertificate : client %s send invalid certificate\n"),
  632. lpContext->m_Client
  633. );
  634. status = TLS_E_INVALID_DATA;
  635. }
  636. else
  637. {
  638. lpContext->m_ClientFlags |= CLIENT_ACCESS_REQUEST;
  639. }
  640. }
  641. catch(...) {
  642. status = TLS_E_INVALID_DATA;
  643. }
  644. // midl_user_free(pbCert);
  645. lpContext->m_LastError=status;
  646. InterlockedDecrement( &lpContext->m_RefCount );
  647. #if DBG
  648. lpContext->m_LastCall = RPC_CALL_SEND_CERTIFICATE;
  649. #endif
  650. *dwErrCode = TLSMapReturnCode(status);
  651. return RPC_S_OK;
  652. }
  653. //-------------------------------------------------------------------------------
  654. error_status_t
  655. TLSRpcGetServerName(
  656. /* [in] */ PCONTEXT_HANDLE phContext,
  657. /* [size_is][string][out][in] */ LPTSTR szMachineName,
  658. /* [out][in] */ PDWORD cbSize,
  659. /* [ref][out][in] */ PDWORD dwErrCode
  660. )
  661. /*++
  662. Description:
  663. Return server's machine name.
  664. Arguments:
  665. phContext - Client context handle
  666. szMachineName - return server's machine name, must be at least
  667. MAX_COMPUTERNAME_LENGTH + 1 in length
  668. Return:
  669. TLS_E_INVALID_DATA - buffer size too small.
  670. ++*/
  671. {
  672. // TODO: no need for this buffer - use caller's buffer instead
  673. TCHAR szComputerName[MAX_COMPUTERNAME_LENGTH+2];
  674. DWORD dwBufferSize=MAX_COMPUTERNAME_LENGTH+1;
  675. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  676. DBGPrintf(
  677. DBG_INFORMATION,
  678. DBG_FACILITY_RPC,
  679. DBGLEVEL_FUNCTION_TRACE,
  680. _TEXT("%s : TLSRpcGetServerName\n"),
  681. lpContext->m_Client
  682. );
  683. if(lpContext->m_ContextType != CONTEXTHANDLE_CHALLENGE_SERVER_TYPE)
  684. {
  685. *dwErrCode = TLSMapReturnCode(TLS_E_INVALID_DATA);
  686. return RPC_S_OK;
  687. }
  688. *dwErrCode = ERROR_SUCCESS;
  689. if(!GetComputerName(szComputerName, &dwBufferSize))
  690. {
  691. *dwErrCode = GetLastError();
  692. }
  693. //
  694. // return buffer must be big enough for NULL,
  695. // dwBufferSize return does not include NULL.
  696. //
  697. if(*cbSize <= dwBufferSize)
  698. {
  699. DBGPrintf(
  700. DBG_WARNING,
  701. DBG_FACILITY_RPC,
  702. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  703. _TEXT("TLSRpcGetServerName : Client %s invalid parameter\n"),
  704. lpContext->m_Client
  705. );
  706. *dwErrCode = TLSMapReturnCode(TLS_E_INVALID_DATA);
  707. }
  708. else
  709. {
  710. _tcsncpy(szMachineName, szComputerName, min(_tcslen(szComputerName), *cbSize));
  711. szMachineName[min(_tcslen(szComputerName), *cbSize - 1)] = _TEXT('\0');
  712. }
  713. *cbSize = _tcslen(szComputerName) + 1; // include NULL terminate string
  714. #if DBG
  715. lpContext->m_LastCall = RPC_CALL_GET_SERVERNAME;
  716. #endif
  717. return RPC_S_OK;
  718. }
  719. //-------------------------------------------------------------------------------
  720. error_status_t
  721. TLSRpcGetServerNameEx(
  722. /* [in] */ PCONTEXT_HANDLE phContext,
  723. /* [size_is][string][out][in] */ LPTSTR szMachineName,
  724. /* [out][in] */ PDWORD cbSize,
  725. /* [ref][out][in] */ PDWORD dwErrCode
  726. )
  727. /*++
  728. Description:
  729. Return server's machine name.
  730. Arguments:
  731. phContext - Client context handle
  732. szMachineName - return server's machine name, must be at least
  733. MAX_COMPUTERNAME_LENGTH + 1 in length
  734. Return:
  735. TLS_E_INVALID_DATA - buffer size too small.
  736. ++*/
  737. {
  738. // TODO: no need for this buffer - use caller's buffer instead
  739. TCHAR szComputerName[MAX_COMPUTERNAME_LENGTH+2];
  740. DWORD dwBufferSize=MAX_COMPUTERNAME_LENGTH+1;
  741. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  742. DBGPrintf(
  743. DBG_INFORMATION,
  744. DBG_FACILITY_RPC,
  745. DBGLEVEL_FUNCTION_TRACE,
  746. _TEXT("%s : TLSRpcGetServerNameEx\n"),
  747. lpContext->m_Client
  748. );
  749. *dwErrCode = ERROR_SUCCESS;
  750. if(!GetComputerName(szComputerName, &dwBufferSize))
  751. {
  752. *dwErrCode = GetLastError();
  753. }
  754. //
  755. // return buffer must be big enough for NULL,
  756. // dwBufferSize return does not include NULL.
  757. //
  758. if(*cbSize <= dwBufferSize)
  759. {
  760. DBGPrintf(
  761. DBG_WARNING,
  762. DBG_FACILITY_RPC,
  763. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  764. _TEXT("TLSRpcGetServerNameEx : Client %s invalid parameter\n"),
  765. lpContext->m_Client
  766. );
  767. *dwErrCode = TLSMapReturnCode(TLS_E_INVALID_DATA);
  768. }
  769. else
  770. {
  771. _tcsncpy(szMachineName, szComputerName, min(_tcslen(szComputerName), *cbSize));
  772. szMachineName[min(_tcslen(szComputerName), *cbSize - 1)] = _TEXT('\0');
  773. }
  774. *cbSize = _tcslen(szComputerName) + 1; // include NULL terminate string
  775. #if DBG
  776. lpContext->m_LastCall = RPC_CALL_GET_SERVERNAME;
  777. #endif
  778. return RPC_S_OK;
  779. }
  780. //-------------------------------------------------------------------------------
  781. error_status_t
  782. TLSRpcGetServerScope(
  783. /* [in] */ PCONTEXT_HANDLE phContext,
  784. /* [size_is][string][out][in] */ LPTSTR szScopeName,
  785. /* [out][in] */ PDWORD cbSize,
  786. /* [ref][out][in] */ PDWORD dwErrCode
  787. )
  788. /*++
  789. Description:
  790. Return License Server's scope
  791. Arguments:
  792. phContext - Client context
  793. szScopeName - return server's scope, must be at least
  794. MAX_COMPUTERNAME_LENGTH in length
  795. Return:
  796. LSERVER_S_SUCCESS or error code from WideCharToMultiByte()
  797. TLS_E_INVALID_DATA - buffer size too small.
  798. ++*/
  799. {
  800. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  801. DBGPrintf(
  802. DBG_INFORMATION,
  803. DBG_FACILITY_RPC,
  804. DBGLEVEL_FUNCTION_TRACE,
  805. _TEXT("%s : TLSRpcGetServerScope\n"),
  806. lpContext->m_Client
  807. );
  808. *dwErrCode = ERROR_SUCCESS;
  809. if(*cbSize <= _tcslen(g_pszScope))
  810. {
  811. *dwErrCode = TLSMapReturnCode(TLS_E_INVALID_DATA);
  812. }
  813. else
  814. {
  815. _tcsncpy(szScopeName, g_pszScope, min(_tcslen(g_pszScope), *cbSize));
  816. szScopeName[min(_tcslen(g_pszScope), *cbSize-1)] = _TEXT('\0');
  817. }
  818. *cbSize = _tcslen(g_pszScope) + 1; // include NULL terminate string
  819. #if DBG
  820. lpContext->m_LastCall = RPC_CALL_GET_SERVERSCOPE;
  821. #endif
  822. return RPC_S_OK;
  823. }
  824. //-------------------------------------------------------------------------------
  825. error_status_t
  826. TLSRpcGetInfo(
  827. /* [in] */ PCONTEXT_HANDLE phContext,
  828. /* [in] */ DWORD cbHSCert,
  829. /* [size_is][in] */ PBYTE pHSCert,
  830. /* [ref][out] */ DWORD __RPC_FAR *pcbLSCert,
  831. /* [size_is][size_is][out] */ BYTE __RPC_FAR *__RPC_FAR *pLSCert,
  832. /* [ref][out] */ DWORD __RPC_FAR *pcbLSSecretKey,
  833. /* [size_is][size_is][out] */ BYTE __RPC_FAR *__RPC_FAR *pLSSecretKey,
  834. /* [ref][out][in] */ PDWORD dwErrCode
  835. )
  836. /*++
  837. Description:
  838. Routine to exchange Hydra server's certificate and License server's
  839. certificate/private key for signing client machine's hardware ID.
  840. Arguments:
  841. phContext - client context handle
  842. cbHSCert - size of Hydra Server's certificate
  843. pHSCert - Hydra Server's certificate
  844. pcbLSCert - return size of License Server's certificate
  845. pLSCert - return License Server's certificate
  846. pcbLSSecretKey - return size of License Server's private key.
  847. pLSSecretKey - retrun License Server's private key
  848. Return Value:
  849. LSERVER_S_SUCCESS success
  850. LSERVER_E_INVALID_DATA Invalid hydra server certificate
  851. LSERVER_E_OUTOFMEMORY Can't allocate required memory
  852. TLS_E_INTERNAL Internal error occurred in License Server
  853. ++*/
  854. {
  855. DWORD status=ERROR_SUCCESS;
  856. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  857. DBGPrintf(
  858. DBG_INFORMATION,
  859. DBG_FACILITY_RPC,
  860. DBGLEVEL_FUNCTION_TRACE,
  861. _TEXT("%s : TLSRpcGetInfo\n"),
  862. lpContext->m_Client
  863. );
  864. try {
  865. do {
  866. if( pHSCert == NULL || cbHSCert == 0 ||
  867. TLSVerifyHydraCertificate(pHSCert, cbHSCert) != LICENSE_STATUS_OK)
  868. {
  869. status= TLS_E_INVALID_DATA;
  870. break;
  871. }
  872. *pcbLSCert = g_cbSignatureEncodedCert;
  873. *pLSCert = (PBYTE)midl_user_allocate(*pcbLSCert);
  874. if(!*pLSCert)
  875. {
  876. status = ERROR_OUTOFMEMORY;
  877. break;
  878. }
  879. memcpy(*pLSCert, (PBYTE)g_pbSignatureEncodedCert, *pcbLSCert);
  880. *pcbLSSecretKey=0;
  881. *pLSSecretKey=NULL;
  882. } while(FALSE);
  883. if(status != ERROR_SUCCESS)
  884. {
  885. if(*pLSCert)
  886. midl_user_free(*pLSCert);
  887. if(*pLSSecretKey)
  888. midl_user_free(*pLSSecretKey);
  889. *pcbLSCert=0;
  890. *pcbLSSecretKey=0;
  891. *pLSCert=NULL;
  892. *pLSSecretKey=NULL;
  893. }
  894. }
  895. catch(...) {
  896. status = TLS_E_INTERNAL;
  897. }
  898. // midl_user_free(pHSCert);
  899. #if DBG
  900. lpContext->m_LastCall = RPC_CALL_GETINFO;
  901. #endif
  902. lpContext->m_LastError = status;
  903. *dwErrCode = TLSMapReturnCode(status);
  904. return RPC_S_OK;
  905. }
  906. //-------------------------------------------------------------------------------
  907. #define RANDOM_CHALLENGE_DATA _TEXT("TEST")
  908. DWORD
  909. TLSGenerateChallengeData(
  910. IN DWORD ClientInfo,
  911. OUT PDWORD pcbChallengeData,
  912. IN OUT PBYTE* pChallengeData
  913. )
  914. {
  915. DWORD hr=ERROR_SUCCESS;
  916. *pcbChallengeData = (_tcslen(RANDOM_CHALLENGE_DATA) + 1) * sizeof(WCHAR);
  917. *pChallengeData=(PBYTE)midl_user_allocate(*pcbChallengeData);
  918. if(*pChallengeData)
  919. {
  920. memcpy(*pChallengeData, RANDOM_CHALLENGE_DATA, *pcbChallengeData);
  921. }
  922. else
  923. {
  924. SetLastError(hr=ERROR_OUTOFMEMORY);
  925. }
  926. return hr;
  927. }
  928. //++----------------------------------------------------------------------------
  929. DWORD
  930. TLSVerifyChallengeDataGetWantedLicenseLevel(
  931. IN const CHALLENGE_CONTEXT ChallengeContext,
  932. IN const DWORD cbChallengeData,
  933. IN const PBYTE pbChallengeData,
  934. OUT WORD* pwLicenseDetail
  935. )
  936. /*++
  937. --*/
  938. {
  939. DWORD dwStatus = ERROR_SUCCESS;
  940. DWORD dwChallengeDataSize = (_tcslen(RANDOM_CHALLENGE_DATA) + 1) * sizeof(WCHAR);
  941. PPlatformChallengeResponseData pChallengeResponse;
  942. if( cbChallengeData < dwChallengeDataSize || pbChallengeData == NULL )
  943. {
  944. //
  945. // Assume old client, new client always send back our challenge data
  946. //
  947. *pwLicenseDetail = LICENSE_DETAIL_SIMPLE;
  948. }
  949. else if( cbChallengeData == dwChallengeDataSize &&
  950. _tcsicmp( (LPCTSTR)pbChallengeData, RANDOM_CHALLENGE_DATA ) == 0 )
  951. {
  952. //
  953. // old client, set license chain to LICENSE_DETAIL_SIMPLE
  954. //
  955. *pwLicenseDetail = LICENSE_DETAIL_SIMPLE;
  956. }
  957. else
  958. {
  959. BOOL bValidStruct = TRUE;
  960. //
  961. // we still don't have a good challenge so ignore actual verification
  962. //
  963. pChallengeResponse = (PPlatformChallengeResponseData) pbChallengeData;
  964. bValidStruct = (pChallengeResponse->wVersion == CURRENT_PLATFORMCHALLENGE_VERSION);
  965. if( bValidStruct == TRUE )
  966. {
  967. bValidStruct = (pChallengeResponse->cbChallenge + offsetof(PlatformChallengeResponseData, pbChallenge) == cbChallengeData);
  968. }
  969. if (bValidStruct == TRUE )
  970. {
  971. if( pChallengeResponse->wClientType == WIN32_PLATFORMCHALLENGE_TYPE ||
  972. pChallengeResponse->wClientType == WIN16_PLATFORMCHALLENGE_TYPE ||
  973. pChallengeResponse->wClientType == WINCE_PLATFORMCHALLENGE_TYPE ||
  974. pChallengeResponse->wClientType == OTHER_PLATFORMCHALLENGE_TYPE )
  975. {
  976. bValidStruct = TRUE;
  977. }
  978. else
  979. {
  980. bValidStruct = FALSE;
  981. }
  982. }
  983. if( bValidStruct == TRUE )
  984. {
  985. if( pChallengeResponse->wLicenseDetailLevel == LICENSE_DETAIL_SIMPLE ||
  986. pChallengeResponse->wLicenseDetailLevel == LICENSE_DETAIL_MODERATE ||
  987. pChallengeResponse->wLicenseDetailLevel == LICENSE_DETAIL_DETAIL )
  988. {
  989. bValidStruct = TRUE;
  990. }
  991. else
  992. {
  993. bValidStruct = FALSE;
  994. }
  995. }
  996. //
  997. // For now, we simply let it go thru, assert or deny request once
  998. // we settle down of challenge
  999. //
  1000. if( bValidStruct == FALSE )
  1001. {
  1002. // bad data, assume old client
  1003. *pwLicenseDetail = LICENSE_DETAIL_SIMPLE;
  1004. }
  1005. //else if( pChallengeResponse->wClientType == WINCE_PLATFORMCHALLENGE_TYPE )
  1006. //{
  1007. //
  1008. // UN-comment this to limit WINCE to get a self-signed certificate
  1009. //
  1010. // *pwLicenseDetail = LICENSE_DETAIL_SIMPLE;
  1011. //}
  1012. else
  1013. {
  1014. *pwLicenseDetail = pChallengeResponse->wLicenseDetailLevel;
  1015. }
  1016. }
  1017. return dwStatus;
  1018. }
  1019. //++----------------------------------------------------------------------------
  1020. error_status_t
  1021. TLSRpcIssuePlatformChallenge(
  1022. /* [in] */ PCONTEXT_HANDLE phContext,
  1023. /* [in] */ DWORD dwClientInfo,
  1024. /* [ref][out] */ PCHALLENGE_CONTEXT pChallengeContext,
  1025. /* [out] */ PDWORD pcbChallengeData,
  1026. /* [size_is][size_is][out] */ BYTE __RPC_FAR *__RPC_FAR *pChallengeData,
  1027. /* [ref][out][in] */ PDWORD dwErrCode
  1028. )
  1029. /*++
  1030. Description:
  1031. Issue a platform challenge to hydra client.
  1032. Arguments:
  1033. phContext - client context handle
  1034. dwClientInfo - client info.
  1035. pChallengeContext - pointer to client challenge context.
  1036. pcbChallengeData - size of challenge data.
  1037. pChallengeData - random client challenge data.
  1038. Returns via dwErrCode:
  1039. LSERVER_S_SUCCESS
  1040. LSERVER_E_OUTOFMEMORY Out of memory
  1041. LSERVER_E_INVALID_DATA Invalid client info.
  1042. LSERVER_E_SERVER_BUSY Server is busy
  1043. ++*/
  1044. {
  1045. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  1046. LPCLIENTCHALLENGECONTEXT lpChallenge=NULL;
  1047. DWORD status=ERROR_SUCCESS;
  1048. DBGPrintf(
  1049. DBG_INFORMATION,
  1050. DBG_FACILITY_RPC,
  1051. DBGLEVEL_FUNCTION_TRACE,
  1052. _TEXT("%s : TLSRpcIssuePlatformChallenge\n"),
  1053. lpContext->m_Client
  1054. );
  1055. InterlockedIncrement( &lpContext->m_RefCount );
  1056. try {
  1057. do {
  1058. status=TLSGenerateChallengeData(
  1059. dwClientInfo,
  1060. pcbChallengeData,
  1061. pChallengeData
  1062. );
  1063. if(status != ERROR_SUCCESS)
  1064. {
  1065. break;
  1066. }
  1067. *pChallengeContext = dwClientInfo;
  1068. } while (FALSE);
  1069. if(status != ERROR_SUCCESS)
  1070. {
  1071. if(*pChallengeData)
  1072. {
  1073. midl_user_free(*pChallengeData);
  1074. *pChallengeData = NULL;
  1075. }
  1076. *pcbChallengeData=0;
  1077. }
  1078. }
  1079. catch(...) {
  1080. status = TLS_E_INTERNAL;
  1081. }
  1082. lpContext->m_LastError=status;
  1083. #if DBG
  1084. lpContext->m_LastCall = RPC_CALL_ISSUEPLATFORMCHLLENGE;
  1085. #endif
  1086. InterlockedDecrement( &lpContext->m_RefCount );
  1087. *dwErrCode = TLSMapReturnCode(status);
  1088. return RPC_S_OK;
  1089. }
  1090. //-------------------------------------------------------------------------------
  1091. error_status_t
  1092. TLSRpcRequestNewLicense(
  1093. /* [in] */ PCONTEXT_HANDLE phContext,
  1094. /* [in] */ const CHALLENGE_CONTEXT ChallengeContext,
  1095. /* [in] */ TLSLICENSEREQUEST __RPC_FAR *pRequest,
  1096. /* [string][in] */ LPTSTR szMachineName,
  1097. /* [string][in] */ LPTSTR szUserName,
  1098. /* [in] */ const DWORD cbChallengeResponse,
  1099. /* [size_is][in] */ const PBYTE pbChallenge,
  1100. /* [in] */ BOOL bAcceptTemporaryLicense,
  1101. /* [out] */ PDWORD pcbLicense,
  1102. /* [size_is][size_is][out] */ BYTE __RPC_FAR *__RPC_FAR *ppbLicense,
  1103. /* [ref][out][in] */ PDWORD pdwErrCode
  1104. )
  1105. /*++
  1106. Description:
  1107. Routine to issue new license to hydra client based on product requested,
  1108. it returns existing license if client already has a license and the
  1109. license is not expired/returned/revoked, if request product has not been
  1110. installed, it will issue a temporary license, if license found is temporary
  1111. or expired, it will tried to upgrade/re-issue a new license with latest
  1112. version of requested product, if the existing license is temporary and
  1113. no license can be issued, it returns LSERVER_E_LICENSE_EXPIRED
  1114. Arguments:
  1115. phContext - client context handle.
  1116. ChallengeContext - client challenge context handle, return from
  1117. call TLSRpcIssuePlatformChallenge()
  1118. pRequest - product license request.
  1119. pMachineName - client's machine name.
  1120. pUserName - client user name.
  1121. cbChallengeResponse - size of the client's response to license server's
  1122. platform challenge.
  1123. pbChallenge - client's response to license server's platform challenge
  1124. bAcceptTemporaryLicense - TRUE if client wants temp. license FALSE otherwise.
  1125. pcbLicense - size of return license.
  1126. ppLicense - return license, could be old license
  1127. Return Value:
  1128. LSERVER_S_SUCCESS
  1129. LSERVER_E_OUTOFMEMORY
  1130. LSERVER_E_SERVER_BUSY Server is busy to process request.
  1131. LSERVER_E_INVALID_DATA Invalid platform challenge response.
  1132. LSERVER_E_NO_LICENSE No license available.
  1133. LSERVER_E_NO_PRODUCT Request product is not installed on server.
  1134. LSERVER_E_LICENSE_REJECTED License request is rejected by cert. server
  1135. LSERVER_E_LICENSE_REVOKED Old license found and has been revoked
  1136. LSERVER_E_LICENSE_EXPIRED Request product's license has expired
  1137. LSERVER_E_CORRUPT_DATABASE Corrupted database.
  1138. LSERVER_E_INTERNAL_ERROR Internal error in license server
  1139. LSERVER_I_PROXIMATE_LICENSE Closest match license returned.
  1140. LSERVER_I_TEMPORARY_LICENSE Temporary license has been issued
  1141. LSERVER_I_LICENSE_UPGRADED Old license has been upgraded.
  1142. ++*/
  1143. {
  1144. DWORD dwSupportFlags = 0;
  1145. return TLSRpcRequestNewLicenseEx(
  1146. phContext,
  1147. &dwSupportFlags,
  1148. ChallengeContext,
  1149. pRequest,
  1150. szMachineName,
  1151. szUserName,
  1152. cbChallengeResponse,
  1153. pbChallenge,
  1154. bAcceptTemporaryLicense,
  1155. 1, // dwQuantity
  1156. pcbLicense,
  1157. ppbLicense,
  1158. pdwErrCode
  1159. );
  1160. }
  1161. error_status_t
  1162. TLSRpcRequestNewLicenseEx(
  1163. /* [in] */ PCONTEXT_HANDLE phContext,
  1164. /* [in, out] */ DWORD *pdwSupportFlags,
  1165. /* [in] */ const CHALLENGE_CONTEXT ChallengeContext,
  1166. /* [in] */ TLSLICENSEREQUEST __RPC_FAR *pRequest,
  1167. /* [string][in] */ LPTSTR szMachineName,
  1168. /* [string][in] */ LPTSTR szUserName,
  1169. /* [in] */ const DWORD cbChallengeResponse,
  1170. /* [size_is][in] */ const PBYTE pbChallenge,
  1171. /* [in] */ BOOL bAcceptTemporaryLicense,
  1172. /* [in] */ DWORD dwQuantity,
  1173. /* [out] */ PDWORD pcbLicense,
  1174. /* [size_is][size_is][out] */ BYTE __RPC_FAR *__RPC_FAR *ppbLicense,
  1175. /* [ref][out][in] */ PDWORD pdwErrCode
  1176. )
  1177. /*++
  1178. Description:
  1179. Routine to issue new license to hydra client based on product requested
  1180. and input support flags.
  1181. *pdwSupportFlags == 0:
  1182. it returns existing license if client already has a license and the
  1183. license is not expired/returned/revoked, if request product has not
  1184. been installed, it will issue a temporary license, if license found is
  1185. temporary or expired, it will tried to upgrade/re-issue a new license
  1186. with latest version of requested product, if the existing license is
  1187. temporary and no license can be issued, it returns
  1188. LSERVER_E_LICENSE_EXPIRED
  1189. *pdwSupportFlags & SUPPORT_PER_SEAT_POST_LOGON:
  1190. For non-per-seat licenses, it behaves as if the flag wasn't set.
  1191. For per-seat licenses, if bAcceptTemporaryLicense is TRUE, it always
  1192. returns a temporary license. If bAcceptTemporaryLicense if FALSE, it
  1193. returns LSERVER_E_NO_LICENSE.
  1194. Arguments:
  1195. phContext - client context handle.
  1196. pdwSupportFlags - on input, abilities supported by TS. on output,
  1197. abilities supported by both TS and LS
  1198. ChallengeContext - client challenge context handle, return from
  1199. call TLSRpcIssuePlatformChallenge()
  1200. pRequest - product license request.
  1201. pMachineName - client's machine name.
  1202. pUserName - client user name.
  1203. cbChallengeResponse - size of the client's response to license server's
  1204. platform challenge.
  1205. pbChallenge - client's response to license server's platform challenge
  1206. bAcceptTemporaryLicense - TRUE if client wants temp. license FALSE
  1207. otherwise.
  1208. dwQuantity - number of licenses to allocate
  1209. pcbLicense - size of return license.
  1210. ppLicense - return license, could be old license
  1211. Return Value:
  1212. LSERVER_S_SUCCESS
  1213. LSERVER_E_OUTOFMEMORY
  1214. LSERVER_E_SERVER_BUSY Server is busy to process request.
  1215. LSERVER_E_INVALID_DATA Invalid platform challenge response.
  1216. LSERVER_E_NO_LICENSE No license available.
  1217. LSERVER_E_NO_PRODUCT Request product is not installed on server.
  1218. LSERVER_E_LICENSE_REJECTED License request is rejected by cert. server
  1219. LSERVER_E_LICENSE_REVOKED Old license found and has been revoked
  1220. LSERVER_E_LICENSE_EXPIRED Request product's license has expired
  1221. LSERVER_E_CORRUPT_DATABASE Corrupted database.
  1222. LSERVER_E_INTERNAL_ERROR Internal error in license server
  1223. LSERVER_I_PROXIMATE_LICENSE Closest match license returned.
  1224. LSERVER_I_TEMPORARY_LICENSE Temporary license has been issued
  1225. LSERVER_I_LICENSE_UPGRADED Old license has been upgraded.
  1226. ++*/
  1227. {
  1228. return TLSRpcRequestNewLicenseExEx(
  1229. phContext,
  1230. pdwSupportFlags,
  1231. ChallengeContext,
  1232. pRequest,
  1233. szMachineName,
  1234. szUserName,
  1235. cbChallengeResponse,
  1236. pbChallenge,
  1237. bAcceptTemporaryLicense,
  1238. FALSE, // bAcceptFewerLicense
  1239. &dwQuantity,
  1240. pcbLicense,
  1241. ppbLicense,
  1242. pdwErrCode
  1243. );
  1244. }
  1245. error_status_t
  1246. TLSRpcRequestNewLicenseExEx(
  1247. /* [in] */ PCONTEXT_HANDLE phContext,
  1248. /* [in, out] */ DWORD *pdwSupportFlags,
  1249. /* [in] */ const CHALLENGE_CONTEXT ChallengeContext,
  1250. /* [in] */ TLSLICENSEREQUEST __RPC_FAR *pRequest,
  1251. /* [string][in] */ LPTSTR szMachineName,
  1252. /* [string][in] */ LPTSTR szUserName,
  1253. /* [in] */ const DWORD cbChallengeResponse,
  1254. /* [size_is][in] */ const PBYTE pbChallenge,
  1255. /* [in] */ BOOL bAcceptTemporaryLicense,
  1256. /* [in] */ BOOL bAcceptFewerLicenses,
  1257. /* [in,out] */ DWORD *pdwQuantity,
  1258. /* [out] */ PDWORD pcbLicense,
  1259. /* [size_is][size_is][out] */ BYTE __RPC_FAR *__RPC_FAR *ppbLicense,
  1260. /* [ref][out][in] */ PDWORD pdwErrCode
  1261. )
  1262. /*++
  1263. Description:
  1264. Routine to issue new license to hydra client based on product requested
  1265. and input support flags.
  1266. *pdwSupportFlags == 0:
  1267. it returns existing license if client already has a license and the
  1268. license is not expired/returned/revoked, if request product has not
  1269. been installed, it will issue a temporary license, if license found is
  1270. temporary or expired, it will tried to upgrade/re-issue a new license
  1271. with latest version of requested product, if the existing license is
  1272. temporary and no license can be issued, it returns
  1273. LSERVER_E_LICENSE_EXPIRED
  1274. *pdwSupportFlags & SUPPORT_PER_SEAT_POST_LOGON:
  1275. For non-per-seat licenses, it behaves as if the flag wasn't set.
  1276. For per-seat licenses, if bAcceptTemporaryLicense is TRUE, it always
  1277. returns a temporary license. If bAcceptTemporaryLicense if FALSE, it
  1278. returns LSERVER_E_NO_LICENSE.
  1279. Arguments:
  1280. phContext - client context handle.
  1281. pdwSupportFlags - on input, abilities supported by TS. on output,
  1282. abilities supported by both TS and LS
  1283. ChallengeContext - client challenge context handle, return from
  1284. call TLSRpcIssuePlatformChallenge()
  1285. pRequest - product license request.
  1286. pMachineName - client's machine name.
  1287. pUserName - client user name.
  1288. cbChallengeResponse - size of the client's response to license server's
  1289. platform challenge.
  1290. pbChallenge - client's response to license server's platform challenge
  1291. bAcceptTemporaryLicense - TRUE if client wants temp. license FALSE
  1292. otherwise.
  1293. bAcceptFewerLicenses - TRUE if succeeding with fewer licenses than
  1294. requested is acceptable
  1295. pdwQuantity - on input, number of licenses to allocate. on output,
  1296. number of licenses actually allocated
  1297. pcbLicense - size of return license.
  1298. ppLicense - return license, could be old license
  1299. Return Value:
  1300. LSERVER_S_SUCCESS
  1301. LSERVER_E_OUTOFMEMORY
  1302. LSERVER_E_SERVER_BUSY Server is busy to process request.
  1303. LSERVER_E_INVALID_DATA Invalid platform challenge response.
  1304. LSERVER_E_NO_LICENSE No license available.
  1305. LSERVER_E_NO_PRODUCT Request product is not installed on server.
  1306. LSERVER_E_LICENSE_REJECTED License request is rejected by cert. server
  1307. LSERVER_E_LICENSE_REVOKED Old license found and has been revoked
  1308. LSERVER_E_LICENSE_EXPIRED Request product's license has expired
  1309. LSERVER_E_CORRUPT_DATABASE Corrupted database.
  1310. LSERVER_E_INTERNAL_ERROR Internal error in license server
  1311. LSERVER_I_PROXIMATE_LICENSE Closest match license returned.
  1312. LSERVER_I_TEMPORARY_LICENSE Temporary license has been issued
  1313. LSERVER_I_LICENSE_UPGRADED Old license has been upgraded.
  1314. ++*/
  1315. {
  1316. PMHANDLE hClient;
  1317. DWORD status=ERROR_SUCCESS;
  1318. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  1319. TCHAR szUnknown[LSERVER_MAX_STRING_SIZE+1];
  1320. TCHAR szClientMachineName[LSERVER_MAX_STRING_SIZE];
  1321. TCHAR szClientUserName[LSERVER_MAX_STRING_SIZE];
  1322. TCHAR szCompanyName[LSERVER_MAX_STRING_SIZE+1];
  1323. TCHAR szProductId[LSERVER_MAX_STRING_SIZE+1];
  1324. TLSForwardNewLicenseRequest Forward;
  1325. TLSDBLICENSEREQUEST LsLicenseRequest;
  1326. CTLSPolicy* pPolicy=NULL;
  1327. PMLICENSEREQUEST PMLicenseRequest;
  1328. PPMLICENSEREQUEST pAdjustedRequest;
  1329. BOOL bForwardRequest = TRUE;
  1330. _se_translator_function old_trans_se_func = NULL;
  1331. old_trans_se_func = _set_se_translator( trans_se_func );
  1332. #ifdef DBG
  1333. DWORD dwStartTime=GetTickCount();
  1334. #endif
  1335. DBGPrintf(
  1336. DBG_INFORMATION,
  1337. DBG_FACILITY_RPC,
  1338. DBGLEVEL_FUNCTION_TRACE,
  1339. _TEXT("%s : TLSRpcRequestNewLicense\n"),
  1340. lpContext->m_Client
  1341. );
  1342. InterlockedIncrement( &lpContext->m_RefCount );
  1343. if ((NULL == pdwQuantity) || (0 == *pdwQuantity))
  1344. {
  1345. status = TLS_E_INVALID_DATA;
  1346. goto cleanup;
  1347. }
  1348. if(VerifyLicenseRequest(pRequest) == FALSE)
  1349. {
  1350. status = TLS_E_INVALID_DATA;
  1351. goto cleanup;
  1352. }
  1353. if(NULL == pdwSupportFlags)
  1354. {
  1355. status = TLS_E_INVALID_DATA;
  1356. goto cleanup;
  1357. }
  1358. *pdwSupportFlags &= ALL_KNOWN_SUPPORT_FLAGS;
  1359. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_REQUEST))
  1360. {
  1361. status = TLS_E_ACCESS_DENIED;
  1362. goto cleanup;
  1363. }
  1364. if(lpContext->m_ClientFlags == CLIENT_ACCESS_LSERVER)
  1365. {
  1366. //
  1367. // do not forward any request or infinite loop might
  1368. // occur.
  1369. //
  1370. bForwardRequest = FALSE;
  1371. }
  1372. Forward.m_ChallengeContext = ChallengeContext;
  1373. Forward.m_pRequest = pRequest;
  1374. Forward.m_szMachineName = szMachineName;
  1375. Forward.m_szUserName = szUserName;
  1376. Forward.m_cbChallengeResponse = cbChallengeResponse;
  1377. Forward.m_pbChallengeResponse = pbChallenge;
  1378. memset(szCompanyName, 0, sizeof(szCompanyName));
  1379. memset(szProductId, 0, sizeof(szProductId));
  1380. memcpy(
  1381. szCompanyName,
  1382. pRequest->ProductInfo.pbCompanyName,
  1383. min(pRequest->ProductInfo.cbCompanyName, sizeof(szCompanyName)-sizeof(TCHAR))
  1384. );
  1385. memcpy(
  1386. szProductId,
  1387. pRequest->ProductInfo.pbProductID,
  1388. min(pRequest->ProductInfo.cbProductID, sizeof(szProductId)-sizeof(TCHAR))
  1389. );
  1390. //
  1391. // Acquire policy module, a default policy module will
  1392. // be returned.
  1393. //
  1394. pPolicy = AcquirePolicyModule(
  1395. szCompanyName, //(LPCTSTR)pRequest->ProductInfo.pbCompanyName,
  1396. szProductId, //(LPCTSTR)pRequest->ProductInfo.pbProductID
  1397. FALSE
  1398. );
  1399. if(pPolicy == NULL)
  1400. {
  1401. status = TLS_E_INTERNAL;
  1402. goto cleanup;
  1403. }
  1404. hClient = GenerateClientId();
  1405. //
  1406. // return error if string is too big.
  1407. //
  1408. LoadResourceString(
  1409. IDS_UNKNOWN_STRING,
  1410. szUnknown,
  1411. sizeof(szUnknown)/sizeof(szUnknown[0])
  1412. );
  1413. _tcsncpy(szClientMachineName,
  1414. (szMachineName) ? szMachineName : szUnknown,
  1415. LSERVER_MAX_STRING_SIZE
  1416. );
  1417. szClientMachineName[LSERVER_MAX_STRING_SIZE-1] = 0;
  1418. _tcsncpy(szClientUserName,
  1419. (szUserName) ? szUserName : szUnknown,
  1420. LSERVER_MAX_STRING_SIZE
  1421. );
  1422. szClientUserName[LSERVER_MAX_STRING_SIZE-1] = 0;
  1423. //
  1424. // Convert request to PMLICENSEREQUEST
  1425. //
  1426. TlsLicenseRequestToPMLicenseRequest(
  1427. LICENSETYPE_LICENSE,
  1428. pRequest,
  1429. szClientMachineName,
  1430. szClientUserName,
  1431. *pdwSupportFlags,
  1432. &PMLicenseRequest
  1433. );
  1434. //
  1435. // Inform Policy module start of new license request
  1436. //
  1437. status = pPolicy->PMLicenseRequest(
  1438. hClient,
  1439. REQUEST_NEW,
  1440. (PVOID) &PMLicenseRequest,
  1441. (PVOID *) &pAdjustedRequest
  1442. );
  1443. if(status != ERROR_SUCCESS)
  1444. {
  1445. goto cleanup;
  1446. }
  1447. if(pAdjustedRequest != NULL)
  1448. {
  1449. if(_tcsicmp(PMLicenseRequest.pszCompanyName,pAdjustedRequest->pszCompanyName) != 0)
  1450. {
  1451. // try to steal license from other company???
  1452. TLSLogEvent(
  1453. EVENTLOG_ERROR_TYPE,
  1454. TLS_E_POLICYERROR,
  1455. status = TLS_E_POLICYMODULEERROR,
  1456. pPolicy->GetCompanyName(),
  1457. pPolicy->GetProductId()
  1458. );
  1459. goto cleanup;
  1460. }
  1461. }
  1462. else
  1463. {
  1464. pAdjustedRequest = &PMLicenseRequest;
  1465. }
  1466. //
  1467. // form DB request structure
  1468. //
  1469. status = TLSFormDBRequest(
  1470. pRequest->pbEncryptedHwid,
  1471. pRequest->cbEncryptedHwid,
  1472. pAdjustedRequest->dwProductVersion,
  1473. pAdjustedRequest->pszCompanyName,
  1474. pAdjustedRequest->pszProductId,
  1475. pAdjustedRequest->dwLanguageId,
  1476. pAdjustedRequest->dwPlatformId,
  1477. pAdjustedRequest->pszMachineName,
  1478. pAdjustedRequest->pszUserName,
  1479. &LsLicenseRequest
  1480. );
  1481. if(status != ERROR_SUCCESS)
  1482. {
  1483. goto cleanup;
  1484. }
  1485. LsLicenseRequest.pPolicy = pPolicy;
  1486. LsLicenseRequest.hClient = hClient;
  1487. LsLicenseRequest.pPolicyLicenseRequest = pAdjustedRequest;
  1488. LsLicenseRequest.pClientLicenseRequest = &PMLicenseRequest;
  1489. try {
  1490. status = TLSVerifyChallengeDataGetWantedLicenseLevel(
  1491. ChallengeContext,
  1492. cbChallengeResponse,
  1493. pbChallenge,
  1494. &LsLicenseRequest.wLicenseDetail
  1495. );
  1496. if( status == ERROR_SUCCESS )
  1497. {
  1498. status = TLSNewLicenseRequest(
  1499. bForwardRequest,
  1500. pdwSupportFlags,
  1501. &Forward,
  1502. &LsLicenseRequest,
  1503. bAcceptTemporaryLicense,
  1504. pAdjustedRequest->fTemporary,
  1505. TRUE, // bFindLostLicense
  1506. bAcceptFewerLicenses,
  1507. pdwQuantity,
  1508. pcbLicense,
  1509. ppbLicense
  1510. );
  1511. }
  1512. }
  1513. catch( SE_Exception e ) {
  1514. status = e.getSeNumber();
  1515. }
  1516. catch(...) {
  1517. status = TLS_E_INTERNAL;
  1518. }
  1519. cleanup:
  1520. lpContext->m_LastError=status;
  1521. InterlockedDecrement( &lpContext->m_RefCount );
  1522. #if DBG
  1523. lpContext->m_LastCall = RPC_CALL_ISSUENEWLICENSE;
  1524. #endif
  1525. #ifdef DBG
  1526. DBGPrintf(
  1527. DBG_INFORMATION,
  1528. DBG_FACILITY_RPC,
  1529. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  1530. _TEXT("\t%s : TLSRpcRequestNewLicense() takes %dms\n"),
  1531. lpContext->m_Client,
  1532. GetTickCount() - dwStartTime
  1533. );
  1534. #endif
  1535. *pdwErrCode = TLSMapReturnCode(status);
  1536. if(pPolicy)
  1537. {
  1538. pPolicy->PMLicenseRequest(
  1539. hClient,
  1540. REQUEST_COMPLETE,
  1541. UlongToPtr(status),
  1542. NULL
  1543. );
  1544. ReleasePolicyModule(pPolicy);
  1545. }
  1546. //
  1547. // Reset SE translator
  1548. //
  1549. _set_se_translator(old_trans_se_func);
  1550. return RPC_S_OK;
  1551. }
  1552. //-------------------------------------------------------------------------------
  1553. error_status_t
  1554. TLSRpcUpgradeLicense(
  1555. /* [in] */ PCONTEXT_HANDLE phContext,
  1556. /* [in] */ TLSLICENSEREQUEST __RPC_FAR *pRequest,
  1557. /* [in] */ const CHALLENGE_CONTEXT ChallengeContext,
  1558. /* [in] */ const DWORD cbChallengeResponse,
  1559. /* [size_is][in] */ const PBYTE pbChallenge,
  1560. /* [in] */ DWORD cbOldLicense,
  1561. /* [size_is][in] */ PBYTE pbOldLicense,
  1562. /* [out] */ PDWORD pcbNewLicense,
  1563. /* [size_is][size_is][out] */ PBYTE __RPC_FAR *ppbNewLicense,
  1564. /* [ref][out][in] */ PDWORD dwErrCode
  1565. )
  1566. /*++
  1567. Description:
  1568. Update an old license.
  1569. Arguments:
  1570. Return Value:
  1571. LSERVER_S_SUCCESS
  1572. TLS_E_INTERNAL
  1573. LSERVER_E_INTERNAL_ERROR
  1574. LSERVER_E_INVALID_DATA old license is invalid.
  1575. LSERVER_E_NO_LICENSE no available license
  1576. LSERVER_E_NO_PRODUCT request product not install in current server.
  1577. LSERVER_E_CORRUPT_DATABASE Corrupted database.
  1578. LSERVER_E_LICENSE_REJECTED License request rejected by cert. server.
  1579. LSERVER_E_SERVER_BUSY
  1580. ++*/
  1581. {
  1582. DWORD dwSupportFlags = 0;
  1583. return TLSRpcUpgradeLicenseEx(
  1584. phContext,
  1585. &dwSupportFlags,
  1586. pRequest,
  1587. ChallengeContext,
  1588. cbChallengeResponse,
  1589. pbChallenge,
  1590. cbOldLicense,
  1591. pbOldLicense,
  1592. 1, // dwQuantity
  1593. pcbNewLicense,
  1594. ppbNewLicense,
  1595. dwErrCode
  1596. );
  1597. }
  1598. //-------------------------------------------------------------------------------
  1599. error_status_t
  1600. TLSRpcUpgradeLicenseEx(
  1601. /* [in] */ PCONTEXT_HANDLE phContext,
  1602. /* [in,out] */ DWORD *pdwSupportFlags,
  1603. /* [in] */ TLSLICENSEREQUEST __RPC_FAR *pRequest,
  1604. /* [in] */ const CHALLENGE_CONTEXT ChallengeContext,
  1605. /* [in] */ const DWORD cbChallengeResponse,
  1606. /* [size_is][in] */ const PBYTE pbChallenge,
  1607. /* [in] */ DWORD cbOldLicense,
  1608. /* [size_is][in] */ PBYTE pbOldLicense,
  1609. /* [in] */ DWORD dwQuantity,
  1610. /* [out] */ PDWORD pcbNewLicense,
  1611. /* [size_is][size_is][out] */ PBYTE __RPC_FAR *ppbNewLicense,
  1612. /* [ref][out][in] */ PDWORD dwErrCode
  1613. )
  1614. /*++
  1615. Description:
  1616. Update an old license. Behavior varies depending on product requested,
  1617. the old license, and input support flags.
  1618. *pdwSupportFlags == 0:
  1619. it returns existing license if client already has a current-version
  1620. license and the license is not expired/returned/revoked. if requested
  1621. product has not been installed, it will issue a temporary license (if
  1622. the client doesn't already have one). if old license is temporary
  1623. or expired, it will try to upgrade/re-issue a new license
  1624. with latest version of requested product. if the existing license is
  1625. temporary and no license can be issued, it returns
  1626. LSERVER_E_LICENSE_EXPIRED
  1627. *pdwSupportFlags & SUPPORT_PER_SEAT_POST_LOGON:
  1628. For non-per-seat licenses, it behaves as if the flag wasn't set.
  1629. For per-seat licenses, if the old license isn't current-version
  1630. temporary, it also behaves as if the flag wasn't set.
  1631. Otherwise, it checks that the temporary license was marked as having
  1632. been authenticated. If so, it tries to issue a permanent license.
  1633. If a license can't be issued, or if he temporary license wasn't marked,
  1634. it returns the old license.
  1635. Arguments:
  1636. phContext - client context handle.
  1637. pdwSupportFlags - on input, abilities supported by TS. on output,
  1638. abilities supported by both TS and LS
  1639. pRequest - product license request.
  1640. ChallengeContext - client challenge context handle, return from
  1641. call TLSRpcIssuePlatformChallenge()
  1642. cbChallengeResponse - size of the client's response to license server's
  1643. platform challenge.
  1644. pbChallenge - client's response to license server's platform challenge
  1645. cbOldLicense - size of old license.
  1646. pbOldLicense - old license
  1647. dwQuantity - number of licenses to allocate
  1648. pcbNewLicense - size of return license.
  1649. ppbNewLicense - return license, could be old license
  1650. Return Value:
  1651. LSERVER_S_SUCCESS
  1652. TLS_E_INTERNAL
  1653. LSERVER_E_INTERNAL_ERROR
  1654. LSERVER_E_INVALID_DATA old license is invalid.
  1655. LSERVER_E_NO_LICENSE no available license
  1656. LSERVER_E_NO_PRODUCT request product not install in current server.
  1657. LSERVER_E_CORRUPT_DATABASE Corrupted database.
  1658. LSERVER_E_LICENSE_REJECTED License request rejected by cert. server.
  1659. LSERVER_E_SERVER_BUSY
  1660. ++*/
  1661. {
  1662. DWORD status = ERROR_SUCCESS;
  1663. BOOL bTemporaryLicense;
  1664. PMUPGRADEREQUEST pmRequestUpgrade;
  1665. PMLICENSEREQUEST pmLicenseRequest;
  1666. PPMLICENSEREQUEST pmAdjustedRequest;
  1667. PPMLICENSEDPRODUCT ppmLicensedProduct=NULL;
  1668. DWORD dwNumLicensedProduct=0;
  1669. PLICENSEDPRODUCT pLicensedProduct=NULL;
  1670. TLSDBLICENSEREQUEST LsLicenseRequest;
  1671. PMHANDLE hClient;
  1672. CTLSPolicy* pPolicy=NULL;
  1673. DWORD dwNumPermLicense;
  1674. DWORD dwNumTempLicense;
  1675. TLSForwardUpgradeLicenseRequest Forward;
  1676. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  1677. LICENSEDCLIENT license;
  1678. LICENSEPACK keypack;
  1679. DWORD index;
  1680. BOOL bForwardRequest = TRUE;
  1681. _se_translator_function old_trans_se_func = NULL;
  1682. old_trans_se_func = _set_se_translator( trans_se_func );
  1683. DBGPrintf(
  1684. DBG_INFORMATION,
  1685. DBG_FACILITY_RPC,
  1686. DBGLEVEL_FUNCTION_TRACE,
  1687. _TEXT("%s : TLSRpcUpgradeLicense\n"),
  1688. lpContext->m_Client
  1689. );
  1690. InterlockedIncrement( &lpContext->m_RefCount );
  1691. if (1 != dwQuantity)
  1692. {
  1693. status = TLS_E_INVALID_DATA;
  1694. goto cleanup;
  1695. }
  1696. if(VerifyLicenseRequest(pRequest) == FALSE)
  1697. {
  1698. status = TLS_E_INVALID_DATA;
  1699. goto cleanup;
  1700. }
  1701. if(NULL == pdwSupportFlags)
  1702. {
  1703. status = TLS_E_INVALID_DATA;
  1704. goto cleanup;
  1705. }
  1706. *pdwSupportFlags &= ALL_KNOWN_SUPPORT_FLAGS;
  1707. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_REQUEST))
  1708. {
  1709. status = TLS_E_ACCESS_DENIED;
  1710. goto cleanup;
  1711. }
  1712. if(lpContext->m_ClientFlags == CLIENT_ACCESS_LSERVER)
  1713. {
  1714. //
  1715. // do not forward any request or infinite loop might
  1716. // occur.
  1717. //
  1718. bForwardRequest = FALSE;
  1719. }
  1720. //
  1721. // Convert blob to licensed product structure
  1722. //
  1723. status = LSVerifyDecodeClientLicense(
  1724. pbOldLicense,
  1725. cbOldLicense,
  1726. g_pbSecretKey,
  1727. g_cbSecretKey,
  1728. &dwNumLicensedProduct,
  1729. NULL
  1730. );
  1731. if(status != LICENSE_STATUS_OK || dwNumLicensedProduct == 0)
  1732. {
  1733. status = TLS_E_INVALID_LICENSE;
  1734. goto cleanup;
  1735. }
  1736. pLicensedProduct = (PLICENSEDPRODUCT)AllocateMemory(
  1737. dwNumLicensedProduct * sizeof(LICENSEDPRODUCT)
  1738. );
  1739. if(pLicensedProduct == NULL)
  1740. {
  1741. status = TLS_E_ALLOCATE_MEMORY;
  1742. goto cleanup;
  1743. }
  1744. status = LSVerifyDecodeClientLicense(
  1745. pbOldLicense,
  1746. cbOldLicense,
  1747. g_pbSecretKey,
  1748. g_cbSecretKey,
  1749. &dwNumLicensedProduct,
  1750. pLicensedProduct
  1751. );
  1752. if(status != LICENSE_STATUS_OK)
  1753. {
  1754. status = TLS_E_INVALID_LICENSE;
  1755. goto cleanup;
  1756. }
  1757. //
  1758. // Verify licensed product array.
  1759. //
  1760. for(index = 1; index < dwNumLicensedProduct; index++)
  1761. {
  1762. //
  1763. // licensed product array always sorted in decending order
  1764. //
  1765. //
  1766. // Product ID in original request in licensed product must
  1767. // be the same otherwise invalid license.
  1768. //
  1769. if(pLicensedProduct->cbOrgProductID != (pLicensedProduct-1)->cbOrgProductID)
  1770. {
  1771. status = TLS_E_INVALID_LICENSE;
  1772. break;
  1773. }
  1774. if( memcmp(
  1775. pLicensedProduct->pbOrgProductID,
  1776. (pLicensedProduct-1)->pbOrgProductID,
  1777. pLicensedProduct->cbOrgProductID) != 0 )
  1778. {
  1779. status = TLS_E_INVALID_LICENSE;
  1780. goto cleanup;
  1781. }
  1782. if( (pLicensedProduct->pLicensedVersion->dwFlags & LICENSED_VERSION_TEMPORARY) )
  1783. {
  1784. //
  1785. // only latest licensed version can be temporary
  1786. //
  1787. status = TLS_E_INVALID_LICENSE;
  1788. goto cleanup;
  1789. }
  1790. }
  1791. //
  1792. // Find the policy module
  1793. //
  1794. hClient = GenerateClientId();
  1795. TCHAR szCompanyName[LSERVER_MAX_STRING_SIZE+1];
  1796. TCHAR szProductId[LSERVER_MAX_STRING_SIZE+1];
  1797. memset(szCompanyName, 0, sizeof(szCompanyName));
  1798. memset(szProductId, 0, sizeof(szProductId));
  1799. memcpy(
  1800. szCompanyName,
  1801. pRequest->ProductInfo.pbCompanyName,
  1802. min(pRequest->ProductInfo.cbCompanyName, sizeof(szCompanyName)-sizeof(TCHAR))
  1803. );
  1804. memcpy(
  1805. szProductId,
  1806. pRequest->ProductInfo.pbProductID,
  1807. min(pRequest->ProductInfo.cbProductID, sizeof(szProductId)-sizeof(TCHAR))
  1808. );
  1809. //
  1810. // Acquire policy module, a default policy module will
  1811. // be returned.
  1812. //
  1813. pPolicy = AcquirePolicyModule(
  1814. szCompanyName, // (LPCTSTR) pLicensedProduct->LicensedProduct.pProductInfo->pbCompanyName,
  1815. szProductId, // (LPCTSTR) pLicensedProduct->pbOrgProductID
  1816. FALSE
  1817. );
  1818. if(pPolicy == NULL)
  1819. {
  1820. //
  1821. // Must have a policy module, default policy module always there
  1822. //
  1823. status = TLS_E_INTERNAL;
  1824. goto cleanup;
  1825. }
  1826. //
  1827. // Convert request to PMLICENSEREQUEST
  1828. //
  1829. TlsLicenseRequestToPMLicenseRequest(
  1830. LICENSETYPE_LICENSE,
  1831. pRequest,
  1832. pLicensedProduct->szLicensedClient,
  1833. pLicensedProduct->szLicensedUser,
  1834. *pdwSupportFlags,
  1835. &pmLicenseRequest
  1836. );
  1837. //
  1838. // generate PMUPGRADEREQUEST and pass it to Policy Module
  1839. //
  1840. memset(&pmRequestUpgrade, 0, sizeof(pmRequestUpgrade));
  1841. ppmLicensedProduct = (PPMLICENSEDPRODUCT)AllocateMemory(sizeof(PMLICENSEDPRODUCT)*dwNumLicensedProduct);
  1842. if(ppmLicensedProduct == NULL)
  1843. {
  1844. status = TLS_E_ALLOCATE_MEMORY;
  1845. goto cleanup;
  1846. }
  1847. for(index=0; index < dwNumLicensedProduct; index++)
  1848. {
  1849. ppmLicensedProduct[index].pbData =
  1850. pLicensedProduct[index].pbPolicyData;
  1851. ppmLicensedProduct[index].cbData =
  1852. pLicensedProduct[index].cbPolicyData;
  1853. ppmLicensedProduct[index].bTemporary =
  1854. ((pLicensedProduct[index].pLicensedVersion->dwFlags & LICENSED_VERSION_TEMPORARY) != 0);
  1855. // treat license issued from beta server as temporary
  1856. if(ppmLicensedProduct[index].bTemporary == FALSE && TLSIsBetaNTServer() == FALSE)
  1857. {
  1858. if(IS_LICENSE_ISSUER_RTM(pLicensedProduct[index].pLicensedVersion->dwFlags) == FALSE)
  1859. {
  1860. ppmLicensedProduct[index].bTemporary = TRUE;
  1861. }
  1862. }
  1863. ppmLicensedProduct[index].ucMarked = 0;
  1864. if (0 == index)
  1865. {
  1866. // for first license, check markings on license
  1867. status = TLSCheckLicenseMarkRequest(
  1868. TRUE, // forward request if necessary
  1869. pLicensedProduct,
  1870. cbOldLicense,
  1871. pbOldLicense,
  1872. &(ppmLicensedProduct[index].ucMarked)
  1873. );
  1874. }
  1875. ppmLicensedProduct[index].LicensedProduct.dwProductVersion =
  1876. pLicensedProduct[index].LicensedProduct.pProductInfo->dwVersion;
  1877. ppmLicensedProduct[index].LicensedProduct.pszProductId =
  1878. (LPTSTR)(pLicensedProduct[index].LicensedProduct.pProductInfo->pbProductID);
  1879. ppmLicensedProduct[index].LicensedProduct.pszCompanyName =
  1880. (LPTSTR)(pLicensedProduct[index].LicensedProduct.pProductInfo->pbCompanyName);
  1881. ppmLicensedProduct[index].LicensedProduct.dwLanguageId =
  1882. pLicensedProduct[index].LicensedProduct.dwLanguageID;
  1883. ppmLicensedProduct[index].LicensedProduct.dwPlatformId =
  1884. pLicensedProduct[index].LicensedProduct.dwPlatformID;
  1885. ppmLicensedProduct[index].LicensedProduct.pszMachineName =
  1886. pLicensedProduct[index].szLicensedClient;
  1887. ppmLicensedProduct[index].LicensedProduct.pszUserName =
  1888. pLicensedProduct[index].szLicensedUser;
  1889. }
  1890. pmRequestUpgrade.pbOldLicense = pbOldLicense;
  1891. pmRequestUpgrade.cbOldLicense = cbOldLicense;
  1892. pmRequestUpgrade.pUpgradeRequest = &pmLicenseRequest;
  1893. pmRequestUpgrade.dwNumProduct = dwNumLicensedProduct;
  1894. pmRequestUpgrade.pProduct = ppmLicensedProduct;
  1895. status = pPolicy->PMLicenseUpgrade(
  1896. hClient,
  1897. REQUEST_UPGRADE,
  1898. (PVOID)&pmRequestUpgrade,
  1899. (PVOID *) &pmAdjustedRequest
  1900. );
  1901. if(status != ERROR_SUCCESS)
  1902. {
  1903. goto cleanup;
  1904. }
  1905. if(pmAdjustedRequest != NULL)
  1906. {
  1907. if(_tcsicmp(
  1908. pmLicenseRequest.pszCompanyName,
  1909. pmAdjustedRequest->pszCompanyName
  1910. ) != 0)
  1911. {
  1912. //
  1913. // Try to steal license from other company???
  1914. //
  1915. TLSLogEvent(
  1916. EVENTLOG_ERROR_TYPE,
  1917. TLS_E_POLICYERROR,
  1918. status = TLS_E_POLICYMODULEERROR,
  1919. pPolicy->GetCompanyName(),
  1920. pPolicy->GetProductId()
  1921. );
  1922. goto cleanup;
  1923. }
  1924. }
  1925. else
  1926. {
  1927. pmAdjustedRequest = &pmLicenseRequest;
  1928. }
  1929. for(index =0; index < dwNumLicensedProduct; index++)
  1930. {
  1931. DWORD tExpireDate;
  1932. FileTimeToLicenseDate(&(pLicensedProduct[index].NotAfter),
  1933. &tExpireDate);
  1934. if( CompareTLSVersions(pmAdjustedRequest->dwProductVersion, pLicensedProduct[index].LicensedProduct.pProductInfo->dwVersion) <= 0 &&
  1935. !(pLicensedProduct[index].pLicensedVersion->dwFlags & LICENSED_VERSION_TEMPORARY) &&
  1936. _tcscmp(pmAdjustedRequest->pszProductId, (LPTSTR)(pLicensedProduct[index].LicensedProduct.pProductInfo->pbProductID)) == 0 &&
  1937. tExpireDate-g_dwReissueLeaseLeeway >= ((DWORD)time(NULL)) )
  1938. {
  1939. if( TLSIsBetaNTServer() == TRUE ||
  1940. IS_LICENSE_ISSUER_RTM(pLicensedProduct[index].pLicensedVersion->dwFlags) == TRUE )
  1941. {
  1942. //
  1943. // Blob already contain perm. license that is >= version
  1944. // requested.
  1945. //
  1946. *ppbNewLicense = (PBYTE)midl_user_allocate(cbOldLicense);
  1947. if(*ppbNewLicense != NULL)
  1948. {
  1949. memcpy(*ppbNewLicense, pbOldLicense, cbOldLicense);
  1950. *pcbNewLicense = cbOldLicense;
  1951. status = ERROR_SUCCESS;
  1952. }
  1953. else
  1954. {
  1955. status = TLS_E_ALLOCATE_MEMORY;
  1956. }
  1957. goto cleanup;
  1958. }
  1959. }
  1960. }
  1961. memset(&LsLicenseRequest, 0, sizeof(TLSDBLICENSEREQUEST));
  1962. status = TLSFormDBRequest(
  1963. pRequest->pbEncryptedHwid,
  1964. pRequest->cbEncryptedHwid,
  1965. pmAdjustedRequest->dwProductVersion,
  1966. pmAdjustedRequest->pszCompanyName,
  1967. pmAdjustedRequest->pszProductId,
  1968. pmAdjustedRequest->dwLanguageId,
  1969. pmAdjustedRequest->dwPlatformId,
  1970. pmAdjustedRequest->pszMachineName,
  1971. pmAdjustedRequest->pszUserName,
  1972. &LsLicenseRequest
  1973. );
  1974. if(status != ERROR_SUCCESS)
  1975. {
  1976. goto cleanup;
  1977. }
  1978. LsLicenseRequest.pPolicy = pPolicy;
  1979. LsLicenseRequest.hClient = hClient;
  1980. LsLicenseRequest.pPolicyLicenseRequest = pmAdjustedRequest;
  1981. LsLicenseRequest.pClientLicenseRequest = &pmLicenseRequest;
  1982. memset(&keypack, 0, sizeof(keypack));
  1983. try {
  1984. status = TLSVerifyChallengeDataGetWantedLicenseLevel(
  1985. ChallengeContext,
  1986. cbChallengeResponse,
  1987. pbChallenge,
  1988. &LsLicenseRequest.wLicenseDetail
  1989. );
  1990. if( status == ERROR_SUCCESS )
  1991. {
  1992. //
  1993. // if client challenge context handle is 0xFFFFFFFF,
  1994. // cbChallenge = 0 and pbChallenge is NULL.
  1995. // client is old version, don't verify challenge
  1996. //
  1997. Forward.m_pRequest = pRequest;
  1998. Forward.m_ChallengeContext = ChallengeContext;
  1999. Forward.m_cbChallengeResponse = cbChallengeResponse;
  2000. Forward.m_pbChallengeResponse = pbChallenge;
  2001. Forward.m_cbOldLicense = cbOldLicense;
  2002. Forward.m_pbOldLicense = pbOldLicense;
  2003. status = TLSUpgradeLicenseRequest(
  2004. bForwardRequest,
  2005. &Forward,
  2006. pdwSupportFlags,
  2007. &LsLicenseRequest,
  2008. pbOldLicense,
  2009. cbOldLicense,
  2010. dwNumLicensedProduct,
  2011. pLicensedProduct,
  2012. pmAdjustedRequest->fTemporary,
  2013. pcbNewLicense,
  2014. ppbNewLicense
  2015. );
  2016. }
  2017. } // end try
  2018. catch( SE_Exception e ) {
  2019. status = e.getSeNumber();
  2020. }
  2021. catch(...)
  2022. {
  2023. status = TLS_E_INTERNAL;
  2024. }
  2025. cleanup:
  2026. FreeMemory(ppmLicensedProduct);
  2027. for(index =0; index < dwNumLicensedProduct; index++)
  2028. {
  2029. LSFreeLicensedProduct(pLicensedProduct+index);
  2030. }
  2031. FreeMemory(pLicensedProduct);
  2032. lpContext->m_LastError=status;
  2033. InterlockedDecrement( &lpContext->m_RefCount );
  2034. #if DBG
  2035. lpContext->m_LastCall = RPC_CALL_UPGRADELICENSE;
  2036. #endif
  2037. *dwErrCode = TLSMapReturnCode(status);
  2038. if(pPolicy)
  2039. {
  2040. pPolicy->PMLicenseRequest(
  2041. hClient,
  2042. REQUEST_COMPLETE,
  2043. UlongToPtr (status),
  2044. NULL
  2045. );
  2046. ReleasePolicyModule(pPolicy);
  2047. }
  2048. //
  2049. // Reset SE translator
  2050. //
  2051. _set_se_translator(old_trans_se_func);
  2052. return RPC_S_OK;
  2053. }
  2054. //-----------------------------------------------------------------------------
  2055. error_status_t
  2056. TLSRpcCheckLicenseMark(
  2057. /* [in] */ PCONTEXT_HANDLE phContext,
  2058. /* [in] */ const DWORD cbLicense,
  2059. /* [in, size_is(cbLicense)] */ PBYTE pbLicense,
  2060. /* [out] */ UCHAR *pucMarkFlags,
  2061. /* [in, out, ref] */ PDWORD pdwErrCode
  2062. )
  2063. /*++
  2064. Description:
  2065. Check markings on the passed in license
  2066. Arguments:
  2067. phContext - client context handle
  2068. cbLicense - size of license to be checked
  2069. pbLicense - license to be checked
  2070. pucMarkFlags - markings on license
  2071. Return via pdwErrCode:
  2072. LSERVER_S_SUCCESS
  2073. LSERVER_E_INVALID_DATA Invalid parameter.
  2074. LSERVER_E_INVALID_LICENSE License passed in is bad
  2075. LSERVER_E_DATANOTFOUND license not found in database
  2076. LSERVER_E_CORRUPT_DATABASE Corrupt database
  2077. LSERVER_E_INTERNAL_ERROR Internal error in license server
  2078. Note:
  2079. This function forwards the request to the issuing license server. If
  2080. the issuer isn't available, or doesn't have the license in the database,
  2081. it searches in the local database for a license with the same HWID.
  2082. ++*/
  2083. {
  2084. DWORD status = ERROR_SUCCESS;
  2085. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  2086. DWORD dwNumLicensedProduct = 0;
  2087. PLICENSEDPRODUCT pLicensedProduct=NULL;
  2088. DWORD index;
  2089. _se_translator_function old_trans_se_func = NULL;
  2090. old_trans_se_func = _set_se_translator( trans_se_func );
  2091. DBGPrintf(
  2092. DBG_INFORMATION,
  2093. DBG_FACILITY_RPC,
  2094. DBGLEVEL_FUNCTION_TRACE,
  2095. _TEXT("%s : TLSRpcCheckLicenseMark\n"),
  2096. lpContext->m_Client
  2097. );
  2098. InterlockedIncrement( &lpContext->m_RefCount );
  2099. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_LSERVER))
  2100. {
  2101. status = TLS_E_ACCESS_DENIED;
  2102. goto cleanup;
  2103. }
  2104. if (NULL == pucMarkFlags)
  2105. {
  2106. status = TLS_E_INVALID_DATA;
  2107. goto cleanup;
  2108. }
  2109. //
  2110. // Convert blob to licensed product structure
  2111. //
  2112. status=LSVerifyDecodeClientLicense(
  2113. pbLicense,
  2114. cbLicense,
  2115. g_pbSecretKey,
  2116. g_cbSecretKey,
  2117. &dwNumLicensedProduct,
  2118. NULL // find size to allocate
  2119. );
  2120. if(status != LICENSE_STATUS_OK || dwNumLicensedProduct == 0)
  2121. {
  2122. status = TLS_E_INVALID_LICENSE;
  2123. goto cleanup;
  2124. }
  2125. pLicensedProduct = (PLICENSEDPRODUCT)AllocateMemory(
  2126. dwNumLicensedProduct * sizeof(LICENSEDPRODUCT)
  2127. );
  2128. if(pLicensedProduct == NULL)
  2129. {
  2130. status = TLS_E_ALLOCATE_MEMORY;
  2131. goto cleanup;
  2132. }
  2133. status=LSVerifyDecodeClientLicense(
  2134. pbLicense,
  2135. cbLicense,
  2136. g_pbSecretKey,
  2137. g_cbSecretKey,
  2138. &dwNumLicensedProduct,
  2139. pLicensedProduct
  2140. );
  2141. if(status != LICENSE_STATUS_OK)
  2142. {
  2143. status = TLS_E_INVALID_LICENSE;
  2144. goto cleanup;
  2145. }
  2146. try {
  2147. status = TLSCheckLicenseMarkRequest(
  2148. FALSE, // don't forward the request
  2149. pLicensedProduct,
  2150. cbLicense,
  2151. pbLicense,
  2152. pucMarkFlags
  2153. );
  2154. } // end try
  2155. catch( SE_Exception e ) {
  2156. status = e.getSeNumber();
  2157. }
  2158. catch(...)
  2159. {
  2160. status = TLS_E_INTERNAL;
  2161. }
  2162. cleanup:
  2163. for(index =0; index < dwNumLicensedProduct; index++)
  2164. {
  2165. LSFreeLicensedProduct(pLicensedProduct+index);
  2166. }
  2167. FreeMemory(pLicensedProduct);
  2168. lpContext->m_LastError=status;
  2169. *pdwErrCode = TLSMapReturnCode(status);
  2170. InterlockedDecrement( &lpContext->m_RefCount );
  2171. //
  2172. // Reset SE translator
  2173. //
  2174. _set_se_translator(old_trans_se_func);
  2175. return RPC_S_OK;
  2176. }
  2177. //-----------------------------------------------------------------------------
  2178. error_status_t
  2179. TLSRpcMarkLicense(
  2180. /* [in] */ PCONTEXT_HANDLE phContext,
  2181. /* [in] */ UCHAR ucMarkFlags,
  2182. /* [in] */ const DWORD cbLicense,
  2183. /* [in, size_is(cbLicense)] */ PBYTE pbLicense,
  2184. /* [in, out, ref] */ PDWORD pdwErrCode
  2185. )
  2186. /*++
  2187. Description:
  2188. Set markings on the passed in license
  2189. Arguments:
  2190. phContext - client context handle
  2191. ucMarkFlags - markings on license
  2192. cbLicense - size of license to be checked
  2193. pbLicense - license to be checked
  2194. Return via pdwErrCode:
  2195. LSERVER_S_SUCCESS
  2196. LSERVER_E_INVALID_DATA Invalid parameter.
  2197. LSERVER_E_INVALID_LICENSE License passed in is bad
  2198. LSERVER_E_DATANOTFOUND license not found in database
  2199. LSERVER_E_CORRUPT_DATABASE Corrupt database
  2200. LSERVER_E_INTERNAL_ERROR Internal error in license server
  2201. Note:
  2202. This function forwards the request to the issuing license server. The
  2203. issuer modifies the database entry of the license to set the markings.
  2204. ++*/
  2205. {
  2206. DWORD status = ERROR_SUCCESS;
  2207. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  2208. BOOL bForwardRequest = TRUE;
  2209. DWORD dwNumLicensedProduct = 0;
  2210. PLICENSEDPRODUCT pLicensedProduct=NULL;
  2211. DWORD index;
  2212. _se_translator_function old_trans_se_func = NULL;
  2213. old_trans_se_func = _set_se_translator( trans_se_func );
  2214. DBGPrintf(
  2215. DBG_INFORMATION,
  2216. DBG_FACILITY_RPC,
  2217. DBGLEVEL_FUNCTION_TRACE,
  2218. _TEXT("%s : TLSRpcMarkLicense\n"),
  2219. lpContext->m_Client
  2220. );
  2221. InterlockedIncrement( &lpContext->m_RefCount );
  2222. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_REQUEST))
  2223. {
  2224. status = TLS_E_ACCESS_DENIED;
  2225. goto cleanup;
  2226. }
  2227. if(lpContext->m_ClientFlags == CLIENT_ACCESS_LSERVER)
  2228. {
  2229. //
  2230. // do not forward any request or infinite loop might
  2231. // occur.
  2232. //
  2233. bForwardRequest = FALSE;
  2234. }
  2235. //
  2236. // Convert blob to licensed product structure
  2237. //
  2238. status=LSVerifyDecodeClientLicense(
  2239. pbLicense,
  2240. cbLicense,
  2241. g_pbSecretKey,
  2242. g_cbSecretKey,
  2243. &dwNumLicensedProduct,
  2244. NULL // find size to allocate
  2245. );
  2246. if(status != LICENSE_STATUS_OK || dwNumLicensedProduct == 0)
  2247. {
  2248. status = TLS_E_INVALID_LICENSE;
  2249. goto cleanup;
  2250. }
  2251. pLicensedProduct = (PLICENSEDPRODUCT)AllocateMemory(
  2252. dwNumLicensedProduct * sizeof(LICENSEDPRODUCT)
  2253. );
  2254. if(pLicensedProduct == NULL)
  2255. {
  2256. status = TLS_E_ALLOCATE_MEMORY;
  2257. goto cleanup;
  2258. }
  2259. status=LSVerifyDecodeClientLicense(
  2260. pbLicense,
  2261. cbLicense,
  2262. g_pbSecretKey,
  2263. g_cbSecretKey,
  2264. &dwNumLicensedProduct,
  2265. pLicensedProduct
  2266. );
  2267. if(status != LICENSE_STATUS_OK)
  2268. {
  2269. status = TLS_E_INVALID_LICENSE;
  2270. goto cleanup;
  2271. }
  2272. try {
  2273. status = TLSMarkLicenseRequest(
  2274. bForwardRequest,
  2275. ucMarkFlags,
  2276. pLicensedProduct,
  2277. cbLicense,
  2278. pbLicense
  2279. );
  2280. } // end try
  2281. catch( SE_Exception e ) {
  2282. status = e.getSeNumber();
  2283. }
  2284. catch(...)
  2285. {
  2286. status = TLS_E_INTERNAL;
  2287. }
  2288. cleanup:
  2289. for(index =0; index < dwNumLicensedProduct; index++)
  2290. {
  2291. LSFreeLicensedProduct(pLicensedProduct+index);
  2292. }
  2293. FreeMemory(pLicensedProduct);
  2294. lpContext->m_LastError=status;
  2295. *pdwErrCode = TLSMapReturnCode(status);
  2296. InterlockedDecrement( &lpContext->m_RefCount );
  2297. //
  2298. // Reset SE translator
  2299. //
  2300. _set_se_translator(old_trans_se_func);
  2301. return RPC_S_OK;
  2302. }
  2303. //-------------------------------------------------------------------------------
  2304. error_status_t
  2305. TLSRpcAllocateConcurrentLicense(
  2306. /* [in] */ PCONTEXT_HANDLE phContext,
  2307. /* [string][in] */ LPTSTR szHydraServer,
  2308. /* [in] */ TLSLICENSEREQUEST __RPC_FAR *pRequest,
  2309. /* [ref][out][in] */ LONG __RPC_FAR *pdwQuantity,
  2310. /* [ref][out][in] */ PDWORD pdwErrCode
  2311. )
  2312. /*++
  2313. Description:
  2314. Allocate concurrent licenses base on product.
  2315. Arguments:
  2316. phContext - client context handle
  2317. szHydraServer - name of hydra server requesting concurrent licenses
  2318. pRequest - product to request for concurrent license.
  2319. dwQuantity - See note
  2320. Return via dwErrCode:
  2321. LSERVER_S_SUCCESS
  2322. LSERVER_E_INVALID_DATA Invalid parameter.
  2323. LSERVER_E_NO_PRODUCT request product not installed
  2324. LSERVER_E_NO_LICNESE no available license for request product
  2325. LSERVER_E_LICENSE_REVOKED Request license has been revoked
  2326. LSERVER_E_LICENSE_EXPIRED Request license has expired
  2327. LSERVER_E_CORRUPT_DATABASE Corrupt database
  2328. LSERVER_E_INTERNAL_ERROR Internal error in license server
  2329. Note:
  2330. dwQuantity
  2331. Input Output
  2332. ------------------------- -----------------------------------------
  2333. 0 Total number of concurrent license
  2334. issued to hydra server.
  2335. > 0, number of license Actual number of license allocated
  2336. requested
  2337. < 0, number of license Actual number of license returned, always
  2338. to return positive value.
  2339. ++*/
  2340. {
  2341. #if 1
  2342. *pdwErrCode = TLSMapReturnCode(TLS_E_NOTSUPPORTED);
  2343. return RPC_S_OK;
  2344. #else
  2345. PMHANDLE hClient;
  2346. DWORD status=ERROR_SUCCESS;
  2347. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  2348. PTLSDbWorkSpace pDbWorkSpace;
  2349. TLSDBLICENSEREQUEST LsLicenseRequest;
  2350. TCHAR szUnknown[LSERVER_MAX_STRING_SIZE+1];
  2351. TCHAR szClientMachineName[LSERVER_MAX_STRING_SIZE];
  2352. TCHAR szClientUserName[LSERVER_MAX_STRING_SIZE];
  2353. TCHAR szCompanyName[LSERVER_MAX_STRING_SIZE+1];
  2354. TCHAR szProductId[LSERVER_MAX_STRING_SIZE+1];
  2355. _se_translator_function old_trans_se_func = NULL;
  2356. old_trans_se_func = _set_se_translator( trans_se_func );
  2357. CTLSPolicy* pPolicy = NULL;
  2358. BOOL bAllocateLicense = TRUE;
  2359. PMLICENSEREQUEST PMLicenseRequest;
  2360. PPMLICENSEREQUEST pAdjustedRequest;
  2361. #ifdef DBG
  2362. DWORD dwStartTime=GetTickCount();
  2363. #endif
  2364. DBGPrintf(
  2365. DBG_INFORMATION,
  2366. DBG_FACILITY_RPC,
  2367. DBGLEVEL_FUNCTION_TRACE,
  2368. _TEXT("%s : TLSRpcAllocateConcurrentLicense\n"),
  2369. lpContext->m_Client
  2370. );
  2371. if(*pdwQuantity > 1)
  2372. {
  2373. TLSASSERT(*pdwQuantity > 1);
  2374. }
  2375. InterlockedIncrement( &lpContext->m_RefCount );
  2376. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_REQUEST))
  2377. {
  2378. status = TLS_E_ACCESS_DENIED;
  2379. goto cleanup;
  2380. }
  2381. memset(szCompanyName, 0, sizeof(szCompanyName));
  2382. memset(szProductId, 0, sizeof(szProductId));
  2383. memcpy(
  2384. szCompanyName,
  2385. pRequest->ProductInfo.pbCompanyName,
  2386. min(pRequest->ProductInfo.cbCompanyName, sizeof(szCompanyName)-sizeof(TCHAR))
  2387. );
  2388. memcpy(
  2389. szProductId,
  2390. pRequest->ProductInfo.pbProductID,
  2391. min(pRequest->ProductInfo.cbProductID, sizeof(szProductId)-sizeof(TCHAR))
  2392. );
  2393. //
  2394. // Acquire policy module, a default policy module will
  2395. // be returned.
  2396. //
  2397. pPolicy = AcquirePolicyModule(
  2398. szCompanyName, //(LPCTSTR)pRequest->ProductInfo.pbCompanyName,
  2399. szProductId, //(LPCTSTR)pRequest->ProductInfo.pbProductID
  2400. TRUE
  2401. );
  2402. if(pPolicy == NULL)
  2403. {
  2404. status = GetLastError();
  2405. goto cleanup;
  2406. }
  2407. hClient = GenerateClientId();
  2408. //
  2409. // return error if string is too big.
  2410. //
  2411. LoadResourceString(
  2412. IDS_UNKNOWN_STRING,
  2413. szUnknown,
  2414. sizeof(szUnknown)/sizeof(szUnknown[0])
  2415. );
  2416. _tcsncpy(
  2417. szClientMachineName,
  2418. (szHydraServer) ? szHydraServer : szUnknown,
  2419. LSERVER_MAX_STRING_SIZE
  2420. );
  2421. _tcsncpy(
  2422. szClientUserName,
  2423. (szHydraServer) ? szHydraServer : szUnknown,
  2424. LSERVER_MAX_STRING_SIZE
  2425. );
  2426. //
  2427. // Convert request to PMLICENSEREQUEST
  2428. //
  2429. TlsLicenseRequestToPMLicenseRequest(
  2430. LICENSETYPE_LICENSE,
  2431. pRequest,
  2432. szClientMachineName,
  2433. szClientUserName,
  2434. 0,
  2435. &PMLicenseRequest
  2436. );
  2437. //
  2438. // Inform Policy module start of new license request
  2439. //
  2440. status = pPolicy->PMLicenseRequest(
  2441. hClient,
  2442. REQUEST_NEW,
  2443. (PVOID) &PMLicenseRequest,
  2444. (PVOID *) &pAdjustedRequest
  2445. );
  2446. if(status != ERROR_SUCCESS)
  2447. {
  2448. goto cleanup;
  2449. }
  2450. if(pAdjustedRequest != NULL)
  2451. {
  2452. if(_tcsicmp(PMLicenseRequest.pszCompanyName,pAdjustedRequest->pszCompanyName) != 0)
  2453. {
  2454. // try to steal license from other company???
  2455. TLSLogEvent(
  2456. EVENTLOG_ERROR_TYPE,
  2457. TLS_E_POLICYERROR,
  2458. status = TLS_E_POLICYMODULEERROR,
  2459. pPolicy->GetCompanyName(),
  2460. pPolicy->GetProductId()
  2461. );
  2462. goto cleanup;
  2463. }
  2464. }
  2465. else
  2466. {
  2467. pAdjustedRequest = &PMLicenseRequest;
  2468. }
  2469. //
  2470. // form DB request structure
  2471. //
  2472. status = TLSFormDBRequest(
  2473. pRequest->pbEncryptedHwid,
  2474. pRequest->cbEncryptedHwid,
  2475. pAdjustedRequest->dwProductVersion,
  2476. pAdjustedRequest->pszCompanyName,
  2477. pAdjustedRequest->pszProductId,
  2478. pAdjustedRequest->dwLanguageId,
  2479. pAdjustedRequest->dwPlatformId,
  2480. pAdjustedRequest->pszMachineName,
  2481. pAdjustedRequest->pszUserName,
  2482. &LsLicenseRequest
  2483. );
  2484. if(status != ERROR_SUCCESS)
  2485. {
  2486. goto cleanup;
  2487. }
  2488. LsLicenseRequest.pPolicy = pPolicy;
  2489. LsLicenseRequest.hClient = hClient;
  2490. LsLicenseRequest.pPolicyLicenseRequest = pAdjustedRequest;
  2491. LsLicenseRequest.pClientLicenseRequest = &PMLicenseRequest;
  2492. if(!ALLOCATEDBHANDLE(pDbWkSpace, g_GeneralDbTimeout))
  2493. {
  2494. status=TLS_E_ALLOCATE_HANDLE;
  2495. goto cleanup;
  2496. }
  2497. bAllocateLicense = (*pdwQuantity) > 0;
  2498. CLEANUPSTMT;
  2499. BEGIN_TRANSACTION(pDbWorkSpace);
  2500. try {
  2501. status = TLSDBAllocateConcurrentLicense(
  2502. USEHANDLE(pDbWorkSpace),
  2503. szHydraServer,
  2504. &LsLicenseRequest,
  2505. pdwQuantity
  2506. );
  2507. }
  2508. catch( SE_Exception e ) {
  2509. status = e.getSeNumber();
  2510. }
  2511. catch(...) {
  2512. SetLastError(status = TLS_E_INTERNAL);
  2513. }
  2514. if(TLS_ERROR(status))
  2515. {
  2516. ROLLBACK_TRANSACTION(pDbWorkSpace);
  2517. }
  2518. else
  2519. {
  2520. COMMIT_TRANSACTION(pDbWorkSpace);
  2521. }
  2522. FREEDBHANDLE(pDbWorkSpace);
  2523. cleanup:
  2524. #if DBG
  2525. lpContext->m_LastCall = RPC_CALL_ALLOCATECONCURRENT;
  2526. #endif
  2527. #ifdef DBG
  2528. DBGPrintf(
  2529. DBG_INFORMATION,
  2530. DBG_FACILITY_RPC,
  2531. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  2532. _TEXT("\t%s : TLSRpcRequestNewLicense() takes %dms\n"),
  2533. lpContext->m_Client,
  2534. GetTickCount() - dwStartTime
  2535. );
  2536. #endif
  2537. lpContext->m_LastError=status;
  2538. InterlockedDecrement( &lpContext->m_RefCount );
  2539. *dwErrCode = TLSMapReturnCode(status);
  2540. if(pPolicy)
  2541. {
  2542. pPolicy->PMLicenseRequest(
  2543. hClient,
  2544. REQUEST_COMPLETE,
  2545. (PVOID) status,
  2546. NULL
  2547. );
  2548. ReleasePolicyModule(pPolicy);
  2549. }
  2550. //
  2551. // Reset SE translator
  2552. //
  2553. _set_se_translator(old_trans_se_func);
  2554. return RPC_S_OK;
  2555. #endif
  2556. }
  2557. //-------------------------------------------------------------------------------
  2558. error_status_t
  2559. TLSRpcGetLastError(
  2560. /* [in] */ PCONTEXT_HANDLE phContext,
  2561. /* [ref][out][in] */ PDWORD cbBufferSize,
  2562. /* [size_is][string][out][in] */ LPTSTR szBuffer,
  2563. /* [ref][out][in] */ PDWORD dwErrCode
  2564. )
  2565. /*++
  2566. Description:
  2567. Return error description text for client's last LSXXX call
  2568. Arguments:
  2569. IN phContext - Client context
  2570. IN cbBufferSize - max. size of szBuffer
  2571. IN OUT szBuffer - Pointer to a buffer to receive the
  2572. null-terminated character string containing
  2573. error description
  2574. Returns via dwErrCode:
  2575. LSERVER_S_SUCCESS
  2576. TLS_E_INTERNAL No error or can't find corresponding error
  2577. description.
  2578. Error code from WideCharToMultiByte().
  2579. ++*/
  2580. {
  2581. DWORD status=ERROR_SUCCESS;
  2582. LPTSTR lpMsgBuf=NULL;
  2583. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  2584. DBGPrintf(
  2585. DBG_INFORMATION,
  2586. DBG_FACILITY_RPC,
  2587. DBGLEVEL_FUNCTION_TRACE,
  2588. _TEXT("%s : TLSRpcGetLastError\n"),
  2589. lpContext->m_Client
  2590. );
  2591. try {
  2592. DWORD dwRet;
  2593. dwRet=FormatMessage(
  2594. FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM |
  2595. FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  2596. NULL,
  2597. lpContext->m_LastError,
  2598. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  2599. (LPTSTR) &lpMsgBuf,
  2600. 0,
  2601. NULL
  2602. );
  2603. if(dwRet == 0)
  2604. {
  2605. status = GetLastError();
  2606. }
  2607. else
  2608. {
  2609. _tcsncpy(
  2610. szBuffer,
  2611. (LPTSTR)lpMsgBuf,
  2612. min(_tcslen((LPTSTR)lpMsgBuf), *cbBufferSize-1)
  2613. );
  2614. szBuffer[min(_tcslen((LPTSTR)lpMsgBuf), *cbBufferSize-1)] = _TEXT('\0');
  2615. *cbBufferSize = _tcslen(szBuffer) + 1;
  2616. }
  2617. }
  2618. catch(...) {
  2619. status = TLS_E_INTERNAL;
  2620. }
  2621. if(lpMsgBuf)
  2622. LocalFree(lpMsgBuf);
  2623. #if DBG
  2624. lpContext->m_LastCall = RPC_CALL_GET_LASTERROR;
  2625. #endif
  2626. lpContext->m_LastError=status;
  2627. *dwErrCode = TLSMapReturnCode(status);
  2628. return RPC_S_OK;
  2629. }
  2630. //-------------------------------------------------------------------------------
  2631. error_status_t
  2632. TLSRpcKeyPackEnumBegin(
  2633. /* [in] */ PCONTEXT_HANDLE phContext,
  2634. /* [in] */ DWORD dwSearchParm,
  2635. /* [in] */ BOOL bMatchAll,
  2636. /* [ref][in] */ LPLSKeyPackSearchParm lpSearchParm,
  2637. /* [ref][out][in] */ PDWORD dwErrCode
  2638. )
  2639. /*++
  2640. Description:
  2641. Function to begin enumerate through all key pack installed on server
  2642. based on search criterial.
  2643. Arguments:
  2644. phContext - client context handle.
  2645. dwSearchParm - search criterial.
  2646. bMatchAll - match all search criterial.
  2647. lpSearchParm - search parameter.
  2648. Return Value:
  2649. LSERVER_S_SUCCESS
  2650. LSERVER_E_SERVER_BUSY Server is too busy to process request
  2651. LSERVER_E_OUTOFMEMORY
  2652. TLS_E_INTERNAL
  2653. LSERVER_E_INTERNAL_ERROR
  2654. LSERVER_E_INVALID_DATA Invalid data in search parameter
  2655. LSERVER_E_INVALID_SEQUENCE Invalid calling sequence, likely, previous
  2656. enumeration has not ended.
  2657. ++*/
  2658. {
  2659. DWORD status=ERROR_SUCCESS;
  2660. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  2661. InterlockedIncrement( &lpContext->m_RefCount );
  2662. _se_translator_function old_trans_se_func = NULL;
  2663. old_trans_se_func = _set_se_translator( trans_se_func );
  2664. DBGPrintf(
  2665. DBG_INFORMATION,
  2666. DBG_FACILITY_RPC,
  2667. DBGLEVEL_FUNCTION_TRACE,
  2668. _TEXT("%s : TLSRpcKeyPackEnumBegin\n"),
  2669. lpContext->m_Client
  2670. );
  2671. //
  2672. // This will use cached db connection, in-consistency may occurred,
  2673. // visibility of changes in one connection may not appear right away
  2674. // on another connection handle, this is expected behavoir for Jet and
  2675. // so are we, user can always re-fresh.
  2676. //
  2677. do {
  2678. if(lpContext->m_ContextType != CONTEXTHANDLE_EMPTY_TYPE)
  2679. {
  2680. SetLastError(status=TLS_E_INVALID_SEQUENCE);
  2681. break;
  2682. }
  2683. try {
  2684. LPENUMHANDLE hEnum;
  2685. hEnum = TLSDBLicenseKeyPackEnumBegin(
  2686. bMatchAll,
  2687. dwSearchParm,
  2688. lpSearchParm
  2689. );
  2690. if(hEnum)
  2691. {
  2692. lpContext->m_ContextType = CONTEXTHANDLE_KEYPACK_ENUM_TYPE;
  2693. lpContext->m_ContextHandle = (PVOID)hEnum;
  2694. }
  2695. else
  2696. {
  2697. status = GetLastError();
  2698. }
  2699. }
  2700. catch( SE_Exception e ) {
  2701. status = e.getSeNumber();
  2702. }
  2703. catch(...) {
  2704. status = TLS_E_INTERNAL;
  2705. }
  2706. } while(FALSE);
  2707. lpContext->m_LastError=status;
  2708. InterlockedDecrement( &lpContext->m_RefCount );
  2709. #if DBG
  2710. lpContext->m_LastCall = RPC_CALL_KEYPACKENUMBEGIN;
  2711. #endif
  2712. *dwErrCode = TLSMapReturnCode(status);
  2713. //
  2714. // Reset SE translator
  2715. //
  2716. _set_se_translator(old_trans_se_func);
  2717. return RPC_S_OK;
  2718. }
  2719. //-------------------------------------------------------------------------------
  2720. error_status_t
  2721. TLSRpcKeyPackEnumNext(
  2722. /* [in] */ PCONTEXT_HANDLE phContext,
  2723. /* [ref][out] */ LPLSKeyPack lpKeyPack,
  2724. /* [ref][out][in] */ PDWORD dwErrCode
  2725. )
  2726. /*++
  2727. Description:
  2728. Return next key pack that match search criterial
  2729. Arguments:
  2730. phContext - client context handle
  2731. lpKeyPack - key pack that match search criterial
  2732. Return Value:
  2733. LSERVER_S_SUCCESS
  2734. LSERVER_I_NO_MORE_DATA No more keypack match search criterial
  2735. TLS_E_INTERNAL General error in license server
  2736. LSERVER_E_INTERNAL_ERROR Internal error in license server
  2737. LSERVER_E_SERVER_BUSY License server is too busy to process request
  2738. LSERVER_E_OUTOFMEMORY Can't process request due to insufficient memory
  2739. LSERVER_E_INVALID_SEQUENCE Invalid calling sequence, must call
  2740. LSKeyPackEnumBegin().
  2741. ++*/
  2742. {
  2743. DWORD status = ERROR_SUCCESS;
  2744. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  2745. BOOL bShowAll = FALSE;
  2746. _se_translator_function old_trans_se_func = NULL;
  2747. old_trans_se_func = _set_se_translator( trans_se_func );
  2748. DBGPrintf(
  2749. DBG_INFORMATION,
  2750. DBG_FACILITY_RPC,
  2751. DBGLEVEL_FUNCTION_TRACE,
  2752. _TEXT("%s : TLSRpcKeyPackEnumNext\n"),
  2753. lpContext->m_Client
  2754. );
  2755. InterlockedIncrement( &lpContext->m_RefCount );
  2756. if(lpContext->m_ClientFlags == CLIENT_ACCESS_LSERVER)
  2757. {
  2758. bShowAll = TRUE;
  2759. }
  2760. // this one might cause access violation
  2761. memset(lpKeyPack, 0, sizeof(LSKeyPack));
  2762. if(lpContext->m_ContextType != CONTEXTHANDLE_KEYPACK_ENUM_TYPE)
  2763. {
  2764. SetLastError(status=TLS_E_INVALID_SEQUENCE);
  2765. }
  2766. else
  2767. {
  2768. do {
  2769. try {
  2770. LPENUMHANDLE hEnum=(LPENUMHANDLE)lpContext->m_ContextHandle;
  2771. status=TLSDBLicenseKeyPackEnumNext(
  2772. hEnum,
  2773. lpKeyPack,
  2774. bShowAll
  2775. );
  2776. }
  2777. catch( SE_Exception e ) {
  2778. status = e.getSeNumber();
  2779. }
  2780. catch(...) {
  2781. status = TLS_E_INTERNAL;
  2782. }
  2783. } while(status == TLS_I_MORE_DATA);
  2784. }
  2785. lpContext->m_LastError=GetLastError();
  2786. InterlockedDecrement( &lpContext->m_RefCount );
  2787. #if DBG
  2788. lpContext->m_LastCall = RPC_CALL_KEYPACKENUMNEXT;
  2789. #endif
  2790. *dwErrCode = TLSMapReturnCode(status);
  2791. //
  2792. // Reset SE translator
  2793. //
  2794. _set_se_translator(old_trans_se_func);
  2795. return RPC_S_OK;
  2796. }
  2797. //-------------------------------------------------------------------------------
  2798. error_status_t
  2799. TLSRpcKeyPackEnumEnd(
  2800. /* [in] */ PCONTEXT_HANDLE phContext,
  2801. /* [ref][out][in] */ PDWORD dwErrCode
  2802. )
  2803. /*++
  2804. ++*/
  2805. {
  2806. DWORD status=ERROR_SUCCESS;
  2807. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  2808. DBGPrintf(
  2809. DBG_INFORMATION,
  2810. DBG_FACILITY_RPC,
  2811. DBGLEVEL_FUNCTION_TRACE,
  2812. _TEXT("%s : TLSRpcKeyPackEnumEnd\n"),
  2813. lpContext->m_Client
  2814. );
  2815. InterlockedIncrement( &lpContext->m_RefCount );
  2816. try {
  2817. if(lpContext->m_ContextType != CONTEXTHANDLE_KEYPACK_ENUM_TYPE)
  2818. {
  2819. SetLastError(status=ERROR_INVALID_HANDLE);
  2820. }
  2821. else
  2822. {
  2823. LPENUMHANDLE hEnum=(LPENUMHANDLE)lpContext->m_ContextHandle;
  2824. TLSDBLicenseKeyPackEnumEnd(hEnum);
  2825. lpContext->m_ContextType = CONTEXTHANDLE_EMPTY_TYPE;
  2826. lpContext->m_ContextHandle=NULL;
  2827. }
  2828. }
  2829. catch(...) {
  2830. status = TLS_E_INTERNAL;
  2831. }
  2832. lpContext->m_LastError=GetLastError();
  2833. InterlockedDecrement( &lpContext->m_RefCount );
  2834. #if DBG
  2835. lpContext->m_LastCall = RPC_CALL_KEYPACKENUMEND;
  2836. #endif
  2837. *dwErrCode = TLSMapReturnCode(status);
  2838. return RPC_S_OK;
  2839. }
  2840. //-------------------------------------------------------------------------------
  2841. error_status_t
  2842. TLSRpcKeyPackAdd(
  2843. /* [in] */ PCONTEXT_HANDLE phContext,
  2844. /* [ref][out][in] */ LPLSKeyPack lpKeypack,
  2845. /* [ref][out][in] */ PDWORD dwErrCode
  2846. )
  2847. /*++
  2848. Description:
  2849. Add a license key pack.
  2850. Arguments:
  2851. phContext - client context handle.
  2852. lpKeyPack - key pack to be added.
  2853. Return Value:
  2854. LSERVER_S_SUCCESS
  2855. LSERVER_E_INTERNAL_ERROR
  2856. TLS_E_INTERNAL
  2857. LSERVER_E_SERVER_BUSY
  2858. LSERVER_E_DUPLICATE Product already installed.
  2859. LSERVER_E_INVALID_DATA
  2860. LSERVER_E_CORRUPT_DATABASE
  2861. Note:
  2862. Application must call LSKeyPackSetStatus() to activate keypack
  2863. ++*/
  2864. {
  2865. PTLSDbWorkSpace pDbWkSpace=NULL;
  2866. DWORD status=ERROR_SUCCESS;
  2867. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  2868. InterlockedIncrement( &lpContext->m_RefCount );
  2869. DBGPrintf(
  2870. DBG_INFORMATION,
  2871. DBG_FACILITY_RPC,
  2872. DBGLEVEL_FUNCTION_TRACE,
  2873. _TEXT("%s : TLSRpcKeyPackAdd\n"),
  2874. lpContext->m_Client
  2875. );
  2876. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_ADMIN))
  2877. {
  2878. status = TLS_E_ACCESS_DENIED;
  2879. }
  2880. else
  2881. {
  2882. if(!ALLOCATEDBHANDLE(pDbWkSpace, g_GeneralDbTimeout))
  2883. {
  2884. status=TLS_E_ALLOCATE_HANDLE;
  2885. }
  2886. else
  2887. {
  2888. CLEANUPSTMT;
  2889. BEGIN_TRANSACTION(pDbWkSpace);
  2890. try {
  2891. if(lpKeypack->ucKeyPackStatus == LSKEYPACKSTATUS_ADD_LICENSE ||
  2892. lpKeypack->ucKeyPackStatus == LSKEYPACKSTATUS_REMOVE_LICENSE)
  2893. {
  2894. status = TLSDBLicenseKeyPackUpdateLicenses(
  2895. USEHANDLE(pDbWkSpace),
  2896. lpKeypack->ucKeyPackStatus == LSKEYPACKSTATUS_ADD_LICENSE,
  2897. lpKeypack
  2898. );
  2899. }
  2900. else
  2901. {
  2902. status = TLSDBLicenseKeyPackAdd(
  2903. USEHANDLE(pDbWkSpace),
  2904. lpKeypack
  2905. );
  2906. }
  2907. if(status == ERROR_SUCCESS)
  2908. {
  2909. if( _tcsicmp( lpKeypack->szCompanyName, PRODUCT_INFO_COMPANY_NAME ) == 0 )
  2910. {
  2911. //
  2912. // check with known termsrv product ID.
  2913. //
  2914. if( _tcsnicmp( lpKeypack->szProductId,
  2915. TERMSERV_PRODUCTID_SKU,
  2916. _tcslen(TERMSERV_PRODUCTID_SKU)) == 0 )
  2917. {
  2918. TLSResetLogLowLicenseWarning(
  2919. lpKeypack->szCompanyName,
  2920. TERMSERV_PRODUCTID_SKU,
  2921. MAKELONG(lpKeypack->wMinorVersion, lpKeypack->wMajorVersion),
  2922. FALSE
  2923. );
  2924. }
  2925. else if(_tcsnicmp( lpKeypack->szProductId,
  2926. TERMSERV_PRODUCTID_INTERNET_SKU,
  2927. _tcslen(TERMSERV_PRODUCTID_INTERNET_SKU)) == 0 )
  2928. {
  2929. TLSResetLogLowLicenseWarning(
  2930. lpKeypack->szCompanyName,
  2931. TERMSERV_PRODUCTID_INTERNET_SKU,
  2932. MAKELONG(lpKeypack->wMinorVersion, lpKeypack->wMajorVersion),
  2933. FALSE
  2934. );
  2935. }
  2936. else
  2937. {
  2938. TLSResetLogLowLicenseWarning(
  2939. lpKeypack->szCompanyName,
  2940. lpKeypack->szProductId,
  2941. MAKELONG(lpKeypack->wMinorVersion, lpKeypack->wMajorVersion),
  2942. FALSE
  2943. );
  2944. }
  2945. }
  2946. else
  2947. {
  2948. TLSResetLogLowLicenseWarning(
  2949. lpKeypack->szCompanyName,
  2950. lpKeypack->szProductId,
  2951. MAKELONG(lpKeypack->wMinorVersion, lpKeypack->wMajorVersion),
  2952. FALSE
  2953. );
  2954. }
  2955. }
  2956. }
  2957. catch(...) {
  2958. status = TLS_E_INTERNAL;
  2959. }
  2960. if(TLS_ERROR(status))
  2961. {
  2962. ROLLBACK_TRANSACTION(pDbWkSpace);
  2963. }
  2964. else
  2965. {
  2966. COMMIT_TRANSACTION(pDbWkSpace);
  2967. }
  2968. FREEDBHANDLE(pDbWkSpace);
  2969. }
  2970. }
  2971. //
  2972. // Post a sync work object
  2973. //
  2974. if( status == ERROR_SUCCESS )
  2975. {
  2976. if( lpKeypack->ucKeyPackType != LSKEYPACKTYPE_FREE )
  2977. {
  2978. if(TLSAnnounceLKPToAllRemoteServer(
  2979. lpKeypack->dwKeyPackId,
  2980. 0
  2981. ) != ERROR_SUCCESS)
  2982. {
  2983. TLSLogWarningEvent(TLS_W_ANNOUNCELKP_FAILED);
  2984. }
  2985. }
  2986. }
  2987. #if DBG
  2988. lpContext->m_LastCall = RPC_CALL_KEYPACKADD;
  2989. #endif
  2990. lpContext->m_LastError=status;
  2991. InterlockedDecrement( &lpContext->m_RefCount );
  2992. *dwErrCode = TLSMapReturnCode(status);
  2993. return RPC_S_OK;
  2994. }
  2995. //-------------------------------------------------------------------------------
  2996. error_status_t
  2997. TLSRpcKeyPackSetStatus(
  2998. /* [in] */ PCONTEXT_HANDLE phContext,
  2999. /* [in] */ DWORD dwSetParm,
  3000. /* [ref][in] */ LPLSKeyPack lpKeyPack,
  3001. /* [ref][out][in] */ PDWORD dwErrCode
  3002. )
  3003. /*++
  3004. Description:
  3005. Routine to activate/deactivated a key pack.
  3006. Arguments:
  3007. phContext - client context handle
  3008. dwSetParam - type of key pack status to be set.
  3009. lpKeyPack - new key pack status.
  3010. Return Value:
  3011. LSERVER_S_SUCCESS
  3012. LSERVER_E_INTERNAL_ERROR
  3013. TLS_E_INTERNAL
  3014. LSERVER_E_INVALID_DATA
  3015. LSERVER_E_SERVER_BUSY
  3016. LSERVER_E_DATANOTFOUND Key pack is not in server
  3017. LSERVER_E_CORRUPT_DATABASE
  3018. ++*/
  3019. {
  3020. PTLSDbWorkSpace pDbWkSpace=NULL;
  3021. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  3022. DWORD status=ERROR_SUCCESS;
  3023. InterlockedIncrement( &lpContext->m_RefCount );
  3024. DBGPrintf(
  3025. DBG_INFORMATION,
  3026. DBG_FACILITY_RPC,
  3027. DBGLEVEL_FUNCTION_TRACE,
  3028. _TEXT("%s : TLSRpcKeyPackSetStatus\n"),
  3029. lpContext->m_Client
  3030. );
  3031. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_ADMIN))
  3032. {
  3033. status = TLS_E_ACCESS_DENIED;
  3034. }
  3035. else if( (dwSetParm & ~(LSKEYPACK_SET_KEYPACKSTATUS | LSKEYPACK_SET_ACTIVATEDATE | LSKEYPACK_SET_EXPIREDATE)) &&
  3036. !(lpContext->m_ClientFlags & CLIENT_ACCESS_LRWIZ) )
  3037. {
  3038. status = TLS_E_INVALID_DATA;
  3039. }
  3040. else
  3041. {
  3042. if(!ALLOCATEDBHANDLE(pDbWkSpace, g_GeneralDbTimeout))
  3043. {
  3044. status=TLS_E_ALLOCATE_HANDLE;
  3045. }
  3046. else
  3047. {
  3048. CLEANUPSTMT;
  3049. BEGIN_TRANSACTION(pDbWkSpace);
  3050. try {
  3051. status=TLSDBLicenseKeyPackSetStatus(
  3052. USEHANDLE(pDbWkSpace),
  3053. dwSetParm,
  3054. lpKeyPack
  3055. );
  3056. }
  3057. catch(...) {
  3058. status = TLS_E_INTERNAL;
  3059. }
  3060. if(TLS_ERROR(status))
  3061. {
  3062. ROLLBACK_TRANSACTION(pDbWkSpace);
  3063. }
  3064. else
  3065. {
  3066. COMMIT_TRANSACTION(pDbWkSpace);
  3067. }
  3068. FREEDBHANDLE(pDbWkSpace);
  3069. }
  3070. }
  3071. #if DBG
  3072. lpContext->m_LastCall = RPC_CALL_KEYPACKSETSTATUS;
  3073. #endif
  3074. lpContext->m_LastError=status;
  3075. InterlockedDecrement( &lpContext->m_RefCount );
  3076. *dwErrCode = TLSMapReturnCode(status);
  3077. return RPC_S_OK;
  3078. }
  3079. //-------------------------------------------------------------------------------
  3080. error_status_t
  3081. TLSRpcLicenseEnumBegin(
  3082. /* [in] */ PCONTEXT_HANDLE phContext,
  3083. /* [in] */ DWORD dwSearchParm,
  3084. /* [in] */ BOOL bMatchAll,
  3085. /* [ref][in] */ LPLSLicenseSearchParm lpSearchParm,
  3086. /* [ref][out][in] */ PDWORD dwErrCode
  3087. )
  3088. /*++
  3089. Description:
  3090. Begin enumeration of license issued based on search criterial
  3091. Arguments:
  3092. phContext - client context handle
  3093. dwSearchParm - license search criterial.
  3094. bMatchAll - match all search criterial
  3095. lpSearchParm - license(s) to be enumerated.
  3096. Return Value:
  3097. Same as LSKeyPackEnumBegin().
  3098. ++*/
  3099. {
  3100. PTLSDbWorkSpace pDbWkSpace = NULL;
  3101. DWORD status=ERROR_SUCCESS;
  3102. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  3103. _se_translator_function old_trans_se_func = NULL;
  3104. old_trans_se_func = _set_se_translator( trans_se_func );
  3105. DBGPrintf(
  3106. DBG_INFORMATION,
  3107. DBG_FACILITY_RPC,
  3108. DBGLEVEL_FUNCTION_TRACE,
  3109. _TEXT("%s : TLSRpcLicenseEnumBegin\n"),
  3110. lpContext->m_Client
  3111. );
  3112. InterlockedIncrement( &lpContext->m_RefCount );
  3113. //
  3114. // This will use cached db connection, in-consistency may occurred,
  3115. // visibility of changes in one connection may not appear right away
  3116. // on another connection handle, this is expected behavoir for Jet and
  3117. // so are we, user can always re-fresh.
  3118. //
  3119. do {
  3120. if(lpContext->m_ContextType != CONTEXTHANDLE_EMPTY_TYPE)
  3121. {
  3122. SetLastError(status=TLS_E_INVALID_SEQUENCE);
  3123. break;
  3124. }
  3125. pDbWkSpace = AllocateWorkSpace(g_EnumDbTimeout);
  3126. // allocate ODBC connections
  3127. if(pDbWkSpace == NULL)
  3128. {
  3129. status=TLS_E_ALLOCATE_HANDLE;
  3130. break;
  3131. }
  3132. try {
  3133. LICENSEDCLIENT license;
  3134. ConvertLSLicenseToLicense(lpSearchParm, &license);
  3135. status = TLSDBLicenseEnumBegin(
  3136. pDbWkSpace,
  3137. bMatchAll,
  3138. dwSearchParm & LICENSE_TABLE_EXTERN_SEARCH_MASK,
  3139. &license
  3140. );
  3141. }
  3142. catch( SE_Exception e ) {
  3143. status = e.getSeNumber();
  3144. }
  3145. catch(...) {
  3146. status = TLS_E_INTERNAL;
  3147. }
  3148. if(status == ERROR_SUCCESS)
  3149. {
  3150. lpContext->m_ContextType = CONTEXTHANDLE_LICENSE_ENUM_TYPE;
  3151. lpContext->m_ContextHandle = (PVOID)pDbWkSpace;
  3152. }
  3153. } while(FALSE);
  3154. if(status != ERROR_SUCCESS)
  3155. {
  3156. if(pDbWkSpace)
  3157. {
  3158. ReleaseWorkSpace(&pDbWkSpace);
  3159. }
  3160. }
  3161. #if DBG
  3162. lpContext->m_LastCall = RPC_CALL_LICENSEENUMBEGIN;
  3163. #endif
  3164. InterlockedDecrement( &lpContext->m_RefCount );
  3165. lpContext->m_LastError=status;
  3166. *dwErrCode = TLSMapReturnCode(status);
  3167. //
  3168. // Reset SE translator
  3169. //
  3170. _set_se_translator(old_trans_se_func);
  3171. return RPC_S_OK;
  3172. }
  3173. //-------------------------------------------------------------------------------
  3174. error_status_t
  3175. TLSRpcLicenseEnumNext(
  3176. /* [in] */ PCONTEXT_HANDLE phContext,
  3177. /* [ref][out] */ LPLSLicense lpLicense,
  3178. /* [ref][out][in] */ PDWORD dwErrCode
  3179. )
  3180. /*++
  3181. Abstract:
  3182. Fetch next record match enumeration criterial.
  3183. Parameters:
  3184. phContext : Client context handle.
  3185. lpLicense : return next record that match enumeration criterial.
  3186. dwErrCode : error code.
  3187. Returns:
  3188. Function returns RPC status, dwErrCode return error code.
  3189. Note:
  3190. Must have call TLSRpcLicenseEnumBegin().
  3191. ++*/
  3192. {
  3193. DWORD status=ERROR_SUCCESS;
  3194. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  3195. InterlockedIncrement( &lpContext->m_RefCount );
  3196. _se_translator_function old_trans_se_func = NULL;
  3197. old_trans_se_func = _set_se_translator( trans_se_func );
  3198. DBGPrintf(
  3199. DBG_INFORMATION,
  3200. DBG_FACILITY_RPC,
  3201. DBGLEVEL_FUNCTION_TRACE,
  3202. _TEXT("%s : TLSRpcLicenseEnumNext\n"),
  3203. lpContext->m_Client
  3204. );
  3205. if(lpContext->m_ContextType != CONTEXTHANDLE_LICENSE_ENUM_TYPE)
  3206. {
  3207. SetLastError(status=TLS_E_INVALID_SEQUENCE);
  3208. }
  3209. else
  3210. {
  3211. PTLSDbWorkSpace pDbWkSpace=(PTLSDbWorkSpace)lpContext->m_ContextHandle;
  3212. try {
  3213. LICENSEDCLIENT license;
  3214. memset(lpLicense, 0, sizeof(LSLicense));
  3215. status=TLSDBLicenseEnumNext(
  3216. pDbWkSpace,
  3217. &license
  3218. );
  3219. if(status == ERROR_SUCCESS)
  3220. {
  3221. ConvertLicenseToLSLicense(&license, lpLicense);
  3222. }
  3223. }
  3224. catch( SE_Exception e ) {
  3225. status = e.getSeNumber();
  3226. }
  3227. catch(...) {
  3228. status = TLS_E_INTERNAL;
  3229. }
  3230. }
  3231. #if DBG
  3232. lpContext->m_LastCall = RPC_CALL_LICENSEENUMNEXT;
  3233. #endif
  3234. lpContext->m_LastError=status;
  3235. InterlockedDecrement( &lpContext->m_RefCount );
  3236. *dwErrCode = TLSMapReturnCode(status);
  3237. //
  3238. // Reset SE translator
  3239. //
  3240. _set_se_translator(old_trans_se_func);
  3241. return RPC_S_OK;
  3242. }
  3243. //-------------------------------------------------------------------------------
  3244. error_status_t
  3245. TLSRpcLicenseEnumNextEx(
  3246. /* [in] */ PCONTEXT_HANDLE phContext,
  3247. /* [ref][out] */ LPLSLicenseEx lpLicense,
  3248. /* [ref][out][in] */ PDWORD dwErrCode
  3249. )
  3250. /*++
  3251. Abstract:
  3252. Fetch next record match enumeration criterial.
  3253. Parameters:
  3254. phContext : Client context handle.
  3255. lpLicense : return next record that match enumeration criterial.
  3256. dwErrCode : error code.
  3257. Returns:
  3258. Function returns RPC status, dwErrCode return error code.
  3259. Note:
  3260. Must have call TLSRpcLicenseEnumBegin().
  3261. ++*/
  3262. {
  3263. DWORD status=ERROR_SUCCESS;
  3264. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  3265. InterlockedIncrement( &lpContext->m_RefCount );
  3266. _se_translator_function old_trans_se_func = NULL;
  3267. old_trans_se_func = _set_se_translator( trans_se_func );
  3268. DBGPrintf(
  3269. DBG_INFORMATION,
  3270. DBG_FACILITY_RPC,
  3271. DBGLEVEL_FUNCTION_TRACE,
  3272. _TEXT("%s : TLSRpcLicenseEnumNextEx\n"),
  3273. lpContext->m_Client
  3274. );
  3275. if(lpContext->m_ContextType != CONTEXTHANDLE_LICENSE_ENUM_TYPE)
  3276. {
  3277. SetLastError(status=TLS_E_INVALID_SEQUENCE);
  3278. }
  3279. else
  3280. {
  3281. PTLSDbWorkSpace pDbWkSpace=(PTLSDbWorkSpace)lpContext->m_ContextHandle;
  3282. try {
  3283. LICENSEDCLIENT license;
  3284. memset(lpLicense, 0, sizeof(LSLicenseEx));
  3285. status=TLSDBLicenseEnumNext(
  3286. pDbWkSpace,
  3287. &license
  3288. );
  3289. if(status == ERROR_SUCCESS)
  3290. {
  3291. ConvertLicenseToLSLicenseEx(&license, lpLicense);
  3292. }
  3293. }
  3294. catch( SE_Exception e ) {
  3295. status = e.getSeNumber();
  3296. }
  3297. catch(...) {
  3298. status = TLS_E_INTERNAL;
  3299. }
  3300. }
  3301. #if DBG
  3302. lpContext->m_LastCall = RPC_CALL_LICENSEENUMNEXT;
  3303. #endif
  3304. lpContext->m_LastError=status;
  3305. InterlockedDecrement( &lpContext->m_RefCount );
  3306. *dwErrCode = TLSMapReturnCode(status);
  3307. //
  3308. // Reset SE translator
  3309. //
  3310. _set_se_translator(old_trans_se_func);
  3311. return RPC_S_OK;
  3312. }
  3313. //-------------------------------------------------------------------------------
  3314. error_status_t
  3315. TLSRpcLicenseEnumEnd(
  3316. /* [in] */ PCONTEXT_HANDLE phContext,
  3317. /* [ref][out][in] */ PDWORD dwErrCode
  3318. )
  3319. /*++
  3320. Abstract:
  3321. Terminate a enumeration.
  3322. Parameters:
  3323. phContext :
  3324. dwErrCode :
  3325. Returns:
  3326. Note
  3327. ++*/
  3328. {
  3329. DWORD status=ERROR_SUCCESS;
  3330. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  3331. InterlockedIncrement( &lpContext->m_RefCount );
  3332. DBGPrintf(
  3333. DBG_INFORMATION,
  3334. DBG_FACILITY_RPC,
  3335. DBGLEVEL_FUNCTION_TRACE,
  3336. _TEXT("%s : TLSRpcLicenseEnumEnd\n"),
  3337. lpContext->m_Client
  3338. );
  3339. try {
  3340. if(lpContext->m_ContextType != CONTEXTHANDLE_LICENSE_ENUM_TYPE)
  3341. {
  3342. SetLastError(status=ERROR_INVALID_HANDLE);
  3343. }
  3344. else
  3345. {
  3346. PTLSDbWorkSpace pDbWkSpace = (PTLSDbWorkSpace)lpContext->m_ContextHandle;
  3347. TLSDBLicenseEnumEnd(pDbWkSpace);
  3348. ReleaseWorkSpace(&pDbWkSpace);
  3349. lpContext->m_ContextType = CONTEXTHANDLE_EMPTY_TYPE;
  3350. }
  3351. }
  3352. catch(...) {
  3353. status = TLS_E_INTERNAL;
  3354. }
  3355. #if DBG
  3356. lpContext->m_LastCall = RPC_CALL_LICENSEENUMEND;
  3357. #endif
  3358. lpContext->m_LastError=status;
  3359. InterlockedDecrement( &lpContext->m_RefCount );
  3360. *dwErrCode = TLSMapReturnCode(status);
  3361. return RPC_S_OK;
  3362. }
  3363. //-------------------------------------------------------------------------------
  3364. error_status_t
  3365. TLSRpcLicenseSetStatus(
  3366. /* [in] */ PCONTEXT_HANDLE phContext,
  3367. /* [in] */ DWORD dwSetParam,
  3368. /* [in] */ LPLSLicense lpLicense,
  3369. /* [ref][out][in] */ PDWORD dwErrCode
  3370. )
  3371. /*++
  3372. ++*/
  3373. {
  3374. PTLSDbWorkSpace pDbWkSpace=NULL;
  3375. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  3376. DWORD status=ERROR_SUCCESS;
  3377. InterlockedIncrement( &lpContext->m_RefCount );
  3378. DBGPrintf(
  3379. DBG_INFORMATION,
  3380. DBG_FACILITY_RPC,
  3381. DBGLEVEL_FUNCTION_TRACE,
  3382. _TEXT("%s : TLSRpcLicenseSetStatus\n"),
  3383. lpContext->m_Client
  3384. );
  3385. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_ADMIN))
  3386. {
  3387. status = TLS_E_ACCESS_DENIED;
  3388. }
  3389. else if(dwSetParam != LSLICENSE_EXSEARCH_LICENSESTATUS)
  3390. {
  3391. status = TLS_E_INVALID_DATA;
  3392. }
  3393. else
  3394. {
  3395. if(!ALLOCATEDBHANDLE(pDbWkSpace, g_GeneralDbTimeout))
  3396. {
  3397. status=TLS_E_ALLOCATE_HANDLE;
  3398. }
  3399. else
  3400. {
  3401. CLEANUPSTMT;
  3402. BEGIN_TRANSACTION(pDbWkSpace);
  3403. try {
  3404. LICENSEDCLIENT license;
  3405. ConvertLSLicenseToLicense(lpLicense, &license);
  3406. status=TLSDBLicenseSetValue(
  3407. USEHANDLE(pDbWkSpace),
  3408. dwSetParam,
  3409. &license,
  3410. FALSE
  3411. );
  3412. }
  3413. catch(...) {
  3414. status = TLS_E_INTERNAL;
  3415. }
  3416. if(TLS_ERROR(status))
  3417. {
  3418. ROLLBACK_TRANSACTION(pDbWkSpace);
  3419. }
  3420. else
  3421. {
  3422. COMMIT_TRANSACTION(pDbWkSpace);
  3423. }
  3424. FREEDBHANDLE(pDbWkSpace);
  3425. }
  3426. }
  3427. #if DBG
  3428. lpContext->m_LastCall = RPC_CALL_LICENSESETSTATUS;
  3429. #endif
  3430. lpContext->m_LastError = status;
  3431. InterlockedDecrement( &lpContext->m_RefCount );
  3432. *dwErrCode = TLSMapReturnCode(status);
  3433. return RPC_S_OK;
  3434. }
  3435. //-------------------------------------------------------------------------------
  3436. error_status_t
  3437. TLSRpcGetAvailableLicenses(
  3438. /* [in] */ PCONTEXT_HANDLE phContext,
  3439. /* [in] */ DWORD dwSearchParm,
  3440. /* [ref][in] */ LPLSKeyPack lplsKeyPack,
  3441. /* [ref][out] */ LPDWORD lpdwAvail,
  3442. /* [ref][out][in] */ PDWORD dwErrCode
  3443. )
  3444. /*++
  3445. ++*/
  3446. {
  3447. PTLSDbWorkSpace pDbWkSpace=NULL;
  3448. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  3449. DWORD status=ERROR_SUCCESS;
  3450. DBGPrintf(
  3451. DBG_INFORMATION,
  3452. DBG_FACILITY_RPC,
  3453. DBGLEVEL_FUNCTION_TRACE,
  3454. _TEXT("%s : TLSRpcGetAvailableLicenses\n"),
  3455. lpContext->m_Client
  3456. );
  3457. InterlockedIncrement( &lpContext->m_RefCount );
  3458. //
  3459. // Don't use global cached DB connection handle, it is possible
  3460. // to get in-consistent value using other DB handle, however, it is
  3461. // also possible that during the time that this function return and
  3462. // the time that client actually make the call to allocate license,
  3463. // all available licenses were allocated by other client.
  3464. //
  3465. pDbWkSpace = AllocateWorkSpace(g_GeneralDbTimeout);
  3466. if(pDbWkSpace == NULL)
  3467. {
  3468. status=TLS_E_ALLOCATE_HANDLE;
  3469. }
  3470. else
  3471. {
  3472. try {
  3473. LICENSEPACK keypack;
  3474. memset(&keypack, 0, sizeof(keypack));
  3475. ConvertLsKeyPackToKeyPack(
  3476. lplsKeyPack,
  3477. &keypack,
  3478. NULL
  3479. );
  3480. status = TLSDBKeyPackGetAvailableLicenses(
  3481. pDbWkSpace,
  3482. dwSearchParm,
  3483. &keypack,
  3484. lpdwAvail
  3485. );
  3486. //FreeTlsLicensePack(&keypack);
  3487. }
  3488. catch(...) {
  3489. status = TLS_E_INTERNAL;
  3490. }
  3491. ReleaseWorkSpace(&pDbWkSpace);
  3492. }
  3493. lpContext->m_LastError=status;
  3494. InterlockedDecrement( &lpContext->m_RefCount );
  3495. *dwErrCode = TLSMapReturnCode(status);
  3496. return RPC_S_OK;
  3497. }
  3498. //-------------------------------------------------------------------------------
  3499. error_status_t
  3500. TLSRpcGetRevokeKeyPackList(
  3501. /* [in] */ PCONTEXT_HANDLE phContext,
  3502. /* [out][in] */ PDWORD pcbNumberOfRange,
  3503. /* [size_is][out] */ LPLSRange __RPC_FAR *ppRevokeRange,
  3504. /* [ref][out][in] */ PDWORD pdwErrCode
  3505. )
  3506. /*++
  3507. ++*/
  3508. {
  3509. *pdwErrCode = TLSMapReturnCode(TLS_E_NOTSUPPORTED);
  3510. return RPC_S_OK;
  3511. }
  3512. //-------------------------------------------------------------------------------
  3513. error_status_t
  3514. TLSRpcGetRevokeLicenseList(
  3515. /* [in] */ PCONTEXT_HANDLE phContext,
  3516. /* [out][in] */ PDWORD pcbNumberOfRange,
  3517. /* [size_is][out] */ LPLSRange __RPC_FAR *ppRevokeRange,
  3518. /* [ref][out][in] */ PDWORD pdwErrCode
  3519. )
  3520. /*++
  3521. ++*/
  3522. {
  3523. *pdwErrCode = TLSMapReturnCode(TLS_E_NOTSUPPORTED);
  3524. return RPC_S_OK;
  3525. }
  3526. //-------------------------------------------------------------------------------
  3527. error_status_t
  3528. TLSRpcReturnKeyPack(
  3529. /* [in] */ PCONTEXT_HANDLE phContext,
  3530. /* [in] */ DWORD dwKeyPackId,
  3531. /* [in] */ DWORD dwReturnReason,
  3532. /* [ref][out][in] */ PDWORD pdwErrCode
  3533. )
  3534. /*++
  3535. ++*/
  3536. {
  3537. *pdwErrCode = TLSMapReturnCode(TLS_E_NOTSUPPORTED);
  3538. return RPC_S_OK;
  3539. }
  3540. //-------------------------------------------------------------------------------
  3541. error_status_t
  3542. TLSRpcReturnLicense(
  3543. /* [in] */ PCONTEXT_HANDLE phContext,
  3544. /* [in] */ DWORD dwKeyPackId,
  3545. /* [in] */ DWORD dwLicenseId,
  3546. /* [in] */ DWORD dwReturnReason,
  3547. /* [ref][out][in] */ PDWORD pdwErrCode
  3548. )
  3549. /*++
  3550. ++*/
  3551. {
  3552. *pdwErrCode = TLSMapReturnCode(TLS_E_NOTSUPPORTED);
  3553. return RPC_S_OK;
  3554. }
  3555. //-------------------------------------------------------------------------------
  3556. error_status_t
  3557. TLSRpcInstallCertificate(
  3558. /* [in] */ PCONTEXT_HANDLE phContext,
  3559. /* [in] */ DWORD dwCertType,
  3560. /* [in] */ DWORD dwCertLevel,
  3561. /* [in] */ DWORD cbSignCert,
  3562. /* [size_is][in] */ PBYTE pbSignCert,
  3563. /* [in] */ DWORD cbExchCert,
  3564. /* [size_is][in] */ PBYTE pbExchCert,
  3565. /* [ref][out][in] */ PDWORD dwErrCode
  3566. )
  3567. /*++
  3568. ++*/
  3569. {
  3570. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  3571. DWORD status=ERROR_SUCCESS;
  3572. _se_translator_function old_trans_se_func = NULL;
  3573. old_trans_se_func = _set_se_translator( trans_se_func );
  3574. DBGPrintf(
  3575. DBG_INFORMATION,
  3576. DBG_FACILITY_RPC,
  3577. DBGLEVEL_FUNCTION_TRACE,
  3578. _TEXT("%s : TLSRpcInstallCertificate\n"),
  3579. lpContext->m_Client
  3580. );
  3581. InterlockedIncrement( &lpContext->m_RefCount );
  3582. DWORD cbLsSignCert=0;
  3583. PBYTE pbLsSignCert=NULL;
  3584. DWORD cbLsExchCert=0;
  3585. PBYTE pbLsExchCert=NULL;
  3586. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_ADMIN))
  3587. {
  3588. status = TLS_E_ACCESS_DENIED;
  3589. goto cleanup;
  3590. }
  3591. if(pbSignCert == NULL || pbExchCert == NULL)
  3592. {
  3593. status = TLS_E_INVALID_DATA;
  3594. goto cleanup;
  3595. }
  3596. #if DBG
  3597. //
  3598. // Verify input data
  3599. //
  3600. status = TLSVerifyCertChainInMomory(
  3601. g_hCryptProv,
  3602. pbSignCert,
  3603. cbSignCert
  3604. );
  3605. if(status != ERROR_SUCCESS)
  3606. {
  3607. status = TLS_E_INVALID_DATA;
  3608. }
  3609. //
  3610. // Verify input data
  3611. //
  3612. status = TLSVerifyCertChainInMomory(
  3613. g_hCryptProv,
  3614. pbExchCert,
  3615. cbExchCert
  3616. );
  3617. if(status != ERROR_SUCCESS)
  3618. {
  3619. status = TLS_E_INVALID_DATA;
  3620. }
  3621. #endif
  3622. //
  3623. // Block RPC call to serialize install certificate
  3624. //
  3625. if(AcquireRPCExclusiveLock(INFINITE) == FALSE)
  3626. {
  3627. status=TLS_E_ALLOCATE_HANDLE;
  3628. goto cleanup;
  3629. }
  3630. if(AcquireAdministrativeLock(INFINITE) == TRUE)
  3631. {
  3632. try {
  3633. if(dwCertLevel == 0)
  3634. {
  3635. status = TLSSaveRootCertificatesToStore(
  3636. g_hCryptProv,
  3637. cbSignCert,
  3638. pbSignCert,
  3639. cbExchCert,
  3640. pbExchCert
  3641. );
  3642. }
  3643. else
  3644. {
  3645. status = TLSSaveCertificatesToStore(
  3646. g_hCryptProv,
  3647. dwCertType,
  3648. dwCertLevel,
  3649. cbSignCert,
  3650. pbSignCert,
  3651. cbExchCert,
  3652. pbExchCert
  3653. );
  3654. if(status == ERROR_SUCCESS && dwCertType == CERTIFICATE_CA_TYPE)
  3655. {
  3656. if(cbSignCert)
  3657. {
  3658. status = IsCertificateLicenseServerCertificate(
  3659. g_hCryptProv,
  3660. AT_SIGNATURE,
  3661. cbSignCert,
  3662. pbSignCert,
  3663. &cbLsSignCert,
  3664. &pbLsSignCert
  3665. );
  3666. }
  3667. if(status == ERROR_SUCCESS && cbExchCert)
  3668. {
  3669. status = IsCertificateLicenseServerCertificate(
  3670. g_hCryptProv,
  3671. AT_KEYEXCHANGE,
  3672. cbExchCert,
  3673. pbExchCert,
  3674. &cbLsExchCert,
  3675. &pbLsExchCert
  3676. );
  3677. }
  3678. //
  3679. // Install what we have here.
  3680. //
  3681. if(status == ERROR_SUCCESS && (cbLsExchCert || pbLsExchCert))
  3682. {
  3683. status = TLSInstallLsCertificate(
  3684. cbLsSignCert,
  3685. pbLsSignCert,
  3686. cbLsExchCert,
  3687. pbLsExchCert
  3688. );
  3689. }
  3690. #ifdef ENFORCE_LICENSING
  3691. // enforce version, check what's installed and restore backup if necessary
  3692. // non-enforce, just install, we won't use it anyway.
  3693. if(status == ERROR_SUCCESS && (cbLsExchCert || pbLsExchCert))
  3694. {
  3695. // reload certificate
  3696. if(TLSLoadVerifyLicenseServerCertificates() != ERROR_SUCCESS)
  3697. {
  3698. status = TLS_E_INVALID_DATA;
  3699. // delete the primary certificate registry key
  3700. TLSRegDeleteKey(
  3701. HKEY_LOCAL_MACHINE,
  3702. LSERVER_SERVER_CERTIFICATE_REGKEY
  3703. );
  3704. //
  3705. // reload certificate, if anything goes wrong, we will goes
  3706. // back to unregister mode.
  3707. //
  3708. if(TLSLoadServerCertificate() == FALSE)
  3709. {
  3710. // critical error occurred
  3711. TLSLogErrorEvent(TLS_E_LOAD_CERTIFICATE);
  3712. // initiate self-shutdown
  3713. GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
  3714. }
  3715. }
  3716. else
  3717. {
  3718. DWORD dwStatus;
  3719. // make sure our backup is up to date.
  3720. dwStatus = TLSRestoreLicenseServerCertificate(
  3721. LSERVER_SERVER_CERTIFICATE_REGKEY,
  3722. LSERVER_SERVER_CERTIFICATE_REGKEY_BACKUP1
  3723. );
  3724. if(dwStatus != ERROR_SUCCESS)
  3725. {
  3726. TLSLogWarningEvent(TLS_W_BACKUPCERTIFICATE);
  3727. TLSRegDeleteKey(
  3728. HKEY_LOCAL_MACHINE,
  3729. LSERVER_SERVER_CERTIFICATE_REGKEY_BACKUP1
  3730. );
  3731. }
  3732. dwStatus = TLSRestoreLicenseServerCertificate(
  3733. LSERVER_SERVER_CERTIFICATE_REGKEY,
  3734. LSERVER_SERVER_CERTIFICATE_REGKEY_BACKUP2
  3735. );
  3736. if(dwStatus != ERROR_SUCCESS)
  3737. {
  3738. TLSLogWarningEvent(TLS_W_BACKUPCERTIFICATE);
  3739. TLSRegDeleteKey(
  3740. HKEY_LOCAL_MACHINE,
  3741. LSERVER_SERVER_CERTIFICATE_REGKEY_BACKUP2
  3742. );
  3743. }
  3744. }
  3745. }
  3746. #endif
  3747. if(pbLsSignCert)
  3748. {
  3749. FreeMemory(pbLsSignCert);
  3750. }
  3751. if(pbLsExchCert)
  3752. {
  3753. FreeMemory(pbLsExchCert);
  3754. }
  3755. }
  3756. }
  3757. }
  3758. catch( SE_Exception e ) {
  3759. status = e.getSeNumber();
  3760. }
  3761. catch(...) {
  3762. status = TLS_E_INTERNAL;
  3763. }
  3764. ReleaseAdministrativeLock();
  3765. }
  3766. else
  3767. {
  3768. status=TLS_E_ALLOCATE_HANDLE;
  3769. }
  3770. ReleaseRPCExclusiveLock();
  3771. cleanup:
  3772. lpContext->m_LastError=status;
  3773. #if DBG
  3774. lpContext->m_LastCall = RPC_CALL_INSTALL_SERV_CERT;
  3775. #endif
  3776. InterlockedDecrement( &lpContext->m_RefCount );
  3777. *dwErrCode = TLSMapReturnCode(status);
  3778. //
  3779. // Reset SE translator
  3780. //
  3781. _set_se_translator(old_trans_se_func);
  3782. return RPC_S_OK;
  3783. }
  3784. //-------------------------------------------------------------------------------
  3785. error_status_t
  3786. TLSRpcGetServerCertificate(
  3787. /* [in] */ PCONTEXT_HANDLE phContext,
  3788. /* [in] */ BOOL bSignCert,
  3789. /* [size_is][size_is][out] */ LPBYTE __RPC_FAR *ppCertBlob,
  3790. /* [ref][out] */ LPDWORD lpdwCertBlobLen,
  3791. /* [ref][out][in] */ PDWORD dwErrCode
  3792. )
  3793. /*++
  3794. ++*/
  3795. {
  3796. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  3797. DWORD status=ERROR_SUCCESS;
  3798. _se_translator_function old_trans_se_func = NULL;
  3799. old_trans_se_func = _set_se_translator( trans_se_func );
  3800. InterlockedIncrement( &lpContext->m_RefCount );
  3801. DBGPrintf(
  3802. DBG_INFORMATION,
  3803. DBG_FACILITY_RPC,
  3804. DBGLEVEL_FUNCTION_TRACE,
  3805. _TEXT("%s : TLSRpcGetServerCertificate\n"),
  3806. lpContext->m_Client
  3807. );
  3808. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_ADMIN))
  3809. {
  3810. status = TLS_E_ACCESS_DENIED;
  3811. }
  3812. else if(!g_pbExchangeEncodedCert || !g_cbExchangeEncodedCert ||
  3813. !g_pbSignatureEncodedCert || !g_cbSignatureEncodedCert)
  3814. {
  3815. status = TLS_E_NO_CERTIFICATE;
  3816. }
  3817. else
  3818. {
  3819. if(AcquireAdministrativeLock(INFINITE) == TRUE)
  3820. {
  3821. try{
  3822. status = TLSSaveCertAsPKCS7(
  3823. (bSignCert) ? g_pbSignatureEncodedCert : g_pbExchangeEncodedCert,
  3824. (bSignCert) ? g_cbSignatureEncodedCert : g_cbExchangeEncodedCert,
  3825. ppCertBlob,
  3826. lpdwCertBlobLen
  3827. );
  3828. // hack so that we can continue testing...
  3829. if(g_bHasHydraCert == FALSE)
  3830. {
  3831. if(g_pbServerSPK != NULL && g_cbServerSPK != 0)
  3832. {
  3833. status = TLS_W_SELFSIGN_CERTIFICATE;
  3834. }
  3835. else
  3836. {
  3837. status = TLS_W_TEMP_SELFSIGN_CERT;
  3838. }
  3839. }
  3840. }
  3841. catch( SE_Exception e ) {
  3842. status = e.getSeNumber();
  3843. }
  3844. catch(...)
  3845. {
  3846. status = TLS_E_INTERNAL;
  3847. }
  3848. ReleaseAdministrativeLock();
  3849. }
  3850. else
  3851. {
  3852. status = TLS_E_ALLOCATE_HANDLE;
  3853. }
  3854. }
  3855. lpContext->m_LastError=status;
  3856. #if DBG
  3857. lpContext->m_LastCall = RPC_CALL_GETSERV_CERT;
  3858. #endif
  3859. InterlockedDecrement( &lpContext->m_RefCount );
  3860. *dwErrCode = TLSMapReturnCode(status);
  3861. //
  3862. // Reset SE translator
  3863. //
  3864. _set_se_translator(old_trans_se_func);
  3865. return RPC_S_OK;
  3866. }
  3867. //-------------------------------------------------------------------------------
  3868. void
  3869. MyFreeLicenseKeyPack(
  3870. PLicense_KeyPack pLicenseKeyPack
  3871. )
  3872. /*
  3873. */
  3874. {
  3875. DWORD i;
  3876. PKeyPack_Description pKpDesc;
  3877. if( pLicenseKeyPack->pDescription )
  3878. {
  3879. for( i = 0, pKpDesc = pLicenseKeyPack->pDescription;
  3880. i < pLicenseKeyPack->dwDescriptionCount;
  3881. i++, pKpDesc++ )
  3882. {
  3883. if(pKpDesc->pDescription)
  3884. LocalFree( pKpDesc->pDescription );
  3885. if(pKpDesc->pbProductName)
  3886. LocalFree( pKpDesc->pbProductName );
  3887. }
  3888. }
  3889. if(pLicenseKeyPack->pDescription)
  3890. LocalFree( pLicenseKeyPack->pDescription );
  3891. if(pLicenseKeyPack->pbManufacturer && pLicenseKeyPack->cbManufacturer != 0)
  3892. LocalFree( pLicenseKeyPack->pbManufacturer );
  3893. if(pLicenseKeyPack->pbManufacturerData && pLicenseKeyPack->cbManufacturerData != 0)
  3894. LocalFree( pLicenseKeyPack->pbManufacturerData );
  3895. if(pLicenseKeyPack->pbProductId && pLicenseKeyPack->cbProductId != 0)
  3896. LocalFree( pLicenseKeyPack->pbProductId );
  3897. return;
  3898. }
  3899. //---------------------------------------------------------------------
  3900. error_status_t
  3901. TLSRpcRegisterLicenseKeyPack(
  3902. /* [in] */ PCONTEXT_HANDLE phContext,
  3903. /* [size_is][in] */ LPBYTE pbCHCertBlob,
  3904. /* [in] */ DWORD cbCHCertBlobSize,
  3905. /* [size_is][in] */ LPBYTE pbRootCertBlob,
  3906. /* [in] */ DWORD cbRootCertBlob,
  3907. /* [size_is][in] */ LPBYTE lpKeyPackBlob,
  3908. /* [in] */ DWORD dwKeyPackBlobLen,
  3909. /* [ref][out][in] */ PDWORD dwErrCode
  3910. )
  3911. /*++
  3912. ++*/
  3913. {
  3914. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  3915. DWORD status=ERROR_SUCCESS;
  3916. LSKeyPack keypack;
  3917. _se_translator_function old_trans_se_func = NULL;
  3918. old_trans_se_func = _set_se_translator( trans_se_func );
  3919. DBGPrintf(
  3920. DBG_INFORMATION,
  3921. DBG_FACILITY_RPC,
  3922. DBGLEVEL_FUNCTION_TRACE,
  3923. _TEXT("%s : TLSRpcRegisterLicenseKeyPack\n"),
  3924. lpContext->m_Client
  3925. );
  3926. PTLSDbWorkSpace pDbWkSpace;
  3927. InterlockedIncrement( &lpContext->m_RefCount );
  3928. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_ADMIN))
  3929. {
  3930. status = TLS_E_ACCESS_DENIED;
  3931. goto cleanup;
  3932. }
  3933. if(!ALLOCATEDBHANDLE(pDbWkSpace, g_GeneralDbTimeout))
  3934. {
  3935. status=TLS_E_ALLOCATE_HANDLE;
  3936. }
  3937. else
  3938. {
  3939. License_KeyPack pLicenseKeyPack;
  3940. LicensePackDecodeParm LkpDecodeParm;
  3941. memset(
  3942. &LkpDecodeParm,
  3943. 0,
  3944. sizeof(LicensePackDecodeParm)
  3945. );
  3946. LkpDecodeParm.hCryptProv = g_hCryptProv;
  3947. LkpDecodeParm.pbDecryptParm = (PBYTE)g_pszServerPid;
  3948. LkpDecodeParm.cbDecryptParm = (lstrlen(g_pszServerPid) * sizeof(TCHAR));
  3949. LkpDecodeParm.cbClearingHouseCert = cbCHCertBlobSize;
  3950. LkpDecodeParm.pbClearingHouseCert = pbCHCertBlob;
  3951. LkpDecodeParm.pbRootCertificate = pbRootCertBlob;
  3952. LkpDecodeParm.cbRootCertificate = cbRootCertBlob;
  3953. //
  3954. // make code clean, always start a transaction
  3955. //
  3956. CLEANUPSTMT;
  3957. BEGIN_TRANSACTION(pDbWkSpace);
  3958. try {
  3959. status = DecodeLicenseKeyPackEx(
  3960. &pLicenseKeyPack,
  3961. &LkpDecodeParm,
  3962. dwKeyPackBlobLen,
  3963. lpKeyPackBlob
  3964. );
  3965. if(status != LICENSE_STATUS_OK)
  3966. {
  3967. status = TLS_E_DECODE_KEYPACKBLOB;
  3968. DBGPrintf(
  3969. DBG_INFORMATION,
  3970. DBG_FACILITY_RPC,
  3971. DBGLEVEL_FUNCTION_DETAILSIMPLE,
  3972. _TEXT("Can't decode key pack blob - %d...\n"),
  3973. status);
  3974. }
  3975. else
  3976. {
  3977. status=TLSDBRegisterLicenseKeyPack(
  3978. USEHANDLE(pDbWkSpace),
  3979. &pLicenseKeyPack,
  3980. &keypack
  3981. );
  3982. MyFreeLicenseKeyPack(&pLicenseKeyPack);
  3983. }
  3984. }
  3985. catch( SE_Exception e ) {
  3986. status = e.getSeNumber();
  3987. }
  3988. catch(...) {
  3989. status = TLS_E_INTERNAL;
  3990. }
  3991. if(TLS_ERROR(status))
  3992. {
  3993. ROLLBACK_TRANSACTION(pDbWkSpace);
  3994. }
  3995. else
  3996. {
  3997. COMMIT_TRANSACTION(pDbWkSpace);
  3998. }
  3999. FREEDBHANDLE(pDbWkSpace);
  4000. }
  4001. //
  4002. // Post a sync work object
  4003. //
  4004. if(status == ERROR_SUCCESS)
  4005. {
  4006. if(TLSAnnounceLKPToAllRemoteServer(
  4007. keypack.dwKeyPackId,
  4008. 0
  4009. ) != ERROR_SUCCESS)
  4010. {
  4011. TLSLogWarningEvent(TLS_W_ANNOUNCELKP_FAILED);
  4012. }
  4013. }
  4014. cleanup:
  4015. lpContext->m_LastError=status;
  4016. #if DBG
  4017. lpContext->m_LastCall = RPC_CALL_REGISTER_LICENSE_PACK;
  4018. #endif
  4019. InterlockedDecrement( &lpContext->m_RefCount );
  4020. *dwErrCode = TLSMapReturnCode(status);
  4021. //
  4022. // Reset SE translator
  4023. //
  4024. _set_se_translator(old_trans_se_func);
  4025. return RPC_S_OK;
  4026. }
  4027. ////////////////////////////////////////////////////////////////////////////
  4028. error_status_t
  4029. TLSRpcRequestTermServCert(
  4030. /* [in] */ PCONTEXT_HANDLE phContext,
  4031. /* [in] */ LPTLSHYDRACERTREQUEST pRequest,
  4032. /* [ref][out][in] */ PDWORD pcbChallengeData,
  4033. /* [size_is][out] */ PBYTE* ppbChallengeData,
  4034. /* [ref][out][in] */ PDWORD pdwErrCode
  4035. )
  4036. /*++
  4037. Abstract:
  4038. Private routine to issue certificate to Terminal Server.
  4039. Parameter:
  4040. phContext : Client context handle.
  4041. pRequest : Terminal Server specific certificate request.
  4042. pcbChallengeData : size of Server randomly generated challenge data
  4043. to Terminal Server.
  4044. ppbChallengeData : Server randomly generated challenge data to Terminal
  4045. server.
  4046. pdwErrCode : Error code.
  4047. Returns:
  4048. Function always return RPC_S_OK, actual error code is returned in
  4049. pdwErrCode.
  4050. Note:
  4051. Routine does not actually issue a license to Terminal Server, Terminal
  4052. Server must call TLSRpcRetrieveTermServCert() to retrieve its own
  4053. license.
  4054. --*/
  4055. {
  4056. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  4057. DWORD status=ERROR_SUCCESS;
  4058. LPTERMSERVCERTREQHANDLE lpHandle=NULL;
  4059. DBGPrintf(
  4060. DBG_INFORMATION,
  4061. DBG_FACILITY_RPC,
  4062. DBGLEVEL_FUNCTION_TRACE,
  4063. _TEXT("%s : TLSRpcRequestTermServCert\n"),
  4064. lpContext->m_Client
  4065. );
  4066. *ppbChallengeData = NULL;
  4067. *pcbChallengeData = 0;
  4068. // verify client handle
  4069. InterlockedIncrement( &lpContext->m_RefCount );
  4070. if(lpContext->m_ContextType != CONTEXTHANDLE_EMPTY_TYPE)
  4071. {
  4072. SetLastError(status=TLS_E_INVALID_SEQUENCE);
  4073. goto cleanup;
  4074. }
  4075. lpHandle = (LPTERMSERVCERTREQHANDLE)AllocateMemory(
  4076. sizeof(TERMSERVCERTREQHANDLE)
  4077. );
  4078. if(lpHandle == NULL)
  4079. {
  4080. SetLastError(status = ERROR_OUTOFMEMORY);
  4081. goto cleanup;
  4082. }
  4083. //
  4084. // Generate Challenge Data
  4085. //
  4086. lpHandle->pCertRequest = pRequest;
  4087. status = TLSGenerateChallengeData(
  4088. CLIENT_INFO_HYDRA_SERVER,
  4089. &lpHandle->cbChallengeData,
  4090. &lpHandle->pbChallengeData
  4091. );
  4092. if(status != ERROR_SUCCESS)
  4093. {
  4094. goto cleanup;
  4095. }
  4096. // return challenge data
  4097. *pcbChallengeData = lpHandle->cbChallengeData;
  4098. *ppbChallengeData = (PBYTE)midl_user_allocate(*pcbChallengeData);
  4099. if(*ppbChallengeData == NULL)
  4100. {
  4101. SetLastError(status = ERROR_OUTOFMEMORY);
  4102. goto cleanup;
  4103. }
  4104. memcpy( *ppbChallengeData,
  4105. lpHandle->pbChallengeData,
  4106. lpHandle->cbChallengeData);
  4107. lpContext->m_ContextHandle = (HANDLE)lpHandle;
  4108. lpContext->m_ContextType = CONTEXTHANDLE_HYDRA_REQUESTCERT_TYPE;
  4109. cleanup:
  4110. if(status != ERROR_SUCCESS)
  4111. {
  4112. // frees up memory.
  4113. // Can't overwrite context type.
  4114. //lpContext->m_ContextType = CONTEXTHANDLE_EMPTY_TYPE;
  4115. if(lpHandle != NULL)
  4116. {
  4117. FreeMemory(lpHandle->pbChallengeData);
  4118. FreeMemory(lpHandle);
  4119. }
  4120. if(*ppbChallengeData != NULL)
  4121. {
  4122. midl_user_free(*ppbChallengeData);
  4123. }
  4124. if(pRequest != NULL)
  4125. {
  4126. midl_user_free(pRequest);
  4127. }
  4128. *ppbChallengeData = NULL;
  4129. *pcbChallengeData = 0;
  4130. }
  4131. InterlockedDecrement( &lpContext->m_RefCount );
  4132. lpContext->m_LastError=status;
  4133. #if DBG
  4134. lpContext->m_LastCall = RPC_CALL_REQUEST_TERMSRV_CERT;
  4135. #endif
  4136. *pdwErrCode = TLSMapReturnCode(status);
  4137. return RPC_S_OK;
  4138. }
  4139. ////////////////////////////////////////////////////////////////////////////
  4140. error_status_t
  4141. TLSRpcRetrieveTermServCert(
  4142. /* [in] */ PCONTEXT_HANDLE phContext,
  4143. /* [in] */ DWORD cbResponseData,
  4144. /* [size_is][in] */ PBYTE pbResponseData,
  4145. /* [ref][out][in] */ PDWORD pcbCert,
  4146. /* [size_is][out] */ PBYTE* ppbCert,
  4147. /* [ref][out][in] */ PDWORD pdwErrCode
  4148. )
  4149. /*++
  4150. Abstract:
  4151. Private routine to retrieve Terminal Server's license.
  4152. Parameters:
  4153. phContext : client context handle.
  4154. cbResponseData : size of Terminal Server responses data to
  4155. license server's challenge.
  4156. pbResponseData : Terminal Server responses data to license
  4157. server's challenge.
  4158. pcbCert : Size of Terminal Server's license in bytes.
  4159. ppbCert : Terminal Server's license.
  4160. pdwErrCode : error code if fail.
  4161. Returns:
  4162. Function returns RPC_S_OK, actual error code returns in
  4163. pdwErrCode.
  4164. Note:
  4165. Must have call TLSRpcRequestTermServCert().
  4166. --*/
  4167. {
  4168. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  4169. DWORD status=ERROR_SUCCESS;
  4170. LPTERMSERVCERTREQHANDLE lpHandle=NULL;
  4171. CTLSPolicy* pPolicy=NULL;
  4172. PMHANDLE hClient;
  4173. PBYTE pbPkcs7=NULL;
  4174. DWORD cbPkcs7=0;
  4175. TLSDBLICENSEREQUEST LicenseRequest;
  4176. DWORD dwQuantity = 1;
  4177. TLSPRODUCTINFO ProductInfo;
  4178. TCHAR szCompanyName[LSERVER_MAX_STRING_SIZE];
  4179. TCHAR szMachineName[MAXCOMPUTERNAMELENGTH];
  4180. TCHAR szUserName[MAXUSERNAMELENGTH];
  4181. PTLSDbWorkSpace pDbWkSpace;
  4182. PMLICENSEREQUEST PMLicenseRequest;
  4183. PPMLICENSEREQUEST pAdjustedRequest;
  4184. TLSDBLICENSEDPRODUCT LicensedProduct;
  4185. _se_translator_function old_trans_se_func = NULL;
  4186. old_trans_se_func = _set_se_translator( trans_se_func );
  4187. DBGPrintf(
  4188. DBG_INFORMATION,
  4189. DBG_FACILITY_RPC,
  4190. DBGLEVEL_FUNCTION_TRACE,
  4191. _TEXT("%s : TLSRpcRetrieveTermServCert\n"),
  4192. lpContext->m_Client
  4193. );
  4194. // verify client handle
  4195. InterlockedIncrement( &lpContext->m_RefCount );
  4196. if(lpContext->m_ContextType != CONTEXTHANDLE_HYDRA_REQUESTCERT_TYPE)
  4197. {
  4198. SetLastError(status = TLS_E_INVALID_SEQUENCE);
  4199. goto cleanup;
  4200. }
  4201. lpHandle = (LPTERMSERVCERTREQHANDLE)lpContext->m_ContextHandle;
  4202. if( lpHandle == NULL || lpHandle->pCertRequest == NULL ||
  4203. lpHandle->pCertRequest->pbEncryptedHwid == NULL ||
  4204. lpHandle->pCertRequest->cbEncryptedHwid == 0 )
  4205. {
  4206. SetLastError(status = TLS_E_INVALID_SEQUENCE);
  4207. goto cleanup;
  4208. }
  4209. //
  4210. // Verify challenge response data
  4211. //
  4212. //
  4213. // Request a license from specific key pack
  4214. //
  4215. memset(&LicenseRequest, 0, sizeof(TLSDBLICENSEREQUEST));
  4216. if(!LoadResourceString(
  4217. IDS_HS_COMPANYNAME,
  4218. szCompanyName,
  4219. sizeof(szCompanyName) / sizeof(szCompanyName[0])))
  4220. {
  4221. SetLastError(status = TLS_E_INTERNAL);
  4222. goto cleanup;
  4223. }
  4224. if(lpContext->m_Client == NULL)
  4225. {
  4226. if(!LoadResourceString(
  4227. IDS_HS_MACHINENAME,
  4228. LicenseRequest.szMachineName,
  4229. sizeof(LicenseRequest.szMachineName)/sizeof(LicenseRequest.szMachineName[0])))
  4230. {
  4231. SetLastError(status = TLS_E_INTERNAL);
  4232. goto cleanup;
  4233. }
  4234. if(!LoadResourceString(
  4235. IDS_HS_USERNAME,
  4236. LicenseRequest.szUserName,
  4237. sizeof(LicenseRequest.szUserName)/sizeof(LicenseRequest.szUserName[0])))
  4238. {
  4239. SetLastError(status = TLS_E_INTERNAL);
  4240. goto cleanup;
  4241. }
  4242. }
  4243. else
  4244. {
  4245. SAFESTRCPY(LicenseRequest.szMachineName, lpContext->m_Client);
  4246. SAFESTRCPY(LicenseRequest.szUserName, lpContext->m_Client);
  4247. }
  4248. LicenseRequest.dwProductVersion = HYDRACERT_PRODUCT_VERSION;
  4249. LicenseRequest.pszProductId = HYDRAPRODUCT_HS_CERTIFICATE_SKU;
  4250. LicenseRequest.pszCompanyName = szCompanyName;
  4251. LicenseRequest.dwLanguageID = GetSystemDefaultLangID(); // ignore
  4252. LicenseRequest.dwPlatformID = CLIENT_PLATFORMID_WINDOWS_NT_FREE; // WINDOWS
  4253. LicenseRequest.pbEncryptedHwid = lpHandle->pCertRequest->pbEncryptedHwid;
  4254. LicenseRequest.cbEncryptedHwid = lpHandle->pCertRequest->cbEncryptedHwid;
  4255. status=LicenseDecryptHwid(
  4256. &LicenseRequest.hWid,
  4257. LicenseRequest.cbEncryptedHwid,
  4258. LicenseRequest.pbEncryptedHwid,
  4259. g_cbSecretKey,
  4260. g_pbSecretKey
  4261. );
  4262. if(status != ERROR_SUCCESS)
  4263. {
  4264. status = ERROR_INVALID_PARAMETER;
  4265. goto cleanup;
  4266. }
  4267. LicenseRequest.pClientPublicKey = (PCERT_PUBLIC_KEY_INFO)lpHandle->pCertRequest->pSubjectPublicKeyInfo;
  4268. LicenseRequest.clientCertRdn.type = LSCERT_RDN_STRING_TYPE;
  4269. LicenseRequest.clientCertRdn.szRdn = lpHandle->pCertRequest->szSubjectRdn;
  4270. LicenseRequest.dwNumExtensions = lpHandle->pCertRequest->dwNumCertExtension;
  4271. LicenseRequest.pExtensions = (PCERT_EXTENSION)lpHandle->pCertRequest->pCertExtensions;
  4272. hClient = GenerateClientId();
  4273. pPolicy = AcquirePolicyModule(NULL, NULL, FALSE);
  4274. if(pPolicy == NULL)
  4275. {
  4276. SetLastError(status = TLS_E_INTERNAL);
  4277. goto cleanup;
  4278. }
  4279. PMLicenseRequest.dwProductVersion = LicenseRequest.dwProductVersion;
  4280. PMLicenseRequest.pszProductId = LicenseRequest.pszProductId;
  4281. PMLicenseRequest.pszCompanyName = LicenseRequest.pszCompanyName;
  4282. PMLicenseRequest.dwLanguageId = LicenseRequest.dwLanguageID;
  4283. PMLicenseRequest.dwPlatformId = LicenseRequest.dwPlatformID;
  4284. PMLicenseRequest.pszMachineName = LicenseRequest.szMachineName;
  4285. PMLicenseRequest.pszUserName = LicenseRequest.szUserName;
  4286. PMLicenseRequest.dwLicenseType = LICENSETYPE_LICENSE;
  4287. //
  4288. // Inform Policy module start of new license request
  4289. //
  4290. status = pPolicy->PMLicenseRequest(
  4291. hClient,
  4292. REQUEST_NEW,
  4293. (PVOID) &PMLicenseRequest,
  4294. (PVOID *) &pAdjustedRequest
  4295. );
  4296. if(status != ERROR_SUCCESS)
  4297. {
  4298. goto cleanup;
  4299. }
  4300. LicenseRequest.pPolicy = pPolicy;
  4301. LicenseRequest.hClient = hClient;
  4302. LicenseRequest.pPolicyLicenseRequest = pAdjustedRequest;
  4303. LicenseRequest.pClientLicenseRequest = &PMLicenseRequest;
  4304. // Call issue new license from sepcific keypack
  4305. if(!ALLOCATEDBHANDLE(pDbWkSpace, g_GeneralDbTimeout))
  4306. {
  4307. status = TLS_E_ALLOCATE_HANDLE;
  4308. goto cleanup;
  4309. }
  4310. CLEANUPSTMT;
  4311. BEGIN_TRANSACTION(pDbWkSpace);
  4312. try {
  4313. status = TLSDBIssuePermanentLicense(
  4314. USEHANDLE(pDbWkSpace),
  4315. &LicenseRequest,
  4316. FALSE, // bLatestVersion
  4317. FALSE, // bAcceptFewerLicenses
  4318. &dwQuantity,
  4319. &LicensedProduct,
  4320. 0
  4321. );
  4322. }
  4323. catch( SE_Exception e ) {
  4324. status = e.getSeNumber();
  4325. }
  4326. catch(...) {
  4327. status = TLS_E_INTERNAL;
  4328. }
  4329. if(TLS_ERROR(status))
  4330. {
  4331. ROLLBACK_TRANSACTION(pDbWkSpace);
  4332. }
  4333. else
  4334. {
  4335. COMMIT_TRANSACTION(pDbWkSpace);
  4336. }
  4337. FREEDBHANDLE(pDbWkSpace);
  4338. if(status == ERROR_SUCCESS)
  4339. {
  4340. LicensedProduct.pSubjectPublicKeyInfo = (PCERT_PUBLIC_KEY_INFO)lpHandle->pCertRequest->pSubjectPublicKeyInfo;
  4341. //
  4342. // Generate client certificate
  4343. //
  4344. status = TLSGenerateClientCertificate(
  4345. g_hCryptProv,
  4346. 1,
  4347. &LicensedProduct,
  4348. LICENSE_DETAIL_DETAIL,
  4349. &pbPkcs7,
  4350. &cbPkcs7
  4351. );
  4352. if(TLS_ERROR(status) == TRUE)
  4353. {
  4354. goto cleanup;
  4355. }
  4356. status = TLSChainProprietyCertificate(
  4357. g_hCryptProv,
  4358. (CanIssuePermLicense() == FALSE),
  4359. pbPkcs7,
  4360. cbPkcs7,
  4361. ppbCert,
  4362. pcbCert
  4363. );
  4364. if(status == ERROR_SUCCESS)
  4365. {
  4366. if(CanIssuePermLicense() == FALSE)
  4367. {
  4368. status = TLS_W_SELFSIGN_CERTIFICATE;
  4369. }
  4370. }
  4371. }
  4372. cleanup:
  4373. FreeMemory(pbPkcs7);
  4374. if(pPolicy)
  4375. {
  4376. ReleasePolicyModule(pPolicy);
  4377. }
  4378. //
  4379. // Free up Hydra Certificate Request handle,
  4380. // all_nodes attribute so single free.
  4381. //
  4382. if(lpHandle)
  4383. {
  4384. if(lpHandle->pCertRequest)
  4385. {
  4386. midl_user_free(lpHandle->pCertRequest);
  4387. }
  4388. if(lpHandle->pbChallengeData)
  4389. {
  4390. midl_user_free(lpHandle->pbChallengeData);
  4391. }
  4392. FreeMemory(lpHandle);
  4393. }
  4394. if(lpContext->m_ContextType == CONTEXTHANDLE_HYDRA_REQUESTCERT_TYPE)
  4395. {
  4396. //
  4397. // force calling TLSRpcRequestTermServCert() again
  4398. //
  4399. lpContext->m_ContextType = CONTEXTHANDLE_EMPTY_TYPE;
  4400. lpContext->m_ContextHandle = NULL;
  4401. }
  4402. InterlockedDecrement( &lpContext->m_RefCount );
  4403. lpContext->m_LastError=status;
  4404. #if DBG
  4405. lpContext->m_LastCall = RPC_CALL_RETRIEVE_TERMSRV_CERT;
  4406. #endif
  4407. *pdwErrCode = TLSMapReturnCode(status);
  4408. //
  4409. // Reset SE translator
  4410. //
  4411. _set_se_translator(old_trans_se_func);
  4412. return RPC_S_OK;
  4413. }
  4414. ////////////////////////////////////////////////////////////////////////////
  4415. error_status_t
  4416. TLSRpcAuditLicenseKeyPack(
  4417. /* [in] */ PCONTEXT_HANDLE phContext,
  4418. /* [in] */ DWORD dwKeyPackId,
  4419. /* [in] */ FILETIME ftStartTime,
  4420. /* [in] */ FILETIME ftEndTime,
  4421. /* [in] */ BOOL bResetCounter,
  4422. /* [ref][out][in] */ LPTLSKeyPackAudit lplsAudit,
  4423. /* [ref][out][in] */ PDWORD pdwErrCode
  4424. )
  4425. /*++
  4426. Not implemented yet!.
  4427. --*/
  4428. {
  4429. *pdwErrCode = TLSMapReturnCode(TLS_E_NOTSUPPORTED);
  4430. return RPC_S_OK;
  4431. }
  4432. ////////////////////////////////////////////////////////////////////////////
  4433. error_status_t
  4434. TLSRpcGetLSPKCS10CertRequest(
  4435. /* [in] */ PCONTEXT_HANDLE phContext,
  4436. /* [in] */ DWORD dwCertType,
  4437. /* [ref][out][in] */ PDWORD pcbData,
  4438. /* [size_is][size_is][out] */ PBYTE __RPC_FAR *ppbData,
  4439. /* [ref][out][in] */ PDWORD dwErrCode
  4440. )
  4441. /*
  4442. Abstract:
  4443. Note:
  4444. Only return our key at this time, not a PKCS10 request
  4445. */
  4446. {
  4447. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  4448. DWORD status=ERROR_SUCCESS;
  4449. DBGPrintf(
  4450. DBG_INFORMATION,
  4451. DBG_FACILITY_RPC,
  4452. DBGLEVEL_FUNCTION_TRACE,
  4453. _TEXT("%s : TLSRpcGetLSPKCS10CertRequest\n"),
  4454. lpContext->m_Client
  4455. );
  4456. InterlockedIncrement( &lpContext->m_RefCount );
  4457. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_ADMIN))
  4458. {
  4459. status = TLS_E_ACCESS_DENIED;
  4460. goto cleanup;
  4461. }
  4462. if(AcquireAdministrativeLock(INFINITE) == TRUE)
  4463. {
  4464. status = RetrieveKey(
  4465. (dwCertType == TLSCERT_TYPE_EXCHANGE) ?
  4466. LSERVER_LSA_PRIVATEKEY_EXCHANGE :
  4467. LSERVER_LSA_PRIVATEKEY_SIGNATURE,
  4468. ppbData,
  4469. pcbData
  4470. );
  4471. ReleaseAdministrativeLock();
  4472. }
  4473. else
  4474. {
  4475. status = TLS_E_ALLOCATE_HANDLE;
  4476. }
  4477. cleanup:
  4478. lpContext->m_LastError=status;
  4479. InterlockedDecrement( &lpContext->m_RefCount );
  4480. #if DBG
  4481. lpContext->m_LastCall = RPC_CALL_GETPKCS10CERT_REQUEST;
  4482. #endif
  4483. *dwErrCode = TLSMapReturnCode(status);
  4484. return RPC_S_OK;
  4485. }
  4486. //////////////////////////////////////////////////////////////////////////////
  4487. //
  4488. // Replication function
  4489. //
  4490. ////////////////////////////////////////////////////////////////////////////
  4491. error_status_t
  4492. TLSRpcBeginReplication(
  4493. /* [in] */ PCONTEXT_HANDLE phContext,
  4494. /* [string][in] */ LPTSTR pszLsSetupId,
  4495. /* [string][in] */ LPTSTR pszLsServerName,
  4496. /* [in] */ DWORD cbDomainSid,
  4497. /* [size_is][in] */ PBYTE pbDomainSid,
  4498. /* [ref][out][in] */ FILETIME __RPC_FAR *pftLastBackupTime,
  4499. /* [ref][out][in] */ PDWORD pdwErrCode
  4500. )
  4501. /*++
  4502. ++*/
  4503. {
  4504. *pdwErrCode = TLSMapReturnCode(TLS_E_NOTSUPPORTED);
  4505. return RPC_S_OK;
  4506. }
  4507. ////////////////////////////////////////////////////////////////////////////
  4508. error_status_t
  4509. TLSRpcEndReplication(
  4510. /* [in] */ PCONTEXT_HANDLE phContext
  4511. )
  4512. /*++
  4513. ++*/
  4514. {
  4515. return RPC_S_OK;
  4516. }
  4517. ////////////////////////////////////////////////////////////////////////////
  4518. error_status_t
  4519. TLSRpcReplicateRecord(
  4520. /* [in] */ PCONTEXT_HANDLE phContext,
  4521. /* [ref][in] */ PTLSReplRecord pReplRecord,
  4522. /* [ref][out][in] */ PDWORD pdwErrCode
  4523. )
  4524. /*++
  4525. ++*/
  4526. {
  4527. *pdwErrCode = TLSMapReturnCode(TLS_E_NOTSUPPORTED);
  4528. return RPC_S_OK;
  4529. }
  4530. ////////////////////////////////////////////////////////////////////////////
  4531. error_status_t
  4532. TLSRpcTableEnumBegin(
  4533. /* [in] */ PCONTEXT_HANDLE phContext,
  4534. /* [in] */ DWORD dwSearchParam,
  4535. /* [ref][in] */ PTLSReplRecord pRecord,
  4536. /* [ref][out][in] */ PDWORD pdwErrCode
  4537. )
  4538. /*++
  4539. ++*/
  4540. {
  4541. *pdwErrCode = TLSMapReturnCode(TLS_E_NOTSUPPORTED);
  4542. return RPC_S_OK;
  4543. }
  4544. ////////////////////////////////////////////////////////////////////////////
  4545. error_status_t
  4546. TLSRpcTableEnumNext(
  4547. /* [in] */ PCONTEXT_HANDLE phContext,
  4548. /* [ref][out][in] */ PTLSReplRecord pRecord,
  4549. /* [ref][out][in] */ PDWORD pdwErrCode
  4550. )
  4551. /*++
  4552. ++*/
  4553. {
  4554. *pdwErrCode = TLSMapReturnCode(TLS_E_NOTSUPPORTED);
  4555. return RPC_S_OK;
  4556. }
  4557. ////////////////////////////////////////////////////////////////////////////
  4558. error_status_t
  4559. TLSRpcTableEnumEnd(
  4560. /* [in] */ PCONTEXT_HANDLE phContext
  4561. )
  4562. /*++
  4563. ++*/
  4564. {
  4565. return RPC_S_OK;
  4566. }
  4567. ////////////////////////////////////////////////////////////////////////////
  4568. error_status_t
  4569. TLSRpcInstallPolicyModule(
  4570. /* [in] */ PCONTEXT_HANDLE phContext,
  4571. /* [string][in] */ LPTSTR pszCompanyName,
  4572. /* [string][in] */ LPTSTR pszProductId,
  4573. /* [string][in] */ LPTSTR pszPolicyDllName,
  4574. /* [ref][out][in] */ PDWORD pdwErrCode
  4575. )
  4576. /*++
  4577. ++*/
  4578. {
  4579. *pdwErrCode = TLSMapReturnCode(TLS_E_NOTSUPPORTED);
  4580. return RPC_S_OK;
  4581. }
  4582. ////////////////////////////////////////////////////////////////////////////
  4583. error_status_t
  4584. TLSRpcAnnounceServer(
  4585. /* [in] */ PCONTEXT_HANDLE phContext,
  4586. /* [in] */ DWORD dwAnnounceType,
  4587. /* [in] */ FILETIME __RPC_FAR *pLastStartupTime,
  4588. /* [string][in] */ LPTSTR pszSetupId,
  4589. /* [string][in] */ LPTSTR pszDomainName,
  4590. /* [string][in] */ LPTSTR pszLserverName,
  4591. /* [ref][out][in] */ PDWORD pdwErrCode
  4592. )
  4593. /*++
  4594. Abstract:
  4595. Private routine for other license server to announce presence of
  4596. itself.
  4597. Parameters:
  4598. Returns:
  4599. Note:
  4600. ++*/
  4601. {
  4602. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  4603. DWORD status = ERROR_SUCCESS;
  4604. BOOL bSuccess = TRUE;
  4605. DBGPrintf(
  4606. DBG_INFORMATION,
  4607. DBG_FACILITY_RPC,
  4608. DBGLEVEL_FUNCTION_TRACE,
  4609. _TEXT("%s : TLSRpcAnnounceServer\n"),
  4610. lpContext->m_Client
  4611. );
  4612. InterlockedIncrement( &lpContext->m_RefCount );
  4613. __try {
  4614. //
  4615. // Verify it is a license server
  4616. //
  4617. if(lpContext->m_ClientFlags != CLIENT_ACCESS_LSERVER)
  4618. {
  4619. status = TLS_E_ACCESS_DENIED;
  4620. }
  4621. if( status == ERROR_SUCCESS &&
  4622. (dwAnnounceType == TLSANNOUNCE_TYPE_STARTUP || dwAnnounceType == TLSANNOUNCE_TYPE_RESPONSE) )
  4623. {
  4624. status = TLSRegisterServerWithName(
  4625. pszSetupId,
  4626. pszDomainName,
  4627. pszLserverName
  4628. );
  4629. if(status == TLS_E_DUPLICATE_RECORD)
  4630. {
  4631. status = ERROR_SUCCESS;
  4632. }
  4633. }
  4634. if(status == ERROR_SUCCESS)
  4635. {
  4636. if(dwAnnounceType == TLSANNOUNCE_TYPE_STARTUP)
  4637. {
  4638. //
  4639. // Prevent loop back, use job to response announce
  4640. //
  4641. status = TLSStartAnnounceResponseJob(
  4642. pszSetupId,
  4643. pszDomainName,
  4644. pszLserverName,
  4645. &g_ftLastShutdownTime
  4646. );
  4647. }
  4648. if(status == ERROR_SUCCESS)
  4649. {
  4650. // Create a CSSync workobject to sync. local LKP
  4651. status = TLSPushSyncLocalLkpToServer(
  4652. pszSetupId,
  4653. pszDomainName,
  4654. pszLserverName,
  4655. pLastStartupTime
  4656. );
  4657. }
  4658. else
  4659. {
  4660. // reset error code, can't connect back to server -
  4661. // server might be available anymore.
  4662. status = ERROR_SUCCESS;
  4663. }
  4664. }
  4665. }
  4666. __except(EXCEPTION_EXECUTE_HANDLER)
  4667. {
  4668. status = GetExceptionCode();
  4669. }
  4670. lpContext->m_LastError=status;
  4671. InterlockedDecrement( &lpContext->m_RefCount );
  4672. #if DBG
  4673. lpContext->m_LastCall = RPC_CALL_ANNOUNCE_SERVER;
  4674. #endif
  4675. *pdwErrCode = TLSMapReturnCode(status);
  4676. return RPC_S_OK;
  4677. }
  4678. ////////////////////////////////////////////////////////////////////////////
  4679. error_status_t
  4680. TLSRpcLookupServer(
  4681. /* [in] */ PCONTEXT_HANDLE phContext,
  4682. /* [string][in] */ LPTSTR pszLookupSetupId,
  4683. /* [size_is][string][out][in] */ LPTSTR pszLsSetupId,
  4684. /* [out][in] */ PDWORD pcbSetupId,
  4685. /* [size_is][string][out][in] */ LPTSTR pszDomainName,
  4686. /* [ref][out][in] */ PDWORD pcbDomainName,
  4687. /* [size_is][string][out][in] */ LPTSTR pszMachineName,
  4688. /* [ref][out][in] */ PDWORD pcbMachineName,
  4689. /* [ref][out][in] */ PDWORD pdwErrCode
  4690. )
  4691. /*++
  4692. Abstract:
  4693. Look up a license server via a license server's setupId.
  4694. Parameters:
  4695. Returns:
  4696. Note:
  4697. ++*/
  4698. {
  4699. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  4700. DBGPrintf(
  4701. DBG_INFORMATION,
  4702. DBG_FACILITY_RPC,
  4703. DBGLEVEL_FUNCTION_TRACE,
  4704. _TEXT("%s : TLSRpcLookupServer\n"),
  4705. lpContext->m_Client
  4706. );
  4707. InterlockedIncrement( &lpContext->m_RefCount );
  4708. TLServerInfo ServerInfo;
  4709. DWORD dwStatus = ERROR_SUCCESS;
  4710. __try {
  4711. if(_tcsicmp(pszLookupSetupId, g_pszServerPid) == 0)
  4712. {
  4713. _tcsncpy(
  4714. pszLsSetupId,
  4715. g_pszServerPid,
  4716. min(_tcslen(g_pszServerPid), *pcbSetupId)
  4717. );
  4718. if(*pcbSetupId <= _tcslen(g_pszServerPid))
  4719. {
  4720. dwStatus = TLS_E_INVALID_DATA;
  4721. }
  4722. else
  4723. {
  4724. pszLsSetupId[min(_tcslen(g_pszServerPid), *pcbSetupId - 1)] = _TEXT('\0');
  4725. }
  4726. *pcbSetupId = _tcslen(g_pszServerPid) + 1;
  4727. //--------------------------------------------------------------
  4728. _tcsncpy(
  4729. pszDomainName,
  4730. g_szScope,
  4731. min(_tcslen(g_szScope), *pcbDomainName)
  4732. );
  4733. if(*pcbDomainName <= _tcslen(g_szScope))
  4734. {
  4735. dwStatus = TLS_E_INVALID_DATA;
  4736. }
  4737. else
  4738. {
  4739. pszDomainName[min(_tcslen(g_szScope), *pcbDomainName - 1)] = _TEXT('\0');
  4740. }
  4741. *pcbDomainName = _tcslen(g_szScope) + 1;
  4742. //--------------------------------------------------------------
  4743. _tcsncpy(
  4744. pszMachineName,
  4745. g_szComputerName,
  4746. min(_tcslen(g_szComputerName), *pcbMachineName)
  4747. );
  4748. if(*pcbMachineName <= _tcslen(g_szComputerName))
  4749. {
  4750. dwStatus = TLS_E_INVALID_DATA;
  4751. }
  4752. else
  4753. {
  4754. pszMachineName[min(_tcslen(g_szComputerName), *pcbMachineName - 1)] = _TEXT('\0');
  4755. }
  4756. *pcbMachineName = _tcslen(g_szComputerName) + 1;
  4757. }
  4758. else
  4759. {
  4760. dwStatus = TLSLookupRegisteredServer(
  4761. pszLookupSetupId,
  4762. NULL,
  4763. pszMachineName,
  4764. &ServerInfo
  4765. );
  4766. if(dwStatus == ERROR_SUCCESS)
  4767. {
  4768. _tcsncpy(
  4769. pszLsSetupId,
  4770. ServerInfo.GetServerId(),
  4771. min(_tcslen(ServerInfo.GetServerId()), *pcbSetupId)
  4772. );
  4773. if(*pcbSetupId <= _tcslen(ServerInfo.GetServerId()))
  4774. {
  4775. dwStatus = TLS_E_INVALID_DATA;
  4776. }
  4777. else
  4778. {
  4779. pszLsSetupId[min(_tcslen(ServerInfo.GetServerId()), *pcbSetupId - 1)] = _TEXT('\0');
  4780. }
  4781. *pcbSetupId = _tcslen(ServerInfo.GetServerId()) + 1;
  4782. //--------------------------------------------------------------
  4783. _tcsncpy(
  4784. pszDomainName,
  4785. ServerInfo.GetServerDomain(),
  4786. min(_tcslen(ServerInfo.GetServerDomain()), *pcbDomainName)
  4787. );
  4788. if(*pcbDomainName <= _tcslen(ServerInfo.GetServerDomain()))
  4789. {
  4790. dwStatus = TLS_E_INVALID_DATA;
  4791. }
  4792. else
  4793. {
  4794. pszDomainName[min(_tcslen(ServerInfo.GetServerDomain()), *pcbDomainName - 1)] = _TEXT('\0');
  4795. }
  4796. *pcbDomainName = _tcslen(ServerInfo.GetServerDomain()) + 1;
  4797. //--------------------------------------------------------------
  4798. _tcsncpy(
  4799. pszMachineName,
  4800. ServerInfo.GetServerName(),
  4801. min(_tcslen(ServerInfo.GetServerName()), *pcbMachineName)
  4802. );
  4803. if(*pcbMachineName <= _tcslen(ServerInfo.GetServerName()))
  4804. {
  4805. dwStatus = TLS_E_INVALID_DATA;
  4806. }
  4807. else
  4808. {
  4809. pszMachineName[min(_tcslen(ServerInfo.GetServerName()), *pcbMachineName - 1)] = _TEXT('\0');
  4810. }
  4811. *pcbMachineName = _tcslen(ServerInfo.GetServerName()) + 1;
  4812. }
  4813. }
  4814. }
  4815. __except(EXCEPTION_EXECUTE_HANDLER)
  4816. {
  4817. dwStatus = GetExceptionCode();
  4818. }
  4819. lpContext->m_LastError=dwStatus;
  4820. InterlockedDecrement( &lpContext->m_RefCount );
  4821. #if DBG
  4822. lpContext->m_LastCall = RPC_CALL_SERVERLOOKUP;
  4823. #endif
  4824. *pdwErrCode = TLSMapReturnCode(dwStatus);
  4825. return RPC_S_OK;
  4826. }
  4827. ////////////////////////////////////////////////////////////////////////////
  4828. error_status_t
  4829. TLSRpcAnnounceLicensePack(
  4830. /* [in] */ PCONTEXT_HANDLE phContext,
  4831. /* [in] */ PTLSReplRecord pReplRecord,
  4832. /* [ref][out][in] */ PDWORD pdwErrCode
  4833. )
  4834. /*++
  4835. Abstract:
  4836. Private routine for one license server to announce it has particular
  4837. License Pack.
  4838. Parameters:
  4839. Returns:
  4840. ++*/
  4841. {
  4842. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  4843. DWORD dwStatus=ERROR_SUCCESS;
  4844. PTLSDbWorkSpace pDbWkSpace=NULL;
  4845. _se_translator_function old_trans_se_func = NULL;
  4846. old_trans_se_func = _set_se_translator( trans_se_func );
  4847. DBGPrintf(
  4848. DBG_INFORMATION,
  4849. DBG_FACILITY_RPC,
  4850. DBGLEVEL_FUNCTION_TRACE,
  4851. _TEXT("%s : TLSRpcAnnounceLicensePack\n"),
  4852. lpContext->m_Client
  4853. );
  4854. InterlockedIncrement( &lpContext->m_RefCount );
  4855. if(lpContext->m_ClientFlags != CLIENT_ACCESS_LSERVER)
  4856. {
  4857. dwStatus = TLS_E_ACCESS_DENIED;
  4858. goto cleanup;
  4859. }
  4860. if(pReplRecord->dwUnionType != UNION_TYPE_LICENSEPACK)
  4861. {
  4862. dwStatus = TLS_E_INVALID_DATA;
  4863. goto cleanup;
  4864. }
  4865. if(!ALLOCATEDBHANDLE(pDbWkSpace, g_GeneralDbTimeout))
  4866. {
  4867. dwStatus = TLS_E_ALLOCATE_HANDLE;
  4868. goto cleanup;
  4869. }
  4870. CLEANUPSTMT;
  4871. BEGIN_TRANSACTION(pDbWkSpace);
  4872. try {
  4873. TLSLICENSEPACK LicPack;
  4874. LicPack = pReplRecord->w.ReplLicPack;
  4875. //
  4876. // TODO - verify input parameters
  4877. //
  4878. dwStatus = TLSDBRemoteKeyPackAdd(
  4879. USEHANDLE(pDbWkSpace),
  4880. &LicPack
  4881. );
  4882. }
  4883. catch( SE_Exception e ) {
  4884. dwStatus = e.getSeNumber();
  4885. }
  4886. catch(...) {
  4887. dwStatus = TLS_E_INTERNAL;
  4888. }
  4889. if(TLS_ERROR(dwStatus) && dwStatus != TLS_E_DUPLICATE_RECORD)
  4890. {
  4891. ROLLBACK_TRANSACTION(pDbWkSpace);
  4892. }
  4893. else
  4894. {
  4895. COMMIT_TRANSACTION(pDbWkSpace);
  4896. }
  4897. FREEDBHANDLE(pDbWkSpace);
  4898. cleanup:
  4899. lpContext->m_LastError=dwStatus;
  4900. InterlockedDecrement( &lpContext->m_RefCount );
  4901. #if DBG
  4902. lpContext->m_LastCall = RPC_CALL_ANNOUNCELICENSEPACK;
  4903. #endif
  4904. *pdwErrCode = TLSMapReturnCode(dwStatus);
  4905. //
  4906. // Reset SE translator
  4907. //
  4908. _set_se_translator(old_trans_se_func);
  4909. return RPC_S_OK;
  4910. }
  4911. ////////////////////////////////////////////////////////////////////////////
  4912. error_status_t
  4913. TLSRpcReturnLicensedProduct(
  4914. /* [in] */ PCONTEXT_HANDLE phContext,
  4915. /* [in] */ PTLSLicenseToBeReturn pClientLicense,
  4916. /* [ref][out][in] */ PDWORD pdwErrCode
  4917. )
  4918. /*++
  4919. ++*/
  4920. {
  4921. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  4922. DWORD dwStatus=ERROR_SUCCESS;
  4923. CTLSPolicy* pPolicy=NULL;
  4924. PTLSDbWorkSpace pDbWorkSpace;
  4925. PMHANDLE hClient;
  4926. _se_translator_function old_trans_se_func = NULL;
  4927. old_trans_se_func = _set_se_translator( trans_se_func );
  4928. DBGPrintf(
  4929. DBG_INFORMATION,
  4930. DBG_FACILITY_RPC,
  4931. DBGLEVEL_FUNCTION_TRACE,
  4932. _TEXT("%s : TLSRpcReturnLicensedProduct\n"),
  4933. lpContext->m_Client
  4934. );
  4935. InterlockedIncrement( &lpContext->m_RefCount );
  4936. if(lpContext->m_ClientFlags != CLIENT_ACCESS_LSERVER)
  4937. {
  4938. dwStatus = TLS_E_ACCESS_DENIED;
  4939. goto cleanup;
  4940. }
  4941. pPolicy = AcquirePolicyModule(
  4942. pClientLicense->pszCompanyName,
  4943. pClientLicense->pszOrgProductId,
  4944. FALSE
  4945. );
  4946. if(pPolicy == NULL)
  4947. {
  4948. dwStatus = GetLastError();
  4949. goto cleanup;
  4950. }
  4951. hClient = GenerateClientId();
  4952. if(!ALLOCATEDBHANDLE(pDbWkSpace, g_GeneralDbTimeout))
  4953. {
  4954. dwStatus=TLS_E_ALLOCATE_HANDLE;
  4955. goto cleanup;
  4956. }
  4957. CLEANUPSTMT;
  4958. BEGIN_TRANSACTION(pDbWorkSpace);
  4959. try {
  4960. dwStatus = TLSReturnClientLicensedProduct(
  4961. USEHANDLE(pDbWkSpace),
  4962. hClient,
  4963. pPolicy,
  4964. pClientLicense
  4965. );
  4966. }
  4967. catch( SE_Exception e ) {
  4968. dwStatus = e.getSeNumber();
  4969. }
  4970. catch(...) {
  4971. SetLastError(dwStatus = TLS_E_INTERNAL);
  4972. }
  4973. if(TLS_ERROR(dwStatus))
  4974. {
  4975. ROLLBACK_TRANSACTION(pDbWorkSpace);
  4976. }
  4977. else
  4978. {
  4979. COMMIT_TRANSACTION(pDbWorkSpace);
  4980. }
  4981. FREEDBHANDLE(pDbWorkSpace);
  4982. cleanup:
  4983. lpContext->m_LastError=dwStatus;
  4984. InterlockedDecrement( &lpContext->m_RefCount );
  4985. #if DBG
  4986. lpContext->m_LastCall = RPC_CALL_RETURNLICENSE;
  4987. #endif
  4988. *pdwErrCode = TLSMapReturnCode(dwStatus);
  4989. if(pPolicy)
  4990. {
  4991. pPolicy->PMLicenseRequest(
  4992. hClient,
  4993. REQUEST_COMPLETE,
  4994. UlongToPtr (dwStatus),
  4995. NULL
  4996. );
  4997. ReleasePolicyModule(pPolicy);
  4998. }
  4999. _set_se_translator(old_trans_se_func);
  5000. return RPC_S_OK;
  5001. }
  5002. ////////////////////////////////////////////////////////////////////////////
  5003. error_status_t
  5004. TLSRpcChallengeServer(
  5005. /* [in] */ PCONTEXT_HANDLE phContext,
  5006. /* [in] */ DWORD dwClientType,
  5007. /* [in] */ PTLSCHALLENGEDATA pClientChallenge,
  5008. /* [out][in] */ PTLSCHALLENGERESPONSEDATA* pServerResponse,
  5009. /* [out][in] */ PTLSCHALLENGEDATA* pServerChallenge,
  5010. /* [ref][out][in] */ PDWORD pdwErrCode
  5011. )
  5012. /*++
  5013. Abstract:
  5014. Private routine for client to challenge server in order for client
  5015. confirm server's identity. License Server, in addition to response to
  5016. client's challenge, also generate random challenge data based on
  5017. client's self-declare type back to client.
  5018. Parameter:
  5019. phContext : Client's context handle.
  5020. dwClientType : Client self-pronounce type, valid values are ...
  5021. pClientChallenge : Client challenge data.
  5022. pServerResponse : Server's responses to client's challenge.
  5023. pServerChallenge : Server's challenge to client.
  5024. pdwErrCode : Error code if failed.
  5025. Returns:
  5026. Notes:
  5027. Private routine for LrWiz and License Server to identify itself.
  5028. --*/
  5029. {
  5030. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  5031. DWORD status=ERROR_SUCCESS;
  5032. PTLSCHALLENGEDATA pChallenge=NULL;
  5033. PTLSCHALLENGERESPONSEDATA pResponse = NULL;
  5034. HCRYPTPROV hProv = NULL;
  5035. DBGPrintf(
  5036. DBG_INFORMATION,
  5037. DBG_FACILITY_RPC,
  5038. DBGLEVEL_FUNCTION_TRACE,
  5039. _TEXT("%s : TLSRpcChallengeServer\n"),
  5040. lpContext->m_Client
  5041. );
  5042. InterlockedIncrement( &lpContext->m_RefCount );
  5043. //if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_ADMIN))
  5044. //{
  5045. // status = TLS_E_ACCESS_DENIED;
  5046. // goto cleanup;
  5047. //}
  5048. if(lpContext->m_ContextType != CONTEXTHANDLE_EMPTY_TYPE)
  5049. {
  5050. status = TLS_E_INVALID_SEQUENCE;
  5051. goto cleanup;
  5052. }
  5053. //
  5054. // Input parameters...
  5055. //
  5056. if( pClientChallenge == NULL ||
  5057. pServerResponse == NULL ||
  5058. pServerChallenge == NULL )
  5059. {
  5060. status = TLS_E_INVALID_DATA;
  5061. goto cleanup;
  5062. }
  5063. //
  5064. // Verify Data send by client
  5065. //
  5066. if( pClientChallenge->dwVersion != TLS_CURRENT_CHALLENGE_VERSION ||
  5067. pClientChallenge->cbChallengeData == 0 ||
  5068. pClientChallenge->pbChallengeData == NULL )
  5069. {
  5070. status = TLS_E_INVALID_DATA;
  5071. goto cleanup;
  5072. }
  5073. pResponse = (PTLSCHALLENGERESPONSEDATA)midl_user_allocate(sizeof(TLSCHALLENGERESPONSEDATA));
  5074. if(pResponse == NULL)
  5075. {
  5076. status = GetLastError();
  5077. goto cleanup;
  5078. }
  5079. pChallenge = (PTLSCHALLENGEDATA)AllocateMemory(sizeof(TLSCHALLENGEDATA));
  5080. if(pChallenge == NULL)
  5081. {
  5082. status = GetLastError();
  5083. goto cleanup;
  5084. }
  5085. *pServerChallenge = (PTLSCHALLENGEDATA)midl_user_allocate(sizeof(TLSCHALLENGEDATA));
  5086. if(*pServerChallenge == NULL)
  5087. {
  5088. status = GetLastError();
  5089. goto cleanup;
  5090. }
  5091. //
  5092. // Generate Challenge response data
  5093. //
  5094. status = TLSGenerateChallengeResponseData(
  5095. g_hCryptProv,
  5096. dwClientType,
  5097. pClientChallenge,
  5098. &(pResponse->pbResponseData),
  5099. &(pResponse->cbResponseData)
  5100. );
  5101. if(status != ERROR_SUCCESS)
  5102. {
  5103. status = TLS_E_INVALID_DATA;
  5104. goto cleanup;
  5105. }
  5106. //
  5107. // Generate Server side challenge data
  5108. //
  5109. pChallenge->dwVersion = TLS_CURRENT_CHALLENGE_VERSION;
  5110. if (CryptAcquireContext(&hProv,NULL,NULL,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT)) {
  5111. if (!CryptGenRandom(hProv,sizeof(pChallenge->dwRandom), (BYTE *) &pChallenge->dwRandom)) {
  5112. status = TLS_E_INTERNAL;
  5113. goto cleanup;
  5114. }
  5115. } else {
  5116. status = TLS_E_INTERNAL;
  5117. goto cleanup;
  5118. }
  5119. //
  5120. // This must range from 1 to 128, as it's used as an offset into the
  5121. // challenge data buffer
  5122. //
  5123. pChallenge->dwRandom %= RANDOM_CHALLENGE_DATASIZE;
  5124. pChallenge->dwRandom++;
  5125. status = TLSGenerateRandomChallengeData(
  5126. g_hCryptProv,
  5127. &(pChallenge->pbChallengeData),
  5128. &(pChallenge->cbChallengeData)
  5129. );
  5130. // base on type, mark this handle...
  5131. if(dwClientType == CLIENT_TYPE_LRWIZ)
  5132. {
  5133. lpContext->m_ContextType = CONTEXTHANDLE_CHALLENGE_LRWIZ_TYPE;
  5134. }
  5135. else
  5136. {
  5137. lpContext->m_ContextType = CONTEXTHANDLE_CHALLENGE_SERVER_TYPE;
  5138. }
  5139. (*pServerChallenge)->pbChallengeData = (PBYTE)midl_user_allocate(pChallenge->cbChallengeData);
  5140. if((*pServerChallenge)->pbChallengeData == NULL)
  5141. {
  5142. status = GetLastError();
  5143. goto cleanup;
  5144. }
  5145. (*pServerChallenge)->dwVersion = TLS_CURRENT_CHALLENGE_VERSION;
  5146. (*pServerChallenge)->dwRandom = pChallenge->dwRandom;
  5147. (*pServerChallenge)->cbChallengeData = pChallenge->cbChallengeData;
  5148. memcpy(
  5149. (*pServerChallenge)->pbChallengeData,
  5150. pChallenge->pbChallengeData,
  5151. pChallenge->cbChallengeData
  5152. );
  5153. lpContext->m_ContextHandle = (HANDLE)(pChallenge);
  5154. *pServerResponse = pResponse;
  5155. cleanup:
  5156. if(status != ERROR_SUCCESS)
  5157. {
  5158. if(pChallenge)
  5159. {
  5160. if(pChallenge->pbChallengeData)
  5161. {
  5162. FreeMemory(pChallenge->pbChallengeData);
  5163. }
  5164. if(pChallenge->pbReservedData)
  5165. {
  5166. FreeMemory(pChallenge->pbReservedData);
  5167. }
  5168. FreeMemory(pChallenge);
  5169. }
  5170. if(pResponse)
  5171. {
  5172. if(pResponse->pbResponseData)
  5173. {
  5174. FreeMemory(pResponse->pbResponseData);
  5175. }
  5176. if(pResponse->pbReservedData)
  5177. {
  5178. FreeMemory(pResponse->pbReservedData);
  5179. }
  5180. midl_user_free(pResponse);
  5181. }
  5182. }
  5183. if (hProv)
  5184. CryptReleaseContext(hProv,0);
  5185. lpContext->m_LastError=status;
  5186. InterlockedDecrement( &lpContext->m_RefCount );
  5187. #if DBG
  5188. lpContext->m_LastCall = RPC_CALL_CHALLENGESERVER;
  5189. #endif
  5190. *pdwErrCode = TLSMapReturnCode(status);
  5191. return RPC_S_OK;
  5192. }
  5193. ////////////////////////////////////////////////////////////////////////////
  5194. error_status_t
  5195. TLSRpcResponseServerChallenge(
  5196. /* [in] */ PCONTEXT_HANDLE phContext,
  5197. /* [in] */ PTLSCHALLENGERESPONSEDATA pClientResponse,
  5198. /* [ref][out][in] */ PDWORD pdwErrCode
  5199. )
  5200. /*++
  5201. Abstract:
  5202. Client's responses to Server challenge returned TLSRpcChallengeServer(),
  5203. must have call TLSRpcChallengeServer().
  5204. Parameter:
  5205. phContext:
  5206. pClientResponses: Client's response to server's challenge.
  5207. pdwErrCode : Return error code.
  5208. Returns:
  5209. Note:
  5210. --*/
  5211. {
  5212. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  5213. DWORD status=ERROR_SUCCESS;
  5214. DWORD dwClientType;
  5215. PTLSCHALLENGEDATA pServerToClientChallenge;
  5216. DBGPrintf(
  5217. DBG_INFORMATION,
  5218. DBG_FACILITY_RPC,
  5219. DBGLEVEL_FUNCTION_TRACE,
  5220. _TEXT("%s : TLSRpcResponseServerChallenge\n"),
  5221. lpContext->m_Client
  5222. );
  5223. InterlockedIncrement( &lpContext->m_RefCount );
  5224. //if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_ADMIN))
  5225. //{
  5226. // status = TLS_E_ACCESS_DENIED;
  5227. // goto cleanup;
  5228. //}
  5229. if( pClientResponse == NULL ||
  5230. pClientResponse->pbResponseData == NULL ||
  5231. pClientResponse->cbResponseData == 0 )
  5232. {
  5233. status = TLS_E_INVALID_DATA;
  5234. goto cleanup;
  5235. }
  5236. if(lpContext->m_ContextType != CONTEXTHANDLE_CHALLENGE_SERVER_TYPE &&
  5237. lpContext->m_ContextType != CONTEXTHANDLE_CHALLENGE_LRWIZ_TYPE)
  5238. {
  5239. status = TLS_E_INVALID_SEQUENCE;
  5240. goto cleanup;
  5241. }
  5242. if(lpContext->m_ContextHandle == NULL)
  5243. {
  5244. status = TLS_E_INTERNAL;
  5245. goto cleanup;
  5246. }
  5247. if(lpContext->m_ContextType == CONTEXTHANDLE_CHALLENGE_LRWIZ_TYPE)
  5248. {
  5249. dwClientType = CLIENT_TYPE_LRWIZ;
  5250. }
  5251. else
  5252. {
  5253. dwClientType = CLIENT_TYPE_TLSERVER;
  5254. }
  5255. pServerToClientChallenge = (PTLSCHALLENGEDATA)lpContext->m_ContextHandle;
  5256. //
  5257. // base on client type, verify challenge response data
  5258. //
  5259. status = TLSVerifyChallengeResponse(
  5260. g_hCryptProv,
  5261. dwClientType,
  5262. pServerToClientChallenge,
  5263. pClientResponse
  5264. );
  5265. if(status != ERROR_SUCCESS)
  5266. {
  5267. status = TLS_E_INVALID_DATA;
  5268. }
  5269. else
  5270. {
  5271. if(dwClientType == CLIENT_TYPE_LRWIZ)
  5272. {
  5273. lpContext->m_ClientFlags |= CLIENT_ACCESS_LRWIZ;
  5274. }
  5275. else
  5276. {
  5277. lpContext->m_ClientFlags |= CLIENT_ACCESS_LSERVER;
  5278. }
  5279. }
  5280. if(pServerToClientChallenge != NULL)
  5281. {
  5282. FreeMemory(pServerToClientChallenge->pbChallengeData);
  5283. FreeMemory(pServerToClientChallenge);
  5284. }
  5285. lpContext->m_ContextHandle = NULL;
  5286. lpContext->m_ContextType = CONTEXTHANDLE_EMPTY_TYPE;
  5287. cleanup:
  5288. lpContext->m_LastError=status;
  5289. InterlockedDecrement( &lpContext->m_RefCount );
  5290. #if DBG
  5291. lpContext->m_LastCall = RPC_CALL_RESPONSESERVERCHALLENGE;
  5292. #endif
  5293. *pdwErrCode = TLSMapReturnCode(status);
  5294. return RPC_S_OK;
  5295. }
  5296. ////////////////////////////////////////////////////////////////////////////
  5297. error_status_t
  5298. TLSRpcGetTlsPrivateData(
  5299. /* [in] */ PCONTEXT_HANDLE phContext,
  5300. /* [in] */ DWORD dwPrivateDataType,
  5301. /* [switch_is][in] */ PTLSPrivateDataUnion pSearchData,
  5302. /* [ref][out][in] */ PDWORD pdwRetDataType,
  5303. /* [switch_is][out] */ PTLSPrivateDataUnion __RPC_FAR *ppPrivateData,
  5304. /* [ref][out][in] */ PDWORD pdwErrCode
  5305. )
  5306. /*++
  5307. Abstract:
  5308. Retrieve license server's private data, this include Server's
  5309. unique ID, PID, and registered SPK if any.
  5310. Parameters:
  5311. phContext : Client's context handle.
  5312. dwPrivateDataType : Type of private data interested.
  5313. pSearchData : Type of data to search, currently ignore.
  5314. pdwRetDataType : Return data type.
  5315. ppPrivateData : License Server's private data.
  5316. pdwErrCode : Error Code.
  5317. Returns:
  5318. Note:
  5319. Only LrWiz and License Server can invoke this RPC call.
  5320. --*/
  5321. {
  5322. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  5323. DWORD status=ERROR_SUCCESS;
  5324. DWORD cbSource=0;
  5325. PBYTE pbSource=NULL;
  5326. DBGPrintf(
  5327. DBG_INFORMATION,
  5328. DBG_FACILITY_RPC,
  5329. DBGLEVEL_FUNCTION_TRACE,
  5330. _TEXT("%s : TLSRpcGetTlsPrivateData\n"),
  5331. lpContext->m_Client
  5332. );
  5333. InterlockedIncrement( &lpContext->m_RefCount );
  5334. //
  5335. // relax restriction on who can get private data
  5336. //
  5337. if( dwPrivateDataType != TLS_PRIVATEDATA_PID &&
  5338. dwPrivateDataType != TLS_PRIVATEDATA_UNIQUEID )
  5339. {
  5340. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_LRWIZ))
  5341. {
  5342. status = TLS_E_ACCESS_DENIED;
  5343. goto cleanup;
  5344. }
  5345. }
  5346. if( dwPrivateDataType < TLS_PRIVATEDATA_MIN ||
  5347. dwPrivateDataType > TLS_PRIVATEDATA_MAX )
  5348. {
  5349. status = TLS_E_INVALID_DATA;
  5350. goto cleanup;
  5351. }
  5352. //
  5353. // Not supported yet...
  5354. //
  5355. if(dwPrivateDataType == TLS_PRIVATEDATA_INSTALLED_CERT)
  5356. {
  5357. status = TLS_E_NOTSUPPORTED;
  5358. goto cleanup;
  5359. }
  5360. //
  5361. // Don't really need this but we might need to support
  5362. // re-generate of License Server ID
  5363. //
  5364. if(!AcquireAdministrativeLock(INFINITE))
  5365. {
  5366. status = TLS_E_ALLOCATE_HANDLE;
  5367. goto cleanup;
  5368. }
  5369. switch(dwPrivateDataType)
  5370. {
  5371. case TLS_PRIVATEDATA_UNIQUEID:
  5372. pbSource = (PBYTE)g_pszServerUniqueId;
  5373. cbSource = g_cbServerUniqueId;
  5374. break;
  5375. case TLS_PRIVATEDATA_PID:
  5376. pbSource = (PBYTE)g_pszServerPid;
  5377. cbSource = g_cbServerPid;
  5378. break;
  5379. case TLS_PRIVATEDATA_SPK:
  5380. pbSource = g_pbServerSPK;
  5381. cbSource = g_cbServerSPK;
  5382. }
  5383. //
  5384. // Currently, what you ask is what you get.
  5385. //
  5386. *pdwRetDataType = dwPrivateDataType;
  5387. if( (dwPrivateDataType != TLS_PRIVATEDATA_SYSTEMLANGID) &&
  5388. (pbSource == NULL || cbSource == 0) )
  5389. {
  5390. status = TLS_E_RECORD_NOTFOUND;
  5391. }
  5392. else
  5393. {
  5394. *ppPrivateData = (PTLSPrivateDataUnion)midl_user_allocate(sizeof(TLSPrivateDataUnion));
  5395. if(*ppPrivateData != NULL)
  5396. {
  5397. memset(
  5398. *ppPrivateData,
  5399. 0,
  5400. sizeof(TLSPrivateDataUnion)
  5401. );
  5402. if(*pdwRetDataType == TLS_PRIVATEDATA_SYSTEMLANGID)
  5403. {
  5404. (*ppPrivateData)->systemLangId = GetSystemDefaultLangID();
  5405. }
  5406. else if(*pdwRetDataType == TLS_PRIVATEDATA_SPK)
  5407. {
  5408. (*ppPrivateData)->SPK.cbSPK = cbSource;
  5409. (*ppPrivateData)->SPK.pbSPK = pbSource;
  5410. (*ppPrivateData)->SPK.pCertExtensions = g_pCertExtensions;
  5411. //(*ppPrivateData)->SPK.pCertExtensions = (PTLSCERT_EXTENSIONS)midl_user_allocate(g_cbCertExtensions);
  5412. //memcpy(
  5413. // (*ppPrivateData)->SPK.pCertExtensions,
  5414. // g_pCertExtensions,
  5415. // g_cbCertExtensions
  5416. // );
  5417. }
  5418. else
  5419. {
  5420. (*ppPrivateData)->BinaryData.cbData = cbSource;
  5421. (*ppPrivateData)->BinaryData.pbData = pbSource;
  5422. }
  5423. }
  5424. else
  5425. {
  5426. status = ERROR_OUTOFMEMORY;
  5427. }
  5428. }
  5429. ReleaseAdministrativeLock();
  5430. cleanup:
  5431. lpContext->m_LastError=status;
  5432. InterlockedDecrement( &lpContext->m_RefCount );
  5433. #if DBG
  5434. lpContext->m_LastCall = RPC_CALL_GETPRIVATEDATA;
  5435. #endif
  5436. *pdwErrCode = TLSMapReturnCode(status);
  5437. return RPC_S_OK;
  5438. }
  5439. ////////////////////////////////////////////////////////////////////////////
  5440. error_status_t
  5441. TLSRpcSetTlsPrivateData(
  5442. /* [in] */ PCONTEXT_HANDLE phContext,
  5443. /* [in] */ DWORD dwPrivateDataType,
  5444. /* [switch_is][in] */ PTLSPrivateDataUnion pPrivateData,
  5445. /* [ref][out][in] */ PDWORD pdwErrCode
  5446. )
  5447. /*++
  5448. Abstract:
  5449. Private routine for LrWiz to set license server's private data.
  5450. Parameter:
  5451. phContext: Client context handle.
  5452. dwPrivateDataType : Type of private data to set.
  5453. pPrivateData : Private data to set/install.
  5454. pdwErrCode : Server return code.
  5455. Returns:
  5456. Note:
  5457. Only support installing of SPK/Extension at this time.
  5458. --*/
  5459. {
  5460. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  5461. DWORD status=ERROR_SUCCESS;
  5462. DWORD dwSpkVerifyResult;
  5463. _se_translator_function old_trans_se_func = NULL;
  5464. old_trans_se_func = _set_se_translator( trans_se_func );
  5465. DBGPrintf(
  5466. DBG_INFORMATION,
  5467. DBG_FACILITY_RPC,
  5468. DBGLEVEL_FUNCTION_TRACE,
  5469. _TEXT("%s : TLSRpcDepositeSPK\n"),
  5470. lpContext->m_Client
  5471. );
  5472. InterlockedIncrement( &lpContext->m_RefCount );
  5473. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_LRWIZ))
  5474. {
  5475. status = TLS_E_ACCESS_DENIED;
  5476. goto cleanup;
  5477. }
  5478. //
  5479. // Only support SPK at this time
  5480. //
  5481. if(dwPrivateDataType != TLS_PRIVATEDATA_SPK)
  5482. {
  5483. status = TLS_E_INVALID_DATA;
  5484. goto cleanup;
  5485. }
  5486. //
  5487. // Lock all RPC calls related to issuing certificate
  5488. //
  5489. if(!AcquireRPCExclusiveLock(INFINITE))
  5490. {
  5491. status = TLS_E_ALLOCATE_HANDLE;
  5492. goto cleanup;
  5493. }
  5494. do {
  5495. //if(g_pbServerSPK != NULL && g_cbServerSPK != 0)
  5496. //{
  5497. // status = TLS_E_SPKALREADYEXIST;
  5498. // break;
  5499. //}
  5500. if(AcquireAdministrativeLock(INFINITE))
  5501. {
  5502. try {
  5503. status = TLSReGenSelfSignCert(
  5504. g_hCryptProv,
  5505. pPrivateData->SPK.pbSPK,
  5506. pPrivateData->SPK.cbSPK,
  5507. pPrivateData->SPK.pCertExtensions->cExtension,
  5508. pPrivateData->SPK.pCertExtensions->rgExtension
  5509. );
  5510. }
  5511. catch( SE_Exception e ) {
  5512. status = e.getSeNumber();
  5513. }
  5514. catch(...) {
  5515. status = TLS_E_INTERNAL;
  5516. }
  5517. ReleaseAdministrativeLock();
  5518. }
  5519. else
  5520. {
  5521. status = TLS_E_ALLOCATE_HANDLE;
  5522. }
  5523. } while(FALSE);
  5524. ReleaseRPCExclusiveLock();
  5525. cleanup:
  5526. lpContext->m_LastError=status;
  5527. InterlockedDecrement( &lpContext->m_RefCount );
  5528. #if DBG
  5529. lpContext->m_LastCall = RPC_CALL_SETPRIVATEDATA;
  5530. #endif
  5531. *pdwErrCode = TLSMapReturnCode(status);
  5532. //
  5533. // Reset SE translator
  5534. //
  5535. _set_se_translator(old_trans_se_func);
  5536. return RPC_S_OK;
  5537. }
  5538. ////////////////////////////////////////////////////////////////////////////
  5539. error_status_t
  5540. TLSRpcTriggerReGenKey(
  5541. /* [in] */ PCONTEXT_HANDLE phContext,
  5542. /* [in] */ BOOL bRegenKey,
  5543. /* [ref][out][in] */ PDWORD pdwErrCode
  5544. )
  5545. /*++
  5546. Abstract:
  5547. Private routine to force license server to re-generate its
  5548. public/private key pair, all installed certificates/SPK are
  5549. deleted, User are required to re-register license server.
  5550. Parameters:
  5551. phContext : Client context handle.
  5552. bKeepSPKAndExtension : For future use only.
  5553. pdwErrCode : Return error code.
  5554. Returns:
  5555. ++*/
  5556. {
  5557. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  5558. DWORD status=ERROR_SUCCESS;
  5559. DBGPrintf(
  5560. DBG_INFORMATION,
  5561. DBG_FACILITY_RPC,
  5562. DBGLEVEL_FUNCTION_TRACE,
  5563. _TEXT("%s : TLSRpcTriggerReGenKey\n"),
  5564. lpContext->m_Client
  5565. );
  5566. _se_translator_function old_trans_se_func = NULL;
  5567. old_trans_se_func = _set_se_translator( trans_se_func );
  5568. InterlockedIncrement( &lpContext->m_RefCount );
  5569. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_LRWIZ))
  5570. {
  5571. status = TLS_E_ACCESS_DENIED;
  5572. goto cleanup;
  5573. }
  5574. LPCTSTR pString[1];
  5575. pString[0] = lpContext->m_Client;
  5576. TLSLogEventString(
  5577. EVENTLOG_INFORMATION_TYPE,
  5578. TLS_I_TRIGGER_REGENKEY,
  5579. 1,
  5580. pString
  5581. );
  5582. //
  5583. // Block ALL RPC calls
  5584. //
  5585. if(!AcquireRPCExclusiveLock(INFINITE))
  5586. {
  5587. status=TLS_E_ALLOCATE_HANDLE;
  5588. goto cleanup;
  5589. }
  5590. do {
  5591. if(!AcquireAdministrativeLock(INFINITE))
  5592. {
  5593. status = TLS_E_ALLOCATE_HANDLE;
  5594. break;
  5595. }
  5596. try {
  5597. status = TLSReGenKeysAndReloadServerCert(
  5598. bRegenKey
  5599. );
  5600. }
  5601. catch( SE_Exception e ) {
  5602. status = e.getSeNumber();
  5603. }
  5604. catch(...) {
  5605. status = TLS_E_INTERNAL;
  5606. }
  5607. ReleaseAdministrativeLock();
  5608. } while(FALSE);
  5609. ReleaseRPCExclusiveLock();
  5610. cleanup:
  5611. lpContext->m_LastError=status;
  5612. InterlockedDecrement( &lpContext->m_RefCount );
  5613. #if DBG
  5614. lpContext->m_LastCall = RPC_CALL_TRIGGERREGENKEY;
  5615. #endif
  5616. *pdwErrCode = TLSMapReturnCode(status);
  5617. //
  5618. // Reset SE translator
  5619. //
  5620. _set_se_translator(old_trans_se_func);
  5621. return RPC_S_OK;
  5622. }
  5623. ////////////////////////////////////////////////////////////////////////////
  5624. error_status_t
  5625. TLSRpcTelephoneRegisterLKP(
  5626. /* [in] */ PCONTEXT_HANDLE phContext,
  5627. /* [in] */ DWORD cbData,
  5628. /* [size_is][in] */ PBYTE pbData,
  5629. /* [ref][out] */ PDWORD pdwErrCode
  5630. )
  5631. /*++
  5632. --*/
  5633. {
  5634. DWORD status=ERROR_SUCCESS;
  5635. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  5636. PTLSDbWorkSpace pDbWkSpace;
  5637. LSKeyPack keypack;
  5638. _se_translator_function old_trans_se_func = NULL;
  5639. old_trans_se_func = _set_se_translator( trans_se_func );
  5640. DBGPrintf(
  5641. DBG_INFORMATION,
  5642. DBG_FACILITY_RPC,
  5643. DBGLEVEL_FUNCTION_TRACE,
  5644. _TEXT("%s : TLSRpcTelephoneRegisterLKP\n"),
  5645. lpContext->m_Client
  5646. );
  5647. InterlockedIncrement( &lpContext->m_RefCount );
  5648. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_LRWIZ))
  5649. {
  5650. status = TLS_E_ACCESS_DENIED;
  5651. goto cleanup;
  5652. }
  5653. if(!ALLOCATEDBHANDLE(pDbWkSpace, g_GeneralDbTimeout))
  5654. {
  5655. status=TLS_E_ALLOCATE_HANDLE;
  5656. goto cleanup;
  5657. }
  5658. CLEANUPSTMT;
  5659. BEGIN_TRANSACTION(pDbWkSpace);
  5660. try {
  5661. status = TLSDBTelephoneRegisterLicenseKeyPack(
  5662. USEHANDLE(pDbWkSpace),
  5663. g_pszServerPid,
  5664. pbData,
  5665. cbData,
  5666. &keypack
  5667. );
  5668. }
  5669. catch( SE_Exception e ) {
  5670. status = e.getSeNumber();
  5671. }
  5672. catch(...) {
  5673. status = TLS_E_INTERNAL;
  5674. }
  5675. if(TLS_ERROR(status))
  5676. {
  5677. ROLLBACK_TRANSACTION(pDbWkSpace);
  5678. }
  5679. else
  5680. {
  5681. COMMIT_TRANSACTION(pDbWkSpace);
  5682. }
  5683. FREEDBHANDLE(pDbWkSpace);
  5684. //
  5685. // Post a sync work object
  5686. //
  5687. if(status == ERROR_SUCCESS)
  5688. {
  5689. if(TLSAnnounceLKPToAllRemoteServer(
  5690. keypack.dwKeyPackId,
  5691. 0
  5692. ) != ERROR_SUCCESS)
  5693. {
  5694. TLSLogWarningEvent(TLS_W_ANNOUNCELKP_FAILED);
  5695. }
  5696. }
  5697. cleanup:
  5698. lpContext->m_LastError=status;
  5699. InterlockedDecrement( &lpContext->m_RefCount );
  5700. #if DBG
  5701. lpContext->m_LastCall = RPC_CALL_TELEPHONEREGISTERLKP;
  5702. #endif
  5703. *pdwErrCode = TLSMapReturnCode(status);
  5704. //
  5705. // Reset SE translator
  5706. //
  5707. _set_se_translator(old_trans_se_func);
  5708. return RPC_S_OK;
  5709. }
  5710. ////////////////////////////////////////////////////////////////////////////
  5711. error_status_t
  5712. TLSRpcAllocateInternetLicense(
  5713. /* [in] */ PCONTEXT_HANDLE phContext,
  5714. /* [in] */ const CHALLENGE_CONTEXT ChallengeContext,
  5715. /* [in] */ const PTLSLICENSEREQUEST pRequest,
  5716. /* [string][in] */ LPTSTR pMachineName,
  5717. /* [string][in] */ LPTSTR pUserName,
  5718. /* [in] */ const DWORD cbChallengeResponse,
  5719. /* [size_is][in] */ const PBYTE pbChallengeResponse,
  5720. /* [out] */ PDWORD pcbLicense,
  5721. /* [size_is][size_is][out] */ BYTE __RPC_FAR *__RPC_FAR *pbLicense,
  5722. /* [ref][out][in] */ PDWORD pdwErrCode
  5723. )
  5724. /*++
  5725. --*/
  5726. {
  5727. return TLSRpcRequestNewLicense(
  5728. phContext,
  5729. ChallengeContext,
  5730. pRequest,
  5731. pMachineName,
  5732. pUserName,
  5733. cbChallengeResponse,
  5734. pbChallengeResponse,
  5735. FALSE,
  5736. pcbLicense,
  5737. pbLicense,
  5738. pdwErrCode
  5739. );
  5740. }
  5741. ////////////////////////////////////////////////////////////////////////////
  5742. error_status_t
  5743. TLSRpcAllocateInternetLicenseEx(
  5744. /* [in] */ PCONTEXT_HANDLE phContext,
  5745. /* [in] */ const CHALLENGE_CONTEXT ChallengeContext,
  5746. /* [in] */ const PTLSLICENSEREQUEST pRequest,
  5747. /* [string][in] */ LPTSTR pMachineName,
  5748. /* [string][in] */ LPTSTR pUserName,
  5749. /* [in] */ const DWORD cbChallengeResponse,
  5750. /* [size_is][in] */ const PBYTE pbChallengeResponse,
  5751. /* [ref][out] */ PTLSInternetLicense pInternetLicense,
  5752. /* [ref][out] */ PDWORD pdwErrCode
  5753. )
  5754. /*++
  5755. --*/
  5756. {
  5757. PBYTE pbLicense = NULL;
  5758. DWORD cbLicense = 0;
  5759. DWORD dwStatus = ERROR_SUCCESS;
  5760. DWORD index = 0;
  5761. PLICENSEDPRODUCT pLicensedProduct = NULL;
  5762. DWORD dwNumLicensedProduct = 0;
  5763. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  5764. if(lpContext == NULL)
  5765. {
  5766. SetLastError(ERROR_INVALID_PARAMETER);
  5767. goto cleanup;
  5768. }
  5769. DBGPrintf(
  5770. DBG_INFORMATION,
  5771. DBG_FACILITY_RPC,
  5772. DBGLEVEL_FUNCTION_TRACE,
  5773. _TEXT("%s : TLSRpcAllocateInternetLicenseEx\n"),
  5774. lpContext->m_Client
  5775. );
  5776. //
  5777. // Internally forward the request.
  5778. //
  5779. dwStatus = TLSRpcAllocateInternetLicense(
  5780. phContext,
  5781. ChallengeContext,
  5782. pRequest,
  5783. pMachineName,
  5784. pUserName,
  5785. cbChallengeResponse,
  5786. pbChallengeResponse,
  5787. &cbLicense,
  5788. &pbLicense,
  5789. pdwErrCode
  5790. );
  5791. if(*pdwErrCode >= LSERVER_ERROR_BASE)
  5792. {
  5793. goto cleanup;
  5794. }
  5795. //
  5796. // decode the license.
  5797. //
  5798. dwStatus = LSVerifyDecodeClientLicense(
  5799. pbLicense,
  5800. cbLicense,
  5801. g_pbSecretKey,
  5802. g_cbSecretKey,
  5803. &dwNumLicensedProduct,
  5804. pLicensedProduct
  5805. );
  5806. //
  5807. // Internet license can only have one licensed product
  5808. //
  5809. if(dwStatus != LICENSE_STATUS_OK || dwNumLicensedProduct == 0 || dwNumLicensedProduct > 1)
  5810. {
  5811. dwStatus = TLS_E_INTERNAL;
  5812. goto cleanup;
  5813. }
  5814. pLicensedProduct = (PLICENSEDPRODUCT)AllocateMemory(
  5815. dwNumLicensedProduct * sizeof(LICENSEDPRODUCT)
  5816. );
  5817. if(pLicensedProduct == NULL)
  5818. {
  5819. dwStatus = TLS_E_ALLOCATE_MEMORY;
  5820. goto cleanup;
  5821. }
  5822. dwStatus = LSVerifyDecodeClientLicense(
  5823. pbLicense,
  5824. cbLicense,
  5825. g_pbSecretKey,
  5826. g_cbSecretKey,
  5827. &dwNumLicensedProduct,
  5828. pLicensedProduct
  5829. );
  5830. if(dwStatus != LICENSE_STATUS_OK)
  5831. {
  5832. dwStatus = TLS_E_INTERNAL;
  5833. goto cleanup;
  5834. }
  5835. //
  5836. // Sets up returns.
  5837. //
  5838. SAFESTRCPY(pInternetLicense->szServerId, pLicensedProduct->szIssuerId);
  5839. SAFESTRCPY(pInternetLicense->szServerName, pLicensedProduct->szIssuer);
  5840. pInternetLicense->ulSerialNumber = pLicensedProduct->ulSerialNumber;
  5841. pInternetLicense->dwQuantity = pLicensedProduct->dwQuantity;
  5842. cleanup:
  5843. lpContext->m_LastError=dwStatus;
  5844. InterlockedDecrement( &lpContext->m_RefCount );
  5845. #if DBG
  5846. lpContext->m_LastCall = RPC_CALL_ALLOCATEINTERNETLICNESEEX;
  5847. #endif
  5848. if(*pdwErrCode == ERROR_SUCCESS)
  5849. {
  5850. *pdwErrCode = TLSMapReturnCode(dwStatus);
  5851. }
  5852. if(pLicensedProduct != NULL)
  5853. {
  5854. for(index =0; index < dwNumLicensedProduct; index++)
  5855. {
  5856. LSFreeLicensedProduct(pLicensedProduct+index);
  5857. }
  5858. FreeMemory(pLicensedProduct);
  5859. }
  5860. if(pbLicense != NULL)
  5861. {
  5862. midl_user_free(pbLicense);
  5863. }
  5864. return RPC_S_OK;
  5865. }
  5866. ////////////////////////////////////////////////////////////////////////////
  5867. error_status_t
  5868. TLSRpcReturnInternetLicenseEx(
  5869. /* [in] */ PCONTEXT_HANDLE phContext,
  5870. /* [in] */ const PTLSLICENSEREQUEST pRequest,
  5871. /* [in] */ const ULARGE_INTEGER __RPC_FAR *pulSerialNumber,
  5872. /* [in] */ DWORD dwQuantity,
  5873. /* [ref][out][in] */ PDWORD pdwErrCode
  5874. )
  5875. /*++
  5876. --*/
  5877. {
  5878. DWORD dwStatus = ERROR_SUCCESS;
  5879. TLSLicenseToBeReturn TobeReturn;
  5880. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  5881. PTLSDbWorkSpace pDbWorkSpace = NULL;
  5882. CTLSPolicy* pPolicy = NULL;
  5883. PMHANDLE hClient;
  5884. PMLICENSEREQUEST PMLicenseRequest;
  5885. PPMLICENSEREQUEST pAdjustedRequest;
  5886. TCHAR szCompanyName[LSERVER_MAX_STRING_SIZE+2];
  5887. TCHAR szProductId[LSERVER_MAX_STRING_SIZE+2];
  5888. _se_translator_function old_trans_se_func = NULL;
  5889. old_trans_se_func = _set_se_translator( trans_se_func );
  5890. if(VerifyLicenseRequest(pRequest) == FALSE)
  5891. {
  5892. SetLastError(dwStatus = TLS_E_INVALID_DATA);
  5893. goto cleanup;
  5894. }
  5895. if(lpContext == NULL)
  5896. {
  5897. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  5898. goto cleanup;
  5899. }
  5900. DBGPrintf(
  5901. DBG_INFORMATION,
  5902. DBG_FACILITY_RPC,
  5903. DBGLEVEL_FUNCTION_TRACE,
  5904. _TEXT("%s : TLSRpcReturnInternetLicenseEx\n"),
  5905. lpContext->m_Client
  5906. );
  5907. InterlockedIncrement( &lpContext->m_RefCount );
  5908. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_REQUEST))
  5909. {
  5910. dwStatus = TLS_E_ACCESS_DENIED;
  5911. goto cleanup;
  5912. }
  5913. memset(szCompanyName, 0, sizeof(szCompanyName));
  5914. memset(szProductId, 0, sizeof(szProductId));
  5915. memcpy(
  5916. szCompanyName,
  5917. pRequest->ProductInfo.pbCompanyName,
  5918. min(pRequest->ProductInfo.cbCompanyName, sizeof(szCompanyName)-sizeof(TCHAR))
  5919. );
  5920. memcpy(
  5921. szProductId,
  5922. pRequest->ProductInfo.pbProductID,
  5923. min(pRequest->ProductInfo.cbProductID, sizeof(szProductId)-sizeof(TCHAR))
  5924. );
  5925. //
  5926. // Allocate policy module, must have the right policy module to
  5927. // return license.
  5928. //
  5929. pPolicy = AcquirePolicyModule(
  5930. szCompanyName,
  5931. szProductId,
  5932. TRUE
  5933. );
  5934. if(pPolicy == NULL)
  5935. {
  5936. dwStatus = GetLastError();
  5937. goto cleanup;
  5938. }
  5939. hClient = GenerateClientId();
  5940. //
  5941. // Convert request to PMLICENSEREQUEST
  5942. //
  5943. TlsLicenseRequestToPMLicenseRequest(
  5944. LICENSETYPE_LICENSE,
  5945. pRequest,
  5946. _TEXT(""),
  5947. _TEXT(""),
  5948. 0,
  5949. &PMLicenseRequest
  5950. );
  5951. //
  5952. // Ask policy module the actual product ID
  5953. //
  5954. dwStatus = pPolicy->PMLicenseRequest(
  5955. hClient,
  5956. REQUEST_NEW,
  5957. (PVOID) &PMLicenseRequest,
  5958. (PVOID *) &pAdjustedRequest
  5959. );
  5960. memset(&TobeReturn, 0, sizeof(TobeReturn));
  5961. TobeReturn.dwQuantity = dwQuantity;
  5962. TobeReturn.dwKeyPackId = pulSerialNumber->HighPart;
  5963. TobeReturn.dwLicenseId = pulSerialNumber->LowPart;
  5964. TobeReturn.dwPlatformID = pAdjustedRequest->dwPlatformId;
  5965. TobeReturn.cbEncryptedHwid = pRequest->cbEncryptedHwid;
  5966. TobeReturn.pbEncryptedHwid = pRequest->pbEncryptedHwid;
  5967. TobeReturn.dwProductVersion = pAdjustedRequest->dwProductVersion;
  5968. TobeReturn.pszOrgProductId = szProductId;
  5969. TobeReturn.pszCompanyName = szCompanyName;
  5970. TobeReturn.pszProductId = pAdjustedRequest->pszProductId;
  5971. //
  5972. // Allocate DB handle
  5973. //
  5974. if(!ALLOCATEDBHANDLE(pDbWkSpace, g_GeneralDbTimeout))
  5975. {
  5976. dwStatus = TLS_E_ALLOCATE_HANDLE;
  5977. goto cleanup;
  5978. }
  5979. CLEANUPSTMT;
  5980. BEGIN_TRANSACTION(pDbWorkSpace);
  5981. try {
  5982. dwStatus = TLSReturnClientLicensedProduct(
  5983. USEHANDLE(pDbWkSpace),
  5984. hClient,
  5985. pPolicy,
  5986. &TobeReturn
  5987. );
  5988. }
  5989. catch( SE_Exception e ) {
  5990. dwStatus = e.getSeNumber();
  5991. }
  5992. catch(...) {
  5993. SetLastError(dwStatus = TLS_E_INTERNAL);
  5994. }
  5995. if(TLS_ERROR(dwStatus))
  5996. {
  5997. ROLLBACK_TRANSACTION(pDbWorkSpace);
  5998. }
  5999. else
  6000. {
  6001. COMMIT_TRANSACTION(pDbWorkSpace);
  6002. }
  6003. FREEDBHANDLE(pDbWorkSpace);
  6004. cleanup:
  6005. lpContext->m_LastError=dwStatus;
  6006. InterlockedDecrement( &lpContext->m_RefCount );
  6007. #if DBG
  6008. lpContext->m_LastCall = RPC_CALL_RETURNINTERNETLICENSEEX;
  6009. #endif
  6010. *pdwErrCode = TLSMapReturnCode(dwStatus);
  6011. if(pPolicy)
  6012. {
  6013. pPolicy->PMLicenseRequest(
  6014. hClient,
  6015. REQUEST_COMPLETE,
  6016. UlongToPtr (dwStatus),
  6017. NULL
  6018. );
  6019. ReleasePolicyModule(pPolicy);
  6020. }
  6021. //
  6022. // Reset SE translator
  6023. //
  6024. _set_se_translator(old_trans_se_func);
  6025. return RPC_S_OK;
  6026. }
  6027. ////////////////////////////////////////////////////////////////////////////
  6028. error_status_t
  6029. TLSRpcReturnInternetLicense(
  6030. /* [in] */ PCONTEXT_HANDLE phContext,
  6031. /* [in] */ DWORD cbLicense,
  6032. /* [size_is][in] */ PBYTE pbLicense,
  6033. /* [ref][out][in] */ PDWORD pdwErrCode
  6034. )
  6035. /*++
  6036. --*/
  6037. {
  6038. DWORD dwStatus = ERROR_SUCCESS;
  6039. DWORD index = 0;
  6040. PLICENSEDPRODUCT pLicensedProduct = NULL;
  6041. DWORD dwNumLicensedProduct = 0;
  6042. TLSLicenseToBeReturn TobeReturn;
  6043. LPCLIENTCONTEXT lpContext = (LPCLIENTCONTEXT)phContext;
  6044. PTLSDbWorkSpace pDbWorkSpace = NULL;
  6045. CTLSPolicy* pPolicy = NULL;
  6046. PMHANDLE hClient;
  6047. _se_translator_function old_trans_se_func = NULL;
  6048. old_trans_se_func = _set_se_translator( trans_se_func );
  6049. if(lpContext == NULL)
  6050. {
  6051. SetLastError(dwStatus = ERROR_INVALID_PARAMETER);
  6052. goto cleanup;
  6053. }
  6054. DBGPrintf(
  6055. DBG_INFORMATION,
  6056. DBG_FACILITY_RPC,
  6057. DBGLEVEL_FUNCTION_TRACE,
  6058. _TEXT("%s : TLSRpcReturnInternetLicense\n"),
  6059. lpContext->m_Client
  6060. );
  6061. InterlockedIncrement( &lpContext->m_RefCount );
  6062. if(!(lpContext->m_ClientFlags & CLIENT_ACCESS_REQUEST))
  6063. {
  6064. dwStatus = TLS_E_ACCESS_DENIED;
  6065. goto cleanup;
  6066. }
  6067. // -------------------------------------------------------
  6068. // decode the license.
  6069. // -------------------------------------------------------
  6070. dwStatus = LSVerifyDecodeClientLicense(
  6071. pbLicense,
  6072. cbLicense,
  6073. g_pbSecretKey,
  6074. g_cbSecretKey,
  6075. &dwNumLicensedProduct,
  6076. pLicensedProduct
  6077. );
  6078. // -------------------------------------------------------
  6079. // Internet license can only have one licensed product
  6080. // -------------------------------------------------------
  6081. if(dwStatus != LICENSE_STATUS_OK || dwNumLicensedProduct == 0 || dwNumLicensedProduct > 1)
  6082. {
  6083. dwStatus = TLS_E_INVALID_LICENSE;
  6084. goto cleanup;
  6085. }
  6086. pLicensedProduct = (PLICENSEDPRODUCT)AllocateMemory(
  6087. dwNumLicensedProduct * sizeof(LICENSEDPRODUCT)
  6088. );
  6089. if(pLicensedProduct == NULL)
  6090. {
  6091. dwStatus = TLS_E_ALLOCATE_MEMORY;
  6092. goto cleanup;
  6093. }
  6094. dwStatus = LSVerifyDecodeClientLicense(
  6095. pbLicense,
  6096. cbLicense,
  6097. g_pbSecretKey,
  6098. g_cbSecretKey,
  6099. &dwNumLicensedProduct,
  6100. pLicensedProduct
  6101. );
  6102. if(dwStatus != LICENSE_STATUS_OK)
  6103. {
  6104. dwStatus = TLS_E_INVALID_LICENSE;
  6105. goto cleanup;
  6106. }
  6107. TobeReturn.dwQuantity = pLicensedProduct->dwQuantity;
  6108. TobeReturn.dwKeyPackId = pLicensedProduct->ulSerialNumber.HighPart;
  6109. TobeReturn.dwLicenseId = pLicensedProduct->ulSerialNumber.LowPart;
  6110. TobeReturn.dwPlatformID = pLicensedProduct->LicensedProduct.dwPlatformID;
  6111. TobeReturn.cbEncryptedHwid = pLicensedProduct->LicensedProduct.cbEncryptedHwid;
  6112. TobeReturn.pbEncryptedHwid = pLicensedProduct->LicensedProduct.pbEncryptedHwid;
  6113. TobeReturn.dwProductVersion = MAKELONG(
  6114. pLicensedProduct->pLicensedVersion->wMinorVersion,
  6115. pLicensedProduct->pLicensedVersion->wMajorVersion
  6116. );
  6117. TobeReturn.pszOrgProductId = (LPTSTR) pLicensedProduct->pbOrgProductID;
  6118. TobeReturn.pszCompanyName = (LPTSTR) pLicensedProduct->LicensedProduct.pProductInfo->pbCompanyName;
  6119. TobeReturn.pszProductId = (LPTSTR) pLicensedProduct->LicensedProduct.pProductInfo->pbProductID;
  6120. TobeReturn.pszUserName = (LPTSTR) pLicensedProduct->szLicensedUser;
  6121. TobeReturn.pszMachineName = pLicensedProduct->szLicensedClient;
  6122. //
  6123. // Allocate policy module, must have the right policy module to
  6124. // return license.
  6125. //
  6126. pPolicy = AcquirePolicyModule(
  6127. TobeReturn.pszCompanyName,
  6128. TobeReturn.pszOrgProductId,
  6129. TRUE
  6130. );
  6131. if(pPolicy == NULL)
  6132. {
  6133. dwStatus = GetLastError();
  6134. goto cleanup;
  6135. }
  6136. hClient = GenerateClientId();
  6137. //
  6138. // Allocate DB handle
  6139. //
  6140. if(!ALLOCATEDBHANDLE(pDbWkSpace, g_GeneralDbTimeout))
  6141. {
  6142. dwStatus = TLS_E_ALLOCATE_HANDLE;
  6143. goto cleanup;
  6144. }
  6145. CLEANUPSTMT;
  6146. BEGIN_TRANSACTION(pDbWorkSpace);
  6147. try {
  6148. dwStatus = TLSReturnClientLicensedProduct(
  6149. USEHANDLE(pDbWkSpace),
  6150. hClient,
  6151. pPolicy,
  6152. &TobeReturn
  6153. );
  6154. }
  6155. catch( SE_Exception e ) {
  6156. dwStatus = e.getSeNumber();
  6157. }
  6158. catch(...) {
  6159. SetLastError(dwStatus = TLS_E_INTERNAL);
  6160. }
  6161. if(TLS_ERROR(dwStatus))
  6162. {
  6163. ROLLBACK_TRANSACTION(pDbWorkSpace);
  6164. }
  6165. else
  6166. {
  6167. COMMIT_TRANSACTION(pDbWorkSpace);
  6168. }
  6169. FREEDBHANDLE(pDbWorkSpace);
  6170. cleanup:
  6171. lpContext->m_LastError=dwStatus;
  6172. InterlockedDecrement( &lpContext->m_RefCount );
  6173. #if DBG
  6174. lpContext->m_LastCall = RPC_CALL_RETURNINTERNETLICENSE;
  6175. #endif
  6176. *pdwErrCode = TLSMapReturnCode(dwStatus);
  6177. if(pLicensedProduct != NULL)
  6178. {
  6179. for(index =0; index < dwNumLicensedProduct; index++)
  6180. {
  6181. LSFreeLicensedProduct(pLicensedProduct+index);
  6182. }
  6183. FreeMemory(pLicensedProduct);
  6184. }
  6185. if(pPolicy)
  6186. {
  6187. pPolicy->PMLicenseRequest(
  6188. hClient,
  6189. REQUEST_COMPLETE,
  6190. UlongToPtr (dwStatus),
  6191. NULL
  6192. );
  6193. ReleasePolicyModule(pPolicy);
  6194. }
  6195. //
  6196. // Reset SE translator
  6197. //
  6198. _set_se_translator(old_trans_se_func);
  6199. return RPC_S_OK;
  6200. }