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