1 | /* $Id: VBoxNetPortForwardString.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxNetPortForwardString - Routines for managing port-forward strings.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #ifndef RT_OS_WINDOWS
|
---|
33 | # include <netinet/in.h>
|
---|
34 | #else
|
---|
35 | # include <iprt/win/winsock2.h>
|
---|
36 | # include <Ws2ipdef.h>
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | #include <iprt/cdefs.h>
|
---|
40 | #include <iprt/cidr.h>
|
---|
41 | #include <iprt/ctype.h>
|
---|
42 | #include <iprt/errcore.h>
|
---|
43 | #include <iprt/getopt.h>
|
---|
44 | #include <iprt/net.h>
|
---|
45 | #include <iprt/param.h>
|
---|
46 | #include <iprt/path.h>
|
---|
47 | #include <iprt/stream.h>
|
---|
48 | #include <iprt/string.h>
|
---|
49 |
|
---|
50 | #include <VBox/log.h>
|
---|
51 |
|
---|
52 | #include "VBoxPortForwardString.h"
|
---|
53 |
|
---|
54 |
|
---|
55 | /*********************************************************************************************************************************
|
---|
56 | * Defined Constants And Macros *
|
---|
57 | *********************************************************************************************************************************/
|
---|
58 | #define PF_FIELD_SEPARATOR ':'
|
---|
59 | #define PF_ADDRESS_FIELD_STARTS '['
|
---|
60 | #define PF_ADDRESS_FIELD_ENDS ']'
|
---|
61 |
|
---|
62 | #define PF_STR_FIELD_SEPARATOR ":"
|
---|
63 | #define PF_STR_ADDRESS_FIELD_STARTS "["
|
---|
64 | #define PF_STR_ADDRESS_FIELD_ENDS "]"
|
---|
65 |
|
---|
66 |
|
---|
67 | static int netPfStrAddressParse(char *pszRaw, size_t cchRaw,
|
---|
68 | char *pszAddress, int cbAddress,
|
---|
69 | bool fEmptyAcceptable)
|
---|
70 | {
|
---|
71 | size_t cchField = 0;
|
---|
72 |
|
---|
73 | AssertPtrReturn(pszRaw, -1);
|
---|
74 | AssertPtrReturn(pszAddress, -1);
|
---|
75 | AssertReturn(pszRaw[0] == PF_ADDRESS_FIELD_STARTS, -1);
|
---|
76 |
|
---|
77 | /* shift pszRaw to next symbol */
|
---|
78 | pszRaw++;
|
---|
79 | cchRaw--;
|
---|
80 |
|
---|
81 | /* we shouldn't face with ending here */
|
---|
82 | AssertReturn(cchRaw > 0, VERR_INVALID_PARAMETER);
|
---|
83 |
|
---|
84 | char *pszEndOfAddress = RTStrStr(pszRaw, PF_STR_ADDRESS_FIELD_ENDS);
|
---|
85 |
|
---|
86 | /* no pair closing sign */
|
---|
87 | AssertPtrReturn(pszEndOfAddress, VERR_INVALID_PARAMETER);
|
---|
88 |
|
---|
89 | cchField = pszEndOfAddress - pszRaw;
|
---|
90 |
|
---|
91 | /* field should be less then the rest of the string */
|
---|
92 | AssertReturn(cchField < cchRaw, VERR_INVALID_PARAMETER);
|
---|
93 |
|
---|
94 | if (cchField != 0)
|
---|
95 | RTStrCopy(pszAddress, RT_MIN(cchField + 1, (size_t)cbAddress), pszRaw);
|
---|
96 | else if (!fEmptyAcceptable)
|
---|
97 | return -1;
|
---|
98 |
|
---|
99 | AssertReturn(pszRaw[cchField] == PF_ADDRESS_FIELD_ENDS, -1);
|
---|
100 | return (int)cchField + 2; /* length of the field and closing braces */
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Parses a port something.
|
---|
106 | *
|
---|
107 | * @returns Offset relative to @a pszRaw of the end of the port field.
|
---|
108 | * -1 on failure.
|
---|
109 | * @param pszRaw The zero terminated string to parse. Points a field
|
---|
110 | * separator.
|
---|
111 | * @param pu16Port Where to store the port number on success.
|
---|
112 | */
|
---|
113 | static int netPfStrPortParse(char *pszRaw, uint16_t *pu16Port)
|
---|
114 | {
|
---|
115 | #if 1
|
---|
116 | AssertPtrReturn(pszRaw, -1);
|
---|
117 | AssertPtrReturn(pu16Port, -1);
|
---|
118 | AssertReturn(pszRaw[0] == PF_FIELD_SEPARATOR, -1);
|
---|
119 |
|
---|
120 | char *pszNext = NULL;
|
---|
121 | int rc = RTStrToUInt16Ex(&pszRaw[1], &pszNext, 0, pu16Port);
|
---|
122 | if (rc == VWRN_TRAILING_CHARS)
|
---|
123 | AssertReturn(*pszNext == PF_FIELD_SEPARATOR, -1);
|
---|
124 | else if (rc == VINF_SUCCESS)
|
---|
125 | Assert(*pszNext == '\0');
|
---|
126 | else
|
---|
127 | AssertMsgFailedReturn(("rc=%Rrc\n", rc), -1);
|
---|
128 | if (*pu16Port == 0)
|
---|
129 | return -1;
|
---|
130 | return (int)(pszNext - pszRaw);
|
---|
131 |
|
---|
132 | #else /* The same code, just a little more verbose: */
|
---|
133 | char *pszEndOfPort = NULL;
|
---|
134 | uint16_t u16Port = 0;
|
---|
135 | int idxRaw = 1; /* we increment pszRaw after checks. */
|
---|
136 | int cbRest = 0;
|
---|
137 | size_t cbPort = 0;
|
---|
138 |
|
---|
139 | AssertPtrReturn(pszRaw, -1);
|
---|
140 | AssertPtrReturn(pu16Port, -1);
|
---|
141 | AssertReturn(pszRaw[0] == PF_FIELD_SEPARATOR, -1);
|
---|
142 |
|
---|
143 | pszRaw++; /* skip field separator */
|
---|
144 | cchRaw --;
|
---|
145 |
|
---|
146 | char *pszEndOfPort = RTStrStr(pszRaw, ":");
|
---|
147 | if (!pszEndOfPort)
|
---|
148 | {
|
---|
149 | cbRest = strlen(pszRaw);
|
---|
150 |
|
---|
151 | Assert(cchRaw == cbRest);
|
---|
152 |
|
---|
153 | /* XXX: Assumption that if string is too big, it will be reported by
|
---|
154 | * RTStrToUint16.
|
---|
155 | */
|
---|
156 | if (cbRest > 0)
|
---|
157 | {
|
---|
158 | pszEndOfPort = pszRaw + cbRest;
|
---|
159 | cbPort = cbRest;
|
---|
160 | }
|
---|
161 | else
|
---|
162 | return -1;
|
---|
163 | }
|
---|
164 | else
|
---|
165 | cbPort = pszEndOfPort - pszRaw;
|
---|
166 |
|
---|
167 |
|
---|
168 | idxRaw += cbPort;
|
---|
169 |
|
---|
170 | Assert(cbRest || pszRaw[idxRaw - 1] == PF_FIELD_SEPARATOR); /* we are 1 char ahead */
|
---|
171 |
|
---|
172 | char szPort[10];
|
---|
173 | RT_ZERO(szPort);
|
---|
174 |
|
---|
175 | Assert(idxRaw > 0);
|
---|
176 | RTStrCopy(szPort, RT_MIN(sizeof(szPort), (size_t)(cbPort) + 1), pszRaw);
|
---|
177 |
|
---|
178 | if (!(u16Port = RTStrToUInt16(szPort)))
|
---|
179 | return -1;
|
---|
180 |
|
---|
181 | *pu16Port = u16Port;
|
---|
182 |
|
---|
183 | return idxRaw;
|
---|
184 | #endif
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | static int netPfStrAddressPortPairParse(char *pszRaw, size_t cchRaw,
|
---|
189 | char *pszAddress, int cbAddress,
|
---|
190 | bool fEmptyAddressAcceptable,
|
---|
191 | uint16_t *pu16Port)
|
---|
192 | {
|
---|
193 | int idxRaw = 0;
|
---|
194 | int idxRawTotal = 0;
|
---|
195 |
|
---|
196 | AssertPtrReturn(pszRaw, -1);
|
---|
197 | AssertPtrReturn(pszAddress, -1);
|
---|
198 | AssertPtrReturn(pu16Port, -2);
|
---|
199 |
|
---|
200 | /* XXX: Here we should check 0 - ':' and 1 - '[' */
|
---|
201 | Assert( pszRaw[0] == PF_FIELD_SEPARATOR
|
---|
202 | && pszRaw[1] == PF_ADDRESS_FIELD_STARTS);
|
---|
203 |
|
---|
204 | pszRaw++; /* field separator skip */
|
---|
205 | cchRaw--;
|
---|
206 | AssertReturn(cchRaw > 0, VERR_INVALID_PARAMETER);
|
---|
207 |
|
---|
208 | idxRaw = 0;
|
---|
209 |
|
---|
210 | if (pszRaw[0] == PF_ADDRESS_FIELD_STARTS)
|
---|
211 | {
|
---|
212 | idxRaw += netPfStrAddressParse(pszRaw,
|
---|
213 | cchRaw - idxRaw,
|
---|
214 | pszAddress,
|
---|
215 | cbAddress,
|
---|
216 | fEmptyAddressAcceptable);
|
---|
217 | if (idxRaw == -1)
|
---|
218 | return -1;
|
---|
219 |
|
---|
220 | Assert(pszRaw[idxRaw] == PF_FIELD_SEPARATOR);
|
---|
221 | }
|
---|
222 | else return -1;
|
---|
223 |
|
---|
224 | pszRaw += idxRaw;
|
---|
225 | idxRawTotal += idxRaw;
|
---|
226 | cchRaw -= idxRaw;
|
---|
227 |
|
---|
228 | AssertReturn(cchRaw > 0, VERR_INVALID_PARAMETER);
|
---|
229 |
|
---|
230 | idxRaw = 0;
|
---|
231 |
|
---|
232 | Assert(pszRaw[0] == PF_FIELD_SEPARATOR);
|
---|
233 |
|
---|
234 | if (pszRaw[0] == PF_FIELD_SEPARATOR)
|
---|
235 | {
|
---|
236 | idxRaw = netPfStrPortParse(pszRaw, pu16Port);
|
---|
237 |
|
---|
238 | Assert(strlen(&pszRaw[idxRaw]) == 0 || pszRaw[idxRaw] == PF_FIELD_SEPARATOR);
|
---|
239 |
|
---|
240 | if (idxRaw == -1)
|
---|
241 | return -2;
|
---|
242 |
|
---|
243 | idxRawTotal += idxRaw;
|
---|
244 |
|
---|
245 | return idxRawTotal + 1;
|
---|
246 | }
|
---|
247 | else return -1; /* trailing garbage in the address */
|
---|
248 | }
|
---|
249 |
|
---|
250 | /* XXX: Having fIPv6 we might emprove adress verification comparing address length
|
---|
251 | * with INET[6]_ADDRLEN
|
---|
252 | *
|
---|
253 | */
|
---|
254 | int netPfStrToPf(const char *pcszStrPortForward, bool fIPv6, PPORTFORWARDRULE pPfr)
|
---|
255 | {
|
---|
256 | /** r=bird: Redo from scratch? This is very hard to read. And it's going about
|
---|
257 | * things in a very complicated, potentially leaky (pszRaw) fashion. */
|
---|
258 |
|
---|
259 | int proto;
|
---|
260 | uint16_t u16HostPort;
|
---|
261 | uint16_t u16GuestPort;
|
---|
262 | bool fTcpProto = false;
|
---|
263 |
|
---|
264 | int idxRaw = 0;
|
---|
265 | int cbToken = 0;
|
---|
266 |
|
---|
267 | AssertPtrReturn(pcszStrPortForward, VERR_INVALID_PARAMETER);
|
---|
268 | AssertPtrReturn(pPfr, VERR_INVALID_PARAMETER);
|
---|
269 |
|
---|
270 | RT_ZERO(*pPfr);
|
---|
271 |
|
---|
272 | char *pszHostAddr = &pPfr->szPfrHostAddr[0];
|
---|
273 | char *pszGuestAddr = &pPfr->szPfrGuestAddr[0];
|
---|
274 | char *pszName = &pPfr->szPfrName[0];
|
---|
275 |
|
---|
276 | size_t cchRaw = strlen(pcszStrPortForward);
|
---|
277 |
|
---|
278 | /* Minimal rule ":tcp:[]:0:[]:0" has got lenght 14 */
|
---|
279 | AssertReturn(cchRaw > 14, VERR_INVALID_PARAMETER);
|
---|
280 |
|
---|
281 | char *pszRaw = RTStrDup(pcszStrPortForward);
|
---|
282 | AssertReturn(pszRaw, VERR_NO_MEMORY);
|
---|
283 |
|
---|
284 | char *pszRawBegin = pszRaw;
|
---|
285 |
|
---|
286 | /* name */
|
---|
287 | if (pszRaw[idxRaw] != PF_FIELD_SEPARATOR)
|
---|
288 | {
|
---|
289 | char *pszEndOfName = RTStrStr(pszRaw + 1, PF_STR_FIELD_SEPARATOR);
|
---|
290 | if (!pszEndOfName)
|
---|
291 | goto invalid_parameter;
|
---|
292 |
|
---|
293 | cbToken = pszEndOfName - pszRaw; /* don't take : into account */
|
---|
294 | /* XXX it's unacceptable to have only name entry in PF */
|
---|
295 | AssertReturn(cbToken < (ssize_t)cchRaw, VERR_INVALID_PARAMETER);
|
---|
296 |
|
---|
297 | if ( cbToken < 0
|
---|
298 | || (size_t)cbToken >= PF_NAMELEN)
|
---|
299 | goto invalid_parameter;
|
---|
300 |
|
---|
301 | RTStrCopy(pszName,
|
---|
302 | RT_MIN((size_t)cbToken + 1, PF_NAMELEN),
|
---|
303 | pszRaw);
|
---|
304 | pszRaw += cbToken; /* move to separator */
|
---|
305 | cchRaw -= cbToken;
|
---|
306 | }
|
---|
307 |
|
---|
308 | AssertReturn(pszRaw[0] == PF_FIELD_SEPARATOR, VERR_INVALID_PARAMETER);
|
---|
309 | /* protocol */
|
---|
310 |
|
---|
311 | pszRaw++; /* skip separator */
|
---|
312 | cchRaw--;
|
---|
313 | idxRaw = 0;
|
---|
314 |
|
---|
315 | if ( ( (fTcpProto = (RTStrNICmp(pszRaw, "tcp", 3) == 0))
|
---|
316 | || RTStrNICmp(pszRaw, "udp", 3) == 0)
|
---|
317 | && pszRaw[3] == PF_FIELD_SEPARATOR)
|
---|
318 | {
|
---|
319 | proto = (fTcpProto ? IPPROTO_TCP : IPPROTO_UDP);
|
---|
320 | idxRaw = 3;
|
---|
321 | }
|
---|
322 | else
|
---|
323 | goto invalid_parameter;
|
---|
324 |
|
---|
325 | pszRaw += idxRaw;
|
---|
326 | cchRaw -= idxRaw;
|
---|
327 |
|
---|
328 | idxRaw = netPfStrAddressPortPairParse(pszRaw, cchRaw,
|
---|
329 | pszHostAddr, INET6_ADDRSTRLEN,
|
---|
330 | true, &u16HostPort);
|
---|
331 | if (idxRaw < 0)
|
---|
332 | return VERR_INVALID_PARAMETER;
|
---|
333 |
|
---|
334 | pszRaw += idxRaw;
|
---|
335 | cchRaw -= idxRaw;
|
---|
336 |
|
---|
337 | Assert(pszRaw[0] == PF_FIELD_SEPARATOR);
|
---|
338 |
|
---|
339 | idxRaw = netPfStrAddressPortPairParse(pszRaw, cchRaw,
|
---|
340 | pszGuestAddr, INET6_ADDRSTRLEN,
|
---|
341 | false, &u16GuestPort);
|
---|
342 |
|
---|
343 | if (idxRaw < 0)
|
---|
344 | goto invalid_parameter;
|
---|
345 |
|
---|
346 | /* XXX: fill the rule */
|
---|
347 | pPfr->fPfrIPv6 = fIPv6;
|
---|
348 | pPfr->iPfrProto = proto;
|
---|
349 |
|
---|
350 | pPfr->u16PfrHostPort = u16HostPort;
|
---|
351 |
|
---|
352 | if (*pszGuestAddr == '\0')
|
---|
353 | goto invalid_parameter; /* guest address should be defined */
|
---|
354 |
|
---|
355 | pPfr->u16PfrGuestPort = u16GuestPort;
|
---|
356 |
|
---|
357 | Log(("name: %s\n"
|
---|
358 | "proto: %d\n"
|
---|
359 | "host address: %s\n"
|
---|
360 | "host port: %d\n"
|
---|
361 | "guest address: %s\n"
|
---|
362 | "guest port:%d\n",
|
---|
363 | pszName, proto,
|
---|
364 | pszHostAddr, u16HostPort,
|
---|
365 | pszGuestAddr, u16GuestPort));
|
---|
366 |
|
---|
367 | RTStrFree(pszRawBegin);
|
---|
368 | return VINF_SUCCESS;
|
---|
369 |
|
---|
370 | invalid_parameter:
|
---|
371 | RTStrFree(pszRawBegin);
|
---|
372 | RT_ZERO(*pPfr);
|
---|
373 | return VERR_INVALID_PARAMETER;
|
---|
374 | }
|
---|