summaryrefslogtreecommitdiffstats
path: root/src/utils/common/params.c
blob: fe5a8546817e8da4a00f11f7cdbf566e5297ba08 (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
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
/*  Copyright (C) 2024 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    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 3 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, see <https://www.gnu.org/licenses/>.
 */

#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/socket.h>

#ifdef LIBIDN
#include <idn2.h>
#endif

#include "utils/common/params.h"
#include "utils/common/msg.h"
#include "utils/common/resolv.h"
#include "utils/common/token.h"
#include "libknot/libknot.h"
#include "contrib/macros.h"
#include "contrib/mempattern.h"
#include "contrib/openbsd/strlcpy.h"
#include "contrib/strtonum.h"

#define IPV4_REVERSE_DOMAIN	"in-addr.arpa."
#define IPV6_REVERSE_DOMAIN	"ip6.arpa."

char *name_from_idn(const char *idn_name) {
#ifdef LIBIDN
	char *name = NULL;

	int rc = idna_to_ascii_lz(idn_name, &name, 0);
	if (rc != IDNA_SUCCESS) {
		ERR("IDNA (%s)", idna_strerror(rc));
		return NULL;
	}

	if (strcasecmp(idn_name, name) == 0) {
		free(name);
		return strdup(idn_name);
	} else {
		return name;
	}
#endif
	return strdup(idn_name);
}

void name_to_idn(char **name) {
#ifdef LIBIDN
	char *idn_name = NULL;

	int rc = idna_to_unicode_8zlz(*name, &idn_name, 0);
	if (rc != IDNA_SUCCESS) {
		return;
	}

	free(*name);
	*name = idn_name;
#endif
	return;
}

/*!
 * \brief Checks if string is a prefix of reference string.
 *
 * \param pref		Prefix string.
 * \param pref_len	Prefix length.
 * \param str		Reference string (must have trailing zero).
 *
 * \retval -1		\a pref is not a prefix of \a str.
 * \retval  0<=		number of chars after prefix \a pref in \a str.
 */
static int cmp_prefix(const char *pref, const size_t pref_len,
                      const char *str)
{
	size_t i = 0;
	while (1) {
		// Different characters => NOT prefix.
		if (pref[i] != str[i]) {
			return -1;
		}

		i++;

		// Pref IS a prefix of pref.
		if (i == pref_len) {
			size_t rest = 0;
			while (str[i + rest] != '\0') {
				rest++;
			}
			return rest;
		// Pref is longer then ref => NOT prefix.
		} else if (str[i] == '\0') {
			return -1;
		}
	}
}

int best_param(const char *str, const size_t str_len, const param_t *tbl,
               bool *unique)
{
	if (str == NULL || str_len == 0 || tbl == NULL) {
		DBG_NULL;
		return KNOT_EINVAL;
	}

	int best_pos = -1;
	int best_match = INT_MAX;
	size_t matches = 0;
	for (int i = 0; tbl[i].name != NULL; i++) {
		int ret = cmp_prefix(str, str_len, tbl[i].name);
		switch (ret) {
		case -1:
			continue;
		case 0:
			*unique = true;
			return i;
		default:
			if (ret < best_match) {
				best_pos = i;
				best_match = ret;
			}
			matches++;
		}
	}

	switch (matches) {
	case 0:
		return KNOT_ENOTSUP;
	case 1:
		*unique = true;
		return best_pos;
	default:
		*unique = false;
		return best_pos;
	}
}

char *get_reverse_name(const char *name)
{
	struct in_addr	addr4;
	struct in6_addr	addr6;
	int		ret;
	char		buf[128] = "\0";

	if (name == NULL) {
		DBG_NULL;
		return NULL;
	}

	// Check name for IPv4 address, IPv6 address or other.
	if (inet_pton(AF_INET, name, &addr4) == 1) {
		uint32_t num = ntohl(addr4.s_addr);

		// Create IPv4 reverse FQD name.
		ret = snprintf(buf, sizeof(buf), "%u.%u.%u.%u.%s",
		               (num >>  0) & 0xFF, (num >>  8) & 0xFF,
		               (num >> 16) & 0xFF, (num >> 24) & 0xFF,
		               IPV4_REVERSE_DOMAIN);
		if (ret < 0 || (size_t)ret >= sizeof(buf)) {
			return NULL;
		}

		return strdup(buf);
	} else if (inet_pton(AF_INET6, name, &addr6) == 1) {
		char	*pos = buf;
		size_t  len = sizeof(buf);
		uint8_t left, right;

		// Create IPv6 reverse name.
		for (int i = 15; i >= 0; i--) {
			left = ((addr6.s6_addr)[i] & 0xF0) >> 4;
			right = (addr6.s6_addr)[i] & 0x0F;

			ret = snprintf(pos, len, "%x.%x.", right, left);
			if (ret < 0 || (size_t)ret >= len) {
				return NULL;
			}

			pos += ret;
			len -= ret;
		}

		// Add IPv6 reverse domain.
		ret = snprintf(pos, len, "%s", IPV6_REVERSE_DOMAIN);
		if (ret < 0 || (size_t)ret >= len) {
			return NULL;
		}

		return strdup(buf);
	} else {
		return NULL;
	}
}

char *get_fqd_name(const char *name)
{
	char *fqd_name = NULL;

	if (name == NULL) {
		DBG_NULL;
		return NULL;
	}

	size_t name_len = strlen(name);

	// If the name is FQDN, make a copy.
	if (name_len > 0 && name[name_len - 1] == '.') {
		fqd_name = strdup(name);
	// Else make a copy and append a trailing dot.
	} else {
		size_t fqd_name_size = name_len + 2;
		fqd_name = malloc(fqd_name_size);
		if (fqd_name != NULL) {
			strlcpy(fqd_name, name, fqd_name_size);
			fqd_name[name_len] = '.';
			fqd_name[name_len + 1] = 0;
		}
	}

	return fqd_name;
}

int params_parse_class(const char *value, uint16_t *rclass)
{
	if (value == NULL || rclass == NULL) {
		DBG_NULL;
		return KNOT_EINVAL;
	}

	if (knot_rrclass_from_string(value, rclass) == 0) {
		return KNOT_EOK;
	} else {
		return KNOT_EINVAL;
	}
}

int params_parse_type(const char *value, uint16_t *rtype, int64_t *serial,
                      bool *notify)
{
	if (value == NULL || rtype == NULL || serial == NULL) {
		DBG_NULL;
		return KNOT_EINVAL;
	}

	// Find and parse type name.
	size_t param_pos = strcspn(value, "=");
	char *type_char = strndup(value, param_pos);

	if (knot_rrtype_from_string(type_char, rtype) != 0) {
		size_t cmp_len = MAX(strlen("NOTIFY"), param_pos);
		if (strncasecmp(type_char, "NOTIFY", cmp_len) == 0) {
			*rtype = KNOT_RRTYPE_SOA;
			*notify = true;
		} else {
			free(type_char);
			return KNOT_EINVAL;
		}
	} else {
		*notify = false;
	}

	free(type_char);

	// Parse additional parameter.
	if (param_pos == strlen(value)) {
		// IXFR requires serial parameter.
		if (*rtype == KNOT_RRTYPE_IXFR) {
			DBG("SOA serial is required for IXFR query");
			return KNOT_EINVAL;
		} else {
			*serial = -1;
		}
	} else {
		// Additional parameter is accepted for IXFR or NOTIFY.
		if (*rtype == KNOT_RRTYPE_IXFR || *notify) {
			const char *param_str = value + 1 + param_pos;
			char *end;

			// Convert string to serial.
			unsigned long long num = strtoull(param_str, &end, 10);

			// Check for bad serial string.
			if (end == param_str || *end != '\0' || num > UINT32_MAX) {
				DBG("bad SOA serial '%s'", param_str);
				return KNOT_EINVAL;
			}

			*serial = num;
		} else {
			DBG("unsupported parameter '%s'", value);
			return KNOT_EINVAL;
		}
	}

	return KNOT_EOK;
}

int params_parse_server(const char *value, list_t *servers, const char *def_port)
{
	if (value == NULL || servers == NULL) {
		DBG_NULL;
		return KNOT_EINVAL;
	}

	// Add specified nameserver.
	srv_info_t *server = parse_nameserver(value, def_port);
	if (server == NULL) {
		return KNOT_EINVAL;
	}
	add_tail(servers, (node_t *)server);

	return KNOT_EOK;
}

int params_parse_wait(const char *value, int32_t *dst)
{
	if (value == NULL || dst == NULL) {
		DBG_NULL;
		return KNOT_EINVAL;
	}

	uint32_t num = 0;
	int ret = str_to_u32(value, &num);
	if (ret != KNOT_EOK) {
		return ret;
	}

	if (num < 1 || num > INT32_MAX / 1000) {
		num = INT32_MAX / 1000;
	}

	*dst = num;

	return KNOT_EOK;
}