]>
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 # Call the ddnsctrl helper binary to perform the update.
48 my @ddnsprog = ("/usr/local/bin/ddnsctrl", "update-all");
51 my $errormessage = '';
53 # DDNS General settings.
54 $settings{'BEHINDROUTER'} = 'RED_IP';
57 $settings{'HOSTNAME'} = '';
58 $settings{'DOMAIN'} = '';
59 $settings{'LOGIN'} = '';
60 $settings{'PASSWORD'} = '';
61 $settings{'ENABLED'} = '';
62 $settings{'PROXY'} = '';
63 $settings{'SERVICE'} = '';
65 $settings{'ACTION'} = '';
67 # Get supported ddns providers.
68 my @providers = &GetProviders
();
70 # Hook to regenerate the configuration files, if cgi got called from command line.
71 if ($ENV{"REMOTE_ADDR"} eq "") {
72 &GenerateDDNSConfigFile
();
76 &Header
::showhttpheaders
();
79 &Header
::getcgihash
(\
%settings);
81 # Read configuration file.
82 open(FILE
, "$datafile") or die "Unable to open $datafile.";
87 # Save General Settings.
89 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
90 # Open /var/ipfire/ddns/settings for writing.
91 open(FILE
, ">$settingsfile") or die "Unable to open $settingsfile.";
93 # Lock file for writing.
96 # Check if BEHINDROUTER has been configured.
97 if ($settings{'BEHINDROUTER'} ne '') {
98 print FILE
"BEHINDROUTER=$settings{'BEHINDROUTER'}\n";
101 # Close file after writing.
104 # Update ddns config file.
105 &GenerateDDNSConfigFile
();
109 # Toggle enable/disable field. Field is in second position
111 if ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
112 # Open /var/ipfire/ddns/config for writing.
113 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
115 # Lock file for writing.
121 # Read file line by line.
122 foreach my $line (@current) {
126 if ($settings{'ID'} eq $id) {
127 # Splitt lines (splitting element is a single ",") and save values into temp array.
128 @temp = split(/\,/,$line);
130 # Check if we want to toggle ENABLED or WILDCARDS.
131 if ($settings{'ENABLED'} ne '') {
133 print FILE
"$temp[0],$temp[1],$temp[2],$temp[3],$temp[4],$temp[5],$temp[6],$settings{'ENABLED'}\n";
136 # Print unmodified line.
137 print FILE
"$line\n";
143 undef $settings{'ID'};
145 # Close file after writing.
148 # Write out logging notice.
149 &General
::log($Lang::tr
{'ddns hostname modified'});
151 # Update ddns config file.
152 &GenerateDDNSConfigFile
();
156 # Add new accounts, or edit existing ones.
158 if (($settings{'ACTION'} eq $Lang::tr
{'add'}) || ($settings{'ACTION'} eq $Lang::tr
{'update'})) {
159 # Check if a hostname has been given.
160 if ($settings{'HOSTNAME'} eq '') {
161 $errormessage = $Lang::tr
{'hostname not set'};
164 # Check if a valid domainname has been provided.
165 if (!&General
::validdomainname
($settings{'HOSTNAME'})) {
166 $errormessage = $Lang::tr
{'invalid domain name'};
169 # Check if a username has been sent.
170 if ($settings{'LOGIN'} eq '') {
171 $errormessage = $Lang::tr
{'username not set'};
174 # Check if a password has been typed in.
175 # freedns.afraid.org does not require this field.
176 if (($settings{'PASSWORD'} eq '') && ($settings{'SERVICE'} ne 'freedns.afraid.org') && ($settings{'SERVICE'} ne 'regfish.com')) {
177 $errormessage = $Lang::tr
{'password not set'};
180 # Go furter if there was no error.
181 if (!$errormessage) {
182 # Splitt hostname field into 2 parts for storrage.
183 my($hostname, $domain) = split(/\./, $settings{'HOSTNAME'}, 2);
185 # Handle enabled checkbox. When the checkbox is selected a "on" will be returned,
186 # if the checkbox is not checked nothing is returned in this case we set the value to "off".
187 if ($settings{'ENABLED'} ne 'on') {
188 $settings{'ENABLED'} = 'off';
191 # Handle adding new accounts.
192 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
193 # Open /var/ipfire/ddns/config for writing.
194 open(FILE
, ">>$datafile") or die "Unable to open $datafile.";
196 # Lock file for writing.
199 # Add account data to the file.
200 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
202 # Close file after writing.
205 # Write out notice to logfile.
206 &General
::log($Lang::tr
{'ddns hostname added'});
208 # Handle account edditing.
209 } elsif ($settings{'ACTION'} eq $Lang::tr
{'update'}) {
210 # Open /var/ipfire/ddns/config for writing.
211 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
213 # Lock file for writing.
218 # Read file line by line.
219 foreach my $line (@current) {
220 if ($settings{'ID'} eq $id) {
221 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
230 # Close file after writing.
233 # Write out notice to logfile.
234 &General
::log($Lang::tr
{'ddns hostname modified'});
236 undef $settings{'ID'};
238 # Update ddns config file.
239 &GenerateDDNSConfigFile
();
244 # Remove existing accounts.
246 if ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
247 # Open /var/ipfire/ddns/config for writing.
248 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
250 # Lock file for writing.
255 # Read file line by line.
256 foreach my $line (@current) {
257 # Write back every line, except the one we want to drop
258 # (identified by the ID)
259 unless ($settings{'ID'} eq $id) {
266 undef $settings{'ID'};
268 # Close file after writing.
271 # Write out notice to logfile.
272 &General
::log($Lang::tr
{'ddns hostname removed'});
274 # Update ddns config file.
275 &GenerateDDNSConfigFile
();
279 # Read items for editing.
281 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
285 # Read file line by line.
286 foreach my $line (@current) {
287 if ($settings{'ID'} eq $id) {
291 # Splitt lines (splitting element is a single ",") and save values into temp array.
292 @temp = split(/\,/,$line);
294 # Handle hostname details. Only connect the values with a dott if both are available.
297 if (($temp[1]) && ($temp[2])) {
298 $hostname = "$temp[1].$temp[2]";
300 $hostname = "$temp[1]";
303 $settings{'SERVICE'} = $temp[0];
304 $settings{'HOSTNAME'} = $hostname;
305 $settings{'PROXY'} = $temp[3];
306 $settings{'WILDCARDS'} = $temp[4];
307 $settings{'LOGIN'} = $temp[5];
308 $settings{'PASSWORD'} = $temp[6];
309 $settings{'ENABLED'} = $temp[7];
316 &GenerateDDNSConfigFile
();
320 # Handle forced updates.
322 if ($settings{'ACTION'} eq $Lang::tr
{'instant update'}) {
323 system(@ddnsprog) == 0 or die "@ddnsprog failed: $?\n";
327 # Set default values.
329 if (!$settings{'ACTION'}) {
330 $settings{'SERVICE'} = 'dyndns.org';
331 $settings{'ENABLED'} = 'on';
332 $settings{'ID'} = '';
335 &Header
::openpage
($Lang::tr
{'dynamic dns'}, 1, '');
336 &Header
::openbigbox
('100%', 'left', '', $errormessage);
338 # Read file for general ddns settings.
339 &General
::readhash
($settingsfile, \
%settings);
342 $checked{'BEHINDROUTER'}{'RED_IP'} = '';
343 $checked{'BEHINDROUTER'}{'FETCH_IP'} = '';
344 $checked{'BEHINDROUTER'}{$settings{'BEHINDROUTER'}} = "checked='checked'";
346 $checked{'ENABLED'}{'on'} = '';
347 $checked{'ENABLED'}{'off'} = '';
348 $checked{'ENABLED'}{$settings{'ENABLED'}} = "checked='checked'";
350 # Show box for errormessages..
352 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
353 print "<font class='base'>$errormessage </font>";
357 &Header
::openbox
('100%', 'left', $Lang::tr
{'settings'});
360 # Section for general ddns setup.
362 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
365 <td class='base'>$Lang::tr{'dyn dns source choice'}</td>
368 <td class='base'><input type='radio' name='BEHINDROUTER' value='RED_IP' $checked{'BEHINDROUTER'}{'RED_IP'} />
369 $Lang::tr{'use ipfire red ip'}</td>
372 <td class='base'><input type='radio' name='BEHINDROUTER' value='FETCH_IP' $checked{'BEHINDROUTER'}{'FETCH_IP'} />
373 $Lang::tr{'fetch ip from'}</td>
381 <td align='right' valign='top' class='base'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
391 # Section to add or edit an existing entry.
394 my $buttontext = $Lang::tr
{'add'};
396 # Change buttontext and headline if we edit an account.
397 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
398 # Rename button and print headline for updating.
399 $buttontext = $Lang::tr
{'update'};
400 &Header
::openbox
('100%', 'left', $Lang::tr
{'edit an existing host'});
402 # Otherwise use default button text and show headline for adding a new account.
403 &Header
::openbox
('100%', 'left', $Lang::tr
{'add a host'});
407 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
408 <input type='hidden' name='ID' value='$settings{'ID'}' />
411 <td width='25%' class='base'>$Lang::tr{'service'}:</td>
415 # Generate dropdown menu for service selection.
416 print"<select size='1' name='SERVICE'>\n";
420 # Loop to print the providerlist.
421 foreach my $provider (@providers) {
422 # Check if the current provider needs to be selected.
423 if ($provider eq $settings{'SERVICE'}) {
424 $selected = 'selected';
429 # Print out the HTML option field.
430 print "<option value=\"$provider\" $selected>$provider</option>\n";
433 print"</select></td>\n";
435 <td width='20%' class='base'>$Lang::tr{'hostname'}:</td>
436 <td width='30%'><input type='text' name='HOSTNAME' value='$settings{'HOSTNAME'}' /></td>
440 <td class='base'>$Lang::tr{'enabled'}</td>
441 <td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
442 <td class='base'>$Lang::tr{'username'}</td>
443 <td><input type='text' name='LOGIN' value='$settings{'LOGIN'}' /></td>
447 <td class='base'></td>
449 <td class='base'>$Lang::tr{'password'}</td>
450 <td><input type='password' name='PASSWORD' value='$settings{'PASSWORD'}' /></td>
458 <td width='30%' align='right' class='base'>
459 <input type='hidden' name='ACTION' value='$buttontext'>
460 <input type='submit' name='SUBMIT' value='$buttontext'></td>
469 # Third section, display all created ddns hosts.
470 # Re-open file to get changes.
471 open(FILE
, $datafile) or die "Unable to open $datafile.";
475 # Get IP address of the red interface.
476 my $ip = &General
::GetDyndnsRedIP
();
481 &Header
::openbox
('100%', 'left', $Lang::tr
{'current hosts'});
484 <table width='100%' class='tbl'>
486 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'service'}</b></th>
487 <th width='50%' align='center' class='boldbase'><b>$Lang::tr{'hostname'}</b></th>
488 <th width='20%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
492 foreach my $line (@current) {
495 my @temp = split(/\,/,$line);
497 # Handle hostname details. Only connect the values with a dott if both are available.
500 if (($temp[1]) && ($temp[2])) {
501 $hostname="$temp[1].$temp[2]";
503 $hostname="$temp[1]";
506 # Generate value for enable/disable checkbox.
511 if ($temp[7] eq "on") {
513 $gdesc = $Lang::tr
{'click to disable'};
515 # Check if the given hostname is a FQDN before doing a nslookup.
516 if (&General
::validfqdn
($hostname)) {
517 $sync = (&General
::DyndnsServiceSync
($ip,$temp[1], $temp[2]) ?
"<font color='green'>": "<font color='red'>") ;
520 $toggle_enabled = 'off';
522 $sync = "<font color='blue'>";
524 $gdesc = $Lang::tr
{'click to enable'};
525 $toggle_enabled = 'on';
531 if ($settings{'ID'} eq $id) {
532 $col="bgcolor='${Header::colouryellow}'";
533 } elsif (!($temp[0] ~~ @providers)) {
534 $col="bgcolor='#FF4D4D'";
536 $col="bgcolor='$color{'color20'}'";
538 $col="bgcolor='$color{'color22'}'";
541 # Handle hostname details. Only connect the values with a dott if both are available.
544 if (($temp[1]) && ($temp[2])) {
545 $hostname="$temp[1].$temp[2]";
547 $hostname="$temp[1]";
550 # The following HTML Code still is part of the loop.
553 <td align='center' $col><a href='http://$temp[0]'>$temp[0]</a></td>
554 <td align='center' $col>$sync$hostname</td>
556 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
557 <input type='hidden' name='ID' value='$id'>
558 <input type='hidden' name='ENABLED' value='$toggle_enabled'>
559 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
560 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
563 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
564 <input type='hidden' name='ID' value='$id'>
565 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
566 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
569 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
570 <input type='hidden' name='ID' value='$id'>
571 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
572 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
583 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
584 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
585 <td class='base'>$Lang::tr{'click to disable'}</td>
586 <td> </td>
587 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
588 <td class='base'>$Lang::tr{'click to enable'}</td>
589 <td> </td>
590 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
591 <td class='base'>$Lang::tr{'edit'}</td>
592 <td> </td>
593 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
594 <td class='base'>$Lang::tr{'remove'}</td>
595 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
596 <td align='right' width='30%'><input type='submit' name='ACTION' value='$Lang::tr{'instant update'}' /></td>
605 &Header
::closebigbox
();
606 &Header
::closepage
();
608 # Function to generate the required configuration file for the DDNS tool.
609 sub GenerateDDNSConfigFile
{
611 open(SETTINGS
, "<$datafile") or die "Could not open $datafile.";
613 open(FILE
, ">${General::swroot}/ddns/ddns.conf");
615 # Global configuration options.
616 print FILE
"[config]\n";
618 # Check if we guess our IP address by an extranal server.
619 if ($settings{'BEHINDROUTER'} eq "FETCH_IP") {
620 print FILE
"guess_external_ip = true\n";
622 print FILE
"guess_external_ip = false\n";
625 # Use an upstream proxy and generate proxy url.
627 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
628 if ($proxysettings{'UPSTREAM_PROXY'}) {
629 my $proxy_string = "http://";
631 if ($proxysettings{'UPSTREAM_USER'} && $proxysettings{'UPSTREAM_PASSWORD'}) {
632 $proxy_string .= "$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@";
635 $proxy_string .= $proxysettings{'UPSTREAM_PROXY'};
637 print FILE
"proxy = $proxy_string\n";
646 # Generate array based on the line content (seperator is a single or multiple space's)
647 my @settings = split(/,/, $line);
648 my ($provider, $hostname, $domain, $proxy, $wildcards, $username, $password, $enabled) = @settings;
650 # Skip entries if they are not (longer) supported.
651 next unless ($provider ~~ @providers);
653 # Skip disabled entries.
654 next unless ($enabled eq "on");
656 # Handle hostname details. Only connect the values with a dott if both are available.
657 if (($hostname) && ($domain)) {
658 print FILE
"[$hostname.$domain]\n";
660 print FILE
"[$hostname]\n";
663 print FILE
"provider = $provider\n";
667 # Handle token based auth for various providers.
668 if ($provider ~~ ["dns.lightningwirelabs.com", "entrydns.net", "regfish.com",
669 "spdns.de", "zzzz.io"] && $username eq "token") {
672 # Handle token auth for freedns.afraid.org and regfish.com.
673 } elsif ($provider ~~ ["freedns.afraid.org", "regfish.com"] && $password eq "") {
675 $password = $username;
677 # Handle keys for nsupdate
678 } elsif (($provider eq "nsupdate") && $username && $password) {
679 print FILE
"key = $username\n";
680 print FILE
"secret = $password\n";
685 # Handle keys for nsupdate.info
686 } elsif (($provider eq "nsupdate.info") && $password) {
687 print FILE
"secret = $password\n";
693 # Write auth details.
695 print FILE
"token = $password\n";
696 } elsif ($username && $password) {
697 print FILE
"username = $username\n";
698 print FILE
"password = $password\n";
708 # Function which generates an array (@providers) which contains the supported providers.
710 # Get supported providers.
711 open(PROVIDERS
, "/usr/bin/ddns list-providers |");
713 # Create new array to store the providers.
716 while (<PROVIDERS
>) {
719 # Remove following newlines.
722 # Add provider to the array.
723 push(@providers, $provider);