ServerService.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.Text;
  10. using Utilities;
  11. namespace SMBLibrary.Services
  12. {
  13. /// <summary>
  14. /// [MS-SRVS]
  15. /// </summary>
  16. public class ServerService : RemoteService
  17. {
  18. public const string ServicePipeName = @"srvsvc";
  19. public static readonly Guid ServiceInterfaceGuid = new Guid("4B324FC8-1670-01D3-1278-5A47BF6EE188");
  20. public const int ServiceVersion = 3;
  21. public const int MaxPreferredLength = -1; // MAX_PREFERRED_LENGTH
  22. private PlatformName m_platformID;
  23. private string m_serverName;
  24. private uint m_verMajor;
  25. private uint m_verMinor;
  26. private ServerType m_serverType;
  27. private List<string> m_shares;
  28. public ServerService(string serverName, List<string> shares)
  29. {
  30. m_platformID = PlatformName.NT;
  31. m_serverName = serverName;
  32. m_verMajor = 5;
  33. m_verMinor = 2;
  34. m_serverType = ServerType.Workstation | ServerType.Server | ServerType.WindowsNT | ServerType.ServerNT | ServerType.MasterBrowser;
  35. m_shares = shares;
  36. }
  37. public override byte[] GetResponseBytes(ushort opNum, byte[] requestBytes)
  38. {
  39. switch ((ServerServiceOpName)opNum)
  40. {
  41. case ServerServiceOpName.NetrShareEnum:
  42. {
  43. NetrShareEnumRequest request = new NetrShareEnumRequest(requestBytes);
  44. NetrShareEnumResponse response = GetNetrShareEnumResponse(request);
  45. return response.GetBytes();
  46. }
  47. case ServerServiceOpName.NetrShareGetInfo:
  48. {
  49. NetrShareGetInfoRequest request = new NetrShareGetInfoRequest(requestBytes);
  50. NetrShareGetInfoResponse response = GetNetrShareGetInfoResponse(request);
  51. return response.GetBytes();
  52. }
  53. case ServerServiceOpName.NetrServerGetInfo:
  54. {
  55. NetrServerGetInfoRequest request = new NetrServerGetInfoRequest(requestBytes);
  56. NetrServerGetInfoResponse response = GetNetrWkstaGetInfoResponse(request);
  57. return response.GetBytes();
  58. }
  59. default:
  60. throw new NotImplementedException();
  61. }
  62. }
  63. public NetrShareEnumResponse GetNetrShareEnumResponse(NetrShareEnumRequest request)
  64. {
  65. NetrShareEnumResponse response = new NetrShareEnumResponse();
  66. if (request.InfoStruct.Level == 0)
  67. {
  68. // We ignore request.PreferedMaximumLength
  69. ShareInfo0Container info = new ShareInfo0Container();
  70. foreach (string shareName in m_shares)
  71. {
  72. info.Add(new ShareInfo0Entry(shareName));
  73. }
  74. response.InfoStruct = new ShareEnum(info);
  75. response.TotalEntries = (uint)m_shares.Count;
  76. }
  77. else if (request.InfoStruct.Level == 1)
  78. {
  79. // We ignore request.PreferedMaximumLength
  80. ShareInfo1Container info = new ShareInfo1Container();
  81. foreach (string shareName in m_shares)
  82. {
  83. info.Add(new ShareInfo1Entry(shareName, new ShareTypeExtended(ShareType.DiskDrive)));
  84. }
  85. response.InfoStruct = new ShareEnum(info);
  86. response.TotalEntries = (uint)m_shares.Count;
  87. }
  88. else
  89. {
  90. throw new NotImplementedException();
  91. }
  92. response.Result = Win32Error.ERROR_SUCCESS;
  93. return response;
  94. }
  95. public NetrShareGetInfoResponse GetNetrShareGetInfoResponse(NetrShareGetInfoRequest request)
  96. {
  97. int shareIndex = IndexOfShare(request.NetName);
  98. NetrShareGetInfoResponse response = new NetrShareGetInfoResponse();
  99. if (shareIndex == -1)
  100. {
  101. response.InfoStruct = new ShareInfo(request.Level);
  102. response.Result = Win32Error.NERR_NetNameNotFound;
  103. return response;
  104. }
  105. if (request.Level == 0)
  106. {
  107. ShareInfo0Entry info = new ShareInfo0Entry(m_shares[shareIndex]);
  108. response.InfoStruct = new ShareInfo(info);
  109. }
  110. else if (request.Level == 1)
  111. {
  112. ShareInfo1Entry info = new ShareInfo1Entry(m_shares[shareIndex], new ShareTypeExtended(ShareType.DiskDrive));
  113. response.InfoStruct = new ShareInfo(info);
  114. }
  115. else if (request.Level == 2)
  116. {
  117. ShareInfo2Entry info = new ShareInfo2Entry(m_shares[shareIndex], new ShareTypeExtended(ShareType.DiskDrive));
  118. response.InfoStruct = new ShareInfo(info);
  119. }
  120. else
  121. {
  122. throw new NotImplementedException();
  123. }
  124. response.Result = Win32Error.ERROR_SUCCESS;
  125. return response;
  126. }
  127. public NetrServerGetInfoResponse GetNetrWkstaGetInfoResponse(NetrServerGetInfoRequest request)
  128. {
  129. NetrServerGetInfoResponse response = new NetrServerGetInfoResponse();
  130. if (request.Level == 100)
  131. {
  132. ServerInfo100 info = new ServerInfo100();
  133. info.PlatformID = m_platformID;
  134. info.ServerName.Value = m_serverName;
  135. response.InfoStruct = new ServerInfo(info);
  136. }
  137. else if (request.Level == 101)
  138. {
  139. ServerInfo101 info = new ServerInfo101();
  140. info.PlatformID = m_platformID;
  141. info.ServerName.Value = m_serverName;
  142. info.VerMajor = m_verMajor;
  143. info.VerMinor = m_verMinor;
  144. info.Type = m_serverType;
  145. info.Comment.Value = String.Empty;
  146. response.InfoStruct = new ServerInfo(info);
  147. }
  148. else
  149. {
  150. throw new NotImplementedException();
  151. }
  152. response.Result = Win32Error.ERROR_SUCCESS;
  153. return response;
  154. }
  155. private int IndexOfShare(string shareName)
  156. {
  157. for (int index = 0; index < m_shares.Count; index++)
  158. {
  159. if (m_shares[index].Equals(shareName, StringComparison.OrdinalIgnoreCase))
  160. {
  161. return index;
  162. }
  163. }
  164. return -1;
  165. }
  166. public override Guid InterfaceGuid
  167. {
  168. get
  169. {
  170. return ServiceInterfaceGuid;
  171. }
  172. }
  173. public override string PipeName
  174. {
  175. get
  176. {
  177. return ServicePipeName;
  178. }
  179. }
  180. }
  181. }