]> git.sesse.net Git - vlc/blob - modules/access/smb.c
* modules/access/smb.c: win32 implementation which doesn't depend on libsmbclient...
[vlc] / modules / access / smb.c
1 /*****************************************************************************
2  * smb.c: SMB input module
3  *****************************************************************************
4  * Copyright (C) 2001-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #ifdef WIN32
33 #ifdef HAVE_FCNTL_H
34 #   include <fcntl.h>
35 #endif
36 #   ifdef HAVE_SYS_STAT_H
37 #       include <sys/stat.h>
38 #   endif
39 #   include <io.h>
40 #   define smbc_open(a,b,c) open(a,b,c)
41 #   define stat _stati64
42 #   define smbc_fstat(a,b) _fstati64(a,b)
43 #   define smbc_read read
44 #   define smbc_lseek _lseeki64
45 #   define smbc_close close
46 #else
47 #   include <libsmbclient.h>
48 #   define USE_CTX 1
49 #endif
50
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 static int  Open ( vlc_object_t * );
55 static void Close( vlc_object_t * );
56
57 #define CACHING_TEXT N_("Caching value in ms")
58 #define CACHING_LONGTEXT N_( \
59     "Allows you to modify the default caching value for SMB streams. This " \
60     "value should be set in millisecond units." )
61 #define USER_TEXT N_("SMB user name")
62 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
63     "be used for the connection.")
64 #define PASS_TEXT N_("SMB password")
65 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
66     "used for the connection.")
67 #define DOMAIN_TEXT N_("SMB domain")
68 #define DOMAIN_LONGTEXT N_("Allows you to modify the domain/workgroup that " \
69     "will be used for the connection.")
70
71 vlc_module_begin();
72     set_shortname( "SMB" );
73     set_description( _("SMB input") );
74     set_capability( "access2", 0 );
75     set_category( CAT_INPUT );
76     set_subcategory( SUBCAT_INPUT_ACCESS );
77     add_integer( "smb-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
78                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
79     add_string( "smb-user", NULL, NULL, USER_TEXT, USER_LONGTEXT,
80                 VLC_FALSE );
81     add_string( "smb-pwd", NULL, NULL, PASS_TEXT,
82                 PASS_LONGTEXT, VLC_FALSE );
83     add_string( "smb-domain", NULL, NULL, DOMAIN_TEXT,
84                 DOMAIN_LONGTEXT, VLC_FALSE );
85     add_shortcut( "smb" );
86     set_callbacks( Open, Close );
87 vlc_module_end();
88
89 /*****************************************************************************
90  * Local prototypes
91  *****************************************************************************/
92 static int Read( access_t *, uint8_t *, int );
93 static int Seek( access_t *, int64_t );
94 static int Control( access_t *, int, va_list );
95
96 struct access_sys_t
97 {
98 #ifdef USE_CTX
99     SMBCCTX *p_smb;
100     SMBCFILE *p_file;
101 #else
102     int i_smb;
103 #endif
104 };
105
106 #ifdef WIN32
107 static void Win32AddConnection( access_t *, char *, char *, char *, char * );
108 #endif
109
110 void smb_auth( const char *srv, const char *shr, char *wg, int wglen,
111                char *un, int unlen, char *pw, int pwlen )
112 {
113     //wglen = unlen = pwlen = 0;
114 }
115
116 /****************************************************************************
117  * Open: connect to smb server and ask for file
118  ****************************************************************************/
119 static int Open( vlc_object_t *p_this )
120 {
121     access_t     *p_access = (access_t*)p_this;
122     access_sys_t *p_sys;
123     struct stat  filestat;
124     char         *psz_path, *psz_uri;
125     char         *psz_user = 0, *psz_pwd = 0, *psz_domain = 0;
126     int          i_ret;
127
128 #ifdef USE_CTX
129     SMBCCTX      *p_smb;
130     SMBCFILE     *p_file;
131 #else
132     int          i_smb;
133 #endif
134
135     /* Parse input URI
136      * [[[domain;]user[:password@]]server[/share[/path[/file]]]] */
137
138     psz_path = strchr( p_access->psz_path, '/' );
139     if( !psz_path )
140     {
141         msg_Err( p_access, "invalid SMB URI: smb://%s", psz_path );
142         return VLC_EGENERIC;
143     }
144     else
145     {
146         char *psz_tmp = strdup( p_access->psz_path );
147         char *psz_parser;
148
149         psz_tmp[ psz_path - p_access->psz_path ] = 0;
150         psz_path = p_access->psz_path;
151         psz_parser = strchr( psz_tmp, '@' );
152         if( psz_parser )
153         {
154             /* User info is there */
155             *psz_parser = 0;
156             psz_path = p_access->psz_path + (psz_parser - psz_tmp) + 1;
157
158             psz_parser = strchr( psz_tmp, ':' );
159             if( psz_parser )
160             {
161                 /* Password found */
162                 psz_pwd = strdup( psz_parser+1 );
163                 *psz_parser = 0;
164             }
165
166             psz_parser = strchr( psz_tmp, ';' );
167             if( psz_parser )
168             {
169                 /* Domain found */
170                 *psz_parser = 0; psz_parser++;
171                 psz_domain = strdup( psz_tmp );
172             }
173             else psz_parser = psz_tmp;
174
175             psz_user = strdup( psz_parser );
176         }
177
178         free( psz_tmp );
179     }
180
181     /* Build an SMB URI
182      * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]] */
183
184     if( !psz_user ) psz_user = var_CreateGetString( p_access, "smb-user" );
185     if( psz_user && !*psz_user ) { free( psz_user ); psz_user = 0; }
186     if( !psz_pwd ) psz_pwd = var_CreateGetString( p_access, "smb-pwd" );
187     if( psz_pwd && !*psz_pwd ) { free( psz_pwd ); psz_pwd = 0; }
188     if(!psz_domain) psz_domain = var_CreateGetString( p_access, "smb-domain" );
189     if( psz_domain && !*psz_domain ) { free( psz_domain ); psz_domain = 0; }
190
191 #ifdef WIN32
192     if( psz_user )
193         Win32AddConnection( p_access, psz_path, psz_user, psz_pwd, psz_domain);
194     asprintf( &psz_uri, "//%s", psz_path );
195 #else
196     if( psz_user )
197         asprintf( &psz_uri, "smb://%s%s%s%s%s@%s",
198                   psz_domain ? psz_domain : "", psz_domain ? ";" : "",
199                   psz_user, psz_pwd ? ":" : "",
200                   psz_pwd ? psz_pwd : "", psz_path );
201     else
202         asprintf( &psz_uri, "smb://%s", psz_path );
203 #endif
204
205     if( psz_user ) free( psz_user );
206     if( psz_pwd ) free( psz_pwd );
207     if( psz_domain ) free( psz_domain );
208
209 #ifdef USE_CTX
210     if( !(p_smb = smbc_new_context()) )
211     {
212         msg_Err( p_access, "out of memory" );
213         free( psz_uri );
214         return VLC_ENOMEM;
215     }
216     p_smb->debug = 1;
217     p_smb->callbacks.auth_fn = smb_auth;
218
219     if( !smbc_init_context( p_smb ) )
220     {
221         msg_Err( p_access, "cannot initialize context (%s)", strerror(errno) );
222         smbc_free_context( p_smb, 1 );
223         free( psz_uri );
224         return VLC_EGENERIC;
225     }
226
227     if( !(p_file = p_smb->open( p_smb, psz_uri, O_RDONLY, 0 )) )
228     {
229         msg_Err( p_access, "open failed for '%s' (%s)",
230                  p_access->psz_path, strerror(errno) );
231         smbc_free_context( p_smb, 1 );
232         free( psz_uri );
233         return VLC_EGENERIC;
234     }
235
236     p_access->info.i_size = 0;
237     i_ret = p_smb->fstat( p_smb, p_file, &filestat );
238     if( i_ret ) msg_Err( p_access, "stat failed (%s)", strerror(errno) );
239     else p_access->info.i_size = filestat.st_size;
240
241 #else
242
243 #ifndef WIN32
244     if( smbc_init( smb_auth, 1 ) )
245     {
246         free( psz_uri );
247         return VLC_EGENERIC;
248     }
249 #endif
250
251     if( (i_smb = smbc_open( psz_uri, O_RDONLY, 0 )) < 0 )
252     {
253         msg_Err( p_access, "open failed for '%s' (%s)",
254                  p_access->psz_path, strerror(errno) );
255         free( psz_uri );
256         return VLC_EGENERIC;
257     }
258
259     p_access->info.i_size = 0;
260     i_ret = smbc_fstat( i_smb, &filestat );
261     if( i_ret ) msg_Err( p_access, "stat failed (%s)", strerror(i_ret) );
262     else p_access->info.i_size = filestat.st_size;
263 #endif
264
265     free( psz_uri );
266
267     /* Init p_access */
268     p_access->pf_read = Read;
269     p_access->pf_block = NULL;
270     p_access->pf_seek = Seek;
271     p_access->pf_control = Control;
272     p_access->info.i_update = 0;
273     p_access->info.i_pos = 0;
274     p_access->info.b_eof = VLC_FALSE;
275     p_access->info.i_title = 0;
276     p_access->info.i_seekpoint = 0;
277     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
278     memset( p_sys, 0, sizeof( access_sys_t ) );
279
280 #ifdef USE_CTX
281     p_sys->p_smb = p_smb;
282     p_sys->p_file = p_file;
283 #else
284     p_sys->i_smb = i_smb;
285 #endif
286
287     /* Update default_pts to a suitable value for smb access */
288     var_Create( p_access, "smb-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
289
290     return VLC_SUCCESS;
291 }
292
293 /*****************************************************************************
294  * Close: free unused data structures
295  *****************************************************************************/
296 static void Close( vlc_object_t *p_this )
297 {
298     access_t     *p_access = (access_t*)p_this;
299     access_sys_t *p_sys = p_access->p_sys;
300
301 #ifdef USE_CTX
302     p_sys->p_smb->close( p_sys->p_smb, p_sys->p_file );
303     smbc_free_context( p_sys->p_smb, 1 );
304 #else
305     smbc_close( p_sys->i_smb );
306 #endif
307
308     free( p_sys );
309 }
310
311 /*****************************************************************************
312  * Seek: try to go at the right place
313  *****************************************************************************/
314 static int Seek( access_t *p_access, int64_t i_pos )
315 {
316     access_sys_t *p_sys = p_access->p_sys;
317     int64_t      i_ret;
318
319     if( i_pos < 0 ) return VLC_EGENERIC;
320
321     msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
322
323 #ifdef USE_CTX
324     i_ret = p_sys->p_smb->lseek(p_sys->p_smb, p_sys->p_file, i_pos, SEEK_SET);
325 #else
326     i_ret = smbc_lseek( p_sys->i_smb, i_pos, SEEK_SET );
327 #endif
328     if( i_ret == -1 )
329     {
330         msg_Err( p_access, "seek failed (%s)", strerror(errno) );
331         return VLC_EGENERIC;
332     }
333
334     p_access->info.b_eof = VLC_FALSE;
335     p_access->info.i_pos = i_ret;
336
337     return VLC_SUCCESS;
338 }
339
340 /*****************************************************************************
341  * Read:
342  *****************************************************************************/
343 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
344 {
345     access_sys_t *p_sys = p_access->p_sys;
346     int i_read;
347
348     if( p_access->info.b_eof ) return 0;
349
350 #ifdef USE_CTX
351     i_read = p_sys->p_smb->read(p_sys->p_smb, p_sys->p_file, p_buffer, i_len);
352 #else
353     i_read = smbc_read( p_sys->i_smb, p_buffer, i_len );
354 #endif
355     if( i_read < 0 )
356     {
357         msg_Err( p_access, "read failed (%s)", strerror(errno) );
358         return -1;
359     }
360
361     if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
362     else if( i_read > 0 ) p_access->info.i_pos += i_read;
363
364     return i_read;
365 }
366
367 /*****************************************************************************
368  * Control:
369  *****************************************************************************/
370 static int Control( access_t *p_access, int i_query, va_list args )
371 {
372     vlc_bool_t   *pb_bool;
373     int          *pi_int;
374     int64_t      *pi_64;
375
376     switch( i_query )
377     {
378     case ACCESS_CAN_SEEK:
379         pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
380         *pb_bool = VLC_TRUE;
381         break;
382     case ACCESS_CAN_FASTSEEK:
383         pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
384         *pb_bool = VLC_TRUE;
385         break;
386     case ACCESS_CAN_PAUSE:
387         pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
388         *pb_bool = VLC_TRUE;
389         break;
390     case ACCESS_CAN_CONTROL_PACE:
391         pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
392         *pb_bool = VLC_TRUE;
393         break;
394
395     case ACCESS_GET_MTU:
396         pi_int = (int*)va_arg( args, int * );
397         *pi_int = 0;
398         break;
399
400     case ACCESS_GET_PTS_DELAY:
401         pi_64 = (int64_t*)va_arg( args, int64_t * );
402         *pi_64 = (int64_t)var_GetInteger( p_access, "smb-caching" ) * 1000;
403         break;
404
405     case ACCESS_SET_PAUSE_STATE:
406         /* Nothing to do */
407         break;
408
409     case ACCESS_GET_TITLE_INFO:
410     case ACCESS_SET_TITLE:
411     case ACCESS_SET_SEEKPOINT:
412     case ACCESS_SET_PRIVATE_ID_STATE:
413         return VLC_EGENERIC;
414
415     default:
416         msg_Warn( p_access, "unimplemented query in control" );
417         return VLC_EGENERIC;
418
419     }
420
421     return VLC_SUCCESS;
422 }
423
424 #ifdef WIN32
425 static void Win32AddConnection( access_t *p_access, char *psz_path,
426                                 char *psz_user, char *psz_pwd,
427                                 char *psz_domain )
428 {
429     DWORD (*OurWNetAddConnection2)( LPNETRESOURCE, LPCTSTR, LPCTSTR, DWORD );
430     char psz_remote[MAX_PATH], psz_server[MAX_PATH], psz_share[MAX_PATH];
431     NETRESOURCE net_resource;
432     DWORD i_result;
433     char *psz_parser;
434
435     HINSTANCE hdll = LoadLibrary(_T("MPR.DLL"));
436     if( !hdll )
437     {
438         msg_Warn( p_access, "couldn't load mpr.dll" );
439         return;
440     }
441
442     OurWNetAddConnection2 =
443       (void *)GetProcAddress( hdll, _T("WNetAddConnection2A") );
444     if( !OurWNetAddConnection2 )
445     {
446         msg_Warn( p_access, "couldn't find WNetAddConnection2 in mpr.dll" );
447         return;
448     }
449
450     memset( &net_resource, 0, sizeof(net_resource) );
451     net_resource.dwType = RESOURCETYPE_DISK;
452
453     /* Find out server and share names */
454     psz_server[0] = psz_share[0] = 0;
455     psz_parser = strchr( psz_path, '/' );
456     if( psz_parser )
457     {
458         char *psz_parser2;
459         strncat( psz_server, psz_path, psz_parser - psz_path );
460         psz_parser2 = strchr( psz_parser+1, '/' );
461         if( psz_parser2 )
462             strncat( psz_share, psz_parser+1, psz_parser2 - psz_parser -1 );
463     }
464     else strncat( psz_server, psz_path, MAX_PATH );
465
466     sprintf( psz_remote, "\\\\%s\\%s", psz_server, psz_share );
467     net_resource.lpRemoteName = psz_remote;
468
469     i_result = OurWNetAddConnection2( &net_resource, psz_pwd, psz_user, 0 );
470
471     if( i_result != NO_ERROR )
472     {
473         msg_Dbg( p_access, "connected to %s", psz_remote );
474     }
475     else if( i_result != ERROR_ALREADY_ASSIGNED && 
476              i_result != ERROR_DEVICE_ALREADY_REMEMBERED )
477     {
478         msg_Dbg( p_access, "already connected to %s", psz_remote );
479     }
480     else
481     {
482         msg_Dbg( p_access, "failed to connect to %s", psz_remote );
483     }
484
485     FreeLibrary( hdll );
486 }
487 #endif // WIN32