blob: c181728ec382801c363c92635aadaf2912030e1c [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
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 Veillardcbaf3992001-12-31 16:16:02 +000015#else /* TESTING */
Daniel Veillardf3afa7d2001-06-09 13:52:58 +000016#define NEED_SOCKETS
Daniel Veillardcbaf3992001-12-31 16:16:02 +000017#endif /* TESTING */
Owen Taylor3473f882001-02-23 17:55:21 +000018
Daniel Veillard34ce8be2002-03-18 19:37:11 +000019#define IN_LIBXML
Daniel Veillard5e2dace2001-07-18 19:30:27 +000020#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000021
22#ifdef LIBXML_FTP_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000023#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 Veillard75eb1ad2003-07-07 14:42:44 +000055#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 Taylor3473f882001-02-23 17:55:21 +000061#ifdef HAVE_STRINGS_H
62#include <strings.h>
63#endif
64
65#include <libxml/xmlmemory.h>
Daniel Veillardd0463562001-10-13 09:15:48 +000066#include <libxml/parser.h>
Owen Taylor3473f882001-02-23 17:55:21 +000067#include <libxml/xmlerror.h>
Daniel Veillardcacbe5d2003-01-10 16:09:51 +000068#include <libxml/uri.h>
Daniel Veillardd0463562001-10-13 09:15:48 +000069#include <libxml/nanoftp.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000070#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000071
72/* #define DEBUG_FTP 1 */
73#ifdef STANDALONE
74#ifndef DEBUG_FTP
75#define DEBUG_FTP 1
76#endif
77#endif
78
Daniel Veillard1638a472003-08-14 01:23:25 +000079
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 Taylor3473f882001-02-23 17:55:21 +000089/**
90 * A couple portability macros
91 */
92#ifndef _WINSOCKAPI_
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000093#ifndef __BEOS__
Owen Taylor3473f882001-02-23 17:55:21 +000094#define closesocket(s) close(s)
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000095#endif
Owen Taylor3473f882001-02-23 17:55:21 +000096#define SOCKET int
97#endif
Daniel Veillardacf7ff02001-10-29 20:21:47 +000098#if defined(VMS) || defined(__VMS)
99#define SOCKLEN_T unsigned int
100#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000101
Daniel Veillard89f7f272003-09-29 13:29:09 +0000102#ifdef __BEOS__
103#ifndef PF_INET
104#define PF_INET AF_INET
105#endif
106#endif
107
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000108
Owen Taylor3473f882001-02-23 17:55:21 +0000109#define FTP_COMMAND_OK 200
110#define FTP_SYNTAX_ERROR 500
111#define FTP_GET_PASSWD 331
112#define FTP_BUF_SIZE 512
113
114typedef 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 Veillardde2a67b2003-06-21 14:20:04 +0000121#ifdef SUPPORT_IP6
122 struct sockaddr_storage ftpAddr; /* this is large enough to hold IPv6 address*/
123#else
Owen Taylor3473f882001-02-23 17:55:21 +0000124 struct sockaddr_in ftpAddr; /* the socket address struct */
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000125#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000126 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
138static int initialized = 0;
139static char *proxy = NULL; /* the proxy name if any */
140static int proxyPort = 0; /* the proxy port if any */
141static char *proxyUser = NULL; /* user for proxy authentication */
142static char *proxyPasswd = NULL;/* passwd for proxy authentication */
143static int proxyType = 0; /* uses TYPE or a@b ? */
144
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000145#ifdef SUPPORT_IP6
Daniel Veillard2db8c122003-07-08 12:16:59 +0000146static
147int have_ipv6(void) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000148 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 Taylor3473f882001-02-23 17:55:21 +0000159/**
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000160 * xmlFTPErrMemory:
161 * @extra: extra informations
162 *
163 * Handle an out of memory condition
164 */
165static void
166xmlFTPErrMemory(const char *extra)
167{
168 __xmlSimpleError(XML_FROM_FTP, XML_ERR_NO_MEMORY, NULL, NULL, extra);
169}
170
171/**
Owen Taylor3473f882001-02-23 17:55:21 +0000172 * xmlNanoFTPInit:
173 *
174 * Initialize the FTP protocol layer.
175 * Currently it just checks for proxy informations,
176 * and get the hostname
177 */
178
179void
180xmlNanoFTPInit(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 Taylor3473f882001-02-23 17:55:21 +0000194 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 Veillarde356c282001-03-10 12:32:04 +0000219 * xmlNanoFTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000220 *
221 * Cleanup the FTP protocol layer. This cleanup proxy informations.
222 */
223
224void
225xmlNanoFTPCleanup(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 Taylor3473f882001-02-23 17:55:21 +0000238#ifdef _WINSOCKAPI_
239 if (initialized)
240 WSACleanup();
241#endif
242 initialized = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000243}
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
258void
259xmlNanoFTPProxy(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
286static void
287xmlNanoFTPScanURL(void *ctx, const char *URL) {
288 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
289 const char *cur = URL;
290 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000291 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000292 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 Veillard56a4cb82001-03-24 17:00:36 +0000307 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000308 while (*cur != 0) {
309 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000310 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000311 ctxt->protocol = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000312 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000313 cur += 3;
314 break;
315 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000316 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000317 }
318 if (*cur == 0) return;
319
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000320 buf[indx] = 0;
Daniel Veillardc69e0b12001-11-20 08:35:07 +0000321 /* 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 Taylor3473f882001-02-23 17:55:21 +0000346 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000347 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 Veillard56a4cb82001-03-24 17:00:36 +0000365 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000366 ctxt->hostname = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000367 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000368 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000369 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000370 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000371 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 Taylor3473f882001-02-23 17:55:21 +0000380 }
Owen Taylor3473f882001-02-23 17:55:21 +0000381 break;
382 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000383 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 Taylor3473f882001-02-23 17:55:21 +0000405 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000406 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000407 }
408 if (*cur == 0)
409 ctxt->path = xmlMemStrdup("/");
410 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000411 indx = 0;
412 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000413 while (*cur != 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000414 buf[indx++] = *cur++;
415 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000416 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
434int
435xmlNanoFTPUpdateURL(void *ctx, const char *URL) {
436 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
437 const char *cur = URL;
438 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000439 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000440 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 Veillard56a4cb82001-03-24 17:00:36 +0000450 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000451 while (*cur != 0) {
452 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000453 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000454 if (strcmp(ctxt->protocol, buf))
455 return(-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000456 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000457 cur += 3;
458 break;
459 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000460 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000461 }
462 if (*cur == 0)
463 return(-1);
464
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000465 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000466 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000467 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 Veillard56a4cb82001-03-24 17:00:36 +0000485 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000486 if (strcmp (ctxt->hostname, buf))
487 return (-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000488 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000489 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000490 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000491 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000492 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 Taylor3473f882001-02-23 17:55:21 +0000502 }
Owen Taylor3473f882001-02-23 17:55:21 +0000503 break;
504 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000505 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 Taylor3473f882001-02-23 17:55:21 +0000530 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000531 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000532 }
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 Veillard56a4cb82001-03-24 17:00:36 +0000541 indx = 0;
542 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000543 while (*cur != 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000544 buf[indx++] = *cur++;
545 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000546 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
561void
562xmlNanoFTPScanProxy(const char *URL) {
563 const char *cur = URL;
564 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000565 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000566 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 Veillard56a4cb82001-03-24 17:00:36 +0000582 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000583 while (*cur != 0) {
584 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000585 buf[indx] = 0;
586 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000587 cur += 3;
588 break;
589 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000590 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000591 }
592 if (*cur == 0) return;
593
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000594 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000595 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000596 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 Veillard56a4cb82001-03-24 17:00:36 +0000613 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000614 proxy = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000615 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000616 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000617 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000618 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000619 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 Taylor3473f882001-02-23 17:55:21 +0000628 }
Owen Taylor3473f882001-02-23 17:55:21 +0000629 break;
630 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000631 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 Taylor3473f882001-02-23 17:55:21 +0000653 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000654 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000655 }
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
667void*
668xmlNanoFTPNewCtxt(const char *URL) {
669 xmlNanoFTPCtxtPtr ret;
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000670 char *unescaped;
Owen Taylor3473f882001-02-23 17:55:21 +0000671
672 ret = (xmlNanoFTPCtxtPtr) xmlMalloc(sizeof(xmlNanoFTPCtxt));
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000673 if (ret == NULL) {
674 xmlFTPErrMemory("allocating FTP context");
675 return(NULL);
676 }
Owen Taylor3473f882001-02-23 17:55:21 +0000677
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 Veillardc69e0b12001-11-20 08:35:07 +0000684 ret->controlFd = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000685
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000686 unescaped = xmlURIUnescapeString(URL, 0, NULL);
687 if (unescaped != NULL)
688 xmlNanoFTPScanURL(ret, unescaped);
689 else if (URL != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +0000690 xmlNanoFTPScanURL(ret, URL);
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000691 xmlFree(unescaped);
Owen Taylor3473f882001-02-23 17:55:21 +0000692
693 return(ret);
694}
695
696/**
697 * xmlNanoFTPFreeCtxt:
698 * @ctx: an FTP context
699 *
700 * Frees the context after closing the connection.
701 */
702
703void
704xmlNanoFTPFreeCtxt(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 Taylor3473f882001-02-23 17:55:21 +0000720 * @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 */
729static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000730xmlNanoFTPParseResponse(char *buf, int len) {
Owen Taylor3473f882001-02-23 17:55:21 +0000731 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 */
761static int
762xmlNanoFTPGetMore(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 Veillard60087f32001-10-10 09:45:09 +0000812 * Read the amount left on the control connection
Owen Taylor3473f882001-02-23 17:55:21 +0000813 */
814 if ((len = recv(ctxt->controlFd, &ctxt->controlBuf[ctxt->controlBufIndex],
815 size, 0)) < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000816 __xmlIOErr(XML_FROM_FTP, 0, "recv failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000817 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 */
839static int
840xmlNanoFTPReadResponse(void *ctx) {
841 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
842 char *ptr, *end;
843 int len;
844 int res = -1, cur = -1;
845
846get_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 Veillard56a4cb82001-03-24 17:00:36 +0000866 cur = xmlNanoFTPParseResponse(ptr, end - ptr);
Owen Taylor3473f882001-02-23 17:55:21 +0000867 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
910int
911xmlNanoFTPGetResponse(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
927int
928xmlNanoFTPCheckResponse(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 Veillard2b0f8792003-10-10 19:36:36 +0000941 __xmlIOErr(XML_FROM_FTP, 0, "select");
Owen Taylor3473f882001-02-23 17:55:21 +0000942 return(-1);
943
944 }
945
946 return(xmlNanoFTPReadResponse(ctx));
947}
948
949/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000950 * Send the user authentication
Owen Taylor3473f882001-02-23 17:55:21 +0000951 */
952
953static int
954xmlNanoFTPSendUser(void *ctx) {
955 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
956 char buf[200];
957 int len;
958 int res;
959
960 if (ctxt->user == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000961 snprintf(buf, sizeof(buf), "USER anonymous\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000962 else
Owen Taylor3473f882001-02-23 17:55:21 +0000963 snprintf(buf, sizeof(buf), "USER %s\r\n", ctxt->user);
Owen Taylor3473f882001-02-23 17:55:21 +0000964 buf[sizeof(buf) - 1] = 0;
965 len = strlen(buf);
966#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +0000967 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000968#endif
969 res = send(ctxt->controlFd, buf, len, 0);
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000970 if (res < 0) {
971 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
972 return(res);
973 }
Owen Taylor3473f882001-02-23 17:55:21 +0000974 return(0);
975}
976
977/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000978 * Send the password authentication
Owen Taylor3473f882001-02-23 17:55:21 +0000979 */
980
981static int
982xmlNanoFTPSendPasswd(void *ctx) {
983 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
984 char buf[200];
985 int len;
986 int res;
987
988 if (ctxt->passwd == NULL)
Daniel Veillard9ae1eba2001-10-19 09:48:35 +0000989 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000990 else
Owen Taylor3473f882001-02-23 17:55:21 +0000991 snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
Owen Taylor3473f882001-02-23 17:55:21 +0000992 buf[sizeof(buf) - 1] = 0;
993 len = strlen(buf);
994#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +0000995 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000996#endif
997 res = send(ctxt->controlFd, buf, len, 0);
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000998 if (res < 0) {
999 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1000 return(res);
1001 }
Owen Taylor3473f882001-02-23 17:55:21 +00001002 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
1015int
1016xmlNanoFTPQuit(void *ctx) {
1017 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1018 char buf[200];
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001019 int len, res;
Owen Taylor3473f882001-02-23 17:55:21 +00001020
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001021 snprintf(buf, sizeof(buf), "QUIT\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001022 len = strlen(buf);
1023#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001024 xmlGenericError(xmlGenericErrorContext, "%s", buf); /* Just to be consistent, even though we know it can't have a % in it */
Owen Taylor3473f882001-02-23 17:55:21 +00001025#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001026 res = send(ctxt->controlFd, buf, len, 0);
1027 if (res < 0) {
1028 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1029 return(res);
1030 }
Owen Taylor3473f882001-02-23 17:55:21 +00001031 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
1043int
1044xmlNanoFTPConnect(void *ctx) {
1045 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1046 struct hostent *hp;
1047 int port;
1048 int res;
Daniel Veillard2db8c122003-07-08 12:16:59 +00001049 int addrlen = sizeof (struct sockaddr_in);
Owen Taylor3473f882001-02-23 17:55:21 +00001050
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 Taylor3473f882001-02-23 17:55:21 +00001059 if (proxy) {
1060 port = proxyPort;
1061 } else {
1062 port = ctxt->port;
1063 }
1064 if (port == 0)
1065 port = 21;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001066
1067 memset (&ctxt->ftpAddr, 0, sizeof(ctxt->ftpAddr));
1068
1069#ifdef SUPPORT_IP6
1070 if (have_ipv6 ()) {
Daniel Veillard2db8c122003-07-08 12:16:59 +00001071 struct addrinfo hints, *tmp, *result;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001072
1073 result = NULL;
1074 memset (&hints, 0, sizeof(hints));
1075 hints.ai_socktype = SOCK_STREAM;
1076
1077 if (proxy) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001078 if (getaddrinfo (proxy, NULL, &hints, &result) != 0) {
1079 __xmlIOErr(XML_FROM_FTP, 0, "getaddrinfo failed");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001080 return (-1);
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001081 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001082 }
1083 else
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001084 if (getaddrinfo (ctxt->hostname, NULL, &hints, &result) != 0) {
1085 __xmlIOErr(XML_FROM_FTP, 0, "getaddrinfo failed");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001086 return (-1);
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001087 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001088
Daniel Veillard2db8c122003-07-08 12:16:59 +00001089 for (tmp = result; tmp; tmp = tmp->ai_next)
1090 if (tmp->ai_family == AF_INET || tmp->ai_family == AF_INET6)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001091 break;
1092
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001093 if (!tmp) {
1094 if (result)
1095 freeaddrinfo (result);
1096 return (-1);
1097 }
1098 else {
Daniel Veillard2db8c122003-07-08 12:16:59 +00001099 if (tmp->ai_family == AF_INET6) {
1100 memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001101 ((struct sockaddr_in6 *) &ctxt->ftpAddr)->sin6_port = htons (port);
1102 ctxt->controlFd = socket (AF_INET6, SOCK_STREAM, 0);
1103 }
1104 else {
Daniel Veillard2db8c122003-07-08 12:16:59 +00001105 memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001106 ((struct sockaddr_in *) &ctxt->ftpAddr)->sin_port = htons (port);
1107 ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0);
1108 }
Daniel Veillard2db8c122003-07-08 12:16:59 +00001109 addrlen = tmp->ai_addrlen;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001110 freeaddrinfo (result);
1111 }
1112 }
1113 else
1114#endif
1115 {
1116 if (proxy)
1117 hp = gethostbyname (proxy);
1118 else
1119 hp = gethostbyname (ctxt->hostname);
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001120 if (hp == NULL) {
1121 __xmlIOErr(XML_FROM_FTP, 0, "gethostbyname failed");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001122 return (-1);
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001123 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001124
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 Veillard2b0f8792003-10-10 19:36:36 +00001136 if (ctxt->controlFd < 0) {
1137 __xmlIOErr(XML_FROM_FTP, 0, "socket failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001138 return(-1);
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001139 }
Owen Taylor3473f882001-02-23 17:55:21 +00001140
1141 /*
1142 * Do the connect.
1143 */
1144 if (connect(ctxt->controlFd, (struct sockaddr *) &ctxt->ftpAddr,
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001145 addrlen) < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001146 __xmlIOErr(XML_FROM_FTP, 0, "Failed to create a connection");
Owen Taylor3473f882001-02-23 17:55:21 +00001147 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 Taylor3473f882001-02-23 17:55:21 +00001205 snprintf(buf, sizeof(buf), "USER %s\r\n", proxyUser);
Owen Taylor3473f882001-02-23 17:55:21 +00001206 buf[sizeof(buf) - 1] = 0;
1207 len = strlen(buf);
1208#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001209 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001210#endif
1211 res = send(ctxt->controlFd, buf, len, 0);
1212 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001213 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001214 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 Taylor3473f882001-02-23 17:55:21 +00001225 snprintf(buf, sizeof(buf), "PASS %s\r\n", proxyPasswd);
Owen Taylor3473f882001-02-23 17:55:21 +00001226 else
Daniel Veillard9ae1eba2001-10-19 09:48:35 +00001227 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001228 buf[sizeof(buf) - 1] = 0;
1229 len = strlen(buf);
1230#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001231 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001232#endif
1233 res = send(ctxt->controlFd, buf, len, 0);
1234 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001235 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001236 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 Veillardcbaf3992001-12-31 16:16:02 +00001265 /* we will try in sequence */
Owen Taylor3473f882001-02-23 17:55:21 +00001266 case 1:
1267 /* Using SITE command */
Owen Taylor3473f882001-02-23 17:55:21 +00001268 snprintf(buf, sizeof(buf), "SITE %s\r\n", ctxt->hostname);
Owen Taylor3473f882001-02-23 17:55:21 +00001269 buf[sizeof(buf) - 1] = 0;
1270 len = strlen(buf);
1271#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001272 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001273#endif
1274 res = send(ctxt->controlFd, buf, len, 0);
1275 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001276 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001277 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 Taylor3473f882001-02-23 17:55:21 +00001295 snprintf(buf, sizeof(buf), "USER anonymous@%s\r\n",
1296 ctxt->hostname);
Owen Taylor3473f882001-02-23 17:55:21 +00001297 else
Owen Taylor3473f882001-02-23 17:55:21 +00001298 snprintf(buf, sizeof(buf), "USER %s@%s\r\n",
1299 ctxt->user, ctxt->hostname);
Owen Taylor3473f882001-02-23 17:55:21 +00001300 buf[sizeof(buf) - 1] = 0;
1301 len = strlen(buf);
1302#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001303 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001304#endif
1305 res = send(ctxt->controlFd, buf, len, 0);
1306 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001307 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001308 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 Veillard9ae1eba2001-10-19 09:48:35 +00001319 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001320 else
Owen Taylor3473f882001-02-23 17:55:21 +00001321 snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
Owen Taylor3473f882001-02-23 17:55:21 +00001322 buf[sizeof(buf) - 1] = 0;
1323 len = strlen(buf);
1324#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001325 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001326#endif
1327 res = send(ctxt->controlFd, buf, len, 0);
1328 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001329 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001330 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 Veillard2b0f8792003-10-10 19:36:36 +00001391 __xmlIOErr(XML_FROM_FTP, XML_FTP_ACCNT,
1392 "FTP server asking for ACCNT on anonymous\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001393 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
1416void*
1417xmlNanoFTPConnectTo(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
1446int
1447xmlNanoFTPCwd(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 Taylor3473f882001-02-23 17:55:21 +00001460 snprintf(buf, sizeof(buf), "CWD %s\r\n", directory);
Owen Taylor3473f882001-02-23 17:55:21 +00001461 buf[sizeof(buf) - 1] = 0;
1462 len = strlen(buf);
1463#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001464 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001465#endif
1466 res = send(ctxt->controlFd, buf, len, 0);
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001467 if (res < 0) {
1468 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1469 return(res);
1470 }
Owen Taylor3473f882001-02-23 17:55:21 +00001471 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 Veillard6c73cb82003-03-05 16:45:40 +00001483 * 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
1492int
1493xmlNanoFTPDele(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 Veillard2b0f8792003-10-10 19:36:36 +00001515 if (res < 0) {
1516 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1517 return(res);
1518 }
Daniel Veillard6c73cb82003-03-05 16:45:40 +00001519 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 Taylor3473f882001-02-23 17:55:21 +00001530 * 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
1539int
1540xmlNanoFTPGetConnection(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 Veillardde2a67b2003-06-21 14:20:04 +00001547#ifdef SUPPORT_IP6
1548 struct sockaddr_storage dataAddr;
1549#else
Owen Taylor3473f882001-02-23 17:55:21 +00001550 struct sockaddr_in dataAddr;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001551#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001552 SOCKLEN_T dataAddrLen;
1553
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001554 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 Taylor3473f882001-02-23 17:55:21 +00001566 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001567
1568 if (ctxt->dataFd < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001569 __xmlIOErr(XML_FROM_FTP, 0, "socket failed");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001570 return (-1);
1571 }
Owen Taylor3473f882001-02-23 17:55:21 +00001572
1573 if (ctxt->passive) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001574#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 Taylor3473f882001-02-23 17:55:21 +00001581#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001582 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001583#endif
1584 res = send(ctxt->controlFd, buf, len, 0);
1585 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001586 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001587 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 Veillardde2a67b2003-06-21 14:20:04 +00001605#ifdef SUPPORT_IP6
1606 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1607 if (sscanf (cur, "%u", &temp[0]) != 1) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001608 __xmlIOErr(XML_FROM_FTP, XML_FTP_EPSV_ANSWER,
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001609 "Invalid answer to EPSV\n");
1610 if (ctxt->dataFd != -1) {
1611 closesocket (ctxt->dataFd); ctxt->dataFd = -1;
1612 }
1613 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001614 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001615 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 Taylor3473f882001-02-23 17:55:21 +00001617 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001618 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 Veillard2b0f8792003-10-10 19:36:36 +00001623 __xmlIOErr(XML_FROM_FTP, XML_FTP_PASV_ANSWER,
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001624 "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 Taylor3473f882001-02-23 17:55:21 +00001635 if (connect(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001636 __xmlIOErr(XML_FROM_FTP, 0, "Failed to create a data connection");
Owen Taylor3473f882001-02-23 17:55:21 +00001637 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1638 return (-1);
1639 }
1640 } else {
1641 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001642#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 Taylor3473f882001-02-23 17:55:21 +00001649 if (bind(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001650 __xmlIOErr(XML_FROM_FTP, 0, "bind failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001651 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 Veillard2b0f8792003-10-10 19:36:36 +00001657 __xmlIOErr(XML_FROM_FTP, 0, "listen failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001658 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1659 return (-1);
1660 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001661#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 Veillard2db8c122003-07-08 12:16:59 +00001666 adp = (unsigned char *) buf6;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001667 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 Taylor3473f882001-02-23 17:55:21 +00001679 buf[sizeof(buf) - 1] = 0;
1680 len = strlen(buf);
1681#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001682 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001683#endif
1684
1685 res = send(ctxt->controlFd, buf, len, 0);
1686 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001687 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001688 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
1710int
1711xmlNanoFTPCloseConnection(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 Veillard60087f32001-10-10 09:45:09 +00001756 * Returns -1 incase of error, the length of data parsed otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00001757 */
1758
1759static int
1760xmlNanoFTPParseList(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
1887int
1888xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData,
1889 char *filename) {
1890 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1891 char buf[4096 + 1];
1892 int len, res;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001893 int indx = 0, base;
Owen Taylor3473f882001-02-23 17:55:21 +00001894 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 Sanin49cc9752002-06-14 17:07:10 +00001903 snprintf(buf, sizeof(buf), "LIST -L\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001904 } 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 Taylor3473f882001-02-23 17:55:21 +00001912 snprintf(buf, sizeof(buf), "LIST -L %s\r\n", filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001913 }
1914 buf[sizeof(buf) - 1] = 0;
1915 len = strlen(buf);
1916#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001917 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001918#endif
1919 res = send(ctxt->controlFd, buf, len, 0);
1920 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001921 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001922 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 Veillard56a4cb82001-03-24 17:00:36 +00001961 if ((len = recv(ctxt->dataFd, &buf[indx], sizeof(buf) - (indx + 1), 0)) < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001962 __xmlIOErr(XML_FROM_FTP, 0, "recv");
Owen Taylor3473f882001-02-23 17:55:21 +00001963 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1964 ctxt->dataFd = -1;
1965 return(-1);
1966 }
1967#ifdef DEBUG_FTP
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001968 write(1, &buf[indx], len);
Owen Taylor3473f882001-02-23 17:55:21 +00001969#endif
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001970 indx += len;
1971 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001972 base = 0;
1973 do {
1974 res = xmlNanoFTPParseList(&buf[base], callback, userData);
1975 base += res;
1976 } while (res > 0);
1977
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001978 memmove(&buf[0], &buf[base], indx - base);
1979 indx -= base;
Owen Taylor3473f882001-02-23 17:55:21 +00001980 } 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
1996int
1997xmlNanoFTPGetSocket(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 Sanin49cc9752002-06-14 17:07:10 +00002007 snprintf(buf, sizeof(buf), "TYPE I\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002008 len = strlen(buf);
2009#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00002010 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00002011#endif
2012 res = send(ctxt->controlFd, buf, len, 0);
2013 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00002014 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002015 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 Taylor3473f882001-02-23 17:55:21 +00002024 snprintf(buf, sizeof(buf), "RETR %s\r\n", ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00002025 else
Owen Taylor3473f882001-02-23 17:55:21 +00002026 snprintf(buf, sizeof(buf), "RETR %s\r\n", filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002027 buf[sizeof(buf) - 1] = 0;
2028 len = strlen(buf);
2029#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00002030 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00002031#endif
2032 res = send(ctxt->controlFd, buf, len, 0);
2033 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00002034 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002035 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
2059int
2060xmlNanoFTPGet(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 Veillard2b0f8792003-10-10 19:36:36 +00002103 __xmlIOErr(XML_FROM_FTP, 0, "recv failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002104 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 */
2126int
2127xmlNanoFTPRead(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 Veillard2b0f8792003-10-10 19:36:36 +00002136 if (len <= 0) {
2137 __xmlIOErr(XML_FROM_FTP, 0, "recv failed");
2138 xmlNanoFTPCloseConnection(ctxt);
2139 }
Owen Taylor3473f882001-02-23 17:55:21 +00002140#ifdef DEBUG_FTP
2141 xmlGenericError(xmlGenericErrorContext, "Recvd %d bytes\n", len);
2142#endif
Owen Taylor3473f882001-02-23 17:55:21 +00002143 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
2155void*
2156xmlNanoFTPOpen(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
2187int
2188xmlNanoFTPClose(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 Veillard01c13b52002-12-10 15:19:08 +00002213static
Owen Taylor3473f882001-02-23 17:55:21 +00002214void 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 Veillard01c13b52002-12-10 15:19:08 +00002220static
Owen Taylor3473f882001-02-23 17:55:21 +00002221void ftpData(void *userData, const char *data, int len) {
2222 if (userData == NULL) return;
2223 if (len <= 0) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00002224 fclose((FILE*)userData);
Owen Taylor3473f882001-02-23 17:55:21 +00002225 return;
2226 }
Daniel Veillard82cb3192003-10-29 13:39:15 +00002227 fwrite(data, len, 1, (FILE*)userData);
Owen Taylor3473f882001-02-23 17:55:21 +00002228}
2229
2230int 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>
2268int 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 */