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