1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 * Copyright (C) 2013 Intel Corporation.
6 * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include "dbus-manager.h"
25 #include "common/dbus-manager-glue.h"
26 #include "common/dbus-service-glue.h"
27 #include "common/dbus-error.h"
28 #include "common/log.h"
29 #include "dbus-service.h"
30 #include "dbus-server.h"
35 #include <pkgmgr-info.h>
37 G_DEFINE_TYPE (MsgPortDbusManager, msgport_dbus_manager, G_TYPE_OBJECT)
39 #define MSGPORT_DBUS_MANAGER_GET_PRIV(obj) \
40 G_TYPE_INSTANCE_GET_PRIVATE ((obj), MSGPORT_TYPE_DBUS_MANAGER, MsgPortDbusManagerPrivate)
42 struct _MsgPortDbusManagerPrivate {
43 MsgPortDbusGlueManager *dbus_skeleton;
44 GDBusConnection *connection;
45 MsgPortManager *manager;
46 MsgPortDbusServer *server;
48 gboolean is_null_cert;
49 GHashTable *peer_certs;
54 _dbus_manager_finalize (GObject *self)
56 //MsgPortDbusManager *manager = MSGPORT_DBUS_MANAGER (self);
58 G_OBJECT_CLASS (msgport_dbus_manager_parent_class)->finalize (self);
62 _dbus_manager_dispose (GObject *self)
64 MsgPortDbusManager *dbus_mgr = MSGPORT_DBUS_MANAGER (self);
66 DBG ("Unexporting dbus manager %p on connection %p", dbus_mgr, dbus_mgr->priv->connection);
67 if (dbus_mgr->priv->dbus_skeleton) {
68 g_dbus_interface_skeleton_unexport (
69 G_DBUS_INTERFACE_SKELETON (dbus_mgr->priv->dbus_skeleton));
70 g_clear_object (&dbus_mgr->priv->dbus_skeleton);
73 g_clear_object (&dbus_mgr->priv->connection);
75 /* unregister all services owned by this connection */
76 msgport_manager_unregister_services (dbus_mgr->priv->manager, dbus_mgr, NULL);
78 g_clear_object (&dbus_mgr->priv->manager);
80 if (dbus_mgr->priv->peer_certs) {
81 g_hash_table_unref (dbus_mgr->priv->peer_certs);
82 dbus_mgr->priv->peer_certs = NULL;
85 G_OBJECT_CLASS (msgport_dbus_manager_parent_class)->dispose (self);
90 _dbus_manager_handle_register_service (
91 MsgPortDbusManager *dbus_mgr,
92 GDBusMethodInvocation *invocation,
93 const gchar *port_name,
98 MsgPortDbusService *dbus_service = NULL;
99 msgport_return_val_if_fail (dbus_mgr && MSGPORT_IS_DBUS_MANAGER (dbus_mgr), FALSE);
101 DBG ("register service request from %p for port %s", dbus_mgr, port_name);
103 dbus_service = msgport_manager_register_service (
104 dbus_mgr->priv->manager, dbus_mgr,
105 port_name, is_trusted, &error);
108 msgport_dbus_glue_manager_complete_register_service (
109 dbus_mgr->priv->dbus_skeleton, invocation,
110 msgport_dbus_service_get_object_path(dbus_service));
114 if (!error) error = msgport_error_unknown_new ();
115 g_dbus_method_invocation_take_error (invocation, error);
121 _dbus_manager_handle_check_for_remote_service (
122 MsgPortDbusManager *dbus_mgr,
123 GDBusMethodInvocation *invocation,
124 const gchar *remote_app_id,
125 const gchar *remote_port_name,
129 GError *error = NULL;
130 MsgPortDbusService *dbus_service = NULL;
131 MsgPortDbusManager *remote_dbus_manager = NULL;
133 msgport_return_val_if_fail (dbus_mgr && MSGPORT_IS_DBUS_MANAGER (dbus_mgr), FALSE);
135 DBG ("check remote service request from %p for %s %s %d",
136 dbus_mgr, remote_app_id, remote_port_name, is_trusted);
138 remote_dbus_manager = msgport_dbus_server_get_dbus_manager_by_app_id (
139 dbus_mgr->priv->server, remote_app_id);
141 if (remote_dbus_manager) {
142 dbus_service = msgport_manager_get_service (dbus_mgr->priv->manager,
143 remote_dbus_manager, remote_port_name, is_trusted, &error);
145 DBG ("Found service id : %d", msgport_dbus_service_get_id (dbus_service));
146 msgport_dbus_glue_manager_complete_check_for_remote_service (
147 dbus_mgr->priv->dbus_skeleton, invocation,
148 msgport_dbus_service_get_id (dbus_service));
153 if (!error) error = msgport_error_port_not_found (remote_app_id, remote_port_name);
154 g_dbus_method_invocation_take_error (invocation, error);
160 _dbus_manager_handle_send_message (
161 MsgPortDbusManager *dbus_mgr,
162 GDBusMethodInvocation *invocation,
167 GError *error = NULL;
168 MsgPortDbusService *peer_dbus_service = 0;
170 msgport_return_val_if_fail (dbus_mgr && MSGPORT_IS_DBUS_MANAGER (dbus_mgr), FALSE);
172 DBG ("send_message from %p : %d ", dbus_mgr, service_id);
174 peer_dbus_service = msgport_manager_get_service_by_id (
175 dbus_mgr->priv->manager, service_id, &error);
177 if (peer_dbus_service) {
178 if (msgport_dbus_service_send_message (peer_dbus_service, data, dbus_mgr->priv->app_id, "", FALSE, &error)) {
179 msgport_dbus_glue_manager_complete_send_message (
180 dbus_mgr->priv->dbus_skeleton, invocation);
185 if (!error) error = msgport_error_unknown_new ();
186 g_dbus_method_invocation_take_error (invocation, error);
192 msgport_dbus_manager_class_init (MsgPortDbusManagerClass *klass)
194 GObjectClass *gklass = G_OBJECT_CLASS(klass);
196 g_type_class_add_private (klass, sizeof(MsgPortDbusManagerPrivate));
198 gklass->finalize = _dbus_manager_finalize;
199 gklass->dispose = _dbus_manager_dispose;
203 msgport_dbus_manager_init (MsgPortDbusManager *self)
205 MsgPortDbusManagerPrivate *priv = MSGPORT_DBUS_MANAGER_GET_PRIV (self);
207 priv->dbus_skeleton = msgport_dbus_glue_manager_skeleton_new ();
208 priv->manager = msgport_manager_new ();
209 priv->is_null_cert = FALSE;
210 priv->peer_certs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
212 g_signal_connect_swapped (priv->dbus_skeleton, "handle-register-service",
213 G_CALLBACK (_dbus_manager_handle_register_service), (gpointer)self);
214 g_signal_connect_swapped (priv->dbus_skeleton, "handle-check-for-remote-service",
215 G_CALLBACK (_dbus_manager_handle_check_for_remote_service), (gpointer)self);
216 g_signal_connect_swapped (priv->dbus_skeleton, "handle-send-message",
217 G_CALLBACK (_dbus_manager_handle_send_message), (gpointer)self);
222 _get_app_id_from_connection (GDBusConnection *connection)
225 GError *error = NULL;
226 GCredentials *cred = g_dbus_connection_get_peer_credentials (connection);
228 msgport_return_val_if_fail (cred != NULL, NULL);
231 gchar *str_cred = g_credentials_to_string (cred);
232 DBG ("Client Credentials : %s", str_cred);
236 peer_pid = g_credentials_get_unix_pid (cred, &error);
238 WARN ("Faild to get peer pid on conneciton %p : %s", connection, error->message);
239 g_error_free (error);
246 if ((res = aul_app_get_appid_bypid (peer_pid, app_id, sizeof(app_id))) != AUL_R_OK) {
247 WARN ("Fail to get appid of peer pid '%d', error : %d", peer_pid, res);
250 else g_strdup (app_id);
252 return g_strdup_printf ("%d", peer_pid);
257 msgport_dbus_manager_new (
258 GDBusConnection *connection,
259 MsgPortDbusServer *server,
262 MsgPortDbusManager *dbus_mgr = NULL;
264 dbus_mgr = MSGPORT_DBUS_MANAGER (g_object_new (MSGPORT_TYPE_DBUS_MANAGER, NULL));
266 if (error) *error = msgport_error_new (MSGPORT_ERROR_OUT_OF_MEMORY, "Out of memory");
270 if (!g_dbus_interface_skeleton_export (
271 G_DBUS_INTERFACE_SKELETON (dbus_mgr->priv->dbus_skeleton),
275 WARN ("Failed to export dbus object on connection %p : %s",
276 connection, error ? (*error)->message : "");
277 g_object_unref (dbus_mgr);
280 dbus_mgr->priv->connection = g_object_ref (connection);
281 dbus_mgr->priv->server = server;
282 dbus_mgr->priv->app_id = _get_app_id_from_connection (connection);
288 msgport_dbus_manager_get_manager (MsgPortDbusManager *dbus_manager)
290 msgport_return_val_if_fail (dbus_manager && MSGPORT_IS_DBUS_MANAGER (dbus_manager), NULL);
292 return dbus_manager->priv->manager;
296 msgport_dbus_manager_get_connection (MsgPortDbusManager *dbus_manager)
298 msgport_return_val_if_fail (dbus_manager && MSGPORT_IS_DBUS_MANAGER (dbus_manager), NULL);
300 return dbus_manager->priv->connection;
304 msgport_dbus_manager_get_app_id (MsgPortDbusManager *dbus_manager)
306 msgport_return_val_if_fail (dbus_manager && MSGPORT_IS_DBUS_MANAGER (dbus_manager), NULL);
308 return (const gchar *)dbus_manager->priv->app_id;
312 msgport_dbus_manager_validate_peer_certificate (MsgPortDbusManager *dbus_manager, const gchar *peer_app_id)
315 pkgmgrinfo_cert_compare_result_type_e compare_result;
316 gboolean is_valid_cert = FALSE;
318 /* check if the source application has no certificate info */
319 if (dbus_manager->priv->is_null_cert)
320 return TRUE; /* allow all peers to connect */
322 /* check if we have cached status */
323 if (g_hash_table_contains (dbus_manager->priv->peer_certs, peer_app_id))
324 return ((gboolean)(glong)g_hash_table_lookup (dbus_manager->priv->peer_certs, peer_app_id));
326 if ((res = pkgmgrinfo_pkginfo_compare_app_cert_info (dbus_manager->priv->app_id,
327 peer_app_id, &compare_result)) != PMINFO_R_OK) {
328 WARN ("Fail to compare certificates of applications('%s', '%s') : error %d",
329 dbus_manager->priv->app_id, peer_app_id, res);
333 if (compare_result == PMINFO_CERT_COMPARE_LHS_NO_CERT) {
334 dbus_manager->priv->is_null_cert = TRUE;
338 is_valid_cert = (compare_result == PMINFO_CERT_COMPARE_MATCH) ;
339 g_hash_table_insert (dbus_manager->priv->peer_certs, g_strdup (peer_app_id), (gpointer)is_valid_cert);
341 return is_valid_cert;