Exemplo n.º 1
0
        function testDNSZone2()
        {
			Core::Load("NET/DNS/class.DNSZone2.php");
			Core::Load("NET/DNS/class.DNSRecord.php");
			
			$dnszone = new DNSZone();
			
			/////
			// Test SOA DNS Record
			//
			Core::Load("NET/DNS/class.SOADNSRecord.php");
			
			// Valid SOA
			$SOA = new SOADNSRecord("test.com","ns.hostdad.com", "*****@*****.**");			
			$this->assertWantedPattern("/@\s+IN\s+SOA[\s\t]+/msi", $SOA->__toString(), "Generated SOA Record");
			$dnszone->AddRecord($SOA);
			
			// Invalid SOA
			$soa = new SOADNSRecord("test", "ns.hostdad.com", "*****@*****.**");
			$this->assertFalse($soa->__toString(), "SOA Record NOT generated with invalid params");
			
			/////
			// Test A DNS Record
			//
			Core::Load("NET/DNS/class.ADNSRecord.php");
			
			// subdomain record
			$a1 = new ADNSRecord("test", "192.168.1.1");
			$this->assertWantedPattern("/[A-Za-z0-9]+\s+IN\s+A[\s\t]+/msi", $a1->__toString(), "Generated A Record");
			$dnszone->AddRecord($a1);
			
			//domain record
			$a2 = new ADNSRecord("test.com", "192.168.1.2");
			$this->assertWantedPattern("/[A-Za-z0-9\.]+\s+IN\s+A[\s\t]+/msi", $a2->__toString(), "Generated A Record");
			$dnszone->AddRecord($a2);
			
			//dottify domain record
			$a3 = new ADNSRecord("test.com.", "192.168.1.3");
			$this->assertWantedPattern("/[A-Za-z0-9\.]+\s+IN\s+A[\s\t]+/msi", $a3->__toString(), "Generated A Record");
			$dnszone->AddRecord($a3);
			
			//@ domain record
			$a4 = new ADNSRecord("@", "192.168.1.100");
			$this->assertWantedPattern("/@\s+[0-9]*\sIN\s+A[\s\t]+/msi", $a4->__toString(), "Generated A Record");
			$dnszone->AddRecord($a4);
			
			//invalid record
			$record = new ADNSRecord("-1test.com", "192.168.1");
			$this->assertFalse($record->__toString(), "A Record NOT generated with invalid params");
			
			//////
			// Test MX DNS Record
			//
			Core::Load("NET/DNS/class.MXDNSRecord.php");
			
			//domain record
			$record = new MXDNSRecord("mail", "test.com");
			$this->assertWantedPattern("/[A-Za-z0-9\.]+\s+IN\s+MX[\s\t]+/msi", $record->__toString(), "Generated MX Record");
			$dnszone->AddRecord($record);
			
			//dottify domain record
			$record = new MXDNSRecord("test.com.", "mailtest.com");
			$this->assertWantedPattern("/[A-Za-z0-9\.]+\s+IN\s+MX[\s\t]+/msi", $record->__toString(), "Generated MX Record");
			$dnszone->AddRecord($record);
			
			//@ domain record
			$record = new MXDNSRecord("@", "mail2.test.com");
			
			$this->assertWantedPattern("/@\s+[0-9]*\sIN\s+MX[\s\t]+/msi", $record->__toString(), "Generated MX Record");
			$dnszone->AddRecord($record);
			
			//invalid record
			$record = new MXDNSRecord("-1test.com", "test2");
			$this->assertFalse($record->__toString(), "MX Record NOT generated with invalid params");
			
			///////
			// Test NS DNS Record
			//
			Core::Load("NET/DNS/class.NSDNSRecord.php");
			
			// subdomain record
	
			//domain record
			$record = new NSDNSRecord("test.com", "ns1.test.com");
			$this->assertWantedPattern("/[A-Za-z0-9\.]+\s+IN\s+NS[\s\t]+/msi", $record->__toString(), "Generated NS Record");
			$dnszone->AddRecord($record);
			
			//dottify domain record
			$record = new NSDNSRecord("test.com.", "ns2.test.com");
			$this->assertWantedPattern("/[A-Za-z0-9\.]+\s+IN\s+NS[\s\t]+/msi", $record->__toString(), "Generated NS Record");
			$dnszone->AddRecord($record);
			
			//sub domain record
			$record = new NSDNSRecord("name.com", "ns1");
			$this->assertWantedPattern("/[A-Za-z0-9\.]+\s+[0-9]*\sIN\s+NS[\s\t]+/msi", $record->__toString(), "Generated NS Record");
			$dnszone->AddRecord($record);
			
			//invalid record
			$record = new NSDNSRecord("-1test.com", "asdasda");
			$this->assertFalse($record->__toString(), "NS Record NOT generated with invalid params");
			
			///////
			// Test CNAME DNS Record
			//
			Core::Load("NET/DNS/class.CNAMEDNSRecord.php");
			
			//domain record
			$record = new CNAMEDNSRecord("test", "ns2.test.com");
			$this->assertWantedPattern("/[A-Za-z0-9\.]+\s+IN\s+CNAME[\s\t]+/msi", $record->__toString(), "Generated CNAME Record");
			$dnszone->AddRecord($record);
			
			//sub domain record
			$record = new CNAMEDNSRecord("name", "ns1", 28800);
			$this->assertWantedPattern("/[A-Za-z0-9\.]+\s+[0-9]+\sIN\s+CNAME[\s\t]+/msi", $record->__toString(), "Generated CNAME Record");
			$dnszone->AddRecord($record);
			
			//invalid record
			$record = new CNAMEDNSRecord("-1test.com", "192.168.1.1");
			$this->assertFalse($record->__toString(), "CNAME Record NOT generated with invalid params");
			
			///////
			// Test TXT DNS Record
			//
			Core::Load("NET/DNS/class.TXTDNSRecord.php");
			
			$record = new TXTDNSRecord("example.com.", "This is a test TXT record");
			$this->assertWantedPattern("/[A-Za-z0-9\.]+\s+[0-9]*\sIN\s+TXT[\s\t]+\"([^\"]+)\"/si", $record->__toString(), "Generated TXT record");
			
			$record = new TXTDNSRecord("test", "This is a test TXT record");
			$this->assertWantedPattern("/[A-Za-z0-9\.]+\s+[0-9]*\sIN\s+TXT[\s\t]+\"([^\"]+)\"/si", $record->__toString(), "Generated TXT record");
			
			$record = new CNAMEDNSRecord("192.168.1.20", "This is a test TXT record");
			$this->assertFalse($record->__toString(), "TXT Record NOT generated with invalid params");
			
			////////
			// Test SPF DNS Record
			//
			Core::Load("NET/DNS/class.SPFDNSRecord.php");
			
			
			// Test data
			$basics = array(
			                 array("?", "include:test.com"),
			                 array("-", "all")
			               );
			$sender_a    = array(
			                     array("", "a"),
			                     array("+", "a:test.com"), 
			                     array("~", "a:test.com/16"), 
			                     array("?", "a/16")
			                     );
			
			$sender_mx   = array(
			                     array("+", "mx"), 
			                     array("-", "mx:test.com"), 
			                     array("~", "mx:test.com/16"), 
			                     array("?", "mx/16")
			                     );
			
			$sender_ptr  = array(
			                     array("+", "ptr"), 
			                     array("", "ptr:test.com")
			                    );
			
			$sender_ip4  = array(
			                     array("~", "ip4:192.168.1.1"),
			                     array("", "ip4:192.168.1.1/16")
			                    );
			
			$sender_ip6  = array(
			                     array("?", "ip6:2001:db8::10"), 
			                     array("", "ip6:2001:db8::10/16")
			                    );
			
			$sender_exists  = array(
			                         array("-", "exists:test.com")
			                       );
			
			$mods = array(array("redirect", "test.net"), array("exp", "test.test.com"));
			
			$record = new SPFDNSRecord("test.com.", $sender_a, $basics, $mods);
			$this->assertTrue($record->__toString(), "Generated SPF TXT Record Width A rules");
			
			$record = new SPFDNSRecord("test.com.", $sender_mx, $basics, $mods);
			$this->assertTrue($record->__toString(), "Generated SPF TXT Record Width MX rules");
			
			$record = new SPFDNSRecord("test.com.", $sender_ptr, $basics, $mods);
			$this->assertTrue($record->__toString(), "Generated SPF TXT Record Width PTR rules");
			
			$record = new SPFDNSRecord("test.com.", $sender_ip4, $basics, $mods);
			$this->assertTrue($record->__toString(), "Generated SPF TXT Record Width IP4 rules");
			
			$record = new SPFDNSRecord("test.com.", $sender_exists, $basics, $mods);
			$this->assertTrue($record->__toString(), "Generated SPF TXT Record Width EXISTS rules");
			
			$record = new SPFDNSRecord("-test.com.", $sender_exists, $basics, $mods);
			$this->assertFalse($record->__toString(), "SPF TXT Record NOT generated Width invalid name");
			
			// Custom SPF record
			$basics = array(
			                 array("", "include:webmail.pair.com"),
			                 array("?", "include:spf.trusted-forwarder.org"),
			                 array("-", "all")
			               );
			$sender = array(
			                 array("", "ip4:72.81.252.18"),
			                 array("", "ip4:72.81.252.19"),
			                 array("", "ip4:70.91.79.100"),
			                 array("?", "a:relay.pair.com")
			               );
            $record = new SPFDNSRecord("kitterman.com.", $sender, $basics);
            $this->assertTrue($record->__toString(), "Custom SPF TXT Record generated");
			
            // Test SPF With macroses
            $basics = array(
			                 array("-", "include:ip4._spf.%{d}"),
			                 array("-", "include:include:ip4._spf.%{d}"),
			                 array("+", "all")
			               );
			$sender = array(
			                 array("", "exists:%{l1r+}.%{d}"),
			                 array("", "exists:%{l1r+}.%{d}")
			               );
            $record = new SPFDNSRecord("kitterman.com.", $sender, $basics);
            $this->assertTrue($record->__toString(), "Custom SPF TXT Record with macroses generated");
            
            // Test SPF With bad macroses
            $basics = array(
			                 array("-", "include:ip4._spf.%{dfhsd}"),
			                 array("-", "include:include:ip4._spf.%{asdfklj}"),
			                 array("+", "all")
			               );
			$sender = array(
			                 array("", "exists:%{l1r+}.%{32}"),
			                 array("", "exists:%{l1r+}.%{sdaf}")
			               );
            $record = new SPFDNSRecord("kitterman.com.", $sender, $basics);
            $this->assertFalse($record->__toString(), "Custom SPF TXT Record with bad macroses NOT generated");
            
			///////
			// Test PTR DNS Record
			//
			Core::Load("NET/DNS/class.PTRDNSRecord.php");
			
			//domain record
			$record = new PTRDNSRecord("2", "c1.test.com");
			$this->assertWantedPattern("/[0-9]+\s+[0-9]*\s+IN\s+PTR[\s\t]+/msi", $record->__toString(), "Generated PTR Record");
			
			//dotify domain record
			$record = new PTRDNSRecord("245", "c2.test.com.");
			$this->assertWantedPattern("/[A-Za-z0-9\.]+\s+[0-9]*\sIN\s+PTR[\s\t]+/msi", $record->__toString(), "Generated PTR Record");

			//invalid record
			$record = new PTRDNSRecord("370", "192.168.1.1");
			$this->assertFalse($record->__toString(), "PTR Record NOT generated with invalid params");

			$content = $dnszone->__toString();  
			$this->assertWantedPattern("/test.com/msi", $content, "Generated DNS zone contains at least SOA name");
			$this->assertWantedPattern("/IN\s+A[\s\t]+192\.168\.1\.100/msi", $content, "Generated DNS zone contains added A record");
        }
