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