]> git.ipfire.org Git - location/libloc.git/blob - src/python/location-importer.in
45cacad81c58db8b0aeed25cac7333092c54049e
[location/libloc.git] / src / python / location-importer.in
1 #!/usr/bin/python3
2 ###############################################################################
3 # #
4 # libloc - A library to determine the location of someone on the Internet #
5 # #
6 # Copyright (C) 2020 IPFire Development Team <info@ipfire.org> #
7 # #
8 # This library is free software; you can redistribute it and/or #
9 # modify it under the terms of the GNU Lesser General Public #
10 # License as published by the Free Software Foundation; either #
11 # version 2.1 of the License, or (at your option) any later version. #
12 # #
13 # This library is distributed in the hope that it will be useful, #
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
16 # Lesser General Public License for more details. #
17 # #
18 ###############################################################################
19
20 import argparse
21 import ipaddress
22 import logging
23 import math
24 import re
25 import sys
26 import telnetlib
27
28 # Load our location module
29 import location
30 import location.database
31 import location.importer
32 from location.i18n import _
33
34 # Initialise logging
35 log = logging.getLogger("location.importer")
36 log.propagate = 1
37
38 class CLI(object):
39 def parse_cli(self):
40 parser = argparse.ArgumentParser(
41 description=_("Location Importer Command Line Interface"),
42 )
43 subparsers = parser.add_subparsers()
44
45 # Global configuration flags
46 parser.add_argument("--debug", action="store_true",
47 help=_("Enable debug output"))
48
49 # version
50 parser.add_argument("--version", action="version",
51 version="%(prog)s @VERSION@")
52
53 # Database
54 parser.add_argument("--database-host", required=True,
55 help=_("Database Hostname"), metavar=_("HOST"))
56 parser.add_argument("--database-name", required=True,
57 help=_("Database Name"), metavar=_("NAME"))
58 parser.add_argument("--database-username", required=True,
59 help=_("Database Username"), metavar=_("USERNAME"))
60 parser.add_argument("--database-password", required=True,
61 help=_("Database Password"), metavar=_("PASSWORD"))
62
63 # Write Database
64 write = subparsers.add_parser("write", help=_("Write database to file"))
65 write.set_defaults(func=self.handle_write)
66 write.add_argument("file", nargs=1, help=_("Database File"))
67 write.add_argument("--signing-key", nargs="?", type=open, help=_("Signing Key"))
68 write.add_argument("--vendor", nargs="?", help=_("Sets the vendor"))
69 write.add_argument("--description", nargs="?", help=_("Sets a description"))
70 write.add_argument("--license", nargs="?", help=_("Sets the license"))
71
72 # Update WHOIS
73 update_whois = subparsers.add_parser("update-whois", help=_("Update WHOIS Information"))
74 update_whois.set_defaults(func=self.handle_update_whois)
75
76 # Update announcements
77 update_announcements = subparsers.add_parser("update-announcements",
78 help=_("Update BGP Annoucements"))
79 update_announcements.set_defaults(func=self.handle_update_announcements)
80 update_announcements.add_argument("server", nargs=1,
81 help=_("Route Server to connect to"), metavar=_("SERVER"))
82
83 # Update overrides
84 update_overrides = subparsers.add_parser("update-overrides",
85 help=_("Update overrides"),
86 )
87 update_overrides.add_argument(
88 "files", nargs="+", help=_("Files to import"),
89 )
90 update_overrides.set_defaults(func=self.handle_update_overrides)
91
92 args = parser.parse_args()
93
94 # Enable debug logging
95 if args.debug:
96 location.logger.set_level(logging.DEBUG)
97
98 # Print usage if no action was given
99 if not "func" in args:
100 parser.print_usage()
101 sys.exit(2)
102
103 return args
104
105 def run(self):
106 # Parse command line arguments
107 args = self.parse_cli()
108
109 # Initialise database
110 self.db = self._setup_database(args)
111
112 # Call function
113 ret = args.func(args)
114
115 # Return with exit code
116 if ret:
117 sys.exit(ret)
118
119 # Otherwise just exit
120 sys.exit(0)
121
122 def _setup_database(self, ns):
123 """
124 Initialise the database
125 """
126 # Connect to database
127 db = location.database.Connection(
128 host=ns.database_host, database=ns.database_name,
129 user=ns.database_username, password=ns.database_password,
130 )
131
132 with db.transaction():
133 db.execute("""
134 -- announcements
135 CREATE TABLE IF NOT EXISTS announcements(network inet, autnum bigint,
136 first_seen_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
137 last_seen_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP);
138 CREATE UNIQUE INDEX IF NOT EXISTS announcements_networks ON announcements(network);
139 CREATE INDEX IF NOT EXISTS announcements_family ON announcements(family(network));
140
141 -- autnums
142 CREATE TABLE IF NOT EXISTS autnums(number bigint, name text NOT NULL);
143 CREATE UNIQUE INDEX IF NOT EXISTS autnums_number ON autnums(number);
144
145 -- networks
146 CREATE TABLE IF NOT EXISTS networks(network inet, country text);
147 CREATE UNIQUE INDEX IF NOT EXISTS networks_network ON networks(network);
148 CREATE INDEX IF NOT EXISTS networks_search ON networks USING GIST(network inet_ops);
149
150 -- overrides
151 CREATE TABLE IF NOT EXISTS autnum_overrides(
152 number bigint NOT NULL,
153 name text,
154 is_anonymous_proxy boolean DEFAULT FALSE,
155 is_satellite_provider boolean DEFAULT FALSE,
156 is_anycast boolean DEFAULT FALSE
157 );
158 CREATE UNIQUE INDEX IF NOT EXISTS autnum_overrides_number
159 ON autnum_overrides(number);
160
161 CREATE TABLE IF NOT EXISTS network_overrides(
162 network inet NOT NULL,
163 country text,
164 is_anonymous_proxy boolean DEFAULT FALSE,
165 is_satellite_provider boolean DEFAULT FALSE,
166 is_anycast boolean DEFAULT FALSE
167 );
168 CREATE UNIQUE INDEX IF NOT EXISTS network_overrides_network
169 ON network_overrides(network);
170 """)
171
172 return db
173
174 def handle_write(self, ns):
175 """
176 Compiles a database in libloc format out of what is in the database
177 """
178 print(ns)
179
180 # Allocate a writer
181 writer = location.Writer(ns.signing_key)
182
183 # Set all metadata
184 if ns.vendor:
185 writer.vendor = ns.vendor
186
187 if ns.description:
188 writer.description = ns.description
189
190 if ns.license:
191 writer.license = ns.license
192
193 # Add all Autonomous Systems
194 log.info("Writing Autonomous Systems...")
195
196 # Select all ASes with a name
197 rows = self.db.query("""
198 SELECT
199 autnums.number AS number,
200 COALESCE(
201 (SELECT overrides.name FROM autnum_overrides overrides
202 WHERE overrides.number = autnums.number),
203 autnums.name
204 ) AS name
205 FROM autnums
206 WHERE name <> %s ORDER BY number
207 """, "")
208
209 for row in rows:
210 a = writer.add_as(row.number)
211 a.name = row.name
212
213 # Add all networks
214 log.info("Writing networks...")
215
216 # Select all known networks
217 rows = self.db.query("""
218 SELECT
219 DISTINCT ON (announcements.network)
220 announcements.network AS network,
221 announcements.autnum AS autnum,
222 networks.country AS country,
223
224 -- Must be part of returned values for ORDER BY clause
225 masklen(networks.network) AS sort,
226
227 -- Flags
228 COALESCE(
229 (
230 SELECT is_anonymous_proxy FROM network_overrides overrides
231 WHERE announcements.network <<= overrides.network
232 ORDER BY masklen(overrides.network) DESC
233 LIMIT 1
234 ),
235 (
236 SELECT is_anonymous_proxy FROM autnum_overrides overrides
237 WHERE announcements.autnum = overrides.number
238 )
239 ) AS is_anonymous_proxy,
240 COALESCE(
241 (
242 SELECT is_satellite_provider FROM network_overrides overrides
243 WHERE announcements.network <<= overrides.network
244 ORDER BY masklen(overrides.network) DESC
245 LIMIT 1
246 ),
247 (
248 SELECT is_satellite_provider FROM autnum_overrides overrides
249 WHERE announcements.autnum = overrides.number
250 )
251 ) AS is_satellite_provider,
252 COALESCE(
253 (
254 SELECT is_anycast FROM network_overrides overrides
255 WHERE announcements.network <<= overrides.network
256 ORDER BY masklen(overrides.network) DESC
257 LIMIT 1
258 ),
259 (
260 SELECT is_anycast FROM autnum_overrides overrides
261 WHERE announcements.autnum = overrides.number
262 )
263 ) AS is_anycast
264 FROM announcements
265 LEFT JOIN networks ON announcements.network <<= networks.network
266 ORDER BY announcements.network, sort DESC
267 """)
268
269 for row in rows:
270 network = writer.add_network(row.network)
271
272 # Save AS & country
273 network.asn, network.country_code = row.autnum, row.country
274
275 # Set flags
276 if row.is_anonymous_proxy:
277 network.set_flag(location.NETWORK_FLAG_ANONYMOUS_PROXY)
278
279 if row.is_satellite_provider:
280 network.set_flag(location.NETWORK_FLAG_SATELLITE_PROVIDER)
281
282 if row.is_anycast:
283 network.set_flag(location.NETWORK_FLAG_ANYCAST)
284
285 # Write everything to file
286 log.info("Writing database to file...")
287 for file in ns.file:
288 writer.write(file)
289
290 def handle_update_whois(self, ns):
291 downloader = location.importer.Downloader()
292
293 # Download all sources
294 with self.db.transaction():
295 # Create some temporary tables to store parsed data
296 self.db.execute("""
297 CREATE TEMPORARY TABLE _autnums(number integer, organization text)
298 ON COMMIT DROP;
299 CREATE UNIQUE INDEX _autnums_number ON _autnums(number);
300
301 CREATE TEMPORARY TABLE _organizations(handle text, name text)
302 ON COMMIT DROP;
303 CREATE UNIQUE INDEX _organizations_handle ON _organizations(handle);
304 """)
305
306 for source in location.importer.WHOIS_SOURCES:
307 with downloader.request(source, return_blocks=True) as f:
308 for block in f:
309 self._parse_block(block)
310
311 self.db.execute("""
312 INSERT INTO autnums(number, name)
313 SELECT _autnums.number, _organizations.name FROM _autnums
314 LEFT JOIN _organizations ON _autnums.organization = _organizations.handle
315 ON CONFLICT (number) DO UPDATE SET name = excluded.name;
316 """)
317
318 # Download all extended sources
319 for source in location.importer.EXTENDED_SOURCES:
320 with self.db.transaction():
321 # Download data
322 with downloader.request(source) as f:
323 for line in f:
324 self._parse_line(line)
325
326 def _parse_block(self, block):
327 # Get first line to find out what type of block this is
328 line = block[0]
329
330 # aut-num
331 if line.startswith("aut-num:"):
332 return self._parse_autnum_block(block)
333
334 # organisation
335 elif line.startswith("organisation:"):
336 return self._parse_org_block(block)
337
338 def _parse_autnum_block(self, block):
339 autnum = {}
340 for line in block:
341 # Split line
342 key, val = split_line(line)
343
344 if key == "aut-num":
345 m = re.match(r"^(AS|as)(\d+)", val)
346 if m:
347 autnum["asn"] = m.group(2)
348
349 elif key == "org":
350 autnum[key] = val
351
352 # Skip empty objects
353 if not autnum:
354 return
355
356 # Insert into database
357 self.db.execute("INSERT INTO _autnums(number, organization) \
358 VALUES(%s, %s) ON CONFLICT (number) DO UPDATE SET \
359 organization = excluded.organization",
360 autnum.get("asn"), autnum.get("org"),
361 )
362
363 def _parse_org_block(self, block):
364 org = {}
365 for line in block:
366 # Split line
367 key, val = split_line(line)
368
369 if key in ("organisation", "org-name"):
370 org[key] = val
371
372 # Skip empty objects
373 if not org:
374 return
375
376 self.db.execute("INSERT INTO _organizations(handle, name) \
377 VALUES(%s, %s) ON CONFLICT (handle) DO \
378 UPDATE SET name = excluded.name",
379 org.get("organisation"), org.get("org-name"),
380 )
381
382 def _parse_line(self, line):
383 # Skip version line
384 if line.startswith("2"):
385 return
386
387 # Skip comments
388 if line.startswith("#"):
389 return
390
391 try:
392 registry, country_code, type, line = line.split("|", 3)
393 except:
394 log.warning("Could not parse line: %s" % line)
395 return
396
397 # Skip any lines that are for stats only
398 if country_code == "*":
399 return
400
401 if type in ("ipv6", "ipv4"):
402 return self._parse_ip_line(country_code, type, line)
403
404 def _parse_ip_line(self, country, type, line):
405 try:
406 address, prefix, date, status, organization = line.split("|")
407 except ValueError:
408 organization = None
409
410 # Try parsing the line without organization
411 try:
412 address, prefix, date, status = line.split("|")
413 except ValueError:
414 log.warning("Unhandled line format: %s" % line)
415 return
416
417 # Skip anything that isn't properly assigned
418 if not status in ("assigned", "allocated"):
419 return
420
421 # Cast prefix into an integer
422 try:
423 prefix = int(prefix)
424 except:
425 log.warning("Invalid prefix: %s" % prefix)
426 return
427
428 # Fix prefix length for IPv4
429 if type == "ipv4":
430 prefix = 32 - int(math.log(prefix, 2))
431
432 # Try to parse the address
433 try:
434 network = ipaddress.ip_network("%s/%s" % (address, prefix), strict=False)
435 except ValueError:
436 log.warning("Invalid IP address: %s" % address)
437 return
438
439 self.db.execute("INSERT INTO networks(network, country) \
440 VALUES(%s, %s) ON CONFLICT (network) DO \
441 UPDATE SET country = excluded.country",
442 "%s" % network, country,
443 )
444
445 def handle_update_announcements(self, ns):
446 server = ns.server[0]
447
448 # Pre-compile regular expression for routes
449 #route = re.compile(b"^\*>?\s[\si]?([^\s]+)[.\s]*?(\d+)\si$", re.MULTILINE)
450 route = re.compile(b"^\*[\s\>]i([^\s]+).+?(\d+)\si\r\n", re.MULTILINE|re.DOTALL)
451
452 with telnetlib.Telnet(server) as t:
453 # Enable debug mode
454 #if ns.debug:
455 # t.set_debuglevel(10)
456
457 # Wait for console greeting
458 greeting = t.read_until(b"> ")
459 log.debug(greeting.decode())
460
461 # Disable pagination
462 t.write(b"terminal length 0\n")
463
464 # Wait for the prompt to return
465 t.read_until(b"> ")
466
467 # Fetch the routing tables
468 with self.db.transaction():
469 for protocol in ("ipv6", "ipv4"):
470 log.info("Requesting %s routing table" % protocol)
471
472 # Request the full unicast routing table
473 t.write(b"show bgp %s unicast\n" % protocol.encode())
474
475 # Read entire header which ends with "Path"
476 t.read_until(b"Path\r\n")
477
478 while True:
479 # Try reading a full entry
480 # Those might be broken across multiple lines but ends with i
481 line = t.read_until(b"i\r\n", timeout=5)
482 if not line:
483 break
484
485 # Show line for debugging
486 #log.debug(repr(line))
487
488 # Try finding a route in here
489 m = route.match(line)
490 if m:
491 network, autnum = m.groups()
492
493 # Convert network to string
494 network = network.decode()
495
496 # Append /24 for IPv4 addresses
497 if not "/" in network and not ":" in network:
498 network = "%s/24" % network
499
500 # Convert AS number to integer
501 autnum = int(autnum)
502
503 log.info("Found announcement for %s by %s" % (network, autnum))
504
505 self.db.execute("INSERT INTO announcements(network, autnum) \
506 VALUES(%s, %s) ON CONFLICT (network) DO \
507 UPDATE SET autnum = excluded.autnum, last_seen_at = CURRENT_TIMESTAMP",
508 network, autnum,
509 )
510
511 log.info("Finished reading the %s routing table" % protocol)
512
513 # Purge anything we never want here
514 self.db.execute("""
515 -- Delete default routes
516 DELETE FROM announcements WHERE network = '::/0' OR network = '0.0.0.0/0';
517
518 -- Delete anything that is not global unicast address space
519 DELETE FROM announcements WHERE family(network) = 6 AND NOT network <<= '2000::/3';
520
521 -- DELETE "current network" address space
522 DELETE FROM announcements WHERE family(network) = 4 AND network <<= '0.0.0.0/8';
523
524 -- DELETE local loopback address space
525 DELETE FROM announcements WHERE family(network) = 4 AND network <<= '127.0.0.0/8';
526
527 -- DELETE RFC 1918 address space
528 DELETE FROM announcements WHERE family(network) = 4 AND network <<= '10.0.0.0/8';
529 DELETE FROM announcements WHERE family(network) = 4 AND network <<= '172.16.0.0/12';
530 DELETE FROM announcements WHERE family(network) = 4 AND network <<= '192.168.0.0/16';
531
532 -- DELETE test, benchmark and documentation address space
533 DELETE FROM announcements WHERE family(network) = 4 AND network <<= '192.0.0.0/24';
534 DELETE FROM announcements WHERE family(network) = 4 AND network <<= '192.0.2.0/24';
535 DELETE FROM announcements WHERE family(network) = 4 AND network <<= '198.18.0.0/15';
536 DELETE FROM announcements WHERE family(network) = 4 AND network <<= '198.51.100.0/24';
537 DELETE FROM announcements WHERE family(network) = 4 AND network <<= '203.0.113.0/24';
538
539 -- DELETE CGNAT address space (RFC 6598)
540 DELETE FROM announcements WHERE family(network) = 4 AND network <<= '100.64.0.0/10';
541
542 -- DELETE link local address space
543 DELETE FROM announcements WHERE family(network) = 4 AND network <<= '169.254.0.0/16';
544
545 -- DELETE IPv6 to IPv4 (6to4) address space
546 DELETE FROM announcements WHERE family(network) = 4 AND network <<= '192.88.99.0/24';
547
548 -- DELETE multicast and reserved address space
549 DELETE FROM announcements WHERE family(network) = 4 AND network <<= '224.0.0.0/4';
550 DELETE FROM announcements WHERE family(network) = 4 AND network <<= '240.0.0.0/4';
551
552 -- Delete networks that are too small to be in the global routing table
553 DELETE FROM announcements WHERE family(network) = 6 AND masklen(network) > 48;
554 DELETE FROM announcements WHERE family(network) = 4 AND masklen(network) > 24;
555
556 -- Delete any non-public or reserved ASNs
557 DELETE FROM announcements WHERE NOT (
558 (autnum >= 1 AND autnum <= 23455)
559 OR
560 (autnum >= 23457 AND autnum <= 64495)
561 OR
562 (autnum >= 131072 AND autnum <= 4199999999)
563 );
564
565 -- Delete everything that we have not seen for 14 days
566 DELETE FROM announcements WHERE last_seen_at <= CURRENT_TIMESTAMP - INTERVAL '14 days';
567 """)
568
569 def handle_update_overrides(self, ns):
570 with self.db.transaction():
571 # Drop all data that we have
572 self.db.execute("""
573 TRUNCATE TABLE autnum_overrides;
574 TRUNCATE TABLE network_overrides;
575 """)
576
577 for file in ns.files:
578 log.info("Reading %s..." % file)
579
580 with open(file, "rb") as f:
581 for type, block in location.importer.read_blocks(f):
582 if type == "net":
583 network = block.get("net")
584 # Try to parse and normalise the network
585 try:
586 network = ipaddress.ip_network(network, strict=False)
587 except ValueError as e:
588 log.warning("Invalid IP network: %s: %s" % (network, e))
589 continue
590
591 self.db.execute("""
592 INSERT INTO network_overrides(
593 network,
594 country,
595 is_anonymous_proxy,
596 is_satellite_provider,
597 is_anycast
598 ) VALUES (%s, %s, %s, %s, %s)
599 ON CONFLICT (network) DO NOTHING""",
600 "%s" % network,
601 block.get("country"),
602 block.get("is-anonymous-proxy") == "yes",
603 block.get("is-satellite-provider") == "yes",
604 block.get("is-anycast") == "yes",
605 )
606
607 elif type == "autnum":
608 autnum = block.get("autnum")
609
610 # Check if AS number begins with "AS"
611 if not autnum.startswith("AS"):
612 log.warning("Invalid AS number: %s" % autnum)
613 continue
614
615 # Strip "AS"
616 autnum = autnum[2:]
617
618 self.db.execute("""
619 INSERT INTO autnum_overrides(
620 number,
621 name,
622 is_anonymous_proxy,
623 is_satellite_provider,
624 is_anycast
625 ) VALUES(%s, %s, %s, %s, %s)
626 ON CONFLICT DO NOTHING""",
627 autnum, block.get("name"),
628 block.get("is-anonymous-proxy") == "yes",
629 block.get("is-satellite-provider") == "yes",
630 block.get("is-anycast") == "yes",
631 )
632
633 else:
634 log.warning("Unsupport type: %s" % type)
635
636
637 def split_line(line):
638 key, colon, val = line.partition(":")
639
640 # Strip any excess space
641 key = key.strip()
642 val = val.strip()
643
644 return key, val
645
646 def main():
647 # Run the command line interface
648 c = CLI()
649 c.run()
650
651 main()