ServerUI.cs 10.0 KB

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