Completed
Push — master ( 0a5329...7a9b88 )
by Juan José
34s
created

gvm.protocols.gmpv7.Gmp.create_scanner()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 8
dl 0
loc 6
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
# -*- coding: utf-8 -*-
0 ignored issues
show
coding-style introduced by
Too many lines in module (1204/1000)
Loading history...
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
Module for communication with gvmd in Greenbone Management Protocol version 7
20
"""
21
import logging
22
23
from lxml import etree
24
25
from gvm.errors import InvalidArgument, RequiredArgument
26
from gvm.utils import get_version_string
27
from gvm.xml import _GmpCommandFactory as GmpCommandFactory, XmlCommand
28
29
from .base import GvmProtocol
30
31
logger = logging.getLogger(__name__)
32
33
PROTOCOL_VERSION = (7,)
34
35
FILTER_TYPES = [
36
    'agent',
37
    'alert',
38
    'asset',
39
    'config',
40
    'credential',
41
    'filter',
42
    'group',
43
    'note',
44
    'override',
45
    'permission',
46
    'port_list',
47
    'report',
48
    'report_format',
49
    'result',
50
    'role',
51
    'schedule',
52
    'secinfo',
53
    'tag',
54
    'target',
55
    'task',
56
    'user',
57
]
58
59
60
def _check_command_status(xml):
61
    """Check gmp response
62
63
    Look into the gmp response and check for the status in the root element
64
65
    Arguments:
66
        xml {string} -- XML-Source
67
68
    Returns:
69
        bool -- True if valid, otherwise False
70
    """
71
72
    if xml is 0 or xml is None:
73
        logger.error('XML Command is empty')
74
        return False
75
76
    try:
77
        parser = etree.XMLParser(encoding='utf-8', recover=True)
78
79
        root = etree.XML(xml, parser=parser)
80
        status = root.attrib['status']
81
        return status is not None and status[0] == '2'
82
83
    except etree.Error as e:
84
        logger.error('etree.XML(xml): %s', e)
85
        return False
86
87
88
class Gmp(GvmProtocol):
0 ignored issues
show
best-practice introduced by
Too many public methods (112/30)
Loading history...
89
    """Python interface for Greenbone Management Protocol
90
91
    This class implements the `Greenbone Management Protocol version 7`_
92
93
    Attributes:
94
        connection (:class:`gvm.connections.GvmConnection`): Connection to use
95
            to talk with the gvmd daemon. See :mod:`gvm.connections` for
96
            possible connection types.
97
        transform (`callable`_, optional): Optional transform callable to
98
            convert response data. After each request the callable gets passed
99
            the plain response data which can be used to check the data and/or
100
            conversion into different representaitions like a xml dom.
101
102
            See :mod:`gvm.transforms` for existing transforms.
103
104
    .. _Greenbone Management Protocol version 7:
105
        https://docs.greenbone.net/API/GMP/gmp-7.0.html
106
    .. _callable:
107
        https://docs.python.org/3.6/library/functions.html#callable
108
    """
109
110
    def __init__(self, connection, transform=None):
111
        super().__init__(connection, transform)
112
113
        # Is authenticated on gvmd
114
        self._authenticated = False
115
116
        # GMP Message Creator
117
        self._generator = GmpCommandFactory()
118
119
    @staticmethod
120
    def get_protocol_version():
121
        """Allow to determine the Greenbone Management Protocol version.
122
123
            Returns:
124
                str: Implemented version of the Greenbone Management Protocol
125
        """
126
        return get_version_string(PROTOCOL_VERSION)
127
128
    def is_authenticated(self):
129
        """Checks if the user is authenticated
130
131
        If the user is authenticated privilged GMP commands like get_tasks
132
        may be send to gvmd.
133
134
        Returns:
135
            bool: True if an authenticated connection to gvmd has been
136
            established.
137
        """
138
        return self._authenticated
139
140
    def authenticate(self, username, password):
141
        """Authenticate to gvmd.
142
143
        The generated authenticate command will be send to server.
144
        Afterwards the response is read, transformed and returned.
145
146
        Arguments:
147
            username (str): Username
148
            password (str): Password
149
150
        Returns:
151
            any, str by default: Transformed response from server.
152
        """
153
        cmd = XmlCommand('authenticate')
154
155
        if not username:
156
            raise RequiredArgument('authenticate requires username')
157
158
        if not password:
159
            raise RequiredArgument('authenticate requires password')
160
161
        credentials = cmd.add_element('credentials')
162
        credentials.add_element('username', username)
163
        credentials.add_element('password', password)
164
165
        self._send(cmd.to_string())
166
        response = self._read()
167
168
        if _check_command_status(response):
169
            self._authenticated = True
170
171
        return self._transform(response)
172
173
    def create_agent(self, installer, signature, name, comment=None, copy=None,
174
                     howto_install=None, howto_use=None):
175
        """Create a new agent
176
177
        Arguments:
178
            installer (str): A base64 encoded file that installs the agent on a
179
                target machine
180
            signature: (str): A detached OpenPGP signature of the installer
181
            name (str): A name for the agent
182
            comment (str, optional): A comment for the agent
183
            copy (str, optional): UUID of an existing agent to clone from
184
            howto_install (str, optional): A file that describes how to install
185
                the agent
186
            howto_user (str, optional): A file that describes how to use the
187
                agent
188
189
        Returns:
190
            The response. See :py:meth:`send_command` for details.
