-
Notifications
You must be signed in to change notification settings - Fork 0
/
socks_mosq.c
470 lines (418 loc) · 13.2 KB
/
socks_mosq.c
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
Copyright (c) 2014-2020 Roger Light <[email protected]>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include <errno.h>
#include <string.h>
#include <limits.h>
#ifdef WIN32
# include <ws2tcpip.h>
#elif defined(__QNX__)
# include <sys/socket.h>
# include <arpa/inet.h>
# include <netinet/in.h>
#else
# include <arpa/inet.h>
#endif
#if defined(__FreeBSD__) || defined(__OpenBSD__)
# include <sys/socket.h>
# include <netinet/in.h>
#endif
#include "mosquitto_internal.h"
#include "memory_mosq.h"
#include "net_mosq.h"
#include "packet_mosq.h"
#include "send_mosq.h"
#include "socks_mosq.h"
#include "util_mosq.h"
#define SOCKS_AUTH_NONE 0x00U
#define SOCKS_AUTH_GSS 0x01U
#define SOCKS_AUTH_USERPASS 0x02U
#define SOCKS_AUTH_NO_ACCEPTABLE 0xFFU
#define SOCKS_ATYPE_IP_V4 1U /* four bytes */
#define SOCKS_ATYPE_DOMAINNAME 3U /* one byte length, followed by fqdn no null, 256 max chars */
#define SOCKS_ATYPE_IP_V6 4U /* 16 bytes */
#define SOCKS_REPLY_SUCCEEDED 0x00U
#define SOCKS_REPLY_GENERAL_FAILURE 0x01U
#define SOCKS_REPLY_CONNECTION_NOT_ALLOWED 0x02U
#define SOCKS_REPLY_NETWORK_UNREACHABLE 0x03U
#define SOCKS_REPLY_HOST_UNREACHABLE 0x04U
#define SOCKS_REPLY_CONNECTION_REFUSED 0x05U
#define SOCKS_REPLY_TTL_EXPIRED 0x06U
#define SOCKS_REPLY_COMMAND_NOT_SUPPORTED 0x07U
#define SOCKS_REPLY_ADDRESS_TYPE_NOT_SUPPORTED 0x08U
int mosquitto_socks5_set(struct mosquitto *mosq, const char *host, int port, const char *username, const char *password)
{
#ifdef WITH_SOCKS
if(!mosq) return MOSQ_ERR_INVAL;
if(!host || strlen(host) > 256) return MOSQ_ERR_INVAL;
if(port < 1 || port > UINT16_MAX) return MOSQ_ERR_INVAL;
mosquitto__free(mosq->socks5_host);
mosq->socks5_host = NULL;
mosq->socks5_host = mosquitto__strdup(host);
if(!mosq->socks5_host){
return MOSQ_ERR_NOMEM;
}
mosq->socks5_port = (uint16_t)port;
mosquitto__free(mosq->socks5_username);
mosq->socks5_username = NULL;
mosquitto__free(mosq->socks5_password);
mosq->socks5_password = NULL;
if(username){
if(strlen(username) > UINT8_MAX){
return MOSQ_ERR_INVAL;
}
mosq->socks5_username = mosquitto__strdup(username);
if(!mosq->socks5_username){
return MOSQ_ERR_NOMEM;
}
if(password){
if(strlen(password) > UINT8_MAX){
return MOSQ_ERR_INVAL;
}
mosq->socks5_password = mosquitto__strdup(password);
if(!mosq->socks5_password){
mosquitto__free(mosq->socks5_username);
return MOSQ_ERR_NOMEM;
}
}
}
return MOSQ_ERR_SUCCESS;
#else
UNUSED(mosq);
UNUSED(host);
UNUSED(port);
UNUSED(username);
UNUSED(password);
return MOSQ_ERR_NOT_SUPPORTED;
#endif
}
#ifdef WITH_SOCKS
int socks5__send(struct mosquitto *mosq)
{
struct mosquitto__packet *packet;
size_t slen;
uint8_t ulen, plen;
struct in_addr addr_ipv4;
struct in6_addr addr_ipv6;
int ipv4_pton_result;
int ipv6_pton_result;
enum mosquitto_client_state state;
state = mosquitto__get_state(mosq);
if(state == mosq_cs_socks5_new){
packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet));
if(!packet) return MOSQ_ERR_NOMEM;
if(mosq->socks5_username){
packet->packet_length = 4;
}else{
packet->packet_length = 3;
}
packet->payload = mosquitto__malloc(sizeof(uint8_t)*packet->packet_length);
packet->payload[0] = 0x05;
if(mosq->socks5_username){
packet->payload[1] = 2;
packet->payload[2] = SOCKS_AUTH_NONE;
packet->payload[3] = SOCKS_AUTH_USERPASS;
}else{
packet->payload[1] = 1;
packet->payload[2] = SOCKS_AUTH_NONE;
}
mosquitto__set_state(mosq, mosq_cs_socks5_start);
mosq->in_packet.pos = 0;
mosq->in_packet.packet_length = 2;
mosq->in_packet.to_process = 2;
mosq->in_packet.payload = mosquitto__malloc(sizeof(uint8_t)*2);
if(!mosq->in_packet.payload){
mosquitto__free(packet->payload);
mosquitto__free(packet);
return MOSQ_ERR_NOMEM;
}
return packet__queue(mosq, packet);
}else if(state == mosq_cs_socks5_auth_ok){
packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet));
if(!packet) return MOSQ_ERR_NOMEM;
ipv4_pton_result = inet_pton(AF_INET, mosq->host, &addr_ipv4);
ipv6_pton_result = inet_pton(AF_INET6, mosq->host, &addr_ipv6);
if(ipv4_pton_result == 1){
packet->packet_length = 10;
packet->payload = mosquitto__malloc(sizeof(uint8_t)*packet->packet_length);
if(!packet->payload){
mosquitto__free(packet);
return MOSQ_ERR_NOMEM;
}
packet->payload[3] = SOCKS_ATYPE_IP_V4;
memcpy(&(packet->payload[4]), (const void*)&addr_ipv4, 4);
packet->payload[4+4] = MOSQ_MSB(mosq->port);
packet->payload[4+4+1] = MOSQ_LSB(mosq->port);
}else if(ipv6_pton_result == 1){
packet->packet_length = 22;
packet->payload = mosquitto__malloc(sizeof(uint8_t)*packet->packet_length);
if(!packet->payload){
mosquitto__free(packet);
return MOSQ_ERR_NOMEM;
}
packet->payload[3] = SOCKS_ATYPE_IP_V6;
memcpy(&(packet->payload[4]), (const void*)&addr_ipv6, 16);
packet->payload[4+16] = MOSQ_MSB(mosq->port);
packet->payload[4+16+1] = MOSQ_LSB(mosq->port);
}else{
slen = strlen(mosq->host);
if(slen > UCHAR_MAX){
mosquitto__free(packet);
return MOSQ_ERR_NOMEM;
}
packet->packet_length = 7U + (uint32_t)slen;
packet->payload = mosquitto__malloc(sizeof(uint8_t)*packet->packet_length);
if(!packet->payload){
mosquitto__free(packet);
return MOSQ_ERR_NOMEM;
}
packet->payload[3] = SOCKS_ATYPE_DOMAINNAME;
packet->payload[4] = (uint8_t)slen;
memcpy(&(packet->payload[5]), mosq->host, slen);
packet->payload[5+slen] = MOSQ_MSB(mosq->port);
packet->payload[6+slen] = MOSQ_LSB(mosq->port);
}
packet->payload[0] = 0x05;
packet->payload[1] = 0x01;
packet->payload[2] = 0x00;
mosquitto__set_state(mosq, mosq_cs_socks5_request);
mosq->in_packet.pos = 0;
mosq->in_packet.packet_length = 5;
mosq->in_packet.to_process = 5;
mosq->in_packet.payload = mosquitto__malloc(sizeof(uint8_t)*5);
if(!mosq->in_packet.payload){
mosquitto__free(packet->payload);
mosquitto__free(packet);
return MOSQ_ERR_NOMEM;
}
return packet__queue(mosq, packet);
}else if(state == mosq_cs_socks5_send_userpass){
packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet));
if(!packet) return MOSQ_ERR_NOMEM;
ulen = (uint8_t)strlen(mosq->socks5_username);
plen = (uint8_t)strlen(mosq->socks5_password);
packet->packet_length = 3U + ulen + plen;
packet->payload = mosquitto__malloc(sizeof(uint8_t)*packet->packet_length);
packet->payload[0] = 0x01;
packet->payload[1] = ulen;
memcpy(&(packet->payload[2]), mosq->socks5_username, ulen);
packet->payload[2+ulen] = plen;
memcpy(&(packet->payload[3+ulen]), mosq->socks5_password, plen);
mosquitto__set_state(mosq, mosq_cs_socks5_userpass_reply);
mosq->in_packet.pos = 0;
mosq->in_packet.packet_length = 2;
mosq->in_packet.to_process = 2;
mosq->in_packet.payload = mosquitto__malloc(sizeof(uint8_t)*2);
if(!mosq->in_packet.payload){
mosquitto__free(packet->payload);
mosquitto__free(packet);
return MOSQ_ERR_NOMEM;
}
return packet__queue(mosq, packet);
}
return MOSQ_ERR_SUCCESS;
}
int socks5__read(struct mosquitto *mosq)
{
ssize_t len;
uint8_t *payload;
uint8_t i;
enum mosquitto_client_state state;
state = mosquitto__get_state(mosq);
if(state == mosq_cs_socks5_start){
while(mosq->in_packet.to_process > 0){
len = net__read(mosq, &(mosq->in_packet.payload[mosq->in_packet.pos]), mosq->in_packet.to_process);
if(len > 0){
mosq->in_packet.pos += (uint32_t)len;
mosq->in_packet.to_process -= (uint32_t)len;
}else{
#ifdef WIN32
errno = WSAGetLastError();
#endif
if(errno == EAGAIN || errno == COMPAT_EWOULDBLOCK){
return MOSQ_ERR_SUCCESS;
}else{
packet__cleanup(&mosq->in_packet);
switch(errno){
case 0:
return MOSQ_ERR_PROXY;
case COMPAT_ECONNRESET:
return MOSQ_ERR_CONN_LOST;
default:
return MOSQ_ERR_ERRNO;
}
}
}
}
if(mosq->in_packet.payload[0] != 5){
packet__cleanup(&mosq->in_packet);
return MOSQ_ERR_PROXY;
}
switch(mosq->in_packet.payload[1]){
case SOCKS_AUTH_NONE:
packet__cleanup(&mosq->in_packet);
mosquitto__set_state(mosq, mosq_cs_socks5_auth_ok);
return socks5__send(mosq);
case SOCKS_AUTH_USERPASS:
packet__cleanup(&mosq->in_packet);
mosquitto__set_state(mosq, mosq_cs_socks5_send_userpass);
return socks5__send(mosq);
default:
packet__cleanup(&mosq->in_packet);
return MOSQ_ERR_AUTH;
}
}else if(state == mosq_cs_socks5_userpass_reply){
while(mosq->in_packet.to_process > 0){
len = net__read(mosq, &(mosq->in_packet.payload[mosq->in_packet.pos]), mosq->in_packet.to_process);
if(len > 0){
mosq->in_packet.pos += (uint32_t)len;
mosq->in_packet.to_process -= (uint32_t)len;
}else{
#ifdef WIN32
errno = WSAGetLastError();
#endif
if(errno == EAGAIN || errno == COMPAT_EWOULDBLOCK){
return MOSQ_ERR_SUCCESS;
}else{
packet__cleanup(&mosq->in_packet);
switch(errno){
case 0:
return MOSQ_ERR_PROXY;
case COMPAT_ECONNRESET:
return MOSQ_ERR_CONN_LOST;
default:
return MOSQ_ERR_ERRNO;
}
}
}
}
if(mosq->in_packet.payload[0] != 1){
packet__cleanup(&mosq->in_packet);
return MOSQ_ERR_PROXY;
}
if(mosq->in_packet.payload[1] == 0){
packet__cleanup(&mosq->in_packet);
mosquitto__set_state(mosq, mosq_cs_socks5_auth_ok);
return socks5__send(mosq);
}else{
i = mosq->in_packet.payload[1];
packet__cleanup(&mosq->in_packet);
switch(i){
case SOCKS_REPLY_CONNECTION_NOT_ALLOWED:
return MOSQ_ERR_AUTH;
case SOCKS_REPLY_NETWORK_UNREACHABLE:
case SOCKS_REPLY_HOST_UNREACHABLE:
case SOCKS_REPLY_CONNECTION_REFUSED:
return MOSQ_ERR_NO_CONN;
case SOCKS_REPLY_GENERAL_FAILURE:
case SOCKS_REPLY_TTL_EXPIRED:
case SOCKS_REPLY_COMMAND_NOT_SUPPORTED:
case SOCKS_REPLY_ADDRESS_TYPE_NOT_SUPPORTED:
return MOSQ_ERR_PROXY;
default:
return MOSQ_ERR_INVAL;
}
return MOSQ_ERR_PROXY;
}
}else if(state == mosq_cs_socks5_request){
while(mosq->in_packet.to_process > 0){
len = net__read(mosq, &(mosq->in_packet.payload[mosq->in_packet.pos]), mosq->in_packet.to_process);
if(len > 0){
mosq->in_packet.pos += (uint32_t)len;
mosq->in_packet.to_process -= (uint32_t)len;
}else{
#ifdef WIN32
errno = WSAGetLastError();
#endif
if(errno == EAGAIN || errno == COMPAT_EWOULDBLOCK){
return MOSQ_ERR_SUCCESS;
}else{
packet__cleanup(&mosq->in_packet);
switch(errno){
case 0:
return MOSQ_ERR_PROXY;
case COMPAT_ECONNRESET:
return MOSQ_ERR_CONN_LOST;
default:
return MOSQ_ERR_ERRNO;
}
}
}
}
if(mosq->in_packet.packet_length == 5){
/* First part of the packet has been received, we now know what else to expect. */
if(mosq->in_packet.payload[3] == SOCKS_ATYPE_IP_V4){
mosq->in_packet.to_process += 4+2-1; /* 4 bytes IPv4, 2 bytes port, -1 byte because we've already read the first byte */
mosq->in_packet.packet_length += 4+2-1;
}else if(mosq->in_packet.payload[3] == SOCKS_ATYPE_IP_V6){
mosq->in_packet.to_process += 16+2-1; /* 16 bytes IPv6, 2 bytes port, -1 byte because we've already read the first byte */
mosq->in_packet.packet_length += 16+2-1;
}else if(mosq->in_packet.payload[3] == SOCKS_ATYPE_DOMAINNAME){
if(mosq->in_packet.payload[4] > 0){
mosq->in_packet.to_process += mosq->in_packet.payload[4];
mosq->in_packet.packet_length += mosq->in_packet.payload[4];
}
}else{
packet__cleanup(&mosq->in_packet);
return MOSQ_ERR_PROTOCOL;
}
payload = mosquitto__realloc(mosq->in_packet.payload, mosq->in_packet.packet_length);
if(payload){
mosq->in_packet.payload = payload;
}else{
packet__cleanup(&mosq->in_packet);
return MOSQ_ERR_NOMEM;
}
return MOSQ_ERR_SUCCESS;
}
/* Entire packet is now read. */
if(mosq->in_packet.payload[0] != 5){
packet__cleanup(&mosq->in_packet);
return MOSQ_ERR_PROXY;
}
if(mosq->in_packet.payload[1] == 0){
/* Auth passed */
packet__cleanup(&mosq->in_packet);
mosquitto__set_state(mosq, mosq_cs_new);
if(mosq->socks5_host){
int rc = net__socket_connect_step3(mosq, mosq->host);
if(rc) return rc;
}
return send__connect(mosq, mosq->keepalive, mosq->clean_start, NULL);
}else{
i = mosq->in_packet.payload[1];
packet__cleanup(&mosq->in_packet);
mosquitto__set_state(mosq, mosq_cs_socks5_new);
switch(i){
case SOCKS_REPLY_CONNECTION_NOT_ALLOWED:
return MOSQ_ERR_AUTH;
case SOCKS_REPLY_NETWORK_UNREACHABLE:
case SOCKS_REPLY_HOST_UNREACHABLE:
case SOCKS_REPLY_CONNECTION_REFUSED:
return MOSQ_ERR_NO_CONN;
case SOCKS_REPLY_GENERAL_FAILURE:
case SOCKS_REPLY_TTL_EXPIRED:
case SOCKS_REPLY_COMMAND_NOT_SUPPORTED:
case SOCKS_REPLY_ADDRESS_TYPE_NOT_SUPPORTED:
return MOSQ_ERR_PROXY;
default:
return MOSQ_ERR_INVAL;
}
}
}else{
return packet__read(mosq);
}
return MOSQ_ERR_SUCCESS;
}
#endif