access.c 21.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*****************************************************************************
 * bdsm/access.c: liBDSM based SMB/CIFS access module
 *****************************************************************************
 * Copyright (C) 2001-2014 VLC authors and VideoLAN
 *
 * Authors: Julien 'Lta' BALLET <contact # lta 'dot' io>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

Thomas Guillem's avatar
Thomas Guillem committed
30
31
32
33
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_services_discovery.h>
#include <vlc_url.h>
34
35
#include <vlc_access.h>
#include <vlc_variables.h>
Thomas Guillem's avatar
Thomas Guillem committed
36
#include <vlc_keystore.h>
37

38
#include <assert.h>
39
40
41
42
43
44
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

Thomas Guillem's avatar
Thomas Guillem committed
45
#include <bdsm/bdsm.h>
46
#include "../smb_common.h"
Thomas Guillem's avatar
Thomas Guillem committed
47

48
49
50
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Thomas Guillem's avatar
Thomas Guillem committed
51
52
53
54
int bdsm_SdOpen( vlc_object_t * );
void bdsm_SdClose( vlc_object_t * );
int bdsm_sd_probe_Open( vlc_object_t * );

55
56
57
58
static int Open( vlc_object_t * );
static void Close( vlc_object_t * );

#define vlc_sd_probe_Open bdsm_sd_probe_Open
59
60
61
62
63
64
65
66
67
68

#define BDSM_HELP N_("libdsm's SMB (Windows network shares) input and browser")

vlc_module_begin ()
    set_shortname( "dsm" )
    set_description( N_("libdsm SMB input") )
    set_help(BDSM_HELP)
    set_capability( "access", 20 )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACCESS )
69
70
71
    add_string( "smb-user", NULL, SMB_USER_TEXT, SMB_USER_LONGTEXT, false )
    add_password( "smb-pwd", NULL, SMB_PASS_TEXT, SMB_PASS_LONGTEXT, false )
    add_string( "smb-domain", NULL, SMB_DOMAIN_TEXT, SMB_DOMAIN_LONGTEXT, false )
72
73
    add_shortcut( "smb", "cifs" )
    set_callbacks( Open, Close )
74
75

    add_submodule()
76
        add_shortcut( "dsm-sd" )
77
        set_description( N_("libdsm NETBIOS discovery module") )
78
79
80
        set_category( CAT_PLAYLIST )
        set_subcategory( SUBCAT_PLAYLIST_SD )
        set_capability( "services_discovery", 0 )
81
        set_callbacks( bdsm_SdOpen, bdsm_SdClose )
82
83
84

        VLC_SD_PROBE_SUBMODULE

85
86
87
88
89
90
91
92
vlc_module_end ()

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static ssize_t Read( access_t *, uint8_t *, size_t );
static int Seek( access_t *, uint64_t );
static int Control( access_t *, int, va_list );
Thomas Guillem's avatar
Thomas Guillem committed
93
static int BrowserInit( access_t *p_access );
94

Thomas Guillem's avatar
Thomas Guillem committed
95
96
static int get_address( access_t *p_access );
static int login( access_t *p_access );
97
static void backslash_path( vlc_url_t *p_url );
Thomas Guillem's avatar
Thomas Guillem committed
98
static bool get_path( access_t *p_access );
Thomas Guillem's avatar
Thomas Guillem committed
99
static input_item_t* new_item( access_t *p_access, const char *psz_name, int i_type );
Thomas Guillem's avatar
Thomas Guillem committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

struct access_sys_t
{
    netbios_ns         *p_ns;               /**< Netbios name service */
    smb_session        *p_session;          /**< bdsm SMB Session object */
    smb_creds           creds;              /**< Credentials used to connect */

    vlc_url_t           url;
    char               *psz_share;
    char               *psz_path;

    char                netbios_name[16];
    struct in_addr      addr;

    smb_fd              i_fd;               /**< SMB fd for the file we're reading */
    smb_tid             i_tid;              /**< SMB Tree ID we're connected to */
Thomas Guillem's avatar
Thomas Guillem committed
116
117
118
119
120

    size_t              i_browse_count;
    size_t              i_browse_idx;
    smb_share_list      shares;
    smb_stat_list       files;
Thomas Guillem's avatar
Thomas Guillem committed
121
};
122
123
124
125

