1//
2// Copyright (C) 2015 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#include <sysexits.h>
18#include <string>
19
20#include <base/command_line.h>
21#include <brillo/daemons/dbus_daemon.h>
22#include <brillo/dbus/async_event_sequencer.h>
23#include <brillo/minijail/minijail.h>
24#include <brillo/syslog_logging.h>
25#include <brillo/userdb_utils.h>
26
27#include "tpm_manager/common/tpm_manager_constants.h"
28#include "tpm_manager/server/dbus_service.h"
29#include "tpm_manager/server/local_data_store_impl.h"
30#include "tpm_manager/server/tpm_manager_service.h"
31
32#if USE_TPM2
33#include "tpm_manager/server/tpm2_initializer_impl.h"
34#include "tpm_manager/server/tpm2_nvram_impl.h"
35#include "tpm_manager/server/tpm2_status_impl.h"
36#else
37#include "tpm_manager/server/tpm_initializer_impl.h"
38#include "tpm_manager/server/tpm_nvram_impl.h"
39#include "tpm_manager/server/tpm_status_impl.h"
40#endif
41
42using brillo::dbus_utils::AsyncEventSequencer;
43
44namespace {
45
46const char kWaitForOwnershipTriggerSwitch[] = "wait_for_ownership_trigger";
47
48class TpmManagerDaemon : public brillo::DBusServiceDaemon {
49 public:
50  TpmManagerDaemon()
51      : brillo::DBusServiceDaemon(tpm_manager::kTpmManagerServiceName) {
52    base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
53    local_data_store_.reset(new tpm_manager::LocalDataStoreImpl());
54#if USE_TPM2
55    tpm_status_.reset(new tpm_manager::Tpm2StatusImpl);
56    tpm_initializer_.reset(new tpm_manager::Tpm2InitializerImpl(
57        local_data_store_.get(),
58        tpm_status_.get()));
59    tpm_nvram_.reset(new tpm_manager::Tpm2NvramImpl(local_data_store_.get()));
60#else
61    tpm_status_.reset(new tpm_manager::TpmStatusImpl);
62    tpm_initializer_.reset(new tpm_manager::TpmInitializerImpl(
63        local_data_store_.get(),
64        tpm_status_.get()));
65    tpm_nvram_.reset(new tpm_manager::TpmNvramImpl(local_data_store_.get()));
66#endif
67    tpm_manager_service_.reset(new tpm_manager::TpmManagerService(
68        command_line->HasSwitch(kWaitForOwnershipTriggerSwitch),
69        local_data_store_.get(),
70        tpm_status_.get(),
71        tpm_initializer_.get(),
72        tpm_nvram_.get()));
73  }
74
75 protected:
76  int OnInit() override {
77    int result = brillo::DBusServiceDaemon::OnInit();
78    if (result != EX_OK) {
79      LOG(ERROR) << "Error starting tpm_manager dbus daemon.";
80      return result;
81    }
82    CHECK(tpm_manager_service_->Initialize());
83    return EX_OK;
84  }
85
86  void RegisterDBusObjectsAsync(AsyncEventSequencer* sequencer) override {
87    dbus_service_.reset(new tpm_manager::DBusService(
88        bus_, tpm_manager_service_.get(), tpm_manager_service_.get()));
89    dbus_service_->Register(sequencer->GetHandler("Register() failed.", true));
90  }
91
92 private:
93  std::unique_ptr<tpm_manager::LocalDataStore> local_data_store_;
94  std::unique_ptr<tpm_manager::TpmStatus> tpm_status_;
95  std::unique_ptr<tpm_manager::TpmInitializer> tpm_initializer_;
96  std::unique_ptr<tpm_manager::TpmNvram> tpm_nvram_;
97  std::unique_ptr<tpm_manager::TpmManagerService> tpm_manager_service_;
98  std::unique_ptr<tpm_manager::DBusService> dbus_service_;
99
100  DISALLOW_COPY_AND_ASSIGN(TpmManagerDaemon);
101};
102
103}  // namespace
104
105int main(int argc, char* argv[]) {
106  base::CommandLine::Init(argc, argv);
107  base::CommandLine *cl = base::CommandLine::ForCurrentProcess();
108  int flags = brillo::kLogToSyslog;
109  if (cl->HasSwitch("log_to_stderr")) {
110    flags |= brillo::kLogToStderr;
111  }
112  brillo::InitLog(flags);
113  TpmManagerDaemon daemon;
114  LOG(INFO) << "TpmManager Daemon Started.";
115  return daemon.Run();
116}
117