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