comparison sat_frontends/jp/cmd_encryption.py @ 2740:8fd8ce5a5855

jp (message/send, encryption): encryption handling: - encryption algorithm can now be requested when sending a message (using --encrypt option) - new encryption commands to (de)activate encryption session, check available algorithms, or manage trust.
author Goffi <goffi@goffi.org>
date Wed, 02 Jan 2019 18:50:57 +0100
parents
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2739:e8dc00f612fb 2740:8fd8ce5a5855
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # jp: a SAT command line tool
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 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 Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat_frontends.jp import base
21 from sat_frontends.jp.constants import Const as C
22 from sat.core.i18n import _
23 from functools import partial
24 from sat.tools.common import data_format
25 from sat_frontends.jp import xmlui_manager
26
27 __commands__ = ["Encryption"]
28
29
30 class EncryptionAlgorithms(base.CommandBase):
31
32 def __init__(self, host):
33 extra_outputs = {"default": self.default_output}
34 super(EncryptionAlgorithms, self).__init__(
35 host, "algorithms",
36 use_output=C.OUTPUT_LIST_DICT,
37 extra_outputs=extra_outputs,
38 use_profile=False,
39 help=_("show available encryption algorithms"))
40 self.need_loop = True
41
42 def add_parser_options(self):
43 pass
44
45 def encryptionPluginsGetCb(self, plugins):
46 self.output(plugins)
47 self.host.quit()
48
49 def default_output(self, plugins):
50 if not plugins:
51 self.disp(_(u"No encryption plugin registered!"))
52 self.host.quit(C.EXIT_NOT_FOUND)
53 else:
54 self.disp(_(u"Following encryption algorithms are available: {algos}").format(
55 algos=', '.join([p['name'] for p in plugins])))
56 self.host.quit()
57
58 def start(self):
59 self.host.bridge.encryptionPluginsGet(
60 callback=self.encryptionPluginsGetCb,
61 errback=partial(
62 self.errback,
63 msg=_(u"can't retrieve plugins: {}"),
64 exit_code=C.EXIT_BRIDGE_ERRBACK,
65 ),
66 )
67
68
69 class EncryptionGet(base.CommandBase):
70
71 def __init__(self, host):
72 super(EncryptionGet, self).__init__(
73 host, "get",
74 use_output=C.OUTPUT_DICT,
75 help=_(u"get encryption session data"))
76 self.need_loop = True
77
78 def add_parser_options(self):
79 self.parser.add_argument(
80 "jid", type=base.unicode_decoder,
81 help=_(u"jid of the entity to check")
82 )
83
84 def messageEncryptionGetCb(self, serialised):
85 session_data = data_format.deserialise(serialised)
86 if session_data is None:
87 self.disp(
88 u"No encryption session found, the messages are sent in plain text.")
89 self.host.quit(C.EXIT_NOT_FOUND)
90 self.output(session_data)
91 self.host.quit()
92
93 def start(self):
94 jids = self.host.check_jids([self.args.jid])
95 jid = jids[0]
96 self.host.bridge.messageEncryptionGet(
97 jid, self.profile,
98 callback=self.messageEncryptionGetCb,
99 errback=partial(
100 self.errback,
101 msg=_(u"can't get session: {}"),
102 exit_code=C.EXIT_BRIDGE_ERRBACK,
103 ),
104 )
105
106
107 class EncryptionStart(base.CommandBase):
108
109 def __init__(self, host):
110 super(EncryptionStart, self).__init__(
111 host, "start",
112 help=_(u"start encrypted session with an entity"))
113 self.need_loop = True
114
115 def add_parser_options(self):
116 self.parser.add_argument(
117 "--encrypt-noreplace",
118 action="store_true",
119 help=_(u"don't replace encryption algorithm if an other one is already used"))
120 algorithm = self.parser.add_mutually_exclusive_group()
121 algorithm.add_argument(
122 "-n", "--name", help=_(u"algorithm name (DEFAULT: choose automatically)"))
123 algorithm.add_argument(
124 "-N", "--namespace",
125 help=_(u"algorithm namespace (DEFAULT: choose automatically)"))
126 self.parser.add_argument(
127 "jid", type=base.unicode_decoder,
128 help=_(u"jid of the entity to stop encrypted session with")
129 )
130
131 def encryptionNamespaceGetCb(self, namespace):
132 jids = self.host.check_jids([self.args.jid])
133 jid = jids[0]
134 self.host.bridge.messageEncryptionStart(
135 jid, namespace, not self.args.encrypt_noreplace,
136 self.profile,
137 callback=self.host.quit,
138 errback=partial(self.errback,
139 msg=_(u"Can't start encryption session: {}"),
140 exit_code=C.EXIT_BRIDGE_ERRBACK,
141 ))
142
143 def start(self):
144 if self.args.name is not None:
145 self.host.bridge.encryptionNamespaceGet(self.args.name,
146 callback=self.encryptionNamespaceGetCb,
147 errback=partial(self.errback,
148 msg=_(u"Can't get encryption namespace: {}"),
149 exit_code=C.EXIT_BRIDGE_ERRBACK,
150 ))
151 elif self.args.namespace is not None:
152 self.encryptionNamespaceGetCb(self.args.namespace)
153 else:
154 self.encryptionNamespaceGetCb(u"")
155
156
157 class EncryptionStop(base.CommandBase):
158
159 def __init__(self, host):
160 super(EncryptionStop, self).__init__(
161 host, "stop",
162 help=_(u"stop encrypted session with an entity"))
163 self.need_loop = True
164
165 def add_parser_options(self):
166 self.parser.add_argument(
167 "jid", type=base.unicode_decoder,
168 help=_(u"jid of the entity to stop encrypted session with")
169 )
170
171 def start(self):
172 jids = self.host.check_jids([self.args.jid])
173 jid = jids[0]
174 self.host.bridge.messageEncryptionStop(
175 jid, self.profile,
176 callback=self.host.quit,
177 errback=partial(
178 self.errback,
179 msg=_(u"can't end encrypted session: {}"),
180 exit_code=C.EXIT_BRIDGE_ERRBACK,
181 ),
182 )
183
184
185 class TrustUI(base.CommandBase):
186
187 def __init__(self, host):
188 super(TrustUI, self).__init__(
189 host, "ui",
190 help=_(u"get UI to manage trust"))
191 self.need_loop = True
192
193 def add_parser_options(self):
194 self.parser.add_argument(
195 "jid", type=base.unicode_decoder,
196 help=_(u"jid of the entity to stop encrypted session with")
197 )
198 algorithm = self.parser.add_mutually_exclusive_group()
199 algorithm.add_argument(
200 "-n", "--name", help=_(u"algorithm name (DEFAULT: current algorithm)"))
201 algorithm.add_argument(
202 "-N", "--namespace",
203 help=_(u"algorithm namespace (DEFAULT: current algorithm)"))
204
205 def encryptionTrustUIGetCb(self, xmlui_raw):
206 xmlui = xmlui_manager.create(self.host, xmlui_raw)
207 xmlui.show()
208 xmlui.submitForm()
209
210 def encryptionNamespaceGetCb(self, namespace):
211 jids = self.host.check_jids([self.args.jid])
212 jid = jids[0]
213 self.host.bridge.encryptionTrustUIGet(
214 jid, namespace, self.profile,
215 callback=self.encryptionTrustUIGetCb,
216 errback=partial(
217 self.errback,
218 msg=_(u"can't end encrypted session: {}"),
219 exit_code=C.EXIT_BRIDGE_ERRBACK,
220 ),
221 )
222
223 def start(self):
224 if self.args.name is not None:
225 self.host.bridge.encryptionNamespaceGet(self.args.name,
226 callback=self.encryptionNamespaceGetCb,
227 errback=partial(self.errback,
228 msg=_(u"Can't get encryption namespace: {}"),
229 exit_code=C.EXIT_BRIDGE_ERRBACK,
230 ))
231 elif self.args.namespace is not None:
232 self.encryptionNamespaceGetCb(self.args.namespace)
233 else:
234 self.encryptionNamespaceGetCb(u"")
235
236
237 class EncryptionTrust(base.CommandBase):
238 subcommands = (TrustUI,)
239
240 def __init__(self, host):
241 super(EncryptionTrust, self).__init__(
242 host, "trust", use_profile=False, help=_(u"trust manangement")
243 )
244
245
246 class Encryption(base.CommandBase):
247 subcommands = (EncryptionAlgorithms, EncryptionGet, EncryptionStart, EncryptionStop,
248 EncryptionTrust)
249
250 def __init__(self, host):
251 super(Encryption, self).__init__(
252 host, "encryption", use_profile=False, help=_(u"encryption sessions handling")
253 )