5d6ab6b15d57c9a64671cb9d34ac10d917428ad0
[platform/core/security/ode.git] / server / server.cpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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 #include <cynara-client.h>
17 #include <cynara-session.h>
18
19 #include <klay/audit/logger.h>
20 #include <klay/audit/dlog-sink.h>
21
22 #include "rmi/secure-erase.h"
23 #include "rmi/internal-encryption.h"
24 #include "rmi/external-encryption.h"
25 #include "rmi/extension-encryption.h"
26 #include "key-manager/key-generator.h"
27
28 #include "server.h"
29
30 using namespace std::placeholders;
31
32 namespace {
33
34 const std::string ODE_MANAGER_ADDRESS = "/tmp/.ode.sock";
35
36 std::unique_ptr<ode::SecureErase> secureErase;
37 std::unique_ptr<ode::InternalEncryption> internalEncryption;
38 std::unique_ptr<ode::ExternalEncryption> externalEncryption;
39 std::unique_ptr<ode::ExtensionEncryption> extensionEncryption;
40
41 } // namespace
42
43 Server::Server()
44 {
45         audit::Logger::setBackend(new audit::DlogLogSink());
46         audit::Logger::setTag("ODE");
47
48         INFO("ODE server starting.");
49
50         service.reset(new rmi::Service(ODE_MANAGER_ADDRESS));
51
52         service->setPrivilegeChecker(std::bind(&Server::checkPeerPrivilege, this, _1, _2));
53
54         service->expose(this, "", (runtime::FileDescriptor)(Server::registerNotificationSubscriber)(std::string));
55         service->expose(this, "", (int)(Server::unregisterNotificationSubscriber)(std::string, int));
56
57         secureErase.reset(new ode::SecureErase(*this));
58         internalEncryption.reset(new ode::InternalEncryption(*this));
59         externalEncryption.reset(new ode::ExternalEncryption(*this));
60         extensionEncryption.reset(new ode::ExtensionEncryption(*this));
61
62         ode::KeyGenerator::init();
63 }
64
65 Server::~Server()
66 {
67         ode::KeyGenerator::cleanup();
68 }
69
70 void Server::run()
71 {
72         // Prepare execution environment
73         service->start(true);
74 }
75
76 void Server::terminate()
77 {
78         service->stop();
79 }
80
81 bool Server::checkPeerPrivilege(const rmi::Credentials& cred, const std::string& privilege)
82 {
83         cynara *p_cynara;
84
85         if (privilege.empty()) {
86                 return true;
87         }
88
89         if (::cynara_initialize(&p_cynara, NULL) != CYNARA_API_SUCCESS) {
90                 ERROR("Failure in cynara API");
91                 return false;
92         }
93
94         if (::cynara_check(p_cynara, cred.security.c_str(), "",
95                                            std::to_string(cred.uid).c_str(),
96                                            privilege.c_str()) != CYNARA_API_ACCESS_ALLOWED) {
97                 ::cynara_finish(p_cynara);
98                 ERROR("Access denied: " + cred.security + " : " + privilege);
99                 return false;
100         }
101
102         ::cynara_finish(p_cynara);
103
104         return true;
105 }
106
107 runtime::FileDescriptor Server::registerNotificationSubscriber(const std::string& name)
108 {
109         INFO("registerNotificationSubscriber");
110         INFO(name);
111         return runtime::FileDescriptor(service->subscribeNotification(name), true);
112 }
113
114 int Server::unregisterNotificationSubscriber(const std::string& name, int id)
115 {
116         return service->unsubscribeNotification(name, id);
117 }