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