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
24 /* Large file support */
25 #define _FILE_OFFSET_BITS 64
26 #define _LARGEFILE_SOURCE
27 #define _LARGEFILE64_SOURCE
29 #include <sys/types.h>
30 #include <netinet/in.h>
32 /* Attributes help to catch silly mistakes, but they are not always available */
34 # define __attribute__(x)
38 #define DEFAULT_PORT 2211
39 #define LINE_SIZE 4096 /* Same value as PATH_MAX */
41 /* Recognized commands */
45 FTP_ABOR
, FTP_ACCT
, FTP_ALLO
, FTP_APPE
, FTP_CDUP
, FTP_CWD
, FTP_DELE
,
46 FTP_FEAT
, FTP_HELP
, FTP_LIST
, FTP_MDTM
, FTP_MKD
, FTP_MODE
, FTP_NLST
,
47 FTP_NOOP
, FTP_OPTS
, FTP_PASS
, FTP_PASV
, FTP_PORT
, FTP_PWD
, FTP_QUIT
,
48 FTP_REIN
, FTP_REST
, FTP_RETR
, FTP_RMD
, FTP_RNFR
, FTP_RNTO
, FTP_SITE
,
49 FTP_SIZE
, FTP_SMNT
, FTP_STAT
, FTP_STOR
, FTP_STOU
, FTP_STRU
, FTP_SYST
,
53 /* Data channel modes */
56 DEFAULT_MODE
, /* RFC default, unimplemented/unsupported */
57 ACTIVE_MODE
, /* Active mode enabled with PORT requests */
58 PASSIVE_MODE
/* Passive mode enabled with PASV requests */
61 /* Session (client) state */
65 int control_sk
; /* Control channel */
66 int data_sk
; /* Data channel */
67 int passive_sk
; /* Server socket for passive mode */
69 /* Buffer offsets and fill counters */
70 int input_offset
; /* Input buffer data offset */
71 int input_len
; /* Bytes in input buffer */
72 int cwd_len
; /* Length of current working directory */
74 /* Misc state information */
75 enum data_mode mode
; /* Current data channel mode */
76 off_t file_offset
; /* Last REST offset accepted */
77 char *arg
; /* Pointer to comand line argument */
79 /* Session addresses */
80 struct sockaddr_in port_destination
; /* Parsed PORT argument */
81 struct sockaddr_in local_address
; /* Control local IP */
82 struct sockaddr_in client_address
; /* Control peer IP */
85 char input
[LINE_SIZE
]; /* Incoming command buffer */
86 char aux
[LINE_SIZE
]; /* Auxiliary buffer */
87 char cwd
[LINE_SIZE
]; /* Current Working Directory */
90 extern struct _SessionScope SS
; /* SS --> Session State */
94 * Logging functions. These functions are implemented in log.c
97 # define assert(cond) if (!(cond)) warning("Assertion '" #cond "' failed")
98 void debug (const char *, ...) __attribute__((format(printf
,1,2)));
100 # define assert(cond)
101 static inline void debug (const char *msg
, ...) {}
105 * These functions will be replaced by variadic macros when compilers other than
108 void notice (const char *, ...) __attribute__((format(printf
,1,2)));
109 void warning (const char *, ...) __attribute__((format(printf
,1,2)));
110 void error (const char *, ...) __attribute__((format(printf
,1,2)));
111 void fatal (const char *, ...) __attribute__((format(printf
,1,2), noreturn
));
115 * Reply functions, used to send data over the control and data channels
116 * respectively. Implemented in reply.c.
118 void reply (const char *, int);
119 int data_reply (const char *, int);
123 * Other functions. Each function declared here is implemented in a separate
124 * file, with the same name as the function. Functions sorted alphabetically.
126 void change_dir (void);
127 void command_loop (void) __attribute__((noreturn
));
128 void enable_passive (void);
129 int expand_arg (void);
130 void file_stats (int);
131 void init_session (int);
133 enum command
next_command (void);
134 int open_data_channel (void);
135 int open_file (off_t
*);
136 void parse_port_argument (void);
137 void send_file (void);
141 * Utility macro to call reply() with a constant string. At compile time, the
142 * length of these strings is known.
144 #define reply_c(str) reply(str, sizeof(str) - 1)