[前][次][番号順一覧][スレッド一覧]

ruby-changes:63570

From: NAKAMURA <ko1@a...>
Date: Tue, 10 Nov 2020 14:47:50 +0900 (JST)
Subject: [ruby-changes:63570] 9728cb730f (master): Use fiddle (win32/importer) instead of Win32API

https://git.ruby-lang.org/ruby.git/commit/?id=9728cb730f

From 9728cb730f29783f949ba2824f4e411c6abade18 Mon Sep 17 00:00:00 2001
From: NAKAMURA Usaku <usa@r...>
Date: Tue, 10 Nov 2020 14:45:15 +0900
Subject: Use fiddle (win32/importer) instead of Win32API

- ext/win32/lib/win32/sspi.rb: Use fiddle (win32/importer) instead of Win32API

diff --git a/ext/win32/lib/win32/sspi.rb b/ext/win32/lib/win32/sspi.rb
index 8103893..56cfc93 100644
--- a/ext/win32/lib/win32/sspi.rb
+++ b/ext/win32/lib/win32/sspi.rb
@@ -11,321 +11,328 @@ https://github.com/ruby/ruby/blob/trunk/ext/win32/lib/win32/sspi.rb#L11
 # Ruby Distribution License or GNU General Public License.
 #
 
-require 'Win32API'
+require 'win32/importer'
 
 # Implements bindings to Win32 SSPI functions, focused on authentication to a proxy server over HTTP.
 module Win32
