4 * Copyright (c) 2014 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 <cynara-client.h>
21 #include <cynara-creds-socket.h>
22 #include <cynara-session.h>
23 #include <permission_checker.h>
24 #include <sensor_common.h>
25 #include <sensor_log.h>
26 #include <sensor_loader.h>
27 #include <sensor_base.h>
32 static cynara *cynara_env = NULL;
34 static bool check_privilege_by_sockfd(int sock_fd, const char *priv, const char *access)
36 retvm_if(cynara_env == NULL, false, "Cynara not initialized");
44 retvm_if(cynara_creds_socket_get_pid(sock_fd, &pid) != CYNARA_API_SUCCESS, false, "Getting PID failed");
46 if (cynara_creds_socket_get_client(sock_fd, CLIENT_METHOD_DEFAULT, &client) != CYNARA_API_SUCCESS ||
47 cynara_creds_socket_get_user(sock_fd, USER_METHOD_DEFAULT, &user) != CYNARA_API_SUCCESS ||
48 (session = cynara_session_from_pid(pid)) == NULL) {
49 _E("Getting client info failed");
56 ret = cynara_check(cynara_env, client, session, user, priv);
62 return (ret == CYNARA_API_ACCESS_ALLOWED);
65 permission_checker::permission_checker()
72 permission_checker::~permission_checker()
77 permission_checker& permission_checker::get_instance(void)
79 static permission_checker inst;
83 void permission_checker::init(void)
85 m_permission_infos.push_back(std::make_shared<permission_info>(SENSOR_PERMISSION_BIO, "http://tizen.org/privilege/healthinfo", ""));
87 std::vector<sensor_base *> sensors;
88 sensors = sensor_loader::get_instance().get_sensors(ALL_SENSOR);
90 for (unsigned int i = 0; i < sensors.size(); ++i)
91 m_permission_set |= sensors[i]->get_permission();
93 _I("Permission Set = %d", m_permission_set);
96 void permission_checker::init_cynara(void)
100 cynara_configuration *conf;
102 int err = cynara_configuration_create(&conf);
103 retm_if(err != CYNARA_API_SUCCESS, "Failed to create cynara configuration");
105 err = cynara_configuration_set_cache_size(conf, CACHE_SIZE);
106 if (err != CYNARA_API_SUCCESS) {
107 _E("Failed to set cynara cache");
108 cynara_configuration_destroy(conf);
112 err = cynara_initialize(&cynara_env, conf);
113 cynara_configuration_destroy(conf);
115 if (err != CYNARA_API_SUCCESS) {
116 _E("Failed to initialize cynara");
121 _I("Cynara initialized");
124 void permission_checker::deinit_cynara(void)
129 cynara_finish(cynara_env);
134 int permission_checker::get_permission(int sock_fd)
138 int permission = SENSOR_PERMISSION_STANDARD;
140 for (unsigned int i = 0; i < m_permission_infos.size(); ++i) {
141 if (!(m_permission_set & m_permission_infos[i]->permission))
144 if (check_privilege_by_sockfd(sock_fd, m_permission_infos[i]->privilege.c_str(), m_permission_infos[i]->access.c_str()))
145 permission |= m_permission_infos[i]->permission;