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