61721ac7c255d6e7197bb426089d1f07b57ba4c1
2 Copyright 2007-2008, Red Hat, Inc
5 This software may be freely redistributed under the terms of the GNU
6 general public license.
8 You should have received a copy of the GNU General Public License
9 along with this program; if not, write to the Free Software
10 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 from config
import read_config
25 from commonconfig
import MinionConfig
29 # FIXME: module needs better pydoc
32 # FIXME: can remove this constant?
33 REMOTE_ERROR
= "REMOTE_ERROR"
37 x
= traceback
.extract_stack()
38 bar
= string
.join(traceback
.format_list(x
))
41 def daemonize(pidfile
=None):
43 Daemonize this process with the UNIX double-fork trick.
44 Writes the new PID to the provided file name if not None.
56 if pidfile
is not None:
57 open(pidfile
, "w").write(str(pid
))
61 def nice_exception(etype
, evalue
, etb
):
64 lefti
= etype
.index("'") + 1
65 righti
= etype
.rindex("'")
66 nicetype
= etype
[lefti
:righti
]
69 nicestack
= string
.join(traceback
.format_list(traceback
.extract_tb(etb
)))
70 return [ REMOTE_ERROR
, nicetype
, str(evalue
), nicestack
]
73 # FIXME: I believe we can remove this function
74 if type(result
) != list:
78 if result
[0] == REMOTE_ERROR
:
82 def get_hostname(talk_to_certmaster
=True):
84 "localhost" is a lame hostname to use for a key, so try to get
85 a more meaningful hostname. We do this by connecting to the certmaster
86 and seeing what interface/ip it uses to make that connection, and looking
87 up the hostname for that.
89 # FIXME: this code ignores http proxies (which granted, we don't
90 # support elsewhere either. It also hardcodes the port number
91 # for the certmaster for now
93 hostname
= socket
.gethostname()
94 # print "DEBUG: HOSTNAME TRY1: %s" % hostname
96 ip
= socket
.gethostbyname(hostname
)
97 # print "DEBUG: IP TRY2: %s" % ip
99 # print "DEBUG: ERROR: returning"
101 if ip
!= "127.0.0.1":
102 # print "DEBUG: ERROR: returning 2"
105 if talk_to_certmaster
:
106 config_file
= '/etc/certmaster/minion.conf'
107 config
= read_config(config_file
, MinionConfig
)
109 server
= config
.certmaster
115 # print "server, port", server, port
116 s
.connect((server
, port
))
117 (intf
, port
) = s
.getsockname()
118 remote_hostname
= socket
.gethostbyaddr(intf
)[0]
119 if remote_hostname
!= "localhost":
120 hostname
= remote_hostname
121 # print "DEBUG: HOSTNAME FROM CERTMASTER == %s" % hostname
127 # print "DEBUG: final hostname=%s" % hostname
131 # FIXME: move to requestor module and also create a verbose mode
132 # prints to the screen for usage by /usr/bin/certmaster-request
134 def create_minion_keys():
135 # FIXME: paths should not be hard coded here, move to settings universally
136 config_file
= '/etc/certmaster/minion.conf'
137 config
= read_config(config_file
, MinionConfig
)
138 cert_dir
= config
.cert_dir
139 master_uri
= 'http://%s:51235/' % config
.certmaster
140 # print "DEBUG: acquiring hostname"
142 # print "DEBUG: hostname = %s\n" % hn
145 raise codes
.CMException("Could not determine a hostname other than localhost")
147 key_file
= '%s/%s.pem' % (cert_dir
, hn
)
148 csr_file
= '%s/%s.csr' % (cert_dir
, hn
)
149 cert_file
= '%s/%s.cert' % (cert_dir
, hn
)
150 ca_cert_file
= '%s/ca.cert' % cert_dir
153 if os
.path
.exists(cert_file
) and os
.path
.exists(ca_cert_file
):
154 # print "DEBUG: err, no cert_file"
159 if not os
.path
.exists(cert_dir
):
160 os
.makedirs(cert_dir
)
161 if not os
.path
.exists(key_file
):
162 keypair
= certs
.make_keypair(dest
=key_file
)
163 if not os
.path
.exists(csr_file
):
165 keypair
= certs
.retrieve_key_from_file(key_file
)
166 csr
= certs
.make_csr(keypair
, dest
=csr_file
)
168 traceback
.print_exc()
169 raise codes
.CMException
, "Could not create local keypair or csr for session"
172 log
= logger
.Logger().logger
175 # print "DEBUG: submitting CSR to certmaster: %s" % master_uri
176 log
.debug("submitting CSR to certmaster %s" % master_uri
)
177 result
, cert_string
, ca_cert_string
= submit_csr_to_master(csr_file
, master_uri
)
178 except socket
.gaierror
, e
:
179 raise codes
.CMException
, "Could not locate certmaster at %s" % master_uri
181 # logging here would be nice
183 # print "DEBUG: no response from certmaster, sleeping 10 seconds"
184 log
.warning("no response from certmaster %s, sleeping 10 seconds" % master_uri
)
189 # print "DEBUG: recieved certificate from certmaster"
190 log
.debug("received certificate from certmaster %s, storing to %s" % (master_uri
, cert_file
))
191 cert_fd
= os
.open(cert_file
, os
.O_RDWR|os
.O_CREAT
, 0644)
192 os
.write(cert_fd
, cert_string
)
195 ca_cert_fd
= os
.open(ca_cert_file
, os
.O_RDWR|os
.O_CREAT
, 0644)
196 os
.write(ca_cert_fd
, ca_cert_string
)
199 def run_triggers(ref
, globber
):
201 Runs all the trigger scripts in a given directory.
202 ref can be a certmaster object, if not None, the name will be passed
203 to the script. If ref is None, the script will be called with
204 no argumenets. Globber is a wildcard expression indicating which
205 triggers to run. Example: "/var/lib/certmaster/triggers/blah/*"
208 log
= logger
.Logger().logger
209 triggers
= glob
.glob(globber
)
211 for file in triggers
:
212 log
.debug("Executing trigger: %s" % file)
214 if file.find(".rpm") != -1:
215 # skip .rpmnew files that may have been installed
216 # in the triggers directory
219 rc
= sub_process
.call([file, ref
.name
], shell
=False)
221 rc
= sub_process
.call([file], shell
=False)
223 log
.warning("Warning: failed to execute trigger: %s" % file)
227 raise codes
.CMException
, "certmaster trigger failed: %(file)s returns %(code)d" % { "file" : file, "code" : rc
}
230 def submit_csr_to_master(csr_file
, master_uri
):
232 gets us our cert back from the certmaster.wait_for_cert() method
233 takes csr_file as path location and master_uri
234 returns Bool, str(cert), str(ca_cert)
239 s
= xmlrpclib
.ServerProxy(master_uri
)
241 # print "DEBUG: waiting for cert"
242 return s
.wait_for_cert(csr
)