1 // Copyright 2015 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 #ifndef WEBSERVER_WEBSERVD_SERVER_H_
16 #define WEBSERVER_WEBSERVD_SERVER_H_
17 
18 #include <map>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include <base/macros.h>
24 #include <base/memory/weak_ptr.h>
25 #include <brillo/dbus/dbus_object.h>
26 #include <brillo/dbus/exported_object_manager.h>
27 #include <brillo/secure_blob.h>
28 
29 #include "dbus_bindings/org.chromium.WebServer.Server.h"
30 #include "webservd/encryptor.h"
31 #include "webservd/firewall_interface.h"
32 #include "webservd/server_interface.h"
33 #include "webservd/temp_file_manager.h"
34 
35 namespace webservd {
36 
37 class DBusProtocolHandler;
38 class DBusServerRequest;
39 
40 // Top-level D-Bus object to interface with the server as a whole.
41 class Server final : public org::chromium::WebServer::ServerInterface,
42                      public ServerInterface {
43  public:
44   Server(brillo::dbus_utils::ExportedObjectManager* object_manager,
45          const Config& config, std::unique_ptr<FirewallInterface> firewall);
46   // Need to off-line the destructor to allow |protocol_handler_map_| to contain
47   // a forward-declared pointer to DBusProtocolHandler.
48   ~Server();
49 
50   void RegisterAsync(
51       const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& cb);
52 
53   // Overrides from org::chromium::WebServer::ServerInterface.
54   std::string Ping() override;
55 
56   // Overrides from webservd::ServerInterface.
57   void ProtocolHandlerStarted(ProtocolHandler* handler) override;
58   void ProtocolHandlerStopped(ProtocolHandler* handler) override;
GetConfig()59   const Config& GetConfig() const override { return config_; }
GetTempFileManager()60   TempFileManager* GetTempFileManager() override { return &temp_file_manager_; }
61 
GetBus()62   scoped_refptr<dbus::Bus> GetBus() { return dbus_object_->GetBus(); }
63 
64   // Allows injection of a non-default |encryptor| (used for testing). The
65   // caller retains ownership of the pointer.
SetEncryptor(Encryptor * encryptor)66   void SetEncryptor(Encryptor* encryptor) {
67     encryptor_ = encryptor;
68   }
69 
70  private:
71   void CreateProtocolHandler(Config::ProtocolHandler* handler_config);
72   void InitTlsData();
73   void OnFirewallServiceOnline();
74   base::FilePath GetUploadDirectory() const;
75 
76   org::chromium::WebServer::ServerAdaptor dbus_adaptor_{this};
77   std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_;
78   std::unique_ptr<Encryptor> default_encryptor_;
79   Encryptor* encryptor_;
80 
81   Config config_;
82   int last_protocol_handler_index_{0};
83   brillo::Blob TLS_certificate_;
84   brillo::Blob TLS_certificate_fingerprint_;
85   brillo::SecureBlob TLS_private_key_;
86 
87   std::map<ProtocolHandler*,
88            std::unique_ptr<DBusProtocolHandler>> protocol_handler_map_;
89   // |protocol_handlers_| is currently used to maintain the lifetime of
90   // ProtocolHandler object instances. When (if) we start to add/remove
91   // protocol handlers dynamically at run-time, it will be used to locate
92   // existing handlers so they can be removed.
93   std::vector<std::unique_ptr<ProtocolHandler>> protocol_handlers_;
94 
95   // The firewall service handler.
96   const std::unique_ptr<FirewallInterface> firewall_;
97 
98   FileDeleter file_deleter_;
99   TempFileManager temp_file_manager_{GetUploadDirectory(), &file_deleter_};
100 
101   base::WeakPtrFactory<Server> weak_ptr_factory_{this};
102   DISALLOW_COPY_AND_ASSIGN(Server);
103 };
104 
105 }  // namespace webservd
106 
107 #endif  // WEBSERVER_WEBSERVD_SERVER_H_
108