]> git.ipfire.org Git - location/libloc.git/blame - src/python/location-query.in
database: Fix use-after-free error after last iteration of search
[location/libloc.git] / src / python / location-query.in
CommitLineData
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
20import argparse
2bb7d64e 21import gettext
5118a4b8
MT
22import sys
23import syslog
24
25# Load our location module
26import location
27
28# i18n
2bb7d64e
MT
29def _(singular, plural=None, n=None):
30 if plural:
31 return gettext.dngettext("libloc", singular, plural, n)
32
33 return gettext.dgettext("libloc", singular)
5118a4b8
MT
34
35class CLI(object):
5118a4b8
MT
36 def parse_cli(self):
37 parser = argparse.ArgumentParser(
38 description=_("Location Database Command Line Interface"),
39 )
40 subparsers = parser.add_subparsers()
41
42 # Global configuration flags
43 parser.add_argument("--debug", action="store_true",
44 help=_("Enable debug output"))
45
ddb184be
MT
46 # version
47 parser.add_argument("--version", action="version",
48 version="%%(prog)s %s" % location.__version__)
49
2538ed9a
MT
50 # database
51 parser.add_argument("--database", "-d",
52 default="@databasedir@/database.db", help=_("Path to database"),
53 )
54
5118a4b8
MT
55 # lookup an IP address
56 lookup = subparsers.add_parser("lookup",
57 help=_("Lookup one or multiple IP addresses"),
58 )
59 lookup.add_argument("address", nargs="+")
60 lookup.set_defaults(func=self.handle_lookup)
61
fadc1af0
MT
62 # Get AS
63 get_as = subparsers.add_parser("get-as",
64 help=_("Get information about one or multiple Autonomous Systems"),
65 )
66 get_as.add_argument("asn", nargs="+")
67 get_as.set_defaults(func=self.handle_get_as)
68
da3e360e
MT
69 # Search for AS
70 search_as = subparsers.add_parser("search-as",
71 help=_("Search for Autonomous Systems that match the string"),
72 )
73 search_as.add_argument("query", nargs=1)
74 search_as.set_defaults(func=self.handle_search_as)
75
ccc7ab4e
MT
76 # List all networks in a country
77 search_as = subparsers.add_parser("list-networks-by-cc",
78 help=_("Lists all networks in a country"),
79 )
80 search_as.add_argument("country_code", nargs=1)
81 search_as.set_defaults(func=self.handle_list_networks_by_cc)
82
78f37815
MT
83 args = parser.parse_args()
84
85 # Print usage if no action was given
86 if not "func" in args:
87 parser.print_usage()
88 sys.exit(2)
89
90 return args
5118a4b8
MT
91
92 def run(self):
93 # Parse command line arguments
94 args = self.parse_cli()
95
2538ed9a
MT
96 # Open database
97 try:
98 db = location.Database(args.database)
99 except FileNotFoundError as e:
100 sys.stderr.write("location-query: Could not open database %s: %s\n" \
101 % (args.database, e))
102 sys.exit(1)
103
5118a4b8 104 # Call function
2538ed9a 105 ret = args.func(db, args)
5118a4b8
MT
106
107 # Return with exit code
108 if ret:
109 sys.exit(ret)
110
111 # Otherwise just exit
112 sys.exit(0)
113
2538ed9a 114 def handle_lookup(self, db, ns):
5118a4b8
MT
115 ret = 0
116
117 for address in ns.address:
118 try:
2538ed9a 119 n = db.lookup(address)
5118a4b8 120 except ValueError:
9f2f5d13 121 print(_("Invalid IP address: %s") % address, file=sys.stderr)
5118a4b8
MT
122
123 args = {
124 "address" : address,
125 "network" : n,
126 }
127
128 # Nothing found?
129 if not n:
9f2f5d13 130 print(_("Nothing found for %(address)s") % args, file=sys.stderr)
5118a4b8
MT
131 ret = 1
132 continue
133
134 # Try to retrieve the AS if we have an AS number
135 if n.asn:
2538ed9a 136 a = db.get_as(n.asn)
5118a4b8
MT
137
138 # If we have found an AS we will print it in the message
139 if a:
140 args.update({
141 "as" : a,
142 })
143
144 print(_("%(address)s belongs to %(network)s which is a part of %(as)s") % args)
145 continue
146
147 print(_("%(address)s belongs to %(network)s") % args)
148
149 return ret
150
2538ed9a 151 def handle_get_as(self, db, ns):
fadc1af0
MT
152 """
153 Gets information about Autonomous Systems
154 """
155 ret = 0
156
157 for asn in ns.asn:
158 try:
159 asn = int(asn)
160 except ValueError:
9f2f5d13 161 print(_("Invalid ASN: %s") % asn, file=sys.stderr)
fadc1af0
MT
162 ret = 1
163 continue
164
165 # Fetch AS from database
2538ed9a 166 a = db.get_as(asn)
fadc1af0
MT
167
168 # Nothing found
169 if not a:
9f2f5d13 170 print(_("Could not find AS%s") % asn, file=sys.stderr)
fadc1af0
MT
171 ret = 1
172 continue
173
174 print(_("AS%(asn)s belongs to %(name)s") % { "asn" : a.number, "name" : a.name })
175
176 return ret
5118a4b8 177
2538ed9a 178 def handle_search_as(self, db, ns):
da3e360e
MT
179 for query in ns.query:
180 # Print all matches ASes
2538ed9a 181 for a in db.search_as(query):
da3e360e
MT
182 print(a)
183
ccc7ab4e
MT
184 def handle_list_networks_by_cc(self, db, ns):
185 for country_code in ns.country_code:
186 # Print all matching networks
187 for n in db.search_networks(country_code=country_code):
188 print(n)
189
5118a4b8
MT
190def main():
191 # Run the command line interface
192 c = CLI()
193 c.run()
194
195main()