r13924: Split more prototypes out of include/proto.h + initial work on header
[bbaumbach/samba-autobuild/.git] / source4 / libcli / cliconnect.c
index da8a842dae936415f322f01b751cc0ebf6d00311..8616e42cd48b0bdc5a551631f2118084d3544a91 100644 (file)
@@ -1,7 +1,10 @@
 /*
    Unix SMB/CIFS implementation.
+
    client connect/disconnect routines
-   Copyright (C) Andrew Tridgell 2003
+
+   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
 */
 
 #include "includes.h"
+#include "libcli/libcli.h"
+#include "libcli/raw/libcliraw.h"
+#include "libcli/auth/proto.h"
+#include "libcli/smb_composite/smb_composite.h"
 
 /*
-  wrapper around cli_sock_connect()
+  wrapper around smbcli_sock_connect()
 */
-BOOL cli_socket_connect(struct cli_state *cli, const char *server, struct in_addr *ip)
+BOOL smbcli_socket_connect(struct smbcli_state *cli, const char *server)
 {
-       struct cli_socket *sock;
+       struct smbcli_socket *sock;
 
-       sock = cli_sock_init();
-       if (!sock) return False;
+       sock = smbcli_sock_connect_byname(server, 0, NULL, NULL);
 
-       if (!cli_sock_connect_byname(sock, server, 0)) {
-               cli_sock_close(sock);
-               return False;
-       }
+       if (sock == NULL) return False;
        
-       cli->transport = cli_transport_init(sock);
+       cli->transport = smbcli_transport_init(sock, cli, True);
        if (!cli->transport) {
-               cli_sock_close(sock);
                return False;
        }
 
        return True;
 }
 
-/* wrapper around cli_transport_connect() */
-BOOL cli_transport_establish(struct cli_state *cli, 
-                            struct nmb_name *calling,
-                            struct nmb_name *called)
+/* wrapper around smbcli_transport_connect() */
+BOOL smbcli_transport_establish(struct smbcli_state *cli, 
+                               struct nbt_name *calling,
+                               struct nbt_name *called)
 {
-       return cli_transport_connect(cli->transport, calling, called);
+       return smbcli_transport_connect(cli->transport, calling, called);
 }
 
 /* wrapper around smb_raw_negotiate() */
-BOOL cli_negprot(struct cli_state *cli)
+NTSTATUS smbcli_negprot(struct smbcli_state *cli)
 {
-       NTSTATUS status;
-       status = smb_raw_negotiate(cli->transport);
-       return NT_STATUS_IS_OK(status);
+       return smb_raw_negotiate(cli->transport, lp_maxprotocol());
 }
 
-/* wrapper around smb_raw_session_setup() */
-BOOL cli_session_setup(struct cli_state *cli, 
-                      const char *user, 
-                      const char *password, 
-                      const char *domain)
+/* wrapper around smb_raw_sesssetup() */
+NTSTATUS smbcli_session_setup(struct smbcli_state *cli, 
+                             struct cli_credentials *credentials)
 {
-       union smb_sesssetup setup;
+       struct smb_composite_sesssetup setup;
        NTSTATUS status;
-       TALLOC_CTX *mem_ctx;
 
-       cli->session = cli_session_init(cli->transport);
-       if (!cli->session) return False;
+       cli->session = smbcli_session_init(cli->transport, cli, True);
+       if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
 
-       mem_ctx = talloc_init("cli_session_setup");
-       if (!mem_ctx) return False;
+       setup.in.sesskey = cli->transport->negotiate.sesskey;
+       setup.in.capabilities = cli->transport->negotiate.capabilities;
+       setup.in.credentials = credentials;
+       setup.in.workgroup = lp_workgroup();
 
-       setup.generic.level = RAW_SESSSETUP_GENERIC;
-       setup.generic.in.sesskey = cli->transport->negotiate.sesskey;
-       setup.generic.in.capabilities = CAP_UNICODE | CAP_STATUS32 | 
-               CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS | 
-               CAP_W2K_SMBS | CAP_LARGE_READX | CAP_LARGE_WRITEX;
-       setup.generic.in.password = password;
-       setup.generic.in.user = user;
-       setup.generic.in.domain = domain;
+       status = smb_composite_sesssetup(cli->session, &setup);
 
-       status = smb_raw_session_setup(cli->session, mem_ctx, &setup);
+       cli->session->vuid = setup.out.vuid;
 
-       cli->session->vuid = setup.generic.out.vuid;
-
-       talloc_destroy(mem_ctx);
-
-       return NT_STATUS_IS_OK(status);
+       return status;
 }
 
-/* wrapper around smb_tree_connect() */
-BOOL cli_send_tconX(struct cli_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 = cli_tree_init(cli->session);
-       if (!cli->tree) return False;
+       cli->tree = smbcli_tree_init(cli->session, cli, True);
+       if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
 
-       cli->tree->reference_count++;
+       mem_ctx = talloc_init("tcon");
+       if (!mem_ctx) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
        /* setup a tree connect */
        tcon.generic.level = RAW_TCON_TCONX;
        tcon.tconx.in.flags = 0;
-       tcon.tconx.in.password = data_blob(password, strlen(password)+1);
+       if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
+               tcon.tconx.in.password = data_blob(NULL, 0);
+       } else if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
+               tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24);
+               if (cli->transport->negotiate.secblob.length < 8) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+               SMBencrypt(password, cli->transport->negotiate.secblob.data, tcon.tconx.in.password.data);
+       } else {
+               tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1);
+       }
        tcon.tconx.in.path = sharename;
        tcon.tconx.in.device = devtype;
        
