]> err.no Git - systemd/commitdiff
dbus: properly generate UnknownInterface, UnknownProperty and PropertyReadOnly errors
authorLennart Poettering <lennart@poettering.net>
Wed, 9 Mar 2011 19:01:53 +0000 (20:01 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 9 Mar 2011 19:01:53 +0000 (20:01 +0100)
17 files changed:
src/dbus-automount.c
src/dbus-device.c
src/dbus-job.c
src/dbus-manager.c
src/dbus-mount.c
src/dbus-path.c
src/dbus-service.c
src/dbus-snapshot.c
src/dbus-socket.c
src/dbus-swap.c
src/dbus-target.c
src/dbus-timer.c
src/dbus-unit.h
src/dbus.c
src/dbus.h
src/util.c
src/util.h

index af277af39c77f317cbfe63b92770ecc25892c697..eccad37e8b062f2876386f96066d5b05ac73e52e 100644 (file)
         BUS_INTROSPECTABLE_INTERFACE                                 \
         "</node>\n"
 
+#define INTERFACES_LIST                              \
+        BUS_UNIT_INTERFACES_LIST                     \
+        "org.freedesktop.systemd1.Automount\0"
+
 const char bus_automount_interface[] _introspect_("Automount") = BUS_AUTOMOUNT_INTERFACE;
 
 DBusHandlerResult bus_automount_message_handler(Unit *u, DBusConnection *c, DBusMessage *message) {
@@ -48,5 +52,5 @@ DBusHandlerResult bus_automount_message_handler(Unit *u, DBusConnection *c, DBus
                 { NULL, NULL, NULL, NULL, NULL }
         };
 
-        return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, properties);
+        return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties);
 }
index 9b2861de4548cb40678cbb372d0e541cd024eca0..b046eae9f74b85f35210a3e0b9600822f26b57e2 100644 (file)
         BUS_INTROSPECTABLE_INTERFACE                                    \
         "</node>\n"
 
+#define INTERFACES_LIST                              \
+        BUS_UNIT_INTERFACES_LIST                     \
+        "org.freedesktop.systemd1.Device\0"
+
 const char bus_device_interface[] _introspect_("Device") = BUS_DEVICE_INTERFACE;
 
 const char bus_device_invalidating_properties[] =
@@ -49,5 +53,5 @@ DBusHandlerResult bus_device_message_handler(Unit *u, DBusConnection *c, DBusMes
                 { NULL, NULL, NULL, NULL, NULL }
         };
 
-        return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, properties);
+        return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties);
 }
index 95367c4226afb1a610fcb716eb9c4edaed9ae31d..b66d23604ba1a1a76de7853958aecd59c53fe741 100644 (file)
 
 const char bus_job_interface[] _introspect_("Job") = BUS_JOB_INTERFACE;
 
+#define INTERFACES_LIST                              \
+        BUS_GENERIC_INTERFACES_LIST                  \
+        "org.freedesktop.systemd1.Job\0"
+
 #define INVALIDATING_PROPERTIES                 \
         "State\0"
 
@@ -99,7 +103,7 @@ static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusConnection *connec
                 job_finish_and_invalidate(j, JOB_CANCELED);
 
         } else
-                return bus_default_message_handler(j->manager, connection, message, INTROSPECTION, properties);
+                return bus_default_message_handler(j->manager, connection, message, INTROSPECTION, INTERFACES_LIST, properties);
 
         if (reply) {
                 if (!dbus_connection_send(connection, reply, NULL))
index 7d4703b88b04e4e0c2baa1036442885bad08745e..2edbc37b5e2bfedb8ce8b8b3a49ce6a971893a06 100644 (file)
 #define INTROSPECTION_END                                               \
         "</node>\n"
 
+#define INTERFACES_LIST                              \
+        BUS_GENERIC_INTERFACES_LIST                  \
+        "org.freedesktop.systemd1.Manager\0"
+
 const char bus_manager_interface[] _introspect_("Manager") = BUS_MANAGER_INTERFACE;
 
 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
@@ -1021,7 +1025,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection,
                 m->environment = e;
 
         } else
