r9022: One more step in the game of whack-a-mole with the PAC.
[Samba/gbeck.git] / source4 / libcli / cliconnect.c
blob624b54c3f2a671df1921d23b23c080e56fd194ef
1 /*
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.
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/composite/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_init(cli, NULL);
35 if (!sock) return False;
37 if (!smbcli_sock_connect_byname(sock, server, 0)) {
38 talloc_free(sock);
39 return False;
42 cli->transport = smbcli_transport_init(sock, cli, True);
43 if (!cli->transport) {
44 return False;
47 return True;
50 /* wrapper around smbcli_transport_connect() */
51 BOOL smbcli_transport_establish(struct smbcli_state *cli,
52 struct nbt_name *calling,
53 struct nbt_name *called)
55 return smbcli_transport_connect(cli->transport, calling, called);
58 /* wrapper around smb_raw_negotiate() */
59 NTSTATUS smbcli_negprot(struct smbcli_state *cli)
61 return smb_raw_negotiate(cli->transport, lp_maxprotocol());
64 /* wrapper around smb_raw_session_setup() */
65 NTSTATUS smbcli_session_setup(struct smbcli_state *cli,
66 struct cli_credentials *credentials)
68 struct smb_composite_sesssetup setup;
69 NTSTATUS status;
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();
79 status = smb_composite_sesssetup(cli->session, &setup);
81 cli->session->vuid = setup.out.vuid;
83 return status;
86 /* wrapper around smb_tree_connect() */
87 NTSTATUS smbcli_tconX(struct smbcli_state *cli, const char *sharename,
88 const char *devtype, const char *password)
90 union smb_tcon tcon;
91 TALLOC_CTX *mem_ctx;
92 NTSTATUS status;
94 cli->tree = smbcli_tree_init(cli->session, cli, True);
95 if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
97 mem_ctx = talloc_init("tcon");
98 if (!mem_ctx) {
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);
113 } else {
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_tree_connect(cli->tree, mem_ctx, &tcon);
121 cli->tree->tid = tcon.tconx.out.tid;
123 talloc_free(mem_ctx);
125 return status;
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,
134 const char *host,
135 const char *sharename,
136 const char *devtype,
137 struct cli_credentials *credentials,
138 struct event_context *ev)
140 struct smbcli_tree *tree;
141 NTSTATUS status;
143 *ret_cli = NULL;
145 status = smbcli_tree_full_connection(parent_ctx,
146 &tree, host, 0, sharename, devtype,
147 credentials, ev);
148 if (!NT_STATUS_IS_OK(status)) {
149 goto done;
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);
160 done:
161 return status;
166 disconnect the 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);
182 parse a //server/share type UNC name
184 BOOL smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
185 const char **hostname, const char **sharename)
187 char *p;
189 if (strncmp(unc_name, "\\\\", 2) &&
190 strncmp(unc_name, "//", 2)) {
191 return False;
194 *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
195 p = strchr_m(&(*hostname)[2],'/');
196 if (!p) {
197 p = strchr_m(&(*hostname)[2],'\\');
198 if (!p) return False;
200 *p = 0;
201 *sharename = talloc_strdup(mem_ctx, p+1);
203 return True;