2022-07-29 02:02:23 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import json
|
2023-11-22 08:46:05 +00:00
|
|
|
import asyncio
|
2022-07-29 02:02:23 +00:00
|
|
|
|
2023-08-07 01:37:24 +00:00
|
|
|
from channels.generic.websocket import AsyncJsonWebsocketConsumer
|
2023-11-22 08:46:05 +00:00
|
|
|
from django.core.cache import cache
|
|
|
|
from django.conf import settings
|
2024-02-22 03:26:12 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-07-29 02:02:23 +00:00
|
|
|
|
|
|
|
from common.db.utils import close_old_connections
|
|
|
|
from common.utils import get_logger
|
2023-11-22 08:46:05 +00:00
|
|
|
from settings.serializers import (
|
|
|
|
LDAPTestConfigSerializer,
|
|
|
|
LDAPTestLoginSerializer
|
|
|
|
)
|
2024-02-22 03:26:12 +00:00
|
|
|
from orgs.models import Organization
|
|
|
|
from orgs.utils import current_org
|
2023-11-22 08:46:05 +00:00
|
|
|
from settings.tasks import sync_ldap_user
|
|
|
|
from settings.utils import (
|
2024-02-22 03:26:12 +00:00
|
|
|
LDAPServerUtil, LDAPCacheUtil, LDAPImportUtil, LDAPSyncUtil,
|
|
|
|
LDAP_USE_CACHE_FLAGS, LDAPTestUtil
|
2023-11-22 08:46:05 +00:00
|
|
|
)
|
2023-09-06 02:30:55 +00:00
|
|
|
from .tools import (
|
|
|
|
verbose_ping, verbose_telnet, verbose_nmap,
|
|
|
|
verbose_tcpdump, verbose_traceroute
|
|
|
|
)
|
2023-08-07 01:37:24 +00:00
|
|
|
|
2022-07-29 02:02:23 +00:00
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
2023-11-22 08:46:05 +00:00
|
|
|
CACHE_KEY_LDAP_TEST_CONFIG_TASK_STATUS = 'CACHE_KEY_LDAP_TEST_CONFIG_TASK_STATUS'
|
|
|
|
TASK_STATUS_IS_OVER = 'OVER'
|
|
|
|
|
2022-07-29 02:02:23 +00:00
|
|
|
|
2023-08-07 01:37:24 +00:00
|
|
|
class ToolsWebsocket(AsyncJsonWebsocketConsumer):
|
2022-07-29 02:02:23 +00:00
|
|
|
|
2023-08-07 01:37:24 +00:00
|
|
|
async def connect(self):
|
2022-07-29 02:02:23 +00:00
|
|
|
user = self.scope["user"]
|
|
|
|
if user.is_authenticated:
|
2023-08-07 01:37:24 +00:00
|
|
|
await self.accept()
|
2022-07-29 02:02:23 +00:00
|
|
|
else:
|
2023-08-07 01:37:24 +00:00
|
|
|
await self.close()
|
2022-07-29 02:02:23 +00:00
|
|
|
|
2023-08-07 01:37:24 +00:00
|
|
|
async def send_msg(self, msg=''):
|
2023-08-29 09:02:51 +00:00
|
|
|
await self.send_json({'msg': f'{msg}\r\n'})
|
2023-02-17 11:38:34 +00:00
|
|
|
|
2023-08-29 09:02:51 +00:00
|
|
|
async def imitate_ping(self, dest_ips, timeout=3, count=5, psize=64):
|
2023-08-07 01:37:24 +00:00
|
|
|
params = {
|
2023-08-29 09:02:51 +00:00
|
|
|
'dest_ips': dest_ips, 'timeout': timeout,
|
2023-08-07 01:37:24 +00:00
|
|
|
'count': count, 'psize': psize
|
|
|
|
}
|
|
|
|
logger.info(f'Receive request ping: {params}')
|
|
|
|
await verbose_ping(display=self.send_msg, **params)
|
2022-07-29 02:02:23 +00:00
|
|
|
|
2023-08-29 09:02:51 +00:00
|
|
|
async def imitate_telnet(self, dest_ips, dest_port=23, timeout=10):
|
2023-08-07 01:37:24 +00:00
|
|
|
params = {
|
2023-08-29 09:02:51 +00:00
|
|
|
'dest_ips': dest_ips, 'dest_port': dest_port, 'timeout': timeout,
|
2023-08-07 01:37:24 +00:00
|
|
|
}
|
|
|
|
logger.info(f'Receive request telnet: {params}')
|
|
|
|
await verbose_telnet(display=self.send_msg, **params)
|
2023-07-28 02:40:48 +00:00
|
|
|
|
2023-08-07 01:37:24 +00:00
|
|
|
async def imitate_nmap(self, dest_ips, dest_ports=None, timeout=None):
|
|
|
|
params = {
|
|
|
|
'dest_ips': dest_ips, 'dest_ports': dest_ports, 'timeout': timeout,
|
|
|
|
}
|
|
|
|
logger.info(f'Receive request nmap: {params}')
|
|
|
|
await verbose_nmap(display=self.send_msg, **params)
|
2022-07-29 02:02:23 +00:00
|
|
|
|
2023-08-07 01:37:24 +00:00
|
|
|
async def imitate_tcpdump(
|
|
|
|
self, interfaces=None, src_ips='',
|
|
|
|
src_ports='', dest_ips='', dest_ports=''
|
|
|
|
):
|
|
|
|
params = {
|
|
|
|
'interfaces': interfaces, 'src_ips': src_ips, 'src_ports': src_ports,
|
|
|
|
'dest_ips': dest_ips, 'dest_ports': dest_ports
|
|
|
|
}
|
|
|
|
logger.info(f'Receive request tcpdump: {params}')
|
|
|
|
await verbose_tcpdump(display=self.send_msg, **params)
|
|
|
|
|
2023-11-22 08:46:05 +00:00
|
|
|
async def imitate_traceroute(self, dest_ips):
|
2023-09-06 02:30:55 +00:00
|
|
|
params = {'dest_ips': dest_ips}
|
|
|
|
await verbose_traceroute(display=self.send_msg, **params)
|
|
|
|
|
2023-08-07 01:37:24 +00:00
|
|
|
async def receive(self, text_data=None, bytes_data=None, **kwargs):
|
2022-07-29 02:02:23 +00:00
|
|
|
data = json.loads(text_data)
|
2023-07-28 02:40:48 +00:00
|
|
|
tool_type = data.pop('tool_type', 'Ping')
|
2023-08-07 01:37:24 +00:00
|
|
|
try:
|
|
|
|
tool_func = getattr(self, f'imitate_{tool_type.lower()}')
|
|
|
|
await tool_func(**data)
|
|
|
|
except Exception as error:
|
|
|
|
await self.send_msg('Exception: %s' % error)
|
|
|
|
await self.send_msg()
|
|
|
|
await self.close()
|
2023-07-28 02:40:48 +00:00
|
|
|
|
2023-08-07 01:37:24 +00:00
|
|
|
async def disconnect(self, code):
|
|
|
|
await self.close()
|
2022-07-29 02:02:23 +00:00
|
|
|
close_old_connections()
|
2023-11-22 08:46:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LdapWebsocket(AsyncJsonWebsocketConsumer):
|
|
|
|
async def connect(self):
|
|
|
|
user = self.scope["user"]
|
|
|
|
if user.is_authenticated:
|
|
|
|
await self.accept()
|
|
|
|
else:
|
|
|
|
await self.close()
|
|
|
|
|
|
|
|
async def receive(self, text_data=None, bytes_data=None, **kwargs):
|
|
|
|
data = json.loads(text_data)
|
|
|
|
msg_type = data.pop('msg_type', 'testing_config')
|
|
|
|
try:
|
|
|
|
tool_func = getattr(self, f'run_{msg_type.lower()}')
|
2024-03-14 08:52:52 +00:00
|
|
|
ok, msg = await asyncio.to_thread(tool_func, data)
|
2023-11-22 08:46:05 +00:00
|
|
|
await self.send_msg(ok, msg)
|
|
|
|
except Exception as error:
|
|
|
|
await self.send_msg(msg='Exception: %s' % error)
|
|
|
|
|
|
|
|
async def send_msg(self, ok=True, msg=''):
|
|
|
|
await self.send_json({'ok': ok, 'msg': f'{msg}'})
|
|
|
|
|
|
|
|
async def disconnect(self, code):
|
|
|
|
await self.close()
|
|
|
|
close_old_connections()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_ldap_config(serializer):
|
|
|
|
server_uri = serializer.validated_data["AUTH_LDAP_SERVER_URI"]
|
|
|
|
bind_dn = serializer.validated_data["AUTH_LDAP_BIND_DN"]
|
|
|
|
password = serializer.validated_data["AUTH_LDAP_BIND_PASSWORD"]
|
|
|
|
use_ssl = serializer.validated_data.get("AUTH_LDAP_START_TLS", False)
|
|
|
|
search_ou = serializer.validated_data["AUTH_LDAP_SEARCH_OU"]
|
|
|
|
search_filter = serializer.validated_data["AUTH_LDAP_SEARCH_FILTER"]
|
|
|
|
attr_map = serializer.validated_data["AUTH_LDAP_USER_ATTR_MAP"]
|
|
|
|
auth_ldap = serializer.validated_data.get('AUTH_LDAP', False)
|
|
|
|
|
|
|
|
if not password:
|
|
|
|
password = settings.AUTH_LDAP_BIND_PASSWORD
|
|
|
|
|
|
|
|
config = {
|
|
|
|
'server_uri': server_uri,
|
|
|
|
'bind_dn': bind_dn,
|
|
|
|
'password': password,
|
|
|
|
'use_ssl': use_ssl,
|
|
|
|
'search_ou': search_ou,
|
|
|
|
'search_filter': search_filter,
|
|
|
|
'attr_map': attr_map,
|
|
|
|
'auth_ldap': auth_ldap
|
|
|
|
}
|
|
|
|
return config
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def task_is_over(task_key):
|
|
|
|
return cache.get(task_key) == TASK_STATUS_IS_OVER
|
|
|
|
|
|
|
|
@staticmethod
|
2023-12-15 09:10:51 +00:00
|
|
|
def set_task_status_over(task_key, ttl=120):
|
|
|
|
cache.set(task_key, TASK_STATUS_IS_OVER, ttl)
|
2023-11-22 08:46:05 +00:00
|
|
|
|
|
|
|
def run_testing_config(self, data):
|
2024-03-14 08:52:52 +00:00
|
|
|
serializer = LDAPTestConfigSerializer(data=data)
|
|
|
|
if not serializer.is_valid():
|
|
|
|
self.send_msg(msg=f'error: {str(serializer.errors)}')
|
|
|
|
config = self.get_ldap_config(serializer)
|
|
|
|
ok, msg = LDAPTestUtil(config).test_config()
|
|
|
|
if ok:
|
|
|
|
self.set_task_status_over(CACHE_KEY_LDAP_TEST_CONFIG_TASK_STATUS)
|
|
|
|
return ok, msg
|
2023-11-22 08:46:05 +00:00
|
|
|
|
|
|
|
def run_testing_login(self, data):
|
2024-03-14 08:52:52 +00:00
|
|
|
serializer = LDAPTestLoginSerializer(data=data)
|
|
|
|
if not serializer.is_valid():
|
|
|
|
self.send_msg(msg=f'error: {str(serializer.errors)}')
|
|
|
|
username = serializer.validated_data['username']
|
|
|
|
password = serializer.validated_data['password']
|
|
|
|
ok, msg = LDAPTestUtil().test_login(username, password)
|
|
|
|
return ok, msg
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def run_sync_user(data):
|
|
|
|
sync_util = LDAPSyncUtil()
|
|
|
|
sync_util.clear_cache()
|
|
|
|
sync_ldap_user()
|
|
|
|
msg = sync_util.get_task_error_msg()
|
|
|
|
ok = False if msg else True
|
|
|
|
return ok, msg
|
2024-02-22 03:26:12 +00:00
|
|
|
|
|
|
|
def run_import_user(self, data):
|
|
|
|
ok = False
|
|
|
|
org_ids = data.get('org_ids')
|
|
|
|
username_list = data.get('username_list', [])
|
|
|
|
cache_police = data.get('cache_police', True)
|
|
|
|
try:
|
|
|
|
users = self.get_ldap_users(username_list, cache_police)
|
|
|
|
if users is None:
|
|
|
|
msg = _('Get ldap users is None')
|
|
|
|
|
|
|
|
orgs = self.get_orgs(org_ids)
|
|
|
|
new_users, error_msg = LDAPImportUtil().perform_import(users, orgs)
|
|
|
|
if error_msg:
|
|
|
|
msg = error_msg
|
|
|
|
|
|
|
|
count = users if users is None else len(users)
|
|
|
|
orgs_name = ', '.join([str(org) for org in orgs])
|
|
|
|
ok = True
|
|
|
|
msg = _('Imported {} users successfully (Organization: {})').format(count, orgs_name)
|
|
|
|
except Exception as e:
|
|
|
|
msg = str(e)
|
|
|
|
return ok, msg
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_orgs(org_ids):
|
|
|
|
if org_ids:
|
|
|
|
orgs = list(Organization.objects.filter(id__in=org_ids))
|
|
|
|
else:
|
|
|
|
orgs = [current_org]
|
|
|
|
return orgs
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_ldap_users(username_list, cache_police):
|
|
|
|
if '*' in username_list:
|
|
|
|
users = LDAPServerUtil().search()
|
|
|
|
elif cache_police in LDAP_USE_CACHE_FLAGS:
|
|
|
|
users = LDAPCacheUtil().search(search_users=username_list)
|
|
|
|
else:
|
|
|
|
users = LDAPServerUtil().search(search_users=username_list)
|
|
|
|
return users
|