proxy_ftp.c revision 4ecd546edd89824908c2a9ad2e07339d89368f9c
842ae4bd224140319ae7feec1872b93dfd491143fielding/* ====================================================================
842ae4bd224140319ae7feec1872b93dfd491143fielding * The Apache Software License, Version 1.1
842ae4bd224140319ae7feec1872b93dfd491143fielding * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
842ae4bd224140319ae7feec1872b93dfd491143fielding * reserved.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Redistribution and use in source and binary forms, with or without
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * modification, are permitted provided that the following conditions
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * 1. Redistributions of source code must retain the above copyright
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * notice, this list of conditions and the following disclaimer.
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.
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 * 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.
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.
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 * 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
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/* FTP routines for Apache proxy */
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovenerint ap_proxy_ftp_handler(request_rec *r, proxy_server_conf *conf,
fa123db15501821e36e513afa78e839775ad2800covenerapr_status_t ap_proxy_send_dir_filter(ap_filter_t * f,
0568280364eb026393be492ebc732795c4934643jorton * Decodes a '%' escaped string, and returns the number of characters
0568280364eb026393be492ebc732795c4934643jortonstatic int decodeenc(char *x)
0568280364eb026393be492ebc732795c4934643jorton int i, j, ch;
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 if (ch == '%' && ap_isxdigit(x[i + 1]) && ap_isxdigit(x[i + 2])) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes x[j] = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * checks an encoded ftp string for bad characters, namely, CR, LF or
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * non-ascii character
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int ftp_check_string(const char *x)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int i, ch = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (i = 0; x[i] != '\0'; i++) {
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe if (ch == '%' && ap_isxdigit(x[i + 1]) && ap_isxdigit(x[i + 2])) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#else /* APR_CHARSET_EBCDIC */
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin if (ch == '\r' || ch == '\n' || (os_toascii[ch] & 0x80))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif /* APR_CHARSET_EBCDIC */
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin * Canonicalise ftp URLs.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *user, *password, *host, *path, *parms, *strp, sport[7];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *err;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes def_port = apr_uri_default_port_for_scheme("ftp");
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes err = ap_proxy_canon_netloc(p, &url, &user, &password, &host, &port);
fa123db15501821e36e513afa78e839775ad2800covener if (password != NULL && !ftp_check_string(password))
fa123db15501821e36e513afa78e839775ad2800covener /* now parse path/parameters args, according to rfc1738 */
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.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes parms = ap_proxy_canonenc(p, strp, strlen(strp), enc_parm,
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener path = ap_proxy_canonenc(p, url, strlen(url), enc_path, r->proxyreq);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes strp = ap_proxy_canonenc(p, r->args, strlen(r->args), enc_parm, 1);
cceddc0b6c0fdaed0c73abda39975bb1d388243acovener strp = ap_proxy_canonenc(p, r->args, strlen(r->args), enc_fpath, 1);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener/* now, rebuild URL */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener r->filename = apr_pstrcat(p, "proxy:ftp://", (user != NULL) ? user : "",
6683642c1e0032eeeed5f99e8c14880692ef84c5sf/* we chop lines longer than 80 characters */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener * Reads response lines, returns both the ftp status code and
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener * remembers the response message in the supplied buffer
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenerstatic int ftp_getrc_msg(conn_rec *ftp_ctrl, apr_bucket_brigade *bb, char *msgbuf, int msglen)
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener if (APR_SUCCESS != (rv = ap_proxy_string_read(ftp_ctrl, bb, response, sizeof(response), &eos))) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: <FTP: %s", response);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener if (!apr_isdigit(response[0]) || !apr_isdigit(response[1]) ||
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener !apr_isdigit(response[2]) || (response[3] != ' ' && response[3] != '-'))
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener status = 100 * response[0] + 10 * response[1] + response[2] - 111 * '0';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener if (APR_SUCCESS != (rv = ap_proxy_string_read(ftp_ctrl, bb, response, sizeof(response), &eos))) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes mb = apr_cpystrn(mb, response + (' ' == response[0] ? 1 : 4), me - mb);
b08925593f214f621161742925dcf074a8047e0acovener/* this is a filter that turns a raw ASCII directory listing into pretty HTML */
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...
e8f95a682820a599fe41b22977010636be5c2717jim * all in good time...! :)
54d22ed1c429b903b029bbd62621f11a9e286137minfrintypedef struct {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholesapr_status_t ap_proxy_send_dir_filter(ap_filter_t *f, apr_bucket_brigade *in)
513b324e774c559b579896df131fd7c8471ed529rederpj regex_t *re = NULL; /* @@@ put this in the context */
513b324e774c559b579896df131fd7c8471ed529rederpj register int n;
513b324e774c559b579896df131fd7c8471ed529rederpj const char *pwd = apr_table_get(r->notes, "Directory-PWD");
513b324e774c559b579896df131fd7c8471ed529rederpj const char *readme = apr_table_get(r->notes, "Directory-README");
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 /* combine the stored and the new */
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 /* 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);
9ad7b260be233be7d7b5576979825cac72e15498rederpj /* If path began with /%2f, change the basedir */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* Strip off a type qualifier. It is ignored for dir listings */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes while (path[1] == '/') /* collapse multiple leading slashes to one */
ebe5305f8b22507374358f32b74d12fb50c05a25covener ++n; /* escaped character */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes reldir[0] = '\0'; /* strip off the wildcard suffix */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* Copy path, strip (all except the last) trailing slashes */
b08925593f214f621161742925dcf074a8047e0acovener /* (the trailing slash is needed for the dir component loop below) */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (n = strlen(path); n > 1 && path[n - 1] == '/' && path[n - 2] == '/'; --n)
9ad7b260be233be7d7b5576979825cac72e15498rederpj /* Add a link to the root directory (if %2f hack was used) */
707f6d077f73cc948deead8df5b40ea42c1eaa78covener str = (basedir[0] != '\0') ? "<a href=\"/%2f/\">%2f</a>/" : "";
9ad7b260be233be7d7b5576979825cac72e15498rederpj /* print "ftp://host/" */
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 for (dir = path+1; (dir = strchr(dir, '/')) != NULL; )
707f6d077f73cc948deead8df5b40ea42c1eaa78covener /* print "path/" component */
707f6d077f73cc948deead8df5b40ea42c1eaa78covener str = apr_psprintf(p, "<a href=\"%s%s/\">%s</a>/", basedir,
707f6d077f73cc948deead8df5b40ea42c1eaa78covener APR_BRIGADE_INSERT_TAIL(out, apr_bucket_pool_create(str, strlen(str), p));
707f6d077f73cc948deead8df5b40ea42c1eaa78covener APR_BRIGADE_INSERT_TAIL(out, apr_bucket_pool_create(wildcard, strlen(wildcard), p));
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) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem str = apr_psprintf(p, "</h2>\n\n(%s)\n\n <hr />\n\n<pre>",
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* print README */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes str = apr_psprintf(p, "%s\n</pre>\n\n<hr />\n\n<pre>\n",
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* make sure page intro gets sent out */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (APR_SUCCESS != (rv = ap_pass_brigade(f->next, out))) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf /* loop through each line of directory */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* get a complete line */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* if the buffer overruns - throw data away */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (APR_SUCCESS != (rv = apr_bucket_read(e, (const char **)&response, &len, APR_BLOCK_READ))) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* len+1 to leave space for the trailing nil char */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_cpystrn(ctx->buffer+strlen(ctx->buffer), response, len+1);
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* EOS? jump to footer */
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* not complete? leave and try get some more */
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (ctx->buffer[n-1] == CRLF[1]) /* strip trailing '\n' */
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (ctx->buffer[n-1] == CRLF[0]) /* strip trailing '\r' if present */
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* a symlink? */
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (ctx->buffer[0] == 'l' && (filename = strstr(ctx->buffer, " -> ")) != NULL) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes else if (ctx->buffer[0] == 'd' || ctx->buffer[0] == '-' || ctx->buffer[0] == 'l' || apr_isdigit(ctx->buffer[0])) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* handle filenames with spaces in 'em */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (!strcmp(filename, ".") || !strcmp(filename, "..") || firstfile) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes else if (searchidx != 0 && ctx->buffer[searchidx] != 0) {
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",
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)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes filename = apr_pstrndup(p, &ctx->buffer[re_result[2].rm_so], re_result[2].rm_eo - re_result[2].rm_so);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes str = ap_pstrcat(p, ap_escape_html(p, apr_pstrndup(p, ctx->buffer, re_result[2].rm_so)),
9c63a05713cb83a44a1590b4af33edeebf39f118sf /* erase buffer for next time around */
9c63a05713cb83a44a1590b4af33edeebf39f118sf if (APR_SUCCESS != (rv = ap_pass_brigade(f->next, out))) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf str = apr_psprintf(p, "</pre>\n\n <hr />\n\n %s\n\n </body>\n</html>\n", ap_psignature("", r));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (APR_SUCCESS != (rv = ap_pass_brigade(f->next, out))) {
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)
54d22ed1c429b903b029bbd62621f11a9e286137minfrinproxy_ftp_command(const char *cmd, request_rec *r, conn_rec *ftp_ctrl,
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /* If cmd == NULL, we retrieve the next ftp response line */
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 /* strip off the CRLF for logging */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener rc = ftp_getrc_msg(ftp_ctrl, bb, message, sizeof message);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
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,
e8f95a682820a599fe41b22977010636be5c2717jim static char old_type[2] = { 'A', '\0' }; /* After logon, mode is ASCII */
f0f6f1b90ab582896f8a7d56d85bd62a55e57d90covener /* set desired type */
560fd0658902ab57754616c172d8953e69fc4722bnicholes rc = proxy_ftp_command(apr_pstrcat(r->pool, "TYPE ", old_type, CRLF, NULL),
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 "Error reading from remote server");
560fd0658902ab57754616c172d8953e69fc4722bnicholes "Unable to set transfer type");
560fd0658902ab57754616c172d8953e69fc4722bnicholes/* Allow not implemented */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* ignore it silently */;
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)
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 "Failed to read PWD on ftp server");
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-)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int ftp_unauthorized(request_rec *r, int log_it)
e8f95a682820a599fe41b22977010636be5c2717jim * Log failed requests if they supplied a password (log username/password
560fd0658902ab57754616c172d8953e69fc4722bnicholes * guessing attempts)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_rerror(APLOG_MARK, APLOG_INFO | APLOG_NOERRNO, 0, r,
7a55c294da84865fe13262ed66ffd0c5841a9da5covener "proxy: missing or failed auth to %s",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_table_setn(r->err_headers_out, "WWW-Authenticate",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes APR_URI_UNP_OMITPASSWORD | APR_URI_UNP_OMITPATHINFO),
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>]
0e05808dc59a321566303084c84b9826a4353cefrederpjint ap_proxy_ftp_handler(request_rec *r, proxy_server_conf *conf,
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener/* char *account = NULL; how to supply an account in a URL? */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char xfer_type = 'A'; /* after ftp login, the default is ASCII */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* stuff for PASV mode */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* is this for us? */
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 ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
560fd0658902ab57754616c172d8953e69fc4722bnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
ae1981fc94adf2b231e2d0e15d2f895b2138c969covener /* create space for state information */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes backend = (proxy_conn_rec *) ap_get_module_config(c->conn_config, &proxy_ftp_module);
7a55c294da84865fe13262ed66ffd0c5841a9da5covener backend = ap_pcalloc(c->pool, sizeof(proxy_conn_rec));
fa123db15501821e36e513afa78e839775ad2800covener ap_set_module_config(c->conn_config, &proxy_ftp_module, backend);
fa123db15501821e36e513afa78e839775ad2800covener origin_sock = ap_get_module_config(backend->connection->conn_config, &core_module);
fa123db15501821e36e513afa78e839775ad2800covener * I: Who Do I Connect To? -----------------------
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Break up the URL to determine the host to connect to
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* we only support GET and HEAD */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* We break the URL into host, port, path-search */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes path = (path != NULL && path[0] != '\0') ? &path[1] : "";
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.
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 * 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 user = ap_getword_nulls(r->connection->pool, &password, ':');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((password = r->parsed_uri.password) != NULL) {
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 /* do a DNS lookup for the destination host */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes err = apr_sockaddr_info_get(&connect_addr, connectname, APR_UNSPEC, connectport, 0, p);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* check if ProxyBlock directive on this host */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (OK != ap_proxy_checkproxyblock(r, conf, connect_addr)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Connect to remote machine blocked");
e8f95a682820a599fe41b22977010636be5c2717jim * II: Make the Connection -----------------------
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * We have determined who to connect to. Now make the connection.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * get all the possible IP addresses for the destname and loop through
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * them until we get a successful connection
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_proxyerror(r, HTTP_BAD_GATEWAY, apr_pstrcat(p,
e8f95a682820a599fe41b22977010636be5c2717jim "DNS lookup failure for: ",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((rv = apr_socket_create(&sock, APR_INET, SOCK_STREAM, r->pool)) != APR_SUCCESS) {
43c3e6a4b559b76b750c245ee95e2782c15b4296jim "proxy: FTP: error creating socket");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "setsockopt(SO_RCVBUF): Failed to set ProxyReceiveBufferSize, using default");
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (APR_SUCCESS != (rv = apr_setsocketopt(sock, APR_SO_REUSEADDR, one))) {
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener#ifndef _OSD_POSIX /* BS2000 has this option "always on" */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe "proxy: FTP: error setting reuseaddr option: setsockopt(SO_REUSEADDR)");
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes#endif /* _OSD_POSIX */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf /* Set a timeout on the socket */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf apr_setsocketopt(sock, APR_SO_TIMEOUT, (int)(r->server->timeout * APR_USEC_PER_SEC));
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf "proxy: FTP: socket has been created");
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 * For now we do nothing, ie we get DNS round robin. XXX FIXME
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* try each IP address until we connect successfully */
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 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: trying to connect to %pI (%s)...", connect_addr, connectname);
e8f95a682820a599fe41b22977010636be5c2717jim /* make the connection out of the socket */
e8f95a682820a599fe41b22977010636be5c2717jim /* if an error occurred, loop round and try again */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: attempt to connect to %pI (%s) failed", connect_addr, connectname);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* if we get here, all is well */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* handle a permanent error from the above loop */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_proxyerror(r, HTTP_BAD_GATEWAY, apr_psprintf(r->pool,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Could not connect to remote machine: %s port %d",
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 * the peer reset the connection already; ap_new_connection() closed
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the socket
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 /* if a keepalive connection is floating around, close it first! */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* we might support ftp keepalives later, but not now... */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: control connection complete");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * III: Send Control Request -------------------------
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Log into the ftp server, send the username & password, change to the
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * correct directory...
e8f95a682820a599fe41b22977010636be5c2717jim /* set up the connection filters */
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);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return ap_proxyerror(r, HTTP_BAD_GATEWAY, "Error reading from remote server");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * RFC2616 states: 14.37 Retry-After
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 /* Look for a number, preceded by whitespace */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (apr_isspace(secs_str[-1]) && apr_isdigit(secs_str[0]))
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj apr_psprintf(p, "%lu", (unsigned long)(60 * secs)));
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj return ap_proxyerror(r, HTTP_SERVICE_UNAVAILABLE, ftpmessage);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: connected.");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes rc = proxy_ftp_command(apr_pstrcat(p, "USER ", user, CRLF, NULL),
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 return ap_proxyerror(r, HTTP_BAD_GATEWAY, "Error reading from remote server");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ftp_unauthorized(r, 1); /* log it: user name guessing
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * attempt? */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf return ftp_unauthorized(r, 0);
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf rc = proxy_ftp_command(apr_pstrcat(p, "PASS ", password, CRLF, NULL),
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. */
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj "Error reading from remote server");
e8f95a682820a599fe41b22977010636be5c2717jim apr_pstrcat(p, "Need account for login: ", ftpmessage, NULL));
e8f95a682820a599fe41b22977010636be5c2717jim /* @@@ questionable -- we might as well return a 403 Forbidden here */
e8f95a682820a599fe41b22977010636be5c2717jim return ftp_unauthorized(r, 1); /* log it: passwd guessing
e8f95a682820a599fe41b22977010636be5c2717jim * attempt? */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem apr_table_set(r->notes, "Directory-README", ftpmessage);
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
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 /* 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 "Error reading from remote server");
8869662bb1a4078297020e94ae5e928626d877c6rederpj return ap_proxyerror(r, HTTP_NOT_FOUND, ftpmessage);
8869662bb1a4078297020e94ae5e928626d877c6rederpj return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (type_suffix != NULL && strncmp(type_suffix, "type=", 5) == 0
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* "type=d" forces a dir listing.
8869662bb1a4078297020e94ae5e928626d877c6rederpj * The other types (i|a|e) are directly used for the ftp TYPE command
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if ( ! (dirlisting = (ap_tolower(type_suffix[5]) == 'd')))
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* Check valid types, rather than ignoring invalid types silently: */
8869662bb1a4078297020e94ae5e928626d877c6rederpj return ap_proxyerror(r, HTTP_BAD_REQUEST, ap_pstrcat(r->pool,
8869662bb1a4078297020e94ae5e928626d877c6rederpj "ftp proxy supports only types 'a', 'i', or 'e': \"",
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* make binary transfers the default */
8869662bb1a4078297020e94ae5e928626d877c6rederpj * IV: Make Data Connection? -------------------------
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * Try EPSV, if that fails... try PASV, if that fails... try PORT.
8869662bb1a4078297020e94ae5e928626d877c6rederpj/* this temporarily switches off EPSV/PASV */
8869662bb1a4078297020e94ae5e928626d877c6rederpj/*goto bypass;*/
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf /* set up data connection - EPSV */
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 /* 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 "Error reading from remote server");
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (rc != 229 && rc != 500 && rc != 501 && rc != 502) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
8869662bb1a4078297020e94ae5e928626d877c6rederpj pstr = apr_strtok(pstr, " ", &tok_cntx); /* separate result code */
8869662bb1a4078297020e94ae5e928626d877c6rederpj pstr = apr_strtok(NULL, "(", &tok_cntx); /* separate address &
8869662bb1a4078297020e94ae5e928626d877c6rederpj * port params */
8869662bb1a4078297020e94ae5e928626d877c6rederpj ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
8869662bb1a4078297020e94ae5e928626d877c6rederpj "proxy: FTP: EPSV contacting remote host on port %d",
8869662bb1a4078297020e94ae5e928626d877c6rederpj if ((rv = apr_socket_create(&data_sock, APR_INET, SOCK_STREAM, r->pool)) != APR_SUCCESS) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj "proxy: FTP: error creating EPSV socket");
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (conf->recv_buffer_size > 0 && (rv = apr_setsocketopt(data_sock, APR_SO_RCVBUF,
8869662bb1a4078297020e94ae5e928626d877c6rederpj "proxy: FTP: setsockopt(SO_RCVBUF): Failed to set ProxyReceiveBufferSize, using default");
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* make the connection */
8869662bb1a4078297020e94ae5e928626d877c6rederpj apr_sockaddr_info_get(&epsv_addr, data_ip, APR_INET, data_port, 0, p);
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 /* and try the regular way */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* set up data connection - PASV */
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 "Error reading from remote server");
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem/* FIXME: Check PASV against RFC1123 */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem pstr = apr_strtok(pstr, " ", &tok_cntx); /* separate result code */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem pstr = apr_strtok(NULL, "(", &tok_cntx); /* separate address &
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * port params */
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj/* FIXME: Only supports IPV4 - fix in RFC2428 */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem "%d,%d,%d,%d,%d,%d", &h3, &h2, &h1, &h0, &p1, &p0) == 6)) {
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 if ((rv = apr_socket_create(&data_sock, APR_INET, SOCK_STREAM, r->pool)) != APR_SUCCESS) {
a9c4332dc6241dc11dd104826bd179d42ccc0f12fuankg "proxy: error creating PASV socket");
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj if (conf->recv_buffer_size > 0 && (rv = apr_setsocketopt(data_sock, APR_SO_RCVBUF,
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem "proxy: FTP: setsockopt(SO_RCVBUF): Failed to set ProxyReceiveBufferSize, using default");
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);
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 /* and try the regular way */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj /* set up data connection - PORT */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if ((rv = apr_socket_create(&local_sock, APR_INET, SOCK_STREAM, r->pool)) != APR_SUCCESS) {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj "proxy: FTP: error creating local socket");
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if ((rv = apr_setsocketopt(local_sock, APR_SO_REUSEADDR, one)) != APR_SUCCESS) {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj#ifndef _OSD_POSIX /* BS2000 has this option "always on" */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj "proxy: FTP: error setting reuseaddr option");
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj#endif /* _OSD_POSIX */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem apr_sockaddr_info_get(&local_addr, local_ip, APR_UNSPEC, local_port, 0, r->pool);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj if ((rv = apr_bind(local_sock, local_addr)) != APR_SUCCESS) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem "proxy: FTP: error binding to ftp data socket %pI", local_addr);
0e05808dc59a321566303084c84b9826a4353cefrederpj /* only need a short queue */
0e05808dc59a321566303084c84b9826a4353cefrederpj if ((rv = apr_listen(local_sock, 2)) != APR_SUCCESS) {
0e05808dc59a321566303084c84b9826a4353cefrederpj "proxy: FTP: error listening to ftp data socket %pI", local_addr);
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf/* FIXME: Sent PORT here */
0e05808dc59a321566303084c84b9826a4353cefrederpj rc = proxy_ftp_command(apr_psprintf(p, "PORT %d,%d,%d,%d,%d,%d" CRLF, h3, h2, h1, h0, p1, p0),
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. */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj "Error reading from remote server");
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf /* signal that we must use the EPRT/PORT loop */
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|
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem return ap_proxyerror(r, HTTP_NOT_IMPLEMENTED, "Connect to IPV6 ftp server using EPRT not supported. Enable EPSV.");
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * V: Set The Headers -------------------
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * Get the size of the request, set up the environment for HTTP.
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj /* set request; "path" holds last path component */
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 rc = proxy_ftp_command(apr_pstrcat(p, "SIZE ", path, CRLF, NULL),
0e05808dc59a321566303084c84b9826a4353cefrederpj "Error reading from remote server");
0e05808dc59a321566303084c84b9826a4353cefrederpj size = ftpmessage; /* already pstrdup'ed: no copy necessary */
40a1aee60a66f7c8dbd0835fdd4f09334e12fc15rpluem ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
0e05808dc59a321566303084c84b9826a4353cefrederpj "proxy: FTP: SIZE shows this is a directory");
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 "Error reading from remote server");
0e05808dc59a321566303084c84b9826a4353cefrederpj return ap_proxyerror(r, HTTP_NOT_FOUND, ftpmessage);
0e05808dc59a321566303084c84b9826a4353cefrederpj return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
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 /* Note that we do not escape the path here, to allow for
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj * queries like: ftp://user@host/apache/src/server/http_*.c
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* switch to binary if the user did not specify ";type=a" */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj/* FIXME: Handle range requests - send REST */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj buf = apr_pstrcat(p, "RETR ", ap_escape_shell_cmd(p, path), CRLF, NULL);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem rc = proxy_ftp_command(buf, r, origin, bb, &ftpmessage);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* rc is an intermediate response for the LIST or RETR commands */
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,
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 "Error reading from remote server");
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj "proxy: FTP: RETR failed, trying LIST instead");
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe /* Directory Listings should always be fetched in ASCII mode */
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. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Error reading from remote server");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_proxyerror(r, HTTP_NOT_FOUND, ftpmessage);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
e8f95a682820a599fe41b22977010636be5c2717jim /* Update current directory after CWD */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* See above for the "LIST" vs. "LIST -lag" discussion. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes rc = proxy_ftp_command((cwd == NULL || strchr(cwd, '/') != NULL)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* rc is an intermediate response for the LIST command (125 transfer starting, 150 opening data connection) */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Error reading from remote server");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_proxyerror(r, HTTP_BAD_GATEWAY, ftpmessage);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_table_setn(r->headers_out, "Server", ap_get_server_version());
e8f95a682820a599fe41b22977010636be5c2717jim /* set 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);
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 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);
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 /* 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 /* wait for connection */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: failed to accept data connection");
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 * the peer reset the connection already; ap_new_connection() closed
e8f95a682820a599fe41b22977010636be5c2717jim * the socket
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: an error occurred creating the transfer connection");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* set up the connection filters */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_run_install_transport_filters(data, data_sock);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * VI: Receive the Response ------------------------
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Get response from the remote ftp socket, and pass it up the filter chain.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* send response */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* insert directory filter */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_add_output_filter("PROXY_SEND_DIR", NULL, r, r->connection);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* send body */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: FTP: start body send");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* read the body, pass it to the output filters */
e8f95a682820a599fe41b22977010636be5c2717jim while (ap_get_brigade(data->input_filters, bb, AP_MODE_EXHAUSTIVE,
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (ap_pass_brigade(r->output_filters, bb) != APR_SUCCESS) {
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* Ack! Phbtt! Die! User aborted! */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes "proxy: FTP: Closing Data connection.");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Retrieve the final response for the RETR or LIST commands */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes rc = proxy_ftp_command(NULL, r, origin, bb, &ftpmessage);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
7dbf29be626018bc389ef94c1846aeac4b72633bsf "proxy: FTP: end body send");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * VII: Clean Up -------------
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * If there are no KeepAlives, or if the connection has been signalled to
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * close, close the socket and clean up
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* finish */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* responses: 221, 500 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 221 Service closing control connection. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* 500 Syntax error, command unrecognized. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic void ap_proxy_ftp_register_hook(apr_pool_t *p)
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 */