r4891: - added a generic resolve_name() async interface in libcli/resolve/,
[amitay/samba.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
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.
12
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.
17
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.
21 */
22
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/composite/composite.h"
27
28 /*
29   wrapper around smbcli_sock_connect()
30 */
31 BOOL smbcli_socket_connect(struct smbcli_state *cli, const char *server)
32 {
33         struct smbcli_socket *sock;
34
35         sock = smbcli_sock_init(cli);
36         if (!sock) return False;
37
38         if (!smbcli_sock_connect_byname(sock, server, 0)) {
39                 talloc_free(sock);
40                 return False;
41         }
42         
43         cli->transport = smbcli_transport_init(sock);
44         talloc_free(sock);
45         if (!cli->transport) {
46                 return False;
47         }
48
49         return True;
50 }
51
52 /* wrapper around smbcli_transport_connect() */
53 BOOL smbcli_transport_establish(struct smbcli_state *cli, 
54                                 struct nbt_name *calling,
55                                 struct nbt_name *called)
56 {
57         return smbcli_transport_connect(cli->transport, calling, called);
58 }
59
60 /* wrapper around smb_raw_negotiate() */
61 NTSTATUS smbcli_negprot(struct smbcli_state *cli)
62 {
63         return smb_raw_negotiate(cli->transport, lp_maxprotocol());
64 }
65
66 /* wrapper around smb_raw_session_setup() */
67 NTSTATUS smbcli_session_setup(struct smbcli_state *cli, 
68                               const char *user, 
69                               const char *password, 
70                               const char *domain)
71 {
72         struct smb_composite_sesssetup setup;
73         NTSTATUS status;
74         TALLOC_CTX *mem_ctx;
75
76         cli->session = smbcli_session_init(cli->transport);
77         if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
78         talloc_free(cli->transport);
79
80         mem_ctx = talloc_init("smbcli_session_setup");
81         if (!mem_ctx) return NT_STATUS_NO_MEMORY;
82
83         setup.in.sesskey = cli->transport->negotiate.sesskey;
84         setup.in.capabilities = cli->transport->negotiate.capabilities;
85         if (!user || !user[0]) {
86                 setup.in.password = NULL;
87                 setup.in.user = "";
88                 setup.in.domain = "";
89                 setup.in.capabilities &= ~CAP_EXTENDED_SECURITY;
90         } else {
91                 if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
92                         setup.in.password = password;
93                 } else {
94                         setup.in.password = NULL;
95                 }
96                 setup.in.user = user;
97                 setup.in.domain = domain;
98         }
99
100         status = smb_composite_sesssetup(cli->session, &setup);
101
102         cli->session->vuid = setup.out.vuid;
103
104         talloc_free(mem_ctx);
105
106         return status;
107 }
108
109 /* wrapper around smb_tree_connect() */
110 NTSTATUS smbcli_send_tconX(struct smbcli_state *cli, const char *sharename, 
111                            const char *devtype, const char *password)
112 {
113         union smb_tcon tcon;
114         TALLOC_CTX *mem_ctx;
115         NTSTATUS status;
116
117         cli->tree = smbcli_tree_init(cli->session);
118         talloc_free(cli->session);
119         if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
120
121         mem_ctx = talloc_init("tcon");
122         if (!mem_ctx) {
123                 return NT_STATUS_NO_MEMORY;
124         }
125
126         /* setup a tree connect */
127         tcon.generic.level = RAW_TCON_TCONX;
128         tcon.tconx.in.flags = 0;
129         if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
130                 tcon.tconx.in.password = data_blob(NULL, 0);
131         } else if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
132                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24);
133                 if (cli->transport->negotiate.secblob.length < 8) {
134                         return NT_STATUS_INVALID_PARAMETER;
135                 }
136                 SMBencrypt(password, cli->transport->negotiate.secblob.data, tcon.tconx.in.password.data);
137         } else {
138                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1);
139         }
140         tcon.tconx.in.path = sharename;
141         tcon.tconx.in.device = devtype;
142         
143         status = smb_tree_connect(cli->tree, mem_ctx, &tcon);
144
145         cli->tree->tid = tcon.tconx.out.tid;
146
147         talloc_free(mem_ctx);
148
149         return status;
150 }
151
152
153 /*
154   easy way to get to a fully connected smbcli_state in one call
155 */
156 NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
157                                 struct smbcli_state **ret_cli, 
158                                 const char *myname,
159                                 const char *host,
160                                 const char *sharename,
161                                 const char *devtype,
162                                 const char *username,
163                                 const char *domain,
164                                 const char *password)
165 {
166         struct smbcli_tree *tree;
167         NTSTATUS status;
168         char *p;
169         TALLOC_CTX *mem_ctx;
170
171         mem_ctx = talloc_init("smbcli_full_connection");
172
173         *ret_cli = NULL;
174
175         /* if the username is of the form DOMAIN\username then split out the domain */
176         p = strpbrk(username, "\\/");
177         if (p) {
178                 domain = talloc_strndup(mem_ctx, username, PTR_DIFF(p, username));
179                 username = talloc_strdup(mem_ctx, p+1);
180         }
181
182         status = smbcli_tree_full_connection(parent_ctx,
183                                              &tree, myname, host, 0, sharename, devtype,
184                                              username, domain, password);
185         if (!NT_STATUS_IS_OK(status)) {
186                 goto done;
187         }
188
189         (*ret_cli) = smbcli_state_init(parent_ctx);
190
191         (*ret_cli)->tree = tree;
192         (*ret_cli)->session = tree->session;
193         (*ret_cli)->transport = tree->session->transport;
194         talloc_steal(*ret_cli, tree->session->transport->socket);
195         
196 done:
197         talloc_free(mem_ctx);
198
199         return status;
200 }
201
202
203 /*
204   disconnect the tree
205 */
206 NTSTATUS smbcli_tdis(struct smbcli_state *cli)
207 {
208         return smb_tree_disconnect(cli->tree);
209 }
210
211 /****************************************************************************
212  Initialise a client state structure.
213 ****************************************************************************/
214 struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
215 {
216         struct smbcli_state *cli;
217
218         cli = talloc_zero_p(mem_ctx, struct smbcli_state);
219         if (cli) {
220                 ZERO_STRUCTP(cli);
221         }
222
223         return cli;
224 }
225
226 /****************************************************************************
227  Shutdown a client structure.
228 ****************************************************************************/
229 void smbcli_shutdown(struct smbcli_state *cli)
230 {
231         talloc_free(cli);
232 }
233
234 /*
235   parse a //server/share type UNC name
236 */
237 BOOL smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
238                       const char **hostname, const char **sharename)
239 {
240         char *p;
241
242         if (strncmp(unc_name, "\\\\", 2) &&
243             strncmp(unc_name, "//", 2)) {
244                 return False;
245         }
246
247         *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
248         p = strchr_m(&(*hostname)[2],'/');
249         if (!p) {
250                 p = strchr_m(&(*hostname)[2],'\\');
251                 if (!p) return False;
252         }
253         *p = 0;
254         *sharename = talloc_strdup(mem_ctx, p+1);
255
256         return True;
257 }
258
259
260