sensorctl: add options/features for usability
[platform/core/system/sensord.git] / src / server / permission_checker.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2017 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 "permission_checker.h"
21
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>
27
28 #define CACHE_SIZE 16
29
30 using namespace sensor;
31
32 static cynara *cynara_env = NULL;
33 static std::unordered_map<int, std::string> permissions;
34
35 permission_checker::permission_checker()
36 {
37         init_cynara();
38 }
39
40 permission_checker::~permission_checker()
41 {
42         deinit_cynara();
43 }
44
45 void permission_checker::init_cynara(void)
46 {
47         int err;
48         cynara_configuration *conf = NULL;
49
50         err = cynara_configuration_create(&conf);
51         retm_if(err != CYNARA_API_SUCCESS, "Failed to create cynara configuration");
52
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);
57                 return;
58         }
59
60         err = cynara_initialize(&cynara_env, conf);
61         cynara_configuration_destroy(conf);
62
63         if (err != CYNARA_API_SUCCESS) {
64                 _E("Failed to initialize cynara");
65                 cynara_env = NULL;
66                 return;
67         }
68
69         _I("Initialized");
70 }
71
72 void permission_checker::deinit_cynara(void)
73 {
74         if (cynara_env) {
75                 cynara_finish(cynara_env);
76                 cynara_env = NULL;
77         }
78
79         _I("Deinitialized");
80 }
81
82 bool permission_checker::has_permission_cynara(int sock_fd, std::string &perm)
83 {
84         retvm_if(cynara_env == NULL, false, "Cynara not initialized");
85
86         int ret;
87         int pid = -1;
88         char *client = NULL;
89         char *session = NULL;
90         char *user = NULL;
91
92         retvm_if(cynara_creds_socket_get_pid(sock_fd, &pid) != CYNARA_API_SUCCESS,
93                         false, "Failed to get pid");
94
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");
101                 free(client);
102                 free(user);
103                 free(session);
104                 return false;
105         }
106
107         ret = cynara_check(cynara_env, client, session, user, perm.c_str());
108
109         free(client);
110         free(session);
111         free(user);
112
113         return (ret == CYNARA_API_ACCESS_ALLOWED);
114 }
115
116 bool permission_checker::has_permission(int sock_fd, std::string &perm)
117 {
118         retv_if(perm.empty(), true);
119
120         return has_permission_cynara(sock_fd, perm);
121 }