]> git.sesse.net Git - vlc/blob - modules/access/smb.c
b47e6fe06f9e71e771e68fd8191309d434ab3eed
[vlc] / modules / access / smb.c
1 /*****************************************************************************
2  * smb.c: SMB input module
3  *****************************************************************************
4  * Copyright (C) 2001-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@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_charset.h>
33 #include <vlc_plugin.h>
34 #include <vlc_access.h>
35
36 #ifdef WIN32
37 #   ifdef HAVE_FCNTL_H
38 #       include <fcntl.h>
39 #   endif
40 #   ifdef HAVE_SYS_STAT_H
41 #       include <sys/stat.h>
42 #   endif
43 #   include <io.h>
44 #   define smbc_open(a,b,c) utf8_open(a,b,c)
45 #   define smbc_fstat(a,b) _fstati64(a,b)
46 #   define smbc_read read
47 #   define smbc_lseek _lseeki64
48 #   define smbc_close close
49 #else
50 #   include <libsmbclient.h>
51 #endif
52
53 #include <errno.h>
54
55 /*****************************************************************************
56  * Module descriptor
57  *****************************************************************************/
58 static int  Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
60
61 #define CACHING_TEXT N_("Caching value in ms")
62 #define CACHING_LONGTEXT N_( \
63     "Caching value for SMB streams. This " \
64     "value should be set in milliseconds." )
65 #define USER_TEXT N_("SMB user name")
66 #define USER_LONGTEXT N_("User name that will " \
67     "be used for the connection.")
68 #define PASS_TEXT N_("SMB password")
69 #define PASS_LONGTEXT N_("Password that will be " \
70     "used for the connection.")
71 #define DOMAIN_TEXT N_("SMB domain")
72 #define DOMAIN_LONGTEXT N_("Domain/Workgroup that " \
73     "will be used for the connection.")
74
75 vlc_module_begin ()
76     set_shortname( "SMB" )
77     set_description( N_("SMB input") )
78     set_capability( "access", 0 )
79     set_category( CAT_INPUT )
80     set_subcategory( SUBCAT_INPUT_ACCESS )
81     add_integer( "smb-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
82                  CACHING_TEXT, CACHING_LONGTEXT, true )
83         change_safe()
84     add_string( "smb-user", NULL, NULL, USER_TEXT, USER_LONGTEXT,
85                 false )
86     add_password( "smb-pwd", NULL, NULL, PASS_TEXT,
87                   PASS_LONGTEXT, false )
88     add_string( "smb-domain", NULL, NULL, DOMAIN_TEXT,
89                 DOMAIN_LONGTEXT, false )
90     add_shortcut( "smb" )
91     set_callbacks( Open, Close )
92 vlc_module_end ()
93
94 /*****************************************************************************
95  * Local prototypes
96  *****************************************************************************/
97 static ssize_t Read( access_t *, uint8_t *, size_t );
98 static int Seek( access_t *, int64_t );
99 static int Control( access_t *, int, va_list );
100
101 struct access_sys_t
102 {
103     int i_smb;
104 };
105
106 #ifdef WIN32
107 static void Win32AddConnection( access_t *, char *, char *, char *, char * );
108 #else
109 static void smb_auth( const char *srv, const char *shr, char *wg, int wglen,
110                       char *un, int unlen, char *pw, int pwlen )
111 {
112     VLC_UNUSED(srv);
113     VLC_UNUSED(shr);
114     VLC_UNUSED(wg);
115     VLC_UNUSED(wglen);
116     VLC_UNUSED(un);
117     VLC_UNUSED(unlen);
118     VLC_UNUSED(pw);
119     VLC_UNUSED(pwlen);
120     //wglen = unlen = pwlen = 0;
121 }
122 #endif
123
124 /****************************************************************************
125  * Open: connect to smb server and ask for file
126  ****************************************************************************/
127 static int Open( vlc_object_t *p_this )
128 {
129     access_t     *p_access = (access_t*)p_this;
130     access_sys_t *p_sys;
131     struct stat  filestat;
132     char         *psz_path, *psz_uri;
133     char         *psz_user = NULL, *psz_pwd = NULL, *psz_domain = NULL;
134     int          i_ret;
135     int          i_smb;
136
137     /* Parse input URI
138      * [[[domain;]user[:password@]]server[/share[/path[/file]]]] */
139     psz_path = strchr( p_access->psz_path, '/' );
140     if( !psz_path )
141     {
142         msg_Err( p_access, "invalid SMB URI: smb://%s", psz_path );
143         return VLC_EGENERIC;
144     }
145     else
146     {
147         char *psz_tmp = strdup( p_access->psz_path );
148         char *psz_parser;
149
150         psz_tmp[ psz_path - p_access->psz_path ] = 0;
151         psz_path = p_access->psz_path;
152         psz_parser = strchr( psz_tmp, '@' );
153         if( psz_parser )
154         {
155             /* User info is there */
156             *psz_parser = 0;
157             psz_path = p_access->psz_path + (psz_parser - psz_tmp) + 1;
158
159             psz_parser = strchr( psz_tmp, ':' );
160             if( psz_parser )
161             {
162                 /* Password found */
163                 psz_pwd = strdup( psz_parser+1 );
164                 *psz_parser = 0;
165             }
166
167             psz_parser = strchr( psz_tmp, ';' );
168             if( psz_parser )
169             {
170                 /* Domain found */
171                 *psz_parser = 0; psz_parser++;
172                 psz_domain = strdup( psz_tmp );
173             }
174             else psz_parser = psz_tmp;
175
176             psz_user = strdup( psz_parser );
177         }
178
179         free( psz_tmp );
180     }
181
182     /* Build an SMB URI
183      * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]] */
184
185     if( !psz_user ) psz_user = var_CreateGetString( p_access, "smb-user" );
186     if( psz_user && !*psz_user ) { free( psz_user ); psz_user = NULL; }
187     if( !psz_pwd ) psz_pwd = var_CreateGetString( p_access, "smb-pwd" );
188     if( psz_pwd && !*psz_pwd ) { free( psz_pwd ); psz_pwd = NULL; }
189     if( !psz_domain ) psz_domain = var_CreateGetString( p_access, "smb-domain" );
190     if( psz_domain && !*psz_domain ) { free( psz_domain ); psz_domain = NULL; }
191
192 #ifdef WIN32
193     if( psz_user )
194         Win32AddConnection( p_access, psz_path, psz_user, psz_pwd, psz_domain);
195     i_ret = asprintf( &psz_uri, "//%s", psz_path );
196 #else
197     if( psz_user )
198         i_ret = asprintf( &psz_uri, "smb://%s%s%s%s%s@%s",
199                           psz_domain ? psz_domain : "", psz_domain ? ";" : "",
200                           psz_user, psz_pwd ? ":" : "",
201                           psz_pwd ? psz_pwd : "", psz_path );
202     else
203         i_ret = asprintf( &psz_uri, "smb://%s", psz_path );
204 #endif
205
206     free( psz_user );
207     free( psz_pwd );
208     free( psz_domain );
209
210     if( i_ret == -1 )
211         return VLC_ENOMEM;
212
213 #ifndef WIN32
214     if( smbc_init( smb_auth, 0 ) )
215     {
216         free( psz_uri );
217         return VLC_EGENERIC;
218     }
219 #endif
220
221 /*
222 ** some version of glibc defines open as a macro, causing havoc
223 ** with other macros using 'open' under the hood, such as the
224 ** following one:
225 */
226 #if defined(smbc_open) && defined(open)
227 # undef open
228 #endif
229     if( (i_smb = smbc_open( psz_uri, O_RDONLY, 0 )) < 0 )
230     {
231         msg_Err( p_access, "open failed for '%s' (%m)", p_access->psz_path );
232         free( psz_uri );
233         return VLC_EGENERIC;
234     }
235
236     /* Init p_access */
237     STANDARD_READ_ACCESS_INIT;
238
239     i_ret = smbc_fstat( i_smb, &filestat );
240     if( i_ret )
241     {
242         errno = i_ret;
243         msg_Err( p_access, "stat failed (%m)" );
244     }
245     else
246         p_access->info.i_size = filestat.st_size;
247
248     free( psz_uri );
249
250     p_sys->i_smb = i_smb;
251
252     /* Update default_pts to a suitable value for smb access */
253     var_Create( p_access, "smb-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
254
255     return VLC_SUCCESS;
256 }
257
258 /*****************************************************************************
259  * Close: free unused data structures
260  *****************************************************************************/
261 static void Close( vlc_object_t *p_this )
262 {
263     access_t     *p_access = (access_t*)p_this;
264     access_sys_t *p_sys = p_access->p_sys;
265
266     smbc_close( p_sys->i_smb );
267     free( p_sys );
268 }
269
270 /*****************************************************************************
271  * Seek: try to go at the right place
272  *****************************************************************************/
273 static int Seek( access_t *p_access, int64_t i_pos )
274 {
275     access_sys_t *p_sys = p_access->p_sys;
276     int64_t      i_ret;
277
278     if( i_pos < 0 ) return VLC_EGENERIC;
279
280     msg_Dbg( p_access, "seeking to %"PRId64, i_pos );
281
282     i_ret = smbc_lseek( p_sys->i_smb, i_pos, SEEK_SET );
283     if( i_ret == -1 )
284     {
285         msg_Err( p_access, "seek failed (%m)" );
286         return VLC_EGENERIC;
287     }
288
289     p_access->info.b_eof = false;
290     p_access->info.i_pos = i_ret;
291
292     return VLC_SUCCESS;
293 }
294
295 /*****************************************************************************
296  * Read:
297  *****************************************************************************/
298 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
299 {
300     access_sys_t *p_sys = p_access->p_sys;
301     int i_read;
302
303     if( p_access->info.b_eof ) return 0;
304
305     i_read = smbc_read( p_sys->i_smb, p_buffer, i_len );
306     if( i_read < 0 )
307     {
308         msg_Err( p_access, "read failed (%m)" );
309         return -1;
310     }
311
312     if( i_read == 0 ) p_access->info.b_eof = true;
313     else if( i_read > 0 ) p_access->info.i_pos += i_read;
314
315     return i_read;
316 }
317
318 /*****************************************************************************
319  * Control:
320  *****************************************************************************/
321 static int Control( access_t *p_access, int i_query, va_list args )
322 {
323     switch( i_query )
324     {
325     case ACCESS_CAN_SEEK:
326     case ACCESS_CAN_FASTSEEK:
327     case ACCESS_CAN_PAUSE:
328     case ACCESS_CAN_CONTROL_PACE:
329         *va_arg( args, bool* ) = true;
330         break;
331
332     case ACCESS_GET_PTS_DELAY:
333         *va_arg( args, int64_t * )
334                   = (int64_t)var_GetInteger( p_access, "smb-caching" ) * 1000;
335         break;
336
337     case ACCESS_SET_PAUSE_STATE:
338         /* Nothing to do */
339         break;
340
341     case ACCESS_GET_TITLE_INFO:
342     case ACCESS_SET_TITLE:
343     case ACCESS_SET_SEEKPOINT:
344     case ACCESS_SET_PRIVATE_ID_STATE:
345     case ACCESS_GET_CONTENT_TYPE:
346         return VLC_EGENERIC;
347
348     default:
349         msg_Warn( p_access, "unimplemented query in control" );
350         return VLC_EGENERIC;
351
352     }
353
354     return VLC_SUCCESS;
355 }
356
357 #ifdef WIN32
358 static void Win32AddConnection( access_t *p_access, char *psz_path,
359                                 char *psz_user, char *psz_pwd,
360                                 char *psz_domain )
361 {
362     DWORD (*OurWNetAddConnection2)( LPNETRESOURCE, LPCTSTR, LPCTSTR, DWORD );
363     char psz_remote[MAX_PATH], psz_server[MAX_PATH], psz_share[MAX_PATH];
364     NETRESOURCE net_resource;
365     DWORD i_result;
366     char *psz_parser;
367     VLC_UNUSED( psz_domain );
368
369     HINSTANCE hdll = LoadLibrary(_T("MPR.DLL"));
370     if( !hdll )
371     {
372         msg_Warn( p_access, "couldn't load mpr.dll" );
373         return;
374     }
375
376     OurWNetAddConnection2 =
377       (void *)GetProcAddress( hdll, _T("WNetAddConnection2A") );
378     if( !OurWNetAddConnection2 )
379     {
380         msg_Warn( p_access, "couldn't find WNetAddConnection2 in mpr.dll" );
381         return;
382     }
383
384     memset( &net_resource, 0, sizeof(net_resource) );
385     net_resource.dwType = RESOURCETYPE_DISK;
386
387     /* Find out server and share names */
388     strlcpy( psz_server, psz_path, sizeof( psz_server ) );
389     psz_share[0] = 0;
390     psz_parser = strchr( psz_path, '/' );
391     if( psz_parser )
392     {
393         char *psz_parser2 = strchr( ++psz_parser, '/' );
394         if( psz_parser2 )
395             strlcpy( psz_share, psz_parser, sizeof( psz_share ) );
396    }
397
398     snprintf( psz_remote, sizeof( psz_remote ), "\\\\%s\\%s", psz_server, psz_share );
399     net_resource.lpRemoteName = psz_remote;
400
401     i_result = OurWNetAddConnection2( &net_resource, psz_pwd, psz_user, 0 );
402
403     if( i_result != NO_ERROR )
404     {
405         msg_Dbg( p_access, "connected to %s", psz_remote );
406     }
407     else if( i_result != ERROR_ALREADY_ASSIGNED &&
408              i_result != ERROR_DEVICE_ALREADY_REMEMBERED )
409     {
410         msg_Dbg( p_access, "already connected to %s", psz_remote );
411     }
412     else
413     {
414         msg_Dbg( p_access, "failed to connect to %s", psz_remote );
415     }
416
417     FreeLibrary( hdll );
418 }
419 #endif // WIN32