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