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