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