]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ddns.cgi
b3f6e3699cde135a026861c1db863e7588968442
[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='20%' align='center' class='boldbase'><b>$Lang::tr{'service'}</b></th>
490 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'hostname'}</b></th>
491 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'domain'}</b></th>
492 <th width='20%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
493 </tr>
494 END
495 ;
496
497 # Re-open file to get changes.
498 open(FILE, $datafile) or die "Unable to open $datafile.";
499 @current = <FILE>;
500 close(FILE);
501
502 # Get IP address of the red interface.
503 my $ip = &General::GetDyndnsRedIP;
504 my $id = 0;
505 my $toggle_enabled;
506
507 foreach my $line (@current) {
508
509 # Remove newlines.
510 chomp(@current);
511 my @temp = split(/\,/,$line);
512
513 # Generate value for enable/disable checkbox.
514 my $sync = "<font color='blue'>";
515 my $gif = '';
516 my $gdesc = '';
517
518 if ($temp[7] eq "on") {
519 $gif = 'on.gif';
520 $gdesc = $Lang::tr{'click to disable'};
521 $sync = (&General::DyndnsServiceSync ($ip,$temp[1], $temp[2]) ? "<font color='green'>": "<font color='red'>") ;
522 $toggle_enabled = 'off';
523 } else {
524 $gif = 'off.gif';
525 $gdesc = $Lang::tr{'click to enable'};
526 $toggle_enabled = 'on';
527 }
528
529 # Background color.
530 my $col="";
531
532 if ($settings{'ID'} eq $id) {
533 $col="bgcolor='${Header::colouryellow}'";
534 } elsif (!&General::is_part_of("$temp[0]", @providers)) {
535 $col="bgcolor='#FF4D4D'";
536 } elsif ($id % 2) {
537 $col="bgcolor='$color{'color20'}'";
538 } else {
539 $col="bgcolor='$color{'color22'}'";
540 }
541
542 # The following HTML Code still is part of the loop.
543 print <<END
544 <tr>
545 <td align='center' $col><a href='http://$temp[0]'>$temp[0]</a></td>
546 <td align='center' $col>$sync$temp[1]</td>
547 <td align='center' $col>$sync$temp[2]</td>
548
549 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
550 <input type='hidden' name='ID' value='$id'>
551 <input type='hidden' name='ENABLED' value='$toggle_enabled'>
552 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
553 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
554 </form></td>
555
556 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
557 <input type='hidden' name='ID' value='$id'>
558 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
559 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
560 </form></td>
561
562 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
563 <input type='hidden' name='ID' value='$id'>
564 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
565 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
566 </form></td>
567 </tr>
568 END
569 ;
570 $id++;
571 }
572 print "</table>";
573
574 # If table contains entries, print 'Key to action icons'
575 if ($id) {
576 print <<END
577 <table width='100%'>
578 <tr>
579 <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>
580 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
581 <td class='base'>$Lang::tr{'click to disable'}</td>
582 <td>&nbsp;&nbsp;</td>
583 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
584 <td class='base'>$Lang::tr{'click to enable'}</td>
585 <td>&nbsp;&nbsp;</td>
586 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
587 <td class='base'>$Lang::tr{'edit'}</td>
588 <td>&nbsp;&nbsp;</td>
589 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
590 <td class='base'>$Lang::tr{'remove'}</td>
591 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
592 <td align='right' width='30%'><input type='submit' name='ACTION' value='$Lang::tr{'instant update'}' /></td>
593 </form>
594 </tr>
595 </table>
596 END
597 ;
598 }
599
600 &Header::closebox();
601 &Header::closebigbox();
602 &Header::closepage();
603
604 # Function to generate the required configuration file for the DDNS tool.
605 sub GenerateDDNSConfigFile {
606 # Open datafile file
607 open(SETTINGS, "<$datafile") or die "Could not open $datafile.";
608
609 open(FILE, ">${General::swroot}/ddns/ddns.conf");
610
611 # Global configuration options.
612 print FILE "[config]\n";
613
614 # Check if we guess our IP address by an extranal server.
615 if ($settings{'BEHINDROUTER'} eq "FETCH_IP") {
616 print FILE "guess_external_ip = true\n";
617 } else {
618 print FILE "guess_external_ip = false\n";
619 }
620
621 # Use an upstream proxy and generate proxy url.
622 my %proxysettings;
623 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
624 if ($proxysettings{'UPSTREAM_PROXY'}) {
625 my $proxy_string = "http://";
626
627 if ($proxysettings{'UPSTREAM_USER'} && $proxysettings{'UPSTREAM_PASSWORD'}) {
628 $proxy_string .= "$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@";
629 }
630
631 $proxy_string .= $proxysettings{'UPSTREAM_PROXY'};
632
633 print FILE "proxy = $proxy_string\n";
634 }
635
636 print FILE "\n";
637
638 while (<SETTINGS>) {
639 my $line = $_;
640
641 # Generate array based on the line content (seperator is a single or multiple space's)
642 my @settings = split(/,/, $line);
643 my ($provider, $hostname, $domain, $proxy, $wildcards, $username, $password, $enabled) = @settings;
644
645 # Skip entries if they are not (longer) supported.
646 next if (!&General::is_part_of("$provider", @providers));
647
648 # Skip disabled entries.
649 next if ($enabled eq "off");
650
651 print FILE "[$hostname.$domain]\n";
652 print FILE "provider = $provider\n";
653
654 my $use_token = 0;
655
656 # Handle token based auth for various providers.
657 if ($provider ~~ ["dns.lightningwirelabs.com", "regfish.com"] && $username eq "token") {
658 $use_token = 1;
659
660 # Handle token auth for freedns.afraid.org.
661 } elsif ($provider eq "freedns.afraid.org" && $password eq "") {
662 $use_token = 1;
663 $password = $username;
664 }
665
666 # Write auth details.
667 if ($use_token) {
668 print FILE "token = $password\n";
669 } else {
670 print FILE "username = $username\n";
671 print FILE "password = $password\n";
672 }
673
674 # These providers need to be set to only use IPv4.
675 if ($provider ~~ ["freedns.afraid.org", "variomedia.de", "zoneedit.com"]) {
676 print FILE "proto = ipv4\n";
677 }
678
679 print FILE "\n";
680 }
681
682 close(SETTINGS);
683 close(FILE);
684 }
685
686 # Function which generates an array (@providers) which contains the supported providers.
687 sub GetProviders {
688 # Get supported providers.
689 open(PROVIDERS, "/usr/bin/ddns list-providers |");
690
691 # Create new array to store the providers.
692 my @providers = ();
693
694 while (<PROVIDERS>) {
695 my $provider = $_;
696
697 # Remove following newlines.
698 chomp($provider);
699
700 # Add provider to the array.
701 push(@providers, $provider);
702 }
703
704 close(PROVIDERS);
705
706 # Return our array.
707 return @providers;
708 }