]> git.sesse.net Git - vlc/blob - modules/access/smb.c
Add a bunch of help strings. Feel free to correct them, and add more
[vlc] / modules / access / smb.c
1 /*****************************************************************************
2  * smb.c: SMB input module
3  *****************************************************************************
4  * Copyright (C) 2001-2009 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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_charset.h>
33 #include <vlc_plugin.h>
34 #include <vlc_access.h>
35
36 #ifdef WIN32
37 #   ifdef HAVE_FCNTL_H
38 #       include <fcntl.h>
39 #   endif
40 #   ifdef HAVE_SYS_STAT_H
41 #       include <sys/stat.h>
42 #   endif
43 #   include <io.h>
44 #   define smbc_open(a,b,c) utf8_open(a,b,c)
45 #   define smbc_fstat(a,b) _fstati64(a,b)
46 #   define smbc_read read
47 #   define smbc_lseek _lseeki64
48 #   define smbc_close close
49 #else
50 #   include <libsmbclient.h>
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 #define SMB_HELP N_("Samba (Windows network shares) input")
76 vlc_module_begin ()
77     set_shortname( "SMB" )
78     set_description( N_("SMB input") )
79     set_help(SMB_HELP)
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 )
85         change_safe()
86     add_string( "smb-user", NULL, NULL, USER_TEXT, USER_LONGTEXT,
87                 false )
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 )
92     add_shortcut( "smb" )
93     set_callbacks( Open, Close )
94 vlc_module_end ()
95
96 /*****************************************************************************
97  * Local prototypes
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 );
102
103 struct access_sys_t
104 {
105     int i_smb;
106 };
107
108 #ifdef WIN32
109 static void Win32AddConnection( access_t *, char *, char *, char *, char * );
110 #else
111 static void smb_auth( const char *srv, const char *shr, char *wg, int wglen,
112                       char *un, int unlen, char *pw, int pwlen )
113 {
114     VLC_UNUSED(srv);
115     VLC_UNUSED(shr);
116     VLC_UNUSED(wg);
117     VLC_UNUSED(wglen);
118     VLC_UNUSED(un);
119     VLC_UNUSED(unlen);
120     VLC_UNUSED(pw);
121     VLC_UNUSED(pwlen);
122     //wglen = unlen = pwlen = 0;
123 }
124 #endif
125
126 /****************************************************************************
127  * Open: connect to smb server and ask for file
128  ****************************************************************************/
129 static int Open( vlc_object_t *p_this )
130 {
131     access_t     *p_access = (access_t*)p_this;
132     access_sys_t *p_sys;
133     struct stat  filestat;
134     char         *psz_path, *psz_uri;
135     char         *psz_user = NULL, *psz_pwd = NULL, *psz_domain = NULL;
136     int          i_ret;
137     int          i_smb;
138
139     /* Parse input URI
140      * [[[domain;]user[:password@]]server[/share[/path[/file]]]] */
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 = NULL; }
189     if( !psz_pwd ) psz_pwd = var_CreateGetString( p_access, "smb-pwd" );
190     if( psz_pwd && !*psz_pwd ) { free( psz_pwd ); psz_pwd = NULL; }
191     if( !psz_domain ) psz_domain = var_CreateGetString( p_access, "smb-domain" );
192     if( psz_domain && !*psz_domain ) { free( psz_domain ); psz_domain = NULL; }
193
194 #ifdef WIN32
195     if( psz_user )
196         Win32AddConnection( p_access, psz_path, psz_user, psz_pwd, psz_domain);
197     i_ret = asprintf( &psz_uri, "//%s", psz_path );
198 #else
199     if( psz_user )
200         i_ret = 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         i_ret = asprintf( &psz_uri, "smb://%s", psz_path );
206 #endif
207
208     free( psz_user );
209     free( psz_pwd );
210     free( psz_domain );
211
212     if( i_ret == -1 )
213         return VLC_ENOMEM;
214
215 #ifndef WIN32
216     if( smbc_init( smb_auth, 0 ) )
217     {
218         free( psz_uri );
219         return VLC_EGENERIC;
220     }
221 #endif
222
223 /*
224 ** some version of glibc defines open as a macro, causing havoc
225 ** with other macros using 'open' under the hood, such as the
226 ** following one:
227 */
228 #if defined(smbc_open) && defined(open)
229 # undef open
230 #endif
231     if( (i_smb = smbc_open( psz_uri, O_RDONLY, 0 )) < 0 )
232     {
233         msg_Err( p_access, "open failed for '%s' (%m)", p_access->psz_path );
234         free( psz_uri );
235         return VLC_EGENERIC;
236     }
237
238     /* Init p_access */
239     STANDARD_READ_ACCESS_INIT;
240
241     i_ret = smbc_fstat( i_smb, &filestat );
242     if( i_ret )
243     {
244         errno = i_ret;
245         msg_Err( p_access, "stat failed (%m)" );
246     }
247     else
248         p_access->info.i_size = filestat.st_size;
249
250     free( psz_uri );
251
252     p_sys->i_smb = i_smb;
253
254     /* Update default_pts to a suitable value for smb access */
255     var_Create( p_access, "smb-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
256
257     return VLC_SUCCESS;
258 }
259
260 /*****************************************************************************
261  * Close: free unused data structures
262  *****************************************************************************/
263 static void Close( vlc_object_t *p_this )
264 {
265     access_t     *p_access = (access_t*)p_this;
266     access_sys_t *p_sys = p_access->p_sys;
267
268     smbc_close( p_sys->i_smb );
269     free( p_sys );
270 }
271
272 /*****************************************************************************
273  * Seek: try to go at the right place
274  *****************************************************************************/
275 static int Seek( access_t *p_access, int64_t i_pos )
276 {
277     access_sys_t *p_sys = p_access->p_sys;
278     int64_t      i_ret;
279
280     if( i_pos < 0 ) return VLC_EGENERIC;
281
282     msg_Dbg( p_access, "seeking to %"PRId64, i_pos );
283
284     i_ret = smbc_lseek( p_sys->i_smb, i_pos, SEEK_SET );
285     if( i_ret == -1 )
286     {
287         msg_Err( p_access, "seek failed (%m)" );
288         return VLC_EGENERIC;
289     }
290
291     p_access->info.b_eof = false;
292     p_access->info.i_pos = i_ret;
293
294     return VLC_SUCCESS;
295 }
296
297 /*****************************************************************************
298  * Read:
299  *****************************************************************************/
300 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
301 {
302     access_sys_t *p_sys = p_access->p_sys;
303     int i_read;
304
305     if( p_access->info.b_eof ) return 0;
306
307     i_read = smbc_read( p_sys->i_smb, p_buffer, i_len );
308     if( i_read < 0 )
309     {
310         msg_Err( p_access, "read failed (%m)" );
311         return -1;
312     }
313
314     if( i_read == 0 ) p_access->info.b_eof = true;
315     else if( i_read > 0 ) p_access->info.i_pos += i_read;
316
317     return i_read;
318 }
319
320 /*****************************************************************************
321  * Control:
322  *****************************************************************************/
323 static int Control( access_t *p_access, int i_query, va_list args )
324 {
325     switch( i_query )
326     {
327     case ACCESS_CAN_SEEK:
328     case ACCESS_CAN_FASTSEEK:
329     case ACCESS_CAN_PAUSE:
330     case ACCESS_CAN_CONTROL_PACE:
331         *va_arg( args, bool* ) = true;
332         break;
333
334     case ACCESS_GET_PTS_DELAY:
335         *va_arg( args, int64_t * )
336                   = (int64_t)var_GetInteger( p_access, "smb-caching" ) * 1000;
337         break;
338
339     case ACCESS_SET_PAUSE_STATE:
340         /* Nothing to do */
341         break;
342
343     case ACCESS_GET_TITLE_INFO:
344     case ACCESS_SET_TITLE:
345     case ACCESS_SET_SEEKPOINT:
346     case ACCESS_SET_PRIVATE_ID_STATE:
347     case ACCESS_GET_CONTENT_TYPE:
348         return VLC_EGENERIC;
349
350     default:
351         msg_Warn( p_access, "unimplemented query in control" );
352         return VLC_EGENERIC;
353
354     }
355
356     return VLC_SUCCESS;
357 }
358
359 #ifdef WIN32
360 static void Win32AddConnection( access_t *p_access, char *psz_path,
361                                 char *psz_user, char *psz_pwd,
362                                 char *psz_domain )
363 {
364     DWORD (*OurWNetAddConnection2)( LPNETRESOURCE, LPCTSTR, LPCTSTR, DWORD );
365     char psz_remote[MAX_PATH], psz_server[MAX_PATH], psz_share[MAX_PATH];
366     NETRESOURCE net_resource;
367     DWORD i_result;
368     char *psz_parser;
369     VLC_UNUSED( psz_domain );
370
371     HINSTANCE hdll = LoadLibrary(_T("MPR.DLL"));
372     if( !hdll )
373     {
374         msg_Warn( p_access, "couldn't load mpr.dll" );
375         return;
376     }
377
378     OurWNetAddConnection2 =
379       (void *)GetProcAddress( hdll, _T("WNetAddConnection2A") );
380     if( !OurWNetAddConnection2 )
381     {
382         msg_Warn( p_access, "couldn't find WNetAddConnection2 in mpr.dll" );
383         return;
384     }
385
386     memset( &net_resource, 0, sizeof(net_resource) );
387     net_resource.dwType = RESOURCETYPE_DISK;
388
389     /* Find out server and share names */
390     strlcpy( psz_server, psz_path, sizeof( psz_server ) );
391     psz_share[0] = 0;
392     psz_parser = strchr( psz_path, '/' );
393     if( psz_parser )
394     {
395         char *psz_parser2 = strchr( ++psz_parser, '/' );
396         if( psz_parser2 )
397             strlcpy( psz_share, psz_parser, sizeof( psz_share ) );
398    }
399
400     snprintf( psz_remote, sizeof( psz_remote ), "\\\\%s\\%s", psz_server, psz_share );
401     net_resource.lpRemoteName = psz_remote;
402
403     i_result = OurWNetAddConnection2( &net_resource, psz_pwd, psz_user, 0 );
404
405     if( i_result != NO_ERROR )
406     {
407         msg_Dbg( p_access, "connected to %s", psz_remote );
408     }
409     else if( i_result != ERROR_ALREADY_ASSIGNED &&
410              i_result != ERROR_DEVICE_ALREADY_REMEMBERED )
411     {
412         msg_Dbg( p_access, "already connected to %s", psz_remote );
413     }
414     else
415     {
416         msg_Dbg( p_access, "failed to connect to %s", psz_remote );
417     }
418
419     FreeLibrary( hdll );
420 }
421 #endif // WIN32