]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ddns.cgi
43ee95275a7f2f92f07f036269b117365defb237
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{'WILDCARDS'} = '';
66 $settings{'SERVICE'} = '';
68 $settings{'ACTION'} = '';
70 &Header
::showhttpheaders
();
73 &Header
::getcgihash
(\
%settings);
75 # Read configuration file.
76 open(FILE
, "$datafile") or die "Unable to open $datafile.";
80 # Get supported ddns providers.
81 my @providers = &GetProviders
();
84 # Save General Settings.
86 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
88 # Open /var/ipfire/ddns/settings for writing.
89 open(FILE
, ">$settingsfile") or die "Unable to open $settingsfile.";
91 # Lock file for writing.
94 # Check if BEHINDROUTER has been configured.
95 if ($settings{'BEHINDROUTER'} ne '') {
96 print FILE
"BEHINDROUTER=$settings{'BEHINDROUTER'}\n";
99 # Close file after writing.
102 # Unset given CGI parmas.
105 # Update ddns config file.
106 &GenerateDDNSConfigFile
();
110 # Toggle enable/disable field. Field is in second position
112 if ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
114 # Open /var/ipfire/ddns/config for writing.
115 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
117 # Lock file for writing.
123 # Read file line by line.
124 foreach my $line (@current) {
129 if ($settings{'ID'} eq $id) {
131 # Splitt lines (splitting element is a single ",") and save values into temp array.
132 @temp = split(/\,/,$line);
134 # Check if we want to toggle ENABLED or WILDCARDS.
135 if ($settings{'ENABLED'} ne '') {
138 print FILE
"$temp[0],$temp[1],$temp[2],$temp[3],$temp[4],$temp[5],$temp[6],$settings{'ENABLED'}\n";
140 } elsif ($settings{'WILDCARDS'} ne '') {
143 print FILE
"$temp[0],$temp[1],$temp[2],$temp[3],$settings{'WILDCARDS'},$temp[5],$temp[6],$temp[7]\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 # XXX TODO: Check for provider which supports wildcards.
175 # Check if a hostname has been given.
176 if ($settings{'HOSTNAME'} eq '') {
177 $errormessage = $Lang::tr
{'hostname not set'};
180 # Check if a valid domainname has been provided.
181 if (!&General
::validdomainname
($settings{'HOSTNAME'})) {
182 $errormessage = $Lang::tr
{'invalid domain name'};
185 # Check if a username has been sent.
186 if ($settings{'LOGIN'} eq '') {
187 $errormessage = $Lang::tr
{'username not set'};
190 # Check if a password has been typed in.
191 # freedns.afraid.org does not require this field.
192 if (($settings{'PASSWORD'} eq '') && ($settings{'SERVICE'} ne 'freedns.afraid.org')) {
193 $errormessage = $Lang::tr
{'password not set'};
196 # Go furter if there was no error.
197 if ( ! $errormessage) {
199 # Splitt hostname field into 2 parts for storrage.
200 my($hostname, $domain) = split(/\./, $settings{'HOSTNAME'}, 2);
202 # Handle adding new accounts.
203 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
205 # Open /var/ipfire/ddns/config for writing.
206 open(FILE
, ">>$datafile") or die "Unable to open $datafile.";
208 # Lock file for writing.
211 # Add account data to the file.
212 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
214 # Close file after writing.
217 # Write out notice to logfile.
218 &General
::log($Lang::tr
{'ddns hostname added'});
220 # Update ddns config file.
222 # Handle account edditing.
223 } elsif ($settings{'ACTION'} eq $Lang::tr
{'update'}) {
225 # Open /var/ipfire/ddns/config for writing.
226 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
228 # Lock file for writing.
233 # Read file line by line.
234 foreach my $line (@current) {
236 if ($settings{'ID'} eq $id) {
237 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
246 # Close file after writing.
249 # Write out notice to logfile.
250 &General
::log($Lang::tr
{'ddns hostname modified'});
253 # Unset given CGI params.
256 # Update ddns config file.
257 &GenerateDDNSConfigFile
();
262 # Remove existing accounts.
264 if ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
266 # Open /var/ipfire/ddns/config for writing.
267 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
269 # Lock file for writing.
274 # Read file line by line.
275 foreach my $line (@current) {
277 # Write back every line, except the one we want to drop
278 # (identified by the ID)
279 unless ($settings{'ID'} eq $id) {
287 # Close file after writing.
290 # Unset given CGI params.
293 # Write out notice to logfile.
294 &General
::log($Lang::tr
{'ddns hostname removed'});
296 # Update ddns config file.
297 &GenerateDDNSConfigFile
();
301 # Read items for editing.
303 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
308 # Read file line by line.
309 foreach my $line (@current) {
311 if ($settings{'ID'} eq $id) {
316 # Splitt lines (splitting element is a single ",") and save values into temp array.
317 @temp = split(/\,/,$line);
319 $settings{'SERVICE'} = $temp[0];
320 $settings{'HOSTNAME'} = "$temp[1].$temp[2]";
321 $settings{'PROXY'} = $temp[3];
322 $settings{'WILDCARDS'} = $temp[4];
323 $settings{'LOGIN'} = $temp[5];
324 $settings{'PASSWORD'} = $temp[6];
325 $settings{'ENABLED'} = $temp[7];
334 # Handle forced updates.
336 if ($settings{'ACTION'} eq $Lang::tr
{'instant update'}) {
337 system(@ddnsprog) == 0 or die "@ddnsprog failed: $?\n";
341 # Set default values.
343 if (! $settings{'ACTION'}) {
344 $settings{'SERVICE'} = 'dyndns.org';
345 $settings{'ENABLED'} = 'on';
348 &Header
::openpage
($Lang::tr
{'dynamic dns'}, 1, '');
349 &Header
::openbigbox
('100%', 'left', '', $errormessage);
351 # Read file for general ddns settings.
352 &General
::readhash
($settingsfile, \
%settings);
355 $checked{'BEHINDROUTER'}{'RED_IP'} = '';
356 $checked{'BEHINDROUTER'}{'FETCH_IP'} = '';
357 $checked{'BEHINDROUTER'}{$settings{'BEHINDROUTER'}} = "checked='checked'";
359 $checked{'WILDCARDS'}{'on'} = ($settings{'WILDCARDS'} eq '') ?
'' : "checked='checked'";
360 $checked{'ENABLED'}{'on'} = ($settings{'ENABLED'} eq '' ) ?
'' : "checked='checked'";
362 # Show box for errormessages..
364 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
365 print "<font class='base'>$errormessage </font>";
369 &Header
::openbox
('100%', 'left', $Lang::tr
{'settings'});
372 # Section for general ddns setup.
374 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
377 <td class='base'>$Lang::tr{'dyn dns source choice'}</td>
380 <td class='base'><input type='radio' name='BEHINDROUTER' value='RED_IP' $checked{'BEHINDROUTER'}{'RED_IP'} />
381 $Lang::tr{'use ipfire red ip'}</td>
384 <td class='base'><input type='radio' name='BEHINDROUTER' value='FETCH_IP' $checked{'BEHINDROUTER'}{'FETCH_IP'} />
385 $Lang::tr{'fetch ip from'}</td>
393 <td align='right' valign='top' class='base'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
403 # Section to add or edit an existing entry.
406 my $buttontext = $Lang::tr
{'add'};
408 # Change buttontext and headline if we edit an account.
409 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
411 # Rename button and print headline for updating.
412 $buttontext = $Lang::tr
{'update'};
413 &Header
::openbox
('100%', 'left', $Lang::tr
{'edit an existing host'});
416 # Otherwise use default button text and show headline for adding a new account.
417 &Header
::openbox
('100%', 'left', $Lang::tr
{'add a host'});
422 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
423 <input type='hidden' name='ID' value='$settings{'ID'}' />
426 <td width='25%' class='base'>$Lang::tr{'service'}:</td>
430 # Generate dropdown menu for service selection.
431 print"<select size='1' name='SERVICE'>\n";
435 # Loop to print the providerlist.
436 foreach my $provider (@providers) {
438 # Check if the current provider needs to be selected.
439 if ($provider eq $settings{'SERVICE'}) {
440 $selected = 'selected';
445 # Print out the HTML option field.
446 print "<option value=\"$provider\" $selected>$provider</option>\n";
449 print"</select></td>\n";
451 <td width='20%' class='base'>$Lang::tr{'hostname'}:</td>
452 <td width='30%'><input type='text' name='HOSTNAME' value='$settings{'HOSTNAME'}' /></td>
456 <td class='base'>$Lang::tr{'enable wildcards'}</td>
457 <td><input type='checkbox' name='WILDCARDS' value='on' $checked{'WILDCARDS'}{'on'} /></td>
458 <td class='base'>$Lang::tr{'username'}:</td>
459 <td><input type='text' name='LOGIN' value='$settings{'LOGIN'}' /></td>
463 <td class='base'></td>
465 <td class='base'>$Lang::tr{'password'}</td>
466 <td><input type='password' name='PASSWORD' value='$settings{'PASSWORD'}' /></td>
470 <td class='base'>$Lang::tr{'enabled'}</td>
471 <td><input type='checkbox' name='ENABLED' value='on' $checked{'ENABLED'}{'on'} /></td>
472 <td class='base'></td>
481 <td width='30%' align='right' class='base'>
482 <input type='hidden' name='ACTION' value='$buttontext'>
483 <input type='submit' name='SUBMIT' value='$buttontext'></td>
492 # Third section, display all created ddns hosts.
494 &Header
::openbox
('100%', 'left', $Lang::tr
{'current hosts'});
496 <table width='100%' class='tbl'>
498 <th width='15%' align='center' class='boldbase'><b>$Lang::tr{'service'}</b></th>
499 <th width='25%' align='center' class='boldbase'><b>$Lang::tr{'hostname'}</b></th>
500 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'domain'}</b></th>
501 <th width='10%' align='center' class='boldbase'><b>$Lang::tr{'wildcards'}</b></th>
502 <th width='20%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
507 # Re-open file to get changes.
508 open(FILE
, $datafile) or die "Unable to open $datafile.";
512 # Get IP address of the red interface.
513 my $ip = &General
::GetDyndnsRedIP
;
516 my $toggle_wildcards;
518 foreach my $line (@current) {
522 my @temp = split(/\,/,$line);
524 # Generate checkbox icons for wildcard.
527 if ($temp[4] eq "on") {
528 $gifwildcard = 'on.gif';
529 $descwildcard = $Lang::tr
{'click to disable'};
530 $toggle_wildcards = 'off';
532 $gifwildcard = 'off.gif';
533 $descwildcard = $Lang::tr
{'click to enable'};
534 $toggle_wildcards = 'on';
537 # Generate value for enable/disable checkbox.
538 my $sync = "<font color='blue'>";
542 if ($temp[7] eq "on") {
544 $gdesc = $Lang::tr
{'click to disable'};
545 $sync = (&General
::DyndnsServiceSync
($ip,$temp[1], $temp[2]) ?
"<font color='green'>": "<font color='red'>") ;
546 $toggle_enabled = 'off';
549 $gdesc = $Lang::tr
{'click to enable'};
550 $toggle_enabled = 'on';
556 if ($settings{'ID'} eq $id) {
557 $col="bgcolor='${Header::colouryellow}'";
558 } elsif (!&General
::is_part_of
("$temp[0]", @providers)) {
559 $col="bgcolor='#FF4D4D'";
561 $col="bgcolor='$color{'color20'}'";
563 $col="bgcolor='$color{'color22'}'";
566 # The following HTML Code still is part of the loop.
569 <td align='center' $col><a href='http://$temp[0]'>$temp[0]</a></td>
570 <td align='center' $col>$sync$temp[1]</td>
571 <td align='center' $col>$sync$temp[2]</td>
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='WILDCARDS' value='$toggle_wildcards'>
576 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
577 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gifwildcard' alt='$descwildcard' title='$descwildcard' />
580 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
581 <input type='hidden' name='ID' value='$id'>
582 <input type='hidden' name='ENABLED' value='$toggle_enabled'>
583 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
584 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
587 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
588 <input type='hidden' name='ID' value='$id'>
589 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
590 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
593 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
594 <input type='hidden' name='ID' value='$id'>
595 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
596 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
605 # If table contains entries, print 'Key to action icons'
610 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
611 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
612 <td class='base'>$Lang::tr{'click to disable'}</td>
613 <td> </td>
614 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
615 <td class='base'>$Lang::tr{'click to enable'}</td>
616 <td> </td>
617 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
618 <td class='base'>$Lang::tr{'edit'}</td>
619 <td> </td>
620 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
621 <td class='base'>$Lang::tr{'remove'}</td>
622 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
623 <td align='right' width='30%'><input type='submit' name='ACTION' value='$Lang::tr{'instant update'}' /></td>
632 &Header
::closebigbox
();
633 &Header
::closepage
();
635 # Function to generate the required configuration file for the DDNS tool.
636 sub GenerateDDNSConfigFile
{
638 open(SETTINGS
, "<$datafile") or die "Could not open $datafile.";
640 open(FILE
, ">${General::swroot}/ddns/ddns.conf");
642 # Global configuration options.
643 print FILE
"[config]\n";
645 # Check if we guess our IP address by an extranal server.
646 if ($settings{'BEHINDROUTER'} eq "FETCH_IP") {
647 print FILE
"guess_external_ip = true\n";
649 print FILE
"guess_external_ip = false\n";
652 # Use an upstream proxy and generate proxy url.
654 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
655 if ($proxysettings{'UPSTREAM_PROXY'}) {
656 my $proxy_string = "http://";
658 if ($proxysettings{'UPSTREAM_USER'} && $proxysettings{'UPSTREAM_PASSWORD'}) {
659 $proxy_string .= "$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@";
662 $proxy_string .= $proxysettings{'UPSTREAM_PROXY'};
664 print FILE
"proxy = $proxy_string\n";
672 # Generate array based on the line content (seperator is a single or multiple space's)
673 my @settings = split(/,/, $line);
674 my ($provider, $hostname, $domain, $proxy, $wildcards, $username, $password, $enabled) = @settings;
676 # Skip entries if they are not (longer) supported.
677 next if (!&General
::is_part_of
("$provider", @providers));
679 # Skip disabled entries.
680 next if ($enabled eq "off");
682 print FILE
"[$hostname.$domain]\n";
683 print FILE
"provider = $provider\n";
687 # Handle token based auth for various providers.
688 if ($provider ~~ ["dns.lightningwirelabs.com", "regfish.com"] && $username eq "token") {
691 # Handle token auth for freedns.afraid.org.
692 } elsif ($provider eq "freedns.afraid.org" && $password eq "") {
694 $password = $username;
697 # Write auth details.
699 print FILE
"token = $password\n";
701 print FILE
"username = $username\n";
702 print FILE
"password = $password\n";
705 # These providers need to be set to only use IPv4.
706 if ($provider ~~ ["freedns.afraid.org", "variomedia.de", "zoneedit.com"]) {
707 print FILE
"proto = ipv4\n";
717 # Function which generates an array (@providers) which contains the supported providers.
719 # Get supported providers.
720 open(PROVIDERS
, "/usr/bin/ddns list-providers |");
722 # Create new array to store the providers.
725 while (<PROVIDERS
>) {
728 # Remove following newlines.
731 # Add provider to the array.
732 push(@providers, $provider);