191
        """
192
        if not name:
193
            raise RequiredArgument('create_agent requires name argument')
194
195
        if not installer:
196
            raise RequiredArgument('create_agent requires installer argument')
197
198
        if not signature:
199
            raise RequiredArgument('create_agent requires signature argument')
200
201
        cmd = XmlCommand('create_agent')
202
        cmd.add_element('installer', installer)
203
        cmd.add_element('signature', signature)
204
        cmd.add_element('name', name)
205
206
        if comment:
207
            cmd.add_element('comment', comment)
208
209
        if copy:
210
            cmd.add_element('copy', copy)
211
212
        if howto_install:
213
            cmd.add_element('howto_install', howto_install)
214
215
        if howto_use:
216
            cmd.add_element('howto_use', howto_use)
217
218
        return self._send_xml_command(cmd)
219
220
    def create_alert(self, name, condition, event, method, method_data=None,
0 ignored issues
show
Comprehensibility introduced by
This function exceeds the maximum number of variables (19/15).
Loading history...
221
                     event_data=None, condition_data=None, filter_id=None,
222
                     copy=None, comment=None):
223
        """Create a new alert
224
225
        Arguments:
226
            name (str): Name of the new Alert
227
            condition (str): The condition that must be satisfied for the alert
228
                to occur.
229
            event (str): The event that must happen for the alert to occur
230
            method (str): The method by which the user is alerted
231
            condition_data (dict, optional): Data that defines the condition
232
            event_data (dict, optional): Data that defines the event
233
            method_data (dict, optional): Data that defines the method
234
            filter_id (str, optional): Filter to apply when executing alert
235
            copy (str, optional): UUID of the alert to clone from
236
            comment (str, optional): Comment for the alert
237
238
        Returns:
239
            The response. See :py:meth:`send_command` for details.
240
        """
241
        if not name:
242
            raise RequiredArgument('create_alert requires name argument')
243
244
        if condition is None or len(condition) == 0:
245
            raise RequiredArgument('create_alert requires condition argument')
246
247
        if event is None or len(event) == 0:
248
            raise RequiredArgument('create_alert requires event argument')
249
250
        cmd = XmlCommand('create_alert')
251
        cmd.add_element('name', name)
252
253
        conditions = cmd.add_element('condition', condition)
254
255
        if not condition_data is None:
256
            for value, key in condition_data.items():
257
                _data = conditions.add_element('data', value)
258
                _data.add_element('name', key)
259
260
        events = cmd.add_element('event', event)
261
262
        if not event_data is None:
263
            for value, key in event_data.items():
264
                _data = events.add_element('data', value)
265
                _data.add_element('name', key)
266
267
        methods = cmd.add_element('method', method)
268
269
        if not method_data is None:
270
            for value, key in method_data.items():
271
                _data = methods.add_element('data', value)
272
                _data.add_element('name', key)
273
274
        if filter_id:
275
            cmd.add_element('filter', attrs={'id': filter_id})
276
277
        if copy:
278
            cmd.add_element('copy', copy)
279
280
        if comment:
281
            cmd.add_element('comment', comment)
282
283
        return self._send_xml_command(cmd)
284
285
    def create_asset(self, name, asset_type, comment=None):
286
        """Create a new asset
287
288
        Arguments:
289
            name (str): Name for the new asset
290
            asset_type (str): Either 'os' or 'host'
291
            comment (str, optional): Comment for the new asset
292
293
        Returns:
294
            The response. See :py:meth:`send_command` for details.
295
        """
296
        if asset_type not in ('host', 'os'):
297
            raise InvalidArgument(
298
                'create_asset requires asset_type to be either host or os')
299
300
        if not name:
301
            raise RequiredArgument('create_asset requires name argument')
302
303
        cmd = XmlCommand('create_asset')
304
        asset = cmd.add_element('asset')
305
        asset.add_element('type', asset_type)
306
        asset.add_element('name', name)
307
308
        if comment:
309
            asset.add_element('comment', comment)
310
311
        return self._send_xml_command(cmd)
312
313
    def create_config(self, name, copy):
314
        """Create a new scan config from an existing one
315
316
        Arguments:
317
            name (str): Name of the new scan config
318
            copy (str): UUID of the existing scan config
319
320
        Returns:
321
            The response. See :py:meth:`send_command` for details.
322
        """
323
        if not name:
324
            raise RequiredArgument('create_config requires name argument')
325
326
        if not copy:
327
            raise RequiredArgument('create_config requires copy argument')
328
329
        cmd = XmlCommand('create_config')
330
        cmd.add_element('copy', copy)
331
        cmd.add_element('name', name)
332
        return self._send_xml_command(cmd)
333
334
    def create_credential(self, name, comment=None, copy=None,
0 ignored issues
show
Comprehensibility introduced by
This function exceeds the maximum number of variables (19/15).
Loading history...
335
                          allow_insecure=False, certificate=None,
336
                          key_phrase=None, private_key=None, login=None,
337
                          password=None, auth_algorithm=None, community=None,
338
                          privacy_algorithm=None, privacy_password=None,
339
                          credential_type=None):
340
        """Create a new credential
341
342
        Arguments:
343
            name (str): Name of the new credential
344
            comment (str, optional): Comment for the credential
345
            copy (str, optional): UUID of credential to clone from
346
            allow_insecure (boolean, optional): Whether to allow insecure use of
347
                the credential
348
            certificate (str, optional): Certificate for the credential
349
            key_phrase (str, optional): Key passphrase for the private key
350
            private_key (str, optional): Private key to use for login
351
            login (str, optional): Username for the credential
352
            password (str, optional): Password for the credential
