summaryrefslogtreecommitdiffstats
path: root/lib/puppet/network/server/servlet.rb
blob: 325956d8cd113375e1ddccf87070bb5092db5c88 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
require 'xmlrpc/server'

class Puppet::Network::Server
    class ServletError < RuntimeError; end
    class Servlet < XMLRPC::WEBrickServlet
        ERR_UNAUTHORIZED = 30

        attr_accessor :request

        # this is just a duplicate of the normal method; it's here for
        # debugging when i need it
        def self.get_instance(server, *options)
            self.new(server, *options)
        end

        # This is a hackish way to avoid an auth message every time we have a
        # normal operation
        def self.log(msg)
            unless defined? @logs
                @logs = {}
            end
            if @logs.include?(msg)
                @logs[msg] += 1
            else
                Puppet.info msg
                @logs[msg] = 1
            end
        end

        def add_handler(interface, handler)
            @loadedhandlers << interface.prefix
            super
        end

        # Verify that our client has access.  We allow untrusted access to
        # puppetca methods but no others.
        def authorize(request, method)
            namespace = method.sub(/\..+/, '')
            client = request.peeraddr[2]
            if defined? @client and @client
                client = @client
            end
            ip = request.peeraddr[3]
            if request.client_cert
                if @puppetserver.authconfig.exists?
                    allowed = @puppetserver.authconfig.allowed?(method, client, ip)

                    if allowed
                        Puppet.debug "Allowing %s(%s) trusted access to %s" %
                            [client, ip, method]
                        return true
                    else
                        Puppet.debug "Denying %s(%s) trusted access to %s" %
                            [client, ip, method]
                        return false
                    end
                else
                    # This is pretty hackish, but...
                    # This means we can't actually test this method at this point.
                    # The next release of Puppet will almost definitely require
                    # this file to exist or will default to denying all access.
                    if Puppet.execname == "puppetmasterd" or defined? Test::Unit::TestCase
                        Puppet.debug "Allowing %s(%s) trusted access to %s" %
                            [client, ip, method]
                        return true
                    else
                        Puppet.debug "Denying %s(%s) trusted access to %s on %s" %
                            [client, ip, method, Puppet.execname]
                        return false
                    end
                end
            else
                if method =~ /^puppetca\./
                    Puppet.notice "Allowing %s(%s) untrusted access to CA methods" %
                        [client, ip]
                else
                    Puppet.err "Unauthenticated client %s(%s) cannot call %s" %
                        [client, ip, method]
                    return false
                end
            end
        end

        def available?(method)
            namespace = method.sub(/\..+/, '')
            client = request.peeraddr[2]
            ip = request.peeraddr[3]
            if @loadedhandlers.include?(namespace)
                return true
            else
                Puppet.warning "Client %s(%s) requested unavailable functionality %s" %
                    [client, ip, namespace]
                return false
            end
        end

        def initialize(server, handlers)
            @puppetserver = server
            @notified = {}
            # the servlet base class does not consume any arguments
            # and its BasicServer base class only accepts a 'class_delim'
            # option which won't change in Puppet at all
            # thus, we don't need to pass any args to our base class,
            # and we can consume them all ourselves
            super()

            @loadedhandlers = []
            handlers.each { |handler|
                #Puppet.debug "adding handler for %s" % handler.class
                self.add_handler(handler.class.interface, handler)
            }

            # Initialize these to nil, but they will get set to values
            # by the 'service' method.  These have to instance variables
            # because I don't have a clear line from the service method to
            # the service hook.
            @request = nil
            @client = nil
            @clientip = nil

            self.set_service_hook { |obj, *args|
                if @client and @clientip
                    args.push(@client, @clientip)
                end
                begin
                    obj.call(*args)
                rescue XMLRPC::FaultException
                    raise
                rescue Puppet::AuthorizationError => detail
                    #Puppet.warning obj.inspect
                    #Puppet.warning args.inspect
                    Puppet.err "Permission denied: %s" % detail.to_s
                    raise XMLRPC::FaultException.new(
                        1, detail.to_s
                    )
                rescue Puppet::Error => detail
                    #Puppet.warning obj.inspect
                    #Puppet.warning args.inspect
                    if Puppet[:trace]
                        puts detail.backtrace
                    end
                    Puppet.err detail.to_s
                    error = XMLRPC::FaultException.new(
                        1, detail.to_s
                    )
                    error.set_backtrace detail.backtrace
                    raise error
                rescue => detail
                    #Puppet.warning obj.inspect
                    #Puppet.warning args.inspect
                    if Puppet[:trace]
                        puts detail.backtrace
                    end
                    Puppet.err "Could not call: %s" % detail.to_s
                    error = XMLRPC::FaultException.new(1, detail.to_s)
                    error.set_backtrace detail.backtrace
                    raise error
                end
            }
        end

        # Handle the actual request.  This does some basic collection of
        # data, and then just calls the parent method.
        def service(request, response)
            @request = request

            # The only way that @client can be nil is if the request is local.
            if peer = request.peeraddr
                @client = peer[2]
                @clientip = peer[3]
            else
                raise XMLRPC::FaultException.new(
                    ERR_UNCAUGHT_EXCEPTION,
                    "Could not retrieve client information"
                )
            end

            # If they have a certificate (which will almost always be true)
            # then we get the hostname from the cert, instead of via IP
            # info
            if cert = request.client_cert
                nameary = cert.subject.to_a.find { |ary|
                    ary[0] == "CN"
                }   

                if nameary.nil?
                    Puppet.warning "Could not retrieve server name from cert"
                else
                    unless @client == nameary[1]
                        Puppet.debug "Overriding %s with cert name %s" %
                            [@client, nameary[1]]
                        @client = nameary[1]
                    end
                end
            end
            begin
                super
            rescue => detail
                Puppet.err "Could not service request: %s: %s" %
                    [detail.class, detail]
            end
            @client = nil
            @clientip = nil
            @request = nil
        end

        private

        # this is pretty much just a copy of the original method but with more
        # feedback
        # here's where we have our authorization hooks
        def dispatch(methodname, *args)

            if defined? @request and @request
                unless self.available?(methodname)
                    raise XMLRPC::FaultException.new(
                        ERR_UNAUTHORIZED,
                        "Functionality %s not available" %
                            methodname.sub(/\..+/, '')
                    )
                end
                unless self.authorize(@request, methodname)
                    raise XMLRPC::FaultException.new(
                        ERR_UNAUTHORIZED,
                        "Host %s not authorized to call %s" %
                            [@request.host, methodname]
                    )
                end
            else
                raise Puppet::DevError, "Did not get request in dispatch"
            end

            #Puppet.warning "dispatch on %s called with %s" %
            #    [methodname, args.inspect]
            for name, obj in @handler
                if obj.kind_of? Proc
                    unless methodname == name
                        #Puppet.debug "obj is proc but %s != %s" %
                        #    [methodname, name]
                        next
                    end
                else
                    unless methodname =~ /^#{name}(.+)$/
                        #Puppet.debug "methodname did not match"
                        next
                    end
                    unless obj.respond_to? $1
                        #Puppet.debug "methodname does not respond to %s" % $1
                        next
                    end
                    obj = obj.method($1)
                end

                if check_arity(obj, args.size)
                    if @service_hook.nil?
                        return obj.call(*args) 
                    else
                        return @service_hook.call(obj, *args)
                    end
                else
                    Puppet.debug "arity is incorrect"
                end
            end 

            if @default_handler.nil?
                raise XMLRPC::FaultException.new(
                    ERR_METHOD_MISSING,
                    "Method #{methodname} missing or wrong number of parameters!"
                )
            else
                @default_handler.call(methodname, *args) 
            end
        end
    end
end

# $Id$