forked from pflanze/chj-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cj-ddclient
executable file
·524 lines (479 loc) · 16.4 KB
/
cj-ddclient
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#!/usr/bin/perl -w
# Son Feb 11 18:41:05 CET 2007
(my $email='pflanze%gmx,ch')=~ tr/%,/@./;
use strict;
$0=~ /(.*?)([^\/]+)\z/s or die "?";
my ($mydir, $myname)=($1,$2);
our $statedbpath= "$ENV{HOME}/.$myname"; mkdir $statedbpath, 0700;
sub usage {
print STDERR map{"$_\n"} @_ if @_;
print "$myname configfile ip
(Christian Jaeger <$email>)
";
exit (@_ ? 1 : 0);
}
my @args;
my $DEBUG=0;
for (my $i=0; $i<=$#ARGV; $i++) {
local $_=$ARGV[$i];
if (/^--?h(elp)?$/) {
usage
} elsif ($_ eq '--') {
push @args, @ARGV[$i+1..$#ARGV];
last;
} elsif (/^--?d(ebug)?$/) {
$DEBUG=1;
# } elsif (/^--?X(?:XXX(?:=(.*))?)?$/) {
# if (defined $1) {
# $XXX=$1
# } else {
# $XXX=$ARGV[++$i] or usage "missing argument for '$_' option";
# }
} elsif (/^-./) {
usage("Unknown option '$_'\n");
} else {
push @args, $_
}
}
usage unless @args==2;
use Chj::singlequote 'singlequote_many';
use Chj::Socket::SSL;
our $useragent= "cj-ddclient/1";
#sub xgeturl ($ $ $ $ ) {
{
package _Connections;
sub Sysreadline ($ ) {
my ($fh)=@_;
my $buf;
my $line="";
while(1) {
my $res= sysread($fh,$buf,1);
defined $res or die "Sysreadline: error $!";
if ($res) {
$res==1 or die "??? got $res bytes how can this be";
} else {
# eof.
# complain?
warn "got eof before end of line";
#return $line
if (length $line) {
return $line;
} else {
return undef
}
}
$line.= $buf;
if ($buf eq "\n") {
return $line;
}
}
}
use MIME::Base64 'encode_base64';
use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
use Class::Array -fields=> -publica=>
('connections', # host => connection
);
sub connection_for_host_port {
my $s=shift;
my ($host,$port)=@_;
my $chs=
$$s[Connections]{"$host:$port"}
||= Chj::Socket::SSL->new_host_port ($host,$port);
##TODO $chs->xvalid;
# {
# # switch to non-blocking io so that we can read using <> ok?
# my $s=$chs->socket;
# my $flags= fcntl ($s, F_GETFL, 0);
# fcntl($s, F_SETFL, $flags | O_NONBLOCK) or die "fcntl: $!";
# }
#nope not a good idea of course since i would read too fast before result is here (i get undef before anything has arrived (even on localhost)). [ah and btw what should it mean with encrypted connection???!]
{
# switch buffering off, so <> is safe at least while reading the header ok? -- it STILL isn't safe to mix sysread and <>.though. right?oderbinich BUG OFF?
my $oldfh= select $chs->socket;
$|++;
select $oldfh;
}
$chs #und wie machen dass rausfinden ob noch lebendig ? ?[todo]
}
sub remove_connection_for_host_port {
my $s=shift;
my ($host,$port)=@_;
warn "remove_connection_for_host_port ($host,$port)";#
if (my $c= $$s[Connections]{"$host:$port"}) {
$c->socket->close;
}
delete $$s[Connections]{"$host:$port"};
}
sub DESTROY {
my $s=shift;
for my $c (values %{$$s[Connections]}) {
eval {
$c->socket->close;
};#hm was ichnich erwartet hatt: 'Can't call method "close" on an undefined value' "manchmal"
my $e=$@;
eval{ warn "note: ".$c->host.": $e" if $e}
}
}
sub xgeturl {
my $self=shift;
my ($url, $login, $password)=@_;
#warn "xgeturl ".singlequote_many ($url);
my $default_port= 443;
$url =~ s%^https?://%%i;
my $server = $url;
$server =~ s%/.*%%;
my ($host,$port)= split /:/, $server;
$port||=$default_port;
$url = "/" unless $url =~ m%/%;
$url =~ s%^[^/]*/%%;
my $request= "GET ";
#$request .= "http://$server" if $proxy;
$request .= "/$url HTTP/1.1\n";
$request .= "Host: $server\n";
if (defined $login) {
my $auth = encode_base64("${login}:${password}");
$request .= "Authorization: Basic $auth";
}
$request .= "User-Agent: $useragent\n";
$request .= "Connection: keep-alive\n";
$request .= "\n";
warn "xgeturl: firing request to '$host' port $port: ---\n$request--- ";
(my $rq = $request) =~ s/\n/\r\n/g;
my $nretries=0;
SENDREQUEST:
my $chs= $self->connection_for_host_port ($host,$port);
local our $s= $chs->socket;
my $res= $s->syswrite ($rq);
if($res != length ($rq)) {
my $e= $!;
eval { $self->remove_connection_for_host_port ($host,$port); }; warn $@ if $@;
die "cannot sent to $host:$port ($e)"; ## ssl anders? und nicht die ?.achdochhe.?.
#"[und close?.. na wenn weakref installed sei es safe?]"ehnumm
} else {
####hmm timouts gehen doch nicht mehr so bei neuem perl ?
##ABER das socket hat doch SELBER gute timeout oderetwanid?
my $reply= _Reply->new;
#while (<$s>) {
REPLY: {
while (defined($_= Sysreadline($s))) {
$reply->add_headerline($_);
if (/^\r?\n\z/) {
# body will follow if any.
if (my $length= $reply->header ("Content-Length")) {
if (my $v=$reply->header("Accept-Ranges")) {
$v eq 'bytes' or warn "warning: unknown range '$v'"; #grr hatte eq /bytes/ und perl tut nid reklame
}
# my $body;
# my $res= $s->sysread ($body,$length);
# defined $res or die "error while reading: $!";## und close und so!..
# $res == $length or die "expected $length bytes, got $res";## dito
#und NEINNN das ist NICIICIICIHT so bei perl, das gibt 8192 statt 39132 motze.
my $body="";
my $pos=0;
while ($pos<$length) {
my $res= $s->sysread ($body,$length-$pos,$pos);
defined $res or die "error while reading: $!";## und close und so!..
#todo MUSS ich nun signals behandeln oder muss ich nicht. perl sooo x ?.
if ($res == $length-$pos) {
last;
}
if (!$res) {
die "expected ".($length-$pos)." bytes, got $res";## dito
}
$pos+= $res;
}
$reply->set_body ($body);
} elsif (do {
#$reply->is_chunked
my $str= $reply->header("Transfer-Encoding");
$str and lc($str) eq 'chunked'
}) {
# chunked:
my $body="";
my $globalpos=0;
CHUNK: {
while (defined (my $line= Sysreadline($s))) { #"#PS HEH: "wie kann eigtl sein" dass dies geht aber chomp dann doch \r nid sensitiv ist ? hm klaro: <> tut an \n brechen. chomp nur \n removen. so kommt das."
#again:warn "line: {{{$line}}}";
#if ($line eq "\r\n" or $line eq "\n") {
# $line= Sysreadline ($s); #which is probably even *expected*.
#}
$line=~ /^([0-9a-fA-F]+) *\r?\n\z/ or die "expected chunk size, got: '$line'";# the " *" *is* necessary for elvis-proxy even!!!
#do {warn "expected chunk size, got: '$line'";#
#$line= Sysreadline($s); goto again;};#todo wieder die
my $hexlength= $1;
my $length= hex $hexlength;
if ($length==0) {
# OCH but first read off AGAIN AN expected terminating linebreak after the chunk size designation ??!:
my $line= Sysreadline($s);
if ($line eq "\r\n" or $line eq "\n") {
#ok
} else {
die "expected linebreak after chunk, got: '$line'";
}
#/OCH
last CHUNK;
}
# nun kommt quasi COPY von oben, AUSSER $globalpos statt $pos bei string pos
# OCH ud dann ich sovielefehler. $remaininglength wirklich zuweisen zuerstmal WIRKLICH.
my $pos=0;
while ($pos<$length) {
my $remaininglength= ($length-$pos);
my $res= $s->sysread ($body,$remaininglength,$globalpos);
#warn " -- res= ".(defined($res)? $res : "undef");
defined $res or die "error while reading: $!";##
#warn "$res bytes: {{{{".substr($body,$globalpos,$res)."}}}}\n";#
$pos+=$res; ## VOOR last!! gr.
$globalpos+=$res;
if ($res == $remaininglength) {
# no more buf part to read. go to next chunk.
# but first read off the expected terminating linebreak after the chunk!
my $line= Sysreadline($s);
if ($line eq "\r\n" or $line eq "\n") {
#ok
} else {
die "expected linebreak after chunk, got: '$line'";
}
last;
}
if (!$res) {
die "expected $remaininglength bytes, got eof?";## dito
}
}## und newlines hintedran eingeschlossen oder wie?. kommt zeilenumbruch vor der nexchsten zalhl oder nicht?
#warn "did read $pos bytes, body is now: '$body'";
}
die "got eof before final chunk";#
}
$reply->set_body ($body);
}
last REPLY;
}
}
warn "got eof before end of header";
$self->remove_connection_for_host_port ($host,$port);#mit eval drumrum?
if ($nretries++ < 3) {
warn "resendig request";
goto SENDREQUEST;
}
}
unless ($reply->is_keepalive) {
$self->remove_connection_for_host_port ($host,$port);
}
$reply
}
}
end Class::Array;
}
{
package _Reply;
use Class::Array -fields=> -publica=>
("headerlines","body");
sub Chomp ($ ) {
$_[0]=~ s|\r?\n\z||;
}
sub add_headerline {
my $s=shift;
push @{$$s[Headerlines]},@_;
}
sub is_keepalive {
my $s=shift;
if (defined(my $v= $s->header ("connection"))) {
#warn "found connection header: '$v'";#
$v=~ /keep-?alive/i #i nid vergessen bittee..
} else {
0
}
}
sub header { #get_, _ref
my $s=shift;
my ($name)=@_;
#$name=lc ($name); ach ich idio, die sind ja auch nid lowercase. also futile
for (@{$$s[Headerlines]}) {
#warn "looking at hdr '$_'";
return if /^\r?\n\z/;
#warn " checking for '$name'";
#if (/^\Q$name\E:\s*(.*?)\r?\n?\z/s) {
# return $1
if (/^\Q$name\E:/i) {
my $str= $_; $str=~ s/.*?:\s*//; $str=~ s/\r?\n\z//s;
#warn "MATCH: '$_'";
#warn "returning: '$str'";
return $str
}
}
}
sub xheader_ok {
my $s=shift;
my ($host) = @_;
my $line= $$s[Headerlines][0];
defined $line or die "no reply";#
Chomp $line;# argh chomp doesn't work.
if ($line =~ m%^s*HTTP/1.*\s+(\d+)%i) {
my $result = $1;
if ($result eq '200') {
#ok
} elsif ($result eq '401') {
die sprintf("updating '%s': authorization failed (%s)", $host, $line);
} else {
die "updating '$host': $line";
}
} else {
die sprintf("updating %s: unexpected line (%s)", $host, $line);
}
}
##hmm eben wirklich mit return schaffen nid excns? aber valius doch bitte nicht seiten sematnikn.
end Class::Array;
}
#our $cjconfig= require "$ENV{HOME}/tmp/cj-ddclient.pl";
#our $dyndnshost= join(":",@args);##hack; members.dyndns.org
our $dyndnshost= "https://members.dyndns.org";
{
package _Configentry;#damit s kein multifunction copy ist/wird. patternmatch nur an einer stelle. wastive?no
use Class::Array -fields=> -publica=>
qw(type host flag_wildcard maybe_mx flag_backupmx login password);
sub new {
my $class=shift;
my ($configentry)=@_;
@$configentry==6 or die "invalid argument width";
my ($type, $host, $flag_wildcard, $maybe_mx, $flag_backupmx, $account)=@$configentry;#'patternmatch'
@$account==2 or die "invalid account data width";
my ($login,$password)=@$account;
bless [($type, $host, $flag_wildcard, $maybe_mx, $flag_backupmx, $login, $password)],$class #dank perl nicht nur dollarzeichen uberall sondern auch noch kommas.
}
end Class::Array;
}
our $connections= new _Connections;
sub nic_dyndns2_update_GET ($ $ ) {
my ($configentry,$ip)=@_;
my $url = "$dyndnshost/nic/update?system=";
my $type= $configentry->type;
if ($type eq 'custom') {
$url .= 'custom';
} elsif ($type eq 'static') {
$url .= 'statdns';
} elsif ($type eq 'dynamic') {
$url .= 'dyndns';
} else {
die "invalid type '$type'";
}
my $hosts=$configentry->host;##derzeit eben nur einer, keine kommaseparierte liste.
$url .= "&hostname=$hosts";
$url .= "&myip=";
$url .= $ip if $ip;
# some args are not valid for a custom domain.
$url .= "&wildcard=ON" if $configentry->flag_wildcard;
if ($configentry->maybe_mx) {
$url .= "&mx=".$configentry->maybe_mx;
$url .= "&backmx=" . ($configentry->flag_backupmx ? 'YES' : 'NO');
}
my $reply = $connections->xgeturl($url, $configentry->login, $configentry->password);
$reply
}
our $statedb= Chj::FileStore::PIndex->new ($statedbpath);
our $state= sub ($ $ $ ) {
my ($host,$key,$value)=@_;
warn "SAVING '$host', '$key', '$value'";
$statedb->set ("$host|$key",$value);
};
our $now= time;
sub warning {
my ($fmt,@vals)=@_;
print STDERR "warning: ".sprintf($fmt,@vals)."\n";
}
sub nic_dyndns2_update_CHECK ($ $ ) {
my ($configentry,$reply)=@_;
$reply->xheader_ok($configentry->host);
my %errors = (
'badauth' => 'Bad authorization (username or password)',
'badsys' => 'The system parameter given was not valid',
'notfqdn' => 'A Fully-Qualified Domain Name was not provided',
'nohost' => 'The hostname specified does not exist in the database',
'!yours' => 'The hostname specified exists, but not under the username currently being used',
'!donator' => 'The offline setting was set, when the user is not a donator',
'!active' => 'The hostname specified is in a Custom DNS domain which has not yet been activated.',
'abuse', => 'The hostname specified is blocked for abuse; contact [email protected] to be unblocked',
'numhost' => 'System error: Too many or too few hosts found. Contact [email protected]',
'dnserr' => 'System error: DNS error encountered. Contact [email protected]',
'nochg' => 'No update required; unnecessary attempts to change to the current address are considered abusive',
);
my $h= $configentry->host;
if (defined $reply->body) {
my ($status, $ip) = $reply->body =~ /^(.*?) (\S+)/;
# $result{'status'} = $status;
if ($status eq 'good') {
# $config{$h}{'ip'} = $ip;
# $config{$h}{'mtime'} = $now;
# success("updating %s: %s: IP address set to %s", $h, $status, $ip);
#$state->host($h)->set_ip ($ip);
#oder eher n function mmit multi values ??
#kür fein kurry
$state->($h,'ip',$ip);
$state->($h,'mtime',$now);
# multikall versus commits ?.
$state->($h,'status','good'); #fehlte, hab ich wohl weg editiert?.
} elsif (exists $errors{$status}) {
if ($status eq 'nochg') {
warning("updating %s: %s: %s", $h, $status, $errors{$status});
# $config{$h}{'ip'} = $ip;
# $config{$h}{'mtime'} = $now;
# $config{$h}{'status'} = 'good';
$state->($h,'ip',$ip);
$state->($h,'mtime',$now);
$state->($h,'status','good');
} else {
#failed("updating %s: %s: %s", $h, $status, $errors{$status});
die "failed updating '$h': status '$status' ($errors{$status})";
##und, state was setzen ?
}
} elsif ($status =~ /w(\d+)(.)/) {
my ($wait, $units) = ($1, lc $2);
my ($sec, $scale) = ($wait, 1);
($scale, $units) = (1, 'seconds') if $units eq 's';
($scale, $units) = (60, 'minutes') if $units eq 'm';
($scale, $units) = (60*60, 'hours') if $units eq 'h';
$sec = $wait * $scale;
#$config{$h}{'wtime'} = $now + $sec;
$state->($h,'wtime',$now + $sec); ##todo wo wird das benutzt?
warning("updating %s: %s: wait $wait $units before further updates", $h, $status, $ip);
} else {
#failed("updating %s: %s: unexpected status (%s)", $h, $line);
die "unexpected status '$status'";#
}
} else {
#failed("updating %s: Could not connect to %s.", $hosts, $config{$h}{'server'});
die "did not get a body in the reply";## irgend error handlicng isch ja ibmmer schwer sagt man
}
}
sub nic_dyndns2_update ($ $ ) { # gets one host entry. do only one update at once.(we're using keep-alive instead, this gives us more for sure --except that dyndns doesn't support it tja)
my ($configentry,$ip)=@_;
my $reply= nic_dyndns2_update_GET ($configentry,$ip);
nic_dyndns2_update_CHECK ($configentry,$reply);
}
my ($configfile,$ip)=@args;
#use Storable 'store','retrieve';
#our $cjconfig= require "$ENV{HOME}/tmp/cj-ddclient.pl";
#our $cfg0= _Configentry->new ($$cjconfig[0]);
#use Chj::repl;;repl;
my $cfgs= require $configfile;
use Chj::FileStore::PIndex;
our $updateinterval= 15*24*60*60; #seconds
for my $_cfg (@$cfgs) {
my $cfg= _Configentry->new ($_cfg);
# does the host need updating?
my $host= $cfg->host;
my $lastip= $statedb->get ("$host|ip");
if ($lastip) {
if ($lastip eq $ip) {
my $lastupdate= $statedb->get ("$host|mtime");
if (!$lastupdate or ($now-$lastupdate)>$updateinterval) {
# yes needs update
nic_dyndns2_update ($cfg, $ip);
} else {
warn "note: unchanged ip and last update has been on ".localtime($lastupdate).", so no update";
}
} else {
nic_dyndns2_update ($cfg, $ip);
}
} else {
nic_dyndns2_update ($cfg, $ip);
}
}