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