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