Modify the LDAP-CLDAP test for better coverage.
[Samba/gebeck_regimport.git] / source4 / libcli / cliconnect.c
blobc20a7fd9353f9572f804c394463bbe939aaebbb9
1 /*
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/>.
23 #include "includes.h"
24 #include "libcli/libcli.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/raw/raw_proto.h"
27 #include "libcli/auth/libcli_auth.h"
28 #include "libcli/smb_composite/smb_composite.h"
29 #include "param/param.h"
32 wrapper around smbcli_sock_connect()
34 bool smbcli_socket_connect(struct smbcli_state *cli, const char *server,
35 const char **ports,
36 struct event_context *ev_ctx,
37 struct resolve_context *resolve_ctx,
38 struct smbcli_options *options)
40 struct smbcli_socket *sock;
42 sock = smbcli_sock_connect_byname(server, ports, NULL,
43 resolve_ctx, ev_ctx);
45 if (sock == NULL) return false;
47 cli->transport = smbcli_transport_init(sock, cli, true, options);
48 if (!cli->transport) {
49 return false;
52 return true;
55 /* wrapper around smbcli_transport_connect() */
56 bool smbcli_transport_establish(struct smbcli_state *cli,
57 struct nbt_name *calling,
58 struct nbt_name *called)
60 return smbcli_transport_connect(cli->transport, calling, called);
63 /* wrapper around smb_raw_negotiate() */
64 NTSTATUS smbcli_negprot(struct smbcli_state *cli, bool unicode, int maxprotocol)
66 return smb_raw_negotiate(cli->transport, unicode, maxprotocol);
69 /* wrapper around smb_raw_sesssetup() */
70 NTSTATUS smbcli_session_setup(struct smbcli_state *cli,
71 struct cli_credentials *credentials,
72 const char *workgroup)
74 struct smb_composite_sesssetup setup;
75 NTSTATUS status;
77 cli->session = smbcli_session_init(cli->transport, cli, true);
78 if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
80 setup.in.sesskey = cli->transport->negotiate.sesskey;
81 setup.in.capabilities = cli->transport->negotiate.capabilities;
82 setup.in.credentials = credentials;
83 setup.in.workgroup = workgroup;
85 status = smb_composite_sesssetup(cli->session, &setup);
87 cli->session->vuid = setup.out.vuid;
89 return status;
92 /* wrapper around smb_raw_tcon() */
93 NTSTATUS smbcli_tconX(struct smbcli_state *cli, const char *sharename,
94 const char *devtype, const char *password)
96 union smb_tcon tcon;
97 TALLOC_CTX *mem_ctx;
98 NTSTATUS status;
100 cli->tree = smbcli_tree_init(cli->session, cli, true);
101 if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
103 mem_ctx = talloc_init("tcon");
104 if (!mem_ctx) {
105 return NT_STATUS_NO_MEMORY;
108 /* setup a tree connect */
109 tcon.generic.level = RAW_TCON_TCONX;
110 tcon.tconx.in.flags = 0;
111 if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
112 tcon.tconx.in.password = data_blob(NULL, 0);
113 } else if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
114 tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24);
115 if (cli->transport->negotiate.secblob.length < 8) {
116 return NT_STATUS_INVALID_PARAMETER;
118 SMBencrypt(password, cli->transport->negotiate.secblob.data, tcon.tconx.in.password.data);
119 } else {
120 tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1);
122 tcon.tconx.in.path = sharename;
123 tcon.tconx.in.device = devtype;
125 status = smb_raw_tcon(cli->tree, mem_ctx, &tcon);
127 cli->tree->tid = tcon.tconx.out.tid;
129 talloc_free(mem_ctx);
131 return status;
136 easy way to get to a fully connected smbcli_state in one call
138 NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
139 struct smbcli_state **ret_cli,
140 const char *host,
141 const char **ports,
142 const char *sharename,
143 const char *devtype,
144 struct cli_credentials *credentials,
145 struct resolve_context *resolve_ctx,
146 struct event_context *ev,
147 struct smbcli_options *options)
149 struct smbcli_tree *tree;
150 NTSTATUS status;
152 *ret_cli = NULL;
154 status = smbcli_tree_full_connection(parent_ctx,
155 &tree, host, ports,
156 sharename, devtype,
157 credentials, resolve_ctx, ev,
158 options);
159 if (!NT_STATUS_IS_OK(status)) {
160 goto done;
163 (*ret_cli) = smbcli_state_init(parent_ctx);
165 (*ret_cli)->tree = tree;
166 (*ret_cli)->session = tree->session;
167 (*ret_cli)->transport = tree->session->transport;
169 talloc_steal(*ret_cli, tree);
171 done:
172 return status;
177 disconnect the tree
179 NTSTATUS smbcli_tdis(struct smbcli_state *cli)
181 return smb_tree_disconnect(cli->tree);
184 /****************************************************************************
185 Initialise a client state structure.
186 ****************************************************************************/
187 struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
189 return talloc_zero(mem_ctx, struct smbcli_state);
192 /* Insert a NULL at the first separator of the given path and return a pointer
193 * to the remainder of the string.
195 static char *
196 terminate_path_at_separator(char * path)
198 char * p;
200 if (!path) {
201 return NULL;
204 if ((p = strchr_m(path, '/'))) {
205 *p = '\0';
206 return p + 1;
209 if ((p = strchr_m(path, '\\'))) {
210 *p = '\0';
211 return p + 1;
214 /* No separator. */
215 return NULL;
219 parse a //server/share type UNC name
221 bool smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
222 char **hostname, char **sharename)
224 char *p;
226 *hostname = *sharename = NULL;
228 if (strncmp(unc_name, "\\\\", 2) &&
229 strncmp(unc_name, "//", 2)) {
230 return false;
233 *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
234 p = terminate_path_at_separator(*hostname);
236 if (p != NULL && *p) {
237 *sharename = talloc_strdup(mem_ctx, p);
238 terminate_path_at_separator(*sharename);
241 if (*hostname && *sharename) {
242 return true;
245 talloc_free(*hostname);
246 talloc_free(*sharename);
247 *hostname = *sharename = NULL;
248 return false;