Skip to content
Snippets Groups Projects
ftp.c 123 KiB
Newer Older
/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
Daniel Stenberg's avatar
Daniel Stenberg committed
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
Daniel Stenberg's avatar
Daniel Stenberg committed
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at http://curl.haxx.se/docs/copyright.html.
Daniel Stenberg's avatar
Daniel Stenberg committed
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
Daniel Stenberg's avatar
Daniel Stenberg committed
 *
Daniel Stenberg's avatar
Daniel Stenberg committed
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
Daniel Stenberg's avatar
Daniel Stenberg committed
 *
Daniel Stenberg's avatar
Daniel Stenberg committed
 * $Id$
 ***************************************************************************/
Daniel Stenberg's avatar
Daniel Stenberg committed

#include "setup.h"

Daniel Stenberg's avatar
Daniel Stenberg committed
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
Daniel Stenberg's avatar
Daniel Stenberg committed
#include <ctype.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

Daniel Stenberg's avatar
Daniel Stenberg committed
#ifdef HAVE_SYS_SOCKET_H
Daniel Stenberg's avatar
Daniel Stenberg committed
#include <sys/socket.h>
Daniel Stenberg's avatar
Daniel Stenberg committed
#endif
#ifdef HAVE_NETINET_IN_H
Daniel Stenberg's avatar
Daniel Stenberg committed
#include <netinet/in.h>
#endif
Daniel Stenberg's avatar
Daniel Stenberg committed
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_UTSNAME_H
Daniel Stenberg's avatar
Daniel Stenberg committed
#include <sys/utsname.h>
#ifdef HAVE_NETDB_H
Daniel Stenberg's avatar
Daniel Stenberg committed
#include <netdb.h>
#endif
Daniel Stenberg's avatar
Daniel Stenberg committed
#include <in.h>
Daniel Stenberg's avatar
Daniel Stenberg committed

#if (defined(NETWARE) && defined(__NOVELL_LIBC__))
#undef in_addr_t
#define in_addr_t unsigned long
#endif

Daniel Stenberg's avatar
Daniel Stenberg committed
#include <curl/curl.h>
#include "urldata.h"
#include "sendf.h"
#include "easyif.h" /* for Curl_convert_... prototypes */
Daniel Stenberg's avatar
Daniel Stenberg committed

#include "if2ip.h"
#include "hostip.h"
#include "progress.h"
Daniel Stenberg's avatar
Daniel Stenberg committed
#include "escape.h"
#include "http.h" /* for HTTP proxy tunnel stuff */
#include "socks.h"
Daniel Stenberg's avatar
Daniel Stenberg committed

#if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
#include "strtoofft.h"
#include "strequal.h"
#include "connect.h"
#include "strerror.h"
#include "select.h"
#include "parsedate.h" /* for the week day and month names */
#include "sockaddr.h" /* required for Curl_sockaddr_storage */
#include "url.h"
#if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
#include "inet_ntoa_r.h"
#endif

Daniel Stenberg's avatar
Daniel Stenberg committed
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>

/* The last #include file should be: */
#include "memdebug.h"
#endif
#define NIFLAGS NI_NUMERICHOST | NI_NUMERICSERV | NI_WITHSCOPEID
#else
#define NIFLAGS NI_NUMERICHOST | NI_NUMERICSERV
#endif

Yang Tse's avatar
Yang Tse committed
#ifdef CURL_DISABLE_VERBOSE_STRINGS
#define ftp_pasv_verbose(a,b,c,d)  do { } while (0)
#endif

Sterling Hughes's avatar
Sterling Hughes committed
/* Local API functions */
static CURLcode ftp_sendquote(struct connectdata *conn,
                              struct curl_slist *quote);
static CURLcode ftp_quit(struct connectdata *conn);
static CURLcode ftp_parse_url_path(struct connectdata *conn);
static CURLcode ftp_regular_transfer(struct connectdata *conn, bool *done);
Yang Tse's avatar
Yang Tse committed
#ifndef CURL_DISABLE_VERBOSE_STRINGS
static void ftp_pasv_verbose(struct connectdata *conn,
                             Curl_addrinfo *ai,
                             char *newhost, /* ascii version */
                             int port);
Yang Tse's avatar
Yang Tse committed
#endif
static CURLcode ftp_state_post_rest(struct connectdata *conn);
static CURLcode ftp_state_post_cwd(struct connectdata *conn);
static CURLcode ftp_state_quote(struct connectdata *conn,
                                bool init, ftpstate instate);
static CURLcode ftp_nb_type(struct connectdata *conn,
                            bool ascii, ftpstate newstate);
