簡體   English   中英

Mono DllImport libsmbclient無法驗證

[英]Mono DllImport libsmbclient not authenticating

我需要能夠使用特定的憑據從Mono訪問Samba / Cifs共享。

到目前為止,我發現最好的選擇是使用libsmbclient。 不幸的是,我無法對其進行身份驗證。 為了排除防火牆/安全性等,我嘗試使用smbclient可執行文件,該可執行文件可以毫無問題地連接。

低級DLLImport內容和一些用於測試的硬編碼憑據...

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);

我正在嘗試像這樣使用它...

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)我得到一個否定句柄並引發以下異常...

Slurpy.Exceptions.FetchException:異常:無法獲取:EACCES>(file://// targetserver / Public / ValidSubfolder)---> System.Exception:EACCES

我究竟做錯了什么? 有什么辦法可以調試嗎?

編輯:似乎問題在於auth回調中的值沒有任何作用(但是肯定會在處理了該回調的情況下將其稱為log語句)。 我想知道這是否與字符串的不變性和使用新值創建新字符串實例而不是覆蓋舊值有關?

編輯:從libsmbclient嘗試連接的完整調試輸出已刪除。 如果需要,您可以在編輯歷史記錄中看到它。

根據要求,從標頭中定義方法...

/**@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);

您的回調由libsmbclient調用,並且正在從非托管代碼傳遞緩沖區及其長度,期望您填充用戶名和密碼。 由於此分配的內存是由調用方控制的,因此不能使用string或stringbuilder。 我建議使用IntPtr,並在盡可能低的級別上填充緩沖區。

(另一方面,服務器和共享是只讀字符串,並且不會更改,因此我們可以將其保留為字符串)。

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM