#!/usr/bin/perl
######################################################################
#
# dynname -- Add or Remove a dynamic dns name for the authenticated
# http user.
#
# Written by David Simmons, July 1997.
# This script is in the public domain.
#
######################################################################
######################################################################
# Site-specific variabels
######################################################################
$title_html = "Title goes here
";
$zone = "dyn.company.com";
######################################################################
# Main function code
######################################################################
use CGI;
use Net::DNS;
$query = new CGI;
print $query->header();
#
# determine the ip address
#
$ipaddress = $query->remote_addr();
#
# determine the authenticated user
#
$authuser = $ENV{'REMOTE_USER'};
#
# print html beginnings...
#
print <<'_EOM_'
Dynamic DNS results
_EOM_
;
print "$title_html\n";
print <<'_EOM_'
DNS update in progress...
_EOM_
;
#
# take action base on the parameters
#
if (! $query->param('func')) {
print "Error: please specify \"set\" or \"remove\".\n";
} else {
$func = $query->param('func');
#
# Process functions
#
if ($func eq 'set') {
&set_name;
} elsif ($func eq 'remove') {
&remove_name;
} else {
print "Unknown function: $func";
}
}
######################################################################
# Subroutines
######################################################################
sub set_name {
local $name = $authuser . '.' . $zone;
#
# If there is already an A record for this name, remove it.
#
$res = new Net::DNS::Resolver;
$query = $res->search($name);
if ($query) {
print "Attempting to remove old entry: $name = ";
foreach $rr ($query->answer) {
next unless $rr->type eq "A";
print $rr->address, " ";
}
print "
";
$update = new Net::DNS::Update($zone);
# Prerequisite (assumed) is that an A record must already exist.
$update->push("update",
$b = new Net::DNS::RR(Name => $name,
Type => "A",
Ttl => 0,
Class => "ANY",
Rdata => ""));
$res = new Net::DNS::Resolver;
$res->nameservers("127.0.0.1");
$ans = $res->send($update);
if (defined $ans) {
print "Return code: ", $ans->header->rcode, "
\n";
if ($ans->header->rcode eq "NOERROR") {
print "Old entry removed successfully.
\n";
} else {
print "Failed to remove old data!!!
\n";
}
} else {
print "Error: $res->errorstring
\n";
print "Failed to remove old data!!!!
\n";
}
}
print "adding \"$name\" to zone \"$zone\".
\n";
$update = new Net::DNS::Update($zone);
# NXRRSET - Prerequisite is that no A records exist for the name.
$update->push("pre", new Net::DNS::RR(Name => $name,
Class => "NONE",
Type => "A"));
# Add one A records for the name.
$update->push("update", new Net::DNS::RR(Name => $name,
Ttl => 10,
Type => "A",
Address => $ipaddress));
$res = new Net::DNS::Resolver;
$res->nameservers("127.0.0.1");
$ans = $res->send($update);
if (defined $ans) {
print "Return code: ", $ans->header->rcode, "\n";
if ($ans->header->rcode eq "NOERROR") {
print "Success!
\n";
} else {
print "Failure!
\n";
}
} else {
print "Error: $res->errorstring
\n";
print "Failure!
\n";
}
}
sub remove_name {
local $name = $authuser . '.' . $zone;
print "removing \"$name\" from zone \"$zone\".
\n";
$update = new Net::DNS::Update($zone);
# Prerequisite is that an A record must already exist.
$update->push("update",
$b = new Net::DNS::RR(Name => $name,
Type => "A",
Ttl => 0,
Class => "ANY",
Rdata => ""));
$res = new Net::DNS::Resolver;
$res->nameservers("127.0.0.1");
$ans = $res->send($update);
if (defined $ans) {
print "Return code: ", $ans->header->rcode, "
\n";
if ($ans->header->rcode eq "NOERROR") {
print "Success!
\n";
} else {
print "Failure!
\n";
}
} else {
print "Error: $res->errorstring
\n";
print "Failure!
\n";
}
}