1 /* Internal "ftp" protocol implementation */
12 #include <sys/stat.h> /* For converting permissions to strings */
13 #include <sys/types.h>
14 #ifdef HAVE_SYS_SOCKET_H
15 #include <sys/socket.h>
21 #include <fcntl.h> /* OS/2 needs this after sys/types.h */
24 /* We need to have it here. Stupid BSD. */
25 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
28 #ifdef HAVE_ARPA_INET_H
29 #include <arpa/inet.h>
34 #include "cache/cache.h"
35 #include "config/options.h"
36 #include "intl/gettext/libintl.h"
37 #include "main/select.h"
38 #include "main/module.h"
39 #include "network/connection.h"
40 #include "network/progress.h"
41 #include "network/socket.h"
42 #include "osdep/osdep.h"
43 #include "osdep/stat.h"
44 #include "protocol/auth/auth.h"
45 #include "protocol/common.h"
46 #include "protocol/ftp/ftp.h"
47 #include "protocol/ftp/parse.h"
48 #include "protocol/uri.h"
49 #include "util/conv.h"
50 #include "util/error.h"
51 #include "util/memory.h"
52 #include "util/string.h"
55 union option_info ftp_options
[] = {
56 INIT_OPT_TREE("protocol", N_("FTP"),
58 N_("FTP specific options.")),
60 INIT_OPT_TREE("protocol.ftp", N_("Proxy configuration"),
62 N_("FTP proxy configuration.")),
64 INIT_OPT_STRING("protocol.ftp.proxy", N_("Host and port-number"),
66 N_("Host and port-number (host:port) of the FTP proxy, "
67 "or blank. If it's blank, FTP_PROXY environment variable "
68 "is checked as well.")),
70 INIT_OPT_STRING("protocol.ftp", N_("Anonymous password"),
71 "anon_passwd", 0, "some@host.domain",
72 N_("FTP anonymous password to be sent.")),
74 INIT_OPT_BOOL("protocol.ftp", N_("Use passive mode (IPv4)"),
76 N_("Use PASV instead of PORT (passive vs active mode, "
79 INIT_OPT_BOOL("protocol.ftp", N_("Use passive mode (IPv6)"),
81 N_("Use EPSV instead of EPRT (passive vs active mode, "
83 #endif /* CONFIG_IPV6 */
88 struct module ftp_protocol_module
= struct_module(
89 /* name: */ N_("FTP"),
90 /* options: */ ftp_options
,
92 /* submodules: */ NULL
,
101 #define FTP_BUF_SIZE 16384
104 /* Types and structs */
106 struct ftp_connection_info
{
107 int pending_commands
; /* Num of commands queued */
108 int opc
; /* Total num of commands queued */
112 unsigned int dir
:1; /* Directory listing in progress */
113 unsigned int rest_sent
:1; /* Sent RESTore command */
114 unsigned int use_pasv
:1; /* Use PASV (yes or no) */
116 unsigned int use_epsv
:1; /* Use EPSV */
118 unsigned char ftp_buffer
[FTP_BUF_SIZE
];
119 unsigned char cmd_buffer
[1]; /* Must be last field !! */
124 static void ftp_login(struct socket
*);
125 static void ftp_send_retr_req(struct connection
*, struct connection_state
);
126 static void ftp_got_info(struct socket
*, struct read_buffer
*);
127 static void ftp_got_user_info(struct socket
*, struct read_buffer
*);
128 static void ftp_pass(struct connection
*);
129 static void ftp_pass_info(struct socket
*, struct read_buffer
*);
130 static void ftp_retr_file(struct socket
*, struct read_buffer
*);
131 static void ftp_got_final_response(struct socket
*, struct read_buffer
*);
132 static void got_something_from_data_connection(struct connection
*);
133 static void ftp_end_request(struct connection
*, struct connection_state
);
134 static struct ftp_connection_info
*add_file_cmd_to_str(struct connection
*);
135 static void ftp_data_accept(struct connection
*conn
);
137 /* Parse EPSV or PASV response for address and/or port.
138 * int *n should point to a sizeof(int) * 6 space.
139 * It returns zero on error or count of parsed numbers.
140 * It returns an error if:
141 * - there's more than 6 or less than 1 numbers.
142 * - a number is strictly greater than max.
144 * On success, array of integers *n is filled with numbers starting
145 * from end of array (ie. if we found one number, you can access it using
149 * Negative numbers aren't handled so -123 is taken as 123.
150 * We don't take care about separators.
153 parse_psv_resp(unsigned char *data
, int *n
, int max_value
)
155 unsigned char *p
= data
;
158 memset(n
, 0, 6 * sizeof(*n
));
160 if (*p
< ' ') return 0;
163 while (*p
>= ' ') p
++;
165 /* Ignore non-numeric ending chars. */
166 while (p
!= data
&& !isdigit(*p
)) p
--;
167 if (p
== data
) return 0;
172 /* Parse one number. */
173 while (p
!= data
&& isdigit(*p
)) {
174 n
[i
] += (*p
- '0') * x
;
175 if (n
[i
] > max_value
) return 0;
179 /* Ignore non-numeric chars. */
180 while (p
!= data
&& !isdigit(*p
)) p
--;
181 if (p
== data
) return (6 - i
);
182 /* Get the next one. */
189 /* Returns 0 if there's no numeric response, -1 if error, the positive response
190 * number otherwise. */
192 get_ftp_response(struct connection
*conn
, struct read_buffer
*rb
, int part
,
193 struct sockaddr_storage
*sa
)
196 unsigned char *num_end
;
201 eol
= memchr(rb
->data
, ASCII_LF
, rb
->length
);
204 pos
= eol
- rb
->data
;
207 response
= strtoul(rb
->data
, (char **) &num_end
, 10);
209 if (errno
|| num_end
!= rb
->data
+ 3 || response
< 100)
212 if (sa
&& response
== 227) { /* PASV response parsing. */
213 struct sockaddr_in
*s
= (struct sockaddr_in
*) sa
;
216 if (parse_psv_resp(num_end
, (int *) &n
, 255) != 6)
219 memset(s
, 0, sizeof(*s
));
220 s
->sin_family
= AF_INET
;
221 s
->sin_addr
.s_addr
= htonl((n
[0] << 24) + (n
[1] << 16) + (n
[2] << 8) + n
[3]);
222 s
->sin_port
= htons((n
[4] << 8) + n
[5]);
226 if (sa
&& response
== 229) { /* EPSV response parsing. */
228 struct sockaddr_in6
*s
= (struct sockaddr_in6
*) sa
;
229 int sal
= sizeof(*s
);
232 if (parse_psv_resp(num_end
, (int *) &n
, 65535) != 1) {
236 memset(s
, 0, sizeof(*s
));
237 if (getpeername(conn
->socket
->fd
, (struct sockaddr
*) sa
, &sal
)) {
240 s
->sin6_family
= AF_INET6
;
241 s
->sin6_port
= htons(n
[5]);
245 if (*num_end
== '-') {
248 for (i
= 0; i
< rb
->length
- 5; i
++)
249 if (rb
->data
[i
] == ASCII_LF
250 && !memcmp(rb
->data
+i
+1, rb
->data
, 3)
251 && rb
->data
[i
+4] == ' ') {
252 for (i
++; i
< rb
->length
; i
++)
253 if (rb
->data
[i
] == ASCII_LF
)
263 kill_buffer_data(rb
, pos
+ 1);
265 if (!part
&& response
>= 100 && response
< 200) {
273 /* Initialize or continue ftp connection. */
275 ftp_protocol_handler(struct connection
*conn
)
277 if (!has_keepalive_connection(conn
)) {
278 make_connection(conn
->socket
, conn
->uri
, ftp_login
,
279 conn
->cache_mode
>= CACHE_MODE_FORCE_RELOAD
);
282 ftp_send_retr_req(conn
, connection_state(S_SENT
));
286 /* Send command, set connection state and free cmd string. */
288 send_cmd(struct connection
*conn
, struct string
*cmd
, void *callback
,
289 struct connection_state state
)
291 request_from_socket(conn
->socket
, cmd
->source
, cmd
->length
, state
,
292 SOCKET_RETRY_ONCLOSE
, callback
);
297 /* Check if this auth token really belongs to this URI. */
299 auth_user_matching_uri(struct auth_entry
*auth
, struct uri
*uri
)
301 if (!uri
->userlen
) /* Noone said it doesn't. */
303 return !c_strlcasecmp(auth
->user
, -1, uri
->user
, uri
->userlen
);
307 /* Kill the current connection and ask for a username/password for the next
310 prompt_username_pw(struct connection
*conn
)
313 conn
->cached
= get_cache_entry(conn
->uri
);
315 abort_connection(conn
, connection_state(S_OUT_OF_MEM
));
320 mem_free_set(&conn
->cached
->content_type
, stracpy("text/html"));
321 if (!conn
->cached
->content_type
) {
322 abort_connection(conn
, connection_state(S_OUT_OF_MEM
));
326 add_auth_entry(conn
->uri
, "FTP Login", NULL
, NULL
, 0);
328 abort_connection(conn
, connection_state(S_OK
));
331 /* Send USER command. */
333 ftp_login(struct socket
*socket
)
335 struct connection
*conn
= socket
->conn
;
337 struct auth_entry
* auth
;
339 auth
= find_auth(conn
->uri
);
341 if (!init_string(&cmd
)) {
342 abort_connection(conn
, connection_state(S_OUT_OF_MEM
));
346 add_to_string(&cmd
, "USER ");
347 if (conn
->uri
->userlen
) {
348 struct uri
*uri
= conn
->uri
;
350 add_bytes_to_string(&cmd
, uri
->user
, uri
->userlen
);
352 } else if (auth
&& auth
->valid
) {
353 add_to_string(&cmd
, auth
->user
);
356 add_to_string(&cmd
, "anonymous");
358 add_crlf_to_string(&cmd
);
360 send_cmd(conn
, &cmd
, (void *) ftp_got_info
, connection_state(S_SENT
));
363 /* Parse connection response. */
365 ftp_got_info(struct socket
*socket
, struct read_buffer
*rb
)
367 struct connection
*conn
= socket
->conn
;
368 int response
= get_ftp_response(conn
, rb
, 0, NULL
);
370 if (response
== -1) {
371 abort_connection(conn
, connection_state(S_FTP_ERROR
));
376 read_from_socket(conn
->socket
, rb
, conn
->state
, ftp_got_info
);
380 /* RFC959 says that possible response codes on connection are:
381 * 120 Service ready in nnn minutes.
382 * 220 Service ready for new user.
383 * 421 Service not available, closing control connection. */
385 if (response
!= 220) {
386 /* TODO? Retry in case of ... ?? */
387 retry_connection(conn
, connection_state(S_FTP_UNAVAIL
));
391 ftp_got_user_info(socket
, rb
);
395 /* Parse USER response and send PASS command if needed. */
397 ftp_got_user_info(struct socket
*socket
, struct read_buffer
*rb
)
399 struct connection
*conn
= socket
->conn
;
400 int response
= get_ftp_response(conn
, rb
, 0, NULL
);
402 if (response
== -1) {
403 abort_connection(conn
, connection_state(S_FTP_ERROR
));
408 read_from_socket(conn
->socket
, rb
, conn
->state
, ftp_got_user_info
);
412 /* RFC959 says that possible response codes for USER are:
413 * 230 User logged in, proceed.
414 * 331 User name okay, need password.
415 * 332 Need account for login.
416 * 421 Service not available, closing control connection.
417 * 500 Syntax error, command unrecognized.
418 * 501 Syntax error in parameters or arguments.
419 * 530 Not logged in. */
421 /* FIXME? Since ACCT command isn't implemented, login to a ftp server
422 * requiring it will fail (332). */
424 if (response
== 332 || response
>= 500) {
425 prompt_username_pw(conn
);
429 /* We don't require exact match here, as this is always error and some
430 * non-RFC compliant servers may return even something other than 421.
432 if (response
>= 400) {
433 abort_connection(conn
, connection_state(S_FTP_UNAVAIL
));
437 if (response
== 230) {
438 ftp_send_retr_req(conn
, connection_state(S_GETH
));
445 /* Send PASS command. */
447 ftp_pass(struct connection
*conn
)
450 struct auth_entry
*auth
;
452 auth
= find_auth(conn
->uri
);
454 if (!init_string(&cmd
)) {
455 abort_connection(conn
, connection_state(S_OUT_OF_MEM
));
459 add_to_string(&cmd
, "PASS ");
460 if (conn
->uri
->passwordlen
) {
461 struct uri
*uri
= conn
->uri
;
463 add_bytes_to_string(&cmd
, uri
->password
, uri
->passwordlen
);
465 } else if (auth
&& auth
->valid
) {
466 if (!auth_user_matching_uri(auth
, conn
->uri
)) {
467 prompt_username_pw(conn
);
470 add_to_string(&cmd
, auth
->password
);
473 add_to_string(&cmd
, get_opt_str("protocol.ftp.anon_passwd"));
475 add_crlf_to_string(&cmd
);
477 send_cmd(conn
, &cmd
, (void *) ftp_pass_info
, connection_state(S_LOGIN
));
480 /* Parse PASS command response. */
482 ftp_pass_info(struct socket
*socket
, struct read_buffer
*rb
)
484 struct connection
*conn
= socket
->conn
;
485 int response
= get_ftp_response(conn
, rb
, 0, NULL
);
487 if (response
== -1) {
488 abort_connection(conn
, connection_state(S_FTP_ERROR
));
493 read_from_socket(conn
->socket
, rb
, connection_state(S_LOGIN
),
498 /* RFC959 says that possible response codes for PASS are:
499 * 202 Command not implemented, superfluous at this site.
500 * 230 User logged in, proceed.
501 * 332 Need account for login.
502 * 421 Service not available, closing control connection.
503 * 500 Syntax error, command unrecognized.
504 * 501 Syntax error in parameters or arguments.
505 * 503 Bad sequence of commands.
506 * 530 Not logged in. */
508 if (response
== 332 || response
>= 500) {
509 /* If we didn't have a user, we tried anonymous. But it failed, so ask for a
510 * user and password */
511 prompt_username_pw(conn
);
515 if (response
>= 400) {
516 abort_connection(conn
, connection_state(S_FTP_UNAVAIL
));
520 ftp_send_retr_req(conn
, connection_state(S_GETH
));
523 /* Construct PORT command. */
525 add_portcmd_to_string(struct string
*string
, unsigned char *pc
)
527 /* From RFC 959: DATA PORT (PORT)
529 * The argument is a HOST-PORT specification for the data port
530 * to be used in data connection. There are defaults for both
531 * the user and server data ports, and under normal
532 * circumstances this command and its reply are not needed. If
533 * this command is used, the argument is the concatenation of a
534 * 32-bit internet host address and a 16-bit TCP port address.
535 * This address information is broken into 8-bit fields and the
536 * value of each field is transmitted as a decimal number (in
537 * character string representation). The fields are separated
538 * by commas. A port command would be:
540 * PORT h1,h2,h3,h4,p1,p2
542 * where h1 is the high order 8 bits of the internet host
544 add_to_string(string
, "PORT ");
545 add_long_to_string(string
, pc
[0]);
546 add_char_to_string(string
, ',');
547 add_long_to_string(string
, pc
[1]);
548 add_char_to_string(string
, ',');
549 add_long_to_string(string
, pc
[2]);
550 add_char_to_string(string
, ',');
551 add_long_to_string(string
, pc
[3]);
552 add_char_to_string(string
, ',');
553 add_long_to_string(string
, pc
[4]);
554 add_char_to_string(string
, ',');
555 add_long_to_string(string
, pc
[5]);
559 /* Construct EPRT command. */
561 add_eprtcmd_to_string(struct string
*string
, struct sockaddr_in6
*addr
)
563 unsigned char addr_str
[INET6_ADDRSTRLEN
];
565 inet_ntop(AF_INET6
, &addr
->sin6_addr
, addr_str
, INET6_ADDRSTRLEN
);
567 /* From RFC 2428: EPRT
569 * The format of EPRT is:
571 * EPRT<space><d><net-prt><d><net-addr><d><tcp-port><d>
576 * 1 Internet Protocol, Version 4 [Pos81a]
577 * 2 Internet Protocol, Version 6 [DH96] */
578 add_to_string(string
, "EPRT |2|");
579 add_to_string(string
, addr_str
);
580 add_char_to_string(string
, '|');
581 add_long_to_string(string
, ntohs(addr
->sin6_port
));
582 add_char_to_string(string
, '|');
586 /* Depending on options, get proper ftp data socket and command.
587 * It appends ftp command (either PASV,PORT,EPSV or EPRT) to @command
589 * When PORT or EPRT are used, related sockets are created.
590 * It returns 0 on error (data socket creation failure). */
592 get_ftp_data_socket(struct connection
*conn
, struct string
*command
)
594 struct ftp_connection_info
*ftp
= conn
->info
;
596 ftp
->use_pasv
= get_opt_bool("protocol.ftp.use_pasv");
599 ftp
->use_epsv
= get_opt_bool("protocol.ftp.use_epsv");
601 if (conn
->socket
->protocol_family
== EL_PF_INET6
) {
603 add_to_string(command
, "EPSV");
606 struct sockaddr_storage data_addr
;
609 memset(&data_addr
, 0, sizeof(data_addr
));
610 data_sock
= get_pasv_socket(conn
->socket
, &data_addr
);
611 if (data_sock
< 0) return 0;
613 conn
->data_socket
->fd
= data_sock
;
614 add_eprtcmd_to_string(command
,
615 (struct sockaddr_in6
*) &data_addr
);
622 add_to_string(command
, "PASV");
625 struct sockaddr_in sa
;
629 memset(pc
, 0, sizeof(pc
));
630 data_sock
= get_pasv_socket(conn
->socket
,
631 (struct sockaddr_storage
*) &sa
);
632 if (data_sock
< 0) return 0;
634 memcpy(pc
, &sa
.sin_addr
.s_addr
, 4);
635 memcpy(pc
+ 4, &sa
.sin_port
, 2);
636 conn
->data_socket
->fd
= data_sock
;
637 add_portcmd_to_string(command
, pc
);
641 add_crlf_to_string(command
);
646 /* Check if the file or directory name @s can be safely sent to the
647 * FTP server. To prevent command injection attacks, this function
648 * must reject CR LF sequences. */
650 is_ftp_pathname_safe(const struct string
*s
)
654 /* RFC 959 says the argument of CWD and RETR is a <pathname>,
655 * which consists of <char>s, "any of the 128 ASCII characters
656 * except <CR> and <LF>". So other control characters, such
657 * as 0x00 and 0x7F, are allowed here. Bytes 0x80...0xFF
658 * should not be allowed, but if we reject them, users will
659 * probably complain. */
660 for (i
= 0; i
< s
->length
; i
++) {
661 if (s
->source
[i
] == 0x0A || s
->source
[i
] == 0x0D)
667 /* Create passive socket and add appropriate announcing commands to str. Then
668 * go and retrieve appropriate object from server.
669 * Returns NULL if error. */
670 static struct ftp_connection_info
*
671 add_file_cmd_to_str(struct connection
*conn
)
674 struct ftp_connection_info
*ftp
= NULL
;
675 struct string command
= NULL_STRING
;
676 struct string ftp_data_command
= NULL_STRING
;
677 struct string pathname
= NULL_STRING
;
679 if (!conn
->uri
->data
) {
680 INTERNAL("conn->uri->data empty");
681 abort_connection(conn
, connection_state(S_INTERNAL
));
685 /* This will be reallocated below when we know how long the
686 * command string should be. Error handling could be
687 * simplified a little by allocating this initial structure on
688 * the stack, but it's several kilobytes long so that might be
690 ftp
= mem_calloc(1, sizeof(*ftp
));
692 abort_connection(conn
, connection_state(S_OUT_OF_MEM
));
696 conn
->info
= ftp
; /* Freed when connection is destroyed. */
698 if (!init_string(&command
)
699 || !init_string(&ftp_data_command
)
700 || !init_string(&pathname
)) {
701 abort_connection(conn
, connection_state(S_OUT_OF_MEM
));
705 if (!get_ftp_data_socket(conn
, &ftp_data_command
)) {
706 INTERNAL("Ftp data socket failure");
707 abort_connection(conn
, connection_state(S_INTERNAL
));
711 if (!add_uri_to_string(&pathname
, conn
->uri
, URI_PATH
)) {
712 abort_connection(conn
, connection_state(S_OUT_OF_MEM
));
716 decode_uri_string(&pathname
);
717 if (!is_ftp_pathname_safe(&pathname
)) {
718 abort_connection(conn
, connection_state(S_BAD_URL
));
722 if (!conn
->uri
->datalen
723 || conn
->uri
->data
[conn
->uri
->datalen
- 1] == '/') {
724 /* Commands to get directory listing. */
727 ftp
->pending_commands
= 4;
729 if (!add_to_string(&command
, "TYPE A") /* ASCII */
730 || !add_crlf_to_string(&command
)
732 || !add_string_to_string(&command
, &ftp_data_command
)
734 || !add_to_string(&command
, "CWD ")
735 || !add_string_to_string(&command
, &pathname
)
736 || !add_crlf_to_string(&command
)
738 || !add_to_string(&command
, "LIST")
739 || !add_crlf_to_string(&command
)) {
740 abort_connection(conn
, connection_state(S_OUT_OF_MEM
));
747 /* Commands to get a file. */
750 ftp
->pending_commands
= 3;
752 if (!add_to_string(&command
, "TYPE I") /* BINARY */
753 || !add_crlf_to_string(&command
)
755 || !add_string_to_string(&command
, &ftp_data_command
)) {
756 abort_connection(conn
, connection_state(S_OUT_OF_MEM
));
760 if (conn
->from
|| conn
->progress
->start
> 0) {
761 const off_t offset
= conn
->from
763 : conn
->progress
->start
;
765 if (!add_to_string(&command
, "REST ")
766 || !add_long_to_string(&command
, offset
)
767 || !add_crlf_to_string(&command
)) {
768 abort_connection(conn
, connection_state(S_OUT_OF_MEM
));
773 ftp
->pending_commands
++;
776 if (!add_to_string(&command
, "RETR ")
777 || !add_string_to_string(&command
, &pathname
)
778 || !add_crlf_to_string(&command
)) {
779 abort_connection(conn
, connection_state(S_OUT_OF_MEM
));
784 ftp
->opc
= ftp
->pending_commands
;
786 /* 1 byte is already reserved for cmd_buffer in struct ftp_connection_info. */
787 ftp
= mem_realloc(ftp
, sizeof(*ftp
) + command
.length
);
789 abort_connection(conn
, connection_state(S_OUT_OF_MEM
));
792 conn
->info
= ftp
; /* in case mem_realloc moved the buffer */
794 memcpy(ftp
->cmd_buffer
, command
.source
, command
.length
+ 1);
798 /* If @ok is false here, then abort_connection has already
799 * freed @ftp, which now is a dangling pointer. */
800 done_string(&pathname
);
801 done_string(&ftp_data_command
);
802 done_string(&command
);
803 return ok
? ftp
: NULL
;
807 send_it_line_by_line(struct connection
*conn
, struct string
*cmd
)
809 struct ftp_connection_info
*ftp
= conn
->info
;
810 unsigned char *nl
= strchr(ftp
->cmd_buffer
, '\n');
813 add_to_string(cmd
, ftp
->cmd_buffer
);
818 add_bytes_to_string(cmd
, ftp
->cmd_buffer
, nl
- ftp
->cmd_buffer
);
819 memmove(ftp
->cmd_buffer
, nl
, strlen(nl
) + 1);
822 /* Send commands to retrieve file or directory. */
824 ftp_send_retr_req(struct connection
*conn
, struct connection_state state
)
828 if (!init_string(&cmd
)) {
829 abort_connection(conn
, connection_state(S_OUT_OF_MEM
));
833 /* We don't save return value from add_file_cmd_to_str(), as it's saved
834 * in conn->info as well. */
835 if (!conn
->info
&& !add_file_cmd_to_str(conn
)) {
840 /* Send it line-by-line. */
841 send_it_line_by_line(conn
, &cmd
);
843 send_cmd(conn
, &cmd
, (void *) ftp_retr_file
, state
);
846 /* Parse RETR response and return file size or -1 on error. */
848 get_filesize_from_RETR(unsigned char *data
, int data_len
, int *resume
)
852 int pos_file_len
= 0;
854 /* Getting file size from text response.. */
855 /* 150 Opening BINARY mode data connection for hello-1.0-1.1.diff.gz (16452 bytes). */
858 for (pos
= 0; pos
< data_len
&& data
[pos
] != ASCII_LF
; pos
++)
859 if (data
[pos
] == '(')
862 if (!pos_file_len
|| pos_file_len
== data_len
- 1) {
863 /* Getting file size from ftp.task.gda.pl */
864 /* 150 5676.4 kbytes to download */
865 unsigned char tmp
= data
[data_len
- 1];
866 unsigned char *kbytes
;
870 data
[data_len
- 1] = '\0';
871 kbytes
= strstr(data
, "kbytes");
872 data
[data_len
- 1] = tmp
;
873 if (!kbytes
) return -1;
875 for (kbytes
-= 2; kbytes
>= data
; kbytes
--) {
876 if (*kbytes
== ' ') break;
878 if (*kbytes
!= ' ') return -1;
880 size
= strtod((const char *)kbytes
, &endptr
);
881 if (endptr
== (char *)kbytes
) return -1;
883 return (off_t
)(size
* 1024.0);
887 if (!isdigit(data
[pos_file_len
]))
890 for (pos
= pos_file_len
; pos
< data_len
; pos
++)
891 if (!isdigit(data
[pos
]))
897 for (; pos
< data_len
; pos
++)
898 if (data
[pos
] != ' ')
901 if (pos
+ 4 > data_len
)
904 if (c_strncasecmp(&data
[pos
], "byte", 4))
908 file_len
= (off_t
) strtol(&data
[pos_file_len
], NULL
, 10);
909 if (errno
) return -1;
914 /* Connect to the host and port specified by a passive FTP server. */
916 ftp_data_connect(struct connection
*conn
, int pf
, struct sockaddr_storage
*sa
,
917 int size_of_sockaddr
)
921 if (conn
->data_socket
->fd
!= -1) {
922 /* The server maliciously sent multiple 227 or 229
923 * responses. Do not leak the previous data_socket. */
924 abort_connection(conn
, connection_state(S_FTP_ERROR
));
928 fd
= socket(pf
, SOCK_STREAM
, 0);
929 if (fd
< 0 || set_nonblocking_fd(fd
) < 0) {
930 abort_connection(conn
, connection_state(S_FTP_ERROR
));
934 set_ip_tos_throughput(fd
);
936 conn
->data_socket
->fd
= fd
;
937 /* XXX: We ignore connect() errors here. */
938 connect(fd
, (struct sockaddr
*) sa
, size_of_sockaddr
);
943 ftp_retr_file(struct socket
*socket
, struct read_buffer
*rb
)
945 struct connection
*conn
= socket
->conn
;
946 struct ftp_connection_info
*ftp
= conn
->info
;
949 if (ftp
->pending_commands
> 1) {
950 struct sockaddr_storage sa
;
952 response
= get_ftp_response(conn
, rb
, 0, &sa
);
954 if (response
== -1) {
955 abort_connection(conn
, connection_state(S_FTP_ERROR
));
960 read_from_socket(conn
->socket
, rb
,
961 connection_state(S_GETH
),
966 if (response
== 227) {
967 if (ftp_data_connect(conn
, PF_INET
, &sa
, sizeof(struct sockaddr_in
)))
972 if (response
== 229) {
973 if (ftp_data_connect(conn
, PF_INET6
, &sa
, sizeof(struct sockaddr_in6
)))
978 ftp
->pending_commands
--;
980 /* XXX: The case values are order numbers of commands. */
981 switch (ftp
->opc
- ftp
->pending_commands
) {
986 if (response
>= 400) {
987 abort_connection(conn
,
988 connection_state(S_FTP_PORT
));
993 case 3: /* REST / CWD */
994 if (response
>= 400) {
996 abort_connection(conn
,
997 connection_state(S_FTP_NO_FILE
));
1001 } else if (ftp
->rest_sent
) {
1002 /* Following code is related to resume
1004 if (response
== 350)
1005 conn
->from
= conn
->progress
->start
;
1006 /* Come on, don't be nervous ;-). */
1007 if (conn
->progress
->start
>= 0) {
1008 /* Update to the real value
1009 * which we've got from
1011 conn
->progress
->seek
= conn
->from
;
1013 conn
->progress
->start
= conn
->from
;
1018 INTERNAL("WHAT???");
1021 ftp_send_retr_req(conn
, connection_state(S_GETH
));
1025 response
= get_ftp_response(conn
, rb
, 2, NULL
);
1027 if (response
== -1) {
1028 abort_connection(conn
, connection_state(S_FTP_ERROR
));
1033 read_from_socket(conn
->socket
, rb
, connection_state(S_GETH
),
1038 if (response
>= 100 && response
< 200) {
1039 /* We only need to parse response after RETR to
1040 * get filesize if needed. */
1041 if (!ftp
->dir
&& conn
->est_length
== -1) {
1045 file_len
= get_filesize_from_RETR(rb
->data
, rb
->length
, &res
);
1047 /* FIXME: ..when downloads resuming
1049 /* This is right for vsftpd.
1050 * Show me urls where this is wrong. --witekfl */
1051 conn
->est_length
= res
?
1052 file_len
+ conn
->progress
->start
: file_len
;
1057 if (conn
->data_socket
->fd
== -1) {
1058 /* The passive FTP server did not send a 227 or 229
1059 * response. We check this down here, rather than
1060 * immediately after getting the response to the PASV
1061 * or EPSV command, to make sure that nothing can
1062 * close the socket between the check and the
1063 * following set_handlers call.
1065 * If we were using active FTP, then
1066 * get_ftp_data_socket would have created the
1067 * data_socket without waiting for anything from the
1069 abort_connection(conn
, connection_state(S_FTP_ERROR
));
1072 set_handlers(conn
->data_socket
->fd
, (select_handler_T
) ftp_data_accept
,
1075 /* read_from_socket(conn->socket, rb, ftp_got_final_response); */
1076 ftp_got_final_response(socket
, rb
);
1080 ftp_got_final_response(struct socket
*socket
, struct read_buffer
*rb
)
1082 struct connection
*conn
= socket
->conn
;
1083 struct ftp_connection_info
*ftp
= conn
->info
;
1084 int response
= get_ftp_response(conn
, rb
, 0, NULL
);
1086 if (response
== -1) {
1087 abort_connection(conn
, connection_state(S_FTP_ERROR
));
1092 struct connection_state state
= !is_in_state(conn
->state
, S_TRANS
)
1093 ? connection_state(S_GETH
) : conn
->state
;
1095 read_from_socket(conn
->socket
, rb
, state
, ftp_got_final_response
);
1099 if (response
>= 550 || response
== 450) {
1100 /* Requested action not taken.
1101 * File unavailable (e.g., file not found, no access). */
1104 conn
->cached
= get_cache_entry(conn
->uri
);
1107 || !redirect_cache(conn
->cached
, "/", 1, 0)) {
1108 abort_connection(conn
, connection_state(S_OUT_OF_MEM
));
1112 abort_connection(conn
, connection_state(S_OK
));
1116 if (response
>= 400) {
1117 abort_connection(conn
, connection_state(S_FTP_FILE_ERROR
));
1121 if (ftp
->conn_state
== 2) {
1122 ftp_end_request(conn
, connection_state(S_OK
));
1124 ftp
->conn_state
= 1;
1125 if (!is_in_state(conn
->state
, S_TRANS
))
1126 set_connection_state(conn
, connection_state(S_GETH
));
1131 /** How to format an FTP directory listing in HTML. */
1132 struct ftp_dir_html_format
{
1133 /** Codepage used by C library functions such as strftime().
1134 * If the FTP server sends non-ASCII bytes in file names or
1135 * such, ELinks normally passes them straight through to the
1136 * generated HTML, which will eventually be parsed using the
1137 * codepage specified in the document.codepage.assume option.
1138 * However, when ELinks itself generates strings with
1139 * strftime(), it turns non-ASCII bytes into entity references
1140 * based on libc_codepage, to make sure they become the right
1141 * characters again. */
1144 /** Nonzero if directories should be displayed in a different
1145 * color. From the document.browse.links.color_dirs option. */
1148 /** The color of directories, in "#rrggbb" format. This is
1149 * initialized and used only if colorize_dir is nonzero. */
1150 unsigned char dircolor
[8];
1153 /* Display directory entry formatted in HTML. */
1155 display_dir_entry(struct cache_entry
*cached
, off_t
*pos
, int *tries
,
1156 const struct ftp_dir_html_format
*format
,
1157 struct ftp_file_info
*ftp_info
)
1159 struct string string
;
1160 unsigned char permissions
[10] = "---------";
1162 if (!init_string(&string
)) return -1;
1164 add_char_to_string(&string
, ftp_info
->type
);
1166 if (ftp_info
->permissions
) {
1167 mode_t p
= ftp_info
->permissions
;
1169 #define FTP_PERM(perms, buffer, flag, index, id) \
1170 if ((perms) & (flag)) (buffer)[(index)] = (id);
1172 FTP_PERM(p
, permissions
, S_IRUSR
, 0, 'r');
1173 FTP_PERM(p
, permissions
, S_IWUSR
, 1, 'w');
1174 FTP_PERM(p
, permissions
, S_IXUSR
, 2, 'x');
1175 FTP_PERM(p
, permissions
, S_ISUID
, 2, (p
& S_IXUSR
? 's' : 'S'));
1177 FTP_PERM(p
, permissions
, S_IRGRP
, 3, 'r');
1178 FTP_PERM(p
, permissions
, S_IWGRP
, 4, 'w');
1179 FTP_PERM(p
, permissions
, S_IXGRP
, 5, 'x');
1180 FTP_PERM(p
, permissions
, S_ISGID
, 5, (p
& S_IXGRP
? 's' : 'S'));
1182 FTP_PERM(p
, permissions
, S_IROTH
, 6, 'r');
1183 FTP_PERM(p
, permissions
, S_IWOTH
, 7, 'w');
1184 FTP_PERM(p
, permissions
, S_IXOTH
, 8, 'x');
1185 FTP_PERM(p
, permissions
, S_ISVTX
, 8, (p
& 0001 ? 't' : 'T'));
1191 add_to_string(&string
, permissions
);
1192 add_char_to_string(&string
, ' ');
1194 add_to_string(&string
, " 1 ftp ftp ");
1196 if (ftp_info
->size
!= FTP_SIZE_UNKNOWN
) {
1197 add_format_to_string(&string
, "%12" OFF_PRINT_FORMAT
" ",
1198 (off_print_T
) ftp_info
->size
);
1200 add_to_string(&string
, " - ");
1203 #ifdef HAVE_STRFTIME
1204 if (ftp_info
->mtime
> 0) {
1205 time_t current_time
= time(NULL
);
1206 time_t when
= ftp_info
->mtime
;
1209 /* LC_TIME=fi_FI.UTF_8 can generate "elo___ 31 23:59"
1210 * where each _ denotes U+00A0 encoded as 0xC2 0xA0,
1211 * thus needing a 19-byte buffer. */
1212 unsigned char date
[80];
1215 if (ftp_info
->local_time_zone
)
1216 when_tm
= localtime(&when
);
1218 when_tm
= gmtime(&when
);
1220 if (current_time
> when
+ 6L * 30L * 24L * 60L * 60L
1221 || current_time
< when
- 60L * 60L)
1224 fmt
= "%b %e %H:%M";
1226 wr
= strftime(date
, sizeof(date
), fmt
, when_tm
);
1227 add_cp_html_to_string(&string
, format
->libc_codepage
,
1231 add_to_string(&string
, " ");
1232 /* TODO: Above, the number of spaces might not match the width
1233 * of the string generated by strftime. It depends on the
1234 * locale. So if ELinks knows the timestamps of some FTP
1235 * files but not others, it may misalign the file names.
1236 * Potential solutions:
1237 * - Pad the strings to a compile-time fixed width.
1238 * Con: If we choose a width that suffices for all known
1239 * locales, then it will be stupidly large for most of them.
1240 * - Generate an HTML table.
1241 * Con: Bloats the HTML source.
1242 * Any solution chosen here should also be applied to the
1243 * file: protocol handler. */
1245 add_char_to_string(&string
, ' ');
1247 if (ftp_info
->type
== FTP_FILE_DIRECTORY
&& format
->colorize_dir
) {
1248 add_to_string(&string
, "<font color=\"");
1249 add_to_string(&string
, format
->dircolor
);
1250 add_to_string(&string
, "\"><b>");
1253 add_to_string(&string
, "<a href=\"");
1254 encode_uri_string(&string
, ftp_info
->name
.source
, ftp_info
->name
.length
, 0);
1255 if (ftp_info
->type
== FTP_FILE_DIRECTORY
)
1256 add_char_to_string(&string
, '/');
1257 add_to_string(&string
, "\">");
1258 add_html_to_string(&string
, ftp_info
->name
.source
, ftp_info
->name
.length
);
1259 add_to_string(&string
, "</a>");
1261 if (ftp_info
->type
== FTP_FILE_DIRECTORY
&& format
->colorize_dir
) {
1262 add_to_string(&string
, "</b></font>");
1265 if (ftp_info
->symlink
.length
) {
1266 add_to_string(&string
, " -> ");
1267 add_html_to_string(&string
, ftp_info
->symlink
.source
,
1268 ftp_info
->symlink
.length
);
1271 add_char_to_string(&string
, '\n');
1273 if (add_fragment(cached
, *pos
, string
.source
, string
.length
)) *tries
= 0;
1274 *pos
+= string
.length
;
1276 done_string(&string
);
1280 /* Get the next line of input and set *@len to the length of the line.
1281 * Return the number of newline characters at the end of the line or -1
1282 * if we must wait for more input. */
1284 ftp_get_line(struct cache_entry
*cached
, unsigned char *buf
, int bufl
,
1287 unsigned char *newline
;
1289 if (!bufl
) return -1;
1291 newline
= memchr(buf
, ASCII_LF
, bufl
);
1294 *len
= newline
- buf
;
1295 if (*len
&& buf
[*len
- 1] == ASCII_CR
) {
1303 if (last
|| bufl
>= FTP_BUF_SIZE
) {
1311 /** Generate HTML for a line that was received from the FTP server but
1312 * could not be parsed. The caller is supposed to have added a \<pre>
1313 * start tag. (At the time of writing, init_directory_listing() was
1316 * @return -1 if out of memory, or 0 if successful. */
1318 ftp_add_unparsed_line(struct cache_entry
*cached
, off_t
*pos
, int *tries
,
1319 const unsigned char *line
, int line_length
)
1322 struct string string
;
1325 our_ret
= -1; /* assume out of memory if returning early */
1326 if (!init_string(&string
)) goto out
;
1327 if (!add_html_to_string(&string
, line
, line_length
)) goto out
;
1328 if (!add_char_to_string(&string
, '\n')) goto out
;
1330 frag_ret
= add_fragment(cached
, *pos
, string
.source
, string
.length
);
1331 if (frag_ret
== -1) goto out
;
1332 *pos
+= string
.length
;
1333 if (frag_ret
== 1) *tries
= 0;
1335 our_ret
= 0; /* success */
1338 done_string(&string
); /* safe even if init_string failed */
1342 /** List a directory in html format.
1344 * @return the number of bytes used from the beginning of @a buffer,
1345 * or -1 if out of memory. */
1347 ftp_process_dirlist(struct cache_entry
*cached
, off_t
*pos
,
1348 unsigned char *buffer
, int buflen
, int last
,
1349 int *tries
, const struct ftp_dir_html_format
*format
)
1354 struct ftp_file_info ftp_info
= INIT_FTP_FILE_INFO
;
1355 unsigned char *buf
= buffer
+ ret
;
1356 int bufl
= buflen
- ret
;
1357 int line_length
, eol
;
1359 eol
= ftp_get_line(cached
, buf
, bufl
, last
, &line_length
);
1363 ret
+= line_length
+ eol
;
1365 /* Process line whose end we've already found. */
1367 if (parse_ftp_file_info(&ftp_info
, buf
, line_length
)) {
1370 if (ftp_info
.name
.source
[0] == '.'
1371 && (ftp_info
.name
.length
== 1
1372 || (ftp_info
.name
.length
== 2
1373 && ftp_info
.name
.source
[1] == '.')))
1376 retv
= display_dir_entry(cached
, pos
, tries
,
1383 int retv
= ftp_add_unparsed_line(cached
, pos
, tries
,
1386 if (retv
== -1) /* out of memory; propagate to caller */
1392 /* This is the initial read handler for conn->data_socket->fd,
1393 * which may be either trying to connect to a passive FTP server or
1394 * listening for a connection from an active FTP server. In active
1395 * FTP, this function then accepts the connection and replaces
1396 * conn->data_socket->fd with the resulting socket. In any case,
1397 * this function does not read any data from the FTP server, but
1398 * rather hands the socket over to got_something_from_data_connection,
1399 * which then does the reads. */
1401 ftp_data_accept(struct connection
*conn
)
1403 struct ftp_connection_info
*ftp
= conn
->info
;
1406 /* Because this function is called only as a read handler of
1407 * conn->data_socket->fd, the socket must be valid if we get
1409 assert(conn
->data_socket
->fd
>= 0);
1410 if_assert_failed
return;
1412 set_connection_timeout(conn
);
1413 clear_handlers(conn
->data_socket
->fd
);
1415 if ((conn
->socket
->protocol_family
!= EL_PF_INET6
&& ftp
->use_pasv
)
1417 || (conn
->socket
->protocol_family
== EL_PF_INET6
&& ftp
->use_epsv
)
1420 newsock
= conn
->data_socket
->fd
;
1422 newsock
= accept(conn
->data_socket
->fd
, NULL
, NULL
);
1424 retry_connection(conn
, connection_state_for_errno(errno
));
1427 close(conn
->data_socket
->fd
);
1430 conn
->data_socket
->fd
= newsock
;
1432 set_handlers(newsock
,
1433 (select_handler_T
) got_something_from_data_connection
,
1437 /* A read handler for conn->data_socket->fd. This function reads
1438 * data from the FTP server, reformats it to HTML if it's a directory
1439 * listing, and adds the result to the cache entry. */
1441 got_something_from_data_connection(struct connection
*conn
)
1443 struct ftp_connection_info
*ftp
= conn
->info
;
1444 struct ftp_dir_html_format format
;
1447 /* Because this function is called only as a read handler of
1448 * conn->data_socket->fd, the socket must be valid if we get
1450 assert(conn
->data_socket
->fd
>= 0);
1451 if_assert_failed
return;
1453 /* XXX: This probably belongs rather to connect.c ? */
1455 set_connection_timeout(conn
);
1457 if (!conn
->cached
) conn
->cached
= get_cache_entry(conn
->uri
);
1458 if (!conn
->cached
) {
1460 abort_connection(conn
, connection_state(S_OUT_OF_MEM
));
1465 format
.libc_codepage
= get_cp_index("System");
1467 format
.colorize_dir
= get_opt_bool("document.browse.links.color_dirs");
1469 if (format
.colorize_dir
) {
1470 color_to_string(get_opt_color("document.colors.dirs"),
1475 if (ftp
->dir
&& !conn
->from
) {
1476 struct string string
;
1477 struct connection_state state
;
1479 if (!conn
->uri
->data
) {
1480 abort_connection(conn
, connection_state(S_FTP_ERROR
));
1484 state
= init_directory_listing(&string
, conn
->uri
);
1485 if (!is_in_state(state
, S_OK
)) {
1486 abort_connection(conn
, state
);
1490 add_fragment(conn
->cached
, conn
->from
, string
.source
, string
.length
);
1491 conn
->from
+= string
.length
;
1493 done_string(&string
);
1495 if (conn
->uri
->datalen
) {
1496 struct ftp_file_info ftp_info
= INIT_FTP_FILE_INFO_ROOT
;
1498 display_dir_entry(conn
->cached
, &conn
->from
, &conn
->tries
,
1499 &format
, &ftp_info
);
1502 mem_free_set(&conn
->cached
->content_type
, stracpy("text/html"));
1505 len
= safe_read(conn
->data_socket
->fd
, ftp
->ftp_buffer
+ ftp
->buf_pos
,
1506 FTP_BUF_SIZE
- ftp
->buf_pos
);
1508 retry_connection(conn
, connection_state_for_errno(errno
));
1513 conn
->received
+= len
;
1516 if (add_fragment(conn
->cached
, conn
->from
,
1517 ftp
->ftp_buffer
, len
) == 1)
1524 proceeded
= ftp_process_dirlist(conn
->cached
,
1531 if (proceeded
== -1) goto out_of_mem
;
1533 ftp
->buf_pos
+= len
- proceeded
;
1535 memmove(ftp
->ftp_buffer
, ftp
->ftp_buffer
+ proceeded
,
1540 set_connection_state(conn
, connection_state(S_TRANS
));
1544 if (ftp_process_dirlist(conn
->cached
, &conn
->from
,
1545 ftp
->ftp_buffer
, ftp
->buf_pos
, 1,
1546 &conn
->tries
, &format
) == -1)
1549 #define ADD_CONST(str) { \
1550 add_fragment(conn->cached, conn->from, str, sizeof(str) - 1); \
1551 conn->from += (sizeof(str) - 1); }
1553 if (ftp
->dir
) ADD_CONST("</pre>\n<hr/>\n</body>\n</html>");
1555 close_socket(conn
->data_socket
);
1557 if (ftp
->conn_state
== 1) {
1558 ftp_end_request(conn
, connection_state(S_OK
));
1560 ftp
->conn_state
= 2;
1561 set_connection_state(conn
, connection_state(S_TRANS
));
1566 ftp_end_request(struct connection
*conn
, struct connection_state state
)
1568 if (is_in_state(state
, S_OK
) && conn
->cached
) {
1569 normalize_cache_entry(conn
->cached
, conn
->from
);
1572 set_connection_state(conn
, state
);
1573 add_keepalive_connection(conn
, FTP_KEEPALIVE_TIMEOUT
, NULL
);