Debian Codegraph @

ipseckey

raw
1 #!/usr/bin/python3
2 # Show IPSECKEY records generated by libreswan IPsec
3 # might work on some versions of older openswan as well
4 #
5 # Copyright 2015 Paul Wouters <[email protected]>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 VERSION = "3.5"
22
23 import os
24 import sys
25 import subprocess
26 import optparse
27 import base64
28 import time
29 import hashlib
30
31 ipsec = "/usr/sbin/ipsec"
32 if not os.path.isfile(ipsec):
33 ipsec = "/usr/local/sbin/ipsec"
34 if not os.path.isfile(ipsec):
35 print("ipseckey: ipsec command not found (not a libreswan or openswan IPsec servers?)", file=sys.stderr)
36 sys.exit(1)
37
38 def show_version():
39 print("sshfp version: " + VERSION, file=sys.stderr)
40
41 def main():
42
43 parser = optparse.OptionParser()
44 parser.add_option("-v", "--version",
45 action="store_true",
46 dest="version",
47 help="print version information and exit")
48 (options, args) = parser.parse_args()
49
50 if options.version:
51 show_version()
52 sys.exit(0)
53
54 if os.geteuid() != 0:
55 print("ipseckey: root access is needed to read the IPsec NSS database for the public key", file=sys.stderr)
56 sys.exit(2)
57
58 cmd = [ipsec, "showhostkey", "--ipseckey"]
59 process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
60 (stdout, stderr) = process.communicate()
61 print(stdout)
62
63 if __name__ == "__main__":
64 main()