From 3b683cd4ae632fc7ac0f9e5337c0f383b4e2750e Mon Sep 17 00:00:00 2001 From: Tal Aloni Date: Sat, 6 Jan 2024 21:26:22 +0200 Subject: [PATCH] Server: ServerService: correctly handle unsupported ShareEnum levels --- .../Exceptions/InvalidLevelException.cs | 28 +++++++++++++++++++ .../Exceptions/UnsupportedLevelException.cs | 28 +++++++++++++++++++ .../Services/ServerService/ServerService.cs | 27 ++++++++++++++---- .../Structures/ServerInfo/ServerInfo.cs | 8 ++---- .../Structures/ShareInfo/ShareEnum.cs | 8 ++---- .../Structures/ShareInfo/ShareInfo.cs | 7 ++--- 6 files changed, 84 insertions(+), 22 deletions(-) create mode 100644 SMBLibrary/Services/Exceptions/InvalidLevelException.cs create mode 100644 SMBLibrary/Services/Exceptions/UnsupportedLevelException.cs diff --git a/SMBLibrary/Services/Exceptions/InvalidLevelException.cs b/SMBLibrary/Services/Exceptions/InvalidLevelException.cs new file mode 100644 index 0000000..03476ac --- /dev/null +++ b/SMBLibrary/Services/Exceptions/InvalidLevelException.cs @@ -0,0 +1,28 @@ +/* Copyright (C) 2024 Tal Aloni . All rights reserved. + * + * You can redistribute this program and/or modify it under the terms of + * the GNU Lesser Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + */ +using System; + +namespace SMBLibrary.Services +{ + public class InvalidLevelException : Exception + { + private uint m_level; + + public InvalidLevelException(uint level) + { + m_level = level; + } + + public uint Level + { + get + { + return m_level; + } + } + } +} diff --git a/SMBLibrary/Services/Exceptions/UnsupportedLevelException.cs b/SMBLibrary/Services/Exceptions/UnsupportedLevelException.cs new file mode 100644 index 0000000..234a8fb --- /dev/null +++ b/SMBLibrary/Services/Exceptions/UnsupportedLevelException.cs @@ -0,0 +1,28 @@ +/* Copyright (C) 2024 Tal Aloni . All rights reserved. + * + * You can redistribute this program and/or modify it under the terms of + * the GNU Lesser Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + */ +using System; + +namespace SMBLibrary.Services +{ + public class UnsupportedLevelException : Exception + { + private uint m_level; + + public UnsupportedLevelException(uint level) + { + m_level = level; + } + + public uint Level + { + get + { + return m_level; + } + } + } +} diff --git a/SMBLibrary/Services/ServerService/ServerService.cs b/SMBLibrary/Services/ServerService/ServerService.cs index 5b84318..6ad5d8f 100644 --- a/SMBLibrary/Services/ServerService/ServerService.cs +++ b/SMBLibrary/Services/ServerService/ServerService.cs @@ -1,4 +1,4 @@ -/* Copyright (C) 2014-2018 Tal Aloni . All rights reserved. +/* Copyright (C) 2014-2024 Tal Aloni . All rights reserved. * * You can redistribute this program and/or modify it under the terms of * the GNU Lesser Public License as published by the Free Software Foundation, @@ -6,8 +6,6 @@ */ using System; using System.Collections.Generic; -using System.Text; -using Utilities; namespace SMBLibrary.Services { @@ -47,8 +45,7 @@ namespace SMBLibrary.Services { case ServerServiceOpName.NetrShareEnum: { - NetrShareEnumRequest request = new NetrShareEnumRequest(requestBytes); - NetrShareEnumResponse response = GetNetrShareEnumResponse(request); + NetrShareEnumResponse response = GetNetrShareEnumResponse(requestBytes); return response.GetBytes(); } case ServerServiceOpName.NetrShareGetInfo: @@ -68,9 +65,27 @@ namespace SMBLibrary.Services } } - public NetrShareEnumResponse GetNetrShareEnumResponse(NetrShareEnumRequest request) + public NetrShareEnumResponse GetNetrShareEnumResponse(byte[] requestBytes) { + NetrShareEnumRequest request; NetrShareEnumResponse response = new NetrShareEnumResponse(); + try + { + request = new NetrShareEnumRequest(requestBytes); + } + catch (UnsupportedLevelException ex) + { + response.InfoStruct = new ShareEnum(ex.Level); + response.Result = Win32Error.ERROR_NOT_SUPPORTED; + return response; + } + catch (InvalidLevelException ex) + { + response.InfoStruct = new ShareEnum(ex.Level); + response.Result = Win32Error.ERROR_INVALID_LEVEL; + return response; + } + switch (request.InfoStruct.Level) { case 0: diff --git a/SMBLibrary/Services/ServerService/Structures/ServerInfo/ServerInfo.cs b/SMBLibrary/Services/ServerService/Structures/ServerInfo/ServerInfo.cs index 501783f..7daef0e 100644 --- a/SMBLibrary/Services/ServerService/Structures/ServerInfo/ServerInfo.cs +++ b/SMBLibrary/Services/ServerService/Structures/ServerInfo/ServerInfo.cs @@ -1,13 +1,10 @@ -/* Copyright (C) 2014-2018 Tal Aloni . All rights reserved. +/* Copyright (C) 2014-2024 Tal Aloni . All rights reserved. * * You can redistribute this program and/or modify it under the terms of * the GNU Lesser Public License as published by the Free Software Foundation, * either version 3 of the License, or (at your option) any later version. */ -using System; -using System.Collections.Generic; using SMBLibrary.RPC; -using Utilities; namespace SMBLibrary.Services { @@ -56,9 +53,8 @@ namespace SMBLibrary.Services Info = info101; break; default: - throw new NotImplementedException(); + throw new InvalidLevelException(Level); } - ; parser.EndStructure(); // SERVER_INFO Union } diff --git a/SMBLibrary/Services/ServerService/Structures/ShareInfo/ShareEnum.cs b/SMBLibrary/Services/ServerService/Structures/ShareInfo/ShareEnum.cs index 5347839..3cca632 100644 --- a/SMBLibrary/Services/ServerService/Structures/ShareInfo/ShareEnum.cs +++ b/SMBLibrary/Services/ServerService/Structures/ShareInfo/ShareEnum.cs @@ -1,13 +1,11 @@ -/* Copyright (C) 2014-2018 Tal Aloni . All rights reserved. +/* Copyright (C) 2014-2024 Tal Aloni . All rights reserved. * * You can redistribute this program and/or modify it under the terms of * the GNU Lesser Public License as published by the Free Software Foundation, * either version 3 of the License, or (at your option) any later version. */ using System; -using System.Collections.Generic; using SMBLibrary.RPC; -using Utilities; namespace SMBLibrary.Services { @@ -64,9 +62,9 @@ namespace SMBLibrary.Services case 501: case 502: case 503: - throw new NotImplementedException(); + throw new UnsupportedLevelException(level); default: - break; + throw new InvalidLevelException(level); } parser.EndStructure(); // SHARE_ENUM_UNION parser.EndStructure(); // SHARE_ENUM_STRUCT diff --git a/SMBLibrary/Services/ServerService/Structures/ShareInfo/ShareInfo.cs b/SMBLibrary/Services/ServerService/Structures/ShareInfo/ShareInfo.cs index 22eb9d3..3d9d27c 100644 --- a/SMBLibrary/Services/ServerService/Structures/ShareInfo/ShareInfo.cs +++ b/SMBLibrary/Services/ServerService/Structures/ShareInfo/ShareInfo.cs @@ -1,13 +1,10 @@ -/* Copyright (C) 2014 Tal Aloni . All rights reserved. +/* Copyright (C) 2014-2024 Tal Aloni . All rights reserved. * * You can redistribute this program and/or modify it under the terms of * the GNU Lesser Public License as published by the Free Software Foundation, * either version 3 of the License, or (at your option) any later version. */ -using System; -using System.Collections.Generic; using SMBLibrary.RPC; -using Utilities; namespace SMBLibrary.Services { @@ -56,7 +53,7 @@ namespace SMBLibrary.Services Info = info1; break; default: - throw new NotImplementedException(); + throw new InvalidLevelException(Level); } parser.EndStructure(); // SHARE_INFO Union }