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