File: smbfs_mounter.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (245 lines) | stat: -rw-r--r-- 8,628 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/smbfs/smbfs_mounter.h"

#include <utility>

#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/strings/strcat.h"
#include "chromeos/components/mojo_bootstrap/pending_connection_manager.h"
#include "mojo/core/configuration.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/system/platform_handle.h"

namespace smbfs {

namespace {
constexpr char kMountUrlPrefix[] = "smbfs://";
constexpr base::TimeDelta kMountTimeout = base::Seconds(20);
}  // namespace

SmbFsMounter::KerberosOptions::KerberosOptions(Source source,
                                               const std::string& identity)
    : source(source), identity(identity) {
  DCHECK(source == Source::kActiveDirectory || source == Source::kKerberos);
  DCHECK(!identity.empty());
}

SmbFsMounter::KerberosOptions::~KerberosOptions() = default;

SmbFsMounter::MountOptions::MountOptions() = default;

SmbFsMounter::MountOptions::MountOptions(const MountOptions&) = default;

SmbFsMounter::MountOptions::~MountOptions() = default;

SmbFsMounter::SmbFsMounter(const std::string& share_path,
                           const std::string& mount_dir_name,
                           const MountOptions& options,
                           SmbFsHost::Delegate* delegate,
                           ash::disks::DiskMountManager* disk_mount_manager)
    : SmbFsMounter(share_path,
                   mount_dir_name,
                   options,
                   delegate,
                   disk_mount_manager,
                   {}) {}

SmbFsMounter::SmbFsMounter(const std::string& share_path,
                           const std::string& mount_dir_name,
                           const MountOptions& options,
                           SmbFsHost::Delegate* delegate,
                           ash::disks::DiskMountManager* disk_mount_manager,
                           mojo::Remote<mojom::SmbFsBootstrap> bootstrap)
    : share_path_(share_path),
      mount_dir_name_(mount_dir_name),
      options_(options),
      delegate_(delegate),
      disk_mount_manager_(disk_mount_manager),
      token_(base::UnguessableToken::Create()),
      mount_url_(base::StrCat({kMountUrlPrefix, token_.ToString()})),
      bootstrap_(std::move(bootstrap)) {
  DCHECK(delegate_);
  DCHECK(disk_mount_manager_);
}

SmbFsMounter::SmbFsMounter()
    : delegate_(nullptr), disk_mount_manager_(nullptr) {}

SmbFsMounter::~SmbFsMounter() {
  if (mojo_fd_pending_) {
    mojo_bootstrap::PendingConnectionManager::Get()
        .CancelExpectedOpenIpcChannel(token_);
  }
}

void SmbFsMounter::Mount(SmbFsMounter::DoneCallback callback) {
  DCHECK(!callback_);
  DCHECK(callback);
  CHECK(!mojo_fd_pending_);

  callback_ = std::move(callback);

  // If |bootstrap_| is already bound, it was provided by a test subclass.
  if (!bootstrap_) {
    mojo_bootstrap::PendingConnectionManager::Get().ExpectOpenIpcChannel(
        token_,
        base::BindOnce(&SmbFsMounter::OnIpcChannel, base::Unretained(this)));
    mojo_fd_pending_ = true;

    bootstrap_.Bind(mojo::PendingRemote<mojom::SmbFsBootstrap>(
        bootstrap_invitation_.AttachMessagePipe(mojom::kBootstrapPipeName),
        mojom::SmbFsBootstrap::Version_));
  }
  bootstrap_.set_disconnect_handler(
      base::BindOnce(&SmbFsMounter::OnMojoDisconnect, base::Unretained(this)));

  std::vector<std::string> mount_options;
  if (options_.enable_verbose_logging) {
    mount_options.emplace_back("log-level=-2");
  }

  ash::disks::MountPoint::Mount(
      disk_mount_manager_, mount_url_, "" /* source_format */, mount_dir_name_,
      mount_options, ash::MountType::kNetworkStorage,
      ash::MountAccessMode::kReadWrite,
      base::BindOnce(&SmbFsMounter::OnMountDone, weak_factory_.GetWeakPtr()));
  mount_timer_.Start(
      FROM_HERE, kMountTimeout,
      base::BindOnce(&SmbFsMounter::OnMountTimeout, base::Unretained(this)));
}

void SmbFsMounter::OnMountDone(
    ash::MountError error_code,
    std::unique_ptr<ash::disks::MountPoint> mount_point) {
  if (!callback_) {
    // This can happen if the mount timeout expires and the callback is already
    // run with a timeout error.
    return;
  }

  if (error_code != ash::MountError::kSuccess) {
    LOG(WARNING) << "smbfs mount error: " << error_code;
    ProcessMountError(mojom::MountError::kUnknown);
    return;
  }

  DCHECK(mount_point);
  mount_point_ = std::move(mount_point);

  mojom::MountOptionsPtr mount_options = mojom::MountOptions::New();
  mount_options->share_path = share_path_;
  if (options_.resolved_host.IsIPv4()) {
    // TODO(crbug.com/1051291): Support IPv6.
    mount_options->resolved_host = options_.resolved_host;
  }
  mount_options->username = options_.username;
  mount_options->workgroup = options_.workgroup;
  mount_options->allow_ntlm = options_.allow_ntlm;
  mount_options->skip_connect = options_.skip_connect;

  if (options_.save_restore_password) {
    DCHECK_GE(
        options_.password_salt.size(),
        static_cast<size_t>(mojom::CredentialStorageOptions::kMinSaltLength));
    mount_options->credential_storage_options =
        mojom::CredentialStorageOptions::New(options_.account_hash,
                                             options_.password_salt);
  }

  if (options_.kerberos_options) {
    mojom::KerberosConfigPtr kerberos_config = mojom::KerberosConfig::New();
    kerberos_config->source = options_.kerberos_options->source;
    kerberos_config->identity = options_.kerberos_options->identity;
    mount_options->kerberos_config = std::move(kerberos_config);
  } else if (!options_.password.empty()) {
    if (options_.password.size() > mojom::Password::kMaxLength) {
      LOG(WARNING) << "smbfs password too long";
      ProcessMountError(mojom::MountError::kUnknown);
      return;
    }
    int pipe_fds[2];
    CHECK(base::CreateLocalNonBlockingPipe(pipe_fds));
    base::ScopedFD pipe_read_end(pipe_fds[0]);
    base::ScopedFD pipe_write_end(pipe_fds[1]);
    // Write password to pipe.
    CHECK(base::WriteFileDescriptor(pipe_write_end.get(), options_.password));

    mojom::PasswordPtr password = mojom::Password::New();
    password->length = static_cast<int32_t>(options_.password.size());
    password->fd = mojo::WrapPlatformHandle(
        mojo::PlatformHandle(std::move(pipe_read_end)));
    mount_options->password = std::move(password);
  }

  mojo::PendingRemote<mojom::SmbFsDelegate> delegate_remote;
  mojo::PendingReceiver<mojom::SmbFsDelegate> delegate_receiver =
      delegate_remote.InitWithNewPipeAndPassReceiver();

  bootstrap_->MountShare(
      std::move(mount_options), std::move(delegate_remote),
      base::BindOnce(&SmbFsMounter::OnMountShare, base::Unretained(this),
                     std::move(delegate_receiver)));
}

void SmbFsMounter::OnIpcChannel(base::ScopedFD mojo_fd) {
  DCHECK(mojo_fd.is_valid());
  if (!mojo::core::GetConfiguration().is_broker_process) {
    bootstrap_invitation_.set_extra_flags(
        MOJO_SEND_INVITATION_FLAG_SHARE_BROKER);
  }
  mojo::OutgoingInvitation::Send(
      std::move(bootstrap_invitation_), base::kNullProcessHandle,
      mojo::PlatformChannelEndpoint(mojo::PlatformHandle(std::move(mojo_fd))));
  mojo_fd_pending_ = false;
}

void SmbFsMounter::OnMountShare(
    mojo::PendingReceiver<mojom::SmbFsDelegate> delegate_receiver,
    mojom::MountError mount_error,
    mojo::PendingRemote<mojom::SmbFs> smbfs) {
  if (!callback_) {
    return;
  }

  if (mount_error != mojom::MountError::kOk) {
    LOG(WARNING) << "smbfs mount share error: " << mount_error;
    ProcessMountError(mount_error);
    return;
  }

  DCHECK(mount_point_);
  std::unique_ptr<SmbFsHost> host =
      std::make_unique<SmbFsHost>(std::move(mount_point_), delegate_,
                                  mojo::Remote<mojom::SmbFs>(std::move(smbfs)),
                                  std::move(delegate_receiver));
  std::move(callback_).Run(mojom::MountError::kOk, std::move(host));
}

void SmbFsMounter::OnMojoDisconnect() {
  if (!callback_) {
    return;
  }

  LOG(WARNING) << "smbfs bootstrap disconnection";
  ProcessMountError(mojom::MountError::kUnknown);
}

void SmbFsMounter::OnMountTimeout() {
  if (!callback_) {
    return;
  }

  LOG(ERROR) << "smbfs mount timeout";
  ProcessMountError(mojom::MountError::kTimeout);
}

void SmbFsMounter::ProcessMountError(mojom::MountError mount_error) {
  mount_point_.reset();
  std::move(callback_).Run(mount_error, nullptr);
}

}  // namespace smbfs