dbus/dbus dbus-address.c, 1.14, 1.15 dbus-address.h, 1.4,
1.5 dbus-internals.h, 1.51, 1.52 dbus-server-debug-pipe.c,
1.16, 1.17 dbus-server-unix.c, 1.24, 1.25 dbus-string.c, 1.65,
1.66 dbus-string.h, 1.38, 1.39
Havoc Pennington
hp at freedesktop.org
Thu Feb 10 19:37:06 PST 2005
Update of /cvs/dbus/dbus/dbus
In directory gabe:/tmp/cvs-serv15549/dbus
Modified Files:
dbus-address.c dbus-address.h dbus-internals.h
dbus-server-debug-pipe.c dbus-server-unix.c dbus-string.c
dbus-string.h
Log Message:
2005-02-10 Havoc Pennington <hp at redhat.com>
* test/glib/test-dbus-glib.c (main): fix so this test doesn't fail
(call dbus_g_proxy_add_signal)
* dbus/dbus-server-unix.c (_dbus_server_new_for_tcp_socket):
escape the hostname
(_dbus_server_new_for_domain_socket): escape the path
* dbus/dbus-address.c (dbus_address_escape_value): new
(dbus_address_unescape_value): new
(dbus_parse_address): unescape values
* dbus/dbus-string.c (_dbus_string_append_byte_as_hex): new function
* doc/dbus-specification.xml: explain how to escape values in
addresses
Index: dbus-address.c
===================================================================
RCS file: /cvs/dbus/dbus/dbus/dbus-address.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- dbus-address.c 10 Aug 2004 03:06:59 -0000 1.14
+++ dbus-address.c 11 Feb 2005 03:37:03 -0000 1.15
@@ -2,7 +2,7 @@
/* dbus-address.c Server address parser.
*
* Copyright (C) 2003 CodeFactory AB
- * Copyright (C) 2004 Red Hat, Inc.
+ * Copyright (C) 2004,2005 Red Hat, Inc.
*
* Licensed under the Academic Free License version 2.1
*
@@ -140,7 +140,7 @@
*
* @param entry the entry.
* @param key the key.
- * @returns the key value. This string must not be fred.
+ * @returns the key value. This string must not be freed.
*/
const char *
dbus_address_entry_get_value (DBusAddressEntry *entry,
@@ -165,15 +165,150 @@
return NULL;
}
+#define _DBUS_ADDRESS_OPTIONALLY_ESCAPED_BYTE(b) \
+ (((b) >= 'a' && (b) <= 'z') || \
+ ((b) >= 'A' && (b) <= 'Z') || \
+ ((b) >= '0' && (b) <= '9') || \
+ (b) == '-' || \
+ (b) == '_' || \
+ (b) == '/' || \
+ (b) == '\\' || \
+ (b) == '.')
+
+/**
+ * Appends an escaped version of one string to another string,
+ * using the D-BUS address escaping mechanism
+ *
+ * @param escaped the string to append to
+ * @param unescaped the string to escape
+ * @returns #FALSE if no memory
+ */
+dbus_bool_t
+_dbus_address_append_escaped (DBusString *escaped,
+ const DBusString *unescaped)
+{
+ const char *p;
+ const char *end;
+ dbus_bool_t ret;
+ int orig_len;
+
+ ret = FALSE;
+
+ orig_len = _dbus_string_get_length (escaped);
+ p = _dbus_string_get_const_data (unescaped);
+ end = p + _dbus_string_get_length (unescaped);
+ while (p != end)
+ {
+ if (_DBUS_ADDRESS_OPTIONALLY_ESCAPED_BYTE (*p))
+ {
+ if (!_dbus_string_append_byte (escaped, *p))
+ goto out;
+ }
+ else
+ {
+ if (!_dbus_string_append_byte (escaped, '%'))
+ goto out;
+ if (!_dbus_string_append_byte_as_hex (escaped, *p))
+ goto out;
+ }
+
+ ++p;
+ }
+
+ ret = TRUE;
+
+ out:
+ if (!ret)
+ _dbus_string_set_length (escaped, orig_len);
+ return ret;
+}
+
+static dbus_bool_t
+append_unescaped_value (DBusString *unescaped,
+ const DBusString *escaped,
+ int escaped_start,
+ int escaped_len,
+ DBusError *error)
+{
+ const char *p;
+ const char *end;
+ dbus_bool_t ret;
+
+ ret = FALSE;
+
+ p = _dbus_string_get_const_data (escaped) + escaped_start;
+ end = p + escaped_len;
+ while (p != end)
+ {
+ if (_DBUS_ADDRESS_OPTIONALLY_ESCAPED_BYTE (*p))
+ {
+ if (!_dbus_string_append_byte (unescaped, *p))
+ goto out;
+ }
+ else if (*p == '%')
+ {
+ /* Efficiency is king */
+ char buf[3];
+ DBusString hex;
+ int hex_end;
+
+ ++p;
+
+ if ((p + 2) > end)
+ {
+ dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
+ "In D-BUS address, percent character was not followed by two hex digits");
+ goto out;
+ }
+
+ buf[0] = *p;
+ ++p;
+ buf[1] = *p;
+ buf[2] = '\0';
+
+ _dbus_string_init_const (&hex, buf);
+
+ if (!_dbus_string_hex_decode (&hex, 0, &hex_end,
+ unescaped,
+ _dbus_string_get_length (unescaped)))
+ goto out;
+
+ if (hex_end != 2)
+ {
+ dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
+ "In D-BUS address, percent character was followed by characters other than hex digits");
+ goto out;
+ }
+ }
+ else
+ {
+ /* Error, should have been escaped */
+ dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
+ "In D-BUS address, character '%c' should have been escaped\n",
+ *p);
+ goto out;
+ }
+
+ ++p;
+ }
+
+ ret = TRUE;
+
+ out:
+ if (!ret && error && !dbus_error_is_set (error))
+ _DBUS_SET_OOM (error);
+
+ _dbus_assert (ret || error == NULL || dbus_error_is_set (error));
+
+ return ret;
+}
+
/**
* Parses an address string of the form:
*
* method:key=value,key=value;method:key=value
*
* @todo document address format in the specification
- *
- * @todo need to be able to escape ';' and ',' in the
- * key values, and the parsing needs to handle that.
*
* @param address the address.
* @param entry return location to an array of entries.
@@ -306,9 +441,10 @@
goto error;
}
- if (!_dbus_string_copy_len (&str, equals_pos + 1, comma_pos - equals_pos - 1, value, 0))
+ if (!append_unescaped_value (value, &str, equals_pos + 1,
+ comma_pos - equals_pos - 1, error))
{
- dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+ _dbus_assert (error == NULL || dbus_error_is_set (error));
_dbus_string_free (key);
_dbus_string_free (value);
@@ -386,11 +522,124 @@
}
+/**
+ * Escapes the given string as a value in a key=value pair
+ * for a D-BUS address.
+ *
+ * @param value the unescaped value
+ * @returns newly-allocated escaped value or #NULL if no memory
+ */
+char*
+dbus_address_escape_value (const char *value)
+{
+ DBusString escaped;
+ DBusString unescaped;
+ char *ret;
+
+ ret = NULL;
+
+ _dbus_string_init_const (&unescaped, value);
+
+ if (!_dbus_string_init (&escaped))
+ return NULL;
+
+ if (!_dbus_address_append_escaped (&escaped, &unescaped))
+ goto out;
+
+ if (!_dbus_string_steal_data (&escaped, &ret))
+ goto out;
+
+ out:
+ _dbus_string_free (&escaped);
+ return ret;
+}
+
+/**
+ * Unescapes the given string as a value in a key=value pair
+ * for a D-BUS address.
+ *
+ * @param value the escaped value
+ * @param error error to set if the unescaping fails
+ * @returns newly-allocated unescaped value or #NULL if no memory
+ */
+char*
+dbus_address_unescape_value (const char *value,
+ DBusError *error)
+{
+ DBusString unescaped;
+ DBusString escaped;
+ char *ret;
+
+ ret = NULL;
+
+ _dbus_string_init_const (&escaped, value);
+
+ if (!_dbus_string_init (&unescaped))
+ return NULL;
+
+ if (!append_unescaped_value (&unescaped, &escaped,
+ 0, _dbus_string_get_length (&escaped),
+ error))
+ goto out;
+
+ if (!_dbus_string_steal_data (&unescaped, &ret))
+ goto out;
+
+ out:
+ if (ret == NULL && error && !dbus_error_is_set (error))
+ _DBUS_SET_OOM (error);
+
+ _dbus_assert (ret != NULL || error == NULL || dbus_error_is_set (error));
+
+ _dbus_string_free (&unescaped);
+ return ret;
+}
/** @} */ /* End of public API */
#ifdef DBUS_BUILD_TESTS
#include "dbus-test.h"
+#include <stdlib.h>
+
+typedef struct
+{
+ const char *escaped;
+ const char *unescaped;
+} EscapeTest;
+
+static const EscapeTest escape_tests[] = {
+ { "abcde", "abcde" },
+ { "", "" },
+ { "%20%20", " " },
+ { "%24", "$" },
+ { "%25", "%" },
+ { "abc%24", "abc$" },
+ { "%24abc", "$abc" },
+ { "abc%24abc", "abc$abc" },
+ { "/", "/" },
+ { "-", "-" },
+ { "_", "_" },
+ { "A", "A" },
+ { "I", "I" },
+ { "Z", "Z" },
+ { "a", "a" },
+ { "i", "i" },
+ { "z", "z" }
+};
+
+static const char* invalid_escaped_values[] = {
+ "%a",
+ "%q",
+ "%az",
+ "%%",
+ "%$$",
+ "abc%a",
+ "%axyz",
+ "%",
+ "$",
+ " ",
+ "*"
+};
dbus_bool_t
_dbus_address_test (void)
@@ -398,8 +647,69 @@
DBusAddressEntry **entries;
int len;
DBusError error;
+ int i;
dbus_error_init (&error);
+
+ i = 0;
+ while (i < _DBUS_N_ELEMENTS (escape_tests))
+ {
+ const EscapeTest *test = &escape_tests[i];
+ char *escaped;
+ char *unescaped;
+
+ escaped = dbus_address_escape_value (test->unescaped);
+ if (escaped == NULL)
+ _dbus_assert_not_reached ("oom");
+
+ if (strcmp (escaped, test->escaped) != 0)
+ {
+ _dbus_warn ("Escaped '%s' as '%s' should have been '%s'\n",
+ test->unescaped, escaped, test->escaped);
+ exit (1);
+ }
+ dbus_free (escaped);
+
+ unescaped = dbus_address_unescape_value (test->escaped, &error);
+ if (unescaped == NULL)
+ {
+ _dbus_warn ("Failed to unescape '%s': %s\n",
+ test->escaped, error.message);
+ dbus_error_free (&error);
+ exit (1);
+ }
+
+ if (strcmp (unescaped, test->unescaped) != 0)
+ {
+ _dbus_warn ("Unescaped '%s' as '%s' should have been '%s'\n",
+ test->escaped, unescaped, test->unescaped);
+ exit (1);
+ }
+ dbus_free (unescaped);
+
+ ++i;
+ }
+
+ i = 0;
+ while (i < _DBUS_N_ELEMENTS (invalid_escaped_values))
+ {
+ char *unescaped;
+
+ unescaped = dbus_address_unescape_value (invalid_escaped_values[i],
+ &error);
+ if (unescaped != NULL)
+ {
+ _dbus_warn ("Should not have successfully unescaped '%s' to '%s'\n",
+ invalid_escaped_values[i], unescaped);
+ dbus_free (unescaped);
+ exit (1);
+ }
+
+ _dbus_assert (dbus_error_is_set (&error));
+ dbus_error_free (&error);
+
+ ++i;
+ }
if (!dbus_parse_address ("unix:path=/tmp/foo;debug:name=test,sliff=sloff;",
&entries, &len, &error))
Index: dbus-address.h
===================================================================
RCS file: /cvs/dbus/dbus/dbus/dbus-address.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- dbus-address.h 10 Aug 2004 03:06:59 -0000 1.4
+++ dbus-address.h 11 Feb 2005 03:37:03 -0000 1.5
@@ -41,8 +41,9 @@
const char *dbus_address_entry_get_method (DBusAddressEntry *entry);
void dbus_address_entries_free (DBusAddressEntry **entries);
-
-
+char* dbus_address_escape_value (const char *value);
+char* dbus_address_unescape_value (const char *value,
+ DBusError *error);
#endif /* DBUS_ADDRESS_H */
Index: dbus-internals.h
===================================================================
RCS file: /cvs/dbus/dbus/dbus/dbus-internals.h,v
retrieving revision 1.51
retrieving revision 1.52
diff -u -d -r1.51 -r1.52
--- dbus-internals.h 28 Jan 2005 03:06:55 -0000 1.51
+++ dbus-internals.h 11 Feb 2005 03:37:03 -0000 1.52
@@ -284,6 +284,9 @@
dbus_bool_t _dbus_threads_init_debug (void);
+dbus_bool_t _dbus_address_append_escaped (DBusString *escaped,
+ const DBusString *unescaped);
+
DBUS_END_DECLS
#endif /* DBUS_INTERNALS_H */
Index: dbus-server-debug-pipe.c
===================================================================
RCS file: /cvs/dbus/dbus/dbus/dbus-server-debug-pipe.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -d -r1.16 -r1.17
--- dbus-server-debug-pipe.c 10 Aug 2004 03:07:00 -0000 1.16
+++ dbus-server-debug-pipe.c 11 Feb 2005 03:37:03 -0000 1.17
@@ -134,6 +134,7 @@
{
DBusServerDebugPipe *debug_server;
DBusString address;
+ DBusString name_str;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
@@ -154,8 +155,9 @@
if (!_dbus_string_init (&address))
goto nomem_1;
+ _dbus_string_init_const (&name_str, server_name);
if (!_dbus_string_append (&address, "debug-pipe:name=") ||
- !_dbus_string_append (&address, server_name))
+ !_dbus_address_append_escaped (&address, &name_str))
goto nomem_2;
debug_server->name = _dbus_strdup (server_name);
Index: dbus-server-unix.c
===================================================================
RCS file: /cvs/dbus/dbus/dbus/dbus-server-unix.c,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -d -r1.24 -r1.25
--- dbus-server-unix.c 26 Nov 2004 01:53:13 -0000 1.24
+++ dbus-server-unix.c 11 Feb 2005 03:37:03 -0000 1.25
@@ -279,6 +279,7 @@
int listen_fd;
DBusString address;
char *path_copy;
+ DBusString path_str;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
@@ -288,11 +289,12 @@
return NULL;
}
+ _dbus_string_init_const (&path_str, path);
if ((abstract &&
!_dbus_string_append (&address, "unix:abstract=")) ||
(!abstract &&
!_dbus_string_append (&address, "unix:path=")) ||
- !_dbus_string_append (&address, path))
+ !_dbus_address_append_escaped (&address, &path_str))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
goto failed_0;
@@ -355,6 +357,7 @@
DBusServer *server;
int listen_fd;
DBusString address;
+ DBusString host_str;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
@@ -366,9 +369,10 @@
if (host == NULL)
host = "localhost";
-
+
+ _dbus_string_init_const (&host_str, host);
if (!_dbus_string_append (&address, "tcp:host=") ||
- !_dbus_string_append (&address, host) ||
+ !_dbus_address_append_escaped (&address, &host_str) ||
!_dbus_string_append (&address, ",port=") ||
!_dbus_string_append_int (&address, port))
{
Index: dbus-string.c
===================================================================
RCS file: /cvs/dbus/dbus/dbus/dbus-string.c,v
retrieving revision 1.65
retrieving revision 1.66
diff -u -d -r1.65 -r1.66
--- dbus-string.c 28 Jan 2005 03:06:56 -0000 1.65
+++ dbus-string.c 11 Feb 2005 03:37:03 -0000 1.66
@@ -2237,6 +2237,38 @@
#endif /* DBUS_BUILD_TESTS */
/**
+ * Appends a two-character hex digit to a string, where the hex digit
+ * has the value of the given byte.
+ *
+ * @param str the string
+ * @param byte the byte
+ * @returns #FALSE if no memory
+ */
+dbus_bool_t
+_dbus_string_append_byte_as_hex (DBusString *str,
+ int byte)
+{
+ const char hexdigits[16] = {
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ 'a', 'b', 'c', 'd', 'e', 'f'
+ };
+
+ if (!_dbus_string_append_byte (str,
+ hexdigits[(byte >> 4)]))
+ return FALSE;
+
+ if (!_dbus_string_append_byte (str,
+ hexdigits[(byte & 0x0f)]))
+ {
+ _dbus_string_set_length (str,
+ _dbus_string_get_length (str) - 1);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/**
* Encodes a string in hex, the way MD5 and SHA-1 are usually
* encoded. (Each byte is two hex digits.)
*
@@ -2253,10 +2285,6 @@
int insert_at)
{
DBusString result;
- const char hexdigits[16] = {
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
- 'a', 'b', 'c', 'd', 'e', 'f'
- };
const unsigned char *p;
const unsigned char *end;
dbus_bool_t retval;
@@ -2274,14 +2302,9 @@
while (p != end)
{
- if (!_dbus_string_append_byte (&result,
- hexdigits[(*p >> 4)]))
+ if (!_dbus_string_append_byte_as_hex (&result, *p))
goto out;
- if (!_dbus_string_append_byte (&result,
- hexdigits[(*p & 0x0f)]))
- goto out;
-
++p;
}
Index: dbus-string.h
===================================================================
RCS file: /cvs/dbus/dbus/dbus/dbus-string.h,v
retrieving revision 1.38
retrieving revision 1.39
diff -u -d -r1.38 -r1.39
--- dbus-string.h 28 Jan 2005 03:06:56 -0000 1.38
+++ dbus-string.h 11 Feb 2005 03:37:03 -0000 1.39
@@ -253,6 +253,8 @@
DBusString *dest);
void _dbus_string_delete_first_word (DBusString *str);
void _dbus_string_delete_leading_blanks (DBusString *str);
+dbus_bool_t _dbus_string_append_byte_as_hex (DBusString *str,
+ int byte);
dbus_bool_t _dbus_string_hex_encode (const DBusString *source,
int start,
DBusString *dest,
More information about the dbus-commit
mailing list