xref: /PHP-8.1/ext/ftp/ftp.h (revision 01b3fc03)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Andrew Skalski <askalski@chek.com>                          |
14    |          Stefan Esser <sesser@php.net> (resume functions)            |
15    +----------------------------------------------------------------------+
16  */
17 
18 #ifndef	FTP_H
19 #define	FTP_H
20 
21 #include "php_network.h"
22 
23 #include <stdio.h>
24 #ifdef HAVE_NETINET_IN_H
25 #include <netinet/in.h>
26 #endif
27 
28 #define	FTP_DEFAULT_TIMEOUT	90
29 #define FTP_DEFAULT_AUTOSEEK 1
30 #define FTP_DEFAULT_USEPASVADDRESS	1
31 #define PHP_FTP_FAILED			0
32 #define PHP_FTP_FINISHED		1
33 #define PHP_FTP_MOREDATA		2
34 
35 /* XXX this should be configurable at runtime XXX */
36 #define	FTP_BUFSIZE	4096
37 
38 typedef enum ftptype {
39 	FTPTYPE_ASCII=1,
40 	FTPTYPE_IMAGE
41 } ftptype_t;
42 
43 typedef struct databuf
44 {
45 	int		listener;		/* listener socket */
46 	php_socket_t		fd;			/* data connection */
47 	ftptype_t	type;			/* transfer type */
48 	char		buf[FTP_BUFSIZE];	/* data buffer */
49 #ifdef HAVE_FTP_SSL
50 	SSL		*ssl_handle;	/* ssl handle */
51 	int		ssl_active;		/* flag if ssl is active or not */
52 #endif
53 } databuf_t;
54 
55 typedef struct ftpbuf
56 {
57 	php_socket_t		fd;			/* control connection */
58 	php_sockaddr_storage	localaddr;	/* local address */
59 	int		resp;			/* last response code */
60 	char		inbuf[FTP_BUFSIZE];	/* last response text */
61 	char		*extra;			/* extra characters */
62 	int		extralen;		/* number of extra chars */
63 	char		outbuf[FTP_BUFSIZE];	/* command output buffer */
64 	char		*pwd;			/* cached pwd */
65 	char		*syst;			/* cached system type */
66 	ftptype_t	type;			/* current transfer type */
67 	int		pasv;			/* 0=off; 1=pasv; 2=ready */
68 	php_sockaddr_storage	pasvaddr;	/* passive mode address */
69 	zend_long	timeout_sec;	/* User configurable timeout (seconds) */
70 	int			autoseek;	/* User configurable autoseek flag */
71 	int			usepasvaddress;	/* Use the address returned by the pasv command */
72 
73 	int				nb;		/* "nonblocking" transfer in progress */
74 	databuf_t		*data;	/* Data connection for "nonblocking" transfers */
75 	php_stream		*stream; /* output stream for "nonblocking" transfers */
76 	int				lastch;		/* last char of previous call */
77 	int				direction;	/* recv = 0 / send = 1 */
78 	int				closestream;/* close or not close stream */
79 #ifdef HAVE_FTP_SSL
80 	int				use_ssl; /* enable(1) or disable(0) ssl */
81 	int				use_ssl_for_data; /* en/disable ssl for the dataconnection */
82 	int				old_ssl;	/* old mode = forced data encryption */
83 	SSL				*ssl_handle;      /* handle for control connection */
84 	int				ssl_active;		  /* ssl active on control conn */
85 #endif
86 
87 } ftpbuf_t;
88 
89 
90 
91 /* open a FTP connection, returns ftpbuf (NULL on error)
92  * port is the ftp port in network byte order, or 0 for the default
93  */
94 ftpbuf_t*	ftp_open(const char *host, short port, zend_long timeout_sec);
95 
96 /* quits from the ftp session (it still needs to be closed)
97  * return true on success, false on error
98  */
99 int		ftp_quit(ftpbuf_t *ftp);
100 
101 /* frees up any cached data held in the ftp buffer */
102 void		ftp_gc(ftpbuf_t *ftp);
103 
104 /* close the FTP connection and return NULL */
105 ftpbuf_t*	ftp_close(ftpbuf_t *ftp);
106 
107 /* logs into the FTP server, returns true on success, false on error */
108 int		ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const char *pass, const size_t pass_len);
109 
110 /* reinitializes the connection, returns true on success, false on error */
111 int		ftp_reinit(ftpbuf_t *ftp);
112 
113 /* returns the remote system type (NULL on error) */
114 const char*	ftp_syst(ftpbuf_t *ftp);
115 
116 /* returns the present working directory (NULL on error) */
117 const char*	ftp_pwd(ftpbuf_t *ftp);
118 
119 /* exec a command [special features], return true on success, false on error */
120 int 	ftp_exec(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len);
121 
122 /* send a raw ftp command, return response as a hashtable, NULL on error */
123 void	ftp_raw(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len, zval *return_value);
124 
125 /* changes directories, return true on success, false on error */
126 int		ftp_chdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len);
127 
128 /* changes to parent directory, return true on success, false on error */
129 int		ftp_cdup(ftpbuf_t *ftp);
130 
131 /* creates a directory, return the directory name on success, NULL on error.
132  * the return value must be freed
133  */
134 zend_string* ftp_mkdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len);
135 
136 /* removes a directory, return true on success, false on error */
137 int		ftp_rmdir(ftpbuf_t *ftp, const char *dir, const size_t dir_len);
138 
139 /* Set permissions on a file */
140 int		ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const int filename_len);
141 
142 /* Allocate space on remote server with ALLO command
143  * Many servers will respond with 202 Allocation not necessary,
144  * however some servers will not accept STOR or APPE until ALLO is confirmed.
145  * If response is passed, it is estrdup()ed from ftp->inbuf and must be freed
146  * or assigned to a zval returned to the user */
147 int		ftp_alloc(ftpbuf_t *ftp, const zend_long size, zend_string **response);
148 
149 /* returns a NULL-terminated array of filenames in the given path
150  * or NULL on error.  the return array must be freed (but don't
151  * free the array elements)
152  */
153 char**		ftp_nlist(ftpbuf_t *ftp, const char *path, const size_t path_len);
154 
155 /* returns a NULL-terminated array of lines returned by the ftp
156  * LIST command for the given path or NULL on error.  the return
157  * array must be freed (but don't
158  * free the array elements)
159  */
160 char**		ftp_list(ftpbuf_t *ftp, const char *path, const size_t path_len, int recursive);
161 
162 /* populates a hashtable with the facts contained in one line of
163  * an MLSD response.
164  */
165 int			ftp_mlsd_parse_line(HashTable *ht, const char *input);
166 
167 /* returns a NULL-terminated array of lines returned by the ftp
168  * MLSD command for the given path or NULL on error.  the return
169  * array must be freed (but don't
170  * free the array elements)
171  */
172 char**		ftp_mlsd(ftpbuf_t *ftp, const char *path, const size_t path_len);
173 
174 /* switches passive mode on or off
175  * returns true on success, false on error
176  */
177 int		ftp_pasv(ftpbuf_t *ftp, int pasv);
178 
179 /* retrieves a file and saves its contents to outfp
180  * returns true on success, false on error
181  */
182 int		ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos);
183 
184 /* stores the data from a file, socket, or process as a file on the remote server
185  * returns true on success, false on error
186  */
187 int		ftp_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type, zend_long startpos);
188 
189 /* append the data from a file, socket, or process as a file on the remote server
190  * returns true on success, false on error
191  */
192 int		ftp_append(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type);
193 
194 /* returns the size of the given file, or -1 on error */
195 zend_long		ftp_size(ftpbuf_t *ftp, const char *path, const size_t path_len);
196 
197 /* returns the last modified time of the given file, or -1 on error */
198 time_t		ftp_mdtm(ftpbuf_t *ftp, const char *path, const size_t path_len);
199 
200 /* renames a file on the server */
201 int		ftp_rename(ftpbuf_t *ftp, const char *src, const size_t src_len, const char *dest, const size_t dest_len);
202 
203 /* deletes the file from the server */
204 int		ftp_delete(ftpbuf_t *ftp, const char *path, const size_t path_len);
205 
206 /* sends a SITE command to the server */
207 int		ftp_site(ftpbuf_t *ftp, const char *cmd, const size_t cmd_len);
208 
209 /* retrieves part of a file and saves its contents to outfp
210  * returns true on success, false on error
211  */
212 int		ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos);
213 
214 /* stores the data from a file, socket, or process as a file on the remote server
215  * returns true on success, false on error
216  */
217 int		ftp_nb_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_stream *instream, ftptype_t type, zend_long startpos);
218 
219 /* continues a previous nb_(f)get command
220  */
221 int		ftp_nb_continue_read(ftpbuf_t *ftp);
222 
223 /* continues a previous nb_(f)put command
224  */
225 int		ftp_nb_continue_write(ftpbuf_t *ftp);
226 
227 
228 #endif
229