76b4d8610979e77ac3718e8ee936fefa545c86b8
[profile/ivi/message-port.git] / daemon / dbus-manager.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * Copyright (C) 2013 Intel Corporation.
5  *
6  * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
7  *
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.
12  *
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.
17  *
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
21  * 02110-1301 USA
22  */
23
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"
31 #include "manager.h"
32 #include "utils.h"
33
34 #include <aul/aul.h>
35 #include <pkgmgr-info.h>
36
37 G_DEFINE_TYPE (MsgPortDbusManager, msgport_dbus_manager, G_TYPE_OBJECT)
38
39 #define MSGPORT_DBUS_MANAGER_GET_PRIV(obj) \
40     G_TYPE_INSTANCE_GET_PRIVATE ((obj), MSGPORT_TYPE_DBUS_MANAGER, MsgPortDbusManagerPrivate)
41
42 struct _MsgPortDbusManagerPrivate {
43     MsgPortDbusGlueManager *dbus_skeleton;
44     GDBusConnection        *connection;
45     MsgPortManager         *manager;
46     MsgPortDbusServer      *server;
47     gchar                  *app_id;
48     gboolean                is_null_cert;
49     GHashTable             *peer_certs;
50 };
51
52
53 static void
54 _dbus_manager_finalize (GObject *self)
55 {
56     //MsgPortDbusManager *manager = MSGPORT_DBUS_MANAGER (self);
57
58     G_OBJECT_CLASS (msgport_dbus_manager_parent_class)->finalize (self);
59 }
60
61 static void
62 _dbus_manager_dispose (GObject *self)
63 {
64     MsgPortDbusManager *dbus_mgr = MSGPORT_DBUS_MANAGER (self);
65
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);
71     }
72
73     g_clear_object (&dbus_mgr->priv->connection);
74
75     /* unregister all services owned by this connection */
76     msgport_manager_unregister_services (dbus_mgr->priv->manager, dbus_mgr, NULL);
77
78     g_clear_object (&dbus_mgr->priv->manager);
79
80     if (dbus_mgr->priv->peer_certs) {
81         g_hash_table_unref (dbus_mgr->priv->peer_certs);
82         dbus_mgr->priv->peer_certs = NULL;
83     }
84
85     G_OBJECT_CLASS (msgport_dbus_manager_parent_class)->dispose (self);
86 }
87
88
89 static gboolean
90 _dbus_manager_handle_register_service (
91     MsgPortDbusManager    *dbus_mgr,
92     GDBusMethodInvocation *invocation,
93     const gchar           *port_name,
94     gboolean               is_trusted,
95     gpointer               userdata)
96 {
97     GError *error = NULL;
98     MsgPortDbusService *dbus_service = NULL;
99     msgport_return_val_if_fail (dbus_mgr &&  MSGPORT_IS_DBUS_MANAGER (dbus_mgr), FALSE);
100
101     DBG ("register service request from %p('%s') for port '%s', is_trusted: %d",
102         dbus_mgr, dbus_mgr->priv->app_id, port_name, is_trusted);
103
104     dbus_service = msgport_manager_register_service (
105             dbus_mgr->priv->manager, dbus_mgr, 
106             port_name, is_trusted, &error);
107
108     if (dbus_service) {
109         msgport_dbus_glue_manager_complete_register_service (
110                 dbus_mgr->priv->dbus_skeleton, invocation, 
111                 msgport_dbus_service_get_object_path(dbus_service));
112         return TRUE;
113     }
114
115     if (!error) error = msgport_error_unknown_new ();
116     g_dbus_method_invocation_take_error (invocation, error);
117
118     return TRUE;
119 }
120
121 static gboolean
122 _dbus_manager_handle_check_for_remote_service (
123     MsgPortDbusManager    *dbus_mgr,
124     GDBusMethodInvocation *invocation,
125     const gchar    *remote_app_id,
126     const gchar    *remote_port_name,
127     gboolean        is_trusted,
128     gpointer        userdata)
129 {
130     GError *error = NULL;
131     MsgPortDbusService *dbus_service = NULL;
132     MsgPortDbusManager *remote_dbus_manager = NULL;
133
134     msgport_return_val_if_fail (dbus_mgr && MSGPORT_IS_DBUS_MANAGER (dbus_mgr), FALSE);
135
136     DBG ("check remote service request from %p for '%s' '%s', is_trusted: %d", 
137             dbus_mgr, remote_app_id, remote_port_name, is_trusted);
138
139     remote_dbus_manager = msgport_dbus_server_get_dbus_manager_by_app_id (
140                 dbus_mgr->priv->server, remote_app_id);
141
142     if (remote_dbus_manager) {
143         dbus_service = msgport_manager_get_service (dbus_mgr->priv->manager, 
144                             remote_dbus_manager, remote_port_name, is_trusted, &error);
145         if (dbus_service) {
146             DBG ("Found service id : %d", msgport_dbus_service_get_id (dbus_service));
147             msgport_dbus_glue_manager_complete_check_for_remote_service (
148                 dbus_mgr->priv->dbus_skeleton, invocation, 
149                 msgport_dbus_service_get_id (dbus_service));
150             return TRUE;
151         }
152     }
153
154     if (!error) error = msgport_error_port_not_found (remote_app_id, remote_port_name);
155     g_dbus_method_invocation_take_error (invocation, error);
156
157     return TRUE;
158 }
159
160 static gboolean
161 _dbus_manager_handle_send_message (
162     MsgPortDbusManager    *dbus_mgr,
163     GDBusMethodInvocation *invocation,
164     guint                  service_id,
165     GVariant              *data,
166     gpointer               userdata)
167 {
168     GError *error = NULL;
169     MsgPortDbusService *peer_dbus_service = 0;
170
171     msgport_return_val_if_fail (dbus_mgr && MSGPORT_IS_DBUS_MANAGER (dbus_mgr), FALSE);
172
173     DBG ("send_message from %p('%s') to service_id %d", 
174         dbus_mgr, dbus_mgr->priv->app_id, service_id);
175
176     peer_dbus_service = msgport_manager_get_service_by_id (
177             dbus_mgr->priv->manager, service_id, &error);
178
179     if (peer_dbus_service) {
180         if (msgport_dbus_service_send_message (peer_dbus_service, data, dbus_mgr->priv->app_id, "", FALSE, &error)) {
181             msgport_dbus_glue_manager_complete_send_message (
182                 dbus_mgr->priv->dbus_skeleton, invocation);
183             return TRUE;
184         }
185     }
186
187     if (!error) error = msgport_error_unknown_new ();
188     g_dbus_method_invocation_take_error (invocation, error);
189
190     return TRUE;
191 }
192
193 static void
194 msgport_dbus_manager_class_init (MsgPortDbusManagerClass *klass)
195 {
196     GObjectClass *gklass = G_OBJECT_CLASS(klass);
197
198     g_type_class_add_private (klass, sizeof(MsgPortDbusManagerPrivate));
199
200     gklass->finalize = _dbus_manager_finalize;
201     gklass->dispose = _dbus_manager_dispose;
202 }
203
204 static void
205 msgport_dbus_manager_init (MsgPortDbusManager *self)
206 {
207     MsgPortDbusManagerPrivate *priv = MSGPORT_DBUS_MANAGER_GET_PRIV (self);
208
209     priv->dbus_skeleton = msgport_dbus_glue_manager_skeleton_new ();
210     priv->manager = msgport_manager_new ();
211     priv->is_null_cert = FALSE;
212     priv->peer_certs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
213
214     g_signal_connect_swapped (priv->dbus_skeleton, "handle-register-service",
215                 G_CALLBACK (_dbus_manager_handle_register_service), (gpointer)self);
216     g_signal_connect_swapped (priv->dbus_skeleton, "handle-check-for-remote-service",
217                 G_CALLBACK (_dbus_manager_handle_check_for_remote_service), (gpointer)self);
218     g_signal_connect_swapped (priv->dbus_skeleton, "handle-send-message",
219                 G_CALLBACK (_dbus_manager_handle_send_message), (gpointer)self);
220
221     self->priv = priv;
222 }
223 static gchar *
224 _get_app_id_from_connection (GDBusConnection *connection)
225 {
226     pid_t peer_pid;
227     GError *error = NULL;
228     GCredentials *cred = g_dbus_connection_get_peer_credentials (connection);
229
230     msgport_return_val_if_fail (cred != NULL, NULL);
231
232 #ifdef ENABLE_DEBUG
233     gchar *str_cred = g_credentials_to_string (cred);
234     DBG ("Client Credentials : %s", str_cred);
235     g_free (str_cred);
236 #endif
237
238     peer_pid = g_credentials_get_unix_pid (cred, &error);
239     if (error) {
240         WARN ("Faild to get peer pid on conneciton %p : %s", connection, error->message);
241         g_error_free (error);
242         return NULL;
243     }
244
245 #if 1
246     char app_id[255];
247     aul_return_val res;
248     if ((res = aul_app_get_appid_bypid (peer_pid, app_id, sizeof(app_id))) != AUL_R_OK) {
249         WARN ("Fail to get appid of peer pid '%d', error : %d", peer_pid, res);
250         return NULL;
251     }
252     else g_strdup (app_id);
253 #else
254     return g_strdup_printf ("%d", peer_pid);
255 #endif
256 }
257
258 MsgPortDbusManager *
259 msgport_dbus_manager_new (
260     GDBusConnection *connection,
261     MsgPortDbusServer *server,
262     GError **error)
263 {
264     MsgPortDbusManager *dbus_mgr = NULL;
265
266     dbus_mgr = MSGPORT_DBUS_MANAGER (g_object_new (MSGPORT_TYPE_DBUS_MANAGER, NULL));
267     if (!dbus_mgr) {
268         if (error) *error = msgport_error_new (MSGPORT_ERROR_OUT_OF_MEMORY, "Out of memory");
269         return NULL;
270     }
271
272     if (!g_dbus_interface_skeleton_export (
273             G_DBUS_INTERFACE_SKELETON (dbus_mgr->priv->dbus_skeleton),
274             connection,
275             "/",
276             error)) {
277         WARN ("Failed to export dbus object on connection %p : %s",
278                     connection, error ? (*error)->message : "");
279         g_object_unref (dbus_mgr);
280         return NULL;
281     }
282     dbus_mgr->priv->connection = g_object_ref (connection);
283     dbus_mgr->priv->server = server;
284     dbus_mgr->priv->app_id =  _get_app_id_from_connection (connection);
285
286     return dbus_mgr;
287 }
288
289 MsgPortManager *
290 msgport_dbus_manager_get_manager (MsgPortDbusManager *dbus_manager)
291 {
292     msgport_return_val_if_fail (dbus_manager && MSGPORT_IS_DBUS_MANAGER (dbus_manager), NULL);
293
294     return dbus_manager->priv->manager;
295 }
296
297 GDBusConnection *
298 msgport_dbus_manager_get_connection (MsgPortDbusManager *dbus_manager)
299 {
300     msgport_return_val_if_fail (dbus_manager && MSGPORT_IS_DBUS_MANAGER (dbus_manager), NULL);
301
302     return dbus_manager->priv->connection;
303 }
304
305 const gchar *
306 msgport_dbus_manager_get_app_id (MsgPortDbusManager *dbus_manager)
307 {
308     msgport_return_val_if_fail (dbus_manager && MSGPORT_IS_DBUS_MANAGER (dbus_manager), NULL);
309
310     return (const gchar *)dbus_manager->priv->app_id;
311 }
312
313 gboolean
314 msgport_dbus_manager_validate_peer_certificate (MsgPortDbusManager *dbus_manager, const gchar *peer_app_id)
315 {
316     int res ;
317     pkgmgrinfo_cert_compare_result_type_e compare_result;
318     gboolean is_valid_cert = FALSE;
319
320     /* check if the source application has no certificate info */
321     if (dbus_manager->priv->is_null_cert)
322         return TRUE; /* allow all peers to connect */
323
324     /* check if we have cached status */
325     if (g_hash_table_contains (dbus_manager->priv->peer_certs, peer_app_id))
326         return ((gboolean)(glong)g_hash_table_lookup (dbus_manager->priv->peer_certs, peer_app_id));
327
328     if ((res = pkgmgrinfo_pkginfo_compare_app_cert_info (dbus_manager->priv->app_id,
329                     peer_app_id, &compare_result)) != PMINFO_R_OK) {
330         WARN ("Fail to compare certificates of applications('%s', '%s') : error %d", 
331                 dbus_manager->priv->app_id, peer_app_id, res);
332         return FALSE;
333     }
334
335     if (compare_result == PMINFO_CERT_COMPARE_LHS_NO_CERT) {
336         dbus_manager->priv->is_null_cert = TRUE;
337         return TRUE;
338     }
339
340     is_valid_cert = (compare_result == PMINFO_CERT_COMPARE_MATCH) ;
341     g_hash_table_insert (dbus_manager->priv->peer_certs, g_strdup (peer_app_id), (gpointer)is_valid_cert);
342
343     return is_valid_cert;
344 }
345