NTFileStoreHelper.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 Utilities;
  11. namespace SMBLibrary
  12. {
  13. public partial class NTFileStoreHelper
  14. {
  15. public static FileAccess ToCreateFileAccess(AccessMask desiredAccess, CreateDisposition createDisposition)
  16. {
  17. FileAccess result = 0;
  18. if (((FileAccessMask)desiredAccess & FileAccessMask.FILE_READ_DATA) > 0 ||
  19. ((FileAccessMask)desiredAccess & FileAccessMask.FILE_READ_EA) > 0 ||
  20. ((FileAccessMask)desiredAccess & FileAccessMask.FILE_READ_ATTRIBUTES) > 0 ||
  21. (desiredAccess & AccessMask.MAXIMUM_ALLOWED) > 0 ||
  22. (desiredAccess & AccessMask.GENERIC_ALL) > 0 ||
  23. (desiredAccess & AccessMask.GENERIC_READ) > 0)
  24. {
  25. result |= FileAccess.Read;
  26. }
  27. if (((FileAccessMask)desiredAccess & FileAccessMask.FILE_WRITE_DATA) > 0 ||
  28. ((FileAccessMask)desiredAccess & FileAccessMask.FILE_APPEND_DATA) > 0 ||
  29. ((FileAccessMask)desiredAccess & FileAccessMask.FILE_WRITE_EA) > 0 ||
  30. ((FileAccessMask)desiredAccess & FileAccessMask.FILE_WRITE_ATTRIBUTES) > 0 ||
  31. (desiredAccess & AccessMask.DELETE) > 0 ||
  32. (desiredAccess & AccessMask.WRITE_DAC) > 0 ||
  33. (desiredAccess & AccessMask.WRITE_OWNER) > 0 ||
  34. (desiredAccess & AccessMask.MAXIMUM_ALLOWED) > 0 ||
  35. (desiredAccess & AccessMask.GENERIC_ALL) > 0 ||
  36. (desiredAccess & AccessMask.GENERIC_WRITE) > 0)
  37. {
  38. result |= FileAccess.Write;
  39. }
  40. if (((DirectoryAccessMask)desiredAccess & DirectoryAccessMask.FILE_DELETE_CHILD) > 0)
  41. {
  42. result |= FileAccess.Write;
  43. }
  44. // Technically, FILE_OPEN_IF should only require Write access if the file does not exist,
  45. // However, It's uncommon for a client to open a file with FILE_OPEN_IF
  46. // without requesting any kind of write access in the access mask.
  47. // (because [if the file does not exist] an empty file will be created without the ability to write data to the file).
  48. if (createDisposition == CreateDisposition.FILE_CREATE ||
  49. createDisposition == CreateDisposition.FILE_SUPERSEDE ||
  50. createDisposition == CreateDisposition.FILE_OPEN_IF ||
  51. createDisposition == CreateDisposition.FILE_OVERWRITE ||
  52. createDisposition == CreateDisposition.FILE_OVERWRITE_IF)
  53. {
  54. result |= FileAccess.Write;
  55. }
  56. return result;
  57. }
  58. /// <summary>
  59. /// Will return desired FileAccess rights to the file data.
  60. /// </summary>
  61. public static FileAccess ToFileAccess(AccessMask desiredAccess)
  62. {
  63. return ToFileAccess((FileAccessMask)desiredAccess);
  64. }
  65. /// <summary>
  66. /// Will return desired FileAccess rights to the file data.
  67. /// </summary>
  68. public static FileAccess ToFileAccess(FileAccessMask desiredAccess)
  69. {
  70. FileAccess result = 0;
  71. if ((desiredAccess & FileAccessMask.FILE_READ_DATA) > 0 ||
  72. (desiredAccess & FileAccessMask.MAXIMUM_ALLOWED) > 0 ||
  73. (desiredAccess & FileAccessMask.GENERIC_ALL) > 0 ||
  74. (desiredAccess & FileAccessMask.GENERIC_READ) > 0)
  75. {
  76. result |= FileAccess.Read;
  77. }
  78. if ((desiredAccess & FileAccessMask.FILE_WRITE_DATA) > 0 ||
  79. (desiredAccess & FileAccessMask.FILE_APPEND_DATA) > 0 ||
  80. (desiredAccess & FileAccessMask.MAXIMUM_ALLOWED) > 0 ||
  81. (desiredAccess & FileAccessMask.GENERIC_ALL) > 0 ||
  82. (desiredAccess & FileAccessMask.GENERIC_WRITE) > 0)
  83. {
  84. result |= FileAccess.Write;
  85. }
  86. return result;
  87. }
  88. public static FileShare ToFileShare(ShareAccess shareAccess)
  89. {
  90. FileShare result = FileShare.None;
  91. if ((shareAccess & ShareAccess.Read) > 0)
  92. {
  93. result |= FileShare.Read;
  94. }
  95. if ((shareAccess & ShareAccess.Write) > 0)
  96. {
  97. result |= FileShare.Write;
  98. }
  99. if ((shareAccess & ShareAccess.Delete) > 0)
  100. {
  101. result |= FileShare.Delete;
  102. }
  103. return result;
  104. }
  105. public static FileNetworkOpenInformation GetNetworkOpenInformation(INTFileStore fileStore, string path, SecurityContext securityContext)
  106. {
  107. object handle;
  108. FileStatus fileStatus;
  109. NTStatus openStatus = fileStore.CreateFile(out handle, out fileStatus, path, (AccessMask)FileAccessMask.FILE_READ_ATTRIBUTES, 0, ShareAccess.Read | ShareAccess.Write, CreateDisposition.FILE_OPEN, 0, securityContext);
  110. if (openStatus != NTStatus.STATUS_SUCCESS)
  111. {
  112. return null;
  113. }
  114. FileInformation fileInfo;
  115. NTStatus queryStatus = fileStore.GetFileInformation(out fileInfo, handle, FileInformationClass.FileNetworkOpenInformation);
  116. fileStore.CloseFile(handle);
  117. if (queryStatus != NTStatus.STATUS_SUCCESS)
  118. {
  119. return null;
  120. }
  121. return (FileNetworkOpenInformation)fileInfo;
  122. }
  123. public static FileNetworkOpenInformation GetNetworkOpenInformation(INTFileStore fileStore, object handle)
  124. {
  125. FileInformation fileInfo;
  126. NTStatus status = fileStore.GetFileInformation(out fileInfo, handle, FileInformationClass.FileNetworkOpenInformation);
  127. if (status != NTStatus.STATUS_SUCCESS)
  128. {
  129. return null;
  130. }
  131. return (FileNetworkOpenInformation)fileInfo;
  132. }
  133. }
  134. }