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.

295 lines
7.7 KiB

  1. // Copyright (c) 1998 - 1999 Microsoft Corporation
  2. #include "stdafx.h"
  3. #include "license.h"
  4. #include "tlsapi.h"
  5. #include "tlsapip.h"
  6. #include "secstore.h"
  7. #define LICENSING_TIME_BOMB L"TIMEBOMB_832cc540-3244-11d2-b416-00c04fa30cc4"
  8. #define RTMLICENSING_TIME_BOMB L"RTMTSTB_832cc540-3244-11d2-b416-00c04fa30cc4"
  9. #define LICENSING_TIME_BOMB_5_0 L"TIMEBOMB_832cc540-3244-11d2-b416-00c04fa30cc4"
  10. #define RTMLICENSING_TIME_BOMB_5_0 L"RTMTSTB_832cc540-3244-11d2-b416-00c04fa30cc4"
  11. // L$ means only readable from the local machine
  12. #define BETA_LICENSING_TIME_BOMB_5_1 L"L$BETA3TIMEBOMB_1320153D-8DA3-4e8e-B27B-0D888223A588"
  13. #define RTM_LICENSING_TIME_BOMB_5_1 L"L$RTMTIMEBOMB_1320153D-8DA3-4e8e-B27B-0D888223A588"
  14. #define BETA_LICENSING_TIME_BOMB_LATEST_VERSION BETA_LICENSING_TIME_BOMB_5_1
  15. #define RTM_LICENSING_TIME_BOMB_LATEST_VERSION RTM_LICENSING_TIME_BOMB_5_1
  16. #define X509_CERT_PRIVATE_KEY_NAME \
  17. L"L$HYDRAENCKEY_dd2d98db-2316-11d2-b414-00c04fa30cc4"
  18. ///////////////////////////////////////////////////////////////////
  19. typedef BOOL
  20. (* TLSISBETANTSERVER)();
  21. BOOL FIsBetaSystem ()
  22. {
  23. HMODULE hmodTlsAPI = LoadLibrary( _T("mstlsapi.dll") );
  24. if (hmodTlsAPI)
  25. {
  26. TLSISBETANTSERVER pfnTLSIsBetaNTServer;
  27. BOOL fBeta = FALSE;
  28. pfnTLSIsBetaNTServer = (TLSISBETANTSERVER) GetProcAddress(hmodTlsAPI,"TLSIsBetaNTServer");
  29. if (NULL != pfnTLSIsBetaNTServer)
  30. {
  31. fBeta = pfnTLSIsBetaNTServer();
  32. }
  33. FreeLibrary(hmodTlsAPI);
  34. return fBeta;
  35. }
  36. else
  37. {
  38. return FALSE;
  39. }
  40. }
  41. bool IsBetaSystem ()
  42. {
  43. return (FIsBetaSystem() != 0);
  44. }
  45. bool HasLicenceGracePeriodExpired ()
  46. {
  47. DWORD status;
  48. DWORD cbByte;
  49. PBYTE pbByte = NULL;
  50. LPWSTR szTimeBombKey;
  51. FILETIME timebomb;
  52. FILETIME filetimeCurrent;
  53. DWORD dwVersion;
  54. dwVersion = GetVersion();
  55. if ((dwVersion & 0x80000000)
  56. || (LOBYTE(LOWORD(dwVersion)) <= 4))
  57. {
  58. cout << endl << " Pre-Windows 2000. No grace period";
  59. return false;
  60. }
  61. else if ((LOBYTE(LOWORD(dwVersion)) == 5)
  62. && (HIBYTE(LOWORD(dwVersion)) == 0))
  63. {
  64. // Windows 2000
  65. if (FIsBetaSystem())
  66. szTimeBombKey = LICENSING_TIME_BOMB_5_0;
  67. else
  68. szTimeBombKey = RTMLICENSING_TIME_BOMB_5_0;
  69. }
  70. else if ((LOBYTE(LOWORD(dwVersion)) == 5)
  71. && (HIBYTE(LOWORD(dwVersion)) == 1))
  72. {
  73. // Whistler
  74. if (FIsBetaSystem())
  75. szTimeBombKey = BETA_LICENSING_TIME_BOMB_5_1;
  76. else
  77. szTimeBombKey = RTM_LICENSING_TIME_BOMB_5_1;
  78. }
  79. else
  80. {
  81. cout << endl << " Unknown OS. Assume no grace period";
  82. return false;
  83. }
  84. status = RetrieveKey(
  85. szTimeBombKey,
  86. &pbByte,
  87. &cbByte
  88. );
  89. if(status == ERROR_SUCCESS && pbByte)
  90. {
  91. timebomb = *(FILETIME *)pbByte;
  92. GetSystemTimeAsFileTime(&filetimeCurrent);
  93. // yes license has expired if filetimeCurrent >= timebomb
  94. return (CompareFileTime(&timebomb, &filetimeCurrent) < 1);
  95. }
  96. else
  97. {
  98. // It hasn't been set yet, so we're not expired
  99. return false;
  100. }
  101. }
  102. static gdwCount = 0;
  103. static TCHAR gLicenseServers[1024];
  104. BOOL ServerEnumCallBack(
  105. IN TLS_HANDLE hHandle,
  106. IN LPCTSTR pszServerName,
  107. IN HANDLE /* dwUserData */)
  108. {
  109. USES_CONVERSION;
  110. if (hHandle)
  111. {
  112. _tcscat(gLicenseServers, pszServerName);
  113. cout << endl << " Found #" << ++gdwCount << ":"<< T2A(pszServerName);
  114. }
  115. return 0;
  116. }
  117. typedef HANDLE
  118. (* TLSCONNECTTOANYLSSERVER)(
  119. DWORD dwTimeOut
  120. );
  121. typedef DWORD
  122. (* ENUMERATETLSSERVERNEW)(
  123. TLSENUMERATECALLBACK fCallBack,
  124. HANDLE dwUserData,
  125. DWORD dwTimeOut,
  126. BOOL fRegOnly
  127. );
  128. typedef DWORD
  129. (* ENUMERATETLSSERVEROLD)(
  130. LPCTSTR szDomain,
  131. LPCTSTR szScope,
  132. DWORD dwPlatformType,
  133. TLSENUMERATECALLBACK fCallBack,
  134. HANDLE dwUserData,
  135. DWORD dwTimeOut,
  136. BOOL fRegOnly
  137. );
  138. bool EnumerateLicenseServers ()
  139. {
  140. HMODULE hmodTlsAPI = LoadLibrary( _T("mstlsapi.dll") );
  141. if (hmodTlsAPI)
  142. {
  143. DWORD dwResult;
  144. _tcscat(gLicenseServers, _T(""));
  145. // load TLSShutdown to see if we have the new APIs
  146. if (NULL == GetProcAddress(hmodTlsAPI,"TLSShutdown"))
  147. {
  148. ENUMERATETLSSERVEROLD pfnEnumerateTlsServer;
  149. pfnEnumerateTlsServer = (ENUMERATETLSSERVEROLD) GetProcAddress(hmodTlsAPI,"EnumerateTlsServer");
  150. if (NULL != pfnEnumerateTlsServer)
  151. {
  152. dwResult = pfnEnumerateTlsServer(
  153. NULL,
  154. NULL,
  155. LSKEYPACKPLATFORMTYPE_UNKNOWN,
  156. ServerEnumCallBack,
  157. 0,
  158. 0,
  159. FALSE);
  160. }
  161. else
  162. {
  163. cout << endl << " Failed to GetProcAddress,ErrorCode = " << GetLastError() << endl;
  164. return false;
  165. }
  166. }
  167. else
  168. {
  169. if (NULL != GetProcAddress(hmodTlsAPI,"TLSGetSupportFlags"))
  170. {
  171. // Use newer discovery function
  172. TLSCONNECTTOANYLSSERVER pfnTLSConnectToAnyLsServer;
  173. TLS_HANDLE hServer;
  174. pfnTLSConnectToAnyLsServer = (TLSCONNECTTOANYLSSERVER) GetProcAddress(hmodTlsAPI,"TLSConnectToAnyLsServer");
  175. if (NULL != pfnTLSConnectToAnyLsServer)
  176. {
  177. hServer = pfnTLSConnectToAnyLsServer(INFINITE);
  178. if (NULL != hServer)
  179. {
  180. return true;
  181. }
  182. else
  183. {
  184. return false;
  185. }
  186. }
  187. else
  188. {
  189. cout << endl << " Failed to GetProcAddress TLSConnectToAnyLsServer,ErrorCode = " << GetLastError() << endl;
  190. return FALSE;
  191. }
  192. }
  193. else
  194. {
  195. ENUMERATETLSSERVERNEW pfnEnumerateTlsServer;
  196. pfnEnumerateTlsServer = (ENUMERATETLSSERVERNEW) GetProcAddress(hmodTlsAPI,"EnumerateTlsServer");
  197. if (NULL != pfnEnumerateTlsServer)
  198. {
  199. dwResult = pfnEnumerateTlsServer(
  200. ServerEnumCallBack,
  201. 0,
  202. 0,
  203. FALSE);
  204. }
  205. else
  206. {
  207. cout << endl << " Failed to GetProcAddress,ErrorCode = " << GetLastError() << endl;
  208. return false;
  209. }
  210. }
  211. }
  212. FreeLibrary(hmodTlsAPI);
  213. if (dwResult != NO_ERROR)
  214. cout << endl << " Failed to EnumerateTlsServer,ErrorCode = " << dwResult << endl;
  215. if (gdwCount > 0)
  216. cout << endl;
  217. return gdwCount > 0;
  218. }
  219. else
  220. {
  221. cout << endl << " Failed to load mstlsapi.dll,ErrorCode = " << GetLastError() << endl;
  222. return false;
  223. }
  224. }
  225. TCHAR *GetLicenseServers ()
  226. {
  227. if (EnumerateLicenseServers ())
  228. return gLicenseServers;
  229. else
  230. return TEXT("Failed");
  231. }