blob: c7ea79afb1f5be8779298ba4763dd5b598588629 [file] [log] [blame]
Daniel Veillard06047432000-04-24 11:33:38 +00001/*
Daniel Veillard49703262000-07-10 10:27:46 +00002 * nanoftp.c: basic FTP client support
Daniel Veillardda07c342000-01-25 18:31:22 +00003 *
4 * Reference: RFC 959
5 */
6
Daniel Veillard32bc74e2000-07-14 14:49:25 +00007#ifdef TESTING
8#define STANDALONE
9#define HAVE_STDLIB_H
10#define HAVE_UNISTD_H
11#define HAVE_SYS_SOCKET_H
12#define HAVE_NETINET_IN_H
13#define HAVE_NETDB_H
14#define HAVE_SYS_TIME_H
15#else /* STANDALONE */
Daniel Veillardda07c342000-01-25 18:31:22 +000016#ifdef WIN32
Daniel Veillardf341f932000-02-02 14:52:08 +000017#define INCLUDE_WINSOCK
Daniel Veillardda07c342000-01-25 18:31:22 +000018#include "win32config.h"
19#else
20#include "config.h"
21#endif
Daniel Veillard32bc74e2000-07-14 14:49:25 +000022#endif /* STANDALONE */
Daniel Veillard06047432000-04-24 11:33:38 +000023
Daniel Veillardb71379b2000-10-09 12:30:39 +000024#include <libxml/xmlversion.h>
Daniel Veillardda07c342000-01-25 18:31:22 +000025
Daniel Veillard361d8452000-04-03 19:48:13 +000026#ifdef LIBXML_FTP_ENABLED
Daniel Veillardda07c342000-01-25 18:31:22 +000027#include <stdio.h>
28#include <string.h>
29
Daniel Veillard06047432000-04-24 11:33:38 +000030#ifdef HAVE_STDLIB_H
31#include <stdlib.h>
Daniel Veillardda07c342000-01-25 18:31:22 +000032#endif
33#ifdef HAVE_UNISTD_H
34#include <unistd.h>
35#endif
Daniel Veillardda07c342000-01-25 18:31:22 +000036#ifdef HAVE_SYS_SOCKET_H
37#include <sys/socket.h>
38#endif
James Henstridgef3be9312000-01-28 13:59:21 +000039#ifdef HAVE_NETINET_IN_H
40#include <netinet/in.h>
41#endif
Daniel Veillarde41f2b72000-01-30 20:00:07 +000042#ifdef HAVE_ARPA_INET_H
43#include <arpa/inet.h>
44#endif
Daniel Veillardda07c342000-01-25 18:31:22 +000045#ifdef HAVE_NETDB_H
46#include <netdb.h>
47#endif
Daniel Veillarde41f2b72000-01-30 20:00:07 +000048#ifdef HAVE_FCNTL_H
49#include <fcntl.h>
50#endif
51#ifdef HAVE_ERRNO_H
52#include <errno.h>
53#endif
54#ifdef HAVE_SYS_TIME_H
55#include <sys/time.h>
56#endif
57#ifdef HAVE_SYS_SELECT_H
58#include <sys/select.h>
59#endif
Daniel Veillard5feb8492000-02-02 17:15:36 +000060#ifdef HAVE_STRINGS_H
61#include <strings.h>
62#endif
Daniel Veillardda07c342000-01-25 18:31:22 +000063
Daniel Veillard361d8452000-04-03 19:48:13 +000064#include <libxml/xmlmemory.h>
65#include <libxml/nanoftp.h>
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +000066#include <libxml/xmlerror.h>
Daniel Veillardda07c342000-01-25 18:31:22 +000067
68/* #define DEBUG_FTP 1 */
69#ifdef STANDALONE
Daniel Veillarde41f2b72000-01-30 20:00:07 +000070#ifndef DEBUG_FTP
Daniel Veillardda07c342000-01-25 18:31:22 +000071#define DEBUG_FTP 1
72#endif
Daniel Veillarde41f2b72000-01-30 20:00:07 +000073#endif
Daniel Veillardda07c342000-01-25 18:31:22 +000074
Daniel Veillard2f971a22000-10-12 23:26:32 +000075/**
76 * A couple portability macros
77 */
78#ifndef _WINSOCKAPI_
79#define closesocket(s) close(s)
80#define SOCKET int
81#endif
82
Daniel Veillardda07c342000-01-25 18:31:22 +000083static char hostname[100];
84
85#define FTP_COMMAND_OK 200
86#define FTP_SYNTAX_ERROR 500
87#define FTP_GET_PASSWD 331
Daniel Veillard49703262000-07-10 10:27:46 +000088#define FTP_BUF_SIZE 512
Daniel Veillardda07c342000-01-25 18:31:22 +000089
90typedef struct xmlNanoFTPCtxt {
91 char *protocol; /* the protocol name */
92 char *hostname; /* the host name */
93 int port; /* the port */
94 char *path; /* the path within the URL */
95 char *user; /* user string */
96 char *passwd; /* passwd string */
97 struct sockaddr_in ftpAddr; /* the socket address struct */
98 int passive; /* currently we support only passive !!! */
Daniel Veillard2f971a22000-10-12 23:26:32 +000099 SOCKET controlFd; /* the file descriptor for the control socket */
100 SOCKET dataFd; /* the file descriptor for the data socket */
Daniel Veillardda07c342000-01-25 18:31:22 +0000101 int state; /* WRITE / READ / CLOSED */
102 int returnValue; /* the protocol return value */
Daniel Veillard49703262000-07-10 10:27:46 +0000103 /* buffer for data received from the control connection */
104 char controlBuf[FTP_BUF_SIZE + 1];
105 int controlBufIndex;
106 int controlBufUsed;
107 int controlBufAnswer;
Daniel Veillardda07c342000-01-25 18:31:22 +0000108} xmlNanoFTPCtxt, *xmlNanoFTPCtxtPtr;
109
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000110static int initialized = 0;
111static char *proxy = NULL; /* the proxy name if any */
112static int proxyPort = 0; /* the proxy port if any */
113static char *proxyUser = NULL; /* user for proxy authentication */
114static char *proxyPasswd = NULL;/* passwd for proxy authentication */
115static int proxyType = 0; /* uses TYPE or a@b ? */
116
117/**
118 * xmlNanoFTPInit:
119 *
120 * Initialize the FTP protocol layer.
121 * Currently it just checks for proxy informations,
122 * and get the hostname
123 */
124
125void
126xmlNanoFTPInit(void) {
127 const char *env;
Daniel Veillard2f971a22000-10-12 23:26:32 +0000128#ifdef _WINSOCKAPI_
129 WSADATA wsaData;
130#endif
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000131
132 if (initialized)
133 return;
134
Daniel Veillard2f971a22000-10-12 23:26:32 +0000135#ifdef _WINSOCKAPI_
136 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
137 return;
138#endif
139
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000140 gethostname(hostname, sizeof(hostname));
141
142 proxyPort = 21;
143 env = getenv("no_proxy");
144 if (env != NULL)
145 return;
146 env = getenv("ftp_proxy");
147 if (env != NULL) {
148 xmlNanoFTPScanProxy(env);
149 } else {
150 env = getenv("FTP_PROXY");
151 if (env != NULL) {
152 xmlNanoFTPScanProxy(env);
153 }
154 }
155 env = getenv("ftp_proxy_user");
156 if (env != NULL) {
157 proxyUser = xmlMemStrdup(env);
158 }
159 env = getenv("ftp_proxy_password");
160 if (env != NULL) {
161 proxyPasswd = xmlMemStrdup(env);
162 }
163 initialized = 1;
164}
165
166/**
167 * xmlNanoFTPClenup:
168 *
169 * Cleanup the FTP protocol layer. This cleanup proxy informations.
170 */
171
172void
173xmlNanoFTPCleanup(void) {
174 if (proxy != NULL) {
175 xmlFree(proxy);
176 proxy = NULL;
177 }
178 if (proxyUser != NULL) {
179 xmlFree(proxyUser);
180 proxyUser = NULL;
181 }
182 if (proxyPasswd != NULL) {
183 xmlFree(proxyPasswd);
184 proxyPasswd = NULL;
185 }
186 hostname[0] = 0;
Daniel Veillard2f971a22000-10-12 23:26:32 +0000187#ifdef _WINSOCKAPI_
188 if (initialized)
189 WSACleanup();
190#endif
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000191 initialized = 0;
192 return;
193}
194
195/**
196 * xmlNanoFTPProxy:
197 * @host: the proxy host name
198 * @port: the proxy port
199 * @user: the proxy user name
200 * @passwd: the proxy password
201 * @type: the type of proxy 1 for using SITE, 2 for USER a@b
202 *
203 * Setup the FTP proxy informations.
204 * This can also be done by using ftp_proxy ftp_proxy_user and
205 * ftp_proxy_password environment variables.
206 */
207
208void
209xmlNanoFTPProxy(const char *host, int port, const char *user,
210 const char *passwd, int type) {
211 if (proxy != NULL)
212 xmlFree(proxy);
213 if (proxyUser != NULL)
214 xmlFree(proxyUser);
215 if (proxyPasswd != NULL)
216 xmlFree(proxyPasswd);
217 if (host)
218 proxy = xmlMemStrdup(host);
219 if (user)
220 proxyUser = xmlMemStrdup(user);
221 if (passwd)
222 proxyPasswd = xmlMemStrdup(passwd);
223 proxyPort = port;
224 proxyType = type;
225}
226
Daniel Veillardda07c342000-01-25 18:31:22 +0000227/**
228 * xmlNanoFTPScanURL:
229 * @ctx: an FTP context
230 * @URL: The URL used to initialize the context
231 *
232 * (Re)Initialize an FTP context by parsing the URL and finding
233 * the protocol host port and path it indicates.
234 */
235
236static void
237xmlNanoFTPScanURL(void *ctx, const char *URL) {
238 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
239 const char *cur = URL;
240 char buf[4096];
241 int index = 0;
242 int port = 0;
243
244 if (ctxt->protocol != NULL) {
245 xmlFree(ctxt->protocol);
246 ctxt->protocol = NULL;
247 }
248 if (ctxt->hostname != NULL) {
249 xmlFree(ctxt->hostname);
250 ctxt->hostname = NULL;
251 }
252 if (ctxt->path != NULL) {
253 xmlFree(ctxt->path);
254 ctxt->path = NULL;
255 }
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000256 if (URL == NULL) return;
Daniel Veillardda07c342000-01-25 18:31:22 +0000257 buf[index] = 0;
258 while (*cur != 0) {
259 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
260 buf[index] = 0;
261 ctxt->protocol = xmlMemStrdup(buf);
262 index = 0;
263 cur += 3;
264 break;
265 }
266 buf[index++] = *cur++;
267 }
268 if (*cur == 0) return;
269
270 buf[index] = 0;
271 while (1) {
272 if (cur[0] == ':') {
273 buf[index] = 0;
274 ctxt->hostname = xmlMemStrdup(buf);
275 index = 0;
276 cur += 1;
277 while ((*cur >= '0') && (*cur <= '9')) {
278 port *= 10;
279 port += *cur - '0';
280 cur++;
281 }
282 if (port != 0) ctxt->port = port;
283 while ((cur[0] != '/') && (*cur != 0))
284 cur++;
285 break;
286 }
287 if ((*cur == '/') || (*cur == 0)) {
288 buf[index] = 0;
289 ctxt->hostname = xmlMemStrdup(buf);
290 index = 0;
291 break;
292 }
293 buf[index++] = *cur++;
294 }
295 if (*cur == 0)
296 ctxt->path = xmlMemStrdup("/");
297 else {
Daniel Veillard726e8792000-01-30 20:04:29 +0000298 index = 0;
Daniel Veillardda07c342000-01-25 18:31:22 +0000299 buf[index] = 0;
Daniel Veillard726e8792000-01-30 20:04:29 +0000300 while (*cur != 0)
Daniel Veillardda07c342000-01-25 18:31:22 +0000301 buf[index++] = *cur++;
Daniel Veillardda07c342000-01-25 18:31:22 +0000302 buf[index] = 0;
303 ctxt->path = xmlMemStrdup(buf);
304 }
305}
306
307/**
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000308 * xmlNanoFTPUpdateURL:
309 * @ctx: an FTP context
310 * @URL: The URL used to update the context
311 *
312 * Update an FTP context by parsing the URL and finding
313 * new path it indicates. If there is an error in the
314 * protocol, hostname, port or other information, the
315 * error is raised. It indicates a new connection has to
316 * be established.
317 *
318 * Returns 0 if Ok, -1 in case of error (other host).
319 */
320
321int
322xmlNanoFTPUpdateURL(void *ctx, const char *URL) {
323 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
324 const char *cur = URL;
325 char buf[4096];
326 int index = 0;
327 int port = 0;
328
329 if (URL == NULL)
330 return(-1);
331 if (ctxt == NULL)
332 return(-1);
333 if (ctxt->protocol == NULL)
334 return(-1);
335 if (ctxt->hostname == NULL)
336 return(-1);
337 buf[index] = 0;
338 while (*cur != 0) {
339 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
340 buf[index] = 0;
341 if (strcmp(ctxt->protocol, buf))
342 return(-1);
343 index = 0;
344 cur += 3;
345 break;
346 }
347 buf[index++] = *cur++;
348 }
349 if (*cur == 0)
350 return(-1);
351
352 buf[index] = 0;
353 while (1) {
354 if (cur[0] == ':') {
355 buf[index] = 0;
356 if (strcmp(ctxt->hostname, buf))
357 return(-1);
358 index = 0;
359 cur += 1;
360 while ((*cur >= '0') && (*cur <= '9')) {
361 port *= 10;
362 port += *cur - '0';
363 cur++;
364 }
365 if (port != ctxt->port)
366 return(-1);
367 while ((cur[0] != '/') && (*cur != 0))
368 cur++;
369 break;
370 }
371 if ((*cur == '/') || (*cur == 0)) {
372 buf[index] = 0;
373 if (strcmp(ctxt->hostname, buf))
374 return(-1);
375 index = 0;
376 break;
377 }
378 buf[index++] = *cur++;
379 }
380 if (ctxt->path != NULL) {
381 xmlFree(ctxt->path);
382 ctxt->path = NULL;
383 }
384
385 if (*cur == 0)
386 ctxt->path = xmlMemStrdup("/");
387 else {
Daniel Veillard726e8792000-01-30 20:04:29 +0000388 index = 0;
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000389 buf[index] = 0;
Daniel Veillard726e8792000-01-30 20:04:29 +0000390 while (*cur != 0)
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000391 buf[index++] = *cur++;
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000392 buf[index] = 0;
393 ctxt->path = xmlMemStrdup(buf);
394 }
395 return(0);
396}
397
398/**
399 * xmlNanoFTPScanProxy:
400 * @URL: The proxy URL used to initialize the proxy context
401 *
402 * (Re)Initialize the FTP Proxy context by parsing the URL and finding
403 * the protocol host port it indicates.
404 * Should be like ftp://myproxy/ or ftp://myproxy:3128/
405 * A NULL URL cleans up proxy informations.
406 */
407
408void
409xmlNanoFTPScanProxy(const char *URL) {
410 const char *cur = URL;
411 char buf[4096];
412 int index = 0;
413 int port = 0;
414
415 if (proxy != NULL) {
416 xmlFree(proxy);
417 proxy = NULL;
418 }
419 if (proxyPort != 0) {
420 proxyPort = 0;
421 }
422#ifdef DEBUG_FTP
423 if (URL == NULL)
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000424 xmlGenericError(xmlGenericErrorContext, "Removing FTP proxy info\n");
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000425 else
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000426 xmlGenericError(xmlGenericErrorContext, "Using FTP proxy %s\n", URL);
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000427#endif
428 if (URL == NULL) return;
429 buf[index] = 0;
430 while (*cur != 0) {
431 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
432 buf[index] = 0;
433 index = 0;
434 cur += 3;
435 break;
436 }
437 buf[index++] = *cur++;
438 }
439 if (*cur == 0) return;
440
441 buf[index] = 0;
442 while (1) {
443 if (cur[0] == ':') {
444 buf[index] = 0;
445 proxy = xmlMemStrdup(buf);
446 index = 0;
447 cur += 1;
448 while ((*cur >= '0') && (*cur <= '9')) {
449 port *= 10;
450 port += *cur - '0';
451 cur++;
452 }
453 if (port != 0) proxyPort = port;
454 while ((cur[0] != '/') && (*cur != 0))
455 cur++;
456 break;
457 }
458 if ((*cur == '/') || (*cur == 0)) {
459 buf[index] = 0;
460 proxy = xmlMemStrdup(buf);
461 index = 0;
462 break;
463 }
464 buf[index++] = *cur++;
465 }
466}
467
468/**
Daniel Veillardda07c342000-01-25 18:31:22 +0000469 * xmlNanoFTPNewCtxt:
470 * @URL: The URL used to initialize the context
471 *
472 * Allocate and initialize a new FTP context.
473 *
474 * Returns an FTP context or NULL in case of error.
475 */
476
Daniel Veillard06047432000-04-24 11:33:38 +0000477void*
Daniel Veillardda07c342000-01-25 18:31:22 +0000478xmlNanoFTPNewCtxt(const char *URL) {
479 xmlNanoFTPCtxtPtr ret;
480
481 ret = (xmlNanoFTPCtxtPtr) xmlMalloc(sizeof(xmlNanoFTPCtxt));
482 if (ret == NULL) return(NULL);
483
484 memset(ret, 0, sizeof(xmlNanoFTPCtxt));
485 ret->port = 21;
486 ret->passive = 1;
487 ret->returnValue = 0;
Daniel Veillard49703262000-07-10 10:27:46 +0000488 ret->controlBufIndex = 0;
489 ret->controlBufUsed = 0;
Daniel Veillardda07c342000-01-25 18:31:22 +0000490
491 if (URL != NULL)
492 xmlNanoFTPScanURL(ret, URL);
493
494 return(ret);
495}
496
497/**
498 * xmlNanoFTPFreeCtxt:
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000499 * @ctx: an FTP context
Daniel Veillardda07c342000-01-25 18:31:22 +0000500 *
501 * Frees the context after closing the connection.
502 */
503
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000504void
505xmlNanoFTPFreeCtxt(void * ctx) {
506 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
Daniel Veillardda07c342000-01-25 18:31:22 +0000507 if (ctxt == NULL) return;
508 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
509 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
510 if (ctxt->path != NULL) xmlFree(ctxt->path);
511 ctxt->passive = 1;
Daniel Veillard2f971a22000-10-12 23:26:32 +0000512 if (ctxt->controlFd >= 0) closesocket(ctxt->controlFd);
Daniel Veillardda07c342000-01-25 18:31:22 +0000513 ctxt->controlFd = -1;
Daniel Veillard49703262000-07-10 10:27:46 +0000514 ctxt->controlBufIndex = -1;
515 ctxt->controlBufUsed = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +0000516 xmlFree(ctxt);
517}
518
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000519/**
Daniel Veillard06047432000-04-24 11:33:38 +0000520 * xmlNanoFTPParseResponse:
521 * @ctx: the FTP connection context
522 * @buf: the buffer containing the response
523 * @len: the buffer length
524 *
Daniel Veillardda07c342000-01-25 18:31:22 +0000525 * Parsing of the server answer, we just extract the code.
Daniel Veillard06047432000-04-24 11:33:38 +0000526 *
527 * returns 0 for errors
Daniel Veillardda07c342000-01-25 18:31:22 +0000528 * +XXX for last line of response
529 * -XXX for response to be continued
530 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000531static int
Daniel Veillardda07c342000-01-25 18:31:22 +0000532xmlNanoFTPParseResponse(void *ctx, char *buf, int len) {
533 int val = 0;
534
535 if (len < 3) return(-1);
536 if ((*buf >= '0') && (*buf <= '9'))
537 val = val * 10 + (*buf - '0');
538 else
539 return(0);
540 buf++;
541 if ((*buf >= '0') && (*buf <= '9'))
542 val = val * 10 + (*buf - '0');
543 else
544 return(0);
545 buf++;
546 if ((*buf >= '0') && (*buf <= '9'))
547 val = val * 10 + (*buf - '0');
548 else
549 return(0);
550 buf++;
551 if (*buf == '-')
552 return(-val);
553 return(val);
554}
555
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000556/**
Daniel Veillard49703262000-07-10 10:27:46 +0000557 * xmlNanoFTPGetMore:
558 * @ctx: an FTP context
559 *
560 * Read more information from the FTP control connection
561 * Returns the number of bytes read, < 0 indicates an error
562 */
563static int
564xmlNanoFTPGetMore(void *ctx) {
565 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
566 int len;
567 int size;
568
569 if ((ctxt->controlBufIndex < 0) || (ctxt->controlBufIndex > FTP_BUF_SIZE)) {
570#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000571 xmlGenericError(xmlGenericErrorContext,
572 "xmlNanoFTPGetMore : controlBufIndex = %d\n",
Daniel Veillard49703262000-07-10 10:27:46 +0000573 ctxt->controlBufIndex);
574#endif
575 return(-1);
576 }
577
578 if ((ctxt->controlBufUsed < 0) || (ctxt->controlBufUsed > FTP_BUF_SIZE)) {
579#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000580 xmlGenericError(xmlGenericErrorContext,
581 "xmlNanoFTPGetMore : controlBufUsed = %d\n",
Daniel Veillard49703262000-07-10 10:27:46 +0000582 ctxt->controlBufUsed);
583#endif
584 return(-1);
585 }
586 if (ctxt->controlBufIndex > ctxt->controlBufUsed) {
587#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000588 xmlGenericError(xmlGenericErrorContext,
589 "xmlNanoFTPGetMore : controlBufIndex > controlBufUsed %d > %d\n",
Daniel Veillard49703262000-07-10 10:27:46 +0000590 ctxt->controlBufIndex, ctxt->controlBufUsed);
591#endif
592 return(-1);
593 }
594
595 /*
596 * First pack the control buffer
597 */
598 if (ctxt->controlBufIndex > 0) {
599 memmove(&ctxt->controlBuf[0], &ctxt->controlBuf[ctxt->controlBufIndex],
600 ctxt->controlBufUsed - ctxt->controlBufIndex);
601 ctxt->controlBufUsed -= ctxt->controlBufIndex;
602 ctxt->controlBufIndex = 0;
603 }
604 size = FTP_BUF_SIZE - ctxt->controlBufUsed;
605 if (size == 0) {
606#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000607 xmlGenericError(xmlGenericErrorContext,
608 "xmlNanoFTPGetMore : buffer full %d \n", ctxt->controlBufUsed);
Daniel Veillard49703262000-07-10 10:27:46 +0000609#endif
610 return(0);
611 }
612
613 /*
614 * Read the amount left on teh control connection
615 */
616 if ((len = recv(ctxt->controlFd, &ctxt->controlBuf[ctxt->controlBufIndex],
617 size, 0)) < 0) {
Daniel Veillard2f971a22000-10-12 23:26:32 +0000618 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
Daniel Veillard49703262000-07-10 10:27:46 +0000619 ctxt->controlFd = -1;
620 return(-1);
621 }
622#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000623 xmlGenericError(xmlGenericErrorContext,
624 "xmlNanoFTPGetMore : read %d [%d - %d]\n", len,
Daniel Veillard49703262000-07-10 10:27:46 +0000625 ctxt->controlBufUsed, ctxt->controlBufUsed + len);
626#endif
627 ctxt->controlBufUsed += len;
628 ctxt->controlBuf[ctxt->controlBufUsed] = 0;
629
630 return(len);
631}
632
633/**
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000634 * xmlNanoFTPReadResponse:
635 * @ctx: an FTP context
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000636 *
Daniel Veillardda07c342000-01-25 18:31:22 +0000637 * Read the response from the FTP server after a command.
638 * Returns the code number
Daniel Veillardda07c342000-01-25 18:31:22 +0000639 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000640static int
Daniel Veillard49703262000-07-10 10:27:46 +0000641xmlNanoFTPReadResponse(void *ctx) {
Daniel Veillardda07c342000-01-25 18:31:22 +0000642 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
643 char *ptr, *end;
644 int len;
Daniel Veillard49703262000-07-10 10:27:46 +0000645 int res = -1, cur = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +0000646
647get_more:
Daniel Veillard49703262000-07-10 10:27:46 +0000648 /*
649 * Assumes everything up to controlBuf[controlBufIndex] has been read
650 * and analyzed.
651 */
652 len = xmlNanoFTPGetMore(ctx);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000653 if (len < 0) {
654 return(-1);
655 }
Daniel Veillard49703262000-07-10 10:27:46 +0000656 if ((ctxt->controlBufUsed == 0) && (len == 0)) {
Daniel Veillardda07c342000-01-25 18:31:22 +0000657 return(-1);
658 }
Daniel Veillard49703262000-07-10 10:27:46 +0000659 ptr = &ctxt->controlBuf[ctxt->controlBufIndex];
660 end = &ctxt->controlBuf[ctxt->controlBufUsed];
Daniel Veillardda07c342000-01-25 18:31:22 +0000661
Daniel Veillardda07c342000-01-25 18:31:22 +0000662#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000663 xmlGenericError(xmlGenericErrorContext,
664 "\n<<<\n%s\n--\n", ptr);
Daniel Veillardda07c342000-01-25 18:31:22 +0000665#endif
Daniel Veillardda07c342000-01-25 18:31:22 +0000666 while (ptr < end) {
Daniel Veillard49703262000-07-10 10:27:46 +0000667 cur = xmlNanoFTPParseResponse(ctxt, ptr, end - ptr);
668 if (cur > 0) {
669 /*
670 * Successfully scanned the control code, scratch
671 * till the end of the line, but keep the index to be
672 * able to analyze the result if needed.
673 */
674 res = cur;
675 ptr += 3;
676 ctxt->controlBufAnswer = ptr - ctxt->controlBuf;
677 while ((ptr < end) && (*ptr != '\n')) ptr++;
678 if (*ptr == '\n') ptr++;
679 if (*ptr == '\r') ptr++;
680 break;
Daniel Veillardda07c342000-01-25 18:31:22 +0000681 }
682 while ((ptr < end) && (*ptr != '\n')) ptr++;
683 if (ptr >= end) {
Daniel Veillard49703262000-07-10 10:27:46 +0000684 ctxt->controlBufIndex = ctxt->controlBufUsed;
685 goto get_more;
Daniel Veillardda07c342000-01-25 18:31:22 +0000686 }
687 if (*ptr != '\r') ptr++;
688 }
689
690 if (res < 0) goto get_more;
Daniel Veillard49703262000-07-10 10:27:46 +0000691 ctxt->controlBufIndex = ptr - ctxt->controlBuf;
692#ifdef DEBUG_FTP
693 ptr = &ctxt->controlBuf[ctxt->controlBufIndex];
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000694 xmlGenericError(xmlGenericErrorContext, "\n---\n%s\n--\n", ptr);
Daniel Veillard49703262000-07-10 10:27:46 +0000695#endif
Daniel Veillardda07c342000-01-25 18:31:22 +0000696
697#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000698 xmlGenericError(xmlGenericErrorContext, "Got %d\n", res);
Daniel Veillardda07c342000-01-25 18:31:22 +0000699#endif
700 return(res / 100);
701}
702
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000703/**
704 * xmlNanoFTPGetResponse:
705 * @ctx: an FTP context
706 *
Daniel Veillardda07c342000-01-25 18:31:22 +0000707 * Get the response from the FTP server after a command.
708 * Returns the code number
Daniel Veillardda07c342000-01-25 18:31:22 +0000709 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000710
Daniel Veillardda07c342000-01-25 18:31:22 +0000711int
712xmlNanoFTPGetResponse(void *ctx) {
Daniel Veillardda07c342000-01-25 18:31:22 +0000713 int res;
714
Daniel Veillard49703262000-07-10 10:27:46 +0000715 res = xmlNanoFTPReadResponse(ctx);
Daniel Veillardda07c342000-01-25 18:31:22 +0000716
Daniel Veillard49703262000-07-10 10:27:46 +0000717 return(res);
Daniel Veillardda07c342000-01-25 18:31:22 +0000718}
719
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000720/**
721 * xmlNanoFTPCheckResponse:
722 * @ctx: an FTP context
723 *
Daniel Veillardda07c342000-01-25 18:31:22 +0000724 * Check if there is a response from the FTP server after a command.
725 * Returns the code number, or 0
726 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000727
Daniel Veillardda07c342000-01-25 18:31:22 +0000728int
729xmlNanoFTPCheckResponse(void *ctx) {
730 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
Daniel Veillardda07c342000-01-25 18:31:22 +0000731 fd_set rfd;
732 struct timeval tv;
733
734 tv.tv_sec = 0;
735 tv.tv_usec = 0;
736 FD_ZERO(&rfd);
737 FD_SET(ctxt->controlFd, &rfd);
738 switch(select(ctxt->controlFd + 1, &rfd, NULL, NULL, &tv)) {
739 case 0:
740 return(0);
741 case -1:
742#ifdef DEBUG_FTP
743 perror("select");
744#endif
745 return(-1);
746
747 }
748
Daniel Veillard49703262000-07-10 10:27:46 +0000749 return(xmlNanoFTPReadResponse(ctx));
Daniel Veillardda07c342000-01-25 18:31:22 +0000750}
751
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000752/**
Daniel Veillardda07c342000-01-25 18:31:22 +0000753 * Send the user authentification
754 */
755
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000756static int
757xmlNanoFTPSendUser(void *ctx) {
Daniel Veillardda07c342000-01-25 18:31:22 +0000758 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
759 char buf[200];
760 int len;
761 int res;
762
763 if (ctxt->user == NULL)
Daniel Veillard39c7d712000-09-10 16:14:55 +0000764 sprintf(buf, "USER anonymous\r\n");
Daniel Veillardda07c342000-01-25 18:31:22 +0000765 else
Daniel Veillard39c7d712000-09-10 16:14:55 +0000766#ifdef HAVE_SNPRINTF
767 snprintf(buf, sizeof(buf), "USER %s\r\n", ctxt->user);
768#else
769 sprintf(buf, "USER %s\r\n", ctxt->user);
770#endif
771 buf[sizeof(buf) - 1] = 0;
772 len = strlen(buf);
Daniel Veillardda07c342000-01-25 18:31:22 +0000773#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000774 xmlGenericError(xmlGenericErrorContext, buf);
Daniel Veillardda07c342000-01-25 18:31:22 +0000775#endif
776 res = send(ctxt->controlFd, buf, len, 0);
777 if (res < 0) return(res);
778 return(0);
779}
780
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000781/**
Daniel Veillardda07c342000-01-25 18:31:22 +0000782 * Send the password authentification
783 */
784
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000785static int
786xmlNanoFTPSendPasswd(void *ctx) {
Daniel Veillardda07c342000-01-25 18:31:22 +0000787 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
788 char buf[200];
789 int len;
790 int res;
791
792 if (ctxt->passwd == NULL)
Daniel Veillard39c7d712000-09-10 16:14:55 +0000793#ifdef HAVE_SNPRINTF
794 snprintf(buf, sizeof(buf), "PASS libxml@%s\r\n", hostname);
795#else
796 sprintf(buf, "PASS libxml@%s\r\n", hostname);
797#endif
Daniel Veillardda07c342000-01-25 18:31:22 +0000798 else
Daniel Veillard39c7d712000-09-10 16:14:55 +0000799#ifdef HAVE_SNPRINTF
800 snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
801#else
802 sprintf(buf, "PASS %s\r\n", ctxt->passwd);
803#endif
804 buf[sizeof(buf) - 1] = 0;
805 len = strlen(buf);
Daniel Veillardda07c342000-01-25 18:31:22 +0000806#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000807 xmlGenericError(xmlGenericErrorContext, buf);
Daniel Veillardda07c342000-01-25 18:31:22 +0000808#endif
809 res = send(ctxt->controlFd, buf, len, 0);
810 if (res < 0) return(res);
811 return(0);
812}
813
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000814/**
815 * xmlNanoFTPQuit:
816 * @ctx: an FTP context
817 *
818 * Send a QUIT command to the server
819 *
820 * Returns -1 in case of error, 0 otherwise
Daniel Veillardda07c342000-01-25 18:31:22 +0000821 */
822
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000823
Daniel Veillardda07c342000-01-25 18:31:22 +0000824int
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000825xmlNanoFTPQuit(void *ctx) {
Daniel Veillardda07c342000-01-25 18:31:22 +0000826 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
827 char buf[200];
828 int len;
829 int res;
830
Daniel Veillard39c7d712000-09-10 16:14:55 +0000831 sprintf(buf, "QUIT\r\n");
832 len = strlen(buf);
Daniel Veillardda07c342000-01-25 18:31:22 +0000833#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000834 xmlGenericError(xmlGenericErrorContext, buf);
Daniel Veillardda07c342000-01-25 18:31:22 +0000835#endif
836 res = send(ctxt->controlFd, buf, len, 0);
837 return(0);
838}
839
Daniel Veillardaeea04f2000-01-25 19:27:27 +0000840/**
841 * xmlNanoFTPConnect:
842 * @ctx: an FTP context
843 *
844 * Tries to open a control connection
845 *
846 * Returns -1 in case of error, 0 otherwise
Daniel Veillardda07c342000-01-25 18:31:22 +0000847 */
848
849int
850xmlNanoFTPConnect(void *ctx) {
851 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
852 struct hostent *hp;
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000853 int port;
Daniel Veillardda07c342000-01-25 18:31:22 +0000854 int res;
855
856 if (ctxt == NULL)
857 return(-1);
858 if (ctxt->hostname == NULL)
859 return(-1);
860
861 /*
862 * do the blocking DNS query.
863 */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000864 if (proxy)
865 hp = gethostbyname(proxy);
866 else
867 hp = gethostbyname(ctxt->hostname);
Daniel Veillardda07c342000-01-25 18:31:22 +0000868 if (hp == NULL)
869 return(-1);
870
871 /*
872 * Prepare the socket
873 */
874 memset(&ctxt->ftpAddr, 0, sizeof(ctxt->ftpAddr));
875 ctxt->ftpAddr.sin_family = AF_INET;
876 memcpy(&ctxt->ftpAddr.sin_addr, hp->h_addr_list[0], hp->h_length);
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000877 if (proxy) {
878 port = proxyPort;
879 } else {
880 port = ctxt->port;
881 }
882 if (port == 0)
883 port = 21;
884 ctxt->ftpAddr.sin_port = htons(port);
Daniel Veillardda07c342000-01-25 18:31:22 +0000885 ctxt->controlFd = socket(AF_INET, SOCK_STREAM, 0);
886 if (ctxt->controlFd < 0)
887 return(-1);
888
889 /*
890 * Do the connect.
891 */
892 if (connect(ctxt->controlFd, (struct sockaddr *) &ctxt->ftpAddr,
893 sizeof(struct sockaddr_in)) < 0) {
Daniel Veillard2f971a22000-10-12 23:26:32 +0000894 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +0000895 ctxt->controlFd = -1;
896 return(-1);
897 }
898
899 /*
900 * Wait for the HELLO from the server.
901 */
902 res = xmlNanoFTPGetResponse(ctxt);
903 if (res != 2) {
Daniel Veillard2f971a22000-10-12 23:26:32 +0000904 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +0000905 ctxt->controlFd = -1;
906 return(-1);
907 }
908
909 /*
910 * State diagram for the login operation on the FTP server
911 *
912 * Reference: RFC 959
913 *
914 * 1
915 * +---+ USER +---+------------->+---+
916 * | B |---------->| W | 2 ---->| E |
917 * +---+ +---+------ | -->+---+
918 * | | | | |
919 * 3 | | 4,5 | | |
920 * -------------- ----- | | |
921 * | | | | |
922 * | | | | |
923 * | --------- |
924 * | 1| | | |
925 * V | | | |
926 * +---+ PASS +---+ 2 | ------>+---+
927 * | |---------->| W |------------->| S |
928 * +---+ +---+ ---------->+---+
929 * | | | | |
930 * 3 | |4,5| | |
931 * -------------- -------- |
932 * | | | | |
933 * | | | | |
934 * | -----------
935 * | 1,3| | | |
936 * V | 2| | |
937 * +---+ ACCT +---+-- | ----->+---+
938 * | |---------->| W | 4,5 -------->| F |
939 * +---+ +---+------------->+---+
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000940 *
941 * Of course in case of using a proxy this get really nasty and is not
942 * standardized at all :-(
943 */
944 if (proxy) {
945 int len;
946 char buf[400];
947
948 if (proxyUser != NULL) {
949 /*
950 * We need proxy auth
951 */
Daniel Veillard39c7d712000-09-10 16:14:55 +0000952#ifdef HAVE_SNPRINTF
953 snprintf(buf, sizeof(buf), "USER %s\r\n", proxyUser);
954#else
955 sprintf(buf, "USER %s\r\n", proxyUser);
956#endif
957 buf[sizeof(buf) - 1] = 0;
958 len = strlen(buf);
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000959#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000960 xmlGenericError(xmlGenericErrorContext, buf);
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000961#endif
962 res = send(ctxt->controlFd, buf, len, 0);
963 if (res < 0) {
Daniel Veillard2f971a22000-10-12 23:26:32 +0000964 closesocket(ctxt->controlFd);
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000965 ctxt->controlFd = -1;
966 return(res);
967 }
968 res = xmlNanoFTPGetResponse(ctxt);
969 switch (res) {
970 case 2:
971 if (proxyPasswd == NULL)
972 break;
973 case 3:
974 if (proxyPasswd != NULL)
Daniel Veillard39c7d712000-09-10 16:14:55 +0000975#ifdef HAVE_SNPRINTF
976 snprintf(buf, sizeof(buf), "PASS %s\r\n", proxyPasswd);
977#else
978 sprintf(buf, "PASS %s\r\n", proxyPasswd);
979#endif
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000980 else
Daniel Veillard39c7d712000-09-10 16:14:55 +0000981#ifdef HAVE_SNPRINTF
982 snprintf(buf, sizeof(buf), "PASS libxml@%s\r\n",
Daniel Veillardcf461992000-03-14 18:30:20 +0000983 hostname);
Daniel Veillard39c7d712000-09-10 16:14:55 +0000984#else
985 sprintf(buf, "PASS libxml@%s\r\n", hostname);
986#endif
987 buf[sizeof(buf) - 1] = 0;
988 len = strlen(buf);
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000989#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000990 xmlGenericError(xmlGenericErrorContext, buf);
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000991#endif
992 res = send(ctxt->controlFd, buf, len, 0);
993 if (res < 0) {
Daniel Veillard2f971a22000-10-12 23:26:32 +0000994 closesocket(ctxt->controlFd);
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000995 ctxt->controlFd = -1;
996 return(res);
997 }
998 res = xmlNanoFTPGetResponse(ctxt);
999 if (res > 3) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001000 closesocket(ctxt->controlFd);
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001001 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:
Daniel Veillard2f971a22000-10-12 23:26:32 +00001011 closesocket(ctxt->controlFd);
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001012 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:
1023 /* we will try in seqence */
1024 case 1:
1025 /* Using SITE command */
Daniel Veillard39c7d712000-09-10 16:14:55 +00001026#ifdef HAVE_SNPRINTF
1027 snprintf(buf, sizeof(buf), "SITE %s\r\n", ctxt->hostname);
1028#else
1029 sprintf(buf, "SITE %s\r\n", ctxt->hostname);
1030#endif
1031 buf[sizeof(buf) - 1] = 0;
1032 len = strlen(buf);
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001033#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001034 xmlGenericError(xmlGenericErrorContext, buf);
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001035#endif
1036 res = send(ctxt->controlFd, buf, len, 0);
1037 if (res < 0) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001038 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001039 ctxt->controlFd = -1;
1040 return(res);
1041 }
1042 res = xmlNanoFTPGetResponse(ctxt);
1043 if (res == 2) {
1044 /* we assume it worked :-\ 1 is error for SITE command */
1045 proxyType = 1;
1046 break;
1047 }
1048 if (proxyType == 1) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001049 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001050 ctxt->controlFd = -1;
1051 return(-1);
1052 }
1053 case 2:
1054 /* USER user@host command */
1055 if (ctxt->user == NULL)
Daniel Veillard39c7d712000-09-10 16:14:55 +00001056#ifdef HAVE_SNPRINTF
1057 snprintf(buf, sizeof(buf), "USER anonymous@%s\r\n",
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001058 ctxt->hostname);
Daniel Veillard39c7d712000-09-10 16:14:55 +00001059#else
1060 sprintf(buf, "USER anonymous@%s\r\n", ctxt->hostname);
1061#endif
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001062 else
Daniel Veillard39c7d712000-09-10 16:14:55 +00001063#ifdef HAVE_SNPRINTF
1064 snprintf(buf, sizeof(buf), "USER %s@%s\r\n",
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001065 ctxt->user, ctxt->hostname);
Daniel Veillard39c7d712000-09-10 16:14:55 +00001066#else
1067 sprintf(buf, "USER %s@%s\r\n",
1068 ctxt->user, ctxt->hostname);
1069#endif
1070 buf[sizeof(buf) - 1] = 0;
1071 len = strlen(buf);
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001072#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001073 xmlGenericError(xmlGenericErrorContext, buf);
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001074#endif
1075 res = send(ctxt->controlFd, buf, len, 0);
1076 if (res < 0) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001077 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001078 ctxt->controlFd = -1;
1079 return(res);
1080 }
1081 res = xmlNanoFTPGetResponse(ctxt);
1082 if ((res == 1) || (res == 2)) {
1083 /* we assume it worked :-\ */
1084 proxyType = 2;
1085 return(0);
1086 }
1087 if (ctxt->passwd == NULL)
Daniel Veillard39c7d712000-09-10 16:14:55 +00001088#ifdef HAVE_SNPRINTF
1089 snprintf(buf, sizeof(buf), "PASS libxml@%s\r\n", hostname);
1090#else
1091 sprintf(buf, "PASS libxml@%s\r\n", hostname);
1092#endif
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001093 else
Daniel Veillard39c7d712000-09-10 16:14:55 +00001094#ifdef HAVE_SNPRINTF
1095 snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
1096#else
1097 sprintf(buf, "PASS %s\r\n", ctxt->passwd);
1098#endif
1099 buf[sizeof(buf) - 1] = 0;
1100 len = strlen(buf);
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001101#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001102 xmlGenericError(xmlGenericErrorContext, buf);
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001103#endif
1104 res = send(ctxt->controlFd, buf, len, 0);
1105 if (res < 0) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001106 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001107 ctxt->controlFd = -1;
1108 return(res);
1109 }
1110 res = xmlNanoFTPGetResponse(ctxt);
1111 if ((res == 1) || (res == 2)) {
1112 /* we assume it worked :-\ */
1113 proxyType = 2;
1114 return(0);
1115 }
1116 if (proxyType == 2) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001117 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001118 ctxt->controlFd = -1;
1119 return(-1);
1120 }
1121 case 3:
1122 /*
1123 * If you need support for other Proxy authentication scheme
1124 * send the code or at least the sequence in use.
1125 */
1126 default:
Daniel Veillard2f971a22000-10-12 23:26:32 +00001127 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001128 ctxt->controlFd = -1;
1129 return(-1);
1130 }
1131 }
1132 /*
1133 * Non-proxy handling.
Daniel Veillardda07c342000-01-25 18:31:22 +00001134 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001135 res = xmlNanoFTPSendUser(ctxt);
Daniel Veillardda07c342000-01-25 18:31:22 +00001136 if (res < 0) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001137 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001138 ctxt->controlFd = -1;
1139 return(-1);
1140 }
1141 res = xmlNanoFTPGetResponse(ctxt);
1142 switch (res) {
1143 case 2:
1144 return(0);
1145 case 3:
1146 break;
1147 case 1:
1148 case 4:
1149 case 5:
1150 case -1:
1151 default:
Daniel Veillard2f971a22000-10-12 23:26:32 +00001152 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001153 ctxt->controlFd = -1;
1154 return(-1);
1155 }
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001156 res = xmlNanoFTPSendPasswd(ctxt);
Daniel Veillardda07c342000-01-25 18:31:22 +00001157 if (res < 0) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001158 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001159 ctxt->controlFd = -1;
1160 return(-1);
1161 }
1162 res = xmlNanoFTPGetResponse(ctxt);
1163 switch (res) {
1164 case 2:
Daniel Veillard5feb8492000-02-02 17:15:36 +00001165 break;
Daniel Veillardda07c342000-01-25 18:31:22 +00001166 case 3:
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001167 xmlGenericError(xmlGenericErrorContext,
1168 "FTP server asking for ACCNT on anonymous\n");
Daniel Veillardda07c342000-01-25 18:31:22 +00001169 case 1:
1170 case 4:
1171 case 5:
1172 case -1:
1173 default:
Daniel Veillard2f971a22000-10-12 23:26:32 +00001174 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001175 ctxt->controlFd = -1;
1176 return(-1);
1177 }
1178
1179 return(0);
1180}
1181
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001182/**
1183 * xmlNanoFTPConnectTo:
1184 * @server: an FTP server name
Daniel Veillard06047432000-04-24 11:33:38 +00001185 * @port: the port (use 21 if 0)
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001186 *
1187 * Tries to open a control connection to the given server/port
1188 *
Daniel Veillard06047432000-04-24 11:33:38 +00001189 * Returns an fTP context or NULL if it failed
Daniel Veillardda07c342000-01-25 18:31:22 +00001190 */
1191
Daniel Veillard06047432000-04-24 11:33:38 +00001192void*
Daniel Veillardda07c342000-01-25 18:31:22 +00001193xmlNanoFTPConnectTo(const char *server, int port) {
1194 xmlNanoFTPCtxtPtr ctxt;
1195 int res;
1196
1197 xmlNanoFTPInit();
1198 if (server == NULL)
1199 return(NULL);
Daniel Veillard49703262000-07-10 10:27:46 +00001200 ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(NULL);
Daniel Veillardda07c342000-01-25 18:31:22 +00001201 ctxt->hostname = xmlMemStrdup(server);
1202 if (port != 0)
1203 ctxt->port = port;
1204 res = xmlNanoFTPConnect(ctxt);
1205 if (res < 0) {
1206 xmlNanoFTPFreeCtxt(ctxt);
1207 return(NULL);
1208 }
1209 return(ctxt);
1210}
1211
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001212/**
Daniel Veillard49703262000-07-10 10:27:46 +00001213 * xmlNanoFTPCwd:
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001214 * @ctx: an FTP context
1215 * @directory: a directory on the server
1216 *
1217 * Tries to change the remote directory
1218 *
1219 * Returns -1 incase of error, 1 if CWD worked, 0 if it failed
Daniel Veillardda07c342000-01-25 18:31:22 +00001220 */
1221
1222int
1223xmlNanoFTPCwd(void *ctx, char *directory) {
1224 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1225 char buf[400];
1226 int len;
1227 int res;
1228
1229 /*
1230 * Expected response code for CWD:
1231 *
1232 * CWD
1233 * 250
1234 * 500, 501, 502, 421, 530, 550
1235 */
Daniel Veillard39c7d712000-09-10 16:14:55 +00001236#ifdef HAVE_SNPRINTF
1237 snprintf(buf, sizeof(buf), "CWD %s\r\n", directory);
1238#else
1239 sprintf(buf, "CWD %s\r\n", directory);
1240#endif
1241 buf[sizeof(buf) - 1] = 0;
1242 len = strlen(buf);
Daniel Veillardda07c342000-01-25 18:31:22 +00001243#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001244 xmlGenericError(xmlGenericErrorContext, buf);
Daniel Veillardda07c342000-01-25 18:31:22 +00001245#endif
1246 res = send(ctxt->controlFd, buf, len, 0);
1247 if (res < 0) return(res);
1248 res = xmlNanoFTPGetResponse(ctxt);
1249 if (res == 4) {
Daniel Veillardda07c342000-01-25 18:31:22 +00001250 return(-1);
1251 }
1252 if (res == 2) return(1);
1253 if (res == 5) {
1254 return(0);
1255 }
1256 return(0);
1257}
1258
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001259/**
1260 * xmlNanoFTPGetConnection:
1261 * @ctx: an FTP context
1262 *
1263 * Try to open a data connection to the server. Currently only
1264 * passive mode is supported.
1265 *
1266 * Returns -1 incase of error, 0 otherwise
Daniel Veillardda07c342000-01-25 18:31:22 +00001267 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001268
Daniel Veillardda07c342000-01-25 18:31:22 +00001269int
1270xmlNanoFTPGetConnection(void *ctx) {
1271 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1272 char buf[200], *cur;
1273 int len, i;
1274 int res;
1275 unsigned char ad[6], *adp, *portp;
1276 unsigned int temp[6];
1277 struct sockaddr_in dataAddr;
Daniel Veillardb0426ca2000-10-11 23:39:43 +00001278 SOCKLEN_T dataAddrLen;
Daniel Veillardda07c342000-01-25 18:31:22 +00001279
1280 ctxt->dataFd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1281 if (ctxt->dataFd < 0) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001282 xmlGenericError(xmlGenericErrorContext,
1283 "xmlNanoFTPGetConnection: failed to create socket\n");
Daniel Veillard2f971a22000-10-12 23:26:32 +00001284 return(-1);
Daniel Veillardda07c342000-01-25 18:31:22 +00001285 }
1286 dataAddrLen = sizeof(dataAddr);
1287 memset(&dataAddr, 0, dataAddrLen);
1288 dataAddr.sin_family = AF_INET;
1289
1290 if (ctxt->passive) {
Daniel Veillard39c7d712000-09-10 16:14:55 +00001291 sprintf(buf, "PASV\r\n");
1292 len = strlen(buf);
Daniel Veillardda07c342000-01-25 18:31:22 +00001293#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001294 xmlGenericError(xmlGenericErrorContext, buf);
Daniel Veillardda07c342000-01-25 18:31:22 +00001295#endif
1296 res = send(ctxt->controlFd, buf, len, 0);
1297 if (res < 0) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001298 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001299 return(res);
1300 }
Daniel Veillard49703262000-07-10 10:27:46 +00001301 res = xmlNanoFTPReadResponse(ctx);
Daniel Veillardda07c342000-01-25 18:31:22 +00001302 if (res != 2) {
1303 if (res == 5) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001304 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001305 return(-1);
1306 } else {
1307 /*
1308 * retry with an active connection
1309 */
Daniel Veillard2f971a22000-10-12 23:26:32 +00001310 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001311 ctxt->passive = 0;
1312 }
1313 }
Daniel Veillard49703262000-07-10 10:27:46 +00001314 cur = &ctxt->controlBuf[ctxt->controlBufAnswer];
Daniel Veillardda07c342000-01-25 18:31:22 +00001315 while (((*cur < '0') || (*cur > '9')) && *cur != '\0') cur++;
Daniel Veillardbf432752000-11-12 15:56:56 +00001316 if (sscanf(cur, "%u,%u,%u,%u,%u,%u", &temp[0], &temp[1], &temp[2],
Daniel Veillardda07c342000-01-25 18:31:22 +00001317 &temp[3], &temp[4], &temp[5]) != 6) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001318 xmlGenericError(xmlGenericErrorContext,
1319 "Invalid answer to PASV\n");
Daniel Veillardbe803962000-06-28 23:40:59 +00001320 if (ctxt->dataFd != -1) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001321 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardbe803962000-06-28 23:40:59 +00001322 }
Daniel Veillardda07c342000-01-25 18:31:22 +00001323 return(-1);
1324 }
1325 for (i=0; i<6; i++) ad[i] = (unsigned char) (temp[i] & 0xff);
1326 memcpy(&dataAddr.sin_addr, &ad[0], 4);
1327 memcpy(&dataAddr.sin_port, &ad[4], 2);
1328 if (connect(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001329 xmlGenericError(xmlGenericErrorContext,
1330 "Failed to create a data connection\n");
Daniel Veillard2f971a22000-10-12 23:26:32 +00001331 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001332 return (-1);
1333 }
1334 } else {
1335 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
1336 dataAddr.sin_port = 0;
1337 if (bind(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001338 xmlGenericError(xmlGenericErrorContext,
1339 "Failed to bind a port\n");
Daniel Veillard2f971a22000-10-12 23:26:32 +00001340 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001341 return (-1);
1342 }
1343 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
1344
1345 if (listen(ctxt->dataFd, 1) < 0) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001346 xmlGenericError(xmlGenericErrorContext,
1347 "Could not listen on port %d\n",
Daniel Veillardda07c342000-01-25 18:31:22 +00001348 ntohs(dataAddr.sin_port));
Daniel Veillard2f971a22000-10-12 23:26:32 +00001349 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001350 return (-1);
1351 }
1352 adp = (unsigned char *) &dataAddr.sin_addr;
1353 portp = (unsigned char *) &dataAddr.sin_port;
Daniel Veillard39c7d712000-09-10 16:14:55 +00001354#ifdef HAVE_SNPRINTF
1355 snprintf(buf, sizeof(buf), "PORT %d,%d,%d,%d,%d,%d\r\n",
Daniel Veillardcf461992000-03-14 18:30:20 +00001356 adp[0] & 0xff, adp[1] & 0xff, adp[2] & 0xff, adp[3] & 0xff,
1357 portp[0] & 0xff, portp[1] & 0xff);
Daniel Veillard39c7d712000-09-10 16:14:55 +00001358#else
1359 sprintf(buf, "PORT %d,%d,%d,%d,%d,%d\r\n",
Daniel Veillardcf461992000-03-14 18:30:20 +00001360 adp[0] & 0xff, adp[1] & 0xff, adp[2] & 0xff, adp[3] & 0xff,
1361 portp[0] & 0xff, portp[1] & 0xff);
Daniel Veillard39c7d712000-09-10 16:14:55 +00001362#endif
Daniel Veillardda07c342000-01-25 18:31:22 +00001363 buf[sizeof(buf) - 1] = 0;
Daniel Veillard39c7d712000-09-10 16:14:55 +00001364 len = strlen(buf);
Daniel Veillardda07c342000-01-25 18:31:22 +00001365#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001366 xmlGenericError(xmlGenericErrorContext, buf);
Daniel Veillardda07c342000-01-25 18:31:22 +00001367#endif
1368
1369 res = send(ctxt->controlFd, buf, len, 0);
1370 if (res < 0) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001371 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001372 return(res);
1373 }
1374 res = xmlNanoFTPGetResponse(ctxt);
1375 if (res != 2) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001376 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001377 return(-1);
1378 }
1379 }
1380 return(ctxt->dataFd);
1381
1382}
1383
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001384/**
1385 * xmlNanoFTPCloseConnection:
1386 * @ctx: an FTP context
1387 *
1388 * Close the data connection from the server
1389 *
1390 * Returns -1 incase of error, 0 otherwise
Daniel Veillardda07c342000-01-25 18:31:22 +00001391 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001392
Daniel Veillardda07c342000-01-25 18:31:22 +00001393int
1394xmlNanoFTPCloseConnection(void *ctx) {
1395 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1396 int res;
Daniel Veillardcf461992000-03-14 18:30:20 +00001397 fd_set rfd, efd;
1398 struct timeval tv;
Daniel Veillardda07c342000-01-25 18:31:22 +00001399
Daniel Veillard2f971a22000-10-12 23:26:32 +00001400 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardcf461992000-03-14 18:30:20 +00001401 tv.tv_sec = 15;
1402 tv.tv_usec = 0;
1403 FD_ZERO(&rfd);
1404 FD_SET(ctxt->controlFd, &rfd);
1405 FD_ZERO(&efd);
1406 FD_SET(ctxt->controlFd, &efd);
1407 res = select(ctxt->controlFd + 1, &rfd, NULL, &efd, &tv);
1408 if (res < 0) {
1409#ifdef DEBUG_FTP
1410 perror("select");
1411#endif
Daniel Veillard2f971a22000-10-12 23:26:32 +00001412 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001413 return(-1);
1414 }
Daniel Veillardcf461992000-03-14 18:30:20 +00001415 if (res == 0) {
Daniel Veillard2f2bf412000-08-20 15:11:02 +00001416#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001417 xmlGenericError(xmlGenericErrorContext,
1418 "xmlNanoFTPCloseConnection: timeout\n");
Daniel Veillard2f2bf412000-08-20 15:11:02 +00001419#endif
Daniel Veillard2f971a22000-10-12 23:26:32 +00001420 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
Daniel Veillardcf461992000-03-14 18:30:20 +00001421 } else {
1422 res = xmlNanoFTPGetResponse(ctxt);
1423 if (res != 2) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001424 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
Daniel Veillardcf461992000-03-14 18:30:20 +00001425 return(-1);
1426 }
1427 }
Daniel Veillardda07c342000-01-25 18:31:22 +00001428 return(0);
1429}
1430
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001431/**
1432 * xmlNanoFTPParseList:
1433 * @list: some data listing received from the server
1434 * @callback: the user callback
1435 * @userData: the user callback data
1436 *
1437 * Parse at most one entry from the listing.
1438 *
1439 * Returns -1 incase of error, the lenght of data parsed otherwise
Daniel Veillardda07c342000-01-25 18:31:22 +00001440 */
1441
1442static int
1443xmlNanoFTPParseList(const char *list, ftpListCallback callback, void *userData) {
1444 const char *cur = list;
1445 char filename[151];
1446 char attrib[11];
1447 char owner[11];
1448 char group[11];
1449 char month[4];
1450 int year = 0;
1451 int minute = 0;
1452 int hour = 0;
1453 int day = 0;
1454 unsigned long size = 0;
1455 int links = 0;
1456 int i;
1457
1458 if (!strncmp(cur, "total", 5)) {
1459 cur += 5;
1460 while (*cur == ' ') cur++;
1461 while ((*cur >= '0') && (*cur <= '9'))
1462 links = (links * 10) + (*cur++ - '0');
1463 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1464 cur++;
1465 return(cur - list);
1466 } else if (*list == '+') {
1467 return(0);
1468 } else {
1469 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1470 cur++;
1471 if (*cur == 0) return(0);
1472 i = 0;
1473 while (*cur != ' ') {
1474 if (i < 10)
1475 attrib[i++] = *cur;
1476 cur++;
1477 if (*cur == 0) return(0);
1478 }
1479 attrib[10] = 0;
1480 while (*cur == ' ') cur++;
1481 if (*cur == 0) return(0);
1482 while ((*cur >= '0') && (*cur <= '9'))
1483 links = (links * 10) + (*cur++ - '0');
1484 while (*cur == ' ') cur++;
1485 if (*cur == 0) return(0);
1486 i = 0;
1487 while (*cur != ' ') {
1488 if (i < 10)
1489 owner[i++] = *cur;
1490 cur++;
1491 if (*cur == 0) return(0);
1492 }
1493 owner[i] = 0;
1494 while (*cur == ' ') cur++;
1495 if (*cur == 0) return(0);
1496 i = 0;
1497 while (*cur != ' ') {
1498 if (i < 10)
1499 group[i++] = *cur;
1500 cur++;
1501 if (*cur == 0) return(0);
1502 }
1503 group[i] = 0;
1504 while (*cur == ' ') cur++;
1505 if (*cur == 0) return(0);
1506 while ((*cur >= '0') && (*cur <= '9'))
1507 size = (size * 10) + (*cur++ - '0');
1508 while (*cur == ' ') cur++;
1509 if (*cur == 0) return(0);
1510 i = 0;
1511 while (*cur != ' ') {
1512 if (i < 3)
1513 month[i++] = *cur;
1514 cur++;
1515 if (*cur == 0) return(0);
1516 }
1517 month[i] = 0;
1518 while (*cur == ' ') cur++;
1519 if (*cur == 0) return(0);
1520 while ((*cur >= '0') && (*cur <= '9'))
1521 day = (day * 10) + (*cur++ - '0');
1522 while (*cur == ' ') cur++;
1523 if (*cur == 0) return(0);
1524 if ((cur[1] == 0) || (cur[2] == 0)) return(0);
1525 if ((cur[1] == ':') || (cur[2] == ':')) {
1526 while ((*cur >= '0') && (*cur <= '9'))
1527 hour = (hour * 10) + (*cur++ - '0');
1528 if (*cur == ':') cur++;
1529 while ((*cur >= '0') && (*cur <= '9'))
1530 minute = (minute * 10) + (*cur++ - '0');
1531 } else {
1532 while ((*cur >= '0') && (*cur <= '9'))
1533 year = (year * 10) + (*cur++ - '0');
1534 }
1535 while (*cur == ' ') cur++;
1536 if (*cur == 0) return(0);
1537 i = 0;
1538 while ((*cur != '\n') && (*cur != '\r')) {
1539 if (i < 150)
1540 filename[i++] = *cur;
1541 cur++;
1542 if (*cur == 0) return(0);
1543 }
1544 filename[i] = 0;
1545 if ((*cur != '\n') && (*cur != '\r'))
1546 return(0);
1547 while ((*cur == '\n') || (*cur == '\r'))
1548 cur++;
1549 }
1550 if (callback != NULL) {
1551 callback(userData, filename, attrib, owner, group, size, links,
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001552 year, month, day, hour, minute);
Daniel Veillardda07c342000-01-25 18:31:22 +00001553 }
1554 return(cur - list);
1555}
1556
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001557/**
1558 * xmlNanoFTPList:
1559 * @ctx: an FTP context
1560 * @callback: the user callback
1561 * @userData: the user callback data
1562 * @filename: optional files to list
1563 *
1564 * Do a listing on the server. All files info are passed back
1565 * in the callbacks.
1566 *
1567 * Returns -1 incase of error, 0 otherwise
Daniel Veillardda07c342000-01-25 18:31:22 +00001568 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001569
Daniel Veillardda07c342000-01-25 18:31:22 +00001570int
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001571xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData,
1572 char *filename) {
Daniel Veillardda07c342000-01-25 18:31:22 +00001573 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1574 char buf[4096 + 1];
1575 int len, res;
1576 int index = 0, base;
1577 fd_set rfd, efd;
1578 struct timeval tv;
1579
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001580 if (filename == NULL) {
1581 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1582 return(-1);
1583 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
Daniel Veillardbe803962000-06-28 23:40:59 +00001584 if (ctxt->dataFd == -1)
1585 return(-1);
Daniel Veillard39c7d712000-09-10 16:14:55 +00001586 sprintf(buf, "LIST -L\r\n");
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001587 } else {
1588 if (filename[0] != '/') {
1589 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1590 return(-1);
1591 }
1592 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
Daniel Veillardbe803962000-06-28 23:40:59 +00001593 if (ctxt->dataFd == -1)
1594 return(-1);
Daniel Veillard39c7d712000-09-10 16:14:55 +00001595#ifdef HAVE_SNPRINTF
1596 snprintf(buf, sizeof(buf), "LIST -L %s\r\n", filename);
1597#else
1598 sprintf(buf, "LIST -L %s\r\n", filename);
1599#endif
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001600 }
Daniel Veillard39c7d712000-09-10 16:14:55 +00001601 buf[sizeof(buf) - 1] = 0;
1602 len = strlen(buf);
Daniel Veillardda07c342000-01-25 18:31:22 +00001603#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001604 xmlGenericError(xmlGenericErrorContext, buf);
Daniel Veillardda07c342000-01-25 18:31:22 +00001605#endif
1606 res = send(ctxt->controlFd, buf, len, 0);
1607 if (res < 0) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001608 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001609 return(res);
1610 }
Daniel Veillard49703262000-07-10 10:27:46 +00001611 res = xmlNanoFTPReadResponse(ctxt);
Daniel Veillardda07c342000-01-25 18:31:22 +00001612 if (res != 1) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001613 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001614 return(-res);
1615 }
1616
1617 do {
1618 tv.tv_sec = 1;
1619 tv.tv_usec = 0;
1620 FD_ZERO(&rfd);
1621 FD_SET(ctxt->dataFd, &rfd);
1622 FD_ZERO(&efd);
1623 FD_SET(ctxt->dataFd, &efd);
1624 res = select(ctxt->dataFd + 1, &rfd, NULL, &efd, &tv);
1625 if (res < 0) {
1626#ifdef DEBUG_FTP
1627 perror("select");
1628#endif
Daniel Veillard2f971a22000-10-12 23:26:32 +00001629 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001630 return(-1);
1631 }
1632 if (res == 0) {
1633 res = xmlNanoFTPCheckResponse(ctxt);
1634 if (res < 0) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001635 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001636 ctxt->dataFd = -1;
1637 return(-1);
1638 }
1639 if (res == 2) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001640 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001641 return(0);
1642 }
1643
1644 continue;
1645 }
1646
Daniel Veillard2f971a22000-10-12 23:26:32 +00001647 if ((len = recv(ctxt->dataFd, &buf[index], sizeof(buf) - (index + 1), 0)) < 0) {
Daniel Veillardda07c342000-01-25 18:31:22 +00001648#ifdef DEBUG_FTP
Daniel Veillard2f971a22000-10-12 23:26:32 +00001649 perror("recv");
Daniel Veillardda07c342000-01-25 18:31:22 +00001650#endif
Daniel Veillard2f971a22000-10-12 23:26:32 +00001651 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001652 ctxt->dataFd = -1;
1653 return(-1);
1654 }
1655#ifdef DEBUG_FTP
1656 write(1, &buf[index], len);
1657#endif
1658 index += len;
1659 buf[index] = 0;
1660 base = 0;
1661 do {
1662 res = xmlNanoFTPParseList(&buf[base], callback, userData);
1663 base += res;
1664 } while (res > 0);
1665
1666 memmove(&buf[0], &buf[base], index - base);
1667 index -= base;
1668 } while (len != 0);
1669 xmlNanoFTPCloseConnection(ctxt);
1670 return(0);
1671}
1672
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001673/**
Daniel Veillardda07c342000-01-25 18:31:22 +00001674 * xmlNanoFTPGetSocket:
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001675 * @ctx: an FTP context
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001676 * @filename: the file to retrieve (or NULL if path is in context).
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001677 *
1678 * Initiate fetch of the given file from the server.
1679 *
1680 * Returns the socket for the data connection, or <0 in case of error
Daniel Veillardda07c342000-01-25 18:31:22 +00001681 */
1682
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001683
Daniel Veillardda07c342000-01-25 18:31:22 +00001684int
1685xmlNanoFTPGetSocket(void *ctx, const char *filename) {
1686 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1687 char buf[300];
1688 int res, len;
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001689 if ((filename == NULL) && (ctxt->path == NULL))
Daniel Veillardda07c342000-01-25 18:31:22 +00001690 return(-1);
1691 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
Daniel Veillardbe803962000-06-28 23:40:59 +00001692 if (ctxt->dataFd == -1)
1693 return(-1);
Daniel Veillardda07c342000-01-25 18:31:22 +00001694
Daniel Veillard39c7d712000-09-10 16:14:55 +00001695 sprintf(buf, "TYPE I\r\n");
1696 len = strlen(buf);
Daniel Veillardda07c342000-01-25 18:31:22 +00001697#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001698 xmlGenericError(xmlGenericErrorContext, buf);
Daniel Veillardda07c342000-01-25 18:31:22 +00001699#endif
1700 res = send(ctxt->controlFd, buf, len, 0);
1701 if (res < 0) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001702 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001703 return(res);
1704 }
Daniel Veillard49703262000-07-10 10:27:46 +00001705 res = xmlNanoFTPReadResponse(ctxt);
Daniel Veillardda07c342000-01-25 18:31:22 +00001706 if (res != 2) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001707 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001708 return(-res);
1709 }
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001710 if (filename == NULL)
Daniel Veillard39c7d712000-09-10 16:14:55 +00001711#ifdef HAVE_SNPRINTF
1712 snprintf(buf, sizeof(buf), "RETR %s\r\n", ctxt->path);
1713#else
1714 sprintf(buf, "RETR %s\r\n", ctxt->path);
1715#endif
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001716 else
Daniel Veillard39c7d712000-09-10 16:14:55 +00001717#ifdef HAVE_SNPRINTF
1718 snprintf(buf, sizeof(buf), "RETR %s\r\n", filename);
1719#else
1720 sprintf(buf, "RETR %s\r\n", filename);
1721#endif
1722 buf[sizeof(buf) - 1] = 0;
1723 len = strlen(buf);
Daniel Veillardda07c342000-01-25 18:31:22 +00001724#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001725 xmlGenericError(xmlGenericErrorContext, buf);
Daniel Veillardda07c342000-01-25 18:31:22 +00001726#endif
1727 res = send(ctxt->controlFd, buf, len, 0);
1728 if (res < 0) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001729 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001730 return(res);
1731 }
Daniel Veillard49703262000-07-10 10:27:46 +00001732 res = xmlNanoFTPReadResponse(ctxt);
Daniel Veillardda07c342000-01-25 18:31:22 +00001733 if (res != 1) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001734 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001735 return(-res);
1736 }
1737 return(ctxt->dataFd);
1738}
1739
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001740/**
1741 * xmlNanoFTPGet:
1742 * @ctx: an FTP context
1743 * @callback: the user callback
1744 * @userData: the user callback data
1745 * @filename: the file to retrieve
1746 *
1747 * Fetch the given file from the server. All data are passed back
1748 * in the callbacks. The last callback has a size of 0 block.
1749 *
1750 * Returns -1 incase of error, 0 otherwise
Daniel Veillardda07c342000-01-25 18:31:22 +00001751 */
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001752
Daniel Veillardda07c342000-01-25 18:31:22 +00001753int
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001754xmlNanoFTPGet(void *ctx, ftpDataCallback callback, void *userData,
1755 const char *filename) {
Daniel Veillardda07c342000-01-25 18:31:22 +00001756 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1757 char buf[4096];
1758 int len = 0, res;
1759 fd_set rfd;
1760 struct timeval tv;
1761
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001762 if ((filename == NULL) && (ctxt->path == NULL))
Daniel Veillardda07c342000-01-25 18:31:22 +00001763 return(-1);
1764 if (callback == NULL)
1765 return(-1);
1766 if (xmlNanoFTPGetSocket(ctxt, filename) < 0)
1767 return(-1);
1768
1769 do {
1770 tv.tv_sec = 1;
1771 tv.tv_usec = 0;
1772 FD_ZERO(&rfd);
1773 FD_SET(ctxt->dataFd, &rfd);
1774 res = select(ctxt->dataFd + 1, &rfd, NULL, NULL, &tv);
1775 if (res < 0) {
1776#ifdef DEBUG_FTP
1777 perror("select");
1778#endif
Daniel Veillard2f971a22000-10-12 23:26:32 +00001779 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001780 return(-1);
1781 }
1782 if (res == 0) {
1783 res = xmlNanoFTPCheckResponse(ctxt);
1784 if (res < 0) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001785 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001786 ctxt->dataFd = -1;
1787 return(-1);
1788 }
1789 if (res == 2) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001790 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001791 return(0);
1792 }
1793
1794 continue;
1795 }
Daniel Veillard2f971a22000-10-12 23:26:32 +00001796 if ((len = recv(ctxt->dataFd, buf, sizeof(buf), 0)) < 0) {
Daniel Veillardda07c342000-01-25 18:31:22 +00001797 callback(userData, buf, len);
Daniel Veillard2f971a22000-10-12 23:26:32 +00001798 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
Daniel Veillardda07c342000-01-25 18:31:22 +00001799 return(-1);
1800 }
1801 callback(userData, buf, len);
1802 } while (len != 0);
1803
1804 return(xmlNanoFTPCloseConnection(ctxt));
1805}
1806
1807/**
1808 * xmlNanoFTPRead:
1809 * @ctx: the FTP context
1810 * @dest: a buffer
1811 * @len: the buffer length
1812 *
1813 * This function tries to read @len bytes from the existing FTP connection
1814 * and saves them in @dest. This is a blocking call.
1815 *
1816 * Returns the number of byte read. 0 is an indication of an end of connection.
1817 * -1 indicates a parameter error.
1818 */
1819int
1820xmlNanoFTPRead(void *ctx, void *dest, int len) {
1821 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1822
1823 if (ctx == NULL) return(-1);
1824 if (ctxt->dataFd < 0) return(0);
1825 if (dest == NULL) return(-1);
1826 if (len <= 0) return(0);
1827
Daniel Veillard2f971a22000-10-12 23:26:32 +00001828 len = recv(ctxt->dataFd, dest, len, 0);
Daniel Veillardda07c342000-01-25 18:31:22 +00001829#ifdef DEBUG_FTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001830 xmlGenericError(xmlGenericErrorContext, "Recvd %d bytes\n", len);
Daniel Veillardda07c342000-01-25 18:31:22 +00001831#endif
1832 if (len <= 0) {
1833 xmlNanoFTPCloseConnection(ctxt);
1834 }
1835 return(len);
1836}
1837
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001838/**
Daniel Veillardda07c342000-01-25 18:31:22 +00001839 * xmlNanoFTPOpen:
1840 * @URL: the URL to the resource
1841 *
1842 * Start to fetch the given ftp:// resource
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001843 *
1844 * Returns an FTP context, or NULL
Daniel Veillardda07c342000-01-25 18:31:22 +00001845 */
1846
Daniel Veillard06047432000-04-24 11:33:38 +00001847void*
Daniel Veillardda07c342000-01-25 18:31:22 +00001848xmlNanoFTPOpen(const char *URL) {
1849 xmlNanoFTPCtxtPtr ctxt;
1850 int sock;
1851
1852 xmlNanoFTPInit();
1853 if (URL == NULL) return(NULL);
1854 if (strncmp("ftp://", URL, 6)) return(NULL);
1855
Daniel Veillard49703262000-07-10 10:27:46 +00001856 ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(URL);
Daniel Veillardda07c342000-01-25 18:31:22 +00001857 if (ctxt == NULL) return(NULL);
1858 if (xmlNanoFTPConnect(ctxt) < 0) {
1859 xmlNanoFTPFreeCtxt(ctxt);
1860 return(NULL);
1861 }
1862 sock = xmlNanoFTPGetSocket(ctxt, ctxt->path);
1863 if (sock < 0) {
1864 xmlNanoFTPFreeCtxt(ctxt);
1865 return(NULL);
1866 }
1867 return(ctxt);
1868}
1869
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001870/**
1871 * xmlNanoFTPClose:
1872 * @ctx: an FTP context
1873 *
1874 * Close the connection and both control and transport
1875 *
1876 * Returns -1 incase of error, 0 otherwise
Daniel Veillardda07c342000-01-25 18:31:22 +00001877 */
1878
1879int
1880xmlNanoFTPClose(void *ctx) {
1881 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1882
1883 if (ctxt == NULL)
1884 return(-1);
1885
1886 if (ctxt->dataFd >= 0) {
Daniel Veillard2f971a22000-10-12 23:26:32 +00001887 closesocket(ctxt->dataFd);
Daniel Veillardda07c342000-01-25 18:31:22 +00001888 ctxt->dataFd = -1;
1889 }
1890 if (ctxt->controlFd >= 0) {
Daniel Veillardaeea04f2000-01-25 19:27:27 +00001891 xmlNanoFTPQuit(ctxt);
Daniel Veillard2f971a22000-10-12 23:26:32 +00001892 closesocket(ctxt->controlFd);
Daniel Veillardda07c342000-01-25 18:31:22 +00001893 ctxt->controlFd = -1;
1894 }
1895 xmlNanoFTPFreeCtxt(ctxt);
1896 return(0);
1897}
1898
1899#ifdef STANDALONE
1900/************************************************************************
1901 * *
1902 * Basic test in Standalone mode *
1903 * *
1904 ************************************************************************/
1905void ftpList(void *userData, const char *filename, const char* attrib,
1906 const char *owner, const char *group, unsigned long size, int links,
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001907 int year, const char *month, int day, int hour, int minute) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001908 xmlGenericError(xmlGenericErrorContext,
1909 "%s %s %s %ld %s\n", attrib, owner, group, size, filename);
Daniel Veillardda07c342000-01-25 18:31:22 +00001910}
1911void ftpData(void *userData, const char *data, int len) {
1912 if (userData == NULL) return;
1913 if (len <= 0) {
1914 fclose(userData);
1915 return;
1916 }
1917 fwrite(data, len, 1, userData);
1918}
1919
1920int main(int argc, char **argv) {
1921 void *ctxt;
1922 FILE *output;
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001923 char *tstfile = NULL;
Daniel Veillardda07c342000-01-25 18:31:22 +00001924
1925 xmlNanoFTPInit();
1926 if (argc > 1) {
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001927 ctxt = xmlNanoFTPNewCtxt(argv[1]);
1928 if (xmlNanoFTPConnect(ctxt) < 0) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001929 xmlGenericError(xmlGenericErrorContext,
1930 "Couldn't connect to %s\n", argv[1]);
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001931 exit(1);
1932 }
Daniel Veillardda07c342000-01-25 18:31:22 +00001933 if (argc > 2)
1934 tstfile = argv[2];
1935 } else
1936 ctxt = xmlNanoFTPConnectTo("localhost", 0);
1937 if (ctxt == NULL) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001938 xmlGenericError(xmlGenericErrorContext,
1939 "Couldn't connect to localhost\n");
Daniel Veillardda07c342000-01-25 18:31:22 +00001940 exit(1);
1941 }
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001942 xmlNanoFTPList(ctxt, ftpList, NULL, tstfile);
Daniel Veillardda07c342000-01-25 18:31:22 +00001943 output = fopen("/tmp/tstdata", "w");
1944 if (output != NULL) {
1945 if (xmlNanoFTPGet(ctxt, ftpData, (void *) output, tstfile) < 0)
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001946 xmlGenericError(xmlGenericErrorContext,
1947 "Failed to get file\n");
Daniel Veillardda07c342000-01-25 18:31:22 +00001948
1949 }
1950 xmlNanoFTPClose(ctxt);
1951 xmlMemoryDump();
1952 exit(0);
1953}
1954#endif /* STANDALONE */
Daniel Veillard361d8452000-04-03 19:48:13 +00001955#else /* !LIBXML_FTP_ENABLED */
1956#ifdef STANDALONE
1957#include <stdio.h>
1958int main(int argc, char **argv) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001959 xmlGenericError(xmlGenericErrorContext,
1960 "%s : FTP support not compiled in\n", argv[0]);
Daniel Veillard361d8452000-04-03 19:48:13 +00001961 return(0);
1962}
1963#endif /* STANDALONE */
1964#endif /* LIBXML_FTP_ENABLED */