2007-09-20 14:10:21 -05:00
|
|
|
# Authors: Simo Sorce <ssorce@redhat.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
|
2007-09-20 14:10:21 -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
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
|
|
|
import string
|
|
|
|
import tempfile
|
|
|
|
import shutil
|
|
|
|
import os
|
|
|
|
import socket
|
2008-03-27 18:01:38 -05:00
|
|
|
import logging
|
2007-09-20 14:10:21 -05:00
|
|
|
|
2007-12-13 03:31:28 -06:00
|
|
|
import service
|
2009-02-05 14:03:08 -06:00
|
|
|
from ipapython import sysrestore
|
|
|
|
from ipapython import ipautil
|
2007-12-13 03:31:28 -06:00
|
|
|
|
2008-05-28 21:46:08 -05:00
|
|
|
def check_inst():
|
|
|
|
# So far this file is always present in both RHEL5 and Fedora if all the necessary
|
|
|
|
# bind packages are installed (RHEL5 requires also the pkg: caching-nameserver)
|
|
|
|
if not os.path.exists('/etc/named.rfc1912.zones'):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2007-12-13 03:31:28 -06:00
|
|
|
class BindInstance(service.Service):
|
2008-03-27 18:01:38 -05:00
|
|
|
def __init__(self, fstore=None):
|
2007-12-13 03:31:28 -06:00
|
|
|
service.Service.__init__(self, "named")
|
2007-09-20 14:10:21 -05:00
|
|
|
self.fqdn = None
|
|
|
|
self.domain = None
|
|
|
|
self.host = None
|
|
|
|
self.ip_address = None
|
|
|
|
self.realm = None
|
|
|
|
self.sub_dict = None
|
|
|
|
|
2008-03-27 18:01:38 -05:00
|
|
|
if fstore:
|
|
|
|
self.fstore = fstore
|
|
|
|
else:
|
|
|
|
self.fstore = sysrestore.FileStore('/var/lib/ipa/sysrestore')
|
|
|
|
|
2008-02-15 19:47:29 -06:00
|
|
|
def setup(self, fqdn, ip_address, realm_name, domain_name):
|
2007-09-20 14:10:21 -05:00
|
|
|
self.fqdn = fqdn
|
|
|
|
self.ip_address = ip_address
|
|
|
|
self.realm = realm_name
|
2008-02-15 19:47:29 -06:00
|
|
|
self.domain = domain_name
|
2008-05-15 10:33:07 -05:00
|
|
|
self.host = fqdn.split(".")[0]
|
2007-09-20 14:10:21 -05:00
|
|
|
|
|
|
|
self.__setup_sub_dict()
|
|
|
|
|
|
|
|
def create_sample_bind_zone(self):
|
2007-12-13 03:31:28 -06:00
|
|
|
bind_txt = ipautil.template_file(ipautil.SHARE_DIR + "bind.zone.db.template", self.sub_dict)
|
2007-09-20 14:10:21 -05:00
|
|
|
[bind_fd, bind_name] = tempfile.mkstemp(".db","sample.zone.")
|
|
|
|
os.write(bind_fd, bind_txt)
|
|
|
|
os.close(bind_fd)
|
|
|
|
print "Sample zone file for bind has been created in "+bind_name
|
|
|
|
|
|
|
|
def create_instance(self):
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.stop()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2008-03-27 18:01:38 -05:00
|
|
|
self.step("Setting up our zone", self.__setup_zone)
|
|
|
|
self.step("Setting up named.conf", self.__setup_named_conf)
|
|
|
|
|
|
|
|
self.step("restarting named", self.__start)
|
|
|
|
self.step("configuring named to start on boot", self.__enable)
|
|
|
|
|
2008-05-13 12:35:20 -05:00
|
|
|
self.step("Changing resolv.conf to point to ourselves", self.__setup_resolv_conf)
|
2008-04-02 21:04:24 -05:00
|
|
|
self.start_creation("Configuring bind:")
|
2007-09-20 14:10:21 -05:00
|
|
|
|
2008-03-27 18:01:38 -05:00
|
|
|
def __start(self):
|
2007-10-03 16:37:13 -05:00
|
|
|
try:
|
2008-01-14 11:43:26 -06:00
|
|
|
self.backup_state("running", self.is_running())
|
2008-03-27 18:01:38 -05:00
|
|
|
self.restart()
|
2007-10-03 16:37:13 -05:00
|
|
|
except:
|
|
|
|
print "named service failed to start"
|
2007-09-20 14:10:21 -05:00
|
|
|
|
2008-03-27 18:01:38 -05:00
|
|
|
def __enable(self):
|
|
|
|
self.backup_state("enabled", self.is_running())
|
|
|
|
self.chkconfig_on()
|
|
|
|
|
2007-09-20 14:10:21 -05:00
|
|
|
def __setup_sub_dict(self):
|
|
|
|
self.sub_dict = dict(FQDN=self.fqdn,
|
|
|
|
IP=self.ip_address,
|
|
|
|
DOMAIN=self.domain,
|
|
|
|
HOST=self.host,
|
|
|
|
REALM=self.realm)
|
|
|
|
|
|
|
|
def __setup_zone(self):
|
2008-01-14 11:43:26 -06:00
|
|
|
self.backup_state("domain", self.domain)
|
2007-12-13 03:31:28 -06:00
|
|
|
zone_txt = ipautil.template_file(ipautil.SHARE_DIR + "bind.zone.db.template", self.sub_dict)
|
2008-03-27 18:01:38 -05:00
|
|
|
self.fstore.backup_file('/var/named/'+self.domain+'.zone.db')
|
2007-09-20 14:10:21 -05:00
|
|
|
zone_fd = open('/var/named/'+self.domain+'.zone.db', 'w')
|
|
|
|
zone_fd.write(zone_txt)
|
|
|
|
zone_fd.close()
|
|
|
|
|
|
|
|
def __setup_named_conf(self):
|
2008-03-27 18:01:38 -05:00
|
|
|
self.fstore.backup_file('/etc/named.conf')
|
2007-12-13 03:31:28 -06:00
|
|
|
named_txt = ipautil.template_file(ipautil.SHARE_DIR + "bind.named.conf.template", self.sub_dict)
|
2007-09-20 14:10:21 -05:00
|
|
|
named_fd = open('/etc/named.conf', 'w')
|
|
|
|
named_fd.seek(0)
|
|
|
|
named_fd.truncate(0)
|
|
|
|
named_fd.write(named_txt)
|
|
|
|
named_fd.close()
|
|
|
|
|
2008-05-13 12:35:20 -05:00
|
|
|
def __setup_resolv_conf(self):
|
|
|
|
self.fstore.backup_file('/etc/resolv.conf')
|
|
|
|
resolv_txt = "search "+self.domain+"\nnameserver "+self.ip_address+"\n"
|
|
|
|
resolv_fd = open('/etc/resolv.conf', 'w')
|
|
|
|
resolv_fd.seek(0)
|
|
|
|
resolv_fd.truncate(0)
|
|
|
|
resolv_fd.write(resolv_txt)
|
|
|
|
resolv_fd.close()
|
2007-09-20 14:10:21 -05:00
|
|
|
|
2008-01-11 05:57:36 -06:00
|
|
|
def uninstall(self):
|
|
|
|
running = self.restore_state("running")
|
2008-03-27 18:01:38 -05:00
|
|
|
enabled = self.restore_state("enabled")
|
2008-01-11 05:57:36 -06:00
|
|
|
domain = self.restore_state("domain")
|
|
|
|
|
|
|
|
if not running is None:
|
|
|
|
self.stop()
|
|
|
|
|
2008-03-27 18:01:38 -05:00
|
|
|
if not domain is None:
|
|
|
|
try:
|
|
|
|
self.fstore.restore_file(os.path.join ("/var/named/", domain + ".zone.db"))
|
|
|
|
except ValueError, error:
|
|
|
|
logging.debug(error)
|
|
|
|
pass
|
|
|
|
|
2008-05-13 12:35:20 -05:00
|
|
|
for f in ["/etc/named.conf", "/etc/resolv.conf"]:
|
2008-03-27 18:01:38 -05:00
|
|
|
try:
|
|
|
|
self.fstore.restore_file(f)
|
|
|
|
except ValueError, error:
|
|
|
|
logging.debug(error)
|
|
|
|
pass
|
|
|
|
|
|
|
|
if not enabled is None and not enabled:
|
|
|
|
self.chkconfig_off()
|
2008-01-11 05:57:36 -06:00
|
|
|
|
|
|
|
if not running is None and running:
|
|
|
|
self.start()
|