blob: 3c6257c8913cc2d788f2b2b82b3ac14fce33d008 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * nanoftp.c: basic FTP client support
3 *
4 * Reference: RFC 959
5 */
6
7#ifdef TESTING
8#define STANDALONE
9#define HAVE_STDLIB_H
10#define HAVE_UNISTD_H
11#define HAVE_SYS_SOCKET_H
12#define HAVE_NETINET_IN_H
13#define HAVE_NETDB_H
14#define HAVE_SYS_TIME_H
Daniel Veillardcbaf3992001-12-31 16:16:02 +000015#else /* TESTING */
Daniel Veillardf3afa7d2001-06-09 13:52:58 +000016#define NEED_SOCKETS
Daniel Veillardcbaf3992001-12-31 16:16:02 +000017#endif /* TESTING */
Owen Taylor3473f882001-02-23 17:55:21 +000018
Daniel Veillard34ce8be2002-03-18 19:37:11 +000019#define IN_LIBXML
Daniel Veillard5e2dace2001-07-18 19:30:27 +000020#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000021
22#ifdef LIBXML_FTP_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000023#include <string.h>
24
25#ifdef HAVE_STDLIB_H
26#include <stdlib.h>
27#endif
28#ifdef HAVE_UNISTD_H
29#include <unistd.h>
30#endif
31#ifdef HAVE_SYS_SOCKET_H
32#include <sys/socket.h>
33#endif
34#ifdef HAVE_NETINET_IN_H
35#include <netinet/in.h>
36#endif
37#ifdef HAVE_ARPA_INET_H
38#include <arpa/inet.h>
39#endif
40#ifdef HAVE_NETDB_H
41#include <netdb.h>
42#endif
43#ifdef HAVE_FCNTL_H
44#include <fcntl.h>
45#endif
46#ifdef HAVE_ERRNO_H
47#include <errno.h>
48#endif
49#ifdef HAVE_SYS_TIME_H
50#include <sys/time.h>
51#endif
52#ifdef HAVE_SYS_SELECT_H
53#include <sys/select.h>
54#endif
Daniel Veillard75eb1ad2003-07-07 14:42:44 +000055#ifdef HAVE_SYS_SOCKET_H
56#include <sys/socket.h>
57#endif
58#ifdef HAVE_SYS_TYPES_H
59#include <sys/types.h>
60#endif
Owen Taylor3473f882001-02-23 17:55:21 +000061#ifdef HAVE_STRINGS_H
62#include <strings.h>
63#endif
64
65#include <libxml/xmlmemory.h>
Daniel Veillardd0463562001-10-13 09:15:48 +000066#include <libxml/parser.h>
Owen Taylor3473f882001-02-23 17:55:21 +000067#include <libxml/xmlerror.h>
Daniel Veillardcacbe5d2003-01-10 16:09:51 +000068#include <libxml/uri.h>
Daniel Veillardd0463562001-10-13 09:15:48 +000069#include <libxml/nanoftp.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000070#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000071
72/* #define DEBUG_FTP 1 */
73#ifdef STANDALONE
74#ifndef DEBUG_FTP
75#define DEBUG_FTP 1
76#endif
77#endif
78
79/**
80 * A couple portability macros
81 */
82#ifndef _WINSOCKAPI_
83#define closesocket(s) close(s)
84#define SOCKET int
85#endif
Daniel Veillardacf7ff02001-10-29 20:21:47 +000086#if defined(VMS) || defined(__VMS)
87#define SOCKLEN_T unsigned int
88#endif
Owen Taylor3473f882001-02-23 17:55:21 +000089
Owen Taylor3473f882001-02-23 17:55:21 +000090#define FTP_COMMAND_OK 200
91#define FTP_SYNTAX_ERROR 500
92#define FTP_GET_PASSWD 331
93#define FTP_BUF_SIZE 512
94
95typedef struct xmlNanoFTPCtxt {
96 char *protocol; /* the protocol name */
97 char *hostname; /* the host name */
98 int port; /* the port */
99 char *path; /* the path within the URL */
100 char *user; /* user string */
101 char *passwd; /* passwd string */
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000102#ifdef SUPPORT_IP6
103 struct sockaddr_storage ftpAddr; /* this is large enough to hold IPv6 address*/
104#else
Owen Taylor3473f882001-02-23 17:55:21 +0000105 struct sockaddr_in ftpAddr; /* the socket address struct */
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000106#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000107 int passive; /* currently we support only passive !!! */
108 SOCKET controlFd; /* the file descriptor for the control socket */
109 SOCKET dataFd; /* the file descriptor for the data socket */
110 int state; /* WRITE / READ / CLOSED */
111 int returnValue; /* the protocol return value */
112 /* buffer for data received from the control connection */
113 char controlBuf[FTP_BUF_SIZE + 1];
114 int controlBufIndex;
115 int controlBufUsed;
116 int controlBufAnswer;
117} xmlNanoFTPCtxt, *xmlNanoFTPCtxtPtr;
118
119static int initialized = 0;
120static char *proxy = NULL; /* the proxy name if any */
121static int proxyPort = 0; /* the proxy port if any */
122static char *proxyUser = NULL; /* user for proxy authentication */
123static char *proxyPasswd = NULL;/* passwd for proxy authentication */
124static int proxyType = 0; /* uses TYPE or a@b ? */
125
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000126#ifdef SUPPORT_IP6
Daniel Veillard2db8c122003-07-08 12:16:59 +0000127static
128int have_ipv6(void) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000129 int s;
130
131 s = socket (AF_INET6, SOCK_STREAM, 0);
132 if (s != -1) {
133 close (s);
134 return (1);
135 }
136 return (0);
137}
138#endif
139
Owen Taylor3473f882001-02-23 17:55:21 +0000140/**
141 * xmlNanoFTPInit:
142 *
143 * Initialize the FTP protocol layer.
144 * Currently it just checks for proxy informations,
145 * and get the hostname
146 */
147
148void
149xmlNanoFTPInit(void) {
150 const char *env;
151#ifdef _WINSOCKAPI_
152 WSADATA wsaData;
153#endif
154
155 if (initialized)
156 return;
157
158#ifdef _WINSOCKAPI_
159 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
160 return;
161#endif
162
Owen Taylor3473f882001-02-23 17:55:21 +0000163 proxyPort = 21;
164 env = getenv("no_proxy");
165 if (env != NULL)
166 return;
167 env = getenv("ftp_proxy");
168 if (env != NULL) {
169 xmlNanoFTPScanProxy(env);
170 } else {
171 env = getenv("FTP_PROXY");
172 if (env != NULL) {
173 xmlNanoFTPScanProxy(env);
174 }
175 }
176 env = getenv("ftp_proxy_user");
177 if (env != NULL) {
178 proxyUser = xmlMemStrdup(env);
179 }
180 env = getenv("ftp_proxy_password");
181 if (env != NULL) {
182 proxyPasswd = xmlMemStrdup(env);
183 }
184 initialized = 1;
185}
186
187/**
Daniel Veillarde356c282001-03-10 12:32:04 +0000188 * xmlNanoFTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000189 *
190 * Cleanup the FTP protocol layer. This cleanup proxy informations.
191 */
192
193void
194xmlNanoFTPCleanup(void) {
195 if (proxy != NULL) {
196 xmlFree(proxy);
197 proxy = NULL;
198 }
199 if (proxyUser != NULL) {
200 xmlFree(proxyUser);
201 proxyUser = NULL;
202 }
203 if (proxyPasswd != NULL) {
204 xmlFree(proxyPasswd);
205 proxyPasswd = NULL;
206 }
Owen Taylor3473f882001-02-23 17:55:21 +0000207#ifdef _WINSOCKAPI_
208 if (initialized)
209 WSACleanup();
210#endif
211 initialized = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000212}
213
214/**
215 * xmlNanoFTPProxy:
216 * @host: the proxy host name
217 * @port: the proxy port
218 * @user: the proxy user name
219 * @passwd: the proxy password
220 * @type: the type of proxy 1 for using SITE, 2 for USER a@b
221 *
222 * Setup the FTP proxy informations.
223 * This can also be done by using ftp_proxy ftp_proxy_user and
224 * ftp_proxy_password environment variables.
225 */
226
227void
228xmlNanoFTPProxy(const char *host, int port, const char *user,
229 const char *passwd, int type) {
230 if (proxy != NULL)
231 xmlFree(proxy);
232 if (proxyUser != NULL)
233 xmlFree(proxyUser);
234 if (proxyPasswd != NULL)
235 xmlFree(proxyPasswd);
236 if (host)
237 proxy = xmlMemStrdup(host);
238 if (user)
239 proxyUser = xmlMemStrdup(user);
240 if (passwd)
241 proxyPasswd = xmlMemStrdup(passwd);
242 proxyPort = port;
243 proxyType = type;
244}
245
246/**
247 * xmlNanoFTPScanURL:
248 * @ctx: an FTP context
249 * @URL: The URL used to initialize the context
250 *
251 * (Re)Initialize an FTP context by parsing the URL and finding
252 * the protocol host port and path it indicates.
253 */
254
255static void
256xmlNanoFTPScanURL(void *ctx, const char *URL) {
257 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
258 const char *cur = URL;
259 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000260 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000261 int port = 0;
262
263 if (ctxt->protocol != NULL) {
264 xmlFree(ctxt->protocol);
265 ctxt->protocol = NULL;
266 }
267 if (ctxt->hostname != NULL) {
268 xmlFree(ctxt->hostname);
269 ctxt->hostname = NULL;
270 }
271 if (ctxt->path != NULL) {
272 xmlFree(ctxt->path);
273 ctxt->path = NULL;
274 }
275 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000276 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000277 while (*cur != 0) {
278 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000279 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000280 ctxt->protocol = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000281 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000282 cur += 3;
283 break;
284 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000285 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000286 }
287 if (*cur == 0) return;
288
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000289 buf[indx] = 0;
Daniel Veillardc69e0b12001-11-20 08:35:07 +0000290 /* allow user@ and user:pass@ forms */
291 {
292 const char *p = strchr(cur, '@');
293 if(p) {
294 while(1) {
295 if(cur[0] == ':' || cur[0] == '@') break;
296 buf[indx++] = *cur++;
297 }
298 buf[indx] = 0;
299 ctxt->user = xmlMemStrdup(buf);
300 indx = 0;
301 if(cur[0] == ':') {
302 cur++;
303 while(1) {
304 if(cur[0] == '@') break;
305 buf[indx++] = *cur++;
306 }
307 buf[indx] = 0;
308 ctxt->passwd = xmlMemStrdup(buf);
309 indx = 0;
310 }
311 cur = p+1;
312 }
313 }
314
Owen Taylor3473f882001-02-23 17:55:21 +0000315 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000316 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
317 (!strchr (cur, '[') && strchr (cur, ']'))) {
318 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoFTPScanURL: %s",
319 "Syntax Error\n");
320 return;
321 }
322
323 if (cur[0] == '[') {
324 cur++;
325 while (cur[0] != ']')
326 buf[indx++] = *cur++;
327
328 if (!strchr (buf, ':')) {
329 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoFTPScanURL: %s",
330 "Use [IPv6]/IPv4 format\n");
331 return;
332 }
333
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000334 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000335 ctxt->hostname = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000336 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000337 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000338 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000339 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000340 while (*cur >= '0' && *cur <= '9') {
341 port *= 10;
342 port += *cur - '0';
343 cur++;
344 }
345
346 if (port != 0) ctxt->port = port;
347 while ((cur[0] != '/') && (*cur != 0))
348 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000349 }
Owen Taylor3473f882001-02-23 17:55:21 +0000350 break;
351 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000352 else { /* address is an IPv4 one*/
353 if (cur[0] == ':') {
354 buf[indx] = 0;
355 ctxt->hostname = xmlMemStrdup (buf);
356 indx = 0;
357 cur += 1;
358 while ((*cur >= '0') && (*cur <= '9')) {
359 port *= 10;
360 port += *cur - '0';
361 cur++;
362 }
363 if (port != 0) ctxt->port = port;
364 while ((cur[0] != '/') && (*cur != 0))
365 cur++;
366 break;
367 }
368 if ((*cur == '/') || (*cur == 0)) {
369 buf[indx] = 0;
370 ctxt->hostname = xmlMemStrdup (buf);
371 indx = 0;
372 break;
373 }
Owen Taylor3473f882001-02-23 17:55:21 +0000374 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000375 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000376 }
377 if (*cur == 0)
378 ctxt->path = xmlMemStrdup("/");
379 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000380 indx = 0;
381 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000382 while (*cur != 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000383 buf[indx++] = *cur++;
384 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000385 ctxt->path = xmlMemStrdup(buf);
386 }
387}
388
389/**
390 * xmlNanoFTPUpdateURL:
391 * @ctx: an FTP context
392 * @URL: The URL used to update the context
393 *
394 * Update an FTP context by parsing the URL and finding
395 * new path it indicates. If there is an error in the
396 * protocol, hostname, port or other information, the
397 * error is raised. It indicates a new connection has to
398 * be established.
399 *
400 * Returns 0 if Ok, -1 in case of error (other host).
401 */
402
403int
404xmlNanoFTPUpdateURL(void *ctx, const char *URL) {
405 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
406 const char *cur = URL;
407 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000408 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000409 int port = 0;
410
411 if (URL == NULL)
412 return(-1);
413 if (ctxt == NULL)
414 return(-1);
415 if (ctxt->protocol == NULL)
416 return(-1);
417 if (ctxt->hostname == NULL)
418 return(-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000419 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000420 while (*cur != 0) {
421 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000422 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000423 if (strcmp(ctxt->protocol, buf))
424 return(-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000425 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000426 cur += 3;
427 break;
428 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000429 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000430 }
431 if (*cur == 0)
432 return(-1);
433
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000434 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000435 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000436 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
437 (!strchr (cur, '[') && strchr (cur, ']'))) {
438 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoFTPUpdateURL: %s",
439 "Syntax Error\n");
440 return (-1);
441 }
442
443 if (cur[0] == '[') {
444 cur++;
445 while (cur[0] != ']')
446 buf[indx++] = *cur++;
447
448 if (!strchr (buf, ':')) {
449 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoFTPUpdateURL: %s",
450 "Use [IPv6]/IPv4 format\n");
451 return (-1);
452 }
453
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000454 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000455 if (strcmp (ctxt->hostname, buf))
456 return (-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000457 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000458 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000459 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000460 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000461 while (*cur >= '0' && *cur <= '9') {
462 port *= 10;
463 port += *cur - '0';
464 cur++;
465 }
466
467 if (port != ctxt->port)
468 return (-1);
469 while ((cur[0] != '/') && (*cur != 0))
470 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000471 }
Owen Taylor3473f882001-02-23 17:55:21 +0000472 break;
473 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000474 else {
475 if (cur[0] == ':') {
476 buf[indx] = 0;
477 if (strcmp (ctxt->hostname, buf))
478 return (-1);
479 indx = 0;
480 cur += 1;
481 while ((*cur >= '0') && (*cur <= '9')) {
482 port *= 10;
483 port += *cur - '0';
484 cur++;
485 }
486 if (port != ctxt->port)
487 return (-1);
488 while ((cur[0] != '/') && (*cur != 0))
489 cur++;
490 break;
491 }
492 if ((*cur == '/') || (*cur == 0)) {
493 buf[indx] = 0;
494 if (strcmp (ctxt->hostname, buf))
495 return (-1);
496 indx = 0;
497 break;
498 }
Owen Taylor3473f882001-02-23 17:55:21 +0000499 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000500 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000501 }
502 if (ctxt->path != NULL) {
503 xmlFree(ctxt->path);
504 ctxt->path = NULL;
505 }
506
507 if (*cur == 0)
508 ctxt->path = xmlMemStrdup("/");
509 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000510 indx = 0;
511 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000512 while (*cur != 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000513 buf[indx++] = *cur++;
514 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000515 ctxt->path = xmlMemStrdup(buf);
516 }
517 return(0);
518}
519
520/**
521 * xmlNanoFTPScanProxy:
522 * @URL: The proxy URL used to initialize the proxy context
523 *
524 * (Re)Initialize the FTP Proxy context by parsing the URL and finding
525 * the protocol host port it indicates.
526 * Should be like ftp://myproxy/ or ftp://myproxy:3128/
527 * A NULL URL cleans up proxy informations.
528 */
529
530void
531xmlNanoFTPScanProxy(const char *URL) {
532 const char *cur = URL;
533 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000534 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000535 int port = 0;
536
537 if (proxy != NULL) {
538 xmlFree(proxy);
539 proxy = NULL;
540 }
541 if (proxyPort != 0) {
542 proxyPort = 0;
543 }
544#ifdef DEBUG_FTP
545 if (URL == NULL)
546 xmlGenericError(xmlGenericErrorContext, "Removing FTP proxy info\n");
547 else
548 xmlGenericError(xmlGenericErrorContext, "Using FTP proxy %s\n", URL);
549#endif
550 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000551 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000552 while (*cur != 0) {
553 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000554 buf[indx] = 0;
555 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000556 cur += 3;
557 break;
558 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000559 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000560 }
561 if (*cur == 0) return;
562
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000563 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000564 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000565 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
566 (!strchr (cur, '[') && strchr (cur, ']'))) {
567 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoFTPScanProxy: %s",
568 "Syntax error\n");
569 return;
570 }
571
572 if (cur[0] == '[') {
573 cur++;
574 while (cur[0] != ']')
575 buf[indx++] = *cur++;
576 if (!strchr (buf, ':')) {
577 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoFTPScanProxy: %s",
578 "Use [IPv6]/IPv4 format\n");
579 return;
580 }
581
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000582 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000583 proxy = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000584 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000585 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000586 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000587 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000588 while (*cur >= '0' && *cur <= '9') {
589 port *= 10;
590 port += *cur - '0';
591 cur++;
592 }
593
594 if (port != 0) proxyPort = port;
595 while ((cur[0] != '/') && (*cur != 0))
596 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000597 }
Owen Taylor3473f882001-02-23 17:55:21 +0000598 break;
599 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000600 else {
601 if (cur[0] == ':') {
602 buf[indx] = 0;
603 proxy = xmlMemStrdup (buf);
604 indx = 0;
605 cur += 1;
606 while ((*cur >= '0') && (*cur <= '9')) {
607 port *= 10;
608 port += *cur - '0';
609 cur++;
610 }
611 if (port != 0) proxyPort = port;
612 while ((cur[0] != '/') && (*cur != 0))
613 cur++;
614 break;
615 }
616 if ((*cur == '/') || (*cur == 0)) {
617 buf[indx] = 0;
618 proxy = xmlMemStrdup (buf);
619 indx = 0;
620 break;
621 }
Owen Taylor3473f882001-02-23 17:55:21 +0000622 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000623 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000624 }
625}
626
627/**
628 * xmlNanoFTPNewCtxt:
629 * @URL: The URL used to initialize the context
630 *
631 * Allocate and initialize a new FTP context.
632 *
633 * Returns an FTP context or NULL in case of error.
634 */
635
636void*
637xmlNanoFTPNewCtxt(const char *URL) {
638 xmlNanoFTPCtxtPtr ret;
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000639 char *unescaped;
Owen Taylor3473f882001-02-23 17:55:21 +0000640
641 ret = (xmlNanoFTPCtxtPtr) xmlMalloc(sizeof(xmlNanoFTPCtxt));
642 if (ret == NULL) return(NULL);
643
644 memset(ret, 0, sizeof(xmlNanoFTPCtxt));
645 ret->port = 21;
646 ret->passive = 1;
647 ret->returnValue = 0;
648 ret->controlBufIndex = 0;
649 ret->controlBufUsed = 0;
Daniel Veillardc69e0b12001-11-20 08:35:07 +0000650 ret->controlFd = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000651
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000652 unescaped = xmlURIUnescapeString(URL, 0, NULL);
653 if (unescaped != NULL)
654 xmlNanoFTPScanURL(ret, unescaped);
655 else if (URL != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +0000656 xmlNanoFTPScanURL(ret, URL);
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000657 xmlFree(unescaped);
Owen Taylor3473f882001-02-23 17:55:21 +0000658
659 return(ret);
660}
661
662/**
663 * xmlNanoFTPFreeCtxt:
664 * @ctx: an FTP context
665 *
666 * Frees the context after closing the connection.
667 */
668
669void
670xmlNanoFTPFreeCtxt(void * ctx) {
671 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
672 if (ctxt == NULL) return;
673 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
674 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
675 if (ctxt->path != NULL) xmlFree(ctxt->path);
676 ctxt->passive = 1;
677 if (ctxt->controlFd >= 0) closesocket(ctxt->controlFd);
678 ctxt->controlFd = -1;
679 ctxt->controlBufIndex = -1;
680 ctxt->controlBufUsed = -1;
681 xmlFree(ctxt);
682}
683
684/**
685 * xmlNanoFTPParseResponse:
Owen Taylor3473f882001-02-23 17:55:21 +0000686 * @buf: the buffer containing the response
687 * @len: the buffer length
688 *
689 * Parsing of the server answer, we just extract the code.
690 *
691 * returns 0 for errors
692 * +XXX for last line of response
693 * -XXX for response to be continued
694 */
695static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000696xmlNanoFTPParseResponse(char *buf, int len) {
Owen Taylor3473f882001-02-23 17:55:21 +0000697 int val = 0;
698
699 if (len < 3) return(-1);
700 if ((*buf >= '0') && (*buf <= '9'))
701 val = val * 10 + (*buf - '0');
702 else
703 return(0);
704 buf++;
705 if ((*buf >= '0') && (*buf <= '9'))
706 val = val * 10 + (*buf - '0');
707 else
708 return(0);
709 buf++;
710 if ((*buf >= '0') && (*buf <= '9'))
711 val = val * 10 + (*buf - '0');
712 else
713 return(0);
714 buf++;
715 if (*buf == '-')
716 return(-val);
717 return(val);
718}
719
720/**
721 * xmlNanoFTPGetMore:
722 * @ctx: an FTP context
723 *
724 * Read more information from the FTP control connection
725 * Returns the number of bytes read, < 0 indicates an error
726 */
727static int
728xmlNanoFTPGetMore(void *ctx) {
729 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
730 int len;
731 int size;
732
733 if ((ctxt->controlBufIndex < 0) || (ctxt->controlBufIndex > FTP_BUF_SIZE)) {
734#ifdef DEBUG_FTP
735 xmlGenericError(xmlGenericErrorContext,
736 "xmlNanoFTPGetMore : controlBufIndex = %d\n",
737 ctxt->controlBufIndex);
738#endif
739 return(-1);
740 }
741
742 if ((ctxt->controlBufUsed < 0) || (ctxt->controlBufUsed > FTP_BUF_SIZE)) {
743#ifdef DEBUG_FTP
744 xmlGenericError(xmlGenericErrorContext,
745 "xmlNanoFTPGetMore : controlBufUsed = %d\n",
746 ctxt->controlBufUsed);
747#endif
748 return(-1);
749 }
750 if (ctxt->controlBufIndex > ctxt->controlBufUsed) {
751#ifdef DEBUG_FTP
752 xmlGenericError(xmlGenericErrorContext,
753 "xmlNanoFTPGetMore : controlBufIndex > controlBufUsed %d > %d\n",
754 ctxt->controlBufIndex, ctxt->controlBufUsed);
755#endif
756 return(-1);
757 }
758
759 /*
760 * First pack the control buffer
761 */
762 if (ctxt->controlBufIndex > 0) {
763 memmove(&ctxt->controlBuf[0], &ctxt->controlBuf[ctxt->controlBufIndex],
764 ctxt->controlBufUsed - ctxt->controlBufIndex);
765 ctxt->controlBufUsed -= ctxt->controlBufIndex;
766 ctxt->controlBufIndex = 0;
767 }
768 size = FTP_BUF_SIZE - ctxt->controlBufUsed;
769 if (size == 0) {
770#ifdef DEBUG_FTP
771 xmlGenericError(xmlGenericErrorContext,
772 "xmlNanoFTPGetMore : buffer full %d \n", ctxt->controlBufUsed);
773#endif
774 return(0);
775 }
776
777 /*
Daniel Veillard60087f32001-10-10 09:45:09 +0000778 * Read the amount left on the control connection
Owen Taylor3473f882001-02-23 17:55:21 +0000779 */
780 if ((len = recv(ctxt->controlFd, &ctxt->controlBuf[ctxt->controlBufIndex],
781 size, 0)) < 0) {
782 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
783 ctxt->controlFd = -1;
784 return(-1);
785 }
786#ifdef DEBUG_FTP
787 xmlGenericError(xmlGenericErrorContext,
788 "xmlNanoFTPGetMore : read %d [%d - %d]\n", len,
789 ctxt->controlBufUsed, ctxt->controlBufUsed + len);
790#endif
791 ctxt->controlBufUsed += len;
792 ctxt->controlBuf[ctxt->controlBufUsed] = 0;
793
794 return(len);
795}
796
797/**
798 * xmlNanoFTPReadResponse:
799 * @ctx: an FTP context
800 *
801 * Read the response from the FTP server after a command.
802 * Returns the code number
803 */
804static int
805xmlNanoFTPReadResponse(void *ctx) {
806 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
807 char *ptr, *end;
808 int len;
809 int res = -1, cur = -1;
810
811get_more:
812 /*
813 * Assumes everything up to controlBuf[controlBufIndex] has been read
814 * and analyzed.
815 */
816 len = xmlNanoFTPGetMore(ctx);
817 if (len < 0) {
818 return(-1);
819 }
820 if ((ctxt->controlBufUsed == 0) && (len == 0)) {
821 return(-1);
822 }
823 ptr = &ctxt->controlBuf[ctxt->controlBufIndex];
824 end = &ctxt->controlBuf[ctxt->controlBufUsed];
825
826#ifdef DEBUG_FTP
827 xmlGenericError(xmlGenericErrorContext,
828 "\n<<<\n%s\n--\n", ptr);
829#endif
830 while (ptr < end) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000831 cur = xmlNanoFTPParseResponse(ptr, end - ptr);
Owen Taylor3473f882001-02-23 17:55:21 +0000832 if (cur > 0) {
833 /*
834 * Successfully scanned the control code, scratch
835 * till the end of the line, but keep the index to be
836 * able to analyze the result if needed.
837 */
838 res = cur;
839 ptr += 3;
840 ctxt->controlBufAnswer = ptr - ctxt->controlBuf;
841 while ((ptr < end) && (*ptr != '\n')) ptr++;
842 if (*ptr == '\n') ptr++;
843 if (*ptr == '\r') ptr++;
844 break;
845 }
846 while ((ptr < end) && (*ptr != '\n')) ptr++;
847 if (ptr >= end) {
848 ctxt->controlBufIndex = ctxt->controlBufUsed;
849 goto get_more;
850 }
851 if (*ptr != '\r') ptr++;
852 }
853
854 if (res < 0) goto get_more;
855 ctxt->controlBufIndex = ptr - ctxt->controlBuf;
856#ifdef DEBUG_FTP
857 ptr = &ctxt->controlBuf[ctxt->controlBufIndex];
858 xmlGenericError(xmlGenericErrorContext, "\n---\n%s\n--\n", ptr);
859#endif
860
861#ifdef DEBUG_FTP
862 xmlGenericError(xmlGenericErrorContext, "Got %d\n", res);
863#endif
864 return(res / 100);
865}
866
867/**
868 * xmlNanoFTPGetResponse:
869 * @ctx: an FTP context
870 *
871 * Get the response from the FTP server after a command.
872 * Returns the code number
873 */
874
875int
876xmlNanoFTPGetResponse(void *ctx) {
877 int res;
878
879 res = xmlNanoFTPReadResponse(ctx);
880
881 return(res);
882}
883
884/**
885 * xmlNanoFTPCheckResponse:
886 * @ctx: an FTP context
887 *
888 * Check if there is a response from the FTP server after a command.
889 * Returns the code number, or 0
890 */
891
892int
893xmlNanoFTPCheckResponse(void *ctx) {
894 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
895 fd_set rfd;
896 struct timeval tv;
897
898 tv.tv_sec = 0;
899 tv.tv_usec = 0;
900 FD_ZERO(&rfd);
901 FD_SET(ctxt->controlFd, &rfd);
902 switch(select(ctxt->controlFd + 1, &rfd, NULL, NULL, &tv)) {
903 case 0:
904 return(0);
905 case -1:
906#ifdef DEBUG_FTP
907 perror("select");
908#endif
909 return(-1);
910
911 }
912
913 return(xmlNanoFTPReadResponse(ctx));
914}
915
916/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000917 * Send the user authentication
Owen Taylor3473f882001-02-23 17:55:21 +0000918 */
919
920static int
921xmlNanoFTPSendUser(void *ctx) {
922 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
923 char buf[200];
924 int len;
925 int res;
926
927 if (ctxt->user == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000928 snprintf(buf, sizeof(buf), "USER anonymous\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000929 else
Owen Taylor3473f882001-02-23 17:55:21 +0000930 snprintf(buf, sizeof(buf), "USER %s\r\n", ctxt->user);
Owen Taylor3473f882001-02-23 17:55:21 +0000931 buf[sizeof(buf) - 1] = 0;
932 len = strlen(buf);
933#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +0000934 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000935#endif
936 res = send(ctxt->controlFd, buf, len, 0);
937 if (res < 0) return(res);
938 return(0);
939}
940
941/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000942 * Send the password authentication
Owen Taylor3473f882001-02-23 17:55:21 +0000943 */
944
945static int
946xmlNanoFTPSendPasswd(void *ctx) {
947 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
948 char buf[200];
949 int len;
950 int res;
951
952 if (ctxt->passwd == NULL)
Daniel Veillard9ae1eba2001-10-19 09:48:35 +0000953 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000954 else
Owen Taylor3473f882001-02-23 17:55:21 +0000955 snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
Owen Taylor3473f882001-02-23 17:55:21 +0000956 buf[sizeof(buf) - 1] = 0;
957 len = strlen(buf);
958#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +0000959 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000960#endif
961 res = send(ctxt->controlFd, buf, len, 0);
962 if (res < 0) return(res);
963 return(0);
964}
965
966/**
967 * xmlNanoFTPQuit:
968 * @ctx: an FTP context
969 *
970 * Send a QUIT command to the server
971 *
972 * Returns -1 in case of error, 0 otherwise
973 */
974
975
976int
977xmlNanoFTPQuit(void *ctx) {
978 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
979 char buf[200];
980 int len;
981 int res;
982
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000983 snprintf(buf, sizeof(buf), "QUIT\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000984 len = strlen(buf);
985#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +0000986 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 +0000987#endif
988 res = send(ctxt->controlFd, buf, len, 0);
989 return(0);
990}
991
992/**
993 * xmlNanoFTPConnect:
994 * @ctx: an FTP context
995 *
996 * Tries to open a control connection
997 *
998 * Returns -1 in case of error, 0 otherwise
999 */
1000
1001int
1002xmlNanoFTPConnect(void *ctx) {
1003 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1004 struct hostent *hp;
1005 int port;
1006 int res;
Daniel Veillard2db8c122003-07-08 12:16:59 +00001007 int addrlen = sizeof (struct sockaddr_in);
Owen Taylor3473f882001-02-23 17:55:21 +00001008
1009 if (ctxt == NULL)
1010 return(-1);
1011 if (ctxt->hostname == NULL)
1012 return(-1);
1013
1014 /*
1015 * do the blocking DNS query.
1016 */
Owen Taylor3473f882001-02-23 17:55:21 +00001017 if (proxy) {
1018 port = proxyPort;
1019 } else {
1020 port = ctxt->port;
1021 }
1022 if (port == 0)
1023 port = 21;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001024
1025 memset (&ctxt->ftpAddr, 0, sizeof(ctxt->ftpAddr));
1026
1027#ifdef SUPPORT_IP6
1028 if (have_ipv6 ()) {
Daniel Veillard2db8c122003-07-08 12:16:59 +00001029 struct addrinfo hints, *tmp, *result;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001030
1031 result = NULL;
1032 memset (&hints, 0, sizeof(hints));
1033 hints.ai_socktype = SOCK_STREAM;
1034
1035 if (proxy) {
1036 if (getaddrinfo (proxy, NULL, &hints, &result) != 0)
1037 return (-1);
1038 }
1039 else
1040 if (getaddrinfo (ctxt->hostname, NULL, &hints, &result) != 0)
1041 return (-1);
1042
Daniel Veillard2db8c122003-07-08 12:16:59 +00001043 for (tmp = result; tmp; tmp = tmp->ai_next)
1044 if (tmp->ai_family == AF_INET || tmp->ai_family == AF_INET6)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001045 break;
1046
Daniel Veillard2db8c122003-07-08 12:16:59 +00001047 if (tmp) {
1048 if (tmp->ai_family == AF_INET6) {
1049 memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001050 ((struct sockaddr_in6 *) &ctxt->ftpAddr)->sin6_port = htons (port);
1051 ctxt->controlFd = socket (AF_INET6, SOCK_STREAM, 0);
1052 }
1053 else {
Daniel Veillard2db8c122003-07-08 12:16:59 +00001054 memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001055 ((struct sockaddr_in *) &ctxt->ftpAddr)->sin_port = htons (port);
1056 ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0);
1057 }
Daniel Veillard2db8c122003-07-08 12:16:59 +00001058 addrlen = tmp->ai_addrlen;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001059 freeaddrinfo (result);
1060 }
1061 }
1062 else
1063#endif
1064 {
1065 if (proxy)
1066 hp = gethostbyname (proxy);
1067 else
1068 hp = gethostbyname (ctxt->hostname);
1069 if (hp == NULL)
1070 return (-1);
1071
1072 /*
1073 * Prepare the socket
1074 */
1075 ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_family = AF_INET;
1076 memcpy (&((struct sockaddr_in *)&ctxt->ftpAddr)->sin_addr,
1077 hp->h_addr_list[0], hp->h_length);
1078 ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_port = htons (port);
1079 ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0);
1080 addrlen = sizeof (struct sockaddr_in);
1081 }
1082
Owen Taylor3473f882001-02-23 17:55:21 +00001083 if (ctxt->controlFd < 0)
1084 return(-1);
1085
1086 /*
1087 * Do the connect.
1088 */
1089 if (connect(ctxt->controlFd, (struct sockaddr *) &ctxt->ftpAddr,
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001090 addrlen) < 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00001091 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1092 ctxt->controlFd = -1;
1093 return(-1);
1094 }
1095
1096 /*
1097 * Wait for the HELLO from the server.
1098 */
1099 res = xmlNanoFTPGetResponse(ctxt);
1100 if (res != 2) {
1101 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1102 ctxt->controlFd = -1;
1103 return(-1);
1104 }
1105
1106 /*
1107 * State diagram for the login operation on the FTP server
1108 *
1109 * Reference: RFC 959
1110 *
1111 * 1
1112 * +---+ USER +---+------------->+---+
1113 * | B |---------->| W | 2 ---->| E |
1114 * +---+ +---+------ | -->+---+
1115 * | | | | |
1116 * 3 | | 4,5 | | |
1117 * -------------- ----- | | |
1118 * | | | | |
1119 * | | | | |
1120 * | --------- |
1121 * | 1| | | |
1122 * V | | | |
1123 * +---+ PASS +---+ 2 | ------>+---+
1124 * | |---------->| W |------------->| S |
1125 * +---+ +---+ ---------->+---+
1126 * | | | | |
1127 * 3 | |4,5| | |
1128 * -------------- -------- |
1129 * | | | | |
1130 * | | | | |
1131 * | -----------
1132 * | 1,3| | | |
1133 * V | 2| | |
1134 * +---+ ACCT +---+-- | ----->+---+
1135 * | |---------->| W | 4,5 -------->| F |
1136 * +---+ +---+------------->+---+
1137 *
1138 * Of course in case of using a proxy this get really nasty and is not
1139 * standardized at all :-(
1140 */
1141 if (proxy) {
1142 int len;
1143 char buf[400];
1144
1145 if (proxyUser != NULL) {
1146 /*
1147 * We need proxy auth
1148 */
Owen Taylor3473f882001-02-23 17:55:21 +00001149 snprintf(buf, sizeof(buf), "USER %s\r\n", proxyUser);
Owen Taylor3473f882001-02-23 17:55:21 +00001150 buf[sizeof(buf) - 1] = 0;
1151 len = strlen(buf);
1152#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001153 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001154#endif
1155 res = send(ctxt->controlFd, buf, len, 0);
1156 if (res < 0) {
1157 closesocket(ctxt->controlFd);
1158 ctxt->controlFd = -1;
1159 return(res);
1160 }
1161 res = xmlNanoFTPGetResponse(ctxt);
1162 switch (res) {
1163 case 2:
1164 if (proxyPasswd == NULL)
1165 break;
1166 case 3:
1167 if (proxyPasswd != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001168 snprintf(buf, sizeof(buf), "PASS %s\r\n", proxyPasswd);
Owen Taylor3473f882001-02-23 17:55:21 +00001169 else
Daniel Veillard9ae1eba2001-10-19 09:48:35 +00001170 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001171 buf[sizeof(buf) - 1] = 0;
1172 len = strlen(buf);
1173#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001174 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001175#endif
1176 res = send(ctxt->controlFd, buf, len, 0);
1177 if (res < 0) {
1178 closesocket(ctxt->controlFd);
1179 ctxt->controlFd = -1;
1180 return(res);
1181 }
1182 res = xmlNanoFTPGetResponse(ctxt);
1183 if (res > 3) {
1184 closesocket(ctxt->controlFd);
1185 ctxt->controlFd = -1;
1186 return(-1);
1187 }
1188 break;
1189 case 1:
1190 break;
1191 case 4:
1192 case 5:
1193 case -1:
1194 default:
1195 closesocket(ctxt->controlFd);
1196 ctxt->controlFd = -1;
1197 return(-1);
1198 }
1199 }
1200
1201 /*
1202 * We assume we don't need more authentication to the proxy
1203 * and that it succeeded :-\
1204 */
1205 switch (proxyType) {
1206 case 0:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001207 /* we will try in sequence */
Owen Taylor3473f882001-02-23 17:55:21 +00001208 case 1:
1209 /* Using SITE command */
Owen Taylor3473f882001-02-23 17:55:21 +00001210 snprintf(buf, sizeof(buf), "SITE %s\r\n", ctxt->hostname);
Owen Taylor3473f882001-02-23 17:55:21 +00001211 buf[sizeof(buf) - 1] = 0;
1212 len = strlen(buf);
1213#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001214 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001215#endif
1216 res = send(ctxt->controlFd, buf, len, 0);
1217 if (res < 0) {
1218 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1219 ctxt->controlFd = -1;
1220 return(res);
1221 }
1222 res = xmlNanoFTPGetResponse(ctxt);
1223 if (res == 2) {
1224 /* we assume it worked :-\ 1 is error for SITE command */
1225 proxyType = 1;
1226 break;
1227 }
1228 if (proxyType == 1) {
1229 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1230 ctxt->controlFd = -1;
1231 return(-1);
1232 }
1233 case 2:
1234 /* USER user@host command */
1235 if (ctxt->user == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001236 snprintf(buf, sizeof(buf), "USER anonymous@%s\r\n",
1237 ctxt->hostname);
Owen Taylor3473f882001-02-23 17:55:21 +00001238 else
Owen Taylor3473f882001-02-23 17:55:21 +00001239 snprintf(buf, sizeof(buf), "USER %s@%s\r\n",
1240 ctxt->user, ctxt->hostname);
Owen Taylor3473f882001-02-23 17:55:21 +00001241 buf[sizeof(buf) - 1] = 0;
1242 len = strlen(buf);
1243#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001244 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001245#endif
1246 res = send(ctxt->controlFd, buf, len, 0);
1247 if (res < 0) {
1248 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1249 ctxt->controlFd = -1;
1250 return(res);
1251 }
1252 res = xmlNanoFTPGetResponse(ctxt);
1253 if ((res == 1) || (res == 2)) {
1254 /* we assume it worked :-\ */
1255 proxyType = 2;
1256 return(0);
1257 }
1258 if (ctxt->passwd == NULL)
Daniel Veillard9ae1eba2001-10-19 09:48:35 +00001259 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001260 else
Owen Taylor3473f882001-02-23 17:55:21 +00001261 snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
Owen Taylor3473f882001-02-23 17:55:21 +00001262 buf[sizeof(buf) - 1] = 0;
1263 len = strlen(buf);
1264#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001265 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001266#endif
1267 res = send(ctxt->controlFd, buf, len, 0);
1268 if (res < 0) {
1269 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1270 ctxt->controlFd = -1;
1271 return(res);
1272 }
1273 res = xmlNanoFTPGetResponse(ctxt);
1274 if ((res == 1) || (res == 2)) {
1275 /* we assume it worked :-\ */
1276 proxyType = 2;
1277 return(0);
1278 }
1279 if (proxyType == 2) {
1280 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1281 ctxt->controlFd = -1;
1282 return(-1);
1283 }
1284 case 3:
1285 /*
1286 * If you need support for other Proxy authentication scheme
1287 * send the code or at least the sequence in use.
1288 */
1289 default:
1290 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1291 ctxt->controlFd = -1;
1292 return(-1);
1293 }
1294 }
1295 /*
1296 * Non-proxy handling.
1297 */
1298 res = xmlNanoFTPSendUser(ctxt);
1299 if (res < 0) {
1300 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1301 ctxt->controlFd = -1;
1302 return(-1);
1303 }
1304 res = xmlNanoFTPGetResponse(ctxt);
1305 switch (res) {
1306 case 2:
1307 return(0);
1308 case 3:
1309 break;
1310 case 1:
1311 case 4:
1312 case 5:
1313 case -1:
1314 default:
1315 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1316 ctxt->controlFd = -1;
1317 return(-1);
1318 }
1319 res = xmlNanoFTPSendPasswd(ctxt);
1320 if (res < 0) {
1321 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1322 ctxt->controlFd = -1;
1323 return(-1);
1324 }
1325 res = xmlNanoFTPGetResponse(ctxt);
1326 switch (res) {
1327 case 2:
1328 break;
1329 case 3:
1330 xmlGenericError(xmlGenericErrorContext,
1331 "FTP server asking for ACCNT on anonymous\n");
1332 case 1:
1333 case 4:
1334 case 5:
1335 case -1:
1336 default:
1337 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1338 ctxt->controlFd = -1;
1339 return(-1);
1340 }
1341
1342 return(0);
1343}
1344
1345/**
1346 * xmlNanoFTPConnectTo:
1347 * @server: an FTP server name
1348 * @port: the port (use 21 if 0)
1349 *
1350 * Tries to open a control connection to the given server/port
1351 *
1352 * Returns an fTP context or NULL if it failed
1353 */
1354
1355void*
1356xmlNanoFTPConnectTo(const char *server, int port) {
1357 xmlNanoFTPCtxtPtr ctxt;
1358 int res;
1359
1360 xmlNanoFTPInit();
1361 if (server == NULL)
1362 return(NULL);
1363 ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(NULL);
1364 ctxt->hostname = xmlMemStrdup(server);
1365 if (port != 0)
1366 ctxt->port = port;
1367 res = xmlNanoFTPConnect(ctxt);
1368 if (res < 0) {
1369 xmlNanoFTPFreeCtxt(ctxt);
1370 return(NULL);
1371 }
1372 return(ctxt);
1373}
1374
1375/**
1376 * xmlNanoFTPCwd:
1377 * @ctx: an FTP context
1378 * @directory: a directory on the server
1379 *
1380 * Tries to change the remote directory
1381 *
1382 * Returns -1 incase of error, 1 if CWD worked, 0 if it failed
1383 */
1384
1385int
1386xmlNanoFTPCwd(void *ctx, char *directory) {
1387 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1388 char buf[400];
1389 int len;
1390 int res;
1391
1392 /*
1393 * Expected response code for CWD:
1394 *
1395 * CWD
1396 * 250
1397 * 500, 501, 502, 421, 530, 550
1398 */
Owen Taylor3473f882001-02-23 17:55:21 +00001399 snprintf(buf, sizeof(buf), "CWD %s\r\n", directory);
Owen Taylor3473f882001-02-23 17:55:21 +00001400 buf[sizeof(buf) - 1] = 0;
1401 len = strlen(buf);
1402#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001403 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001404#endif
1405 res = send(ctxt->controlFd, buf, len, 0);
1406 if (res < 0) return(res);
1407 res = xmlNanoFTPGetResponse(ctxt);
1408 if (res == 4) {
1409 return(-1);
1410 }
1411 if (res == 2) return(1);
1412 if (res == 5) {
1413 return(0);
1414 }
1415 return(0);
1416}
1417
1418/**
Daniel Veillard6c73cb82003-03-05 16:45:40 +00001419 * xmlNanoFTPDele:
1420 * @ctx: an FTP context
1421 * @file: a file or directory on the server
1422 *
1423 * Tries to delete an item (file or directory) from server
1424 *
1425 * Returns -1 incase of error, 1 if DELE worked, 0 if it failed
1426 */
1427
1428int
1429xmlNanoFTPDele(void *ctx, char *file) {
1430 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1431 char buf[400];
1432 int len;
1433 int res;
1434
1435 /*
1436 * Expected response code for DELE:
1437 *
1438 * DELE
1439 * 250
1440 * 450, 550
1441 * 500, 501, 502, 421, 530
1442 */
1443
1444 snprintf(buf, sizeof(buf), "DELE %s\r\n", file);
1445 buf[sizeof(buf) - 1] = 0;
1446 len = strlen(buf);
1447#ifdef DEBUG_FTP
1448 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1449#endif
1450 res = send(ctxt->controlFd, buf, len, 0);
1451 if (res < 0) return(res);
1452 res = xmlNanoFTPGetResponse(ctxt);
1453 if (res == 4) {
1454 return(-1);
1455 }
1456 if (res == 2) return(1);
1457 if (res == 5) {
1458 return(0);
1459 }
1460 return(0);
1461}
1462/**
Owen Taylor3473f882001-02-23 17:55:21 +00001463 * xmlNanoFTPGetConnection:
1464 * @ctx: an FTP context
1465 *
1466 * Try to open a data connection to the server. Currently only
1467 * passive mode is supported.
1468 *
1469 * Returns -1 incase of error, 0 otherwise
1470 */
1471
1472int
1473xmlNanoFTPGetConnection(void *ctx) {
1474 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1475 char buf[200], *cur;
1476 int len, i;
1477 int res;
1478 unsigned char ad[6], *adp, *portp;
1479 unsigned int temp[6];
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001480#ifdef SUPPORT_IP6
1481 struct sockaddr_storage dataAddr;
1482#else
Owen Taylor3473f882001-02-23 17:55:21 +00001483 struct sockaddr_in dataAddr;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001484#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001485 SOCKLEN_T dataAddrLen;
1486
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001487 memset (&dataAddr, 0, sizeof(dataAddr));
1488#ifdef SUPPORT_IP6
1489 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1490 ctxt->dataFd = socket (AF_INET6, SOCK_STREAM, IPPROTO_TCP);
1491 ((struct sockaddr_in6 *)&dataAddr)->sin6_family = AF_INET6;
1492 dataAddrLen = sizeof(struct sockaddr_in6);
1493 } else
1494#endif
1495 {
1496 ctxt->dataFd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1497 ((struct sockaddr_in *)&dataAddr)->sin_family = AF_INET;
1498 dataAddrLen = sizeof (struct sockaddr_in);
Owen Taylor3473f882001-02-23 17:55:21 +00001499 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001500
1501 if (ctxt->dataFd < 0) {
1502 xmlGenericError (xmlGenericErrorContext,
1503 "xmlNanoFTPGetConnection: failed to create socket\n");
1504 return (-1);
1505 }
Owen Taylor3473f882001-02-23 17:55:21 +00001506
1507 if (ctxt->passive) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001508#ifdef SUPPORT_IP6
1509 if ((ctxt->ftpAddr).ss_family == AF_INET6)
1510 snprintf (buf, sizeof(buf), "EPSV\r\n");
1511 else
1512#endif
1513 snprintf (buf, sizeof(buf), "PASV\r\n");
1514 len = strlen (buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001515#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001516 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001517#endif
1518 res = send(ctxt->controlFd, buf, len, 0);
1519 if (res < 0) {
1520 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1521 return(res);
1522 }
1523 res = xmlNanoFTPReadResponse(ctx);
1524 if (res != 2) {
1525 if (res == 5) {
1526 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1527 return(-1);
1528 } else {
1529 /*
1530 * retry with an active connection
1531 */
1532 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1533 ctxt->passive = 0;
1534 }
1535 }
1536 cur = &ctxt->controlBuf[ctxt->controlBufAnswer];
1537 while (((*cur < '0') || (*cur > '9')) && *cur != '\0') cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001538#ifdef SUPPORT_IP6
1539 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1540 if (sscanf (cur, "%u", &temp[0]) != 1) {
1541 xmlGenericError (xmlGenericErrorContext,
1542 "Invalid answer to EPSV\n");
1543 if (ctxt->dataFd != -1) {
1544 closesocket (ctxt->dataFd); ctxt->dataFd = -1;
1545 }
1546 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001547 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001548 memcpy (&((struct sockaddr_in6 *)&dataAddr)->sin6_addr, &((struct sockaddr_in6 *)&ctxt->ftpAddr)->sin6_addr, sizeof(struct in6_addr));
1549 ((struct sockaddr_in6 *)&dataAddr)->sin6_port = htons (temp[0]);
Owen Taylor3473f882001-02-23 17:55:21 +00001550 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001551 else
1552#endif
1553 {
1554 if (sscanf (cur, "%u,%u,%u,%u,%u,%u", &temp[0], &temp[1], &temp[2],
1555 &temp[3], &temp[4], &temp[5]) != 6) {
1556 xmlGenericError (xmlGenericErrorContext,
1557 "Invalid answer to PASV\n");
1558 if (ctxt->dataFd != -1) {
1559 closesocket (ctxt->dataFd); ctxt->dataFd = -1;
1560 }
1561 return (-1);
1562 }
1563 for (i=0; i<6; i++) ad[i] = (unsigned char) (temp[i] & 0xff);
1564 memcpy (&((struct sockaddr_in *)&dataAddr)->sin_addr, &ad[0], 4);
1565 memcpy (&((struct sockaddr_in *)&dataAddr)->sin_port, &ad[4], 2);
1566 }
1567
Owen Taylor3473f882001-02-23 17:55:21 +00001568 if (connect(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
1569 xmlGenericError(xmlGenericErrorContext,
1570 "Failed to create a data connection\n");
1571 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1572 return (-1);
1573 }
1574 } else {
1575 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001576#ifdef SUPPORT_IP6
1577 if ((ctxt->ftpAddr).ss_family == AF_INET6)
1578 ((struct sockaddr_in6 *)&dataAddr)->sin6_port = 0;
1579 else
1580#endif
1581 ((struct sockaddr_in *)&dataAddr)->sin_port = 0;
1582
Owen Taylor3473f882001-02-23 17:55:21 +00001583 if (bind(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
1584 xmlGenericError(xmlGenericErrorContext,
1585 "Failed to bind a port\n");
1586 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1587 return (-1);
1588 }
1589 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
1590
1591 if (listen(ctxt->dataFd, 1) < 0) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001592#ifdef SUPPORT_IP6
1593 if ((ctxt->ftpAddr).ss_family == AF_INET6)
1594 xmlGenericError (xmlGenericErrorContext,
1595 "Could not listen on port %d\n",
1596 ntohs (((struct sockaddr_in6 *)&dataAddr)->sin6_port));
1597 else
1598#endif
1599 xmlGenericError (xmlGenericErrorContext,
1600 "Could not listen on port %d\n",
1601 ntohs (((struct sockaddr_in *)&dataAddr)->sin_port));
Owen Taylor3473f882001-02-23 17:55:21 +00001602 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1603 return (-1);
1604 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001605#ifdef SUPPORT_IP6
1606 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1607 char buf6[INET6_ADDRSTRLEN];
1608 inet_ntop (AF_INET6, &((struct sockaddr_in6 *)&dataAddr)->sin6_addr,
1609 buf6, INET6_ADDRSTRLEN);
Daniel Veillard2db8c122003-07-08 12:16:59 +00001610 adp = (unsigned char *) buf6;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001611 portp = (unsigned char *) &((struct sockaddr_in6 *)&dataAddr)->sin6_port;
1612 snprintf (buf, sizeof(buf), "EPRT |2|%s|%s|\r\n", adp, portp);
1613 } else
1614#endif
1615 {
1616 adp = (unsigned char *) &((struct sockaddr_in *)&dataAddr)->sin_addr;
1617 portp = (unsigned char *) &((struct sockaddr_in *)&dataAddr)->sin_port;
1618 snprintf (buf, sizeof(buf), "PORT %d,%d,%d,%d,%d,%d\r\n",
1619 adp[0] & 0xff, adp[1] & 0xff, adp[2] & 0xff, adp[3] & 0xff,
1620 portp[0] & 0xff, portp[1] & 0xff);
1621 }
1622
Owen Taylor3473f882001-02-23 17:55:21 +00001623 buf[sizeof(buf) - 1] = 0;
1624 len = strlen(buf);
1625#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001626 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001627#endif
1628
1629 res = send(ctxt->controlFd, buf, len, 0);
1630 if (res < 0) {
1631 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1632 return(res);
1633 }
1634 res = xmlNanoFTPGetResponse(ctxt);
1635 if (res != 2) {
1636 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1637 return(-1);
1638 }
1639 }
1640 return(ctxt->dataFd);
1641
1642}
1643
1644/**
1645 * xmlNanoFTPCloseConnection:
1646 * @ctx: an FTP context
1647 *
1648 * Close the data connection from the server
1649 *
1650 * Returns -1 incase of error, 0 otherwise
1651 */
1652
1653int
1654xmlNanoFTPCloseConnection(void *ctx) {
1655 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1656 int res;
1657 fd_set rfd, efd;
1658 struct timeval tv;
1659
1660 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1661 tv.tv_sec = 15;
1662 tv.tv_usec = 0;
1663 FD_ZERO(&rfd);
1664 FD_SET(ctxt->controlFd, &rfd);
1665 FD_ZERO(&efd);
1666 FD_SET(ctxt->controlFd, &efd);
1667 res = select(ctxt->controlFd + 1, &rfd, NULL, &efd, &tv);
1668 if (res < 0) {
1669#ifdef DEBUG_FTP
1670 perror("select");
1671#endif
1672 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1673 return(-1);
1674 }
1675 if (res == 0) {
1676#ifdef DEBUG_FTP
1677 xmlGenericError(xmlGenericErrorContext,
1678 "xmlNanoFTPCloseConnection: timeout\n");
1679#endif
1680 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1681 } else {
1682 res = xmlNanoFTPGetResponse(ctxt);
1683 if (res != 2) {
1684 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1685 return(-1);
1686 }
1687 }
1688 return(0);
1689}
1690
1691/**
1692 * xmlNanoFTPParseList:
1693 * @list: some data listing received from the server
1694 * @callback: the user callback
1695 * @userData: the user callback data
1696 *
1697 * Parse at most one entry from the listing.
1698 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001699 * Returns -1 incase of error, the length of data parsed otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00001700 */
1701
1702static int
1703xmlNanoFTPParseList(const char *list, ftpListCallback callback, void *userData) {
1704 const char *cur = list;
1705 char filename[151];
1706 char attrib[11];
1707 char owner[11];
1708 char group[11];
1709 char month[4];
1710 int year = 0;
1711 int minute = 0;
1712 int hour = 0;
1713 int day = 0;
1714 unsigned long size = 0;
1715 int links = 0;
1716 int i;
1717
1718 if (!strncmp(cur, "total", 5)) {
1719 cur += 5;
1720 while (*cur == ' ') cur++;
1721 while ((*cur >= '0') && (*cur <= '9'))
1722 links = (links * 10) + (*cur++ - '0');
1723 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1724 cur++;
1725 return(cur - list);
1726 } else if (*list == '+') {
1727 return(0);
1728 } else {
1729 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1730 cur++;
1731 if (*cur == 0) return(0);
1732 i = 0;
1733 while (*cur != ' ') {
1734 if (i < 10)
1735 attrib[i++] = *cur;
1736 cur++;
1737 if (*cur == 0) return(0);
1738 }
1739 attrib[10] = 0;
1740 while (*cur == ' ') cur++;
1741 if (*cur == 0) return(0);
1742 while ((*cur >= '0') && (*cur <= '9'))
1743 links = (links * 10) + (*cur++ - '0');
1744 while (*cur == ' ') cur++;
1745 if (*cur == 0) return(0);
1746 i = 0;
1747 while (*cur != ' ') {
1748 if (i < 10)
1749 owner[i++] = *cur;
1750 cur++;
1751 if (*cur == 0) return(0);
1752 }
1753 owner[i] = 0;
1754 while (*cur == ' ') cur++;
1755 if (*cur == 0) return(0);
1756 i = 0;
1757 while (*cur != ' ') {
1758 if (i < 10)
1759 group[i++] = *cur;
1760 cur++;
1761 if (*cur == 0) return(0);
1762 }
1763 group[i] = 0;
1764 while (*cur == ' ') cur++;
1765 if (*cur == 0) return(0);
1766 while ((*cur >= '0') && (*cur <= '9'))
1767 size = (size * 10) + (*cur++ - '0');
1768 while (*cur == ' ') cur++;
1769 if (*cur == 0) return(0);
1770 i = 0;
1771 while (*cur != ' ') {
1772 if (i < 3)
1773 month[i++] = *cur;
1774 cur++;
1775 if (*cur == 0) return(0);
1776 }
1777 month[i] = 0;
1778 while (*cur == ' ') cur++;
1779 if (*cur == 0) return(0);
1780 while ((*cur >= '0') && (*cur <= '9'))
1781 day = (day * 10) + (*cur++ - '0');
1782 while (*cur == ' ') cur++;
1783 if (*cur == 0) return(0);
1784 if ((cur[1] == 0) || (cur[2] == 0)) return(0);
1785 if ((cur[1] == ':') || (cur[2] == ':')) {
1786 while ((*cur >= '0') && (*cur <= '9'))
1787 hour = (hour * 10) + (*cur++ - '0');
1788 if (*cur == ':') cur++;
1789 while ((*cur >= '0') && (*cur <= '9'))
1790 minute = (minute * 10) + (*cur++ - '0');
1791 } else {
1792 while ((*cur >= '0') && (*cur <= '9'))
1793 year = (year * 10) + (*cur++ - '0');
1794 }
1795 while (*cur == ' ') cur++;
1796 if (*cur == 0) return(0);
1797 i = 0;
1798 while ((*cur != '\n') && (*cur != '\r')) {
1799 if (i < 150)
1800 filename[i++] = *cur;
1801 cur++;
1802 if (*cur == 0) return(0);
1803 }
1804 filename[i] = 0;
1805 if ((*cur != '\n') && (*cur != '\r'))
1806 return(0);
1807 while ((*cur == '\n') || (*cur == '\r'))
1808 cur++;
1809 }
1810 if (callback != NULL) {
1811 callback(userData, filename, attrib, owner, group, size, links,
1812 year, month, day, hour, minute);
1813 }
1814 return(cur - list);
1815}
1816
1817/**
1818 * xmlNanoFTPList:
1819 * @ctx: an FTP context
1820 * @callback: the user callback
1821 * @userData: the user callback data
1822 * @filename: optional files to list
1823 *
1824 * Do a listing on the server. All files info are passed back
1825 * in the callbacks.
1826 *
1827 * Returns -1 incase of error, 0 otherwise
1828 */
1829
1830int
1831xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData,
1832 char *filename) {
1833 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1834 char buf[4096 + 1];
1835 int len, res;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001836 int indx = 0, base;
Owen Taylor3473f882001-02-23 17:55:21 +00001837 fd_set rfd, efd;
1838 struct timeval tv;
1839
1840 if (filename == NULL) {
1841 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1842 return(-1);
1843 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1844 if (ctxt->dataFd == -1)
1845 return(-1);
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001846 snprintf(buf, sizeof(buf), "LIST -L\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001847 } else {
1848 if (filename[0] != '/') {
1849 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1850 return(-1);
1851 }
1852 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1853 if (ctxt->dataFd == -1)
1854 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001855 snprintf(buf, sizeof(buf), "LIST -L %s\r\n", filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001856 }
1857 buf[sizeof(buf) - 1] = 0;
1858 len = strlen(buf);
1859#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001860 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001861#endif
1862 res = send(ctxt->controlFd, buf, len, 0);
1863 if (res < 0) {
1864 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1865 return(res);
1866 }
1867 res = xmlNanoFTPReadResponse(ctxt);
1868 if (res != 1) {
1869 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1870 return(-res);
1871 }
1872
1873 do {
1874 tv.tv_sec = 1;
1875 tv.tv_usec = 0;
1876 FD_ZERO(&rfd);
1877 FD_SET(ctxt->dataFd, &rfd);
1878 FD_ZERO(&efd);
1879 FD_SET(ctxt->dataFd, &efd);
1880 res = select(ctxt->dataFd + 1, &rfd, NULL, &efd, &tv);
1881 if (res < 0) {
1882#ifdef DEBUG_FTP
1883 perror("select");
1884#endif
1885 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1886 return(-1);
1887 }
1888 if (res == 0) {
1889 res = xmlNanoFTPCheckResponse(ctxt);
1890 if (res < 0) {
1891 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1892 ctxt->dataFd = -1;
1893 return(-1);
1894 }
1895 if (res == 2) {
1896 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1897 return(0);
1898 }
1899
1900 continue;
1901 }
1902
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001903 if ((len = recv(ctxt->dataFd, &buf[indx], sizeof(buf) - (indx + 1), 0)) < 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00001904#ifdef DEBUG_FTP
1905 perror("recv");
1906#endif
1907 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1908 ctxt->dataFd = -1;
1909 return(-1);
1910 }
1911#ifdef DEBUG_FTP
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001912 write(1, &buf[indx], len);
Owen Taylor3473f882001-02-23 17:55:21 +00001913#endif
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001914 indx += len;
1915 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001916 base = 0;
1917 do {
1918 res = xmlNanoFTPParseList(&buf[base], callback, userData);
1919 base += res;
1920 } while (res > 0);
1921
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001922 memmove(&buf[0], &buf[base], indx - base);
1923 indx -= base;
Owen Taylor3473f882001-02-23 17:55:21 +00001924 } while (len != 0);
1925 xmlNanoFTPCloseConnection(ctxt);
1926 return(0);
1927}
1928
1929/**
1930 * xmlNanoFTPGetSocket:
1931 * @ctx: an FTP context
1932 * @filename: the file to retrieve (or NULL if path is in context).
1933 *
1934 * Initiate fetch of the given file from the server.
1935 *
1936 * Returns the socket for the data connection, or <0 in case of error
1937 */
1938
1939
1940int
1941xmlNanoFTPGetSocket(void *ctx, const char *filename) {
1942 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1943 char buf[300];
1944 int res, len;
1945 if ((filename == NULL) && (ctxt->path == NULL))
1946 return(-1);
1947 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1948 if (ctxt->dataFd == -1)
1949 return(-1);
1950
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001951 snprintf(buf, sizeof(buf), "TYPE I\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001952 len = strlen(buf);
1953#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001954 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001955#endif
1956 res = send(ctxt->controlFd, buf, len, 0);
1957 if (res < 0) {
1958 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1959 return(res);
1960 }
1961 res = xmlNanoFTPReadResponse(ctxt);
1962 if (res != 2) {
1963 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1964 return(-res);
1965 }
1966 if (filename == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001967 snprintf(buf, sizeof(buf), "RETR %s\r\n", ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001968 else
Owen Taylor3473f882001-02-23 17:55:21 +00001969 snprintf(buf, sizeof(buf), "RETR %s\r\n", filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001970 buf[sizeof(buf) - 1] = 0;
1971 len = strlen(buf);
1972#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001973 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001974#endif
1975 res = send(ctxt->controlFd, buf, len, 0);
1976 if (res < 0) {
1977 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1978 return(res);
1979 }
1980 res = xmlNanoFTPReadResponse(ctxt);
1981 if (res != 1) {
1982 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1983 return(-res);
1984 }
1985 return(ctxt->dataFd);
1986}
1987
1988/**
1989 * xmlNanoFTPGet:
1990 * @ctx: an FTP context
1991 * @callback: the user callback
1992 * @userData: the user callback data
1993 * @filename: the file to retrieve
1994 *
1995 * Fetch the given file from the server. All data are passed back
1996 * in the callbacks. The last callback has a size of 0 block.
1997 *
1998 * Returns -1 incase of error, 0 otherwise
1999 */
2000
2001int
2002xmlNanoFTPGet(void *ctx, ftpDataCallback callback, void *userData,
2003 const char *filename) {
2004 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
2005 char buf[4096];
2006 int len = 0, res;
2007 fd_set rfd;
2008 struct timeval tv;
2009
2010 if ((filename == NULL) && (ctxt->path == NULL))
2011 return(-1);
2012 if (callback == NULL)
2013 return(-1);
2014 if (xmlNanoFTPGetSocket(ctxt, filename) < 0)
2015 return(-1);
2016
2017 do {
2018 tv.tv_sec = 1;
2019 tv.tv_usec = 0;
2020 FD_ZERO(&rfd);
2021 FD_SET(ctxt->dataFd, &rfd);
2022 res = select(ctxt->dataFd + 1, &rfd, NULL, NULL, &tv);
2023 if (res < 0) {
2024#ifdef DEBUG_FTP
2025 perror("select");
2026#endif
2027 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
2028 return(-1);
2029 }
2030 if (res == 0) {
2031 res = xmlNanoFTPCheckResponse(ctxt);
2032 if (res < 0) {
2033 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
2034 ctxt->dataFd = -1;
2035 return(-1);
2036 }
2037 if (res == 2) {
2038 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
2039 return(0);
2040 }
2041
2042 continue;
2043 }
2044 if ((len = recv(ctxt->dataFd, buf, sizeof(buf), 0)) < 0) {
2045 callback(userData, buf, len);
2046 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
2047 return(-1);
2048 }
2049 callback(userData, buf, len);
2050 } while (len != 0);
2051
2052 return(xmlNanoFTPCloseConnection(ctxt));
2053}
2054
2055/**
2056 * xmlNanoFTPRead:
2057 * @ctx: the FTP context
2058 * @dest: a buffer
2059 * @len: the buffer length
2060 *
2061 * This function tries to read @len bytes from the existing FTP connection
2062 * and saves them in @dest. This is a blocking call.
2063 *
2064 * Returns the number of byte read. 0 is an indication of an end of connection.
2065 * -1 indicates a parameter error.
2066 */
2067int
2068xmlNanoFTPRead(void *ctx, void *dest, int len) {
2069 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
2070
2071 if (ctx == NULL) return(-1);
2072 if (ctxt->dataFd < 0) return(0);
2073 if (dest == NULL) return(-1);
2074 if (len <= 0) return(0);
2075
2076 len = recv(ctxt->dataFd, dest, len, 0);
2077#ifdef DEBUG_FTP
2078 xmlGenericError(xmlGenericErrorContext, "Recvd %d bytes\n", len);
2079#endif
2080 if (len <= 0) {
2081 xmlNanoFTPCloseConnection(ctxt);
2082 }
2083 return(len);
2084}
2085
2086/**
2087 * xmlNanoFTPOpen:
2088 * @URL: the URL to the resource
2089 *
2090 * Start to fetch the given ftp:// resource
2091 *
2092 * Returns an FTP context, or NULL
2093 */
2094
2095void*
2096xmlNanoFTPOpen(const char *URL) {
2097 xmlNanoFTPCtxtPtr ctxt;
2098 int sock;
2099
2100 xmlNanoFTPInit();
2101 if (URL == NULL) return(NULL);
2102 if (strncmp("ftp://", URL, 6)) return(NULL);
2103
2104 ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(URL);
2105 if (ctxt == NULL) return(NULL);
2106 if (xmlNanoFTPConnect(ctxt) < 0) {
2107 xmlNanoFTPFreeCtxt(ctxt);
2108 return(NULL);
2109 }
2110 sock = xmlNanoFTPGetSocket(ctxt, ctxt->path);
2111 if (sock < 0) {
2112 xmlNanoFTPFreeCtxt(ctxt);
2113 return(NULL);
2114 }
2115 return(ctxt);
2116}
2117
2118/**
2119 * xmlNanoFTPClose:
2120 * @ctx: an FTP context
2121 *
2122 * Close the connection and both control and transport
2123 *
2124 * Returns -1 incase of error, 0 otherwise
2125 */
2126
2127int
2128xmlNanoFTPClose(void *ctx) {
2129 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
2130
2131 if (ctxt == NULL)
2132 return(-1);
2133
2134 if (ctxt->dataFd >= 0) {
2135 closesocket(ctxt->dataFd);
2136 ctxt->dataFd = -1;
2137 }
2138 if (ctxt->controlFd >= 0) {
2139 xmlNanoFTPQuit(ctxt);
2140 closesocket(ctxt->controlFd);
2141 ctxt->controlFd = -1;
2142 }
2143 xmlNanoFTPFreeCtxt(ctxt);
2144 return(0);
2145}
2146
2147#ifdef STANDALONE
2148/************************************************************************
2149 * *
2150 * Basic test in Standalone mode *
2151 * *
2152 ************************************************************************/
Daniel Veillard01c13b52002-12-10 15:19:08 +00002153static
Owen Taylor3473f882001-02-23 17:55:21 +00002154void ftpList(void *userData, const char *filename, const char* attrib,
2155 const char *owner, const char *group, unsigned long size, int links,
2156 int year, const char *month, int day, int hour, int minute) {
2157 xmlGenericError(xmlGenericErrorContext,
2158 "%s %s %s %ld %s\n", attrib, owner, group, size, filename);
2159}
Daniel Veillard01c13b52002-12-10 15:19:08 +00002160static
Owen Taylor3473f882001-02-23 17:55:21 +00002161void ftpData(void *userData, const char *data, int len) {
2162 if (userData == NULL) return;
2163 if (len <= 0) {
2164 fclose(userData);
2165 return;
2166 }
2167 fwrite(data, len, 1, userData);
2168}
2169
2170int main(int argc, char **argv) {
2171 void *ctxt;
2172 FILE *output;
2173 char *tstfile = NULL;
2174
2175 xmlNanoFTPInit();
2176 if (argc > 1) {
2177 ctxt = xmlNanoFTPNewCtxt(argv[1]);
2178 if (xmlNanoFTPConnect(ctxt) < 0) {
2179 xmlGenericError(xmlGenericErrorContext,
2180 "Couldn't connect to %s\n", argv[1]);
2181 exit(1);
2182 }
2183 if (argc > 2)
2184 tstfile = argv[2];
2185 } else
2186 ctxt = xmlNanoFTPConnectTo("localhost", 0);
2187 if (ctxt == NULL) {
2188 xmlGenericError(xmlGenericErrorContext,
2189 "Couldn't connect to localhost\n");
2190 exit(1);
2191 }
2192 xmlNanoFTPList(ctxt, ftpList, NULL, tstfile);
2193 output = fopen("/tmp/tstdata", "w");
2194 if (output != NULL) {
2195 if (xmlNanoFTPGet(ctxt, ftpData, (void *) output, tstfile) < 0)
2196 xmlGenericError(xmlGenericErrorContext,
2197 "Failed to get file\n");
2198
2199 }
2200 xmlNanoFTPClose(ctxt);
2201 xmlMemoryDump();
2202 exit(0);
2203}
2204#endif /* STANDALONE */
2205#else /* !LIBXML_FTP_ENABLED */
2206#ifdef STANDALONE
2207#include <stdio.h>
2208int main(int argc, char **argv) {
2209 xmlGenericError(xmlGenericErrorContext,
2210 "%s : FTP support not compiled in\n", argv[0]);
2211 return(0);
2212}
2213#endif /* STANDALONE */
2214#endif /* LIBXML_FTP_ENABLED */