r26644: Janitorial: Pass resolve_context explicitly to various SMB functions, should...
[gd/samba-autobuild/.git] / source4 / libcli / cliconnect.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    client connect/disconnect routines
5
6    Copyright (C) Andrew Tridgell 2003-2005
7    Copyright (C) James Peach 2005
8
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.
13
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.
18
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/>.
21 */
22
23 #include "includes.h"
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"
29
30 /*
31   wrapper around smbcli_sock_connect()
32 */
33 bool smbcli_socket_connect(struct smbcli_state *cli, const char *server, 
34                            const char **ports, struct resolve_context *resolve_ctx,
35                            int max_xmit, int max_mux, bool use_spnego)
36 {
37         struct smbcli_socket *sock;
38
39         sock = smbcli_sock_connect_byname(server, ports, NULL, resolve_ctx, 
40                                           NULL);
41
42         if (sock == NULL) return false;
43         
44         cli->transport = smbcli_transport_init(sock, cli, true, max_xmit,
45                                                max_mux, use_spnego);
46         if (!cli->transport) {
47                 return false;
48         }
49
50         return true;
51 }
52
53 /* wrapper around smbcli_transport_connect() */
54 bool smbcli_transport_establish(struct smbcli_state *cli, 
55                                 struct nbt_name *calling,
56                                 struct nbt_name *called)
57 {
58         return smbcli_transport_connect(cli->transport, calling, called);
59 }
60
61 /* wrapper around smb_raw_negotiate() */
62 NTSTATUS smbcli_negprot(struct smbcli_state *cli, bool unicode, int maxprotocol)
63 {
64         return smb_raw_negotiate(cli->transport, unicode, maxprotocol);
65 }
66
67 /* wrapper around smb_raw_sesssetup() */
68 NTSTATUS smbcli_session_setup(struct smbcli_state *cli, 
69                               struct cli_credentials *credentials,
70                               const char *workgroup)
71 {
72         struct smb_composite_sesssetup setup;
73         NTSTATUS status;
74
75         cli->session = smbcli_session_init(cli->transport, cli, true);
76         if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
77
78         setup.in.sesskey = cli->transport->negotiate.sesskey;
79         setup.in.capabilities = cli->transport->negotiate.capabilities;
80         setup.in.credentials = credentials;
81         setup.in.workgroup = workgroup;
82
83         status = smb_composite_sesssetup(cli->session, &setup);
84
85         cli->session->vuid = setup.out.vuid;
86
87         return status;
88 }
89
90 /* wrapper around smb_raw_tcon() */
91 NTSTATUS smbcli_tconX(struct smbcli_state *cli, const char *sharename, 
92                       const char *devtype, const char *password)
93 {
94         union smb_tcon tcon;
95         TALLOC_CTX *mem_ctx;
96         NTSTATUS status;
97
98         cli->tree = smbcli_tree_init(cli->session, cli, true);
99         if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
100
101         mem_ctx = talloc_init("tcon");
102         if (!mem_ctx) {
103                 return NT_STATUS_NO_MEMORY;
104         }
105
106         /* setup a tree connect */
107         tcon.generic.level = RAW_TCON_TCONX;
108         tcon.tconx.in.flags = 0;
109         if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
110                 tcon.tconx.in.password = data_blob(NULL, 0);
111         } else if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
112                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24);
113                 if (cli->transport->negotiate.secblob.length < 8) {
114                         return NT_STATUS_INVALID_PARAMETER;
115                 }
116                 SMBencrypt(password, cli->transport->negotiate.secblob.data, tcon.tconx.in.password.data);
117         } else {
118                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1);
119         }
120         tcon.tconx.in.path = sharename;
121         tcon.tconx.in.device = devtype;
122         
123         status = smb_raw_tcon(cli->tree, mem_ctx, &tcon);
124
125         cli->tree->tid = tcon.tconx.out.tid;
126
127         talloc_free(mem_ctx);
128
129         return status;
130 }
131
132
133 /*
134   easy way to get to a fully connected smbcli_state in one call
135 */
136 NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
137                                 struct smbcli_state **ret_cli, 
138                                 const char *host,
139                                 const char **ports,
140                                 const char *sharename,
141                                 const char *devtype,
142                                 struct cli_credentials *credentials,
143                                 struct resolve_context *resolve_ctx,
144                                 struct event_context *ev)
145 {
146         struct smbcli_tree *tree;
147         NTSTATUS status;
148
149         *ret_cli = NULL;
150
151         status = smbcli_tree_full_connection(parent_ctx,
152                                              &tree, host, ports, 
153                                              sharename, devtype,
154                                              credentials, resolve_ctx, ev);
155         if (!NT_STATUS_IS_OK(status)) {
156                 goto done;
157         }
158
159         (*ret_cli) = smbcli_state_init(parent_ctx);
160
161         (*ret_cli)->tree = tree;
162         (*ret_cli)->session = tree->session;
163         (*ret_cli)->transport = tree->session->transport;
164
165         talloc_steal(*ret_cli, tree);
166         
167 done:
168         return status;
169 }
170
171
172 /*
173   disconnect the tree
174 */
175 NTSTATUS smbcli_tdis(struct smbcli_state *cli)
176 {
177         return smb_tree_disconnect(cli->tree);
178 }
179
180 /****************************************************************************
181  Initialise a client state structure.
182 ****************************************************************************/
183 struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
184 {
185         return talloc_zero(mem_ctx, struct smbcli_state);
186 }
187
188 /* Insert a NULL at the first separator of the given path and return a pointer
189  * to the remainder of the string.
190  */
191 static char *
192 terminate_path_at_separator(char * path)
193 {
194         char * p;
195
196         if (!path) {
197                 return NULL;
198         }
199
200         if ((p = strchr_m(path, '/'))) {
201                 *p = '\0';
202                 return p + 1;
203         }
204
205         if ((p = strchr_m(path, '\\'))) {
206                 *p = '\0';
207                 return p + 1;
208         }
209         
210         /* No separator. */
211         return NULL;
212 }
213
214 /*
215   parse a //server/share type UNC name
216 */
217 bool smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
218                       char **hostname, char **sharename)
219 {
220         char *p;
221
222         *hostname = *sharename = NULL;
223
224         if (strncmp(unc_name, "\\\\", 2) &&
225             strncmp(unc_name, "//", 2)) {
226                 return false;
227         }
228
229         *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
230         p = terminate_path_at_separator(*hostname);
231
232         if (p != NULL && *p) {
233                 *sharename = talloc_strdup(mem_ctx, p);
234                 terminate_path_at_separator(*sharename);
235         }
236
237         if (*hostname && *sharename) {
238                 return true;
239         }
240
241         talloc_free(*hostname);
242         talloc_free(*sharename);
243         *hostname = *sharename = NULL;
244         return false;
245 }
246
247
248