1 /*****************************************************************************
2 * smb.c: SMB input module
3 *****************************************************************************
4 * Copyright (C) 2001-2004 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 *****************************************************************************/
30 #include <vlc_access.h>
36 # ifdef HAVE_SYS_STAT_H
37 # include <sys/stat.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
47 # include <libsmbclient.h>
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Open ( vlc_object_t * );
57 static void Close( vlc_object_t * );
59 #define CACHING_TEXT N_("Caching value in ms")
60 #define CACHING_LONGTEXT N_( \
61 "Caching value for SMB streams. This " \
62 "value should be set in milliseconds." )
63 #define USER_TEXT N_("SMB user name")
64 #define USER_LONGTEXT N_("User name that will " \
65 "be used for the connection.")
66 #define PASS_TEXT N_("SMB password")
67 #define PASS_LONGTEXT N_("Password that will be " \
68 "used for the connection.")
69 #define DOMAIN_TEXT N_("SMB domain")
70 #define DOMAIN_LONGTEXT N_("Domain/Workgroup that " \
71 "will be used for the connection.")
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,
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 );
91 /*****************************************************************************
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 );
109 static void Win32AddConnection( access_t *, char *, char *, char *, char * );
112 static void smb_auth( const char *srv, const char *shr, char *wg, int wglen,
113 char *un, int unlen, char *pw, int pwlen )
115 //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 = 0, *psz_pwd = 0, *psz_domain = 0;
138 * [[[domain;]user[:password@]]server[/share[/path[/file]]]] */
140 psz_path = strchr( p_access->psz_path, '/' );
143 msg_Err( p_access, "invalid SMB URI: smb://%s", psz_path );
148 char *psz_tmp = strdup( p_access->psz_path );
151 psz_tmp[ psz_path - p_access->psz_path ] = 0;
152 psz_path = p_access->psz_path;
153 psz_parser = strchr( psz_tmp, '@' );
156 /* User info is there */
158 psz_path = p_access->psz_path + (psz_parser - psz_tmp) + 1;
160 psz_parser = strchr( psz_tmp, ':' );
164 psz_pwd = strdup( psz_parser+1 );
168 psz_parser = strchr( psz_tmp, ';' );
172 *psz_parser = 0; psz_parser++;
173 psz_domain = strdup( psz_tmp );
175 else psz_parser = psz_tmp;
177 psz_user = strdup( psz_parser );
184 * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]] */
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; }
195 Win32AddConnection( p_access, psz_path, psz_user, psz_pwd, psz_domain);
196 asprintf( &psz_uri, "//%s", psz_path );
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 );
204 asprintf( &psz_uri, "smb://%s", psz_path );
207 if( psz_user ) free( psz_user );
208 if( psz_pwd ) free( psz_pwd );
209 if( psz_domain ) free( psz_domain );
212 if( !(p_smb = smbc_new_context()) )
214 msg_Err( p_access, "out of memory" );
219 p_smb->callbacks.auth_fn = smb_auth;
221 if( !smbc_init_context( p_smb ) )
223 msg_Err( p_access, "cannot initialize context (%s)", strerror(errno) );
224 smbc_free_context( p_smb, 1 );
229 if( !(p_file = (p_smb->open)( p_smb, psz_uri, O_RDONLY, 0 )) )
231 msg_Err( p_access, "open failed for '%s' (%s)",
232 p_access->psz_path, strerror(errno) );
233 smbc_free_context( p_smb, 1 );
239 STANDARD_READ_ACCESS_INIT;
241 i_ret = p_smb->fstat( p_smb, p_file, &filestat );
242 if( i_ret ) msg_Err( p_access, "stat failed (%s)", strerror(errno) );
243 else p_access->info.i_size = filestat.st_size;
247 if( smbc_init( smb_auth, 1 ) )
255 ** some version of glibc defines open as a macro, causing havoc
256 ** with other macros using 'open' under the hood, such as the
259 #if defined(smbc_open) && defined(open)
262 if( (i_smb = smbc_open( psz_uri, O_RDONLY, 0 )) < 0 )
264 msg_Err( p_access, "open failed for '%s' (%s)",
265 p_access->psz_path, strerror(errno) );
271 STANDARD_READ_ACCESS_INIT;
273 i_ret = smbc_fstat( i_smb, &filestat );
274 if( i_ret ) msg_Err( p_access, "stat failed (%s)", strerror(i_ret) );
275 else p_access->info.i_size = filestat.st_size;
281 p_sys->p_smb = p_smb;
282 p_sys->p_file = p_file;
284 p_sys->i_smb = i_smb;
287 /* Update default_pts to a suitable value for smb access */
288 var_Create( p_access, "smb-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
293 /*****************************************************************************
294 * Close: free unused data structures
295 *****************************************************************************/
296 static void Close( vlc_object_t *p_this )
298 access_t *p_access = (access_t*)p_this;
299 access_sys_t *p_sys = p_access->p_sys;
302 # ifndef HAVE__SMBCCTX_CLOSE_FN
303 p_sys->p_smb->close( p_sys->p_smb, p_sys->p_file );
305 p_sys->p_smb->close_fn( p_sys->p_smb, p_sys->p_file );
307 smbc_free_context( p_sys->p_smb, 1 );
309 smbc_close( p_sys->i_smb );
315 /*****************************************************************************
316 * Seek: try to go at the right place
317 *****************************************************************************/
318 static int Seek( access_t *p_access, int64_t i_pos )
320 access_sys_t *p_sys = p_access->p_sys;
323 if( i_pos < 0 ) return VLC_EGENERIC;
325 msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
328 i_ret = p_sys->p_smb->lseek(p_sys->p_smb, p_sys->p_file, i_pos, SEEK_SET);
330 i_ret = smbc_lseek( p_sys->i_smb, i_pos, SEEK_SET );
334 msg_Err( p_access, "seek failed (%s)", strerror(errno) );
338 p_access->info.b_eof = VLC_FALSE;
339 p_access->info.i_pos = i_ret;
344 /*****************************************************************************
346 *****************************************************************************/
347 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
349 access_sys_t *p_sys = p_access->p_sys;
352 if( p_access->info.b_eof ) return 0;
355 i_read = p_sys->p_smb->read(p_sys->p_smb, p_sys->p_file, p_buffer, i_len);
357 i_read = smbc_read( p_sys->i_smb, p_buffer, i_len );
361 msg_Err( p_access, "read failed (%s)", strerror(errno) );
365 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
366 else if( i_read > 0 ) p_access->info.i_pos += i_read;
371 /*****************************************************************************
373 *****************************************************************************/
374 static int Control( access_t *p_access, int i_query, va_list args )
382 case ACCESS_CAN_SEEK:
383 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
386 case ACCESS_CAN_FASTSEEK:
387 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
390 case ACCESS_CAN_PAUSE:
391 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
394 case ACCESS_CAN_CONTROL_PACE:
395 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
400 pi_int = (int*)va_arg( args, int * );
404 case ACCESS_GET_PTS_DELAY:
405 pi_64 = (int64_t*)va_arg( args, int64_t * );
406 *pi_64 = (int64_t)var_GetInteger( p_access, "smb-caching" ) * 1000;
409 case ACCESS_SET_PAUSE_STATE:
413 case ACCESS_GET_TITLE_INFO:
414 case ACCESS_SET_TITLE:
415 case ACCESS_SET_SEEKPOINT:
416 case ACCESS_SET_PRIVATE_ID_STATE:
420 msg_Warn( p_access, "unimplemented query in control" );
429 static void Win32AddConnection( access_t *p_access, char *psz_path,
430 char *psz_user, char *psz_pwd,
433 DWORD (*OurWNetAddConnection2)( LPNETRESOURCE, LPCTSTR, LPCTSTR, DWORD );
434 char psz_remote[MAX_PATH], psz_server[MAX_PATH], psz_share[MAX_PATH];
435 NETRESOURCE net_resource;
439 HINSTANCE hdll = LoadLibrary(_T("MPR.DLL"));
442 msg_Warn( p_access, "couldn't load mpr.dll" );
446 OurWNetAddConnection2 =
447 (void *)GetProcAddress( hdll, _T("WNetAddConnection2A") );
448 if( !OurWNetAddConnection2 )
450 msg_Warn( p_access, "couldn't find WNetAddConnection2 in mpr.dll" );
454 memset( &net_resource, 0, sizeof(net_resource) );
455 net_resource.dwType = RESOURCETYPE_DISK;
457 /* Find out server and share names */
458 strlcpy( psz_server, psz_path, sizeof( psz_server ) );
460 psz_parser = strchr( psz_path, '/' );
463 char *psz_parser2 = strchr( ++psz_parser, '/' );
465 strlcpy( psz_share, psz_parser, sizeof( psz_share ) );
468 sprintf( psz_remote, "\\\\%s\\%s", psz_server, psz_share );
469 net_resource.lpRemoteName = psz_remote;
471 i_result = OurWNetAddConnection2( &net_resource, psz_pwd, psz_user, 0 );
473 if( i_result != NO_ERROR )
475 msg_Dbg( p_access, "connected to %s", psz_remote );
477 else if( i_result != ERROR_ALREADY_ASSIGNED &&
478 i_result != ERROR_DEVICE_ALREADY_REMEMBERED )
480 msg_Dbg( p_access, "already connected to %s", psz_remote );
484 msg_Dbg( p_access, "failed to connect to %s", psz_remote );