rubysspi-server 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE.txt ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2009 Alexey Borzenkov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.txt ADDED
@@ -0,0 +1,25 @@
1
+ = Server bindings to Win32 SSPI
2
+
3
+ The rubysspi gem provides a ruby interface to the SSPI functions in Windows
4
+ but it is mostly concerned with the client side of SSPI, to get through corporate
5
+ firewalls with NTLM.
6
+
7
+ This gem extends it to also support server side SSPI
8
+
9
+ = Using rubysspi-server
10
+
11
+ Instantiate NegotiateServer instance:
12
+
13
+ sspi = Win32::SSPI::NegotiateServer.new # optionally specifying "Negotiate" package (defaults to "NTLM")
14
+ sspi.acquire_credentials_handle
15
+
16
+ When you receive Type1 message, accept it:
17
+
18
+ t2 = sspi.accept_security_context(t1) # t2 is already Base64 encoded
19
+
20
+ When you receive Type3 message, accept it:
21
+
22
+ t2 = sspi.accept_security_context(t3)
23
+ username = sspi.get_username_from_context
24
+
25
+ Now connection is authenticated with NTLM/Negotiate.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "rubysspi-server"
5
+ gemspec.summary = "A library which implements Ruby server bindings to the Win32 SSPI library."
6
+ gemspec.author = "Alexey Borzenkov"
7
+ gemspec.email = "snaury@gmail.com"
8
+
9
+ gemspec.add_dependency('rubysspi', '>= 1.3.1')
10
+ end
11
+ rescue LoadError
12
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
13
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 0
4
+ :patch: 1
@@ -0,0 +1,117 @@
1
+ # Copyright (c) 2009 Alexey Borzenkov
2
+ #
3
+ # The rubysspi gem provides a ruby interface to the SSPI functions in Windows
4
+ # but it is mostly concerned with the client side of SSPI, to get through corporate
5
+ # firewalls with NTLM.
6
+ #
7
+ # This gem extends it to also support server side SSPI
8
+ #
9
+ # Originally part of mongrel-ntlm (c) 2008 Seggy Umboh
10
+
11
+ require 'win32/sspi'
12
+
13
+ module Win32
14
+ module SSPI
15
+ SECPKG_ATTR_NAMES = 0x00000001
16
+ ASC_REQ_DELEGATE = 0x00000001
17
+
18
+ module API
19
+ AcceptSecurityContext = Win32API.new('secur32', 'AcceptSecurityContext', 'pppLLpppp', 'L')
20
+ FreeContextBuffer = Win32API.new('secur32', 'FreeContextBuffer', 'P', 'L')
21
+ QueryContextAttributes = Win32API.new('secur32', 'QueryContextAttributes', 'pLp', 'L')
22
+ Strncpy = Win32API.new('msvcrt', 'strncpy', 'PLL', 'L')
23
+ end
24
+
25
+ class SecPkgCredentials_Names
26
+ BUF_SZ = 512
27
+
28
+ def initialize
29
+ @buffer = "\0" * BUF_SZ
30
+ end
31
+
32
+ def to_s
33
+ API::Strncpy.call(@buffer, @struct.unpack('L')[0], BUF_SZ-1) if @buffer.rstrip.empty?
34
+ @buffer.rstrip
35
+ end
36
+
37
+ def to_p
38
+ @struct ||= [@buffer].pack('p')
39
+ end
40
+
41
+ def cleanup
42
+ API::FreeContextBuffer.call(self.to_p)
43
+ end
44
+ end
45
+
46
+ class NegotiateServer
47
+ WORD_SZ = [0].pack('L').size
48
+ attr_accessor :package
49
+
50
+ def initialize(package = "NTLM")
51
+ @package = package
52
+ end
53
+
54
+ def acquire_credentials_handle
55
+ @credentials = CredHandle.new
56
+
57
+ result = SSPIResult.new(API::AcquireCredentialsHandle.call(
58
+ nil,
59
+ @package,
60
+ SECPKG_CRED_INBOUND,
61
+ nil,
62
+ nil,
63
+ nil,
64
+ nil,
65
+ @credentials.to_p,
66
+ TimeStamp.new.to_p
67
+ ))
68
+ raise "AcquireCredentialsHandle Error: #{result}" unless result.ok?
69
+ end
70
+
71
+ def accept_security_context(token)
72
+ incoming = SecurityBuffer.new(token)
73
+ outgoing = SecurityBuffer.new
74
+
75
+ current_context = @context.nil? ? nil : @context.to_p
76
+ @context ||= CtxtHandle.new
77
+ @contextAttributes = "\0" * WORD_SZ
78
+
79
+ result = SSPIResult.new(API::AcceptSecurityContext.call(
80
+ @credentials.to_p,
81
+ current_context,
82
+ incoming.to_p,
83
+ ASC_REQ_DELEGATE,
84
+ SECURITY_NETWORK_DREP,
85
+ @context.to_p,
86
+ outgoing.to_p,
87
+ @contextAttributes,
88
+ TimeStamp.new.to_p
89
+ ))
90
+ raise "AcceptSecurityContext Error: #{result}" unless result.ok?
91
+
92
+ Base64.encode64(outgoing.token).delete("\n")
93
+ end
94
+
95
+ def get_username_from_context
96
+ return @username unless @username.nil?
97
+ return nil if @context.nil?
98
+
99
+ names = SecPkgCredentials_Names.new
100
+ result = SSPIResult.new(API::QueryContextAttributes.call(
101
+ @context.to_p,
102
+ SECPKG_ATTR_NAMES,
103
+ names.to_p
104
+ ))
105
+ @username = names.to_s if result.ok?
106
+ ensure
107
+ names.cleanup
108
+ end
109
+
110
+ def cleanup
111
+ API::FreeCredentialsHandle.call(@credentials.to_p) unless @credentials.nil?
112
+ API::DeleteSecurityContext.call(@context.to_p) unless @context.nil?
113
+ @credentials = @context = @contextAttributes = nil
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,41 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{rubysspi-server}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Alexey Borzenkov"]
9
+ s.date = %q{2009-05-20}
10
+ s.email = %q{snaury@gmail.com}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE.txt",
13
+ "README.txt"
14
+ ]
15
+ s.files = [
16
+ "LICENSE.txt",
17
+ "README.txt",
18
+ "Rakefile",
19
+ "VERSION.yml",
20
+ "lib/win32/sspi/server.rb",
21
+ "rubysspi-server.gemspec"
22
+ ]
23
+ s.has_rdoc = true
24
+ s.rdoc_options = ["--charset=UTF-8"]
25
+ s.require_paths = ["lib"]
26
+ s.rubygems_version = %q{1.3.1}
27
+ s.summary = %q{A library which implements Ruby server bindings to the Win32 SSPI library.}
28
+
29
+ if s.respond_to? :specification_version then
30
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
31
+ s.specification_version = 2
32
+
33
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
34
+ s.add_runtime_dependency(%q<rubysspi>, [">= 1.3.1"])
35
+ else
36
+ s.add_dependency(%q<rubysspi>, [">= 1.3.1"])
37
+ end
38
+ else
39
+ s.add_dependency(%q<rubysspi>, [">= 1.3.1"])
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubysspi-server
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexey Borzenkov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-20 00:00:00 +04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rubysspi
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.3.1
24
+ version:
25
+ description:
26
+ email: snaury@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE.txt
33
+ - README.txt
34
+ files:
35
+ - LICENSE.txt
36
+ - README.txt
37
+ - Rakefile
38
+ - VERSION.yml
39
+ - lib/win32/sspi/server.rb
40
+ - rubysspi-server.gemspec
41
+ has_rdoc: true
42
+ homepage:
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.5
66
+ signing_key:
67
+ specification_version: 2
68
+ summary: A library which implements Ruby server bindings to the Win32 SSPI library.
69
+ test_files: []
70
+