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.

286 lines
5.7 KiB

  1. #if 0
  2. //++++++++++++++++++++++++++++++++++
  3. #include <sspi.h>
  4. #include <secext.h>
  5. void PrintIdentity();
  6. SidHandle MakeSessionUserSid( unsigned session );
  7. BOOL
  8. GetUserToken(
  9. ULONG LogonId,
  10. PHANDLE pUserToken
  11. );
  12. HANDLE
  13. MakeToken(
  14. wchar_t Domain[],
  15. wchar_t User[],
  16. wchar_t Password[]
  17. );
  18. void TestImpersonationObjects()
  19. {
  20. HANDLE token;
  21. SidHandle sid;
  22. PrintIdentity();
  23. {
  24. sid = MakeSessionUserSid( 0 );
  25. CNestedImpersonation imp( sid );
  26. // verify that ID changed to the logged-on user
  27. //
  28. PrintIdentity();
  29. token = MakeToken( NULL, L"u2", L"Test222" );
  30. {
  31. CNestedImpersonation imp( token );
  32. // verify that ID changed to the token
  33. //
  34. PrintIdentity();
  35. DbgPrint("end of u2 scope\n");
  36. }
  37. // verify that ID reverted to previous impersonation
  38. //
  39. PrintIdentity();
  40. DbgPrint("end of u1 scope\n");
  41. }
  42. // verify that ID reverted to non-impersonated ID
  43. //
  44. PrintIdentity();
  45. {
  46. token = MakeToken( NULL, L"u1", L"Test111" );
  47. CNestedImpersonation imp( token );
  48. // verify that ID changed to the token
  49. //
  50. PrintIdentity();
  51. token = MakeToken( NULL, L"u2", L"Test222" );
  52. {
  53. CNestedImpersonation imp( token );
  54. // verify that ID changed to the token
  55. //
  56. PrintIdentity();
  57. DbgPrint("end of u2 scope\n");
  58. }
  59. // verify that ID reverted to previous impersonation
  60. //
  61. PrintIdentity();
  62. DbgPrint("end of u1 scope\n");
  63. }
  64. }
  65. HANDLE
  66. MakeToken(
  67. wchar_t Domain[],
  68. wchar_t User[],
  69. wchar_t Password[]
  70. )
  71. {
  72. HANDLE Token;
  73. if (!LogonUser( User,
  74. Domain,
  75. Password,
  76. LOGON32_LOGON_INTERACTIVE,
  77. LOGON32_PROVIDER_DEFAULT,
  78. &Token))
  79. {
  80. DbgPrint("ERROR: LogonUser failed with %d\n", GetLastError() );
  81. exit(1);
  82. }
  83. DbgPrint("made token for %S\n", User);
  84. return Token;
  85. }
  86. SidHandle MakeSessionUserSid( unsigned session )
  87. {
  88. HANDLE token;
  89. if (!GetUserToken( session, &token ))
  90. {
  91. DbgPrint("ERROR: GetUserToken failed with %d\n", GetLastError() );
  92. }
  93. SidHandle sid = CopyTokenSid( token );
  94. wchar_t buf[1000];
  95. if (!SidToString( sid.get(), buf, sizeof(buf)))
  96. {
  97. DbgPrint("ERROR: unable to print the sid\n");
  98. exit(1);
  99. }
  100. DbgPrint("made sid handle for %S\n", buf);
  101. return sid;
  102. }
  103. void PrintIdentity()
  104. {
  105. ULONG size;
  106. wchar_t buf[1000];
  107. size = sizeof(buf)/sizeof(buf[0]);
  108. if (!GetUserNameEx( NameSamCompatible,
  109. buf,
  110. &size))
  111. {
  112. DbgPrint("ERROR: GetUserNameEx failed with %d\n", GetLastError() );
  113. exit(1);
  114. }
  115. DbgPrint("current identity is %S\n", buf);
  116. }
  117. #endif // 1
  118. #if 0
  119. //++++++++++++++++++++++++++++++++++
  120. #include <sspi.h>
  121. #include <secext.h>
  122. void PrintIdentity();
  123. SidHandle MakeSessionUserSid( unsigned session );
  124. BOOL
  125. GetUserToken(
  126. ULONG LogonId,
  127. PHANDLE pUserToken
  128. );
  129. HANDLE
  130. MakeToken(
  131. wchar_t Domain[],
  132. wchar_t User[],
  133. wchar_t Password[]
  134. );
  135. void TestComImpersonation()
  136. {
  137. HANDLE token;
  138. SidHandle sid;
  139. PrintIdentity();
  140. {
  141. CNestedImpersonation imp;
  142. // verify that ID changed to the logged-on user
  143. //
  144. PrintIdentity();
  145. token = MakeToken( NULL, L"u2", L"Test222" );
  146. {
  147. CNestedImpersonation imp( token );
  148. // verify that ID changed to the token
  149. //
  150. PrintIdentity();
  151. DbgPrint("end of u2 scope\n");
  152. }
  153. // verify that ID reverted to previous impersonation
  154. //
  155. PrintIdentity();
  156. imp.SwitchToLogonToken();
  157. // verify that ID reverted to previous impersonation
  158. //
  159. PrintIdentity();
  160. DbgPrint("end of u1 scope\n");
  161. }
  162. PrintIdentity();
  163. }
  164. HANDLE
  165. MakeToken(
  166. wchar_t Domain[],
  167. wchar_t User[],
  168. wchar_t Password[]
  169. )
  170. {
  171. HANDLE Token;
  172. if (!LogonUser( User,
  173. Domain,
  174. Password,
  175. LOGON32_LOGON_INTERACTIVE,
  176. LOGON32_PROVIDER_DEFAULT,
  177. &Token))
  178. {
  179. DbgPrint("ERROR: LogonUser failed with %d\n", GetLastError() );
  180. exit(1);
  181. }
  182. DbgPrint("made token for %S\n", User);
  183. return Token;
  184. }
  185. SidHandle MakeSessionUserSid( unsigned session )
  186. {
  187. HANDLE token;
  188. if (!GetUserToken( session, &token ))
  189. {
  190. DbgPrint("ERROR: GetUserToken failed with %d\n", GetLastError() );
  191. }
  192. SidHandle sid = CopyTokenSid( token );
  193. wchar_t buf[1000];
  194. if (!SidToString( sid.get(), buf, sizeof(buf)))
  195. {
  196. DbgPrint("ERROR: unable to print the sid\n");
  197. exit(1);
  198. }
  199. DbgPrint("made sid handle for %S\n", buf);
  200. return sid;
  201. }
  202. void PrintIdentity()
  203. {
  204. ULONG size;
  205. wchar_t buf[1000];
  206. size = sizeof(buf)/sizeof(buf[0]);
  207. if (!GetUserNameEx( NameSamCompatible,
  208. buf,
  209. &size))
  210. {
  211. DbgPrint("ERROR: GetUserNameEx failed with %d\n", GetLastError() );
  212. exit(1);
  213. }
  214. DbgPrint("current identity is %S\n", buf);
  215. }
  216. #endif // 1