summaryrefslogtreecommitdiff
path: root/dbus_bindings/validation.c
blob: 5a21dc2e71ee4db23b7336389549e7fa2c62c2e8 (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
/* Implementation of various validation functions for use in dbus-python.
 *
 * Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "dbus_bindings-internal.h"

dbus_bool_t
dbus_py_validate_bus_name(const char *name,
                          dbus_bool_t may_be_unique,
                          dbus_bool_t may_be_not_unique)
{
    dbus_bool_t dot = FALSE;
    dbus_bool_t unique;
    char last;
    const char *ptr;

    if (name[0] == '\0') {
        PyErr_SetString(PyExc_ValueError, "Invalid bus name: "
                        "may not be empty");
        return FALSE;
    }
    unique = (name[0] == ':');
    if (unique && !may_be_unique) {
        PyErr_Format(PyExc_ValueError, "Invalid well-known bus name '%s':"
                     "only unique names may start with ':'", name);
        return FALSE;
    }
    if (!unique && !may_be_not_unique) {
        PyErr_Format(PyExc_ValueError, "Invalid unique bus name '%s': "
                     "unique names must start with ':'", name);
        return FALSE;
    }
    if (strlen(name) > 255) {
        PyErr_Format(PyExc_ValueError, "Invalid bus name '%s': "
                     "too long (> 255 characters)", name);
        return FALSE;
    }
    last = '\0';
    for (ptr = name + (unique ? 1 : 0); *ptr; ptr++) {
        if (*ptr == '.') {
            dot = TRUE;
            if (last == '.') {
                PyErr_Format(PyExc_ValueError, "Invalid bus name '%s': "
                             "contains substring '..'", name);
                return FALSE;
            }
            else if (last == '\0') {
                PyErr_Format(PyExc_ValueError, "Invalid bus name '%s': "
                             "must not start with '.'", name);
                return FALSE;
            }
        }
        else if (*ptr >= '0' && *ptr <= '9') {
            if (!unique) {
                if (last == '.') {
                    PyErr_Format(PyExc_ValueError, "Invalid bus name '%s': "
                                 "a digit may not follow '.' except in a "
                                 "unique name starting with ':'", name);
                    return FALSE;
                }
                else if (last == '\0') {
                    PyErr_Format(PyExc_ValueError, "Invalid bus name '%s': "
                             "must not start with a digit", name);
                    return FALSE;
                }
            }
        }
        else if ((*ptr < 'a' || *ptr > 'z') &&
                 (*ptr < 'A' || *ptr > 'Z') && *ptr != '_' && *ptr != '-') {
            PyErr_Format(PyExc_ValueError, "Invalid bus name '%s': "
                         "contains invalid character '%c'", name, *ptr);
            return FALSE;
        }
        last = *ptr;
    }
    if (last == '.') {
        PyErr_Format(PyExc_ValueError, "Invalid bus name '%s': must "
                        "not end with '.'", name);
        return FALSE;
    }
    if (!dot) {
        PyErr_Format(PyExc_ValueError, "Invalid bus name '%s': must "
                        "contain '.'", name);
        return FALSE;
    }
    return TRUE;
}

dbus_bool_t
dbus_py_validate_member_name(const char *name)
{
    const char *ptr;

    if (name[0] == '\0') {
        PyErr_SetString(PyExc_ValueError, "Invalid member name: may not "
                        "be empty");
        return FALSE;
    }
    if (strlen(name) > 255) {
        PyErr_Format(PyExc_ValueError, "Invalid member name '%s': "
                     "too long (> 255 characters)", name);
        return FALSE;
    }
    for (ptr = name; *ptr; ptr++) {
        if (*ptr >= '0' && *ptr <= '9') {
            if (ptr == name) {
                PyErr_Format(PyExc_ValueError, "Invalid member name '%s': "
                             "must not start with a digit", name);
                return FALSE;
            }
        }
        else if ((*ptr < 'a' || *ptr > 'z') &&
                 (*ptr < 'A' || *ptr > 'Z') && *ptr != '_') {
            PyErr_Format(PyExc_ValueError, "Invalid member name '%s': "
                         "contains invalid character '%c'", name, *ptr);
            return FALSE;
        }
    }
    return TRUE;
}

dbus_bool_t
dbus_py_validate_interface_name(const char *name)
{
    dbus_bool_t dot = FALSE;
    char last;
    const char *ptr;

    if (name[0] == '\0') {
        PyErr_SetString(PyExc_ValueError, "Invalid interface or error name: "
                        "may not be empty");
        return FALSE;
    }
    if (strlen(name) > 255) {
        PyErr_Format(PyExc_ValueError, "Invalid interface or error name '%s': "
                     "too long (> 255 characters)", name);
        return FALSE;
    }
    last = '\0';
    for (ptr = name; *ptr; ptr++) {
        if (*ptr == '.') {
            dot = TRUE;
            if (last == '.') {
                PyErr_Format(PyExc_ValueError, "Invalid interface or "
                             "error name '%s': contains substring '..'", name);
                return FALSE;
            }
            else if (last == '\0') {
                PyErr_Format(PyExc_ValueError, "Invalid interface or error "
                             "name '%s': must not start with '.'", name);
                return FALSE;
            }
        }
        else if (*ptr >= '0' && *ptr <= '9') {
            if (last == '.') {
                PyErr_Format(PyExc_ValueError, "Invalid interface or error "
                             "name '%s': a digit may not follow '.'", name);
                return FALSE;
            }
            else if (last == '\0') {
                PyErr_Format(PyExc_ValueError, "Invalid interface or error "
                             "name '%s': must not start with a digit", name);
                return FALSE;
            }
        }
        else if ((*ptr < 'a' || *ptr > 'z') &&
                 (*ptr < 'A' || *ptr > 'Z') && *ptr != '_') {
            PyErr_Format(PyExc_ValueError, "Invalid interface or error "
                         "name '%s': contains invalid character '%c'",
                         name, *ptr);
            return FALSE;
        }
        last = *ptr;
    }
    if (last == '.') {
        PyErr_Format(PyExc_ValueError, "Invalid interface or error name "
                     "'%s': must not end with '.'", name);
        return FALSE;
    }
    if (!dot) {
        PyErr_Format(PyExc_ValueError, "Invalid interface or error name "
                     "'%s': must contain '.'", name);
        return FALSE;
    }
    return TRUE;
}


dbus_bool_t
dbus_py_validate_object_path(const char *path)
{
    const char *ptr;

    if (path[0] != '/') {
        PyErr_Format(PyExc_ValueError, "Invalid object path '%s': does not "
                     "start with '/'", path);
        return FALSE;
    }
    if (path[1] == '\0') return TRUE;
    for (ptr = path + 1; *ptr; ptr++) {
        if (*ptr == '/') {
            if (ptr[-1] == '/') {
                PyErr_Format(PyExc_ValueError, "Invalid object path '%s': "
                             "contains substring '//'", path);
                return FALSE;
            }
        }
        else if ((*ptr < 'a' || *ptr > 'z') &&
                 (*ptr < 'A' || *ptr > 'Z') &&
                 (*ptr < '0' || *ptr > '9') && *ptr != '_') {
            PyErr_Format(PyExc_ValueError, "Invalid object path '%s': "
                         "contains invalid character '%c'", path, *ptr);
            return FALSE;
        }
    }
    if (ptr[-1] == '/') {
        PyErr_Format(PyExc_ValueError, "Invalid object path '%s': ends "
                     "with '/' and is not just '/'", path);
        return FALSE;
    }
    return TRUE;
}

/* vim:set ft=c cino< sw=4 sts=4 et: */