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
|
// Copyright 2015 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/components/firewall_hole/firewall_hole.h"
#include <fcntl.h>
#include <stdint.h>
#include <unistd.h>
#include <utility>
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/task/single_thread_task_runner.h"
#include "chromeos/dbus/permission_broker/permission_broker_client.h"
namespace chromeos {
namespace {
const char* PortTypeToString(FirewallHole::PortType type) {
switch (type) {
case FirewallHole::PortType::kTcp:
return "TCP";
case FirewallHole::PortType::kUdp:
return "UDP";
}
NOTREACHED();
}
void PortReleased(FirewallHole::PortType type,
uint16_t port,
const std::string& interface,
base::ScopedFD lifeline_fd,
bool success) {
if (!success) {
LOG(WARNING) << "Failed to release firewall hole for "
<< PortTypeToString(type) << " port " << port << " on "
<< interface << ".";
}
}
} // namespace
// static
void FirewallHole::Open(PortType type,
uint16_t port,
const std::string& interface,
OpenCallback callback) {
int lifeline[2] = {-1, -1};
if (pipe2(lifeline, O_CLOEXEC) < 0) {
PLOG(ERROR) << "Failed to create a lifeline pipe";
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), nullptr));
return;
}
base::ScopedFD lifeline_local(lifeline[1]);
base::ScopedFD lifeline_remote(lifeline[0]);
base::OnceCallback<void(bool)> access_granted_closure =
base::BindOnce(&FirewallHole::PortAccessGranted, type, port, interface,
std::move(lifeline_local), std::move(callback));
chromeos::PermissionBrokerClient* client =
chromeos::PermissionBrokerClient::Get();
DCHECK(client) << "Could not get permission broker client.";
switch (type) {
case PortType::kTcp:
client->RequestTcpPortAccess(port, interface, lifeline_remote.get(),
std::move(access_granted_closure));
return;
case PortType::kUdp:
client->RequestUdpPortAccess(port, interface, lifeline_remote.get(),
std::move(access_granted_closure));
return;
}
}
FirewallHole::~FirewallHole() {
base::OnceCallback<void(bool)> port_released_closure = base::BindOnce(
&PortReleased, type_, port_, interface_, std::move(lifeline_fd_));
chromeos::PermissionBrokerClient* client =
chromeos::PermissionBrokerClient::Get();
// See crbug.com/1429368: sometimes the shutdown routine for
// PermissionBrokerClient takes place before firewall holes are released.
if (!client) {
LOG(ERROR) << "Could not get permission broker client.";
return;
}
switch (type_) {
case PortType::kTcp:
client->ReleaseTcpPort(port_, interface_,
std::move(port_released_closure));
return;
case PortType::kUdp:
client->ReleaseUdpPort(port_, interface_,
std::move(port_released_closure));
return;
}
}
void FirewallHole::PortAccessGranted(PortType type,
uint16_t port,
const std::string& interface,
base::ScopedFD lifeline_fd,
FirewallHole::OpenCallback callback,
bool success) {
if (success) {
std::move(callback).Run(base::WrapUnique(
new FirewallHole(type, port, interface, std::move(lifeline_fd))));
} else {
std::move(callback).Run(nullptr);
}
}
FirewallHole::FirewallHole(PortType type,
uint16_t port,
const std::string& interface,
base::ScopedFD lifeline_fd)
: type_(type),
port_(port),
interface_(interface),
lifeline_fd_(std::move(lifeline_fd)) {}
} // namespace chromeos
|