c18dc48e40e37b7823506451196a17ac33941bf9
[platform/core/appfw/message-port.git] / src / message_port_remote.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #define _GNU_SOURCE
18
19 #include <bundle.h>
20 #include <bundle_internal.h>
21 #include <aul.h>
22
23 #include <sys/socket.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26
27 #include <glib.h>
28 #include <gio/gio.h>
29 #include <gio/gunixfdlist.h>
30 #include <glib-unix.h>
31
32 #include "message_port_log.h"
33 #include "message_port_common.h"
34 #include "message_port_remote.h"
35
36
37 #define MAX_PACKAGE_STR_SIZE 512
38
39 #define DBUS_RELEASE_NAME_REPLY_RELEASED        1 /* *< Service was released from the given name */
40 #define DBUS_RELEASE_NAME_REPLY_NON_EXISTENT    2 /* *< The given name does not exist on the bus */
41 #define DBUS_RELEASE_NAME_REPLY_NOT_OWNER       3 /* *< Service is not an owner of the given name */
42
43 #define MAX_RETRY_CNT 10
44 #define SOCK_PAIR_SENDER 0
45 #define SOCK_PAIR_RECEIVER 1
46
47 static bool _initialized = false;
48 static GHashTable *__local_port_info;
49 static GHashTable *__trusted_app_list_hash;
50 static GHashTable *__callback_info_hash;
51 static GHashTable *__sender_appid_hash;
52
53 typedef struct message_port_pkt {
54         int remote_port_name_len;
55         char *remote_port_name;
56         bool is_bidirection;
57         bool is_trusted;
58         int data_len;
59         unsigned char *data;
60 } message_port_pkt_s;
61
62 typedef struct message_port_callback_info {
63         message_port_local_port_info_s *local_info;
64         int local_id;
65         char *remote_app_id;
66         GIOChannel *gio_read;
67         int g_src_id;
68 } message_port_callback_info_s;
69
70 typedef struct callback_key_info {
71         int local_id;
72         message_port_callback_info_s *callback_info;
73 } callback_key_info_s;
74
75 static void __callback_info_free(gpointer data)
76 {
77         message_port_callback_info_s *callback_info = (message_port_callback_info_s *)data;
78         GError *error = NULL;
79         if (callback_info == NULL)
80                 return;
81
82         if (callback_info->remote_app_id)
83                 FREE_AND_NULL(callback_info->remote_app_id);
84
85         if (callback_info->local_info) {
86                 if (callback_info->local_info->port_name)
87                                 FREE_AND_NULL(callback_info->local_info->port_name);
88
89                 FREE_AND_NULL(callback_info->local_info);
90         }
91
92         if (callback_info->gio_read != NULL) {
93                 g_io_channel_shutdown(callback_info->gio_read, TRUE, &error);
94                 if (error) {
95                         _LOGE("g_io_channel_shutdown error : %s", error->message);
96                         g_error_free(error);
97                 }
98                 g_io_channel_unref(callback_info->gio_read);
99                 callback_info->gio_read = NULL;
100         }
101
102         if (callback_info->g_src_id != 0) {
103                 g_source_remove(callback_info->g_src_id);
104                 callback_info->g_src_id = 0;
105         }
106
107         FREE_AND_NULL(callback_info);
108 }
109
110 /* LCOV_EXCL_START */
111 static void __callback_info_free_by_info(message_port_callback_info_s *callback_info)
112 {
113         GList *callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
114         GList *find_list;
115
116         if (callback_info_list == NULL)
117                 return;
118
119         find_list = g_list_find(callback_info_list, callback_info);
120         if (find_list == NULL)
121                 return;
122
123         callback_info_list = g_list_remove_link(callback_info_list, find_list);
124         __callback_info_free(callback_info);
125         g_list_free(find_list);
126 }
127 /* LCOV_EXCL_STOP */
128
129 static void __hash_destroy_callback_info(gpointer data)
130 {
131         GList *callback_list = (GList *)data;
132
133         if (callback_list != NULL)
134                 g_list_free_full(callback_list, __callback_info_free);
135 }
136
137 /* LCOV_EXCL_START */
138 static void __hash_destory_local_value(gpointer data)
139 {
140         message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
141         if (mli) {
142                 if (mli->port_name)
143                         free(mli->port_name);
144                 free(mli);
145         }
146 }
147 /* LCOV_EXCL_STOP */
148
149 static bool __initialize(void)
150 {
151         if (!initialized_common) {
152                 if (!initialize_common())
153                         return false;
154         }
155
156         if (__local_port_info == NULL) {
157                 __local_port_info = g_hash_table_new_full(g_direct_hash,  g_direct_equal, NULL, __hash_destory_local_value);
158                 retvm_if(!__local_port_info, false, "fail to create __local_port_info");
159         }
160
161         if (__sender_appid_hash == NULL) {
162                 __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
163                 retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash");
164         }
165
166         if (__trusted_app_list_hash == NULL) {
167                 __trusted_app_list_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
168                 retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
169         }
170
171         if (__callback_info_hash == NULL) {
172                 __callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destroy_callback_info);
173                 retvm_if(!__callback_info_hash, false, "fail to create __callback_info_hash");
174         }
175
176         _initialized = true;
177
178         return true;
179 }
180
181 bool is_local_port_registed(const char *local_port, bool trusted, int *local_id, message_port_local_port_info_s **lpi)
182 {
183         GHashTableIter iter;
184         gpointer key, value;
185
186         if (__local_port_info == NULL) {
187                 _LOGI("There is no registed local port");
188                 return false;
189         }
190
191         g_hash_table_iter_init(&iter, __local_port_info);
192         while (g_hash_table_iter_next(&iter, &key, &value)) {
193                 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)value;
194
195                 if ((mi->is_trusted == trusted) && strcmp(mi->port_name, local_port) == 0) {
196                         *local_id = mi->local_id;
197                         if (lpi != NULL)
198                                 *lpi = mi;
199                         return true;
200                 }
201         }
202         return false;
203 }
204
205 static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
206 {
207         GDBusMessage *msg = NULL;
208         GDBusMessage *reply = NULL;
209         GError *err = NULL;
210         GVariant *body;
211         int pid = 0;
212
213         msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
214                         "org.freedesktop.DBus", "GetConnectionUnixProcessID");
215         if (!msg) {
216                 _LOGE("Can't allocate new method call");
217                 goto out;
218         }
219
220         g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
221         reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
222                                                         G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
223
224         if (!reply) {
225                 if (err != NULL) {
226                         _LOGE("Failed to get pid [%s]", err->message);
227                         g_error_free(err);
228                 } else {
229                         _LOGE("Failed to get pid");
230                 }
231                 goto out;
232         }
233
234         body = g_dbus_message_get_body(reply);
235         g_variant_get(body, "(u)", &pid);
236
237 out:
238         if (msg)
239                 g_object_unref(msg);
240         if (reply)
241                 g_object_unref(reply);
242
243         return pid;
244 }
245
246 static message_port_pkt_s *__message_port_recv_raw(int fd)
247 {
248         message_port_pkt_s *pkt = NULL;
249         unsigned int nb;
250
251         pkt = (message_port_pkt_s *)calloc(sizeof(message_port_pkt_s), 1);
252         if (pkt == NULL) {
253 /* LCOV_EXCL_START */
254                 close(fd);
255                 return NULL;
256 /* LCOV_EXCL_STOP */
257         }
258
259         if (read_string_from_socket(fd, (char **)&pkt->remote_port_name, &pkt->remote_port_name_len) != MESSAGE_PORT_ERROR_NONE) {
260 /* LCOV_EXCL_START */
261                 LOGE("read socket fail: port_name");
262                 free(pkt->remote_port_name);
263                 free(pkt);
264                 return NULL;
265 /* LCOV_EXCL_STOP */
266         }
267
268         if (read_socket(fd, (char *)&pkt->is_bidirection, sizeof(pkt->is_bidirection), &nb) != MESSAGE_PORT_ERROR_NONE) {
269 /* LCOV_EXCL_START */
270                 LOGE("read socket fail: is_bidirection");
271                 free(pkt->remote_port_name);
272                 free(pkt);
273                 return NULL;
274 /* LCOV_EXCL_STOP */
275         }
276
277         if (read_socket(fd, (char *)&pkt->is_trusted, sizeof(pkt->is_trusted), &nb) != MESSAGE_PORT_ERROR_NONE) {
278 /* LCOV_EXCL_START */
279                 LOGE("read socket fail: is_trusted");
280                 free(pkt->remote_port_name);
281                 free(pkt);
282                 return NULL;
283 /* LCOV_EXCL_STOP */
284         }
285
286         if (read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGE_PORT_ERROR_NONE) {
287 /* LCOV_EXCL_START */
288                 LOGE("read socket fail: data");
289                 if (pkt->data)
290                         free(pkt->data);
291                 free(pkt->remote_port_name);
292                 free(pkt);
293                 return NULL;
294 /* LCOV_EXCL_STOP */
295         }
296
297         return pkt;
298 }
299
300 static bool __validate_callback_info(callback_key_info_s *key_info)
301 {
302         GList *cb_list;
303
304         cb_list = g_hash_table_lookup(__callback_info_hash,
305                         GUINT_TO_POINTER(key_info->local_id));
306         if (cb_list == NULL) {
307                 _LOGI("local_info : %d is already released", key_info->local_id);
308                 return false;
309         }
310
311         cb_list = g_list_find(cb_list, key_info->callback_info);
312         if (cb_list == NULL) {
313                 _LOGI("local_info : %d is already released from list",
314                                 key_info->local_id);
315                 return false;
316         }
317
318         return true;
319 }
320
321 static bool __validate_local_info(callback_key_info_s *key_info)
322 {
323         GList *cb_list;
324
325         cb_list = g_hash_table_lookup(__local_port_info,
326                         GUINT_TO_POINTER(key_info->local_id));
327         if (cb_list == NULL) {
328                 _LOGI("local_info : %d is already released", key_info->local_id);
329                 return false;
330         }
331
332         return true;
333 }
334
335 static gboolean __socket_request_handler(GIOChannel *gio,
336                 GIOCondition cond,
337                 gpointer data)
338 {
339         int fd = 0;
340         message_port_callback_info_s *mi = NULL;
341         callback_key_info_s *key_info;
342         message_port_pkt_s *pkt = NULL;
343         message_port_local_port_info_s *local_port_info;
344         bundle *kb = NULL;
345         bool ret = true;
346         bool existed = true;
347
348         key_info = (callback_key_info_s *)data;
349         if (key_info == NULL)
350                 return FALSE;
351
352         message_port_lock_mutex();
353         if (__validate_callback_info(key_info) == false) {
354                 ret = FALSE;
355                 existed = FALSE;
356                 message_port_unlock_mutex();
357                 goto out;
358         }
359
360         if (__validate_local_info(key_info) == false) {
361                 ret = FALSE;
362                 message_port_unlock_mutex();
363                 goto out;
364         }
365         message_port_unlock_mutex();
366
367         mi = key_info->callback_info;
368
369         local_port_info = mi->local_info;
370         if (local_port_info == NULL || local_port_info->callback == NULL) {
371                 _LOGE("Failed to get callback info");
372                 ret = FALSE;
373                 goto out;
374         }
375
376         if (cond == G_IO_HUP) {
377                 _LOGI("socket G_IO_HUP");
378                 ret = FALSE;
379                 goto out;
380         }
381
382         fd = g_io_channel_unix_get_fd(gio);
383         if (fd < 0) {
384                 _LOGE("fail to get fd from io channel");
385                 ret = FALSE;
386                 goto out;
387         }
388
389         pkt = __message_port_recv_raw(fd);
390         if (pkt == NULL) {
391                 _LOGE("recv error on SOCKET");
392                 ret = FALSE;
393                 goto out;
394         }
395
396         kb = bundle_decode(pkt->data, pkt->data_len);
397         if (!kb) {
398                 _LOGE("Invalid argument : message");
399                 ret = FALSE;
400                 goto out;
401         }
402
403         if (pkt->is_bidirection)
404                 local_port_info->callback(mi->local_id, mi->remote_app_id,
405                         pkt->remote_port_name, pkt->is_trusted, kb, local_port_info->user_data);
406         else
407                 local_port_info->callback(mi->local_id, mi->remote_app_id,
408                         NULL, pkt->is_trusted, kb, local_port_info->user_data);
409
410         bundle_free(kb);
411
412 out:
413         if (pkt) {
414                 if (pkt->remote_port_name)
415                         free(pkt->remote_port_name);
416                 if (pkt->data)
417                         free(pkt->data);
418                 free(pkt);
419         }
420
421         if (mi && ret == FALSE && existed == TRUE) {
422                 message_port_lock_mutex();
423                 __callback_info_free_by_info(mi);
424                 message_port_unlock_mutex();
425         }
426
427         return ret;
428 }
429
430
431 static void __socket_destroy_handler(gpointer data)
432 {
433         _LOGI("__socket_destroy_handler");
434         callback_key_info_s *key_info = (callback_key_info_s *)data;
435         free(key_info);
436 }
437
438 static callback_key_info_s *__create_callback_key_info(message_port_callback_info_s *callback_info)
439 {
440         callback_key_info_s *_key_info = (callback_key_info_s *)
441                 calloc(1, sizeof(callback_key_info_s));
442
443         if (_key_info == NULL) {
444 /* LCOV_EXCL_START */
445                 _LOGE("out of memory");
446                 return NULL;
447 /* LCOV_EXCL_STOP */
448         }
449
450         _key_info->local_id = callback_info->local_id;
451         _key_info->callback_info = callback_info;
452
453         return _key_info;
454 }
455
456 static message_port_callback_info_s *__create_callback_info(message_port_local_port_info_s *mi, char *local_appid)
457 {
458         message_port_local_port_info_s *local_info = NULL;
459         message_port_callback_info_s *callback_info = NULL;
460         bool ret = true;
461
462         callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
463         if (callback_info == NULL) {
464 /* LCOV_EXCL_START */
465                 _LOGE("out of memory");
466                 return NULL;
467 /* LCOV_EXCL_STOP */
468         }
469
470         local_info = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
471         if (local_info == NULL) {
472 /* LCOV_EXCL_START */
473                 ret = false;
474                 _LOGE("out of memory");
475                 goto out;
476 /* LCOV_EXCL_STOP */
477         }
478
479         callback_info->local_id = mi->local_id;
480         callback_info->local_info = local_info;
481         callback_info->remote_app_id = strdup(local_appid);
482         if (callback_info->remote_app_id == NULL) {
483 /* LCOV_EXCL_START */
484                 ret = false;
485                 _LOGE("out of memory");
486                 goto out;
487 /* LCOV_EXCL_STOP */
488         }
489
490         local_info->port_name = strdup(mi->port_name);
491         if (local_info->port_name == NULL) {
492 /* LCOV_EXCL_START */
493                 ret = false;
494                 _LOGE("out of memory");
495                 goto out;
496 /* LCOV_EXCL_STOP */
497         }
498
499         local_info->callback = mi->callback;
500         local_info->is_trusted = mi->is_trusted;
501         local_info->local_id = mi->local_id;
502         local_info->user_data = mi->user_data;
503
504 out:
505         if (ret == false) {
506                 __callback_info_free(callback_info);
507                 return NULL;
508         }
509
510         return callback_info;
511 }
512
513 static bool __callback_info_append(message_port_callback_info_s *callback_info)
514 {
515         GList *callback_info_list = NULL;
516         message_port_callback_info_s *head_callback_info;
517
518         callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
519         if (callback_info_list == NULL) {
520                 head_callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
521                 if (head_callback_info == NULL) {
522 /* LCOV_EXCL_START */
523                         _LOGE("fail to alloc head_callback_info");
524                         return false;
525 /* LCOV_EXCL_STOP */
526                 }
527                 head_callback_info->local_id = 0;
528                 head_callback_info->remote_app_id = NULL;
529                 head_callback_info->local_info = NULL;
530                 head_callback_info->gio_read = NULL;
531                 head_callback_info->g_src_id = 0;
532                 callback_info_list = g_list_append(callback_info_list, head_callback_info);
533                 callback_info_list = g_list_append(callback_info_list, callback_info);
534                 g_hash_table_insert(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id), callback_info_list);
535         } else {
536                 callback_info_list = g_list_append(callback_info_list, callback_info);
537         }
538
539         return true;
540 }
541
542 static void __callback_info_update_user_data(int local_id, message_port_message_cb callback, void *user_data)
543 {
544         GList *callback_info_list;
545         GList *iter;
546         message_port_callback_info_s *callback_info;
547
548         callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(local_id));
549         if (callback_info_list != NULL) {
550 /* LCOV_EXCL_START */
551                 for (iter = callback_info_list; iter != NULL; iter = iter->next) {
552                         callback_info = (message_port_callback_info_s *)iter->data;
553                         if (callback_info->local_info != NULL) {
554                                 callback_info->local_info->callback = callback;
555                                 callback_info->local_info->user_data = user_data;
556                         }
557                 }
558 /* LCOV_EXCL_STOP */
559         } else {
560                 _LOGE("fail to find local_id %d ", local_id);
561         }
562 }
563
564 static bool __receive_message(GVariant *parameters, GDBusMethodInvocation *invocation)
565 {
566         char *local_port = NULL;
567         char *local_appid = NULL;
568         char *remote_appid = NULL;
569         char *remote_port = NULL;
570         gboolean local_trusted = false;
571         gboolean remote_trusted = false;
572         gboolean bi_dir = false;
573         int len = 0;
574
575         bundle *data = NULL;
576         bundle_raw *raw = NULL;
577         message_port_local_port_info_s *mi;
578         int local_reg_id = 0;
579         message_port_callback_info_s *callback_info = NULL;
580         callback_key_info_s *key_info;
581
582         char buf[1024];
583         GDBusMessage *msg;
584         GUnixFDList *fd_list;
585         int fd_len;
586         int *returned_fds = NULL;
587         int fd;
588         bool ret = false;
589         char *key_appid;
590
591         g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
592                         &remote_appid, &remote_port, &remote_trusted, &len, &raw);
593
594         if (!remote_port) {
595                 _LOGE("Invalid argument : remote_port is NULL");
596                 goto out;
597         }
598         if (!remote_appid) {
599                 _LOGE("Invalid argument : remote_appid is NULL");
600                 goto out;
601         }
602
603         if (!local_appid) {
604                 _LOGE("Invalid argument : local_appid");
605                 goto out;
606         }
607
608         message_port_lock_mutex();
609         if (!is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
610                 _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
611                 message_port_unlock_mutex();
612                 goto out;
613         }
614
615         callback_info = __create_callback_info(mi, local_appid);
616         if (callback_info == NULL) {
617                 message_port_unlock_mutex();
618                 goto out;
619         }
620         message_port_unlock_mutex();
621
622         if (!local_port) {
623                 _LOGE("Invalid argument : local_port");
624                 goto out;
625         }
626         if (strcmp(remote_appid, app_id) != 0) {
627                 _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
628                 goto out;
629         }
630         if (strcmp(remote_port, callback_info->local_info->port_name) != 0) {
631                 _LOGE("Invalid argument : remote_port (%s)", remote_port);
632                 goto out;
633         }
634         if (!len) {
635                 _LOGE("Invalid argument : data_len");
636                 goto out;
637         }
638         if (remote_trusted) {
639 /* LCOV_EXCL_START */
640                 if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
641                         if (!is_preloaded(local_appid, remote_appid)) {
642                                 int ret = check_certificate(local_appid, remote_appid);
643                                 if (ret == MESSAGE_PORT_ERROR_NONE) {
644                                         key_appid = strdup(local_appid);
645                                         if (key_appid)
646                                                 g_hash_table_insert(__trusted_app_list_hash, key_appid, "TRUE");
647                                 } else {
648                                         _LOGE("The application (%s) is not signed with the same certificate",
649                                                         local_appid);
650                                         goto out;
651                                 }
652                         }
653                 }
654 /* LCOV_EXCL_STOP */
655         }
656
657         data = bundle_decode(raw, len);
658         if (!data) {
659                 _LOGE("Invalid argument : message");
660                 goto out;
661         }
662
663         LOGD("call calback %s", local_appid);
664         if (bi_dir)
665                 callback_info->local_info->callback(callback_info->local_info->local_id,
666                         local_appid, local_port, local_trusted, data, callback_info->local_info->user_data);
667         else
668                 callback_info->local_info->callback(callback_info->local_info->local_id,
669                         local_appid, NULL, false, data, callback_info->local_info->user_data);
670         bundle_free(data);
671
672         ret = true;
673
674         msg = g_dbus_method_invocation_get_message(invocation);
675         fd_list = g_dbus_message_get_unix_fd_list(msg);
676
677         /* When application send message to self fd_list is NULL */
678         if (fd_list != NULL) {
679                 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
680                 if (returned_fds == NULL) {
681                         _LOGE("fail to get fds");
682                         ret = false;
683                         goto out;
684                 }
685                 fd = returned_fds[0];
686
687                 LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
688                 if (fd > 0) {
689                         callback_info->gio_read = g_io_channel_unix_new(fd);
690                         if (!callback_info->gio_read) {
691                                 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
692                                 ret = false;
693                                 goto out;
694                         }
695
696                         key_info = __create_callback_key_info(callback_info);
697                         if (key_info == NULL) {
698                                 _LOGE("out of memory");
699                                 ret = false;
700                                 goto out;
701                         }
702
703                         callback_info->g_src_id = g_io_add_watch_full(
704                                         callback_info->gio_read,
705                                         G_PRIORITY_DEFAULT,
706                                         G_IO_IN | G_IO_HUP,
707                                         __socket_request_handler,
708                                         (gpointer)key_info,
709                                         __socket_destroy_handler);
710                         if (callback_info->g_src_id == 0) {
711                                 _LOGE("fail to add watch on socket");
712                                 free(key_info);
713                                 ret = false;
714                                 goto out;
715                         }
716
717                         message_port_lock_mutex();
718                         if (__callback_info_append(callback_info) == false) {
719                                 _LOGE("fail to append callback_info");
720                                 ret = false;
721                         }
722                         message_port_unlock_mutex();
723                 }
724         }
725
726 out:
727         if (ret == false)
728                 __callback_info_free(callback_info);
729
730         if (returned_fds)
731                 g_free(returned_fds);
732
733         return ret;
734 }
735
736 static void __on_sender_name_appeared(GDBusConnection *connection,
737                 const gchar     *name,
738                 const gchar     *name_owner,
739                 gpointer         user_data)
740 {
741         _LOGI("sender name appeared : %s", name);
742 }
743
744 static void __on_sender_name_vanished(GDBusConnection *connection,
745                 const gchar     *name,
746                 gpointer         user_data)
747 {
748         int *watcher_id = (int *)user_data;
749         char *local_appid;
750
751         local_appid = (char *)g_hash_table_lookup(__sender_appid_hash, name);
752         if (local_appid) {
753                 g_hash_table_remove(__trusted_app_list_hash, (gpointer)local_appid);
754                 g_hash_table_remove(__sender_appid_hash, (gpointer)name);
755         }
756
757         if (watcher_id) {
758                 if (*watcher_id > 0)
759                         g_bus_unwatch_name(*watcher_id);
760                 else
761                         LOGE("Invalid watcher_id %d", *watcher_id);
762                 free(watcher_id);
763         } else {
764                 LOGE("watcher_id is NULL");
765         }
766 }
767
768 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
769 {
770         int ret = 0;
771         char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
772         char *local_appid = NULL;
773         int pid = __get_sender_pid(conn, sender);
774         int *watcher_id = (int *)calloc(1, sizeof(int));
775         char *_sender;
776         char *_appid;
777         retvm_if(!watcher_id, false, "Malloc failed");
778
779         ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
780         if (ret != AUL_R_OK) {
781                 _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
782                 free(watcher_id);
783                 return false;
784         }
785
786         g_variant_get_child(parameters, 0, "&s", &local_appid);
787         if (local_appid == NULL) {
788                 _LOGE("appid is NULL : (%s) (%d)", sender, pid);
789                 free(watcher_id);
790                 return false;
791         }
792
793         if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
794                 _LOGD("insert sender !!!!! %s", sender);
795                 _sender = strdup(sender);
796                 if (_sender == NULL) {
797 /* LCOV_EXCL_START */
798                         _LOGE("out of memory");
799                         free(watcher_id);
800                         return false;
801 /* LCOV_EXCL_STOP */
802                 }
803
804                 _appid = strdup(local_appid);
805                 if (_appid == NULL) {
806 /* LCOV_EXCL_START */
807                         _LOGE("out of memory");
808                         free(watcher_id);
809                         free(_sender);
810                         return false;
811 /* LCOV_EXCL_STOP */
812                 }
813
814                 g_hash_table_insert(__sender_appid_hash, (gpointer)_sender, (gpointer)_appid);
815                 *watcher_id = g_bus_watch_name_on_connection(
816                                         gdbus_conn,
817                                         sender,
818                                         G_BUS_NAME_WATCHER_FLAGS_NONE,
819                                         __on_sender_name_appeared,
820                                         __on_sender_name_vanished,
821                                         watcher_id,
822                                         NULL);
823         } else {
824                 free(watcher_id);
825                 return false;
826         }
827         return true;
828 }
829 /* LCOV_EXCL_START */
830 static void __close_socket(GDBusMethodInvocation *invocation)
831 {
832         GDBusMessage *msg;
833         GUnixFDList *fd_list;
834         int fd_len;
835         int *returned_fds = NULL;
836
837         msg = g_dbus_method_invocation_get_message(invocation);
838         fd_list = g_dbus_message_get_unix_fd_list(msg);
839         if (fd_list != NULL) {
840                 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
841                 if (returned_fds != NULL) {
842                         close(returned_fds[0]);
843                         g_free(returned_fds);
844                 }
845         }
846 }
847 /* LCOV_EXCL_STOP */
848 static void __dbus_method_call_handler(GDBusConnection *conn,
849                                 const gchar *sender, const gchar *object_path,
850                                 const gchar *iface_name, const gchar *method_name,
851                                 GVariant *parameters, GDBusMethodInvocation *invocation,
852                                 gpointer user_data)
853 {
854         _LOGI("method_name: %s, sender: %s", method_name, sender);
855         gpointer sender_appid = g_hash_table_lookup(__sender_appid_hash, sender);
856         if (sender_appid == NULL) {
857                 if (!__check_sender_validation(parameters, sender, conn)) {
858                         _LOGE("Failed to validate");
859                         __close_socket(invocation);
860                         goto out;
861                 }
862         }
863
864         if (g_strcmp0(method_name, "send_message") == 0)
865                 __receive_message(parameters, invocation);
866 out:
867         g_dbus_method_invocation_return_value(invocation, NULL);
868 }
869
870 static const GDBusInterfaceVTable interface_vtable = {
871         __dbus_method_call_handler,
872         NULL,
873         NULL
874 };
875
876 static int __register_dbus_interface(const char *port_name, bool is_trusted)
877 {
878
879         GDBusNodeInfo *introspection_data = NULL;
880         int registration_id = 0;
881
882         static gchar introspection_prefix[] =
883                 "<node>"
884                 "  <interface name='";
885
886         static gchar introspection_postfix[] =
887                 "'>"
888                 "        <method name='send_message'>"
889                 "          <arg type='s' name='local_appid' direction='in'/>"
890                 "          <arg type='s' name='local_port' direction='in'/>"
891                 "          <arg type='b' name='local_trusted' direction='in'/>"
892                 "          <arg type='b' name='bi_dir' direction='in'/>"
893                 "          <arg type='s' name='remote_appid' direction='in'/>"
894                 "          <arg type='s' name='remote_port' direction='in'/>"
895                 "          <arg type='b' name='remote_trusted' direction='in'/>"
896                 "          <arg type='u' name='data_len' direction='in'/>"
897                 "          <arg type='s' name='data' direction='in'/>"
898                 "        </method>"
899                 "  </interface>"
900                 "</node>";
901
902         char *introspection_xml = NULL;
903         int introspection_xml_len = 0;
904
905
906         int owner_id = 0;
907         GError *error = NULL;
908         char *bus_name = NULL;
909         char *interface_name = NULL;
910         GVariant *result = NULL;
911
912         bus_name = get_encoded_name(app_id, port_name, is_trusted);
913         if (!bus_name) {
914                 _LOGE("Fail to get bus name");
915                 goto out;
916         }
917         interface_name = bus_name;
918
919         introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
920                 strlen(introspection_postfix) + 1;
921
922         introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
923         if (!introspection_xml) {
924 /* LCOV_EXCL_START */
925                 _LOGE("out of memory");
926                 goto out;
927 /* LCOV_EXCL_STOP */
928         }
929
930
931         result = g_dbus_connection_call_sync(
932                         gdbus_conn,
933                         DBUS_SERVICE_DBUS,
934                         DBUS_PATH_DBUS,
935                         DBUS_INTERFACE_DBUS,
936                         "RequestName",
937                         g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
938                         G_VARIANT_TYPE("(u)"),
939                         G_DBUS_CALL_FLAGS_NONE,
940                         -1,
941                         NULL,
942                         &error);
943         if (error) {
944                 _LOGE("RequestName fail : %s", error->message);
945                 g_error_free(error);
946                 goto out;
947         }
948         if (result == NULL) {
949                 _LOGE("fail to get name NULL");
950                 goto out;
951         }
952         g_variant_get(result, "(u)", &owner_id);
953         if (owner_id == 0) {
954                 _LOGE("Acquiring the own name is failed");
955                 goto out;
956         }
957
958         _LOGD("Acquiring the own name : %d", owner_id);
959
960         snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
961
962         introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
963         if (!introspection_data) {
964                 _LOGE("g_dbus_node_info_new_for_xml() is failed.");
965                 goto out;
966         }
967
968         registration_id = g_dbus_connection_register_object(gdbus_conn,
969                                                 MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
970                                                 &interface_vtable, NULL, NULL, NULL);
971
972         _LOGD("registration_id %d", registration_id);
973
974         if (registration_id == 0) {
975                 _LOGE("Failed to g_dbus_connection_register_object");
976                 goto out;
977         }
978
979 out:
980         if (introspection_data)
981                 g_dbus_node_info_unref(introspection_data);
982         if (introspection_xml)
983                 free(introspection_xml);
984         if (bus_name)
985                 free(bus_name);
986         if (result)
987                 g_variant_unref(result);
988
989
990         return registration_id;
991 }
992
993 static bool __message_port_register_port(const int local_id, const char *local_port, bool is_trusted, message_port_message_cb callback, void *user_data)
994 {
995         message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
996         retvm_if(!mi, false, "Malloc failed");
997
998         mi->callback = callback;
999         mi->is_trusted = is_trusted;
1000         mi->port_name = strdup(local_port);
1001         if (mi->port_name == NULL) {
1002 /* LCOV_EXCL_START */
1003                 _LOGE("Malloc failed (%s)", local_port);
1004                 free(mi);
1005                 return false;
1006 /* LCOV_EXCL_STOP */
1007         }
1008         mi->local_id = local_id;
1009         mi->user_data = user_data;
1010
1011         g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
1012         return true;
1013 }
1014
1015 int get_local_port_info(int id, message_port_local_port_info_s **info)
1016 {
1017         message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
1018
1019         if (mi == NULL)
1020                 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1021         *info = mi;
1022
1023         return MESSAGE_PORT_ERROR_NONE;
1024 }
1025
1026 int register_message_port(const char *local_port, bool is_trusted, message_port_message_cb callback, void *user_data)
1027 {
1028         _SECURE_LOGI("local_port : [%s:%s]", local_port, is_trusted ? "trusted" : "non-trusted");
1029
1030         int local_id = 0;
1031         message_port_local_port_info_s *port_info;
1032         if (!_initialized) {
1033                 if (!__initialize())
1034                         return MESSAGE_PORT_ERROR_IO_ERROR;
1035         }
1036
1037         /* Check the message port is already registed */
1038         if (is_local_port_registed(local_port, is_trusted, &local_id, &port_info)) {
1039                 port_info->callback = callback;
1040                 port_info->user_data = user_data;
1041                 __callback_info_update_user_data(local_id, callback, user_data);
1042                 return local_id;
1043         }
1044
1045         local_id = __register_dbus_interface(local_port, is_trusted);
1046         if (local_id < 1) {
1047                 _LOGE("register_dbus_interface fail !!");
1048                 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1049         }
1050
1051         if (!__message_port_register_port(local_id, local_port, is_trusted, callback, user_data))
1052                 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1053
1054         return local_id;
1055 }
1056
1057 int unregister_local_port(int local_port_id, bool trusted_port)
1058 {
1059
1060         GVariant *result;
1061         char *bus_name = NULL;
1062         GError *err = NULL;
1063         int ret = 0;
1064         message_port_local_port_info_s *mi;
1065
1066         _LOGI("unregister : %d", local_port_id);
1067
1068         if (!_initialized) {
1069                 if (!__initialize())
1070                         return MESSAGE_PORT_ERROR_IO_ERROR;
1071         }
1072
1073         mi = (message_port_local_port_info_s *)
1074                 g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
1075         if (mi == NULL)
1076                 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1077
1078         if (mi->is_trusted != trusted_port)
1079                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
1080
1081         g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
1082
1083         bus_name = get_encoded_name(app_id, mi->port_name, mi->is_trusted);
1084         if (bus_name == NULL)
1085                 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1086
1087         g_dbus_connection_unregister_object(gdbus_conn, local_port_id);
1088
1089         result = g_dbus_connection_call_sync(
1090                         gdbus_conn,
1091                         DBUS_SERVICE_DBUS,
1092                         DBUS_PATH_DBUS,
1093                         DBUS_INTERFACE_DBUS,
1094                         "ReleaseName",
1095                         g_variant_new("(s)", bus_name),
1096                         G_VARIANT_TYPE("(u)"),
1097                         G_DBUS_CALL_FLAGS_NONE,
1098                         -1,
1099                         NULL,
1100                         &err);
1101
1102         if (bus_name)
1103                 free(bus_name);
1104
1105         if (err) {
1106                 _LOGE("RequestName fail : %s", err->message);
1107                 g_error_free(err);
1108                 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1109         }
1110         g_variant_get(result, "(u)", &ret);
1111
1112         if (result)
1113                 g_variant_unref(result);
1114 /* LCOV_EXCL_START */
1115         if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
1116                 if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
1117                         _LOGE("Port Not exist");
1118                         return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1119                 } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
1120                         _LOGE("Try to release not owned name. MESSAGE_PORT_ERROR_INVALID_PARAMETER");
1121                         return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
1122                 }
1123         }
1124 /* LCOV_EXCL_STOP */
1125         g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
1126
1127         return MESSAGE_PORT_ERROR_NONE;
1128 }