strtoll instead of strtol to show the filesize of big files in FTP
[elinks.git] / src / protocol / ftp / ftp.c
blob4e1713108a563be8b0a1016fdc96fd67fd9b45f1
1 /* Internal "ftp" protocol implementation */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <stdio.h>
8 #include <ctype.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <string.h>
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>
16 #endif
17 #ifdef HAVE_UNISTD_H
18 #include <unistd.h>
19 #endif
20 #ifdef HAVE_FCNTL_H
21 #include <fcntl.h> /* OS/2 needs this after sys/types.h */
22 #endif
24 /* We need to have it here. Stupid BSD. */
25 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
27 #endif
28 #ifdef HAVE_ARPA_INET_H
29 #include <arpa/inet.h>
30 #endif
32 #include "elinks.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 struct option_info ftp_options[] = {
56 INIT_OPT_TREE("protocol", N_("FTP"),
57 "ftp", 0,
58 N_("FTP specific options.")),
60 INIT_OPT_TREE("protocol.ftp", N_("Proxy configuration"),
61 "proxy", 0,
62 N_("FTP proxy configuration.")),
64 INIT_OPT_STRING("protocol.ftp.proxy", N_("Host and port-number"),
65 "host", 0, "",
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)"),
75 "use_pasv", 0, 1,
76 N_("Use PASV instead of PORT (passive vs active mode, "
77 "IPv4 only).")),
78 #ifdef CONFIG_IPV6
79 INIT_OPT_BOOL("protocol.ftp", N_("Use passive mode (IPv6)"),
80 "use_epsv", 0, 0,
81 N_("Use EPSV instead of EPRT (passive vs active mode, "
82 "IPv6 only).")),
83 #endif /* CONFIG_IPV6 */
84 NULL_OPTION_INFO,
88 struct module ftp_protocol_module = struct_module(
89 /* name: */ N_("FTP"),
90 /* options: */ ftp_options,
91 /* hooks: */ NULL,
92 /* submodules: */ NULL,
93 /* data: */ NULL,
94 /* init: */ NULL,
95 /* done: */ NULL
99 /* Constants */
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 */
109 int conn_state;
110 int buf_pos;
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) */
115 #ifdef CONFIG_IPV6
116 unsigned int use_epsv:1; /* Use EPSV */
117 #endif
118 unsigned char ftp_buffer[FTP_BUF_SIZE];
119 unsigned char cmd_buffer[1]; /* Must be last field !! */
123 /* Prototypes */
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
146 * n[5]).
148 * Important:
149 * Negative numbers aren't handled so -123 is taken as 123.
150 * We don't take care about separators.
152 static int
153 parse_psv_resp(unsigned char *data, int *n, int max_value)
155 unsigned char *p = data;
156 int i = 5;
158 memset(n, 0, 6 * sizeof(*n));
160 if (*p < ' ') return 0;
162 /* Find the end. */
163 while (*p >= ' ') p++;
165 /* Ignore non-numeric ending chars. */
166 while (p != data && !isdigit(*p)) p--;
167 if (p == data) return 0;
169 while (i >= 0) {
170 int x = 1;
172 /* Parse one number. */
173 while (p != data && isdigit(*p)) {
174 n[i] += (*p - '0') * x;
175 if (n[i] > max_value) return 0;
176 x *= 10;
177 p--;
179 /* Ignore non-numeric chars. */
180 while (p != data && !isdigit(*p)) p--;
181 if (p == data) return (6 - i);
182 /* Get the next one. */
183 i--;
186 return 0;
189 /* Returns 0 if there's no numeric response, -1 if error, the positive response
190 * number otherwise. */
191 static int
192 get_ftp_response(struct connection *conn, struct read_buffer *rb, int part,
193 struct sockaddr_storage *sa)
195 unsigned char *eol;
196 unsigned char *num_end;
197 int response;
198 int pos;
200 again:
201 eol = memchr(rb->data, ASCII_LF, rb->length);
202 if (!eol) return 0;
204 pos = eol - rb->data;
206 errno = 0;
207 response = strtoul(rb->data, (char **) &num_end, 10);
209 if (errno || num_end != rb->data + 3 || response < 100)
210 return -1;
212 if (sa && response == 227) { /* PASV response parsing. */
213 struct sockaddr_in *s = (struct sockaddr_in *) sa;
214 int n[6];
216 if (parse_psv_resp(num_end, (int *) &n, 255) != 6)
217 return -1;
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]);
225 #ifdef CONFIG_IPV6
226 if (sa && response == 229) { /* EPSV response parsing. */
227 /* See RFC 2428 */
228 struct sockaddr_in6 *s = (struct sockaddr_in6 *) sa;
229 int sal = sizeof(*s);
230 int n[6];
232 if (parse_psv_resp(num_end, (int *) &n, 65535) != 1) {
233 return -1;
236 memset(s, 0, sizeof(*s));
237 if (getpeername(conn->socket->fd, (struct sockaddr *) sa, &sal)) {
238 return -1;
240 s->sin6_family = AF_INET6;
241 s->sin6_port = htons(n[5]);
243 #endif
245 if (*num_end == '-') {
246 int i;
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)
254 goto ok;
255 return 0;
257 return 0;
259 pos = i;
262 if (part != 2)
263 kill_buffer_data(rb, pos + 1);
265 if (!part && response >= 100 && response < 200) {
266 goto again;
269 return response;
273 /* Initialize or continue ftp connection. */
274 void
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);
281 } else {
282 ftp_send_retr_req(conn, connection_state(S_SENT));
286 /* Send command, set connection state and free cmd string. */
287 static void
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);
294 done_string(cmd);
297 /* Check if this auth token really belongs to this URI. */
298 static int
299 auth_user_matching_uri(struct auth_entry *auth, struct uri *uri)
301 if (!uri->userlen) /* Noone said it doesn't. */
302 return 1;
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
308 * try. */
309 static void
310 prompt_username_pw(struct connection *conn)
312 if (!conn->cached) {
313 conn->cached = get_cache_entry(conn->uri);
314 if (!conn->cached) {
315 abort_connection(conn, connection_state(S_OUT_OF_MEM));
316 return;
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));
323 return;
326 add_auth_entry(conn->uri, "FTP Login", NULL, NULL, 0);
328 abort_connection(conn, connection_state(S_OK));
331 /* Send USER command. */
332 static void
333 ftp_login(struct socket *socket)
335 struct connection *conn = socket->conn;
336 struct string cmd;
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));
343 return;
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);
355 } else {
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. */
364 static void
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));
372 return;
375 if (!response) {
376 read_from_socket(conn->socket, rb, conn->state, ftp_got_info);
377 return;
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));
388 return;
391 ftp_got_user_info(socket, rb);
395 /* Parse USER response and send PASS command if needed. */
396 static void
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));
404 return;
407 if (!response) {
408 read_from_socket(conn->socket, rb, conn->state, ftp_got_user_info);
409 return;
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);
426 return;
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.
431 * --Zas */
432 if (response >= 400) {
433 abort_connection(conn, connection_state(S_FTP_UNAVAIL));
434 return;
437 if (response == 230) {
438 ftp_send_retr_req(conn, connection_state(S_GETH));
439 return;
442 ftp_pass(conn);
445 /* Send PASS command. */
446 static void
447 ftp_pass(struct connection *conn)
449 struct string cmd;
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));
456 return;
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);
468 return;
470 add_to_string(&cmd, auth->password);
472 } else {
473 add_to_string(&cmd, get_opt_str("protocol.ftp.anon_passwd",
474 NULL));
476 add_crlf_to_string(&cmd);
478 send_cmd(conn, &cmd, (void *) ftp_pass_info, connection_state(S_LOGIN));
481 /* Parse PASS command response. */
482 static void
483 ftp_pass_info(struct socket *socket, struct read_buffer *rb)
485 struct connection *conn = socket->conn;
486 int response = get_ftp_response(conn, rb, 0, NULL);
488 if (response == -1) {
489 abort_connection(conn, connection_state(S_FTP_ERROR));
490 return;
493 if (!response) {
494 read_from_socket(conn->socket, rb, connection_state(S_LOGIN),
495 ftp_pass_info);
496 return;
499 /* RFC959 says that possible response codes for PASS are:
500 * 202 Command not implemented, superfluous at this site.
501 * 230 User logged in, proceed.
502 * 332 Need account for login.
503 * 421 Service not available, closing control connection.
504 * 500 Syntax error, command unrecognized.
505 * 501 Syntax error in parameters or arguments.
506 * 503 Bad sequence of commands.
507 * 530 Not logged in. */
509 if (response == 332 || response >= 500) {
510 /* If we didn't have a user, we tried anonymous. But it failed, so ask for a
511 * user and password */
512 prompt_username_pw(conn);
513 return;
516 if (response >= 400) {
517 abort_connection(conn, connection_state(S_FTP_UNAVAIL));
518 return;
521 ftp_send_retr_req(conn, connection_state(S_GETH));
524 /* Construct PORT command. */
525 static void
526 add_portcmd_to_string(struct string *string, unsigned char *pc)
528 /* From RFC 959: DATA PORT (PORT)
530 * The argument is a HOST-PORT specification for the data port
531 * to be used in data connection. There are defaults for both
532 * the user and server data ports, and under normal
533 * circumstances this command and its reply are not needed. If
534 * this command is used, the argument is the concatenation of a
535 * 32-bit internet host address and a 16-bit TCP port address.
536 * This address information is broken into 8-bit fields and the
537 * value of each field is transmitted as a decimal number (in
538 * character string representation). The fields are separated
539 * by commas. A port command would be:
541 * PORT h1,h2,h3,h4,p1,p2
543 * where h1 is the high order 8 bits of the internet host
544 * address. */
545 add_to_string(string, "PORT ");
546 add_long_to_string(string, pc[0]);
547 add_char_to_string(string, ',');
548 add_long_to_string(string, pc[1]);
549 add_char_to_string(string, ',');
550 add_long_to_string(string, pc[2]);
551 add_char_to_string(string, ',');
552 add_long_to_string(string, pc[3]);
553 add_char_to_string(string, ',');
554 add_long_to_string(string, pc[4]);
555 add_char_to_string(string, ',');
556 add_long_to_string(string, pc[5]);
559 #ifdef CONFIG_IPV6
560 /* Construct EPRT command. */
561 static void
562 add_eprtcmd_to_string(struct string *string, struct sockaddr_in6 *addr)
564 unsigned char addr_str[INET6_ADDRSTRLEN];
566 inet_ntop(AF_INET6, &addr->sin6_addr, addr_str, INET6_ADDRSTRLEN);
568 /* From RFC 2428: EPRT
570 * The format of EPRT is:
572 * EPRT<space><d><net-prt><d><net-addr><d><tcp-port><d>
574 * <net-prt>:
575 * AF Number Protocol
576 * --------- --------
577 * 1 Internet Protocol, Version 4 [Pos81a]
578 * 2 Internet Protocol, Version 6 [DH96] */
579 add_to_string(string, "EPRT |2|");
580 add_to_string(string, addr_str);
581 add_char_to_string(string, '|');
582 add_long_to_string(string, ntohs(addr->sin6_port));
583 add_char_to_string(string, '|');
585 #endif
587 /* Depending on options, get proper ftp data socket and command.
588 * It appends ftp command (either PASV,PORT,EPSV or EPRT) to @command
589 * string.
590 * When PORT or EPRT are used, related sockets are created.
591 * It returns 0 on error (data socket creation failure). */
592 static int
593 get_ftp_data_socket(struct connection *conn, struct string *command)
595 struct ftp_connection_info *ftp = conn->info;
597 ftp->use_pasv = get_opt_bool("protocol.ftp.use_pasv", NULL);
599 #ifdef CONFIG_IPV6
600 ftp->use_epsv = get_opt_bool("protocol.ftp.use_epsv", NULL);
602 if (conn->socket->protocol_family == EL_PF_INET6) {
603 if (ftp->use_epsv) {
604 add_to_string(command, "EPSV");
606 } else {
607 struct sockaddr_storage data_addr;
608 int data_sock;
610 memset(&data_addr, 0, sizeof(data_addr));
611 data_sock = get_pasv_socket(conn->socket, &data_addr);
612 if (data_sock < 0) return 0;
614 conn->data_socket->fd = data_sock;
615 add_eprtcmd_to_string(command,
616 (struct sockaddr_in6 *) &data_addr);
619 } else
620 #endif
622 if (ftp->use_pasv) {
623 add_to_string(command, "PASV");
625 } else {
626 struct sockaddr_in sa;
627 unsigned char pc[6];
628 int data_sock;
630 memset(pc, 0, sizeof(pc));
631 data_sock = get_pasv_socket(conn->socket,
632 (struct sockaddr_storage *) &sa);
633 if (data_sock < 0) return 0;
635 memcpy(pc, &sa.sin_addr.s_addr, 4);
636 memcpy(pc + 4, &sa.sin_port, 2);
637 conn->data_socket->fd = data_sock;
638 add_portcmd_to_string(command, pc);
642 add_crlf_to_string(command);
644 return 1;
647 /* Check if the file or directory name @s can be safely sent to the
648 * FTP server. To prevent command injection attacks, this function
649 * must reject CR LF sequences. */
650 static int
651 is_ftp_pathname_safe(const struct string *s)
653 int i;
655 /* RFC 959 says the argument of CWD and RETR is a <pathname>,
656 * which consists of <char>s, "any of the 128 ASCII characters
657 * except <CR> and <LF>". So other control characters, such
658 * as 0x00 and 0x7F, are allowed here. Bytes 0x80...0xFF
659 * should not be allowed, but if we reject them, users will
660 * probably complain. */
661 for (i = 0; i < s->length; i++) {
662 if (s->source[i] == 0x0A || s->source[i] == 0x0D)
663 return 0;
665 return 1;
668 /* Create passive socket and add appropriate announcing commands to str. Then
669 * go and retrieve appropriate object from server.
670 * Returns NULL if error. */
671 static struct ftp_connection_info *
672 add_file_cmd_to_str(struct connection *conn)
674 int ok = 0;
675 struct ftp_connection_info *ftp = NULL;
676 struct string command = NULL_STRING;
677 struct string ftp_data_command = NULL_STRING;
678 struct string pathname = NULL_STRING;
680 if (!conn->uri->data) {
681 INTERNAL("conn->uri->data empty");
682 abort_connection(conn, connection_state(S_INTERNAL));
683 goto ret;
686 assert(conn->info == NULL);
687 assert(conn->done == NULL);
688 if_assert_failed {
689 abort_connection(conn, connection_state(S_INTERNAL));
690 goto ret;
693 /* This will be reallocated below when we know how long the
694 * command string should be. Error handling could be
695 * simplified a little by allocating this initial structure on
696 * the stack, but it's several kilobytes long so that might be
697 * risky. */
698 ftp = mem_calloc(1, sizeof(*ftp));
699 if (!ftp) {
700 abort_connection(conn, connection_state(S_OUT_OF_MEM));
701 goto ret;
704 /* conn->info and conn->done were asserted as NULL above. */
705 conn->info = ftp; /* Freed when connection is destroyed. */
707 if (!init_string(&command)
708 || !init_string(&ftp_data_command)
709 || !init_string(&pathname)) {
710 abort_connection(conn, connection_state(S_OUT_OF_MEM));
711 goto ret;
714 if (!get_ftp_data_socket(conn, &ftp_data_command)) {
715 INTERNAL("Ftp data socket failure");
716 abort_connection(conn, connection_state(S_INTERNAL));
717 goto ret;
720 if (!add_uri_to_string(&pathname, conn->uri, URI_PATH)) {
721 abort_connection(conn, connection_state(S_OUT_OF_MEM));
722 goto ret;
725 decode_uri_string(&pathname);
726 if (!is_ftp_pathname_safe(&pathname)) {
727 abort_connection(conn, connection_state(S_BAD_URL));
728 goto ret;
731 if (!conn->uri->datalen
732 || conn->uri->data[conn->uri->datalen - 1] == '/') {
733 /* Commands to get directory listing. */
735 ftp->dir = 1;
736 ftp->pending_commands = 4;
738 if (!add_to_string(&command, "TYPE A") /* ASCII */
739 || !add_crlf_to_string(&command)
741 || !add_string_to_string(&command, &ftp_data_command)
743 || !add_to_string(&command, "CWD ")
744 || !add_string_to_string(&command, &pathname)
745 || !add_crlf_to_string(&command)
747 || !add_to_string(&command, "LIST")
748 || !add_crlf_to_string(&command)) {
749 abort_connection(conn, connection_state(S_OUT_OF_MEM));
750 goto ret;
753 conn->from = 0;
755 } else {
756 /* Commands to get a file. */
758 ftp->dir = 0;
759 ftp->pending_commands = 3;
761 if (!add_to_string(&command, "TYPE I") /* BINARY */
762 || !add_crlf_to_string(&command)
764 || !add_string_to_string(&command, &ftp_data_command)) {
765 abort_connection(conn, connection_state(S_OUT_OF_MEM));
766 goto ret;
769 if (conn->from || conn->progress->start > 0) {
770 const off_t offset = conn->from
771 ? conn->from
772 : conn->progress->start;
774 if (!add_to_string(&command, "REST ")
775 || !add_long_to_string(&command, offset)
776 || !add_crlf_to_string(&command)) {
777 abort_connection(conn, connection_state(S_OUT_OF_MEM));
778 goto ret;
781 ftp->rest_sent = 1;
782 ftp->pending_commands++;
785 if (!add_to_string(&command, "RETR ")
786 || !add_string_to_string(&command, &pathname)
787 || !add_crlf_to_string(&command)) {
788 abort_connection(conn, connection_state(S_OUT_OF_MEM));
789 goto ret;
793 ftp->opc = ftp->pending_commands;
795 /* 1 byte is already reserved for cmd_buffer in struct ftp_connection_info. */
796 ftp = mem_realloc(ftp, sizeof(*ftp) + command.length);
797 if (!ftp) {
798 abort_connection(conn, connection_state(S_OUT_OF_MEM));
799 goto ret;
801 conn->info = ftp; /* in case mem_realloc moved the buffer */
803 memcpy(ftp->cmd_buffer, command.source, command.length + 1);
804 ok = 1;
806 ret:
807 /* If @ok is false here, then abort_connection has already
808 * freed @ftp, which now is a dangling pointer. */
809 done_string(&pathname);
810 done_string(&ftp_data_command);
811 done_string(&command);
812 return ok ? ftp : NULL;
815 static void
816 send_it_line_by_line(struct connection *conn, struct string *cmd)
818 struct ftp_connection_info *ftp = conn->info;
819 unsigned char *nl = strchr(ftp->cmd_buffer, '\n');
821 if (!nl) {
822 add_to_string(cmd, ftp->cmd_buffer);
823 return;
826 nl++;
827 add_bytes_to_string(cmd, ftp->cmd_buffer, nl - ftp->cmd_buffer);
828 memmove(ftp->cmd_buffer, nl, strlen(nl) + 1);
831 /* Send commands to retrieve file or directory. */
832 static void
833 ftp_send_retr_req(struct connection *conn, struct connection_state state)
835 struct string cmd;
837 if (!init_string(&cmd)) {
838 abort_connection(conn, connection_state(S_OUT_OF_MEM));
839 return;
842 /* We don't save return value from add_file_cmd_to_str(), as it's saved
843 * in conn->info as well. */
844 if (!conn->info && !add_file_cmd_to_str(conn)) {
845 done_string(&cmd);
846 return;
849 /* Send it line-by-line. */
850 send_it_line_by_line(conn, &cmd);
852 send_cmd(conn, &cmd, (void *) ftp_retr_file, state);
855 /* Parse RETR response and return file size or -1 on error. */
856 static off_t
857 get_filesize_from_RETR(unsigned char *data, int data_len, int *resume)
859 off_t file_len;
860 int pos;
861 int pos_file_len = 0;
863 /* Getting file size from text response.. */
864 /* 150 Opening BINARY mode data connection for hello-1.0-1.1.diff.gz (16452 bytes). */
866 *resume = 0;
867 for (pos = 0; pos < data_len && data[pos] != ASCII_LF; pos++)
868 if (data[pos] == '(')
869 pos_file_len = pos;
871 if (!pos_file_len || pos_file_len == data_len - 1) {
872 /* Getting file size from ftp.task.gda.pl */
873 /* 150 5676.4 kbytes to download */
874 unsigned char tmp = data[data_len - 1];
875 unsigned char *kbytes;
876 char *endptr;
877 double size;
879 data[data_len - 1] = '\0';
880 kbytes = strstr(data, "kbytes");
881 data[data_len - 1] = tmp;
882 if (!kbytes) return -1;
884 for (kbytes -= 2; kbytes >= data; kbytes--) {
885 if (*kbytes == ' ') break;
887 if (*kbytes != ' ') return -1;
888 kbytes++;
889 size = strtod((const char *)kbytes, &endptr);
890 if (endptr == (char *)kbytes) return -1;
891 *resume = 1;
892 return (off_t)(size * 1024.0);
895 pos_file_len++;
896 if (!isdigit(data[pos_file_len]))
897 return -1;
899 for (pos = pos_file_len; pos < data_len; pos++)
900 if (!isdigit(data[pos]))
901 goto next;
903 return -1;
905 next:
906 for (; pos < data_len; pos++)
907 if (data[pos] != ' ')
908 break;
910 if (pos + 4 > data_len)
911 return -1;
913 if (c_strncasecmp(&data[pos], "byte", 4))
914 return -1;
916 errno = 0;
917 file_len = (off_t) strtoll(&data[pos_file_len], NULL, 10);
918 if (errno) return -1;
920 return file_len;
923 /* Connect to the host and port specified by a passive FTP server. */
924 static int
925 ftp_data_connect(struct connection *conn, int pf, struct sockaddr_storage *sa,
926 int size_of_sockaddr)
928 int fd;
930 if (conn->data_socket->fd != -1) {
931 /* The server maliciously sent multiple 227 or 229
932 * responses. Do not leak the previous data_socket. */
933 abort_connection(conn, connection_state(S_FTP_ERROR));
934 return -1;
937 fd = socket(pf, SOCK_STREAM, 0);
938 if (fd < 0 || set_nonblocking_fd(fd) < 0) {
939 abort_connection(conn, connection_state(S_FTP_ERROR));
940 return -1;
943 set_ip_tos_throughput(fd);
945 conn->data_socket->fd = fd;
946 /* XXX: We ignore connect() errors here. */
947 connect(fd, (struct sockaddr *) sa, size_of_sockaddr);
948 return 0;
951 static void
952 ftp_retr_file(struct socket *socket, struct read_buffer *rb)
954 struct connection *conn = socket->conn;
955 struct ftp_connection_info *ftp = conn->info;
956 int response;
958 if (ftp->pending_commands > 1) {
959 struct sockaddr_storage sa;
961 response = get_ftp_response(conn, rb, 0, &sa);
963 if (response == -1) {
964 abort_connection(conn, connection_state(S_FTP_ERROR));
965 return;
968 if (!response) {
969 read_from_socket(conn->socket, rb,
970 connection_state(S_GETH),
971 ftp_retr_file);
972 return;
975 if (response == 227) {
976 if (ftp_data_connect(conn, PF_INET, &sa, sizeof(struct sockaddr_in)))
977 return;
980 #ifdef CONFIG_IPV6
981 if (response == 229) {
982 if (ftp_data_connect(conn, PF_INET6, &sa, sizeof(struct sockaddr_in6)))
983 return;
985 #endif
987 ftp->pending_commands--;
989 /* XXX: The case values are order numbers of commands. */
990 switch (ftp->opc - ftp->pending_commands) {
991 case 1: /* TYPE */
992 break;
994 case 2: /* PORT */
995 if (response >= 400) {
996 abort_connection(conn,
997 connection_state(S_FTP_PORT));
998 return;
1000 break;
1002 case 3: /* REST / CWD */
1003 if (response >= 400) {
1004 if (ftp->dir) {
1005 abort_connection(conn,
1006 connection_state(S_FTP_NO_FILE));
1007 return;
1009 conn->from = 0;
1010 } else if (ftp->rest_sent) {
1011 /* Following code is related to resume
1012 * feature. */
1013 if (response == 350)
1014 conn->from = conn->progress->start;
1015 /* Come on, don't be nervous ;-). */
1016 if (conn->progress->start >= 0) {
1017 /* Update to the real value
1018 * which we've got from
1019 * Content-Range. */
1020 conn->progress->seek = conn->from;
1022 conn->progress->start = conn->from;
1024 break;
1026 default:
1027 INTERNAL("WHAT???");
1030 ftp_send_retr_req(conn, connection_state(S_GETH));
1031 return;
1034 response = get_ftp_response(conn, rb, 2, NULL);
1036 if (response == -1) {
1037 abort_connection(conn, connection_state(S_FTP_ERROR));
1038 return;
1041 if (!response) {
1042 read_from_socket(conn->socket, rb, connection_state(S_GETH),
1043 ftp_retr_file);
1044 return;
1047 if (response >= 100 && response < 200) {
1048 /* We only need to parse response after RETR to
1049 * get filesize if needed. */
1050 if (!ftp->dir && conn->est_length == -1) {
1051 off_t file_len;
1052 int res;
1054 file_len = get_filesize_from_RETR(rb->data, rb->length, &res);
1055 if (file_len > 0) {
1056 /* FIXME: ..when downloads resuming
1057 * implemented.. */
1058 /* This is right for vsftpd.
1059 * Show me urls where this is wrong. --witekfl */
1060 conn->est_length = res ?
1061 file_len + conn->progress->start : file_len;
1066 if (conn->data_socket->fd == -1) {
1067 /* The passive FTP server did not send a 227 or 229
1068 * response. We check this down here, rather than
1069 * immediately after getting the response to the PASV
1070 * or EPSV command, to make sure that nothing can
1071 * close the socket between the check and the
1072 * following set_handlers call.
1074 * If we were using active FTP, then
1075 * get_ftp_data_socket would have created the
1076 * data_socket without waiting for anything from the
1077 * server. */
1078 abort_connection(conn, connection_state(S_FTP_ERROR));
1079 return;
1081 set_handlers(conn->data_socket->fd, (select_handler_T) ftp_data_accept,
1082 NULL, NULL, conn);
1084 /* read_from_socket(conn->socket, rb, ftp_got_final_response); */
1085 ftp_got_final_response(socket, rb);
1088 static void
1089 ftp_got_final_response(struct socket *socket, struct read_buffer *rb)
1091 struct connection *conn = socket->conn;
1092 struct ftp_connection_info *ftp = conn->info;
1093 int response = get_ftp_response(conn, rb, 0, NULL);
1095 if (response == -1) {
1096 abort_connection(conn, connection_state(S_FTP_ERROR));
1097 return;
1100 if (!response) {
1101 struct connection_state state = !is_in_state(conn->state, S_TRANS)
1102 ? connection_state(S_GETH) : conn->state;
1104 read_from_socket(conn->socket, rb, state, ftp_got_final_response);
1105 return;
1108 if (response >= 550 || response == 450) {
1109 /* Requested action not taken.
1110 * File unavailable (e.g., file not found, no access). */
1112 if (!conn->cached)
1113 conn->cached = get_cache_entry(conn->uri);
1115 if (!conn->cached
1116 || !redirect_cache(conn->cached, "/", 1, 0)) {
1117 abort_connection(conn, connection_state(S_OUT_OF_MEM));
1118 return;
1121 abort_connection(conn, connection_state(S_OK));
1122 return;
1125 if (response >= 400) {
1126 abort_connection(conn, connection_state(S_FTP_FILE_ERROR));
1127 return;
1130 if (ftp->conn_state == 2) {
1131 ftp_end_request(conn, connection_state(S_OK));
1132 } else {
1133 ftp->conn_state = 1;
1134 if (!is_in_state(conn->state, S_TRANS))
1135 set_connection_state(conn, connection_state(S_GETH));
1140 /** How to format an FTP directory listing in HTML. */
1141 struct ftp_dir_html_format {
1142 /** Codepage used by C library functions such as strftime().
1143 * If the FTP server sends non-ASCII bytes in file names or
1144 * such, ELinks normally passes them straight through to the
1145 * generated HTML, which will eventually be parsed using the
1146 * codepage specified in the document.codepage.assume option.
1147 * However, when ELinks itself generates strings with
1148 * strftime(), it turns non-ASCII bytes into entity references
1149 * based on libc_codepage, to make sure they become the right
1150 * characters again. */
1151 int libc_codepage;
1153 /** Nonzero if directories should be displayed in a different
1154 * color. From the document.browse.links.color_dirs option. */
1155 int colorize_dir;
1157 /** The color of directories, in "#rrggbb" format. This is
1158 * initialized and used only if colorize_dir is nonzero. */
1159 unsigned char dircolor[8];
1162 /* Display directory entry formatted in HTML. */
1163 static int
1164 display_dir_entry(struct cache_entry *cached, off_t *pos, int *tries,
1165 const struct ftp_dir_html_format *format,
1166 struct ftp_file_info *ftp_info)
1168 struct string string;
1169 unsigned char permissions[10] = "---------";
1171 if (!init_string(&string)) return -1;
1173 add_char_to_string(&string, ftp_info->type);
1175 if (ftp_info->permissions) {
1176 mode_t p = ftp_info->permissions;
1178 #define FTP_PERM(perms, buffer, flag, index, id) \
1179 if ((perms) & (flag)) (buffer)[(index)] = (id);
1181 FTP_PERM(p, permissions, S_IRUSR, 0, 'r');
1182 FTP_PERM(p, permissions, S_IWUSR, 1, 'w');
1183 FTP_PERM(p, permissions, S_IXUSR, 2, 'x');
1184 FTP_PERM(p, permissions, S_ISUID, 2, (p & S_IXUSR ? 's' : 'S'));
1186 FTP_PERM(p, permissions, S_IRGRP, 3, 'r');
1187 FTP_PERM(p, permissions, S_IWGRP, 4, 'w');
1188 FTP_PERM(p, permissions, S_IXGRP, 5, 'x');
1189 FTP_PERM(p, permissions, S_ISGID, 5, (p & S_IXGRP ? 's' : 'S'));
1191 FTP_PERM(p, permissions, S_IROTH, 6, 'r');
1192 FTP_PERM(p, permissions, S_IWOTH, 7, 'w');
1193 FTP_PERM(p, permissions, S_IXOTH, 8, 'x');
1194 FTP_PERM(p, permissions, S_ISVTX, 8, (p & 0001 ? 't' : 'T'));
1196 #undef FTP_PERM
1200 add_to_string(&string, permissions);
1201 add_char_to_string(&string, ' ');
1203 add_to_string(&string, " 1 ftp ftp ");
1205 if (ftp_info->size != FTP_SIZE_UNKNOWN) {
1206 add_format_to_string(&string, "%12" OFF_PRINT_FORMAT " ",
1207 (off_print_T) ftp_info->size);
1208 } else {
1209 add_to_string(&string, " - ");
1212 #ifdef HAVE_STRFTIME
1213 if (ftp_info->mtime > 0) {
1214 time_t current_time = time(NULL);
1215 time_t when = ftp_info->mtime;
1216 struct tm *when_tm;
1217 unsigned char *fmt;
1218 /* LC_TIME=fi_FI.UTF_8 can generate "elo___ 31 23:59"
1219 * where each _ denotes U+00A0 encoded as 0xC2 0xA0,
1220 * thus needing a 19-byte buffer. */
1221 unsigned char date[80];
1222 int wr;
1224 if (ftp_info->local_time_zone)
1225 when_tm = localtime(&when);
1226 else
1227 when_tm = gmtime(&when);
1229 if (current_time > when + 6L * 30L * 24L * 60L * 60L
1230 || current_time < when - 60L * 60L)
1231 fmt = "%b %e %Y";
1232 else
1233 fmt = "%b %e %H:%M";
1235 wr = strftime(date, sizeof(date), fmt, when_tm);
1236 add_cp_html_to_string(&string, format->libc_codepage,
1237 date, wr);
1238 } else
1239 #endif
1240 add_to_string(&string, " ");
1241 /* TODO: Above, the number of spaces might not match the width
1242 * of the string generated by strftime. It depends on the
1243 * locale. So if ELinks knows the timestamps of some FTP
1244 * files but not others, it may misalign the file names.
1245 * Potential solutions:
1246 * - Pad the strings to a compile-time fixed width.
1247 * Con: If we choose a width that suffices for all known
1248 * locales, then it will be stupidly large for most of them.
1249 * - Generate an HTML table.
1250 * Con: Bloats the HTML source.
1251 * Any solution chosen here should also be applied to the
1252 * file: protocol handler. */
1254 add_char_to_string(&string, ' ');
1256 if (ftp_info->type == FTP_FILE_DIRECTORY && format->colorize_dir) {
1257 add_to_string(&string, "<font color=\"");
1258 add_to_string(&string, format->dircolor);
1259 add_to_string(&string, "\"><b>");
1262 add_to_string(&string, "<a href=\"");
1263 encode_uri_string(&string, ftp_info->name.source, ftp_info->name.length, 0);
1264 if (ftp_info->type == FTP_FILE_DIRECTORY)
1265 add_char_to_string(&string, '/');
1266 add_to_string(&string, "\">");
1267 add_html_to_string(&string, ftp_info->name.source, ftp_info->name.length);
1268 add_to_string(&string, "</a>");
1270 if (ftp_info->type == FTP_FILE_DIRECTORY && format->colorize_dir) {
1271 add_to_string(&string, "</b></font>");
1274 if (ftp_info->symlink.length) {
1275 add_to_string(&string, " -&gt; ");
1276 add_html_to_string(&string, ftp_info->symlink.source,
1277 ftp_info->symlink.length);
1280 add_char_to_string(&string, '\n');
1282 if (add_fragment(cached, *pos, string.source, string.length)) *tries = 0;
1283 *pos += string.length;
1285 done_string(&string);
1286 return 0;
1289 /* Get the next line of input and set *@len to the length of the line.
1290 * Return the number of newline characters at the end of the line or -1
1291 * if we must wait for more input. */
1292 static int
1293 ftp_get_line(struct cache_entry *cached, unsigned char *buf, int bufl,
1294 int last, int *len)
1296 unsigned char *newline;
1298 if (!bufl) return -1;
1300 newline = memchr(buf, ASCII_LF, bufl);
1302 if (newline) {
1303 *len = newline - buf;
1304 if (*len && buf[*len - 1] == ASCII_CR) {
1305 --*len;
1306 return 2;
1307 } else {
1308 return 1;
1312 if (last || bufl >= FTP_BUF_SIZE) {
1313 *len = bufl;
1314 return 0;
1317 return -1;
1320 /** Generate HTML for a line that was received from the FTP server but
1321 * could not be parsed. The caller is supposed to have added a \<pre>
1322 * start tag. (At the time of writing, init_directory_listing() was
1323 * used for that.)
1325 * @return -1 if out of memory, or 0 if successful. */
1326 static int
1327 ftp_add_unparsed_line(struct cache_entry *cached, off_t *pos, int *tries,
1328 const unsigned char *line, int line_length)
1330 int our_ret;
1331 struct string string;
1332 int frag_ret;
1334 our_ret = -1; /* assume out of memory if returning early */
1335 if (!init_string(&string)) goto out;
1336 if (!add_html_to_string(&string, line, line_length)) goto out;
1337 if (!add_char_to_string(&string, '\n')) goto out;
1339 frag_ret = add_fragment(cached, *pos, string.source, string.length);
1340 if (frag_ret == -1) goto out;
1341 *pos += string.length;
1342 if (frag_ret == 1) *tries = 0;
1344 our_ret = 0; /* success */
1346 out:
1347 done_string(&string); /* safe even if init_string failed */
1348 return our_ret;
1351 /** List a directory in html format.
1353 * @return the number of bytes used from the beginning of @a buffer,
1354 * or -1 if out of memory. */
1355 static int
1356 ftp_process_dirlist(struct cache_entry *cached, off_t *pos,
1357 unsigned char *buffer, int buflen, int last,
1358 int *tries, const struct ftp_dir_html_format *format)
1360 int ret = 0;
1362 while (1) {
1363 struct ftp_file_info ftp_info = INIT_FTP_FILE_INFO;
1364 unsigned char *buf = buffer + ret;
1365 int bufl = buflen - ret;
1366 int line_length, eol;
1368 eol = ftp_get_line(cached, buf, bufl, last, &line_length);
1369 if (eol == -1)
1370 return ret;
1372 ret += line_length + eol;
1374 /* Process line whose end we've already found. */
1376 if (parse_ftp_file_info(&ftp_info, buf, line_length)) {
1377 int retv;
1379 if (ftp_info.name.source[0] == '.'
1380 && (ftp_info.name.length == 1
1381 || (ftp_info.name.length == 2
1382 && ftp_info.name.source[1] == '.')))
1383 continue;
1385 retv = display_dir_entry(cached, pos, tries,
1386 format, &ftp_info);
1387 if (retv < 0) {
1388 return ret;
1391 } else {
1392 int retv = ftp_add_unparsed_line(cached, pos, tries,
1393 buf, line_length);
1395 if (retv == -1) /* out of memory; propagate to caller */
1396 return retv;
1401 /* This is the initial read handler for conn->data_socket->fd,
1402 * which may be either trying to connect to a passive FTP server or
1403 * listening for a connection from an active FTP server. In active
1404 * FTP, this function then accepts the connection and replaces
1405 * conn->data_socket->fd with the resulting socket. In any case,
1406 * this function does not read any data from the FTP server, but
1407 * rather hands the socket over to got_something_from_data_connection,
1408 * which then does the reads. */
1409 static void
1410 ftp_data_accept(struct connection *conn)
1412 struct ftp_connection_info *ftp = conn->info;
1413 int newsock;
1415 /* Because this function is called only as a read handler of
1416 * conn->data_socket->fd, the socket must be valid if we get
1417 * here. */
1418 assert(conn->data_socket->fd >= 0);
1419 if_assert_failed return;
1421 set_connection_timeout(conn);
1422 clear_handlers(conn->data_socket->fd);
1424 if ((conn->socket->protocol_family != EL_PF_INET6 && ftp->use_pasv)
1425 #ifdef CONFIG_IPV6
1426 || (conn->socket->protocol_family == EL_PF_INET6 && ftp->use_epsv)
1427 #endif
1429 newsock = conn->data_socket->fd;
1430 } else {
1431 newsock = accept(conn->data_socket->fd, NULL, NULL);
1432 if (newsock < 0) {
1433 retry_connection(conn, connection_state_for_errno(errno));
1434 return;
1436 close(conn->data_socket->fd);
1439 conn->data_socket->fd = newsock;
1441 set_handlers(newsock,
1442 (select_handler_T) got_something_from_data_connection,
1443 NULL, NULL, conn);
1446 /* A read handler for conn->data_socket->fd. This function reads
1447 * data from the FTP server, reformats it to HTML if it's a directory
1448 * listing, and adds the result to the cache entry. */
1449 static void
1450 got_something_from_data_connection(struct connection *conn)
1452 struct ftp_connection_info *ftp = conn->info;
1453 struct ftp_dir_html_format format;
1454 ssize_t len;
1456 /* Because this function is called only as a read handler of
1457 * conn->data_socket->fd, the socket must be valid if we get
1458 * here. */
1459 assert(conn->data_socket->fd >= 0);
1460 if_assert_failed return;
1462 /* XXX: This probably belongs rather to connect.c ? */
1464 set_connection_timeout(conn);
1466 if (!conn->cached) conn->cached = get_cache_entry(conn->uri);
1467 if (!conn->cached) {
1468 out_of_mem:
1469 abort_connection(conn, connection_state(S_OUT_OF_MEM));
1470 return;
1473 if (ftp->dir) {
1474 format.libc_codepage = get_cp_index("System");
1476 format.colorize_dir = get_opt_bool("document.browse.links.color_dirs", NULL);
1478 if (format.colorize_dir) {
1479 color_to_string(get_opt_color("document.colors.dirs", NULL),
1480 format.dircolor);
1484 if (ftp->dir && !conn->from) {
1485 struct string string;
1486 struct connection_state state;
1488 if (!conn->uri->data) {
1489 abort_connection(conn, connection_state(S_FTP_ERROR));
1490 return;
1493 state = init_directory_listing(&string, conn->uri);
1494 if (!is_in_state(state, S_OK)) {
1495 abort_connection(conn, state);
1496 return;
1499 add_fragment(conn->cached, conn->from, string.source, string.length);
1500 conn->from += string.length;
1502 done_string(&string);
1504 if (conn->uri->datalen) {
1505 struct ftp_file_info ftp_info = INIT_FTP_FILE_INFO_ROOT;
1507 display_dir_entry(conn->cached, &conn->from, &conn->tries,
1508 &format, &ftp_info);
1511 mem_free_set(&conn->cached->content_type, stracpy("text/html"));
1514 len = safe_read(conn->data_socket->fd, ftp->ftp_buffer + ftp->buf_pos,
1515 FTP_BUF_SIZE - ftp->buf_pos);
1516 if (len < 0) {
1517 retry_connection(conn, connection_state_for_errno(errno));
1518 return;
1521 if (len > 0) {
1522 conn->received += len;
1524 if (!ftp->dir) {
1525 if (add_fragment(conn->cached, conn->from,
1526 ftp->ftp_buffer, len) == 1)
1527 conn->tries = 0;
1528 conn->from += len;
1530 } else {
1531 int proceeded;
1533 proceeded = ftp_process_dirlist(conn->cached,
1534 &conn->from,
1535 ftp->ftp_buffer,
1536 len + ftp->buf_pos,
1537 0, &conn->tries,
1538 &format);
1540 if (proceeded == -1) goto out_of_mem;
1542 ftp->buf_pos += len - proceeded;
1544 memmove(ftp->ftp_buffer, ftp->ftp_buffer + proceeded,
1545 ftp->buf_pos);
1549 set_connection_state(conn, connection_state(S_TRANS));
1550 return;
1553 if (ftp_process_dirlist(conn->cached, &conn->from,
1554 ftp->ftp_buffer, ftp->buf_pos, 1,
1555 &conn->tries, &format) == -1)
1556 goto out_of_mem;
1558 #define ADD_CONST(str) { \
1559 add_fragment(conn->cached, conn->from, str, sizeof(str) - 1); \
1560 conn->from += (sizeof(str) - 1); }
1562 if (ftp->dir) ADD_CONST("</pre>\n<hr/>\n</body>\n</html>");
1564 close_socket(conn->data_socket);
1566 if (ftp->conn_state == 1) {
1567 ftp_end_request(conn, connection_state(S_OK));
1568 } else {
1569 ftp->conn_state = 2;
1570 set_connection_state(conn, connection_state(S_TRANS));
1574 static void
1575 ftp_end_request(struct connection *conn, struct connection_state state)
1577 if (is_in_state(state, S_OK) && conn->cached) {
1578 normalize_cache_entry(conn->cached, conn->from);
1581 set_connection_state(conn, state);
1582 add_keepalive_connection(conn, FTP_KEEPALIVE_TIMEOUT, NULL);