Fix static analysis
[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                 close(fd);
254                 return NULL;
255         }
256
257         if (read_string_from_socket(fd, (char **)&pkt->remote_port_name, &pkt->remote_port_name_len) != MESSAGE_PORT_ERROR_NONE) {
258 /* LCOV_EXCL_START */
259                 LOGE("read socket fail: port_name");
260                 free(pkt->remote_port_name);
261                 free(pkt);
262                 return NULL;
263 /* LCOV_EXCL_STOP */
264         }
265
266         if (read_socket(fd, (char *)&pkt->is_bidirection, sizeof(pkt->is_bidirection), &nb) != MESSAGE_PORT_ERROR_NONE) {
267 /* LCOV_EXCL_START */
268                 LOGE("read socket fail: is_bidirection");
269                 free(pkt->remote_port_name);
270                 free(pkt);
271                 return NULL;
272 /* LCOV_EXCL_STOP */
273         }
274
275         if (read_socket(fd, (char *)&pkt->is_trusted, sizeof(pkt->is_trusted), &nb) != MESSAGE_PORT_ERROR_NONE) {
276 /* LCOV_EXCL_START */
277                 LOGE("read socket fail: is_trusted");
278                 free(pkt->remote_port_name);
279                 free(pkt);
280                 return NULL;
281 /* LCOV_EXCL_STOP */
282         }
283
284         if (read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGE_PORT_ERROR_NONE) {
285 /* LCOV_EXCL_START */
286                 LOGE("read socket fail: data");
287                 if (pkt->data)
288                         free(pkt->data);
289                 free(pkt->remote_port_name);
290                 free(pkt);
291                 return NULL;
292 /* LCOV_EXCL_STOP */
293         }
294
295         return pkt;
296 }
297
298 static bool __validate_callback_info(callback_key_info_s *key_info)
299 {
300         GList *cb_list;
301
302         cb_list = g_hash_table_lookup(__callback_info_hash,
303                         GUINT_TO_POINTER(key_info->local_id));
304         if (cb_list == NULL) {
305                 _LOGI("local_info : %d is already released", key_info->local_id);
306                 return false;
307         }
308
309         cb_list = g_list_find(cb_list, key_info->callback_info);
310         if (cb_list == NULL) {
311                 _LOGI("local_info : %d is already released", key_info->local_id);
312                 return false;
313         }
314
315         return true;
316 }
317
318 static bool __validate_local_info(callback_key_info_s *key_info)
319 {
320         GList *cb_list;
321
322         cb_list = g_hash_table_lookup(__local_port_info,
323                         GUINT_TO_POINTER(key_info->local_id));
324         if (cb_list == NULL) {
325                 _LOGI("local_info : %d is already released", key_info->local_id);
326                 return false;
327         }
328
329         return true;
330 }
331
332 static gboolean __socket_request_handler(GIOChannel *gio,
333                 GIOCondition cond,
334                 gpointer data)
335 {
336         int fd = 0;
337         message_port_callback_info_s *mi = NULL;
338         callback_key_info_s *key_info;
339         message_port_pkt_s *pkt = NULL;
340         message_port_local_port_info_s *local_port_info;
341         bundle *kb = NULL;
342         bool ret = true;
343         bool existed = true;
344
345         key_info = (callback_key_info_s *)data;
346         if (key_info == NULL)
347                 return FALSE;
348
349         message_port_lock_mutex();
350         if (__validate_callback_info(key_info) == false) {
351                 ret = FALSE;
352                 existed = FALSE;
353                 message_port_unlock_mutex();
354                 goto out;
355         }
356
357         if (__validate_local_info(key_info) == false) {
358                 ret = FALSE;
359                 message_port_unlock_mutex();
360                 goto out;
361         }
362         message_port_unlock_mutex();
363
364         mi = key_info->callback_info;
365
366         local_port_info = mi->local_info;
367         if (local_port_info == NULL || local_port_info->callback == NULL) {
368                 _LOGE("Failed to get callback info");
369                 ret = FALSE;
370                 goto out;
371         }
372
373         if (cond == G_IO_HUP) {
374                 _LOGI("socket G_IO_HUP");
375                 ret = FALSE;
376                 goto out;
377         }
378
379         fd = g_io_channel_unix_get_fd(gio);
380         if (fd < 0) {
381                 _LOGE("fail to get fd from io channel");
382                 ret = FALSE;
383                 goto out;
384         }
385
386         pkt = __message_port_recv_raw(fd);
387         if (pkt == NULL) {
388                 _LOGE("recv error on SOCKET");
389                 ret = FALSE;
390                 goto out;
391         }
392
393         kb = bundle_decode(pkt->data, pkt->data_len);
394         if (!kb) {
395                 _LOGE("Invalid argument : message");
396                 ret = FALSE;
397                 goto out;
398         }
399
400         if (pkt->is_bidirection)
401                 local_port_info->callback(mi->local_id, mi->remote_app_id,
402                         pkt->remote_port_name, pkt->is_trusted, kb, local_port_info->user_data);
403         else
404                 local_port_info->callback(mi->local_id, mi->remote_app_id,
405                         NULL, pkt->is_trusted, kb, local_port_info->user_data);
406
407         bundle_free(kb);
408
409 out:
410         if (pkt) {
411                 if (pkt->remote_port_name)
412                         free(pkt->remote_port_name);
413                 if (pkt->data)
414                         free(pkt->data);
415                 free(pkt);
416         }
417
418         if (mi && ret == FALSE && existed == TRUE) {
419                 message_port_lock_mutex();
420                 __callback_info_free_by_info(mi);
421                 message_port_unlock_mutex();
422         }
423
424         return ret;
425 }
426
427
428 static void __socket_destroy_handler(gpointer data)
429 {
430         _LOGI("__socket_destroy_handler");
431         callback_key_info_s *key_info = (callback_key_info_s *)data;
432         free(key_info);
433 }
434
435 static callback_key_info_s *__create_callback_key_info(message_port_callback_info_s *callback_info)
436 {
437         callback_key_info_s *_key_info = (callback_key_info_s *)
438                 calloc(1, sizeof(callback_key_info_s));
439
440         if (_key_info == NULL) {
441                 _LOGE("out of memory");
442                 return NULL;
443         }
444
445         _key_info->local_id = callback_info->local_id;
446         _key_info->callback_info = callback_info;
447
448         return _key_info;
449 }
450
451 static message_port_callback_info_s *__create_callback_info(message_port_local_port_info_s *mi, char *local_appid)
452 {
453         message_port_local_port_info_s *local_info = NULL;
454         message_port_callback_info_s *callback_info = NULL;
455         bool ret = true;
456
457         callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
458         if (callback_info == NULL) {
459                 _LOGE("out of memory");
460                 return NULL;
461         }
462
463         local_info = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
464         if (local_info == NULL) {
465                 ret = false;
466                 _LOGE("out of memory");
467                 goto out;
468         }
469
470         callback_info->local_id = mi->local_id;
471         callback_info->local_info = local_info;
472         callback_info->remote_app_id = strdup(local_appid);
473         if (callback_info->remote_app_id == NULL) {
474                 ret = false;
475                 _LOGE("out of memory");
476                 goto out;
477         }
478
479         local_info->port_name = strdup(mi->port_name);
480         if (local_info->port_name == NULL) {
481                 ret = false;
482                 _LOGE("out of memory");
483                 goto out;
484         }
485
486         local_info->callback = mi->callback;
487         local_info->is_trusted = mi->is_trusted;
488         local_info->local_id = mi->local_id;
489         local_info->user_data = mi->user_data;
490
491 out:
492         if (ret == false) {
493                 __callback_info_free(callback_info);
494                 return NULL;
495         }
496
497         return callback_info;
498 }
499
500 static bool __callback_info_append(message_port_callback_info_s *callback_info)
501 {
502         GList *callback_info_list = NULL;
503         message_port_callback_info_s *head_callback_info;
504
505         callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
506         if (callback_info_list == NULL) {
507                 head_callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
508                 if (head_callback_info == NULL) {
509                         _LOGE("fail to alloc head_callback_info");
510                         return false;
511                 }
512                 head_callback_info->local_id = 0;
513                 head_callback_info->remote_app_id = NULL;
514                 head_callback_info->local_info = NULL;
515                 head_callback_info->gio_read = NULL;
516                 head_callback_info->g_src_id = 0;
517                 callback_info_list = g_list_append(callback_info_list, head_callback_info);
518                 callback_info_list = g_list_append(callback_info_list, callback_info);
519                 g_hash_table_insert(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id), callback_info_list);
520         } else {
521                 callback_info_list = g_list_append(callback_info_list, callback_info);
522         }
523
524         return true;
525 }
526
527 static void __callback_info_update_user_data(int local_id, message_port_message_cb callback, void *user_data)
528 {
529         GList *callback_info_list;
530         GList *iter;
531         message_port_callback_info_s *callback_info;
532
533         callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(local_id));
534         if (callback_info_list != NULL) {
535                 for (iter = callback_info_list; iter != NULL; iter = iter->next) {
536                         callback_info = (message_port_callback_info_s *)iter->data;
537                         if (callback_info->local_info != NULL) {
538                                 callback_info->local_info->callback = callback;
539                                 callback_info->local_info->user_data = user_data;
540                         }
541                 }
542         } else {
543                 _LOGE("fail to find local_id %d ", local_id);
544         }
545 }
546
547 static bool __receive_message(GVariant *parameters, GDBusMethodInvocation *invocation)
548 {
549         char *local_port = NULL;
550         char *local_appid = NULL;
551         char *remote_appid = NULL;
552         char *remote_port = NULL;
553         gboolean local_trusted = false;
554         gboolean remote_trusted = false;
555         gboolean bi_dir = false;
556         int len = 0;
557
558         bundle *data = NULL;
559         bundle_raw *raw = NULL;
560         message_port_local_port_info_s *mi;
561         int local_reg_id = 0;
562         message_port_callback_info_s *callback_info = NULL;
563         callback_key_info_s *key_info;
564
565         char buf[1024];
566         GDBusMessage *msg;
567         GUnixFDList *fd_list;
568         int fd_len;
569         int *returned_fds = NULL;
570         int fd;
571         bool ret = false;
572         char *key_appid;
573
574         g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
575                         &remote_appid, &remote_port, &remote_trusted, &len, &raw);
576
577         if (!remote_port) {
578                 _LOGE("Invalid argument : remote_port is NULL");
579                 goto out;
580         }
581         if (!remote_appid) {
582                 _LOGE("Invalid argument : remote_appid is NULL");
583                 goto out;
584         }
585
586         if (!local_appid) {
587                 _LOGE("Invalid argument : local_appid");
588                 goto out;
589         }
590
591         message_port_lock_mutex();
592         if (!is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
593                 _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
594                 message_port_unlock_mutex();
595                 goto out;
596         }
597
598         callback_info = __create_callback_info(mi, local_appid);
599         if (callback_info == NULL) {
600                 message_port_unlock_mutex();
601                 goto out;
602         }
603         message_port_unlock_mutex();
604
605         if (!local_port) {
606                 _LOGE("Invalid argument : local_port");
607                 goto out;
608         }
609         if (strcmp(remote_appid, app_id) != 0) {
610                 _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
611                 goto out;
612         }
613         if (strcmp(remote_port, callback_info->local_info->port_name) != 0) {
614                 _LOGE("Invalid argument : remote_port (%s)", remote_port);
615                 goto out;
616         }
617         if (!len) {
618                 _LOGE("Invalid argument : data_len");
619                 goto out;
620         }
621         if (remote_trusted) {
622                 if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
623                         if (!is_preloaded(local_appid, remote_appid)) {
624                                 int ret = check_certificate(local_appid, remote_appid);
625                                 if (ret == MESSAGE_PORT_ERROR_NONE) {
626                                         key_appid = strdup(local_appid);
627                                         if (key_appid)
628                                                 g_hash_table_insert(__trusted_app_list_hash, key_appid, "TRUE");
629                                 } else {
630                                         _LOGE("The application (%s) is not signed with the same certificate",
631                                                         local_appid);
632                                         goto out;
633                                 }
634                         }
635                 }
636         }
637
638         data = bundle_decode(raw, len);
639         if (!data) {
640                 _LOGE("Invalid argument : message");
641                 goto out;
642         }
643
644         LOGD("call calback %s", local_appid);
645         if (bi_dir)
646                 callback_info->local_info->callback(callback_info->local_info->local_id,
647                         local_appid, local_port, local_trusted, data, callback_info->local_info->user_data);
648         else
649                 callback_info->local_info->callback(callback_info->local_info->local_id,
650                         local_appid, NULL, false, data, callback_info->local_info->user_data);
651         bundle_free(data);
652
653         ret = true;
654
655         msg = g_dbus_method_invocation_get_message(invocation);
656         fd_list = g_dbus_message_get_unix_fd_list(msg);
657
658         /* When application send message to self fd_list is NULL */
659         if (fd_list != NULL) {
660                 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
661                 if (returned_fds == NULL) {
662                         _LOGE("fail to get fds");
663                         ret = false;
664                         goto out;
665                 }
666                 fd = returned_fds[0];
667
668                 LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
669                 if (fd > 0) {
670                         callback_info->gio_read = g_io_channel_unix_new(fd);
671                         if (!callback_info->gio_read) {
672                                 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
673                                 ret = false;
674                                 goto out;
675                         }
676
677                         key_info = __create_callback_key_info(callback_info);
678                         if (key_info == NULL) {
679                                 _LOGE("out of memory");
680                                 ret = false;
681                                 goto out;
682                         }
683
684                         callback_info->g_src_id = g_io_add_watch_full(
685                                         callback_info->gio_read,
686                                         G_PRIORITY_DEFAULT,
687                                         G_IO_IN | G_IO_HUP,
688                                         __socket_request_handler,
689                                         (gpointer)key_info,
690                                         __socket_destroy_handler);
691                         if (callback_info->g_src_id == 0) {
692                                 _LOGE("fail to add watch on socket");
693                                 free(key_info);
694                                 ret = false;
695                                 goto out;
696                         }
697
698                         message_port_lock_mutex();
699                         if (__callback_info_append(callback_info) == false) {
700                                 _LOGE("fail to append callback_info");
701                                 ret = false;
702                         }
703                         message_port_unlock_mutex();
704                 }
705         }
706
707 out:
708         if (ret == false)
709                 __callback_info_free(callback_info);
710
711         if (returned_fds)
712                 g_free(returned_fds);
713
714         return ret;
715 }
716
717 static void __on_sender_name_appeared(GDBusConnection *connection,
718                 const gchar     *name,
719                 const gchar     *name_owner,
720                 gpointer         user_data)
721 {
722         _LOGI("sender name appeared : %s", name);
723 }
724
725 static void __on_sender_name_vanished(GDBusConnection *connection,
726                 const gchar     *name,
727                 gpointer         user_data)
728 {
729         int *watcher_id = (int *)user_data;
730         char *local_appid;
731
732         local_appid = (char *)g_hash_table_lookup(__sender_appid_hash, name);
733         if (local_appid) {
734                 g_hash_table_remove(__trusted_app_list_hash, (gpointer)local_appid);
735                 g_hash_table_remove(__sender_appid_hash, (gpointer)name);
736         }
737
738         if (watcher_id) {
739                 if (*watcher_id > 0)
740                         g_bus_unwatch_name(*watcher_id);
741                 else
742                         LOGE("Invalid watcher_id %d", *watcher_id);
743                 free(watcher_id);
744         } else {
745                 LOGE("watcher_id is NULL");
746         }
747 }
748
749 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
750 {
751         int ret = 0;
752         char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
753         char *local_appid = NULL;
754         int pid = __get_sender_pid(conn, sender);
755         int *watcher_id = (int *)calloc(1, sizeof(int));
756         char *_sender;
757         char *_appid;
758         retvm_if(!watcher_id, false, "Malloc failed");
759
760         ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
761         if (ret != AUL_R_OK) {
762                 _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
763                 free(watcher_id);
764                 return false;
765         }
766
767         g_variant_get_child(parameters, 0, "&s", &local_appid);
768         if (local_appid == NULL) {
769                 _LOGE("appid is NULL : (%s) (%d)", sender, pid);
770                 free(watcher_id);
771                 return false;
772         }
773
774         if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
775                 _LOGD("insert sender !!!!! %s", sender);
776                 _sender = strdup(sender);
777                 if (_sender == NULL) {
778                         _LOGE("out of memory");
779                         free(watcher_id);
780                         return false;
781                 }
782
783                 _appid = strdup(local_appid);
784                 if (_appid == NULL) {
785                         _LOGE("out of memory");
786                         free(watcher_id);
787                         free(_sender);
788                         return false;
789                 }
790
791                 g_hash_table_insert(__sender_appid_hash, (gpointer)_sender, (gpointer)_appid);
792                 *watcher_id = g_bus_watch_name_on_connection(
793                                         gdbus_conn,
794                                         sender,
795                                         G_BUS_NAME_WATCHER_FLAGS_NONE,
796                                         __on_sender_name_appeared,
797                                         __on_sender_name_vanished,
798                                         watcher_id,
799                                         NULL);
800         } else {
801                 free(watcher_id);
802                 return false;
803         }
804         return true;
805 }
806
807 static void __close_socket(GDBusMethodInvocation *invocation)
808 {
809         GDBusMessage *msg;
810         GUnixFDList *fd_list;
811         int fd_len;
812         int *returned_fds = NULL;
813
814         msg = g_dbus_method_invocation_get_message(invocation);
815         fd_list = g_dbus_message_get_unix_fd_list(msg);
816         if (fd_list != NULL) {
817                 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
818                 if (returned_fds != NULL) {
819                         close(returned_fds[0]);
820                 }
821         }
822 }
823 static void __dbus_method_call_handler(GDBusConnection *conn,
824                                 const gchar *sender, const gchar *object_path,
825                                 const gchar *iface_name, const gchar *method_name,
826                                 GVariant *parameters, GDBusMethodInvocation *invocation,
827                                 gpointer user_data)
828 {
829         _LOGI("method_name: %s, sender: %s", method_name, sender);
830         gpointer sender_appid = g_hash_table_lookup(__sender_appid_hash, sender);
831         if (sender_appid == NULL) {
832                 if (!__check_sender_validation(parameters, sender, conn)) {
833                         _LOGE("Failed to validate");
834                         __close_socket(invocation);
835                         goto out;
836                 }
837         }
838
839         if (g_strcmp0(method_name, "send_message") == 0)
840                 __receive_message(parameters, invocation);
841 out:
842         g_dbus_method_invocation_return_value(invocation, NULL);
843 }
844
845 static const GDBusInterfaceVTable interface_vtable = {
846         __dbus_method_call_handler,
847         NULL,
848         NULL
849 };
850
851 static int __register_dbus_interface(const char *port_name, bool is_trusted)
852 {
853
854         GDBusNodeInfo *introspection_data = NULL;
855         int registration_id = 0;
856
857         static gchar introspection_prefix[] =
858                 "<node>"
859                 "  <interface name='";
860
861         static gchar introspection_postfix[] =
862                 "'>"
863                 "        <method name='send_message'>"
864                 "          <arg type='s' name='local_appid' direction='in'/>"
865                 "          <arg type='s' name='local_port' direction='in'/>"
866                 "          <arg type='b' name='local_trusted' direction='in'/>"
867                 "          <arg type='b' name='bi_dir' direction='in'/>"
868                 "          <arg type='s' name='remote_appid' direction='in'/>"
869                 "          <arg type='s' name='remote_port' direction='in'/>"
870                 "          <arg type='b' name='remote_trusted' direction='in'/>"
871                 "          <arg type='u' name='data_len' direction='in'/>"
872                 "          <arg type='s' name='data' direction='in'/>"
873                 "        </method>"
874                 "  </interface>"
875                 "</node>";
876
877         char *introspection_xml = NULL;
878         int introspection_xml_len = 0;
879
880
881         int owner_id = 0;
882         GError *error = NULL;
883         char *bus_name = NULL;
884         char *interface_name = NULL;
885         GVariant *result = NULL;
886
887         bus_name = get_encoded_name(app_id, port_name, is_trusted);
888         if (!bus_name) {
889                 _LOGE("Fail to get bus name");
890                 goto out;
891         }
892         interface_name = bus_name;
893
894         introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
895                 strlen(introspection_postfix) + 1;
896
897         introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
898         if (!introspection_xml) {
899                 _LOGE("out of memory");
900                 goto out;
901         }
902
903
904         result = g_dbus_connection_call_sync(
905                         gdbus_conn,
906                         DBUS_SERVICE_DBUS,
907                         DBUS_PATH_DBUS,
908                         DBUS_INTERFACE_DBUS,
909                         "RequestName",
910                         g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
911                         G_VARIANT_TYPE("(u)"),
912                         G_DBUS_CALL_FLAGS_NONE,
913                         -1,
914                         NULL,
915                         &error);
916         if (error) {
917                 _LOGE("RequestName fail : %s", error->message);
918                 g_error_free(error);
919                 goto out;
920         }
921         if (result == NULL) {
922                 _LOGE("fail to get name NULL");
923                 goto out;
924         }
925         g_variant_get(result, "(u)", &owner_id);
926         if (owner_id == 0) {
927                 _LOGE("Acquiring the own name is failed");
928                 goto out;
929         }
930
931         _LOGD("Acquiring the own name : %d", owner_id);
932
933         snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
934
935         introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
936         if (!introspection_data) {
937                 _LOGE("g_dbus_node_info_new_for_xml() is failed.");
938                 goto out;
939         }
940
941         registration_id = g_dbus_connection_register_object(gdbus_conn,
942                                                 MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
943                                                 &interface_vtable, NULL, NULL, NULL);
944
945         _LOGD("registration_id %d", registration_id);
946
947         if (registration_id == 0) {
948                 _LOGE("Failed to g_dbus_connection_register_object");
949                 goto out;
950         }
951
952 out:
953         if (introspection_data)
954                 g_dbus_node_info_unref(introspection_data);
955         if (introspection_xml)
956                 free(introspection_xml);
957         if (bus_name)
958                 free(bus_name);
959         if (result)
960                 g_variant_unref(result);
961
962
963         return registration_id;
964 }
965
966 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)
967 {
968         message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
969         retvm_if(!mi, false, "Malloc failed");
970
971         mi->callback = callback;
972         mi->is_trusted = is_trusted;
973         mi->port_name = strdup(local_port);
974         if (mi->port_name == NULL) {
975                 _LOGE("Malloc failed (%s)", local_port);
976                 free(mi);
977                 return false;
978         }
979         mi->local_id = local_id;
980         mi->user_data = user_data;
981
982         g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
983         return true;
984 }
985
986 int get_local_port_info(int id, message_port_local_port_info_s **info)
987 {
988         message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
989
990         if (mi == NULL)
991                 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
992         *info = mi;
993
994         return MESSAGE_PORT_ERROR_NONE;
995 }
996
997 int register_message_port(const char *local_port, bool is_trusted, message_port_message_cb callback, void *user_data)
998 {
999         _SECURE_LOGI("local_port : [%s:%s]", local_port, is_trusted ? "trusted" : "non-trusted");
1000
1001         int local_id = 0;
1002         message_port_local_port_info_s *port_info;
1003         if (!_initialized) {
1004                 if (!__initialize())
1005                         return MESSAGE_PORT_ERROR_IO_ERROR;
1006         }
1007
1008         /* Check the message port is already registed */
1009         if (is_local_port_registed(local_port, is_trusted, &local_id, &port_info)) {
1010                 port_info->callback = callback;
1011                 port_info->user_data = user_data;
1012                 __callback_info_update_user_data(local_id, callback, user_data);
1013                 return local_id;
1014         }
1015
1016         local_id = __register_dbus_interface(local_port, is_trusted);
1017         if (local_id < 1) {
1018                 _LOGE("register_dbus_interface fail !!");
1019                 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1020         }
1021
1022         if (!__message_port_register_port(local_id, local_port, is_trusted, callback, user_data))
1023                 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1024
1025         return local_id;
1026 }
1027
1028 int unregister_local_port(int local_port_id, bool trusted_port)
1029 {
1030
1031         GVariant *result;
1032         char *bus_name = NULL;
1033         GError *err = NULL;
1034         int ret = 0;
1035         message_port_local_port_info_s *mi;
1036
1037         _LOGI("unregister : %d", local_port_id);
1038
1039         if (!_initialized) {
1040                 if (!__initialize())
1041                         return MESSAGE_PORT_ERROR_IO_ERROR;
1042         }
1043
1044         mi = (message_port_local_port_info_s *)
1045                 g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
1046         if (mi == NULL)
1047                 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1048
1049         if (mi->is_trusted != trusted_port)
1050                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
1051
1052         g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
1053
1054         bus_name = get_encoded_name(app_id, mi->port_name, mi->is_trusted);
1055         if (bus_name == NULL)
1056                 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1057
1058         g_dbus_connection_unregister_object(gdbus_conn, local_port_id);
1059
1060         result = g_dbus_connection_call_sync(
1061                         gdbus_conn,
1062                         DBUS_SERVICE_DBUS,
1063                         DBUS_PATH_DBUS,
1064                         DBUS_INTERFACE_DBUS,
1065                         "ReleaseName",
1066                         g_variant_new("(s)", bus_name),
1067                         G_VARIANT_TYPE("(u)"),
1068                         G_DBUS_CALL_FLAGS_NONE,
1069                         -1,
1070                         NULL,
1071                         &err);
1072
1073         if (bus_name)
1074                 free(bus_name);
1075
1076         if (err) {
1077                 _LOGE("RequestName fail : %s", err->message);
1078                 g_error_free(err);
1079                 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1080         }
1081         g_variant_get(result, "(u)", &ret);
1082
1083         if (result)
1084                 g_variant_unref(result);
1085
1086         if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
1087
1088                 if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
1089                         _LOGE("Port Not exist");
1090                         return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1091                 } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
1092                         _LOGE("Try to release not owned name. MESSAGE_PORT_ERROR_INVALID_PARAMETER");
1093                         return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
1094                 }
1095         }
1096         g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
1097
1098         return MESSAGE_PORT_ERROR_NONE;
1099 }