1

特定の資格情報を使用して、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()呼び出しは成功しますが、呼び出しDirectoryExistsSambaWrapper.smbc_opendir(fullpath)に負のハンドルを取得し、次の例外をスローします...

Slurpy.Exceptions.FetchException: 例外: フェッチに失敗しました: EACCES > (file:////targetserver/Public/ValidSubfolder) ---> System.Exception: EACCES

私は何を間違っていますか?これをデバッグする方法はありますか?

編集:問題は、認証コールバックからの値が効果を持たないことです(ただし、コールバックは処理されたログステートメントとして確実に呼び出されます)。文字列の不変性と、古い値を上書きするのではなく、新しい値で作成された新しい文字列インスタンスと関係があるのでしょうか?

編集:接続しようとしている 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);
4

1 に答える 1