NCBI C++ ToolKit
ncbi_os_mswin_p.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef CORELIB___NCBI_OS_MSWIN_P__HPP
2 #define CORELIB___NCBI_OS_MSWIN_P__HPP
3 
4 /* $Id: ncbi_os_mswin_p.hpp 85116 2019-01-16 14:28:00Z ivanov $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Vladimir Ivanov
30  *
31  *
32  */
33 
34 /// @file ncbi_os_mswin_p.hpp
35 ///
36 /// Defines MS Windows specific private functions and classes.
37 ///
38 
39 #include <ncbiconf.h>
40 #if !defined(NCBI_OS_MSWIN)
41 # error "ncbi_os_mswin_p.hpp can be used on MS Windows platforms only"
42 #endif
43 
45 
46 // Access Control APIs
47 #include <accctrl.h>
48 #include <aclapi.h>
49 #include <Lmcons.h>
50 
51 #include <tlhelp32.h> // CreateToolhelp32Snapshot
52 
53 
55 
56 
57 /////////////////////////////////////////////////////////////////////////////
58 ///
59 /// CWinSecurity --
60 ///
61 /// Utility class with wrappers for MS Windows security functions.
62 
64 {
65 public:
66  /// Get name of the current user.
67  ///
68  /// Retrieves the user name of the current thread.
69  /// This is the name of the user currently logged onto the system.
70  /// @return
71  /// Current user name, or empty string if there was an error.
72  static string GetUserName(void);
73 
74  /// Get owner name of specified system object.
75  ///
76  /// Retrieve the name of the named object owner and the name of the first
77  /// group, which the account belongs to. The obtained group name may
78  /// be an empty string, if we don't have permissions to get it.
79  /// Win32 really does not use groups, but they exist for the sake
80  /// of POSIX compatibility.
81  /// Windows 2000/XP: In addition to looking up for local accounts,
82  /// local domain accounts, and explicitly trusted domain accounts,
83  /// it also can look for any account in any known domain around.
84  /// @param owner
85  /// Pointer to a string to receive an owner name.
86  /// @param group
87  /// Pointer to a string to receive a group name.
88  /// @param uid
89  /// Pointer to an int to receive a (fake) user id.
90  /// @param gid
91  /// Pointer to an int to receive a (fake) group id.
92  /// @return
93  /// TRUE if successful, FALSE otherwise.
94  static bool GetObjectOwner(const string& obj_name, SE_OBJECT_TYPE obj_type,
95  string* owner, string* group,
96  unsigned int* uid = 0, unsigned int* gid = 0);
97 
98  /// Same as GetObjectOwner(objname) but gets the owner/group information
99  /// by an arbitrary handle rather than by name.
100  static bool GetObjectOwner(HANDLE obj_handle, SE_OBJECT_TYPE obj_type,
101  string* owner, string* group,
102  unsigned int* uid = 0, unsigned int* gid = 0);
103 
104 
105  /// Get file owner name.
106  ///
107  /// @sa
108  /// GetObjectOwner, SetFileOwner
109  static bool GetFileOwner(const string& filename,
110  string* owner, string* group,
111  unsigned int* uid = 0, unsigned int* gid = 0)
112  {
113  return GetObjectOwner(filename, SE_FILE_OBJECT, owner, group, uid, gid);
114  }
115 
116 
117  /// Set file object owner.
118  ///
119  /// You should have administrative rights to change an owner.
120  /// Only administrative privileges (Restore and Take Ownership)
121  /// grant rights to change ownership. Without one of the privileges,
122  /// an administrator cannot take ownership of any file or give ownership
123  /// back to the original owner.
124  /// @param filename
125  /// Filename to change the owner of.
126  /// @param owner
127  /// New owner name to set. If specified as empty, then is not changed.
128  /// @param group
129  /// New group name to set. If specified as empty, then is not changed.
130  /// @param uid
131  /// To receive (fake) numeric user id of the prospective owner
132  /// (even if the ownership change was unsuccessful), or 0 if unknown.
133  /// @param gid
134  /// To receive (fake) numeric user id of the prospective group
135  /// (even if the ownership change was unsuccessful), or 0 if unknown.
136  /// @return
137  /// TRUE if successful, FALSE otherwise.
138  /// @sa
139  /// GetFileOwner, SetThreadPrivilege, SetTokenPrivilege
140  static bool SetFileOwner(/* in */ const string& filename,
141  /* in */ const string& owner, const string& group = kEmptyStr,
142  /* out */ unsigned int* uid = 0 , unsigned int* gid = 0);
143 
144 
145  /// Enables or disables privileges in the specified access token.
146  ///
147  /// In most case you should have administrative rights to change
148  /// some privileges.
149  /// @param token
150  /// A handle to the access token that contains the privileges
151  /// to be modified. The handle must have TOKEN_ADJUST_PRIVILEGES
152  /// and TOKEN_QUERY access to the token.
153  /// @param privilege
154  /// Name of privilege to enable/disable.
155  /// @param enable
156  /// TRUE/FALSE, to enable or disable privilege.
157  /// @param prev
158  /// To receive previous state of chnaged privilege (if specified).
159  /// @return
160  /// TRUE if successful, FALSE otherwise.
161  /// @sa
162  /// SetFileOwner, SetThreadPrivilege
163  static bool SetTokenPrivilege(HANDLE token, LPCTSTR privilege,
164  bool enable, bool* prev = 0);
165 
166  /// Enables or disables privileges for the current thread.
167  ///
168  /// In most case you should have administrative rights to change
169  /// some privileges.
170  /// @param privilege
171  /// Name of privilege to enable/disable.
172  /// @param enable
173  /// TRUE/FALSE, to enable or disable privilege.
174  /// @param prev
175  /// To receive previous state of chnaged privilege (if specified).
176  /// @return
177  /// TRUE if successful, FALSE otherwise.
178  /// @sa
179  /// SetFileOwner, SetTokenPrivilege
180  static bool SetThreadPrivilege(LPCTSTR privilege,
181  bool enable, bool* prev = 0);
182 
183  /// Get file access permissions.
184  ///
185  /// The permissions will be taken for current process thread owner only.
186  /// @param strPath
187  /// Path to the file object.
188  /// @param pPermissions
189  /// Pointer to a variable that receives a file access mask.
190  /// See MSDN or WinNT.h for all access rights constants.
191  /// @return
192  /// TRUE if the operation was completed successfully; FALSE, otherwise.
193  static bool GetFilePermissions(const string& path, ACCESS_MASK* permissions);
194 };
195 
196 
197 /////////////////////////////////////////////////////////////////////////////
198 ///
199 /// CWinFeature --
200 ///
201 /// Utility class with wrappers for MS Windows specific features.
202 
204 {
205 public:
206  /// Find process entry information by process identifier (pid).
207  ///
208  /// @param id
209  /// Process identifier to look for.
210  /// @param entry
211  /// Entry to store retrieved information.
212  /// @return
213  /// TRUE if the operation was completed successfully; FALSE, otherwise.
214  static bool FindProcessEntry(DWORD id, PROCESSENTRY32& entry);
215 };
216 
217 
219 
220 
221 #endif /* CORELIB___NCBI_OS_MSWIN_P__HPP */
CWinFeature –.
static bool FindProcessEntry(DWORD id, PROCESSENTRY32 &entry)
Find process entry information by process identifier (pid).
CWinSecurity –.
static bool GetFileOwner(const string &filename, string *owner, string *group, unsigned int *uid=0, unsigned int *gid=0)
Get file owner name.
static bool GetFilePermissions(const string &path, ACCESS_MASK *permissions)
Get file access permissions.
static string GetUserName(void)
Get name of the current user.
static bool SetTokenPrivilege(HANDLE token, LPCTSTR privilege, bool enable, bool *prev=0)
Enables or disables privileges in the specified access token.
static bool SetFileOwner(const string &filename, const string &owner, const string &group=kEmptyStr, unsigned int *uid=0, unsigned int *gid=0)
Set file object owner.
static bool GetObjectOwner(const string &obj_name, SE_OBJECT_TYPE obj_type, string *owner, string *group, unsigned int *uid=0, unsigned int *gid=0)
Get owner name of specified system object.
static bool SetThreadPrivilege(LPCTSTR privilege, bool enable, bool *prev=0)
Enables or disables privileges for the current thread.
static DLIST_TYPE *DLIST_NAME() prev(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:61
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define kEmptyStr
Definition: ncbistr.hpp:123
#define HANDLE
An abstraction for a file handle.
Definition: mdb.c:383
Defines MS Windows specifics for our "C++" code.
Front end for a platform-specific configuration summary.
unsigned int DWORD
Definition: sqltypes.h:98
Modified on Mon Jul 15 05:34:33 2024 by modify_doxy.py rev. 669887