2 Unix SMB/CIFS implementation.
4 client connect/disconnect routines
6 Copyright (C) Andrew Tridgell 2003-2005
7 Copyright (C) James Peach 2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "libcli/libcli.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/auth/libcli_auth.h"
27 #include "libcli/smb_composite/smb_composite.h"
28 #include "param/param.h"
31 wrapper around smbcli_sock_connect()
33 bool smbcli_socket_connect(struct smbcli_state *cli, const char *server)
35 struct smbcli_socket *sock;
37 sock = smbcli_sock_connect_byname(server, 0, NULL, NULL);
39 if (sock == NULL) return false;
41 cli->transport = smbcli_transport_init(sock, cli, true);
42 if (!cli->transport) {
49 /* wrapper around smbcli_transport_connect() */
50 bool smbcli_transport_establish(struct smbcli_state *cli,
51 struct nbt_name *calling,
52 struct nbt_name *called)
54 return smbcli_transport_connect(cli->transport, calling, called);
57 /* wrapper around smb_raw_negotiate() */
58 NTSTATUS smbcli_negprot(struct smbcli_state *cli)
60 return smb_raw_negotiate(cli->transport,
61 lp_cli_maxprotocol(global_loadparm));
64 /* wrapper around smb_raw_sesssetup() */
65 NTSTATUS smbcli_session_setup(struct smbcli_state *cli,
66 struct cli_credentials *credentials)
68 struct smb_composite_sesssetup setup;
71 cli->session = smbcli_session_init(cli->transport, cli, true);
72 if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
74 setup.in.sesskey = cli->transport->negotiate.sesskey;
75 setup.in.capabilities = cli->transport->negotiate.capabilities;
76 setup.in.credentials = credentials;
77 setup.in.workgroup = lp_workgroup(global_loadparm);
79 status = smb_composite_sesssetup(cli->session, &setup);
81 cli->session->vuid = setup.out.vuid;
86 /* wrapper around smb_raw_tcon() */
87 NTSTATUS smbcli_tconX(struct smbcli_state *cli, const char *sharename,
88 const char *devtype, const char *password)
94 cli->tree = smbcli_tree_init(cli->session, cli, true);
95 if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
97 mem_ctx = talloc_init("tcon");
99 return NT_STATUS_NO_MEMORY;
102 /* setup a tree connect */
103 tcon.generic.level = RAW_TCON_TCONX;
104 tcon.tconx.in.flags = 0;
105 if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
106 tcon.tconx.in.password = data_blob(NULL, 0);
107 } else if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
108 tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24);
109 if (cli->transport->negotiate.secblob.length < 8) {
110 return NT_STATUS_INVALID_PARAMETER;
112 SMBencrypt(password, cli->transport->negotiate.secblob.data, tcon.tconx.in.password.data);
114 tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1);
116 tcon.tconx.in.path = sharename;
117 tcon.tconx.in.device = devtype;
119 status = smb_raw_tcon(cli->tree, mem_ctx, &tcon);
121 cli->tree->tid = tcon.tconx.out.tid;
123 talloc_free(mem_ctx);
130 easy way to get to a fully connected smbcli_state in one call
132 NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
133 struct smbcli_state **ret_cli,
135 const char *sharename,
137 struct cli_credentials *credentials,
138 struct event_context *ev)
140 struct smbcli_tree *tree;
145 status = smbcli_tree_full_connection(parent_ctx,
146 &tree, host, 0, sharename, devtype,
148 if (!NT_STATUS_IS_OK(status)) {
152 (*ret_cli) = smbcli_state_init(parent_ctx);
154 (*ret_cli)->tree = tree;
155 (*ret_cli)->session = tree->session;
156 (*ret_cli)->transport = tree->session->transport;
158 talloc_steal(*ret_cli, tree);
168 NTSTATUS smbcli_tdis(struct smbcli_state *cli)
170 return smb_tree_disconnect(cli->tree);
173 /****************************************************************************
174 Initialise a client state structure.
175 ****************************************************************************/
176 struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
178 return talloc_zero(mem_ctx, struct smbcli_state);
181 /* Insert a NULL at the first separator of the given path and return a pointer
182 * to the remainder of the string.
185 terminate_path_at_separator(char * path)
193 if ((p = strchr_m(path, '/'))) {
198 if ((p = strchr_m(path, '\\'))) {
208 parse a //server/share type UNC name
210 bool smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
211 char **hostname, char **sharename)
215 *hostname = *sharename = NULL;
217 if (strncmp(unc_name, "\\\\", 2) &&
218 strncmp(unc_name, "//", 2)) {
222 *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
223 p = terminate_path_at_separator(*hostname);
225 if (p != NULL && *p) {
226 *sharename = talloc_strdup(mem_ctx, p);
227 terminate_path_at_separator(*sharename);
230 if (*hostname && *sharename) {
234 talloc_free(*hostname);
235 talloc_free(*sharename);
236 *hostname = *sharename = NULL;