s3:libsmb: call smbXcli_tcon_set_fs_attributes() directly
[mat/samba.git] / source3 / libsmb / libsmb_server.c
index 80faaed1af722b8b5f92972e1ad140e4c1d061ac..9d87f421fb2d84a235dd98cd77fa4ba65869f276 100644 (file)
@@ -1,10 +1,10 @@
-/* 
+/*
    Unix SMB/Netbios implementation.
    SMB client library implementation
    Copyright (C) Andrew Tridgell 1998
    Copyright (C) Richard Sharpe 2000, 2002
    Copyright (C) John Terpstra 2000
-   Copyright (C) Tom Jansen (Ninja ISD) 2002 
+   Copyright (C) Tom Jansen (Ninja ISD) 2002
    Copyright (C) Derrell Lipman 2003-2008
    Copyright (C) Jeremy Allison 2007, 2008
    Copyright (C) SATOH Fumiyasu <fumiyas@osstech.co.jp> 2009.
 #include "rpc_client/cli_lsarpc.h"
 #include "libcli/security/security.h"
 #include "libsmb/nmblib.h"
+#include "../libcli/smb/smbXcli_base.h"
 
-/* 
+/*
  * Check a server for being alive and well.
- * returns 0 if the server is in shape. Returns 1 on error 
- * 
+ * returns 0 if the server is in shape. Returns 1 on error
+ *
  * Also useable outside libsmbclient to enable external cache
  * to do some checks too.
  */
 int
 SMBC_check_server(SMBCCTX * context,
-                  SMBCSRV * server) 
+                  SMBCSRV * server)
 {
-        socklen_t size;
-        struct sockaddr addr;
+       if (!cli_state_is_connected(server->cli)) {
+               return 1;
+       }
 
-        size = sizeof(addr);
-        return (getpeername(server->cli->fd, &addr, &size) == -1);
+       return 0;
 }
 
-/* 
+/*
  * Remove a server from the cached server list it's unused.
  * On success, 0 is returned. 1 is returned if the server could not be removed.
- * 
+ *
  * Also useable outside libsmbclient
  */
 int
