diff --git a/SMBServer/SMBServer.csproj b/SMBServer/SMBServer.csproj index 1545fe7..c1bcdc8 100644 --- a/SMBServer/SMBServer.csproj +++ b/SMBServer/SMBServer.csproj @@ -50,6 +50,8 @@ Designer ServerUI.cs + + diff --git a/SMBServer/ServerUI.cs b/SMBServer/ServerUI.cs index f26b39b..7786d57 100644 --- a/SMBServer/ServerUI.cs +++ b/SMBServer/ServerUI.cs @@ -1,4 +1,4 @@ -/* Copyright (C) 2014-2017 Tal Aloni . All rights reserved. +/* Copyright (C) 2014-2018 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, @@ -7,15 +7,11 @@ using System; using System.Collections.Generic; using System.ComponentModel; -using System.Data; using System.Drawing; using System.IO; using System.Net; -using System.Net.NetworkInformation; using System.Net.Sockets; -using System.Text; using System.Windows.Forms; -using System.Xml; using SMBLibrary; using SMBLibrary.Authentication.GSSAPI; using SMBLibrary.Authentication.NTLM; @@ -28,7 +24,6 @@ namespace SMBServer { public partial class ServerUI : Form { - public const string SettingsFileName = "Settings.xml"; private SMBLibrary.Server.SMBServer m_server; private SMBLibrary.Server.NameServer m_nameServer; private LogWriter m_logWriter; @@ -75,28 +70,35 @@ namespace SMBServer UserCollection users; try { - users = ReadUserSettings(); + users = SettingsHelper.ReadUserSettings(); } catch { - MessageBox.Show("Cannot read " + SettingsFileName, "Error"); + MessageBox.Show("Cannot read " + SettingsHelper.SettingsFileName, "Error"); return; } authenticationMechanism = new IndependentNTLMAuthenticationProvider(users.GetUserPassword); } - SMBShareCollection shares; + List sharesSettings; try { - shares = ReadShareSettings(); + sharesSettings = SettingsHelper.ReadSharesSettings(); } catch (Exception) { - MessageBox.Show("Cannot read " + SettingsFileName, "Error"); + MessageBox.Show("Cannot read " + SettingsHelper.SettingsFileName, "Error"); return; } + SMBShareCollection shares = new SMBShareCollection(); + foreach (ShareSettings shareSettings in sharesSettings) + { + FileSystemShare share = InitializeShare(shareSettings); + shares.Add(share); + } + GSSProvider securityProvider = new GSSProvider(authenticationMechanism); m_server = new SMBLibrary.Server.SMBServer(shares, securityProvider); m_logWriter = new LogWriter(); @@ -133,85 +135,6 @@ namespace SMBServer chkIntegratedWindowsAuthentication.Enabled = false; } - private XmlDocument GetSettingsXML() - { - string executableDirectory = Path.GetDirectoryName(Application.ExecutablePath) + "\\"; - XmlDocument document = GetXmlDocument(executableDirectory + SettingsFileName); - return document; - } - - private UserCollection ReadUserSettings() - { - UserCollection users = new UserCollection(); - XmlDocument document = GetSettingsXML(); - XmlNode usersNode = document.SelectSingleNode("Settings/Users"); - - foreach (XmlNode userNode in usersNode.ChildNodes) - { - string accountName = userNode.Attributes["AccountName"].Value; - string password = userNode.Attributes["Password"].Value; - users.Add(accountName, password); - } - return users; - } - - private SMBShareCollection ReadShareSettings() - { - SMBShareCollection shares = new SMBShareCollection(); - XmlDocument document = GetSettingsXML(); - XmlNode sharesNode = document.SelectSingleNode("Settings/Shares"); - - foreach (XmlNode shareNode in sharesNode.ChildNodes) - { - string shareName = shareNode.Attributes["Name"].Value; - string sharePath = shareNode.Attributes["Path"].Value; - - XmlNode readAccessNode = shareNode.SelectSingleNode("ReadAccess"); - List readAccess = ReadAccessList(readAccessNode); - XmlNode writeAccessNode = shareNode.SelectSingleNode("WriteAccess"); - List writeAccess = ReadAccessList(writeAccessNode); - FileSystemShare share = new FileSystemShare(shareName, new NTDirectoryFileSystem(sharePath)); - share.AccessRequested += delegate(object sender, AccessRequestArgs args) - { - bool hasReadAccess = Contains(readAccess, "Users") || Contains(readAccess, args.UserName); - bool hasWriteAccess = Contains(writeAccess, "Users") || Contains(writeAccess, args.UserName); - if (args.RequestedAccess == FileAccess.Read) - { - args.Allow = hasReadAccess; - } - else if (args.RequestedAccess == FileAccess.Write) - { - args.Allow = hasWriteAccess; - } - else // FileAccess.ReadWrite - { - args.Allow = hasReadAccess && hasWriteAccess; - } - }; - shares.Add(share); - } - return shares; - } - - private List ReadAccessList(XmlNode node) - { - List result = new List(); - if (node != null) - { - string accounts = node.Attributes["Accounts"].Value; - if (accounts == "*") - { - result.Add("Users"); - } - else - { - string[] splitted = accounts.Split(','); - result.AddRange(splitted); - } - } - return result; - } - private void btnStop_Click(object sender, EventArgs e) { m_server.Stop(); @@ -247,11 +170,31 @@ namespace SMBServer } } - public static XmlDocument GetXmlDocument(string path) + public static FileSystemShare InitializeShare(ShareSettings shareSettings) { - XmlDocument doc = new XmlDocument(); - doc.Load(path); - return doc; + string shareName = shareSettings.ShareName; + string sharePath = shareSettings.SharePath; + List readAccess = shareSettings.ReadAccess; + List writeAccess = shareSettings.WriteAccess; + FileSystemShare share = new FileSystemShare(shareName, new NTDirectoryFileSystem(sharePath)); + share.AccessRequested += delegate(object sender, AccessRequestArgs args) + { + bool hasReadAccess = Contains(readAccess, "Users") || Contains(readAccess, args.UserName); + bool hasWriteAccess = Contains(writeAccess, "Users") || Contains(writeAccess, args.UserName); + if (args.RequestedAccess == FileAccess.Read) + { + args.Allow = hasReadAccess; + } + else if (args.RequestedAccess == FileAccess.Write) + { + args.Allow = hasWriteAccess; + } + else // FileAccess.ReadWrite + { + args.Allow = hasReadAccess && hasWriteAccess; + } + }; + return share; } public static bool Contains(List list, string value) diff --git a/SMBServer/SettingsHelper.cs b/SMBServer/SettingsHelper.cs new file mode 100644 index 0000000..26d8be7 --- /dev/null +++ b/SMBServer/SettingsHelper.cs @@ -0,0 +1,88 @@ +/* Copyright (C) 2014-2018 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 System.IO; +using System.Windows.Forms; +using System.Xml; + +namespace SMBServer +{ + public class SettingsHelper + { + public const string SettingsFileName = "Settings.xml"; + + public static XmlDocument ReadXmlDocument(string path) + { + XmlDocument doc = new XmlDocument(); + doc.Load(path); + return doc; + } + + public static XmlDocument ReadSettingsXML() + { + string executableDirectory = Path.GetDirectoryName(Application.ExecutablePath) + "\\"; + XmlDocument document = ReadXmlDocument(executableDirectory + SettingsFileName); + return document; + } + + public static UserCollection ReadUserSettings() + { + UserCollection users = new UserCollection(); + XmlDocument document = ReadSettingsXML(); + XmlNode usersNode = document.SelectSingleNode("Settings/Users"); + + foreach (XmlNode userNode in usersNode.ChildNodes) + { + string accountName = userNode.Attributes["AccountName"].Value; + string password = userNode.Attributes["Password"].Value; + users.Add(accountName, password); + } + return users; + } + + public static List ReadSharesSettings() + { + List shares = new List(); + XmlDocument document = ReadSettingsXML(); + XmlNode sharesNode = document.SelectSingleNode("Settings/Shares"); + + foreach (XmlNode shareNode in sharesNode.ChildNodes) + { + string shareName = shareNode.Attributes["Name"].Value; + string sharePath = shareNode.Attributes["Path"].Value; + + XmlNode readAccessNode = shareNode.SelectSingleNode("ReadAccess"); + List readAccess = ReadAccessList(readAccessNode); + XmlNode writeAccessNode = shareNode.SelectSingleNode("WriteAccess"); + List writeAccess = ReadAccessList(writeAccessNode); + ShareSettings share = new ShareSettings(shareName, sharePath, readAccess, writeAccess); + shares.Add(share); + } + return shares; + } + + private static List ReadAccessList(XmlNode node) + { + List result = new List(); + if (node != null) + { + string accounts = node.Attributes["Accounts"].Value; + if (accounts == "*") + { + result.Add("Users"); + } + else + { + string[] splitted = accounts.Split(','); + result.AddRange(splitted); + } + } + return result; + } + } +} diff --git a/SMBServer/ShareSettings.cs b/SMBServer/ShareSettings.cs new file mode 100644 index 0000000..e13a0eb --- /dev/null +++ b/SMBServer/ShareSettings.cs @@ -0,0 +1,27 @@ +/* Copyright (C) 2014-2018 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; + +namespace SMBServer +{ + public class ShareSettings + { + public string ShareName; + public string SharePath; + public List ReadAccess; + public List WriteAccess; + + public ShareSettings(string shareName, string sharePath, List readAccess, List writeAccess) + { + ShareName = shareName; + SharePath = sharePath; + ReadAccess = readAccess; + WriteAccess = writeAccess; + } + } +}