contrib: cargo: use the 0.6.13 cargo-c version
[vlc.git] / modules / access / ftp.c
blob553e5b5ddbdf2efd92dbe472abf3591b5cdab0c2
1 /*****************************************************************************
2 * ftp.c: FTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2006 VLC authors and VideoLAN
5 * Copyright © 2006 Rémi Denis-Courmont
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr> - original code
8 * Rémi Denis-Courmont - EPSV support
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <assert.h>
33 #include <stdint.h>
34 #include <errno.h>
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_access.h>
39 #include <vlc_dialog.h>
40 #include <vlc_input_item.h>
41 #include <vlc_network.h>
42 #include <vlc_url.h>
43 #include <vlc_tls.h>
44 #include <vlc_sout.h>
45 #include <vlc_charset.h>
46 #include <vlc_interrupt.h>
47 #include <vlc_keystore.h>
49 #ifndef IPPORT_FTP
50 # define IPPORT_FTP 21u
51 #endif
53 #ifndef IPPORT_FTPS
54 # define IPPORT_FTPS 990u
55 #endif
57 /*****************************************************************************
58 * Module descriptor
59 *****************************************************************************/
60 static int InOpen ( vlc_object_t * );
61 static void InClose( vlc_object_t * );
62 #ifdef ENABLE_SOUT
63 static int OutOpen ( vlc_object_t * );
64 static void OutClose( vlc_object_t * );
65 #endif
67 #define USER_TEXT N_("Username")
68 #define USER_LONGTEXT N_("Username that will be used for the connection, " \
69 "if no username is set in the URL.")
70 #define PASS_TEXT N_("Password")
71 #define PASS_LONGTEXT N_("Password that will be used for the connection, " \
72 "if no username or password are set in URL.")
73 #define ACCOUNT_TEXT N_("FTP account")
74 #define ACCOUNT_LONGTEXT N_("Account that will be " \
75 "used for the connection.")
77 #define LOGIN_DIALOG_TITLE _("FTP authentication")
78 #define LOGIN_DIALOG_TEXT _("Please enter a valid login and password for " \
79 "the ftp connexion to %s")
81 vlc_module_begin ()
82 set_shortname( "FTP" )
83 set_description( N_("FTP input") )
84 set_capability( "access", 0 )
85 set_category( CAT_INPUT )
86 set_subcategory( SUBCAT_INPUT_ACCESS )
87 add_string( "ftp-user", NULL, USER_TEXT, USER_LONGTEXT, false )
88 add_password( "ftp-pwd", NULL, PASS_TEXT, PASS_LONGTEXT )
89 add_string( "ftp-account", "anonymous", ACCOUNT_TEXT,
90 ACCOUNT_LONGTEXT, false )
91 add_shortcut( "ftp", "ftps", "ftpes" )
92 set_callbacks( InOpen, InClose )
94 #ifdef ENABLE_SOUT
95 add_submodule ()
96 set_shortname( "FTP" )
97 set_description( N_("FTP upload output") )
98 set_capability( "sout access", 0 )
99 set_category( CAT_SOUT )
100 set_subcategory( SUBCAT_SOUT_ACO )
101 add_shortcut( "ftp", "ftps", "ftpes" )
102 set_callbacks( OutOpen, OutClose )
103 #endif
104 vlc_module_end ()
106 /*****************************************************************************
107 * Local prototypes
108 *****************************************************************************/
110 typedef struct access_sys_t access_sys_t;
112 static ssize_t Read( stream_t *, void *, size_t );
113 static int Seek( stream_t *, uint64_t );
114 static int Control( stream_t *, int, va_list );
115 static int DirRead( stream_t *, input_item_node_t * );
116 #ifdef ENABLE_SOUT
117 static int OutSeek( sout_access_out_t *, off_t );
118 static ssize_t Write( sout_access_out_t *, block_t * );
119 #endif
121 static int LoginUserPwd( vlc_object_t *, access_sys_t *,
122 const char *, const char *, bool * );
123 static void FeaturesCheck( void *, const char * );
125 typedef struct ftp_features_t
127 bool b_unicode;
128 bool b_authtls;
129 bool b_mlst;
130 } ftp_features_t;
132 enum tls_mode_e
134 NONE = 0,
135 IMPLICIT,/* ftps */
136 EXPLICIT /* ftpes */
139 struct access_sys_t
141 vlc_url_t url;
143 ftp_features_t features;
144 vlc_tls_client_t *p_creds;
145 enum tls_mode_e tlsmode;
146 vlc_tls_t *cmd;
147 vlc_tls_t *data;
149 char sz_epsv_ip[NI_MAXNUMERICHOST];
150 bool out;
151 uint64_t offset;
152 uint64_t size;
154 #define GET_OUT_SYS( p_this ) \
155 ((access_sys_t *)(((sout_access_out_t *)(p_this))->p_sys))
157 VLC_FORMAT(3, 4)
158 static int ftp_SendCommand( vlc_object_t *obj, access_sys_t *sys,
159 const char *fmt, ... )
161 size_t fmtlen = strlen( fmt );
162 char fmtbuf[fmtlen + 3];
164 memcpy( fmtbuf, fmt, fmtlen );
165 memcpy( fmtbuf + fmtlen, "\r\n", 3 );
167 va_list args;
168 char *cmd;
169 int val;
171 va_start( args, fmt );
172 val = vasprintf( &cmd, fmtbuf, args );
173 va_end( args );
174 if( unlikely(val == -1) )
175 return -1;
177 if( strncmp( cmd, "PASS ", 5 ) && strncmp( cmd, "ACCT ", 5 ) )
178 msg_Dbg( obj, "sending request: \"%.*s\" (%d bytes)", val-2, cmd, val );
179 else
180 msg_Dbg( obj, "sending request: \"%.*s XXXX\" (XX bytes)", 4, cmd );
182 if( vlc_tls_Write( sys->cmd, cmd, val ) != val )
184 msg_Err( obj, "request failure" );
185 val = -1;
187 else
188 val = 0;
189 free( cmd );
190 return val;
193 static char *ftp_GetLine( vlc_object_t *obj, access_sys_t *sys )
195 char *resp = vlc_tls_GetLine( sys->cmd );
196 if( resp == NULL )
197 msg_Err( obj, "response failure" );
198 return resp;
201 /* TODO support this s**t :
202 RFC 959 allows the client to send certain TELNET strings at any moment,
203 even in the middle of a request:
205 * \377\377.
206 * \377\376x where x is one byte.
207 * \377\375x where x is one byte. The server is obliged to send \377\374x
208 * immediately after reading x.
209 * \377\374x where x is one byte.
210 * \377\373x where x is one byte. The server is obliged to send \377\376x
211 * immediately after reading x.
212 * \377x for any other byte x.
214 These strings are not part of the requests, except in the case \377\377,
215 where the request contains one \377. */
216 static int ftp_RecvReply( vlc_object_t *obj, access_sys_t *sys,
217 char **restrict strp,
218 void (*cb)(void *, const char *), void *opaque )
220 char *resp = ftp_GetLine( obj, sys );
221 if( resp == NULL )
222 return -1;
224 char *end;
225 unsigned code = strtoul( resp, &end, 10 );
226 if( (end - resp) != 3 || (*end != '-' && *end != ' ') )
228 msg_Err( obj, "malformatted response" );
229 goto error;
231 msg_Dbg( obj, "received response: \"%s\"", resp );
233 if( *end == '-' ) /* Multi-line response */
235 bool done;
237 *end = ' ';
240 char *line = ftp_GetLine( obj, sys );
241 if( line == NULL )
242 goto error;
244 done = !strncmp( resp, line, 4 );
245 if( !done )
246 cb( opaque, line );
247 free( line );
249 while( !done );
252 if( strp != NULL )
253 *strp = resp;
254 else
255 free( resp );
256 return code;
257 error:
258 free( resp );
259 return -1;
262 static int ftp_RecvAnswer( vlc_object_t *obj, access_sys_t *sys,
263 int *restrict codep, char **restrict strp,
264 void (*cb)(void *, const char *), void *opaque )
266 char *str;
267 int val = ftp_RecvReply( obj, sys, &str, cb, opaque );
268 if( (val / 100) == 1 )
269 { /* There can be zero or one preliminary reply per command */
270 free( str );
271 val = ftp_RecvReply( obj, sys, &str, cb, opaque );
274 if( val >= 0 )
276 if( codep != NULL )
277 *codep = val;
278 if( strp != NULL )
279 *strp = str;
280 else
281 free( str );
282 val /= 100;
284 else
286 if( codep != NULL )
287 *codep = 500;
288 if( strp != NULL )
289 *strp = NULL;
291 return val;
294 static void DummyLine( void *data, const char *str )
296 (void) data; (void) str;
299 static int ftp_RecvCommand( vlc_object_t *obj, access_sys_t *sys,
300 int *restrict codep, char **restrict strp )
302 return ftp_RecvAnswer( obj, sys, codep, strp, DummyLine, NULL );
305 static int ftp_RecvCommandInit( vlc_object_t *obj, access_sys_t *sys )
307 int val = ftp_RecvReply( obj, sys, NULL, DummyLine, NULL );
308 if( val >= 0 )
309 val /= 100;
310 return val;
313 static int ftp_StartStream( vlc_object_t *, access_sys_t *, uint64_t, bool );
314 static int ftp_StopStream ( vlc_object_t *, access_sys_t * );
316 static int readTLSMode( vlc_object_t *obj, access_sys_t *p_sys,
317 const char * psz_access )
319 if ( !strncmp( psz_access, "ftps", 4 ) )
320 p_sys->tlsmode = IMPLICIT;
321 else
322 if ( !strncmp( psz_access, "ftpes", 5 ) )
323 p_sys->tlsmode = EXPLICIT;
324 else
326 p_sys->p_creds = NULL;
327 p_sys->tlsmode = NONE;
328 return 0;
331 p_sys->p_creds = vlc_tls_ClientCreate( obj );
332 return (p_sys->p_creds != NULL) ? 0 : -1;
335 static int createCmdTLS( vlc_object_t *p_access, access_sys_t *p_sys,
336 const char *psz_session_name )
338 /* TLS/SSL handshake */
339 vlc_tls_t *secure = vlc_tls_ClientSessionCreate( p_sys->p_creds,
340 p_sys->cmd,
341 p_sys->url.psz_host,
342 psz_session_name,
343 NULL, NULL );
344 if( secure == NULL )
346 msg_Err( p_access, "cannot establish FTP/TLS session on command channel" );
347 return -1;
349 p_sys->cmd = secure;
350 return 0;
353 static void clearCmd( access_sys_t *p_sys )
355 if( p_sys->cmd != NULL )
357 vlc_tls_Close( p_sys->cmd );
358 p_sys->cmd = NULL;
362 static int Login( vlc_object_t *p_access, access_sys_t *p_sys, const char *path )
364 int i_answer;
366 /* *** Open a TCP connection with server *** */
367 p_sys->cmd = vlc_tls_SocketOpenTCP( p_access, p_sys->url.psz_host,
368 p_sys->url.i_port );
369 if( p_sys->cmd == NULL )
371 msg_Err( p_access, "connection failed" );
372 vlc_dialog_display_error( p_access, _("Network interaction failed"), "%s",
373 _("VLC could not connect with the given server.") );
374 goto error;
377 if ( p_sys->tlsmode == IMPLICIT ) /* FTPS Mode */
379 if ( createCmdTLS( p_access, p_sys, "ftps") < 0 )
380 goto error;
383 while( ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) == 1 );
385 if( i_answer / 100 != 2 )
387 msg_Err( p_access, "connection rejected" );
388 vlc_dialog_display_error( p_access, _("Network interaction failed"), "%s",
389 _("VLC's connection to the given server was rejected.") );
390 goto error;
393 msg_Dbg( p_access, "connection accepted (%d)", i_answer );
395 /* Features check first */
396 if( ftp_SendCommand( p_access, p_sys, "FEAT" ) < 0
397 || ftp_RecvAnswer( p_access, p_sys, NULL, NULL,
398 FeaturesCheck, &p_sys->features ) < 0 )
400 msg_Err( p_access, "cannot get server features" );
401 goto error;
404 /* Create TLS Session */
405 if( p_sys->tlsmode == EXPLICIT )
407 if ( ! p_sys->features.b_authtls )
409 msg_Err( p_access, "Server does not support TLS" );
410 goto error;
413 if( ftp_SendCommand( p_access, p_sys, "AUTH TLS" ) < 0
414 || ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0
415 || i_answer != 234 )
417 msg_Err( p_access, "cannot switch to TLS: server replied with code %d",
418 i_answer );
419 goto error;
422 if( createCmdTLS( p_access, p_sys, "ftpes") < 0 )
424 goto error;
428 if( p_sys->tlsmode != NONE )
430 if( ftp_SendCommand( p_access, p_sys, "PBSZ 0" ) < 0 ||
431 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 ||
432 i_answer != 200 )
434 msg_Err( p_access, "Can't truncate Protection buffer size for TLS" );
435 goto error;
438 if( ftp_SendCommand( p_access, p_sys, "PROT P" ) < 0 ||
439 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 ||
440 i_answer != 200 )
442 msg_Err( p_access, "Can't set Data channel protection" );
443 goto error;
447 vlc_url_t url;
448 vlc_credential credential;
449 if( vlc_UrlParseFixup( &url, path ) != 0 )
451 vlc_UrlClean( &url );
452 goto error;
454 vlc_credential_init( &credential, &url );
455 bool b_logged = false;
457 /* First: try credentials from url / option */
458 vlc_credential_get( &credential, p_access, "ftp-user", "ftp-pwd",
459 NULL, NULL );
462 const char *psz_username = credential.psz_username;
464 if( psz_username == NULL ) /* use anonymous by default */
465 psz_username = "anonymous";
467 if( LoginUserPwd( p_access, p_sys, psz_username,
468 credential.psz_password, &b_logged ) != 0
469 || b_logged )
470 break;
472 while( vlc_credential_get( &credential, p_access, "ftp-user", "ftp-pwd",
473 LOGIN_DIALOG_TITLE, LOGIN_DIALOG_TEXT,
474 url.psz_host ) );
476 if( b_logged )
478 vlc_credential_store( &credential, p_access );
479 vlc_credential_clean( &credential );
480 vlc_UrlClean( &url );
481 return 0;
483 vlc_credential_clean( &credential );
484 vlc_UrlClean( &url );
485 error:
486 clearCmd( p_sys );
487 return -1;
490 static int LoginUserPwd( vlc_object_t *p_access, access_sys_t *p_sys,
491 const char *psz_user, const char *psz_pwd,
492 bool *p_logged )
494 int i_answer;
496 /* Send credentials over channel */
497 if( ftp_SendCommand( p_access, p_sys, "USER %s", psz_user ) < 0 ||
498 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
499 return -1;
501 switch( i_answer / 100 )
503 case 2:
504 /* X.509 auth successful after AUTH TLS / RFC 2228 sec. 4 */
505 if ( i_answer == 232 )
506 msg_Dbg( p_access, "user accepted and authenticated" );
507 else
508 msg_Dbg( p_access, "user accepted" );
509 break;
510 case 3:
511 msg_Dbg( p_access, "password needed" );
513 if( ftp_SendCommand( p_access, p_sys, "PASS %s", psz_pwd ) < 0 ||
514 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
515 return -1;
517 switch( i_answer / 100 )
519 case 2:
520 msg_Dbg( p_access, "password accepted" );
521 break;
522 case 3:
524 char *psz;
525 msg_Dbg( p_access, "account needed" );
526 psz = var_InheritString( p_access, "ftp-account" );
527 if( ftp_SendCommand( p_access, p_sys, "ACCT %s",
528 psz ) < 0 ||
529 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) < 0 )
531 free( psz );
532 return -1;
534 free( psz );
536 if( i_answer / 100 != 2 )
538 msg_Err( p_access, "account rejected" );
539 vlc_dialog_display_error( p_access,
540 _("Network interaction failed"),
541 "%s", _("Your account was rejected.") );
542 return -1;
544 msg_Dbg( p_access, "account accepted" );
545 break;
548 default:
549 msg_Warn( p_access, "password rejected" );
550 *p_logged = false;
551 return 0;
553 break;
554 default:
555 msg_Warn( p_access, "user rejected" );
556 *p_logged = false;
557 return 0;
560 *p_logged = true;
561 return 0;
564 static void FeaturesCheck( void *opaque, const char *feature )
566 ftp_features_t *features = opaque;
568 if( strcasestr( feature, "UTF8" ) != NULL )
569 features->b_unicode = true;
570 else
571 if( strcasestr( feature, "AUTH TLS" ) != NULL )
572 features->b_authtls = true;
574 if( strcasestr( feature, "MLST" ) != NULL )
575 features->b_mlst = true;
578 static int Connect( vlc_object_t *p_access, access_sys_t *p_sys, const char *path )
580 if( Login( p_access, p_sys, path ) < 0 )
581 return -1;
583 /* Extended passive mode */
584 if( ftp_SendCommand( p_access, p_sys, "EPSV ALL" ) < 0 )
586 msg_Err( p_access, "cannot request extended passive mode" );
587 goto error;
590 if( ftp_RecvCommand( p_access, p_sys, NULL, NULL ) == 2 )
592 int fd = vlc_tls_GetFD(p_sys->cmd);
593 if( net_GetPeerAddress( fd, p_sys->sz_epsv_ip, NULL ) )
594 goto error;
596 else
598 /* If ESPV ALL fails, we fallback to PASV.
599 * We have to restart the connection in case there is a NAT that
600 * understands EPSV ALL in the way, and hence won't allow PASV on
601 * the initial connection.
603 msg_Info( p_access, "FTP Extended passive mode disabled" );
604 clearCmd( p_sys );
606 if( Login( p_access, p_sys, path ) )
607 goto error;
610 if( p_sys->url.psz_path &&
611 (p_sys->features.b_unicode ? IsUTF8 : IsASCII)(p_sys->url.psz_path) == NULL )
613 msg_Err( p_access, "unsupported path: \"%s\"", p_sys->url.psz_path );
614 goto error;
617 /* check binary mode support */
618 if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
619 ftp_RecvCommand( p_access, p_sys, NULL, NULL ) != 2 )
621 msg_Err( p_access, "cannot set binary transfer mode" );
622 goto error;
625 return 0;
627 error:
628 clearCmd( p_sys );
629 return -1;
633 static int parseURL( vlc_url_t *url, const char *path, enum tls_mode_e mode )
635 if( path == NULL )
636 return VLC_EGENERIC;
638 /* *** Parse URL and get server addr/port and path *** */
639 while( *path == '/' )
640 path++;
642 vlc_UrlParseFixup( url, path );
644 if( url->psz_host == NULL || *url->psz_host == '\0' )
645 return VLC_EGENERIC;
647 if( url->i_port <= 0 )
649 if( mode == IMPLICIT )
650 url->i_port = IPPORT_FTPS;
651 else
652 url->i_port = IPPORT_FTP; /* default port */
655 if( url->psz_path == NULL )
656 return VLC_SUCCESS;
657 /* FTP URLs are relative to user's default directory (RFC1738 §3.2)
658 For absolute path use ftp://foo.bar//usr/local/etc/filename */
659 /* FIXME: we should issue a series of CWD, one per slash */
660 if( url->psz_path )
662 assert( url->psz_path[0] == '/' );
663 url->psz_path++;
666 char *type = strstr( url->psz_path, ";type=" );
667 if( type )
669 *type = '\0';
670 if( strchr( "iI", type[6] ) == NULL )
671 return VLC_EGENERIC; /* ASCII and directory not supported */
673 vlc_uri_decode( url->psz_path );
674 return VLC_SUCCESS;
678 /****************************************************************************
679 * Open: connect to ftp server and ask for file
680 ****************************************************************************/
681 static int InOpen( vlc_object_t *p_this )
683 stream_t *p_access = (stream_t*)p_this;
684 access_sys_t *p_sys;
685 char *psz_arg;
686 bool b_directory;
688 /* Init p_access */
689 p_sys = p_access->p_sys = (access_sys_t*)vlc_obj_calloc( p_this, 1, sizeof( access_sys_t ) );
690 if( !p_sys )
691 return VLC_ENOMEM;
692 p_sys->data = NULL;
693 p_sys->out = false;
694 p_sys->offset = 0;
695 p_sys->size = UINT64_MAX;
697 if( readTLSMode( p_this, p_sys, p_access->psz_name ) )
698 goto exit_error;
700 if( parseURL( &p_sys->url, p_access->psz_url, p_sys->tlsmode ) )
701 goto exit_error;
703 if( Connect( p_this, p_sys, p_access->psz_url ) )
704 goto exit_error;
706 do {
707 /* get size */
708 if( p_sys->url.psz_path == NULL || !*p_sys->url.psz_path )
710 b_directory = true;
711 break;
714 if( ftp_SendCommand( p_this, p_sys, "SIZE %s",
715 p_sys->url.psz_path ) < 0 )
716 goto error;
718 int val = ftp_RecvCommand( p_this, p_sys, NULL, &psz_arg );
719 if( val == 2 )
721 b_directory = false;
722 p_sys->size = atoll( &psz_arg[4] );
723 free( psz_arg );
724 msg_Dbg( p_access, "file size: %"PRIu64, p_sys->size );
725 break;
727 if( val >= 0 )
728 free( psz_arg );
730 if( ftp_SendCommand( p_this, p_sys, "CWD %s",
731 p_sys->url.psz_path ) < 0 )
732 goto error;
734 if( ftp_RecvCommand( p_this, p_sys, NULL, NULL ) == 2 )
736 b_directory = true;
737 break;
740 msg_Err( p_this, "file or directory does not exist" );
741 goto error;
742 } while (0);
744 if( b_directory )
746 p_access->pf_readdir = DirRead;
747 p_access->pf_control = access_vaDirectoryControlHelper;
748 } else
749 ACCESS_SET_CALLBACKS( Read, NULL, Control, Seek ); \
751 /* Start the 'stream' */
752 if( ftp_StartStream( p_this, p_sys, 0, b_directory ) < 0 )
754 msg_Err( p_this, "cannot retrieve file" );
755 goto error;
758 return VLC_SUCCESS;
760 error:
761 clearCmd( p_sys );
763 exit_error:
764 vlc_UrlClean( &p_sys->url );
765 vlc_tls_ClientDelete( p_sys->p_creds );
766 return VLC_EGENERIC;
769 #ifdef ENABLE_SOUT
770 static int OutOpen( vlc_object_t *p_this )
772 sout_access_out_t *p_access = (sout_access_out_t *)p_this;
773 access_sys_t *p_sys;
775 p_sys = vlc_obj_calloc( p_this, 1, sizeof( *p_sys ) );
776 if( !p_sys )
777 return VLC_ENOMEM;
779 /* Init p_access */
780 p_sys->data = NULL;
781 p_sys->out = true;
783 if( readTLSMode( p_this, p_sys, p_access->psz_access ) )
784 goto exit_error;
786 if( parseURL( &p_sys->url, p_access->psz_path, p_sys->tlsmode ) )
787 goto exit_error;
788 if( p_sys->url.psz_path == NULL )
790 msg_Err( p_this, "no filename specified" );
791 goto exit_error;
794 if( Connect( p_this, p_sys, p_access->psz_path ) )
795 goto exit_error;
797 /* Start the 'stream' */
798 if( ftp_StartStream( p_this, p_sys, 0, false ) < 0 )
800 msg_Err( p_access, "cannot store file" );
801 clearCmd( p_sys );
802 goto exit_error;
805 p_access->pf_seek = OutSeek;
806 p_access->pf_write = Write;
807 p_access->p_sys = (void *)p_sys;
809 return VLC_SUCCESS;
811 exit_error:
812 vlc_UrlClean( &p_sys->url );
813 vlc_tls_ClientDelete( p_sys->p_creds );
814 return VLC_EGENERIC;
816 #endif
818 /*****************************************************************************
819 * Close: free unused data structures
820 *****************************************************************************/
821 static void Close( vlc_object_t *p_access, access_sys_t *p_sys )
823 msg_Dbg( p_access, "stopping stream" );
824 ftp_StopStream( p_access, p_sys );
826 if( ftp_SendCommand( p_access, p_sys, "QUIT" ) < 0 )
828 msg_Warn( p_access, "cannot quit" );
830 else
832 ftp_RecvCommand( p_access, p_sys, NULL, NULL );
835 clearCmd( p_sys );
837 /* free memory */
838 vlc_UrlClean( &p_sys->url );
839 vlc_tls_ClientDelete( p_sys->p_creds );
842 static void InClose( vlc_object_t *p_this )
844 Close( p_this, ((stream_t *)p_this)->p_sys);
847 #ifdef ENABLE_SOUT
848 static void OutClose( vlc_object_t *p_this )
850 Close( p_this, GET_OUT_SYS(p_this));
852 #endif
855 /*****************************************************************************
856 * Seek: try to go at the right place
857 *****************************************************************************/
858 static int SeekCommon( vlc_object_t *p_access, access_sys_t *p_sys,
859 uint64_t i_pos )
861 msg_Dbg( p_access, "seeking to %"PRIu64, i_pos );
863 ftp_StopStream( p_access, p_sys );
865 if( ftp_StartStream( p_access, p_sys, i_pos, false ) < 0 )
866 return VLC_EGENERIC;
867 return VLC_SUCCESS;
870 static int Seek( stream_t *p_access, uint64_t i_pos )
872 access_sys_t *p_sys = p_access->p_sys;
874 int val = SeekCommon( (vlc_object_t *)p_access, p_sys, i_pos );
875 if( val )
876 return val;
878 p_sys->offset = i_pos;
880 return VLC_SUCCESS;
883 #ifdef ENABLE_SOUT
884 static int OutSeek( sout_access_out_t *p_access, off_t i_pos )
886 return SeekCommon((vlc_object_t *)p_access, GET_OUT_SYS(p_access), i_pos);
888 #endif
890 /*****************************************************************************
891 * Read:
892 *****************************************************************************/
893 static ssize_t Read( stream_t *p_access, void *p_buffer, size_t i_len )
895 access_sys_t *p_sys = p_access->p_sys;
897 if( p_sys->data == NULL )
898 return 0;
899 assert( !p_sys->out );
901 ssize_t i_read = vlc_tls_Read( p_sys->data, p_buffer, i_len, false );
902 if( i_read >= 0 )
903 p_sys->offset += i_read;
904 else if( errno != EINTR && errno != EAGAIN )
906 msg_Err( p_access, "receive error: %s", vlc_strerror_c(errno) );
907 i_read = 0;
910 return i_read;
913 /*****************************************************************************
914 * DirRead:
915 *****************************************************************************/
916 static int DirRead (stream_t *p_access, input_item_node_t *p_current_node)
918 access_sys_t *p_sys = p_access->p_sys;
919 int i_ret = VLC_SUCCESS;
921 assert( p_sys->data != NULL );
922 assert( !p_sys->out );
924 struct vlc_readdir_helper rdh;
925 vlc_readdir_helper_init( &rdh, p_access, p_current_node );
927 while (i_ret == VLC_SUCCESS)
929 char *psz_file;
930 int type = ITEM_TYPE_UNKNOWN;
932 char *psz_line = vlc_tls_GetLine( p_sys->data );
933 if( psz_line == NULL )
934 break;
936 if( p_sys->features.b_mlst )
938 /* MLST Format is key=val;key=val...; FILENAME */
939 if( strstr( psz_line, "type=dir" ) )
940 type = ITEM_TYPE_DIRECTORY;
941 if( strstr( psz_line, "type=file" ) )
942 type = ITEM_TYPE_FILE;
944 /* Get the filename or fail */
945 psz_file = strchr( psz_line, ' ' );
946 if( psz_file )
947 psz_file++;
948 else
950 msg_Warn( p_access, "Empty filename in MLST list" );
951 free( psz_line );
952 continue;
955 else
956 psz_file = psz_line;
958 char *psz_uri;
959 char *psz_filename = vlc_uri_encode( psz_file );
960 if( psz_filename != NULL &&
961 asprintf( &psz_uri, "%s://%s:%d%s%s/%s",
962 ( p_sys->tlsmode == NONE ) ? "ftp" :
963 ( ( p_sys->tlsmode == IMPLICIT ) ? "ftps" : "ftpes" ),
964 p_sys->url.psz_host, p_sys->url.i_port,
965 p_sys->url.psz_path ? "/" : "",
966 p_sys->url.psz_path ? p_sys->url.psz_path : "",
967 psz_filename ) != -1 )
969 i_ret = vlc_readdir_helper_additem( &rdh, psz_uri, NULL, psz_file,
970 type, ITEM_NET );
971 free( psz_uri );
973 free( psz_filename );
974 free( psz_line );
977 vlc_readdir_helper_finish( &rdh, i_ret == VLC_SUCCESS );
978 return i_ret;
981 /*****************************************************************************
982 * Write:
983 *****************************************************************************/
984 #ifdef ENABLE_SOUT
985 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
987 access_sys_t *p_sys = GET_OUT_SYS(p_access);
988 size_t i_write = 0;
990 assert( p_sys->data != NULL );
992 while( p_buffer != NULL )
994 block_t *p_next = p_buffer->p_next;
996 i_write += vlc_tls_Write( p_sys->data,
997 p_buffer->p_buffer, p_buffer->i_buffer );
998 block_Release( p_buffer );
1000 p_buffer = p_next;
1003 return i_write;
1005 #endif
1007 /*****************************************************************************
1008 * Control:
1009 *****************************************************************************/
1010 static int Control( stream_t *p_access, int i_query, va_list args )
1012 access_sys_t *sys = p_access->p_sys;
1013 bool *pb_bool;
1015 switch( i_query )
1017 case STREAM_CAN_SEEK:
1018 pb_bool = va_arg( args, bool * );
1019 *pb_bool = true;
1020 break;
1021 case STREAM_CAN_FASTSEEK:
1022 pb_bool = va_arg( args, bool * );
1023 *pb_bool = false;
1024 break;
1025 case STREAM_CAN_PAUSE:
1026 pb_bool = va_arg( args, bool * );
1027 *pb_bool = true; /* FIXME */
1028 break;
1029 case STREAM_CAN_CONTROL_PACE:
1030 pb_bool = va_arg( args, bool * );
1031 *pb_bool = true; /* FIXME */
1032 break;
1033 case STREAM_GET_SIZE:
1034 if( sys->size == UINT64_MAX )
1035 return VLC_EGENERIC;
1036 *va_arg( args, uint64_t * ) = sys->size;
1037 break;
1039 case STREAM_GET_PTS_DELAY:
1040 *va_arg( args, vlc_tick_t * ) =
1041 VLC_TICK_FROM_MS(var_InheritInteger( p_access, "network-caching" ));
1042 break;
1044 case STREAM_SET_PAUSE_STATE:
1045 pb_bool = va_arg( args, bool * );
1046 if ( !pb_bool )
1047 return Seek( p_access, sys->offset );
1048 break;
1050 default:
1051 return VLC_EGENERIC;
1054 return VLC_SUCCESS;
1057 static int ftp_StartStream( vlc_object_t *p_access, access_sys_t *p_sys,
1058 uint64_t i_start, bool b_directory )
1060 char psz_ipv4[16], *psz_ip = p_sys->sz_epsv_ip;
1061 int i_answer;
1062 char *psz_arg, *psz_parser;
1063 int i_port;
1065 assert( p_sys->data == NULL );
1067 if( ( ftp_SendCommand( p_access, p_sys, *psz_ip ? "EPSV" : "PASV" ) < 0 )
1068 || ( ftp_RecvCommand( p_access, p_sys, &i_answer, &psz_arg ) != 2 ) )
1070 msg_Err( p_access, "cannot set passive mode" );
1071 return VLC_EGENERIC;
1074 psz_parser = strchr( psz_arg, '(' );
1075 if( psz_parser == NULL )
1077 free( psz_arg );
1078 msg_Err( p_access, "cannot parse passive mode response" );
1079 return VLC_EGENERIC;
1082 if( *psz_ip )
1084 if( sscanf( psz_parser, "(%*3c%u", &i_port ) < 1 )
1086 free( psz_arg );
1087 msg_Err( p_access, "cannot parse passive mode response" );
1088 return VLC_EGENERIC;
1091 else
1093 unsigned a1, a2, a3, a4, p1, p2;
1095 if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,
1096 &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )
1097 || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )
1099 free( psz_arg );
1100 msg_Err( p_access, "cannot parse passive mode response" );
1101 return VLC_EGENERIC;
1104 sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );
1105 psz_ip = psz_ipv4;
1106 i_port = (p1 << 8) | p2;
1108 free( psz_arg );
1110 msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
1112 if( ftp_SendCommand( p_access, p_sys, "TYPE I" ) < 0 ||
1113 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) != 2 )
1115 msg_Err( p_access, "cannot set binary transfer mode" );
1116 return VLC_EGENERIC;
1119 if( i_start > 0 )
1121 if( ftp_SendCommand( p_access, p_sys, "REST %"PRIu64, i_start ) < 0 ||
1122 ftp_RecvCommand( p_access, p_sys, &i_answer, NULL ) > 3 )
1124 msg_Err( p_access, "cannot set restart offset" );
1125 return VLC_EGENERIC;
1129 msg_Dbg( p_access, "waiting for data connection..." );
1130 p_sys->data = vlc_tls_SocketOpenTCP( p_access, psz_ip, i_port );
1131 if( p_sys->data == NULL )
1133 msg_Err( p_access, "failed to connect with server" );
1134 return VLC_EGENERIC;
1136 msg_Dbg( p_access, "connection with \"%s:%d\" successful",
1137 psz_ip, i_port );
1139 if( b_directory )
1141 if( p_sys->features.b_mlst &&
1142 ftp_SendCommand( p_access, p_sys, "MLSD" ) >= 0 &&
1143 ftp_RecvCommandInit( p_access, p_sys ) == 1 )
1145 msg_Dbg( p_access, "Using MLST extension to list" );
1147 else
1148 if( ftp_SendCommand( p_access, p_sys, "NLST" ) < 0 ||
1149 ftp_RecvCommandInit( p_access, p_sys ) == 1 )
1151 msg_Err( p_access, "cannot list directory contents" );
1152 return VLC_EGENERIC;
1155 else
1157 /* "1xx" message */
1158 assert( p_sys->url.psz_path );
1159 if( ftp_SendCommand( p_access, p_sys, "%s %s",
1160 p_sys->out ? "STOR" : "RETR",
1161 p_sys->url.psz_path ) < 0
1162 || ftp_RecvCommandInit( p_access, p_sys ) != 1 )
1164 msg_Err( p_access, "cannot retrieve file" );
1165 return VLC_EGENERIC;
1169 if( p_sys->tlsmode != NONE )
1171 /* FIXME: Do Reuse TLS Session */
1172 /* TLS/SSL handshake */
1173 vlc_tls_t *secure = vlc_tls_ClientSessionCreate( p_sys->p_creds,
1174 p_sys->data, p_sys->url.psz_host,
1175 ( p_sys->tlsmode == EXPLICIT ) ? "ftpes-data"
1176 : "ftps-data",
1177 NULL, NULL );
1178 if( secure == NULL )
1180 msg_Err( p_access, "cannot establish FTP/TLS session for data" \
1181 ": server not allowing new session ?" );
1182 return VLC_EGENERIC;
1184 p_sys->data = secure;
1187 return VLC_SUCCESS;
1190 static int ftp_StopStream ( vlc_object_t *p_access, access_sys_t *p_sys )
1192 int ret = VLC_SUCCESS;
1194 if( ftp_SendCommand( p_access, p_sys, "ABOR" ) < 0 )
1196 msg_Warn( p_access, "cannot abort file" );
1197 ret = VLC_EGENERIC;
1200 if( p_sys->data != NULL )
1202 vlc_tls_Close( p_sys->data );
1203 p_sys->data = NULL;
1205 if( ret == VLC_SUCCESS )
1206 /* Read the final response from RETR/STOR, i.e. 426 or 226 */
1207 ftp_RecvCommand( p_access, p_sys, NULL, NULL );
1210 if( ret == VLC_SUCCESS )
1211 /* Read the response from ABOR, i.e. 226 or 225 */
1212 ftp_RecvCommand( p_access, p_sys, NULL, NULL );
1214 return ret;