Code Sync up from tizen_2.4
[platform/core/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         dbus_info->interface_object = object;
75         dbus_info->invocation = invocation;
76
77         tcore_user_request_set_user_info(ur, dbus_info);
78         tcore_user_request_set_free_hook(ur, _free_hook);
79
80         return ur;
81 }
82
83 gboolean check_access_control (GDBusMethodInvocation *invoc, const char *label, const char *perm)
84 {
85         GDBusConnection *conn;
86         GVariant *result_pid;
87         GVariant *param;
88         GError *error = NULL;
89         const char *sender;
90         unsigned int pid;
91         int ret;
92         int result = FALSE;
93
94         conn = g_dbus_method_invocation_get_connection (invoc);
95         if (!conn) {
96                 warn ("access control denied (no connection info)");
97                 goto OUT;
98         }
99
100         sender = g_dbus_method_invocation_get_sender (invoc);
101
102         param = g_variant_new ("(s)", sender);
103         if (!param) {
104                 warn ("access control denied (sender info fail)");
105                 goto OUT;
106         }
107
108         result_pid = g_dbus_connection_call_sync (conn, "org.freedesktop.DBus",
109                         "/org/freedesktop/DBus",
110                         "org.freedesktop.DBus",
111                         "GetConnectionUnixProcessID",
112                         param, NULL,
113                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
114         if (error) {
115                 warn ("access control denied (dbus error: %d(%s))",
116                                 error->code, error->message);
117                 g_error_free (error);
118                 goto OUT;
119         }
120
121         if (!result_pid) {
122                 warn ("access control denied (fail to get pid)");
123                 goto OUT;
124         }
125
126         g_variant_get (result_pid, "(u)", &pid);
127         g_variant_unref (result_pid);
128
129         dbg ("sender: %s pid = %u", sender, pid);
130
131         ret = security_server_check_privilege_by_pid (pid, label, perm);
132         if (ret != SECURITY_SERVER_API_SUCCESS) {
133                 warn ("pid(%u) access (%s - %s) denied(%d)", pid, label, perm, ret);
134         }
135         else
136                 result = TRUE;
137
138 OUT:
139         if (result == FALSE) {
140                 g_dbus_method_invocation_return_error (invoc,
141                                 G_DBUS_ERROR,
142                                 G_DBUS_ERROR_ACCESS_DENIED,
143                                 "No access rights");
144         }
145         return result;
146 }
147
148 enum dbus_tapi_sim_slot_id get_sim_slot_id_by_cp_name(char *cp_name)
149 {
150         if(g_str_has_suffix(cp_name , "0")){
151                 return SIM_SLOT_PRIMARY;
152         } else if (g_str_has_suffix(cp_name , "1")){
153                 return SIM_SLOT_SECONDARY;
154         } else if(g_str_has_suffix(cp_name , "2")){
155                 return SIM_SLOT_TERTIARY;
156         }
157         return SIM_SLOT_PRIMARY;
158 }
159
160 gboolean dbus_plugin_util_load_xml(char *docname, char *groupname, void **i_doc, void **i_root_node)
161 {
162         xmlDocPtr *doc = (xmlDocPtr *)i_doc;
163         xmlNodePtr *root_node = (xmlNodePtr *)i_root_node;
164
165         dbg("docname:%s, groupname:%s", docname, groupname);
166
167         *doc = xmlParseFile(docname);
168         if (*doc) {
169                 *root_node = xmlDocGetRootElement(*doc);
170                 if (*root_node) {
171                         dbg("*root_node->name:%s", (*root_node)->name);
172                         if (0 == xmlStrcmp((*root_node)->name, (const xmlChar *) groupname)) {
173                                 *root_node = (*root_node)->xmlChildrenNode;
174                                 return TRUE;
175                         } 
176                         *root_node = NULL;
177                 }
178         }
179         xmlFreeDoc(*doc);
180         *doc = NULL;
181         err("Cannot parse doc(%s)", docname);
182         return FALSE;
183 }
184
185 void dbus_plugin_util_unload_xml(void **i_doc, void **i_root_node)
186 {
187         xmlDocPtr *doc = (xmlDocPtr *)i_doc;
188         xmlNodePtr *root_node = (xmlNodePtr *)i_root_node;
189
190         dbg("unloading XML");
191         if (doc && *doc) {
192                 xmlFreeDoc(*doc);
193                 *doc = NULL;
194                 if (root_node)
195                         *root_node = NULL;
196         }
197 }