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