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_log.h>
25 #include <sensor_loader.h>
26 #include <sensor_base.h>
30 static cynara *cynara_env = NULL;
32 static bool check_privilege_by_sockfd(int sock_fd, const char *priv)
34 retvm_if(cynara_env == NULL, false, "Cynara not initialized");
42 retvm_if(cynara_creds_socket_get_pid(sock_fd, &pid) != CYNARA_API_SUCCESS, false, "Getting PID failed");
44 if (cynara_creds_socket_get_client(sock_fd, CLIENT_METHOD_DEFAULT, &client) != CYNARA_API_SUCCESS ||
45 cynara_creds_socket_get_user(sock_fd, USER_METHOD_DEFAULT, &user) != CYNARA_API_SUCCESS ||
46 (session = cynara_session_from_pid(pid)) == NULL) {
47 _E("Getting client info failed");
54 ret = cynara_check(cynara_env, client, session, user, priv);
60 return (ret == CYNARA_API_ACCESS_ALLOWED);
63 permission_checker::permission_checker(void)
69 permission_checker::~permission_checker(void)
74 permission_checker& permission_checker::get_instance(void)
76 static permission_checker inst;
80 void permission_checker::init(void)
84 m_permission_infos.push_back(std::make_shared<permission_info> (SENSOR_PERMISSION_STANDARD, false, ""));
85 m_permission_infos.push_back(std::make_shared<permission_info> (SENSOR_PERMISSION_BIO, true, "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);
98 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(void)
129 cynara_finish(cynara_env);
134 int permission_checker::get_permission(int sock_fd)
138 int permission = SENSOR_PERMISSION_NONE;
140 for (unsigned int i = 0; i < m_permission_infos.size(); ++i) {
141 if (!m_permission_infos[i]->need_to_check) {
142 permission |= m_permission_infos[i]->permission;
143 } else if (m_permission_set & m_permission_infos[i]->permission) {
144 if (check_privilege_by_sockfd(sock_fd, m_permission_infos[i]->privilege.c_str())) {
145 permission |= m_permission_infos[i]->permission;