s4:libcli/raw: implement on top of smbXcli_conn/req
[amitay/samba.git] / source4 / libcli / cliconnect.c
index 53a97da168e10a7d92b8ee96e84f9451aa4d01bb..45d8d2a77649fb01bd16664528a5c278866dd421 100644 (file)
@@ -4,10 +4,11 @@
    client connect/disconnect routines
 
    Copyright (C) Andrew Tridgell 2003-2005
+   Copyright (C) James Peach 2005
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
+#include "libcli/libcli.h"
 #include "libcli/raw/libcliraw.h"
-#include "libcli/composite/composite.h"
+#include "libcli/raw/raw_proto.h"
+#include "libcli/auth/libcli_auth.h"
+#include "libcli/smb_composite/smb_composite.h"
 
 /*
   wrapper around smbcli_sock_connect()
 */
-BOOL smbcli_socket_connect(struct smbcli_state *cli, const char *server)
+bool smbcli_socket_connect(struct smbcli_state *cli, const char *server, 
+                          const char **ports, 
+                          struct tevent_context *ev_ctx,
+                          struct resolve_context *resolve_ctx,
+                          struct smbcli_options *options,
+                          const char *socket_options,
+                          struct nbt_name *calling,
+                          struct nbt_name *called)
 {
-       struct smbcli_socket *sock;
-
-       sock = smbcli_sock_init(cli, NULL);
-       if (!sock) return False;
+       NTSTATUS status;
 
-       if (!smbcli_sock_connect_byname(sock, server, 0)) {
-               talloc_free(sock);
-               return False;
-       }
-       
-       cli->transport = smbcli_transport_init(sock, cli, True);
-       if (!cli->transport) {
-               return False;
+       cli->options = *options;
+
+       status = smbcli_sock_connect(cli,
+                                    NULL, /* host_addr */
+                                    ports,
+                                    server,
+                                    resolve_ctx,
+                                    ev_ctx,
+                                    socket_options,
+                                    calling,
+                                    called,
+                                    &cli->sock);
+       if (!NT_STATUS_IS_OK(status)) {
+               return false;
        }
 
-       return True;
-}
-
-/* wrapper around smbcli_transport_connect() */
-BOOL smbcli_transport_establish(struct smbcli_state *cli, 
-                               struct nbt_name *calling,
-                               struct nbt_name *called)
-{
-       return smbcli_transport_connect(cli->transport, calling, called);
+       return true;
 }
 
 /* wrapper around smb_raw_negotiate() */
-NTSTATUS smbcli_negprot(struct smbcli_state *cli)
+NTSTATUS smbcli_negprot(struct smbcli_state *cli, bool unicode, int maxprotocol)
 {
-       return smb_raw_negotiate(cli->transport, lp_maxprotocol());
+       if (unicode) {
+               cli->options.unicode = 1;
+       } else {
+               cli->options.unicode = 0;
+       }
+
+       cli->transport = smbcli_transport_init(cli->sock, cli,
+                                              true, &cli->options);
+       cli->sock = NULL;
+       if (!cli->transport) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       return smb_raw_negotiate(cli->transport, unicode, maxprotocol);
 }
 
-/* wrapper around smb_raw_session_setup() */
+/* wrapper around smb_raw_sesssetup() */
 NTSTATUS smbcli_session_setup(struct smbcli_state *cli, 
-                             struct cli_credentials *credentials)
+                             struct cli_credentials *credentials,
+                             const char *workgroup,
+                             struct smbcli_session_options options,
+                             struct gensec_settings *gensec_settings)
 {
        struct smb_composite_sesssetup setup;
        NTSTATUS status;
-       TALLOC_CTX *mem_ctx;
 
-       cli->session = smbcli_session_init(cli->transport, cli, True);
+       cli->session = smbcli_session_init(cli->transport, cli, true,
+                                          options);
        if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
 
-       mem_ctx = talloc_init("smbcli_session_setup");
-       if (!mem_ctx) return NT_STATUS_NO_MEMORY;
-
        setup.in.sesskey = cli->transport->negotiate.sesskey;
        setup.in.capabilities = cli->transport->negotiate.capabilities;
        setup.in.credentials = credentials;
-       setup.in.workgroup = lp_workgroup();
+       setup.in.workgroup = workgroup;
+       setup.in.gensec_settings = gensec_settings;
 
        status = smb_composite_sesssetup(cli->session, &setup);
 
        cli->session->vuid = setup.out.vuid;
 
-       talloc_free(mem_ctx);
-
        return status;
 }
 
