1 /*****************************************************************************
2 * sftp.c: SFTP input module
3 *****************************************************************************
4 * Copyright (C) 2009 the VideoLAN team
7 * Authors: Rémi Duraffort <ivoire@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
36 #include <vlc_access.h>
37 #include <vlc_dialog.h>
38 #include <vlc_network.h>
42 #include <libssh2_sftp.h>
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Open ( vlc_object_t* );
49 static void Close( vlc_object_t* );
51 #define CACHING_TEXT N_("Caching value in ms")
52 #define CACHING_LONGTEXT N_( \
53 "Caching value for SFTP streams. This value should be set in milliseconds." )
54 #define USER_TEXT N_("SFTP user name")
55 #define USER_LONGTEXT N_("User name that will be used for the connection.")
56 #define PASS_TEXT N_("SFTP password")
57 #define PASS_LONGTEXT N_("Password that will be used for the connection.")
58 #define PORT_TEXT N_("SFTP port")
59 #define PORT_LONGTEXT N_("SFTP port number to use on the server")
60 #define MTU_TEXT N_("Read size")
61 #define MTU_LONGTEXT N_("Size of the request for reading access")
64 set_shortname( "SFTP" )
65 set_description( N_("SFTP input") )
66 set_capability( "access", 0 )
67 set_category( CAT_INPUT )
68 set_subcategory( SUBCAT_INPUT_ACCESS )
69 add_integer( "sftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
70 CACHING_TEXT, CACHING_LONGTEXT, true );
71 add_integer( "sftp-readsize", 8192, NULL, MTU_TEXT, MTU_LONGTEXT, true )
72 add_integer( "sftp-port", 22, NULL, PORT_TEXT, PORT_LONGTEXT, true )
73 add_shortcut( "sftp" )
74 set_callbacks( Open, Close )
78 /*****************************************************************************
80 *****************************************************************************/
81 static block_t* Block( access_t * );
82 static int Seek( access_t *, uint64_t );
83 static int Control( access_t *, int, va_list );
89 LIBSSH2_SESSION* ssh_session;
90 LIBSSH2_SFTP* sftp_session;
91 LIBSSH2_SFTP_HANDLE* file;
98 * Connect to the sftp server and ask for a file
99 * @param p_this: the vlc_object
100 * @return VLC_SUCCESS if everything was fine
102 static int Open( vlc_object_t* p_this )
104 access_t* p_access = (access_t*)p_this;
106 char* psz_username = NULL;
107 char* psz_password = NULL;
112 if( !p_access->psz_location )
115 STANDARD_BLOCK_ACCESS_INIT;
118 const char* path = p_access->psz_location;
119 vlc_UrlParse( &url, path, 0 );
121 /* Check for some parameters */
122 if( EMPTY_STR( url.psz_host ) )
124 msg_Err( p_access, "You might give a non empty host" );
128 /* If the user name is empty, ask the user */
129 if( !EMPTY_STR( url.psz_username ) && url.psz_password )
131 psz_username = strdup( url.psz_username );
132 psz_password = strdup( url.psz_password );
136 dialog_Login( p_access, &psz_username, &psz_password,
137 _("SFTP authentication"),
138 _("Please enter a valid login and password for the sftp "
139 "connexion to %s"), url.psz_host );
140 if( EMPTY_STR(psz_username) || !psz_password )
144 if( url.i_port <= 0 )
145 i_port = var_CreateGetInteger( p_access, "sftp-port" );
150 /* Connect to the server using a regular socket */
151 p_sys->i_socket = net_Connect( p_access, url.psz_host, i_port, SOCK_STREAM, 0 );
153 /* Create the ssh connexion and wait until the server answer */
154 p_sys->ssh_session = libssh2_session_init();
155 while( ( i_ret = libssh2_session_startup( p_sys->ssh_session,
157 == LIBSSH2_ERROR_EAGAIN );
161 msg_Err( p_access, "Impossible to open the connection to %s:%i", url.psz_host, i_port );
165 /* Ask for the fingerprint ... */
167 libssh2_session_set_blocking( p_sys->ssh_session, 1 );
168 const char* fingerprint = libssh2_hostkey_hash( p_sys->ssh_session, LIBSSH2_HOSTKEY_HASH_MD5 );
169 fprintf(stderr, "Fingerprint: ");
170 for( int i = 0; i < 16; i++) {
171 fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
173 fprintf(stderr, "\n");
175 //TODO: ask for the available auth methods
177 /* send the login/password */
178 if( libssh2_userauth_password( p_sys->ssh_session, psz_username, psz_password ) )
180 msg_Err( p_access, "Authentication by password failed" );
184 /* Create the sftp session */
185 p_sys->sftp_session = libssh2_sftp_init( p_sys->ssh_session );
187 if( !p_sys->sftp_session )
189 msg_Err( p_access, "Unable to initialize the SFTP session" );
193 /* Open the given file */
194 p_sys->file = libssh2_sftp_open( p_sys->sftp_session, url.psz_path, LIBSSH2_FXF_READ, 0 );
197 msg_Err( p_access, "Unable to open the remote file %s", url.psz_path );
201 /* Get some informations */
202 LIBSSH2_SFTP_ATTRIBUTES attributes;
203 if( libssh2_sftp_stat( p_sys->sftp_session, url.psz_path, &attributes ) )
205 msg_Err( p_access, "Impossible to get informations about the remote file %s", url.psz_path );
208 p_access->info.i_size = attributes.filesize;
210 /* Create the two variables */
211 var_Create( p_access, "sftp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
212 p_sys->i_read_size = var_CreateGetInteger( p_access, "sftp-readsize" );
214 free( psz_password );
215 free( psz_username );
216 vlc_UrlClean( &url );
220 free( psz_password );
221 free( psz_username );
222 vlc_UrlClean( &url );
228 /* Close: quit the module */
229 static void Close( vlc_object_t* p_this )
231 access_t* p_access = (access_t*)p_this;
232 access_sys_t* p_sys = p_access->p_sys;
234 libssh2_sftp_close_handle( p_sys->file );
235 libssh2_sftp_shutdown( p_sys->sftp_session );
237 libssh2_session_free( p_sys->ssh_session );
242 static block_t* Block( access_t* p_access )
244 if( p_access->info.b_eof )
247 /* Allocate the buffer we need */
248 size_t i_len = __MIN( p_access->p_sys->i_read_size, p_access->info.i_size -
249 p_access->info.i_pos );
250 block_t* p_block = block_New( p_access, i_len );
254 /* Read the specified size */
255 ssize_t i_ret = libssh2_sftp_read( p_access->p_sys->file, (char*)p_block->p_buffer, i_len );
259 block_Release( p_block );
260 msg_Err( p_access, "read failed" );
263 else if( i_ret == 0 )
265 p_access->info.b_eof = true;
266 block_Release( p_block );
271 p_access->info.i_pos += i_ret;
277 static int Seek( access_t* p_access, uint64_t i_pos )
279 p_access->info.i_pos = i_pos;
280 p_access->info.b_eof = false;
282 libssh2_sftp_seek( p_access->p_sys->file, i_pos );
287 static int Control( access_t* p_access, int i_query, va_list args )
294 case ACCESS_CAN_SEEK:
295 pb_bool = (bool*)va_arg( args, bool* );
299 case ACCESS_CAN_FASTSEEK:
300 pb_bool = (bool*)va_arg( args, bool* );
304 case ACCESS_CAN_PAUSE:
305 case ACCESS_CAN_CONTROL_PACE:
306 pb_bool = (bool*)va_arg( args, bool* );
310 case ACCESS_GET_PTS_DELAY:
311 pi_64 = (int64_t*)va_arg( args, int64_t* );
312 *pi_64 = var_GetInteger( p_access, "sftp-caching" ) * INT64_C(1000);
315 case ACCESS_SET_PAUSE_STATE:
318 case ACCESS_GET_TITLE_INFO:
319 case ACCESS_SET_TITLE:
320 case ACCESS_SET_SEEKPOINT:
321 case ACCESS_SET_PRIVATE_ID_STATE:
322 case ACCESS_GET_META:
323 case ACCESS_GET_PRIVATE_ID_STATE:
324 case ACCESS_GET_CONTENT_TYPE:
328 msg_Warn( p_access, "unimplemented query %d in control", i_query );