]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/ddns.cgi
libusb: Add comment to update nut when libusb is updated
[ipfire-2.x.git] / html / cgi-bin / ddns.cgi
CommitLineData
ac1cfefa 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
1f8fccc5 5# Copyright (C) 2007-2014 IPFire Team <info@ipfire.org> #
70df8302
MT
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###############################################################################
ac1cfefa
MT
21
22use strict;
5653e551 23use experimental 'smartmatch';
ac1cfefa
MT
24
25# enable only the following on debugging purpose
26#use warnings;
27#use CGI::Carp 'fatalsToBrowser';
28
986e08d9 29require '/var/ipfire/general-functions.pl';
ac1cfefa
MT
30require "${General::swroot}/lang.pl";
31require "${General::swroot}/header.pl";
4363f099 32require "${General::swroot}/http-client-functions.pl";
ac1cfefa
MT
33
34#workaround to suppress a warning when a variable is used only once
35my @dummy = ( ${Header::table2colour}, ${Header::colouryellow} );
36undef (@dummy);
37
f2fdd0c1
CS
38my %color = ();
39my %mainsettings = ();
40&General::readhash("${General::swroot}/main/settings", \%mainsettings);
8186b372 41&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
f2fdd0c1 42
1f8fccc5
SS
43# Config file for basic configuration.
44my $settingsfile = "${General::swroot}/ddns/settings";
45
46# Config file to store the configured ddns providers.
47my $datafile = "${General::swroot}/ddns/config";
48
02391903
SS
49# Call the ddnsctrl helper binary to perform the update.
50my @ddnsprog = ("/usr/local/bin/ddnsctrl", "update-all");
ac1cfefa
MT
51
52my %settings=();
1f8fccc5
SS
53my $errormessage = '';
54
55# DDNS General settings.
ac1cfefa 56$settings{'BEHINDROUTER'} = 'RED_IP';
ac1cfefa 57
1f8fccc5 58# Account settings.
ac1cfefa
MT
59$settings{'HOSTNAME'} = '';
60$settings{'DOMAIN'} = '';
61$settings{'LOGIN'} = '';
62$settings{'PASSWORD'} = '';
0562cdb3 63$settings{'TOKEN'} = '';
ac1cfefa
MT
64$settings{'ENABLED'} = '';
65$settings{'PROXY'} = '';
ac1cfefa
MT
66$settings{'SERVICE'} = '';
67
1f8fccc5 68$settings{'ACTION'} = '';
ac1cfefa 69
0562cdb3
SS
70# Get all supported ddns providers.
71my @providers = &GetProviders("all");
72
73# Get provider which support a token based authentication mechanism.
74my @token_provider = &GetProviders("token-providers");
e611222f
AF
75
76# Hook to regenerate the configuration files, if cgi got called from command line.
77if ($ENV{"REMOTE_ADDR"} eq "") {
78 &GenerateDDNSConfigFile();
79 exit(0);
80}
81
ac1cfefa
MT
82&Header::showhttpheaders();
83
84#Get GUI values
85&Header::getcgihash(\%settings);
86
1f8fccc5
SS
87# Read configuration file.
88open(FILE, "$datafile") or die "Unable to open $datafile.";
89my @current = <FILE>;
90close (FILE);
91
ac1cfefa 92#
1f8fccc5 93# Save General Settings.
ac1cfefa
MT
94#
95if ($settings{'ACTION'} eq $Lang::tr{'save'}) {
1f8fccc5
SS
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
1f8fccc5
SS
110 # Update ddns config file.
111 &GenerateDDNSConfigFile();
ac1cfefa
MT
112}
113
ac1cfefa
MT
114#
115# Toggle enable/disable field. Field is in second position
1f8fccc5 116#
ac1cfefa 117if ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
1f8fccc5
SS
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) {
1f8fccc5
SS
129 # Remove newlines.
130 chomp($line);
131
132 if ($settings{'ID'} eq $id) {
1f8fccc5
SS
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 '') {
1f8fccc5
SS
138 # Update ENABLED.
139 print FILE "$temp[0],$temp[1],$temp[2],$temp[3],$temp[4],$temp[5],$temp[6],$settings{'ENABLED'}\n";
1f8fccc5
SS
140 }
141 } else {
1f8fccc5
SS
142 # Print unmodified line.
143 print FILE "$line\n";
144 }
145
146 # Increase $id.
147 $id++;
ac1cfefa 148 }
a6df8026 149 undef $settings{'ID'};
1f8fccc5
SS
150
151 # Close file after writing.
152 close(FILE);
153
1f8fccc5
SS
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#
164if (($settings{'ACTION'} eq $Lang::tr{'add'}) || ($settings{'ACTION'} eq $Lang::tr{'update'})) {
1f8fccc5
SS
165 # Check if a hostname has been given.
166 if ($settings{'HOSTNAME'} eq '') {
167 $errormessage = $Lang::tr{'hostname not set'};
ac1cfefa 168 }
1f8fccc5
SS
169
170 # Check if a valid domainname has been provided.
171 if (!&General::validdomainname($settings{'HOSTNAME'})) {
172 $errormessage = $Lang::tr{'invalid domain name'};
ac1cfefa 173 }
ac1cfefa 174
1d32c50e
SS
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 }
1f8fccc5 181
1d32c50e
SS
182 # Automatically set the username to token.
183 $settings{'LOGIN'} = "token";
1f8fccc5 184
1d32c50e
SS
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 }
0562cdb3
SS
197 }
198
1f8fccc5 199 # Go furter if there was no error.
c330d115 200 if (!$errormessage) {
1f8fccc5
SS
201 # Splitt hostname field into 2 parts for storrage.
202 my($hostname, $domain) = split(/\./, $settings{'HOSTNAME'}, 2);
203
c2f80e67
SS
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
0562cdb3
SS
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
1f8fccc5
SS
220 # Handle adding new accounts.
221 if ($settings{'ACTION'} eq $Lang::tr{'add'}) {
1f8fccc5
SS
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
1f8fccc5
SS
237 # Handle account edditing.
238 } elsif ($settings{'ACTION'} eq $Lang::tr{'update'}) {
1f8fccc5
SS
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) {
1f8fccc5
SS
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 }
0562cdb3
SS
265 # Clear settings hash.
266 %settings = '';
1f8fccc5
SS
267
268 # Update ddns config file.
269 &GenerateDDNSConfigFile();
ac1cfefa 270 }
1f8fccc5 271}
ac1cfefa 272
1f8fccc5
SS
273#
274# Remove existing accounts.
275#
276if ($settings{'ACTION'} eq $Lang::tr{'remove'}) {
1f8fccc5
SS
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) {
1f8fccc5
SS
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++;
ac1cfefa 295 }
a6df8026 296 undef $settings{'ID'};
1f8fccc5
SS
297
298 # Close file after writing.
299 close(FILE);
300
1f8fccc5
SS
301 # Write out notice to logfile.
302 &General::log($Lang::tr{'ddns hostname removed'});
303
304 # Update ddns config file.
305 &GenerateDDNSConfigFile();
ac1cfefa
MT
306}
307
1f8fccc5
SS
308#
309# Read items for editing.
310#
ac1cfefa 311if ($settings{'ACTION'} eq $Lang::tr{'edit'}) {
1f8fccc5
SS
312 my $id = 0;
313 my @temp;
314
315 # Read file line by line.
316 foreach my $line (@current) {
1f8fccc5 317 if ($settings{'ID'} eq $id) {
1f8fccc5
SS
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
4851bc81
SS
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
1f8fccc5 333 $settings{'SERVICE'} = $temp[0];
4851bc81 334 $settings{'HOSTNAME'} = $hostname;
1f8fccc5
SS
335 $settings{'PROXY'} = $temp[3];
336 $settings{'WILDCARDS'} = $temp[4];
337 $settings{'LOGIN'} = $temp[5];
338 $settings{'PASSWORD'} = $temp[6];
0562cdb3 339 $settings{'TOKEN'} = $temp[6];
1f8fccc5
SS
340 $settings{'ENABLED'} = $temp[7];
341 }
1f8fccc5 342
c330d115
MT
343 # Increase $id.
344 $id++;
1f8fccc5 345 }
c330d115
MT
346
347 &GenerateDDNSConfigFile();
ac1cfefa
MT
348}
349
1f8fccc5
SS
350#
351# Handle forced updates.
352#
ac1cfefa 353if ($settings{'ACTION'} eq $Lang::tr{'instant update'}) {
ea9ad05e 354 &General::system(@ddnsprog) == 0 or die "@ddnsprog failed: $?\n";
ac1cfefa
MT
355}
356
1f8fccc5
SS
357#
358# Set default values.
359#
a6df8026 360if (!$settings{'ACTION'}) {
1f8fccc5
SS
361 $settings{'SERVICE'} = 'dyndns.org';
362 $settings{'ENABLED'} = 'on';
a6df8026 363 $settings{'ID'} = '';
ac1cfefa
MT
364}
365
366&Header::openpage($Lang::tr{'dynamic dns'}, 1, '');
0562cdb3
SS
367
368### Java Script ###
369print"<script>\n";
370
371# Generate Java Script Array which contains the provider that support token.
372my $line = "";
373$line = join("', '", @token_provider);
374
375print "\t// Array which contains the providers that support token.\n";
376print "\ttoken_provider = ['$line']\;\n\n";
377
378print <<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>
416END
417;
418
ac1cfefa
MT
419&Header::openbigbox('100%', 'left', '', $errormessage);
420
1f8fccc5
SS
421# Read file for general ddns settings.
422&General::readhash($settingsfile, \%settings);
ac1cfefa 423
1f8fccc5 424my %checked =();
ac1cfefa
MT
425$checked{'BEHINDROUTER'}{'RED_IP'} = '';
426$checked{'BEHINDROUTER'}{'FETCH_IP'} = '';
427$checked{'BEHINDROUTER'}{$settings{'BEHINDROUTER'}} = "checked='checked'";
ac1cfefa 428
c2f80e67
SS
429$checked{'ENABLED'}{'on'} = '';
430$checked{'ENABLED'}{'off'} = '';
431$checked{'ENABLED'}{$settings{'ENABLED'}} = "checked='checked'";
ac1cfefa 432
1f8fccc5 433# Show box for errormessages..
ac1cfefa
MT
434if ($errormessage) {
435 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
436 print "<font class='base'>$errormessage&nbsp;</font>";
437 &Header::closebox();
438}
439
ac1cfefa 440&Header::openbox('100%', 'left', $Lang::tr{'settings'});
1f8fccc5
SS
441
442##
443# Section for general ddns setup.
ac1cfefa 444print <<END
1f8fccc5 445<form method='post' action='$ENV{'SCRIPT_NAME'}'>
ac1cfefa 446<table width='100%'>
1f8fccc5
SS
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>
ac1cfefa 458</table>
1f8fccc5
SS
459<br />
460<hr />
ac1cfefa 461
ac1cfefa 462<table width='100%'>
1f8fccc5
SS
463 <tr>
464 <td align='right' valign='top' class='base'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
465 </tr>
ac1cfefa
MT
466</table>
467</form>
468END
469;
ac1cfefa 470
1f8fccc5
SS
471&Header::closebox();
472
473##
474# Section to add or edit an existing entry.
ac1cfefa 475
1f8fccc5 476# Default is add.
ac1cfefa 477my $buttontext = $Lang::tr{'add'};
1f8fccc5
SS
478
479# Change buttontext and headline if we edit an account.
480if ($settings{'ACTION'} eq $Lang::tr{'edit'}) {
1f8fccc5
SS
481 # Rename button and print headline for updating.
482 $buttontext = $Lang::tr{'update'};
483 &Header::openbox('100%', 'left', $Lang::tr{'edit an existing host'});
ac1cfefa 484} else {
1f8fccc5
SS
485 # Otherwise use default button text and show headline for adding a new account.
486 &Header::openbox('100%', 'left', $Lang::tr{'add a host'});
ac1cfefa
MT
487}
488
ac1cfefa
MT
489print <<END
490<form method='post' action='$ENV{'SCRIPT_NAME'}'>
1f8fccc5 491<input type='hidden' name='ID' value='$settings{'ID'}' />
ac1cfefa 492<table width='100%'>
1f8fccc5
SS
493 <tr>
494 <td width='25%' class='base'>$Lang::tr{'service'}:</td>
495 <td width='25%'>
496END
497;
498 # Generate dropdown menu for service selection.
0562cdb3 499 print"<select size='1' name='SERVICE' id='SERVICE'>\n";
1f8fccc5
SS
500
501 my $selected;
502
503 # Loop to print the providerlist.
504 foreach my $provider (@providers) {
1f8fccc5
SS
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";
517print <<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>
c61ac6e8 523 <td class='base'>$Lang::tr{'enabled'}</td>
c2f80e67 524 <td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
0562cdb3
SS
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>
1f8fccc5
SS
531 </tr>
532
0562cdb3 533 <tr class='password'>
1f8fccc5
SS
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>
ac1cfefa 539</table>
ec5a7d21 540<br>
1f8fccc5
SS
541<hr>
542
ac1cfefa
MT
543<table width='100%'>
544<tr>
ec5a7d21 545 <td width='30%' align='right' class='base'>
1f8fccc5
SS
546 <input type='hidden' name='ACTION' value='$buttontext'>
547 <input type='submit' name='SUBMIT' value='$buttontext'></td>
ac1cfefa
MT
548</tr>
549</table>
550</form>
551END
552;
553&Header::closebox();
554
1f8fccc5
SS
555##
556# Third section, display all created ddns hosts.
9a33b04f
MT
557# Re-open file to get changes.
558open(FILE, $datafile) or die "Unable to open $datafile.";
559@current = <FILE>;
560close(FILE);
1f8fccc5 561
9a33b04f 562# Get IP address of the red interface.
4363f099 563my $ip = &HTTPClient::GetDyndnsRedIP();
9a33b04f
MT
564my $id = 0;
565my $toggle_enabled;
566
567if (@current) {
568 &Header::openbox('100%', 'left', $Lang::tr{'current hosts'});
569
570 print <<END;
09eb51c9 571<table width='100%' class='tbl'>
1f8fccc5 572 <tr>
4188fbf1
SS
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>
1f8fccc5
SS
575 <th width='20%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
576 </tr>
ac1cfefa 577END
1f8fccc5 578
9a33b04f
MT
579 foreach my $line (@current) {
580 # Remove newlines.
581 chomp(@current);
582 my @temp = split(/\,/,$line);
583
1f080b34
SS
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
9a33b04f 593 # Generate value for enable/disable checkbox.
1f080b34 594 my $sync = '';
9a33b04f
MT
595 my $gif = '';
596 my $gdesc = '';
597
598 if ($temp[7] eq "on") {
599 $gif = 'on.gif';
600 $gdesc = $Lang::tr{'click to disable'};
1f080b34
SS
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
9a33b04f
MT
607 $toggle_enabled = 'off';
608 } else {
1f080b34 609 $sync = "<font color='blue'>";
9a33b04f
MT
610 $gif = 'off.gif';
611 $gdesc = $Lang::tr{'click to enable'};
612 $toggle_enabled = 'on';
613 }
1f8fccc5 614
9a33b04f
MT
615 # Background color.
616 my $col="";
ac1cfefa 617
9a33b04f
MT
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 }
1f8fccc5 627
4851bc81
SS
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
9a33b04f
MT
637 # The following HTML Code still is part of the loop.
638 print <<END;
1f8fccc5
SS
639<tr>
640 <td align='center' $col><a href='http://$temp[0]'>$temp[0]</a></td>
4851bc81 641 <td align='center' $col>$sync$hostname</td>
1f8fccc5 642
1f8fccc5
SS
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>
ac1cfefa
MT
661</tr>
662END
9a33b04f
MT
663 $id++;
664 }
ac1cfefa 665
9a33b04f
MT
666 print <<END;
667</table>
ac1cfefa 668<table width='100%'>
1f8fccc5
SS
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>
ac1cfefa
MT
686</table>
687END
9a33b04f
MT
688
689 &Header::closebox();
ac1cfefa
MT
690}
691
ac1cfefa
MT
692&Header::closebigbox();
693&Header::closepage();
694
1f8fccc5
SS
695# Function to generate the required configuration file for the DDNS tool.
696sub 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 = $_;
458064c5 731 chomp($line);
1f8fccc5
SS
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.
06fb88bc 738 next unless ($provider ~~ @providers);
1f8fccc5
SS
739
740 # Skip disabled entries.
458064c5 741 next unless ($enabled eq "on");
1f8fccc5 742
4851bc81
SS
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
1f8fccc5
SS
750 print FILE "provider = $provider\n";
751
752 my $use_token = 0;
753
0562cdb3
SS
754 # Handle token based auth for various providers.
755 if ($provider ~~ @token_provider) {
1f8fccc5 756 $use_token = 1;
00e1105b 757 }
1f8fccc5 758
5fe185f8 759 # Handle token auth for freedns.afraid.org and regfish.com.
00e1105b 760 if ($provider ~~ ["freedns.afraid.org", "regfish.com"] && $password eq "") {
1f8fccc5
SS
761 $use_token = 1;
762 $password = $username;
3c781f63
MT
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 = "";
1f8fccc5 771
f5770b46
SS
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
1f8fccc5
SS
780 # Write auth details.
781 if ($use_token) {
782 print FILE "token = $password\n";
3c781f63 783 } elsif ($username && $password) {
1f8fccc5
SS
784 print FILE "username = $username\n";
785 print FILE "password = $password\n";
786 }
787
1f8fccc5
SS
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.
0562cdb3
SS
796sub 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 }
1f8fccc5
SS
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 }
ac1cfefa 823
1f8fccc5 824 close(PROVIDERS);
ac1cfefa 825
1f8fccc5
SS
826 # Return our array.
827 return @providers;
ac1cfefa 828}