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