]>
git.ipfire.org Git - people/mfischer/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 ###############################################################################
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";
33 #workaround to suppress a warning when a variable is used only once
34 my @dummy = ( ${Header
::table2colour
}, ${Header
::colouryellow
} );
38 my %mainsettings = ();
39 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
40 &General
::readhash
("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \
%color);
42 # Config file for basic configuration.
43 my $settingsfile = "${General::swroot}/ddns/settings";
45 # Config file to store the configured ddns providers.
46 my $datafile = "${General::swroot}/ddns/config";
48 # Call the ddnsctrl helper binary to perform the update.
49 my @ddnsprog = ("/usr/local/bin/ddnsctrl", "update-all");
52 my $errormessage = '';
54 # DDNS General settings.
55 $settings{'BEHINDROUTER'} = 'RED_IP';
58 $settings{'HOSTNAME'} = '';
59 $settings{'DOMAIN'} = '';
60 $settings{'LOGIN'} = '';
61 $settings{'PASSWORD'} = '';
62 $settings{'TOKEN'} = '';
63 $settings{'ENABLED'} = '';
64 $settings{'PROXY'} = '';
65 $settings{'SERVICE'} = '';
67 $settings{'ACTION'} = '';
69 # Get all supported ddns providers.
70 my @providers = &GetProviders
("all");
72 # Get provider which support a token based authentication mechanism.
73 my @token_provider = &GetProviders
("token-providers");
75 # Hook to regenerate the configuration files, if cgi got called from command line.
76 if ($ENV{"REMOTE_ADDR"} eq "") {
77 &GenerateDDNSConfigFile
();
81 &Header
::showhttpheaders
();
84 &Header
::getcgihash
(\
%settings);
86 # Read configuration file.
87 open(FILE
, "$datafile") or die "Unable to open $datafile.";
92 # Save General Settings.
94 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
95 # Open /var/ipfire/ddns/settings for writing.
96 open(FILE
, ">$settingsfile") or die "Unable to open $settingsfile.";
98 # Lock file for writing.
101 # Check if BEHINDROUTER has been configured.
102 if ($settings{'BEHINDROUTER'} ne '') {
103 print FILE
"BEHINDROUTER=$settings{'BEHINDROUTER'}\n";
106 # Close file after writing.
109 # Update ddns config file.
110 &GenerateDDNSConfigFile
();
114 # Toggle enable/disable field. Field is in second position
116 if ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
117 # Open /var/ipfire/ddns/config for writing.
118 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
120 # Lock file for writing.
126 # Read file line by line.
127 foreach my $line (@current) {
131 if ($settings{'ID'} eq $id) {
132 # Splitt lines (splitting element is a single ",") and save values into temp array.
133 @temp = split(/\,/,$line);
135 # Check if we want to toggle ENABLED or WILDCARDS.
136 if ($settings{'ENABLED'} ne '') {
138 print FILE
"$temp[0],$temp[1],$temp[2],$temp[3],$temp[4],$temp[5],$temp[6],$settings{'ENABLED'}\n";
141 # Print unmodified line.
142 print FILE
"$line\n";
148 undef $settings{'ID'};
150 # Close file after writing.
153 # Write out logging notice.
154 &General
::log($Lang::tr
{'ddns hostname modified'});
156 # Update ddns config file.
157 &GenerateDDNSConfigFile
();
161 # Add new accounts, or edit existing ones.
163 if (($settings{'ACTION'} eq $Lang::tr
{'add'}) || ($settings{'ACTION'} eq $Lang::tr
{'update'})) {
164 # Check if a hostname has been given.
165 if ($settings{'HOSTNAME'} eq '') {
166 $errormessage = $Lang::tr
{'hostname not set'};
169 # Check if a valid domainname has been provided.
170 if (!&General
::validdomainname
($settings{'HOSTNAME'})) {
171 $errormessage = $Lang::tr
{'invalid domain name'};
174 # Check if a username has been sent.
175 if ($settings{'LOGIN'} eq '') {
176 $errormessage = $Lang::tr
{'username not set'};
179 # Check if a password has been typed in.
180 # freedns.afraid.org does not require this field.
181 if (($settings{'PASSWORD'} eq '') && ($settings{'SERVICE'} ne 'freedns.afraid.org') && ($settings{'SERVICE'} ne 'regfish.com')) {
182 $errormessage = $Lang::tr
{'password not set'};
185 # Check if a token has been given for provider which support tokens.
186 if (($settings{'SERVICE'} ~~ @token_provider) && ($settings{'TOKEN'} eq '')) {
187 $errormessage = $Lang::tr
{'token not set'};
190 # Go furter if there was no error.
191 if (!$errormessage) {
192 # Splitt hostname field into 2 parts for storrage.
193 my($hostname, $domain) = split(/\./, $settings{'HOSTNAME'}, 2);
195 # Handle enabled checkbox. When the checkbox is selected a "on" will be returned,
196 # if the checkbox is not checked nothing is returned in this case we set the value to "off".
197 if ($settings{'ENABLED'} ne 'on') {
198 $settings{'ENABLED'} = 'off';
201 # Handle token provider.
202 if($settings{'SERVICE'} ~~ @token_provider) {
203 # Clear username and password if they contain values.
204 undef($settings{'LOGIN'});
205 undef($settings{'PASSWORD'});
207 # Assign the token as a password for saving.
208 $settings{'PASSWORD'} = $settings{'TOKEN'};
211 # Handle adding new accounts.
212 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
213 # Open /var/ipfire/ddns/config for writing.
214 open(FILE
, ">>$datafile") or die "Unable to open $datafile.";
216 # Lock file for writing.
219 # Add account data to the file.
220 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
222 # Close file after writing.
225 # Write out notice to logfile.
226 &General
::log($Lang::tr
{'ddns hostname added'});
228 # Handle account edditing.
229 } elsif ($settings{'ACTION'} eq $Lang::tr
{'update'}) {
230 # Open /var/ipfire/ddns/config for writing.
231 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
233 # Lock file for writing.
238 # Read file line by line.
239 foreach my $line (@current) {
240 if ($settings{'ID'} eq $id) {
241 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
250 # Close file after writing.
253 # Write out notice to logfile.
254 &General
::log($Lang::tr
{'ddns hostname modified'});
256 # Clear settings hash.
259 # Update ddns config file.
260 &GenerateDDNSConfigFile
();
265 # Remove existing accounts.
267 if ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
268 # Open /var/ipfire/ddns/config for writing.
269 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
271 # Lock file for writing.
276 # Read file line by line.
277 foreach my $line (@current) {
278 # Write back every line, except the one we want to drop
279 # (identified by the ID)
280 unless ($settings{'ID'} eq $id) {
287 undef $settings{'ID'};
289 # Close file after writing.
292 # Write out notice to logfile.
293 &General
::log($Lang::tr
{'ddns hostname removed'});
295 # Update ddns config file.
296 &GenerateDDNSConfigFile
();
300 # Read items for editing.
302 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
306 # Read file line by line.
307 foreach my $line (@current) {
308 if ($settings{'ID'} eq $id) {
312 # Splitt lines (splitting element is a single ",") and save values into temp array.
313 @temp = split(/\,/,$line);
315 # Handle hostname details. Only connect the values with a dott if both are available.
318 if (($temp[1]) && ($temp[2])) {
319 $hostname = "$temp[1].$temp[2]";
321 $hostname = "$temp[1]";
324 $settings{'SERVICE'} = $temp[0];
325 $settings{'HOSTNAME'} = $hostname;
326 $settings{'PROXY'} = $temp[3];
327 $settings{'WILDCARDS'} = $temp[4];
328 $settings{'LOGIN'} = $temp[5];
329 $settings{'PASSWORD'} = $temp[6];
330 $settings{'TOKEN'} = $temp[6];
331 $settings{'ENABLED'} = $temp[7];
338 &GenerateDDNSConfigFile
();
342 # Handle forced updates.
344 if ($settings{'ACTION'} eq $Lang::tr
{'instant update'}) {
345 &General
::system(@ddnsprog) == 0 or die "@ddnsprog failed: $?\n";
349 # Set default values.
351 if (!$settings{'ACTION'}) {
352 $settings{'SERVICE'} = 'dyndns.org';
353 $settings{'ENABLED'} = 'on';
354 $settings{'ID'} = '';
357 &Header
::openpage
($Lang::tr
{'dynamic dns'}, 1, '');
362 # Generate Java Script Array which contains the provider that support token.
364 $line = join("', '", @token_provider);
366 print "\t// Array which contains the providers that support token.\n";
367 print "\ttoken_provider = ['$line']\;\n\n";
370 // Java Script function to swap the text input fields for
371 // username and password or token.
372 var update_auth = function() {
373 if(inArray(\$('#SERVICE').val(), token_provider)) {
374 \$('.username').hide();
375 \$('.password').hide();
378 \$('.username').show();
379 \$('.password').show();
384 // Java Script function to check if a given value is part of
386 function inArray(value,array) {
387 var count=array.length;
389 for(var i=0;i<count;i++) {
390 if(array[i]===value){
398 // JQuery function to call corresponding function when
399 // the service provider is changed or the page is loaded for showing/hiding
400 // the username/password or token area.
401 \$(document).ready(function() {
402 \$('#SERVICE').change(update_auth);
410 &Header
::openbigbox
('100%', 'left', '', $errormessage);
412 # Read file for general ddns settings.
413 &General
::readhash
($settingsfile, \
%settings);
416 $checked{'BEHINDROUTER'}{'RED_IP'} = '';
417 $checked{'BEHINDROUTER'}{'FETCH_IP'} = '';
418 $checked{'BEHINDROUTER'}{$settings{'BEHINDROUTER'}} = "checked='checked'";
420 $checked{'ENABLED'}{'on'} = '';
421 $checked{'ENABLED'}{'off'} = '';
422 $checked{'ENABLED'}{$settings{'ENABLED'}} = "checked='checked'";
424 # Show box for errormessages..
426 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
427 print "<font class='base'>$errormessage </font>";
431 &Header
::openbox
('100%', 'left', $Lang::tr
{'settings'});
434 # Section for general ddns setup.
436 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
439 <td class='base'>$Lang::tr{'dyn dns source choice'}</td>
442 <td class='base'><input type='radio' name='BEHINDROUTER' value='RED_IP' $checked{'BEHINDROUTER'}{'RED_IP'} />
443 $Lang::tr{'use ipfire red ip'}</td>
446 <td class='base'><input type='radio' name='BEHINDROUTER' value='FETCH_IP' $checked{'BEHINDROUTER'}{'FETCH_IP'} />
447 $Lang::tr{'fetch ip from'}</td>
455 <td align='right' valign='top' class='base'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
465 # Section to add or edit an existing entry.
468 my $buttontext = $Lang::tr
{'add'};
470 # Change buttontext and headline if we edit an account.
471 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
472 # Rename button and print headline for updating.
473 $buttontext = $Lang::tr
{'update'};
474 &Header
::openbox
('100%', 'left', $Lang::tr
{'edit an existing host'});
476 # Otherwise use default button text and show headline for adding a new account.
477 &Header
::openbox
('100%', 'left', $Lang::tr
{'add a host'});
481 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
482 <input type='hidden' name='ID' value='$settings{'ID'}' />
485 <td width='25%' class='base'>$Lang::tr{'service'}:</td>
489 # Generate dropdown menu for service selection.
490 print"<select size='1' name='SERVICE' id='SERVICE'>\n";
494 # Loop to print the providerlist.
495 foreach my $provider (@providers) {
496 # Check if the current provider needs to be selected.
497 if ($provider eq $settings{'SERVICE'}) {
498 $selected = 'selected';
503 # Print out the HTML option field.
504 print "<option value=\"$provider\" $selected>$provider</option>\n";
507 print"</select></td>\n";
509 <td width='20%' class='base'>$Lang::tr{'hostname'}:</td>
510 <td width='30%'><input type='text' name='HOSTNAME' value='$settings{'HOSTNAME'}' /></td>
514 <td class='base'>$Lang::tr{'enabled'}</td>
515 <td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
517 <td class='username'>$Lang::tr{'username'}</td>
518 <td class='username'><input type='text' name='LOGIN' value='$settings{'LOGIN'}' /></td>
520 <td class='token' style='display:none'>$Lang::tr{'token'}</td>
521 <td class='token' style='display:none'><input type='text' name='TOKEN' value='$settings{'TOKEN'}' /></td>
524 <tr class='password'>
525 <td class='base'></td>
527 <td class='base'>$Lang::tr{'password'}</td>
528 <td><input type='password' name='PASSWORD' value='$settings{'PASSWORD'}' /></td>
536 <td width='30%' align='right' class='base'>
537 <input type='hidden' name='ACTION' value='$buttontext'>
538 <input type='submit' name='SUBMIT' value='$buttontext'></td>
547 # Third section, display all created ddns hosts.
548 # Re-open file to get changes.
549 open(FILE
, $datafile) or die "Unable to open $datafile.";
553 # Get IP address of the red interface.
554 my $ip = &General
::GetDyndnsRedIP
();
559 &Header
::openbox
('100%', 'left', $Lang::tr
{'current hosts'});
562 <table width='100%' class='tbl'>
564 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'service'}</b></th>
565 <th width='50%' align='center' class='boldbase'><b>$Lang::tr{'hostname'}</b></th>
566 <th width='20%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
570 foreach my $line (@current) {
573 my @temp = split(/\,/,$line);
575 # Handle hostname details. Only connect the values with a dott if both are available.
578 if (($temp[1]) && ($temp[2])) {
579 $hostname="$temp[1].$temp[2]";
581 $hostname="$temp[1]";
584 # Generate value for enable/disable checkbox.
589 if ($temp[7] eq "on") {
591 $gdesc = $Lang::tr
{'click to disable'};
593 # Check if the given hostname is a FQDN before doing a nslookup.
594 if (&General
::validfqdn
($hostname)) {
595 $sync = (&General
::DyndnsServiceSync
($ip,$temp[1], $temp[2]) ?
"<font color='green'>": "<font color='red'>") ;
598 $toggle_enabled = 'off';
600 $sync = "<font color='blue'>";
602 $gdesc = $Lang::tr
{'click to enable'};
603 $toggle_enabled = 'on';
609 if ($settings{'ID'} eq $id) {
610 $col="bgcolor='${Header::colouryellow}'";
611 } elsif (!($temp[0] ~~ @providers)) {
612 $col="bgcolor='#FF4D4D'";
614 $col="bgcolor='$color{'color20'}'";
616 $col="bgcolor='$color{'color22'}'";
619 # Handle hostname details. Only connect the values with a dott if both are available.
622 if (($temp[1]) && ($temp[2])) {
623 $hostname="$temp[1].$temp[2]";
625 $hostname="$temp[1]";
628 # The following HTML Code still is part of the loop.
631 <td align='center' $col><a href='http://$temp[0]'>$temp[0]</a></td>
632 <td align='center' $col>$sync$hostname</td>
634 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
635 <input type='hidden' name='ID' value='$id'>
636 <input type='hidden' name='ENABLED' value='$toggle_enabled'>
637 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
638 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
641 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
642 <input type='hidden' name='ID' value='$id'>
643 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
644 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
647 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
648 <input type='hidden' name='ID' value='$id'>
649 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
650 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
661 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
662 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
663 <td class='base'>$Lang::tr{'click to disable'}</td>
664 <td> </td>
665 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
666 <td class='base'>$Lang::tr{'click to enable'}</td>
667 <td> </td>
668 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
669 <td class='base'>$Lang::tr{'edit'}</td>
670 <td> </td>
671 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
672 <td class='base'>$Lang::tr{'remove'}</td>
673 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
674 <td align='right' width='30%'><input type='submit' name='ACTION' value='$Lang::tr{'instant update'}' /></td>
683 &Header
::closebigbox
();
684 &Header
::closepage
();
686 # Function to generate the required configuration file for the DDNS tool.
687 sub GenerateDDNSConfigFile
{
689 open(SETTINGS
, "<$datafile") or die "Could not open $datafile.";
691 open(FILE
, ">${General::swroot}/ddns/ddns.conf");
693 # Global configuration options.
694 print FILE
"[config]\n";
696 # Check if we guess our IP address by an extranal server.
697 if ($settings{'BEHINDROUTER'} eq "FETCH_IP") {
698 print FILE
"guess_external_ip = true\n";
700 print FILE
"guess_external_ip = false\n";
703 # Use an upstream proxy and generate proxy url.
705 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
706 if ($proxysettings{'UPSTREAM_PROXY'}) {
707 my $proxy_string = "http://";
709 if ($proxysettings{'UPSTREAM_USER'} && $proxysettings{'UPSTREAM_PASSWORD'}) {
710 $proxy_string .= "$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@";
713 $proxy_string .= $proxysettings{'UPSTREAM_PROXY'};
715 print FILE
"proxy = $proxy_string\n";
724 # Generate array based on the line content (seperator is a single or multiple space's)
725 my @settings = split(/,/, $line);
726 my ($provider, $hostname, $domain, $proxy, $wildcards, $username, $password, $enabled) = @settings;
728 # Skip entries if they are not (longer) supported.
729 next unless ($provider ~~ @providers);
731 # Skip disabled entries.
732 next unless ($enabled eq "on");
734 # Handle hostname details. Only connect the values with a dott if both are available.
735 if (($hostname) && ($domain)) {
736 print FILE
"[$hostname.$domain]\n";
738 print FILE
"[$hostname]\n";
741 print FILE
"provider = $provider\n";
745 # Handle token based auth for various providers.
746 if ($provider ~~ @token_provider) {
750 # Handle token auth for freedns.afraid.org and regfish.com.
751 if ($provider ~~ ["freedns.afraid.org", "regfish.com"] && $password eq "") {
753 $password = $username;
755 # Handle keys for nsupdate
756 } elsif (($provider eq "nsupdate") && $username && $password) {
757 print FILE
"key = $username\n";
758 print FILE
"secret = $password\n";
763 # Handle keys for nsupdate.info
764 } elsif (($provider eq "nsupdate.info") && $password) {
765 print FILE
"secret = $password\n";
771 # Write auth details.
773 print FILE
"token = $password\n";
774 } elsif ($username && $password) {
775 print FILE
"username = $username\n";
776 print FILE
"password = $password\n";
786 # Function which generates an array (@providers) which contains the supported providers.
787 sub GetProviders
($) {
790 # Set default type to get all providers
791 $type = $type ?
$type : "all";
793 # Check if the requested type is "token-providers".
794 if ($type eq "token-providers") {
795 # Call ddns util to only get providers which supports token based auth.
796 open(PROVIDERS
, "/usr/bin/ddns list-token-providers |");
798 # Get all supported providers.
799 open(PROVIDERS
, "/usr/bin/ddns list-providers |");
802 # Create new array to store the providers.
805 while (<PROVIDERS
>) {
808 # Remove following newlines.
811 # Add provider to the array.
812 push(@providers, $provider);