2 Unix SMB/CIFS implementation.
4 client connect/disconnect routines
6 Copyright (C) Andrew Tridgell 2003-2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/smb_composite/smb_composite.h"
28 wrapper around smbcli_sock_connect()
30 BOOL smbcli_socket_connect(struct smbcli_state *cli, const char *server)
32 struct smbcli_socket *sock;
34 sock = smbcli_sock_connect_byname(server, 0, NULL, NULL);
36 if (sock == NULL) return False;
38 cli->transport = smbcli_transport_init(sock, cli, True);
39 if (!cli->transport) {
46 /* wrapper around smbcli_transport_connect() */
47 BOOL smbcli_transport_establish(struct smbcli_state *cli,
48 struct nbt_name *calling,
49 struct nbt_name *called)
51 return smbcli_transport_connect(cli->transport, calling, called);
54 /* wrapper around smb_raw_negotiate() */
55 NTSTATUS smbcli_negprot(struct smbcli_state *cli)
57 return smb_raw_negotiate(cli->transport, lp_maxprotocol());
60 /* wrapper around smb_raw_sesssetup() */
61 NTSTATUS smbcli_session_setup(struct smbcli_state *cli,
62 struct cli_credentials *credentials)
64 struct smb_composite_sesssetup setup;
67 cli->session = smbcli_session_init(cli->transport, cli, True);
68 if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
70 setup.in.sesskey = cli->transport->negotiate.sesskey;
71 setup.in.capabilities = cli->transport->negotiate.capabilities;
72 setup.in.credentials = credentials;
73 setup.in.workgroup = lp_workgroup();
75 status = smb_composite_sesssetup(cli->session, &setup);
77 cli->session->vuid = setup.out.vuid;
82 /* wrapper around smb_raw_tcon() */
83 NTSTATUS smbcli_tconX(struct smbcli_state *cli, const char *sharename,
84 const char *devtype, const char *password)
90 cli->tree = smbcli_tree_init(cli->session, cli, True);
91 if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
93 mem_ctx = talloc_init("tcon");
95 return NT_STATUS_NO_MEMORY;
98 /* setup a tree connect */
99 tcon.generic.level = RAW_TCON_TCONX;
100 tcon.tconx.in.flags = 0;
101 if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
102 tcon.tconx.in.password = data_blob(NULL, 0);
103 } else if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
104 tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24);
105 if (cli->transport->negotiate.secblob.length < 8) {
106 return NT_STATUS_INVALID_PARAMETER;
108 SMBencrypt(password, cli->transport->negotiate.secblob.data, tcon.tconx.in.password.data);
110 tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1);
112 tcon.tconx.in.path = sharename;
113 tcon.tconx.in.device = devtype;
115 status = smb_raw_tcon(cli->tree, mem_ctx, &tcon);
117 cli->tree->tid = tcon.tconx.out.tid;
119 talloc_free(mem_ctx);
126 easy way to get to a fully connected smbcli_state in one call
128 NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
129 struct smbcli_state **ret_cli,
131 const char *sharename,
133 struct cli_credentials *credentials,
134 struct event_context *ev)
136 struct smbcli_tree *tree;
141 status = smbcli_tree_full_connection(parent_ctx,
142 &tree, host, 0, sharename, devtype,
144 if (!NT_STATUS_IS_OK(status)) {
148 (*ret_cli) = smbcli_state_init(parent_ctx);
150 (*ret_cli)->tree = tree;
151 (*ret_cli)->session = tree->session;
152 (*ret_cli)->transport = tree->session->transport;
154 talloc_steal(*ret_cli, tree);
164 NTSTATUS smbcli_tdis(struct smbcli_state *cli)
166 return smb_tree_disconnect(cli->tree);
169 /****************************************************************************
170 Initialise a client state structure.
171 ****************************************************************************/
172 struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
174 return talloc_zero(mem_ctx, struct smbcli_state);
178 parse a //server/share type UNC name
180 BOOL smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
181 const char **hostname, const char **sharename)
185 if (strncmp(unc_name, "\\\\", 2) &&
186 strncmp(unc_name, "//", 2)) {
190 *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
191 p = strchr_m(&(*hostname)[2],'/');
193 p = strchr_m(&(*hostname)[2],'\\');
194 if (!p) return False;
197 *sharename = talloc_strdup(mem_ctx, p+1);