Fix a memleak
[platform/core/system/peripheral-bus.git] / src / util / peripheral_privilege.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <cynara-creds-gdbus.h>
18 #include <cynara-client.h>
19 #include <cynara-session.h>
20
21 #include "peripheral_privilege.h"
22 #include "peripheral_log.h"
23
24 #define PERIPHERAL_PRIVILEGE "http://tizen.org/privilege/peripheralio"
25
26 #define CACHE_SIZE  100
27
28 static cynara *__cynara;
29
30 void peripheral_privilege_init(void)
31 {
32         int err;
33         cynara_configuration* conf = NULL;
34
35         err = cynara_configuration_create(&conf);
36         RETM_IF(err != CYNARA_API_SUCCESS, "Failed to create cynara configuration");
37
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);
42                 return;
43         }
44
45         err = cynara_initialize(&__cynara, conf);
46         cynara_configuration_destroy(conf);
47         if (err != CYNARA_API_SUCCESS) {
48                 _E("Failed to initialize cynara");
49                 __cynara = NULL;
50                 return;
51         }
52
53         _D("Cynara initialized");
54 }
55
56 void peripheral_privilege_deinit(void)
57 {
58         if (__cynara)
59                 cynara_finish(__cynara);
60
61         _D("Cynara deinitialized");
62 }
63
64 int peripheral_privilege_check(GDBusMethodInvocation *invocation, GDBusConnection *connection)
65 {
66         RETVM_IF(!__cynara, -1, "Cynara does not initialized");
67
68         int ret;
69         int pid;
70         const char *sender;
71         char *session = NULL;
72         char *client = NULL;
73         char *user = NULL;
74
75         sender = g_dbus_method_invocation_get_sender(invocation);
76
77         cynara_creds_gdbus_get_pid(connection, sender, &pid);
78         session = cynara_session_from_pid(pid);
79
80         cynara_creds_gdbus_get_client(connection, sender, CLIENT_METHOD_DEFAULT, &client);
81         cynara_creds_gdbus_get_user(connection, sender, USER_METHOD_DEFAULT, &user);
82
83         if (!session || !client || !user) {
84                 _E("Failed to get client info");
85                 ret = -1;
86                 goto cleanup;
87         }
88
89         ret = cynara_check(__cynara, client, session, user, PERIPHERAL_PRIVILEGE);
90         if (ret != CYNARA_API_ACCESS_ALLOWED) {
91                 _E("Failed to check privilege");
92                 ret = -EACCES;
93                 goto cleanup;
94         }
95
96         ret = 0;
97
98 cleanup:
99         g_free(session);
100         g_free(client);
101         g_free(user);
102
103         return ret;
104 }