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
|
/*
Copyright (c) 2009-2018 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
http://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include <assert.h>
#include <string.h>
#ifdef WIN32
# include <winsock2.h>
# include <aclapi.h>
# include <io.h>
# include <lmcons.h>
#else
# include <sys/stat.h>
#endif
#ifdef WITH_BROKER
#include "mosquitto_broker_internal.h"
#endif
#include "mosquitto.h"
#include "memory_mosq.h"
#include "net_mosq.h"
#include "send_mosq.h"
#include "time_mosq.h"
#include "tls_mosq.h"
#include "util_mosq.h"
/* Check that a topic used for publishing is valid.
* Search for + or # in a topic. Return MOSQ_ERR_INVAL if found.
* Also returns MOSQ_ERR_INVAL if the topic string is too long.
* Returns MOSQ_ERR_SUCCESS if everything is fine.
*/
int mosquitto_pub_topic_check(const char *str)
{
int len = 0;
while(str && str[0]){
if(str[0] == '+' || str[0] == '#'){
return MOSQ_ERR_INVAL;
}
len++;
str = &str[1];
}
if(len > 65535) return MOSQ_ERR_INVAL;
return MOSQ_ERR_SUCCESS;
}
int mosquitto_pub_topic_check2(const char *str, size_t len)
{
size_t i;
if(len > 65535) return MOSQ_ERR_INVAL;
for(i=0; i<len; i++){
if(str[i] == '+' || str[i] == '#'){
return MOSQ_ERR_INVAL;
}
}
return MOSQ_ERR_SUCCESS;
}
/* Check that a topic used for subscriptions is valid.
* Search for + or # in a topic, check they aren't in invalid positions such as
* foo/#/bar, foo/+bar or foo/bar#.
* Return MOSQ_ERR_INVAL if invalid position found.
* Also returns MOSQ_ERR_INVAL if the topic string is too long.
* Returns MOSQ_ERR_SUCCESS if everything is fine.
*/
int mosquitto_sub_topic_check(const char *str)
{
char c = '\0';
int len = 0;
while(str && str[0]){
if(str[0] == '+'){
if((c != '\0' && c != '/') || (str[1] != '\0' && str[1] != '/')){
return MOSQ_ERR_INVAL;
}
}else if(str[0] == '#'){
if((c != '\0' && c != '/') || str[1] != '\0'){
return MOSQ_ERR_INVAL;
}
}
len++;
c = str[0];
str = &str[1];
}
if(len > 65535) return MOSQ_ERR_INVAL;
return MOSQ_ERR_SUCCESS;
}
int mosquitto_sub_topic_check2(const char *str, size_t len)
{
char c = '\0';
size_t i;
if(len > 65535) return MOSQ_ERR_INVAL;
for(i=0; i<len; i++){
if(str[i] == '+'){
if((c != '\0' && c != '/') || (i<len-1 && str[i+1] != '/')){
return MOSQ_ERR_INVAL;
}
}else if(str[i] == '#'){
if((c != '\0' && c != '/') || i<len-1){
return MOSQ_ERR_INVAL;
}
}
c = str[i];
}
return MOSQ_ERR_SUCCESS;
}
int mosquitto_topic_matches_sub(const char *sub, const char *topic, bool *result)
{
return mosquitto_topic_matches_sub2(sub, 0, topic, 0, result);
}
/* Does a topic match a subscription? */
int mosquitto_topic_matches_sub2(const char *sub, size_t sublen, const char *topic, size_t topiclen, bool *result)
{
size_t spos;
UNUSED(sublen);
UNUSED(topiclen);
if(!result) return MOSQ_ERR_INVAL;
*result = false;
if(!sub || !topic || sub[0] == 0 || topic[0] == 0){
return MOSQ_ERR_INVAL;
}
if((sub[0] == '$' && topic[0] != '$')
|| (topic[0] == '$' && sub[0] != '$')){
return MOSQ_ERR_SUCCESS;
}
spos = 0;
while(sub[0] != 0){
if(topic[0] == '+' || topic[0] == '#'){
return MOSQ_ERR_INVAL;
}
if(sub[0] != topic[0] || topic[0] == 0){ /* Check for wildcard matches */
if(sub[0] == '+'){
/* Check for bad "+foo" or "a/+foo" subscription */
if(spos > 0 && sub[-1] != '/'){
return MOSQ_ERR_INVAL;
}
/* Check for bad "foo+" or "foo+/a" subscription */
if(sub[1] != 0 && sub[1] != '/'){
return MOSQ_ERR_INVAL;
}
spos++;
sub++;
while(topic[0] != 0 && topic[0] != '/'){
topic++;
}
if(topic[0] == 0 && sub[0] == 0){
*result = true;
return MOSQ_ERR_SUCCESS;
}
}else if(sub[0] == '#'){
/* Check for bad "foo#" subscription */
if(spos > 0 && sub[-1] != '/'){
return MOSQ_ERR_INVAL;
}
/* Check for # not the final character of the sub, e.g. "#foo" */
if(sub[1] != 0){
return MOSQ_ERR_INVAL;
}else{
*result = true;
return MOSQ_ERR_SUCCESS;
}
}else{
/* Check for e.g. foo/bar matching foo/+/# */
if(topic[0] == 0
&& spos > 0
&& sub[-1] == '+'
&& sub[0] == '/'
&& sub[1] == '#')
{
*result = true;
return MOSQ_ERR_SUCCESS;
}
/* There is no match at this point, but is the sub invalid? */
while(sub[0] != 0){
if(sub[0] == '#' && sub[1] != 0){
return MOSQ_ERR_INVAL;
}
spos++;
sub++;
}
/* Valid input, but no match */
return MOSQ_ERR_SUCCESS;
}
}else{
/* sub[spos] == topic[tpos] */
if(topic[1] == 0){
/* Check for e.g. foo matching foo/# */
if(sub[1] == '/'
&& sub[2] == '#'
&& sub[3] == 0){
*result = true;
return MOSQ_ERR_SUCCESS;
}
}
spos++;
sub++;
topic++;
if(sub[0] == 0 && topic[0] == 0){
*result = true;
return MOSQ_ERR_SUCCESS;
}else if(topic[0] == 0 && sub[0] == '+' && sub[1] == 0){
if(spos > 0 && sub[-1] != '/'){
return MOSQ_ERR_INVAL;
}
spos++;
sub++;
*result = true;
return MOSQ_ERR_SUCCESS;
}
}
}
if((topic[0] != 0 || sub[0] != 0)){
*result = false;
}
return MOSQ_ERR_SUCCESS;
}
|