-	module SSPI
-		# Specifies how credential structure requested will be used. Only SECPKG_CRED_OUTBOUND is used
-		# here.
-		SECPKG_CRED_INBOUND = 0x00000001
-		SECPKG_CRED_OUTBOUND = 0x00000002
-		SECPKG_CRED_BOTH = 0x00000003
-
-		# Format of token. NETWORK format is used here.
-		SECURITY_NATIVE_DREP = 0x00000010
-		SECURITY_NETWORK_DREP = 0x00000000
-
-		# InitializeSecurityContext Requirement flags
-		ISC_REQ_REPLAY_DETECT = 0x00000004
-		ISC_REQ_SEQUENCE_DETECT = 0x00000008
-		ISC_REQ_CONFIDENTIALITY = 0x00000010
-		ISC_REQ_USE_SESSION_KEY = 0x00000020
-		ISC_REQ_PROMPT_FOR_CREDS = 0x00000040
-		ISC_REQ_CONNECTION = 0x00000800
-
-		# 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')
-			# Can be called with InitializeSecurityContext.call()
-			InitializeSecurityContext = Win32API.new("secur32", "InitializeSecurityContext", 'pppLLLpLpppp', 'L')
-			# Can be called with DeleteSecurityContext.call()
-			DeleteSecurityContext = Win32API.new("secur32", "DeleteSecurityContext", 'P', 'L')
-			# 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
-
-		# TimeStamp struct
-		class TimeStamp
-			attr_reader :struct
-
-			def to_p
-				@struct ||= "\0" * 8
-			end
-		end
-
-		# Creates binary representations of a SecBufferDesc structure,
-		# including the SecBuffer contained inside.
-		class SecurityBuffer
-
-			SECBUFFER_TOKEN = 2   # Security token
-
-			TOKENBUFSIZE = 12288
-			SECBUFFER_VERSION = 0
-
-			def initialize(buffer = nil)
-				@buffer = buffer || "\0" * TOKENBUFSIZE
-				@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.
-				@unpacked = nil
-				# Assignment of inner structure to variable is very important here. Without it,
-				# 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")
-			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.
-			def unpack
-				if ! @unpacked && @sec_buffer && @struct
-					@bufferSize, @type = @sec_buffer.unpack("LL")
-					@buffer = @sec_buffer.unpack("LLP#{@bufferSize}")[2]
-					@struct = nil
-					@sec_buffer = nil
-					@unpacked = true
-				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,
-				 @domain, @domain ? @domain.length : 0,
-				 @password, @password ? @password.length : 0,
-				 @flags].pack("PLPLPLL")
-			end
-		end
-
-		# Takes a return result from an SSPI function and interprets the value.
-		class SSPIResult
-			# Good results
-			SEC_E_OK = 0x00000000
-			SEC_I_CONTINUE_NEEDED = 0x00090312
-
-			# These are generally returned by InitializeSecurityContext
-			SEC_E_INSUFFICIENT_MEMORY = 0x80090300
-			SEC_E_INTERNAL_ERROR = 0x80090304
-			SEC_E_INVALID_HANDLE = 0x80090301
-			SEC_E_INVALID_TOKEN = 0x80090308
-			SEC_E_LOGON_DENIED = 0x8009030C
-			SEC_E_NO_AUTHENTICATING_AUTHORITY = 0x80090311
-			SEC_E_NO_CREDENTIALS = 0x8009030E
-			SEC_E_TARGET_UNKNOWN = 0x80090303
-			SEC_E_UNSUPPORTED_FUNCTION = 0x80090302
-			SEC_E_WRONG_PRINCIPAL = 0x80090322
-
-			# These are generally returned by AcquireCredentialsHandle
-			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
-				elsif other.is_a?(Fixnum)
-					@value == @@map[other]
-				else
-					false
-				end
-			end
-		end
-
-		# Handles "Negotiate" type authentication. Geared towards authenticating with a proxy server over HTTP
-		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
+  module SSPI
+    # Specifies how credential structure requested will be used. Only SECPKG_CRED_OUTBOUND is used
+    # here.
+    SECPKG_CRED_INBOUND = 0x00000001
+    SECPKG_CRED_OUTBOUND = 0x00000002
+    SECPKG_CRED_BOTH = 0x00000003
+
+    # Format of token. NETWORK format is used here.
+    SECURITY_NATIVE_DREP = 0x00000010
+    SECURITY_NETWORK_DREP = 0x00000000
+
+    # InitializeSecurityContext Requirement flags
+    ISC_REQ_REPLAY_DETECT = 0x00000004
+    ISC_REQ_SEQUENCE_DETECT = 0x00000008
+    ISC_REQ_CONFIDENTIALITY = 0x00000010
+    ISC_REQ_USE_SESSION_KEY = 0x00000020
+    ISC_REQ_PROMPT_FOR_CREDS = 0x00000040
+    ISC_REQ_CONNECTION = 0x00000800
+
+    # Win32 API Functions. Uses Win32API to bind methods to constants contained in class.
+    module API
+      extend Importer
+      dlload "secur32.dll"
+      [
+        # Can be called with AcquireCredentialsHandleA.call()
+        "unsigned long AcquireCredentialsHandleA(void *, void *, unsigned long, void *, void *, void *, void *, void *, void *)",
+        # Can be called with InitializeSecurityContextA.call()
+        "unsigned long InitializeSecurityContextA(void *, void *, void *, unsigned long, unsigned long, unsigned long, void *, unsigned long, void *, void *, void *, void *)",
+        # Can be called with DeleteSecurityContext.call()
+        "unsigned long DeleteSecurityContext(void *)",
+        # Can be called with FreeCredentialsHandle.call()
+        "unsigned long FreeCredentialsHandle(void *)"
+      ].each do |fn|
+        cfunc = extern fn, :stdcall
+        const_set cfunc.name.intern, cfunc
+      end
+    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
+
+    # TimeStamp struct
+    class TimeStamp
+      attr_reader :struct
+
+      def to_p
+        @struct ||= "\0" * 8
+      end
+    end
+
+    # Creates binary representations of a SecBufferDesc structure,
+    # including the SecBuffer contained inside.
+    class SecurityBuffer
+
+      SECBUFFER_TOKEN = 2   # Security token
+
+      TOKENBUFSIZE = 12288
+      SECBUFFER_VERSION = 0
+
+      def initialize(buffer = nil)
+        @buffer = buffer || "\0" * TOKENBUFSIZE
+        @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.
+        @unpacked = nil
+        # Assignment of inner structure to variable is very important here. Without it,
+        # 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 ||= [SEC (... truncated)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]