win32-security 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +23 -19
- data/MANIFEST +8 -8
- data/README +35 -35
- data/Rakefile +46 -46
- data/lib/win32/security.rb +67 -67
- data/lib/win32/security/ace.rb +39 -39
- data/lib/win32/security/acl.rb +148 -148
- data/lib/win32/security/sid.rb +388 -381
- data/test/test_acl.rb +67 -67
- data/test/test_security.rb +34 -34
- data/test/test_sid.rb +141 -137
- data/win32-security.gemspec +27 -27
- metadata +2 -2
data/lib/win32/security/sid.rb
CHANGED
@@ -1,381 +1,388 @@
|
|
1
|
-
require 'windows/security'
|
2
|
-
require 'windows/thread'
|
3
|
-
require 'windows/process'
|
4
|
-
require 'windows/error'
|
5
|
-
require 'windows/msvcrt/string'
|
6
|
-
require 'windows/msvcrt/buffer'
|
7
|
-
require 'socket'
|
8
|
-
|
9
|
-
# The Win32 module serves as a namespace only.
|
10
|
-
module Win32
|
11
|
-
|
12
|
-
# The Security class serves as a toplevel class namespace.
|
13
|
-
class Security
|
14
|
-
|
15
|
-
# The SID class encapsulates a Security Identifier.
|
16
|
-
class SID
|
17
|
-
include Windows::Security
|
18
|
-
include Windows::Error
|
19
|
-
include Windows::MSVCRT::String
|
20
|
-
include Windows::MSVCRT::Buffer
|
21
|
-
include Windows::Thread
|
22
|
-
include Windows::Process
|
23
|
-
|
24
|
-
extend Windows::Security
|
25
|
-
extend Windows::Error
|
26
|
-
extend Windows::MSVCRT::String
|
27
|
-
extend Windows::MSVCRT::Buffer
|
28
|
-
|
29
|
-
# Error class typically raised if any of the SID methods fail
|
30
|
-
class Error < StandardError; end
|
31
|
-
|
32
|
-
# The version of the Win32::Security::SID class.
|
33
|
-
VERSION = '0.1.
|
34
|
-
|
35
|
-
# Some constant SID's for your convenience, in string format.
|
36
|
-
# See http://support.microsoft.com/kb/243330 for details.
|
37
|
-
|
38
|
-
Null = 'S-1-0'
|
39
|
-
Nobody = 'S-1-0-0'
|
40
|
-
World = 'S-1-1'
|
41
|
-
Everyone = 'S-1-1-0'
|
42
|
-
Local = 'S-1-2'
|
43
|
-
Creator = 'S-1-3'
|
44
|
-
CreatorOwner = 'S-1-3-0'
|
45
|
-
CreatorGroup = 'S-1-3-1'
|
46
|
-
CreatorOwnerServer = 'S-1-3-2'
|
47
|
-
CreatorGroupServer = 'S-1-3-3'
|
48
|
-
NonUnique = 'S-1-4'
|
49
|
-
Nt = 'S-1-5'
|
50
|
-
Dialup = 'S-1-5-1'
|
51
|
-
Network = 'S-1-5-2'
|
52
|
-
Batch = 'S-1-5-3'
|
53
|
-
Interactive = 'S-1-5-4'
|
54
|
-
Service = 'S-1-5-6'
|
55
|
-
Anonymous = 'S-1-5-7'
|
56
|
-
Proxy = 'S-1-5-8'
|
57
|
-
EnterpriseDomainControllers = 'S-1-5-9'
|
58
|
-
PrincipalSelf = 'S-1-5-10'
|
59
|
-
AuthenticatedUsers = 'S-1-5-11'
|
60
|
-
RestrictedCode = 'S-1-5-12'
|
61
|
-
TerminalServerUsers = 'S-1-5-13'
|
62
|
-
LocalSystem = 'S-1-5-18'
|
63
|
-
NtLocal = 'S-1-5-19'
|
64
|
-
NtNetwork = 'S-1-5-20'
|
65
|
-
BuiltinAdministrators = 'S-1-5-32-544'
|
66
|
-
BuiltinUsers = 'S-1-5-32-545'
|
67
|
-
Guests = 'S-1-5-32-546'
|
68
|
-
PowerUsers = 'S-1-5-32-547'
|
69
|
-
AccountOperators = 'S-1-5-32-548'
|
70
|
-
ServerOperators = 'S-1-5-32-549'
|
71
|
-
PrintOperators = 'S-1-5-32-550'
|
72
|
-
BackupOperators = 'S-1-5-32-551'
|
73
|
-
Replicators = 'S-1-5-32-552'
|
74
|
-
|
75
|
-
# The binary SID object itself.
|
76
|
-
attr_reader :sid
|
77
|
-
|
78
|
-
# The account name passed to the constructor.
|
79
|
-
attr_reader :account
|
80
|
-
|
81
|
-
# The SID account type, e.g. 'user, 'group', etc.
|
82
|
-
attr_reader :account_type
|
83
|
-
|
84
|
-
# The domain the SID is on.
|
85
|
-
attr_reader :domain
|
86
|
-
|
87
|
-
# The host passed to the constructor, or the localhost if none
|
88
|
-
# was specified.
|
89
|
-
attr_reader :host
|
90
|
-
|
91
|
-
# Converts a binary SID to a string in S-R-I-S-S... format.
|
92
|
-
#
|
93
|
-
def self.sid_to_string(sid)
|
94
|
-
sid_addr = [sid].pack('p*').unpack('L')[0]
|
95
|
-
sid_buf = 0.chr * 80
|
96
|
-
sid_ptr = 0.chr * 4
|
97
|
-
|
98
|
-
unless ConvertSidToStringSid(sid_addr, sid_ptr)
|
99
|
-
raise Error, get_last_error
|
100
|
-
end
|
101
|
-
|
102
|
-
strcpy(sid_buf, sid_ptr.unpack('L')[0])
|
103
|
-
sid_buf.strip
|
104
|
-
end
|
105
|
-
|
106
|
-
# Converts a string in S-R-I-S-S... format back to a binary SID.
|
107
|
-
#
|
108
|
-
def self.string_to_sid(string)
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
unless ConvertStringSidToSid(string_addr,
|
113
|
-
raise Error, get_last_error
|
114
|
-
end
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
#
|
131
|
-
#
|
132
|
-
#
|
133
|
-
#
|
134
|
-
#
|
135
|
-
#
|
136
|
-
#
|
137
|
-
#
|
138
|
-
#
|
139
|
-
#
|
140
|
-
#
|
141
|
-
#
|
142
|
-
#
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
#
|
173
|
-
#
|
174
|
-
#
|
175
|
-
#
|
176
|
-
#
|
177
|
-
#
|
178
|
-
#
|
179
|
-
#
|
180
|
-
#
|
181
|
-
#
|
182
|
-
#
|
183
|
-
#
|
184
|
-
#
|
185
|
-
#
|
186
|
-
#
|
187
|
-
#
|
188
|
-
#
|
189
|
-
#
|
190
|
-
#
|
191
|
-
#
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
if
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
@
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
#
|
306
|
-
#
|
307
|
-
def
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
#
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
#
|
336
|
-
#
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
#
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
1
|
+
require 'windows/security'
|
2
|
+
require 'windows/thread'
|
3
|
+
require 'windows/process'
|
4
|
+
require 'windows/error'
|
5
|
+
require 'windows/msvcrt/string'
|
6
|
+
require 'windows/msvcrt/buffer'
|
7
|
+
require 'socket'
|
8
|
+
|
9
|
+
# The Win32 module serves as a namespace only.
|
10
|
+
module Win32
|
11
|
+
|
12
|
+
# The Security class serves as a toplevel class namespace.
|
13
|
+
class Security
|
14
|
+
|
15
|
+
# The SID class encapsulates a Security Identifier.
|
16
|
+
class SID
|
17
|
+
include Windows::Security
|
18
|
+
include Windows::Error
|
19
|
+
include Windows::MSVCRT::String
|
20
|
+
include Windows::MSVCRT::Buffer
|
21
|
+
include Windows::Thread
|
22
|
+
include Windows::Process
|
23
|
+
|
24
|
+
extend Windows::Security
|
25
|
+
extend Windows::Error
|
26
|
+
extend Windows::MSVCRT::String
|
27
|
+
extend Windows::MSVCRT::Buffer
|
28
|
+
|
29
|
+
# Error class typically raised if any of the SID methods fail
|
30
|
+
class Error < StandardError; end
|
31
|
+
|
32
|
+
# The version of the Win32::Security::SID class.
|
33
|
+
VERSION = '0.1.4'
|
34
|
+
|
35
|
+
# Some constant SID's for your convenience, in string format.
|
36
|
+
# See http://support.microsoft.com/kb/243330 for details.
|
37
|
+
|
38
|
+
Null = 'S-1-0'
|
39
|
+
Nobody = 'S-1-0-0'
|
40
|
+
World = 'S-1-1'
|
41
|
+
Everyone = 'S-1-1-0'
|
42
|
+
Local = 'S-1-2'
|
43
|
+
Creator = 'S-1-3'
|
44
|
+
CreatorOwner = 'S-1-3-0'
|
45
|
+
CreatorGroup = 'S-1-3-1'
|
46
|
+
CreatorOwnerServer = 'S-1-3-2'
|
47
|
+
CreatorGroupServer = 'S-1-3-3'
|
48
|
+
NonUnique = 'S-1-4'
|
49
|
+
Nt = 'S-1-5'
|
50
|
+
Dialup = 'S-1-5-1'
|
51
|
+
Network = 'S-1-5-2'
|
52
|
+
Batch = 'S-1-5-3'
|
53
|
+
Interactive = 'S-1-5-4'
|
54
|
+
Service = 'S-1-5-6'
|
55
|
+
Anonymous = 'S-1-5-7'
|
56
|
+
Proxy = 'S-1-5-8'
|
57
|
+
EnterpriseDomainControllers = 'S-1-5-9'
|
58
|
+
PrincipalSelf = 'S-1-5-10'
|
59
|
+
AuthenticatedUsers = 'S-1-5-11'
|
60
|
+
RestrictedCode = 'S-1-5-12'
|
61
|
+
TerminalServerUsers = 'S-1-5-13'
|
62
|
+
LocalSystem = 'S-1-5-18'
|
63
|
+
NtLocal = 'S-1-5-19'
|
64
|
+
NtNetwork = 'S-1-5-20'
|
65
|
+
BuiltinAdministrators = 'S-1-5-32-544'
|
66
|
+
BuiltinUsers = 'S-1-5-32-545'
|
67
|
+
Guests = 'S-1-5-32-546'
|
68
|
+
PowerUsers = 'S-1-5-32-547'
|
69
|
+
AccountOperators = 'S-1-5-32-548'
|
70
|
+
ServerOperators = 'S-1-5-32-549'
|
71
|
+
PrintOperators = 'S-1-5-32-550'
|
72
|
+
BackupOperators = 'S-1-5-32-551'
|
73
|
+
Replicators = 'S-1-5-32-552'
|
74
|
+
|
75
|
+
# The binary SID object itself.
|
76
|
+
attr_reader :sid
|
77
|
+
|
78
|
+
# The account name passed to the constructor.
|
79
|
+
attr_reader :account
|
80
|
+
|
81
|
+
# The SID account type, e.g. 'user, 'group', etc.
|
82
|
+
attr_reader :account_type
|
83
|
+
|
84
|
+
# The domain the SID is on.
|
85
|
+
attr_reader :domain
|
86
|
+
|
87
|
+
# The host passed to the constructor, or the localhost if none
|
88
|
+
# was specified.
|
89
|
+
attr_reader :host
|
90
|
+
|
91
|
+
# Converts a binary SID to a string in S-R-I-S-S... format.
|
92
|
+
#
|
93
|
+
def self.sid_to_string(sid)
|
94
|
+
sid_addr = [sid].pack('p*').unpack('L')[0]
|
95
|
+
sid_buf = 0.chr * 80
|
96
|
+
sid_ptr = 0.chr * 4
|
97
|
+
|
98
|
+
unless ConvertSidToStringSid(sid_addr, sid_ptr)
|
99
|
+
raise Error, get_last_error
|
100
|
+
end
|
101
|
+
|
102
|
+
strcpy(sid_buf, sid_ptr.unpack('L')[0])
|
103
|
+
sid_buf.strip
|
104
|
+
end
|
105
|
+
|
106
|
+
# Converts a string in S-R-I-S-S... format back to a binary SID.
|
107
|
+
#
|
108
|
+
def self.string_to_sid(string)
|
109
|
+
string_addr = [string].pack('p*').unpack('L')[0]
|
110
|
+
sid_ptr = 0.chr * 4
|
111
|
+
|
112
|
+
unless ConvertStringSidToSid(string_addr, sid_ptr)
|
113
|
+
raise Error, get_last_error
|
114
|
+
end
|
115
|
+
|
116
|
+
unless IsValidSid(sid_ptr.unpack('L')[0])
|
117
|
+
raise Error, get_last_error
|
118
|
+
end
|
119
|
+
|
120
|
+
sid_len = GetLengthSid(sid_ptr.unpack('L')[0])
|
121
|
+
sid_buf = 0.chr * sid_len
|
122
|
+
|
123
|
+
unless CopySid(sid_len, [sid_buf].pack('p*').unpack('L')[0], sid_ptr.unpack('L')[0])
|
124
|
+
raise Error, get_last_error
|
125
|
+
end
|
126
|
+
|
127
|
+
sid_buf
|
128
|
+
end
|
129
|
+
|
130
|
+
# Creates a new SID with +authority+ and up to 8 +subauthorities+,
|
131
|
+
# and returns new Win32::Security::SID object.
|
132
|
+
#
|
133
|
+
# Example:
|
134
|
+
#
|
135
|
+
# sec = Security::SID.create(
|
136
|
+
# Security::SID::SECURITY_WORLD_SID_AUTHORITY,
|
137
|
+
# Security::SID::SECURITY_WORLD_RID
|
138
|
+
# )
|
139
|
+
#
|
140
|
+
# p sec
|
141
|
+
#
|
142
|
+
# #<Win32::Security::SID:0x2c5a95c
|
143
|
+
# @host="your_host",
|
144
|
+
# @account="Everyone",
|
145
|
+
# @account_type="well known group",
|
146
|
+
# @sid="\001\001\000\000\000\000\000\001\000\000\000\000",
|
147
|
+
# @domain=""
|
148
|
+
# >
|
149
|
+
#
|
150
|
+
def self.create(authority, *sub_authorities)
|
151
|
+
if sub_authorities.length > 8
|
152
|
+
raise ArgumentError, "maximum of 8 subauthorities allowed"
|
153
|
+
end
|
154
|
+
|
155
|
+
sid = 0.chr * GetSidLengthRequired(sub_authorities.length)
|
156
|
+
|
157
|
+
auth = 0.chr * 5 + authority.chr
|
158
|
+
|
159
|
+
unless InitializeSid(sid, auth, sub_authorities.length)
|
160
|
+
raise Error, get_last_error
|
161
|
+
end
|
162
|
+
|
163
|
+
sub_authorities.each_index do |i|
|
164
|
+
value = [sub_authorities[i]].pack('L')
|
165
|
+
auth_ptr = GetSidSubAuthority(sid, i)
|
166
|
+
memcpy(auth_ptr, value, 4)
|
167
|
+
end
|
168
|
+
|
169
|
+
new(sid)
|
170
|
+
end
|
171
|
+
|
172
|
+
# Creates and returns a new Win32::Security::SID object, based on
|
173
|
+
# the account name, which may also be a binary SID. If a host is
|
174
|
+
# provided, then the information is retrieved from that host.
|
175
|
+
# Otherwise, the local host is used.
|
176
|
+
#
|
177
|
+
# If no account is provided then it retrieves information for the
|
178
|
+
# user account associated with the calling thread and the host argument
|
179
|
+
# is ignored.
|
180
|
+
#
|
181
|
+
# Note that this does NOT create a new SID, but merely retrieves
|
182
|
+
# information for an existing SID. To create a new SID, use the
|
183
|
+
# SID.create method.
|
184
|
+
#
|
185
|
+
# Examples:
|
186
|
+
#
|
187
|
+
# # Current user
|
188
|
+
# Win32::Security::SID.new
|
189
|
+
#
|
190
|
+
# # User 'john' on the localhost
|
191
|
+
# Win32::Security::SID.new('john')
|
192
|
+
#
|
193
|
+
# # User 'jane' on a remote machine
|
194
|
+
# Win32::Security::SID.new('jane', 'some_host')
|
195
|
+
#
|
196
|
+
# # Binary SID
|
197
|
+
# Win32::Security::SID.new("\001\000\000\000\000\000\001\000\000\000\000")
|
198
|
+
#
|
199
|
+
def initialize(account=nil, host=Socket.gethostname)
|
200
|
+
if account.nil?
|
201
|
+
htoken = [0].pack('L')
|
202
|
+
bool = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, 1, htoken)
|
203
|
+
errno = GetLastError()
|
204
|
+
|
205
|
+
if !bool
|
206
|
+
if errno == ERROR_NO_TOKEN
|
207
|
+
unless OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, htoken)
|
208
|
+
raise get_last_error
|
209
|
+
end
|
210
|
+
else
|
211
|
+
raise get_last_error(errno)
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
htoken = htoken.unpack('V').first
|
216
|
+
cbti = [0].pack('L')
|
217
|
+
token_info = 0.chr * 36
|
218
|
+
|
219
|
+
bool = GetTokenInformation(
|
220
|
+
htoken,
|
221
|
+
TokenOwner,
|
222
|
+
token_info,
|
223
|
+
token_info.size,
|
224
|
+
cbti
|
225
|
+
)
|
226
|
+
|
227
|
+
unless bool
|
228
|
+
raise Error, get_last_error
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
bool = false
|
233
|
+
sid = 0.chr * 28
|
234
|
+
sid_cb = [sid.size].pack('L')
|
235
|
+
|
236
|
+
domain_buf = 0.chr * 80
|
237
|
+
domain_cch = [domain_buf.size].pack('L')
|
238
|
+
|
239
|
+
sid_name_use = 0.chr * 4
|
240
|
+
|
241
|
+
if account
|
242
|
+
ordinal_val = account[0]
|
243
|
+
ordinal_val = ordinal_val.ord if RUBY_VERSION.to_f >= 1.9
|
244
|
+
else
|
245
|
+
ordinal_val = nil
|
246
|
+
end
|
247
|
+
|
248
|
+
if ordinal_val.nil?
|
249
|
+
bool = LookupAccountSid(
|
250
|
+
nil,
|
251
|
+
token_info.unpack('L')[0],
|
252
|
+
sid,
|
253
|
+
sid_cb,
|
254
|
+
domain_buf,
|
255
|
+
domain_cch,
|
256
|
+
sid_name_use
|
257
|
+
)
|
258
|
+
elsif ordinal_val < 10 # Assume it's a binary SID.
|
259
|
+
bool = LookupAccountSid(
|
260
|
+
host,
|
261
|
+
[account].pack('p*').unpack('L')[0],
|
262
|
+
sid,
|
263
|
+
sid_cb,
|
264
|
+
domain_buf,
|
265
|
+
domain_cch,
|
266
|
+
sid_name_use
|
267
|
+
)
|
268
|
+
else
|
269
|
+
bool = LookupAccountName(
|
270
|
+
host,
|
271
|
+
account,
|
272
|
+
sid,
|
273
|
+
sid_cb,
|
274
|
+
domain_buf,
|
275
|
+
domain_cch,
|
276
|
+
sid_name_use
|
277
|
+
)
|
278
|
+
end
|
279
|
+
|
280
|
+
unless bool
|
281
|
+
raise Error, get_last_error
|
282
|
+
end
|
283
|
+
|
284
|
+
# The arguments are flipped depending on which path we took
|
285
|
+
if ordinal_val.nil?
|
286
|
+
buf = 0.chr * 260
|
287
|
+
ptr = token_info.unpack('L')[0]
|
288
|
+
memcpy(buf, ptr, token_info.size)
|
289
|
+
@sid = buf.strip
|
290
|
+
@account = sid.strip
|
291
|
+
elsif ordinal_val < 10
|
292
|
+
@sid = account
|
293
|
+
@account = sid.strip
|
294
|
+
else
|
295
|
+
@sid = sid.strip
|
296
|
+
@account = account
|
297
|
+
end
|
298
|
+
|
299
|
+
@host = host
|
300
|
+
@domain = domain_buf.strip
|
301
|
+
|
302
|
+
@account_type = get_account_type(sid_name_use.unpack('L')[0])
|
303
|
+
end
|
304
|
+
|
305
|
+
# Synonym for SID.new.
|
306
|
+
#
|
307
|
+
def self.open(account=nil, host=Socket.gethostname)
|
308
|
+
new(account, host)
|
309
|
+
end
|
310
|
+
|
311
|
+
# Returns the binary SID in string format suitable for display,
|
312
|
+
# storage or transmission.
|
313
|
+
#
|
314
|
+
def to_s
|
315
|
+
sid_addr = [@sid].pack('p*').unpack('L').first
|
316
|
+
sid_buf = 0.chr * 80
|
317
|
+
sid_ptr = 0.chr * 4
|
318
|
+
|
319
|
+
unless ConvertSidToStringSid(sid_addr, sid_ptr)
|
320
|
+
raise Error, get_last_error
|
321
|
+
end
|
322
|
+
|
323
|
+
strcpy(sid_buf, sid_ptr.unpack('L').first)
|
324
|
+
sid_buf.strip
|
325
|
+
end
|
326
|
+
|
327
|
+
alias to_str to_s
|
328
|
+
|
329
|
+
# Returns whether or not the SID object is equal to +other+.
|
330
|
+
#
|
331
|
+
def ==(other)
|
332
|
+
EqualSid(@sid, other.sid)
|
333
|
+
end
|
334
|
+
|
335
|
+
# Returns whether or not the SID is a valid sid.
|
336
|
+
#
|
337
|
+
def valid?
|
338
|
+
IsValidSid(@sid)
|
339
|
+
end
|
340
|
+
|
341
|
+
# Returns whether or not the SID is a well known SID.
|
342
|
+
#
|
343
|
+
# Requires Windows XP or later. Earlier versions will raise a
|
344
|
+
# NoMethodError.
|
345
|
+
#
|
346
|
+
def well_known?
|
347
|
+
if defined? IsWellKnownSid
|
348
|
+
IsWellKnownSid(@sid)
|
349
|
+
else
|
350
|
+
raise NoMethodError, 'requires Windows XP or later'
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
# Returns the length of the SID object, in bytes.
|
355
|
+
#
|
356
|
+
def length
|
357
|
+
GetLengthSid(@sid)
|
358
|
+
end
|
359
|
+
|
360
|
+
private
|
361
|
+
|
362
|
+
# Converts a numeric account type into a human readable string.
|
363
|
+
#
|
364
|
+
def get_account_type(value)
|
365
|
+
case value
|
366
|
+
when SidTypeUser
|
367
|
+
'user'
|
368
|
+
when SidTypeGroup
|
369
|
+
'group'
|
370
|
+
when SidTypeDomain
|
371
|
+
'domain'
|
372
|
+
when SidTypeAlias
|
373
|
+
'alias'
|
374
|
+
when SidTypeWellKnownGroup
|
375
|
+
'well known group'
|
376
|
+
when SidTypeDeletedAccount
|
377
|
+
'deleted account'
|
378
|
+
when SidTypeInvalid
|
379
|
+
'invalid'
|
380
|
+
when SidTypeUnknown
|
381
|
+
'unknown'
|
382
|
+
when SidComputer
|
383
|
+
'computer'
|
384
|
+
end
|
385
|
+
end
|
386
|
+
end
|
387
|
+
end
|
388
|
+
end
|