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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
// Copyright 2017 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/dbus/smbprovider/smb_provider_client.h"
#include <memory>
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chromeos/ash/components/dbus/smbprovider/fake_smb_provider_client.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"
namespace ash {
// Extend the timeout for the `GetShares` D-Bus method as share discovery
// has been observed to take longer than 25s (the default D-Bus timeout).
constexpr base::TimeDelta kGetSharesTimeout = base::Minutes(1);
namespace {
SmbProviderClient* g_instance = nullptr;
smbprovider::ErrorType GetErrorFromReader(dbus::MessageReader* reader) {
int32_t int_error;
if (!reader->PopInt32(&int_error) ||
!smbprovider::ErrorType_IsValid(int_error)) {
DLOG(ERROR)
<< "SmbProviderClient: Failed to get an error from the response";
return smbprovider::ERROR_DBUS_PARSE_FAILED;
}
return static_cast<smbprovider::ErrorType>(int_error);
}
smbprovider::ErrorType GetErrorAndProto(
dbus::Response* response,
google::protobuf::MessageLite* protobuf_out) {
if (!response) {
DLOG(ERROR) << "Failed to call smbprovider";
return smbprovider::ERROR_DBUS_PARSE_FAILED;
}
dbus::MessageReader reader(response);
smbprovider::ErrorType error = GetErrorFromReader(&reader);
if (error != smbprovider::ERROR_OK) {
return error;
}
if (!reader.PopArrayOfBytesAsProto(protobuf_out)) {
DLOG(ERROR) << "Failed to parse protobuf.";
return smbprovider::ERROR_DBUS_PARSE_FAILED;
}
return smbprovider::ERROR_OK;
}
class SmbProviderClientImpl final : public SmbProviderClient {
public:
SmbProviderClientImpl() = default;
SmbProviderClientImpl(const SmbProviderClientImpl&) = delete;
SmbProviderClientImpl& operator=(const SmbProviderClientImpl&) = delete;
~SmbProviderClientImpl() override {}
void GetShares(const base::FilePath& server_url,
ReadDirectoryCallback callback) override {
smbprovider::GetSharesOptionsProto options;
options.set_server_url(server_url.value());
CallMethod(smbprovider::kGetSharesMethod, options,
kGetSharesTimeout.InMilliseconds(),
&SmbProviderClientImpl::HandleProtoCallback<
smbprovider::DirectoryEntryListProto>,
&callback);
}
void SetupKerberos(const std::string& account_id,
SetupKerberosCallback callback) override {
dbus::MethodCall method_call(smbprovider::kSmbProviderInterface,
smbprovider::kSetupKerberosMethod);
dbus::MessageWriter writer(&method_call);
writer.AppendString(account_id);
CallMethod(&method_call,
&SmbProviderClientImpl::HandleSetupKerberosCallback, &callback);
}
void ParseNetBiosPacket(const std::vector<uint8_t>& packet,
uint16_t transaction_id,
ParseNetBiosPacketCallback callback) override {
dbus::MethodCall method_call(smbprovider::kSmbProviderInterface,
smbprovider::kParseNetBiosPacketMethod);
dbus::MessageWriter writer(&method_call);
writer.AppendArrayOfBytes(packet);
writer.AppendUint16(transaction_id);
CallMethod(&method_call,
&SmbProviderClientImpl::HandleParseNetBiosPacketCallback,
&callback);
}
base::WeakPtr<SmbProviderClient> AsWeakPtr() override {
return weak_ptr_factory_.GetWeakPtr();
}
// chromeos::DBusClient override.
void Init(dbus::Bus* bus) override {
proxy_ = bus->GetObjectProxy(
smbprovider::kSmbProviderServiceName,
dbus::ObjectPath(smbprovider::kSmbProviderServicePath));
DCHECK(proxy_);
}
private:
// Calls the DBUS method |name|, passing the |protobuf| as an argument.
// |handler| is the member function in this class that receives
// the response and then passes the processed response to |callback|.
template <typename CallbackHandler, typename Callback>
void CallMethod(const char* name,
const google::protobuf::MessageLite& protobuf,
int timeout_ms,
CallbackHandler handler,
Callback callback) {
dbus::MethodCall method_call(smbprovider::kSmbProviderInterface, name);
dbus::MessageWriter writer(&method_call);
writer.AppendProtoAsArrayOfBytes(protobuf);
proxy_->CallMethod(&method_call, timeout_ms,
base::BindOnce(handler, weak_ptr_factory_.GetWeakPtr(),
std::move(*callback)));
}
// Calls the method specified in |method_call|. |handler| is the member
// function in this class that receives the response and then passes the
// processed response to |callback|.
template <typename CallbackHandler, typename Callback>
void CallMethod(dbus::MethodCall* method_call,
CallbackHandler handler,
Callback callback) {
proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(handler, weak_ptr_factory_.GetWeakPtr(),
std::move(*callback)));
}
// Calls the D-Bus method |name|, passing the |protobuf| as an argument.
// Uses the default callback handler to process |callback|.
template <typename Callback>
void CallDefaultMethod(const char* name,
const google::protobuf::MessageLite& protobuf,
Callback callback) {
dbus::MethodCall method_call(smbprovider::kSmbProviderInterface, name);
dbus::MessageWriter writer(&method_call);
writer.AppendProtoAsArrayOfBytes(protobuf);
CallDefaultMethod(&method_call, callback);
}
// Calls the method specified in |method_call|. Uses the default callback
// handler to process |callback|.
template <typename Callback>
void CallDefaultMethod(dbus::MethodCall* method_call, Callback callback) {
proxy_->CallMethod(
method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&SmbProviderClientImpl::HandleDefaultCallback,
weak_ptr_factory_.GetWeakPtr(), method_call->GetMember(),
std::move(*callback)));
}
// Handles D-Bus callback for SetupKerberos.
void HandleSetupKerberosCallback(SetupKerberosCallback callback,
dbus::Response* response) {
if (!response) {
LOG(ERROR) << "SetupKerberos: failed to call smbprovider";
std::move(callback).Run(false /* success */);
return;
}
dbus::MessageReader reader(response);
bool result;
if (!reader.PopBool(&result)) {
LOG(ERROR) << "SetupKerberos: parse failure.";
std::move(callback).Run(false /* success */);
return;
}
std::move(callback).Run(result);
}
void HandleParseNetBiosPacketCallback(ParseNetBiosPacketCallback callback,
dbus::Response* response) {
if (!response) {
LOG(ERROR) << "ParseNetBiosPacket: failed to call smbprovider";
std::move(callback).Run(std::vector<std::string>());
return;
}
dbus::MessageReader reader(response);
smbprovider::HostnamesProto proto;
if (!reader.PopArrayOfBytesAsProto(&proto)) {
LOG(ERROR) << "ParseNetBiosPacket: Failed to parse protobuf.";
std::move(callback).Run(std::vector<std::string>());
return;
}
std::vector<std::string> hostnames(proto.hostnames().begin(),
proto.hostnames().end());
std::move(callback).Run(hostnames);
}
// Default callback handler for D-Bus calls.
void HandleDefaultCallback(const std::string& method_name,
StatusCallback callback,
dbus::Response* response) {
if (!response) {
LOG(ERROR) << method_name << ": failed to call smbprovider";
std::move(callback).Run(smbprovider::ERROR_DBUS_PARSE_FAILED);
return;
}
dbus::MessageReader reader(response);
std::move(callback).Run(GetErrorFromReader(&reader));
}
// Handles D-Bus responses for methods that return an error and a protobuf
// object.
template <class T>
void HandleProtoCallback(base::OnceCallback<void(smbprovider::ErrorType error,
const T& response)> callback,
dbus::Response* response) {
T proto;
smbprovider::ErrorType error(GetErrorAndProto(response, &proto));
std::move(callback).Run(error, proto);
}
raw_ptr<dbus::ObjectProxy> proxy_ = nullptr;
base::WeakPtrFactory<SmbProviderClientImpl> weak_ptr_factory_{this};
};
} // namespace
// static
SmbProviderClient* SmbProviderClient::Get() {
return g_instance;
}
// static
void SmbProviderClient::Initialize(dbus::Bus* bus) {
CHECK(bus);
(new SmbProviderClientImpl())->Init(bus);
}
// static
void SmbProviderClient::InitializeFake() {
(new FakeSmbProviderClient())->Init(nullptr);
}
// static
void SmbProviderClient::Shutdown() {
CHECK(g_instance);
delete g_instance;
}
SmbProviderClient::SmbProviderClient() {
CHECK(!g_instance);
g_instance = this;
}
SmbProviderClient::~SmbProviderClient() {
CHECK_EQ(g_instance, this);
g_instance = nullptr;
}
} // namespace ash
|