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