Hi all.

I need help with a modify perl script for SLES11 with IDM Sriptdriver.

I am trying to do the following:


- Delete old entries of the information in the reverse lookup
zonefile for DNS
- Delete the forward lookup zonefile for the modified domain
- Edit the named.conf file to be up-to-date
- Create the forward lookup zonefile for the modifyed domain
- Insert the reverse information about the modified domain
- Add the information about where to look for forward lookup zone in
named.conf


Evything works perfectly. I even gets the serial number changed and
runs the commands needed for reloading the zones to the dns server. But
in fact it works a little to well. All information is 100% correctly
added to reverse zonefile, and forward lookup zonefile is correctly
created and written. But in named.conf.include(using a included file
with the information), it writes the information 2 times.

I have inserted the full code below here, but I have edited it heavyli
to try keep confidential information out. But as it is not totaly
possible, I humble asks you to tread this carefull.


PHP code:
--------------------
#!/usr/bin/perl

use strict;
use IDMLib;
use Data:umper;

our $global_config;

# include the IDM Library
my $idmlib = new IDMLib();

$idmlib->logger($global_config->{TRACEPRIO}, "modify.pl", " modify invoked ");
$idmlib->trace(" *** modify.pl *** ");

my $CLASS_NAME = $idmlib->idmgetvar("CLASS_NAME");
my $CN = $idmlib->idmgetvar("ADD_CN");
my $DNS = $idmlib->idmgetvar("ADD_ForwardRecordString");
my $REVERSEDNS = $idmlib->idmgetvar("ADD_ReverseRecordString");

# INSERT CUSTOM CODE HERE
#
# Retrieve additional information about the event and send back a
# status document indicating the level of success for this modify.
#

#
# Deletes the forward zonefile for this domain.
#

unlink("/var/lib/named/kunder/".$CN);

#
# The path to the conf file pointing at the forward zone.
#

my $path = '/etc/named.conf.include';

#
# The text that we are gonna remove.
#

my $b = 'zone "'.$CN.'" in {
allow-transfer { any; };
file "../kunder/'.$CN.'";
type master;
};

';

#
# the text that we are gonna replace with
#

my $a = '
';

my $filetext = file_get_contents();
my $final = str_replace();

$filetext = '';

#
# Write the file with changes
#

open(FILE, ">".$path);
print FILE $final;
close(FILE);

#
# Each line will be searched to match this value
#

my $keyword = $CN;

#
# The placement of the reverse zonefile
#

my $reversefile = '/var/lib/named/reverse/X.X.X.in-addr.arpa';

#
# The vaiables we are going to use in our deletion
#

my $cont = "";
my $temp = "";
open(FH, "<".$reversefile) or die $!;
while(<FH>)
{
$temp = $_;
#
# If a match is found on a line, dont save that line to the $cont var
#
if($temp =~ /$keyword/)
{
$cont .= '';
}
elsif($temp =~ /serial/)
{
$cont .= ' '.time().' ; serial
';
}
#
# Else if no match, write line to $cont var
#
else
{
$cont .= $_;
}
}
close(FH);

#
# Write the content of the file and owerrite the existing content
#

open(FILE, ">".$reversefile);
print FILE $cont;
close(FILE);

my $fortext = '$ORIGIN
$TTL 2D
';

#
# We need to find how many records that will be in first dimension
# of our 2 dimensionel array.
#

my @tmp = split (/\\#/, $DNS);
my $num = scalar(@tmp);

#
# Builds our 2 dimensionel array.
#

my @out = map{[split /\\,/, $_]}split(/\\#/, $DNS);

#
# Builds the records based on type of record.
#

my $text .= '@ IN SOA HOST.domain.tld root.HOST.domain.tld (
'.time().' ; serial
3H
1H
1W
1D )
IN NS HOST1.domain.tld
IN NS HOST2.domain.tld
$ORIGIN @
@ IN NS HOST1.domain.tld.
IN NS HOST2.domain.tld.
';

for(my $i = 0; $i<$num; $i++)
{
if($out[$i][0]=="www")
{
$text .= $out[$i][0].' IN A '.$out[$i][1].'
';
}
elsif($out[$i][0]=="mail")
{
$text .= $out[$i][0].' IN MX '.$out[$i][1].'
';
}
else
{
$text .= $out[$i][0].' IN A '.$out[$i][1].'
';
}
}

#
# Builds last part of the forward file zone text.
#

$fortext .= $text;

#
# Writes the forward zonefile text to the forward zonefile.
#

open(FILE, ">/var/lib/named/kunder/".$CN);
print FILE $fortext;
close(FILE);

#
# ************************************************** ****
# This section is for building the reverse lookup zone.
# ************************************************** ****
#

my @rev = map{[split /\\,/, $_]}split(/\\#/, $REVERSEDNS);
@tmp = split (/\\#/, $REVERSEDNS);
$num = scalar(@tmp);
my $reverstext = '';
for(my $i = 0; $i<$num; $i++)
{
my $iptxt = '';
my @ip = split(/\\./, $rev[$i][0]);
my $num2 = scalar(@ip);
my $z = $num2 - 1;
$iptxt .= $ip[$z];

$reverstext .= $iptxt.' PTR '.$rev[$i][1].'
';
}
$reverstext .= ';VAR';
$a = $reverstext;
$b = ';VAR';

$path = "/var/lib/named/reverse/X.X.X.in-addr.arpa";

$filetext = file_get_contents();
my $finalreverse = str_replace();

open(FILE, ">".$path);
print FILE $finalreverse;
close(FILE);

#
# ************************************************** *******************
# This section builds and writes the informations to named.conf.include
# ************************************************** *******************
#

my $path2 = "/etc/named.conf.include";


$a = 'zone "'.$CN.'" in {
allow-transfer { any; };
file "../kunder/'.$CN.'";
type master;
};

#VAR2';

$b = '#VAR2';

$filetext = file_get_contents();
$final = str_replace();

open(FILE, ">".$path2);
print FILE $final;
close(FILE);

# ***********************************************
# This section holds our functions.
# ***********************************************

sub file_get_contents
{
my $filename = $path;
my $temp = "";
my $content = "";
my $keyword2 = "; serial";
open(FH, "<".$filename) or die $!;
while(<FH>)
{
$temp = $_;
if($temp =~ /$keyword2/)
{
$content .= ' '.time().' ; serial
';
}
else
{
$content .= $_;
}

}
close(FH);

return $content;
}

sub str_replace
{
my $replace_this = $b;
my $with_this = $a;
my $string = $filetext;

my $length = length($string);
my $target = length($replace_this);

for(my $i=0; $i<$length - $target + 1; $i++)
{
if(substr($string,$i,$target) eq $replace_this)
{
$string = substr($string,0,$i) . $with_this . substr($string,$i+$target);
}
}
return $string;
}

system("command", "named-checkconf", 'named-checkzone "X"', '"master/X"', "Reload zone");


# For the skeleton script, simply return a "Not Implemented" status
$idmlib->status_warning("Not Implemented");

--------------------


--
kkrasmussen
------------------------------------------------------------------------
kkrasmussen's Profile: http://forums.novell.com/member.php?userid=20966
View this thread: http://forums.novell.com/showthread.php?t=347421