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