ServerUI.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Data;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Net;
  14. using System.Net.NetworkInformation;
  15. using System.Net.Sockets;
  16. using System.Text;
  17. using System.Windows.Forms;
  18. using System.Xml;
  19. using SMBLibrary;
  20. using SMBLibrary.Authentication.NTLM;
  21. using SMBLibrary.Server;
  22. using SMBLibrary.Win32.Security;
  23. using Utilities;
  24. namespace SMBServer
  25. {
  26. public partial class ServerUI : Form
  27. {
  28. public const string SettingsFileName = "Settings.xml";
  29. private SMBLibrary.Server.SMBServer m_server;
  30. private SMBLibrary.Server.NameServer m_nameServer;
  31. public ServerUI()
  32. {
  33. InitializeComponent();
  34. }
  35. private void ServerUI_Load(object sender, EventArgs e)
  36. {
  37. List<IPAddress> localIPs = NetworkInterfaceHelper.GetHostIPAddresses();
  38. KeyValuePairList<string, IPAddress> list = new KeyValuePairList<string, IPAddress>();
  39. list.Add("Any", IPAddress.Any);
  40. foreach (IPAddress address in localIPs)
  41. {
  42. list.Add(address.ToString(), address);
  43. }
  44. comboIPAddress.DataSource = list;
  45. comboIPAddress.DisplayMember = "Key";
  46. comboIPAddress.ValueMember = "Value";
  47. }
  48. private void btnStart_Click(object sender, EventArgs e)
  49. {
  50. IPAddress serverAddress = (IPAddress)comboIPAddress.SelectedValue;
  51. SMBTransportType transportType;
  52. if (rbtNetBiosOverTCP.Checked)
  53. {
  54. transportType = SMBTransportType.NetBiosOverTCP;
  55. }
  56. else
  57. {
  58. transportType = SMBTransportType.DirectTCPTransport;
  59. }
  60. NTLMAuthenticationProviderBase provider;
  61. if (chkIntegratedWindowsAuthentication.Checked)
  62. {
  63. provider = new IntegratedNTLMAuthenticationProvider();
  64. }
  65. else
  66. {
  67. UserCollection users;
  68. try
  69. {
  70. users = ReadUserSettings();
  71. }
  72. catch
  73. {
  74. MessageBox.Show("Cannot read " + SettingsFileName, "Error");
  75. return;
  76. }
  77. provider = new IndependentNTLMAuthenticationProvider(users.GetUserPassword);
  78. }
  79. ShareCollection shares;
  80. try
  81. {
  82. shares = ReadShareSettings();
  83. }
  84. catch (Exception)
  85. {
  86. MessageBox.Show("Cannot read " + SettingsFileName, "Error");
  87. return;
  88. }
  89. m_server = new SMBLibrary.Server.SMBServer(shares, provider, serverAddress, transportType, chkSMB1.Checked, chkSMB2.Checked);
  90. m_server.OnLogEntry += new EventHandler<LogEntry>(Server_OnLogEntry);
  91. try
  92. {
  93. m_server.Start();
  94. if (transportType == SMBTransportType.NetBiosOverTCP)
  95. {
  96. if (serverAddress.AddressFamily == AddressFamily.InterNetwork && !IPAddress.Equals(serverAddress, IPAddress.Any))
  97. {
  98. IPAddress subnetMask = NetworkInterfaceHelper.GetSubnetMask(serverAddress);
  99. m_nameServer = new NameServer(serverAddress, subnetMask);
  100. m_nameServer.Start();
  101. }
  102. }
  103. }
  104. catch (Exception ex)
  105. {
  106. MessageBox.Show(ex.Message, "Error");
  107. return;
  108. }
  109. btnStart.Enabled = false;
  110. btnStop.Enabled = true;
  111. comboIPAddress.Enabled = false;
  112. rbtDirectTCPTransport.Enabled = false;
  113. rbtNetBiosOverTCP.Enabled = false;
  114. chkSMB1.Enabled = false;
  115. chkSMB2.Enabled = false;
  116. chkIntegratedWindowsAuthentication.Enabled = false;
  117. }
  118. private XmlDocument GetSettingsXML()
  119. {
  120. string executableDirectory = Path.GetDirectoryName(Application.ExecutablePath) + "\\";
  121. XmlDocument document = GetXmlDocument(executableDirectory + SettingsFileName);
  122. return document;
  123. }
  124. private UserCollection ReadUserSettings()
  125. {
  126. UserCollection users = new UserCollection();
  127. XmlDocument document = GetSettingsXML();
  128. XmlNode usersNode = document.SelectSingleNode("Settings/Users");
  129. foreach (XmlNode userNode in usersNode.ChildNodes)
  130. {
  131. string accountName = userNode.Attributes["AccountName"].Value;
  132. string password = userNode.Attributes["Password"].Value;
  133. users.Add(accountName, password);
  134. }
  135. return users;
  136. }
  137. private ShareCollection ReadShareSettings()
  138. {
  139. ShareCollection shares = new ShareCollection();
  140. XmlDocument document = GetSettingsXML();
  141. XmlNode sharesNode = document.SelectSingleNode("Settings/Shares");
  142. foreach (XmlNode shareNode in sharesNode.ChildNodes)
  143. {
  144. string shareName = shareNode.Attributes["Name"].Value;
  145. string sharePath = shareNode.Attributes["Path"].Value;
  146. XmlNode readAccessNode = shareNode.SelectSingleNode("ReadAccess");
  147. List<string> readAccess = ReadAccessList(readAccessNode);
  148. XmlNode writeAccessNode = shareNode.SelectSingleNode("WriteAccess");
  149. List<string> writeAccess = ReadAccessList(writeAccessNode);
  150. FileSystemShare share = new FileSystemShare(shareName, new DirectoryFileSystem(sharePath));
  151. share.OnAccessRequest += delegate(object sender, AccessRequestArgs args)
  152. {
  153. bool hasReadAccess = Contains(readAccess, "Users") || Contains(readAccess, args.UserName);
  154. bool hasWriteAccess = Contains(writeAccess, "Users") || Contains(writeAccess, args.UserName);
  155. if (args.RequestedAccess == FileAccess.Read)
  156. {
  157. args.Allow = hasReadAccess;
  158. }
  159. else if (args.RequestedAccess == FileAccess.Write)
  160. {
  161. args.Allow = hasWriteAccess;
  162. }
  163. else // FileAccess.ReadWrite
  164. {
  165. args.Allow = hasReadAccess && hasWriteAccess;
  166. }
  167. };
  168. shares.Add(share);
  169. }
  170. return shares;
  171. }
  172. private List<string> ReadAccessList(XmlNode node)
  173. {
  174. List<string> result = new List<string>();
  175. if (node != null)
  176. {
  177. string accounts = node.Attributes["Accounts"].Value;
  178. if (accounts == "*")
  179. {
  180. result.Add("Users");
  181. }
  182. else
  183. {
  184. string[] splitted = accounts.Split(',');
  185. result.AddRange(splitted);
  186. }
  187. }
  188. return result;
  189. }
  190. private void btnStop_Click(object sender, EventArgs e)
  191. {
  192. m_server.Stop();
  193. btnStart.Enabled = true;
  194. btnStop.Enabled = false;
  195. comboIPAddress.Enabled = true;
  196. rbtDirectTCPTransport.Enabled = true;
  197. rbtNetBiosOverTCP.Enabled = true;
  198. chkSMB1.Enabled = true;
  199. chkSMB2.Enabled = true;
  200. chkIntegratedWindowsAuthentication.Enabled = true;
  201. if (m_nameServer != null)
  202. {
  203. m_nameServer.Stop();
  204. }
  205. }
  206. private void Server_OnLogEntry(object sender, LogEntry entry)
  207. {
  208. string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ");
  209. string message = String.Format("{0} {1} {2}", entry.Severity.ToString().PadRight(12), timestamp, entry.Message);
  210. System.Diagnostics.Debug.Print(message);
  211. }
  212. private void chkSMB1_CheckedChanged(object sender, EventArgs e)
  213. {
  214. if (!chkSMB1.Checked)
  215. {
  216. chkSMB2.Checked = true;
  217. }
  218. }
  219. private void chkSMB2_CheckedChanged(object sender, EventArgs e)
  220. {
  221. if (!chkSMB2.Checked)
  222. {
  223. chkSMB1.Checked = true;
  224. }
  225. }
  226. public static XmlDocument GetXmlDocument(string path)
  227. {
  228. XmlDocument doc = new XmlDocument();
  229. doc.Load(path);
  230. return doc;
  231. }
  232. public static bool Contains(List<string> list, string value)
  233. {
  234. return (IndexOf(list, value) >= 0);
  235. }
  236. public static int IndexOf(List<string> list, string value)
  237. {
  238. for (int index = 0; index < list.Count; index++)
  239. {
  240. if (string.Equals(list[index], value, StringComparison.InvariantCultureIgnoreCase))
  241. {
  242. return index;
  243. }
  244. }
  245. return -1;
  246. }
  247. }
  248. }