FileSystemShare.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.Net;
  11. using System.Text;
  12. using Utilities;
  13. namespace SMBLibrary.Server
  14. {
  15. public class AccessRequestArgs : EventArgs
  16. {
  17. public string UserName;
  18. public string Path;
  19. public FileAccess RequestedAccess;
  20. public string MachineName;
  21. public IPEndPoint ClientEndPoint;
  22. public bool Allow = true;
  23. public AccessRequestArgs(string userName, string path, FileAccess requestedAccess, string machineName, IPEndPoint clientEndPoint)
  24. {
  25. UserName = userName;
  26. Path = path;
  27. RequestedAccess = requestedAccess;
  28. MachineName = machineName;
  29. ClientEndPoint = clientEndPoint;
  30. }
  31. }
  32. public class FileSystemShare : ISMBShare
  33. {
  34. private string m_name;
  35. private INTFileStore m_fileSystem;
  36. public event EventHandler<AccessRequestArgs> OnAccessRequest;
  37. public FileSystemShare(string shareName, INTFileStore fileSystem)
  38. {
  39. m_name = shareName;
  40. m_fileSystem = fileSystem;
  41. }
  42. public FileSystemShare(string shareName, IFileSystem fileSystem)
  43. {
  44. m_name = shareName;
  45. m_fileSystem = new NTFileSystemAdapter(fileSystem);
  46. }
  47. public bool HasReadAccess(SecurityContext securityContext, string path)
  48. {
  49. return HasAccess(securityContext, path, FileAccess.Read);
  50. }
  51. public bool HasWriteAccess(SecurityContext securityContext, string path)
  52. {
  53. return HasAccess(securityContext, path, FileAccess.Write);
  54. }
  55. public bool HasAccess(SecurityContext securityContext, string path, FileAccess requestedAccess)
  56. {
  57. // To be thread-safe we must capture the delegate reference first
  58. EventHandler<AccessRequestArgs> handler = OnAccessRequest;
  59. if (handler != null)
  60. {
  61. AccessRequestArgs args = new AccessRequestArgs(securityContext.UserName, path, requestedAccess, securityContext.MachineName, securityContext.ClientEndPoint);
  62. handler(this, args);
  63. return args.Allow;
  64. }
  65. return true;
  66. }
  67. public string Name
  68. {
  69. get
  70. {
  71. return m_name;
  72. }
  73. }
  74. public INTFileStore FileStore
  75. {
  76. get
  77. {
  78. return m_fileSystem;
  79. }
  80. }
  81. }
  82. }