1 /*****************************************************************************
2 * sftp.c: SFTP input module
3 *****************************************************************************
4 * Copyright (C) 2009 VLC authors and VideoLAN
7 * Authors: RĂ©mi Duraffort <ivoire@videolan.org>
8 * Petri Hintukainen <phintuka@gmail.com>
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 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
37 #include <vlc_access.h>
38 #include <vlc_dialog.h>
39 #include <vlc_input_item.h>
40 #include <vlc_network.h>
44 #include <libssh2_sftp.h>
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Open ( vlc_object_t* );
51 static void Close( vlc_object_t* );
53 #define PORT_TEXT N_("SFTP port")
54 #define PORT_LONGTEXT N_("SFTP port number to use on the server")
55 #define MTU_TEXT N_("Read size")
56 #define MTU_LONGTEXT N_("Size of the request for reading access")
57 #define USER_TEXT N_("Username")
58 #define USER_LONGTEXT N_("Username that will be used for the connection, " \
59 "if no username is set in the URL.")
60 #define PASS_TEXT N_("Password")
61 #define PASS_LONGTEXT N_("Password that will be used for the connection, " \
62 "if no username or password are set in URL.")
65 set_shortname( "SFTP" )
66 set_description( N_("SFTP input") )
67 set_capability( "access", 0 )
68 set_category( CAT_INPUT )
69 set_subcategory( SUBCAT_INPUT_ACCESS )
70 add_integer( "sftp-readsize", 8192, MTU_TEXT, MTU_LONGTEXT, true )
71 add_integer( "sftp-port", 22, PORT_TEXT, PORT_LONGTEXT, true )
72 add_string( "sftp-user", NULL, USER_TEXT, USER_LONGTEXT, false )
73 add_password( "sftp-pwd", NULL, PASS_TEXT, PASS_LONGTEXT, false )
74 add_shortcut( "sftp" )
75 set_callbacks( Open, Close )
79 /*****************************************************************************
81 *****************************************************************************/
82 static block_t* Block( access_t * );
83 static int Seek( access_t *, uint64_t );
84 static int Control( access_t *, int, va_list );
86 static int DirControl( access_t *, int, va_list );
87 static int DirRead( access_t *p_access, input_item_node_t *p_current_node );
92 LIBSSH2_SESSION* ssh_session;
93 LIBSSH2_SFTP* sftp_session;
94 LIBSSH2_SFTP_HANDLE* file;
99 char* psz_username_opt;
100 char* psz_password_opt;
106 * Connect to the sftp server and ask for a file
107 * @param p_this: the vlc_object
108 * @return VLC_SUCCESS if everything was fine
110 static int Open( vlc_object_t* p_this )
112 access_t* p_access = (access_t*)p_this;
114 char* psz_username = NULL;
115 char* psz_password = NULL;
122 if( !p_access->psz_location )
125 access_InitFields( p_access );
126 p_sys = p_access->p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
127 if( !p_sys ) return VLC_ENOMEM;
129 p_sys->i_socket = -1;
132 const char* path = p_access->psz_location;
133 vlc_UrlParse( &url, path, 0 );
135 /* Check for some parameters */
136 if( EMPTY_STR( url.psz_host ) )
138 msg_Err( p_access, "You might give a non empty host" );
142 /* get user/password from url or options */
143 if( !EMPTY_STR( url.psz_username ) )
144 psz_username = strdup( url.psz_username );
146 psz_username = var_InheritString( p_access, "sftp-user" );
148 if( url.psz_password )
149 psz_password = strdup( url.psz_password );
151 psz_password = var_InheritString( p_access, "sftp-pwd" );
153 /* If the user name or password is empty, ask the user */
154 if( EMPTY_STR( psz_username ) || !psz_password )
156 dialog_Login( p_access, &psz_username, &psz_password,
157 _("SFTP authentication"),
158 _("Please enter a valid login and password for the sftp "
159 "connexion to %s"), url.psz_host );
160 if( EMPTY_STR(psz_username) || !psz_password )
164 if( url.i_port <= 0 )
165 i_port = var_InheritInteger( p_access, "sftp-port" );
170 /* Connect to the server using a regular socket */
171 p_sys->i_socket = net_Connect( p_access, url.psz_host, i_port, SOCK_STREAM, 0 );
172 if( p_sys->i_socket < 0 )
174 msg_Err( p_access, "Impossible to open the connection to %s:%i", url.psz_host, i_port );
178 /* Create the ssh connexion and wait until the server answer */
179 if( ( p_sys->ssh_session = libssh2_session_init() ) == NULL )
182 while( ( i_ret = libssh2_session_startup( p_sys->ssh_session,
184 == LIBSSH2_ERROR_EAGAIN );
188 msg_Err( p_access, "Impossible to open the connection to %s:%i", url.psz_host, i_port );
192 /* Set the socket in non-blocking mode */
193 libssh2_session_set_blocking( p_sys->ssh_session, 1 );
195 /* List the know hosts */
196 LIBSSH2_KNOWNHOSTS *ssh_knownhosts = libssh2_knownhost_init( p_sys->ssh_session );
197 if( !ssh_knownhosts )
200 char *psz_home = config_GetUserDir( VLC_HOME_DIR );
201 char *psz_knownhosts_file;
202 if( asprintf( &psz_knownhosts_file, "%s/.ssh/known_hosts", psz_home ) != -1 )
204 libssh2_knownhost_readfile( ssh_knownhosts, psz_knownhosts_file,
205 LIBSSH2_KNOWNHOST_FILE_OPENSSH );
206 free( psz_knownhosts_file );
210 const char *fingerprint = libssh2_session_hostkey( p_sys->ssh_session, &i_len, &i_type );
211 struct libssh2_knownhost *host;
212 int check = libssh2_knownhost_check( ssh_knownhosts, url.psz_host,
214 LIBSSH2_KNOWNHOST_TYPE_PLAIN |
215 LIBSSH2_KNOWNHOST_KEYENC_RAW,
218 libssh2_knownhost_free( ssh_knownhosts );
220 /* Check that it does match or at least that the host is unknown */
223 case LIBSSH2_KNOWNHOST_CHECK_FAILURE:
224 case LIBSSH2_KNOWNHOST_CHECK_NOTFOUND:
225 msg_Dbg( p_access, "Unable to check the remote host" );
227 case LIBSSH2_KNOWNHOST_CHECK_MATCH:
228 msg_Dbg( p_access, "Succesfuly matched the host" );
230 case LIBSSH2_KNOWNHOST_CHECK_MISMATCH:
231 msg_Err( p_access, "The host does not match !! The remote key changed !!" );
235 //TODO: ask for the available auth methods
237 /* send the login/password */
238 if( libssh2_userauth_password( p_sys->ssh_session, psz_username, psz_password ) )
240 msg_Err( p_access, "Authentication by password failed" );
244 /* Create the sftp session */
245 p_sys->sftp_session = libssh2_sftp_init( p_sys->ssh_session );
247 if( !p_sys->sftp_session )
249 msg_Err( p_access, "Unable to initialize the SFTP session" );
253 /* Get some information */
254 LIBSSH2_SFTP_ATTRIBUTES attributes;
255 if( libssh2_sftp_stat( p_sys->sftp_session, url.psz_path, &attributes ) )
257 msg_Err( p_access, "Impossible to get information about the remote path %s", url.psz_path );
261 if( !LIBSSH2_SFTP_S_ISDIR( attributes.permissions ))
263 /* Open the given file */
264 p_sys->file = libssh2_sftp_open( p_sys->sftp_session, url.psz_path, LIBSSH2_FXF_READ, 0 );
265 p_sys->filesize = attributes.filesize;
267 ACCESS_SET_CALLBACKS( NULL, Block, Control, Seek );
271 /* Open the given directory */
272 p_sys->file = libssh2_sftp_opendir( p_sys->sftp_session, url.psz_path );
274 p_access->pf_readdir = DirRead;
275 p_access->pf_control = DirControl;
279 if( -1 == asprintf( &p_sys->psz_username_opt, "sftp-user=%s", psz_username ) )
280 p_sys->psz_username_opt = NULL;
281 if( -1 == asprintf( &p_sys->psz_password_opt, "sftp-pwd=%s", psz_password ) )
282 p_sys->psz_password_opt = NULL;
288 msg_Err( p_access, "Unable to open the remote path %s", url.psz_path );
292 p_sys->i_read_size = var_InheritInteger( p_access, "sftp-readsize" );
294 free( psz_password );
295 free( psz_username );
296 vlc_UrlClean( &url );
301 libssh2_sftp_close_handle( p_sys->file );
302 if( p_sys->ssh_session )
303 libssh2_session_free( p_sys->ssh_session );
304 free( psz_password );
305 free( psz_username );
306 vlc_UrlClean( &url );
307 net_Close( p_sys->i_socket );
313 /* Close: quit the module */
314 static void Close( vlc_object_t* p_this )
316 access_t* p_access = (access_t*)p_this;
317 access_sys_t* p_sys = p_access->p_sys;
319 libssh2_sftp_close_handle( p_sys->file );
320 libssh2_sftp_shutdown( p_sys->sftp_session );
322 libssh2_session_free( p_sys->ssh_session );
323 net_Close( p_sys->i_socket );
325 free( p_sys->psz_password_opt );
326 free( p_sys->psz_username_opt );
332 static block_t* Block( access_t* p_access )
334 access_sys_t *p_sys = p_access->p_sys;
336 if( p_access->info.b_eof )
339 /* Allocate the buffer we need */
340 size_t i_len = __MIN( p_sys->i_read_size,
341 p_sys->filesize - p_access->info.i_pos );
342 block_t* p_block = block_Alloc( i_len );
346 /* Read the specified size */
347 ssize_t i_ret = libssh2_sftp_read( p_access->p_sys->file, (char*)p_block->p_buffer, i_len );
351 block_Release( p_block );
352 msg_Err( p_access, "read failed" );
355 else if( i_ret == 0 )
357 p_access->info.b_eof = true;
358 block_Release( p_block );
363 p_block->i_buffer = i_ret;
364 p_access->info.i_pos += i_ret;
370 static int Seek( access_t* p_access, uint64_t i_pos )
372 p_access->info.i_pos = i_pos;
373 p_access->info.b_eof = false;
375 libssh2_sftp_seek( p_access->p_sys->file, i_pos );
380 static int Control( access_t* p_access, int i_query, va_list args )
387 case ACCESS_CAN_SEEK:
388 pb_bool = (bool*)va_arg( args, bool* );
392 case ACCESS_CAN_FASTSEEK:
393 pb_bool = (bool*)va_arg( args, bool* );
397 case ACCESS_CAN_PAUSE:
398 case ACCESS_CAN_CONTROL_PACE:
399 pb_bool = (bool*)va_arg( args, bool* );
403 case ACCESS_GET_SIZE:
404 *va_arg( args, uint64_t * ) = p_access->p_sys->filesize;
407 case ACCESS_GET_PTS_DELAY:
408 pi_64 = (int64_t*)va_arg( args, int64_t* );
409 *pi_64 = INT64_C(1000)
410 * var_InheritInteger( p_access, "network-caching" );
413 case ACCESS_SET_PAUSE_STATE:
424 /*****************************************************************************
426 *****************************************************************************/
428 static int DirRead (access_t *p_access, input_item_node_t *p_current_node)
430 access_sys_t *p_sys = p_access->p_sys;
431 LIBSSH2_SFTP_ATTRIBUTES attrs;
433 /* Allocate 1024 bytes for file name. Longer names are skipped.
434 * libssh2 does not support seeking in directory streams.
435 * Retrying with larger buffer is possible, but would require
436 * re-opening the directory stream.
438 size_t i_size = 1024;
439 char *psz_file = malloc( i_size );
444 while( 0 != ( err = libssh2_sftp_readdir( p_sys->file, psz_file, i_size, &attrs ) ) )
448 if( err == LIBSSH2_ERROR_BUFFER_TOO_SMALL )
450 /* seeking back is not possible ... */
451 msg_Dbg( p_access, "skipped too long file name" );
454 if( err == LIBSSH2_ERROR_EAGAIN )
458 msg_Err( p_access, "directory read failed" );
462 if( psz_file[0] == '.' )
467 /* Create an input item for the current entry */
469 char *psz_full_uri, *psz_uri;
471 psz_uri = encode_URI_component( psz_file );
472 if( psz_uri == NULL )
475 if( asprintf( &psz_full_uri, "sftp://%s/%s", p_access->psz_location, psz_uri ) == -1 )
482 int i_type = LIBSSH2_SFTP_S_ISDIR( attrs.permissions ) ? ITEM_TYPE_DIRECTORY : ITEM_TYPE_FILE;
483 input_item_t *p_new = input_item_NewWithType( psz_full_uri, psz_file,
484 0, NULL, 0, 0, i_type );
488 free( psz_full_uri );
492 /* Here we save on the node the credentials that allowed us to login.
493 * That way the user isn't prompted more than once for credentials */
494 if( p_sys->psz_password_opt )
495 input_item_AddOption( p_new, p_sys->psz_password_opt, VLC_INPUT_OPTION_TRUSTED );
496 if( p_sys->psz_username_opt )
497 input_item_AddOption( p_new, p_sys->psz_username_opt, VLC_INPUT_OPTION_TRUSTED );
499 input_item_CopyOptions( p_current_node->p_item, p_new );
500 input_item_node_AppendItem( p_current_node, p_new );
502 free( psz_full_uri );
503 input_item_Release( p_new );
511 static int DirControl( access_t *p_access, int i_query, va_list args )
513 VLC_UNUSED( p_access );
517 case ACCESS_CAN_SEEK:
518 case ACCESS_CAN_FASTSEEK:
519 *va_arg( args, bool* ) = false;
522 case ACCESS_CAN_PAUSE:
523 case ACCESS_CAN_CONTROL_PACE:
524 *va_arg( args, bool* ) = true;
527 case ACCESS_GET_PTS_DELAY:
528 *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;