daemon: aul & pkgmgr-info dependent code enabled
[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 for port %s", dbus_mgr, port_name);
102
103     dbus_service = msgport_manager_register_service (
104             dbus_mgr->priv->manager, dbus_mgr, 
105             port_name, is_trusted, &error);
106
107     if (dbus_service) {
108         msgport_dbus_glue_manager_complete_register_service (
109                 dbus_mgr->priv->dbus_skeleton, invocation, 
110                 msgport_dbus_service_get_object_path(dbus_service));
111         return TRUE;
112     }
113
114     if (!error) error = msgport_error_unknown_new ();
115     g_dbus_method_invocation_take_error (invocation, error);
116
117     return TRUE;
118 }
119
120 static gboolean
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,
126     gboolean        is_trusted,
127     gpointer        userdata)
128 {
129     GError *error = NULL;
130     MsgPortDbusService *dbus_service = NULL;
131     MsgPortDbusManager *remote_dbus_manager = NULL;
132
133     msgport_return_val_if_fail (dbus_mgr && MSGPORT_IS_DBUS_MANAGER (dbus_mgr), FALSE);
134
135     DBG ("check remote service request from %p for %s %s %d", 
136             dbus_mgr, remote_app_id, remote_port_name, is_trusted);
137
138     remote_dbus_manager = msgport_dbus_server_get_dbus_manager_by_app_id (
139                 dbus_mgr->priv->server, remote_app_id);
140
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);
144         if (dbus_service) {
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));
149             return TRUE;
150         }
151     }
152
153     if (!error) error = msgport_error_port_not_found (remote_app_id, remote_port_name);
154     g_dbus_method_invocation_take_error (invocation, error);
155
156     return TRUE;
157 }
158
159 static gboolean
160 _dbus_manager_handle_send_message (
161     MsgPortDbusManager    *dbus_mgr,
162     GDBusMethodInvocation *invocation,
163     guint                  service_id,
164     GVariant              *data,
165     gpointer               userdata)
166 {
167     GError *error = NULL;
168     MsgPortDbusService *peer_dbus_service = 0;
169
170     msgport_return_val_if_fail (dbus_mgr && MSGPORT_IS_DBUS_MANAGER (dbus_mgr), FALSE);
171
172     DBG ("send_message from %p : %d ", dbus_mgr, service_id);
173
174     peer_dbus_service = msgport_manager_get_service_by_id (
175             dbus_mgr->priv->manager, service_id, &error);
176
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);
181             return TRUE;
182         }
183     }
184
185     if (!error) error = msgport_error_unknown_new ();
186     g_dbus_method_invocation_take_error (invocation, error);
187
188     return TRUE;
189 }
190
191 static void
192 msgport_dbus_manager_class_init (MsgPortDbusManagerClass *klass)
193 {
194     GObjectClass *gklass = G_OBJECT_CLASS(klass);
195
196     g_type_class_add_private (klass, sizeof(MsgPortDbusManagerPrivate));
197
198     gklass->finalize = _dbus_manager_finalize;
199     gklass->dispose = _dbus_manager_dispose;
200 }
201
202 static void
203 msgport_dbus_manager_init (MsgPortDbusManager *self)
204 {
205     MsgPortDbusManagerPrivate *priv = MSGPORT_DBUS_MANAGER_GET_PRIV (self);
206
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);
211
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);
218
219     self->priv = priv;
220 }
221 static gchar *
222 _get_app_id_from_connection (GDBusConnection *connection)
223 {
224     pid_t peer_pid;
225     GError *error = NULL;
226     GCredentials *cred = g_dbus_connection_get_peer_credentials (connection);
227
228     msgport_return_val_if_fail (cred != NULL, NULL);
229
230 #ifdef ENABLE_DEBUG
231     gchar *str_cred = g_credentials_to_string (cred);
232     DBG ("Client Credentials : %s", str_cred);
233     g_free (str_cred);
234 #endif
235
236     peer_pid = g_credentials_get_unix_pid (cred, &error);
237     if (error) {
238         WARN ("Faild to get peer pid on conneciton %p : %s", connection, error->message);
239         g_error_free (error);
240         return NULL;
241     }
242
243 #if 1
244     char app_id[255];
245     aul_return_val res;
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);
248         return NULL;
249     }
250     else g_strdup (app_id);
251 #else
252     return g_strdup_printf ("%d", peer_pid);
253 #endif
254 }
255
256 MsgPortDbusManager *
257 msgport_dbus_manager_new (
258     GDBusConnection *connection,
259     MsgPortDbusServer *server,
260     GError **error)
261 {
262     MsgPortDbusManager *dbus_mgr = NULL;
263
264     dbus_mgr = MSGPORT_DBUS_MANAGER (g_object_new (MSGPORT_TYPE_DBUS_MANAGER, NULL));
265     if (!dbus_mgr) {
266         if (error) *error = msgport_error_new (MSGPORT_ERROR_OUT_OF_MEMORY, "Out of memory");
267         return NULL;
268     }
269
270     if (!g_dbus_interface_skeleton_export (
271             G_DBUS_INTERFACE_SKELETON (dbus_mgr->priv->dbus_skeleton),
272             connection,
273             "/",
274             error)) {
275         WARN ("Failed to export dbus object on connection %p : %s",
276                     connection, error ? (*error)->message : "");
277         g_object_unref (dbus_mgr);
278         return NULL;
279     }
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);
283
284     return dbus_mgr;
285 }
286
287 MsgPortManager *
288 msgport_dbus_manager_get_manager (MsgPortDbusManager *dbus_manager)
289 {
290     msgport_return_val_if_fail (dbus_manager && MSGPORT_IS_DBUS_MANAGER (dbus_manager), NULL);
291
292     return dbus_manager->priv->manager;
293 }
294
295 GDBusConnection *
296 msgport_dbus_manager_get_connection (MsgPortDbusManager *dbus_manager)
297 {
298     msgport_return_val_if_fail (dbus_manager && MSGPORT_IS_DBUS_MANAGER (dbus_manager), NULL);
299
300     return dbus_manager->priv->connection;
301 }
302
303 const gchar *
304 msgport_dbus_manager_get_app_id (MsgPortDbusManager *dbus_manager)
305 {
306     msgport_return_val_if_fail (dbus_manager && MSGPORT_IS_DBUS_MANAGER (dbus_manager), NULL);
307
308     return (const gchar *)dbus_manager->priv->app_id;
309 }
310
311 gboolean
312 msgport_dbus_manager_validate_peer_certificate (MsgPortDbusManager *dbus_manager, const gchar *peer_app_id)
313 {
314     int res ;
315     pkgmgrinfo_cert_compare_result_type_e compare_result;
316     gboolean is_valid_cert = FALSE;
317
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 */
321
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));
325
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);
330         return FALSE;
331     }
332
333     if (compare_result == PMINFO_CERT_COMPARE_LHS_NO_CERT) {
334         dbus_manager->priv->is_null_cert = TRUE;
335         return TRUE;
336     }
337
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);
340
341     return is_valid_cert;
342 }
343