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