]>
Commit | Line | Data |
---|---|---|
23c9e63c DD |
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> | |
ad3c7385 | 13 | * and Robert Collins <rbtcollins@hotmail.com> |
23c9e63c DD |
14 | * |
15 | */ | |
16 | ||
c93bc6d0 MB |
17 | #ifndef SETUP_WIN32_H |
18 | #define SETUP_WIN32_H | |
19 | ||
af67048a | 20 | #include <sys/types.h> |
5072c0bb BD |
21 | #include <string> |
22 | ||
7cc4d95e CV |
23 | /* Needed for some buffers etc., to have a useful replacement for MAX_PATH. */ |
24 | #define CYG_PATH_MAX 4096 | |
25 | ||
4875ac88 MB |
26 | /* Any include of <windows.h> should be through this file, which wraps it in |
27 | * various other handling. */ | |
23c9e63c | 28 | |
4875ac88 | 29 | /* Basic Windows features only. */ |
ed96c6da | 30 | #define WIN32_LEAN_AND_MEAN |
46d04e97 | 31 | |
12a7e246 CV |
32 | #undef _WIN32_WINNT |
33 | #define _WIN32_WINNT 0x0a00 | |
34 | ||
666bf37d | 35 | /* libstdc++-v3 _really_ dislikes min & max defined as macros. */ |
587a67fc MB |
36 | /* As of gcc 3.3.1, it defines NOMINMAX itself, so test first, |
37 | * to avoid a redefinition error */ | |
38 | #ifndef NOMINMAX | |
666bf37d | 39 | #define NOMINMAX |
587a67fc | 40 | #endif |
46d04e97 | 41 | |
42c2254e CV |
42 | /* 100ns difference between Windows and UNIX timebase. */ |
43 | #define FACTOR (0x19db1ded53e8000LL) | |
44 | /* # of 100ns intervals per second. */ | |
45 | #define NSPERSEC 10000000LL | |
46 | ||
417e0dce CV |
47 | /* Defining WINBASEAPI is required due the overloading definitions of some |
48 | kernel32 functions in filemanip.cc. */ | |
382cca42 CV |
49 | #define WINBASEAPI |
50 | ||
4875ac88 | 51 | #include <windows.h> |
46d04e97 | 52 | |
4875ac88 MB |
53 | /* FIXME: The use of _access(fname, 0) as an existence check should be |
54 | * replaced with a more readable idiom, and this fragment removed. */ | |
45e01f23 RC |
55 | #ifndef _access |
56 | #define _access access | |
57 | #endif | |
ed96c6da | 58 | |
4d10ec8c CV |
59 | /* When we have to check for a path delimiter, check for both, slash and |
60 | backslash. */ | |
61 | #define isdirsep(ch) \ | |
62 | ({ \ | |
63 | char __c = (ch); \ | |
64 | ((__c) == '/' || (__c) == '\\'); \ | |
65 | }) | |
66 | ||
c4670c8e | 67 | /* Maximum size of a SID. */ |
5072c0bb BD |
68 | #define MAX_SID_LEN 40 |
69 | ||
70 | /* Computes the size of an ACL in relation to the number of ACEs it | |
71 | should contain. */ | |
72 | #define TOKEN_ACL_SIZE(cnt) (sizeof (ACL) + \ | |
73 | (cnt) * (sizeof (ACCESS_ALLOWED_ACE) + MAX_SID_LEN)) | |
74 | ||
26922cd2 | 75 | struct acl_t { |
c26c3f38 CV |
76 | /* Make sure &acl is 4-byte aligned. */ |
77 | ACL acl __attribute__ ((aligned (4))); | |
e44ac577 | 78 | char aclbuf[TOKEN_ACL_SIZE (7)]; |
26922cd2 CV |
79 | }; |
80 | ||
5072c0bb BD |
81 | class SIDWrapper { |
82 | public: | |
83 | SIDWrapper () : value (NULL) {} | |
84 | /* Prevent synthetics. If assignment is needed, this should be | |
85 | refcounting. */ | |
86 | SIDWrapper (SIDWrapper const &); | |
87 | SIDWrapper& operator= (SIDWrapper const &); | |
88 | ~SIDWrapper () { if (value) FreeSid (value); } | |
89 | ||
90 | /* We could look at doing weird typcast overloads here, | |
91 | but manual access is easier for now. */ | |
92 | PSID &theSID () { return value; } | |
93 | PSID const &theSID () const { return value; } | |
94 | private: | |
95 | PSID value; | |
96 | }; | |
97 | ||
98 | class HANDLEWrapper { | |
99 | public: | |
100 | HANDLEWrapper () : value (NULL) {} | |
101 | /* Prevent synthetics. If assignment is needed, we should duphandles, | |
102 | or refcount. */ | |
103 | HANDLEWrapper (HANDLEWrapper const &); | |
104 | HANDLEWrapper& operator= (HANDLEWrapper const &); | |
105 | ~HANDLEWrapper () { if (value) CloseHandle (value); } | |
106 | HANDLE &theHANDLE () { return value; } | |
107 | HANDLE const &theHANDLE () const { return value; } | |
108 | private: | |
109 | HANDLE value; | |
110 | }; | |
111 | ||
5072c0bb BD |
112 | class NTSecurity |
113 | { | |
114 | public: | |
be617b59 | 115 | NTSecurity () : nullSID (), everyOneSID (), administratorsSID (), usersSID (), |
26922cd2 | 116 | cr_ownerSID (), cr_groupSID (), groupSID (NULL), |
be617b59 | 117 | _wellKnownSIDsinitialized (false), token () {} |
5072c0bb BD |
118 | ~NTSecurity() {} |
119 | ||
120 | /* prevent synthetics */ | |
121 | NTSecurity& operator= (NTSecurity const &); | |
122 | NTSecurity (NTSecurity const &); | |
123 | ||
be617b59 CV |
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). */ | |
26922cd2 CV |
127 | PSECURITY_DESCRIPTOR GetPosixPerms (const char *fname, PSID owner_sid, |
128 | PSID group_sid, mode_t mode, | |
129 | SECURITY_DESCRIPTOR &out_sd, acl_t &acl); | |
770e3aed | 130 | void resetPrimaryGroup(); |
d29a864d | 131 | void setAdminGroup (); |
fd1f35c0 | 132 | void initialiseWellKnownSIDs (); |
5518ac1d | 133 | void setDefaultSecurity(bool isAdmin); |
aa09dcbb | 134 | bool isRunAsAdmin (); |
5072c0bb | 135 | private: |
be617b59 CV |
136 | void NoteFailedAPI (const std::string &); |
137 | bool wellKnownSIDsinitialized () const { return _wellKnownSIDsinitialized; } | |
138 | void wellKnownSIDsinitialized (bool b) { _wellKnownSIDsinitialized = b; } | |
5072c0bb | 139 | void setDefaultDACL (); |
be617b59 CV |
140 | void setBackupPrivileges (); |
141 | ||
89cc2408 | 142 | SIDWrapper nullSID, everyOneSID, administratorsSID, usersSID, |
26922cd2 CV |
143 | cr_ownerSID, cr_groupSID; |
144 | struct { | |
145 | TOKEN_USER user; | |
146 | char buf[MAX_SID_LEN]; | |
147 | } ownerSID; | |
148 | PSID groupSID; | |
5072c0bb | 149 | struct { |
d29a864d | 150 | TOKEN_PRIMARY_GROUP pgrp; |
5072c0bb | 151 | char buf[MAX_SID_LEN]; |
d29a864d CV |
152 | } primaryGroupSID; |
153 | ||
154 | bool _wellKnownSIDsinitialized; | |
155 | HANDLEWrapper token; | |
5072c0bb BD |
156 | DWORD size; |
157 | }; | |
158 | ||
b41c2908 CV |
159 | extern NTSecurity nt_sec; |
160 | ||
f26f525f CF |
161 | #undef major |
162 | #undef minor | |
5072c0bb BD |
163 | class VersionInfo |
164 | { | |
165 | public: | |
166 | VersionInfo (); | |
53d33c5d CV |
167 | DWORD major () const { return v.dwMajorVersion; } |
168 | DWORD minor () const { return v.dwMinorVersion; } | |
a70f0d56 | 169 | DWORD buildNumber () const { return v.dwBuildNumber; } |
5072c0bb BD |
170 | private: |
171 | OSVERSIONINFO v; | |
172 | }; | |
173 | ||
174 | VersionInfo& GetVer (); | |
175 | ||
53d33c5d CV |
176 | #define OSMajorVersion() (GetVer ().major ()) |
177 | #define OSMinorVersion() (GetVer ().minor ()) | |
a70f0d56 | 178 | #define OSBuildNumber() (GetVer ().buildNumber ()) |
ad3c7385 | 179 | |
c0fd969d CV |
180 | static inline void |
181 | GetDlgItemRect (HWND h, int item, LPRECT r) | |
182 | { | |
183 | GetWindowRect (GetDlgItem (h, item), r); | |
184 | MapWindowPoints (HWND_DESKTOP, h, (LPPOINT) r, 2); | |
185 | } | |
186 | ||
187 | static inline void | |
188 | SetDlgItemRect (HWND h, int item, LPRECT r) | |
189 | { | |
190 | MoveWindow (GetDlgItem (h, item), r->left, r->top, | |
191 | r->right - r->left, r->bottom - r->top, TRUE); | |
192 | } | |
193 | ||
c93bc6d0 | 194 | #endif /* SETUP_WIN32_H */ |