2 * User FTP Server, Share folders over FTP without being root.
3 * Copyright (C) 2008 Isaac Jurado
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any later
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 * RETR command implementation. It uses sendfile() because seems quite optimal,
23 * just as VsFTPd does.
25 * Same restrictions, as in change_dir.c, apply for the argument. See that file
28 * Usually, a client sends a PASV command then immediately connects to the port
29 * indicated by the server reply. If we are continuously reusing the same port
30 * for passive data connections, we have to open whether theres is error or not
31 * to shift one position in the connection wait queue.
34 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/sendfile.h>
38 #include <netinet/in.h>
46 struct sockaddr_in saddr
;
48 socklen_t slen
= sizeof(saddr
);
51 SS
.data_sk
= accept(SS
.passive_bind_sk
,
52 (struct sockaddr
*) &saddr
, &slen
);
56 reply_c("501 Argument required.\r\n");
61 fd
= open(SS
.arg
, O_RDONLY
, 0);
64 reply_c("550 Could not open file.\r\n");
69 if (err
== -1 || !S_ISREG(st
.st_mode
)) {
70 reply_c("550 Could not stat file.\r\n");
74 reply_c("150 Sending file content.\r\n");
76 /* Apply a possible previous REST command. Ignore errors, is it allowd
79 (void) lseek(fd
, (off_t
) SS
.offset
, SEEK_SET
);
81 while (SS
.offset
< st
.st_size
) {
82 debug("Offset step: %lld", SS
.offset
);
84 err
= sendfile(SS
.data_sk
, fd
, (off_t
*) &SS
.offset
, INT_MAX
);
86 fatal("Could not send file");
89 debug("Offset end: %lld", SS
.offset
);
91 reply_c("226 File content sent.\r\n");