summaryrefslogtreecommitdiffstats
path: root/utils-python/seppl-ls
blob: 0b0decba9c5e31e3f0286f9724cd1254a93c0042 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/python

# $Id$
#
# This file is part of seppl.
#
# seppl is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# seppl is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with seppl; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

import getopt, sys

from seppl_common import *
import xml.sax, xml.sax.handler

name = None
algorithm = None
bits = 0
reverse = 0
PROC_FILE_NAME = "/proc/net/seppl_keyring"
file = None

class KeyContentHandler(xml.sax.handler.ContentHandler):

	last = None

	def startElement(self, name, attrs):
		self.last = name
		
		if name != "key":
			return
		
		self.name = ""
		self.algorithm = ""
		self.bits = ""
		self.data = ""

	def endElement(self, n):
		global name, algorithm, bits
		self.last = None
		
		if n != "key":
			return

		if self.name == "":
			self.name = "def"

		if self.algorithm == "":
			self.algorithm = "aes"

		if self.bits == "":
			self.bits = 128
		else:
			self.bits = int(self.bits)
		
		a = find_alg_by_name(self.algorithm, self.bits)
		if a == -1:
			raise xml.sax.SAXNotRecognizedException("Cipher not known")

		key = parse_key(self.data)
		if key is None or len(key) != self.bits/8:
			raise xml.sax.SAXNotRecognizedException("Could not parse key data.")

		if (name is None or name == self.name) and (algorithm is None or algorithm == self.algorithm) and (bits == 0 or bits == self.bits):
			dump_key_bin(a, self.name, key)

	def characters(self, content):
		content = content.strip()

		if len(content) == 0:
			return
		
		if self.last == "name":
			self.name += content
			return
		if self.last == "algorithm":
			self.algorithm += content
			return
		if self.last == "bits":
			self.bits += content
			return
		if self.last == "data":
			self.data += content
			return

		raise xml.sax.SAXNotRecognizedException("Malformed XML structure |%s|%s|" % (self.last, content))

def usage():
	global name, algorithm, bits, reverse, file
	
	print "%s:" % sys.argv[0]
	print "   -h --help                   Show this help"
	print "   -r --reverse                Convert XML to binary, instead of the other way round (%s)" % { 0 : "disabled", 1 : "enabled" }[reverse]
	print "   -a --algorithm ALGORITHM    Show only keys with  algorithm (%s)" % algorithm
	print "   -b --bits BITS              Show only keys with bit length (%s)" % bits
	print "   -n --name NAME              Show only keys with name (%s)" % name
	print "   -f --file FILE              Specify file (- for STDIN) (%s)" % file
	pass

def main():
	global name, algorithm, bits, reverse, file
	
	try:	
		opts, args = getopt.getopt(sys.argv[1:], "hn:a:b:rf:", ["help", "name=", "algorithm=", "bits=", "reverse", "file"])
	except getopt.GetoptError:
		usage()
		sys.exit(2)

	u = 0
	
	for o, a in opts:
		if o in ("-h", "--help"):
			u = 1
			
		if o in ("-n", "--name"):
			name = a[:7]

		if o in ("-a", "--algorithm"):
			algorithm = a

		if o in ("-b", "--bits"):
			bits = int(a)

		if o in ("-r", "--reverse"):
			reverse = 1

		if o in ("-f", "--file"):
			file = a

	if file is None:
		if reverse:
			file = "-"
		else:
			file = PROC_FILE_NAME

	if u:
		usage()
		sys.exit(0)


	if file == "-":
		buf = sys.stdin.read();
	else:
		try:
			fd = open(file, "r+")
			buf = fd.read();
			fd.close()
		except IOError, e:
			sys.stderr.write("Could not open proc-file (%s).\n" % str(e))
			sys.exit(2)


	if len(buf) == 0:
		sys.exit(0)

	if buf[0] == '<':
		reverse = 1

	if not reverse:

		print "<seppl-keyring>"
		while len(buf) >= 8:
			a, _name = unpack("B7s", buf[:8])
			n, b = find_alg_by_number(a)
			
			if (b == 0):
				sys.stderr.write("ERROR: Unknown cipher. Please update.\n")
				break

			_name = _name.replace("\000", "")

			if (name is None or name == _name) and (algorithm is None or algorithm == n) and (bits == 0 or b == bits):
				dump_key_xml(a, _name, buf[8:b/8+8])

			buf = buf[8+b/8:]
		print "</seppl-keyring>"
	else:
		try:
			xml.sax.parseString(buf, KeyContentHandler())
		except xml.sax.SAXException, e:
			sys.stderr.write("Parse error (%s)\n" %str(e))

	
if __name__ == "__main__":
	main()