smbfs_mounter.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_COMPONENTS_SMBFS_SMBFS_MOUNTER_H_
  5. #define ASH_COMPONENTS_SMBFS_SMBFS_MOUNTER_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "ash/components/disks/disk_mount_manager.h"
  10. #include "ash/components/disks/mount_point.h"
  11. #include "ash/components/smbfs/mojom/smbfs.mojom.h"
  12. #include "ash/components/smbfs/smbfs_host.h"
  13. #include "base/callback.h"
  14. #include "base/component_export.h"
  15. #include "base/files/scoped_file.h"
  16. #include "base/memory/weak_ptr.h"
  17. #include "base/timer/timer.h"
  18. #include "base/unguessable_token.h"
  19. #include "mojo/public/cpp/bindings/remote.h"
  20. #include "mojo/public/cpp/system/invitation.h"
  21. #include "net/base/ip_address.h"
  22. #include "third_party/abseil-cpp/absl/types/optional.h"
  23. namespace smbfs {
  24. // SmbFsMounter is a helper class that is used to mount an instance of smbfs. It
  25. // performs all the actions necessary to start smbfs and initiate a connection
  26. // to the SMB server.
  27. class COMPONENT_EXPORT(SMBFS) SmbFsMounter {
  28. public:
  29. using DoneCallback =
  30. base::OnceCallback<void(mojom::MountError, std::unique_ptr<SmbFsHost>)>;
  31. struct KerberosOptions {
  32. using Source = mojom::KerberosConfig::Source;
  33. KerberosOptions(Source source, const std::string& identity);
  34. ~KerberosOptions();
  35. // Don't allow an invalid options struct to be created.
  36. KerberosOptions() = delete;
  37. Source source;
  38. std::string identity;
  39. };
  40. struct MountOptions {
  41. MountOptions();
  42. MountOptions(const MountOptions&);
  43. ~MountOptions();
  44. // Resolved IP address for share's hostname.
  45. net::IPAddress resolved_host;
  46. // Authentication options.
  47. std::string username;
  48. std::string workgroup;
  49. std::string password;
  50. absl::optional<KerberosOptions> kerberos_options;
  51. // Allow NTLM authentication to be used.
  52. bool allow_ntlm = false;
  53. // Skip attempting to connect to the share.
  54. bool skip_connect = false;
  55. // Have smbfs save/restore the share's password.
  56. bool save_restore_password = false;
  57. std::string account_hash;
  58. std::vector<uint8_t> password_salt;
  59. };
  60. SmbFsMounter(const std::string& share_path,
  61. const std::string& mount_dir_name,
  62. const MountOptions& options,
  63. SmbFsHost::Delegate* delegate,
  64. ash::disks::DiskMountManager* disk_mount_manager);
  65. SmbFsMounter(const SmbFsMounter&) = delete;
  66. SmbFsMounter& operator=(const SmbFsMounter&) = delete;
  67. virtual ~SmbFsMounter();
  68. // Initiate the filesystem mount request, and run |callback| when completed.
  69. // |callback| is guaranteed not to run after |this| is destroyed.
  70. // Must only be called once. Virtual for testing.
  71. virtual void Mount(DoneCallback callback);
  72. protected:
  73. // Additional constructors for tests.
  74. SmbFsMounter();
  75. SmbFsMounter(const std::string& share_path,
  76. const std::string& mount_dir_name,
  77. const MountOptions& options,
  78. SmbFsHost::Delegate* delegate,
  79. ash::disks::DiskMountManager* disk_mount_manager,
  80. mojo::Remote<mojom::SmbFsBootstrap> bootstrap);
  81. private:
  82. // Callback for MountPoint::Mount().
  83. void OnMountDone(ash::MountError error_code,
  84. std::unique_ptr<ash::disks::MountPoint> mount_point);
  85. // Callback for receiving a Mojo bootstrap channel.
  86. void OnIpcChannel(base::ScopedFD mojo_fd);
  87. // Callback for bootstrap Mojo MountShare() method.
  88. void OnMountShare(
  89. mojo::PendingReceiver<mojom::SmbFsDelegate> delegate_receiver,
  90. mojom::MountError mount_error,
  91. mojo::PendingRemote<mojom::SmbFs> smbfs);
  92. // Mojo disconnection handler.
  93. void OnMojoDisconnect();
  94. // Mount timeout handler.
  95. void OnMountTimeout();
  96. // Perform cleanup and run |callback_| with |mount_error|.
  97. void ProcessMountError(mojom::MountError mount_error);
  98. const std::string share_path_;
  99. const std::string mount_dir_name_;
  100. const MountOptions options_;
  101. SmbFsHost::Delegate* const delegate_;
  102. ash::disks::DiskMountManager* const disk_mount_manager_;
  103. const base::UnguessableToken token_;
  104. const std::string mount_url_;
  105. bool mojo_fd_pending_ = false;
  106. base::OneShotTimer mount_timer_;
  107. DoneCallback callback_;
  108. std::unique_ptr<ash::disks::MountPoint> mount_point_;
  109. mojo::OutgoingInvitation bootstrap_invitation_;
  110. mojo::Remote<mojom::SmbFsBootstrap> bootstrap_;
  111. base::WeakPtrFactory<SmbFsMounter> weak_factory_{this};
  112. };
  113. } // namespace smbfs
  114. #endif // ASH_COMPONENTS_SMBFS_SMBFS_MOUNTER_H_