ServerUI.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* Copyright (C) 2014-2018 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 SMBLibrary;
  8. using SMBLibrary.Authentication.GSSAPI;
  9. using SMBLibrary.Authentication.NTLM;
  10. using SMBLibrary.Server;
  11. using SMBLibrary.Win32;
  12. using SMBLibrary.Win32.Security;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Net;
  17. using System.Net.Sockets;
  18. using System.Windows.Forms;
  19. using Utilities;
  20. namespace SMBServer
  21. {
  22. public partial class ServerUI : Form
  23. {
  24. private SMBLibrary.Server.SMBServer m_server;
  25. private SMBLibrary.Server.NameServer m_nameServer;
  26. private LogWriter m_logWriter;
  27. public ServerUI()
  28. {
  29. InitializeComponent();
  30. }
  31. private void ServerUI_Load(object sender, EventArgs e)
  32. {
  33. List<IPAddress> localIPs = NetworkInterfaceHelper.GetHostIPAddresses();
  34. KeyValuePairList<string, IPAddress> list = new KeyValuePairList<string, IPAddress>();
  35. list.Add("Any", IPAddress.Any);
  36. foreach (IPAddress address in localIPs)
  37. {
  38. list.Add(address.ToString(), address);
  39. }
  40. comboIPAddress.DataSource = list;
  41. comboIPAddress.DisplayMember = "Key";
  42. comboIPAddress.ValueMember = "Value";
  43. }
  44. private void btnStart_Click(object sender, EventArgs e)
  45. {
  46. IPAddress serverAddress = (IPAddress)comboIPAddress.SelectedValue;
  47. SMBTransportType transportType;
  48. if (rbtNetBiosOverTCP.Checked)
  49. {
  50. transportType = SMBTransportType.NetBiosOverTCP;
  51. }
  52. else
  53. {
  54. transportType = SMBTransportType.DirectTCPTransport;
  55. }
  56. NTLMAuthenticationProviderBase authenticationMechanism;
  57. if (chkIntegratedWindowsAuthentication.Checked)
  58. {
  59. authenticationMechanism = new IntegratedNTLMAuthenticationProvider();
  60. }
  61. else
  62. {
  63. UserCollection users;
  64. try
  65. {
  66. users = SettingsHelper.ReadUserSettings();
  67. }
  68. catch
  69. {
  70. MessageBox.Show("Cannot read " + SettingsHelper.SettingsFileName, "Error");
  71. return;
  72. }
  73. authenticationMechanism = new IndependentNTLMAuthenticationProvider(users.GetUserPassword);
  74. }
  75. List<ShareSettings> sharesSettings;
  76. try
  77. {
  78. sharesSettings = SettingsHelper.ReadSharesSettings();
  79. }
  80. catch (Exception)
  81. {
  82. MessageBox.Show("Cannot read " + SettingsHelper.SettingsFileName, "Error");
  83. return;
  84. }
  85. SMBShareCollection shares = new SMBShareCollection();
  86. foreach (ShareSettings shareSettings in sharesSettings)
  87. {
  88. FileSystemShare share = InitializeShare(shareSettings);
  89. shares.Add(share);
  90. }
  91. GSSProvider securityProvider = new GSSProvider(authenticationMechanism);
  92. m_server = new SMBLibrary.Server.SMBServer(shares, securityProvider);
  93. m_logWriter = new LogWriter();
  94. // The provided logging mechanism will synchronously write to the disk during server activity.
  95. // To maximize server performance, you can disable logging by commenting out the following line.
  96. //m_server.LogEntryAdded += new EventHandler<LogEntry>(m_logWriter.OnLogEntryAdded);
  97. try
  98. {
  99. m_server.Start(serverAddress, transportType, chkSMB1.Checked, chkSMB2.Checked);
  100. if (transportType == SMBTransportType.NetBiosOverTCP)
  101. {
  102. if (serverAddress.AddressFamily == AddressFamily.InterNetwork && !IPAddress.Equals(serverAddress, IPAddress.Any))
  103. {
  104. IPAddress subnetMask = NetworkInterfaceHelper.GetSubnetMask(serverAddress);
  105. m_nameServer = new NameServer(serverAddress, subnetMask);
  106. m_nameServer.Start();
  107. }
  108. }
  109. }
  110. catch (Exception ex)
  111. {
  112. MessageBox.Show(ex.Message, "Error");
  113. return;
  114. }
  115. btnStart.Enabled = false;
  116. btnStop.Enabled = true;
  117. comboIPAddress.Enabled = false;
  118. rbtDirectTCPTransport.Enabled = false;
  119. rbtNetBiosOverTCP.Enabled = false;
  120. chkSMB1.Enabled = false;
  121. chkSMB2.Enabled = false;
  122. chkIntegratedWindowsAuthentication.Enabled = false;
  123. }
  124. private void btnStop_Click(object sender, EventArgs e)
  125. {
  126. m_server.Stop();
  127. m_logWriter.CloseLogFile();
  128. btnStart.Enabled = true;
  129. btnStop.Enabled = false;
  130. comboIPAddress.Enabled = true;
  131. rbtDirectTCPTransport.Enabled = true;
  132. rbtNetBiosOverTCP.Enabled = true;
  133. chkSMB1.Enabled = true;
  134. chkSMB2.Enabled = true;
  135. chkIntegratedWindowsAuthentication.Enabled = true;
  136. if (m_nameServer != null)
  137. {
  138. m_nameServer.Stop();
  139. }
  140. }
  141. private void chkSMB1_CheckedChanged(object sender, EventArgs e)
  142. {
  143. if (!chkSMB1.Checked)
  144. {
  145. chkSMB2.Checked = true;
  146. }
  147. }
  148. private void chkSMB2_CheckedChanged(object sender, EventArgs e)
  149. {
  150. if (!chkSMB2.Checked)
  151. {
  152. chkSMB1.Checked = true;
  153. }
  154. }
  155. public static FileSystemShare InitializeShare(ShareSettings shareSettings)
  156. {
  157. string shareName = shareSettings.ShareName;
  158. string sharePath = shareSettings.SharePath;
  159. List<string> readAccess = shareSettings.ReadAccess;
  160. List<string> writeAccess = shareSettings.WriteAccess;
  161. FileSystemShare share = new FileSystemShare(shareName, new NTDirectoryFileSystem(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. return share;
  180. }
  181. public static bool Contains(List<string> list, string value)
  182. {
  183. return (IndexOf(list, value) >= 0);
  184. }
  185. public static int IndexOf(List<string> list, string value)
  186. {
  187. for (int index = 0; index < list.Count; index++)
  188. {
  189. if (string.Equals(list[index], value, StringComparison.OrdinalIgnoreCase))
  190. {
  191. return index;
  192. }
  193. }
  194. return -1;
  195. }
  196. private void ServerUI_Shown(object sender, EventArgs e)
  197. {
  198. var conf = SettingsHelper.ReadConfigs();
  199. if (conf.TryGetValue("Listen", out var listen)) comboIPAddress.Text = listen;
  200. if (conf.TryGetValue("Transport", out var transport))
  201. {
  202. if (transport == "DirectTCP") rbtDirectTCPTransport.Checked = true;
  203. if (transport == "NetBIOS") rbtNetBiosOverTCP.Checked = true;
  204. }
  205. if (conf.TryGetValue("EnableSmb1", out var smb1) && bool.TryParse(smb1, out var enableSmb1)) chkSMB1.Checked = enableSmb1;
  206. if (conf.TryGetValue("EnableSmb2", out var smb2) && bool.TryParse(smb2, out var enableSmb2)) chkSMB2.Checked = enableSmb2;
  207. if (conf.TryGetValue("IntegratedAuthentication", out var integratedAuth) && bool.TryParse(integratedAuth, out var enableIntegratedAuth)) chkIntegratedWindowsAuthentication.Checked = enableIntegratedAuth;
  208. if (conf.TryGetValue("AutoStart", out var auto) && bool.TryParse(auto, out var autoStart) && autoStart) btnStart_Click(null, null);
  209. }
  210. }
  211. }