]>
Commit | Line | Data |
---|---|---|
5118a4b8 MT |
1 | #!/usr/bin/python3 |
2 | ############################################################################### | |
3 | # # | |
4 | # libloc - A library to determine the location of someone on the Internet # | |
5 | # # | |
6 | # Copyright (C) 2017 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 | |
2bb7d64e | 21 | import gettext |
4439e317 MT |
22 | import ipaddress |
23 | import os | |
24 | import socket | |
5118a4b8 MT |
25 | import sys |
26 | import syslog | |
27 | ||
28 | # Load our location module | |
29 | import location | |
30 | ||
31 | # i18n | |
2bb7d64e MT |
32 | def _(singular, plural=None, n=None): |
33 | if plural: | |
34 | return gettext.dngettext("libloc", singular, plural, n) | |
35 | ||
36 | return gettext.dgettext("libloc", singular) | |
5118a4b8 | 37 | |
4439e317 MT |
38 | # Output formatters |
39 | ||
40 | class OutputFormatter(object): | |
71e0ad0b MT |
41 | def __init__(self, ns): |
42 | self.ns = ns | |
43 | ||
4439e317 MT |
44 | def __enter__(self): |
45 | # Open the output | |
46 | self.open() | |
47 | ||
48 | return self | |
49 | ||
50 | def __exit__(self, type, value, tb): | |
51 | if tb is None: | |
52 | self.close() | |
53 | ||
71e0ad0b MT |
54 | @property |
55 | def name(self): | |
56 | if "country_code" in self.ns: | |
57 | return "networks_country_%s" % self.ns.country_code[0] | |
58 | ||
59 | elif "asn" in self.ns: | |
60 | return "networks_AS%s" % self.ns.asn[0] | |
61 | ||
4439e317 MT |
62 | def open(self): |
63 | pass | |
64 | ||
65 | def close(self): | |
66 | pass | |
67 | ||
68 | def network(self, network): | |
69 | print(network) | |
70 | ||
71 | ||
6da14cc1 MT |
72 | class IpsetOutputFormatter(OutputFormatter): |
73 | """ | |
74 | For nftables | |
75 | """ | |
76 | def open(self): | |
77 | print("create %s hash:net family inet hashsize 1024 maxelem 65536" % self.name) | |
78 | ||
79 | def network(self, network): | |
80 | print("add %s %s" % (self.name, network)) | |
81 | ||
82 | ||
71e0ad0b MT |
83 | class NftablesOutputFormatter(OutputFormatter): |
84 | """ | |
85 | For nftables | |
86 | """ | |
87 | def open(self): | |
88 | print("define %s = {" % self.name) | |
89 | ||
90 | def close(self): | |
91 | print("}") | |
92 | ||
93 | def network(self, network): | |
94 | print(" %s," % network) | |
95 | ||
96 | ||
4439e317 MT |
97 | class XTGeoIPOutputFormatter(OutputFormatter): |
98 | """ | |
99 | Formats the output in that way, that it can be loaded by | |
100 | the xt_geoip kernel module from xtables-addons. | |
101 | """ | |
102 | def network(self, network): | |
103 | n = ipaddress.ip_network("%s" % network) | |
104 | ||
105 | for address in (n.network_address, n.broadcast_address): | |
106 | bytes = socket.inet_pton( | |
107 | socket.AF_INET6 if address.version == 6 else socket.AF_INET, | |
108 | "%s" % address, | |
109 | ) | |
110 | ||
111 | os.write(1, bytes) | |
112 | ||
113 | ||
5118a4b8 | 114 | class CLI(object): |
4439e317 | 115 | output_formats = { |
6da14cc1 | 116 | "ipset" : IpsetOutputFormatter, |
4439e317 | 117 | "list" : OutputFormatter, |
71e0ad0b | 118 | "nftables" : NftablesOutputFormatter, |
4439e317 MT |
119 | "xt_geoip" : XTGeoIPOutputFormatter, |
120 | } | |
121 | ||
5118a4b8 MT |
122 | def parse_cli(self): |
123 | parser = argparse.ArgumentParser( | |
124 | description=_("Location Database Command Line Interface"), | |
125 | ) | |
126 | subparsers = parser.add_subparsers() | |
127 | ||
128 | # Global configuration flags | |
129 | parser.add_argument("--debug", action="store_true", | |
130 | help=_("Enable debug output")) | |
131 | ||
ddb184be MT |
132 | # version |
133 | parser.add_argument("--version", action="version", | |
134 | version="%%(prog)s %s" % location.__version__) | |
135 | ||
2538ed9a MT |
136 | # database |
137 | parser.add_argument("--database", "-d", | |
138 | default="@databasedir@/database.db", help=_("Path to database"), | |
139 | ) | |
140 | ||
5118a4b8 MT |
141 | # lookup an IP address |
142 | lookup = subparsers.add_parser("lookup", | |
143 | help=_("Lookup one or multiple IP addresses"), | |
144 | ) | |
145 | lookup.add_argument("address", nargs="+") | |
146 | lookup.set_defaults(func=self.handle_lookup) | |
147 | ||
fadc1af0 MT |
148 | # Get AS |
149 | get_as = subparsers.add_parser("get-as", | |
150 | help=_("Get information about one or multiple Autonomous Systems"), | |
151 | ) | |
152 | get_as.add_argument("asn", nargs="+") | |
153 | get_as.set_defaults(func=self.handle_get_as) | |
154 | ||
da3e360e MT |
155 | # Search for AS |
156 | search_as = subparsers.add_parser("search-as", | |
157 | help=_("Search for Autonomous Systems that match the string"), | |
158 | ) | |
159 | search_as.add_argument("query", nargs=1) | |
160 | search_as.set_defaults(func=self.handle_search_as) | |
161 | ||
43154ed7 MT |
162 | # List all networks in an AS |
163 | list_networks_by_as = subparsers.add_parser("list-networks-by-as", | |
164 | help=_("Lists all networks in an AS"), | |
165 | ) | |
166 | list_networks_by_as.add_argument("asn", nargs=1, type=int) | |
4439e317 MT |
167 | list_networks_by_as.add_argument("--output-format", |
168 | choices=self.output_formats.keys(), default="list") | |
43154ed7 MT |
169 | list_networks_by_as.set_defaults(func=self.handle_list_networks_by_as) |
170 | ||
ccc7ab4e | 171 | # List all networks in a country |
b5cdfad7 | 172 | list_networks_by_cc = subparsers.add_parser("list-networks-by-cc", |
ccc7ab4e MT |
173 | help=_("Lists all networks in a country"), |
174 | ) | |
b5cdfad7 | 175 | list_networks_by_cc.add_argument("country_code", nargs=1) |
4439e317 MT |
176 | list_networks_by_cc.add_argument("--output-format", |
177 | choices=self.output_formats.keys(), default="list") | |
b5cdfad7 | 178 | list_networks_by_cc.set_defaults(func=self.handle_list_networks_by_cc) |
ccc7ab4e | 179 | |
bbdb2e0a MT |
180 | # List all networks with flags |
181 | list_networks_by_flags = subparsers.add_parser("list-networks-by-flags", | |
182 | help=_("Lists all networks with flags"), | |
183 | ) | |
184 | list_networks_by_flags.add_argument("--anonymous-proxy", | |
185 | action="store_true", help=_("Anonymous Proxies"), | |
186 | ) | |
187 | list_networks_by_flags.add_argument("--satellite-provider", | |
188 | action="store_true", help=_("Satellite Providers"), | |
189 | ) | |
190 | list_networks_by_flags.add_argument("--anycast", | |
191 | action="store_true", help=_("Anycasts"), | |
192 | ) | |
193 | list_networks_by_flags.add_argument("--output-format", | |
194 | choices=self.output_formats.keys(), default="list") | |
195 | list_networks_by_flags.set_defaults(func=self.handle_list_networks_by_flags) | |
196 | ||
78f37815 MT |
197 | args = parser.parse_args() |
198 | ||
199 | # Print usage if no action was given | |
200 | if not "func" in args: | |
201 | parser.print_usage() | |
202 | sys.exit(2) | |
203 | ||
204 | return args | |
5118a4b8 MT |
205 | |
206 | def run(self): | |
207 | # Parse command line arguments | |
208 | args = self.parse_cli() | |
209 | ||
2538ed9a MT |
210 | # Open database |
211 | try: | |
212 | db = location.Database(args.database) | |
213 | except FileNotFoundError as e: | |
214 | sys.stderr.write("location-query: Could not open database %s: %s\n" \ | |
215 | % (args.database, e)) | |
216 | sys.exit(1) | |
217 | ||
b1720435 MT |
218 | # Verify the database |
219 | if not db.verify(): | |
220 | sys.stderr.write("location-query: Could not verify the database\n") | |
221 | sys.exit(1) | |
222 | ||
5118a4b8 | 223 | # Call function |
2538ed9a | 224 | ret = args.func(db, args) |
5118a4b8 MT |
225 | |
226 | # Return with exit code | |
227 | if ret: | |
228 | sys.exit(ret) | |
229 | ||
230 | # Otherwise just exit | |
231 | sys.exit(0) | |
232 | ||
2538ed9a | 233 | def handle_lookup(self, db, ns): |
5118a4b8 MT |
234 | ret = 0 |
235 | ||
236 | for address in ns.address: | |
237 | try: | |
2538ed9a | 238 | n = db.lookup(address) |
5118a4b8 | 239 | except ValueError: |
9f2f5d13 | 240 | print(_("Invalid IP address: %s") % address, file=sys.stderr) |
5118a4b8 MT |
241 | |
242 | args = { | |
243 | "address" : address, | |
244 | "network" : n, | |
245 | } | |
246 | ||
247 | # Nothing found? | |
248 | if not n: | |
9f2f5d13 | 249 | print(_("Nothing found for %(address)s") % args, file=sys.stderr) |
5118a4b8 MT |
250 | ret = 1 |
251 | continue | |
252 | ||
253 | # Try to retrieve the AS if we have an AS number | |
254 | if n.asn: | |
2538ed9a | 255 | a = db.get_as(n.asn) |
5118a4b8 MT |
256 | |
257 | # If we have found an AS we will print it in the message | |
258 | if a: | |
259 | args.update({ | |
260 | "as" : a, | |
261 | }) | |
262 | ||
263 | print(_("%(address)s belongs to %(network)s which is a part of %(as)s") % args) | |
264 | continue | |
265 | ||
266 | print(_("%(address)s belongs to %(network)s") % args) | |
267 | ||
268 | return ret | |
269 | ||
2538ed9a | 270 | def handle_get_as(self, db, ns): |
fadc1af0 MT |
271 | """ |
272 | Gets information about Autonomous Systems | |
273 | """ | |
274 | ret = 0 | |
275 | ||
276 | for asn in ns.asn: | |
277 | try: | |
278 | asn = int(asn) | |
279 | except ValueError: | |
9f2f5d13 | 280 | print(_("Invalid ASN: %s") % asn, file=sys.stderr) |
fadc1af0 MT |
281 | ret = 1 |
282 | continue | |
283 | ||
284 | # Fetch AS from database | |
2538ed9a | 285 | a = db.get_as(asn) |
fadc1af0 MT |
286 | |
287 | # Nothing found | |
288 | if not a: | |
9f2f5d13 | 289 | print(_("Could not find AS%s") % asn, file=sys.stderr) |
fadc1af0 MT |
290 | ret = 1 |
291 | continue | |
292 | ||
293 | print(_("AS%(asn)s belongs to %(name)s") % { "asn" : a.number, "name" : a.name }) | |
294 | ||
295 | return ret | |
5118a4b8 | 296 | |
2538ed9a | 297 | def handle_search_as(self, db, ns): |
da3e360e MT |
298 | for query in ns.query: |
299 | # Print all matches ASes | |
2538ed9a | 300 | for a in db.search_as(query): |
da3e360e MT |
301 | print(a) |
302 | ||
4439e317 MT |
303 | def __get_output_formatter(self, ns): |
304 | try: | |
305 | cls = self.output_formats[ns.output_format] | |
306 | except KeyError: | |
307 | cls = OutputFormatter | |
308 | ||
71e0ad0b | 309 | return cls(ns) |
4439e317 | 310 | |
43154ed7 | 311 | def handle_list_networks_by_as(self, db, ns): |
4439e317 MT |
312 | with self.__get_output_formatter(ns) as f: |
313 | for asn in ns.asn: | |
314 | # Print all matching networks | |
315 | for n in db.search_networks(asn=asn): | |
316 | f.network(n) | |
43154ed7 | 317 | |
ccc7ab4e | 318 | def handle_list_networks_by_cc(self, db, ns): |
4439e317 MT |
319 | with self.__get_output_formatter(ns) as f: |
320 | for country_code in ns.country_code: | |
321 | # Print all matching networks | |
322 | for n in db.search_networks(country_code=country_code): | |
323 | f.network(n) | |
324 | ||
bbdb2e0a MT |
325 | def handle_list_networks_by_flags(self, db, ns): |
326 | flags = 0 | |
327 | ||
328 | if ns.anonymous_proxy: | |
329 | flags |= location.NETWORK_FLAG_ANONYMOUS_PROXY | |
330 | ||
331 | if ns.satellite_provider: | |
332 | flags |= location.NETWORK_FLAG_SATELLITE_PROVIDER | |
333 | ||
334 | if ns.anycast: | |
335 | flags |= location.NETWORK_FLAG_ANYCAST | |
336 | ||
337 | with self.__get_output_formatter(ns) as f: | |
338 | for n in db.search_networks(flags=flags): | |
339 | f.network(n) | |
340 | ||
ccc7ab4e | 341 | |
5118a4b8 MT |
342 | def main(): |
343 | # Run the command line interface | |
344 | c = CLI() | |
345 | c.run() | |
346 | ||
347 | main() |