a56100fc80c50294d11dec09624f8126fb353306
[jelmer/samba4-debian.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 #include "param/param.h"
29
30 /*
31   wrapper around smbcli_sock_connect()
32 */
33 bool smbcli_socket_connect(struct smbcli_state *cli, const char *server)
34 {
35         struct smbcli_socket *sock;
36
37         sock = smbcli_sock_connect_byname(server, 0, NULL, NULL);
38
39         if (sock == NULL) return false;
40         
41         cli->transport = smbcli_transport_init(sock, cli, true);
42         if (!cli->transport) {
43                 return false;
44         }
45
46         return true;
47 }
48
49 /* wrapper around smbcli_transport_connect() */
50 bool smbcli_transport_establish(struct smbcli_state *cli, 
51                                 struct nbt_name *calling,
52                                 struct nbt_name *called)
53 {
54         return smbcli_transport_connect(cli->transport, calling, called);
55 }
56
57 /* wrapper around smb_raw_negotiate() */
58 NTSTATUS smbcli_negprot(struct smbcli_state *cli)
59 {
60         return smb_raw_negotiate(cli->transport, 
61                                  lp_cli_maxprotocol(global_loadparm));
62 }
63
64 /* wrapper around smb_raw_sesssetup() */
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
71         cli->session = smbcli_session_init(cli->transport, cli, true);
72         if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
73
74         setup.in.sesskey = cli->transport->negotiate.sesskey;
75         setup.in.capabilities = cli->transport->negotiate.capabilities;
76         setup.in.credentials = credentials;
77         setup.in.workgroup = lp_workgroup(global_loadparm);
78
79         status = smb_composite_sesssetup(cli->session, &setup);
80
81         cli->session->vuid = setup.out.vuid;
82
83         return status;
84 }
85
86 /* wrapper around smb_raw_tcon() */
87 NTSTATUS smbcli_tconX(struct smbcli_state *cli, const char *sharename, 
88                       const char *devtype, const char *password)
89 {
90         union smb_tcon tcon;
91         TALLOC_CTX *mem_ctx;
92         NTSTATUS status;
93
94         cli->tree = smbcli_tree_init(cli->session, cli, true);
95         if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
96
97         mem_ctx = talloc_init("tcon");
98         if (!mem_ctx) {
99                 return NT_STATUS_NO_MEMORY;
100         }
101
102         /* setup a tree connect */
103         tcon.generic.level = RAW_TCON_TCONX;
104         tcon.tconx.in.flags = 0;
105         if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
106                 tcon.tconx.in.password = data_blob(NULL, 0);
107         } else if (cli->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
108                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24);
109                 if (cli->transport->negotiate.secblob.length < 8) {
110                         return NT_STATUS_INVALID_PARAMETER;
111                 }
112                 SMBencrypt(password, cli->transport->negotiate.secblob.data, tcon.tconx.in.password.data);
113         } else {
114                 tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1);
115         }
116         tcon.tconx.in.path = sharename;
117         tcon.tconx.in.device = devtype;
118         
119         status = smb_raw_tcon(cli->tree, mem_ctx, &tcon);
120
121         cli->tree->tid = tcon.tconx.out.tid;
122
123         talloc_free(mem_ctx);
124
125         return status;
126 }
127
128
129 /*
130   easy way to get to a fully connected smbcli_state in one call
131 */
132 NTSTATUS smbcli_full_connection(TALLOC_CTX *parent_ctx,
133                                 struct smbcli_state **ret_cli, 
134                                 const char *host,
135                                 const char *sharename,
136                                 const char *devtype,
137                                 struct cli_credentials *credentials,
138                                 struct event_context *ev)
139 {
140         struct smbcli_tree *tree;
141         NTSTATUS status;
142
143         *ret_cli = NULL;
144
145         status = smbcli_tree_full_connection(parent_ctx,
146                                              &tree, host, 0, sharename, devtype,
147                                              credentials, ev);
148         if (!NT_STATUS_IS_OK(status)) {
149                 goto done;
150         }
151
152         (*ret_cli) = smbcli_state_init(parent_ctx);
153
154         (*ret_cli)->tree = tree;
155         (*ret_cli)->session = tree->session;
156         (*ret_cli)->transport = tree->session->transport;
157
158         talloc_steal(*ret_cli, tree);
159         
160 done:
161         return status;
162 }
163
164
165 /*
166   disconnect the tree
167 */
168 NTSTATUS smbcli_tdis(struct smbcli_state *cli)
169 {
170         return smb_tree_disconnect(cli->tree);
171 }
172
173 /****************************************************************************
174  Initialise a client state structure.
175 ****************************************************************************/
176 struct smbcli_state *smbcli_state_init(TALLOC_CTX *mem_ctx)
177 {
178         return talloc_zero(mem_ctx, struct smbcli_state);
179 }
180
181 /* Insert a NULL at the first separator of the given path and return a pointer
182  * to the remainder of the string.
183  */
184 static char *
185 terminate_path_at_separator(char * path)
186 {
187         char * p;
188
189         if (!path) {
190                 return NULL;
191         }
192
193         if ((p = strchr_m(path, '/'))) {
194                 *p = '\0';
195                 return p + 1;
196         }
197
198         if ((p = strchr_m(path, '\\'))) {
199                 *p = '\0';
200                 return p + 1;
201         }
202         
203         /* No separator. */
204         return NULL;
205 }
206
207 /*
208   parse a //server/share type UNC name
209 */
210 bool smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
211                       char **hostname, char **sharename)
212 {
213         char *p;
214
215         *hostname = *sharename = NULL;
216
217         if (strncmp(unc_name, "\\\\", 2) &&
218             strncmp(unc_name, "//", 2)) {
219                 return false;
220         }
221
222         *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
223         p = terminate_path_at_separator(*hostname);
224
225         if (p != NULL && *p) {
226                 *sharename = talloc_strdup(mem_ctx, p);
227                 terminate_path_at_separator(*sharename);
228         }
229
230         if (*hostname && *sharename) {
231                 return true;
232         }
233
234         talloc_free(*hostname);
235         talloc_free(*sharename);
236         *hostname = *sharename = NULL;
237         return false;
238 }
239
240
241