libsmbclient: Allow server (NetApp) to return STATUS_INVALID_PARAMETER from an echo.
[nivanova/samba-autobuild/.git] / source3 / libsmb / libsmb_server.c
index 9d87f421fb2d84a235dd98cd77fa4ba65869f276..e6067be2013025b8517fef912e44c548cb807d75 100644 (file)
@@ -45,10 +45,35 @@ int
 SMBC_check_server(SMBCCTX * context,
                   SMBCSRV * server)
 {
+       time_t now;
+
        if (!cli_state_is_connected(server->cli)) {
                return 1;
        }
 
+       now = time_mono(NULL);
+
+       if (server->last_echo_time == (time_t)0 ||
+                       now > server->last_echo_time +
+                               (server->cli->timeout/1000)) {
+               unsigned char data[16] = {0};
+               NTSTATUS status = cli_echo(server->cli,
+                                       1,
+                                       data_blob_const(data, sizeof(data)));
+               if (!NT_STATUS_IS_OK(status)) {
+                       /*
+                        * Some NetApp servers return
+                        * NT_STATUS_INVALID_PARAMETER.That's OK, they still
+                        * replied.
+                        * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13007
+                        */
+                       if (!NT_STATUS_EQUAL(status,
+                                       NT_STATUS_INVALID_PARAMETER)) {
+                               return 1;
+                       }
+               }
+               server->last_echo_time = now;
+       }
        return 0;
 }
 
