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