Add context for libcli_resolve.
[samba-svnmirror.git] / source / libcli / cliconnect.c
index 624b54c3f2a671df1921d23b23c080e56fd194ef..d70f592d9d1ef495a787a448a1ec416711e59c66 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/auth/libcli_auth.h"
+#include "libcli/smb_composite/smb_composite.h"
+#include "param/param.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, 
+                          struct resolve_context *resolve_ctx,
+                          int max_xmit, int max_mux)
 {
        struct smbcli_socket *sock;
 
-       sock = smbcli_sock_init(cli, NULL);
-       if (!sock) return False;
+       sock = smbcli_sock_connect_byname(server, 0, NULL, resolve_ctx, 
+                                         NULL);
 
-       if (!smbcli_sock_connect_byname(sock, server, 0)) {
-               talloc_free(sock);
-               return False;
-       }
+       if (sock == NULL) return false;
        
-       cli->transport = smbcli_transport_init(sock, cli, True);
+       cli->transport = smbcli_transport_init(sock, cli, true, max_xmit,
+                                              max_mux);
        if (!cli->transport) {
-               return False;
+               return false;
        }
 
-       return True;
+       return true;
 }
 
 /* wrapper around smbcli_transport_connect() */
-BOOL smbcli_transport_establish(struct smbcli_state *cli, 
+bool smbcli_transport_establish(struct smbcli_state *cli, 
                                struct nbt_name *calling,
                                struct nbt_name *called)
 {
@@ -56,25 +59,25 @@ BOOL smbcli_transport_establish(struct smbcli_state *cli,
 }
 
 /* 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());
+       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 smb_composite_sesssetup setup;
        NTSTATUS status;
 
-       cli->session = smbcli_session_init(cli->transport, cli, True);
+       cli->session = smbcli_session_init(cli->transport, cli, true);
        if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
 
        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 = lp_workgroup(global_loadparm);
 
        status = smb_composite_sesssetup(cli->session, &setup);
 
@@ -83,7 +86,7 @@ NTSTATUS smbcli_session_setup(struct smbcli_state *cli,
        return status;
 }
 
-/* wrapper around smb_tree_connect() */
+/* wrapper around smb_raw_tcon() */
 NTSTATUS smbcli_tconX(struct smbcli_state *cli, const char *sharename, 
                      const char *devtype, const char *password)
 {
@@ -91,7 +94,7 @@ NTSTATUS smbcli_tconX(struct smbcli_state *cli, const char *sharename,
        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");
@@ -116,7 +119,7 @@ NTSTATUS smbcli_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;
 
@@ -178,29 +181,63 @@ struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
        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;
+
+       if (!path) {
+               return NULL;
+       }
+
+       if ((p = strchr_m(path, '/'))) {
+               *p = '\0';
+               return p + 1;
+       }
+
+       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;
 
+       *hostname = *sharename = NULL;
+
        if (strncmp(unc_name, "\\\\", 2) &&
            strncmp(unc_name, "//", 2)) {
-               return False;
+               return false;
        }
 
        *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;
 }