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 * Main FTP server loop which attends one client.
24 * It seems unbelievable but it's possible to implement a FTP server without
25 * the need of select() or poll(). In fact, this kind of complexity is present
28 * Commands not implemented are interpreted as unknown. There used to be
29 * dedicated dummy messages for some of them but code simplicity is preferred.
38 void command_loop (void)
41 switch (next_command()) {
44 * Straightforward implementations.
47 send_reply(S_cmd_sk
, "200 I'm alive, don't worry\r\n");
52 "211-Feature list:\r\n"
54 "211- REST STREAM\r\n"
62 send_reply(S_cmd_sk
, "230 I don't care.\r\n");
66 send_reply(S_cmd_sk
, "215 UNIX Type: L8\r\n");
70 send_reply(S_cmd_sk
, "501 Option not understood.\r\n");
75 send_reply(S_cmd_sk
, "202 Unimplemented, ignored.\r\n");
79 send_reply(S_cmd_sk
, "220 Nothing to REIN.\r\n");
83 * A bit more complex commands.
86 if (toupper(S_arg
[0]) == 'S')
88 "200 MODE set to stream.\r\n");
91 "504 Mode not supported.\r\n");
95 if (toupper(S_arg
[0]) == 'F')
97 "200 STRUcture set to file.\r\n");
100 "504 Structure not supported.\r\n");
104 switch (toupper(S_arg
[0])) {
108 send_reply(S_cmd_sk
, "200 Whatever.\r\n");
112 "501 Invalid type.\r\n");
117 send_reply(S_cmd_sk
, "221 Goodbye.\r\n");
119 close(S_passive_bind_sk
);
125 send_reply(S_cmd_sk
, S_passive_str
);
129 getcwd(AuxBuf
, LINE_SIZE
);
130 /* Root directory santy check */
131 if (AuxBuf
[Basedir_len
] == '\0') {
132 AuxBuf
[Basedir_len
] = '/';
133 AuxBuf
[Basedir_len
+ 1] = '\0';
135 /* We can overwrite the line buffer as no useful
136 * argument will be present */
137 snprintf(LineBuf
, LINE_SIZE
, "257 \"%s\"\r\n",
138 AuxBuf
+ Basedir_len
);
139 send_reply(S_cmd_sk
, LineBuf
);
143 /* We don't need str_to_ll() as sscanf() does de job */
144 sscanf(S_arg
, "%lld", &S_offset
);
145 snprintf(AuxBuf
, LINE_SIZE
, "350 Got it (%lld).\r\n",
147 send_reply(S_cmd_sk
, AuxBuf
);
151 * Complex commands implemented separately.
183 send_reply(S_cmd_sk
, "500 Command unrecognized.\r\n");