summaryrefslogtreecommitdiffstats
path: root/lib/puppet/server/servlet.rb
blob: e35a1d5184f884c8e1d12e06a91975d9204ca54b (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
require 'xmlrpc/server'

module Puppet
class 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

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

        # Verify that our client has access.  We allow untrusted access to
        # puppetca methods but none others.
        def authorize(request, method)
            namespace = method.sub(/\..+/, '')
            client = request.peeraddr[2]
            ip = request.peeraddr[3]
            if request.client_cert
                Puppet.info "Allowing %s(%s) trusted access to %s" %
                    [client, ip, method]
                return true
            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)
            #Puppet.info server.inspect
            
            # 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)
            }

            @request = nil
            @client = nil
            @clientip = nil
            self.set_service_hook { |obj, *args|
                #raise "crap!"
                if @client and @clientip
                    args.push(@client, @clientip)
                    #obj.call(args, @request)
                end
                begin
                    obj.call(*args)
                rescue Puppet::Server::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
                    Puppet.err detail.to_s
                    raise XMLRPC::FaultException.new(
                        1, detail.to_s
                    )
                rescue => detail
                    #Puppet.warning obj.inspect
                    #Puppet.warning args.inspect
                    Puppet.err "Could not call: %s" % detail.to_s
                    raise error
                end
            }
        end

        def service(request, response)
            @request = request
            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 request.client_cert
            #    Puppet.info "client cert is %s" % request.client_cert
            #end
            #if request.server_cert
            #    Puppet.info "server cert is %s" % @request.server_cert
            #end
            #p @request
            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
end