2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <bundle_internal.h>
23 #include <sys/socket.h>
29 #include <gio/gunixfdlist.h>
30 #include <glib-unix.h>
32 #include "message_port_log.h"
33 #include "message_port_common.h"
34 #include "message_port_remote.h"
37 #define MAX_PACKAGE_STR_SIZE 512
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 */
43 #define MAX_RETRY_CNT 10
44 #define SOCK_PAIR_SENDER 0
45 #define SOCK_PAIR_RECEIVER 1
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;
53 typedef struct message_port_pkt {
54 int remote_port_name_len;
55 char *remote_port_name;
62 typedef struct message_port_callback_info {
63 message_port_local_port_info_s *local_info;
68 } message_port_callback_info_s;
70 static void __callback_info_free(gpointer data)
72 message_port_callback_info_s *callback_info = (message_port_callback_info_s *)data;
74 if (callback_info == NULL)
77 if (callback_info->remote_app_id)
78 FREE_AND_NULL(callback_info->remote_app_id);
80 if (callback_info->gio_read != NULL) {
81 g_io_channel_shutdown(callback_info->gio_read, TRUE, &error);
83 _LOGE("g_io_channel_shutdown error : %s", error->message);
86 g_io_channel_unref(callback_info->gio_read);
87 callback_info->gio_read = NULL;
90 if (callback_info->g_src_id != 0) {
91 g_source_remove(callback_info->g_src_id);
92 callback_info->g_src_id = 0;
95 FREE_AND_NULL(callback_info);
99 static void __callback_info_free_by_info(message_port_callback_info_s *callback_info)
101 GList *callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
104 if (callback_info_list == NULL)
107 find_list = g_list_find(callback_info_list, callback_info);
108 if (find_list == NULL)
111 callback_info_list = g_list_remove_link(callback_info_list, find_list);
112 __callback_info_free(callback_info);
113 g_list_free(find_list);
117 static void __hash_destroy_callback_info(gpointer data)
120 GList *callback_list = (GList *)data;
121 if (callback_list != NULL)
122 g_list_free_full(callback_list, __callback_info_free);
125 /* LCOV_EXCL_START */
126 static void __hash_destory_local_value(gpointer data)
128 message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
131 free(mli->port_name);
137 static bool __initialize(void)
139 if (!initialized_common) {
140 if (!initialize_common())
144 if (__local_port_info == NULL) {
145 __local_port_info = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destory_local_value);
146 retvm_if(!__local_port_info, false, "fail to create __local_port_info");
149 if (__sender_appid_hash == NULL) {
150 __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
151 retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash");
154 if (__trusted_app_list_hash == NULL) {
155 __trusted_app_list_hash = g_hash_table_new(g_str_hash, g_str_equal);
156 retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
159 if (__callback_info_hash == NULL) {
160 __callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destroy_callback_info);
161 retvm_if(!__callback_info_hash, false, "fail to create __callback_info_hash");
169 bool is_local_port_registed(const char *local_port, bool trusted, int *local_id, message_port_local_port_info_s **lpi)
174 g_hash_table_iter_init(&iter, __local_port_info);
175 while (g_hash_table_iter_next(&iter, &key, &value)) {
176 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)value;
178 if ((mi->is_trusted == trusted) && strcmp(mi->port_name, local_port) == 0) {
179 *local_id = mi->local_id;
188 static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
190 GDBusMessage *msg = NULL;
191 GDBusMessage *reply = NULL;
196 msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
197 "org.freedesktop.DBus", "GetConnectionUnixProcessID");
199 _LOGE("Can't allocate new method call");
203 g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
204 reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
205 G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
209 _LOGE("Failed to get pid [%s]", err->message);
215 body = g_dbus_message_get_body(reply);
216 g_variant_get(body, "(u)", &pid);
222 g_object_unref(reply);
227 static message_port_pkt_s *__message_port_recv_raw(int fd)
229 message_port_pkt_s *pkt = NULL;
232 pkt = (message_port_pkt_s *)calloc(sizeof(message_port_pkt_s), 1);
238 if (read_string_from_socket(fd, (char **)&pkt->remote_port_name, &pkt->remote_port_name_len) != MESSAGE_PORT_ERROR_NONE) {
239 /* LCOV_EXCL_START */
240 LOGE("read socket fail: port_name");
241 free(pkt->remote_port_name);
247 if (read_socket(fd, (char *)&pkt->is_bidirection, sizeof(pkt->is_bidirection), &nb) != MESSAGE_PORT_ERROR_NONE) {
248 /* LCOV_EXCL_START */
249 LOGE("read socket fail: is_bidirection");
250 free(pkt->remote_port_name);
256 if (read_socket(fd, (char *)&pkt->is_trusted, sizeof(pkt->is_trusted), &nb) != MESSAGE_PORT_ERROR_NONE) {
257 /* LCOV_EXCL_START */
258 LOGE("read socket fail: is_trusted");
259 free(pkt->remote_port_name);
265 if (read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGE_PORT_ERROR_NONE) {
266 /* LCOV_EXCL_START */
267 LOGE("read socket fail: data");
270 free(pkt->remote_port_name);
279 static gboolean __socket_request_handler(GIOChannel *gio,
284 message_port_callback_info_s *mi;
285 message_port_pkt_s *pkt;
286 message_port_local_port_info_s *local_port_info;
288 GError *error = NULL;
291 mi = (message_port_callback_info_s *)data;
294 g_io_channel_shutdown(gio, TRUE, &error);
296 _LOGE("g_io_channel_shutdown error : %s", error->message);
299 g_io_channel_unref(gio);
303 local_port_info = mi->local_info;
304 if (local_port_info == NULL || local_port_info->callback == NULL) {
305 _LOGE("Failed to get callback info");
306 __callback_info_free_by_info(mi);
310 if (cond == G_IO_HUP) {
311 _LOGI("socket G_IO_HUP");
312 __callback_info_free_by_info(mi);
316 fd = g_io_channel_unix_get_fd(gio);
318 _LOGE("fail to get fd from io channel");
319 __callback_info_free_by_info(mi);
323 pkt = __message_port_recv_raw(fd);
325 _LOGE("recv error on SOCKET");
326 __callback_info_free_by_info(mi);
330 kb = bundle_decode(pkt->data, pkt->data_len);
332 _LOGE("Invalid argument : message");
333 __callback_info_free_by_info(mi);
338 if (pkt->is_bidirection)
339 local_port_info->callback(mi->local_id, mi->remote_app_id,
340 pkt->remote_port_name, pkt->is_trusted, kb, local_port_info->user_data);
342 local_port_info->callback(mi->local_id, mi->remote_app_id,
343 NULL, pkt->is_trusted, kb, local_port_info->user_data);
349 if (pkt->remote_port_name)
350 free(pkt->remote_port_name);
359 static bool __receive_message(GVariant *parameters, GDBusMethodInvocation *invocation)
361 char *local_port = NULL;
362 char *local_appid = NULL;
363 char *remote_appid = NULL;
364 char *remote_port = NULL;
365 gboolean local_trusted = false;
366 gboolean remote_trusted = false;
367 gboolean bi_dir = false;
371 bundle_raw *raw = NULL;
372 message_port_local_port_info_s *mi;
373 int local_reg_id = 0;
374 message_port_callback_info_s *callback_info = NULL;
375 message_port_callback_info_s *head_callback_info;
376 GList *callback_info_list = NULL;
380 GUnixFDList *fd_list;
382 int *returned_fds = NULL;
386 g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
387 &remote_appid, &remote_port, &remote_trusted, &len, &raw);
390 _LOGE("Invalid argument : remote_port is NULL");
394 _LOGE("Invalid argument : remote_appid is NULL");
397 if (!is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
398 _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
402 _LOGE("Invalid argument : local_appid");
406 _LOGE("Invalid argument : local_port");
409 if (strcmp(remote_appid, app_id) != 0) {
410 _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
413 if (strcmp(remote_port, mi->port_name) != 0) {
414 _LOGE("Invalid argument : remote_port (%s)", remote_port);
418 _LOGE("Invalid argument : data_len");
421 if (remote_trusted) {
422 if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
423 if (!is_preloaded(local_appid, remote_appid)) {
424 int ret = check_certificate(local_appid, remote_appid);
425 if (ret == MESSAGE_PORT_ERROR_NONE)
426 g_hash_table_insert(__trusted_app_list_hash, local_appid, "TRUE");
428 _LOGE("The application (%s) is not signed with the same certificate",
436 callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
437 if (callback_info == NULL) {
438 _LOGE("out of memory");
442 callback_info->local_id = mi->local_id;
443 callback_info->local_info = mi;
444 callback_info->remote_app_id = strdup(local_appid);
445 if (callback_info->remote_app_id == NULL) {
446 _LOGE("out of memory");
450 msg = g_dbus_method_invocation_get_message(invocation);
451 fd_list = g_dbus_message_get_unix_fd_list(msg);
453 /* When application send message to self fd_list is NULL */
454 if (fd_list != NULL) {
455 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
456 if (returned_fds == NULL) {
457 _LOGE("fail to get fds");
460 fd = returned_fds[0];
462 LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
465 callback_info->gio_read = g_io_channel_unix_new(fd);
466 if (!callback_info->gio_read) {
467 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
471 callback_info->g_src_id = g_io_add_watch(callback_info->gio_read, G_IO_IN | G_IO_HUP,
472 __socket_request_handler, (gpointer)callback_info);
473 if (callback_info->g_src_id == 0) {
474 _LOGE("fail to add watch on socket");
478 callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(mi->local_id));
479 if (callback_info_list == NULL) {
480 head_callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
481 if (head_callback_info == NULL) {
482 _LOGE("fail to alloc head_callback_info");
485 head_callback_info->local_id = 0;
486 head_callback_info->remote_app_id = NULL;
487 head_callback_info->local_info = NULL;
488 head_callback_info->gio_read = NULL;
489 head_callback_info->g_src_id = 0;
490 callback_info_list = g_list_append(callback_info_list, head_callback_info);
491 callback_info_list = g_list_append(callback_info_list, callback_info);
492 g_hash_table_insert(__callback_info_hash, GUINT_TO_POINTER(mi->local_id), callback_info_list);
494 callback_info_list = g_list_append(callback_info_list, callback_info);
499 data = bundle_decode(raw, len);
501 _LOGE("Invalid argument : message");
505 LOGD("call calback %s", local_appid);
507 mi->callback(mi->local_id, local_appid, local_port, local_trusted, data, mi->user_data);
509 mi->callback(mi->local_id, local_appid, NULL, false, data, mi->user_data);
515 __callback_info_free(callback_info);
523 static void __on_sender_name_appeared(GDBusConnection *connection,
525 const gchar *name_owner,
528 _LOGI("sender name appeared : %s", name);
531 static void __on_sender_name_vanished(GDBusConnection *connection,
535 gboolean remove_result = FALSE;
536 int *watcher_id = (int *)user_data;
537 remove_result = g_hash_table_remove(__sender_appid_hash, (gpointer)name);
539 _LOGE("Fail to remove sender appid from hash : %s", name);
543 g_bus_unwatch_name(*watcher_id);
545 LOGE("Invalid watcher_id %d", *watcher_id);
548 LOGE("watcher_id is NULL");
552 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
555 char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
556 char *local_appid = NULL;
557 int pid = __get_sender_pid(conn, sender);
558 int *watcher_id = (int *)calloc(1, sizeof(int));
560 retvm_if(!watcher_id, false, "Malloc failed");
562 ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
563 if (ret != AUL_R_OK) {
564 _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
569 g_variant_get_child(parameters, 0, "&s", &local_appid);
570 if (local_appid == NULL) {
571 _LOGE("appid is NULL : (%s) (%d)", sender, pid);
576 if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
577 _LOGD("insert sender !!!!! %s", sender);
578 _sender = strdup(sender);
579 if (_sender == NULL) {
580 _LOGE("out of memory");
584 g_hash_table_insert(__sender_appid_hash, (gpointer)_sender, GINT_TO_POINTER(pid));
585 *watcher_id = g_bus_watch_name_on_connection(
588 G_BUS_NAME_WATCHER_FLAGS_NONE,
589 __on_sender_name_appeared,
590 __on_sender_name_vanished,
600 static void __dbus_method_call_handler(GDBusConnection *conn,
601 const gchar *sender, const gchar *object_path,
602 const gchar *iface_name, const gchar *method_name,
603 GVariant *parameters, GDBusMethodInvocation *invocation,
606 _LOGI("method_name: %s, sender: %s", method_name, sender);
607 gpointer sender_pid = g_hash_table_lookup(__sender_appid_hash, sender);
608 if (sender_pid == NULL) {
609 if (!__check_sender_validation(parameters, sender, conn))
612 if (g_strcmp0(method_name, "send_message") == 0)
613 __receive_message(parameters, invocation);
615 g_dbus_method_invocation_return_value(invocation, NULL);
618 static const GDBusInterfaceVTable interface_vtable = {
619 __dbus_method_call_handler,
624 static int __register_dbus_interface(const char *port_name, bool is_trusted)
627 GDBusNodeInfo *introspection_data = NULL;
628 int registration_id = 0;
630 static gchar introspection_prefix[] =
632 " <interface name='";
634 static gchar introspection_postfix[] =
636 " <method name='send_message'>"
637 " <arg type='s' name='local_appid' direction='in'/>"
638 " <arg type='s' name='local_port' direction='in'/>"
639 " <arg type='b' name='local_trusted' direction='in'/>"
640 " <arg type='b' name='bi_dir' direction='in'/>"
641 " <arg type='s' name='remote_appid' direction='in'/>"
642 " <arg type='s' name='remote_port' direction='in'/>"
643 " <arg type='b' name='remote_trusted' direction='in'/>"
644 " <arg type='u' name='data_len' direction='in'/>"
645 " <arg type='s' name='data' direction='in'/>"
650 char *introspection_xml = NULL;
651 int introspection_xml_len = 0;
655 GError *error = NULL;
656 char *bus_name = NULL;
657 char *interface_name = NULL;
658 GVariant *result = NULL;
660 bus_name = get_encoded_name(app_id, port_name, is_trusted);
662 _LOGE("Fail to get bus name");
665 interface_name = bus_name;
667 introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
668 strlen(introspection_postfix) + 1;
670 introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
671 if (!introspection_xml) {
672 _LOGE("out of memory");
677 result = g_dbus_connection_call_sync(
683 g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
684 G_VARIANT_TYPE("(u)"),
685 G_DBUS_CALL_FLAGS_NONE,
690 _LOGE("RequestName fail : %s", error->message);
694 if (result == NULL) {
695 _LOGE("fail to get name NULL");
698 g_variant_get(result, "(u)", &owner_id);
700 _LOGE("Acquiring the own name is failed");
704 _LOGD("Acquiring the own name : %d", owner_id);
706 snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
708 introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
709 if (!introspection_data) {
710 _LOGE("g_dbus_node_info_new_for_xml() is failed.");
714 registration_id = g_dbus_connection_register_object(gdbus_conn,
715 MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
716 &interface_vtable, NULL, NULL, NULL);
718 _LOGD("registration_id %d", registration_id);
720 if (registration_id == 0) {
721 _LOGE("Failed to g_dbus_connection_register_object");
726 if (introspection_data)
727 g_dbus_node_info_unref(introspection_data);
728 if (introspection_xml)
729 free(introspection_xml);
733 g_variant_unref(result);
736 return registration_id;
739 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)
741 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
742 retvm_if(!mi, false, "Malloc failed");
744 mi->callback = callback;
745 mi->is_trusted = is_trusted;
746 mi->port_name = strdup(local_port);
747 if (mi->port_name == NULL) {
748 _LOGE("Malloc failed (%s)", local_port);
752 mi->local_id = local_id;
753 mi->user_data = user_data;
755 g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
759 int get_local_port_info(int id, message_port_local_port_info_s **info)
761 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
764 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
767 return MESSAGE_PORT_ERROR_NONE;
770 int register_message_port(const char *local_port, bool is_trusted, message_port_message_cb callback, void *user_data)
772 _SECURE_LOGI("local_port : [%s:%s]", local_port, is_trusted ? "trusted" : "non-trusted");
775 message_port_local_port_info_s *port_info;
778 return MESSAGE_PORT_ERROR_IO_ERROR;
781 /* Check the message port is already registed */
782 if (is_local_port_registed(local_port, is_trusted, &local_id, &port_info)) {
783 port_info->callback = callback;
784 port_info->user_data = user_data;
788 local_id = __register_dbus_interface(local_port, is_trusted);
790 _LOGE("register_dbus_interface fail !!");
791 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
794 if (!__message_port_register_port(local_id, local_port, is_trusted, callback, user_data))
795 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
800 int unregister_local_port(int local_port_id, bool trusted_port)
804 char *bus_name = NULL;
807 message_port_local_port_info_s *mi;
809 _LOGI("unregister : %d", local_port_id);
813 return MESSAGE_PORT_ERROR_IO_ERROR;
816 mi = (message_port_local_port_info_s *)
817 g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
819 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
821 if (mi->is_trusted != trusted_port)
822 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
824 g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
826 bus_name = get_encoded_name(app_id, mi->port_name, mi->is_trusted);
827 if (bus_name == NULL)
828 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
830 g_dbus_connection_unregister_object(gdbus_conn, local_port_id);
832 result = g_dbus_connection_call_sync(
838 g_variant_new("(s)", bus_name),
839 G_VARIANT_TYPE("(u)"),
840 G_DBUS_CALL_FLAGS_NONE,
849 _LOGE("RequestName fail : %s", err->message);
851 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
853 g_variant_get(result, "(u)", &ret);
856 g_variant_unref(result);
858 if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
860 if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
861 _LOGE("Port Not exist");
862 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
863 } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
864 _LOGE("Try to release not owned name. MESSAGE_PORT_ERROR_INVALID_PARAMETER");
865 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
869 g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
871 return MESSAGE_PORT_ERROR_NONE;