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.

122 lines
3.1 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: token.cxx
  8. //
  9. //--------------------------------------------------------------------------
  10. #include <windows.h>
  11. #include <usertok.h>
  12. #include <malloc.h>
  13. extern "C"
  14. {
  15. HANDLE
  16. GetCurrentUserTokenW(
  17. WCHAR Winsta[],
  18. DWORD DesiredAccess
  19. );
  20. HANDLE
  21. GetCurrentUserTokenA(
  22. char Winsta[],
  23. DWORD DesiredAccess
  24. );
  25. }
  26. HANDLE
  27. GetCurrentUserTokenW(
  28. WCHAR Winsta[],
  29. DWORD DesiredAccess
  30. )
  31. {
  32. unsigned long handle = 0;
  33. error_status_t status;
  34. handle_t binding;
  35. //
  36. // Use a dynamic binding - it will pick up the endpoint from the interfaace.
  37. //
  38. status = RpcBindingFromStringBinding(L"ncacn_np:", &binding);
  39. if (status)
  40. {
  41. SetLastError(status);
  42. return 0;
  43. }
  44. RPC_SECURITY_QOS Qos;
  45. Qos.Version = 1;
  46. Qos.Capabilities = RPC_C_QOS_CAPABILITIES_DEFAULT;
  47. Qos.IdentityTracking = RPC_C_QOS_IDENTITY_DYNAMIC;
  48. Qos.ImpersonationType = RPC_C_IMP_LEVEL_IMPERSONATE;
  49. status = RpcBindingSetAuthInfoEx( binding,
  50. NULL,
  51. RPC_C_AUTHN_LEVEL_PKT_INTEGRITY,
  52. RPC_C_AUTHN_WINNT,
  53. NULL, // default credentials
  54. RPC_C_AUTHZ_NONE,
  55. &Qos
  56. );
  57. if (status)
  58. {
  59. RpcBindingFree( &binding );
  60. SetLastError(status);
  61. return 0;
  62. }
  63. status = SecpGetCurrentUserToken( binding, Winsta, GetCurrentProcessId(), &handle, DesiredAccess);
  64. if (status)
  65. {
  66. RpcBindingFree( &binding );
  67. if (status == RPC_S_UNKNOWN_AUTHN_SERVICE ||
  68. status == RPC_S_SERVER_UNAVAILABLE ||
  69. status == RPC_S_CALL_FAILED )
  70. {
  71. status = ERROR_NOT_LOGGED_ON;
  72. }
  73. SetLastError(status);
  74. return 0;
  75. }
  76. RpcBindingFree( &binding );
  77. return ULongToPtr(handle);
  78. }
  79. HANDLE
  80. GetCurrentUserTokenA(
  81. char Winsta[],
  82. DWORD DesiredAccess
  83. )
  84. {
  85. wchar_t * UnicodeWinsta;
  86. unsigned Length;
  87. Length = strlen(Winsta);
  88. UnicodeWinsta = (wchar_t *) _alloca(sizeof(wchar_t) * (Length + 1));
  89. if (!UnicodeWinsta)
  90. {
  91. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  92. return 0;
  93. }
  94. if (MultiByteToWideChar( CP_ACP,
  95. 0, // no special flags
  96. Winsta,
  97. Length,
  98. UnicodeWinsta,
  99. Length+1
  100. ))
  101. {
  102. return 0;
  103. }
  104. return GetCurrentUserTokenW( UnicodeWinsta, DesiredAccess );
  105. }