X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Faccess%2Fsftp.c;h=5c29a9ceab445a5767eea9652adc2e0703a08218;hb=1dcc337d1f41bb760d72118849d53c3856d030ca;hp=ac90b4936459aabe04ddef792132503084735639;hpb=3e258835cb95562a5a8ebbbb9f6c11e7b073a41b;p=vlc diff --git a/modules/access/sftp.c b/modules/access/sftp.c index ac90b49364..5c29a9ceab 100644 --- a/modules/access/sftp.c +++ b/modules/access/sftp.c @@ -1,24 +1,25 @@ /***************************************************************************** * sftp.c: SFTP input module ***************************************************************************** - * Copyright (C) 2009 the VideoLAN team + * Copyright (C) 2009 VLC authors and VideoLAN * $Id$ * * Authors: Rémi Duraffort + * Petri Hintukainen * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ /***************************************************************************** @@ -35,6 +36,7 @@ #include #include +#include #include #include @@ -48,17 +50,16 @@ static int Open ( vlc_object_t* ); static void Close( vlc_object_t* ); -#define CACHING_TEXT N_("Caching value in ms") -#define CACHING_LONGTEXT N_( \ - "Caching value for SFTP streams. This value should be set in milliseconds." ) -#define USER_TEXT N_("SFTP user name") -#define USER_LONGTEXT N_("User name that will be used for the connection.") -#define PASS_TEXT N_("SFTP password") -#define PASS_LONGTEXT N_("Password that will be used for the connection.") #define PORT_TEXT N_("SFTP port") #define PORT_LONGTEXT N_("SFTP port number to use on the server") #define MTU_TEXT N_("Read size") #define MTU_LONGTEXT N_("Size of the request for reading access") +#define USER_TEXT N_("Username") +#define USER_LONGTEXT N_("Username that will be used for the connection, " \ + "if no username is set in the URL.") +#define PASS_TEXT N_("Password") +#define PASS_LONGTEXT N_("Password that will be used for the connection, " \ + "if no username or password are set in URL.") vlc_module_begin () set_shortname( "SFTP" ) @@ -66,10 +67,10 @@ vlc_module_begin () set_capability( "access", 0 ) set_category( CAT_INPUT ) set_subcategory( SUBCAT_INPUT_ACCESS ) - add_integer( "sftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL, - CACHING_TEXT, CACHING_LONGTEXT, true ); - add_integer( "sftp-readsize", 8192, NULL, MTU_TEXT, MTU_LONGTEXT, true ) - add_integer( "sftp-port", 22, NULL, PORT_TEXT, PORT_LONGTEXT, true ) + add_integer( "sftp-readsize", 8192, MTU_TEXT, MTU_LONGTEXT, true ) + add_integer( "sftp-port", 22, PORT_TEXT, PORT_LONGTEXT, true ) + add_string( "sftp-user", NULL, USER_TEXT, USER_LONGTEXT, false ) + add_password( "sftp-pwd", NULL, PASS_TEXT, PASS_LONGTEXT, false ) add_shortcut( "sftp" ) set_callbacks( Open, Close ) vlc_module_end () @@ -82,6 +83,8 @@ static block_t* Block( access_t * ); static int Seek( access_t *, uint64_t ); static int Control( access_t *, int, va_list ); +static int DirControl( access_t *, int, va_list ); +static int DirRead( access_t *p_access, input_item_node_t *p_current_node ); struct access_sys_t { @@ -89,7 +92,12 @@ struct access_sys_t LIBSSH2_SESSION* ssh_session; LIBSSH2_SFTP* sftp_session; LIBSSH2_SFTP_HANDLE* file; - int i_read_size; + uint64_t filesize; + size_t i_read_size; + + /* browser */ + char* psz_username_opt; + char* psz_password_opt; }; @@ -108,11 +116,17 @@ static int Open( vlc_object_t* p_this ) int i_port; int i_ret; vlc_url_t url; + size_t i_len; + int i_type; if( !p_access->psz_location ) return VLC_EGENERIC; - STANDARD_BLOCK_ACCESS_INIT; + access_InitFields( p_access ); + p_sys = p_access->p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) ); + if( !p_sys ) return VLC_ENOMEM; + + p_sys->i_socket = -1; /* Parse the URL */ const char* path = p_access->psz_location; @@ -125,13 +139,19 @@ static int Open( vlc_object_t* p_this ) goto error; } - /* If the user name is empty, ask the user */ - if( !EMPTY_STR( url.psz_username ) && url.psz_password ) - { + /* get user/password from url or options */ + if( !EMPTY_STR( url.psz_username ) ) psz_username = strdup( url.psz_username ); + else + psz_username = var_InheritString( p_access, "sftp-user" ); + + if( url.psz_password ) psz_password = strdup( url.psz_password ); - } else + psz_password = var_InheritString( p_access, "sftp-pwd" ); + + /* If the user name or password is empty, ask the user */ + if( EMPTY_STR( psz_username ) || !psz_password ) { dialog_Login( p_access, &psz_username, &psz_password, _("SFTP authentication"), @@ -142,16 +162,23 @@ static int Open( vlc_object_t* p_this ) } if( url.i_port <= 0 ) - i_port = var_CreateGetInteger( p_access, "sftp-port" ); + i_port = var_InheritInteger( p_access, "sftp-port" ); else i_port = url.i_port; /* Connect to the server using a regular socket */ p_sys->i_socket = net_Connect( p_access, url.psz_host, i_port, SOCK_STREAM, 0 ); + if( p_sys->i_socket < 0 ) + { + msg_Err( p_access, "Impossible to open the connection to %s:%i", url.psz_host, i_port ); + goto error; + } /* Create the ssh connexion and wait until the server answer */ - p_sys->ssh_session = libssh2_session_init(); + if( ( p_sys->ssh_session = libssh2_session_init() ) == NULL ) + goto error; + while( ( i_ret = libssh2_session_startup( p_sys->ssh_session, p_sys->i_socket ) ) == LIBSSH2_ERROR_EAGAIN ); @@ -162,15 +189,48 @@ static int Open( vlc_object_t* p_this ) goto error; } - /* Ask for the fingerprint ... */ - // TODO: check it + /* Set the socket in non-blocking mode */ libssh2_session_set_blocking( p_sys->ssh_session, 1 ); - const char* fingerprint = libssh2_hostkey_hash( p_sys->ssh_session, LIBSSH2_HOSTKEY_HASH_MD5 ); - fprintf(stderr, "Fingerprint: "); - for( int i = 0; i < 16; i++) { - fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + + /* List the know hosts */ + LIBSSH2_KNOWNHOSTS *ssh_knownhosts = libssh2_knownhost_init( p_sys->ssh_session ); + if( !ssh_knownhosts ) + goto error; + + char *psz_home = config_GetUserDir( VLC_HOME_DIR ); + char *psz_knownhosts_file; + if( asprintf( &psz_knownhosts_file, "%s/.ssh/known_hosts", psz_home ) != -1 ) + { + libssh2_knownhost_readfile( ssh_knownhosts, psz_knownhosts_file, + LIBSSH2_KNOWNHOST_FILE_OPENSSH ); + free( psz_knownhosts_file ); + } + free( psz_home ); + + const char *fingerprint = libssh2_session_hostkey( p_sys->ssh_session, &i_len, &i_type ); + struct libssh2_knownhost *host; + int check = libssh2_knownhost_check( ssh_knownhosts, url.psz_host, + fingerprint, i_len, + LIBSSH2_KNOWNHOST_TYPE_PLAIN | + LIBSSH2_KNOWNHOST_KEYENC_RAW, + &host ); + + libssh2_knownhost_free( ssh_knownhosts ); + + /* Check that it does match or at least that the host is unknown */ + switch(check) + { + case LIBSSH2_KNOWNHOST_CHECK_FAILURE: + case LIBSSH2_KNOWNHOST_CHECK_NOTFOUND: + msg_Dbg( p_access, "Unable to check the remote host" ); + break; + case LIBSSH2_KNOWNHOST_CHECK_MATCH: + msg_Dbg( p_access, "Succesfuly matched the host" ); + break; + case LIBSSH2_KNOWNHOST_CHECK_MISMATCH: + msg_Err( p_access, "The host does not match !! The remote key changed !!" ); + goto error; } - fprintf(stderr, "\n"); //TODO: ask for the available auth methods @@ -190,26 +250,46 @@ static int Open( vlc_object_t* p_this ) goto error; } - /* Open the given file */ - p_sys->file = libssh2_sftp_open( p_sys->sftp_session, url.psz_path, LIBSSH2_FXF_READ, 0 ); - if( !p_sys->file ) + /* Get some information */ + LIBSSH2_SFTP_ATTRIBUTES attributes; + if( libssh2_sftp_stat( p_sys->sftp_session, url.psz_path, &attributes ) ) { - msg_Err( p_access, "Unable to open the remote file %s", url.psz_path ); + msg_Err( p_access, "Impossible to get information about the remote path %s", url.psz_path ); goto error; } - /* Get some informations */ - LIBSSH2_SFTP_ATTRIBUTES attributes; - if( libssh2_sftp_stat( p_sys->sftp_session, url.psz_path, &attributes ) ) + if( !LIBSSH2_SFTP_S_ISDIR( attributes.permissions )) { - msg_Err( p_access, "Impossible to get informations about the remote file %s", url.psz_path ); + /* Open the given file */ + p_sys->file = libssh2_sftp_open( p_sys->sftp_session, url.psz_path, LIBSSH2_FXF_READ, 0 ); + p_sys->filesize = attributes.filesize; + + ACCESS_SET_CALLBACKS( NULL, Block, Control, Seek ); + } + else + { + /* Open the given directory */ + p_sys->file = libssh2_sftp_opendir( p_sys->sftp_session, url.psz_path ); + + p_access->pf_readdir = DirRead; + p_access->pf_control = DirControl; + + if( p_sys->file ) + { + if( -1 == asprintf( &p_sys->psz_username_opt, "sftp-user=%s", psz_username ) ) + p_sys->psz_username_opt = NULL; + if( -1 == asprintf( &p_sys->psz_password_opt, "sftp-pwd=%s", psz_password ) ) + p_sys->psz_password_opt = NULL; + } + } + + if( !p_sys->file ) + { + msg_Err( p_access, "Unable to open the remote path %s", url.psz_path ); goto error; } - p_access->info.i_size = attributes.filesize; - /* Create the two variables */ - var_Create( p_access, "sftp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT ); - p_sys->i_read_size = var_CreateGetInteger( p_access, "sftp-readsize" ); + p_sys->i_read_size = var_InheritInteger( p_access, "sftp-readsize" ); free( psz_password ); free( psz_username ); @@ -217,9 +297,14 @@ static int Open( vlc_object_t* p_this ) return VLC_SUCCESS; error: + if( p_sys->file ) + libssh2_sftp_close_handle( p_sys->file ); + if( p_sys->ssh_session ) + libssh2_session_free( p_sys->ssh_session ); free( psz_password ); free( psz_username ); vlc_UrlClean( &url ); + net_Close( p_sys->i_socket ); free( p_sys ); return VLC_EGENERIC; } @@ -235,19 +320,26 @@ static void Close( vlc_object_t* p_this ) libssh2_sftp_shutdown( p_sys->sftp_session ); libssh2_session_free( p_sys->ssh_session ); + net_Close( p_sys->i_socket ); + + free( p_sys->psz_password_opt ); + free( p_sys->psz_username_opt ); + free( p_sys ); } static block_t* Block( access_t* p_access ) { + access_sys_t *p_sys = p_access->p_sys; + if( p_access->info.b_eof ) return NULL; /* Allocate the buffer we need */ - size_t i_len = __MIN( p_access->p_sys->i_read_size, p_access->info.i_size - - p_access->info.i_pos ); - block_t* p_block = block_New( p_access, i_len ); + size_t i_len = __MIN( p_sys->i_read_size, + p_sys->filesize - p_access->info.i_pos ); + block_t* p_block = block_Alloc( i_len ); if( !p_block ) return NULL; @@ -268,6 +360,7 @@ static block_t* Block( access_t* p_access ) } else { + p_block->i_buffer = i_ret; p_access->info.i_pos += i_ret; return p_block; } @@ -307,28 +400,136 @@ static int Control( access_t* p_access, int i_query, va_list args ) *pb_bool = true; break; + case ACCESS_GET_SIZE: + *va_arg( args, uint64_t * ) = p_access->p_sys->filesize; + break; + case ACCESS_GET_PTS_DELAY: pi_64 = (int64_t*)va_arg( args, int64_t* ); - *pi_64 = (int64_t)var_GetInteger( p_access, "sftp-caching" ) * INT64_C(1000); + *pi_64 = INT64_C(1000) + * var_InheritInteger( p_access, "network-caching" ); break; case ACCESS_SET_PAUSE_STATE: break; - case ACCESS_GET_TITLE_INFO: - case ACCESS_SET_TITLE: - case ACCESS_SET_SEEKPOINT: - case ACCESS_SET_PRIVATE_ID_STATE: - case ACCESS_GET_META: - case ACCESS_GET_PRIVATE_ID_STATE: - case ACCESS_GET_CONTENT_TYPE: - return VLC_EGENERIC; - default: - msg_Warn( p_access, "unimplemented query %d in control", i_query ); return VLC_EGENERIC; } return VLC_SUCCESS; } + +/***************************************************************************** + * Directory access + *****************************************************************************/ + +static int DirRead (access_t *p_access, input_item_node_t *p_current_node) +{ + access_sys_t *p_sys = p_access->p_sys; + LIBSSH2_SFTP_ATTRIBUTES attrs; + int err; + /* Allocate 1024 bytes for file name. Longer names are skipped. + * libssh2 does not support seeking in directory streams. + * Retrying with larger buffer is possible, but would require + * re-opening the directory stream. + */ + size_t i_size = 1024; + char *psz_file = malloc( i_size ); + + if( !psz_file ) + return VLC_ENOMEM; + + while( 0 != ( err = libssh2_sftp_readdir( p_sys->file, psz_file, i_size, &attrs ) ) ) + { + if( err < 0 ) + { + if( err == LIBSSH2_ERROR_BUFFER_TOO_SMALL ) + { + /* seeking back is not possible ... */ + msg_Dbg( p_access, "skipped too long file name" ); + continue; + } + if( err == LIBSSH2_ERROR_EAGAIN ) + { + continue; + } + msg_Err( p_access, "directory read failed" ); + break; + } + + if( psz_file[0] == '.' ) + { + continue; + } + + /* Create an input item for the current entry */ + + char *psz_full_uri, *psz_uri; + + psz_uri = encode_URI_component( psz_file ); + if( psz_uri == NULL ) + continue; + + if( asprintf( &psz_full_uri, "sftp://%s/%s", p_access->psz_location, psz_uri ) == -1 ) + { + free( psz_uri ); + continue; + } + free( psz_uri ); + + int i_type = LIBSSH2_SFTP_S_ISDIR( attrs.permissions ) ? ITEM_TYPE_DIRECTORY : ITEM_TYPE_FILE; + input_item_t *p_new = input_item_NewWithType( psz_full_uri, psz_file, + 0, NULL, 0, 0, i_type ); + + if( p_new == NULL ) + { + free( psz_full_uri ); + continue; + } + + /* Here we save on the node the credentials that allowed us to login. + * That way the user isn't prompted more than once for credentials */ + if( p_sys->psz_password_opt ) + input_item_AddOption( p_new, p_sys->psz_password_opt, VLC_INPUT_OPTION_TRUSTED ); + if( p_sys->psz_username_opt ) + input_item_AddOption( p_new, p_sys->psz_username_opt, VLC_INPUT_OPTION_TRUSTED ); + + input_item_CopyOptions( p_current_node->p_item, p_new ); + input_item_node_AppendItem( p_current_node, p_new ); + + free( psz_full_uri ); + input_item_Release( p_new ); + } + + free( psz_file ); + return VLC_SUCCESS; +} + + +static int DirControl( access_t *p_access, int i_query, va_list args ) +{ + VLC_UNUSED( p_access ); + + switch( i_query ) + { + case ACCESS_CAN_SEEK: + case ACCESS_CAN_FASTSEEK: + *va_arg( args, bool* ) = false; + break; + + case ACCESS_CAN_PAUSE: + case ACCESS_CAN_CONTROL_PACE: + *va_arg( args, bool* ) = true; + break; + + case ACCESS_GET_PTS_DELAY: + *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000; + break; + + default: + return VLC_EGENERIC; + } + return VLC_SUCCESS; +}