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.

279 lines
6.6 KiB

  1. #include <windows.h>
  2. #include <shellapi.h>
  3. #include <stdio.h>
  4. #include <tchar.h>
  5. #include <stdlib.h>
  6. #include <rpc.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <crt/io.h>
  10. #include <wincrypt.h>
  11. #include <winsta.h>
  12. #include <license.h>
  13. /*****************************************************************************
  14. *
  15. * MIDL_user_allocate
  16. *
  17. * Handles RPC's allocation of argument data structures
  18. *
  19. * ENTRY:
  20. * Param1 (input/output)
  21. * Comments
  22. *
  23. * EXIT:
  24. * ERROR_SUCCESS - no error
  25. *
  26. ****************************************************************************/
  27. extern "C" void __RPC_FAR * __RPC_USER
  28. midl_user_allocate(
  29. size_t Size
  30. )
  31. {
  32. return( LocalAlloc(LMEM_FIXED,Size) );
  33. }
  34. /*****************************************************************************
  35. *
  36. * MIDL_user_free
  37. *
  38. * Handles RPC's de-allocation of argument data structures
  39. *
  40. * ENTRY:
  41. * Param1 (input/output)
  42. * Comments
  43. *
  44. * EXIT:
  45. * ERROR_SUCCESS - no error
  46. *
  47. ****************************************************************************/
  48. void __RPC_USER
  49. midl_user_free(
  50. void __RPC_FAR *p
  51. )
  52. {
  53. LocalFree( p );
  54. }
  55. void MyReportError(DWORD errCode)
  56. {
  57. DWORD dwRet;
  58. LPTSTR lpszTemp = NULL;
  59. dwRet=FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  60. NULL,
  61. errCode,
  62. LANG_NEUTRAL,
  63. (LPTSTR)&lpszTemp,
  64. 0,
  65. NULL);
  66. if(dwRet != 0)
  67. {
  68. _tprintf(_TEXT("Error : %s (%d)\n"), lpszTemp, errCode);
  69. if(lpszTemp)
  70. LocalFree((HLOCAL)lpszTemp);
  71. }
  72. return;
  73. }
  74. void Usage(char * szName)
  75. {
  76. wprintf(L"Usage: %S\n"
  77. L"\t-s <mode> - Set licensing mode\n"
  78. L"\t-g - Get licensing mode\n"
  79. L"\t-l - List available licensing modes\n"
  80. L"\t-i <mode> - Get information about licensing mode\n"
  81. L"\n"
  82. L"\t\t0:\tPersonal Terminal Server\n"
  83. L"\t\t1:\tRemote Administration\n"
  84. L"\t\t2:\tPer Seat\n"
  85. L"\t\t3:\tInternet Connector\n"
  86. L"\t\t4:\tPer User\n"
  87. ,
  88. szName
  89. );
  90. return;
  91. }
  92. int __cdecl main(int argc, char *argv[])
  93. {
  94. DWORD dwStatus;
  95. DWORD dwNewStatus;
  96. BOOL fSet = FALSE;
  97. BOOL fGet = FALSE;
  98. BOOL fList = FALSE;
  99. BOOL fInfo = FALSE;
  100. ULONG ulMode;
  101. HANDLE hServer = NULL;
  102. BOOL fRet;
  103. ULONG *pulPolicyIds = NULL;
  104. ULONG cPolicies;
  105. ULONG ulInfoStructVersion = LCPOLICYINFOTYPE_CURRENT;
  106. LPLCPOLICYINFO_V1W pPolicyInfo = NULL;
  107. if (argc < 2)
  108. {
  109. Usage(argv[0]);
  110. goto cleanup;
  111. }
  112. if ((argv[1][1] == 's') || (argv[1][1] == 'S'))
  113. {
  114. if (argc < 3)
  115. {
  116. Usage(argv[0]);
  117. goto cleanup;
  118. }
  119. ulMode = atol(argv[2]);
  120. fSet = TRUE;
  121. }
  122. else if ((argv[1][1] == 'i') || (argv[1][1] == 'I'))
  123. {
  124. if (argc < 3)
  125. {
  126. Usage(argv[0]);
  127. goto cleanup;
  128. }
  129. ulMode = atol(argv[2]);
  130. fInfo = TRUE;
  131. }
  132. else if ((argv[1][1] == 'g') || (argv[1][1] == 'G'))
  133. {
  134. fGet = TRUE;
  135. }
  136. else if ((argv[1][1] == 'l') || (argv[1][1] == 'L'))
  137. {
  138. fList = TRUE;
  139. }
  140. else
  141. {
  142. Usage(argv[0]);
  143. goto cleanup;
  144. }
  145. hServer = ServerLicensingOpen(NULL);
  146. if (NULL == hServer)
  147. {
  148. wprintf(L"Connect to server failed\n");
  149. MyReportError(GetLastError());
  150. goto cleanup;
  151. }
  152. if (fGet)
  153. {
  154. fRet = ServerLicensingGetPolicy(
  155. hServer,
  156. &ulMode
  157. );
  158. wprintf(L"Get Mode\n");
  159. if (fRet)
  160. {
  161. wprintf(L"Mode: %d\n"
  162. ,
  163. ulMode
  164. );
  165. }
  166. else
  167. {
  168. wprintf(L"Failed\n");
  169. MyReportError(GetLastError());
  170. }
  171. }
  172. else if (fList)
  173. {
  174. fRet = ServerLicensingGetAvailablePolicyIds(
  175. hServer,
  176. &pulPolicyIds,
  177. &cPolicies
  178. );
  179. wprintf(L"List Modes\n");
  180. if (fRet)
  181. {
  182. wprintf(L"Modes: \n");
  183. for (ULONG i = 0; i < cPolicies; i++)
  184. {
  185. wprintf(L"%d "
  186. ,
  187. pulPolicyIds[i]
  188. );
  189. }
  190. wprintf(L"\n");
  191. MIDL_user_free(pulPolicyIds);
  192. }
  193. else
  194. {
  195. wprintf(L"Failed\n");
  196. MyReportError(GetLastError());
  197. }
  198. }
  199. else if (fSet)
  200. {
  201. dwStatus = ServerLicensingSetPolicy(hServer,
  202. ulMode,
  203. &dwNewStatus);
  204. wprintf(L"Set Mode\n"
  205. L"RPC Status: %d\n"
  206. L"New Mode Status: %d\n"
  207. ,
  208. dwStatus,
  209. dwNewStatus
  210. );
  211. if (ERROR_SUCCESS != dwStatus)
  212. MyReportError(dwStatus);
  213. else if (ERROR_SUCCESS != dwNewStatus)
  214. MyReportError(dwNewStatus);
  215. }
  216. else if (fInfo)
  217. {
  218. fRet = ServerLicensingGetPolicyInformation(
  219. hServer,
  220. ulMode,
  221. &ulInfoStructVersion,
  222. (LPLCPOLICYINFOGENERIC *) &pPolicyInfo
  223. );
  224. if (fRet)
  225. {
  226. wprintf(L"Get Mode Info\n"
  227. L"Name: %s\n"
  228. L"Description: %s\n"
  229. ,
  230. pPolicyInfo->lpPolicyName,
  231. pPolicyInfo->lpPolicyDescription
  232. );
  233. ServerLicensingFreePolicyInformation((LPLCPOLICYINFOGENERIC *)&pPolicyInfo);
  234. }
  235. else
  236. {
  237. wprintf(L"Failed\n");
  238. MyReportError(GetLastError());
  239. }
  240. }
  241. ServerLicensingClose(hServer);
  242. cleanup:
  243. return 0;
  244. }