r23792: convert Samba4 to GPLv3
[ira/wip.git] / source / libcli / cliconnect.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    client connect/disconnect routines
5
6    Copyright (C) Andrew Tridgell 2003-2005
7    Copyright (C) James Peach 2005
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "libcli/libcli.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/auth/libcli_auth.h"
27 #include "libcli/smb_composite/smb_composite.h"
28
29 /*
30   wrapper around smbcli_sock_connect()
31 */
32 BOOL smbcli_socket_connect(struct smbcli_state *cli, const char *server)
33 {
34         struct smbcli_socket *sock;
35
36         sock = smbcli_sock_connect_byname(server, 0, NULL, NULL);
37
38         if (sock == NULL) return False;
39         
40         cli->transport = smbcli_transport_init(sock, cli, True);
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 nbt_name *calling,
51                                 struct nbt_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, lp_cli_maxprotocol());
60 }
61
62 /* wrapper around smb_raw_sesssetup() */
63 NTSTATUS smbcli_session_setup(struct smbcli_state *cli, 
64                               struct cli_credentials *credentials)
65 {
66         struct smb_composite_sesssetup setup;
67         NTSTATUS status;
68
69         cli->session = smbcli_session_init(cli->transport, cli, True);
70         if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
71
72         setup.in.sesskey = cli->transport->negotiate.sesskey;
73         setup.in.capabilities = cli->transport->negotiate.capabilities;
74         setup.in.credentials = credentials;
75         setup.in.workgroup = lp_workgroup();
76
77         status = smb_composite_sesssetup(cli->session, &setup);
78
79         cli->session->vuid = setup.out.vuid;
80
81         return status;
82 }
83
84 /* wrapper around smb_raw_tcon() */
85 NTSTATUS smbcli_tconX(struct smbcli_state *cli, const char *sharename, 
86                       const char *devtype, const char *password)
87 {
88         union smb_tcon tcon;
89         TALLOC_CTX *mem_ctx;
90         NTSTATUS status;
91
92         cli->tree = smbcli_tree_init(cli->session, cli, True);
93         if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
94
95         mem_ctx = talloc_init("tcon");
96         if (!mem_ctx) {
97                 return NT_STATUS_NO_MEMORY;
98         }
99
100         /* setup a tree connect */
101         tcon.generic.level = RAW_TCON_TCONX;
102         tcon.tconx.in.flags = 0;
103         if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
104                 tcon.tconx.in.password = data_blob(NULL, 0);
105         } else if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
106                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24);
107                 if (cli->transport->negotiate.secblob.length < 8) {
108                         return NT_STATUS_INVALID_PARAMETER;
109                 }
110                 SMBencrypt(password, cli->transport->negotiate.secblob.data, tcon.tconx.in.password.data);
111         } else {
112                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1);
113         }
114         tcon.tconx.in.path = sharename;
115         tcon.tconx.in.device = devtype;
116         
117         status = smb_raw_tcon(cli->tree, mem_ctx, &tcon);
118
119         cli->tree->tid = tcon.tconx.out.tid;
120
121         talloc_free(mem_ctx);
122
123         return status;
124 }
125
126
127 /*
128   easy way to get to a fully connected smbcli_state in one call
129 */
130 NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
131                                 struct smbcli_state **ret_cli, 
132                                 const char *host,
133                                 const char *sharename,
134                                 const char *devtype,
135                                 struct cli_credentials *credentials,
136                                 struct event_context *ev)
137 {
138         struct smbcli_tree *tree;
139         NTSTATUS status;
140
141         *ret_cli = NULL;
142
143         status = smbcli_tree_full_connection(parent_ctx,
144                                              &tree, host, 0, sharename, devtype,
145                                              credentials, ev);
146         if (!NT_STATUS_IS_OK(status)) {
147                 goto done;
148         }
149
150         (*ret_cli) = smbcli_state_init(parent_ctx);
151
152         (*ret_cli)->tree = tree;
153         (*ret_cli)->session = tree->session;
154         (*ret_cli)->transport = tree->session->transport;
155
156         talloc_steal(*ret_cli, tree);
157         
158 done:
159         return status;
160 }
161
162
163 /*
164   disconnect the tree
165 */
166 NTSTATUS smbcli_tdis(struct smbcli_state *cli)
167 {
168         return smb_tree_disconnect(cli->tree);
169 }
170
171 /****************************************************************************
172  Initialise a client state structure.
173 ****************************************************************************/
174 struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
175 {
176         return talloc_zero(mem_ctx, struct smbcli_state);
177 }
178
179 /* Insert a NULL at the first separator of the given path and return a pointer
180  * to the remainder of the string.
181  */
182 static char *
183 terminate_path_at_separator(char * path)
184 {
185         char * p;
186
187         if (!path) {
188                 return NULL;
189         }
190
191         if ((p = strchr_m(path, '/'))) {
192                 *p = '\0';
193                 return p + 1;
194         }
195
196         if ((p = strchr_m(path, '\\'))) {
197                 *p = '\0';
198                 return p + 1;
199         }
200         
201         /* No separator. */
202         return NULL;
203 }
204
205 /*
206   parse a //server/share type UNC name
207 */
208 BOOL smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
209                       char **hostname, char **sharename)
210 {
211         char *p;
212
213         *hostname = *sharename = NULL;
214
215         if (strncmp(unc_name, "\\\\", 2) &&
216             strncmp(unc_name, "//", 2)) {
217                 return False;
218         }
219
220         *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
221         p = terminate_path_at_separator(*hostname);
222
223         if (p && *p) {
224                 *sharename = talloc_strdup(mem_ctx, p);
225                 terminate_path_at_separator(*sharename);
226         } else {
227                 *sharename = talloc_strdup(mem_ctx, 
228                                            lp_parm_string(-1, "torture", "share"));
229         }
230
231         if (*hostname && *sharename) {
232                 return True;
233         }
234
235         talloc_free(*hostname);
236         talloc_free(*sharename);
237         *hostname = *sharename = NULL;
238         return False;
239 }
240
241
242