]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ddns.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2014 IPFire Team <info@ipfire.org> #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
24 # enable only the following on debugging purpose
26 #use CGI::Carp 'fatalsToBrowser';
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
32 #workaround to suppress a warning when a variable is used only once
33 my @dummy = ( ${Header
::table2colour
}, ${Header
::colouryellow
} );
37 my %mainsettings = ();
38 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
39 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
41 # Config file for basic configuration.
42 my $settingsfile = "${General::swroot}/ddns/settings";
44 # Config file to store the configured ddns providers.
45 my $datafile = "${General::swroot}/ddns/config";
47 # Dynamic ddns programm call.
48 my @ddnsprog = ("/usr/bin/ddns", "--config",
49 "/var/ipfire/ddns/ddns.conf",
53 my $errormessage = '';
55 # DDNS General settings.
56 $settings{'BEHINDROUTER'} = 'RED_IP';
59 $settings{'HOSTNAME'} = '';
60 $settings{'DOMAIN'} = '';
61 $settings{'LOGIN'} = '';
62 $settings{'PASSWORD'} = '';
63 $settings{'ENABLED'} = '';
64 $settings{'PROXY'} = '';
65 $settings{'SERVICE'} = '';
67 $settings{'ACTION'} = '';
69 # Get supported ddns providers.
70 my @providers = &GetProviders
();
72 # Hook to regenerate the configuration files, if cgi got called from command line.
73 if ($ENV{"REMOTE_ADDR"} eq "") {
74 &GenerateDDNSConfigFile
();
78 &Header
::showhttpheaders
();
81 &Header
::getcgihash
(\
%settings);
83 # Read configuration file.
84 open(FILE
, "$datafile") or die "Unable to open $datafile.";
89 # Save General Settings.
91 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
92 # Open /var/ipfire/ddns/settings for writing.
93 open(FILE
, ">$settingsfile") or die "Unable to open $settingsfile.";
95 # Lock file for writing.
98 # Check if BEHINDROUTER has been configured.
99 if ($settings{'BEHINDROUTER'} ne '') {
100 print FILE
"BEHINDROUTER=$settings{'BEHINDROUTER'}\n";
103 # Close file after writing.
106 # Update ddns config file.
107 &GenerateDDNSConfigFile
();
111 # Toggle enable/disable field. Field is in second position
113 if ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
114 # Open /var/ipfire/ddns/config for writing.
115 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
117 # Lock file for writing.
123 # Read file line by line.
124 foreach my $line (@current) {
128 if ($settings{'ID'} eq $id) {
129 # Splitt lines (splitting element is a single ",") and save values into temp array.
130 @temp = split(/\,/,$line);
132 # Check if we want to toggle ENABLED or WILDCARDS.
133 if ($settings{'ENABLED'} ne '') {
135 print FILE
"$temp[0],$temp[1],$temp[2],$temp[3],$temp[4],$temp[5],$temp[6],$settings{'ENABLED'}\n";
138 # Print unmodified line.
139 print FILE
"$line\n";
145 undef $settings{'ID'};
147 # Close file after writing.
150 # Write out logging notice.
151 &General
::log($Lang::tr
{'ddns hostname modified'});
153 # Update ddns config file.
154 &GenerateDDNSConfigFile
();
158 # Add new accounts, or edit existing ones.
160 if (($settings{'ACTION'} eq $Lang::tr
{'add'}) || ($settings{'ACTION'} eq $Lang::tr
{'update'})) {
161 # Check if a hostname has been given.
162 if ($settings{'HOSTNAME'} eq '') {
163 $errormessage = $Lang::tr
{'hostname not set'};
166 # Check if a valid domainname has been provided.
167 if (!&General
::validdomainname
($settings{'HOSTNAME'})) {
168 $errormessage = $Lang::tr
{'invalid domain name'};
171 # Check if a username has been sent.
172 if ($settings{'LOGIN'} eq '') {
173 $errormessage = $Lang::tr
{'username not set'};
176 # Check if a password has been typed in.
177 # freedns.afraid.org does not require this field.
178 if (($settings{'PASSWORD'} eq '') && ($settings{'SERVICE'} ne 'freedns.afraid.org') && ($settings{'SERVICE'} ne 'regfish.com')) {
179 $errormessage = $Lang::tr
{'password not set'};
182 # Go furter if there was no error.
183 if (!$errormessage) {
184 # Splitt hostname field into 2 parts for storrage.
185 my($hostname, $domain) = split(/\./, $settings{'HOSTNAME'}, 2);
187 # Handle enabled checkbox. When the checkbox is selected a "on" will be returned,
188 # if the checkbox is not checked nothing is returned in this case we set the value to "off".
189 if ($settings{'ENABLED'} ne 'on') {
190 $settings{'ENABLED'} = 'off';
193 # Handle adding new accounts.
194 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
195 # Open /var/ipfire/ddns/config for writing.
196 open(FILE
, ">>$datafile") or die "Unable to open $datafile.";
198 # Lock file for writing.
201 # Add account data to the file.
202 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
204 # Close file after writing.
207 # Write out notice to logfile.
208 &General
::log($Lang::tr
{'ddns hostname added'});
210 # Handle account edditing.
211 } elsif ($settings{'ACTION'} eq $Lang::tr
{'update'}) {
212 # Open /var/ipfire/ddns/config for writing.
213 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
215 # Lock file for writing.
220 # Read file line by line.
221 foreach my $line (@current) {
222 if ($settings{'ID'} eq $id) {
223 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
232 # Close file after writing.
235 # Write out notice to logfile.
236 &General
::log($Lang::tr
{'ddns hostname modified'});
238 undef $settings{'ID'};
240 # Update ddns config file.
241 &GenerateDDNSConfigFile
();
246 # Remove existing accounts.
248 if ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
249 # Open /var/ipfire/ddns/config for writing.
250 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
252 # Lock file for writing.
257 # Read file line by line.
258 foreach my $line (@current) {
259 # Write back every line, except the one we want to drop
260 # (identified by the ID)
261 unless ($settings{'ID'} eq $id) {
268 undef $settings{'ID'};
270 # Close file after writing.
273 # Write out notice to logfile.
274 &General
::log($Lang::tr
{'ddns hostname removed'});
276 # Update ddns config file.
277 &GenerateDDNSConfigFile
();
281 # Read items for editing.
283 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
287 # Read file line by line.
288 foreach my $line (@current) {
289 if ($settings{'ID'} eq $id) {
293 # Splitt lines (splitting element is a single ",") and save values into temp array.
294 @temp = split(/\,/,$line);
296 $settings{'SERVICE'} = $temp[0];
297 $settings{'HOSTNAME'} = "$temp[1].$temp[2]";
298 $settings{'PROXY'} = $temp[3];
299 $settings{'WILDCARDS'} = $temp[4];
300 $settings{'LOGIN'} = $temp[5];
301 $settings{'PASSWORD'} = $temp[6];
302 $settings{'ENABLED'} = $temp[7];
309 &GenerateDDNSConfigFile
();
313 # Handle forced updates.
315 if ($settings{'ACTION'} eq $Lang::tr
{'instant update'}) {
316 system(@ddnsprog) == 0 or die "@ddnsprog failed: $?\n";
320 # Set default values.
322 if (!$settings{'ACTION'}) {
323 $settings{'SERVICE'} = 'dyndns.org';
324 $settings{'ENABLED'} = 'on';
325 $settings{'ID'} = '';
328 &Header
::openpage
($Lang::tr
{'dynamic dns'}, 1, '');
329 &Header
::openbigbox
('100%', 'left', '', $errormessage);
331 # Read file for general ddns settings.
332 &General
::readhash
($settingsfile, \
%settings);
335 $checked{'BEHINDROUTER'}{'RED_IP'} = '';
336 $checked{'BEHINDROUTER'}{'FETCH_IP'} = '';
337 $checked{'BEHINDROUTER'}{$settings{'BEHINDROUTER'}} = "checked='checked'";
339 $checked{'ENABLED'}{'on'} = '';
340 $checked{'ENABLED'}{'off'} = '';
341 $checked{'ENABLED'}{$settings{'ENABLED'}} = "checked='checked'";
343 # Show box for errormessages..
345 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
346 print "<font class='base'>$errormessage </font>";
350 &Header
::openbox
('100%', 'left', $Lang::tr
{'settings'});
353 # Section for general ddns setup.
355 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
358 <td class='base'>$Lang::tr{'dyn dns source choice'}</td>
361 <td class='base'><input type='radio' name='BEHINDROUTER' value='RED_IP' $checked{'BEHINDROUTER'}{'RED_IP'} />
362 $Lang::tr{'use ipfire red ip'}</td>
365 <td class='base'><input type='radio' name='BEHINDROUTER' value='FETCH_IP' $checked{'BEHINDROUTER'}{'FETCH_IP'} />
366 $Lang::tr{'fetch ip from'}</td>
374 <td align='right' valign='top' class='base'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
384 # Section to add or edit an existing entry.
387 my $buttontext = $Lang::tr
{'add'};
389 # Change buttontext and headline if we edit an account.
390 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
391 # Rename button and print headline for updating.
392 $buttontext = $Lang::tr
{'update'};
393 &Header
::openbox
('100%', 'left', $Lang::tr
{'edit an existing host'});
395 # Otherwise use default button text and show headline for adding a new account.
396 &Header
::openbox
('100%', 'left', $Lang::tr
{'add a host'});
400 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
401 <input type='hidden' name='ID' value='$settings{'ID'}' />
404 <td width='25%' class='base'>$Lang::tr{'service'}:</td>
408 # Generate dropdown menu for service selection.
409 print"<select size='1' name='SERVICE'>\n";
413 # Loop to print the providerlist.
414 foreach my $provider (@providers) {
415 # Check if the current provider needs to be selected.
416 if ($provider eq $settings{'SERVICE'}) {
417 $selected = 'selected';
422 # Print out the HTML option field.
423 print "<option value=\"$provider\" $selected>$provider</option>\n";
426 print"</select></td>\n";
428 <td width='20%' class='base'>$Lang::tr{'hostname'}:</td>
429 <td width='30%'><input type='text' name='HOSTNAME' value='$settings{'HOSTNAME'}' /></td>
433 <td class='base'>$Lang::tr{'enabled'}</td>
434 <td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
435 <td class='base'>$Lang::tr{'username'}</td>
436 <td><input type='text' name='LOGIN' value='$settings{'LOGIN'}' /></td>
440 <td class='base'></td>
442 <td class='base'>$Lang::tr{'password'}</td>
443 <td><input type='password' name='PASSWORD' value='$settings{'PASSWORD'}' /></td>
451 <td width='30%' align='right' class='base'>
452 <input type='hidden' name='ACTION' value='$buttontext'>
453 <input type='submit' name='SUBMIT' value='$buttontext'></td>
462 # Third section, display all created ddns hosts.
463 # Re-open file to get changes.
464 open(FILE
, $datafile) or die "Unable to open $datafile.";
468 # Get IP address of the red interface.
469 my $ip = &General
::GetDyndnsRedIP
();
474 &Header
::openbox
('100%', 'left', $Lang::tr
{'current hosts'});
477 <table width='100%' class='tbl'>
479 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'service'}</b></th>
480 <th width='50%' align='center' class='boldbase'><b>$Lang::tr{'hostname'}</b></th>
481 <th width='20%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
485 foreach my $line (@current) {
488 my @temp = split(/\,/,$line);
490 # Handle hostname details. Only connect the values with a dott if both are available.
493 if (($temp[1]) && ($temp[2])) {
494 $hostname="$temp[1].$temp[2]";
496 $hostname="$temp[1]";
499 # Generate value for enable/disable checkbox.
504 if ($temp[7] eq "on") {
506 $gdesc = $Lang::tr
{'click to disable'};
508 # Check if the given hostname is a FQDN before doing a nslookup.
509 if (&General
::validfqdn
($hostname)) {
510 $sync = (&General
::DyndnsServiceSync
($ip,$temp[1], $temp[2]) ?
"<font color='green'>": "<font color='red'>") ;
513 $toggle_enabled = 'off';
515 $sync = "<font color='blue'>";
517 $gdesc = $Lang::tr
{'click to enable'};
518 $toggle_enabled = 'on';
524 if ($settings{'ID'} eq $id) {
525 $col="bgcolor='${Header::colouryellow}'";
526 } elsif (!($temp[0] ~~ @providers)) {
527 $col="bgcolor='#FF4D4D'";
529 $col="bgcolor='$color{'color20'}'";
531 $col="bgcolor='$color{'color22'}'";
534 # The following HTML Code still is part of the loop.
537 <td align='center' $col><a href='http://$temp[0]'>$temp[0]</a></td>
538 <td align='center' $col>$sync$temp[1].$sync$temp[2]</td>
540 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
541 <input type='hidden' name='ID' value='$id'>
542 <input type='hidden' name='ENABLED' value='$toggle_enabled'>
543 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
544 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
547 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
548 <input type='hidden' name='ID' value='$id'>
549 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
550 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
553 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
554 <input type='hidden' name='ID' value='$id'>
555 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
556 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
567 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
568 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
569 <td class='base'>$Lang::tr{'click to disable'}</td>
570 <td> </td>
571 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
572 <td class='base'>$Lang::tr{'click to enable'}</td>
573 <td> </td>
574 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
575 <td class='base'>$Lang::tr{'edit'}</td>
576 <td> </td>
577 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
578 <td class='base'>$Lang::tr{'remove'}</td>
579 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
580 <td align='right' width='30%'><input type='submit' name='ACTION' value='$Lang::tr{'instant update'}' /></td>
589 &Header
::closebigbox
();
590 &Header
::closepage
();
592 # Function to generate the required configuration file for the DDNS tool.
593 sub GenerateDDNSConfigFile
{
595 open(SETTINGS
, "<$datafile") or die "Could not open $datafile.";
597 open(FILE
, ">${General::swroot}/ddns/ddns.conf");
599 # Global configuration options.
600 print FILE
"[config]\n";
602 # Check if we guess our IP address by an extranal server.
603 if ($settings{'BEHINDROUTER'} eq "FETCH_IP") {
604 print FILE
"guess_external_ip = true\n";
606 print FILE
"guess_external_ip = false\n";
609 # Use an upstream proxy and generate proxy url.
611 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
612 if ($proxysettings{'UPSTREAM_PROXY'}) {
613 my $proxy_string = "http://";
615 if ($proxysettings{'UPSTREAM_USER'} && $proxysettings{'UPSTREAM_PASSWORD'}) {
616 $proxy_string .= "$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@";
619 $proxy_string .= $proxysettings{'UPSTREAM_PROXY'};
621 print FILE
"proxy = $proxy_string\n";
630 # Generate array based on the line content (seperator is a single or multiple space's)
631 my @settings = split(/,/, $line);
632 my ($provider, $hostname, $domain, $proxy, $wildcards, $username, $password, $enabled) = @settings;
634 # Skip entries if they are not (longer) supported.
635 next unless ($provider ~~ @providers);
637 # Skip disabled entries.
638 next unless ($enabled eq "on");
640 print FILE
"[$hostname.$domain]\n";
641 print FILE
"provider = $provider\n";
645 # Handle token based auth for various providers.
646 if ($provider ~~ ["dns.lightningwirelabs.com", "entrydns.net", "regfish.com"] && $username eq "token") {
649 # Handle token auth for freedns.afraid.org and regfish.com.
650 } elsif ($provider ~~ ["freedns.afraid.org", "regfish.com"] && $password eq "") {
652 $password = $username;
654 # Handle keys for nsupdate
655 } elsif (($provider eq "nsupdate") && $username && $password) {
656 print FILE
"key = $username\n";
657 print FILE
"secret = $password\n";
662 # Handle keys for nsupdate.info
663 } elsif (($provider eq "nsupdate.info") && $password) {
664 print FILE
"secret = $password\n";
670 # Write auth details.
672 print FILE
"token = $password\n";
673 } elsif ($username && $password) {
674 print FILE
"username = $username\n";
675 print FILE
"password = $password\n";
678 # These providers need to be set to only use IPv4.
679 if ($provider ~~ ["freedns.afraid.org", "nsupdate.info", "opendns.com", "variomedia.de", "zoneedit.com"]) {
680 print FILE
"proto = ipv4\n";
690 # Function which generates an array (@providers) which contains the supported providers.
692 # Get supported providers.
693 open(PROVIDERS
, "/usr/bin/ddns list-providers |");
695 # Create new array to store the providers.
698 while (<PROVIDERS
>) {
701 # Remove following newlines.
704 # Add provider to the array.
705 push(@providers, $provider);