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.

386 lines
11 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. sspicli.cxx
  6. Abstract:
  7. sspicli
  8. Author:
  9. Larry Zhu (LZhu) Januray 1, 2002
  10. Revision History:
  11. --*/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <rpc.h> // RPC API functions, types
  16. #include <output.hxx>
  17. #include <assert.h>
  18. #include "sspitest.h" // header file generated by MIDL compiler
  19. #ifndef COUNTOF
  20. #define COUNTOF(s) ( sizeof( (s) ) / sizeof( *(s) ) )
  21. #endif // COUNTOF
  22. INT Usage(PSTR pszProgramName)
  23. {
  24. DebugPrintf(SSPI_ERROR, "Usage: %s\n", pszProgramName);
  25. DebugPrintf(SSPI_ERROR, " -protocolsequence protocol_sequence\n");
  26. DebugPrintf(SSPI_ERROR, " -networkaddress network_address\n");
  27. DebugPrintf(SSPI_ERROR, " -delegationaddress delegation address\n");
  28. DebugPrintf(SSPI_ERROR, " -endpoint endpoint\n");
  29. DebugPrintf(SSPI_ERROR, " -networkoptions network options\n");
  30. DebugPrintf(SSPI_ERROR, " -authnlevel authn level\n");
  31. DebugPrintf(SSPI_ERROR, " -imperlevel impersonation level\n");
  32. DebugPrintf(SSPI_ERROR, " -authnservice authn service\n");
  33. DebugPrintf(SSPI_ERROR, " -user username\n");
  34. DebugPrintf(SSPI_ERROR, " -password password\n");
  35. DebugPrintf(SSPI_ERROR, " -domain domain\n");
  36. DebugPrintf(SSPI_ERROR, " -shutdown shutdownserver\n");
  37. DebugPrintf(SSPI_ERROR, " -serviceprincipalname target principal\n");
  38. exit(1);
  39. }
  40. #ifndef UNLEN
  41. #define UNLEN 256
  42. #endif
  43. INT __cdecl
  44. main(
  45. INT argc,
  46. PSTR argv[]
  47. )
  48. {
  49. RPC_STATUS status; // returned by RPC API function
  50. INT mark = 1;
  51. PSTR pszProtocolSequence = "ncacn_ip_tcp";
  52. PSTR pszNetworkAddress = NULL;
  53. PSTR pszEndpoint = "10";
  54. PSTR pszOptions = NULL;
  55. PSTR pszStringBinding = NULL;
  56. PSTR pszDelegationAddress = NULL;
  57. PSTR pszPrincipal = NULL;
  58. CHAR PrincipalBuffer[UNLEN] = {0};
  59. ULONG PrincipalLength;
  60. ULONG AuthnLevel = RPC_C_AUTHN_LEVEL_DEFAULT;
  61. ULONG AuthnService = RPC_C_AUTHN_WINNT;
  62. BOOLEAN ShutdownService = FALSE;
  63. handle_t BindingHandle = NULL;
  64. SEC_WINNT_AUTH_IDENTITY_A sID = {0};
  65. ULONG RootKey = (ULONG) (ULONG_PTR) HKEY_LOCAL_MACHINE;
  66. UCHAR RegValue[MAX_PATH] = {0};
  67. ULONG RegValueType = 0;
  68. ULONG cbRegValue = 0;
  69. RPC_SECURITY_QOS SecurityQos = {0};
  70. PSTR pszKeys[] = {
  71. "SYSTEM\\CurrentControlSet\\Control\\Lsa\\MSV1_0",
  72. "SYSTEM\\CurrentControlSet\\Control\\Lsa\\MSV1_0",
  73. "SYSTEM\\CurrentControlSet\\Control\\Lsa\\MSV1_0"
  74. };
  75. PSTR pszValues[] = {
  76. "ntlmminclientsec",
  77. "ntlmminserversec",
  78. "Auth1"
  79. };
  80. sID.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
  81. SecurityQos.Version = RPC_C_SECURITY_QOS_VERSION;
  82. SecurityQos.IdentityTracking = RPC_C_QOS_IDENTITY_STATIC;
  83. SecurityQos.Capabilities = RPC_C_QOS_CAPABILITIES_DEFAULT;
  84. SecurityQos.ImpersonationType = RPC_C_IMP_LEVEL_IMPERSONATE; // RPC_C_IMP_LEVEL_IDENTIFY;
  85. assert(COUNTOF(pszKeys) == COUNTOF(pszValues));
  86. argc--;
  87. // allow the user to override settings with command line switches
  88. while (argc)
  89. {
  90. if (!strcmp(argv[mark], "-protocolsequence") && (argc > 1))
  91. {
  92. argc--; mark++;
  93. pszProtocolSequence = argv[mark];
  94. argc--; mark++;
  95. }
  96. else if (!strcmp(argv[mark], "-networkaddress") && (argc > 1))
  97. {
  98. argc--; mark++;
  99. pszNetworkAddress = argv[mark];
  100. argc--; mark++;
  101. }
  102. else if (!strcmp(argv[mark], "-delegationaddress") && (argc > 1))
  103. {
  104. argc--; mark++;
  105. pszDelegationAddress = argv[mark];
  106. argc--; mark++;
  107. }
  108. else if (!strcmp(argv[mark], "-endpoint") && (argc > 1))
  109. {
  110. argc--; mark++;
  111. pszEndpoint = argv[mark];
  112. argc--; mark++;
  113. }
  114. else if (!strcmp(argv[mark], "-networkoptions") && (argc > 1))
  115. {
  116. argc--; mark++;
  117. pszOptions = argv[mark];
  118. argc--; mark++;
  119. }
  120. else if (!strcmp(argv[mark], "-serviceprincipalname") && (argc > 1))
  121. {
  122. argc--; mark++;
  123. pszPrincipal = argv[mark];
  124. argc--; mark++;
  125. }
  126. else if (!strcmp(argv[mark], "-user") && (argc > 1))
  127. {
  128. argc--; mark++;
  129. sID.User = (UCHAR*) argv[mark]; sID.UserLength = strlen((PSTR) sID.User);
  130. argc--; mark++;
  131. }
  132. else if (!strcmp(argv[mark], "-domain") && (argc > 1))
  133. {
  134. argc--; mark++;
  135. sID.Domain = (UCHAR*) argv[mark]; sID.DomainLength = strlen((PSTR) sID.Domain);
  136. argc--; mark++;
  137. }
  138. else if (!strcmp(argv[mark], "-password") && (argc > 1))
  139. {
  140. argc--; mark++;
  141. sID.Password = (UCHAR*) argv[mark]; sID.PasswordLength = strlen((PSTR) sID.Password);
  142. argc--; mark++;
  143. }
  144. else if (!strcmp(argv[mark], "-key1") && (argc > 1))
  145. {
  146. argc--; mark++;
  147. pszKeys[0] = argv[mark];
  148. argc--; mark++;
  149. }
  150. else if (!strcmp(argv[mark], "-value1") && (argc > 1))
  151. {
  152. argc--; mark++;
  153. pszValues[0] = argv[mark];
  154. argc--; mark++;
  155. }
  156. else if (!strcmp(argv[mark], "-key2") && (argc > 1))
  157. {
  158. argc--; mark++;
  159. pszKeys[1] = argv[mark];
  160. argc--; mark++;
  161. }
  162. else if (!strcmp(argv[mark], "-value2") && (argc > 1))
  163. {
  164. argc--; mark++;
  165. pszValues[1] = argv[mark];
  166. argc--; mark++;
  167. }
  168. else if (!strcmp(argv[mark], "-key3") && (argc > 1))
  169. {
  170. argc--; mark++;
  171. pszKeys[2] = argv[mark];
  172. argc--; mark++;
  173. }
  174. else if (!strcmp(argv[mark], "-value3") && (argc > 1))
  175. {
  176. argc--; mark++;
  177. pszValues[2] = argv[mark];
  178. argc--; mark++;
  179. }
  180. else if (!strcmp(argv[mark], "-rootkey") && (argc > 1))
  181. {
  182. argc--; mark++;
  183. RootKey = strtol(argv[mark], NULL, 0);
  184. argc--; mark++;
  185. }
  186. else if (!strcmp(argv[mark], "-authnlevel") && (argc > 1))
  187. {
  188. argc--; mark++;
  189. AuthnLevel = strtol(argv[mark], NULL, 0);
  190. argc--; mark++;
  191. }
  192. else if (!strcmp(argv[mark], "-imperlevel") && (argc > 1))
  193. {
  194. argc--; mark++;
  195. SecurityQos.ImpersonationType = strtol(argv[mark], NULL, 0);
  196. argc--; mark++;
  197. }
  198. else if (!strcmp(argv[mark], "-authnservice") && (argc > 1))
  199. {
  200. argc--; mark++;
  201. AuthnService = strtol(argv[mark], NULL, 0);
  202. argc--; mark++;
  203. }
  204. else if (!strcmp(argv[mark], "-shutdown"))
  205. {
  206. argc--; mark++;
  207. ShutdownService = TRUE;
  208. }
  209. else if (!strcmp(argv[mark], "-h") || !strcmp(argv[mark], "-?"))
  210. {
  211. argc--; mark++;
  212. Usage(argv[0]);
  213. }
  214. else
  215. {
  216. Usage(argv[0]);
  217. }
  218. }
  219. //
  220. // If the principal is NULL, get it from the environment
  221. //
  222. if (pszPrincipal == NULL)
  223. {
  224. DebugPrintf(SSPI_WARN, "!!!!!!!!!!!!!!!Targeted service principal name is not provided!!!!!\n");
  225. }
  226. // Use a convenience function to concatenate the elements of
  227. // the string binding into the proper sequence.
  228. status = RpcStringBindingComposeA(NULL,
  229. (UCHAR*) pszProtocolSequence,
  230. (UCHAR*) pszNetworkAddress,
  231. (UCHAR*) pszEndpoint,
  232. (UCHAR*) pszOptions,
  233. (UCHAR**) &pszStringBinding);
  234. if (status)
  235. {
  236. DebugPrintf(SSPI_LOG, "RpcStringBindingCompose returned %d\n", status);
  237. exit(2);
  238. }
  239. DebugPrintf(SSPI_LOG, "pszStringBinding = %s\n", pszStringBinding);
  240. //
  241. // Set the binding handle that will be used to bind to the server.
  242. //
  243. status = RpcBindingFromStringBindingA((UCHAR*) pszStringBinding,
  244. &BindingHandle);
  245. if (status)
  246. {
  247. DebugPrintf(SSPI_LOG, "RpcBindingFromStringBinding returned %d\n", status);
  248. exit(2);
  249. }
  250. status = RpcStringFreeA((UCHAR**) &pszStringBinding); // remote calls done; unbind
  251. if (status)
  252. {
  253. DebugPrintf(SSPI_LOG, "RpcStringFree returned %d\n", status);
  254. exit(2);
  255. }
  256. //
  257. // Tell RPC to do the security thing.
  258. //
  259. DebugPrintf(SSPI_LOG,
  260. "Binding auth info user %s, userlen %#x, domain %s, domanlen %#x, password %s, passwordlen %#x, sid flag %#x, "
  261. "set to level %d, service %d, principal %s, SecurityQos.ImpersonationType %#x\n",
  262. sID.User, sID.UserLength, sID.Domain, sID.DomainLength, sID.Password, sID.PasswordLength, sID.Flags,
  263. AuthnLevel, AuthnService, pszPrincipal, SecurityQos.ImpersonationType);
  264. status = RpcBindingSetAuthInfoExA(BindingHandle,
  265. (UCHAR*) pszPrincipal,
  266. AuthnLevel,
  267. AuthnService,
  268. (sID.UserLength || sID.DomainLength || sID.PasswordLength) ? &sID : NULL,
  269. RPC_C_AUTHZ_NAME,
  270. &SecurityQos);
  271. if ( status )
  272. {
  273. DebugPrintf(SSPI_LOG, "RpcBindingSetAuthInfo returned %ld\n", status);
  274. exit(2);
  275. }
  276. //
  277. // Do the actual RPC calls to the server.
  278. //
  279. RpcTryExcept
  280. {
  281. for (ULONG i = 0; i < COUNTOF(pszKeys); i++)
  282. {
  283. status = ReadRegistryValueData(
  284. BindingHandle,
  285. RootKey,
  286. (UCHAR*) pszKeys[i],
  287. (UCHAR*) pszValues[i],
  288. sizeof(RegValue),
  289. RegValue,
  290. &RegValueType,
  291. &cbRegValue
  292. );
  293. if (status == ERROR_SUCCESS)
  294. {
  295. DebugPrintf(SSPI_LOG, "RootKey %#x, Key %s, Value %s, RegValueType %#x, cbRegValue %#x\n",
  296. RootKey, pszKeys[i], pszValues[i], RegValueType, cbRegValue);
  297. DebugPrintHex(SSPI_LOG, "RegValue", cbRegValue, RegValue);
  298. }
  299. else
  300. {
  301. DebugPrintf(SSPI_ERROR, "RootKey %#x, Key %s, Value %s, status %#x\n",
  302. RootKey, pszKeys[i], pszValues[i], status);
  303. }
  304. }
  305. if (ShutdownService)
  306. {
  307. Shutdown( BindingHandle );
  308. }
  309. }
  310. RpcExcept(EXCEPTION_EXECUTE_HANDLER)
  311. {
  312. DebugPrintf(SSPI_LOG, "Runtime library reported an exception %d\n", RpcExceptionCode());
  313. } RpcEndExcept
  314. // The calls to the remote procedures are complete.
  315. // Free the binding handle
  316. status = RpcBindingFree(&BindingHandle); // remote calls done; unbind
  317. if (status)
  318. {
  319. DebugPrintf(SSPI_LOG, "RpcBindingFree returned %d\n", status);
  320. exit(2);
  321. }
  322. return 0;
  323. }
  324. // ====================================================================
  325. // MIDL allocate and free
  326. // ====================================================================
  327. void __RPC_FAR * __RPC_API MIDL_user_allocate(size_t len)
  328. {
  329. return malloc(len);
  330. }
  331. void __RPC_API MIDL_user_free(void __RPC_FAR * ptr)
  332. {
  333. free(ptr);
  334. }
  335. /* end file helloc.c */