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