首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mono DllImport libsmbclient不认证

Mono DllImport libsmbclient不认证
EN

Stack Overflow用户
提问于 2014-06-28 00:06:20
回答 1查看 505关注 0票数 1

我需要能够使用特定的凭据从Mono访问Samba/Cifs共享。

到目前为止,我发现最好的选择是使用libsmbclient。不幸的是,我无法让它进行身份验证。为了排除防火墙/安全性/etc,我尝试使用smbclient可执行文件,它可以毫无问题地连接。

低水平的DLLImport材料和一些硬编码的测试卡.

代码语言:javascript
复制
public static void Initialise() {
    log.Trace("Initialising libsmbclient wrapper");
    try {
        smbc_init(callbackAuth, 1);
    } catch (Exception e) {
        log.Trace(String.Format("{0}: {1}", e.GetType().Name, e.ToString()));
        throw;
    }
}

public static void callbackAuth(
    [MarshalAs(UnmanagedType.LPStr)]String server,
    [MarshalAs(UnmanagedType.LPStr)]String share,
    [MarshalAs(UnmanagedType.LPStr)]String workgroup, int workgroupMaxLen,
    [MarshalAs(UnmanagedType.LPStr)]String username, int usernameMaxLen,
    [MarshalAs(UnmanagedType.LPStr)]String password, int passwordMaxLen) {
    server = "targetserver";
    share = "Public";
    username = "Management.Service";
    password = @"{SomeComplexPassword}";
    workgroup = "targetserver";
    usernameMaxLen = username.Length;
    passwordMaxLen = password.Length;
    workgroupMaxLen = workgroup.Length;
}

[DllImport("libsmbclient.so", SetLastError = true)]
extern internal static int smbc_init(smbCGetAuthDataFn callBackAuth, int debug);

[DllImport("libsmbclient.so", SetLastError = true)]
extern internal static int smbc_opendir([MarshalAs(UnmanagedType.LPStr)]String durl);

我试着像这样使用它..。

代码语言:javascript
复制
public bool DirectoryExists(string path) {
    log.Trace("Checking directory exists {0}", path);
    int handle;
    string fullpath = @"smb:" + parseUNCPath(path);
    handle = SambaWrapper.smbc_opendir(fullpath);
    if (handle < 0) {
    var error = Stdlib.GetLastError().ToString();
        if (error == "ENOENT"
           |error == "EINVAL")
            return false;
        else
            throw new Exception(error);
    } else {
    WrapperSambaClient.smbc_close(fd);
    return true;
}
}

Initialise()调用成功,但是当DirectoryExists调用SambaWrapper.smbc_opendir(fullpath)时,我得到一个负句柄并抛出以下异常.

File:////targetserver/Public/ValidSubfolder):Slurpy.Exceptions.FetchException: Slurpy.Exceptions.FetchException: Exception:未能获取: EACCES >(Slurpy.Exceptions.FetchException--> System.Exception: EACCES

我做错了什么?有什么方法可以调试吗?

编辑:,问题似乎是来自auth回调的值没有任何影响(但是回调肯定是作为日志语句在那里被处理的)。我想知道这是否与字符串的不可变性和用新值创建的新字符串实例有关,而不是覆盖旧值?

编辑:试图连接删除的libsmbclient的完全调试输出。如果需要,您可以在编辑历史记录中看到它。

根据请求,从标头中定义方法.

代码语言:javascript
复制
/**@ingroup misc
 * Initialize the samba client library.
 *
 * Must be called before using any of the smbclient API function
 *  
 * @param fn        The function that will be called to obtaion 
 *                  authentication credentials.
 *
 * @param debug     Allows caller to set the debug level. Can be
 *                  changed in smb.conf file. Allows caller to set
 *                  debugging if no smb.conf.
 *   
 * @return          0 on success, < 0 on error with errno set:
 *                  - ENOMEM Out of memory
 *                  - ENOENT The smb.conf file would not load
 *
 */

int smbc_init(smbc_get_auth_data_fn fn, int debug);

/**@ingroup callback
 * Authentication callback function type (traditional method)
 * 
 * Type for the the authentication function called by the library to
 * obtain authentication credentals
 *
 * For kerberos support the function should just be called without
 * prompting the user for credentials. Which means a simple 'return'
 * should work. Take a look at examples/libsmbclient/get_auth_data_fn.h
 * and examples/libsmbclient/testbrowse.c.
 *
 * @param srv       Server being authenticated to
 *
 * @param shr       Share being authenticated to
 *
 * @param wg        Pointer to buffer containing a "hint" for the
 *                  workgroup to be authenticated.  Should be filled in
 *                  with the correct workgroup if the hint is wrong.
 * 
 * @param wglen     The size of the workgroup buffer in bytes
 *
 * @param un        Pointer to buffer containing a "hint" for the
 *                  user name to be use for authentication. Should be
 *                  filled in with the correct workgroup if the hint is
 *                  wrong.
 * 
 * @param unlen     The size of the username buffer in bytes
 *
 * @param pw        Pointer to buffer containing to which password 
 *                  copied
 * 
 * @param pwlen     The size of the password buffer in bytes
 *           
 */
typedef void (*smbc_get_auth_data_fn)(const char *srv, 
                                      const char *shr,
                                      char *wg, int wglen, 
                                      char *un, int unlen,
                                      char *pw, int pwlen);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-01 13:49:02

您的回调由libsmbclient调用,它从非托管代码中传递缓冲区及其长度,期望您填充用户名和密码。由于此分配的内存由调用方控制,因此不能使用字符串或字符串生成器。我建议使用IntPtr,并在尽可能低的级别填充缓冲区。

(另一方面,服务器和共享都是只读字符串,不需要更改,因此我们可以保留它们)。

代码语言:javascript
复制
public static void callbackAuth(
    [MarshalAs(UnmanagedType.LPStr)]String server,
    [MarshalAs(UnmanagedType.LPStr)]String share,
    IntPtr workgroup, int workgroupMaxLen,
    IntPtr username, int usernameMaxLen,
    IntPtr password, int passwordMaxLen) 
{
    //server = "targetserver";
    //share = "Public"; 
    // should not be assigned - 
    // you must provide credentials for specified server

    SetString(username, "Management.Service", username.Length);
    SetString(password, @"{SomeComplexPassword}", password.Length);
    SetString(workgroup, "targetserver", workgroup.Length);
}

private void SetString(IntPtr dest, string str, int maxLen)
{
    // include null string terminator
    byte[] buffer = Encoding.ASCII.GetBytes(str + "\0");
    if (buffer.Length >= maxLen) return; // buffer is not big enough

    Marshal.Copy(buffer, 0, dest, buffer.Length);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24462334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档