]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ddns.cgi
b3f6e3699cde135a026861c1db863e7588968442
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",
50 "update-all", "--force" );
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 &Header
::showhttpheaders
();
72 &Header
::getcgihash
(\
%settings);
74 # Read configuration file.
75 open(FILE
, "$datafile") or die "Unable to open $datafile.";
79 # Get supported ddns providers.
80 my @providers = &GetProviders
();
83 # Save General Settings.
85 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
87 # Open /var/ipfire/ddns/settings for writing.
88 open(FILE
, ">$settingsfile") or die "Unable to open $settingsfile.";
90 # Lock file for writing.
93 # Check if BEHINDROUTER has been configured.
94 if ($settings{'BEHINDROUTER'} ne '') {
95 print FILE
"BEHINDROUTER=$settings{'BEHINDROUTER'}\n";
98 # Close file after writing.
101 # Unset given CGI parmas.
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'}) {
113 # Open /var/ipfire/ddns/config for writing.
114 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
116 # Lock file for writing.
122 # Read file line by line.
123 foreach my $line (@current) {
128 if ($settings{'ID'} eq $id) {
130 # Splitt lines (splitting element is a single ",") and save values into temp array.
131 @temp = split(/\,/,$line);
133 # Check if we want to toggle ENABLED or WILDCARDS.
134 if ($settings{'ENABLED'} ne '') {
137 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";
149 # Close file after writing.
152 # Unset given CGI params.
155 # Write out logging notice.
156 &General
::log($Lang::tr
{'ddns hostname modified'});
158 # Update ddns config file.
159 &GenerateDDNSConfigFile
();
163 # Add new accounts, or edit existing ones.
165 if (($settings{'ACTION'} eq $Lang::tr
{'add'}) || ($settings{'ACTION'} eq $Lang::tr
{'update'})) {
167 # Check if a hostname has been given.
168 if ($settings{'HOSTNAME'} eq '') {
169 $errormessage = $Lang::tr
{'hostname not set'};
172 # Check if a valid domainname has been provided.
173 if (!&General
::validdomainname
($settings{'HOSTNAME'})) {
174 $errormessage = $Lang::tr
{'invalid domain name'};
177 # Check if a username has been sent.
178 if ($settings{'LOGIN'} eq '') {
179 $errormessage = $Lang::tr
{'username not set'};
182 # Check if a password has been typed in.
183 # freedns.afraid.org does not require this field.
184 if (($settings{'PASSWORD'} eq '') && ($settings{'SERVICE'} ne 'freedns.afraid.org')) {
185 $errormessage = $Lang::tr
{'password not set'};
188 # Go furter if there was no error.
189 if ( ! $errormessage) {
191 # Splitt hostname field into 2 parts for storrage.
192 my($hostname, $domain) = split(/\./, $settings{'HOSTNAME'}, 2);
194 # Handle adding new accounts.
195 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
197 # Open /var/ipfire/ddns/config for writing.
198 open(FILE
, ">>$datafile") or die "Unable to open $datafile.";
200 # Lock file for writing.
203 # Add account data to the file.
204 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
206 # Close file after writing.
209 # Write out notice to logfile.
210 &General
::log($Lang::tr
{'ddns hostname added'});
212 # Update ddns config file.
214 # Handle account edditing.
215 } elsif ($settings{'ACTION'} eq $Lang::tr
{'update'}) {
217 # Open /var/ipfire/ddns/config for writing.
218 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
220 # Lock file for writing.
225 # Read file line by line.
226 foreach my $line (@current) {
228 if ($settings{'ID'} eq $id) {
229 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
238 # Close file after writing.
241 # Write out notice to logfile.
242 &General
::log($Lang::tr
{'ddns hostname modified'});
245 # Unset given CGI params.
248 # Update ddns config file.
249 &GenerateDDNSConfigFile
();
254 # Remove existing accounts.
256 if ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
258 # Open /var/ipfire/ddns/config for writing.
259 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
261 # Lock file for writing.
266 # Read file line by line.
267 foreach my $line (@current) {
269 # Write back every line, except the one we want to drop
270 # (identified by the ID)
271 unless ($settings{'ID'} eq $id) {
279 # Close file after writing.
282 # Unset given CGI params.
285 # Write out notice to logfile.
286 &General
::log($Lang::tr
{'ddns hostname removed'});
288 # Update ddns config file.
289 &GenerateDDNSConfigFile
();
293 # Read items for editing.
295 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
300 # Read file line by line.
301 foreach my $line (@current) {
303 if ($settings{'ID'} eq $id) {
308 # Splitt lines (splitting element is a single ",") and save values into temp array.
309 @temp = split(/\,/,$line);
311 $settings{'SERVICE'} = $temp[0];
312 $settings{'HOSTNAME'} = "$temp[1].$temp[2]";
313 $settings{'PROXY'} = $temp[3];
314 $settings{'WILDCARDS'} = $temp[4];
315 $settings{'LOGIN'} = $temp[5];
316 $settings{'PASSWORD'} = $temp[6];
317 $settings{'ENABLED'} = $temp[7];
326 # Handle forced updates.
328 if ($settings{'ACTION'} eq $Lang::tr
{'instant update'}) {
329 system(@ddnsprog) == 0 or die "@ddnsprog failed: $?\n";
333 # Set default values.
335 if (! $settings{'ACTION'}) {
336 $settings{'SERVICE'} = 'dyndns.org';
337 $settings{'ENABLED'} = 'on';
340 &Header
::openpage
($Lang::tr
{'dynamic dns'}, 1, '');
341 &Header
::openbigbox
('100%', 'left', '', $errormessage);
343 # Read file for general ddns settings.
344 &General
::readhash
($settingsfile, \
%settings);
347 $checked{'BEHINDROUTER'}{'RED_IP'} = '';
348 $checked{'BEHINDROUTER'}{'FETCH_IP'} = '';
349 $checked{'BEHINDROUTER'}{$settings{'BEHINDROUTER'}} = "checked='checked'";
351 $checked{'ENABLED'}{'on'} = ($settings{'ENABLED'} eq '' ) ?
'' : "checked='checked'";
353 # Show box for errormessages..
355 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
356 print "<font class='base'>$errormessage </font>";
360 &Header
::openbox
('100%', 'left', $Lang::tr
{'settings'});
363 # Section for general ddns setup.
365 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
368 <td class='base'>$Lang::tr{'dyn dns source choice'}</td>
371 <td class='base'><input type='radio' name='BEHINDROUTER' value='RED_IP' $checked{'BEHINDROUTER'}{'RED_IP'} />
372 $Lang::tr{'use ipfire red ip'}</td>
375 <td class='base'><input type='radio' name='BEHINDROUTER' value='FETCH_IP' $checked{'BEHINDROUTER'}{'FETCH_IP'} />
376 $Lang::tr{'fetch ip from'}</td>
384 <td align='right' valign='top' class='base'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
394 # Section to add or edit an existing entry.
397 my $buttontext = $Lang::tr
{'add'};
399 # Change buttontext and headline if we edit an account.
400 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
402 # Rename button and print headline for updating.
403 $buttontext = $Lang::tr
{'update'};
404 &Header
::openbox
('100%', 'left', $Lang::tr
{'edit an existing host'});
407 # Otherwise use default button text and show headline for adding a new account.
408 &Header
::openbox
('100%', 'left', $Lang::tr
{'add a host'});
413 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
414 <input type='hidden' name='ID' value='$settings{'ID'}' />
417 <td width='25%' class='base'>$Lang::tr{'service'}:</td>
421 # Generate dropdown menu for service selection.
422 print"<select size='1' name='SERVICE'>\n";
426 # Loop to print the providerlist.
427 foreach my $provider (@providers) {
429 # Check if the current provider needs to be selected.
430 if ($provider eq $settings{'SERVICE'}) {
431 $selected = 'selected';
436 # Print out the HTML option field.
437 print "<option value=\"$provider\" $selected>$provider</option>\n";
440 print"</select></td>\n";
442 <td width='20%' class='base'>$Lang::tr{'hostname'}:</td>
443 <td width='30%'><input type='text' name='HOSTNAME' value='$settings{'HOSTNAME'}' /></td>
447 <td class='base'></td>
449 <td class='base'>$Lang::tr{'username'}:</td>
450 <td><input type='text' name='LOGIN' value='$settings{'LOGIN'}' /></td>
454 <td class='base'></td>
456 <td class='base'>$Lang::tr{'password'}</td>
457 <td><input type='password' name='PASSWORD' value='$settings{'PASSWORD'}' /></td>
461 <td class='base'>$Lang::tr{'enabled'}</td>
462 <td><input type='checkbox' name='ENABLED' value='on' $checked{'ENABLED'}{'on'} /></td>
463 <td class='base'></td>
472 <td width='30%' align='right' class='base'>
473 <input type='hidden' name='ACTION' value='$buttontext'>
474 <input type='submit' name='SUBMIT' value='$buttontext'></td>
483 # Third section, display all created ddns hosts.
485 &Header
::openbox
('100%', 'left', $Lang::tr
{'current hosts'});
487 <table width='100%' class='tbl'>
489 <th width='20%' align='center' class='boldbase'><b>$Lang::tr{'service'}</b></th>
490 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'hostname'}</b></th>
491 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'domain'}</b></th>
492 <th width='20%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
497 # Re-open file to get changes.
498 open(FILE
, $datafile) or die "Unable to open $datafile.";
502 # Get IP address of the red interface.
503 my $ip = &General
::GetDyndnsRedIP
;
507 foreach my $line (@current) {
511 my @temp = split(/\,/,$line);
513 # Generate value for enable/disable checkbox.
514 my $sync = "<font color='blue'>";
518 if ($temp[7] eq "on") {
520 $gdesc = $Lang::tr
{'click to disable'};
521 $sync = (&General
::DyndnsServiceSync
($ip,$temp[1], $temp[2]) ?
"<font color='green'>": "<font color='red'>") ;
522 $toggle_enabled = 'off';
525 $gdesc = $Lang::tr
{'click to enable'};
526 $toggle_enabled = 'on';
532 if ($settings{'ID'} eq $id) {
533 $col="bgcolor='${Header::colouryellow}'";
534 } elsif (!&General
::is_part_of
("$temp[0]", @providers)) {
535 $col="bgcolor='#FF4D4D'";
537 $col="bgcolor='$color{'color20'}'";
539 $col="bgcolor='$color{'color22'}'";
542 # The following HTML Code still is part of the loop.
545 <td align='center' $col><a href='http://$temp[0]'>$temp[0]</a></td>
546 <td align='center' $col>$sync$temp[1]</td>
547 <td align='center' $col>$sync$temp[2]</td>
549 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
550 <input type='hidden' name='ID' value='$id'>
551 <input type='hidden' name='ENABLED' value='$toggle_enabled'>
552 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
553 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
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='ACTION' value='$Lang::tr{'edit'}' />
559 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
562 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
563 <input type='hidden' name='ID' value='$id'>
564 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
565 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
574 # If table contains entries, print 'Key to action icons'
579 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
580 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
581 <td class='base'>$Lang::tr{'click to disable'}</td>
582 <td> </td>
583 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
584 <td class='base'>$Lang::tr{'click to enable'}</td>
585 <td> </td>
586 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
587 <td class='base'>$Lang::tr{'edit'}</td>
588 <td> </td>
589 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
590 <td class='base'>$Lang::tr{'remove'}</td>
591 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
592 <td align='right' width='30%'><input type='submit' name='ACTION' value='$Lang::tr{'instant update'}' /></td>
601 &Header
::closebigbox
();
602 &Header
::closepage
();
604 # Function to generate the required configuration file for the DDNS tool.
605 sub GenerateDDNSConfigFile
{
607 open(SETTINGS
, "<$datafile") or die "Could not open $datafile.";
609 open(FILE
, ">${General::swroot}/ddns/ddns.conf");
611 # Global configuration options.
612 print FILE
"[config]\n";
614 # Check if we guess our IP address by an extranal server.
615 if ($settings{'BEHINDROUTER'} eq "FETCH_IP") {
616 print FILE
"guess_external_ip = true\n";
618 print FILE
"guess_external_ip = false\n";
621 # Use an upstream proxy and generate proxy url.
623 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
624 if ($proxysettings{'UPSTREAM_PROXY'}) {
625 my $proxy_string = "http://";
627 if ($proxysettings{'UPSTREAM_USER'} && $proxysettings{'UPSTREAM_PASSWORD'}) {
628 $proxy_string .= "$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@";
631 $proxy_string .= $proxysettings{'UPSTREAM_PROXY'};
633 print FILE
"proxy = $proxy_string\n";
641 # Generate array based on the line content (seperator is a single or multiple space's)
642 my @settings = split(/,/, $line);
643 my ($provider, $hostname, $domain, $proxy, $wildcards, $username, $password, $enabled) = @settings;
645 # Skip entries if they are not (longer) supported.
646 next if (!&General
::is_part_of
("$provider", @providers));
648 # Skip disabled entries.
649 next if ($enabled eq "off");
651 print FILE
"[$hostname.$domain]\n";
652 print FILE
"provider = $provider\n";
656 # Handle token based auth for various providers.
657 if ($provider ~~ ["dns.lightningwirelabs.com", "regfish.com"] && $username eq "token") {
660 # Handle token auth for freedns.afraid.org.
661 } elsif ($provider eq "freedns.afraid.org" && $password eq "") {
663 $password = $username;
666 # Write auth details.
668 print FILE
"token = $password\n";
670 print FILE
"username = $username\n";
671 print FILE
"password = $password\n";
674 # These providers need to be set to only use IPv4.
675 if ($provider ~~ ["freedns.afraid.org", "variomedia.de", "zoneedit.com"]) {
676 print FILE
"proto = ipv4\n";
686 # Function which generates an array (@providers) which contains the supported providers.
688 # Get supported providers.
689 open(PROVIDERS
, "/usr/bin/ddns list-providers |");
691 # Create new array to store the providers.
694 while (<PROVIDERS
>) {
697 # Remove following newlines.
700 # Add provider to the array.
701 push(@providers, $provider);