mod_proxy_ftp.c revision 47ff2654d827dd3596ce2e4099d69cec0f1009b9
/* Automatic timestamping (Last-Modified header) based on MDTM is used if: * 1) the FTP server supports the MDTM command and * 2) HAVE_TIMEGM (preferred) or HAVE_GMTOFF is available at compile time /* Put these in the dir config so they work inside <Location> */ /* Put these in the dir config so they work inside <Location> */ * Decodes a '%' escaped string, and returns the number of characters return 0;
/* special case for no characters */ for (i = 0, j = 0; x[i] !=
'\0'; i++, j++) {
/* decode it if not already done */ * Escape the globbing characters in a path used as argument to * the FTP commands (SIZE, CWD, RETR, MDTM, ...). * ftpd assumes '\\' as a quoting character to escape special characters. * Just returns the original string if ProxyFtpEscapeWildcards has been * Returns: escaped string * Check for globbing characters in a path used as argument to * the FTP commands (SIZE, CWD, RETR, MDTM, ...). * ftpd assumes '\\' as a quoting character to escape special characters. * Returns: 0 (no globbing chars, or all globbing chars escaped), 1 (globbing chars) * checks an encoded ftp string for bad characters, namely, CR, LF or for (i = 0; x[i] !=
'\0'; i++) {
if (
ch ==
'\015' ||
ch ==
'\012' || (
ch &
0x80))
#
else /* APR_CHARSET_EBCDIC */ if (
ch ==
'\r' ||
ch ==
'\n')
#
endif /* APR_CHARSET_EBCDIC */ "proxy: FTP: canonicalising URL %s",
url);
* N.B. if this isn't a true proxy request, then the URL path (but not * query args) has already been decoded. This gives rise to the problem * of a ; being decoded into the path. /* we chop lines longer than 80 characters */ * Reads response lines, returns both the ftp status code and * remembers the response message in the supplied buffer ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL, "proxy: <FTP: %s", response); /* this is a filter that turns a raw ASCII directory listing into pretty HTML */ /* ideally, mod_proxy should simply send the raw directory list up the filter * stack to mod_autoindex, which in theory should turn the raw ascii into * pretty html along with all the bells and whistles it provides... * all in good time...! :) /* fallback regex for ls -s1; ($0..$2) == 3 */ /* combine the stored and the new */ /* basedir is either "", or "/%2f" for the "squid %2f hack" */ const char *
basedir =
"";
/* By default, path is relative to the $HOME dir */ * In the reverse proxy case we need to construct our site string * via ap_construct_url. For non anonymous sites apr_uri_unparse would * only supply us with 'username@' which leads to the construction of * an invalid base href later on. Losing the username part of the URL * is no problem in the reverse proxy case as the browser sents the * credentials anyway once entered. /* Save "scheme://site" prefix without password */ /* ... and path without query args */ /* If path began with /%2f, change the basedir */ /* Strip off a type qualifier. It is ignored for dir listings */ while (
path[
1] ==
'/')
/* collapse multiple leading slashes to one */ reldir[0] =
'\0';
/* strip off the wildcard suffix */ /* Copy path, strip (all except the last) trailing slashes */ /* (the trailing slash is needed for the dir component loop below) */ /* Add a link to the root directory (if %2f hack was used) */ str = (
basedir[0] !=
'\0') ?
"<a href=\"/%2f/\">%2f</a>/" :
"";
"<html>\n <head>\n <title>%s%s%s</title>\n" "<base href=\"%s%s%s\">\n" " <body>\n <h2>Directory of " "<a href=\"/\">%s</a>/%s",
/* print "path/" component */ /* If the caller has determined the current directory, and it differs */ /* from what the client requested, then show the real name */ /* make sure page intro gets sent out */ /* loop through each line of directory */ /* Compile the output format of "ls -s1" as a fallback for non-unix ftp listings */ /* get a complete line */ /* if the buffer overruns - throw data away */ /* len+1 to leave space for the trailing nil char */ /* EOS? jump to footer */ /* not complete? leave and try get some more */ if (
ctx->
buffer[n-
1] ==
CRLF[0])
/* strip trailing '\r' if present */ /* Line is broken. Ignore it. */ "proxy_ftp: could not parse line %s",
ctx->
buffer);
/* erase buffer for next time around */ continue;
/* while state is BODY */ /* handle filenames with spaces in 'em */ /* Append a slash to the HREF link for directories */ /* Try a fallback for listings in the format of "ls -s1" */ /* erase buffer for next time around */ /* Parse EPSV reply and return port, or zero on error. */ /* Reply syntax per RFC 2428: "229 blah blah (|||port|)" where '|' * can be any character in ASCII from 33-126, obscurely. Verify if (p ==
NULL || !p[
1] || p[
1] != p[
2] || p[
1] != p[
3]
* Generic "send FTP command to server" routine, using the control socket. * Returns the FTP returncode (3 digit code) * Allows for tracing the FTP protocol (in LogLevel debug) /* If cmd == NULL, we retrieve the next ftp response line */ /* strip off the CRLF for logging */ if (
rc == -
1 ||
rc ==
421)
/* Set ftp server to TYPE {A,I,E} before transfer of a directory or file */ char old_type[
2] = {
'A',
'\0' };
/* After logon, mode is ASCII */ /* responses: 200, 421, 500, 501, 504, 530 */ /* 421 Service not available, closing control connection. */ /* 500 Syntax error, command unrecognized. */ /* 501 Syntax error in parameters or arguments. */ /* 504 Command not implemented for that parameter. */ if (
rc == -
1 ||
rc ==
421) {
"Error reading from remote server");
else if (
rc !=
200 &&
rc !=
504) {
"Unable to set transfer type");
/* Allow not implemented */ /* ignore it silently */;
/* Return the current directory which we have selected on the FTP server, or NULL */ /* responses: 257, 500, 501, 502, 421, 550 */ /* 257 "<directory-name>" <commentary> */ /* 421 Service not available, closing control connection. */ /* 500 Syntax error, command unrecognized. */ /* 501 Syntax error in parameters or arguments. */ /* 502 Command not implemented. */ /* 550 Requested action not taken. */ "Failed to read PWD on ftp server");
/* Common routine for failed authorization (i.e., missing or wrong password) * to an ftp service. This causes most browsers to retry the request * with username and password (which was presumably queried from the user) * supplied in the Authorization: header. * Note that we "invent" a realm name which consists of the * ftp://user@host part of the reqest (sans password -if supplied but invalid-) "proxy: missing or failed auth to %s",
* Handles direct access of ftp:// URLs * Original (Non-PASV) version from * Troy Morrison <spiffnet@zoom.com> * Filters by [Graham Leggett <minfrin@sharp.fm>] /* char *account = NULL; how to supply an account in a URL? */ char xfer_type =
'A';
/* after ftp login, the default is ASCII */ /* stuff for PASV mode */ "proxy: FTP: declining URL %s - proxyhost %s specified:",
url,
proxyhost);
return DECLINED;
/* proxy connections are via HTTP */ "proxy: FTP: declining URL %s - not ftp:",
url);
return DECLINED;
/* only interested in FTP */ "proxy: FTP: serving URL %s",
url);
* I: Who Do I Connect To? ----------------------- * Break up the URL to determine the host to connect to /* we only support GET and HEAD */ /* We break the URL into host, port, path-search */ /* "type=d" forces a dir listing. * The other types (i|a|e) are directly used for the ftp TYPE command /* Check valid types, rather than ignoring invalid types silently: */ "ftp proxy supports only types 'a', 'i', or 'e': \"",
/* make binary transfers the default */ * The "Authorization:" header must be checked first. We allow the user * to "override" the URL-coded user [ & password ] in the Browsers' * User&Password Dialog. NOTE that this is only marginally more secure * than having the password travel in plain as part of the URL, because * Basic Auth simply uuencodes the plain text password. But chances are * still smaller that the URL is logged regularly. /* Check the decoded string for special characters. */ "user credentials contained invalid character");
* Note that this allocation has to be made from r->connection->pool * because it has the lifetime of the connection. The other * allocations are temporary and can be tossed away any time. /* do a DNS lookup for the destination host */ * get all the possible IP addresses for the destname and loop through * them until we get a successful connection "DNS lookup failure for: ",
/* check if ProxyBlock directive on this host */ "Connect to remote machine blocked");
/* create space for state information */ /* TODO: see if ftp could use determine_connection */ * II: Make the Connection ----------------------- * We have determined who to connect to. Now make the connection. "proxy: FTP: an error occurred creating a new connection to %pI (%s)",
"proxy: FTP: control connection complete");
* III: Send Control Request ------------------------- * Log into the ftp server, send the username & password, change to the /* 120 Service ready in nnn minutes. */ /* 220 Service ready for new user. */ /* 421 Service not available, closing control connection. */ if (
rc == -
1 ||
rc ==
421) {
* RFC2616 states: 14.37 Retry-After * The Retry-After response-header field can be used with a 503 (Service * Unavailable) response to indicate how long the service is expected * to be unavailable to the requesting client. [...] The value of * this field can be either an HTTP-date or an integer number of * seconds (in decimal) after the time of the response. Retry-After * = "Retry-After" ":" ( HTTP-date | delta-seconds ) /* Look for a number, preceded by whitespace */ /* possible results; 230, 331, 332, 421, 500, 501, 530 */ /* states: 1 - error, 2 - success; 3 - send password, 4,5 fail */ /* 230 User logged in, proceed. */ /* 331 User name okay, need password. */ /* 332 Need account for login. */ /* 421 Service not available, closing control connection. */ /* 500 Syntax error, command unrecognized. */ /* (This may include errors such as command line too long.) */ /* 501 Syntax error in parameters or arguments. */ if (
rc == -
1 ||
rc ==
421) {
if (
rc !=
230 &&
rc !=
331) {
if (
rc ==
331) {
/* send password */ /* possible results 202, 230, 332, 421, 500, 501, 503, 530 */ /* 230 User logged in, proceed. */ /* 332 Need account for login. */ /* 421 Service not available, closing control connection. */ /* 500 Syntax error, command unrecognized. */ /* 501 Syntax error in parameters or arguments. */ /* 503 Bad sequence of commands. */ if (
rc == -
1 ||
rc ==
421) {
"Error reading from remote server");
/* @@@ questionable -- we might as well return a 403 Forbidden here */ if (
rc !=
230 &&
rc !=
202) {
/* Special handling for leading "%2f": this enforces a "cwd /" * out of the $HOME directory which was the starting point after login while (*
path ==
'/')
/* skip leading '/' (after root %2f) */ if (
rc == -
1 ||
rc ==
421)
"Error reading from remote server");
* set the directory (walk directory component by component): this is * what we must do if we don't know the OS type of the remote machine if (
strchr(
path,
'/')) {
/* are there now any '/' characters? */ "Use of /%2f is only allowed at the base directory");
/* NOTE: FTP servers do globbing on the path. * So we need to escape the URI metacharacters. * We use a special glob-escaping routine to escape globbing chars. * We could also have extended gen_test_char.c with a special T_ESCAPE_FTP_PATH /* responses: 250, 421, 500, 501, 502, 530, 550 */ /* 250 Requested file action okay, completed. */ /* 421 Service not available, closing control connection. */ /* 500 Syntax error, command unrecognized. */ /* 501 Syntax error in parameters or arguments. */ /* 502 Command not implemented. */ /* 550 Requested action not taken. */ if (
rc == -
1 ||
rc ==
421) {
"Error reading from remote server");
* IV: Make Data Connection? ------------------------- * Try EPSV, if that fails... try PASV, if that fails... try PORT. /* this temporarily switches off EPSV/PASV */ /* set up data connection - EPSV */ * The EPSV command replaces PASV where both IPV4 and IPV6 is * supported. Only the port is returned, the IP address is always the * same as that on the control connection. Example: Entering Extended * Passive Mode (|||6446|) /* possible results: 227, 421, 500, 501, 502, 530 */ /* 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2). */ /* 421 Service not available, closing control connection. */ /* 500 Syntax error, command unrecognized. */ /* 501 Syntax error in parameters or arguments. */ /* 502 Command not implemented. */ if (
rc == -
1 ||
rc ==
421) {
"Error reading from remote server");
if (
rc !=
229 &&
rc !=
500 &&
rc !=
501 &&
rc !=
502) {
/* Parse the port out of the EPSV reply. */ "proxy: FTP: EPSV contacting remote host on port %d",
"proxy: FTP: error creating EPSV socket");
"proxy: FTP: apr_socket_opt_set(SO_RCVBUF): Failed to set ProxyReceiveBufferSize, using default");
"apr_socket_opt_set(APR_TCP_NODELAY): Failed to set");
/* make the connection */ /* set up data connection - PASV */ /* possible results: 227, 421, 500, 501, 502, 530 */ /* 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2). */ /* 421 Service not available, closing control connection. */ /* 500 Syntax error, command unrecognized. */ /* 501 Syntax error in parameters or arguments. */ /* 502 Command not implemented. */ if (
rc == -
1 ||
rc ==
421) {
"Error reading from remote server");
if (
rc !=
227 &&
rc !=
502) {
/* FIXME: Check PASV against RFC1123 */ /* FIXME: Only supports IPV4 - fix in RFC2428 */ "%d,%d,%d,%d,%d,%d", &
h3, &
h2, &
h1, &
h0, &
p1, &
p0) ==
6)) {
"proxy: FTP: PASV contacting host %d.%d.%d.%d:%d",
"proxy: error creating PASV socket");
"proxy: FTP: apr_socket_opt_set(SO_RCVBUF): Failed to set ProxyReceiveBufferSize, using default");
"apr_socket_opt_set(APR_TCP_NODELAY): Failed to set");
/* make the connection */ apr_sockaddr_info_get(&
pasv_addr,
apr_psprintf(p,
"%d.%d.%d.%d",
h3,
h2,
h1,
h0),
connect_addr->
family,
pasvport, 0, p);
/* set up data connection - PORT */ "proxy: FTP: error creating local socket");
#
ifndef _OSD_POSIX /* BS2000 has this option "always on" */ "proxy: FTP: error setting reuseaddr option");
"proxy: FTP: error binding to ftp data socket %pI",
local_addr);
/* only need a short queue */ "proxy: FTP: error listening to ftp data socket %pI",
local_addr);
/* FIXME: Sent PORT here */ "%d.%d.%d.%d", &
h3, &
h2, &
h1, &
h0) ==
4)) {
rc =
proxy_ftp_command(
apr_psprintf(p,
"PORT %d,%d,%d,%d,%d,%d" CRLF,
h3,
h2,
h1,
h0,
p1,
p0),
/* possible results: 200, 421, 500, 501, 502, 530 */ /* 421 Service not available, closing control connection. */ /* 500 Syntax error, command unrecognized. */ /* 501 Syntax error in parameters or arguments. */ /* 502 Command not implemented. */ if (
rc == -
1 ||
rc ==
421) {
"Error reading from remote server");
/* signal that we must use the EPRT/PORT loop */ * The EPRT command replaces PORT where both IPV4 and IPV6 is supported. The first * number (1,2) indicates the protocol type. Examples: * EPRT |1|132.235.1.2|6275| * EPRT |2|1080::8:800:200C:417A|5282| "Connect to IPV6 ftp server using EPRT not supported. Enable EPSV.");
* V: Set The Headers ------------------- * Get the size of the request, set up the environment for HTTP. /* set request; "path" holds last path component */ if (
strchr(
path,
'/')) {
/* are there now any '/' characters? */ "Use of /%2f is only allowed at the base directory");
/* If len == 0 then it must be a directory (you can't RETR nothing) * Also, don't allow to RETR by wildcard. Instead, create a dirlisting, * unless ProxyFtpListOnWildcard is off. * SIZE is not in RFC959, but Postel has blessed it and * it will be in the updated RFC. * Return size of file in a format suitable for * using with RESTART (we just count bytes). * change depending on the current STRUcture, MODE and TYPE of the data * connection, or a data connection which would be created were one * created now. Thus, the result of the SIZE command is dependent on * the currently established STRU, MODE and TYPE parameters. /* Therefore: switch to binary if the user did not specify ";type=a" */ if (
rc == -
1 ||
rc ==
421) {
"Error reading from remote server");
else if (
rc ==
213) {
/* Size command ok */ else if (
rc ==
550) {
/* Not a regular file */ "proxy: FTP: SIZE shows this is a directory");
/* possible results: 250, 421, 500, 501, 502, 530, 550 */ /* 250 Requested file action okay, completed. */ /* 421 Service not available, closing control connection. */ /* 500 Syntax error, command unrecognized. */ /* 501 Syntax error in parameters or arguments. */ /* 502 Command not implemented. */ /* 550 Requested action not taken. */ if (
rc == -
1 ||
rc ==
421) {
"Error reading from remote server");
/* If the current directory contains no slash, we are talking to * a non-unix ftp system. Try LIST instead of "LIST -lag", it * should return a long listing anyway (unlike NLST). * Some exotic FTP servers might choke on the "-lag" switch. /* Note that we do not escape the path here, to allow for /* switch to binary if the user did not specify ";type=a" */ * The FTP command, MODIFICATION TIME (MDTM), can be used to determine * when a file in the server NVFS was last modified. <..> * The syntax of a time value is: * time-val = 14DIGIT [ "." 1*DIGIT ] <..> * Symbolically, a time-val may be viewed as * The "." and subsequent digits ("sss") are optional. <..> * Time values are always represented in UTC (GMT) /* then extract the Last-Modified time from it (YYYYMMDDhhmmss or YYYYMMDDhhmmss.xxx GMT). */ /* mktime will subtract the local timezone, which is not what we want. * Add it again because the MDTM string is GMT /* FIXME: Handle range requests - send REST */ /* rc is an intermediate response for the LIST or RETR commands */ * RETR: 110, 125, 150, 226, 250, 421, 425, 426, 450, 451, 500, 501, 530, * 550 NLST: 125, 150, 226, 250, 421, 425, 426, 450, 451, 500, 501, 502, /* 110 Restart marker reply. */ /* 125 Data connection already open; transfer starting. */ /* 150 File status okay; about to open data connection. */ /* 226 Closing data connection. */ /* 250 Requested file action okay, completed. */ /* 421 Service not available, closing control connection. */ /* 425 Can't open data connection. */ /* 426 Connection closed; transfer aborted. */ /* 450 Requested file action not taken. */ /* 451 Requested action aborted. Local error in processing. */ /* 500 Syntax error, command unrecognized. */ /* 501 Syntax error in parameters or arguments. */ /* 550 Requested action not taken. */ if (
rc == -
1 ||
rc ==
421) {
"Error reading from remote server");
"proxy: FTP: RETR failed, trying LIST instead");
/* Directory Listings should always be fetched in ASCII mode */ /* possible results: 250, 421, 500, 501, 502, 530, 550 */ /* 250 Requested file action okay, completed. */ /* 421 Service not available, closing control connection. */ /* 500 Syntax error, command unrecognized. */ /* 501 Syntax error in parameters or arguments. */ /* 502 Command not implemented. */ /* 550 Requested action not taken. */ if (
rc == -
1 ||
rc ==
421) {
"Error reading from remote server");
/* Update current directory after CWD */ /* See above for the "LIST" vs. "LIST -lag" discussion. */ /* rc is an intermediate response for the LIST command (125 transfer starting, 150 opening data connection) */ if (
rc == -
1 ||
rc ==
421)
"Error reading from remote server");
if (
rc !=
125 &&
rc !=
150 &&
rc !=
226 &&
rc !=
250) {
/* We "trust" the ftp server to really serve (size) bytes... */ "proxy: FTP: Content-Length set to %s",
size);
"proxy: FTP: Last-Modified set to %s",
datestr);
/* If an encoding has been set by mistake, delete it. * @@@ the encoding is currently set to x-gzip) /* set content-encoding (not for dir listings, they are uncompressed)*/ /* wait for connection */ "proxy: FTP: failed to accept data connection");
/* the transfer socket is now open, create a new connection */ * the peer reset the connection already; ap_run_create_connection() closed "proxy: FTP: an error occurred creating the transfer connection");
* We do not do SSL over the data connection, even if the virtual host we * are in might have SSL enabled /* set up the connection filters */ "proxy: FTP: pre_connection setup failed (%d)",
* VI: Receive the Response ------------------------ * Get response from the remote ftp socket, and pass it up the filter chain. /* insert directory filter */ "proxy: FTP: start body send");
/* read the body, pass it to the output filters */ r->
server,
"proxy (PID %d): readbytes: %#x",
/* found the last brigade? */ /* if this is the last brigade, cleanup the * backend connection first to prevent the * backend server from hanging around waiting * for a slow client to eat these bytes "proxy: FTP: data connection closed");
/* signal that we must leave */ /* if no EOS yet, then we must flush */ /* try send what we read */ /* Ack! Phbtt! Die! User aborted! */ /* make sure we always clean up after ourselves */ /* if we are done, leave */ "proxy: FTP: end body send");
"proxy: FTP: data connection closed");
/* Retrieve the final response for the RETR or LIST commands */ * VII: Clean Up ------------- * If there are no KeepAlives, or if the connection has been signalled to * close, close the socket and clean up /* responses: 221, 500 */ /* 221 Service closing control connection. */ /* 500 Syntax error, command unrecognized. */ RSRC_CONF|
ACCESS_CONF,
"Whether wildcard characters in a path cause mod_proxy_ftp to list the files instead of trying to get them. Defaults to on."),
RSRC_CONF|
ACCESS_CONF,
"Whether the proxy should escape wildcards in paths before sending them to the FTP server. Defaults to on, but most FTP servers will need it turned off if you need to manage paths that contain wildcard characters."),
NULL,
/* create per-server config structure */ NULL,
/* merge per-server config structures */