]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/python/location-importer.in
Bump database version to "1"
[people/ms/libloc.git] / src / python / location-importer.in
index 5cfa09efe95e9c1f78425572e2e1a7039147d3bd..eb4a303b80aa0d375f3184e893f2cf8981a154ed 100644 (file)
@@ -45,6 +45,8 @@ class CLI(object):
                # Global configuration flags
                parser.add_argument("--debug", action="store_true",
                        help=_("Enable debug output"))
+               parser.add_argument("--quiet", action="store_true",
+                       help=_("Enable quiet mode"))
 
                # version
                parser.add_argument("--version", action="version",
@@ -68,6 +70,7 @@ class CLI(object):
                write.add_argument("--vendor", nargs="?", help=_("Sets the vendor"))
                write.add_argument("--description", nargs="?", help=_("Sets a description"))
                write.add_argument("--license", nargs="?", help=_("Sets the license"))
+               write.add_argument("--version", type=int, help=_("Database Format Version"))
 
                # Update WHOIS
                update_whois = subparsers.add_parser("update-whois", help=_("Update WHOIS Information"))
@@ -91,9 +94,11 @@ class CLI(object):
 
                args = parser.parse_args()
 
-               # Enable debug logging
+               # Configure logging
                if args.debug:
-                       log.setLevel(logging.DEBUG)
+                       location.logger.set_level(logging.DEBUG)
+               elif args.quiet:
+                       location.logger.set_level(logging.WARNING)
 
                # Print usage if no action was given
                if not "func" in args:
@@ -151,6 +156,7 @@ class CLI(object):
                                CREATE TABLE IF NOT EXISTS autnum_overrides(
                                        number bigint NOT NULL,
                                        name text,
+                                       country text,
                                        is_anonymous_proxy boolean DEFAULT FALSE,
                                        is_satellite_provider boolean DEFAULT FALSE,
                                        is_anycast boolean DEFAULT FALSE
@@ -175,8 +181,6 @@ class CLI(object):
                """
                        Compiles a database in libloc format out of what is in the database
                """
-               print(ns)
-
                # Allocate a writer
                writer = location.Writer(ns.signing_key)
 
@@ -216,15 +220,28 @@ class CLI(object):
                # Select all known networks
                rows = self.db.query("""
                        SELECT
+                               DISTINCT ON (announcements.network)
                                announcements.network AS network,
                                announcements.autnum AS autnum,
-                               (
-                                       SELECT networks.country FROM networks
-                                               WHERE announcements.network <<= networks.network
-                                               ORDER BY masklen(networks.network) DESC
-                                               LIMIT 1
+
+                               -- Country
+                               COALESCE(
+                                       (
+                                               SELECT country FROM network_overrides overrides
+                                                       WHERE announcements.network <<= overrides.network
+                                                       ORDER BY masklen(overrides.network) DESC
+                                                       LIMIT 1
+                                       ),
+                                       (
+                                               SELECT country FROM autnum_overrides overrides
+                                                       WHERE announcements.autnum = overrides.number
+                                       ),
+                                       networks.country
                                ) AS country,
 
+                               -- Must be part of returned values for ORDER BY clause
+                               masklen(networks.network) AS sort,
+
                                -- Flags
                                COALESCE(
                                        (
@@ -263,6 +280,8 @@ class CLI(object):
                                        )
                                ) AS is_anycast
                        FROM announcements
+                               LEFT JOIN networks ON announcements.network <<= networks.network
+                       ORDER BY announcements.network, sort DESC
                """)
 
                for row in rows:
@@ -422,6 +441,7 @@ class CLI(object):
                        prefix = int(prefix)
                except:
                        log.warning("Invalid prefix: %s" % prefix)
+                       return
 
                # Fix prefix length for IPv4
                if type == "ipv4":
@@ -444,7 +464,6 @@ class CLI(object):
                server = ns.server[0]
 
                # Pre-compile regular expression for routes
-               #route = re.compile(b"^\*>?\s[\si]?([^\s]+)[.\s]*?(\d+)\si$", re.MULTILINE)
                route = re.compile(b"^\*[\s\>]i([^\s]+).+?(\d+)\si\r\n", re.MULTILINE|re.DOTALL)
 
                with telnetlib.Telnet(server) as t:
@@ -453,8 +472,10 @@ class CLI(object):
                        #       t.set_debuglevel(10)
 
                        # Wait for console greeting
-                       greeting = t.read_until(b"> ")
-                       log.debug(greeting.decode())
+                       greeting = t.read_until(b"> ", timeout=30)
+                       if not greeting:
+                               log.error("Could not get a console prompt")
+                               return 1
 
                        # Disable pagination
                        t.write(b"terminal length 0\n")
@@ -516,10 +537,13 @@ class CLI(object):
                                        -- Delete anything that is not global unicast address space
                                        DELETE FROM announcements WHERE family(network) = 6 AND NOT network <<= '2000::/3';
 
+                                       -- DELETE "current network" address space
+                                       DELETE FROM announcements WHERE family(network) = 4 AND network <<= '0.0.0.0/8';
+
                                        -- DELETE local loopback address space
                                        DELETE FROM announcements WHERE family(network) = 4 AND network <<= '127.0.0.0/8';
 
-                                       -- DELETE RFC1918 address space
+                                       -- DELETE RFC 1918 address space
                                        DELETE FROM announcements WHERE family(network) = 4 AND network <<= '10.0.0.0/8';
                                        DELETE FROM announcements WHERE family(network) = 4 AND network <<= '172.16.0.0/12';
                                        DELETE FROM announcements WHERE family(network) = 4 AND network <<= '192.168.0.0/16';
@@ -540,6 +564,10 @@ class CLI(object):
                                        -- DELETE IPv6 to IPv4 (6to4) address space
                                        DELETE FROM announcements WHERE family(network) = 4 AND network <<= '192.88.99.0/24';
 
+                                       -- DELETE multicast and reserved address space
+                                       DELETE FROM announcements WHERE family(network) = 4 AND network <<= '224.0.0.0/4';
+                                       DELETE FROM announcements WHERE family(network) = 4 AND network <<= '240.0.0.0/4';
+
                                        -- Delete networks that are too small to be in the global routing table
                                        DELETE FROM announcements WHERE family(network) = 6 AND masklen(network) > 48;
                                        DELETE FROM announcements WHERE family(network) = 4 AND masklen(network) > 24;
@@ -579,6 +607,11 @@ class CLI(object):
                                                                log.warning("Invalid IP network: %s: %s" % (network, e))
                                                                continue
 
+                                                       # Prevent that we overwrite all networks
+                                                       if network.prefixlen == 0:
+                                                               log.warning("Skipping %s: You cannot overwrite default" % network)
+                                                               continue
+
                                                        self.db.execute("""
                                                                INSERT INTO network_overrides(
                                                                        network,
@@ -586,7 +619,7 @@ class CLI(object):
                                                                        is_anonymous_proxy,
                                                                        is_satellite_provider,
                                                                        is_anycast
-                                                               ) VALUES (%s, %s, %s, %s)
+                                                               ) VALUES (%s, %s, %s, %s, %s)
                                                                ON CONFLICT (network) DO NOTHING""",
                                                                "%s" % network,
                                                                block.get("country"),
@@ -595,8 +628,8 @@ class CLI(object):
                                                                block.get("is-anycast") == "yes",
                                                        )
 
-                                               elif type == "autnum":
-                                                       autnum = block.get("autnum")
+                                               elif type == "aut-num":
+                                                       autnum = block.get("aut-num")
 
                                                        # Check if AS number begins with "AS"
                                                        if not autnum.startswith("AS"):
@@ -610,12 +643,15 @@ class CLI(object):
                                                                INSERT INTO autnum_overrides(
                                                                        number,
                                                                        name,
+                                                                       country,
                                                                        is_anonymous_proxy,
                                                                        is_satellite_provider,
                                                                        is_anycast
-                                                               ) VALUES(%s, %s, %s, %s, %s)
+                                                               ) VALUES(%s, %s, %s, %s, %s, %s)
                                                                ON CONFLICT DO NOTHING""",
-                                                               autnum, block.get("name"),
+                                                               autnum,
+                                                               block.get("name"),
+                                                               block.get("country"),
                                                                block.get("is-anonymous-proxy") == "yes",
                                                                block.get("is-satellite-provider") == "yes",
                                                                block.get("is-anycast") == "yes",