2007-10-15 14:42:12 -05:00
|
|
|
# Authors: Rob Crittenden <rcritten@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2007 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
2010-12-09 06:59:11 -06:00
|
|
|
# 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.
|
2007-10-15 14:42:12 -05:00
|
|
|
#
|
|
|
|
# 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
|
2010-12-09 06:59:11 -06:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-15 14:42:12 -05:00
|
|
|
#
|
|
|
|
|
2007-12-13 03:31:28 -06:00
|
|
|
import os
|
|
|
|
import os.path
|
2007-10-15 14:42:12 -05:00
|
|
|
import tempfile
|
|
|
|
import logging
|
|
|
|
import pwd
|
2007-12-12 08:36:32 -06:00
|
|
|
import shutil
|
0000-12-31 18:09:24 -05:50
|
|
|
|
|
|
|
import service
|
0000-12-31 18:09:24 -05:50
|
|
|
import certs
|
|
|
|
import dsinstance
|
0000-12-31 18:09:24 -05:50
|
|
|
import installutils
|
2009-02-05 14:03:08 -06:00
|
|
|
from ipapython import sysrestore
|
|
|
|
from ipapython import ipautil
|
2010-11-01 12:51:14 -05:00
|
|
|
from ipalib import util, api
|
2007-10-15 14:42:12 -05:00
|
|
|
|
|
|
|
HTTPD_DIR = "/etc/httpd"
|
|
|
|
SSL_CONF = HTTPD_DIR + "/conf.d/ssl.conf"
|
|
|
|
NSS_CONF = HTTPD_DIR + "/conf.d/nss.conf"
|
|
|
|
|
0000-12-31 18:09:24 -05:50
|
|
|
selinux_warning = """WARNING: could not set selinux boolean httpd_can_network_connect to true.
|
|
|
|
The web interface may not function correctly until this boolean is
|
|
|
|
successfully change with the command:
|
|
|
|
/usr/sbin/setsebool -P httpd_can_network_connect true
|
|
|
|
Try updating the policycoreutils and selinux-policy packages.
|
|
|
|
"""
|
|
|
|
|
2008-01-22 05:58:06 -06:00
|
|
|
class WebGuiInstance(service.SimpleServiceInstance):
|
|
|
|
def __init__(self):
|
|
|
|
service.SimpleServiceInstance.__init__(self, "ipa_webgui")
|
|
|
|
|
0000-12-31 18:09:24 -05:50
|
|
|
class HTTPInstance(service.Service):
|
2008-03-27 18:01:38 -05:00
|
|
|
def __init__(self, fstore = None):
|
0000-12-31 18:09:24 -05:50
|
|
|
service.Service.__init__(self, "httpd")
|
2008-03-27 18:01:38 -05:00
|
|
|
if fstore:
|
|
|
|
self.fstore = fstore
|
|
|
|
else:
|
|
|
|
self.fstore = sysrestore.FileStore('/var/lib/ipa/sysrestore')
|
2007-10-15 14:42:12 -05:00
|
|
|
|
2010-01-20 10:26:20 -06:00
|
|
|
def create_instance(self, realm, fqdn, domain_name, dm_password=None, autoconfig=True, pkcs12_info=None, self_signed_ca=False, subject_base=None):
|
0000-12-31 18:09:24 -05:50
|
|
|
self.fqdn = fqdn
|
|
|
|
self.realm = realm
|
2008-02-15 19:47:29 -06:00
|
|
|
self.domain = domain_name
|
2009-12-07 22:17:00 -06:00
|
|
|
self.dm_password = dm_password
|
|
|
|
self.suffix = util.realm_to_suffix(self.realm)
|
2008-02-05 11:23:53 -06:00
|
|
|
self.pkcs12_info = pkcs12_info
|
2009-04-13 12:39:15 -05:00
|
|
|
self.self_signed_ca = self_signed_ca
|
2009-12-07 22:17:00 -06:00
|
|
|
self.principal = "HTTP/%s@%s" % (self.fqdn, self.realm)
|
|
|
|
self.dercert = None
|
2010-01-20 10:26:20 -06:00
|
|
|
self.subject_base = subject_base
|
2007-12-12 08:36:32 -06:00
|
|
|
self.sub_dict = { "REALM" : realm, "FQDN": fqdn, "DOMAIN" : self.domain }
|
2008-03-27 18:01:38 -05:00
|
|
|
|
2011-01-05 06:46:30 -06:00
|
|
|
# get a connection to the DS
|
|
|
|
self.ldap_connect()
|
|
|
|
|
|
|
|
|
2007-12-13 03:31:28 -06:00
|
|
|
self.step("disabling mod_ssl in httpd", self.__disable_mod_ssl)
|
2011-02-02 09:24:30 -06:00
|
|
|
self.step("setting mod_nss port to 443", self.__set_mod_nss_port)
|
|
|
|
self.step("setting mod_nss password file", self.__set_mod_nss_passwordfile)
|
|
|
|
self.step("adding URL rewriting rules", self.__add_include)
|
2007-12-13 03:31:28 -06:00
|
|
|
self.step("configuring httpd", self.__configure_http)
|
2011-02-02 09:24:30 -06:00
|
|
|
self.step("setting up ssl", self.__setup_ssl)
|
2008-02-05 11:23:53 -06:00
|
|
|
if autoconfig:
|
2011-02-02 09:24:30 -06:00
|
|
|
self.step("setting up browser autoconfig", self.__setup_autoconfig)
|
2009-04-13 12:39:15 -05:00
|
|
|
self.step("publish CA cert", self.__publish_ca_cert)
|
2009-12-07 22:17:00 -06:00
|
|
|
self.step("creating a keytab for httpd", self.__create_http_keytab)
|
2007-12-13 03:31:28 -06:00
|
|
|
self.step("configuring SELinux for httpd", self.__selinux_config)
|
2008-01-14 11:43:26 -06:00
|
|
|
self.step("restarting httpd", self.__start)
|
|
|
|
self.step("configuring httpd to start on boot", self.__enable)
|
2007-12-13 03:31:28 -06:00
|
|
|
|
2010-09-29 12:55:54 -05:00
|
|
|
self.start_creation("Configuring the web interface", 60)
|
0000-12-31 18:09:24 -05:50
|
|
|
|
2008-01-14 11:43:26 -06:00
|
|
|
def __start(self):
|
|
|
|
self.backup_state("running", self.is_running())
|
|
|
|
self.restart()
|
|
|
|
|
|
|
|
def __enable(self):
|
|
|
|
self.backup_state("enabled", self.is_running())
|
2010-12-04 14:42:14 -06:00
|
|
|
# We do not let the system start IPA components on its own,
|
|
|
|
# Instead we reply on the IPA init script to start only enabled
|
|
|
|
# components as found in our LDAP configuration tree
|
|
|
|
self.ldap_enable('HTTP', self.fqdn, self.dm_password, self.suffix)
|
2008-01-14 11:43:26 -06:00
|
|
|
|
0000-12-31 18:09:24 -05:50
|
|
|
def __selinux_config(self):
|
|
|
|
selinux=0
|
2007-10-15 14:42:12 -05:00
|
|
|
try:
|
0000-12-31 18:09:24 -05:50
|
|
|
if (os.path.exists('/usr/sbin/selinuxenabled')):
|
2007-12-13 03:31:28 -06:00
|
|
|
ipautil.run(["/usr/sbin/selinuxenabled"])
|
0000-12-31 18:09:24 -05:50
|
|
|
selinux=1
|
2007-11-30 14:53:02 -06:00
|
|
|
except ipautil.CalledProcessError:
|
0000-12-31 18:09:24 -05:50
|
|
|
# selinuxenabled returns 1 if not enabled
|
|
|
|
pass
|
|
|
|
|
|
|
|
if selinux:
|
2008-01-14 11:43:26 -06:00
|
|
|
try:
|
|
|
|
# returns e.g. "httpd_can_network_connect --> off"
|
2009-11-30 14:28:09 -06:00
|
|
|
(stdout, stderr, returncode) = ipautil.run(["/usr/sbin/getsebool",
|
2009-08-11 16:08:09 -05:00
|
|
|
"httpd_can_network_connect"])
|
2008-01-14 11:43:26 -06:00
|
|
|
self.backup_state("httpd_can_network_connect", stdout.split()[2])
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
0000-12-31 18:09:24 -05:50
|
|
|
# Allow apache to connect to the turbogears web gui
|
|
|
|
# This can still fail even if selinux is enabled
|
|
|
|
try:
|
2007-12-13 03:31:28 -06:00
|
|
|
ipautil.run(["/usr/sbin/setsebool", "-P", "httpd_can_network_connect", "true"])
|
0000-12-31 18:09:24 -05:50
|
|
|
except:
|
|
|
|
self.print_msg(selinux_warning)
|
2008-03-27 18:01:38 -05:00
|
|
|
|
0000-12-31 18:09:24 -05:50
|
|
|
def __create_http_keytab(self):
|
2010-09-08 21:11:31 -05:00
|
|
|
installutils.kadmin_addprinc(self.principal)
|
|
|
|
installutils.create_keytab("/etc/httpd/conf/ipa.keytab", self.principal)
|
|
|
|
self.move_service(self.principal)
|
2009-12-07 22:17:00 -06:00
|
|
|
self.add_cert_to_service()
|
2007-10-15 14:42:12 -05:00
|
|
|
|
0000-12-31 18:09:24 -05:50
|
|
|
pent = pwd.getpwnam("apache")
|
|
|
|
os.chown("/etc/httpd/conf/ipa.keytab", pent.pw_uid, pent.pw_gid)
|
2007-10-15 14:42:12 -05:00
|
|
|
|
0000-12-31 18:09:24 -05:50
|
|
|
def __configure_http(self):
|
2007-12-13 03:31:28 -06:00
|
|
|
http_txt = ipautil.template_file(ipautil.SHARE_DIR + "ipa.conf", self.sub_dict)
|
2008-03-27 18:01:38 -05:00
|
|
|
self.fstore.backup_file("/etc/httpd/conf.d/ipa.conf")
|
0000-12-31 18:09:24 -05:50
|
|
|
http_fd = open("/etc/httpd/conf.d/ipa.conf", "w")
|
|
|
|
http_fd.write(http_txt)
|
2008-02-21 15:25:09 -06:00
|
|
|
http_fd.close()
|
2007-10-15 14:42:12 -05:00
|
|
|
|
2008-02-21 15:25:09 -06:00
|
|
|
http_txt = ipautil.template_file(ipautil.SHARE_DIR + "ipa-rewrite.conf", self.sub_dict)
|
2008-03-27 18:01:38 -05:00
|
|
|
self.fstore.backup_file("/etc/httpd/conf.d/ipa-rewrite.conf")
|
2008-02-21 15:25:09 -06:00
|
|
|
http_fd = open("/etc/httpd/conf.d/ipa-rewrite.conf", "w")
|
|
|
|
http_fd.write(http_txt)
|
|
|
|
http_fd.close()
|
2007-10-15 14:42:12 -05:00
|
|
|
|
|
|
|
def __disable_mod_ssl(self):
|
|
|
|
if os.path.exists(SSL_CONF):
|
2008-03-27 18:01:38 -05:00
|
|
|
self.fstore.backup_file(SSL_CONF)
|
2008-01-14 11:43:26 -06:00
|
|
|
os.unlink(SSL_CONF)
|
2007-10-15 14:42:12 -05:00
|
|
|
|
|
|
|
def __set_mod_nss_port(self):
|
2008-03-27 18:01:38 -05:00
|
|
|
self.fstore.backup_file(NSS_CONF)
|
0000-12-31 18:09:24 -05:50
|
|
|
if installutils.update_file(NSS_CONF, '8443', '443') != 0:
|
2008-02-21 15:25:09 -06:00
|
|
|
print "Updating port in %s failed." % NSS_CONF
|
|
|
|
|
2008-07-11 10:34:29 -05:00
|
|
|
def __set_mod_nss_nickname(self, nickname):
|
|
|
|
installutils.set_directive(NSS_CONF, 'NSSNickname', nickname)
|
|
|
|
|
2009-04-13 12:39:15 -05:00
|
|
|
def __set_mod_nss_passwordfile(self):
|
|
|
|
installutils.set_directive(NSS_CONF, 'NSSPassPhraseDialog', 'file:/etc/httpd/conf/password.conf')
|
|
|
|
|
2008-02-21 15:25:09 -06:00
|
|
|
def __add_include(self):
|
|
|
|
"""This should run after __set_mod_nss_port so is already backed up"""
|
|
|
|
if installutils.update_file(NSS_CONF, '</VirtualHost>', 'Include conf.d/ipa-rewrite.conf\n</VirtualHost>') != 0:
|
|
|
|
print "Adding Include conf.d/ipa-rewrite to %s failed." % NSS_CONF
|
0000-12-31 18:09:24 -05:50
|
|
|
|
|
|
|
def __setup_ssl(self):
|
2010-12-08 15:35:12 -06:00
|
|
|
fqdn = None
|
|
|
|
if not self.self_signed_ca:
|
|
|
|
fqdn = self.fqdn
|
|
|
|
|
|
|
|
ca_db = certs.CertDB(self.realm, host_name=fqdn, subject_base=self.subject_base)
|
|
|
|
|
|
|
|
db = certs.CertDB(self.realm, subject_base=self.subject_base)
|
2008-02-05 11:23:53 -06:00
|
|
|
if self.pkcs12_info:
|
2009-04-13 12:39:15 -05:00
|
|
|
db.create_from_pkcs12(self.pkcs12_info[0], self.pkcs12_info[1], passwd="")
|
|
|
|
server_certs = db.find_server_certs()
|
2008-07-11 10:34:29 -05:00
|
|
|
if len(server_certs) == 0:
|
2009-08-11 16:08:09 -05:00
|
|
|
raise RuntimeError("Could not find a suitable server cert in import in %s" % self.pkcs12_info[0])
|
2008-07-11 10:34:29 -05:00
|
|
|
|
2009-07-24 08:29:33 -05:00
|
|
|
db.create_password_conf()
|
2008-07-11 10:34:29 -05:00
|
|
|
# We only handle one server cert
|
|
|
|
nickname = server_certs[0][0]
|
2009-12-07 22:17:00 -06:00
|
|
|
self.dercert = db.get_cert_from_db(nickname)
|
2011-03-14 15:27:19 -05:00
|
|
|
db.track_server_cert(nickname, self.principal, db.passwd_fname)
|
2008-07-11 10:34:29 -05:00
|
|
|
|
|
|
|
self.__set_mod_nss_nickname(nickname)
|
2008-02-05 11:23:53 -06:00
|
|
|
else:
|
2009-04-13 12:39:15 -05:00
|
|
|
if self.self_signed_ca:
|
|
|
|
db.create_from_cacert(ca_db.cacert_fname)
|
2010-12-08 15:35:12 -06:00
|
|
|
|
|
|
|
db.create_password_conf()
|
|
|
|
self.dercert = db.create_server_cert("Server-Cert", self.fqdn, ca_db)
|
|
|
|
db.track_server_cert("Server-Cert", self.principal, db.passwd_fname)
|
|
|
|
db.create_signing_cert("Signing-Cert", "Object Signing Cert", ca_db)
|
2007-12-12 08:36:32 -06:00
|
|
|
|
2008-04-28 14:28:13 -05:00
|
|
|
# Fix the database permissions
|
2010-12-08 15:35:12 -06:00
|
|
|
os.chmod(certs.NSS_DIR + "/cert8.db", 0660)
|
|
|
|
os.chmod(certs.NSS_DIR + "/key3.db", 0660)
|
|
|
|
os.chmod(certs.NSS_DIR + "/secmod.db", 0660)
|
|
|
|
os.chmod(certs.NSS_DIR + "/pwdfile.txt", 0660)
|
2008-04-28 14:28:13 -05:00
|
|
|
|
|
|
|
pent = pwd.getpwnam("apache")
|
2010-12-08 15:35:12 -06:00
|
|
|
os.chown(certs.NSS_DIR + "/cert8.db", 0, pent.pw_gid )
|
|
|
|
os.chown(certs.NSS_DIR + "/key3.db", 0, pent.pw_gid )
|
|
|
|
os.chown(certs.NSS_DIR + "/secmod.db", 0, pent.pw_gid )
|
|
|
|
os.chown(certs.NSS_DIR + "/pwdfile.txt", 0, pent.pw_gid )
|
2009-09-10 15:15:14 -05:00
|
|
|
|
|
|
|
# Fix SELinux permissions on the database
|
2010-12-08 15:35:12 -06:00
|
|
|
ipautil.run(["/sbin/restorecon", certs.NSS_DIR + "/cert8.db"])
|
|
|
|
ipautil.run(["/sbin/restorecon", certs.NSS_DIR + "/key3.db"])
|
2009-09-10 15:15:14 -05:00
|
|
|
|
|
|
|
# In case this got generated as part of the install, reset the
|
|
|
|
# context
|
|
|
|
if ipautil.file_exists(certs.CA_SERIALNO):
|
|
|
|
ipautil.run(["/sbin/restorecon", certs.CA_SERIALNO])
|
|
|
|
os.chown(certs.CA_SERIALNO, 0, pent.pw_gid)
|
|
|
|
os.chmod(certs.CA_SERIALNO, 0664)
|
2008-04-28 14:28:13 -05:00
|
|
|
|
2007-12-12 08:36:32 -06:00
|
|
|
def __setup_autoconfig(self):
|
2007-12-13 03:31:28 -06:00
|
|
|
prefs_txt = ipautil.template_file(ipautil.SHARE_DIR + "preferences.html.template", self.sub_dict)
|
2007-12-12 08:36:32 -06:00
|
|
|
prefs_fd = open("/usr/share/ipa/html/preferences.html", "w")
|
|
|
|
prefs_fd.write(prefs_txt)
|
2008-03-27 18:01:38 -05:00
|
|
|
prefs_fd.close()
|
2007-12-12 08:36:32 -06:00
|
|
|
|
|
|
|
# The signing cert is generated in __setup_ssl
|
2010-12-08 15:35:12 -06:00
|
|
|
db = certs.CertDB(self.realm, subject_base=self.subject_base)
|
2007-12-12 08:36:32 -06:00
|
|
|
|
2009-09-10 15:15:14 -05:00
|
|
|
pwdfile = open(db.passwd_fname)
|
|
|
|
pwd = pwdfile.read()
|
|
|
|
pwdfile.close()
|
|
|
|
|
2008-01-11 04:36:25 -06:00
|
|
|
tmpdir = tempfile.mkdtemp(prefix = "tmp-")
|
|
|
|
shutil.copy("/usr/share/ipa/html/preferences.html", tmpdir)
|
2009-04-13 12:39:15 -05:00
|
|
|
db.run_signtool(["-k", "Signing-Cert",
|
2007-12-12 08:36:32 -06:00
|
|
|
"-Z", "/usr/share/ipa/html/configure.jar",
|
2009-09-10 15:15:14 -05:00
|
|
|
"-e", ".html", "-p", pwd,
|
2008-01-11 04:36:25 -06:00
|
|
|
tmpdir])
|
|
|
|
shutil.rmtree(tmpdir)
|
2008-01-11 05:57:36 -06:00
|
|
|
|
2009-04-13 12:39:15 -05:00
|
|
|
def __publish_ca_cert(self):
|
2010-12-08 15:35:12 -06:00
|
|
|
ca_db = certs.CertDB(self.realm)
|
|
|
|
ca_db.publish_ca_cert("/usr/share/ipa/html/ca.crt")
|
2009-04-13 12:39:15 -05:00
|
|
|
|
2008-01-11 05:57:36 -06:00
|
|
|
def uninstall(self):
|
2010-05-03 14:21:51 -05:00
|
|
|
if self.is_configured():
|
|
|
|
self.print_msg("Unconfiguring web server")
|
|
|
|
|
2008-01-11 05:57:36 -06:00
|
|
|
running = self.restore_state("running")
|
|
|
|
enabled = self.restore_state("enabled")
|
|
|
|
|
|
|
|
if not running is None:
|
|
|
|
self.stop()
|
|
|
|
|
2010-12-08 15:35:12 -06:00
|
|
|
db = certs.CertDB(api.env.realm)
|
2010-09-08 21:11:31 -05:00
|
|
|
db.untrack_server_cert("Server-Cert")
|
2008-01-11 05:57:36 -06:00
|
|
|
if not enabled is None and not enabled:
|
|
|
|
self.chkconfig_off()
|
|
|
|
|
|
|
|
for f in ["/etc/httpd/conf.d/ipa.conf", SSL_CONF, NSS_CONF]:
|
2008-03-27 18:01:38 -05:00
|
|
|
try:
|
|
|
|
self.fstore.restore_file(f)
|
|
|
|
except ValueError, error:
|
|
|
|
logging.debug(error)
|
|
|
|
pass
|
2008-01-11 05:57:36 -06:00
|
|
|
|
|
|
|
sebool_state = self.restore_state("httpd_can_network_connect")
|
|
|
|
if not sebool_state is None:
|
|
|
|
try:
|
|
|
|
ipautil.run(["/usr/sbin/setsebool", "-P", "httpd_can_network_connect", sebool_state])
|
|
|
|
except:
|
|
|
|
self.print_msg(selinux_warning)
|
|
|
|
|
|
|
|
if not running is None and running:
|
|
|
|
self.start()
|