#!/usr/bin/env perl
# @(#) DnsPixie.pl  Update client for DtDNS, FreeDNS and other popular
#                   dynamic DNS services. Rev'd: 2014-12-25.
#
# Copyright (c) 2014 Graham Jenkins <grahjenk@cpan.org>. All rights reserved.
# This program is free software; you can redistribute it and/or modify it under
# the same terms as Perl itself.

use strict;
use warnings;
use File::Basename;
use LWP::Simple qw($ua get);
use vars qw($VERSION);
$ua->timeout(30);
$VERSION = "1.02";

# Check usage, open configuration file
my ($Conn,$Minutes)=("https",);
if ( ($#ARGV >= 0) && ($ARGV[0] eq "-u"    ) ) { $Conn="http"; shift}
if ( ($#ARGV == 1) && ($ARGV[0] =~ m/^\d+$/) ) { $Minutes=shift }
if ( $#ARGV != 0 ) {
  die "Usage: ".basename($0)." [repeat-interval-mins] config-file\n".
      " e.g.: ".basename($0)." 15 /usr/local/etc/DnsPixie.conf\n"
}
if ( defined($Minutes) && ($Minutes==0) ) { die "Zero interval not allowed!\n"}
if ( ! open(CF,$ARGV[0]) ) { die "Can't read file: ".$ARGV[0]."\n" }

# Read and store potentially meaningful records, then close the file
my (%Type,%Pass,%OldAddr,%ChangeTime);
while (<CF>) {
  chomp;
  my ($t,$h,$p) = split;
  if ( defined($p) && ($t !~ /^\043/) ) {
    $Type{$h}=$t; $Pass{$h}=$p; $OldAddr{$h}="x"; $ChangeTime{$h}=time()
  }
}
close(CF);
if ( keys(%Pass) < 1 ) { die "No valid records found in file: ".$ARGV[0]."\n" }

# Sources for current IP address; use these in sequence
my @MyIps=("myip.dtdns.com","myip.dnsomatic.com","myip.dnsdynamic.org");

# Use Syslog if we can
my $rc=eval { require Sys::Syslog; Sys::Syslog->import(); 1 };
if ($rc) { openlog(basename($0),"","user") }
else     { sub syslog {}                   }
 
# Assemble the client name, force an address update on first pass, enter loop
my ($ClNm,$Status)=(basename($0)."-".$VERSION,0);
while (1) {
  # Select next IP-address source, get current address
  my $MyIp=shift(@MyIps); push(@MyIps,$MyIp);
  my $Address;
  if( defined($Address=get("http://".$MyIp)) &&
                                 ($Address=~m/^((\d){1,3}\.){3}(\d){1,3}$/) ) {
    chomp($Address);
    # Update each host address on first pass or if current address changed;
    # also force update if this program hasn't changed it during last 7 days.
    foreach my $h ( sort(keys(%Pass)) ) {
      if ( ($Address ne $OldAddr{$h}) || (time()-$ChangeTime{$h}>7*24*3600) ) {
        syslog("info","Attempting update for $h to: $Address");
        my ($p,$Response,$Good)=($Pass{$h},,);
        if ( $Type{$h} eq "DtDNS"   ) {
          if ( defined($Response=get("$Conn://www.dtdns.com/api".
                                  "/autodns.cfm?id=$h&pw=$p&client=$ClNm")) ) {
            if ( $Response=~m/now points/ ) { $Good="Y" }
          }
        }
        if ( $Type{$h} eq "FreeDNS" ) {
          if ( defined($Response=get(
              "$Conn://freedns.afraid.org/dynamic/update.php?".$p)) ) {
            if ( ($Response=~m/has not chang/) || ($Response=~m/^Updated/) )  {
              $Response=~s/^ERROR: //; $Good="Y"
            }
          } 
        }
        if ( $Type{$h} eq "DuckDNS" ) {
          my $x=lc($h); $x=~s/\.duckdns\.org//;
          if ( defined($Response=get(
              "$Conn://www.duckdns.org/update?domains=$x&token=$p&ip=")) )    {
            if ( $Response eq "OK"  ) { $Good="Y" }
          }
        }
        if ( $Type{$h} eq "ChangeIP" ) {
          my ($x,$y)=split /:/,$p;
          if ( defined($Response=get(
              "$Conn://nic.ChangeIP.com/nic/update?u=$x&p=$y&hostname=$h")) ) {
            if ( $Response=~m/200 Successful Update/ ) { $Good="Y" }
          }
        }
        if ( defined($Good) ) { 
          $OldAddr{$h}=$Address; $ChangeTime{$h}=time();
          syslog("info"," ==> $Response")
        }
        else { $Status=1; syslog("info","Update failed!") }
      }
    }
  }
  else { $Status=1;syslog("info","Can't get current address from: $MyIp")}
  if ( defined($Minutes) ) {sleep(60*$Minutes)}
  else                     {exit($Status)     }
}

__END__

=head1 NAME

DnsPixie - update client for popular dynamic DNS services

=head1 README

DnsPixie will update one or more designated DNS records at
www.dtdns.com, freedns.afraid.org and elsewhere either once or periodically.

=head1 DESCRIPTION

C<DnsPixie> is a simple update client for the DtDNS, FreeDNS and other dynamic
DNS services. It will attempt an immediate update when called,
then optionally loop at designated intervals, attempting 
further updates when necessary.

=head1 USAGE

=over 6

DnsPixie [-u] [repeat-interval-minutes] config-file

=back

e.g.: DnsPixie 15 /usr/local/etc/DnsPixie.conf

The repeat-interval value (where present) must be
expressed as a positive integer number of minutes.
If the '-u' flag is included, then an unsecure (http)
connection will be used.

The config-file value must be the name of a configuration
file which contains one or more 'service hostname password' records
where each password relates to the owner of the
corresponding hostname. Each record should appear on a
separate line thus:

  DtDNS lassie.dtdns.net     mypassword
  FreeDNS daisy.moo.com      ABC7PqRsTUvwXYEjLLM2R7ST8uvWX92AA3BbCz
  DuckDNS donald.duckdns.org 064a0540-864c-4f0f-8bf5-23857452b0c1
  ChangeIP fido.changeip.net myuser:mypassword
   .. etc.

Lines which begin in "#" will be ignored.
It is suggested that the configuration file should be
readable only by the intended program-user.
I<Sys::Syslog> will be used for logging if it's installed;
you might want to check your system's syslog configuration so
that you can view log messages.

=head1 NOTE

By default DnsPixie uses a secure (https) connection. For this to
work, you need to ensure that I<LWP::Protocol::https> is installed.

=head1 SCRIPT CATEGORIES

Networking
UNIX/System_administration

=head1 AUTHOR

Graham Jenkins <grahjenk@cpan.org>

=head1 COPYRIGHT

Copyright (c) 2014 Graham Jenkins. All rights reserved.
This program is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.

=cut
