2013-07-04 10:45:42 -05:00
|
|
|
# Authors: Karl MacMillan <kmacmillan@mentalrootkit.com>
|
|
|
|
# Jan Cholasta <jcholast@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2007-2013 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 published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import pwd
|
2013-08-20 01:39:39 -05:00
|
|
|
import optparse
|
2013-07-04 10:45:42 -05:00
|
|
|
|
2014-05-29 07:47:17 -05:00
|
|
|
from ipaplatform.paths import paths
|
2013-07-04 10:45:42 -05:00
|
|
|
from ipapython import admintool
|
|
|
|
from ipapython.dn import DN
|
|
|
|
from ipapython.ipautil import user_input, write_tmp_file
|
2013-07-04 11:43:08 -05:00
|
|
|
from ipalib import api, errors
|
2013-09-11 03:27:34 -05:00
|
|
|
from ipalib.constants import CACERT
|
2013-07-04 10:45:42 -05:00
|
|
|
from ipaserver.install import certs, dsinstance, httpinstance, installutils
|
2015-06-22 04:59:33 -05:00
|
|
|
|
2013-07-04 10:45:42 -05:00
|
|
|
|
|
|
|
class ServerCertInstall(admintool.AdminTool):
|
|
|
|
command_name = 'ipa-server-certinstall'
|
|
|
|
|
2014-09-24 09:41:47 -05:00
|
|
|
usage = "%prog <-d|-w> [options] <file> ..."
|
2013-07-04 10:45:42 -05:00
|
|
|
|
|
|
|
description = "Install new SSL server certificates."
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def add_options(cls, parser):
|
|
|
|
super(ServerCertInstall, cls).add_options(parser)
|
|
|
|
|
|
|
|
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")
|
|
|
|
parser.add_option(
|
2013-08-20 01:39:39 -05:00
|
|
|
"--pin",
|
2014-09-24 09:41:47 -05:00
|
|
|
dest="pin", metavar="PIN", sensitive=True,
|
2013-08-20 01:39:39 -05:00
|
|
|
help="The password of the PKCS#12 file")
|
2013-07-04 10:45:42 -05:00
|
|
|
parser.add_option(
|
2013-08-20 01:39:39 -05:00
|
|
|
"--dirsrv_pin", "--http_pin",
|
|
|
|
dest="pin",
|
|
|
|
help=optparse.SUPPRESS_HELP)
|
2014-09-24 09:48:15 -05:00
|
|
|
parser.add_option(
|
|
|
|
"--cert-name",
|
|
|
|
dest="cert_name", metavar="NAME",
|
|
|
|
help="Name of the certificate to install")
|
2013-08-27 11:06:24 -05:00
|
|
|
parser.add_option(
|
|
|
|
"-p", "--dirman-password",
|
|
|
|
dest="dirman_password",
|
|
|
|
help="Directory Manager password")
|
2013-07-04 10:45:42 -05:00
|
|
|
|
|
|
|
def validate_options(self):
|
|
|
|
super(ServerCertInstall, self).validate_options(needs_root=True)
|
|
|
|
|
|
|
|
installutils.check_server_configuration()
|
|
|
|
|
|
|
|
if not self.options.dirsrv and not self.options.http:
|
|
|
|
self.option_parser.error("you must specify dirsrv and/or http")
|
|
|
|
|
2014-09-24 09:41:47 -05:00
|
|
|
if not self.args:
|
|
|
|
self.option_parser.error("you must provide certificate filename")
|
2013-07-04 10:45:42 -05:00
|
|
|
|
|
|
|
def ask_for_options(self):
|
|
|
|
super(ServerCertInstall, self).ask_for_options()
|
|
|
|
|
2014-10-13 07:30:15 -05:00
|
|
|
if not self.options.dirman_password:
|
2013-08-27 11:06:24 -05:00
|
|
|
self.options.dirman_password = installutils.read_password(
|
2013-07-04 10:45:42 -05:00
|
|
|
"Directory Manager", confirm=False, validate=False, retry=False)
|
2013-08-27 11:06:24 -05:00
|
|
|
if self.options.dirman_password is None:
|
2013-07-04 10:45:42 -05:00
|
|
|
raise admintool.ScriptError(
|
|
|
|
"Directory Manager password required")
|
|
|
|
|
2013-09-25 03:40:05 -05:00
|
|
|
if self.options.pin is None:
|
2013-08-20 01:44:58 -05:00
|
|
|
self.options.pin = installutils.read_password(
|
2014-09-24 09:41:47 -05:00
|
|
|
"Enter private key unlock", confirm=False, validate=False)
|
2013-08-20 01:44:58 -05:00
|
|
|
if self.options.pin is None:
|
|
|
|
raise admintool.ScriptError(
|
2014-09-24 09:41:47 -05:00
|
|
|
"Private key unlock password required")
|
2013-08-20 01:44:58 -05:00
|
|
|
|
2013-07-04 10:45:42 -05:00
|
|
|
def run(self):
|
|
|
|
api.bootstrap(in_server=True)
|
|
|
|
api.finalize()
|
|
|
|
|
2014-10-13 07:30:15 -05:00
|
|
|
conn = api.Backend.ldap2
|
|
|
|
conn.connect(bind_dn=DN(('cn', 'directory manager')),
|
|
|
|
bind_pw=self.options.dirman_password)
|
|
|
|
|
2013-07-04 10:45:42 -05:00
|
|
|
if self.options.dirsrv:
|
|
|
|
self.install_dirsrv_cert()
|
|
|
|
|
|
|
|
if self.options.http:
|
|
|
|
self.install_http_cert()
|
|
|
|
|
2014-10-13 07:30:15 -05:00
|
|
|
conn.disconnect()
|
|
|
|
|
2013-07-04 10:45:42 -05:00
|
|
|
def install_dirsrv_cert(self):
|
2015-04-27 07:42:31 -05:00
|
|
|
serverid = installutils.realm_to_serverid(api.env.realm)
|
2013-07-04 10:45:42 -05:00
|
|
|
dirname = dsinstance.config_dirname(serverid)
|
|
|
|
|
2014-10-13 07:30:15 -05:00
|
|
|
conn = api.Backend.ldap2
|
2013-07-15 03:12:14 -05:00
|
|
|
entry = conn.get_entry(DN(('cn', 'RSA'), ('cn', 'encryption'),
|
|
|
|
('cn', 'config')),
|
|
|
|
['nssslpersonalityssl'])
|
2013-09-10 05:20:24 -05:00
|
|
|
old_cert = entry.single_value['nssslpersonalityssl']
|
2013-07-15 03:12:14 -05:00
|
|
|
|
2013-08-20 01:39:39 -05:00
|
|
|
server_cert = self.import_cert(dirname, self.options.pin,
|
2013-07-15 03:12:50 -05:00
|
|
|
old_cert, 'ldap/%s' % api.env.host,
|
|
|
|
'restart_dirsrv %s' % serverid)
|
2013-07-15 03:12:14 -05:00
|
|
|
|
|
|
|
entry['nssslpersonalityssl'] = [server_cert]
|
2013-07-04 11:43:08 -05:00
|
|
|
try:
|
|
|
|
conn.update_entry(entry)
|
|
|
|
except errors.EmptyModlist:
|
|
|
|
pass
|
2013-07-04 10:45:42 -05:00
|
|
|
|
|
|
|
def install_http_cert(self):
|
|
|
|
dirname = certs.NSS_DIR
|
|
|
|
|
2014-05-29 07:47:17 -05:00
|
|
|
old_cert = installutils.get_directive(paths.HTTPD_NSS_CONF,
|
2013-07-15 03:12:14 -05:00
|
|
|
'NSSNickname')
|
|
|
|
|
2013-08-20 01:39:39 -05:00
|
|
|
server_cert = self.import_cert(dirname, self.options.pin,
|
2013-07-15 03:12:50 -05:00
|
|
|
old_cert, 'HTTP/%s' % api.env.host,
|
|
|
|
'restart_httpd')
|
2013-07-04 10:45:42 -05:00
|
|
|
|
2014-05-29 07:47:17 -05:00
|
|
|
installutils.set_directive(paths.HTTPD_NSS_CONF,
|
2013-07-04 10:45:42 -05:00
|
|
|
'NSSNickname', server_cert)
|
|
|
|
|
|
|
|
# Fix the database permissions
|
2015-07-15 09:38:06 -05:00
|
|
|
os.chmod(os.path.join(dirname, 'cert8.db'), 0o640)
|
|
|
|
os.chmod(os.path.join(dirname, 'key3.db'), 0o640)
|
|
|
|
os.chmod(os.path.join(dirname, 'secmod.db'), 0o640)
|
2013-07-04 10:45:42 -05:00
|
|
|
|
|
|
|
pent = pwd.getpwnam("apache")
|
|
|
|
os.chown(os.path.join(dirname, 'cert8.db'), 0, pent.pw_gid)
|
|
|
|
os.chown(os.path.join(dirname, 'key3.db'), 0, pent.pw_gid)
|
|
|
|
os.chown(os.path.join(dirname, 'secmod.db'), 0, pent.pw_gid)
|
|
|
|
|
2013-07-15 03:12:50 -05:00
|
|
|
def import_cert(self, dirname, pkcs12_passwd, old_cert, principal, command):
|
2014-09-24 09:41:47 -05:00
|
|
|
pkcs12_file, pin, ca_cert = installutils.load_pkcs12(
|
|
|
|
cert_files=self.args,
|
|
|
|
key_password=pkcs12_passwd,
|
2014-09-24 09:48:15 -05:00
|
|
|
key_nickname=self.options.cert_name,
|
2014-09-24 09:41:47 -05:00
|
|
|
ca_cert_files=[CACERT],
|
|
|
|
host_name=api.env.host)
|
2013-07-04 10:45:42 -05:00
|
|
|
|
|
|
|
cdb = certs.CertDB(api.env.realm, nssdir=dirname)
|
|
|
|
try:
|
2014-10-13 07:30:15 -05:00
|
|
|
ca_enabled = api.Command.ca_is_enabled()['result']
|
|
|
|
if ca_enabled:
|
2013-07-15 03:12:50 -05:00
|
|
|
cdb.untrack_server_cert(old_cert)
|
|
|
|
|
2013-07-15 03:12:14 -05:00
|
|
|
cdb.delete_cert(old_cert)
|
2014-09-24 09:41:47 -05:00
|
|
|
cdb.import_pkcs12(pkcs12_file.name, pin)
|
2014-08-05 02:06:39 -05:00
|
|
|
server_cert = cdb.find_server_certs()[0][0]
|
2013-07-15 03:12:50 -05:00
|
|
|
|
2014-10-13 07:30:15 -05:00
|
|
|
if ca_enabled:
|
2013-07-15 03:12:50 -05:00
|
|
|
cdb.track_server_cert(server_cert, principal, cdb.passwd_fname,
|
|
|
|
command)
|
2013-07-04 10:45:42 -05:00
|
|
|
except RuntimeError, e:
|
|
|
|
raise admintool.ScriptError(str(e))
|
|
|
|
|
|
|
|
return server_cert
|