-       mem_ctx = talloc_init("tcon");
-       if (!mem_ctx) {
-               return False;
-       }
-
-       status = smb_tree_connect(cli->tree, mem_ctx, &tcon);
+       status = smb_raw_tcon(cli->tree, mem_ctx, &tcon);
 
-       cli->tree->tid = tcon.tconx.out.cnum;
+       cli->tree->tid = tcon.tconx.out.tid;
 
-       talloc_destroy(mem_ctx);
+       talloc_free(mem_ctx);
 
-       return NT_STATUS_IS_OK(status);
+       return status;
 }
 
 
 /*
-  easy way to get to a fully connected cli_state in one call
+  easy way to get to a fully connected smbcli_state in one call
 */
-NTSTATUS cli_full_connection(struct cli_state **ret_cli, 
-                            const char *myname,
-                            const char *host,
-                            struct in_addr *ip,
-                            const char *sharename,
-                            const char *devtype,
-                            const char *username,
-                            const char *domain,
-                            const char *password,
-                            uint_t flags,
-                            BOOL *retry)
+NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
+                               struct smbcli_state **ret_cli, 
+                               const char *host,
+                               const char *sharename,
+                               const char *devtype,
+                               struct cli_credentials *credentials,
+                               struct event_context *ev)
 {
-       struct cli_tree *tree;
+       struct smbcli_tree *tree;
        NTSTATUS status;
 
        *ret_cli = NULL;
 
-       status = cli_tree_full_connection(&tree, myname, host, 0, sharename, devtype,
-                                         username, domain, password);
+       status = smbcli_tree_full_connection(parent_ctx,
+                                            &tree, host, 0, sharename, devtype,
+                                            credentials, ev);
        if (!NT_STATUS_IS_OK(status)) {
-               return status;
+               goto done;
        }
 
-       (*ret_cli) = cli_state_init();
+       (*ret_cli) = smbcli_state_init(parent_ctx);
 
        (*ret_cli)->tree = tree;
        (*ret_cli)->session = tree->session;
        (*ret_cli)->transport = tree->session->transport;
-       tree->reference_count++;
 
+       talloc_steal(*ret_cli, tree);
+       
+done:
        return status;
 }
 
@@ -169,39 +164,77 @@ NTSTATUS cli_full_connection(struct cli_state **ret_cli,
 /*
   disconnect the tree
 */
-BOOL cli_tdis(struct cli_state *cli)
+NTSTATUS smbcli_tdis(struct smbcli_state *cli)
 {
-       NTSTATUS status;
-       status = smb_tree_disconnect(cli->tree);
-       return NT_STATUS_IS_OK(status);
+       return smb_tree_disconnect(cli->tree);
 }
 
 /****************************************************************************
  Initialise a client state structure.
 ****************************************************************************/
-struct cli_state *cli_state_init(void)
+struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
 {
-       struct cli_state *cli;
-       TALLOC_CTX *mem_ctx;
+       return talloc_zero(mem_ctx, struct smbcli_state);
+}
 
-       mem_ctx = talloc_init("cli_state");
-       if (!mem_ctx) return NULL;
+/* 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;
+
+       if (!path) {
+               return NULL;
+       }
 
-       cli = talloc_zero(mem_ctx, sizeof(*cli));
-       cli->mem_ctx = mem_ctx;
+       if ((p = strchr_m(path, '/'))) {
+               *p = '\0';
+               return p + 1;
+       }
 
-       return cli;
+       if ((p = strchr_m(path, '\\'))) {
+               *p = '\0';
+               return p + 1;
+       }
+       
+       /* No separator. */
+       return NULL;
 }
 
-/****************************************************************************
- Shutdown a client structure.
-****************************************************************************/
-void cli_shutdown(struct cli_state *cli)
+/*
+  parse a //server/share type UNC name
+*/
+BOOL smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
+                     char **hostname, char **sharename)
 {
-       if (!cli) return;
-       cli->tree->reference_count++;
-       cli_tree_close(cli->tree);
-       if (cli->mem_ctx) {
-               talloc_destroy(cli->mem_ctx);
+       char *p;
+
+       *hostname = *sharename = NULL;
+
+       if (strncmp(unc_name, "\\\\", 2) &&
+           strncmp(unc_name, "//", 2)) {
+               return False;
+       }
+
+       *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
+       p = terminate_path_at_separator(*hostname);
+
+       if (p && *p) {
+               *sharename = talloc_strdup(mem_ctx, p);
+               terminate_path_at_separator(*sharename);
        }
+
+       if (*hostname && *sharename) {
+               return True;
+       }
+
+       talloc_free(*hostname);
+       talloc_free(*sharename);
+       *hostname = *sharename = NULL;
+       return False;
 }
+
+
+