]> git.sesse.net Git - vlc/blob - modules/access/sftp.c
raop: handle VLC_ADD_LIBS
[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 USER_TEXT N_("SFTP user name")
52 #define USER_LONGTEXT N_("User name that will be used for the connection.")
53 #define PASS_TEXT N_("SFTP password")
54 #define PASS_LONGTEXT N_("Password that will be used for the connection.")
55 #define PORT_TEXT N_("SFTP port")
56 #define PORT_LONGTEXT N_("SFTP port number to use on the server")
57 #define MTU_TEXT N_("Read size")
58 #define MTU_LONGTEXT N_("Size of the request for reading access")
59
60 vlc_module_begin ()
61     set_shortname( "SFTP" )
62     set_description( N_("SFTP input") )
63     set_capability( "access", 0 )
64     set_category( CAT_INPUT )
65     set_subcategory( SUBCAT_INPUT_ACCESS )
66     add_integer( "sftp-readsize", 8192, MTU_TEXT, MTU_LONGTEXT, true )
67     add_integer( "sftp-port", 22, PORT_TEXT, PORT_LONGTEXT, true )
68     add_shortcut( "sftp" )
69     set_callbacks( Open, Close )
70 vlc_module_end ()
71
72
73 /*****************************************************************************
74  * Local prototypes
75  *****************************************************************************/
76 static block_t* Block( access_t * );
77 static int      Seek( access_t *, uint64_t );
78 static int      Control( access_t *, int, va_list );
79
80
81 struct access_sys_t
82 {
83     int i_socket;
84     LIBSSH2_SESSION* ssh_session;
85     LIBSSH2_SFTP* sftp_session;
86     LIBSSH2_SFTP_HANDLE* file;
87     int i_read_size;
88 };
89
90
91
92 /**
93  * Connect to the sftp server and ask for a file
94  * @param p_this: the vlc_object
95  * @return VLC_SUCCESS if everything was fine
96  */
97 static int Open( vlc_object_t* p_this )
98 {
99     access_t*   p_access = (access_t*)p_this;
100     access_sys_t* p_sys;
101     char* psz_username = NULL;
102     char* psz_password = NULL;
103     int i_port;
104     int i_ret;
105     vlc_url_t url;
106     size_t i_len;
107     int i_type;
108
109     if( !p_access->psz_location )
110         return VLC_EGENERIC;
111
112     STANDARD_BLOCK_ACCESS_INIT;
113
114     /* Parse the URL */
115     const char* path = p_access->psz_location;
116     vlc_UrlParse( &url, path, 0 );
117
118     /* Check for some parameters */
119     if( EMPTY_STR( url.psz_host ) )
120     {
121         msg_Err( p_access, "You might give a non empty host" );
122         goto error;
123     }
124
125     /* If the user name is empty, ask the user */
126     if( !EMPTY_STR( url.psz_username ) && url.psz_password )
127     {
128         psz_username = strdup( url.psz_username );
129         psz_password = strdup( url.psz_password );
130     }
131     else
132     {
133         dialog_Login( p_access, &psz_username, &psz_password,
134                       _("SFTP authentication"),
135                       _("Please enter a valid login and password for the sftp "
136                         "connexion to %s"), url.psz_host );
137         if( EMPTY_STR(psz_username) || !psz_password )
138             goto error;
139     }
140
141     if( url.i_port <= 0 )
142         i_port = var_InheritInteger( p_access, "sftp-port" );
143     else
144         i_port = url.i_port;
145
146
147     /* Connect to the server using a regular socket */
148     p_sys->i_socket = net_Connect( p_access, url.psz_host, i_port, SOCK_STREAM, 0 );
149
150     /* Create the ssh connexion and wait until the server answer */
151     if( ( p_sys->ssh_session = libssh2_session_init() ) == NULL )
152         goto error;
153
154     while( ( i_ret = libssh2_session_startup( p_sys->ssh_session,
155                                               p_sys->i_socket ) )
156            == LIBSSH2_ERROR_EAGAIN );
157
158     if( i_ret != 0 )
159     {
160         msg_Err( p_access, "Impossible to open the connection to %s:%i", url.psz_host, i_port );
161         goto error;
162     }
163
164     /* Set the socket in non-blocking mode */
165     libssh2_session_set_blocking( p_sys->ssh_session, 1 );
166
167     /* List the know hosts */
168     LIBSSH2_KNOWNHOSTS *ssh_knownhosts = libssh2_knownhost_init( p_sys->ssh_session );
169     if( !ssh_knownhosts )
170         goto error;
171
172     char *psz_home = config_GetUserDir( VLC_HOME_DIR );
173     char *psz_knownhosts_file;
174     asprintf( &psz_knownhosts_file, "%s/.ssh/known_hosts", psz_home );
175     libssh2_knownhost_readfile( ssh_knownhosts, psz_knownhosts_file,
176                                 LIBSSH2_KNOWNHOST_FILE_OPENSSH );
177     free( psz_knownhosts_file );
178     free( psz_home );
179
180     const char *fingerprint = libssh2_session_hostkey( p_sys->ssh_session, &i_len, &i_type );
181     struct libssh2_knownhost *host;
182     int check = libssh2_knownhost_check( ssh_knownhosts, url.psz_host,
183                                          fingerprint, i_len,
184                                          LIBSSH2_KNOWNHOST_TYPE_PLAIN |
185                                          LIBSSH2_KNOWNHOST_KEYENC_RAW,
186                                          &host );
187
188     libssh2_knownhost_free( ssh_knownhosts );
189
190     /* Check that it does match or at least that the host is unkown */
191     switch(check)
192     {
193     case LIBSSH2_KNOWNHOST_CHECK_FAILURE:
194     case LIBSSH2_KNOWNHOST_CHECK_NOTFOUND:
195         msg_Dbg( p_access, "Unable to check the remote host" );
196         break;
197     case LIBSSH2_KNOWNHOST_CHECK_MATCH:
198         msg_Dbg( p_access, "Succesfuly matched the host" );
199         break;
200     case LIBSSH2_KNOWNHOST_CHECK_MISMATCH:
201         msg_Err( p_access, "The host does not match !! The remote key changed !!" );
202         goto error;
203     }
204
205     //TODO: ask for the available auth methods
206
207     /* send the login/password */
208     if( libssh2_userauth_password( p_sys->ssh_session, psz_username, psz_password ) )
209     {
210         msg_Err( p_access, "Authentication by password failed" );
211         goto error;
212     }
213
214     /* Create the sftp session */
215     p_sys->sftp_session = libssh2_sftp_init( p_sys->ssh_session );
216
217     if( !p_sys->sftp_session )
218     {
219         msg_Err( p_access, "Unable to initialize the SFTP session" );
220         goto error;
221     }
222
223     /* Open the given file */
224     p_sys->file = libssh2_sftp_open( p_sys->sftp_session, url.psz_path, LIBSSH2_FXF_READ, 0 );
225     if( !p_sys->file )
226     {
227         msg_Err( p_access, "Unable to open the remote file %s", url.psz_path );
228         goto error;
229     }
230
231     /* Get some information */
232     LIBSSH2_SFTP_ATTRIBUTES attributes;
233     if( libssh2_sftp_stat( p_sys->sftp_session, url.psz_path, &attributes ) )
234     {
235         msg_Err( p_access, "Impossible to get information about the remote file %s", url.psz_path );
236         goto error;
237     }
238     p_access->info.i_size = attributes.filesize;
239
240     p_sys->i_read_size = var_InheritInteger( p_access, "sftp-readsize" );
241
242     free( psz_password );
243     free( psz_username );
244     vlc_UrlClean( &url );
245     return VLC_SUCCESS;
246
247 error:
248     if( p_sys->ssh_session )
249         libssh2_session_free( p_sys->ssh_session );
250     free( psz_password );
251     free( psz_username );
252     vlc_UrlClean( &url );
253     free( p_sys );
254     return VLC_EGENERIC;
255 }
256
257
258 /* Close: quit the module */
259 static void Close( vlc_object_t* p_this )
260 {
261     access_t*   p_access = (access_t*)p_this;
262     access_sys_t* p_sys = p_access->p_sys;
263
264     libssh2_sftp_close_handle( p_sys->file );
265     libssh2_sftp_shutdown( p_sys->sftp_session );
266
267     libssh2_session_free( p_sys->ssh_session );
268     free( p_sys );
269 }
270
271
272 static block_t* Block( access_t* p_access )
273 {
274     if( p_access->info.b_eof )
275         return NULL;
276
277     /* Allocate the buffer we need */
278     size_t i_len = __MIN( p_access->p_sys->i_read_size, p_access->info.i_size -
279                                               p_access->info.i_pos );
280     block_t* p_block = block_New( p_access, i_len );
281     if( !p_block )
282         return NULL;
283
284     /* Read the specified size */
285     ssize_t i_ret = libssh2_sftp_read( p_access->p_sys->file, (char*)p_block->p_buffer, i_len );
286
287     if( i_ret < 0 )
288     {
289         block_Release( p_block );
290         msg_Err( p_access, "read failed" );
291         return NULL;
292     }
293     else if( i_ret == 0 )
294     {
295         p_access->info.b_eof = true;
296         block_Release( p_block );
297         return NULL;
298     }
299     else
300     {
301         p_access->info.i_pos += i_ret;
302         return p_block;
303     }
304 }
305
306
307 static int Seek( access_t* p_access, uint64_t i_pos )
308 {
309     p_access->info.i_pos = i_pos;
310     p_access->info.b_eof = false;
311
312     libssh2_sftp_seek( p_access->p_sys->file, i_pos );
313     return VLC_SUCCESS;
314 }
315
316
317 static int Control( access_t* p_access, int i_query, va_list args )
318 {
319     bool*       pb_bool;
320     int64_t*    pi_64;
321
322     switch( i_query )
323     {
324     case ACCESS_CAN_SEEK:
325         pb_bool = (bool*)va_arg( args, bool* );
326         *pb_bool = true;
327         break;
328
329     case ACCESS_CAN_FASTSEEK:
330         pb_bool = (bool*)va_arg( args, bool* );
331         *pb_bool = false;
332         break;
333
334     case ACCESS_CAN_PAUSE:
335     case ACCESS_CAN_CONTROL_PACE:
336         pb_bool = (bool*)va_arg( args, bool* );
337         *pb_bool = true;
338         break;
339
340     case ACCESS_GET_PTS_DELAY:
341         pi_64 = (int64_t*)va_arg( args, int64_t* );
342         *pi_64 = INT64_C(1000)
343                * var_InheritInteger( p_access, "network-caching" );
344         break;
345
346     case ACCESS_SET_PAUSE_STATE:
347         break;
348
349     case ACCESS_GET_TITLE_INFO:
350     case ACCESS_SET_TITLE:
351     case ACCESS_SET_SEEKPOINT:
352     case ACCESS_SET_PRIVATE_ID_STATE:
353     case ACCESS_GET_META:
354     case ACCESS_GET_PRIVATE_ID_STATE:
355     case ACCESS_GET_CONTENT_TYPE:
356         return VLC_EGENERIC;
357
358     default:
359         msg_Warn( p_access, "unimplemented query %d in control", i_query );
360         return VLC_EGENERIC;
361     }
362
363     return VLC_SUCCESS;
364 }
365