summaryrefslogtreecommitdiff
path: root/ext/dl/win32
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-06 03:56:38 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-06 03:56:38 +0000
commit287a34ae0dfc23e4158f67cb7783d239f202c368 (patch)
tree5e35d5b41aae961b37cf6632f60c42f51c7aa775 /ext/dl/win32
parent9b52ae2e6491bb5d6c59e1799449f6268baf6f89 (diff)
* {ext,lib,test}/**/*.rb: removed trailing spaces.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22784 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/dl/win32')
-rw-r--r--ext/dl/win32/lib/win32/registry.rb170
-rw-r--r--ext/dl/win32/lib/win32/resolv.rb46
-rw-r--r--ext/dl/win32/lib/win32/sspi.rb84
3 files changed, 150 insertions, 150 deletions
diff --git a/ext/dl/win32/lib/win32/registry.rb b/ext/dl/win32/lib/win32/registry.rb
index 175b34cede..a2b69a08fe 100644
--- a/ext/dl/win32/lib/win32/registry.rb
+++ b/ext/dl/win32/lib/win32/registry.rb
@@ -11,10 +11,10 @@ It uses dl/import to call Win32 Registry APIs.
reg['foo'] = 'bar' # write a value
reg['foo', Win32::Registry::REG_SZ] = 'bar' # write a value with type
reg.write('foo', Win32::Registry::REG_SZ, 'bar') # write a value
-
+
reg.each_value { |name, type, data| ... } # Enumerate values
reg.each_key { |key, wtime| ... } # Enumerate subkeys
-
+
reg.delete_value(name) # Delete a value
reg.delete_key(name) # Delete a subkey
reg.delete_key(name, true) # Delete a subkey recursively
@@ -35,26 +35,26 @@ It uses dl/import to call Win32 Registry APIs.
Open the registry key ((|subkey|)) under ((|key|)).
((|key|)) is Win32::Registry object of parent key.
You can use predefined key HKEY_* (see ((<constants>)))
-
+
((|desired|)) and ((|opt|)) is access mask and key option.
For detail, see ((<MSDN Library|URL:http://msdn.microsoft.com/library/en-us/sysinfo/base/regopenkeyex.asp>)).
-
+
If block is given, the key is closed automatically.
--- Registry.create(key, subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED)
--- Registry.create(key, subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED) { |reg| ... }
Create or open the registry key ((|subkey|)) under ((|key|)).
You can use predefined key HKEY_* (see ((<constants>)))
-
+
If subkey is already exists, key is opened and Registry#((<created?>))
method will return false.
-
+
If block is given, the key is closed automatically.
--- Registry.expand_environ(str)
Replace (({%\w+%})) into the environment value of ((|str|)).
This method is used for REG_EXPAND_SZ.
-
+
For detail, see ((<ExpandEnvironmentStrings|URL:http://msdn.microsoft.com/library/en-us/sysinfo/base/expandenvironmentstrings.asp>)) Win32 API.
--- Registry.type2name(type)
@@ -75,14 +75,14 @@ It uses dl/import to call Win32 Registry APIs.
--- close
Close key.
-
+
After closed, most method raises error.
--- read(name, *rtype)
Read a registry value named ((|name|)) and return array of
[ ((|type|)), ((|data|)) ].
When name is nil, the `default' value is read.
-
+
((|type|)) is value type. (see ((<Win32::Registry::Constants module>)))
((|data|)) is value data, its class is:
:REG_SZ, REG_EXPAND_SZ
@@ -93,19 +93,19 @@ It uses dl/import to call Win32 Registry APIs.
Integer
:REG_BINARY
String (contains binary data)
-
+
When ((|rtype|)) is specified, the value type must be included by
((|rtype|)) array, or TypeError is raised.
--- self[name, *rtype]
Read a registry value named ((|name|)) and return its value data.
The class of value is same as ((<read>)) method returns.
-
+
If the value type is REG_EXPAND_SZ, returns value data whose environment
variables are replaced.
If the value type is neither REG_SZ, REG_MULTI_SZ, REG_DWORD,
REG_DWORD_BIG_ENDIAN, nor REG_QWORD, TypeError is raised.
-
+
The meaning of ((|rtype|)) is same as ((<read>)) method.
--- read_s(name)
@@ -113,26 +113,26 @@ It uses dl/import to call Win32 Registry APIs.
--- read_bin(name)
Read a REG_SZ(read_s), REG_DWORD(read_i), or REG_BINARY(read_bin)
registry value named ((|name|)).
-
+
If the values type does not match, TypeError is raised.
--- read_s_expand(name)
Read a REG_SZ or REG_EXPAND_SZ registry value named ((|name|)).
-
+
If the value type is REG_EXPAND_SZ, environment variables are replaced.
Unless the value type is REG_SZ or REG_EXPAND_SZ, TypeError is raised.
--- write(name, type, data)
Write ((|data|)) to a registry value named ((|name|)).
When name is nil, write to the `default' value.
-
+
((|type|)) is type value. (see ((<Registry::Constants module>)))
Class of ((|data|)) must be same as which ((<read>))
method returns.
--- self[name, wtype = nil] = value
Write ((|value|)) to a registry value named ((|name|)).
-
+
If ((|wtype|)) is specified, the value type is it.
Otherwise, the value type is depend on class of ((|value|)):
:Integer
@@ -146,7 +146,7 @@ It uses dl/import to call Win32 Registry APIs.
--- write_i(name, value)
--- write_bin(name, value)
Write ((|value|)) to a registry value named ((|name|)).
-
+
The value type is REG_SZ(write_s), REG_DWORD(write_i), or
REG_BINARY(write_bin).
@@ -156,7 +156,7 @@ It uses dl/import to call Win32 Registry APIs.
--- each_key { |subkey, wtime| ... }
Enumerate subkeys.
-
+
((|subkey|)) is String which contains name of subkey.
((|wtime|)) is last write time as FILETIME (64-bit integer).
(see ((<Registry.wtime2time>)))
@@ -168,7 +168,7 @@ It uses dl/import to call Win32 Registry APIs.
--- delete_key(name, recursive = false)
Delete a subkey named ((|name|)) and all its values.
-
+
If ((|recursive|)) is false, the subkey must not have subkeys.
Otherwise, this method deletes all subkeys and values recursively.
@@ -215,7 +215,7 @@ It uses dl/import to call Win32 Registry APIs.
Length of security descriptor.
:wtime
Last write time as FILETIME(64-bit integer)
-
+
For detail, see ((<RegQueryInfoKey|URL:http://msdn.microsoft.com/library/en-us/sysinfo/base/regqueryinfokey.asp>)) Win32 API.
--- num_keys
@@ -275,7 +275,7 @@ module Win32
HKEY_PERFORMANCE_NLSTEXT = 0x80000060
HKEY_CURRENT_CONFIG = 0x80000005
HKEY_DYN_DATA = 0x80000006
-
+
REG_NONE = 0
REG_SZ = 1
REG_EXPAND_SZ = 2
@@ -290,7 +290,7 @@ module Win32
REG_RESOURCE_REQUIREMENTS_LIST = 10
REG_QWORD = 11
REG_QWORD_LITTLE_ENDIAN = 11
-
+
STANDARD_RIGHTS_READ = 0x00020000
STANDARD_RIGHTS_WRITE = 0x00020000
KEY_QUERY_VALUE = 0x0001
@@ -305,7 +305,7 @@ module Win32
KEY_SET_VALUE | KEY_CREATE_SUB_KEY
KEY_EXECUTE = KEY_READ
KEY_ALL_ACCESS = KEY_READ | KEY_WRITE | KEY_CREATE_LINK
-
+
REG_OPTION_RESERVED = 0x0000
REG_OPTION_NON_VOLATILE = 0x0000
REG_OPTION_VOLATILE = 0x0001
@@ -315,21 +315,21 @@ module Win32
REG_LEGAL_OPTION = REG_OPTION_RESERVED |
REG_OPTION_NON_VOLATILE | REG_OPTION_CREATE_LINK |
REG_OPTION_BACKUP_RESTORE | REG_OPTION_OPEN_LINK
-
+
REG_CREATED_NEW_KEY = 1
REG_OPENED_EXISTING_KEY = 2
-
+
REG_WHOLE_HIVE_VOLATILE = 0x0001
REG_REFRESH_HIVE = 0x0002
REG_NO_LAZY_FLUSH = 0x0004
REG_FORCE_RESTORE = 0x0008
-
+
MAX_KEY_LENGTH = 514
MAX_VALUE_LENGTH = 32768
end
include Constants
include Enumerable
-
+
#
# Error
#
@@ -348,7 +348,7 @@ module Win32
end
attr_reader :code
end
-
+
#
# Predefined Keys
#
@@ -359,23 +359,23 @@ module Win32
@keyname = keyname
@disposition = REG_OPENED_EXISTING_KEY
end
-
+
# Predefined keys cannot be closed
def close
raise Error.new(5) ## ERROR_ACCESS_DENIED
end
-
+
# Fake class for Registry#open, Registry#create
def class
Registry
end
-
+
# Make all
Constants.constants.grep(/^HKEY_/) do |c|
Registry.const_set c, new(Constants.const_get(c), c)
end
end
-
+
#
# Win32 APIs
#
@@ -398,37 +398,37 @@ module Win32
cfunc = extern fn, :stdcall
const_set cfunc.name.intern, cfunc
end
-
+
module_function
-
+
def check(result)
raise Error, result, caller(2) if result != 0
end
-
+
def packdw(dw)
[dw].pack('V')
end
-
+
def unpackdw(dw)
dw += [0].pack('V')
dw.unpack('V')[0]
end
-
+
def packqw(qw)
[ qw & 0xFFFFFFFF, qw >> 32 ].pack('VV')
end
-
+
def unpackqw(qw)
qw = qw.unpack('VV')
(qw[1] << 32) | qw[0]
end
-
+
def OpenKey(hkey, name, opt, desired)
result = packdw(0)
check RegOpenKeyExA.call(hkey, name, opt, desired, result)
unpackdw(result)
end
-
+
def CreateKey(hkey, name, opt, desired)
result = packdw(0)
disp = packdw(0)
@@ -436,14 +436,14 @@ module Win32
0, result, disp)
[ unpackdw(result), unpackdw(disp) ]
end
-
+
def EnumValue(hkey, index)
name = ' ' * Constants::MAX_KEY_LENGTH
size = packdw(Constants::MAX_KEY_LENGTH)
check RegEnumValueA.call(hkey, index, name, size, 0, 0, 0, 0)
name[0, unpackdw(size)]
end
-
+
def EnumKey(hkey, index)
name = ' ' * Constants::MAX_KEY_LENGTH
size = packdw(Constants::MAX_KEY_LENGTH)
@@ -451,7 +451,7 @@ module Win32
check RegEnumKeyExA.call(hkey, index, name, size, 0, 0, 0, wtime)
[ name[0, unpackdw(size)], unpackqw(wtime) ]
end
-
+
def QueryValue(hkey, name)
type = packdw(0)
size = packdw(0)
@@ -460,27 +460,27 @@ module Win32
check RegQueryValueExA.call(hkey, name, 0, type, data, size)
[ unpackdw(type), data[0, unpackdw(size)] ]
end
-
+
def SetValue(hkey, name, type, data, size)
check RegSetValueExA.call(hkey, name, 0, type, data, size)
end
-
+
def DeleteValue(hkey, name)
check RegDeleteValue.call(hkey, name)
end
-
+
def DeleteKey(hkey, name)
check RegDeleteKey.call(hkey, name)
end
-
+
def FlushKey(hkey)
check RegFlushKey.call(hkey)
end
-
+
def CloseKey(hkey)
check RegCloseKey.call(hkey)
end
-
+
def QueryInfoKey(hkey)
subkeys = packdw(0)
maxsubkeylen = packdw(0)
@@ -496,14 +496,14 @@ module Win32
unpackdw(secdescs), unpackqw(wtime) ]
end
end
-
+
#
# utility functions
#
def self.expand_environ(str)
str.gsub(/%([^%]+)%/) { ENV[$1] || ENV[$1.upcase] || $& }
end
-
+
@@type2name = { }
%w[
REG_NONE REG_SZ REG_EXPAND_SZ REG_BINARY REG_DWORD
@@ -513,24 +513,24 @@ module Win32
].each do |type|
@@type2name[Constants.const_get(type)] = type
end
-
+
def self.type2name(type)
@@type2name[type] || type.to_s
end
-
+
def self.wtime2time(wtime)
Time.at((wtime - 116444736000000000) / 10000000)
end
-
+
def self.time2wtime(time)
time.to_i * 10000000 + 116444736000000000
end
-
+
#
# constructors
#
private_class_method :new
-
+
def self.open(hkey, subkey, desired = KEY_READ, opt = REG_OPTION_RESERVED)
subkey = subkey.chomp('\\')
newkey = API.OpenKey(hkey.hkey, subkey, opt, desired)
@@ -545,7 +545,7 @@ module Win32
obj
end
end
-
+
def self.create(hkey, subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED)
newkey, disp = API.CreateKey(hkey.hkey, subkey, opt, desired)
obj = new(newkey, hkey, subkey, disp)
@@ -559,12 +559,12 @@ module Win32
obj
end
end
-
+
#
# finalizer
#
@@final = proc { |hkey| proc { API.CloseKey(hkey[0]) if hkey[0] } }
-
+
#
# initialize
#
@@ -577,18 +577,18 @@ module Win32
ObjectSpace.define_finalizer self, @@final.call(@hkeyfinal)
end
attr_reader :hkey, :parent, :keyname, :disposition
-
+
#
# attributes
#
def created?
@disposition == REG_CREATED_NEW_KEY
end
-
+
def open?
!@hkey.nil?
end
-
+
def name
parent = self
name = @keyname
@@ -597,35 +597,35 @@ module Win32
end
name
end
-
+
def inspect
"\#<Win32::Registry key=#{name.inspect}>"
end
-
+
#
# marshalling
#
def _dump(depth)
raise TypeError, "can't dump Win32::Registry"
end
-
+
#
# open/close
#
def open(subkey, desired = KEY_READ, opt = REG_OPTION_RESERVED, &blk)
self.class.open(self, subkey, desired, opt, &blk)
end
-
+
def create(subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED, &blk)
self.class.create(self, subkey, desired, opt, &blk)
end
-
+
def close
API.CloseKey(@hkey)
@hkey = @parent = @keyname = nil
@hkeyfinal[0] = nil
end
-
+
#
# iterator
#
@@ -648,7 +648,7 @@ module Win32
index
end
alias each each_value
-
+
def each_key
index = 0
while true
@@ -662,13 +662,13 @@ module Win32
end
index
end
-
+
def keys
keys_ary = []
each_key { |key,| keys_ary << key }
keys_ary
end
-
+
#
# reader
#
@@ -694,7 +694,7 @@ module Win32
raise TypeError, "Type #{type} is not supported."
end
end
-
+
def [](name, *rtype)
type, data = read(name, *rtype)
case type
@@ -706,11 +706,11 @@ module Win32
raise TypeError, "Type #{type} is not supported."
end
end
-
+
def read_s(name)
read(name, REG_SZ)[1]
end
-
+
def read_s_expand(name)
type, data = read(name, REG_SZ, REG_EXPAND_SZ)
if type == REG_EXPAND_SZ
@@ -719,15 +719,15 @@ module Win32
data
end
end
-
+
def read_i(name)
read(name, REG_DWORD, REG_DWORD_BIG_ENDIAN, REG_QWORD)[1]
end
-
+
def read_bin(name)
read(name, REG_BINARY)[1]
end
-
+
#
# writer
#
@@ -750,7 +750,7 @@ module Win32
end
API.SetValue(@hkey, name, type, data, data.length)
end
-
+
def []=(name, rtype, value = nil)
if value
write name, rtype, value
@@ -768,19 +768,19 @@ module Win32
end
value
end
-
+
def write_s(name, value)
write name, REG_SZ, value.to_s
end
-
+
def write_i(name, value)
write name, REG_DWORD, value.to_i
end
-
+
def write_bin(name, value)
write name, REG_BINARY, value.to_s
end
-
+
#
# delete
#
@@ -788,7 +788,7 @@ module Win32
API.DeleteValue(@hkey, name)
end
alias delete delete_value
-
+
def delete_key(name, recursive = false)
if recursive
open(name, KEY_ALL_ACCESS) do |reg|
@@ -810,14 +810,14 @@ module Win32
raise Error.new(5) ## ERROR_ACCESS_DENIED
end
end
-
+
#
# flush
#
def flush
API.FlushKey @hkey
end
-
+
#
# key information
#
diff --git a/ext/dl/win32/lib/win32/resolv.rb b/ext/dl/win32/lib/win32/resolv.rb
index beb7f40ca4..a164fd54d2 100644
--- a/ext/dl/win32/lib/win32/resolv.rb
+++ b/ext/dl/win32/lib/win32/resolv.rb
@@ -9,13 +9,13 @@ require 'win32/registry'
module Win32
module Resolv
API = Registry::API
-
+
def self.get_hosts_path
path = get_hosts_dir
path = File.expand_path('hosts', path)
File.exist?(path) ? path : nil
end
-
+
def self.get_resolv_info
search, nameserver = get_info
if search.empty?
@@ -47,7 +47,7 @@ if info.unpack('V5')[4] == 2 # VER_PLATFORM_WIN32_NT
#====================================================================
module_eval <<-'__EOS__', __FILE__, __LINE__+1
TCPIP_NT = 'SYSTEM\CurrentControlSet\Services\Tcpip\Parameters'
-
+
class << self
private
def get_hosts_dir
@@ -55,7 +55,7 @@ if info.unpack('V5')[4] == 2 # VER_PLATFORM_WIN32_NT
reg.read_s_expand('DataBasePath')
end
end
-
+
def get_info
search = nil
nameserver = []
@@ -65,7 +65,7 @@ if info.unpack('V5')[4] == 2 # VER_PLATFORM_WIN32_NT
search = slist.split(/,\s*/) unless slist.empty?
rescue Registry::Error
end
-
+
if add_search = search.nil?
search = []
begin
@@ -81,7 +81,7 @@ if info.unpack('V5')[4] == 2 # VER_PLATFORM_WIN32_NT
rescue Registry::Error
end
end
-
+
reg.open('Interfaces') do |h|
h.each_key do |iface,|
h.open(iface) do |regif|
@@ -99,7 +99,7 @@ if info.unpack('V5')[4] == 2 # VER_PLATFORM_WIN32_NT
end
rescue Registry::Error
end
-
+
if add_search
begin
[ 'Domain', 'DhcpDomain' ].each do |key|
@@ -129,16 +129,16 @@ else
TCPIP_9X = 'SYSTEM\CurrentControlSet\Services\VxD\MSTCP'
DHCP_9X = 'SYSTEM\CurrentControlSet\Services\VxD\DHCP'
WINDOWS = 'Software\Microsoft\Windows\CurrentVersion'
-
+
class << self
# private
-
+
def get_hosts_dir
Registry::HKEY_LOCAL_MACHINE.open(WINDOWS) do |reg|
reg.read_s_expand('SystemRoot')
end
end
-
+
def get_info
search = []
nameserver = []
@@ -155,13 +155,13 @@ else
end
rescue Registry::Error
end
-
+
dhcpinfo = get_dhcpinfo
search.concat(dhcpinfo[0])
nameserver.concat(dhcpinfo[1])
[ search, nameserver ]
end
-
+
def get_dhcpinfo
macaddrs = {}
ipaddrs = {}
@@ -170,7 +170,7 @@ else
ipaddr.each { |ipaddr| ipaddrs[ipaddr] = 1 }
end
iflist = [ macaddrs, ipaddrs ]
-
+
search = []
nameserver = []
version = -1
@@ -179,7 +179,7 @@ else
version = API.unpackdw(reg.read_bin("Version"))
rescue Registry::Error
end
-
+
reg.each_key do |key,|
catch(:not_used) do
reg.open(key) do |regdi|
@@ -192,7 +192,7 @@ else
end
[ search, nameserver ]
end
-
+
def get_dhcpinfo_95(reg)
dhcp = reg.read_bin("DhcpInfo")
[
@@ -203,7 +203,7 @@ else
reg.read_bin("OptionInfo"),
]
end
-
+
def get_dhcpinfo_98(reg)
[
API.unpackdw(reg.read_bin("DhcpIPAddress")),
@@ -213,7 +213,7 @@ else
reg.read_bin("OptionInfo"),
]
end
-
+
def get_dhcpinfo_key(version, reg, iflist)
info = case version
when 1
@@ -234,7 +234,7 @@ else
macaddr and macaddr.size == 6 and
hwtype == 1 and
iflist[0][macaddr] and iflist[1][ipaddr]
-
+
size = opt.size
idx = 0
while idx <= size
@@ -258,7 +258,7 @@ else
throw :not_used
end
end
-
+
module WsControl
module WSock32
extend DL::Importer
@@ -266,7 +266,7 @@ else
end
WsControl = WSock32.extern "int WsControl(int, int, void *, void *, void *, void *", :stdcall
WSAGetLastError = WSock32.extern "int WSAGetLastError(void)", :stdcall
-
+
MAX_TDI_ENTITIES = 512
IPPROTO_TCP = 6
WSCTL_TCP_QUERY_INFORMATION = 0
@@ -282,7 +282,7 @@ else
IF_MIB = 0x202
IF_MIB_STATS_ID = 1
IP_MIB_ADDRTABLE_ENTRY_ID = 0x102
-
+
def self.wsctl(tei_entity, tei_instance,
toi_class, toi_type, toi_id,
buffsize)
@@ -308,7 +308,7 @@ else
[ buff, API.unpackdw(buffsize) ]
end
private_class_method :wsctl
-
+
def self.get_iflist
# Get TDI Entity List
entities, size =
@@ -345,7 +345,7 @@ else
end
end
end
-
+
# Get IP Addresses
entities.each do |entity, instance|
if entity == CL_NL_ENTITY
diff --git a/ext/dl/win32/lib/win32/sspi.rb b/ext/dl/win32/lib/win32/sspi.rb
index ef90ae35a7..6022df96c3 100644
--- a/ext/dl/win32/lib/win32/sspi.rb
+++ b/ext/dl/win32/lib/win32/sspi.rb
@@ -2,7 +2,7 @@
# = win32/sspi.rb
#
# Copyright (c) 2006-2007 Justin Bailey
-#
+#
# Written and maintained by Justin Bailey <jgbailey@gmail.com>.
#
# This program is free software. You can re-distribute and/or
@@ -33,7 +33,7 @@ module Win32
ISC_REQ_PROMPT_FOR_CREDS = 0x00000040
ISC_REQ_CONNECTION = 0x00000800
- # Win32 API Functions. Uses Win32API to bind methods to constants contained in class.
+ # Win32 API Functions. Uses Win32API to bind methods to constants contained in class.
module API
# Can be called with AcquireCredentialsHandle.call()
AcquireCredentialsHandle = Win32API.new("secur32", "AcquireCredentialsHandle", 'ppLpppppp', 'L')
@@ -44,22 +44,22 @@ module Win32
# Can be called with FreeCredentialsHandle.call()
FreeCredentialsHandle = Win32API.new("secur32", "FreeCredentialsHandle", 'P', 'L')
end
-
+
# SecHandle struct
class SecurityHandle
def upper
@struct.unpack("LL")[1]
end
-
+
def lower
@struct.unpack("LL")[0]
end
-
+
def to_p
@struct ||= "\0" * 8
end
end
-
+
# Some familiar aliases for the SecHandle structure
CredHandle = CtxtHandle = SecurityHandle
@@ -71,13 +71,13 @@ module Win32
@struct ||= "\0" * 8
end
end
-
+
# Creates binary representaiton of a SecBufferDesc structure,
# including the SecBuffer contained inside.
class SecurityBuffer
SECBUFFER_TOKEN = 2 # Security token
-
+
TOKENBUFSIZE = 12288
SECBUFFER_VERSION = 0
@@ -86,22 +86,22 @@ module Win32
@bufferSize = @buffer.length
@type = SECBUFFER_TOKEN
end
-
+
def bufferSize
unpack
@bufferSize
end
-
+
def bufferType
unpack
@type
end
-
+
def token
unpack
@buffer
end
-
+
def to_p
# Assumption is that when to_p is called we are going to get a packed structure. Therefore,
# set @unpacked back to nil so we know to unpack when accessors are next accessed.
@@ -110,14 +110,14 @@ module Win32
# will not be able to unpack changes to the structure. Alternative, nested unpacks,
# does not work (i.e. @struct.unpack("LLP12")[2].unpack("LLP12") results in "no associated pointer")
@sec_buffer ||= [@bufferSize, @type, @buffer].pack("LLP")
- @struct ||= [SECBUFFER_VERSION, 1, @sec_buffer].pack("LLP")
+ @struct ||= [SECBUFFER_VERSION, 1, @sec_buffer].pack("LLP")
end
-
+
private
-
+
# Unpacks the SecurityBufferDesc structure into member variables. We
# only want to do this once per struct, so the struct is deleted
- # after unpacking.
+ # after unpacking.
def unpack
if ! @unpacked && @sec_buffer && @struct
@bufferSize, @type = @sec_buffer.unpack("LL")
@@ -128,30 +128,30 @@ module Win32
end
end
end
-
+
# SEC_WINNT_AUTH_IDENTITY structure
class Identity
SEC_WINNT_AUTH_IDENTITY_ANSI = 0x1
attr_accessor :user, :domain, :password
-
+
def initialize(user = nil, domain = nil, password = nil)
@user = user
@domain = domain
@password = password
@flags = SEC_WINNT_AUTH_IDENTITY_ANSI
end
-
+
def to_p
- [@user, @user ? @user.length : 0,
+ [@user, @user ? @user.length : 0,
@domain, @domain ? @domain.length : 0,
@password, @password ? @password.length : 0,
@flags].pack("PLPLPLL")
- end
+ end
end
# Takes a return result from an SSPI function and interprets the value.
- class SSPIResult
+ class SSPIResult
# Good results
SEC_E_OK = 0x00000000
SEC_I_CONTINUE_NEEDED = 0x00090312
@@ -172,27 +172,27 @@ module Win32
SEC_E_NOT_OWNER = 0x80090306
SEC_E_SECPKG_NOT_FOUND = 0x80090305
SEC_E_UNKNOWN_CREDENTIALS = 0x8009030D
-
+
@@map = {}
constants.each { |v| @@map[self.const_get(v.to_s)] = v }
attr_reader :value
-
+
def initialize(value)
# convert to unsigned long
value = [value].pack("L").unpack("L").first
raise "#{value.to_s(16)} is not a recognized result" unless @@map.has_key? value
@value = value
end
-
+
def to_s
@@map[@value].to_s
end
-
+
def ok?
@value == SEC_I_CONTINUE_NEEDED || @value == SEC_E_OK
end
-
+
def ==(other)
if other.is_a?(SSPIResult)
@value == other.value
@@ -205,14 +205,14 @@ module Win32
end
# Handles "Negotiate" type authentication. Geared towards authenticating with a proxy server over HTTP
- class NegotiateAuth
+ class NegotiateAuth
attr_accessor :credentials, :context, :contextAttributes, :user, :domain
# Default request flags for SSPI functions
REQUEST_FLAGS = ISC_REQ_CONFIDENTIALITY | ISC_REQ_REPLAY_DETECT | ISC_REQ_CONNECTION
# NTLM tokens start with this header always. Encoding alone adds "==" and newline, so remove those
- B64_TOKEN_PREFIX = ["NTLMSSP"].pack("m").delete("=\n")
+ B64_TOKEN_PREFIX = ["NTLMSSP"].pack("m").delete("=\n")
# Given a connection and a request path, performs authentication as the current user and returns
# the response from a GET request. The connnection should be a Net::HTTP object, and it should
@@ -222,7 +222,7 @@ module Win32
def NegotiateAuth.proxy_auth_get(http, path, user = nil, domain = nil)
raise "http must respond to :get" unless http.respond_to?(:get)
nego_auth = self.new user, domain
-
+
resp = http.get path, { "Proxy-Authorization" => "Negotiate " + nego_auth.get_initial_token }
if resp["Proxy-Authenticate"]
resp = http.get path, { "Proxy-Authorization" => "Negotiate " + nego_auth.complete_authentication(resp["Proxy-Authenticate"].split(" ").last.strip) }
@@ -230,7 +230,7 @@ module Win32
resp
end
-
+
# Creates a new instance ready for authentication as the given user in the given domain.
# Defaults to current user and domain as defined by ENV["USERDOMAIN"] and ENV["USERNAME"] if
# no arguments are supplied.
@@ -238,7 +238,7 @@ module Win32
if user.nil? && domain.nil? && ENV["USERNAME"].nil? && ENV["USERDOMAIN"].nil?
raise "A username or domain must be supplied since they cannot be retrieved from the environment"
end
-
+
@user = user || ENV["USERNAME"]
@domain = domain || ENV["USERDOMAIN"]
end
@@ -253,7 +253,7 @@ module Win32
@context = CtxtHandle.new
@contextAttributes = "\0" * 4
- result = SSPIResult.new(API::InitializeSecurityContext.call(@credentials.to_p, nil, nil,
+ result = SSPIResult.new(API::InitializeSecurityContext.call(@credentials.to_p, nil, nil,
REQUEST_FLAGS,0, SECURITY_NETWORK_DREP, nil, 0, @context.to_p, outputBuffer.to_p, @contextAttributes, TimeStamp.new.to_p))
if result.ok? then
@@ -262,15 +262,15 @@ module Win32
raise "Error: #{result.to_s}"
end
end
-
- # Takes a token and gets the next token in the Negotiate authentication chain. Token can be Base64 encoded or not.
+
+ # Takes a token and gets the next token in the Negotiate authentication chain. Token can be Base64 encoded or not.
# The token can include the "Negotiate" header and it will be stripped.
# Does not indicate if SEC_I_CONTINUE or SEC_E_OK was returned.
# Token returned is Base64 encoded w/ all new lines removed.
def complete_authentication(token)
raise "This object is no longer usable because its resources have been freed." if @cleaned_up
- # Nil token OK, just set it to empty string
+ # Nil token OK, just set it to empty string
token = "" if token.nil?
if token.include? "Negotiate"
@@ -278,17 +278,17 @@ module Win32
token = token.split(" ").last
end
- if token.include? B64_TOKEN_PREFIX
+ if token.include? B64_TOKEN_PREFIX
# indicates base64 encoded token
token = token.strip.unpack("m")[0]
end
-
+
outputBuffer = SecurityBuffer.new
- result = SSPIResult.new(API::InitializeSecurityContext.call(@credentials.to_p, @context.to_p, nil,
- REQUEST_FLAGS, 0, SECURITY_NETWORK_DREP, SecurityBuffer.new(token).to_p, 0,
+ result = SSPIResult.new(API::InitializeSecurityContext.call(@credentials.to_p, @context.to_p, nil,
+ REQUEST_FLAGS, 0, SECURITY_NETWORK_DREP, SecurityBuffer.new(token).to_p, 0,
@context.to_p,
outputBuffer.to_p, @contextAttributes, TimeStamp.new.to_p))
-
+
if result.ok? then
return encode_token(outputBuffer.token)
else
@@ -316,7 +316,7 @@ module Win32
@credentials = CredHandle.new
ts = TimeStamp.new
@identity = Identity.new @user, @domain
- result = SSPIResult.new(API::AcquireCredentialsHandle.call(nil, "Negotiate", SECPKG_CRED_OUTBOUND, nil, @identity.to_p,
+ result = SSPIResult.new(API::AcquireCredentialsHandle.call(nil, "Negotiate", SECPKG_CRED_OUTBOUND, nil, @identity.to_p,
nil, nil, @credentials.to_p, ts.to_p))
raise "Error acquire credentials: #{result}" unless result.ok?
end