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);
98 static void __callback_info_free_by_info(message_port_callback_info_s *callback_info)
100 GList *callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
103 if (callback_info_list == NULL)
106 find_list = g_list_find(callback_info_list, callback_info);
107 if (find_list == NULL)
110 callback_info_list = g_list_remove_link(callback_info_list, find_list);
111 __callback_info_free(callback_info);
112 g_list_free(find_list);
115 static void __hash_destroy_callback_info(gpointer data)
118 GList *callback_list = (GList *)data;
119 if (callback_list != NULL)
120 g_list_free_full(callback_list, __callback_info_free);
123 /* LCOV_EXCL_START */
124 static void __hash_destory_local_value(gpointer data)
126 message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
129 free(mli->port_name);
135 static bool __initialize(void)
137 if (!initialized_common) {
138 if (!initialize_common())
139 return MESSAGE_PORT_ERROR_IO_ERROR;
142 if (__local_port_info == NULL) {
143 __local_port_info = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destory_local_value);
144 retvm_if(!__local_port_info, false, "fail to create __local_port_info");
147 if (__sender_appid_hash == NULL) {
148 __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
149 retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash");
152 if (__trusted_app_list_hash == NULL) {
153 __trusted_app_list_hash = g_hash_table_new(g_str_hash, g_str_equal);
154 retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
157 if (__callback_info_hash == NULL) {
158 __callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destroy_callback_info);
159 retvm_if(!__callback_info_hash, false, "fail to create __callback_info_hash");
167 bool is_local_port_registed(const char *local_port, bool trusted, int *local_id, message_port_local_port_info_s **lpi)
172 g_hash_table_iter_init(&iter, __local_port_info);
173 while (g_hash_table_iter_next(&iter, &key, &value)) {
174 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)value;
176 if ((mi->is_trusted == trusted) && strcmp(mi->port_name, local_port) == 0) {
177 *local_id = mi->local_id;
186 static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
188 GDBusMessage *msg = NULL;
189 GDBusMessage *reply = NULL;
194 msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
195 "org.freedesktop.DBus", "GetConnectionUnixProcessID");
197 _LOGE("Can't allocate new method call");
201 g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
202 reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
203 G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
207 _LOGE("Failed to get pid [%s]", err->message);
213 body = g_dbus_message_get_body(reply);
214 g_variant_get(body, "(u)", &pid);
220 g_object_unref(reply);
225 static message_port_pkt_s *__message_port_recv_raw(int fd)
227 message_port_pkt_s *pkt = NULL;
230 pkt = (message_port_pkt_s *)calloc(sizeof(message_port_pkt_s), 1);
236 if (read_string_from_socket(fd, (char **)&pkt->remote_port_name, &pkt->remote_port_name_len) != MESSAGE_PORT_ERROR_NONE) {
237 LOGE("read socket fail: port_name");
238 free(pkt->remote_port_name);
243 if (read_socket(fd, (char *)&pkt->is_bidirection, sizeof(pkt->is_bidirection), &nb) != MESSAGE_PORT_ERROR_NONE) {
244 LOGE("read socket fail: is_bidirection");
245 free(pkt->remote_port_name);
250 if (read_socket(fd, (char *)&pkt->is_trusted, sizeof(pkt->is_trusted), &nb) != MESSAGE_PORT_ERROR_NONE) {
251 LOGE("read socket fail: is_trusted");
252 free(pkt->remote_port_name);
257 if (read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGE_PORT_ERROR_NONE) {
258 LOGE("read socket fail: data");
261 free(pkt->remote_port_name);
269 static gboolean __socket_request_handler(GIOChannel *gio,
274 message_port_callback_info_s *mi;
275 message_port_pkt_s *pkt;
276 message_port_local_port_info_s *local_port_info;
278 GError *error = NULL;
280 mi = (message_port_callback_info_s *)data;
283 g_io_channel_shutdown(gio, TRUE, &error);
285 _LOGE("g_io_channel_shutdown error : %s", error->message);
288 g_io_channel_unref(gio);
292 local_port_info = mi->local_info;
293 if (local_port_info == NULL || local_port_info->callback == NULL) {
294 _LOGE("Failed to get callback info");
295 __callback_info_free_by_info(mi);
299 if (cond == G_IO_HUP) {
300 _LOGI("socket G_IO_HUP");
301 __callback_info_free_by_info(mi);
305 if ((fd = g_io_channel_unix_get_fd(gio)) < 0) {
306 _LOGE("fail to get fd from io channel");
307 __callback_info_free_by_info(mi);
311 if ((pkt = __message_port_recv_raw(fd)) == NULL) {
312 _LOGE("recv error on SOCKET");
313 __callback_info_free_by_info(mi);
317 kb = bundle_decode(pkt->data, pkt->data_len);
318 if (pkt->is_bidirection)
319 local_port_info->callback(mi->local_id, mi->remote_app_id,
320 pkt->remote_port_name, pkt->is_trusted, kb, local_port_info->user_data);
323 local_port_info->callback(mi->local_id, mi->remote_app_id,
324 NULL, pkt->is_trusted, kb, local_port_info->user_data);
328 if (pkt->remote_port_name)
329 free(pkt->remote_port_name);
338 static bool __receive_message(GVariant *parameters, GDBusMethodInvocation *invocation)
340 char *local_port = NULL;
341 char *local_appid = NULL;
342 char *remote_appid = NULL;
343 char *remote_port = NULL;
344 gboolean local_trusted = false;
345 gboolean remote_trusted = false;
346 gboolean bi_dir = false;
350 bundle_raw *raw = NULL;
351 message_port_local_port_info_s *mi;
352 int local_reg_id = 0;
353 message_port_callback_info_s *callback_info = NULL;
354 message_port_callback_info_s *head_callback_info;
355 GList *callback_info_list = NULL;
359 GUnixFDList *fd_list;
361 int *returned_fds = NULL;
365 g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
366 &remote_appid, &remote_port, &remote_trusted, &len, &raw);
369 _LOGE("Invalid argument : remote_port is NULL");
373 _LOGE("Invalid argument : remote_appid is NULL");
376 if (!is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
377 _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
381 _LOGE("Invalid argument : local_appid");
385 _LOGE("Invalid argument : local_port");
388 if (strcmp(remote_appid, app_id) != 0) {
389 _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
392 if (strcmp(remote_port, mi->port_name) != 0) {
393 _LOGE("Invalid argument : remote_port (%s)", remote_port);
397 _LOGE("Invalid argument : data_len");
400 if (remote_trusted) {
401 if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
402 if (!is_preloaded(local_appid, remote_appid)) {
403 int ret = check_certificate(local_appid, remote_appid);
404 if (ret == MESSAGE_PORT_ERROR_NONE)
405 g_hash_table_insert(__trusted_app_list_hash, local_appid, "TRUE");
407 _LOGE("The application (%s) is not signed with the same certificate",
415 callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
416 if (callback_info == NULL) {
417 _LOGE("out of memory");
421 callback_info->local_id = mi->local_id;
422 callback_info->local_info = mi;
423 callback_info->remote_app_id = strdup(local_appid);
424 if (callback_info->remote_app_id == NULL) {
425 _LOGE("out of memory");
429 msg = g_dbus_method_invocation_get_message(invocation);
430 fd_list = g_dbus_message_get_unix_fd_list(msg);
432 /* When application send message to self fd_list is NULL */
433 if (fd_list != NULL) {
434 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
435 if (returned_fds == NULL) {
436 _LOGE("fail to get fds");
439 fd = returned_fds[0];
441 LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
444 callback_info->gio_read = g_io_channel_unix_new(fd);
445 if (!callback_info->gio_read) {
446 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
450 callback_info->g_src_id = g_io_add_watch(callback_info->gio_read, G_IO_IN | G_IO_HUP,
451 __socket_request_handler, (gpointer)callback_info);
452 if (callback_info->g_src_id == 0) {
453 _LOGE("fail to add watch on socket");
457 callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(mi->local_id));
458 if (callback_info_list == NULL) {
459 head_callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
460 if (head_callback_info == NULL) {
461 _LOGE("fail to alloc head_callback_info");
464 head_callback_info->local_id = 0;
465 head_callback_info->remote_app_id = NULL;
466 head_callback_info->local_info = NULL;
467 head_callback_info->gio_read = NULL;
468 head_callback_info->g_src_id = 0;
469 callback_info_list = g_list_append(callback_info_list, head_callback_info);
470 callback_info_list = g_list_append(callback_info_list, callback_info);
471 g_hash_table_insert(__callback_info_hash, GUINT_TO_POINTER(mi->local_id), callback_info_list);
473 callback_info_list = g_list_append(callback_info_list, callback_info);
478 data = bundle_decode(raw, len);
480 _LOGE("Invalid argument : message");
484 LOGD("call calback %s", local_appid);
486 mi->callback(mi->local_id, local_appid, local_port, local_trusted, data, mi->user_data);
488 mi->callback(mi->local_id, local_appid, NULL, false, data, mi->user_data);
494 __callback_info_free(callback_info);
502 static void __on_sender_name_appeared(GDBusConnection *connection,
504 const gchar *name_owner,
507 _LOGI("sender name appeared : %s", name);
510 static void __on_sender_name_vanished(GDBusConnection *connection,
514 gboolean remove_result = FALSE;
515 int *watcher_id = (int *)user_data;
516 remove_result = g_hash_table_remove(__sender_appid_hash, (gpointer)name);
518 _LOGE("Fail to remove sender appid from hash : %s", name);
522 g_bus_unwatch_name(*watcher_id);
524 LOGE("Invalid watcher_id %d", *watcher_id);
527 LOGE("watcher_id is NULL");
531 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
534 char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
535 char *local_appid = NULL;
536 int pid = __get_sender_pid(conn, sender);
537 int *watcher_id = (int *)calloc(1, sizeof(int));
539 retvm_if(!watcher_id, false, "Malloc failed");
541 ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
542 if (ret != AUL_R_OK) {
543 _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
548 g_variant_get_child(parameters, 0, "&s", &local_appid);
549 if (local_appid == NULL) {
550 _LOGE("appid is NULL : (%s) (%d)", sender, pid);
555 if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
556 _LOGD("insert sender !!!!! %s", sender);
557 _sender = strdup(sender);
558 if (_sender == NULL) {
559 _LOGE("out of memory");
563 g_hash_table_insert(__sender_appid_hash, (gpointer)_sender, GINT_TO_POINTER(pid));
564 *watcher_id = g_bus_watch_name_on_connection(
567 G_BUS_NAME_WATCHER_FLAGS_NONE,
568 __on_sender_name_appeared,
569 __on_sender_name_vanished,
579 static void __dbus_method_call_handler(GDBusConnection *conn,
580 const gchar *sender, const gchar *object_path,
581 const gchar *iface_name, const gchar *method_name,
582 GVariant *parameters, GDBusMethodInvocation *invocation,
585 _LOGI("method_name: %s, sender: %s", method_name, sender);
586 gpointer sender_pid = g_hash_table_lookup(__sender_appid_hash, sender);
587 if (sender_pid == NULL) {
588 if (!__check_sender_validation(parameters, sender, conn))
591 if (g_strcmp0(method_name, "send_message") == 0)
592 __receive_message(parameters, invocation);
594 g_dbus_method_invocation_return_value(invocation, NULL);
597 static const GDBusInterfaceVTable interface_vtable = {
598 __dbus_method_call_handler,
603 static int __register_dbus_interface(const char *port_name, bool is_trusted)
606 GDBusNodeInfo *introspection_data = NULL;
607 int registration_id = 0;
609 static gchar introspection_prefix[] =
611 " <interface name='";
613 static gchar introspection_postfix[] =
615 " <method name='send_message'>"
616 " <arg type='s' name='local_appid' direction='in'/>"
617 " <arg type='s' name='local_port' direction='in'/>"
618 " <arg type='b' name='local_trusted' direction='in'/>"
619 " <arg type='b' name='bi_dir' direction='in'/>"
620 " <arg type='s' name='remote_appid' direction='in'/>"
621 " <arg type='s' name='remote_port' direction='in'/>"
622 " <arg type='b' name='remote_trusted' direction='in'/>"
623 " <arg type='u' name='data_len' direction='in'/>"
624 " <arg type='s' name='data' direction='in'/>"
629 char *introspection_xml = NULL;
630 int introspection_xml_len = 0;
634 GError *error = NULL;
635 char *bus_name = NULL;
636 char *interface_name = NULL;
637 GVariant *result = NULL;
639 bus_name = get_encoded_name(app_id, port_name, is_trusted);
641 _LOGE("Fail to get bus name");
644 interface_name = bus_name;
646 introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
647 strlen(introspection_postfix) + 1;
649 introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
650 if (!introspection_xml) {
651 _LOGE("out of memory");
656 result = g_dbus_connection_call_sync(
662 g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
663 G_VARIANT_TYPE("(u)"),
664 G_DBUS_CALL_FLAGS_NONE,
669 _LOGE("RequestName fail : %s", error->message);
673 if (result == NULL) {
674 _LOGE("fail to get name NULL");
677 g_variant_get(result, "(u)", &owner_id);
679 _LOGE("Acquiring the own name is failed");
683 _LOGD("Acquiring the own name : %d", owner_id);
685 snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
687 introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
688 if (!introspection_data) {
689 _LOGE("g_dbus_node_info_new_for_xml() is failed.");
693 registration_id = g_dbus_connection_register_object(gdbus_conn,
694 MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
695 &interface_vtable, NULL, NULL, NULL);
697 _LOGD("registration_id %d", registration_id);
699 if (registration_id == 0) {
700 _LOGE("Failed to g_dbus_connection_register_object");
705 if (introspection_data)
706 g_dbus_node_info_unref(introspection_data);
707 if (introspection_xml)
708 free(introspection_xml);
712 g_variant_unref(result);
715 return registration_id;
718 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)
720 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
721 retvm_if(!mi, false, "Malloc failed");
723 mi->callback = callback;
724 mi->is_trusted = is_trusted;
725 mi->port_name = strdup(local_port);
726 if (mi->port_name == NULL) {
727 _LOGE("Malloc failed (%s)", local_port);
731 mi->local_id = local_id;
732 mi->user_data = user_data;
734 g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
738 int get_local_port_info(int id, message_port_local_port_info_s **info)
740 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
743 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
746 return MESSAGE_PORT_ERROR_NONE;
749 int register_message_port(const char *local_port, bool is_trusted, message_port_message_cb callback, void *user_data)
751 _SECURE_LOGI("local_port : [%s:%s]", local_port, is_trusted ? "trusted" : "non-trusted");
754 message_port_local_port_info_s *port_info;
757 return MESSAGE_PORT_ERROR_IO_ERROR;
760 /* Check the message port is already registed */
761 if (is_local_port_registed(local_port, is_trusted, &local_id, &port_info)) {
762 port_info->callback = callback;
763 port_info->user_data = user_data;
767 local_id = __register_dbus_interface(local_port, is_trusted);
769 _LOGE("register_dbus_interface fail !!");
770 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
773 if (!__message_port_register_port(local_id, local_port, is_trusted, callback, user_data))
774 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
779 int unregister_local_port(int local_port_id, bool trusted_port)
783 char *bus_name = NULL;
786 message_port_local_port_info_s *mi;
788 _LOGI("unregister : %d", local_port_id);
792 return MESSAGE_PORT_ERROR_IO_ERROR;
795 mi = (message_port_local_port_info_s *)
796 g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
798 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
800 if (mi->is_trusted != trusted_port)
801 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
803 g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
805 bus_name = get_encoded_name(app_id, mi->port_name, mi->is_trusted);
806 if (bus_name == NULL)
807 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
809 g_dbus_connection_unregister_object(gdbus_conn, local_port_id);
811 result = g_dbus_connection_call_sync(
817 g_variant_new("(s)", bus_name),
818 G_VARIANT_TYPE("(u)"),
819 G_DBUS_CALL_FLAGS_NONE,
828 _LOGE("RequestName fail : %s", err->message);
830 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
832 g_variant_get(result, "(u)", &ret);
835 g_variant_unref(result);
837 if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
839 if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
840 _LOGE("Port Not exist");
841 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
842 } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
843 _LOGE("Try to release not owned name. MESSAGE_PORT_ERROR_INVALID_PARAMETER");
844 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
848 g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
850 return MESSAGE_PORT_ERROR_NONE;