summaryrefslogtreecommitdiff
path: root/dbus/dbus-syntax.c
blob: 7ef659cbc08d88db3892d25f9275554d7caa6177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-syntax.c - utility functions for strings with special syntax
 *
 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
 * Copyright © 2011 Nokia Corporation
 *
 * 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-syntax.h"

#include "dbus-internals.h"
#include "dbus-marshal-validate.h"
#include "dbus-shared.h"

/**
 * @defgroup DBusSyntax Utility functions for strings with special syntax
 * @ingroup  DBus
 * @brief Parsing D-Bus type signatures
 * @{
 */

/**
 * Check an object path for validity. Remember that #NULL can always
 * be passed instead of a DBusError *, if you don't care about having
 * an error name and message.
 *
 * This function is suitable for validating C strings, but is not suitable
 * for validating untrusted data from a network unless the string's length
 * is also checked, since it assumes that the string ends at the first zero
 * byte according to normal C conventions.
 *
 * @param path a potentially invalid object path, which must not be #NULL
 * @param error error return
 * @returns #TRUE if path is valid
 */
dbus_bool_t
dbus_validate_path (const char       *path,
                    DBusError        *error)
{
  DBusString str;
  int len;

  _dbus_return_val_if_fail (path != NULL, FALSE);

  _dbus_string_init_const (&str, path);
  len = _dbus_string_get_length (&str);

  /* In general, it ought to be valid... */
  if (_DBUS_LIKELY (_dbus_validate_path (&str, 0, len)))
    return TRUE;

  /* slow path: string is invalid, find out why */

  if (!_dbus_string_validate_utf8 (&str, 0, len))
    {
      /* don't quote the actual string here, since a DBusError also needs to
       * be valid UTF-8 */
      dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
                      "Object path was not valid UTF-8");
      return FALSE;
    }

  /* FIXME: later, diagnose exactly how it was invalid */
  dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
                  "Object path was not valid: '%s'", path);
  return FALSE;
}

/**
 * Check an interface name for validity. Remember that #NULL can always
 * be passed instead of a DBusError *, if you don't care about having
 * an error name and message.
 *
 * This function is suitable for validating C strings, but is not suitable
 * for validating untrusted data from a network unless the string's length
 * is also checked, since it assumes that the string ends at the first zero
 * byte according to normal C conventions.
 *
 * @param name a potentially invalid interface name, which must not be #NULL
 * @param error error return
 * @returns #TRUE if name is valid
 */
dbus_bool_t
dbus_validate_interface (const char       *name,
                         DBusError        *error)
{
  DBusString str;
  int len;

  _dbus_return_val_if_fail (name != NULL, FALSE);

  _dbus_string_init_const (&str, name);
  len = _dbus_string_get_length (&str);

  /* In general, it ought to be valid... */
  if (_DBUS_LIKELY (_dbus_validate_interface (&str, 0, len)))
    return TRUE;

  /* slow path: string is invalid, find out why */

  if (!_dbus_string_validate_utf8 (&str, 0, len))
    {
      /* don't quote the actual string here, since a DBusError also needs to
       * be valid UTF-8 */
      dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
                      "Interface name was not valid UTF-8");
      return FALSE;
    }

  /* FIXME: later, diagnose exactly how it was invalid */
  dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
                  "Interface name was not valid: '%s'", name);
  return FALSE;
}

/**
 * Check a member (method/signal) name for validity. Remember that #NULL
 * can always be passed instead of a DBusError *, if you don't care about
 * having an error name and message.
 *
 * This function is suitable for validating C strings, but is not suitable
 * for validating untrusted data from a network unless the string's length
 * is also checked, since it assumes that the string ends at the first zero
 * byte according to normal C conventions.
 *
 * @param name a potentially invalid member name, which must not be #NULL
 * @param error error return
 * @returns #TRUE if name is valid
 */
dbus_bool_t
dbus_validate_member (const char       *name,
                      DBusError        *error)
{
  DBusString str;
  int len;

  _dbus_return_val_if_fail (name != NULL, FALSE);

  _dbus_string_init_const (&str, name);
  len = _dbus_string_get_length (&str);

  /* In general, it ought to be valid... */
  if (_DBUS_LIKELY (_dbus_validate_member (&str, 0, len)))
    return TRUE;

  /* slow path: string is invalid, find out why */

  if (!_dbus_string_validate_utf8 (&str, 0, len))
    {
      /* don't quote the actual string here, since a DBusError also needs to
       * be valid UTF-8 */
      dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
                      "Member name was not valid UTF-8");
      return FALSE;
    }

  /* FIXME: later, diagnose exactly how it was invalid */
  dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
                  "Member name was not valid: '%s'", name);
  return FALSE;
}