/*****************************************************************************
 * Open: Initialize module's data structures and libdsm
 *****************************************************************************/
126
static int Open( vlc_object_t *p_this )
127
128
129
{
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;
Thomas Guillem's avatar
Thomas Guillem committed
130
    smb_stat st;
131
132
133
134
135
136
137

    /* Init p_access */
    access_InitFields( p_access );
    p_sys = p_access->p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
    if( p_access->p_sys == NULL )
        return VLC_ENOMEM;

Thomas Guillem's avatar
Thomas Guillem committed
138
139
140
141
142
143
144
145
    p_sys->p_ns = netbios_ns_new();
    if( p_sys->p_ns == NULL )
        goto error;

    p_sys->p_session = smb_session_new();
    if( p_sys->p_session == NULL )
        goto error;

Thomas Guillem's avatar
Thomas Guillem committed
146
147
148
149
150
    char *psz_decoded_location = vlc_uri_decode_duplicate( p_access->psz_location );
    if( psz_decoded_location == NULL )
        goto error;
    vlc_UrlParse( &p_sys->url, psz_decoded_location );
    free( psz_decoded_location );
Thomas Guillem's avatar
Thomas Guillem committed
151
    if( get_address( p_access ) != VLC_SUCCESS )
152
153
        goto error;

Thomas Guillem's avatar
Thomas Guillem committed
154
155
    msg_Dbg( p_access, "Creds: username = %s, domain = %s",
             p_sys->creds.login, p_sys->creds.domain );
Thomas Guillem's avatar
Thomas Guillem committed
156
157
158
159
160
161
162
163
164
165
166
    msg_Dbg( p_access, "Session: Host name = %s, ip = %s", p_sys->netbios_name,
             inet_ntoa( p_sys->addr ) );

    /* Now that we have the required data, let's establish a session */
    if( !smb_session_connect( p_sys->p_session, p_sys->netbios_name,
                              p_sys->addr.s_addr, SMB_TRANSPORT_TCP ) )
    {
        msg_Err( p_access, "Unable to connect/negotiate SMB session");
        goto error;
    }

167
168
    get_path( p_access );

Thomas Guillem's avatar
Thomas Guillem committed
169
    if( login( p_access ) != VLC_SUCCESS )
170
    {
171
172
        msg_Err( p_access, "Unable to open file with path %s (in share %s)",
                 p_sys->psz_path, p_sys->psz_share );
Thomas Guillem's avatar
Thomas Guillem committed
173
        goto error;
174
    }
Thomas Guillem's avatar
Thomas Guillem committed
175

176
177
    /* If there is no shares, browse them */
    if( !p_sys->psz_share )
178
        return BrowserInit( p_access );
Thomas Guillem's avatar
Thomas Guillem committed
179

180
181
    assert(p_sys->i_fd > 0);

182
183
184
    msg_Dbg( p_access, "Path: Share name = %s, path = %s", p_sys->psz_share,
             p_sys->psz_path );

Thomas Guillem's avatar
Thomas Guillem committed
185
    st = smb_stat_fd( p_sys->p_session, p_sys->i_fd );
186
187
188
189
190
191
192
    if( smb_stat_get( st, SMB_STAT_ISDIR ) )
    {
        smb_fclose( p_sys->p_session, p_sys->i_fd );
        return BrowserInit( p_access );
    }

    msg_Dbg( p_access, "Successfully opened smb://%s", p_access->psz_location );
Thomas Guillem's avatar
Thomas Guillem committed
193

194
195
196
197
    ACCESS_SET_CALLBACKS( Read, NULL, Control, Seek );
    return VLC_SUCCESS;

    error:
Thomas Guillem's avatar
Thomas Guillem committed
198
        Close( p_this );
199
200
201
202
203
204
        return VLC_EGENERIC;
}

/*****************************************************************************
 * Close: free unused data structures
 *****************************************************************************/
