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 getpass |
20
|
|
|
import sys |
21
|
|
|
|
22
|
|
|
from gvm.errors import GvmError |
23
|
|
|
from gvm.xml import pretty_print |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
__all__ = ['authenticate', 'pretty_print', 'run_script'] |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def authenticate(gmp, username=None, password=None): |
30
|
|
|
"""Authentication helper |
31
|
|
|
|
32
|
|
|
Tries to get authentication username and password from arguments and if not |
33
|
|
|
present asks the username and/or password from the terminal. |
34
|
|
|
|
35
|
|
|
Arguments: |
36
|
|
|
gmp: A protocol instance |
37
|
|
|
username (:obj:`str`, optional): Username to authenticate with. If None, |
38
|
|
|
username will be read from terminal. |
39
|
|
|
password (:obj:`str`, optional): Password to authenticate with. If None, |
40
|
|
|
password will be read from the terminal. |
41
|
|
|
|
42
|
|
|
Returns: |
43
|
|
|
tuple: (username, password) tuple |
44
|
|
|
|
45
|
|
|
Raises: |
46
|
|
|
GmpError: Raises GmpError if authentication fails. |
47
|
|
|
""" |
48
|
|
|
if gmp.is_authenticated(): |
49
|
|
|
return |
50
|
|
|
|
51
|
|
|
# Ask for login credentials if none are given. |
52
|
|
|
if not username: |
53
|
|
|
while len(username) == 0: |
54
|
|
|
username = input('Enter username: ') |
55
|
|
|
|
56
|
|
|
if not password: |
57
|
|
|
password = getpass.getpass( |
58
|
|
|
'Enter password for {0}: '.format(username)) |
59
|
|
|
|
60
|
|
|
try: |
61
|
|
|
gmp.authenticate(username, password) |
62
|
|
|
return (username, password,) |
63
|
|
|
except GvmError as e: |
64
|
|
|
print('Could not authenticate. Please check your credentials.') |
65
|
|
|
raise e |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
def run_script(path, global_vars): |
69
|
|
|
"""Loads and executes a file as a python script |
70
|
|
|
|
71
|
|
|
Arguments: |
72
|
|
|
path (str): Path to the script file |
73
|
|
|
vars (dict): Variables passed as globals to the script |
74
|
|
|
""" |
75
|
|
|
try: |
76
|
|
|
file = open(path, 'r', newline='').read() |
77
|
|
|
except FileNotFoundError: |
78
|
|
|
print('Script {path} does not exist'.format(path=path), file=sys.stderr) |
79
|
|
|
sys.exit(2) |
80
|
|
|
|
81
|
|
|
exec(file, global_vars) # pylint: disable=exec-used |
82
|
|
|
|