mirror of
https://github.com/TalAloni/SMBLibrary.git
synced 2025-05-30 01:09:53 +02:00
SMB Server application: Added SettingsHelper class to handle settings logic
This commit is contained in:
parent
ff368056c5
commit
6d562c41e9
4 changed files with 154 additions and 94 deletions
|
@ -50,6 +50,8 @@
|
||||||
<SubType>Designer</SubType>
|
<SubType>Designer</SubType>
|
||||||
<DependentUpon>ServerUI.cs</DependentUpon>
|
<DependentUpon>ServerUI.cs</DependentUpon>
|
||||||
</EmbeddedResource>
|
</EmbeddedResource>
|
||||||
|
<Compile Include="SettingsHelper.cs" />
|
||||||
|
<Compile Include="ShareSettings.cs" />
|
||||||
<Compile Include="User.cs" />
|
<Compile Include="User.cs" />
|
||||||
<Compile Include="UserCollection.cs" />
|
<Compile Include="UserCollection.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
|
/* Copyright (C) 2014-2018 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
|
||||||
*
|
*
|
||||||
* You can redistribute this program and/or modify it under the terms of
|
* 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,
|
* the GNU Lesser Public License as published by the Free Software Foundation,
|
||||||
|
@ -7,15 +7,11 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Data;
|
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.NetworkInformation;
|
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using System.Xml;
|
|
||||||
using SMBLibrary;
|
using SMBLibrary;
|
||||||
using SMBLibrary.Authentication.GSSAPI;
|
using SMBLibrary.Authentication.GSSAPI;
|
||||||
using SMBLibrary.Authentication.NTLM;
|
using SMBLibrary.Authentication.NTLM;
|
||||||
|
@ -28,7 +24,6 @@ namespace SMBServer
|
||||||
{
|
{
|
||||||
public partial class ServerUI : Form
|
public partial class ServerUI : Form
|
||||||
{
|
{
|
||||||
public const string SettingsFileName = "Settings.xml";
|
|
||||||
private SMBLibrary.Server.SMBServer m_server;
|
private SMBLibrary.Server.SMBServer m_server;
|
||||||
private SMBLibrary.Server.NameServer m_nameServer;
|
private SMBLibrary.Server.NameServer m_nameServer;
|
||||||
private LogWriter m_logWriter;
|
private LogWriter m_logWriter;
|
||||||
|
@ -75,28 +70,35 @@ namespace SMBServer
|
||||||
UserCollection users;
|
UserCollection users;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
users = ReadUserSettings();
|
users = SettingsHelper.ReadUserSettings();
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
MessageBox.Show("Cannot read " + SettingsFileName, "Error");
|
MessageBox.Show("Cannot read " + SettingsHelper.SettingsFileName, "Error");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
authenticationMechanism = new IndependentNTLMAuthenticationProvider(users.GetUserPassword);
|
authenticationMechanism = new IndependentNTLMAuthenticationProvider(users.GetUserPassword);
|
||||||
}
|
}
|
||||||
|
|
||||||
SMBShareCollection shares;
|
List<ShareSettings> sharesSettings;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
shares = ReadShareSettings();
|
sharesSettings = SettingsHelper.ReadSharesSettings();
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Cannot read " + SettingsFileName, "Error");
|
MessageBox.Show("Cannot read " + SettingsHelper.SettingsFileName, "Error");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SMBShareCollection shares = new SMBShareCollection();
|
||||||
|
foreach (ShareSettings shareSettings in sharesSettings)
|
||||||
|
{
|
||||||
|
FileSystemShare share = InitializeShare(shareSettings);
|
||||||
|
shares.Add(share);
|
||||||
|
}
|
||||||
|
|
||||||
GSSProvider securityProvider = new GSSProvider(authenticationMechanism);
|
GSSProvider securityProvider = new GSSProvider(authenticationMechanism);
|
||||||
m_server = new SMBLibrary.Server.SMBServer(shares, securityProvider);
|
m_server = new SMBLibrary.Server.SMBServer(shares, securityProvider);
|
||||||
m_logWriter = new LogWriter();
|
m_logWriter = new LogWriter();
|
||||||
|
@ -133,85 +135,6 @@ namespace SMBServer
|
||||||
chkIntegratedWindowsAuthentication.Enabled = false;
|
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<string> readAccess = ReadAccessList(readAccessNode);
|
|
||||||
XmlNode writeAccessNode = shareNode.SelectSingleNode("WriteAccess");
|
|
||||||
List<string> 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<string> ReadAccessList(XmlNode node)
|
|
||||||
{
|
|
||||||
List<string> result = new List<string>();
|
|
||||||
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)
|
private void btnStop_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
m_server.Stop();
|
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();
|
string shareName = shareSettings.ShareName;
|
||||||
doc.Load(path);
|
string sharePath = shareSettings.SharePath;
|
||||||
return doc;
|
List<string> readAccess = shareSettings.ReadAccess;
|
||||||
|
List<string> 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<string> list, string value)
|
public static bool Contains(List<string> list, string value)
|
||||||
|
|
88
SMBServer/SettingsHelper.cs
Normal file
88
SMBServer/SettingsHelper.cs
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
/* Copyright (C) 2014-2018 Tal Aloni <tal.aloni.il@gmail.com>. 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<ShareSettings> ReadSharesSettings()
|
||||||
|
{
|
||||||
|
List<ShareSettings> shares = new List<ShareSettings>();
|
||||||
|
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<string> readAccess = ReadAccessList(readAccessNode);
|
||||||
|
XmlNode writeAccessNode = shareNode.SelectSingleNode("WriteAccess");
|
||||||
|
List<string> writeAccess = ReadAccessList(writeAccessNode);
|
||||||
|
ShareSettings share = new ShareSettings(shareName, sharePath, readAccess, writeAccess);
|
||||||
|
shares.Add(share);
|
||||||
|
}
|
||||||
|
return shares;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<string> ReadAccessList(XmlNode node)
|
||||||
|
{
|
||||||
|
List<string> result = new List<string>();
|
||||||
|
if (node != null)
|
||||||
|
{
|
||||||
|
string accounts = node.Attributes["Accounts"].Value;
|
||||||
|
if (accounts == "*")
|
||||||
|
{
|
||||||
|
result.Add("Users");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string[] splitted = accounts.Split(',');
|
||||||
|
result.AddRange(splitted);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
27
SMBServer/ShareSettings.cs
Normal file
27
SMBServer/ShareSettings.cs
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
/* Copyright (C) 2014-2018 Tal Aloni <tal.aloni.il@gmail.com>. 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<string> ReadAccess;
|
||||||
|
public List<string> WriteAccess;
|
||||||
|
|
||||||
|
public ShareSettings(string shareName, string sharePath, List<string> readAccess, List<string> writeAccess)
|
||||||
|
{
|
||||||
|
ShareName = shareName;
|
||||||
|
SharePath = sharePath;
|
||||||
|
ReadAccess = readAccess;
|
||||||
|
WriteAccess = writeAccess;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue