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.Text; using System.Windows.Forms; using System.Xml; using SMBLibrary; using SMBLibrary.Server; using SMBLibrary.Server.Win32; using Utilities; 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; public ServerUI() { InitializeComponent(); } private void ServerUI_Load(object sender, EventArgs e) { List localIPs = GetHostIPAddresses(); KeyValuePairList list = new KeyValuePairList(); list.Add("Any", IPAddress.Any); foreach (IPAddress address in localIPs) { list.Add(address.ToString(), address); } comboIPAddress.DataSource = list; comboIPAddress.DisplayMember = "Key"; comboIPAddress.ValueMember = "Value"; } private void btnStart_Click(object sender, EventArgs e) { IPAddress serverAddress = (IPAddress)comboIPAddress.SelectedValue; SMBTransportType transportType; if (rbtNetBiosOverTCP.Checked) { transportType = SMBTransportType.NetBiosOverTCP; } else { transportType = SMBTransportType.DirectTCPTransport; } INTLMAuthenticationProvider provider; if (chkIntegratedWindowsAuthentication.Checked) { provider = new Win32UserCollection(); } else { UserCollection users; try { users = ReadUserSettings(); } catch { MessageBox.Show("Cannot read " + SettingsFileName, "Error"); return; } provider = new IndependentUserCollection(users); } List allUsers = provider.ListUsers(); ShareCollection shares; try { shares = ReadShareSettings(allUsers); } catch (Exception) { MessageBox.Show("Cannot read " + SettingsFileName, "Error"); return; } m_server = new SMBLibrary.Server.SMBServer(shares, provider, serverAddress, transportType, chkSMB1.Checked, chkSMB2.Checked); m_server.OnLogEntry += new EventHandler(Server_OnLogEntry); try { m_server.Start(); if (transportType == SMBTransportType.NetBiosOverTCP) { m_nameServer = new NameServer(serverAddress); m_nameServer.Start(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); return; } btnStart.Enabled = false; btnStop.Enabled = true; comboIPAddress.Enabled = false; rbtDirectTCPTransport.Enabled = false; rbtNetBiosOverTCP.Enabled = false; chkSMB1.Enabled = false; chkSMB2.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 ShareCollection ReadShareSettings(List allUsers) { ShareCollection shares = new ShareCollection(); 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, allUsers); XmlNode writeAccessNode = shareNode.SelectSingleNode("WriteAccess"); List writeAccess = ReadAccessList(writeAccessNode, allUsers); shares.Add(shareName, readAccess, writeAccess, new DirectoryFileSystem(sharePath)); } return shares; } private List ReadAccessList(XmlNode node, List allUsers) { List result = new List(); if (node != null) { string accounts = node.Attributes["Accounts"].Value; if (accounts == "*") { result.AddRange(allUsers); } else { string[] splitted = accounts.Split(','); result.AddRange(splitted); } } return result; } private void btnStop_Click(object sender, EventArgs e) { m_server.Stop(); btnStart.Enabled = true; btnStop.Enabled = false; comboIPAddress.Enabled = true; rbtDirectTCPTransport.Enabled = true; rbtNetBiosOverTCP.Enabled = true; chkSMB1.Enabled = true; chkSMB2.Enabled = true; chkIntegratedWindowsAuthentication.Enabled = true; if (m_nameServer != null) { m_nameServer.Stop(); } } private static XmlDocument GetXmlDocument(string path) { XmlDocument doc = new XmlDocument(); doc.Load(path); return doc; } private static List GetHostIPAddresses() { List result = new List(); foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces()) { IPInterfaceProperties ipProperties = netInterface.GetIPProperties(); foreach (UnicastIPAddressInformation addressInfo in ipProperties.UnicastAddresses) { if (addressInfo.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { result.Add(addressInfo.Address); } } } return result; } private void Server_OnLogEntry(object sender, LogEntry entry) { string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss "); string message = String.Format("{0} {1} {2}", entry.Severity.ToString().PadRight(12), timestamp, entry.Message); System.Diagnostics.Debug.Print(message); } private void chkSMB1_CheckedChanged(object sender, EventArgs e) { if (!chkSMB1.Checked) { chkSMB2.Checked = true; } } private void chkSMB2_CheckedChanged(object sender, EventArgs e) { if (!chkSMB2.Checked) { chkSMB1.Checked = true; } } } }