-                return bus_default_message_handler(m, connection, message, NULL, properties);
+                return bus_default_message_handler(m, connection, message, NULL, INTERFACES_LIST, properties);
 
         if (job_type != _JOB_TYPE_INVALID) {
                 const char *name, *smode, *old_name = NULL;
index e3a793d194a54cf83792586d5d6011743266803a..5fb9a3f0713092c6ac65eddcb32ffd255f057ada 100644 (file)
         BUS_INTROSPECTABLE_INTERFACE                                    \
         "</node>\n"
 
+#define INTERFACES_LIST                              \
+        BUS_UNIT_INTERFACES_LIST                     \
+        "org.freedesktop.systemd1.Mount\0"
+
 const char bus_mount_interface[] _introspect_("Mount") = BUS_MOUNT_INTERFACE;
 
 const char bus_mount_invalidating_properties[] =
@@ -150,5 +154,5 @@ DBusHandlerResult bus_mount_message_handler(Unit *u, DBusConnection *c, DBusMess
                 { NULL, NULL, NULL, NULL, NULL }
         };
 
-        return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, properties);
+        return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties);
 }
index 6155792d0dd8f13ee45c69920157761430142051..cb1d4f09cb0694f4ef47bfa794bb1c9db66e0fe3 100644 (file)
         BUS_INTROSPECTABLE_INTERFACE                                    \
         "</node>\n"
 
+#define INTERFACES_LIST                              \
+        BUS_UNIT_INTERFACES_LIST                     \
+        "org.freedesktop.systemd1.Path\0"
+
 const char bus_path_interface[] _introspect_("Path") = BUS_PATH_INTERFACE;
 
 static int bus_path_append_paths(Manager *m, DBusMessageIter *i, const char *property, void *data) {
@@ -94,5 +98,5 @@ DBusHandlerResult bus_path_message_handler(Unit *u, DBusConnection *c, DBusMessa
                 { NULL, NULL, NULL, NULL, NULL }
         };
 
-        return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, properties);
+        return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties);
 }
index 1b6c7f47b4daae1f24b0cd5ac52cb14ae5b92fac..6bb6a9d6e58221af2398c793bcbbbb4915e02aad 100644 (file)
         BUS_INTROSPECTABLE_INTERFACE                                    \
         "</node>\n"
 
+#define INTERFACES_LIST                              \
+        BUS_UNIT_INTERFACES_LIST                     \
+        "org.freedesktop.systemd1.Service\0"
+
 const char bus_service_interface[] _introspect_("Service") = BUS_SERVICE_INTERFACE;
 
 const char bus_service_invalidating_properties[] =
@@ -144,5 +148,5 @@ DBusHandlerResult bus_service_message_handler(Unit *u, DBusConnection *connectio
                 { NULL, NULL, NULL, NULL, NULL }
         };
 
-        return bus_default_message_handler(u->meta.manager, connection, message, INTROSPECTION, properties);
+        return bus_default_message_handler(u->meta.manager, connection, message, INTROSPECTION, INTERFACES_LIST, properties);
 }
index a9903ec484ef19297f07d73e50bb1144f4b311cd..cc12b1bd8bd06e07a3089d39e5ce83fd90d523b7 100644 (file)
         BUS_INTROSPECTABLE_INTERFACE                                    \
         "</node>\n"
 
+#define INTERFACES_LIST                              \
+        BUS_UNIT_INTERFACES_LIST                     \
+        "org.freedesktop.systemd1.Snapshot\0"
+
 const char bus_snapshot_interface[] _introspect_("Snapshot") = BUS_SNAPSHOT_INTERFACE;
 
 DBusHandlerResult bus_snapshot_message_handler(Unit *u, DBusConnection *c, DBusMessage *message) {
@@ -60,7 +64,7 @@ DBusHandlerResult bus_snapshot_message_handler(Unit *u, DBusConnection *c, DBusM
                         goto oom;
 
         } else
-                return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, properties);
+                return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties);
 
         if (reply) {
                 if (!dbus_connection_send(c, reply, NULL))
index 3fda76db865b753cba33443eb0d84f7f236cfb13..5b068b45fa8336979a21ac5b2c2aaf359a5bb91e 100644 (file)
         BUS_INTROSPECTABLE_INTERFACE                                    \
         "</node>\n"
 
+#define INTERFACES_LIST                              \
+        BUS_UNIT_INTERFACES_LIST                     \
+        "org.freedesktop.systemd1.Socket\0"
+
 const char bus_socket_interface[] _introspect_("Socket") = BUS_SOCKET_INTERFACE;
 
 const char bus_socket_invalidating_properties[] =
@@ -109,5 +113,5 @@ DBusHandlerResult bus_socket_message_handler(Unit *u, DBusConnection *c, DBusMes
                 { NULL, NULL, NULL, NULL, NULL }
         };
 
-        return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, properties);
+        return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties);
 }
index 723cd64a8217f8cc64c0136c686d1eb9edc13b27..079912a5326073fe8238091c82899d8075aa05ab 100644 (file)
         BUS_INTROSPECTABLE_INTERFACE                                    \
         "</node>\n"
 
