]>
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'}) {
92 # Open /var/ipfire/ddns/settings for writing.
93 open(FILE
, ">$settingsfile") or die "Unable to open $settingsfile.";
95 # Lock file for writing.
98 # Check if BEHINDROUTER has been configured.
99 if ($settings{'BEHINDROUTER'} ne '') {
100 print FILE
"BEHINDROUTER=$settings{'BEHINDROUTER'}\n";
103 # Close file after writing.
106 # Unset given CGI parmas.
109 # Update ddns config file.
110 &GenerateDDNSConfigFile
();
114 # Toggle enable/disable field. Field is in second position
116 if ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
117 # Open /var/ipfire/ddns/config for writing.
118 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
120 # Lock file for writing.
126 # Read file line by line.
127 foreach my $line (@current) {
131 if ($settings{'ID'} eq $id) {
132 # Splitt lines (splitting element is a single ",") and save values into temp array.
133 @temp = split(/\,/,$line);
135 # Check if we want to toggle ENABLED or WILDCARDS.
136 if ($settings{'ENABLED'} ne '') {
138 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 # Write out logging notice.
153 &General
::log($Lang::tr
{'ddns hostname modified'});
155 # Update ddns config file.
156 &GenerateDDNSConfigFile
();
160 # Add new accounts, or edit existing ones.
162 if (($settings{'ACTION'} eq $Lang::tr
{'add'}) || ($settings{'ACTION'} eq $Lang::tr
{'update'})) {
164 # Check if a hostname has been given.
165 if ($settings{'HOSTNAME'} eq '') {
166 $errormessage = $Lang::tr
{'hostname not set'};
169 # Check if a valid domainname has been provided.
170 if (!&General
::validdomainname
($settings{'HOSTNAME'})) {
171 $errormessage = $Lang::tr
{'invalid domain name'};
174 # Check if a username has been sent.
175 if ($settings{'LOGIN'} eq '') {
176 $errormessage = $Lang::tr
{'username not set'};
179 # Check if a password has been typed in.
180 # freedns.afraid.org does not require this field.
181 if (($settings{'PASSWORD'} eq '') && ($settings{'SERVICE'} ne 'freedns.afraid.org') && ($settings{'SERVICE'} ne 'regfish.com')) {
182 $errormessage = $Lang::tr
{'password not set'};
185 # Go furter if there was no error.
186 if (!$errormessage) {
187 # Splitt hostname field into 2 parts for storrage.
188 my($hostname, $domain) = split(/\./, $settings{'HOSTNAME'}, 2);
190 # Handle enabled checkbox. When the checkbox is selected a "on" will be returned,
191 # if the checkbox is not checked nothing is returned in this case we set the value to "off".
192 if ($settings{'ENABLED'} ne 'on') {
193 $settings{'ENABLED'} = 'off';
196 # Handle adding new accounts.
197 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
198 # Open /var/ipfire/ddns/config for writing.
199 open(FILE
, ">>$datafile") or die "Unable to open $datafile.";
201 # Lock file for writing.
204 # Add account data to the file.
205 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
207 # Close file after writing.
210 # Write out notice to logfile.
211 &General
::log($Lang::tr
{'ddns hostname added'});
213 # Handle account edditing.
214 } elsif ($settings{'ACTION'} eq $Lang::tr
{'update'}) {
215 # Open /var/ipfire/ddns/config for writing.
216 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
218 # Lock file for writing.
223 # Read file line by line.
224 foreach my $line (@current) {
225 if ($settings{'ID'} eq $id) {
226 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
235 # Close file after writing.
238 # Write out notice to logfile.
239 &General
::log($Lang::tr
{'ddns hostname modified'});
242 # Unset given CGI params.
245 # Update ddns config file.
246 &GenerateDDNSConfigFile
();
251 # Remove existing accounts.
253 if ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
254 # Open /var/ipfire/ddns/config for writing.
255 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
257 # Lock file for writing.
262 # Read file line by line.
263 foreach my $line (@current) {
264 # Write back every line, except the one we want to drop
265 # (identified by the ID)
266 unless ($settings{'ID'} eq $id) {
274 # Close file after writing.
277 # Unset given CGI params.
280 # Write out notice to logfile.
281 &General
::log($Lang::tr
{'ddns hostname removed'});
283 # Update ddns config file.
284 &GenerateDDNSConfigFile
();
288 # Read items for editing.
290 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
294 # Read file line by line.
295 foreach my $line (@current) {
296 if ($settings{'ID'} eq $id) {
300 # Splitt lines (splitting element is a single ",") and save values into temp array.
301 @temp = split(/\,/,$line);
303 $settings{'SERVICE'} = $temp[0];
304 $settings{'HOSTNAME'} = "$temp[1].$temp[2]";
305 $settings{'PROXY'} = $temp[3];
306 $settings{'WILDCARDS'} = $temp[4];
307 $settings{'LOGIN'} = $temp[5];
308 $settings{'PASSWORD'} = $temp[6];
309 $settings{'ENABLED'} = $temp[7];
316 &GenerateDDNSConfigFile
();
320 # Handle forced updates.
322 if ($settings{'ACTION'} eq $Lang::tr
{'instant update'}) {
323 system(@ddnsprog) == 0 or die "@ddnsprog failed: $?\n";
327 # Set default values.
329 if (! $settings{'ACTION'}) {
330 $settings{'SERVICE'} = 'dyndns.org';
331 $settings{'ENABLED'} = 'on';
334 &Header
::openpage
($Lang::tr
{'dynamic dns'}, 1, '');
335 &Header
::openbigbox
('100%', 'left', '', $errormessage);
337 # Read file for general ddns settings.
338 &General
::readhash
($settingsfile, \
%settings);
341 $checked{'BEHINDROUTER'}{'RED_IP'} = '';
342 $checked{'BEHINDROUTER'}{'FETCH_IP'} = '';
343 $checked{'BEHINDROUTER'}{$settings{'BEHINDROUTER'}} = "checked='checked'";
345 $checked{'ENABLED'}{'on'} = '';
346 $checked{'ENABLED'}{'off'} = '';
347 $checked{'ENABLED'}{$settings{'ENABLED'}} = "checked='checked'";
349 # Show box for errormessages..
351 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
352 print "<font class='base'>$errormessage </font>";
356 &Header
::openbox
('100%', 'left', $Lang::tr
{'settings'});
359 # Section for general ddns setup.
361 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
364 <td class='base'>$Lang::tr{'dyn dns source choice'}</td>
367 <td class='base'><input type='radio' name='BEHINDROUTER' value='RED_IP' $checked{'BEHINDROUTER'}{'RED_IP'} />
368 $Lang::tr{'use ipfire red ip'}</td>
371 <td class='base'><input type='radio' name='BEHINDROUTER' value='FETCH_IP' $checked{'BEHINDROUTER'}{'FETCH_IP'} />
372 $Lang::tr{'fetch ip from'}</td>
380 <td align='right' valign='top' class='base'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
390 # Section to add or edit an existing entry.
393 my $buttontext = $Lang::tr
{'add'};
395 # Change buttontext and headline if we edit an account.
396 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
397 # Rename button and print headline for updating.
398 $buttontext = $Lang::tr
{'update'};
399 &Header
::openbox
('100%', 'left', $Lang::tr
{'edit an existing host'});
401 # Otherwise use default button text and show headline for adding a new account.
402 &Header
::openbox
('100%', 'left', $Lang::tr
{'add a host'});
406 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
407 <input type='hidden' name='ID' value='$settings{'ID'}' />
410 <td width='25%' class='base'>$Lang::tr{'service'}:</td>
414 # Generate dropdown menu for service selection.
415 print"<select size='1' name='SERVICE'>\n";
419 # Loop to print the providerlist.
420 foreach my $provider (@providers) {
421 # Check if the current provider needs to be selected.
422 if ($provider eq $settings{'SERVICE'}) {
423 $selected = 'selected';
428 # Print out the HTML option field.
429 print "<option value=\"$provider\" $selected>$provider</option>\n";
432 print"</select></td>\n";
434 <td width='20%' class='base'>$Lang::tr{'hostname'}:</td>
435 <td width='30%'><input type='text' name='HOSTNAME' value='$settings{'HOSTNAME'}' /></td>
439 <td class='base'>$Lang::tr{'enabled'}</td>
440 <td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
441 <td class='base'>$Lang::tr{'username'}</td>
442 <td><input type='text' name='LOGIN' value='$settings{'LOGIN'}' /></td>
446 <td class='base'></td>
448 <td class='base'>$Lang::tr{'password'}</td>
449 <td><input type='password' name='PASSWORD' value='$settings{'PASSWORD'}' /></td>
457 <td width='30%' align='right' class='base'>
458 <input type='hidden' name='ACTION' value='$buttontext'>
459 <input type='submit' name='SUBMIT' value='$buttontext'></td>
468 # Third section, display all created ddns hosts.
469 # Re-open file to get changes.
470 open(FILE
, $datafile) or die "Unable to open $datafile.";
474 # Get IP address of the red interface.
475 my $ip = &General
::GetDyndnsRedIP
();
480 &Header
::openbox
('100%', 'left', $Lang::tr
{'current hosts'});
483 <table width='100%' class='tbl'>
485 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'service'}</b></th>
486 <th width='50%' align='center' class='boldbase'><b>$Lang::tr{'hostname'}</b></th>
487 <th width='20%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
491 foreach my $line (@current) {
494 my @temp = split(/\,/,$line);
496 # Handle hostname details. Only connect the values with a dott if both are available.
499 if (($temp[1]) && ($temp[2])) {
500 $hostname="$temp[1].$temp[2]";
502 $hostname="$temp[1]";
505 # Generate value for enable/disable checkbox.
510 if ($temp[7] eq "on") {
512 $gdesc = $Lang::tr
{'click to disable'};
514 # Check if the given hostname is a FQDN before doing a nslookup.
515 if (&General
::validfqdn
($hostname)) {
516 $sync = (&General
::DyndnsServiceSync
($ip,$temp[1], $temp[2]) ?
"<font color='green'>": "<font color='red'>") ;
519 $toggle_enabled = 'off';
521 $sync = "<font color='blue'>";
523 $gdesc = $Lang::tr
{'click to enable'};
524 $toggle_enabled = 'on';
530 if ($settings{'ID'} eq $id) {
531 $col="bgcolor='${Header::colouryellow}'";
532 } elsif (!($temp[0] ~~ @providers)) {
533 $col="bgcolor='#FF4D4D'";
535 $col="bgcolor='$color{'color20'}'";
537 $col="bgcolor='$color{'color22'}'";
540 # The following HTML Code still is part of the loop.
543 <td align='center' $col><a href='http://$temp[0]'>$temp[0]</a></td>
544 <td align='center' $col>$sync$temp[1].$sync$temp[2]</td>
546 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
547 <input type='hidden' name='ID' value='$id'>
548 <input type='hidden' name='ENABLED' value='$toggle_enabled'>
549 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
550 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
553 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
554 <input type='hidden' name='ID' value='$id'>
555 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
556 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
559 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
560 <input type='hidden' name='ID' value='$id'>
561 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
562 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
573 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
574 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
575 <td class='base'>$Lang::tr{'click to disable'}</td>
576 <td> </td>
577 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
578 <td class='base'>$Lang::tr{'click to enable'}</td>
579 <td> </td>
580 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
581 <td class='base'>$Lang::tr{'edit'}</td>
582 <td> </td>
583 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
584 <td class='base'>$Lang::tr{'remove'}</td>
585 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
586 <td align='right' width='30%'><input type='submit' name='ACTION' value='$Lang::tr{'instant update'}' /></td>
595 &Header
::closebigbox
();
596 &Header
::closepage
();
598 # Function to generate the required configuration file for the DDNS tool.
599 sub GenerateDDNSConfigFile
{
601 open(SETTINGS
, "<$datafile") or die "Could not open $datafile.";
603 open(FILE
, ">${General::swroot}/ddns/ddns.conf");
605 # Global configuration options.
606 print FILE
"[config]\n";
608 # Check if we guess our IP address by an extranal server.
609 if ($settings{'BEHINDROUTER'} eq "FETCH_IP") {
610 print FILE
"guess_external_ip = true\n";
612 print FILE
"guess_external_ip = false\n";
615 # Use an upstream proxy and generate proxy url.
617 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
618 if ($proxysettings{'UPSTREAM_PROXY'}) {
619 my $proxy_string = "http://";
621 if ($proxysettings{'UPSTREAM_USER'} && $proxysettings{'UPSTREAM_PASSWORD'}) {
622 $proxy_string .= "$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@";
625 $proxy_string .= $proxysettings{'UPSTREAM_PROXY'};
627 print FILE
"proxy = $proxy_string\n";
636 # Generate array based on the line content (seperator is a single or multiple space's)
637 my @settings = split(/,/, $line);
638 my ($provider, $hostname, $domain, $proxy, $wildcards, $username, $password, $enabled) = @settings;
640 # Skip entries if they are not (longer) supported.
641 next unless ($provider ~~ @providers);
643 # Skip disabled entries.
644 next unless ($enabled eq "on");
646 print FILE
"[$hostname.$domain]\n";
647 print FILE
"provider = $provider\n";
651 # Handle token based auth for various providers.
652 if ($provider ~~ ["dns.lightningwirelabs.com", "entrydns.net", "regfish.com"] && $username eq "token") {
655 # Handle token auth for freedns.afraid.org and regfish.com.
656 } elsif ($provider ~~ ["freedns.afraid.org", "regfish.com"] && $password eq "") {
658 $password = $username;
660 # Handle keys for nsupdate
661 } elsif (($provider eq "nsupdate") && $username && $password) {
662 print FILE
"key = $username\n";
663 print FILE
"secret = $password\n";
668 # Handle keys for nsupdate.info
669 } elsif (($provider eq "nsupdate.info") && $password) {
670 print FILE
"secret = $password\n";
676 # Write auth details.
678 print FILE
"token = $password\n";
679 } elsif ($username && $password) {
680 print FILE
"username = $username\n";
681 print FILE
"password = $password\n";
684 # These providers need to be set to only use IPv4.
685 if ($provider ~~ ["freedns.afraid.org", "nsupdate.info", "opendns.com", "variomedia.de", "zoneedit.com"]) {
686 print FILE
"proto = ipv4\n";
696 # Function which generates an array (@providers) which contains the supported providers.
698 # Get supported providers.
699 open(PROVIDERS
, "/usr/bin/ddns list-providers |");
701 # Create new array to store the providers.
704 while (<PROVIDERS
>) {
707 # Remove following newlines.
710 # Add provider to the array.
711 push(@providers, $provider);