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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
/*
* ProFTPD - mod_proxy URI implementation
* Copyright (c) 2012-2020 TJ Saunders
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, TJ Saunders and other respective copyright holders
* give permission to link this program with OpenSSL, and distribute the
* resulting executable, without including the source code for OpenSSL in the
* source distribution.
*/
#include "mod_proxy.h"
#include "proxy/uri.h"
/* Relevant RFCs:
*
* RFC 1738: Uniform Resource Locators (obsolete)
* RFC 3986: Uniform Resource Identifier - Generic Syntax
*/
static const char *trace_channel = "proxy.uri";
static char *uri_parse_host(pool *p, const char *orig_uri,
const char *uri, char **remaining) {
char *host = NULL, *ptr = NULL;
/* We have either of:
*
* host<:...>
* [host]<:...>
*
* Look for an opening square bracket, to see if we have an IPv6 address
* in the URI.
*/
if (uri[0] == '[') {
ptr = strchr(uri + 1, ']');
if (ptr == NULL) {
/* If there is no ']', then it's a badly-formatted URI. */
pr_trace_msg(trace_channel, 4,
"badly formatted IPv6 address in host info '%.100s'", orig_uri);
errno = EINVAL;
return NULL;
}
host = pstrndup(p, uri + 1, ptr - uri - 1);
if (remaining != NULL) {
size_t urilen;
urilen = strlen(ptr);
if (urilen > 0) {
*remaining = ptr + 1;
} else {
*remaining = NULL;
}
}
pr_trace_msg(trace_channel, 17, "parsed host '%s' out of URI '%s'", host,
orig_uri);
return host;
}
ptr = strchr(uri + 1, ':');
if (ptr == NULL) {
if (remaining != NULL) {
*remaining = NULL;
}
host = pstrdup(p, uri);
pr_trace_msg(trace_channel, 17, "parsed host '%s' out of URI '%s'", host,
orig_uri);
return host;
}
if (remaining != NULL) {
*remaining = ptr;
}
host = pstrndup(p, uri, ptr - uri);
pr_trace_msg(trace_channel, 17, "parsed host '%s' out of URI '%s'", host,
orig_uri);
return host;
}
/* Determine whether "username:password@" are present. If so, then parse it
* out, and return a pointer to the portion of the URI after the parsed-out
* userinfo.
*/
static char *uri_parse_userinfo(pool *p, const char *orig_uri,
const char *uri, char **username, char **password) {
char *ptr, *ptr2, *rem_uri = NULL, *userinfo, *user = NULL, *passwd = NULL;
/* We have either:
*
* host<:...>
* [host]<:...>
*
* thus no user info, OR:
*
* username:password@host...
* username:password@[host]...
* username:@host...
* username:pass@word@host...
* user@domain.com:pass@word@host...
*
* all of which have at least one occurrence of the '@' character.
*/
ptr = strchr(uri, '@');
if (ptr == NULL) {
/* No '@' character at all? No user info, then. */
if (username != NULL) {
*username = NULL;
}
if (password != NULL) {
*password = NULL;
}
return pstrdup(p, uri);
}
/* To handle the case where the password field might itself contain an
* '@' character, we first search from the end for '@'. If found, then we
* search for '@' from the beginning. If also found, AND if both ocurrences
* are the same, then we have a plain "username:password@" string.
*
* Note that we can handle '@' characters within passwords (or usernames),
* but we currently cannot handle ':' characters within usernames.
*/
ptr2 = strrchr(uri, '@');
if (ptr2 != NULL) {
if (ptr != ptr2) {
/* Use the last found '@' as the delimiter. */
ptr = ptr2;
}
}
userinfo = pstrndup(p, uri, ptr - uri);
rem_uri = ptr + 1;
ptr = strchr(userinfo, ':');
if (ptr == NULL) {
pr_trace_msg(trace_channel, 4,
"badly formatted userinfo '%.100s' (missing ':' character) in "
"URI '%.100s', ignoring", userinfo, orig_uri);
if (username != NULL) {
*username = NULL;
}
if (password != NULL) {
*password = NULL;
}
return rem_uri;
}
user = pstrndup(p, userinfo, ptr - userinfo);
if (username != NULL) {
*username = user;
}
/* Watch for empty passwords. */
if (*(ptr+1) == '\0') {
passwd = pstrdup(p, "");
} else {
passwd = pstrdup(p, ptr + 1);
}
if (password != NULL) {
*password = passwd;
}
pr_trace_msg(trace_channel, 17,
"parsed username '%s', password '%s' out of URI '%s'", user, passwd,
orig_uri);
return rem_uri;
}
int proxy_uri_parse(pool *p, const char *uri, char **scheme, char **host,
unsigned int *port, char **username, char **password) {
char *ptr, *ptr2;
size_t idx, len;
if (uri == NULL ||
scheme == NULL ||
host == NULL ||
port == NULL) {
errno = EINVAL;
return -1;
}
/* First, look for a ':' */
ptr = strchr(uri, ':');
if (ptr == NULL) {
pr_trace_msg(trace_channel, 4, "missing colon in URI '%.100s'", uri);
errno = EINVAL;
return -1;
}
len = (ptr - uri);
*scheme = pstrndup(p, uri, len);
idx = strspn(*scheme, "abcdefghijklmnopqrstuvwxyz+.-");
if (idx < len &&
(*scheme)[idx] != '\0') {
/* Invalid character in the scheme string, according to RFC 1738 rules. */
pr_trace_msg(trace_channel, 4,
"invalid character (%c) at index %lu in scheme '%.100s'", (*scheme)[idx],
(unsigned long) idx, *scheme);
errno = EINVAL;
return -1;
}
/* The double-slashes must immediately follow the colon. */
if (*(ptr + 1) != '/' ||
*(ptr + 2) != '/') {
pr_trace_msg(trace_channel, 4,
"missing required '//' following colon in URI '%.100s'", uri);
errno = EINVAL;
return -1;
}
ptr += 3;
if (*ptr == '\0') {
/* The given URL looked like "scheme://". */
pr_trace_msg(trace_channel, 4,
"missing required authority following '//' in URI '%.100s'", uri);
errno = EINVAL;
return -1;
}
/* Possible URIs at this point:
*
* scheme://host:port/path/...
* scheme://host:port/
* scheme://host:port
* scheme://host
* scheme://username:password@host...
*
* And, in the case where 'host' is an IPv6 address:
*
* scheme://[host]:port/path/...
* scheme://[host]:port/
* scheme://[host]:port
* scheme://[host]
* scheme://username:password@[host]...
*/
/* We explicitly do NOT support URL-encoded characters in the URIs we
* will handle.
*/
ptr2 = strchr(ptr, '%');
if (ptr2 != NULL) {
pr_trace_msg(trace_channel, 4,
"invalid character (%%) at index %ld in scheme-specific info '%.100s'",
(long) (ptr2 - ptr), ptr);
errno = EINVAL;
return -1;
}
ptr = uri_parse_userinfo(p, uri, ptr, username, password);
ptr2 = strchr(ptr, ':');
if (ptr2 == NULL) {
*host = uri_parse_host(p, uri, ptr, NULL);
if (strcmp(*scheme, "ftp") == 0 ||
strcmp(*scheme, "ftps") == 0) {
*port = 21;
} else if (strcmp(*scheme, "sftp") == 0) {
*port = 22;
} else {
if (pr_strnrstr(*scheme, 0, "+srv", 0, PR_STR_FL_IGNORE_CASE) != TRUE &&
pr_strnrstr(*scheme, 0, "+txt", 0, PR_STR_FL_IGNORE_CASE) != TRUE) {
pr_trace_msg(trace_channel, 4,
"unable to determine port for scheme '%.100s'", *scheme);
errno = EINVAL;
return -1;
}
}
} else {
*host = uri_parse_host(p, uri, ptr, &ptr2);
}
/* Optional port field present? */
if (ptr2 != NULL) {
ptr2 = strchr(ptr2, ':');
}
if (ptr2 == NULL) {
/* XXX How to configure "implicit" FTPS, if at all? */
if (strcmp(*scheme, "ftp") == 0 ||
strcmp(*scheme, "ftps") == 0) {
*port = 21;
} else if (strcmp(*scheme, "sftp") == 0) {
*port = 22;
} else {
if (pr_strnrstr(*scheme, 0, "+srv", 0, PR_STR_FL_IGNORE_CASE) != TRUE &&
pr_strnrstr(*scheme, 0, "+txt", 0, PR_STR_FL_IGNORE_CASE) != TRUE) {
pr_trace_msg(trace_channel, 4,
"unable to determine port for scheme '%.100s'", *scheme);
errno = EINVAL;
return -1;
}
}
} else {
register unsigned int i;
char *ptr3, *portspec;
size_t portspeclen;
/* Look for any possible trailing '/'. */
ptr3 = strchr(ptr2, '/');
if (ptr3 == NULL) {
portspec = ptr2 + 1;
portspeclen = strlen(portspec);
} else {
portspeclen = ptr3 - (ptr2 + 1);
portspec = pstrndup(p, ptr2 + 1, portspeclen);
}
/* Ensure that only numeric characters appear in the portspec. */
for (i = 0; i < portspeclen; i++) {
if (isdigit((int) portspec[i]) == 0) {
pr_trace_msg(trace_channel, 4,
"invalid character (%c) at index %d in port specification '%.100s'",
portspec[i], i, portspec);
errno = EINVAL;
return -1;
}
}
/* The above check will rule out any negative numbers, since it will
* reject the minus character. Thus we only need to check for a zero
* port, or a number that's outside the 1-65535 range.
*/
*port = atoi(portspec);
if (*port == 0 ||
*port >= 65536) {
pr_trace_msg(trace_channel, 4,
"port specification '%.100s' yields invalid port number %d",
portspec, *port);
errno = EINVAL;
return -1;
}
}
/* We deliberately ignore any configured for SRV, TXT scheme variants.
* The ports to use will be obtained from the DNS records for such
* schemes.
*/
if (pr_strnrstr(*scheme, 0, "+srv", 0, PR_STR_FL_IGNORE_CASE) == TRUE ||
pr_strnrstr(*scheme, 0, "+txt", 0, PR_STR_FL_IGNORE_CASE) == TRUE) {
*port = 0;
}
return 0;
}
|