1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2018 Greenbone Networks GmbH |
3
|
|
|
# |
4
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later |
5
|
|
|
# |
6
|
|
|
# This program is free software: you can redistribute it and/or modify |
7
|
|
|
# it under the terms of the GNU General Public License as published by |
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
# (at your option) any later version. |
10
|
|
|
# |
11
|
|
|
# This program is distributed in the hope that it will be useful, |
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
# GNU General Public License for more details. |
15
|
|
|
# |
16
|
|
|
# You should have received a copy of the GNU General Public License |
17
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
|
19
|
|
|
class GvmProtocol: |
20
|
|
|
"""Base class for different GVM protocols |
21
|
|
|
|
22
|
|
|
Attributes: |
23
|
|
|
connection (:class:`gvm.connections.GvmConnection`): Connection to use |
24
|
|
|
to talk with the remote daemon. See :mod:`gvm.connections` for |
25
|
|
|
possible connection types. |
26
|
|
|
transform (`callable`_, optional): Optional transform callable to |
27
|
|
|
convert response data. After each request the callable gets passed |
28
|
|
|
the plain response data which can be used to check the data and/or |
29
|
|
|
conversion into different representaitions like a xml dom. |
30
|
|
|
|
31
|
|
|
See :mod:`gvm.transforms` for existing transforms. |
32
|
|
|
""" |
33
|
|
|
|
34
|
|
|
def __init__(self, connection, transform=None): |
35
|
|
|
self._connection = connection |
36
|
|
|
|
37
|
|
|
self._connected = False |
38
|
|
|
|
39
|
|
|
self._transform_callable = transform |
40
|
|
|
|
41
|
|
|
def __enter__(self): |
42
|
|
|
self.connect() |
43
|
|
|
return self |
44
|
|
|
|
45
|
|
|
def __exit__(self, exc_type, exc_value, traceback): |
46
|
|
|
self.disconnect() |
47
|
|
|
|
48
|
|
|
def _read(self): |
49
|
|
|
"""Read a command response from gvmd |
50
|
|
|
|
51
|
|
|
Returns: |
52
|
|
|
str: Response from server. |
53
|
|
|
""" |
54
|
|
|
return self._connection.read() |
55
|
|
|
|
56
|
|
|
def _send(self, data): |
57
|
|
|
"""Send a command to the server |
58
|
|
|
|
59
|
|
|
Args: |
60
|
|
|
data (str): Data to be send over the connection to the server |
61
|
|
|
""" |
62
|
|
|
self.connect() |
63
|
|
|
self._connection.send(data) |
64
|
|
|
|
65
|
|
|
def _transform(self, data): |
66
|
|
|
transform = self._transform_callable |
67
|
|
|
if transform is None: |
68
|
|
|
return data |
69
|
|
|
return transform(data) |
70
|
|
|
|
71
|
|
|
def _send_xml_command(self, xmlcmd): |
72
|
|
|
"""Send a xml command to the remote server |
73
|
|
|
|
74
|
|
|
Arguments: |
75
|
|
|
xmlcmd (gvm.xml.XmlCommand): XmlCommand instance to send |
76
|
|
|
""" |
77
|
|
|
return self.send_command(xmlcmd.to_string()) |
78
|
|
|
|
79
|
|
|
def is_connected(self): |
80
|
|
|
"""Status of the current connection |
81
|
|
|
|
82
|
|
|
Returns: |
83
|
|
|
bool: True if a connection to the remote server has been |
84
|
|
|
established. |
85
|
|
|
""" |
86
|
|
|
return self._connected |
87
|
|
|
|
88
|
|
|
def connect(self): |
89
|
|
|
"""Initiates a protocol connection |
90
|
|
|
|
91
|
|
|
Normally connect isn't called directly. Either it's called automatically |
92
|
|
|
when sending a protocol command or when using a `with statement`_. |
93
|
|
|
|
94
|
|
|
.. _with statement: |
95
|
|
|
https://docs.python.org/3.5/reference/datamodel.html#with-statement-context-managers |
96
|
|
|
""" |
97
|
|
|
if not self.is_connected(): |
98
|
|
|
self._connection.connect() |
99
|
|
|
self._connected = True |
100
|
|
|
|
101
|
|
|
def disconnect(self): |
102
|
|
|
"""Disconnect the connection |
103
|
|
|
|
104
|
|
|
Ends and closes the connection. |
105
|
|
|
""" |
106
|
|
|
if self.is_connected(): |
107
|
|
|
self._connection.disconnect() |
108
|
|
|
self._connected = False |
109
|
|
|
|
110
|
|
|
def send_command(self, cmd): |
111
|
|
|
"""Send a command to the remote server |
112
|
|
|
|
113
|
|
|
If the class isn't connected to the server yet the connection will be |
114
|
|
|
established automatically. |
115
|
|
|
|
116
|
|
|
Arguments: |
117
|
|
|
cmd (str): Command as string to be send over the connection to |
118
|
|
|
the server. |
119
|
|
|
|
120
|
|
|
Returns: |
121
|
|
|
any: The actual returned type depends on the set transform. |
122
|
|
|
|
123
|
|
|
Per default - if no transform is set explicitly - the response is |
124
|
|
|
returned as string. |
125
|
|
|
""" |
126
|
|
|
try: |
127
|
|
|
self._send(cmd) |
128
|
|
|
response = self._read() |
129
|
|
|
finally: |
130
|
|
|
self.disconnect() |
131
|
|
|
|
132
|
|
|
return self._transform(response) |
|
|
|
|
133
|
|
|
|