]> git.ipfire.org Git - ipfire-2.x.git/blame_incremental - html/cgi-bin/ddns.cgi
ddns.cgi: Move enabled checkbox a bit up.
[ipfire-2.x.git] / html / cgi-bin / ddns.cgi
... / ...
CommitLineData
1#!/usr/bin/perl
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2007-2014 IPFire Team <info@ipfire.org> #
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
21
22use strict;
23
24# enable only the following on debugging purpose
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
27
28require '/var/ipfire/general-functions.pl';
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
31
32# Hook to regenerate the configuration files, if cgi got called from command line.
33if ($ENV{"REMOTE_ADDR"} eq "") {
34 &GenerateDDNSConfigFile();
35 exit(0);
36}
37
38#workaround to suppress a warning when a variable is used only once
39my @dummy = ( ${Header::table2colour}, ${Header::colouryellow} );
40undef (@dummy);
41
42my %color = ();
43my %mainsettings = ();
44&General::readhash("${General::swroot}/main/settings", \%mainsettings);
45&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
46
47# Config file for basic configuration.
48my $settingsfile = "${General::swroot}/ddns/settings";
49
50# Config file to store the configured ddns providers.
51my $datafile = "${General::swroot}/ddns/config";
52
53# Dynamic ddns programm call.
54my @ddnsprog = ("/usr/bin/ddns", "--config",
55 "/var/ipfire/ddns/ddns.conf",
56 "update-all", "--force" );
57
58my %settings=();
59my $errormessage = '';
60
61# DDNS General settings.
62$settings{'BEHINDROUTER'} = 'RED_IP';
63
64# Account settings.
65$settings{'HOSTNAME'} = '';
66$settings{'DOMAIN'} = '';
67$settings{'LOGIN'} = '';
68$settings{'PASSWORD'} = '';
69$settings{'ENABLED'} = '';
70$settings{'PROXY'} = '';
71$settings{'SERVICE'} = '';
72
73$settings{'ACTION'} = '';
74
75&Header::showhttpheaders();
76
77#Get GUI values
78&Header::getcgihash(\%settings);
79
80# Read configuration file.
81open(FILE, "$datafile") or die "Unable to open $datafile.";
82my @current = <FILE>;
83close (FILE);
84
85# Get supported ddns providers.
86my @providers = &GetProviders();
87
88#
89# Save General Settings.
90#
91if ($settings{'ACTION'} eq $Lang::tr{'save'}) {
92
93 # Open /var/ipfire/ddns/settings for writing.
94 open(FILE, ">$settingsfile") or die "Unable to open $settingsfile.";
95
96 # Lock file for writing.
97 flock FILE, 2;
98
99 # Check if BEHINDROUTER has been configured.
100 if ($settings{'BEHINDROUTER'} ne '') {
101 print FILE "BEHINDROUTER=$settings{'BEHINDROUTER'}\n";
102 }
103
104 # Close file after writing.
105 close(FILE);
106
107 # Unset given CGI parmas.
108 undef %settings;
109
110 # Update ddns config file.
111 &GenerateDDNSConfigFile();
112}
113
114#
115# Toggle enable/disable field. Field is in second position
116#
117if ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
118
119 # Open /var/ipfire/ddns/config for writing.
120 open(FILE, ">$datafile") or die "Unable to open $datafile.";
121
122 # Lock file for writing.
123 flock FILE, 2;
124
125 my @temp;
126 my $id = 0;
127
128 # Read file line by line.
129 foreach my $line (@current) {
130
131 # Remove newlines.
132 chomp($line);
133
134 if ($settings{'ID'} eq $id) {
135
136 # Splitt lines (splitting element is a single ",") and save values into temp array.
137 @temp = split(/\,/,$line);
138
139 # Check if we want to toggle ENABLED or WILDCARDS.
140 if ($settings{'ENABLED'} ne '') {
141
142 # Update ENABLED.
143 print FILE "$temp[0],$temp[1],$temp[2],$temp[3],$temp[4],$temp[5],$temp[6],$settings{'ENABLED'}\n";
144 }
145 } else {
146
147 # Print unmodified line.
148 print FILE "$line\n";
149 }
150
151 # Increase $id.
152 $id++;
153 }
154
155 # Close file after writing.
156 close(FILE);
157
158 # Unset given CGI params.
159 undef %settings;
160
161 # Write out logging notice.
162 &General::log($Lang::tr{'ddns hostname modified'});
163
164 # Update ddns config file.
165 &GenerateDDNSConfigFile();
166}
167
168#
169# Add new accounts, or edit existing ones.
170#
171if (($settings{'ACTION'} eq $Lang::tr{'add'}) || ($settings{'ACTION'} eq $Lang::tr{'update'})) {
172
173 # Check if a hostname has been given.
174 if ($settings{'HOSTNAME'} eq '') {
175 $errormessage = $Lang::tr{'hostname not set'};
176 }
177
178 # Check if a valid domainname has been provided.
179 if (!&General::validdomainname($settings{'HOSTNAME'})) {
180 $errormessage = $Lang::tr{'invalid domain name'};
181 }
182
183 # Check if a username has been sent.
184 if ($settings{'LOGIN'} eq '') {
185 $errormessage = $Lang::tr{'username not set'};
186 }
187
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'};
192 }
193
194 # Go furter if there was no error.
195 if ( ! $errormessage) {
196
197 # Splitt hostname field into 2 parts for storrage.
198 my($hostname, $domain) = split(/\./, $settings{'HOSTNAME'}, 2);
199
200 # Handle adding new accounts.
201 if ($settings{'ACTION'} eq $Lang::tr{'add'}) {
202
203 # Open /var/ipfire/ddns/config for writing.
204 open(FILE, ">>$datafile") or die "Unable to open $datafile.";
205
206 # Lock file for writing.
207 flock FILE, 2;
208
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";
211
212 # Close file after writing.
213 close(FILE);
214
215 # Write out notice to logfile.
216 &General::log($Lang::tr{'ddns hostname added'});
217
218 # Update ddns config file.
219
220 # Handle account edditing.
221 } elsif ($settings{'ACTION'} eq $Lang::tr{'update'}) {
222
223 # Open /var/ipfire/ddns/config for writing.
224 open(FILE, ">$datafile") or die "Unable to open $datafile.";
225
226 # Lock file for writing.
227 flock FILE, 2;
228
229 my $id = 0;
230
231 # Read file line by line.
232 foreach my $line (@current) {
233
234 if ($settings{'ID'} eq $id) {
235 print FILE "$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
236 } else {
237 print FILE "$line";
238 }
239
240 # Increase $id.
241 $id++;
242 }
243
244 # Close file after writing.
245 close(FILE);
246
247 # Write out notice to logfile.
248 &General::log($Lang::tr{'ddns hostname modified'});
249 }
250
251 # Unset given CGI params.
252 undef %settings;
253
254 # Update ddns config file.
255 &GenerateDDNSConfigFile();
256 }
257}
258
259#
260# Remove existing accounts.
261#
262if ($settings{'ACTION'} eq $Lang::tr{'remove'}) {
263
264 # Open /var/ipfire/ddns/config for writing.
265 open(FILE, ">$datafile") or die "Unable to open $datafile.";
266
267 # Lock file for writing.
268 flock FILE, 2;
269
270 my $id = 0;
271
272 # Read file line by line.
273 foreach my $line (@current) {
274
275 # Write back every line, except the one we want to drop
276 # (identified by the ID)
277 unless ($settings{'ID'} eq $id) {
278 print FILE "$line";
279 }
280
281 # Increase id.
282 $id++;
283 }
284
285 # Close file after writing.
286 close(FILE);
287
288 # Unset given CGI params.
289 undef %settings;
290
291 # Write out notice to logfile.
292 &General::log($Lang::tr{'ddns hostname removed'});
293
294 # Update ddns config file.
295 &GenerateDDNSConfigFile();
296}
297
298#
299# Read items for editing.
300#
301if ($settings{'ACTION'} eq $Lang::tr{'edit'}) {
302
303 my $id = 0;
304 my @temp;
305
306 # Read file line by line.
307 foreach my $line (@current) {
308
309 if ($settings{'ID'} eq $id) {
310
311 # Remove newlines.
312 chomp($line);
313
314 # Splitt lines (splitting element is a single ",") and save values into temp array.
315 @temp = split(/\,/,$line);
316
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];
324 }
325 # Increase $id.
326 $id++;
327
328 }
329}
330
331#
332# Handle forced updates.
333#
334if ($settings{'ACTION'} eq $Lang::tr{'instant update'}) {
335 system(@ddnsprog) == 0 or die "@ddnsprog failed: $?\n";
336}
337
338#
339# Set default values.
340#
341if (! $settings{'ACTION'}) {
342 $settings{'SERVICE'} = 'dyndns.org';
343 $settings{'ENABLED'} = 'on';
344}
345
346&Header::openpage($Lang::tr{'dynamic dns'}, 1, '');
347&Header::openbigbox('100%', 'left', '', $errormessage);
348
349# Read file for general ddns settings.
350&General::readhash($settingsfile, \%settings);
351
352my %checked =();
353$checked{'BEHINDROUTER'}{'RED_IP'} = '';
354$checked{'BEHINDROUTER'}{'FETCH_IP'} = '';
355$checked{'BEHINDROUTER'}{$settings{'BEHINDROUTER'}} = "checked='checked'";
356
357$checked{'ENABLED'}{'on'} = ($settings{'ENABLED'} eq '' ) ? '' : "checked='checked'";
358
359# Show box for errormessages..
360if ($errormessage) {
361 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
362 print "<font class='base'>$errormessage&nbsp;</font>";
363 &Header::closebox();
364}
365
366&Header::openbox('100%', 'left', $Lang::tr{'settings'});
367
368##
369# Section for general ddns setup.
370print <<END
371<form method='post' action='$ENV{'SCRIPT_NAME'}'>
372<table width='100%'>
373 <tr>
374 <td class='base'>$Lang::tr{'dyn dns source choice'}</td>
375 </tr>
376 <tr>
377 <td class='base'><input type='radio' name='BEHINDROUTER' value='RED_IP' $checked{'BEHINDROUTER'}{'RED_IP'} />
378 $Lang::tr{'use ipfire red ip'}</td>
379 </tr>
380 <tr>
381 <td class='base'><input type='radio' name='BEHINDROUTER' value='FETCH_IP' $checked{'BEHINDROUTER'}{'FETCH_IP'} />
382 $Lang::tr{'fetch ip from'}</td>
383 </tr>
384</table>
385<br />
386<hr />
387
388<table width='100%'>
389 <tr>
390 <td align='right' valign='top' class='base'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
391 </tr>
392</table>
393</form>
394END
395;
396
397&Header::closebox();
398
399##
400# Section to add or edit an existing entry.
401
402# Default is add.
403my $buttontext = $Lang::tr{'add'};
404
405# Change buttontext and headline if we edit an account.
406if ($settings{'ACTION'} eq $Lang::tr{'edit'}) {
407
408 # Rename button and print headline for updating.
409 $buttontext = $Lang::tr{'update'};
410 &Header::openbox('100%', 'left', $Lang::tr{'edit an existing host'});
411} else {
412
413 # Otherwise use default button text and show headline for adding a new account.
414 &Header::openbox('100%', 'left', $Lang::tr{'add a host'});
415}
416
417print <<END
418
419<form method='post' action='$ENV{'SCRIPT_NAME'}'>
420<input type='hidden' name='ID' value='$settings{'ID'}' />
421<table width='100%'>
422 <tr>
423 <td width='25%' class='base'>$Lang::tr{'service'}:</td>
424 <td width='25%'>
425END
426;
427 # Generate dropdown menu for service selection.
428 print"<select size='1' name='SERVICE'>\n";
429
430 my $selected;
431
432 # Loop to print the providerlist.
433 foreach my $provider (@providers) {
434
435 # Check if the current provider needs to be selected.
436 if ($provider eq $settings{'SERVICE'}) {
437 $selected = 'selected';
438 } else {
439 $selected = "";
440 }
441
442 # Print out the HTML option field.
443 print "<option value=\"$provider\" $selected>$provider</option>\n";
444 }
445
446 print"</select></td>\n";
447print <<END
448 <td width='20%' class='base'>$Lang::tr{'hostname'}:</td>
449 <td width='30%'><input type='text' name='HOSTNAME' value='$settings{'HOSTNAME'}' /></td>
450 </tr>
451
452 <tr>
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>
457 </tr>
458
459 <tr>
460 <td class='base'></td>
461 <td></td>
462 <td class='base'>$Lang::tr{'password'}</td>
463 <td><input type='password' name='PASSWORD' value='$settings{'PASSWORD'}' /></td>
464 </tr>
465</table>
466<br>
467<hr>
468
469<table width='100%'>
470<tr>
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>
474</tr>
475</table>
476</form>
477END
478;
479&Header::closebox();
480
481##
482# Third section, display all created ddns hosts.
483
484&Header::openbox('100%', 'left', $Lang::tr{'current hosts'});
485print <<END
486<table width='100%' class='tbl'>
487 <tr>
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>
491 </tr>
492END
493;
494
495# Re-open file to get changes.
496open(FILE, $datafile) or die "Unable to open $datafile.";
497@current = <FILE>;
498close(FILE);
499
500# Get IP address of the red interface.
501my $ip = &General::GetDyndnsRedIP;
502my $id = 0;
503my $toggle_enabled;
504
505foreach my $line (@current) {
506
507 # Remove newlines.
508 chomp(@current);
509 my @temp = split(/\,/,$line);
510
511 # Generate value for enable/disable checkbox.
512 my $sync = "<font color='blue'>";
513 my $gif = '';
514 my $gdesc = '';
515
516 if ($temp[7] eq "on") {
517 $gif = 'on.gif';
518 $gdesc = $Lang::tr{'click to disable'};
519 $sync = (&General::DyndnsServiceSync ($ip,$temp[1], $temp[2]) ? "<font color='green'>": "<font color='red'>") ;
520 $toggle_enabled = 'off';
521 } else {
522 $gif = 'off.gif';
523 $gdesc = $Lang::tr{'click to enable'};
524 $toggle_enabled = 'on';
525 }
526
527 # Background color.
528 my $col="";
529
530 if ($settings{'ID'} eq $id) {
531 $col="bgcolor='${Header::colouryellow}'";
532 } elsif (!&General::is_part_of("$temp[0]", @providers)) {
533 $col="bgcolor='#FF4D4D'";
534 } elsif ($id % 2) {
535 $col="bgcolor='$color{'color20'}'";
536 } else {
537 $col="bgcolor='$color{'color22'}'";
538 }
539
540# The following HTML Code still is part of the loop.
541print <<END
542<tr>
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>
545
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' />
551 </form></td>
552
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'}' />
557 </form></td>
558
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'}' />
563 </form></td>
564</tr>
565END
566;
567 $id++;
568}
569print "</table>";
570
571# If table contains entries, print 'Key to action icons'
572if ($id) {
573print <<END
574<table width='100%'>
575 <tr>
576 <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>
577 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
578 <td class='base'>$Lang::tr{'click to disable'}</td>
579 <td>&nbsp;&nbsp;</td>
580 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
581 <td class='base'>$Lang::tr{'click to enable'}</td>
582 <td>&nbsp;&nbsp;</td>
583 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
584 <td class='base'>$Lang::tr{'edit'}</td>
585 <td>&nbsp;&nbsp;</td>
586 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
587 <td class='base'>$Lang::tr{'remove'}</td>
588 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
589 <td align='right' width='30%'><input type='submit' name='ACTION' value='$Lang::tr{'instant update'}' /></td>
590 </form>
591 </tr>
592</table>
593END
594;
595}
596
597&Header::closebox();
598&Header::closebigbox();
599&Header::closepage();
600
601# Function to generate the required configuration file for the DDNS tool.
602sub GenerateDDNSConfigFile {
603 # Open datafile file
604 open(SETTINGS, "<$datafile") or die "Could not open $datafile.";
605
606 open(FILE, ">${General::swroot}/ddns/ddns.conf");
607
608 # Global configuration options.
609 print FILE "[config]\n";
610
611 # Check if we guess our IP address by an extranal server.
612 if ($settings{'BEHINDROUTER'} eq "FETCH_IP") {
613 print FILE "guess_external_ip = true\n";
614 } else {
615 print FILE "guess_external_ip = false\n";
616 }
617
618 # Use an upstream proxy and generate proxy url.
619 my %proxysettings;
620 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
621 if ($proxysettings{'UPSTREAM_PROXY'}) {
622 my $proxy_string = "http://";
623
624 if ($proxysettings{'UPSTREAM_USER'} && $proxysettings{'UPSTREAM_PASSWORD'}) {
625 $proxy_string .= "$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@";
626 }
627
628 $proxy_string .= $proxysettings{'UPSTREAM_PROXY'};
629
630 print FILE "proxy = $proxy_string\n";
631 }
632
633 print FILE "\n";
634
635 while (<SETTINGS>) {
636 my $line = $_;
637
638 # Generate array based on the line content (seperator is a single or multiple space's)
639 my @settings = split(/,/, $line);
640 my ($provider, $hostname, $domain, $proxy, $wildcards, $username, $password, $enabled) = @settings;
641
642 # Skip entries if they are not (longer) supported.
643 next if (!&General::is_part_of("$provider", @providers));
644
645 # Skip disabled entries.
646 next if ($enabled eq "off");
647
648 print FILE "[$hostname.$domain]\n";
649 print FILE "provider = $provider\n";
650
651 my $use_token = 0;
652
653 # Handle token based auth for various providers.
654 if ($provider ~~ ["dns.lightningwirelabs.com", "regfish.com"] && $username eq "token") {
655 $use_token = 1;
656
657 # Handle token auth for freedns.afraid.org.
658 } elsif ($provider eq "freedns.afraid.org" && $password eq "") {
659 $use_token = 1;
660 $password = $username;
661
662 # Handle keys for nsupdate
663 } elsif (($provider eq "nsupdate") && $username && $password) {
664 print FILE "key = $username\n";
665 print FILE "secret = $password\n";
666
667 $username = "";
668 $password = "";
669
670 # Handle keys for nsupdate.info
671 } elsif (($provider eq "nsupdate.info") && $password) {
672 print FILE "secret = $password\n";
673
674 $username = "";
675 $password = "";
676 }
677
678 # Write auth details.
679 if ($use_token) {
680 print FILE "token = $password\n";
681 } elsif ($username && $password) {
682 print FILE "username = $username\n";
683 print FILE "password = $password\n";
684 }
685
686 # These providers need to be set to only use IPv4.
687 if ($provider ~~ ["freedns.afraid.org", "nsupdate.info", "variomedia.de", "zoneedit.com"]) {
688 print FILE "proto = ipv4\n";
689 }
690
691 print FILE "\n";
692 }
693
694 close(SETTINGS);
695 close(FILE);
696}
697
698# Function which generates an array (@providers) which contains the supported providers.
699sub GetProviders {
700 # Get supported providers.
701 open(PROVIDERS, "/usr/bin/ddns list-providers |");
702
703 # Create new array to store the providers.
704 my @providers = ();
705
706 while (<PROVIDERS>) {
707 my $provider = $_;
708
709 # Remove following newlines.
710 chomp($provider);
711
712 # Add provider to the array.
713 push(@providers, $provider);
714 }
715
716 close(PROVIDERS);
717
718 # Return our array.
719 return @providers;
720}