OpenAndXHelper.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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.IO;
  10. using System.Text;
  11. using SMBLibrary.Services;
  12. using SMBLibrary.SMB1;
  13. using Utilities;
  14. namespace SMBLibrary.Server.SMB1
  15. {
  16. internal class OpenAndXHelper
  17. {
  18. internal static SMB1Command GetOpenAndXResponse(SMB1Header header, OpenAndXRequest request, ISMBShare share, SMB1ConnectionState state)
  19. {
  20. SMB1Session session = state.GetSession(header.UID);
  21. bool isExtended = (request.Flags & OpenFlags.SMB_OPEN_EXTENDED_RESPONSE) > 0;
  22. string path = request.FileName;
  23. if (!path.StartsWith(@"\"))
  24. {
  25. path = @"\" + path;
  26. }
  27. AccessMask desiredAccess;
  28. ShareAccess shareAccess;
  29. CreateDisposition createDisposition;
  30. try
  31. {
  32. desiredAccess = ToAccessMask(request.AccessMode.AccessMode);
  33. shareAccess = ToShareAccess(request.AccessMode.SharingMode);
  34. createDisposition = ToCreateDisposition(request.OpenMode);
  35. }
  36. catch (ArgumentException)
  37. {
  38. // Invalid input according to MS-CIFS
  39. header.Status = NTStatus.STATUS_OS2_INVALID_ACCESS;
  40. return new ErrorResponse(request.CommandName);
  41. }
  42. CreateOptions createOptions = ToCreateOptions(request.AccessMode);
  43. FileAccess fileAccess = ToFileAccess(request.AccessMode.AccessMode);
  44. if (share is FileSystemShare)
  45. {
  46. if (!((FileSystemShare)share).HasAccess(session.SecurityContext, path, fileAccess))
  47. {
  48. state.LogToServer(Severity.Verbose, "OpenAndX: Opening '{0}{1}' failed. User '{2}' was denied access.", share.Name, request.FileName, session.UserName);
  49. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  50. return new ErrorResponse(request.CommandName);
  51. }
  52. }
  53. object handle;
  54. FileStatus fileStatus;
  55. header.Status = share.FileStore.CreateFile(out handle, out fileStatus, path, desiredAccess, 0, shareAccess, createDisposition, createOptions, session.SecurityContext);
  56. if (header.Status != NTStatus.STATUS_SUCCESS)
  57. {
  58. state.LogToServer(Severity.Verbose, "OpenAndX: Opening '{0}{1}' failed. NTStatus: {2}.", share.Name, path, header.Status);
  59. return new ErrorResponse(request.CommandName);
  60. }
  61. ushort? fileID = session.AddOpenFile(header.TID, share.Name, path, handle);
  62. if (!fileID.HasValue)
  63. {
  64. share.FileStore.CloseFile(handle);
  65. state.LogToServer(Severity.Verbose, "Create: Opening '{0}{1}' failed. Too many open files.", share.Name, path);
  66. header.Status = NTStatus.STATUS_TOO_MANY_OPENED_FILES;
  67. return new ErrorResponse(request.CommandName);
  68. }
  69. state.LogToServer(Severity.Verbose, "OpenAndX: Opened '{0}{1}'. (UID: {2}, TID: {3}, FID: {4})", share.Name, path, header.UID, header.TID, fileID.Value);
  70. OpenResult openResult = ToOpenResult(fileStatus);
  71. if (share is NamedPipeShare)
  72. {
  73. if (isExtended)
  74. {
  75. return CreateResponseExtendedForNamedPipe(fileID.Value, openResult);
  76. }
  77. else
  78. {
  79. return CreateResponseForNamedPipe(fileID.Value, openResult);
  80. }
  81. }
  82. else // FileSystemShare
  83. {
  84. FileNetworkOpenInformation fileInfo = NTFileStoreHelper.GetNetworkOpenInformation(share.FileStore, handle);
  85. if (isExtended)
  86. {
  87. return CreateResponseExtendedFromFileInfo(fileInfo, fileID.Value, openResult);
  88. }
  89. else
  90. {
  91. return CreateResponseFromFileInfo(fileInfo, fileID.Value, openResult);
  92. }
  93. }
  94. }
  95. private static AccessMask ToAccessMask(AccessMode accessMode)
  96. {
  97. if (accessMode == AccessMode.Read)
  98. {
  99. return FileAccessMask.GENERIC_READ;
  100. }
  101. if (accessMode == AccessMode.Write)
  102. {
  103. return FileAccessMask.GENERIC_WRITE | FileAccessMask.FILE_READ_ATTRIBUTES;
  104. }
  105. else if (accessMode == AccessMode.ReadWrite)
  106. {
  107. return FileAccessMask.GENERIC_READ | FileAccessMask.GENERIC_WRITE;
  108. }
  109. else if (accessMode == AccessMode.Execute)
  110. {
  111. return FileAccessMask.GENERIC_READ | FileAccessMask.GENERIC_EXECUTE;
  112. }
  113. else
  114. {
  115. throw new ArgumentException("Invalid AccessMode value");
  116. }
  117. }
  118. private static FileAccess ToFileAccess(AccessMode accessMode)
  119. {
  120. if (accessMode == AccessMode.Write)
  121. {
  122. return FileAccess.Write;
  123. }
  124. else if (accessMode == AccessMode.ReadWrite)
  125. {
  126. return FileAccess.ReadWrite;
  127. }
  128. else
  129. {
  130. return FileAccess.Read;
  131. }
  132. }
  133. private static ShareAccess ToShareAccess(SharingMode sharingMode)
  134. {
  135. if (sharingMode == SharingMode.Compatibility)
  136. {
  137. return ShareAccess.FILE_SHARE_READ;
  138. }
  139. else if (sharingMode == SharingMode.DenyReadWriteExecute)
  140. {
  141. return 0;
  142. }
  143. else if (sharingMode == SharingMode.DenyWrite)
  144. {
  145. return ShareAccess.FILE_SHARE_READ;
  146. }
  147. else if (sharingMode == SharingMode.DenyReadExecute)
  148. {
  149. return ShareAccess.FILE_SHARE_WRITE;
  150. }
  151. else if (sharingMode == SharingMode.DenyNothing)
  152. {
  153. return ShareAccess.FILE_SHARE_READ | ShareAccess.FILE_SHARE_WRITE;
  154. }
  155. else if (sharingMode == (SharingMode)0xFF)
  156. {
  157. return 0;
  158. }
  159. else
  160. {
  161. throw new ArgumentException("Invalid SharingMode value");
  162. }
  163. }
  164. private static CreateDisposition ToCreateDisposition(OpenMode openMode)
  165. {
  166. if (openMode.CreateFile == CreateFile.ReturnErrorIfNotExist)
  167. {
  168. if (openMode.FileExistsOpts == FileExistsOpts.ReturnError)
  169. {
  170. throw new ArgumentException("Invalid OpenMode combination");
  171. }
  172. else if (openMode.FileExistsOpts == FileExistsOpts.Append)
  173. {
  174. return CreateDisposition.FILE_OPEN;
  175. }
  176. else if (openMode.FileExistsOpts == FileExistsOpts.TruncateToZero)
  177. {
  178. return CreateDisposition.FILE_OVERWRITE;
  179. }
  180. }
  181. else if (openMode.CreateFile == CreateFile.CreateIfNotExist)
  182. {
  183. if (openMode.FileExistsOpts == FileExistsOpts.ReturnError)
  184. {
  185. return CreateDisposition.FILE_CREATE;
  186. }
  187. else if (openMode.FileExistsOpts == FileExistsOpts.Append)
  188. {
  189. return CreateDisposition.FILE_OPEN_IF;
  190. }
  191. else if (openMode.FileExistsOpts == FileExistsOpts.TruncateToZero)
  192. {
  193. return CreateDisposition.FILE_OVERWRITE_IF;
  194. }
  195. }
  196. throw new ArgumentException("Invalid OpenMode combination");
  197. }
  198. private static CreateOptions ToCreateOptions(AccessModeOptions accessModeOptions)
  199. {
  200. CreateOptions result = CreateOptions.FILE_NON_DIRECTORY_FILE | CreateOptions.FILE_COMPLETE_IF_OPLOCKED;
  201. if (accessModeOptions.ReferenceLocality == ReferenceLocality.Sequential)
  202. {
  203. result |= CreateOptions.FILE_SEQUENTIAL_ONLY;
  204. }
  205. else if (accessModeOptions.ReferenceLocality == ReferenceLocality.Random)
  206. {
  207. result |= CreateOptions.FILE_RANDOM_ACCESS;
  208. }
  209. else if (accessModeOptions.ReferenceLocality == ReferenceLocality.RandomWithLocality)
  210. {
  211. result |= CreateOptions.FILE_RANDOM_ACCESS;
  212. }
  213. if (accessModeOptions.CachedMode == CachedMode.DoNotCacheFile)
  214. {
  215. result |= CreateOptions.FILE_NO_INTERMEDIATE_BUFFERING;
  216. }
  217. if (accessModeOptions.WriteThroughMode == WriteThroughMode.WriteThrough)
  218. {
  219. result |= CreateOptions.FILE_WRITE_THROUGH;
  220. }
  221. return result;
  222. }
  223. private static OpenResult ToOpenResult(FileStatus fileStatus)
  224. {
  225. if (fileStatus == FileStatus.FILE_OVERWRITTEN ||
  226. fileStatus == FileStatus.FILE_SUPERSEDED)
  227. {
  228. return OpenResult.FileExistedAndWasTruncated;
  229. }
  230. else if (fileStatus == FileStatus.FILE_CREATED)
  231. {
  232. return OpenResult.NotExistedAndWasCreated;
  233. }
  234. else
  235. {
  236. return OpenResult.FileExistedAndWasOpened;
  237. }
  238. }
  239. private static OpenAndXResponse CreateResponseForNamedPipe(ushort fileID, OpenResult openResult)
  240. {
  241. OpenAndXResponse response = new OpenAndXResponse();
  242. response.FID = fileID;
  243. response.AccessRights = AccessRights.SMB_DA_ACCESS_READ_WRITE;
  244. response.ResourceType = ResourceType.FileTypeMessageModePipe;
  245. response.NMPipeStatus.ICount = 255;
  246. response.NMPipeStatus.ReadMode = ReadMode.MessageMode;
  247. response.NMPipeStatus.NamedPipeType = NamedPipeType.MessageModePipe;
  248. response.OpenResults.OpenResult = openResult;
  249. return response;
  250. }
  251. private static OpenAndXResponseExtended CreateResponseExtendedForNamedPipe(ushort fileID, OpenResult openResult)
  252. {
  253. OpenAndXResponseExtended response = new OpenAndXResponseExtended();
  254. response.FID = fileID;
  255. response.AccessRights = AccessRights.SMB_DA_ACCESS_READ_WRITE;
  256. response.ResourceType = ResourceType.FileTypeMessageModePipe;
  257. response.NMPipeStatus.ICount = 255;
  258. response.NMPipeStatus.ReadMode = ReadMode.MessageMode;
  259. response.NMPipeStatus.NamedPipeType = NamedPipeType.MessageModePipe;
  260. response.OpenResults.OpenResult = openResult;
  261. return response;
  262. }
  263. private static OpenAndXResponse CreateResponseFromFileInfo(FileNetworkOpenInformation fileInfo, ushort fileID, OpenResult openResult)
  264. {
  265. OpenAndXResponse response = new OpenAndXResponse();
  266. response.FID = fileID;
  267. response.FileAttrs = SMB1FileStoreHelper.GetFileAttributes(fileInfo.FileAttributes);
  268. response.LastWriteTime = fileInfo.LastWriteTime;
  269. response.FileDataSize = (uint)Math.Min(UInt32.MaxValue, fileInfo.EndOfFile);
  270. response.AccessRights = AccessRights.SMB_DA_ACCESS_READ;
  271. response.ResourceType = ResourceType.FileTypeDisk;
  272. response.OpenResults.OpenResult = openResult;
  273. return response;
  274. }
  275. private static OpenAndXResponseExtended CreateResponseExtendedFromFileInfo(FileNetworkOpenInformation fileInfo, ushort fileID, OpenResult openResult)
  276. {
  277. OpenAndXResponseExtended response = new OpenAndXResponseExtended();
  278. response.FID = fileID;
  279. response.FileAttrs = SMB1FileStoreHelper.GetFileAttributes(fileInfo.FileAttributes);
  280. response.LastWriteTime = fileInfo.LastWriteTime;
  281. response.FileDataSize = (uint)Math.Min(UInt32.MaxValue, fileInfo.EndOfFile);
  282. response.AccessRights = AccessRights.SMB_DA_ACCESS_READ;
  283. response.ResourceType = ResourceType.FileTypeDisk;
  284. response.OpenResults.OpenResult = openResult;
  285. response.MaximalAccessRights.File = FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA | FileAccessMask.FILE_APPEND_DATA |
  286. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  287. FileAccessMask.FILE_EXECUTE |
  288. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES |
  289. FileAccessMask.DELETE | FileAccessMask.READ_CONTROL | FileAccessMask.WRITE_DAC | FileAccessMask.WRITE_OWNER | FileAccessMask.SYNCHRONIZE;
  290. response.GuestMaximalAccessRights.File = FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA |
  291. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  292. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES |
  293. FileAccessMask.READ_CONTROL | FileAccessMask.SYNCHRONIZE;
  294. return response;
  295. }
  296. }
  297. }