Changeset 1227d46e2e899fdfcbfdd42812374171f137adc8

Show
Ignore:
Timestamp:
27/01/05 16:44:34 (4 years ago)
Author:
Gildas Bazin <gbazin@videolan.org>
git-committer:
Gildas Bazin <gbazin@videolan.org> 1106840674 +0000
git-parent:

[4bc98f0e636c218fdfae184a7be04618fb5e244e]

git-author:
Gildas Bazin <gbazin@videolan.org> 1106840674 +0000
Message:

* modules/access/smb.c: win32 implementation which doesn't depend on libsmbclient + proper URI parsing.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • configure.ac

    r0e27492 r1227d46  
    978978    VLC_ADD_PLUGINS([screensaver]) 
    979979else 
    980     VLC_ADD_PLUGINS([ntservice]) 
    981     VLC_ADD_PLUGINS([dmo]) 
     980    VLC_ADD_PLUGINS([ntservice smb dmo]) 
    982981    VLC_ADD_LDFLAGS([dmo],[-lole32]) 
    983982fi 
  • modules/access/smb.c

    r0e27492 r1227d46  
    3030#include <vlc/input.h> 
    3131 
    32 #include <libsmbclient.h> 
    33 #define USE_CTX 1 
     32#ifdef WIN32 
     33#ifdef HAVE_FCNTL_H 
     34#   include <fcntl.h> 
     35#endif 
     36#   ifdef HAVE_SYS_STAT_H 
     37#       include <sys/stat.h> 
     38#   endif 
     39#   include <io.h> 
     40#   define smbc_open(a,b,c) open(a,b,c) 
     41#   define stat _stati64 
     42#   define smbc_fstat(a,b) _fstati64(a,b) 
     43#   define smbc_read read 
     44#   define smbc_lseek _lseeki64 
     45#   define smbc_close close 
     46#else 
     47#   include <libsmbclient.h> 
     48#   define USE_CTX 1 
     49#endif 
    3450 
    3551/***************************************************************************** 
     
    88104}; 
    89105 
     106#ifdef WIN32 
     107static void Win32AddConnection( access_t *, char *, char *, char *, char * ); 
     108#endif 
     109 
    90110void smb_auth( const char *srv, const char *shr, char *wg, int wglen, 
    91111               char *un, int unlen, char *pw, int pwlen ) 
     
    102122    access_sys_t *p_sys; 
    103123    struct stat  filestat; 
    104     char         *psz_uri, *psz_user, *psz_pwd, *psz_domain; 
     124    char         *psz_path, *psz_uri; 
     125    char         *psz_user = 0, *psz_pwd = 0, *psz_domain = 0; 
    105126    int          i_ret; 
    106127 
     
    112133#endif 
    113134 
     135    /* Parse input URI 
     136     * [[[domain;]user[:password@]]server[/share[/path[/file]]]] */ 
     137 
     138    psz_path = strchr( p_access->psz_path, '/' ); 
     139    if( !psz_path ) 
     140    { 
     141        msg_Err( p_access, "invalid SMB URI: smb://%s", psz_path ); 
     142        return VLC_EGENERIC; 
     143    } 
     144    else 
     145    { 
     146        char *psz_tmp = strdup( p_access->psz_path ); 
     147        char *psz_parser; 
     148 
     149        psz_tmp[ psz_path - p_access->psz_path ] = 0; 
     150        psz_path = p_access->psz_path; 
     151        psz_parser = strchr( psz_tmp, '@' ); 
     152        if( psz_parser ) 
     153        { 
     154            /* User info is there */ 
     155            *psz_parser = 0; 
     156            psz_path = p_access->psz_path + (psz_parser - psz_tmp) + 1; 
     157 
     158            psz_parser = strchr( psz_tmp, ':' ); 
     159            if( psz_parser ) 
     160            { 
     161                /* Password found */ 
     162                psz_pwd = strdup( psz_parser+1 ); 
     163                *psz_parser = 0; 
     164            } 
     165 
     166            psz_parser = strchr( psz_tmp, ';' ); 
     167            if( psz_parser ) 
     168            { 
     169                /* Domain found */ 
     170                *psz_parser = 0; psz_parser++; 
     171                psz_domain = strdup( psz_tmp ); 
     172            } 
     173            else psz_parser = psz_tmp; 
     174 
     175            psz_user = strdup( psz_parser ); 
     176        } 
     177 
     178        free( psz_tmp ); 
     179    } 
     180 
    114181    /* Build an SMB URI 
    115182     * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]] */ 
    116183 
    117     psz_user = var_CreateGetString( p_access, "smb-user" ); 
     184    if( !psz_user ) psz_user = var_CreateGetString( p_access, "smb-user" ); 
    118185    if( psz_user && !*psz_user ) { free( psz_user ); psz_user = 0; } 
    119     psz_pwd = var_CreateGetString( p_access, "smb-pwd" ); 
     186    if( !psz_pwd ) psz_pwd = var_CreateGetString( p_access, "smb-pwd" ); 
    120187    if( psz_pwd && !*psz_pwd ) { free( psz_pwd ); psz_pwd = 0; } 
    121     psz_domain = var_CreateGetString( p_access, "smb-domain" ); 
     188    if(!psz_domain) psz_domain = var_CreateGetString( p_access, "smb-domain" ); 
    122189    if( psz_domain && !*psz_domain ) { free( psz_domain ); psz_domain = 0; } 
    123190 
    124     /* FIXME: will need to parse the URI so we don't override credentials 
    125      * if there are already present. */ 
     191#ifdef WIN32 
     192    if( psz_user ) 
     193        Win32AddConnection( p_access, psz_path, psz_user, psz_pwd, psz_domain); 
     194    asprintf( &psz_uri, "//%s", psz_path ); 
     195#else 
    126196    if( psz_user ) 
    127197        asprintf( &psz_uri, "smb://%s%s%s%s%s@%s", 
    128198                  psz_domain ? psz_domain : "", psz_domain ? ";" : "", 
    129199                  psz_user, psz_pwd ? ":" : "", 
    130                   psz_pwd ? psz_pwd : "", p_access->psz_path ); 
     200                  psz_pwd ? psz_pwd : "", psz_path ); 
    131201    else 
    132         asprintf( &psz_uri, "smb://%s", p_access->psz_path ); 
     202        asprintf( &psz_uri, "smb://%s", psz_path ); 
     203#endif 
    133204 
    134205    if( psz_user ) free( psz_user ); 
     
    169240 
    170241#else 
     242 
     243#ifndef WIN32 
    171244    if( smbc_init( smb_auth, 1 ) ) 
    172245    { 
     
    174247        return VLC_EGENERIC; 
    175248    } 
     249#endif 
    176250 
    177251    if( (i_smb = smbc_open( psz_uri, O_RDONLY, 0 )) < 0 ) 
     
    347421    return VLC_SUCCESS; 
    348422} 
     423 
     424#ifdef WIN32 
     425static void Win32AddConnection( access_t *p_access, char *psz_path, 
     426                                char *psz_user, char *psz_pwd, 
     427                                char *psz_domain ) 
     428{ 
     429    DWORD (*OurWNetAddConnection2)( LPNETRESOURCE, LPCTSTR, LPCTSTR, DWORD ); 
     430    char psz_remote[MAX_PATH], psz_server[MAX_PATH], psz_share[MAX_PATH]; 
     431    NETRESOURCE net_resource; 
     432    DWORD i_result; 
     433    char *psz_parser; 
     434 
     435    HINSTANCE hdll = LoadLibrary(_T("MPR.DLL")); 
     436    if( !hdll ) 
     437    { 
     438        msg_Warn( p_access, "couldn't load mpr.dll" ); 
     439        return; 
     440    } 
     441 
     442    OurWNetAddConnection2 = 
     443      (void *)GetProcAddress( hdll, _T("WNetAddConnection2A") ); 
     444    if( !OurWNetAddConnection2 ) 
     445    { 
     446        msg_Warn( p_access, "couldn't find WNetAddConnection2 in mpr.dll" ); 
     447        return; 
     448    } 
     449 
     450    memset( &net_resource, 0, sizeof(net_resource) ); 
     451    net_resource.dwType = RESOURCETYPE_DISK; 
     452 
     453    /* Find out server and share names */ 
     454    psz_server[0] = psz_share[0] = 0; 
     455    psz_parser = strchr( psz_path, '/' ); 
     456    if( psz_parser ) 
     457    { 
     458        char *psz_parser2; 
     459        strncat( psz_server, psz_path, psz_parser - psz_path ); 
     460        psz_parser2 = strchr( psz_parser+1, '/' ); 
     461        if( psz_parser2 ) 
     462            strncat( psz_share, psz_parser+1, psz_parser2 - psz_parser -1 ); 
     463    } 
     464    else strncat( psz_server, psz_path, MAX_PATH ); 
     465 
     466    sprintf( psz_remote, "\\\\%s\\%s", psz_server, psz_share ); 
     467    net_resource.lpRemoteName = psz_remote; 
     468 
     469    i_result = OurWNetAddConnection2( &net_resource, psz_pwd, psz_user, 0 ); 
     470 
     471    if( i_result != NO_ERROR ) 
     472    { 
     473        msg_Dbg( p_access, "connected to %s", psz_remote ); 
     474    } 
     475    else if( i_result != ERROR_ALREADY_ASSIGNED &&  
     476             i_result != ERROR_DEVICE_ALREADY_REMEMBERED ) 
     477    { 
     478        msg_Dbg( p_access, "already connected to %s", psz_remote ); 
     479    } 
     480    else 
     481    { 
     482        msg_Dbg( p_access, "failed to connect to %s", psz_remote ); 
     483    } 
     484 
     485    FreeLibrary( hdll ); 
     486} 
     487#endif // WIN32