blob: 8df6f29d0f45021fab356e33c243023624064d9b [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * nanoftp.c: basic FTP client support
3 *
4 * Reference: RFC 959
5 */
6
7#ifdef TESTING
8#define STANDALONE
9#define HAVE_STDLIB_H
10#define HAVE_UNISTD_H
11#define HAVE_SYS_SOCKET_H
12#define HAVE_NETINET_IN_H
13#define HAVE_NETDB_H
14#define HAVE_SYS_TIME_H
Daniel Veillardcbaf3992001-12-31 16:16:02 +000015#else /* TESTING */
Daniel Veillardf3afa7d2001-06-09 13:52:58 +000016#define NEED_SOCKETS
Daniel Veillardcbaf3992001-12-31 16:16:02 +000017#endif /* TESTING */
Owen Taylor3473f882001-02-23 17:55:21 +000018
Daniel Veillard34ce8be2002-03-18 19:37:11 +000019#define IN_LIBXML
Daniel Veillard5e2dace2001-07-18 19:30:27 +000020#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000021
22#ifdef LIBXML_FTP_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000023#include <string.h>
24
25#ifdef HAVE_STDLIB_H
26#include <stdlib.h>
27#endif
28#ifdef HAVE_UNISTD_H
29#include <unistd.h>
30#endif
31#ifdef HAVE_SYS_SOCKET_H
32#include <sys/socket.h>
33#endif
34#ifdef HAVE_NETINET_IN_H
35#include <netinet/in.h>
36#endif
37#ifdef HAVE_ARPA_INET_H
38#include <arpa/inet.h>
39#endif
40#ifdef HAVE_NETDB_H
41#include <netdb.h>
42#endif
43#ifdef HAVE_FCNTL_H
44#include <fcntl.h>
45#endif
46#ifdef HAVE_ERRNO_H
47#include <errno.h>
48#endif
49#ifdef HAVE_SYS_TIME_H
50#include <sys/time.h>
51#endif
52#ifdef HAVE_SYS_SELECT_H
53#include <sys/select.h>
54#endif
55#ifdef HAVE_STRINGS_H
56#include <strings.h>
57#endif
58
59#include <libxml/xmlmemory.h>
Daniel Veillardd0463562001-10-13 09:15:48 +000060#include <libxml/parser.h>
Owen Taylor3473f882001-02-23 17:55:21 +000061#include <libxml/xmlerror.h>
Daniel Veillardcacbe5d2003-01-10 16:09:51 +000062#include <libxml/uri.h>
Daniel Veillardd0463562001-10-13 09:15:48 +000063#include <libxml/nanoftp.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000064#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000065
66/* #define DEBUG_FTP 1 */
67#ifdef STANDALONE
68#ifndef DEBUG_FTP
69#define DEBUG_FTP 1
70#endif
71#endif
72
73/**
74 * A couple portability macros
75 */
76#ifndef _WINSOCKAPI_
77#define closesocket(s) close(s)
78#define SOCKET int
79#endif
Daniel Veillardacf7ff02001-10-29 20:21:47 +000080#if defined(VMS) || defined(__VMS)
81#define SOCKLEN_T unsigned int
82#endif
Owen Taylor3473f882001-02-23 17:55:21 +000083
Owen Taylor3473f882001-02-23 17:55:21 +000084#define FTP_COMMAND_OK 200
85#define FTP_SYNTAX_ERROR 500
86#define FTP_GET_PASSWD 331
87#define FTP_BUF_SIZE 512
88
89typedef struct xmlNanoFTPCtxt {
90 char *protocol; /* the protocol name */
91 char *hostname; /* the host name */
92 int port; /* the port */
93 char *path; /* the path within the URL */
94 char *user; /* user string */
95 char *passwd; /* passwd string */
96 struct sockaddr_in ftpAddr; /* the socket address struct */
97 int passive; /* currently we support only passive !!! */
98 SOCKET controlFd; /* the file descriptor for the control socket */
99 SOCKET dataFd; /* the file descriptor for the data socket */
100 int state; /* WRITE / READ / CLOSED */
101 int returnValue; /* the protocol return value */
102 /* buffer for data received from the control connection */
103 char controlBuf[FTP_BUF_SIZE + 1];
104 int controlBufIndex;
105 int controlBufUsed;
106 int controlBufAnswer;
107} xmlNanoFTPCtxt, *xmlNanoFTPCtxtPtr;
108
109static int initialized = 0;
110static char *proxy = NULL; /* the proxy name if any */
111static int proxyPort = 0; /* the proxy port if any */
112static char *proxyUser = NULL; /* user for proxy authentication */
113static char *proxyPasswd = NULL;/* passwd for proxy authentication */
114static int proxyType = 0; /* uses TYPE or a@b ? */
115
116/**
117 * xmlNanoFTPInit:
118 *
119 * Initialize the FTP protocol layer.
120 * Currently it just checks for proxy informations,
121 * and get the hostname
122 */
123
124void
125xmlNanoFTPInit(void) {
126 const char *env;
127#ifdef _WINSOCKAPI_
128 WSADATA wsaData;
129#endif
130
131 if (initialized)
132 return;
133
134#ifdef _WINSOCKAPI_
135 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
136 return;
137#endif
138
Owen Taylor3473f882001-02-23 17:55:21 +0000139 proxyPort = 21;
140 env = getenv("no_proxy");
141 if (env != NULL)
142 return;
143 env = getenv("ftp_proxy");
144 if (env != NULL) {
145 xmlNanoFTPScanProxy(env);
146 } else {
147 env = getenv("FTP_PROXY");
148 if (env != NULL) {
149 xmlNanoFTPScanProxy(env);
150 }
151 }
152 env = getenv("ftp_proxy_user");
153 if (env != NULL) {
154 proxyUser = xmlMemStrdup(env);
155 }
156 env = getenv("ftp_proxy_password");
157 if (env != NULL) {
158 proxyPasswd = xmlMemStrdup(env);
159 }
160 initialized = 1;
161}
162
163/**
Daniel Veillarde356c282001-03-10 12:32:04 +0000164 * xmlNanoFTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000165 *
166 * Cleanup the FTP protocol layer. This cleanup proxy informations.
167 */
168
169void
170xmlNanoFTPCleanup(void) {
171 if (proxy != NULL) {
172 xmlFree(proxy);
173 proxy = NULL;
174 }
175 if (proxyUser != NULL) {
176 xmlFree(proxyUser);
177 proxyUser = NULL;
178 }
179 if (proxyPasswd != NULL) {
180 xmlFree(proxyPasswd);
181 proxyPasswd = NULL;
182 }
Owen Taylor3473f882001-02-23 17:55:21 +0000183#ifdef _WINSOCKAPI_
184 if (initialized)
185 WSACleanup();
186#endif
187 initialized = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000188}
189
190/**
191 * xmlNanoFTPProxy:
192 * @host: the proxy host name
193 * @port: the proxy port
194 * @user: the proxy user name
195 * @passwd: the proxy password
196 * @type: the type of proxy 1 for using SITE, 2 for USER a@b
197 *
198 * Setup the FTP proxy informations.
199 * This can also be done by using ftp_proxy ftp_proxy_user and
200 * ftp_proxy_password environment variables.
201 */
202
203void
204xmlNanoFTPProxy(const char *host, int port, const char *user,
205 const char *passwd, int type) {
206 if (proxy != NULL)
207 xmlFree(proxy);
208 if (proxyUser != NULL)
209 xmlFree(proxyUser);
210 if (proxyPasswd != NULL)
211 xmlFree(proxyPasswd);
212 if (host)
213 proxy = xmlMemStrdup(host);
214 if (user)
215 proxyUser = xmlMemStrdup(user);
216 if (passwd)
217 proxyPasswd = xmlMemStrdup(passwd);
218 proxyPort = port;
219 proxyType = type;
220}
221
222/**
223 * xmlNanoFTPScanURL:
224 * @ctx: an FTP context
225 * @URL: The URL used to initialize the context
226 *
227 * (Re)Initialize an FTP context by parsing the URL and finding
228 * the protocol host port and path it indicates.
229 */
230
231static void
232xmlNanoFTPScanURL(void *ctx, const char *URL) {
233 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
234 const char *cur = URL;
235 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000236 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000237 int port = 0;
238
239 if (ctxt->protocol != NULL) {
240 xmlFree(ctxt->protocol);
241 ctxt->protocol = NULL;
242 }
243 if (ctxt->hostname != NULL) {
244 xmlFree(ctxt->hostname);
245 ctxt->hostname = NULL;
246 }
247 if (ctxt->path != NULL) {
248 xmlFree(ctxt->path);
249 ctxt->path = NULL;
250 }
251 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000252 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000253 while (*cur != 0) {
254 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000255 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000256 ctxt->protocol = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000257 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000258 cur += 3;
259 break;
260 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000261 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000262 }
263 if (*cur == 0) return;
264
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000265 buf[indx] = 0;
Daniel Veillardc69e0b12001-11-20 08:35:07 +0000266 /* allow user@ and user:pass@ forms */
267 {
268 const char *p = strchr(cur, '@');
269 if(p) {
270 while(1) {
271 if(cur[0] == ':' || cur[0] == '@') break;
272 buf[indx++] = *cur++;
273 }
274 buf[indx] = 0;
275 ctxt->user = xmlMemStrdup(buf);
276 indx = 0;
277 if(cur[0] == ':') {
278 cur++;
279 while(1) {
280 if(cur[0] == '@') break;
281 buf[indx++] = *cur++;
282 }
283 buf[indx] = 0;
284 ctxt->passwd = xmlMemStrdup(buf);
285 indx = 0;
286 }
287 cur = p+1;
288 }
289 }
290
Owen Taylor3473f882001-02-23 17:55:21 +0000291 while (1) {
292 if (cur[0] == ':') {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000293 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000294 ctxt->hostname = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000295 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000296 cur += 1;
297 while ((*cur >= '0') && (*cur <= '9')) {
298 port *= 10;
299 port += *cur - '0';
300 cur++;
301 }
302 if (port != 0) ctxt->port = port;
303 while ((cur[0] != '/') && (*cur != 0))
304 cur++;
305 break;
306 }
307 if ((*cur == '/') || (*cur == 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000308 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000309 ctxt->hostname = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000310 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000311 break;
312 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000313 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000314 }
315 if (*cur == 0)
316 ctxt->path = xmlMemStrdup("/");
317 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000318 indx = 0;
319 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000320 while (*cur != 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000321 buf[indx++] = *cur++;
322 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000323 ctxt->path = xmlMemStrdup(buf);
324 }
325}
326
327/**
328 * xmlNanoFTPUpdateURL:
329 * @ctx: an FTP context
330 * @URL: The URL used to update the context
331 *
332 * Update an FTP context by parsing the URL and finding
333 * new path it indicates. If there is an error in the
334 * protocol, hostname, port or other information, the
335 * error is raised. It indicates a new connection has to
336 * be established.
337 *
338 * Returns 0 if Ok, -1 in case of error (other host).
339 */
340
341int
342xmlNanoFTPUpdateURL(void *ctx, const char *URL) {
343 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
344 const char *cur = URL;
345 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000346 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000347 int port = 0;
348
349 if (URL == NULL)
350 return(-1);
351 if (ctxt == NULL)
352 return(-1);
353 if (ctxt->protocol == NULL)
354 return(-1);
355 if (ctxt->hostname == NULL)
356 return(-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000357 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000358 while (*cur != 0) {
359 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000360 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000361 if (strcmp(ctxt->protocol, buf))
362 return(-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000363 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000364 cur += 3;
365 break;
366 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000367 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000368 }
369 if (*cur == 0)
370 return(-1);
371
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000372 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000373 while (1) {
374 if (cur[0] == ':') {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000375 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000376 if (strcmp(ctxt->hostname, buf))
377 return(-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000378 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000379 cur += 1;
380 while ((*cur >= '0') && (*cur <= '9')) {
381 port *= 10;
382 port += *cur - '0';
383 cur++;
384 }
385 if (port != ctxt->port)
386 return(-1);
387 while ((cur[0] != '/') && (*cur != 0))
388 cur++;
389 break;
390 }
391 if ((*cur == '/') || (*cur == 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000392 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000393 if (strcmp(ctxt->hostname, buf))
394 return(-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000395 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000396 break;
397 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000398 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000399 }
400 if (ctxt->path != NULL) {
401 xmlFree(ctxt->path);
402 ctxt->path = NULL;
403 }
404
405 if (*cur == 0)
406 ctxt->path = xmlMemStrdup("/");
407 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000408 indx = 0;
409 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000410 while (*cur != 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000411 buf[indx++] = *cur++;
412 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000413 ctxt->path = xmlMemStrdup(buf);
414 }
415 return(0);
416}
417
418/**
419 * xmlNanoFTPScanProxy:
420 * @URL: The proxy URL used to initialize the proxy context
421 *
422 * (Re)Initialize the FTP Proxy context by parsing the URL and finding
423 * the protocol host port it indicates.
424 * Should be like ftp://myproxy/ or ftp://myproxy:3128/
425 * A NULL URL cleans up proxy informations.
426 */
427
428void
429xmlNanoFTPScanProxy(const char *URL) {
430 const char *cur = URL;
431 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000432 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000433 int port = 0;
434
435 if (proxy != NULL) {
436 xmlFree(proxy);
437 proxy = NULL;
438 }
439 if (proxyPort != 0) {
440 proxyPort = 0;
441 }
442#ifdef DEBUG_FTP
443 if (URL == NULL)
444 xmlGenericError(xmlGenericErrorContext, "Removing FTP proxy info\n");
445 else
446 xmlGenericError(xmlGenericErrorContext, "Using FTP proxy %s\n", URL);
447#endif
448 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000449 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000450 while (*cur != 0) {
451 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000452 buf[indx] = 0;
453 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000454 cur += 3;
455 break;
456 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000457 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000458 }
459 if (*cur == 0) return;
460
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000461 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000462 while (1) {
463 if (cur[0] == ':') {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000464 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000465 proxy = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000466 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000467 cur += 1;
468 while ((*cur >= '0') && (*cur <= '9')) {
469 port *= 10;
470 port += *cur - '0';
471 cur++;
472 }
473 if (port != 0) proxyPort = port;
474 while ((cur[0] != '/') && (*cur != 0))
475 cur++;
476 break;
477 }
478 if ((*cur == '/') || (*cur == 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000479 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000480 proxy = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000481 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000482 break;
483 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000484 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000485 }
486}
487
488/**
489 * xmlNanoFTPNewCtxt:
490 * @URL: The URL used to initialize the context
491 *
492 * Allocate and initialize a new FTP context.
493 *
494 * Returns an FTP context or NULL in case of error.
495 */
496
497void*
498xmlNanoFTPNewCtxt(const char *URL) {
499 xmlNanoFTPCtxtPtr ret;
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000500 char *unescaped;
Owen Taylor3473f882001-02-23 17:55:21 +0000501
502 ret = (xmlNanoFTPCtxtPtr) xmlMalloc(sizeof(xmlNanoFTPCtxt));
503 if (ret == NULL) return(NULL);
504
505 memset(ret, 0, sizeof(xmlNanoFTPCtxt));
506 ret->port = 21;
507 ret->passive = 1;
508 ret->returnValue = 0;
509 ret->controlBufIndex = 0;
510 ret->controlBufUsed = 0;
Daniel Veillardc69e0b12001-11-20 08:35:07 +0000511 ret->controlFd = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000512
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000513 unescaped = xmlURIUnescapeString(URL, 0, NULL);
514 if (unescaped != NULL)
515 xmlNanoFTPScanURL(ret, unescaped);
516 else if (URL != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +0000517 xmlNanoFTPScanURL(ret, URL);
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000518 xmlFree(unescaped);
Owen Taylor3473f882001-02-23 17:55:21 +0000519
520 return(ret);
521}
522
523/**
524 * xmlNanoFTPFreeCtxt:
525 * @ctx: an FTP context
526 *
527 * Frees the context after closing the connection.
528 */
529
530void
531xmlNanoFTPFreeCtxt(void * ctx) {
532 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
533 if (ctxt == NULL) return;
534 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
535 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
536 if (ctxt->path != NULL) xmlFree(ctxt->path);
537 ctxt->passive = 1;
538 if (ctxt->controlFd >= 0) closesocket(ctxt->controlFd);
539 ctxt->controlFd = -1;
540 ctxt->controlBufIndex = -1;
541 ctxt->controlBufUsed = -1;
542 xmlFree(ctxt);
543}
544
545/**
546 * xmlNanoFTPParseResponse:
Owen Taylor3473f882001-02-23 17:55:21 +0000547 * @buf: the buffer containing the response
548 * @len: the buffer length
549 *
550 * Parsing of the server answer, we just extract the code.
551 *
552 * returns 0 for errors
553 * +XXX for last line of response
554 * -XXX for response to be continued
555 */
556static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000557xmlNanoFTPParseResponse(char *buf, int len) {
Owen Taylor3473f882001-02-23 17:55:21 +0000558 int val = 0;
559
560 if (len < 3) return(-1);
561 if ((*buf >= '0') && (*buf <= '9'))
562 val = val * 10 + (*buf - '0');
563 else
564 return(0);
565 buf++;
566 if ((*buf >= '0') && (*buf <= '9'))
567 val = val * 10 + (*buf - '0');
568 else
569 return(0);
570 buf++;
571 if ((*buf >= '0') && (*buf <= '9'))
572 val = val * 10 + (*buf - '0');
573 else
574 return(0);
575 buf++;
576 if (*buf == '-')
577 return(-val);
578 return(val);
579}
580
581/**
582 * xmlNanoFTPGetMore:
583 * @ctx: an FTP context
584 *
585 * Read more information from the FTP control connection
586 * Returns the number of bytes read, < 0 indicates an error
587 */
588static int
589xmlNanoFTPGetMore(void *ctx) {
590 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
591 int len;
592 int size;
593
594 if ((ctxt->controlBufIndex < 0) || (ctxt->controlBufIndex > FTP_BUF_SIZE)) {
595#ifdef DEBUG_FTP
596 xmlGenericError(xmlGenericErrorContext,
597 "xmlNanoFTPGetMore : controlBufIndex = %d\n",
598 ctxt->controlBufIndex);
599#endif
600 return(-1);
601 }
602
603 if ((ctxt->controlBufUsed < 0) || (ctxt->controlBufUsed > FTP_BUF_SIZE)) {
604#ifdef DEBUG_FTP
605 xmlGenericError(xmlGenericErrorContext,
606 "xmlNanoFTPGetMore : controlBufUsed = %d\n",
607 ctxt->controlBufUsed);
608#endif
609 return(-1);
610 }
611 if (ctxt->controlBufIndex > ctxt->controlBufUsed) {
612#ifdef DEBUG_FTP
613 xmlGenericError(xmlGenericErrorContext,
614 "xmlNanoFTPGetMore : controlBufIndex > controlBufUsed %d > %d\n",
615 ctxt->controlBufIndex, ctxt->controlBufUsed);
616#endif
617 return(-1);
618 }
619
620 /*
621 * First pack the control buffer
622 */
623 if (ctxt->controlBufIndex > 0) {
624 memmove(&ctxt->controlBuf[0], &ctxt->controlBuf[ctxt->controlBufIndex],
625 ctxt->controlBufUsed - ctxt->controlBufIndex);
626 ctxt->controlBufUsed -= ctxt->controlBufIndex;
627 ctxt->controlBufIndex = 0;
628 }
629 size = FTP_BUF_SIZE - ctxt->controlBufUsed;
630 if (size == 0) {
631#ifdef DEBUG_FTP
632 xmlGenericError(xmlGenericErrorContext,
633 "xmlNanoFTPGetMore : buffer full %d \n", ctxt->controlBufUsed);
634#endif
635 return(0);
636 }
637
638 /*
Daniel Veillard60087f32001-10-10 09:45:09 +0000639 * Read the amount left on the control connection
Owen Taylor3473f882001-02-23 17:55:21 +0000640 */
641 if ((len = recv(ctxt->controlFd, &ctxt->controlBuf[ctxt->controlBufIndex],
642 size, 0)) < 0) {
643 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
644 ctxt->controlFd = -1;
645 return(-1);
646 }
647#ifdef DEBUG_FTP
648 xmlGenericError(xmlGenericErrorContext,
649 "xmlNanoFTPGetMore : read %d [%d - %d]\n", len,
650 ctxt->controlBufUsed, ctxt->controlBufUsed + len);
651#endif
652 ctxt->controlBufUsed += len;
653 ctxt->controlBuf[ctxt->controlBufUsed] = 0;
654
655 return(len);
656}
657
658/**
659 * xmlNanoFTPReadResponse:
660 * @ctx: an FTP context
661 *
662 * Read the response from the FTP server after a command.
663 * Returns the code number
664 */
665static int
666xmlNanoFTPReadResponse(void *ctx) {
667 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
668 char *ptr, *end;
669 int len;
670 int res = -1, cur = -1;
671
672get_more:
673 /*
674 * Assumes everything up to controlBuf[controlBufIndex] has been read
675 * and analyzed.
676 */
677 len = xmlNanoFTPGetMore(ctx);
678 if (len < 0) {
679 return(-1);
680 }
681 if ((ctxt->controlBufUsed == 0) && (len == 0)) {
682 return(-1);
683 }
684 ptr = &ctxt->controlBuf[ctxt->controlBufIndex];
685 end = &ctxt->controlBuf[ctxt->controlBufUsed];
686
687#ifdef DEBUG_FTP
688 xmlGenericError(xmlGenericErrorContext,
689 "\n<<<\n%s\n--\n", ptr);
690#endif
691 while (ptr < end) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000692 cur = xmlNanoFTPParseResponse(ptr, end - ptr);
Owen Taylor3473f882001-02-23 17:55:21 +0000693 if (cur > 0) {
694 /*
695 * Successfully scanned the control code, scratch
696 * till the end of the line, but keep the index to be
697 * able to analyze the result if needed.
698 */
699 res = cur;
700 ptr += 3;
701 ctxt->controlBufAnswer = ptr - ctxt->controlBuf;
702 while ((ptr < end) && (*ptr != '\n')) ptr++;
703 if (*ptr == '\n') ptr++;
704 if (*ptr == '\r') ptr++;
705 break;
706 }
707 while ((ptr < end) && (*ptr != '\n')) ptr++;
708 if (ptr >= end) {
709 ctxt->controlBufIndex = ctxt->controlBufUsed;
710 goto get_more;
711 }
712 if (*ptr != '\r') ptr++;
713 }
714
715 if (res < 0) goto get_more;
716 ctxt->controlBufIndex = ptr - ctxt->controlBuf;
717#ifdef DEBUG_FTP
718 ptr = &ctxt->controlBuf[ctxt->controlBufIndex];
719 xmlGenericError(xmlGenericErrorContext, "\n---\n%s\n--\n", ptr);
720#endif
721
722#ifdef DEBUG_FTP
723 xmlGenericError(xmlGenericErrorContext, "Got %d\n", res);
724#endif
725 return(res / 100);
726}
727
728/**
729 * xmlNanoFTPGetResponse:
730 * @ctx: an FTP context
731 *
732 * Get the response from the FTP server after a command.
733 * Returns the code number
734 */
735
736int
737xmlNanoFTPGetResponse(void *ctx) {
738 int res;
739
740 res = xmlNanoFTPReadResponse(ctx);
741
742 return(res);
743}
744
745/**
746 * xmlNanoFTPCheckResponse:
747 * @ctx: an FTP context
748 *
749 * Check if there is a response from the FTP server after a command.
750 * Returns the code number, or 0
751 */
752
753int
754xmlNanoFTPCheckResponse(void *ctx) {
755 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
756 fd_set rfd;
757 struct timeval tv;
758
759 tv.tv_sec = 0;
760 tv.tv_usec = 0;
761 FD_ZERO(&rfd);
762 FD_SET(ctxt->controlFd, &rfd);
763 switch(select(ctxt->controlFd + 1, &rfd, NULL, NULL, &tv)) {
764 case 0:
765 return(0);
766 case -1:
767#ifdef DEBUG_FTP
768 perror("select");
769#endif
770 return(-1);
771
772 }
773
774 return(xmlNanoFTPReadResponse(ctx));
775}
776
777/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000778 * Send the user authentication
Owen Taylor3473f882001-02-23 17:55:21 +0000779 */
780
781static int
782xmlNanoFTPSendUser(void *ctx) {
783 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
784 char buf[200];
785 int len;
786 int res;
787
788 if (ctxt->user == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000789 snprintf(buf, sizeof(buf), "USER anonymous\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000790 else
Owen Taylor3473f882001-02-23 17:55:21 +0000791 snprintf(buf, sizeof(buf), "USER %s\r\n", ctxt->user);
Owen Taylor3473f882001-02-23 17:55:21 +0000792 buf[sizeof(buf) - 1] = 0;
793 len = strlen(buf);
794#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +0000795 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000796#endif
797 res = send(ctxt->controlFd, buf, len, 0);
798 if (res < 0) return(res);
799 return(0);
800}
801
802/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000803 * Send the password authentication
Owen Taylor3473f882001-02-23 17:55:21 +0000804 */
805
806static int
807xmlNanoFTPSendPasswd(void *ctx) {
808 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
809 char buf[200];
810 int len;
811 int res;
812
813 if (ctxt->passwd == NULL)
Daniel Veillard9ae1eba2001-10-19 09:48:35 +0000814 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000815 else
Owen Taylor3473f882001-02-23 17:55:21 +0000816 snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
Owen Taylor3473f882001-02-23 17:55:21 +0000817 buf[sizeof(buf) - 1] = 0;
818 len = strlen(buf);
819#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +0000820 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000821#endif
822 res = send(ctxt->controlFd, buf, len, 0);
823 if (res < 0) return(res);
824 return(0);
825}
826
827/**
828 * xmlNanoFTPQuit:
829 * @ctx: an FTP context
830 *
831 * Send a QUIT command to the server
832 *
833 * Returns -1 in case of error, 0 otherwise
834 */
835
836
837int
838xmlNanoFTPQuit(void *ctx) {
839 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
840 char buf[200];
841 int len;
842 int res;
843
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000844 snprintf(buf, sizeof(buf), "QUIT\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000845 len = strlen(buf);
846#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +0000847 xmlGenericError(xmlGenericErrorContext, "%s", buf); /* Just to be consistent, even though we know it can't have a % in it */
Owen Taylor3473f882001-02-23 17:55:21 +0000848#endif
849 res = send(ctxt->controlFd, buf, len, 0);
850 return(0);
851}
852
853/**
854 * xmlNanoFTPConnect:
855 * @ctx: an FTP context
856 *
857 * Tries to open a control connection
858 *
859 * Returns -1 in case of error, 0 otherwise
860 */
861
862int
863xmlNanoFTPConnect(void *ctx) {
864 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
865 struct hostent *hp;
866 int port;
867 int res;
868
869 if (ctxt == NULL)
870 return(-1);
871 if (ctxt->hostname == NULL)
872 return(-1);
873
874 /*
875 * do the blocking DNS query.
876 */
877 if (proxy)
878 hp = gethostbyname(proxy);
879 else
880 hp = gethostbyname(ctxt->hostname);
881 if (hp == NULL)
882 return(-1);
883
884 /*
885 * Prepare the socket
886 */
887 memset(&ctxt->ftpAddr, 0, sizeof(ctxt->ftpAddr));
888 ctxt->ftpAddr.sin_family = AF_INET;
889 memcpy(&ctxt->ftpAddr.sin_addr, hp->h_addr_list[0], hp->h_length);
890 if (proxy) {
891 port = proxyPort;
892 } else {
893 port = ctxt->port;
894 }
895 if (port == 0)
896 port = 21;
897 ctxt->ftpAddr.sin_port = htons(port);
898 ctxt->controlFd = socket(AF_INET, SOCK_STREAM, 0);
899 if (ctxt->controlFd < 0)
900 return(-1);
901
902 /*
903 * Do the connect.
904 */
905 if (connect(ctxt->controlFd, (struct sockaddr *) &ctxt->ftpAddr,
906 sizeof(struct sockaddr_in)) < 0) {
907 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
908 ctxt->controlFd = -1;
909 return(-1);
910 }
911
912 /*
913 * Wait for the HELLO from the server.
914 */
915 res = xmlNanoFTPGetResponse(ctxt);
916 if (res != 2) {
917 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
918 ctxt->controlFd = -1;
919 return(-1);
920 }
921
922 /*
923 * State diagram for the login operation on the FTP server
924 *
925 * Reference: RFC 959
926 *
927 * 1
928 * +---+ USER +---+------------->+---+
929 * | B |---------->| W | 2 ---->| E |
930 * +---+ +---+------ | -->+---+
931 * | | | | |
932 * 3 | | 4,5 | | |
933 * -------------- ----- | | |
934 * | | | | |
935 * | | | | |
936 * | --------- |
937 * | 1| | | |
938 * V | | | |
939 * +---+ PASS +---+ 2 | ------>+---+
940 * | |---------->| W |------------->| S |
941 * +---+ +---+ ---------->+---+
942 * | | | | |
943 * 3 | |4,5| | |
944 * -------------- -------- |
945 * | | | | |
946 * | | | | |
947 * | -----------
948 * | 1,3| | | |
949 * V | 2| | |
950 * +---+ ACCT +---+-- | ----->+---+
951 * | |---------->| W | 4,5 -------->| F |
952 * +---+ +---+------------->+---+
953 *
954 * Of course in case of using a proxy this get really nasty and is not
955 * standardized at all :-(
956 */
957 if (proxy) {
958 int len;
959 char buf[400];
960
961 if (proxyUser != NULL) {
962 /*
963 * We need proxy auth
964 */
Owen Taylor3473f882001-02-23 17:55:21 +0000965 snprintf(buf, sizeof(buf), "USER %s\r\n", proxyUser);
Owen Taylor3473f882001-02-23 17:55:21 +0000966 buf[sizeof(buf) - 1] = 0;
967 len = strlen(buf);
968#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +0000969 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000970#endif
971 res = send(ctxt->controlFd, buf, len, 0);
972 if (res < 0) {
973 closesocket(ctxt->controlFd);
974 ctxt->controlFd = -1;
975 return(res);
976 }
977 res = xmlNanoFTPGetResponse(ctxt);
978 switch (res) {
979 case 2:
980 if (proxyPasswd == NULL)
981 break;
982 case 3:
983 if (proxyPasswd != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +0000984 snprintf(buf, sizeof(buf), "PASS %s\r\n", proxyPasswd);
Owen Taylor3473f882001-02-23 17:55:21 +0000985 else
Daniel Veillard9ae1eba2001-10-19 09:48:35 +0000986 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000987 buf[sizeof(buf) - 1] = 0;
988 len = strlen(buf);
989#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +0000990 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000991#endif
992 res = send(ctxt->controlFd, buf, len, 0);
993 if (res < 0) {
994 closesocket(ctxt->controlFd);
995 ctxt->controlFd = -1;
996 return(res);
997 }
998 res = xmlNanoFTPGetResponse(ctxt);
999 if (res > 3) {
1000 closesocket(ctxt->controlFd);
1001 ctxt->controlFd = -1;
1002 return(-1);
1003 }
1004 break;
1005 case 1:
1006 break;
1007 case 4:
1008 case 5:
1009 case -1:
1010 default:
1011 closesocket(ctxt->controlFd);
1012 ctxt->controlFd = -1;
1013 return(-1);
1014 }
1015 }
1016
1017 /*
1018 * We assume we don't need more authentication to the proxy
1019 * and that it succeeded :-\
1020 */
1021 switch (proxyType) {
1022 case 0:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001023 /* we will try in sequence */
Owen Taylor3473f882001-02-23 17:55:21 +00001024 case 1:
1025 /* Using SITE command */
Owen Taylor3473f882001-02-23 17:55:21 +00001026 snprintf(buf, sizeof(buf), "SITE %s\r\n", ctxt->hostname);
Owen Taylor3473f882001-02-23 17:55:21 +00001027 buf[sizeof(buf) - 1] = 0;
1028 len = strlen(buf);
1029#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001030 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001031#endif
1032 res = send(ctxt->controlFd, buf, len, 0);
1033 if (res < 0) {
1034 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1035 ctxt->controlFd = -1;
1036 return(res);
1037 }
1038 res = xmlNanoFTPGetResponse(ctxt);
1039 if (res == 2) {
1040 /* we assume it worked :-\ 1 is error for SITE command */
1041 proxyType = 1;
1042 break;
1043 }
1044 if (proxyType == 1) {
1045 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1046 ctxt->controlFd = -1;
1047 return(-1);
1048 }
1049 case 2:
1050 /* USER user@host command */
1051 if (ctxt->user == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001052 snprintf(buf, sizeof(buf), "USER anonymous@%s\r\n",
1053 ctxt->hostname);
Owen Taylor3473f882001-02-23 17:55:21 +00001054 else
Owen Taylor3473f882001-02-23 17:55:21 +00001055 snprintf(buf, sizeof(buf), "USER %s@%s\r\n",
1056 ctxt->user, ctxt->hostname);
Owen Taylor3473f882001-02-23 17:55:21 +00001057 buf[sizeof(buf) - 1] = 0;
1058 len = strlen(buf);
1059#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001060 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001061#endif
1062 res = send(ctxt->controlFd, buf, len, 0);
1063 if (res < 0) {
1064 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1065 ctxt->controlFd = -1;
1066 return(res);
1067 }
1068 res = xmlNanoFTPGetResponse(ctxt);
1069 if ((res == 1) || (res == 2)) {
1070 /* we assume it worked :-\ */
1071 proxyType = 2;
1072 return(0);
1073 }
1074 if (ctxt->passwd == NULL)
Daniel Veillard9ae1eba2001-10-19 09:48:35 +00001075 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001076 else
Owen Taylor3473f882001-02-23 17:55:21 +00001077 snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
Owen Taylor3473f882001-02-23 17:55:21 +00001078 buf[sizeof(buf) - 1] = 0;
1079 len = strlen(buf);
1080#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001081 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001082#endif
1083 res = send(ctxt->controlFd, buf, len, 0);
1084 if (res < 0) {
1085 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1086 ctxt->controlFd = -1;
1087 return(res);
1088 }
1089 res = xmlNanoFTPGetResponse(ctxt);
1090 if ((res == 1) || (res == 2)) {
1091 /* we assume it worked :-\ */
1092 proxyType = 2;
1093 return(0);
1094 }
1095 if (proxyType == 2) {
1096 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1097 ctxt->controlFd = -1;
1098 return(-1);
1099 }
1100 case 3:
1101 /*
1102 * If you need support for other Proxy authentication scheme
1103 * send the code or at least the sequence in use.
1104 */
1105 default:
1106 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1107 ctxt->controlFd = -1;
1108 return(-1);
1109 }
1110 }
1111 /*
1112 * Non-proxy handling.
1113 */
1114 res = xmlNanoFTPSendUser(ctxt);
1115 if (res < 0) {
1116 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1117 ctxt->controlFd = -1;
1118 return(-1);
1119 }
1120 res = xmlNanoFTPGetResponse(ctxt);
1121 switch (res) {
1122 case 2:
1123 return(0);
1124 case 3:
1125 break;
1126 case 1:
1127 case 4:
1128 case 5:
1129 case -1:
1130 default:
1131 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1132 ctxt->controlFd = -1;
1133 return(-1);
1134 }
1135 res = xmlNanoFTPSendPasswd(ctxt);
1136 if (res < 0) {
1137 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1138 ctxt->controlFd = -1;
1139 return(-1);
1140 }
1141 res = xmlNanoFTPGetResponse(ctxt);
1142 switch (res) {
1143 case 2:
1144 break;
1145 case 3:
1146 xmlGenericError(xmlGenericErrorContext,
1147 "FTP server asking for ACCNT on anonymous\n");
1148 case 1:
1149 case 4:
1150 case 5:
1151 case -1:
1152 default:
1153 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1154 ctxt->controlFd = -1;
1155 return(-1);
1156 }
1157
1158 return(0);
1159}
1160
1161/**
1162 * xmlNanoFTPConnectTo:
1163 * @server: an FTP server name
1164 * @port: the port (use 21 if 0)
1165 *
1166 * Tries to open a control connection to the given server/port
1167 *
1168 * Returns an fTP context or NULL if it failed
1169 */
1170
1171void*
1172xmlNanoFTPConnectTo(const char *server, int port) {
1173 xmlNanoFTPCtxtPtr ctxt;
1174 int res;
1175
1176 xmlNanoFTPInit();
1177 if (server == NULL)
1178 return(NULL);
1179 ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(NULL);
1180 ctxt->hostname = xmlMemStrdup(server);
1181 if (port != 0)
1182 ctxt->port = port;
1183 res = xmlNanoFTPConnect(ctxt);
1184 if (res < 0) {
1185 xmlNanoFTPFreeCtxt(ctxt);
1186 return(NULL);
1187 }
1188 return(ctxt);
1189}
1190
1191/**
1192 * xmlNanoFTPCwd:
1193 * @ctx: an FTP context
1194 * @directory: a directory on the server
1195 *
1196 * Tries to change the remote directory
1197 *
1198 * Returns -1 incase of error, 1 if CWD worked, 0 if it failed
1199 */
1200
1201int
1202xmlNanoFTPCwd(void *ctx, char *directory) {
1203 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1204 char buf[400];
1205 int len;
1206 int res;
1207
1208 /*
1209 * Expected response code for CWD:
1210 *
1211 * CWD
1212 * 250
1213 * 500, 501, 502, 421, 530, 550
1214 */
Owen Taylor3473f882001-02-23 17:55:21 +00001215 snprintf(buf, sizeof(buf), "CWD %s\r\n", directory);
Owen Taylor3473f882001-02-23 17:55:21 +00001216 buf[sizeof(buf) - 1] = 0;
1217 len = strlen(buf);
1218#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001219 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001220#endif
1221 res = send(ctxt->controlFd, buf, len, 0);
1222 if (res < 0) return(res);
1223 res = xmlNanoFTPGetResponse(ctxt);
1224 if (res == 4) {
1225 return(-1);
1226 }
1227 if (res == 2) return(1);
1228 if (res == 5) {
1229 return(0);
1230 }
1231 return(0);
1232}
1233
1234/**
Daniel Veillard6c73cb82003-03-05 16:45:40 +00001235 * xmlNanoFTPDele:
1236 * @ctx: an FTP context
1237 * @file: a file or directory on the server
1238 *
1239 * Tries to delete an item (file or directory) from server
1240 *
1241 * Returns -1 incase of error, 1 if DELE worked, 0 if it failed
1242 */
1243
1244int
1245xmlNanoFTPDele(void *ctx, char *file) {
1246 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1247 char buf[400];
1248 int len;
1249 int res;
1250
1251 /*
1252 * Expected response code for DELE:
1253 *
1254 * DELE
1255 * 250
1256 * 450, 550
1257 * 500, 501, 502, 421, 530
1258 */
1259
1260 snprintf(buf, sizeof(buf), "DELE %s\r\n", file);
1261 buf[sizeof(buf) - 1] = 0;
1262 len = strlen(buf);
1263#ifdef DEBUG_FTP
1264 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1265#endif
1266 res = send(ctxt->controlFd, buf, len, 0);
1267 if (res < 0) return(res);
1268 res = xmlNanoFTPGetResponse(ctxt);
1269 if (res == 4) {
1270 return(-1);
1271 }
1272 if (res == 2) return(1);
1273 if (res == 5) {
1274 return(0);
1275 }
1276 return(0);
1277}
1278/**
Owen Taylor3473f882001-02-23 17:55:21 +00001279 * xmlNanoFTPGetConnection:
1280 * @ctx: an FTP context
1281 *
1282 * Try to open a data connection to the server. Currently only
1283 * passive mode is supported.
1284 *
1285 * Returns -1 incase of error, 0 otherwise
1286 */
1287
1288int
1289xmlNanoFTPGetConnection(void *ctx) {
1290 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1291 char buf[200], *cur;
1292 int len, i;
1293 int res;
1294 unsigned char ad[6], *adp, *portp;
1295 unsigned int temp[6];
1296 struct sockaddr_in dataAddr;
1297 SOCKLEN_T dataAddrLen;
1298
1299 ctxt->dataFd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1300 if (ctxt->dataFd < 0) {
1301 xmlGenericError(xmlGenericErrorContext,
1302 "xmlNanoFTPGetConnection: failed to create socket\n");
1303 return(-1);
1304 }
1305 dataAddrLen = sizeof(dataAddr);
1306 memset(&dataAddr, 0, dataAddrLen);
1307 dataAddr.sin_family = AF_INET;
1308
1309 if (ctxt->passive) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001310 snprintf(buf, sizeof(buf), "PASV\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001311 len = strlen(buf);
1312#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001313 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001314#endif
1315 res = send(ctxt->controlFd, buf, len, 0);
1316 if (res < 0) {
1317 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1318 return(res);
1319 }
1320 res = xmlNanoFTPReadResponse(ctx);
1321 if (res != 2) {
1322 if (res == 5) {
1323 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1324 return(-1);
1325 } else {
1326 /*
1327 * retry with an active connection
1328 */
1329 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1330 ctxt->passive = 0;
1331 }
1332 }
1333 cur = &ctxt->controlBuf[ctxt->controlBufAnswer];
1334 while (((*cur < '0') || (*cur > '9')) && *cur != '\0') cur++;
1335 if (sscanf(cur, "%u,%u,%u,%u,%u,%u", &temp[0], &temp[1], &temp[2],
1336 &temp[3], &temp[4], &temp[5]) != 6) {
1337 xmlGenericError(xmlGenericErrorContext,
1338 "Invalid answer to PASV\n");
1339 if (ctxt->dataFd != -1) {
1340 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1341 }
1342 return(-1);
1343 }
1344 for (i=0; i<6; i++) ad[i] = (unsigned char) (temp[i] & 0xff);
1345 memcpy(&dataAddr.sin_addr, &ad[0], 4);
1346 memcpy(&dataAddr.sin_port, &ad[4], 2);
1347 if (connect(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
1348 xmlGenericError(xmlGenericErrorContext,
1349 "Failed to create a data connection\n");
1350 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1351 return (-1);
1352 }
1353 } else {
1354 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
1355 dataAddr.sin_port = 0;
1356 if (bind(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
1357 xmlGenericError(xmlGenericErrorContext,
1358 "Failed to bind a port\n");
1359 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1360 return (-1);
1361 }
1362 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
1363
1364 if (listen(ctxt->dataFd, 1) < 0) {
1365 xmlGenericError(xmlGenericErrorContext,
1366 "Could not listen on port %d\n",
1367 ntohs(dataAddr.sin_port));
1368 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1369 return (-1);
1370 }
1371 adp = (unsigned char *) &dataAddr.sin_addr;
1372 portp = (unsigned char *) &dataAddr.sin_port;
Owen Taylor3473f882001-02-23 17:55:21 +00001373 snprintf(buf, sizeof(buf), "PORT %d,%d,%d,%d,%d,%d\r\n",
1374 adp[0] & 0xff, adp[1] & 0xff, adp[2] & 0xff, adp[3] & 0xff,
1375 portp[0] & 0xff, portp[1] & 0xff);
Owen Taylor3473f882001-02-23 17:55:21 +00001376 buf[sizeof(buf) - 1] = 0;
1377 len = strlen(buf);
1378#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001379 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001380#endif
1381
1382 res = send(ctxt->controlFd, buf, len, 0);
1383 if (res < 0) {
1384 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1385 return(res);
1386 }
1387 res = xmlNanoFTPGetResponse(ctxt);
1388 if (res != 2) {
1389 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1390 return(-1);
1391 }
1392 }
1393 return(ctxt->dataFd);
1394
1395}
1396
1397/**
1398 * xmlNanoFTPCloseConnection:
1399 * @ctx: an FTP context
1400 *
1401 * Close the data connection from the server
1402 *
1403 * Returns -1 incase of error, 0 otherwise
1404 */
1405
1406int
1407xmlNanoFTPCloseConnection(void *ctx) {
1408 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1409 int res;
1410 fd_set rfd, efd;
1411 struct timeval tv;
1412
1413 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1414 tv.tv_sec = 15;
1415 tv.tv_usec = 0;
1416 FD_ZERO(&rfd);
1417 FD_SET(ctxt->controlFd, &rfd);
1418 FD_ZERO(&efd);
1419 FD_SET(ctxt->controlFd, &efd);
1420 res = select(ctxt->controlFd + 1, &rfd, NULL, &efd, &tv);
1421 if (res < 0) {
1422#ifdef DEBUG_FTP
1423 perror("select");
1424#endif
1425 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1426 return(-1);
1427 }
1428 if (res == 0) {
1429#ifdef DEBUG_FTP
1430 xmlGenericError(xmlGenericErrorContext,
1431 "xmlNanoFTPCloseConnection: timeout\n");
1432#endif
1433 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1434 } else {
1435 res = xmlNanoFTPGetResponse(ctxt);
1436 if (res != 2) {
1437 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1438 return(-1);
1439 }
1440 }
1441 return(0);
1442}
1443
1444/**
1445 * xmlNanoFTPParseList:
1446 * @list: some data listing received from the server
1447 * @callback: the user callback
1448 * @userData: the user callback data
1449 *
1450 * Parse at most one entry from the listing.
1451 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001452 * Returns -1 incase of error, the length of data parsed otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00001453 */
1454
1455static int
1456xmlNanoFTPParseList(const char *list, ftpListCallback callback, void *userData) {
1457 const char *cur = list;
1458 char filename[151];
1459 char attrib[11];
1460 char owner[11];
1461 char group[11];
1462 char month[4];
1463 int year = 0;
1464 int minute = 0;
1465 int hour = 0;
1466 int day = 0;
1467 unsigned long size = 0;
1468 int links = 0;
1469 int i;
1470
1471 if (!strncmp(cur, "total", 5)) {
1472 cur += 5;
1473 while (*cur == ' ') cur++;
1474 while ((*cur >= '0') && (*cur <= '9'))
1475 links = (links * 10) + (*cur++ - '0');
1476 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1477 cur++;
1478 return(cur - list);
1479 } else if (*list == '+') {
1480 return(0);
1481 } else {
1482 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1483 cur++;
1484 if (*cur == 0) return(0);
1485 i = 0;
1486 while (*cur != ' ') {
1487 if (i < 10)
1488 attrib[i++] = *cur;
1489 cur++;
1490 if (*cur == 0) return(0);
1491 }
1492 attrib[10] = 0;
1493 while (*cur == ' ') cur++;
1494 if (*cur == 0) return(0);
1495 while ((*cur >= '0') && (*cur <= '9'))
1496 links = (links * 10) + (*cur++ - '0');
1497 while (*cur == ' ') cur++;
1498 if (*cur == 0) return(0);
1499 i = 0;
1500 while (*cur != ' ') {
1501 if (i < 10)
1502 owner[i++] = *cur;
1503 cur++;
1504 if (*cur == 0) return(0);
1505 }
1506 owner[i] = 0;
1507 while (*cur == ' ') cur++;
1508 if (*cur == 0) return(0);
1509 i = 0;
1510 while (*cur != ' ') {
1511 if (i < 10)
1512 group[i++] = *cur;
1513 cur++;
1514 if (*cur == 0) return(0);
1515 }
1516 group[i] = 0;
1517 while (*cur == ' ') cur++;
1518 if (*cur == 0) return(0);
1519 while ((*cur >= '0') && (*cur <= '9'))
1520 size = (size * 10) + (*cur++ - '0');
1521 while (*cur == ' ') cur++;
1522 if (*cur == 0) return(0);
1523 i = 0;
1524 while (*cur != ' ') {
1525 if (i < 3)
1526 month[i++] = *cur;
1527 cur++;
1528 if (*cur == 0) return(0);
1529 }
1530 month[i] = 0;
1531 while (*cur == ' ') cur++;
1532 if (*cur == 0) return(0);
1533 while ((*cur >= '0') && (*cur <= '9'))
1534 day = (day * 10) + (*cur++ - '0');
1535 while (*cur == ' ') cur++;
1536 if (*cur == 0) return(0);
1537 if ((cur[1] == 0) || (cur[2] == 0)) return(0);
1538 if ((cur[1] == ':') || (cur[2] == ':')) {
1539 while ((*cur >= '0') && (*cur <= '9'))
1540 hour = (hour * 10) + (*cur++ - '0');
1541 if (*cur == ':') cur++;
1542 while ((*cur >= '0') && (*cur <= '9'))
1543 minute = (minute * 10) + (*cur++ - '0');
1544 } else {
1545 while ((*cur >= '0') && (*cur <= '9'))
1546 year = (year * 10) + (*cur++ - '0');
1547 }
1548 while (*cur == ' ') cur++;
1549 if (*cur == 0) return(0);
1550 i = 0;
1551 while ((*cur != '\n') && (*cur != '\r')) {
1552 if (i < 150)
1553 filename[i++] = *cur;
1554 cur++;
1555 if (*cur == 0) return(0);
1556 }
1557 filename[i] = 0;
1558 if ((*cur != '\n') && (*cur != '\r'))
1559 return(0);
1560 while ((*cur == '\n') || (*cur == '\r'))
1561 cur++;
1562 }
1563 if (callback != NULL) {
1564 callback(userData, filename, attrib, owner, group, size, links,
1565 year, month, day, hour, minute);
1566 }
1567 return(cur - list);
1568}
1569
1570/**
1571 * xmlNanoFTPList:
1572 * @ctx: an FTP context
1573 * @callback: the user callback
1574 * @userData: the user callback data
1575 * @filename: optional files to list
1576 *
1577 * Do a listing on the server. All files info are passed back
1578 * in the callbacks.
1579 *
1580 * Returns -1 incase of error, 0 otherwise
1581 */
1582
1583int
1584xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData,
1585 char *filename) {
1586 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1587 char buf[4096 + 1];
1588 int len, res;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001589 int indx = 0, base;
Owen Taylor3473f882001-02-23 17:55:21 +00001590 fd_set rfd, efd;
1591 struct timeval tv;
1592
1593 if (filename == NULL) {
1594 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1595 return(-1);
1596 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1597 if (ctxt->dataFd == -1)
1598 return(-1);
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001599 snprintf(buf, sizeof(buf), "LIST -L\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001600 } else {
1601 if (filename[0] != '/') {
1602 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1603 return(-1);
1604 }
1605 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1606 if (ctxt->dataFd == -1)
1607 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001608 snprintf(buf, sizeof(buf), "LIST -L %s\r\n", filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001609 }
1610 buf[sizeof(buf) - 1] = 0;
1611 len = strlen(buf);
1612#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001613 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001614#endif
1615 res = send(ctxt->controlFd, buf, len, 0);
1616 if (res < 0) {
1617 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1618 return(res);
1619 }
1620 res = xmlNanoFTPReadResponse(ctxt);
1621 if (res != 1) {
1622 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1623 return(-res);
1624 }
1625
1626 do {
1627 tv.tv_sec = 1;
1628 tv.tv_usec = 0;
1629 FD_ZERO(&rfd);
1630 FD_SET(ctxt->dataFd, &rfd);
1631 FD_ZERO(&efd);
1632 FD_SET(ctxt->dataFd, &efd);
1633 res = select(ctxt->dataFd + 1, &rfd, NULL, &efd, &tv);
1634 if (res < 0) {
1635#ifdef DEBUG_FTP
1636 perror("select");
1637#endif
1638 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1639 return(-1);
1640 }
1641 if (res == 0) {
1642 res = xmlNanoFTPCheckResponse(ctxt);
1643 if (res < 0) {
1644 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1645 ctxt->dataFd = -1;
1646 return(-1);
1647 }
1648 if (res == 2) {
1649 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1650 return(0);
1651 }
1652
1653 continue;
1654 }
1655
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001656 if ((len = recv(ctxt->dataFd, &buf[indx], sizeof(buf) - (indx + 1), 0)) < 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00001657#ifdef DEBUG_FTP
1658 perror("recv");
1659#endif
1660 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1661 ctxt->dataFd = -1;
1662 return(-1);
1663 }
1664#ifdef DEBUG_FTP
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001665 write(1, &buf[indx], len);
Owen Taylor3473f882001-02-23 17:55:21 +00001666#endif
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001667 indx += len;
1668 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001669 base = 0;
1670 do {
1671 res = xmlNanoFTPParseList(&buf[base], callback, userData);
1672 base += res;
1673 } while (res > 0);
1674
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001675 memmove(&buf[0], &buf[base], indx - base);
1676 indx -= base;
Owen Taylor3473f882001-02-23 17:55:21 +00001677 } while (len != 0);
1678 xmlNanoFTPCloseConnection(ctxt);
1679 return(0);
1680}
1681
1682/**
1683 * xmlNanoFTPGetSocket:
1684 * @ctx: an FTP context
1685 * @filename: the file to retrieve (or NULL if path is in context).
1686 *
1687 * Initiate fetch of the given file from the server.
1688 *
1689 * Returns the socket for the data connection, or <0 in case of error
1690 */
1691
1692
1693int
1694xmlNanoFTPGetSocket(void *ctx, const char *filename) {
1695 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1696 char buf[300];
1697 int res, len;
1698 if ((filename == NULL) && (ctxt->path == NULL))
1699 return(-1);
1700 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1701 if (ctxt->dataFd == -1)
1702 return(-1);
1703
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001704 snprintf(buf, sizeof(buf), "TYPE I\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001705 len = strlen(buf);
1706#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001707 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001708#endif
1709 res = send(ctxt->controlFd, buf, len, 0);
1710 if (res < 0) {
1711 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1712 return(res);
1713 }
1714 res = xmlNanoFTPReadResponse(ctxt);
1715 if (res != 2) {
1716 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1717 return(-res);
1718 }
1719 if (filename == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001720 snprintf(buf, sizeof(buf), "RETR %s\r\n", ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001721 else
Owen Taylor3473f882001-02-23 17:55:21 +00001722 snprintf(buf, sizeof(buf), "RETR %s\r\n", filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001723 buf[sizeof(buf) - 1] = 0;
1724 len = strlen(buf);
1725#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001726 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001727#endif
1728 res = send(ctxt->controlFd, buf, len, 0);
1729 if (res < 0) {
1730 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1731 return(res);
1732 }
1733 res = xmlNanoFTPReadResponse(ctxt);
1734 if (res != 1) {
1735 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1736 return(-res);
1737 }
1738 return(ctxt->dataFd);
1739}
1740
1741/**
1742 * xmlNanoFTPGet:
1743 * @ctx: an FTP context
1744 * @callback: the user callback
1745 * @userData: the user callback data
1746 * @filename: the file to retrieve
1747 *
1748 * Fetch the given file from the server. All data are passed back
1749 * in the callbacks. The last callback has a size of 0 block.
1750 *
1751 * Returns -1 incase of error, 0 otherwise
1752 */
1753
1754int
1755xmlNanoFTPGet(void *ctx, ftpDataCallback callback, void *userData,
1756 const char *filename) {
1757 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1758 char buf[4096];
1759 int len = 0, res;
1760 fd_set rfd;
1761 struct timeval tv;
1762
1763 if ((filename == NULL) && (ctxt->path == NULL))
1764 return(-1);
1765 if (callback == NULL)
1766 return(-1);
1767 if (xmlNanoFTPGetSocket(ctxt, filename) < 0)
1768 return(-1);
1769
1770 do {
1771 tv.tv_sec = 1;
1772 tv.tv_usec = 0;
1773 FD_ZERO(&rfd);
1774 FD_SET(ctxt->dataFd, &rfd);
1775 res = select(ctxt->dataFd + 1, &rfd, NULL, NULL, &tv);
1776 if (res < 0) {
1777#ifdef DEBUG_FTP
1778 perror("select");
1779#endif
1780 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1781 return(-1);
1782 }
1783 if (res == 0) {
1784 res = xmlNanoFTPCheckResponse(ctxt);
1785 if (res < 0) {
1786 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1787 ctxt->dataFd = -1;
1788 return(-1);
1789 }
1790 if (res == 2) {
1791 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1792 return(0);
1793 }
1794
1795 continue;
1796 }
1797 if ((len = recv(ctxt->dataFd, buf, sizeof(buf), 0)) < 0) {
1798 callback(userData, buf, len);
1799 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1800 return(-1);
1801 }
1802 callback(userData, buf, len);
1803 } while (len != 0);
1804
1805 return(xmlNanoFTPCloseConnection(ctxt));
1806}
1807
1808/**
1809 * xmlNanoFTPRead:
1810 * @ctx: the FTP context
1811 * @dest: a buffer
1812 * @len: the buffer length
1813 *
1814 * This function tries to read @len bytes from the existing FTP connection
1815 * and saves them in @dest. This is a blocking call.
1816 *
1817 * Returns the number of byte read. 0 is an indication of an end of connection.
1818 * -1 indicates a parameter error.
1819 */
1820int
1821xmlNanoFTPRead(void *ctx, void *dest, int len) {
1822 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1823
1824 if (ctx == NULL) return(-1);
1825 if (ctxt->dataFd < 0) return(0);
1826 if (dest == NULL) return(-1);
1827 if (len <= 0) return(0);
1828
1829 len = recv(ctxt->dataFd, dest, len, 0);
1830#ifdef DEBUG_FTP
1831 xmlGenericError(xmlGenericErrorContext, "Recvd %d bytes\n", len);
1832#endif
1833 if (len <= 0) {
1834 xmlNanoFTPCloseConnection(ctxt);
1835 }
1836 return(len);
1837}
1838
1839/**
1840 * xmlNanoFTPOpen:
1841 * @URL: the URL to the resource
1842 *
1843 * Start to fetch the given ftp:// resource
1844 *
1845 * Returns an FTP context, or NULL
1846 */
1847
1848void*
1849xmlNanoFTPOpen(const char *URL) {
1850 xmlNanoFTPCtxtPtr ctxt;
1851 int sock;
1852
1853 xmlNanoFTPInit();
1854 if (URL == NULL) return(NULL);
1855 if (strncmp("ftp://", URL, 6)) return(NULL);
1856
1857 ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(URL);
1858 if (ctxt == NULL) return(NULL);
1859 if (xmlNanoFTPConnect(ctxt) < 0) {
1860 xmlNanoFTPFreeCtxt(ctxt);
1861 return(NULL);
1862 }
1863 sock = xmlNanoFTPGetSocket(ctxt, ctxt->path);
1864 if (sock < 0) {
1865 xmlNanoFTPFreeCtxt(ctxt);
1866 return(NULL);
1867 }
1868 return(ctxt);
1869}
1870
1871/**
1872 * xmlNanoFTPClose:
1873 * @ctx: an FTP context
1874 *
1875 * Close the connection and both control and transport
1876 *
1877 * Returns -1 incase of error, 0 otherwise
1878 */
1879
1880int
1881xmlNanoFTPClose(void *ctx) {
1882 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1883
1884 if (ctxt == NULL)
1885 return(-1);
1886
1887 if (ctxt->dataFd >= 0) {
1888 closesocket(ctxt->dataFd);
1889 ctxt->dataFd = -1;
1890 }
1891 if (ctxt->controlFd >= 0) {
1892 xmlNanoFTPQuit(ctxt);
1893 closesocket(ctxt->controlFd);
1894 ctxt->controlFd = -1;
1895 }
1896 xmlNanoFTPFreeCtxt(ctxt);
1897 return(0);
1898}
1899
1900#ifdef STANDALONE
1901/************************************************************************
1902 * *
1903 * Basic test in Standalone mode *
1904 * *
1905 ************************************************************************/
Daniel Veillard01c13b52002-12-10 15:19:08 +00001906static
Owen Taylor3473f882001-02-23 17:55:21 +00001907void ftpList(void *userData, const char *filename, const char* attrib,
1908 const char *owner, const char *group, unsigned long size, int links,
1909 int year, const char *month, int day, int hour, int minute) {
1910 xmlGenericError(xmlGenericErrorContext,
1911 "%s %s %s %ld %s\n", attrib, owner, group, size, filename);
1912}
Daniel Veillard01c13b52002-12-10 15:19:08 +00001913static
Owen Taylor3473f882001-02-23 17:55:21 +00001914void ftpData(void *userData, const char *data, int len) {
1915 if (userData == NULL) return;
1916 if (len <= 0) {
1917 fclose(userData);
1918 return;
1919 }
1920 fwrite(data, len, 1, userData);
1921}
1922
1923int main(int argc, char **argv) {
1924 void *ctxt;
1925 FILE *output;
1926 char *tstfile = NULL;
1927
1928 xmlNanoFTPInit();
1929 if (argc > 1) {
1930 ctxt = xmlNanoFTPNewCtxt(argv[1]);
1931 if (xmlNanoFTPConnect(ctxt) < 0) {
1932 xmlGenericError(xmlGenericErrorContext,
1933 "Couldn't connect to %s\n", argv[1]);
1934 exit(1);
1935 }
1936 if (argc > 2)
1937 tstfile = argv[2];
1938 } else
1939 ctxt = xmlNanoFTPConnectTo("localhost", 0);
1940 if (ctxt == NULL) {
1941 xmlGenericError(xmlGenericErrorContext,
1942 "Couldn't connect to localhost\n");
1943 exit(1);
1944 }
1945 xmlNanoFTPList(ctxt, ftpList, NULL, tstfile);
1946 output = fopen("/tmp/tstdata", "w");
1947 if (output != NULL) {
1948 if (xmlNanoFTPGet(ctxt, ftpData, (void *) output, tstfile) < 0)
1949 xmlGenericError(xmlGenericErrorContext,
1950 "Failed to get file\n");
1951
1952 }
1953 xmlNanoFTPClose(ctxt);
1954 xmlMemoryDump();
1955 exit(0);
1956}
1957#endif /* STANDALONE */
1958#else /* !LIBXML_FTP_ENABLED */
1959#ifdef STANDALONE
1960#include <stdio.h>
1961int main(int argc, char **argv) {
1962 xmlGenericError(xmlGenericErrorContext,
1963 "%s : FTP support not compiled in\n", argv[0]);
1964 return(0);
1965}
1966#endif /* STANDALONE */
1967#endif /* LIBXML_FTP_ENABLED */