Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

689 lines
16 KiB

  1. //Copyright (c) 1998 - 1999 Microsoft Corporation
  2. /*++
  3. Module Name:
  4. LsMgrDoc.cpp
  5. Abstract:
  6. This Module contains the implementation of CLicMgrDoc class
  7. (The Document class)
  8. Author:
  9. Arathi Kundapur (v-akunda) 11-Feb-1998
  10. Revision History:
  11. --*/
  12. #include "stdafx.h"
  13. #include "defines.h"
  14. #include "LicMgr.h"
  15. #include "LSMgrDoc.h"
  16. #include "LSServer.h"
  17. #include "MainFrm.h"
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CLicMgrDoc
  25. IMPLEMENT_DYNCREATE(CLicMgrDoc, CDocument)
  26. BEGIN_MESSAGE_MAP(CLicMgrDoc, CDocument)
  27. //{{AFX_MSG_MAP(CLicMgrDoc)
  28. // NOTE - the ClassWizard will add and remove mapping macros here.
  29. // DO NOT EDIT what you see in these blocks of generated code!
  30. //}}AFX_MSG_MAP
  31. END_MESSAGE_MAP()
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CLicMgrDoc construction/destruction
  34. CLicMgrDoc::CLicMgrDoc()
  35. {
  36. // TODO: add one-time construction code here
  37. m_NodeType = NODE_NONE;
  38. m_pAllServers = NULL;
  39. }
  40. CLicMgrDoc::~CLicMgrDoc()
  41. {
  42. if(m_pAllServers)
  43. {
  44. delete m_pAllServers;
  45. m_pAllServers = NULL;
  46. }
  47. }
  48. BOOL CLicMgrDoc::OnNewDocument()
  49. {
  50. if (!CDocument::OnNewDocument())
  51. return FALSE;
  52. // TODO: add reinitialization code here
  53. // (SDI documents will reuse this document)
  54. return TRUE;
  55. }
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CLicMgrDoc serialization
  58. void CLicMgrDoc::Serialize(CArchive& ar)
  59. {
  60. if (ar.IsStoring())
  61. {
  62. // TODO: add storing code here
  63. }
  64. else
  65. {
  66. // TODO: add loading code here
  67. }
  68. }
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CLicMgrDoc diagnostics
  71. #ifdef _DEBUG
  72. void CLicMgrDoc::AssertValid() const
  73. {
  74. CDocument::AssertValid();
  75. }
  76. void CLicMgrDoc::Dump(CDumpContext& dc) const
  77. {
  78. CDocument::Dump(dc);
  79. }
  80. #endif //_DEBUG
  81. HRESULT CLicMgrDoc::EnumerateKeyPacks(CLicServer *pServer,DWORD dwSearchParm,BOOL bMatchAll)
  82. {
  83. ASSERT(pServer);
  84. HRESULT hResult = S_OK;
  85. if(pServer == NULL)
  86. return E_FAIL;
  87. if(TRUE == pServer->IsExpanded())
  88. return ALREADY_EXPANDED;
  89. PCONTEXT_HANDLE hBinding = NULL;
  90. BOOL bContext = FALSE;
  91. RPC_STATUS status;
  92. LSKeyPack keypack;
  93. CString Server;
  94. hBinding = pServer->GetContext();
  95. if(NULL == hBinding)
  96. {
  97. if(pServer->UseIpAddress())
  98. Server = pServer->GetIpAddress();
  99. else
  100. Server = pServer->GetName();
  101. hBinding=ConnectToLsServer(Server.GetBuffer(Server.GetLength()));
  102. if(hBinding == NULL)
  103. {
  104. hResult = CONNECTION_FAILED;
  105. goto cleanup;
  106. }
  107. }
  108. memset(&keypack, 0, sizeof(keypack));
  109. keypack.dwLanguageId = GetUserDefaultUILanguage();
  110. status = LSKeyPackEnumBegin(hBinding, dwSearchParm, bMatchAll, &keypack);
  111. if(status != ERROR_SUCCESS)
  112. {
  113. hResult = status;
  114. goto cleanup;
  115. }
  116. else
  117. {
  118. do
  119. {
  120. status = LSKeyPackEnumNext(hBinding, &keypack);
  121. if(status == ERROR_SUCCESS)
  122. {
  123. DBGMSG( L"LICMGR:CLicMgrDoc::EnumerateKeyPacks - LSKeyPackEnumNext\n" , 0 );
  124. CKeyPack * pKeyPack = new CKeyPack(keypack);
  125. if(pKeyPack == NULL)
  126. {
  127. hResult = E_OUTOFMEMORY;
  128. goto cleanup;
  129. }
  130. pServer->AddKeyPack(pKeyPack);
  131. }
  132. } while(status == ERROR_SUCCESS);
  133. LSKeyPackEnumEnd(hBinding);
  134. pServer->Expand(TRUE);
  135. }
  136. cleanup:
  137. //put cleanup code here.
  138. if(hBinding)
  139. LSDisconnect(&hBinding);
  140. return hResult;
  141. }
  142. HRESULT
  143. CLicMgrDoc::EnumerateLicenses(
  144. CKeyPack *pKeyPack,
  145. DWORD dwSearchParm,
  146. BOOL bMatchAll
  147. )
  148. /*++
  149. --*/
  150. {
  151. ASSERT(pKeyPack);
  152. if(NULL == pKeyPack)
  153. {
  154. return E_FAIL;
  155. }
  156. CLicServer *pServer = pKeyPack->GetServer();
  157. ASSERT(pServer);
  158. if(NULL == pKeyPack)
  159. {
  160. return E_FAIL;
  161. }
  162. HRESULT hResult = S_OK;
  163. if(TRUE == pKeyPack->IsExpanded())
  164. {
  165. return ALREADY_EXPANDED;
  166. }
  167. PCONTEXT_HANDLE hBinding = NULL;
  168. BOOL bContext = FALSE;
  169. DWORD status = ERROR_SUCCESS;
  170. LSLicenseEx sLicense;
  171. CString Server;
  172. hBinding = pServer->GetContext();
  173. if(NULL == hBinding)
  174. {
  175. if(pServer->UseIpAddress())
  176. {
  177. Server = pServer->GetIpAddress();
  178. }
  179. else
  180. {
  181. Server = pServer->GetName();
  182. }
  183. hBinding=ConnectToLsServer(Server.GetBuffer(Server.GetLength()));
  184. if(hBinding == NULL)
  185. {
  186. hResult = CONNECTION_FAILED;
  187. goto cleanup;
  188. }
  189. }
  190. memset(&sLicense, 0, sizeof(LSLicenseEx));
  191. sLicense.dwKeyPackId = pKeyPack->GetKeyPackStruct().dwKeyPackId;
  192. TLSLicenseEnumBegin( hBinding, dwSearchParm,bMatchAll ,(LPLSLicenseSearchParm) &sLicense, &status);
  193. if(status != ERROR_SUCCESS)
  194. {
  195. hResult = status;
  196. goto cleanup;
  197. }
  198. else
  199. {
  200. do {
  201. memset(&sLicense, 0, sizeof(LSLicenseEx));
  202. sLicense.dwKeyPackId = pKeyPack->GetKeyPackStruct().dwKeyPackId;
  203. TLSLicenseEnumNextEx(hBinding,&sLicense,&status);
  204. if(status == ERROR_SUCCESS)
  205. {
  206. CLicense * pLicense = new CLicense(sLicense);
  207. if(NULL == pLicense)
  208. {
  209. hResult = E_OUTOFMEMORY;
  210. goto cleanup;
  211. }
  212. pKeyPack->AddIssuedLicense(pLicense);
  213. }
  214. } while(status == ERROR_SUCCESS);
  215. TLSLicenseEnumEnd(hBinding,&status);
  216. pKeyPack->Expand(TRUE);
  217. }
  218. cleanup:
  219. //put cleanup code here
  220. if(hBinding)
  221. {
  222. LSDisconnect(&hBinding);
  223. }
  224. return hResult;
  225. }
  226. HRESULT
  227. CLicMgrDoc::ConnectToServer(
  228. CString& Server,
  229. CString& Scope,
  230. SERVER_TYPE& ServerType
  231. )
  232. /*++
  233. --*/
  234. {
  235. PCONTEXT_HANDLE hBinding = NULL;
  236. HRESULT hResult = ERROR_SUCCESS;
  237. RPC_STATUS status;
  238. TCHAR szServerScope[LSERVER_MAX_STRING_SIZE];
  239. TCHAR szMachineName[LSERVER_MAX_STRING_SIZE];
  240. ULONG uSize;
  241. DWORD dwVersion = 0;
  242. PBYTE pbData = NULL;
  243. DWORD cbData = 0;
  244. hBinding = ConnectToLsServer(Server.GetBuffer(Server.GetLength()));
  245. if(hBinding == NULL)
  246. {
  247. hResult = E_FAIL;
  248. goto cleanup;
  249. }
  250. uSize = sizeof(szMachineName) / sizeof(szMachineName[0]);
  251. memset(szMachineName, 0, sizeof(szMachineName));
  252. status = LSGetServerName(hBinding,szMachineName,&uSize);
  253. if(status == RPC_S_OK)
  254. {
  255. Server = szMachineName;
  256. }
  257. uSize = sizeof(szServerScope) / sizeof(szServerScope[0]);
  258. memset(szServerScope, 0, sizeof(szServerScope));
  259. status = LSGetServerScope(hBinding, szServerScope,&uSize);
  260. if(status == RPC_S_OK)
  261. {
  262. Scope = szServerScope;
  263. }
  264. else
  265. {
  266. Scope.LoadString(IDS_UNKNOWN);
  267. }
  268. //Get whether this is a TS4 server or TS5Enforced or TS5NonEnforced
  269. //Use TLSGetVersion
  270. status = TLSGetVersion (hBinding, &dwVersion);
  271. if(status == RPC_S_OK)
  272. {
  273. if(dwVersion & 0x40000000)
  274. {
  275. ServerType = SERVER_TS5_ENFORCED;
  276. }
  277. else
  278. {
  279. ServerType = SERVER_TS5_NONENFORCED;
  280. }
  281. }
  282. else if(status == RPC_S_UNKNOWN_IF)
  283. {
  284. ServerType = SERVER_TS4;
  285. Scope = Server ;
  286. }
  287. else
  288. {
  289. hResult = E_FAIL;
  290. }
  291. cleanup:
  292. if(pbData != NULL)
  293. {
  294. midl_user_free(pbData);
  295. }
  296. if(hBinding)
  297. {
  298. LSDisconnect(&hBinding);
  299. }
  300. return hResult;
  301. }
  302. HRESULT CLicMgrDoc::ConnectWithCurrentParams()
  303. {
  304. CLicMgrApp *pApp = (CLicMgrApp*)AfxGetApp();
  305. CMainFrame * pWnd = (CMainFrame *)pApp->m_pMainWnd ;
  306. HRESULT hResult = ERROR_SUCCESS;
  307. CString Scope;
  308. CString IpAddress;
  309. CString Server = pApp->m_Server;
  310. if(NULL == m_pAllServers)
  311. m_pAllServers = new CAllServers(_T(""));
  312. if(NULL == m_pAllServers)
  313. {
  314. hResult = E_OUTOFMEMORY;
  315. goto cleanup;
  316. }
  317. pWnd->SendMessage(WM_ADD_ALL_SERVERS,0,(LPARAM)m_pAllServers);
  318. if(!Server.IsEmpty())
  319. {
  320. if(TRUE == IsServerInList(Server))
  321. {
  322. hResult = E_DUPLICATE;
  323. }
  324. if( hResult == ERROR_SUCCESS )
  325. {
  326. pWnd->ConnectServer( Server );
  327. }
  328. /* Why did we have this here?
  329. IpAddress = Server;
  330. hResult = ConnectToServer(
  331. Server,
  332. Scope,
  333. ServerType
  334. );
  335. if(ERROR_SUCCESS == hResult)
  336. {
  337. CAllServers * pAllServers = m_pAllServers;
  338. CLicServer *pServer1 = NULL;
  339. if(IpAddress != Server)
  340. {
  341. if(TRUE == IsServerInList(Server))
  342. {
  343. hResult = E_DUPLICATE;
  344. goto cleanup;
  345. }
  346. pServer1 = new CLicServer(Server,ServerType,Scope,IpAddress);
  347. }
  348. else
  349. {
  350. pServer1 = new CLicServer(Server,ServerType,Scope);
  351. }
  352. if(pServer1)
  353. {
  354. pAllServers->AddLicServer(pServer1);
  355. pWnd->SendMessage(WM_ADD_SERVER,0,(LPARAM)pServer1);
  356. }
  357. else
  358. {
  359. return E_OUTOFMEMORY;
  360. }
  361. }
  362. */
  363. }
  364. cleanup:
  365. //Add any cleanup code required here.
  366. return hResult;
  367. }
  368. HRESULT CLicMgrDoc::AddLicenses(PLICSERVER pServer,LPLSKeyPack pKeyPack,UINT nLicenses)
  369. {
  370. //Though the name of the function says add keypack, it adds licenses
  371. HRESULT hResult = E_FAIL;
  372. if(NULL == pServer || NULL == pKeyPack)
  373. return hResult;
  374. if(LSKEYPACKTYPE_TEMPORARY == pKeyPack->ucKeyPackType ||
  375. LSKEYPACKTYPE_SELECT == pKeyPack->ucKeyPackType ||
  376. LSKEYPACKTYPE_FREE == pKeyPack->ucKeyPackType )
  377. {
  378. hResult = ERROR_UNLIMITED_KEYPACK;
  379. return hResult;
  380. }
  381. RPC_STATUS status;
  382. PCONTEXT_HANDLE hBinding = NULL;
  383. LSKeyPack skeypack;
  384. hBinding = pServer->GetContext();
  385. CString Server;
  386. if(NULL == hBinding)
  387. {
  388. if(pServer->UseIpAddress())
  389. Server = pServer->GetIpAddress();
  390. else
  391. Server = pServer->GetName();
  392. hBinding=ConnectToLsServer(Server.GetBuffer(Server.GetLength()));
  393. if(hBinding == NULL)
  394. {
  395. hResult = CONNECTION_FAILED;
  396. goto cleanup;
  397. }
  398. }
  399. skeypack = *pKeyPack;
  400. skeypack.dwTotalLicenseInKeyPack = nLicenses;
  401. skeypack.ucKeyPackStatus = LSKEYPACKSTATUS_ADD_LICENSE;
  402. status=LSKeyPackAdd(hBinding, &skeypack);
  403. if(ERROR_SUCCESS == status)
  404. {
  405. *pKeyPack = skeypack;
  406. }
  407. hResult = status;
  408. cleanup:
  409. if(hBinding)
  410. LSDisconnect(&hBinding);
  411. return hResult;
  412. }
  413. void CLicMgrDoc:: TimeToString(DWORD *ptime, CString& rString)
  414. {
  415. TCHAR m_szTime[MAX_PATH];
  416. time_t time;
  417. rString.Empty();
  418. ASSERT(ptime);
  419. if(NULL == ptime)
  420. return;
  421. //
  422. // Times are stored in the ANSI time_t style in the database,
  423. // however they are type cast to a DWORD (unsigned long). Because
  424. // time_t is 64 bit on a 64 bit machine, and because it is a signed
  425. // value, we must be careful here to make sure that the sign of the
  426. // value is not lost as the value goes from 32 to 64 bit.
  427. //
  428. time = (time_t)(LONG)(*ptime);
  429. LPTSTR lpszTime = NULL;
  430. //Getting the local time as the time is stored as GMT
  431. //in the license server database.
  432. struct tm * pTm = localtime(&time);
  433. if(NULL == pTm)
  434. return;
  435. SYSTEMTIME SystemTime;
  436. SystemTime.wYear = (WORD)(pTm->tm_year + 1900);
  437. SystemTime.wMonth = (WORD)(pTm->tm_mon + 1);
  438. SystemTime.wDayOfWeek = (WORD)pTm->tm_wday;
  439. SystemTime.wDay = (WORD)pTm->tm_mday;
  440. SystemTime.wHour = (WORD)pTm->tm_hour;
  441. SystemTime.wMinute = (WORD)pTm->tm_min;
  442. SystemTime.wSecond = (WORD)pTm->tm_sec;
  443. SystemTime.wMilliseconds = 0;
  444. int RetLen;
  445. TCHAR DateFormat[MAX_PATH];
  446. TCHAR TimeFormat[MAX_PATH];
  447. RetLen = ::GetLocaleInfo(LOCALE_USER_DEFAULT,
  448. LOCALE_SLONGDATE,
  449. DateFormat,
  450. sizeof(DateFormat)/sizeof(TCHAR));
  451. ASSERT(RetLen!=0);
  452. RetLen = ::GetLocaleInfo(LOCALE_USER_DEFAULT,
  453. LOCALE_STIMEFORMAT,
  454. TimeFormat,
  455. sizeof(TimeFormat)/sizeof(TCHAR));
  456. ASSERT(RetLen!=0);
  457. RetLen = ::GetDateFormat(LOCALE_USER_DEFAULT,
  458. 0, /* dwFlag */
  459. &SystemTime,
  460. DateFormat, /* lpFormat */
  461. m_szTime,
  462. sizeof(m_szTime)/sizeof(TCHAR));
  463. if (RetLen == 0)
  464. return;
  465. _tcscat(m_szTime, _T(" ")); /* Separator of date and time */
  466. lpszTime = &m_szTime[lstrlen(m_szTime)];
  467. RetLen = ::GetTimeFormat(LOCALE_USER_DEFAULT,
  468. 0, /* dwFlag */
  469. &SystemTime,
  470. TimeFormat, /* lpFormat */
  471. lpszTime,
  472. sizeof(m_szTime)/sizeof(TCHAR) - lstrlen(m_szTime));
  473. if (RetLen == 0)
  474. return;
  475. rString = m_szTime;
  476. return;
  477. }
  478. BOOL CLicMgrDoc::IsServerInList(CString & Server)
  479. {
  480. ASSERT(m_pAllServers);
  481. if(NULL == m_pAllServers)
  482. return FALSE;
  483. BOOL bServerInList = FALSE;
  484. LicServerList * pServerList = m_pAllServers->GetLicServerList();
  485. //Assumption: ServerName is unique
  486. POSITION pos = pServerList->GetHeadPosition();
  487. while(pos)
  488. {
  489. CLicServer *pLicServer = (CLicServer *)pServerList->GetNext(pos);
  490. ASSERT(pLicServer);
  491. if(NULL == pLicServer)
  492. continue;
  493. if((0 == Server.CompareNoCase(pLicServer->GetName())) || (0 == Server.CompareNoCase(pLicServer->GetIpAddress())))
  494. {
  495. bServerInList = TRUE;
  496. break;
  497. }
  498. }
  499. return bServerInList;
  500. }
  501. HRESULT CLicMgrDoc::RemoveLicenses(PLICSERVER pServer,LPLSKeyPack pKeyPack,UINT nLicenses)
  502. {
  503. HRESULT hResult = E_FAIL;
  504. if(NULL == pServer || NULL == pKeyPack)
  505. return hResult;
  506. if(LSKEYPACKTYPE_TEMPORARY == pKeyPack->ucKeyPackType ||
  507. LSKEYPACKTYPE_SELECT == pKeyPack->ucKeyPackType ||
  508. LSKEYPACKTYPE_FREE == pKeyPack->ucKeyPackType )
  509. {
  510. hResult = ERROR_UNLIMITED_KEYPACK;
  511. return hResult;
  512. }
  513. RPC_STATUS status;
  514. PCONTEXT_HANDLE hBinding = NULL;
  515. LSKeyPack skeypack;
  516. hBinding = pServer->GetContext();
  517. CString Server;
  518. if(NULL == hBinding)
  519. {
  520. if(pServer->UseIpAddress())
  521. Server = pServer->GetIpAddress();
  522. else
  523. Server = pServer->GetName();
  524. hBinding=ConnectToLsServer(Server.GetBuffer(Server.GetLength()));
  525. if(hBinding == NULL)
  526. {
  527. hResult = CONNECTION_FAILED;
  528. goto cleanup;
  529. }
  530. }
  531. skeypack = *pKeyPack;
  532. if(nLicenses > skeypack.dwTotalLicenseInKeyPack)
  533. {
  534. hResult = E_FAIL;
  535. goto cleanup;
  536. }
  537. skeypack.dwTotalLicenseInKeyPack = nLicenses;
  538. skeypack.ucKeyPackStatus = LSKEYPACKSTATUS_REMOVE_LICENSE;
  539. status=LSKeyPackAdd(hBinding, &skeypack);
  540. if(ERROR_SUCCESS == status || LSERVER_I_REMOVE_TOOMANY == status)
  541. {
  542. *pKeyPack = skeypack;
  543. }
  544. hResult = status;
  545. cleanup:
  546. if(hBinding)
  547. LSDisconnect(&hBinding);
  548. return hResult;
  549. }