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