tizen 2.3.1 release
[framework/telephony/tel-plugin-dbus_tapi.git] / src / common.c
1 /*
2  * tel-plugin-dbus-tapi
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Ja-young Gu <jygu@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <glib.h>
25 #include <security-server.h>
26
27 #include <tcore.h>
28 #include <plugin.h>
29 #include <communicator.h>
30 #include <server.h>
31 #include <user_request.h>
32
33 #include <libxml/xmlmemory.h>
34 #include <libxml/parser.h>
35 #include <libxml/tree.h>
36
37 #include "generated-code.h"
38 #include "common.h"
39
40
41 static void _free_hook(UserRequest *ur)
42 {
43         struct dbus_request_info *user_info;
44
45         user_info = (struct dbus_request_info *)tcore_user_request_ref_user_info(ur);
46         if (user_info)
47                 free(user_info);
48 }
49
50 char *dbus_plugin_get_cp_name_by_object_path(const char *object_path)
51 {
52         if (!object_path)
53                 return NULL;
54
55         if (!g_str_has_prefix(object_path, MY_DBUS_PATH)) {
56                 return NULL;
57         }
58
59         return (char *)object_path + strlen(MY_DBUS_PATH) + 1;
60 }
61
62 UserRequest *dbus_plugin_macro_user_request_new(struct custom_data *ctx, void *object, GDBusMethodInvocation *invocation)
63 {
64         UserRequest *ur = NULL;
65         char *cp_name;
66         struct dbus_request_info *dbus_info;
67
68         cp_name = GET_CP_NAME(invocation);
69         dbg("cp_name = [%s]", cp_name);
70
71         ur = tcore_user_request_new(ctx->comm, cp_name);
72
73         dbus_info = calloc(1, sizeof(struct dbus_request_info));
74         if (!dbus_info)
75                 return NULL;
76
77         dbus_info->interface_object = object;
78         dbus_info->invocation = invocation;
79
80         tcore_user_request_set_user_info(ur, dbus_info);
81         tcore_user_request_set_free_hook(ur, _free_hook);
82
83         return ur;
84 }
85
86 gboolean check_access_control (GDBusMethodInvocation *invoc, const char *label, const char *perm)
87 {
88         GDBusConnection *conn;
89         GVariant *result_pid;
90         GVariant *param;
91         GError *error = NULL;
92         const char *sender;
93         unsigned int pid;
94         int ret;
95         int result = FALSE;
96
97         conn = g_dbus_method_invocation_get_connection (invoc);
98         if (!conn) {
99                 warn ("access control denied (no connection info)");
100                 goto OUT;
101         }
102
103         sender = g_dbus_method_invocation_get_sender (invoc);
104
105         param = g_variant_new ("(s)", sender);
106         if (!param) {
107                 warn ("access control denied (sender info fail)");
108                 goto OUT;
109         }
110
111         result_pid = g_dbus_connection_call_sync (conn, "org.freedesktop.DBus",
112                         "/org/freedesktop/DBus",
113                         "org.freedesktop.DBus",
114                         "GetConnectionUnixProcessID",
115                         param, NULL,
116                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
117         if (error) {
118                 warn ("access control denied (dbus error: %d(%s))",
119                                 error->code, error->message);
120                 g_error_free (error);
121                 goto OUT;
122         }
123
124         if (!result_pid) {
125                 warn ("access control denied (fail to get pid)");
126                 goto OUT;
127         }
128
129         g_variant_get (result_pid, "(u)", &pid);
130         g_variant_unref (result_pid);
131
132         dbg ("sender: %s pid = %u", sender, pid);
133
134         ret = security_server_check_privilege_by_pid (pid, label, perm);
135         if (ret != SECURITY_SERVER_API_SUCCESS) {
136                 warn ("pid(%u) access (%s - %s) denied(%d)", pid, label, perm, ret);
137         }
138         else
139                 result = TRUE;
140
141 OUT:
142         if (result == FALSE) {
143                 g_dbus_method_invocation_return_error (invoc,
144                                 G_DBUS_ERROR,
145                                 G_DBUS_ERROR_ACCESS_DENIED,
146                                 "No access rights");
147         }
148         return result;
149 }
150
151 enum dbus_tapi_sim_slot_id get_sim_slot_id_by_cp_name(char *cp_name)
152 {
153         if(g_str_has_suffix(cp_name , "0")){
154                 return SIM_SLOT_PRIMARY;
155         } else if (g_str_has_suffix(cp_name , "1")){
156                 return SIM_SLOT_SECONDARY;
157         } else if(g_str_has_suffix(cp_name , "2")){
158                 return SIM_SLOT_TERTIARY;
159         }
160         return SIM_SLOT_PRIMARY;
161 }
162
163 gboolean dbus_plugin_util_load_xml(char *docname, char *groupname, void **i_doc, void **i_root_node)
164 {
165         xmlDocPtr *doc = (xmlDocPtr *)i_doc;
166         xmlNodePtr *root_node = (xmlNodePtr *)i_root_node;
167
168         dbg("docname:%s, groupname:%s", docname, groupname);
169
170         *doc = xmlParseFile(docname);
171         if (*doc) {
172                 *root_node = xmlDocGetRootElement(*doc);
173                 if (*root_node) {
174                         dbg("*root_node->name:%s", (*root_node)->name);
175                         if (0 == xmlStrcmp((*root_node)->name, (const xmlChar *) groupname)) {
176                                 *root_node = (*root_node)->xmlChildrenNode;
177                                 return TRUE;
178                         } 
179                         *root_node = NULL;
180                 }
181         }
182         xmlFreeDoc(*doc);
183         *doc = NULL;
184         err("Cannot parse doc(%s)", docname);
185         return FALSE;
186 }
187
188 void dbus_plugin_util_unload_xml(void **i_doc, void **i_root_node)
189 {
190         xmlDocPtr *doc = (xmlDocPtr *)i_doc;
191         xmlNodePtr *root_node = (xmlNodePtr *)i_root_node;
192
193         dbg("unloading XML");
194         if (doc && *doc) {
195                 xmlFreeDoc(*doc);
196                 *doc = NULL;
197                 if (root_node)
198                         *root_node = NULL;
199         }
200 }