blob: b6fd9a11057876b5e883f4e31fa06808fd28cb0f [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;
Owen Taylor3473f882001-02-23 17:55:21 +0000981
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000982 snprintf(buf, sizeof(buf), "QUIT\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000983 len = strlen(buf);
984#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +0000985 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 +0000986#endif
William M. Brack78637da2003-07-31 14:47:38 +0000987 send(ctxt->controlFd, buf, len, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000988 return(0);
989}
990
991/**
992 * xmlNanoFTPConnect:
993 * @ctx: an FTP context
994 *
995 * Tries to open a control connection
996 *
997 * Returns -1 in case of error, 0 otherwise
998 */
999
1000int
1001xmlNanoFTPConnect(void *ctx) {
1002 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1003 struct hostent *hp;
1004 int port;
1005 int res;
Daniel Veillard2db8c122003-07-08 12:16:59 +00001006 int addrlen = sizeof (struct sockaddr_in);
Owen Taylor3473f882001-02-23 17:55:21 +00001007
1008 if (ctxt == NULL)
1009 return(-1);
1010 if (ctxt->hostname == NULL)
1011 return(-1);
1012
1013 /*
1014 * do the blocking DNS query.
1015 */
Owen Taylor3473f882001-02-23 17:55:21 +00001016 if (proxy) {
1017 port = proxyPort;
1018 } else {
1019 port = ctxt->port;
1020 }
1021 if (port == 0)
1022 port = 21;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001023
1024 memset (&ctxt->ftpAddr, 0, sizeof(ctxt->ftpAddr));
1025
1026#ifdef SUPPORT_IP6
1027 if (have_ipv6 ()) {
Daniel Veillard2db8c122003-07-08 12:16:59 +00001028 struct addrinfo hints, *tmp, *result;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001029
1030 result = NULL;
1031 memset (&hints, 0, sizeof(hints));
1032 hints.ai_socktype = SOCK_STREAM;
1033
1034 if (proxy) {
1035 if (getaddrinfo (proxy, NULL, &hints, &result) != 0)
1036 return (-1);
1037 }
1038 else
1039 if (getaddrinfo (ctxt->hostname, NULL, &hints, &result) != 0)
1040 return (-1);
1041
Daniel Veillard2db8c122003-07-08 12:16:59 +00001042 for (tmp = result; tmp; tmp = tmp->ai_next)
1043 if (tmp->ai_family == AF_INET || tmp->ai_family == AF_INET6)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001044 break;
1045
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001046 if (!tmp) {
1047 if (result)
1048 freeaddrinfo (result);
1049 return (-1);
1050 }
1051 else {
Daniel Veillard2db8c122003-07-08 12:16:59 +00001052 if (tmp->ai_family == AF_INET6) {
1053 memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001054 ((struct sockaddr_in6 *) &ctxt->ftpAddr)->sin6_port = htons (port);
1055 ctxt->controlFd = socket (AF_INET6, SOCK_STREAM, 0);
1056 }
1057 else {
Daniel Veillard2db8c122003-07-08 12:16:59 +00001058 memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001059 ((struct sockaddr_in *) &ctxt->ftpAddr)->sin_port = htons (port);
1060 ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0);
1061 }
Daniel Veillard2db8c122003-07-08 12:16:59 +00001062 addrlen = tmp->ai_addrlen;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001063 freeaddrinfo (result);
1064 }
1065 }
1066 else
1067#endif
1068 {
1069 if (proxy)
1070 hp = gethostbyname (proxy);
1071 else
1072 hp = gethostbyname (ctxt->hostname);
1073 if (hp == NULL)
1074 return (-1);
1075
1076 /*
1077 * Prepare the socket
1078 */
1079 ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_family = AF_INET;
1080 memcpy (&((struct sockaddr_in *)&ctxt->ftpAddr)->sin_addr,
1081 hp->h_addr_list[0], hp->h_length);
1082 ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_port = htons (port);
1083 ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0);
1084 addrlen = sizeof (struct sockaddr_in);
1085 }
1086
Owen Taylor3473f882001-02-23 17:55:21 +00001087 if (ctxt->controlFd < 0)
1088 return(-1);
1089
1090 /*
1091 * Do the connect.
1092 */
1093 if (connect(ctxt->controlFd, (struct sockaddr *) &ctxt->ftpAddr,
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001094 addrlen) < 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00001095 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1096 ctxt->controlFd = -1;
1097 return(-1);
1098 }
1099
1100 /*
1101 * Wait for the HELLO from the server.
1102 */
1103 res = xmlNanoFTPGetResponse(ctxt);
1104 if (res != 2) {
1105 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1106 ctxt->controlFd = -1;
1107 return(-1);
1108 }
1109
1110 /*
1111 * State diagram for the login operation on the FTP server
1112 *
1113 * Reference: RFC 959
1114 *
1115 * 1
1116 * +---+ USER +---+------------->+---+
1117 * | B |---------->| W | 2 ---->| E |
1118 * +---+ +---+------ | -->+---+
1119 * | | | | |
1120 * 3 | | 4,5 | | |
1121 * -------------- ----- | | |
1122 * | | | | |
1123 * | | | | |
1124 * | --------- |
1125 * | 1| | | |
1126 * V | | | |
1127 * +---+ PASS +---+ 2 | ------>+---+
1128 * | |---------->| W |------------->| S |
1129 * +---+ +---+ ---------->+---+
1130 * | | | | |
1131 * 3 | |4,5| | |
1132 * -------------- -------- |
1133 * | | | | |
1134 * | | | | |
1135 * | -----------
1136 * | 1,3| | | |
1137 * V | 2| | |
1138 * +---+ ACCT +---+-- | ----->+---+
1139 * | |---------->| W | 4,5 -------->| F |
1140 * +---+ +---+------------->+---+
1141 *
1142 * Of course in case of using a proxy this get really nasty and is not
1143 * standardized at all :-(
1144 */
1145 if (proxy) {
1146 int len;
1147 char buf[400];
1148
1149 if (proxyUser != NULL) {
1150 /*
1151 * We need proxy auth
1152 */
Owen Taylor3473f882001-02-23 17:55:21 +00001153 snprintf(buf, sizeof(buf), "USER %s\r\n", proxyUser);
Owen Taylor3473f882001-02-23 17:55:21 +00001154 buf[sizeof(buf) - 1] = 0;
1155 len = strlen(buf);
1156#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001157 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001158#endif
1159 res = send(ctxt->controlFd, buf, len, 0);
1160 if (res < 0) {
1161 closesocket(ctxt->controlFd);
1162 ctxt->controlFd = -1;
1163 return(res);
1164 }
1165 res = xmlNanoFTPGetResponse(ctxt);
1166 switch (res) {
1167 case 2:
1168 if (proxyPasswd == NULL)
1169 break;
1170 case 3:
1171 if (proxyPasswd != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001172 snprintf(buf, sizeof(buf), "PASS %s\r\n", proxyPasswd);
Owen Taylor3473f882001-02-23 17:55:21 +00001173 else
Daniel Veillard9ae1eba2001-10-19 09:48:35 +00001174 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001175 buf[sizeof(buf) - 1] = 0;
1176 len = strlen(buf);
1177#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001178 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001179#endif
1180 res = send(ctxt->controlFd, buf, len, 0);
1181 if (res < 0) {
1182 closesocket(ctxt->controlFd);
1183 ctxt->controlFd = -1;
1184 return(res);
1185 }
1186 res = xmlNanoFTPGetResponse(ctxt);
1187 if (res > 3) {
1188 closesocket(ctxt->controlFd);
1189 ctxt->controlFd = -1;
1190 return(-1);
1191 }
1192 break;
1193 case 1:
1194 break;
1195 case 4:
1196 case 5:
1197 case -1:
1198 default:
1199 closesocket(ctxt->controlFd);
1200 ctxt->controlFd = -1;
1201 return(-1);
1202 }
1203 }
1204
1205 /*
1206 * We assume we don't need more authentication to the proxy
1207 * and that it succeeded :-\
1208 */
1209 switch (proxyType) {
1210 case 0:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001211 /* we will try in sequence */
Owen Taylor3473f882001-02-23 17:55:21 +00001212 case 1:
1213 /* Using SITE command */
Owen Taylor3473f882001-02-23 17:55:21 +00001214 snprintf(buf, sizeof(buf), "SITE %s\r\n", ctxt->hostname);
Owen Taylor3473f882001-02-23 17:55:21 +00001215 buf[sizeof(buf) - 1] = 0;
1216 len = strlen(buf);
1217#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001218 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001219#endif
1220 res = send(ctxt->controlFd, buf, len, 0);
1221 if (res < 0) {
1222 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1223 ctxt->controlFd = -1;
1224 return(res);
1225 }
1226 res = xmlNanoFTPGetResponse(ctxt);
1227 if (res == 2) {
1228 /* we assume it worked :-\ 1 is error for SITE command */
1229 proxyType = 1;
1230 break;
1231 }
1232 if (proxyType == 1) {
1233 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1234 ctxt->controlFd = -1;
1235 return(-1);
1236 }
1237 case 2:
1238 /* USER user@host command */
1239 if (ctxt->user == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001240 snprintf(buf, sizeof(buf), "USER anonymous@%s\r\n",
1241 ctxt->hostname);
Owen Taylor3473f882001-02-23 17:55:21 +00001242 else
Owen Taylor3473f882001-02-23 17:55:21 +00001243 snprintf(buf, sizeof(buf), "USER %s@%s\r\n",
1244 ctxt->user, ctxt->hostname);
Owen Taylor3473f882001-02-23 17:55:21 +00001245 buf[sizeof(buf) - 1] = 0;
1246 len = strlen(buf);
1247#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001248 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001249#endif
1250 res = send(ctxt->controlFd, buf, len, 0);
1251 if (res < 0) {
1252 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1253 ctxt->controlFd = -1;
1254 return(res);
1255 }
1256 res = xmlNanoFTPGetResponse(ctxt);
1257 if ((res == 1) || (res == 2)) {
1258 /* we assume it worked :-\ */
1259 proxyType = 2;
1260 return(0);
1261 }
1262 if (ctxt->passwd == NULL)
Daniel Veillard9ae1eba2001-10-19 09:48:35 +00001263 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001264 else
Owen Taylor3473f882001-02-23 17:55:21 +00001265 snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
Owen Taylor3473f882001-02-23 17:55:21 +00001266 buf[sizeof(buf) - 1] = 0;
1267 len = strlen(buf);
1268#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001269 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001270#endif
1271 res = send(ctxt->controlFd, buf, len, 0);
1272 if (res < 0) {
1273 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1274 ctxt->controlFd = -1;
1275 return(res);
1276 }
1277 res = xmlNanoFTPGetResponse(ctxt);
1278 if ((res == 1) || (res == 2)) {
1279 /* we assume it worked :-\ */
1280 proxyType = 2;
1281 return(0);
1282 }
1283 if (proxyType == 2) {
1284 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1285 ctxt->controlFd = -1;
1286 return(-1);
1287 }
1288 case 3:
1289 /*
1290 * If you need support for other Proxy authentication scheme
1291 * send the code or at least the sequence in use.
1292 */
1293 default:
1294 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1295 ctxt->controlFd = -1;
1296 return(-1);
1297 }
1298 }
1299 /*
1300 * Non-proxy handling.
1301 */
1302 res = xmlNanoFTPSendUser(ctxt);
1303 if (res < 0) {
1304 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1305 ctxt->controlFd = -1;
1306 return(-1);
1307 }
1308 res = xmlNanoFTPGetResponse(ctxt);
1309 switch (res) {
1310 case 2:
1311 return(0);
1312 case 3:
1313 break;
1314 case 1:
1315 case 4:
1316 case 5:
1317 case -1:
1318 default:
1319 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1320 ctxt->controlFd = -1;
1321 return(-1);
1322 }
1323 res = xmlNanoFTPSendPasswd(ctxt);
1324 if (res < 0) {
1325 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1326 ctxt->controlFd = -1;
1327 return(-1);
1328 }
1329 res = xmlNanoFTPGetResponse(ctxt);
1330 switch (res) {
1331 case 2:
1332 break;
1333 case 3:
1334 xmlGenericError(xmlGenericErrorContext,
1335 "FTP server asking for ACCNT on anonymous\n");
1336 case 1:
1337 case 4:
1338 case 5:
1339 case -1:
1340 default:
1341 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1342 ctxt->controlFd = -1;
1343 return(-1);
1344 }
1345
1346 return(0);
1347}
1348
1349/**
1350 * xmlNanoFTPConnectTo:
1351 * @server: an FTP server name
1352 * @port: the port (use 21 if 0)
1353 *
1354 * Tries to open a control connection to the given server/port
1355 *
1356 * Returns an fTP context or NULL if it failed
1357 */
1358
1359void*
1360xmlNanoFTPConnectTo(const char *server, int port) {
1361 xmlNanoFTPCtxtPtr ctxt;
1362 int res;
1363
1364 xmlNanoFTPInit();
1365 if (server == NULL)
1366 return(NULL);
1367 ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(NULL);
1368 ctxt->hostname = xmlMemStrdup(server);
1369 if (port != 0)
1370 ctxt->port = port;
1371 res = xmlNanoFTPConnect(ctxt);
1372 if (res < 0) {
1373 xmlNanoFTPFreeCtxt(ctxt);
1374 return(NULL);
1375 }
1376 return(ctxt);
1377}
1378
1379/**
1380 * xmlNanoFTPCwd:
1381 * @ctx: an FTP context
1382 * @directory: a directory on the server
1383 *
1384 * Tries to change the remote directory
1385 *
1386 * Returns -1 incase of error, 1 if CWD worked, 0 if it failed
1387 */
1388
1389int
1390xmlNanoFTPCwd(void *ctx, char *directory) {
1391 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1392 char buf[400];
1393 int len;
1394 int res;
1395
1396 /*
1397 * Expected response code for CWD:
1398 *
1399 * CWD
1400 * 250
1401 * 500, 501, 502, 421, 530, 550
1402 */
Owen Taylor3473f882001-02-23 17:55:21 +00001403 snprintf(buf, sizeof(buf), "CWD %s\r\n", directory);
Owen Taylor3473f882001-02-23 17:55:21 +00001404 buf[sizeof(buf) - 1] = 0;
1405 len = strlen(buf);
1406#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001407 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001408#endif
1409 res = send(ctxt->controlFd, buf, len, 0);
1410 if (res < 0) return(res);
1411 res = xmlNanoFTPGetResponse(ctxt);
1412 if (res == 4) {
1413 return(-1);
1414 }
1415 if (res == 2) return(1);
1416 if (res == 5) {
1417 return(0);
1418 }
1419 return(0);
1420}
1421
1422/**
Daniel Veillard6c73cb82003-03-05 16:45:40 +00001423 * xmlNanoFTPDele:
1424 * @ctx: an FTP context
1425 * @file: a file or directory on the server
1426 *
1427 * Tries to delete an item (file or directory) from server
1428 *
1429 * Returns -1 incase of error, 1 if DELE worked, 0 if it failed
1430 */
1431
1432int
1433xmlNanoFTPDele(void *ctx, char *file) {
1434 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1435 char buf[400];
1436 int len;
1437 int res;
1438
1439 /*
1440 * Expected response code for DELE:
1441 *
1442 * DELE
1443 * 250
1444 * 450, 550
1445 * 500, 501, 502, 421, 530
1446 */
1447
1448 snprintf(buf, sizeof(buf), "DELE %s\r\n", file);
1449 buf[sizeof(buf) - 1] = 0;
1450 len = strlen(buf);
1451#ifdef DEBUG_FTP
1452 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1453#endif
1454 res = send(ctxt->controlFd, buf, len, 0);
1455 if (res < 0) return(res);
1456 res = xmlNanoFTPGetResponse(ctxt);
1457 if (res == 4) {
1458 return(-1);
1459 }
1460 if (res == 2) return(1);
1461 if (res == 5) {
1462 return(0);
1463 }
1464 return(0);
1465}
1466/**
Owen Taylor3473f882001-02-23 17:55:21 +00001467 * xmlNanoFTPGetConnection:
1468 * @ctx: an FTP context
1469 *
1470 * Try to open a data connection to the server. Currently only
1471 * passive mode is supported.
1472 *
1473 * Returns -1 incase of error, 0 otherwise
1474 */
1475
1476int
1477xmlNanoFTPGetConnection(void *ctx) {
1478 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1479 char buf[200], *cur;
1480 int len, i;
1481 int res;
1482 unsigned char ad[6], *adp, *portp;
1483 unsigned int temp[6];
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001484#ifdef SUPPORT_IP6
1485 struct sockaddr_storage dataAddr;
1486#else
Owen Taylor3473f882001-02-23 17:55:21 +00001487 struct sockaddr_in dataAddr;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001488#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001489 SOCKLEN_T dataAddrLen;
1490
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001491 memset (&dataAddr, 0, sizeof(dataAddr));
1492#ifdef SUPPORT_IP6
1493 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1494 ctxt->dataFd = socket (AF_INET6, SOCK_STREAM, IPPROTO_TCP);
1495 ((struct sockaddr_in6 *)&dataAddr)->sin6_family = AF_INET6;
1496 dataAddrLen = sizeof(struct sockaddr_in6);
1497 } else
1498#endif
1499 {
1500 ctxt->dataFd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1501 ((struct sockaddr_in *)&dataAddr)->sin_family = AF_INET;
1502 dataAddrLen = sizeof (struct sockaddr_in);
Owen Taylor3473f882001-02-23 17:55:21 +00001503 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001504
1505 if (ctxt->dataFd < 0) {
1506 xmlGenericError (xmlGenericErrorContext,
1507 "xmlNanoFTPGetConnection: failed to create socket\n");
1508 return (-1);
1509 }
Owen Taylor3473f882001-02-23 17:55:21 +00001510
1511 if (ctxt->passive) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001512#ifdef SUPPORT_IP6
1513 if ((ctxt->ftpAddr).ss_family == AF_INET6)
1514 snprintf (buf, sizeof(buf), "EPSV\r\n");
1515 else
1516#endif
1517 snprintf (buf, sizeof(buf), "PASV\r\n");
1518 len = strlen (buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001519#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001520 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001521#endif
1522 res = send(ctxt->controlFd, buf, len, 0);
1523 if (res < 0) {
1524 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1525 return(res);
1526 }
1527 res = xmlNanoFTPReadResponse(ctx);
1528 if (res != 2) {
1529 if (res == 5) {
1530 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1531 return(-1);
1532 } else {
1533 /*
1534 * retry with an active connection
1535 */
1536 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1537 ctxt->passive = 0;
1538 }
1539 }
1540 cur = &ctxt->controlBuf[ctxt->controlBufAnswer];
1541 while (((*cur < '0') || (*cur > '9')) && *cur != '\0') cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001542#ifdef SUPPORT_IP6
1543 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1544 if (sscanf (cur, "%u", &temp[0]) != 1) {
1545 xmlGenericError (xmlGenericErrorContext,
1546 "Invalid answer to EPSV\n");
1547 if (ctxt->dataFd != -1) {
1548 closesocket (ctxt->dataFd); ctxt->dataFd = -1;
1549 }
1550 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001551 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001552 memcpy (&((struct sockaddr_in6 *)&dataAddr)->sin6_addr, &((struct sockaddr_in6 *)&ctxt->ftpAddr)->sin6_addr, sizeof(struct in6_addr));
1553 ((struct sockaddr_in6 *)&dataAddr)->sin6_port = htons (temp[0]);
Owen Taylor3473f882001-02-23 17:55:21 +00001554 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001555 else
1556#endif
1557 {
1558 if (sscanf (cur, "%u,%u,%u,%u,%u,%u", &temp[0], &temp[1], &temp[2],
1559 &temp[3], &temp[4], &temp[5]) != 6) {
1560 xmlGenericError (xmlGenericErrorContext,
1561 "Invalid answer to PASV\n");
1562 if (ctxt->dataFd != -1) {
1563 closesocket (ctxt->dataFd); ctxt->dataFd = -1;
1564 }
1565 return (-1);
1566 }
1567 for (i=0; i<6; i++) ad[i] = (unsigned char) (temp[i] & 0xff);
1568 memcpy (&((struct sockaddr_in *)&dataAddr)->sin_addr, &ad[0], 4);
1569 memcpy (&((struct sockaddr_in *)&dataAddr)->sin_port, &ad[4], 2);
1570 }
1571
Owen Taylor3473f882001-02-23 17:55:21 +00001572 if (connect(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
1573 xmlGenericError(xmlGenericErrorContext,
1574 "Failed to create a data connection\n");
1575 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1576 return (-1);
1577 }
1578 } else {
1579 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001580#ifdef SUPPORT_IP6
1581 if ((ctxt->ftpAddr).ss_family == AF_INET6)
1582 ((struct sockaddr_in6 *)&dataAddr)->sin6_port = 0;
1583 else
1584#endif
1585 ((struct sockaddr_in *)&dataAddr)->sin_port = 0;
1586
Owen Taylor3473f882001-02-23 17:55:21 +00001587 if (bind(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
1588 xmlGenericError(xmlGenericErrorContext,
1589 "Failed to bind a port\n");
1590 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1591 return (-1);
1592 }
1593 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
1594
1595 if (listen(ctxt->dataFd, 1) < 0) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001596#ifdef SUPPORT_IP6
1597 if ((ctxt->ftpAddr).ss_family == AF_INET6)
1598 xmlGenericError (xmlGenericErrorContext,
1599 "Could not listen on port %d\n",
1600 ntohs (((struct sockaddr_in6 *)&dataAddr)->sin6_port));
1601 else
1602#endif
1603 xmlGenericError (xmlGenericErrorContext,
1604 "Could not listen on port %d\n",
1605 ntohs (((struct sockaddr_in *)&dataAddr)->sin_port));
Owen Taylor3473f882001-02-23 17:55:21 +00001606 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1607 return (-1);
1608 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001609#ifdef SUPPORT_IP6
1610 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1611 char buf6[INET6_ADDRSTRLEN];
1612 inet_ntop (AF_INET6, &((struct sockaddr_in6 *)&dataAddr)->sin6_addr,
1613 buf6, INET6_ADDRSTRLEN);
Daniel Veillard2db8c122003-07-08 12:16:59 +00001614 adp = (unsigned char *) buf6;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001615 portp = (unsigned char *) &((struct sockaddr_in6 *)&dataAddr)->sin6_port;
1616 snprintf (buf, sizeof(buf), "EPRT |2|%s|%s|\r\n", adp, portp);
1617 } else
1618#endif
1619 {
1620 adp = (unsigned char *) &((struct sockaddr_in *)&dataAddr)->sin_addr;
1621 portp = (unsigned char *) &((struct sockaddr_in *)&dataAddr)->sin_port;
1622 snprintf (buf, sizeof(buf), "PORT %d,%d,%d,%d,%d,%d\r\n",
1623 adp[0] & 0xff, adp[1] & 0xff, adp[2] & 0xff, adp[3] & 0xff,
1624 portp[0] & 0xff, portp[1] & 0xff);
1625 }
1626
Owen Taylor3473f882001-02-23 17:55:21 +00001627 buf[sizeof(buf) - 1] = 0;
1628 len = strlen(buf);
1629#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001630 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001631#endif
1632
1633 res = send(ctxt->controlFd, buf, len, 0);
1634 if (res < 0) {
1635 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1636 return(res);
1637 }
1638 res = xmlNanoFTPGetResponse(ctxt);
1639 if (res != 2) {
1640 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1641 return(-1);
1642 }
1643 }
1644 return(ctxt->dataFd);
1645
1646}
1647
1648/**
1649 * xmlNanoFTPCloseConnection:
1650 * @ctx: an FTP context
1651 *
1652 * Close the data connection from the server
1653 *
1654 * Returns -1 incase of error, 0 otherwise
1655 */
1656
1657int
1658xmlNanoFTPCloseConnection(void *ctx) {
1659 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1660 int res;
1661 fd_set rfd, efd;
1662 struct timeval tv;
1663
1664 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1665 tv.tv_sec = 15;
1666 tv.tv_usec = 0;
1667 FD_ZERO(&rfd);
1668 FD_SET(ctxt->controlFd, &rfd);
1669 FD_ZERO(&efd);
1670 FD_SET(ctxt->controlFd, &efd);
1671 res = select(ctxt->controlFd + 1, &rfd, NULL, &efd, &tv);
1672 if (res < 0) {
1673#ifdef DEBUG_FTP
1674 perror("select");
1675#endif
1676 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1677 return(-1);
1678 }
1679 if (res == 0) {
1680#ifdef DEBUG_FTP
1681 xmlGenericError(xmlGenericErrorContext,
1682 "xmlNanoFTPCloseConnection: timeout\n");
1683#endif
1684 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1685 } else {
1686 res = xmlNanoFTPGetResponse(ctxt);
1687 if (res != 2) {
1688 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1689 return(-1);
1690 }
1691 }
1692 return(0);
1693}
1694
1695/**
1696 * xmlNanoFTPParseList:
1697 * @list: some data listing received from the server
1698 * @callback: the user callback
1699 * @userData: the user callback data
1700 *
1701 * Parse at most one entry from the listing.
1702 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001703 * Returns -1 incase of error, the length of data parsed otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00001704 */
1705
1706static int
1707xmlNanoFTPParseList(const char *list, ftpListCallback callback, void *userData) {
1708 const char *cur = list;
1709 char filename[151];
1710 char attrib[11];
1711 char owner[11];
1712 char group[11];
1713 char month[4];
1714 int year = 0;
1715 int minute = 0;
1716 int hour = 0;
1717 int day = 0;
1718 unsigned long size = 0;
1719 int links = 0;
1720 int i;
1721
1722 if (!strncmp(cur, "total", 5)) {
1723 cur += 5;
1724 while (*cur == ' ') cur++;
1725 while ((*cur >= '0') && (*cur <= '9'))
1726 links = (links * 10) + (*cur++ - '0');
1727 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1728 cur++;
1729 return(cur - list);
1730 } else if (*list == '+') {
1731 return(0);
1732 } else {
1733 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1734 cur++;
1735 if (*cur == 0) return(0);
1736 i = 0;
1737 while (*cur != ' ') {
1738 if (i < 10)
1739 attrib[i++] = *cur;
1740 cur++;
1741 if (*cur == 0) return(0);
1742 }
1743 attrib[10] = 0;
1744 while (*cur == ' ') cur++;
1745 if (*cur == 0) return(0);
1746 while ((*cur >= '0') && (*cur <= '9'))
1747 links = (links * 10) + (*cur++ - '0');
1748 while (*cur == ' ') cur++;
1749 if (*cur == 0) return(0);
1750 i = 0;
1751 while (*cur != ' ') {
1752 if (i < 10)
1753 owner[i++] = *cur;
1754 cur++;
1755 if (*cur == 0) return(0);
1756 }
1757 owner[i] = 0;
1758 while (*cur == ' ') cur++;
1759 if (*cur == 0) return(0);
1760 i = 0;
1761 while (*cur != ' ') {
1762 if (i < 10)
1763 group[i++] = *cur;
1764 cur++;
1765 if (*cur == 0) return(0);
1766 }
1767 group[i] = 0;
1768 while (*cur == ' ') cur++;
1769 if (*cur == 0) return(0);
1770 while ((*cur >= '0') && (*cur <= '9'))
1771 size = (size * 10) + (*cur++ - '0');
1772 while (*cur == ' ') cur++;
1773 if (*cur == 0) return(0);
1774 i = 0;
1775 while (*cur != ' ') {
1776 if (i < 3)
1777 month[i++] = *cur;
1778 cur++;
1779 if (*cur == 0) return(0);
1780 }
1781 month[i] = 0;
1782 while (*cur == ' ') cur++;
1783 if (*cur == 0) return(0);
1784 while ((*cur >= '0') && (*cur <= '9'))
1785 day = (day * 10) + (*cur++ - '0');
1786 while (*cur == ' ') cur++;
1787 if (*cur == 0) return(0);
1788 if ((cur[1] == 0) || (cur[2] == 0)) return(0);
1789 if ((cur[1] == ':') || (cur[2] == ':')) {
1790 while ((*cur >= '0') && (*cur <= '9'))
1791 hour = (hour * 10) + (*cur++ - '0');
1792 if (*cur == ':') cur++;
1793 while ((*cur >= '0') && (*cur <= '9'))
1794 minute = (minute * 10) + (*cur++ - '0');
1795 } else {
1796 while ((*cur >= '0') && (*cur <= '9'))
1797 year = (year * 10) + (*cur++ - '0');
1798 }
1799 while (*cur == ' ') cur++;
1800 if (*cur == 0) return(0);
1801 i = 0;
1802 while ((*cur != '\n') && (*cur != '\r')) {
1803 if (i < 150)
1804 filename[i++] = *cur;
1805 cur++;
1806 if (*cur == 0) return(0);
1807 }
1808 filename[i] = 0;
1809 if ((*cur != '\n') && (*cur != '\r'))
1810 return(0);
1811 while ((*cur == '\n') || (*cur == '\r'))
1812 cur++;
1813 }
1814 if (callback != NULL) {
1815 callback(userData, filename, attrib, owner, group, size, links,
1816 year, month, day, hour, minute);
1817 }
1818 return(cur - list);
1819}
1820
1821/**
1822 * xmlNanoFTPList:
1823 * @ctx: an FTP context
1824 * @callback: the user callback
1825 * @userData: the user callback data
1826 * @filename: optional files to list
1827 *
1828 * Do a listing on the server. All files info are passed back
1829 * in the callbacks.
1830 *
1831 * Returns -1 incase of error, 0 otherwise
1832 */
1833
1834int
1835xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData,
1836 char *filename) {
1837 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1838 char buf[4096 + 1];
1839 int len, res;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001840 int indx = 0, base;
Owen Taylor3473f882001-02-23 17:55:21 +00001841 fd_set rfd, efd;
1842 struct timeval tv;
1843
1844 if (filename == NULL) {
1845 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1846 return(-1);
1847 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1848 if (ctxt->dataFd == -1)
1849 return(-1);
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001850 snprintf(buf, sizeof(buf), "LIST -L\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001851 } else {
1852 if (filename[0] != '/') {
1853 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1854 return(-1);
1855 }
1856 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1857 if (ctxt->dataFd == -1)
1858 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001859 snprintf(buf, sizeof(buf), "LIST -L %s\r\n", filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001860 }
1861 buf[sizeof(buf) - 1] = 0;
1862 len = strlen(buf);
1863#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001864 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001865#endif
1866 res = send(ctxt->controlFd, buf, len, 0);
1867 if (res < 0) {
1868 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1869 return(res);
1870 }
1871 res = xmlNanoFTPReadResponse(ctxt);
1872 if (res != 1) {
1873 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1874 return(-res);
1875 }
1876
1877 do {
1878 tv.tv_sec = 1;
1879 tv.tv_usec = 0;
1880 FD_ZERO(&rfd);
1881 FD_SET(ctxt->dataFd, &rfd);
1882 FD_ZERO(&efd);
1883 FD_SET(ctxt->dataFd, &efd);
1884 res = select(ctxt->dataFd + 1, &rfd, NULL, &efd, &tv);
1885 if (res < 0) {
1886#ifdef DEBUG_FTP
1887 perror("select");
1888#endif
1889 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1890 return(-1);
1891 }
1892 if (res == 0) {
1893 res = xmlNanoFTPCheckResponse(ctxt);
1894 if (res < 0) {
1895 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1896 ctxt->dataFd = -1;
1897 return(-1);
1898 }
1899 if (res == 2) {
1900 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1901 return(0);
1902 }
1903
1904 continue;
1905 }
1906
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001907 if ((len = recv(ctxt->dataFd, &buf[indx], sizeof(buf) - (indx + 1), 0)) < 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00001908#ifdef DEBUG_FTP
1909 perror("recv");
1910#endif
1911 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1912 ctxt->dataFd = -1;
1913 return(-1);
1914 }
1915#ifdef DEBUG_FTP
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001916 write(1, &buf[indx], len);
Owen Taylor3473f882001-02-23 17:55:21 +00001917#endif
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001918 indx += len;
1919 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001920 base = 0;
1921 do {
1922 res = xmlNanoFTPParseList(&buf[base], callback, userData);
1923 base += res;
1924 } while (res > 0);
1925
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001926 memmove(&buf[0], &buf[base], indx - base);
1927 indx -= base;
Owen Taylor3473f882001-02-23 17:55:21 +00001928 } while (len != 0);
1929 xmlNanoFTPCloseConnection(ctxt);
1930 return(0);
1931}
1932
1933/**
1934 * xmlNanoFTPGetSocket:
1935 * @ctx: an FTP context
1936 * @filename: the file to retrieve (or NULL if path is in context).
1937 *
1938 * Initiate fetch of the given file from the server.
1939 *
1940 * Returns the socket for the data connection, or <0 in case of error
1941 */
1942
1943
1944int
1945xmlNanoFTPGetSocket(void *ctx, const char *filename) {
1946 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1947 char buf[300];
1948 int res, len;
1949 if ((filename == NULL) && (ctxt->path == NULL))
1950 return(-1);
1951 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1952 if (ctxt->dataFd == -1)
1953 return(-1);
1954
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001955 snprintf(buf, sizeof(buf), "TYPE I\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001956 len = strlen(buf);
1957#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001958 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001959#endif
1960 res = send(ctxt->controlFd, buf, len, 0);
1961 if (res < 0) {
1962 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1963 return(res);
1964 }
1965 res = xmlNanoFTPReadResponse(ctxt);
1966 if (res != 2) {
1967 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1968 return(-res);
1969 }
1970 if (filename == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001971 snprintf(buf, sizeof(buf), "RETR %s\r\n", ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001972 else
Owen Taylor3473f882001-02-23 17:55:21 +00001973 snprintf(buf, sizeof(buf), "RETR %s\r\n", filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001974 buf[sizeof(buf) - 1] = 0;
1975 len = strlen(buf);
1976#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001977 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001978#endif
1979 res = send(ctxt->controlFd, buf, len, 0);
1980 if (res < 0) {
1981 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1982 return(res);
1983 }
1984 res = xmlNanoFTPReadResponse(ctxt);
1985 if (res != 1) {
1986 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1987 return(-res);
1988 }
1989 return(ctxt->dataFd);
1990}
1991
1992/**
1993 * xmlNanoFTPGet:
1994 * @ctx: an FTP context
1995 * @callback: the user callback
1996 * @userData: the user callback data
1997 * @filename: the file to retrieve
1998 *
1999 * Fetch the given file from the server. All data are passed back
2000 * in the callbacks. The last callback has a size of 0 block.
2001 *
2002 * Returns -1 incase of error, 0 otherwise
2003 */
2004
2005int
2006xmlNanoFTPGet(void *ctx, ftpDataCallback callback, void *userData,
2007 const char *filename) {
2008 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
2009 char buf[4096];
2010 int len = 0, res;
2011 fd_set rfd;
2012 struct timeval tv;
2013
2014 if ((filename == NULL) && (ctxt->path == NULL))
2015 return(-1);
2016 if (callback == NULL)
2017 return(-1);
2018 if (xmlNanoFTPGetSocket(ctxt, filename) < 0)
2019 return(-1);
2020
2021 do {
2022 tv.tv_sec = 1;
2023 tv.tv_usec = 0;
2024 FD_ZERO(&rfd);
2025 FD_SET(ctxt->dataFd, &rfd);
2026 res = select(ctxt->dataFd + 1, &rfd, NULL, NULL, &tv);
2027 if (res < 0) {
2028#ifdef DEBUG_FTP
2029 perror("select");
2030#endif
2031 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
2032 return(-1);
2033 }
2034 if (res == 0) {
2035 res = xmlNanoFTPCheckResponse(ctxt);
2036 if (res < 0) {
2037 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
2038 ctxt->dataFd = -1;
2039 return(-1);
2040 }
2041 if (res == 2) {
2042 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
2043 return(0);
2044 }
2045
2046 continue;
2047 }
2048 if ((len = recv(ctxt->dataFd, buf, sizeof(buf), 0)) < 0) {
2049 callback(userData, buf, len);
2050 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
2051 return(-1);
2052 }
2053 callback(userData, buf, len);
2054 } while (len != 0);
2055
2056 return(xmlNanoFTPCloseConnection(ctxt));
2057}
2058
2059/**
2060 * xmlNanoFTPRead:
2061 * @ctx: the FTP context
2062 * @dest: a buffer
2063 * @len: the buffer length
2064 *
2065 * This function tries to read @len bytes from the existing FTP connection
2066 * and saves them in @dest. This is a blocking call.
2067 *
2068 * Returns the number of byte read. 0 is an indication of an end of connection.
2069 * -1 indicates a parameter error.
2070 */
2071int
2072xmlNanoFTPRead(void *ctx, void *dest, int len) {
2073 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
2074
2075 if (ctx == NULL) return(-1);
2076 if (ctxt->dataFd < 0) return(0);
2077 if (dest == NULL) return(-1);
2078 if (len <= 0) return(0);
2079
2080 len = recv(ctxt->dataFd, dest, len, 0);
2081#ifdef DEBUG_FTP
2082 xmlGenericError(xmlGenericErrorContext, "Recvd %d bytes\n", len);
2083#endif
2084 if (len <= 0) {
2085 xmlNanoFTPCloseConnection(ctxt);
2086 }
2087 return(len);
2088}
2089
2090/**
2091 * xmlNanoFTPOpen:
2092 * @URL: the URL to the resource
2093 *
2094 * Start to fetch the given ftp:// resource
2095 *
2096 * Returns an FTP context, or NULL
2097 */
2098
2099void*
2100xmlNanoFTPOpen(const char *URL) {
2101 xmlNanoFTPCtxtPtr ctxt;
2102 int sock;
2103
2104 xmlNanoFTPInit();
2105 if (URL == NULL) return(NULL);
2106 if (strncmp("ftp://", URL, 6)) return(NULL);
2107
2108 ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(URL);
2109 if (ctxt == NULL) return(NULL);
2110 if (xmlNanoFTPConnect(ctxt) < 0) {
2111 xmlNanoFTPFreeCtxt(ctxt);
2112 return(NULL);
2113 }
2114 sock = xmlNanoFTPGetSocket(ctxt, ctxt->path);
2115 if (sock < 0) {
2116 xmlNanoFTPFreeCtxt(ctxt);
2117 return(NULL);
2118 }
2119 return(ctxt);
2120}
2121
2122/**
2123 * xmlNanoFTPClose:
2124 * @ctx: an FTP context
2125 *
2126 * Close the connection and both control and transport
2127 *
2128 * Returns -1 incase of error, 0 otherwise
2129 */
2130
2131int
2132xmlNanoFTPClose(void *ctx) {
2133 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
2134
2135 if (ctxt == NULL)
2136 return(-1);
2137
2138 if (ctxt->dataFd >= 0) {
2139 closesocket(ctxt->dataFd);
2140 ctxt->dataFd = -1;
2141 }
2142 if (ctxt->controlFd >= 0) {
2143 xmlNanoFTPQuit(ctxt);
2144 closesocket(ctxt->controlFd);
2145 ctxt->controlFd = -1;
2146 }
2147 xmlNanoFTPFreeCtxt(ctxt);
2148 return(0);
2149}
2150
2151#ifdef STANDALONE
2152/************************************************************************
2153 * *
2154 * Basic test in Standalone mode *
2155 * *
2156 ************************************************************************/
Daniel Veillard01c13b52002-12-10 15:19:08 +00002157static
Owen Taylor3473f882001-02-23 17:55:21 +00002158void ftpList(void *userData, const char *filename, const char* attrib,
2159 const char *owner, const char *group, unsigned long size, int links,
2160 int year, const char *month, int day, int hour, int minute) {
2161 xmlGenericError(xmlGenericErrorContext,
2162 "%s %s %s %ld %s\n", attrib, owner, group, size, filename);
2163}
Daniel Veillard01c13b52002-12-10 15:19:08 +00002164static
Owen Taylor3473f882001-02-23 17:55:21 +00002165void ftpData(void *userData, const char *data, int len) {
2166 if (userData == NULL) return;
2167 if (len <= 0) {
2168 fclose(userData);
2169 return;
2170 }
2171 fwrite(data, len, 1, userData);
2172}
2173
2174int main(int argc, char **argv) {
2175 void *ctxt;
2176 FILE *output;
2177 char *tstfile = NULL;
2178
2179 xmlNanoFTPInit();
2180 if (argc > 1) {
2181 ctxt = xmlNanoFTPNewCtxt(argv[1]);
2182 if (xmlNanoFTPConnect(ctxt) < 0) {
2183 xmlGenericError(xmlGenericErrorContext,
2184 "Couldn't connect to %s\n", argv[1]);
2185 exit(1);
2186 }
2187 if (argc > 2)
2188 tstfile = argv[2];
2189 } else
2190 ctxt = xmlNanoFTPConnectTo("localhost", 0);
2191 if (ctxt == NULL) {
2192 xmlGenericError(xmlGenericErrorContext,
2193 "Couldn't connect to localhost\n");
2194 exit(1);
2195 }
2196 xmlNanoFTPList(ctxt, ftpList, NULL, tstfile);
2197 output = fopen("/tmp/tstdata", "w");
2198 if (output != NULL) {
2199 if (xmlNanoFTPGet(ctxt, ftpData, (void *) output, tstfile) < 0)
2200 xmlGenericError(xmlGenericErrorContext,
2201 "Failed to get file\n");
2202
2203 }
2204 xmlNanoFTPClose(ctxt);
2205 xmlMemoryDump();
2206 exit(0);
2207}
2208#endif /* STANDALONE */
2209#else /* !LIBXML_FTP_ENABLED */
2210#ifdef STANDALONE
2211#include <stdio.h>
2212int main(int argc, char **argv) {
2213 xmlGenericError(xmlGenericErrorContext,
2214 "%s : FTP support not compiled in\n", argv[0]);
2215 return(0);
2216}
2217#endif /* STANDALONE */
2218#endif /* LIBXML_FTP_ENABLED */