@@ -95,7 +96,7 @@ SMBC_remove_unused_server(SMBCCTX * context,
 /****************************************************************
  * Call the auth_fn with fixed size (fstring) buffers.
  ***************************************************************/
-void
+static void
 SMBC_call_auth_fn(TALLOC_CTX *ctx,
                   SMBCCTX *context,
                   const char *server,
@@ -198,7 +199,7 @@ check_server_cache:
                          * servers in the cache
                          */
                        if (smbc_getFunctionRemoveUnusedServer(context)(context,
-                                                                        srv)) { 
+                                                                        srv)) {
                                 /*
                                  * We could not remove the server completely,
                                  * remove it from the cache so we will not get
@@ -238,6 +239,7 @@ SMBC_server_internal(TALLOC_CTX *ctx,
             SMBCCTX *context,
             bool connect_if_not_found,
             const char *server,
+            uint16_t port,
             const char *share,
             char **pp_workgroup,
             char **pp_username,
@@ -246,13 +248,15 @@ SMBC_server_internal(TALLOC_CTX *ctx,
 {
        SMBCSRV *srv=NULL;
        char *workgroup = NULL;
-       struct cli_state *c;
+       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;
        NTSTATUS status;
        char *newserver, *newshare;
+       int flags = 0;
+       struct smbXcli_tcon *tcon = NULL;
 
        ZERO_STRUCT(c);
        *in_cache = false;
@@ -271,7 +275,7 @@ SMBC_server_internal(TALLOC_CTX *ctx,
          * server...
          */
         if (srv &&
-            *share != '\0' &&
+           share != NULL && *share != '\0' &&
             smbc_getOptionOneSharePerServer(context)) {
 
                 /*
@@ -289,10 +293,10 @@ SMBC_server_internal(TALLOC_CTX *ctx,
                 * i.e., a normal share or a referred share from
                 * 'msdfs proxy' share.
                 */
-                if (srv->cli->cnum == (uint16) -1) {
+                if (!cli_state_has_tcon(srv->cli)) {
                         /* Ensure we have accurate auth info */
                        SMBC_call_auth_fn(ctx, context,
-                                         srv->cli->desthost,
+                                         smbXcli_conn_remote_name(srv->cli->conn),
                                          srv->cli->share,
                                           pp_workgroup,
                                           pp_username,
@@ -313,9 +317,11 @@ SMBC_server_internal(TALLOC_CTX *ctx,
                         * tid.
                         */
 
-                       status = cli_tcon_andx(srv->cli, srv->cli->share, "?????",
-                                              *pp_password,
-                                              strlen(*pp_password)+1);
+                       status = cli_tree_connect(srv->cli,
+                                                 srv->cli->share,
+                                                 "?????",
+                                                 *pp_password,
+                                                 strlen(*pp_password)+1);
                        if (!NT_STATUS_IS_OK(status)) {
                                 errno = map_errno_from_nt_status(status);
                                 cli_shutdown(srv->cli);
@@ -329,10 +335,15 @@ SMBC_server_internal(TALLOC_CTX *ctx,
                         if (is_ipc) {
                                 DEBUG(4,
                                       ("IPC$ so ignore case sensitivity\n"));
-                        } else if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(c, &fs_attrs))) {
+                                status = NT_STATUS_OK;
+                        } else {
+                                status = cli_get_fs_attr_info(c, &fs_attrs);
+                        }
+
+                        if (!NT_STATUS_IS_OK(status)) {
                                 DEBUG(4, ("Could not retrieve "
                                           "case sensitivity flag: %s.\n",
-                                          cli_errstr(c)));
+                                          nt_errstr(status)));
 
                                 /*
                                  * We can't determine the case sensitivity of
@@ -344,7 +355,7 @@ SMBC_server_internal(TALLOC_CTX *ctx,
                                 } else {
                                         cli_set_case_sensitive(c, False);
                                 }
-                        } else {
+                        } else if (!is_ipc) {
                                 DEBUG(4,
                                       ("Case sensitive: %s\n",
                                        (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
@@ -362,7 +373,10 @@ SMBC_server_internal(TALLOC_CTX *ctx,
                          * server and share
                          */
                         if (srv) {
-                                srv->dev = (dev_t)(str_checksum(srv->cli->desthost) ^
+                               const char *remote_name =
+                                       smbXcli_conn_remote_name(srv->cli->conn);
+
+                               srv->dev = (dev_t)(str_checksum(remote_name) ^
                                                    str_checksum(srv->cli->share));
                         }
                 }
@@ -393,22 +407,40 @@ SMBC_server_internal(TALLOC_CTX *ctx,
 
        status = NT_STATUS_UNSUCCESSFUL;
 
-        if (share == NULL || *share == '\0' || is_ipc) {
-               /*
-                * Try 139 first for IPC$
-                */
-               status = cli_connect_nb(server_n, NULL, 139, 0x20,
+       if (smbc_getOptionUseKerberos(context)) {
+               flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
+       }
+
+       if (smbc_getOptionFallbackAfterKerberos(context)) {
+               flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
+       }
+
+       if (smbc_getOptionUseCCache(context)) {
+               flags |= CLI_FULL_CONNECTION_USE_CCACHE;
+       }
+
+       if (smbc_getOptionUseNTHash(context)) {
+               flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
+       }
+
+       if (port == 0) {
+               if (share == NULL || *share == '\0' || is_ipc) {
+                       /*
+                        * Try 139 first for IPC$
+                        */
+                       status = cli_connect_nb(server_n, NULL, NBT_SMB_PORT, 0x20,
                                        smbc_getNetbiosName(context),
-                                       Undefined, &c);
+                                       SMB_SIGNING_DEFAULT, flags, &c);
+               }
        }
 
        if (!NT_STATUS_IS_OK(status)) {
                /*
                 * No IPC$ or 139 did not work
                 */
-               status = cli_connect_nb(server_n, NULL, 0, 0x20,
+               status = cli_connect_nb(server_n, NULL, port, 0x20,
                                        smbc_getNetbiosName(context),
-                                       Undefined, &c);
+                                       SMB_SIGNING_DEFAULT, flags, &c);
        }
 
        if (!NT_STATUS_IS_OK(status)) {
@@ -416,22 +448,11 @@ SMBC_server_internal(TALLOC_CTX *ctx,
                return NULL;
        }
 
-        if (smbc_getOptionUseKerberos(context)) {
-               c->use_kerberos = True;
-       }
-
-        if (smbc_getOptionFallbackAfterKerberos(context)) {
-               c->fallback_after_kerberos = True;
-       }
-
-        if (smbc_getOptionUseCCache(context)) {
-               c->use_ccache = True;
-       }
-
-       c->timeout = smbc_getTimeout(context);
-
-       status = cli_negprot(c);
+       cli_set_timeout(c, smbc_getTimeout(context));
 
+       status = smbXcli_negprot(c->conn, c->timeout,
+                                lp_cli_minprotocol(),
+                                lp_cli_maxprotocol());
        if (!NT_STATUS_IS_OK(status)) {
                cli_shutdown(c);
                errno = ETIMEDOUT;
@@ -477,7 +498,7 @@ SMBC_server_internal(TALLOC_CTX *ctx,
           here before trying to connect to the original share.
           cli_check_msdfs_proxy() will fail if it is a normal share. */
 
-       if ((c->capabilities & CAP_DFS) &&
+       if (smbXcli_conn_dfs_supported(c->conn) &&
                        cli_check_msdfs_proxy(ctx, c, share,
                                &newserver, &newshare,
                                /* FIXME: cli_check_msdfs_proxy() does
@@ -489,7 +510,7 @@ SMBC_server_internal(TALLOC_CTX *ctx,
                                *pp_workgroup)) {
                cli_shutdown(c);
                srv = SMBC_server_internal(ctx, context, connect_if_not_found,
-                               newserver, newshare, pp_workgroup,
+                               newserver, port, newshare, pp_workgroup,
                                pp_username, pp_password, in_cache);
                TALLOC_FREE(newserver);
                TALLOC_FREE(newshare);
@@ -498,8 +519,8 @@ SMBC_server_internal(TALLOC_CTX *ctx,
 
        /* must be a normal share */
 
-       status = cli_tcon_andx(c, share, "?????", *pp_password,
-                              strlen(*pp_password)+1);
+       status = cli_tree_connect(c, share, "?????", *pp_password,
+                                 strlen(*pp_password)+1);
        if (!NT_STATUS_IS_OK(status)) {
                errno = map_errno_from_nt_status(status);
                cli_shutdown(c);
@@ -508,12 +529,23 @@ SMBC_server_internal(TALLOC_CTX *ctx,
 
        DEBUG(4,(" tconx ok\n"));
 
+       if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
+               tcon = c->smb2.tcon;
+       } else {
+               tcon = c->smb1.tcon;
+       }
+
         /* Determine if this share supports case sensitivity */
        if (is_ipc) {
                 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
-        } else if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(c, &fs_attrs))) {
+                status = NT_STATUS_OK;
+        } else {
+                status = cli_get_fs_attr_info(c, &fs_attrs);
+        }
+
+        if (!NT_STATUS_IS_OK(status)) {
                 DEBUG(4, ("Could not retrieve case sensitivity flag: %s.\n",
-                          cli_errstr(c)));
+                          nt_errstr(status)));
 
                 /*
                  * We can't determine the case sensitivity of the share. We
@@ -525,15 +557,12 @@ SMBC_server_internal(TALLOC_CTX *ctx,
                 } else {
                         cli_set_case_sensitive(c, False);
                 }
-       } else {
+       } else if (!is_ipc) {
                 DEBUG(4, ("Case sensitive: %s\n",
                           (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
                            ? "True"
                            : "False")));
-                cli_set_case_sensitive(c,
-                                       (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
-                                        ? True
-                                        : False));
+               smbXcli_tcon_set_fs_attributes(tcon, fs_attrs);
         }
 
        if (context->internal->smb_encryption_level) {
@@ -586,6 +615,10 @@ done:
                workgroup = *pp_workgroup;
        }
        if(!workgroup) {
+               if (c != NULL) {
+                       cli_shutdown(c);
+               }
+               SAFE_FREE(srv);
                return NULL;
        }
 
@@ -603,6 +636,7 @@ SMBC_server(TALLOC_CTX *ctx,
                SMBCCTX *context,
                bool connect_if_not_found,
                const char *server,
+               uint16_t port,
                const char *share,
                char **pp_workgroup,
                char **pp_username,
@@ -612,7 +646,7 @@ SMBC_server(TALLOC_CTX *ctx,
        bool in_cache = false;
 
        srv = SMBC_server_internal(ctx, context, connect_if_not_found,
-                       server, share, pp_workgroup,
+                       server, port, share, pp_workgroup,
                        pp_username, pp_password, &in_cache);
 
        if (!srv) {
@@ -654,13 +688,13 @@ SMBCSRV *
 SMBC_attr_server(TALLOC_CTX *ctx,
                  SMBCCTX *context,
                  const char *server,
+                 uint16_t port,
                  const char *share,
                  char **pp_workgroup,
                  char **pp_username,
                  char **pp_password)
 {
         int flags;
-        struct sockaddr_storage ss;
        struct cli_state *ipc_cli = NULL;
        struct rpc_pipe_client *pipe_hnd = NULL;
         NTSTATUS nt_status;
@@ -673,12 +707,12 @@ SMBC_attr_server(TALLOC_CTX *ctx,
         * i.e., a normal share or a referred share from
         * 'msdfs proxy' share.
         */
-       srv = SMBC_server(ctx, context, true, server, share,
+       srv = SMBC_server(ctx, context, true, server, port, share,
                        pp_workgroup, pp_username, pp_password);
        if (!srv) {
                return NULL;
        }
-       server = srv->cli->desthost;
+       server = smbXcli_conn_remote_name(srv->cli->conn);
        share = srv->cli->share;
 
         /*
@@ -711,15 +745,14 @@ SMBC_attr_server(TALLOC_CTX *ctx,
                         flags |= CLI_FULL_CONNECTION_USE_CCACHE;
                 }
 
-                zero_sockaddr(&ss);
                 nt_status = cli_full_connection(&ipc_cli,
-                                               global_myname(), server,
-                                               &ss, 0, "IPC$", "?????",
+                                               lp_netbios_name(), server,
+                                               NULL, 0, "IPC$", "?????",
                                                *pp_username,
                                                *pp_workgroup,
                                                *pp_password,
                                                flags,
-                                               Undefined);
+                                               SMB_SIGNING_DEFAULT);
                 if (! NT_STATUS_IS_OK(nt_status)) {
                         DEBUG(1,("cli_full_connection failed! (%s)\n",
                                  nt_errstr(nt_status)));
@@ -763,7 +796,7 @@ SMBC_attr_server(TALLOC_CTX *ctx,
                 ipc_srv->cli = ipc_cli;
 
                 nt_status = cli_rpc_pipe_open_noauth(
-                       ipc_srv->cli, &ndr_table_lsarpc.syntax_id, &pipe_hnd);
+                       ipc_srv->cli, &ndr_table_lsarpc, &pipe_hnd);
                 if (!NT_STATUS_IS_OK(nt_status)) {
                         DEBUG(1, ("cli_nt_session_open fail!\n"));
                         errno = ENOTSUP;
@@ -788,6 +821,7 @@ SMBC_attr_server(TALLOC_CTX *ctx,
                 if (!NT_STATUS_IS_OK(nt_status)) {
                         errno = SMBC_errno(context, ipc_srv->cli);
                         cli_shutdown(ipc_srv->cli);
+                        free(ipc_srv);
                         return NULL;
                 }