Archive for April, 2008

Frontpage web hosting - 650 Part IV: Using PHP for Sysadmin Tasks

Wednesday, April 30th, 2008

650 Part IV: Using PHP for Sysadmin Tasks Listing 18-5 (Continued) echo $namedConf; } else { echo Warning: $baseZoneFile . already used in . NAMED_CONF . n ; } return TRUE; } function zoneInNamedConf($file = null) { $lines = file(NAMED_CONF); if (count($lines) <1) return FALSE; $search = / . $file . / ; foreach ($lines as $named_conf) { if (preg_match($search, $named_conf)) return TRUE; } return FALSE; } function appendNamedConfFile($config = null) { $fp = fopen(NAMED_CONF, a ); if (! $fp) { echo Error: could not open . NAMED_CONF . for update.n ; return FALSE; } fputs($fp, $config); fclose($fp); return TRUE;

Chapter 18: BIND Domain Manager 649 } $zoneTemplate

Wednesday, April 30th, 2008

Chapter 18: BIND Domain Manager 649 } $zoneTemplate = getFQPNZoneTemplate($template); if (empty($zoneTemplate)) return FALSE; echo Adding $zone using $zoneTemplate n ; require_once($zoneTemplate); $GLOBALS[ZONE] = $zone; $config = getZoneConfiguration(); echo $config; $status = writeZoneFile($zoneFile, $config); $namedMasterZoneTemplate = getFQPNNamedMasterZoneTemplate(); if ( ! file_exists($namedMasterZoneTemplate)) { echo Error: $namedMasterZoneTemplate is missingn ; return FALSE; } echo Loading $namedMasterZoneTemplate … ; require_once($namedMasterZoneTemplate); echo OK.n ; $GLOBALS[ZONE_FILE] = basename($zoneFile); $baseZoneFile = basename($zoneFile); if (! zoneInNamedConf($baseZoneFile)) { $namedConf = getNamedZoneConfig(); $status = appendNamedConfFile($namedConf); Continued

648 Part IV: (Web server on xp) Using PHP for Sysadmin Tasks

Wednesday, April 30th, 2008

648 Part IV: Using PHP for Sysadmin Tasks Listing 18-5: makezone #!/usr/bin/php -q

Jetty web server - Chapter 18: BIND Domain Manager 647 Like the

Tuesday, April 29th, 2008

Chapter 18: BIND Domain Manager 647 Like the zone template, this is also a PHP script. It has a function called getNamedZoneConfig(), which is called by makezone. This function returns the configuration that is appended to the /etc/named.conf file to hook up the new zone to the DNS server. The following code shows sample output of this template: // // Master zone configuration for example.com // zone example.com IN { type master; file example.com.zone ; allow-update { none; }; }; This configuration is appended to /etc/named.conf. Notice that we only create a master configuration for the new forward domain. When makezone is run successfully to create a new zone, a zone file is created in the location specified by ZONE_DIR, and the appropriate configuration is appended to the file specified by NAMED_CONF to enable the DNS server to find the new zone configuration. Once makezone is successful, you can restart the BIND name server using the following: /etc/rc.d/init.d/named restart This will load the new zone, and you can test your new zone data using the dig command, which is discussed in the section, Testing makezone. Understanding makezone The makezone utility is implemented in Listing 18-5. This script works as follows: . It expects the command-line arguments and options defined in $CMD_SHORT_OPTIONS and $CMD_LONG_OPTIONS. . It retrieves the command-line arguments and options into $cmd using the getCommandLineOptions() function, which is called with Console_Getopt::getopt() output, which returns valid command-line arguments and options or an error object. . If no command-line argument is provided, the syntax() function is called to display syntax. . If the –add option is specified, the addZone() function is called to create the new zone.

646 Part IV: (Web site) Using PHP for Sysadmin Tasks

Tuesday, April 29th, 2008

646 Part IV: Using PHP for Sysadmin Tasks All the IP addresses and host names are inserted using various $GLOBALS set from the makezone script and makezone.conf file. You can create as many zone templates as you wish. To use them, just call the desired zone template using the –template=zone_template option. Remember to place your zone template in the templates directory pointed to by the ZONE_TEMPLATE_DIR constant in makezone.conf. Make sure your template is a PHP script containing the getZone Configuration()function, which returns the full zone configuration. The zone template produced configuration is stored in the ZONE_DIR directory as a separate zone file. There is one other kind of template that makezone uses for creating the configuration needed to add a new zone configuration to /etc/named.conf. This template is shown in Listing 18-4. Listing 18-4: named.master_zone.conf

Web hosting india - Chapter 18: BIND Domain Manager 645 As you

Monday, April 28th, 2008

Chapter 18: BIND Domain Manager 645 As you can see, the standard template is a PHP script, which means you can do anything you want in this template using the power of PHP. The standard template defines a zone that has a Start of Authority (SOA) record, two name servers (NS) records, two mail exchanger (MX) records, three address (A) records, and a CNAME alias. The configuration shown here (number of records and types) is simply a solution. Feel free to edit the template according to your own needs, adding or removing records as you see fit. If you need more information about BIND, pick up a copy of…. When this zone template is loaded, the makezone script calls the getZoneConfiguration() function, which returns a complete zone configuration. Listing 18-3 shows a sample configuration created using the standard.template. Listing 18-3: Sample Output of standard.template ; ; This zone file is generated automatically by makezone script ; If you edit this file manually, the changes will be lost ; if you regenerate the zone again using makezone ; $TTL 86400 $ORIGIN example.com. @ 1D IN SOA @ root ( 01 ; serial 3H ; refresh 15M ; retry 1W ; expiry 1D ) ; minimum 1D IN NS 192.168.0.11 1D IN NS 192.168.1.254 1D IN MX 5 192.168.0.100 1D IN MX 10 192.168.0.101 ns 1D IN A 192.168.0.11 www 1D IN A 192.168.0.12 www IN CNAME apache.example.com. ftp 1D IN A 192.168.0.12

644 Part IV: Using PHP for Sysadmin Tasks (Web file server)

Monday, April 28th, 2008

644 Part IV: Using PHP for Sysadmin Tasks Listing 18-2: standard.template

Chapter 18: BIND Domain Manager 643 ini_set( include_path , (Web server logs)

Saturday, April 26th, 2008

Chapter 18: BIND Domain Manager 643 ini_set( include_path , : . $PEAR_DIR . : . ini_get( include_path )); require_once Console/Getopt.php ; define(DEBUG, TRUE); define(NAMED_CONF, /etc/named.conf ); define(ZONE_DIR, /var/named ); define(ZONE_TEMPLATE_DIR, templates ); define(ZONE_MASTER_TEMPLATE, named.master_zone.conf ); define(DEFAULT_TEMPLATE, standard ); $DOMAIN = example.com ; $PRIMARY_NAME_SERVER = 192.168.0.11 ; $SECONDARY_NAME_SERVER = 192.168.1.254 ; $PRI05_MAIL_SERVER = 192.168.0.100 ; $PRI10_MAIL_SERVER = 192.168.0.101 ; $WWW_SERVER_IP_ADDR = 192.168.0.12 ; $FTP_SERVER_IP_ADDR = 192.168.0.12 ; $WWW_SERVER_ALIAS = apache.example.com ; ?> As mentioned before, the makezone script uses two types of template, one of which is used to create the zone. There can be many different zone templates. Listing 18-2 shows a zone template called standard.template. This template is used to create a new zone when the option –template=standard is provided. The DEFAULT_TEMPLATEis set to standard; therefore, in the absence of a - template option, the standard template is used. If you wish to use a different template as the default, change the value of the DEFAULT_TEMPLATE constant. For example, if you specify –template=advanced, makezone will use the templates/advanced.template file as the zone template. Now let s look at the standard. template in detail.

642 Part IV: Using PHP for Sysadmin Tasks (Web site design and hosting)

Saturday, April 26th, 2008

642 Part IV: Using PHP for Sysadmin Tasks configuration files. Also note that some versions of Linux store their BIND files in other locations edit the configuration for this application according to your version of BIND. To determine if your machine is running BIND, use the following command: ps A | grep named If the system replies with something similar to the following, you are run ning BIND: 15314 ? 00:00:04 named If nothing is returned, named(the BIND server) is not running. Following is a sample command line of makezone. Here, makezone is instructed to add a new zone called example.com using the standard template. ./makezone –add=zone –name=example.com –template=standard Creating the Configuration File Listing 18-1 shows the makezone.conf configuration file. This is the primary configuration file for the makezone script. The makezone script uses the Console/Getopt.php package from the PEAR package; therefore, the PEAR path must be added using $PEAR_DIR. The sample PEAR path /www/pear is not likely to be the same as yours, so you should change it and any other paths to reflect your system configuration. The NAMED_CONF constant points to the central BIND configuration file /etc/named.conf. The ZONE_DIR constant points to the standard BIND zone directory /var/named. The ZONE_TEMPLATE_DIR constant points to the template directory, which is a subdirectory of the makezone script directory. The makezone script looks for two types of templates: one to create the /etc/named.conf configuration and another to create the actual zone configuration. The DEFAULT_TEMPLATE constant specifies the name of the default template. The rest of the configuration defines various host name and IP addresses that are used in the zone configuration, and will be discussed later in this section when we cover the standard template. Listing 18-1: makezone.conf

Chapter 18 BIND Domain Manager IN THIS CHAPTER (Web hosting solutions)

Friday, April 25th, 2008

Chapter 18 BIND Domain Manager IN THIS CHAPTER . Developing a BIND administrator tool . Using the BIND administrator tool IN THIS CHAPTER, we will develop a simple DNS management application that runs via the command line and creates DNS configurations for domains that you can host on your Linux-based BIND server. BIND is the most widely used DNS server on the Linux/UNIX platform. The script developed here creates only forward domain configuration, as reverse DNS domain management is the primary task of ISPs. Features of makezone The DNS administration tool we will develop is called makezone. The makezone utility has the following features: . It is a command-line tool that can be run by the root user to create new DNS configurations for domains that are primarily used for Web service. . It uses a template-based configuration that enables an administrator to create classes of DNS configurations. For example, an administrator can create a DNS configuration template that creates a new DNS domain with the bare minimum number of entries, namely the name server entries, a Web server entry, an FTP server entry, and two mail (MX) server records (with differing priorities). The administrator can also create another configuration template that creates multiple round-robin Web server configurations. . The utility works only with forward domains, as reverse DNS is primarily handed by ISPs, and requires a fair amount of knowledge of DNS and IP addresses to implement. Our focus is to create a Web server host DNS configuration, which can often work without any reverse DNS setup. Note that this utility assumes you are running on a Linux server and running a suitable version of BIND. If you are using another DNS server application you can still use this application, but significant editing of the templates, file names, and 641