-/* wrapper around smb_tree_connect() */
-NTSTATUS smbcli_send_tconX(struct smbcli_state *cli, const char *sharename, 
-                          const char *devtype, const char *password)
+/* wrapper around smb_raw_tcon() */
+NTSTATUS smbcli_tconX(struct smbcli_state *cli, const char *sharename, 
+                     const char *devtype, const char *password)
 {
        union smb_tcon tcon;
        TALLOC_CTX *mem_ctx;
        NTSTATUS status;
 
-       cli->tree = smbcli_tree_init(cli->session, cli, True);
+       cli->tree = smbcli_tree_init(cli->session, cli, true);
        if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
 
        mem_ctx = talloc_init("tcon");
@@ -122,7 +139,7 @@ NTSTATUS smbcli_send_tconX(struct smbcli_state *cli, const char *sharename,
        tcon.tconx.in.path = sharename;
        tcon.tconx.in.device = devtype;
        
-       status = smb_tree_connect(cli->tree, mem_ctx, &tcon);
+       status = smb_raw_tcon(cli->tree, mem_ctx, &tcon);
 
        cli->tree->tid = tcon.tconx.out.tid;
 
@@ -138,21 +155,30 @@ NTSTATUS smbcli_send_tconX(struct smbcli_state *cli, const char *sharename,
 NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
                                struct smbcli_state **ret_cli, 
                                const char *host,
+                               const char **ports,
                                const char *sharename,
                                const char *devtype,
-                               struct cli_credentials *credentials)
+                               const char *socket_options,
+                               struct cli_credentials *credentials,
+                               struct resolve_context *resolve_ctx,
+                               struct tevent_context *ev,
+                               struct smbcli_options *options,
+                               struct smbcli_session_options *session_options,
+                               struct gensec_settings *gensec_settings)
 {
        struct smbcli_tree *tree;
        NTSTATUS status;
-       TALLOC_CTX *mem_ctx;
-
-       mem_ctx = talloc_init("smbcli_full_connection");
 
        *ret_cli = NULL;
 
        status = smbcli_tree_full_connection(parent_ctx,
-                                            &tree, host, 0, sharename, devtype,
-                                            credentials);
+                                            &tree, host, ports, 
+                                            sharename, devtype,
+                                                socket_options,
+                                            credentials, resolve_ctx, ev,
+                                            options,
+                                            session_options,
+                                                gensec_settings);
        if (!NT_STATUS_IS_OK(status)) {
                goto done;
        }
@@ -162,10 +188,10 @@ NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
        (*ret_cli)->tree = tree;
        (*ret_cli)->session = tree->session;
        (*ret_cli)->transport = tree->session->transport;
+
+       talloc_steal(*ret_cli, tree);
        
 done:
-       talloc_free(mem_ctx);
-
        return status;
 }
 
@@ -183,47 +209,66 @@ NTSTATUS smbcli_tdis(struct smbcli_state *cli)
 ****************************************************************************/
 struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
 {
-       struct smbcli_state *cli;
+       return talloc_zero(mem_ctx, struct smbcli_state);
+}
+
+/* Insert a NULL at the first separator of the given path and return a pointer
+ * to the remainder of the string.
+ */
+static char *
+terminate_path_at_separator(char * path)
+{
+       char * p;
 
-       cli = talloc_zero(mem_ctx, struct smbcli_state);
-       if (cli) {
-               ZERO_STRUCTP(cli);
+       if (!path) {
+               return NULL;
        }
 
-       return cli;
-}
+       if ((p = strchr_m(path, '/'))) {
+               *p = '\0';
+               return p + 1;
+       }
 
-/****************************************************************************
- Shutdown a client structure.
-****************************************************************************/
-void smbcli_shutdown(struct smbcli_state *cli)
-{
-       talloc_free(cli);
+       if ((p = strchr_m(path, '\\'))) {
+               *p = '\0';
+               return p + 1;
+       }
+       
+       /* No separator. */
+       return NULL;
 }
 
 /*
   parse a //server/share type UNC name
 */
-BOOL smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
-                     const char **hostname, const char **sharename)
+bool smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
+                     char **hostname, char **sharename)
 {
        char *p;
 
        if (strncmp(unc_name, "\\\\", 2) &&
            strncmp(unc_name, "//", 2)) {
-               return False;
+               return false;
        }
 
+       *hostname = *sharename = NULL;
+
        *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
-       p = strchr_m(&(*hostname)[2],'/');
-       if (!p) {
-               p = strchr_m(&(*hostname)[2],'\\');
-               if (!p) return False;
+       p = terminate_path_at_separator(*hostname);
+
+       if (p != NULL && *p) {
+               *sharename = talloc_strdup(mem_ctx, p);
+               terminate_path_at_separator(*sharename);
+       }
+
+       if (*hostname && *sharename) {
+               return true;
        }
-       *p = 0;
-       *sharename = talloc_strdup(mem_ctx, p+1);
 
-       return True;
+       talloc_free(*hostname);
+       talloc_free(*sharename);
+       *hostname = *sharename = NULL;
+       return false;
 }