353
            community (str, optional): The SNMP community
354
            privacy_alogorithm (str, optional): The SNMP privacy algorithm,
355
                either aes or des.
356
            privacy_password (str, optional): The SNMP privacy password
357
            credential_type (str, optionla): The credential type. One of 'cc',
358
                'snmp', 'up', 'usk'
359
360
        Returns:
361
            The response. See :py:meth:`send_command` for details.
362
        """
363
        if not name:
364
            raise RequiredArgument('create_credential requires name argument')
365
366
        cmd = XmlCommand('create_credential')
367
        cmd.add_element('name', name)
368
369
        if comment:
370
            cmd.add_element('comment', comment)
371
372
        if copy:
373
            cmd.add_element('copy', copy)
374
375
        if allow_insecure:
376
            cmd.add_element('allow_insecure', '1')
377
378
        if certificate:
379
            cmd.add_element('certificate', certificate)
380
381
        if not key_phrase is None and private_key:
382
            _xmlkey = cmd.add_element('key')
383
            _xmlkey.add_element('phrase', key_phrase)
384
            _xmlkey.add_element('private', private_key)
385
386
        if login:
387
            cmd.add_element('login', login)
388
389
        if password:
390
            cmd.add_element('password', password)
391
392
        if auth_algorithm:
393
            if auth_algorithm not in ('md5', 'sha1'):
394
                raise InvalidArgument(
395
                    'create_credential requires auth_algorithm to be either '
396
                    'md5 or sha1')
397
            cmd.add_element('auth_algorithm', auth_algorithm)
398
399
        if community:
400
            cmd.add_element('community', community)
401
402
        if privacy_algorithm and privacy_password:
403
            if privacy_algorithm not in ('aes', 'des'):
404
                raise InvalidArgument(
405
                    'create_credential requires algorithm to be either aes or '
406
                    'des')
407
            _xmlprivacy = cmd.add_element('privacy')
408
            _xmlprivacy.add_element('algorithm', privacy_algorithm)
409
            _xmlprivacy.add_element('password', privacy_password)
410
411
        if credential_type:
412
            if credential_type not in ('cc', 'snmp', 'up', 'usk'):
413
                raise InvalidArgument(
414
                    'create_credential requires type to be either cc, snmp, up '
415
                    ' or usk')
416
            cmd.add_element('type', credential_type)
417
418
        return self._send_xml_command(cmd)
419
420
    def create_filter(self, name, make_unique=False, filter_type=None,
421
                      comment=None, term=None, copy=None):
422
        """Create a new filter
423
424
        Arguments:
425
            name (str): Name of the new filter
426
            make_unique (boolean, optional):
427
            filter_type (str, optional): Filter for entity type
428
            comment (str, optional): Comment for the filter
429
            term (str, optional): Filter term e.g. 'name=foo'
430
            copy (str, optional): UUID of an existing filter
431
432
        Returns:
433
            The response. See :py:meth:`send_command` for details.
434
        """
435
        if not name:
436
            raise RequiredArgument('create_filter requires a name argument')
437
438
        cmd = XmlCommand('create_filter')
439
        _xmlname = cmd.add_element('name', name)
440
        if make_unique:
441
            _xmlname.add_element('make_unique', '1')
442
443
        if comment:
444
            cmd.add_element('comment', comment)
445
446
        # TODO: Move copy into an extra method
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
447
        if copy:
448
            cmd.add_element('copy', copy)
449
450
        if term:
451
            cmd.add_element('term', term)
452
453
        if filter_type:
454
            filter_type = filter_type.lower()
455
            if filter_type not in FILTER_TYPES:
456
                raise InvalidArgument(
457
                    'create_filter requires type to be one of {0} but '
458
                    'was {1}'.format(', '.join(FILTER_TYPES), filter_type))
459
            cmd.add_element('type', filter_type)
460
461
        return self._send_xml_command(cmd)
462
463
    def create_group(self, name, comment=None, copy=None, special=False,
464
                     users=None):
465
        """Create a new group
466
467
        Arguments:
468
            name (str): Name of the new group
469
            comment (str, optional): Comment for the group
470
            copy (str, optional): UUID of group to clone from
471
            special (boolean, optional): Create permission giving members full
472
                access to each other's entities
473
            users (list, optional): List of user names to be in the group
474
475
        Returns:
476
            The response. See :py:meth:`send_command` for details.
477
        """
478
        if not name:
479
            raise RequiredArgument('create_group requires a name argument')
480
481
        cmd = XmlCommand('create_group')
482
        cmd.add_element('name', name)
483
484
        if comment:
485
            cmd.add_element('comment', comment)
486
487
        if copy:
488
            cmd.add_element('copy', copy)
489
490
        if special:
491
            _xmlspecial = cmd.add_element('specials')
492
            _xmlspecial.add_element('full')
493
494
        if users:
495
            cmd.add_element('users', ', '.join(users))
496
497
        return self._send_xml_command(cmd)
498
499
    def create_note(self, text, nvt_oid, active=None, comment=None, copy=None,
500
                    hosts=None, result_id=None, severity=None, task_id=None,
501
                    threat=None, port=None):
502
        """Create a new note
503
504
        Arguments:
505
            text (str): Text of the new note
506
            nvt_id (str): OID of the nvt to which note applies
507
            active (int, optional): Seconds note will be active. -1 on
508
                always, 0 off
509
            comment (str, optional): Comment for the note
510
            copy (str, optional): UUID of existing note to clone from
511
            hosts (str, optional): A textual list of hosts
