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