205
static void Close( vlc_object_t *p_this )
206
207
208
209
{
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys = p_access->p_sys;

Thomas Guillem's avatar
Thomas Guillem committed
210
211
212
213
214
215
    if( p_sys->p_ns )
        netbios_ns_destroy( p_sys->p_ns );
    if( p_sys->i_fd )
        smb_fclose( p_sys->p_session, p_sys->i_fd );
    if( p_sys->p_session )
        smb_session_destroy( p_sys->p_session );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
216
    vlc_UrlClean( &p_sys->url );
Thomas Guillem's avatar
Thomas Guillem committed
217
218
219
220
    if( p_sys->shares )
        smb_share_list_destroy( p_sys->shares );
    if( p_sys->files )
        smb_stat_list_destroy( p_sys->files );
Thomas Guillem's avatar
Thomas Guillem committed
221
222
223
224
225
    free( p_sys->creds.login );
    free( p_sys->creds.password );
    free( p_sys->creds.domain );
    free( p_sys->psz_share );
    free( p_sys->psz_path );
226
227
228
229
230
231
232
    free( p_sys );
}

/*****************************************************************************
 * Local functions
 *****************************************************************************/

Thomas Guillem's avatar
Thomas Guillem committed
233
234
/* Returns VLC_EGENERIC if it wasn't able to get an ip address to connect to */
static int get_address( access_t *p_access )
235
{
Thomas Guillem's avatar
Thomas Guillem committed
236
237
    access_sys_t *p_sys = p_access->p_sys;

238
239
240
241
242
243
    if( !inet_aton( p_sys->url.psz_host, &p_sys->addr ) )
    {
        /* This is not an ip address, let's try netbios/dns resolve */
        struct addrinfo *p_info = NULL;

        /* Is this a netbios name on this LAN ? */
244
245
        if( netbios_ns_resolve( p_sys->p_ns, p_sys->url.psz_host,
                                NETBIOS_FILESERVER,
246
247
248
                                &p_sys->addr.s_addr) )
        {
            strlcpy( p_sys->netbios_name, p_sys->url.psz_host, 16);
Thomas Guillem's avatar
Thomas Guillem committed
249
            return VLC_SUCCESS;
250
251
252
253
254
255
256
257
258
259
260
        }
        /* or is it an existing dns name ? */
        else if( getaddrinfo( p_sys->url.psz_host, NULL, NULL, &p_info ) == 0 )
        {
            if( p_info->ai_family == AF_INET )
            {
                struct sockaddr_in *in = (struct sockaddr_in *)p_info->ai_addr;
                p_sys->addr.s_addr = in->sin_addr.s_addr;
            }
            freeaddrinfo( p_info );
            if( p_info->ai_family != AF_INET )
Thomas Guillem's avatar
Thomas Guillem committed
261
                return VLC_EGENERIC;
262
263
        }
        else
Thomas Guillem's avatar
Thomas Guillem committed
264
            return VLC_EGENERIC;
265
266
267
268
269
270
271
272
273
274
275
276
277
    }

    /* We have an IP address, let's find the NETBIOS name */
    const char *psz_nbt = netbios_ns_inverse( p_sys->p_ns, p_sys->addr.s_addr );
    if( psz_nbt != NULL )
        strlcpy( p_sys->netbios_name, psz_nbt, 16 );
    else
    {
        msg_Warn( p_access, "Unable to get netbios name of %s",
            p_sys->url.psz_host );
        p_sys->netbios_name[0] = '\0';
    }

Thomas Guillem's avatar
Thomas Guillem committed
278
    return VLC_SUCCESS;
279
280
}

Thomas Guillem's avatar
Thomas Guillem committed
281
282
static int smb_connect( access_t *p_access, const char *psz_login,
                        const char *psz_password, const char *psz_domain )
283
{
Thomas Guillem's avatar
Thomas Guillem committed
284
285
    access_sys_t *p_sys = p_access->p_sys;

286
    smb_session_set_creds( p_sys->p_session, psz_domain,
287
                           psz_login, psz_password );
288
289
290
291
292
293
294
295
    if( smb_session_login( p_sys->p_session ) )
    {
        if( p_sys->psz_share )
        {
            /* Connect to the share */
            p_sys->i_tid = smb_tree_connect( p_sys->p_session, p_sys->psz_share );
            if( !p_sys->i_tid )
                return VLC_EGENERIC;
296
297
298
299
300
301
302

            /* Let's finally ask a handle to the file we wanna read ! */
            p_sys->i_fd = smb_fopen( p_sys->p_session, p_sys->i_tid,
                                     p_sys->psz_path, SMB_MOD_RO );
            /* TODO: fix smb_fopen to return a specific error code in case of
             * wrong permissions */
            return p_sys->i_fd > 0 ? VLC_SUCCESS : VLC_EGENERIC;
303
        }
304
305
        else
            return VLC_SUCCESS;
306
307
308
    }
    else
        return VLC_EGENERIC;
309
310
}

