libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
NTSecuritySettings.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2002 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 #pragma once
20 
21 #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
22 #include<winbase.h>
23 #include<winnt.h>
24 
25 #pragma warning(disable:4290) // "C++ Exception Specification ignored."
26 #pragma warning(disable:4284) // "return type for 'std::auto_ptr<unsigned char>::operator ->' is 'unsigned char *' (ie; not a UDT or reference to a UDT. Will produce errors if applied using infix notation)."
27 
28 namespace jmmcg { namespace NTUtils {
29 
31  public:
32  virtual inline __stdcall ~SecuritySettings(void) noexcept(true) {
33  }
34 
35  inline SecuritySettings & __fastcall operator=(const SecuritySettings &) noexcept(true) {
36  ::InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
37  sa.nLength=sizeof(SECURITY_ATTRIBUTES);
38  sa.lpSecurityDescriptor=&sd;
39  sa.bInheritHandle=true;
40  return *this;
41  }
42 
43  inline const SECURITY_ATTRIBUTES & __fastcall SA(void) const noexcept(true) {
44  return sa;
45  }
46  inline SECURITY_ATTRIBUTES & __fastcall SA(void) noexcept(true) {
47  return sa;
48  }
49  inline const SECURITY_DESCRIPTOR & __fastcall SD(void) const noexcept(true) {
50  return sd;
51  }
52  inline SECURITY_DESCRIPTOR & __fastcall SD(void) noexcept(true) {
53  return sd;
54  }
55 
56  protected:
58 
59  inline __stdcall SecuritySettings(void) {
60  ::InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
61  sa.nLength=sizeof(SECURITY_ATTRIBUTES);
62  sa.lpSecurityDescriptor=&sd;
63  sa.bInheritHandle=true;
64  }
65  inline __stdcall SecuritySettings(const SecuritySettings &) {
66  ::InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
67  sa.nLength=sizeof(SECURITY_ATTRIBUTES);
68  sa.lpSecurityDescriptor=&sd;
69  sa.bInheritHandle=true;
70  }
71 
72  private:
73  SECURITY_ATTRIBUTES sa;
74  };
75 
76 
77 // This class creates a security attributes object with an "everyone" & "full control"
78 // access list.
80  public:
81  inline __stdcall EveryoneSecuritySettings(void) : SecuritySettings() {
82  ::SetSecurityDescriptorDacl(&sd, true, (ACL *)NULL, false);
83  }
85  ::SetSecurityDescriptorDacl(&sd, true, (ACL *)NULL, false);
86  }
87  inline __stdcall ~EveryoneSecuritySettings(void) noexcept(true) {
88  }
89  inline EveryoneSecuritySettings & __fastcall operator=(const EveryoneSecuritySettings &ess) noexcept(true) {
90  static_cast<SecuritySettings &>(*this).operator=(ess);
91  ::SetSecurityDescriptorDacl(&sd, true, (ACL *)NULL, false);
92  return *this;
93  }
94  };
95 
96  class SecurityID {
97  public:
98  inline __stdcall SecurityID(const TCHAR *usr, const TCHAR *mc = NULL)
99  : user(usr), machine(mc ? mc : _T("")) {
100  Create();
101  }
102  inline __stdcall ~SecurityID(void) noexcept(true) {
103  }
104  inline __stdcall SecurityID(const SecurityID &secid)
106  Create();
107  }
108 
109  inline __fastcall operator SID *() noexcept(true) {
110  return reinterpret_cast<SID *>(sid.get());
111  }
112  inline const TCHAR * __fastcall Domain(void) const noexcept(true) {
113  return domain.get();
114  }
115 
116  private:
117  tstring user, machine;
118  std::auto_ptr<BYTE> sid;
120 
121  inline void __fastcall Create(void) {
122  DWORD sid_size=0;
123  DWORD domain_size=0;
124  SID_NAME_USE sid_use;
125  ::LookupAccountName(machine.empty() ? NULL : machine.c_str(), user.c_str(), NULL, &sid_size, NULL, &domain_size, &sid_use);
126  assert(sid_size && domain_size);
127  sid=std::auto_ptr<BYTE>(new BYTE[sid_size]);
128  domain=std::auto_ptr<TCHAR>(new TCHAR[domain_size]);
129  ::LookupAccountName(machine.empty() ? NULL : machine.c_str(), user.c_str(), sid.get(), &sid_size, domain.get(), &domain_size, &sid_use);
130  assert(sid_use!=SidTypeInvalid);
131  }
132 
133  // I don't allow assignment.
134  inline SecurityID & __fastcall operator=(const SecurityID &) noexcept(true);
135  };
136 
137  /**
138  The documentation states that the call to "CoQueryClientBlanket(...)" will only work on Win2K or higher. So on lower versions of NT you'll just get a NULL SID pointer.
139  */
141  public:
142  inline __stdcall CurrentSecurityID(void) {
143 #if(WINVER >= 0x0500)
144  wchar_t *user_name=NULL;
145  const HRESULT hr=::CoQueryClientBlanket(NULL, NULL, NULL, NULL, NULL, reinterpret_cast<void **>(&user_name), NULL);
146  JMMCG_TRACE(_T("CurrentSecurityID::CurrentSecurityID(): ::CoQueryClientBlanket(...) return code: ")<<win_exception::StrFromWinErr(hr));
147  if (user_name) {
148  current_sid=std::auto_ptr<SecurityID>(new SecurityID(NTUtils::WStringToTString(user_name).c_str()));
149  }
150 #endif // (WINVER >= 0x0500)
151  }
152  inline __stdcall ~CurrentSecurityID(void) noexcept(true) {
153  }
154 
155  inline __fastcall operator SID *() noexcept(true) {
156  return reinterpret_cast<SID *>(current_sid.get());
157  }
158 
159  private:
161 
162  // I don't allow copying or assignment.
163  inline __stdcall CurrentSecurityID(const CurrentSecurityID &) noexcept(true);
164  inline CurrentSecurityID & __fastcall operator=(const CurrentSecurityID &) noexcept(true);
165  };
166 
167 } }
168 
169 #pragma warning(default:4290) // "C++ Exception Specification ignored."
170 #pragma warning(default:4284) // "return type for 'std::auto_ptr<unsigned char>::operator ->' is 'unsigned char *' (ie; not a UDT or reference to a UDT. Will produce errors if applied using infix notation)."