]>
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 # 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 # 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 $settings{'SERVICE'} = $temp[0];
318 $settings{'HOSTNAME'} = "$temp[1].$temp[2]";
319 $settings{'PROXY'} = $temp[3];
320 $settings{'WILDCARDS'} = $temp[4];
321 $settings{'LOGIN'} = $temp[5];
322 $settings{'PASSWORD'} = $temp[6];
323 $settings{'ENABLED'} = $temp[7];
332 # Handle forced updates.
334 if ($settings{'ACTION'} eq $Lang::tr
{'instant update'}) {
335 system(@ddnsprog) == 0 or die "@ddnsprog failed: $?\n";
339 # Set default values.
341 if (! $settings{'ACTION'}) {
342 $settings{'SERVICE'} = 'dyndns.org';
343 $settings{'ENABLED'} = 'on';
346 &Header
::openpage
($Lang::tr
{'dynamic dns'}, 1, '');
347 &Header
::openbigbox
('100%', 'left', '', $errormessage);
349 # Read file for general ddns settings.
350 &General
::readhash
($settingsfile, \
%settings);
353 $checked{'BEHINDROUTER'}{'RED_IP'} = '';
354 $checked{'BEHINDROUTER'}{'FETCH_IP'} = '';
355 $checked{'BEHINDROUTER'}{$settings{'BEHINDROUTER'}} = "checked='checked'";
357 $checked{'ENABLED'}{'on'} = ($settings{'ENABLED'} eq '' ) ?
'' : "checked='checked'";
359 # Show box for errormessages..
361 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
362 print "<font class='base'>$errormessage </font>";
366 &Header
::openbox
('100%', 'left', $Lang::tr
{'settings'});
369 # Section for general ddns setup.
371 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
374 <td class='base'>$Lang::tr{'dyn dns source choice'}</td>
377 <td class='base'><input type='radio' name='BEHINDROUTER' value='RED_IP' $checked{'BEHINDROUTER'}{'RED_IP'} />
378 $Lang::tr{'use ipfire red ip'}</td>
381 <td class='base'><input type='radio' name='BEHINDROUTER' value='FETCH_IP' $checked{'BEHINDROUTER'}{'FETCH_IP'} />
382 $Lang::tr{'fetch ip from'}</td>
390 <td align='right' valign='top' class='base'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
400 # Section to add or edit an existing entry.
403 my $buttontext = $Lang::tr
{'add'};
405 # Change buttontext and headline if we edit an account.
406 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
408 # Rename button and print headline for updating.
409 $buttontext = $Lang::tr
{'update'};
410 &Header
::openbox
('100%', 'left', $Lang::tr
{'edit an existing host'});
413 # Otherwise use default button text and show headline for adding a new account.
414 &Header
::openbox
('100%', 'left', $Lang::tr
{'add a host'});
419 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
420 <input type='hidden' name='ID' value='$settings{'ID'}' />
423 <td width='25%' class='base'>$Lang::tr{'service'}:</td>
427 # Generate dropdown menu for service selection.
428 print"<select size='1' name='SERVICE'>\n";
432 # Loop to print the providerlist.
433 foreach my $provider (@providers) {
435 # Check if the current provider needs to be selected.
436 if ($provider eq $settings{'SERVICE'}) {
437 $selected = 'selected';
442 # Print out the HTML option field.
443 print "<option value=\"$provider\" $selected>$provider</option>\n";
446 print"</select></td>\n";
448 <td width='20%' class='base'>$Lang::tr{'hostname'}:</td>
449 <td width='30%'><input type='text' name='HOSTNAME' value='$settings{'HOSTNAME'}' /></td>
453 <td class='base'>$Lang::tr{'enabled'}</td>
454 <td><input type='checkbox' name='ENABLED' value='on' $checked{'ENABLED'}{'on'} /></td>
455 <td class='base'>$Lang::tr{'username'}</td>
456 <td><input type='text' name='LOGIN' value='$settings{'LOGIN'}' /></td>
460 <td class='base'></td>
462 <td class='base'>$Lang::tr{'password'}</td>
463 <td><input type='password' name='PASSWORD' value='$settings{'PASSWORD'}' /></td>
471 <td width='30%' align='right' class='base'>
472 <input type='hidden' name='ACTION' value='$buttontext'>
473 <input type='submit' name='SUBMIT' value='$buttontext'></td>
482 # Third section, display all created ddns hosts.
483 # Re-open file to get changes.
484 open(FILE
, $datafile) or die "Unable to open $datafile.";
488 # Get IP address of the red interface.
489 my $ip = &General
::GetDyndnsRedIP
();
494 &Header
::openbox
('100%', 'left', $Lang::tr
{'current hosts'});
497 <table width='100%' class='tbl'>
499 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'service'}</b></th>
500 <th width='50%' align='center' class='boldbase'><b>$Lang::tr{'hostname'}</b></th>
501 <th width='20%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
505 foreach my $line (@current) {
508 my @temp = split(/\,/,$line);
510 # Generate value for enable/disable checkbox.
511 my $sync = "<font color='blue'>";
515 if ($temp[7] eq "on") {
517 $gdesc = $Lang::tr
{'click to disable'};
518 $sync = (&General
::DyndnsServiceSync
($ip,$temp[1], $temp[2]) ?
"<font color='green'>": "<font color='red'>") ;
519 $toggle_enabled = 'off';
522 $gdesc = $Lang::tr
{'click to enable'};
523 $toggle_enabled = 'on';
529 if ($settings{'ID'} eq $id) {
530 $col="bgcolor='${Header::colouryellow}'";
531 } elsif (!($temp[0] ~~ @providers)) {
532 $col="bgcolor='#FF4D4D'";
534 $col="bgcolor='$color{'color20'}'";
536 $col="bgcolor='$color{'color22'}'";
539 # The following HTML Code still is part of the loop.
542 <td align='center' $col><a href='http://$temp[0]'>$temp[0]</a></td>
543 <td align='center' $col>$sync$temp[1].$sync$temp[2]</td>
545 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
546 <input type='hidden' name='ID' value='$id'>
547 <input type='hidden' name='ENABLED' value='$toggle_enabled'>
548 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
549 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
552 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
553 <input type='hidden' name='ID' value='$id'>
554 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
555 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
558 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
559 <input type='hidden' name='ID' value='$id'>
560 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
561 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
572 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
573 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
574 <td class='base'>$Lang::tr{'click to disable'}</td>
575 <td> </td>
576 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
577 <td class='base'>$Lang::tr{'click to enable'}</td>
578 <td> </td>
579 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
580 <td class='base'>$Lang::tr{'edit'}</td>
581 <td> </td>
582 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
583 <td class='base'>$Lang::tr{'remove'}</td>
584 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
585 <td align='right' width='30%'><input type='submit' name='ACTION' value='$Lang::tr{'instant update'}' /></td>
594 &Header
::closebigbox
();
595 &Header
::closepage
();
597 # Function to generate the required configuration file for the DDNS tool.
598 sub GenerateDDNSConfigFile
{
600 open(SETTINGS
, "<$datafile") or die "Could not open $datafile.";
602 open(FILE
, ">${General::swroot}/ddns/ddns.conf");
604 # Global configuration options.
605 print FILE
"[config]\n";
607 # Check if we guess our IP address by an extranal server.
608 if ($settings{'BEHINDROUTER'} eq "FETCH_IP") {
609 print FILE
"guess_external_ip = true\n";
611 print FILE
"guess_external_ip = false\n";
614 # Use an upstream proxy and generate proxy url.
616 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
617 if ($proxysettings{'UPSTREAM_PROXY'}) {
618 my $proxy_string = "http://";
620 if ($proxysettings{'UPSTREAM_USER'} && $proxysettings{'UPSTREAM_PASSWORD'}) {
621 $proxy_string .= "$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@";
624 $proxy_string .= $proxysettings{'UPSTREAM_PROXY'};
626 print FILE
"proxy = $proxy_string\n";
634 # Generate array based on the line content (seperator is a single or multiple space's)
635 my @settings = split(/,/, $line);
636 my ($provider, $hostname, $domain, $proxy, $wildcards, $username, $password, $enabled) = @settings;
638 # Skip entries if they are not (longer) supported.
639 next unless ($provider ~~ @providers);
641 # Skip disabled entries.
642 next if ($enabled eq "off");
644 print FILE
"[$hostname.$domain]\n";
645 print FILE
"provider = $provider\n";
649 # Handle token based auth for various providers.
650 if ($provider ~~ ["dns.lightningwirelabs.com", "regfish.com"] && $username eq "token") {
653 # Handle token auth for freedns.afraid.org.
654 } elsif ($provider eq "freedns.afraid.org" && $password eq "") {
656 $password = $username;
658 # Handle keys for nsupdate
659 } elsif (($provider eq "nsupdate") && $username && $password) {
660 print FILE
"key = $username\n";
661 print FILE
"secret = $password\n";
666 # Handle keys for nsupdate.info
667 } elsif (($provider eq "nsupdate.info") && $password) {
668 print FILE
"secret = $password\n";
674 # Write auth details.
676 print FILE
"token = $password\n";
677 } elsif ($username && $password) {
678 print FILE
"username = $username\n";
679 print FILE
"password = $password\n";
682 # These providers need to be set to only use IPv4.
683 if ($provider ~~ ["freedns.afraid.org", "nsupdate.info", "variomedia.de", "zoneedit.com"]) {
684 print FILE
"proto = ipv4\n";
694 # Function which generates an array (@providers) which contains the supported providers.
696 # Get supported providers.
697 open(PROVIDERS
, "/usr/bin/ddns list-providers |");
699 # Create new array to store the providers.
702 while (<PROVIDERS
>) {
705 # Remove following newlines.
708 # Add provider to the array.
709 push(@providers, $provider);