Add AccessController Class
[platform/core/appfw/rpc-port.git] / src / ac-internal.cc
1 /*
2  * Copyright (c) 2017 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
17 #ifndef _GNU_SOURCE
18 #define _GNU_SOURCE
19 #endif
20
21 #include <dlog.h>
22 #include <cynara-error.h>
23 #include <cynara-creds-gdbus.h>
24
25 #include "ac-internal.h"
26
27 #ifdef LOG_TAG
28 #undef LOG_TAG
29 #endif
30
31 #define LOG_TAG "RPC_PORT"
32
33 namespace rpc_port {
34 namespace internal {
35
36 AccessController::~AccessController() {}
37
38 void AccessController::AddPrivilege(const std::string& privilege) {
39   privileges_.push_back(privilege);
40 }
41
42 int AccessController::CheckPrivilege(GDBusConnection *connection, const char* sender_appid) {
43   Cynara c;
44
45   if (c.FetchCredsFromDBus(connection, sender_appid) != 0 )
46     return -1;
47
48   for (auto& privilege : privileges_) {
49     if (c.Check(privilege) != 0) {
50       return -1;
51     }
52   }
53
54   return 0;
55 }
56
57 int AccessController::SetCache(const std::string& sender) {
58   return -1;
59 }
60
61 AccessController::Cynara::Cynara() {
62   cynara_ = nullptr;
63   client_ = nullptr;
64   user_ = nullptr;
65
66   if (cynara_initialize(&cynara_, NULL) != CYNARA_API_SUCCESS) {
67     LOGE("cynara_initialize() is failed");
68   }
69 }
70
71 AccessController::Cynara::~Cynara() {
72   if (client_)
73     free(client_);
74   if (user_)
75     free(user_);
76   if (cynara_)
77     cynara_finish(cynara_);
78 }
79
80 int AccessController::Cynara::FetchCredsFromDBus(GDBusConnection *connection, const char *sender_appid) {
81   int ret;
82
83   if (client_) {
84     free(client_);
85      client_ = nullptr;
86   }
87
88   if (user_) {
89     free(user_);
90     user_ = nullptr;
91   }
92
93   ret = cynara_creds_gdbus_get_user(connection, sender_appid, USER_METHOD_DEFAULT, &user_);
94   if (ret != CYNARA_API_SUCCESS) {
95     LOGE("cynara_creds_gdbus_get_user() is failed : %d", ret);
96     return -1;
97   }
98
99   ret = cynara_creds_gdbus_get_client(connection, sender_appid, CLIENT_METHOD_DEFAULT, &client_);
100   if (ret != CYNARA_API_SUCCESS) {
101     LOGE("cynara_creds_gdbus_get_client() is failed : %d", ret);
102     return -1;
103   }
104
105   LOGD("cred client : %s, cred user : %s", client_, user_);
106   return 0;
107 }
108
109 int AccessController::Cynara::Check(const std::string& privilege) {
110   LOGD("check %s", privilege.c_str());
111   if (cynara_check(cynara_, client_, "", user_, privilege.c_str()) != CYNARA_API_ACCESS_ALLOWED) {
112     LOGE("cynara_check() is failed : %s", privilege.c_str());
113     return -1;
114   }
115
116   return 0;
117 }
118
119 }  // namespace internal
120 }  // namespace rpc_port