OpenAndXHelper.cs 14 KB

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