]>
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')) {
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 adding new accounts.
201 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
203 # Open /var/ipfire/ddns/config for writing.
204 open(FILE
, ">>$datafile") or die "Unable to open $datafile.";
206 # Lock file for writing.
209 # Add account data to the file.
210 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
212 # Close file after writing.
215 # Write out notice to logfile.
216 &General
::log($Lang::tr
{'ddns hostname added'});
218 # Update ddns config file.
220 # Handle account edditing.
221 } elsif ($settings{'ACTION'} eq $Lang::tr
{'update'}) {
223 # Open /var/ipfire/ddns/config for writing.
224 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
226 # Lock file for writing.
231 # Read file line by line.
232 foreach my $line (@current) {
234 if ($settings{'ID'} eq $id) {
235 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
244 # Close file after writing.
247 # Write out notice to logfile.
248 &General
::log($Lang::tr
{'ddns hostname modified'});
251 # Unset given CGI params.
254 # Update ddns config file.
255 &GenerateDDNSConfigFile
();
260 # Remove existing accounts.
262 if ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
264 # Open /var/ipfire/ddns/config for writing.
265 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
267 # Lock file for writing.
272 # Read file line by line.
273 foreach my $line (@current) {
275 # Write back every line, except the one we want to drop
276 # (identified by the ID)
277 unless ($settings{'ID'} eq $id) {
285 # Close file after writing.
288 # Unset given CGI params.
291 # Write out notice to logfile.
292 &General
::log($Lang::tr
{'ddns hostname removed'});
294 # Update ddns config file.
295 &GenerateDDNSConfigFile
();
299 # Read items for editing.
301 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
306 # Read file line by line.
307 foreach my $line (@current) {
309 if ($settings{'ID'} eq $id) {
314 # Splitt lines (splitting element is a single ",") and save values into temp array.
315 @temp = split(/\,/,$line);
317 # Handle hostname details. Only connect the values with a dott if both are available.
320 if (($temp[1]) && ($temp[2])) {
321 $hostname = "$temp[1].$temp[2]";
323 $hostname = "$temp[1]";
326 $settings{'SERVICE'} = $temp[0];
327 $settings{'HOSTNAME'} = $hostname;
328 $settings{'PROXY'} = $temp[3];
329 $settings{'WILDCARDS'} = $temp[4];
330 $settings{'LOGIN'} = $temp[5];
331 $settings{'PASSWORD'} = $temp[6];
332 $settings{'ENABLED'} = $temp[7];
341 # Handle forced updates.
343 if ($settings{'ACTION'} eq $Lang::tr
{'instant update'}) {
344 system(@ddnsprog) == 0 or die "@ddnsprog failed: $?\n";
348 # Set default values.
350 if (! $settings{'ACTION'}) {
351 $settings{'SERVICE'} = 'dyndns.org';
352 $settings{'ENABLED'} = 'on';
355 &Header
::openpage
($Lang::tr
{'dynamic dns'}, 1, '');
356 &Header
::openbigbox
('100%', 'left', '', $errormessage);
358 # Read file for general ddns settings.
359 &General
::readhash
($settingsfile, \
%settings);
362 $checked{'BEHINDROUTER'}{'RED_IP'} = '';
363 $checked{'BEHINDROUTER'}{'FETCH_IP'} = '';
364 $checked{'BEHINDROUTER'}{$settings{'BEHINDROUTER'}} = "checked='checked'";
366 $checked{'ENABLED'}{'on'} = ($settings{'ENABLED'} eq '' ) ?
'' : "checked='checked'";
368 # Show box for errormessages..
370 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
371 print "<font class='base'>$errormessage </font>";
375 &Header
::openbox
('100%', 'left', $Lang::tr
{'settings'});
378 # Section for general ddns setup.
380 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
383 <td class='base'>$Lang::tr{'dyn dns source choice'}</td>
386 <td class='base'><input type='radio' name='BEHINDROUTER' value='RED_IP' $checked{'BEHINDROUTER'}{'RED_IP'} />
387 $Lang::tr{'use ipfire red ip'}</td>
390 <td class='base'><input type='radio' name='BEHINDROUTER' value='FETCH_IP' $checked{'BEHINDROUTER'}{'FETCH_IP'} />
391 $Lang::tr{'fetch ip from'}</td>
399 <td align='right' valign='top' class='base'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
409 # Section to add or edit an existing entry.
412 my $buttontext = $Lang::tr
{'add'};
414 # Change buttontext and headline if we edit an account.
415 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
417 # Rename button and print headline for updating.
418 $buttontext = $Lang::tr
{'update'};
419 &Header
::openbox
('100%', 'left', $Lang::tr
{'edit an existing host'});
422 # Otherwise use default button text and show headline for adding a new account.
423 &Header
::openbox
('100%', 'left', $Lang::tr
{'add a host'});
428 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
429 <input type='hidden' name='ID' value='$settings{'ID'}' />
432 <td width='25%' class='base'>$Lang::tr{'service'}:</td>
436 # Generate dropdown menu for service selection.
437 print"<select size='1' name='SERVICE'>\n";
441 # Loop to print the providerlist.
442 foreach my $provider (@providers) {
444 # Check if the current provider needs to be selected.
445 if ($provider eq $settings{'SERVICE'}) {
446 $selected = 'selected';
451 # Print out the HTML option field.
452 print "<option value=\"$provider\" $selected>$provider</option>\n";
455 print"</select></td>\n";
457 <td width='20%' class='base'>$Lang::tr{'hostname'}:</td>
458 <td width='30%'><input type='text' name='HOSTNAME' value='$settings{'HOSTNAME'}' /></td>
462 <td class='base'>$Lang::tr{'enabled'}</td>
463 <td><input type='checkbox' name='ENABLED' value='on' $checked{'ENABLED'}{'on'} /></td>
464 <td class='base'>$Lang::tr{'username'}</td>
465 <td><input type='text' name='LOGIN' value='$settings{'LOGIN'}' /></td>
469 <td class='base'></td>
471 <td class='base'>$Lang::tr{'password'}</td>
472 <td><input type='password' name='PASSWORD' value='$settings{'PASSWORD'}' /></td>
480 <td width='30%' align='right' class='base'>
481 <input type='hidden' name='ACTION' value='$buttontext'>
482 <input type='submit' name='SUBMIT' value='$buttontext'></td>
491 # Third section, display all created ddns hosts.
492 # Re-open file to get changes.
493 open(FILE
, $datafile) or die "Unable to open $datafile.";
497 # Get IP address of the red interface.
498 my $ip = &General
::GetDyndnsRedIP
();
503 &Header
::openbox
('100%', 'left', $Lang::tr
{'current hosts'});
506 <table width='100%' class='tbl'>
508 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'service'}</b></th>
509 <th width='50%' align='center' class='boldbase'><b>$Lang::tr{'hostname'}</b></th>
510 <th width='20%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
514 foreach my $line (@current) {
517 my @temp = split(/\,/,$line);
519 # Generate value for enable/disable checkbox.
520 my $sync = "<font color='blue'>";
524 if ($temp[7] eq "on") {
526 $gdesc = $Lang::tr
{'click to disable'};
527 $sync = (&General
::DyndnsServiceSync
($ip,$temp[1], $temp[2]) ?
"<font color='green'>": "<font color='red'>") ;
528 $toggle_enabled = 'off';
531 $gdesc = $Lang::tr
{'click to enable'};
532 $toggle_enabled = 'on';
538 if ($settings{'ID'} eq $id) {
539 $col="bgcolor='${Header::colouryellow}'";
540 } elsif (!($temp[0] ~~ @providers)) {
541 $col="bgcolor='#FF4D4D'";
543 $col="bgcolor='$color{'color20'}'";
545 $col="bgcolor='$color{'color22'}'";
548 # Handle hostname details. Only connect the values with a dott if both are available.
551 if (($temp[1]) && ($temp[2])) {
552 $hostname="$temp[1].$temp[2]";
554 $hostname="$temp[1]";
557 # The following HTML Code still is part of the loop.
560 <td align='center' $col><a href='http://$temp[0]'>$temp[0]</a></td>
561 <td align='center' $col>$sync$hostname</td>
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='ENABLED' value='$toggle_enabled'>
566 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
567 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
570 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
571 <input type='hidden' name='ID' value='$id'>
572 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
573 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
576 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
577 <input type='hidden' name='ID' value='$id'>
578 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
579 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
590 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
591 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
592 <td class='base'>$Lang::tr{'click to disable'}</td>
593 <td> </td>
594 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
595 <td class='base'>$Lang::tr{'click to enable'}</td>
596 <td> </td>
597 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
598 <td class='base'>$Lang::tr{'edit'}</td>
599 <td> </td>
600 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
601 <td class='base'>$Lang::tr{'remove'}</td>
602 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
603 <td align='right' width='30%'><input type='submit' name='ACTION' value='$Lang::tr{'instant update'}' /></td>
612 &Header
::closebigbox
();
613 &Header
::closepage
();
615 # Function to generate the required configuration file for the DDNS tool.
616 sub GenerateDDNSConfigFile
{
618 open(SETTINGS
, "<$datafile") or die "Could not open $datafile.";
620 open(FILE
, ">${General::swroot}/ddns/ddns.conf");
622 # Global configuration options.
623 print FILE
"[config]\n";
625 # Check if we guess our IP address by an extranal server.
626 if ($settings{'BEHINDROUTER'} eq "FETCH_IP") {
627 print FILE
"guess_external_ip = true\n";
629 print FILE
"guess_external_ip = false\n";
632 # Use an upstream proxy and generate proxy url.
634 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
635 if ($proxysettings{'UPSTREAM_PROXY'}) {
636 my $proxy_string = "http://";
638 if ($proxysettings{'UPSTREAM_USER'} && $proxysettings{'UPSTREAM_PASSWORD'}) {
639 $proxy_string .= "$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@";
642 $proxy_string .= $proxysettings{'UPSTREAM_PROXY'};
644 print FILE
"proxy = $proxy_string\n";
652 # Generate array based on the line content (seperator is a single or multiple space's)
653 my @settings = split(/,/, $line);
654 my ($provider, $hostname, $domain, $proxy, $wildcards, $username, $password, $enabled) = @settings;
656 # Skip entries if they are not (longer) supported.
657 next unless ($provider ~~ @providers);
659 # Skip disabled entries.
660 next if ($enabled eq "off");
662 # Handle hostname details. Only connect the values with a dott if both are available.
663 if (($hostname) && ($domain)) {
664 print FILE
"[$hostname.$domain]\n";
666 print FILE
"[$hostname]\n";
669 print FILE
"provider = $provider\n";
673 # Handle token based auth for various providers.
674 if ($provider ~~ ["dns.lightningwirelabs.com", "entrydns.net", "regfish.com"] && $username eq "token") {
677 # Handle token auth for freedns.afraid.org.
678 } elsif ($provider eq "freedns.afraid.org" && $password eq "") {
680 $password = $username;
682 # Handle keys for nsupdate
683 } elsif (($provider eq "nsupdate") && $username && $password) {
684 print FILE
"key = $username\n";
685 print FILE
"secret = $password\n";
690 # Handle keys for nsupdate.info
691 } elsif (($provider eq "nsupdate.info") && $password) {
692 print FILE
"secret = $password\n";
698 # Write auth details.
700 print FILE
"token = $password\n";
701 } elsif ($username && $password) {
702 print FILE
"username = $username\n";
703 print FILE
"password = $password\n";
706 # These providers need to be set to only use IPv4.
707 if ($provider ~~ ["freedns.afraid.org", "nsupdate.info", "opendns.com", "variomedia.de", "zoneedit.com"]) {
708 print FILE
"proto = ipv4\n";
718 # Function which generates an array (@providers) which contains the supported providers.
720 # Get supported providers.
721 open(PROVIDERS
, "/usr/bin/ddns list-providers |");
723 # Create new array to store the providers.
726 while (<PROVIDERS
>) {
729 # Remove following newlines.
732 # Add provider to the array.
733 push(@providers, $provider);