OpenAndXHelper.cs 13 KB

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