r4776: Add more debugs to SamSync test.
[gd/samba-autobuild/.git] / source4 / libcli / cliconnect.c
1 /*
2    Unix SMB/CIFS implementation.
3    client connect/disconnect routines
4    Copyright (C) Andrew Tridgell 2003
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "libcli/raw/libcliraw.h"
24
25 /*
26   wrapper around smbcli_sock_connect()
27 */
28 BOOL smbcli_socket_connect(struct smbcli_state *cli, const char *server)
29 {
30         struct smbcli_socket *sock;
31
32         sock = smbcli_sock_init(cli);
33         if (!sock) return False;
34
35         if (!smbcli_sock_connect_byname(sock, server, 0)) {
36                 talloc_free(sock);
37                 return False;
38         }
39         
40         cli->transport = smbcli_transport_init(sock);
41         talloc_free(sock);
42         if (!cli->transport) {
43                 return False;
44         }
45
46         return True;
47 }
48
49 /* wrapper around smbcli_transport_connect() */
50 BOOL smbcli_transport_establish(struct smbcli_state *cli, 
51                              struct nmb_name *calling,
52                              struct nmb_name *called)
53 {
54         return smbcli_transport_connect(cli->transport, calling, called);
55 }
56
57 /* wrapper around smb_raw_negotiate() */
58 NTSTATUS smbcli_negprot(struct smbcli_state *cli)
59 {
60         return smb_raw_negotiate(cli->transport, lp_maxprotocol());
61 }
62
63 /* wrapper around smb_raw_session_setup() */
64 NTSTATUS smbcli_session_setup(struct smbcli_state *cli, 
65                               const char *user, 
66                               const char *password, 
67                               const char *domain)
68 {
69         union smb_sesssetup setup;
70         NTSTATUS status;
71         TALLOC_CTX *mem_ctx;
72
73         cli->session = smbcli_session_init(cli->transport);
74         if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
75         talloc_free(cli->transport);
76
77         mem_ctx = talloc_init("smbcli_session_setup");
78         if (!mem_ctx) return NT_STATUS_NO_MEMORY;
79
80         setup.generic.level = RAW_SESSSETUP_GENERIC;
81         setup.generic.in.sesskey = cli->transport->negotiate.sesskey;
82         setup.generic.in.capabilities = cli->transport->negotiate.capabilities;
83         if (!user || !user[0]) {
84                 setup.generic.in.password = NULL;
85                 setup.generic.in.user = "";
86                 setup.generic.in.domain = "";
87                 setup.generic.in.capabilities &= ~CAP_EXTENDED_SECURITY;
88         } else {
89                 if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
90                         setup.generic.in.password = password;
91                 } else {
92                         setup.generic.in.password = NULL;
93                 }
94                 setup.generic.in.user = user;
95                 setup.generic.in.domain = domain;
96         }
97
98         status = smb_raw_session_setup(cli->session, mem_ctx, &setup);
99
100         cli->session->vuid = setup.generic.out.vuid;
101
102         talloc_free(mem_ctx);
103
104         return status;
105 }
106
107 /* wrapper around smb_tree_connect() */
108 NTSTATUS smbcli_send_tconX(struct smbcli_state *cli, const char *sharename, 
109                            const char *devtype, const char *password)
110 {
111         union smb_tcon tcon;
112         TALLOC_CTX *mem_ctx;
113         NTSTATUS status;
114
115         cli->tree = smbcli_tree_init(cli->session);
116         talloc_free(cli->session);
117         if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
118
119         mem_ctx = talloc_init("tcon");
120         if (!mem_ctx) {
121                 return NT_STATUS_NO_MEMORY;
122         }
123
124         /* setup a tree connect */
125         tcon.generic.level = RAW_TCON_TCONX;
126         tcon.tconx.in.flags = 0;
127         if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
128                 tcon.tconx.in.password = data_blob(NULL, 0);
129         } else if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
130                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24);
131                 if (cli->transport->negotiate.secblob.length < 8) {
132                         return NT_STATUS_INVALID_PARAMETER;
133                 }
134                 SMBencrypt(password, cli->transport->negotiate.secblob.data, tcon.tconx.in.password.data);
135         } else {
136                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1);
137         }
138         tcon.tconx.in.path = sharename;
139         tcon.tconx.in.device = devtype;
140         
141         status = smb_tree_connect(cli->tree, mem_ctx, &tcon);
142
143         cli->tree->tid = tcon.tconx.out.tid;
144
145         talloc_free(mem_ctx);
146
147         return status;
148 }
149
150
151 /*
152   easy way to get to a fully connected smbcli_state in one call
153 */
154 NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
155                                 struct smbcli_state **ret_cli, 
156                                 const char *myname,
157                                 const char *host,
158                                 const char *sharename,
159                                 const char *devtype,
160                                 const char *username,
161                                 const char *domain,
162                                 const char *password)
163 {
164         struct smbcli_tree *tree;
165         NTSTATUS status;
166         char *p;
167         TALLOC_CTX *mem_ctx;
168
169         mem_ctx = talloc_init("smbcli_full_connection");
170
171         *ret_cli = NULL;
172
173         /* if the username is of the form DOMAIN\username then split out the domain */
174         p = strpbrk(username, "\\/");
175         if (p) {
176                 domain = talloc_strndup(mem_ctx, username, PTR_DIFF(p, username));
177                 username = talloc_strdup(mem_ctx, p+1);
178         }
179
180         status = smbcli_tree_full_connection(parent_ctx,
181                                              &tree, myname, host, 0, sharename, devtype,
182                                              username, domain, password);
183         if (!NT_STATUS_IS_OK(status)) {
184                 goto done;
185         }
186
187         (*ret_cli) = smbcli_state_init(parent_ctx);
188
189         (*ret_cli)->tree = tree;
190         (*ret_cli)->session = tree->session;
191         (*ret_cli)->transport = tree->session->transport;
192         talloc_steal(*ret_cli, tree->session->transport->socket);
193         
194 done:
195         talloc_free(mem_ctx);
196
197         return status;
198 }
199
200
201 /*
202   disconnect the tree
203 */
204 NTSTATUS smbcli_tdis(struct smbcli_state *cli)
205 {
206         return smb_tree_disconnect(cli->tree);
207 }
208
209 /****************************************************************************
210  Initialise a client state structure.
211 ****************************************************************************/
212 struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
213 {
214         struct smbcli_state *cli;
215
216         cli = talloc_zero_p(mem_ctx, struct smbcli_state);
217         if (cli) {
218                 ZERO_STRUCTP(cli);
219         }
220
221         return cli;
222 }
223
224 /****************************************************************************
225  Shutdown a client structure.
226 ****************************************************************************/
227 void smbcli_shutdown(struct smbcli_state *cli)
228 {
229         talloc_free(cli);
230 }
231
232 /*
233   parse a //server/share type UNC name
234 */
235 BOOL smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
236                       const char **hostname, const char **sharename)
237 {
238         char *p;
239
240         if (strncmp(unc_name, "\\\\", 2) &&
241             strncmp(unc_name, "//", 2)) {
242                 return False;
243         }
244
245         *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
246         p = strchr_m(&(*hostname)[2],'/');
247         if (!p) {
248                 p = strchr_m(&(*hostname)[2],'\\');
249                 if (!p) return False;
250         }
251         *p = 0;
252         *sharename = talloc_strdup(mem_ctx, p+1);
253
254         return True;
255 }
256
257
258