r3419: moved the libcli/raw structures into libcli/raw/libcliraw.h
[samba.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 "libcli/raw/libcliraw.h"
23
24 /*
25   wrapper around smbcli_sock_connect()
26 */
27 BOOL smbcli_socket_connect(struct smbcli_state *cli, const char *server)
28 {
29         struct smbcli_socket *sock;
30
31         sock = smbcli_sock_init(cli);
32         if (!sock) return False;
33
34         if (!smbcli_sock_connect_byname(sock, server, 0)) {
35                 talloc_free(sock);
36                 return False;
37         }
38         
39         cli->transport = smbcli_transport_init(sock);
40         talloc_free(sock);
41         if (!cli->transport) {
42                 return False;
43         }
44
45         return True;
46 }
47
48 /* wrapper around smbcli_transport_connect() */
49 BOOL smbcli_transport_establish(struct smbcli_state *cli, 
50                              struct nmb_name *calling,
51                              struct nmb_name *called)
52 {
53         return smbcli_transport_connect(cli->transport, calling, called);
54 }
55
56 /* wrapper around smb_raw_negotiate() */
57 NTSTATUS smbcli_negprot(struct smbcli_state *cli)
58 {
59         return smb_raw_negotiate(cli->transport);
60 }
61
62 /* wrapper around smb_raw_session_setup() */
63 NTSTATUS smbcli_session_setup(struct smbcli_state *cli, 
64                               const char *user, 
65                               const char *password, 
66                               const char *domain)
67 {
68         union smb_sesssetup setup;
69         NTSTATUS status;
70         TALLOC_CTX *mem_ctx;
71
72         cli->session = smbcli_session_init(cli->transport);
73         if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
74         talloc_free(cli->transport);
75
76         mem_ctx = talloc_init("smbcli_session_setup");
77         if (!mem_ctx) return NT_STATUS_NO_MEMORY;
78
79         setup.generic.level = RAW_SESSSETUP_GENERIC;
80         setup.generic.in.sesskey = cli->transport->negotiate.sesskey;
81         setup.generic.in.capabilities = cli->transport->negotiate.capabilities;
82         if (!user || !user[0]) {
83                 setup.generic.in.password = NULL;
84                 setup.generic.in.user = "";
85                 setup.generic.in.domain = "";
86                 setup.generic.in.capabilities &= ~CAP_EXTENDED_SECURITY;
87         } else {
88                 if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
89                         setup.generic.in.password = password;
90                 } else {
91                         setup.generic.in.password = NULL;
92                 }
93                 setup.generic.in.user = user;
94                 setup.generic.in.domain = domain;
95         }
96
97         status = smb_raw_session_setup(cli->session, mem_ctx, &setup);
98
99         cli->session->vuid = setup.generic.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);
115         talloc_free(cli->session);
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.cnum;
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                                 struct in_addr *ip,
158                                 const char *sharename,
159                                 const char *devtype,
160                                 const char *username,
161                                 const char *domain,
162                                 const char *password,
163                                 uint_t flags,
164                                 BOOL *retry)
165 {
166         struct smbcli_tree *tree;
167         NTSTATUS status;
168         char *p;
169         TALLOC_CTX *mem_ctx;
170
171         mem_ctx = talloc_init("smbcli_full_connection");
172
173         *ret_cli = NULL;
174
175         /* if the username is of the form DOMAIN\username then split out the domain */
176         p = strpbrk(username, "\\/");
177         if (p) {
178                 domain = talloc_strndup(mem_ctx, username, PTR_DIFF(p, username));
179                 username = talloc_strdup(mem_ctx, p+1);
180         }
181
182         status = smbcli_tree_full_connection(parent_ctx,
183                                              &tree, myname, host, 0, sharename, devtype,
184                                              username, domain, password);
185         if (!NT_STATUS_IS_OK(status)) {
186                 goto done;
187         }
188
189         (*ret_cli) = smbcli_state_init(parent_ctx);
190
191         (*ret_cli)->tree = tree;
192         (*ret_cli)->session = tree->session;
193         (*ret_cli)->transport = tree->session->transport;
194         talloc_steal(*ret_cli, tree->session->transport->socket);
195         
196 done:
197         talloc_free(mem_ctx);
198
199         return status;
200 }
201
202
203 /*
204   disconnect the tree
205 */
206 NTSTATUS smbcli_tdis(struct smbcli_state *cli)
207 {
208         return smb_tree_disconnect(cli->tree);
209 }
210
211 /****************************************************************************
212  Initialise a client state structure.
213 ****************************************************************************/
214 struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
215 {
216         struct smbcli_state *cli;
217
218         cli = talloc_p(mem_ctx, struct smbcli_state);
219         if (cli) {
220                 ZERO_STRUCTP(cli);
221         }
222
223         return cli;
224 }
225
226 /****************************************************************************
227  Shutdown a client structure.
228 ****************************************************************************/
229 void smbcli_shutdown(struct smbcli_state *cli)
230 {
231         talloc_free(cli);
232 }