6
6
* Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
7
7
*
8
8
* IDENTIFICATION
9
- * $PostgreSQL: pgsql/src/backend/port/win32/security.c,v 1.6 2004/11/09 13:01:25 petere Exp $
9
+ * $PostgreSQL: pgsql/src/backend/port/win32/security.c,v 1.7 2004/11/16 19:52:22 tgl Exp $
10
10
*
11
11
*-------------------------------------------------------------------------
12
12
*/
13
13
14
14
#include "postgres.h"
15
15
16
16
17
+ static BOOL pgwin32_get_dynamic_tokeninfo (HANDLE token ,
18
+ TOKEN_INFORMATION_CLASS class , char * * InfoBuffer ,
19
+ char * errbuf , int errsize );
20
+
17
21
/*
18
22
* Returns nonzero if the current user has administrative privileges,
19
23
* or zero if not.
@@ -26,8 +30,8 @@ pgwin32_is_admin(void)
26
30
{
27
31
HANDLE AccessToken ;
28
32
char * InfoBuffer = NULL ;
33
+ char errbuf [256 ];
29
34
PTOKEN_GROUPS Groups ;
30
- DWORD InfoBufferSize ;
31
35
PSID AdministratorsSid ;
32
36
PSID PowerUsersSid ;
33
37
SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY };
@@ -41,36 +45,15 @@ pgwin32_is_admin(void)
41
45
exit (1 );
42
46
}
43
47
44
- if (GetTokenInformation (AccessToken , TokenGroups , NULL , 0 , & InfoBufferSize ))
48
+ if (!pgwin32_get_dynamic_tokeninfo (AccessToken , TokenGroups ,
49
+ & InfoBuffer , errbuf , sizeof (errbuf )))
45
50
{
46
- write_stderr ("could not get token information: got zero size\n" );
51
+ write_stderr (errbuf );
47
52
exit (1 );
48
53
}
49
54
50
- if (GetLastError () != ERROR_INSUFFICIENT_BUFFER )
51
- {
52
- write_stderr ("could not get token information: error code %d\n" ,
53
- (int ) GetLastError ());
54
- exit (1 );
55
- }
56
-
57
- InfoBuffer = malloc (InfoBufferSize );
58
- if (!InfoBuffer )
59
- {
60
- write_stderr ("could not allocate %i bytes for token information\n" ,
61
- (int ) InfoBufferSize );
62
- exit (1 );
63
- }
64
55
Groups = (PTOKEN_GROUPS ) InfoBuffer ;
65
56
66
- if (!GetTokenInformation (AccessToken , TokenGroups , InfoBuffer ,
67
- InfoBufferSize , & InfoBufferSize ))
68
- {
69
- write_stderr ("could not get token information: error code %d\n" ,
70
- (int ) GetLastError ());
71
- exit (1 );
72
- }
73
-
74
57
CloseHandle (AccessToken );
75
58
76
59
if (!AllocateAndInitializeSid (& NtAuthority , 2 ,
@@ -131,10 +114,10 @@ pgwin32_is_service(void)
131
114
{
132
115
static int _is_service = -1 ;
133
116
HANDLE AccessToken ;
134
- UCHAR InfoBuffer [ 1024 ] ;
135
- PTOKEN_GROUPS Groups = ( PTOKEN_GROUPS ) InfoBuffer ;
136
- PTOKEN_USER User = ( PTOKEN_USER ) InfoBuffer ;
137
- DWORD InfoBufferSize ;
117
+ char * InfoBuffer = NULL ;
118
+ char errbuf [ 256 ] ;
119
+ PTOKEN_GROUPS Groups ;
120
+ PTOKEN_USER User ;
138
121
PSID ServiceSid ;
139
122
PSID LocalSystemSid ;
140
123
SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY };
@@ -152,13 +135,15 @@ pgwin32_is_service(void)
152
135
}
153
136
154
137
/* First check for local system */
155
- if (!GetTokenInformation (AccessToken , TokenUser , InfoBuffer , 1024 , & InfoBufferSize ))
138
+ if (!pgwin32_get_dynamic_tokeninfo (AccessToken , TokenUser , & InfoBuffer ,
139
+ errbuf , sizeof (errbuf )))
156
140
{
157
- fprintf (stderr , "could not get token information: error code %d\n" ,
158
- (int ) GetLastError ());
141
+ fprintf (stderr ,errbuf );
159
142
return -1 ;
160
143
}
161
144
145
+ User = (PTOKEN_USER ) InfoBuffer ;
146
+
162
147
if (!AllocateAndInitializeSid (& NtAuthority , 1 ,
163
148
SECURITY_LOCAL_SYSTEM_RID , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
164
149
& LocalSystemSid ))
@@ -171,26 +156,31 @@ pgwin32_is_service(void)
171
156
if (EqualSid (LocalSystemSid , User -> User .Sid ))
172
157
{
173
158
FreeSid (LocalSystemSid );
159
+ free (InfoBuffer );
174
160
CloseHandle (AccessToken );
175
161
_is_service = 1 ;
176
162
return _is_service ;
177
163
}
178
164
179
165
FreeSid (LocalSystemSid );
166
+ free (InfoBuffer );
180
167
181
168
/* Now check for group SID */
182
- if (!GetTokenInformation (AccessToken , TokenGroups , InfoBuffer , 1024 , & InfoBufferSize ))
169
+ if (!pgwin32_get_dynamic_tokeninfo (AccessToken , TokenGroups , & InfoBuffer ,
170
+ errbuf , sizeof (errbuf )))
183
171
{
184
- fprintf (stderr , "could not get token information: error code %d\n" ,
185
- (int ) GetLastError ());
172
+ fprintf (stderr ,errbuf );
186
173
return -1 ;
187
174
}
188
175
176
+ Groups = (PTOKEN_GROUPS ) InfoBuffer ;
177
+
189
178
if (!AllocateAndInitializeSid (& NtAuthority , 1 ,
190
179
SECURITY_SERVICE_RID , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
191
180
& ServiceSid ))
192
181
{
193
182
fprintf (stderr , "could not get SID for service group\n" );
183
+ free (InfoBuffer );
194
184
CloseHandle (AccessToken );
195
185
return -1 ;
196
186
}
@@ -205,9 +195,54 @@ pgwin32_is_service(void)
205
195
}
206
196
}
207
197
198
+ free (InfoBuffer );
208
199
FreeSid (ServiceSid );
209
200
210
201
CloseHandle (AccessToken );
211
202
212
203
return _is_service ;
213
204
}
205
+
206
+
207
+ /*
208
+ * Call GetTokenInformation() on a token and return a dynamically sized
209
+ * buffer with the information in it. This buffer must be free():d by
210
+ * the calling function!
211
+ */
212
+ static BOOL
213
+ pgwin32_get_dynamic_tokeninfo (HANDLE token , TOKEN_INFORMATION_CLASS class ,
214
+ char * * InfoBuffer , char * errbuf , int errsize )
215
+ {
216
+ DWORD InfoBufferSize ;
217
+
218
+ if (GetTokenInformation (token , class , NULL , 0 , & InfoBufferSize ))
219
+ {
220
+ snprintf (errbuf ,errsize ,"could not get token information: got zero size\n" );
221
+ return FALSE;
222
+ }
223
+
224
+ if (GetLastError () != ERROR_INSUFFICIENT_BUFFER )
225
+ {
226
+ snprintf (errbuf ,errsize ,"could not get token information: error code %d\n" ,
227
+ (int ) GetLastError ());
228
+ return FALSE;
229
+ }
230
+
231
+ * InfoBuffer = malloc (InfoBufferSize );
232
+ if (* InfoBuffer == NULL )
233
+ {
234
+ snprintf (errbuf ,errsize ,"could not allocate %d bytes for token information\n" ,
235
+ (int ) InfoBufferSize );
236
+ return FALSE;
237
+ }
238
+
239
+ if (!GetTokenInformation (token , class , * InfoBuffer ,
240
+ InfoBufferSize , & InfoBufferSize ))
241
+ {
242
+ snprintf (errbuf ,errsize ,"could not get token information: error code %d\n" ,
243
+ (int ) GetLastError ());
244
+ return FALSE;
245
+ }
246
+
247
+ return TRUE;
248
+ }
0 commit comments