CreateHelper.cs 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright (C) 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 SMBLibrary.SMB2;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB2
  13. {
  14. internal class CreateHelper
  15. {
  16. internal static SMB2Command GetCreateResponse(CreateRequest request, ISMBShare share, SMB2ConnectionState state)
  17. {
  18. SMB2Session session = state.GetSession(request.Header.SessionID);
  19. string path = request.Name;
  20. if (!path.StartsWith(@"\"))
  21. {
  22. path = @"\" + path;
  23. }
  24. FileAccess createAccess = NTFileStoreHelper.ToCreateFileAccess(request.DesiredAccess, request.CreateDisposition);
  25. if (share is FileSystemShare)
  26. {
  27. if (!((FileSystemShare)share).HasAccess(session.SecurityContext, path, createAccess))
  28. {
  29. state.LogToServer(Severity.Verbose, "Create: Opening '{0}{1}' failed. User '{2}' was denied access.", share.Name, path, session.UserName);
  30. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  31. }
  32. }
  33. object handle;
  34. FileStatus fileStatus;
  35. // GetFileInformation/FileNetworkOpenInformation requires FILE_READ_ATTRIBUTES
  36. AccessMask desiredAccess = request.DesiredAccess | (AccessMask)FileAccessMask.FILE_READ_ATTRIBUTES;
  37. NTStatus createStatus = share.FileStore.CreateFile(out handle, out fileStatus, path, desiredAccess, request.FileAttributes, request.ShareAccess, request.CreateDisposition, request.CreateOptions, session.SecurityContext);
  38. if (createStatus != NTStatus.STATUS_SUCCESS)
  39. {
  40. state.LogToServer(Severity.Verbose, "Create: Opening '{0}{1}' failed. NTStatus: {2}.", share.Name, path, createStatus);
  41. return new ErrorResponse(request.CommandName, createStatus);
  42. }
  43. FileAccess fileAccess = NTFileStoreHelper.ToFileAccess(desiredAccess);
  44. FileID? fileID = session.AddOpenFile(request.Header.TreeID, share.Name, path, handle, fileAccess);
  45. if (fileID == null)
  46. {
  47. share.FileStore.CloseFile(handle);
  48. state.LogToServer(Severity.Verbose, "Create: Opening '{0}{1}' failed. Too many open files.", share.Name, path);
  49. return new ErrorResponse(request.CommandName, NTStatus.STATUS_TOO_MANY_OPENED_FILES);
  50. }
  51. string fileAccessString = fileAccess.ToString().Replace(", ", "|");
  52. string shareAccessString = request.ShareAccess.ToString().Replace(", ", "|");
  53. state.LogToServer(Severity.Verbose, "Create: Opened '{0}{1}', FileAccess: {2}, ShareAccess: {3}. (SessionID: {4}, TreeID: {5}, FileId: {6})", share.Name, path, fileAccessString, shareAccessString, request.Header.SessionID, request.Header.TreeID, fileID.Value.Volatile);
  54. if (share is NamedPipeShare)
  55. {
  56. return CreateResponseForNamedPipe(fileID.Value, FileStatus.FILE_OPENED);
  57. }
  58. else
  59. {
  60. FileNetworkOpenInformation fileInfo = NTFileStoreHelper.GetNetworkOpenInformation(share.FileStore, handle);
  61. CreateResponse response = CreateResponseFromFileSystemEntry(fileInfo, fileID.Value, fileStatus);
  62. return response;
  63. }
  64. }
  65. private static CreateResponse CreateResponseForNamedPipe(FileID fileID, FileStatus fileStatus)
  66. {
  67. CreateResponse response = new CreateResponse();
  68. response.CreateAction = (CreateAction)fileStatus;
  69. response.FileAttributes = FileAttributes.Normal;
  70. response.FileId = fileID;
  71. return response;
  72. }
  73. private static CreateResponse CreateResponseFromFileSystemEntry(FileNetworkOpenInformation fileInfo, FileID fileID, FileStatus fileStatus)
  74. {
  75. CreateResponse response = new CreateResponse();
  76. response.CreateAction = (CreateAction)fileStatus;
  77. response.CreationTime = fileInfo.CreationTime;
  78. response.LastWriteTime = fileInfo.LastWriteTime;
  79. response.ChangeTime = fileInfo.LastWriteTime;
  80. response.LastAccessTime = fileInfo.LastAccessTime;
  81. response.AllocationSize = fileInfo.AllocationSize;
  82. response.EndofFile = fileInfo.EndOfFile;
  83. response.FileAttributes = fileInfo.FileAttributes;
  84. response.FileId = fileID;
  85. return response;
  86. }
  87. }
  88. }