### ### Copyright 2000-2007 University of Illinois Board of Trustees ### All rights reserved. ### ### CachingDNS.pm - psgconf control module for caching DNS server ### ### Campus Information Technologies and Educational Services ### University of Illinois at Urbana-Champaign ### package PSGConf::Control::CachingDNS; use strict; use PSGConf::Action::GenerateFile::named_conf; use PSGConf::Action::RunCommand; use PSGConf::Data::Boolean; use PSGConf::Data::List; use PSGConf::Data::String; use PSGConf::Control::Packages qw(_add_pkgs); ############################################################################### ### policy methods ############################################################################### sub _policy_check_resolver { my ($self, $psgconf) = @_; $psgconf->data_obj('dns_cache_enable')->set('false') if (! $psgconf->data_obj('dns_servers')->count()); } ### add 127.0.0.1 to dns_servers sub _policy_add_nameserver { my ($self, $psgconf) = @_; return if ($psgconf->data_obj('dns_cache_enable')->equals('false')); $psgconf->data_obj('dns_servers')->add_top('127.0.0.1'); } sub _policy_enable_rc_scripts { my ($self, $psgconf) = @_; $psgconf->data_obj('rc_scripts')->insert( {'named' => { 'state' => 'enable' }} ) if ($psgconf->data_obj('dns_cache_enable')->equals('true')); } ############################################################################### ### decide() method ############################################################################### sub decide { my ($self, $psgconf) = @_; my ($optref, $zoneref, $servers); return if ($psgconf->data_obj('dns_cache_enable')->equals('false')); $optref = { 'listen-on' => [ '127.0.0.1' ], 'allow-query' => [ 'localhost' ], 'allow-transfer' => [ 'none' ] }; if ($psgconf->data_obj('dns_forwarders_enable')->equals('true')) { $optref->{forwarders} = [ grep { $_ ne '127.0.0.1' } @{$psgconf->data_obj('dns_servers')->get()} ]; } $zoneref = { '.' => { type => 'hint', file => '"/etc/named.ca"' } }; $psgconf->register_actions( PSGConf::Action::GenerateFile::named_conf->new( name => $psgconf->data_obj('named_conf_path')->get(), description => 'named configuration file', options => $optref, zones => $zoneref ), PSGConf::Action::RunCommand->new( name => 'generating /etc/named.ca', command => $psgconf->data_obj('dig_path')->get() . ' @' . $psgconf->data_obj('dns_root_server')->get() . ' > /etc/named.ca', filename => [ '/etc/named.ca', $psgconf->data_obj('named_conf_path')->get() ] ) ); if ($psgconf->{'restart_daemons'}) { if ($psgconf->data_obj('use_rndc')->equals('true')) { $psgconf->register_actions( PSGConf::Action::RunCommand->new( name => 'Stopping named', command => $psgconf->data_obj('rndc_path')->get() . ' stop' ), PSGConf::Action::RunCommand->new( name => 'Starting named', command => $psgconf->data_obj('named_path')->get() ) ); } else { $psgconf->register_actions( PSGConf::Action::RunCommand->new( name => 'Restarting named', command => $psgconf->data_obj('ndc_path')->get() . ' restart' ) ); } } } ############################################################################### ### constructor ############################################################################### sub new { my ($class, $psgconf) = @_; my ($self); $self = {}; bless($self, $class); ### So that _add_pkgs knows which directives to look at $self->{name} = 'dns_cache'; $self->{enable} = $self->{name} . '_enable'; $self->{packages} = $self->{name} . '_packages'; $psgconf->register_data( 'dig_path' => PSGConf::Data::String->new( 'value_abspath' => 1, value => '/usr/bin/dig' ), 'dns_root_server' => PSGConf::Data::String->new( value => 'a.root-servers.net.' ), 'dns_cache_enable' => PSGConf::Data::Boolean->new( value => 'true' ), 'dns_cache_packages' => PSGConf::Data::List->new(), 'dns_forwarders_enable' => PSGConf::Data::Boolean->new( value => 'true' ), 'named_conf_path' => PSGConf::Data::String->new( 'value_abspath' => 1, value => '/etc/named.conf' ), 'named_path' => PSGConf::Data::String->new( 'value_abspath' => 1, value => (-e '/usr/sbin/named' ? '/usr/sbin/named' : '/usr/sbin/in.named') ), 'ndc_path' => PSGConf::Data::String->new( 'value_abspath' => 1, value => '/usr/sbin/ndc' ), 'rndc_path' => PSGConf::Data::String->new( 'value_abspath' => 1, value => '/usr/sbin/rndc' ), 'use_rndc' => PSGConf::Data::Boolean->new( value => (-e '/usr/sbin/rndc')? 'true': 'false' ) ); $psgconf->register_policy($self, named_check_resolver => '_policy_check_resolver', named_add_packages => '_add_pkgs', named_add_nameserver => '_policy_add_nameserver', named_enable_rc_scripts => '_policy_enable_rc_scripts', ); return $self; } ############################################################################### ### documentation ############################################################################### 1; __END__ =head1 NAME PSGConf::Control::CachingDNS - psgconf control class for caching DNS server =head1 SYNOPSIS In F: Control PSGConf::Control::CachingDNS =head1 DESCRIPTION The B module provides a B Control object for configuring DNS. It provides the following methods: =over 4 =item new() The constructor. Its parameter is a reference to the B object. It registers the following data objects: =over 4 =item I A B object containing the absolute path to the C command. The default value is F. =item I A B object containing the root server to query when creating the F file from. The default value is C. =item I A B object that indicates whether a caching-only DNS server should be configured. The default is yes. =item I A B object listing all packages that need to be installed. =item I A B object that indicates whether forwarders should be configured as part of the DNS server configs. The default is yes. It uses the values in the I data object as the forwarder addresses. =item I A B object containing the absolute path to the C config file. The default value is F. =item I A B object containing the absolute path to the C daemon. The default value is F if it exists, or F otherwise. =item I A B object containing the absolute path to the C command. The default value is F. =item I A B object containing the absolute path to the C command. The default value is F. =item I A B object that determines whether to use C instead of C to restart C. The default is true if F exists; false otherwise. =back The constructor also registers the following policy methods: =over 4 =item I If the I Data object is not set, disable I. =item I If the I Data object is set, prepends C<127.0.0.1> to the I data object (supplied by B). =item I If the I Data object is set, adds C to the I Data object (supplied by B). =item I Modifies the I data object (supplied by B) to enable C. =back =item decide() Instantiates and registers a B object to create F. The generated file will tell C to only bind to C<127.0.0.1>. It will also use the entries in I as C. It uses a hints file called F. =back =head1 BUGS A special RC script should be created for C, rather than jumping through all of the hoops to figure out how to restart it. =head1 SEE ALSO L dig(1) ndc(8) or rndc(8) named.conf(5) L L L L L L L L L L =cut