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