311
312
/* Performs login with existing credentials and ask the user for new ones on
   failure */
Thomas Guillem's avatar
Thomas Guillem committed
313
static int login( access_t *p_access )
314
{
Thomas Guillem's avatar
Thomas Guillem committed
315
    int i_ret = VLC_EGENERIC;
Thomas Guillem's avatar
Thomas Guillem committed
316
    access_sys_t *p_sys = p_access->p_sys;
Thomas Guillem's avatar
Thomas Guillem committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
    vlc_url_t url;
    vlc_credential credential;
    char *psz_var_domain;
    const char *psz_login, *psz_password, *psz_domain;

    vlc_UrlParse( &url, p_access->psz_url );
    vlc_credential_init( &credential, &url );
    psz_var_domain = var_InheritString( p_access, "smb-domain" );
    credential.psz_realm = psz_var_domain ? psz_var_domain : p_sys->netbios_name;

    vlc_credential_get( &credential, p_access, "smb-user", "smb-pwd",
                        NULL, NULL );

    if( !credential.psz_username )
    {
        psz_login = "Guest";
        psz_password = "Guest";
    }
    else
    {
        psz_login = credential.psz_username;
        psz_password = credential.psz_password;
    }
    psz_domain = credential.psz_realm;
Thomas Guillem's avatar
Thomas Guillem committed
341

342
    /* Try to authenticate on the remote machine */
Thomas Guillem's avatar
Thomas Guillem committed
343
344
    if( smb_connect( p_access, psz_login, psz_password, psz_domain )
                     != VLC_SUCCESS )
345
    {
Thomas Guillem's avatar
Thomas Guillem committed
346
        while( vlc_credential_get( &credential, p_access, "smb-user", "smb-pwd",
347
348
                                   SMB_LOGIN_DIALOG_TITLE,
                                   SMB_LOGIN_DIALOG_TEXT, p_sys->netbios_name ) )
349
        {
Thomas Guillem's avatar
Thomas Guillem committed
350
351
352
353
354
355
            psz_login = credential.psz_username;
            psz_password = credential.psz_password;
            psz_domain = credential.psz_realm;
            if( smb_connect( p_access, psz_login, psz_password, psz_domain )
                             == VLC_SUCCESS )
                goto success;
356
357
        }

358
359
        msg_Err( p_access, "Unable to login with username = %s, domain = %s",
                   p_sys->creds.login, p_sys->creds.domain );
Thomas Guillem's avatar
Thomas Guillem committed
360
        goto error;
361
    }
Thomas Guillem's avatar
Thomas Guillem committed
362
    else if( smb_session_is_guest( p_sys->p_session )  )
363
364
        msg_Warn( p_access, "Login failure but you were logged in as a Guest");

Thomas Guillem's avatar
Thomas Guillem committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
success:
    p_sys->creds.login = strdup(psz_login);
    p_sys->creds.password = strdup(psz_password);
    p_sys->creds.domain = strdup(psz_domain);
    if (!p_sys->creds.login || !p_sys->creds.password || !p_sys->creds.domain)
    {
        free(p_sys->creds.login);
        free(p_sys->creds.password);
        free(p_sys->creds.domain);
        p_sys->creds.login = p_sys->creds.password = p_sys->creds.domain = NULL;
        goto error;
    }
    vlc_credential_store( &credential );
    i_ret = VLC_SUCCESS;
error:
    vlc_credential_clean( &credential );
    vlc_UrlClean( &url );
    free( psz_var_domain );
    return i_ret;
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
}

static void backslash_path( vlc_url_t *p_url )
{
    char *iter = p_url->psz_path;

    /* Let's switch the path delimiters from / to \ */
    while( *iter != '\0' )
    {
        if( *iter == '/' )
            *iter = '\\';
        iter++;
    }
}

