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