CreateHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. public 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. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCESS_DENIED);
  30. }
  31. }
  32. object handle;
  33. FileStatus fileStatus;
  34. NTStatus createStatus = share.FileStore.CreateFile(out handle, out fileStatus, path, request.DesiredAccess, request.ShareAccess, request.CreateDisposition, request.CreateOptions, session.SecurityContext);
  35. if (createStatus != NTStatus.STATUS_SUCCESS)
  36. {
  37. return new ErrorResponse(request.CommandName, createStatus);
  38. }
  39. ulong? persistentFileID = session.AddOpenFile(path, handle);
  40. if (!persistentFileID.HasValue)
  41. {
  42. share.FileStore.CloseFile(handle);
  43. return new ErrorResponse(request.CommandName, NTStatus.STATUS_TOO_MANY_OPENED_FILES);
  44. }
  45. if (share is NamedPipeShare)
  46. {
  47. return CreateResponseForNamedPipe(persistentFileID.Value, FileStatus.FILE_OPENED);
  48. }
  49. else
  50. {
  51. FileNetworkOpenInformation fileInfo = NTFileStoreHelper.GetNetworkOpenInformation(share.FileStore, handle);
  52. CreateResponse response = CreateResponseFromFileSystemEntry(fileInfo, persistentFileID.Value, fileStatus);
  53. if (request.RequestedOplockLevel == OplockLevel.Batch)
  54. {
  55. response.OplockLevel = OplockLevel.Batch;
  56. }
  57. return response;
  58. }
  59. }
  60. private static CreateResponse CreateResponseForNamedPipe(ulong persistentFileID, FileStatus fileStatus)
  61. {
  62. CreateResponse response = new CreateResponse();
  63. response.CreateAction = (CreateAction)fileStatus;
  64. response.FileAttributes = FileAttributes.Normal;
  65. response.FileId.Persistent = persistentFileID;
  66. return response;
  67. }
  68. private static CreateResponse CreateResponseFromFileSystemEntry(FileNetworkOpenInformation fileInfo, ulong persistentFileID, FileStatus fileStatus)
  69. {
  70. CreateResponse response = new CreateResponse();
  71. response.CreateAction = (CreateAction)fileStatus;
  72. response.CreationTime = fileInfo.CreationTime;
  73. response.LastWriteTime = fileInfo.LastWriteTime;
  74. response.ChangeTime = fileInfo.LastWriteTime;
  75. response.LastAccessTime = fileInfo.LastAccessTime;
  76. response.AllocationSize = fileInfo.AllocationSize;
  77. response.EndofFile = fileInfo.EndOfFile;
  78. response.FileAttributes = fileInfo.FileAttributes;
  79. response.FileId.Persistent = persistentFileID;
  80. return response;
  81. }
  82. }
  83. }