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