]> git.sesse.net Git - vlc/blob - modules/access/sftp.c
New sftp access module.
[vlc] / modules / access / sftp.c
1 /*****************************************************************************
2  * sftp.c: SFTP input module
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Rémi Duraffort <ivoire@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33
34 #include <assert.h>
35
36 #include <vlc_access.h>
37 #include <vlc_dialog.h>
38 #include <vlc_network.h>
39 #include <vlc_url.h>
40
41 #include <libssh2.h>
42 #include <libssh2_sftp.h>
43
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 static int  Open ( vlc_object_t* );
49 static void Close( vlc_object_t* );
50
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")
62
63 vlc_module_begin ()
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 )
75 vlc_module_end ()
76
77
78 /*****************************************************************************
79  * Local prototypes
80  *****************************************************************************/
81 static block_t* Block( access_t * );
82 static int      Seek( access_t *, int64_t );
83 static int      Control( access_t *, int, va_list );
84
85
86 struct access_sys_t
87 {
88     int i_socket;
89     LIBSSH2_SESSION* ssh_session;
90     LIBSSH2_SFTP* sftp_session;
91     LIBSSH2_SFTP_HANDLE* file;
92     int i_read_size;
93 };
94
95
96
97 /**
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
101  */
102 static int Open( vlc_object_t* p_this )
103 {
104     access_t*   p_access = (access_t*)p_this;
105     access_sys_t* p_sys;
106     char* psz_username = NULL;
107     char* psz_password = NULL;
108     int i_port;
109     int i_ret;
110     vlc_url_t url;
111
112     if( !p_access->psz_path )
113         return VLC_EGENERIC;
114
115     STANDARD_BLOCK_ACCESS_INIT;
116
117     /* Parse the URL */
118     char* path = p_access->psz_path;
119     vlc_UrlParse( &url, path, 0 );
120
121     /* Check for some parameters */
122     if( EMPTY_STR( url.psz_host ) )
123     {
124         msg_Err( p_access, "You might give a non empty host" );
125         goto error;
126     }
127
128     /* If the user name is empty, ask the user */
129     if( !EMPTY_STR( url.psz_username ) && url.psz_password )
130     {
131         psz_username = strdup( url.psz_username );
132         psz_password = strdup( url.psz_password );
133     }
134     else
135     {
136         dialog_Login( p_access, &psz_username, &psz_password,
137                       _("SFTP authentification"),
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 )
141             goto error;
142     }
143
144     if( url.i_port <= 0 )
145         i_port = var_CreateGetInteger( p_access, "sftp-port" );
146     else
147         i_port = url.i_port;
148
149
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 );
152
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,
156                                               p_sys->i_socket ) )
157            == LIBSSH2_ERROR_EAGAIN );
158
159     if( i_ret != 0 )
160     {
161         msg_Err( p_access, "Impossible to open the connection to %s:%i", url.psz_host, i_port );
162         goto error;
163     }
164
165     /* Ask for the fingerprint ... */
166     // TODO: check it
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]);
172     }
173     fprintf(stderr, "\n");
174
175     //TODO: ask for the available auth methods
176
177     /* send the login/password */
178     if( libssh2_userauth_password( p_sys->ssh_session, psz_username, psz_password ) )
179     {
180         msg_Err( p_access, "Authentication by password failed" );
181         goto error;
182     }
183
184     /* Create the sftp session */
185     p_sys->sftp_session = libssh2_sftp_init( p_sys->ssh_session );
186
187     if( !p_sys->sftp_session )
188     {
189         msg_Err( p_access, "Unable to initialize the SFTP session" );
190         goto error;
191     }
192
193     /* Open the given file */
194     p_sys->file = libssh2_sftp_open( p_sys->sftp_session, url.psz_path, LIBSSH2_FXF_READ, 0 );
195     if( !p_sys->file )
196     {
197         msg_Err( p_access, "Unable to open the remote file %s", url.psz_path );
198         goto error;
199     }
200
201     /* Get some informations */
202     LIBSSH2_SFTP_ATTRIBUTES attributes;
203     if( libssh2_sftp_stat( p_sys->sftp_session, url.psz_path, &attributes ) )
204     {
205         msg_Err( p_access, "Impossible to get informations about the remote file %s", url.psz_path );
206         goto error;
207     }
208     p_access->info.i_size = attributes.filesize;
209
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" );
213
214     free( psz_password );
215     free( psz_username );
216     vlc_UrlClean( &url );
217     return VLC_SUCCESS;
218
219
220 error:
221     free( psz_password );
222     free( psz_username );
223     if( p_sys )
224     {
225         vlc_UrlClean( &url );
226         free( p_sys );
227     }
228     return VLC_EGENERIC;
229 }
230
231
232 /* Close: quit the module */
233 static void Close( vlc_object_t* p_this )
234 {
235     access_t*   p_access = (access_t*)p_this;
236     access_sys_t* p_sys = p_access->p_sys;
237
238     libssh2_sftp_close_handle( p_sys->file );
239     libssh2_sftp_shutdown( p_sys->sftp_session );
240
241     libssh2_session_free( p_sys->ssh_session );
242     free( p_sys );
243 }
244
245
246 static block_t* Block( access_t* p_access )
247 {
248     if( p_access->info.b_eof )
249         return NULL;
250
251     /* Allocate the buffer we need */
252     size_t i_len = __MIN( p_access->p_sys->i_read_size, p_access->info.i_size -
253                                               p_access->info.i_pos );
254     block_t* p_block = block_New( p_access, i_len );
255     if( !p_block )
256         return NULL;
257
258     /* Read the specified size */
259     ssize_t i_ret = libssh2_sftp_read( p_access->p_sys->file, (char*)p_block->p_buffer, i_len );
260
261     if( i_ret < 0 )
262     {
263         block_Release( p_block );
264         msg_Err( p_access, "read failed" );
265         return NULL;
266     }
267     else if( i_ret == 0 )
268     {
269         p_access->info.b_eof = true;
270         block_Release( p_block );
271         return NULL;
272     }
273     else
274     {
275         p_access->info.i_pos += i_ret;
276         return p_block;
277     }
278 }
279
280
281 static int Seek( access_t* p_access, int64_t i_pos )
282 {
283     p_access->info.i_pos = i_pos;
284     p_access->info.b_eof = false;
285
286     libssh2_sftp_seek( p_access->p_sys->file, i_pos );
287     return VLC_SUCCESS;
288 }
289
290
291 static int Control( access_t* p_access, int i_query, va_list args )
292 {
293     bool*       pb_bool;
294     int64_t*    pi_64;
295
296     switch( i_query )
297     {
298     case ACCESS_CAN_SEEK:
299         pb_bool = (bool*)va_arg( args, bool* );
300         *pb_bool = true;
301         break;
302
303     case ACCESS_CAN_FASTSEEK:
304         pb_bool = (bool*)va_arg( args, bool* );
305         *pb_bool = false;
306         break;
307
308     case ACCESS_CAN_PAUSE:
309     case ACCESS_CAN_CONTROL_PACE:
310         pb_bool = (bool*)va_arg( args, bool* );
311         *pb_bool = true;
312         break;
313
314     case ACCESS_GET_PTS_DELAY:
315         pi_64 = (int64_t*)va_arg( args, int64_t* );
316         *pi_64 = (int64_t)var_GetInteger( p_access, "sftp-caching" ) * INT64_C(1000);
317         break;
318
319     case ACCESS_SET_PAUSE_STATE:
320         break;
321
322     case ACCESS_GET_TITLE_INFO:
323     case ACCESS_SET_TITLE:
324     case ACCESS_SET_SEEKPOINT:
325     case ACCESS_SET_PRIVATE_ID_STATE:
326     case ACCESS_GET_META:
327     case ACCESS_GET_PRIVATE_ID_STATE:
328     case ACCESS_GET_CONTENT_TYPE:
329         return VLC_EGENERIC;
330
331     default:
332         msg_Warn( p_access, "unimplemented query %d in control", i_query );
333         return VLC_EGENERIC;
334     }
335
336     return VLC_SUCCESS;
337 }
338