proxy_ftp.c revision 05b232c7bd0b185d329bab8a30eefe1ca38bd6b8
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* ====================================================================
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * The Apache Software License, Version 1.1
bc8fd1b0b1afdf89b8d28eefa8cd74e26ba97986fielding * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * reserved.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Redistribution and use in source and binary forms, with or without
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * modification, are permitted provided that the following conditions
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * 1. Redistributions of source code must retain the above copyright
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * notice, this list of conditions and the following disclaimer.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * 2. Redistributions in binary form must reproduce the above copyright
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * notice, this list of conditions and the following disclaimer in
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * the documentation and/or other materials provided with the
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * distribution.
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * 3. The end-user documentation included with the redistribution,
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * if any, must include the following acknowledgment:
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * "This product includes software developed by the
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * Apache Software Foundation (http://www.apache.org/)."
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * Alternately, this acknowledgment may appear in the software itself,
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * if and wherever such third-party acknowledgments normally appear.
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * 4. The names "Apache" and "Apache Software Foundation" must
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * not be used to endorse or promote products derived from this
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * software without prior written permission. For written
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * permission, please contact apache@apache.org.
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * 5. Products derived from this software may not be called "Apache",
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * nor may "Apache" appear in their name, without prior written
64185f9824e42f21ca7b9ae6c004484215c031a7rbb * permission of the Apache Software Foundation.
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * SUCH DAMAGE.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * ====================================================================
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * This software consists of voluntary contributions made by many
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * individuals on behalf of the Apache Software Foundation. For more
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * information on the Apache Software Foundation, please see
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * Portions of this software are based upon public domain software
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * originally written at the National Center for Supercomputing Applications,
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * University of Illinois, Urbana-Champaign.
3568de757bac0b47256647504c186d17ca272f85rbb/* FTP routines for Apache proxy */
3568de757bac0b47256647504c186d17ca272f85rbb * Decodes a '%' escaped string, and returns the number of characters
3568de757bac0b47256647504c186d17ca272f85rbbstatic int decodeenc(char *x)
3568de757bac0b47256647504c186d17ca272f85rbb int i, j, ch;
3568de757bac0b47256647504c186d17ca272f85rbb if (x[0] == '\0')
3568de757bac0b47256647504c186d17ca272f85rbb return 0; /* special case for no characters */
3568de757bac0b47256647504c186d17ca272f85rbb for (i = 0, j = 0; x[i] != '\0'; i++, j++) {
3568de757bac0b47256647504c186d17ca272f85rbb/* decode it if not already done */
3568de757bac0b47256647504c186d17ca272f85rbb if (ch == '%' && ap_isxdigit(x[i + 1]) && ap_isxdigit(x[i + 2])) {
3568de757bac0b47256647504c186d17ca272f85rbb x[j] = '\0';
3568de757bac0b47256647504c186d17ca272f85rbb * checks an encoded ftp string for bad characters, namely, CR, LF or
3568de757bac0b47256647504c186d17ca272f85rbb * non-ascii character
3568de757bac0b47256647504c186d17ca272f85rbbstatic int ftp_check_string(const char *x)
3568de757bac0b47256647504c186d17ca272f85rbb int i, ch = 0;
3568de757bac0b47256647504c186d17ca272f85rbb for (i = 0; x[i] != '\0'; i++) {
3568de757bac0b47256647504c186d17ca272f85rbb if (ch == '%' && ap_isxdigit(x[i + 1]) && ap_isxdigit(x[i + 2])) {
3568de757bac0b47256647504c186d17ca272f85rbb#else /*APR_CHARSET_EBCDIC*/
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick if (ch == '\r' || ch == '\n' || (os_toascii[ch] & 0x80))
3568de757bac0b47256647504c186d17ca272f85rbb#endif /*APR_CHARSET_EBCDIC*/
333eac96e4fb7d6901cb75e6ca7bb22b2ccb84cetrawick * Canonicalise ftp URLs.
3568de757bac0b47256647504c186d17ca272f85rbb char *user, *password, *host, *path, *parms, *strp, sport[7];
3568de757bac0b47256647504c186d17ca272f85rbb const char *err;
3568de757bac0b47256647504c186d17ca272f85rbb err = ap_proxy_canon_netloc(p, &url, &user, &password, &host, &port);
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (password != NULL && !ftp_check_string(password))
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz/* now parse path/parameters args, according to rfc1738 */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz/* N.B. if this isn't a true proxy request, then the URL path
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * (but not query args) has already been decoded.
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz * This gives rise to the problem of a ; being decoded into the
3d96ee83babeec32482c9082c9426340cee8c44dwrowe parms = ap_proxy_canonenc(p, strp, strlen(strp), enc_parm,
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz path = ap_proxy_canonenc(p, url, strlen(url), enc_path, r->proxyreq);
3568de757bac0b47256647504c186d17ca272f85rbb strp = ap_proxy_canonenc(p, r->args, strlen(r->args), enc_parm, 1);
3568de757bac0b47256647504c186d17ca272f85rbb strp = ap_proxy_canonenc(p, r->args, strlen(r->args), enc_fpath, 1);
3568de757bac0b47256647504c186d17ca272f85rbb/* now, rebuild URL */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz apr_snprintf(sport, sizeof(sport), ":%d", port);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding r->filename = apr_pstrcat(p, "proxy:ftp://", (user != NULL) ? user : "",
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* we chop lines longer than 80 characters */
fc1efab92032301e317f07e1b3a00082d9d71f3frbb * Reads response lines, returns both the ftp status code and
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz * remembers the response message in the supplied buffer
3568de757bac0b47256647504c186d17ca272f85rbbstatic int ftp_getrc_msg(conn_rec *c, apr_bucket_brigade *bb, char *msgbuf, int msglen)
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (APR_SUCCESS != (rv = ap_proxy_string_read(c, bb, response, sizeof(response), &eos))) {
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (!apr_isdigit(response[0]) || !apr_isdigit(response[1]) ||
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz !apr_isdigit(response[2]) || (response[3] != ' ' && response[3] != '-'))
3568de757bac0b47256647504c186d17ca272f85rbb status = 100 * response[0] + 10 * response[1] + response[2] - 111 * '0';
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (APR_SUCCESS != (rv = ap_proxy_string_read(c, bb, response, sizeof(response), &eos))) {
3568de757bac0b47256647504c186d17ca272f85rbb mb = apr_cpystrn(mb, response + (' ' == response[0] ? 1 : 4), me - mb);
3568de757bac0b47256647504c186d17ca272f85rbb/* this is a filter that turns a raw ASCII directory listing into pretty HTML */
3568de757bac0b47256647504c186d17ca272f85rbb/* ideally, mod_proxy should simply send the raw directory list up the filter
3568de757bac0b47256647504c186d17ca272f85rbb * stack to mod_autoindex, which in theory should turn the raw ascii into
3568de757bac0b47256647504c186d17ca272f85rbb * pretty html along with all the bells and whistles it provides...
3568de757bac0b47256647504c186d17ca272f85rbb * all in good time...! :)
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingtypedef struct {
6653a33e820463abd4f81915b7a1eba0f602e200brianpapr_status_t ap_proxy_send_dir_filter(ap_filter_t *f, apr_bucket_brigade *in)
6653a33e820463abd4f81915b7a1eba0f602e200brianp register int n;
6653a33e820463abd4f81915b7a1eba0f602e200brianp const char *pwd = apr_table_get(r->notes, "Directory-PWD");
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm const char *readme = apr_table_get(r->notes, "Directory-README");
ca53a74f4012a45cbad48e940eddf27d866981f9dougm /* combine the stored and the new */
6653a33e820463abd4f81915b7a1eba0f602e200brianp /* Save "scheme://site" prefix without password */
6653a33e820463abd4f81915b7a1eba0f602e200brianp site = ap_unparse_uri_components(p, &f->r->parsed_uri, UNP_OMITPASSWORD|UNP_OMITPATHINFO);
6653a33e820463abd4f81915b7a1eba0f602e200brianp /* ... and path without query args */
6653a33e820463abd4f81915b7a1eba0f602e200brianp path = ap_unparse_uri_components(p, &f->r->parsed_uri, UNP_OMITSITEPART|UNP_OMITQUERY);
6653a33e820463abd4f81915b7a1eba0f602e200brianp /* Copy path, strip (all except the last) trailing slashes */
6653a33e820463abd4f81915b7a1eba0f602e200brianp while ((n = strlen(path)) > 1 && path[n-1] == '/' && path[n-2] == '/')
6653a33e820463abd4f81915b7a1eba0f602e200brianp /* print "ftp://host/" */
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick "\n\n<HTML>\n<HEAD>\n<TITLE>%s%s</TITLE>\n"
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick "<BASE HREF=\"%s%s\">\n</HEAD>\n\n"
239f998fbee5ac5b114b965bb76e217cce0003edstoddard "<BODY>\n\n<H2>Directory of "
3568de757bac0b47256647504c186d17ca272f85rbb "<A HREF=\"/\">%s</A>/",
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* print "path/" component */
3568de757bac0b47256647504c186d17ca272f85rbb str = apr_psprintf(p, "<A HREF=\"/%s/\">%s</A>/", path+1, reldir);
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard /* If the caller has determined the current directory, and it differs */
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard /* from what the client requested, then show the real name */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (pwd == NULL || strncmp (pwd, path, strlen(pwd)) == 0) {
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz str = apr_psprintf(p, "</H2>\n\n<HR></HR>\n\n<PRE>");
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard str = apr_psprintf(p, "</H2>\n\n(%s)\n\n<HR></HR>\n\n<PRE>", pwd);
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz e = apr_bucket_pool_create(str, strlen(str), p);
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard /* print README */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz str = apr_psprintf(p, "%s\n</PRE>\n\n<HR></HR>\n\n<PRE>\n",
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard /* make sure page intro gets sent out */
e0d102c882a7ed34d3eec24b36da49f097066a36stoddard if (APR_SUCCESS != (rv = ap_pass_brigade(f->next, out))) {
3568de757bac0b47256647504c186d17ca272f85rbb /* loop through each line of directory */
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick /* get a complete line */
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm if (APR_SUCCESS != (rv = apr_bucket_read(e, (const char **)&response, &len, APR_BLOCK_READ))) {
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard apr_cpystrn(ctx->buffer+strlen(ctx->buffer), response, len);
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm /* EOS? jump to footer */
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard /* not complete? leave and try get some more */
3568de757bac0b47256647504c186d17ca272f85rbb /* a symlink? */
3568de757bac0b47256647504c186d17ca272f85rbb if (ctx->buffer[0] == 'l' && (filename=strstr(ctx->buffer, " -> ")) != NULL) {
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard if ((n = strlen(link_ptr)) > 1 && link_ptr[n - 1] == '\n')
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard str = apr_psprintf(p, "%s <A HREF=\"%s\">%s %s</A>\n", ctx->buffer, filename, filename, link_ptr);
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard else if (ctx->buffer[0] == 'd' || ctx->buffer[0] == '-' || ctx->buffer[0] == 'l' || apr_isdigit(ctx->buffer[0])) {
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (apr_isdigit(ctx->buffer[0])) { /* handle DOS dir */
3568de757bac0b47256647504c186d17ca272f85rbb /* handle filenames with spaces in 'em */
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard if (!strcmp(filename, ".") || !strcmp(filename, "..") || firstfile) {
3568de757bac0b47256647504c186d17ca272f85rbb else if (searchidx != 0 && ctx->buffer[searchidx] != 0) {
3568de757bac0b47256647504c186d17ca272f85rbb /* Special handling for '.' and '..' */
3568de757bac0b47256647504c186d17ca272f85rbb if (!strcmp(filename, ".") || !strcmp(filename, "..") || ctx->buffer[0] == 'd') {
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard str = apr_psprintf(p, "%s <A HREF=\"%s/\">%s</A>\n",
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (APR_SUCCESS != (rv = ap_pass_brigade(f->next, out))) {
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard str = apr_psprintf(p, "</PRE>\n\n<HR></HR>\n\n%s\n\n</BODY>\n</HTML>\n", ap_psignature("", r));
3568de757bac0b47256647504c186d17ca272f85rbb if (APR_SUCCESS != (rv = ap_pass_brigade(f->next, out))) {
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard/* Common routine for failed authorization (i.e., missing or wrong password)
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard * to an ftp service. This causes most browsers to retry the request
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard * with username and password (which was presumably queried from the user)
3568de757bac0b47256647504c186d17ca272f85rbb * supplied in the Authorization: header.
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard * Note that we "invent" a realm name which consists of the
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz * ftp://user@host part of the reqest (sans password -if supplied but invalid-)
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz /* Log failed requests if they supplied a password
3568de757bac0b47256647504c186d17ca272f85rbb * (log username/password guessing attempts)
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_rerror(APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, 0, r,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy: missing or failed auth to %s",
3568de757bac0b47256647504c186d17ca272f85rbb * Handles direct access of ftp:// URLs
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard * Original (Non-PASV) version from
3568de757bac0b47256647504c186d17ca272f85rbb * Troy Morrison <spiffnet@zoom.com>
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard * PASV added by Chuck
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard * Filters by [Graham Leggett <minfrin@sharp.fm>]
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick/* char *account = NULL; how to supply an account in a URL? */
0f113d7123e8bd3e3e2e9b6373461a1a773bfccatrawick /* stuff for PASV mode */
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard (proxy_conn_rec *) ap_get_module_config(c->conn_config, &proxy_module);
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard backend = ap_pcalloc(c->pool, sizeof(proxy_conn_rec));
3568de757bac0b47256647504c186d17ca272f85rbb ap_set_module_config(c->conn_config, &proxy_module, backend);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * I: Who Do I Connect To?
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * -----------------------
f886987cd0bd4220c14043c4d9be77ec22902e73trawick * Break up the URL to determine the host to connect to
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* we only support GET and HEAD */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz /* We break the URL into host, port, path-search */
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm path = (path != NULL && path[0] != '\0') ? &path[1] : "";
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm /* The "Authorization:" header must be checked first.
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz * We allow the user to "override" the URL-coded user [ & password ]
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz * in the Browsers' User&Password Dialog.
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz * NOTE that this is only marginally more secure than having the
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm * password travel in plain as part of the URL, because Basic Auth
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm * simply uuencodes the plain text password.
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz * But chances are still smaller that the URL is logged regularly.
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if ((password = apr_table_get(r->headers_in, "Authorization")) != NULL
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm && strcasecmp(ap_getword(r->pool, &password, ' '), "Basic") == 0
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm && (password = ap_pbase64decode(r->pool, password))[0] != ':') {
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm /* Note that this allocation has to be made from r->connection->pool
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm * because it has the lifetime of the connection. The other allocations
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm * are temporary and can be tossed away any time.
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz user = ap_getword_nulls (r->connection->pool, &password, ':');
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r->server,
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick "proxy: FTP connecting %s to %s:%d", url, connectname, connectport);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* do a DNS lookup for the destination host */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick err = apr_sockaddr_info_get(&connect_addr, connectname, APR_UNSPEC, connectport, 0, p);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* check if ProxyBlock directive on this host */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick if (OK != ap_proxy_checkproxyblock(r, conf, connect_addr)) {
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick "Connect to remote machine blocked");
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * II: Make the Connection
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * -----------------------
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * We have determined who to connect to. Now make the connection.
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* get all the possible IP addresses for the destname and loop through them
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * until we get a successful connection
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick return ap_proxyerror(r, HTTP_BAD_GATEWAY, apr_pstrcat(p,
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick "DNS lookup failure for: ",
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick if ((apr_socket_create(&sock, APR_INET, SOCK_STREAM, r->pool)) != APR_SUCCESS) {
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick "proxy: error creating socket");
f886987cd0bd4220c14043c4d9be77ec22902e73trawick "setsockopt(SO_RCVBUF): Failed to set ProxyReceiveBufferSize, using default");
3568de757bac0b47256647504c186d17ca272f85rbb#ifndef _OSD_POSIX /* BS2000 has this option "always on" */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick "proxy: error setting reuseaddr option: setsockopt(SO_REUSEADDR)");
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick#endif /*_OSD_POSIX*/
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r->server,
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick "proxy: socket has been created");
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * At this point we have a list of one or more IP addresses of
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * the machine to connect to. If configured, reorder this
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm * list so that the "best candidate" is first try. "best
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * candidate" could mean the least loaded server, the fastest
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm * responding server, whatever.
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm * For now we do nothing, ie we get DNS round robin.
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * XXX FIXME
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* try each IP address until we connect successfully */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* make the connection out of the socket */
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm /* if an error occurred, loop round and try again */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick "proxy: attempt to connect to %pI (%s) failed", connect_addr, connectname);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* if we get here, all is well */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* handle a permanent error from the above loop */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick return ap_proxyerror(r, HTTP_BAD_GATEWAY, apr_psprintf(r->pool,
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick "Could not connect to remote machine: %s port %d",
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* the socket is now open, create a new connection */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick origin = ap_new_connection(p, r->server, sock, r->connection->id);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* the peer reset the connection already; ap_new_connection()
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * closed the socket */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r->server,
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick "proxy: an error occurred creating a new connection to %pI (%s)", connect_addr, connectname);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* if a keepalive connection is floating around, close it first! */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* we might support ftp keepalives later, but not now... */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick apr_socket_close(backend->connection->client_socket);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r->server,
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick "proxy: connection complete");
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * III: Send Control Request
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * -------------------------
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * Log into the ftp server, send the username & password, change to the correct
f886987cd0bd4220c14043c4d9be77ec22902e73trawick * directory...
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm /* set up the connection filters */
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm /* possible results: */
3568de757bac0b47256647504c186d17ca272f85rbb /* 120 Service ready in nnn minutes. */
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm /* 220 Service ready for new user. */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* 421 Service not available, closing control connection. */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick i = ftp_getrc_msg(origin, cbb, buffer, sizeof(buffer));
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r->server,
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick if (i == -1) {
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick return ap_proxyerror(r, HTTP_BAD_GATEWAY, "Error reading from remote server");
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick if (i == 120) {
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* RFC2068 states:
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * 14.38 Retry-After
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * The Retry-After response-header field can be used with a 503 (Service
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * Unavailable) response to indicate how long the service is expected to
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm * be unavailable to the requesting client. The value of this field can
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * be either an HTTP-date or an integer number of seconds (in decimal)
64c351fd973428b5bb4c28e983fa86875ea4e60fdougm * after the time of the response.
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick * Retry-After = "Retry-After" ":" ( HTTP-date | delta-seconds )
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick ap_table_add(r->headers_out, "Retry-After", apr_psprintf(p, "%u", 60*wait_mins);
2e7f1d7da527c09e717251e186deffe55e6fbd0ftrawick return ap_proxyerror(r, HTTP_SERVICE_UNAVAILABLE, buffer);
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick if (i != 220) {
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz return ap_proxyerror(r, HTTP_BAD_GATEWAY, buffer);
1ec8bd0373f11c07688ec9afbbf778cf78a0bc52wrowe ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r->server,
1ec8bd0373f11c07688ec9afbbf778cf78a0bc52wrowe "proxy: FTP: connected.");
f886987cd0bd4220c14043c4d9be77ec22902e73trawick ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r->server,
1ec8bd0373f11c07688ec9afbbf778cf78a0bc52wrowe /* possible results; 230, 331, 332, 421, 500, 501, 530 */
1ec8bd0373f11c07688ec9afbbf778cf78a0bc52wrowe /* states: 1 - error, 2 - success; 3 - send password, 4,5 fail */
3568de757bac0b47256647504c186d17ca272f85rbb /* 230 User logged in, proceed. */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz /* 331 User name okay, need password. */
8bfe865d8d61be4ba4a89e45427a3c4211ebabdctrawick /* 332 Need account for login. */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz /* 421 Service not available, closing control connection. */
3568de757bac0b47256647504c186d17ca272f85rbb /* 500 Syntax error, command unrecognized. */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz /* (This may include errors such as command line too long.) */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz /* 501 Syntax error in parameters or arguments. */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz /* 530 Not logged in. */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz i = ftp_getrc_msg(origin, cbb, buffer, sizeof(buffer));
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r->server,
3568de757bac0b47256647504c186d17ca272f85rbb if (i == -1) {
3568de757bac0b47256647504c186d17ca272f85rbb return ap_proxyerror(r, HTTP_BAD_GATEWAY, "Error reading from remote server");
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (i == 530) {
3568de757bac0b47256647504c186d17ca272f85rbb return ftp_unauthorized (r, 1); /* log it: user name guessing attempt? */
3568de757bac0b47256647504c186d17ca272f85rbb return ftp_unauthorized (r, 0);
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz buf = apr_pstrcat(p, "PASS ", password, CRLF, NULL);
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r->server,
ce03576b2434cec77f2921db9d5b6a0510581c23rederpj /* possible results 202, 230, 332, 421, 500, 501, 503, 530 */
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick /* 230 User logged in, proceed. */
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick /* 332 Need account for login. */
9d0665da83d1e22c0ea0e5f6f940f70f75bf5237ianh /* 421 Service not available, closing control connection. */
3568de757bac0b47256647504c186d17ca272f85rbb /* 500 Syntax error, command unrecognized. */
3568de757bac0b47256647504c186d17ca272f85rbb /* 501 Syntax error in parameters or arguments. */
73e8b26287de5c06fa470d36162e103dbac9c7e5wrowe /* 503 Bad sequence of commands. */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* 530 Not logged in. */
b980ad7fdc218b4855cde9f75a747527f50c554dwrowe i = ftp_getrc_msg(origin, cbb, buffer, sizeof(buffer));
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r->server,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (i == -1) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "Error reading from remote server");
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz if (i == 332) {
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz apr_pstrcat(p, "Need account for login: ", buffer, NULL));
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz /* @@@ questionable -- we might as well return a 403 Forbidden here */
3568de757bac0b47256647504c186d17ca272f85rbb if (i == 530) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return ftp_unauthorized (r, 1); /* log it: passwd guessing attempt? */
e = apr_bucket_flush_create();
e = apr_bucket_flush_create();
e = apr_bucket_flush_create();
char *pstr;
return HTTP_INTERNAL_SERVER_ERROR;
apr_sockaddr_info_get(&pasv_addr, apr_psprintf(p, "%d.%d.%d.%d", h3, h2, h1, h0), APR_INET, pasvport, 0, p);
if (!pasvmode) {
char *local_ip;
return HTTP_INTERNAL_SERVER_ERROR;
return HTTP_INTERNAL_SERVER_ERROR;
return HTTP_INTERNAL_SERVER_ERROR;
return HTTP_INTERNAL_SERVER_ERROR;
e = apr_bucket_flush_create();
if (len == 0) {
e = apr_bucket_flush_create();
e = apr_bucket_flush_create();
len = 0;
#ifdef AUTODETECT_PWD
e = apr_bucket_flush_create();
if (len != 0)
e = apr_bucket_flush_create();
e = apr_bucket_flush_create();
#ifdef AUTODETECT_PWD
e = apr_bucket_flush_create();
e = apr_bucket_flush_create();
if (!pasvmode) {
case APR_EINTR:
case APR_SUCCESS:
return HTTP_BAD_GATEWAY;
if (!remote) {
return HTTP_INTERNAL_SERVER_ERROR;
if (!r->header_only) {
e = apr_bucket_flush_create();
e = apr_bucket_flush_create();
e = apr_bucket_flush_create();
return OK;