Completed
Push — master ( e0b5f5...6e63d2 )
by
unknown
12s
created

ospd_openvas.nvticache.get_nvt_family()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
# -*- coding: utf-8 -*-
2
# Description:
3
# Provide functions to handle NVT Info Cache
4
#
5
# Authors:
6
# Juan José Nicola <[email protected]>
7
#
8
# Copyright:
9
# Copyright (C) 2018 Greenbone Networks GmbH
10
#
11
# This program is free software; you can redistribute it and/or
12
# modify it under the terms of the GNU General Public License
13
# as published by the Free Software Foundation; either version 2
14
# of the License, or (at your option) any later version.
15
#
16
# This program is distributed in the hope that it will be useful,
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
# GNU General Public License for more details.
20
#
21
# You should have received a copy of the GNU General Public License
22
# along with this program; if not, write to the Free Software
23
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25
""" Functions related to the NVT information. """
26
27
# Needed to say that when we import ospd, we mean the package and not the
28
# module in that directory.
29
from __future__ import absolute_import
30
from __future__ import print_function
31
32
import xml.etree.ElementTree as ET
33
import ospd_openvas.openvas_db as openvas_db
34
35
36
NVTICACHE_STR = 'nvticache10'
37
38
def get_feed_version():
39
    """ Get feed version.
40
    """
41
    return openvas_db.item_get_single(NVTICACHE_STR)
42
43
def get_oids():
44
    """ Get the list of NVT OIDs.
45
    """
46
    return openvas_db.get_elem_pattern_by_index('filename:*')
47
48 View Code Duplication
def get_nvt_params(oid, str_format=False):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
49
    """ Get NVT's preferences.
50
        @Return XML tree with preferences
51
    """
52
    ctx = openvas_db.get_kb_context()
53
    resp = ctx.smembers('oid:%s:prefs' % oid)
54
    timeout = ctx.lindex('nvt:%s' % oid,
55
                         openvas_db.nvt_meta_fields.index("NVT_TIMEOUT_POS"))
56
    vt_params = ET.Element('vt_params')
57
    if int(timeout) > 0:
58
        vt_param = ET.Element('vt_param')
59
        vt_param.set('id', 'timeout')
60
        vt_param.set('type', 'entry')
61
        xml_name = ET.SubElement(vt_param, 'name')
62
        xml_name.text = "Timeout"
63
        xml_desc =  ET.SubElement(vt_param, 'description')
0 ignored issues
show
Coding Style introduced by
Exactly one space required after assignment
Loading history...
64
        xml_desc.text = "Script Timeout"
65
        xml_def = ET.SubElement(vt_param, 'default')
66
        xml_def.text = timeout
67
        vt_params.append(vt_param)
68
69
    if resp:
70
        for nvt_pref in resp:
71
            elem = nvt_pref.split('|||')
72
            vt_param = ET.Element('vt_param')
73
            vt_param.set('id', elem[0])
74
            vt_param.set('type', elem[1])
75
            xml_name = ET.SubElement(vt_param, 'name')
76
            xml_name.text = elem[0]
77
            if elem[2]:
78
                xml_def = ET.SubElement(vt_param, 'default')
79
                xml_def.text = elem[2]
80
            xml_desc =  ET.SubElement(vt_param, 'description')
0 ignored issues
show
Coding Style introduced by
Exactly one space required after assignment
Loading history...
81
            vt_params.append(vt_param)
82
83
    if str_format:
84
        params_list = vt_params.findall("vt_param")
85
        params = ''
86
        for param in params_list:
87
            params += (ET.tostring(param).decode('utf-8'))
88
        return params
89
90
    return vt_params
91
92 View Code Duplication
def get_nvt_metadata(oid, str_format=False):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
93
    """ Get a full NVT. Returns an XML tree with the NVT metadata.
94
    """
95
    ctx = openvas_db.get_kb_context()
96
97
    resp = ctx.lrange("nvt:%s" % oid,
98
                      openvas_db.nvt_meta_fields.index("NVT_FILENAME_POS"),
99
                      openvas_db.nvt_meta_fields.index("NVT_VERSION_POS"))
100
    if (isinstance(resp, list) and resp) is False:
101
        return None
102
103
    nvt = ET.Element('vt')
104
    nvt.set('id', oid)
105
106
    subelem = ['file_name', 'required_keys', 'mandatory_keys',
107
               'excluded_keys', 'required_udp_ports', 'required_ports',
108
               'dependencies', 'tag', 'cve', 'bid', 'xref', 'category',
109
               'timeout', 'family', 'copyright', 'name', 'version']
110
111
    for elem in subelem:
112
        ET.SubElement(nvt, elem)
113
    for child, res in zip(list(nvt), resp):
114
        child.text = res
115
116
    if str_format:
117
        itera = nvt.iter()
118
        metadata = ''
119
        for elem in itera:
120
            if elem.tag != 'vt' and elem.tag != 'file_name':
121
                metadata += (ET.tostring(elem).decode('utf-8'))
122
        return metadata
123
124
    return nvt
125
126
def get_nvt_name(ctx, oid):
127
    """ Get the NVT name of the given OID."""
128
    return ctx.lindex("nvt:%s" % oid,
129
                      openvas_db.nvt_meta_fields.index("NVT_NAME_POS"))
130
131
def get_nvt_family(ctx, oid):
132
    """ Get the NVT family of the given OID."""
133
    return ctx.lindex("nvt:%s" % oid,
134
                      openvas_db.nvt_meta_fields.index("NVT_FAMILY_POS"))
135