forked from ddclient/ddclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddclient.in
executable file
·7663 lines (7021 loc) · 306 KB
/
ddclient.in
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
######################################################################
#
# DDCLIENT - a Perl client for updating DynDNS information
#
# Original Author: Paul Burry ([email protected])
# Current maintainers:
# Reuben Thomas <[email protected]>
# Lenard Heß <[email protected]>
#
# website: https://github.com/ddclient/ddclient
#
######################################################################
package ddclient;
require v5.10.1;
use strict;
use warnings;
use Data::Dumper;
use File::Basename;
use File::Path qw(make_path);
use File::Temp;
use Getopt::Long;
use Sys::Hostname;
# Declare the ddclient version number.
#
# Perl's version strings do not support pre-release versions (alpha/development, beta, or release
# candidate) very well. The best it does is an optional underscore between arbitrary digits in the
# final component (e.g., "v1.2.3_4"). The underscore doesn't behave as most developers expect; it
# is treated as if it never existed (e.g., "v1.2.3_4" becomes "v1.2.34") except:
#
# * $v->is_alpha() will return true
# * $v->is_strict() will return false
# * $v->stringify() preserves the underscore (in its original position)
#
# Note that version::normal and version::numify lose information because the underscore is
# effectively removed.
#
# To work around Perl's limitations, human-readable versions are translated to/from Perl versions
# as follows:
#
# Human-readable Perl version Notes
# -------------------------------------------------------------------------------------------
# 1.2.3~alpha v1.2.3.0_0 compares equal to Perl version v1.2.3 (unfortunately)
# 1.2.3~betaN v1.2.3.0_N 1 <= N < 900; compares equal to Perl v1.2.3.N
# 1.2.3~rcN v1.2.3.0_M 1 <= N < 99; M = N + 900; compares equal to Perl v1.2.3.M
# 1.2.3 v1.2.3.999 for releases; no underscore in Perl version string
# 1.2.3rN v1.2.3.999.N 1 <= N < 1000; for re-releases, if necessary (rare)
#
# A tilde is used to separate "alpha", "beta", and "rc" from the version numbers because it has
# special meaning for the version comparison algorithms in RPM and Debian:
# https://docs.fedoraproject.org/en-US/packaging-guidelines/Versioning/#_handling_non_sorting_versions_with_tilde_dot_and_caret
# https://manpages.debian.org/bookworm/dpkg-dev/deb-version.7.en.html
#
# No period separator is required between "beta", "rc", or "r" and its adjacent number(s); both RPM
# and Debian will compare the adjacent number numerically, not lexicographically ("~beta2" sorts
# before "~beta10" as expected).
#
# The Perl version is declared first then converted to a human-readable form. It would be nicer to
# declare a human-readable version string and convert that to a Perl version string, but various
# tools in the Perl ecosystem require the line of source code that defines the VERSION variable to
# be self-contained (because they grep the source code and evaluate only that one line).
#
# For consistency and to match user expectations, the release part of the version is always three
# components: MAJOR.MINOR.PATCH.
use version 0.77; our $VERSION = version->declare('v4.0.0.0_0');
sub parse_version {
my ($v) = @_;
# Matches a non-negative integer with 1-3 decimal digits (zero padding disallowed).
my $n = qr/0|[1-9]\d{0,2}/;
my $vre = qr/
^
v # required "v" prefix
((?:$n\.)*?$n) # release version (e.g., 1.2, 1.2.3, or 1.2.3.4)
\.(?: # release or pre-release suffix
0_(?!999)($n)| # pre-release (alpha, beta, rc) revision
999(?:\.($n))? # release with optional re-release revision
)
$
/x;
return $v =~ $vre;
}
sub humanize_version {
my ($v) = @_;
my ($r, $pr, $rr) = parse_version($v);
return $v if !defined($r);
$v = $r;
if (!defined($pr)) {
$v .= "r$rr" if defined($rr);
} elsif ($pr eq '0') {
$v .= '~alpha';
} elsif ($pr < 900) {
$v .= "~beta$pr";
} elsif ($pr < 999) {
$v .= '~rc' . ($pr - 900);
}
return $v;
}
our $version = humanize_version($VERSION);
my $programd = $0;
$programd =~ s%^.*/%%;
our $program = $programd;
$program =~ s/d$//;
our $now = time;
my $hostname = hostname();
# subst_var(subst, default) returns subst unless it looks like @foo@ in which case it returns
# default. The @foo@ strings are expected to be replaced by make; this function makes it possible
# to run this file as a Perl script before those substitutions are made.
sub subst_var {
my ($subst, $default) = @_;
return $default if $subst =~ qr'^@\w+@$';
return $subst;
}
my $etc = subst_var('@sysconfdir@', '/etc/ddclient');
my $cachedir = subst_var('@localstatedir@', '/var') . '/cache/ddclient';
our @curl = (subst_var('@CURL@', 'curl'));
our $emailbody = '';
my $last_emailbody = '';
## If run as *d (e.g., ddclientd) then daemonize by default (but allow
## flags and options to override).
my $daemon_default = ($programd =~ /d$/) ? interval('5m') : undef;
# Current Logger instance. To push a context prefix onto the context stack:
# local _l = pushlogctx('additional context goes here');
our $_l = ddclient::Logger->new();
$ENV{'PATH'} = (exists($ENV{PATH}) ? "$ENV{PATH}:" : "") . "/sbin:/usr/sbin:/bin:/usr/bin:/etc:/usr/lib:";
our %globals;
our %config;
# `%recap` holds details about recent updates (and attempts) that are needed to implement various
# service-specific and protocol-independent mechanisms such as `min-interval`. This data is
# persisted in the cache file (`--cache`) so that it survives ddclient restarts. This hash maps a
# hostname to a hashref with entries that map variable names to values. Only entries for the
# host's "recap variables" -- those declared in the host's protocol's `recapvars` property -- are
# included.
#
# There are two classes of recap variables:
# * "Status" variables: These track update success/failure, the IP address of the last successful
# update, etc. These do not hold configuration data; they are unrelated to any entries in
# `%config`.
# * "Configuration change detection" variables: These are used to force an update if the value in
# the same-named entry in `%config` has changed since the previous update attempt. The value
# stored in `%config` is the desired setting; the value in `%recap` is the desired setting as
# it was just before the previous update attempt. Values are synchronized from `%config` to
# `%recap` during each update attempt.
#
# A protocol's set of config change detection variables can be found in the protocol's
# `force_update_if_changed` property; all other recap variables are assumed to be status variables.
#
# A note about terminology: This was previously named `%cache`, but "cache" implies that the
# purpose is to reduce the cost or latency of data retrieval or computation, and that deletion only
# affects performance. That is not the case here, so the variable was renamed. "Recap" is meant
# to evoke the "previously on" clips that play before TV episodes, which are designed to give you
# just enough context to recall the state. The recap is written to the cache file, so-named for
# historical reasons. (Renaming "cache file" to "recap file" is more difficult due to
# compatibility concerns with the public `--cache` option.)
our %recap;
my $result;
my $saved_recap;
my %saved_opt;
my $daemon;
# Control how many times warning message logged for invalid IP addresses
my (%warned_ipv4, %warned_ipv6);
sub repr {
my $vals = @_ % 2 ? [shift] : [];
my %opts = @_;
my $d = Data::Dumper->new($vals)->Sortkeys(1)->Terse(!exists($opts{Names}))->Useqq(1);
$d->$_($opts{$_}) for keys(%opts);
return $d->Dump();
}
sub T_ANY { 'any' }
sub T_STRING { 'string' }
sub T_EMAIL { 'e-mail address' }
sub T_NUMBER { 'number' }
sub T_DELAY { 'time delay (ie. 1d, 1hour, 1m)' }
sub T_LOGIN { 'login' }
sub T_PASSWD { 'password' }
sub T_BOOL { 'boolean value' }
sub T_FQDN { 'fully qualified host name' }
sub T_FILE { 'file name' }
sub T_FQDNP { 'fully qualified host name and optional port number' }
sub T_PROTO { 'protocol' }
sub T_URL { 'url including fully qualified host name, optional port number, and path' }
sub T_USE { 'ip strategy' }
sub T_USEV4 { 'ipv4 strategy' }
sub T_USEV6 { 'ipv6 strategy' }
sub T_IF { 'interface' }
sub T_PROG { 'program name' }
sub T_IP { 'ip' }
sub T_IPV4 { 'ipv4' }
sub T_IPV6 { 'ipv6' }
sub T_POSTS { 'postscript' }
# `%recapvars` contains common recap variable declarations that are used by multiple protocols (see
# the protocol `recapvars` property).
our %recapvars = (
'common' => {
'host' => T_STRING,
'protocol' => T_PROTO,
# The IPv4 address most recently saved at the DDNS service.
# TODO: This is independent of the `ipv4` configuration setting. Rename the `%recap`
# status variable to something like `saved-ipv4` to avoid confusion with the `%config`
# setting variable.
'ipv4' => T_IPV4,
# As `ipv4`, but for an IPv6 address.
'ipv6' => T_IPV6,
# Timestamp (seconds since epoch) indicating the earliest time the next update is
# permitted.
# TODO: Create a timestamp type and change this to that type.
'wtime' => T_NUMBER,
# Timestamp (seconds since epoch) indicating when an IP address was last sent to the DDNS
# service, even if the IP address was not different from what was already stored.
# TODO: Create a timestamp type and change this to that type.
'mtime' => T_NUMBER,
# Timestamp (seconds since epoch) of the most recent attempt to update the DDNS service
# (including attempts to update with the same IP address). This equals mtime if the most
# recent attempt was successful, otherwise it will be more recent than mtime.
# TODO: Create a timestamp type and change this to that type.
'atime' => T_NUMBER,
# Disposition of the most recent (or currently in progress) attempt to update the DDNS
# service with the IP address in `wantipv4`. Anything other than `good`, including undef,
# is treated as a failure.
'status-ipv4' => T_ANY,
# As `status-ipv4`, but with `wantipv6`.
'status-ipv6' => T_ANY,
# Timestamp (seconds since epoch) of the most recent attempt that would have been made had
# `min-interval` not inhibited the attempt. This is reset to 0 once an attempt is actually
# made. This is used as a boolean to suppress repeated warnings to the user that indicate
# that `min-interval` has inhibited an update attempt.
# TODO: Change to a boolean and rename to improve readability.
'warned-min-interval' => T_ANY,
# Timestamp (seconds since epoch) of the most recent attempt that would have been made had
# `min-error-interval` not inhibited the attempt. This is reset to 0 once an attempt is
# actually made. This is used as a boolean to suppress repeated warnings to the user that
# indicate that `min-error-interval` has inhibited an update attempt.
# TODO: Change to a boolean and rename to improve readability.
'warned-min-error-interval' => T_ANY,
},
'dyndns-common' => {
'backupmx' => T_BOOL,
'mx' => T_FQDN,
'wildcard' => T_BOOL,
},
);
## strategies for obtaining an ip address.
our %builtinweb = (
'dyndns' => {'url' => 'http://checkip.dyndns.org/', 'skip' => 'Current IP Address:'},
'freedns' => {'url' => 'https://freedns.afraid.org/dynamic/check.php'},
'he' => {
url => 'https://checkip.dns.he.net/',
deprecated => "Use 'he.net' instead.",
},
'he.net' => {'url' => 'https://checkip.dns.he.net/'},
'ip4only.me' => {'url' => 'https://ip4only.me/api/'},
'ip6only.me' => {'url' => 'https://ip6only.me/api/'},
'ipify-ipv4' => {'url' => 'https://api.ipify.org/'},
'ipify-ipv6' => {'url' => 'https://api6.ipify.org/'},
'loopia' => {'url' => 'https://dns.loopia.se/checkip/checkip.php', 'skip' => 'Current IP Address:'},
'myonlineportal' => {'url' => 'https://myonlineportal.net/checkip'},
'noip-ipv4' => {'url' => 'http://ip1.dynupdate.no-ip.com/'},
'noip-ipv6' => {'url' => 'http://ip1.dynupdate6.no-ip.com/'},
'nsupdate.info-ipv4' => {'url' => 'https://ipv4.nsupdate.info/myip'},
'nsupdate.info-ipv6' => {'url' => 'https://ipv6.nsupdate.info/myip'},
'zoneedit' => {'url' => 'https://dynamic.zoneedit.com/checkip.html'},
);
sub query_cisco {
my ($asa, $v4, %p) = @_;
warning("'--if' is deprecated; use '--ifv4' instead")
if ($v4 && !defined($p{'ifv4'}) && defined($p{'if'}));
my $if = ($v4 ? $p{'ifv4'} : undef) // $p{'if'};
my $fw = ($v4 ? $p{'fwv4'} : undef) // $p{'fw'};
# Convert slashes to protected value "\/"
$if =~ s%\/%\\\/%g;
# Protect special HTML characters (like '?')
$if =~ s/([\?&= ])/sprintf("%%%02x", ord($1))/ge;
my $reply = geturl(
url => ($asa)
? "https://$fw/exec/show%20interface%20$if"
: "http://$fw/level/1/exec/show/ip/interface/brief/$if/CR",
login => $p{'fw-login'},
password => $p{'fw-password'},
ignore_ssl_option => 1,
ssl_validate => $p{'fw-ssl-validate'},
);
return undef if !header_ok($reply, \&warning);
$reply =~ s/^.*?\n\n//s;
return $reply;
}
our %builtinfw = (
'2wire' => {
'name' => '2Wire 1701HG Gateway',
'url' => '/xslt?PAGE=B01',
'skip' => 'Internet Address:',
},
'3com-3c886a' => {
'name' => '3com 3c886a 56k Lan Modem',
'url' => '/stat3.htm',
'skip' => 'IP address in use',
},
'3com-oc-remote812' => {
'name' => '3com OfficeConnect Remote 812',
'url' => '/callEvent',
'skip' => '.*LOCAL',
},
'alcatel-510' => {
'name' => 'Alcatel Speed Touch 510',
'url' => '/cgi/ip/',
'skip' => 'ppp',
},
'alcatel-530' => {
'name' => 'Alcatel/Thomson SpeedTouch 530',
'url' => '/cgi/status/',
'skip' => 'IP Address',
},
'alcatel-stp' => {
'name' => 'Alcatel Speed Touch Pro',
'url' => '/cgi/router/',
'skip' => 'Brt',
},
'allnet-1298' => {
'name' => 'Allnet 1298',
'url' => '/cgi/router/',
'skip' => 'WAN',
},
'cayman-3220h' => {
'name' => 'Cayman 3220-H DSL',
'url' => '/shell/show+ip+interfaces',
'skip' => '.*inet',
},
'cisco' => {
'name' => 'Cisco FW',
'query' => sub { return query_cisco(0, 0, @_); },
'inputs' => ['fw', 'if', 'fw-login', 'fw-password', 'fw-ssl-validate'],
'queryv4' => sub { return query_cisco(0, 1, @_); },
'inputsv4' => ['fwv4', 'fw', 'ifv4', 'if', 'fw-login', 'fw-password', 'fw-ssl-validate'],
'help' => sub { return " at the host given by --fw$_[0]=<host> and interface given by --if$_[0]=<interface>"; },
},
'cisco-asa' => {
'name' => 'Cisco ASA',
'query' => sub { return query_cisco(1, 0, @_); },
'inputs' => ['fw', 'if', 'fw-login', 'fw-password', 'fw-ssl-validate'],
'queryv4' => sub { return query_cisco(1, 1, @_); },
'inputsv4' => ['fwv4', 'fw', 'ifv4', 'if', 'fw-login', 'fw-password', 'fw-ssl-validate'],
'help' => sub { return " at the host given by --fw$_[0]=<host> and interface given by --if$_[0]=<interface>"; },
},
'dlink-524' => {
'name' => 'D-Link DI-524',
'url' => '/st_device.html',
'skip' => 'WAN.*?Addres',
},
'dlink-604' => {
'name' => 'D-Link DI-604',
'url' => '/st_devic.html',
'skip' => 'WAN.*?IP.*Address',
},
'dlink-614' => {
'name' => 'D-Link DI-614+',
'url' => '/st_devic.html',
'skip' => 'WAN',
},
'e-tech' => {
'name' => 'E-tech Router',
'url' => '/Status.htm',
'skip' => 'Public IP Address',
},
'elsa-lancom-dsl10' => {
'name' => 'ELSA LanCom DSL/10 DSL FW',
'url' => '/config/1/6/8/3/',
'skip' => 'IP.Address',
},
'elsa-lancom-dsl10-ch01' => {
'name' => 'ELSA LanCom DSL/10 DSL FW (isdn ch01)',
'url' => '/config/1/6/8/3/',
'skip' => 'IP.Address.*?CH01',
},
'elsa-lancom-dsl10-ch02' => {
'name' => 'ELSA LanCom DSL/10 DSL FW (isdn ch01)',
'url' => '/config/1/6/8/3/',
'skip' => 'IP.Address.*?CH02',
},
'linksys' => {
'name' => 'Linksys FW',
'url' => '/Status.htm',
'skip' => 'WAN.*?Address',
},
'linksys-rv042-wan1' => {
'name' => 'Linksys RV042 Dual Homed Router WAN Port 2',
'url' => '/home.htm',
'skip' => 'WAN1 IP',
},
'linksys-rv042-wan2' => {
'name' => 'Linksys RV042 Dual Homed Router WAN Port 2',
'url' => '/home.htm',
'skip' => 'WAN2 IP',
},
'linksys-ver2' => {
'name' => 'Linksys FW version 2',
'url' => '/RouterStatus.htm',
'skip' => 'WAN.*?Address',
},
'linksys-ver3' => {
'name' => 'Linksys FW version 3',
'url' => '/Status_Router.htm',
'skip' => 'WAN.*?Address',
},
'linksys-wcg200' => {
'name' => 'Linksys WCG200 FW',
'url' => '/RgStatus.asp',
'skip' => 'WAN.IP.*?Address',
},
'linksys-wrt854g' => {
'name' => 'Linksys WRT854G FW',
'url' => '/Status_Router.asp',
'skip' => 'IP Address:',
},
'maxgate-ugate3x00' => {
'name' => 'MaxGate UGATE-3x00 FW',
'url' => '/Status.htm',
'skip' => 'WAN.*?IP Address',
},
'netcomm-nb3' => {
'name' => 'NetComm NB3',
'url' => '/MainPage?id=6',
'skip' => 'ppp-0',
},
'netgear-dg834g' => {
'name' => 'netgear-dg834g',
'url' => '/setup.cgi?next_file=s_status.htm&todo=cfg_init',
'skip' => '',
},
'netgear-rp614' => {
'name' => 'Netgear RP614 FW',
'url' => '/sysstatus.html',
'skip' => 'IP Address',
},
'netgear-rt3xx' => {
'name' => 'Netgear FW',
'url' => '/mtenSysStatus.html',
'skip' => 'IP Address',
},
'netgear-wgt624' => {
'name' => 'Netgear WGT624',
'url' => '/RST_st_dhcp.htm',
'skip' => 'IP Address</B></td><TD NOWRAP width="50%">',
},
'netgear-wpn824' => {
'name' => 'Netgear WPN824 FW',
'url' => '/RST_status.htm',
'skip' => 'IP Address',
},
'netopia-r910' => {
'name' => 'Netopia R910 FW',
'url' => '/WanEvtLog',
'skip' => 'local:',
},
'olitec-SX200' => {
'name' => 'olitec-SX200',
'url' => '/doc/wan.htm',
'skip' => 'st_wan_ip[0] = "',
},
'rtp300' => {
'name' => 'Linksys RTP300',
'url' => '/cgi-bin/webcm?getpage=%2Fusr%2Fwww_safe%2Fhtml%2Fstatus%2FRouter.html',
'skip' => 'Internet.*?IP Address',
},
'siemens-ss4200' => {
'name' => 'Siemens SpeedStream 4200',
'url' => '/summary.htm',
'skip' => '',
},
'sitecom-dc202' => {
'name' => 'Sitecom DC-202 FW',
'url' => '/status.htm',
'skip' => 'Internet IP Address',
},
'smc-barricade' => {
'name' => 'SMC Barricade FW',
'url' => '/status.htm',
'skip' => 'IP Address',
},
'smc-barricade-7004vbr' => {
'name' => 'SMC Barricade FW (7004VBR model config)',
'url' => '/status_main.stm',
'skip' => 'var wan_ip=',
},
'smc-barricade-7401bra' => {
'name' => 'SMC Barricade 7401BRA FW',
'url' => '/admin/wan1.htm',
'skip' => 'IP Address',
},
'smc-barricade-alt' => {
'name' => 'SMC Barricade FW (alternate config)',
'url' => '/status.HTM',
'skip' => 'WAN IP',
},
'sohoware-nbg800' => {
'name' => 'SOHOWare BroadGuard NBG800',
'url' => '/status.htm',
'skip' => 'Internet IP',
},
'sveasoft' => {
'name' => 'Sveasoft WRT54G/WRT54GS',
'url' => '/Status_Router.asp',
'skip' => 'var wan_ip',
},
'thomson-st536v6' => {
'name' => 'Thomson SpeedTouch 536v6',
'url' => '/cgi/b/is/',
'skip' => 'IP Address',
},
'thomson-tg782' => {
'name' => 'Thomson/Technicolor TG782',
'url' => '/cgi/b/is/',
'skip' => 'IP Address',
},
'vigor-2200usb' => {
'name' => 'Vigor 2200 USB',
'url' => '/doc/online.sht',
'skip' => 'PPPoA',
},
'watchguard-edge-x' => {
'name' => 'Watchguard Edge X FW',
'url' => '/netstat.htm',
'skip' => 'inet addr:',
},
'watchguard-soho' => {
'name' => 'Watchguard SOHO FW',
'url' => '/pubnet.htm',
'skip' => 'NAME=IPAddress VALUE=',
},
'westell-6100' => {
'name' => 'Westell C90-610015-06 DSL Router',
'url' => '/advstat.htm',
'skip' => 'IP.+?Address',
},
'xsense-aero' => {
'name' => 'Xsense Aero',
'url' => '/A_SysInfo.htm',
'skip' => 'WAN.*?IP Address',
},
);
sub builtinfw_strategy {
my ($n) = @_;
my $fw = $builtinfw{$n};
return ($n => {
help => ": deprecated, see '--usev4=$n'" .
(defined($fw->{queryv6}) ? " and '--usev6=$n'" : ''),
inputs => $fw->{inputs} // ['fw', 'fw-skip', 'fw-login', 'fw-password', 'fw-ssl-validate'],
});
}
our %ip_strategies = (
'disabled' => {help => ": do not use a deprecated method to obtain an IP address for this host",
inputs => []},
'no' => {help => ": deprecated, see '--use=disabled'",
inputs => []},
'ip' => {help => ": deprecated, see '--usev4=ipv4' and '--usev6=ipv6'",
inputs => ['ip']},
'web' => {help => ": deprecated, see '--usev4=webv4' and '--usev6=webv6'",
inputs => ['web', 'web-skip', 'proxy', 'web-ssl-validate']},
'fw' => {help => ": deprecated, see '--usev4=fwv4' and '--usev6=fwv6'",
inputs => ['fw', 'fw-skip', 'fw-login', 'fw-password', 'fw-ssl-validate']},
'if' => {help => ": deprecated, see '--usev4=ifv4' and '--usev6=ifv6'",
inputs => ['if']},
'cmd' => {help => ": deprecated, see '--usev4=cmdv4' and '--usev6=cmdv6'",
inputs => ['cmd', 'cmd-skip']},
map(builtinfw_strategy($_), keys(%builtinfw)),
);
sub ip_strategies_usage {
return map({ sprintf(" --use=%-22s %s.", $_, $ip_strategies{$_}{help}); }
'disabled', 'no', 'ip', 'web', 'if', 'cmd', 'fw', sort(keys(%builtinfw)));
}
sub builtinfwv4_strategy {
my ($n) = @_;
my $fw = $builtinfw{$n};
return ($n => {
help => defined($fw->{queryv4})
? ": obtain IPv4 from $fw->{name}@{[($fw->{help} // sub {})->('v4') // '']}"
: ": obtain IPv4 from $fw->{name} at the host or URL given by --fwv4=<host|URL>",
inputs => $fw->{inputsv4} // ['fwv4', 'fw', 'fwv4-skip', 'fw-skip', 'fw-login',
'fw-password', 'fw-ssl-validate'],
});
}
our %ipv4_strategies = (
'disabled' => {help => ": do not obtain an IPv4 address for this host (except possibly via the deprecated '--use' option, if it is enabled)",
inputs => []},
'ipv4' => {help => ": obtain IPv4 from the address given by --ipv4=<address>",
inputs => ['ipv4']},
'webv4' => {help => ": obtain IPv4 from an IP discovery page on the web",
inputs => ['webv4', 'webv4-skip', 'proxy', 'web-ssl-validate']},
'ifv4' => {help => ": obtain IPv4 from the interface given by --ifv4=<interface>",
inputs => ['ifv4']},
'cmdv4' => {help => ": obtain IPv4 from the command given by --cmdv4=<command>",
inputs => ['cmdv4', 'cmd-skip']},
'fwv4' => {help => ": obtain IPv4 from the URL given by --fwv4=<URL>",
inputs => ['fwv4', 'fw', 'fwv4-skip', 'fw-skip', 'fw-login', 'fw-password', 'fw-ssl-validate']},
map(builtinfwv4_strategy($_), keys(%builtinfw)),
);
sub ipv4_strategies_usage {
return map({ sprintf(" --usev4=%-22s %s.", $_, $ipv4_strategies{$_}{help}) }
'disabled', 'ipv4', 'webv4', 'ifv4', 'cmdv4', 'fwv4', sort(keys(%builtinfw)));
}
sub builtinfwv6_strategy {
my ($n) = @_;
my $fw = $builtinfw{$n};
return defined($fw->{queryv6})
? ($n => {
help => ": obtain IPv6 from $fw->{name}@{[($fw->{help} // sub {})->('v6') // '']}",
inputs => $fw->{inputsv6} // ['fwv6', 'fwv6-skip'],
})
: ();
}
our %ipv6_strategies = (
'disabled' => {help => ": do not obtain an IPv6 address for this host (except possibly via the deprecated '--use' option, if it is enabled)",
inputs => []},
'no' => {help => ": deprecated, use '--usev6=disabled'",
inputs => []},
'ipv6' => {help => ": obtain IPv6 from the address given by --ipv6=<address>",
inputs => ['ipv6', 'ip']},
'ip' => {help => ": deprecated, use '--usev6=ipv6'",
inputs => ['ipv6', 'ip']},
'webv6' => {help => ": obtain IPv6 from an IP discovery page on the web",
inputs => ['webv6', 'web', 'webv6-skip', 'web-skip', 'proxy', 'web-ssl-validate']},
'web' => {help => ": deprecated, use '--usev6=webv6'",
inputs => ['webv6', 'web', 'webv6-skip', 'web-skip', 'proxy', 'web-ssl-validate']},
'ifv6' => {help => ": obtain IPv6 from the interface given by --ifv6=<interface>",
inputs => ['ifv6', 'if']},
'if' => {help => ": deprecated, use '--usev6=ifv6'",
inputs => ['ifv6', 'if']},
'cmdv6' => {help => ": obtain IPv6 from the command given by --cmdv6=<command>",
inputs => ['cmdv6', 'cmd', 'cmd-skip']},
'cmd' => {help => ": deprecated, use '--usev6=cmdv6'",
inputs => ['cmdv6', 'cmd', 'cmd-skip']},
'fwv6' => {help => ": obtain IPv6 from the URL given by --fwv6=<URL>",
inputs => ['fwv6', 'fwv6-skip']},
map(builtinfwv6_strategy($_), keys(%builtinfw)),
);
sub ipv6_strategies_usage {
return map({ sprintf(" --usev6=%-22s %s.", $_, $ipv6_strategies{$_}{help}) }
'disabled', 'no', 'ipv6', 'ip', 'webv6', 'web', 'ifv6', 'if', 'cmdv6', 'cmd',
'fwv6', sort(map({exists($ipv6_strategies{$_}) ? ($_) : ()} keys(%builtinfw))));
}
sub setv {
return {
'type' => shift,
'required' => shift,
'default' => shift,
'minimum' => shift,
};
}
our %cfgvars = (
'global-defaults' => {
'daemon' => setv(T_DELAY, 0, $daemon_default, interval('60s')),
'foreground' => setv(T_BOOL, 0, 0, undef),
'file' => setv(T_FILE, 0, "$etc/$program.conf", undef),
'cache' => setv(T_FILE, 0, "$cachedir/$program.cache", undef),
'pid' => setv(T_FILE, 0, undef, undef),
'proxy' => setv(T_FQDNP, 0, undef, undef),
'protocol' => setv(T_PROTO, 0, 'dyndns2', undef),
'timeout' => setv(T_DELAY, 0, interval('120s'), interval('120s')),
'force' => setv(T_BOOL, 0, 0, undef),
'ssl' => setv(T_BOOL, 0, 1, undef),
'syslog' => setv(T_BOOL, 0, 0, undef),
'facility' => setv(T_STRING,0, 'daemon', undef),
'priority' => setv(T_STRING,0, 'notice', undef),
'mail' => setv(T_EMAIL, 0, undef, undef),
'mail-failure' => setv(T_EMAIL, 0, undef, undef),
'max-warn' => setv(T_NUMBER,0, 1, undef),
'exec' => setv(T_BOOL, 0, 1, undef),
'debug' => setv(T_BOOL, 0, 0, undef),
'verbose' => setv(T_BOOL, 0, 0, undef),
'quiet' => setv(T_BOOL, 0, 0, undef),
'test' => setv(T_BOOL, 0, 0, undef),
'postscript' => setv(T_POSTS, 0, undef, undef),
'ssl_ca_dir' => setv(T_FILE, 0, undef, undef),
'ssl_ca_file' => setv(T_FILE, 0, undef, undef),
'redirect' => setv(T_NUMBER,0, 0, undef)
},
'protocol-common-defaults' => {
'server' => setv(T_FQDNP, 0, 'members.dyndns.org', undef),
'login' => setv(T_LOGIN, 1, undef, undef),
'password' => setv(T_PASSWD,1, undef, undef),
'host' => setv(T_STRING,1, undef, undef),
'use' => setv(T_USE, 0, sub {
my ($h) = @_;
return "'disabled' if '--usev4' or '--usev6' is enabled, otherwise 'ip'"
if ($h // '') eq '<usage>';
return 'disabled' if opt('usev4', $h) ne 'disabled' || opt('usev6', $h) ne 'disabled';
return 'ip';
}, undef),
'usev4' => setv(T_USEV4, 0, 'disabled', undef),
'usev6' => setv(T_USEV6, 0, 'disabled', undef),
'if' => setv(T_IF, 0, 'ppp0', undef),
'ifv4' => setv(T_IF, 0, 'default', undef),
'ifv6' => setv(T_IF, 0, 'default', undef),
'web' => setv(T_STRING,0, 'dyndns', undef),
'web-skip' => setv(T_STRING,0, undef, undef),
'web-ssl-validate' => setv(T_BOOL, 0, 1, undef),
'webv4' => setv(T_STRING,0, 'ipify-ipv4', undef),
'webv4-skip' => setv(T_STRING,0, undef, undef),
'webv6' => setv(T_STRING,0, 'ipify-ipv6', undef),
'webv6-skip' => setv(T_STRING,0, undef, undef),
'fw' => setv(T_ANY, 0, undef, undef),
'fw-skip' => setv(T_STRING,0, undef, undef),
'fw-login' => setv(T_LOGIN, 0, undef, undef),
'fw-password' => setv(T_PASSWD,0, undef, undef),
'fw-ssl-validate' => setv(T_BOOL, 0, 1, undef),
'fwv4' => setv(T_ANY, 0, undef, undef),
'fwv4-skip' => setv(T_STRING,0, undef, undef),
'fwv6' => setv(T_ANY, 0, undef, undef),
'fwv6-skip' => setv(T_STRING,0, undef, undef),
'cmd' => setv(T_PROG, 0, undef, undef),
'cmd-skip' => setv(T_STRING,0, undef, undef),
'cmdv4' => setv(T_PROG, 0, undef, undef),
'cmdv6' => setv(T_PROG, 0, undef, undef),
'min-interval' => setv(T_DELAY, 0, interval('30s'), 0),
'max-interval' => setv(T_DELAY, 0, interval('25d'), 0),
'min-error-interval' => setv(T_DELAY, 0, interval('5m'), 0),
'ip' => setv(T_IP, 0, undef, undef),
'ipv4' => setv(T_IPV4, 0, undef, undef),
'ipv6' => setv(T_IPV6, 0, undef, undef),
},
'dyndns-common-defaults' => {
'backupmx' => setv(T_BOOL, 0, 0, undef),
'mx' => setv(T_FQDN, 0, undef, undef),
'wildcard' => setv(T_BOOL, 0, 0, undef),
},
);
{
package ddclient::Protocol;
# Keyword arguments:
# * `update`: Required coderef that takes `($self, @hosts)` and updates the given hosts.
# * `examples`: Required coderef that takes `($self)` and returns a string showing
# configuration examples for using the protocol.
# * `cfgvars`: Optional hashref of configuration variable declarations. If omitted or
# `undef`, `$cfgvars{'protocol-common-defaults'}` is used.
# * `recapvars`: Optional hashref of recap variable declarations. If omitted or `undef`,
# `$recapvars{'common'}` is used.
# * `force_update`: Optional coderef that takes `($self, $h)` and returns truthy to force the
# given host to update. Omitting or passing `undef` is equivalent to passing a subroutine
# that always returns falsy.
# * `force_update_if_changed`: Optional arrayref of variable names to watch for changes. If
# any of the named values in `%config` have changed since the previous update attempt
# (successful or not), the host update is forced. If omitted or `undef`, an empty array is
# used.
sub new {
my ($class, %args) = @_;
my $self = bless({%args}, $class);
# Set defaults and normalize.
$self->{cfgvars} //= $ddclient::cfgvars{'protocol-common-defaults'};
$self->{recapvars} //= $ddclient::recapvars{'common'};
for my $varset (qw(cfgvars recapvars)) {
$self->{$varset} = {%{$self->{$varset}}}; # Shallow clone.
# Delete `undef` variable declarations to make it easier to cancel previously declared
# variables.
delete($self->{$varset}{$_}) for grep(!defined($self->{$varset}{$_}),
keys(%{$self->{$varset}}));
}
$self->{force_update} //= sub { return 0; };
$self->{force_update_if_changed} //= [];
# Eliminate duplicates and non-recap variables.
my %fvs = map({ ($_ => undef); } @{$self->{force_update_if_changed}}, 'protocol');
$self->{force_update_if_changed} =
[grep({ $self->{cfgvars}{$_} && $self->{recapvars}{$_}; } sort(keys(%fvs)))];
return $self;
}
sub force_update {
my ($self, $h) = @_;
my @changed = grep({
my $rv = $ddclient::recap{$h}{$_};
my $cv = ddclient::opt($_, $h);
return defined($rv) && defined($cv) && $rv ne $cv;
} @{$self->{force_update_if_changed}});
if (@changed) {
ddclient::info("update forced because options changed: " . join(', ', @changed));
return 1;
}
my $f = $self->{force_update};
return $f if ref($f) ne 'CODE';
return $f->($self, $h);
}
sub update {
my ($self, @hosts) = @_;
for my $h (@hosts) {
$ddclient::recap{$h}{'atime'} = $now;
delete($ddclient::recap{$h}{$_}) for qw(status-ipv4 status-ipv6 wtime
warned-min-interval warned-min-error-interval);
# Update the configuration change detection variables. The vars are updated regardless
# of whether the update actually succeeds because update failures should be retried at
# the error retry rate (`min-error-interval`), not forced by `force_update`. Notes
# about why the recap vars are updated here in this method:
# * The vars must not be updated if the host is not being updated because change
# detection is defined relative to the previous update attempt. In particular,
# these can't be updated when the protocol's `force_update` method is called
# because that method is not always called before an update is attempted.
# * The vars must be updated after the `force_update` method would be called so that
# `force_update` can check whether any settings have changed since the last time an
# update was attempted.
# * The vars are updated before the protocol's `update` method is called so that
# `update` sees consistent values between `%recap` and `%config`. This reduces the
# impact of Hyrum's Law; if a protocol needs a variable to be updated after the
# `update` method is called then that behavior should be made explicit.
for my $v (@{$self->{force_update_if_changed}}) {
if (defined(my $val = ddclient::opt($v, $h))) {
$ddclient::recap{$h}{$v} = $val;
} else {
# Entries in `%recap` with `undef` values are deleted to avoid needing to
# figure out how to represent `undef` in the cache file and to simplify
# testing.
delete($ddclient::recap{$h}{$v});
}
}
}
$self->_update(@hosts);
}
sub _update {
my $self = shift;
$self->{update}($self, @_);
}
sub examples {
my ($self) = @_;
return $self->{examples}($self);
}
}
{
# A legacy protocol implementation reads `$config{$h}{wantip}` and sets `$recap{$h}{status}`
# and `$recap{$h}{ip}`, rather than reading `wantipv4` and `wantipv6` and setting
# `status-ipv4`, `status-ipv6`, `ipv4`, and `ipv6`.
package ddclient::LegacyProtocol;
use parent qw(-norequire ddclient::Protocol);
sub _update {
my ($self, @hosts) = @_;
my %ipv;
for my $h (@hosts) {
$ipv{$h} = defined($ddclient::config{$h}{'wantipv4'}) ? '4' : '6';
$ddclient::config{$h}{'wantip'} //= delete($ddclient::config{$h}{"wantipv$ipv{$h}"});
delete($ddclient::recap{$h}{$_}) for qw(ip status);
}
$self->SUPER::_update(@hosts);
for my $h (@hosts) {
local $ddclient::_l = ddclient::pushlogctx($h);
delete($ddclient::config{$h}{'wantip'});
ddclient::debug(
"legacy protocol; moving 'status' to 'status-ipv$ipv{$h}', 'ip' to 'ipv$ipv{$h}'");
$ddclient::recap{$h}{"status-ipv$ipv{$h}"} = delete($ddclient::recap{$h}{'status'});
$ddclient::recap{$h}{"ipv$ipv{$h}"} = delete($ddclient::recap{$h}{'ip'});
}
}
}
our %protocols = (
'1984' => ddclient::LegacyProtocol->new(
'update' => \&nic_1984_update,
'examples' => \&nic_1984_examples,
'cfgvars' => {
%{$cfgvars{'protocol-common-defaults'}},
'login' => undef,
'server' => setv(T_FQDNP, 0, 'api.1984.is', undef),
},
),
'changeip' => ddclient::LegacyProtocol->new(
'update' => \&nic_changeip_update,
'examples' => \&nic_changeip_examples,
'cfgvars' => {
%{$cfgvars{'protocol-common-defaults'}},
'server' => setv(T_FQDNP, 0, 'nic.changeip.com', undef),
},
),
'cloudflare' => ddclient::Protocol->new(
'update' => \&nic_cloudflare_update,
'examples' => \&nic_cloudflare_examples,
'cfgvars' => {
%{$cfgvars{'protocol-common-defaults'}},
'login' => setv(T_LOGIN, 0, 'token', undef),
'min-interval' => setv(T_DELAY, 0, interval('5m'), 0),
'server' => setv(T_FQDNP, 0, 'api.cloudflare.com/client/v4', undef),
'zone' => setv(T_FQDN, 1, undef, undef),
},
),
'cloudns' => ddclient::LegacyProtocol->new(
'update' => \&nic_cloudns_update,
'examples' => \&nic_cloudns_examples,
'cfgvars' => {
%{$cfgvars{'protocol-common-defaults'}},
'login' => undef,
'password' => undef,
'dynurl' => setv(T_STRING, 1, undef, undef),
},
),
'ddns.fm' => ddclient::Protocol->new(
'update' => \&nic_ddnsfm_update,
'examples' => \&nic_ddnsfm_examples,
'cfgvars' => {
%{$cfgvars{'protocol-common-defaults'}},
'login' => undef,
'server' => setv(T_FQDNP, 0, 'https://api.ddns.fm', undef),
},
),
'digitalocean' => ddclient::Protocol->new(
'update' => \&nic_digitalocean_update,
'examples' => \&nic_digitalocean_examples,
'cfgvars' => {
%{$cfgvars{'protocol-common-defaults'}},
'login' => undef,
'server' => setv(T_FQDNP, 0, 'api.digitalocean.com', undef),
'zone' => setv(T_FQDN, 1, undef, undef),
},
),
'dinahosting' => ddclient::LegacyProtocol->new(
'update' => \&nic_dinahosting_update,
'examples' => \&nic_dinahosting_examples,
'cfgvars' => {
%{$cfgvars{'protocol-common-defaults'}},
'min-error-interval' => setv(T_DELAY, 0, interval('8m'), 0),
'script' => setv(T_STRING, 0, '/special/api.php', undef),
'server' => setv(T_FQDNP, 0, 'dinahosting.com', undef),
},
),
'directnic' => ddclient::Protocol->new(
'update' => \&nic_directnic_update,
'examples' => \&nic_directnic_examples,
'cfgvars' => {
%{$cfgvars{'protocol-common-defaults'}},
'login' => undef,
'password' => undef,
'urlv4' => setv(T_URL, 0, undef, undef),
'urlv6' => setv(T_URL, 0, undef, undef),
},
),
'dnsmadeeasy' => ddclient::LegacyProtocol->new(
'update' => \&nic_dnsmadeeasy_update,
'examples' => \&nic_dnsmadeeasy_examples,
'cfgvars' => {
%{$cfgvars{'protocol-common-defaults'}},
'script' => setv(T_STRING, 0, '/servlet/updateip', undef),
'server' => setv(T_FQDNP, 0, 'cp.dnsmadeeasy.com', undef),
},
),
'dondominio' => ddclient::LegacyProtocol->new(
'update' => \&nic_dondominio_update,
'examples' => \&nic_dondominio_examples,
'cfgvars' => {
%{$cfgvars{'protocol-common-defaults'}},
'server' => setv(T_FQDNP, 0, 'dondns.dondominio.com', undef),
},
),
'dslreports1' => ddclient::LegacyProtocol->new(
'update' => \&nic_dslreports1_update,
'examples' => \&nic_dslreports1_examples,
'cfgvars' => {
%{$cfgvars{'protocol-common-defaults'}},
'server' => setv(T_FQDNP, 0, 'www.dslreports.com', undef),
},
),
'domeneshop' => ddclient::Protocol->new(
'update' => \&nic_domeneshop_update,
'examples' => \&nic_domeneshop_examples,
'cfgvars' => {
%{$cfgvars{'protocol-common-defaults'}},
'server' => setv(T_FQDNP, 0, 'api.domeneshop.no', undef),
},
),
'duckdns' => ddclient::Protocol->new(
'update' => \&nic_duckdns_update,
'examples' => \&nic_duckdns_examples,