Exemplo n.º 2
0
    function doTests()
    {
        // prepare
        $zone_tpl = '; !Automatically generated - do not edit!
						; Zone file for {name}
						$TTL {ttl}
						@      IN      SOA     {origin} {person} (
						       {serial}    ; serial, todays date+todays
						       {refresh}        ; refresh, seconds
						       {retry}        ; retry, seconds
						       {expire}        ; expire, seconds
						       {minimum} )    ; minimum, seconds
						
						; Records
						{records}';
        define("CF_DNSZONETPL", $zone_tpl);
        $Zone = new DNSZone();
        //
        // Generation
        //
        $SOA = array("name" => "domain-dns.com", "serial" => "2005052700", "origin" => "ns.hostdad.com.", "person" => "ak.webta.net.", "ttl" => 14400, "refresh" => 14400, "retry" => 7200, "expire" => 3600000, "minimum" => 86400);
        if ($Zone->SetSOAValue($SOA)) {
            $params = array("*", "127.0.0.7");
            $Zone->AddRecord("A", $params, $rec["ttl"]);
        }
        $Zone->Generate();
        $template = 'zone "{zone}" {
								   type master;
								   file "{db_filename}";
								};';
        $this->BIND->SetZoneTemplate($template);
        $retval = $this->BIND->ListZones();
        $this->BIND->ConfCleanup();
        $retval = $this->BIND->SaveConf();
        $this->assertTrue($retval, "named.conf saved ok");
        $this->assertNoUnwantedPattern("/\\n\\n\\n/m", "named.conf does not contain unneeded stuff");
        $res = $this->BIND->SaveZone($Zone->Name, $Zone->Content);
        $this->assertTrue($res, "Zone successfully saved");
        $res = $this->BIND->DeleteZone($Zone->Name);
        $this->assertTrue($res, "Zone successfully deleted");
        $retval = $this->BIND->IsZoneExists($Zone->Name);
        $this->assertFalse($retval, "Zone file does not exists");
    }
