2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <cynara-creds-gdbus.h>
18 #include <cynara-client.h>
19 #include <cynara-session.h>
21 #include "peripheral_privilege.h"
22 #include "peripheral_log.h"
24 #define PERIPHERAL_PRIVILEGE "http://tizen.org/privilege/peripheralio"
26 #define CACHE_SIZE 100
28 static cynara *__cynara;
30 void peripheral_privilege_init(void)
33 cynara_configuration* conf = NULL;
35 err = cynara_configuration_create(&conf);
36 RETM_IF(err != CYNARA_API_SUCCESS, "Failed to create cynara configuration");
38 err = cynara_configuration_set_cache_size(conf, CACHE_SIZE);
39 if (err != CYNARA_API_SUCCESS) {
40 _E("Failed to set cynara cache size");
41 cynara_configuration_destroy(conf);
45 err = cynara_initialize(&__cynara, conf);
46 cynara_configuration_destroy(conf);
47 if (err != CYNARA_API_SUCCESS) {
48 _E("Failed to initialize cynara");
53 _D("Cynara initialized");
56 void peripheral_privilege_deinit(void)
59 cynara_finish(__cynara);
61 _D("Cynara deinitialized");
64 int peripheral_privilege_check(GDBusMethodInvocation *invocation, GDBusConnection *connection)
66 RETVM_IF(!__cynara, -1, "Cynara does not initialized");
75 sender = g_dbus_method_invocation_get_sender(invocation);
77 cynara_creds_gdbus_get_pid(connection, sender, &pid);
78 session = cynara_session_from_pid(pid);
80 cynara_creds_gdbus_get_client(connection, sender, CLIENT_METHOD_DEFAULT, &client);
81 cynara_creds_gdbus_get_user(connection, sender, USER_METHOD_DEFAULT, &user);
83 if (!session || !client || !user) {
84 _E("Failed to get client info");
89 ret = cynara_check(__cynara, client, session, user, PERIPHERAL_PRIVILEGE);
90 if (ret != CYNARA_API_ACCESS_ALLOWED) {
91 _E("Failed to check privilege");