From a6278b88305b237b02eabff0d870b57fe851822d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 11 Mar 2014 19:03:50 +0100 Subject: bus: replace sd_bus_label_{escape,unescape}() by new sd_bus_path_{encode,decode}() The new calls work similarly, but enforce a that a common, fixed bus path prefix is used. This follows discussions with Simon McVittie on IRC that it should be a good idea to make sure that people don't use the escaping applied here too wildly as anything other than the last label of a bus path. --- src/libsystemd/libsystemd.sym.m4 | 4 +-- src/libsystemd/sd-bus/bus-creds.c | 3 ++- src/libsystemd/sd-bus/bus-kernel.c | 9 ++++--- src/libsystemd/sd-bus/sd-bus.c | 42 +++++++++++++++++++++++++++++--- src/libsystemd/sd-bus/sd-memfd.c | 7 +++--- src/libsystemd/sd-bus/test-bus-marshal.c | 25 ++++++++++++++++--- src/login/logind-seat-dbus.c | 5 ++-- src/login/logind-session-dbus.c | 5 ++-- src/machine/machine-dbus.c | 5 ++-- src/systemd/sd-bus.h | 4 +-- 10 files changed, 84 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/libsystemd/libsystemd.sym.m4 b/src/libsystemd/libsystemd.sym.m4 index c708c7f38f..9c29169e7c 100644 --- a/src/libsystemd/libsystemd.sym.m4 +++ b/src/libsystemd/libsystemd.sym.m4 @@ -311,8 +311,8 @@ m4_ifdef(`ENABLE_KDBUS', sd_bus_error_copy; sd_bus_error_is_set; sd_bus_error_has_name; - sd_bus_label_escape; - sd_bus_label_unescape; + sd_bus_path_encode; + sd_bus_path_decode; sd_bus_track_new; sd_bus_track_ref; sd_bus_track_unref; diff --git a/src/libsystemd/sd-bus/bus-creds.c b/src/libsystemd/sd-bus/bus-creds.c index 2d56f3c461..3da77cf1d7 100644 --- a/src/libsystemd/sd-bus/bus-creds.c +++ b/src/libsystemd/sd-bus/bus-creds.c @@ -30,6 +30,7 @@ #include "time-util.h" #include "strv.h" #include "bus-creds.h" +#include "bus-label.h" enum { CAP_OFFSET_INHERITABLE = 0, @@ -474,7 +475,7 @@ _public_ int sd_bus_creds_get_connection_name(sd_bus_creds *c, const char **ret) assert(c->conn_name); if (!c->unescaped_conn_name) { - c->unescaped_conn_name = sd_bus_label_unescape(c->conn_name); + c->unescaped_conn_name = bus_label_unescape(c->conn_name); if (!c->unescaped_conn_name) return -ENOMEM; } diff --git a/src/libsystemd/sd-bus/bus-kernel.c b/src/libsystemd/sd-bus/bus-kernel.c index 2a1b0b424a..8a2ca02996 100644 --- a/src/libsystemd/sd-bus/bus-kernel.c +++ b/src/libsystemd/sd-bus/bus-kernel.c @@ -36,6 +36,7 @@ #include "bus-kernel.h" #include "bus-bloom.h" #include "bus-util.h" +#include "bus-label.h" #include "cgroup-util.h" #define UNIQUE_NAME_MAX (3+DECIMAL_STR_MAX(uint64_t)) @@ -658,7 +659,7 @@ int bus_kernel_take_fd(sd_bus *b) { b->use_memfd = 1; if (b->connection_name) { - g = sd_bus_label_escape(b->connection_name); + g = bus_label_escape(b->connection_name); if (!g) return -ENOMEM; @@ -678,7 +679,7 @@ int bus_kernel_take_fd(sd_bus *b) { } else { _cleanup_free_ char *e = NULL; - e = sd_bus_label_escape(pr); + e = bus_label_escape(pr); if (!e) return -ENOMEM; @@ -691,7 +692,7 @@ int bus_kernel_take_fd(sd_bus *b) { name = g; } - b->connection_name = sd_bus_label_unescape(name); + b->connection_name = bus_label_unescape(name); if (!b->connection_name) return -ENOMEM; } @@ -1119,7 +1120,7 @@ int bus_kernel_pop_memfd(sd_bus *bus, void **address, size_t *mapped, size_t *al assert(bus->connection_name); - g = sd_bus_label_escape(bus->connection_name); + g = bus_label_escape(bus->connection_name); if (!g) return -ENOMEM; diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index fbf1a5919f..ffa3369feb 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -3063,12 +3063,46 @@ _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) { return -ENXIO; } -_public_ char *sd_bus_label_escape(const char *s) { - return bus_label_escape(s); +_public_ int sd_bus_path_encode(const char *prefix, const char *external_id, char **ret_path) { + _cleanup_free_ char *e = NULL; + char *ret; + + assert_return(object_path_is_valid(prefix), -EINVAL); + assert_return(external_id, -EINVAL); + assert_return(ret_path, -EINVAL); + + e = bus_label_escape(external_id); + if (!e) + return -ENOMEM; + + ret = strjoin(prefix, "/", e, NULL); + if (!ret) + return -ENOMEM; + + *ret_path = ret; + return 0; } -_public_ char *sd_bus_label_unescape(const char *f) { - return bus_label_unescape(f); +_public_ int sd_bus_path_decode(const char *path, const char *prefix, char **external_id) { + const char *e; + char *ret; + + assert_return(object_path_is_valid(path), -EINVAL); + assert_return(object_path_is_valid(prefix), -EINVAL); + assert_return(external_id, -EINVAL); + + e = object_path_startswith(path, prefix); + if (!e) { + *external_id = NULL; + return 0; + } + + ret = bus_label_unescape(e); + if (!ret) + return -ENOMEM; + + *external_id = ret; + return 1; } _public_ int sd_bus_get_peer_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) { diff --git a/src/libsystemd/sd-bus/sd-memfd.c b/src/libsystemd/sd-bus/sd-memfd.c index 7c71476eee..8f9e236392 100644 --- a/src/libsystemd/sd-bus/sd-memfd.c +++ b/src/libsystemd/sd-bus/sd-memfd.c @@ -27,6 +27,7 @@ #include "util.h" #include "kdbus.h" +#include "bus-label.h" #include "sd-memfd.h" #include "sd-bus.h" @@ -56,7 +57,7 @@ _public_ int sd_memfd_new(sd_memfd **m, const char *name) { * set here, let's do the usual bus escaping to deal * with that. */ - g = sd_bus_label_escape(name); + g = bus_label_escape(name); if (!g) return -ENOMEM; @@ -76,7 +77,7 @@ _public_ int sd_memfd_new(sd_memfd **m, const char *name) { else { _cleanup_free_ char *e = NULL; - e = sd_bus_label_escape(pr); + e = bus_label_escape(pr); if (!e) return -ENOMEM; @@ -311,7 +312,7 @@ _public_ int sd_memfd_get_name(sd_memfd *m, char **name) { if (!n) return -ENOMEM; - e = sd_bus_label_unescape(n); + e = bus_label_unescape(n); if (!e) return -ENOMEM; diff --git a/src/libsystemd/sd-bus/test-bus-marshal.c b/src/libsystemd/sd-bus/test-bus-marshal.c index d438a231a6..2321873da5 100644 --- a/src/libsystemd/sd-bus/test-bus-marshal.c +++ b/src/libsystemd/sd-bus/test-bus-marshal.c @@ -38,17 +38,35 @@ #include "bus-message.h" #include "bus-util.h" #include "bus-dump.h" +#include "bus-label.h" + +static void test_bus_path_encode(void) { + _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL, *f = NULL; + + assert_se(sd_bus_path_encode("/foo/bar", "waldo", &a) >= 0 && streq(a, "/foo/bar/waldo")); + assert_se(sd_bus_path_decode(a, "/waldo", &b) == 0 && b == NULL); + assert_se(sd_bus_path_decode(a, "/foo/bar", &b) > 0 && streq(b, "waldo")); + + assert_se(sd_bus_path_encode("xxxx", "waldo", &c) < 0); + assert_se(sd_bus_path_encode("/foo/", "waldo", &c) < 0); + + assert_se(sd_bus_path_encode("/foo/bar", "", &c) >= 0 && streq(c, "/foo/bar/_")); + assert_se(sd_bus_path_decode(c, "/foo/bar", &d) > 0 && streq(d, "")); + + assert_se(sd_bus_path_encode("/foo/bar", "foo.bar", &e) >= 0 && streq(e, "/foo/bar/foo_2ebar")); + assert_se(sd_bus_path_decode(e, "/foo/bar", &f) > 0 && streq(f, "foo.bar")); +} static void test_bus_label_escape_one(const char *a, const char *b) { _cleanup_free_ char *t = NULL, *x = NULL, *y = NULL; - assert_se(t = sd_bus_label_escape(a)); + assert_se(t = bus_label_escape(a)); assert_se(streq(t, b)); - assert_se(x = sd_bus_label_unescape(t)); + assert_se(x = bus_label_unescape(t)); assert_se(streq(a, x)); - assert_se(y = sd_bus_label_unescape(b)); + assert_se(y = bus_label_unescape(b)); assert_se(streq(a, y)); } @@ -322,6 +340,7 @@ int main(int argc, char *argv[]) { assert_se(streq(d, "3")); test_bus_label_escape(); + test_bus_path_encode(); return 0; } diff --git a/src/login/logind-seat-dbus.c b/src/login/logind-seat-dbus.c index 315e6baf6b..82323d6b87 100644 --- a/src/login/logind-seat-dbus.c +++ b/src/login/logind-seat-dbus.c @@ -27,6 +27,7 @@ #include "bus-util.h" #include "strv.h" #include "bus-errors.h" +#include "bus-label.h" #include "logind.h" #include "logind-seat.h" @@ -356,7 +357,7 @@ int seat_object_find(sd_bus *bus, const char *path, const char *interface, void if (!p) return 0; - e = sd_bus_label_unescape(p); + e = bus_label_unescape(p); if (!e) return -ENOMEM; @@ -374,7 +375,7 @@ char *seat_bus_path(Seat *s) { assert(s); - t = sd_bus_label_escape(s->id); + t = bus_label_escape(s->id); if (!t) return NULL; diff --git a/src/login/logind-session-dbus.c b/src/login/logind-session-dbus.c index c3d4d60158..ffdc5582dc 100644 --- a/src/login/logind-session-dbus.c +++ b/src/login/logind-session-dbus.c @@ -27,6 +27,7 @@ #include "strv.h" #include "bus-util.h" #include "bus-errors.h" +#include "bus-label.h" #include "logind.h" #include "logind-session.h" @@ -515,7 +516,7 @@ int session_object_find(sd_bus *bus, const char *path, const char *interface, vo if (!p) return 0; - e = sd_bus_label_unescape(p); + e = bus_label_unescape(p); if (!e) return -ENOMEM; @@ -533,7 +534,7 @@ char *session_bus_path(Session *s) { assert(s); - t = sd_bus_label_escape(s->id); + t = bus_label_escape(s->id); if (!t) return NULL; diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c index c9e3bb7c75..920c9956ae 100644 --- a/src/machine/machine-dbus.c +++ b/src/machine/machine-dbus.c @@ -24,6 +24,7 @@ #include #include "bus-util.h" +#include "bus-label.h" #include "strv.h" #include "machine.h" @@ -182,7 +183,7 @@ int machine_object_find(sd_bus *bus, const char *path, const char *interface, vo if (!p) return 0; - e = sd_bus_label_unescape(p); + e = bus_label_unescape(p); if (!e) return -ENOMEM; @@ -200,7 +201,7 @@ char *machine_bus_path(Machine *m) { assert(m); - e = sd_bus_label_escape(m->name); + e = bus_label_escape(m->name); if (!e) return NULL; diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h index e26ca6b704..8817380f1a 100644 --- a/src/systemd/sd-bus.h +++ b/src/systemd/sd-bus.h @@ -349,8 +349,8 @@ int sd_bus_error_has_name(const sd_bus_error *e, const char *name); /* Label escaping */ -char *sd_bus_label_escape(const char *s); -char *sd_bus_label_unescape(const char *f); +int sd_bus_path_encode(const char *prefix, const char *external_id, char **ret_path); +int sd_bus_path_decode(const char *path, const char *prefix, char **ret_external_id); /* Tracking peers */ -- cgit v1.2.3-54-g00ecf