Passed
Pull Request — master (#183)
by
unknown
01:41 queued 31s
created

gvmtools.helper.do_not_run_as_root()   A

Complexity

Conditions 2

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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