-
Notifications
You must be signed in to change notification settings - Fork 6
/
nanoftp.c
1982 lines (1807 loc) · 48.4 KB
/
nanoftp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* nanoftp.c: basic FTP client support
*
* Reference: RFC 959
*/
#ifdef TESTING
#define STANDALONE
#define HAVE_UNISTD_H
#define HAVE_SYS_SOCKET_H
#define HAVE_NETINET_IN_H
#define HAVE_NETDB_H
#define HAVE_SYS_TIME_H
#endif /* TESTING */
#define IN_LIBXML
#include "libxml.h"
#ifdef LIBXML_FTP_ENABLED
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#elif defined (_WIN32)
#include <io.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/xmlerror.h>
#include <libxml/uri.h>
#include <libxml/nanoftp.h>
#include "private/error.h"
#include "private/io.h"
#if defined(_WIN32)
#include <wsockcompat.h>
#endif
/**
* A couple portability macros
*/
#ifndef _WINSOCKAPI_
#define closesocket(s) close(s)
#endif
#ifndef XML_SOCKLEN_T
#define XML_SOCKLEN_T unsigned int
#endif
#define GETHOSTBYNAME_ARG_CAST (char *)
#define SEND_ARG2_CAST (char *)
#define FTP_COMMAND_OK 200
#define FTP_SYNTAX_ERROR 500
#define FTP_GET_PASSWD 331
#define FTP_BUF_SIZE 1024
#define XML_NANO_MAX_URLBUF 4096
typedef struct xmlNanoFTPCtxt {
char *protocol; /* the protocol name */
char *hostname; /* the host name */
int port; /* the port */
char *path; /* the path within the URL */
char *user; /* user string */
char *passwd; /* passwd string */
#ifdef SUPPORT_IP6
struct sockaddr_storage ftpAddr; /* this is large enough to hold IPv6 address*/
#else
struct sockaddr_in ftpAddr; /* the socket address struct */
#endif
int passive; /* currently we support only passive !!! */
SOCKET controlFd; /* the file descriptor for the control socket */
SOCKET dataFd; /* the file descriptor for the data socket */
int state; /* WRITE / READ / CLOSED */
int returnValue; /* the protocol return value */
/* buffer for data received from the control connection */
char controlBuf[FTP_BUF_SIZE + 1];
int controlBufIndex;
int controlBufUsed;
int controlBufAnswer;
} xmlNanoFTPCtxt, *xmlNanoFTPCtxtPtr;
static int initialized = 0;
static char *proxy = NULL; /* the proxy name if any */
static int proxyPort = 0; /* the proxy port if any */
static char *proxyUser = NULL; /* user for proxy authentication */
static char *proxyPasswd = NULL;/* passwd for proxy authentication */
static int proxyType = 0; /* uses TYPE or a@b ? */
#ifdef SUPPORT_IP6
static
int have_ipv6(void) {
int s;
s = socket (AF_INET6, SOCK_STREAM, 0);
if (s != -1) {
close (s);
return (1);
}
return (0);
}
#endif
/**
* xmlFTPErrMemory:
* @extra: extra information
*
* Handle an out of memory condition
*/
static void
xmlFTPErrMemory(const char *extra ATTRIBUTE_UNUSED)
{
xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_FTP, NULL);
}
/**
* xmlNanoFTPInit:
*
* Initialize the FTP protocol layer.
* Currently it just checks for proxy information,
* and get the hostname
*/
void
xmlNanoFTPInit(void) {
const char *env;
#ifdef _WINSOCKAPI_
WSADATA wsaData;
#endif
if (initialized)
return;
#ifdef _WINSOCKAPI_
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
return;
#endif
proxyPort = 21;
env = getenv("no_proxy");
if (env && ((env[0] == '*' ) && (env[1] == 0)))
return;
env = getenv("ftp_proxy");
if (env != NULL) {
xmlNanoFTPScanProxy(env);
} else {
env = getenv("FTP_PROXY");
if (env != NULL) {
xmlNanoFTPScanProxy(env);
}
}
env = getenv("ftp_proxy_user");
if (env != NULL) {
proxyUser = xmlMemStrdup(env);
}
env = getenv("ftp_proxy_password");
if (env != NULL) {
proxyPasswd = xmlMemStrdup(env);
}
initialized = 1;
}
/**
* xmlNanoFTPCleanup:
*
* Cleanup the FTP protocol layer. This cleanup proxy information.
*/
void
xmlNanoFTPCleanup(void) {
if (proxy != NULL) {
xmlFree(proxy);
proxy = NULL;
}
if (proxyUser != NULL) {
xmlFree(proxyUser);
proxyUser = NULL;
}
if (proxyPasswd != NULL) {
xmlFree(proxyPasswd);
proxyPasswd = NULL;
}
#ifdef _WINSOCKAPI_
if (initialized)
WSACleanup();
#endif
initialized = 0;
}
/**
* xmlNanoFTPProxy:
* @host: the proxy host name
* @port: the proxy port
* @user: the proxy user name
* @passwd: the proxy password
* @type: the type of proxy 1 for using SITE, 2 for USER a@b
*
* Setup the FTP proxy information.
* This can also be done by using ftp_proxy ftp_proxy_user and
* ftp_proxy_password environment variables.
*/
void
xmlNanoFTPProxy(const char *host, int port, const char *user,
const char *passwd, int type) {
if (proxy != NULL) {
xmlFree(proxy);
proxy = NULL;
}
if (proxyUser != NULL) {
xmlFree(proxyUser);
proxyUser = NULL;
}
if (proxyPasswd != NULL) {
xmlFree(proxyPasswd);
proxyPasswd = NULL;
}
if (host)
proxy = xmlMemStrdup(host);
if (user)
proxyUser = xmlMemStrdup(user);
if (passwd)
proxyPasswd = xmlMemStrdup(passwd);
proxyPort = port;
proxyType = type;
}
/**
* xmlNanoFTPScanURL:
* @ctx: an FTP context
* @URL: The URL used to initialize the context
*
* (Re)Initialize an FTP context by parsing the URL and finding
* the protocol host port and path it indicates.
*/
static void
xmlNanoFTPScanURL(void *ctx, const char *URL) {
xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
xmlURIPtr uri;
/*
* Clear any existing data from the context
*/
if (ctxt->protocol != NULL) {
xmlFree(ctxt->protocol);
ctxt->protocol = NULL;
}
if (ctxt->hostname != NULL) {
xmlFree(ctxt->hostname);
ctxt->hostname = NULL;
}
if (ctxt->path != NULL) {
xmlFree(ctxt->path);
ctxt->path = NULL;
}
if (URL == NULL) return;
uri = xmlParseURIRaw(URL, 1);
if (uri == NULL)
return;
if ((uri->scheme == NULL) || (uri->server == NULL)) {
xmlFreeURI(uri);
return;
}
ctxt->protocol = xmlMemStrdup(uri->scheme);
ctxt->hostname = xmlMemStrdup(uri->server);
if (uri->path != NULL)
ctxt->path = xmlMemStrdup(uri->path);
else
ctxt->path = xmlMemStrdup("/");
if (uri->port != 0)
ctxt->port = uri->port;
if (uri->user != NULL) {
char *cptr;
if ((cptr=strchr(uri->user, ':')) == NULL)
ctxt->user = xmlMemStrdup(uri->user);
else {
ctxt->user = (char *)xmlStrndup((xmlChar *)uri->user,
(cptr - uri->user));
ctxt->passwd = xmlMemStrdup(cptr+1);
}
}
xmlFreeURI(uri);
}
/**
* xmlNanoFTPUpdateURL:
* @ctx: an FTP context
* @URL: The URL used to update the context
*
* Update an FTP context by parsing the URL and finding
* new path it indicates. If there is an error in the
* protocol, hostname, port or other information, the
* error is raised. It indicates a new connection has to
* be established.
*
* Returns 0 if Ok, -1 in case of error (other host).
*/
int
xmlNanoFTPUpdateURL(void *ctx, const char *URL) {
xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
xmlURIPtr uri;
if (URL == NULL)
return(-1);
if (ctxt == NULL)
return(-1);
if (ctxt->protocol == NULL)
return(-1);
if (ctxt->hostname == NULL)
return(-1);
uri = xmlParseURIRaw(URL, 1);
if (uri == NULL)
return(-1);
if ((uri->scheme == NULL) || (uri->server == NULL)) {
xmlFreeURI(uri);
return(-1);
}
if ((strcmp(ctxt->protocol, uri->scheme)) ||
(strcmp(ctxt->hostname, uri->server)) ||
((uri->port != 0) && (ctxt->port != uri->port))) {
xmlFreeURI(uri);
return(-1);
}
if (uri->port != 0)
ctxt->port = uri->port;
if (ctxt->path != NULL) {
xmlFree(ctxt->path);
ctxt->path = NULL;
}
if (uri->path == NULL)
ctxt->path = xmlMemStrdup("/");
else
ctxt->path = xmlMemStrdup(uri->path);
xmlFreeURI(uri);
return(0);
}
/**
* xmlNanoFTPScanProxy:
* @URL: The proxy URL used to initialize the proxy context
*
* (Re)Initialize the FTP Proxy context by parsing the URL and finding
* the protocol host port it indicates.
* Should be like ftp://myproxy/ or ftp://myproxy:3128/
* A NULL URL cleans up proxy information.
*/
void
xmlNanoFTPScanProxy(const char *URL) {
xmlURIPtr uri;
if (proxy != NULL) {
xmlFree(proxy);
proxy = NULL;
}
proxyPort = 0;
if (URL == NULL) return;
uri = xmlParseURIRaw(URL, 1);
if ((uri == NULL) || (uri->scheme == NULL) ||
(strcmp(uri->scheme, "ftp")) || (uri->server == NULL)) {
__xmlIOErr(XML_FROM_FTP, XML_FTP_URL_SYNTAX, "Syntax Error\n");
if (uri != NULL)
xmlFreeURI(uri);
return;
}
proxy = xmlMemStrdup(uri->server);
if (uri->port != 0)
proxyPort = uri->port;
xmlFreeURI(uri);
}
/**
* xmlNanoFTPNewCtxt:
* @URL: The URL used to initialize the context
*
* Allocate and initialize a new FTP context.
*
* Returns an FTP context or NULL in case of error.
*/
void*
xmlNanoFTPNewCtxt(const char *URL) {
xmlNanoFTPCtxtPtr ret;
char *unescaped;
ret = (xmlNanoFTPCtxtPtr) xmlMalloc(sizeof(xmlNanoFTPCtxt));
if (ret == NULL) {
xmlFTPErrMemory("allocating FTP context");
return(NULL);
}
memset(ret, 0, sizeof(xmlNanoFTPCtxt));
ret->port = 21;
ret->passive = 1;
ret->returnValue = 0;
ret->controlBufIndex = 0;
ret->controlBufUsed = 0;
ret->controlFd = INVALID_SOCKET;
unescaped = xmlURIUnescapeString(URL, 0, NULL);
if (unescaped != NULL) {
xmlNanoFTPScanURL(ret, unescaped);
xmlFree(unescaped);
} else if (URL != NULL)
xmlNanoFTPScanURL(ret, URL);
return(ret);
}
/**
* xmlNanoFTPFreeCtxt:
* @ctx: an FTP context
*
* Frees the context after closing the connection.
*/
void
xmlNanoFTPFreeCtxt(void * ctx) {
xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
if (ctxt == NULL) return;
if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
if (ctxt->path != NULL) xmlFree(ctxt->path);
if (ctxt->user != NULL) xmlFree(ctxt->user);
if (ctxt->passwd != NULL) xmlFree(ctxt->passwd);
ctxt->passive = 1;
if (ctxt->controlFd != INVALID_SOCKET) closesocket(ctxt->controlFd);
ctxt->controlFd = INVALID_SOCKET;
ctxt->controlBufIndex = -1;
ctxt->controlBufUsed = -1;
xmlFree(ctxt);
}
/**
* xmlNanoFTPParseResponse:
* @buf: the buffer containing the response
* @len: the buffer length
*
* Parsing of the server answer, we just extract the code.
*
* returns 0 for errors
* +XXX for last line of response
* -XXX for response to be continued
*/
static int
xmlNanoFTPParseResponse(char *buf, int len) {
int val = 0;
if (len < 3) return(-1);
if ((*buf >= '0') && (*buf <= '9'))
val = val * 10 + (*buf - '0');
else
return(0);
buf++;
if ((*buf >= '0') && (*buf <= '9'))
val = val * 10 + (*buf - '0');
else
return(0);
buf++;
if ((*buf >= '0') && (*buf <= '9'))
val = val * 10 + (*buf - '0');
else
return(0);
buf++;
if (*buf == '-')
return(-val);
return(val);
}
/**
* xmlNanoFTPGetMore:
* @ctx: an FTP context
*
* Read more information from the FTP control connection
* Returns the number of bytes read, < 0 indicates an error
*/
static int
xmlNanoFTPGetMore(void *ctx) {
xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
int len;
int size;
if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
if ((ctxt->controlBufIndex < 0) || (ctxt->controlBufIndex > FTP_BUF_SIZE)) {
return(-1);
}
if ((ctxt->controlBufUsed < 0) || (ctxt->controlBufUsed > FTP_BUF_SIZE)) {
return(-1);
}
if (ctxt->controlBufIndex > ctxt->controlBufUsed) {
return(-1);
}
/*
* First pack the control buffer
*/
if (ctxt->controlBufIndex > 0) {
memmove(&ctxt->controlBuf[0], &ctxt->controlBuf[ctxt->controlBufIndex],
ctxt->controlBufUsed - ctxt->controlBufIndex);
ctxt->controlBufUsed -= ctxt->controlBufIndex;
ctxt->controlBufIndex = 0;
}
size = FTP_BUF_SIZE - ctxt->controlBufUsed;
if (size == 0) {
return(0);
}
/*
* Read the amount left on the control connection
*/
if ((len = recv(ctxt->controlFd, &ctxt->controlBuf[ctxt->controlBufIndex],
size, 0)) < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "recv failed");
closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
ctxt->controlBufUsed += len;
ctxt->controlBuf[ctxt->controlBufUsed] = 0;
return(len);
}
/**
* xmlNanoFTPReadResponse:
* @ctx: an FTP context
*
* Read the response from the FTP server after a command.
* Returns the code number
*/
static int
xmlNanoFTPReadResponse(void *ctx) {
xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
char *ptr, *end;
int len;
int res = -1, cur = -1;
if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
get_more:
/*
* Assumes everything up to controlBuf[controlBufIndex] has been read
* and analyzed.
*/
len = xmlNanoFTPGetMore(ctx);
if (len < 0) {
return(-1);
}
if ((ctxt->controlBufUsed == 0) && (len == 0)) {
return(-1);
}
ptr = &ctxt->controlBuf[ctxt->controlBufIndex];
end = &ctxt->controlBuf[ctxt->controlBufUsed];
while (ptr < end) {
cur = xmlNanoFTPParseResponse(ptr, end - ptr);
if (cur > 0) {
/*
* Successfully scanned the control code, scratch
* till the end of the line, but keep the index to be
* able to analyze the result if needed.
*/
res = cur;
ptr += 3;
ctxt->controlBufAnswer = ptr - ctxt->controlBuf;
while ((ptr < end) && (*ptr != '\n')) ptr++;
if (*ptr == '\n') ptr++;
if (*ptr == '\r') ptr++;
break;
}
while ((ptr < end) && (*ptr != '\n')) ptr++;
if (ptr >= end) {
ctxt->controlBufIndex = ctxt->controlBufUsed;
goto get_more;
}
if (*ptr != '\r') ptr++;
}
if (res < 0) goto get_more;
ctxt->controlBufIndex = ptr - ctxt->controlBuf;
return(res / 100);
}
/**
* xmlNanoFTPGetResponse:
* @ctx: an FTP context
*
* Get the response from the FTP server after a command.
* Returns the code number
*/
int
xmlNanoFTPGetResponse(void *ctx) {
int res;
res = xmlNanoFTPReadResponse(ctx);
return(res);
}
/**
* xmlNanoFTPCheckResponse:
* @ctx: an FTP context
*
* Check if there is a response from the FTP server after a command.
* Returns the code number, or 0
*/
int
xmlNanoFTPCheckResponse(void *ctx) {
xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
fd_set rfd;
struct timeval tv;
if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&rfd);
FD_SET(ctxt->controlFd, &rfd);
switch(select(ctxt->controlFd + 1, &rfd, NULL, NULL, &tv)) {
case 0:
return(0);
case -1:
__xmlIOErr(XML_FROM_FTP, 0, "select");
return(-1);
}
return(xmlNanoFTPReadResponse(ctx));
}
/**
* Send the user authentication
*/
static int
xmlNanoFTPSendUser(void *ctx) {
xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
char buf[200];
int len;
int res;
if (ctxt->user == NULL)
snprintf(buf, sizeof(buf), "USER anonymous\r\n");
else
snprintf(buf, sizeof(buf), "USER %s\r\n", ctxt->user);
buf[sizeof(buf) - 1] = 0;
len = strlen(buf);
res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
return(res);
}
return(0);
}
/**
* Send the password authentication
*/
static int
xmlNanoFTPSendPasswd(void *ctx) {
xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
char buf[200];
int len;
int res;
if (ctxt->passwd == NULL)
snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
else
snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
buf[sizeof(buf) - 1] = 0;
len = strlen(buf);
res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
return(res);
}
return(0);
}
/**
* xmlNanoFTPQuit:
* @ctx: an FTP context
*
* Send a QUIT command to the server
*
* Returns -1 in case of error, 0 otherwise
*/
int
xmlNanoFTPQuit(void *ctx) {
xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
char buf[200];
int len, res;
if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
snprintf(buf, sizeof(buf), "QUIT\r\n");
len = strlen(buf);
res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
return(res);
}
return(0);
}
/**
* xmlNanoFTPConnect:
* @ctx: an FTP context
*
* Tries to open a control connection
*
* Returns -1 in case of error, 0 otherwise
*/
int
xmlNanoFTPConnect(void *ctx) {
xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
struct hostent *hp;
int port;
int res;
int addrlen = sizeof (struct sockaddr_in);
if (ctxt == NULL)
return(-1);
if (ctxt->hostname == NULL)
return(-1);
/*
* do the blocking DNS query.
*/
if (proxy) {
port = proxyPort;
} else {
port = ctxt->port;
}
if (port == 0)
port = 21;
memset (&ctxt->ftpAddr, 0, sizeof(ctxt->ftpAddr));
#ifdef SUPPORT_IP6
if (have_ipv6 ()) {
struct addrinfo hints, *tmp, *result;
result = NULL;
memset (&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
if (proxy) {
if (getaddrinfo (proxy, NULL, &hints, &result) != 0) {
__xmlIOErr(XML_FROM_FTP, 0, "getaddrinfo failed");
return (-1);
}
}
else
if (getaddrinfo (ctxt->hostname, NULL, &hints, &result) != 0) {
__xmlIOErr(XML_FROM_FTP, 0, "getaddrinfo failed");
return (-1);
}
for (tmp = result; tmp; tmp = tmp->ai_next)
if (tmp->ai_family == AF_INET || tmp->ai_family == AF_INET6)
break;
if (!tmp) {
if (result)
freeaddrinfo (result);
__xmlIOErr(XML_FROM_FTP, 0, "getaddrinfo failed");
return (-1);
}
if ((size_t)tmp->ai_addrlen > sizeof(ctxt->ftpAddr)) {
if (result)
freeaddrinfo (result);
__xmlIOErr(XML_FROM_FTP, 0, "gethostbyname address mismatch");
return (-1);
}
if (tmp->ai_family == AF_INET6) {
memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen);
((struct sockaddr_in6 *) &ctxt->ftpAddr)->sin6_port = htons (port);
ctxt->controlFd = socket (AF_INET6, SOCK_STREAM, 0);
}
else {
memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen);
((struct sockaddr_in *) &ctxt->ftpAddr)->sin_port = htons (port);
ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0);
}
addrlen = tmp->ai_addrlen;
freeaddrinfo (result);
}
else
#endif
{
if (proxy)
hp = gethostbyname (GETHOSTBYNAME_ARG_CAST proxy);
else
hp = gethostbyname (GETHOSTBYNAME_ARG_CAST ctxt->hostname);
if (hp == NULL) {
__xmlIOErr(XML_FROM_FTP, 0, "gethostbyname failed");
return (-1);
}
if ((unsigned int) hp->h_length >
sizeof(((struct sockaddr_in *)&ctxt->ftpAddr)->sin_addr)) {
__xmlIOErr(XML_FROM_FTP, 0, "gethostbyname address mismatch");
return (-1);
}
/*
* Prepare the socket
*/
((struct sockaddr_in *)&ctxt->ftpAddr)->sin_family = AF_INET;
memcpy (&((struct sockaddr_in *)&ctxt->ftpAddr)->sin_addr,
hp->h_addr_list[0], hp->h_length);
((struct sockaddr_in *)&ctxt->ftpAddr)->sin_port =
(unsigned short)htons ((unsigned short)port);
ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0);
addrlen = sizeof (struct sockaddr_in);
}
if (ctxt->controlFd == INVALID_SOCKET) {
__xmlIOErr(XML_FROM_FTP, 0, "socket failed");
return(-1);
}
/*
* Do the connect.
*/
if (connect(ctxt->controlFd, (struct sockaddr *) &ctxt->ftpAddr,
addrlen) < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "Failed to create a connection");
closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
/*
* Wait for the HELLO from the server.
*/
res = xmlNanoFTPGetResponse(ctxt);
if (res != 2) {
closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
/*
* State diagram for the login operation on the FTP server
*
* Reference: RFC 959
*
* 1
* +---+ USER +---+------------->+---+
* | B |---------->| W | 2 ---->| E |
* +---+ +---+------ | -->+---+
* | | | | |
* 3 | | 4,5 | | |
* -------------- ----- | | |
* | | | | |
* | | | | |
* | --------- |
* | 1| | | |
* V | | | |
* +---+ PASS +---+ 2 | ------>+---+
* | |---------->| W |------------->| S |
* +---+ +---+ ---------->+---+
* | | | | |
* 3 | |4,5| | |
* -------------- -------- |
* | | | | |
* | | | | |
* | -----------
* | 1,3| | | |
* V | 2| | |
* +---+ ACCT +---+-- | ----->+---+
* | |---------->| W | 4,5 -------->| F |
* +---+ +---+------------->+---+
*
* Of course in case of using a proxy this get really nasty and is not
* standardized at all :-(
*/
if (proxy) {
int len;
char buf[400];
if (proxyUser != NULL) {
/*
* We need proxy auth
*/
snprintf(buf, sizeof(buf), "USER %s\r\n", proxyUser);
buf[sizeof(buf) - 1] = 0;
len = strlen(buf);
res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
closesocket(ctxt->controlFd);
ctxt->controlFd = INVALID_SOCKET;
return(res);
}
res = xmlNanoFTPGetResponse(ctxt);
switch (res) {
case 2:
if (proxyPasswd == NULL)
break;
/* Falls through. */
case 3:
if (proxyPasswd != NULL)
snprintf(buf, sizeof(buf), "PASS %s\r\n", proxyPasswd);
else
snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
buf[sizeof(buf) - 1] = 0;
len = strlen(buf);
res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
closesocket(ctxt->controlFd);
ctxt->controlFd = INVALID_SOCKET;
return(res);
}
res = xmlNanoFTPGetResponse(ctxt);
if (res > 3) {
closesocket(ctxt->controlFd);
ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
break;
case 1:
break;
case 4:
case 5:
case -1:
default:
closesocket(ctxt->controlFd);
ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
}
/*
* We assume we don't need more authentication to the proxy
* and that it succeeded :-\
*/
switch (proxyType) {
case 0:
/* we will try in sequence */
case 1:
/* Using SITE command */