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 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_access.h>
41 # ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
45 # define smbc_open(a,b,c) utf8_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
52 # include <libsmbclient.h>
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
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.")
78 set_shortname( "SMB" )
79 set_description( N_("SMB input") )
80 set_capability( "access", 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, true )
86 add_string( "smb-user", NULL, NULL, USER_TEXT, USER_LONGTEXT,
88 add_password( "smb-pwd", NULL, NULL, PASS_TEXT,
89 PASS_LONGTEXT, false )
90 add_string( "smb-domain", NULL, NULL, DOMAIN_TEXT,
91 DOMAIN_LONGTEXT, false )
93 set_callbacks( Open, Close )
96 /*****************************************************************************
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 );
109 static void Win32AddConnection( access_t *, char *, char *, char *, char * );
111 static void smb_auth( const char *srv, const char *shr, char *wg, int wglen,
112 char *un, int unlen, char *pw, int pwlen )
114 //wglen = unlen = pwlen = 0;
118 /****************************************************************************
119 * Open: connect to smb server and ask for file
120 ****************************************************************************/
121 static int Open( vlc_object_t *p_this )
123 access_t *p_access = (access_t*)p_this;
125 struct stat filestat;
126 char *psz_path, *psz_uri;
127 char *psz_user = NULL, *psz_pwd = NULL, *psz_domain = NULL;
132 * [[[domain;]user[:password@]]server[/share[/path[/file]]]] */
133 psz_path = strchr( p_access->psz_path, '/' );
136 msg_Err( p_access, "invalid SMB URI: smb://%s", psz_path );
141 char *psz_tmp = strdup( p_access->psz_path );
144 psz_tmp[ psz_path - p_access->psz_path ] = 0;
145 psz_path = p_access->psz_path;
146 psz_parser = strchr( psz_tmp, '@' );
149 /* User info is there */
151 psz_path = p_access->psz_path + (psz_parser - psz_tmp) + 1;
153 psz_parser = strchr( psz_tmp, ':' );
157 psz_pwd = strdup( psz_parser+1 );
161 psz_parser = strchr( psz_tmp, ';' );
165 *psz_parser = 0; psz_parser++;
166 psz_domain = strdup( psz_tmp );
168 else psz_parser = psz_tmp;
170 psz_user = strdup( psz_parser );
177 * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]] */
179 if( !psz_user ) psz_user = var_CreateGetString( p_access, "smb-user" );
180 if( psz_user && !*psz_user ) { free( psz_user ); psz_user = NULL; }
181 if( !psz_pwd ) psz_pwd = var_CreateGetString( p_access, "smb-pwd" );
182 if( psz_pwd && !*psz_pwd ) { free( psz_pwd ); psz_pwd = NULL; }
183 if( !psz_domain ) psz_domain = var_CreateGetString( p_access, "smb-domain" );
184 if( psz_domain && !*psz_domain ) { free( psz_domain ); psz_domain = NULL; }
188 Win32AddConnection( p_access, psz_path, psz_user, psz_pwd, psz_domain);
189 i_ret = asprintf( &psz_uri, "//%s", psz_path );
192 i_ret = asprintf( &psz_uri, "smb://%s%s%s%s%s@%s",
193 psz_domain ? psz_domain : "", psz_domain ? ";" : "",
194 psz_user, psz_pwd ? ":" : "",
195 psz_pwd ? psz_pwd : "", psz_path );
197 i_ret = asprintf( &psz_uri, "smb://%s", psz_path );
208 if( smbc_init( smb_auth, 0 ) )
216 ** some version of glibc defines open as a macro, causing havoc
217 ** with other macros using 'open' under the hood, such as the
220 #if defined(smbc_open) && defined(open)
223 if( (i_smb = smbc_open( psz_uri, O_RDONLY, 0 )) < 0 )
225 msg_Err( p_access, "open failed for '%s' (%m)", p_access->psz_path );
231 STANDARD_READ_ACCESS_INIT;
233 i_ret = smbc_fstat( i_smb, &filestat );
237 msg_Err( p_access, "stat failed (%m)" );
240 p_access->info.i_size = filestat.st_size;
244 p_sys->i_smb = i_smb;
246 /* Update default_pts to a suitable value for smb access */
247 var_Create( p_access, "smb-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
252 /*****************************************************************************
253 * Close: free unused data structures
254 *****************************************************************************/
255 static void Close( vlc_object_t *p_this )
257 access_t *p_access = (access_t*)p_this;
258 access_sys_t *p_sys = p_access->p_sys;
260 smbc_close( p_sys->i_smb );
264 /*****************************************************************************
265 * Seek: try to go at the right place
266 *****************************************************************************/
267 static int Seek( access_t *p_access, int64_t i_pos )
269 access_sys_t *p_sys = p_access->p_sys;
272 if( i_pos < 0 ) return VLC_EGENERIC;
274 msg_Dbg( p_access, "seeking to %"PRId64, i_pos );
276 i_ret = smbc_lseek( p_sys->i_smb, i_pos, SEEK_SET );
279 msg_Err( p_access, "seek failed (%m)" );
283 p_access->info.b_eof = false;
284 p_access->info.i_pos = i_ret;
289 /*****************************************************************************
291 *****************************************************************************/
292 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
294 access_sys_t *p_sys = p_access->p_sys;
297 if( p_access->info.b_eof ) return 0;
299 i_read = smbc_read( p_sys->i_smb, p_buffer, i_len );
302 msg_Err( p_access, "read failed (%m)" );
306 if( i_read == 0 ) p_access->info.b_eof = true;
307 else if( i_read > 0 ) p_access->info.i_pos += i_read;
312 /*****************************************************************************
314 *****************************************************************************/
315 static int Control( access_t *p_access, int i_query, va_list args )
322 case ACCESS_CAN_SEEK:
323 case ACCESS_CAN_FASTSEEK:
324 case ACCESS_CAN_PAUSE:
325 case ACCESS_CAN_CONTROL_PACE:
326 pb_bool = (bool*)va_arg( args, bool* );
330 case ACCESS_GET_PTS_DELAY:
331 pi_64 = (int64_t*)va_arg( args, int64_t * );
332 *pi_64 = (int64_t)var_GetInteger( p_access, "smb-caching" ) * 1000;
335 case ACCESS_SET_PAUSE_STATE:
339 case ACCESS_GET_TITLE_INFO:
340 case ACCESS_SET_TITLE:
341 case ACCESS_SET_SEEKPOINT:
342 case ACCESS_SET_PRIVATE_ID_STATE:
343 case ACCESS_GET_CONTENT_TYPE:
347 msg_Warn( p_access, "unimplemented query in control" );
356 static void Win32AddConnection( access_t *p_access, char *psz_path,
357 char *psz_user, char *psz_pwd,
360 DWORD (*OurWNetAddConnection2)( LPNETRESOURCE, LPCTSTR, LPCTSTR, DWORD );
361 char psz_remote[MAX_PATH], psz_server[MAX_PATH], psz_share[MAX_PATH];
362 NETRESOURCE net_resource;
365 VLC_UNUSED( psz_domain );
367 HINSTANCE hdll = LoadLibrary(_T("MPR.DLL"));
370 msg_Warn( p_access, "couldn't load mpr.dll" );
374 OurWNetAddConnection2 =
375 (void *)GetProcAddress( hdll, _T("WNetAddConnection2A") );
376 if( !OurWNetAddConnection2 )
378 msg_Warn( p_access, "couldn't find WNetAddConnection2 in mpr.dll" );
382 memset( &net_resource, 0, sizeof(net_resource) );
383 net_resource.dwType = RESOURCETYPE_DISK;
385 /* Find out server and share names */
386 strlcpy( psz_server, psz_path, sizeof( psz_server ) );
388 psz_parser = strchr( psz_path, '/' );
391 char *psz_parser2 = strchr( ++psz_parser, '/' );
393 strlcpy( psz_share, psz_parser, sizeof( psz_share ) );
396 snprintf( psz_remote, sizeof( psz_remote ), "\\\\%s\\%s", psz_server, psz_share );
397 net_resource.lpRemoteName = psz_remote;
399 i_result = OurWNetAddConnection2( &net_resource, psz_pwd, psz_user, 0 );
401 if( i_result != NO_ERROR )
403 msg_Dbg( p_access, "connected to %s", psz_remote );
405 else if( i_result != ERROR_ALREADY_ASSIGNED &&
406 i_result != ERROR_DEVICE_ALREADY_REMEMBERED )
408 msg_Dbg( p_access, "already connected to %s", psz_remote );
412 msg_Dbg( p_access, "failed to connect to %s", psz_remote );