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.
24 wrapper around smbcli_sock_connect()
26 BOOL smbcli_socket_connect(struct smbcli_state *cli, const char *server)
28 struct smbcli_socket *sock;
30 sock = smbcli_sock_init();
31 if (!sock) return False;
33 if (!smbcli_sock_connect_byname(sock, server, 0)) {
34 smbcli_sock_close(sock);
38 cli->transport = smbcli_transport_init(sock);
39 if (!cli->transport) {
40 smbcli_sock_close(sock);
47 /* wrapper around smbcli_transport_connect() */
48 BOOL smbcli_transport_establish(struct smbcli_state *cli,
49 struct nmb_name *calling,
50 struct nmb_name *called)
52 return smbcli_transport_connect(cli->transport, calling, called);
55 /* wrapper around smb_raw_negotiate() */
56 NTSTATUS smbcli_negprot(struct smbcli_state *cli)
58 return smb_raw_negotiate(cli->transport);
61 /* wrapper around smb_raw_session_setup() */
62 NTSTATUS smbcli_session_setup(struct smbcli_state *cli,
67 union smb_sesssetup setup;
71 cli->session = smbcli_session_init(cli->transport);
72 if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
74 mem_ctx = talloc_init("smbcli_session_setup");
75 if (!mem_ctx) return NT_STATUS_NO_MEMORY;
77 setup.generic.level = RAW_SESSSETUP_GENERIC;
78 setup.generic.in.sesskey = cli->transport->negotiate.sesskey;
79 setup.generic.in.capabilities = cli->transport->negotiate.capabilities;
80 if (!user || !user[0]) {
81 setup.generic.in.password = NULL;
82 setup.generic.in.user = "";
83 setup.generic.in.domain = "";
84 setup.generic.in.capabilities &= ~CAP_EXTENDED_SECURITY;
86 if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
87 setup.generic.in.password = password;
89 setup.generic.in.password = NULL;
91 setup.generic.in.user = user;
92 setup.generic.in.domain = domain;
95 status = smb_raw_session_setup(cli->session, mem_ctx, &setup);
97 cli->session->vuid = setup.generic.out.vuid;
104 /* wrapper around smb_tree_connect() */
105 NTSTATUS smbcli_send_tconX(struct smbcli_state *cli, const char *sharename,
106 const char *devtype, const char *password)
112 cli->tree = smbcli_tree_init(cli->session);
113 if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
115 cli->tree->reference_count++;
117 mem_ctx = talloc_init("tcon");
119 return NT_STATUS_NO_MEMORY;
122 /* setup a tree connect */
123 tcon.generic.level = RAW_TCON_TCONX;
124 tcon.tconx.in.flags = 0;
125 if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
126 tcon.tconx.in.password = data_blob(NULL, 0);
127 } else if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
128 tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24);
129 if (cli->transport->negotiate.secblob.length < 8) {
130 return NT_STATUS_INVALID_PARAMETER;
132 SMBencrypt(password, cli->transport->negotiate.secblob.data, tcon.tconx.in.password.data);
134 tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1);
136 tcon.tconx.in.path = sharename;
137 tcon.tconx.in.device = devtype;
139 status = smb_tree_connect(cli->tree, mem_ctx, &tcon);
141 cli->tree->tid = tcon.tconx.out.cnum;
143 talloc_free(mem_ctx);
150 easy way to get to a fully connected smbcli_state in one call
152 NTSTATUS smbcli_full_connection(struct smbcli_state **ret_cli,
156 const char *sharename,
158 const char *username,
160 const char *password,
164 struct smbcli_tree *tree;
169 mem_ctx = talloc_init("smbcli_full_connection");
173 /* if the username is of the form DOMAIN\username then split out the domain */
174 p = strpbrk(username, "\\/");
176 domain = talloc_strndup(mem_ctx, username, PTR_DIFF(p, username));
177 username = talloc_strdup(mem_ctx, p+1);
180 status = smbcli_tree_full_connection(&tree, myname, host, 0, sharename, devtype,
181 username, domain, password);
182 if (!NT_STATUS_IS_OK(status)) {
186 (*ret_cli) = smbcli_state_init();
188 (*ret_cli)->tree = tree;
189 (*ret_cli)->session = tree->session;
190 (*ret_cli)->transport = tree->session->transport;
191 tree->reference_count++;
194 talloc_free(mem_ctx);
202 NTSTATUS smbcli_tdis(struct smbcli_state *cli)
204 return smb_tree_disconnect(cli->tree);
207 /****************************************************************************
208 Initialise a client state structure.
209 ****************************************************************************/
210 struct smbcli_state *smbcli_state_init(void)
212 struct smbcli_state *cli;
214 cli = talloc_p(NULL, struct smbcli_state);
222 /****************************************************************************
223 Shutdown a client structure.
224 ****************************************************************************/
225 void smbcli_shutdown(struct smbcli_state *cli)
229 cli->tree->reference_count++;
230 smbcli_tree_close(cli->tree);