]>
git.ipfire.org Git - people/pmueller/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'}) {
93 # Open /var/ipfire/ddns/settings for writing.
94 open(FILE
, ">$settingsfile") or die "Unable to open $settingsfile.";
96 # Lock file for writing.
99 # Check if BEHINDROUTER has been configured.
100 if ($settings{'BEHINDROUTER'} ne '') {
101 print FILE
"BEHINDROUTER=$settings{'BEHINDROUTER'}\n";
104 # Close file after writing.
107 # Unset given CGI parmas.
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'}) {
119 # Open /var/ipfire/ddns/config for writing.
120 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
122 # Lock file for writing.
128 # Read file line by line.
129 foreach my $line (@current) {
134 if ($settings{'ID'} eq $id) {
136 # Splitt lines (splitting element is a single ",") and save values into temp array.
137 @temp = split(/\,/,$line);
139 # Check if we want to toggle ENABLED or WILDCARDS.
140 if ($settings{'ENABLED'} ne '') {
143 print FILE
"$temp[0],$temp[1],$temp[2],$temp[3],$temp[4],$temp[5],$temp[6],$settings{'ENABLED'}\n";
147 # Print unmodified line.
148 print FILE
"$line\n";
155 # Close file after writing.
158 # Unset given CGI params.
161 # Write out logging notice.
162 &General
::log($Lang::tr
{'ddns hostname modified'});
164 # Update ddns config file.
165 &GenerateDDNSConfigFile
();
169 # Add new accounts, or edit existing ones.
171 if (($settings{'ACTION'} eq $Lang::tr
{'add'}) || ($settings{'ACTION'} eq $Lang::tr
{'update'})) {
173 # Check if a hostname has been given.
174 if ($settings{'HOSTNAME'} eq '') {
175 $errormessage = $Lang::tr
{'hostname not set'};
178 # Check if a valid domainname has been provided.
179 if (!&General
::validdomainname
($settings{'HOSTNAME'})) {
180 $errormessage = $Lang::tr
{'invalid domain name'};
183 # Check if a username has been sent.
184 if ($settings{'LOGIN'} eq '') {
185 $errormessage = $Lang::tr
{'username not set'};
188 # Check if a password has been typed in.
189 # freedns.afraid.org does not require this field.
190 if (($settings{'PASSWORD'} eq '') && ($settings{'SERVICE'} ne 'freedns.afraid.org') && ($settings{'SERVICE'} ne 'regfish.com')) {
191 $errormessage = $Lang::tr
{'password not set'};
194 # Go furter if there was no error.
195 if ( ! $errormessage) {
197 # Splitt hostname field into 2 parts for storrage.
198 my($hostname, $domain) = split(/\./, $settings{'HOSTNAME'}, 2);
200 # Handle enabled checkbox. When the checkbox is selected a "on" will be returned,
201 # if the checkbox is not checked nothing is returned in this case we set the value to "off".
202 if ($settings{'ENABLED'} ne 'on') {
203 $settings{'ENABLED'} = 'off';
206 # Handle adding new accounts.
207 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
209 # Open /var/ipfire/ddns/config for writing.
210 open(FILE
, ">>$datafile") or die "Unable to open $datafile.";
212 # Lock file for writing.
215 # Add account data to the file.
216 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
218 # Close file after writing.
221 # Write out notice to logfile.
222 &General
::log($Lang::tr
{'ddns hostname added'});
224 # Handle account edditing.
225 } elsif ($settings{'ACTION'} eq $Lang::tr
{'update'}) {
227 # Open /var/ipfire/ddns/config for writing.
228 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
230 # Lock file for writing.
235 # Read file line by line.
236 foreach my $line (@current) {
238 if ($settings{'ID'} eq $id) {
239 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
248 # Close file after writing.
251 # Write out notice to logfile.
252 &General
::log($Lang::tr
{'ddns hostname modified'});
255 # Unset given CGI params.
258 # Update ddns config file.
259 &GenerateDDNSConfigFile
();
264 # Remove existing accounts.
266 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) {
279 # Write back every line, except the one we want to drop
280 # (identified by the ID)
281 unless ($settings{'ID'} eq $id) {
289 # Close file after writing.
292 # Unset given CGI params.
295 # Write out notice to logfile.
296 &General
::log($Lang::tr
{'ddns hostname removed'});
298 # Update ddns config file.
299 &GenerateDDNSConfigFile
();
303 # Read items for editing.
305 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
310 # Read file line by line.
311 foreach my $line (@current) {
313 if ($settings{'ID'} eq $id) {
318 # Splitt lines (splitting element is a single ",") and save values into temp array.
319 @temp = split(/\,/,$line);
321 $settings{'SERVICE'} = $temp[0];
322 $settings{'HOSTNAME'} = "$temp[1].$temp[2]";
323 $settings{'PROXY'} = $temp[3];
324 $settings{'WILDCARDS'} = $temp[4];
325 $settings{'LOGIN'} = $temp[5];
326 $settings{'PASSWORD'} = $temp[6];
327 $settings{'ENABLED'} = $temp[7];
336 # Handle forced updates.
338 if ($settings{'ACTION'} eq $Lang::tr
{'instant update'}) {
339 system(@ddnsprog) == 0 or die "@ddnsprog failed: $?\n";
343 # Set default values.
345 if (! $settings{'ACTION'}) {
346 $settings{'SERVICE'} = 'dyndns.org';
347 $settings{'ENABLED'} = 'on';
350 &Header
::openpage
($Lang::tr
{'dynamic dns'}, 1, '');
351 &Header
::openbigbox
('100%', 'left', '', $errormessage);
353 # Read file for general ddns settings.
354 &General
::readhash
($settingsfile, \
%settings);
357 $checked{'BEHINDROUTER'}{'RED_IP'} = '';
358 $checked{'BEHINDROUTER'}{'FETCH_IP'} = '';
359 $checked{'BEHINDROUTER'}{$settings{'BEHINDROUTER'}} = "checked='checked'";
361 $checked{'ENABLED'}{'on'} = '';
362 $checked{'ENABLED'}{'off'} = '';
363 $checked{'ENABLED'}{$settings{'ENABLED'}} = "checked='checked'";
365 # Show box for errormessages..
367 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
368 print "<font class='base'>$errormessage </font>";
372 &Header
::openbox
('100%', 'left', $Lang::tr
{'settings'});
375 # Section for general ddns setup.
377 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
380 <td class='base'>$Lang::tr{'dyn dns source choice'}</td>
383 <td class='base'><input type='radio' name='BEHINDROUTER' value='RED_IP' $checked{'BEHINDROUTER'}{'RED_IP'} />
384 $Lang::tr{'use ipfire red ip'}</td>
387 <td class='base'><input type='radio' name='BEHINDROUTER' value='FETCH_IP' $checked{'BEHINDROUTER'}{'FETCH_IP'} />
388 $Lang::tr{'fetch ip from'}</td>
396 <td align='right' valign='top' class='base'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
406 # Section to add or edit an existing entry.
409 my $buttontext = $Lang::tr
{'add'};
411 # Change buttontext and headline if we edit an account.
412 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
414 # Rename button and print headline for updating.
415 $buttontext = $Lang::tr
{'update'};
416 &Header
::openbox
('100%', 'left', $Lang::tr
{'edit an existing host'});
419 # Otherwise use default button text and show headline for adding a new account.
420 &Header
::openbox
('100%', 'left', $Lang::tr
{'add a host'});
425 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
426 <input type='hidden' name='ID' value='$settings{'ID'}' />
429 <td width='25%' class='base'>$Lang::tr{'service'}:</td>
433 # Generate dropdown menu for service selection.
434 print"<select size='1' name='SERVICE'>\n";
438 # Loop to print the providerlist.
439 foreach my $provider (@providers) {
441 # Check if the current provider needs to be selected.
442 if ($provider eq $settings{'SERVICE'}) {
443 $selected = 'selected';
448 # Print out the HTML option field.
449 print "<option value=\"$provider\" $selected>$provider</option>\n";
452 print"</select></td>\n";
454 <td width='20%' class='base'>$Lang::tr{'hostname'}:</td>
455 <td width='30%'><input type='text' name='HOSTNAME' value='$settings{'HOSTNAME'}' /></td>
459 <td class='base'>$Lang::tr{'enabled'}</td>
460 <td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
461 <td class='base'>$Lang::tr{'username'}</td>
462 <td><input type='text' name='LOGIN' value='$settings{'LOGIN'}' /></td>
466 <td class='base'></td>
468 <td class='base'>$Lang::tr{'password'}</td>
469 <td><input type='password' name='PASSWORD' value='$settings{'PASSWORD'}' /></td>
477 <td width='30%' align='right' class='base'>
478 <input type='hidden' name='ACTION' value='$buttontext'>
479 <input type='submit' name='SUBMIT' value='$buttontext'></td>
488 # Third section, display all created ddns hosts.
489 # Re-open file to get changes.
490 open(FILE
, $datafile) or die "Unable to open $datafile.";
494 # Get IP address of the red interface.
495 my $ip = &General
::GetDyndnsRedIP
();
500 &Header
::openbox
('100%', 'left', $Lang::tr
{'current hosts'});
503 <table width='100%' class='tbl'>
505 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'service'}</b></th>
506 <th width='50%' align='center' class='boldbase'><b>$Lang::tr{'hostname'}</b></th>
507 <th width='20%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
511 foreach my $line (@current) {
514 my @temp = split(/\,/,$line);
516 # Handle hostname details. Only connect the values with a dott if both are available.
519 if (($temp[1]) && ($temp[2])) {
520 $hostname="$temp[1].$temp[2]";
522 $hostname="$temp[1]";
525 # Generate value for enable/disable checkbox.
530 if ($temp[7] eq "on") {
532 $gdesc = $Lang::tr
{'click to disable'};
534 # Check if the given hostname is a FQDN before doing a nslookup.
535 if (&General
::validfqdn
($hostname)) {
536 $sync = (&General
::DyndnsServiceSync
($ip,$temp[1], $temp[2]) ?
"<font color='green'>": "<font color='red'>") ;
539 $toggle_enabled = 'off';
541 $sync = "<font color='blue'>";
543 $gdesc = $Lang::tr
{'click to enable'};
544 $toggle_enabled = 'on';
550 if ($settings{'ID'} eq $id) {
551 $col="bgcolor='${Header::colouryellow}'";
552 } elsif (!($temp[0] ~~ @providers)) {
553 $col="bgcolor='#FF4D4D'";
555 $col="bgcolor='$color{'color20'}'";
557 $col="bgcolor='$color{'color22'}'";
560 # The following HTML Code still is part of the loop.
563 <td align='center' $col><a href='http://$temp[0]'>$temp[0]</a></td>
564 <td align='center' $col>$sync$temp[1].$sync$temp[2]</td>
566 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
567 <input type='hidden' name='ID' value='$id'>
568 <input type='hidden' name='ENABLED' value='$toggle_enabled'>
569 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
570 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
573 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
574 <input type='hidden' name='ID' value='$id'>
575 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
576 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
579 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
580 <input type='hidden' name='ID' value='$id'>
581 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
582 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
593 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
594 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
595 <td class='base'>$Lang::tr{'click to disable'}</td>
596 <td> </td>
597 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
598 <td class='base'>$Lang::tr{'click to enable'}</td>
599 <td> </td>
600 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
601 <td class='base'>$Lang::tr{'edit'}</td>
602 <td> </td>
603 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
604 <td class='base'>$Lang::tr{'remove'}</td>
605 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
606 <td align='right' width='30%'><input type='submit' name='ACTION' value='$Lang::tr{'instant update'}' /></td>
615 &Header
::closebigbox
();
616 &Header
::closepage
();
618 # Function to generate the required configuration file for the DDNS tool.
619 sub GenerateDDNSConfigFile
{
621 open(SETTINGS
, "<$datafile") or die "Could not open $datafile.";
623 open(FILE
, ">${General::swroot}/ddns/ddns.conf");
625 # Global configuration options.
626 print FILE
"[config]\n";
628 # Check if we guess our IP address by an extranal server.
629 if ($settings{'BEHINDROUTER'} eq "FETCH_IP") {
630 print FILE
"guess_external_ip = true\n";
632 print FILE
"guess_external_ip = false\n";
635 # Use an upstream proxy and generate proxy url.
637 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
638 if ($proxysettings{'UPSTREAM_PROXY'}) {
639 my $proxy_string = "http://";
641 if ($proxysettings{'UPSTREAM_USER'} && $proxysettings{'UPSTREAM_PASSWORD'}) {
642 $proxy_string .= "$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@";
645 $proxy_string .= $proxysettings{'UPSTREAM_PROXY'};
647 print FILE
"proxy = $proxy_string\n";
656 # Generate array based on the line content (seperator is a single or multiple space's)
657 my @settings = split(/,/, $line);
658 my ($provider, $hostname, $domain, $proxy, $wildcards, $username, $password, $enabled) = @settings;
660 # Skip entries if they are not (longer) supported.
661 next unless ($provider ~~ @providers);
663 # Skip disabled entries.
664 next unless ($enabled eq "on");
666 print FILE
"[$hostname.$domain]\n";
667 print FILE
"provider = $provider\n";
671 # Handle token based auth for various providers.
672 if ($provider ~~ ["dns.lightningwirelabs.com", "entrydns.net", "regfish.com"] && $username eq "token") {
675 # Handle token auth for freedns.afraid.org and regfish.com.
676 } elsif ($provider ~~ ["freedns.afraid.org", "regfish.com"] && $password eq "") {
678 $password = $username;
680 # Handle keys for nsupdate
681 } elsif (($provider eq "nsupdate") && $username && $password) {
682 print FILE
"key = $username\n";
683 print FILE
"secret = $password\n";
688 # Handle keys for nsupdate.info
689 } elsif (($provider eq "nsupdate.info") && $password) {
690 print FILE
"secret = $password\n";
696 # Write auth details.
698 print FILE
"token = $password\n";
699 } elsif ($username && $password) {
700 print FILE
"username = $username\n";
701 print FILE
"password = $password\n";
704 # These providers need to be set to only use IPv4.
705 if ($provider ~~ ["freedns.afraid.org", "nsupdate.info", "opendns.com", "variomedia.de", "zoneedit.com"]) {
706 print FILE
"proto = ipv4\n";
716 # Function which generates an array (@providers) which contains the supported providers.
718 # Get supported providers.
719 open(PROVIDERS
, "/usr/bin/ddns list-providers |");
721 # Create new array to store the providers.
724 while (<PROVIDERS
>) {
727 # Remove following newlines.
730 # Add provider to the array.
731 push(@providers, $provider);