1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2018-2021 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 os |
20
|
|
|
import sys |
21
|
|
|
|
22
|
|
|
from argparse import Namespace |
23
|
|
|
|
24
|
|
|
from gvm import get_version as get_gvm_version |
25
|
|
|
from gvm.protocols.gmp import Gmp |
26
|
|
|
from gvm.protocols.latest import Osp |
27
|
|
|
from gvm.transforms import EtreeCheckCommandTransform |
28
|
|
|
|
29
|
|
|
from gvmtools import get_version |
30
|
|
|
from gvmtools.helper import authenticate, run_script, do_not_run_as_root |
31
|
|
|
from gvmtools.parser import ( |
32
|
|
|
create_parser, |
33
|
|
|
create_connection, |
34
|
|
|
PROTOCOL_OSP, |
35
|
|
|
PROTOCOL_GMP, |
36
|
|
|
) |
37
|
|
|
|
38
|
|
|
HELP_TEXT = """ |
39
|
|
|
Command line tool to execute custom GMP (Greenbone Management |
40
|
|
|
Protocol) and OSP (Open Scanner Protocol) scripts. |
41
|
|
|
|
42
|
|
|
The protocol specifications for GMP and OSP are available at: |
43
|
|
|
https://docs.greenbone.net/index.html#api_documentation |
44
|
|
|
""" |
45
|
|
|
|
46
|
|
|
__version__ = get_version() |
47
|
|
|
__api_version__ = get_gvm_version() |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
def main(): |
51
|
|
|
do_not_run_as_root() |
52
|
|
|
|
53
|
|
|
parser = create_parser(description=HELP_TEXT, logfilename='gvm-script.log') |
54
|
|
|
|
55
|
|
|
parser.add_protocol_argument() |
56
|
|
|
|
57
|
|
|
parser.add_argument( |
58
|
|
|
'scriptname', |
59
|
|
|
metavar="SCRIPT", |
60
|
|
|
help='Path to script to be executed (example: myscript.gmp.py)', |
61
|
|
|
) |
62
|
|
|
parser.add_argument( |
63
|
|
|
'scriptargs', nargs='*', metavar="ARG", help='Arguments for the script' |
64
|
|
|
) |
65
|
|
|
args, script_args = parser.parse_known_args() |
66
|
|
|
|
67
|
|
|
connection = create_connection(**vars(args)) |
68
|
|
|
|
69
|
|
|
transform = EtreeCheckCommandTransform() |
70
|
|
|
|
71
|
|
|
global_vars = { |
72
|
|
|
'__version__': __version__, |
73
|
|
|
'__api_version__': __api_version__, |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
username = None |
77
|
|
|
password = None |
78
|
|
|
|
79
|
|
|
if args.protocol == PROTOCOL_OSP: |
80
|
|
|
protocol_class = Osp |
81
|
|
|
name = 'osp' |
82
|
|
|
else: |
83
|
|
|
protocol_class = Gmp |
84
|
|
|
name = 'gmp' |
85
|
|
|
|
86
|
|
|
try: |
87
|
|
|
with protocol_class(connection, transform=transform) as protocol: |
88
|
|
|
global_vars[name] = protocol |
89
|
|
|
global_vars['__name__'] = '__{}__'.format(name) |
90
|
|
|
|
91
|
|
|
if args.protocol == PROTOCOL_GMP: |
92
|
|
|
if args.gmp_username: |
93
|
|
|
(username, password) = authenticate( |
94
|
|
|
protocol, |
95
|
|
|
username=args.gmp_username, |
96
|
|
|
password=args.gmp_password, |
97
|
|
|
) |
98
|
|
|
|
99
|
|
|
argv = [os.path.abspath(args.scriptname), *args.scriptargs] |
100
|
|
|
|
101
|
|
|
shell_args = Namespace( |
102
|
|
|
username=username, |
103
|
|
|
password=password, |
104
|
|
|
argv=argv, |
105
|
|
|
# for backwards compatibility we add script here |
106
|
|
|
script=argv, |
107
|
|
|
# the unknown args, which are owned by the script. |
108
|
|
|
script_args=script_args, |
109
|
|
|
) |
110
|
|
|
|
111
|
|
|
global_vars['args'] = shell_args |
112
|
|
|
|
113
|
|
|
run_script(args.scriptname, global_vars) |
114
|
|
|
|
115
|
|
|
except Exception as e: # pylint: disable=broad-except |
116
|
|
|
print(e, file=sys.stderr) |
117
|
|
|
sys.exit(1) |
118
|
|
|
|
119
|
|
|
sys.exit(0) |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
if __name__ == '__main__': |
123
|
|
|
main() |
124
|
|
|
|