r5929: Use cli_credentials for the SMB functions as well.
[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                                                           struct cli_credentials *credentials)
67 {
68         struct smb_composite_sesssetup setup;
69         NTSTATUS status;
70         TALLOC_CTX *mem_ctx;
71
72         cli->session = smbcli_session_init(cli->transport, cli, True);
73         if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
74
75         mem_ctx = talloc_init("smbcli_session_setup");
76         if (!mem_ctx) return NT_STATUS_NO_MEMORY;
77
78         setup.in.sesskey = cli->transport->negotiate.sesskey;
79         setup.in.capabilities = cli->transport->negotiate.capabilities;
80         if (cli_credentials_is_anonymous(credentials)) {
81                 setup.in.password = NULL;
82                 setup.in.user = "";
83                 setup.in.domain = "";
84                 setup.in.capabilities &= ~CAP_EXTENDED_SECURITY;
85         } else {
86                 if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
87                         setup.in.password = cli_credentials_get_password(credentials);
88                 } else {
89                         setup.in.password = NULL;
90                 }
91                 setup.in.user = cli_credentials_get_username(credentials);
92                 setup.in.domain = cli_credentials_get_domain(credentials);
93         }
94
95         status = smb_composite_sesssetup(cli->session, &setup);
96
97         cli->session->vuid = setup.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, cli, True);
113         if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
114
115         mem_ctx = talloc_init("tcon");
116         if (!mem_ctx) {
117                 return NT_STATUS_NO_MEMORY;
118         }
119
120         /* setup a tree connect */
121         tcon.generic.level = RAW_TCON_TCONX;
122         tcon.tconx.in.flags = 0;
123         if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
124                 tcon.tconx.in.password = data_blob(NULL, 0);
125         } else if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
126                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24);
127                 if (cli->transport->negotiate.secblob.length < 8) {
128                         return NT_STATUS_INVALID_PARAMETER;
129                 }
130                 SMBencrypt(password, cli->transport->negotiate.secblob.data, tcon.tconx.in.password.data);
131         } else {
132                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1);
133         }
134         tcon.tconx.in.path = sharename;
135         tcon.tconx.in.device = devtype;
136         
137         status = smb_tree_connect(cli->tree, mem_ctx, &tcon);
138
139         cli->tree->tid = tcon.tconx.out.tid;
140
141         talloc_free(mem_ctx);
142
143         return status;
144 }
145
146
147 /*
148   easy way to get to a fully connected smbcli_state in one call
149 */
150 NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
151                                 struct smbcli_state **ret_cli, 
152                                 const char *myname,
153                                 const char *host,
154                                 const char *sharename,
155                                 const char *devtype,
156                                 struct cli_credentials *credentials)
157 {
158         struct smbcli_tree *tree;
159         NTSTATUS status;
160         TALLOC_CTX *mem_ctx;
161
162         mem_ctx = talloc_init("smbcli_full_connection");
163
164         *ret_cli = NULL;
165
166         status = smbcli_tree_full_connection(parent_ctx,
167                                              &tree, myname, host, 0, sharename, devtype,
168                                              credentials);
169         if (!NT_STATUS_IS_OK(status)) {
170                 goto done;
171         }
172
173         (*ret_cli) = smbcli_state_init(parent_ctx);
174
175         (*ret_cli)->tree = tree;
176         (*ret_cli)->session = tree->session;
177         (*ret_cli)->transport = tree->session->transport;
178         
179 done:
180         talloc_free(mem_ctx);
181
182         return status;
183 }
184
185
186 /*
187   disconnect the tree
188 */
189 NTSTATUS smbcli_tdis(struct smbcli_state *cli)
190 {
191         return smb_tree_disconnect(cli->tree);
192 }
193
194 /****************************************************************************
195  Initialise a client state structure.
196 ****************************************************************************/
197 struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
198 {
199         struct smbcli_state *cli;
200
201         cli = talloc_zero(mem_ctx, struct smbcli_state);
202         if (cli) {
203                 ZERO_STRUCTP(cli);
204         }
205
206         return cli;
207 }
208
209 /****************************************************************************
210  Shutdown a client structure.
211 ****************************************************************************/
212 void smbcli_shutdown(struct smbcli_state *cli)
213 {
214         talloc_free(cli);
215 }
216
217 /*
218   parse a //server/share type UNC name
219 */
220 BOOL smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
221                       const char **hostname, const char **sharename)
222 {
223         char *p;
224
225         if (strncmp(unc_name, "\\\\", 2) &&
226             strncmp(unc_name, "//", 2)) {
227                 return False;
228         }
229
230         *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
231         p = strchr_m(&(*hostname)[2],'/');
232         if (!p) {
233                 p = strchr_m(&(*hostname)[2],'\\');
234                 if (!p) return False;
235         }
236         *p = 0;
237         *sharename = talloc_strdup(mem_ctx, p+1);
238
239         return True;
240 }
241
242
243