Remove mydyn.de from ddns services.
[people/pmueller/ipfire-2.x.git] / src / scripts / setddns.pl
CommitLineData
ee4fdd07
MT
1#!/usr/bin/perl
2#
3# SmoothWall CGIs
4#
5# This code is distributed under the terms of the GPL
6#
7# (c) The SmoothWall Team
8#
9# $Id: setddns.pl,v 1.4.2.32 2006/02/07 01:29:47 franck78 Exp $
10#
11
12#close(STDIN);
13#close(STDOUT);
14#close(STDERR);
15
16use strict;
17use IO::Socket;
18use Net::SSLeay;
19
20require '/var/ipfire/general-functions.pl';
21
22#Prototypes functions
23sub encode_base64 ($;$);
24
25my %settings;
26my $filename = "${General::swroot}/ddns/config";
27my $cachefile = "${General::swroot}/ddns/ipcache";
28my $ipcache = 0;
29my @current = ();
30
31if (open(FILE, "$filename")) {
32 @current = <FILE>;
33 close(FILE);
34 unless(@current) {
35 exit 0;
36 }
37} else {
38 &General::log('Dynamic DNS failure : unable to open config file.');
39 exit 0;
40}
41
42&General::readhash("${General::swroot}/ddns/settings", \%settings);
43
44# ignore monthly update if not in minimize update mode
45exit 0 if (($settings{'MINIMIZEUPDATES'} ne 'on') && ($ARGV[1] eq '-m'));
46
47my $ip;
48if (open(IP, "${General::swroot}/red/local-ipaddress")) {
49 $ip = <IP>;
50 close(IP);
51 chomp $ip;
52} else {
53 &General::log('Dynamic DNS failure : unable to open local-ipaddress file.');
54 exit 0;
55}
56
57#If IP is reserved network, we are behind a router. May we ask for our real public IP ?
58if ( &General::IpInSubnet ($ip,'10.0.0.0','255.0.0.0') ||
59 &General::IpInSubnet ($ip,'172.16.0.0','255.240.0.0') ||
60 &General::IpInSubnet ($ip,'192.168.0.0','255.255.0.0')) {
61 # We can, but are we authorized by GUI ?
62 if ($settings{'BEHINDROUTER'} eq 'FETCH_IP') {
63 if ($ARGV[0] eq '-f'){
64 $settings{'BEHINDROUTERWAITLOOP'} = -1; # When forced option, fectch PublicIP now
65 }
66
67 # Increment counter modulo 4. When it is zero, fetch ip else exit
68 # This divides by 4 the requests to the dyndns server.
69 $settings{'BEHINDROUTERWAITLOOP'} = ($settings{'BEHINDROUTERWAITLOOP'}+1) %4;
70 &General::writehash("${General::swroot}/ddns/settings", \%settings);
71 exit 0 if ( $settings{'BEHINDROUTERWAITLOOP'} ne 0 );
72 my $RealIP = &General::FetchPublicIp;
73 $ip = (&General::validip ($RealIP) ? $RealIP : 'unavailable');
74 &General::log ("Dynamic DNS public router IP is:$ip");
75 }
76}
77
ee4fdd07
MT
78if ($ARGV[0] eq '-f') {
79 unlink ($cachefile); # next regular calls will try again if this force update fails.
80} else {
81 open(IPCACHE, "$cachefile");
82 $ipcache = <IPCACHE>;
83 close(IPCACHE);
84 chomp $ipcache;
85}
86
87if ($ip ne $ipcache) {
88 my $id = 0;
89 my $success = 0;
90 my $line;
91 my $lines = @current;
92
93 foreach $line (@current) {
94 $id++;
95 chomp($line);
96 my @temp = split(/\,/,$line);
97 unless ($temp[7] ne "on") {
98 $settings{'SERVICE'} = $temp[0];
99 $settings{'HOSTNAME'} = $temp[1];
100 $settings{'DOMAIN'} = $temp[2];
101 $settings{'PROXY'} = $temp[3];
102 $settings{'WILDCARDS'} = $temp[4];
103 $settings{'LOGIN'} = $temp[5];
104 $settings{'PASSWORD'} = $temp[6];
105 $settings{'ENABLED'} = $temp[7];
106
107 #Some connection are very stable (more than 40 days). Finally force
108 #one update / month to avoid account lost
109 #cron call once/week with -f & once/month with -f -m options
110 #minimize update ?
111 if ( ($settings{'MINIMIZEUPDATES'} eq 'on') && ($ARGV[1] ne '-m') ) {
112 if (General::DyndnsServiceSync($ip, $settings{'HOSTNAME'},$settings{'DOMAIN'})) {
113 &General::log ("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} is uptodate [$ip]");
114 $success++;
115 next; # do not update, go to test next service
116 }
117 }
118 my @service = split(/\./, "$settings{'SERVICE'}");
119 $settings{'SERVICE'} = "$service[0]";
120 if ($settings{'SERVICE'} eq 'no-ip') {
121 open(F, ">${General::swroot}/ddns/noipsettings");
122 flock F, 2;
123 print F "PROXY=" . ($settings{'PROXY'} eq 'on' ? "Y\n" : "N\n");
124 print F "PASSWORD=$settings{'PASSWORD'}\n";
125 print F "NAT=N\n";
126 print F "LOGIN=$settings{'LOGIN'}\n";
127 print F "INTERVAL=1\n";
128 if ($settings{'HOSTNAME'} !~ s/$General::noipprefix//) {
129 print F "HOSTNAME=$settings{'HOSTNAME'}\n";
130 print F "GROUP=\n";
131 } else {
132 print F "HOSTNAME=\n";
133 print F "GROUP=$settings{'HOSTNAME'}\n";
134 }
135 print F "DOMAIN=$settings{'DOMAIN'}\n";
136 print F "DEVICE=\n";
137 print F "DAEMON=N\n";
138 close(F);
139
140 my @ddnscommand = ('/usr/bin/noip','-c',"${General::swroot}/ddns/noipsettings",'-i',"$ip");
141
142 my $result = system(@ddnscommand);
143 if ( $result != 0) {
144 &General::log("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} : failure");
145 } else {
146 &General::log("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} : success");
147 $success++;
148 }
149 }
150
151 elsif ($settings{'SERVICE'} eq 'cjb') {
152 # use proxy ?
153 my %proxysettings;
154 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
155 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
156 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
157 Net::SSLeay::set_proxy($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
158 }
159
160 my ($out, $response) = Net::SSLeay::get_http( 'www.cjb.net',
161 80,
162 "/cgi-bin/dynip.cgi?username=$settings{'LOGIN'}&password=$settings{'PASSWORD'}&ip=$ip",
4ed2816f 163 Net::SSLeay::make_headers('User-Agent' => 'IPFire' )
ee4fdd07
MT
164 );
165
166 if ($response =~ m%HTTP/1\.. 200 OK%) {
167 if ( $out !~ m/has been updated to point to/ ) {
168 &General::log("Dynamic DNS ip-update for cjb.net ($settings{'LOGIN'}) : failure (bad password or login)");
169 } else {
170 &General::log("Dynamic DNS ip-update for cjb.net ($settings{'LOGIN'}) : success");
171 $success++;
172 }
173 } else {
174 &General::log("Dynamic DNS ip-update for cjb.net ($settings{'LOGIN'}) : failure (could not connect to server)");
175 }
176 }
177 elsif ($settings{'SERVICE'} eq 'selfhost') {
178 # use proxy ?
179 my %proxysettings;
180 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
181 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
182 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
183 Net::SSLeay::set_proxy($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
184 }
185
186 my ($out, $response) = Net::SSLeay::get_https( 'carol.selfhost.de',
187 443,
188 "/update?username=$settings{'LOGIN'}&password=$settings{'PASSWORD'}&textmodi=1",
4ed2816f 189 Net::SSLeay::make_headers('User-Agent' => 'IPFire' )
ee4fdd07
MT
190 );
191
192 if ($response =~ m%HTTP/1\.. 200 OK%) {
193 if ( $out !~ m/status=(200|204)/ ) {
194 $out =~ s/\n/ /g;
195 &General::log("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} : failure ($out)");
196 } else {
197 &General::log("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} : success");
198 $success++;
199 }
200 } else {
201 &General::log("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} : failure (could not connect to server)");
202 }
203 }
204 elsif ($settings{'SERVICE'} eq 'dnspark') {
205 # use proxy ?
206 my %proxysettings;
207 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
208 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
209 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
210 Net::SSLeay::set_proxy($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
211 }
212
213 if ($settings{'HOSTNAME'} eq '') {
214 $settings{'HOSTDOMAIN'} = $settings{'DOMAIN'};
215 } else {
216 $settings{'HOSTDOMAIN'} = "$settings{'HOSTNAME'}.$settings{'DOMAIN'}";
217 }
218
219 my ($out, $response) = Net::SSLeay::get_https( "www.dnspark.net",
220 443,
221 "/api/dynamic/update.php?hostname=$settings{'HOSTDOMAIN'}&ip=$ip",
4ed2816f 222 Net::SSLeay::make_headers('User-Agent' => 'IPFire',
ee4fdd07
MT
223 'Authorization' => 'Basic ' . encode_base64("$settings{'LOGIN'}:$settings{'PASSWORD'}")
224 )
225 );
226 # Valid response are 'ok' 'nochange'
227 if ($response =~ m%HTTP/1\.. 200 OK%) {
228 if ( $out !~ m/^(ok|nochange)/ ) {
229 $out =~ s/\n/ /g;
230 &General::log("Dynamic DNS ip-update for $settings{'HOSTDOMAIN'} : failure ($out)");
231 } else {
232 &General::log("Dynamic DNS ip-update for $settings{'HOSTDOMAIN'} : success");
233 $success++;
234 }
235 } else {
236 &General::log("Dynamic DNS ip-update for $settings{'HOSTDOMAIN'} : failure (could not connect to server, check your credentials)");
237 }
238 }
239 elsif ($settings{'SERVICE'} eq 'enom') {
240 # use proxy ?
241 my %proxysettings;
242 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
243 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
244 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
245 Net::SSLeay::set_proxy($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
246 }
247 if ($settings{'HOSTNAME'} eq '') {
248 $settings{'HOSTDOMAIN'} = $settings{'DOMAIN'};
249 } else {
250 $settings{'HOSTDOMAIN'} = "$settings{'HOSTNAME'}.$settings{'DOMAIN'}";
251 }
252
253 my ($out, $response) = Net::SSLeay::get_http( 'dynamic.name-services.com',
254 80,
255 "/interface.asp?Command=SetDNSHost&Zone=$settings{'DOMAIN'}&DomainPassword=$settings{'PASSWORD'}&Address=$ip",
4ed2816f 256 Net::SSLeay::make_headers('User-Agent' => 'IPFire' )
ee4fdd07
MT
257 );
258
259 if ($response =~ m%HTTP/1\.. 200 OK%) {
260 #Valid responses from update => ErrCount=0
261 if ( $out !~ m/ErrCount=0/ ) {
262 $out =~ s/(\n|\x0D)/ /g;
263 $out =~ /Err1=([\w ]+) /;
264 &General::log("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} : failure ($1)");
265 } else {
266 &General::log("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} : success");
267 $success++;
268 }
269 } else {
270 &General::log("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} : failure (could not connect to server)");
271 }
272 }
273 elsif ($settings{'SERVICE'} eq 'nsupdate') {
274 # Fetch UI configurable values and assemble the host name.
275
276 my $hostName="$settings{'DOMAIN'}";
277 if ($settings{'HOSTNAME'} ne "") {
278 $hostName="$settings{'HOSTNAME'}.$hostName";
279 }
280 my $keyName=$settings{'LOGIN'};
281 my $keySecret=$settings{'PASSWORD'};
282
283 # Use a relatively long TTL value to reduce load on DNS.
284 # Some public Dynamic DNS servers use values around 4 hours,
285 # some use values as low as 60 seconds.
286 # XXX Maybe we could fetch the master value from the server
287 # (not the timed-down version supplied by DNS cache)
288
289 my $timeToLive="3600";
290
291 # Internal setting that can be used to override the DNS server
292 # where the update is applied. It can be of use when testing
293 # against a private DNS server.
294
295 my $masterServer="";
296
297 # Prepare the nsupdate command script to remove and re-add the
298 # updated A record for the domain.
299
300 my $cmdFile="/tmp/nsupdate-$hostName-commands";
301 my $logFile="/tmp/nsupdate-$hostName-result";
302 open(TF, ">$cmdFile");
303 if ($masterServer ne "") {
304 print TF "server $masterServer\n";
305 }
306 if ($keyName ne "" && $keySecret ne "") {
307 print TF "key $keyName $keySecret\n";
308 }
309 print TF "update delete $hostName A\n";
310 print TF "update add $hostName $timeToLive A $ip\n";
311 print TF "send\n";
312 close(TF);
313
314 # Run nsupdate with -v to use TCP instead of UDP because we're
315 # issuing multiple cmds and potentially long keys, and -d to
316 # get diagnostic result output.
317
318 my $result = system("/usr/bin/nsupdate -v -d $cmdFile 2>$logFile");
319 if ($result != 0) {
320 &General::log("Dynamic DNS ip-update for $hostName : failure");
321 open(NSLOG, "$logFile");
322 my @nsLog = <NSLOG>;
323 close(NSLOG);
324 my $logLine;
325 foreach $logLine (@nsLog) {
326 chomp($logLine);
327 if ($logLine ne "") {
328 &General::log("... $logLine");
329 }
330 }
331 } else {
332 &General::log("Dynamic DNS ip-update for $hostName : success");
333 $success++;
334 }
335 unlink $cmdFile, $logFile;
336 }
337 elsif ($settings{'SERVICE'} eq 'freedns') {
338 # use proxy ?
339 my %proxysettings;
340 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
341 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
342 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
343 Net::SSLeay::set_proxy($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
344 }
345
346 my ($out, $response) = Net::SSLeay::get_https( 'freedns.afraid.org',
347 443,
348 "/dynamic/update.php?$settings{'LOGIN'}",
4ed2816f 349 Net::SSLeay::make_headers('User-Agent' => 'IPFire' )
ee4fdd07
MT
350 );
351 #Valid responses from service are:
352 #Updated n host(s) <domain>
353 #ERROR: <ip> has not changed.
354 if ($response =~ m%HTTP/1\.. 200 OK%) {
355 #Valid responses from update => ErrCount=0
356 if ( $out !~ m/(^Updated|Address .* has not changed)/ig ) {
357 &General::log("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} : failure ($out)");
358 } else {
359 &General::log("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} : success");
360 $success++;
361 }
362 } else {
363 &General::log("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} : failure (could not connect to server)");
364 }
365 }
b411505d 366 elsif ($settings{'SERVICE'} eq 'strato') {
ee974f39
MS
367 # use proxy ?
368 my %proxysettings;
369 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
370 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
371 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
372 Net::SSLeay::set_proxy($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
373 }
b411505d
CS
374
375 if ($settings{'HOSTNAME'} eq '') {
376 $settings{'HOSTDOMAIN'} = $settings{'DOMAIN'};
377 } else {
378 $settings{'HOSTDOMAIN'} = "$settings{'HOSTNAME'}.$settings{'DOMAIN'}";
379 }
380
381 my ($out, $response) = Net::SSLeay::get_https( 'dyndns.strato.com',
382 443,
383 "/nic/update?hostname=$settings{'HOSTDOMAIN'}&myip=$ip",
384 Net::SSLeay::make_headers('User-Agent' => 'IPFire',
385 'Authorization' => 'Basic ' . encode_base64("$settings{'LOGIN'}:$settings{'PASSWORD'}") )
386 );
387
ee974f39 388 if ($response =~ m%HTTP/1\.. 200 OK%) {
b411505d
CS
389 #Valid responses from update => ErrCount=0
390 if ( $out =~ m/good |nochg /ig) {
391 &General::log("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} : success");
392 $success++;
ee974f39 393 } else {
b411505d 394 &General::log("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} : failure1 ($out)");
ee974f39
MS
395 $success++;
396 }
b411505d
CS
397 } elsif ( $out =~ m/<title>(.*)<\/title>/ig ) {
398 &General::log("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} : failure2 ($1)");
399 } else {
400 &General::log("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} : failure3 ($response)");
401 }
402 }
403 elsif ($settings{'SERVICE'} eq 'regfish') {
404 # use proxy ?
405 my %proxysettings;
406 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
407 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
408 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
409 Net::SSLeay::set_proxy($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
410 }
411 my ($out, $response) = Net::SSLeay::get_https( 'dyndns.regfish.de',
412 443,
413 "/?fqdn=$settings{'DOMAIN'}&ipv4=$ip&forcehost=1&authtype=secure&token=$settings{'LOGIN'}",
414 Net::SSLeay::make_headers('User-Agent' => 'Ipfire' )
415 );
416 #Valid responses from service are:
417 #success|100|update succeeded!
418 #success|101|no update needed at this time..
419 if ($response =~ m%HTTP/1\.. 200 OK%) {
420 if ( $out !~ m/(success\|(100|101)\|)/ig ) {
421 &General::log("Dynamic DNS ip-update for $settings{'DOMAIN'} : failure ($out)");
ee4fdd07 422 } else {
b411505d
CS
423 &General::log("Dynamic DNS ip-update for $settings{'DOMAIN'} : success");
424 $success++;
ee4fdd07 425 }
b411505d
CS
426 } else {
427 &General::log("Dynamic DNS ip-update for $settings{'DOMAIN'} : failure (could not connect to server)");
428 }
ee4fdd07
MT
429 }
430 elsif ($settings{'SERVICE'} eq 'ovh') {
431 my %proxysettings;
432 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
433
434 my $peer = 'www.ovh.com';
435 my $peerport = 80;
436
437 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
438 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
439 }
440
441 my $sock;
442 unless($sock = new IO::Socket::INET (PeerAddr => $peer, PeerPort => $peerport, Proto => 'tcp', Timeout => 5)) {
443 &General::log("Dynamic DNS failure : could not connect to $peer:$peerport: $@");
444 next;
445 }
446
447 if ($settings{'HOSTNAME'} eq '') {
448 $settings{'HOSTDOMAIN'} = $settings{'DOMAIN'};
449 } else {
450 $settings{'HOSTDOMAIN'} = "$settings{'HOSTNAME'}.$settings{'DOMAIN'}";
451 }
452
453 my ($GET_CMD, $code64);
454 $GET_CMD = "GET http://www.ovh.com/nic/update?system=dyndns&hostname=$settings{'HOSTDOMAIN'}&myip=$ip HTTP/1.1\r\n";
455 $GET_CMD .= "Host: www.ovh.com\r\n";
456 chomp($code64 = encode_base64("$settings{'LOGIN'}:$settings{'PASSWORD'}"));
457 $GET_CMD .= "Authorization: Basic $code64\r\n";
4ed2816f 458 $GET_CMD .= "User-Agent: ipfire\r\n";
ee4fdd07
MT
459 #$GET_CMD .= "Content-Type: application/x-www-form-urlencoded\r\n";
460 $GET_CMD .= "\r\n";
461 print $sock "$GET_CMD";
462
463 my $out = '';
464 while(<$sock>) {
465 $out .= $_;
466 }
467 close($sock);
468
469 #HTTP response => error (in Title tag) else text response
470 #Valid responses from service:good,nochg (ez-ipupdate like)
471 #Should use ez-ipdate but "system=dyndns" is not present
472 if ( $out =~ m/<Title>(.*)<\/Title>/ig ) {
473 &General::log("Dynamic DNS ovh.com : failure ($1)");
474 }
475 elsif ($out !~ m/good |nochg /ig) {
476 $out =~ s/.+?\015?\012\015?\012//s; # header HTTP
477 my @out = split("\r", $out);
478 &General::log("Dynamic DNS ip-update for $settings{'DOMAIN'} : failure ($out[1])");
479 } else {
480 &General::log("Dynamic DNS ip-update for $settings{'DOMAIN'} : success");
481 $success++;
482 }
483 }
484 elsif ($settings{'SERVICE'} eq 'dtdns') {
485 # use proxy ?
486 my %proxysettings;
487 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
488 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
489 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
490 Net::SSLeay::set_proxy($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
491 }
492
493 if ($settings{'HOSTNAME'} eq '') {
494 $settings{'HOSTDOMAIN'} = $settings{'DOMAIN'};
495 } else {
496 $settings{'HOSTDOMAIN'} = "$settings{'HOSTNAME'}.$settings{'DOMAIN'}";
497 }
498
499 my ($out, $response) = Net::SSLeay::get_http( 'www.dtdns.com',
500 80,
501 "/api/autodns.cfm?id=$settings{'HOSTDOMAIN'}&pw=$settings{'PASSWORD'}",
4ed2816f 502 Net::SSLeay::make_headers('User-Agent' => 'IPFire' )
ee4fdd07
MT
503 );
504 #Valid responses from service are:
505 # now points to
506 #
507 if ($response =~ m%HTTP/1\.. 200 OK%) {
508 if ( $out !~ m/Host .* now points to/ig ) {
509 &General::log("Dynamic DNS ip-update for $settings{'HOSTDOMAIN'} : failure ($out)");
510 } else {
511 &General::log("Dynamic DNS ip-update for $settings{'HOSTDOMAIN'} : success");
512 $success++;
513 }
514 } else {
515 &General::log("Dynamic DNS ip-update for $settings{'HOSTDOMAIN'} : failure (could not connect to server)");
516 }
517 }
518 elsif ($settings{'SERVICE'} eq 'dynu') {
519 # use proxy ?
520 my %proxysettings;
521 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
522 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
523 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
524 Net::SSLeay::set_proxy($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
525 }
526
527 if ($settings{'HOSTNAME'} eq '') {
528 $settings{'HOSTDOMAIN'} = $settings{'DOMAIN'};
529 } else {
530 $settings{'HOSTDOMAIN'} = "$settings{'HOSTNAME'}.$settings{'DOMAIN'}";
531 }
532
533 my ($out, $response) = Net::SSLeay::get_http( 'dynserv.ca',
534 80,
535 "/dyn/dynengine.cgi?func=set&name=$settings{'LOGIN'}&pass=$settings{'PASSWORD'}&ip=$ip&domain=$settings{'DOMAIN'}",
4ed2816f 536 Net::SSLeay::make_headers('User-Agent' => 'IPFire' )
ee4fdd07
MT
537 );
538 #Valid responses from service are:
539 # 02 == Domain already exists, refreshing data for ... => xxx.xxx.xxx.xxx
540 #
541 if ($response =~ m%HTTP/1\.. 200 OK%) {
542 if ( $out !~ m/Domain already exists, refreshing data for/ig ) {
543 &General::log("Dynamic DNS ip-update for $settings{'HOSTDOMAIN'} : failure ($out)");
544 } else {
545 &General::log("Dynamic DNS ip-update for $settings{'HOSTDOMAIN'} : success");
546 $success++;
547 }
548 } else {
549 &General::log("Dynamic DNS ip-update for $settings{'HOSTDOMAIN'} : failure (could not connect to server)");
550 }
551 } else {
552 if ($settings{'WILDCARDS'} eq 'on') {
553 $settings{'WILDCARDS'} = '-w';
554 } else {
555 $settings{'WILDCARDS'} = '';
556 }
557
558 if (($settings{'SERVICE'} eq 'dyndns-custom' ||
559 $settings{'SERVICE'} eq 'easydns' ||
560 $settings{'SERVICE'} eq 'zoneedit') && $settings{'HOSTNAME'} eq '') {
561 $settings{'HOSTDOMAIN'} = $settings{'DOMAIN'};
562 } else {
563 $settings{'HOSTDOMAIN'} = "$settings{'HOSTNAME'}.$settings{'DOMAIN'}";
564 }
565
566 my @ddnscommand = ('/usr/bin/ez-ipupdate', '-a', "$ip", '-S', "$settings{'SERVICE'}", '-u', "$settings{'LOGIN'}:$settings{'PASSWORD'}", '-h', "$settings{'HOSTDOMAIN'}", "$settings{'WILDCARDS'}", '-q');
567
568 my $result = system(@ddnscommand);
569 if ( $result != 0) {
570 &General::log("Dynamic DNS ip-update for $settings{'HOSTDOMAIN'}: failure");
571 } else {
572 &General::log("Dynamic DNS ip-update for $settings{'HOSTDOMAIN'}: success");
573 $success++;
574 }
575 }
576 } else {
577 # If a line is disabled, then we should discount it
578 $lines--;
579 }
580 }
581
582 if ($lines == $success) {
583 open(IPCACHE, ">$cachefile");
584 flock IPCACHE, 2;
585 print IPCACHE $ip;
586 close(IPCACHE);
587 exit 1;
588 }
589
590}
591exit 0;
592
593# Extracted from Base64.pm
594sub encode_base64 ($;$) {
595 my $res = "";
596 my $eol = $_[1];
597 $eol = "\n" unless defined $eol;
598 pos($_[0]) = 0; # ensure start at the beginning
599 while ($_[0] =~ /(.{1,45})/gs) {
600 $res .= substr(pack('u', $1), 1);
601 chop($res);
602 }
603 $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
604 # fix padding at the end
605 my $padding = (3 - length($_[0]) % 3) % 3;
606 $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
607 # break encoded string into lines of no more than 76 characters each
608 if (length $eol) {
609 $res =~ s/(.{1,76})/$1$eol/g;
610 }
611 $res;
612}
613
614
615
616__END__
617old code for selfhost.de
618
619 my %proxysettings;
620 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
621
622 my $peer = 'carol.selfhost.de';
623 my $peerport = 80;
624
625 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
626 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
627 }
628
629 my $sock;
630 unless($sock = new IO::Socket::INET (PeerAddr => $peer, PeerPort => $peerport, Proto => 'tcp', Timeout => 5)) {
631 die "Could not connect to $peer:$peerport: $@";
632 return 1;
633 }
634
635 my $GET_CMD;
636 $GET_CMD = "GET https://carol.selfhost.de/update?username=$settings{'LOGIN'}&password=$settings{'PASSWORD'}&myip=$ip&textmodi=1 HTTP/1.1\r\n";
637 $GET_CMD .= "Host: carol.selfhost.de\r\n";
4ed2816f 638 $GET_CMD .= "User-Agent: ipfire\r\n";
ee4fdd07
MT
639 $GET_CMD .= "Connection: close\r\n\r\n";
640 print $sock "$GET_CMD";
641
642 my $out = '';
643 while(<$sock>) {
644 $out .= $_;
645 }
646 close($sock);
647
648 if ( $out !~ m/status=(200|204)/ ) {
649 #cleanup http response...
650 $out =~ s/.+?\015?\012\015?\012//s; # header HTTP
651 my @out = split("\r", $out);
652 &General::log("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} : failure ($out[1])");
653 } else {
654 &General::log("Dynamic DNS ip-update for $settings{'HOSTNAME'}.$settings{'DOMAIN'} : success");
655 $success++;
656 }
657
658
659