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