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