1 // Copyright 2014 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "firewall_service.h"
16 
17 #include "dbus_interface.h"
18 #include "iptables.h"
19 
20 namespace firewalld {
21 
FirewallService(brillo::dbus_utils::ExportedObjectManager * object_manager)22 FirewallService::FirewallService(
23     brillo::dbus_utils::ExportedObjectManager* object_manager)
24     : org::chromium::FirewalldAdaptor(&iptables_),
25       dbus_object_{object_manager, object_manager->GetBus(),
26                    org::chromium::FirewalldAdaptor::GetObjectPath()} {}
27 
RegisterAsync(const CompletionAction & callback)28 void FirewallService::RegisterAsync(const CompletionAction& callback) {
29   RegisterWithDBusObject(&dbus_object_);
30 
31 #if !defined(__ANDROID__)
32   // Track permission_broker's lifetime so that we can close firewall holes
33   // if/when permission_broker exits.
34   permission_broker_.reset(
35       new org::chromium::PermissionBroker::ObjectManagerProxy(
36           dbus_object_.GetBus()));
37   permission_broker_->SetPermissionBrokerRemovedCallback(
38       base::Bind(&FirewallService::OnPermissionBrokerRemoved,
39                  weak_ptr_factory_.GetWeakPtr()));
40 #endif  // __ANDROID__
41 
42   dbus_object_.RegisterAsync(callback);
43 }
44 
45 #if !defined(__ANDROID__)
OnPermissionBrokerRemoved(const dbus::ObjectPath & path)46 void FirewallService::OnPermissionBrokerRemoved(const dbus::ObjectPath& path) {
47   LOG(INFO) << "permission_broker died, plugging all firewall holes";
48   iptables_.PlugAllHoles();
49 }
50 #endif  // __ANDROID__
51 
52 }  // namespace firewalld
53