512
            port (str, optional): Port ot which the note applies
513
            result_id (str, optional): UUID of a result to which note applies
514
            severity (decimal, optional): Severity to which note applies
515
            task_id (str, optional): UUID of task to which note applies
516
            threat (str, optional): Threat level to which note applies. Will be
517
                converted to severity
518
519
        Returns:
520
            The response. See :py:meth:`send_command` for details.
521
        """
522
        if not text:
523
            raise RequiredArgument('create_note requires a text argument')
524
525
        if not nvt_oid:
526
            raise RequiredArgument('create_note requires a nvt_oid argument')
527
528
        cmd = XmlCommand('create_note')
529
        cmd.add_element('text', text)
530
        cmd.add_element('nvt', attrs={"oid": nvt_oid})
531
532
        if not active is None:
533
            cmd.add_element('active', str(active))
534
535
        if comment:
536
            cmd.add_element('comment', comment)
537
538
        if copy:
539
            cmd.add_element('copy', copy)
540
541
        if hosts:
542
            cmd.add_element('hosts', hosts)
543
544
        if port:
545
            cmd.add_element('port', port)
546
547
        if result_id:
548
            cmd.add_element('result', attrs={'id': result_id})
549
550
        if severity:
551
            cmd.add_element('severity', severity)
552
553
        if task_id:
554
            cmd.add_element('task', attrs={'id': task_id})
555
556
        if threat:
557
            cmd.add_element('threat', threat)
558
559
        return self._send_xml_command(cmd)
560
561
    def create_override(self, text, nvt_oid, active=None, copy=None, hosts=None,
0 ignored issues
show
Comprehensibility introduced by
This function exceeds the maximum number of variables (16/15).
Loading history...
562
                        port=None, result_id=None, severity=None, comment=None,
563
                        new_severity=None, task_id=None, threat=None,
564
                        new_threat=None):
565
        """Create a new override
566
567
        Arguments:
568
            text (str): Text of the new override
569
            nvt_id (str): OID of the nvt to which override applies
570
            active (int, optional): Seconds override will be active. -1 on
571
                always, 0 off
572
            comment (str, optional): Comment for the override
573
            copy (str, optional): UUID of existing override to clone from
574
            hosts (str, optional): A textual list of hosts
575
            port (str, optional): Port ot which the override applies
576
            result_id (str, optional): UUID of a result to which override
577
                applies
578
            severity (decimal, optional): Severity to which override applies
579
            new_severity (decimal, optional): New severity for result
580
            task_id (str, optional): UUID of task to which override applies
581
            threat (str, optional): Threat level to which override applies. Will
582
                be converted to severity
583
            new_threat (str, optional): New threat level for result, will be
584
                converted to a new_severity
585
586
        Returns:
587
            The response. See :py:meth:`send_command` for details.
588
        """
589
        if not text:
590
            raise RequiredArgument('create_override requires a text argument')
591
592
        if not nvt_oid:
593
            raise RequiredArgument('create_override requires a nvt_oid '
594
                                   'argument')
595
596
        cmd = XmlCommand('create_override')
597
        cmd.add_element('text', text)
598
        cmd.add_element('nvt', attrs={'oid': nvt_oid})
599
600
        if active:
601
            cmd.add_element('active', active)
602
603
        if comment:
604
            cmd.add_element('comment', comment)
605
606
        if copy:
607
            cmd.add_element('copy', copy)
608
609
        if hosts:
610
            cmd.add_element('hosts', hosts)
611
612
        if port:
613
            cmd.add_element('port', port)
614
615
        if result_id:
616
            cmd.add_element('result', attrs={'id': result_id})
617
618
        if severity:
619
            cmd.add_element('severity', severity)
620
621
        if new_severity:
622
            cmd.add_element('new_severity', new_severity)
623
624
        if task_id:
625
            cmd.add_element('task', attrs={'id': task_id})
626
627
        if threat:
628
            cmd.add_element('threat', threat)
629
630
        if new_threat:
631
            cmd.add_element('new_threat', new_threat)
632
633
        return self._send_xml_command(cmd)
634
635
    def create_permission(self, name, subject_id, subject_type,
636
                          resource_id=None, resource_type=None, copy=None,
637
                          comment=None):
638
        """Create a new permission
639
640
        Arguments:
641
            name (str): Name of the new permission
642
            subject_id (str): UUID of subject to whom the permission is granted
643
            subject_type (str): Type of the subject user, group or role
644
            comment (str, optional): Comment for the permission
645
            resource_id (str, optional): UUID of entity to which the permission
646
                applies
647
            resource_type (str, optional): Type of the resource. For Super
648
                permissions user, group or role
649
650
        Returns:
651
            The response. See :py:meth:`send_command` for details.
