Add SDcard password confirm popup
[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 "rmi/secure-erase.h"
20 #include "rmi/internal-encryption.h"
21 #include "rmi/external-encryption.h"
22 #include "key-manager/key-generator.h"
23
24 #include "server.h"
25
26 using namespace std::placeholders;
27
28 namespace {
29
30 const std::string ODE_MANAGER_ADDRESS = "/tmp/.ode.sock";
31
32 std::unique_ptr<ode::SecureErase> secureErase;
33 std::unique_ptr<ode::InternalEncryption> internalEncryption;
34 std::unique_ptr<ode::ExternalEncryption> externalEncryption;
35
36 } // namespace
37
38 Server::Server()
39 {
40         service.reset(new rmi::Service(ODE_MANAGER_ADDRESS));
41
42         service->setPrivilegeChecker(std::bind(&Server::checkPeerPrivilege, this, _1, _2));
43
44         secureErase.reset(new ode::SecureErase(*this));
45         internalEncryption.reset(new ode::InternalEncryption(*this));
46         externalEncryption.reset(new ode::ExternalEncryption(*this));
47
48         ode::KeyGenerator::init();
49 }
50
51 Server::~Server()
52 {
53         ode::KeyGenerator::cleanup();
54 }
55
56 void Server::run()
57 {
58         // Prepare execution environment
59         service->start(true);
60 }
61
62 void Server::terminate()
63 {
64         service->stop();
65 }
66
67 bool Server::checkPeerPrivilege(const rmi::Credentials& cred, const std::string& privilege)
68 {
69         cynara *p_cynara;
70
71         if (privilege.empty()) {
72                 return true;
73         }
74
75         if (::cynara_initialize(&p_cynara, NULL) != CYNARA_API_SUCCESS) {
76                 ERROR("Failure in cynara API");
77                 return false;
78         }
79
80         if (::cynara_check(p_cynara, cred.security.c_str(), "",
81                                            std::to_string(cred.uid).c_str(),
82                                            privilege.c_str()) != CYNARA_API_ACCESS_ALLOWED) {
83                 ::cynara_finish(p_cynara);
84                 ERROR("Access denied: " + cred.security + " : " + privilege);
85                 return false;
86         }
87
88         ::cynara_finish(p_cynara);
89
90         return true;
91 }