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