blob: 9ebe015e22fb0868410f03ade9f40328b01adbf2 [file] [log] [blame]
Daniel Veillard06047432000-04-24 11:33:38 +00001/*
2 * nanoftp.c: basic handling of an FTP command connection to check for
Daniel Veillardda07c342000-01-25 18:31:22 +00003 * directory availability. No transfer is needed.
4 *
5 * Reference: RFC 959
6 */
7
8#ifdef WIN32
Daniel Veillardf341f932000-02-02 14:52:08 +00009#define INCLUDE_WINSOCK
Daniel Veillardda07c342000-01-25 18:31:22 +000010#include "win32config.h"
11#else
12#include "config.h"
13#endif
Daniel Veillard06047432000-04-24 11:33:38 +000014
Daniel Veillard361d8452000-04-03 19:48:13 +000015#include "xmlversion.h"
Daniel Veillardda07c342000-01-25 18:31:22 +000016
Daniel Veillard361d8452000-04-03 19:48:13 +000017#ifdef LIBXML_FTP_ENABLED
Daniel Veillardda07c342000-01-25 18:31:22 +000018#include <stdio.h>
19#include <string.h>
20
Daniel Veillard06047432000-04-24 11:33:38 +000021#ifdef HAVE_STDLIB_H
22#include <stdlib.h>
Daniel Veillardda07c342000-01-25 18:31:22 +000023#endif
24#ifdef HAVE_UNISTD_H
25#include <unistd.h>
26#endif
Daniel Veillardda07c342000-01-25 18:31:22 +000027#ifdef HAVE_SYS_SOCKET_H
28#include <sys/socket.h>
29#endif
James Henstridgef3be9312000-01-28 13:59:21 +000030#ifdef HAVE_NETINET_IN_H
31#include <netinet/in.h>
32#endif
Daniel Veillarde41f2b72000-01-30 20:00:07 +000033#ifdef HAVE_ARPA_INET_H
34#include <arpa/inet.h>
35#endif
Daniel Veillardda07c342000-01-25 18:31:22 +000036#ifdef HAVE_NETDB_H
37#include <netdb.h>
38#endif
Daniel Veillarde41f2b72000-01-30 20:00:07 +000039#ifdef HAVE_FCNTL_H
40#include <fcntl.h>
41#endif
42#ifdef HAVE_ERRNO_H
43#include <errno.h>
44#endif
45#ifdef HAVE_SYS_TIME_H
46#include <sys/time.h>
47#endif
48#ifdef HAVE_SYS_SELECT_H
49#include <sys/select.h>
50#endif
Daniel Veillard5feb8492000-02-02 17:15:36 +000051#ifdef HAVE_STRINGS_H
52#include <strings.h>
53#endif
Daniel Veillardda07c342000-01-25 18:31:22 +000054
Daniel Veillard361d8452000-04-03 19:48:13 +000055#include <libxml/xmlmemory.h>
56#include <libxml/nanoftp.h>
Daniel Veillardda07c342000-01-25 18:31:22 +000057
58/* #define DEBUG_FTP 1 */
59#ifdef STANDALONE
Daniel Veillarde41f2b72000-01-30 20:00:07 +000060#ifndef DEBUG_FTP
Daniel Veillardda07c342000-01-25 18:31:22 +000061#define DEBUG_FTP 1
62#endif
Daniel Veillarde41f2b72000-01-30 20:00:07 +000063#endif
Daniel Veillardda07c342000-01-25 18:31:22 +000064
65static char hostname[100];
66
67#define FTP_COMMAND_OK 200
68#define FTP_SYNTAX_ERROR 500
69#define FTP_GET_PASSWD 331
70
71typedef struct xmlNanoFTPCtxt {
72 char *protocol; /* the protocol name */
73 char *hostname; /* the host name */
74 int port; /* the port */
75 char *path; /* the path within the URL */
76 char *user; /* user string */
77 char *passwd; /* passwd string */
78 struct sockaddr_in ftpAddr; /* the socket address struct */
79 int passive; /* currently we support only passive !!! */
80 int controlFd; /* the file descriptor for the control socket */
81 int dataFd; /* the file descriptor for the data socket */
82 int state; /* WRITE / READ / CLOSED */
83 int returnValue; /* the protocol return value */
84} xmlNanoFTPCtxt, *xmlNanoFTPCtxtPtr;
85
Daniel Veillarde41f2b72000-01-30 20:00:07 +000086static int initialized = 0;
87static char *proxy = NULL; /* the proxy name if any */
88static int proxyPort = 0; /* the proxy port if any */
89static char *proxyUser = NULL; /* user for proxy authentication */
90static char *proxyPasswd = NULL;/* passwd for proxy authentication */
91static int proxyType = 0; /* uses TYPE or a@b ? */
92
93/**
94 * xmlNanoFTPInit:
95 *
96 * Initialize the FTP protocol layer.
97 * Currently it just checks for proxy informations,
98 * and get the hostname
99 */
100
101void
102xmlNanoFTPInit(void) {
103 const char *env;
104
105 if (initialized)
106 return;
107
108 gethostname(hostname, sizeof(hostname));
109
110 proxyPort = 21;
111 env = getenv("no_proxy");
112 if (env != NULL)
113 return;
114 env = getenv("ftp_proxy");
115 if (env != NULL) {
116 xmlNanoFTPScanProxy(env);
117 } else {
118 env = getenv("FTP_PROXY");
119 if (env != NULL) {
120 xmlNanoFTPScanProxy(env);
121 }
122 }
123 env = getenv("ftp_proxy_user");
124 if (env != NULL) {
125 proxyUser = xmlMemStrdup(env);
126 }
127 env = getenv("ftp_proxy_password");
128 if (env != NULL) {
129 proxyPasswd = xmlMemStrdup(env);
130 }
131 initialized = 1;
132}
133
134/**
135 * xmlNanoFTPClenup:
136 *
137 * Cleanup the FTP protocol layer. This cleanup proxy informations.
138 */
139
140void
141xmlNanoFTPCleanup(void) {
142 if (proxy != NULL) {
143 xmlFree(proxy);
144 proxy = NULL;
145 }
146 if (proxyUser != NULL) {
147 xmlFree(proxyUser);
148 proxyUser = NULL;
149 }
150 if (proxyPasswd != NULL) {
151 xmlFree(proxyPasswd);
152 proxyPasswd = NULL;
153 }
154 hostname[0] = 0;
155 initialized = 0;
156 return;
157}
158
159/**
160 * xmlNanoFTPProxy:
161 * @host: the proxy host name
162 * @port: the proxy port
163 * @user: the proxy user name
164 * @passwd: the proxy password
165 * @type: the type of proxy 1 for using SITE, 2 for USER a@b
166 *
167 * Setup the FTP proxy informations.
168 * This can also be done by using ftp_proxy ftp_proxy_user and
169 * ftp_proxy_password environment variables.
170 */
171
172void
173xmlNanoFTPProxy(const char *host, int port, const char *user,
174 const char *passwd, int type) {
175 if (proxy != NULL)
176 xmlFree(proxy);
177 if (proxyUser != NULL)
178 xmlFree(proxyUser);
179 if (proxyPasswd != NULL)
180 xmlFree(proxyPasswd);
181 if (host)
182 proxy = xmlMemStrdup(host);
183 if (user)
184 proxyUser = xmlMemStrdup(user);
185 if (passwd)
186 proxyPasswd = xmlMemStrdup(passwd);
187 proxyPort = port;
188 proxyType = type;
189}
190
Daniel Veillardda07c342000-01-25 18:31:22 +0000191/**
192 * xmlNanoFTPScanURL:
193 * @ctx: an FTP context
194 * @URL: The URL used to initialize the context
195 *
196 * (Re)Initialize an FTP context by parsing the URL and finding
197 * the protocol host port and path it indicates.
198 */
199
200static void
201xmlNanoFTPScanURL(void *ctx, const char *URL) {
202 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
203 const char *cur = URL;
204 char buf[4096];
205 int index = 0;
206 int port = 0;
207
208 if (ctxt->protocol != NULL) {
209 xmlFree(ctxt->protocol);
210 ctxt->protocol = NULL;
211 }
212 if (ctxt->hostname != NULL) {
213 xmlFree(ctxt->hostname);
214 ctxt->hostname = NULL;
215 }
216 if (ctxt->path != NULL) {
217 xmlFree(ctxt->path);
218 ctxt->path = NULL;
219 }
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000220 if (URL == NULL) return;
Daniel Veillardda07c342000-01-25 18:31:22 +0000221 buf[index] = 0;
222 while (*cur != 0) {
223 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
224 buf[index] = 0;
225 ctxt->protocol = xmlMemStrdup(buf);
226 index = 0;
227 cur += 3;
228 break;
229 }
230 buf[index++] = *cur++;
231 }
232 if (*cur == 0) return;
233
234 buf[index] = 0;
235 while (1) {
236 if (cur[0] == ':') {
237 buf[index] = 0;
238 ctxt->hostname = xmlMemStrdup(buf);
239 index = 0;
240 cur += 1;
241 while ((*cur >= '0') && (*cur <= '9')) {
242 port *= 10;
243 port += *cur - '0';
244 cur++;
245 }
246 if (port != 0) ctxt->port = port;
247 while ((cur[0] != '/') && (*cur != 0))
248 cur++;
249 break;
250 }
251 if ((*cur == '/') || (*cur == 0)) {
252 buf[index] = 0;
253 ctxt->hostname = xmlMemStrdup(buf);
254 index = 0;
255 break;
256 }
257 buf[index++] = *cur++;
258 }
259 if (*cur == 0)
260 ctxt->path = xmlMemStrdup("/");
261 else {
Daniel Veillard726e8792000-01-30 20:04:29 +0000262 index = 0;
Daniel Veillardda07c342000-01-25 18:31:22 +0000263 buf[index] = 0;
Daniel Veillard726e8792000-01-30 20:04:29 +0000264 while (*cur != 0)
Daniel Veillardda07c342000-01-25 18:31:22 +0000265 buf[index++] = *cur++;
Daniel Veillardda07c342000-01-25 18:31:22 +0000266 buf[index] = 0;
267 ctxt->path = xmlMemStrdup(buf);
268 }
269}
270
271/**
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000272 * xmlNanoFTPUpdateURL:
273 * @ctx: an FTP context
274 * @URL: The URL used to update the context
275 *
276 * Update an FTP context by parsing the URL and finding
277 * new path it indicates. If there is an error in the
278 * protocol, hostname, port or other information, the
279 * error is raised. It indicates a new connection has to
280 * be established.
281 *
282 * Returns 0 if Ok, -1 in case of error (other host).
283 */
284
285int
286xmlNanoFTPUpdateURL(void *ctx, const char *URL) {
287 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
288 const char *cur = URL;
289 char buf[4096];
290 int index = 0;
291 int port = 0;
292
293 if (URL == NULL)
294 return(-1);
295 if (ctxt == NULL)
296 return(-1);
297 if (ctxt->protocol == NULL)
298 return(-1);
299 if (ctxt->hostname == NULL)
300 return(-1);
301 buf[index] = 0;
302 while (*cur != 0) {
303 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
304 buf[index] = 0;
305 if (strcmp(ctxt->protocol, buf))
306 return(-1);
307 index = 0;
308 cur += 3;
309 break;
310 }
311 buf[index++] = *cur++;
312 }
313 if (*cur == 0)
314 return(-1);
315
316 buf[index] = 0;
317 while (1) {
318 if (cur[0] == ':') {
319 buf[index] = 0;
320 if (strcmp(ctxt->hostname, buf))
321 return(-1);
322 index = 0;
323 cur += 1;
324 while ((*cur >= '0') && (*cur <= '9')) {
325 port *= 10;
326 port += *cur - '0';
327 cur++;
328 }
329 if (port != ctxt->port)
330 return(-1);
331 while ((cur[0] != '/') && (*cur != 0))
332 cur++;
333 break;
334 }
335 if ((*cur == '/') || (*cur == 0)) {
336 buf[index] = 0;
337 if (strcmp(ctxt->hostname, buf))
338 return(-1);
339 index = 0;
340 break;
341 }
342 buf[index++] = *cur++;
343 }
344 if (ctxt->path != NULL) {
345 xmlFree(ctxt->path);
346 ctxt->path = NULL;
347 }
348
349 if (*cur == 0)
350 ctxt->path = xmlMemStrdup("/");
351 else {
Daniel Veillard726e8792000-01-30 20:04:29 +0000352 index = 0;
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000353 buf[index] = 0;
Daniel Veillard726e8792000-01-30 20:04:29 +0000354 while (*cur != 0)
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000355 buf[index++] = *cur++;
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000356 buf[index] = 0;
357 ctxt->path = xmlMemStrdup(buf);
358 }
359 return(0);
360}
361
362/**
363 * xmlNanoFTPScanProxy:
364 * @URL: The proxy URL used to initialize the proxy context
365 *
366 * (Re)Initialize the FTP Proxy context by parsing the URL and finding
367 * the protocol host port it indicates.
368 * Should be like ftp://myproxy/ or ftp://myproxy:3128/
369 * A NULL URL cleans up proxy informations.
370 */
371
372void
373xmlNanoFTPScanProxy(const char *URL) {
374 const char *cur = URL;
375 char buf[4096];
376 int index = 0;
377 int port = 0;
378
379 if (proxy != NULL) {
380 xmlFree(proxy);
381 proxy = NULL;
382 }
383 if (proxyPort != 0) {
384 proxyPort = 0;
385 }
386#ifdef DEBUG_FTP
387 if (URL == NULL)
388 printf("Removing FTP proxy info\n");
389 else
390 printf("Using FTP proxy %s\n", URL);
391#endif
392 if (URL == NULL) return;
393 buf[index] = 0;
394 while (*cur != 0) {
395 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
396 buf[index] = 0;
397 index = 0;
398 cur += 3;
399 break;
400 }
401 buf[index++] = *cur++;
402 }
403 if (*cur == 0) return;
404
405 buf[index] = 0;
406 while (1) {
407 if (cur[0] == ':') {
408 buf[index] = 0;
409 proxy = xmlMemStrdup(buf);
410 index = 0;
411 cur += 1;
412 while ((*cur >= '0') && (*cur <= '9')) {
413 port *= 10;
414 port += *cur - '0';
415 cur++;
416 }
417 if (port != 0) proxyPort = port;
418 while ((cur[0] != '/') && (*cur != 0))
419 cur++;
420 break;
421 }
422 if ((*cur == '/') || (*cur == 0)) {
423 buf[index] = 0;
424 proxy = xmlMemStrdup(buf);
425 index = 0;
426 break;
427 }
428 buf[index++] = *cur++;
429 }
430}
431
432/**
Daniel Veillardda07c342000-01-25 18:31:22 +0000433 * xmlNanoFTPNewCtxt:
434 * @URL: The URL used to initialize the context
435 *
436 * Allocate and initialize a new FTP context.
437 *
438 * Returns an FTP context or NULL in case of error.
439 */
440
Daniel Veillard06047432000-04-24 11:33:38 +0000441void*
Daniel Veillardda07c342000-01-25 18:31:22 +0000442xmlNanoFTPNewCtxt(const char *URL) {
443 xmlNanoFTPCtxtPtr ret;
444
445 ret = (xmlNanoFTPCtxtPtr) xmlMalloc(sizeof(xmlNanoFTPCtxt));
446 if (ret == NULL) return(NULL);
447
448 memset(ret, 0, sizeof(xmlNanoFTPCtxt));
449 ret->port = 21;
450 ret->passive = 1;
451 ret->returnValue = 0;
452
453 if (URL != NULL)
454 xmlNanoFTPScanURL(ret, URL);
455
456 return(ret);
457}
458
459/**
460 * xmlNanoFTPFreeCtxt:
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000461 * @ctx: an FTP context
Daniel Veillardda07c342000-01-25 18:31:22 +0000462 *
463 * Frees the context after closing the connection.
464 */
465
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000466void
467xmlNanoFTPFreeCtxt(void * ctx) {
468 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
Daniel Veillardda07c342000-01-25 18:31:22 +0000469 if (ctxt == NULL) return;
470 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
471 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
472 if (ctxt->path != NULL) xmlFree(ctxt->path);
473 ctxt->passive = 1;
474 if (ctxt->controlFd >= 0) close(ctxt->controlFd);
475 ctxt->controlFd = -1;
476 xmlFree(ctxt);
477}
478
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000479/**
Daniel Veillard06047432000-04-24 11:33:38 +0000480 * xmlNanoFTPParseResponse:
481 * @ctx: the FTP connection context
482 * @buf: the buffer containing the response
483 * @len: the buffer length
484 *
Daniel Veillardda07c342000-01-25 18:31:22 +0000485 * Parsing of the server answer, we just extract the code.
Daniel Veillard06047432000-04-24 11:33:38 +0000486 *
487 * returns 0 for errors
Daniel Veillardda07c342000-01-25 18:31:22 +0000488 * +XXX for last line of response
489 * -XXX for response to be continued
490 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000491static int
Daniel Veillardda07c342000-01-25 18:31:22 +0000492xmlNanoFTPParseResponse(void *ctx, char *buf, int len) {
493 int val = 0;
494
495 if (len < 3) return(-1);
496 if ((*buf >= '0') && (*buf <= '9'))
497 val = val * 10 + (*buf - '0');
498 else
499 return(0);
500 buf++;
501 if ((*buf >= '0') && (*buf <= '9'))
502 val = val * 10 + (*buf - '0');
503 else
504 return(0);
505 buf++;
506 if ((*buf >= '0') && (*buf <= '9'))
507 val = val * 10 + (*buf - '0');
508 else
509 return(0);
510 buf++;
511 if (*buf == '-')
512 return(-val);
513 return(val);
514}
515
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000516/**
517 * xmlNanoFTPReadResponse:
518 * @ctx: an FTP context
519 * @buf: buffer to read in
520 * @size: buffer length
521 *
Daniel Veillardda07c342000-01-25 18:31:22 +0000522 * Read the response from the FTP server after a command.
523 * Returns the code number
Daniel Veillardda07c342000-01-25 18:31:22 +0000524 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000525static int
Daniel Veillardda07c342000-01-25 18:31:22 +0000526xmlNanoFTPReadResponse(void *ctx, char *buf, int size) {
527 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
528 char *ptr, *end;
529 int len;
530 int res = -1;
531
532 if (size <= 0) return(-1);
533
534get_more:
535 if ((len = recv(ctxt->controlFd, buf, size - 1, 0)) < 0) {
536 close(ctxt->controlFd); ctxt->controlFd = -1;
537 ctxt->controlFd = -1;
538 return(-1);
539 }
540 if (len == 0) {
541 return(-1);
542 }
543
544 end = &buf[len];
545 *end = 0;
546#ifdef DEBUG_FTP
547 printf(buf);
548#endif
549 ptr = buf;
550 while (ptr < end) {
551 res = xmlNanoFTPParseResponse(ctxt, ptr, end - ptr);
552 if (res > 0) break;
553 if (res == 0) {
554#ifdef DEBUG_FTP
555 fprintf(stderr, "xmlNanoFTPReadResponse failed: %s\n", ptr);
556#endif
557 return(-1);
558 }
559 while ((ptr < end) && (*ptr != '\n')) ptr++;
560 if (ptr >= end) {
561#ifdef DEBUG_FTP
562 fprintf(stderr, "xmlNanoFTPReadResponse: unexpected end %s\n", buf);
563#endif
564 return((-res) / 100);
565 }
566 if (*ptr != '\r') ptr++;
567 }
568
569 if (res < 0) goto get_more;
570
571#ifdef DEBUG_FTP
572 printf("Got %d\n", res);
573#endif
574 return(res / 100);
575}
576
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000577/**
578 * xmlNanoFTPGetResponse:
579 * @ctx: an FTP context
580 *
Daniel Veillardda07c342000-01-25 18:31:22 +0000581 * Get the response from the FTP server after a command.
582 * Returns the code number
Daniel Veillardda07c342000-01-25 18:31:22 +0000583 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000584
Daniel Veillardda07c342000-01-25 18:31:22 +0000585int
586xmlNanoFTPGetResponse(void *ctx) {
587 char buf[16 * 1024 + 1];
588
589/**************
590 fd_set rfd;
591 struct timeval tv;
592 int res;
593
594 tv.tv_sec = 10;
595 tv.tv_usec = 0;
596 FD_ZERO(&rfd);
597 FD_SET(ctxt->controlFd, &rfd);
598 res = select(ctxt->controlFd + 1, &rfd, NULL, NULL, &tv);
599 if (res <= 0) return(res);
600 **************/
601
602 return(xmlNanoFTPReadResponse(ctx, buf, 16 * 1024));
603}
604
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000605/**
606 * xmlNanoFTPCheckResponse:
607 * @ctx: an FTP context
608 *
Daniel Veillardda07c342000-01-25 18:31:22 +0000609 * Check if there is a response from the FTP server after a command.
610 * Returns the code number, or 0
611 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000612
Daniel Veillardda07c342000-01-25 18:31:22 +0000613int
614xmlNanoFTPCheckResponse(void *ctx) {
615 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
616 char buf[1024 + 1];
617 fd_set rfd;
618 struct timeval tv;
619
620 tv.tv_sec = 0;
621 tv.tv_usec = 0;
622 FD_ZERO(&rfd);
623 FD_SET(ctxt->controlFd, &rfd);
624 switch(select(ctxt->controlFd + 1, &rfd, NULL, NULL, &tv)) {
625 case 0:
626 return(0);
627 case -1:
628#ifdef DEBUG_FTP
629 perror("select");
630#endif
631 return(-1);
632
633 }
634
635 return(xmlNanoFTPReadResponse(ctx, buf, 1024));
636}
637
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000638/**
Daniel Veillardda07c342000-01-25 18:31:22 +0000639 * Send the user authentification
640 */
641
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000642static int
643xmlNanoFTPSendUser(void *ctx) {
Daniel Veillardda07c342000-01-25 18:31:22 +0000644 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
645 char buf[200];
646 int len;
647 int res;
648
649 if (ctxt->user == NULL)
Daniel Veillard13c757e2000-02-01 23:59:15 +0000650#ifndef HAVE_SNPRINTF
651 len = sprintf(buf, "USER anonymous\r\n");
652#else /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +0000653 len = snprintf(buf, sizeof(buf), "USER anonymous\r\n");
Daniel Veillard13c757e2000-02-01 23:59:15 +0000654#endif /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +0000655 else
Daniel Veillard13c757e2000-02-01 23:59:15 +0000656#ifndef HAVE_SNPRINTF
657 len = sprintf(buf, "USER %s\r\n", ctxt->user);
658#else /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +0000659 len = snprintf(buf, sizeof(buf), "USER %s\r\n", ctxt->user);
Daniel Veillard13c757e2000-02-01 23:59:15 +0000660#endif /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +0000661#ifdef DEBUG_FTP
662 printf(buf);
663#endif
664 res = send(ctxt->controlFd, buf, len, 0);
665 if (res < 0) return(res);
666 return(0);
667}
668
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000669/**
Daniel Veillardda07c342000-01-25 18:31:22 +0000670 * Send the password authentification
671 */
672
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000673static int
674xmlNanoFTPSendPasswd(void *ctx) {
Daniel Veillardda07c342000-01-25 18:31:22 +0000675 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
676 char buf[200];
677 int len;
678 int res;
679
680 if (ctxt->passwd == NULL)
Daniel Veillard13c757e2000-02-01 23:59:15 +0000681#ifndef HAVE_SNPRINTF
682 len = sprintf(buf, "PASS libxml@%s\r\n", hostname);
683#else /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +0000684 len = snprintf(buf, sizeof(buf), "PASS libxml@%s\r\n", hostname);
Daniel Veillard13c757e2000-02-01 23:59:15 +0000685#endif /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +0000686 else
Daniel Veillard13c757e2000-02-01 23:59:15 +0000687#ifndef HAVE_SNPRINTF
688 len = sprintf(buf, "PASS %s\r\n", ctxt->passwd);
689#else /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +0000690 len = snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
Daniel Veillard13c757e2000-02-01 23:59:15 +0000691#endif /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +0000692#ifdef DEBUG_FTP
693 printf(buf);
694#endif
695 res = send(ctxt->controlFd, buf, len, 0);
696 if (res < 0) return(res);
697 return(0);
698}
699
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000700/**
701 * xmlNanoFTPQuit:
702 * @ctx: an FTP context
703 *
704 * Send a QUIT command to the server
705 *
706 * Returns -1 in case of error, 0 otherwise
Daniel Veillardda07c342000-01-25 18:31:22 +0000707 */
708
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000709
Daniel Veillardda07c342000-01-25 18:31:22 +0000710int
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000711xmlNanoFTPQuit(void *ctx) {
Daniel Veillardda07c342000-01-25 18:31:22 +0000712 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
713 char buf[200];
714 int len;
715 int res;
716
Daniel Veillard13c757e2000-02-01 23:59:15 +0000717#ifndef HAVE_SNPRINTF
718 len = sprintf(buf, "QUIT\r\n");
719#else /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +0000720 len = snprintf(buf, sizeof(buf), "QUIT\r\n");
Daniel Veillard13c757e2000-02-01 23:59:15 +0000721#endif /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +0000722#ifdef DEBUG_FTP
723 printf(buf);
724#endif
725 res = send(ctxt->controlFd, buf, len, 0);
726 return(0);
727}
728
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000729/**
730 * xmlNanoFTPConnect:
731 * @ctx: an FTP context
732 *
733 * Tries to open a control connection
734 *
735 * Returns -1 in case of error, 0 otherwise
Daniel Veillardda07c342000-01-25 18:31:22 +0000736 */
737
738int
739xmlNanoFTPConnect(void *ctx) {
740 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
741 struct hostent *hp;
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000742 int port;
Daniel Veillardda07c342000-01-25 18:31:22 +0000743 int res;
744
745 if (ctxt == NULL)
746 return(-1);
747 if (ctxt->hostname == NULL)
748 return(-1);
749
750 /*
751 * do the blocking DNS query.
752 */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000753 if (proxy)
754 hp = gethostbyname(proxy);
755 else
756 hp = gethostbyname(ctxt->hostname);
Daniel Veillardda07c342000-01-25 18:31:22 +0000757 if (hp == NULL)
758 return(-1);
759
760 /*
761 * Prepare the socket
762 */
763 memset(&ctxt->ftpAddr, 0, sizeof(ctxt->ftpAddr));
764 ctxt->ftpAddr.sin_family = AF_INET;
765 memcpy(&ctxt->ftpAddr.sin_addr, hp->h_addr_list[0], hp->h_length);
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000766 if (proxy) {
767 port = proxyPort;
768 } else {
769 port = ctxt->port;
770 }
771 if (port == 0)
772 port = 21;
773 ctxt->ftpAddr.sin_port = htons(port);
Daniel Veillardda07c342000-01-25 18:31:22 +0000774 ctxt->controlFd = socket(AF_INET, SOCK_STREAM, 0);
775 if (ctxt->controlFd < 0)
776 return(-1);
777
778 /*
779 * Do the connect.
780 */
781 if (connect(ctxt->controlFd, (struct sockaddr *) &ctxt->ftpAddr,
782 sizeof(struct sockaddr_in)) < 0) {
783 close(ctxt->controlFd); ctxt->controlFd = -1;
784 ctxt->controlFd = -1;
785 return(-1);
786 }
787
788 /*
789 * Wait for the HELLO from the server.
790 */
791 res = xmlNanoFTPGetResponse(ctxt);
792 if (res != 2) {
793 close(ctxt->controlFd); ctxt->controlFd = -1;
794 ctxt->controlFd = -1;
795 return(-1);
796 }
797
798 /*
799 * State diagram for the login operation on the FTP server
800 *
801 * Reference: RFC 959
802 *
803 * 1
804 * +---+ USER +---+------------->+---+
805 * | B |---------->| W | 2 ---->| E |
806 * +---+ +---+------ | -->+---+
807 * | | | | |
808 * 3 | | 4,5 | | |
809 * -------------- ----- | | |
810 * | | | | |
811 * | | | | |
812 * | --------- |
813 * | 1| | | |
814 * V | | | |
815 * +---+ PASS +---+ 2 | ------>+---+
816 * | |---------->| W |------------->| S |
817 * +---+ +---+ ---------->+---+
818 * | | | | |
819 * 3 | |4,5| | |
820 * -------------- -------- |
821 * | | | | |
822 * | | | | |
823 * | -----------
824 * | 1,3| | | |
825 * V | 2| | |
826 * +---+ ACCT +---+-- | ----->+---+
827 * | |---------->| W | 4,5 -------->| F |
828 * +---+ +---+------------->+---+
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000829 *
830 * Of course in case of using a proxy this get really nasty and is not
831 * standardized at all :-(
832 */
833 if (proxy) {
834 int len;
835 char buf[400];
836
837 if (proxyUser != NULL) {
838 /*
839 * We need proxy auth
840 */
Daniel Veillard13c757e2000-02-01 23:59:15 +0000841#ifndef HAVE_SNPRINTF
842 len = sprintf(buf, "USER %s\r\n", proxyUser);
843#else /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000844 len = snprintf(buf, sizeof(buf), "USER %s\r\n", proxyUser);
Daniel Veillard13c757e2000-02-01 23:59:15 +0000845#endif /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000846#ifdef DEBUG_FTP
847 printf(buf);
848#endif
849 res = send(ctxt->controlFd, buf, len, 0);
850 if (res < 0) {
851 close(ctxt->controlFd);
852 ctxt->controlFd = -1;
853 return(res);
854 }
855 res = xmlNanoFTPGetResponse(ctxt);
856 switch (res) {
857 case 2:
858 if (proxyPasswd == NULL)
859 break;
860 case 3:
861 if (proxyPasswd != NULL)
Daniel Veillard13c757e2000-02-01 23:59:15 +0000862#ifndef HAVE_SNPRINTF
863 len = sprintf(buf, "PASS %s\r\n", proxyPasswd);
864#else /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000865 len = snprintf(buf, sizeof(buf), "PASS %s\r\n", proxyPasswd);
Daniel Veillard13c757e2000-02-01 23:59:15 +0000866#endif /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000867 else
Daniel Veillard13c757e2000-02-01 23:59:15 +0000868#ifndef HAVE_SNPRINTF
869 len = sprintf(buf, "PASS libxml@%s\r\n",
Daniel Veillardcf461992000-03-14 18:30:20 +0000870 hostname);
Daniel Veillard13c757e2000-02-01 23:59:15 +0000871#else /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000872 len = snprintf(buf, sizeof(buf), "PASS libxml@%s\r\n",
873 hostname);
Daniel Veillardcf461992000-03-14 18:30:20 +0000874#endif /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000875#ifdef DEBUG_FTP
876 printf(buf);
877#endif
878 res = send(ctxt->controlFd, buf, len, 0);
879 if (res < 0) {
880 close(ctxt->controlFd);
881 ctxt->controlFd = -1;
882 return(res);
883 }
884 res = xmlNanoFTPGetResponse(ctxt);
885 if (res > 3) {
886 close(ctxt->controlFd);
887 ctxt->controlFd = -1;
888 return(-1);
889 }
890 break;
891 case 1:
892 break;
893 case 4:
894 case 5:
895 case -1:
896 default:
897 close(ctxt->controlFd);
898 ctxt->controlFd = -1;
899 return(-1);
900 }
901 }
902
903 /*
904 * We assume we don't need more authentication to the proxy
905 * and that it succeeded :-\
906 */
907 switch (proxyType) {
908 case 0:
909 /* we will try in seqence */
910 case 1:
911 /* Using SITE command */
Daniel Veillard13c757e2000-02-01 23:59:15 +0000912#ifndef HAVE_SNPRINTF
913 len = sprintf(buf, "SITE %s\r\n", ctxt->hostname);
914#else /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000915 len = snprintf(buf, sizeof(buf), "SITE %s\r\n", ctxt->hostname);
Daniel Veillard13c757e2000-02-01 23:59:15 +0000916#endif /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000917#ifdef DEBUG_FTP
918 printf(buf);
919#endif
920 res = send(ctxt->controlFd, buf, len, 0);
921 if (res < 0) {
922 close(ctxt->controlFd); ctxt->controlFd = -1;
923 ctxt->controlFd = -1;
924 return(res);
925 }
926 res = xmlNanoFTPGetResponse(ctxt);
927 if (res == 2) {
928 /* we assume it worked :-\ 1 is error for SITE command */
929 proxyType = 1;
930 break;
931 }
932 if (proxyType == 1) {
933 close(ctxt->controlFd); ctxt->controlFd = -1;
934 ctxt->controlFd = -1;
935 return(-1);
936 }
937 case 2:
938 /* USER user@host command */
939 if (ctxt->user == NULL)
Daniel Veillard13c757e2000-02-01 23:59:15 +0000940#ifndef HAVE_SNPRINTF
941 len = sprintf(buf, "USER anonymous@%s\r\n",
942#else /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000943 len = snprintf(buf, sizeof(buf), "USER anonymous@%s\r\n",
Daniel Veillard13c757e2000-02-01 23:59:15 +0000944#endif /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000945 ctxt->hostname);
946 else
Daniel Veillard13c757e2000-02-01 23:59:15 +0000947#ifndef HAVE_SNPRINTF
948 len = sprintf(buf, "USER %s@%s\r\n",
949#else /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000950 len = snprintf(buf, sizeof(buf), "USER %s@%s\r\n",
Daniel Veillard13c757e2000-02-01 23:59:15 +0000951#endif /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000952 ctxt->user, ctxt->hostname);
953#ifdef DEBUG_FTP
954 printf(buf);
955#endif
956 res = send(ctxt->controlFd, buf, len, 0);
957 if (res < 0) {
958 close(ctxt->controlFd); ctxt->controlFd = -1;
959 ctxt->controlFd = -1;
960 return(res);
961 }
962 res = xmlNanoFTPGetResponse(ctxt);
963 if ((res == 1) || (res == 2)) {
964 /* we assume it worked :-\ */
965 proxyType = 2;
966 return(0);
967 }
968 if (ctxt->passwd == NULL)
Daniel Veillard13c757e2000-02-01 23:59:15 +0000969#ifndef HAVE_SNPRINTF
970 len = sprintf(buf, "PASS libxml@%s\r\n", hostname);
971#else /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000972 len = snprintf(buf, sizeof(buf), "PASS libxml@%s\r\n", hostname);
Daniel Veillard13c757e2000-02-01 23:59:15 +0000973#endif /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000974 else
Daniel Veillard13c757e2000-02-01 23:59:15 +0000975#ifndef HAVE_SNPRINTF
976 len = sprintf(buf, "PASS %s\r\n", ctxt->passwd);
977#else /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000978 len = snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
Daniel Veillard13c757e2000-02-01 23:59:15 +0000979#endif /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000980#ifdef DEBUG_FTP
981 printf(buf);
982#endif
983 res = send(ctxt->controlFd, buf, len, 0);
984 if (res < 0) {
985 close(ctxt->controlFd); ctxt->controlFd = -1;
986 ctxt->controlFd = -1;
987 return(res);
988 }
989 res = xmlNanoFTPGetResponse(ctxt);
990 if ((res == 1) || (res == 2)) {
991 /* we assume it worked :-\ */
992 proxyType = 2;
993 return(0);
994 }
995 if (proxyType == 2) {
996 close(ctxt->controlFd); ctxt->controlFd = -1;
997 ctxt->controlFd = -1;
998 return(-1);
999 }
1000 case 3:
1001 /*
1002 * If you need support for other Proxy authentication scheme
1003 * send the code or at least the sequence in use.
1004 */
1005 default:
1006 close(ctxt->controlFd); ctxt->controlFd = -1;
1007 ctxt->controlFd = -1;
1008 return(-1);
1009 }
1010 }
1011 /*
1012 * Non-proxy handling.
Daniel Veillardda07c342000-01-25 18:31:22 +00001013 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001014 res = xmlNanoFTPSendUser(ctxt);
Daniel Veillardda07c342000-01-25 18:31:22 +00001015 if (res < 0) {
1016 close(ctxt->controlFd); ctxt->controlFd = -1;
1017 ctxt->controlFd = -1;
1018 return(-1);
1019 }
1020 res = xmlNanoFTPGetResponse(ctxt);
1021 switch (res) {
1022 case 2:
1023 return(0);
1024 case 3:
1025 break;
1026 case 1:
1027 case 4:
1028 case 5:
1029 case -1:
1030 default:
1031 close(ctxt->controlFd); ctxt->controlFd = -1;
1032 ctxt->controlFd = -1;
1033 return(-1);
1034 }
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001035 res = xmlNanoFTPSendPasswd(ctxt);
Daniel Veillardda07c342000-01-25 18:31:22 +00001036 if (res < 0) {
1037 close(ctxt->controlFd); ctxt->controlFd = -1;
1038 ctxt->controlFd = -1;
1039 return(-1);
1040 }
1041 res = xmlNanoFTPGetResponse(ctxt);
1042 switch (res) {
1043 case 2:
Daniel Veillard5feb8492000-02-02 17:15:36 +00001044 break;
Daniel Veillardda07c342000-01-25 18:31:22 +00001045 case 3:
1046 fprintf(stderr, "FTP server asking for ACCNT on anonymous\n");
1047 case 1:
1048 case 4:
1049 case 5:
1050 case -1:
1051 default:
1052 close(ctxt->controlFd); ctxt->controlFd = -1;
1053 ctxt->controlFd = -1;
1054 return(-1);
1055 }
1056
1057 return(0);
1058}
1059
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001060/**
1061 * xmlNanoFTPConnectTo:
1062 * @server: an FTP server name
Daniel Veillard06047432000-04-24 11:33:38 +00001063 * @port: the port (use 21 if 0)
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001064 *
1065 * Tries to open a control connection to the given server/port
1066 *
Daniel Veillard06047432000-04-24 11:33:38 +00001067 * Returns an fTP context or NULL if it failed
Daniel Veillardda07c342000-01-25 18:31:22 +00001068 */
1069
Daniel Veillard06047432000-04-24 11:33:38 +00001070void*
Daniel Veillardda07c342000-01-25 18:31:22 +00001071xmlNanoFTPConnectTo(const char *server, int port) {
1072 xmlNanoFTPCtxtPtr ctxt;
1073 int res;
1074
1075 xmlNanoFTPInit();
1076 if (server == NULL)
1077 return(NULL);
1078 ctxt = xmlNanoFTPNewCtxt(NULL);
1079 ctxt->hostname = xmlMemStrdup(server);
1080 if (port != 0)
1081 ctxt->port = port;
1082 res = xmlNanoFTPConnect(ctxt);
1083 if (res < 0) {
1084 xmlNanoFTPFreeCtxt(ctxt);
1085 return(NULL);
1086 }
1087 return(ctxt);
1088}
1089
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001090/**
1091 * xmlNanoFTPGetConnection:
1092 * @ctx: an FTP context
1093 * @directory: a directory on the server
1094 *
1095 * Tries to change the remote directory
1096 *
1097 * Returns -1 incase of error, 1 if CWD worked, 0 if it failed
Daniel Veillardda07c342000-01-25 18:31:22 +00001098 */
1099
1100int
1101xmlNanoFTPCwd(void *ctx, char *directory) {
1102 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1103 char buf[400];
1104 int len;
1105 int res;
1106
1107 /*
1108 * Expected response code for CWD:
1109 *
1110 * CWD
1111 * 250
1112 * 500, 501, 502, 421, 530, 550
1113 */
Daniel Veillard13c757e2000-02-01 23:59:15 +00001114#ifndef HAVE_SNPRINTF
1115 len = sprintf(buf, "CWD %s\r\n", directory);
1116#else /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +00001117 len = snprintf(buf, sizeof(buf), "CWD %s\r\n", directory);
Daniel Veillard13c757e2000-02-01 23:59:15 +00001118#endif /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +00001119#ifdef DEBUG_FTP
1120 printf(buf);
1121#endif
1122 res = send(ctxt->controlFd, buf, len, 0);
1123 if (res < 0) return(res);
1124 res = xmlNanoFTPGetResponse(ctxt);
1125 if (res == 4) {
Daniel Veillardda07c342000-01-25 18:31:22 +00001126 return(-1);
1127 }
1128 if (res == 2) return(1);
1129 if (res == 5) {
1130 return(0);
1131 }
1132 return(0);
1133}
1134
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001135/**
1136 * xmlNanoFTPGetConnection:
1137 * @ctx: an FTP context
1138 *
1139 * Try to open a data connection to the server. Currently only
1140 * passive mode is supported.
1141 *
1142 * Returns -1 incase of error, 0 otherwise
Daniel Veillardda07c342000-01-25 18:31:22 +00001143 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001144
Daniel Veillardda07c342000-01-25 18:31:22 +00001145int
1146xmlNanoFTPGetConnection(void *ctx) {
1147 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1148 char buf[200], *cur;
1149 int len, i;
1150 int res;
1151 unsigned char ad[6], *adp, *portp;
1152 unsigned int temp[6];
1153 struct sockaddr_in dataAddr;
1154 size_t dataAddrLen;
1155
1156 ctxt->dataFd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1157 if (ctxt->dataFd < 0) {
1158 fprintf(stderr, "xmlNanoFTPGetConnection: failed to create socket\n");
1159 }
1160 dataAddrLen = sizeof(dataAddr);
1161 memset(&dataAddr, 0, dataAddrLen);
1162 dataAddr.sin_family = AF_INET;
1163
1164 if (ctxt->passive) {
Daniel Veillard13c757e2000-02-01 23:59:15 +00001165#ifndef HAVE_SNPRINTF
1166 len = sprintf(buf, "PASV\r\n");
1167#else /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +00001168 len = snprintf(buf, sizeof(buf), "PASV\r\n");
Daniel Veillard13c757e2000-02-01 23:59:15 +00001169#endif /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +00001170#ifdef DEBUG_FTP
1171 printf(buf);
1172#endif
1173 res = send(ctxt->controlFd, buf, len, 0);
1174 if (res < 0) {
1175 close(ctxt->dataFd); ctxt->dataFd = -1;
1176 return(res);
1177 }
1178 res = xmlNanoFTPReadResponse(ctx, buf, sizeof(buf) -1);
1179 if (res != 2) {
1180 if (res == 5) {
1181 close(ctxt->dataFd); ctxt->dataFd = -1;
1182 return(-1);
1183 } else {
1184 /*
1185 * retry with an active connection
1186 */
1187 close(ctxt->dataFd); ctxt->dataFd = -1;
1188 ctxt->passive = 0;
1189 }
1190 }
1191 cur = &buf[4];
1192 while (((*cur < '0') || (*cur > '9')) && *cur != '\0') cur++;
1193 if (sscanf(cur, "%d,%d,%d,%d,%d,%d", &temp[0], &temp[1], &temp[2],
1194 &temp[3], &temp[4], &temp[5]) != 6) {
1195 fprintf(stderr, "Invalid answer to PASV\n");
Daniel Veillardbe803962000-06-28 23:40:59 +00001196 if (ctxt->dataFd != -1) {
1197 close(ctxt->dataFd); ctxt->dataFd = -1;
1198 }
Daniel Veillardda07c342000-01-25 18:31:22 +00001199 return(-1);
1200 }
1201 for (i=0; i<6; i++) ad[i] = (unsigned char) (temp[i] & 0xff);
1202 memcpy(&dataAddr.sin_addr, &ad[0], 4);
1203 memcpy(&dataAddr.sin_port, &ad[4], 2);
1204 if (connect(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
1205 fprintf(stderr, "Failed to create a data connection\n");
1206 close(ctxt->dataFd); ctxt->dataFd = -1;
1207 return (-1);
1208 }
1209 } else {
1210 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
1211 dataAddr.sin_port = 0;
1212 if (bind(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
1213 fprintf(stderr, "Failed to bind a port\n");
1214 close(ctxt->dataFd); ctxt->dataFd = -1;
1215 return (-1);
1216 }
1217 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
1218
1219 if (listen(ctxt->dataFd, 1) < 0) {
1220 fprintf(stderr, "Could not listen on port %d\n",
1221 ntohs(dataAddr.sin_port));
1222 close(ctxt->dataFd); ctxt->dataFd = -1;
1223 return (-1);
1224 }
1225 adp = (unsigned char *) &dataAddr.sin_addr;
1226 portp = (unsigned char *) &dataAddr.sin_port;
Daniel Veillard13c757e2000-02-01 23:59:15 +00001227#ifndef HAVE_SNPRINTF
1228 len = sprintf(buf, "PORT %d,%d,%d,%d,%d,%d\r\n",
Daniel Veillardcf461992000-03-14 18:30:20 +00001229 adp[0] & 0xff, adp[1] & 0xff, adp[2] & 0xff, adp[3] & 0xff,
1230 portp[0] & 0xff, portp[1] & 0xff);
Daniel Veillard13c757e2000-02-01 23:59:15 +00001231#else /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +00001232 len = snprintf(buf, sizeof(buf), "PORT %d,%d,%d,%d,%d,%d\r\n",
Daniel Veillardcf461992000-03-14 18:30:20 +00001233 adp[0] & 0xff, adp[1] & 0xff, adp[2] & 0xff, adp[3] & 0xff,
1234 portp[0] & 0xff, portp[1] & 0xff);
Daniel Veillard13c757e2000-02-01 23:59:15 +00001235#endif /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +00001236 buf[sizeof(buf) - 1] = 0;
1237#ifdef DEBUG_FTP
1238 printf(buf);
1239#endif
1240
1241 res = send(ctxt->controlFd, buf, len, 0);
1242 if (res < 0) {
1243 close(ctxt->dataFd); ctxt->dataFd = -1;
1244 return(res);
1245 }
1246 res = xmlNanoFTPGetResponse(ctxt);
1247 if (res != 2) {
1248 close(ctxt->dataFd); ctxt->dataFd = -1;
1249 return(-1);
1250 }
1251 }
1252 return(ctxt->dataFd);
1253
1254}
1255
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001256/**
1257 * xmlNanoFTPCloseConnection:
1258 * @ctx: an FTP context
1259 *
1260 * Close the data connection from the server
1261 *
1262 * Returns -1 incase of error, 0 otherwise
Daniel Veillardda07c342000-01-25 18:31:22 +00001263 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001264
Daniel Veillardda07c342000-01-25 18:31:22 +00001265int
1266xmlNanoFTPCloseConnection(void *ctx) {
1267 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1268 int res;
Daniel Veillardcf461992000-03-14 18:30:20 +00001269 fd_set rfd, efd;
1270 struct timeval tv;
Daniel Veillardda07c342000-01-25 18:31:22 +00001271
1272 close(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardcf461992000-03-14 18:30:20 +00001273 tv.tv_sec = 15;
1274 tv.tv_usec = 0;
1275 FD_ZERO(&rfd);
1276 FD_SET(ctxt->controlFd, &rfd);
1277 FD_ZERO(&efd);
1278 FD_SET(ctxt->controlFd, &efd);
1279 res = select(ctxt->controlFd + 1, &rfd, NULL, &efd, &tv);
1280 if (res < 0) {
1281#ifdef DEBUG_FTP
1282 perror("select");
1283#endif
Daniel Veillardda07c342000-01-25 18:31:22 +00001284 close(ctxt->controlFd); ctxt->controlFd = -1;
1285 return(-1);
1286 }
Daniel Veillardcf461992000-03-14 18:30:20 +00001287 if (res == 0) {
1288 fprintf(stderr, "xmlNanoFTPCloseConnection: timeout\n");
1289 close(ctxt->controlFd); ctxt->controlFd = -1;
1290 } else {
1291 res = xmlNanoFTPGetResponse(ctxt);
1292 if (res != 2) {
1293 close(ctxt->controlFd); ctxt->controlFd = -1;
1294 return(-1);
1295 }
1296 }
Daniel Veillardda07c342000-01-25 18:31:22 +00001297 return(0);
1298}
1299
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001300/**
1301 * xmlNanoFTPParseList:
1302 * @list: some data listing received from the server
1303 * @callback: the user callback
1304 * @userData: the user callback data
1305 *
1306 * Parse at most one entry from the listing.
1307 *
1308 * Returns -1 incase of error, the lenght of data parsed otherwise
Daniel Veillardda07c342000-01-25 18:31:22 +00001309 */
1310
1311static int
1312xmlNanoFTPParseList(const char *list, ftpListCallback callback, void *userData) {
1313 const char *cur = list;
1314 char filename[151];
1315 char attrib[11];
1316 char owner[11];
1317 char group[11];
1318 char month[4];
1319 int year = 0;
1320 int minute = 0;
1321 int hour = 0;
1322 int day = 0;
1323 unsigned long size = 0;
1324 int links = 0;
1325 int i;
1326
1327 if (!strncmp(cur, "total", 5)) {
1328 cur += 5;
1329 while (*cur == ' ') cur++;
1330 while ((*cur >= '0') && (*cur <= '9'))
1331 links = (links * 10) + (*cur++ - '0');
1332 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1333 cur++;
1334 return(cur - list);
1335 } else if (*list == '+') {
1336 return(0);
1337 } else {
1338 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1339 cur++;
1340 if (*cur == 0) return(0);
1341 i = 0;
1342 while (*cur != ' ') {
1343 if (i < 10)
1344 attrib[i++] = *cur;
1345 cur++;
1346 if (*cur == 0) return(0);
1347 }
1348 attrib[10] = 0;
1349 while (*cur == ' ') cur++;
1350 if (*cur == 0) return(0);
1351 while ((*cur >= '0') && (*cur <= '9'))
1352 links = (links * 10) + (*cur++ - '0');
1353 while (*cur == ' ') cur++;
1354 if (*cur == 0) return(0);
1355 i = 0;
1356 while (*cur != ' ') {
1357 if (i < 10)
1358 owner[i++] = *cur;
1359 cur++;
1360 if (*cur == 0) return(0);
1361 }
1362 owner[i] = 0;
1363 while (*cur == ' ') cur++;
1364 if (*cur == 0) return(0);
1365 i = 0;
1366 while (*cur != ' ') {
1367 if (i < 10)
1368 group[i++] = *cur;
1369 cur++;
1370 if (*cur == 0) return(0);
1371 }
1372 group[i] = 0;
1373 while (*cur == ' ') cur++;
1374 if (*cur == 0) return(0);
1375 while ((*cur >= '0') && (*cur <= '9'))
1376 size = (size * 10) + (*cur++ - '0');
1377 while (*cur == ' ') cur++;
1378 if (*cur == 0) return(0);
1379 i = 0;
1380 while (*cur != ' ') {
1381 if (i < 3)
1382 month[i++] = *cur;
1383 cur++;
1384 if (*cur == 0) return(0);
1385 }
1386 month[i] = 0;
1387 while (*cur == ' ') cur++;
1388 if (*cur == 0) return(0);
1389 while ((*cur >= '0') && (*cur <= '9'))
1390 day = (day * 10) + (*cur++ - '0');
1391 while (*cur == ' ') cur++;
1392 if (*cur == 0) return(0);
1393 if ((cur[1] == 0) || (cur[2] == 0)) return(0);
1394 if ((cur[1] == ':') || (cur[2] == ':')) {
1395 while ((*cur >= '0') && (*cur <= '9'))
1396 hour = (hour * 10) + (*cur++ - '0');
1397 if (*cur == ':') cur++;
1398 while ((*cur >= '0') && (*cur <= '9'))
1399 minute = (minute * 10) + (*cur++ - '0');
1400 } else {
1401 while ((*cur >= '0') && (*cur <= '9'))
1402 year = (year * 10) + (*cur++ - '0');
1403 }
1404 while (*cur == ' ') cur++;
1405 if (*cur == 0) return(0);
1406 i = 0;
1407 while ((*cur != '\n') && (*cur != '\r')) {
1408 if (i < 150)
1409 filename[i++] = *cur;
1410 cur++;
1411 if (*cur == 0) return(0);
1412 }
1413 filename[i] = 0;
1414 if ((*cur != '\n') && (*cur != '\r'))
1415 return(0);
1416 while ((*cur == '\n') || (*cur == '\r'))
1417 cur++;
1418 }
1419 if (callback != NULL) {
1420 callback(userData, filename, attrib, owner, group, size, links,
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001421 year, month, day, hour, minute);
Daniel Veillardda07c342000-01-25 18:31:22 +00001422 }
1423 return(cur - list);
1424}
1425
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001426/**
1427 * xmlNanoFTPList:
1428 * @ctx: an FTP context
1429 * @callback: the user callback
1430 * @userData: the user callback data
1431 * @filename: optional files to list
1432 *
1433 * Do a listing on the server. All files info are passed back
1434 * in the callbacks.
1435 *
1436 * Returns -1 incase of error, 0 otherwise
Daniel Veillardda07c342000-01-25 18:31:22 +00001437 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001438
Daniel Veillardda07c342000-01-25 18:31:22 +00001439int
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001440xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData,
1441 char *filename) {
Daniel Veillardda07c342000-01-25 18:31:22 +00001442 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1443 char buf[4096 + 1];
1444 int len, res;
1445 int index = 0, base;
1446 fd_set rfd, efd;
1447 struct timeval tv;
1448
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001449 if (filename == NULL) {
1450 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1451 return(-1);
1452 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
Daniel Veillardbe803962000-06-28 23:40:59 +00001453 if (ctxt->dataFd == -1)
1454 return(-1);
Daniel Veillard13c757e2000-02-01 23:59:15 +00001455#ifndef HAVE_SNPRINTF
1456 len = sprintf(buf, "LIST -L\r\n");
1457#else /* HAVE_SNPRINTF */
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001458 len = snprintf(buf, sizeof(buf), "LIST -L\r\n");
Daniel Veillard13c757e2000-02-01 23:59:15 +00001459#endif /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001460 } else {
1461 if (filename[0] != '/') {
1462 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1463 return(-1);
1464 }
1465 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
Daniel Veillardbe803962000-06-28 23:40:59 +00001466 if (ctxt->dataFd == -1)
1467 return(-1);
Daniel Veillard13c757e2000-02-01 23:59:15 +00001468#ifndef HAVE_SNPRINTF
1469 len = sprintf(buf, "LIST -L %s\r\n", filename);
1470#else /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001471 len = snprintf(buf, sizeof(buf), "LIST -L %s\r\n", filename);
Daniel Veillard13c757e2000-02-01 23:59:15 +00001472#endif /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001473 }
Daniel Veillardda07c342000-01-25 18:31:22 +00001474#ifdef DEBUG_FTP
1475 printf(buf);
1476#endif
1477 res = send(ctxt->controlFd, buf, len, 0);
1478 if (res < 0) {
1479 close(ctxt->dataFd); ctxt->dataFd = -1;
1480 return(res);
1481 }
1482 res = xmlNanoFTPReadResponse(ctxt, buf, sizeof(buf) -1);
1483 if (res != 1) {
1484 close(ctxt->dataFd); ctxt->dataFd = -1;
1485 return(-res);
1486 }
1487
1488 do {
1489 tv.tv_sec = 1;
1490 tv.tv_usec = 0;
1491 FD_ZERO(&rfd);
1492 FD_SET(ctxt->dataFd, &rfd);
1493 FD_ZERO(&efd);
1494 FD_SET(ctxt->dataFd, &efd);
1495 res = select(ctxt->dataFd + 1, &rfd, NULL, &efd, &tv);
1496 if (res < 0) {
1497#ifdef DEBUG_FTP
1498 perror("select");
1499#endif
1500 close(ctxt->dataFd); ctxt->dataFd = -1;
1501 return(-1);
1502 }
1503 if (res == 0) {
1504 res = xmlNanoFTPCheckResponse(ctxt);
1505 if (res < 0) {
1506 close(ctxt->dataFd); ctxt->dataFd = -1;
1507 ctxt->dataFd = -1;
1508 return(-1);
1509 }
1510 if (res == 2) {
1511 close(ctxt->dataFd); ctxt->dataFd = -1;
1512 return(0);
1513 }
1514
1515 continue;
1516 }
1517
1518 if ((len = read(ctxt->dataFd, &buf[index], sizeof(buf) - (index + 1))) < 0) {
1519#ifdef DEBUG_FTP
1520 perror("read");
1521#endif
1522 close(ctxt->dataFd); ctxt->dataFd = -1;
1523 ctxt->dataFd = -1;
1524 return(-1);
1525 }
1526#ifdef DEBUG_FTP
1527 write(1, &buf[index], len);
1528#endif
1529 index += len;
1530 buf[index] = 0;
1531 base = 0;
1532 do {
1533 res = xmlNanoFTPParseList(&buf[base], callback, userData);
1534 base += res;
1535 } while (res > 0);
1536
1537 memmove(&buf[0], &buf[base], index - base);
1538 index -= base;
1539 } while (len != 0);
1540 xmlNanoFTPCloseConnection(ctxt);
1541 return(0);
1542}
1543
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001544/**
Daniel Veillardda07c342000-01-25 18:31:22 +00001545 * xmlNanoFTPGetSocket:
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001546 * @ctx: an FTP context
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001547 * @filename: the file to retrieve (or NULL if path is in context).
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001548 *
1549 * Initiate fetch of the given file from the server.
1550 *
1551 * Returns the socket for the data connection, or <0 in case of error
Daniel Veillardda07c342000-01-25 18:31:22 +00001552 */
1553
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001554
Daniel Veillardda07c342000-01-25 18:31:22 +00001555int
1556xmlNanoFTPGetSocket(void *ctx, const char *filename) {
1557 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1558 char buf[300];
1559 int res, len;
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001560 if ((filename == NULL) && (ctxt->path == NULL))
Daniel Veillardda07c342000-01-25 18:31:22 +00001561 return(-1);
1562 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
Daniel Veillardbe803962000-06-28 23:40:59 +00001563 if (ctxt->dataFd == -1)
1564 return(-1);
Daniel Veillardda07c342000-01-25 18:31:22 +00001565
Daniel Veillard13c757e2000-02-01 23:59:15 +00001566#ifndef HAVE_SNPRINTF
1567 len = sprintf(buf, "TYPE I\r\n");
1568#else /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +00001569 len = snprintf(buf, sizeof(buf), "TYPE I\r\n");
Daniel Veillard13c757e2000-02-01 23:59:15 +00001570#endif /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +00001571#ifdef DEBUG_FTP
1572 printf(buf);
1573#endif
1574 res = send(ctxt->controlFd, buf, len, 0);
1575 if (res < 0) {
1576 close(ctxt->dataFd); ctxt->dataFd = -1;
1577 return(res);
1578 }
1579 res = xmlNanoFTPReadResponse(ctxt, buf, sizeof(buf) -1);
1580 if (res != 2) {
1581 close(ctxt->dataFd); ctxt->dataFd = -1;
1582 return(-res);
1583 }
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001584 if (filename == NULL)
Daniel Veillard13c757e2000-02-01 23:59:15 +00001585#ifndef HAVE_SNPRINTF
1586 len = sprintf(buf, "RETR %s\r\n", ctxt->path);
1587#else /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001588 len = snprintf(buf, sizeof(buf), "RETR %s\r\n", ctxt->path);
Daniel Veillard13c757e2000-02-01 23:59:15 +00001589#endif /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001590 else
Daniel Veillard13c757e2000-02-01 23:59:15 +00001591#ifndef HAVE_SNPRINTF
1592 len = sprintf(buf, "RETR %s\r\n", filename);
1593#else /* HAVE_SNPRINTF */
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001594 len = snprintf(buf, sizeof(buf), "RETR %s\r\n", filename);
Daniel Veillard13c757e2000-02-01 23:59:15 +00001595#endif /* HAVE_SNPRINTF */
Daniel Veillardda07c342000-01-25 18:31:22 +00001596#ifdef DEBUG_FTP
1597 printf(buf);
1598#endif
1599 res = send(ctxt->controlFd, buf, len, 0);
1600 if (res < 0) {
1601 close(ctxt->dataFd); ctxt->dataFd = -1;
1602 return(res);
1603 }
1604 res = xmlNanoFTPReadResponse(ctxt, buf, sizeof(buf) -1);
1605 if (res != 1) {
1606 close(ctxt->dataFd); ctxt->dataFd = -1;
1607 return(-res);
1608 }
1609 return(ctxt->dataFd);
1610}
1611
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001612/**
1613 * xmlNanoFTPGet:
1614 * @ctx: an FTP context
1615 * @callback: the user callback
1616 * @userData: the user callback data
1617 * @filename: the file to retrieve
1618 *
1619 * Fetch the given file from the server. All data are passed back
1620 * in the callbacks. The last callback has a size of 0 block.
1621 *
1622 * Returns -1 incase of error, 0 otherwise
Daniel Veillardda07c342000-01-25 18:31:22 +00001623 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001624
Daniel Veillardda07c342000-01-25 18:31:22 +00001625int
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001626xmlNanoFTPGet(void *ctx, ftpDataCallback callback, void *userData,
1627 const char *filename) {
Daniel Veillardda07c342000-01-25 18:31:22 +00001628 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1629 char buf[4096];
1630 int len = 0, res;
1631 fd_set rfd;
1632 struct timeval tv;
1633
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001634 if ((filename == NULL) && (ctxt->path == NULL))
Daniel Veillardda07c342000-01-25 18:31:22 +00001635 return(-1);
1636 if (callback == NULL)
1637 return(-1);
1638 if (xmlNanoFTPGetSocket(ctxt, filename) < 0)
1639 return(-1);
1640
1641 do {
1642 tv.tv_sec = 1;
1643 tv.tv_usec = 0;
1644 FD_ZERO(&rfd);
1645 FD_SET(ctxt->dataFd, &rfd);
1646 res = select(ctxt->dataFd + 1, &rfd, NULL, NULL, &tv);
1647 if (res < 0) {
1648#ifdef DEBUG_FTP
1649 perror("select");
1650#endif
1651 close(ctxt->dataFd); ctxt->dataFd = -1;
1652 return(-1);
1653 }
1654 if (res == 0) {
1655 res = xmlNanoFTPCheckResponse(ctxt);
1656 if (res < 0) {
1657 close(ctxt->dataFd); ctxt->dataFd = -1;
1658 ctxt->dataFd = -1;
1659 return(-1);
1660 }
1661 if (res == 2) {
1662 close(ctxt->dataFd); ctxt->dataFd = -1;
1663 return(0);
1664 }
1665
1666 continue;
1667 }
1668 if ((len = read(ctxt->dataFd, &buf, sizeof(buf))) < 0) {
1669 callback(userData, buf, len);
1670 close(ctxt->dataFd); ctxt->dataFd = -1;
1671 return(-1);
1672 }
1673 callback(userData, buf, len);
1674 } while (len != 0);
1675
1676 return(xmlNanoFTPCloseConnection(ctxt));
1677}
1678
1679/**
1680 * xmlNanoFTPRead:
1681 * @ctx: the FTP context
1682 * @dest: a buffer
1683 * @len: the buffer length
1684 *
1685 * This function tries to read @len bytes from the existing FTP connection
1686 * and saves them in @dest. This is a blocking call.
1687 *
1688 * Returns the number of byte read. 0 is an indication of an end of connection.
1689 * -1 indicates a parameter error.
1690 */
1691int
1692xmlNanoFTPRead(void *ctx, void *dest, int len) {
1693 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1694
1695 if (ctx == NULL) return(-1);
1696 if (ctxt->dataFd < 0) return(0);
1697 if (dest == NULL) return(-1);
1698 if (len <= 0) return(0);
1699
1700 len = read(ctxt->dataFd, dest, len);
1701#ifdef DEBUG_FTP
1702 printf("Read %d bytes\n", len);
1703#endif
1704 if (len <= 0) {
1705 xmlNanoFTPCloseConnection(ctxt);
1706 }
1707 return(len);
1708}
1709
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001710/**
Daniel Veillardda07c342000-01-25 18:31:22 +00001711 * xmlNanoFTPOpen:
1712 * @URL: the URL to the resource
1713 *
1714 * Start to fetch the given ftp:// resource
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001715 *
1716 * Returns an FTP context, or NULL
Daniel Veillardda07c342000-01-25 18:31:22 +00001717 */
1718
Daniel Veillard06047432000-04-24 11:33:38 +00001719void*
Daniel Veillardda07c342000-01-25 18:31:22 +00001720xmlNanoFTPOpen(const char *URL) {
1721 xmlNanoFTPCtxtPtr ctxt;
1722 int sock;
1723
1724 xmlNanoFTPInit();
1725 if (URL == NULL) return(NULL);
1726 if (strncmp("ftp://", URL, 6)) return(NULL);
1727
1728 ctxt = xmlNanoFTPNewCtxt(URL);
1729 if (ctxt == NULL) return(NULL);
1730 if (xmlNanoFTPConnect(ctxt) < 0) {
1731 xmlNanoFTPFreeCtxt(ctxt);
1732 return(NULL);
1733 }
1734 sock = xmlNanoFTPGetSocket(ctxt, ctxt->path);
1735 if (sock < 0) {
1736 xmlNanoFTPFreeCtxt(ctxt);
1737 return(NULL);
1738 }
1739 return(ctxt);
1740}
1741
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001742/**
1743 * xmlNanoFTPClose:
1744 * @ctx: an FTP context
1745 *
1746 * Close the connection and both control and transport
1747 *
1748 * Returns -1 incase of error, 0 otherwise
Daniel Veillardda07c342000-01-25 18:31:22 +00001749 */
1750
1751int
1752xmlNanoFTPClose(void *ctx) {
1753 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1754
1755 if (ctxt == NULL)
1756 return(-1);
1757
1758 if (ctxt->dataFd >= 0) {
1759 close(ctxt->dataFd);
1760 ctxt->dataFd = -1;
1761 }
1762 if (ctxt->controlFd >= 0) {
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001763 xmlNanoFTPQuit(ctxt);
Daniel Veillardda07c342000-01-25 18:31:22 +00001764 close(ctxt->controlFd);
1765 ctxt->controlFd = -1;
1766 }
1767 xmlNanoFTPFreeCtxt(ctxt);
1768 return(0);
1769}
1770
1771#ifdef STANDALONE
1772/************************************************************************
1773 * *
1774 * Basic test in Standalone mode *
1775 * *
1776 ************************************************************************/
1777void ftpList(void *userData, const char *filename, const char* attrib,
1778 const char *owner, const char *group, unsigned long size, int links,
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001779 int year, const char *month, int day, int hour, int minute) {
Daniel Veillardda07c342000-01-25 18:31:22 +00001780 printf("%s %s %s %ld %s\n", attrib, owner, group, size, filename);
1781}
1782void ftpData(void *userData, const char *data, int len) {
1783 if (userData == NULL) return;
1784 if (len <= 0) {
1785 fclose(userData);
1786 return;
1787 }
1788 fwrite(data, len, 1, userData);
1789}
1790
1791int main(int argc, char **argv) {
1792 void *ctxt;
1793 FILE *output;
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001794 char *tstfile = NULL;
Daniel Veillardda07c342000-01-25 18:31:22 +00001795
1796 xmlNanoFTPInit();
1797 if (argc > 1) {
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001798 ctxt = xmlNanoFTPNewCtxt(argv[1]);
1799 if (xmlNanoFTPConnect(ctxt) < 0) {
1800 fprintf(stderr, "Couldn't connect to %s\n", argv[1]);
1801 exit(1);
1802 }
Daniel Veillardda07c342000-01-25 18:31:22 +00001803 if (argc > 2)
1804 tstfile = argv[2];
1805 } else
1806 ctxt = xmlNanoFTPConnectTo("localhost", 0);
1807 if (ctxt == NULL) {
1808 fprintf(stderr, "Couldn't connect to localhost\n");
1809 exit(1);
1810 }
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001811 xmlNanoFTPList(ctxt, ftpList, NULL, tstfile);
Daniel Veillardda07c342000-01-25 18:31:22 +00001812 output = fopen("/tmp/tstdata", "w");
1813 if (output != NULL) {
1814 if (xmlNanoFTPGet(ctxt, ftpData, (void *) output, tstfile) < 0)
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001815 fprintf(stderr, "Failed to get file\n");
Daniel Veillardda07c342000-01-25 18:31:22 +00001816
1817 }
1818 xmlNanoFTPClose(ctxt);
1819 xmlMemoryDump();
1820 exit(0);
1821}
1822#endif /* STANDALONE */
Daniel Veillard361d8452000-04-03 19:48:13 +00001823#else /* !LIBXML_FTP_ENABLED */
1824#ifdef STANDALONE
1825#include <stdio.h>
1826int main(int argc, char **argv) {
1827 printf("%s : FTP support not compiled in\n", argv[0]);
1828 return(0);
1829}
1830#endif /* STANDALONE */
1831#endif /* LIBXML_FTP_ENABLED */