1 # Copyright (C) 2012 W. Trevor King <wking@drexel.edu>
3 # This file is part of pyassuan.
5 # pyassuan is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation, either version 3 of the License, or (at your option) any later
10 # pyassuan is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # pyassuan. If not, see <http://www.gnu.org/licenses/>.
17 import logging as _logging
19 import socket as _socket
21 import threading as _threading
22 import traceback as _traceback
24 from . import LOG as _LOG
25 from . import common as _common
26 from . import error as _error
29 _OPTION_REGEXP = _re.compile('^-?-?([-\w]+)( *)(=?) *(.*?) *\Z')
32 class AssuanServer (object):
33 """A single-threaded Assuan server based on the `devolpment suggestions`_
35 Extend by subclassing and adding ``_handle_XXX`` methods for each
36 command you want to handle.
38 .. _development suggestions:
39 http://www.gnupg.org/documentation/manuals/assuan/Server-code.html
41 def __init__(self, name, logger=_LOG, use_sublogger=True,
42 valid_options=None, strict_options=True,
43 single_request=False, listen_to_quit=False,
44 close_on_disconnect=False):
47 logger = _logging.getLogger('{}.{}'.format(logger.name, self.name))
49 if valid_options is None:
51 self.valid_options = valid_options
52 self.strict_options = strict_options
53 self.single_request = single_request
54 self.listen_to_quit = listen_to_quit
55 self.close_on_disconnect = close_on_disconnect
56 self.input = self.output = None
66 self.logger.info('running')
69 self.handle_requests()
72 self.logger.info('stopping')
76 self.logger.info('read from stdin')
77 self.input = _sys.stdin
79 self.logger.info('write to stdout')
80 self.output = _sys.stdout
83 if self.close_on_disconnect:
84 self.logger.info('disconnecting')
88 def handle_requests(self):
89 self.send_response(_common.Response('OK', 'Your orders please'))
92 line = self.input.readline()
95 if len(line) > _common.LINE_LENGTH:
97 _error.AssuanError(message='Line too long'))
98 if not line.endswith(b'\n'):
99 self.logger.info('C: {}'.format(line))
100 self.send_error_response(
101 _error.AssuanError(message='Invalid request'))
103 line = line[:-1] # remove the trailing newline
104 self.logger.info('C: {}'.format(line))
105 request = _common.Request()
107 request.from_bytes(line)
108 except _error.AssuanError as e:
109 self.send_error_response(e)
111 self.handle_request(request)
113 def handle_request(self, request):
116 self, '_handle_{}'.format(request.command))
117 except AttributeError:
118 self.send_error_response(
119 _error.AssuanError(message='Unknown command'))
122 responses = handle(request.parameters)
123 for response in responses:
124 self.send_response(response)
125 except _error.AssuanError as error:
126 self.send_error_response(error)
128 except Exception as e:
130 'exception while executing {}:\n{}'.format(
131 handle, _traceback.format_exc().rstrip()))
132 self.send_error_response(
133 _error.AssuanError(message='Unspecific Assuan server fault'))
136 def send_response(self, response):
137 """For internal use by ``.handle_requests()``
139 rstring = str(response)
140 self.logger.info('S: {}'.format(response))
141 self.output.write(bytes(response))
142 self.output.write('\n')
149 def send_error_response(self, error):
150 """For internal use by ``.handle_requests()``
152 self.send_response(_common.error_response(error))
154 # common commands defined at
155 # http://www.gnupg.org/documentation/manuals/assuan/Client-requests.html
157 def _handle_BYE(self, arg):
158 if self.single_request:
160 yield _common.Response('OK', 'closing connection')
162 def _handle_RESET(self, arg):
165 def _handle_END(self, arg):
166 raise _error.AssuanError(
167 code=175, message='Unknown command (reserved)')
169 def _handle_HELP(self, arg):
170 raise _error.AssuanError(
171 code=175, message='Unknown command (reserved)')
173 def _handle_QUIT(self, arg):
174 if self.listen_to_quit:
176 yield _common.Response('OK', 'stopping the server')
177 raise _error.AssuanError(
178 code=175, message='Unknown command (reserved)')
180 def _handle_OPTION(self, arg):
183 >>> s = AssuanServer(name='test', valid_options=['my-op'])
184 >>> list(s._handle_OPTION('my-op = 1 ')) # doctest: +ELLIPSIS
185 [<pyassuan.common.Response object at ...>]
188 >>> list(s._handle_OPTION('my-op 2')) # doctest: +ELLIPSIS
189 [<pyassuan.common.Response object at ...>]
192 >>> list(s._handle_OPTION('--my-op 3')) # doctest: +ELLIPSIS
193 [<pyassuan.common.Response object at ...>]
196 >>> list(s._handle_OPTION('my-op')) # doctest: +ELLIPSIS
197 [<pyassuan.common.Response object at ...>]
200 >>> list(s._handle_OPTION('inv'))
201 Traceback (most recent call last):
203 pyassuan.error.AssuanError: 174 Unknown option
204 >>> list(s._handle_OPTION('in|valid'))
205 Traceback (most recent call last):
207 pyassuan.error.AssuanError: 90 Invalid parameter
209 match = _OPTION_REGEXP.match(arg)
211 raise _error.AssuanError(message='Invalid parameter')
212 name,space,equal,value = match.groups()
213 if value and not space and not equal:
214 # need either space or equal to separate value
215 raise _error.AssuanError(message='Invalid parameter')
216 if name not in self.valid_options:
217 if self.strict_options:
218 raise _error.AssuanError(message='Unknown option')
220 self.logger.info('skipping invalid option: {}'.format(name))
224 self.options[name] = value
225 yield _common.Response('OK')
227 def _handle_CANCEL(self, arg):
228 raise _error.AssuanError(
229 code=175, message='Unknown command (reserved)')
231 def _handle_AUTH(self, arg):
232 raise _error.AssuanError(
233 code=175, message='Unknown command (reserved)')
236 class AssuanSocketServer (object):
237 """A threaded server spawning ``AssuanServer``\s for each connection
239 def __init__(self, name, socket, server, kwargs={}, max_threads=10,
240 logger=_LOG, use_sublogger=True):
243 logger = _logging.getLogger('{}.{}'.format(logger.name, self.name))
247 assert 'name' not in kwargs, kwargs['name']
248 assert 'logger' not in kwargs, kwargs['logger']
249 kwargs['logger'] = self.logger
250 assert 'use_sublogger' not in kwargs, kwargs['use_sublogger']
251 kwargs['use_sublogger'] = True
252 if 'close_on_disconnect' in kwargs:
253 assert kwargs['close_on_disconnect'] == True, (
254 kwargs['close_on_disconnect'])
256 kwargs['close_on_disconnect'] = True
258 self.max_threads = max_threads
262 self.logger.info('listen on socket')
266 socket,address = self.socket.accept()
267 self.logger.info('connection from {}'.format(address))
268 self.cleanup_threads()
269 if len(threads) > self.max_threads:
270 self.drop_connection(socket, address)
272 'server-thread-{}'.format(thread_index), socket, address)
273 thread_index = (thread_index + 1) % self.max_threads
275 def cleanup_threads(self):
277 while i < len(self.threads):
278 thread = self.threads[i]
280 if thread.is_alive():
281 self.logger.info('joined thread {}'.format(thread.name))
283 thread.socket.shutdown()
284 thread.socket.close()
288 def drop_connection(self, socket, address):
289 self.logger.info('drop connection from {}'.format(address))
290 # TODO: proper error to send to the client?
292 def spawn_thread(self, name, socket, address):
293 server = self.server(name=name, **self.kwargs)
294 server.input = socket.makefile('r')
295 server.output = socket.makefile('w')
296 thread = _threading.Thread(target=server.run, name=name)
298 self.threads.append(thread)