652
        """
653
        if not name:
654
            raise RequiredArgument('create_permission requires a name argument')
655
656
        if not subject_id:
657
            raise RequiredArgument(
658
                'create_permission requires a subject_id argument')
659
660
        if subject_type not in ('user', 'group', 'role'):
661
            raise InvalidArgument(
662
                'create_permission requires subject_type to be either user, '
663
                'group or role')
664
665
        cmd = XmlCommand('create_permission')
666
        cmd.add_element('name', name)
667
668
        _xmlsubject = cmd.add_element('subject', attrs={'id': subject_id})
669
        _xmlsubject.add_element('type', type)
670
671
        if comment:
672
            cmd.add_element('comment', comment)
673
674
        if copy:
675
            cmd.add_element('copy', copy)
676
677
        if resource_id and resource_type:
678
            _xmlresource = cmd.add_element('resource',
679
                                           attrs={'id': resource_id})
680
            _xmlresource.add_element('type', resource_type)
681
682
683
        return self._send_xml_command(cmd)
684
685
    def create_port_list(self, name, port_range, **kwargs):
686
        cmd = self._generator.create_port_list_command(name, port_range, kwargs)
687
        return self.send_command(cmd)
688
689
    def create_port_range(self, port_list_id, start, end, port_range_type,
690
                          comment=''):
691
        cmd = self._generator.create_port_range_command(
692
            port_list_id, start, end, port_range_type, comment)
693
        return self.send_command(cmd)
694
695
    def create_report(self, report_xml_string, **kwargs):
696
        cmd = self._generator.create_report_command(report_xml_string, kwargs)
697
        return self.send_command(cmd)
698
699
    def create_role(self, name, **kwargs):
700
        cmd = self._generator.create_role_command(name, kwargs)
701
        return self.send_command(cmd)
702
703
    def create_scanner(self, name, host, port, scanner_type, ca_pub,
704
                       credential_id, **kwargs):
705
        cmd = self._generator.create_scanner_command(name, host, port,
706
                                                     scanner_type, ca_pub,
707
                                                     credential_id, kwargs)
708
        return self.send_command(cmd)
709
710
    def create_schedule(self, name, **kwargs):
711
        cmd = self._generator.create_schedule_command(name, kwargs)
712
        return self.send_command(cmd)
713
714
    def create_tag(self, name, resource_id, resource_type, **kwargs):
715
        cmd = self._generator.create_tag_command(name, resource_id,
716
                                                 resource_type, kwargs)
717
        return self.send_command(cmd)
718
719
    def create_target(self, name, make_unique=False, asset_hosts_filter=None,
0 ignored issues
show
best-practice introduced by
Too many arguments (18/15)
Loading history...
Comprehensibility introduced by
This function exceeds the maximum number of variables (22/15).
Loading history...
720
                      hosts=None, comment=None, copy=None, exclude_hosts=None,
721
                      ssh_credential_id=None, ssh_credential_port=None,
722
                      smb_credential_id=None, esxi_credential_id=None,
723
                      snmp_credential_id=None, alive_tests=None,
724
                      reverse_lookup_only=None, reverse_lookup_unify=None,
725
                      port_range=None, port_list_id=None):
726
        """Create a new target
727
728
        Arguments:
729
            name (str): Name of the target
730
            make_unique (boolean, optional): Append a unique suffix if the name
731
                already exists
732
            asset_hosts_filter (str, optional): Filter to select target host
733
                from assets hosts
734
            hosts (str, optional): Hosts to scan
735
            exclude_hosts (str, optional): Hosts to exclude from scan
736
            comment (str, optional): Comment for the target
737
            copy (str, optional): UUID of an existing target to clone from
738
            ssh_credential_id (str, optional): UUID of a ssh credential to use
739
                on target
740
            ssh_credential_port (str, optional): The port to use for ssh
741
                credential
742
            smb_credential_id (str, optional): UUID of a smb credential to use
743
                on target
744
            snmp_credential_id (str, optional): UUID of a snmp credential to use
745
                on target
746
            esxi_credential_id (str, optional): UUID of a esxi credential to use
747
                on target
748
            alive_tests (str, optional): Which alive tests to use
749
            reverse_lookup_only (boolean, optional): Whether to scan only hosts
750
                that have names
751
            reverse_lookup_unify (boolean, optional): Whether to scan only one
752
                IP when multiple IPs have the same name.
753
            port_range (str, optional): Port range for the target
754
            port_list_id (str, optional): UUID of the port list to use on target
755
756
        Returns:
757
            The response. See :py:meth:`send_command` for details.
