proxy_ftp.c revision 4ecd546edd89824908c2a9ad2e07339d89368f9c
842ae4bd224140319ae7feec1872b93dfd491143fielding/* ====================================================================
842ae4bd224140319ae7feec1872b93dfd491143fielding * The Apache Software License, Version 1.1
842ae4bd224140319ae7feec1872b93dfd491143fielding *
842ae4bd224140319ae7feec1872b93dfd491143fielding * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
842ae4bd224140319ae7feec1872b93dfd491143fielding * reserved.
842ae4bd224140319ae7feec1872b93dfd491143fielding *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Redistribution and use in source and binary forms, with or without
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * modification, are permitted provided that the following conditions
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * are met:
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 1. Redistributions of source code must retain the above copyright
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * notice, this list of conditions and the following disclaimer.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 2. Redistributions in binary form must reproduce the above copyright
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * notice, this list of conditions and the following disclaimer in
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the documentation and/or other materials provided with the
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * distribution.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
e8f95a682820a599fe41b22977010636be5c2717jim * 3. The end-user documentation included with the redistribution,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * if any, must include the following acknowledgment:
e8f95a682820a599fe41b22977010636be5c2717jim * "This product includes software developed by the
1747d30b98aa1bdbc43994c02cd46ab4cb9319e4fielding * Apache Software Foundation (http://www.apache.org/)."
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Alternately, this acknowledgment may appear in the software itself,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * if and wherever such third-party acknowledgments normally appear.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 4. The names "Apache" and "Apache Software Foundation" must
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * not be used to endorse or promote products derived from this
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * software without prior written permission. For written
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * permission, please contact apache@apache.org.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
11f2c481e1d57bedb3f758565307501e9a2730ddtrawick * 5. Products derived from this software may not be called "Apache",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * nor may "Apache" appear in their name, without prior written
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * permission of the Apache Software Foundation.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
5c0419d51818eb02045cf923a9fe456127a44c60wrowe * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
5c0419d51818eb02045cf923a9fe456127a44c60wrowe * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes * SUCH DAMAGE.
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes * ====================================================================
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes *
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes * This software consists of voluntary contributions made by many
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes * individuals on behalf of the Apache Software Foundation. For more
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes * information on the Apache Software Foundation, please see
cd3bbd6d2df78d6c75e5d159a81ef8bdd5f70df9trawick * <http://www.apache.org/>.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
0f60998368b493f90120180a93fc2e1e74490872covener * Portions of this software are based upon public domain software
0f60998368b493f90120180a93fc2e1e74490872covener * originally written at the National Center for Supercomputing Applications,
0f60998368b493f90120180a93fc2e1e74490872covener * University of Illinois, Urbana-Champaign.
0f60998368b493f90120180a93fc2e1e74490872covener */
0f60998368b493f90120180a93fc2e1e74490872covener
0f60998368b493f90120180a93fc2e1e74490872covener/* FTP routines for Apache proxy */
0f60998368b493f90120180a93fc2e1e74490872covener
0f60998368b493f90120180a93fc2e1e74490872covener#include "mod_proxy.h"
87587593f1a53030e840acc0dec6cc881022ea40covener
87587593f1a53030e840acc0dec6cc881022ea40covener#define AUTODETECT_PWD
87587593f1a53030e840acc0dec6cc881022ea40covener
87587593f1a53030e840acc0dec6cc881022ea40covenermodule AP_MODULE_DECLARE_DATA proxy_ftp_module;
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovenerint ap_proxy_ftp_canon(request_rec *r, char *url);
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovenerint ap_proxy_ftp_handler(request_rec *r, proxy_server_conf *conf,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *url, const char *proxyhost,
43997561b2302d13dee973998e77743a3ddd2374trawick apr_port_t proxyport);
fa123db15501821e36e513afa78e839775ad2800covenerapr_status_t ap_proxy_send_dir_filter(ap_filter_t * f,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_bucket_brigade *bb);
0568280364eb026393be492ebc732795c4934643jorton
0568280364eb026393be492ebc732795c4934643jorton
0568280364eb026393be492ebc732795c4934643jorton/*
0568280364eb026393be492ebc732795c4934643jorton * Decodes a '%' escaped string, and returns the number of characters
0568280364eb026393be492ebc732795c4934643jorton */
0568280364eb026393be492ebc732795c4934643jortonstatic int decodeenc(char *x)
0568280364eb026393be492ebc732795c4934643jorton{
0568280364eb026393be492ebc732795c4934643jorton int i, j, ch;
0568280364eb026393be492ebc732795c4934643jorton
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (x[0] == '\0')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0; /* special case for no characters */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (i = 0, j = 0; x[i] != '\0'; i++, j++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* decode it if not already done */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ch = x[i];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (ch == '%' && ap_isxdigit(x[i + 1]) && ap_isxdigit(x[i + 2])) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ch = ap_proxy_hex2c(&x[i + 1]);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes i += 2;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
796e4a7141265d8ed7036e4628161c6eafb2a789jorton x[j] = ch;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes x[j] = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return j;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * checks an encoded ftp string for bad characters, namely, CR, LF or
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * non-ascii character
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int ftp_check_string(const char *x)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int i, ch = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (i = 0; x[i] != '\0'; i++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ch = x[i];
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe if (ch == '%' && ap_isxdigit(x[i + 1]) && ap_isxdigit(x[i + 2])) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ch = ap_proxy_hex2c(&x[i + 1]);
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin i += 2;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if !APR_CHARSET_EBCDIC
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin if (ch == '\015' || ch == '\012' || (ch & 0x80))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#else /* APR_CHARSET_EBCDIC */
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin if (ch == '\r' || ch == '\n' || (os_toascii[ch] & 0x80))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif /* APR_CHARSET_EBCDIC */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin/*
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin * Canonicalise ftp URLs.
a1790fb35c4b352dab721370985c623a9f8f5062rpluem */
713a2b68bac4aeb1e9c48785006c0732451039depquernaint ap_proxy_ftp_canon(request_rec *r, char *url)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *user, *password, *host, *path, *parms, *strp, sport[7];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_pool_t *p = r->pool;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *err;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_port_t port, def_port;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (strncasecmp(url, "ftp:", 4) == 0) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes url += 4;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return DECLINED;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes def_port = apr_uri_default_port_for_scheme("ftp");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: canonicalising URL %s", url);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes port = def_port;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes err = ap_proxy_canon_netloc(p, &url, &user, &password, &host, &port);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (err)
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe return HTTP_BAD_REQUEST;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (user != NULL && !ftp_check_string(user))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return HTTP_BAD_REQUEST;
fa123db15501821e36e513afa78e839775ad2800covener if (password != NULL && !ftp_check_string(password))
fa123db15501821e36e513afa78e839775ad2800covener return HTTP_BAD_REQUEST;
fa123db15501821e36e513afa78e839775ad2800covener
fa123db15501821e36e513afa78e839775ad2800covener /* now parse path/parameters args, according to rfc1738 */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener /*
fa123db15501821e36e513afa78e839775ad2800covener * N.B. if this isn't a true proxy request, then the URL path (but not
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener * query args) has already been decoded. This gives rise to the problem
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener * of a ; being decoded into the path.
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener */
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener strp = strchr(url, ';');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (strp != NULL) {
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *(strp++) = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes parms = ap_proxy_canonenc(p, strp, strlen(strp), enc_parm,
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener r->proxyreq);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (parms == NULL)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return HTTP_BAD_REQUEST;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes parms = "";
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener path = ap_proxy_canonenc(p, url, strlen(url), enc_path, r->proxyreq);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (path == NULL)
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe return HTTP_BAD_REQUEST;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!ftp_check_string(path))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return HTTP_BAD_REQUEST;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (r->proxyreq && r->args != NULL) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (strp != NULL) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes strp = ap_proxy_canonenc(p, r->args, strlen(r->args), enc_parm, 1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (strp == NULL)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return HTTP_BAD_REQUEST;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes parms = apr_pstrcat(p, parms, "?", strp, NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
fa123db15501821e36e513afa78e839775ad2800covener else {
cceddc0b6c0fdaed0c73abda39975bb1d388243acovener strp = ap_proxy_canonenc(p, r->args, strlen(r->args), enc_fpath, 1);
cceddc0b6c0fdaed0c73abda39975bb1d388243acovener if (strp == NULL)
fa123db15501821e36e513afa78e839775ad2800covener return HTTP_BAD_REQUEST;
cceddc0b6c0fdaed0c73abda39975bb1d388243acovener path = apr_pstrcat(p, path, "?", strp, NULL);
f2be127030aa4190033084f0a6add531c9bc41desf }
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener r->args = NULL;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener }
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener/* now, rebuild URL */
60215f303c7e1ce8b6d272acb5bfa5b3d99dfd34covener
60215f303c7e1ce8b6d272acb5bfa5b3d99dfd34covener if (port != def_port)
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener apr_snprintf(sport, sizeof(sport), ":%d", port);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener else
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener sport[0] = '\0';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener r->filename = apr_pstrcat(p, "proxy:ftp://", (user != NULL) ? user : "",
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener (password != NULL) ? ":" : "",
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener (password != NULL) ? password : "",
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener (user != NULL) ? "@" : "", host, sport, "/", path,
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener (parms[0] != '\0') ? ";" : "", parms, NULL);
6683642c1e0032eeeed5f99e8c14880692ef84c5sf
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener return OK;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener}
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
6683642c1e0032eeeed5f99e8c14880692ef84c5sf/* we chop lines longer than 80 characters */
6683642c1e0032eeeed5f99e8c14880692ef84c5sf#define MAX_LINE_LEN 80
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener/*
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener * Reads response lines, returns both the ftp status code and
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener * remembers the response message in the supplied buffer
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenerstatic int ftp_getrc_msg(conn_rec *ftp_ctrl, apr_bucket_brigade *bb, char *msgbuf, int msglen)
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener{
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener int status;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener char response[MAX_LINE_LEN];
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener char buff[5];
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener char *mb = msgbuf, *me = &msgbuf[msglen];
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener apr_status_t rv;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener int eos;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener if (APR_SUCCESS != (rv = ap_proxy_string_read(ftp_ctrl, bb, response, sizeof(response), &eos))) {
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener return -1;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener }
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: <FTP: %s", response);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener*/
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener if (!apr_isdigit(response[0]) || !apr_isdigit(response[1]) ||
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener !apr_isdigit(response[2]) || (response[3] != ' ' && response[3] != '-'))
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener status = 0;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener else
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener status = 100 * response[0] + 10 * response[1] + response[2] - 111 * '0';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener mb = apr_cpystrn(mb, response + 4, me - mb);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener if (response[3] == '-') {
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener memcpy(buff, response, 3);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener buff[3] = ' ';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener do {
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener if (APR_SUCCESS != (rv = ap_proxy_string_read(ftp_ctrl, bb, response, sizeof(response), &eos))) {
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener return -1;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes mb = apr_cpystrn(mb, response + (' ' == response[0] ? 1 : 4), me - mb);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes } while (memcmp(response, buff, 4) != 0);
60215f303c7e1ce8b6d272acb5bfa5b3d99dfd34covener }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes return status;
0e05808dc59a321566303084c84b9826a4353cefrederpj}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
b08925593f214f621161742925dcf074a8047e0acovener/* this is a filter that turns a raw ASCII directory listing into pretty HTML */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
54d22ed1c429b903b029bbd62621f11a9e286137minfrin/* ideally, mod_proxy should simply send the raw directory list up the filter
465bb68501690d7a47bfd2a6129580047d76d8f1rederpj * stack to mod_autoindex, which in theory should turn the raw ascii into
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * pretty html along with all the bells and whistles it provides...
465bb68501690d7a47bfd2a6129580047d76d8f1rederpj *
e8f95a682820a599fe41b22977010636be5c2717jim * all in good time...! :)
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes */
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes
54d22ed1c429b903b029bbd62621f11a9e286137minfrintypedef struct {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_bucket_brigade *in;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes char buffer[MAX_STRING_LEN];
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes enum {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes HEADER, BODY, FOOTER
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes } state;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes} proxy_dir_ctx_t;
ebe5305f8b22507374358f32b74d12fb50c05a25covener
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholesapr_status_t ap_proxy_send_dir_filter(ap_filter_t *f, apr_bucket_brigade *in)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes{
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes request_rec *r = f->r;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_pool_t *p = r->pool;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_bucket *e;
513b324e774c559b579896df131fd7c8471ed529rederpj apr_bucket_brigade *out = apr_brigade_create(p);
513b324e774c559b579896df131fd7c8471ed529rederpj apr_status_t rv;
513b324e774c559b579896df131fd7c8471ed529rederpj regex_t *re = NULL; /* @@@ put this in the context */
513b324e774c559b579896df131fd7c8471ed529rederpj regmatch_t re_result[3];
513b324e774c559b579896df131fd7c8471ed529rederpj
513b324e774c559b579896df131fd7c8471ed529rederpj register int n;
513b324e774c559b579896df131fd7c8471ed529rederpj char *dir, *path, *reldir, *site, *str, *type;
513b324e774c559b579896df131fd7c8471ed529rederpj
513b324e774c559b579896df131fd7c8471ed529rederpj const char *pwd = apr_table_get(r->notes, "Directory-PWD");
513b324e774c559b579896df131fd7c8471ed529rederpj const char *readme = apr_table_get(r->notes, "Directory-README");
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
02fd88c85a9850109753b87612955ad372de1575sf proxy_dir_ctx_t *ctx = f->ctx;
02fd88c85a9850109753b87612955ad372de1575sf
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!ctx) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes f->ctx = ctx = apr_pcalloc(p, sizeof(*ctx));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ctx->in = apr_brigade_create(p);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ctx->buffer[0] = 0;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ctx->state = HEADER;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* Compile the output format of "ls -s1" as a fallback for non-unix ftp listings */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes re = ap_pregcomp(p, "^ *([0-9]+) +([^ ]+)$", REG_EXTENDED);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* combine the stored and the new */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes APR_BRIGADE_CONCAT(ctx->in, in);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (HEADER == ctx->state) {
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
707f6d077f73cc948deead8df5b40ea42c1eaa78covener /* basedir is either "", or "/%2f" for the "squid %2f hack" */
fa123db15501821e36e513afa78e839775ad2800covener const char *basedir = ""; /* By default, path is relative to the $HOME dir */
707f6d077f73cc948deead8df5b40ea42c1eaa78covener char *wildcard = NULL;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
707f6d077f73cc948deead8df5b40ea42c1eaa78covener /* Save "scheme://site" prefix without password */
707f6d077f73cc948deead8df5b40ea42c1eaa78covener site = apr_uri_unparse(p, &f->r->parsed_uri, APR_URI_UNP_OMITPASSWORD | APR_URI_UNP_OMITPATHINFO);
707f6d077f73cc948deead8df5b40ea42c1eaa78covener /* ... and path without query args */
707f6d077f73cc948deead8df5b40ea42c1eaa78covener path = apr_uri_unparse(p, &f->r->parsed_uri, APR_URI_UNP_OMITSITEPART | APR_URI_UNP_OMITQUERY);
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
9ad7b260be233be7d7b5576979825cac72e15498rederpj /* If path began with /%2f, change the basedir */
9ad7b260be233be7d7b5576979825cac72e15498rederpj if (strncasecmp(path, "/%2f", 4) == 0) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes basedir = "/%2f";
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* Strip off a type qualifier. It is ignored for dir listings */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if ((type = strstr(path, ";type=")) != NULL)
ebe5305f8b22507374358f32b74d12fb50c05a25covener *type++ = '\0';
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes (void)decodeenc(path);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes while (path[1] == '/') /* collapse multiple leading slashes to one */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ++path;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes reldir = strrchr(path, '/');
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (reldir != NULL) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes for (n=0; reldir[n] != '\0'; ++n) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (reldir[n] == '\\' && reldir[n+1] != '\0')
ebe5305f8b22507374358f32b74d12fb50c05a25covener ++n; /* escaped character */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes else if (reldir[n] == '*' || reldir[n] == '?') {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes wildcard = &reldir[1];
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes reldir[0] = '\0'; /* strip off the wildcard suffix */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes break;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* Copy path, strip (all except the last) trailing slashes */
b08925593f214f621161742925dcf074a8047e0acovener /* (the trailing slash is needed for the dir component loop below) */
b08925593f214f621161742925dcf074a8047e0acovener path = dir = ap_pstrcat(p, path, "/", NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (n = strlen(path); n > 1 && path[n - 1] == '/' && path[n - 2] == '/'; --n)
707f6d077f73cc948deead8df5b40ea42c1eaa78covener path[n - 1] = '\0';
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
9ad7b260be233be7d7b5576979825cac72e15498rederpj /* Add a link to the root directory (if %2f hack was used) */
707f6d077f73cc948deead8df5b40ea42c1eaa78covener str = (basedir[0] != '\0') ? "<a href=\"/%2f/\">%2f</a>/" : "";
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
9ad7b260be233be7d7b5576979825cac72e15498rederpj /* print "ftp://host/" */
707f6d077f73cc948deead8df5b40ea42c1eaa78covener str = apr_psprintf(p, DOCTYPE_HTML_3_2
707f6d077f73cc948deead8df5b40ea42c1eaa78covener "<html>\n <head>\n <title>%s%s%s</title>\n"
707f6d077f73cc948deead8df5b40ea42c1eaa78covener " <base href=\"%s%s%s\">\n </head>\n"
707f6d077f73cc948deead8df5b40ea42c1eaa78covener " <body>\n <h2>Directory of "
9ad7b260be233be7d7b5576979825cac72e15498rederpj "<a href=\"/\">%s</a>/%s",
707f6d077f73cc948deead8df5b40ea42c1eaa78covener site, basedir, ap_escape_html(p, path),
707f6d077f73cc948deead8df5b40ea42c1eaa78covener site, basedir, ap_escape_uri(p, path),
707f6d077f73cc948deead8df5b40ea42c1eaa78covener site, str);
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
707f6d077f73cc948deead8df5b40ea42c1eaa78covener e = apr_bucket_pool_create(str, strlen(str), p);
707f6d077f73cc948deead8df5b40ea42c1eaa78covener APR_BRIGADE_INSERT_TAIL(out, e);
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
707f6d077f73cc948deead8df5b40ea42c1eaa78covener for (dir = path+1; (dir = strchr(dir, '/')) != NULL; )
707f6d077f73cc948deead8df5b40ea42c1eaa78covener {
707f6d077f73cc948deead8df5b40ea42c1eaa78covener *dir = '\0';
707f6d077f73cc948deead8df5b40ea42c1eaa78covener if ((reldir = strrchr(path+1, '/'))==NULL) {
707f6d077f73cc948deead8df5b40ea42c1eaa78covener reldir = path+1;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener }
707f6d077f73cc948deead8df5b40ea42c1eaa78covener else
707f6d077f73cc948deead8df5b40ea42c1eaa78covener ++reldir;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener /* print "path/" component */
707f6d077f73cc948deead8df5b40ea42c1eaa78covener str = apr_psprintf(p, "<a href=\"%s%s/\">%s</a>/", basedir,
707f6d077f73cc948deead8df5b40ea42c1eaa78covener ap_escape_uri(p, path),
707f6d077f73cc948deead8df5b40ea42c1eaa78covener ap_escape_html(p, reldir));
707f6d077f73cc948deead8df5b40ea42c1eaa78covener *dir = '/';
707f6d077f73cc948deead8df5b40ea42c1eaa78covener while (*dir == '/')
707f6d077f73cc948deead8df5b40ea42c1eaa78covener ++dir;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener APR_BRIGADE_INSERT_TAIL(out, apr_bucket_pool_create(str, strlen(str), p));
707f6d077f73cc948deead8df5b40ea42c1eaa78covener }
707f6d077f73cc948deead8df5b40ea42c1eaa78covener if (wildcard != NULL) {
707f6d077f73cc948deead8df5b40ea42c1eaa78covener APR_BRIGADE_INSERT_TAIL(out, apr_bucket_pool_create(wildcard, strlen(wildcard), p));
707f6d077f73cc948deead8df5b40ea42c1eaa78covener }
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
707f6d077f73cc948deead8df5b40ea42c1eaa78covener /* If the caller has determined the current directory, and it differs */
9ad7b260be233be7d7b5576979825cac72e15498rederpj /* from what the client requested, then show the real name */
9ad7b260be233be7d7b5576979825cac72e15498rederpj if (pwd == NULL || strncmp(pwd, path, strlen(pwd)) == 0) {
9ad7b260be233be7d7b5576979825cac72e15498rederpj str = apr_psprintf(p, "</h2>\n\n <hr />\n\n<pre>");
7add8f7fb048534390571801b7794f71cd9e127abnicholes }
7add8f7fb048534390571801b7794f71cd9e127abnicholes else {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem str = apr_psprintf(p, "</h2>\n\n(%s)\n\n <hr />\n\n<pre>",
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem ap_escape_html(p, pwd));
7add8f7fb048534390571801b7794f71cd9e127abnicholes }
7add8f7fb048534390571801b7794f71cd9e127abnicholes e = apr_bucket_pool_create(str, strlen(str), p);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes APR_BRIGADE_INSERT_TAIL(out, e);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* print README */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (readme) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes str = apr_psprintf(p, "%s\n</pre>\n\n<hr />\n\n<pre>\n",
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ap_escape_html(p, readme));
7add8f7fb048534390571801b7794f71cd9e127abnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes e = apr_bucket_pool_create(str, strlen(str), p);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes APR_BRIGADE_INSERT_TAIL(out, e);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* make sure page intro gets sent out */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes e = apr_bucket_flush_create();
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes APR_BRIGADE_INSERT_TAIL(out, e);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (APR_SUCCESS != (rv = ap_pass_brigade(f->next, out))) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes return rv;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
7add8f7fb048534390571801b7794f71cd9e127abnicholes apr_brigade_cleanup(out);
7add8f7fb048534390571801b7794f71cd9e127abnicholes
141e1368614dc7564e1627671361b01b4869b491bnicholes ctx->state = BODY;
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf /* loop through each line of directory */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes while (BODY == ctx->state) {
e8f95a682820a599fe41b22977010636be5c2717jim char *filename;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes int found = 0;
ebe5305f8b22507374358f32b74d12fb50c05a25covener int eos = 0;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* get a complete line */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* if the buffer overruns - throw data away */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes while (!found && !APR_BRIGADE_EMPTY(ctx->in)) {
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes char *pos, *response;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_size_t len, max;
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes e = APR_BRIGADE_FIRST(ctx->in);
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes if (APR_BUCKET_IS_EOS(e)) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf eos = 1;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf break;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (APR_SUCCESS != (rv = apr_bucket_read(e, (const char **)&response, &len, APR_BLOCK_READ))) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf return rv;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf pos = memchr(response, APR_ASCII_LF, len);
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (pos != NULL) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if ((response + len) != (pos + 1)) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf len = pos - response + 1;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf apr_bucket_split(e, pos - response + 1);
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf found = 1;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf max = sizeof(ctx->buffer) - strlen(ctx->buffer) - 1;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (len > max) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf len = max;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* len+1 to leave space for the trailing nil char */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_cpystrn(ctx->buffer+strlen(ctx->buffer), response, len+1);
e8f95a682820a599fe41b22977010636be5c2717jim
7dbf29be626018bc389ef94c1846aeac4b72633bsf APR_BUCKET_REMOVE(e);
7dbf29be626018bc389ef94c1846aeac4b72633bsf apr_bucket_destroy(e);
7dbf29be626018bc389ef94c1846aeac4b72633bsf }
7dbf29be626018bc389ef94c1846aeac4b72633bsf
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* EOS? jump to footer */
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (eos) {
7dbf29be626018bc389ef94c1846aeac4b72633bsf ctx->state = FOOTER;
7dbf29be626018bc389ef94c1846aeac4b72633bsf break;
7dbf29be626018bc389ef94c1846aeac4b72633bsf }
7dbf29be626018bc389ef94c1846aeac4b72633bsf
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* not complete? leave and try get some more */
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (!found) {
7dbf29be626018bc389ef94c1846aeac4b72633bsf return APR_SUCCESS;
7dbf29be626018bc389ef94c1846aeac4b72633bsf }
7dbf29be626018bc389ef94c1846aeac4b72633bsf
7dbf29be626018bc389ef94c1846aeac4b72633bsf {
7dbf29be626018bc389ef94c1846aeac4b72633bsf size_t n = strlen(ctx->buffer);
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (ctx->buffer[n-1] == CRLF[1]) /* strip trailing '\n' */
7dbf29be626018bc389ef94c1846aeac4b72633bsf ctx->buffer[--n] = '\0';
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (ctx->buffer[n-1] == CRLF[0]) /* strip trailing '\r' if present */
7dbf29be626018bc389ef94c1846aeac4b72633bsf ctx->buffer[--n] = '\0';
7dbf29be626018bc389ef94c1846aeac4b72633bsf }
7dbf29be626018bc389ef94c1846aeac4b72633bsf
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* a symlink? */
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (ctx->buffer[0] == 'l' && (filename = strstr(ctx->buffer, " -> ")) != NULL) {
7dbf29be626018bc389ef94c1846aeac4b72633bsf char *link_ptr = filename;
7dbf29be626018bc389ef94c1846aeac4b72633bsf
7dbf29be626018bc389ef94c1846aeac4b72633bsf do {
7dbf29be626018bc389ef94c1846aeac4b72633bsf filename--;
7dbf29be626018bc389ef94c1846aeac4b72633bsf } while (filename[0] != ' ' && filename > ctx->buffer);
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (filename > ctx->buffer)
7dbf29be626018bc389ef94c1846aeac4b72633bsf *(filename++) = '\0';
7dbf29be626018bc389ef94c1846aeac4b72633bsf *(link_ptr++) = '\0';
7dbf29be626018bc389ef94c1846aeac4b72633bsf str = apr_psprintf(p, "%s <a href=\"%s\">%s %s</a>\n",
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ap_escape_html(p, ctx->buffer),
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ap_escape_uri(p, filename),
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ap_escape_html(p, filename),
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ap_escape_html(p, link_ptr));
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* a directory/file? */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes else if (ctx->buffer[0] == 'd' || ctx->buffer[0] == '-' || ctx->buffer[0] == 'l' || apr_isdigit(ctx->buffer[0])) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes int searchidx = 0;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes char *searchptr = NULL;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes int firstfile = 1;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (apr_isdigit(ctx->buffer[0])) { /* handle DOS dir */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf searchptr = strchr(ctx->buffer, '<');
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (searchptr != NULL)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes *searchptr = '[';
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes searchptr = strchr(ctx->buffer, '>');
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (searchptr != NULL)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes *searchptr = ']';
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes filename = strrchr(ctx->buffer, ' ');
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes *(filename++) = '\0';
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* handle filenames with spaces in 'em */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (!strcmp(filename, ".") || !strcmp(filename, "..") || firstfile) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes firstfile = 0;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes searchidx = filename - ctx->buffer;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes else if (searchidx != 0 && ctx->buffer[searchidx] != 0) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes *(--filename) = ' ';
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ctx->buffer[searchidx - 1] = '\0';
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf filename = &ctx->buffer[searchidx];
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* Append a slash to the HREF link for directories */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (!strcmp(filename, ".") || !strcmp(filename, "..") || ctx->buffer[0] == 'd') {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes str = apr_psprintf(p, "%s <a href=\"%s/\">%s</a>\n",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_escape_html(p, ctx->buffer),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_escape_uri(p, filename),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_escape_html(p, filename));
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf else {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf str = apr_psprintf(p, "%s <a href=\"%s\">%s</a>\n",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_escape_html(p, ctx->buffer),
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf ap_escape_uri(p, filename),
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf ap_escape_html(p, filename));
7dbf29be626018bc389ef94c1846aeac4b72633bsf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf /* Try a fallback for listings in the format of "ls -s1" */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else if (0 == ap_regexec(re, ctx->buffer, 3, re_result, 0)) {
e8f95a682820a599fe41b22977010636be5c2717jim
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes filename = apr_pstrndup(p, &ctx->buffer[re_result[2].rm_so], re_result[2].rm_eo - re_result[2].rm_so);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes str = ap_pstrcat(p, ap_escape_html(p, apr_pstrndup(p, ctx->buffer, re_result[2].rm_so)),
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf "<a href=\"", ap_escape_uri(p, filename), "\">",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_escape_html(p, filename), "</a>\n", NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
7dbf29be626018bc389ef94c1846aeac4b72633bsf else {
7dbf29be626018bc389ef94c1846aeac4b72633bsf strcat(ctx->buffer, "\n"); /* re-append the newline */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf str = ap_escape_html(p, ctx->buffer);
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
9c63a05713cb83a44a1590b4af33edeebf39f118sf /* erase buffer for next time around */
9c63a05713cb83a44a1590b4af33edeebf39f118sf ctx->buffer[0] = 0;
9c63a05713cb83a44a1590b4af33edeebf39f118sf
9c63a05713cb83a44a1590b4af33edeebf39f118sf e = apr_bucket_pool_create(str, strlen(str), p);
9c63a05713cb83a44a1590b4af33edeebf39f118sf APR_BRIGADE_INSERT_TAIL(out, e);
9c63a05713cb83a44a1590b4af33edeebf39f118sf e = apr_bucket_flush_create();
9c63a05713cb83a44a1590b4af33edeebf39f118sf APR_BRIGADE_INSERT_TAIL(out, e);
9c63a05713cb83a44a1590b4af33edeebf39f118sf if (APR_SUCCESS != (rv = ap_pass_brigade(f->next, out))) {
9c63a05713cb83a44a1590b4af33edeebf39f118sf return rv;
9c63a05713cb83a44a1590b4af33edeebf39f118sf }
9c63a05713cb83a44a1590b4af33edeebf39f118sf apr_brigade_cleanup(out);
9c63a05713cb83a44a1590b4af33edeebf39f118sf
9c63a05713cb83a44a1590b4af33edeebf39f118sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (FOOTER == ctx->state) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf str = apr_psprintf(p, "</pre>\n\n <hr />\n\n %s\n\n </body>\n</html>\n", ap_psignature("", r));
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf e = apr_bucket_pool_create(str, strlen(str), p);
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf APR_BRIGADE_INSERT_TAIL(out, e);
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf e = apr_bucket_flush_create();
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes APR_BRIGADE_INSERT_TAIL(out, e);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes e = apr_bucket_eos_create();
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes APR_BRIGADE_INSERT_TAIL(out, e);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (APR_SUCCESS != (rv = ap_pass_brigade(f->next, out))) {
9c63a05713cb83a44a1590b4af33edeebf39f118sf return rv;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_brigade_destroy(out);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return APR_SUCCESS;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
54d22ed1c429b903b029bbd62621f11a9e286137minfrin/*
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * Generic "send FTP command to server" routine, using the control socket.
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * Returns the FTP returncode (3 digit code)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * Allows for tracing the FTP protocol (in LogLevel debug)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin */
54d22ed1c429b903b029bbd62621f11a9e286137minfrinstatic int
54d22ed1c429b903b029bbd62621f11a9e286137minfrinproxy_ftp_command(const char *cmd, request_rec *r, conn_rec *ftp_ctrl,
54d22ed1c429b903b029bbd62621f11a9e286137minfrin apr_bucket_brigade *bb, char **pmessage)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
e8f95a682820a599fe41b22977010636be5c2717jim char *crlf;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe int rc;
8a03cd420b800a2428f49f4617293de9b2387b20jorton char message[HUGE_STRING_LEN];
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /* If cmd == NULL, we retrieve the next ftp response line */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (cmd != NULL) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin APR_BRIGADE_INSERT_TAIL(bb, apr_bucket_pool_create(cmd, strlen(cmd), r->pool));
54d22ed1c429b903b029bbd62621f11a9e286137minfrin APR_BRIGADE_INSERT_TAIL(bb, apr_bucket_flush_create());
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ap_pass_brigade(ftp_ctrl->output_filters, bb);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /* strip off the CRLF for logging */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ap_cpystrn(message, cmd, sizeof message);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if ((crlf = strchr(message, '\r')) != NULL ||
54d22ed1c429b903b029bbd62621f11a9e286137minfrin (crlf = strchr(message, '\n')) != NULL)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin *crlf = '\0';
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (strncmp(message,"PASS ", 5) == 0)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin strcpy(&message[5], "****");
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
54d22ed1c429b903b029bbd62621f11a9e286137minfrin "proxy:>FTP: %s", message);
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener }
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener rc = ftp_getrc_msg(ftp_ctrl, bb, message, sizeof message);
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener if (rc == -1 || rc == 421)
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener strcpy(message,"<unable to read result>");
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener if ((crlf = strchr(message, '\r')) != NULL ||
54d22ed1c429b903b029bbd62621f11a9e286137minfrin (crlf = strchr(message, '\n')) != NULL)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin *crlf = '\0';
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
54d22ed1c429b903b029bbd62621f11a9e286137minfrin "proxy:<FTP: %3.3u %s", rc, message);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (pmessage != NULL)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin *pmessage = ap_pstrdup(r->pool, message);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return rc;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Set ftp server to TYPE {A,I,E} before transfer of a directory or file */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int ftp_set_TYPE(char xfer_type, request_rec *r, conn_rec *ftp_ctrl,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_bucket_brigade *bb, char **pmessage)
e8f95a682820a599fe41b22977010636be5c2717jim{
e8f95a682820a599fe41b22977010636be5c2717jim static char old_type[2] = { 'A', '\0' }; /* After logon, mode is ASCII */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe int ret = HTTP_OK;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int rc;
e8f95a682820a599fe41b22977010636be5c2717jim
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe if (xfer_type == old_type[0])
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe return ret;
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe
f0f6f1b90ab582896f8a7d56d85bd62a55e57d90covener /* set desired type */
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe old_type[0] = xfer_type;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
560fd0658902ab57754616c172d8953e69fc4722bnicholes rc = proxy_ftp_command(apr_pstrcat(r->pool, "TYPE ", old_type, CRLF, NULL),
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener r, ftp_ctrl, bb, pmessage);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* responses: 200, 421, 500, 501, 504, 530 */
e8f95a682820a599fe41b22977010636be5c2717jim /* 200 Command okay. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 421 Service not available, closing control connection. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 500 Syntax error, command unrecognized. */
9ad7b260be233be7d7b5576979825cac72e15498rederpj /* 501 Syntax error in parameters or arguments. */
9ad7b260be233be7d7b5576979825cac72e15498rederpj /* 504 Command not implemented for that parameter. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 530 Not logged in. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (rc == -1 || rc == 421) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ret = ap_proxyerror(r, HTTP_BAD_GATEWAY,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Error reading from remote server");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else if (rc != 200 && rc != 504) {
560fd0658902ab57754616c172d8953e69fc4722bnicholes ret = ap_proxyerror(r, HTTP_BAD_GATEWAY,
560fd0658902ab57754616c172d8953e69fc4722bnicholes "Unable to set transfer type");
560fd0658902ab57754616c172d8953e69fc4722bnicholes }
560fd0658902ab57754616c172d8953e69fc4722bnicholes/* Allow not implemented */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else if (rc == 504)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* ignore it silently */;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ret;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e8f95a682820a599fe41b22977010636be5c2717jim/* Return the current directory which we have selected on the FTP server, or NULL */
e8f95a682820a599fe41b22977010636be5c2717jimstatic char *ftp_get_PWD(request_rec *r, conn_rec *ftp_ctrl, apr_bucket_brigade *bb)
e8f95a682820a599fe41b22977010636be5c2717jim{
e8f95a682820a599fe41b22977010636be5c2717jim char *cwd = NULL;
e8f95a682820a599fe41b22977010636be5c2717jim char *ftpmessage = NULL;
e8f95a682820a599fe41b22977010636be5c2717jim
ae1981fc94adf2b231e2d0e15d2f895b2138c969covener /* responses: 257, 500, 501, 502, 421, 550 */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* 257 "<directory-name>" <commentary> */
7a55c294da84865fe13262ed66ffd0c5841a9da5covener /* 421 Service not available, closing control connection. */
7a55c294da84865fe13262ed66ffd0c5841a9da5covener /* 500 Syntax error, command unrecognized. */
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener /* 501 Syntax error in parameters or arguments. */
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener /* 502 Command not implemented. */
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener /* 550 Requested action not taken. */
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener switch (proxy_ftp_command("PWD" CRLF, r, ftp_ctrl, bb, &ftpmessage)) {
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener case -1:
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener case 421:
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener case 550:
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener ap_proxyerror(r, HTTP_BAD_GATEWAY,
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener "Failed to read PWD on ftp server");
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener break;
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes case 257: {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *dirp = ftpmessage;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes cwd = ap_getword_conf(r->pool, &dirp);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return cwd;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Common routine for failed authorization (i.e., missing or wrong password)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * to an ftp service. This causes most browsers to retry the request
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * with username and password (which was presumably queried from the user)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * supplied in the Authorization: header.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Note that we "invent" a realm name which consists of the
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * ftp://user@host part of the reqest (sans password -if supplied but invalid-)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int ftp_unauthorized(request_rec *r, int log_it)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes r->proxyreq = PROXYREQ_NONE;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
e8f95a682820a599fe41b22977010636be5c2717jim * Log failed requests if they supplied a password (log username/password
560fd0658902ab57754616c172d8953e69fc4722bnicholes * guessing attempts)
ae1981fc94adf2b231e2d0e15d2f895b2138c969covener */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (log_it)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_rerror(APLOG_MARK, APLOG_INFO | APLOG_NOERRNO, 0, r,
7a55c294da84865fe13262ed66ffd0c5841a9da5covener "proxy: missing or failed auth to %s",
fa123db15501821e36e513afa78e839775ad2800covener apr_uri_unparse(r->pool,
fa123db15501821e36e513afa78e839775ad2800covener &r->parsed_uri, APR_URI_UNP_OMITPATHINFO));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_table_setn(r->err_headers_out, "WWW-Authenticate",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_pstrcat(r->pool, "Basic realm=\"",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_uri_unparse(r->pool, &r->parsed_uri,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes APR_URI_UNP_OMITPASSWORD | APR_URI_UNP_OMITPATHINFO),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "\"", NULL));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return HTTP_UNAUTHORIZED;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Handles direct access of ftp:// URLs
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Original (Non-PASV) version from
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Troy Morrison <spiffnet@zoom.com>
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * PASV added by Chuck
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Filters by [Graham Leggett <minfrin@sharp.fm>]
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
0e05808dc59a321566303084c84b9826a4353cefrederpjint ap_proxy_ftp_handler(request_rec *r, proxy_server_conf *conf,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *url, const char *proxyhost,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_port_t proxyport)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener apr_pool_t *p = r->pool;
0e05808dc59a321566303084c84b9826a4353cefrederpj conn_rec *c = r->connection;
ebe5305f8b22507374358f32b74d12fb50c05a25covener proxy_conn_rec *backend;
ebe5305f8b22507374358f32b74d12fb50c05a25covener apr_socket_t *sock, *local_sock, *data_sock = NULL;
ebe5305f8b22507374358f32b74d12fb50c05a25covener apr_sockaddr_t *connect_addr;
ebe5305f8b22507374358f32b74d12fb50c05a25covener apr_status_t rv;
ebe5305f8b22507374358f32b74d12fb50c05a25covener conn_rec *origin, *data = NULL;
ebe5305f8b22507374358f32b74d12fb50c05a25covener int err;
ebe5305f8b22507374358f32b74d12fb50c05a25covener apr_bucket_brigade *bb = apr_brigade_create(p);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener char *buf, *connectname;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener apr_port_t connectport;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener char buffer[MAX_STRING_LEN];
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener char *ftpmessage = NULL;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener char *path, *strp, *type_suffix, *cwd = NULL;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener char *user = NULL;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener/* char *account = NULL; how to supply an account in a URL? */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener const char *password = NULL;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener int len, rc;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener int one = 1;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener char *size = NULL;
0e05808dc59a321566303084c84b9826a4353cefrederpj apr_socket_t *origin_sock = NULL;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char xfer_type = 'A'; /* after ftp login, the default is ASCII */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener int dirlisting = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* stuff for PASV mode */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int connect = 0, use_port = 0;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener char dates[AP_RFC822_DATE_LEN];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* is this for us? */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (proxyhost) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
9ad7b260be233be7d7b5576979825cac72e15498rederpj "proxy: FTP: declining URL %s - proxyhost %s specified:", url, proxyhost);
9ad7b260be233be7d7b5576979825cac72e15498rederpj return DECLINED; /* proxy connections are via HTTP */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (strncasecmp(url, "ftp:", 4)) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
54d22ed1c429b903b029bbd62621f11a9e286137minfrin "proxy: FTP: declining URL %s - not ftp:", url);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return DECLINED; /* only interested in FTP */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
560fd0658902ab57754616c172d8953e69fc4722bnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
54d22ed1c429b903b029bbd62621f11a9e286137minfrin "proxy: FTP: serving URL %s", url);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
ae1981fc94adf2b231e2d0e15d2f895b2138c969covener /* create space for state information */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes backend = (proxy_conn_rec *) ap_get_module_config(c->conn_config, &proxy_ftp_module);
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener if (!backend) {
7a55c294da84865fe13262ed66ffd0c5841a9da5covener backend = ap_pcalloc(c->pool, sizeof(proxy_conn_rec));
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener backend->connection = NULL;
fa123db15501821e36e513afa78e839775ad2800covener backend->hostname = NULL;
fa123db15501821e36e513afa78e839775ad2800covener backend->port = 0;
fa123db15501821e36e513afa78e839775ad2800covener ap_set_module_config(c->conn_config, &proxy_ftp_module, backend);
fa123db15501821e36e513afa78e839775ad2800covener }
fa123db15501821e36e513afa78e839775ad2800covener if (backend->connection)
fa123db15501821e36e513afa78e839775ad2800covener origin_sock = ap_get_module_config(backend->connection->conn_config, &core_module);
fa123db15501821e36e513afa78e839775ad2800covener
fa123db15501821e36e513afa78e839775ad2800covener
fa123db15501821e36e513afa78e839775ad2800covener /*
fa123db15501821e36e513afa78e839775ad2800covener * I: Who Do I Connect To? -----------------------
fa123db15501821e36e513afa78e839775ad2800covener *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Break up the URL to determine the host to connect to
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* we only support GET and HEAD */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (r->method_number != M_GET)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return HTTP_NOT_IMPLEMENTED;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* We break the URL into host, port, path-search */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes connectname = r->parsed_uri.hostname;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes connectport = (r->parsed_uri.port != 0)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ? r->parsed_uri.port
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes : apr_uri_default_port_for_scheme("ftp");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes path = apr_pstrdup(p, r->parsed_uri.path);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes path = (path != NULL && path[0] != '\0') ? &path[1] : "";
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes type_suffix = strchr(path, ';');
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (type_suffix != NULL)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe *(type_suffix++) = '\0';
e8f95a682820a599fe41b22977010636be5c2717jim
e8f95a682820a599fe41b22977010636be5c2717jim /*
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * The "Authorization:" header must be checked first. We allow the user
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * to "override" the URL-coded user [ & password ] in the Browsers'
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * User&Password Dialog. NOTE that this is only marginally more secure
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * than having the password travel in plain as part of the URL, because
e8f95a682820a599fe41b22977010636be5c2717jim * Basic Auth simply uuencodes the plain text password. But chances are
e8f95a682820a599fe41b22977010636be5c2717jim * still smaller that the URL is logged regularly.
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((password = apr_table_get(r->headers_in, "Authorization")) != NULL
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes && strcasecmp(ap_getword(r->pool, &password, ' '), "Basic") == 0
e8f95a682820a599fe41b22977010636be5c2717jim && (password = ap_pbase64decode(r->pool, password))[0] != ':') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Note that this allocation has to be made from r->connection->pool
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * because it has the lifetime of the connection. The other
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * allocations are temporary and can be tossed away any time.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes user = ap_getword_nulls(r->connection->pool, &password, ':');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes r->ap_auth_type = "Basic";
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe r->user = r->parsed_uri.user = user;
e8f95a682820a599fe41b22977010636be5c2717jim }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe else if ((user = r->parsed_uri.user) != NULL) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes user = apr_pstrdup(p, user);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes decodeenc(user);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((password = r->parsed_uri.password) != NULL) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *tmp = apr_pstrdup(p, password);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes decodeenc(tmp);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes password = tmp;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes user = "anonymous";
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes password = "apache-proxy@";
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: connecting %s to %s:%d", url, connectname, connectport);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* do a DNS lookup for the destination host */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes err = apr_sockaddr_info_get(&connect_addr, connectname, APR_UNSPEC, connectport, 0, p);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* check if ProxyBlock directive on this host */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (OK != ap_proxy_checkproxyblock(r, conf, connect_addr)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_proxyerror(r, HTTP_FORBIDDEN,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Connect to remote machine blocked");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
e8f95a682820a599fe41b22977010636be5c2717jim * II: Make the Connection -----------------------
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * We have determined who to connect to. Now make the connection.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * get all the possible IP addresses for the destname and loop through
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * them until we get a successful connection
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (APR_SUCCESS != err) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_proxyerror(r, HTTP_BAD_GATEWAY, apr_pstrcat(p,
e8f95a682820a599fe41b22977010636be5c2717jim "DNS lookup failure for: ",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes connectname, NULL));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((rv = apr_socket_create(&sock, APR_INET, SOCK_STREAM, r->pool)) != APR_SUCCESS) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
43c3e6a4b559b76b750c245ee95e2782c15b4296jim "proxy: FTP: error creating socket");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return HTTP_INTERNAL_SERVER_ERROR;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if !defined(TPF) && !defined(BEOS)
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes if (conf->recv_buffer_size > 0
43c3e6a4b559b76b750c245ee95e2782c15b4296jim && (rv = apr_setsocketopt(sock, APR_SO_RCVBUF,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes conf->recv_buffer_size))) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "setsockopt(SO_RCVBUF): Failed to set ProxyReceiveBufferSize, using default");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener#endif
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (APR_SUCCESS != (rv = apr_setsocketopt(sock, APR_SO_REUSEADDR, one))) {
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener#ifndef _OSD_POSIX /* BS2000 has this option "always on" */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe "proxy: FTP: error setting reuseaddr option: setsockopt(SO_REUSEADDR)");
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return HTTP_INTERNAL_SERVER_ERROR;
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes#endif /* _OSD_POSIX */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf /* Set a timeout on the socket */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf apr_setsocketopt(sock, APR_SO_TIMEOUT, (int)(r->server->timeout * APR_USEC_PER_SEC));
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf "proxy: FTP: socket has been created");
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf /*
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * At this point we have a list of one or more IP addresses of the
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * machine to connect to. If configured, reorder this list so that the
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * "best candidate" is first try. "best candidate" could mean the least
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * loaded server, the fastest responding server, whatever.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * For now we do nothing, ie we get DNS round robin. XXX FIXME
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* try each IP address until we connect successfully */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int failed = 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (connect_addr) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* FIXME: @@@: We created an APR_INET socket. Now there may be
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * IPv6 (AF_INET6) DNS addresses in the list... IMO the socket
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * should be created with the correct family in the first place.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * (either do it in this loop, or make at least two attempts
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * with the AF_INET and AF_INET6 elements in the list)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: trying to connect to %pI (%s)...", connect_addr, connectname);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e8f95a682820a599fe41b22977010636be5c2717jim /* make the connection out of the socket */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes rv = apr_connect(sock, connect_addr);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
e8f95a682820a599fe41b22977010636be5c2717jim /* if an error occurred, loop round and try again */
e8f95a682820a599fe41b22977010636be5c2717jim if (rv != APR_SUCCESS) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: attempt to connect to %pI (%s) failed", connect_addr, connectname);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes connect_addr = connect_addr->next;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes continue;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* if we get here, all is well */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes failed = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* handle a permanent error from the above loop */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (failed) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_proxyerror(r, HTTP_BAD_GATEWAY, apr_psprintf(r->pool,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Could not connect to remote machine: %s port %d",
e8f95a682820a599fe41b22977010636be5c2717jim connectname, connectport));
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* the socket is now open, create a new connection */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes origin = ap_new_connection(p, r->server, sock, r->connection->id, r->connection->sbh);
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe if (!origin) {
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe /*
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe * the peer reset the connection already; ap_new_connection() closed
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the socket
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
e8f95a682820a599fe41b22977010636be5c2717jim ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: an error occurred creating a new connection to %pI (%s)", connect_addr, connectname);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return HTTP_INTERNAL_SERVER_ERROR;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* if a keepalive connection is floating around, close it first! */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* we might support ftp keepalives later, but not now... */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (backend->connection) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe apr_socket_close(origin_sock);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe backend->connection = NULL;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes origin_sock = NULL;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: control connection complete");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * III: Send Control Request -------------------------
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Log into the ftp server, send the username & password, change to the
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * correct directory...
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e8f95a682820a599fe41b22977010636be5c2717jim /* set up the connection filters */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_run_install_transport_filters(origin, sock);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* possible results: */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 120 Service ready in nnn minutes. */
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj /* 220 Service ready for new user. */
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj /* 421 Service not available, closing control connection. */
e8f95a682820a599fe41b22977010636be5c2717jim rc = proxy_ftp_command(NULL, r, origin, bb, &ftpmessage);
e8f95a682820a599fe41b22977010636be5c2717jim if (rc == -1 || rc == 421) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return ap_proxyerror(r, HTTP_BAD_GATEWAY, "Error reading from remote server");
e8f95a682820a599fe41b22977010636be5c2717jim }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (rc == 120) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * RFC2616 states: 14.37 Retry-After
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * The Retry-After response-header field can be used with a 503 (Service
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Unavailable) response to indicate how long the service is expected
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * to be unavailable to the requesting client. [...] The value of
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * this field can be either an HTTP-date or an integer number of
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * seconds (in decimal) after the time of the response. Retry-After
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * = "Retry-After" ":" ( HTTP-date | delta-seconds )
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *secs_str = ftpmessage;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes time_t secs;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Look for a number, preceded by whitespace */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (*secs_str)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (apr_isspace(secs_str[-1]) && apr_isdigit(secs_str[0]))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (*secs_str != '\0') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes secs = atol(secs_str);
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj ap_table_add(r->headers_out, "Retry-After",
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj apr_psprintf(p, "%lu", (unsigned long)(60 * secs)));
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj }
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj return ap_proxyerror(r, HTTP_SERVICE_UNAVAILABLE, ftpmessage);
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj }
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if (rc != 220) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: connected.");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes rc = proxy_ftp_command(apr_pstrcat(p, "USER ", user, CRLF, NULL),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes r, origin, bb, &ftpmessage);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* possible results; 230, 331, 332, 421, 500, 501, 530 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* states: 1 - error, 2 - success; 3 - send password, 4,5 fail */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 230 User logged in, proceed. */
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj /* 331 User name okay, need password. */
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes /* 332 Need account for login. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 421 Service not available, closing control connection. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 500 Syntax error, command unrecognized. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* (This may include errors such as command line too long.) */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 501 Syntax error in parameters or arguments. */
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener /* 530 Not logged in. */
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener if (rc == -1 || rc == 421) {
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener return ap_proxyerror(r, HTTP_BAD_GATEWAY, "Error reading from remote server");
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener }
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener if (rc == 530) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ftp_unauthorized(r, 1); /* log it: user name guessing
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * attempt? */
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (rc != 230 && rc != 331) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (rc == 331) { /* send password */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (password == NULL) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf return ftp_unauthorized(r, 0);
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf rc = proxy_ftp_command(apr_pstrcat(p, "PASS ", password, CRLF, NULL),
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf r, origin, bb, &ftpmessage);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* possible results 202, 230, 332, 421, 500, 501, 503, 530 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 230 User logged in, proceed. */
e8f95a682820a599fe41b22977010636be5c2717jim /* 332 Need account for login. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 421 Service not available, closing control connection. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 500 Syntax error, command unrecognized. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 501 Syntax error in parameters or arguments. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 503 Bad sequence of commands. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 530 Not logged in. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (rc == -1 || rc == 421) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_proxyerror(r, HTTP_BAD_GATEWAY,
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj "Error reading from remote server");
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (rc == 332) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return ap_proxyerror(r, HTTP_UNAUTHORIZED,
e8f95a682820a599fe41b22977010636be5c2717jim apr_pstrcat(p, "Need account for login: ", ftpmessage, NULL));
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
e8f95a682820a599fe41b22977010636be5c2717jim /* @@@ questionable -- we might as well return a 403 Forbidden here */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (rc == 530) {
e8f95a682820a599fe41b22977010636be5c2717jim return ftp_unauthorized(r, 1); /* log it: passwd guessing
e8f95a682820a599fe41b22977010636be5c2717jim * attempt? */
e8f95a682820a599fe41b22977010636be5c2717jim }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (rc != 230 && rc != 202) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj }
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem apr_table_set(r->notes, "Directory-README", ftpmessage);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
9c63a05713cb83a44a1590b4af33edeebf39f118sf /*
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * set the directory (walk directory component by component): this is
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf * what we must do if we don't know the OS type of the remote machine
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (;;) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes strp = strchr(path, '/');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (strp == NULL)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *strp = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes len = decodeenc(path);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* NOTE: FTP servers do globbing on the path.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * So we need to escape the URI metacharacters.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * In the current implementation, we use shell escaping, because
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * it masks all characters which are also dangerous for FTP.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * We could also have extended gen_test_char.c with a special T_ESCAPE_FTP_PATH
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes rc = proxy_ftp_command(apr_pstrcat(p, "CWD ",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_escape_shell_cmd(p, path), CRLF, NULL),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes r, origin, bb, &ftpmessage);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *strp = '/';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* responses: 250, 421, 500, 501, 502, 530, 550 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 250 Requested file action okay, completed. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 421 Service not available, closing control connection. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 500 Syntax error, command unrecognized. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 501 Syntax error in parameters or arguments. */
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* 502 Command not implemented. */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* 530 Not logged in. */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* 550 Requested action not taken. */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (rc == -1 || rc == 421) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem return ap_proxyerror(r, HTTP_BAD_GATEWAY,
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem "Error reading from remote server");
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (rc == 550) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj return ap_proxyerror(r, HTTP_NOT_FOUND, ftpmessage);
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (rc != 250) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj path = strp + 1;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (type_suffix != NULL && strncmp(type_suffix, "type=", 5) == 0
8869662bb1a4078297020e94ae5e928626d877c6rederpj && ap_isalpha(type_suffix[5])) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* "type=d" forces a dir listing.
8869662bb1a4078297020e94ae5e928626d877c6rederpj * The other types (i|a|e) are directly used for the ftp TYPE command
8869662bb1a4078297020e94ae5e928626d877c6rederpj */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if ( ! (dirlisting = (ap_tolower(type_suffix[5]) == 'd')))
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem xfer_type = ap_toupper(type_suffix[5]);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* Check valid types, rather than ignoring invalid types silently: */
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (strchr("AEI", xfer_type) == NULL)
8869662bb1a4078297020e94ae5e928626d877c6rederpj return ap_proxyerror(r, HTTP_BAD_REQUEST, ap_pstrcat(r->pool,
8869662bb1a4078297020e94ae5e928626d877c6rederpj "ftp proxy supports only types 'a', 'i', or 'e': \"",
8869662bb1a4078297020e94ae5e928626d877c6rederpj type_suffix, "\" is invalid.", NULL));
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj else {
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* make binary transfers the default */
8869662bb1a4078297020e94ae5e928626d877c6rederpj xfer_type = 'I';
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj /*
8869662bb1a4078297020e94ae5e928626d877c6rederpj * IV: Make Data Connection? -------------------------
6733d943c9e8d0f27dd077a04037e8c49eb090ffcovener *
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * Try EPSV, if that fails... try PASV, if that fails... try PORT.
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem */
8869662bb1a4078297020e94ae5e928626d877c6rederpj/* this temporarily switches off EPSV/PASV */
8869662bb1a4078297020e94ae5e928626d877c6rederpj/*goto bypass;*/
8869662bb1a4078297020e94ae5e928626d877c6rederpj
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf /* set up data connection - EPSV */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf apr_sockaddr_t *data_addr;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf char *data_ip;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf apr_port_t data_port;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf /*
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * The EPSV command replaces PASV where both IPV4 and IPV6 is
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * supported. Only the port is returned, the IP address is always the
8869662bb1a4078297020e94ae5e928626d877c6rederpj * same as that on the control connection. Example: Entering Extended
8869662bb1a4078297020e94ae5e928626d877c6rederpj * Passive Mode (|||6446|)
8869662bb1a4078297020e94ae5e928626d877c6rederpj */
8869662bb1a4078297020e94ae5e928626d877c6rederpj rc = proxy_ftp_command("EPSV" CRLF,
8869662bb1a4078297020e94ae5e928626d877c6rederpj r, origin, bb, &ftpmessage);
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* possible results: 227, 421, 500, 501, 502, 530 */
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2). */
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* 421 Service not available, closing control connection. */
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* 500 Syntax error, command unrecognized. */
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* 501 Syntax error in parameters or arguments. */
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* 502 Command not implemented. */
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* 530 Not logged in. */
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (rc == -1 || rc == 421) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj return ap_proxyerror(r, HTTP_BAD_GATEWAY,
8869662bb1a4078297020e94ae5e928626d877c6rederpj "Error reading from remote server");
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (rc != 229 && rc != 500 && rc != 501 && rc != 502) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj else if (rc == 229) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj char *pstr;
8869662bb1a4078297020e94ae5e928626d877c6rederpj char *tok_cntx;
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj pstr = ftpmessage;
8869662bb1a4078297020e94ae5e928626d877c6rederpj pstr = apr_strtok(pstr, " ", &tok_cntx); /* separate result code */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (pstr != NULL) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (*(pstr + strlen(pstr) + 1) == '=') {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem pstr += strlen(pstr) + 2;
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj else {
8869662bb1a4078297020e94ae5e928626d877c6rederpj pstr = apr_strtok(NULL, "(", &tok_cntx); /* separate address &
8869662bb1a4078297020e94ae5e928626d877c6rederpj * port params */
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (pstr != NULL)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem pstr = apr_strtok(NULL, ")", &tok_cntx);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (pstr) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem apr_sockaddr_t *epsv_addr;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem data_port = atoi(pstr + 3);
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
8869662bb1a4078297020e94ae5e928626d877c6rederpj "proxy: FTP: EPSV contacting remote host on port %d",
8869662bb1a4078297020e94ae5e928626d877c6rederpj data_port);
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj if ((rv = apr_socket_create(&data_sock, APR_INET, SOCK_STREAM, r->pool)) != APR_SUCCESS) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
8869662bb1a4078297020e94ae5e928626d877c6rederpj "proxy: FTP: error creating EPSV socket");
8869662bb1a4078297020e94ae5e928626d877c6rederpj return HTTP_INTERNAL_SERVER_ERROR;
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj#if !defined (TPF) && !defined(BEOS)
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (conf->recv_buffer_size > 0 && (rv = apr_setsocketopt(data_sock, APR_SO_RCVBUF,
8869662bb1a4078297020e94ae5e928626d877c6rederpj conf->recv_buffer_size))) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
8869662bb1a4078297020e94ae5e928626d877c6rederpj "proxy: FTP: setsockopt(SO_RCVBUF): Failed to set ProxyReceiveBufferSize, using default");
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj#endif
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* make the connection */
8869662bb1a4078297020e94ae5e928626d877c6rederpj apr_socket_addr_get(&data_addr, APR_REMOTE, sock);
8869662bb1a4078297020e94ae5e928626d877c6rederpj apr_sockaddr_ip_get(&data_ip, data_addr);
8869662bb1a4078297020e94ae5e928626d877c6rederpj apr_sockaddr_info_get(&epsv_addr, data_ip, APR_INET, data_port, 0, p);
8869662bb1a4078297020e94ae5e928626d877c6rederpj rv = apr_connect(data_sock, epsv_addr);
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (rv != APR_SUCCESS) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem "proxy: FTP: EPSV attempt to connect to %pI failed - Firewall/NAT?", epsv_addr);
8869662bb1a4078297020e94ae5e928626d877c6rederpj return ap_proxyerror(r, HTTP_BAD_GATEWAY, apr_psprintf(r->pool,
8869662bb1a4078297020e94ae5e928626d877c6rederpj "EPSV attempt to connect to %pI failed - firewall/NAT?", epsv_addr));
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj else {
8869662bb1a4078297020e94ae5e928626d877c6rederpj connect = 1;
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj else {
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* and try the regular way */
8869662bb1a4078297020e94ae5e928626d877c6rederpj apr_socket_close(data_sock);
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* set up data connection - PASV */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (!connect) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem rc = proxy_ftp_command("PASV" CRLF,
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj r, origin, bb, &ftpmessage);
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj /* possible results: 227, 421, 500, 501, 502, 530 */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2). */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* 421 Service not available, closing control connection. */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* 500 Syntax error, command unrecognized. */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* 501 Syntax error in parameters or arguments. */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* 502 Command not implemented. */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* 530 Not logged in. */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (rc == -1 || rc == 421) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem return ap_proxyerror(r, HTTP_BAD_GATEWAY,
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem "Error reading from remote server");
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (rc != 227 && rc != 502) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem else if (rc == 227) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem unsigned int h0, h1, h2, h3, p0, p1;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem char *pstr;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem char *tok_cntx;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem/* FIXME: Check PASV against RFC1123 */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem pstr = ftpmessage;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem pstr = apr_strtok(pstr, " ", &tok_cntx); /* separate result code */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (pstr != NULL) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (*(pstr + strlen(pstr) + 1) == '=') {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem pstr += strlen(pstr) + 2;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem else {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem pstr = apr_strtok(NULL, "(", &tok_cntx); /* separate address &
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * port params */
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if (pstr != NULL)
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj pstr = apr_strtok(NULL, ")", &tok_cntx);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj/* FIXME: Only supports IPV4 - fix in RFC2428 */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (pstr != NULL && (sscanf(pstr,
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem "%d,%d,%d,%d,%d,%d", &h3, &h2, &h1, &h0, &p1, &p0) == 6)) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj apr_sockaddr_t *pasv_addr;
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj apr_port_t pasvport = (p1 << 8) + p0;
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj "proxy: FTP: PASV contacting host %d.%d.%d.%d:%d",
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj h3, h2, h1, h0, pasvport);
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj if ((rv = apr_socket_create(&data_sock, APR_INET, SOCK_STREAM, r->pool)) != APR_SUCCESS) {
0e05808dc59a321566303084c84b9826a4353cefrederpj ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
a9c4332dc6241dc11dd104826bd179d42ccc0f12fuankg "proxy: error creating PASV socket");
a9c4332dc6241dc11dd104826bd179d42ccc0f12fuankg return HTTP_INTERNAL_SERVER_ERROR;
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj }
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj#if !defined (TPF) && !defined(BEOS)
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj if (conf->recv_buffer_size > 0 && (rv = apr_setsocketopt(data_sock, APR_SO_RCVBUF,
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem conf->recv_buffer_size))) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem "proxy: FTP: setsockopt(SO_RCVBUF): Failed to set ProxyReceiveBufferSize, using default");
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem#endif
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* make the connection */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem apr_sockaddr_info_get(&pasv_addr, apr_psprintf(p, "%d.%d.%d.%d", h3, h2, h1, h0), APR_INET, pasvport, 0, p);
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj rv = apr_connect(data_sock, pasv_addr);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj if (rv != APR_SUCCESS) {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj "proxy: FTP: PASV attempt to connect to %pI failed - Firewall/NAT?", pasv_addr);
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj return ap_proxyerror(r, HTTP_BAD_GATEWAY, apr_psprintf(r->pool,
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem "PASV attempt to connect to %pI failed - firewall/NAT?", pasv_addr));
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem else {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem connect = 1;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem else {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* and try the regular way */
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj apr_socket_close(data_sock);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj }
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj }
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj }
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj/*bypass:*/
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj /* set up data connection - PORT */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj if (!connect) {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj apr_sockaddr_t *local_addr;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj char *local_ip;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem apr_port_t local_port;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem unsigned int h0, h1, h2, h3, p0, p1;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if ((rv = apr_socket_create(&local_sock, APR_INET, SOCK_STREAM, r->pool)) != APR_SUCCESS) {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj "proxy: FTP: error creating local socket");
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj return HTTP_INTERNAL_SERVER_ERROR;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj }
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj apr_socket_addr_get(&local_addr, APR_LOCAL, sock);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj apr_sockaddr_port_get(&local_port, local_addr);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj apr_sockaddr_ip_get(&local_ip, local_addr);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if ((rv = apr_setsocketopt(local_sock, APR_SO_REUSEADDR, one)) != APR_SUCCESS) {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj#ifndef _OSD_POSIX /* BS2000 has this option "always on" */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj "proxy: FTP: error setting reuseaddr option");
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj return HTTP_INTERNAL_SERVER_ERROR;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj#endif /* _OSD_POSIX */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj }
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem apr_sockaddr_info_get(&local_addr, local_ip, APR_UNSPEC, local_port, 0, r->pool);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj if ((rv = apr_bind(local_sock, local_addr)) != APR_SUCCESS) {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem "proxy: FTP: error binding to ftp data socket %pI", local_addr);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem return HTTP_INTERNAL_SERVER_ERROR;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
0e05808dc59a321566303084c84b9826a4353cefrederpj /* only need a short queue */
0e05808dc59a321566303084c84b9826a4353cefrederpj if ((rv = apr_listen(local_sock, 2)) != APR_SUCCESS) {
0e05808dc59a321566303084c84b9826a4353cefrederpj ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
0e05808dc59a321566303084c84b9826a4353cefrederpj "proxy: FTP: error listening to ftp data socket %pI", local_addr);
0e05808dc59a321566303084c84b9826a4353cefrederpj return HTTP_INTERNAL_SERVER_ERROR;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf }
0e05808dc59a321566303084c84b9826a4353cefrederpj
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf/* FIXME: Sent PORT here */
0e05808dc59a321566303084c84b9826a4353cefrederpj
0e05808dc59a321566303084c84b9826a4353cefrederpj if (local_ip && (sscanf(local_ip,
0e05808dc59a321566303084c84b9826a4353cefrederpj "%d.%d.%d.%d", &h3, &h2, &h1, &h0) == 4)) {
0e05808dc59a321566303084c84b9826a4353cefrederpj p1 = (local_port >> 8);
0e05808dc59a321566303084c84b9826a4353cefrederpj p0 = (local_port & 0xFF);
0e05808dc59a321566303084c84b9826a4353cefrederpj
0e05808dc59a321566303084c84b9826a4353cefrederpj rc = proxy_ftp_command(apr_psprintf(p, "PORT %d,%d,%d,%d,%d,%d" CRLF, h3, h2, h1, h0, p1, p0),
0e05808dc59a321566303084c84b9826a4353cefrederpj r, origin, bb, &ftpmessage);
0e05808dc59a321566303084c84b9826a4353cefrederpj /* possible results: 200, 421, 500, 501, 502, 530 */
0e05808dc59a321566303084c84b9826a4353cefrederpj /* 200 Command okay. */
0e05808dc59a321566303084c84b9826a4353cefrederpj /* 421 Service not available, closing control connection. */
0e05808dc59a321566303084c84b9826a4353cefrederpj /* 500 Syntax error, command unrecognized. */
0e05808dc59a321566303084c84b9826a4353cefrederpj /* 501 Syntax error in parameters or arguments. */
0e05808dc59a321566303084c84b9826a4353cefrederpj /* 502 Command not implemented. */
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj /* 530 Not logged in. */
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if (rc == -1 || rc == 421) {
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj return ap_proxyerror(r, HTTP_BAD_GATEWAY,
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj "Error reading from remote server");
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj }
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if (rc != 200) {
0e05808dc59a321566303084c84b9826a4353cefrederpj return ap_proxyerror(r, HTTP_BAD_GATEWAY, buffer);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf /* signal that we must use the EPRT/PORT loop */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem use_port = 1;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj else {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj/* IPV6 FIXME:
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf * The EPRT command replaces PORT where both IPV4 and IPV6 is supported. The first
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf * number (1,2) indicates the protocol type. Examples:
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * EPRT |1|132.235.1.2|6275|
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj * EPRT |2|1080::8:800:200C:417A|5282|
0e05808dc59a321566303084c84b9826a4353cefrederpj */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem return ap_proxyerror(r, HTTP_NOT_IMPLEMENTED, "Connect to IPV6 ftp server using EPRT not supported. Enable EPSV.");
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj /*
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * V: Set The Headers -------------------
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj *
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * Get the size of the request, set up the environment for HTTP.
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj /* set request; "path" holds last path component */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem len = decodeenc(path);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj /* If len == 0 then it must be a directory (you can't RETR nothing)
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * Also, don't allow to RETR by wildcard. Instead, create a dirlisting
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem */
0e05808dc59a321566303084c84b9826a4353cefrederpj if (len == 0 || strpbrk(path, "*?[]") != NULL) {
0e05808dc59a321566303084c84b9826a4353cefrederpj dirlisting = 1;
0e05808dc59a321566303084c84b9826a4353cefrederpj }
0e05808dc59a321566303084c84b9826a4353cefrederpj else {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem rc = proxy_ftp_command(apr_pstrcat(p, "SIZE ", path, CRLF, NULL),
0e05808dc59a321566303084c84b9826a4353cefrederpj r, origin, bb, &ftpmessage);
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf if (rc == -1 || rc == 421) {
0e05808dc59a321566303084c84b9826a4353cefrederpj return ap_proxyerror(r, HTTP_BAD_GATEWAY,
0e05808dc59a321566303084c84b9826a4353cefrederpj "Error reading from remote server");
0e05808dc59a321566303084c84b9826a4353cefrederpj }
0e05808dc59a321566303084c84b9826a4353cefrederpj else if (rc == 213) {/* Size command ok */
0e05808dc59a321566303084c84b9826a4353cefrederpj int j;
0e05808dc59a321566303084c84b9826a4353cefrederpj for (j = 0; apr_isdigit(ftpmessage[j]); j++)
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf ;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf ftpmessage[j] = '\0';
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf if (ftpmessage[0] != '\0')
0e05808dc59a321566303084c84b9826a4353cefrederpj size = ftpmessage; /* already pstrdup'ed: no copy necessary */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj }
0e05808dc59a321566303084c84b9826a4353cefrederpj else if (rc == 550) { /* Not a regular file */
40a1aee60a66f7c8dbd0835fdd4f09334e12fc15rpluem ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
0e05808dc59a321566303084c84b9826a4353cefrederpj "proxy: FTP: SIZE shows this is a directory");
0e05808dc59a321566303084c84b9826a4353cefrederpj dirlisting = 1;
40a1aee60a66f7c8dbd0835fdd4f09334e12fc15rpluem rc = proxy_ftp_command(apr_pstrcat(p, "CWD ",
0e05808dc59a321566303084c84b9826a4353cefrederpj ap_escape_shell_cmd(p, path), CRLF, NULL),
0e05808dc59a321566303084c84b9826a4353cefrederpj r, origin, bb, &ftpmessage);
0e05808dc59a321566303084c84b9826a4353cefrederpj /* possible results: 250, 421, 500, 501, 502, 530, 550 */
0e05808dc59a321566303084c84b9826a4353cefrederpj /* 250 Requested file action okay, completed. */
0e05808dc59a321566303084c84b9826a4353cefrederpj /* 421 Service not available, closing control connection. */
0e05808dc59a321566303084c84b9826a4353cefrederpj /* 500 Syntax error, command unrecognized. */
0e05808dc59a321566303084c84b9826a4353cefrederpj /* 501 Syntax error in parameters or arguments. */
0e05808dc59a321566303084c84b9826a4353cefrederpj /* 502 Command not implemented. */
0e05808dc59a321566303084c84b9826a4353cefrederpj /* 530 Not logged in. */
0e05808dc59a321566303084c84b9826a4353cefrederpj /* 550 Requested action not taken. */
0e05808dc59a321566303084c84b9826a4353cefrederpj if (rc == -1 || rc == 421) {
0e05808dc59a321566303084c84b9826a4353cefrederpj return ap_proxyerror(r, HTTP_BAD_GATEWAY,
0e05808dc59a321566303084c84b9826a4353cefrederpj "Error reading from remote server");
0e05808dc59a321566303084c84b9826a4353cefrederpj }
0e05808dc59a321566303084c84b9826a4353cefrederpj if (rc == 550) {
0e05808dc59a321566303084c84b9826a4353cefrederpj return ap_proxyerror(r, HTTP_NOT_FOUND, ftpmessage);
0e05808dc59a321566303084c84b9826a4353cefrederpj }
0e05808dc59a321566303084c84b9826a4353cefrederpj if (rc != 250) {
0e05808dc59a321566303084c84b9826a4353cefrederpj return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
0e05808dc59a321566303084c84b9826a4353cefrederpj }
0e05808dc59a321566303084c84b9826a4353cefrederpj path = "";
0e05808dc59a321566303084c84b9826a4353cefrederpj len = 0;
0e05808dc59a321566303084c84b9826a4353cefrederpj }
0e05808dc59a321566303084c84b9826a4353cefrederpj }
0e05808dc59a321566303084c84b9826a4353cefrederpj
0e05808dc59a321566303084c84b9826a4353cefrederpj cwd = ftp_get_PWD(r, origin, bb);
0e05808dc59a321566303084c84b9826a4353cefrederpj if (cwd != NULL) {
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj apr_table_set(r->notes, "Directory-PWD", cwd);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj }
0e05808dc59a321566303084c84b9826a4353cefrederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj if (dirlisting) {
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj /* If the current directory contains no slash, we are talking to
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * a non-unix ftp system. Try LIST instead of "LIST -lag", it
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * should return a long listing anyway (unlike NLST).
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * Some exotic FTP servers might choke on the "-lag" switch.
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* Note that we do not escape the path here, to allow for
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * queries like: ftp://user@host/apache/src/server/http_*.c
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj if (len != 0)
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj buf = apr_pstrcat(p, "LIST ", path, CRLF, NULL);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj else if (cwd == NULL || strchr(cwd, '/') != NULL)
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj buf = apr_pstrcat(p, "LIST -lag", CRLF, NULL);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj else
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj buf = "LIST" CRLF;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem else {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* switch to binary if the user did not specify ";type=a" */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem ftp_set_TYPE(xfer_type, r, origin, bb, &ftpmessage);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj/* FIXME: Handle range requests - send REST */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj buf = apr_pstrcat(p, "RETR ", ap_escape_shell_cmd(p, path), CRLF, NULL);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem rc = proxy_ftp_command(buf, r, origin, bb, &ftpmessage);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* rc is an intermediate response for the LIST or RETR commands */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf /*
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf * RETR: 110, 125, 150, 226, 250, 421, 425, 426, 450, 451, 500, 501, 530,
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf * 550 NLST: 125, 150, 226, 250, 421, 425, 426, 450, 451, 500, 501, 502,
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf * 530
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj /* 110 Restart marker reply. */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* 125 Data connection already open; transfer starting. */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* 150 File status okay; about to open data connection. */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* 226 Closing data connection. */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* 250 Requested file action okay, completed. */
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf /* 421 Service not available, closing control connection. */
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf /* 425 Can't open data connection. */
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf /* 426 Connection closed; transfer aborted. */
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf /* 450 Requested file action not taken. */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj /* 451 Requested action aborted. Local error in processing. */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* 500 Syntax error, command unrecognized. */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* 501 Syntax error in parameters or arguments. */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* 530 Not logged in. */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* 550 Requested action not taken. */
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if (rc == -1 || rc == 421) {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj return ap_proxyerror(r, HTTP_BAD_GATEWAY,
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj "Error reading from remote server");
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj }
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if (rc == 550) {
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj "proxy: FTP: RETR failed, trying LIST instead");
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe /* Directory Listings should always be fetched in ASCII mode */
e8f95a682820a599fe41b22977010636be5c2717jim dirlisting = 1;
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe ftp_set_TYPE('A', r, origin, bb, NULL);
e8f95a682820a599fe41b22977010636be5c2717jim
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe rc = proxy_ftp_command(apr_pstrcat(p, "CWD ",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_escape_shell_cmd(p, path), CRLF, NULL),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes r, origin, bb, &ftpmessage);
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes /* possible results: 250, 421, 500, 501, 502, 530, 550 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 250 Requested file action okay, completed. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 421 Service not available, closing control connection. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 500 Syntax error, command unrecognized. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 501 Syntax error in parameters or arguments. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 502 Command not implemented. */
f05787953018140838ad51456c86c965d6a86267jim /* 530 Not logged in. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 550 Requested action not taken. */
f05787953018140838ad51456c86c965d6a86267jim if (rc == -1 || rc == 421) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_proxyerror(r, HTTP_BAD_GATEWAY,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Error reading from remote server");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
e8f95a682820a599fe41b22977010636be5c2717jim if (rc == 550) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_proxyerror(r, HTTP_NOT_FOUND, ftpmessage);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (rc != 250) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e8f95a682820a599fe41b22977010636be5c2717jim /* Update current directory after CWD */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe cwd = ftp_get_PWD(r, origin, bb);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (cwd != NULL) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_table_set(r->notes, "Directory-PWD", cwd);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* See above for the "LIST" vs. "LIST -lag" discussion. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes rc = proxy_ftp_command((cwd == NULL || strchr(cwd, '/') != NULL)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ? "LIST -lag" CRLF : "LIST" CRLF,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes r, origin, bb, &ftpmessage);
e8f95a682820a599fe41b22977010636be5c2717jim
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* rc is an intermediate response for the LIST command (125 transfer starting, 150 opening data connection) */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (rc == -1 || rc == 421)
e8f95a682820a599fe41b22977010636be5c2717jim return ap_proxyerror(r, HTTP_BAD_GATEWAY,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Error reading from remote server");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
e8f95a682820a599fe41b22977010636be5c2717jim if (rc != 125 && rc != 150 && rc != 226 && rc != 250) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
e8f95a682820a599fe41b22977010636be5c2717jim }
e8f95a682820a599fe41b22977010636be5c2717jim
e8f95a682820a599fe41b22977010636be5c2717jim r->status = HTTP_OK;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe r->status_line = "200 OK";
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_rfc822_date(dates, r->request_time);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_table_setn(r->headers_out, "Date", dates);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_table_setn(r->headers_out, "Server", ap_get_server_version());
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e8f95a682820a599fe41b22977010636be5c2717jim /* set content-type */
e8f95a682820a599fe41b22977010636be5c2717jim if (dirlisting) {
e8f95a682820a599fe41b22977010636be5c2717jim r->content_type = "text/html";
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener if (r->content_type) {
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener "proxy: FTP: Content-Type set to %s", r->content_type);
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener }
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener else {
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener r->content_type = ap_default_type(r);
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener }
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener if (xfer_type != 'A' && size != NULL) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* We "trust" the ftp server to really serve (size) bytes... */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_table_setn(r->headers_out, "Content-Length", size);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: Content-Length set to %s", size);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_table_setn(r->headers_out, "Content-Type", r->content_type);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: Content-Type set to %s", r->content_type);
e8f95a682820a599fe41b22977010636be5c2717jim
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* If an encoding has been set by mistake, delete it.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * @@@ FIXME (e.g., for ftp://user@host/file*.tar.gz,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * @@@ the encoding is currently set to x-gzip)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (dirlisting && r->content_encoding != NULL)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes r->content_encoding = NULL;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* set content-encoding (not for dir listings, they are uncompressed)*/
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (r->content_encoding != NULL && r->content_encoding[0] != '\0') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes "proxy: FTP: Content-Encoding set to %s", r->content_encoding);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_table_setn(r->headers_out, "Content-Encoding", r->content_encoding);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* wait for connection */
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener if (use_port) {
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener for (;;) {
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener rv = apr_accept(&data_sock, local_sock, r->pool);
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (rv == APR_EINTR) {
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener continue;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else if (rv == APR_SUCCESS) {
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: failed to accept data connection");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return HTTP_BAD_GATEWAY;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e8f95a682820a599fe41b22977010636be5c2717jim /* the transfer socket is now open, create a new connection */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes data = ap_new_connection(p, r->server, data_sock, r->connection->id, r->connection->sbh);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!data) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the peer reset the connection already; ap_new_connection() closed
e8f95a682820a599fe41b22977010636be5c2717jim * the socket
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: an error occurred creating the transfer connection");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return HTTP_INTERNAL_SERVER_ERROR;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* set up the connection filters */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_run_install_transport_filters(data, data_sock);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * VI: Receive the Response ------------------------
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Get response from the remote ftp socket, and pass it up the filter chain.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* send response */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes r->sent_bodyct = 1;
e8f95a682820a599fe41b22977010636be5c2717jim
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (dirlisting) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* insert directory filter */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_add_output_filter("PROXY_SEND_DIR", NULL, r, r->connection);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* send body */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (!r->header_only) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: start body send");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* read the body, pass it to the output filters */
e8f95a682820a599fe41b22977010636be5c2717jim while (ap_get_brigade(data->input_filters, bb, AP_MODE_EXHAUSTIVE,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes APR_BLOCK_READ, 0) == APR_SUCCESS) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(bb))) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_pass_brigade(r->output_filters, bb);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
7dbf29be626018bc389ef94c1846aeac4b72633bsf }
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (ap_pass_brigade(r->output_filters, bb) != APR_SUCCESS) {
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* Ack! Phbtt! Die! User aborted! */
7dbf29be626018bc389ef94c1846aeac4b72633bsf break;
7dbf29be626018bc389ef94c1846aeac4b72633bsf }
7dbf29be626018bc389ef94c1846aeac4b72633bsf apr_brigade_cleanup(bb);
7dbf29be626018bc389ef94c1846aeac4b72633bsf }
7dbf29be626018bc389ef94c1846aeac4b72633bsf }
7dbf29be626018bc389ef94c1846aeac4b72633bsf ap_flush_conn(data);
7dbf29be626018bc389ef94c1846aeac4b72633bsf apr_socket_close(data_sock);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes "proxy: FTP: Closing Data connection.");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Retrieve the final response for the RETR or LIST commands */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes rc = proxy_ftp_command(NULL, r, origin, bb, &ftpmessage);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_brigade_cleanup(bb);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
7dbf29be626018bc389ef94c1846aeac4b72633bsf "proxy: FTP: end body send");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * VII: Clean Up -------------
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * If there are no KeepAlives, or if the connection has been signalled to
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * close, close the socket and clean up
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* finish */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes rc = proxy_ftp_command("QUIT" CRLF,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes r, origin, bb, &ftpmessage);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* responses: 221, 500 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 221 Service closing control connection. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 500 Syntax error, command unrecognized. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_flush_conn(origin);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (origin_sock) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_socket_close(origin_sock);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes origin_sock = NULL;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_brigade_destroy(bb);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return OK;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic void ap_proxy_ftp_register_hook(apr_pool_t *p)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* hooks */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes proxy_hook_scheme_handler(ap_proxy_ftp_handler, NULL, NULL, APR_HOOK_MIDDLE);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes proxy_hook_canon_handler(ap_proxy_ftp_canon, NULL, NULL, APR_HOOK_MIDDLE);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* filters */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_register_output_filter("PROXY_SEND_DIR", ap_proxy_send_dir_filter, AP_FTYPE_CONTENT);
e8f95a682820a599fe41b22977010636be5c2717jim}
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesmodule AP_MODULE_DECLARE_DATA proxy_ftp_module = {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes STANDARD20_MODULE_STUFF,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes NULL, /* create per-directory config structure */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes NULL, /* merge per-directory config structures */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes NULL, /* create per-server config structure */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes NULL, /* merge per-server config structures */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes NULL, /* command apr_table_t */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_proxy_ftp_register_hook /* register hooks */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes};
e8f95a682820a599fe41b22977010636be5c2717jim