Exemplo n.º 3
0
	if ($_POST) 
	{
		if ($post_zonename)	
		{
			$db->BeginTrans();
			
			try
			{
				Core::ClearWarnings();
				$Zone = new DNSZone();
    			$SOA = new SOADNSRecord($post_zonename, $post_zone["soa_parent"], $post_zone["soa_owner"]);
    			if (!$SOA->__toString())				        
    			    $error = true;
    			else 
    			{
                    $Zone->AddRecord($SOA);
                    $CNAMERecords = array();
                    
                    foreach ((array)$post_zone["records"] as $k=>$record)
					{
						if ($record["rvalue"] != '')
    					{
        					foreach ($record as $kk=>$vv)
        					{
        						$record[$kk] = str_replace('{$hostname}', $post_zonename, $record[$kk]);
        						$record[$kk] = str_replace('{$ipaddr}', $post_ip, $record[$kk]);
        					}
        					
        					switch($record["rtype"])
            				{
            					case "A":
Exemplo n.º 4
0
        public function StartThread($serverinfo)
        {   
            $db = Core::GetDBInstance();
            $Crypto = Core::GetInstance("Crypto", LICENSE_FLAGS::REGISTERED_TO);
	
            $db->Execute("UPDATE nameservers SET isnew='0' WHERE id='{$serverinfo['id']}'");
            
            //Get zones for update
            if ($serverinfo["isnew"] == 0)
            	$zones = $db->Execute("SELECT * FROM zones WHERE isupdated='0' OR isdeleted='1'");
            else 
            	$zones = $db->Execute("SELECT * FROM zones");
            	
            $count = $zones->RecordCount();
            Log::Log("[PID: ".posix_getpid()."] Found {$count} zones for update on {$serverinfo['host']}", E_USER_NOTICE);
            
            if ($count == 0)
            {
                Log::Log("[PID: ".posix_getpid()."] Nothing to update on {$serverinfo['host']}. Exiting...", E_USER_NOTICE);
                exit();
            }
            
            // Set credentials for connecting to NS server
        	$authinfo = array(
    							"type" 			=> "password",
    							"login"			=> $serverinfo["username"],
    							"password"		=> $Crypto->Decrypt($serverinfo["password"])
    						);
        	
        	// Connect to server
        	Log::Log("[PID: ".posix_getpid()."] Connecting to {$serverinfo['host']}...", E_USER_NOTICE);
        	
        	$zonetemplate = @file_get_contents(dirname(__FILE__)."/../templates/DNS/zone.tpl");
        	
            $remoteBIND = new RemoteBIND(	
                                        $serverinfo["host"], 
                                        $serverinfo["port"], 
                                        $authinfo,
                                        $serverinfo["rndc_path"],
                                        $serverinfo["namedconf_path"],
                                        $serverinfo["named_path"],
                                        $zonetemplate,
                                        false
            				            );
            if (!$remoteBIND->SetTransport("ssh"))
            {
            	Log::Log("[PID: ".posix_getpid()."] Connection to {$serverinfo['host']} failed.", E_USER_NOTICE);
            	$remoteBIND = false;
            }
            				              
            while ($zone = $zones->FetchRow()) 
            {
                $zone_updated = false;
            	if ($remoteBIND)
                {
	            	Log::Log("[PID: ".posix_getpid()."] Processing zone '{$zone['zone']}'...", E_USER_NOTICE);
	                
	                if ($zone["isdeleted"] == '0')
	                {
	                	$Zone = new DNSZone();
		        		$new_serial = SOADNSRecord::RaiseSerial($zone["soa_serial"]);
	                	
		        	    Core::ClearWarnings();
		        		$SOA = new SOADNSRecord(	
		        									$zone["zone"], 
		        									$zone["soa_parent"], 
		        									$zone["soa_owner"],
		        									$zone["soa_ttl"],
		        									$new_serial,
		        									$zone["soa_refresh"],
		        									$zone["soa_retry"],
		        									$zone["soa_expire"],
		        									$zone["soa_min"]
		        								);
		        								
		                if ($SOA->__toString() != "")
		                {
		                    $Zone->AddRecord($SOA);
		                    
		                    $records = $db->Execute("SELECT * FROM records WHERE zoneid = ? ORDER BY rtype", array($zone['id']));
		                    
		                    while($record = $records->FetchRow())
		        			{
		        			    if ($record["rkey"] || $record["rvalue"])
		        			    {
		            				switch($record["rtype"])
		            				{
		            					case "A":
		            							$record = new ADNSRecord($record["rkey"], $record["rvalue"], $record["ttl"]);
		            							$Zone->AddRecord($record);
		            						break;
		            						
		            					case "NS":
		            					        $record = new NSDNSRecord($record["rkey"], trim($record["rvalue"]), $record["ttl"]);
		            							$Zone->AddRecord($record);
		            						break;
		            						
		            					case "CNAME":
		            							$record = new CNAMEDNSRecord($record["rkey"], $record["rvalue"], $record["ttl"]);
		            							$Zone->AddRecord($record);
		            						break;
		            						
		            					case "MX":
		            							$record = new MXDNSRecord($record["rkey"], $record["rvalue"], $record["ttl"], $record["rpriority"]);
		            							$Zone->AddRecord($record);
		            						break;
		            				    
		            					case "TXT":
		                                        $record = new TXTDNSRecord($record["rkey"], $record["rvalue"], $record["ttl"]);
		                                        $Zone->AddRecord($record);
		                                    break;
		            				}
		        			    }
		        			}
		        			
		        			if (!Core::HasWarnings())
					        {
					            Log::Log("[PID: ".posix_getpid()."] Sending '{$zone['zone']}' zone config to server...", E_USER_NOTICE);
		                        $content = $Zone->__toString();
		                        if ($remoteBIND->SaveZone($zone["zone"], $content, false))
		                        {
			                        $db->Execute("UPDATE zones SET soa_serial=? WHERE id=?", array($new_serial, $zone["id"]));
			                        
		                        	Log::Log("[PID: ".posix_getpid()."] '{$zone['zone']}' zone successfully updated", E_USER_NOTICE);
			                        $zone_updated = true;
		                        }
			                    else 
			                    {
			                    	$mess = "There are warnings for zone '{$zone['zone']}':\n";
		                            foreach ($GLOBALS['warnings'] as $warn)
		                                $mess .= "              - {$warn}\n";
		            		          
		            		        Log::Log("[PID: ".posix_getpid()."] {$mess}", E_USER_ERROR);
			                    }
					        }
					        else 
					        {
					            if (Core::HasWarnings())
		            		    {
		                            $mess = "There are warnings for zone '{$zone['zone']}':\n";
		                            foreach ($GLOBALS['warnings'] as $warn)
		                                $mess .= "              - {$warn}\n";
		            		          
		            		        Log::Log("[PID: ".posix_getpid()."] {$mess}", E_USER_ERROR);
		            		    }
					        }
		                }
		                else 
		                {	                    
		                	Log::Log(sprintf("[PID: %s] Broken SOA record for zone '%s' (%s)", posix_getpid(), $zone['zone'], Core::GetLastWarning()), E_USER_ERROR);
		                }
	                }
	                elseif ($serverinfo["isnew"] == '0')
	                {
	                	$remoteBIND->DeleteZone($zone["zone"]);
	                }
                }
                
                if (!$zone_updated)
                {
		        	Log::Log("[PID: ".posix_getpid()."] Zone '{$zone['zone']}' update failed", E_USER_ERROR);
                	$db->Execute("UPDATE zones SET isupdated='2' WHERE id='{$zone['id']}'");
                }
            }
            
            if ($remoteBIND)
            	$remoteBIND->ReloadRndc();
        }