|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# Copyright (C) 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
|
|
|
from typing import Any |
|
20
|
|
|
|
|
21
|
|
|
from gvm.errors import RequiredArgument |
|
22
|
|
|
from gvm.utils import check_command_status |
|
23
|
|
|
from gvm.xml import XmlCommand |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
class AuthenticationMixin: |
|
27
|
|
|
def is_authenticated(self) -> bool: |
|
28
|
|
|
"""Checks if the user is authenticated |
|
29
|
|
|
|
|
30
|
|
|
If the user is authenticated privileged GMP commands like get_tasks |
|
31
|
|
|
may be send to gvmd. |
|
32
|
|
|
|
|
33
|
|
|
Returns: |
|
34
|
|
|
bool: True if an authenticated connection to gvmd has been |
|
35
|
|
|
established. |
|
36
|
|
|
""" |
|
37
|
|
|
return self._authenticated |
|
38
|
|
|
|
|
39
|
|
|
def authenticate(self, username: str, password: str) -> Any: |
|
40
|
|
|
"""Authenticate to gvmd. |
|
41
|
|
|
|
|
42
|
|
|
The generated authenticate command will be send to server. |
|
43
|
|
|
Afterwards the response is read, transformed and returned. |
|
44
|
|
|
|
|
45
|
|
|
Arguments: |
|
46
|
|
|
username: Username |
|
47
|
|
|
password: Password |
|
48
|
|
|
|
|
49
|
|
|
Returns: |
|
50
|
|
|
Transformed response from server. |
|
51
|
|
|
""" |
|
52
|
|
|
cmd = XmlCommand("authenticate") |
|
53
|
|
|
|
|
54
|
|
|
if not username: |
|
55
|
|
|
raise RequiredArgument( |
|
56
|
|
|
function=self.authenticate.__name__, argument='username' |
|
57
|
|
|
) |
|
58
|
|
|
|
|
59
|
|
|
if not password: |
|
60
|
|
|
raise RequiredArgument( |
|
61
|
|
|
function=self.authenticate.__name__, argument='password' |
|
62
|
|
|
) |
|
63
|
|
|
|
|
64
|
|
|
credentials = cmd.add_element("credentials") |
|
65
|
|
|
credentials.add_element("username", username) |
|
66
|
|
|
credentials.add_element("password", password) |
|
67
|
|
|
|
|
68
|
|
|
self._send(cmd.to_string()) |
|
69
|
|
|
response = self._read() |
|
70
|
|
|
|
|
71
|
|
|
if check_command_status(response): |
|
72
|
|
|
self._authenticated = True |
|
73
|
|
|
|
|
74
|
|
|
return self._transform(response) |
|
75
|
|
|
|
|
76
|
|
|
def describe_auth(self) -> Any: |
|
77
|
|
|
"""Describe authentication methods |
|
78
|
|
|
|
|
79
|
|
|
Returns a list of all used authentication methods if such a list is |
|
80
|
|
|
available. |
|
81
|
|
|
|
|
82
|
|
|
Returns: |
|
83
|
|
|
The response. See :py:meth:`send_command` for details. |
|
84
|
|
|
""" |
|
85
|
|
|
return self._send_xml_command(XmlCommand("describe_auth")) |
|
86
|
|
|
|
|
87
|
|
|
def modify_auth(self, group_name: str, auth_conf_settings: dict) -> Any: |
|
88
|
|
|
"""Modifies an existing auth. |
|
89
|
|
|
|
|
90
|
|
|
Arguments: |
|
91
|
|
|
group_name: Name of the group to be modified. |
|
92
|
|
|
auth_conf_settings: The new auth config. |
|
93
|
|
|
|
|
94
|
|
|
Returns: |
|
95
|
|
|
The response. See :py:meth:`send_command` for details. |
|
96
|
|
|
""" |
|
97
|
|
|
if not group_name: |
|
98
|
|
|
raise RequiredArgument( |
|
99
|
|
|
function=self.modify_auth.__name__, argument='group_name' |
|
100
|
|
|
) |
|
101
|
|
|
if not auth_conf_settings: |
|
102
|
|
|
raise RequiredArgument( |
|
103
|
|
|
function=self.modify_auth.__name__, |
|
104
|
|
|
argument='auth_conf_settings', |
|
105
|
|
|
) |
|
106
|
|
|
cmd = XmlCommand("modify_auth") |
|
107
|
|
|
_xmlgroup = cmd.add_element("group", attrs={"name": str(group_name)}) |
|
108
|
|
|
|
|
109
|
|
|
for key, value in auth_conf_settings.items(): |
|
110
|
|
|
_xmlauthconf = _xmlgroup.add_element("auth_conf_setting") |
|
111
|
|
|
_xmlauthconf.add_element("key", key) |
|
112
|
|
|
_xmlauthconf.add_element("value", value) |
|
113
|
|
|
|
|
114
|
|
|
return self._send_xml_command(cmd) |
|
115
|
|
|
|