| 75 | rodolico | 1 | #! /usr/bin/perl -w
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | use Sys::Syslog;
 | 
        
           |  |  | 4 |   | 
        
           |  |  | 5 | my $statusFile = '/opt/bin/updatedns.status';
 | 
        
           |  |  | 6 | my $template = '/opt/bin/updatedns.template';
 | 
        
           |  |  | 7 | my $nsupdate = '/usr/bin/nsupdate';
 | 
        
           |  |  | 8 | my $keyfile = '/opt/bin/keys/Kdyndd.net.+157+35226.key';
 | 
        
           |  |  | 9 |   | 
        
           |  |  | 10 | my $entries;
 | 
        
           |  |  | 11 |   | 
        
           |  |  | 12 | sub doUpdateNS {
 | 
        
           |  |  | 13 |    my ($hostname, $ip) = @_;
 | 
        
           |  |  | 14 |    my $nsupdateFileName = "/tmp/$hostname.nsupdate";
 | 
        
           |  |  | 15 |    open TEMPLATE, "<$template" or die "could not open template $template for read: $!\n";
 | 
        
           |  |  | 16 |    my $template = join( '', <TEMPLATE> );
 | 
        
           |  |  | 17 |    close TEMPLATE;
 | 
        
           |  |  | 18 |    $template =~ s/\{hostname\}/$hostname/gi;
 | 
        
           |  |  | 19 |    $template =~ s/\{ip\}/$ip/gi;
 | 
        
           |  |  | 20 |    open OUTPUT, ">$nsupdateFileName" or die "Could not create $nsupdateFileName: $!\n";
 | 
        
           |  |  | 21 |    print OUTPUT $template;
 | 
        
           |  |  | 22 |    close OUTPUT;
 | 
        
           |  |  | 23 |    return `$nsupdate -k $keyfile $nsupdateFileName`;
 | 
        
           |  |  | 24 | }
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | my $realIP = $ENV{'SSH_CLIENT'};
 | 
        
           |  |  | 28 | $realIP =~ m/^([\d.]+)/;
 | 
        
           |  |  | 29 | $realIP = $1;
 | 
        
           |  |  | 30 | my ($hostname,$temp) = split( ' ', $ENV{'SSH_ORIGINAL_COMMAND'});
 | 
        
           |  |  | 31 | $realIP = $temp if $temp;
 | 
        
           |  |  | 32 |   | 
        
           |  |  | 33 | openlog('updatedns', 'cons,pid', 'user');
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 | die "You must send hostname with command\n" unless $hostname;
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 | if ( $hostname !~ m/[a-z0-9-]\.dyndd\.net/ ) {
 | 
        
           |  |  | 38 |    syslog( 'warning', "Attempt to set incoming server name to invalid host [$hostname]");
 | 
        
           |  |  | 39 |    exit;
 | 
        
           |  |  | 40 | }
 | 
        
           |  |  | 41 | if ( -f $statusFile ) {
 | 
        
           |  |  | 42 |    open STATUS, "<$statusFile" or die "could not open $statusFile: $!\n";
 | 
        
           |  |  | 43 |    chomp( (%entries) = map { split "\t" } my (@a) = <STATUS> );
 | 
        
           |  |  | 44 |    close STATUS;
 | 
        
           |  |  | 45 | }
 | 
        
           |  |  | 46 | if ( ( exists $entries{$hostname} ) && ( $entries{$hostname} eq $realIP ) ) {
 | 
        
           |  |  | 47 |    print "dyndd.net: Already set to the correct IP, [$hostname] = [$realIP]\n";
 | 
        
           |  |  | 48 |    syslog('info', '%s', "Already set to the correct IP, [$hostname] = [$realIP]");
 | 
        
           |  |  | 49 | } else {
 | 
        
           |  |  | 50 |    $entries{$hostname} = $realIP;
 | 
        
           |  |  | 51 |    open STATUS, ">$statusFile" or die "could not open $statusFile: $!\n";
 | 
        
           |  |  | 52 |    foreach my $key ( keys %entries ) {
 | 
        
           |  |  | 53 |       print STATUS "$key\t$entries{$key}\n";
 | 
        
           |  |  | 54 |    } # foreach
 | 
        
           |  |  | 55 |    my $output = &doUpdateNS( $hostname, $realIP );
 | 
        
           |  |  | 56 |    print "$hostname updated to $realIP, results are\n$output\n";
 | 
        
           |  |  | 57 |    syslog('info', '%s', "$hostname updated to $realIP" );
 | 
        
           |  |  | 58 | }
 | 
        
           |  |  | 59 | closelog();
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 | 1;
 |