1 /*****************************************************************************
2 * smb.c: SMB input module
3 *****************************************************************************
4 * Copyright (C) 2001-2009 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_charset.h>
33 #include <vlc_plugin.h>
34 #include <vlc_access.h>
40 # ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
44 # define smbc_open(a,b,c) utf8_open(a,b,c)
45 # define smbc_fstat(a,b) _fstati64(a,b)
46 # define smbc_read read
47 # define smbc_lseek _lseeki64
48 # define smbc_close close
50 # include <libsmbclient.h>
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
61 #define CACHING_TEXT N_("Caching value in ms")
62 #define CACHING_LONGTEXT N_( \
63 "Caching value for SMB streams. This " \
64 "value should be set in milliseconds." )
65 #define USER_TEXT N_("SMB user name")
66 #define USER_LONGTEXT N_("User name that will " \
67 "be used for the connection.")
68 #define PASS_TEXT N_("SMB password")
69 #define PASS_LONGTEXT N_("Password that will be " \
70 "used for the connection.")
71 #define DOMAIN_TEXT N_("SMB domain")
72 #define DOMAIN_LONGTEXT N_("Domain/Workgroup that " \
73 "will be used for the connection.")
76 set_shortname( "SMB" )
77 set_description( N_("SMB input") )
78 set_capability( "access", 0 )
79 set_category( CAT_INPUT )
80 set_subcategory( SUBCAT_INPUT_ACCESS )
81 add_integer( "smb-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
82 CACHING_TEXT, CACHING_LONGTEXT, true )
84 add_string( "smb-user", NULL, NULL, USER_TEXT, USER_LONGTEXT,
86 add_password( "smb-pwd", NULL, NULL, PASS_TEXT,
87 PASS_LONGTEXT, false )
88 add_string( "smb-domain", NULL, NULL, DOMAIN_TEXT,
89 DOMAIN_LONGTEXT, false )
91 set_callbacks( Open, Close )
94 /*****************************************************************************
96 *****************************************************************************/
97 static ssize_t Read( access_t *, uint8_t *, size_t );
98 static int Seek( access_t *, int64_t );
99 static int Control( access_t *, int, va_list );
107 static void Win32AddConnection( access_t *, char *, char *, char *, char * );
109 static void smb_auth( const char *srv, const char *shr, char *wg, int wglen,
110 char *un, int unlen, char *pw, int pwlen )
112 //wglen = unlen = pwlen = 0;
116 /****************************************************************************
117 * Open: connect to smb server and ask for file
118 ****************************************************************************/
119 static int Open( vlc_object_t *p_this )
121 access_t *p_access = (access_t*)p_this;
123 struct stat filestat;
124 char *psz_path, *psz_uri;
125 char *psz_user = NULL, *psz_pwd = NULL, *psz_domain = NULL;
130 * [[[domain;]user[:password@]]server[/share[/path[/file]]]] */
131 psz_path = strchr( p_access->psz_path, '/' );
134 msg_Err( p_access, "invalid SMB URI: smb://%s", psz_path );
139 char *psz_tmp = strdup( p_access->psz_path );
142 psz_tmp[ psz_path - p_access->psz_path ] = 0;
143 psz_path = p_access->psz_path;
144 psz_parser = strchr( psz_tmp, '@' );
147 /* User info is there */
149 psz_path = p_access->psz_path + (psz_parser - psz_tmp) + 1;
151 psz_parser = strchr( psz_tmp, ':' );
155 psz_pwd = strdup( psz_parser+1 );
159 psz_parser = strchr( psz_tmp, ';' );
163 *psz_parser = 0; psz_parser++;
164 psz_domain = strdup( psz_tmp );
166 else psz_parser = psz_tmp;
168 psz_user = strdup( psz_parser );
175 * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]] */
177 if( !psz_user ) psz_user = var_CreateGetString( p_access, "smb-user" );
178 if( psz_user && !*psz_user ) { free( psz_user ); psz_user = NULL; }
179 if( !psz_pwd ) psz_pwd = var_CreateGetString( p_access, "smb-pwd" );
180 if( psz_pwd && !*psz_pwd ) { free( psz_pwd ); psz_pwd = NULL; }
181 if( !psz_domain ) psz_domain = var_CreateGetString( p_access, "smb-domain" );
182 if( psz_domain && !*psz_domain ) { free( psz_domain ); psz_domain = NULL; }
186 Win32AddConnection( p_access, psz_path, psz_user, psz_pwd, psz_domain);
187 i_ret = asprintf( &psz_uri, "//%s", psz_path );
190 i_ret = asprintf( &psz_uri, "smb://%s%s%s%s%s@%s",
191 psz_domain ? psz_domain : "", psz_domain ? ";" : "",
192 psz_user, psz_pwd ? ":" : "",
193 psz_pwd ? psz_pwd : "", psz_path );
195 i_ret = asprintf( &psz_uri, "smb://%s", psz_path );
206 if( smbc_init( smb_auth, 0 ) )
214 ** some version of glibc defines open as a macro, causing havoc
215 ** with other macros using 'open' under the hood, such as the
218 #if defined(smbc_open) && defined(open)
221 if( (i_smb = smbc_open( psz_uri, O_RDONLY, 0 )) < 0 )
223 msg_Err( p_access, "open failed for '%s' (%m)", p_access->psz_path );
229 STANDARD_READ_ACCESS_INIT;
231 i_ret = smbc_fstat( i_smb, &filestat );
235 msg_Err( p_access, "stat failed (%m)" );
238 p_access->info.i_size = filestat.st_size;
242 p_sys->i_smb = i_smb;
244 /* Update default_pts to a suitable value for smb access */
245 var_Create( p_access, "smb-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
250 /*****************************************************************************
251 * Close: free unused data structures
252 *****************************************************************************/
253 static void Close( vlc_object_t *p_this )
255 access_t *p_access = (access_t*)p_this;
256 access_sys_t *p_sys = p_access->p_sys;
258 smbc_close( p_sys->i_smb );
262 /*****************************************************************************
263 * Seek: try to go at the right place
264 *****************************************************************************/
265 static int Seek( access_t *p_access, int64_t i_pos )
267 access_sys_t *p_sys = p_access->p_sys;
270 if( i_pos < 0 ) return VLC_EGENERIC;
272 msg_Dbg( p_access, "seeking to %"PRId64, i_pos );
274 i_ret = smbc_lseek( p_sys->i_smb, i_pos, SEEK_SET );
277 msg_Err( p_access, "seek failed (%m)" );
281 p_access->info.b_eof = false;
282 p_access->info.i_pos = i_ret;
287 /*****************************************************************************
289 *****************************************************************************/
290 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
292 access_sys_t *p_sys = p_access->p_sys;
295 if( p_access->info.b_eof ) return 0;
297 i_read = smbc_read( p_sys->i_smb, p_buffer, i_len );
300 msg_Err( p_access, "read failed (%m)" );
304 if( i_read == 0 ) p_access->info.b_eof = true;
305 else if( i_read > 0 ) p_access->info.i_pos += i_read;
310 /*****************************************************************************
312 *****************************************************************************/
313 static int Control( access_t *p_access, int i_query, va_list args )
317 case ACCESS_CAN_SEEK:
318 case ACCESS_CAN_FASTSEEK:
319 case ACCESS_CAN_PAUSE:
320 case ACCESS_CAN_CONTROL_PACE:
321 *va_arg( args, bool* ) = true;
324 case ACCESS_GET_PTS_DELAY:
325 *va_arg( args, int64_t * )
326 = (int64_t)var_GetInteger( p_access, "smb-caching" ) * 1000;
329 case ACCESS_SET_PAUSE_STATE:
333 case ACCESS_GET_TITLE_INFO:
334 case ACCESS_SET_TITLE:
335 case ACCESS_SET_SEEKPOINT:
336 case ACCESS_SET_PRIVATE_ID_STATE:
337 case ACCESS_GET_CONTENT_TYPE:
341 msg_Warn( p_access, "unimplemented query in control" );
350 static void Win32AddConnection( access_t *p_access, char *psz_path,
351 char *psz_user, char *psz_pwd,
354 DWORD (*OurWNetAddConnection2)( LPNETRESOURCE, LPCTSTR, LPCTSTR, DWORD );
355 char psz_remote[MAX_PATH], psz_server[MAX_PATH], psz_share[MAX_PATH];
356 NETRESOURCE net_resource;
359 VLC_UNUSED( psz_domain );
361 HINSTANCE hdll = LoadLibrary(_T("MPR.DLL"));
364 msg_Warn( p_access, "couldn't load mpr.dll" );
368 OurWNetAddConnection2 =
369 (void *)GetProcAddress( hdll, _T("WNetAddConnection2A") );
370 if( !OurWNetAddConnection2 )
372 msg_Warn( p_access, "couldn't find WNetAddConnection2 in mpr.dll" );
376 memset( &net_resource, 0, sizeof(net_resource) );
377 net_resource.dwType = RESOURCETYPE_DISK;
379 /* Find out server and share names */
380 strlcpy( psz_server, psz_path, sizeof( psz_server ) );
382 psz_parser = strchr( psz_path, '/' );
385 char *psz_parser2 = strchr( ++psz_parser, '/' );
387 strlcpy( psz_share, psz_parser, sizeof( psz_share ) );
390 snprintf( psz_remote, sizeof( psz_remote ), "\\\\%s\\%s", psz_server, psz_share );
391 net_resource.lpRemoteName = psz_remote;
393 i_result = OurWNetAddConnection2( &net_resource, psz_pwd, psz_user, 0 );
395 if( i_result != NO_ERROR )
397 msg_Dbg( p_access, "connected to %s", psz_remote );
399 else if( i_result != ERROR_ALREADY_ASSIGNED &&
400 i_result != ERROR_DEVICE_ALREADY_REMEMBERED )
402 msg_Dbg( p_access, "already connected to %s", psz_remote );
406 msg_Dbg( p_access, "failed to connect to %s", psz_remote );