bug 764: Initialize the right member of union option_value
[elinks.git] / src / protocol / ftp / ftp.c
blob10c9e2833e1b32ddcf9a11ce7f9a2e1411712daa
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 union 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"));
475 add_crlf_to_string(&cmd);
477 send_cmd(conn, &cmd, (void *) ftp_pass_info, connection_state(S_LOGIN));
480 /* Parse PASS command response. */
481 static void
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));
489 return;
492 if (!response) {
493 read_from_socket(conn->socket, rb, connection_state(S_LOGIN),
494 ftp_pass_info);
495 return;
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);
512 return;
515 if (response >= 400) {
516 abort_connection(conn, connection_state(S_FTP_UNAVAIL));
517 return;
520 ftp_send_retr_req(conn, connection_state(S_GETH));
523 /* Construct PORT command. */
524 static void
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
543 * address. */
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]);
558 #ifdef CONFIG_IPV6
559 /* Construct EPRT command. */
560 static void
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>
573 * <net-prt>:
574 * AF Number Protocol
575 * --------- --------
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, '|');
584 #endif
586 /* Depending on options, get proper ftp data socket and command.
587 * It appends ftp command (either PASV,PORT,EPSV or EPRT) to @command
588 * string.
589 * When PORT or EPRT are used, related sockets are created.
590 * It returns 0 on error (data socket creation failure). */
591 static int
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");
598 #ifdef CONFIG_IPV6
599 ftp->use_epsv = get_opt_bool("protocol.ftp.use_epsv");
601 if (conn->socket->protocol_family == EL_PF_INET6) {
602 if (ftp->use_epsv) {
603 add_to_string(command, "EPSV");
605 } else {
606 struct sockaddr_storage data_addr;
607 int data_sock;
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);
618 } else
619 #endif
621 if (ftp->use_pasv) {
622 add_to_string(command, "PASV");
624 } else {
625 struct sockaddr_in sa;
626 unsigned char pc[6];
627 int data_sock;
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);
643 return 1;
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. */
649 static int
650 is_ftp_pathname_safe(const struct string *s)
652 int i;
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)
662 return 0;
664 return 1;
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)
673 int ok = 0;
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));
682 goto ret;
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
689 * risky. */
690 ftp = mem_calloc(1, sizeof(*ftp));
691 if (!ftp) {
692 abort_connection(conn, connection_state(S_OUT_OF_MEM));
693 goto ret;
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));
702 goto ret;
705 if (!get_ftp_data_socket(conn, &ftp_data_command)) {
706 INTERNAL("Ftp data socket failure");
707 abort_connection(conn, connection_state(S_INTERNAL));
708 goto ret;
711 if (!add_uri_to_string(&pathname, conn->uri, URI_PATH)) {
712 abort_connection(conn, connection_state(S_OUT_OF_MEM));
713 goto ret;
716 decode_uri_string(&pathname);
717 if (!is_ftp_pathname_safe(&pathname)) {
718 abort_connection(conn, connection_state(S_BAD_URL));
719 goto ret;
722 if (!conn->uri->datalen
723 || conn->uri->data[conn->uri->datalen - 1] == '/') {
724 /* Commands to get directory listing. */
726 ftp->dir = 1;
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));
741 goto ret;
744 conn->from = 0;
746 } else {
747 /* Commands to get a file. */
749 ftp->dir = 0;
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));
757 goto ret;
760 if (conn->from || conn->progress->start > 0) {
761 const off_t offset = conn->from
762 ? 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));
769 goto ret;
772 ftp->rest_sent = 1;
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));
780 goto ret;
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);
788 if (!ftp) {
789 abort_connection(conn, connection_state(S_OUT_OF_MEM));
790 goto ret;
792 conn->info = ftp; /* in case mem_realloc moved the buffer */
794 memcpy(ftp->cmd_buffer, command.source, command.length + 1);
795 ok = 1;
797 ret:
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;
806 static void
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');
812 if (!nl) {
813 add_to_string(cmd, ftp->cmd_buffer);
814 return;
817 nl++;
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. */
823 static void
824 ftp_send_retr_req(struct connection *conn, struct connection_state state)
826 struct string cmd;
828 if (!init_string(&cmd)) {
829 abort_connection(conn, connection_state(S_OUT_OF_MEM));
830 return;
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)) {
836 done_string(&cmd);
837 return;
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. */
847 static off_t
848 get_filesize_from_RETR(unsigned char *data, int data_len, int *resume)
850 off_t file_len;
851 int pos;
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). */
857 *resume = 0;
858 for (pos = 0; pos < data_len && data[pos] != ASCII_LF; pos++)
859 if (data[pos] == '(')
860 pos_file_len = 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;
867 char *endptr;
868 double size;
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;
879 kbytes++;
880 size = strtod((const char *)kbytes, &endptr);
881 if (endptr == (char *)kbytes) return -1;
882 *resume = 1;
883 return (off_t)(size * 1024.0);
886 pos_file_len++;
887 if (!isdigit(data[pos_file_len]))
888 return -1;
890 for (pos = pos_file_len; pos < data_len; pos++)
891 if (!isdigit(data[pos]))
892 goto next;
894 return -1;
896 next:
897 for (; pos < data_len; pos++)
898 if (data[pos] != ' ')
899 break;
901 if (pos + 4 > data_len)
902 return -1;
904 if (c_strncasecmp(&data[pos], "byte", 4))
905 return -1;
907 errno = 0;
908 file_len = (off_t) strtol(&data[pos_file_len], NULL, 10);
909 if (errno) return -1;
911 return file_len;
914 /* Connect to the host and port specified by a passive FTP server. */
915 static int
916 ftp_data_connect(struct connection *conn, int pf, struct sockaddr_storage *sa,
917 int size_of_sockaddr)
919 int fd;
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));
925 return -1;
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));
931 return -1;
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);
939 return 0;
942 static void
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;
947 int response;
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));
956 return;
959 if (!response) {
960 read_from_socket(conn->socket, rb,
961 connection_state(S_GETH),
962 ftp_retr_file);
963 return;
966 if (response == 227) {
967 if (ftp_data_connect(conn, PF_INET, &sa, sizeof(struct sockaddr_in)))
968 return;
971 #ifdef CONFIG_IPV6
972 if (response == 229) {
973 if (ftp_data_connect(conn, PF_INET6, &sa, sizeof(struct sockaddr_in6)))
974 return;
976 #endif
978 ftp->pending_commands--;
980 /* XXX: The case values are order numbers of commands. */
981 switch (ftp->opc - ftp->pending_commands) {
982 case 1: /* TYPE */
983 break;
985 case 2: /* PORT */
986 if (response >= 400) {
987 abort_connection(conn,
988 connection_state(S_FTP_PORT));
989 return;
991 break;
993 case 3: /* REST / CWD */
994 if (response >= 400) {
995 if (ftp->dir) {
996 abort_connection(conn,
997 connection_state(S_FTP_NO_FILE));
998 return;
1000 conn->from = 0;
1001 } else if (ftp->rest_sent) {
1002 /* Following code is related to resume
1003 * feature. */
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
1010 * Content-Range. */
1011 conn->progress->seek = conn->from;
1013 conn->progress->start = conn->from;
1015 break;
1017 default:
1018 INTERNAL("WHAT???");
1021 ftp_send_retr_req(conn, connection_state(S_GETH));
1022 return;
1025 response = get_ftp_response(conn, rb, 2, NULL);
1027 if (response == -1) {
1028 abort_connection(conn, connection_state(S_FTP_ERROR));
1029 return;
1032 if (!response) {
1033 read_from_socket(conn->socket, rb, connection_state(S_GETH),
1034 ftp_retr_file);
1035 return;
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) {
1042 off_t file_len;
1043 int res;
1045 file_len = get_filesize_from_RETR(rb->data, rb->length, &res);
1046 if (file_len > 0) {
1047 /* FIXME: ..when downloads resuming
1048 * implemented.. */
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
1068 * server. */
1069 abort_connection(conn, connection_state(S_FTP_ERROR));
1070 return;
1072 set_handlers(conn->data_socket->fd, (select_handler_T) ftp_data_accept,
1073 NULL, NULL, conn);
1075 /* read_from_socket(conn->socket, rb, ftp_got_final_response); */
1076 ftp_got_final_response(socket, rb);
1079 static void
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));
1088 return;
1091 if (!response) {
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);
1096 return;
1099 if (response >= 550 || response == 450) {
1100 /* Requested action not taken.
1101 * File unavailable (e.g., file not found, no access). */
1103 if (!conn->cached)
1104 conn->cached = get_cache_entry(conn->uri);
1106 if (!conn->cached
1107 || !redirect_cache(conn->cached, "/", 1, 0)) {
1108 abort_connection(conn, connection_state(S_OUT_OF_MEM));
1109 return;
1112 abort_connection(conn, connection_state(S_OK));
1113 return;
1116 if (response >= 400) {
1117 abort_connection(conn, connection_state(S_FTP_FILE_ERROR));
1118 return;
1121 if (ftp->conn_state == 2) {
1122 ftp_end_request(conn, connection_state(S_OK));
1123 } else {
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. */
1142 int libc_codepage;
1144 /** Nonzero if directories should be displayed in a different
1145 * color. From the document.browse.links.color_dirs option. */
1146 int colorize_dir;
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. */
1154 static int
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'));
1187 #undef FTP_PERM
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);
1199 } else {
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;
1207 struct tm *when_tm;
1208 unsigned char *fmt;
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];
1213 int wr;
1215 if (ftp_info->local_time_zone)
1216 when_tm = localtime(&when);
1217 else
1218 when_tm = gmtime(&when);
1220 if (current_time > when + 6L * 30L * 24L * 60L * 60L
1221 || current_time < when - 60L * 60L)
1222 fmt = "%b %e %Y";
1223 else
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,
1228 date, wr);
1229 } else
1230 #endif
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, " -&gt; ");
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);
1277 return 0;
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. */
1283 static int
1284 ftp_get_line(struct cache_entry *cached, unsigned char *buf, int bufl,
1285 int last, int *len)
1287 unsigned char *newline;
1289 if (!bufl) return -1;
1291 newline = memchr(buf, ASCII_LF, bufl);
1293 if (newline) {
1294 *len = newline - buf;
1295 if (*len && buf[*len - 1] == ASCII_CR) {
1296 --*len;
1297 return 2;
1298 } else {
1299 return 1;
1303 if (last || bufl >= FTP_BUF_SIZE) {
1304 *len = bufl;
1305 return 0;
1308 return -1;
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
1314 * used for that.)
1316 * @return -1 if out of memory, or 0 if successful. */
1317 static int
1318 ftp_add_unparsed_line(struct cache_entry *cached, off_t *pos, int *tries,
1319 const unsigned char *line, int line_length)
1321 int our_ret;
1322 struct string string;
1323 int frag_ret;
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 */
1337 out:
1338 done_string(&string); /* safe even if init_string failed */
1339 return our_ret;
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. */
1346 static int
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)
1351 int ret = 0;
1353 while (1) {
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);
1360 if (eol == -1)
1361 return ret;
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)) {
1368 int retv;
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] == '.')))
1374 continue;
1376 retv = display_dir_entry(cached, pos, tries,
1377 format, &ftp_info);
1378 if (retv < 0) {
1379 return ret;
1382 } else {
1383 int retv = ftp_add_unparsed_line(cached, pos, tries,
1384 buf, line_length);
1386 if (retv == -1) /* out of memory; propagate to caller */
1387 return retv;
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. */
1400 static void
1401 ftp_data_accept(struct connection *conn)
1403 struct ftp_connection_info *ftp = conn->info;
1404 int newsock;
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
1408 * here. */
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)
1416 #ifdef CONFIG_IPV6
1417 || (conn->socket->protocol_family == EL_PF_INET6 && ftp->use_epsv)
1418 #endif
1420 newsock = conn->data_socket->fd;
1421 } else {
1422 newsock = accept(conn->data_socket->fd, NULL, NULL);
1423 if (newsock < 0) {
1424 retry_connection(conn, connection_state_for_errno(errno));
1425 return;
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,
1434 NULL, NULL, conn);
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. */
1440 static void
1441 got_something_from_data_connection(struct connection *conn)
1443 struct ftp_connection_info *ftp = conn->info;
1444 struct ftp_dir_html_format format;
1445 ssize_t len;
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
1449 * here. */
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) {
1459 out_of_mem:
1460 abort_connection(conn, connection_state(S_OUT_OF_MEM));
1461 return;
1464 if (ftp->dir) {
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"),
1471 format.dircolor);
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));
1481 return;
1484 state = init_directory_listing(&string, conn->uri);
1485 if (!is_in_state(state, S_OK)) {
1486 abort_connection(conn, state);
1487 return;
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);
1507 if (len < 0) {
1508 retry_connection(conn, connection_state_for_errno(errno));
1509 return;
1512 if (len > 0) {
1513 conn->received += len;
1515 if (!ftp->dir) {
1516 if (add_fragment(conn->cached, conn->from,
1517 ftp->ftp_buffer, len) == 1)
1518 conn->tries = 0;
1519 conn->from += len;
1521 } else {
1522 int proceeded;
1524 proceeded = ftp_process_dirlist(conn->cached,
1525 &conn->from,
1526 ftp->ftp_buffer,
1527 len + ftp->buf_pos,
1528 0, &conn->tries,
1529 &format);
1531 if (proceeded == -1) goto out_of_mem;
1533 ftp->buf_pos += len - proceeded;
1535 memmove(ftp->ftp_buffer, ftp->ftp_buffer + proceeded,
1536 ftp->buf_pos);
1540 set_connection_state(conn, connection_state(S_TRANS));
1541 return;
1544 if (ftp_process_dirlist(conn->cached, &conn->from,
1545 ftp->ftp_buffer, ftp->buf_pos, 1,
1546 &conn->tries, &format) == -1)
1547 goto out_of_mem;
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));
1559 } else {
1560 ftp->conn_state = 2;
1561 set_connection_state(conn, connection_state(S_TRANS));
1565 static void
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);