]> git.sesse.net Git - vlc/blob - modules/access/smb.c
http: do not bother with login dialog if there is no realm
[vlc] / modules / access / smb.c
1 /*****************************************************************************
2  * smb.c: SMB input module
3  *****************************************************************************
4  * Copyright (C) 2001-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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 <errno.h>
32 #ifdef WIN32
33 #   include <fcntl.h>
34 #   include <sys/stat.h>
35 #   include <io.h>
36 #   define smbc_open(a,b,c) vlc_open(a,b,c)
37 #   define smbc_fstat(a,b) _fstati64(a,b)
38 #   define smbc_read read
39 #   define smbc_lseek _lseeki64
40 #   define smbc_close close
41 #else
42 #   include <libsmbclient.h>
43 #endif
44
45 #include <vlc_common.h>
46 #include <vlc_fs.h>
47 #include <vlc_plugin.h>
48 #include <vlc_access.h>
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 static int  Open ( vlc_object_t * );
54 static void Close( vlc_object_t * );
55
56 #define USER_TEXT N_("SMB user name")
57 #define USER_LONGTEXT N_("User name that will " \
58     "be used for the connection.")
59 #define PASS_TEXT N_("SMB password")
60 #define PASS_LONGTEXT N_("Password that will be " \
61     "used for the connection.")
62 #define DOMAIN_TEXT N_("SMB domain")
63 #define DOMAIN_LONGTEXT N_("Domain/Workgroup that " \
64     "will be used for the connection.")
65
66 #define SMB_HELP N_("Samba (Windows network shares) input")
67 vlc_module_begin ()
68     set_shortname( "SMB" )
69     set_description( N_("SMB input") )
70     set_help(SMB_HELP)
71     set_capability( "access", 0 )
72     set_category( CAT_INPUT )
73     set_subcategory( SUBCAT_INPUT_ACCESS )
74     add_string( "smb-user", NULL, USER_TEXT, USER_LONGTEXT,
75                 false )
76     add_password( "smb-pwd", NULL, PASS_TEXT,
77                   PASS_LONGTEXT, false )
78     add_string( "smb-domain", NULL, DOMAIN_TEXT,
79                 DOMAIN_LONGTEXT, false )
80     add_shortcut( "smb" )
81     set_callbacks( Open, Close )
82 vlc_module_end ()
83
84 /*****************************************************************************
85  * Local prototypes
86  *****************************************************************************/
87 static ssize_t Read( access_t *, uint8_t *, size_t );
88 static int Seek( access_t *, uint64_t );
89 static int Control( access_t *, int, va_list );
90
91 struct access_sys_t
92 {
93     int i_smb;
94 };
95
96 #ifdef WIN32
97 static void Win32AddConnection( access_t *, char *, char *, char *, char * );
98 #else
99 static void smb_auth( const char *srv, const char *shr, char *wg, int wglen,
100                       char *un, int unlen, char *pw, int pwlen )
101 {
102     VLC_UNUSED(srv);
103     VLC_UNUSED(shr);
104     VLC_UNUSED(wg);
105     VLC_UNUSED(wglen);
106     VLC_UNUSED(un);
107     VLC_UNUSED(unlen);
108     VLC_UNUSED(pw);
109     VLC_UNUSED(pwlen);
110     //wglen = unlen = pwlen = 0;
111 }
112 #endif
113
114 /****************************************************************************
115  * Open: connect to smb server and ask for file
116  ****************************************************************************/
117 static int Open( vlc_object_t *p_this )
118 {
119     access_t     *p_access = (access_t*)p_this;
120     access_sys_t *p_sys;
121     struct stat  filestat;
122     char         *psz_location, *psz_uri;
123     char         *psz_user = NULL, *psz_pwd = NULL, *psz_domain = NULL;
124     int          i_ret;
125     int          i_smb;
126
127     /* Parse input URI
128      * [[[domain;]user[:password@]]server[/share[/path[/file]]]] */
129     psz_location = strchr( p_access->psz_location, '/' );
130     if( !psz_location )
131     {
132         msg_Err( p_access, "invalid SMB URI: smb://%s", psz_location );
133         return VLC_EGENERIC;
134     }
135     else
136     {
137         char *psz_tmp = strdup( p_access->psz_location );
138         char *psz_parser;
139
140         psz_tmp[ psz_location - p_access->psz_location ] = 0;
141         psz_location = p_access->psz_location;
142         psz_parser = strchr( psz_tmp, '@' );
143         if( psz_parser )
144         {
145             /* User info is there */
146             *psz_parser = 0;
147             psz_location = p_access->psz_location + (psz_parser - psz_tmp) + 1;
148
149             psz_parser = strchr( psz_tmp, ':' );
150             if( psz_parser )
151             {
152                 /* Password found */
153                 psz_pwd = strdup( psz_parser+1 );
154                 *psz_parser = 0;
155             }
156
157             psz_parser = strchr( psz_tmp, ';' );
158             if( psz_parser )
159             {
160                 /* Domain found */
161                 *psz_parser = 0; psz_parser++;
162                 psz_domain = strdup( psz_tmp );
163             }
164             else psz_parser = psz_tmp;
165
166             psz_user = strdup( psz_parser );
167         }
168
169         free( psz_tmp );
170     }
171
172     /* Build an SMB URI
173      * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]] */
174
175     if( !psz_user ) psz_user = var_InheritString( p_access, "smb-user" );
176     if( psz_user && !*psz_user ) { free( psz_user ); psz_user = NULL; }
177     if( !psz_pwd ) psz_pwd = var_InheritString( p_access, "smb-pwd" );
178     if( psz_pwd && !*psz_pwd ) { free( psz_pwd ); psz_pwd = NULL; }
179     if( !psz_domain ) psz_domain = var_InheritString( p_access, "smb-domain" );
180     if( psz_domain && !*psz_domain ) { free( psz_domain ); psz_domain = NULL; }
181
182 #ifdef WIN32
183     if( psz_user )
184         Win32AddConnection( p_access, psz_location, psz_user, psz_pwd, psz_domain);
185     i_ret = asprintf( &psz_uri, "//%s", psz_location );
186 #else
187     if( psz_user )
188         i_ret = asprintf( &psz_uri, "smb://%s%s%s%s%s@%s",
189                           psz_domain ? psz_domain : "", psz_domain ? ";" : "",
190                           psz_user, psz_pwd ? ":" : "",
191                           psz_pwd ? psz_pwd : "", psz_location );
192     else
193         i_ret = asprintf( &psz_uri, "smb://%s", psz_location );
194 #endif
195
196     free( psz_user );
197     free( psz_pwd );
198     free( psz_domain );
199
200     if( i_ret == -1 )
201         return VLC_ENOMEM;
202
203 #ifndef WIN32
204     if( smbc_init( smb_auth, 0 ) )
205     {
206         free( psz_uri );
207         return VLC_EGENERIC;
208     }
209 #endif
210
211 /*
212 ** some version of glibc defines open as a macro, causing havoc
213 ** with other macros using 'open' under the hood, such as the
214 ** following one:
215 */
216 #if defined(smbc_open) && defined(open)
217 # undef open
218 #endif
219     if( (i_smb = smbc_open( psz_uri, O_RDONLY, 0 )) < 0 )
220     {
221         msg_Err( p_access, "open failed for '%s' (%m)",
222                  p_access->psz_location );
223         free( psz_uri );
224         return VLC_EGENERIC;
225     }
226
227     /* Init p_access */
228     STANDARD_READ_ACCESS_INIT;
229
230     i_ret = smbc_fstat( i_smb, &filestat );
231     if( i_ret )
232     {
233         errno = i_ret;
234         msg_Err( p_access, "stat failed (%m)" );
235     }
236     else
237         p_access->info.i_size = filestat.st_size;
238
239     free( psz_uri );
240
241     p_sys->i_smb = i_smb;
242
243     return VLC_SUCCESS;
244 }
245
246 /*****************************************************************************
247  * Close: free unused data structures
248  *****************************************************************************/
249 static void Close( vlc_object_t *p_this )
250 {
251     access_t     *p_access = (access_t*)p_this;
252     access_sys_t *p_sys = p_access->p_sys;
253
254     smbc_close( p_sys->i_smb );
255     free( p_sys );
256 }
257
258 /*****************************************************************************
259  * Seek: try to go at the right place
260  *****************************************************************************/
261 static int Seek( access_t *p_access, uint64_t i_pos )
262 {
263     access_sys_t *p_sys = p_access->p_sys;
264     int64_t      i_ret;
265
266     if( i_pos >= INT64_MAX )
267         return VLC_EGENERIC;
268
269     msg_Dbg( p_access, "seeking to %"PRId64, i_pos );
270
271     i_ret = smbc_lseek( p_sys->i_smb, i_pos, SEEK_SET );
272     if( i_ret == -1 )
273     {
274         msg_Err( p_access, "seek failed (%m)" );
275         return VLC_EGENERIC;
276     }
277
278     p_access->info.b_eof = false;
279     p_access->info.i_pos = i_ret;
280
281     return VLC_SUCCESS;
282 }
283
284 /*****************************************************************************
285  * Read:
286  *****************************************************************************/
287 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
288 {
289     access_sys_t *p_sys = p_access->p_sys;
290     int i_read;
291
292     if( p_access->info.b_eof ) return 0;
293
294     i_read = smbc_read( p_sys->i_smb, p_buffer, i_len );
295     if( i_read < 0 )
296     {
297         msg_Err( p_access, "read failed (%m)" );
298         return -1;
299     }
300
301     if( i_read == 0 ) p_access->info.b_eof = true;
302     else if( i_read > 0 ) p_access->info.i_pos += i_read;
303
304     return i_read;
305 }
306
307 /*****************************************************************************
308  * Control:
309  *****************************************************************************/
310 static int Control( access_t *p_access, int i_query, va_list args )
311 {
312     switch( i_query )
313     {
314     case ACCESS_CAN_SEEK:
315     case ACCESS_CAN_FASTSEEK:
316     case ACCESS_CAN_PAUSE:
317     case ACCESS_CAN_CONTROL_PACE:
318         *va_arg( args, bool* ) = true;
319         break;
320
321     case ACCESS_GET_PTS_DELAY:
322         *va_arg( args, int64_t * ) = INT64_C(1000)
323             * var_InheritInteger( p_access, "network-caching" );
324         break;
325
326     case ACCESS_SET_PAUSE_STATE:
327         /* Nothing to do */
328         break;
329
330     case ACCESS_GET_TITLE_INFO:
331     case ACCESS_SET_TITLE:
332     case ACCESS_SET_SEEKPOINT:
333     case ACCESS_SET_PRIVATE_ID_STATE:
334     case ACCESS_GET_CONTENT_TYPE:
335         return VLC_EGENERIC;
336
337     default:
338         msg_Warn( p_access, "unimplemented query in control" );
339         return VLC_EGENERIC;
340
341     }
342
343     return VLC_SUCCESS;
344 }
345
346 #ifdef WIN32
347 static void Win32AddConnection( access_t *p_access, char *psz_path,
348                                 char *psz_user, char *psz_pwd,
349                                 char *psz_domain )
350 {
351     char psz_remote[MAX_PATH], psz_server[MAX_PATH], psz_share[MAX_PATH];
352     NETRESOURCE net_resource;
353     DWORD i_result;
354     char *psz_parser;
355     VLC_UNUSED( psz_domain );
356
357     memset( &net_resource, 0, sizeof(net_resource) );
358     net_resource.dwType = RESOURCETYPE_DISK;
359
360     /* Find out server and share names */
361     strlcpy( psz_server, psz_path, sizeof( psz_server ) );
362     psz_share[0] = 0;
363     psz_parser = strchr( psz_path, '/' );
364     if( psz_parser )
365     {
366         char *psz_parser2 = strchr( ++psz_parser, '/' );
367         if( psz_parser2 )
368             strlcpy( psz_share, psz_parser, sizeof( psz_share ) );
369    }
370
371     snprintf( psz_remote, sizeof( psz_remote ), "\\\\%s\\%s", psz_server, psz_share );
372     net_resource.lpRemoteName = psz_remote;
373
374     i_result = WNetAddConnection2( &net_resource, psz_pwd, psz_user, 0 );
375
376     if( i_result != NO_ERROR )
377     {
378         msg_Dbg( p_access, "connected to %s", psz_remote );
379     }
380     else if( i_result != ERROR_ALREADY_ASSIGNED &&
381              i_result != ERROR_DEVICE_ALREADY_REMEMBERED )
382     {
383         msg_Dbg( p_access, "already connected to %s", psz_remote );
384     }
385     else
386     {
387         msg_Dbg( p_access, "failed to connect to %s", psz_remote );
388     }
389 }
390 #endif // WIN32