dbus-address.c
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-address.c Server address parser.
*
* Copyright (C) 2003 CodeFactory AB
* Copyright (C) 2004-2007 Red Hat, Inc.
* Copyright (C) 2007 Ralf Habacker
* Copyright (C) 2013 Chengwei Yang / Intel
*
* SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <config.h>
#include "dbus-address.h"
#include "dbus-internals.h"
#include "dbus-list.h"
#include "dbus-string.h"
#include "dbus-protocol.h"
#include <dbus/dbus-test-tap.h>
struct DBusAddressEntry
{
DBusString method;
DBusList *keys;
DBusList *values;
};
void
_dbus_set_bad_address (DBusError *error,
const char *address_problem_type,
const char *address_problem_field,
const char *address_problem_other)
{
if (address_problem_type != NULL)
dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
"Server address of type %s was missing argument %s",
address_problem_type, address_problem_field);
else
dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
"Could not parse server address: %s",
address_problem_other);
}
#define _DBUS_ADDRESS_OPTIONALLY_ESCAPED_BYTE(b) \
(((b) >= 'a' && (b) <= 'z') || \
((b) >= 'A' && (b) <= 'Z') || \
((b) >= '0' && (b) <= '9') || \
(b) == '-' || \
(b) == '_' || \
(b) == '/' || \
(b) == '\\' || \
(b) == '*' || \
(b) == '.')
dbus_bool_t
_dbus_address_append_escaped (DBusString *escaped,
const DBusString *unescaped)
{
const unsigned char *p;
const unsigned char *end;
dbus_bool_t ret;
int orig_len;
ret = FALSE;
orig_len = _dbus_string_get_length (escaped);
p = _dbus_string_get_const_udata (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;
}
/* End of internals */
static void
dbus_address_entry_free (DBusAddressEntry *entry)
{
DBusList *link;
_dbus_string_free (&entry->method);
link = _dbus_list_get_first_link (&entry->keys);
while (link != NULL)
{
_dbus_string_free (link->data);
dbus_free (link->data);
link = _dbus_list_get_next_link (&entry->keys, link);
}
_dbus_list_clear (&entry->keys);
link = _dbus_list_get_first_link (&entry->values);
while (link != NULL)
{
_dbus_string_free (link->data);
dbus_free (link->data);
link = _dbus_list_get_next_link (&entry->values, link);
}
_dbus_list_clear (&entry->values);
dbus_free (entry);
}
void
dbus_address_entries_free (DBusAddressEntry **entries)
{
int i;
for (i = 0; entries[i] != NULL; i++)
dbus_address_entry_free (entries[i]);
dbus_free (entries);
}
static DBusAddressEntry *
create_entry (void)
{
DBusAddressEntry *entry;
entry = dbus_new0 (DBusAddressEntry, 1);
if (entry == NULL)
return NULL;
if (!_dbus_string_init (&entry->method))
{
dbus_free (entry);
return NULL;
}
return entry;
}
const char *
dbus_address_entry_get_method (DBusAddressEntry *entry)
{
return _dbus_string_get_const_data (&entry->method);
}
const char *
dbus_address_entry_get_value (DBusAddressEntry *entry,
const char *key)
{
DBusList *values, *keys;
keys = _dbus_list_get_first_link (&entry->keys);
values = _dbus_list_get_first_link (&entry->values);
while (keys != NULL)
{
_dbus_assert (values != NULL);
if (_dbus_string_equal_c_str (keys->data, key))
return _dbus_string_get_const_data (values->data);
keys = _dbus_list_get_next_link (&entry->keys, keys);
values = _dbus_list_get_next_link (&entry->values, values);
}
return NULL;
}
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;
}
dbus_bool_t
dbus_parse_address (const char *address,
DBusAddressEntry ***entry_result,
int *array_len,
DBusError *error)
{
DBusString str;
int pos, end_pos, len, i;
DBusList *entries, *link;
DBusAddressEntry **entry_array;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
_dbus_string_init_const (&str, address);
entries = NULL;
pos = 0;
len = _dbus_string_get_length (&str);
if (len == 0)
{
dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
"Empty address '%s'", address);
goto error;
}
while (pos < len)
{
DBusAddressEntry *entry;
int found_pos;
entry = create_entry ();
if (!entry)
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
goto error;
}
/* Append the entry */
if (!_dbus_list_append (&entries, entry))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
dbus_address_entry_free (entry);
goto error;
}
/* Look for a semi-colon */
if (!_dbus_string_find (&str, pos, ";", &end_pos))
end_pos = len;
/* Look for the colon : */
if (!_dbus_string_find_to (&str, pos, end_pos, ":", &found_pos))
{
dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, "Address does not contain a colon");
goto error;
}
if (!_dbus_string_copy_len (&str, pos, found_pos - pos, &entry->method, 0))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
goto error;
}
pos = found_pos + 1;
while (pos < end_pos)
{
int comma_pos, equals_pos;
if (!_dbus_string_find_to (&str, pos, end_pos, ",", &comma_pos))
comma_pos = end_pos;
if (!_dbus_string_find_to (&str, pos, comma_pos, "=", &equals_pos) ||
equals_pos == pos || equals_pos + 1 == comma_pos)
{
dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
"'=' character not found or has no value following it");
goto error;
}
else
{
DBusString *key;
DBusString *value;
key = dbus_new0 (DBusString, 1);
if (!key)
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
goto error;
}
value = dbus_new0 (DBusString, 1);
if (!value)
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
dbus_free (key);
goto error;
}
if (!_dbus_string_init (key))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
dbus_free (key);
dbus_free (value);
goto error;
}
if (!_dbus_string_init (value))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
_dbus_string_free (key);
dbus_free (key);
dbus_free (value);
goto error;
}
if (!_dbus_string_copy_len (&str, pos, equals_pos - pos, key, 0))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
_dbus_string_free (key);
_dbus_string_free (value);
dbus_free (key);
dbus_free (value);
goto error;
}
if (!append_unescaped_value (value, &str, equals_pos + 1,
comma_pos - equals_pos - 1, error))
{
_dbus_assert (error == NULL || dbus_error_is_set (error));
_dbus_string_free (key);
_dbus_string_free (value);
dbus_free (key);
dbus_free (value);
goto error;
}
if (!_dbus_list_append (&entry->keys, key))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
_dbus_string_free (key);
_dbus_string_free (value);
dbus_free (key);
dbus_free (value);
goto error;
}
if (!_dbus_list_append (&entry->values, value))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
_dbus_string_free (value);
dbus_free (value);
goto error;
}
}
pos = comma_pos + 1;
}
pos = end_pos + 1;
}
*array_len = _dbus_list_get_length (&entries);
entry_array = dbus_new (DBusAddressEntry *, *array_len + 1);
if (!entry_array)
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
goto error;
}
entry_array [*array_len] = NULL;
link = _dbus_list_get_first_link (&entries);
i = 0;
while (link != NULL)
{
entry_array[i] = link->data;
i++;
link = _dbus_list_get_next_link (&entries, link);
}
_dbus_list_clear (&entries);
*entry_result = entry_array;
return TRUE;
error:
link = _dbus_list_get_first_link (&entries);
while (link != NULL)
{
dbus_address_entry_free (link->data);
link = _dbus_list_get_next_link (&entries, link);
}
_dbus_list_clear (&entries);
return FALSE;
}
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;
}
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 */
_DBUS_ADDRESS_OPTIONALLY_ESCAPED_BYTE
#define _DBUS_ADDRESS_OPTIONALLY_ESCAPED_BYTE(b)
TRUE if the byte need not be escaped when found in a dbus address.
Definition dbus-address.c:89
_dbus_address_append_escaped
dbus_bool_t _dbus_address_append_escaped(DBusString *escaped, const DBusString *unescaped)
Appends an escaped version of one string to another string, using the D-Bus address escaping mechanis...
Definition dbus-address.c:109
_dbus_set_bad_address
void _dbus_set_bad_address(DBusError *error, const char *address_problem_type, const char *address_problem_field, const char *address_problem_other)
Sets DBUS_ERROR_BAD_ADDRESS.
Definition dbus-address.c:70
dbus_address_entries_free
void dbus_address_entries_free(DBusAddressEntry **entries)
Frees a NULL-terminated array of address entries.
Definition dbus-address.c:194
dbus_parse_address
dbus_bool_t dbus_parse_address(const char *address, DBusAddressEntry ***entry_result, int *array_len, DBusError *error)
Parses an address string of the form:
Definition dbus-address.c:368
dbus_address_entry_get_method
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
Definition dbus-address.c:232
dbus_address_unescape_value
char * dbus_address_unescape_value(const char *value, DBusError *error)
Unescapes the given string as a value in a key=value pair for a D-Bus address.
Definition dbus-address.c:622
dbus_address_escape_value
char * dbus_address_escape_value(const char *value)
Escapes the given string as a value in a key=value pair for a D-Bus address.
Definition dbus-address.c:588
dbus_address_entry_get_value
const char * dbus_address_entry_get_value(DBusAddressEntry *entry, const char *key)
Returns a value from a key of an entry.
Definition dbus-address.c:249
dbus_set_error
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition dbus-errors.c:356
dbus_error_is_set
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition dbus-errors.c:331
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition dbus-internals.h:153
_dbus_list_get_first_link
DBusList * _dbus_list_get_first_link(DBusList **list)
Gets the first link in the list.
Definition dbus-list.c:597
_dbus_list_get_length
int _dbus_list_get_length(DBusList **list)
Gets the length of a list.
Definition dbus-list.c:760
_dbus_list_clear
void _dbus_list_clear(DBusList **list)
Frees all links in the list and sets the list head to NULL.
Definition dbus-list.c:545
_dbus_list_append
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
Definition dbus-list.c:273
_dbus_list_get_next_link
#define _dbus_list_get_next_link(list, link)
Gets the next link in the list, or NULL if there are no more links.
Definition dbus-list.h:121
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:51
TRUE
#define TRUE
Expands to "1".
Definition dbus-macros.h:41
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:44
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition dbus-memory.c:710
dbus_new
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
Definition dbus-memory.h:59
dbus_new0
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition dbus-memory.h:60
DBUS_ERROR_BAD_ADDRESS
#define DBUS_ERROR_BAD_ADDRESS
A D-Bus bus address was malformed.
Definition dbus-protocol.h:373
DBUS_ERROR_NO_MEMORY
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
Definition dbus-protocol.h:363
_dbus_string_set_length
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition dbus-string.c:847
_dbus_string_hex_decode
dbus_bool_t _dbus_string_hex_decode(const DBusString *source, int start, int *end_return, DBusString *dest, int insert_at)
Decodes a string from hex encoding.
Definition dbus-string.c:2432
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition dbus-string.c:182
_dbus_string_init_const
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition dbus-string.c:197
_dbus_string_steal_data
dbus_bool_t _dbus_string_steal_data(DBusString *str, char **data_return)
Like _dbus_string_get_data(), but removes the gotten data from the original string.
Definition dbus-string.c:686
_dbus_string_find
dbus_bool_t _dbus_string_find(const DBusString *str, int start, const char *substr, int *found)
Finds the given substring in the string, returning TRUE and filling in the byte index where the subst...
Definition dbus-string.c:1666
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as _DBUS_STRING_IN...
Definition dbus-string.c:278
_dbus_string_equal_c_str
dbus_bool_t _dbus_string_equal_c_str(const DBusString *a, const char *c_str)
Checks whether a string is equal to a C string.
Definition dbus-string.c:2214
_dbus_string_append_byte
dbus_bool_t _dbus_string_append_byte(DBusString *str, unsigned char byte)
Appends a single byte to the string, returning FALSE if not enough memory.
Definition dbus-string.c:1190
_dbus_string_append_byte_as_hex
dbus_bool_t _dbus_string_append_byte_as_hex(DBusString *str, unsigned char byte)
Appends a two-character hex digit to a string, where the hex digit has the value of the given byte.
Definition dbus-string.c:2313
_dbus_string_find_to
dbus_bool_t _dbus_string_find_to(const DBusString *str, int start, int end, const char *substr, int *found)
Finds the given substring in the string, up to a certain position, returning TRUE and filling in the ...
Definition dbus-string.c:1759
_dbus_string_copy_len
dbus_bool_t _dbus_string_copy_len(const DBusString *source, int start, int len, DBusString *dest, int insert_at)
Like _dbus_string_copy(), but can copy a segment from the middle of the source string.
Definition dbus-string.c:1437
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition dbus-types.h:37
DBusAddressEntry
Internals of DBusAddressEntry.
Definition dbus-address.c:49
DBusAddressEntry::method
DBusString method
The address type (unix, tcp, etc.).
Definition dbus-address.c:50
DBusAddressEntry::values
DBusList * values
List of values.
Definition dbus-address.c:53
DBusAddressEntry::keys
DBusList * keys
List of keys.
Definition dbus-address.c:52
DBusError
Object representing an exception.
Definition dbus-errors.h:51
DBusList
A node in a linked list.
Definition dbus-list.h:37
DBusList::data
void * data
Data stored at this element.
Definition dbus-list.h:40
DBusString
Definition dbus-string.h:47