#endif
#include <string.h>
+#include <errno.h>
#include <gdbus.h>
#include "connman.h"
{
unsigned int i;
- if (ident == NULL)
+ if (!ident)
return FALSE;
for (i = 0; i < strlen(ident); i++) {
GString *str;
unsigned int i, size;
- if (value == NULL)
+ if (!value)
return NULL;
size = strlen(value);
str = g_string_new(NULL);
- if (str == NULL)
+ if (!str)
return NULL;
for (i = 0; i < size; i++) {
switch (type) {
case DBUS_TYPE_STRING:
- variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING;
+ variant_sig = DBUS_TYPE_ARRAY_AS_STRING
+ DBUS_TYPE_STRING_AS_STRING;
array_sig = DBUS_TYPE_STRING_AS_STRING;
break;
case DBUS_TYPE_OBJECT_PATH:
- variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING;
+ variant_sig = DBUS_TYPE_ARRAY_AS_STRING
+ DBUS_TYPE_OBJECT_PATH_AS_STRING;
array_sig = DBUS_TYPE_OBJECT_PATH_AS_STRING;
break;
+ case DBUS_TYPE_DICT_ENTRY:
+ variant_sig = DBUS_TYPE_ARRAY_AS_STRING
+ DBUS_STRUCT_BEGIN_CHAR_AS_STRING
+ DBUS_TYPE_ARRAY_AS_STRING
+ DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
+ DBUS_TYPE_STRING_AS_STRING
+ DBUS_TYPE_VARIANT_AS_STRING
+ DBUS_DICT_ENTRY_END_CHAR_AS_STRING
+ DBUS_STRUCT_END_CHAR_AS_STRING;
+ array_sig = DBUS_STRUCT_BEGIN_CHAR_AS_STRING
+ DBUS_TYPE_ARRAY_AS_STRING
+ DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
+ DBUS_TYPE_STRING_AS_STRING
+ DBUS_TYPE_VARIANT_AS_STRING
+ DBUS_DICT_ENTRY_END_CHAR_AS_STRING
+ DBUS_STRUCT_END_CHAR_AS_STRING;
+ break;
default:
return;
}
DBusMessage *signal;
DBusMessageIter iter;
- if (path == NULL)
+ if (!path)
return FALSE;
signal = dbus_message_new_signal(path, interface, "PropertyChanged");
- if (signal == NULL)
+ if (!signal)
return FALSE;
dbus_message_iter_init_append(signal, &iter);
DBusMessage *signal;
DBusMessageIter iter;
- if (path == NULL)
+ if (!path)
return FALSE;
signal = dbus_message_new_signal(path, interface, "PropertyChanged");
- if (signal == NULL)
+ if (!signal)
return FALSE;
dbus_message_iter_init_append(signal, &iter);
DBusMessage *signal;
DBusMessageIter iter;
- if (path == NULL)
+ if (!path)
return FALSE;
signal = dbus_message_new_signal(path, interface, "PropertyChanged");
- if (signal == NULL)
+ if (!signal)
return FALSE;
dbus_message_iter_init_append(signal, &iter);
DBusMessage *msg;
DBusMessageIter array, dict;
- if (owner == NULL || path == NULL)
+ if (!owner || !path)
return FALSE;
msg = dbus_message_new_method_call(owner, path,
CONNMAN_NOTIFICATION_INTERFACE,
"Update");
- if (msg == NULL)
+ if (!msg)
return FALSE;
dbus_message_iter_init_append(msg, &array);
DBusMessage *msg;
DBusMessageIter array, dict;
- if (owner == NULL || path == NULL)
+ if (!owner || !path)
return FALSE;
msg = dbus_message_new_method_call(owner, path,
CONNMAN_NOTIFICATION_INTERFACE,
"Update");
- if (msg == NULL)
+ if (!msg)
return FALSE;
dbus_message_iter_init_append(msg, &array);
DBusMessage *msg;
DBusMessageIter array, dict;
- if (owner == NULL || path == NULL)
+ if (!owner || !path)
return FALSE;
msg = dbus_message_new_method_call(owner, path,
CONNMAN_NOTIFICATION_INTERFACE,
"Update");
- if (msg == NULL)
+ if (!msg)
return FALSE;
dbus_message_iter_init_append(msg, &array);
{
DBusMessageIter iter, array;
- if (msg == NULL || function == NULL)
+ if (!msg || !function)
return FALSE;
dbus_message_iter_init_append(msg, &iter);
return TRUE;
}
+dbus_bool_t __connman_dbus_append_objpath_array(DBusMessage *msg,
+ connman_dbus_append_cb_t function, void *user_data)
+{
+ DBusMessageIter iter, array;
+
+ if (!msg || !function)
+ return FALSE;
+
+ dbus_message_iter_init_append(msg, &iter);
+ dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
+ DBUS_TYPE_OBJECT_PATH_AS_STRING, &array);
+
+ function(&array, user_data);
+
+ dbus_message_iter_close_container(&iter, &array);
+
+ return TRUE;
+}
+
+struct callback_data {
+ void *cb;
+ void *user_data;
+};
+
+static void get_connection_unix_user_reply(DBusPendingCall *call,
+ void *user_data)
+{
+ struct callback_data *data = user_data;
+ connman_dbus_get_connection_unix_user_cb_t cb = data->cb;
+ DBusMessageIter iter;
+ DBusMessage *reply;
+ int err = 0;
+ unsigned int uid = 0;
+
+ reply = dbus_pending_call_steal_reply(call);
+
+ if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
+ DBG("Failed to retrieve UID");
+ err = -EIO;
+ goto done;
+ }
+
+ if (!dbus_message_has_signature(reply, "u")) {
+ DBG("Message signature is wrong");
+ err = -EINVAL;
+ goto done;
+ }
+
+ dbus_message_iter_init(reply, &iter);
+ dbus_message_iter_get_basic(&iter, &uid);
+
+done:
+ (*cb)(uid, data->user_data, err);
+
+ dbus_message_unref(reply);
+
+ dbus_pending_call_unref(call);
+}
+
+int connman_dbus_get_connection_unix_user(DBusConnection *connection,
+ const char *bus_name,
+ connman_dbus_get_connection_unix_user_cb_t func,
+ void *user_data)
+{
+ struct callback_data *data;
+ DBusPendingCall *call;
+ DBusMessage *msg = NULL;
+ int err;
+
+ data = g_try_new0(struct callback_data, 1);
+ if (!data) {
+ DBG("Can't allocate data structure");
+ return -ENOMEM;
+ }
+
+ msg = dbus_message_new_method_call(DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
+ DBUS_INTERFACE_DBUS,
+ "GetConnectionUnixUser");
+ if (!msg) {
+ DBG("Can't allocate new message");
+ err = -ENOMEM;
+ goto err;
+ }
+
+ dbus_message_append_args(msg, DBUS_TYPE_STRING, &bus_name,
+ DBUS_TYPE_INVALID);
+
+ if (!dbus_connection_send_with_reply(connection, msg, &call, -1)) {
+ DBG("Failed to execute method call");
+ err = -EINVAL;
+ goto err;
+ }
+
+ if (!call) {
+ DBG("D-Bus connection not available");
+ err = -EINVAL;
+ goto err;
+ }
+
+ data->cb = func;
+ data->user_data = user_data;
+
+ dbus_pending_call_set_notify(call, get_connection_unix_user_reply,
+ data, g_free);
+
+ dbus_message_unref(msg);
+
+ return 0;
+
+err:
+ dbus_message_unref(msg);
+ g_free(data);
+
+ return err;
+}
+
+static unsigned char *parse_context(DBusMessage *msg)
+{
+ DBusMessageIter iter, array;
+ unsigned char *ctx, *p;
+ int size = 0;
+
+ dbus_message_iter_init(msg, &iter);
+ dbus_message_iter_recurse(&iter, &array);
+ while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_BYTE) {
+ size++;
+
+ dbus_message_iter_next(&array);
+ }
+
+ if (size == 0)
+ return NULL;
+
+ ctx = g_try_malloc0(size + 1);
+ if (!ctx)
+ return NULL;
+
+ p = ctx;
+
+ dbus_message_iter_init(msg, &iter);
+ dbus_message_iter_recurse(&iter, &array);
+ while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_BYTE) {
+ dbus_message_iter_get_basic(&array, p);
+
+ p++;
+ dbus_message_iter_next(&array);
+ }
+
+ return ctx;
+}
+
+static void selinux_get_context_reply(DBusPendingCall *call, void *user_data)
+{
+ struct callback_data *data = user_data;
+ connman_dbus_get_context_cb_t cb = data->cb;
+ DBusMessage *reply;
+ unsigned char *context = NULL;
+ int err = 0;
+
+ reply = dbus_pending_call_steal_reply(call);
+
+ if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
+ DBG("Failed to retrieve SELinux context");
+ err = -EIO;
+ goto done;
+ }
+
+ if (!dbus_message_has_signature(reply, "ay")) {
+ DBG("Message signature is wrong");
+ err = -EINVAL;
+ goto done;
+ }
+
+ context = parse_context(reply);
+
+done:
+ (*cb)(context, data->user_data, err);
+
+ g_free(context);
+
+ dbus_message_unref(reply);
+
+ dbus_pending_call_unref(call);
+}
+
+int connman_dbus_get_selinux_context(DBusConnection *connection,
+ const char *service,
+ connman_dbus_get_context_cb_t func,
+ void *user_data)
+{
+ struct callback_data *data;
+ DBusPendingCall *call;
+ DBusMessage *msg = NULL;
+ int err;
+
+ if (!func)
+ return -EINVAL;
+
+ data = g_try_new0(struct callback_data, 1);
+ if (!data) {
+ DBG("Can't allocate data structure");
+ return -ENOMEM;
+ }
+
+ msg = dbus_message_new_method_call(DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
+ DBUS_INTERFACE_DBUS,
+ "GetConnectionSELinuxSecurityContext");
+ if (!msg) {
+ DBG("Can't allocate new message");
+ err = -ENOMEM;
+ goto err;
+ }
+
+ dbus_message_append_args(msg, DBUS_TYPE_STRING, &service,
+ DBUS_TYPE_INVALID);
+
+ if (!dbus_connection_send_with_reply(connection, msg, &call, -1)) {
+ DBG("Failed to execute method call");
+ err = -EINVAL;
+ goto err;
+ }
+
+ if (!call) {
+ DBG("D-Bus connection not available");
+ err = -EINVAL;
+ goto err;
+ }
+
+ data->cb = func;
+ data->user_data = user_data;
+
+ dbus_pending_call_set_notify(call, selinux_get_context_reply,
+ data, g_free);
+
+ dbus_message_unref(msg);
+
+ return 0;
+
+err:
+ dbus_message_unref(msg);
+ g_free(data);
+
+ return err;
+}
+
+void connman_dbus_reply_pending(DBusMessage *pending,
+ int error, const char *path)
+{
+ if (pending) {
+ if (error > 0) {
+ DBusMessage *reply;
+
+ reply = __connman_error_failed(pending, error);
+ if (reply)
+ g_dbus_send_message(connection, reply);
+ } else {
+ const char *sender;
+
+ sender = dbus_message_get_interface(pending);
+ if (!path)
+ path = dbus_message_get_path(pending);
+
+ DBG("sender %s path %s", sender, path);
+
+ if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
+ g_dbus_send_reply(connection, pending,
+ DBUS_TYPE_OBJECT_PATH, &path,
+ DBUS_TYPE_INVALID);
+ else
+ g_dbus_send_reply(connection, pending,
+ DBUS_TYPE_INVALID);
+ }
+
+ dbus_message_unref(pending);
+ }
+}
+
DBusConnection *connman_dbus_get_connection(void)
{
- if (connection == NULL)
+ if (!connection)
return NULL;
return dbus_connection_ref(connection);