|
1 #!/usr/bin/env python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 # This file is part of Beremiz, a Integrated Development Environment for |
|
5 # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
|
6 # |
|
7 # Copyright (C) 2019: Edouard TISSERANT |
|
8 # |
|
9 # See COPYING file for copyrights details. |
|
10 # |
|
11 # This program is free software; you can redistribute it and/or |
|
12 # modify it under the terms of the GNU General Public License |
|
13 # as published by the Free Software Foundation; either version 2 |
|
14 # of the License, or (at your option) any later version. |
|
15 # |
|
16 # This program is distributed in the hope that it will be useful, |
|
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19 # GNU General Public License for more details. |
|
20 # |
|
21 # You should have received a copy of the GNU General Public License |
|
22 # along with this program; if not, write to the Free Software |
|
23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
24 |
|
25 |
|
26 """ |
|
27 The TLS-PSK adapter that handles SSL connections instead of regular sockets, |
|
28 but using Pre Shared Keys instead of Certificates |
|
29 """ |
|
30 |
1 from __future__ import absolute_import |
31 from __future__ import absolute_import |
2 from __future__ import print_function |
32 from __future__ import print_function |
3 |
33 |
4 import socket |
34 import socket |
5 import re |
35 import re |
|
36 import ssl |
6 import sslpsk |
37 import sslpsk |
7 import ssl |
|
8 import Pyro |
38 import Pyro |
9 from Pyro.core import PyroURI |
39 from Pyro.core import PyroURI |
10 from Pyro.protocol import _connect_socket, TCPConnection, PYROAdapter |
40 from Pyro.protocol import _connect_socket, TCPConnection, PYROAdapter |
11 from Pyro.errors import ConnectionDeniedError, ProtocolError |
41 from Pyro.errors import ConnectionDeniedError, ProtocolError |
12 from Pyro.util import Log |
42 from Pyro.util import Log |
13 |
43 |
14 |
44 |
15 # The TLS-PSK adapter that handles SSL connections instead of regular sockets, |
|
16 # but using Pre Shared Keys instead of Certificates |
|
17 # |
|
18 class PYROPSKAdapter(PYROAdapter): |
45 class PYROPSKAdapter(PYROAdapter): |
19 # This is essentialy the same as in Pyro/protocol.py |
46 """ |
20 # only raw_sock wrapping into sock through sslpsk.wrap_socket was added |
47 This is essentialy the same as in Pyro/protocol.py |
21 # Pyro unfortunately doesn't allow cleaner customization |
48 only raw_sock wrapping into sock through sslpsk.wrap_socket was added |
|
49 Pyro unfortunately doesn't allow cleaner customization |
|
50 """ |
|
51 |
22 def bindToURI(self, URI): |
52 def bindToURI(self, URI): |
23 with self.lock: # only 1 thread at a time can bind the URI |
53 with self.lock: # only 1 thread at a time can bind the URI |
24 try: |
54 try: |
25 self.URI = URI |
55 self.URI = URI |
26 |
56 |
35 |
65 |
36 conn = TCPConnection(sock, sock.getpeername()) |
66 conn = TCPConnection(sock, sock.getpeername()) |
37 # receive the authentication challenge string, and use that to build the actual identification string. |
67 # receive the authentication challenge string, and use that to build the actual identification string. |
38 try: |
68 try: |
39 authChallenge = self.recvAuthChallenge(conn) |
69 authChallenge = self.recvAuthChallenge(conn) |
40 except ProtocolError, x: |
70 except ProtocolError as x: |
41 # check if we were denied |
71 # check if we were denied |
42 if hasattr(x, "partialMsg") and x.partialMsg[:len(self.denyMSG)] == self.denyMSG: |
72 if hasattr(x, "partialMsg") and x.partialMsg[:len(self.denyMSG)] == self.denyMSG: |
43 raise ConnectionDeniedError(Pyro.constants.deniedReasons[int(x.partialMsg[-1])]) |
73 raise ConnectionDeniedError(Pyro.constants.deniedReasons[int(x.partialMsg[-1])]) |
44 else: |
74 else: |
45 raise |
75 raise |
68 if protocol in ('PYROPSK', 'PYROLOCPSK'): |
98 if protocol in ('PYROPSK', 'PYROLOCPSK'): |
69 return PYROPSKAdapter() |
99 return PYROPSKAdapter() |
70 return _getProtocolAdapter(protocol) |
100 return _getProtocolAdapter(protocol) |
71 |
101 |
72 |
102 |
73 Pyro.protocol.getProtocolAdapter = getProtocolAdapter |
|
74 |
|
75 |
|
76 _processStringURI = Pyro.core.processStringURI |
103 _processStringURI = Pyro.core.processStringURI |
77 |
104 |
78 |
105 |
79 def processStringURI(URI): |
106 def processStringURI(URI): |
80 x = re.match(r'(?P<protocol>PYROLOCPSK)://(?P<hostname>[^\s:]+):?(?P<port>\d+)?/(?P<name>\S*)', URI) |
107 x = re.match(r'(?P<protocol>PYROLOCPSK)://(?P<hostname>[^\s:]+):?(?P<port>\d+)?/(?P<name>\S*)', URI) |
89 name = x.group('name') |
116 name = x.group('name') |
90 return PyroURI(hostname, name, port, protocol) |
117 return PyroURI(hostname, name, port, protocol) |
91 return _processStringURI(URI) |
118 return _processStringURI(URI) |
92 |
119 |
93 |
120 |
94 Pyro.core.processStringURI = processStringURI |
121 def setupPSKAdapter(): |
|
122 """ |
|
123 Add PyroAdapter to the list of available in |
|
124 Pyro adapters and handle new supported protocols |
|
125 |
|
126 This function should be called after |
|
127 reimport of Pyro module to enable PYROS:// again. |
|
128 """ |
|
129 Pyro.protocol.getProtocolAdapter = getProtocolAdapter |
|
130 Pyro.core.processStringURI = processStringURI |