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 describe_auth_command(self): |
|
|
|
|
527
|
|
|
"""Generates xml string for describe auth on gvmd""" |
528
|
|
|
cmd = XmlCommand('describe_auth') |
529
|
|
|
return cmd.to_string() |
530
|
|
|
|
531
|
|
|
def empty_trashcan_command(self): |
|
|
|
|
532
|
|
|
"""Generates xml string for empty trashcan on gvmd""" |
533
|
|
|
cmd = XmlCommand('empty_trashcan') |
534
|
|
|
return cmd.to_string() |
535
|
|
|
|
536
|
|
|
def get_agents_command(self, kwargs): |
|
|
|
|
537
|
|
|
"""Generates xml string for get agents on gvmd.""" |
538
|
|
|
cmd = XmlCommand('get_agents') |
539
|
|
|
cmd.set_attributes(kwargs) |
540
|
|
|
return cmd.to_string() |
541
|
|
|
|
542
|
|
|
def get_aggregates_command(self, kwargs): |
|
|
|
|
543
|
|
|
"""Generates xml string for get aggregates on gvmd.""" |
544
|
|
|
cmd = XmlCommand('get_aggregates') |
545
|
|
|
cmd.set_attributes(kwargs) |
546
|
|
|
return cmd.to_string() |
547
|
|
|
|
548
|
|
|
def get_alerts_command(self, kwargs): |
|
|
|
|
549
|
|
|
"""Generates xml string for get alerts on gvmd.""" |
550
|
|
|
cmd = XmlCommand('get_alerts') |
551
|
|
|
cmd.set_attributes(kwargs) |
552
|
|
|
return cmd.to_string() |
553
|
|
|
|
554
|
|
|
def get_assets_command(self, kwargs): |
|
|
|
|
555
|
|
|
"""Generates xml string for get assets on gvmd.""" |
556
|
|
|
cmd = XmlCommand('get_assets') |
557
|
|
|
cmd.set_attributes(kwargs) |
558
|
|
|
return cmd.to_string() |
559
|
|
|
|
560
|
|
|
def get_credentials_command(self, kwargs): |
|
|
|
|
561
|
|
|
"""Generates xml string for get credentials on gvmd.""" |
562
|
|
|
cmd = XmlCommand('get_credentials') |
563
|
|
|
cmd.set_attributes(kwargs) |
564
|
|
|
return cmd.to_string() |
565
|
|
|
|
566
|
|
|
def get_configs_command(self, kwargs): |
|
|
|
|
567
|
|
|
"""Generates xml string for get configs on gvmd.""" |
568
|
|
|
cmd = XmlCommand('get_configs') |
569
|
|
|
cmd.set_attributes(kwargs) |
570
|
|
|
return cmd.to_string() |
571
|
|
|
|
572
|
|
|
def get_feeds_command(self, kwargs): |
|
|
|
|
573
|
|
|
"""Generates xml string for get feeds on gvmd.""" |
574
|
|
|
cmd = XmlCommand('get_feeds') |
575
|
|
|
cmd.set_attributes(kwargs) |
576
|
|
|
return cmd.to_string() |
577
|
|
|
|
578
|
|
|
def get_filters_command(self, kwargs): |
|
|
|
|
579
|
|
|
"""Generates xml string for get filters on gvmd.""" |
580
|
|
|
cmd = XmlCommand('get_filters') |
581
|
|
|
cmd.set_attributes(kwargs) |
582
|
|
|
return cmd.to_string() |
583
|
|
|
|
584
|
|
|
def get_groups_command(self, kwargs): |
|
|
|
|
585
|
|
|
"""Generates xml string for get groups on gvmd.""" |
586
|
|
|
cmd = XmlCommand('get_groups') |
587
|
|
|
cmd.set_attributes(kwargs) |
588
|
|
|
return cmd.to_string() |
589
|
|
|
|
590
|
|
|
def get_info_command(self, kwargs): |
|
|
|
|
591
|
|
|
"""Generates xml string for get info on gvmd.""" |
592
|
|
|
cmd = XmlCommand('get_info') |
593
|
|
|
cmd.set_attributes(kwargs) |
594
|
|
|
return cmd.to_string() |
595
|
|
|
|
596
|
|
|
def get_notes_command(self, kwargs): |
|
|
|
|
597
|
|
|
"""Generates xml string for get notes on gvmd.""" |
598
|
|
|
cmd = XmlCommand('get_notes') |
599
|
|
|
cmd.set_attributes(kwargs) |
600
|
|
|
return cmd.to_string() |
601
|
|
|
|
602
|
|
|
def get_nvts_command(self, kwargs): |
|
|
|
|
603
|
|
|
"""Generates xml string for get nvts on gvmd.""" |
604
|
|
|
cmd = XmlCommand('get_nvts') |
605
|
|
|
cmd.set_attributes(kwargs) |
606
|
|
|
return cmd.to_string() |
607
|
|
|
|
608
|
|
|
def get_nvt_families_command(self, kwargs): |
|
|
|
|
609
|
|
|
"""Generates xml string for get nvt on gvmd.""" |
610
|
|
|
cmd = XmlCommand('get_nvt_families') |
611
|
|
|
cmd.set_attributes(kwargs) |
612
|
|
|
return cmd.to_string() |
613
|
|
|
|
614
|
|
|
def get_overrides_command(self, kwargs): |
|
|
|
|
615
|
|
|
"""Generates xml string for get overrides on gvmd.""" |
616
|
|
|
cmd = XmlCommand('get_overrides') |
617
|
|
|
cmd.set_attributes(kwargs) |
618
|
|
|
return cmd.to_string() |
619
|
|
|
|
620
|
|
|
def get_permissions_command(self, kwargs): |
|
|
|
|
621
|
|
|
"""Generates xml string for get permissions on gvmd.""" |
622
|
|
|
cmd = XmlCommand('get_permissions') |
623
|
|
|
cmd.set_attributes(kwargs) |
624
|
|
|
return cmd.to_string() |
625
|
|
|
|
626
|
|
|
def get_port_lists_command(self, kwargs): |
|
|
|
|
627
|
|
|
"""Generates xml string for get port on gvmd.""" |
628
|
|
|
cmd = XmlCommand('get_port_lists') |
629
|
|
|
cmd.set_attributes(kwargs) |
630
|
|
|
return cmd.to_string() |
631
|
|
|
|
632
|
|
|
def get_preferences_command(self, kwargs): |
|
|
|
|
633
|
|
|
"""Generates xml string for get preferences on gvmd.""" |
634
|
|
|
cmd = XmlCommand('get_preferences') |
635
|
|
|
cmd.set_attributes(kwargs) |
636
|
|
|
return cmd.to_string() |
637
|
|
|
|
638
|
|
|
def get_reports_command(self, kwargs): |
|
|
|
|
639
|
|
|
"""Generates xml string for get reports on gvmd.""" |
640
|
|
|
cmd = XmlCommand('get_reports') |
641
|
|
|
cmd.set_attributes(kwargs) |
642
|
|
|
return cmd.to_string() |
643
|
|
|
|
644
|
|
|
def get_report_formats_command(self, kwargs): |
|
|
|
|
645
|
|
|
"""Generates xml string for get report on gvmd.""" |
646
|
|
|
cmd = XmlCommand('get_report_formats') |
647
|
|
|
cmd.set_attributes(kwargs) |
648
|
|
|
return cmd.to_string() |
649
|
|
|
|
650
|
|
|
def get_results_command(self, kwargs): |
|
|
|
|
651
|
|
|
"""Generates xml string for get results on gvmd.""" |
652
|
|
|
cmd = XmlCommand('get_results') |
653
|
|
|
cmd.set_attributes(kwargs) |
654
|
|
|
return cmd.to_string() |
655
|
|
|
|
656
|
|
|
def get_roles_command(self, kwargs): |
|
|
|
|
657
|
|
|
"""Generates xml string for get roles on gvmd.""" |
658
|
|
|
cmd = XmlCommand('get_roles') |
659
|
|
|
cmd.set_attributes(kwargs) |
660
|
|
|
return cmd.to_string() |
661
|
|
|
|
662
|
|
|
def get_scanners_command(self, kwargs): |
|
|
|
|
663
|
|
|
"""Generates xml string for get scanners on gvmd.""" |
664
|
|
|
cmd = XmlCommand('get_scanners') |
665
|
|
|
cmd.set_attributes(kwargs) |
666
|
|
|
return cmd.to_string() |
667
|
|
|
|
668
|
|
|
def get_schedules_command(self, kwargs): |
|
|
|
|
669
|
|
|
"""Generates xml string for get schedules on gvmd.""" |
670
|
|
|
cmd = XmlCommand('get_schedules') |
671
|
|
|
cmd.set_attributes(kwargs) |
672
|
|
|
return cmd.to_string() |
673
|
|
|
|
674
|
|
|
def get_settings_command(self, kwargs): |
|
|
|
|
675
|
|
|
"""Generates xml string for get settings on gvmd.""" |
676
|
|
|
cmd = XmlCommand('get_settings') |
677
|
|
|
cmd.set_attributes(kwargs) |
678
|
|
|
return cmd.to_string() |
679
|
|
|
|
680
|
|
|
def get_system_reports_command(self, kwargs): |
|
|
|
|
681
|
|
|
"""Generates xml string for get system on gvmd.""" |
682
|
|
|
cmd = XmlCommand('get_system') |
683
|
|
|
cmd.set_attributes(kwargs) |
684
|
|
|
return cmd.to_string() |
685
|
|
|
|
686
|
|
|
def get_tags_command(self, kwargs): |
|
|
|
|
687
|
|
|
"""Generates xml string for get tags on gvmd.""" |
688
|
|
|
cmd = XmlCommand('get_tags') |
689
|
|
|
cmd.set_attributes(kwargs) |
690
|
|
|
return cmd.to_string() |
691
|
|
|
|
692
|
|
|
def get_targets_command(self, kwargs): |
|
|
|
|
693
|
|
|
"""Generates xml string for get targets on gvmd.""" |
694
|
|
|
cmd = XmlCommand('get_targets') |
695
|
|
|
cmd.set_attributes(kwargs) |
696
|
|
|
return cmd.to_string() |
697
|
|
|
|
698
|
|
|
def get_tasks_command(self, kwargs): |
|
|
|
|
699
|
|
|
"""Generates xml string for get tasks on gvmd.""" |
700
|
|
|
cmd = XmlCommand('get_tasks') |
701
|
|
|
cmd.set_attributes(kwargs) |
702
|
|
|
return cmd.to_string() |
703
|
|
|
|
704
|
|
|
def get_users_command(self, kwargs): |
|
|
|
|
705
|
|
|
"""Generates xml string for get users on gvmd.""" |
706
|
|
|
cmd = XmlCommand('get_users') |
707
|
|
|
cmd.set_attributes(kwargs) |
708
|
|
|
return cmd.to_string() |
709
|
|
|
|
710
|
|
|
def get_version_command(self): |
|
|
|
|
711
|
|
|
"""Generates xml string for get version on gvmd.""" |
712
|
|
|
cmd = XmlCommand('get_version') |
713
|
|
|
return cmd.to_string() |
714
|
|
|
|
715
|
|
|
def help_command(self, kwargs): |
|
|
|
|
716
|
|
|
"""Generates xml string for help on gvmd.""" |
717
|
|
|
cmd = XmlCommand('help') |
718
|
|
|
cmd.set_attributes(kwargs) |
719
|
|
|
return cmd.to_string() |
720
|
|
|
|
721
|
|
|
def move_task_command(self, task_id, slave_id): |
|
|
|
|
722
|
|
|
"""Generates xml string for move task on gvmd.""" |
723
|
|
|
cmd = XmlCommand('move_task') |
724
|
|
|
cmd.set_attribute('task_id', task_id) |
725
|
|
|
cmd.set_attribute('slave_id', slave_id) |
726
|
|
|
return cmd.to_string() |
727
|
|
|
|
728
|
|
|
def restore_command(self, entity_id): |
|
|
|
|
729
|
|
|
"""Generates xml string for restore on gvmd.""" |
730
|
|
|
cmd = XmlCommand('restore') |
731
|
|
|
cmd.set_attribute('id', entity_id) |
732
|
|
|
return cmd.to_string() |
733
|
|
|
|
734
|
|
|
def resume_task_command(self, task_id): |
|
|
|
|
735
|
|
|
"""Generates xml string for resume task on gvmd.""" |
736
|
|
|
cmd = XmlCommand('resume_task') |
737
|
|
|
cmd.set_attribute('task_id', task_id) |
738
|
|
|
return cmd.to_string() |
739
|
|
|
|
740
|
|
|
def start_task_command(self, task_id): |
|
|
|
|
741
|
|
|
"""Generates xml string for start task on gvmd.""" |
742
|
|
|
cmd = XmlCommand('start_task') |
743
|
|
|
cmd.set_attribute('task_id', task_id) |
744
|
|
|
return cmd.to_string() |
745
|
|
|
|
746
|
|
|
def stop_task_command(self, task_id): |
|
|
|
|
747
|
|
|
"""Generates xml string for stop task on gvmd.""" |
748
|
|
|
cmd = XmlCommand('stop_task') |
749
|
|
|
cmd.set_attribute('task_id', task_id) |
750
|
|
|
return cmd.to_string() |
751
|
|
|
|
752
|
|
|
def sync_cert_command(self): |
|
|
|
|
753
|
|
|
"""Generates xml string for sync cert on gvmd.""" |
754
|
|
|
cmd = XmlCommand('sync_cert') |
755
|
|
|
return cmd.to_string() |
756
|
|
|
|
757
|
|
|
def sync_config_command(self): |
|
|
|
|
758
|
|
|
"""Generates xml string for sync config on gvmd.""" |
759
|
|
|
cmd = XmlCommand('sync_config') |
760
|
|
|
return cmd.to_string() |
761
|
|
|
|
762
|
|
|
def sync_feed_command(self): |
|
|
|
|
763
|
|
|
"""Generates xml string for sync feed on gvmd.""" |
764
|
|
|
cmd = XmlCommand('sync_feed') |
765
|
|
|
return cmd.to_string() |
766
|
|
|
|
767
|
|
|
def sync_scap_command(self): |
|
|
|
|
768
|
|
|
"""Generates xml string for sync scap on gvmd.""" |
769
|
|
|
cmd = XmlCommand('sync_scap') |
770
|
|
|
return cmd.to_string() |
771
|
|
|
|
772
|
|
|
def test_alert_command(self, alert_id): |
|
|
|
|
773
|
|
|
"""Generates xml string for test alert on gvmd.""" |
774
|
|
|
cmd = XmlCommand('test_alert') |
775
|
|
|
cmd.set_attribute('alert_id', alert_id) |
776
|
|
|
return cmd.to_string() |
777
|
|
|
|
778
|
|
|
def verify_agent_command(self, agent_id): |
|
|
|
|
779
|
|
|
"""Generates xml string for verify agent on gvmd.""" |
780
|
|
|
cmd = XmlCommand('verify_agent') |
781
|
|
|
cmd.set_attribute('agent_id', agent_id) |
782
|
|
|
return cmd.to_string() |
783
|
|
|
|
784
|
|
|
def verify_report_format_command(self, report_format_id): |
|
|
|
|
785
|
|
|
"""Generates xml string for verify report format on gvmd.""" |
786
|
|
|
cmd = XmlCommand('verify_report_format') |
787
|
|
|
cmd.set_attribute('report_format_id', report_format_id) |
788
|
|
|
return cmd.to_string() |
789
|
|
|
|
790
|
|
|
def verify_scanner_command(self, scanner_id): |
|
|
|
|
791
|
|
|
"""Generates xml string for verify scanner on gvmd.""" |
792
|
|
|
cmd = XmlCommand('verify_scanner') |
793
|
|
|
cmd.set_attribute('scanner_id', scanner_id) |
794
|
|
|
return cmd.to_string() |
795
|
|
|
|
796
|
|
|
|
797
|
|
|
def pretty_print(xml): |
798
|
|
|
"""Prints beautiful XML-Code |
799
|
|
|
|
800
|
|
|
This function gets an object of list<lxml.etree._Element> |
801
|
|
|
or directly a lxml element. |
802
|
|
|
Print it with good readable format. |
803
|
|
|
|
804
|
|
|
Arguments: |
805
|
|
|
xml: List<lxml.etree.Element> or directly a lxml element |
806
|
|
|
""" |
807
|
|
|
if isinstance(xml, list): |
808
|
|
|
for item in xml: |
809
|
|
|
if etree.iselement(item): |
810
|
|
|
print(etree.tostring(item, pretty_print=True).decode('utf-8')) |
811
|
|
|
else: |
812
|
|
|
print(item) |
813
|
|
|
elif etree.iselement(xml): |
814
|
|
|
print(etree.tostring(xml, pretty_print=True).decode('utf-8')) |
815
|
|
|
|