/**
 * Check an error name for validity. Remember that #NULL
 * can always be passed instead of a DBusError *, if you don't care about
 * having an error name and message.
 *
 * This function is suitable for validating C strings, but is not suitable
 * for validating untrusted data from a network unless the string's length
 * is also checked, since it assumes that the string ends at the first zero
 * byte according to normal C conventions.
 *
 * @param name a potentially invalid error name, which must not be #NULL
 * @param error error return
 * @returns #TRUE if name is valid
 */
dbus_bool_t
dbus_validate_error_name (const char       *name,
                          DBusError        *error)
{
  DBusString str;
  int len;

  _dbus_return_val_if_fail (name != NULL, FALSE);

  _dbus_string_init_const (&str, name);
  len = _dbus_string_get_length (&str);

  /* In general, it ought to be valid... */
  if (_DBUS_LIKELY (_dbus_validate_error_name (&str, 0, len)))
    return TRUE;

  /* slow path: string is invalid, find out why */

  if (!_dbus_string_validate_utf8 (&str, 0, len))
    {
      /* don't quote the actual string here, since a DBusError also needs to
       * be valid UTF-8 */
      dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
                      "Error name was not valid UTF-8");
      return FALSE;
    }

  /* FIXME: later, diagnose exactly how it was invalid */
  dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
                  "Error name was not valid: '%s'", name);
  return FALSE;
}

/**
 * Check a bus name for validity. Remember that #NULL
 * can always be passed instead of a DBusError *, if you don't care about
 * having an error name and message.
 *
 * This function is suitable for validating C strings, but is not suitable
 * for validating untrusted data from a network unless the string's length
 * is also checked, since it assumes that the string ends at the first zero
 * byte according to normal C conventions.
 *
 * @param name a potentially invalid bus name, which must not be #NULL
 * @param error error return
 * @returns #TRUE if name is valid
 */
dbus_bool_t
dbus_validate_bus_name (const char       *name,
                        DBusError        *error)
{
  DBusString str;
  int len;

  _dbus_return_val_if_fail (name != NULL, FALSE);

  _dbus_string_init_const (&str, name);
  len = _dbus_string_get_length (&str);

  /* In general, it ought to be valid... */
  if (_DBUS_LIKELY (_dbus_validate_bus_name (&str, 0, len)))
    return TRUE;

  /* slow path: string is invalid, find out why */

  if (!_dbus_string_validate_utf8 (&str, 0, len))
    {
      /* don't quote the actual string here, since a DBusError also needs to
       * be valid UTF-8 */
      dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
                      "Bus name was not valid UTF-8");
      return FALSE;
    }

  /* FIXME: later, diagnose exactly how it was invalid */
  dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
                  "Bus name was not valid: '%s'", name);
  return FALSE;
}

/**
 * Check a string for validity. Strings on D-Bus must be valid UTF-8.
 * Remember that #NULL can always be passed instead of a DBusError *,
 * if you don't care about having an error name and message.
 *
 * This function is suitable for validating C strings, but is not suitable
 * for validating untrusted data from a network unless the string's length
 * is also checked, since it assumes that the string ends at the first zero
 * byte according to normal C conventions.
 *
 * @param alleged_utf8 a string to be checked, which must not be #NULL
 * @param error error return
 * @returns #TRUE if alleged_utf8 is valid UTF-8
 */
dbus_bool_t
dbus_validate_utf8 (const char       *alleged_utf8,
                    DBusError        *error)
{
  DBusString str;

  _dbus_return_val_if_fail (alleged_utf8 != NULL, FALSE);

  _dbus_string_init_const (&str, alleged_utf8);

  if (_DBUS_LIKELY (_dbus_string_validate_utf8 (&str, 0,
                                                _dbus_string_get_length (&str))))
    return TRUE;

  /* don't quote the actual string here, since a DBusError also needs to
   * be valid UTF-8 */
  dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
                  "String was not valid UTF-8");
  return FALSE;
}

/** @} */ /* end of group */