Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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