]> cygwin.com Git - cygwin-apps/setup.git/blob - win32.h
* win32.h (NTSecurity::primaryGroupSID): Convert to a structure for
[cygwin-apps/setup.git] / win32.h
1 /*
2 * Copyright (c) 2000, Red Hat, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * Written by DJ Delorie <dj@cygnus.com>
13 * and Robert Collins <rbtcollins@hotmail.com>
14 *
15 */
16
17 #ifndef SETUP_WIN32_H
18 #define SETUP_WIN32_H
19
20 #include <string>
21
22 /* Any include of <windows.h> should be through this file, which wraps it in
23 * various other handling. */
24
25 /* Basic Windows features only. */
26 #define WIN32_LEAN_AND_MEAN
27
28 /* libstdc++-v3 _really_ dislikes min & max defined as macros. */
29 /* As of gcc 3.3.1, it defines NOMINMAX itself, so test first,
30 * to avoid a redefinition error */
31 #ifndef NOMINMAX
32 #define NOMINMAX
33 #endif
34
35 /* In w32api 3.1, __declspec(dllimport) decoration is added to
36 * certain symbols. This breaks our autoload mechanism - the symptom is
37 * multiple declaration errors at link time. This define turns that off again.
38 * It will default to off again in later w32api versions, but we need to work
39 * with 3.1 for now. */
40 #define WINBASEAPI
41
42 /* Require at least Internet Explorer 3, in order to have access to
43 * sufficient Windows Common Controls features from <commctrl.h> . */
44 #define _WIN32_IE 0x0300
45
46 #include <windows.h>
47
48 /* FIXME: The use of _access(fname, 0) as an existence check should be
49 * replaced with a more readable idiom, and this fragment removed. */
50 #ifndef _access
51 #define _access access
52 #endif
53
54 /* Maximum size of a SID on NT/W2K. */
55 #define MAX_SID_LEN 40
56
57 /* Computes the size of an ACL in relation to the number of ACEs it
58 should contain. */
59 #define TOKEN_ACL_SIZE(cnt) (sizeof (ACL) + \
60 (cnt) * (sizeof (ACCESS_ALLOWED_ACE) + MAX_SID_LEN))
61
62 class SIDWrapper {
63 public:
64 SIDWrapper () : value (NULL) {}
65 /* Prevent synthetics. If assignment is needed, this should be
66 refcounting. */
67 SIDWrapper (SIDWrapper const &);
68 SIDWrapper& operator= (SIDWrapper const &);
69 ~SIDWrapper () { if (value) FreeSid (value); }
70
71 /* We could look at doing weird typcast overloads here,
72 but manual access is easier for now. */
73 PSID &theSID () { return value; }
74 PSID const &theSID () const { return value; }
75 private:
76 PSID value;
77 };
78
79 class HANDLEWrapper {
80 public:
81 HANDLEWrapper () : value (NULL) {}
82 /* Prevent synthetics. If assignment is needed, we should duphandles,
83 or refcount. */
84 HANDLEWrapper (HANDLEWrapper const &);
85 HANDLEWrapper& operator= (HANDLEWrapper const &);
86 ~HANDLEWrapper () { if (value) CloseHandle (value); }
87 HANDLE &theHANDLE () { return value; }
88 HANDLE const &theHANDLE () const { return value; }
89 private:
90 HANDLE value;
91 };
92
93 class TokenGroupCollection {
94 public:
95 TokenGroupCollection (DWORD aSize, HANDLEWrapper &aHandle) :
96 populated_(false), buffer(new char[aSize]),
97 bufferSize(aSize), token(aHandle) {}
98 ~TokenGroupCollection () { if (buffer) delete[] buffer; }
99
100 /* prevent synthetics */
101 TokenGroupCollection& operator= (TokenGroupCollection const &);
102 TokenGroupCollection (TokenGroupCollection const &);
103 bool find (SIDWrapper const &) const;
104 bool populated() const { return populated_; }
105 void populate();
106 private:
107 mutable bool populated_;
108 char *buffer;
109 DWORD bufferSize;
110 HANDLEWrapper &token;
111 };
112
113 class NTSecurity
114 {
115 public:
116 NTSecurity () : nullSID (), everyOneSID (), administratorsSID (), usersSID (),
117 _wellKnownSIDsinitialized (false), token () {}
118 ~NTSecurity() {}
119
120 /* prevent synthetics */
121 NTSecurity& operator= (NTSecurity const &);
122 NTSecurity (NTSecurity const &);
123
124 /* Set POSIX-like permissions on files. The fname is only used for printing
125 log output. The function requires an open HANDLE with sufficient
126 permissions (READ_DAC | WRITE_DAC). */
127 void SetPosixPerms (const char *fname, HANDLE fh, mode_t mode);
128 void resetPrimaryGroup();
129 void setAdminGroup ();
130 void setDefaultSecurity();
131 private:
132 void NoteFailedAPI (const std::string &);
133 bool wellKnownSIDsinitialized () const { return _wellKnownSIDsinitialized; }
134 void wellKnownSIDsinitialized (bool b) { _wellKnownSIDsinitialized = b; }
135 void initialiseWellKnownSIDs ();
136 void setDefaultDACL ();
137 void setBackupPrivileges ();
138
139 SIDWrapper nullSID, everyOneSID, administratorsSID, usersSID;
140 struct {
141 TOKEN_PRIMARY_GROUP pgrp;
142 char buf[MAX_SID_LEN];
143 } primaryGroupSID;
144
145 bool _wellKnownSIDsinitialized;
146 HANDLEWrapper token;
147 DWORD size;
148 };
149
150 extern NTSecurity nt_sec;
151
152 class VersionInfo
153 {
154 public:
155 VersionInfo ();
156 bool isNT () { return (v.dwPlatformId == VER_PLATFORM_WIN32_NT); }
157 DWORD major () const { return v.dwMajorVersion; }
158 DWORD minor () const { return v.dwMinorVersion; }
159 private:
160 OSVERSIONINFO v;
161 };
162
163 VersionInfo& GetVer ();
164
165 #define IsWindowsNT() (GetVer ().isNT ())
166 #define OSMajorVersion() (GetVer ().major ())
167 #define OSMinorVersion() (GetVer ().minor ())
168
169 #endif /* SETUP_WIN32_H */
This page took 0.043008 seconds and 6 git commands to generate.