Renamed session structure.
[uftps.git] / command_loop.c
blob2c014c7d683ddf0dc679b45e4858a1266d4250f8
1 /*
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
8 * version.
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
13 * details.
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
19 #include "uftps.h"
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
26 * in the client.
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.
32 #include <ctype.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdio.h>
38 void command_loop (void)
40 int l;
42 while (1) {
43 switch (next_command()) {
46 * Straightforward implementations.
48 case FTP_NOOP:
49 reply_c("200 I'm alive, don't worry\r\n");
50 break;
52 case FTP_FEAT:
53 reply_c("211-Feature list:\r\n"
54 "211- MDTM\r\n"
55 "211- REST STREAM\r\n"
56 "211- SIZE\r\n"
57 "211- TVFS\r\n"
58 "211 End.\r\n");
59 break;
61 case FTP_PASS:
62 case FTP_USER:
63 reply_c("230 I don't care.\r\n");
64 break;
66 case FTP_SYST:
67 reply_c("215 UNIX Type: L8\r\n");
68 break;
70 case FTP_OPTS:
71 reply_c("501 Option not understood.\r\n");
72 break;
74 case FTP_ACCT:
75 case FTP_SMNT:
76 reply_c("202 Unimplemented, ignored.\r\n");
77 break;
79 case FTP_REIN:
80 reply_c("220 Nothing to REIN.\r\n");
81 break;
84 * A bit more complex commands.
86 case FTP_MODE:
87 if (toupper(SS.arg[0]) == 'S')
88 reply_c("200 MODE set to stream.\r\n");
89 else
90 reply_c("504 Mode not supported.\r\n");
91 break;
93 case FTP_STRU:
94 if (toupper(SS.arg[0]) == 'F')
95 reply_c("200 STRUcture set to file.\r\n");
96 else
97 reply_c("504 Structure not supported.\r\n");
98 break;
100 case FTP_TYPE:
101 switch (toupper(SS.arg[0])) {
102 case 'I':
103 case 'A':
104 case 'L':
105 reply_c("200 Whatever.\r\n");
106 break;
107 default:
108 reply_c("501 Invalid type.\r\n");
110 break;
112 case FTP_QUIT:
113 reply_c("221 Goodbye.\r\n");
114 close(SS.control_sk);
115 close(SS.passive_bind_sk);
116 exit(EXIT_SUCCESS);
117 break;
119 case FTP_PASV:
120 SS.passive_mode = 1;
121 reply(SS.passive_str, SS.passive_len);
122 break;
124 case FTP_PWD:
125 l = snprintf(SS.AuxBuf, LINE_SIZE, "257 \"%s\"\r\n", &SS.cwd[1]);
126 reply(SS.AuxBuf, l);
127 break;
129 case FTP_REST:
130 /* We don't need str_to_ll() as sscanf() does de job */
131 sscanf(SS.arg, "%lld", &SS.offset);
132 l = snprintf(SS.AuxBuf, LINE_SIZE, "350 Got it (%lld).\r\n",
133 SS.offset);
134 reply(SS.AuxBuf, l);
135 break;
138 * Complex commands implemented separately.
140 case FTP_PORT:
141 client_port();
142 break;
144 case FTP_NLST:
145 list_dir(0);
146 break;
148 case FTP_LIST:
149 list_dir(1);
150 break;
152 case FTP_MDTM:
153 file_stats(0);
154 break;
156 case FTP_SIZE:
157 file_stats(1);
158 break;
160 case FTP_CWD:
161 change_dir();
162 break;
164 case FTP_RETR:
165 send_file();
166 break;
168 case FTP_NONE:
169 reply_c("500 Command unrecognized.\r\n");
170 break;
172 default:
173 reply_c("500 Command not implemented.\r\n");