+#define INTERFACES_LIST                              \
+        BUS_UNIT_INTERFACES_LIST                     \
+        "org.freedesktop.systemd1.Swap\0"
+
 const char bus_swap_interface[] _introspect_("Swap") = BUS_SWAP_INTERFACE;
 
 const char bus_swap_invalidating_properties[] =
@@ -92,5 +96,5 @@ DBusHandlerResult bus_swap_message_handler(Unit *u, DBusConnection *c, DBusMessa
                 { NULL, NULL, NULL, NULL, NULL }
         };
 
-        return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, properties);
+        return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties);
 }
index 1eb3c249f87d6bea15e10b0c3a94ca3c3a2aad31..1cbeccb579c85239dca9672aac3aa85f38bce622 100644 (file)
         BUS_INTROSPECTABLE_INTERFACE                                    \
         "</node>\n"
 
+#define INTERFACES_LIST                              \
+        BUS_UNIT_INTERFACES_LIST                     \
+        "org.freedesktop.systemd1.Target\0"
+
 const char bus_target_interface[] _introspect_("Target") = BUS_TARGET_INTERFACE;
 
 DBusHandlerResult bus_target_message_handler(Unit *u, DBusConnection *c, DBusMessage *message) {
@@ -46,5 +50,5 @@ DBusHandlerResult bus_target_message_handler(Unit *u, DBusConnection *c, DBusMes
                 { NULL, NULL, NULL, NULL, NULL }
         };
 
-        return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, properties);
+        return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties);
 }
index eed05e6991d5c1888bd1820b9e0fd53088398ed5..e44f4e2feaf8b2b87adbf9364c87ad0ce4bd57c9 100644 (file)
         BUS_INTROSPECTABLE_INTERFACE                                    \
         "</node>\n"
 
+#define INTERFACES_LIST                              \
+        BUS_UNIT_INTERFACES_LIST                     \
+        "org.freedesktop.systemd1.Timer\0"
+
 const char bus_timer_interface[] _introspect_("Timer") = BUS_TIMER_INTERFACE;
 
 const char bus_timer_invalidating_properties[] =
@@ -118,5 +122,5 @@ DBusHandlerResult bus_timer_message_handler(Unit *u, DBusConnection *c, DBusMess
                 { NULL, NULL, NULL, NULL, NULL }
         };
 
-        return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, properties);
+        return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties);
 }
index b51fe4eb672f53f1189b1875985840171c5fb5c2..4e46fc4825a575ec790b07eb2dfc86068da9815f 100644 (file)
         "  <property name=\"JobTimeoutUSec\" type=\"t\" access=\"read\"/>\n" \
         " </interface>\n"
 
+#define BUS_UNIT_INTERFACES_LIST                \
+        BUS_GENERIC_INTERFACES_LIST             \
+        "org.freedesktop.systemd1.Unit\0"
+
 #define BUS_UNIT_PROPERTIES \
         { "org.freedesktop.systemd1.Unit", "Id",                   bus_property_append_string,     "s",    u->meta.id                        }, \
         { "org.freedesktop.systemd1.Unit", "Names",                bus_unit_append_names,          "as",   u                                 }, \
index dae5f9ec4cec082d603bc5fff476bd16cd61e486..af03c5746ca6dddfe4bf73fd9c443fe1d523083f 100644 (file)
@@ -1213,12 +1213,20 @@ oom:
         return -ENOMEM;
 }
 
