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