1 | /* FIXME: 1. Parse command is a hack. We can do better. |
---|
2 | * 2. OSV: hooks support seems to be bad, as it requires storing of |
---|
3 | * entire input file in memory. Seem to be better to change it to |
---|
4 | * something more reasonable, like having |
---|
5 | * 'hook_write(void const *buf, int count)' routine that will be |
---|
6 | * called multiple times while file is being received. |
---|
7 | * 3. OSV: Remove hack with "/dev/null"? |
---|
8 | * |
---|
9 | * FTP Server Daemon |
---|
10 | * |
---|
11 | * Submitted by: Jake Janovetz <janovetz@tempest.ece.uiuc.edu> |
---|
12 | * |
---|
13 | * Changed by: Sergei Organov <osv@javad.ru> (OSV) |
---|
14 | * Arnout Vandecappelle <arnout@mind.be> (AV) |
---|
15 | * Sebastien Bourdeauducq <sebastien@milkymist.org> (MM) |
---|
16 | * |
---|
17 | * |
---|
18 | * Changes: |
---|
19 | * |
---|
20 | * 2010-12-02 Sebastien Bourdeauducq <sebastien@milkymist.org> |
---|
21 | * |
---|
22 | * * Support spaces in filenames |
---|
23 | * |
---|
24 | * 2010-04-29 Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be> |
---|
25 | * |
---|
26 | * * Added USER/PASS authentication. |
---|
27 | * |
---|
28 | * 2001-01-31 Sergei Organov <osv@javad.ru> |
---|
29 | * |
---|
30 | * * Hacks with current dir and root dir removed in favor of new libio |
---|
31 | * support for task-local current and root directories. |
---|
32 | * |
---|
33 | * 2001-01-30 Sergei Organov <osv@javad.ru> |
---|
34 | * |
---|
35 | * * Bug in `close_data_socket()' introduced by previous change fixed. |
---|
36 | * * `command_pasv()' changed to set timeout on socket we are listening on |
---|
37 | * and code fixed to don't close socket twice on error. |
---|
38 | * * `serr()' changed to clear `errno'. |
---|
39 | * * `data_socket()' changed to clear `errno' before `bind()'. |
---|
40 | * * `session()' changed to clear `errno' before processing session. |
---|
41 | * |
---|
42 | * 2001-01-29 Sergei Organov <osv@javad.ru> |
---|
43 | * |
---|
44 | * * `close_data_socket()' fixed to close both active and passive sockets |
---|
45 | * * Initialize info->data_socket to -1 in `daemon()' |
---|
46 | * * Initialize `fname' to empty string in `exec_command()' |
---|
47 | * |
---|
48 | * 2001-01-22 Sergei Organov <osv@javad.ru> |
---|
49 | * |
---|
50 | * * Timeouts on sockets implemented. 'idle' field added to |
---|
51 | * configuration. No timeout by default to keep backward compatibility. |
---|
52 | * Note: SITE IDLE command not implemented yet. |
---|
53 | * * Basic global access control implemented. 'access' field added to |
---|
54 | * configuration. No access limitations by default to keep backward |
---|
55 | * compatibility. |
---|
56 | * |
---|
57 | * 2001-01-17 Sergei Organov <osv@javad.ru> |
---|
58 | * |
---|
59 | * * Anchor data socket for active mode (using self IP and port 20.) |
---|
60 | * * Fixed default data port support (still not tested). |
---|
61 | * * Don't allow IP address different from originating host in |
---|
62 | * PORT command to improve security. |
---|
63 | * * Fixed bug in MDTM command. |
---|
64 | * * Check for correctness of parsing of argument in command_port(). |
---|
65 | * * Fixed squeeze_path() to don't allow names like 'NAME/smth' where |
---|
66 | * 'NAME' is not a directory. |
---|
67 | * * Command parsing a little bit improved: command names are now |
---|
68 | * converted to upper-case to be more compatible with RFC (command |
---|
69 | * names are not case-sensitive.) |
---|
70 | * * Reformat comments so that they have RTEMS look-and-feel. |
---|
71 | * |
---|
72 | * 2001-01-16 Sergei Organov <osv@javad.ru> |
---|
73 | * |
---|
74 | * * Fixed DELE, SITE CHMOD, RMD, MKD broken by previous changes |
---|
75 | * * True ASCII mode implemented (doesn't work for hooks and /dev/null) |
---|
76 | * * Passive mode implemented, PASV command added. |
---|
77 | * * Default port for data connection could be used (untested, can't find |
---|
78 | * ftp client that doesn't send PORT command) |
---|
79 | * * SYST reply changed to UNIX, as former RTEMS isn't registered name. |
---|
80 | * * Reply codes reviewed and fixed. |
---|
81 | * |
---|
82 | * 2001-01-08 Sergei Organov <osv@javad.ru> |
---|
83 | * |
---|
84 | * * use pool of pre-created threads to handle sessions |
---|
85 | * * LIST output now similar to what "/bin/ls -al" would output, thus |
---|
86 | * FTP clients could parse it. |
---|
87 | * * LIST NAME now works (both for files and directories) |
---|
88 | * * keep track of CWD for every session separately |
---|
89 | * * ability to specify root directory name in configuration table |
---|
90 | * * options sent in commands are ignored, thus LIST -al FILE works |
---|
91 | * * added support for NLST, CDUP and MDTM commands |
---|
92 | * * buffers are allocated on stack instead of heap where possible |
---|
93 | * * drop using of task notepad to pass parameters - use function |
---|
94 | * arguments instead |
---|
95 | * * various bug-fixes, e.g., use of PF_INET in socket() instead of |
---|
96 | * AF_INET, use snprintf() instead of sprintf() everywhere for safety, |
---|
97 | * etc. |
---|
98 | * |
---|
99 | * $Id$ |
---|
100 | */ |
---|
101 | |
---|
102 | /************************************************************************* |
---|
103 | * ftpd.c |
---|
104 | ************************************************************************* |
---|
105 | * Description: |
---|
106 | * |
---|
107 | * This file contains the daemon which services requests that appear |
---|
108 | * on the FTP port. This server is compatible with FTP, but it |
---|
109 | * also provides 'hooks' to make it usable in situations where files |
---|
110 | * are not used/necessary. Once the server is started, it runs |
---|
111 | * forever. |
---|
112 | * |
---|
113 | * |
---|
114 | * Organization: |
---|
115 | * |
---|
116 | * The FTP daemon is started upon boot along with a (configurable) |
---|
117 | * number of tasks to handle sessions. It runs all the time and |
---|
118 | * waits for connections on the known FTP port (21). When |
---|
119 | * a connection is made, it wakes-up a 'session' task. That |
---|
120 | * session then interacts with the remote host. When the session |
---|
121 | * is complete, the session task goes to sleep. The daemon still |
---|
122 | * runs, however. |
---|
123 | * |
---|
124 | * |
---|
125 | * Supported commands are: |
---|
126 | * |
---|
127 | * RETR xxx - Sends a file from the client. |
---|
128 | * STOR xxx - Receives a file from the client. xxx = filename. |
---|
129 | * LIST xxx - Sends a file list to the client. |
---|
130 | * NLST xxx - Sends a file list to the client. |
---|
131 | * USER - Does nothing. |
---|
132 | * PASS - Does nothing. |
---|
133 | * SYST - Replies with the system type (`RTEMS'). |
---|
134 | * DELE xxx - Delete file xxx. |
---|
135 | * MKD xxx - Create directory xxx. |
---|
136 | * RMD xxx - Remove directory xxx. |
---|
137 | * PWD - Print working directory. |
---|
138 | * CWD xxx - Change working directory. |
---|
139 | * CDUP - Change to upper directory. |
---|
140 | * SITE CHMOD xxx yyy - Change permissions on file yyy to xxx. |
---|
141 | * PORT a,b,c,d,x,y - Setup for a data port to IP address a.b.c.d |
---|
142 | * and port (x*256 + y). |
---|
143 | * MDTM xxx - Send file modification date/time to the client. |
---|
144 | * xxx = filename. |
---|
145 | * PASV - Use passive mode data connection. |
---|
146 | * |
---|
147 | * |
---|
148 | * The public routines contained in this file are: |
---|
149 | * |
---|
150 | * rtems_initialize_ftpd - Initializes and starts the server daemon, |
---|
151 | * then returns to its caller. |
---|
152 | * |
---|
153 | *------------------------------------------------------------------------ |
---|
154 | * Jake Janovetz |
---|
155 | * University of Illinois |
---|
156 | * 1406 West Green Street |
---|
157 | * Urbana IL 61801 |
---|
158 | ************************************************************************* |
---|
159 | * Change History: |
---|
160 | * 12/01/97 - Creation (JWJ) |
---|
161 | * 2001-01-08 - Changes by OSV |
---|
162 | * 2010-04-29 - Authentication added by AV |
---|
163 | *************************************************************************/ |
---|
164 | |
---|
165 | /************************************************************************* |
---|
166 | * Meanings of first and second digits of reply codes: |
---|
167 | * |
---|
168 | * Reply: Description: |
---|
169 | *-------- -------------- |
---|
170 | * 1yz Positive preliminary reply. The action is being started but |
---|
171 | * expect another reply before sending another command. |
---|
172 | * 2yz Positive completion reply. A new command can be sent. |
---|
173 | * 3yz Positive intermediate reply. The command has been accepted |
---|
174 | * but another command must be sent. |
---|
175 | * 4yz Transient negative completion reply. The requested action did |
---|
176 | * not take place, but the error condition is temporary so the |
---|
177 | * command can be reissued later. |
---|
178 | * 5yz Permanent negative completion reply. The command was not |
---|
179 | * accepted and should not be retried. |
---|
180 | *------------------------------------------------------------------------- |
---|
181 | * x0z Syntax errors. |
---|
182 | * x1z Information. |
---|
183 | * x2z Connections. Replies referring to the control or data |
---|
184 | * connections. |
---|
185 | * x3z Authentication and accounting. Replies for the login or |
---|
186 | * accounting commands. |
---|
187 | * x4z Unspecified. |
---|
188 | * x5z Filesystem status. |
---|
189 | *************************************************************************/ |
---|
190 | |
---|
191 | #if HAVE_CONFIG_H |
---|
192 | #include "config.h" |
---|
193 | #endif |
---|
194 | |
---|
195 | #include <stdio.h> |
---|
196 | #include <stdlib.h> |
---|
197 | #include <string.h> |
---|
198 | #include <unistd.h> |
---|
199 | #include <fcntl.h> |
---|
200 | #include <dirent.h> |
---|
201 | #include <errno.h> |
---|
202 | #include <ctype.h> |
---|
203 | |
---|
204 | #include <rtems.h> |
---|
205 | #include <rtems/rtems_bsdnet.h> |
---|
206 | #include <rtems/error.h> |
---|
207 | #include <rtems/libio.h> |
---|
208 | #include <rtems/userenv.h> |
---|
209 | #include <syslog.h> |
---|
210 | |
---|
211 | #include <sys/types.h> |
---|
212 | #include <sys/socket.h> |
---|
213 | #include <arpa/ftp.h> |
---|
214 | #include <netinet/in.h> |
---|
215 | |
---|
216 | #include "ftpd.h" |
---|
217 | |
---|
218 | |
---|
219 | #ifdef __GNUC__ |
---|
220 | /* change to #if 1 to disable syslog entirely */ |
---|
221 | #if 0 |
---|
222 | #undef syslog |
---|
223 | #define syslog(a, b, ...) while(0){} |
---|
224 | #endif |
---|
225 | #endif |
---|
226 | |
---|
227 | #define FTPD_SERVER_MESSAGE "RTEMS FTP server (Version 1.1-JWJ) ready." |
---|
228 | |
---|
229 | #define FTPD_SYSTYPE "UNIX Type: L8" |
---|
230 | |
---|
231 | /* Seem to be unused */ |
---|
232 | #if 0 |
---|
233 | #define FTPD_WELCOME_MESSAGE \ |
---|
234 | "Welcome to the RTEMS FTP server.\n" \ |
---|
235 | "\n" \ |
---|
236 | "Login accepted.\n" |
---|
237 | #endif |
---|
238 | |
---|
239 | /* Event to be used by session tasks for waiting */ |
---|
240 | enum |
---|
241 | { |
---|
242 | FTPD_RTEMS_EVENT = RTEMS_EVENT_1 |
---|
243 | }; |
---|
244 | |
---|
245 | /* Configuration table */ |
---|
246 | extern struct rtems_ftpd_configuration rtems_ftpd_configuration; |
---|
247 | |
---|
248 | /* this is not prototyped in strict ansi mode */ |
---|
249 | FILE *fdopen (int fildes, const char *mode); |
---|
250 | |
---|
251 | /*SessionInfo structure. |
---|
252 | * |
---|
253 | * The following structure is allocated for each session. |
---|
254 | */ |
---|
255 | typedef struct |
---|
256 | { |
---|
257 | struct sockaddr_in ctrl_addr; /* Control connection self address */ |
---|
258 | struct sockaddr_in data_addr; /* Data address set by PORT command */ |
---|
259 | struct sockaddr_in def_addr; /* Default address for data */ |
---|
260 | int use_default; /* 1 - use default address for data */ |
---|
261 | FILE *ctrl_fp; /* File pointer for control connection */ |
---|
262 | int ctrl_socket; /* Socket for ctrl connection */ |
---|
263 | int pasv_socket; /* Socket for PASV connection */ |
---|
264 | int data_socket; /* Socket for data connection */ |
---|
265 | int idle; /* Timeout in seconds */ |
---|
266 | int xfer_mode; /* Transfer mode (ASCII/binary) */ |
---|
267 | rtems_id tid; /* Task id */ |
---|
268 | char *user; /* user name (0 if not supplied) */ |
---|
269 | char *pass; /* password (0 if not supplied) */ |
---|
270 | bool auth; /* true if user/pass was valid, false if not or not supplied */ |
---|
271 | } FTPD_SessionInfo_t; |
---|
272 | |
---|
273 | |
---|
274 | /* |
---|
275 | * TaskPool structure. |
---|
276 | */ |
---|
277 | typedef struct |
---|
278 | { |
---|
279 | FTPD_SessionInfo_t *info; |
---|
280 | FTPD_SessionInfo_t **queue; |
---|
281 | int count; |
---|
282 | int head; |
---|
283 | int tail; |
---|
284 | rtems_id mutex; |
---|
285 | rtems_id sem; |
---|
286 | } FTPD_TaskPool_t; |
---|
287 | |
---|
288 | /* |
---|
289 | * Task pool instance. |
---|
290 | */ |
---|
291 | static FTPD_TaskPool_t task_pool; |
---|
292 | |
---|
293 | /* |
---|
294 | * Root directory |
---|
295 | */ |
---|
296 | |
---|
297 | static char const* ftpd_root = "/"; |
---|
298 | |
---|
299 | /* |
---|
300 | * Default idle timeout for sockets in seconds. |
---|
301 | */ |
---|
302 | static int ftpd_timeout = 0; |
---|
303 | |
---|
304 | /* |
---|
305 | * Global access flags. |
---|
306 | */ |
---|
307 | static int ftpd_access = 0; |
---|
308 | |
---|
309 | /* |
---|
310 | * serr |
---|
311 | * |
---|
312 | * Return error string corresponding to current 'errno'. |
---|
313 | * |
---|
314 | */ |
---|
315 | static char const* |
---|
316 | serr(void) |
---|
317 | { |
---|
318 | int err = errno; |
---|
319 | errno = 0; |
---|
320 | return strerror(err); |
---|
321 | } |
---|
322 | |
---|
323 | /* |
---|
324 | * Utility routines for access control. |
---|
325 | * |
---|
326 | */ |
---|
327 | |
---|
328 | static int |
---|
329 | can_read(void) |
---|
330 | { |
---|
331 | return (ftpd_access & FTPD_NO_READ) == 0; |
---|
332 | } |
---|
333 | |
---|
334 | static int |
---|
335 | can_write(void) |
---|
336 | { |
---|
337 | return (ftpd_access & FTPD_NO_WRITE) == 0; |
---|
338 | } |
---|
339 | |
---|
340 | /* |
---|
341 | * Task pool management routines |
---|
342 | * |
---|
343 | */ |
---|
344 | |
---|
345 | |
---|
346 | /* |
---|
347 | * task_pool_done |
---|
348 | * |
---|
349 | * Cleanup task pool. |
---|
350 | * |
---|
351 | * Input parameters: |
---|
352 | * count - number of entries in task pool to cleanup |
---|
353 | * |
---|
354 | * Output parameters: |
---|
355 | * NONE |
---|
356 | * |
---|
357 | */ |
---|
358 | static void |
---|
359 | task_pool_done(int count) |
---|
360 | { |
---|
361 | int i; |
---|
362 | for(i = 0; i < count; ++i) |
---|
363 | rtems_task_delete(task_pool.info[i].tid); |
---|
364 | if(task_pool.info) |
---|
365 | free(task_pool.info); |
---|
366 | if(task_pool.queue) |
---|
367 | free(task_pool.queue); |
---|
368 | if(task_pool.mutex != (rtems_id)-1) |
---|
369 | rtems_semaphore_delete(task_pool.mutex); |
---|
370 | if(task_pool.sem != (rtems_id)-1) |
---|
371 | rtems_semaphore_delete(task_pool.sem); |
---|
372 | task_pool.info = 0; |
---|
373 | task_pool.queue = 0; |
---|
374 | task_pool.count = 0; |
---|
375 | task_pool.sem = -1; |
---|
376 | task_pool.mutex = -1; |
---|
377 | } |
---|
378 | |
---|
379 | /* |
---|
380 | * task_pool_init |
---|
381 | * |
---|
382 | * Initialize task pool. |
---|
383 | * |
---|
384 | * Input parameters: |
---|
385 | * count - number of entries in task pool to create |
---|
386 | * priority - priority tasks are started with |
---|
387 | * |
---|
388 | * Output parameters: |
---|
389 | * returns 1 on success, 0 on failure. |
---|
390 | * |
---|
391 | */ |
---|
392 | static void session(rtems_task_argument arg); /* Forward declare */ |
---|
393 | |
---|
394 | static int |
---|
395 | task_pool_init(int count, rtems_task_priority priority) |
---|
396 | { |
---|
397 | int i; |
---|
398 | rtems_status_code sc; |
---|
399 | char id = 'a'; |
---|
400 | |
---|
401 | task_pool.count = 0; |
---|
402 | task_pool.head = task_pool.tail = 0; |
---|
403 | task_pool.mutex = (rtems_id)-1; |
---|
404 | task_pool.sem = (rtems_id)-1; |
---|
405 | |
---|
406 | sc = rtems_semaphore_create( |
---|
407 | rtems_build_name('F', 'T', 'P', 'M'), |
---|
408 | 1, |
---|
409 | RTEMS_DEFAULT_ATTRIBUTES |
---|
410 | | RTEMS_BINARY_SEMAPHORE |
---|
411 | | RTEMS_INHERIT_PRIORITY |
---|
412 | | RTEMS_PRIORITY, |
---|
413 | RTEMS_NO_PRIORITY, |
---|
414 | &task_pool.mutex); |
---|
415 | |
---|
416 | if(sc == RTEMS_SUCCESSFUL) |
---|
417 | sc = rtems_semaphore_create( |
---|
418 | rtems_build_name('F', 'T', 'P', 'S'), |
---|
419 | count, |
---|
420 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
421 | RTEMS_NO_PRIORITY, |
---|
422 | &task_pool.sem); |
---|
423 | |
---|
424 | if(sc != RTEMS_SUCCESSFUL) { |
---|
425 | task_pool_done(0); |
---|
426 | syslog(LOG_ERR, "ftpd: Can not create semaphores"); |
---|
427 | return 0; |
---|
428 | } |
---|
429 | |
---|
430 | task_pool.info = (FTPD_SessionInfo_t*) |
---|
431 | malloc(sizeof(FTPD_SessionInfo_t) * count); |
---|
432 | task_pool.queue = (FTPD_SessionInfo_t**) |
---|
433 | malloc(sizeof(FTPD_SessionInfo_t*) * count); |
---|
434 | if (NULL == task_pool.info || NULL == task_pool.queue) |
---|
435 | { |
---|
436 | task_pool_done(0); |
---|
437 | syslog(LOG_ERR, "ftpd: Not enough memory"); |
---|
438 | return 0; |
---|
439 | } |
---|
440 | |
---|
441 | for(i = 0; i < count; ++i) |
---|
442 | { |
---|
443 | FTPD_SessionInfo_t *info = &task_pool.info[i]; |
---|
444 | sc = rtems_task_create(rtems_build_name('F', 'T', 'P', id), |
---|
445 | priority, FTPD_STACKSIZE, |
---|
446 | RTEMS_PREEMPT | RTEMS_NO_TIMESLICE | |
---|
447 | RTEMS_NO_ASR | RTEMS_INTERRUPT_LEVEL(0), |
---|
448 | RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL, |
---|
449 | &info->tid); |
---|
450 | if (sc == RTEMS_SUCCESSFUL) |
---|
451 | { |
---|
452 | sc = rtems_task_start( |
---|
453 | info->tid, session, (rtems_task_argument)info); |
---|
454 | if (sc != RTEMS_SUCCESSFUL) |
---|
455 | task_pool_done(i); |
---|
456 | } |
---|
457 | else |
---|
458 | task_pool_done(i + 1); |
---|
459 | if (sc != RTEMS_SUCCESSFUL) |
---|
460 | { |
---|
461 | syslog(LOG_ERR, "ftpd: Could not create/start FTPD session: %s", |
---|
462 | rtems_status_text(sc)); |
---|
463 | return 0; |
---|
464 | } |
---|
465 | task_pool.queue[i] = task_pool.info + i; |
---|
466 | if (++id > 'z') |
---|
467 | id = 'a'; |
---|
468 | } |
---|
469 | task_pool.count = count; |
---|
470 | return 1; |
---|
471 | } |
---|
472 | |
---|
473 | /* |
---|
474 | * task_pool_obtain |
---|
475 | * |
---|
476 | * Obtain free task from task pool. |
---|
477 | * |
---|
478 | * Input parameters: |
---|
479 | * NONE |
---|
480 | * |
---|
481 | * Output parameters: |
---|
482 | * returns pointer to the corresponding SessionInfo structure on success, |
---|
483 | * NULL if there are no free tasks in the pool. |
---|
484 | * |
---|
485 | */ |
---|
486 | static FTPD_SessionInfo_t* |
---|
487 | task_pool_obtain(void) |
---|
488 | { |
---|
489 | FTPD_SessionInfo_t* info = 0; |
---|
490 | rtems_status_code sc; |
---|
491 | sc = rtems_semaphore_obtain(task_pool.sem, RTEMS_NO_WAIT, RTEMS_NO_TIMEOUT); |
---|
492 | if (sc == RTEMS_SUCCESSFUL) |
---|
493 | { |
---|
494 | rtems_semaphore_obtain(task_pool.mutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
495 | info = task_pool.queue[task_pool.head]; |
---|
496 | if(++task_pool.head >= task_pool.count) |
---|
497 | task_pool.head = 0; |
---|
498 | rtems_semaphore_release(task_pool.mutex); |
---|
499 | } |
---|
500 | return info; |
---|
501 | } |
---|
502 | |
---|
503 | /* |
---|
504 | * task_pool_release |
---|
505 | * |
---|
506 | * Return task obtained by 'obtain()' back to the task pool. |
---|
507 | * |
---|
508 | * Input parameters: |
---|
509 | * info - pointer to corresponding SessionInfo structure. |
---|
510 | * |
---|
511 | * Output parameters: |
---|
512 | * NONE |
---|
513 | * |
---|
514 | */ |
---|
515 | static void |
---|
516 | task_pool_release(FTPD_SessionInfo_t* info) |
---|
517 | { |
---|
518 | rtems_semaphore_obtain(task_pool.mutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
519 | task_pool.queue[task_pool.tail] = info; |
---|
520 | if(++task_pool.tail >= task_pool.count) |
---|
521 | task_pool.tail = 0; |
---|
522 | rtems_semaphore_release(task_pool.mutex); |
---|
523 | rtems_semaphore_release(task_pool.sem); |
---|
524 | } |
---|
525 | |
---|
526 | /* |
---|
527 | * End of task pool routines |
---|
528 | */ |
---|
529 | |
---|
530 | /* |
---|
531 | * Function: send_reply |
---|
532 | * |
---|
533 | * |
---|
534 | * This procedure sends a reply to the client via the control |
---|
535 | * connection. |
---|
536 | * |
---|
537 | * |
---|
538 | * Input parameters: |
---|
539 | * code - 3-digit reply code. |
---|
540 | * text - Reply text. |
---|
541 | * |
---|
542 | * Output parameters: |
---|
543 | * NONE |
---|
544 | */ |
---|
545 | static void |
---|
546 | send_reply(FTPD_SessionInfo_t *info, int code, char *text) |
---|
547 | { |
---|
548 | text = text != NULL ? text : ""; |
---|
549 | fprintf(info->ctrl_fp, "%d %.70s\r\n", code, text); |
---|
550 | fflush(info->ctrl_fp); |
---|
551 | } |
---|
552 | |
---|
553 | |
---|
554 | /* |
---|
555 | * close_socket |
---|
556 | * |
---|
557 | * Close socket. |
---|
558 | * |
---|
559 | * Input parameters: |
---|
560 | * s - socket descriptor. |
---|
561 | * seconds - number of seconds the timeout should be, |
---|
562 | * if >= 0 - infinite timeout (no timeout). |
---|
563 | * |
---|
564 | * Output parameters: |
---|
565 | * returns 1 on success, 0 on failure. |
---|
566 | */ |
---|
567 | static int |
---|
568 | set_socket_timeout(int s, int seconds) |
---|
569 | { |
---|
570 | int res = 0; |
---|
571 | struct timeval tv; |
---|
572 | int len = sizeof(tv); |
---|
573 | |
---|
574 | if(seconds < 0) |
---|
575 | seconds = 0; |
---|
576 | tv.tv_usec = 0; |
---|
577 | tv.tv_sec = seconds; |
---|
578 | if(0 != setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &tv, len)) |
---|
579 | syslog(LOG_ERR, "ftpd: Can't set send timeout on socket: %s.", serr()); |
---|
580 | else if(0 != setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, len)) |
---|
581 | syslog(LOG_ERR, "ftpd: Can't set receive timeout on socket: %s.", serr()); |
---|
582 | else |
---|
583 | res = 1; |
---|
584 | return res; |
---|
585 | } |
---|
586 | |
---|
587 | /* |
---|
588 | * close_socket |
---|
589 | * |
---|
590 | * Close socket. |
---|
591 | * |
---|
592 | * Input parameters: |
---|
593 | * s - socket descriptor to be closed. |
---|
594 | * |
---|
595 | * Output parameters: |
---|
596 | * returns 1 on success, 0 on failure |
---|
597 | */ |
---|
598 | static int |
---|
599 | close_socket(int s) |
---|
600 | { |
---|
601 | if (0 <= s) |
---|
602 | { |
---|
603 | if (0 != close(s)) |
---|
604 | { |
---|
605 | shutdown(s, 2); |
---|
606 | if (0 != close(s)) |
---|
607 | return 0; |
---|
608 | } |
---|
609 | } |
---|
610 | return 1; |
---|
611 | } |
---|
612 | |
---|
613 | /* |
---|
614 | * data_socket |
---|
615 | * |
---|
616 | * Create data socket for session. |
---|
617 | * |
---|
618 | * Input parameters: |
---|
619 | * info - corresponding SessionInfo structure |
---|
620 | * |
---|
621 | * Output parameters: |
---|
622 | * returns socket descriptor, or -1 if failure |
---|
623 | * |
---|
624 | */ |
---|
625 | static int |
---|
626 | data_socket(FTPD_SessionInfo_t *info) |
---|
627 | { |
---|
628 | int s = info->pasv_socket; |
---|
629 | if(0 > s) |
---|
630 | { |
---|
631 | int on = 1; |
---|
632 | s = socket(PF_INET, SOCK_STREAM, 0); |
---|
633 | if(0 > s) |
---|
634 | send_reply(info, 425, "Can't create data socket."); |
---|
635 | else if(0 > setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) |
---|
636 | { |
---|
637 | close_socket(s); |
---|
638 | s = -1; |
---|
639 | } |
---|
640 | else |
---|
641 | { |
---|
642 | struct sockaddr_in data_source; |
---|
643 | int tries; |
---|
644 | |
---|
645 | /* anchor socket to avoid multi-homing problems */ |
---|
646 | data_source = info->ctrl_addr; |
---|
647 | data_source.sin_port = htons(20); /* ftp-data port */ |
---|
648 | for(tries = 1; tries < 10; ++tries) |
---|
649 | { |
---|
650 | errno = 0; |
---|
651 | if(bind(s, (struct sockaddr *)&data_source, sizeof(data_source)) >= 0) |
---|
652 | break; |
---|
653 | if (errno != EADDRINUSE) |
---|
654 | tries = 10; |
---|
655 | else |
---|
656 | rtems_task_wake_after(tries * 10); |
---|
657 | } |
---|
658 | if(tries >= 10) |
---|
659 | { |
---|
660 | send_reply(info, 425, "Can't bind data socket."); |
---|
661 | close_socket(s); |
---|
662 | s = -1; |
---|
663 | } |
---|
664 | else |
---|
665 | { |
---|
666 | struct sockaddr_in *data_dest = |
---|
667 | (info->use_default) ? &info->def_addr : &info->data_addr; |
---|
668 | if(0 > connect(s, (struct sockaddr *)data_dest, sizeof(*data_dest))) |
---|
669 | { |
---|
670 | send_reply(info, 425, "Can't connect data socket."); |
---|
671 | close_socket(s); |
---|
672 | s = -1; |
---|
673 | } |
---|
674 | } |
---|
675 | } |
---|
676 | } |
---|
677 | info->data_socket = s; |
---|
678 | info->use_default = 1; |
---|
679 | if(s >= 0) |
---|
680 | set_socket_timeout(s, info->idle); |
---|
681 | return s; |
---|
682 | } |
---|
683 | |
---|
684 | /* |
---|
685 | * close_data_socket |
---|
686 | * |
---|
687 | * Close data socket for session. |
---|
688 | * |
---|
689 | * Input parameters: |
---|
690 | * info - corresponding SessionInfo structure |
---|
691 | * |
---|
692 | * Output parameters: |
---|
693 | * NONE |
---|
694 | * |
---|
695 | */ |
---|
696 | static void |
---|
697 | close_data_socket(FTPD_SessionInfo_t *info) |
---|
698 | { |
---|
699 | /* As at most one data socket could be open simultaneously and in some cases |
---|
700 | data_socket == pasv_socket, we select socket to close, then close it. */ |
---|
701 | int s = info->data_socket; |
---|
702 | if(0 > s) |
---|
703 | s = info->pasv_socket; |
---|
704 | if(!close_socket(s)) |
---|
705 | syslog(LOG_ERR, "ftpd: Error closing data socket."); |
---|
706 | info->data_socket = -1; |
---|
707 | info->pasv_socket = -1; |
---|
708 | info->use_default = 1; |
---|
709 | } |
---|
710 | |
---|
711 | /* |
---|
712 | * close_stream |
---|
713 | * |
---|
714 | * Close control stream of session. |
---|
715 | * |
---|
716 | * Input parameters: |
---|
717 | * info - corresponding SessionInfo structure |
---|
718 | * |
---|
719 | * Output parameters: |
---|
720 | * NONE |
---|
721 | * |
---|
722 | */ |
---|
723 | static void |
---|
724 | close_stream(FTPD_SessionInfo_t* info) |
---|
725 | { |
---|
726 | if (NULL != info->ctrl_fp) |
---|
727 | { |
---|
728 | if (0 != fclose(info->ctrl_fp)) |
---|
729 | { |
---|
730 | syslog(LOG_ERR, "ftpd: Could not close control stream: %s", serr()); |
---|
731 | } |
---|
732 | else |
---|
733 | info->ctrl_socket = -1; |
---|
734 | } |
---|
735 | |
---|
736 | if (!close_socket(info->ctrl_socket)) |
---|
737 | syslog(LOG_ERR, "ftpd: Could not close control socket: %s", serr()); |
---|
738 | |
---|
739 | info->ctrl_fp = NULL; |
---|
740 | info->ctrl_socket = -1; |
---|
741 | } |
---|
742 | |
---|
743 | |
---|
744 | /* |
---|
745 | * send_mode_reply |
---|
746 | * |
---|
747 | * Sends BINARY/ASCII reply string depending on current transfer mode. |
---|
748 | * |
---|
749 | * Input parameters: |
---|
750 | * info - corresponding SessionInfo structure |
---|
751 | * |
---|
752 | * Output parameters: |
---|
753 | * NONE |
---|
754 | * |
---|
755 | */ |
---|
756 | static void |
---|
757 | send_mode_reply(FTPD_SessionInfo_t *info) |
---|
758 | { |
---|
759 | if(info->xfer_mode == TYPE_I) |
---|
760 | send_reply(info, 150, "Opening BINARY mode data connection."); |
---|
761 | else |
---|
762 | send_reply(info, 150, "Opening ASCII mode data connection."); |
---|
763 | } |
---|
764 | |
---|
765 | /* |
---|
766 | * command_retrieve |
---|
767 | * |
---|
768 | * Perform the "RETR" command (send file to client). |
---|
769 | * |
---|
770 | * Input parameters: |
---|
771 | * info - corresponding SessionInfo structure |
---|
772 | * char *filename - source filename. |
---|
773 | * |
---|
774 | * Output parameters: |
---|
775 | * NONE |
---|
776 | * |
---|
777 | */ |
---|
778 | static void |
---|
779 | command_retrieve(FTPD_SessionInfo_t *info, char const *filename) |
---|
780 | { |
---|
781 | int s = -1; |
---|
782 | int fd = -1; |
---|
783 | char buf[FTPD_DATASIZE]; |
---|
784 | struct stat stat_buf; |
---|
785 | int res = 0; |
---|
786 | |
---|
787 | if(!can_read() || !info->auth) |
---|
788 | { |
---|
789 | send_reply(info, 550, "Access denied."); |
---|
790 | return; |
---|
791 | } |
---|
792 | |
---|
793 | if (0 > (fd = open(filename, O_RDONLY))) |
---|
794 | { |
---|
795 | send_reply(info, 550, "Error opening file."); |
---|
796 | return; |
---|
797 | } |
---|
798 | |
---|
799 | if (fstat(fd, &stat_buf) == 0 && S_ISDIR(stat_buf.st_mode)) |
---|
800 | { |
---|
801 | if (-1 != fd) |
---|
802 | close(fd); |
---|
803 | send_reply(info, 550, "Is a directory."); |
---|
804 | return; |
---|
805 | } |
---|
806 | |
---|
807 | send_mode_reply(info); |
---|
808 | |
---|
809 | s = data_socket(info); |
---|
810 | |
---|
811 | if (0 <= s) |
---|
812 | { |
---|
813 | int n = -1; |
---|
814 | |
---|
815 | if(info->xfer_mode == TYPE_I) |
---|
816 | { |
---|
817 | while ((n = read(fd, buf, FTPD_DATASIZE)) > 0) |
---|
818 | { |
---|
819 | if(send(s, buf, n, 0) != n) |
---|
820 | break; |
---|
821 | } |
---|
822 | } |
---|
823 | else if (info->xfer_mode == TYPE_A) |
---|
824 | { |
---|
825 | int rest = 0; |
---|
826 | while (rest == 0 && (n = read(fd, buf, FTPD_DATASIZE)) > 0) |
---|
827 | { |
---|
828 | char const* e = buf; |
---|
829 | char const* b; |
---|
830 | int i; |
---|
831 | rest = n; |
---|
832 | do |
---|
833 | { |
---|
834 | char lf = '\0'; |
---|
835 | |
---|
836 | b = e; |
---|
837 | for(i = 0; i < rest; ++i, ++e) |
---|
838 | { |
---|
839 | if(*e == '\n') |
---|
840 | { |
---|
841 | lf = '\n'; |
---|
842 | break; |
---|
843 | } |
---|
844 | } |
---|
845 | if(send(s, b, i, 0) != i) |
---|
846 | break; |
---|
847 | if(lf == '\n') |
---|
848 | { |
---|
849 | if(send(s, "\r\n", 2, 0) != 2) |
---|
850 | break; |
---|
851 | ++e; |
---|
852 | ++i; |
---|
853 | } |
---|
854 | } |
---|
855 | while((rest -= i) > 0); |
---|
856 | } |
---|
857 | } |
---|
858 | |
---|
859 | if (0 == n) |
---|
860 | { |
---|
861 | if (0 == close(fd)) |
---|
862 | { |
---|
863 | fd = -1; |
---|
864 | res = 1; |
---|
865 | } |
---|
866 | } |
---|
867 | } |
---|
868 | |
---|
869 | if (-1 != fd) |
---|
870 | close(fd); |
---|
871 | |
---|
872 | if (0 == res) |
---|
873 | send_reply(info, 451, "File read error."); |
---|
874 | else |
---|
875 | send_reply(info, 226, "Transfer complete."); |
---|
876 | |
---|
877 | close_data_socket(info); |
---|
878 | |
---|
879 | return; |
---|
880 | } |
---|
881 | |
---|
882 | |
---|
883 | /* |
---|
884 | * discard |
---|
885 | * |
---|
886 | * Analog of `write' routine that just discards passed data |
---|
887 | * |
---|
888 | * Input parameters: |
---|
889 | * fd - file descriptor (ignored) |
---|
890 | * buf - data to write (ignored) |
---|
891 | * count - number of bytes in `buf' |
---|
892 | * |
---|
893 | * Output parameters: |
---|
894 | * returns `count' |
---|
895 | * |
---|
896 | */ |
---|
897 | static ssize_t |
---|
898 | discard(int fd, void const* buf, size_t count) |
---|
899 | { |
---|
900 | (void)fd; |
---|
901 | (void)buf; |
---|
902 | return count; |
---|
903 | } |
---|
904 | |
---|
905 | /* |
---|
906 | * command_store |
---|
907 | * |
---|
908 | * Performs the "STOR" command (receive data from client). |
---|
909 | * |
---|
910 | * Input parameters: |
---|
911 | * info - corresponding SessionInfo structure |
---|
912 | * char *filename - Destination filename. |
---|
913 | * |
---|
914 | * Output parameters: |
---|
915 | * NONE |
---|
916 | */ |
---|
917 | static void |
---|
918 | command_store(FTPD_SessionInfo_t *info, char const *filename) |
---|
919 | { |
---|
920 | int s; |
---|
921 | int n; |
---|
922 | unsigned long size = 0; |
---|
923 | struct rtems_ftpd_hook *usehook = NULL; |
---|
924 | char buf[FTPD_DATASIZE]; |
---|
925 | int res = 1; |
---|
926 | int bare_lfs = 0; |
---|
927 | int null = 0; |
---|
928 | typedef ssize_t (*WriteProc)(int, void const*, size_t); |
---|
929 | WriteProc wrt = &write; |
---|
930 | |
---|
931 | if(!can_write() || !info->auth) |
---|
932 | { |
---|
933 | send_reply(info, 550, "Access denied."); |
---|
934 | return; |
---|
935 | } |
---|
936 | |
---|
937 | send_mode_reply(info); |
---|
938 | |
---|
939 | s = data_socket(info); |
---|
940 | if(0 > s) |
---|
941 | return; |
---|
942 | |
---|
943 | null = !strcmp("/dev/null", filename); |
---|
944 | if (null) |
---|
945 | { |
---|
946 | /* File "/dev/null" just throws data away. |
---|
947 | * FIXME: this is hack. Using `/dev/null' filesystem entry would be |
---|
948 | * better. |
---|
949 | */ |
---|
950 | wrt = &discard; |
---|
951 | } |
---|
952 | |
---|
953 | if (!null && rtems_ftpd_configuration.hooks != NULL) |
---|
954 | { |
---|
955 | |
---|
956 | /* Search our list of hooks to see if we need to do something special. */ |
---|
957 | struct rtems_ftpd_hook *hook; |
---|
958 | int i; |
---|
959 | |
---|
960 | i = 0; |
---|
961 | hook = &rtems_ftpd_configuration.hooks[i++]; |
---|
962 | while (hook->filename != NULL) |
---|
963 | { |
---|
964 | if (!strcmp(hook->filename, filename)) |
---|
965 | { |
---|
966 | usehook = hook; |
---|
967 | break; |
---|
968 | } |
---|
969 | hook = &rtems_ftpd_configuration.hooks[i++]; |
---|
970 | } |
---|
971 | } |
---|
972 | |
---|
973 | if (usehook != NULL) |
---|
974 | { |
---|
975 | /* |
---|
976 | * OSV: FIXME: Small buffer could be used and hook routine |
---|
977 | * called multiple times instead. Alternatively, the support could be |
---|
978 | * removed entirely in favor of configuring RTEMS pseudo-device with |
---|
979 | * given name. |
---|
980 | */ |
---|
981 | |
---|
982 | char *bigBufr; |
---|
983 | size_t filesize = rtems_ftpd_configuration.max_hook_filesize + 1; |
---|
984 | |
---|
985 | /* |
---|
986 | * Allocate space for our "file". |
---|
987 | */ |
---|
988 | bigBufr = (char *)malloc(filesize); |
---|
989 | if (bigBufr == NULL) |
---|
990 | { |
---|
991 | send_reply(info, 451, "Local resource failure: malloc."); |
---|
992 | close_data_socket(info); |
---|
993 | return; |
---|
994 | } |
---|
995 | |
---|
996 | /* |
---|
997 | * Retrieve the file into our buffer space. |
---|
998 | */ |
---|
999 | size = 0; |
---|
1000 | while ((n = recv(s, bigBufr + size, filesize - size, 0)) > 0) |
---|
1001 | { |
---|
1002 | size += n; |
---|
1003 | } |
---|
1004 | if (size >= filesize) |
---|
1005 | { |
---|
1006 | send_reply(info, 451, "File too long: buffer size exceeded."); |
---|
1007 | free(bigBufr); |
---|
1008 | close_data_socket(info); |
---|
1009 | return; |
---|
1010 | } |
---|
1011 | |
---|
1012 | /* |
---|
1013 | * Call our hook. |
---|
1014 | */ |
---|
1015 | res = (usehook->hook_function)(bigBufr, size) == 0; |
---|
1016 | free(bigBufr); |
---|
1017 | if(!res) |
---|
1018 | { |
---|
1019 | send_reply(info, 451, "File processing failed."); |
---|
1020 | close_data_socket(info); |
---|
1021 | return; |
---|
1022 | } |
---|
1023 | } |
---|
1024 | else |
---|
1025 | { |
---|
1026 | /* Data transfer to regular file or /dev/null. */ |
---|
1027 | int fd = 0; |
---|
1028 | |
---|
1029 | if(!null) |
---|
1030 | fd = creat(filename, |
---|
1031 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
---|
1032 | |
---|
1033 | if (0 > fd) |
---|
1034 | { |
---|
1035 | send_reply(info, 550, "Error creating file."); |
---|
1036 | close_data_socket(info); |
---|
1037 | return; |
---|
1038 | } |
---|
1039 | |
---|
1040 | if(info->xfer_mode == TYPE_I) |
---|
1041 | { |
---|
1042 | while ((n = recv(s, buf, FTPD_DATASIZE, 0)) > 0) |
---|
1043 | { |
---|
1044 | if (wrt(fd, buf, n) != n) |
---|
1045 | { |
---|
1046 | res = 0; |
---|
1047 | break; |
---|
1048 | } |
---|
1049 | } |
---|
1050 | } |
---|
1051 | else if(info->xfer_mode == TYPE_A) |
---|
1052 | { |
---|
1053 | int rest = 0; |
---|
1054 | int pended_cr = 0; |
---|
1055 | while (res && rest == 0 && (n = recv(s, buf, FTPD_DATASIZE, 0)) > 0) |
---|
1056 | { |
---|
1057 | char const* e = buf; |
---|
1058 | char const* b; |
---|
1059 | int i; |
---|
1060 | |
---|
1061 | rest = n; |
---|
1062 | if(pended_cr && *e != '\n') |
---|
1063 | { |
---|
1064 | char const lf = '\r'; |
---|
1065 | pended_cr = 0; |
---|
1066 | if(wrt(fd, &lf, 1) != 1) |
---|
1067 | { |
---|
1068 | res = 0; |
---|
1069 | break; |
---|
1070 | } |
---|
1071 | } |
---|
1072 | do |
---|
1073 | { |
---|
1074 | int count; |
---|
1075 | int sub = 0; |
---|
1076 | |
---|
1077 | b = e; |
---|
1078 | for(i = 0; i < rest; ++i, ++e) |
---|
1079 | { |
---|
1080 | int pcr = pended_cr; |
---|
1081 | pended_cr = 0; |
---|
1082 | if(*e == '\r') |
---|
1083 | { |
---|
1084 | pended_cr = 1; |
---|
1085 | } |
---|
1086 | else if(*e == '\n') |
---|
1087 | { |
---|
1088 | if(pcr) |
---|
1089 | { |
---|
1090 | sub = 2; |
---|
1091 | ++i; |
---|
1092 | ++e; |
---|
1093 | break; |
---|
1094 | } |
---|
1095 | ++bare_lfs; |
---|
1096 | } |
---|
1097 | } |
---|
1098 | if(res == 0) |
---|
1099 | break; |
---|
1100 | count = i - sub - pended_cr; |
---|
1101 | if(count > 0 && wrt(fd, b, count) != count) |
---|
1102 | { |
---|
1103 | res = 0; |
---|
1104 | break; |
---|
1105 | } |
---|
1106 | if(sub == 2 && wrt(fd, e - 1, 1) != 1) |
---|
1107 | res = 0; |
---|
1108 | } |
---|
1109 | while((rest -= i) > 0); |
---|
1110 | } |
---|
1111 | } |
---|
1112 | |
---|
1113 | if (0 > close(fd) || res == 0) |
---|
1114 | { |
---|
1115 | send_reply(info, 452, "Error writing file."); |
---|
1116 | close_data_socket(info); |
---|
1117 | return; |
---|
1118 | } |
---|
1119 | } |
---|
1120 | |
---|
1121 | if (bare_lfs > 0) |
---|
1122 | { |
---|
1123 | snprintf(buf, FTPD_BUFSIZE, |
---|
1124 | "Transfer complete. WARNING! %d bare linefeeds received in ASCII mode.", |
---|
1125 | bare_lfs); |
---|
1126 | send_reply(info, 226, buf); |
---|
1127 | } |
---|
1128 | else |
---|
1129 | send_reply(info, 226, "Transfer complete."); |
---|
1130 | close_data_socket(info); |
---|
1131 | |
---|
1132 | } |
---|
1133 | |
---|
1134 | |
---|
1135 | /* |
---|
1136 | * send_dirline |
---|
1137 | * |
---|
1138 | * Sends one line of LIST command reply corresponding to single file. |
---|
1139 | * |
---|
1140 | * Input parameters: |
---|
1141 | * s - socket descriptor to send data to |
---|
1142 | * wide - if 0, send only file name. If not 0, send 'stat' info as well in |
---|
1143 | * "ls -l" format. |
---|
1144 | * curTime - current time |
---|
1145 | * path - path to be prepended to what is given by 'add' |
---|
1146 | * add - path to be appended to what is given by 'path', the resulting path |
---|
1147 | * is then passed to 'stat()' routine |
---|
1148 | * name - file name to be reported in output |
---|
1149 | * buf - buffer for temporary data |
---|
1150 | * |
---|
1151 | * Output parameters: |
---|
1152 | * returns 0 on failure, 1 on success |
---|
1153 | * |
---|
1154 | */ |
---|
1155 | static int |
---|
1156 | send_dirline(int s, int wide, time_t curTime, char const* path, |
---|
1157 | char const* add, char const* fname, char* buf) |
---|
1158 | { |
---|
1159 | if(wide) |
---|
1160 | { |
---|
1161 | struct stat stat_buf; |
---|
1162 | |
---|
1163 | int plen = strlen(path); |
---|
1164 | int alen = strlen(add); |
---|
1165 | if(plen == 0) |
---|
1166 | { |
---|
1167 | buf[plen++] = '/'; |
---|
1168 | buf[plen] = '\0'; |
---|
1169 | } |
---|
1170 | else |
---|
1171 | { |
---|
1172 | strcpy(buf, path); |
---|
1173 | if(alen > 0 && buf[plen - 1] != '/') |
---|
1174 | { |
---|
1175 | buf[plen++] = '/'; |
---|
1176 | if(plen >= FTPD_BUFSIZE) |
---|
1177 | return 0; |
---|
1178 | buf[plen] = '\0'; |
---|
1179 | } |
---|
1180 | } |
---|
1181 | if(plen + alen >= FTPD_BUFSIZE) |
---|
1182 | return 0; |
---|
1183 | strcpy(buf + plen, add); |
---|
1184 | |
---|
1185 | if (stat(buf, &stat_buf) == 0) |
---|
1186 | { |
---|
1187 | int len; |
---|
1188 | struct tm bt; |
---|
1189 | time_t tf = stat_buf.st_mtime; |
---|
1190 | enum { SIZE = 80 }; |
---|
1191 | time_t SIX_MONTHS = (365L*24L*60L*60L)/2L; |
---|
1192 | char timeBuf[SIZE]; |
---|
1193 | gmtime_r(&tf, &bt); |
---|
1194 | if(curTime > tf + SIX_MONTHS || tf > curTime + SIX_MONTHS) |
---|
1195 | strftime (timeBuf, SIZE, "%b %d %Y", &bt); |
---|
1196 | else |
---|
1197 | strftime (timeBuf, SIZE, "%b %d %H:%M", &bt); |
---|
1198 | |
---|
1199 | len = snprintf(buf, FTPD_BUFSIZE, |
---|
1200 | "%c%c%c%c%c%c%c%c%c%c 1 %5d %5d %11u %s %s\r\n", |
---|
1201 | (S_ISLNK(stat_buf.st_mode)?('l'): |
---|
1202 | (S_ISDIR(stat_buf.st_mode)?('d'):('-'))), |
---|
1203 | (stat_buf.st_mode & S_IRUSR)?('r'):('-'), |
---|
1204 | (stat_buf.st_mode & S_IWUSR)?('w'):('-'), |
---|
1205 | (stat_buf.st_mode & S_IXUSR)?('x'):('-'), |
---|
1206 | (stat_buf.st_mode & S_IRGRP)?('r'):('-'), |
---|
1207 | (stat_buf.st_mode & S_IWGRP)?('w'):('-'), |
---|
1208 | (stat_buf.st_mode & S_IXGRP)?('x'):('-'), |
---|
1209 | (stat_buf.st_mode & S_IROTH)?('r'):('-'), |
---|
1210 | (stat_buf.st_mode & S_IWOTH)?('w'):('-'), |
---|
1211 | (stat_buf.st_mode & S_IXOTH)?('x'):('-'), |
---|
1212 | (int)stat_buf.st_uid, |
---|
1213 | (int)stat_buf.st_gid, |
---|
1214 | (int)stat_buf.st_size, |
---|
1215 | timeBuf, |
---|
1216 | fname |
---|
1217 | ); |
---|
1218 | |
---|
1219 | if(send(s, buf, len, 0) != len) |
---|
1220 | return 0; |
---|
1221 | } |
---|
1222 | } |
---|
1223 | else |
---|
1224 | { |
---|
1225 | int len = snprintf(buf, FTPD_BUFSIZE, "%s\r\n", fname); |
---|
1226 | if(send(s, buf, len, 0) != len) |
---|
1227 | return 0; |
---|
1228 | } |
---|
1229 | return 1; |
---|
1230 | } |
---|
1231 | |
---|
1232 | /* |
---|
1233 | * command_list |
---|
1234 | * |
---|
1235 | * Send file list to client. |
---|
1236 | * |
---|
1237 | * Input parameters: |
---|
1238 | * info - corresponding SessionInfo structure |
---|
1239 | * char *fname - File (or directory) to list. |
---|
1240 | * |
---|
1241 | * Output parameters: |
---|
1242 | * NONE |
---|
1243 | */ |
---|
1244 | static void |
---|
1245 | command_list(FTPD_SessionInfo_t *info, char const *fname, int wide) |
---|
1246 | { |
---|
1247 | int s; |
---|
1248 | DIR *dirp = 0; |
---|
1249 | struct dirent *dp = 0; |
---|
1250 | struct stat stat_buf; |
---|
1251 | char buf[FTPD_BUFSIZE]; |
---|
1252 | time_t curTime; |
---|
1253 | int sc = 1; |
---|
1254 | |
---|
1255 | if(!info->auth) |
---|
1256 | { |
---|
1257 | send_reply(info, 550, "Access denied."); |
---|
1258 | return; |
---|
1259 | } |
---|
1260 | |
---|
1261 | send_reply(info, 150, "Opening ASCII mode data connection for LIST."); |
---|
1262 | |
---|
1263 | s = data_socket(info); |
---|
1264 | if(0 > s) |
---|
1265 | { |
---|
1266 | syslog(LOG_ERR, "ftpd: Error connecting to data socket."); |
---|
1267 | return; |
---|
1268 | } |
---|
1269 | |
---|
1270 | if(fname[0] == '\0') |
---|
1271 | fname = "."; |
---|
1272 | |
---|
1273 | if (0 > stat(fname, &stat_buf)) |
---|
1274 | { |
---|
1275 | snprintf(buf, FTPD_BUFSIZE, |
---|
1276 | "%s: No such file or directory.\r\n", fname); |
---|
1277 | send(s, buf, strlen(buf), 0); |
---|
1278 | } |
---|
1279 | else if (S_ISDIR(stat_buf.st_mode) && (NULL == (dirp = opendir(fname)))) |
---|
1280 | { |
---|
1281 | snprintf(buf, FTPD_BUFSIZE, |
---|
1282 | "%s: Can not open directory.\r\n", fname); |
---|
1283 | send(s, buf, strlen(buf), 0); |
---|
1284 | } |
---|
1285 | else |
---|
1286 | { |
---|
1287 | time(&curTime); |
---|
1288 | if(!dirp && *fname) |
---|
1289 | sc = sc && send_dirline(s, wide, curTime, fname, "", fname, buf); |
---|
1290 | else { |
---|
1291 | /* FIXME: need "." and ".." only when '-a' option is given */ |
---|
1292 | sc = sc && send_dirline(s, wide, curTime, fname, "", ".", buf); |
---|
1293 | sc = sc && send_dirline(s, wide, curTime, fname, |
---|
1294 | (strcmp(fname, ftpd_root) ? ".." : ""), "..", buf); |
---|
1295 | while (sc && (dp = readdir(dirp)) != NULL) |
---|
1296 | sc = sc && |
---|
1297 | send_dirline(s, wide, curTime, fname, dp->d_name, dp->d_name, buf); |
---|
1298 | } |
---|
1299 | } |
---|
1300 | |
---|
1301 | if(dirp) |
---|
1302 | closedir(dirp); |
---|
1303 | close_data_socket(info); |
---|
1304 | |
---|
1305 | if(sc) |
---|
1306 | send_reply(info, 226, "Transfer complete."); |
---|
1307 | else |
---|
1308 | send_reply(info, 426, "Connection aborted."); |
---|
1309 | } |
---|
1310 | |
---|
1311 | |
---|
1312 | /* |
---|
1313 | * command_cwd |
---|
1314 | * |
---|
1315 | * Change current working directory. |
---|
1316 | * |
---|
1317 | * Input parameters: |
---|
1318 | * info - corresponding SessionInfo structure |
---|
1319 | * dir - directory name passed in CWD command |
---|
1320 | * |
---|
1321 | * Output parameters: |
---|
1322 | * NONE |
---|
1323 | * |
---|
1324 | */ |
---|
1325 | static void |
---|
1326 | command_cwd(FTPD_SessionInfo_t *info, char *dir) |
---|
1327 | { |
---|
1328 | if(!info->auth) |
---|
1329 | { |
---|
1330 | send_reply(info, 550, "Access denied."); |
---|
1331 | return; |
---|
1332 | } |
---|
1333 | |
---|
1334 | if(chdir(dir) == 0) |
---|
1335 | send_reply(info, 250, "CWD command successful."); |
---|
1336 | else |
---|
1337 | send_reply(info, 550, "CWD command failed."); |
---|
1338 | } |
---|
1339 | |
---|
1340 | |
---|
1341 | /* |
---|
1342 | * command_pwd |
---|
1343 | * |
---|
1344 | * Send current working directory to client. |
---|
1345 | * |
---|
1346 | * Input parameters: |
---|
1347 | * info - corresponding SessionInfo structure |
---|
1348 | * |
---|
1349 | * Output parameters: |
---|
1350 | * NONE |
---|
1351 | */ |
---|
1352 | static void |
---|
1353 | command_pwd(FTPD_SessionInfo_t *info) |
---|
1354 | { |
---|
1355 | char buf[FTPD_BUFSIZE]; |
---|
1356 | char const* cwd; |
---|
1357 | errno = 0; |
---|
1358 | buf[0] = '"'; |
---|
1359 | |
---|
1360 | if(!info->auth) |
---|
1361 | { |
---|
1362 | send_reply(info, 550, "Access denied."); |
---|
1363 | return; |
---|
1364 | } |
---|
1365 | |
---|
1366 | cwd = getcwd(buf + 1, FTPD_BUFSIZE - 4); |
---|
1367 | if(cwd) |
---|
1368 | { |
---|
1369 | int len = strlen(cwd); |
---|
1370 | static char const txt[] = "\" is the current directory."; |
---|
1371 | int size = sizeof(txt); |
---|
1372 | if(len + size + 1 >= FTPD_BUFSIZE) |
---|
1373 | size = FTPD_BUFSIZE - len - 2; |
---|
1374 | memcpy(buf + len + 1, txt, size); |
---|
1375 | buf[len + size] = '\0'; |
---|
1376 | send_reply(info, 250, buf); |
---|
1377 | } |
---|
1378 | else { |
---|
1379 | snprintf(buf, FTPD_BUFSIZE, "Error: %s.", serr()); |
---|
1380 | send_reply(info, 452, buf); |
---|
1381 | } |
---|
1382 | } |
---|
1383 | |
---|
1384 | /* |
---|
1385 | * command_mdtm |
---|
1386 | * |
---|
1387 | * Handle FTP MDTM command (send file modification time to client)/ |
---|
1388 | * |
---|
1389 | * Input parameters: |
---|
1390 | * info - corresponding SessionInfo structure |
---|
1391 | * fname - file name passed in MDTM command |
---|
1392 | * |
---|
1393 | * Output parameters: |
---|
1394 | * info->cwd is set to new CWD value. |
---|
1395 | */ |
---|
1396 | static void |
---|
1397 | command_mdtm(FTPD_SessionInfo_t *info, char const* fname) |
---|
1398 | { |
---|
1399 | struct stat stbuf; |
---|
1400 | char buf[FTPD_BUFSIZE]; |
---|
1401 | |
---|
1402 | if(!info->auth) |
---|
1403 | { |
---|
1404 | send_reply(info, 550, "Access denied."); |
---|
1405 | return; |
---|
1406 | } |
---|
1407 | |
---|
1408 | if (0 > stat(fname, &stbuf)) |
---|
1409 | { |
---|
1410 | snprintf(buf, FTPD_BUFSIZE, "%s: %s.", fname, serr()); |
---|
1411 | send_reply(info, 550, buf); |
---|
1412 | } |
---|
1413 | else |
---|
1414 | { |
---|
1415 | struct tm *t = gmtime(&stbuf.st_mtime); |
---|
1416 | snprintf(buf, FTPD_BUFSIZE, "%04d%02d%02d%02d%02d%02d", |
---|
1417 | 1900 + t->tm_year, |
---|
1418 | t->tm_mon+1, t->tm_mday, |
---|
1419 | t->tm_hour, t->tm_min, t->tm_sec); |
---|
1420 | send_reply(info, 213, buf); |
---|
1421 | } |
---|
1422 | } |
---|
1423 | |
---|
1424 | /* |
---|
1425 | * command_port |
---|
1426 | * |
---|
1427 | * This procedure fills address for data connection given the IP address and |
---|
1428 | * port of the remote machine. |
---|
1429 | * |
---|
1430 | * Input parameters: |
---|
1431 | * info - corresponding SessionInfo structure |
---|
1432 | * args - arguments to the "PORT" command. |
---|
1433 | * |
---|
1434 | * Output parameters: |
---|
1435 | * info->data_addr is set according to arguments of the PORT command. |
---|
1436 | * info->use_default is set to 0 on success, 1 on failure. |
---|
1437 | */ |
---|
1438 | static void |
---|
1439 | command_port(FTPD_SessionInfo_t *info, char const *args) |
---|
1440 | { |
---|
1441 | enum { NUM_FIELDS = 6 }; |
---|
1442 | unsigned int a[NUM_FIELDS]; |
---|
1443 | int n; |
---|
1444 | |
---|
1445 | close_data_socket(info); |
---|
1446 | |
---|
1447 | n = sscanf(args, "%u,%u,%u,%u,%u,%u", a+0, a+1, a+2, a+3, a+4, a+5); |
---|
1448 | if(NUM_FIELDS == n) |
---|
1449 | { |
---|
1450 | int i; |
---|
1451 | union { |
---|
1452 | uint8_t b[NUM_FIELDS]; |
---|
1453 | struct { |
---|
1454 | uint32_t ip; |
---|
1455 | uint16_t port; |
---|
1456 | } u ; |
---|
1457 | } ip_info; |
---|
1458 | |
---|
1459 | for(i = 0; i < NUM_FIELDS; ++i) |
---|
1460 | { |
---|
1461 | if(a[i] > 255) |
---|
1462 | break; |
---|
1463 | ip_info.b[i] = (uint8_t)a[i]; |
---|
1464 | } |
---|
1465 | |
---|
1466 | if(i == NUM_FIELDS) |
---|
1467 | { |
---|
1468 | /* Note: while it contradicts with RFC959, we don't allow PORT command |
---|
1469 | * to specify IP address different than those of the originating client |
---|
1470 | * for the sake of safety. */ |
---|
1471 | if (ip_info.u.ip == info->def_addr.sin_addr.s_addr) |
---|
1472 | { |
---|
1473 | info->data_addr.sin_addr.s_addr = ip_info.u.ip; |
---|
1474 | info->data_addr.sin_port = ip_info.u.port; |
---|
1475 | info->data_addr.sin_family = AF_INET; |
---|
1476 | memset(info->data_addr.sin_zero, 0, sizeof(info->data_addr.sin_zero)); |
---|
1477 | |
---|
1478 | info->use_default = 0; |
---|
1479 | send_reply(info, 200, "PORT command successful."); |
---|
1480 | return; /* success */ |
---|
1481 | } |
---|
1482 | else |
---|
1483 | { |
---|
1484 | send_reply(info, 425, "Address doesn't match peer's IP."); |
---|
1485 | return; |
---|
1486 | } |
---|
1487 | } |
---|
1488 | } |
---|
1489 | send_reply(info, 501, "Syntax error."); |
---|
1490 | } |
---|
1491 | |
---|
1492 | |
---|
1493 | /* |
---|
1494 | * command_pasv |
---|
1495 | * |
---|
1496 | * Handle FTP PASV command. |
---|
1497 | * Open socket, listen for and accept connection on it. |
---|
1498 | * |
---|
1499 | * Input parameters: |
---|
1500 | * info - corresponding SessionInfo structure |
---|
1501 | * |
---|
1502 | * Output parameters: |
---|
1503 | * info->pasv_socket is set to the descriptor of the data socket |
---|
1504 | */ |
---|
1505 | static void |
---|
1506 | command_pasv(FTPD_SessionInfo_t *info) |
---|
1507 | { |
---|
1508 | int s = -1; |
---|
1509 | int err = 1; |
---|
1510 | |
---|
1511 | close_data_socket(info); |
---|
1512 | |
---|
1513 | s = socket(PF_INET, SOCK_STREAM, 0); |
---|
1514 | if (s < 0) |
---|
1515 | syslog(LOG_ERR, "ftpd: Error creating PASV socket: %s", serr()); |
---|
1516 | else |
---|
1517 | { |
---|
1518 | struct sockaddr_in addr; |
---|
1519 | socklen_t addrLen = sizeof(addr); |
---|
1520 | |
---|
1521 | addr = info->ctrl_addr; |
---|
1522 | addr.sin_port = htons(0); |
---|
1523 | |
---|
1524 | if (0 > bind(s, (struct sockaddr *)&addr, addrLen)) |
---|
1525 | syslog(LOG_ERR, "ftpd: Error binding PASV socket: %s", serr()); |
---|
1526 | else if (0 > listen(s, 1)) |
---|
1527 | syslog(LOG_ERR, "ftpd: Error listening on PASV socket: %s", serr()); |
---|
1528 | else if(set_socket_timeout(s, info->idle)) |
---|
1529 | { |
---|
1530 | char buf[FTPD_BUFSIZE]; |
---|
1531 | unsigned char const *ip, *p; |
---|
1532 | |
---|
1533 | getsockname(s, (struct sockaddr *)&addr, &addrLen); |
---|
1534 | ip = (unsigned char const*)&(addr.sin_addr); |
---|
1535 | p = (unsigned char const*)&(addr.sin_port); |
---|
1536 | snprintf(buf, FTPD_BUFSIZE, "Entering passive mode (%u,%u,%u,%u,%u,%u).", |
---|
1537 | ip[0], ip[1], ip[2], ip[3], p[0], p[1]); |
---|
1538 | send_reply(info, 227, buf); |
---|
1539 | |
---|
1540 | info->pasv_socket = accept(s, (struct sockaddr *)&addr, &addrLen); |
---|
1541 | if (0 > info->pasv_socket) |
---|
1542 | syslog(LOG_ERR, "ftpd: Error accepting PASV connection: %s", serr()); |
---|
1543 | else |
---|
1544 | { |
---|
1545 | close_socket(s); |
---|
1546 | s = -1; |
---|
1547 | err = 0; |
---|
1548 | } |
---|
1549 | } |
---|
1550 | } |
---|
1551 | if(err) |
---|
1552 | { |
---|
1553 | /* (OSV) The note is from FreeBSD FTPD. |
---|
1554 | * Note: a response of 425 is not mentioned as a possible response to |
---|
1555 | * the PASV command in RFC959. However, it has been blessed as a |
---|
1556 | * legitimate response by Jon Postel in a telephone conversation |
---|
1557 | * with Rick Adams on 25 Jan 89. */ |
---|
1558 | send_reply(info, 425, "Can't open passive connection."); |
---|
1559 | close_socket(s); |
---|
1560 | } |
---|
1561 | } |
---|
1562 | |
---|
1563 | |
---|
1564 | /* |
---|
1565 | * skip_options |
---|
1566 | * |
---|
1567 | * Utility routine to skip options (if any) from input command. |
---|
1568 | * |
---|
1569 | * Input parameters: |
---|
1570 | * p - pointer to pointer to command |
---|
1571 | * |
---|
1572 | * Output parameters: |
---|
1573 | * p - is changed to point to first non-option argument |
---|
1574 | */ |
---|
1575 | static void |
---|
1576 | skip_options(char **p) |
---|
1577 | { |
---|
1578 | char* buf = *p; |
---|
1579 | char* last = NULL; |
---|
1580 | while(1) { |
---|
1581 | while(isspace((unsigned char)*buf)) |
---|
1582 | ++buf; |
---|
1583 | if(*buf == '-') { |
---|
1584 | if(*++buf == '-') { /* `--' should terminate options */ |
---|
1585 | if(isspace((unsigned char)*++buf)) { |
---|
1586 | last = buf; |
---|
1587 | do ++buf; |
---|
1588 | while(isspace((unsigned char)*buf)); |
---|
1589 | break; |
---|
1590 | } |
---|
1591 | } |
---|
1592 | while(*buf && !isspace((unsigned char)*buf)) |
---|
1593 | ++buf; |
---|
1594 | last = buf; |
---|
1595 | } |
---|
1596 | else |
---|
1597 | break; |
---|
1598 | } |
---|
1599 | if(last) |
---|
1600 | *last = '\0'; |
---|
1601 | *p = buf; |
---|
1602 | } |
---|
1603 | |
---|
1604 | /* |
---|
1605 | * split_command |
---|
1606 | * |
---|
1607 | * Split command into command itself, options, and arguments. Command itself |
---|
1608 | * is converted to upper case. |
---|
1609 | * |
---|
1610 | * Input parameters: |
---|
1611 | * buf - initial command string |
---|
1612 | * |
---|
1613 | * Output parameter: |
---|
1614 | * buf - is modified by inserting '\0' at ends of split entities |
---|
1615 | * cmd - upper-cased command code |
---|
1616 | * opts - string containing all the options |
---|
1617 | * args - string containing all the arguments |
---|
1618 | */ |
---|
1619 | void |
---|
1620 | split_command(char *buf, char **cmd, char **opts, char **args) |
---|
1621 | { |
---|
1622 | char* eoc; |
---|
1623 | char* p = buf; |
---|
1624 | while(isspace((unsigned char)*p)) |
---|
1625 | ++p; |
---|
1626 | *cmd = p; |
---|
1627 | while(*p && !isspace((unsigned char)*p)) |
---|
1628 | { |
---|
1629 | *p = toupper((unsigned char)*p); |
---|
1630 | ++p; |
---|
1631 | } |
---|
1632 | eoc = p; |
---|
1633 | if(*p) |
---|
1634 | *p++ = '\0'; |
---|
1635 | while(isspace((unsigned char)*p)) |
---|
1636 | ++p; |
---|
1637 | *opts = p; |
---|
1638 | skip_options(&p); |
---|
1639 | *args = p; |
---|
1640 | if(*opts == p) |
---|
1641 | *opts = eoc; |
---|
1642 | while(*p && *p != '\r' && *p != '\n') |
---|
1643 | ++p; |
---|
1644 | if(*p) |
---|
1645 | *p++ = '\0'; |
---|
1646 | } |
---|
1647 | |
---|
1648 | /* |
---|
1649 | * exec_command |
---|
1650 | * |
---|
1651 | * Parse and execute FTP command. |
---|
1652 | * |
---|
1653 | * FIXME: This section is somewhat of a hack. We should have a better |
---|
1654 | * way to parse commands. |
---|
1655 | * |
---|
1656 | * Input parameters: |
---|
1657 | * info - corresponding SessionInfo structure |
---|
1658 | * cmd - command to be executed (upper-case) |
---|
1659 | * args - arguments of the command |
---|
1660 | * |
---|
1661 | * Output parameters: |
---|
1662 | * NONE |
---|
1663 | */ |
---|
1664 | static void |
---|
1665 | exec_command(FTPD_SessionInfo_t *info, char* cmd, char* args) |
---|
1666 | { |
---|
1667 | char fname[FTPD_BUFSIZE]; |
---|
1668 | int wrong_command = 0; |
---|
1669 | |
---|
1670 | fname[0] = '\0'; |
---|
1671 | |
---|
1672 | if (!strcmp("PORT", cmd)) |
---|
1673 | { |
---|
1674 | command_port(info, args); |
---|
1675 | } |
---|
1676 | else if (!strcmp("PASV", cmd)) |
---|
1677 | { |
---|
1678 | command_pasv(info); |
---|
1679 | } |
---|
1680 | else if (!strcmp("RETR", cmd)) |
---|
1681 | { |
---|
1682 | strncpy(fname, args, 254); |
---|
1683 | command_retrieve(info, fname); |
---|
1684 | } |
---|
1685 | else if (!strcmp("STOR", cmd)) |
---|
1686 | { |
---|
1687 | strncpy(fname, args, 254); |
---|
1688 | command_store(info, fname); |
---|
1689 | } |
---|
1690 | else if (!strcmp("LIST", cmd)) |
---|
1691 | { |
---|
1692 | strncpy(fname, args, 254); |
---|
1693 | command_list(info, fname, 1); |
---|
1694 | } |
---|
1695 | else if (!strcmp("NLST", cmd)) |
---|
1696 | { |
---|
1697 | strncpy(fname, args, 254); |
---|
1698 | command_list(info, fname, 0); |
---|
1699 | } |
---|
1700 | else if (!strcmp("MDTM", cmd)) |
---|
1701 | { |
---|
1702 | strncpy(fname, args, 254); |
---|
1703 | command_mdtm(info, fname); |
---|
1704 | } |
---|
1705 | else if (!strcmp("SYST", cmd)) |
---|
1706 | { |
---|
1707 | send_reply(info, 215, FTPD_SYSTYPE); |
---|
1708 | } |
---|
1709 | else if (!strcmp("TYPE", cmd)) |
---|
1710 | { |
---|
1711 | if (args[0] == 'I') |
---|
1712 | { |
---|
1713 | info->xfer_mode = TYPE_I; |
---|
1714 | send_reply(info, 200, "Type set to I."); |
---|
1715 | } |
---|
1716 | else if (args[0] == 'A') |
---|
1717 | { |
---|
1718 | info->xfer_mode = TYPE_A; |
---|
1719 | send_reply(info, 200, "Type set to A."); |
---|
1720 | } |
---|
1721 | else |
---|
1722 | { |
---|
1723 | info->xfer_mode = TYPE_I; |
---|
1724 | send_reply(info, 504, "Type not implemented. Set to I."); |
---|
1725 | } |
---|
1726 | } |
---|
1727 | else if (!strcmp("USER", cmd)) |
---|
1728 | { |
---|
1729 | sscanf(args, "%254s", fname); |
---|
1730 | if (info->user) |
---|
1731 | free(info->user); |
---|
1732 | if (info->pass) |
---|
1733 | free(info->pass); |
---|
1734 | info->pass = NULL; |
---|
1735 | info->user = strdup(fname); |
---|
1736 | if (rtems_ftpd_configuration.login && |
---|
1737 | !rtems_ftpd_configuration.login(info->user, NULL)) { |
---|
1738 | info->auth = false; |
---|
1739 | send_reply(info, 331, "User name okay, need password."); |
---|
1740 | } else { |
---|
1741 | info->auth = true; |
---|
1742 | send_reply(info, 230, "User logged in."); |
---|
1743 | } |
---|
1744 | } |
---|
1745 | else if (!strcmp("PASS", cmd)) |
---|
1746 | { |
---|
1747 | sscanf(args, "%254s", fname); |
---|
1748 | if (info->pass) |
---|
1749 | free(info->pass); |
---|
1750 | info->pass = strdup(fname); |
---|
1751 | if (!info->user) { |
---|
1752 | send_reply(info, 332, "Need account to log in"); |
---|
1753 | } else { |
---|
1754 | if (rtems_ftpd_configuration.login && |
---|
1755 | !rtems_ftpd_configuration.login(info->user, info->pass)) { |
---|
1756 | info->auth = false; |
---|
1757 | send_reply(info, 530, "Not logged in."); |
---|
1758 | } else { |
---|
1759 | info->auth = true; |
---|
1760 | send_reply(info, 230, "User logged in."); |
---|
1761 | } |
---|
1762 | } |
---|
1763 | } |
---|
1764 | else if (!strcmp("DELE", cmd)) |
---|
1765 | { |
---|
1766 | if(!can_write() || !info->auth) |
---|
1767 | { |
---|
1768 | send_reply(info, 550, "Access denied."); |
---|
1769 | } |
---|
1770 | else if ( |
---|
1771 | strncpy(fname, args, 254) && |
---|
1772 | unlink(fname) == 0) |
---|
1773 | { |
---|
1774 | send_reply(info, 257, "DELE successful."); |
---|
1775 | } |
---|
1776 | else |
---|
1777 | { |
---|
1778 | send_reply(info, 550, "DELE failed."); |
---|
1779 | } |
---|
1780 | } |
---|
1781 | else if (!strcmp("SITE", cmd)) |
---|
1782 | { |
---|
1783 | char* opts; |
---|
1784 | split_command(args, &cmd, &opts, &args); |
---|
1785 | if(!strcmp("CHMOD", cmd)) |
---|
1786 | { |
---|
1787 | int mask; |
---|
1788 | |
---|
1789 | if(!can_write() || !info->auth) |
---|
1790 | { |
---|
1791 | send_reply(info, 550, "Access denied."); |
---|
1792 | } |
---|
1793 | else { |
---|
1794 | char *c; |
---|
1795 | c = strchr(args, ' '); |
---|
1796 | if((c != NULL) && (sscanf(args, "%o", &mask) == 1) && strncpy(fname, c+1, 254) |
---|
1797 | && (chmod(fname, (mode_t)mask) == 0)) |
---|
1798 | send_reply(info, 257, "CHMOD successful."); |
---|
1799 | else |
---|
1800 | send_reply(info, 550, "CHMOD failed."); |
---|
1801 | } |
---|
1802 | } |
---|
1803 | else |
---|
1804 | wrong_command = 1; |
---|
1805 | } |
---|
1806 | else if (!strcmp("RMD", cmd)) |
---|
1807 | { |
---|
1808 | if(!can_write() || !info->auth) |
---|
1809 | { |
---|
1810 | send_reply(info, 550, "Access denied."); |
---|
1811 | } |
---|
1812 | else if ( |
---|
1813 | strncpy(fname, args, 254) && |
---|
1814 | rmdir(fname) == 0) |
---|
1815 | { |
---|
1816 | send_reply(info, 257, "RMD successful."); |
---|
1817 | } |
---|
1818 | else |
---|
1819 | { |
---|
1820 | send_reply(info, 550, "RMD failed."); |
---|
1821 | } |
---|
1822 | } |
---|
1823 | else if (!strcmp("MKD", cmd)) |
---|
1824 | { |
---|
1825 | if(!can_write() || !info->auth) |
---|
1826 | { |
---|
1827 | send_reply(info, 550, "Access denied."); |
---|
1828 | } |
---|
1829 | else if ( |
---|
1830 | strncpy(fname, args, 254) && |
---|
1831 | mkdir(fname, S_IRWXU | S_IRWXG | S_IRWXO) == 0) |
---|
1832 | { |
---|
1833 | send_reply(info, 257, "MKD successful."); |
---|
1834 | } |
---|
1835 | else |
---|
1836 | { |
---|
1837 | send_reply(info, 550, "MKD failed."); |
---|
1838 | } |
---|
1839 | } |
---|
1840 | else if (!strcmp("CWD", cmd)) |
---|
1841 | { |
---|
1842 | strncpy(fname, args, 254); |
---|
1843 | command_cwd(info, fname); |
---|
1844 | } |
---|
1845 | else if (!strcmp("CDUP", cmd)) |
---|
1846 | { |
---|
1847 | command_cwd(info, ".."); |
---|
1848 | } |
---|
1849 | else if (!strcmp("PWD", cmd)) |
---|
1850 | { |
---|
1851 | command_pwd(info); |
---|
1852 | } |
---|
1853 | else |
---|
1854 | wrong_command = 1; |
---|
1855 | |
---|
1856 | if(wrong_command) |
---|
1857 | send_reply(info, 500, "Command not understood."); |
---|
1858 | } |
---|
1859 | |
---|
1860 | |
---|
1861 | /* |
---|
1862 | * session |
---|
1863 | * |
---|
1864 | * This task handles single session. It is waked up when the FTP daemon gets a |
---|
1865 | * service request from a remote machine. Here, we watch for commands that |
---|
1866 | * will come through the control connection. These commands are then parsed |
---|
1867 | * and executed until the connection is closed, either unintentionally or |
---|
1868 | * intentionally with the "QUIT" command. |
---|
1869 | * |
---|
1870 | * Input parameters: |
---|
1871 | * arg - pointer to corresponding SessionInfo. |
---|
1872 | * |
---|
1873 | * Output parameters: |
---|
1874 | * NONE |
---|
1875 | */ |
---|
1876 | static void |
---|
1877 | session(rtems_task_argument arg) |
---|
1878 | { |
---|
1879 | FTPD_SessionInfo_t *const info = (FTPD_SessionInfo_t *)arg; |
---|
1880 | int chroot_made = 0; |
---|
1881 | |
---|
1882 | rtems_libio_set_private_env(); |
---|
1883 | |
---|
1884 | /* chroot() can fail here because the directory may not exist yet. */ |
---|
1885 | chroot_made = chroot(ftpd_root) == 0; |
---|
1886 | |
---|
1887 | while(1) |
---|
1888 | { |
---|
1889 | rtems_event_set set; |
---|
1890 | |
---|
1891 | rtems_event_receive(FTPD_RTEMS_EVENT, RTEMS_EVENT_ANY, RTEMS_NO_TIMEOUT, |
---|
1892 | &set); |
---|
1893 | |
---|
1894 | chroot_made = chroot_made || chroot(ftpd_root) == 0; |
---|
1895 | chdir("/"); |
---|
1896 | |
---|
1897 | errno = 0; |
---|
1898 | |
---|
1899 | send_reply(info, 220, FTPD_SERVER_MESSAGE); |
---|
1900 | |
---|
1901 | while (1) |
---|
1902 | { |
---|
1903 | char buf[FTPD_BUFSIZE]; |
---|
1904 | char *cmd, *opts, *args; |
---|
1905 | |
---|
1906 | if (fgets(buf, FTPD_BUFSIZE, info->ctrl_fp) == NULL) |
---|
1907 | { |
---|
1908 | syslog(LOG_INFO, "ftpd: Connection aborted."); |
---|
1909 | break; |
---|
1910 | } |
---|
1911 | |
---|
1912 | split_command(buf, &cmd, &opts, &args); |
---|
1913 | |
---|
1914 | if (!strcmp("QUIT", cmd)) |
---|
1915 | { |
---|
1916 | send_reply(info, 221, "Goodbye."); |
---|
1917 | break; |
---|
1918 | } |
---|
1919 | else |
---|
1920 | { |
---|
1921 | exec_command(info, cmd, args); |
---|
1922 | } |
---|
1923 | } |
---|
1924 | |
---|
1925 | /* Close connection and put ourselves back into the task pool. */ |
---|
1926 | close_data_socket(info); |
---|
1927 | close_stream(info); |
---|
1928 | free(info->user); |
---|
1929 | free(info->pass); |
---|
1930 | task_pool_release(info); |
---|
1931 | } |
---|
1932 | } |
---|
1933 | |
---|
1934 | |
---|
1935 | /* |
---|
1936 | * daemon |
---|
1937 | * |
---|
1938 | * This task runs forever. It waits for service requests on the FTP port |
---|
1939 | * (port 21 by default). When a request is received, it opens a new session |
---|
1940 | * to handle those requests until the connection is closed. |
---|
1941 | * |
---|
1942 | * Input parameters: |
---|
1943 | * NONE |
---|
1944 | * |
---|
1945 | * Output parameters: |
---|
1946 | * NONE |
---|
1947 | */ |
---|
1948 | static void |
---|
1949 | daemon(rtems_task_argument args __attribute__((unused))) |
---|
1950 | { |
---|
1951 | int s; |
---|
1952 | socklen_t addrLen; |
---|
1953 | struct sockaddr_in addr; |
---|
1954 | FTPD_SessionInfo_t *info = NULL; |
---|
1955 | |
---|
1956 | |
---|
1957 | s = socket(PF_INET, SOCK_STREAM, 0); |
---|
1958 | if (s < 0) |
---|
1959 | syslog(LOG_ERR, "ftpd: Error creating socket: %s", serr()); |
---|
1960 | |
---|
1961 | addr.sin_family = AF_INET; |
---|
1962 | addr.sin_port = htons(rtems_ftpd_configuration.port); |
---|
1963 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
1964 | memset(addr.sin_zero, 0, sizeof(addr.sin_zero)); |
---|
1965 | |
---|
1966 | if (0 > bind(s, (struct sockaddr *)&addr, sizeof(addr))) |
---|
1967 | syslog(LOG_ERR, "ftpd: Error binding control socket: %s", serr()); |
---|
1968 | else if (0 > listen(s, 1)) |
---|
1969 | syslog(LOG_ERR, "ftpd: Error listening on control socket: %s", serr()); |
---|
1970 | else while (1) |
---|
1971 | { |
---|
1972 | int ss; |
---|
1973 | addrLen = sizeof(addr); |
---|
1974 | ss = accept(s, (struct sockaddr *)&addr, &addrLen); |
---|
1975 | if (0 > ss) |
---|
1976 | syslog(LOG_ERR, "ftpd: Error accepting control connection: %s", serr()); |
---|
1977 | else if(!set_socket_timeout(ss, ftpd_timeout)) |
---|
1978 | close_socket(ss); |
---|
1979 | else |
---|
1980 | { |
---|
1981 | info = task_pool_obtain(); |
---|
1982 | if (NULL == info) |
---|
1983 | { |
---|
1984 | close_socket(ss); |
---|
1985 | } |
---|
1986 | else |
---|
1987 | { |
---|
1988 | info->ctrl_socket = ss; |
---|
1989 | if ((info->ctrl_fp = fdopen(info->ctrl_socket, "r+")) == NULL) |
---|
1990 | { |
---|
1991 | syslog(LOG_ERR, "ftpd: fdopen() on socket failed: %s", serr()); |
---|
1992 | close_stream(info); |
---|
1993 | task_pool_release(info); |
---|
1994 | } |
---|
1995 | else |
---|
1996 | { |
---|
1997 | /* Initialize corresponding SessionInfo structure */ |
---|
1998 | info->def_addr = addr; |
---|
1999 | if(0 > getsockname(ss, (struct sockaddr *)&addr, &addrLen)) |
---|
2000 | { |
---|
2001 | syslog(LOG_ERR, "ftpd: getsockname(): %s", serr()); |
---|
2002 | close_stream(info); |
---|
2003 | task_pool_release(info); |
---|
2004 | } |
---|
2005 | else |
---|
2006 | { |
---|
2007 | info->use_default = 1; |
---|
2008 | info->ctrl_addr = addr; |
---|
2009 | info->pasv_socket = -1; |
---|
2010 | info->data_socket = -1; |
---|
2011 | info->xfer_mode = TYPE_A; |
---|
2012 | info->data_addr.sin_port = |
---|
2013 | htons(ntohs(info->ctrl_addr.sin_port) - 1); |
---|
2014 | info->idle = ftpd_timeout; |
---|
2015 | info->user = NULL; |
---|
2016 | info->pass = NULL; |
---|
2017 | if (rtems_ftpd_configuration.login) |
---|
2018 | info->auth = false; |
---|
2019 | else |
---|
2020 | info->auth = true; |
---|
2021 | /* Wakeup the session task. The task will call task_pool_release |
---|
2022 | after it closes connection. */ |
---|
2023 | rtems_event_send(info->tid, FTPD_RTEMS_EVENT); |
---|
2024 | } |
---|
2025 | } |
---|
2026 | } |
---|
2027 | } |
---|
2028 | } |
---|
2029 | rtems_task_delete(RTEMS_SELF); |
---|
2030 | } |
---|
2031 | |
---|
2032 | |
---|
2033 | /* |
---|
2034 | * rtems_ftpd_start |
---|
2035 | * |
---|
2036 | * Here, we start the FTPD task which waits for FTP requests and services |
---|
2037 | * them. This procedure returns to its caller once the task is started. |
---|
2038 | * |
---|
2039 | * |
---|
2040 | * Input parameters: |
---|
2041 | * |
---|
2042 | * Output parameters: |
---|
2043 | * returns RTEMS_SUCCESSFUL on successful start of the daemon. |
---|
2044 | */ |
---|
2045 | int |
---|
2046 | rtems_initialize_ftpd(void) |
---|
2047 | { |
---|
2048 | rtems_status_code sc; |
---|
2049 | rtems_id tid; |
---|
2050 | rtems_task_priority priority; |
---|
2051 | int count; |
---|
2052 | |
---|
2053 | if (rtems_ftpd_configuration.port == 0) |
---|
2054 | { |
---|
2055 | rtems_ftpd_configuration.port = FTPD_CONTROL_PORT; |
---|
2056 | } |
---|
2057 | |
---|
2058 | if (rtems_ftpd_configuration.priority == 0) |
---|
2059 | { |
---|
2060 | rtems_ftpd_configuration.priority = 40; |
---|
2061 | } |
---|
2062 | priority = rtems_ftpd_configuration.priority; |
---|
2063 | |
---|
2064 | ftpd_timeout = rtems_ftpd_configuration.idle; |
---|
2065 | if (ftpd_timeout < 0) |
---|
2066 | ftpd_timeout = 0; |
---|
2067 | rtems_ftpd_configuration.idle = ftpd_timeout; |
---|
2068 | |
---|
2069 | ftpd_access = rtems_ftpd_configuration.access; |
---|
2070 | |
---|
2071 | if (rtems_ftpd_configuration.tasks_count <= 0) |
---|
2072 | rtems_ftpd_configuration.tasks_count = 1; |
---|
2073 | count = rtems_ftpd_configuration.tasks_count; |
---|
2074 | |
---|
2075 | if (!task_pool_init(count, priority)) |
---|
2076 | { |
---|
2077 | syslog(LOG_ERR, "ftpd: Could not initialize task pool."); |
---|
2078 | return RTEMS_UNSATISFIED; |
---|
2079 | } |
---|
2080 | |
---|
2081 | sc = rtems_task_create(rtems_build_name('F', 'T', 'P', 'D'), |
---|
2082 | priority, RTEMS_MINIMUM_STACK_SIZE, |
---|
2083 | RTEMS_PREEMPT | RTEMS_NO_TIMESLICE | RTEMS_NO_ASR | |
---|
2084 | RTEMS_INTERRUPT_LEVEL(0), |
---|
2085 | RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL, |
---|
2086 | &tid); |
---|
2087 | |
---|
2088 | if (sc == RTEMS_SUCCESSFUL) |
---|
2089 | { |
---|
2090 | sc = rtems_task_start(tid, daemon, 0); |
---|
2091 | if (sc != RTEMS_SUCCESSFUL) |
---|
2092 | rtems_task_delete(tid); |
---|
2093 | } |
---|
2094 | |
---|
2095 | if (sc != RTEMS_SUCCESSFUL) |
---|
2096 | { |
---|
2097 | task_pool_done(count); |
---|
2098 | syslog(LOG_ERR, "ftpd: Could not create/start FTP daemon: %s", |
---|
2099 | rtems_status_text(sc)); |
---|
2100 | return RTEMS_UNSATISFIED; |
---|
2101 | } |
---|
2102 | |
---|
2103 | ftpd_root = "/"; |
---|
2104 | if ( |
---|
2105 | rtems_ftpd_configuration.root && |
---|
2106 | rtems_ftpd_configuration.root[0] == '/' |
---|
2107 | ) |
---|
2108 | ftpd_root = rtems_ftpd_configuration.root; |
---|
2109 | |
---|
2110 | rtems_ftpd_configuration.root = ftpd_root; |
---|
2111 | |
---|
2112 | syslog(LOG_INFO, "ftpd: FTP daemon started (%d session%s max)", |
---|
2113 | count, ((count > 1) ? "s" : "")); |
---|
2114 | |
---|
2115 | return RTEMS_SUCCESSFUL; |
---|
2116 | } |
---|