2 Unix SMB/CIFS implementation.
3 client connect/disconnect routines
4 Copyright (C) Andrew Tridgell 2003
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "libcli/raw/libcliraw.h"
25 wrapper around smbcli_sock_connect()
27 BOOL smbcli_socket_connect(struct smbcli_state *cli, const char *server)
29 struct smbcli_socket *sock;
31 sock = smbcli_sock_init(cli);
32 if (!sock) return False;
34 if (!smbcli_sock_connect_byname(sock, server, 0)) {
39 cli->transport = smbcli_transport_init(sock);
41 if (!cli->transport) {
48 /* wrapper around smbcli_transport_connect() */
49 BOOL smbcli_transport_establish(struct smbcli_state *cli,
50 struct nmb_name *calling,
51 struct nmb_name *called)
53 return smbcli_transport_connect(cli->transport, calling, called);
56 /* wrapper around smb_raw_negotiate() */
57 NTSTATUS smbcli_negprot(struct smbcli_state *cli)
59 return smb_raw_negotiate(cli->transport);
62 /* wrapper around smb_raw_session_setup() */
63 NTSTATUS smbcli_session_setup(struct smbcli_state *cli,
68 union smb_sesssetup setup;
72 cli->session = smbcli_session_init(cli->transport);
73 if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
74 talloc_free(cli->transport);
76 mem_ctx = talloc_init("smbcli_session_setup");
77 if (!mem_ctx) return NT_STATUS_NO_MEMORY;
79 setup.generic.level = RAW_SESSSETUP_GENERIC;
80 setup.generic.in.sesskey = cli->transport->negotiate.sesskey;
81 setup.generic.in.capabilities = cli->transport->negotiate.capabilities;
82 if (!user || !user[0]) {
83 setup.generic.in.password = NULL;
84 setup.generic.in.user = "";
85 setup.generic.in.domain = "";
86 setup.generic.in.capabilities &= ~CAP_EXTENDED_SECURITY;
88 if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
89 setup.generic.in.password = password;
91 setup.generic.in.password = NULL;
93 setup.generic.in.user = user;
94 setup.generic.in.domain = domain;
97 status = smb_raw_session_setup(cli->session, mem_ctx, &setup);
99 cli->session->vuid = setup.generic.out.vuid;
101 talloc_free(mem_ctx);
106 /* wrapper around smb_tree_connect() */
107 NTSTATUS smbcli_send_tconX(struct smbcli_state *cli, const char *sharename,
108 const char *devtype, const char *password)
114 cli->tree = smbcli_tree_init(cli->session);
115 talloc_free(cli->session);
116 if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
118 mem_ctx = talloc_init("tcon");
120 return NT_STATUS_NO_MEMORY;
123 /* setup a tree connect */
124 tcon.generic.level = RAW_TCON_TCONX;
125 tcon.tconx.in.flags = 0;
126 if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
127 tcon.tconx.in.password = data_blob(NULL, 0);
128 } else if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
129 tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24);
130 if (cli->transport->negotiate.secblob.length < 8) {
131 return NT_STATUS_INVALID_PARAMETER;
133 SMBencrypt(password, cli->transport->negotiate.secblob.data, tcon.tconx.in.password.data);
135 tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1);
137 tcon.tconx.in.path = sharename;
138 tcon.tconx.in.device = devtype;
140 status = smb_tree_connect(cli->tree, mem_ctx, &tcon);
142 cli->tree->tid = tcon.tconx.out.cnum;
144 talloc_free(mem_ctx);
151 easy way to get to a fully connected smbcli_state in one call
153 NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
154 struct smbcli_state **ret_cli,
158 const char *sharename,
160 const char *username,
162 const char *password,
166 struct smbcli_tree *tree;
171 mem_ctx = talloc_init("smbcli_full_connection");
175 /* if the username is of the form DOMAIN\username then split out the domain */
176 p = strpbrk(username, "\\/");
178 domain = talloc_strndup(mem_ctx, username, PTR_DIFF(p, username));
179 username = talloc_strdup(mem_ctx, p+1);
182 status = smbcli_tree_full_connection(parent_ctx,
183 &tree, myname, host, 0, sharename, devtype,
184 username, domain, password);
185 if (!NT_STATUS_IS_OK(status)) {
189 (*ret_cli) = smbcli_state_init(parent_ctx);
191 (*ret_cli)->tree = tree;
192 (*ret_cli)->session = tree->session;
193 (*ret_cli)->transport = tree->session->transport;
194 talloc_steal(*ret_cli, tree->session->transport->socket);
197 talloc_free(mem_ctx);
206 NTSTATUS smbcli_tdis(struct smbcli_state *cli)
208 return smb_tree_disconnect(cli->tree);
211 /****************************************************************************
212 Initialise a client state structure.
213 ****************************************************************************/
214 struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
216 struct smbcli_state *cli;
218 cli = talloc_p(mem_ctx, struct smbcli_state);
226 /****************************************************************************
227 Shutdown a client structure.
228 ****************************************************************************/
229 void smbcli_shutdown(struct smbcli_state *cli)