Fix to apply whitespace coding rules
[platform/core/security/krate.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 <functional>
17
18 #include <aul.h>
19 #include <cynara-client.h>
20 #include <cynara-session.h>
21
22 #include "server.h"
23
24 #include "rmi/manager.h"
25 #include "rmi/app-proxy.h"
26 #include "rmi/package-proxy.h"
27
28 using namespace std::placeholders;
29
30 namespace {
31
32 const std::string KRATE_MANAGER_ADDRESS = "/tmp/.krate.sock";
33
34 std::unique_ptr<Krate::Manager> manager;
35 std::unique_ptr<Krate::AppProxy> appProxy;
36 std::unique_ptr<Krate::PackageProxy> packageProxy;
37
38 } // namespace
39
40 Server::Server()
41 {
42         service.reset(new rmi::Service(KRATE_MANAGER_ADDRESS));
43
44         service->setPrivilegeChecker(std::bind(&Server::checkPeerPrivilege, this, _1, _2));
45
46         service->registerParametricMethod(this, "", (runtime::FileDescriptor)(Server::registerNotificationSubscriber)(std::string));
47         service->registerParametricMethod(this, "", (int)(Server::unregisterNotificationSubscriber)(std::string, int));
48
49         manager.reset(new Krate::Manager(*this));
50         appProxy.reset(new Krate::AppProxy(*this));
51         packageProxy.reset(new Krate::PackageProxy(*this));
52 }
53
54 Server::~Server()
55 {
56 }
57
58 void Server::run()
59 {
60         // Prepare execution environment
61         service->start(true);
62 }
63
64 void Server::terminate()
65 {
66         service->stop();
67 }
68
69 const std::string Server::getPeerPackageId() const
70 {
71         char pkgid[PATH_MAX];
72
73         if (aul_app_get_pkgid_bypid_for_uid(getPeerPid(), pkgid, sizeof(pkgid), getPeerUid()) != AUL_R_OK) {
74                 return "";
75         }
76
77         return pkgid;
78 }
79
80 runtime::FileDescriptor Server::registerNotificationSubscriber(const std::string& name)
81 {
82         return runtime::FileDescriptor(service->subscribeNotification(name), true);
83 }
84
85 int Server::unregisterNotificationSubscriber(const std::string& name, int id)
86 {
87         return service->unsubscribeNotification(name, id);
88 }
89
90 bool Server::checkPeerPrivilege(const rmi::Credentials& cred, const std::string& privilege)
91 {
92         cynara *p_cynara;
93
94         if (privilege.empty()) {
95                 return true;
96         }
97
98         if (::cynara_initialize(&p_cynara, NULL) != CYNARA_API_SUCCESS) {
99                 ERROR("Failure in cynara API");
100                 return false;
101         }
102
103         if (::cynara_check(p_cynara, cred.security.c_str(), "",
104                                            std::to_string(cred.uid).c_str(),
105                                            privilege.c_str()) != CYNARA_API_ACCESS_ALLOWED) {
106                 ::cynara_finish(p_cynara);
107                 ERROR("Access denied: " + cred.security + " : " + privilege);
108                 return false;
109         }
110
111         ::cynara_finish(p_cynara);
112
113         return true;
114 }