/src/sudo/lib/iolog/host_port.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2019-2020 Todd C. Miller <Todd.Miller@sudo.ws> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | /* |
20 | | * This is an open source non-commercial project. Dear PVS-Studio, please check it. |
21 | | * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
22 | | */ |
23 | | |
24 | | #include <config.h> |
25 | | |
26 | | #ifdef HAVE_STDBOOL_H |
27 | | # include <stdbool.h> |
28 | | #else |
29 | | # include "compat/stdbool.h" |
30 | | #endif /* HAVE_STDBOOL_H */ |
31 | | #include <stdio.h> |
32 | | #include <string.h> |
33 | | #include <time.h> |
34 | | |
35 | | #include "sudo_compat.h" |
36 | | #include "sudo_debug.h" |
37 | | #include "sudo_gettext.h" |
38 | | #include "sudo_util.h" |
39 | | #include "sudo_iolog.h" |
40 | | |
41 | | /* |
42 | | * Parse a string in the form host[:port] where host can also be |
43 | | * an IPv4 address or an IPv6 address in square brackets. |
44 | | * Fills in hostp and portp which may point within str, which is modified. |
45 | | */ |
46 | | bool |
47 | | iolog_parse_host_port(char *str, char **hostp, char **portp, bool *tlsp, |
48 | | const char *defport, const char *defport_tls) |
49 | 1.91k | { |
50 | 1.91k | char *flags, *port, *host = str; |
51 | 1.91k | bool ret = false; |
52 | 1.91k | bool tls = false; |
53 | 1.91k | debug_decl(iolog_parse_host_port, SUDO_DEBUG_UTIL); |
54 | | |
55 | | /* Check for IPv6 address like [::0] followed by optional port */ |
56 | 1.91k | if (*host == '[') { |
57 | 12 | host++; |
58 | 12 | port = strchr(host, ']'); |
59 | 12 | if (port == NULL) { |
60 | 1 | sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, |
61 | 1 | "invalid IPv6 address %s", str); |
62 | 1 | goto done; |
63 | 1 | } |
64 | 11 | *port++ = '\0'; |
65 | 11 | switch (*port) { |
66 | 8 | case ':': |
67 | 8 | port++; |
68 | 8 | break; |
69 | 1 | case '\0': |
70 | 1 | port = NULL; /* no port specified */ |
71 | 1 | break; |
72 | 1 | case '(': |
73 | | /* flag, handled below */ |
74 | 1 | break; |
75 | 1 | default: |
76 | 1 | sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, |
77 | 1 | "invalid IPv6 address %s", str); |
78 | 1 | goto done; |
79 | 11 | } |
80 | 1.90k | } else { |
81 | 1.90k | port = strrchr(host, ':'); |
82 | 1.90k | if (port != NULL) |
83 | 1.83k | *port++ = '\0'; |
84 | 1.90k | } |
85 | | |
86 | | /* Check for optional tls flag at the end. */ |
87 | 1.91k | flags = strchr(port ? port : host, '('); |
88 | 1.91k | if (flags != NULL) { |
89 | 133 | if (strcasecmp(flags, "(tls)") == 0) |
90 | 80 | tls = true; |
91 | 133 | *flags = '\0'; |
92 | 133 | if (port == flags) |
93 | 3 | port = NULL; |
94 | 133 | } |
95 | | |
96 | 1.91k | if (port == NULL) |
97 | 76 | port = tls ? (char *)defport_tls : (char *)defport; |
98 | 1.83k | else if (*port == '\0') |
99 | 2 | goto done; |
100 | | |
101 | 1.91k | *hostp = host; |
102 | 1.91k | *portp = port; |
103 | 1.91k | *tlsp = tls; |
104 | | |
105 | 1.91k | ret = true; |
106 | | |
107 | 1.91k | done: |
108 | 1.91k | debug_return_bool(ret); |
109 | 1.91k | } |