blob: 794f27c6deb533c1dbcf3c47e760fa986f58df19 [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// CurrentUser.h
20
21// Helper class providing the session's logged on username, if
22// a user is logged on. Also allows processes running under
23// XP/2K3 etc to masquerade as the logged on user for security
24// purposes
25
26#ifndef __RFB_WIN32_CURRENT_USER_H__
27#define __RFB_WIN32_CURRENT_USER_H__
28
29#include <rfb_win32/Handle.h>
30#include <rfb_win32/Security.h>
31
32namespace rfb {
33
34 namespace win32 {
35
36 // CurrentUserToken
37 // CurrentUserToken is a Handle containing the security token
38 // for the currently logged-on user, or null if no user is
39 // logged on.
40 //
41 // Under Windows 95/98/Me, which don't support security tokens,
42 // the token will be INVALID_HANDLE_VALUE if a user is logged on.
43 //
44 // Under Windows NT/2K, it may be the case that the token is
45 // null even when a user *is* logged on, because we use some hacks
46 // to detect the user's token and sometimes they fail. On these
47 // platforms, isSafe() will return False if the token is null.
48 //
49 // Under Windows XP, etc, isSafe() will always be True, and the token
50 // will always be set to the currently logged on user's token.
51 //
52 // canImpersonate() tests whether there is a user token that is safe
53 // to impersonate.
54 //
55 // noUserLoggedOn() tests whether there is *definitely* no user logged on.
56
57 struct CurrentUserToken : public Handle {
58 CurrentUserToken();
59 bool isSafe() const { return isSafe_; };
60 bool canImpersonate() const { return h && isSafe(); }
61 bool noUserLoggedOn() const { return !h && isSafe(); }
62 private:
63 bool isSafe_;
64 };
65
66 // ImpersonateCurrentUser
67 // Throws an exception on failure.
68 // Succeeds (trivially) if process is not running as service.
69 // Fails if CurrentUserToken is not valid.
70 // Fails if platform is NT AND cannot impersonate token.
71 // Succeeds otherwise.
72
73 struct ImpersonateCurrentUser {
74 ImpersonateCurrentUser();
75 ~ImpersonateCurrentUser();
76 CurrentUserToken token;
77 };
78
79 // UserName
80 // Returns the name of the user the thread is currently running as.
81 // Raises a SystemException in case of error.
82 // NB: Raises a SystemException with err == ERROR_NOT_LOGGED_ON if
83 // running under Windows 9x/95/Me and no user is logged on.
84
85 struct UserName : public TCharArray {
86 UserName();
87 };
88
89 // UserSID
90 // Returns the SID of the currently logged-on user (i.e. the session user)
91
92 struct UserSID : public Sid {
93 UserSID();
94 };
95
96 }
97
98}
99
100#endif