]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ddns.cgi
34475b75cd989d0b6313a18326ef2f173630a0f8
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 ###############################################################################
23 use experimental
'smartmatch';
25 # enable only the following on debugging purpose
27 #use CGI::Carp 'fatalsToBrowser';
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
32 require "${General::swroot}/http-client-functions.pl";
34 #workaround to suppress a warning when a variable is used only once
35 my @dummy = ( ${Header
::table2colour
}, ${Header
::colouryellow
} );
39 my %mainsettings = ();
40 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
41 &General
::readhash
("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \
%color);
43 # Config file for basic configuration.
44 my $settingsfile = "${General::swroot}/ddns/settings";
46 # Config file to store the configured ddns providers.
47 my $datafile = "${General::swroot}/ddns/config";
49 # Call the ddnsctrl helper binary to perform the update.
50 my @ddnsprog = ("/usr/local/bin/ddnsctrl", "update-all");
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{'TOKEN'} = '';
64 $settings{'ENABLED'} = '';
65 $settings{'PROXY'} = '';
66 $settings{'SERVICE'} = '';
68 $settings{'ACTION'} = '';
70 # Get all supported ddns providers.
71 my @providers = &GetProviders
("all");
73 # Get provider which support a token based authentication mechanism.
74 my @token_provider = &GetProviders
("token-providers");
76 # Hook to regenerate the configuration files, if cgi got called from command line.
77 if ($ENV{"REMOTE_ADDR"} eq "") {
78 &GenerateDDNSConfigFile
();
82 &Header
::showhttpheaders
();
85 &Header
::getcgihash
(\
%settings);
87 # Read configuration file.
88 open(FILE
, "$datafile") or die "Unable to open $datafile.";
93 # Save General Settings.
95 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
96 # Open /var/ipfire/ddns/settings for writing.
97 open(FILE
, ">$settingsfile") or die "Unable to open $settingsfile.";
99 # Lock file for writing.
102 # Check if BEHINDROUTER has been configured.
103 if ($settings{'BEHINDROUTER'} ne '') {
104 print FILE
"BEHINDROUTER=$settings{'BEHINDROUTER'}\n";
107 # Close file after writing.
110 # Update ddns config file.
111 &GenerateDDNSConfigFile
();
115 # Toggle enable/disable field. Field is in second position
117 if ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
118 # Open /var/ipfire/ddns/config for writing.
119 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
121 # Lock file for writing.
127 # Read file line by line.
128 foreach my $line (@current) {
132 if ($settings{'ID'} eq $id) {
133 # Splitt lines (splitting element is a single ",") and save values into temp array.
134 @temp = split(/\,/,$line);
136 # Check if we want to toggle ENABLED or WILDCARDS.
137 if ($settings{'ENABLED'} ne '') {
139 print FILE
"$temp[0],$temp[1],$temp[2],$temp[3],$temp[4],$temp[5],$temp[6],$settings{'ENABLED'}\n";
142 # Print unmodified line.
143 print FILE
"$line\n";
149 undef $settings{'ID'};
151 # Close file after writing.
154 # Write out logging notice.
155 &General
::log($Lang::tr
{'ddns hostname modified'});
157 # Update ddns config file.
158 &GenerateDDNSConfigFile
();
162 # Add new accounts, or edit existing ones.
164 if (($settings{'ACTION'} eq $Lang::tr
{'add'}) || ($settings{'ACTION'} eq $Lang::tr
{'update'})) {
165 # Check if a hostname has been given.
166 if ($settings{'HOSTNAME'} eq '') {
167 $errormessage = $Lang::tr
{'hostname not set'};
170 # Check if a valid domainname has been provided.
171 if (!&General
::validdomainname
($settings{'HOSTNAME'})) {
172 $errormessage = $Lang::tr
{'invalid domain name'};
175 # Check if the choosen provider supports token based authentication.
176 if ($settings{'SERVICE'} ~~ @token_provider) {
177 # Check if a token has been given.
178 unless ($settings{'TOKEN'}) {
179 $errormessage = $Lang::tr
{'token not set'};
182 # Automatically set the username to token.
183 $settings{'LOGIN'} = "token";
185 # A provider without token support has been choosen.
187 # Check if a username has been sent.
188 if ($settings{'LOGIN'} eq '') {
189 $errormessage = $Lang::tr
{'username not set'};
192 # Check if a password has been typed in.
193 # freedns.afraid.org does not require this field.
194 if (($settings{'PASSWORD'} eq '') && ($settings{'SERVICE'} ne 'freedns.afraid.org') && ($settings{'SERVICE'} ne 'regfish.com')) {
195 $errormessage = $Lang::tr
{'password not set'};
199 # Go furter if there was no error.
200 if (!$errormessage) {
201 # Splitt hostname field into 2 parts for storrage.
202 my($hostname, $domain) = split(/\./, $settings{'HOSTNAME'}, 2);
204 # Handle enabled checkbox. When the checkbox is selected a "on" will be returned,
205 # if the checkbox is not checked nothing is returned in this case we set the value to "off".
206 if ($settings{'ENABLED'} ne 'on') {
207 $settings{'ENABLED'} = 'off';
210 # Handle token provider.
211 if($settings{'SERVICE'} ~~ @token_provider) {
212 # Clear username and password if they contain values.
213 undef($settings{'LOGIN'});
214 undef($settings{'PASSWORD'});
216 # Assign the token as a password for saving.
217 $settings{'PASSWORD'} = $settings{'TOKEN'};
220 # Handle adding new accounts.
221 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
222 # Open /var/ipfire/ddns/config for writing.
223 open(FILE
, ">>$datafile") or die "Unable to open $datafile.";
225 # Lock file for writing.
228 # Add account data to the file.
229 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
231 # Close file after writing.
234 # Write out notice to logfile.
235 &General
::log($Lang::tr
{'ddns hostname added'});
237 # Handle account edditing.
238 } elsif ($settings{'ACTION'} eq $Lang::tr
{'update'}) {
239 # Open /var/ipfire/ddns/config for writing.
240 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
242 # Lock file for writing.
247 # Read file line by line.
248 foreach my $line (@current) {
249 if ($settings{'ID'} eq $id) {
250 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
259 # Close file after writing.
262 # Write out notice to logfile.
263 &General
::log($Lang::tr
{'ddns hostname modified'});
265 # Clear settings hash.
268 # Update ddns config file.
269 &GenerateDDNSConfigFile
();
274 # Remove existing accounts.
276 if ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
277 # Open /var/ipfire/ddns/config for writing.
278 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
280 # Lock file for writing.
285 # Read file line by line.
286 foreach my $line (@current) {
287 # Write back every line, except the one we want to drop
288 # (identified by the ID)
289 unless ($settings{'ID'} eq $id) {
296 undef $settings{'ID'};
298 # Close file after writing.
301 # Write out notice to logfile.
302 &General
::log($Lang::tr
{'ddns hostname removed'});
304 # Update ddns config file.
305 &GenerateDDNSConfigFile
();
309 # Read items for editing.
311 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
315 # Read file line by line.
316 foreach my $line (@current) {
317 if ($settings{'ID'} eq $id) {
321 # Splitt lines (splitting element is a single ",") and save values into temp array.
322 @temp = split(/\,/,$line);
324 # Handle hostname details. Only connect the values with a dott if both are available.
327 if (($temp[1]) && ($temp[2])) {
328 $hostname = "$temp[1].$temp[2]";
330 $hostname = "$temp[1]";
333 $settings{'SERVICE'} = $temp[0];
334 $settings{'HOSTNAME'} = $hostname;
335 $settings{'PROXY'} = $temp[3];
336 $settings{'WILDCARDS'} = $temp[4];
337 $settings{'LOGIN'} = $temp[5];
338 $settings{'PASSWORD'} = $temp[6];
339 $settings{'TOKEN'} = $temp[6];
340 $settings{'ENABLED'} = $temp[7];
347 &GenerateDDNSConfigFile
();
351 # Handle forced updates.
353 if ($settings{'ACTION'} eq $Lang::tr
{'instant update'}) {
354 &General
::system(@ddnsprog) == 0 or die "@ddnsprog failed: $?\n";
358 # Set default values.
360 if (!$settings{'ACTION'}) {
361 $settings{'SERVICE'} = 'dyndns.org';
362 $settings{'ENABLED'} = 'on';
363 $settings{'ID'} = '';
366 &Header
::openpage
($Lang::tr
{'dynamic dns'}, 1, '');
371 # Generate Java Script Array which contains the provider that support token.
373 $line = join("', '", @token_provider);
375 print "\t// Array which contains the providers that support token.\n";
376 print "\ttoken_provider = ['$line']\;\n\n";
379 // Java Script function to swap the text input fields for
380 // username and password or token.
381 var update_auth = function() {
382 if(inArray(\$('#SERVICE').val(), token_provider)) {
383 \$('.username').hide();
384 \$('.password').hide();
387 \$('.username').show();
388 \$('.password').show();
393 // Java Script function to check if a given value is part of
395 function inArray(value,array) {
396 var count=array.length;
398 for(var i=0;i<count;i++) {
399 if(array[i]===value){
407 // JQuery function to call corresponding function when
408 // the service provider is changed or the page is loaded for showing/hiding
409 // the username/password or token area.
410 \$(document).ready(function() {
411 \$('#SERVICE').change(update_auth);
419 &Header
::openbigbox
('100%', 'left', '', $errormessage);
421 # Read file for general ddns settings.
422 &General
::readhash
($settingsfile, \
%settings);
425 $checked{'BEHINDROUTER'}{'RED_IP'} = '';
426 $checked{'BEHINDROUTER'}{'FETCH_IP'} = '';
427 $checked{'BEHINDROUTER'}{$settings{'BEHINDROUTER'}} = "checked='checked'";
429 $checked{'ENABLED'}{'on'} = '';
430 $checked{'ENABLED'}{'off'} = '';
431 $checked{'ENABLED'}{$settings{'ENABLED'}} = "checked='checked'";
433 # Show box for errormessages..
435 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
436 print "<font class='base'>$errormessage </font>";
440 &Header
::openbox
('100%', 'left', $Lang::tr
{'settings'});
443 # Section for general ddns setup.
445 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
448 <td class='base'>$Lang::tr{'dyn dns source choice'}</td>
451 <td class='base'><input type='radio' name='BEHINDROUTER' value='RED_IP' $checked{'BEHINDROUTER'}{'RED_IP'} />
452 $Lang::tr{'use ipfire red ip'}</td>
455 <td class='base'><input type='radio' name='BEHINDROUTER' value='FETCH_IP' $checked{'BEHINDROUTER'}{'FETCH_IP'} />
456 $Lang::tr{'fetch ip from'}</td>
464 <td align='right' valign='top' class='base'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
474 # Section to add or edit an existing entry.
477 my $buttontext = $Lang::tr
{'add'};
479 # Change buttontext and headline if we edit an account.
480 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
481 # Rename button and print headline for updating.
482 $buttontext = $Lang::tr
{'update'};
483 &Header
::openbox
('100%', 'left', $Lang::tr
{'edit an existing host'});
485 # Otherwise use default button text and show headline for adding a new account.
486 &Header
::openbox
('100%', 'left', $Lang::tr
{'add a host'});
490 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
491 <input type='hidden' name='ID' value='$settings{'ID'}' />
494 <td width='25%' class='base'>$Lang::tr{'service'}:</td>
498 # Generate dropdown menu for service selection.
499 print"<select size='1' name='SERVICE' id='SERVICE'>\n";
503 # Loop to print the providerlist.
504 foreach my $provider (@providers) {
505 # Check if the current provider needs to be selected.
506 if ($provider eq $settings{'SERVICE'}) {
507 $selected = 'selected';
512 # Print out the HTML option field.
513 print "<option value=\"$provider\" $selected>$provider</option>\n";
516 print"</select></td>\n";
518 <td width='20%' class='base'>$Lang::tr{'hostname'}:</td>
519 <td width='30%'><input type='text' name='HOSTNAME' value='$settings{'HOSTNAME'}' /></td>
523 <td class='base'>$Lang::tr{'enabled'}</td>
524 <td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
526 <td class='username'>$Lang::tr{'username'}</td>
527 <td class='username'><input type='text' name='LOGIN' value='$settings{'LOGIN'}' /></td>
529 <td class='token' style='display:none'>$Lang::tr{'token'}</td>
530 <td class='token' style='display:none'><input type='text' name='TOKEN' value='$settings{'TOKEN'}' /></td>
533 <tr class='password'>
534 <td class='base'></td>
536 <td class='base'>$Lang::tr{'password'}</td>
537 <td><input type='password' name='PASSWORD' value='$settings{'PASSWORD'}' /></td>
545 <td width='30%' align='right' class='base'>
546 <input type='hidden' name='ACTION' value='$buttontext'>
547 <input type='submit' name='SUBMIT' value='$buttontext'></td>
556 # Third section, display all created ddns hosts.
557 # Re-open file to get changes.
558 open(FILE
, $datafile) or die "Unable to open $datafile.";
562 # Get IP address of the red interface.
563 my $ip = &HTTPClient
::GetDyndnsRedIP
();
568 &Header
::openbox
('100%', 'left', $Lang::tr
{'current hosts'});
571 <table width='100%' class='tbl'>
573 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'service'}</b></th>
574 <th width='50%' align='center' class='boldbase'><b>$Lang::tr{'hostname'}</b></th>
575 <th width='20%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
579 foreach my $line (@current) {
582 my @temp = split(/\,/,$line);
584 # Handle hostname details. Only connect the values with a dott if both are available.
587 if (($temp[1]) && ($temp[2])) {
588 $hostname="$temp[1].$temp[2]";
590 $hostname="$temp[1]";
593 # Generate value for enable/disable checkbox.
598 if ($temp[7] eq "on") {
600 $gdesc = $Lang::tr
{'click to disable'};
602 # Check if the given hostname is a FQDN before doing a nslookup.
603 if (&General
::validfqdn
($hostname)) {
604 $sync = (&General
::DyndnsServiceSync
($ip,$temp[1], $temp[2]) ?
"<font color='green'>": "<font color='red'>") ;
607 $toggle_enabled = 'off';
609 $sync = "<font color='blue'>";
611 $gdesc = $Lang::tr
{'click to enable'};
612 $toggle_enabled = 'on';
618 if ($settings{'ID'} eq $id) {
619 $col="bgcolor='${Header::colouryellow}'";
620 } elsif (!($temp[0] ~~ @providers)) {
621 $col="bgcolor='#FF4D4D'";
623 $col="bgcolor='$color{'color20'}'";
625 $col="bgcolor='$color{'color22'}'";
628 # Handle hostname details. Only connect the values with a dott if both are available.
631 if (($temp[1]) && ($temp[2])) {
632 $hostname="$temp[1].$temp[2]";
634 $hostname="$temp[1]";
637 # The following HTML Code still is part of the loop.
640 <td align='center' $col><a href='http://$temp[0]'>$temp[0]</a></td>
641 <td align='center' $col>$sync$hostname</td>
643 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
644 <input type='hidden' name='ID' value='$id'>
645 <input type='hidden' name='ENABLED' value='$toggle_enabled'>
646 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
647 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
650 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
651 <input type='hidden' name='ID' value='$id'>
652 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
653 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
656 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
657 <input type='hidden' name='ID' value='$id'>
658 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
659 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
670 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
671 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
672 <td class='base'>$Lang::tr{'click to disable'}</td>
673 <td> </td>
674 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
675 <td class='base'>$Lang::tr{'click to enable'}</td>
676 <td> </td>
677 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
678 <td class='base'>$Lang::tr{'edit'}</td>
679 <td> </td>
680 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
681 <td class='base'>$Lang::tr{'remove'}</td>
682 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
683 <td align='right' width='30%'><input type='submit' name='ACTION' value='$Lang::tr{'instant update'}' /></td>
692 &Header
::closebigbox
();
693 &Header
::closepage
();
695 # Function to generate the required configuration file for the DDNS tool.
696 sub GenerateDDNSConfigFile
{
698 open(SETTINGS
, "<$datafile") or die "Could not open $datafile.";
700 open(FILE
, ">${General::swroot}/ddns/ddns.conf");
702 # Global configuration options.
703 print FILE
"[config]\n";
705 # Check if we guess our IP address by an extranal server.
706 if ($settings{'BEHINDROUTER'} eq "FETCH_IP") {
707 print FILE
"guess_external_ip = true\n";
709 print FILE
"guess_external_ip = false\n";
712 # Use an upstream proxy and generate proxy url.
714 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
715 if ($proxysettings{'UPSTREAM_PROXY'}) {
716 my $proxy_string = "http://";
718 if ($proxysettings{'UPSTREAM_USER'} && $proxysettings{'UPSTREAM_PASSWORD'}) {
719 $proxy_string .= "$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@";
722 $proxy_string .= $proxysettings{'UPSTREAM_PROXY'};
724 print FILE
"proxy = $proxy_string\n";
733 # Generate array based on the line content (seperator is a single or multiple space's)
734 my @settings = split(/,/, $line);
735 my ($provider, $hostname, $domain, $proxy, $wildcards, $username, $password, $enabled) = @settings;
737 # Skip entries if they are not (longer) supported.
738 next unless ($provider ~~ @providers);
740 # Skip disabled entries.
741 next unless ($enabled eq "on");
743 # Handle hostname details. Only connect the values with a dott if both are available.
744 if (($hostname) && ($domain)) {
745 print FILE
"[$hostname.$domain]\n";
747 print FILE
"[$hostname]\n";
750 print FILE
"provider = $provider\n";
754 # Handle token based auth for various providers.
755 if ($provider ~~ @token_provider) {
759 # Handle token auth for freedns.afraid.org and regfish.com.
760 if ($provider ~~ ["freedns.afraid.org", "regfish.com"] && $password eq "") {
762 $password = $username;
764 # Handle keys for nsupdate
765 } elsif (($provider eq "nsupdate") && $username && $password) {
766 print FILE
"key = $username\n";
767 print FILE
"secret = $password\n";
772 # Handle keys for nsupdate.info
773 } elsif (($provider eq "nsupdate.info") && $password) {
774 print FILE
"secret = $password\n";
780 # Write auth details.
782 print FILE
"token = $password\n";
783 } elsif ($username && $password) {
784 print FILE
"username = $username\n";
785 print FILE
"password = $password\n";
795 # Function which generates an array (@providers) which contains the supported providers.
796 sub GetProviders
($) {
799 # Set default type to get all providers
800 $type = $type ?
$type : "all";
802 # Check if the requested type is "token-providers".
803 if ($type eq "token-providers") {
804 # Call ddns util to only get providers which supports token based auth.
805 open(PROVIDERS
, "/usr/bin/ddns list-token-providers |");
807 # Get all supported providers.
808 open(PROVIDERS
, "/usr/bin/ddns list-providers |");
811 # Create new array to store the providers.
814 while (<PROVIDERS
>) {
817 # Remove following newlines.
820 # Add provider to the array.
821 push(@providers, $provider);