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
23 #define _FILE_OFFSET_BITS 64
24 #define _LARGEFILE_SOURCE
25 #define _LARGEFILE64_SOURCE
28 * Hasefroch requirements. No need to inclose within "#ifdef" as no other
29 * system cares about these definitions.
32 #define __MSVCRT_VERSION__ 0x0601
36 * Early multiplaform support (more details on hase.h).
38 #include <sys/types.h>
40 # include <winsock2.h>
41 typedef __int64 off_t
;
42 typedef int socklen_t
;
43 # define CCP_CAST (const char *)
45 # include <netinet/in.h>
46 # define closesocket(s) close(s)
51 * Attributes help to catch silly mistakes, but they are not always available.
53 #if !defined(__GNUC__) && !defined(__MINGW32__)
54 # define __attribute__(x)
60 #define DEFAULT_PORT 2211
61 #define LINE_SIZE 4096 /* Same value as PATH_MAX */
64 * Recognized commands.
69 FTP_ABOR
, FTP_ACCT
, FTP_ALLO
, FTP_APPE
, FTP_CDUP
, FTP_CWD
, FTP_DELE
,
70 FTP_FEAT
, FTP_HELP
, FTP_LIST
, FTP_MDTM
, FTP_MKD
, FTP_MODE
, FTP_NLST
,
71 FTP_NOOP
, FTP_OPTS
, FTP_PASS
, FTP_PASV
, FTP_PORT
, FTP_PWD
, FTP_QUIT
,
72 FTP_REIN
, FTP_REST
, FTP_RETR
, FTP_RMD
, FTP_RNFR
, FTP_RNTO
, FTP_SITE
,
73 FTP_SIZE
, FTP_SMNT
, FTP_STAT
, FTP_STOR
, FTP_STOU
, FTP_STRU
, FTP_SYST
,
82 DEFAULT_MODE
, /* RFC default, unimplemented/unsupported */
83 ACTIVE_MODE
, /* Active mode enabled with PORT requests */
84 PASSIVE_MODE
/* Passive mode enabled with PASV requests */
88 * Session (client) state.
93 int control_sk
; /* Control channel */
94 int data_sk
; /* Data channel */
95 int passive_sk
; /* Server socket for passive mode */
97 /* Buffer offsets and fill counters */
98 int input_offset
; /* Input buffer data offset */
99 int input_len
; /* Bytes in input buffer */
100 int cwd_len
; /* Length of current working directory */
102 /* Misc state information */
103 int pid
; /* Cached Process ID */
104 enum data_mode mode
; /* Current data channel mode */
105 off_t file_offset
; /* Last REST offset accepted */
106 char *arg
; /* Pointer to comand line argument */
108 /* Session addresses */
109 struct sockaddr_in port_destination
; /* Parsed PORT argument */
110 struct sockaddr_in local_address
; /* Control local IP */
111 struct sockaddr_in client_address
; /* Control peer IP */
114 char input
[LINE_SIZE
]; /* Incoming command buffer */
115 char aux
[LINE_SIZE
]; /* Auxiliary buffer */
116 char cwd
[LINE_SIZE
]; /* Current Working Directory */
119 extern struct _SessionScope SS
; /* SS --> Session State */
123 * Logging functions. These functions are implemented in log.c
126 # define assert(cond) if (!(cond)) warning("Assertion '" #cond "' failed")
127 void debug (const char *, ...) __attribute__((format(printf
,1,2)));
129 # define assert(cond)
130 static inline void debug (const char *msg
, ...) {}
134 * These functions will be replaced by variadic macros when compilers other than
137 void notice (const char *, ...) __attribute__((format(printf
,1,2)));
138 void warning (const char *, ...) __attribute__((format(printf
,1,2)));
139 void error (const char *, ...) __attribute__((format(printf
,1,2)));
140 void fatal (const char *, ...) __attribute__((format(printf
,1,2), noreturn
));
144 * Reply functions, used to send data over the control and data channels
145 * respectively. Implemented in reply.c.
147 void reply (const char *, int);
148 int data_reply (const char *, int);
152 * Other functions. Each function declared here is implemented in a separate
153 * file, with the same name as the function. Functions sorted alphabetically.
155 void change_dir (void);
156 void command_loop (void) __attribute__((noreturn
));
157 void enable_passive (void);
158 int expand_arg (void);
159 void file_stats (int);
160 void init_session (int);
162 enum command
next_command (void);
163 int open_data_channel (void);
164 int open_file (off_t
*);
165 void parse_port_argument (void);
166 void send_file (void);
170 * Utility macro to call reply() with a constant string. At compile time, the
171 * length of these strings is known.
173 #define reply_c(str) reply(str, sizeof(str) - 1)