Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * nanoftp.c: basic FTP client support |
| 3 | * |
| 4 | * Reference: RFC 959 |
| 5 | */ |
| 6 | |
| 7 | #ifdef TESTING |
| 8 | #define STANDALONE |
| 9 | #define HAVE_STDLIB_H |
| 10 | #define HAVE_UNISTD_H |
| 11 | #define HAVE_SYS_SOCKET_H |
| 12 | #define HAVE_NETINET_IN_H |
| 13 | #define HAVE_NETDB_H |
| 14 | #define HAVE_SYS_TIME_H |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 15 | #else /* TESTING */ |
Daniel Veillard | f3afa7d | 2001-06-09 13:52:58 +0000 | [diff] [blame] | 16 | #define NEED_SOCKETS |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 17 | #endif /* TESTING */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 18 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 19 | #define IN_LIBXML |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 20 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 21 | |
| 22 | #ifdef LIBXML_FTP_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 23 | #include <string.h> |
| 24 | |
| 25 | #ifdef HAVE_STDLIB_H |
| 26 | #include <stdlib.h> |
| 27 | #endif |
| 28 | #ifdef HAVE_UNISTD_H |
| 29 | #include <unistd.h> |
| 30 | #endif |
| 31 | #ifdef HAVE_SYS_SOCKET_H |
| 32 | #include <sys/socket.h> |
| 33 | #endif |
| 34 | #ifdef HAVE_NETINET_IN_H |
| 35 | #include <netinet/in.h> |
| 36 | #endif |
| 37 | #ifdef HAVE_ARPA_INET_H |
| 38 | #include <arpa/inet.h> |
| 39 | #endif |
| 40 | #ifdef HAVE_NETDB_H |
| 41 | #include <netdb.h> |
| 42 | #endif |
| 43 | #ifdef HAVE_FCNTL_H |
| 44 | #include <fcntl.h> |
| 45 | #endif |
| 46 | #ifdef HAVE_ERRNO_H |
| 47 | #include <errno.h> |
| 48 | #endif |
| 49 | #ifdef HAVE_SYS_TIME_H |
| 50 | #include <sys/time.h> |
| 51 | #endif |
| 52 | #ifdef HAVE_SYS_SELECT_H |
| 53 | #include <sys/select.h> |
| 54 | #endif |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 55 | #ifdef HAVE_SYS_SOCKET_H |
| 56 | #include <sys/socket.h> |
| 57 | #endif |
| 58 | #ifdef HAVE_SYS_TYPES_H |
| 59 | #include <sys/types.h> |
| 60 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 61 | #ifdef HAVE_STRINGS_H |
| 62 | #include <strings.h> |
| 63 | #endif |
| 64 | |
| 65 | #include <libxml/xmlmemory.h> |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 66 | #include <libxml/parser.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 67 | #include <libxml/xmlerror.h> |
Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 68 | #include <libxml/uri.h> |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 69 | #include <libxml/nanoftp.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 70 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 71 | |
| 72 | /* #define DEBUG_FTP 1 */ |
| 73 | #ifdef STANDALONE |
| 74 | #ifndef DEBUG_FTP |
| 75 | #define DEBUG_FTP 1 |
| 76 | #endif |
| 77 | #endif |
| 78 | |
Daniel Veillard | 1638a47 | 2003-08-14 01:23:25 +0000 | [diff] [blame] | 79 | |
| 80 | #ifdef __MINGW32__ |
| 81 | #define _WINSOCKAPI_ |
| 82 | #include <wsockcompat.h> |
| 83 | #include <winsock2.h> |
| 84 | #undef SOCKLEN_T |
| 85 | #define SOCKLEN_T unsigned int |
| 86 | #endif |
| 87 | |
| 88 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 89 | /** |
| 90 | * A couple portability macros |
| 91 | */ |
| 92 | #ifndef _WINSOCKAPI_ |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 93 | #ifndef __BEOS__ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 94 | #define closesocket(s) close(s) |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 95 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 96 | #define SOCKET int |
| 97 | #endif |
Daniel Veillard | acf7ff0 | 2001-10-29 20:21:47 +0000 | [diff] [blame] | 98 | #if defined(VMS) || defined(__VMS) |
| 99 | #define SOCKLEN_T unsigned int |
| 100 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 101 | |
Daniel Veillard | 89f7f27 | 2003-09-29 13:29:09 +0000 | [diff] [blame] | 102 | #ifdef __BEOS__ |
| 103 | #ifndef PF_INET |
| 104 | #define PF_INET AF_INET |
| 105 | #endif |
| 106 | #endif |
| 107 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 108 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 109 | #define FTP_COMMAND_OK 200 |
| 110 | #define FTP_SYNTAX_ERROR 500 |
| 111 | #define FTP_GET_PASSWD 331 |
| 112 | #define FTP_BUF_SIZE 512 |
| 113 | |
| 114 | typedef struct xmlNanoFTPCtxt { |
| 115 | char *protocol; /* the protocol name */ |
| 116 | char *hostname; /* the host name */ |
| 117 | int port; /* the port */ |
| 118 | char *path; /* the path within the URL */ |
| 119 | char *user; /* user string */ |
| 120 | char *passwd; /* passwd string */ |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 121 | #ifdef SUPPORT_IP6 |
| 122 | struct sockaddr_storage ftpAddr; /* this is large enough to hold IPv6 address*/ |
| 123 | #else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 124 | struct sockaddr_in ftpAddr; /* the socket address struct */ |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 125 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 126 | int passive; /* currently we support only passive !!! */ |
| 127 | SOCKET controlFd; /* the file descriptor for the control socket */ |
| 128 | SOCKET dataFd; /* the file descriptor for the data socket */ |
| 129 | int state; /* WRITE / READ / CLOSED */ |
| 130 | int returnValue; /* the protocol return value */ |
| 131 | /* buffer for data received from the control connection */ |
| 132 | char controlBuf[FTP_BUF_SIZE + 1]; |
| 133 | int controlBufIndex; |
| 134 | int controlBufUsed; |
| 135 | int controlBufAnswer; |
| 136 | } xmlNanoFTPCtxt, *xmlNanoFTPCtxtPtr; |
| 137 | |
| 138 | static int initialized = 0; |
| 139 | static char *proxy = NULL; /* the proxy name if any */ |
| 140 | static int proxyPort = 0; /* the proxy port if any */ |
| 141 | static char *proxyUser = NULL; /* user for proxy authentication */ |
| 142 | static char *proxyPasswd = NULL;/* passwd for proxy authentication */ |
| 143 | static int proxyType = 0; /* uses TYPE or a@b ? */ |
| 144 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 145 | #ifdef SUPPORT_IP6 |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 146 | static |
| 147 | int have_ipv6(void) { |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 148 | int s; |
| 149 | |
| 150 | s = socket (AF_INET6, SOCK_STREAM, 0); |
| 151 | if (s != -1) { |
| 152 | close (s); |
| 153 | return (1); |
| 154 | } |
| 155 | return (0); |
| 156 | } |
| 157 | #endif |
| 158 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 159 | /** |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 160 | * xmlFTPErrMemory: |
| 161 | * @extra: extra informations |
| 162 | * |
| 163 | * Handle an out of memory condition |
| 164 | */ |
| 165 | static void |
| 166 | xmlFTPErrMemory(const char *extra) |
| 167 | { |
| 168 | __xmlSimpleError(XML_FROM_FTP, XML_ERR_NO_MEMORY, NULL, NULL, extra); |
| 169 | } |
| 170 | |
| 171 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 172 | * xmlNanoFTPInit: |
| 173 | * |
| 174 | * Initialize the FTP protocol layer. |
| 175 | * Currently it just checks for proxy informations, |
| 176 | * and get the hostname |
| 177 | */ |
| 178 | |
| 179 | void |
| 180 | xmlNanoFTPInit(void) { |
| 181 | const char *env; |
| 182 | #ifdef _WINSOCKAPI_ |
| 183 | WSADATA wsaData; |
| 184 | #endif |
| 185 | |
| 186 | if (initialized) |
| 187 | return; |
| 188 | |
| 189 | #ifdef _WINSOCKAPI_ |
| 190 | if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) |
| 191 | return; |
| 192 | #endif |
| 193 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 194 | proxyPort = 21; |
| 195 | env = getenv("no_proxy"); |
| 196 | if (env != NULL) |
| 197 | return; |
| 198 | env = getenv("ftp_proxy"); |
| 199 | if (env != NULL) { |
| 200 | xmlNanoFTPScanProxy(env); |
| 201 | } else { |
| 202 | env = getenv("FTP_PROXY"); |
| 203 | if (env != NULL) { |
| 204 | xmlNanoFTPScanProxy(env); |
| 205 | } |
| 206 | } |
| 207 | env = getenv("ftp_proxy_user"); |
| 208 | if (env != NULL) { |
| 209 | proxyUser = xmlMemStrdup(env); |
| 210 | } |
| 211 | env = getenv("ftp_proxy_password"); |
| 212 | if (env != NULL) { |
| 213 | proxyPasswd = xmlMemStrdup(env); |
| 214 | } |
| 215 | initialized = 1; |
| 216 | } |
| 217 | |
| 218 | /** |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 219 | * xmlNanoFTPCleanup: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 220 | * |
| 221 | * Cleanup the FTP protocol layer. This cleanup proxy informations. |
| 222 | */ |
| 223 | |
| 224 | void |
| 225 | xmlNanoFTPCleanup(void) { |
| 226 | if (proxy != NULL) { |
| 227 | xmlFree(proxy); |
| 228 | proxy = NULL; |
| 229 | } |
| 230 | if (proxyUser != NULL) { |
| 231 | xmlFree(proxyUser); |
| 232 | proxyUser = NULL; |
| 233 | } |
| 234 | if (proxyPasswd != NULL) { |
| 235 | xmlFree(proxyPasswd); |
| 236 | proxyPasswd = NULL; |
| 237 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 238 | #ifdef _WINSOCKAPI_ |
| 239 | if (initialized) |
| 240 | WSACleanup(); |
| 241 | #endif |
| 242 | initialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /** |
| 246 | * xmlNanoFTPProxy: |
| 247 | * @host: the proxy host name |
| 248 | * @port: the proxy port |
| 249 | * @user: the proxy user name |
| 250 | * @passwd: the proxy password |
| 251 | * @type: the type of proxy 1 for using SITE, 2 for USER a@b |
| 252 | * |
| 253 | * Setup the FTP proxy informations. |
| 254 | * This can also be done by using ftp_proxy ftp_proxy_user and |
| 255 | * ftp_proxy_password environment variables. |
| 256 | */ |
| 257 | |
| 258 | void |
| 259 | xmlNanoFTPProxy(const char *host, int port, const char *user, |
| 260 | const char *passwd, int type) { |
| 261 | if (proxy != NULL) |
| 262 | xmlFree(proxy); |
| 263 | if (proxyUser != NULL) |
| 264 | xmlFree(proxyUser); |
| 265 | if (proxyPasswd != NULL) |
| 266 | xmlFree(proxyPasswd); |
| 267 | if (host) |
| 268 | proxy = xmlMemStrdup(host); |
| 269 | if (user) |
| 270 | proxyUser = xmlMemStrdup(user); |
| 271 | if (passwd) |
| 272 | proxyPasswd = xmlMemStrdup(passwd); |
| 273 | proxyPort = port; |
| 274 | proxyType = type; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * xmlNanoFTPScanURL: |
| 279 | * @ctx: an FTP context |
| 280 | * @URL: The URL used to initialize the context |
| 281 | * |
| 282 | * (Re)Initialize an FTP context by parsing the URL and finding |
| 283 | * the protocol host port and path it indicates. |
| 284 | */ |
| 285 | |
| 286 | static void |
| 287 | xmlNanoFTPScanURL(void *ctx, const char *URL) { |
| 288 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 289 | const char *cur = URL; |
| 290 | char buf[4096]; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 291 | int indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 292 | int port = 0; |
| 293 | |
| 294 | if (ctxt->protocol != NULL) { |
| 295 | xmlFree(ctxt->protocol); |
| 296 | ctxt->protocol = NULL; |
| 297 | } |
| 298 | if (ctxt->hostname != NULL) { |
| 299 | xmlFree(ctxt->hostname); |
| 300 | ctxt->hostname = NULL; |
| 301 | } |
| 302 | if (ctxt->path != NULL) { |
| 303 | xmlFree(ctxt->path); |
| 304 | ctxt->path = NULL; |
| 305 | } |
| 306 | if (URL == NULL) return; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 307 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 308 | while (*cur != 0) { |
| 309 | if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 310 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 311 | ctxt->protocol = xmlMemStrdup(buf); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 312 | indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 313 | cur += 3; |
| 314 | break; |
| 315 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 316 | buf[indx++] = *cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 317 | } |
| 318 | if (*cur == 0) return; |
| 319 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 320 | buf[indx] = 0; |
Daniel Veillard | c69e0b1 | 2001-11-20 08:35:07 +0000 | [diff] [blame] | 321 | /* allow user@ and user:pass@ forms */ |
| 322 | { |
| 323 | const char *p = strchr(cur, '@'); |
| 324 | if(p) { |
| 325 | while(1) { |
| 326 | if(cur[0] == ':' || cur[0] == '@') break; |
| 327 | buf[indx++] = *cur++; |
| 328 | } |
| 329 | buf[indx] = 0; |
| 330 | ctxt->user = xmlMemStrdup(buf); |
| 331 | indx = 0; |
| 332 | if(cur[0] == ':') { |
| 333 | cur++; |
| 334 | while(1) { |
| 335 | if(cur[0] == '@') break; |
| 336 | buf[indx++] = *cur++; |
| 337 | } |
| 338 | buf[indx] = 0; |
| 339 | ctxt->passwd = xmlMemStrdup(buf); |
| 340 | indx = 0; |
| 341 | } |
| 342 | cur = p+1; |
| 343 | } |
| 344 | } |
| 345 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 346 | while (1) { |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 347 | if ((strchr (cur, '[') && !strchr (cur, ']')) || |
| 348 | (!strchr (cur, '[') && strchr (cur, ']'))) { |
| 349 | xmlGenericError (xmlGenericErrorContext, "\nxmlNanoFTPScanURL: %s", |
| 350 | "Syntax Error\n"); |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | if (cur[0] == '[') { |
| 355 | cur++; |
| 356 | while (cur[0] != ']') |
| 357 | buf[indx++] = *cur++; |
| 358 | |
| 359 | if (!strchr (buf, ':')) { |
| 360 | xmlGenericError (xmlGenericErrorContext, "\nxmlNanoFTPScanURL: %s", |
| 361 | "Use [IPv6]/IPv4 format\n"); |
| 362 | return; |
| 363 | } |
| 364 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 365 | buf[indx] = 0; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 366 | ctxt->hostname = xmlMemStrdup (buf); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 367 | indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 368 | cur += 1; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 369 | if (cur[0] == ':') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 370 | cur++; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 371 | while (*cur >= '0' && *cur <= '9') { |
| 372 | port *= 10; |
| 373 | port += *cur - '0'; |
| 374 | cur++; |
| 375 | } |
| 376 | |
| 377 | if (port != 0) ctxt->port = port; |
| 378 | while ((cur[0] != '/') && (*cur != 0)) |
| 379 | cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 380 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 381 | break; |
| 382 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 383 | else { /* address is an IPv4 one*/ |
| 384 | if (cur[0] == ':') { |
| 385 | buf[indx] = 0; |
| 386 | ctxt->hostname = xmlMemStrdup (buf); |
| 387 | indx = 0; |
| 388 | cur += 1; |
| 389 | while ((*cur >= '0') && (*cur <= '9')) { |
| 390 | port *= 10; |
| 391 | port += *cur - '0'; |
| 392 | cur++; |
| 393 | } |
| 394 | if (port != 0) ctxt->port = port; |
| 395 | while ((cur[0] != '/') && (*cur != 0)) |
| 396 | cur++; |
| 397 | break; |
| 398 | } |
| 399 | if ((*cur == '/') || (*cur == 0)) { |
| 400 | buf[indx] = 0; |
| 401 | ctxt->hostname = xmlMemStrdup (buf); |
| 402 | indx = 0; |
| 403 | break; |
| 404 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 405 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 406 | buf[indx++] = *cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 407 | } |
| 408 | if (*cur == 0) |
| 409 | ctxt->path = xmlMemStrdup("/"); |
| 410 | else { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 411 | indx = 0; |
| 412 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 413 | while (*cur != 0) |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 414 | buf[indx++] = *cur++; |
| 415 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 416 | ctxt->path = xmlMemStrdup(buf); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * xmlNanoFTPUpdateURL: |
| 422 | * @ctx: an FTP context |
| 423 | * @URL: The URL used to update the context |
| 424 | * |
| 425 | * Update an FTP context by parsing the URL and finding |
| 426 | * new path it indicates. If there is an error in the |
| 427 | * protocol, hostname, port or other information, the |
| 428 | * error is raised. It indicates a new connection has to |
| 429 | * be established. |
| 430 | * |
| 431 | * Returns 0 if Ok, -1 in case of error (other host). |
| 432 | */ |
| 433 | |
| 434 | int |
| 435 | xmlNanoFTPUpdateURL(void *ctx, const char *URL) { |
| 436 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 437 | const char *cur = URL; |
| 438 | char buf[4096]; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 439 | int indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 440 | int port = 0; |
| 441 | |
| 442 | if (URL == NULL) |
| 443 | return(-1); |
| 444 | if (ctxt == NULL) |
| 445 | return(-1); |
| 446 | if (ctxt->protocol == NULL) |
| 447 | return(-1); |
| 448 | if (ctxt->hostname == NULL) |
| 449 | return(-1); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 450 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 451 | while (*cur != 0) { |
| 452 | if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 453 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 454 | if (strcmp(ctxt->protocol, buf)) |
| 455 | return(-1); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 456 | indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 457 | cur += 3; |
| 458 | break; |
| 459 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 460 | buf[indx++] = *cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 461 | } |
| 462 | if (*cur == 0) |
| 463 | return(-1); |
| 464 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 465 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 466 | while (1) { |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 467 | if ((strchr (cur, '[') && !strchr (cur, ']')) || |
| 468 | (!strchr (cur, '[') && strchr (cur, ']'))) { |
| 469 | xmlGenericError (xmlGenericErrorContext, "\nxmlNanoFTPUpdateURL: %s", |
| 470 | "Syntax Error\n"); |
| 471 | return (-1); |
| 472 | } |
| 473 | |
| 474 | if (cur[0] == '[') { |
| 475 | cur++; |
| 476 | while (cur[0] != ']') |
| 477 | buf[indx++] = *cur++; |
| 478 | |
| 479 | if (!strchr (buf, ':')) { |
| 480 | xmlGenericError (xmlGenericErrorContext, "\nxmlNanoFTPUpdateURL: %s", |
| 481 | "Use [IPv6]/IPv4 format\n"); |
| 482 | return (-1); |
| 483 | } |
| 484 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 485 | buf[indx] = 0; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 486 | if (strcmp (ctxt->hostname, buf)) |
| 487 | return (-1); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 488 | indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 489 | cur += 1; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 490 | if (cur[0] == ':') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 491 | cur++; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 492 | while (*cur >= '0' && *cur <= '9') { |
| 493 | port *= 10; |
| 494 | port += *cur - '0'; |
| 495 | cur++; |
| 496 | } |
| 497 | |
| 498 | if (port != ctxt->port) |
| 499 | return (-1); |
| 500 | while ((cur[0] != '/') && (*cur != 0)) |
| 501 | cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 502 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 503 | break; |
| 504 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 505 | else { |
| 506 | if (cur[0] == ':') { |
| 507 | buf[indx] = 0; |
| 508 | if (strcmp (ctxt->hostname, buf)) |
| 509 | return (-1); |
| 510 | indx = 0; |
| 511 | cur += 1; |
| 512 | while ((*cur >= '0') && (*cur <= '9')) { |
| 513 | port *= 10; |
| 514 | port += *cur - '0'; |
| 515 | cur++; |
| 516 | } |
| 517 | if (port != ctxt->port) |
| 518 | return (-1); |
| 519 | while ((cur[0] != '/') && (*cur != 0)) |
| 520 | cur++; |
| 521 | break; |
| 522 | } |
| 523 | if ((*cur == '/') || (*cur == 0)) { |
| 524 | buf[indx] = 0; |
| 525 | if (strcmp (ctxt->hostname, buf)) |
| 526 | return (-1); |
| 527 | indx = 0; |
| 528 | break; |
| 529 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 530 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 531 | buf[indx++] = *cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 532 | } |
| 533 | if (ctxt->path != NULL) { |
| 534 | xmlFree(ctxt->path); |
| 535 | ctxt->path = NULL; |
| 536 | } |
| 537 | |
| 538 | if (*cur == 0) |
| 539 | ctxt->path = xmlMemStrdup("/"); |
| 540 | else { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 541 | indx = 0; |
| 542 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 543 | while (*cur != 0) |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 544 | buf[indx++] = *cur++; |
| 545 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 546 | ctxt->path = xmlMemStrdup(buf); |
| 547 | } |
| 548 | return(0); |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * xmlNanoFTPScanProxy: |
| 553 | * @URL: The proxy URL used to initialize the proxy context |
| 554 | * |
| 555 | * (Re)Initialize the FTP Proxy context by parsing the URL and finding |
| 556 | * the protocol host port it indicates. |
| 557 | * Should be like ftp://myproxy/ or ftp://myproxy:3128/ |
| 558 | * A NULL URL cleans up proxy informations. |
| 559 | */ |
| 560 | |
| 561 | void |
| 562 | xmlNanoFTPScanProxy(const char *URL) { |
| 563 | const char *cur = URL; |
| 564 | char buf[4096]; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 565 | int indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 566 | int port = 0; |
| 567 | |
| 568 | if (proxy != NULL) { |
| 569 | xmlFree(proxy); |
| 570 | proxy = NULL; |
| 571 | } |
| 572 | if (proxyPort != 0) { |
| 573 | proxyPort = 0; |
| 574 | } |
| 575 | #ifdef DEBUG_FTP |
| 576 | if (URL == NULL) |
| 577 | xmlGenericError(xmlGenericErrorContext, "Removing FTP proxy info\n"); |
| 578 | else |
| 579 | xmlGenericError(xmlGenericErrorContext, "Using FTP proxy %s\n", URL); |
| 580 | #endif |
| 581 | if (URL == NULL) return; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 582 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 583 | while (*cur != 0) { |
| 584 | if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 585 | buf[indx] = 0; |
| 586 | indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 587 | cur += 3; |
| 588 | break; |
| 589 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 590 | buf[indx++] = *cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 591 | } |
| 592 | if (*cur == 0) return; |
| 593 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 594 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 595 | while (1) { |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 596 | if ((strchr (cur, '[') && !strchr (cur, ']')) || |
| 597 | (!strchr (cur, '[') && strchr (cur, ']'))) { |
| 598 | xmlGenericError (xmlGenericErrorContext, "\nxmlNanoFTPScanProxy: %s", |
| 599 | "Syntax error\n"); |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | if (cur[0] == '[') { |
| 604 | cur++; |
| 605 | while (cur[0] != ']') |
| 606 | buf[indx++] = *cur++; |
| 607 | if (!strchr (buf, ':')) { |
| 608 | xmlGenericError (xmlGenericErrorContext, "\nxmlNanoFTPScanProxy: %s", |
| 609 | "Use [IPv6]/IPv4 format\n"); |
| 610 | return; |
| 611 | } |
| 612 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 613 | buf[indx] = 0; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 614 | proxy = xmlMemStrdup (buf); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 615 | indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 616 | cur += 1; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 617 | if (cur[0] == ':') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 618 | cur++; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 619 | while (*cur >= '0' && *cur <= '9') { |
| 620 | port *= 10; |
| 621 | port += *cur - '0'; |
| 622 | cur++; |
| 623 | } |
| 624 | |
| 625 | if (port != 0) proxyPort = port; |
| 626 | while ((cur[0] != '/') && (*cur != 0)) |
| 627 | cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 628 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 629 | break; |
| 630 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 631 | else { |
| 632 | if (cur[0] == ':') { |
| 633 | buf[indx] = 0; |
| 634 | proxy = xmlMemStrdup (buf); |
| 635 | indx = 0; |
| 636 | cur += 1; |
| 637 | while ((*cur >= '0') && (*cur <= '9')) { |
| 638 | port *= 10; |
| 639 | port += *cur - '0'; |
| 640 | cur++; |
| 641 | } |
| 642 | if (port != 0) proxyPort = port; |
| 643 | while ((cur[0] != '/') && (*cur != 0)) |
| 644 | cur++; |
| 645 | break; |
| 646 | } |
| 647 | if ((*cur == '/') || (*cur == 0)) { |
| 648 | buf[indx] = 0; |
| 649 | proxy = xmlMemStrdup (buf); |
| 650 | indx = 0; |
| 651 | break; |
| 652 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 653 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 654 | buf[indx++] = *cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * xmlNanoFTPNewCtxt: |
| 660 | * @URL: The URL used to initialize the context |
| 661 | * |
| 662 | * Allocate and initialize a new FTP context. |
| 663 | * |
| 664 | * Returns an FTP context or NULL in case of error. |
| 665 | */ |
| 666 | |
| 667 | void* |
| 668 | xmlNanoFTPNewCtxt(const char *URL) { |
| 669 | xmlNanoFTPCtxtPtr ret; |
Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 670 | char *unescaped; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 671 | |
| 672 | ret = (xmlNanoFTPCtxtPtr) xmlMalloc(sizeof(xmlNanoFTPCtxt)); |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 673 | if (ret == NULL) { |
| 674 | xmlFTPErrMemory("allocating FTP context"); |
| 675 | return(NULL); |
| 676 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 677 | |
| 678 | memset(ret, 0, sizeof(xmlNanoFTPCtxt)); |
| 679 | ret->port = 21; |
| 680 | ret->passive = 1; |
| 681 | ret->returnValue = 0; |
| 682 | ret->controlBufIndex = 0; |
| 683 | ret->controlBufUsed = 0; |
Daniel Veillard | c69e0b1 | 2001-11-20 08:35:07 +0000 | [diff] [blame] | 684 | ret->controlFd = -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 685 | |
Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 686 | unescaped = xmlURIUnescapeString(URL, 0, NULL); |
| 687 | if (unescaped != NULL) |
| 688 | xmlNanoFTPScanURL(ret, unescaped); |
| 689 | else if (URL != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 690 | xmlNanoFTPScanURL(ret, URL); |
Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 691 | xmlFree(unescaped); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 692 | |
| 693 | return(ret); |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * xmlNanoFTPFreeCtxt: |
| 698 | * @ctx: an FTP context |
| 699 | * |
| 700 | * Frees the context after closing the connection. |
| 701 | */ |
| 702 | |
| 703 | void |
| 704 | xmlNanoFTPFreeCtxt(void * ctx) { |
| 705 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 706 | if (ctxt == NULL) return; |
| 707 | if (ctxt->hostname != NULL) xmlFree(ctxt->hostname); |
| 708 | if (ctxt->protocol != NULL) xmlFree(ctxt->protocol); |
| 709 | if (ctxt->path != NULL) xmlFree(ctxt->path); |
| 710 | ctxt->passive = 1; |
| 711 | if (ctxt->controlFd >= 0) closesocket(ctxt->controlFd); |
| 712 | ctxt->controlFd = -1; |
| 713 | ctxt->controlBufIndex = -1; |
| 714 | ctxt->controlBufUsed = -1; |
| 715 | xmlFree(ctxt); |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * xmlNanoFTPParseResponse: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 720 | * @buf: the buffer containing the response |
| 721 | * @len: the buffer length |
| 722 | * |
| 723 | * Parsing of the server answer, we just extract the code. |
| 724 | * |
| 725 | * returns 0 for errors |
| 726 | * +XXX for last line of response |
| 727 | * -XXX for response to be continued |
| 728 | */ |
| 729 | static int |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 730 | xmlNanoFTPParseResponse(char *buf, int len) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 731 | int val = 0; |
| 732 | |
| 733 | if (len < 3) return(-1); |
| 734 | if ((*buf >= '0') && (*buf <= '9')) |
| 735 | val = val * 10 + (*buf - '0'); |
| 736 | else |
| 737 | return(0); |
| 738 | buf++; |
| 739 | if ((*buf >= '0') && (*buf <= '9')) |
| 740 | val = val * 10 + (*buf - '0'); |
| 741 | else |
| 742 | return(0); |
| 743 | buf++; |
| 744 | if ((*buf >= '0') && (*buf <= '9')) |
| 745 | val = val * 10 + (*buf - '0'); |
| 746 | else |
| 747 | return(0); |
| 748 | buf++; |
| 749 | if (*buf == '-') |
| 750 | return(-val); |
| 751 | return(val); |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * xmlNanoFTPGetMore: |
| 756 | * @ctx: an FTP context |
| 757 | * |
| 758 | * Read more information from the FTP control connection |
| 759 | * Returns the number of bytes read, < 0 indicates an error |
| 760 | */ |
| 761 | static int |
| 762 | xmlNanoFTPGetMore(void *ctx) { |
| 763 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 764 | int len; |
| 765 | int size; |
| 766 | |
| 767 | if ((ctxt->controlBufIndex < 0) || (ctxt->controlBufIndex > FTP_BUF_SIZE)) { |
| 768 | #ifdef DEBUG_FTP |
| 769 | xmlGenericError(xmlGenericErrorContext, |
| 770 | "xmlNanoFTPGetMore : controlBufIndex = %d\n", |
| 771 | ctxt->controlBufIndex); |
| 772 | #endif |
| 773 | return(-1); |
| 774 | } |
| 775 | |
| 776 | if ((ctxt->controlBufUsed < 0) || (ctxt->controlBufUsed > FTP_BUF_SIZE)) { |
| 777 | #ifdef DEBUG_FTP |
| 778 | xmlGenericError(xmlGenericErrorContext, |
| 779 | "xmlNanoFTPGetMore : controlBufUsed = %d\n", |
| 780 | ctxt->controlBufUsed); |
| 781 | #endif |
| 782 | return(-1); |
| 783 | } |
| 784 | if (ctxt->controlBufIndex > ctxt->controlBufUsed) { |
| 785 | #ifdef DEBUG_FTP |
| 786 | xmlGenericError(xmlGenericErrorContext, |
| 787 | "xmlNanoFTPGetMore : controlBufIndex > controlBufUsed %d > %d\n", |
| 788 | ctxt->controlBufIndex, ctxt->controlBufUsed); |
| 789 | #endif |
| 790 | return(-1); |
| 791 | } |
| 792 | |
| 793 | /* |
| 794 | * First pack the control buffer |
| 795 | */ |
| 796 | if (ctxt->controlBufIndex > 0) { |
| 797 | memmove(&ctxt->controlBuf[0], &ctxt->controlBuf[ctxt->controlBufIndex], |
| 798 | ctxt->controlBufUsed - ctxt->controlBufIndex); |
| 799 | ctxt->controlBufUsed -= ctxt->controlBufIndex; |
| 800 | ctxt->controlBufIndex = 0; |
| 801 | } |
| 802 | size = FTP_BUF_SIZE - ctxt->controlBufUsed; |
| 803 | if (size == 0) { |
| 804 | #ifdef DEBUG_FTP |
| 805 | xmlGenericError(xmlGenericErrorContext, |
| 806 | "xmlNanoFTPGetMore : buffer full %d \n", ctxt->controlBufUsed); |
| 807 | #endif |
| 808 | return(0); |
| 809 | } |
| 810 | |
| 811 | /* |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 812 | * Read the amount left on the control connection |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 813 | */ |
| 814 | if ((len = recv(ctxt->controlFd, &ctxt->controlBuf[ctxt->controlBufIndex], |
| 815 | size, 0)) < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 816 | __xmlIOErr(XML_FROM_FTP, 0, "recv failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 817 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
| 818 | ctxt->controlFd = -1; |
| 819 | return(-1); |
| 820 | } |
| 821 | #ifdef DEBUG_FTP |
| 822 | xmlGenericError(xmlGenericErrorContext, |
| 823 | "xmlNanoFTPGetMore : read %d [%d - %d]\n", len, |
| 824 | ctxt->controlBufUsed, ctxt->controlBufUsed + len); |
| 825 | #endif |
| 826 | ctxt->controlBufUsed += len; |
| 827 | ctxt->controlBuf[ctxt->controlBufUsed] = 0; |
| 828 | |
| 829 | return(len); |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * xmlNanoFTPReadResponse: |
| 834 | * @ctx: an FTP context |
| 835 | * |
| 836 | * Read the response from the FTP server after a command. |
| 837 | * Returns the code number |
| 838 | */ |
| 839 | static int |
| 840 | xmlNanoFTPReadResponse(void *ctx) { |
| 841 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 842 | char *ptr, *end; |
| 843 | int len; |
| 844 | int res = -1, cur = -1; |
| 845 | |
| 846 | get_more: |
| 847 | /* |
| 848 | * Assumes everything up to controlBuf[controlBufIndex] has been read |
| 849 | * and analyzed. |
| 850 | */ |
| 851 | len = xmlNanoFTPGetMore(ctx); |
| 852 | if (len < 0) { |
| 853 | return(-1); |
| 854 | } |
| 855 | if ((ctxt->controlBufUsed == 0) && (len == 0)) { |
| 856 | return(-1); |
| 857 | } |
| 858 | ptr = &ctxt->controlBuf[ctxt->controlBufIndex]; |
| 859 | end = &ctxt->controlBuf[ctxt->controlBufUsed]; |
| 860 | |
| 861 | #ifdef DEBUG_FTP |
| 862 | xmlGenericError(xmlGenericErrorContext, |
| 863 | "\n<<<\n%s\n--\n", ptr); |
| 864 | #endif |
| 865 | while (ptr < end) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 866 | cur = xmlNanoFTPParseResponse(ptr, end - ptr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 867 | if (cur > 0) { |
| 868 | /* |
| 869 | * Successfully scanned the control code, scratch |
| 870 | * till the end of the line, but keep the index to be |
| 871 | * able to analyze the result if needed. |
| 872 | */ |
| 873 | res = cur; |
| 874 | ptr += 3; |
| 875 | ctxt->controlBufAnswer = ptr - ctxt->controlBuf; |
| 876 | while ((ptr < end) && (*ptr != '\n')) ptr++; |
| 877 | if (*ptr == '\n') ptr++; |
| 878 | if (*ptr == '\r') ptr++; |
| 879 | break; |
| 880 | } |
| 881 | while ((ptr < end) && (*ptr != '\n')) ptr++; |
| 882 | if (ptr >= end) { |
| 883 | ctxt->controlBufIndex = ctxt->controlBufUsed; |
| 884 | goto get_more; |
| 885 | } |
| 886 | if (*ptr != '\r') ptr++; |
| 887 | } |
| 888 | |
| 889 | if (res < 0) goto get_more; |
| 890 | ctxt->controlBufIndex = ptr - ctxt->controlBuf; |
| 891 | #ifdef DEBUG_FTP |
| 892 | ptr = &ctxt->controlBuf[ctxt->controlBufIndex]; |
| 893 | xmlGenericError(xmlGenericErrorContext, "\n---\n%s\n--\n", ptr); |
| 894 | #endif |
| 895 | |
| 896 | #ifdef DEBUG_FTP |
| 897 | xmlGenericError(xmlGenericErrorContext, "Got %d\n", res); |
| 898 | #endif |
| 899 | return(res / 100); |
| 900 | } |
| 901 | |
| 902 | /** |
| 903 | * xmlNanoFTPGetResponse: |
| 904 | * @ctx: an FTP context |
| 905 | * |
| 906 | * Get the response from the FTP server after a command. |
| 907 | * Returns the code number |
| 908 | */ |
| 909 | |
| 910 | int |
| 911 | xmlNanoFTPGetResponse(void *ctx) { |
| 912 | int res; |
| 913 | |
| 914 | res = xmlNanoFTPReadResponse(ctx); |
| 915 | |
| 916 | return(res); |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * xmlNanoFTPCheckResponse: |
| 921 | * @ctx: an FTP context |
| 922 | * |
| 923 | * Check if there is a response from the FTP server after a command. |
| 924 | * Returns the code number, or 0 |
| 925 | */ |
| 926 | |
| 927 | int |
| 928 | xmlNanoFTPCheckResponse(void *ctx) { |
| 929 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 930 | fd_set rfd; |
| 931 | struct timeval tv; |
| 932 | |
| 933 | tv.tv_sec = 0; |
| 934 | tv.tv_usec = 0; |
| 935 | FD_ZERO(&rfd); |
| 936 | FD_SET(ctxt->controlFd, &rfd); |
| 937 | switch(select(ctxt->controlFd + 1, &rfd, NULL, NULL, &tv)) { |
| 938 | case 0: |
| 939 | return(0); |
| 940 | case -1: |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 941 | __xmlIOErr(XML_FROM_FTP, 0, "select"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 942 | return(-1); |
| 943 | |
| 944 | } |
| 945 | |
| 946 | return(xmlNanoFTPReadResponse(ctx)); |
| 947 | } |
| 948 | |
| 949 | /** |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 950 | * Send the user authentication |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 951 | */ |
| 952 | |
| 953 | static int |
| 954 | xmlNanoFTPSendUser(void *ctx) { |
| 955 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 956 | char buf[200]; |
| 957 | int len; |
| 958 | int res; |
| 959 | |
| 960 | if (ctxt->user == NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 961 | snprintf(buf, sizeof(buf), "USER anonymous\r\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 962 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 963 | snprintf(buf, sizeof(buf), "USER %s\r\n", ctxt->user); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 964 | buf[sizeof(buf) - 1] = 0; |
| 965 | len = strlen(buf); |
| 966 | #ifdef DEBUG_FTP |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 967 | xmlGenericError(xmlGenericErrorContext, "%s", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 968 | #endif |
| 969 | res = send(ctxt->controlFd, buf, len, 0); |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 970 | if (res < 0) { |
| 971 | __xmlIOErr(XML_FROM_FTP, 0, "send failed"); |
| 972 | return(res); |
| 973 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 974 | return(0); |
| 975 | } |
| 976 | |
| 977 | /** |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 978 | * Send the password authentication |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 979 | */ |
| 980 | |
| 981 | static int |
| 982 | xmlNanoFTPSendPasswd(void *ctx) { |
| 983 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 984 | char buf[200]; |
| 985 | int len; |
| 986 | int res; |
| 987 | |
| 988 | if (ctxt->passwd == NULL) |
Daniel Veillard | 9ae1eba | 2001-10-19 09:48:35 +0000 | [diff] [blame] | 989 | snprintf(buf, sizeof(buf), "PASS anonymous@\r\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 990 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 991 | snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 992 | buf[sizeof(buf) - 1] = 0; |
| 993 | len = strlen(buf); |
| 994 | #ifdef DEBUG_FTP |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 995 | xmlGenericError(xmlGenericErrorContext, "%s", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 996 | #endif |
| 997 | res = send(ctxt->controlFd, buf, len, 0); |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 998 | if (res < 0) { |
| 999 | __xmlIOErr(XML_FROM_FTP, 0, "send failed"); |
| 1000 | return(res); |
| 1001 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1002 | return(0); |
| 1003 | } |
| 1004 | |
| 1005 | /** |
| 1006 | * xmlNanoFTPQuit: |
| 1007 | * @ctx: an FTP context |
| 1008 | * |
| 1009 | * Send a QUIT command to the server |
| 1010 | * |
| 1011 | * Returns -1 in case of error, 0 otherwise |
| 1012 | */ |
| 1013 | |
| 1014 | |
| 1015 | int |
| 1016 | xmlNanoFTPQuit(void *ctx) { |
| 1017 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 1018 | char buf[200]; |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1019 | int len, res; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1020 | |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1021 | snprintf(buf, sizeof(buf), "QUIT\r\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1022 | len = strlen(buf); |
| 1023 | #ifdef DEBUG_FTP |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 1024 | xmlGenericError(xmlGenericErrorContext, "%s", buf); /* Just to be consistent, even though we know it can't have a % in it */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1025 | #endif |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1026 | res = send(ctxt->controlFd, buf, len, 0); |
| 1027 | if (res < 0) { |
| 1028 | __xmlIOErr(XML_FROM_FTP, 0, "send failed"); |
| 1029 | return(res); |
| 1030 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1031 | return(0); |
| 1032 | } |
| 1033 | |
| 1034 | /** |
| 1035 | * xmlNanoFTPConnect: |
| 1036 | * @ctx: an FTP context |
| 1037 | * |
| 1038 | * Tries to open a control connection |
| 1039 | * |
| 1040 | * Returns -1 in case of error, 0 otherwise |
| 1041 | */ |
| 1042 | |
| 1043 | int |
| 1044 | xmlNanoFTPConnect(void *ctx) { |
| 1045 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 1046 | struct hostent *hp; |
| 1047 | int port; |
| 1048 | int res; |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 1049 | int addrlen = sizeof (struct sockaddr_in); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1050 | |
| 1051 | if (ctxt == NULL) |
| 1052 | return(-1); |
| 1053 | if (ctxt->hostname == NULL) |
| 1054 | return(-1); |
| 1055 | |
| 1056 | /* |
| 1057 | * do the blocking DNS query. |
| 1058 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1059 | if (proxy) { |
| 1060 | port = proxyPort; |
| 1061 | } else { |
| 1062 | port = ctxt->port; |
| 1063 | } |
| 1064 | if (port == 0) |
| 1065 | port = 21; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1066 | |
| 1067 | memset (&ctxt->ftpAddr, 0, sizeof(ctxt->ftpAddr)); |
| 1068 | |
| 1069 | #ifdef SUPPORT_IP6 |
| 1070 | if (have_ipv6 ()) { |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 1071 | struct addrinfo hints, *tmp, *result; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1072 | |
| 1073 | result = NULL; |
| 1074 | memset (&hints, 0, sizeof(hints)); |
| 1075 | hints.ai_socktype = SOCK_STREAM; |
| 1076 | |
| 1077 | if (proxy) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1078 | if (getaddrinfo (proxy, NULL, &hints, &result) != 0) { |
| 1079 | __xmlIOErr(XML_FROM_FTP, 0, "getaddrinfo failed"); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1080 | return (-1); |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1081 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1082 | } |
| 1083 | else |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1084 | if (getaddrinfo (ctxt->hostname, NULL, &hints, &result) != 0) { |
| 1085 | __xmlIOErr(XML_FROM_FTP, 0, "getaddrinfo failed"); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1086 | return (-1); |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1087 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1088 | |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 1089 | for (tmp = result; tmp; tmp = tmp->ai_next) |
| 1090 | if (tmp->ai_family == AF_INET || tmp->ai_family == AF_INET6) |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1091 | break; |
| 1092 | |
Daniel Veillard | 3dc93a4 | 2003-07-10 14:04:33 +0000 | [diff] [blame] | 1093 | if (!tmp) { |
| 1094 | if (result) |
| 1095 | freeaddrinfo (result); |
| 1096 | return (-1); |
| 1097 | } |
| 1098 | else { |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 1099 | if (tmp->ai_family == AF_INET6) { |
| 1100 | memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1101 | ((struct sockaddr_in6 *) &ctxt->ftpAddr)->sin6_port = htons (port); |
| 1102 | ctxt->controlFd = socket (AF_INET6, SOCK_STREAM, 0); |
| 1103 | } |
| 1104 | else { |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 1105 | memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1106 | ((struct sockaddr_in *) &ctxt->ftpAddr)->sin_port = htons (port); |
| 1107 | ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0); |
| 1108 | } |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 1109 | addrlen = tmp->ai_addrlen; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1110 | freeaddrinfo (result); |
| 1111 | } |
| 1112 | } |
| 1113 | else |
| 1114 | #endif |
| 1115 | { |
| 1116 | if (proxy) |
| 1117 | hp = gethostbyname (proxy); |
| 1118 | else |
| 1119 | hp = gethostbyname (ctxt->hostname); |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1120 | if (hp == NULL) { |
| 1121 | __xmlIOErr(XML_FROM_FTP, 0, "gethostbyname failed"); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1122 | return (-1); |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1123 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1124 | |
| 1125 | /* |
| 1126 | * Prepare the socket |
| 1127 | */ |
| 1128 | ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_family = AF_INET; |
| 1129 | memcpy (&((struct sockaddr_in *)&ctxt->ftpAddr)->sin_addr, |
| 1130 | hp->h_addr_list[0], hp->h_length); |
| 1131 | ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_port = htons (port); |
| 1132 | ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0); |
| 1133 | addrlen = sizeof (struct sockaddr_in); |
| 1134 | } |
| 1135 | |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1136 | if (ctxt->controlFd < 0) { |
| 1137 | __xmlIOErr(XML_FROM_FTP, 0, "socket failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1138 | return(-1); |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1139 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1140 | |
| 1141 | /* |
| 1142 | * Do the connect. |
| 1143 | */ |
| 1144 | if (connect(ctxt->controlFd, (struct sockaddr *) &ctxt->ftpAddr, |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1145 | addrlen) < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1146 | __xmlIOErr(XML_FROM_FTP, 0, "Failed to create a connection"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1147 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
| 1148 | ctxt->controlFd = -1; |
| 1149 | return(-1); |
| 1150 | } |
| 1151 | |
| 1152 | /* |
| 1153 | * Wait for the HELLO from the server. |
| 1154 | */ |
| 1155 | res = xmlNanoFTPGetResponse(ctxt); |
| 1156 | if (res != 2) { |
| 1157 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
| 1158 | ctxt->controlFd = -1; |
| 1159 | return(-1); |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * State diagram for the login operation on the FTP server |
| 1164 | * |
| 1165 | * Reference: RFC 959 |
| 1166 | * |
| 1167 | * 1 |
| 1168 | * +---+ USER +---+------------->+---+ |
| 1169 | * | B |---------->| W | 2 ---->| E | |
| 1170 | * +---+ +---+------ | -->+---+ |
| 1171 | * | | | | | |
| 1172 | * 3 | | 4,5 | | | |
| 1173 | * -------------- ----- | | | |
| 1174 | * | | | | | |
| 1175 | * | | | | | |
| 1176 | * | --------- | |
| 1177 | * | 1| | | | |
| 1178 | * V | | | | |
| 1179 | * +---+ PASS +---+ 2 | ------>+---+ |
| 1180 | * | |---------->| W |------------->| S | |
| 1181 | * +---+ +---+ ---------->+---+ |
| 1182 | * | | | | | |
| 1183 | * 3 | |4,5| | | |
| 1184 | * -------------- -------- | |
| 1185 | * | | | | | |
| 1186 | * | | | | | |
| 1187 | * | ----------- |
| 1188 | * | 1,3| | | | |
| 1189 | * V | 2| | | |
| 1190 | * +---+ ACCT +---+-- | ----->+---+ |
| 1191 | * | |---------->| W | 4,5 -------->| F | |
| 1192 | * +---+ +---+------------->+---+ |
| 1193 | * |
| 1194 | * Of course in case of using a proxy this get really nasty and is not |
| 1195 | * standardized at all :-( |
| 1196 | */ |
| 1197 | if (proxy) { |
| 1198 | int len; |
| 1199 | char buf[400]; |
| 1200 | |
| 1201 | if (proxyUser != NULL) { |
| 1202 | /* |
| 1203 | * We need proxy auth |
| 1204 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1205 | snprintf(buf, sizeof(buf), "USER %s\r\n", proxyUser); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1206 | buf[sizeof(buf) - 1] = 0; |
| 1207 | len = strlen(buf); |
| 1208 | #ifdef DEBUG_FTP |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 1209 | xmlGenericError(xmlGenericErrorContext, "%s", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1210 | #endif |
| 1211 | res = send(ctxt->controlFd, buf, len, 0); |
| 1212 | if (res < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1213 | __xmlIOErr(XML_FROM_FTP, 0, "send failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1214 | closesocket(ctxt->controlFd); |
| 1215 | ctxt->controlFd = -1; |
| 1216 | return(res); |
| 1217 | } |
| 1218 | res = xmlNanoFTPGetResponse(ctxt); |
| 1219 | switch (res) { |
| 1220 | case 2: |
| 1221 | if (proxyPasswd == NULL) |
| 1222 | break; |
| 1223 | case 3: |
| 1224 | if (proxyPasswd != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1225 | snprintf(buf, sizeof(buf), "PASS %s\r\n", proxyPasswd); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1226 | else |
Daniel Veillard | 9ae1eba | 2001-10-19 09:48:35 +0000 | [diff] [blame] | 1227 | snprintf(buf, sizeof(buf), "PASS anonymous@\r\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1228 | buf[sizeof(buf) - 1] = 0; |
| 1229 | len = strlen(buf); |
| 1230 | #ifdef DEBUG_FTP |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 1231 | xmlGenericError(xmlGenericErrorContext, "%s", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1232 | #endif |
| 1233 | res = send(ctxt->controlFd, buf, len, 0); |
| 1234 | if (res < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1235 | __xmlIOErr(XML_FROM_FTP, 0, "send failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1236 | closesocket(ctxt->controlFd); |
| 1237 | ctxt->controlFd = -1; |
| 1238 | return(res); |
| 1239 | } |
| 1240 | res = xmlNanoFTPGetResponse(ctxt); |
| 1241 | if (res > 3) { |
| 1242 | closesocket(ctxt->controlFd); |
| 1243 | ctxt->controlFd = -1; |
| 1244 | return(-1); |
| 1245 | } |
| 1246 | break; |
| 1247 | case 1: |
| 1248 | break; |
| 1249 | case 4: |
| 1250 | case 5: |
| 1251 | case -1: |
| 1252 | default: |
| 1253 | closesocket(ctxt->controlFd); |
| 1254 | ctxt->controlFd = -1; |
| 1255 | return(-1); |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | /* |
| 1260 | * We assume we don't need more authentication to the proxy |
| 1261 | * and that it succeeded :-\ |
| 1262 | */ |
| 1263 | switch (proxyType) { |
| 1264 | case 0: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1265 | /* we will try in sequence */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1266 | case 1: |
| 1267 | /* Using SITE command */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1268 | snprintf(buf, sizeof(buf), "SITE %s\r\n", ctxt->hostname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1269 | buf[sizeof(buf) - 1] = 0; |
| 1270 | len = strlen(buf); |
| 1271 | #ifdef DEBUG_FTP |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 1272 | xmlGenericError(xmlGenericErrorContext, "%s", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1273 | #endif |
| 1274 | res = send(ctxt->controlFd, buf, len, 0); |
| 1275 | if (res < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1276 | __xmlIOErr(XML_FROM_FTP, 0, "send failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1277 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
| 1278 | ctxt->controlFd = -1; |
| 1279 | return(res); |
| 1280 | } |
| 1281 | res = xmlNanoFTPGetResponse(ctxt); |
| 1282 | if (res == 2) { |
| 1283 | /* we assume it worked :-\ 1 is error for SITE command */ |
| 1284 | proxyType = 1; |
| 1285 | break; |
| 1286 | } |
| 1287 | if (proxyType == 1) { |
| 1288 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
| 1289 | ctxt->controlFd = -1; |
| 1290 | return(-1); |
| 1291 | } |
| 1292 | case 2: |
| 1293 | /* USER user@host command */ |
| 1294 | if (ctxt->user == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1295 | snprintf(buf, sizeof(buf), "USER anonymous@%s\r\n", |
| 1296 | ctxt->hostname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1297 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1298 | snprintf(buf, sizeof(buf), "USER %s@%s\r\n", |
| 1299 | ctxt->user, ctxt->hostname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1300 | buf[sizeof(buf) - 1] = 0; |
| 1301 | len = strlen(buf); |
| 1302 | #ifdef DEBUG_FTP |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 1303 | xmlGenericError(xmlGenericErrorContext, "%s", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1304 | #endif |
| 1305 | res = send(ctxt->controlFd, buf, len, 0); |
| 1306 | if (res < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1307 | __xmlIOErr(XML_FROM_FTP, 0, "send failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1308 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
| 1309 | ctxt->controlFd = -1; |
| 1310 | return(res); |
| 1311 | } |
| 1312 | res = xmlNanoFTPGetResponse(ctxt); |
| 1313 | if ((res == 1) || (res == 2)) { |
| 1314 | /* we assume it worked :-\ */ |
| 1315 | proxyType = 2; |
| 1316 | return(0); |
| 1317 | } |
| 1318 | if (ctxt->passwd == NULL) |
Daniel Veillard | 9ae1eba | 2001-10-19 09:48:35 +0000 | [diff] [blame] | 1319 | snprintf(buf, sizeof(buf), "PASS anonymous@\r\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1320 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1321 | snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1322 | buf[sizeof(buf) - 1] = 0; |
| 1323 | len = strlen(buf); |
| 1324 | #ifdef DEBUG_FTP |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 1325 | xmlGenericError(xmlGenericErrorContext, "%s", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1326 | #endif |
| 1327 | res = send(ctxt->controlFd, buf, len, 0); |
| 1328 | if (res < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1329 | __xmlIOErr(XML_FROM_FTP, 0, "send failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1330 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
| 1331 | ctxt->controlFd = -1; |
| 1332 | return(res); |
| 1333 | } |
| 1334 | res = xmlNanoFTPGetResponse(ctxt); |
| 1335 | if ((res == 1) || (res == 2)) { |
| 1336 | /* we assume it worked :-\ */ |
| 1337 | proxyType = 2; |
| 1338 | return(0); |
| 1339 | } |
| 1340 | if (proxyType == 2) { |
| 1341 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
| 1342 | ctxt->controlFd = -1; |
| 1343 | return(-1); |
| 1344 | } |
| 1345 | case 3: |
| 1346 | /* |
| 1347 | * If you need support for other Proxy authentication scheme |
| 1348 | * send the code or at least the sequence in use. |
| 1349 | */ |
| 1350 | default: |
| 1351 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
| 1352 | ctxt->controlFd = -1; |
| 1353 | return(-1); |
| 1354 | } |
| 1355 | } |
| 1356 | /* |
| 1357 | * Non-proxy handling. |
| 1358 | */ |
| 1359 | res = xmlNanoFTPSendUser(ctxt); |
| 1360 | if (res < 0) { |
| 1361 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
| 1362 | ctxt->controlFd = -1; |
| 1363 | return(-1); |
| 1364 | } |
| 1365 | res = xmlNanoFTPGetResponse(ctxt); |
| 1366 | switch (res) { |
| 1367 | case 2: |
| 1368 | return(0); |
| 1369 | case 3: |
| 1370 | break; |
| 1371 | case 1: |
| 1372 | case 4: |
| 1373 | case 5: |
| 1374 | case -1: |
| 1375 | default: |
| 1376 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
| 1377 | ctxt->controlFd = -1; |
| 1378 | return(-1); |
| 1379 | } |
| 1380 | res = xmlNanoFTPSendPasswd(ctxt); |
| 1381 | if (res < 0) { |
| 1382 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
| 1383 | ctxt->controlFd = -1; |
| 1384 | return(-1); |
| 1385 | } |
| 1386 | res = xmlNanoFTPGetResponse(ctxt); |
| 1387 | switch (res) { |
| 1388 | case 2: |
| 1389 | break; |
| 1390 | case 3: |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1391 | __xmlIOErr(XML_FROM_FTP, XML_FTP_ACCNT, |
| 1392 | "FTP server asking for ACCNT on anonymous\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1393 | case 1: |
| 1394 | case 4: |
| 1395 | case 5: |
| 1396 | case -1: |
| 1397 | default: |
| 1398 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
| 1399 | ctxt->controlFd = -1; |
| 1400 | return(-1); |
| 1401 | } |
| 1402 | |
| 1403 | return(0); |
| 1404 | } |
| 1405 | |
| 1406 | /** |
| 1407 | * xmlNanoFTPConnectTo: |
| 1408 | * @server: an FTP server name |
| 1409 | * @port: the port (use 21 if 0) |
| 1410 | * |
| 1411 | * Tries to open a control connection to the given server/port |
| 1412 | * |
| 1413 | * Returns an fTP context or NULL if it failed |
| 1414 | */ |
| 1415 | |
| 1416 | void* |
| 1417 | xmlNanoFTPConnectTo(const char *server, int port) { |
| 1418 | xmlNanoFTPCtxtPtr ctxt; |
| 1419 | int res; |
| 1420 | |
| 1421 | xmlNanoFTPInit(); |
| 1422 | if (server == NULL) |
| 1423 | return(NULL); |
| 1424 | ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(NULL); |
| 1425 | ctxt->hostname = xmlMemStrdup(server); |
| 1426 | if (port != 0) |
| 1427 | ctxt->port = port; |
| 1428 | res = xmlNanoFTPConnect(ctxt); |
| 1429 | if (res < 0) { |
| 1430 | xmlNanoFTPFreeCtxt(ctxt); |
| 1431 | return(NULL); |
| 1432 | } |
| 1433 | return(ctxt); |
| 1434 | } |
| 1435 | |
| 1436 | /** |
| 1437 | * xmlNanoFTPCwd: |
| 1438 | * @ctx: an FTP context |
| 1439 | * @directory: a directory on the server |
| 1440 | * |
| 1441 | * Tries to change the remote directory |
| 1442 | * |
| 1443 | * Returns -1 incase of error, 1 if CWD worked, 0 if it failed |
| 1444 | */ |
| 1445 | |
| 1446 | int |
| 1447 | xmlNanoFTPCwd(void *ctx, char *directory) { |
| 1448 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 1449 | char buf[400]; |
| 1450 | int len; |
| 1451 | int res; |
| 1452 | |
| 1453 | /* |
| 1454 | * Expected response code for CWD: |
| 1455 | * |
| 1456 | * CWD |
| 1457 | * 250 |
| 1458 | * 500, 501, 502, 421, 530, 550 |
| 1459 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1460 | snprintf(buf, sizeof(buf), "CWD %s\r\n", directory); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1461 | buf[sizeof(buf) - 1] = 0; |
| 1462 | len = strlen(buf); |
| 1463 | #ifdef DEBUG_FTP |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 1464 | xmlGenericError(xmlGenericErrorContext, "%s", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1465 | #endif |
| 1466 | res = send(ctxt->controlFd, buf, len, 0); |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1467 | if (res < 0) { |
| 1468 | __xmlIOErr(XML_FROM_FTP, 0, "send failed"); |
| 1469 | return(res); |
| 1470 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1471 | res = xmlNanoFTPGetResponse(ctxt); |
| 1472 | if (res == 4) { |
| 1473 | return(-1); |
| 1474 | } |
| 1475 | if (res == 2) return(1); |
| 1476 | if (res == 5) { |
| 1477 | return(0); |
| 1478 | } |
| 1479 | return(0); |
| 1480 | } |
| 1481 | |
| 1482 | /** |
Daniel Veillard | 6c73cb8 | 2003-03-05 16:45:40 +0000 | [diff] [blame] | 1483 | * xmlNanoFTPDele: |
| 1484 | * @ctx: an FTP context |
| 1485 | * @file: a file or directory on the server |
| 1486 | * |
| 1487 | * Tries to delete an item (file or directory) from server |
| 1488 | * |
| 1489 | * Returns -1 incase of error, 1 if DELE worked, 0 if it failed |
| 1490 | */ |
| 1491 | |
| 1492 | int |
| 1493 | xmlNanoFTPDele(void *ctx, char *file) { |
| 1494 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 1495 | char buf[400]; |
| 1496 | int len; |
| 1497 | int res; |
| 1498 | |
| 1499 | /* |
| 1500 | * Expected response code for DELE: |
| 1501 | * |
| 1502 | * DELE |
| 1503 | * 250 |
| 1504 | * 450, 550 |
| 1505 | * 500, 501, 502, 421, 530 |
| 1506 | */ |
| 1507 | |
| 1508 | snprintf(buf, sizeof(buf), "DELE %s\r\n", file); |
| 1509 | buf[sizeof(buf) - 1] = 0; |
| 1510 | len = strlen(buf); |
| 1511 | #ifdef DEBUG_FTP |
| 1512 | xmlGenericError(xmlGenericErrorContext, "%s", buf); |
| 1513 | #endif |
| 1514 | res = send(ctxt->controlFd, buf, len, 0); |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1515 | if (res < 0) { |
| 1516 | __xmlIOErr(XML_FROM_FTP, 0, "send failed"); |
| 1517 | return(res); |
| 1518 | } |
Daniel Veillard | 6c73cb8 | 2003-03-05 16:45:40 +0000 | [diff] [blame] | 1519 | res = xmlNanoFTPGetResponse(ctxt); |
| 1520 | if (res == 4) { |
| 1521 | return(-1); |
| 1522 | } |
| 1523 | if (res == 2) return(1); |
| 1524 | if (res == 5) { |
| 1525 | return(0); |
| 1526 | } |
| 1527 | return(0); |
| 1528 | } |
| 1529 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1530 | * xmlNanoFTPGetConnection: |
| 1531 | * @ctx: an FTP context |
| 1532 | * |
| 1533 | * Try to open a data connection to the server. Currently only |
| 1534 | * passive mode is supported. |
| 1535 | * |
| 1536 | * Returns -1 incase of error, 0 otherwise |
| 1537 | */ |
| 1538 | |
| 1539 | int |
| 1540 | xmlNanoFTPGetConnection(void *ctx) { |
| 1541 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 1542 | char buf[200], *cur; |
| 1543 | int len, i; |
| 1544 | int res; |
| 1545 | unsigned char ad[6], *adp, *portp; |
| 1546 | unsigned int temp[6]; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1547 | #ifdef SUPPORT_IP6 |
| 1548 | struct sockaddr_storage dataAddr; |
| 1549 | #else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1550 | struct sockaddr_in dataAddr; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1551 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1552 | SOCKLEN_T dataAddrLen; |
| 1553 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1554 | memset (&dataAddr, 0, sizeof(dataAddr)); |
| 1555 | #ifdef SUPPORT_IP6 |
| 1556 | if ((ctxt->ftpAddr).ss_family == AF_INET6) { |
| 1557 | ctxt->dataFd = socket (AF_INET6, SOCK_STREAM, IPPROTO_TCP); |
| 1558 | ((struct sockaddr_in6 *)&dataAddr)->sin6_family = AF_INET6; |
| 1559 | dataAddrLen = sizeof(struct sockaddr_in6); |
| 1560 | } else |
| 1561 | #endif |
| 1562 | { |
| 1563 | ctxt->dataFd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 1564 | ((struct sockaddr_in *)&dataAddr)->sin_family = AF_INET; |
| 1565 | dataAddrLen = sizeof (struct sockaddr_in); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1566 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1567 | |
| 1568 | if (ctxt->dataFd < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1569 | __xmlIOErr(XML_FROM_FTP, 0, "socket failed"); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1570 | return (-1); |
| 1571 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1572 | |
| 1573 | if (ctxt->passive) { |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1574 | #ifdef SUPPORT_IP6 |
| 1575 | if ((ctxt->ftpAddr).ss_family == AF_INET6) |
| 1576 | snprintf (buf, sizeof(buf), "EPSV\r\n"); |
| 1577 | else |
| 1578 | #endif |
| 1579 | snprintf (buf, sizeof(buf), "PASV\r\n"); |
| 1580 | len = strlen (buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1581 | #ifdef DEBUG_FTP |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 1582 | xmlGenericError(xmlGenericErrorContext, "%s", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1583 | #endif |
| 1584 | res = send(ctxt->controlFd, buf, len, 0); |
| 1585 | if (res < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1586 | __xmlIOErr(XML_FROM_FTP, 0, "send failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1587 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 1588 | return(res); |
| 1589 | } |
| 1590 | res = xmlNanoFTPReadResponse(ctx); |
| 1591 | if (res != 2) { |
| 1592 | if (res == 5) { |
| 1593 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 1594 | return(-1); |
| 1595 | } else { |
| 1596 | /* |
| 1597 | * retry with an active connection |
| 1598 | */ |
| 1599 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 1600 | ctxt->passive = 0; |
| 1601 | } |
| 1602 | } |
| 1603 | cur = &ctxt->controlBuf[ctxt->controlBufAnswer]; |
| 1604 | while (((*cur < '0') || (*cur > '9')) && *cur != '\0') cur++; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1605 | #ifdef SUPPORT_IP6 |
| 1606 | if ((ctxt->ftpAddr).ss_family == AF_INET6) { |
| 1607 | if (sscanf (cur, "%u", &temp[0]) != 1) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1608 | __xmlIOErr(XML_FROM_FTP, XML_FTP_EPSV_ANSWER, |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1609 | "Invalid answer to EPSV\n"); |
| 1610 | if (ctxt->dataFd != -1) { |
| 1611 | closesocket (ctxt->dataFd); ctxt->dataFd = -1; |
| 1612 | } |
| 1613 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1614 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1615 | memcpy (&((struct sockaddr_in6 *)&dataAddr)->sin6_addr, &((struct sockaddr_in6 *)&ctxt->ftpAddr)->sin6_addr, sizeof(struct in6_addr)); |
| 1616 | ((struct sockaddr_in6 *)&dataAddr)->sin6_port = htons (temp[0]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1617 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1618 | else |
| 1619 | #endif |
| 1620 | { |
| 1621 | if (sscanf (cur, "%u,%u,%u,%u,%u,%u", &temp[0], &temp[1], &temp[2], |
| 1622 | &temp[3], &temp[4], &temp[5]) != 6) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1623 | __xmlIOErr(XML_FROM_FTP, XML_FTP_PASV_ANSWER, |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1624 | "Invalid answer to PASV\n"); |
| 1625 | if (ctxt->dataFd != -1) { |
| 1626 | closesocket (ctxt->dataFd); ctxt->dataFd = -1; |
| 1627 | } |
| 1628 | return (-1); |
| 1629 | } |
| 1630 | for (i=0; i<6; i++) ad[i] = (unsigned char) (temp[i] & 0xff); |
| 1631 | memcpy (&((struct sockaddr_in *)&dataAddr)->sin_addr, &ad[0], 4); |
| 1632 | memcpy (&((struct sockaddr_in *)&dataAddr)->sin_port, &ad[4], 2); |
| 1633 | } |
| 1634 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1635 | if (connect(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1636 | __xmlIOErr(XML_FROM_FTP, 0, "Failed to create a data connection"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1637 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 1638 | return (-1); |
| 1639 | } |
| 1640 | } else { |
| 1641 | getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1642 | #ifdef SUPPORT_IP6 |
| 1643 | if ((ctxt->ftpAddr).ss_family == AF_INET6) |
| 1644 | ((struct sockaddr_in6 *)&dataAddr)->sin6_port = 0; |
| 1645 | else |
| 1646 | #endif |
| 1647 | ((struct sockaddr_in *)&dataAddr)->sin_port = 0; |
| 1648 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1649 | if (bind(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1650 | __xmlIOErr(XML_FROM_FTP, 0, "bind failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1651 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 1652 | return (-1); |
| 1653 | } |
| 1654 | getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen); |
| 1655 | |
| 1656 | if (listen(ctxt->dataFd, 1) < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1657 | __xmlIOErr(XML_FROM_FTP, 0, "listen failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1658 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 1659 | return (-1); |
| 1660 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1661 | #ifdef SUPPORT_IP6 |
| 1662 | if ((ctxt->ftpAddr).ss_family == AF_INET6) { |
| 1663 | char buf6[INET6_ADDRSTRLEN]; |
| 1664 | inet_ntop (AF_INET6, &((struct sockaddr_in6 *)&dataAddr)->sin6_addr, |
| 1665 | buf6, INET6_ADDRSTRLEN); |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 1666 | adp = (unsigned char *) buf6; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1667 | portp = (unsigned char *) &((struct sockaddr_in6 *)&dataAddr)->sin6_port; |
| 1668 | snprintf (buf, sizeof(buf), "EPRT |2|%s|%s|\r\n", adp, portp); |
| 1669 | } else |
| 1670 | #endif |
| 1671 | { |
| 1672 | adp = (unsigned char *) &((struct sockaddr_in *)&dataAddr)->sin_addr; |
| 1673 | portp = (unsigned char *) &((struct sockaddr_in *)&dataAddr)->sin_port; |
| 1674 | snprintf (buf, sizeof(buf), "PORT %d,%d,%d,%d,%d,%d\r\n", |
| 1675 | adp[0] & 0xff, adp[1] & 0xff, adp[2] & 0xff, adp[3] & 0xff, |
| 1676 | portp[0] & 0xff, portp[1] & 0xff); |
| 1677 | } |
| 1678 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1679 | buf[sizeof(buf) - 1] = 0; |
| 1680 | len = strlen(buf); |
| 1681 | #ifdef DEBUG_FTP |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 1682 | xmlGenericError(xmlGenericErrorContext, "%s", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1683 | #endif |
| 1684 | |
| 1685 | res = send(ctxt->controlFd, buf, len, 0); |
| 1686 | if (res < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1687 | __xmlIOErr(XML_FROM_FTP, 0, "send failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1688 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 1689 | return(res); |
| 1690 | } |
| 1691 | res = xmlNanoFTPGetResponse(ctxt); |
| 1692 | if (res != 2) { |
| 1693 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 1694 | return(-1); |
| 1695 | } |
| 1696 | } |
| 1697 | return(ctxt->dataFd); |
| 1698 | |
| 1699 | } |
| 1700 | |
| 1701 | /** |
| 1702 | * xmlNanoFTPCloseConnection: |
| 1703 | * @ctx: an FTP context |
| 1704 | * |
| 1705 | * Close the data connection from the server |
| 1706 | * |
| 1707 | * Returns -1 incase of error, 0 otherwise |
| 1708 | */ |
| 1709 | |
| 1710 | int |
| 1711 | xmlNanoFTPCloseConnection(void *ctx) { |
| 1712 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 1713 | int res; |
| 1714 | fd_set rfd, efd; |
| 1715 | struct timeval tv; |
| 1716 | |
| 1717 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 1718 | tv.tv_sec = 15; |
| 1719 | tv.tv_usec = 0; |
| 1720 | FD_ZERO(&rfd); |
| 1721 | FD_SET(ctxt->controlFd, &rfd); |
| 1722 | FD_ZERO(&efd); |
| 1723 | FD_SET(ctxt->controlFd, &efd); |
| 1724 | res = select(ctxt->controlFd + 1, &rfd, NULL, &efd, &tv); |
| 1725 | if (res < 0) { |
| 1726 | #ifdef DEBUG_FTP |
| 1727 | perror("select"); |
| 1728 | #endif |
| 1729 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
| 1730 | return(-1); |
| 1731 | } |
| 1732 | if (res == 0) { |
| 1733 | #ifdef DEBUG_FTP |
| 1734 | xmlGenericError(xmlGenericErrorContext, |
| 1735 | "xmlNanoFTPCloseConnection: timeout\n"); |
| 1736 | #endif |
| 1737 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
| 1738 | } else { |
| 1739 | res = xmlNanoFTPGetResponse(ctxt); |
| 1740 | if (res != 2) { |
| 1741 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
| 1742 | return(-1); |
| 1743 | } |
| 1744 | } |
| 1745 | return(0); |
| 1746 | } |
| 1747 | |
| 1748 | /** |
| 1749 | * xmlNanoFTPParseList: |
| 1750 | * @list: some data listing received from the server |
| 1751 | * @callback: the user callback |
| 1752 | * @userData: the user callback data |
| 1753 | * |
| 1754 | * Parse at most one entry from the listing. |
| 1755 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 1756 | * Returns -1 incase of error, the length of data parsed otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1757 | */ |
| 1758 | |
| 1759 | static int |
| 1760 | xmlNanoFTPParseList(const char *list, ftpListCallback callback, void *userData) { |
| 1761 | const char *cur = list; |
| 1762 | char filename[151]; |
| 1763 | char attrib[11]; |
| 1764 | char owner[11]; |
| 1765 | char group[11]; |
| 1766 | char month[4]; |
| 1767 | int year = 0; |
| 1768 | int minute = 0; |
| 1769 | int hour = 0; |
| 1770 | int day = 0; |
| 1771 | unsigned long size = 0; |
| 1772 | int links = 0; |
| 1773 | int i; |
| 1774 | |
| 1775 | if (!strncmp(cur, "total", 5)) { |
| 1776 | cur += 5; |
| 1777 | while (*cur == ' ') cur++; |
| 1778 | while ((*cur >= '0') && (*cur <= '9')) |
| 1779 | links = (links * 10) + (*cur++ - '0'); |
| 1780 | while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r')) |
| 1781 | cur++; |
| 1782 | return(cur - list); |
| 1783 | } else if (*list == '+') { |
| 1784 | return(0); |
| 1785 | } else { |
| 1786 | while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r')) |
| 1787 | cur++; |
| 1788 | if (*cur == 0) return(0); |
| 1789 | i = 0; |
| 1790 | while (*cur != ' ') { |
| 1791 | if (i < 10) |
| 1792 | attrib[i++] = *cur; |
| 1793 | cur++; |
| 1794 | if (*cur == 0) return(0); |
| 1795 | } |
| 1796 | attrib[10] = 0; |
| 1797 | while (*cur == ' ') cur++; |
| 1798 | if (*cur == 0) return(0); |
| 1799 | while ((*cur >= '0') && (*cur <= '9')) |
| 1800 | links = (links * 10) + (*cur++ - '0'); |
| 1801 | while (*cur == ' ') cur++; |
| 1802 | if (*cur == 0) return(0); |
| 1803 | i = 0; |
| 1804 | while (*cur != ' ') { |
| 1805 | if (i < 10) |
| 1806 | owner[i++] = *cur; |
| 1807 | cur++; |
| 1808 | if (*cur == 0) return(0); |
| 1809 | } |
| 1810 | owner[i] = 0; |
| 1811 | while (*cur == ' ') cur++; |
| 1812 | if (*cur == 0) return(0); |
| 1813 | i = 0; |
| 1814 | while (*cur != ' ') { |
| 1815 | if (i < 10) |
| 1816 | group[i++] = *cur; |
| 1817 | cur++; |
| 1818 | if (*cur == 0) return(0); |
| 1819 | } |
| 1820 | group[i] = 0; |
| 1821 | while (*cur == ' ') cur++; |
| 1822 | if (*cur == 0) return(0); |
| 1823 | while ((*cur >= '0') && (*cur <= '9')) |
| 1824 | size = (size * 10) + (*cur++ - '0'); |
| 1825 | while (*cur == ' ') cur++; |
| 1826 | if (*cur == 0) return(0); |
| 1827 | i = 0; |
| 1828 | while (*cur != ' ') { |
| 1829 | if (i < 3) |
| 1830 | month[i++] = *cur; |
| 1831 | cur++; |
| 1832 | if (*cur == 0) return(0); |
| 1833 | } |
| 1834 | month[i] = 0; |
| 1835 | while (*cur == ' ') cur++; |
| 1836 | if (*cur == 0) return(0); |
| 1837 | while ((*cur >= '0') && (*cur <= '9')) |
| 1838 | day = (day * 10) + (*cur++ - '0'); |
| 1839 | while (*cur == ' ') cur++; |
| 1840 | if (*cur == 0) return(0); |
| 1841 | if ((cur[1] == 0) || (cur[2] == 0)) return(0); |
| 1842 | if ((cur[1] == ':') || (cur[2] == ':')) { |
| 1843 | while ((*cur >= '0') && (*cur <= '9')) |
| 1844 | hour = (hour * 10) + (*cur++ - '0'); |
| 1845 | if (*cur == ':') cur++; |
| 1846 | while ((*cur >= '0') && (*cur <= '9')) |
| 1847 | minute = (minute * 10) + (*cur++ - '0'); |
| 1848 | } else { |
| 1849 | while ((*cur >= '0') && (*cur <= '9')) |
| 1850 | year = (year * 10) + (*cur++ - '0'); |
| 1851 | } |
| 1852 | while (*cur == ' ') cur++; |
| 1853 | if (*cur == 0) return(0); |
| 1854 | i = 0; |
| 1855 | while ((*cur != '\n') && (*cur != '\r')) { |
| 1856 | if (i < 150) |
| 1857 | filename[i++] = *cur; |
| 1858 | cur++; |
| 1859 | if (*cur == 0) return(0); |
| 1860 | } |
| 1861 | filename[i] = 0; |
| 1862 | if ((*cur != '\n') && (*cur != '\r')) |
| 1863 | return(0); |
| 1864 | while ((*cur == '\n') || (*cur == '\r')) |
| 1865 | cur++; |
| 1866 | } |
| 1867 | if (callback != NULL) { |
| 1868 | callback(userData, filename, attrib, owner, group, size, links, |
| 1869 | year, month, day, hour, minute); |
| 1870 | } |
| 1871 | return(cur - list); |
| 1872 | } |
| 1873 | |
| 1874 | /** |
| 1875 | * xmlNanoFTPList: |
| 1876 | * @ctx: an FTP context |
| 1877 | * @callback: the user callback |
| 1878 | * @userData: the user callback data |
| 1879 | * @filename: optional files to list |
| 1880 | * |
| 1881 | * Do a listing on the server. All files info are passed back |
| 1882 | * in the callbacks. |
| 1883 | * |
| 1884 | * Returns -1 incase of error, 0 otherwise |
| 1885 | */ |
| 1886 | |
| 1887 | int |
| 1888 | xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData, |
| 1889 | char *filename) { |
| 1890 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 1891 | char buf[4096 + 1]; |
| 1892 | int len, res; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1893 | int indx = 0, base; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1894 | fd_set rfd, efd; |
| 1895 | struct timeval tv; |
| 1896 | |
| 1897 | if (filename == NULL) { |
| 1898 | if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1) |
| 1899 | return(-1); |
| 1900 | ctxt->dataFd = xmlNanoFTPGetConnection(ctxt); |
| 1901 | if (ctxt->dataFd == -1) |
| 1902 | return(-1); |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1903 | snprintf(buf, sizeof(buf), "LIST -L\r\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1904 | } else { |
| 1905 | if (filename[0] != '/') { |
| 1906 | if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1) |
| 1907 | return(-1); |
| 1908 | } |
| 1909 | ctxt->dataFd = xmlNanoFTPGetConnection(ctxt); |
| 1910 | if (ctxt->dataFd == -1) |
| 1911 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1912 | snprintf(buf, sizeof(buf), "LIST -L %s\r\n", filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1913 | } |
| 1914 | buf[sizeof(buf) - 1] = 0; |
| 1915 | len = strlen(buf); |
| 1916 | #ifdef DEBUG_FTP |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 1917 | xmlGenericError(xmlGenericErrorContext, "%s", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1918 | #endif |
| 1919 | res = send(ctxt->controlFd, buf, len, 0); |
| 1920 | if (res < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1921 | __xmlIOErr(XML_FROM_FTP, 0, "send failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1922 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 1923 | return(res); |
| 1924 | } |
| 1925 | res = xmlNanoFTPReadResponse(ctxt); |
| 1926 | if (res != 1) { |
| 1927 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 1928 | return(-res); |
| 1929 | } |
| 1930 | |
| 1931 | do { |
| 1932 | tv.tv_sec = 1; |
| 1933 | tv.tv_usec = 0; |
| 1934 | FD_ZERO(&rfd); |
| 1935 | FD_SET(ctxt->dataFd, &rfd); |
| 1936 | FD_ZERO(&efd); |
| 1937 | FD_SET(ctxt->dataFd, &efd); |
| 1938 | res = select(ctxt->dataFd + 1, &rfd, NULL, &efd, &tv); |
| 1939 | if (res < 0) { |
| 1940 | #ifdef DEBUG_FTP |
| 1941 | perror("select"); |
| 1942 | #endif |
| 1943 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 1944 | return(-1); |
| 1945 | } |
| 1946 | if (res == 0) { |
| 1947 | res = xmlNanoFTPCheckResponse(ctxt); |
| 1948 | if (res < 0) { |
| 1949 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 1950 | ctxt->dataFd = -1; |
| 1951 | return(-1); |
| 1952 | } |
| 1953 | if (res == 2) { |
| 1954 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 1955 | return(0); |
| 1956 | } |
| 1957 | |
| 1958 | continue; |
| 1959 | } |
| 1960 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1961 | if ((len = recv(ctxt->dataFd, &buf[indx], sizeof(buf) - (indx + 1), 0)) < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1962 | __xmlIOErr(XML_FROM_FTP, 0, "recv"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1963 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 1964 | ctxt->dataFd = -1; |
| 1965 | return(-1); |
| 1966 | } |
| 1967 | #ifdef DEBUG_FTP |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1968 | write(1, &buf[indx], len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1969 | #endif |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1970 | indx += len; |
| 1971 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1972 | base = 0; |
| 1973 | do { |
| 1974 | res = xmlNanoFTPParseList(&buf[base], callback, userData); |
| 1975 | base += res; |
| 1976 | } while (res > 0); |
| 1977 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1978 | memmove(&buf[0], &buf[base], indx - base); |
| 1979 | indx -= base; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1980 | } while (len != 0); |
| 1981 | xmlNanoFTPCloseConnection(ctxt); |
| 1982 | return(0); |
| 1983 | } |
| 1984 | |
| 1985 | /** |
| 1986 | * xmlNanoFTPGetSocket: |
| 1987 | * @ctx: an FTP context |
| 1988 | * @filename: the file to retrieve (or NULL if path is in context). |
| 1989 | * |
| 1990 | * Initiate fetch of the given file from the server. |
| 1991 | * |
| 1992 | * Returns the socket for the data connection, or <0 in case of error |
| 1993 | */ |
| 1994 | |
| 1995 | |
| 1996 | int |
| 1997 | xmlNanoFTPGetSocket(void *ctx, const char *filename) { |
| 1998 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 1999 | char buf[300]; |
| 2000 | int res, len; |
| 2001 | if ((filename == NULL) && (ctxt->path == NULL)) |
| 2002 | return(-1); |
| 2003 | ctxt->dataFd = xmlNanoFTPGetConnection(ctxt); |
| 2004 | if (ctxt->dataFd == -1) |
| 2005 | return(-1); |
| 2006 | |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 2007 | snprintf(buf, sizeof(buf), "TYPE I\r\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2008 | len = strlen(buf); |
| 2009 | #ifdef DEBUG_FTP |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 2010 | xmlGenericError(xmlGenericErrorContext, "%s", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2011 | #endif |
| 2012 | res = send(ctxt->controlFd, buf, len, 0); |
| 2013 | if (res < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 2014 | __xmlIOErr(XML_FROM_FTP, 0, "send failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2015 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 2016 | return(res); |
| 2017 | } |
| 2018 | res = xmlNanoFTPReadResponse(ctxt); |
| 2019 | if (res != 2) { |
| 2020 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 2021 | return(-res); |
| 2022 | } |
| 2023 | if (filename == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2024 | snprintf(buf, sizeof(buf), "RETR %s\r\n", ctxt->path); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2025 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2026 | snprintf(buf, sizeof(buf), "RETR %s\r\n", filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2027 | buf[sizeof(buf) - 1] = 0; |
| 2028 | len = strlen(buf); |
| 2029 | #ifdef DEBUG_FTP |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 2030 | xmlGenericError(xmlGenericErrorContext, "%s", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2031 | #endif |
| 2032 | res = send(ctxt->controlFd, buf, len, 0); |
| 2033 | if (res < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 2034 | __xmlIOErr(XML_FROM_FTP, 0, "send failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2035 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 2036 | return(res); |
| 2037 | } |
| 2038 | res = xmlNanoFTPReadResponse(ctxt); |
| 2039 | if (res != 1) { |
| 2040 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 2041 | return(-res); |
| 2042 | } |
| 2043 | return(ctxt->dataFd); |
| 2044 | } |
| 2045 | |
| 2046 | /** |
| 2047 | * xmlNanoFTPGet: |
| 2048 | * @ctx: an FTP context |
| 2049 | * @callback: the user callback |
| 2050 | * @userData: the user callback data |
| 2051 | * @filename: the file to retrieve |
| 2052 | * |
| 2053 | * Fetch the given file from the server. All data are passed back |
| 2054 | * in the callbacks. The last callback has a size of 0 block. |
| 2055 | * |
| 2056 | * Returns -1 incase of error, 0 otherwise |
| 2057 | */ |
| 2058 | |
| 2059 | int |
| 2060 | xmlNanoFTPGet(void *ctx, ftpDataCallback callback, void *userData, |
| 2061 | const char *filename) { |
| 2062 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 2063 | char buf[4096]; |
| 2064 | int len = 0, res; |
| 2065 | fd_set rfd; |
| 2066 | struct timeval tv; |
| 2067 | |
| 2068 | if ((filename == NULL) && (ctxt->path == NULL)) |
| 2069 | return(-1); |
| 2070 | if (callback == NULL) |
| 2071 | return(-1); |
| 2072 | if (xmlNanoFTPGetSocket(ctxt, filename) < 0) |
| 2073 | return(-1); |
| 2074 | |
| 2075 | do { |
| 2076 | tv.tv_sec = 1; |
| 2077 | tv.tv_usec = 0; |
| 2078 | FD_ZERO(&rfd); |
| 2079 | FD_SET(ctxt->dataFd, &rfd); |
| 2080 | res = select(ctxt->dataFd + 1, &rfd, NULL, NULL, &tv); |
| 2081 | if (res < 0) { |
| 2082 | #ifdef DEBUG_FTP |
| 2083 | perror("select"); |
| 2084 | #endif |
| 2085 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 2086 | return(-1); |
| 2087 | } |
| 2088 | if (res == 0) { |
| 2089 | res = xmlNanoFTPCheckResponse(ctxt); |
| 2090 | if (res < 0) { |
| 2091 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 2092 | ctxt->dataFd = -1; |
| 2093 | return(-1); |
| 2094 | } |
| 2095 | if (res == 2) { |
| 2096 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 2097 | return(0); |
| 2098 | } |
| 2099 | |
| 2100 | continue; |
| 2101 | } |
| 2102 | if ((len = recv(ctxt->dataFd, buf, sizeof(buf), 0)) < 0) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 2103 | __xmlIOErr(XML_FROM_FTP, 0, "recv failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2104 | callback(userData, buf, len); |
| 2105 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
| 2106 | return(-1); |
| 2107 | } |
| 2108 | callback(userData, buf, len); |
| 2109 | } while (len != 0); |
| 2110 | |
| 2111 | return(xmlNanoFTPCloseConnection(ctxt)); |
| 2112 | } |
| 2113 | |
| 2114 | /** |
| 2115 | * xmlNanoFTPRead: |
| 2116 | * @ctx: the FTP context |
| 2117 | * @dest: a buffer |
| 2118 | * @len: the buffer length |
| 2119 | * |
| 2120 | * This function tries to read @len bytes from the existing FTP connection |
| 2121 | * and saves them in @dest. This is a blocking call. |
| 2122 | * |
| 2123 | * Returns the number of byte read. 0 is an indication of an end of connection. |
| 2124 | * -1 indicates a parameter error. |
| 2125 | */ |
| 2126 | int |
| 2127 | xmlNanoFTPRead(void *ctx, void *dest, int len) { |
| 2128 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 2129 | |
| 2130 | if (ctx == NULL) return(-1); |
| 2131 | if (ctxt->dataFd < 0) return(0); |
| 2132 | if (dest == NULL) return(-1); |
| 2133 | if (len <= 0) return(0); |
| 2134 | |
| 2135 | len = recv(ctxt->dataFd, dest, len, 0); |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 2136 | if (len <= 0) { |
| 2137 | __xmlIOErr(XML_FROM_FTP, 0, "recv failed"); |
| 2138 | xmlNanoFTPCloseConnection(ctxt); |
| 2139 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2140 | #ifdef DEBUG_FTP |
| 2141 | xmlGenericError(xmlGenericErrorContext, "Recvd %d bytes\n", len); |
| 2142 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2143 | return(len); |
| 2144 | } |
| 2145 | |
| 2146 | /** |
| 2147 | * xmlNanoFTPOpen: |
| 2148 | * @URL: the URL to the resource |
| 2149 | * |
| 2150 | * Start to fetch the given ftp:// resource |
| 2151 | * |
| 2152 | * Returns an FTP context, or NULL |
| 2153 | */ |
| 2154 | |
| 2155 | void* |
| 2156 | xmlNanoFTPOpen(const char *URL) { |
| 2157 | xmlNanoFTPCtxtPtr ctxt; |
| 2158 | int sock; |
| 2159 | |
| 2160 | xmlNanoFTPInit(); |
| 2161 | if (URL == NULL) return(NULL); |
| 2162 | if (strncmp("ftp://", URL, 6)) return(NULL); |
| 2163 | |
| 2164 | ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(URL); |
| 2165 | if (ctxt == NULL) return(NULL); |
| 2166 | if (xmlNanoFTPConnect(ctxt) < 0) { |
| 2167 | xmlNanoFTPFreeCtxt(ctxt); |
| 2168 | return(NULL); |
| 2169 | } |
| 2170 | sock = xmlNanoFTPGetSocket(ctxt, ctxt->path); |
| 2171 | if (sock < 0) { |
| 2172 | xmlNanoFTPFreeCtxt(ctxt); |
| 2173 | return(NULL); |
| 2174 | } |
| 2175 | return(ctxt); |
| 2176 | } |
| 2177 | |
| 2178 | /** |
| 2179 | * xmlNanoFTPClose: |
| 2180 | * @ctx: an FTP context |
| 2181 | * |
| 2182 | * Close the connection and both control and transport |
| 2183 | * |
| 2184 | * Returns -1 incase of error, 0 otherwise |
| 2185 | */ |
| 2186 | |
| 2187 | int |
| 2188 | xmlNanoFTPClose(void *ctx) { |
| 2189 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 2190 | |
| 2191 | if (ctxt == NULL) |
| 2192 | return(-1); |
| 2193 | |
| 2194 | if (ctxt->dataFd >= 0) { |
| 2195 | closesocket(ctxt->dataFd); |
| 2196 | ctxt->dataFd = -1; |
| 2197 | } |
| 2198 | if (ctxt->controlFd >= 0) { |
| 2199 | xmlNanoFTPQuit(ctxt); |
| 2200 | closesocket(ctxt->controlFd); |
| 2201 | ctxt->controlFd = -1; |
| 2202 | } |
| 2203 | xmlNanoFTPFreeCtxt(ctxt); |
| 2204 | return(0); |
| 2205 | } |
| 2206 | |
| 2207 | #ifdef STANDALONE |
| 2208 | /************************************************************************ |
| 2209 | * * |
| 2210 | * Basic test in Standalone mode * |
| 2211 | * * |
| 2212 | ************************************************************************/ |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2213 | static |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2214 | void ftpList(void *userData, const char *filename, const char* attrib, |
| 2215 | const char *owner, const char *group, unsigned long size, int links, |
| 2216 | int year, const char *month, int day, int hour, int minute) { |
| 2217 | xmlGenericError(xmlGenericErrorContext, |
| 2218 | "%s %s %s %ld %s\n", attrib, owner, group, size, filename); |
| 2219 | } |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2220 | static |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2221 | void ftpData(void *userData, const char *data, int len) { |
| 2222 | if (userData == NULL) return; |
| 2223 | if (len <= 0) { |
| 2224 | fclose(userData); |
| 2225 | return; |
| 2226 | } |
| 2227 | fwrite(data, len, 1, userData); |
| 2228 | } |
| 2229 | |
| 2230 | int main(int argc, char **argv) { |
| 2231 | void *ctxt; |
| 2232 | FILE *output; |
| 2233 | char *tstfile = NULL; |
| 2234 | |
| 2235 | xmlNanoFTPInit(); |
| 2236 | if (argc > 1) { |
| 2237 | ctxt = xmlNanoFTPNewCtxt(argv[1]); |
| 2238 | if (xmlNanoFTPConnect(ctxt) < 0) { |
| 2239 | xmlGenericError(xmlGenericErrorContext, |
| 2240 | "Couldn't connect to %s\n", argv[1]); |
| 2241 | exit(1); |
| 2242 | } |
| 2243 | if (argc > 2) |
| 2244 | tstfile = argv[2]; |
| 2245 | } else |
| 2246 | ctxt = xmlNanoFTPConnectTo("localhost", 0); |
| 2247 | if (ctxt == NULL) { |
| 2248 | xmlGenericError(xmlGenericErrorContext, |
| 2249 | "Couldn't connect to localhost\n"); |
| 2250 | exit(1); |
| 2251 | } |
| 2252 | xmlNanoFTPList(ctxt, ftpList, NULL, tstfile); |
| 2253 | output = fopen("/tmp/tstdata", "w"); |
| 2254 | if (output != NULL) { |
| 2255 | if (xmlNanoFTPGet(ctxt, ftpData, (void *) output, tstfile) < 0) |
| 2256 | xmlGenericError(xmlGenericErrorContext, |
| 2257 | "Failed to get file\n"); |
| 2258 | |
| 2259 | } |
| 2260 | xmlNanoFTPClose(ctxt); |
| 2261 | xmlMemoryDump(); |
| 2262 | exit(0); |
| 2263 | } |
| 2264 | #endif /* STANDALONE */ |
| 2265 | #else /* !LIBXML_FTP_ENABLED */ |
| 2266 | #ifdef STANDALONE |
| 2267 | #include <stdio.h> |
| 2268 | int main(int argc, char **argv) { |
| 2269 | xmlGenericError(xmlGenericErrorContext, |
| 2270 | "%s : FTP support not compiled in\n", argv[0]); |
| 2271 | return(0); |
| 2272 | } |
| 2273 | #endif /* STANDALONE */ |
| 2274 | #endif /* LIBXML_FTP_ENABLED */ |