0000-12-31 18:09:24 -05:50
|
|
|
#! /usr/bin/python -E
|
|
|
|
# Authors: Karl MacMillan <kmacmillan@mentalrootkit.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2007 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License as
|
2008-02-04 14:15:52 -06:00
|
|
|
# published by the Free Software Foundation; version 2 only
|
0000-12-31 18:09:24 -05:50
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
|
|
|
import sys
|
2008-07-01 08:57:46 -05:00
|
|
|
import os
|
|
|
|
import pwd
|
2008-07-11 10:34:29 -05:00
|
|
|
import tempfile
|
0000-12-31 18:09:24 -05:50
|
|
|
|
|
|
|
import traceback
|
|
|
|
|
2010-03-24 09:51:31 -05:00
|
|
|
import krbV, getpass
|
0000-12-31 18:09:24 -05:50
|
|
|
|
2009-02-05 14:03:08 -06:00
|
|
|
from ipapython.ipautil import user_input
|
2009-09-10 08:24:59 -05:00
|
|
|
|
|
|
|
from ipaserver.install import certs, dsinstance, httpinstance, installutils
|
2010-03-10 10:02:50 -06:00
|
|
|
from ipalib import api
|
2010-03-24 09:51:31 -05:00
|
|
|
from ipaserver.plugins.ldap2 import ldap2
|
0000-12-31 18:09:24 -05:50
|
|
|
|
|
|
|
def get_realm_name():
|
|
|
|
c = krbV.default_context()
|
|
|
|
return c.default_realm
|
|
|
|
|
|
|
|
def parse_options():
|
|
|
|
from optparse import OptionParser
|
|
|
|
parser = OptionParser()
|
|
|
|
|
|
|
|
parser.add_option("-d", "--dirsrv", dest="dirsrv", action="store_true",
|
|
|
|
default=False, help="install certificate for the directory server")
|
|
|
|
parser.add_option("-w", "--http", dest="http", action="store_true",
|
|
|
|
default=False, help="install certificate for the http server")
|
2008-07-11 10:34:29 -05:00
|
|
|
parser.add_option("--dirsrv_pin", dest="dirsrv_pin",
|
|
|
|
help="The password of the Directory Server PKCS#12 file")
|
|
|
|
parser.add_option("--http_pin", dest="http_pin",
|
|
|
|
help="The password of the Apache Server PKCS#12 file")
|
0000-12-31 18:09:24 -05:50
|
|
|
|
|
|
|
options, args = parser.parse_args()
|
|
|
|
|
|
|
|
if not options.dirsrv and not options.http:
|
|
|
|
parser.error("you must specify dirsrv and/or http")
|
2008-07-11 10:34:29 -05:00
|
|
|
if ((options.dirsrv and not options.dirsrv_pin) or
|
|
|
|
(options.http and not options.http_pin)):
|
|
|
|
parser.error("you must provide the password for the PKCS#12 file")
|
0000-12-31 18:09:24 -05:50
|
|
|
|
|
|
|
if len(args) != 1:
|
|
|
|
parser.error("you must provide a pkcs12 filename")
|
|
|
|
|
|
|
|
return options, args[0]
|
|
|
|
|
|
|
|
def set_ds_cert_name(cert_name, dm_password):
|
2010-03-24 09:51:31 -05:00
|
|
|
ldapuri = 'ldap://127.0.0.1'
|
|
|
|
conn = ldap2(shared_instance=False, ldap_uri=ldapuri, base_dn='')
|
|
|
|
conn.connect(bind_dn='cn=directory manager', bind_pw=dm_password)
|
|
|
|
mod = {'nssslpersonalityssl': cert_name}
|
|
|
|
conn.update_entry('cn=RSA,cn=encryption,cn=config', mod)
|
|
|
|
conn.disconnect()
|
0000-12-31 18:09:24 -05:50
|
|
|
|
|
|
|
def choose_server_cert(server_certs):
|
|
|
|
print "Please select the certificate to use:"
|
|
|
|
num = 1
|
|
|
|
for cert in server_certs:
|
|
|
|
print "%d. %s" % (num, cert[0])
|
|
|
|
num += 1
|
2008-07-11 10:34:29 -05:00
|
|
|
|
0000-12-31 18:09:24 -05:50
|
|
|
while 1:
|
2008-07-21 05:25:37 -05:00
|
|
|
num = user_input("Certificate number", 1)
|
0000-12-31 18:09:24 -05:50
|
|
|
print ""
|
2008-07-21 05:25:37 -05:00
|
|
|
if num < 1 or num > len(server_certs):
|
|
|
|
print "number out of range"
|
0000-12-31 18:09:24 -05:50
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
2008-07-21 05:25:37 -05:00
|
|
|
return server_certs[num - 1]
|
2008-07-11 10:34:29 -05:00
|
|
|
|
|
|
|
def import_cert(dirname, pkcs12_fname, pkcs12_passwd, db_password):
|
2010-11-01 12:51:14 -05:00
|
|
|
cdb = certs.CertDB(dirname, api.env.realm)
|
2008-07-11 10:34:29 -05:00
|
|
|
cdb.create_passwd_file(db_password)
|
0000-12-31 18:09:24 -05:50
|
|
|
cdb.create_certdbs()
|
2008-07-11 10:34:29 -05:00
|
|
|
[pw_fd, pw_name] = tempfile.mkstemp()
|
|
|
|
os.write(pw_fd, pkcs12_passwd)
|
|
|
|
os.close(pw_fd)
|
|
|
|
|
0000-12-31 18:09:24 -05:50
|
|
|
try:
|
2008-07-11 10:34:29 -05:00
|
|
|
try:
|
|
|
|
cdb.import_pkcs12(pkcs12_fname, pw_name)
|
2010-03-10 10:02:50 -06:00
|
|
|
ca_names = cdb.find_root_cert_from_pkcs12(pkcs12_fname, pw_name)
|
2008-07-11 10:34:29 -05:00
|
|
|
except RuntimeError, e:
|
|
|
|
print str(e)
|
|
|
|
sys.exit(1)
|
|
|
|
finally:
|
|
|
|
os.remove(pw_name)
|
0000-12-31 18:09:24 -05:50
|
|
|
|
|
|
|
server_certs = cdb.find_server_certs()
|
|
|
|
if len(server_certs) == 0:
|
|
|
|
print "could not find a suitable server cert in import"
|
|
|
|
sys.exit(1)
|
|
|
|
elif len(server_certs) == 1:
|
|
|
|
server_cert = server_certs[0]
|
|
|
|
else:
|
|
|
|
server_cert = choose_server_cert(server_certs)
|
|
|
|
|
2010-03-10 10:02:50 -06:00
|
|
|
for ca in ca_names:
|
|
|
|
cdb.trust_root_cert(ca)
|
0000-12-31 18:09:24 -05:50
|
|
|
|
|
|
|
return server_cert
|
|
|
|
|
|
|
|
def main():
|
|
|
|
options, pkcs12_fname = parse_options()
|
|
|
|
|
2010-03-10 10:02:50 -06:00
|
|
|
cfg = dict(in_server=True,)
|
|
|
|
|
|
|
|
api.bootstrap(**cfg)
|
|
|
|
api.finalize()
|
|
|
|
|
0000-12-31 18:09:24 -05:50
|
|
|
try:
|
|
|
|
if options.dirsrv:
|
|
|
|
dm_password = getpass.getpass("Directory Manager password: ")
|
|
|
|
realm = get_realm_name()
|
2008-07-01 09:09:37 -05:00
|
|
|
dirname = dsinstance.config_dirname(dsinstance.realm_to_serverid(realm))
|
2008-07-11 10:34:29 -05:00
|
|
|
fd = open(dirname + "/pwdfile.txt")
|
|
|
|
passwd = fd.read()
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
server_cert = import_cert(dirname, pkcs12_fname, options.dirsrv_pin, passwd)
|
0000-12-31 18:09:24 -05:50
|
|
|
set_ds_cert_name(server_cert[0], dm_password)
|
|
|
|
|
|
|
|
if options.http:
|
|
|
|
dirname = httpinstance.NSS_DIR
|
2008-07-11 10:34:29 -05:00
|
|
|
server_cert = import_cert(dirname, pkcs12_fname, options.http_pin, "")
|
|
|
|
installutils.set_directive(httpinstance.NSS_CONF, 'NSSNickname', server_cert[0])
|
0000-12-31 18:09:24 -05:50
|
|
|
|
2008-07-01 08:57:46 -05:00
|
|
|
# Fix the database permissions
|
2008-07-03 14:18:07 -05:00
|
|
|
os.chmod(dirname + "/cert8.db", 0640)
|
|
|
|
os.chmod(dirname + "/key3.db", 0640)
|
|
|
|
os.chmod(dirname + "/secmod.db", 0640)
|
2008-07-01 08:57:46 -05:00
|
|
|
|
|
|
|
pent = pwd.getpwnam("apache")
|
2008-07-03 14:18:07 -05:00
|
|
|
os.chown(dirname + "/cert8.db", 0, pent.pw_gid )
|
|
|
|
os.chown(dirname + "/key3.db", 0, pent.pw_gid )
|
|
|
|
os.chown(dirname + "/secmod.db", 0, pent.pw_gid )
|
2008-07-01 08:57:46 -05:00
|
|
|
|
0000-12-31 18:09:24 -05:50
|
|
|
except Exception, e:
|
2010-11-08 16:13:48 -06:00
|
|
|
traceback.print_exc(file=sys.stderr)
|
|
|
|
sys.exit("an unexpected error occurred: %s" % str(e))
|
0000-12-31 18:09:24 -05:50
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
sys.exit(main())
|