|
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
|
|
|
import defusedxml.lxml as secET |
|
20
|
|
|
|
|
21
|
|
|
from lxml import etree |
|
22
|
|
|
|
|
23
|
|
|
class XmlCommandElement: |
|
24
|
|
|
|
|
25
|
|
|
def __init__(self, element): |
|
26
|
|
|
self._element = element |
|
27
|
|
|
|
|
28
|
|
|
def add_element(self, name, text=None, attrs=None): |
|
29
|
|
|
node = etree.SubElement(self._element, name, attrib=attrs) |
|
30
|
|
|
node.text = text |
|
31
|
|
|
return XmlCommandElement(node) |
|
32
|
|
|
|
|
33
|
|
|
def set_attribute(self, name, value): |
|
34
|
|
|
self._element.set(name, value) |
|
35
|
|
|
|
|
36
|
|
|
def set_attributes(self, attrs): |
|
37
|
|
|
"""Set several attributes at once. |
|
38
|
|
|
|
|
39
|
|
|
Arguments: |
|
40
|
|
|
attrs (dict): Attributes to be set on the element |
|
41
|
|
|
""" |
|
42
|
|
|
for key, value in attrs.items(): |
|
43
|
|
|
self._element.set(key, value) |
|
44
|
|
|
|
|
45
|
|
|
def append_xml_str(self, xml_text): |
|
46
|
|
|
"""Append a xml element in string format.""" |
|
47
|
|
|
node = secET.fromstring(xml_text) |
|
48
|
|
|
self._element.append(node) |
|
49
|
|
|
|
|
50
|
|
|
def to_string(self): |
|
51
|
|
|
return etree.tostring(self._element).decode('utf-8') |
|
52
|
|
|
|
|
53
|
|
|
def __str__(self): |
|
54
|
|
|
return self.to_string() |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
class XmlCommand(XmlCommandElement): |
|
58
|
|
|
|
|
59
|
|
|
def __init__(self, name): |
|
60
|
|
|
super().__init__(etree.Element(name)) |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
class _GmpCommandFactory: |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
"""Factory to create gmp - Greenbone Management Protocol - commands |
|
66
|
|
|
""" |
|
67
|
|
|
|
|
68
|
|
|
def modify_scanner_command(self, scanner_id, host, port, scanner_type, |
|
|
|
|
|
|
69
|
|
|
kwargs): |
|
70
|
|
|
"""Generates xml string for modify scanner on gvmd.""" |
|
71
|
|
|
if not scanner_id: |
|
72
|
|
|
raise ValueError('modify_scanner requires a scanner_id element') |
|
73
|
|
|
if not host: |
|
74
|
|
|
raise ValueError('modify_scanner requires a host element') |
|
75
|
|
|
if not port: |
|
76
|
|
|
raise ValueError('modify_scanner requires a port element') |
|
77
|
|
|
if not scanner_type: |
|
78
|
|
|
raise ValueError('modify_scanner requires a type element') |
|
79
|
|
|
|
|
80
|
|
|
cmd = XmlCommand('modify_scanner') |
|
81
|
|
|
cmd.set_attribute('scanner_id', scanner_id) |
|
82
|
|
|
cmd.add_element('host', host) |
|
83
|
|
|
cmd.add_element('port', port) |
|
84
|
|
|
cmd.add_element('type', scanner_type) |
|
85
|
|
|
|
|
86
|
|
|
comment = kwargs.get('comment', '') |
|
87
|
|
|
if comment: |
|
88
|
|
|
cmd.add_element('comment', comment) |
|
89
|
|
|
|
|
90
|
|
|
name = kwargs.get('name', '') |
|
91
|
|
|
if name: |
|
92
|
|
|
cmd.add_element('name', name) |
|
93
|
|
|
|
|
94
|
|
|
ca_pub = kwargs.get('ca_pub', '') |
|
95
|
|
|
if ca_pub: |
|
96
|
|
|
cmd.add_element('ca_pub', ca_pub) |
|
97
|
|
|
|
|
98
|
|
|
credential_id = kwargs.get('credential_id', '') |
|
99
|
|
|
if credential_id: |
|
100
|
|
|
cmd.add_element('credential', attrs={'id': str(credential_id)}) |
|
101
|
|
|
|
|
102
|
|
|
return cmd.to_string() |
|
103
|
|
|
|
|
104
|
|
|
def modify_schedule_command(self, schedule_id, kwargs): |
|
|
|
|
|
|
105
|
|
|
"""Generates xml string for modify schedule on gvmd.""" |
|
106
|
|
|
if not schedule_id: |
|
107
|
|
|
raise ValueError('modify_schedule requires a schedule_id element') |
|
108
|
|
|
|
|
109
|
|
|
cmd = XmlCommand('modify_schedule') |
|
110
|
|
|
cmd.set_attribute('schedule_id', schedule_id) |
|
111
|
|
|
comment = kwargs.get('comment', '') |
|
112
|
|
|
if comment: |
|
113
|
|
|
cmd.add_element('comment', comment) |
|
114
|
|
|
|
|
115
|
|
|
name = kwargs.get('name', '') |
|
116
|
|
|
if name: |
|
117
|
|
|
cmd.add_element('name', name) |
|
118
|
|
|
|
|
119
|
|
|
first_time = kwargs.get('first_time', '') |
|
120
|
|
|
if first_time: |
|
121
|
|
|
first_time_minute = first_time['minute'] |
|
122
|
|
|
first_time_hour = first_time['hour'] |
|
123
|
|
|
first_time_day_of_month = first_time['day_of_month'] |
|
124
|
|
|
first_time_month = first_time['month'] |
|
125
|
|
|
first_time_year = first_time['year'] |
|
126
|
|
|
|
|
127
|
|
|
_xmlftime = cmd.add_element('first_time') |
|
128
|
|
|
_xmlftime.add_element('minute', str(first_time_minute)) |
|
129
|
|
|
_xmlftime.add_element('hour', str(first_time_hour)) |
|
130
|
|
|
_xmlftime.add_element('day_of_month', str(first_time_day_of_month)) |
|
131
|
|
|
_xmlftime.add_element('month', str(first_time_month)) |
|
132
|
|
|
_xmlftime.add_element('year', str(first_time_year)) |
|
133
|
|
|
|
|
134
|
|
|
duration = kwargs.get('duration', '') |
|
135
|
|
|
if len(duration) > 1: |
|
136
|
|
|
_xmlduration = cmd.add_element('duration', str(duration[0])) |
|
137
|
|
|
_xmlduration.add_element('unit', str(duration[1])) |
|
138
|
|
|
|
|
139
|
|
|
period = kwargs.get('period', '') |
|
140
|
|
|
if len(period) > 1: |
|
141
|
|
|
_xmlperiod = cmd.add_element('period', str(period[0])) |
|
142
|
|
|
_xmlperiod.add_element('unit', str(period[1])) |
|
143
|
|
|
|
|
144
|
|
|
timezone = kwargs.get('timezone', '') |
|
145
|
|
|
if timezone: |
|
146
|
|
|
cmd.add_element('timezone', str(timezone)) |
|
147
|
|
|
|
|
148
|
|
|
return cmd.to_string() |
|
149
|
|
|
|
|
150
|
|
|
def modify_setting_command(self, setting_id, name, value): |
|
|
|
|
|
|
151
|
|
|
"""Generates xml string for modify setting format on gvmd.""" |
|
152
|
|
|
cmd = XmlCommand('modify_setting') |
|
153
|
|
|
cmd.set_attribute('setting_id', setting_id) |
|
154
|
|
|
cmd.add_element('name', name) |
|
155
|
|
|
cmd.add_element('value', value) |
|
156
|
|
|
|
|
157
|
|
|
return cmd.to_string() |
|
158
|
|
|
|
|
159
|
|
|
def modify_tag_command(self, tag_id, kwargs): |
|
|
|
|
|
|
160
|
|
|
"""Generates xml string for modify tag on gvmd.""" |
|
161
|
|
|
if not tag_id: |
|
162
|
|
|
raise ValueError('modify_tag requires a tag_id element') |
|
163
|
|
|
|
|
164
|
|
|
cmd = XmlCommand('modify_tag') |
|
165
|
|
|
cmd.set_attribute('tag_id', str(tag_id)) |
|
166
|
|
|
|
|
167
|
|
|
comment = kwargs.get('comment', '') |
|
168
|
|
|
if comment: |
|
169
|
|
|
cmd.add_element('comment', comment) |
|
170
|
|
|
|
|
171
|
|
|
name = kwargs.get('name', '') |
|
172
|
|
|
if name: |
|
173
|
|
|
cmd.add_element('name', name) |
|
174
|
|
|
|
|
175
|
|
|
value = kwargs.get('value', '') |
|
176
|
|
|
if value: |
|
177
|
|
|
cmd.add_element('value', value) |
|
178
|
|
|
|
|
179
|
|
|
active = kwargs.get('active', '') |
|
180
|
|
|
if active: |
|
181
|
|
|
cmd.add_element('active', value) |
|
182
|
|
|
|
|
183
|
|
|
resource = kwargs.get('resource', '') |
|
184
|
|
|
if resource: |
|
185
|
|
|
resource_id = resource['id'] |
|
186
|
|
|
resource_type = resource['type'] |
|
187
|
|
|
_xmlresource = cmd.add_element('resource', |
|
188
|
|
|
attrs={'resource_id': resource_id}) |
|
189
|
|
|
_xmlresource.add_element('type', resource_type) |
|
190
|
|
|
|
|
191
|
|
|
return cmd.to_string() |
|
192
|
|
|
|
|
193
|
|
|
def modify_target_command(self, target_id, kwargs): |
|
|
|
|
|
|
194
|
|
|
"""Generates xml string for modify target on gvmd.""" |
|
195
|
|
|
if not target_id: |
|
196
|
|
|
raise ValueError('modify_target requires a target_id element') |
|
197
|
|
|
|
|
198
|
|
|
cmd = XmlCommand('modify_target') |
|
199
|
|
|
cmd.set_attribute('target_id', target_id) |
|
200
|
|
|
|
|
201
|
|
|
comment = kwargs.get('comment', '') |
|
202
|
|
|
if comment: |
|
203
|
|
|
cmd.add_element('comment', comment) |
|
204
|
|
|
|
|
205
|
|
|
name = kwargs.get('name', '') |
|
206
|
|
|
if name: |
|
207
|
|
|
cmd.add_element('name', name) |
|
208
|
|
|
|
|
209
|
|
|
hosts = kwargs.get('hosts', '') |
|
210
|
|
|
if hosts: |
|
211
|
|
|
cmd.add_element('hosts', hosts) |
|
212
|
|
|
|
|
213
|
|
|
copy = kwargs.get('copy', '') |
|
214
|
|
|
if copy: |
|
215
|
|
|
cmd.add_element('copy', copy) |
|
216
|
|
|
|
|
217
|
|
|
exclude_hosts = kwargs.get('exclude_hosts', '') |
|
218
|
|
|
if exclude_hosts: |
|
219
|
|
|
cmd.add_element('exclude_hosts', exclude_hosts) |
|
220
|
|
|
|
|
221
|
|
|
alive_tests = kwargs.get('alive_tests', '') |
|
222
|
|
|
if alive_tests: |
|
223
|
|
|
cmd.add_element('alive_tests', alive_tests) |
|
224
|
|
|
|
|
225
|
|
|
reverse_lookup_only = kwargs.get('reverse_lookup_only', '') |
|
226
|
|
|
if reverse_lookup_only: |
|
227
|
|
|
cmd.add_element('reverse_lookup_only', reverse_lookup_only) |
|
228
|
|
|
|
|
229
|
|
|
reverse_lookup_unify = kwargs.get('reverse_lookup_unify', '') |
|
230
|
|
|
if reverse_lookup_unify: |
|
231
|
|
|
cmd.add_element('reverse_lookup_unify', reverse_lookup_unify) |
|
232
|
|
|
|
|
233
|
|
|
port_range = kwargs.get('port_range', '') |
|
234
|
|
|
if port_range: |
|
235
|
|
|
cmd.add_element('port_range', port_range) |
|
236
|
|
|
|
|
237
|
|
|
port_list = kwargs.get('port_list', '') |
|
238
|
|
|
if port_list: |
|
239
|
|
|
cmd.add_element('port_list', attrs={'id': str(port_list)}) |
|
240
|
|
|
|
|
241
|
|
|
return cmd.to_string() |
|
242
|
|
|
|
|
243
|
|
|
def modify_task_command(self, task_id, kwargs): |
|
|
|
|
|
|
244
|
|
|
"""Generates xml string for modify task on gvmd.""" |
|
245
|
|
|
if not task_id: |
|
246
|
|
|
raise ValueError('modify_task requires a task_id element') |
|
247
|
|
|
|
|
248
|
|
|
cmd = XmlCommand('modify_task') |
|
249
|
|
|
cmd.set_attribute('task_id', task_id) |
|
250
|
|
|
|
|
251
|
|
|
name = kwargs.get('name', '') |
|
252
|
|
|
if name: |
|
253
|
|
|
cmd.add_element('name', name) |
|
254
|
|
|
|
|
255
|
|
|
comment = kwargs.get('comment', '') |
|
256
|
|
|
if comment: |
|
257
|
|
|
cmd.add_element('comment', comment) |
|
258
|
|
|
|
|
259
|
|
|
target_id = kwargs.get('target_id', '') |
|
260
|
|
|
if target_id: |
|
261
|
|
|
cmd.add_element('target', attrs={'id': target_id}) |
|
262
|
|
|
|
|
263
|
|
|
scanner = kwargs.get('scanner', '') |
|
264
|
|
|
if scanner: |
|
265
|
|
|
cmd.add_element('scanner', attrs={'id': scanner}) |
|
266
|
|
|
|
|
267
|
|
|
schedule_periods = kwargs.get('schedule_periods', '') |
|
268
|
|
|
if schedule_periods: |
|
269
|
|
|
cmd.add_element('schedule_periods', str(schedule_periods)) |
|
270
|
|
|
|
|
271
|
|
|
schedule = kwargs.get('schedule', '') |
|
272
|
|
|
if schedule: |
|
273
|
|
|
cmd.add_element('schedule', attrs={'id': str(schedule)}) |
|
274
|
|
|
|
|
275
|
|
|
alert = kwargs.get('alert', '') |
|
276
|
|
|
if alert: |
|
277
|
|
|
cmd.add_element('alert', attrs={'id': str(alert)}) |
|
278
|
|
|
|
|
279
|
|
|
observers = kwargs.get('observers', '') |
|
280
|
|
|
if observers: |
|
281
|
|
|
cmd.add_element('observers', str(observers)) |
|
282
|
|
|
|
|
283
|
|
|
preferences = kwargs.get('preferences', '') |
|
284
|
|
|
if preferences: |
|
285
|
|
|
_xmlprefs = cmd.add_element('preferences') |
|
286
|
|
|
for n in range(len(preferences["scanner_name"])): |
|
|
|
|
|
|
287
|
|
|
preferences_scanner_name = preferences["scanner_name"][n] |
|
288
|
|
|
preferences_value = preferences["value"][n] |
|
289
|
|
|
_xmlpref = _xmlprefs.add_element('preference') |
|
290
|
|
|
_xmlpref.add_element('scanner_name', preferences_scanner_name) |
|
291
|
|
|
_xmlpref.add_element('value', preferences_value) |
|
292
|
|
|
|
|
293
|
|
|
file = kwargs.get('file', '') |
|
294
|
|
|
if file: |
|
295
|
|
|
file_name = file['name'] |
|
296
|
|
|
file_action = file['action'] |
|
297
|
|
|
if file_action != "update" and file_action != "remove": |
|
|
|
|
|
|
298
|
|
|
raise ValueError('action can only be "update" or "remove"!') |
|
299
|
|
|
cmd.add_element('file', attrs={'name': file_name, |
|
300
|
|
|
'action': file_action}) |
|
301
|
|
|
|
|
302
|
|
|
return cmd.to_string() |
|
303
|
|
|
|
|
304
|
|
|
def modify_user_command(self, kwargs): |
|
|
|
|
|
|
305
|
|
|
"""Generates xml string for modify user on gvmd.""" |
|
306
|
|
|
user_id = kwargs.get('user_id', '') |
|
307
|
|
|
name = kwargs.get('name', '') |
|
308
|
|
|
|
|
309
|
|
|
if not user_id and not name: |
|
310
|
|
|
raise ValueError('modify_user requires ' |
|
311
|
|
|
'either a user_id or a name element') |
|
312
|
|
|
|
|
313
|
|
|
cmd = XmlCommand('modify_user') |
|
314
|
|
|
cmd.set_attribute('user_id', str(user_id)) |
|
315
|
|
|
|
|
316
|
|
|
new_name = kwargs.get('new_name', '') |
|
317
|
|
|
if new_name: |
|
318
|
|
|
cmd.add_element('new_name', new_name) |
|
319
|
|
|
|
|
320
|
|
|
password = kwargs.get('password', '') |
|
321
|
|
|
if password: |
|
322
|
|
|
cmd.add_element('password', password) |
|
323
|
|
|
|
|
324
|
|
|
role_ids = kwargs.get('role_ids', '') |
|
325
|
|
|
if len(role_ids) > 0: |
|
326
|
|
|
for role in role_ids: |
|
327
|
|
|
cmd.add_element('role', attrs={'id': str(role)}) |
|
328
|
|
|
|
|
329
|
|
|
hosts = kwargs.get('hosts', '') |
|
330
|
|
|
hosts_allow = kwargs.get('hosts_allow', '') |
|
331
|
|
|
if hosts or hosts_allow: |
|
332
|
|
|
cmd.add_element('hosts', hosts, attrs={'allow': str(hosts_allow)}) |
|
333
|
|
|
|
|
334
|
|
|
ifaces = kwargs.get('ifaces', '') |
|
335
|
|
|
ifaces_allow = kwargs.get('ifaces_allow', '') |
|
336
|
|
|
if ifaces or ifaces_allow: |
|
337
|
|
|
cmd.add_element('ifaces', ifaces, |
|
338
|
|
|
attrs={'allow': str(ifaces_allow)}) |
|
339
|
|
|
|
|
340
|
|
|
sources = kwargs.get('sources', '') |
|
341
|
|
|
if sources: |
|
342
|
|
|
cmd.add_element('sources', sources) |
|
343
|
|
|
|
|
344
|
|
|
return cmd.to_string() |
|
345
|
|
|
|
|
346
|
|
|
def delete_agent_command(self, kwargs): |
|
|
|
|
|
|
347
|
|
|
"""Generates xml string for delete agent on gvmd""" |
|
348
|
|
|
cmd = XmlCommand('delete_agent') |
|
349
|
|
|
for key, value in kwargs.items(): |
|
350
|
|
|
cmd.set_attribute(key, value) |
|
351
|
|
|
|
|
352
|
|
|
return cmd.to_string() |
|
353
|
|
|
|
|
354
|
|
|
def delete_alert_command(self, kwargs): |
|
|
|
|
|
|
355
|
|
|
"""Generates xml string for delete alert on gvmd""" |
|
356
|
|
|
cmd = XmlCommand('delete_alert') |
|
357
|
|
|
for key, value in kwargs.items(): |
|
358
|
|
|
cmd.set_attribute(key, value) |
|
359
|
|
|
|
|
360
|
|
|
return cmd.to_string() |
|
361
|
|
|
|
|
362
|
|
|
def delete_asset_command(self, asset_id, ultimate=0): |
|
|
|
|
|
|
363
|
|
|
"""Generates xml string for delete asset on gvmd""" |
|
364
|
|
|
cmd = XmlCommand('delete_asset') |
|
365
|
|
|
cmd.set_attribute('asset_id', asset_id) |
|
366
|
|
|
cmd.set_attribute('ultimate', ultimate) |
|
367
|
|
|
|
|
368
|
|
|
return cmd.to_string() |
|
369
|
|
|
|
|
370
|
|
|
def delete_config_command(self, config_id, ultimate=0): |
|
|
|
|
|
|
371
|
|
|
"""Generates xml string for delete config on gvmd""" |
|
372
|
|
|
cmd = XmlCommand('delete_config') |
|
373
|
|
|
cmd.set_attribute('config_id', config_id) |
|
374
|
|
|
cmd.set_attribute('ultimate', ultimate) |
|
375
|
|
|
|
|
376
|
|
|
return cmd.to_string() |
|
377
|
|
|
|
|
378
|
|
|
def delete_credential_command(self, credential_id, ultimate=0): |
|
|
|
|
|
|
379
|
|
|
"""Generates xml string for delete credential on gvmd""" |
|
380
|
|
|
cmd = XmlCommand('delete_credential') |
|
381
|
|
|
cmd.set_attribute('credential_id', credential_id) |
|
382
|
|
|
cmd.set_attribute('ultimate', ultimate) |
|
383
|
|
|
return cmd.to_string() |
|
384
|
|
|
|
|
385
|
|
|
def delete_filter_command(self, filter_id, ultimate=0): |
|
|
|
|
|
|
386
|
|
|
"""Generates xml string for delete filter on gvmd""" |
|
387
|
|
|
cmd = XmlCommand('delete_filter') |
|
388
|
|
|
cmd.set_attribute('filter_id', filter_id) |
|
389
|
|
|
cmd.set_attribute('ultimate', ultimate) |
|
390
|
|
|
|
|
391
|
|
|
return cmd.to_string() |
|
392
|
|
|
|
|
393
|
|
|
def delete_group_command(self, group_id, ultimate=0): |
|
|
|
|
|
|
394
|
|
|
"""Generates xml string for delete group on gvmd""" |
|
395
|
|
|
cmd = XmlCommand('delete_group') |
|
396
|
|
|
cmd.set_attribute('group_id', group_id) |
|
397
|
|
|
cmd.set_attribute('ultimate', ultimate) |
|
398
|
|
|
|
|
399
|
|
|
return cmd.to_string() |
|
400
|
|
|
|
|
401
|
|
|
def delete_note_command(self, note_id, ultimate=0): |
|
|
|
|
|
|
402
|
|
|
"""Generates xml string for delete note on gvmd""" |
|
403
|
|
|
cmd = XmlCommand('delete_note') |
|
404
|
|
|
cmd.set_attribute('note_id', note_id) |
|
405
|
|
|
cmd.set_attribute('ultimate', ultimate) |
|
406
|
|
|
|
|
407
|
|
|
return cmd.to_string() |
|
408
|
|
|
|
|
409
|
|
|
def delete_override_command(self, override_id, ultimate=0): |
|
|
|
|
|
|
410
|
|
|
"""Generates xml string for delete override on gvmd""" |
|
411
|
|
|
cmd = XmlCommand('delete_override') |
|
412
|
|
|
cmd.set_attribute('override_id', override_id) |
|
413
|
|
|
cmd.set_attribute('ultimate', ultimate) |
|
414
|
|
|
|
|
415
|
|
|
return cmd.to_string() |
|
416
|
|
|
|
|
417
|
|
|
def delete_permission_command(self, permission_id, ultimate=0): |
|
|
|
|
|
|
418
|
|
|
"""Generates xml string for delete permission on gvmd""" |
|
419
|
|
|
cmd = XmlCommand('delete_permission') |
|
420
|
|
|
cmd.set_attribute('permission_id', permission_id) |
|
421
|
|
|
cmd.set_attribute('ultimate', ultimate) |
|
422
|
|
|
|
|
423
|
|
|
return cmd.to_string() |
|
424
|
|
|
|
|
425
|
|
|
def delete_port_list_command(self, port_list_id, ultimate=0): |
|
|
|
|
|
|
426
|
|
|
"""Generates xml string for delete port on gvmd""" |
|
427
|
|
|
cmd = XmlCommand('delete_port_list') |
|
428
|
|
|
cmd.set_attribute('port_list_id', port_list_id) |
|
429
|
|
|
cmd.set_attribute('ultimate', ultimate) |
|
430
|
|
|
|
|
431
|
|
|
return cmd.to_string() |
|
432
|
|
|
|
|
433
|
|
|
def delete_port_range_command(self, port_range_id): |
|
|
|
|
|
|
434
|
|
|
"""Generates xml string for delete port on gvmd""" |
|
435
|
|
|
cmd = XmlCommand('delete_port_range') |
|
436
|
|
|
cmd.set_attribute('port_range_id', port_range_id) |
|
437
|
|
|
|
|
438
|
|
|
return cmd.to_string() |
|
439
|
|
|
|
|
440
|
|
|
def delete_report_command(self, report_id): |
|
|
|
|
|
|
441
|
|
|
"""Generates xml string for delete report on gvmd""" |
|
442
|
|
|
cmd = XmlCommand('delete_report') |
|
443
|
|
|
cmd.set_attribute('report_id', report_id) |
|
444
|
|
|
|
|
445
|
|
|
return cmd.to_string() |
|
446
|
|
|
|
|
447
|
|
|
def delete_report_format_command(self, report_format_id, ultimate=0): |
|
|
|
|
|
|
448
|
|
|
"""Generates xml string for delete report on gvmd""" |
|
449
|
|
|
cmd = XmlCommand('delete_report_format') |
|
450
|
|
|
cmd.set_attribute('report_format_id', report_format_id) |
|
451
|
|
|
cmd.set_attribute('ultimate', ultimate) |
|
452
|
|
|
|
|
453
|
|
|
return cmd.to_string() |
|
454
|
|
|
|
|
455
|
|
|
def delete_role_command(self, role_id, ultimate=0): |
|
|
|
|
|
|
456
|
|
|
"""Generates xml string for delete role on gvmd""" |
|
457
|
|
|
cmd = XmlCommand('delete_role') |
|
458
|
|
|
cmd.set_attribute('role_id', role_id) |
|
459
|
|
|
cmd.set_attribute('ultimate', ultimate) |
|
460
|
|
|
|
|
461
|
|
|
return cmd.to_string() |
|
462
|
|
|
|
|
463
|
|
|
def delete_scanner_command(self, scanner_id, ultimate=0): |
|
|
|
|
|
|
464
|
|
|
"""Generates xml string for delete scanner on gvmd""" |
|
465
|
|
|
cmd = XmlCommand('delete_scanner') |
|
466
|
|
|
cmd.set_attribute('scanner_id', scanner_id) |
|
467
|
|
|
cmd.set_attribute('ultimate', ultimate) |
|
468
|
|
|
|
|
469
|
|
|
return cmd.to_string() |
|
470
|
|
|
|
|
471
|
|
|
def delete_schedule_command(self, schedule_id, ultimate=0): |
|
|
|
|
|
|
472
|
|
|
"""Generates xml string for delete schedule on gvmd""" |
|
473
|
|
|
# if self.ask_yes_or_no('Are you sure to delete this schedule? '): |
|
474
|
|
|
cmd = XmlCommand('delete_schedule') |
|
475
|
|
|
cmd.set_attribute('schedule_id', schedule_id) |
|
476
|
|
|
cmd.set_attribute('ultimate', ultimate) |
|
477
|
|
|
|
|
478
|
|
|
return cmd.to_string() |
|
479
|
|
|
|
|
480
|
|
|
def delete_tag_command(self, tag_id, ultimate=0): |
|
|
|
|
|
|
481
|
|
|
"""Generates xml string for delete tag on gvmd""" |
|
482
|
|
|
cmd = XmlCommand('delete_tag') |
|
483
|
|
|
cmd.set_attribute('tag_id', tag_id) |
|
484
|
|
|
cmd.set_attribute('ultimate', ultimate) |
|
485
|
|
|
|
|
486
|
|
|
return cmd.to_string() |
|
487
|
|
|
|
|
488
|
|
|
def delete_target_command(self, target_id, ultimate=0): |
|
|
|
|
|
|
489
|
|
|
"""Generates xml string for delete target on gvmd""" |
|
490
|
|
|
cmd = XmlCommand('delete_target') |
|
491
|
|
|
cmd.set_attribute('target_id', target_id) |
|
492
|
|
|
cmd.set_attribute('ultimate', ultimate) |
|
493
|
|
|
|
|
494
|
|
|
return cmd.to_string() |
|
495
|
|
|
|
|
496
|
|
|
def delete_task_command(self, task_id, ultimate=0): |
|
|
|
|
|
|
497
|
|
|
"""Generates xml string for delete task on gvmd""" |
|
498
|
|
|
cmd = XmlCommand('delete_task') |
|
499
|
|
|
cmd.set_attribute('task_id', task_id) |
|
500
|
|
|
cmd.set_attribute('ultimate', ultimate) |
|
501
|
|
|
|
|
502
|
|
|
return cmd.to_string() |
|
503
|
|
|
|
|
504
|
|
|
def delete_user_command(self, kwargs): |
|
|
|
|
|
|
505
|
|
|
"""Generates xml string for delete user on gvmd""" |
|
506
|
|
|
cmd = XmlCommand('delete_user') |
|
507
|
|
|
|
|
508
|
|
|
user_id = kwargs.get('user_id', '') |
|
509
|
|
|
if user_id: |
|
510
|
|
|
cmd.set_attribute('user_id', user_id) |
|
511
|
|
|
|
|
512
|
|
|
name = kwargs.get('name', '') |
|
513
|
|
|
if name: |
|
514
|
|
|
cmd.set_attribute('name', name) |
|
515
|
|
|
|
|
516
|
|
|
inheritor_id = kwargs.get('inheritor_id', '') |
|
517
|
|
|
if inheritor_id: |
|
518
|
|
|
cmd.set_attribute('inheritor_id', inheritor_id) |
|
519
|
|
|
|
|
520
|
|
|
inheritor_name = kwargs.get('inheritor_name', '') |
|
521
|
|
|
if inheritor_name: |
|
522
|
|
|
cmd.set_attribute('inheritor_name', inheritor_name) |
|
523
|
|
|
|
|
524
|
|
|
return cmd.to_string() |
|
525
|
|
|
|
|
526
|
|
|
def get_assets_command(self, kwargs): |
|
|
|
|
|
|
527
|
|
|
"""Generates xml string for get assets on gvmd.""" |
|
528
|
|
|
cmd = XmlCommand('get_assets') |
|
529
|
|
|
cmd.set_attributes(kwargs) |
|
530
|
|
|
return cmd.to_string() |
|
531
|
|
|
|
|
532
|
|
|
def get_credentials_command(self, kwargs): |
|
|
|
|
|
|
533
|
|
|
"""Generates xml string for get credentials on gvmd.""" |
|
534
|
|
|
cmd = XmlCommand('get_credentials') |
|
535
|
|
|
cmd.set_attributes(kwargs) |
|
536
|
|
|
return cmd.to_string() |
|
537
|
|
|
|
|
538
|
|
|
def get_configs_command(self, kwargs): |
|
|
|
|
|
|
539
|
|
|
"""Generates xml string for get configs on gvmd.""" |
|
540
|
|
|
cmd = XmlCommand('get_configs') |
|
541
|
|
|
cmd.set_attributes(kwargs) |
|
542
|
|
|
return cmd.to_string() |
|
543
|
|
|
|
|
544
|
|
|
def get_feeds_command(self, kwargs): |
|
|
|
|
|
|
545
|
|
|
"""Generates xml string for get feeds on gvmd.""" |
|
546
|
|
|
cmd = XmlCommand('get_feeds') |
|
547
|
|
|
cmd.set_attributes(kwargs) |
|
548
|
|
|
return cmd.to_string() |
|
549
|
|
|
|
|
550
|
|
|
def get_filters_command(self, kwargs): |
|
|
|
|
|
|
551
|
|
|
"""Generates xml string for get filters on gvmd.""" |
|
552
|
|
|
cmd = XmlCommand('get_filters') |
|
553
|
|
|
cmd.set_attributes(kwargs) |
|
554
|
|
|
return cmd.to_string() |
|
555
|
|
|
|
|
556
|
|
|
def get_groups_command(self, kwargs): |
|
|
|
|
|
|
557
|
|
|
"""Generates xml string for get groups on gvmd.""" |
|
558
|
|
|
cmd = XmlCommand('get_groups') |
|
559
|
|
|
cmd.set_attributes(kwargs) |
|
560
|
|
|
return cmd.to_string() |
|
561
|
|
|
|
|
562
|
|
|
def get_info_command(self, kwargs): |
|
|
|
|
|
|
563
|
|
|
"""Generates xml string for get info on gvmd.""" |
|
564
|
|
|
cmd = XmlCommand('get_info') |
|
565
|
|
|
cmd.set_attributes(kwargs) |
|
566
|
|
|
return cmd.to_string() |
|
567
|
|
|
|
|
568
|
|
|
def get_notes_command(self, kwargs): |
|
|
|
|
|
|
569
|
|
|
"""Generates xml string for get notes on gvmd.""" |
|
570
|
|
|
cmd = XmlCommand('get_notes') |
|
571
|
|
|
cmd.set_attributes(kwargs) |
|
572
|
|
|
return cmd.to_string() |
|
573
|
|
|
|
|
574
|
|
|
def get_nvts_command(self, kwargs): |
|
|
|
|
|
|
575
|
|
|
"""Generates xml string for get nvts on gvmd.""" |
|
576
|
|
|
cmd = XmlCommand('get_nvts') |
|
577
|
|
|
cmd.set_attributes(kwargs) |
|
578
|
|
|
return cmd.to_string() |
|
579
|
|
|
|
|
580
|
|
|
def get_nvt_families_command(self, kwargs): |
|
|
|
|
|
|
581
|
|
|
"""Generates xml string for get nvt on gvmd.""" |
|
582
|
|
|
cmd = XmlCommand('get_nvt_families') |
|
583
|
|
|
cmd.set_attributes(kwargs) |
|
584
|
|
|
return cmd.to_string() |
|
585
|
|
|
|
|
586
|
|
|
def get_overrides_command(self, kwargs): |
|
|
|
|
|
|
587
|
|
|
"""Generates xml string for get overrides on gvmd.""" |
|
588
|
|
|
cmd = XmlCommand('get_overrides') |
|
589
|
|
|
cmd.set_attributes(kwargs) |
|
590
|
|
|
return cmd.to_string() |
|
591
|
|
|
|
|
592
|
|
|
def get_permissions_command(self, kwargs): |
|
|
|
|
|
|
593
|
|
|
"""Generates xml string for get permissions on gvmd.""" |
|
594
|
|
|
cmd = XmlCommand('get_permissions') |
|
595
|
|
|
cmd.set_attributes(kwargs) |
|
596
|
|
|
return cmd.to_string() |
|
597
|
|
|
|
|
598
|
|
|
def get_port_lists_command(self, kwargs): |
|
|
|
|
|
|
599
|
|
|
"""Generates xml string for get port on gvmd.""" |
|
600
|
|
|
cmd = XmlCommand('get_port_lists') |
|
601
|
|
|
cmd.set_attributes(kwargs) |
|
602
|
|
|
return cmd.to_string() |
|
603
|
|
|
|
|
604
|
|
|
def get_preferences_command(self, kwargs): |
|
|
|
|
|
|
605
|
|
|
"""Generates xml string for get preferences on gvmd.""" |
|
606
|
|
|
cmd = XmlCommand('get_preferences') |
|
607
|
|
|
cmd.set_attributes(kwargs) |
|
608
|
|
|
return cmd.to_string() |
|
609
|
|
|
|
|
610
|
|
|
def get_reports_command(self, kwargs): |
|
|
|
|
|
|
611
|
|
|
"""Generates xml string for get reports on gvmd.""" |
|
612
|
|
|
cmd = XmlCommand('get_reports') |
|
613
|
|
|
cmd.set_attributes(kwargs) |
|
614
|
|
|
return cmd.to_string() |
|
615
|
|
|
|
|
616
|
|
|
def get_report_formats_command(self, kwargs): |
|
|
|
|
|
|
617
|
|
|
"""Generates xml string for get report on gvmd.""" |
|
618
|
|
|
cmd = XmlCommand('get_report_formats') |
|
619
|
|
|
cmd.set_attributes(kwargs) |
|
620
|
|
|
return cmd.to_string() |
|
621
|
|
|
|
|
622
|
|
|
def get_results_command(self, kwargs): |
|
|
|
|
|
|
623
|
|
|
"""Generates xml string for get results on gvmd.""" |
|
624
|
|
|
cmd = XmlCommand('get_results') |
|
625
|
|
|
cmd.set_attributes(kwargs) |
|
626
|
|
|
return cmd.to_string() |
|
627
|
|
|
|
|
628
|
|
|
def get_roles_command(self, kwargs): |
|
|
|
|
|
|
629
|
|
|
"""Generates xml string for get roles on gvmd.""" |
|
630
|
|
|
cmd = XmlCommand('get_roles') |
|
631
|
|
|
cmd.set_attributes(kwargs) |
|
632
|
|
|
return cmd.to_string() |
|
633
|
|
|
|
|
634
|
|
|
def get_scanners_command(self, kwargs): |
|
|
|
|
|
|
635
|
|
|
"""Generates xml string for get scanners on gvmd.""" |
|
636
|
|
|
cmd = XmlCommand('get_scanners') |
|
637
|
|
|
cmd.set_attributes(kwargs) |
|
638
|
|
|
return cmd.to_string() |
|
639
|
|
|
|
|
640
|
|
|
def get_schedules_command(self, kwargs): |
|
|
|
|
|
|
641
|
|
|
"""Generates xml string for get schedules on gvmd.""" |
|
642
|
|
|
cmd = XmlCommand('get_schedules') |
|
643
|
|
|
cmd.set_attributes(kwargs) |
|
644
|
|
|
return cmd.to_string() |
|
645
|
|
|
|
|
646
|
|
|
def get_settings_command(self, kwargs): |
|
|
|
|
|
|
647
|
|
|
"""Generates xml string for get settings on gvmd.""" |
|
648
|
|
|
cmd = XmlCommand('get_settings') |
|
649
|
|
|
cmd.set_attributes(kwargs) |
|
650
|
|
|
return cmd.to_string() |
|
651
|
|
|
|
|
652
|
|
|
def get_system_reports_command(self, kwargs): |
|
|
|
|
|
|
653
|
|
|
"""Generates xml string for get system on gvmd.""" |
|
654
|
|
|
cmd = XmlCommand('get_system') |
|
655
|
|
|
cmd.set_attributes(kwargs) |
|
656
|
|
|
return cmd.to_string() |
|
657
|
|
|
|
|
658
|
|
|
def get_tags_command(self, kwargs): |
|
|
|
|
|
|
659
|
|
|
"""Generates xml string for get tags on gvmd.""" |
|
660
|
|
|
cmd = XmlCommand('get_tags') |
|
661
|
|
|
cmd.set_attributes(kwargs) |
|
662
|
|
|
return cmd.to_string() |
|
663
|
|
|
|
|
664
|
|
|
def get_targets_command(self, kwargs): |
|
|
|
|
|
|
665
|
|
|
"""Generates xml string for get targets on gvmd.""" |
|
666
|
|
|
cmd = XmlCommand('get_targets') |
|
667
|
|
|
cmd.set_attributes(kwargs) |
|
668
|
|
|
return cmd.to_string() |
|
669
|
|
|
|
|
670
|
|
|
def get_tasks_command(self, kwargs): |
|
|
|
|
|
|
671
|
|
|
"""Generates xml string for get tasks on gvmd.""" |
|
672
|
|
|
cmd = XmlCommand('get_tasks') |
|
673
|
|
|
cmd.set_attributes(kwargs) |
|
674
|
|
|
return cmd.to_string() |
|
675
|
|
|
|
|
676
|
|
|
def get_users_command(self, kwargs): |
|
|
|
|
|
|
677
|
|
|
"""Generates xml string for get users on gvmd.""" |
|
678
|
|
|
cmd = XmlCommand('get_users') |
|
679
|
|
|
cmd.set_attributes(kwargs) |
|
680
|
|
|
return cmd.to_string() |
|
681
|
|
|
|
|
682
|
|
|
def move_task_command(self, task_id, slave_id): |
|
|
|
|
|
|
683
|
|
|
"""Generates xml string for move task on gvmd.""" |
|
684
|
|
|
cmd = XmlCommand('move_task') |
|
685
|
|
|
cmd.set_attribute('task_id', task_id) |
|
686
|
|
|
cmd.set_attribute('slave_id', slave_id) |
|
687
|
|
|
return cmd.to_string() |
|
688
|
|
|
|
|
689
|
|
|
def restore_command(self, entity_id): |
|
|
|
|
|
|
690
|
|
|
"""Generates xml string for restore on gvmd.""" |
|
691
|
|
|
cmd = XmlCommand('restore') |
|
692
|
|
|
cmd.set_attribute('id', entity_id) |
|
693
|
|
|
return cmd.to_string() |
|
694
|
|
|
|
|
695
|
|
|
def resume_task_command(self, task_id): |
|
|
|
|
|
|
696
|
|
|
"""Generates xml string for resume task on gvmd.""" |
|
697
|
|
|
cmd = XmlCommand('resume_task') |
|
698
|
|
|
cmd.set_attribute('task_id', task_id) |
|
699
|
|
|
return cmd.to_string() |
|
700
|
|
|
|
|
701
|
|
|
def start_task_command(self, task_id): |
|
|
|
|
|
|
702
|
|
|
"""Generates xml string for start task on gvmd.""" |
|
703
|
|
|
cmd = XmlCommand('start_task') |
|
704
|
|
|
cmd.set_attribute('task_id', task_id) |
|
705
|
|
|
return cmd.to_string() |
|
706
|
|
|
|
|
707
|
|
|
def stop_task_command(self, task_id): |
|
|
|
|
|
|
708
|
|
|
"""Generates xml string for stop task on gvmd.""" |
|
709
|
|
|
cmd = XmlCommand('stop_task') |
|
710
|
|
|
cmd.set_attribute('task_id', task_id) |
|
711
|
|
|
return cmd.to_string() |
|
712
|
|
|
|
|
713
|
|
|
def test_alert_command(self, alert_id): |
|
|
|
|
|
|
714
|
|
|
"""Generates xml string for test alert on gvmd.""" |
|
715
|
|
|
cmd = XmlCommand('test_alert') |
|
716
|
|
|
cmd.set_attribute('alert_id', alert_id) |
|
717
|
|
|
return cmd.to_string() |
|
718
|
|
|
|
|
719
|
|
|
def verify_agent_command(self, agent_id): |
|
|
|
|
|
|
720
|
|
|
"""Generates xml string for verify agent on gvmd.""" |
|
721
|
|
|
cmd = XmlCommand('verify_agent') |
|
722
|
|
|
cmd.set_attribute('agent_id', agent_id) |
|
723
|
|
|
return cmd.to_string() |
|
724
|
|
|
|
|
725
|
|
|
def verify_report_format_command(self, report_format_id): |
|
|
|
|
|
|
726
|
|
|
"""Generates xml string for verify report format on gvmd.""" |
|
727
|
|
|
cmd = XmlCommand('verify_report_format') |
|
728
|
|
|
cmd.set_attribute('report_format_id', report_format_id) |
|
729
|
|
|
return cmd.to_string() |
|
730
|
|
|
|
|
731
|
|
|
def verify_scanner_command(self, scanner_id): |
|
|
|
|
|
|
732
|
|
|
"""Generates xml string for verify scanner on gvmd.""" |
|
733
|
|
|
cmd = XmlCommand('verify_scanner') |
|
734
|
|
|
cmd.set_attribute('scanner_id', scanner_id) |
|
735
|
|
|
return cmd.to_string() |
|
736
|
|
|
|
|
737
|
|
|
|
|
738
|
|
|
def pretty_print(xml): |
|
739
|
|
|
"""Prints beautiful XML-Code |
|
740
|
|
|
|
|
741
|
|
|
This function gets an object of list<lxml.etree._Element> |
|
742
|
|
|
or directly a lxml element. |
|
743
|
|
|
Print it with good readable format. |
|
744
|
|
|
|
|
745
|
|
|
Arguments: |
|
746
|
|
|
xml: List<lxml.etree.Element> or directly a lxml element |
|
747
|
|
|
""" |
|
748
|
|
|
if isinstance(xml, list): |
|
749
|
|
|
for item in xml: |
|
750
|
|
|
if etree.iselement(item): |
|
751
|
|
|
print(etree.tostring(item, pretty_print=True).decode('utf-8')) |
|
752
|
|
|
else: |
|
753
|
|
|
print(item) |
|
754
|
|
|
elif etree.iselement(xml): |
|
755
|
|
|
print(etree.tostring(xml, pretty_print=True).decode('utf-8')) |
|
756
|
|
|
|