proxy_ftp.c revision 32c4bc04f89b16521718145dc731f750144d7b38
da49e9ab4c3cf53de3e41a532877727c6c9e5804Lennart Poettering/* ====================================================================
da49e9ab4c3cf53de3e41a532877727c6c9e5804Lennart Poettering * The Apache Software License, Version 1.1
da49e9ab4c3cf53de3e41a532877727c6c9e5804Lennart Poettering * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
da49e9ab4c3cf53de3e41a532877727c6c9e5804Lennart Poettering * Redistribution and use in source and binary forms, with or without
da49e9ab4c3cf53de3e41a532877727c6c9e5804Lennart Poettering * modification, are permitted provided that the following conditions
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering * 1. Redistributions of source code must retain the above copyright
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering * notice, this list of conditions and the following disclaimer.
da49e9ab4c3cf53de3e41a532877727c6c9e5804Lennart Poettering * 2. Redistributions in binary form must reproduce the above copyright
da49e9ab4c3cf53de3e41a532877727c6c9e5804Lennart Poettering * notice, this list of conditions and the following disclaimer in
da49e9ab4c3cf53de3e41a532877727c6c9e5804Lennart Poettering * the documentation and/or other materials provided with the
da49e9ab4c3cf53de3e41a532877727c6c9e5804Lennart Poettering * distribution.
da49e9ab4c3cf53de3e41a532877727c6c9e5804Lennart Poettering * 3. The end-user documentation included with the redistribution,
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering * if any, must include the following acknowledgment:
da49e9ab4c3cf53de3e41a532877727c6c9e5804Lennart Poettering * "This product includes software developed by the
da49e9ab4c3cf53de3e41a532877727c6c9e5804Lennart Poettering * Apache Software Foundation (http://www.apache.org/)."
da49e9ab4c3cf53de3e41a532877727c6c9e5804Lennart Poettering * Alternately, this acknowledgment may appear in the software itself,
da49e9ab4c3cf53de3e41a532877727c6c9e5804Lennart Poettering * if and wherever such third-party acknowledgments normally appear.
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * 4. The names "Apache" and "Apache Software Foundation" must
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * not be used to endorse or promote products derived from this
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * software without prior written permission. For written
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * permission, please contact apache@apache.org.
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * 5. Products derived from this software may not be called "Apache",
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * nor may "Apache" appear in their name, without prior written
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * permission of the Apache Software Foundation.
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * ====================================================================
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * This software consists of voluntary contributions made by many
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * individuals on behalf of the Apache Software Foundation. For more
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * information on the Apache Software Foundation, please see
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * Portions of this software are based upon public domain software
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * originally written at the National Center for Supercomputing Applications,
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * University of Illinois, Urbana-Champaign.
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek/* FTP routines for Apache proxy */
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek/* Automatic timestamping (Last-Modified header) based on MDTM is used if:
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * 1) the FTP server supports the MDTM command and
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * 2) HAVE_TIMEGM (preferred) or HAVE_GMTOFF is available at compile time
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmekmodule AP_MODULE_DECLARE_DATA proxy_ftp_module;
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmekint ap_proxy_ftp_canon(request_rec *r, char *url);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmekint ap_proxy_ftp_handler(request_rec *r, proxy_server_conf *conf,
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek char *url, const char *proxyhost,
c129bd5df3ca08eb352cf69d01d2f374552624aeLennart Poetteringapr_status_t ap_proxy_send_dir_filter(ap_filter_t * f,
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * Decodes a '%' escaped string, and returns the number of characters
c129bd5df3ca08eb352cf69d01d2f374552624aeLennart Poetteringstatic int decodeenc(char *x)
c129bd5df3ca08eb352cf69d01d2f374552624aeLennart Poettering if (x[0] == '\0')
c129bd5df3ca08eb352cf69d01d2f374552624aeLennart Poettering return 0; /* special case for no characters */
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek for (i = 0, j = 0; x[i] != '\0'; i++, j++) {
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek /* decode it if not already done */
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek if (ch == '%' && ap_isxdigit(x[i + 1]) && ap_isxdigit(x[i + 2])) {
*d++ = *path;
return ret;
++path;
return TRUE;
return FALSE;
static int ftp_check_string(const char *x)
int i, ch = 0;
ch = x[i];
#if !APR_CHARSET_EBCDIC
const char *err;
return DECLINED;
if (err)
return HTTP_BAD_REQUEST;
return HTTP_BAD_REQUEST;
return HTTP_BAD_REQUEST;
/* now parse path/parameters args, according to rfc1738 */
r->proxyreq);
return HTTP_BAD_REQUEST;
return HTTP_BAD_REQUEST;
return HTTP_BAD_REQUEST;
return HTTP_BAD_REQUEST;
return HTTP_BAD_REQUEST;
return OK;
int status;
int eos;
status = 0;
return status;
} state;
request_rec *r = f->r;
if (!ctx) {
++path;
/* print "ftp://host/" */
++reldir;
++dir;
if (readme) {
return rv;
char *filename;
int found = 0;
int eos = 0;
apr_bucket *e;
if (APR_BUCKET_IS_EOS(e)) {
return rv;
if (eos) {
if (!found) {
return APR_SUCCESS;
filename--;
else if (ctx->buffer[0] == 'd' || ctx->buffer[0] == '-' || ctx->buffer[0] == 'l' || apr_isdigit(ctx->buffer[0])) {
int searchidx = 0;
firstfile = 0;
filename = apr_pstrndup(p, &ctx->buffer[re_result[2].rm_so], re_result[2].rm_eo - re_result[2].rm_so);
return rv;
return rv;
return APR_SUCCESS;
char *crlf;
int rc;
return rc;
int rc;
return ret;
return ret;
return cwd;
* ftp://user@host part of the reqest (sans password -if supplied but invalid-)
if (log_it)
return HTTP_UNAUTHORIZED;
int err;
int dirlisting = 0;
if (proxyhost) {
if (!backend) {
return HTTP_NOT_IMPLEMENTED;
return HTTP_INTERNAL_SERVER_ERROR;
return HTTP_INTERNAL_SERVER_ERROR;
while (connect_addr) {
failed = 0;
if (failed) {
if (!origin) {
return HTTP_INTERNAL_SERVER_ERROR;
while (*secs_str)
return ftp_unauthorized(r, 0);
++path;
* We could also have extended gen_test_char.c with a special T_ESCAPE_FTP_PATH
char *data_ip;
char *pstr;
char *tok_cntx;
if (pstr) {
return HTTP_INTERNAL_SERVER_ERROR;
if (!connect) {
char *pstr;
char *tok_cntx;
return HTTP_INTERNAL_SERVER_ERROR;
apr_sockaddr_info_get(&pasv_addr, apr_psprintf(p, "%d.%d.%d.%d", h3, h2, h1, h0), APR_INET, pasvport, 0, p);
if (!connect) {
char *local_ip;
return HTTP_INTERNAL_SERVER_ERROR;
return HTTP_INTERNAL_SERVER_ERROR;
return HTTP_INTERNAL_SERVER_ERROR;
return HTTP_INTERNAL_SERVER_ERROR;
return ap_proxyerror(r, HTTP_NOT_IMPLEMENTED, "Connect to IPV6 ftp server using EPRT not supported. Enable EPSV.");
/* from draft-ietf-ftpext-mlst-14.txt:
len = 0;
if (dirlisting) {
* queries like: ftp://user@host/apache/src/server/http_*.c
if (len != 0)
/* from draft-ietf-ftpext-mlst-14.txt:
* YYYYMMDDHHMMSS.sss
} time_val;
mtime = 0L;
/* rc is an intermediate response for the LIST command (125 transfer starting, 150 opening data connection) */
if (dirlisting) {
if (r->content_type) {
if (mtime != 0L) {
* @@@ FIXME (e.g., for ftp://user@host/file*.tar.gz,
if (use_port) {
return HTTP_BAD_GATEWAY;
if (!data) {
return HTTP_INTERNAL_SERVER_ERROR;
if (dirlisting) {
if (!r->header_only) {
apr_bucket *e;
bb,
#if DEBUGGING
e = apr_bucket_flush_create();
if (data_sock) {
if (origin_sock) {
return OK;