forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.c
2138 lines (1881 loc) · 68.4 KB
/
options.c
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
#include "config.h"
#include <ccan/array_size/array_size.h>
#include <ccan/cast/cast.h>
#include <ccan/err/err.h>
#include <ccan/json_escape/json_escape.h>
#include <ccan/mem/mem.h>
#include <ccan/noerr/noerr.h>
#include <ccan/opt/opt.h>
#include <ccan/opt/private.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/str/hex/hex.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/path/path.h>
#include <ccan/tal/str/str.h>
#include <common/codex32.h>
#include <common/configdir.h>
#include <common/configvar.h>
#include <common/features.h>
#include <common/hsm_encryption.h>
#include <common/json_command.h>
#include <common/json_param.h>
#include <common/type_to_string.h>
#include <common/version.h>
#include <common/wireaddr.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <hsmd/hsmd_wiregen.h>
#include <lightningd/chaintopology.h>
#include <lightningd/hsm_control.h>
#include <lightningd/options.h>
#include <lightningd/plugin.h>
#include <lightningd/subd.h>
#include <sys/stat.h>
#include <sys/wait.h>
/* FIXME: Put into ccan/time. */
#define TIME_FROM_SEC(sec) { { .tv_nsec = 0, .tv_sec = sec } }
#define TIME_FROM_MSEC(msec) \
{ { .tv_nsec = ((msec) % 1000) * 1000000, .tv_sec = (msec) / 1000 } }
static char *opt_set_u64(const char *arg, u64 *u)
{
char *endp;
unsigned long long l;
assert(arg != NULL);
/* This is how the manpage says to do it. Yech. */
errno = 0;
l = strtoull(arg, &endp, 0);
if (*endp || !arg[0])
return tal_fmt(tmpctx, "'%s' is not a number", arg);
*u = l;
if (errno || *u != l)
return tal_fmt(tmpctx, "'%s' is out of range", arg);
return NULL;
}
static char *opt_set_u32(const char *arg, u32 *u)
{
char *endp;
unsigned long l;
assert(arg != NULL);
/* This is how the manpage says to do it. Yech. */
errno = 0;
l = strtoul(arg, &endp, 0);
if (*endp || !arg[0])
return tal_fmt(tmpctx, "'%s' is not a number", arg);
*u = l;
if (errno || *u != l)
return tal_fmt(tmpctx, "'%s' is out of range", arg);
return NULL;
}
static char *opt_set_s32(const char *arg, s32 *u)
{
char *endp;
long l;
assert(arg != NULL);
/* This is how the manpage says to do it. Yech. */
errno = 0;
l = strtol(arg, &endp, 0);
if (*endp || !arg[0])
return tal_fmt(tmpctx, "'%s' is not a number", arg);
*u = l;
if (errno || *u != l)
return tal_fmt(tmpctx, "'%s' is out of range", arg);
return NULL;
}
char *opt_set_autobool_arg(const char *arg, enum opt_autobool *b)
{
if (!strcasecmp(arg, "yes") ||
!strcasecmp(arg, "true")) {
*b = OPT_AUTOBOOL_TRUE;
return NULL;
}
if (!strcasecmp(arg, "no") ||
!strcasecmp(arg, "false")) {
*b = OPT_AUTOBOOL_FALSE;
return NULL;
}
if (!strcasecmp(arg, "auto") ||
!strcasecmp(arg, "default")) {
*b = OPT_AUTOBOOL_AUTO;
return NULL;
}
return opt_invalid_argument(arg);
}
bool opt_show_autobool(char *buf, size_t len, const enum opt_autobool *b)
{
switch (*b) {
case OPT_AUTOBOOL_TRUE:
strncpy(buf, "true", len);
return true;
case OPT_AUTOBOOL_FALSE:
strncpy(buf, "false", len);
return true;
case OPT_AUTOBOOL_AUTO:
strncpy(buf, "auto", len);
return true;
}
abort();
}
static char *opt_set_mode(const char *arg, mode_t *m)
{
char *endp;
long l;
assert(arg != NULL);
/* Ensure length, and starts with 0. */
if (strlen(arg) != 4 || arg[0] != '0')
return tal_fmt(tmpctx, "'%s' is not a file mode", arg);
/* strtol, manpage, yech. */
errno = 0;
l = strtol(arg, &endp, 8); /* Octal. */
if (errno || *endp)
return tal_fmt(tmpctx, "'%s' is not a file mode", arg);
*m = l;
/* Range check not needed, previous strlen checks ensures only
* 9-bit, which fits mode_t (unless your Unix is seriously borked).
*/
return NULL;
}
static char *opt_force_feerates(const char *arg, struct lightningd *ld)
{
char **vals = tal_strsplit(tmpctx, arg, "/", STR_EMPTY_OK);
size_t n;
/* vals has NULL at end, enum feerate is 0 based */
if (tal_count(vals) - 1 > FEERATE_PENALTY + 1)
return "Too many values";
if (!ld->force_feerates)
ld->force_feerates = tal_arr(ld, u32, FEERATE_PENALTY + 1);
n = 0;
for (size_t i = 0; i < tal_count(ld->force_feerates); i++) {
char *err = opt_set_u32(vals[n], &ld->force_feerates[i]);
if (err)
return err;
fprintf(stderr, "Set feerate %zu based on val %zu\n", i, n);
if (vals[n+1])
n++;
}
return NULL;
}
static char *fmt_force_feerates(const tal_t *ctx, const u32 *force_feerates)
{
char *ret;
size_t last;
if (!force_feerates)
return NULL;
ret = tal_fmt(ctx, "%i", force_feerates[0]);
last = 0;
for (size_t i = 1; i < tal_count(force_feerates); i++) {
if (force_feerates[i] == force_feerates[i-1])
continue;
/* Different? Catchup! */
for (size_t j = last + 1; j <= i; j++)
tal_append_fmt(&ret, "/%i", force_feerates[j]);
last = i;
}
return ret;
}
static char *opt_add_accept_htlc_tlv(const char *arg,
u64 **accept_extra_tlv_types)
{
size_t n = tal_count(*accept_extra_tlv_types);
tal_resize(accept_extra_tlv_types, n+1);
return opt_set_u64(arg, &(*accept_extra_tlv_types)[n]);
}
static char *opt_set_accept_extra_tlv_types(const char *arg,
struct lightningd *ld)
{
char *ret, **elements = tal_strsplit(tmpctx, arg, ",", STR_NO_EMPTY);
if (!ld->deprecated_apis)
return "Please use --accept-htlc-tlv-type multiple times";
for (int i = 0; elements[i] != NULL; i++) {
ret = opt_add_accept_htlc_tlv(elements[i],
&ld->accept_extra_tlv_types);
if (ret)
return ret;
}
return NULL;
}
/* Returns the number of wireaddr types already announced */
static size_t num_announced_types(enum wire_addr_type type, struct lightningd *ld)
{
size_t num = 0;
for (size_t i = 0; i < tal_count(ld->proposed_wireaddr); i++) {
if (ld->proposed_wireaddr[i].itype != ADDR_INTERNAL_WIREADDR)
continue;
if (ld->proposed_wireaddr[i].u.wireaddr.wireaddr.type != type)
continue;
if (ld->proposed_listen_announce[i] & ADDR_ANNOUNCE)
num++;
}
return num;
}
static char *opt_add_addr_withtype(const char *arg,
struct lightningd *ld,
enum addr_listen_announce ala)
{
char const *err_msg;
struct wireaddr_internal wi;
bool dns_lookup_ok;
char *address;
u16 port;
assert(arg != NULL);
dns_lookup_ok = !ld->always_use_proxy && ld->config.use_dns;
/* Deprecated announce-addr-dns: autodetect DNS addresses. */
if (ld->announce_dns && (ala == ADDR_ANNOUNCE)
&& separate_address_and_port(tmpctx, arg, &address, &port)
&& is_dnsaddr(address)) {
log_unusual(ld->log, "Adding dns prefix to %s!", arg);
arg = tal_fmt(tmpctx, "dns:%s", arg);
}
err_msg = parse_wireaddr_internal(tmpctx, arg, ld->portnum,
dns_lookup_ok, &wi);
if (err_msg)
return tal_fmt(tmpctx, "Unable to parse address '%s': %s", arg, err_msg);
/* Check they didn't specify some weird type! */
switch (wi.itype) {
case ADDR_INTERNAL_WIREADDR:
switch (wi.u.wireaddr.wireaddr.type) {
case ADDR_TYPE_IPV4:
case ADDR_TYPE_IPV6:
if ((ala & ADDR_ANNOUNCE) && wi.u.allproto.is_websocket)
return tal_fmt(tmpctx,
"Cannot announce websocket address, use --bind-addr=%s", arg);
/* These can be either bind or announce */
break;
case ADDR_TYPE_TOR_V2_REMOVED:
/* Can't happen any more */
abort();
case ADDR_TYPE_TOR_V3:
switch (ala) {
case ADDR_LISTEN:
if (!ld->deprecated_apis)
return tal_fmt(tmpctx,
"Don't use --bind-addr=%s, use --announce-addr=%s",
arg, arg);
log_unusual(ld->log,
"You used `--bind-addr=%s` option with an .onion address,"
" You are lucky in this node live some wizards and"
" fairies, we have done this for you and don't announce, Be as hidden as wished",
arg);
/* And we ignore it */
return NULL;
case ADDR_LISTEN_AND_ANNOUNCE:
if (!ld->deprecated_apis)
return tal_fmt(tmpctx,
"Don't use --addr=%s, use --announce-addr=%s",
arg, arg);
log_unusual(ld->log,
"You used `--addr=%s` option with an .onion address,"
" You are lucky in this node live some wizards and"
" fairies, we have done this for you and don't announce, Be as hidden as wished",
arg);
ala = ADDR_LISTEN;
break;
case ADDR_ANNOUNCE:
break;
}
break;
case ADDR_TYPE_DNS:
/* Can only announce this */
switch (ala) {
case ADDR_ANNOUNCE:
break;
case ADDR_LISTEN:
return tal_fmt(tmpctx,
"Cannot use dns: prefix with --bind-addr, use --bind-addr=%s", arg + strlen("dns:"));
case ADDR_LISTEN_AND_ANNOUNCE:
return tal_fmt(tmpctx,
"Cannot use dns: prefix with --addr, use --bind-addr=%s and --addr=%s",
arg + strlen("dns:"),
arg);
}
/* BOLT-hostnames #7:
* The origin node:
* ...
* - MUST NOT announce more than one `type 5` DNS hostname.
*/
if (num_announced_types(ADDR_TYPE_DNS, ld) > 0)
return tal_fmt(tmpctx, "Only one DNS can be announced");
break;
}
break;
case ADDR_INTERNAL_SOCKNAME:
switch (ala) {
case ADDR_ANNOUNCE:
return tal_fmt(tmpctx,
"Cannot announce sockets, try --bind-addr=%s", arg);
case ADDR_LISTEN_AND_ANNOUNCE:
if (!ld->deprecated_apis)
return tal_fmt(tmpctx, "Don't use --addr=%s, use --bind-addr=%s",
arg, arg);
ala = ADDR_LISTEN;
/* Fall thru */
case ADDR_LISTEN:
break;
}
break;
case ADDR_INTERNAL_AUTOTOR:
case ADDR_INTERNAL_STATICTOR:
/* We turn --announce-addr into --addr */
switch (ala) {
case ADDR_ANNOUNCE:
ala = ADDR_LISTEN_AND_ANNOUNCE;
break;
case ADDR_LISTEN_AND_ANNOUNCE:
case ADDR_LISTEN:
break;
}
break;
case ADDR_INTERNAL_ALLPROTO:
/* You can only bind to wildcard, and optionally announce */
switch (ala) {
case ADDR_ANNOUNCE:
return tal_fmt(tmpctx, "Cannot use wildcard address '%s'", arg);
case ADDR_LISTEN_AND_ANNOUNCE:
if (wi.u.allproto.is_websocket)
return tal_fmt(tmpctx,
"Cannot announce websocket address, use --bind-addr=%s", arg);
/* fall thru */
case ADDR_LISTEN:
break;
}
break;
case ADDR_INTERNAL_FORPROXY:
/* You can't use these addresses here at all: this means we've
* suppressed DNS and given a string-style name */
return tal_fmt(tmpctx, "Cannot resolve address '%s' (not using DNS!)", arg);
}
/* Sanity check for exact duplicates. */
for (size_t i = 0; i < tal_count(ld->proposed_wireaddr); i++) {
/* Only compare announce vs announce and bind vs bind */
if ((ld->proposed_listen_announce[i] & ala) == 0)
continue;
if (wireaddr_internal_eq(&ld->proposed_wireaddr[i], &wi))
return tal_fmt(tmpctx, "Duplicate %s address %s",
ala & ADDR_ANNOUNCE ? "announce" : "listen",
type_to_string(tmpctx, struct wireaddr_internal, &wi));
}
tal_arr_expand(&ld->proposed_listen_announce, ala);
tal_arr_expand(&ld->proposed_wireaddr, wi);
return NULL;
}
static char *opt_add_announce_addr(const char *arg, struct lightningd *ld)
{
return opt_add_addr_withtype(arg, ld, ADDR_ANNOUNCE);
}
static char *opt_add_addr(const char *arg, struct lightningd *ld)
{
return opt_add_addr_withtype(arg, ld, ADDR_LISTEN_AND_ANNOUNCE);
}
static char *opt_add_bind_addr(const char *arg, struct lightningd *ld)
{
return opt_add_addr_withtype(arg, ld, ADDR_LISTEN);
}
static char *opt_subdaemon(const char *arg, struct lightningd *ld)
{
char *subdaemon;
char *sdpath;
/* example arg: "hsmd:remote_hsmd" */
size_t colonoff = strcspn(arg, ":");
if (!arg[colonoff])
return tal_fmt(tmpctx, "argument must contain ':'");
subdaemon = tal_strndup(ld, arg, colonoff);
if (!is_subdaemon(subdaemon))
return tal_fmt(tmpctx, "\"%s\" is not a subdaemon", subdaemon);
/* Make the value a tal-child of the subdaemon */
sdpath = tal_strdup(subdaemon, arg + colonoff + 1);
/* Remove any preexisting alt subdaemon mapping (and
* implicitly, the sdpath). */
tal_free(strmap_del(&ld->alt_subdaemons, subdaemon, NULL));
strmap_add(&ld->alt_subdaemons, subdaemon, sdpath);
return NULL;
}
static bool opt_show_u64(char *buf, size_t len, const u64 *u)
{
snprintf(buf, len, "%"PRIu64, *u);
return true;
}
static bool opt_show_u32(char *buf, size_t len, const u32 *u)
{
snprintf(buf, len, "%"PRIu32, *u);
return true;
}
static bool opt_show_s32(char *buf, size_t len, const s32 *u)
{
snprintf(buf, len, "%"PRIi32, *u);
return true;
}
static bool opt_show_mode(char *buf, size_t len, const mode_t *m)
{
snprintf(buf, len, "%04o", (int) *m);
return true;
}
static bool opt_show_rgb(char *buf, size_t len, const struct lightningd *ld)
{
/* Can happen with -h! */
if (!ld->rgb)
return false;
/* This is always set; if not by arg, then by default */
hex_encode(ld->rgb, 3, buf, len);
return true;
}
static char *opt_set_rgb(const char *arg, struct lightningd *ld)
{
assert(arg != NULL);
ld->rgb = tal_free(ld->rgb);
/* BOLT #7:
*
* - Note: the first byte of `rgb_color` is the red value, the second
* byte is the green value, and the last byte is the blue value.
*/
ld->rgb = tal_hexdata(ld, arg, strlen(arg));
if (!ld->rgb || tal_count(ld->rgb) != 3)
return tal_fmt(tmpctx, "rgb '%s' is not six hex digits", arg);
return NULL;
}
static bool opt_show_alias(char *buf, size_t len, const struct lightningd *ld)
{
/* Can happen with -h! */
if (!ld->alias)
return false;
strncpy(buf, cast_signed(const char *, ld->alias), len);
return true;
}
static char *opt_set_alias(const char *arg, struct lightningd *ld)
{
assert(arg != NULL);
ld->alias = tal_free(ld->alias);
/* BOLT #7:
*
* * [`32*byte`:`alias`]
*...
* - MUST set `alias` to a valid UTF-8 string, with any
* `alias` trailing-bytes equal to 0.
*/
if (strlen(arg) > 32)
return tal_fmt(tmpctx, "Alias '%s' is over 32 characters", arg);
ld->alias = tal_arrz(ld, u8, 33);
strncpy((char*)ld->alias, arg, 32);
return NULL;
}
static char *opt_set_offline(struct lightningd *ld)
{
ld->reconnect = false;
ld->listen = false;
log_info(ld->log, "Started in offline mode!");
return NULL;
}
static char *opt_add_proxy_addr(const char *arg, struct lightningd *ld)
{
bool needed_dns = false;
const char *err;
tal_free(ld->proxyaddr);
/* We use a tal_arr here, so we can marshal it to gossipd */
ld->proxyaddr = tal_arr(ld, struct wireaddr, 1);
err = parse_wireaddr(tmpctx, arg, 9050,
ld->always_use_proxy ? &needed_dns : NULL,
ld->proxyaddr);
return cast_const(char *, err);
}
static char *opt_add_plugin(const char *arg, struct lightningd *ld)
{
struct plugin *p;
if (plugin_blacklisted(ld->plugins, arg)) {
log_info(ld->log, "%s: disabled via disable-plugin", arg);
return NULL;
}
p = plugin_register(ld->plugins, arg, NULL, false, NULL, NULL);
if (!p)
return tal_fmt(tmpctx, "Failed to register %s: %s", arg, strerror(errno));
return NULL;
}
static char *opt_disable_plugin(const char *arg, struct lightningd *ld)
{
plugin_blacklist(ld->plugins, arg);
return NULL;
}
static char *opt_add_plugin_dir(const char *arg, struct lightningd *ld)
{
return add_plugin_dir(ld->plugins, arg, false);
}
static char *opt_clear_plugins(struct lightningd *ld)
{
clear_plugins(ld->plugins);
/* Remove from configvars too! */
for (size_t i = 0; i < tal_count(ld->configvars); i++) {
if (streq(ld->configvars[i]->optvar, "plugin")
|| streq(ld->configvars[i]->optvar, "plugin-dir"))
ld->configvars[i]->overridden = true;
}
return NULL;
}
static char *opt_important_plugin(const char *arg, struct lightningd *ld)
{
struct plugin *p;
if (plugin_blacklisted(ld->plugins, arg)) {
log_info(ld->log, "%s: disabled via disable-plugin", arg);
return NULL;
}
p = plugin_register(ld->plugins, arg, NULL, true, NULL, NULL);
if (!p)
return tal_fmt(tmpctx, "Failed to register %s: %s", arg, strerror(errno));
return NULL;
}
/* Test code looks in logs, so we print prompts to log as well as stdout */
static void prompt(struct lightningd *ld, const char *str)
{
printf("%s\n", str);
log_debug(ld->log, "PROMPT: %s", str);
/* If we don't flush we might end up being buffered and we might seem
* to hang while we wait for the password. */
fflush(stdout);
}
/* Prompt the user to enter a password, from which will be derived the key used
* for `hsm_secret` encryption.
* The algorithm used to derive the key is Argon2(id), to which libsodium
* defaults. However argon2id-specific constants are used in case someone runs it
* with a libsodium version which default constants differs (typically <1.0.9).
*/
static char *opt_set_hsm_password(struct lightningd *ld)
{
char *passwd, *passwd_confirmation, *err_msg;
int is_encrypted;
is_encrypted = is_hsm_secret_encrypted("hsm_secret");
/* While lightningd is performing the first initialization
* this check is always true because the file does not exist.
*
* Maybe the is_hsm_secret_encrypted is performing a not useful
* check at this stage, but the hsm is a delicate part,
* so it is a good information to have inside the log. */
if (is_encrypted == -1)
log_info(ld->log, "'hsm_secret' does not exist (%s)",
strerror(errno));
prompt(ld, "The hsm_secret is encrypted with a password. In order to "
"decrypt it and start the node you must provide the password.");
prompt(ld, "Enter hsm_secret password:");
passwd = read_stdin_pass_with_exit_code(&err_msg, &opt_exitcode);
if (!passwd)
return err_msg;
if (!is_encrypted) {
prompt(ld, "Confirm hsm_secret password:");
fflush(stdout);
passwd_confirmation = read_stdin_pass_with_exit_code(&err_msg, &opt_exitcode);
if (!passwd_confirmation)
return err_msg;
if (!streq(passwd, passwd_confirmation)) {
opt_exitcode = EXITCODE_HSM_BAD_PASSWORD;
return "Passwords confirmation mismatch.";
}
free(passwd_confirmation);
}
prompt(ld, "");
ld->config.keypass = tal(NULL, struct secret);
opt_exitcode = hsm_secret_encryption_key_with_exitcode(passwd, ld->config.keypass, &err_msg);
if (opt_exitcode > 0)
return err_msg;
ld->encrypted_hsm = true;
free(passwd);
return NULL;
}
static char *opt_force_privkey(const char *optarg, struct lightningd *ld)
{
tal_free(ld->dev_force_privkey);
ld->dev_force_privkey = tal(ld, struct privkey);
if (!hex_decode(optarg, strlen(optarg),
ld->dev_force_privkey, sizeof(*ld->dev_force_privkey)))
return tal_fmt(tmpctx, "Unable to parse privkey '%s'", optarg);
return NULL;
}
static char *opt_force_bip32_seed(const char *optarg, struct lightningd *ld)
{
tal_free(ld->dev_force_bip32_seed);
ld->dev_force_bip32_seed = tal(ld, struct secret);
if (!hex_decode(optarg, strlen(optarg),
ld->dev_force_bip32_seed,
sizeof(*ld->dev_force_bip32_seed)))
return tal_fmt(tmpctx, "Unable to parse secret '%s'", optarg);
return NULL;
}
static char *opt_force_tmp_channel_id(const char *optarg, struct lightningd *ld)
{
tal_free(ld->dev_force_tmp_channel_id);
ld->dev_force_tmp_channel_id = tal(ld, struct channel_id);
if (!hex_decode(optarg, strlen(optarg),
ld->dev_force_tmp_channel_id,
sizeof(*ld->dev_force_tmp_channel_id)))
return tal_fmt(tmpctx, "Unable to parse channel id '%s'", optarg);
return NULL;
}
static char *opt_force_channel_secrets(const char *optarg,
struct lightningd *ld)
{
char **strs;
tal_free(ld->dev_force_channel_secrets);
tal_free(ld->dev_force_channel_secrets_shaseed);
ld->dev_force_channel_secrets = tal(ld, struct secrets);
ld->dev_force_channel_secrets_shaseed = tal(ld, struct sha256);
strs = tal_strsplit(tmpctx, optarg, "/", STR_EMPTY_OK);
if (tal_count(strs) != 7) /* Last is NULL */
return "Expected 6 hex secrets separated by /";
if (!hex_decode(strs[0], strlen(strs[0]),
&ld->dev_force_channel_secrets->funding_privkey,
sizeof(ld->dev_force_channel_secrets->funding_privkey))
|| !hex_decode(strs[1], strlen(strs[1]),
&ld->dev_force_channel_secrets->revocation_basepoint_secret,
sizeof(ld->dev_force_channel_secrets->revocation_basepoint_secret))
|| !hex_decode(strs[2], strlen(strs[2]),
&ld->dev_force_channel_secrets->payment_basepoint_secret,
sizeof(ld->dev_force_channel_secrets->payment_basepoint_secret))
|| !hex_decode(strs[3], strlen(strs[3]),
&ld->dev_force_channel_secrets->delayed_payment_basepoint_secret,
sizeof(ld->dev_force_channel_secrets->delayed_payment_basepoint_secret))
|| !hex_decode(strs[4], strlen(strs[4]),
&ld->dev_force_channel_secrets->htlc_basepoint_secret,
sizeof(ld->dev_force_channel_secrets->htlc_basepoint_secret))
|| !hex_decode(strs[5], strlen(strs[5]),
ld->dev_force_channel_secrets_shaseed,
sizeof(*ld->dev_force_channel_secrets_shaseed)))
return "Expected 6 hex secrets separated by /";
return NULL;
}
static char *opt_force_featureset(const char *optarg,
struct lightningd *ld)
{
char **parts = tal_strsplit(tmpctx, optarg, "/", STR_EMPTY_OK);
if (tal_count(parts) != NUM_FEATURE_PLACE + 1) {
if (!strstarts(optarg, "-") && !strstarts(optarg, "+"))
return "Expected 8 feature sets (init/globalinit/"
" node_announce/channel/bolt11/b12offer/b12invreq/b12inv) each terminated by /"
" OR +/-<single_bit_num>";
char *endp;
long int n = strtol(optarg + 1, &endp, 10);
const struct feature_set *f;
if (*endp || endp == optarg + 1)
return "Invalid feature number";
f = feature_set_for_feature(NULL, n);
if (strstarts(optarg, "-")
&& !feature_set_sub(ld->our_features, take(f)))
return "Feature unknown";
if (strstarts(optarg, "+")
&& !feature_set_or(ld->our_features, take(f)))
return "Feature already flagged-on";
return NULL;
}
for (size_t i = 0; parts[i]; i++) {
char **bits = tal_strsplit(tmpctx, parts[i], ",", STR_EMPTY_OK);
tal_resize(&ld->our_features->bits[i], 0);
for (size_t j = 0; bits[j]; j++) {
char *endp;
long int n = strtol(bits[j], &endp, 10);
if (*endp || endp == bits[j])
return "Invalid bitnumber";
set_feature_bit(&ld->our_features->bits[i], n);
}
}
return NULL;
}
static void dev_register_opts(struct lightningd *ld)
{
/* We might want to debug plugins, which are started before normal
* option parsing */
clnopt_witharg("--dev-debugger=<subprocess>", OPT_EARLY|OPT_DEV,
opt_set_charp, opt_show_charp,
&ld->dev_debug_subprocess,
"Invoke gdb at start of <subprocess>");
clnopt_noarg("--dev-no-plugin-checksum", OPT_EARLY|OPT_DEV,
opt_set_bool,
&ld->dev_no_plugin_checksum,
"Don't checksum plugins to detect changes");
clnopt_noarg("--dev-builtin-plugins-unimportant", OPT_EARLY|OPT_DEV,
opt_set_bool,
&ld->plugins->dev_builtin_plugins_unimportant,
"Make builtin plugins unimportant so you can plugin stop them.");
clnopt_noarg("--dev-no-reconnect", OPT_DEV,
opt_set_invbool,
&ld->reconnect,
"Disable automatic reconnect-attempts by this node, but accept incoming");
clnopt_noarg("--dev-fast-reconnect", OPT_DEV,
opt_set_bool,
&ld->dev_fast_reconnect,
"Make max default reconnect delay 3 (not 300) seconds");
clnopt_noarg("--dev-fail-on-subdaemon-fail", OPT_DEV,
opt_set_bool,
&ld->dev_subdaemon_fail, opt_hidden);
clnopt_witharg("--dev-disconnect=<filename>", OPT_DEV,
opt_subd_dev_disconnect,
NULL, ld, "File containing disconnection points");
clnopt_noarg("--dev-allow-localhost", OPT_DEV,
opt_set_bool,
&ld->dev_allow_localhost,
"Announce and allow announcments for localhost address");
clnopt_witharg("--dev-bitcoind-poll", OPT_DEV|OPT_SHOWINT,
opt_set_u32, opt_show_u32,
&ld->topology->poll_seconds,
"Time between polling for new transactions");
clnopt_noarg("--dev-fast-gossip", OPT_DEV,
opt_set_bool,
&ld->dev_fast_gossip,
"Make gossip broadcast 1 second, etc");
clnopt_noarg("--dev-fast-gossip-prune", OPT_DEV,
opt_set_bool,
&ld->dev_fast_gossip_prune,
"Make gossip pruning 30 seconds");
clnopt_witharg("--dev-gossip-time", OPT_DEV|OPT_SHOWINT,
opt_set_u32, opt_show_u32,
&ld->dev_gossip_time,
"UNIX time to override gossipd to use.");
clnopt_witharg("--dev-force-privkey", OPT_DEV,
opt_force_privkey, NULL, ld,
"Force HSM to use this as node private key");
clnopt_witharg("--dev-force-bip32-seed", OPT_DEV,
opt_force_bip32_seed, NULL, ld,
"Force HSM to use this as bip32 seed");
clnopt_witharg("--dev-force-channel-secrets", OPT_DEV,
opt_force_channel_secrets, NULL, ld,
"Force HSM to use these for all per-channel secrets");
clnopt_witharg("--dev-max-funding-unconfirmed-blocks",
OPT_DEV|OPT_SHOWINT,
opt_set_u32, opt_show_u32,
&ld->dev_max_funding_unconfirmed,
"Maximum number of blocks we wait for a channel "
"funding transaction to confirm, if we are the "
"fundee.");
clnopt_witharg("--dev-force-tmp-channel-id", OPT_DEV,
opt_force_tmp_channel_id, NULL, ld,
"Force the temporary channel id, instead of random");
clnopt_noarg("--dev-no-htlc-timeout", OPT_DEV,
opt_set_bool,
&ld->dev_no_htlc_timeout,
"Don't kill channeld if HTLCs not confirmed within 30 seconds");
clnopt_noarg("--dev-fail-process-onionpacket", OPT_DEV,
opt_set_bool,
&dev_fail_process_onionpacket,
"Force all processing of onion packets to fail");
clnopt_noarg("--dev-no-version-checks", OPT_DEV,
opt_set_bool,
&ld->dev_no_version_checks,
"Skip calling subdaemons with --version on startup");
clnopt_witharg("--dev-force-features", OPT_DEV,
opt_force_featureset, NULL, ld,
"Force the init/globalinit/node_announce/channel/bolt11/ features, each comma-separated bitnumbers OR a single +/-<bitnumber>");
clnopt_witharg("--dev-timeout-secs", OPT_DEV|OPT_SHOWINT,
opt_set_u32, opt_show_u32,
&ld->config.connection_timeout_secs,
"Seconds to timeout if we don't receive INIT from peer");
clnopt_noarg("--dev-no-modern-onion", OPT_DEV,
opt_set_bool,
&ld->dev_ignore_modern_onion,
"Ignore modern onion messages");
clnopt_witharg("--dev-disable-commit-after", OPT_DEV|OPT_SHOWINT,
opt_set_intval, opt_show_intval,
&ld->dev_disable_commit,
"Disable commit timer after this many commits");
clnopt_noarg("--dev-no-ping-timer", OPT_DEV,
opt_set_bool,
&ld->dev_no_ping_timer,
"Don't hang up if we don't get a ping response");
clnopt_witharg("--dev-onion-reply-length", OPT_DEV|OPT_SHOWINT,
opt_set_uintval,
opt_show_uintval,
&dev_onion_reply_length,
"Send onion errors of custom length");
clnopt_witharg("--dev-max-fee-multiplier", OPT_DEV|OPT_SHOWINT,
opt_set_uintval,
opt_show_uintval,
&ld->config.max_fee_multiplier,
"Allow the fee proposed by the remote end to"
" be up to multiplier times higher than our "
"own. Small values will cause channels to be"
" closed more often due to fee fluctuations,"
" large values may result in large fees.");
clnopt_witharg("--dev-allowdustreserve", OPT_DEV|OPT_SHOWBOOL,
opt_set_bool_arg, opt_show_bool,
&ld->config.allowdustreserve,
"If true, we allow the `fundchannel` RPC command and the `openchannel` plugin hook to set a reserve that is below the dust limit.");
}
static const struct config testnet_config = {
/* 6 blocks to catch cheating attempts. */
.locktime_blocks = 6,
/* They can have up to 14 days, maximumu value that lnd will ask for by default. */
/* FIXME Convince lnd to use more reasonable defaults... */
.locktime_max = 14 * 24 * 6,
/* We're fairly trusting, under normal circumstances. */
.anchor_confirms = 1,
/* Testnet blockspace is free. */
.max_concurrent_htlcs = 483,
/* channel defaults for htlc min/max values */
.htlc_minimum_msat = AMOUNT_MSAT(0),
.htlc_maximum_msat = AMOUNT_MSAT(-1ULL), /* no limit */
/* Max amount of dust allowed per channel (50ksat) */
.max_dust_htlc_exposure_msat = AMOUNT_MSAT(50000000),
/* Be aggressive on testnet. */
.cltv_expiry_delta = 6,
.cltv_final = 10,
/* Send commit 10msec after receiving; almost immediately. */
.commit_time_ms = 10,
/* Allow dust payments */
.fee_base = 1,
/* Take 0.001% */
.fee_per_satoshi = 10,
/* Testnet sucks */
.ignore_fee_limits = true,
/* Rescan 5 hours of blocks on testnet, it's reorg happy */
.rescan = 30,
.use_dns = true,
/* Excplicitly turns 'on' or 'off' IP discovery feature. */
.ip_discovery = OPT_AUTOBOOL_AUTO,
/* Public TCP port assumed for IP discovery. Defaults to chainparams. */
.ip_discovery_port = 0,
/* Sets min_effective_htlc_capacity - at 1000$/BTC this is 10ct */
.min_capacity_sat = 10000,
/* 1 minute should be enough for anyone! */
.connection_timeout_secs = 60,
.exp_offers = false,
.allowdustreserve = false,
.require_confirmed_inputs = false,
.max_fee_multiplier = 10,
.commit_fee_percent = 100,
.feerate_offset = 5,
};
/* aka. "Dude, where's my coins?" */
static const struct config mainnet_config = {
/* ~one day to catch cheating attempts. */
.locktime_blocks = 6 * 24,
/* They can have up to 14 days, maximumu value that lnd will ask for by default. */
/* FIXME Convince lnd to use more reasonable defaults... */
.locktime_max = 14 * 24 * 6,
/* We're fairly trusting, under normal circumstances. */
.anchor_confirms = 3,
/* While up to 483 htlcs are possible we do 30 by default (as eclair does) to save blockspace */
.max_concurrent_htlcs = 30,
/* defaults for htlc min/max values */
.htlc_minimum_msat = AMOUNT_MSAT(0),
.htlc_maximum_msat = AMOUNT_MSAT(-1ULL), /* no limit */
/* Max amount of dust allowed per channel (50ksat) */
.max_dust_htlc_exposure_msat = AMOUNT_MSAT(50000000),
/* BOLT #2:
*
* 1. the `cltv_expiry_delta` for channels, `3R+2G+2S`: if in doubt, a
* `cltv_expiry_delta` of at least 34 is reasonable (R=2, G=2, S=12)
*/
/* R = 2, G = 2, S = 12 */
.cltv_expiry_delta = 34,
/* BOLT #2:
*
* 4. the minimum `cltv_expiry` accepted for terminal payments: the
* worst case for the terminal node C is `2R+G+S` blocks */
.cltv_final = 18,
/* Send commit 10msec after receiving; almost immediately. */
.commit_time_ms = 10,
/* Discourage dust payments */
.fee_base = 1000,
/* Take 0.001% */
.fee_per_satoshi = 10,
/* Mainnet should have more stable fees */
.ignore_fee_limits = false,