Dyndns.php

From Blink

Jump to: navigation, search
<?

#---------------------------------------------#
#       Simple DYNDNS with MyDNS              #
# by Miljan Karadzic - http://www.miljan.org/ #
#---------------------------------------------#

# Change this to suite your configurtion
$dbhost='mysql_host';                                   # MySQL hostname
$dbuser='mydns_user';                                   # MySQL username
$dbpass='mydns_pass';                                   # MySQL password
$dbname='mydns_db';                                     # MySQL database
                                                        #
$secret="some_ultra_mega_giga_secret_string";           # Secret string; this has to be the same as the
                                                        # secret string your clients are using; you can do
                                                        # md5sum on some random file to generate one
                                                        #
$mail_to='';                                            # If you want to get email report when records are
                                                        # changed enter you email address here; otherwise
                                                        # keep it empty

#################################################
#     !!!! DO NOT EDIT BELOW THIS LINE !!!!     #
#################################################
$domain = $_GET['domain'];        # Domain name
$subdomain = $_GET['sub'];        # Subdomain name
$ip = $_SERVER['REMOTE_ADDR'];    # IP address

# Check if the secret string matches
if($_GET['addr'] == $secret) {
  # Show the client his IP address
  echo $ip;

  # Check if client wants to update
  if(ereg('^[a-zA-Z0-9\.-]$', $domain) && ereg('^[a-zA-Z0-9\.-]$', $subdomain)) {
    @mysql_connect($dbhost, $dbuser, $dbpass) or die("1");
    @mysql_select_db($dbname) or die("2");

    # Update
    $query = "SELECT * FROM rr "
           . "WHERE zone=(SELECT id FROM soa WHERE origin='". $domain .".') "
           . "AND name='". $subdomain ."'";
    $query = @mysql_query($query) or die("3");

    if(mysql_num_rows($query) == 0) {
      $query = "INSERT INTO rr (zone, name, type, data) "
             . "VALUES((SELECT id FROM soa WHERE origin='". $domain .".'), "
             . $subdomain .", 'A', ". $ip .")";
      @mysql_query($query) or die("4");
    } else {
      $query = "UPDATE rr SET data='". $ip ."' "
             . "WHERE zone=(SELECT id FROM soa WHERE origin='". $domain .".') "
             . "AND name='". $subdomain ."'";
      @mysql_query($query) or die("5");
    }

    # Update zone serial
    $query = "UPDATE soa SET serial = (serial+1) "
           . "WHERE origin = '". $domain .".'";
    @mysql_query($query) or die("6");

    # Send email
    if(isset($mail_to)) {
      $subject = 'DNS change: '. $subdomain;
      $header = 'From: DNSAdmin <dnsadmin@'. $domain .">\r\n";
      $body = 'Date of change: '. date("Y-m-d H:i:s") ."\n"
            . 'Remote address: '. $_SERVER['REMOTE_ADDR'] ."\n"
            . 'Changed record: '. $subdomain .".". $domain ."\n";
      @mail($mail_to, $subject, $body, $header);
    }
  }
}
?>
Views
Personal tools