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.
This commit is contained in:
Lennart Poettering 2014-03-11 19:03:50 +01:00
parent 2be56ff1e4
commit a6278b8830
13 changed files with 241 additions and 143 deletions

View File

@ -691,7 +691,6 @@ MANPAGES += \
man/sd_bus_creds_get_pid.3 \
man/sd_bus_creds_new_from_pid.3 \
man/sd_bus_error.3 \
man/sd_bus_label_escape.3 \
man/sd_bus_message_append.3 \
man/sd_bus_message_append_array.3 \
man/sd_bus_message_append_basic.3 \
@ -701,6 +700,7 @@ MANPAGES += \
man/sd_bus_message_get_monotonic_usec.3 \
man/sd_bus_new.3 \
man/sd_bus_open_user.3 \
man/sd_bus_path_encode.3 \
man/sd_bus_request_name.3 \
man/systemd-bus-proxyd.8 \
man/systemd-bus-proxyd@.service.8
@ -742,7 +742,6 @@ MANPAGES_ALIAS += \
man/sd_bus_error_set_const.3 \
man/sd_bus_error_set_errno.3 \
man/sd_bus_error_set_errnof.3 \
man/sd_bus_label_unescape.3 \
man/sd_bus_message_append_array_iovec.3 \
man/sd_bus_message_append_array_memfd.3 \
man/sd_bus_message_append_array_space.3 \
@ -754,6 +753,7 @@ MANPAGES_ALIAS += \
man/sd_bus_open_system.3 \
man/sd_bus_open_system_container.3 \
man/sd_bus_open_system_remote.3 \
man/sd_bus_path_decode.3 \
man/sd_bus_ref.3 \
man/sd_bus_release_name.3 \
man/sd_bus_unref.3 \
@ -795,7 +795,6 @@ man/sd_bus_error_set.3: man/sd_bus_error.3
man/sd_bus_error_set_const.3: man/sd_bus_error.3
man/sd_bus_error_set_errno.3: man/sd_bus_error.3
man/sd_bus_error_set_errnof.3: man/sd_bus_error.3
man/sd_bus_label_unescape.3: man/sd_bus_label_escape.3
man/sd_bus_message_append_array_iovec.3: man/sd_bus_message_append_array.3
man/sd_bus_message_append_array_memfd.3: man/sd_bus_message_append_array.3
man/sd_bus_message_append_array_space.3: man/sd_bus_message_append_array.3
@ -807,6 +806,7 @@ man/sd_bus_message_get_seqnum.3: man/sd_bus_message_get_monotonic_usec.3
man/sd_bus_open_system.3: man/sd_bus_open_user.3
man/sd_bus_open_system_container.3: man/sd_bus_open_user.3
man/sd_bus_open_system_remote.3: man/sd_bus_open_user.3
man/sd_bus_path_decode.3: man/sd_bus_path_encode.3
man/sd_bus_ref.3: man/sd_bus_new.3
man/sd_bus_release_name.3: man/sd_bus_request_name.3
man/sd_bus_unref.3: man/sd_bus_new.3
@ -922,9 +922,6 @@ man/sd_bus_error_set_errno.html: man/sd_bus_error.html
man/sd_bus_error_set_errnof.html: man/sd_bus_error.html
$(html-alias)
man/sd_bus_label_unescape.html: man/sd_bus_label_escape.html
$(html-alias)
man/sd_bus_message_append_array_iovec.html: man/sd_bus_message_append_array.html
$(html-alias)
@ -958,6 +955,9 @@ man/sd_bus_open_system_container.html: man/sd_bus_open_user.html
man/sd_bus_open_system_remote.html: man/sd_bus_open_user.html
$(html-alias)
man/sd_bus_path_decode.html: man/sd_bus_path_encode.html
$(html-alias)
man/sd_bus_ref.html: man/sd_bus_new.html
$(html-alias)
@ -1392,7 +1392,6 @@ EXTRA_DIST += \
man/sd_bus_creds_get_pid.xml \
man/sd_bus_creds_new_from_pid.xml \
man/sd_bus_error.xml \
man/sd_bus_label_escape.xml \
man/sd_bus_message_append.xml \
man/sd_bus_message_append_array.xml \
man/sd_bus_message_append_basic.xml \
@ -1402,6 +1401,7 @@ EXTRA_DIST += \
man/sd_bus_message_get_monotonic_usec.xml \
man/sd_bus_new.xml \
man/sd_bus_open_user.xml \
man/sd_bus_path_encode.xml \
man/sd_bus_request_name.xml \
man/sd_get_seats.xml \
man/sd_id128_get_machine.xml \

View File

