ServerUI.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.GSSAPI;
  21. using SMBLibrary.Authentication.NTLM;
  22. using SMBLibrary.Server;
  23. using SMBLibrary.Win32;
  24. using SMBLibrary.Win32.Security;
  25. using Utilities;
  26. namespace SMBServer
  27. {
  28. public partial class ServerUI : Form
  29. {
  30. public const string SettingsFileName = "Settings.xml";
  31. private SMBLibrary.Server.SMBServer m_server;
  32. private SMBLibrary.Server.NameServer m_nameServer;
  33. private LogWriter m_logWriter;
  34. public ServerUI()
  35. {
  36. InitializeComponent();
  37. }
  38. private void ServerUI_Load(object sender, EventArgs e)
  39. {
  40. List<IPAddress> localIPs = NetworkInterfaceHelper.GetHostIPAddresses();
  41. KeyValuePairList<string, IPAddress> list = new KeyValuePairList<string, IPAddress>();
  42. list.Add("Any", IPAddress.Any);
  43. foreach (IPAddress address in localIPs)
  44. {
  45. list.Add(address.ToString(), address);
  46. }
  47. comboIPAddress.DataSource = list;
  48. comboIPAddress.DisplayMember = "Key";
  49. comboIPAddress.ValueMember = "Value";
  50. }
  51. private void btnStart_Click(object sender, EventArgs e)
  52. {
  53. IPAddress serverAddress = (IPAddress)comboIPAddress.SelectedValue;
  54. SMBTransportType transportType;
  55. if (rbtNetBiosOverTCP.Checked)
  56. {
  57. transportType = SMBTransportType.NetBiosOverTCP;
  58. }
  59. else
  60. {
  61. transportType = SMBTransportType.DirectTCPTransport;
  62. }
  63. NTLMAuthenticationProviderBase authenticationMechanism;
  64. if (chkIntegratedWindowsAuthentication.Checked)
  65. {
  66. authenticationMechanism = new IntegratedNTLMAuthenticationProvider();
  67. }
  68. else
  69. {
  70. UserCollection users;
  71. try
  72. {
  73. users = ReadUserSettings();
  74. }
  75. catch
  76. {
  77. MessageBox.Show("Cannot read " + SettingsFileName, "Error");
  78. return;
  79. }
  80. authenticationMechanism = new IndependentNTLMAuthenticationProvider(users.GetUserPassword);
  81. }
  82. SMBShareCollection shares;
  83. try
  84. {
  85. shares = ReadShareSettings();
  86. }
  87. catch (Exception)
  88. {
  89. MessageBox.Show("Cannot read " + SettingsFileName, "Error");
  90. return;
  91. }
  92. GSSProvider securityProvider = new GSSProvider(authenticationMechanism);
  93. m_server = new SMBLibrary.Server.SMBServer(shares, securityProvider);
  94. m_logWriter = new LogWriter();
  95. // The provided logging mechanism will synchronously write to the disk during server activity.
  96. // To maximize server performance, you can disable logging by commenting out the following line.
  97. m_server.LogEntryAdded += new EventHandler<LogEntry>(m_logWriter.OnLogEntryAdded);
  98. try
  99. {
  100. m_server.Start(serverAddress, transportType, chkSMB1.Checked, chkSMB2.Checked);
  101. if (transportType == SMBTransportType.NetBiosOverTCP)
  102. {
  103. if (serverAddress.AddressFamily == AddressFamily.InterNetwork && !IPAddress.Equals(serverAddress, IPAddress.Any))
  104. {
  105. IPAddress subnetMask = NetworkInterfaceHelper.GetSubnetMask(serverAddress);
  106. m_nameServer = new NameServer(serverAddress, subnetMask);
  107. m_nameServer.Start();
  108. }
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. MessageBox.Show(ex.Message, "Error");
  114. return;
  115. }
  116. btnStart.Enabled = false;
  117. btnStop.Enabled = true;
  118. comboIPAddress.Enabled = false;
  119. rbtDirectTCPTransport.Enabled = false;
  120. rbtNetBiosOverTCP.Enabled = false;
  121. chkSMB1.Enabled = false;
  122. chkSMB2.Enabled = false;
  123. chkIntegratedWindowsAuthentication.Enabled = false;
  124. }
  125. private XmlDocument GetSettingsXML()
  126. {
  127. string executableDirectory = Path.GetDirectoryName(Application.ExecutablePath) + "\\";
  128. XmlDocument document = GetXmlDocument(executableDirectory + SettingsFileName);
  129. return document;
  130. }
  131. private UserCollection ReadUserSettings()
  132. {
  133. UserCollection users = new UserCollection();
  134. XmlDocument document = GetSettingsXML();
  135. XmlNode usersNode = document.SelectSingleNode("Settings/Users");
  136. foreach (XmlNode userNode in usersNode.ChildNodes)
  137. {
  138. string accountName = userNode.Attributes["AccountName"].Value;
  139. string password = userNode.Attributes["Password"].Value;
  140. users.Add(accountName, password);
  141. }
  142. return users;
  143. }
  144. private SMBShareCollection ReadShareSettings()
  145. {
  146. SMBShareCollection shares = new SMBShareCollection();
  147. XmlDocument document = GetSettingsXML();
  148. XmlNode sharesNode = document.SelectSingleNode("Settings/Shares");
  149. foreach (XmlNode shareNode in sharesNode.ChildNodes)
  150. {
  151. string shareName = shareNode.Attributes["Name"].Value;
  152. string sharePath = shareNode.Attributes["Path"].Value;
  153. XmlNode readAccessNode = shareNode.SelectSingleNode("ReadAccess");
  154. List<string> readAccess = ReadAccessList(readAccessNode);
  155. XmlNode writeAccessNode = shareNode.SelectSingleNode("WriteAccess");
  156. List<string> writeAccess = ReadAccessList(writeAccessNode);
  157. FileSystemShare share = new FileSystemShare(shareName, new NTDirectoryFileSystem(sharePath));
  158. share.AccessRequested += delegate(object sender, AccessRequestArgs args)
  159. {
  160. bool hasReadAccess = Contains(readAccess, "Users") || Contains(readAccess, args.UserName);
  161. bool hasWriteAccess = Contains(writeAccess, "Users") || Contains(writeAccess, args.UserName);
  162. if (args.RequestedAccess == FileAccess.Read)
  163. {
  164. args.Allow = hasReadAccess;
  165. }
  166. else if (args.RequestedAccess == FileAccess.Write)
  167. {
  168. args.Allow = hasWriteAccess;
  169. }
  170. else // FileAccess.ReadWrite
  171. {
  172. args.Allow = hasReadAccess && hasWriteAccess;
  173. }
  174. };
  175. shares.Add(share);
  176. }
  177. return shares;
  178. }
  179. private List<string> ReadAccessList(XmlNode node)
  180. {
  181. List<string> result = new List<string>();
  182. if (node != null)
  183. {
  184. string accounts = node.Attributes["Accounts"].Value;
  185. if (accounts == "*")
  186. {
  187. result.Add("Users");
  188. }
  189. else
  190. {
  191. string[] splitted = accounts.Split(',');
  192. result.AddRange(splitted);
  193. }
  194. }
  195. return result;
  196. }
  197. private void btnStop_Click(object sender, EventArgs e)
  198. {
  199. m_server.Stop();
  200. m_logWriter.CloseLogFile();
  201. btnStart.Enabled = true;
  202. btnStop.Enabled = false;
  203. comboIPAddress.Enabled = true;
  204. rbtDirectTCPTransport.Enabled = true;
  205. rbtNetBiosOverTCP.Enabled = true;
  206. chkSMB1.Enabled = true;
  207. chkSMB2.Enabled = true;
  208. chkIntegratedWindowsAuthentication.Enabled = true;
  209. if (m_nameServer != null)
  210. {
  211. m_nameServer.Stop();
  212. }
  213. }
  214. private void chkSMB1_CheckedChanged(object sender, EventArgs e)
  215. {
  216. if (!chkSMB1.Checked)
  217. {
  218. chkSMB2.Checked = true;
  219. }
  220. }
  221. private void chkSMB2_CheckedChanged(object sender, EventArgs e)
  222. {
  223. if (!chkSMB2.Checked)
  224. {
  225. chkSMB1.Checked = true;
  226. }
  227. }
  228. public static XmlDocument GetXmlDocument(string path)
  229. {
  230. XmlDocument doc = new XmlDocument();
  231. doc.Load(path);
  232. return doc;
  233. }
  234. public static bool Contains(List<string> list, string value)
  235. {
  236. return (IndexOf(list, value) >= 0);
  237. }
  238. public static int IndexOf(List<string> list, string value)
  239. {
  240. for (int index = 0; index < list.Count; index++)
  241. {
  242. if (string.Equals(list[index], value, StringComparison.OrdinalIgnoreCase))
  243. {
  244. return index;
  245. }
  246. }
  247. return -1;
  248. }
  249. }
  250. }