Fix: Possible invalid read on string in set_ip_address()
[lttng-tools.git] / src / common / uri.c
1 /*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <assert.h>
20 #include <arpa/inet.h>
21 #include <netdb.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/socket.h>
25
26 #include <common/common.h>
27 #include <common/defaults.h>
28 #include <common/utils.h>
29
30 #include "uri.h"
31
32 enum uri_proto_code {
33 P_NET, P_NET6, P_FILE, P_TCP, P_TCP6,
34 };
35
36 struct uri_proto {
37 const char *name;
38 const char *leading_string;
39 enum uri_proto_code code;
40 enum lttng_proto_type type;
41 enum lttng_dst_type dtype;
42 };
43
44 /* Supported protocols */
45 static const struct uri_proto proto_uri[] = {
46 { .name = "file", .leading_string = "file://", .code = P_FILE, .type = 0, .dtype = LTTNG_DST_PATH },
47 { .name = "net", .leading_string = "net://", .code = P_NET, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV4 },
48 { .name = "net6", .leading_string = "net6://", .code = P_NET6, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV6 },
49 { .name = "tcp", .leading_string = "tcp://", .code = P_TCP, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV4 },
50 { .name = "tcp6", .leading_string = "tcp6://", .code = P_TCP6, .type = LTTNG_TCP, .dtype = LTTNG_DST_IPV6 },
51 /* Invalid proto marking the end of the array. */
52 { NULL, NULL, 0, 0, 0 }
53 };
54
55 /*
56 * Return pointer to the character in s matching one of the characters in
57 * accept. If nothing is found, return pointer to the end of string (eos).
58 */
59 const inline char *strpbrk_or_eos(const char *s, const char *accept)
60 {
61 char *p = strpbrk(s, accept);
62 if (p == NULL) {
63 p = strchr(s, '\0');
64 }
65
66 return p;
67 }
68
69
70 /*
71 * Validate if proto is a supported protocol from proto_uri array.
72 */
73 static const struct uri_proto *get_uri_proto(const char *uri_str)
74 {
75 const struct uri_proto *supported = NULL;
76
77 /* Safety net */
78 if (uri_str == NULL) {
79 goto end;
80 }
81
82 for (supported = &proto_uri[0];
83 supported->leading_string != NULL; ++supported) {
84 if (strncasecmp(uri_str, supported->leading_string,
85 strlen(supported->leading_string)) == 0) {
86 goto end;
87 }
88 }
89
90 /* Proto not found */
91 return NULL;
92
93 end:
94 return supported;
95 }
96
97 /*
98 * Set network address from string into dst. Supports both IP string and
99 * hostname.
100 */
101 static int set_ip_address(const char *addr, int af, char *dst, size_t size)
102 {
103 int ret;
104 unsigned char buf[sizeof(struct in6_addr)];
105 struct hostent *record;
106
107 assert(addr);
108 assert(dst);
109
110 memset(dst, 0, size);
111
112 /* Network protocol */
113 ret = inet_pton(af, addr, buf);
114 if (ret < 1) {
115 /* We consider the dst to be an hostname or an invalid IP char */
116 record = gethostbyname2(addr, af);
117 if (record == NULL) {
118 /* At this point, the IP or the hostname is bad */
119 ERR("URI parse bad hostname %s for af %d", addr, af);
120 goto error;
121 }
122
123 /* Translate IP to string */
124 (void) inet_ntop(af, record->h_addr_list[0], dst, size);
125 } else {
126 if (size > 0) {
127 strncpy(dst, addr, size);
128 dst[size - 1] = '\0';
129 }
130 }
131
132 DBG2("IP address resolved to %s", dst);
133
134 return 0;
135
136 error:
137 return -1;
138 }
139
140 /*
141 * Compare two URIs.
142 *
143 * Return 0 if equal else 1.
144 */
145 int uri_compare(struct lttng_uri *uri1, struct lttng_uri *uri2)
146 {
147 return memcmp(uri1, uri2, sizeof(struct lttng_uri));
148 }
149
150 /*
151 * Free URI memory.
152 */
153 void uri_free(struct lttng_uri *uri)
154 {
155 /* Safety check */
156 if (uri != NULL) {
157 free(uri);
158 }
159 }
160
161 /*
162 * Return an allocated URI.
163 */
164 struct lttng_uri *uri_create(void)
165 {
166 struct lttng_uri *uri;
167
168 uri = zmalloc(sizeof(struct lttng_uri));
169 if (uri == NULL) {
170 PERROR("zmalloc uri");
171 }
172
173 return uri;
174 }
175
176 /*
177 * Parses a string URI to a lttng_uri. This function can potentially return
178 * more than one URI in uris so the size of the array is returned and uris is
179 * allocated and populated. Caller must free(3) the array.
180 *
181 * This function can not detect the stream type of the URI so the caller has to
182 * make sure the correct type (stype) is set on the return URI(s). The default
183 * port must also be set by the caller if the returned URI has its port set to
184 * zero.
185 *
186 * NOTE: A good part of the following code was inspired from the "wget" source
187 * tree from the src/url.c file and url_parse() function. Also, the
188 * strpbrk_or_eos() function found above is also inspired by the same code.
189 * This code was originally licensed GPLv2 so we acknolwedge the Free Software
190 * Foundation here for the work and to make sure we are compliant with it.
191 */
192 ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris)
193 {
194 int ret, i = 0;
195 /* Size of the uris array. Default is 1 */
196 ssize_t size = 1;
197 char subdir[PATH_MAX];
198 unsigned int ctrl_port = 0;
199 unsigned int data_port = 0;
200 struct lttng_uri *tmp_uris;
201 char *addr_f = NULL;
202 const struct uri_proto *proto;
203 const char *purl, *addr_e, *addr_b, *subdir_b = NULL;
204 const char *seps = ":/\0";
205
206 /*
207 * The first part is the protocol portion of a maximum of 5 bytes for now.
208 * The second part is the hostname or IP address. The 255 bytes size is the
209 * limit found in the RFC 1035 for the total length of a domain name
210 * (https://www.ietf.org/rfc/rfc1035.txt). Finally, for the net://
211 * protocol, two ports CAN be specified.
212 */
213
214 DBG3("URI string: %s", str_uri);
215
216 proto = get_uri_proto(str_uri);
217 if (proto == NULL) {
218 ERR("URI parse unknown protocol %s", str_uri);
219 goto error;
220 }
221
222 purl = str_uri;
223
224 if (proto->code == P_NET || proto->code == P_NET6) {
225 /* Special case for net:// which requires two URI objects */
226 size = 2;
227 }
228
229 /* Allocate URI array */
230 tmp_uris = zmalloc(sizeof(struct lttng_uri) * size);
231 if (tmp_uris == NULL) {
232 PERROR("zmalloc uri");
233 goto error;
234 }
235
236 memset(subdir, 0, sizeof(subdir));
237 purl += strlen(proto->leading_string);
238
239 /* Copy known value to the first URI. */
240 tmp_uris[0].dtype = proto->dtype;
241 tmp_uris[0].proto = proto->type;
242
243 if (proto->code == P_FILE) {
244 if (*purl != '/') {
245 ERR("Missing destination full path.");
246 goto free_error;
247 }
248
249 strncpy(tmp_uris[0].dst.path, purl, sizeof(tmp_uris[0].dst.path));
250 tmp_uris[0].dst.path[sizeof(tmp_uris[0].dst.path) - 1] = '\0';
251 DBG3("URI file destination: %s", purl);
252 goto end;
253 }
254
255 /* Assume we are at the beginning of an address or host of some sort. */
256 addr_b = purl;
257
258 /*
259 * Handle IPv6 address inside square brackets as mention by RFC 2732. IPv6
260 * address that does not start AND end with brackets will be rejected even
261 * if valid.
262 *
263 * proto://[<addr>]...
264 * ^
265 */
266 if (*purl == '[') {
267 /* Address begins after '[' */
268 addr_b = purl + 1;
269 addr_e = strchr(addr_b, ']');
270 if (addr_e == NULL || addr_b == addr_e) {
271 ERR("Broken IPv6 address %s", addr_b);
272 goto free_error;
273 }
274
275 /* Moving parsed URL pointer after the final bracket ']' */
276 purl = addr_e + 1;
277
278 /*
279 * The closing bracket must be followed by a seperator or NULL char.
280 */
281 if (strchr(seps, *purl) == NULL) {
282 ERR("Unknown symbol after IPv6 address: %s", purl);
283 goto free_error;
284 }
285 } else {
286 purl = strpbrk_or_eos(purl, seps);
287 addr_e = purl;
288 }
289
290 /* Check if we at least have a char for the addr or hostname. */
291 if (addr_b == addr_e) {
292 ERR("No address or hostname detected.");
293 goto free_error;
294 }
295
296 addr_f = utils_strdupdelim(addr_b, addr_e);
297 if (addr_f == NULL) {
298 goto free_error;
299 }
300
301 /*
302 * Detect PORT after address. The net/net6 protocol allows up to two port
303 * so we can define the control and data port.
304 */
305 while (*purl == ':') {
306 int port;
307 const char *port_b, *port_e;
308 char *port_f;
309
310 /* Update pass counter */
311 i++;
312
313 /*
314 * Maximum of two ports is possible if P_NET/NET6. Bigger than that,
315 * two much stuff.
316 */
317 if ((i == 2 && (proto->code != P_NET && proto->code != P_NET6))
318 || i > 2) {
319 break;
320 }
321
322 /*
323 * Move parsed URL to port value.
324 * proto://addr_host:PORT1:PORT2/foo/bar
325 * ^
326 */
327 ++purl;
328 port_b = purl;
329 purl = strpbrk_or_eos(purl, seps);
330 port_e = purl;
331
332 if (port_b != port_e) {
333 port_f = utils_strdupdelim(port_b, port_e);
334 if (port_f == NULL) {
335 goto free_error;
336 }
337
338 port = atoi(port_f);
339 if (port > 0xffff || port <= 0x0) {
340 ERR("Invalid port number %d", port);
341 free(port_f);
342 goto free_error;
343 }
344 free(port_f);
345
346 if (i == 1) {
347 ctrl_port = port;
348 } else {
349 data_port = port;
350 }
351 }
352 };
353
354 /* Check for a valid subdir or trailing garbage */
355 if (*purl == '/') {
356 /*
357 * Move to subdir value.
358 * proto://addr_host:PORT1:PORT2/foo/bar
359 * ^
360 */
361 ++purl;
362 subdir_b = purl;
363 } else if (*purl != '\0') {
364 ERR("Trailing characters not recognized: %s", purl);
365 goto free_error;
366 }
367
368 /* We have enough valid information to create URI(s) object */
369
370 /* Copy generic information */
371 tmp_uris[0].port = ctrl_port;
372
373 /* Copy subdirectory if one. */
374 if (subdir_b) {
375 strncpy(tmp_uris[0].subdir, subdir_b, sizeof(tmp_uris[0].subdir));
376 tmp_uris[0].subdir[sizeof(tmp_uris[0].subdir) - 1] = '\0';
377 }
378
379 switch (proto->code) {
380 case P_NET:
381 ret = set_ip_address(addr_f, AF_INET, tmp_uris[0].dst.ipv4,
382 sizeof(tmp_uris[0].dst.ipv4));
383 if (ret < 0) {
384 goto free_error;
385 }
386
387 memcpy(tmp_uris[1].dst.ipv4, tmp_uris[0].dst.ipv4, sizeof(tmp_uris[1].dst.ipv4));
388
389 tmp_uris[1].dtype = proto->dtype;
390 tmp_uris[1].proto = proto->type;
391 tmp_uris[1].port = data_port;
392 break;
393 case P_NET6:
394 ret = set_ip_address(addr_f, AF_INET6, tmp_uris[0].dst.ipv6,
395 sizeof(tmp_uris[0].dst.ipv6));
396 if (ret < 0) {
397 goto free_error;
398 }
399
400 memcpy(tmp_uris[1].dst.ipv6, tmp_uris[0].dst.ipv6, sizeof(tmp_uris[1].dst.ipv6));
401
402 tmp_uris[1].dtype = proto->dtype;
403 tmp_uris[1].proto = proto->type;
404 tmp_uris[1].port = data_port;
405 break;
406 case P_TCP:
407 ret = set_ip_address(addr_f, AF_INET, tmp_uris[0].dst.ipv4,
408 sizeof(tmp_uris[0].dst.ipv4));
409 if (ret < 0) {
410 goto free_error;
411 }
412 break;
413 case P_TCP6:
414 ret = set_ip_address(addr_f, AF_INET6, tmp_uris[0].dst.ipv6,
415 sizeof(tmp_uris[0].dst.ipv6));
416 if (ret < 0) {
417 goto free_error;
418 }
419 break;
420 default:
421 goto free_error;
422 }
423
424 end:
425 DBG3("URI dtype: %d, proto: %d, host: %s, subdir: %s, ctrl: %d, data: %d",
426 proto->dtype, proto->type, (addr_f == NULL) ? "" : addr_f,
427 (subdir_b == NULL) ? "" : subdir_b, ctrl_port, data_port);
428
429 free(addr_f);
430
431 *uris = tmp_uris;
432 return size;
433
434 free_error:
435 free(addr_f);
436 free(tmp_uris);
437 error:
438 return -1;
439 }
This page took 0.058495 seconds and 4 git commands to generate.