]> git.sesse.net Git - vlc/blob - modules/access/smb.c
* modules/access/smb.c: new SMB/CIFS access module using libsmbclient.
[vlc] / modules / access / smb.c
1 /*****************************************************************************
2  * smb.c: SMB input module
3  *****************************************************************************
4  * Copyright (C) 2001-2004 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
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include <libsmbclient.h>
33 #define USE_CTX 1
34
35 /*****************************************************************************
36  * Module descriptor
37  *****************************************************************************/
38 static int  Open ( vlc_object_t * );
39 static void Close( vlc_object_t * );
40
41 #define CACHING_TEXT N_("Caching value in ms")
42 #define CACHING_LONGTEXT N_( \
43     "Allows you to modify the default caching value for SMB streams. This " \
44     "value should be set in millisecond units." )
45 #define USER_TEXT N_("SMB user name")
46 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
47     "be used for the connection.")
48 #define PASS_TEXT N_("SMB password")
49 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
50     "used for the connection.")
51 #define DOMAIN_TEXT N_("SMB domain")
52 #define DOMAIN_LONGTEXT N_("Allows you to modify the domain/workgroup that " \
53     "will be used for the connection.")
54
55 vlc_module_begin();
56     set_shortname( "SMB" );
57     set_description( _("SMB input") );
58     set_capability( "access2", 0 );
59     set_category( CAT_INPUT );
60     set_subcategory( SUBCAT_INPUT_ACCESS );
61     add_integer( "smb-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
62                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
63     add_string( "smb-user", NULL, NULL, USER_TEXT, USER_LONGTEXT,
64                 VLC_FALSE );
65     add_string( "smb-pwd", NULL, NULL, PASS_TEXT,
66                 PASS_LONGTEXT, VLC_FALSE );
67     add_string( "smb-domain", NULL, NULL, DOMAIN_TEXT,
68                 DOMAIN_LONGTEXT, VLC_FALSE );
69     add_shortcut( "smb" );
70     set_callbacks( Open, Close );
71 vlc_module_end();
72
73 /*****************************************************************************
74  * Local prototypes
75  *****************************************************************************/
76 static int Read( access_t *, uint8_t *, int );
77 static int Seek( access_t *, int64_t );
78 static int Control( access_t *, int, va_list );
79
80 struct access_sys_t
81 {
82 #ifdef USE_CTX
83     SMBCCTX *p_smb;
84     SMBCFILE *p_file;
85 #else
86     int i_smb;
87 #endif
88 };
89
90 void smb_auth( const char *srv, const char *shr, char *wg, int wglen,
91                char *un, int unlen, char *pw, int pwlen )
92 {
93     //wglen = unlen = pwlen = 0;
94 }
95
96 /****************************************************************************
97  * Open: connect to smb server and ask for file
98  ****************************************************************************/
99 static int Open( vlc_object_t *p_this )
100 {
101     access_t     *p_access = (access_t*)p_this;
102     access_sys_t *p_sys;
103     struct stat  filestat;
104     char         *psz_uri, *psz_user, *psz_pwd, *psz_domain;
105     int          i_ret;
106
107 #ifdef USE_CTX
108     SMBCCTX      *p_smb;
109     SMBCFILE     *p_file;
110 #else
111     int          i_smb;
112 #endif
113
114     /* Build an SMB URI
115      * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]] */
116
117     psz_user = var_CreateGetString( p_access, "smb-user" );
118     if( psz_user && !*psz_user ) { free( psz_user ); psz_user = 0; }
119     psz_pwd = var_CreateGetString( p_access, "smb-pwd" );
120     if( psz_pwd && !*psz_pwd ) { free( psz_pwd ); psz_pwd = 0; }
121     psz_domain = var_CreateGetString( p_access, "smb-domain" );
122     if( psz_domain && !*psz_domain ) { free( psz_domain ); psz_domain = 0; }
123
124     /* FIXME: will need to parse the URI so we don't override credentials
125      * if there are already present. */
126     if( psz_user )
127         asprintf( &psz_uri, "smb://%s%s%s%s%s@%s",
128                   psz_domain ? psz_domain : "", psz_domain ? ";" : "",
129                   psz_user, psz_pwd ? ":" : "",
130                   psz_pwd ? psz_pwd : "", p_access->psz_path );
131     else
132         asprintf( &psz_uri, "smb://%s", p_access->psz_path );
133
134     if( psz_user ) free( psz_user );
135     if( psz_pwd ) free( psz_pwd );
136     if( psz_domain ) free( psz_domain );
137
138 #ifdef USE_CTX
139     if( !(p_smb = smbc_new_context()) )
140     {
141         msg_Err( p_access, "out of memory" );
142         free( psz_uri );
143         return VLC_ENOMEM;
144     }
145     p_smb->debug = 1;
146     p_smb->callbacks.auth_fn = smb_auth;
147
148     if( !smbc_init_context( p_smb ) )
149     {
150         msg_Err( p_access, "cannot initialize context (%s)", strerror(errno) );
151         smbc_free_context( p_smb, 1 );
152         free( psz_uri );
153         return VLC_EGENERIC;
154     }
155
156     if( !(p_file = p_smb->open( p_smb, psz_uri, O_RDONLY, 0 )) )
157     {
158         msg_Err( p_access, "open failed for '%s' (%s)",
159                  p_access->psz_path, strerror(errno) );
160         smbc_free_context( p_smb, 1 );
161         free( psz_uri );
162         return VLC_EGENERIC;
163     }
164
165     p_access->info.i_size = 0;
166     i_ret = p_smb->fstat( p_smb, p_file, &filestat );
167     if( i_ret ) msg_Err( p_access, "stat failed (%s)", strerror(errno) );
168     else p_access->info.i_size = filestat.st_size;
169
170 #else
171     if( smbc_init( smb_auth, 1 ) )
172     {
173         free( psz_uri );
174         return VLC_EGENERIC;
175     }
176
177     if( (i_smb = smbc_open( psz_uri, O_RDONLY, 0 )) < 0 )
178     {
179         msg_Err( p_access, "open failed for '%s' (%s)",
180                  p_access->psz_path, strerror(errno) );
181         free( psz_uri );
182         return VLC_EGENERIC;
183     }
184
185     p_access->info.i_size = 0;
186     i_ret = smbc_fstat( i_smb, &filestat );
187     if( i_ret ) msg_Err( p_access, "stat failed (%s)", strerror(i_ret) );
188     else p_access->info.i_size = filestat.st_size;
189 #endif
190
191     free( psz_uri );
192
193     /* Init p_access */
194     p_access->pf_read = Read;
195     p_access->pf_block = NULL;
196     p_access->pf_seek = Seek;
197     p_access->pf_control = Control;
198     p_access->info.i_update = 0;
199     p_access->info.i_pos = 0;
200     p_access->info.b_eof = VLC_FALSE;
201     p_access->info.i_title = 0;
202     p_access->info.i_seekpoint = 0;
203     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
204     memset( p_sys, 0, sizeof( access_sys_t ) );
205
206 #ifdef USE_CTX
207     p_sys->p_smb = p_smb;
208     p_sys->p_file = p_file;
209 #else
210     p_sys->i_smb = i_smb;
211 #endif
212
213     /* Update default_pts to a suitable value for smb access */
214     var_Create( p_access, "smb-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
215
216     return VLC_SUCCESS;
217 }
218
219 /*****************************************************************************
220  * Close: free unused data structures
221  *****************************************************************************/
222 static void Close( vlc_object_t *p_this )
223 {
224     access_t     *p_access = (access_t*)p_this;
225     access_sys_t *p_sys = p_access->p_sys;
226
227 #ifdef USE_CTX
228     p_sys->p_smb->close( p_sys->p_smb, p_sys->p_file );
229     smbc_free_context( p_sys->p_smb, 1 );
230 #else
231     smbc_close( p_sys->i_smb );
232 #endif
233
234     free( p_sys );
235 }
236
237 /*****************************************************************************
238  * Seek: try to go at the right place
239  *****************************************************************************/
240 static int Seek( access_t *p_access, int64_t i_pos )
241 {
242     access_sys_t *p_sys = p_access->p_sys;
243     int64_t      i_ret;
244
245     if( i_pos < 0 ) return VLC_EGENERIC;
246
247     msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
248
249 #ifdef USE_CTX
250     i_ret = p_sys->p_smb->lseek(p_sys->p_smb, p_sys->p_file, i_pos, SEEK_SET);
251 #else
252     i_ret = smbc_lseek( p_sys->i_smb, i_pos, SEEK_SET );
253 #endif
254     if( i_ret == -1 )
255     {
256         msg_Err( p_access, "seek failed (%s)", strerror(errno) );
257         return VLC_EGENERIC;
258     }
259
260     p_access->info.b_eof = VLC_FALSE;
261     p_access->info.i_pos = i_ret;
262
263     return VLC_SUCCESS;
264 }
265
266 /*****************************************************************************
267  * Read:
268  *****************************************************************************/
269 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
270 {
271     access_sys_t *p_sys = p_access->p_sys;
272     int i_read;
273
274     if( p_access->info.b_eof ) return 0;
275
276 #ifdef USE_CTX
277     i_read = p_sys->p_smb->read(p_sys->p_smb, p_sys->p_file, p_buffer, i_len);
278 #else
279     i_read = smbc_read( p_sys->i_smb, p_buffer, i_len );
280 #endif
281     if( i_read < 0 )
282     {
283         msg_Err( p_access, "read failed (%s)", strerror(errno) );
284         return -1;
285     }
286
287     if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
288     else if( i_read > 0 ) p_access->info.i_pos += i_read;
289
290     return i_read;
291 }
292
293 /*****************************************************************************
294  * Control:
295  *****************************************************************************/
296 static int Control( access_t *p_access, int i_query, va_list args )
297 {
298     vlc_bool_t   *pb_bool;
299     int          *pi_int;
300     int64_t      *pi_64;
301
302     switch( i_query )
303     {
304     case ACCESS_CAN_SEEK:
305         pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
306         *pb_bool = VLC_TRUE;
307         break;
308     case ACCESS_CAN_FASTSEEK:
309         pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
310         *pb_bool = VLC_TRUE;
311         break;
312     case ACCESS_CAN_PAUSE:
313         pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
314         *pb_bool = VLC_TRUE;
315         break;
316     case ACCESS_CAN_CONTROL_PACE:
317         pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
318         *pb_bool = VLC_TRUE;
319         break;
320
321     case ACCESS_GET_MTU:
322         pi_int = (int*)va_arg( args, int * );
323         *pi_int = 0;
324         break;
325
326     case ACCESS_GET_PTS_DELAY:
327         pi_64 = (int64_t*)va_arg( args, int64_t * );
328         *pi_64 = (int64_t)var_GetInteger( p_access, "smb-caching" ) * 1000;
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         return VLC_EGENERIC;
340
341     default:
342         msg_Warn( p_access, "unimplemented query in control" );
343         return VLC_EGENERIC;
344
345     }
346
347     return VLC_SUCCESS;
348 }