File: services.rb

package info (click to toggle)
libnet-ssh-ruby 1.1.2-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,472 kB
  • ctags: 2,465
  • sloc: ruby: 10,848; makefile: 17
file content (141 lines) | stat: -rw-r--r-- 4,727 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
#--
# =============================================================================
# Copyright (c) 2004,2005 Jamis Buck (jamis@37signals.com)
# All rights reserved.
#
# This source file is distributed as part of the Net::SSH Secure Shell Client
# library for Ruby. This file (and the library as a whole) may be used only as
# allowed by either the BSD license, or the Ruby license (or, by association
# with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SSH
# distribution for the texts of these licenses.
# -----------------------------------------------------------------------------
# net-ssh website : http://net-ssh.rubyforge.org
# project website: http://rubyforge.org/projects/net-ssh
# =============================================================================
#++

module Net
  module SSH
    module UserAuth

      def register_services( container )
        container.namespace_define :userauth do |b|

          b.require 'net/ssh/userauth/methods/services', "#{self}::Methods"

          b.agent_socket_factory do
            if File::ALT_SEPARATOR
              require 'net/ssh/userauth/pageant'
              Pageant::Socket
            else
              require 'socket'
              defined?( UNIXSocket ) ? UNIXSocket : nil
            end
          end

          b.default_agent_socket_name { ENV['SSH_AUTH_SOCK'] }

          b.default_agent_version { 2 }
          
          b.agent( :model => :prototype ) do |c,p|
            socket_factory = c[:agent_socket_factory]
            socket_name = c[:default_agent_socket_name]

            if (File::ALT_SEPARATOR || socket_name) && socket_factory
              require 'net/ssh/userauth/agent'
              require 'net/ssh/transport/services'

              agent = Agent.new
              agent.socket_factory = socket_factory
              agent.socket_name = socket_name
              agent.version = c[:default_agent_version]
              agent.buffers = c[:transport][:buffers]
              agent.keys = c[:transport][:keys]
              
              begin
                agent.connect!
              rescue Net::SSH::Exception
                # if there was an error connecting to the agent, swallow the
                # error and move on, without the agent
                agent = nil
              end
            end

            agent
          end

          b.agent_factory do |c,p|
            factory = Object.new
            klass = class << factory; self; end
            klass.send( :define_method, :open ) { c[:agent] }
            factory
          end

          b.default_user_key_locations do
            [ "#{ENV['HOME']}/.ssh/id_dsa",
              "#{ENV['HOME']}/.ssh2/id_dsa",
              "#{ENV['HOME']}/.ssh/id_rsa",
              "#{ENV['HOME']}/.ssh2/id_rsa" ]
          end

          b.default_host_key_locations do
            [ "/etc/ssh/ssh_host_dsa_key",
              "/etc/ssh/ssh_host_rsa_key" ]
          end

          b.key_existence_tester { File }

          b.user_keys do |c,p|
            require 'net/ssh/userauth/userkeys'

            userkeys = UserKeyManager.new
            userkeys.agent_factory = c[:agent_factory]
            userkeys.keys = c[:transport][:keys]
            userkeys.buffers = c[:transport][:buffers]
            userkeys.log = c[:log_for, p]
            userkeys.key_existence_tester = b.key_existence_tester

            b.default_user_key_locations.each { |f| userkeys.add f }
            b.default_host_key_locations.each { |f| userkeys.add_host_key f }

            userkeys
          end

          b.authentication_method_order do
            [ "publickey",
              "hostbased",
              "password",
              "keyboard-interactive" ]
          end

          b.driver do |c,p|
            require 'net/ssh/userauth/driver'

            driver = Driver.new( c[:log_for, p],
                                 c[:transport][:buffers],
                                 c[:methods],
                                 c[:authentication_method_order] )

            driver.key_manager = c[:user_keys]
            driver.session = c[:transport][:session]

            if c.knows_key?(:userauth_keys) && c[:userauth_keys]
              driver.set_key_files c[:userauth_keys]
            end
            if c.knows_key?(:userauth_host_keys) && c[:userauth_host_keys]
              driver.set_host_key_files c[:userauth_host_keys]
            end
            if c.knows_key?(:userauth_method_order) && c[:userauth_method_order]
              driver.set_auth_method_order(*c[:userauth_method_order])
            end

            driver
          end

        end
      end
      module_function :register_services

    end
  end
end