static int ftp_need_type(struct connectdata *conn,
static CURLcode Curl_ftp(struct connectdata *conn, bool *done);
static CURLcode Curl_ftp_done(struct connectdata *conn,
                              CURLcode, bool premature);
static CURLcode Curl_ftp_connect(struct connectdata *conn, bool *done);
static CURLcode Curl_ftp_disconnect(struct connectdata *conn);
static CURLcode Curl_ftp_nextconnect(struct connectdata *conn);
static CURLcode Curl_ftp_multi_statemach(struct connectdata *conn, bool *done);
static int Curl_ftp_getsock(struct connectdata *conn,
                            curl_socket_t *socks,
                            int numsocks);
static CURLcode Curl_ftp_doing(struct connectdata *conn,
                               bool *dophase_done);
static CURLcode Curl_ftp_setup_connection(struct connectdata * conn);
static CURLcode Curl_ftps_setup_connection(struct connectdata * conn);
#define FTPSENDF(x,y,z)    if ((result = Curl_ftpsendf(x,y,z)) != CURLE_OK) \
                              return result
#define NBFTPSENDF(x,y,z)  if ((result = Curl_nbftpsendf(x,y,z)) != CURLE_OK) \
                              return result
/*
 * FTP protocol handler.
 */

const struct Curl_handler Curl_handler_ftp = {
  "FTP",                                /* scheme */
  Curl_ftp_setup_connection,            /* setup_connection */
  Curl_ftp,                             /* do_it */
  Curl_ftp_done,                        /* done */
  Curl_ftp_nextconnect,                 /* do_more */
  Curl_ftp_connect,                     /* connect_it */
  Curl_ftp_multi_statemach,             /* connecting */
  Curl_ftp_doing,                       /* doing */
  Curl_ftp_getsock,                     /* proto_getsock */
  Curl_ftp_getsock,                     /* doing_getsock */
  Curl_ftp_disconnect,                  /* disconnect */
  PORT_FTP,                             /* defport */
  PROT_FTP                              /* protocol */
};


#ifdef USE_SSL
/*
 * FTPS protocol handler.
 */

const struct Curl_handler Curl_handler_ftps = {
  "FTPS",                               /* scheme */
  Curl_ftps_setup_connection,           /* setup_connection */
  Curl_ftp,                             /* do_it */
  Curl_ftp_done,                        /* done */
  Curl_ftp_nextconnect,                 /* do_more */
  Curl_ftp_connect,                     /* connect_it */
  Curl_ftp_multi_statemach,             /* connecting */
  Curl_ftp_doing,                       /* doing */
  Curl_ftp_getsock,                     /* proto_getsock */
  Curl_ftp_getsock,                     /* doing_getsock */
  Curl_ftp_disconnect,                  /* disconnect */
  PORT_FTPS,                            /* defport */
  PROT_FTP | PROT_FTPS | PROT_SSL       /* protocol */
};
#endif

#ifndef CURL_DISABLE_HTTP
/*
 * HTTP-proxyed FTP protocol handler.
 */

const struct Curl_handler Curl_handler_ftp_proxy = {
  "FTP",                                /* scheme */
  ZERO_NULL,                            /* setup_connection */
  Curl_http,                            /* do_it */
  Curl_http_done,                       /* done */
  ZERO_NULL,                            /* do_more */
  ZERO_NULL,                            /* connect_it */
  ZERO_NULL,                            /* connecting */
  ZERO_NULL,                            /* doing */
  ZERO_NULL,                            /* proto_getsock */
  ZERO_NULL,                            /* doing_getsock */
  ZERO_NULL,                            /* disconnect */
#ifdef USE_SSL
/*
 * HTTP-proxyed FTPS protocol handler.
 */

const struct Curl_handler Curl_handler_ftps_proxy = {
  "FTPS",                               /* scheme */
  ZERO_NULL,                            /* setup_connection */
  Curl_http,                            /* do_it */
  Curl_http_done,                       /* done */
  ZERO_NULL,                            /* do_more */
  ZERO_NULL,                            /* connect_it */
  ZERO_NULL,                            /* connecting */
  ZERO_NULL,                            /* doing */
  ZERO_NULL,                            /* proto_getsock */
  ZERO_NULL,                            /* doing_getsock */
  ZERO_NULL,                            /* disconnect */
  PORT_FTPS,                            /* defport */
  PROT_HTTP                             /* protocol */
};
/*
 * NOTE: back in the old days, we added code in the FTP code that made NOBODY
 * requests on files respond with headers passed to the client/stdout that
 * looked like HTTP ones.
 *
 * This approach is not very elegant, it causes confusion and is error-prone.
 * It is subject for removal at the next (or at least a future) soname bump.
 * Until then you can test the effects of the removal by undefining the
 * following define named CURL_FTP_HTTPSTYLE_HEAD.
 */
#define CURL_FTP_HTTPSTYLE_HEAD 1

static void freedirs(struct ftp_conn *ftpc)
  if(ftpc->dirs) {
    for (i=0; i < ftpc->dirdepth; i++){
      if(ftpc->dirs[i]) {
        free(ftpc->dirs[i]);
        ftpc->dirs[i]=NULL;
  if(ftpc->file) {
    free(ftpc->file);
    ftpc->file = NULL;
/* Returns non-zero if the given string contains CR (\r) or LF (\n),
   which 
Loading
Loading full blame...