/* Get the share and filepath from uri (also replace all / by \ in url.psz_path) */
Thomas Guillem's avatar
Thomas Guillem committed
400
static bool get_path( access_t *p_access )
401
{
Thomas Guillem's avatar
Thomas Guillem committed
402
    access_sys_t *p_sys = p_access->p_sys;
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
    char *iter;

    if( p_sys->url.psz_path == NULL )
        return false;

    backslash_path( &p_sys->url );

    /* Is path longer than just "/" ? */
    if( strlen( p_sys->url.psz_path ) > 1 )
    {
        iter = p_sys->url.psz_path;
        while( *iter == '\\' ) iter++; /* Handle smb://Host/////Share/ */

        p_sys->psz_share = strdup( iter );
        if ( p_sys->psz_share == NULL )
            return false;
    }
    else
    {
        msg_Dbg( p_access, "no share, nor file path provided, will switch to browser");
        return false;
    }


    iter = strchr( p_sys->psz_share, '\\' );
    if( iter == NULL || strlen(iter + 1) == 0 )
    {
        if( iter != NULL ) /* Remove the trailing \ */
            *iter = '\0';
        p_sys->psz_path = strdup( "" );

        msg_Dbg( p_access, "no file path provided, will switch to browser ");
        return true;
    }

    p_sys->psz_path = strdup( iter + 1); /* Skip the first \ */
    *iter = '\0';
    if( p_sys->psz_path == NULL )
        return false;

    return true;
}

/*****************************************************************************
 * Seek: try to go at the right place
 *****************************************************************************/
