1 /*****************************************************************************
2 * smb.c: SMB input module
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.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>
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Open ( vlc_object_t * );
55 static void Close( vlc_object_t * );
57 #define CACHING_TEXT N_("Caching value in ms")
58 #define CACHING_LONGTEXT N_( \
59 "Allows you to modify the default caching value for SMB streams. This " \
60 "value should be set in millisecond units." )
61 #define USER_TEXT N_("SMB user name")
62 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
63 "be used for the connection.")
64 #define PASS_TEXT N_("SMB password")
65 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
66 "used for the connection.")
67 #define DOMAIN_TEXT N_("SMB domain")
68 #define DOMAIN_LONGTEXT N_("Allows you to modify the domain/workgroup that " \
69 "will be used for the connection.")
72 set_shortname( "SMB" );
73 set_description( _("SMB input") );
74 set_capability( "access2", 0 );
75 set_category( CAT_INPUT );
76 set_subcategory( SUBCAT_INPUT_ACCESS );
77 add_integer( "smb-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
78 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
79 add_string( "smb-user", NULL, NULL, USER_TEXT, USER_LONGTEXT,
81 add_string( "smb-pwd", NULL, NULL, PASS_TEXT,
82 PASS_LONGTEXT, VLC_FALSE );
83 add_string( "smb-domain", NULL, NULL, DOMAIN_TEXT,
84 DOMAIN_LONGTEXT, VLC_FALSE );
85 add_shortcut( "smb" );
86 set_callbacks( Open, Close );
89 /*****************************************************************************
91 *****************************************************************************/
92 static int Read( access_t *, uint8_t *, int );
93 static int Seek( access_t *, int64_t );
94 static int Control( access_t *, int, va_list );
107 static void Win32AddConnection( access_t *, char *, char *, char *, char * );
110 void smb_auth( const char *srv, const char *shr, char *wg, int wglen,
111 char *un, int unlen, char *pw, int pwlen )
113 //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 = 0, *psz_pwd = 0, *psz_domain = 0;
136 * [[[domain;]user[:password@]]server[/share[/path[/file]]]] */
138 psz_path = strchr( p_access->psz_path, '/' );
141 msg_Err( p_access, "invalid SMB URI: smb://%s", psz_path );
146 char *psz_tmp = strdup( p_access->psz_path );
149 psz_tmp[ psz_path - p_access->psz_path ] = 0;
150 psz_path = p_access->psz_path;
151 psz_parser = strchr( psz_tmp, '@' );
154 /* User info is there */
156 psz_path = p_access->psz_path + (psz_parser - psz_tmp) + 1;
158 psz_parser = strchr( psz_tmp, ':' );
162 psz_pwd = strdup( psz_parser+1 );
166 psz_parser = strchr( psz_tmp, ';' );
170 *psz_parser = 0; psz_parser++;
171 psz_domain = strdup( psz_tmp );
173 else psz_parser = psz_tmp;
175 psz_user = strdup( psz_parser );
182 * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]] */
184 if( !psz_user ) psz_user = var_CreateGetString( p_access, "smb-user" );
185 if( psz_user && !*psz_user ) { free( psz_user ); psz_user = 0; }
186 if( !psz_pwd ) psz_pwd = var_CreateGetString( p_access, "smb-pwd" );
187 if( psz_pwd && !*psz_pwd ) { free( psz_pwd ); psz_pwd = 0; }
188 if(!psz_domain) psz_domain = var_CreateGetString( p_access, "smb-domain" );
189 if( psz_domain && !*psz_domain ) { free( psz_domain ); psz_domain = 0; }
193 Win32AddConnection( p_access, psz_path, psz_user, psz_pwd, psz_domain);
194 asprintf( &psz_uri, "//%s", psz_path );
197 asprintf( &psz_uri, "smb://%s%s%s%s%s@%s",
198 psz_domain ? psz_domain : "", psz_domain ? ";" : "",
199 psz_user, psz_pwd ? ":" : "",
200 psz_pwd ? psz_pwd : "", psz_path );
202 asprintf( &psz_uri, "smb://%s", psz_path );
205 if( psz_user ) free( psz_user );
206 if( psz_pwd ) free( psz_pwd );
207 if( psz_domain ) free( psz_domain );
210 if( !(p_smb = smbc_new_context()) )
212 msg_Err( p_access, "out of memory" );
217 p_smb->callbacks.auth_fn = smb_auth;
219 if( !smbc_init_context( p_smb ) )
221 msg_Err( p_access, "cannot initialize context (%s)", strerror(errno) );
222 smbc_free_context( p_smb, 1 );
227 if( !(p_file = p_smb->open( p_smb, psz_uri, O_RDONLY, 0 )) )
229 msg_Err( p_access, "open failed for '%s' (%s)",
230 p_access->psz_path, strerror(errno) );
231 smbc_free_context( p_smb, 1 );
236 p_access->info.i_size = 0;
237 i_ret = p_smb->fstat( p_smb, p_file, &filestat );
238 if( i_ret ) msg_Err( p_access, "stat failed (%s)", strerror(errno) );
239 else p_access->info.i_size = filestat.st_size;
244 if( smbc_init( smb_auth, 1 ) )
251 if( (i_smb = smbc_open( psz_uri, O_RDONLY, 0 )) < 0 )
253 msg_Err( p_access, "open failed for '%s' (%s)",
254 p_access->psz_path, strerror(errno) );
259 p_access->info.i_size = 0;
260 i_ret = smbc_fstat( i_smb, &filestat );
261 if( i_ret ) msg_Err( p_access, "stat failed (%s)", strerror(i_ret) );
262 else p_access->info.i_size = filestat.st_size;
268 p_access->pf_read = Read;
269 p_access->pf_block = NULL;
270 p_access->pf_seek = Seek;
271 p_access->pf_control = Control;
272 p_access->info.i_update = 0;
273 p_access->info.i_pos = 0;
274 p_access->info.b_eof = VLC_FALSE;
275 p_access->info.i_title = 0;
276 p_access->info.i_seekpoint = 0;
277 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
278 memset( p_sys, 0, sizeof( access_sys_t ) );
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 p_sys->p_smb->close( p_sys->p_smb, p_sys->p_file );
303 smbc_free_context( p_sys->p_smb, 1 );
305 smbc_close( p_sys->i_smb );
311 /*****************************************************************************
312 * Seek: try to go at the right place
313 *****************************************************************************/
314 static int Seek( access_t *p_access, int64_t i_pos )
316 access_sys_t *p_sys = p_access->p_sys;
319 if( i_pos < 0 ) return VLC_EGENERIC;
321 msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
324 i_ret = p_sys->p_smb->lseek(p_sys->p_smb, p_sys->p_file, i_pos, SEEK_SET);
326 i_ret = smbc_lseek( p_sys->i_smb, i_pos, SEEK_SET );
330 msg_Err( p_access, "seek failed (%s)", strerror(errno) );
334 p_access->info.b_eof = VLC_FALSE;
335 p_access->info.i_pos = i_ret;
340 /*****************************************************************************
342 *****************************************************************************/
343 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
345 access_sys_t *p_sys = p_access->p_sys;
348 if( p_access->info.b_eof ) return 0;
351 i_read = p_sys->p_smb->read(p_sys->p_smb, p_sys->p_file, p_buffer, i_len);
353 i_read = smbc_read( p_sys->i_smb, p_buffer, i_len );
357 msg_Err( p_access, "read failed (%s)", strerror(errno) );
361 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
362 else if( i_read > 0 ) p_access->info.i_pos += i_read;
367 /*****************************************************************************
369 *****************************************************************************/
370 static int Control( access_t *p_access, int i_query, va_list args )
378 case ACCESS_CAN_SEEK:
379 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
382 case ACCESS_CAN_FASTSEEK:
383 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
386 case ACCESS_CAN_PAUSE:
387 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
390 case ACCESS_CAN_CONTROL_PACE:
391 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
396 pi_int = (int*)va_arg( args, int * );
400 case ACCESS_GET_PTS_DELAY:
401 pi_64 = (int64_t*)va_arg( args, int64_t * );
402 *pi_64 = (int64_t)var_GetInteger( p_access, "smb-caching" ) * 1000;
405 case ACCESS_SET_PAUSE_STATE:
409 case ACCESS_GET_TITLE_INFO:
410 case ACCESS_SET_TITLE:
411 case ACCESS_SET_SEEKPOINT:
412 case ACCESS_SET_PRIVATE_ID_STATE:
416 msg_Warn( p_access, "unimplemented query in control" );
425 static void Win32AddConnection( access_t *p_access, char *psz_path,
426 char *psz_user, char *psz_pwd,
429 DWORD (*OurWNetAddConnection2)( LPNETRESOURCE, LPCTSTR, LPCTSTR, DWORD );
430 char psz_remote[MAX_PATH], psz_server[MAX_PATH], psz_share[MAX_PATH];
431 NETRESOURCE net_resource;
435 HINSTANCE hdll = LoadLibrary(_T("MPR.DLL"));
438 msg_Warn( p_access, "couldn't load mpr.dll" );
442 OurWNetAddConnection2 =
443 (void *)GetProcAddress( hdll, _T("WNetAddConnection2A") );
444 if( !OurWNetAddConnection2 )
446 msg_Warn( p_access, "couldn't find WNetAddConnection2 in mpr.dll" );
450 memset( &net_resource, 0, sizeof(net_resource) );
451 net_resource.dwType = RESOURCETYPE_DISK;
453 /* Find out server and share names */
454 psz_server[0] = psz_share[0] = 0;
455 psz_parser = strchr( psz_path, '/' );
459 strncat( psz_server, psz_path, psz_parser - psz_path );
460 psz_parser2 = strchr( psz_parser+1, '/' );
462 strncat( psz_share, psz_parser+1, psz_parser2 - psz_parser -1 );
464 else strncat( psz_server, psz_path, MAX_PATH );
466 sprintf( psz_remote, "\\\\%s\\%s", psz_server, psz_share );
467 net_resource.lpRemoteName = psz_remote;
469 i_result = OurWNetAddConnection2( &net_resource, psz_pwd, psz_user, 0 );
471 if( i_result != NO_ERROR )
473 msg_Dbg( p_access, "connected to %s", psz_remote );
475 else if( i_result != ERROR_ALREADY_ASSIGNED &&
476 i_result != ERROR_DEVICE_ALREADY_REMEMBERED )
478 msg_Dbg( p_access, "already connected to %s", psz_remote );
482 msg_Dbg( p_access, "failed to connect to %s", psz_remote );