1 /*****************************************************************************
2 * ftp.c: FTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2006 VLC authors and VideoLAN
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 it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * 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>
44 #include <vlc_charset.h>
47 # define IPPORT_FTP 21u
50 /*****************************************************************************
52 *****************************************************************************/
53 static int InOpen ( vlc_object_t * );
54 static void InClose( vlc_object_t * );
55 static int OutOpen ( vlc_object_t * );
56 static void OutClose( vlc_object_t * );
58 #define USER_TEXT N_("FTP user name")
59 #define USER_LONGTEXT N_("User name that will " \
60 "be used for the connection.")
61 #define PASS_TEXT N_("FTP password")
62 #define PASS_LONGTEXT N_("Password that will be " \
63 "used for the connection.")
64 #define ACCOUNT_TEXT N_("FTP account")
65 #define ACCOUNT_LONGTEXT N_("Account that will be " \
66 "used for the connection.")
69 set_shortname( "FTP" )
70 set_description( N_("FTP input") )
71 set_capability( "access", 0 )
72 set_category( CAT_INPUT )
73 set_subcategory( SUBCAT_INPUT_ACCESS )
74 add_string( "ftp-user", "anonymous", USER_TEXT, USER_LONGTEXT,
76 add_string( "ftp-pwd", "anonymous@example.com", PASS_TEXT,
77 PASS_LONGTEXT, false )
78 add_string( "ftp-account", "anonymous", ACCOUNT_TEXT,
79 ACCOUNT_LONGTEXT, false )
81 set_callbacks( InOpen, InClose )
84 set_shortname( "FTP" )
85 set_description( N_("FTP upload output") )
86 set_capability( "sout access", 0 )
87 set_category( CAT_SOUT )
88 set_subcategory( SUBCAT_SOUT_ACO )
90 set_callbacks( OutOpen, OutClose )
93 /*****************************************************************************
95 *****************************************************************************/
96 static ssize_t Read( access_t *, uint8_t *, size_t );
97 static ssize_t Write( sout_access_out_t *, block_t * );
98 static int Seek( access_t *, uint64_t );
99 static int OutSeek( sout_access_out_t *, off_t );
100 static int Control( access_t *, int, va_list );
109 char sz_epsv_ip[NI_MAXNUMERICHOST];
113 #define GET_OUT_SYS( p_this ) \
114 ((access_sys_t *)(((sout_access_out_t *)(p_this))->p_sys))
116 static int ftp_SendCommand( vlc_object_t *obj, access_sys_t *sys,
117 const char *fmt, ... )
119 size_t fmtlen = strlen( fmt );
120 char fmtbuf[fmtlen + 3];
122 memcpy( fmtbuf, fmt, fmtlen );
123 memcpy( fmtbuf + fmtlen, "\r\n", 3 );
129 va_start( args, fmt );
130 val = vasprintf( &cmd, fmtbuf, args );
132 if( unlikely(val == -1) )
135 msg_Dbg( obj, "sending request: \"%.*s\" (%d bytes)", val - 2, cmd, val );
136 if( net_Write( obj, sys->fd_cmd, NULL, cmd, val ) != val )
138 msg_Err( obj, "request failure" );
147 /* TODO support this s**t :
148 RFC 959 allows the client to send certain TELNET strings at any moment,
149 even in the middle of a request:
152 * \377\376x where x is one byte.
153 * \377\375x where x is one byte. The server is obliged to send \377\374x
154 * immediately after reading x.
155 * \377\374x where x is one byte.
156 * \377\373x where x is one byte. The server is obliged to send \377\376x
157 * immediately after reading x.
158 * \377x for any other byte x.
160 These strings are not part of the requests, except in the case \377\377,
161 where the request contains one \377. */
162 static int ftp_RecvAnswer( vlc_object_t *obj, access_sys_t *sys,
163 int *restrict codep, char **restrict strp,
164 void (*cb)(void *, const char *), void *opaque )
171 char *resp = net_Gets( obj, sys->fd_cmd, NULL );
174 msg_Err( obj, "response failure" );
179 unsigned code = strtoul( resp, &end, 10 );
180 if( (end - resp) != 3 || (*end != '-' && *end != ' ') )
182 msg_Err( obj, "malformatted response" );
185 msg_Dbg( obj, "received response: \"%s\"", resp );
187 if( *end == '-' ) /* Multi-line response */
194 char *line = net_Gets( obj, sys->fd_cmd, NULL );
197 msg_Err( obj, "response failure" );
201 done = !strncmp( resp, line, 4 );
221 static void DummyLine( void *data, const char *str )
223 (void) data; (void) str;
226 static int ftp_RecvCommand( vlc_object_t *obj, access_sys_t *sys,
227 int *restrict codep, char **restrict strp )
229 return ftp_RecvAnswer( obj, sys, codep, strp, DummyLine, NULL );
232 static int ftp_StartStream( vlc_object_t *, access_sys_t *, uint64_t );
233 static int ftp_StopStream ( vlc_object_t *, access_sys_t * );
235 static int Login( vlc_object_t *p_access, access_sys_t *p_sys )
240 /* *** Open a TCP connection with server *** */
241 int fd = p_sys->fd_cmd = net_ConnectTCP( p_access, p_sys->url.psz_host,
245 msg_Err( p_access, "connection failed" );
246 dialog_Fatal( p_access, _("Network interaction failed"), "%s",
247 _("VLC could not connect with the given server.") );
251 while( ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) == 1 );
253 if( i_answer / 100 != 2 )
255 msg_Err( p_access, "connection rejected" );
256 dialog_Fatal( p_access, _("Network interaction failed"), "%s",
257 _("VLC's connection to the given server was rejected.") );
261 msg_Dbg( p_access, "connection accepted (%d)", i_answer );
263 if( p_sys->url.psz_username && *p_sys->url.psz_username )
264 psz = strdup( p_sys->url.psz_username );
266 psz = var_InheritString( p_access, "ftp-user" );
270 if( ftp_SendCommand( p_access, p_sys, "USER %s", psz ) < 0 ||
271 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
278 switch( i_answer / 100 )
281 msg_Dbg( p_access, "user accepted" );
284 msg_Dbg( p_access, "password needed" );
285 if( p_sys->url.psz_password && *p_sys->url.psz_password )
286 psz = strdup( p_sys->url.psz_password );
288 psz = var_InheritString( p_access, "ftp-pwd" );
292 if( ftp_SendCommand( p_access, p_sys, "PASS %s", psz ) < 0 ||
293 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
300 switch( i_answer / 100 )
303 msg_Dbg( p_access, "password accepted" );
306 msg_Dbg( p_access, "account needed" );
307 psz = var_InheritString( p_access, "ftp-account" );
308 if( ftp_SendCommand( p_access, p_sys, "ACCT %s",
310 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
317 if( i_answer / 100 != 2 )
319 msg_Err( p_access, "account rejected" );
320 dialog_Fatal( p_access,
321 _("Network interaction failed"),
322 "%s", _("Your account was rejected.") );
325 msg_Dbg( p_access, "account accepted" );
329 msg_Err( p_access, "password rejected" );
330 dialog_Fatal( p_access, _("Network interaction failed"),
331 "%s", _("Your password was rejected.") );
336 msg_Err( p_access, "user rejected" );
337 dialog_Fatal( p_access, _("Network interaction failed"), "%s",
338 _("Your connection attempt to the server was rejected.") );
345 static void FeaturesCheck( void *opaque, const char *feature )
347 bool *unicode = opaque;
349 if( strcasestr( feature, "UTF8" ) != NULL )
353 static const char *IsASCII( const char *str )
356 for( const char *p = str; (c = *p) != '\0'; p++ )
362 static int Connect( vlc_object_t *p_access, access_sys_t *p_sys )
364 if( Login( p_access, p_sys ) < 0 )
367 /* Extended passive mode */
368 if( ftp_SendCommand( p_access, p_sys, "EPSV ALL" ) < 0 )
370 msg_Err( p_access, "cannot request extended passive mode" );
374 if( ftp_RecvCommand( p_access, p_sys, NULL, NULL ) == 2 )
376 if( net_GetPeerAddress( p_sys->fd_cmd, p_sys->sz_epsv_ip, NULL ) )
381 /* If ESPV ALL fails, we fallback to PASV.
382 * We have to restart the connection in case there is a NAT that
383 * understands EPSV ALL in the way, and hence won't allow PASV on
384 * the initial connection.
386 msg_Info( p_access, "FTP Extended passive mode disabled" );
387 net_Close( p_sys->fd_cmd );
389 if( Login( p_access, p_sys ) )
394 bool unicode = false;
395 if( ftp_SendCommand( p_access, p_sys, "FEAT" ) < 0
396 || ftp_RecvAnswer( p_access, p_sys, NULL, NULL,
397 FeaturesCheck, &unicode ) < 0 )
399 msg_Err( p_access, "cannot get server features" );
403 if( (unicode ? IsUTF8 : IsASCII)(p_sys->url.psz_path) == NULL )
405 msg_Err( p_access, "unsupported path: \"%s\"", p_sys->url.psz_path );
409 /* check binary mode support */
410 if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
411 ftp_RecvCommand( p_access, p_sys, NULL, NULL ) != 2 )
413 msg_Err( p_access, "cannot set binary transfer mode" );
419 net_Close( p_sys->fd_cmd );
424 static int parseURL( vlc_url_t *url, const char *path )
429 /* *** Parse URL and get server addr/port and path *** */
430 while( *path == '/' )
433 vlc_UrlParse( url, path, 0 );
435 if( url->psz_host == NULL || *url->psz_host == '\0' )
438 if( url->i_port <= 0 )
439 url->i_port = IPPORT_FTP; /* default port */
441 if( url->psz_path == NULL )
443 /* FTP URLs are relative to user's default directory (RFC1738 §3.2)
444 For absolute path use ftp://foo.bar//usr/local/etc/filename */
445 /* FIXME: we should issue a series of CWD, one per slash */
448 assert( url->psz_path[0] == '/' );
452 char *type = strstr( url->psz_path, ";type=" );
456 if( strchr( "iI", type[6] ) == NULL )
457 return VLC_EGENERIC; /* ASCII and directory not supported */
459 decode_URI( url->psz_path );
464 /****************************************************************************
465 * Open: connect to ftp server and ask for file
466 ****************************************************************************/
467 static int InOpen( vlc_object_t *p_this )
469 access_t *p_access = (access_t*)p_this;
474 STANDARD_READ_ACCESS_INIT
477 p_sys->directory = false;
479 if( parseURL( &p_sys->url, p_access->psz_location ) )
482 if( Connect( p_this, p_sys ) )
486 if( p_sys->url.psz_path == NULL )
487 p_sys->directory = true;
489 if( ftp_SendCommand( p_this, p_sys, "SIZE %s", p_sys->url.psz_path ) < 0 )
492 if ( ftp_RecvCommand( p_this, p_sys, NULL, &psz_arg ) == 2 )
494 p_access->info.i_size = atoll( &psz_arg[4] );
496 msg_Dbg( p_access, "file size: %"PRIu64, p_access->info.i_size );
499 if( ftp_SendCommand( p_this, p_sys, "CWD %s", p_sys->url.psz_path ) < 0 )
502 if( ftp_RecvCommand( p_this, p_sys, NULL, NULL ) != 2 )
504 msg_Err( p_access, "file or directory does not exist" );
508 p_sys->directory = true;
510 /* Start the 'stream' */
511 if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
513 msg_Err( p_access, "cannot retrieve file" );
514 net_Close( p_sys->fd_cmd );
521 net_Close( p_sys->fd_cmd );
523 vlc_UrlClean( &p_sys->url );
528 static int OutOpen( vlc_object_t *p_this )
530 sout_access_out_t *p_access = (sout_access_out_t *)p_this;
533 p_sys = calloc( 1, sizeof( *p_sys ) );
541 if( parseURL( &p_sys->url, p_access->psz_path ) )
543 if( p_sys->url.psz_path == NULL )
545 msg_Err( p_this, "no filename specified" );
549 if( Connect( p_this, p_sys ) )
552 /* Start the 'stream' */
553 if( ftp_StartStream( p_this, p_sys, 0 ) < 0 )
555 msg_Err( p_access, "cannot store file" );
556 net_Close( p_sys->fd_cmd );
560 p_access->pf_seek = OutSeek;
561 p_access->pf_write = Write;
562 p_access->p_sys = (void *)p_sys;
567 vlc_UrlClean( &p_sys->url );
572 /*****************************************************************************
573 * Close: free unused data structures
574 *****************************************************************************/
575 static void Close( vlc_object_t *p_access, access_sys_t *p_sys )
577 msg_Dbg( p_access, "stopping stream" );
578 ftp_StopStream( p_access, p_sys );
580 if( ftp_SendCommand( p_access, p_sys, "QUIT" ) < 0 )
582 msg_Warn( p_access, "cannot quit" );
586 ftp_RecvCommand( p_access, p_sys, NULL, NULL );
588 net_Close( p_sys->fd_cmd );
591 vlc_UrlClean( &p_sys->url );
595 static void InClose( vlc_object_t *p_this )
597 Close( p_this, ((access_t *)p_this)->p_sys);
600 static void OutClose( vlc_object_t *p_this )
602 Close( p_this, GET_OUT_SYS(p_this));
606 /*****************************************************************************
607 * Seek: try to go at the right place
608 *****************************************************************************/
609 static int _Seek( vlc_object_t *p_access, access_sys_t *p_sys, uint64_t i_pos )
611 msg_Dbg( p_access, "seeking to %"PRIu64, i_pos );
613 ftp_StopStream( (vlc_object_t *)p_access, p_sys );
614 if( ftp_StartStream( (vlc_object_t *)p_access, p_sys, i_pos ) < 0 )
620 static int Seek( access_t *p_access, uint64_t i_pos )
622 int val = _Seek( (vlc_object_t *)p_access, p_access->p_sys, i_pos );
626 p_access->info.b_eof = false;
627 p_access->info.i_pos = i_pos;
632 static int OutSeek( sout_access_out_t *p_access, off_t i_pos )
634 return _Seek( (vlc_object_t *)p_access, GET_OUT_SYS( p_access ), i_pos);
637 /*****************************************************************************
639 *****************************************************************************/
640 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
642 access_sys_t *p_sys = p_access->p_sys;
644 assert( p_sys->fd_data != -1 );
645 assert( !p_sys->out );
647 if( p_access->info.b_eof )
650 if( p_sys->directory )
652 char *psz_line = net_Gets( p_access, p_sys->fd_data, NULL );
655 p_access->info.b_eof = true;
660 snprintf( (char*)p_buffer, i_len, "ftp://%s:%d/%s/%s\n",
661 p_sys->url.psz_host, p_sys->url.i_port,
662 p_sys->url.psz_path, psz_line );
664 return strlen( (const char *)p_buffer );
669 int i_read = net_Read( p_access, p_sys->fd_data, NULL,
670 p_buffer, i_len, false );
672 p_access->info.b_eof = true;
673 else if( i_read > 0 )
674 p_access->info.i_pos += i_read;
680 /*****************************************************************************
682 *****************************************************************************/
683 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
685 access_sys_t *p_sys = GET_OUT_SYS(p_access);
688 assert( p_sys->fd_data != -1 );
690 while( p_buffer != NULL )
692 block_t *p_next = p_buffer->p_next;;
694 i_write += net_Write( p_access, p_sys->fd_data, NULL,
695 p_buffer->p_buffer, p_buffer->i_buffer );
696 block_Release( p_buffer );
704 /*****************************************************************************
706 *****************************************************************************/
707 static int Control( access_t *p_access, int i_query, va_list args )
715 case ACCESS_CAN_SEEK:
716 pb_bool = (bool*)va_arg( args, bool* );
717 *pb_bool = !p_access->p_sys->directory;
719 case ACCESS_CAN_FASTSEEK:
720 pb_bool = (bool*)va_arg( args, bool* );
723 case ACCESS_CAN_PAUSE:
724 pb_bool = (bool*)va_arg( args, bool* );
725 *pb_bool = true; /* FIXME */
727 case ACCESS_CAN_CONTROL_PACE:
728 pb_bool = (bool*)va_arg( args, bool* );
729 *pb_bool = true; /* FIXME */
733 case ACCESS_GET_PTS_DELAY:
734 pi_64 = (int64_t*)va_arg( args, int64_t * );
735 *pi_64 = INT64_C(1000)
736 * var_InheritInteger( p_access, "network-caching" );
740 case ACCESS_SET_PAUSE_STATE:
741 pb_bool = (bool*)va_arg( args, bool* );
743 return Seek( p_access, p_access->info.i_pos );
746 case ACCESS_GET_TITLE_INFO:
747 case ACCESS_SET_TITLE:
748 case ACCESS_SET_SEEKPOINT:
749 case ACCESS_SET_PRIVATE_ID_STATE:
750 case ACCESS_GET_CONTENT_TYPE:
751 case ACCESS_GET_META:
755 msg_Warn( p_access, "unimplemented query in control: %d", i_query);
762 static int ftp_StartStream( vlc_object_t *p_access, access_sys_t *p_sys,
765 char psz_ipv4[16], *psz_ip = p_sys->sz_epsv_ip;
767 char *psz_arg, *psz_parser;
770 assert( p_sys->fd_data == -1 );
772 if( ( ftp_SendCommand( p_access, p_sys, *psz_ip ? "EPSV" : "PASV" ) < 0 )
773 || ( ftp_RecvCommand( p_access, p_sys, &i_answer, &psz_arg ) != 2 ) )
775 msg_Err( p_access, "cannot set passive mode" );
779 psz_parser = strchr( psz_arg, '(' );
780 if( psz_parser == NULL )
783 msg_Err( p_access, "cannot parse passive mode response" );
789 char psz_fmt[7] = "(|||%u";
790 psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1];
792 if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 )
795 msg_Err( p_access, "cannot parse passive mode response" );
801 unsigned a1, a2, a3, a4, p1, p2;
803 if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,
804 &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )
805 || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )
808 msg_Err( p_access, "cannot parse passive mode response" );
812 sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );
814 i_port = (p1 << 8) | p2;
818 msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
820 if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
821 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) != 2 )
823 msg_Err( p_access, "cannot set binary transfer mode" );
829 if( ftp_SendCommand( p_access, p_sys, "REST %"PRIu64, i_start ) < 0 ||
830 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) > 3 )
832 msg_Err( p_access, "cannot set restart offset" );
837 msg_Dbg( p_access, "waiting for data connection..." );
838 p_sys->fd_data = net_ConnectTCP( p_access, psz_ip, i_port );
839 if( p_sys->fd_data < 0 )
841 msg_Err( p_access, "failed to connect with server" );
844 msg_Dbg( p_access, "connection with \"%s:%d\" successful",
847 if( p_sys->directory )
849 if( ftp_SendCommand( p_access, p_sys, "NLST" ) < 0 ||
850 ftp_RecvCommand( p_access, p_sys, NULL, &psz_arg ) > 2 )
852 msg_Err( p_access, "cannot list directory contents" );
859 assert( p_sys->url.psz_path );
860 if( ftp_SendCommand( p_access, p_sys, "%s %s",
861 p_sys->out ? "STOR" : "RETR",
862 p_sys->url.psz_path ) < 0
863 || ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) > 2 )
865 msg_Err( p_access, "cannot retrieve file" );
870 shutdown( p_sys->fd_data, p_sys->out ? SHUT_RD : SHUT_WR );
875 static int ftp_StopStream ( vlc_object_t *p_access, access_sys_t *p_sys )
877 if( ftp_SendCommand( p_access, p_sys, "ABOR" ) < 0 )
879 msg_Warn( p_access, "cannot abort file" );
880 if( p_sys->fd_data > 0 )
881 net_Close( p_sys->fd_data );
886 if( p_sys->fd_data != -1 )
888 net_Close( p_sys->fd_data );
890 /* Read the final response from RETR/STOR, i.e. 426 or 226 */
891 ftp_RecvCommand( p_access, p_sys, NULL, NULL );
893 /* Read the response from ABOR, i.e. 226 or 225 */
894 ftp_RecvCommand( p_access, p_sys, NULL, NULL );