Add context for libcli_resolve.
[samba-svnmirror.git] / source / 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                            struct resolve_context *resolve_ctx,
35                            int max_xmit, int max_mux)
36 {
37         struct smbcli_socket *sock;
38
39         sock = smbcli_sock_connect_byname(server, 0, 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);
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 {
71         struct smb_composite_sesssetup setup;
72         NTSTATUS status;
73
74         cli->session = smbcli_session_init(cli->transport, cli, true);
75         if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
76
77         setup.in.sesskey = cli->transport->negotiate.sesskey;
78         setup.in.capabilities = cli->transport->negotiate.capabilities;
79         setup.in.credentials = credentials;
80         setup.in.workgroup = lp_workgroup(global_loadparm);
81
82         status = smb_composite_sesssetup(cli->session, &setup);
83
84         cli->session->vuid = setup.out.vuid;
85
86         return status;
87 }
88
89 /* wrapper around smb_raw_tcon() */
90 NTSTATUS smbcli_tconX(struct smbcli_state *cli, const char *sharename, 
91                       const char *devtype, const char *password)
92 {
93         union smb_tcon tcon;
94         TALLOC_CTX *mem_ctx;
95         NTSTATUS status;
96
97         cli->tree = smbcli_tree_init(cli->session, cli, true);
98         if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
99
100         mem_ctx = talloc_init("tcon");
101         if (!mem_ctx) {
102                 return NT_STATUS_NO_MEMORY;
103         }
104
105         /* setup a tree connect */
106         tcon.generic.level = RAW_TCON_TCONX;
107         tcon.tconx.in.flags = 0;
108         if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
109                 tcon.tconx.in.password = data_blob(NULL, 0);
110         } else if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
111                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24);
112                 if (cli->transport->negotiate.secblob.length < 8) {
113                         return NT_STATUS_INVALID_PARAMETER;
114                 }
115                 SMBencrypt(password, cli->transport->negotiate.secblob.data, tcon.tconx.in.password.data);
116         } else {
117                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1);
118         }
119         tcon.tconx.in.path = sharename;
120         tcon.tconx.in.device = devtype;
121         
122         status = smb_raw_tcon(cli->tree, mem_ctx, &tcon);
123
124         cli->tree->tid = tcon.tconx.out.tid;
125
126         talloc_free(mem_ctx);
127
128         return status;
129 }
130
131
132 /*
133   easy way to get to a fully connected smbcli_state in one call
134 */
135 NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
136                                 struct smbcli_state **ret_cli, 
137                                 const char *host,
138                                 const char *sharename,
139                                 const char *devtype,
140                                 struct cli_credentials *credentials,
141                                 struct event_context *ev)
142 {
143         struct smbcli_tree *tree;
144         NTSTATUS status;
145
146         *ret_cli = NULL;
147
148         status = smbcli_tree_full_connection(parent_ctx,
149                                              &tree, host, 0, sharename, devtype,
150                                              credentials, ev);
151         if (!NT_STATUS_IS_OK(status)) {
152                 goto done;
153         }
154
155         (*ret_cli) = smbcli_state_init(parent_ctx);
156
157         (*ret_cli)->tree = tree;
158         (*ret_cli)->session = tree->session;
159         (*ret_cli)->transport = tree->session->transport;
160
161         talloc_steal(*ret_cli, tree);
162         
163 done:
164         return status;
165 }
166
167
168 /*
169   disconnect the tree
170 */
171 NTSTATUS smbcli_tdis(struct smbcli_state *cli)
172 {
173         return smb_tree_disconnect(cli->tree);
174 }
175
176 /****************************************************************************
177  Initialise a client state structure.
178 ****************************************************************************/
179 struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
180 {
181         return talloc_zero(mem_ctx, struct smbcli_state);
182 }
183
184 /* Insert a NULL at the first separator of the given path and return a pointer
185  * to the remainder of the string.
186  */
187 static char *
188 terminate_path_at_separator(char * path)
189 {
190         char * p;
191
192         if (!path) {
193                 return NULL;
194         }
195
196         if ((p = strchr_m(path, '/'))) {
197                 *p = '\0';
198                 return p + 1;
199         }
200
201         if ((p = strchr_m(path, '\\'))) {
202                 *p = '\0';
203                 return p + 1;
204         }
205         
206         /* No separator. */
207         return NULL;
208 }
209
210 /*
211   parse a //server/share type UNC name
212 */
213 bool smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
214                       char **hostname, char **sharename)
215 {
216         char *p;
217
218         *hostname = *sharename = NULL;
219
220         if (strncmp(unc_name, "\\\\", 2) &&
221             strncmp(unc_name, "//", 2)) {
222                 return false;
223         }
224
225         *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
226         p = terminate_path_at_separator(*hostname);
227
228         if (p != NULL && *p) {
229                 *sharename = talloc_strdup(mem_ctx, p);
230                 terminate_path_at_separator(*sharename);
231         }
232
233         if (*hostname && *sharename) {
234                 return true;
235         }
236
237         talloc_free(*hostname);
238         talloc_free(*sharename);
239         *hostname = *sharename = NULL;
240         return false;
241 }
242
243
244