summaryrefslogtreecommitdiffstats
path: root/lib/puppet/daemon.rb
blob: 13ae3f6a2dffd63a2cb951a81764e7940b4b84a5 (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
# helper functions for daemons

require 'puppet'

module Puppet
    # A module that handles operations common to all daemons.  This is included
    # into the Server and Client base classes.
    module Daemon
        include Puppet::Util

        Puppet.config.setdefaults(:puppet, :setpidfile => [true,
            "Whether to store a PID file for the daemon."])
        def daemonname
            #$0.sub(/.+#{File::SEPARATOR}/,'')
            Puppet.name
        end

        # The path to the pid file for this server
        def pidfile
            File.join(Puppet[:rundir], daemonname() + ".pid")
        end

        # Put the daemon into the background.
        def daemonize
            if pid = fork()
                Process.detach(pid)
                exit(0)
            end

            # Get rid of console logging
            Puppet::Log.close(:console)

            Process.setsid
            Dir.chdir("/")
            begin
                $stdin.reopen "/dev/null"
                $stdout.reopen "/dev/null", "a"
                $stderr.reopen $stdout
                Puppet::Log.reopen
            rescue => detail
                File.open("/tmp/daemonout", "w") { |f|
                    f.puts "Could not start %s: %s" % [Puppet.name, detail]
                }
                Puppet.err "Could not start %s: %s" % [Puppet.name, detail]
                exit(12)
            end
        end

        def fqdn
            unless defined? @fqdn and @fqdn
                hostname = Facter["hostname"].value
                domain = Facter["domain"].value
                @fqdn = [hostname, domain].join(".")
            end
            return @fqdn
        end

        def httplog
            args = []

            # yuck; separate http logs
            file = nil
            Puppet.config.use(:puppet, :certificates, Puppet.name)
            if Puppet.name == "puppetmasterd"
                file = Puppet[:masterhttplog]
            else
                file = Puppet[:httplog]
            end
#
#            unless FileTest.exists?(File.dirname(file))
#                Puppet.recmkdir(File.dirname(file))
#            end

            args << file
            if Puppet[:debug]
                args << WEBrick::Log::DEBUG
            end

            log = WEBrick::Log.new(*args)


            return log
        end

        # Read in an existing certificate.
        def readcert
            return unless @secureinit
            Puppet.config.use(:puppet, :certificates)
            # verify we've got all of the certs set up and such

            if defined? @cert and defined? @key and @cert and @key
                return true
            end

            unless defined? @fqdn
                self.fqdn
            end

            # we are not going to encrypt our key, but we need at a minimum
            # a keyfile and a certfile
            @certfile = File.join(Puppet[:certdir], [@fqdn, "pem"].join("."))
            @cacertfile = File.join(Puppet[:certdir], ["ca", "pem"].join("."))
            @keyfile = File.join(Puppet[:privatekeydir], [@fqdn, "pem"].join("."))
            @publickeyfile = File.join(Puppet[:publickeydir], [@fqdn, "pem"].join("."))

            if File.exists?(@keyfile)
                # load the key
                @key = OpenSSL::PKey::RSA.new(File.read(@keyfile))
            else
                return false
            end

            if File.exists?(@certfile)
                if File.exists?(@cacertfile)
                    @cacert = OpenSSL::X509::Certificate.new(File.read(@cacertfile))
                else
                    raise Puppet::Error, "Found cert file with no ca cert file"
                end
                @cert = OpenSSL::X509::Certificate.new(File.read(@certfile))
            else
                return false
            end
            return true
        end

        # Request a certificate from the remote system.  This does all of the work
        # of creating the cert request, contacting the remote system, and
        # storing the cert locally.
        def requestcert
            unless @secureinit
                raise Puppet::DevError,
                    "Tried to request cert without initialized security"
            end
            retrieved = false
            Puppet.config.use(:puppet, :certificates)
            # create the directories involved
            # FIXME it's a stupid hack that i have to do this
#            [Puppet[:certdir], Puppet[:privatekeydir], Puppet[:csrdir],
#                Puppet[:publickeydir]].each { |dir|
#                unless FileTest.exists?(dir)
#                    Puppet.recmkdir(dir, 0770)
#                end
#            }

            if self.readcert
                Puppet.info "Certificate already exists; not requesting"
                return true
            end

            unless defined? @key and @key
                # create a new one and store it
                Puppet.info "Creating a new SSL key at %s" % @keyfile
                @key = OpenSSL::PKey::RSA.new(Puppet[:keylength])
                File.open(@keyfile, "w", 0660) { |f| f.print @key.to_pem }
                File.open(@publickeyfile, "w", 0660) { |f|
                    f.print @key.public_key.to_pem
                }
            end

            unless defined? @driver
                Puppet.err "Cannot request a certificate without a defined target"
                return false
            end

            Puppet.info "Creating a new certificate request for %s" % @fqdn
            name = OpenSSL::X509::Name.new([["CN", @fqdn]])

            @csr = OpenSSL::X509::Request.new
            @csr.version = 0
            @csr.subject = name
            @csr.public_key = @key.public_key
            @csr.sign(@key, OpenSSL::Digest::MD5.new)

            Puppet.info "Requesting certificate"

            begin
                cert, cacert = @driver.getcert(@csr.to_pem)
            rescue => detail
                if Puppet[:debug]
                    puts detail.backtrace
                end
                raise Puppet::Error.new("Certificate retrieval failed: %s" %
                    detail)
            end

            if cert.nil? or cert == ""
                return nil
            end
            File.open(@certfile, "w", 0660) { |f| f.print cert }
            File.open(@cacertfile, "w", 0660) { |f| f.print cacert }
            begin
                @cert = OpenSSL::X509::Certificate.new(cert)
                @cacert = OpenSSL::X509::Certificate.new(cacert)
                retrieved = true
            rescue => detail
                raise Puppet::Error.new(
                    "Invalid certificate: %s" % detail
                )
            end

            unless @cert.check_private_key(@key)
                raise Puppet::DevError, "Received invalid certificate"
            end
            return retrieved
        end

        # Remove the pid file
        def rmpidfile
            threadlock(:pidfile) do
                if defined? @pidfile and @pidfile and FileTest.exists?(@pidfile)
                    begin
                        File.unlink(@pidfile)
                    rescue => detail
                        Puppet.err "Could not remove PID file %s: %s" %
                            [@pidfile, detail]
                    end
                end
            end
        end

        # Create the pid file.
        def setpidfile
            return unless Puppet[:setpidfile]
            threadlock(:pidfile) do
                Puppet.config.use(:puppet)
                @pidfile = self.pidfile
                if FileTest.exists?(@pidfile)
                    if defined? $setpidfile
                        return
                    else
                        raise Puppet::Error, "A PID file already exists for #{Puppet.name}
    at #{@pidfile}.  Not starting."
                    end
                end

                Puppet.info "Creating PID file to %s" % @pidfile
                begin
                    File.open(@pidfile, "w") { |f| f.puts $$ }
                rescue => detail
                    Puppet.err "Could not create PID file: %s" % detail
                    exit(74)
                end
                $setpidfile = true
            end
        end

        # Shut down our server
        def shutdown
            # Remove our pid file
            rmpidfile()

            # And close all logs except the console.
            Puppet::Log.destinations.reject { |d| d == :console }.each do |dest|
                Puppet::Log.close(dest)
            end

            super
        end

        def start
            setpidfile()
            super
        end
    end
end

# $Id$