summaryrefslogtreecommitdiffstats
path: root/src/sbus/sssd_dbus_request.c
diff options
context:
space:
mode:
authorStef Walter <stefw@redhat.com>2014-02-21 16:59:48 +0100
committerJakub Hrozek <jhrozek@redhat.com>2014-06-02 19:03:26 +0200
commit462592812dc67a49fa41a46626ebff3d48e20f19 (patch)
tree9f3d4e6b1649ce22c2dfee2279fa595e483cf15e /src/sbus/sssd_dbus_request.c
parentc672fc52ff2bade73ad735162af9b4eb6fcccdfb (diff)
downloadsssd-462592812dc67a49fa41a46626ebff3d48e20f19.tar.gz
sssd-462592812dc67a49fa41a46626ebff3d48e20f19.tar.xz
sssd-462592812dc67a49fa41a46626ebff3d48e20f19.zip
sbus: Add the sbus_request_parse_or_finish() method
Some DBus types returned from dbus_message_get_args() require memory to be released when done. We automatically attach these to the talloc struct sbus_request memory context in this function. This accepts varargs similar to dbus_message_get_args(), which are rather awkward. However instead of reworking them completely, future generated marshalling code will replace most uses of these varargs. If parsing the dbus message fails, then it responds to the DBus caller with an appropriate error such as o.f.D.Error.InvalidArgs. In these cases (ie: when it returns FALSE) the sbus_request is finished. Migrated some, but not all, uses of dbus_message_get_args() to the new function. Some instances have uncommon semantics such as terminating the connection upon failure to parse a message. Reviewed-by: Jakub Hrozek <jhrozek@redhat.com> Reviewed-by: Pavel Březina <pbrezina@redhat.com> Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com> (cherry picked from commit 06b7bc8ca2e005ed510210d3b8dee16afbabbcc9)
Diffstat (limited to 'src/sbus/sssd_dbus_request.c')
-rw-r--r--src/sbus/sssd_dbus_request.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/sbus/sssd_dbus_request.c b/src/sbus/sssd_dbus_request.c
index 973089c4b..c45f3a9fd 100644
--- a/src/sbus/sssd_dbus_request.c
+++ b/src/sbus/sssd_dbus_request.c
@@ -110,3 +110,109 @@ int sbus_request_fail_and_finish(struct sbus_request *dbus_req,
dbus_message_unref(reply);
return ret;
}
+
+struct array_arg {
+ char **dbus_array;
+};
+
+static int array_arg_destructor(struct array_arg *arg)
+{
+ dbus_free_string_array(arg->dbus_array);
+ return 0;
+}
+
+static bool
+parent_dbus_string_arrays(struct sbus_request *request, int first_arg_type,
+ va_list va)
+{
+ struct array_arg *array_arg;
+ int arg_type;
+ void **arg_ptr;
+
+ /*
+ * Here we iterate through the entire thing again and look for
+ * things we need to fix allocation for. Normally certain types
+ * returned from dbus_message_get_args() and friends require
+ * later freeing. We tie those to the talloc context here.
+ *
+ * The list of argument has already been validated by the previous
+ * dbus_message_get_args() call, so we can be cheap.
+ */
+
+ arg_type = first_arg_type;
+ while (arg_type != DBUS_TYPE_INVALID) {
+
+ if (arg_type == DBUS_TYPE_ARRAY) {
+ arg_type = va_arg(va, int); /* the array element type */
+ arg_ptr = va_arg(va, void **); /* the array elements */
+ va_arg(va, int *); /* the array length */
+
+ /* Arrays of these things need to be freed */
+ if (arg_type == DBUS_TYPE_STRING ||
+ arg_type == DBUS_TYPE_OBJECT_PATH ||
+ arg_type == DBUS_TYPE_SIGNATURE) {
+
+ array_arg = talloc_zero(request, struct array_arg);
+ if(array_arg == NULL) {
+ /* no kidding ... */
+ DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory while trying not to leak memory\n");
+ return false;
+ }
+
+ array_arg->dbus_array = *arg_ptr;
+ talloc_set_destructor(array_arg, array_arg_destructor);
+ }
+
+ /* A non array argument */
+ } else {
+ arg_ptr = va_arg(va, void**);
+ }
+
+ /* The next type */
+ arg_type = va_arg(va, int);
+ }
+
+ return true;
+}
+
+bool
+sbus_request_parse_or_finish(struct sbus_request *request,
+ int first_arg_type,
+ ...)
+{
+ DBusError error = DBUS_ERROR_INIT;
+ bool ret = true;
+ va_list va2;
+ va_list va;
+
+ va_start(va, first_arg_type);
+ va_copy(va2, va);
+
+ if (dbus_message_get_args_valist(request->message, &error,
+ first_arg_type, va)) {
+ ret = parent_dbus_string_arrays (request, first_arg_type, va2);
+
+ } else {
+ /* Trying to send the error back to the caller in this case is a joke */
+ if (!dbus_error_is_set(&error) || dbus_error_has_name(&error, DBUS_ERROR_NO_MEMORY)) {
+ DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory parsing DBus message\n");
+ sbus_request_finish(request, NULL);
+
+ /* Log other errors and send them back, this include o.f.d.InvalidArgs */
+ } else {
+ DEBUG(SSSDBG_OP_FAILURE, "Couldn't parse DBus message %s.%s: %s\n",
+ dbus_message_get_interface(request->message),
+ dbus_message_get_member(request->message),
+ error.message);
+ sbus_request_fail_and_finish(request, &error);
+ }
+
+ dbus_error_free(&error);
+ ret = false;
+ }
+
+ va_end(va2);
+ va_end(va);
+
+ return ret;
+}