Method: Win32::Security::SID#initialize
- Defined in:
- lib/win32/security/sid.rb
#initialize(account = nil, host = Socket.gethostname) ⇒ SID
Creates and returns a new Win32::Security::SID object, based on the account name, which may also be a binary SID. If a host is provided, then the information is retrieved from that host. Otherwise, the local host is used.
If no account is provided then it retrieves information for the user account associated with the calling thread and the host argument is ignored.
Note that this does NOT create a new SID, but merely retrieves information for an existing SID. To create a new SID, use the SID.create method.
Examples:
# Current user
Win32::Security::SID.new
# User 'john' on the localhost
Win32::Security::SID.new('john')
# User 'jane' on a remote machine
Win32::Security::SID.new('jane', 'some_host')
# Binary SID
Win32::Security::SID.new("\001\000\000\000\000\000\001\000\000\000\000")
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 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 |
# File 'lib/win32/security/sid.rb', line 172 def initialize(account=nil, host=Socket.gethostname) if account.nil? begin ptoken = FFI::MemoryPointer.new(:ulong) # Try the thread token first, default to the process token. bool = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, true, ptoken) if !bool && FFI.errno != ERROR_NO_TOKEN raise SystemCallError.new("OpenThreadToken", FFI.errno) else ptoken = FFI::MemoryPointer.new(:ulong) unless OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, ptoken) raise SystemCallError.new("OpenProcessToken", FFI.errno) end end token = ptoken.read_ulong pinfo = FFI::MemoryPointer.new(:pointer) plength = FFI::MemoryPointer.new(:ulong) # First pass, just get the size needed (1 is TokenOwner) GetTokenInformation(token, 1, pinfo, pinfo.size, plength) pinfo = FFI::MemoryPointer.new(plength.read_ulong) plength = FFI::MemoryPointer.new(:ulong) # Second pass, actual call (1 is TokenOwner) unless GetTokenInformation(token, 1, pinfo, pinfo.size, plength) raise SystemCallError.new("GetTokenInformation", FFI.errno) end token_info = pinfo.read_pointer ensure CloseHandle(token) if token end end if account ordinal_val = account[0] ordinal_val = ordinal_val.ord if RUBY_VERSION.to_f >= 1.9 else ordinal_val = nil end sid = FFI::MemoryPointer.new(:uchar, 260) sid_size = FFI::MemoryPointer.new(:ulong) sid_size.write_ulong(sid.size) domain = FFI::MemoryPointer.new(:uchar, 260) domain_size = FFI::MemoryPointer.new(:ulong) domain_size.write_ulong(domain.size) use_ptr = FFI::MemoryPointer.new(:ulong) if ordinal_val.nil? bool = LookupAccountSid( nil, token_info, sid, sid_size, domain, domain_size, use_ptr ) unless bool raise SystemCallError.new("LookupAccountSid", FFI.errno) end elsif ordinal_val < 10 # Assume it's a binary SID. account_ptr = FFI::MemoryPointer.from_string(account) bool = LookupAccountSid( host, account_ptr, sid, sid_size, domain, domain_size, use_ptr ) unless bool raise SystemCallError.new("LookupAccountSid", FFI.errno) end else bool = LookupAccountName( host, account, sid, sid_size, domain, domain_size, use_ptr ) unless bool raise SystemCallError.new("LookupAccountName", FFI.errno) end end # The arguments are flipped depending on which path we took if ordinal_val.nil? @sid = token_info.read_string @account = sid.read_string(sid.size).strip elsif ordinal_val < 10 @sid = account @account = sid.read_string(sid.size).strip else length = GetLengthSid(sid) @sid = sid.read_string(length) @account = account end @host = host @domain = domain.read_string @account_type = get_account_type(use_ptr.read_ulong) end |