@@ -105,14 +130,20 @@ SMBC_call_auth_fn(TALLOC_CTX *ctx,
                   char **pp_username,
                   char **pp_password)
 {
-       fstring workgroup;
-       fstring username;
-       fstring password;
+       fstring workgroup = { 0 };
+       fstring username = { 0 };
+       fstring password = { 0 };
         smbc_get_auth_data_with_context_fn auth_with_context_fn;
 
-       strlcpy(workgroup, *pp_workgroup, sizeof(workgroup));
-       strlcpy(username, *pp_username, sizeof(username));
-       strlcpy(password, *pp_password, sizeof(password));
+       if (*pp_workgroup != NULL) {
+               strlcpy(workgroup, *pp_workgroup, sizeof(workgroup));
+       }
+       if (*pp_username != NULL) {
+               strlcpy(username, *pp_username, sizeof(username));
+       }
+       if (*pp_password != NULL) {
+               strlcpy(password, *pp_password, sizeof(password));
+       }
 
         /* See if there's an authentication with context function provided */
         auth_with_context_fn = smbc_getFunctionAuthDataWithContext(context);
@@ -251,12 +282,19 @@ SMBC_server_internal(TALLOC_CTX *ctx,
        struct cli_state *c = NULL;
        const char *server_n = server;
         int is_ipc = (share != NULL && strcmp(share, "IPC$") == 0);
-       uint32 fs_attrs = 0;
-        const char *username_used;
+       uint32_t fs_attrs = 0;
+       const char *username_used = NULL;
+       const char *password_used = NULL;
        NTSTATUS status;
        char *newserver, *newshare;
        int flags = 0;
        struct smbXcli_tcon *tcon = NULL;
+       int signing_state = SMB_SIGNING_DEFAULT;
+       struct cli_credentials *creds = NULL;
+       bool use_kerberos = false;
+       bool fallback_after_kerberos = false;
+       bool use_ccache = false;
+       bool pw_nt_hash = false;
 
        ZERO_STRUCT(c);
        *in_cache = false;
@@ -320,11 +358,10 @@ SMBC_server_internal(TALLOC_CTX *ctx,
                        status = cli_tree_connect(srv->cli,
                                                  srv->cli->share,
                                                  "?????",
-                                                 *pp_password,
-                                                 strlen(*pp_password)+1);
+                                                 *pp_password);
                        if (!NT_STATUS_IS_OK(status)) {
-                                errno = map_errno_from_nt_status(status);
                                 cli_shutdown(srv->cli);
+                                errno = map_errno_from_nt_status(status);
                                srv->cli = NULL;
                                 smbc_getFunctionRemoveCachedServer(context)(context,
                                                                             srv);
@@ -409,18 +446,26 @@ SMBC_server_internal(TALLOC_CTX *ctx,
 
        if (smbc_getOptionUseKerberos(context)) {
                flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
+               use_kerberos = true;
        }
 
        if (smbc_getOptionFallbackAfterKerberos(context)) {
                flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
+               fallback_after_kerberos = true;
        }
 
        if (smbc_getOptionUseCCache(context)) {
                flags |= CLI_FULL_CONNECTION_USE_CCACHE;
+               use_ccache = true;
        }
 
        if (smbc_getOptionUseNTHash(context)) {
                flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
+               pw_nt_hash = true;
+       }
+
+       if (context->internal->smb_encryption_level != SMBC_ENCRYPTLEVEL_NONE) {
+               signing_state = SMB_SIGNING_REQUIRED;
        }
 
        if (port == 0) {
@@ -430,7 +475,7 @@ SMBC_server_internal(TALLOC_CTX *ctx,
                         */
                        status = cli_connect_nb(server_n, NULL, NBT_SMB_PORT, 0x20,
                                        smbc_getNetbiosName(context),
-                                       SMB_SIGNING_DEFAULT, flags, &c);
+                                       signing_state, flags, &c);
                }
        }
 
@@ -440,7 +485,7 @@ SMBC_server_internal(TALLOC_CTX *ctx,
                 */
                status = cli_connect_nb(server_n, NULL, port, 0x20,
                                        smbc_getNetbiosName(context),
-                                       SMB_SIGNING_DEFAULT, flags, &c);
+                                       signing_state, flags, &c);
        }
 
        if (!NT_STATUS_IS_OK(status)) {
@@ -451,31 +496,46 @@ SMBC_server_internal(TALLOC_CTX *ctx,
        cli_set_timeout(c, smbc_getTimeout(context));
 
        status = smbXcli_negprot(c->conn, c->timeout,
-                                lp_cli_minprotocol(),
-                                lp_cli_maxprotocol());
+                                lp_client_min_protocol(),
+                                lp_client_max_protocol());
        if (!NT_STATUS_IS_OK(status)) {
                cli_shutdown(c);
                errno = ETIMEDOUT;
                return NULL;
        }
 
-        username_used = *pp_username;
+       if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
+               /* Ensure we ask for some initial credits. */
+               smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
+       }
 
-       if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
-                                              *pp_password,
-                                               strlen(*pp_password),
-                                              *pp_password,
-                                               strlen(*pp_password),
-                                              *pp_workgroup))) {
+       username_used = *pp_username;
+       password_used = *pp_password;
+
+       creds = cli_session_creds_init(c,
+                                      username_used,
+                                      *pp_workgroup,
+                                      NULL, /* realm */
+                                      password_used,
+                                      use_kerberos,
+                                      fallback_after_kerberos,
+                                      use_ccache,
+                                      pw_nt_hash);
+       if (creds == NULL) {
+               cli_shutdown(c);
+               errno = ENOMEM;
+               return NULL;
+       }
+
+       status = cli_session_setup_creds(c, creds);
+       if (!NT_STATUS_IS_OK(status)) {
 
                 /* Failed.  Try an anonymous login, if allowed by flags. */
-                username_used = "";
+               username_used = "";
+               password_used = "";
 
                 if (smbc_getOptionNoAutoAnonymousLogin(context) ||
-                    !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
-                                                       *pp_password, 1,
-                                                       *pp_password, 0,
-                                                       *pp_workgroup))) {
+                   !NT_STATUS_IS_OK(cli_session_setup_anon(c))) {
 
                         cli_shutdown(c);
                         errno = EPERM;
@@ -483,14 +543,6 @@ SMBC_server_internal(TALLOC_CTX *ctx,
                 }
        }
 
-       status = cli_init_creds(c, username_used,
-                               *pp_workgroup, *pp_password);
-       if (!NT_STATUS_IS_OK(status)) {
-               errno = map_errno_from_nt_status(status);
-               cli_shutdown(c);
-               return NULL;
-       }
-
        DEBUG(4,(" session setup ok\n"));
 
        /* here's the fun part....to support 'msdfs proxy' shares
@@ -505,9 +557,7 @@ SMBC_server_internal(TALLOC_CTX *ctx,
                                   not support smbc_smb_encrypt_level type */
                                context->internal->smb_encryption_level ?
                                        true : false,
