]>
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 # Update ddns config file.
107 &GenerateDDNSConfigFile
();
111 # Toggle enable/disable field. Field is in second position
113 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) {
128 if ($settings{'ID'} eq $id) {
129 # Splitt lines (splitting element is a single ",") and save values into temp array.
130 @temp = split(/\,/,$line);
132 # Check if we want to toggle ENABLED or WILDCARDS.
133 if ($settings{'ENABLED'} ne '') {
135 print FILE
"$temp[0],$temp[1],$temp[2],$temp[3],$temp[4],$temp[5],$temp[6],$settings{'ENABLED'}\n";
138 # Print unmodified line.
139 print FILE
"$line\n";
145 undef $settings{'ID'};
147 # Close file after writing.
150 # Write out logging notice.
151 &General
::log($Lang::tr
{'ddns hostname modified'});
153 # Update ddns config file.
154 &GenerateDDNSConfigFile
();
158 # Add new accounts, or edit existing ones.
160 if (($settings{'ACTION'} eq $Lang::tr
{'add'}) || ($settings{'ACTION'} eq $Lang::tr
{'update'})) {
161 # Check if a hostname has been given.
162 if ($settings{'HOSTNAME'} eq '') {
163 $errormessage = $Lang::tr
{'hostname not set'};
166 # Check if a valid domainname has been provided.
167 if (!&General
::validdomainname
($settings{'HOSTNAME'})) {
168 $errormessage = $Lang::tr
{'invalid domain name'};
171 # Check if a username has been sent.
172 if ($settings{'LOGIN'} eq '') {
173 $errormessage = $Lang::tr
{'username not set'};
176 # Check if a password has been typed in.
177 # freedns.afraid.org does not require this field.
178 if (($settings{'PASSWORD'} eq '') && ($settings{'SERVICE'} ne 'freedns.afraid.org') && ($settings{'SERVICE'} ne 'regfish.com')) {
179 $errormessage = $Lang::tr
{'password not set'};
182 # Go furter if there was no error.
183 if (!$errormessage) {
184 # Splitt hostname field into 2 parts for storrage.
185 my($hostname, $domain) = split(/\./, $settings{'HOSTNAME'}, 2);
187 # Handle enabled checkbox. When the checkbox is selected a "on" will be returned,
188 # if the checkbox is not checked nothing is returned in this case we set the value to "off".
189 if ($settings{'ENABLED'} ne 'on') {
190 $settings{'ENABLED'} = 'off';
193 # Handle adding new accounts.
194 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
195 # Open /var/ipfire/ddns/config for writing.
196 open(FILE
, ">>$datafile") or die "Unable to open $datafile.";
198 # Lock file for writing.
201 # Add account data to the file.
202 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
204 # Close file after writing.
207 # Write out notice to logfile.
208 &General
::log($Lang::tr
{'ddns hostname added'});
210 # Handle account edditing.
211 } elsif ($settings{'ACTION'} eq $Lang::tr
{'update'}) {
212 # Open /var/ipfire/ddns/config for writing.
213 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
215 # Lock file for writing.
220 # Read file line by line.
221 foreach my $line (@current) {
222 if ($settings{'ID'} eq $id) {
223 print FILE
"$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
232 # Close file after writing.
235 # Write out notice to logfile.
236 &General
::log($Lang::tr
{'ddns hostname modified'});
238 undef $settings{'ID'};
240 # Update ddns config file.
241 &GenerateDDNSConfigFile
();
246 # Remove existing accounts.
248 if ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
249 # Open /var/ipfire/ddns/config for writing.
250 open(FILE
, ">$datafile") or die "Unable to open $datafile.";
252 # Lock file for writing.
257 # Read file line by line.
258 foreach my $line (@current) {
259 # Write back every line, except the one we want to drop
260 # (identified by the ID)
261 unless ($settings{'ID'} eq $id) {
268 undef $settings{'ID'};
270 # Close file after writing.
273 # Write out notice to logfile.
274 &General
::log($Lang::tr
{'ddns hostname removed'});
276 # Update ddns config file.
277 &GenerateDDNSConfigFile
();
281 # Read items for editing.
283 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
287 # Read file line by line.
288 foreach my $line (@current) {
289 if ($settings{'ID'} eq $id) {
293 # Splitt lines (splitting element is a single ",") and save values into temp array.
294 @temp = split(/\,/,$line);
296 # Handle hostname details. Only connect the values with a dott if both are available.
299 if (($temp[1]) && ($temp[2])) {
300 $hostname = "$temp[1].$temp[2]";
302 $hostname = "$temp[1]";
305 $settings{'SERVICE'} = $temp[0];
306 $settings{'HOSTNAME'} = $hostname;
307 $settings{'PROXY'} = $temp[3];
308 $settings{'WILDCARDS'} = $temp[4];
309 $settings{'LOGIN'} = $temp[5];
310 $settings{'PASSWORD'} = $temp[6];
311 $settings{'ENABLED'} = $temp[7];
318 &GenerateDDNSConfigFile
();
322 # Handle forced updates.
324 if ($settings{'ACTION'} eq $Lang::tr
{'instant update'}) {
325 system(@ddnsprog) == 0 or die "@ddnsprog failed: $?\n";
329 # Set default values.
331 if (!$settings{'ACTION'}) {
332 $settings{'SERVICE'} = 'dyndns.org';
333 $settings{'ENABLED'} = 'on';
334 $settings{'ID'} = '';
337 &Header
::openpage
($Lang::tr
{'dynamic dns'}, 1, '');
338 &Header
::openbigbox
('100%', 'left', '', $errormessage);
340 # Read file for general ddns settings.
341 &General
::readhash
($settingsfile, \
%settings);
344 $checked{'BEHINDROUTER'}{'RED_IP'} = '';
345 $checked{'BEHINDROUTER'}{'FETCH_IP'} = '';
346 $checked{'BEHINDROUTER'}{$settings{'BEHINDROUTER'}} = "checked='checked'";
348 $checked{'ENABLED'}{'on'} = '';
349 $checked{'ENABLED'}{'off'} = '';
350 $checked{'ENABLED'}{$settings{'ENABLED'}} = "checked='checked'";
352 # Show box for errormessages..
354 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
355 print "<font class='base'>$errormessage </font>";
359 &Header
::openbox
('100%', 'left', $Lang::tr
{'settings'});
362 # Section for general ddns setup.
364 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
367 <td class='base'>$Lang::tr{'dyn dns source choice'}</td>
370 <td class='base'><input type='radio' name='BEHINDROUTER' value='RED_IP' $checked{'BEHINDROUTER'}{'RED_IP'} />
371 $Lang::tr{'use ipfire red ip'}</td>
374 <td class='base'><input type='radio' name='BEHINDROUTER' value='FETCH_IP' $checked{'BEHINDROUTER'}{'FETCH_IP'} />
375 $Lang::tr{'fetch ip from'}</td>
383 <td align='right' valign='top' class='base'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
393 # Section to add or edit an existing entry.
396 my $buttontext = $Lang::tr
{'add'};
398 # Change buttontext and headline if we edit an account.
399 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
400 # Rename button and print headline for updating.
401 $buttontext = $Lang::tr
{'update'};
402 &Header
::openbox
('100%', 'left', $Lang::tr
{'edit an existing host'});
404 # Otherwise use default button text and show headline for adding a new account.
405 &Header
::openbox
('100%', 'left', $Lang::tr
{'add a host'});
409 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
410 <input type='hidden' name='ID' value='$settings{'ID'}' />
413 <td width='25%' class='base'>$Lang::tr{'service'}:</td>
417 # Generate dropdown menu for service selection.
418 print"<select size='1' name='SERVICE'>\n";
422 # Loop to print the providerlist.
423 foreach my $provider (@providers) {
424 # Check if the current provider needs to be selected.
425 if ($provider eq $settings{'SERVICE'}) {
426 $selected = 'selected';
431 # Print out the HTML option field.
432 print "<option value=\"$provider\" $selected>$provider</option>\n";
435 print"</select></td>\n";
437 <td width='20%' class='base'>$Lang::tr{'hostname'}:</td>
438 <td width='30%'><input type='text' name='HOSTNAME' value='$settings{'HOSTNAME'}' /></td>
442 <td class='base'>$Lang::tr{'enabled'}</td>
443 <td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
444 <td class='base'>$Lang::tr{'username'}</td>
445 <td><input type='text' name='LOGIN' value='$settings{'LOGIN'}' /></td>
449 <td class='base'></td>
451 <td class='base'>$Lang::tr{'password'}</td>
452 <td><input type='password' name='PASSWORD' value='$settings{'PASSWORD'}' /></td>
460 <td width='30%' align='right' class='base'>
461 <input type='hidden' name='ACTION' value='$buttontext'>
462 <input type='submit' name='SUBMIT' value='$buttontext'></td>
471 # Third section, display all created ddns hosts.
472 # Re-open file to get changes.
473 open(FILE
, $datafile) or die "Unable to open $datafile.";
477 # Get IP address of the red interface.
478 my $ip = &General
::GetDyndnsRedIP
();
483 &Header
::openbox
('100%', 'left', $Lang::tr
{'current hosts'});
486 <table width='100%' class='tbl'>
488 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'service'}</b></th>
489 <th width='50%' align='center' class='boldbase'><b>$Lang::tr{'hostname'}</b></th>
490 <th width='20%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
494 foreach my $line (@current) {
497 my @temp = split(/\,/,$line);
499 # Handle hostname details. Only connect the values with a dott if both are available.
502 if (($temp[1]) && ($temp[2])) {
503 $hostname="$temp[1].$temp[2]";
505 $hostname="$temp[1]";
508 # Generate value for enable/disable checkbox.
513 if ($temp[7] eq "on") {
515 $gdesc = $Lang::tr
{'click to disable'};
517 # Check if the given hostname is a FQDN before doing a nslookup.
518 if (&General
::validfqdn
($hostname)) {
519 $sync = (&General
::DyndnsServiceSync
($ip,$temp[1], $temp[2]) ?
"<font color='green'>": "<font color='red'>") ;
522 $toggle_enabled = 'off';
524 $sync = "<font color='blue'>";
526 $gdesc = $Lang::tr
{'click to enable'};
527 $toggle_enabled = 'on';
533 if ($settings{'ID'} eq $id) {
534 $col="bgcolor='${Header::colouryellow}'";
535 } elsif (!($temp[0] ~~ @providers)) {
536 $col="bgcolor='#FF4D4D'";
538 $col="bgcolor='$color{'color20'}'";
540 $col="bgcolor='$color{'color22'}'";
543 # Handle hostname details. Only connect the values with a dott if both are available.
546 if (($temp[1]) && ($temp[2])) {
547 $hostname="$temp[1].$temp[2]";
549 $hostname="$temp[1]";
552 # The following HTML Code still is part of the loop.
555 <td align='center' $col><a href='http://$temp[0]'>$temp[0]</a></td>
556 <td align='center' $col>$sync$hostname</td>
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='ENABLED' value='$toggle_enabled'>
561 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
562 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
565 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
566 <input type='hidden' name='ID' value='$id'>
567 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
568 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
571 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
572 <input type='hidden' name='ID' value='$id'>
573 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
574 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
585 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
586 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
587 <td class='base'>$Lang::tr{'click to disable'}</td>
588 <td> </td>
589 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
590 <td class='base'>$Lang::tr{'click to enable'}</td>
591 <td> </td>
592 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
593 <td class='base'>$Lang::tr{'edit'}</td>
594 <td> </td>
595 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
596 <td class='base'>$Lang::tr{'remove'}</td>
597 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
598 <td align='right' width='30%'><input type='submit' name='ACTION' value='$Lang::tr{'instant update'}' /></td>
607 &Header
::closebigbox
();
608 &Header
::closepage
();
610 # Function to generate the required configuration file for the DDNS tool.
611 sub GenerateDDNSConfigFile
{
613 open(SETTINGS
, "<$datafile") or die "Could not open $datafile.";
615 open(FILE
, ">${General::swroot}/ddns/ddns.conf");
617 # Global configuration options.
618 print FILE
"[config]\n";
620 # Check if we guess our IP address by an extranal server.
621 if ($settings{'BEHINDROUTER'} eq "FETCH_IP") {
622 print FILE
"guess_external_ip = true\n";
624 print FILE
"guess_external_ip = false\n";
627 # Use an upstream proxy and generate proxy url.
629 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
630 if ($proxysettings{'UPSTREAM_PROXY'}) {
631 my $proxy_string = "http://";
633 if ($proxysettings{'UPSTREAM_USER'} && $proxysettings{'UPSTREAM_PASSWORD'}) {
634 $proxy_string .= "$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@";
637 $proxy_string .= $proxysettings{'UPSTREAM_PROXY'};
639 print FILE
"proxy = $proxy_string\n";
648 # Generate array based on the line content (seperator is a single or multiple space's)
649 my @settings = split(/,/, $line);
650 my ($provider, $hostname, $domain, $proxy, $wildcards, $username, $password, $enabled) = @settings;
652 # Skip entries if they are not (longer) supported.
653 next unless ($provider ~~ @providers);
655 # Skip disabled entries.
656 next unless ($enabled eq "on");
658 # Handle hostname details. Only connect the values with a dott if both are available.
659 if (($hostname) && ($domain)) {
660 print FILE
"[$hostname.$domain]\n";
662 print FILE
"[$hostname]\n";
665 print FILE
"provider = $provider\n";
669 # Handle token based auth for various providers.
670 if ($provider ~~ ["dns.lightningwirelabs.com", "entrydns.net", "regfish.com",
671 "spdns.de", "zzzz.io"] && $username eq "token") {
674 # Handle token auth for freedns.afraid.org and regfish.com.
675 } elsif ($provider ~~ ["freedns.afraid.org", "regfish.com"] && $password eq "") {
677 $password = $username;
679 # Handle keys for nsupdate
680 } elsif (($provider eq "nsupdate") && $username && $password) {
681 print FILE
"key = $username\n";
682 print FILE
"secret = $password\n";
687 # Handle keys for nsupdate.info
688 } elsif (($provider eq "nsupdate.info") && $password) {
689 print FILE
"secret = $password\n";
695 # Write auth details.
697 print FILE
"token = $password\n";
698 } elsif ($username && $password) {
699 print FILE
"username = $username\n";
700 print FILE
"password = $password\n";
703 # These providers need to be set to only use IPv4.
704 if ($provider ~~ ["freedns.afraid.org", "nsupdate.info", "opendns.com", "variomedia.de", "zoneedit.com"]) {
705 print FILE
"proto = ipv4\n";
715 # Function which generates an array (@providers) which contains the supported providers.
717 # Get supported providers.
718 open(PROVIDERS
, "/usr/bin/ddns list-providers |");
720 # Create new array to store the providers.
723 while (<PROVIDERS
>) {
726 # Remove following newlines.
729 # Add provider to the array.
730 push(@providers, $provider);