1 /*****************************************************************************
2 * ftp.c: FTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
5 * Copyright © 2006 Rémi Denis-Courmont
8 * Authors: Laurent Aimar <fenrir@via.ecp.fr> - original code
9 * Rémi Denis-Courmont <rem # videolan.org> - EPSV support
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
38 #include <vlc_access.h>
39 #include <vlc_dialog.h>
41 #include <vlc_network.h>
46 # define IPPORT_FTP 21u
49 /*****************************************************************************
51 *****************************************************************************/
52 static int InOpen ( vlc_object_t * );
53 static void InClose( vlc_object_t * );
54 static int OutOpen ( vlc_object_t * );
55 static void OutClose( vlc_object_t * );
57 #define CACHING_TEXT N_("Caching value in ms")
58 #define CACHING_LONGTEXT N_( \
59 "Caching value for FTP streams. This " \
60 "value should be set in milliseconds." )
61 #define USER_TEXT N_("FTP user name")
62 #define USER_LONGTEXT N_("User name that will " \
63 "be used for the connection.")
64 #define PASS_TEXT N_("FTP password")
65 #define PASS_LONGTEXT N_("Password that will be " \
66 "used for the connection.")
67 #define ACCOUNT_TEXT N_("FTP account")
68 #define ACCOUNT_LONGTEXT N_("Account that will be " \
69 "used for the connection.")
72 set_shortname( "FTP" )
73 set_description( N_("FTP input") )
74 set_capability( "access", 0 )
75 set_category( CAT_INPUT )
76 set_subcategory( SUBCAT_INPUT_ACCESS )
77 add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
78 CACHING_TEXT, CACHING_LONGTEXT, true )
80 add_string( "ftp-user", "anonymous", NULL, USER_TEXT, USER_LONGTEXT,
82 add_string( "ftp-pwd", "anonymous@example.com", NULL, PASS_TEXT,
83 PASS_LONGTEXT, false )
84 add_string( "ftp-account", "anonymous", NULL, ACCOUNT_TEXT,
85 ACCOUNT_LONGTEXT, false )
87 set_callbacks( InOpen, InClose )
90 set_shortname( "FTP" )
91 set_description( N_("FTP upload output") )
92 set_capability( "sout access", 0 )
93 set_category( CAT_SOUT )
94 set_subcategory( SUBCAT_SOUT_ACO )
96 set_callbacks( OutOpen, OutClose )
99 /*****************************************************************************
101 *****************************************************************************/
102 static ssize_t Read( access_t *, uint8_t *, size_t );
103 static ssize_t Write( sout_access_out_t *, block_t * );
104 static int Seek( access_t *, uint64_t );
105 static int OutSeek( sout_access_out_t *, off_t );
106 static int Control( access_t *, int, va_list );
115 char sz_epsv_ip[NI_MAXNUMERICHOST];
119 #define GET_OUT_SYS( p_this ) \
120 ((access_sys_t *)(((sout_access_out_t *)(p_this))->p_sys))
122 static int ftp_SendCommand( vlc_object_t *, access_sys_t *, const char *, ... );
123 static int ftp_ReadCommand( vlc_object_t *, access_sys_t *, int *, char ** );
124 static int ftp_StartStream( vlc_object_t *, access_sys_t *, uint64_t );
125 static int ftp_StopStream ( vlc_object_t *, access_sys_t * );
127 static int Login( vlc_object_t *p_access, access_sys_t *p_sys )
132 /* *** Open a TCP connection with server *** */
133 int fd = p_sys->fd_cmd = net_ConnectTCP( p_access, p_sys->url.psz_host,
137 msg_Err( p_access, "connection failed" );
138 dialog_Fatal( p_access, _("Network interaction failed"), "%s",
139 _("VLC could not connect with the given server.") );
143 while( ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) == 1 );
145 if( i_answer / 100 != 2 )
147 msg_Err( p_access, "connection rejected" );
148 dialog_Fatal( p_access, _("Network interaction failed"), "%s",
149 _("VLC's connection to the given server was rejected.") );
153 msg_Dbg( p_access, "connection accepted (%d)", i_answer );
155 if( p_sys->url.psz_username && *p_sys->url.psz_username )
156 psz = strdup( p_sys->url.psz_username );
158 psz = var_CreateGetString( p_access, "ftp-user" );
162 if( ftp_SendCommand( p_access, p_sys, "USER %s", psz ) < 0 ||
163 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
170 switch( i_answer / 100 )
173 msg_Dbg( p_access, "user accepted" );
176 msg_Dbg( p_access, "password needed" );
177 if( p_sys->url.psz_password && *p_sys->url.psz_password )
178 psz = strdup( p_sys->url.psz_password );
180 psz = var_CreateGetString( p_access, "ftp-pwd" );
184 if( ftp_SendCommand( p_access, p_sys, "PASS %s", psz ) < 0 ||
185 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
192 switch( i_answer / 100 )
195 msg_Dbg( p_access, "password accepted" );
198 msg_Dbg( p_access, "account needed" );
199 psz = var_CreateGetString( p_access, "ftp-account" );
200 if( ftp_SendCommand( p_access, p_sys, "ACCT %s",
202 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
209 if( i_answer / 100 != 2 )
211 msg_Err( p_access, "account rejected" );
212 dialog_Fatal( p_access,
213 _("Network interaction failed"),
214 "%s", _("Your account was rejected.") );
217 msg_Dbg( p_access, "account accepted" );
221 msg_Err( p_access, "password rejected" );
222 dialog_Fatal( p_access, _("Network interaction failed"),
223 "%s", _("Your password was rejected.") );
228 msg_Err( p_access, "user rejected" );
229 dialog_Fatal( p_access, _("Network interaction failed"), "%s",
230 _("Your connection attempt to the server was rejected.") );
237 static int Connect( vlc_object_t *p_access, access_sys_t *p_sys )
239 if( Login( p_access, p_sys ) < 0 )
242 /* Extended passive mode */
243 if( ftp_SendCommand( p_access, p_sys, "EPSV ALL" ) < 0 )
245 msg_Err( p_access, "cannot request extended passive mode" );
246 net_Close( p_sys->fd_cmd );
250 if( ftp_ReadCommand( p_access, p_sys, NULL, NULL ) == 2 )
252 if( net_GetPeerAddress( p_sys->fd_cmd, p_sys->sz_epsv_ip, NULL ) )
254 net_Close( p_sys->fd_cmd );
260 /* If ESPV ALL fails, we fallback to PASV.
261 * We have to restart the connection in case there is a NAT that
262 * understands EPSV ALL in the way, and hence won't allow PASV on
263 * the initial connection.
265 msg_Info( p_access, "FTP Extended passive mode disabled" );
266 net_Close( p_sys->fd_cmd );
268 if( Login( p_access, p_sys ) )
270 net_Close( p_sys->fd_cmd );
275 /* check binary mode support */
276 if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
277 ftp_ReadCommand( p_access, p_sys, NULL, NULL ) != 2 )
279 msg_Err( p_access, "cannot set binary transfer mode" );
280 net_Close( p_sys->fd_cmd );
288 static int parseURL( vlc_url_t *url, const char *path )
293 /* *** Parse URL and get server addr/port and path *** */
294 while( *path == '/' )
297 vlc_UrlParse( url, path, 0 );
299 if( url->psz_host == NULL || *url->psz_host == '\0' )
302 if( url->i_port <= 0 )
303 url->i_port = IPPORT_FTP; /* default port */
305 /* FTP URLs are relative to user's default directory (RFC1738 §3.2)
306 For absolute path use ftp://foo.bar//usr/local/etc/filename */
307 /* FIXME: we should issue a series of CWD, one per slash */
308 if( url->psz_path && *url->psz_path == '/' )
311 char *type = strstr( url->psz_path, ";type=" );
315 if( strchr( "iI", type[6] ) == NULL )
316 return VLC_EGENERIC; /* ASCII and directory not supported */
318 decode_URI( url->psz_path );
323 /****************************************************************************
324 * Open: connect to ftp server and ask for file
325 ****************************************************************************/
326 static int InOpen( vlc_object_t *p_this )
328 access_t *p_access = (access_t*)p_this;
333 STANDARD_READ_ACCESS_INIT
336 p_sys->directory = false;
338 if( parseURL( &p_sys->url, p_access->psz_path ) )
341 if( Connect( p_this, p_sys ) )
345 if( ftp_SendCommand( p_this, p_sys, "SIZE %s", p_sys->url.psz_path
346 ? p_sys->url.psz_path : "" ) < 0
347 || ftp_ReadCommand( p_this, p_sys, NULL, &psz_arg ) != 2 )
349 msg_Dbg( p_access, "cannot get file size" );
350 msg_Dbg( p_access, "will try to get directory contents" );
351 if( ftp_SendCommand( p_this, p_sys, "CWD %s", p_sys->url.psz_path
352 ? p_sys->url.psz_path : "" ) < 0 ||
353 ftp_ReadCommand( p_this, p_sys, NULL, &psz_arg ) != 2 )
355 msg_Err( p_access, "file or directory doesn't exist" );
356 net_Close( p_sys->fd_cmd );
359 p_sys->directory = true;
363 p_access->info.i_size = atoll( &psz_arg[4] );
365 msg_Dbg( p_access, "file size: %"PRIu64, p_access->info.i_size );
368 /* Start the 'stream' */
369 if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
371 msg_Err( p_access, "cannot retrieve file" );
372 net_Close( p_sys->fd_cmd );
376 /* Update default_pts to a suitable value for ftp access */
377 var_Create( p_access, "ftp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
382 vlc_UrlClean( &p_sys->url );
387 static int OutOpen( vlc_object_t *p_this )
389 sout_access_out_t *p_access = (sout_access_out_t *)p_this;
392 p_sys = calloc( 1, sizeof( *p_sys ) );
400 if( parseURL( &p_sys->url, p_access->psz_path ) )
403 if( Connect( p_this, p_sys ) )
406 /* Start the 'stream' */
407 if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
409 msg_Err( p_access, "cannot store file" );
410 net_Close( p_sys->fd_cmd );
414 p_access->pf_seek = OutSeek;
415 p_access->pf_write = Write;
416 p_access->p_sys = (void *)p_sys;
421 vlc_UrlClean( &p_sys->url );
426 /*****************************************************************************
427 * Close: free unused data structures
428 *****************************************************************************/
429 static void Close( vlc_object_t *p_access, access_sys_t *p_sys )
431 msg_Dbg( p_access, "stopping stream" );
432 ftp_StopStream( p_access, p_sys );
434 if( ftp_SendCommand( p_access, p_sys, "QUIT" ) < 0 )
436 msg_Warn( p_access, "cannot quit" );
440 ftp_ReadCommand( p_access, p_sys, NULL, NULL );
442 net_Close( p_sys->fd_cmd );
445 vlc_UrlClean( &p_sys->url );
449 static void InClose( vlc_object_t *p_this )
451 Close( p_this, ((access_t *)p_this)->p_sys);
454 static void OutClose( vlc_object_t *p_this )
456 Close( p_this, GET_OUT_SYS(p_this));
460 /*****************************************************************************
461 * Seek: try to go at the right place
462 *****************************************************************************/
463 static int _Seek( vlc_object_t *p_access, access_sys_t *p_sys, uint64_t i_pos )
465 msg_Dbg( p_access, "seeking to %"PRIu64, i_pos );
467 ftp_StopStream( (vlc_object_t *)p_access, p_sys );
468 if( ftp_StartStream( (vlc_object_t *)p_access, p_sys, i_pos ) < 0 )
474 static int Seek( access_t *p_access, uint64_t i_pos )
476 int val = _Seek( (vlc_object_t *)p_access, p_access->p_sys, i_pos );
480 p_access->info.b_eof = false;
481 p_access->info.i_pos = i_pos;
486 static int OutSeek( sout_access_out_t *p_access, off_t i_pos )
488 return _Seek( (vlc_object_t *)p_access, GET_OUT_SYS( p_access ), i_pos);
491 /*****************************************************************************
493 *****************************************************************************/
494 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
496 access_sys_t *p_sys = p_access->p_sys;
498 assert( p_sys->fd_data != -1 );
499 assert( !p_sys->out );
501 if( p_access->info.b_eof )
504 if( p_sys->directory )
506 char *psz_line = net_Gets( p_access, p_sys->fd_data, NULL );
509 p_access->info.b_eof = true;
514 snprintf( (char*)p_buffer, i_len, "ftp://%s:%d/%s/%s\n",
515 p_sys->url.psz_host, p_sys->url.i_port,
516 p_sys->url.psz_path, psz_line );
518 return strlen( (const char *)p_buffer );
523 int i_read = net_Read( p_access, p_sys->fd_data, NULL,
524 p_buffer, i_len, false );
526 p_access->info.b_eof = true;
527 else if( i_read > 0 )
528 p_access->info.i_pos += i_read;
534 /*****************************************************************************
536 *****************************************************************************/
537 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
539 access_sys_t *p_sys = GET_OUT_SYS(p_access);
542 assert( p_sys->fd_data != -1 );
544 while( p_buffer != NULL )
546 block_t *p_next = p_buffer->p_next;;
548 i_write += net_Write( p_access, p_sys->fd_data, NULL,
549 p_buffer->p_buffer, p_buffer->i_buffer );
550 block_Release( p_buffer );
558 /*****************************************************************************
560 *****************************************************************************/
561 static int Control( access_t *p_access, int i_query, va_list args )
569 case ACCESS_CAN_SEEK:
570 pb_bool = (bool*)va_arg( args, bool* );
573 case ACCESS_CAN_FASTSEEK:
574 pb_bool = (bool*)va_arg( args, bool* );
577 case ACCESS_CAN_PAUSE:
578 pb_bool = (bool*)va_arg( args, bool* );
579 *pb_bool = true; /* FIXME */
581 case ACCESS_CAN_CONTROL_PACE:
582 pb_bool = (bool*)va_arg( args, bool* );
583 *pb_bool = true; /* FIXME */
587 case ACCESS_GET_PTS_DELAY:
588 pi_64 = (int64_t*)va_arg( args, int64_t * );
589 *pi_64 = (int64_t)var_GetInteger( p_access, "ftp-caching" ) * INT64_C(1000);
593 case ACCESS_SET_PAUSE_STATE:
594 pb_bool = (bool*)va_arg( args, bool* );
596 return Seek( p_access, p_access->info.i_pos );
599 case ACCESS_GET_TITLE_INFO:
600 case ACCESS_SET_TITLE:
601 case ACCESS_SET_SEEKPOINT:
602 case ACCESS_SET_PRIVATE_ID_STATE:
603 case ACCESS_GET_CONTENT_TYPE:
604 case ACCESS_GET_META:
608 msg_Warn( p_access, "unimplemented query in control: %d", i_query);
615 /*****************************************************************************
617 *****************************************************************************/
618 static int ftp_SendCommand( vlc_object_t *p_access, access_sys_t *p_sys,
619 const char *psz_fmt, ... )
624 va_start( args, psz_fmt );
625 if( vasprintf( &psz_cmd, psz_fmt, args ) == -1 )
630 msg_Dbg( p_access, "ftp_SendCommand:\"%s\"", psz_cmd);
632 if( net_Printf( p_access, p_sys->fd_cmd, NULL, "%s\r\n", psz_cmd ) < 0 )
634 msg_Err( p_access, "failed to send command" );
640 /* TODO support this s**t :
641 RFC 959 allows the client to send certain TELNET strings at any moment,
642 even in the middle of a request:
645 * \377\376x where x is one byte.
646 * \377\375x where x is one byte. The server is obliged to send \377\374x
647 * immediately after reading x.
648 * \377\374x where x is one byte.
649 * \377\373x where x is one byte. The server is obliged to send \377\376x
650 * immediately after reading x.
651 * \377x for any other byte x.
653 These strings are not part of the requests, except in the case \377\377,
654 where the request contains one \377. */
655 static int ftp_ReadCommand( vlc_object_t *p_access, access_sys_t *p_sys,
656 int *pi_answer, char **ppsz_answer )
661 psz_line = net_Gets( p_access, p_sys->fd_cmd, NULL );
662 if( psz_line == NULL || strlen( psz_line ) < 3 )
664 msg_Err( p_access, "cannot get answer" );
666 if( pi_answer ) *pi_answer = 500;
667 if( ppsz_answer ) *ppsz_answer = NULL;
670 msg_Dbg( p_access, "answer=%s", psz_line );
672 if( psz_line[3] == '-' ) /* Multiple response */
676 memcpy( end, psz_line, 3 );
681 char *psz_tmp = net_Gets( p_access, p_sys->fd_cmd, NULL );
683 if( psz_tmp == NULL ) /* Error */
686 if( !strncmp( psz_tmp, end, 4 ) )
695 i_answer = atoi( psz_line );
697 if( pi_answer ) *pi_answer = i_answer;
700 *ppsz_answer = psz_line;
706 return( i_answer / 100 );
709 static int ftp_StartStream( vlc_object_t *p_access, access_sys_t *p_sys,
712 char psz_ipv4[16], *psz_ip = p_sys->sz_epsv_ip;
714 char *psz_arg, *psz_parser;
717 assert( p_sys->fd_data == -1 );
719 if( ( ftp_SendCommand( p_access, p_sys, *psz_ip ? "EPSV" : "PASV" ) < 0 )
720 || ( ftp_ReadCommand( p_access, p_sys, &i_answer, &psz_arg ) != 2 ) )
722 msg_Err( p_access, "cannot set passive mode" );
726 psz_parser = strchr( psz_arg, '(' );
727 if( psz_parser == NULL )
730 msg_Err( p_access, "cannot parse passive mode response" );
736 char psz_fmt[7] = "(|||%u";
737 psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1];
739 if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 )
742 msg_Err( p_access, "cannot parse passive mode response" );
748 unsigned a1, a2, a3, a4, p1, p2;
750 if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,
751 &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )
752 || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )
755 msg_Err( p_access, "cannot parse passive mode response" );
759 sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );
761 i_port = (p1 << 8) | p2;
765 msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
767 if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
768 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) != 2 )
770 msg_Err( p_access, "cannot set binary transfer mode" );
776 if( ftp_SendCommand( p_access, p_sys, "REST %"PRIu64, i_start ) < 0 ||
777 ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) > 3 )
779 msg_Err( p_access, "cannot set restart offset" );
784 msg_Dbg( p_access, "waiting for data connection..." );
785 p_sys->fd_data = net_ConnectTCP( p_access, psz_ip, i_port );
786 if( p_sys->fd_data < 0 )
788 msg_Err( p_access, "failed to connect with server" );
791 msg_Dbg( p_access, "connection with \"%s:%d\" successful",
794 if( p_sys->directory )
796 if( ftp_SendCommand( p_access, p_sys, "NLST" ) < 0 ||
797 ftp_ReadCommand( p_access, p_sys, NULL, &psz_arg ) > 2 )
799 msg_Err( p_access, "cannot list directory contents" );
806 if( ftp_SendCommand( p_access, p_sys, "%s %s",
807 p_sys->out ? "STOR" : "RETR",
808 p_sys->url.psz_path ? p_sys->url.psz_path : "" ) < 0
809 || ftp_ReadCommand( p_access, p_sys, &i_answer, NULL ) > 2 )
811 msg_Err( p_access, "cannot retrieve file" );
816 shutdown( p_sys->fd_data, p_sys->out ? SHUT_RD : SHUT_WR );
821 static int ftp_StopStream ( vlc_object_t *p_access, access_sys_t *p_sys )
823 if( ftp_SendCommand( p_access, p_sys, "ABOR" ) < 0 )
825 msg_Warn( p_access, "cannot abort file" );
826 if( p_sys->fd_data > 0 )
827 net_Close( p_sys->fd_data );
832 if( p_sys->fd_data != -1 )
834 net_Close( p_sys->fd_data );
836 /* Read the final response from RETR/STOR, i.e. 426 or 226 */
837 ftp_ReadCommand( p_access, p_sys, NULL, NULL );
839 /* Read the response from ABOR, i.e. 226 or 225 */
840 ftp_ReadCommand( p_access, p_sys, NULL, NULL );