4 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include "permission_checker.h"
22 #include <cynara-client.h>
23 #include <cynara-creds-socket.h>
24 #include <cynara-session.h>
25 #include <sensor_log.h>
26 #include <unordered_map>
30 using namespace sensor;
32 static cynara *cynara_env = NULL;
33 static std::unordered_map<int, std::string> permissions;
35 permission_checker::permission_checker()
40 permission_checker::~permission_checker()
45 void permission_checker::init_cynara(void)
48 cynara_configuration *conf = NULL;
50 err = cynara_configuration_create(&conf);
51 retm_if(err != CYNARA_API_SUCCESS, "Failed to create cynara configuration");
53 err = cynara_configuration_set_cache_size(conf, CACHE_SIZE);
54 if (err != CYNARA_API_SUCCESS) {
55 _E("Failed to set cynara cache");
56 cynara_configuration_destroy(conf);
60 err = cynara_initialize(&cynara_env, conf);
61 cynara_configuration_destroy(conf);
63 if (err != CYNARA_API_SUCCESS) {
64 _E("Failed to initialize cynara");
72 void permission_checker::deinit_cynara(void)
75 cynara_finish(cynara_env);
82 bool permission_checker::has_permission_cynara(int sock_fd, std::string &perm)
84 retvm_if(cynara_env == NULL, false, "Cynara not initialized");
92 retvm_if(cynara_creds_socket_get_pid(sock_fd, &pid) != CYNARA_API_SUCCESS,
93 false, "Failed to get pid");
95 if (cynara_creds_socket_get_client(sock_fd,
96 CLIENT_METHOD_DEFAULT, &client) != CYNARA_API_SUCCESS ||
97 cynara_creds_socket_get_user(sock_fd,
98 USER_METHOD_DEFAULT, &user) != CYNARA_API_SUCCESS ||
99 (session = cynara_session_from_pid(pid)) == NULL) {
100 _E("Failed to get client information");
107 ret = cynara_check(cynara_env, client, session, user, perm.c_str());
113 return (ret == CYNARA_API_ACCESS_ALLOWED);
116 bool permission_checker::has_permission(int sock_fd, std::string &perm)
118 retv_if(perm.empty(), true);
120 return has_permission_cynara(sock_fd, perm);