s3:libsmb: Use cli_cm_force_encryption() instead of cli_force_encryption()
[samba.git] / source3 / libsmb / libsmb_server.c
index a258802df40a1390ff2ae8711ee9c434f302b94f..e0cdc97eeb68c0e4fbfc0c76d15f600206c5b92f 100644 (file)
@@ -45,10 +45,26 @@ 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)) {
+                       return 1;
+               }
+               server->last_echo_time = now;
+       }
        return 0;
 }
 
@@ -105,14 +121,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 +273,14 @@ 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;
 
        ZERO_STRUCT(c);
        *in_cache = false;
@@ -320,8 +344,7 @@ 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);
@@ -423,6 +446,10 @@ SMBC_server_internal(TALLOC_CTX *ctx,
                flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
        }
 
+       if (context->internal->smb_encryption_level != SMBC_ENCRYPTLEVEL_NONE) {
+               signing_state = SMB_SIGNING_REQUIRED;
+       }
+
        if (port == 0) {
                if (share == NULL || *share == '\0' || is_ipc) {
                        /*
@@ -430,7 +457,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 +467,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)) {
@@ -464,22 +491,20 @@ SMBC_server_internal(TALLOC_CTX *ctx,
                smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
        }
 
-        username_used = *pp_username;
+       username_used = *pp_username;
+       password_used = *pp_password;
 
        if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
-                                              *pp_password,
-                                               strlen(*pp_password),
-                                              *pp_password,
-                                               strlen(*pp_password),
+                                              password_used,
                                               *pp_workgroup))) {
 
                 /* 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,
+                   !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
+                                                      password_used,
                                                        *pp_workgroup))) {
 
                         cli_shutdown(c);
@@ -488,13 +513,6 @@ SMBC_server_internal(TALLOC_CTX *ctx,
                 }
        }
 
-       status = cli_set_username(c, username_used);
-       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
@@ -523,8 +541,7 @@ 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(c, share, "?????", *pp_password);
        if (!NT_STATUS_IS_OK(status)) {
                errno = map_errno_from_nt_status(status);
                cli_shutdown(c);
@@ -570,11 +587,13 @@ 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(c,
+                                                username_used,
+                                                password_used,
+                                                *pp_workgroup,
+                                                share);
+               if (!NT_STATUS_IS_OK(status)) {
 
                        /*
                         * context->smb_encryption_level == 1
@@ -606,7 +625,7 @@ 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;
@@ -728,6 +747,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') {
@@ -749,6 +769,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,
@@ -757,7 +780,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)));
@@ -766,11 +789,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 ==
@@ -798,7 +823,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);