static int Seek( access_t *p_access, uint64_t i_pos )
{
    access_sys_t *p_sys = p_access->p_sys;

    if( i_pos >= INT64_MAX )
        return VLC_EGENERIC;

    msg_Dbg( p_access, "seeking to %"PRId64, i_pos );

    /* seek cannot fail in bdsm, but the subsequent read can */
459
    smb_fseek(p_sys->p_session, p_sys->i_fd, i_pos, SMB_SEEK_SET);
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500

    p_access->info.b_eof = false;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Read:
 *****************************************************************************/
static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
{
    access_sys_t *p_sys = p_access->p_sys;
    int i_read;

    if( p_access->info.b_eof ) return 0;

    i_read = smb_fread( p_sys->p_session, p_sys->i_fd, p_buffer, i_len );
    if( i_read < 0 )
    {
        msg_Err( p_access, "read failed" );
        return -1;
    }

    if( i_read == 0 ) p_access->info.b_eof = true;

    return i_read;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( access_t *p_access, int i_query, va_list args )
{
    switch( i_query )
    {
    case ACCESS_CAN_SEEK:
    case ACCESS_CAN_PAUSE:
    case ACCESS_CAN_CONTROL_PACE:
        *va_arg( args, bool* ) = true;
        break;

501
502
503
504
    case ACCESS_CAN_FASTSEEK:
        *va_arg( args, bool* ) = false;
        break;

505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
    case ACCESS_GET_SIZE:
    {
        smb_stat st = smb_stat_fd( p_access->p_sys->p_session,
                                   p_access->p_sys->i_fd );
        *va_arg( args, uint64_t * ) = smb_stat_get( st, SMB_STAT_SIZE );
        break;
    }
    case ACCESS_GET_PTS_DELAY:
        *va_arg( args, int64_t * ) = INT64_C(1000)
            * var_InheritInteger( p_access, "network-caching" );
        break;

    case ACCESS_SET_PAUSE_STATE:
        /* Nothing to do */
        break;

    default:
        return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}
Thomas Guillem's avatar
Thomas Guillem committed
527

Thomas Guillem's avatar
Thomas Guillem committed
528
529
static input_item_t *new_item( access_t *p_access, const char *psz_name,
                               int i_type )
Thomas Guillem's avatar
Thomas Guillem committed
530
531
532
{
    access_sys_t *p_sys = p_access->p_sys;
    input_item_t *p_item;
Thomas Guillem's avatar
Thomas Guillem committed
533
    char         *psz_uri, *psz_option = NULL;
Thomas Guillem's avatar
Thomas Guillem committed
534
535
    int           i_ret;

Thomas Guillem's avatar
Thomas Guillem committed
536
537
538
539
540
541
    char *psz_encoded_name = vlc_uri_encode( psz_name );
    if( psz_encoded_name == NULL )
        return NULL;
    i_ret = asprintf( &psz_uri, "smb://%s/%s", p_access->psz_location,
                      psz_encoded_name );
    free( psz_encoded_name );
Thomas Guillem's avatar
Thomas Guillem committed
542
    if( i_ret == -1 )
Thomas Guillem's avatar
Thomas Guillem committed
543
        return NULL;
Thomas Guillem's avatar
Thomas Guillem committed
544

545
546
    p_item = input_item_NewWithTypeExt( psz_uri, psz_name, 0, NULL, 0, -1,
                                        i_type, 1 );
Thomas Guillem's avatar
Thomas Guillem committed
547
548
    free( psz_uri );
    if( p_item == NULL )
Thomas Guillem's avatar
Thomas Guillem committed
549
        return NULL;
Thomas Guillem's avatar
Thomas Guillem committed
550

551
552
553
554
    /* Don't store user/password when browsing share or if we are guest */
    if( smb_session_is_guest( p_sys->p_session ) || p_sys->psz_share == NULL )
        return p_item;

Thomas Guillem's avatar
Thomas Guillem committed
555
556
    /* Here we save on the node the credentials that allowed us to login.
     * That way the user isn't prompted more than once for credentials */
557
558
559
560
561
562
563
564
    if( p_sys->creds.login )
    {
        i_ret = asprintf( &psz_option, "smb-user=%s", p_sys->creds.login );
        if( i_ret == -1 )
            goto bailout;
        input_item_AddOption( p_item, psz_option, VLC_INPUT_OPTION_TRUSTED );
        free( psz_option );
    }
Thomas Guillem's avatar
Thomas Guillem committed
565

566
567
568
569
570
571
572
573
    if( p_sys->creds.password )
    {
        i_ret = asprintf( &psz_option, "smb-pwd=%s", p_sys->creds.password );
        if( i_ret == -1 )
            goto bailout;
        input_item_AddOption( p_item, psz_option, VLC_INPUT_OPTION_TRUSTED );
        free( psz_option );
    }
Thomas Guillem's avatar
Thomas Guillem committed
574

575
576
577
578
579
580
581
582
    if( p_sys->creds.domain )
    {
        i_ret = asprintf( &psz_option, "smb-domain=%s", p_sys->creds.domain );
        if( i_ret == -1 )
            goto bailout;
        input_item_AddOption( p_item, psz_option, VLC_INPUT_OPTION_TRUSTED );
        free( psz_option );
    }
Thomas Guillem's avatar
Thomas Guillem committed
583

Thomas Guillem's avatar
Thomas Guillem committed
584
585
586
587
588
589
    return p_item;
bailout:
    if( p_item )
        input_item_Release( p_item );
    free( psz_option );
    return NULL;
Thomas Guillem's avatar
Thomas Guillem committed
590
591
}

Thomas Guillem's avatar
Thomas Guillem committed
592
static input_item_t* BrowseShare( access_t *p_access )
Thomas Guillem's avatar
Thomas Guillem committed
593
594
595
{
    access_sys_t *p_sys = p_access->p_sys;
    const char     *psz_name;
Thomas Guillem's avatar
Thomas Guillem committed
596
    input_item_t   *p_item = NULL;
Thomas Guillem's avatar
Thomas Guillem committed
597

Thomas Guillem's avatar
Thomas Guillem committed
598
599
600
601
602
    if( !p_sys->i_browse_count )
        p_sys->i_browse_count = smb_share_get_list( p_sys->p_session,
                                                    &p_sys->shares );
    for( ; !p_item && p_sys->i_browse_idx < p_sys->i_browse_count
         ; p_sys->i_browse_idx++ )
Thomas Guillem's avatar
Thomas Guillem committed
603
    {
Thomas Guillem's avatar
Thomas Guillem committed
604
        psz_name = smb_share_list_at( p_sys->shares, p_sys->i_browse_idx );
Thomas Guillem's avatar
Thomas Guillem committed
605
606
607
608

        if( psz_name[strlen( psz_name ) - 1] == '$')
            continue;

Thomas Guillem's avatar
Thomas Guillem committed
609
610
611
        p_item = new_item( p_access, psz_name, ITEM_TYPE_DIRECTORY );
        if( !p_item )
            return NULL;
Thomas Guillem's avatar
Thomas Guillem committed
612
    }
Thomas Guillem's avatar
Thomas Guillem committed
613
    return p_item;
Thomas Guillem's avatar
Thomas Guillem committed
614
615
}

Thomas Guillem's avatar
Thomas Guillem committed
616
static input_item_t* BrowseDirectory( access_t *p_access )
Thomas Guillem's avatar
Thomas Guillem committed
617
618
619
{
    access_sys_t *p_sys = p_access->p_sys;
    smb_stat        st;
Thomas Guillem's avatar
Thomas Guillem committed
620
    input_item_t   *p_item = NULL;
Thomas Guillem's avatar
Thomas Guillem committed
621
622
623
624
    char           *psz_query;
    const char     *psz_name;
    int             i_ret;

Thomas Guillem's avatar
Thomas Guillem committed
625
    if( !p_sys->i_browse_count )
Thomas Guillem's avatar
Thomas Guillem committed
626
    {
Thomas Guillem's avatar
Thomas Guillem committed
627
628
629
630
631
632
633
634
635
636
637
638
639
        if( p_sys->psz_path != NULL )
        {
            i_ret = asprintf( &psz_query, "%s\\*", p_sys->psz_path );
            if( i_ret == -1 )
                return NULL;
            p_sys->files = smb_find( p_sys->p_session, p_sys->i_tid, psz_query );
            free( psz_query );
        }
        else
            p_sys->files = smb_find( p_sys->p_session, p_sys->i_tid, "\\*" );
        if( p_sys->files == NULL )
            return NULL;
        p_sys->i_browse_count = smb_stat_list_count( p_sys->files );
Thomas Guillem's avatar
Thomas Guillem committed
640
641
    }

Thomas Guillem's avatar
Thomas Guillem committed
642
    if( p_sys->i_browse_idx < p_sys->i_browse_count )
Thomas Guillem's avatar
Thomas Guillem committed
643
    {
644
645
        int i_type;

Thomas Guillem's avatar
Thomas Guillem committed
646
        st = smb_stat_list_at( p_sys->files, p_sys->i_browse_idx++ );
Thomas Guillem's avatar
Thomas Guillem committed
647

Thomas Guillem's avatar
Thomas Guillem committed
648
649
        if( st == NULL )
            return NULL;
Thomas Guillem's avatar
Thomas Guillem committed
650
651
652

        psz_name = smb_stat_name( st );

653
654
        i_type = smb_stat_get( st, SMB_STAT_ISDIR ) ?
                 ITEM_TYPE_DIRECTORY : ITEM_TYPE_FILE;
Thomas Guillem's avatar
Thomas Guillem committed
655
656
657
        p_item = new_item( p_access, psz_name, i_type );
        if( !p_item )
            return NULL;
Thomas Guillem's avatar
Thomas Guillem committed
658
    }
Thomas Guillem's avatar
Thomas Guillem committed
659
    return p_item;
Thomas Guillem's avatar
Thomas Guillem committed
660
661
}

662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
static int DirControl( access_t *p_access, int i_query, va_list args )
{
    switch( i_query )
    {
    case ACCESS_IS_DIRECTORY:
        *va_arg( args, bool * ) = false; /* is not sorted */
        *va_arg( args, bool * ) = p_access->pf_readdir == BrowseDirectory;
                                  /* might loop */
        break;
    default:
        return access_vaDirectoryControlHelper( p_access, i_query, args );
    }

    return VLC_SUCCESS;
}

Thomas Guillem's avatar
Thomas Guillem committed
678
679
680
681
682
683
684
685
static int BrowserInit( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;

    if( p_sys->psz_share == NULL )
        p_access->pf_readdir = BrowseShare;
    else
        p_access->pf_readdir = BrowseDirectory;
686
    p_access->pf_control = DirControl;
Thomas Guillem's avatar
Thomas Guillem committed
687
688
689

    return VLC_SUCCESS;
}