Skip to content

Commit f1db404

Browse files
committed
authoritative-dns-server
1 parent a5b8968 commit f1db404

7 files changed

Lines changed: 484 additions & 34 deletions

File tree

include/libwebsockets/lws-auth-dns.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,38 @@
2424

2525
#if defined(LWS_WITH_AUTHORITATIVE_DNS)
2626

27+
struct auth_dns_rr {
28+
lws_dll2_t list;
29+
30+
char *rdata;
31+
size_t rdata_len;
32+
33+
uint8_t *wire_rdata;
34+
size_t wire_rdata_len;
35+
};
36+
37+
struct auth_dns_rrset {
38+
lws_dll2_t list;
39+
lws_dll2_owner_t rr_list;
40+
41+
char *name;
42+
uint32_t ttl;
43+
uint16_t class_;
44+
uint16_t type;
45+
};
46+
47+
struct auth_dns_zone {
48+
lws_dll2_owner_t rrset_list;
49+
char default_ttl[16];
50+
char origin[256];
51+
};
52+
53+
LWS_VISIBLE LWS_EXTERN int
54+
lws_auth_dns_parse_zone_buf(const char *buf, size_t len, struct auth_dns_zone *zone);
55+
56+
LWS_VISIBLE LWS_EXTERN void
57+
lws_auth_dns_free_zone(struct auth_dns_zone *z);
58+
2759
struct lws_auth_dns_sign_info {
2860
const char *input_filepath;
2961
const char *output_filepath;

lib/system/auth-dns/auth-dns.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,14 @@ lws_auth_dns_parse_zone_buf(const char *buf, size_t len, struct auth_dns_zone *z
8282

8383
char line_accum[4096];
8484
size_t lptr = 0;
85+
int loop_cycles = 0;
8586

8687
while (p <= end) {
88+
if (++loop_cycles > 5000000) {
89+
lwsl_err("auth-dns: parsing exceeded maximum length\n");
90+
break;
91+
}
92+
8793
if (p < end && *p == '(' && !in_comment)
8894
in_parens = 1;
8995
else if (p < end && *p == ')' && !in_comment)
@@ -111,9 +117,10 @@ lws_auth_dns_parse_zone_buf(const char *buf, size_t len, struct auth_dns_zone *z
111117
lws_tokenize_init(&ts, line_accum, LWS_TOKENIZE_F_HASH_COMMENT | LWS_TOKENIZE_F_DOT_NONTERM | LWS_TOKENIZE_F_NO_FLOATS | LWS_TOKENIZE_F_MINUS_NONTERM | LWS_TOKENIZE_F_SLASH_NONTERM | LWS_TOKENIZE_F_COLON_NONTERM | LWS_TOKENIZE_F_EQUALS_NONTERM | LWS_TOKENIZE_F_PLUS_NONTERM);
112118
ts.len = lptr;
113119

120+
int max_tokens = 0;
114121
do {
115122
e = lws_tokenize(&ts);
116-
if (e == LWS_TOKZE_ENDED)
123+
if (e == LWS_TOKZE_ENDED || ++max_tokens > 256)
117124
break;
118125

119126
if (e == LWS_TOKZE_TOKEN || e == LWS_TOKZE_QUOTED_STRING || e == LWS_TOKZE_INTEGER) {

lib/system/auth-dns/private-lib-system-auth-dns.h

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,6 @@
2727

2828
#include "private-lib-core.h"
2929

30-
struct auth_dns_rr {
31-
lws_dll2_t list;
32-
33-
char *rdata; // unparsed or raw payload from zone line
34-
size_t rdata_len;
35-
36-
uint8_t *wire_rdata; // canonical wire format
37-
size_t wire_rdata_len;
38-
};
39-
40-
struct auth_dns_rrset {
41-
lws_dll2_t list;
42-
lws_dll2_owner_t rr_list; // list of auth_dns_rr
43-
44-
char *name;
45-
uint32_t ttl;
46-
uint16_t class_; // e.g. 1 for IN
47-
uint16_t type; // e.g. 1 for A, 2 for NS, etc
48-
};
49-
50-
struct auth_dns_zone {
51-
lws_dll2_owner_t rrset_list;
52-
char default_ttl[16];
53-
char origin[128];
54-
};
55-
5630
int
5731
lws_auth_dns_rdata_to_wire(struct auth_dns_zone *z, struct auth_dns_rr *rr, uint16_t type);
5832

lib/system/auth-dns/sign.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ name_to_wire(const char *name, const char *origin, uint8_t *wire, size_t *wire_l
4242
lws_strncpy(f, name, sizeof(f));
4343
}
4444

45+
int cycles = 0;
4546
p = f;
4647
while (*p) {
48+
if (++cycles > 128)
49+
return 1;
4750
const char *dot = strchr(p, '.');
4851
if (!dot)
4952
l = strlen(p);
@@ -98,9 +101,10 @@ lws_auth_dns_rdata_to_wire(struct auth_dns_zone *z, struct auth_dns_rr *rr, uint
98101
lws_tokenize_init(&ts, rr->rdata, LWS_TOKENIZE_F_HASH_COMMENT | LWS_TOKENIZE_F_DOT_NONTERM | LWS_TOKENIZE_F_NO_FLOATS | LWS_TOKENIZE_F_MINUS_NONTERM | LWS_TOKENIZE_F_SLASH_NONTERM | LWS_TOKENIZE_F_COLON_NONTERM | LWS_TOKENIZE_F_EQUALS_NONTERM | LWS_TOKENIZE_F_PLUS_NONTERM);
99102
ts.len = strlen(rr->rdata);
100103

104+
int max_tokens = 0;
101105
do {
102106
e = lws_tokenize(&ts);
103-
if (e == LWS_TOKZE_ENDED)
107+
if (e == LWS_TOKZE_ENDED || ++max_tokens > 256)
104108
break;
105109

106110
if (e == LWS_TOKZE_TOKEN || e == LWS_TOKZE_QUOTED_STRING || e == LWS_TOKZE_INTEGER) {
@@ -155,9 +159,11 @@ lws_auth_dns_rdata_to_wire(struct auth_dns_zone *z, struct auth_dns_rr *rr, uint
155159
}
156160
} else if (type == 16) { // TXT
157161
/* TXT strings are grouped as 1-byte length prefix + string payload */
158-
for (int i = 0; i < num_toks && i < 8; i++) {
162+
for (int i = 0; i < num_toks && i < 16; i++) {
159163
n = (int)strlen(toks[i]);
160164
if (n > 255) n = 255;
165+
if (wl + 1 + (size_t)n > rr->rdata_len + 512)
166+
goto fail;
161167
w[wl++] = (uint8_t)n;
162168
memcpy(w + wl, toks[i], (size_t)n);
163169
wl += (size_t)n;

plugins/CMakeLists.txt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,11 @@ if (LWS_WITH_DHT)
279279
endif()
280280
endif()
281281

282-
create_plugin(protocol_lws_webrtc ""
283-
"protocol_lws_webrtc.c" "" "")
284-
if (NOT LWS_WITH_PLUGINS_BUILTIN)
285-
target_compile_definitions(protocol_lws_webrtc PRIVATE LWS_BUILDING_SHARED)
286-
endif()
282+
# create_plugin(protocol_lws_webrtc ""
283+
# "protocol_lws_webrtc.c" "" "")
284+
# if (NOT LWS_WITH_PLUGINS_BUILTIN)
285+
# target_compile_definitions(protocol_lws_webrtc PRIVATE LWS_BUILDING_SHARED)
286+
# endif()
287287

288288
if (LWS_WITH_TRANSCODE)
289289
create_plugin(protocol_lws_webrtc_mixer ""
@@ -302,6 +302,15 @@ if (LWS_WITH_LATENCY)
302302
endif()
303303
endif()
304304

305+
if (LWS_WITH_AUTHORITATIVE_DNS)
306+
create_plugin(protocol_lws_auth_dns ""
307+
"protocol_lws_auth_dns.c" "" "")
308+
if (NOT LWS_WITH_PLUGINS_BUILTIN)
309+
target_compile_definitions(protocol_lws_auth_dns PRIVATE LWS_BUILDING_SHARED)
310+
target_include_directories(protocol_lws_auth_dns PRIVATE ../lib/core ../lib/system/auth-dns)
311+
endif()
312+
endif()
313+
305314
endif((LWS_WITH_PLUGINS AND LWS_WITH_SHARED) OR LWS_WITH_PLUGINS_BUILTIN)
306315

307316

0 commit comments

Comments
 (0)