]> git.sesse.net Git - vlc/blob - modules/access/smb.c
Remove stdlib.h
[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 #define _GNU_SOURCE
28
29
30 #include <vlc/vlc.h>
31 #include <vlc_access.h>
32
33 #ifdef WIN32
34 #ifdef HAVE_FCNTL_H
35 #   include <fcntl.h>
36 #endif
37 #   ifdef HAVE_SYS_STAT_H
38 #       include <sys/stat.h>
39 #   endif
40 #   include <io.h>
41 #   define smbc_open(a,b,c) open(a,b,c)
42 #   define stat _stati64
43 #   define smbc_fstat(a,b) _fstati64(a,b)
44 #   define smbc_read read
45 #   define smbc_lseek _lseeki64
46 #   define smbc_close close
47 #else
48 #   include <libsmbclient.h>
49 #   define USE_CTX 1
50 #endif
51
52 #include <errno.h>
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 static int  Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
59
60 #define CACHING_TEXT N_("Caching value in ms")
61 #define CACHING_LONGTEXT N_( \
62     "Caching value for SMB streams. This " \
63     "value should be set in milliseconds." )
64 #define USER_TEXT N_("SMB user name")
65 #define USER_LONGTEXT N_("User name that will " \
66     "be used for the connection.")
67 #define PASS_TEXT N_("SMB password")
68 #define PASS_LONGTEXT N_("Password that will be " \
69     "used for the connection.")
70 #define DOMAIN_TEXT N_("SMB domain")
71 #define DOMAIN_LONGTEXT N_("Domain/Workgroup that " \
72     "will be used for the connection.")
73
74 vlc_module_begin();
75     set_shortname( "SMB" );
76     set_description( _("SMB input") );
77     set_capability( "access2", 0 );
78     set_category( CAT_INPUT );
79     set_subcategory( SUBCAT_INPUT_ACCESS );
80     add_integer( "smb-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
81                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
82     add_string( "smb-user", NULL, NULL, USER_TEXT, USER_LONGTEXT,
83                 VLC_FALSE );
84     add_string( "smb-pwd", NULL, NULL, PASS_TEXT,
85                 PASS_LONGTEXT, VLC_FALSE );
86     add_string( "smb-domain", NULL, NULL, DOMAIN_TEXT,
87                 DOMAIN_LONGTEXT, VLC_FALSE );
88     add_shortcut( "smb" );
89     set_callbacks( Open, Close );
90 vlc_module_end();
91
92 /*****************************************************************************
93  * Local prototypes
94  *****************************************************************************/
95 static int Read( access_t *, uint8_t *, int );
96 static int Seek( access_t *, int64_t );
97 static int Control( access_t *, int, va_list );
98
99 struct access_sys_t
100 {
101 #ifdef USE_CTX
102     SMBCCTX *p_smb;
103     SMBCFILE *p_file;
104 #else
105     int i_smb;
106 #endif
107 };
108
109 #ifdef WIN32
110 static void Win32AddConnection( access_t *, char *, char *, char *, char * );
111 #endif
112
113 void smb_auth( const char *srv, const char *shr, char *wg, int wglen,
114                char *un, int unlen, char *pw, int pwlen )
115 {
116     //wglen = unlen = pwlen = 0;
117 }
118
119 /****************************************************************************
120  * Open: connect to smb server and ask for file
121  ****************************************************************************/
122 static int Open( vlc_object_t *p_this )
123 {
124     access_t     *p_access = (access_t*)p_this;
125     access_sys_t *p_sys;
126     struct stat  filestat;
127     char         *psz_path, *psz_uri;
128     char         *psz_user = 0, *psz_pwd = 0, *psz_domain = 0;
129     int          i_ret;
130
131 #ifdef USE_CTX
132     SMBCCTX      *p_smb;
133     SMBCFILE     *p_file;
134 #else
135     int          i_smb;
136 #endif
137
138     /* Parse input URI
139      * [[[domain;]user[:password@]]server[/share[/path[/file]]]] */
140
141     psz_path = strchr( p_access->psz_path, '/' );
142     if( !psz_path )
143     {
144         msg_Err( p_access, "invalid SMB URI: smb://%s", psz_path );
145         return VLC_EGENERIC;
146     }
147     else
148     {
149         char *psz_tmp = strdup( p_access->psz_path );
150         char *psz_parser;
151
152         psz_tmp[ psz_path - p_access->psz_path ] = 0;
153         psz_path = p_access->psz_path;
154         psz_parser = strchr( psz_tmp, '@' );
155         if( psz_parser )
156         {
157             /* User info is there */
158             *psz_parser = 0;
159             psz_path = p_access->psz_path + (psz_parser - psz_tmp) + 1;
160
161             psz_parser = strchr( psz_tmp, ':' );
162             if( psz_parser )
163             {
164                 /* Password found */
165                 psz_pwd = strdup( psz_parser+1 );
166                 *psz_parser = 0;
167             }
168
169             psz_parser = strchr( psz_tmp, ';' );
170             if( psz_parser )
171             {
172                 /* Domain found */
173                 *psz_parser = 0; psz_parser++;
174                 psz_domain = strdup( psz_tmp );
175             }
176             else psz_parser = psz_tmp;
177
178             psz_user = strdup( psz_parser );
179         }
180
181         free( psz_tmp );
182     }
183
184     /* Build an SMB URI
185      * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]] */
186
187     if( !psz_user ) psz_user = var_CreateGetString( p_access, "smb-user" );
188     if( psz_user && !*psz_user ) { free( psz_user ); psz_user = 0; }
189     if( !psz_pwd ) psz_pwd = var_CreateGetString( p_access, "smb-pwd" );
190     if( psz_pwd && !*psz_pwd ) { free( psz_pwd ); psz_pwd = 0; }
191     if(!psz_domain) psz_domain = var_CreateGetString( p_access, "smb-domain" );
192     if( psz_domain && !*psz_domain ) { free( psz_domain ); psz_domain = 0; }
193
194 #ifdef WIN32
195     if( psz_user )
196         Win32AddConnection( p_access, psz_path, psz_user, psz_pwd, psz_domain);
197     asprintf( &psz_uri, "//%s", psz_path );
198 #else
199     if( psz_user )
200         asprintf( &psz_uri, "smb://%s%s%s%s%s@%s",
201                   psz_domain ? psz_domain : "", psz_domain ? ";" : "",
202                   psz_user, psz_pwd ? ":" : "",
203                   psz_pwd ? psz_pwd : "", psz_path );
204     else
205         asprintf( &psz_uri, "smb://%s", psz_path );
206 #endif
207
208     if( psz_user ) free( psz_user );
209     if( psz_pwd ) free( psz_pwd );
210     if( psz_domain ) free( psz_domain );
211
212 #ifdef USE_CTX
213     if( !(p_smb = smbc_new_context()) )
214     {
215         msg_Err( p_access, "out of memory" );
216         free( psz_uri );
217         return VLC_ENOMEM;
218     }
219     p_smb->debug = 1;
220     p_smb->callbacks.auth_fn = smb_auth;
221
222     if( !smbc_init_context( p_smb ) )
223     {
224         msg_Err( p_access, "cannot initialize context (%s)", strerror(errno) );
225         smbc_free_context( p_smb, 1 );
226         free( psz_uri );
227         return VLC_EGENERIC;
228     }
229
230     if( !(p_file = p_smb->open( p_smb, psz_uri, O_RDONLY, 0 )) )
231     {
232         msg_Err( p_access, "open failed for '%s' (%s)",
233                  p_access->psz_path, strerror(errno) );
234         smbc_free_context( p_smb, 1 );
235         free( psz_uri );
236         return VLC_EGENERIC;
237     }
238
239     /* Init p_access */
240     STANDARD_READ_ACCESS_INIT;
241
242     i_ret = p_smb->fstat( p_smb, p_file, &filestat );
243     if( i_ret ) msg_Err( p_access, "stat failed (%s)", strerror(errno) );
244     else p_access->info.i_size = filestat.st_size;
245 #else
246
247 #ifndef WIN32
248     if( smbc_init( smb_auth, 1 ) )
249     {
250         free( psz_uri );
251         return VLC_EGENERIC;
252     }
253 #endif
254
255     if( (i_smb = smbc_open( psz_uri, O_RDONLY, 0 )) < 0 )
256     {
257         msg_Err( p_access, "open failed for '%s' (%s)",
258                  p_access->psz_path, strerror(errno) );
259         free( psz_uri );
260         return VLC_EGENERIC;
261     }
262
263     /* Init p_access */
264     STANDARD_READ_ACCESS_INIT;
265
266     i_ret = smbc_fstat( i_smb, &filestat );
267     if( i_ret ) msg_Err( p_access, "stat failed (%s)", strerror(i_ret) );
268     else p_access->info.i_size = filestat.st_size;
269 #endif
270
271     free( psz_uri );
272
273 #ifdef USE_CTX
274     p_sys->p_smb = p_smb;
275     p_sys->p_file = p_file;
276 #else
277     p_sys->i_smb = i_smb;
278 #endif
279
280     /* Update default_pts to a suitable value for smb access */
281     var_Create( p_access, "smb-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
282
283     return VLC_SUCCESS;
284 }
285
286 /*****************************************************************************
287  * Close: free unused data structures
288  *****************************************************************************/
289 static void Close( vlc_object_t *p_this )
290 {
291     access_t     *p_access = (access_t*)p_this;
292     access_sys_t *p_sys = p_access->p_sys;
293
294 #ifdef USE_CTX
295 #  ifndef HAVE__SMBCCTX_CLOSE_FN
296     p_sys->p_smb->close( p_sys->p_smb, p_sys->p_file );
297 #  else
298     p_sys->p_smb->close_fn( p_sys->p_smb, p_sys->p_file );
299 #  endif
300     smbc_free_context( p_sys->p_smb, 1 );
301 #else
302     smbc_close( p_sys->i_smb );
303 #endif
304
305     free( p_sys );
306 }
307
308 /*****************************************************************************
309  * Seek: try to go at the right place
310  *****************************************************************************/
311 static int Seek( access_t *p_access, int64_t i_pos )
312 {
313     access_sys_t *p_sys = p_access->p_sys;
314     int64_t      i_ret;
315
316     if( i_pos < 0 ) return VLC_EGENERIC;
317
318     msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
319
320 #ifdef USE_CTX
321     i_ret = p_sys->p_smb->lseek(p_sys->p_smb, p_sys->p_file, i_pos, SEEK_SET);
322 #else
323     i_ret = smbc_lseek( p_sys->i_smb, i_pos, SEEK_SET );
324 #endif
325     if( i_ret == -1 )
326     {
327         msg_Err( p_access, "seek failed (%s)", strerror(errno) );
328         return VLC_EGENERIC;
329     }
330
331     p_access->info.b_eof = VLC_FALSE;
332     p_access->info.i_pos = i_ret;
333
334     return VLC_SUCCESS;
335 }
336
337 /*****************************************************************************
338  * Read:
339  *****************************************************************************/
340 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
341 {
342     access_sys_t *p_sys = p_access->p_sys;
343     int i_read;
344
345     if( p_access->info.b_eof ) return 0;
346
347 #ifdef USE_CTX
348     i_read = p_sys->p_smb->read(p_sys->p_smb, p_sys->p_file, p_buffer, i_len);
349 #else
350     i_read = smbc_read( p_sys->i_smb, p_buffer, i_len );
351 #endif
352     if( i_read < 0 )
353     {
354         msg_Err( p_access, "read failed (%s)", strerror(errno) );
355         return -1;
356     }
357
358     if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
359     else if( i_read > 0 ) p_access->info.i_pos += i_read;
360
361     return i_read;
362 }
363
364 /*****************************************************************************
365  * Control:
366  *****************************************************************************/
367 static int Control( access_t *p_access, int i_query, va_list args )
368 {
369     vlc_bool_t   *pb_bool;
370     int          *pi_int;
371     int64_t      *pi_64;
372
373     switch( i_query )
374     {
375     case ACCESS_CAN_SEEK:
376         pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
377         *pb_bool = VLC_TRUE;
378         break;
379     case ACCESS_CAN_FASTSEEK:
380         pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
381         *pb_bool = VLC_TRUE;
382         break;
383     case ACCESS_CAN_PAUSE:
384         pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
385         *pb_bool = VLC_TRUE;
386         break;
387     case ACCESS_CAN_CONTROL_PACE:
388         pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
389         *pb_bool = VLC_TRUE;
390         break;
391
392     case ACCESS_GET_MTU:
393         pi_int = (int*)va_arg( args, int * );
394         *pi_int = 0;
395         break;
396
397     case ACCESS_GET_PTS_DELAY:
398         pi_64 = (int64_t*)va_arg( args, int64_t * );
399         *pi_64 = (int64_t)var_GetInteger( p_access, "smb-caching" ) * 1000;
400         break;
401
402     case ACCESS_SET_PAUSE_STATE:
403         /* Nothing to do */
404         break;
405
406     case ACCESS_GET_TITLE_INFO:
407     case ACCESS_SET_TITLE:
408     case ACCESS_SET_SEEKPOINT:
409     case ACCESS_SET_PRIVATE_ID_STATE:
410         return VLC_EGENERIC;
411
412     default:
413         msg_Warn( p_access, "unimplemented query in control" );
414         return VLC_EGENERIC;
415
416     }
417
418     return VLC_SUCCESS;
419 }
420
421 #ifdef WIN32
422 static void Win32AddConnection( access_t *p_access, char *psz_path,
423                                 char *psz_user, char *psz_pwd,
424                                 char *psz_domain )
425 {
426     DWORD (*OurWNetAddConnection2)( LPNETRESOURCE, LPCTSTR, LPCTSTR, DWORD );
427     char psz_remote[MAX_PATH], psz_server[MAX_PATH], psz_share[MAX_PATH];
428     NETRESOURCE net_resource;
429     DWORD i_result;
430     char *psz_parser;
431
432     HINSTANCE hdll = LoadLibrary(_T("MPR.DLL"));
433     if( !hdll )
434     {
435         msg_Warn( p_access, "couldn't load mpr.dll" );
436         return;
437     }
438
439     OurWNetAddConnection2 =
440       (void *)GetProcAddress( hdll, _T("WNetAddConnection2A") );
441     if( !OurWNetAddConnection2 )
442     {
443         msg_Warn( p_access, "couldn't find WNetAddConnection2 in mpr.dll" );
444         return;
445     }
446
447     memset( &net_resource, 0, sizeof(net_resource) );
448     net_resource.dwType = RESOURCETYPE_DISK;
449
450     /* Find out server and share names */
451     strlcpy( psz_server, psz_path, sizeof( psz_server ) );
452     psz_share[0] = 0;
453     psz_parser = strchr( psz_path, '/' );
454     if( psz_parser )
455     {
456         char *psz_parser2 = strchr( ++psz_parser, '/' );
457         if( psz_parser2 )
458             strlcpy( psz_share, psz_parser, sizeof( psz_share ) );
459    }
460
461     sprintf( psz_remote, "\\\\%s\\%s", psz_server, psz_share );
462     net_resource.lpRemoteName = psz_remote;
463
464     i_result = OurWNetAddConnection2( &net_resource, psz_pwd, psz_user, 0 );
465
466     if( i_result != NO_ERROR )
467     {
468         msg_Dbg( p_access, "connected to %s", psz_remote );
469     }
470     else if( i_result != ERROR_ALREADY_ASSIGNED && 
471              i_result != ERROR_DEVICE_ALREADY_REMEMBERED )
472     {
473         msg_Dbg( p_access, "already connected to %s", psz_remote );
474     }
475     else
476     {
477         msg_Dbg( p_access, "failed to connect to %s", psz_remote );
478     }
479
480     FreeLibrary( hdll );
481 }
482 #endif // WIN32