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 input_item_t* DirRead( access_t *p_access );
91 LIBSSH2_SESSION* ssh_session;
92 LIBSSH2_SFTP* sftp_session;
93 LIBSSH2_SFTP_HANDLE* file;
98 char* psz_username_opt;
99 char* psz_password_opt;
105 * Connect to the sftp server and ask for a file
106 * @param p_this: the vlc_object
107 * @return VLC_SUCCESS if everything was fine
109 static int Open( vlc_object_t* p_this )
111 access_t* p_access = (access_t*)p_this;
113 const char* psz_path;
114 char* psz_username = NULL;
115 char* psz_password = NULL;
116 char* psz_remote_home = NULL;
123 if( !p_access->psz_location )
126 access_InitFields( p_access );
127 p_sys = p_access->p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
128 if( !p_sys ) return VLC_ENOMEM;
130 p_sys->i_socket = -1;
133 vlc_UrlParse( &url, p_access->psz_location );
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 /* No path, default to user Home */
256 const size_t i_size = 1024;
259 psz_remote_home = malloc( i_size );
260 if( !psz_remote_home )
263 i_ret = libssh2_sftp_symlink_ex( p_sys->sftp_session, ".", 1,
264 psz_remote_home, i_size - 1,
265 LIBSSH2_SFTP_REALPATH );
268 msg_Err( p_access, "Impossible to get the Home directory" );
271 psz_remote_home[i_ret] = '\0';
272 psz_path = psz_remote_home;
275 psz_path = url.psz_path;
277 /* Get some information */
278 LIBSSH2_SFTP_ATTRIBUTES attributes;
279 if( libssh2_sftp_stat( p_sys->sftp_session, psz_path, &attributes ) )
281 msg_Err( p_access, "Impossible to get information about the remote path %s", psz_path );
285 if( !LIBSSH2_SFTP_S_ISDIR( attributes.permissions ))
287 /* Open the given file */
288 p_sys->file = libssh2_sftp_open( p_sys->sftp_session, psz_path, LIBSSH2_FXF_READ, 0 );
289 p_sys->filesize = attributes.filesize;
291 ACCESS_SET_CALLBACKS( NULL, Block, Control, Seek );
295 /* Open the given directory */
296 p_sys->file = libssh2_sftp_opendir( p_sys->sftp_session, psz_path );
298 p_access->pf_readdir = DirRead;
299 p_access->pf_control = access_vaDirectoryControlHelper;
300 p_access->info.b_dir_can_loop = true;
304 if( -1 == asprintf( &p_sys->psz_username_opt, "sftp-user=%s", psz_username ) )
305 p_sys->psz_username_opt = NULL;
306 if( -1 == asprintf( &p_sys->psz_password_opt, "sftp-pwd=%s", psz_password ) )
307 p_sys->psz_password_opt = NULL;
313 msg_Err( p_access, "Unable to open the remote path %s", psz_path );
317 p_sys->i_read_size = var_InheritInteger( p_access, "sftp-readsize" );
319 free( psz_password );
320 free( psz_username );
321 free( psz_remote_home );
322 vlc_UrlClean( &url );
327 libssh2_sftp_close_handle( p_sys->file );
328 if( p_sys->ssh_session )
329 libssh2_session_free( p_sys->ssh_session );
330 free( psz_password );
331 free( psz_username );
332 free( psz_remote_home );
333 vlc_UrlClean( &url );
334 if( p_sys->i_socket >= 0 )
335 net_Close( p_sys->i_socket );
341 /* Close: quit the module */
342 static void Close( vlc_object_t* p_this )
344 access_t* p_access = (access_t*)p_this;
345 access_sys_t* p_sys = p_access->p_sys;
347 libssh2_sftp_close_handle( p_sys->file );
348 libssh2_sftp_shutdown( p_sys->sftp_session );
350 libssh2_session_free( p_sys->ssh_session );
351 net_Close( p_sys->i_socket );
353 free( p_sys->psz_password_opt );
354 free( p_sys->psz_username_opt );
360 static block_t* Block( access_t* p_access )
362 access_sys_t *p_sys = p_access->p_sys;
364 if( p_access->info.b_eof )
367 /* Allocate the buffer we need */
368 size_t i_len = __MIN( p_sys->i_read_size,
369 p_sys->filesize - p_access->info.i_pos );
370 block_t* p_block = block_Alloc( i_len );
374 /* Read the specified size */
375 ssize_t i_ret = libssh2_sftp_read( p_access->p_sys->file, (char*)p_block->p_buffer, i_len );
379 block_Release( p_block );
380 msg_Err( p_access, "read failed" );
383 else if( i_ret == 0 )
385 p_access->info.b_eof = true;
386 block_Release( p_block );
391 p_block->i_buffer = i_ret;
392 p_access->info.i_pos += i_ret;
398 static int Seek( access_t* p_access, uint64_t i_pos )
400 p_access->info.i_pos = i_pos;
401 p_access->info.b_eof = false;
403 libssh2_sftp_seek( p_access->p_sys->file, i_pos );
408 static int Control( access_t* p_access, int i_query, va_list args )
415 case ACCESS_CAN_SEEK:
416 pb_bool = (bool*)va_arg( args, bool* );
420 case ACCESS_CAN_FASTSEEK:
421 pb_bool = (bool*)va_arg( args, bool* );
425 case ACCESS_CAN_PAUSE:
426 case ACCESS_CAN_CONTROL_PACE:
427 pb_bool = (bool*)va_arg( args, bool* );
431 case ACCESS_GET_SIZE:
432 *va_arg( args, uint64_t * ) = p_access->p_sys->filesize;
435 case ACCESS_GET_PTS_DELAY:
436 pi_64 = (int64_t*)va_arg( args, int64_t* );
437 *pi_64 = INT64_C(1000)
438 * var_InheritInteger( p_access, "network-caching" );
441 case ACCESS_SET_PAUSE_STATE:
452 /*****************************************************************************
454 *****************************************************************************/
456 static input_item_t* DirRead( access_t *p_access )
458 access_sys_t *p_sys = p_access->p_sys;
459 LIBSSH2_SFTP_ATTRIBUTES attrs;
460 input_item_t *p_item = NULL;
462 /* Allocate 1024 bytes for file name. Longer names are skipped.
463 * libssh2 does not support seeking in directory streams.
464 * Retrying with larger buffer is possible, but would require
465 * re-opening the directory stream.
467 size_t i_size = 1024;
468 char *psz_file = malloc( i_size );
473 while( !p_item && 0 != ( err = libssh2_sftp_readdir( p_sys->file, psz_file, i_size, &attrs ) ) )
477 if( err == LIBSSH2_ERROR_BUFFER_TOO_SMALL )
479 /* seeking back is not possible ... */
480 msg_Dbg( p_access, "skipped too long file name" );
483 if( err == LIBSSH2_ERROR_EAGAIN )
487 msg_Err( p_access, "directory read failed" );
491 if( psz_file[0] == '.' )
496 /* Create an input item for the current entry */
498 char *psz_full_uri, *psz_uri;
500 psz_uri = encode_URI_component( psz_file );
501 if( psz_uri == NULL )
504 if( asprintf( &psz_full_uri, "sftp://%s/%s", p_access->psz_location, psz_uri ) == -1 )
511 int i_type = LIBSSH2_SFTP_S_ISDIR( attrs.permissions ) ? ITEM_TYPE_DIRECTORY : ITEM_TYPE_FILE;
512 p_item = input_item_NewWithTypeExt( psz_full_uri, psz_file,
513 0, NULL, 0, 0, i_type, 1 );
517 free( psz_full_uri );
521 /* Here we save on the node the credentials that allowed us to login.
522 * That way the user isn't prompted more than once for credentials */
523 if( p_sys->psz_password_opt )
524 input_item_AddOption( p_item, p_sys->psz_password_opt, VLC_INPUT_OPTION_TRUSTED );
525 if( p_sys->psz_username_opt )
526 input_item_AddOption( p_item, p_sys->psz_username_opt, VLC_INPUT_OPTION_TRUSTED );
528 free( psz_full_uri );