ServerUI.cs 7.8 KB

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