1
1
/*-------------------------------------------------------------------------
2
2
*
3
- * security .c
3
+ * win32security .c
4
4
* Microsoft Windows Win32 Security Support Functions
5
5
*
6
6
* Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
7
7
*
8
8
* IDENTIFICATION
9
- * src/backend/ port/win32/security .c
9
+ * src/port/win32security .c
10
10
*
11
11
*-------------------------------------------------------------------------
12
12
*/
13
13
14
+ #ifndef FRONTEND
14
15
#include "postgres.h"
16
+ #else
17
+ #include "postgres_fe.h"
18
+ #endif
15
19
16
20
17
21
static BOOL pgwin32_get_dynamic_tokeninfo (HANDLE token ,
18
- TOKEN_INFORMATION_CLASS class , char * * InfoBuffer ,
19
- char * errbuf , int errsize );
22
+ TOKEN_INFORMATION_CLASS class ,
23
+ char * * InfoBuffer , char * errbuf , int errsize );
24
+
25
+
26
+ /*
27
+ * Utility wrapper for frontend and backend when reporting an error
28
+ * message.
29
+ */
30
+ static
31
+ pg_attribute_printf (1 , 2 )
32
+ void
33
+ log_error (const char * fmt ,...)
34
+ {
35
+ va_list ap ;
36
+
37
+ va_start (fmt , ap );
38
+ #ifndef FRONTEND
39
+ write_stderr (fmt , ap );
40
+ #else
41
+ fprintf (stderr , fmt , ap );
42
+ #endif
43
+ va_end (ap );
44
+ }
20
45
21
46
/*
22
47
* Returns nonzero if the current user has administrative privileges,
@@ -40,15 +65,15 @@ pgwin32_is_admin(void)
40
65
41
66
if (!OpenProcessToken (GetCurrentProcess (), TOKEN_READ , & AccessToken ))
42
67
{
43
- write_stderr ("could not open process token: error code %lu\n" ,
44
- GetLastError ());
68
+ log_error ("could not open process token: error code %lu\n" ,
69
+ GetLastError ());
45
70
exit (1 );
46
71
}
47
72
48
73
if (!pgwin32_get_dynamic_tokeninfo (AccessToken , TokenGroups ,
49
74
& InfoBuffer , errbuf , sizeof (errbuf )))
50
75
{
51
- write_stderr ("%s" , errbuf );
76
+ log_error ("%s" , errbuf );
52
77
exit (1 );
53
78
}
54
79
@@ -57,29 +82,33 @@ pgwin32_is_admin(void)
57
82
CloseHandle (AccessToken );
58
83
59
84
if (!AllocateAndInitializeSid (& NtAuthority , 2 ,
60
- SECURITY_BUILTIN_DOMAIN_RID , DOMAIN_ALIAS_RID_ADMINS , 0 , 0 , 0 , 0 , 0 ,
85
+ SECURITY_BUILTIN_DOMAIN_RID ,
86
+ DOMAIN_ALIAS_RID_ADMINS , 0 , 0 , 0 , 0 , 0 ,
61
87
0 , & AdministratorsSid ))
62
88
{
63
- write_stderr ("could not get SID for Administrators group: error code %lu\n" ,
64
- GetLastError ());
89
+ log_error ("could not get SID for Administrators group: error code %lu\n" ,
90
+ GetLastError ());
65
91
exit (1 );
66
92
}
67
93
68
94
if (!AllocateAndInitializeSid (& NtAuthority , 2 ,
69
- SECURITY_BUILTIN_DOMAIN_RID , DOMAIN_ALIAS_RID_POWER_USERS , 0 , 0 , 0 , 0 , 0 ,
95
+ SECURITY_BUILTIN_DOMAIN_RID ,
96
+ DOMAIN_ALIAS_RID_POWER_USERS , 0 , 0 , 0 , 0 , 0 ,
70
97
0 , & PowerUsersSid ))
71
98
{
72
- write_stderr ("could not get SID for PowerUsers group: error code %lu\n" ,
73
- GetLastError ());
99
+ log_error ("could not get SID for PowerUsers group: error code %lu\n" ,
100
+ GetLastError ());
74
101
exit (1 );
75
102
}
76
103
77
104
success = FALSE;
78
105
79
106
for (x = 0 ; x < Groups -> GroupCount ; x ++ )
80
107
{
81
- if ((EqualSid (AdministratorsSid , Groups -> Groups [x ].Sid ) && (Groups -> Groups [x ].Attributes & SE_GROUP_ENABLED )) ||
82
- (EqualSid (PowerUsersSid , Groups -> Groups [x ].Sid ) && (Groups -> Groups [x ].Attributes & SE_GROUP_ENABLED )))
108
+ if ((EqualSid (AdministratorsSid , Groups -> Groups [x ].Sid ) &&
109
+ (Groups -> Groups [x ].Attributes & SE_GROUP_ENABLED )) ||
110
+ (EqualSid (PowerUsersSid , Groups -> Groups [x ].Sid ) &&
111
+ (Groups -> Groups [x ].Attributes & SE_GROUP_ENABLED )))
83
112
{
84
113
success = TRUE;
85
114
break ;
@@ -105,9 +134,10 @@ pgwin32_is_admin(void)
105
134
* 1 = Service
106
135
* -1 = Error
107
136
*
108
- * Note: we can't report errors via either ereport (we're called too early)
109
- * or write_stderr (because that calls this). We are therefore reduced to
110
- * writing directly on stderr, which sucks, but we have few alternatives.
137
+ * Note: we can't report errors via either ereport (we're called too early
138
+ * in the backend) or write_stderr (because that calls this). We are
139
+ * therefore reduced to writing directly on stderr, which sucks, but we
140
+ * have few alternatives.
111
141
*/
112
142
int
113
143
pgwin32_is_service (void )
@@ -217,29 +247,33 @@ pgwin32_get_dynamic_tokeninfo(HANDLE token, TOKEN_INFORMATION_CLASS class,
217
247
218
248
if (GetTokenInformation (token , class , NULL , 0 , & InfoBufferSize ))
219
249
{
220
- snprintf (errbuf , errsize , "could not get token information: got zero size\n" );
250
+ snprintf (errbuf , errsize ,
251
+ "could not get token information: got zero size\n" );
221
252
return FALSE;
222
253
}
223
254
224
255
if (GetLastError () != ERROR_INSUFFICIENT_BUFFER )
225
256
{
226
- snprintf (errbuf , errsize , "could not get token information: error code %lu\n" ,
257
+ snprintf (errbuf , errsize ,
258
+ "could not get token information: error code %lu\n" ,
227
259
GetLastError ());
228
260
return FALSE;
229
261
}
230
262
231
263
* InfoBuffer = malloc (InfoBufferSize );
232
264
if (* InfoBuffer == NULL )
233
265
{
234
- snprintf (errbuf , errsize , "could not allocate %d bytes for token information\n" ,
266
+ snprintf (errbuf , errsize ,
267
+ "could not allocate %d bytes for token information\n" ,
235
268
(int ) InfoBufferSize );
236
269
return FALSE;
237
270
}
238
271
239
272
if (!GetTokenInformation (token , class , * InfoBuffer ,
240
273
InfoBufferSize , & InfoBufferSize ))
241
274
{
242
- snprintf (errbuf , errsize , "could not get token information: error code %lu\n" ,
275
+ snprintf (errbuf , errsize ,
276
+ "could not get token information: error code %lu\n" ,
243
277
GetLastError ());
244
278
return FALSE;
245
279
}
0 commit comments