xref: /curl/lib/ftp.h (revision c074ba64)
1 #ifndef HEADER_CURL_FTP_H
2 #define HEADER_CURL_FTP_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11  *
12  * This software is licensed as described in the file COPYING, which
13  * you should have received as part of this distribution. The terms
14  * are also available at https://curl.se/docs/copyright.html.
15  *
16  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17  * copies of the Software, and permit persons to whom the Software is
18  * furnished to do so, under the terms of the COPYING file.
19  *
20  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21  * KIND, either express or implied.
22  *
23  * SPDX-License-Identifier: curl
24  *
25  ***************************************************************************/
26 
27 #include "curl_setup.h"
28 
29 #include "pingpong.h"
30 
31 #ifndef CURL_DISABLE_FTP
32 extern const struct Curl_handler Curl_handler_ftp;
33 
34 #ifdef USE_SSL
35 extern const struct Curl_handler Curl_handler_ftps;
36 #endif
37 
38 CURLcode Curl_GetFTPResponse(struct Curl_easy *data, ssize_t *nread,
39                              int *ftpcode);
40 #endif /* CURL_DISABLE_FTP */
41 
42 /****************************************************************************
43  * FTP unique setup
44  ***************************************************************************/
45 enum {
46   FTP_STOP,    /* do nothing state, stops the state machine */
47   FTP_WAIT220, /* waiting for the initial 220 response immediately after
48                   a connect */
49   FTP_AUTH,
50   FTP_USER,
51   FTP_PASS,
52   FTP_ACCT,
53   FTP_PBSZ,
54   FTP_PROT,
55   FTP_CCC,
56   FTP_PWD,
57   FTP_SYST,
58   FTP_NAMEFMT,
59   FTP_QUOTE, /* waiting for a response to a command sent in a quote list */
60   FTP_RETR_PREQUOTE,
61   FTP_STOR_PREQUOTE,
62   FTP_POSTQUOTE,
63   FTP_CWD,  /* change dir */
64   FTP_MKD,  /* if the dir did not exist */
65   FTP_MDTM, /* to figure out the datestamp */
66   FTP_TYPE, /* to set type when doing a head-like request */
67   FTP_LIST_TYPE, /* set type when about to do a dir list */
68   FTP_RETR_TYPE, /* set type when about to RETR a file */
69   FTP_STOR_TYPE, /* set type when about to STOR a file */
70   FTP_SIZE, /* get the remote file's size for head-like request */
71   FTP_RETR_SIZE, /* get the remote file's size for RETR */
72   FTP_STOR_SIZE, /* get the size for STOR */
73   FTP_REST, /* when used to check if the server supports it in head-like */
74   FTP_RETR_REST, /* when asking for "resume" in for RETR */
75   FTP_PORT, /* generic state for PORT, LPRT and EPRT, check count1 */
76   FTP_PRET, /* generic state for PRET RETR, PRET STOR and PRET LIST/NLST */
77   FTP_PASV, /* generic state for PASV and EPSV, check count1 */
78   FTP_LIST, /* generic state for LIST, NLST or a custom list command */
79   FTP_RETR,
80   FTP_STOR, /* generic state for STOR and APPE */
81   FTP_QUIT,
82   FTP_LAST  /* never used */
83 };
84 typedef unsigned char ftpstate; /* use the enum values */
85 
86 struct ftp_parselist_data; /* defined later in ftplistparser.c */
87 
88 struct ftp_wc {
89   struct ftp_parselist_data *parser;
90 
91   struct {
92     curl_write_callback write_function;
93     FILE *file_descriptor;
94   } backup;
95 };
96 
97 typedef enum {
98   FTPFILE_MULTICWD  = 1, /* as defined by RFC1738 */
99   FTPFILE_NOCWD     = 2, /* use SIZE / RETR / STOR on the full path */
100   FTPFILE_SINGLECWD = 3  /* make one CWD, then SIZE / RETR / STOR on the
101                             file */
102 } curl_ftpfile;
103 
104 /* This FTP struct is used in the Curl_easy. All FTP data that is
105    connection-oriented must be in FTP_conn to properly deal with the fact that
106    perhaps the Curl_easy is changed between the times the connection is
107    used. */
108 struct FTP {
109   char *path;    /* points to the urlpieces struct field */
110   char *pathalloc; /* if non-NULL a pointer to an allocated path */
111 
112   /* transfer a file/body or not, done as a typedefed enum just to make
113      debuggers display the full symbol and not just the numerical value */
114   curl_pp_transfer transfer;
115   curl_off_t downloadsize;
116 };
117 
118 
119 /* ftp_conn is used for struct connection-oriented data in the connectdata
120    struct */
121 struct ftp_conn {
122   struct pingpong pp;
123   char *account;
124   char *alternative_to_user;
125   char *entrypath; /* the PWD reply when we logged on */
126   char *file;    /* url-decoded filename (or path) */
127   char **dirs;   /* realloc()ed array for path components */
128   char *newhost;
129   char *prevpath;   /* url-decoded conn->path from the previous transfer */
130   char transfertype; /* set by ftp_transfertype for use by Curl_client_write()a
131                         and others (A/I or zero) */
132   curl_off_t retr_size_saved; /* Size of retrieved file saved */
133   char *server_os;     /* The target server operating system. */
134   curl_off_t known_filesize; /* file size is different from -1, if wildcard
135                                 LIST parsing was done and wc_statemach set
136                                 it */
137   int dirdepth;  /* number of entries used in the 'dirs' array */
138   int cwdcount;     /* number of CWD commands issued */
139   int count1; /* general purpose counter for the state machine */
140   int count2; /* general purpose counter for the state machine */
141   int count3; /* general purpose counter for the state machine */
142   /* newhost is the (allocated) IP addr or hostname to connect the data
143      connection to */
144   unsigned short newport;
145   ftpstate state; /* always use ftp.c:state() to change state! */
146   ftpstate state_saved; /* transfer type saved to be reloaded after data
147                            connection is established */
148   unsigned char use_ssl;   /* if AUTH TLS is to be attempted etc, for FTP or
149                               IMAP or POP3 or others! (type: curl_usessl)*/
150   unsigned char ccc;       /* ccc level for this connection */
151   BIT(ftp_trying_alternative);
152   BIT(dont_check);  /* Set to TRUE to prevent the final (post-transfer)
153                        file size and 226/250 status check. It should still
154                        read the line, just ignore the result. */
155   BIT(ctl_valid);   /* Tells Curl_ftp_quit() whether or not to do anything. If
156                        the connection has timed out or been closed, this
157                        should be FALSE when it gets to Curl_ftp_quit() */
158   BIT(cwddone);     /* if it has been determined that the proper CWD combo
159                        already has been done */
160   BIT(cwdfail);     /* set TRUE if a CWD command fails, as then we must prevent
161                        caching the current directory */
162   BIT(wait_data_conn); /* this is set TRUE if data connection is waited */
163 };
164 
165 #define DEFAULT_ACCEPT_TIMEOUT   60000 /* milliseconds == one minute */
166 
167 #endif /* HEADER_CURL_FTP_H */
168