blob: a54b85b3a545ac3f1e89245124219e0bee06d282 [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
Daniel Veillard1638a472003-08-14 01:23:25 +000079
Daniel Veillard59d3ed82007-04-17 12:44:58 +000080#if defined(__MINGW32__) || defined(_WIN32_WCE)
Daniel Veillard1638a472003-08-14 01:23:25 +000081#define _WINSOCKAPI_
82#include <wsockcompat.h>
83#include <winsock2.h>
Daniel Veillardc284c642005-03-31 10:24:24 +000084#undef XML_SOCKLEN_T
85#define XML_SOCKLEN_T unsigned int
Daniel Veillard1638a472003-08-14 01:23:25 +000086#endif
87
Owen Taylor3473f882001-02-23 17:55:21 +000088/**
89 * A couple portability macros
90 */
91#ifndef _WINSOCKAPI_
Daniel Veillardcba68392008-08-29 12:43:40 +000092#if !defined(__BEOS__) || defined(__HAIKU__)
Owen Taylor3473f882001-02-23 17:55:21 +000093#define closesocket(s) close(s)
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000094#endif
Owen Taylor3473f882001-02-23 17:55:21 +000095#define SOCKET int
96#endif
97
Daniel Veillard89f7f272003-09-29 13:29:09 +000098#ifdef __BEOS__
99#ifndef PF_INET
100#define PF_INET AF_INET
101#endif
102#endif
103
Daniel Veillarddb439252005-01-15 17:18:53 +0000104#ifdef _AIX
Daniel Veillardbd444842007-03-20 08:47:29 +0000105#ifdef HAVE_BROKEN_SS_FAMILY
Daniel Veillarddb439252005-01-15 17:18:53 +0000106#define ss_family __ss_family
107#endif
Daniel Veillardbd444842007-03-20 08:47:29 +0000108#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000109
Daniel Veillard6056ae92005-04-05 21:48:57 +0000110#ifndef XML_SOCKLEN_T
111#define XML_SOCKLEN_T unsigned int
112#endif
113
Owen Taylor3473f882001-02-23 17:55:21 +0000114#define FTP_COMMAND_OK 200
115#define FTP_SYNTAX_ERROR 500
116#define FTP_GET_PASSWD 331
Daniel Veillard99dd7632005-05-06 11:40:56 +0000117#define FTP_BUF_SIZE 1024
Owen Taylor3473f882001-02-23 17:55:21 +0000118
William M. Brack030a7a12004-02-10 12:48:57 +0000119#define XML_NANO_MAX_URLBUF 4096
120
Owen Taylor3473f882001-02-23 17:55:21 +0000121typedef struct xmlNanoFTPCtxt {
122 char *protocol; /* the protocol name */
123 char *hostname; /* the host name */
124 int port; /* the port */
125 char *path; /* the path within the URL */
126 char *user; /* user string */
127 char *passwd; /* passwd string */
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000128#ifdef SUPPORT_IP6
129 struct sockaddr_storage ftpAddr; /* this is large enough to hold IPv6 address*/
130#else
Owen Taylor3473f882001-02-23 17:55:21 +0000131 struct sockaddr_in ftpAddr; /* the socket address struct */
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000132#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000133 int passive; /* currently we support only passive !!! */
134 SOCKET controlFd; /* the file descriptor for the control socket */
135 SOCKET dataFd; /* the file descriptor for the data socket */
136 int state; /* WRITE / READ / CLOSED */
137 int returnValue; /* the protocol return value */
138 /* buffer for data received from the control connection */
139 char controlBuf[FTP_BUF_SIZE + 1];
140 int controlBufIndex;
141 int controlBufUsed;
142 int controlBufAnswer;
143} xmlNanoFTPCtxt, *xmlNanoFTPCtxtPtr;
144
145static int initialized = 0;
146static char *proxy = NULL; /* the proxy name if any */
147static int proxyPort = 0; /* the proxy port if any */
148static char *proxyUser = NULL; /* user for proxy authentication */
149static char *proxyPasswd = NULL;/* passwd for proxy authentication */
150static int proxyType = 0; /* uses TYPE or a@b ? */
151
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000152#ifdef SUPPORT_IP6
Daniel Veillard2db8c122003-07-08 12:16:59 +0000153static
154int have_ipv6(void) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000155 int s;
156
157 s = socket (AF_INET6, SOCK_STREAM, 0);
158 if (s != -1) {
159 close (s);
160 return (1);
161 }
162 return (0);
163}
164#endif
165
Owen Taylor3473f882001-02-23 17:55:21 +0000166/**
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000167 * xmlFTPErrMemory:
168 * @extra: extra informations
169 *
170 * Handle an out of memory condition
171 */
172static void
173xmlFTPErrMemory(const char *extra)
174{
175 __xmlSimpleError(XML_FROM_FTP, XML_ERR_NO_MEMORY, NULL, NULL, extra);
176}
177
178/**
Owen Taylor3473f882001-02-23 17:55:21 +0000179 * xmlNanoFTPInit:
180 *
181 * Initialize the FTP protocol layer.
182 * Currently it just checks for proxy informations,
183 * and get the hostname
184 */
185
186void
187xmlNanoFTPInit(void) {
188 const char *env;
189#ifdef _WINSOCKAPI_
190 WSADATA wsaData;
191#endif
192
193 if (initialized)
194 return;
195
196#ifdef _WINSOCKAPI_
197 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
198 return;
199#endif
200
Owen Taylor3473f882001-02-23 17:55:21 +0000201 proxyPort = 21;
202 env = getenv("no_proxy");
Daniel Veillard29b17482004-08-16 00:39:03 +0000203 if (env && ((env[0] == '*' ) && (env[1] == 0)))
Owen Taylor3473f882001-02-23 17:55:21 +0000204 return;
205 env = getenv("ftp_proxy");
206 if (env != NULL) {
207 xmlNanoFTPScanProxy(env);
208 } else {
209 env = getenv("FTP_PROXY");
210 if (env != NULL) {
211 xmlNanoFTPScanProxy(env);
212 }
213 }
214 env = getenv("ftp_proxy_user");
215 if (env != NULL) {
216 proxyUser = xmlMemStrdup(env);
217 }
218 env = getenv("ftp_proxy_password");
219 if (env != NULL) {
220 proxyPasswd = xmlMemStrdup(env);
221 }
222 initialized = 1;
223}
224
225/**
Daniel Veillarde356c282001-03-10 12:32:04 +0000226 * xmlNanoFTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000227 *
228 * Cleanup the FTP protocol layer. This cleanup proxy informations.
229 */
230
231void
232xmlNanoFTPCleanup(void) {
233 if (proxy != NULL) {
234 xmlFree(proxy);
235 proxy = NULL;
236 }
237 if (proxyUser != NULL) {
238 xmlFree(proxyUser);
239 proxyUser = NULL;
240 }
241 if (proxyPasswd != NULL) {
242 xmlFree(proxyPasswd);
243 proxyPasswd = NULL;
244 }
Owen Taylor3473f882001-02-23 17:55:21 +0000245#ifdef _WINSOCKAPI_
246 if (initialized)
247 WSACleanup();
248#endif
249 initialized = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000250}
251
252/**
253 * xmlNanoFTPProxy:
254 * @host: the proxy host name
255 * @port: the proxy port
256 * @user: the proxy user name
257 * @passwd: the proxy password
258 * @type: the type of proxy 1 for using SITE, 2 for USER a@b
259 *
260 * Setup the FTP proxy informations.
261 * This can also be done by using ftp_proxy ftp_proxy_user and
262 * ftp_proxy_password environment variables.
263 */
264
265void
266xmlNanoFTPProxy(const char *host, int port, const char *user,
267 const char *passwd, int type) {
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000268 if (proxy != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000269 xmlFree(proxy);
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000270 proxy = NULL;
271 }
272 if (proxyUser != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000273 xmlFree(proxyUser);
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000274 proxyUser = NULL;
275 }
276 if (proxyPasswd != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000277 xmlFree(proxyPasswd);
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000278 proxyPasswd = NULL;
279 }
Owen Taylor3473f882001-02-23 17:55:21 +0000280 if (host)
281 proxy = xmlMemStrdup(host);
282 if (user)
283 proxyUser = xmlMemStrdup(user);
284 if (passwd)
285 proxyPasswd = xmlMemStrdup(passwd);
286 proxyPort = port;
287 proxyType = type;
288}
289
290/**
291 * xmlNanoFTPScanURL:
292 * @ctx: an FTP context
293 * @URL: The URL used to initialize the context
294 *
295 * (Re)Initialize an FTP context by parsing the URL and finding
296 * the protocol host port and path it indicates.
297 */
298
299static void
300xmlNanoFTPScanURL(void *ctx, const char *URL) {
301 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
William M. Brack015ccb22005-02-13 08:18:52 +0000302 xmlURIPtr uri;
Owen Taylor3473f882001-02-23 17:55:21 +0000303
William M. Brack015ccb22005-02-13 08:18:52 +0000304 /*
305 * Clear any existing data from the context
306 */
Owen Taylor3473f882001-02-23 17:55:21 +0000307 if (ctxt->protocol != NULL) {
308 xmlFree(ctxt->protocol);
309 ctxt->protocol = NULL;
310 }
311 if (ctxt->hostname != NULL) {
312 xmlFree(ctxt->hostname);
313 ctxt->hostname = NULL;
314 }
315 if (ctxt->path != NULL) {
316 xmlFree(ctxt->path);
317 ctxt->path = NULL;
318 }
319 if (URL == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +0000320
Daniel Veillard336a8e12005-08-07 10:46:19 +0000321 uri = xmlParseURIRaw(URL, 1);
William M. Brack015ccb22005-02-13 08:18:52 +0000322 if (uri == NULL)
323 return;
324
325 if ((uri->scheme == NULL) || (uri->server == NULL)) {
326 xmlFreeURI(uri);
327 return;
328 }
329
330 ctxt->protocol = xmlMemStrdup(uri->scheme);
331 ctxt->hostname = xmlMemStrdup(uri->server);
332 if (uri->path != NULL)
333 ctxt->path = xmlMemStrdup(uri->path);
334 else
335 ctxt->path = xmlMemStrdup("/");
336 if (uri->port != 0)
337 ctxt->port = uri->port;
338
339 if (uri->user != NULL) {
340 char *cptr;
341 if ((cptr=strchr(uri->user, ':')) == NULL)
342 ctxt->user = xmlMemStrdup(uri->user);
343 else {
344 ctxt->user = (char *)xmlStrndup((xmlChar *)uri->user,
345 (cptr - uri->user));
346 ctxt->passwd = xmlMemStrdup(cptr+1);
Daniel Veillardc69e0b12001-11-20 08:35:07 +0000347 }
348 }
349
William M. Brack015ccb22005-02-13 08:18:52 +0000350 xmlFreeURI(uri);
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000351
Owen Taylor3473f882001-02-23 17:55:21 +0000352}
353
354/**
355 * xmlNanoFTPUpdateURL:
356 * @ctx: an FTP context
357 * @URL: The URL used to update the context
358 *
359 * Update an FTP context by parsing the URL and finding
360 * new path it indicates. If there is an error in the
361 * protocol, hostname, port or other information, the
362 * error is raised. It indicates a new connection has to
363 * be established.
364 *
365 * Returns 0 if Ok, -1 in case of error (other host).
366 */
367
368int
369xmlNanoFTPUpdateURL(void *ctx, const char *URL) {
370 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
William M. Brack015ccb22005-02-13 08:18:52 +0000371 xmlURIPtr uri;
Owen Taylor3473f882001-02-23 17:55:21 +0000372
373 if (URL == NULL)
374 return(-1);
375 if (ctxt == NULL)
376 return(-1);
377 if (ctxt->protocol == NULL)
378 return(-1);
379 if (ctxt->hostname == NULL)
380 return(-1);
William M. Brack015ccb22005-02-13 08:18:52 +0000381
Daniel Veillard336a8e12005-08-07 10:46:19 +0000382 uri = xmlParseURIRaw(URL, 1);
William M. Brack015ccb22005-02-13 08:18:52 +0000383 if (uri == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +0000384 return(-1);
385
William M. Brack015ccb22005-02-13 08:18:52 +0000386 if ((uri->scheme == NULL) || (uri->server == NULL)) {
387 xmlFreeURI(uri);
388 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000389 }
William M. Brack015ccb22005-02-13 08:18:52 +0000390 if ((strcmp(ctxt->protocol, uri->scheme)) ||
391 (strcmp(ctxt->hostname, uri->server)) ||
392 ((uri->port != 0) && (ctxt->port != uri->port))) {
393 xmlFreeURI(uri);
394 return(-1);
395 }
396
397 if (uri->port != 0)
398 ctxt->port = uri->port;
399
Owen Taylor3473f882001-02-23 17:55:21 +0000400 if (ctxt->path != NULL) {
401 xmlFree(ctxt->path);
402 ctxt->path = NULL;
403 }
404
William M. Brack015ccb22005-02-13 08:18:52 +0000405 if (uri->path == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +0000406 ctxt->path = xmlMemStrdup("/");
William M. Brack015ccb22005-02-13 08:18:52 +0000407 else
408 ctxt->path = xmlMemStrdup(uri->path);
409
410 xmlFreeURI(uri);
411
Owen Taylor3473f882001-02-23 17:55:21 +0000412 return(0);
413}
414
415/**
416 * xmlNanoFTPScanProxy:
417 * @URL: The proxy URL used to initialize the proxy context
418 *
419 * (Re)Initialize the FTP Proxy context by parsing the URL and finding
420 * the protocol host port it indicates.
421 * Should be like ftp://myproxy/ or ftp://myproxy:3128/
422 * A NULL URL cleans up proxy informations.
423 */
424
425void
426xmlNanoFTPScanProxy(const char *URL) {
William M. Brack015ccb22005-02-13 08:18:52 +0000427 xmlURIPtr uri;
Owen Taylor3473f882001-02-23 17:55:21 +0000428
429 if (proxy != NULL) {
430 xmlFree(proxy);
431 proxy = NULL;
432 }
William M. Brack015ccb22005-02-13 08:18:52 +0000433 proxyPort = 0;
434
Owen Taylor3473f882001-02-23 17:55:21 +0000435#ifdef DEBUG_FTP
436 if (URL == NULL)
William M. Brack015ccb22005-02-13 08:18:52 +0000437 xmlGenericError(xmlGenericErrorContext,
438 "Removing FTP proxy info\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000439 else
William M. Brack015ccb22005-02-13 08:18:52 +0000440 xmlGenericError(xmlGenericErrorContext,
441 "Using FTP proxy %s\n", URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000442#endif
443 if (URL == NULL) return;
William M. Brack015ccb22005-02-13 08:18:52 +0000444
Daniel Veillard336a8e12005-08-07 10:46:19 +0000445 uri = xmlParseURIRaw(URL, 1);
William M. Brack015ccb22005-02-13 08:18:52 +0000446 if ((uri == NULL) || (uri->scheme == NULL) ||
447 (strcmp(uri->scheme, "ftp")) || (uri->server == NULL)) {
448 __xmlIOErr(XML_FROM_FTP, XML_FTP_URL_SYNTAX, "Syntax Error\n");
449 if (uri != NULL)
450 xmlFreeURI(uri);
451 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000452 }
William M. Brack015ccb22005-02-13 08:18:52 +0000453
454 proxy = xmlMemStrdup(uri->server);
455 if (uri->port != 0)
456 proxyPort = uri->port;
Owen Taylor3473f882001-02-23 17:55:21 +0000457
William M. Brack015ccb22005-02-13 08:18:52 +0000458 xmlFreeURI(uri);
Owen Taylor3473f882001-02-23 17:55:21 +0000459}
460
461/**
462 * xmlNanoFTPNewCtxt:
463 * @URL: The URL used to initialize the context
464 *
465 * Allocate and initialize a new FTP context.
466 *
467 * Returns an FTP context or NULL in case of error.
468 */
469
470void*
471xmlNanoFTPNewCtxt(const char *URL) {
472 xmlNanoFTPCtxtPtr ret;
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000473 char *unescaped;
Owen Taylor3473f882001-02-23 17:55:21 +0000474
475 ret = (xmlNanoFTPCtxtPtr) xmlMalloc(sizeof(xmlNanoFTPCtxt));
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000476 if (ret == NULL) {
477 xmlFTPErrMemory("allocating FTP context");
478 return(NULL);
479 }
Owen Taylor3473f882001-02-23 17:55:21 +0000480
481 memset(ret, 0, sizeof(xmlNanoFTPCtxt));
482 ret->port = 21;
483 ret->passive = 1;
484 ret->returnValue = 0;
485 ret->controlBufIndex = 0;
486 ret->controlBufUsed = 0;
Daniel Veillardc69e0b12001-11-20 08:35:07 +0000487 ret->controlFd = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000488
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000489 unescaped = xmlURIUnescapeString(URL, 0, NULL);
Daniel Veillardb1b3a3e2004-11-03 23:25:47 +0000490 if (unescaped != NULL) {
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000491 xmlNanoFTPScanURL(ret, unescaped);
Daniel Veillardb1b3a3e2004-11-03 23:25:47 +0000492 xmlFree(unescaped);
493 } else if (URL != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +0000494 xmlNanoFTPScanURL(ret, URL);
495
496 return(ret);
497}
498
499/**
500 * xmlNanoFTPFreeCtxt:
501 * @ctx: an FTP context
502 *
503 * Frees the context after closing the connection.
504 */
505
506void
507xmlNanoFTPFreeCtxt(void * ctx) {
508 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
509 if (ctxt == NULL) return;
510 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
511 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
512 if (ctxt->path != NULL) xmlFree(ctxt->path);
513 ctxt->passive = 1;
514 if (ctxt->controlFd >= 0) closesocket(ctxt->controlFd);
515 ctxt->controlFd = -1;
516 ctxt->controlBufIndex = -1;
517 ctxt->controlBufUsed = -1;
518 xmlFree(ctxt);
519}
520
521/**
522 * xmlNanoFTPParseResponse:
Owen Taylor3473f882001-02-23 17:55:21 +0000523 * @buf: the buffer containing the response
524 * @len: the buffer length
525 *
526 * Parsing of the server answer, we just extract the code.
527 *
528 * returns 0 for errors
529 * +XXX for last line of response
530 * -XXX for response to be continued
531 */
532static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000533xmlNanoFTPParseResponse(char *buf, int len) {
Owen Taylor3473f882001-02-23 17:55:21 +0000534 int val = 0;
535
536 if (len < 3) return(-1);
537 if ((*buf >= '0') && (*buf <= '9'))
538 val = val * 10 + (*buf - '0');
539 else
540 return(0);
541 buf++;
542 if ((*buf >= '0') && (*buf <= '9'))
543 val = val * 10 + (*buf - '0');
544 else
545 return(0);
546 buf++;
547 if ((*buf >= '0') && (*buf <= '9'))
548 val = val * 10 + (*buf - '0');
549 else
550 return(0);
551 buf++;
552 if (*buf == '-')
553 return(-val);
554 return(val);
555}
556
557/**
558 * xmlNanoFTPGetMore:
559 * @ctx: an FTP context
560 *
561 * Read more information from the FTP control connection
562 * Returns the number of bytes read, < 0 indicates an error
563 */
564static int
565xmlNanoFTPGetMore(void *ctx) {
566 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
567 int len;
568 int size;
569
Daniel Veillard27f20102004-11-05 11:50:11 +0000570 if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
571
Owen Taylor3473f882001-02-23 17:55:21 +0000572 if ((ctxt->controlBufIndex < 0) || (ctxt->controlBufIndex > FTP_BUF_SIZE)) {
573#ifdef DEBUG_FTP
574 xmlGenericError(xmlGenericErrorContext,
575 "xmlNanoFTPGetMore : controlBufIndex = %d\n",
576 ctxt->controlBufIndex);
577#endif
578 return(-1);
579 }
580
581 if ((ctxt->controlBufUsed < 0) || (ctxt->controlBufUsed > FTP_BUF_SIZE)) {
582#ifdef DEBUG_FTP
583 xmlGenericError(xmlGenericErrorContext,
584 "xmlNanoFTPGetMore : controlBufUsed = %d\n",
585 ctxt->controlBufUsed);
586#endif
587 return(-1);
588 }
589 if (ctxt->controlBufIndex > ctxt->controlBufUsed) {
590#ifdef DEBUG_FTP
591 xmlGenericError(xmlGenericErrorContext,
592 "xmlNanoFTPGetMore : controlBufIndex > controlBufUsed %d > %d\n",
593 ctxt->controlBufIndex, ctxt->controlBufUsed);
594#endif
595 return(-1);
596 }
597
598 /*
599 * First pack the control buffer
600 */
601 if (ctxt->controlBufIndex > 0) {
602 memmove(&ctxt->controlBuf[0], &ctxt->controlBuf[ctxt->controlBufIndex],
603 ctxt->controlBufUsed - ctxt->controlBufIndex);
604 ctxt->controlBufUsed -= ctxt->controlBufIndex;
605 ctxt->controlBufIndex = 0;
606 }
607 size = FTP_BUF_SIZE - ctxt->controlBufUsed;
608 if (size == 0) {
609#ifdef DEBUG_FTP
610 xmlGenericError(xmlGenericErrorContext,
611 "xmlNanoFTPGetMore : buffer full %d \n", ctxt->controlBufUsed);
612#endif
613 return(0);
614 }
615
616 /*
Daniel Veillard60087f32001-10-10 09:45:09 +0000617 * Read the amount left on the control connection
Owen Taylor3473f882001-02-23 17:55:21 +0000618 */
619 if ((len = recv(ctxt->controlFd, &ctxt->controlBuf[ctxt->controlBufIndex],
620 size, 0)) < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000621 __xmlIOErr(XML_FROM_FTP, 0, "recv failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000622 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
623 ctxt->controlFd = -1;
624 return(-1);
625 }
626#ifdef DEBUG_FTP
627 xmlGenericError(xmlGenericErrorContext,
628 "xmlNanoFTPGetMore : read %d [%d - %d]\n", len,
629 ctxt->controlBufUsed, ctxt->controlBufUsed + len);
630#endif
631 ctxt->controlBufUsed += len;
632 ctxt->controlBuf[ctxt->controlBufUsed] = 0;
633
634 return(len);
635}
636
637/**
638 * xmlNanoFTPReadResponse:
639 * @ctx: an FTP context
640 *
641 * Read the response from the FTP server after a command.
642 * Returns the code number
643 */
644static int
645xmlNanoFTPReadResponse(void *ctx) {
646 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
647 char *ptr, *end;
648 int len;
649 int res = -1, cur = -1;
650
Daniel Veillard27f20102004-11-05 11:50:11 +0000651 if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
652
Owen Taylor3473f882001-02-23 17:55:21 +0000653get_more:
654 /*
655 * Assumes everything up to controlBuf[controlBufIndex] has been read
656 * and analyzed.
657 */
658 len = xmlNanoFTPGetMore(ctx);
659 if (len < 0) {
660 return(-1);
661 }
662 if ((ctxt->controlBufUsed == 0) && (len == 0)) {
663 return(-1);
664 }
665 ptr = &ctxt->controlBuf[ctxt->controlBufIndex];
666 end = &ctxt->controlBuf[ctxt->controlBufUsed];
667
668#ifdef DEBUG_FTP
669 xmlGenericError(xmlGenericErrorContext,
670 "\n<<<\n%s\n--\n", ptr);
671#endif
672 while (ptr < end) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000673 cur = xmlNanoFTPParseResponse(ptr, end - ptr);
Owen Taylor3473f882001-02-23 17:55:21 +0000674 if (cur > 0) {
675 /*
676 * Successfully scanned the control code, scratch
677 * till the end of the line, but keep the index to be
678 * able to analyze the result if needed.
679 */
680 res = cur;
681 ptr += 3;
682 ctxt->controlBufAnswer = ptr - ctxt->controlBuf;
683 while ((ptr < end) && (*ptr != '\n')) ptr++;
684 if (*ptr == '\n') ptr++;
685 if (*ptr == '\r') ptr++;
686 break;
687 }
688 while ((ptr < end) && (*ptr != '\n')) ptr++;
689 if (ptr >= end) {
690 ctxt->controlBufIndex = ctxt->controlBufUsed;
691 goto get_more;
692 }
693 if (*ptr != '\r') ptr++;
694 }
695
696 if (res < 0) goto get_more;
697 ctxt->controlBufIndex = ptr - ctxt->controlBuf;
698#ifdef DEBUG_FTP
699 ptr = &ctxt->controlBuf[ctxt->controlBufIndex];
700 xmlGenericError(xmlGenericErrorContext, "\n---\n%s\n--\n", ptr);
701#endif
702
703#ifdef DEBUG_FTP
704 xmlGenericError(xmlGenericErrorContext, "Got %d\n", res);
705#endif
706 return(res / 100);
707}
708
709/**
710 * xmlNanoFTPGetResponse:
711 * @ctx: an FTP context
712 *
713 * Get the response from the FTP server after a command.
714 * Returns the code number
715 */
716
717int
718xmlNanoFTPGetResponse(void *ctx) {
719 int res;
720
721 res = xmlNanoFTPReadResponse(ctx);
722
723 return(res);
724}
725
726/**
727 * xmlNanoFTPCheckResponse:
728 * @ctx: an FTP context
729 *
730 * Check if there is a response from the FTP server after a command.
731 * Returns the code number, or 0
732 */
733
734int
735xmlNanoFTPCheckResponse(void *ctx) {
736 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
737 fd_set rfd;
738 struct timeval tv;
739
Daniel Veillard27f20102004-11-05 11:50:11 +0000740 if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000741 tv.tv_sec = 0;
742 tv.tv_usec = 0;
743 FD_ZERO(&rfd);
744 FD_SET(ctxt->controlFd, &rfd);
745 switch(select(ctxt->controlFd + 1, &rfd, NULL, NULL, &tv)) {
746 case 0:
747 return(0);
748 case -1:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000749 __xmlIOErr(XML_FROM_FTP, 0, "select");
Owen Taylor3473f882001-02-23 17:55:21 +0000750 return(-1);
751
752 }
753
754 return(xmlNanoFTPReadResponse(ctx));
755}
756
757/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000758 * Send the user authentication
Owen Taylor3473f882001-02-23 17:55:21 +0000759 */
760
761static int
762xmlNanoFTPSendUser(void *ctx) {
763 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
764 char buf[200];
765 int len;
766 int res;
767
768 if (ctxt->user == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000769 snprintf(buf, sizeof(buf), "USER anonymous\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000770 else
Owen Taylor3473f882001-02-23 17:55:21 +0000771 snprintf(buf, sizeof(buf), "USER %s\r\n", ctxt->user);
Owen Taylor3473f882001-02-23 17:55:21 +0000772 buf[sizeof(buf) - 1] = 0;
773 len = strlen(buf);
774#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +0000775 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000776#endif
777 res = send(ctxt->controlFd, buf, len, 0);
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000778 if (res < 0) {
779 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
780 return(res);
781 }
Owen Taylor3473f882001-02-23 17:55:21 +0000782 return(0);
783}
784
785/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000786 * Send the password authentication
Owen Taylor3473f882001-02-23 17:55:21 +0000787 */
788
789static int
790xmlNanoFTPSendPasswd(void *ctx) {
791 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
792 char buf[200];
793 int len;
794 int res;
795
796 if (ctxt->passwd == NULL)
Daniel Veillard9ae1eba2001-10-19 09:48:35 +0000797 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000798 else
Owen Taylor3473f882001-02-23 17:55:21 +0000799 snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
Owen Taylor3473f882001-02-23 17:55:21 +0000800 buf[sizeof(buf) - 1] = 0;
801 len = strlen(buf);
802#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +0000803 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000804#endif
805 res = send(ctxt->controlFd, buf, len, 0);
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000806 if (res < 0) {
807 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
808 return(res);
809 }
Owen Taylor3473f882001-02-23 17:55:21 +0000810 return(0);
811}
812
813/**
814 * xmlNanoFTPQuit:
815 * @ctx: an FTP context
816 *
817 * Send a QUIT command to the server
818 *
819 * Returns -1 in case of error, 0 otherwise
820 */
821
822
823int
824xmlNanoFTPQuit(void *ctx) {
825 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
826 char buf[200];
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000827 int len, res;
Owen Taylor3473f882001-02-23 17:55:21 +0000828
Daniel Veillard27f20102004-11-05 11:50:11 +0000829 if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
830
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000831 snprintf(buf, sizeof(buf), "QUIT\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000832 len = strlen(buf);
833#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +0000834 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 +0000835#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000836 res = send(ctxt->controlFd, buf, len, 0);
837 if (res < 0) {
838 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
839 return(res);
840 }
Owen Taylor3473f882001-02-23 17:55:21 +0000841 return(0);
842}
843
844/**
845 * xmlNanoFTPConnect:
846 * @ctx: an FTP context
847 *
848 * Tries to open a control connection
849 *
850 * Returns -1 in case of error, 0 otherwise
851 */
852
853int
854xmlNanoFTPConnect(void *ctx) {
855 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
856 struct hostent *hp;
857 int port;
858 int res;
Daniel Veillard2db8c122003-07-08 12:16:59 +0000859 int addrlen = sizeof (struct sockaddr_in);
Owen Taylor3473f882001-02-23 17:55:21 +0000860
861 if (ctxt == NULL)
862 return(-1);
863 if (ctxt->hostname == NULL)
864 return(-1);
865
866 /*
867 * do the blocking DNS query.
868 */
Owen Taylor3473f882001-02-23 17:55:21 +0000869 if (proxy) {
870 port = proxyPort;
871 } else {
872 port = ctxt->port;
873 }
874 if (port == 0)
875 port = 21;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000876
877 memset (&ctxt->ftpAddr, 0, sizeof(ctxt->ftpAddr));
878
879#ifdef SUPPORT_IP6
880 if (have_ipv6 ()) {
Daniel Veillard2db8c122003-07-08 12:16:59 +0000881 struct addrinfo hints, *tmp, *result;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000882
883 result = NULL;
884 memset (&hints, 0, sizeof(hints));
885 hints.ai_socktype = SOCK_STREAM;
886
887 if (proxy) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000888 if (getaddrinfo (proxy, NULL, &hints, &result) != 0) {
889 __xmlIOErr(XML_FROM_FTP, 0, "getaddrinfo failed");
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000890 return (-1);
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000891 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000892 }
893 else
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000894 if (getaddrinfo (ctxt->hostname, NULL, &hints, &result) != 0) {
895 __xmlIOErr(XML_FROM_FTP, 0, "getaddrinfo failed");
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000896 return (-1);
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000897 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000898
Daniel Veillard2db8c122003-07-08 12:16:59 +0000899 for (tmp = result; tmp; tmp = tmp->ai_next)
900 if (tmp->ai_family == AF_INET || tmp->ai_family == AF_INET6)
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000901 break;
902
Daniel Veillard3dc93a42003-07-10 14:04:33 +0000903 if (!tmp) {
904 if (result)
905 freeaddrinfo (result);
Daniel Veillard8e2c9792004-10-27 09:39:50 +0000906 __xmlIOErr(XML_FROM_FTP, 0, "getaddrinfo failed");
Daniel Veillard3dc93a42003-07-10 14:04:33 +0000907 return (-1);
908 }
Daniel Veillard8e2c9792004-10-27 09:39:50 +0000909 if (tmp->ai_addrlen > sizeof(ctxt->ftpAddr)) {
910 __xmlIOErr(XML_FROM_FTP, 0, "gethostbyname address mismatch");
911 return (-1);
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000912 }
Daniel Veillard8e2c9792004-10-27 09:39:50 +0000913 if (tmp->ai_family == AF_INET6) {
914 memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen);
915 ((struct sockaddr_in6 *) &ctxt->ftpAddr)->sin6_port = htons (port);
916 ctxt->controlFd = socket (AF_INET6, SOCK_STREAM, 0);
917 }
918 else {
919 memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen);
920 ((struct sockaddr_in *) &ctxt->ftpAddr)->sin_port = htons (port);
921 ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0);
922 }
923 addrlen = tmp->ai_addrlen;
924 freeaddrinfo (result);
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000925 }
926 else
927#endif
928 {
929 if (proxy)
930 hp = gethostbyname (proxy);
931 else
932 hp = gethostbyname (ctxt->hostname);
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000933 if (hp == NULL) {
934 __xmlIOErr(XML_FROM_FTP, 0, "gethostbyname failed");
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000935 return (-1);
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000936 }
Daniel Veillard6927b102004-10-27 17:29:04 +0000937 if ((unsigned int) hp->h_length >
Daniel Veillard8e2c9792004-10-27 09:39:50 +0000938 sizeof(((struct sockaddr_in *)&ctxt->ftpAddr)->sin_addr)) {
939 __xmlIOErr(XML_FROM_FTP, 0, "gethostbyname address mismatch");
940 return (-1);
941 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000942
Daniel Veillard8e2c9792004-10-27 09:39:50 +0000943 /*
944 * Prepare the socket
945 */
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000946 ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_family = AF_INET;
947 memcpy (&((struct sockaddr_in *)&ctxt->ftpAddr)->sin_addr,
948 hp->h_addr_list[0], hp->h_length);
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000949 ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_port = (u_short)htons ((unsigned short)port);
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000950 ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0);
951 addrlen = sizeof (struct sockaddr_in);
952 }
953
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000954 if (ctxt->controlFd < 0) {
955 __xmlIOErr(XML_FROM_FTP, 0, "socket failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000956 return(-1);
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000957 }
Owen Taylor3473f882001-02-23 17:55:21 +0000958
959 /*
960 * Do the connect.
961 */
962 if (connect(ctxt->controlFd, (struct sockaddr *) &ctxt->ftpAddr,
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000963 addrlen) < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000964 __xmlIOErr(XML_FROM_FTP, 0, "Failed to create a connection");
Owen Taylor3473f882001-02-23 17:55:21 +0000965 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
966 ctxt->controlFd = -1;
967 return(-1);
968 }
969
970 /*
971 * Wait for the HELLO from the server.
972 */
973 res = xmlNanoFTPGetResponse(ctxt);
974 if (res != 2) {
975 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
976 ctxt->controlFd = -1;
977 return(-1);
978 }
979
980 /*
981 * State diagram for the login operation on the FTP server
982 *
983 * Reference: RFC 959
984 *
985 * 1
986 * +---+ USER +---+------------->+---+
987 * | B |---------->| W | 2 ---->| E |
988 * +---+ +---+------ | -->+---+
989 * | | | | |
990 * 3 | | 4,5 | | |
991 * -------------- ----- | | |
992 * | | | | |
993 * | | | | |
994 * | --------- |
995 * | 1| | | |
996 * V | | | |
997 * +---+ PASS +---+ 2 | ------>+---+
998 * | |---------->| W |------------->| S |
999 * +---+ +---+ ---------->+---+
1000 * | | | | |
1001 * 3 | |4,5| | |
1002 * -------------- -------- |
1003 * | | | | |
1004 * | | | | |
1005 * | -----------
1006 * | 1,3| | | |
1007 * V | 2| | |
1008 * +---+ ACCT +---+-- | ----->+---+
1009 * | |---------->| W | 4,5 -------->| F |
1010 * +---+ +---+------------->+---+
1011 *
1012 * Of course in case of using a proxy this get really nasty and is not
1013 * standardized at all :-(
1014 */
1015 if (proxy) {
1016 int len;
1017 char buf[400];
1018
1019 if (proxyUser != NULL) {
1020 /*
1021 * We need proxy auth
1022 */
Owen Taylor3473f882001-02-23 17:55:21 +00001023 snprintf(buf, sizeof(buf), "USER %s\r\n", proxyUser);
Owen Taylor3473f882001-02-23 17:55:21 +00001024 buf[sizeof(buf) - 1] = 0;
1025 len = strlen(buf);
1026#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001027 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001028#endif
1029 res = send(ctxt->controlFd, buf, len, 0);
1030 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001031 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001032 closesocket(ctxt->controlFd);
1033 ctxt->controlFd = -1;
1034 return(res);
1035 }
1036 res = xmlNanoFTPGetResponse(ctxt);
1037 switch (res) {
1038 case 2:
1039 if (proxyPasswd == NULL)
1040 break;
1041 case 3:
1042 if (proxyPasswd != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001043 snprintf(buf, sizeof(buf), "PASS %s\r\n", proxyPasswd);
Owen Taylor3473f882001-02-23 17:55:21 +00001044 else
Daniel Veillard9ae1eba2001-10-19 09:48:35 +00001045 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001046 buf[sizeof(buf) - 1] = 0;
1047 len = strlen(buf);
1048#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001049 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001050#endif
1051 res = send(ctxt->controlFd, buf, len, 0);
1052 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001053 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001054 closesocket(ctxt->controlFd);
1055 ctxt->controlFd = -1;
1056 return(res);
1057 }
1058 res = xmlNanoFTPGetResponse(ctxt);
1059 if (res > 3) {
1060 closesocket(ctxt->controlFd);
1061 ctxt->controlFd = -1;
1062 return(-1);
1063 }
1064 break;
1065 case 1:
1066 break;
1067 case 4:
1068 case 5:
1069 case -1:
1070 default:
1071 closesocket(ctxt->controlFd);
1072 ctxt->controlFd = -1;
1073 return(-1);
1074 }
1075 }
1076
1077 /*
1078 * We assume we don't need more authentication to the proxy
1079 * and that it succeeded :-\
1080 */
1081 switch (proxyType) {
1082 case 0:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001083 /* we will try in sequence */
Owen Taylor3473f882001-02-23 17:55:21 +00001084 case 1:
1085 /* Using SITE command */
Owen Taylor3473f882001-02-23 17:55:21 +00001086 snprintf(buf, sizeof(buf), "SITE %s\r\n", ctxt->hostname);
Owen Taylor3473f882001-02-23 17:55:21 +00001087 buf[sizeof(buf) - 1] = 0;
1088 len = strlen(buf);
1089#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001090 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001091#endif
1092 res = send(ctxt->controlFd, buf, len, 0);
1093 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001094 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001095 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1096 ctxt->controlFd = -1;
1097 return(res);
1098 }
1099 res = xmlNanoFTPGetResponse(ctxt);
1100 if (res == 2) {
1101 /* we assume it worked :-\ 1 is error for SITE command */
1102 proxyType = 1;
1103 break;
1104 }
1105 if (proxyType == 1) {
1106 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1107 ctxt->controlFd = -1;
1108 return(-1);
1109 }
1110 case 2:
1111 /* USER user@host command */
1112 if (ctxt->user == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001113 snprintf(buf, sizeof(buf), "USER anonymous@%s\r\n",
1114 ctxt->hostname);
Owen Taylor3473f882001-02-23 17:55:21 +00001115 else
Owen Taylor3473f882001-02-23 17:55:21 +00001116 snprintf(buf, sizeof(buf), "USER %s@%s\r\n",
1117 ctxt->user, ctxt->hostname);
Owen Taylor3473f882001-02-23 17:55:21 +00001118 buf[sizeof(buf) - 1] = 0;
1119 len = strlen(buf);
1120#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001121 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001122#endif
1123 res = send(ctxt->controlFd, buf, len, 0);
1124 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001125 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001126 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1127 ctxt->controlFd = -1;
1128 return(res);
1129 }
1130 res = xmlNanoFTPGetResponse(ctxt);
1131 if ((res == 1) || (res == 2)) {
1132 /* we assume it worked :-\ */
1133 proxyType = 2;
1134 return(0);
1135 }
1136 if (ctxt->passwd == NULL)
Daniel Veillard9ae1eba2001-10-19 09:48:35 +00001137 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001138 else
Owen Taylor3473f882001-02-23 17:55:21 +00001139 snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
Owen Taylor3473f882001-02-23 17:55:21 +00001140 buf[sizeof(buf) - 1] = 0;
1141 len = strlen(buf);
1142#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001143 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001144#endif
1145 res = send(ctxt->controlFd, buf, len, 0);
1146 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001147 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001148 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1149 ctxt->controlFd = -1;
1150 return(res);
1151 }
1152 res = xmlNanoFTPGetResponse(ctxt);
1153 if ((res == 1) || (res == 2)) {
1154 /* we assume it worked :-\ */
1155 proxyType = 2;
1156 return(0);
1157 }
1158 if (proxyType == 2) {
1159 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1160 ctxt->controlFd = -1;
1161 return(-1);
1162 }
1163 case 3:
1164 /*
1165 * If you need support for other Proxy authentication scheme
1166 * send the code or at least the sequence in use.
1167 */
1168 default:
1169 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1170 ctxt->controlFd = -1;
1171 return(-1);
1172 }
1173 }
1174 /*
1175 * Non-proxy handling.
1176 */
1177 res = xmlNanoFTPSendUser(ctxt);
1178 if (res < 0) {
1179 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1180 ctxt->controlFd = -1;
1181 return(-1);
1182 }
1183 res = xmlNanoFTPGetResponse(ctxt);
1184 switch (res) {
1185 case 2:
1186 return(0);
1187 case 3:
1188 break;
1189 case 1:
1190 case 4:
1191 case 5:
1192 case -1:
1193 default:
1194 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1195 ctxt->controlFd = -1;
1196 return(-1);
1197 }
1198 res = xmlNanoFTPSendPasswd(ctxt);
1199 if (res < 0) {
1200 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1201 ctxt->controlFd = -1;
1202 return(-1);
1203 }
1204 res = xmlNanoFTPGetResponse(ctxt);
1205 switch (res) {
1206 case 2:
1207 break;
1208 case 3:
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001209 __xmlIOErr(XML_FROM_FTP, XML_FTP_ACCNT,
1210 "FTP server asking for ACCNT on anonymous\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001211 case 1:
1212 case 4:
1213 case 5:
1214 case -1:
1215 default:
1216 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1217 ctxt->controlFd = -1;
1218 return(-1);
1219 }
1220
1221 return(0);
1222}
1223
1224/**
1225 * xmlNanoFTPConnectTo:
1226 * @server: an FTP server name
1227 * @port: the port (use 21 if 0)
1228 *
1229 * Tries to open a control connection to the given server/port
1230 *
1231 * Returns an fTP context or NULL if it failed
1232 */
1233
1234void*
1235xmlNanoFTPConnectTo(const char *server, int port) {
1236 xmlNanoFTPCtxtPtr ctxt;
1237 int res;
1238
1239 xmlNanoFTPInit();
1240 if (server == NULL)
1241 return(NULL);
Daniel Veillard27f20102004-11-05 11:50:11 +00001242 if (port <= 0)
1243 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001244 ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(NULL);
1245 ctxt->hostname = xmlMemStrdup(server);
1246 if (port != 0)
1247 ctxt->port = port;
1248 res = xmlNanoFTPConnect(ctxt);
1249 if (res < 0) {
1250 xmlNanoFTPFreeCtxt(ctxt);
1251 return(NULL);
1252 }
1253 return(ctxt);
1254}
1255
1256/**
1257 * xmlNanoFTPCwd:
1258 * @ctx: an FTP context
1259 * @directory: a directory on the server
1260 *
1261 * Tries to change the remote directory
1262 *
1263 * Returns -1 incase of error, 1 if CWD worked, 0 if it failed
1264 */
1265
1266int
Daniel Veillard34099b42004-11-04 17:34:35 +00001267xmlNanoFTPCwd(void *ctx, const char *directory) {
Owen Taylor3473f882001-02-23 17:55:21 +00001268 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1269 char buf[400];
1270 int len;
1271 int res;
1272
Daniel Veillard27f20102004-11-05 11:50:11 +00001273 if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
William M. Brack015ccb22005-02-13 08:18:52 +00001274 if (directory == NULL) return 0;
Daniel Veillard27f20102004-11-05 11:50:11 +00001275
Owen Taylor3473f882001-02-23 17:55:21 +00001276 /*
1277 * Expected response code for CWD:
1278 *
1279 * CWD
1280 * 250
1281 * 500, 501, 502, 421, 530, 550
1282 */
Owen Taylor3473f882001-02-23 17:55:21 +00001283 snprintf(buf, sizeof(buf), "CWD %s\r\n", directory);
Owen Taylor3473f882001-02-23 17:55:21 +00001284 buf[sizeof(buf) - 1] = 0;
1285 len = strlen(buf);
1286#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001287 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001288#endif
1289 res = send(ctxt->controlFd, buf, len, 0);
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001290 if (res < 0) {
1291 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1292 return(res);
1293 }
Owen Taylor3473f882001-02-23 17:55:21 +00001294 res = xmlNanoFTPGetResponse(ctxt);
1295 if (res == 4) {
1296 return(-1);
1297 }
1298 if (res == 2) return(1);
1299 if (res == 5) {
1300 return(0);
1301 }
1302 return(0);
1303}
1304
1305/**
Daniel Veillard6c73cb82003-03-05 16:45:40 +00001306 * xmlNanoFTPDele:
1307 * @ctx: an FTP context
1308 * @file: a file or directory on the server
1309 *
1310 * Tries to delete an item (file or directory) from server
1311 *
1312 * Returns -1 incase of error, 1 if DELE worked, 0 if it failed
1313 */
1314
1315int
Daniel Veillard34099b42004-11-04 17:34:35 +00001316xmlNanoFTPDele(void *ctx, const char *file) {
Daniel Veillard6c73cb82003-03-05 16:45:40 +00001317 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1318 char buf[400];
1319 int len;
1320 int res;
1321
Daniel Veillard27f20102004-11-05 11:50:11 +00001322 if ((ctxt == NULL) || (ctxt->controlFd < 0) || (file == NULL)) return(-1);
William M. Brack015ccb22005-02-13 08:18:52 +00001323 if (file == NULL) return (0);
Daniel Veillard27f20102004-11-05 11:50:11 +00001324
Daniel Veillard6c73cb82003-03-05 16:45:40 +00001325 /*
1326 * Expected response code for DELE:
1327 *
1328 * DELE
1329 * 250
1330 * 450, 550
1331 * 500, 501, 502, 421, 530
1332 */
1333
1334 snprintf(buf, sizeof(buf), "DELE %s\r\n", file);
1335 buf[sizeof(buf) - 1] = 0;
1336 len = strlen(buf);
1337#ifdef DEBUG_FTP
1338 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1339#endif
1340 res = send(ctxt->controlFd, buf, len, 0);
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001341 if (res < 0) {
1342 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1343 return(res);
1344 }
Daniel Veillard6c73cb82003-03-05 16:45:40 +00001345 res = xmlNanoFTPGetResponse(ctxt);
1346 if (res == 4) {
1347 return(-1);
1348 }
1349 if (res == 2) return(1);
1350 if (res == 5) {
1351 return(0);
1352 }
1353 return(0);
1354}
1355/**
Owen Taylor3473f882001-02-23 17:55:21 +00001356 * xmlNanoFTPGetConnection:
1357 * @ctx: an FTP context
1358 *
1359 * Try to open a data connection to the server. Currently only
1360 * passive mode is supported.
1361 *
1362 * Returns -1 incase of error, 0 otherwise
1363 */
1364
1365int
1366xmlNanoFTPGetConnection(void *ctx) {
1367 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1368 char buf[200], *cur;
1369 int len, i;
1370 int res;
1371 unsigned char ad[6], *adp, *portp;
1372 unsigned int temp[6];
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001373#ifdef SUPPORT_IP6
1374 struct sockaddr_storage dataAddr;
1375#else
Owen Taylor3473f882001-02-23 17:55:21 +00001376 struct sockaddr_in dataAddr;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001377#endif
Daniel Veillardc284c642005-03-31 10:24:24 +00001378 XML_SOCKLEN_T dataAddrLen;
Owen Taylor3473f882001-02-23 17:55:21 +00001379
Daniel Veillard27f20102004-11-05 11:50:11 +00001380 if (ctxt == NULL) return(-1);
1381
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001382 memset (&dataAddr, 0, sizeof(dataAddr));
1383#ifdef SUPPORT_IP6
1384 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1385 ctxt->dataFd = socket (AF_INET6, SOCK_STREAM, IPPROTO_TCP);
1386 ((struct sockaddr_in6 *)&dataAddr)->sin6_family = AF_INET6;
1387 dataAddrLen = sizeof(struct sockaddr_in6);
1388 } else
1389#endif
1390 {
1391 ctxt->dataFd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1392 ((struct sockaddr_in *)&dataAddr)->sin_family = AF_INET;
1393 dataAddrLen = sizeof (struct sockaddr_in);
Owen Taylor3473f882001-02-23 17:55:21 +00001394 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001395
1396 if (ctxt->dataFd < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001397 __xmlIOErr(XML_FROM_FTP, 0, "socket failed");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001398 return (-1);
1399 }
Owen Taylor3473f882001-02-23 17:55:21 +00001400
1401 if (ctxt->passive) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001402#ifdef SUPPORT_IP6
1403 if ((ctxt->ftpAddr).ss_family == AF_INET6)
1404 snprintf (buf, sizeof(buf), "EPSV\r\n");
1405 else
1406#endif
1407 snprintf (buf, sizeof(buf), "PASV\r\n");
1408 len = strlen (buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001409#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001410 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001411#endif
1412 res = send(ctxt->controlFd, buf, len, 0);
1413 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001414 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001415 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1416 return(res);
1417 }
1418 res = xmlNanoFTPReadResponse(ctx);
1419 if (res != 2) {
1420 if (res == 5) {
1421 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1422 return(-1);
1423 } else {
1424 /*
1425 * retry with an active connection
1426 */
1427 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1428 ctxt->passive = 0;
1429 }
1430 }
1431 cur = &ctxt->controlBuf[ctxt->controlBufAnswer];
1432 while (((*cur < '0') || (*cur > '9')) && *cur != '\0') cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001433#ifdef SUPPORT_IP6
1434 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1435 if (sscanf (cur, "%u", &temp[0]) != 1) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001436 __xmlIOErr(XML_FROM_FTP, XML_FTP_EPSV_ANSWER,
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001437 "Invalid answer to EPSV\n");
1438 if (ctxt->dataFd != -1) {
1439 closesocket (ctxt->dataFd); ctxt->dataFd = -1;
1440 }
1441 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001442 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001443 memcpy (&((struct sockaddr_in6 *)&dataAddr)->sin6_addr, &((struct sockaddr_in6 *)&ctxt->ftpAddr)->sin6_addr, sizeof(struct in6_addr));
1444 ((struct sockaddr_in6 *)&dataAddr)->sin6_port = htons (temp[0]);
Owen Taylor3473f882001-02-23 17:55:21 +00001445 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001446 else
1447#endif
1448 {
1449 if (sscanf (cur, "%u,%u,%u,%u,%u,%u", &temp[0], &temp[1], &temp[2],
1450 &temp[3], &temp[4], &temp[5]) != 6) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001451 __xmlIOErr(XML_FROM_FTP, XML_FTP_PASV_ANSWER,
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001452 "Invalid answer to PASV\n");
1453 if (ctxt->dataFd != -1) {
1454 closesocket (ctxt->dataFd); ctxt->dataFd = -1;
1455 }
1456 return (-1);
1457 }
1458 for (i=0; i<6; i++) ad[i] = (unsigned char) (temp[i] & 0xff);
1459 memcpy (&((struct sockaddr_in *)&dataAddr)->sin_addr, &ad[0], 4);
1460 memcpy (&((struct sockaddr_in *)&dataAddr)->sin_port, &ad[4], 2);
1461 }
1462
Owen Taylor3473f882001-02-23 17:55:21 +00001463 if (connect(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001464 __xmlIOErr(XML_FROM_FTP, 0, "Failed to create a data connection");
Owen Taylor3473f882001-02-23 17:55:21 +00001465 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1466 return (-1);
1467 }
1468 } else {
1469 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001470#ifdef SUPPORT_IP6
1471 if ((ctxt->ftpAddr).ss_family == AF_INET6)
1472 ((struct sockaddr_in6 *)&dataAddr)->sin6_port = 0;
1473 else
1474#endif
1475 ((struct sockaddr_in *)&dataAddr)->sin_port = 0;
1476
Owen Taylor3473f882001-02-23 17:55:21 +00001477 if (bind(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001478 __xmlIOErr(XML_FROM_FTP, 0, "bind failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001479 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1480 return (-1);
1481 }
1482 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
1483
1484 if (listen(ctxt->dataFd, 1) < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001485 __xmlIOErr(XML_FROM_FTP, 0, "listen failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001486 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1487 return (-1);
1488 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001489#ifdef SUPPORT_IP6
1490 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1491 char buf6[INET6_ADDRSTRLEN];
1492 inet_ntop (AF_INET6, &((struct sockaddr_in6 *)&dataAddr)->sin6_addr,
1493 buf6, INET6_ADDRSTRLEN);
Daniel Veillard2db8c122003-07-08 12:16:59 +00001494 adp = (unsigned char *) buf6;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001495 portp = (unsigned char *) &((struct sockaddr_in6 *)&dataAddr)->sin6_port;
1496 snprintf (buf, sizeof(buf), "EPRT |2|%s|%s|\r\n", adp, portp);
1497 } else
1498#endif
1499 {
1500 adp = (unsigned char *) &((struct sockaddr_in *)&dataAddr)->sin_addr;
1501 portp = (unsigned char *) &((struct sockaddr_in *)&dataAddr)->sin_port;
1502 snprintf (buf, sizeof(buf), "PORT %d,%d,%d,%d,%d,%d\r\n",
1503 adp[0] & 0xff, adp[1] & 0xff, adp[2] & 0xff, adp[3] & 0xff,
1504 portp[0] & 0xff, portp[1] & 0xff);
1505 }
1506
Owen Taylor3473f882001-02-23 17:55:21 +00001507 buf[sizeof(buf) - 1] = 0;
1508 len = strlen(buf);
1509#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001510 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001511#endif
1512
1513 res = send(ctxt->controlFd, buf, len, 0);
1514 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001515 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001516 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1517 return(res);
1518 }
1519 res = xmlNanoFTPGetResponse(ctxt);
1520 if (res != 2) {
1521 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1522 return(-1);
1523 }
1524 }
1525 return(ctxt->dataFd);
1526
1527}
1528
1529/**
1530 * xmlNanoFTPCloseConnection:
1531 * @ctx: an FTP context
1532 *
1533 * Close the data connection from the server
1534 *
1535 * Returns -1 incase of error, 0 otherwise
1536 */
1537
1538int
1539xmlNanoFTPCloseConnection(void *ctx) {
1540 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1541 int res;
1542 fd_set rfd, efd;
1543 struct timeval tv;
1544
Daniel Veillard27f20102004-11-05 11:50:11 +00001545 if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
1546
Owen Taylor3473f882001-02-23 17:55:21 +00001547 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1548 tv.tv_sec = 15;
1549 tv.tv_usec = 0;
1550 FD_ZERO(&rfd);
1551 FD_SET(ctxt->controlFd, &rfd);
1552 FD_ZERO(&efd);
1553 FD_SET(ctxt->controlFd, &efd);
1554 res = select(ctxt->controlFd + 1, &rfd, NULL, &efd, &tv);
1555 if (res < 0) {
1556#ifdef DEBUG_FTP
1557 perror("select");
1558#endif
1559 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1560 return(-1);
1561 }
1562 if (res == 0) {
1563#ifdef DEBUG_FTP
1564 xmlGenericError(xmlGenericErrorContext,
1565 "xmlNanoFTPCloseConnection: timeout\n");
1566#endif
1567 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1568 } else {
1569 res = xmlNanoFTPGetResponse(ctxt);
1570 if (res != 2) {
1571 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1572 return(-1);
1573 }
1574 }
1575 return(0);
1576}
1577
1578/**
1579 * xmlNanoFTPParseList:
1580 * @list: some data listing received from the server
1581 * @callback: the user callback
1582 * @userData: the user callback data
1583 *
1584 * Parse at most one entry from the listing.
1585 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001586 * Returns -1 incase of error, the length of data parsed otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00001587 */
1588
1589static int
1590xmlNanoFTPParseList(const char *list, ftpListCallback callback, void *userData) {
1591 const char *cur = list;
1592 char filename[151];
1593 char attrib[11];
1594 char owner[11];
1595 char group[11];
1596 char month[4];
1597 int year = 0;
1598 int minute = 0;
1599 int hour = 0;
1600 int day = 0;
1601 unsigned long size = 0;
1602 int links = 0;
1603 int i;
1604
1605 if (!strncmp(cur, "total", 5)) {
1606 cur += 5;
1607 while (*cur == ' ') cur++;
1608 while ((*cur >= '0') && (*cur <= '9'))
1609 links = (links * 10) + (*cur++ - '0');
1610 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1611 cur++;
1612 return(cur - list);
1613 } else if (*list == '+') {
1614 return(0);
1615 } else {
1616 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1617 cur++;
1618 if (*cur == 0) return(0);
1619 i = 0;
1620 while (*cur != ' ') {
1621 if (i < 10)
1622 attrib[i++] = *cur;
1623 cur++;
1624 if (*cur == 0) return(0);
1625 }
1626 attrib[10] = 0;
1627 while (*cur == ' ') cur++;
1628 if (*cur == 0) return(0);
1629 while ((*cur >= '0') && (*cur <= '9'))
1630 links = (links * 10) + (*cur++ - '0');
1631 while (*cur == ' ') cur++;
1632 if (*cur == 0) return(0);
1633 i = 0;
1634 while (*cur != ' ') {
1635 if (i < 10)
1636 owner[i++] = *cur;
1637 cur++;
1638 if (*cur == 0) return(0);
1639 }
1640 owner[i] = 0;
1641 while (*cur == ' ') cur++;
1642 if (*cur == 0) return(0);
1643 i = 0;
1644 while (*cur != ' ') {
1645 if (i < 10)
1646 group[i++] = *cur;
1647 cur++;
1648 if (*cur == 0) return(0);
1649 }
1650 group[i] = 0;
1651 while (*cur == ' ') cur++;
1652 if (*cur == 0) return(0);
1653 while ((*cur >= '0') && (*cur <= '9'))
1654 size = (size * 10) + (*cur++ - '0');
1655 while (*cur == ' ') cur++;
1656 if (*cur == 0) return(0);
1657 i = 0;
1658 while (*cur != ' ') {
1659 if (i < 3)
1660 month[i++] = *cur;
1661 cur++;
1662 if (*cur == 0) return(0);
1663 }
1664 month[i] = 0;
1665 while (*cur == ' ') cur++;
1666 if (*cur == 0) return(0);
1667 while ((*cur >= '0') && (*cur <= '9'))
1668 day = (day * 10) + (*cur++ - '0');
1669 while (*cur == ' ') cur++;
1670 if (*cur == 0) return(0);
1671 if ((cur[1] == 0) || (cur[2] == 0)) return(0);
1672 if ((cur[1] == ':') || (cur[2] == ':')) {
1673 while ((*cur >= '0') && (*cur <= '9'))
1674 hour = (hour * 10) + (*cur++ - '0');
1675 if (*cur == ':') cur++;
1676 while ((*cur >= '0') && (*cur <= '9'))
1677 minute = (minute * 10) + (*cur++ - '0');
1678 } else {
1679 while ((*cur >= '0') && (*cur <= '9'))
1680 year = (year * 10) + (*cur++ - '0');
1681 }
1682 while (*cur == ' ') cur++;
1683 if (*cur == 0) return(0);
1684 i = 0;
1685 while ((*cur != '\n') && (*cur != '\r')) {
1686 if (i < 150)
1687 filename[i++] = *cur;
1688 cur++;
1689 if (*cur == 0) return(0);
1690 }
1691 filename[i] = 0;
1692 if ((*cur != '\n') && (*cur != '\r'))
1693 return(0);
1694 while ((*cur == '\n') || (*cur == '\r'))
1695 cur++;
1696 }
1697 if (callback != NULL) {
1698 callback(userData, filename, attrib, owner, group, size, links,
1699 year, month, day, hour, minute);
1700 }
1701 return(cur - list);
1702}
1703
1704/**
1705 * xmlNanoFTPList:
1706 * @ctx: an FTP context
1707 * @callback: the user callback
1708 * @userData: the user callback data
1709 * @filename: optional files to list
1710 *
1711 * Do a listing on the server. All files info are passed back
1712 * in the callbacks.
1713 *
1714 * Returns -1 incase of error, 0 otherwise
1715 */
1716
1717int
1718xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData,
Daniel Veillard34099b42004-11-04 17:34:35 +00001719 const char *filename) {
Owen Taylor3473f882001-02-23 17:55:21 +00001720 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1721 char buf[4096 + 1];
1722 int len, res;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001723 int indx = 0, base;
Owen Taylor3473f882001-02-23 17:55:21 +00001724 fd_set rfd, efd;
1725 struct timeval tv;
1726
William M. Brack015ccb22005-02-13 08:18:52 +00001727 if (ctxt == NULL) return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001728 if (filename == NULL) {
1729 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1730 return(-1);
1731 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1732 if (ctxt->dataFd == -1)
1733 return(-1);
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001734 snprintf(buf, sizeof(buf), "LIST -L\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001735 } else {
1736 if (filename[0] != '/') {
1737 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1738 return(-1);
1739 }
1740 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1741 if (ctxt->dataFd == -1)
1742 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001743 snprintf(buf, sizeof(buf), "LIST -L %s\r\n", filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001744 }
1745 buf[sizeof(buf) - 1] = 0;
1746 len = strlen(buf);
1747#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001748 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001749#endif
1750 res = send(ctxt->controlFd, buf, len, 0);
1751 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001752 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001753 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1754 return(res);
1755 }
1756 res = xmlNanoFTPReadResponse(ctxt);
1757 if (res != 1) {
1758 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1759 return(-res);
1760 }
1761
1762 do {
1763 tv.tv_sec = 1;
1764 tv.tv_usec = 0;
1765 FD_ZERO(&rfd);
1766 FD_SET(ctxt->dataFd, &rfd);
1767 FD_ZERO(&efd);
1768 FD_SET(ctxt->dataFd, &efd);
1769 res = select(ctxt->dataFd + 1, &rfd, NULL, &efd, &tv);
1770 if (res < 0) {
1771#ifdef DEBUG_FTP
1772 perror("select");
1773#endif
1774 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1775 return(-1);
1776 }
1777 if (res == 0) {
1778 res = xmlNanoFTPCheckResponse(ctxt);
1779 if (res < 0) {
1780 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1781 ctxt->dataFd = -1;
1782 return(-1);
1783 }
1784 if (res == 2) {
1785 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1786 return(0);
1787 }
1788
1789 continue;
1790 }
1791
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001792 if ((len = recv(ctxt->dataFd, &buf[indx], sizeof(buf) - (indx + 1), 0)) < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001793 __xmlIOErr(XML_FROM_FTP, 0, "recv");
Owen Taylor3473f882001-02-23 17:55:21 +00001794 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1795 ctxt->dataFd = -1;
1796 return(-1);
1797 }
1798#ifdef DEBUG_FTP
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001799 write(1, &buf[indx], len);
Owen Taylor3473f882001-02-23 17:55:21 +00001800#endif
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001801 indx += len;
1802 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001803 base = 0;
1804 do {
1805 res = xmlNanoFTPParseList(&buf[base], callback, userData);
1806 base += res;
1807 } while (res > 0);
1808
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001809 memmove(&buf[0], &buf[base], indx - base);
1810 indx -= base;
Owen Taylor3473f882001-02-23 17:55:21 +00001811 } while (len != 0);
1812 xmlNanoFTPCloseConnection(ctxt);
1813 return(0);
1814}
1815
1816/**
1817 * xmlNanoFTPGetSocket:
1818 * @ctx: an FTP context
1819 * @filename: the file to retrieve (or NULL if path is in context).
1820 *
1821 * Initiate fetch of the given file from the server.
1822 *
1823 * Returns the socket for the data connection, or <0 in case of error
1824 */
1825
1826
1827int
1828xmlNanoFTPGetSocket(void *ctx, const char *filename) {
1829 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1830 char buf[300];
1831 int res, len;
Daniel Veillard27f20102004-11-05 11:50:11 +00001832 if (ctx == NULL)
1833 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001834 if ((filename == NULL) && (ctxt->path == NULL))
1835 return(-1);
1836 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1837 if (ctxt->dataFd == -1)
1838 return(-1);
1839
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001840 snprintf(buf, sizeof(buf), "TYPE I\r\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001841 len = strlen(buf);
1842#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001843 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001844#endif
1845 res = send(ctxt->controlFd, buf, len, 0);
1846 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001847 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001848 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1849 return(res);
1850 }
1851 res = xmlNanoFTPReadResponse(ctxt);
1852 if (res != 2) {
1853 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1854 return(-res);
1855 }
1856 if (filename == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001857 snprintf(buf, sizeof(buf), "RETR %s\r\n", ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001858 else
Owen Taylor3473f882001-02-23 17:55:21 +00001859 snprintf(buf, sizeof(buf), "RETR %s\r\n", filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001860 buf[sizeof(buf) - 1] = 0;
1861 len = strlen(buf);
1862#ifdef DEBUG_FTP
Daniel Veillard635ef722001-10-29 11:48:19 +00001863 xmlGenericError(xmlGenericErrorContext, "%s", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001864#endif
1865 res = send(ctxt->controlFd, buf, len, 0);
1866 if (res < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001867 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001868 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 return(ctxt->dataFd);
1877}
1878
1879/**
1880 * xmlNanoFTPGet:
1881 * @ctx: an FTP context
1882 * @callback: the user callback
1883 * @userData: the user callback data
1884 * @filename: the file to retrieve
1885 *
1886 * Fetch the given file from the server. All data are passed back
1887 * in the callbacks. The last callback has a size of 0 block.
1888 *
1889 * Returns -1 incase of error, 0 otherwise
1890 */
1891
1892int
1893xmlNanoFTPGet(void *ctx, ftpDataCallback callback, void *userData,
1894 const char *filename) {
1895 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1896 char buf[4096];
1897 int len = 0, res;
1898 fd_set rfd;
1899 struct timeval tv;
1900
William M. Brack015ccb22005-02-13 08:18:52 +00001901 if (ctxt == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001902 if ((filename == NULL) && (ctxt->path == NULL))
1903 return(-1);
1904 if (callback == NULL)
1905 return(-1);
1906 if (xmlNanoFTPGetSocket(ctxt, filename) < 0)
1907 return(-1);
1908
1909 do {
1910 tv.tv_sec = 1;
1911 tv.tv_usec = 0;
1912 FD_ZERO(&rfd);
1913 FD_SET(ctxt->dataFd, &rfd);
1914 res = select(ctxt->dataFd + 1, &rfd, NULL, NULL, &tv);
1915 if (res < 0) {
1916#ifdef DEBUG_FTP
1917 perror("select");
1918#endif
1919 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1920 return(-1);
1921 }
1922 if (res == 0) {
1923 res = xmlNanoFTPCheckResponse(ctxt);
1924 if (res < 0) {
1925 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1926 ctxt->dataFd = -1;
1927 return(-1);
1928 }
1929 if (res == 2) {
1930 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1931 return(0);
1932 }
1933
1934 continue;
1935 }
1936 if ((len = recv(ctxt->dataFd, buf, sizeof(buf), 0)) < 0) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001937 __xmlIOErr(XML_FROM_FTP, 0, "recv failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001938 callback(userData, buf, len);
1939 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1940 return(-1);
1941 }
1942 callback(userData, buf, len);
1943 } while (len != 0);
1944
1945 return(xmlNanoFTPCloseConnection(ctxt));
1946}
1947
1948/**
1949 * xmlNanoFTPRead:
1950 * @ctx: the FTP context
1951 * @dest: a buffer
1952 * @len: the buffer length
1953 *
1954 * This function tries to read @len bytes from the existing FTP connection
1955 * and saves them in @dest. This is a blocking call.
1956 *
1957 * Returns the number of byte read. 0 is an indication of an end of connection.
1958 * -1 indicates a parameter error.
1959 */
1960int
1961xmlNanoFTPRead(void *ctx, void *dest, int len) {
1962 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1963
1964 if (ctx == NULL) return(-1);
1965 if (ctxt->dataFd < 0) return(0);
1966 if (dest == NULL) return(-1);
1967 if (len <= 0) return(0);
1968
1969 len = recv(ctxt->dataFd, dest, len, 0);
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001970 if (len <= 0) {
William M. Brack015ccb22005-02-13 08:18:52 +00001971 if (len < 0)
1972 __xmlIOErr(XML_FROM_FTP, 0, "recv failed");
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001973 xmlNanoFTPCloseConnection(ctxt);
1974 }
Owen Taylor3473f882001-02-23 17:55:21 +00001975#ifdef DEBUG_FTP
1976 xmlGenericError(xmlGenericErrorContext, "Recvd %d bytes\n", len);
1977#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001978 return(len);
1979}
1980
1981/**
1982 * xmlNanoFTPOpen:
1983 * @URL: the URL to the resource
1984 *
1985 * Start to fetch the given ftp:// resource
1986 *
1987 * Returns an FTP context, or NULL
1988 */
1989
1990void*
1991xmlNanoFTPOpen(const char *URL) {
1992 xmlNanoFTPCtxtPtr ctxt;
1993 int sock;
1994
1995 xmlNanoFTPInit();
1996 if (URL == NULL) return(NULL);
1997 if (strncmp("ftp://", URL, 6)) return(NULL);
1998
1999 ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(URL);
2000 if (ctxt == NULL) return(NULL);
2001 if (xmlNanoFTPConnect(ctxt) < 0) {
2002 xmlNanoFTPFreeCtxt(ctxt);
2003 return(NULL);
2004 }
2005 sock = xmlNanoFTPGetSocket(ctxt, ctxt->path);
2006 if (sock < 0) {
2007 xmlNanoFTPFreeCtxt(ctxt);
2008 return(NULL);
2009 }
2010 return(ctxt);
2011}
2012
2013/**
2014 * xmlNanoFTPClose:
2015 * @ctx: an FTP context
2016 *
2017 * Close the connection and both control and transport
2018 *
2019 * Returns -1 incase of error, 0 otherwise
2020 */
2021
2022int
2023xmlNanoFTPClose(void *ctx) {
2024 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
2025
2026 if (ctxt == NULL)
2027 return(-1);
2028
2029 if (ctxt->dataFd >= 0) {
2030 closesocket(ctxt->dataFd);
2031 ctxt->dataFd = -1;
2032 }
2033 if (ctxt->controlFd >= 0) {
2034 xmlNanoFTPQuit(ctxt);
2035 closesocket(ctxt->controlFd);
2036 ctxt->controlFd = -1;
2037 }
2038 xmlNanoFTPFreeCtxt(ctxt);
2039 return(0);
2040}
2041
2042#ifdef STANDALONE
2043/************************************************************************
2044 * *
2045 * Basic test in Standalone mode *
2046 * *
2047 ************************************************************************/
Daniel Veillard01c13b52002-12-10 15:19:08 +00002048static
Owen Taylor3473f882001-02-23 17:55:21 +00002049void ftpList(void *userData, const char *filename, const char* attrib,
2050 const char *owner, const char *group, unsigned long size, int links,
2051 int year, const char *month, int day, int hour, int minute) {
2052 xmlGenericError(xmlGenericErrorContext,
2053 "%s %s %s %ld %s\n", attrib, owner, group, size, filename);
2054}
Daniel Veillard01c13b52002-12-10 15:19:08 +00002055static
Owen Taylor3473f882001-02-23 17:55:21 +00002056void ftpData(void *userData, const char *data, int len) {
2057 if (userData == NULL) return;
2058 if (len <= 0) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00002059 fclose((FILE*)userData);
Owen Taylor3473f882001-02-23 17:55:21 +00002060 return;
2061 }
Daniel Veillard82cb3192003-10-29 13:39:15 +00002062 fwrite(data, len, 1, (FILE*)userData);
Owen Taylor3473f882001-02-23 17:55:21 +00002063}
2064
2065int main(int argc, char **argv) {
2066 void *ctxt;
2067 FILE *output;
2068 char *tstfile = NULL;
2069
2070 xmlNanoFTPInit();
2071 if (argc > 1) {
2072 ctxt = xmlNanoFTPNewCtxt(argv[1]);
2073 if (xmlNanoFTPConnect(ctxt) < 0) {
2074 xmlGenericError(xmlGenericErrorContext,
2075 "Couldn't connect to %s\n", argv[1]);
2076 exit(1);
2077 }
2078 if (argc > 2)
2079 tstfile = argv[2];
2080 } else
2081 ctxt = xmlNanoFTPConnectTo("localhost", 0);
2082 if (ctxt == NULL) {
2083 xmlGenericError(xmlGenericErrorContext,
2084 "Couldn't connect to localhost\n");
2085 exit(1);
2086 }
2087 xmlNanoFTPList(ctxt, ftpList, NULL, tstfile);
2088 output = fopen("/tmp/tstdata", "w");
2089 if (output != NULL) {
2090 if (xmlNanoFTPGet(ctxt, ftpData, (void *) output, tstfile) < 0)
2091 xmlGenericError(xmlGenericErrorContext,
2092 "Failed to get file\n");
2093
2094 }
2095 xmlNanoFTPClose(ctxt);
2096 xmlMemoryDump();
2097 exit(0);
2098}
2099#endif /* STANDALONE */
2100#else /* !LIBXML_FTP_ENABLED */
2101#ifdef STANDALONE
2102#include <stdio.h>
2103int main(int argc, char **argv) {
2104 xmlGenericError(xmlGenericErrorContext,
2105 "%s : FTP support not compiled in\n", argv[0]);
2106 return(0);
2107}
2108#endif /* STANDALONE */
2109#endif /* LIBXML_FTP_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002110#define bottom_nanoftp
2111#include "elfgcchack.h"