]> git.sesse.net Git - vlc/blob - modules/access/smb.c
vpx: fix leak
[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_("Username")
57 #define USER_LONGTEXT N_("Username that will be used for the connection, " \
58         "if no username is set in the URL.")
59 #define PASS_TEXT N_("Password")
60 #define PASS_LONGTEXT N_("Password that will be used for the connection, " \
61         "if no username or password are set in URL.")
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     uint64_t size;
95 };
96
97 #ifdef _WIN32
98 static void Win32AddConnection( access_t *, char *, char *, char *, char * );
99 #else
100 static void smb_auth( const char *srv, const char *shr, char *wg, int wglen,
101                       char *un, int unlen, char *pw, int pwlen )
102 {
103     VLC_UNUSED(srv);
104     VLC_UNUSED(shr);
105     VLC_UNUSED(wg);
106     VLC_UNUSED(wglen);
107     VLC_UNUSED(un);
108     VLC_UNUSED(unlen);
109     VLC_UNUSED(pw);
110     VLC_UNUSED(pwlen);
111     //wglen = unlen = pwlen = 0;
112 }
113 #endif
114
115 /****************************************************************************
116  * Open: connect to smb server and ask for file
117  ****************************************************************************/
118 static int Open( vlc_object_t *p_this )
119 {
120     access_t     *p_access = (access_t*)p_this;
121     access_sys_t *p_sys;
122     struct stat  filestat;
123     char         *psz_location, *psz_uri;
124     char         *psz_user = NULL, *psz_pwd = NULL, *psz_domain = NULL;
125     int          i_ret;
126     int          i_smb;
127
128     /* Parse input URI
129      * [[[domain;]user[:password@]]server[/share[/path[/file]]]] */
130     psz_location = strchr( p_access->psz_location, '/' );
131     if( !psz_location )
132     {
133         msg_Err( p_access, "invalid SMB URI: smb://%s", psz_location );
134         return VLC_EGENERIC;
135     }
136     else
137     {
138         char *psz_tmp = strdup( p_access->psz_location );
139         char *psz_parser;
140
141         psz_tmp[ psz_location - p_access->psz_location ] = 0;
142         psz_location = p_access->psz_location;
143         psz_parser = strchr( psz_tmp, '@' );
144         if( psz_parser )
145         {
146             /* User info is there */
147             *psz_parser = 0;
148             psz_location = p_access->psz_location + (psz_parser - psz_tmp) + 1;
149
150             psz_parser = strchr( psz_tmp, ':' );
151             if( psz_parser )
152             {
153                 /* Password found */
154                 psz_pwd = strdup( psz_parser+1 );
155                 *psz_parser = 0;
156             }
157
158             psz_parser = strchr( psz_tmp, ';' );
159             if( psz_parser )
160             {
161                 /* Domain found */
162                 *psz_parser = 0; psz_parser++;
163                 psz_domain = strdup( psz_tmp );
164             }
165             else psz_parser = psz_tmp;
166
167             psz_user = strdup( psz_parser );
168         }
169
170         free( psz_tmp );
171     }
172
173     /* Build an SMB URI
174      * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]] */
175
176     if( !psz_user ) psz_user = var_InheritString( p_access, "smb-user" );
177     if( psz_user && !*psz_user ) { free( psz_user ); psz_user = NULL; }
178     if( !psz_pwd ) psz_pwd = var_InheritString( p_access, "smb-pwd" );
179     if( psz_pwd && !*psz_pwd ) { free( psz_pwd ); psz_pwd = NULL; }
180     if( !psz_domain ) psz_domain = var_InheritString( p_access, "smb-domain" );
181     if( psz_domain && !*psz_domain ) { free( psz_domain ); psz_domain = NULL; }
182
183 #ifdef _WIN32
184     if( psz_user )
185         Win32AddConnection( p_access, psz_location, psz_user, psz_pwd, psz_domain);
186     i_ret = asprintf( &psz_uri, "//%s", psz_location );
187 #else
188     if( psz_user )
189         i_ret = asprintf( &psz_uri, "smb://%s%s%s%s%s@%s",
190                           psz_domain ? psz_domain : "", psz_domain ? ";" : "",
191                           psz_user, psz_pwd ? ":" : "",
192                           psz_pwd ? psz_pwd : "", psz_location );
193     else
194         i_ret = asprintf( &psz_uri, "smb://%s", psz_location );
195 #endif
196
197     free( psz_user );
198     free( psz_pwd );
199     free( psz_domain );
200
201     if( i_ret == -1 )
202         return VLC_ENOMEM;
203
204 #ifndef _WIN32
205     if( smbc_init( smb_auth, 0 ) )
206     {
207         free( psz_uri );
208         return VLC_EGENERIC;
209     }
210 #endif
211
212 /*
213 ** some version of glibc defines open as a macro, causing havoc
214 ** with other macros using 'open' under the hood, such as the
215 ** following one:
216 */
217 #if defined(smbc_open) && defined(open)
218 # undef open
219 #endif
220     if( (i_smb = smbc_open( psz_uri, O_RDONLY, 0 )) < 0 )
221     {
222         msg_Err( p_access, "open failed for '%s' (%s)",
223                  p_access->psz_location, vlc_strerror_c(errno) );
224         free( psz_uri );
225         return VLC_EGENERIC;
226     }
227
228     /* Init p_access */
229     STANDARD_READ_ACCESS_INIT;
230
231     i_ret = smbc_fstat( i_smb, &filestat );
232     if( i_ret )
233     {
234         errno = i_ret;
235         msg_Err( p_access, "stat failed (%s)", vlc_strerror_c(errno) );
236     }
237     else
238         p_sys->size = filestat.st_size;
239
240     free( psz_uri );
241
242     p_sys->i_smb = i_smb;
243
244     return VLC_SUCCESS;
245 }
246
247 /*****************************************************************************
248  * Close: free unused data structures
249  *****************************************************************************/
250 static void Close( vlc_object_t *p_this )
251 {
252     access_t     *p_access = (access_t*)p_this;
253     access_sys_t *p_sys = p_access->p_sys;
254
255     smbc_close( p_sys->i_smb );
256     free( p_sys );
257 }
258
259 /*****************************************************************************
260  * Seek: try to go at the right place
261  *****************************************************************************/
262 static int Seek( access_t *p_access, uint64_t i_pos )
263 {
264     access_sys_t *p_sys = p_access->p_sys;
265     int64_t      i_ret;
266
267     if( i_pos >= INT64_MAX )
268         return VLC_EGENERIC;
269
270     msg_Dbg( p_access, "seeking to %"PRId64, i_pos );
271
272     i_ret = smbc_lseek( p_sys->i_smb, i_pos, SEEK_SET );
273     if( i_ret == -1 )
274     {
275         msg_Err( p_access, "seek failed (%s)", vlc_strerror_c(errno) );
276         return VLC_EGENERIC;
277     }
278
279     p_access->info.b_eof = false;
280     p_access->info.i_pos = i_ret;
281
282     return VLC_SUCCESS;
283 }
284
285 /*****************************************************************************
286  * Read:
287  *****************************************************************************/
288 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
289 {
290     access_sys_t *p_sys = p_access->p_sys;
291     int i_read;
292
293     if( p_access->info.b_eof ) return 0;
294
295     i_read = smbc_read( p_sys->i_smb, p_buffer, i_len );
296     if( i_read < 0 )
297     {
298         msg_Err( p_access, "read failed (%s)", vlc_strerror_c(errno) );
299         return -1;
300     }
301
302     if( i_read == 0 ) p_access->info.b_eof = true;
303     else if( i_read > 0 ) p_access->info.i_pos += i_read;
304
305     return i_read;
306 }
307
308 /*****************************************************************************
309  * Control:
310  *****************************************************************************/
311 static int Control( access_t *p_access, int i_query, va_list args )
312 {
313     switch( i_query )
314     {
315     case ACCESS_CAN_SEEK:
316     case ACCESS_CAN_FASTSEEK:
317     case ACCESS_CAN_PAUSE:
318     case ACCESS_CAN_CONTROL_PACE:
319         *va_arg( args, bool* ) = true;
320         break;
321
322     case ACCESS_GET_SIZE:
323         *va_arg( args, uint64_t * ) = p_access->p_sys->size;
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     default:
336         return VLC_EGENERIC;
337     }
338
339     return VLC_SUCCESS;
340 }
341
342 #ifdef _WIN32
343 static void Win32AddConnection( access_t *p_access, char *psz_path,
344                                 char *psz_user, char *psz_pwd,
345                                 char *psz_domain )
346 {
347     char psz_remote[MAX_PATH], psz_server[MAX_PATH], psz_share[MAX_PATH];
348     NETRESOURCE net_resource;
349     DWORD i_result;
350     char *psz_parser;
351     VLC_UNUSED( psz_domain );
352
353     memset( &net_resource, 0, sizeof(net_resource) );
354     net_resource.dwType = RESOURCETYPE_DISK;
355
356     /* Find out server and share names */
357     strlcpy( psz_server, psz_path, sizeof( psz_server ) );
358     psz_share[0] = 0;
359     psz_parser = strchr( psz_path, '/' );
360     if( psz_parser )
361     {
362         char *psz_parser2 = strchr( ++psz_parser, '/' );
363         if( psz_parser2 )
364             strlcpy( psz_share, psz_parser, sizeof( psz_share ) );
365    }
366
367     snprintf( psz_remote, sizeof( psz_remote ), "\\\\%s\\%s", psz_server, psz_share );
368     net_resource.lpRemoteName = psz_remote;
369
370     i_result = WNetAddConnection2( &net_resource, psz_pwd, psz_user, 0 );
371
372     if( i_result != NO_ERROR )
373     {
374         msg_Dbg( p_access, "connected to %s", psz_remote );
375     }
376     else if( i_result != ERROR_ALREADY_ASSIGNED &&
377              i_result != ERROR_DEVICE_ALREADY_REMEMBERED )
378     {
379         msg_Dbg( p_access, "already connected to %s", psz_remote );
380     }
381     else
382     {
383         msg_Dbg( p_access, "failed to connect to %s", psz_remote );
384     }
385 }
386 #endif // _WIN32