-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlispconfig.py
4556 lines (4072 loc) · 138 KB
/
lispconfig.py
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
# -----------------------------------------------------------------------------
#
# Copyright 2013-2019 lispers.net - Dino Farinacci <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# -----------------------------------------------------------------------------
#
# lispconfig.py
#
# This file contains all configuration support for the LISP subsystem. That
# includes lisp.config file processing and the RESTful interface via the
# bottle module.
#
# -----------------------------------------------------------------------------
from __future__ import division
from future import standard_library
standard_library.install_aliases()
from builtins import str
from builtins import range
from past.utils import old_div
import lisp
import os
import time
import socket
import bottle
import hmac
import hashlib
import select
import copy
import math
from json import dumps as json_dumps
from subprocess import getoutput
#------------------------------------------------------------------------------
LISP_USER_TIMEOUT = 1800 # Let users stay in web interface for 1/2 hour
#
# This array decides where the command goes to get processed.
#
lisp_commands = {
"lisp user-account" : ["lisp-core"],
"lisp enable" : ["lisp-core"],
"lisp debug" : ["lisp-core"],
"lisp xtr-parameters" : ["lisp-itr", "lisp-rtr", "lisp-etr"],
"lisp interface" : ["lisp-itr", "lisp-etr", "lisp-rtr"],
"lisp rtr-list" : ["lisp-core"],
"lisp map-resolver" : ["lisp-itr", "lisp-rtr"],
"lisp map-cache" : ["lisp-itr", "lisp-rtr"],
"lisp itr-map-cache" : ["lisp-itr"],
"lisp rtr-map-cache" : ["lisp-rtr"],
"lisp map-server" : ["lisp-itr", "lisp-etr"],
"lisp database-mapping" : ["lisp-itr", "lisp-etr", "lisp-rtr"],
"lisp group-mapping" : ["lisp-etr"],
"lisp glean-mapping" : ["lisp-rtr"],
"lisp explicit-locator-path" : ["lisp-itr", "lisp-rtr", "lisp-etr",
"lisp-ms"],
"lisp replication-list-entry" : ["lisp-itr", "lisp-rtr", "lisp-etr",
"lisp-ms"],
"lisp geo-coordinates" : ["lisp-itr", "lisp-etr", "lisp-ms"],
"lisp json" : ["lisp-itr", "lisp-etr", "lisp-rtr",
"lisp-ms"],
"lisp ddt-root" : ["lisp-mr"],
"lisp referral-cache" : ["lisp-mr"],
"lisp site" : ["lisp-ms"],
"lisp eid-crypto-hash" : ["lisp-ms"],
"lisp encryption-keys" : ["lisp-ms"],
"lisp map-server-peer" : ["lisp-ms"],
"lisp ms-authoritative-prefix" : ["lisp-ms"],
"lisp ddt-authoritative-prefix" : ["lisp-ddt"],
"lisp delegation" : ["lisp-ddt"],
"lisp policy" : ["lisp-ms"],
"show itr-map-cache" : ["lisp-itr"],
"show itr-rloc-probing" : ["lisp-itr"],
"show rtr-map-cache" : ["lisp-rtr"],
"show rtr-map-cache-dns" : ["lisp-rtr"],
"show rtr-rloc-probing" : ["lisp-rtr"],
"show database-mapping" : ["lisp-etr"],
"show referral-cache" : ["lisp-mr"],
"show delegations" : ["lisp-ddt"],
"show site" : ["lisp-ms"],
"show itr-dynamic-eid" : ["lisp-itr"],
"show etr-dynamic-eid" : ["lisp-etr"],
"show itr-keys" : ["lisp-itr"],
"show etr-keys" : ["lisp-etr"],
"show rtr-keys" : ["lisp-rtr"]
}
lisp_core_commands = {
"lisp enable" : ["", {
"itr" : [False, "yes", "no"],
"etr" : [False, "yes", "no"],
"rtr" : [False, "yes", "no"],
"map-resolver" : [False, "yes", "no"],
"map-server" : [False, "yes", "no"],
"ddt-node" : [False, "yes", "no"] } ],
"lisp debug" : ["", {
"core" : [False, "yes", "no"],
"itr" : [False, "yes", "no"],
"etr" : [False, "yes", "no"],
"rtr" : [False, "yes", "no"],
"map-resolver" : [False, "yes", "no"],
"map-server" : [False, "yes", "no"],
"ddt-node" : [False, "yes", "no"] } ],
"lisp user-account" : ["", {
"username" : [False],
"password" : [False],
"super-user" : [False, "yes", "no"] } ],
"lisp rtr-list" : ["", {
"address" : [True] }]
}
#------------------------------------------------------------------------------
#
# lisp_banner_top
#
# This is the banner that goes on every web page.
#
def lisp_banner_top(no_hover):
html_title = socket.gethostname()
hostname = lisp.lisp_print_cour(html_title)
if (no_hover == False):
hover = getoutput("ifconfig")
hostname = lisp.lisp_span(hostname, hover)
#endif
hostname = '''
<a href="/lisp/traceback" style="text-decoration: none">{}</a>
'''.format(hostname)
banner = '''
<head><title>{}</title></head>
<body bgcolor="gray">
<div style="margin:20px;background-color:#F5F5F5;padding:15px;
border-radius:20px;border:5px solid #666666;">
<table width="100%"><tr>
<td align="left" width="50%">
<a href="/lisp" style="text-decoration: none"><font size="8"><i>
{}<font color="black">.</font>{}</i></font></a><br>
<font size="4"><i>{}</i></font>
</td>
<td align="right" valign="bottom" width="50%">{}</td>
</tr></table>
<hr style="border: none; border-bottom: 1px solid gray;">
'''.format(html_title, lisp.green("lispers", True), lisp.red("net", True),
lisp.bold("Scalable Open Overlay Networking", True), hostname)
return(banner)
#enddef
#
# lisp_banner_bottom
#
# This is the banner that goes on every web page.
#
def lisp_banner_bottom():
hostname = socket.gethostname()
date = getoutput("date")
uptime = lisp.lisp_print_elapsed(lisp.lisp_uptime)
py = " (py2)" if lisp.lisp_is_python2() else " (py3)"
banner = '''<br><hr style="border: none; border-bottom: 1px solid gray;">
<i><font size="2">{} - Uptime
{}, Version {}<br>Copyright 2013-2019 - all rights reserved by
<a href="http://www.lispers.net"><b>lispers.net</b></a> LLC<br>
Features/Bugs go to <a href=
"mailto:[email protected]?subject=lispers.net v{} bug-report from '{}'">
[email protected]</a></i></font><br><br>
'''.format(date, uptime, lisp.bold(lisp.lisp_version + py, True),
lisp.lisp_version + py, hostname)
return(banner)
#enddef
#
# lisp_show_wrapper
#
# All show command output will be wrapped with a web header and footer.
#
def lisp_show_wrapper(output):
return(lisp_banner_top(False) + output + lisp_banner_bottom())
#enddef
#
# lisp_table_header
#
# Print a consistent table header row for all show commands.
#
def lisp_table_header(title, *args):
output = '''
<font face="Sans-Serif"><h3><i>{}</i></h3></font>
<table border="1" cellspacing="3x" cellpadding="5x">
<tr>
'''.format(title)
for arg in args:
output += '''
<td><font face="Sans-Serif"><b>{}</b></font></td>
'''.format(arg)
#endfor
output += "</tr>"
return(output)
#enddef
#
# lisp_table_row
#
# Print a consistent table row for all show commands.
#
def lisp_table_row(*args):
output = "<tr>"
for arg in args:
output += '''
<td><font face="Sans-Serif">{}</font></td>
'''.format(arg)
#endfor
output += "</tr>"
return(output)
#enddef
#
# lisp_table_footer
#
# Print a consistent table footer for all show commands.
#
def lisp_table_footer():
return("</table>")
#enddef
#
# lisp_write_last_changed_date
#
# Put timestamp in header line of file lisp.config.
#
def lisp_write_last_changed_date(new, line):
ts = getoutput("date")
l = line.find(":")
line = line[0:l+1] + " " + ts + "\n"
new.write(line)
return
#enddef
#
# lisp_comment
#
# Return True if first character of a line from the configuration file line
# has "#".
#
def lisp_comment(line):
return(True if line[0] == "#" else False)
#enddef
#
# lisp_begin_clause
#
# Return true if "{" is found on line.
#
def lisp_begin_clause(line):
return(False if line.find("{") == -1 else True)
#enddef
#
# lisp_end_clause
#
# Return true if "}" is found on line.
#
def lisp_end_clause(line):
return(False if line.find("}") == -1 else True)
#enddef
#
# lisp_end_file
#
# Return True if line is last line from the configuration file of editable
# part of file.
#
def lisp_end_file(line):
return(True if (line[0:10] == "#---------" and line[-2] == "#") else False)
#enddef
#
# lisp_write_error
#
# Line has syntax error. Flag and write comment lines for rest of clause. Go
# see lisp_syntax_check() for why the line starts with "%".
#
def lisp_write_error(line, error):
new_line = "%>>> " + line + " <<< " + error + "\n"
return(new_line)
#enddef
#
# lisp_write_line
#
# Write line to a new clause. We are commenting it, assuming there may be
# an error in the clause. If there is not, we will line.replace("#", " ").
#
def lisp_write_line(line):
return("#" + line + "\n")
#enddef
#
# lisp_not_supported
#
# Come here when a command is not yet implemented.
#
def lisp_not_supported():
return(lisp_show_wrapper(""))
#enddef
#
# lisp_hash_password
#
# Take a plaintext password that came from the lisp.config file and hash
# it to be written out to the file. So we don't store plaintext passwords.
#
def lisp_hash_password(plaintext):
p = plaintext.encode()
ciphertext = hmac.new(b"lispers.net", p, hashlib.sha1).hexdigest()
return(ciphertext)
#enddef
#
# lisp_validate_input_address_string
#
# Validate an input address string from a user can be of the following forms:
#
# <address>
# [<iid>]<address>
# <address>-><group>
# [<iid>]<address>->[<iid>]<group>
#
# An <address> can be a string when it is encoded by user as '<address>'.
#
def lisp_validate_input_address_string(input_str):
eid_str = input_str
group_str = None
if (input_str.find("->") != -1):
sg = input_str.split("->")
eid_str = sg[0]
group_str = sg[1]
#endif
index = lisp_valid_iid_format(eid_str)
if (index == -1): return(False)
if (index == len(eid_str)): return(True)
addr_str = eid_str[index::]
if (addr_str.find("/") == -1):
good = lisp.lisp_valid_address_format("address", addr_str)
else:
good = lisp_valid_prefix_format(addr_str)
#endif
if (good == False): return(False)
#
# No "->" so no group, return.
#
if (group_str == None): return(True)
index = lisp_valid_iid_format(group_str)
if (index == -1): return(False)
addr_str = group_str[index::]
if (group_str.find("/") == -1):
good = lisp.lisp_valid_address_format("address", addr_str)
else:
good = lisp_valid_prefix_format(addr_str)
#endif
if (good == False): return(False)
return(True)
#enddef
#
# lisp_valid_iid_format
#
# Check the string to make sure we have a "[<iid>]" where <iid> is a decimal
# number from 0 to 0xffffffff. If brackets are not found, return 0. No
# "[<iid>]" is a valid one. If a valid string is found return index after
# the "]" character.
#
def lisp_valid_iid_format(iid_str):
start = iid_str.find("[")
if (start == -1): return(0)
if (start != 0): return(-1)
end = iid_str.find("]")
if (end == -1): return(-1)
if ((start+1) == (end-1)):
value = iid_str[start+1]
else:
value = iid_str[start+1:end-1]
#endif
if (value.isdigit() == False): return(-1)
return(end + 1)
#enddef
#
# lisp_valid_prefix_format
#
# Check to see if the string is a valid prefix. We are validating IPv4 and IPv6
# prefixes to have a / and that the address format is correct. Allow
# Distinguished-Names to not require a slash.
#
def lisp_valid_prefix_format(prefix):
if (prefix.find("'") == -1 and prefix.find("/") == -1): return(False)
addr = prefix.split("/")
if (len(addr) != 2):
if (prefix.find("'") == -1): return(False)
addr = [prefix, len(prefix)]
#endif
return(lisp.lisp_valid_address_format("address", addr[0]))
#enddef
#
# lisp_validate_range
#
# Return -1 for mask-len if the range is not in a power-of-2 block. If value
# is "*", then the iid is 0 and the mask-length is 0. This is the ultimate
# default.
#
def lisp_validate_range(value):
if (value == "*"): return([0, 0])
value = value.split("-")
lower = int(value[0])
upper = int(value[1])
if (lower > upper): return([0, -1])
#
# Range must be a power-of-2. Therefore, the upper must be 2**n - 1. Add
# 1 and search for the bit. That will produce the mask length.
#
block = upper - lower + 1
mask_len = str(math.log(block, 2))
mask_len = mask_len.split(".")
if (len(mask_len) == 1 or int(mask_len[1]) != 0): return([0, 0])
mask_len = 32 - int(mask_len[0])
return([lower, mask_len])
#enddef
#
# lisp_setup_kv_pairs
#
# For each sub-clause command; "prefix", "rloc", "delegate", "referral",
# "elp-node", "rle-node", "peer", "allowed-prefix", and "allowed-rloc",
# create an array with number of elements equal to the number of occurences
# of each sub-clause command.
#
# This allows to not supply every sub-command for a sub-clause. For example:
#
# lisp map-cache {
# prefix {
# eid-prefix = 10.0.0.0/8
# }
# prefix {
# instance-id = 13
# eid-prefix = 11.0.0.0/8
# }
# rloc {
# address = 1.1.1.1
# geo-name = san-jose
# priority = 1
# }
# rloc {
# elp-name = via-att
# priority = 1
# }
# }
#
def lisp_setup_kv_pairs(clause):
kv_pairs = {}
#
# Remove comment lines so if keywords appear in comment lines they are
# not counted.
#
index = clause.find("#")
while (index != -1):
end = clause[index::].find("\n")
if (end == -1): end = 0
clause = clause[:index] + clause[index+end+1::]
index = clause.find("#")
#endwhile
count = clause.count(" prefix {")
if (count != 0):
kv_pairs["instance-id"] = [""] * count
kv_pairs["secondary-instance-id"] = [""] * count
kv_pairs["eid-prefix"] = [""] * count
kv_pairs["group-prefix"] = [""] * count
kv_pairs["mr-name"] = [""] * count
kv_pairs["ms-name"] = [""] * count
kv_pairs["dynamic-eid"] = [""] * count
kv_pairs["signature-eid"] = [""] * count
kv_pairs["register-ttl"] = [""] * count
kv_pairs["send-map-request"] = [""] * count
kv_pairs["subscribe-request"] = [""] * count
#endif
count = clause.count(" rloc {")
if (count != 0):
kv_pairs["address"] = [""] * count
kv_pairs["interface"] = [""] * count
kv_pairs["rloc-record-name"] = [""] * count
kv_pairs["elp-name"] = [""] * count
kv_pairs["geo-name"] = [""] * count
kv_pairs["rle-name"] = [""] * count
kv_pairs["json-name"] = [""] * count
kv_pairs["priority"] = [""] * count
kv_pairs["weight"] = [""] * count
#endif
count = clause.count(" match {")
if (count != 0):
kv_pairs["instance-id"] = [""] * count
kv_pairs["source-eid"] = [""] * count
kv_pairs["destination-eid"] = [""] * count
kv_pairs["source-rloc"] = [""] * count
kv_pairs["destination-rloc"] = [""] * count
kv_pairs["rloc-record-name"] = [""] * count
kv_pairs["elp-name"] = [""] * count
kv_pairs["geo-name"] = [""] * count
kv_pairs["rle-name"] = [""] * count
kv_pairs["json-name"] = [""] * count
kv_pairs["datetime-range"] = [""] * count
#endif
count = clause.count(" elp-node {")
if (count != 0):
kv_pairs["address"] = [""] * count
kv_pairs["strict"] = [""] * count
kv_pairs["probe"] = [""] * count
kv_pairs["eid"] = [""] * count
#endif
count = clause.count(" rle-node {")
if (count != 0):
kv_pairs["address"] = [""] * count
kv_pairs["level"] = [""] * count
#endif
count = clause.count(" referral {")
if (count != 0):
kv_pairs["address"] = [""] * count
kv_pairs["priority"] = [""] * count
kv_pairs["weight"] = [""] * count
#endif
count = clause.count(" delegate {")
if (count != 0):
kv_pairs["address"] = [""] * count
kv_pairs["node-type"] = [""] * count
kv_pairs["priority"] = [""] * count
kv_pairs["weight"] = [""] * count
kv_pairs["public-key"] = [""] * count
#endif
count = clause.count(" allowed-rloc {")
if (count != 0):
kv_pairs["address"] = [""] * count
kv_pairs["priority"] = [""] * count
kv_pairs["weight"] = [""] * count
#endif
count = clause.count(" allowed-prefix {")
if (count != 0):
kv_pairs["instance-id"] = [""] * count
kv_pairs["eid-prefix"] = [""] * count
kv_pairs["group-prefix"] = [""] * count
kv_pairs["accept-more-specifics"] = [""] * count
kv_pairs["force-proxy-reply"] = [""] * count
kv_pairs["force-nat-proxy-reply"] = [""] * count
kv_pairs["force-ttl"] = [""] * count
kv_pairs["pitr-proxy-reply-drop"] = [""] * count
kv_pairs["proxy-reply-action"] = [""] * count
kv_pairs["require-signature"] = [""] * count
kv_pairs["encrypt-json"] = [""] * count
kv_pairs["echo-nonce-capable"] = [""] * count
kv_pairs["policy-name"] = [""] * count
#endif
count = clause.count(" peer {")
if (count != 0):
kv_pairs["address"] = [""] * count
kv_pairs["priority"] = [""] * count
kv_pairs["weight"] = [""] * count
#endif
#
# There is only one instance of the "lisp xtr-parameters" command.
#
count = clause.count("data-plane-security =")
if (count != 0):
kv_pairs["data-plane-security"] = [""] * count
#endif
count = clause.count("data-plane-logging =")
if (count != 0):
kv_pairs["data-plane-logging"] = [""] * count
#endif
count = clause.count("frame-logging =")
if (count != 0):
kv_pairs["frame-logging"] = [""] * count
#endif
count = clause.count("flow-logging =")
if (count != 0):
kv_pairs["flow-logging"] = [""] * count
#endif
count = clause.count("nat-traversal =")
if (count != 0):
kv_pairs["nat-traversal"] = [""] * count
#endif
count = clause.count("decentralized-nat =")
if (count != 0):
kv_pairs["decentralized-nat"] = [""] * count
#endif
count = clause.count("rloc-probing =")
if (count != 0):
kv_pairs["rloc-probing"] = [""] * count
#endif
count = clause.count("nonce-echoing =")
if (count != 0):
kv_pairs["nonce-echoing"] = [""] * count
#endif
count = clause.count("checkpoint-map-cache =")
if (count != 0):
kv_pairs["checkpoint-map-cache"] = [""] * count
#endif
count = clause.count("ipc-data-plane =")
if (count != 0):
kv_pairs["ipc-data-plane"] = [""] * count
#endif
count = clause.count("program-hardware =")
if (count != 0):
kv_pairs["program-hardware"] = [""] * count
#endif
count = clause.count("decentralized-push-xtr =")
if (count != 0):
kv_pairs["decentralized-push-xtr"] = [""] * count
#endif
count = clause.count("decentralized-pull-xtr-modulus =")
if (count != 0):
kv_pairs["decentralized-pull-xtr-modulus"] = [""] * count
#endif
count = clause.count("decentralized-pull-xtr-dns-suffix =")
if (count != 0):
kv_pairs["decentralized-pull-xtr-dns-suffix"] = [""] * count
#endif
count = clause.count("register-reachable-rtrs =")
if (count != 0):
kv_pairs["register-reachable-rtrs"] = [""] * count
#endif
#
# There is a special case here where there are no sub-clauses but
# "address" appears multiple times in a command clause. Let's allocate
# enough array space to store all of them. This is referring to the
# "lisp map-server" command.
#
if (kv_pairs == {}):
count = max(clause.count("address ="), clause.count("dns-name ="))
if (count != 0): kv_pairs["address"] = [""] * count
if (count != 0): kv_pairs["dns-name"] = [""] * count
count = clause.count("mr-name =")
if (count != 0): kv_pairs["mr-name"] = [""] * count
count = clause.count("ms-name =")
if (count != 0): kv_pairs["ms-name"] = [""] * count
#endif
return(kv_pairs)
#enddef
#
# lisp_clause_syntax_error
#
# If lisp_syntax_check() built an array for the command 'parm', then it was
# in the required sub-command clause. Otherwise, we return an error and
# display a message to the log.
#
def lisp_clause_syntax_error(kv_pair, parm, clause):
is_array = (parm in kv_pair and type(kv_pair[parm]) == list)
if (is_array): return(False)
err_str = lisp.bold("Syntax error", False)
lisp.fprint("{}, '{}' not in '{}' clause".format(err_str, parm, clause))
return(True)
#enddef
#
# lisp_syntax_check
#
# Return null string if the syntax checking was clean. Otherwise, return
# the command clause with ">>>".
#
def lisp_syntax_check(kv_pairs, clause):
#
# First look for number of subclauses of the same type. Assume the
# syntax is correct.
#
new_kv_pairs = lisp_setup_kv_pairs(clause)
clause = clause.split("\n")
new_clause = ""
open_brace = 0
error = False
index = 0
last_subclause = ""
for line in clause:
if (len(line) == 0): continue
if (line == ""): continue
if (lisp_comment(line)):
line = line.replace("#", "%")
new_clause += lisp_write_line(line)
continue
#endif
json = line.find("json-string") != -1
if (lisp_begin_clause(line) and json == False):
new_clause += lisp_write_line(line)
open_brace += 1
if (last_subclause == line):
index += 1
else:
index = 0
last_subclause = line
#endif
continue
#endif
if (lisp_end_clause(line) and json == False):
open_brace -= 1
if (open_brace == 0):
new_clause += lisp_write_line(line)
break
#endif
if (error):
new_clause += "%" + line + "\n"
else:
new_clause += lisp_write_line(line)
#endif
continue
#endif
if (error):
new_clause += "%" + line + "\n"
continue
#endif
#
# An equal sign must be on the subcommand line. Allow equal sign to be
# in data field.
#
cmd = line.split("=", 1)
if (len(cmd) == 1):
new_clause += lisp_write_error(line, "no equal sign")
error = True
continue
#endif
kw = cmd[0].replace(" ", "")
#
# For values we allow whitespace in. Allow distinguished-names to
# have spaces in it as well as json-strings.
#
if (kw == "description" or kw == "geo-tag"):
value = cmd[1]
elif (kw == "json-string"):
value = cmd[1][1::]
else:
if (kw == "eid-prefix" and cmd[1].count("'") == 2):
value = cmd[1][1::]
elif (kw == "instance-id"):
value = cmd[1][1::]
else:
value = cmd[1].replace(" ", "")
#endif
#endif
kw = kw.replace("\t", "")
value = value.replace("\t", "")
if (kw not in kv_pairs):
new_clause += lisp_write_error(line, "invalid command keyword")
error = True
continue
#endif
#
# If there is no value type, then this could be an "address" that needs
# semantic check. There could also be a prefix that needs a format
# check. Right now special case keyword "eid-prefix" to be a prefix
# type.
#
if (len(kv_pairs[kw]) <= 1):
msg = ""
if (kw == "eid-prefix" and not lisp_valid_prefix_format(value)):
msg = "invalid prefix"
#endif
if (kw == "address" and not \
lisp.lisp_valid_address_format(kw, value)):
msg = "invalid address"
#endif
if (msg != ""):
new_clause += lisp_write_error(line, msg)
error = True
continue
#endif
#endif
#
# Check if this command can have an instance-ID range, a "*", or
# a single instance-ID value.
#
if (len(kv_pairs[kw]) == 4 and kv_pairs[kw][3]):
if (kw == "instance-id"):
is_range = (value.find("-") != -1)
is_ultimate = (value == "*")
if (is_range or is_ultimate):
lower, mask_len = lisp_validate_range(value)
if (mask_len == -1):
new_clause += lisp_write_error(line, "invalid range")
error = True
continue
#endif
else:
lower = value
mask_len = 32
#endif
value = str(lower) + "-" + str(mask_len)
#endif
#endif
#
# Multi-instance commands and clauses makes value into an array. The
# while loop is for cases where a command can appear multiple times
# not inside of sub-clause.
#
if (kv_pairs[kw][0] and kw in new_kv_pairs):
if (new_kv_pairs[kw][index] != ""): index += 1
new_kv_pairs[kw][index] = value
else:
new_kv_pairs[kw] = value
#endif
#
# Check value in range, if value type are integers, otherwise match
# keyword type value. If value is a range, remove "-" and mask-len
# to see if lower range value is allowable in range.
#
if (len(kv_pairs[kw]) > 1):
value_type = kv_pairs[kw][1]
if (type(value_type) == int):
if (type(value) == str): value = value.split("-")[0]
value = int(value)
if (value < kv_pairs[kw][1] and value > kv_pairs[kw][2]):
new_clause += lisp_write_error(line, "invalid range value")
error = True
continue
#endif
elif (value not in kv_pairs[kw][1::]):
new_clause += lisp_write_error(line, "invalid value keyword")
error = True
continue
#endif
#endif
#
# Write good command to return buffer.
#
new_clause += lisp_write_line(line)
#endfor
if (error == True):
new_clause = new_clause.replace("%", "#")
else:
new_clause = new_clause.replace("#", "")
new_clause = new_clause.replace("%", "#")
#endif
return([error, new_clause, new_kv_pairs])
#enddef
#
# lisp_process_command
#
# Process commands in components (not the lisp-core process except for debug
# commands).
#
# Variable "clause" is a string and not a byte string. Caller converts.
#
def lisp_process_command(lisp_socket, opcode, clause, process, command_set):
#
# For configuration commands get first two tokens. For show commands, a
# summary or table display will have only two tokens. However, a detailed
# display will have parameters following a "%" sign.
#
command = clause.split(" ")
#
# If debug command, turn on/off boolean.
#
if (clause.find("lisp debug") != -1):
if (clause.find("= yes") != -1):
lisp.lisp_debug_logging = True
enable = lisp.bold("Enable", False)
lisp.lprint("{} process debug logging".format(enable))
#endif
if (clause.find("= no") != -1):
disable = lisp.bold("Disable", False)
lisp.lprint("{} process debug logging".format(disable))
lisp.lisp_debug_logging = False
#endif
return
#endif
#
# Handle show commands differently than configuration commands. We have
# to pass parameters for detailed displays and lookups.
#
is_show_command = (command[0] == "show")
if (is_show_command):
parameters = clause.split("%")
command = parameters[0].split(" ")
parm_len = len(parameters)
if (parm_len == 2):
parameters = parameters[1]
elif (parm_len == 3):
parameters = parameters[1] + "%" + parameters[2] + "%"
else:
parameters = ""
#endif
#endif
command = command[0] + " " + command[1]
for cmds in command_set:
if (command in cmds):
command_processor, kv_pairs = cmds[command]
break
#endif
if (cmds == command_set[-1]):
lisp.lprint("Invalid command found '{}'".format(command))
return
#endif
#endfor
#
# Configuration commands will have kv-pairs and will produce a new
# clause the same as the input one or a commented out clause showing
# the error line. Show commands don't have any input checking so they
# will return show output. But the parameter passed to show commands could
# be for a detailed display.
#
error = False
if (len(kv_pairs) != 0):
error, new_clause, kv_pairs = lisp_syntax_check(kv_pairs, clause)
if (error):
lisp.lprint("Command syntax error: {}".format(new_clause))
else:
command_processor(kv_pairs)
#endif
else:
if (is_show_command): kv_pairs = parameters
new_clause = command_processor(kv_pairs)
#endif
ipc = lisp.lisp_command_ipc(new_clause, process)
lisp.lisp_ipc(ipc, lisp_socket, "lisp-core")
return
#enddef
#
# lisp_is_user_superuser
#
# Return True if supplied user is super-user.
#
def lisp_is_user_superuser(username):
if (username == None): username = lisp_get_user()