OpenAndXHelper.cs 14 KB

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