758
        """
759
        if not name:
760
            raise RequiredArgument('create_target requires a name argument')
761
762
        cmd = XmlCommand('create_target')
763
        _xmlname = cmd.add_element('name', name)
764
        if make_unique:
765
            _xmlname.add_element('make_unique', '1')
766
767
        if asset_hosts_filter:
768
            cmd.add_element('asset_hosts',
769
                            attrs={'filter': str(asset_hosts_filter)})
770
        elif hosts:
771
            cmd.add_element('hosts', hosts)
772
        else:
773
            raise RequiredArgument('create_target requires either a hosts or '
774
                                   'an asset_hosts_filter argument')
775
776
        if comment:
777
            cmd.add_element('comment', comment)
778
779
        if copy:
780
            # TODO move copy case into clone_target method
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
781
782
            # NOTE: It seems that hosts/asset_hosts is silently ignored by the
783
            # server when copy is supplied. But for specification conformance
784
            # we raise the ValueError above and consider copy optional.
785
            cmd.add_element('copy', copy)
786
787
        if exclude_hosts:
788
            cmd.add_element('exclude_hosts', exclude_hosts)
789
790
        if ssh_credential_id:
791
            _xmlssh = cmd.add_element('ssh_credential',
792
                                      attrs={'id': ssh_credential_id})
793
            if ssh_credential_port:
794
                _xmlssh.add_element('port', ssh_credential_port)
795
796
        if smb_credential_id:
797
            cmd.add_element('smb_credential', attrs={'id': smb_credential_id})
798
799
        if esxi_credential_id:
800
            cmd.add_element('esxi_credential', attrs={'id': esxi_credential_id})
801
802
        if snmp_credential_id:
803
            cmd.add_element('snmp_credential', attrs={'id': snmp_credential_id})
804
805
        if alive_tests:
806
            cmd.add_element('alive_tests', alive_tests)
807
808
        if not reverse_lookup_only is None:
809
            if reverse_lookup_only:
810
                cmd.add_element('reverse_lookup_only', '1')
811
            else:
812
                cmd.add_element('reverse_lookup_only', '0')
813
814
        if not reverse_lookup_unify is None:
815
            if reverse_lookup_unify:
816
                cmd.add_element('reverse_lookup_unify', '1')
817
            else:
818
                cmd.add_element('reverse_lookup_unify', '0')
819
820
        if port_range:
821
            cmd.add_element('port_range', port_range)
822
823
        if port_list_id:
824
            cmd.add_element('port_list', attrs={'id': port_list_id})
825
826
        return self._send_xml_command(cmd)
827
828
    def create_task(self, name, config_id, target_id, scanner_id,
829
                    alert_ids=None, comment=''):
830
        if alert_ids is None:
831
            alert_ids = []
832
        cmd = self._generator.create_task_command(
833
            name, config_id, target_id, scanner_id, alert_ids, comment)
834
        return self.send_command(cmd)
835
836
    def create_user(self, name, password, copy='', hosts_allow='0',
837
                    ifaces_allow='0', role_ids=(), hosts=None, ifaces=None):
838
        cmd = self._generator.create_user_command(
839
            name, password, copy, hosts_allow, ifaces_allow, role_ids, hosts,
840
            ifaces)
841
        return self.send_command(cmd)
842
843
    def delete_agent(self, **kwargs):
844
        cmd = self._generator.delete_agent_command(kwargs)
845
        return self.send_command(cmd)
846
847
    def delete_alert(self, **kwargs):
848
        cmd = self._generator.delete_alert_command(kwargs)
849
        return self.send_command(cmd)
850
851
    def delete_asset(self, asset_id, ultimate=0):
852
        cmd = self._generator.delete_asset_command(asset_id, ultimate)
853
        return self.send_command(cmd)
854
855
    def delete_config(self, config_id, ultimate=0):
856
        cmd = self._generator.delete_config_command(config_id, ultimate)
857
        return self.send_command(cmd)
858
859
    def delete_credential(self, credential_id, ultimate=0):
860
        cmd = self._generator.delete_credential_command(credential_id, ultimate)
861
        return self.send_command(cmd)
862
863
    def delete_filter(self, filter_id, ultimate=0):
864
        cmd = self._generator.delete_filter_command(filter_id, ultimate)
865
        return self.send_command(cmd)
866
867
    def delete_group(self, group_id, ultimate=0):
868
        cmd = self._generator.delete_group_command(group_id, ultimate)
869
        return self.send_command(cmd)
870
871
    def delete_note(self, note_id, ultimate=0):
872
        cmd = self._generator.delete_note_command(note_id, ultimate)
873
        return self.send_command(cmd)
874
875
    def delete_override(self, override_id, ultimate=0):
876
        cmd = self._generator.delete_override_command(override_id, ultimate)
877
        return self.send_command(cmd)
878
879
    def delete_permission(self, permission_id, ultimate=0):
880
        cmd = self._generator.delete_permission_command(permission_id, ultimate)
881
        return self.send_command(cmd)
882
883
    def delete_port_list(self, port_list_id, ultimate=0):
884
        cmd = self._generator.delete_port_list_command(port_list_id, ultimate)
885
        return self.send_command(cmd)
886
887
    def delete_port_range(self, port_range_id):
888
        cmd = self._generator.delete_port_range_command(port_range_id)
889
        return self.send_command(cmd)
890
891
    def delete_report(self, report_id):
892
        cmd = self._generator.delete_report_command(report_id)
893
        return self.send_command(cmd)
894
895
    def delete_report_format(self, report_format_id, ultimate=0):
896
        cmd = self._generator.delete_report_format_command(
897
            report_format_id, ultimate)
898
        return self.send_command(cmd)
899
900
    def delete_role(self, role_id, ultimate=0):
901
        cmd = self._generator.delete_role_command(role_id, ultimate)
902
        return self.send_command(cmd)
903
904
    def delete_scanner(self, scanner_id, ultimate=0):
905
        cmd = self._generator.delete_scanner_command(scanner_id, ultimate)
906
        return self.send_command(cmd)
907
908
    def delete_schedule(self, schedule_id, ultimate=0):
909
        cmd = self._generator.delete_schedule_command(schedule_id, ultimate)
910
        return self.send_command(cmd)
911
912
    def delete_tag(self, tag_id, ultimate=0):
913
        cmd = self._generator.delete_tag_command(tag_id, ultimate)
914
        return self.send_command(cmd)
915
916
    def delete_target(self, target_id, ultimate=0):
917
        cmd = self._generator.delete_target_command(target_id, ultimate)
918
        return self.send_command(cmd)
919
920
    def delete_task(self, task_id, ultimate=0):
921
        cmd = self._generator.delete_task_command(task_id, ultimate)
922
        return self.send_command(cmd)
923
924
    def delete_user(self, **kwargs):
925
        cmd = self._generator.delete_user_command(kwargs)
926
        return self.send_command(cmd)
927
928
    def describe_auth(self):
929
        cmd = self._generator.describe_auth_command()
930
        return self.send_command(cmd)
931
932
    def empty_trashcan(self):
933
        cmd = self._generator.empty_trashcan_command()
934
        return self.send_command(cmd)
935
936
    def get_agents(self, **kwargs):
937
        cmd = self._generator.get_agents_command(kwargs)
938
        return self.send_command(cmd)
939
940
    def get_aggregates(self, **kwargs):
941
        cmd = self._generator.get_aggregates_command(kwargs)
942
        return self.send_command(cmd)
943
944
    def get_alerts(self, **kwargs):
945
        cmd = self._generator.get_alerts_command(kwargs)
946
        return self.send_command(cmd)
947
948
    def get_assets(self, **kwargs):
949
        cmd = self._generator.get_assets_command(kwargs)
950
        return self.send_command(cmd)
951
952
    def get_credentials(self, **kwargs):
953
        cmd = self._generator.get_credentials_command(kwargs)
954
        return self.send_command(cmd)
955
956
    def get_configs(self, **kwargs):
957
        cmd = self._generator.get_configs_command(kwargs)
958
        return self.send_command(cmd)
959
960
    def get_feeds(self, **kwargs):
961
        cmd = self._generator.get_feeds_command(kwargs)
962
        return self.send_command(cmd)
963
964
    def get_filters(self, **kwargs):
965
        cmd = self._generator.get_filters_command(kwargs)
966
        return self.send_command(cmd)
967
968
    def get_groups(self, **kwargs):
969
        cmd = self._generator.get_groups_command(kwargs)
970
        return self.send_command(cmd)
971
972
    def get_info(self, **kwargs):
973
        cmd = self._generator.get_info_command(kwargs)
974
        return self.send_command(cmd)
975
976
    def get_notes(self, **kwargs):
977
        cmd = self._generator.get_notes_command(kwargs)
978
        return self.send_command(cmd)
979
980
    def get_nvts(self, **kwargs):
981
        cmd = self._generator.get_nvts_command(kwargs)
982
        return self.send_command(cmd)
983
984
    def get_nvt_families(self, **kwargs):
985
        cmd = self._generator.get_nvt_families_command(kwargs)
986
        return self.send_command(cmd)
987
988
    def get_overrides(self, **kwargs):
989
        cmd = self._generator.get_overrides_command(kwargs)
990
        return self.send_command(cmd)
991
992
    def get_permissions(self, **kwargs):
993
        cmd = self._generator.get_permissions_command(kwargs)
994
        return self.send_command(cmd)
995
996
    def get_port_lists(self, **kwargs):
997
        cmd = self._generator.get_port_lists_command(kwargs)
998
        return self.send_command(cmd)
999
1000
    def get_preferences(self, **kwargs):
1001
        cmd = self._generator.get_preferences_command(kwargs)
1002
        return self.send_command(cmd)
1003
1004
    def get_reports(self, **kwargs):
1005
        cmd = self._generator.get_reports_command(kwargs)
1006
        return self.send_command(cmd)
1007
1008
    def get_report_formats(self, **kwargs):
1009
        cmd = self._generator.get_report_formats_command(kwargs)
1010
        return self.send_command(cmd)
1011
1012
    def get_results(self, **kwargs):
1013
        cmd = self._generator.get_results_command(kwargs)
1014
        return self.send_command(cmd)
1015
1016
    def get_roles(self, **kwargs):
1017
        cmd = self._generator.get_roles_command(kwargs)
1018
        return self.send_command(cmd)
1019
1020
    def get_scanners(self, **kwargs):
1021
        cmd = self._generator.get_scanners_command(kwargs)
1022
        return self.send_command(cmd)
1023
1024
    def get_schedules(self, **kwargs):
1025
        cmd = self._generator.get_schedules_command(kwargs)
1026
        return self.send_command(cmd)
1027
1028
    def get_settings(self, **kwargs):
1029
        cmd = self._generator.get_settings_command(kwargs)
1030
        return self.send_command(cmd)
1031
1032
    def get_system_reports(self, **kwargs):
1033
        cmd = self._generator.get_system_reports_command(kwargs)
1034
        return self.send_command(cmd)
1035
1036
    def get_tags(self, **kwargs):
1037
        cmd = self._generator.get_tags_command(kwargs)
1038
        return self.send_command(cmd)
1039
1040
    def get_targets(self, **kwargs):
1041
        cmd = self._generator.get_targets_command(kwargs)
1042
        return self.send_command(cmd)
1043
1044
    def get_tasks(self, **kwargs):
1045
        cmd = self._generator.get_tasks_command(kwargs)
1046
        return self.send_command(cmd)
1047
1048
    def get_users(self, **kwargs):
1049
        cmd = self._generator.get_users_command(kwargs)
1050
        return self.send_command(cmd)
1051
1052
    def get_version(self):
1053
        cmd = self._generator.get_version_command()
1054
        return self.send_command(cmd)
1055
1056
    def help(self, **kwargs):
1057
        cmd = self._generator.help_command(kwargs)
1058
        return self.send_command(cmd)
1059
1060
    def modify_agent(self, agent_id, name='', comment=''):
1061
        cmd = self._generator.modify_agent_command(agent_id, name, comment)
1062
        return self.send_command(cmd)
1063
1064
    def modify_alert(self, alert_id, **kwargs):
1065
        cmd = self._generator.modify_alert_command(alert_id, kwargs)
1066
        return self.send_command(cmd)
1067
1068
    def modify_asset(self, asset_id, comment):
1069
        cmd = self._generator.modify_asset_command(asset_id, comment)
1070
        return self.send_command(cmd)
1071
1072
    def modify_auth(self, group_name, auth_conf_settings):
1073
        cmd = self._generator.modify_auth_command(group_name,
1074
                                                  auth_conf_settings)
1075
        return self.send_command(cmd)
1076
1077
    def modify_config(self, selection, **kwargs):
1078
        cmd = self._generator.modify_config_command(selection, kwargs)
1079
        return self.send_command(cmd)
1080
1081
    def modify_credential(self, credential_id, **kwargs):
1082
        cmd = self._generator.modify_credential_command(
1083
            credential_id, kwargs)
1084
        return self.send_command(cmd)
1085
1086
    def modify_filter(self, filter_id, **kwargs):
1087
        cmd = self._generator.modify_filter_command(filter_id, kwargs)
1088
        return self.send_command(cmd)
1089
1090
    def modify_group(self, group_id, **kwargs):
1091
        cmd = self._generator.modify_group_command(group_id, kwargs)
1092
        return self.send_command(cmd)
1093
1094
    def modify_note(self, note_id, text, **kwargs):
1095
        cmd = self._generator.modify_note_command(note_id, text, kwargs)
1096
        return self.send_command(cmd)
1097
1098
    def modify_override(self, override_id, text, **kwargs):
1099
        cmd = self._generator.modify_override_command(override_id, text,
1100
                                                      kwargs)
1101
        return self.send_command(cmd)
1102
1103
    def modify_permission(self, permission_id, **kwargs):
1104
        cmd = self._generator.modify_permission_command(
1105
            permission_id, kwargs)
1106
        return self.send_command(cmd)
1107
1108
    def modify_port_list(self, port_list_id, **kwargs):
1109
        cmd = self._generator.modify_port_list_command(port_list_id, kwargs)
1110
        return self.send_command(cmd)
1111
1112
    def modify_report(self, report_id, comment):
1113
        cmd = self._generator.modify_report_format_command(report_id, comment)
1114
        return self.send_command(cmd)
1115
1116
    def modify_report_format(self, report_format_id, **kwargs):
1117
        cmd = self._generator.modify_report_format_command(report_format_id,
1118
                                                           kwargs)
1119
        return self.send_command(cmd)
1120
1121
    def modify_role(self, role_id, **kwargs):
1122
        cmd = self._generator.modify_role_command(role_id, kwargs)
1123
        return self.send_command(cmd)
1124
1125
    def modify_scanner(self, scanner_id, host, port, scanner_type, **kwargs):
1126
        cmd = self._generator.modify_scanner_command(scanner_id, host, port,
1127
                                                     scanner_type, kwargs)
1128
        return self.send_command(cmd)
1129
1130
    def modify_schedule(self, schedule_id, **kwargs):
1131
        cmd = self._generator.modify_schedule_command(schedule_id, kwargs)
1132
        return self.send_command(cmd)
1133
1134
    def modify_setting(self, setting_id, name, value):
1135
        cmd = self._generator.modify_setting_command(setting_id, name, value)
1136
        return self.send_command(cmd)
1137
1138
    def modify_tag(self, tag_id, **kwargs):
1139
        cmd = self._generator.modify_tag_command(tag_id, kwargs)
1140
        return self.send_command(cmd)
1141
1142
    def modify_target(self, target_id, **kwargs):
1143
        cmd = self._generator.modify_target_command(target_id, kwargs)
1144
        return self.send_command(cmd)
1145
1146
    def modify_task(self, task_id, **kwargs):
1147
        cmd = self._generator.modify_task_command(task_id, kwargs)
1148
        return self.send_command(cmd)
1149
1150
    def modify_user(self, **kwargs):
1151
        cmd = self._generator.modify_user_command(kwargs)
1152
        return self.send_command(cmd)
1153
1154
    def move_task(self, task_id, slave_id):
1155
        cmd = self._generator.move_task_command(task_id, slave_id)
1156
        return self.send_command(cmd)
1157
1158
    def restore(self, entity_id):
1159
        cmd = self._generator.restore_command(entity_id)
1160
        return self.send_command(cmd)
1161
1162
    def resume_task(self, task_id):
1163
        cmd = self._generator.resume_task_command(task_id)
1164
        return self.send_command(cmd)
1165
1166
    def start_task(self, task_id):
1167
        cmd = self._generator.start_task_command(task_id)
1168
        return self.send_command(cmd)
1169
1170
    def stop_task(self, task_id):
1171
        cmd = self._generator.stop_task_command(task_id)
1172
        return self.send_command(cmd)
1173
1174
    def sync_cert(self):
1175
        cmd = self._generator.sync_cert_command()
1176
        return self.send_command(cmd)
1177
1178
    def sync_config(self):
1179
        cmd = self._generator.sync_config_command()
1180
        return self.send_command(cmd)
1181
1182
    def sync_feed(self):
1183
        cmd = self._generator.sync_feed_command()
1184
        return self.send_command(cmd)
1185
1186
    def sync_scap(self):
1187
        cmd = self._generator.sync_scap_command()
1188
        return self.send_command(cmd)
1189
1190
    def test_alert(self, alert_id):
1191
        cmd = self._generator.test_alert_command(alert_id)
1192
        return self.send_command(cmd)
1193
1194
    def verify_agent(self, agent_id):
1195
        cmd = self._generator.verify_agent_command(agent_id)
1196
        return self.send_command(cmd)
1197
1198
    def verify_report_format(self, report_format_id):
1199
        cmd = self._generator.verify_report_format_command(report_format_id)
1200
        return self.send_command(cmd)
1201
1202
    def verify_scanner(self, scanner_id):
1203
        cmd = self._generator.verify_scanner_command(scanner_id)
1204
        return self.send_command(cmd)
1205