@ -1,111 +0,0 @@
<?xml version='1.0'?> <!--*-nxml-*-->
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<!--
This file is part of systemd.
Copyright 2014 Zbigniew Jędrzejewski-Szmek
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
-->
<refentry id="sd_bus_label_escape" conditional="ENABLE_KDBUS">
<refentryinfo>
<title>sd_bus_label_escape</title>
<productname>systemd</productname>
<authorgroup>
<author>
<contrib>A monkey with a typewriter</contrib>
<firstname>Zbigniew</firstname>
<surname>Jędrzejewski-Szmek</surname>
<email>zbyszek@in.waw.pl</email>
</author>
</authorgroup>
</refentryinfo>
<refmeta>
<refentrytitle>sd_bus_label_escape</refentrytitle>
<manvolnum>3</manvolnum>
</refmeta>
<refnamediv>
<refname>sd_bus_label_escape</refname>
<refname>sd_bus_label_unescape</refname>
<refpurpose>Escape D-Bus object path special characters</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcsynopsisinfo>#include &lt;systemd/sd-bus.h&gt;</funcsynopsisinfo>
<funcprototype>
<funcdef>char *<function>sd_bus_label_escape</function></funcdef>
<paramdef>const char *<parameter>s</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>char *<function>sd_bus_label_unescape</function></funcdef>
<paramdef>const char *<parameter>f</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para><function>sd_bus_label_escape()</function> takes a
<constant>NUL</constant>-terminated string as a argument. It will
replace all characters which are invalid in a D-Bus object path by
<literal>_</literal> and a hexadecimal number. As a special case,
the empty string will be replaced by a lone <literal>_</literal>.
<function>sd_bus_label_unescape()</function> can be used to
reverse this process.</para>
</refsect1>
<refsect1>
<title>Return Value</title>
<para>On success, a new <constant>NUL</constant>-terminated string
will be returned. It must be
<citerefentry><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>d
by the caller. If a memory allocation failure occurs,
<constant>NULL</constant> will be returned.
</para>
</refsect1>
<refsect1>
<title>Notes</title>
<para><function>sd_bus_label_escape()</function> and
<function>sd_bus_label_unescape()</function> are available as a
shared library, which can be compiled and linked to with the
<constant>libsystemd</constant> <citerefentry><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
file.</para>
</refsect1>
<refsect1>
<title>See Also</title>
<para>
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
</para>
</refsect1>
</refentry>

150
man/sd_bus_path_encode.xml Normal file
View File

@ -0,0 +1,150 @@
<?xml version='1.0'?> <!--*-nxml-*-->
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<!--
This file is part of systemd.
Copyright 2014 Zbigniew Jędrzejewski-Szmek
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
-->
<refentry id="sd_bus_path_encode" conditional="ENABLE_KDBUS">
<refentryinfo>
<title>sd_bus_path_encode</title>
<productname>systemd</productname>
<authorgroup>
<author>
<contrib>A monkey with a typewriter</contrib>
<firstname>Zbigniew</firstname>
<surname>Jędrzejewski-Szmek</surname>
<email>zbyszek@in.waw.pl</email>
</author>
</authorgroup>
</refentryinfo>
<refmeta>
<refentrytitle>sd_bus_path_encode</refentrytitle>
<manvolnum>3</manvolnum>
</refmeta>
<refnamediv>
<refname>sd_bus_path_encode</refname>
<refname>sd_bus_path_decode</refname>
<refpurpose>Convert an external identifier into an object path and back</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcsynopsisinfo>#include &lt;systemd/sd-bus.h&gt;</funcsynopsisinfo>
<funcprototype>
<funcdef>int <function>sd_bus_path_encode</function></funcdef>
<paramdef>const char *<parameter>prefix</parameter></paramdef>
<paramdef>const char *<parameter>external_id</parameter></paramdef>
<paramdef>char **<parameter>ret_path</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_bus_path_decode</function></funcdef>
<paramdef>const char *<parameter>prefix</parameter></paramdef>
<paramdef>const char *<parameter>path</parameter></paramdef>
<paramdef>char **<parameter>ret_external_id</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para><function>sd_bus_path_encode()</function> and
<function>sd_bus_path_decode()</function> convert external
identifier strings into object paths and back. These functions are
useful to map application-specific string identifiers of any kind
into bus object paths in a simple, reversible and safe way.</para>
<para><function>sd_bus_path_encode()</function> takes a bus path
prefix and an external identifier string as arguments, plus a
place to store the returned bus path string. The bus path prefix
must be a valid bus path, starting with a slash
<literal>/</literal>, but not ending in one. The external
identifier string may be in any format, may be the empty string
and no restrictions on the charset are made - however it must
always be <constant>NUL</constant>-terminated. The returned string
will be the concatenation of the bus path prefix plus an escaped
version of the external identifier string. This operation may be
reversed with <function>sd_bus_decode()</function>. It is
recommended to only use external identifiers here that generally
require little escaping to be turned into valid bus path
identifiers (for example by sticking to a 7bit ASCII character
set), in order to ensure the resulting bus path is still short and
easily processed.</para>
<para><function>sd_bus_path_decode()</function> reverses the
operation of <function>sd_bus_path_encode()</function> and thus
regenerates an external identifier string from a bus path. It
takes a bus path and a prefix string, plus a place to store the
returned external identifier string. If the bus path does not
start with the specified prefix, 0 is returned and the returned
string is set to <constant>NULL</constant>. Otherwise the the
string following the prefix is unescaped and returned in the
external identifier string.</para>
<para>The escaping used will will replace all characters which are
invalid in a bus object path by <literal>_</literal> followed by a
hexadecimal value. As a special case, the empty string will be
replaced by a lone <literal>_</literal>.</para>
</refsect1>
<refsect1>
<title>Return Value</title>
<para>On success, <function>sd_bus_path_encode()</function>
returns positive or 0, and a valid bus path in the return
argument. On success, <function>sd_bus_path_decode()</function>
returns a positive value if the prefixed matched, or 0 if it
didn't. If the prefix matched the external identifier is returned
in the return parameter. If it did not match NULL is returned in
the return parameter. On failure, a negative errno-style error
number is returned by either function. The returned strings must
be
<citerefentry><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>'d
by the caller.</para>
</refsect1>
<refsect1>
<title>Notes</title>
<para><function>sd_bus_path_encode()</function> and
<function>sd_bus_path_decode()</function> are available as a
shared library, which can be compiled and linked to with the
<constant>libsystemd</constant> <citerefentry><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
file.</para>
</refsect1>
<refsect1>
<title>See Also</title>
<para>
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
</para>
</refsect1>
</refentry>

View File

@ -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;

View File

@ -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;
}

View File

@ -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;

View File

@ -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) {

View File

@ -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;

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -24,6 +24,7 @@
#include <sys/capability.h>
#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;

View File

@ -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 */