-DBusHandlerResult bus_default_message_handler(Manager *m, DBusConnection *c, DBusMessage *message, const char*introspection, const BusProperty *properties) {
+DBusHandlerResult bus_default_message_handler(
+                Manager *m,
+                DBusConnection *c,
+                DBusMessage *message,
+                const char *introspection,
+                const char *interfaces,
+                const BusProperty *properties) {
+
         DBusError error;
         DBusMessage *reply = NULL;
         int r;
 
         assert(m);
+        assert(c);
         assert(message);
 
         dbus_error_init(&error);
@@ -1269,6 +1277,13 @@ DBusHandlerResult bus_default_message_handler(Manager *m, DBusConnection *c, DBu
 
                         if (!dbus_message_iter_close_container(&iter, &sub))
                                 goto oom;
+                } else {
+                        if (!nulstr_contains(interfaces, interface))
+                                dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
+                        else
+                                dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
+
+                        return bus_send_error_reply(m, c, message, &error, -EINVAL);
                 }
 
         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
@@ -1283,6 +1298,11 @@ DBusHandlerResult bus_default_message_handler(Manager *m, DBusConnection *c, DBu
                             DBUS_TYPE_INVALID))
                         return bus_send_error_reply(m, c, message, &error, -EINVAL);
 
+                if (interface[0] && !nulstr_contains(interfaces, interface)) {
+                        dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
+                        return bus_send_error_reply(m, c, message, &error, -EINVAL);
+                }
+
                 if (!(reply = dbus_message_new_method_return(message)))
                         goto oom;
 
@@ -1367,8 +1387,20 @@ DBusHandlerResult bus_default_message_handler(Manager *m, DBusConnection *c, DBu
 
                         if (!(reply = dbus_message_new_method_return(message)))
                                 goto oom;
-                } else
-                        return bus_send_error_reply(m, c, message, NULL, -EINVAL);
+                } else {
+                        if (p->property)
+                                dbus_set_error_const(&error, DBUS_ERROR_PROPERTY_READ_ONLY, "Property read-only");
+                        else if (!nulstr_contains(interfaces, interface))
+                                dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
+                        else
+                                dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
+
+                        return bus_send_error_reply(m, c, message, &error, -EINVAL);
+                }
+
+        } else if (!nulstr_contains(interfaces, dbus_message_get_interface(message))) {
+                dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
+                return bus_send_error_reply(m, c, message, &error, -EINVAL);
         }
 
         if (reply) {
index 2aaeb4745ff776afd200a2e3f56cacca18cd2d14..f93ad6203b5ebcaa756bcb8b24a03018d05fdc99 100644 (file)
 #define DBUS_ERROR_UNKNOWN_INTERFACE "org.freedesktop.DBus.Error.UnknownInterface"
 #endif
 
+#ifndef DBUS_ERROR_UNKNOWN_PROPERTY
+#define DBUS_ERROR_UNKNOWN_PROPERTY "org.freedesktop.DBus.Error.UnknownProperty"
+#endif
+
+#ifndef DBUS_ERROR_PROPERTY_READ_ONLY
+#define DBUS_ERROR_PROPERTY_READ_ONLY "org.freedesktop.DBus.Error.PropertyReadOnly"
+#endif
+
 #include "manager.h"
 
 typedef int (*BusPropertyCallback)(Manager *m, DBusMessageIter *iter, const char *property, void *data);
@@ -84,6 +92,11 @@ typedef struct BusProperty {
         " </method>\n"                                                  \
         "</interface>\n"
 
+#define BUS_GENERIC_INTERFACES_LIST             \
+        "org.freedesktop.DBus.Properties\0"     \
+        "org.freedesktop.DBus.Introspectable\0" \
+        "org.freedesktop.DBus.Peer\0"
+
 int bus_init(Manager *m, bool try_bus_connect);
 void bus_done(Manager *m);
 
@@ -94,7 +107,7 @@ void bus_timeout_event(Manager *m, Watch *w, int events);
 
 int bus_query_pid(Manager *m, const char *name);
 
-DBusHandlerResult bus_default_message_handler(Manager *m, DBusConnection *c, DBusMessage *message, const char* introspection, const BusProperty *properties);
+DBusHandlerResult bus_default_message_handler(Manager *m, DBusConnection *c, DBusMessage *message, const char* introspection, const char *interfaces, const BusProperty *properties);
 DBusHandlerResult bus_send_error_reply(Manager *m, DBusConnection *c, DBusMessage *message, DBusError *bus_error, int error);
 
 int bus_broadcast(Manager *m, DBusMessage *message);
index 4acb3c305c0f7d612573558e7104b98895ad7ec2..c02b39e0ad5fb896d1c80977328b9762b2678e3c 100644 (file)
@@ -4079,6 +4079,19 @@ int kill_and_sigcont(pid_t pid, int sig) {
         return r;
 }
 
+bool nulstr_contains(const char*nulstr, const char *needle) {
+        const char *i;
+
+        if (!nulstr)
+                return false;
+
+        NULSTR_FOREACH(i, nulstr)
+                if (streq(i, needle))
+                        return true;
+
+        return false;
+}
+
 static const char *const ioprio_class_table[] = {
         [IOPRIO_CLASS_NONE] = "none",
         [IOPRIO_CLASS_RT] = "realtime",
index a2e3b944399a6a292d0c227e92c40250d3acb460..13e5f6203ac9233980287f25b7cbf5a0562cbff9 100644 (file)
@@ -386,6 +386,8 @@ void execute_directory(const char *directory, DIR *_d, char *argv[]);
 
 int kill_and_sigcont(pid_t pid, int sig);
 
+bool nulstr_contains(const char*nulstr, const char *needle);
+
 #define NULSTR_FOREACH(i, l)                                    \
         for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)