1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | client connect/disconnect routines
|
---|
4 | Copyright (C) Andrew Tridgell 1994-1998
|
---|
5 | Copyright (C) Gerald (Jerry) Carter 2004
|
---|
6 | Copyright (C) Jeremy Allison 2007-2009
|
---|
7 |
|
---|
8 | This program is free software; you can redistribute it and/or modify
|
---|
9 | it under the terms of the GNU General Public License as published by
|
---|
10 | the Free Software Foundation; either version 3 of the License, or
|
---|
11 | (at your option) any later version.
|
---|
12 |
|
---|
13 | This program is distributed in the hope that it will be useful,
|
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | GNU General Public License for more details.
|
---|
17 |
|
---|
18 | You should have received a copy of the GNU General Public License
|
---|
19 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include "includes.h"
|
---|
23 | #include "libsmb/libsmb.h"
|
---|
24 | #include "libsmb/clirap.h"
|
---|
25 | #include "msdfs.h"
|
---|
26 | #include "trans2.h"
|
---|
27 | #include "libsmb/nmblib.h"
|
---|
28 | #include "../libcli/smb/smbXcli_base.h"
|
---|
29 |
|
---|
30 | /********************************************************************
|
---|
31 | Important point.
|
---|
32 |
|
---|
33 | DFS paths are *always* of the form \server\share\<pathname> (the \ characters
|
---|
34 | are not C escaped here).
|
---|
35 |
|
---|
36 | - but if we're using POSIX paths then <pathname> may contain
|
---|
37 | '/' separators, not '\\' separators. So cope with '\\' or '/'
|
---|
38 | as a separator when looking at the pathname part.... JRA.
|
---|
39 | ********************************************************************/
|
---|
40 |
|
---|
41 | /********************************************************************
|
---|
42 | Ensure a connection is encrypted.
|
---|
43 | ********************************************************************/
|
---|
44 |
|
---|
45 | NTSTATUS cli_cm_force_encryption(struct cli_state *c,
|
---|
46 | const char *username,
|
---|
47 | const char *password,
|
---|
48 | const char *domain,
|
---|
49 | const char *sharename)
|
---|
50 | {
|
---|
51 | NTSTATUS status;
|
---|
52 |
|
---|
53 | if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
|
---|
54 | status = smb2cli_session_encryption_on(c->smb2.session);
|
---|
55 | if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
|
---|
56 | d_printf("Encryption required and "
|
---|
57 | "server doesn't support "
|
---|
58 | "SMB3 encryption - failing connect\n");
|
---|
59 | } else if (!NT_STATUS_IS_OK(status)) {
|
---|
60 | d_printf("Encryption required and "
|
---|
61 | "setup failed with error %s.\n",
|
---|
62 | nt_errstr(status));
|
---|
63 | }
|
---|
64 | return status;
|
---|
65 | }
|
---|
66 |
|
---|
67 | status = cli_force_encryption(c,
|
---|
68 | username,
|
---|
69 | password,
|
---|
70 | domain);
|
---|
71 |
|
---|
72 | if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
|
---|
73 | d_printf("Encryption required and "
|
---|
74 | "server that doesn't support "
|
---|
75 | "UNIX extensions - failing connect\n");
|
---|
76 | } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNKNOWN_REVISION)) {
|
---|
77 | d_printf("Encryption required and "
|
---|
78 | "can't get UNIX CIFS extensions "
|
---|
79 | "version from server.\n");
|
---|
80 | } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
|
---|
81 | d_printf("Encryption required and "
|
---|
82 | "share %s doesn't support "
|
---|
83 | "encryption.\n", sharename);
|
---|
84 | } else if (!NT_STATUS_IS_OK(status)) {
|
---|
85 | d_printf("Encryption required and "
|
---|
86 | "setup failed with error %s.\n",
|
---|
87 | nt_errstr(status));
|
---|
88 | }
|
---|
89 |
|
---|
90 | return status;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /********************************************************************
|
---|
94 | Return a connection to a server.
|
---|
95 | ********************************************************************/
|
---|
96 |
|
---|
97 | static NTSTATUS do_connect(TALLOC_CTX *ctx,
|
---|
98 | const char *server,
|
---|
99 | const char *share,
|
---|
100 | const struct user_auth_info *auth_info,
|
---|
101 | bool show_sessetup,
|
---|
102 | bool force_encrypt,
|
---|
103 | int max_protocol,
|
---|
104 | int port,
|
---|
105 | int name_type,
|
---|
106 | struct cli_state **pcli)
|
---|
107 | {
|
---|
108 | struct cli_state *c = NULL;
|
---|
109 | char *servicename;
|
---|
110 | char *sharename;
|
---|
111 | char *newserver, *newshare;
|
---|
112 | const char *username;
|
---|
113 | const char *password;
|
---|
114 | const char *domain;
|
---|
115 | NTSTATUS status;
|
---|
116 | int flags = 0;
|
---|
117 | int signing_state = get_cmdline_auth_info_signing_state(auth_info);
|
---|
118 |
|
---|
119 | if (force_encrypt) {
|
---|
120 | signing_state = SMB_SIGNING_REQUIRED;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /* make a copy so we don't modify the global string 'service' */
|
---|
124 | servicename = talloc_strdup(ctx,share);
|
---|
125 | if (!servicename) {
|
---|
126 | return NT_STATUS_NO_MEMORY;
|
---|
127 | }
|
---|
128 | sharename = servicename;
|
---|
129 | if (*sharename == '\\') {
|
---|
130 | sharename += 2;
|
---|
131 | if (server == NULL) {
|
---|
132 | server = sharename;
|
---|
133 | }
|
---|
134 | sharename = strchr_m(sharename,'\\');
|
---|
135 | if (!sharename) {
|
---|
136 | return NT_STATUS_NO_MEMORY;
|
---|
137 | }
|
---|
138 | *sharename = 0;
|
---|
139 | sharename++;
|
---|
140 | }
|
---|
141 | if (server == NULL) {
|
---|
142 | return NT_STATUS_INVALID_PARAMETER;
|
---|
143 | }
|
---|
144 |
|
---|
145 | if (get_cmdline_auth_info_use_kerberos(auth_info)) {
|
---|
146 | flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
|
---|
147 | }
|
---|
148 | if (get_cmdline_auth_info_fallback_after_kerberos(auth_info)) {
|
---|
149 | flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
|
---|
150 | }
|
---|
151 | if (get_cmdline_auth_info_use_ccache(auth_info)) {
|
---|
152 | flags |= CLI_FULL_CONNECTION_USE_CCACHE;
|
---|
153 | }
|
---|
154 | if (get_cmdline_auth_info_use_pw_nt_hash(auth_info)) {
|
---|
155 | flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
|
---|
156 | }
|
---|
157 |
|
---|
158 | status = cli_connect_nb(
|
---|
159 | server, NULL, port, name_type, NULL,
|
---|
160 | signing_state,
|
---|
161 | flags, &c);
|
---|
162 |
|
---|
163 | if (!NT_STATUS_IS_OK(status)) {
|
---|
164 | d_printf("Connection to %s failed (Error %s)\n",
|
---|
165 | server,
|
---|
166 | nt_errstr(status));
|
---|
167 | return status;
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (max_protocol == 0) {
|
---|
171 | max_protocol = PROTOCOL_NT1;
|
---|
172 | }
|
---|
173 | DEBUG(4,(" session request ok\n"));
|
---|
174 |
|
---|
175 | status = smbXcli_negprot(c->conn, c->timeout,
|
---|
176 | lp_client_min_protocol(),
|
---|
177 | max_protocol);
|
---|
178 |
|
---|
179 | if (!NT_STATUS_IS_OK(status)) {
|
---|
180 | d_printf("protocol negotiation failed: %s\n",
|
---|
181 | nt_errstr(status));
|
---|
182 | cli_shutdown(c);
|
---|
183 | return status;
|
---|
184 | }
|
---|
185 |
|
---|
186 | if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
|
---|
187 | /* Ensure we ask for some initial credits. */
|
---|
188 | smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
|
---|
189 | }
|
---|
190 |
|
---|
191 | username = get_cmdline_auth_info_username(auth_info);
|
---|
192 | password = get_cmdline_auth_info_password(auth_info);
|
---|
193 | domain = get_cmdline_auth_info_domain(auth_info);
|
---|
194 | if ((domain == NULL) || (domain[0] == '\0')) {
|
---|
195 | domain = lp_workgroup();
|
---|
196 | }
|
---|
197 |
|
---|
198 | status = cli_session_setup(c, username,
|
---|
199 | password, strlen(password),
|
---|
200 | password, strlen(password),
|
---|
201 | domain);
|
---|
202 | if (!NT_STATUS_IS_OK(status)) {
|
---|
203 | /* If a password was not supplied then
|
---|
204 | * try again with a null username. */
|
---|
205 | if (password[0] || !username[0] ||
|
---|
206 | get_cmdline_auth_info_use_kerberos(auth_info) ||
|
---|
207 | !NT_STATUS_IS_OK(status = cli_session_setup(c, "",
|
---|
208 | "", 0,
|
---|
209 | "", 0,
|
---|
210 | lp_workgroup()))) {
|
---|
211 | d_printf("session setup failed: %s\n",
|
---|
212 | nt_errstr(status));
|
---|
213 | if (NT_STATUS_EQUAL(status,
|
---|
214 | NT_STATUS_MORE_PROCESSING_REQUIRED))
|
---|
215 | d_printf("did you forget to run kinit?\n");
|
---|
216 | cli_shutdown(c);
|
---|
217 | return status;
|
---|
218 | }
|
---|
219 | d_printf("Anonymous login successful\n");
|
---|
220 | }
|
---|
221 |
|
---|
222 | if (!NT_STATUS_IS_OK(status)) {
|
---|
223 | DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
|
---|
224 | cli_shutdown(c);
|
---|
225 | return status;
|
---|
226 | }
|
---|
227 |
|
---|
228 | if ( show_sessetup ) {
|
---|
229 | if (*c->server_domain) {
|
---|
230 | DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
|
---|
231 | c->server_domain,c->server_os,c->server_type));
|
---|
232 | } else if (*c->server_os || *c->server_type) {
|
---|
233 | DEBUG(0,("OS=[%s] Server=[%s]\n",
|
---|
234 | c->server_os,c->server_type));
|
---|
235 | }
|
---|
236 | }
|
---|
237 | DEBUG(4,(" session setup ok\n"));
|
---|
238 |
|
---|
239 | /* here's the fun part....to support 'msdfs proxy' shares
|
---|
240 | (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
|
---|
241 | here before trying to connect to the original share.
|
---|
242 | cli_check_msdfs_proxy() will fail if it is a normal share. */
|
---|
243 |
|
---|
244 | if (smbXcli_conn_dfs_supported(c->conn) &&
|
---|
245 | cli_check_msdfs_proxy(ctx, c, sharename,
|
---|
246 | &newserver, &newshare,
|
---|
247 | force_encrypt,
|
---|
248 | username,
|
---|
249 | password,
|
---|
250 | domain)) {
|
---|
251 | cli_shutdown(c);
|
---|
252 | return do_connect(ctx, newserver,
|
---|
253 | newshare, auth_info, false,
|
---|
254 | force_encrypt, max_protocol,
|
---|
255 | port, name_type, pcli);
|
---|
256 | }
|
---|
257 |
|
---|
258 | /* must be a normal share */
|
---|
259 |
|
---|
260 | status = cli_tree_connect(c, sharename, "?????",
|
---|
261 | password, strlen(password)+1);
|
---|
262 | if (!NT_STATUS_IS_OK(status)) {
|
---|
263 | d_printf("tree connect failed: %s\n", nt_errstr(status));
|
---|
264 | cli_shutdown(c);
|
---|
265 | return status;
|
---|
266 | }
|
---|
267 |
|
---|
268 | if (force_encrypt) {
|
---|
269 | status = cli_cm_force_encryption(c,
|
---|
270 | username,
|
---|
271 | password,
|
---|
272 | domain,
|
---|
273 | sharename);
|
---|
274 | if (!NT_STATUS_IS_OK(status)) {
|
---|
275 | cli_shutdown(c);
|
---|
276 | return status;
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | DEBUG(4,(" tconx ok\n"));
|
---|
281 | *pcli = c;
|
---|
282 | return NT_STATUS_OK;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /****************************************************************************
|
---|
286 | ****************************************************************************/
|
---|
287 |
|
---|
288 | static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
|
---|
289 | {
|
---|
290 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
291 | char *name = clean_name(frame, mnt);
|
---|
292 | if (!name) {
|
---|
293 | TALLOC_FREE(frame);
|
---|
294 | return;
|
---|
295 | }
|
---|
296 | TALLOC_FREE(cli->dfs_mountpoint);
|
---|
297 | cli->dfs_mountpoint = talloc_strdup(cli, name);
|
---|
298 | TALLOC_FREE(frame);
|
---|
299 | }
|
---|
300 |
|
---|
301 | /********************************************************************
|
---|
302 | Add a new connection to the list.
|
---|
303 | referring_cli == NULL means a new initial connection.
|
---|
304 | ********************************************************************/
|
---|
305 |
|
---|
306 | static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
|
---|
307 | struct cli_state *referring_cli,
|
---|
308 | const char *server,
|
---|
309 | const char *share,
|
---|
310 | const struct user_auth_info *auth_info,
|
---|
311 | bool show_hdr,
|
---|
312 | bool force_encrypt,
|
---|
313 | int max_protocol,
|
---|
314 | int port,
|
---|
315 | int name_type,
|
---|
316 | struct cli_state **pcli)
|
---|
317 | {
|
---|
318 | struct cli_state *cli;
|
---|
319 | NTSTATUS status;
|
---|
320 |
|
---|
321 | status = do_connect(ctx, server, share,
|
---|
322 | auth_info,
|
---|
323 | show_hdr, force_encrypt, max_protocol,
|
---|
324 | port, name_type, &cli);
|
---|
325 |
|
---|
326 | if (!NT_STATUS_IS_OK(status)) {
|
---|
327 | return status;
|
---|
328 | }
|
---|
329 |
|
---|
330 | /* Enter into the list. */
|
---|
331 | if (referring_cli) {
|
---|
332 | DLIST_ADD_END(referring_cli, cli);
|
---|
333 | }
|
---|
334 |
|
---|
335 | if (referring_cli && referring_cli->requested_posix_capabilities) {
|
---|
336 | uint16_t major, minor;
|
---|
337 | uint32_t caplow, caphigh;
|
---|
338 | status = cli_unix_extensions_version(cli, &major, &minor,
|
---|
339 | &caplow, &caphigh);
|
---|
340 | if (NT_STATUS_IS_OK(status)) {
|
---|
341 | cli_set_unix_extensions_capabilities(cli,
|
---|
342 | major, minor,
|
---|
343 | caplow, caphigh);
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 | *pcli = cli;
|
---|
348 | return NT_STATUS_OK;
|
---|
349 | }
|
---|
350 |
|
---|
351 | /********************************************************************
|
---|
352 | Return a connection to a server on a particular share.
|
---|
353 | ********************************************************************/
|
---|
354 |
|
---|
355 | static struct cli_state *cli_cm_find(struct cli_state *cli,
|
---|
356 | const char *server,
|
---|
357 | const char *share)
|
---|
358 | {
|
---|
359 | struct cli_state *p;
|
---|
360 |
|
---|
361 | if (cli == NULL) {
|
---|
362 | return NULL;
|
---|
363 | }
|
---|
364 |
|
---|
365 | /* Search to the start of the list. */
|
---|
366 | for (p = cli; p; p = DLIST_PREV(p)) {
|
---|
367 | const char *remote_name =
|
---|
368 | smbXcli_conn_remote_name(p->conn);
|
---|
369 |
|
---|
370 | if (strequal(server, remote_name) &&
|
---|
371 | strequal(share,p->share)) {
|
---|
372 | return p;
|
---|
373 | }
|
---|
374 | }
|
---|
375 |
|
---|
376 | /* Search to the end of the list. */
|
---|
377 | for (p = cli->next; p; p = p->next) {
|
---|
378 | const char *remote_name =
|
---|
379 | smbXcli_conn_remote_name(p->conn);
|
---|
380 |
|
---|
381 | if (strequal(server, remote_name) &&
|
---|
382 | strequal(share,p->share)) {
|
---|
383 | return p;
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | return NULL;
|
---|
388 | }
|
---|
389 |
|
---|
390 | /****************************************************************************
|
---|
391 | Open a client connection to a \\server\share.
|
---|
392 | ****************************************************************************/
|
---|
393 |
|
---|
394 | NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
|
---|
395 | struct cli_state *referring_cli,
|
---|
396 | const char *server,
|
---|
397 | const char *share,
|
---|
398 | const struct user_auth_info *auth_info,
|
---|
399 | bool show_hdr,
|
---|
400 | bool force_encrypt,
|
---|
401 | int max_protocol,
|
---|
402 | int port,
|
---|
403 | int name_type,
|
---|
404 | struct cli_state **pcli)
|
---|
405 | {
|
---|
406 | /* Try to reuse an existing connection in this list. */
|
---|
407 | struct cli_state *c = cli_cm_find(referring_cli, server, share);
|
---|
408 | NTSTATUS status;
|
---|
409 |
|
---|
410 | if (c) {
|
---|
411 | *pcli = c;
|
---|
412 | return NT_STATUS_OK;
|
---|
413 | }
|
---|
414 |
|
---|
415 | if (auth_info == NULL) {
|
---|
416 | /* Can't do a new connection
|
---|
417 | * without auth info. */
|
---|
418 | d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
|
---|
419 | "without auth info\n",
|
---|
420 | server, share );
|
---|
421 | return NT_STATUS_INVALID_PARAMETER;
|
---|
422 | }
|
---|
423 |
|
---|
424 | status = cli_cm_connect(ctx,
|
---|
425 | referring_cli,
|
---|
426 | server,
|
---|
427 | share,
|
---|
428 | auth_info,
|
---|
429 | show_hdr,
|
---|
430 | force_encrypt,
|
---|
431 | max_protocol,
|
---|
432 | port,
|
---|
433 | name_type,
|
---|
434 | &c);
|
---|
435 | if (!NT_STATUS_IS_OK(status)) {
|
---|
436 | return status;
|
---|
437 | }
|
---|
438 | *pcli = c;
|
---|
439 | return NT_STATUS_OK;
|
---|
440 | }
|
---|
441 |
|
---|
442 | /****************************************************************************
|
---|
443 | ****************************************************************************/
|
---|
444 |
|
---|
445 | void cli_cm_display(struct cli_state *cli)
|
---|
446 | {
|
---|
447 | int i;
|
---|
448 |
|
---|
449 | for (i=0; cli; cli = cli->next,i++ ) {
|
---|
450 | d_printf("%d:\tserver=%s, share=%s\n",
|
---|
451 | i, smbXcli_conn_remote_name(cli->conn), cli->share);
|
---|
452 | }
|
---|
453 | }
|
---|
454 |
|
---|
455 | /****************************************************************************
|
---|
456 | ****************************************************************************/
|
---|
457 |
|
---|
458 | /****************************************************************************
|
---|
459 | ****************************************************************************/
|
---|
460 |
|
---|
461 | #if 0
|
---|
462 | void cli_cm_set_credentials(struct user_auth_info *auth_info)
|
---|
463 | {
|
---|
464 | SAFE_FREE(cm_creds.username);
|
---|
465 | cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
|
---|
466 | auth_info));
|
---|
467 |
|
---|
468 | if (get_cmdline_auth_info_got_pass(auth_info)) {
|
---|
469 | cm_set_password(get_cmdline_auth_info_password(auth_info));
|
---|
470 | }
|
---|
471 |
|
---|
472 | cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
|
---|
473 | cm_creds.fallback_after_kerberos = false;
|
---|
474 | cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
|
---|
475 | }
|
---|
476 | #endif
|
---|
477 |
|
---|
478 | /**********************************************************************
|
---|
479 | split a dfs path into the server, share name, and extrapath components
|
---|
480 | **********************************************************************/
|
---|
481 |
|
---|
482 | static bool split_dfs_path(TALLOC_CTX *ctx,
|
---|
483 | const char *nodepath,
|
---|
484 | char **pp_server,
|
---|
485 | char **pp_share,
|
---|
486 | char **pp_extrapath)
|
---|
487 | {
|
---|
488 | char *p, *q;
|
---|
489 | char *path;
|
---|
490 |
|
---|
491 | *pp_server = NULL;
|
---|
492 | *pp_share = NULL;
|
---|
493 | *pp_extrapath = NULL;
|
---|
494 |
|
---|
495 | path = talloc_strdup(ctx, nodepath);
|
---|
496 | if (!path) {
|
---|
497 | goto fail;
|
---|
498 | }
|
---|
499 |
|
---|
500 | if ( path[0] != '\\' ) {
|
---|
501 | goto fail;
|
---|
502 | }
|
---|
503 |
|
---|
504 | p = strchr_m( path + 1, '\\' );
|
---|
505 | if ( !p ) {
|
---|
506 | goto fail;
|
---|
507 | }
|
---|
508 |
|
---|
509 | *p = '\0';
|
---|
510 | p++;
|
---|
511 |
|
---|
512 | /* Look for any extra/deep path */
|
---|
513 | q = strchr_m(p, '\\');
|
---|
514 | if (q != NULL) {
|
---|
515 | *q = '\0';
|
---|
516 | q++;
|
---|
517 | *pp_extrapath = talloc_strdup(ctx, q);
|
---|
518 | } else {
|
---|
519 | *pp_extrapath = talloc_strdup(ctx, "");
|
---|
520 | }
|
---|
521 | if (*pp_extrapath == NULL) {
|
---|
522 | goto fail;
|
---|
523 | }
|
---|
524 |
|
---|
525 | *pp_share = talloc_strdup(ctx, p);
|
---|
526 | if (*pp_share == NULL) {
|
---|
527 | goto fail;
|
---|
528 | }
|
---|
529 |
|
---|
530 | *pp_server = talloc_strdup(ctx, &path[1]);
|
---|
531 | if (*pp_server == NULL) {
|
---|
532 | goto fail;
|
---|
533 | }
|
---|
534 |
|
---|
535 | TALLOC_FREE(path);
|
---|
536 | return true;
|
---|
537 |
|
---|
538 | fail:
|
---|
539 | TALLOC_FREE(*pp_share);
|
---|
540 | TALLOC_FREE(*pp_extrapath);
|
---|
541 | TALLOC_FREE(path);
|
---|
542 | return false;
|
---|
543 | }
|
---|
544 |
|
---|
545 | /****************************************************************************
|
---|
546 | Return the original path truncated at the directory component before
|
---|
547 | the first wildcard character. Trust the caller to provide a NULL
|
---|
548 | terminated string
|
---|
549 | ****************************************************************************/
|
---|
550 |
|
---|
551 | static char *clean_path(TALLOC_CTX *ctx, const char *path)
|
---|
552 | {
|
---|
553 | size_t len;
|
---|
554 | char *p1, *p2, *p;
|
---|
555 | char *path_out;
|
---|
556 |
|
---|
557 | /* No absolute paths. */
|
---|
558 | while (IS_DIRECTORY_SEP(*path)) {
|
---|
559 | path++;
|
---|
560 | }
|
---|
561 |
|
---|
562 | path_out = talloc_strdup(ctx, path);
|
---|
563 | if (!path_out) {
|
---|
564 | return NULL;
|
---|
565 | }
|
---|
566 |
|
---|
567 | p1 = strchr_m(path_out, '*');
|
---|
568 | p2 = strchr_m(path_out, '?');
|
---|
569 |
|
---|
570 | if (p1 || p2) {
|
---|
571 | if (p1 && p2) {
|
---|
572 | p = MIN(p1,p2);
|
---|
573 | } else if (!p1) {
|
---|
574 | p = p2;
|
---|
575 | } else {
|
---|
576 | p = p1;
|
---|
577 | }
|
---|
578 | *p = '\0';
|
---|
579 |
|
---|
580 | /* Now go back to the start of this component. */
|
---|
581 | p1 = strrchr_m(path_out, '/');
|
---|
582 | p2 = strrchr_m(path_out, '\\');
|
---|
583 | p = MAX(p1,p2);
|
---|
584 | if (p) {
|
---|
585 | *p = '\0';
|
---|
586 | }
|
---|
587 | }
|
---|
588 |
|
---|
589 | /* Strip any trailing separator */
|
---|
590 |
|
---|
591 | len = strlen(path_out);
|
---|
592 | if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
|
---|
593 | path_out[len-1] = '\0';
|
---|
594 | }
|
---|
595 |
|
---|
596 | return path_out;
|
---|
597 | }
|
---|
598 |
|
---|
599 | /****************************************************************************
|
---|
600 | ****************************************************************************/
|
---|
601 |
|
---|
602 | static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
|
---|
603 | struct cli_state *cli,
|
---|
604 | const char *dir)
|
---|
605 | {
|
---|
606 | char path_sep = '\\';
|
---|
607 |
|
---|
608 | /* Ensure the extrapath doesn't start with a separator. */
|
---|
609 | while (IS_DIRECTORY_SEP(*dir)) {
|
---|
610 | dir++;
|
---|
611 | }
|
---|
612 |
|
---|
613 | if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
|
---|
614 | path_sep = '/';
|
---|
615 | }
|
---|
616 | return talloc_asprintf(ctx, "%c%s%c%s%c%s",
|
---|
617 | path_sep,
|
---|
618 | smbXcli_conn_remote_name(cli->conn),
|
---|
619 | path_sep,
|
---|
620 | cli->share,
|
---|
621 | path_sep,
|
---|
622 | dir);
|
---|
623 | }
|
---|
624 |
|
---|
625 | /********************************************************************
|
---|
626 | check for dfs referral
|
---|
627 | ********************************************************************/
|
---|
628 |
|
---|
629 | static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
|
---|
630 | NTSTATUS status)
|
---|
631 | {
|
---|
632 | /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
|
---|
633 |
|
---|
634 | if (!(smbXcli_conn_use_unicode(cli->conn))) {
|
---|
635 | return false;
|
---|
636 | }
|
---|
637 | if (!(smb1cli_conn_capabilities(cli->conn) & CAP_STATUS32)) {
|
---|
638 | return false;
|
---|
639 | }
|
---|
640 | if (NT_STATUS_EQUAL(status, expected)) {
|
---|
641 | return true;
|
---|
642 | }
|
---|
643 | return false;
|
---|
644 | }
|
---|
645 |
|
---|
646 | /********************************************************************
|
---|
647 | Get the dfs referral link.
|
---|
648 | ********************************************************************/
|
---|
649 |
|
---|
650 | NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
|
---|
651 | struct cli_state *cli,
|
---|
652 | const char *path,
|
---|
653 | struct client_dfs_referral **refs,
|
---|
654 | size_t *num_refs,
|
---|
655 | size_t *consumed)
|
---|
656 | {
|
---|
657 | unsigned int param_len = 0;
|
---|
658 | uint16_t recv_flags2;
|
---|
659 | uint8_t *param = NULL;
|
---|
660 | uint8_t *rdata = NULL;
|
---|
661 | char *p;
|
---|
662 | char *endp;
|
---|
663 | smb_ucs2_t *path_ucs;
|
---|
664 | char *consumed_path = NULL;
|
---|
665 | uint16_t consumed_ucs;
|
---|
666 | uint16_t num_referrals;
|
---|
667 | struct client_dfs_referral *referrals = NULL;
|
---|
668 | NTSTATUS status;
|
---|
669 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
670 |
|
---|
671 | *num_refs = 0;
|
---|
672 | *refs = NULL;
|
---|
673 |
|
---|
674 | param = talloc_array(talloc_tos(), uint8_t, 2);
|
---|
675 | if (!param) {
|
---|
676 | status = NT_STATUS_NO_MEMORY;
|
---|
677 | goto out;
|
---|
678 | }
|
---|
679 | SSVAL(param, 0, 0x03); /* max referral level */
|
---|
680 |
|
---|
681 | param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
|
---|
682 | path, strlen(path)+1,
|
---|
683 | NULL);
|
---|
684 | if (!param) {
|
---|
685 | status = NT_STATUS_NO_MEMORY;
|
---|
686 | goto out;
|
---|
687 | }
|
---|
688 | param_len = talloc_get_size(param);
|
---|
689 | path_ucs = (smb_ucs2_t *)¶m[2];
|
---|
690 |
|
---|
691 | if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
|
---|
692 | DATA_BLOB in_input_buffer;
|
---|
693 | DATA_BLOB in_output_buffer = data_blob_null;
|
---|
694 | DATA_BLOB out_input_buffer = data_blob_null;
|
---|
695 | DATA_BLOB out_output_buffer = data_blob_null;
|
---|
696 |
|
---|
697 | in_input_buffer.data = param;
|
---|
698 | in_input_buffer.length = param_len;
|
---|
699 |
|
---|
700 | status = smb2cli_ioctl(cli->conn,
|
---|
701 | cli->timeout,
|
---|
702 | cli->smb2.session,
|
---|
703 | cli->smb2.tcon,
|
---|
704 | UINT64_MAX, /* in_fid_persistent */
|
---|
705 | UINT64_MAX, /* in_fid_volatile */
|
---|
706 | FSCTL_DFS_GET_REFERRALS,
|
---|
707 | 0, /* in_max_input_length */
|
---|
708 | &in_input_buffer,
|
---|
709 | CLI_BUFFER_SIZE, /* in_max_output_length */
|
---|
710 | &in_output_buffer,
|
---|
711 | SMB2_IOCTL_FLAG_IS_FSCTL,
|
---|
712 | talloc_tos(),
|
---|
713 | &out_input_buffer,
|
---|
714 | &out_output_buffer);
|
---|
715 | if (!NT_STATUS_IS_OK(status)) {
|
---|
716 | goto out;
|
---|
717 | }
|
---|
718 |
|
---|
719 | if (out_output_buffer.length < 4) {
|
---|
720 | status = NT_STATUS_INVALID_NETWORK_RESPONSE;
|
---|
721 | goto out;
|
---|
722 | }
|
---|
723 |
|
---|
724 | recv_flags2 = FLAGS2_UNICODE_STRINGS;
|
---|
725 | rdata = out_output_buffer.data;
|
---|
726 | endp = (char *)rdata + out_output_buffer.length;
|
---|
727 | } else {
|
---|
728 | unsigned int data_len = 0;
|
---|
729 | uint16_t setup[1];
|
---|
730 |
|
---|
731 | SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
|
---|
732 |
|
---|
733 | status = cli_trans(talloc_tos(), cli, SMBtrans2,
|
---|
734 | NULL, 0xffff, 0, 0,
|
---|
735 | setup, 1, 0,
|
---|
736 | param, param_len, 2,
|
---|
737 | NULL, 0, CLI_BUFFER_SIZE,
|
---|
738 | &recv_flags2,
|
---|
739 | NULL, 0, NULL, /* rsetup */
|
---|
740 | NULL, 0, NULL,
|
---|
741 | &rdata, 4, &data_len);
|
---|
742 | if (!NT_STATUS_IS_OK(status)) {
|
---|
743 | goto out;
|
---|
744 | }
|
---|
745 |
|
---|
746 | endp = (char *)rdata + data_len;
|
---|
747 | }
|
---|
748 |
|
---|
749 | consumed_ucs = SVAL(rdata, 0);
|
---|
750 | num_referrals = SVAL(rdata, 2);
|
---|
751 |
|
---|
752 | /* consumed_ucs is the number of bytes
|
---|
753 | * of the UCS2 path consumed not counting any
|
---|
754 | * terminating null. We need to convert
|
---|
755 | * back to unix charset and count again
|
---|
756 | * to get the number of bytes consumed from
|
---|
757 | * the incoming path. */
|
---|
758 |
|
---|
759 | errno = 0;
|
---|
760 | if (pull_string_talloc(talloc_tos(),
|
---|
761 | NULL,
|
---|
762 | 0,
|
---|
763 | &consumed_path,
|
---|
764 | path_ucs,
|
---|
765 | consumed_ucs,
|
---|
766 | STR_UNICODE) == 0) {
|
---|
767 | if (errno != 0) {
|
---|
768 | status = map_nt_error_from_unix(errno);
|
---|
769 | } else {
|
---|
770 | status = NT_STATUS_INVALID_NETWORK_RESPONSE;
|
---|
771 | }
|
---|
772 | goto out;
|
---|
773 | }
|
---|
774 | if (consumed_path == NULL) {
|
---|
775 | status = map_nt_error_from_unix(errno);
|
---|
776 | goto out;
|
---|
777 | }
|
---|
778 | *consumed = strlen(consumed_path);
|
---|
779 |
|
---|
780 | if (num_referrals != 0) {
|
---|
781 | uint16_t ref_version;
|
---|
782 | uint16_t ref_size;
|
---|
783 | int i;
|
---|
784 | uint16_t node_offset;
|
---|
785 |
|
---|
786 | referrals = talloc_array(ctx, struct client_dfs_referral,
|
---|
787 | num_referrals);
|
---|
788 |
|
---|
789 | if (!referrals) {
|
---|
790 | status = NT_STATUS_NO_MEMORY;
|
---|
791 | goto out;
|
---|
792 | }
|
---|
793 | /* start at the referrals array */
|
---|
794 |
|
---|
795 | p = (char *)rdata+8;
|
---|
796 | for (i=0; i<num_referrals && p < endp; i++) {
|
---|
797 | if (p + 18 > endp) {
|
---|
798 | goto out;
|
---|
799 | }
|
---|
800 | ref_version = SVAL(p, 0);
|
---|
801 | ref_size = SVAL(p, 2);
|
---|
802 | node_offset = SVAL(p, 16);
|
---|
803 |
|
---|
804 | if (ref_version != 3) {
|
---|
805 | p += ref_size;
|
---|
806 | continue;
|
---|
807 | }
|
---|
808 |
|
---|
809 | referrals[i].proximity = SVAL(p, 8);
|
---|
810 | referrals[i].ttl = SVAL(p, 10);
|
---|
811 |
|
---|
812 | if (p + node_offset > endp) {
|
---|
813 | status = NT_STATUS_INVALID_NETWORK_RESPONSE;
|
---|
814 | goto out;
|
---|
815 | }
|
---|
816 | clistr_pull_talloc(referrals,
|
---|
817 | (const char *)rdata,
|
---|
818 | recv_flags2,
|
---|
819 | &referrals[i].dfspath,
|
---|
820 | p+node_offset,
|
---|
821 | PTR_DIFF(endp, p+node_offset),
|
---|
822 | STR_TERMINATE|STR_UNICODE);
|
---|
823 |
|
---|
824 | if (!referrals[i].dfspath) {
|
---|
825 | status = map_nt_error_from_unix(errno);
|
---|
826 | goto out;
|
---|
827 | }
|
---|
828 | p += ref_size;
|
---|
829 | }
|
---|
830 | if (i < num_referrals) {
|
---|
831 | status = NT_STATUS_INVALID_NETWORK_RESPONSE;
|
---|
832 | goto out;
|
---|
833 | }
|
---|
834 | }
|
---|
835 |
|
---|
836 | *num_refs = num_referrals;
|
---|
837 | *refs = referrals;
|
---|
838 |
|
---|
839 | out:
|
---|
840 |
|
---|
841 | TALLOC_FREE(frame);
|
---|
842 | return status;
|
---|
843 | }
|
---|
844 |
|
---|
845 | /********************************************************************
|
---|
846 | ********************************************************************/
|
---|
847 | struct cli_dfs_path_split {
|
---|
848 | char *server;
|
---|
849 | char *share;
|
---|
850 | char *extrapath;
|
---|
851 | };
|
---|
852 |
|
---|
853 | NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
|
---|
854 | const char *mountpt,
|
---|
855 | const struct user_auth_info *dfs_auth_info,
|
---|
856 | struct cli_state *rootcli,
|
---|
857 | const char *path,
|
---|
858 | struct cli_state **targetcli,
|
---|
859 | char **pp_targetpath)
|
---|
860 | {
|
---|
861 | struct client_dfs_referral *refs = NULL;
|
---|
862 | size_t num_refs = 0;
|
---|
863 | size_t consumed = 0;
|
---|
864 | struct cli_state *cli_ipc = NULL;
|
---|
865 | char *dfs_path = NULL;
|
---|
866 | char *cleanpath = NULL;
|
---|
867 | char *extrapath = NULL;
|
---|
868 | int pathlen;
|
---|
869 | struct cli_state *newcli = NULL;
|
---|
870 | struct cli_state *ccli = NULL;
|
---|
871 | int count = 0;
|
---|
872 | char *newpath = NULL;
|
---|
873 | char *newmount = NULL;
|
---|
874 | char *ppath = NULL;
|
---|
875 | SMB_STRUCT_STAT sbuf;
|
---|
876 | uint32_t attributes;
|
---|
877 | NTSTATUS status;
|
---|
878 | struct smbXcli_tcon *root_tcon = NULL;
|
---|
879 | struct smbXcli_tcon *target_tcon = NULL;
|
---|
880 | struct cli_dfs_path_split *dfs_refs = NULL;
|
---|
881 |
|
---|
882 | if ( !rootcli || !path || !targetcli ) {
|
---|
883 | return NT_STATUS_INVALID_PARAMETER;
|
---|
884 | }
|
---|
885 |
|
---|
886 | /* Don't do anything if this is not a DFS root. */
|
---|
887 |
|
---|
888 | if (smbXcli_conn_protocol(rootcli->conn) >= PROTOCOL_SMB2_02) {
|
---|
889 | root_tcon = rootcli->smb2.tcon;
|
---|
890 | } else {
|
---|
891 | root_tcon = rootcli->smb1.tcon;
|
---|
892 | }
|
---|
893 |
|
---|
894 | if (!smbXcli_tcon_is_dfs_share(root_tcon)) {
|
---|
895 | *targetcli = rootcli;
|
---|
896 | *pp_targetpath = talloc_strdup(ctx, path);
|
---|
897 | if (!*pp_targetpath) {
|
---|
898 | return NT_STATUS_NO_MEMORY;
|
---|
899 | }
|
---|
900 | return NT_STATUS_OK;
|
---|
901 | }
|
---|
902 |
|
---|
903 | *targetcli = NULL;
|
---|
904 |
|
---|
905 | /* Send a trans2_query_path_info to check for a referral. */
|
---|
906 |
|
---|
907 | cleanpath = clean_path(ctx, path);
|
---|
908 | if (!cleanpath) {
|
---|
909 | return NT_STATUS_NO_MEMORY;
|
---|
910 | }
|
---|
911 |
|
---|
912 | dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
|
---|
913 | if (!dfs_path) {
|
---|
914 | return NT_STATUS_NO_MEMORY;
|
---|
915 | }
|
---|
916 |
|
---|
917 | status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
|
---|
918 | if (NT_STATUS_IS_OK(status)) {
|
---|
919 | /* This is an ordinary path, just return it. */
|
---|
920 | *targetcli = rootcli;
|
---|
921 | *pp_targetpath = talloc_strdup(ctx, path);
|
---|
922 | if (!*pp_targetpath) {
|
---|
923 | return NT_STATUS_NO_MEMORY;
|
---|
924 | }
|
---|
925 | goto done;
|
---|
926 | }
|
---|
927 |
|
---|
928 | /* Special case where client asked for a path that does not exist */
|
---|
929 |
|
---|
930 | if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
|
---|
931 | status)) {
|
---|
932 | *targetcli = rootcli;
|
---|
933 | *pp_targetpath = talloc_strdup(ctx, path);
|
---|
934 | if (!*pp_targetpath) {
|
---|
935 | return NT_STATUS_NO_MEMORY;
|
---|
936 | }
|
---|
937 | goto done;
|
---|
938 | }
|
---|
939 |
|
---|
940 | /* We got an error, check for DFS referral. */
|
---|
941 |
|
---|
942 | if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
|
---|
943 | status)) {
|
---|
944 | return status;
|
---|
945 | }
|
---|
946 |
|
---|
947 | /* Check for the referral. */
|
---|
948 |
|
---|
949 | status = cli_cm_open(ctx,
|
---|
950 | rootcli,
|
---|
951 | smbXcli_conn_remote_name(rootcli->conn),
|
---|
952 | "IPC$",
|
---|
953 | dfs_auth_info,
|
---|
954 | false,
|
---|
955 | smb1cli_conn_encryption_on(rootcli->conn),
|
---|
956 | smbXcli_conn_protocol(rootcli->conn),
|
---|
957 | 0,
|
---|
958 | 0x20,
|
---|
959 | &cli_ipc);
|
---|
960 | if (!NT_STATUS_IS_OK(status)) {
|
---|
961 | return status;
|
---|
962 | }
|
---|
963 |
|
---|
964 | status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
|
---|
965 | &num_refs, &consumed);
|
---|
966 | if (!NT_STATUS_IS_OK(status)) {
|
---|
967 | return status;
|
---|
968 | }
|
---|
969 |
|
---|
970 | if (!num_refs || !refs[0].dfspath) {
|
---|
971 | return NT_STATUS_NOT_FOUND;
|
---|
972 | }
|
---|
973 |
|
---|
974 | /*
|
---|
975 | * Bug#10123 - DFS referal entries can be provided in a random order,
|
---|
976 | * so check the connection cache for each item to avoid unnecessary
|
---|
977 | * reconnections.
|
---|
978 | */
|
---|
979 | dfs_refs = talloc_array(ctx, struct cli_dfs_path_split, num_refs);
|
---|
980 | if (dfs_refs == NULL) {
|
---|
981 | return NT_STATUS_NO_MEMORY;
|
---|
982 | }
|
---|
983 |
|
---|
984 | for (count = 0; count < num_refs; count++) {
|
---|
985 | if (!split_dfs_path(dfs_refs, refs[count].dfspath,
|
---|
986 | &dfs_refs[count].server,
|
---|
987 | &dfs_refs[count].share,
|
---|
988 | &dfs_refs[count].extrapath)) {
|
---|
989 | TALLOC_FREE(dfs_refs);
|
---|
990 | return NT_STATUS_NOT_FOUND;
|
---|
991 | }
|
---|
992 |
|
---|
993 | ccli = cli_cm_find(rootcli, dfs_refs[count].server,
|
---|
994 | dfs_refs[count].share);
|
---|
995 | if (ccli != NULL) {
|
---|
996 | extrapath = dfs_refs[count].extrapath;
|
---|
997 | *targetcli = ccli;
|
---|
998 | break;
|
---|
999 | }
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | /*
|
---|
1003 | * If no cached connection was found, then connect to the first live
|
---|
1004 | * referral server in the list.
|
---|
1005 | */
|
---|
1006 | for (count = 0; (ccli == NULL) && (count < num_refs); count++) {
|
---|
1007 | /* Connect to the target server & share */
|
---|
1008 | status = cli_cm_connect(ctx, rootcli,
|
---|
1009 | dfs_refs[count].server,
|
---|
1010 | dfs_refs[count].share,
|
---|
1011 | dfs_auth_info,
|
---|
1012 | false,
|
---|
1013 | smb1cli_conn_encryption_on(rootcli->conn),
|
---|
1014 | smbXcli_conn_protocol(rootcli->conn),
|
---|
1015 | 0,
|
---|
1016 | 0x20,
|
---|
1017 | targetcli);
|
---|
1018 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1019 | d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
|
---|
1020 | dfs_refs[count].server,
|
---|
1021 | dfs_refs[count].share);
|
---|
1022 | continue;
|
---|
1023 | } else {
|
---|
1024 | extrapath = dfs_refs[count].extrapath;
|
---|
1025 | break;
|
---|
1026 | }
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | /* No available referral server for the connection */
|
---|
1030 | if (*targetcli == NULL) {
|
---|
1031 | TALLOC_FREE(dfs_refs);
|
---|
1032 | return status;
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | /* Make sure to recreate the original string including any wildcards. */
|
---|
1036 |
|
---|
1037 | dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
|
---|
1038 | if (!dfs_path) {
|
---|
1039 | TALLOC_FREE(dfs_refs);
|
---|
1040 | return NT_STATUS_NO_MEMORY;
|
---|
1041 | }
|
---|
1042 | pathlen = strlen(dfs_path);
|
---|
1043 | consumed = MIN(pathlen, consumed);
|
---|
1044 | *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
|
---|
1045 | if (!*pp_targetpath) {
|
---|
1046 | TALLOC_FREE(dfs_refs);
|
---|
1047 | return NT_STATUS_NO_MEMORY;
|
---|
1048 | }
|
---|
1049 | dfs_path[consumed] = '\0';
|
---|
1050 |
|
---|
1051 | /*
|
---|
1052 | * *pp_targetpath is now the unconsumed part of the path.
|
---|
1053 | * dfs_path is now the consumed part of the path
|
---|
1054 | * (in \server\share\path format).
|
---|
1055 | */
|
---|
1056 |
|
---|
1057 | if (extrapath && strlen(extrapath) > 0) {
|
---|
1058 | /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
|
---|
1059 | /* put the trailing \ on the path, so to be save we put one in if needed */
|
---|
1060 | if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
|
---|
1061 | *pp_targetpath = talloc_asprintf(ctx,
|
---|
1062 | "%s\\%s",
|
---|
1063 | extrapath,
|
---|
1064 | *pp_targetpath);
|
---|
1065 | } else {
|
---|
1066 | *pp_targetpath = talloc_asprintf(ctx,
|
---|
1067 | "%s%s",
|
---|
1068 | extrapath,
|
---|
1069 | *pp_targetpath);
|
---|
1070 | }
|
---|
1071 | if (!*pp_targetpath) {
|
---|
1072 | TALLOC_FREE(dfs_refs);
|
---|
1073 | return NT_STATUS_NO_MEMORY;
|
---|
1074 | }
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 | /* parse out the consumed mount path */
|
---|
1078 | /* trim off the \server\share\ */
|
---|
1079 |
|
---|
1080 | ppath = dfs_path;
|
---|
1081 |
|
---|
1082 | if (*ppath != '\\') {
|
---|
1083 | d_printf("cli_resolve_path: "
|
---|
1084 | "dfs_path (%s) not in correct format.\n",
|
---|
1085 | dfs_path );
|
---|
1086 | TALLOC_FREE(dfs_refs);
|
---|
1087 | return NT_STATUS_NOT_FOUND;
|
---|
1088 | }
|
---|
1089 |
|
---|
1090 | ppath++; /* Now pointing at start of server name. */
|
---|
1091 |
|
---|
1092 | if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
|
---|
1093 | TALLOC_FREE(dfs_refs);
|
---|
1094 | return NT_STATUS_NOT_FOUND;
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | ppath++; /* Now pointing at start of share name. */
|
---|
1098 |
|
---|
1099 | if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
|
---|
1100 | TALLOC_FREE(dfs_refs);
|
---|
1101 | return NT_STATUS_NOT_FOUND;
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 | ppath++; /* Now pointing at path component. */
|
---|
1105 |
|
---|
1106 | newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
|
---|
1107 | if (!newmount) {
|
---|
1108 | TALLOC_FREE(dfs_refs);
|
---|
1109 | return NT_STATUS_NOT_FOUND;
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | cli_set_mntpoint(*targetcli, newmount);
|
---|
1113 |
|
---|
1114 | /* Check for another dfs referral, note that we are not
|
---|
1115 | checking for loops here. */
|
---|
1116 |
|
---|
1117 | if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
|
---|
1118 | status = cli_resolve_path(ctx,
|
---|
1119 | newmount,
|
---|
1120 | dfs_auth_info,
|
---|
1121 | *targetcli,
|
---|
1122 | *pp_targetpath,
|
---|
1123 | &newcli,
|
---|
1124 | &newpath);
|
---|
1125 | if (NT_STATUS_IS_OK(status)) {
|
---|
1126 | /*
|
---|
1127 | * When cli_resolve_path returns true here it's always
|
---|
1128 | * returning the complete path in newpath, so we're done
|
---|
1129 | * here.
|
---|
1130 | */
|
---|
1131 | *targetcli = newcli;
|
---|
1132 | *pp_targetpath = newpath;
|
---|
1133 | TALLOC_FREE(dfs_refs);
|
---|
1134 | return status;
|
---|
1135 | }
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | done:
|
---|
1139 |
|
---|
1140 | if (smbXcli_conn_protocol((*targetcli)->conn) >= PROTOCOL_SMB2_02) {
|
---|
1141 | target_tcon = (*targetcli)->smb2.tcon;
|
---|
1142 | } else {
|
---|
1143 | target_tcon = (*targetcli)->smb1.tcon;
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | /* If returning true ensure we return a dfs root full path. */
|
---|
1147 | if (smbXcli_tcon_is_dfs_share(target_tcon)) {
|
---|
1148 | dfs_path = talloc_strdup(ctx, *pp_targetpath);
|
---|
1149 | if (!dfs_path) {
|
---|
1150 | TALLOC_FREE(dfs_refs);
|
---|
1151 | return NT_STATUS_NO_MEMORY;
|
---|
1152 | }
|
---|
1153 | *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
|
---|
1154 | if (*pp_targetpath == NULL) {
|
---|
1155 | TALLOC_FREE(dfs_refs);
|
---|
1156 | return NT_STATUS_NO_MEMORY;
|
---|
1157 | }
|
---|
1158 | }
|
---|
1159 |
|
---|
1160 | TALLOC_FREE(dfs_refs);
|
---|
1161 | return NT_STATUS_OK;
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | /********************************************************************
|
---|
1165 | ********************************************************************/
|
---|
1166 |
|
---|
1167 | bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
|
---|
1168 | struct cli_state *cli,
|
---|
1169 | const char *sharename,
|
---|
1170 | char **pp_newserver,
|
---|
1171 | char **pp_newshare,
|
---|
1172 | bool force_encrypt,
|
---|
1173 | const char *username,
|
---|
1174 | const char *password,
|
---|
1175 | const char *domain)
|
---|
1176 | {
|
---|
1177 | struct client_dfs_referral *refs = NULL;
|
---|
1178 | size_t num_refs = 0;
|
---|
1179 | size_t consumed = 0;
|
---|
1180 | char *fullpath = NULL;
|
---|
1181 | bool res;
|
---|
1182 | uint16_t cnum;
|
---|
1183 | char *newextrapath = NULL;
|
---|
1184 | NTSTATUS status;
|
---|
1185 | const char *remote_name;
|
---|
1186 |
|
---|
1187 | if (!cli || !sharename) {
|
---|
1188 | return false;
|
---|
1189 | }
|
---|
1190 |
|
---|
1191 | remote_name = smbXcli_conn_remote_name(cli->conn);
|
---|
1192 | cnum = cli_state_get_tid(cli);
|
---|
1193 |
|
---|
1194 | /* special case. never check for a referral on the IPC$ share */
|
---|
1195 |
|
---|
1196 | if (strequal(sharename, "IPC$")) {
|
---|
1197 | return false;
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 | /* send a trans2_query_path_info to check for a referral */
|
---|
1201 |
|
---|
1202 | fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
|
---|
1203 | if (!fullpath) {
|
---|
1204 | return false;
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | /* check for the referral */
|
---|
1208 |
|
---|
1209 | if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL, 0))) {
|
---|
1210 | return false;
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | if (force_encrypt) {
|
---|
1214 | status = cli_cm_force_encryption(cli,
|
---|
1215 | username,
|
---|
1216 | password,
|
---|
1217 | domain,
|
---|
1218 | "IPC$");
|
---|
1219 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1220 | return false;
|
---|
1221 | }
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 | status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
|
---|
1225 | &num_refs, &consumed);
|
---|
1226 | res = NT_STATUS_IS_OK(status);
|
---|
1227 |
|
---|
1228 | status = cli_tdis(cli);
|
---|
1229 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1230 | return false;
|
---|
1231 | }
|
---|
1232 |
|
---|
1233 | cli_state_set_tid(cli, cnum);
|
---|
1234 |
|
---|
1235 | if (!res || !num_refs) {
|
---|
1236 | return false;
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 | if (!refs[0].dfspath) {
|
---|
1240 | return false;
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 | if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
|
---|
1244 | pp_newshare, &newextrapath)) {
|
---|
1245 | return false;
|
---|
1246 | }
|
---|
1247 |
|
---|
1248 | /* check that this is not a self-referral */
|
---|
1249 |
|
---|
1250 | if (strequal(remote_name, *pp_newserver) &&
|
---|
1251 | strequal(sharename, *pp_newshare)) {
|
---|
1252 | return false;
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | return true;
|
---|
1256 | }
|
---|