]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/ddns.cgi
Merge branch 'master' into next
[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
4851bc81
SS
296 # Handle hostname details. Only connect the values with a dott if both are available.
297 my $hostname;
298
299 if (($temp[1]) && ($temp[2])) {
300 $hostname = "$temp[1].$temp[2]";
301 } else {
302 $hostname = "$temp[1]";
303 }
304
1f8fccc5 305 $settings{'SERVICE'} = $temp[0];
4851bc81 306 $settings{'HOSTNAME'} = $hostname;
1f8fccc5
SS
307 $settings{'PROXY'} = $temp[3];
308 $settings{'WILDCARDS'} = $temp[4];
309 $settings{'LOGIN'} = $temp[5];
310 $settings{'PASSWORD'} = $temp[6];
311 $settings{'ENABLED'} = $temp[7];
312 }
1f8fccc5 313
c330d115
MT
314 # Increase $id.
315 $id++;
1f8fccc5 316 }
c330d115
MT
317
318 &GenerateDDNSConfigFile();
ac1cfefa
MT
319}
320
1f8fccc5
SS
321#
322# Handle forced updates.
323#
ac1cfefa 324if ($settings{'ACTION'} eq $Lang::tr{'instant update'}) {
1f8fccc5 325 system(@ddnsprog) == 0 or die "@ddnsprog failed: $?\n";
ac1cfefa
MT
326}
327
1f8fccc5
SS
328#
329# Set default values.
330#
a6df8026 331if (!$settings{'ACTION'}) {
1f8fccc5
SS
332 $settings{'SERVICE'} = 'dyndns.org';
333 $settings{'ENABLED'} = 'on';
a6df8026 334 $settings{'ID'} = '';
ac1cfefa
MT
335}
336
337&Header::openpage($Lang::tr{'dynamic dns'}, 1, '');
338&Header::openbigbox('100%', 'left', '', $errormessage);
339
1f8fccc5
SS
340# Read file for general ddns settings.
341&General::readhash($settingsfile, \%settings);
ac1cfefa 342
1f8fccc5 343my %checked =();
ac1cfefa
MT
344$checked{'BEHINDROUTER'}{'RED_IP'} = '';
345$checked{'BEHINDROUTER'}{'FETCH_IP'} = '';
346$checked{'BEHINDROUTER'}{$settings{'BEHINDROUTER'}} = "checked='checked'";
ac1cfefa 347
c2f80e67
SS
348$checked{'ENABLED'}{'on'} = '';
349$checked{'ENABLED'}{'off'} = '';
350$checked{'ENABLED'}{$settings{'ENABLED'}} = "checked='checked'";
ac1cfefa 351
1f8fccc5 352# Show box for errormessages..
ac1cfefa
MT
353if ($errormessage) {
354 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
355 print "<font class='base'>$errormessage&nbsp;</font>";
356 &Header::closebox();
357}
358
ac1cfefa 359&Header::openbox('100%', 'left', $Lang::tr{'settings'});
1f8fccc5
SS
360
361##
362# Section for general ddns setup.
ac1cfefa 363print <<END
1f8fccc5 364<form method='post' action='$ENV{'SCRIPT_NAME'}'>
ac1cfefa 365<table width='100%'>
1f8fccc5
SS
366 <tr>
367 <td class='base'>$Lang::tr{'dyn dns source choice'}</td>
368 </tr>
369 <tr>
370 <td class='base'><input type='radio' name='BEHINDROUTER' value='RED_IP' $checked{'BEHINDROUTER'}{'RED_IP'} />
371 $Lang::tr{'use ipfire red ip'}</td>
372 </tr>
373 <tr>
374 <td class='base'><input type='radio' name='BEHINDROUTER' value='FETCH_IP' $checked{'BEHINDROUTER'}{'FETCH_IP'} />
375 $Lang::tr{'fetch ip from'}</td>
376 </tr>
ac1cfefa 377</table>
1f8fccc5
SS
378<br />
379<hr />
ac1cfefa 380
ac1cfefa 381<table width='100%'>
1f8fccc5
SS
382 <tr>
383 <td align='right' valign='top' class='base'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
384 </tr>
ac1cfefa
MT
385</table>
386</form>
387END
388;
ac1cfefa 389
1f8fccc5
SS
390&Header::closebox();
391
392##
393# Section to add or edit an existing entry.
ac1cfefa 394
1f8fccc5 395# Default is add.
ac1cfefa 396my $buttontext = $Lang::tr{'add'};
1f8fccc5
SS
397
398# Change buttontext and headline if we edit an account.
399if ($settings{'ACTION'} eq $Lang::tr{'edit'}) {
1f8fccc5
SS
400 # Rename button and print headline for updating.
401 $buttontext = $Lang::tr{'update'};
402 &Header::openbox('100%', 'left', $Lang::tr{'edit an existing host'});
ac1cfefa 403} else {
1f8fccc5
SS
404 # Otherwise use default button text and show headline for adding a new account.
405 &Header::openbox('100%', 'left', $Lang::tr{'add a host'});
ac1cfefa
MT
406}
407
ac1cfefa
MT
408print <<END
409<form method='post' action='$ENV{'SCRIPT_NAME'}'>
1f8fccc5 410<input type='hidden' name='ID' value='$settings{'ID'}' />
ac1cfefa 411<table width='100%'>
1f8fccc5
SS
412 <tr>
413 <td width='25%' class='base'>$Lang::tr{'service'}:</td>
414 <td width='25%'>
415END
416;
417 # Generate dropdown menu for service selection.
418 print"<select size='1' name='SERVICE'>\n";
419
420 my $selected;
421
422 # Loop to print the providerlist.
423 foreach my $provider (@providers) {
1f8fccc5
SS
424 # Check if the current provider needs to be selected.
425 if ($provider eq $settings{'SERVICE'}) {
426 $selected = 'selected';
427 } else {
428 $selected = "";
429 }
430
431 # Print out the HTML option field.
432 print "<option value=\"$provider\" $selected>$provider</option>\n";
433 }
434
435 print"</select></td>\n";
436print <<END
437 <td width='20%' class='base'>$Lang::tr{'hostname'}:</td>
438 <td width='30%'><input type='text' name='HOSTNAME' value='$settings{'HOSTNAME'}' /></td>
439 </tr>
440
441 <tr>
c61ac6e8 442 <td class='base'>$Lang::tr{'enabled'}</td>
c2f80e67 443 <td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
4576afc5 444 <td class='base'>$Lang::tr{'username'}</td>
1f8fccc5
SS
445 <td><input type='text' name='LOGIN' value='$settings{'LOGIN'}' /></td>
446 </tr>
447
448 <tr>
449 <td class='base'></td>
450 <td></td>
451 <td class='base'>$Lang::tr{'password'}</td>
452 <td><input type='password' name='PASSWORD' value='$settings{'PASSWORD'}' /></td>
453 </tr>
ac1cfefa 454</table>
ec5a7d21 455<br>
1f8fccc5
SS
456<hr>
457
ac1cfefa
MT
458<table width='100%'>
459<tr>
ec5a7d21 460 <td width='30%' align='right' class='base'>
1f8fccc5
SS
461 <input type='hidden' name='ACTION' value='$buttontext'>
462 <input type='submit' name='SUBMIT' value='$buttontext'></td>
ac1cfefa
MT
463</tr>
464</table>
465</form>
466END
467;
468&Header::closebox();
469
1f8fccc5
SS
470##
471# Third section, display all created ddns hosts.
9a33b04f
MT
472# Re-open file to get changes.
473open(FILE, $datafile) or die "Unable to open $datafile.";
474@current = <FILE>;
475close(FILE);
1f8fccc5 476
9a33b04f
MT
477# Get IP address of the red interface.
478my $ip = &General::GetDyndnsRedIP();
479my $id = 0;
480my $toggle_enabled;
481
482if (@current) {
483 &Header::openbox('100%', 'left', $Lang::tr{'current hosts'});
484
485 print <<END;
09eb51c9 486<table width='100%' class='tbl'>
1f8fccc5 487 <tr>
4188fbf1
SS
488 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'service'}</b></th>
489 <th width='50%' align='center' class='boldbase'><b>$Lang::tr{'hostname'}</b></th>
1f8fccc5
SS
490 <th width='20%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
491 </tr>
ac1cfefa 492END
1f8fccc5 493
9a33b04f
MT
494 foreach my $line (@current) {
495 # Remove newlines.
496 chomp(@current);
497 my @temp = split(/\,/,$line);
498
1f080b34
SS
499 # Handle hostname details. Only connect the values with a dott if both are available.
500 my $hostname="";
501
502 if (($temp[1]) && ($temp[2])) {
503 $hostname="$temp[1].$temp[2]";
504 } else {
505 $hostname="$temp[1]";
506 }
507
9a33b04f 508 # Generate value for enable/disable checkbox.
1f080b34 509 my $sync = '';
9a33b04f
MT
510 my $gif = '';
511 my $gdesc = '';
512
513 if ($temp[7] eq "on") {
514 $gif = 'on.gif';
515 $gdesc = $Lang::tr{'click to disable'};
1f080b34
SS
516
517 # Check if the given hostname is a FQDN before doing a nslookup.
518 if (&General::validfqdn($hostname)) {
519 $sync = (&General::DyndnsServiceSync ($ip,$temp[1], $temp[2]) ? "<font color='green'>": "<font color='red'>") ;
520 }
521
9a33b04f
MT
522 $toggle_enabled = 'off';
523 } else {
1f080b34 524 $sync = "<font color='blue'>";
9a33b04f
MT
525 $gif = 'off.gif';
526 $gdesc = $Lang::tr{'click to enable'};
527 $toggle_enabled = 'on';
528 }
1f8fccc5 529
9a33b04f
MT
530 # Background color.
531 my $col="";
ac1cfefa 532
9a33b04f
MT
533 if ($settings{'ID'} eq $id) {
534 $col="bgcolor='${Header::colouryellow}'";
535 } elsif (!($temp[0] ~~ @providers)) {
536 $col="bgcolor='#FF4D4D'";
537 } elsif ($id % 2) {
538 $col="bgcolor='$color{'color20'}'";
539 } else {
540 $col="bgcolor='$color{'color22'}'";
541 }
1f8fccc5 542
4851bc81
SS
543 # Handle hostname details. Only connect the values with a dott if both are available.
544 my $hostname="";
545
546 if (($temp[1]) && ($temp[2])) {
547 $hostname="$temp[1].$temp[2]";
548 } else {
549 $hostname="$temp[1]";
550 }
551
9a33b04f
MT
552 # The following HTML Code still is part of the loop.
553 print <<END;
1f8fccc5
SS
554<tr>
555 <td align='center' $col><a href='http://$temp[0]'>$temp[0]</a></td>
4851bc81 556 <td align='center' $col>$sync$hostname</td>
1f8fccc5 557
1f8fccc5
SS
558 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
559 <input type='hidden' name='ID' value='$id'>
560 <input type='hidden' name='ENABLED' value='$toggle_enabled'>
561 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
562 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
563 </form></td>
564
565 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
566 <input type='hidden' name='ID' value='$id'>
567 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
568 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
569 </form></td>
570
571 <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
572 <input type='hidden' name='ID' value='$id'>
573 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
574 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
575 </form></td>
ac1cfefa
MT
576</tr>
577END
9a33b04f
MT
578 $id++;
579 }
ac1cfefa 580
9a33b04f
MT
581 print <<END;
582</table>
ac1cfefa 583<table width='100%'>
1f8fccc5
SS
584 <tr>
585 <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>
586 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
587 <td class='base'>$Lang::tr{'click to disable'}</td>
588 <td>&nbsp;&nbsp;</td>
589 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
590 <td class='base'>$Lang::tr{'click to enable'}</td>
591 <td>&nbsp;&nbsp;</td>
592 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
593 <td class='base'>$Lang::tr{'edit'}</td>
594 <td>&nbsp;&nbsp;</td>
595 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
596 <td class='base'>$Lang::tr{'remove'}</td>
597 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
598 <td align='right' width='30%'><input type='submit' name='ACTION' value='$Lang::tr{'instant update'}' /></td>
599 </form>
600 </tr>
ac1cfefa
MT
601</table>
602END
9a33b04f
MT
603
604 &Header::closebox();
ac1cfefa
MT
605}
606
ac1cfefa
MT
607&Header::closebigbox();
608&Header::closepage();
609
1f8fccc5
SS
610# Function to generate the required configuration file for the DDNS tool.
611sub GenerateDDNSConfigFile {
612 # Open datafile file
613 open(SETTINGS, "<$datafile") or die "Could not open $datafile.";
614
615 open(FILE, ">${General::swroot}/ddns/ddns.conf");
616
617 # Global configuration options.
618 print FILE "[config]\n";
619
620 # Check if we guess our IP address by an extranal server.
621 if ($settings{'BEHINDROUTER'} eq "FETCH_IP") {
622 print FILE "guess_external_ip = true\n";
623 } else {
624 print FILE "guess_external_ip = false\n";
625 }
626
627 # Use an upstream proxy and generate proxy url.
628 my %proxysettings;
629 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
630 if ($proxysettings{'UPSTREAM_PROXY'}) {
631 my $proxy_string = "http://";
632
633 if ($proxysettings{'UPSTREAM_USER'} && $proxysettings{'UPSTREAM_PASSWORD'}) {
634 $proxy_string .= "$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@";
635 }
636
637 $proxy_string .= $proxysettings{'UPSTREAM_PROXY'};
638
639 print FILE "proxy = $proxy_string\n";
640 }
641
642 print FILE "\n";
643
644 while (<SETTINGS>) {
645 my $line = $_;
458064c5 646 chomp($line);
1f8fccc5
SS
647
648 # Generate array based on the line content (seperator is a single or multiple space's)
649 my @settings = split(/,/, $line);
650 my ($provider, $hostname, $domain, $proxy, $wildcards, $username, $password, $enabled) = @settings;
651
652 # Skip entries if they are not (longer) supported.
06fb88bc 653 next unless ($provider ~~ @providers);
1f8fccc5
SS
654
655 # Skip disabled entries.
458064c5 656 next unless ($enabled eq "on");
1f8fccc5 657
4851bc81
SS
658 # Handle hostname details. Only connect the values with a dott if both are available.
659 if (($hostname) && ($domain)) {
660 print FILE "[$hostname.$domain]\n";
661 } else {
662 print FILE "[$hostname]\n";
663 }
664
1f8fccc5
SS
665 print FILE "provider = $provider\n";
666
667 my $use_token = 0;
668
669 # Handle token based auth for various providers.
82bd80b3 670 if ($provider ~~ ["dns.lightningwirelabs.com", "entrydns.net", "regfish.com", "spdns.de"] && $username eq "token") {
1f8fccc5
SS
671 $use_token = 1;
672
5fe185f8
SS
673 # Handle token auth for freedns.afraid.org and regfish.com.
674 } elsif ($provider ~~ ["freedns.afraid.org", "regfish.com"] && $password eq "") {
1f8fccc5
SS
675 $use_token = 1;
676 $password = $username;
3c781f63
MT
677
678 # Handle keys for nsupdate
679 } elsif (($provider eq "nsupdate") && $username && $password) {
680 print FILE "key = $username\n";
681 print FILE "secret = $password\n";
682
683 $username = "";
684 $password = "";
1f8fccc5 685
f5770b46
SS
686 # Handle keys for nsupdate.info
687 } elsif (($provider eq "nsupdate.info") && $password) {
688 print FILE "secret = $password\n";
689
690 $username = "";
691 $password = "";
692 }
693
1f8fccc5
SS
694 # Write auth details.
695 if ($use_token) {
696 print FILE "token = $password\n";
3c781f63 697 } elsif ($username && $password) {
1f8fccc5
SS
698 print FILE "username = $username\n";
699 print FILE "password = $password\n";
700 }
701
702 # These providers need to be set to only use IPv4.
4fb763ff 703 if ($provider ~~ ["freedns.afraid.org", "nsupdate.info", "opendns.com", "variomedia.de", "zoneedit.com"]) {
1f8fccc5
SS
704 print FILE "proto = ipv4\n";
705 }
706
707 print FILE "\n";
708 }
709
710 close(SETTINGS);
711 close(FILE);
712}
713
714# Function which generates an array (@providers) which contains the supported providers.
715sub GetProviders {
716 # Get supported providers.
717 open(PROVIDERS, "/usr/bin/ddns list-providers |");
718
719 # Create new array to store the providers.
720 my @providers = ();
721
722 while (<PROVIDERS>) {
723 my $provider = $_;
724
725 # Remove following newlines.
726 chomp($provider);
727
728 # Add provider to the array.
729 push(@providers, $provider);
730 }
ac1cfefa 731
1f8fccc5 732 close(PROVIDERS);
ac1cfefa 733
1f8fccc5
SS
734 # Return our array.
735 return @providers;
ac1cfefa 736}