sftp.c 9.68 KB
Newer Older
Rémi Duraffort's avatar
Rémi Duraffort committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/*****************************************************************************
 * sftp.c: SFTP input module
 *****************************************************************************
 * Copyright (C) 2009 the VideoLAN team
 * $Id$
 *
 * Authors: Rémi Duraffort <ivoire@videolan.org>
 *
 * 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
 * (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.
 *
 * 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.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>

#include <assert.h>

#include <vlc_access.h>
#include <vlc_dialog.h>
#include <vlc_network.h>
#include <vlc_url.h>

#include <libssh2.h>
#include <libssh2_sftp.h>


/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
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")

vlc_module_begin ()
    set_shortname( "SFTP" )
    set_description( N_("SFTP input") )
    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_shortcut( "sftp" )
    set_callbacks( Open, Close )
vlc_module_end ()


/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static block_t* Block( access_t * );
82
static int      Seek( access_t *, uint64_t );
Rémi Duraffort's avatar
Rémi Duraffort committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
static int      Control( access_t *, int, va_list );


struct access_sys_t
{
    int i_socket;
    LIBSSH2_SESSION* ssh_session;
    LIBSSH2_SFTP* sftp_session;
    LIBSSH2_SFTP_HANDLE* file;
    int i_read_size;
};



/**
 * Connect to the sftp server and ask for a file
 * @param p_this: the vlc_object
 * @return VLC_SUCCESS if everything was fine
 */
static int Open( vlc_object_t* p_this )
{
    access_t*   p_access = (access_t*)p_this;
    access_sys_t* p_sys;
    char* psz_username = NULL;
    char* psz_password = NULL;
    int i_port;
    int i_ret;
    vlc_url_t url;

112
    if( !p_access->psz_location )
Rémi Duraffort's avatar
Rémi Duraffort committed
113 114 115 116 117
        return VLC_EGENERIC;

    STANDARD_BLOCK_ACCESS_INIT;

    /* Parse the URL */
118
    const char* path = p_access->psz_location;
Rémi Duraffort's avatar
Rémi Duraffort committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    vlc_UrlParse( &url, path, 0 );

    /* Check for some parameters */
    if( EMPTY_STR( url.psz_host ) )
    {
        msg_Err( p_access, "You might give a non empty host" );
        goto error;
    }

    /* If the user name is empty, ask the user */
    if( !EMPTY_STR( url.psz_username ) && url.psz_password )
    {
        psz_username = strdup( url.psz_username );
        psz_password = strdup( url.psz_password );
    }
    else
    {
        dialog_Login( p_access, &psz_username, &psz_password,
                      _("SFTP authentification"),
                      _("Please enter a valid login and password for the sftp "
                        "connexion to %s"), url.psz_host );
        if( EMPTY_STR(psz_username) || !psz_password )
            goto error;
    }

    if( url.i_port <= 0 )
        i_port = var_CreateGetInteger( 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 );

    /* Create the ssh connexion and wait until the server answer */
    p_sys->ssh_session = libssh2_session_init();
    while( ( i_ret = libssh2_session_startup( p_sys->ssh_session,
                                              p_sys->i_socket ) )
           == LIBSSH2_ERROR_EAGAIN );

    if( i_ret != 0 )
    {
        msg_Err( p_access, "Impossible to open the connection to %s:%i", url.psz_host, i_port );
        goto error;
    }

    /* Ask for the fingerprint ... */
    // TODO: check it
    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]);
    }
    fprintf(stderr, "\n");

    //TODO: ask for the available auth methods

    /* send the login/password */
    if( libssh2_userauth_password( p_sys->ssh_session, psz_username, psz_password ) )
    {
        msg_Err( p_access, "Authentication by password failed" );
        goto error;
    }

    /* Create the sftp session */
    p_sys->sftp_session = libssh2_sftp_init( p_sys->ssh_session );

    if( !p_sys->sftp_session )
    {
        msg_Err( p_access, "Unable to initialize the SFTP session" );
        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 )
    {
        msg_Err( p_access, "Unable to open the remote file %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 ) )
    {
        msg_Err( p_access, "Impossible to get informations about the remote file %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" );

    free( psz_password );
    free( psz_username );
    vlc_UrlClean( &url );
    return VLC_SUCCESS;

error:
    free( psz_password );
    free( psz_username );
222 223
    vlc_UrlClean( &url );
    free( p_sys );
Rémi Duraffort's avatar
Rémi Duraffort committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    return VLC_EGENERIC;
}


/* Close: quit the module */
static void Close( vlc_object_t* p_this )
{
    access_t*   p_access = (access_t*)p_this;
    access_sys_t* p_sys = p_access->p_sys;

    libssh2_sftp_close_handle( p_sys->file );
    libssh2_sftp_shutdown( p_sys->sftp_session );

    libssh2_session_free( p_sys->ssh_session );
    free( p_sys );
}


static block_t* Block( access_t* p_access )
{
    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 );
    if( !p_block )
        return NULL;

    /* Read the specified size */
    ssize_t i_ret = libssh2_sftp_read( p_access->p_sys->file, (char*)p_block->p_buffer, i_len );

    if( i_ret < 0 )
    {
        block_Release( p_block );
        msg_Err( p_access, "read failed" );
        return NULL;
    }
    else if( i_ret == 0 )
    {
        p_access->info.b_eof = true;
        block_Release( p_block );
        return NULL;
    }
    else
    {
        p_access->info.i_pos += i_ret;
        return p_block;
    }
}


277
static int Seek( access_t* p_access, uint64_t i_pos )
Rémi Duraffort's avatar
Rémi Duraffort committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
{
    p_access->info.i_pos = i_pos;
    p_access->info.b_eof = false;

    libssh2_sftp_seek( p_access->p_sys->file, i_pos );
    return VLC_SUCCESS;
}


static int Control( access_t* p_access, int i_query, va_list args )
{
    bool*       pb_bool;
    int64_t*    pi_64;

    switch( i_query )
    {
    case ACCESS_CAN_SEEK:
        pb_bool = (bool*)va_arg( args, bool* );
        *pb_bool = true;
        break;

    case ACCESS_CAN_FASTSEEK:
        pb_bool = (bool*)va_arg( args, bool* );
        *pb_bool = false;
        break;

    case ACCESS_CAN_PAUSE:
    case ACCESS_CAN_CONTROL_PACE:
        pb_bool = (bool*)va_arg( args, bool* );
        *pb_bool = true;
        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);
        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;
}