2 * tel-plugin-packetservice
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: DongHoo Park <donghoo.park@samsung.com>
7 * Arun Shukla <arun.shukla@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
25 #include <security-server.h>
27 #include <libxml/xmlmemory.h>
28 #include <libxml/parser.h>
29 #include <libxml/tree.h>
31 #include "ps_common.h"
33 gboolean ps_util_check_access_control(GDBusMethodInvocation *invoc,
34 const char *label, const char *perm)
36 GDBusConnection *conn;
45 conn = g_dbus_method_invocation_get_connection(invoc);
47 warn("access control denied (no connection info)");
51 sender = g_dbus_method_invocation_get_sender(invoc);
53 param = g_variant_new("(s)", sender);
55 warn("access control denied (sender info fail)");
59 result_pid = g_dbus_connection_call_sync(conn, "org.freedesktop.DBus",
60 "/org/freedesktop/DBus",
61 "org.freedesktop.DBus",
62 "GetConnectionUnixProcessID",
64 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
66 warn("access control denied (dbus error: %d(%s))",
67 error->code, error->message);
73 warn("access control denied (fail to get pid)");
77 g_variant_get(result_pid, "(u)", &pid);
78 g_variant_unref(result_pid);
80 dbg("sender: %s pid = %u", sender, pid);
83 * Check for permissions
85 ret = security_server_check_privilege_by_pid(pid, label, perm);
86 if (ret != SECURITY_SERVER_API_SUCCESS)
87 warn("pid(%u) access (%s - %s) denied(%d)", pid, label, perm, ret);
93 g_dbus_method_invocation_return_error(invoc,
95 G_DBUS_ERROR_ACCESS_DENIED,
101 GSource *ps_util_gsource_dispatch(GMainContext *main_context,
102 gint priority, GSourceFunc cb, gpointer data)
104 GSource *request_source = NULL;
106 request_source = g_idle_source_new();
107 g_source_set_callback(request_source, cb, data, NULL);
108 g_source_set_priority(request_source, priority);
109 g_source_attach(request_source, main_context);
111 return request_source;
114 gboolean ps_util_thread_dispatch(GMainContext *main_context,
115 gint priority, GSourceFunc cb, gpointer data)
118 GSource *request_source;
120 if (main_context == NULL || cb == NULL) {
121 err("Failed to dispatch");
128 request_source = ps_util_gsource_dispatch(main_context, priority, cb, data);
129 g_source_unref(request_source);
134 int ps_util_system_command(char *command)
138 const char *environ[] = { NULL };
154 argv[2] = (char *)command;
157 execve("/bin/sh", argv, (char **)environ);
158 err("execve() failed");
162 if (waitpid(pid, &status, 0) == -1) {
166 if (WIFEXITED(status))
167 return WEXITSTATUS(status);
168 else if (WIFSIGNALED(status))
169 return WTERMSIG(status);
170 else if (WIFSTOPPED(status))
171 return WSTOPSIG(status);
173 } while (!WIFEXITED(status) && !WIFSIGNALED(status));
178 void ps_util_load_xml_file(const char *docname,
179 const char *groupname, void **i_doc, void **i_root_node)
181 xmlDocPtr *doc = (xmlDocPtr *)i_doc;
182 xmlNodePtr *root_node = (xmlNodePtr *)i_root_node;
184 dbg("docname: [%s] groupname: [%s]", docname, groupname);
186 *doc = xmlParseFile(docname);
188 *root_node = xmlDocGetRootElement(*doc);
190 dbg("*root_node->name: [%s]", (*root_node)->name);
191 if (0 == xmlStrcmp((*root_node)->name,
192 (const unsigned char *)groupname)) {
193 dbg("root_node is found !!!");
196 err("Cannot find root node.");
205 err("Failed to parse doc: [%s]", docname);
209 void ps_util_unload_xml_file(void **i_doc, void **i_root_node)
211 xmlDocPtr *doc = (xmlDocPtr *)i_doc;
212 xmlNodePtr *root_node = (xmlNodePtr *)i_root_node;
214 dbg("unloading XML");