Merge "sensord: delete batch latency/attribute when client is terminated unexpectedly...
[platform/core/system/sensord.git] / src / server / permission_checker.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
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>
28 #include <vector>
29
30 #define CACHE_SIZE 16
31
32 static cynara *cynara_env = NULL;
33
34 static bool check_privilege_by_sockfd(int sock_fd, const char *priv, const char *access)
35 {
36         retvm_if(cynara_env == NULL, false, "Cynara not initialized");
37
38         int ret;
39         int pid = -1;
40         char *client = NULL;
41         char *session = NULL;
42         char *user = NULL;
43
44         retvm_if(cynara_creds_socket_get_pid(sock_fd, &pid) != CYNARA_API_SUCCESS, false, "Getting PID failed");
45
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");
50                 free(client);
51                 free(user);
52                 free(session);
53                 return false;
54         }
55
56         ret = cynara_check(cynara_env, client, session, user, priv);
57
58         free(client);
59         free(session);
60         free(user);
61
62         return (ret == CYNARA_API_ACCESS_ALLOWED);
63 }
64
65 permission_checker::permission_checker()
66 : m_permission_set(0)
67 {
68         init();
69         init_cynara();
70 }
71
72 permission_checker::~permission_checker()
73 {
74         deinit_cynara();
75 }
76
77 permission_checker& permission_checker::get_instance(void)
78 {
79         static permission_checker inst;
80         return inst;
81 }
82
83 void permission_checker::init(void)
84 {
85         m_permission_infos.push_back(std::make_shared<permission_info>(SENSOR_PERMISSION_BIO, "http://tizen.org/privilege/healthinfo", ""));
86
87         std::vector<sensor_base *> sensors;
88         sensors = sensor_loader::get_instance().get_sensors(ALL_SENSOR);
89
90         for (unsigned int i = 0; i < sensors.size(); ++i)
91                 m_permission_set |= sensors[i]->get_permission();
92
93         _I("Permission Set = %d", m_permission_set);
94 }
95
96 void permission_checker::init_cynara(void)
97 {
98         AUTOLOCK(m_mutex);
99
100         cynara_configuration *conf;
101
102         int err = cynara_configuration_create(&conf);
103         retm_if(err != CYNARA_API_SUCCESS, "Failed to create cynara configuration");
104
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);
109                 return;
110         }
111
112         err = cynara_initialize(&cynara_env, conf);
113         cynara_configuration_destroy(conf);
114
115         if (err != CYNARA_API_SUCCESS) {
116                 _E("Failed to initialize cynara");
117                 cynara_env = NULL;
118                 return;
119         }
120
121         _I("Cynara initialized");
122 }
123
124 void permission_checker::deinit_cynara(void)
125 {
126         AUTOLOCK(m_mutex);
127
128         if (cynara_env)
129                 cynara_finish(cynara_env);
130
131         cynara_env = NULL;
132 }
133
134 int permission_checker::get_permission(int sock_fd)
135 {
136         AUTOLOCK(m_mutex);
137
138         int permission = SENSOR_PERMISSION_STANDARD;
139
140         for (unsigned int i = 0; i < m_permission_infos.size(); ++i) {
141                 if (!(m_permission_set & m_permission_infos[i]->permission))
142                         continue;
143
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;
146         }
147
148         return permission;
149 }