r2654: fixed some more server memory leaks. We are now down to a single leak
[sfrench/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
23 /*
24   wrapper around smbcli_sock_connect()
25 */
26 BOOL smbcli_socket_connect(struct smbcli_state *cli, const char *server)
27 {
28         struct smbcli_socket *sock;
29
30         sock = smbcli_sock_init();
31         if (!sock) return False;
32
33         if (!smbcli_sock_connect_byname(sock, server, 0)) {
34                 smbcli_sock_close(sock);
35                 return False;
36         }
37         
38         cli->transport = smbcli_transport_init(sock);
39         if (!cli->transport) {
40                 smbcli_sock_close(sock);
41                 return False;
42         }
43
44         return True;
45 }
46
47 /* wrapper around smbcli_transport_connect() */
48 BOOL smbcli_transport_establish(struct smbcli_state *cli, 
49                              struct nmb_name *calling,
50                              struct nmb_name *called)
51 {
52         return smbcli_transport_connect(cli->transport, calling, called);
53 }
54
55 /* wrapper around smb_raw_negotiate() */
56 NTSTATUS smbcli_negprot(struct smbcli_state *cli)
57 {
58         return smb_raw_negotiate(cli->transport);
59 }
60
61 /* wrapper around smb_raw_session_setup() */
62 NTSTATUS smbcli_session_setup(struct smbcli_state *cli, 
63                            const char *user, 
64                            const char *password, 
65                            const char *domain)
66 {
67         union smb_sesssetup setup;
68         NTSTATUS status;
69         TALLOC_CTX *mem_ctx;
70
71         cli->session = smbcli_session_init(cli->transport);
72         if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
73
74         mem_ctx = talloc_init("smbcli_session_setup");
75         if (!mem_ctx) return NT_STATUS_NO_MEMORY;
76
77         setup.generic.level = RAW_SESSSETUP_GENERIC;
78         setup.generic.in.sesskey = cli->transport->negotiate.sesskey;
79         setup.generic.in.capabilities = cli->transport->negotiate.capabilities;
80         if (!user || !user[0]) {
81                 setup.generic.in.password = NULL;
82                 setup.generic.in.user = "";
83                 setup.generic.in.domain = "";
84                 setup.generic.in.capabilities &= ~CAP_EXTENDED_SECURITY;
85         } else {
86                 if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
87                         setup.generic.in.password = password;
88                 } else {
89                         setup.generic.in.password = NULL;
90                 }
91                 setup.generic.in.user = user;
92                 setup.generic.in.domain = domain;
93         }
94
95         status = smb_raw_session_setup(cli->session, mem_ctx, &setup);
96
97         cli->session->vuid = setup.generic.out.vuid;
98
99         talloc_free(mem_ctx);
100
101         return status;
102 }
103
104 /* wrapper around smb_tree_connect() */
105 NTSTATUS smbcli_send_tconX(struct smbcli_state *cli, const char *sharename, 
106                            const char *devtype, const char *password)
107 {
108         union smb_tcon tcon;
109         TALLOC_CTX *mem_ctx;
110         NTSTATUS status;
111
112         cli->tree = smbcli_tree_init(cli->session);
113         if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
114
115         cli->tree->reference_count++;
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.cnum;
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(struct smbcli_state **ret_cli, 
153                              const char *myname,
154                              const char *host,
155                              struct in_addr *ip,
156                              const char *sharename,
157                              const char *devtype,
158                              const char *username,
159                              const char *domain,
160                              const char *password,
161                              uint_t flags,
162                              BOOL *retry)
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(&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();
187
188         (*ret_cli)->tree = tree;
189         (*ret_cli)->session = tree->session;
190         (*ret_cli)->transport = tree->session->transport;
191         tree->reference_count++;
192
193 done:
194         talloc_free(mem_ctx);
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(void)
211 {
212         struct smbcli_state *cli;
213
214         cli = talloc_p(NULL, 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         if (!cli) return;
228         if (cli->tree) {
229                 cli->tree->reference_count++;
230                 smbcli_tree_close(cli->tree);
231         }
232         talloc_free(cli);
233 }