-                               *pp_username,
-                               *pp_password,
-                               *pp_workgroup)) {
+                               creds)) {
                cli_shutdown(c);
                srv = SMBC_server_internal(ctx, context, connect_if_not_found,
                                newserver, port, newshare, pp_workgroup,
@@ -519,11 +569,10 @@ SMBC_server_internal(TALLOC_CTX *ctx,
 
        /* must be a normal share */
 
-       status = cli_tree_connect(c, share, "?????", *pp_password,
-                                 strlen(*pp_password)+1);
+       status = cli_tree_connect_creds(c, share, "?????", creds);
        if (!NT_STATUS_IS_OK(status)) {
-               errno = map_errno_from_nt_status(status);
                cli_shutdown(c);
+               errno = map_errno_from_nt_status(status);
                return NULL;
        }
 
@@ -566,11 +615,11 @@ SMBC_server_internal(TALLOC_CTX *ctx,
         }
 
        if (context->internal->smb_encryption_level) {
-               /* Attempt UNIX smb encryption. */
-               if (!NT_STATUS_IS_OK(cli_force_encryption(c,
-                                                          username_used,
-                                                          *pp_password,
-                                                          *pp_workgroup))) {
+               /* Attempt encryption. */
+               status = cli_cm_force_encryption_creds(c,
+                                                      creds,
+                                                      share);
+               if (!NT_STATUS_IS_OK(status)) {
 
                        /*
                         * context->smb_encryption_level == 1
@@ -602,10 +651,11 @@ SMBC_server_internal(TALLOC_CTX *ctx,
        }
 
        ZERO_STRUCTP(srv);
-       srv->cli = c;
+       DLIST_ADD(srv->cli, c);
        srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
         srv->no_pathinfo = False;
         srv->no_pathinfo2 = False;
+       srv->no_pathinfo3 = False;
         srv->no_nt_session = False;
 
 done:
@@ -723,6 +773,7 @@ SMBC_attr_server(TALLOC_CTX *ctx,
         ipc_srv = SMBC_find_server(ctx, context, server, "*IPC$",
                                    pp_workgroup, pp_username, pp_password);
         if (!ipc_srv) {
+               int signing_state = SMB_SIGNING_DEFAULT;
 
                 /* We didn't find a cached connection.  Get the password */
                if (!*pp_password || (*pp_password)[0] == '\0') {
@@ -744,6 +795,9 @@ SMBC_attr_server(TALLOC_CTX *ctx,
                 if (smbc_getOptionUseCCache(context)) {
                         flags |= CLI_FULL_CONNECTION_USE_CCACHE;
                 }
+               if (context->internal->smb_encryption_level != SMBC_ENCRYPTLEVEL_NONE) {
+                       signing_state = SMB_SIGNING_REQUIRED;
+               }
 
                 nt_status = cli_full_connection(&ipc_cli,
                                                lp_netbios_name(), server,
@@ -752,7 +806,7 @@ SMBC_attr_server(TALLOC_CTX *ctx,
                                                *pp_workgroup,
                                                *pp_password,
                                                flags,
-                                               SMB_SIGNING_DEFAULT);
+                                               signing_state);
                 if (! NT_STATUS_IS_OK(nt_status)) {
                         DEBUG(1,("cli_full_connection failed! (%s)\n",
                                  nt_errstr(nt_status)));
@@ -761,11 +815,13 @@ SMBC_attr_server(TALLOC_CTX *ctx,
                 }
 
                if (context->internal->smb_encryption_level) {
-                       /* Attempt UNIX smb encryption. */
-                       if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli,
-                                                                  *pp_username,
-                                                                  *pp_password,
-                                                                  *pp_workgroup))) {
+                       /* Attempt encryption. */
+                       nt_status = cli_cm_force_encryption(ipc_cli,
+                                                           *pp_username,
+                                                           *pp_password,
+                                                           *pp_workgroup,
+                                                           "IPC$");
+                       if (!NT_STATUS_IS_OK(nt_status)) {
 
                                /*
                                 * context->smb_encryption_level ==
@@ -793,7 +849,7 @@ SMBC_attr_server(TALLOC_CTX *ctx,
                 }
 
                 ZERO_STRUCTP(ipc_srv);
-                ipc_srv->cli = ipc_cli;
+                DLIST_ADD(ipc_srv->cli, ipc_cli);
 
                 nt_status = cli_rpc_pipe_open_noauth(
                        ipc_srv->cli, &ndr_table_lsarpc, &pipe_hnd);