messages_ctdb: Handle async msgs for nested event contexts
[Samba.git] / source3 / libsmb / clidfs.c
blobb740007f675348f5f40b452483d06ae30f8c242d
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
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.
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.
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/>.
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"
30 /********************************************************************
31 Important point.
33 DFS paths are *always* of the form \server\share\<pathname> (the \ characters
34 are not C escaped here).
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 ********************************************************************/
41 /********************************************************************
42 Ensure a connection is encrypted.
43 ********************************************************************/
45 NTSTATUS cli_cm_force_encryption_creds(struct cli_state *c,
46 struct cli_credentials *creds,
47 const char *sharename)
49 uint16_t major, minor;
50 uint32_t caplow, caphigh;
51 NTSTATUS status;
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));
64 return status;
67 if (!SERVER_HAS_UNIX_CIFS(c)) {
68 d_printf("Encryption required and "
69 "server that doesn't support "
70 "UNIX extensions - failing connect\n");
71 return NT_STATUS_NOT_SUPPORTED;
74 status = cli_unix_extensions_version(c, &major, &minor, &caplow,
75 &caphigh);
76 if (!NT_STATUS_IS_OK(status)) {
77 d_printf("Encryption required and "
78 "can't get UNIX CIFS extensions "
79 "version from server.\n");
80 return NT_STATUS_UNKNOWN_REVISION;
83 if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
84 d_printf("Encryption required and "
85 "share %s doesn't support "
86 "encryption.\n", sharename);
87 return NT_STATUS_UNSUPPORTED_COMPRESSION;
90 status = cli_smb1_setup_encryption(c, creds);
91 if (!NT_STATUS_IS_OK(status)) {
92 d_printf("Encryption required and "
93 "setup failed with error %s.\n",
94 nt_errstr(status));
95 return status;
98 return NT_STATUS_OK;
101 NTSTATUS cli_cm_force_encryption(struct cli_state *c,
102 const char *username,
103 const char *password,
104 const char *domain,
105 const char *sharename)
107 struct cli_credentials *creds = NULL;
108 NTSTATUS status;
110 creds = cli_session_creds_init(c,
111 username,
112 domain,
113 NULL, /* default realm */
114 password,
115 c->use_kerberos,
116 c->fallback_after_kerberos,
117 c->use_ccache,
118 c->pw_nt_hash);
119 if (creds == NULL) {
120 return NT_STATUS_NO_MEMORY;
123 status = cli_cm_force_encryption_creds(c, creds, sharename);
124 /* gensec currently references the creds so we can't free them here */
125 talloc_unlink(c, creds);
126 return status;
129 /********************************************************************
130 Return a connection to a server.
131 ********************************************************************/
133 static NTSTATUS do_connect(TALLOC_CTX *ctx,
134 const char *server,
135 const char *share,
136 const struct user_auth_info *auth_info,
137 bool force_encrypt,
138 int max_protocol,
139 int port,
140 int name_type,
141 struct cli_state **pcli)
143 struct cli_state *c = NULL;
144 char *servicename;
145 char *sharename;
146 char *newserver, *newshare;
147 const char *username;
148 const char *password;
149 const char *domain;
150 NTSTATUS status;
151 int flags = 0;
152 int signing_state = get_cmdline_auth_info_signing_state(auth_info);
153 struct cli_credentials *creds = NULL;
155 if (force_encrypt) {
156 signing_state = SMB_SIGNING_REQUIRED;
159 /* make a copy so we don't modify the global string 'service' */
160 servicename = talloc_strdup(ctx,share);
161 if (!servicename) {
162 return NT_STATUS_NO_MEMORY;
164 sharename = servicename;
165 if (*sharename == '\\') {
166 sharename += 2;
167 if (server == NULL) {
168 server = sharename;
170 sharename = strchr_m(sharename,'\\');
171 if (!sharename) {
172 return NT_STATUS_NO_MEMORY;
174 *sharename = 0;
175 sharename++;
177 if (server == NULL) {
178 return NT_STATUS_INVALID_PARAMETER;
181 if (get_cmdline_auth_info_use_kerberos(auth_info)) {
182 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
184 if (get_cmdline_auth_info_fallback_after_kerberos(auth_info)) {
185 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
187 if (get_cmdline_auth_info_use_ccache(auth_info)) {
188 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
190 if (get_cmdline_auth_info_use_pw_nt_hash(auth_info)) {
191 flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
194 status = cli_connect_nb(
195 server, NULL, port, name_type, NULL,
196 signing_state,
197 flags, &c);
199 if (!NT_STATUS_IS_OK(status)) {
200 d_printf("Connection to %s failed (Error %s)\n",
201 server,
202 nt_errstr(status));
203 return status;
206 if (max_protocol == 0) {
207 max_protocol = PROTOCOL_NT1;
209 DEBUG(4,(" session request ok\n"));
211 status = smbXcli_negprot(c->conn, c->timeout,
212 lp_client_min_protocol(),
213 max_protocol);
215 if (!NT_STATUS_IS_OK(status)) {
216 d_printf("protocol negotiation failed: %s\n",
217 nt_errstr(status));
218 cli_shutdown(c);
219 return status;
222 if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
223 /* Ensure we ask for some initial credits. */
224 smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
227 username = get_cmdline_auth_info_username(auth_info);
228 password = get_cmdline_auth_info_password(auth_info);
229 domain = get_cmdline_auth_info_domain(auth_info);
230 if ((domain == NULL) || (domain[0] == '\0')) {
231 domain = lp_workgroup();
234 creds = get_cmdline_auth_info_creds(auth_info);
236 status = cli_session_setup_creds(c, creds);
237 if (!NT_STATUS_IS_OK(status)) {
238 /* If a password was not supplied then
239 * try again with a null username. */
240 if (password[0] || !username[0] ||
241 get_cmdline_auth_info_use_kerberos(auth_info) ||
242 !NT_STATUS_IS_OK(status = cli_session_setup_anon(c)))
244 d_printf("session setup failed: %s\n",
245 nt_errstr(status));
246 if (NT_STATUS_EQUAL(status,
247 NT_STATUS_MORE_PROCESSING_REQUIRED))
248 d_printf("did you forget to run kinit?\n");
249 cli_shutdown(c);
250 return status;
252 d_printf("Anonymous login successful\n");
255 if (!NT_STATUS_IS_OK(status)) {
256 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
257 cli_shutdown(c);
258 return status;
261 DEBUG(4,(" session setup ok\n"));
263 /* here's the fun part....to support 'msdfs proxy' shares
264 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
265 here before trying to connect to the original share.
266 cli_check_msdfs_proxy() will fail if it is a normal share. */
268 if (smbXcli_conn_dfs_supported(c->conn) &&
269 cli_check_msdfs_proxy(ctx, c, sharename,
270 &newserver, &newshare,
271 force_encrypt, creds)) {
272 cli_shutdown(c);
273 return do_connect(ctx, newserver,
274 newshare, auth_info,
275 force_encrypt, max_protocol,
276 port, name_type, pcli);
279 /* must be a normal share */
281 status = cli_tree_connect_creds(c, sharename, "?????", creds);
282 if (!NT_STATUS_IS_OK(status)) {
283 d_printf("tree connect failed: %s\n", nt_errstr(status));
284 cli_shutdown(c);
285 return status;
288 if (force_encrypt) {
289 status = cli_cm_force_encryption_creds(c,
290 creds,
291 sharename);
292 if (!NT_STATUS_IS_OK(status)) {
293 cli_shutdown(c);
294 return status;
298 DEBUG(4,(" tconx ok\n"));
299 *pcli = c;
300 return NT_STATUS_OK;
303 /****************************************************************************
304 ****************************************************************************/
306 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
308 TALLOC_CTX *frame = talloc_stackframe();
309 char *name = clean_name(frame, mnt);
310 if (!name) {
311 TALLOC_FREE(frame);
312 return;
314 TALLOC_FREE(cli->dfs_mountpoint);
315 cli->dfs_mountpoint = talloc_strdup(cli, name);
316 TALLOC_FREE(frame);
319 /********************************************************************
320 Add a new connection to the list.
321 referring_cli == NULL means a new initial connection.
322 ********************************************************************/
324 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
325 struct cli_state *referring_cli,
326 const char *server,
327 const char *share,
328 const struct user_auth_info *auth_info,
329 bool force_encrypt,
330 int max_protocol,
331 int port,
332 int name_type,
333 struct cli_state **pcli)
335 struct cli_state *cli;
336 NTSTATUS status;
338 status = do_connect(ctx, server, share,
339 auth_info,
340 force_encrypt, max_protocol,
341 port, name_type, &cli);
343 if (!NT_STATUS_IS_OK(status)) {
344 return status;
347 /* Enter into the list. */
348 if (referring_cli) {
349 DLIST_ADD_END(referring_cli, cli);
352 if (referring_cli && referring_cli->requested_posix_capabilities) {
353 uint16_t major, minor;
354 uint32_t caplow, caphigh;
355 status = cli_unix_extensions_version(cli, &major, &minor,
356 &caplow, &caphigh);
357 if (NT_STATUS_IS_OK(status)) {
358 cli_set_unix_extensions_capabilities(cli,
359 major, minor,
360 caplow, caphigh);
364 *pcli = cli;
365 return NT_STATUS_OK;
368 /********************************************************************
369 Return a connection to a server on a particular share.
370 ********************************************************************/
372 static struct cli_state *cli_cm_find(struct cli_state *cli,
373 const char *server,
374 const char *share)
376 struct cli_state *p;
378 if (cli == NULL) {
379 return NULL;
382 /* Search to the start of the list. */
383 for (p = cli; p; p = DLIST_PREV(p)) {
384 const char *remote_name =
385 smbXcli_conn_remote_name(p->conn);
387 if (strequal(server, remote_name) &&
388 strequal(share,p->share)) {
389 return p;
393 /* Search to the end of the list. */
394 for (p = cli->next; p; p = p->next) {
395 const char *remote_name =
396 smbXcli_conn_remote_name(p->conn);
398 if (strequal(server, remote_name) &&
399 strequal(share,p->share)) {
400 return p;
404 return NULL;
407 /****************************************************************************
408 Open a client connection to a \\server\share.
409 ****************************************************************************/
411 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
412 struct cli_state *referring_cli,
413 const char *server,
414 const char *share,
415 const struct user_auth_info *auth_info,
416 bool force_encrypt,
417 int max_protocol,
418 int port,
419 int name_type,
420 struct cli_state **pcli)
422 /* Try to reuse an existing connection in this list. */
423 struct cli_state *c = cli_cm_find(referring_cli, server, share);
424 NTSTATUS status;
426 if (c) {
427 *pcli = c;
428 return NT_STATUS_OK;
431 if (auth_info == NULL) {
432 /* Can't do a new connection
433 * without auth info. */
434 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
435 "without auth info\n",
436 server, share );
437 return NT_STATUS_INVALID_PARAMETER;
440 status = cli_cm_connect(ctx,
441 referring_cli,
442 server,
443 share,
444 auth_info,
445 force_encrypt,
446 max_protocol,
447 port,
448 name_type,
449 &c);
450 if (!NT_STATUS_IS_OK(status)) {
451 return status;
453 *pcli = c;
454 return NT_STATUS_OK;
457 /****************************************************************************
458 ****************************************************************************/
460 void cli_cm_display(struct cli_state *cli)
462 int i;
464 for (i=0; cli; cli = cli->next,i++ ) {
465 d_printf("%d:\tserver=%s, share=%s\n",
466 i, smbXcli_conn_remote_name(cli->conn), cli->share);
470 /****************************************************************************
471 ****************************************************************************/
473 /****************************************************************************
474 ****************************************************************************/
476 #if 0
477 void cli_cm_set_credentials(struct user_auth_info *auth_info)
479 SAFE_FREE(cm_creds.username);
480 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
481 auth_info));
483 if (get_cmdline_auth_info_got_pass(auth_info)) {
484 cm_set_password(get_cmdline_auth_info_password(auth_info));
487 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
488 cm_creds.fallback_after_kerberos = false;
489 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
491 #endif
493 /**********************************************************************
494 split a dfs path into the server, share name, and extrapath components
495 **********************************************************************/
497 static bool split_dfs_path(TALLOC_CTX *ctx,
498 const char *nodepath,
499 char **pp_server,
500 char **pp_share,
501 char **pp_extrapath)
503 char *p, *q;
504 char *path;
506 *pp_server = NULL;
507 *pp_share = NULL;
508 *pp_extrapath = NULL;
510 path = talloc_strdup(ctx, nodepath);
511 if (!path) {
512 goto fail;
515 if ( path[0] != '\\' ) {
516 goto fail;
519 p = strchr_m( path + 1, '\\' );
520 if ( !p ) {
521 goto fail;
524 *p = '\0';
525 p++;
527 /* Look for any extra/deep path */
528 q = strchr_m(p, '\\');
529 if (q != NULL) {
530 *q = '\0';
531 q++;
532 *pp_extrapath = talloc_strdup(ctx, q);
533 } else {
534 *pp_extrapath = talloc_strdup(ctx, "");
536 if (*pp_extrapath == NULL) {
537 goto fail;
540 *pp_share = talloc_strdup(ctx, p);
541 if (*pp_share == NULL) {
542 goto fail;
545 *pp_server = talloc_strdup(ctx, &path[1]);
546 if (*pp_server == NULL) {
547 goto fail;
550 TALLOC_FREE(path);
551 return true;
553 fail:
554 TALLOC_FREE(*pp_share);
555 TALLOC_FREE(*pp_extrapath);
556 TALLOC_FREE(path);
557 return false;
560 /****************************************************************************
561 Return the original path truncated at the directory component before
562 the first wildcard character. Trust the caller to provide a NULL
563 terminated string
564 ****************************************************************************/
566 static char *clean_path(TALLOC_CTX *ctx, const char *path)
568 size_t len;
569 char *p1, *p2, *p;
570 char *path_out;
572 /* No absolute paths. */
573 while (IS_DIRECTORY_SEP(*path)) {
574 path++;
577 path_out = talloc_strdup(ctx, path);
578 if (!path_out) {
579 return NULL;
582 p1 = strchr_m(path_out, '*');
583 p2 = strchr_m(path_out, '?');
585 if (p1 || p2) {
586 if (p1 && p2) {
587 p = MIN(p1,p2);
588 } else if (!p1) {
589 p = p2;
590 } else {
591 p = p1;
593 *p = '\0';
595 /* Now go back to the start of this component. */
596 p1 = strrchr_m(path_out, '/');
597 p2 = strrchr_m(path_out, '\\');
598 p = MAX(p1,p2);
599 if (p) {
600 *p = '\0';
604 /* Strip any trailing separator */
606 len = strlen(path_out);
607 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
608 path_out[len-1] = '\0';
611 return path_out;
614 /****************************************************************************
615 ****************************************************************************/
617 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
618 struct cli_state *cli,
619 const char *dir)
621 char path_sep = '\\';
623 /* Ensure the extrapath doesn't start with a separator. */
624 while (IS_DIRECTORY_SEP(*dir)) {
625 dir++;
628 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
629 path_sep = '/';
631 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
632 path_sep,
633 smbXcli_conn_remote_name(cli->conn),
634 path_sep,
635 cli->share,
636 path_sep,
637 dir);
640 /********************************************************************
641 check for dfs referral
642 ********************************************************************/
644 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
645 NTSTATUS status)
647 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
649 if (!(smbXcli_conn_use_unicode(cli->conn))) {
650 return false;
652 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_STATUS32)) {
653 return false;
655 if (NT_STATUS_EQUAL(status, expected)) {
656 return true;
658 return false;
661 /********************************************************************
662 Get the dfs referral link.
663 ********************************************************************/
665 NTSTATUS cli_dfs_get_referral_ex(TALLOC_CTX *ctx,
666 struct cli_state *cli,
667 const char *path,
668 uint16_t max_referral_level,
669 struct client_dfs_referral **refs,
670 size_t *num_refs,
671 size_t *consumed)
673 unsigned int param_len = 0;
674 uint16_t recv_flags2;
675 uint8_t *param = NULL;
676 uint8_t *rdata = NULL;
677 char *p;
678 char *endp;
679 smb_ucs2_t *path_ucs;
680 char *consumed_path = NULL;
681 uint16_t consumed_ucs;
682 uint16_t num_referrals;
683 struct client_dfs_referral *referrals = NULL;
684 NTSTATUS status;
685 TALLOC_CTX *frame = talloc_stackframe();
687 *num_refs = 0;
688 *refs = NULL;
690 param = talloc_array(talloc_tos(), uint8_t, 2);
691 if (!param) {
692 status = NT_STATUS_NO_MEMORY;
693 goto out;
695 SSVAL(param, 0, max_referral_level);
697 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
698 path, strlen(path)+1,
699 NULL);
700 if (!param) {
701 status = NT_STATUS_NO_MEMORY;
702 goto out;
704 param_len = talloc_get_size(param);
705 path_ucs = (smb_ucs2_t *)&param[2];
707 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
708 DATA_BLOB in_input_buffer;
709 DATA_BLOB in_output_buffer = data_blob_null;
710 DATA_BLOB out_input_buffer = data_blob_null;
711 DATA_BLOB out_output_buffer = data_blob_null;
713 in_input_buffer.data = param;
714 in_input_buffer.length = param_len;
716 status = smb2cli_ioctl(cli->conn,
717 cli->timeout,
718 cli->smb2.session,
719 cli->smb2.tcon,
720 UINT64_MAX, /* in_fid_persistent */
721 UINT64_MAX, /* in_fid_volatile */
722 FSCTL_DFS_GET_REFERRALS,
723 0, /* in_max_input_length */
724 &in_input_buffer,
725 CLI_BUFFER_SIZE, /* in_max_output_length */
726 &in_output_buffer,
727 SMB2_IOCTL_FLAG_IS_FSCTL,
728 talloc_tos(),
729 &out_input_buffer,
730 &out_output_buffer);
731 if (!NT_STATUS_IS_OK(status)) {
732 goto out;
735 if (out_output_buffer.length < 4) {
736 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
737 goto out;
740 recv_flags2 = FLAGS2_UNICODE_STRINGS;
741 rdata = out_output_buffer.data;
742 endp = (char *)rdata + out_output_buffer.length;
743 } else {
744 unsigned int data_len = 0;
745 uint16_t setup[1];
747 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
749 status = cli_trans(talloc_tos(), cli, SMBtrans2,
750 NULL, 0xffff, 0, 0,
751 setup, 1, 0,
752 param, param_len, 2,
753 NULL, 0, CLI_BUFFER_SIZE,
754 &recv_flags2,
755 NULL, 0, NULL, /* rsetup */
756 NULL, 0, NULL,
757 &rdata, 4, &data_len);
758 if (!NT_STATUS_IS_OK(status)) {
759 goto out;
762 endp = (char *)rdata + data_len;
765 consumed_ucs = SVAL(rdata, 0);
766 num_referrals = SVAL(rdata, 2);
768 /* consumed_ucs is the number of bytes
769 * of the UCS2 path consumed not counting any
770 * terminating null. We need to convert
771 * back to unix charset and count again
772 * to get the number of bytes consumed from
773 * the incoming path. */
775 errno = 0;
776 if (pull_string_talloc(talloc_tos(),
777 NULL,
779 &consumed_path,
780 path_ucs,
781 consumed_ucs,
782 STR_UNICODE) == 0) {
783 if (errno != 0) {
784 status = map_nt_error_from_unix(errno);
785 } else {
786 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
788 goto out;
790 if (consumed_path == NULL) {
791 status = map_nt_error_from_unix(errno);
792 goto out;
794 *consumed = strlen(consumed_path);
796 if (num_referrals != 0) {
797 uint16_t ref_version;
798 uint16_t ref_size;
799 int i;
800 uint16_t node_offset;
802 referrals = talloc_array(ctx, struct client_dfs_referral,
803 num_referrals);
805 if (!referrals) {
806 status = NT_STATUS_NO_MEMORY;
807 goto out;
809 /* start at the referrals array */
811 p = (char *)rdata+8;
812 for (i=0; i<num_referrals && p < endp; i++) {
813 if (p + 18 > endp) {
814 goto out;
816 ref_version = SVAL(p, 0);
817 ref_size = SVAL(p, 2);
818 node_offset = SVAL(p, 16);
820 if (ref_version != 3) {
821 p += ref_size;
822 continue;
825 referrals[i].proximity = SVAL(p, 8);
826 referrals[i].ttl = SVAL(p, 10);
828 if (p + node_offset > endp) {
829 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
830 goto out;
832 clistr_pull_talloc(referrals,
833 (const char *)rdata,
834 recv_flags2,
835 &referrals[i].dfspath,
836 p+node_offset,
837 PTR_DIFF(endp, p+node_offset),
838 STR_TERMINATE|STR_UNICODE);
840 if (!referrals[i].dfspath) {
841 status = map_nt_error_from_unix(errno);
842 goto out;
844 p += ref_size;
846 if (i < num_referrals) {
847 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
848 goto out;
852 *num_refs = num_referrals;
853 *refs = referrals;
855 out:
857 TALLOC_FREE(frame);
858 return status;
861 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
862 struct cli_state *cli,
863 const char *path,
864 struct client_dfs_referral **refs,
865 size_t *num_refs,
866 size_t *consumed)
868 return cli_dfs_get_referral_ex(ctx,
869 cli,
870 path,
872 refs, /* Max referral level we want */
873 num_refs,
874 consumed);
877 /********************************************************************
878 ********************************************************************/
879 struct cli_dfs_path_split {
880 char *server;
881 char *share;
882 char *extrapath;
885 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
886 const char *mountpt,
887 const struct user_auth_info *dfs_auth_info,
888 struct cli_state *rootcli,
889 const char *path,
890 struct cli_state **targetcli,
891 char **pp_targetpath)
893 struct client_dfs_referral *refs = NULL;
894 size_t num_refs = 0;
895 size_t consumed = 0;
896 struct cli_state *cli_ipc = NULL;
897 char *dfs_path = NULL;
898 char *cleanpath = NULL;
899 char *extrapath = NULL;
900 int pathlen;
901 struct cli_state *newcli = NULL;
902 struct cli_state *ccli = NULL;
903 int count = 0;
904 char *newpath = NULL;
905 char *newmount = NULL;
906 char *ppath = NULL;
907 SMB_STRUCT_STAT sbuf;
908 uint32_t attributes;
909 NTSTATUS status;
910 struct smbXcli_tcon *root_tcon = NULL;
911 struct smbXcli_tcon *target_tcon = NULL;
912 struct cli_dfs_path_split *dfs_refs = NULL;
914 if ( !rootcli || !path || !targetcli ) {
915 return NT_STATUS_INVALID_PARAMETER;
918 /* Don't do anything if this is not a DFS root. */
920 if (smbXcli_conn_protocol(rootcli->conn) >= PROTOCOL_SMB2_02) {
921 root_tcon = rootcli->smb2.tcon;
922 } else {
923 root_tcon = rootcli->smb1.tcon;
927 * Avoid more than one leading directory separator
929 while (IS_DIRECTORY_SEP(path[0]) && IS_DIRECTORY_SEP(path[1])) {
930 path++;
933 if (!smbXcli_tcon_is_dfs_share(root_tcon)) {
934 *targetcli = rootcli;
935 *pp_targetpath = talloc_strdup(ctx, path);
936 if (!*pp_targetpath) {
937 return NT_STATUS_NO_MEMORY;
939 return NT_STATUS_OK;
942 *targetcli = NULL;
944 /* Send a trans2_query_path_info to check for a referral. */
946 cleanpath = clean_path(ctx, path);
947 if (!cleanpath) {
948 return NT_STATUS_NO_MEMORY;
951 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
952 if (!dfs_path) {
953 return NT_STATUS_NO_MEMORY;
956 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
957 if (NT_STATUS_IS_OK(status)) {
958 /* This is an ordinary path, just return it. */
959 *targetcli = rootcli;
960 *pp_targetpath = talloc_strdup(ctx, path);
961 if (!*pp_targetpath) {
962 return NT_STATUS_NO_MEMORY;
964 goto done;
967 /* Special case where client asked for a path that does not exist */
969 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
970 status)) {
971 *targetcli = rootcli;
972 *pp_targetpath = talloc_strdup(ctx, path);
973 if (!*pp_targetpath) {
974 return NT_STATUS_NO_MEMORY;
976 goto done;
979 /* We got an error, check for DFS referral. */
981 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
982 status)) {
983 return status;
986 /* Check for the referral. */
988 status = cli_cm_open(ctx,
989 rootcli,
990 smbXcli_conn_remote_name(rootcli->conn),
991 "IPC$",
992 dfs_auth_info,
993 smb1cli_conn_encryption_on(rootcli->conn),
994 smbXcli_conn_protocol(rootcli->conn),
996 0x20,
997 &cli_ipc);
998 if (!NT_STATUS_IS_OK(status)) {
999 return status;
1002 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
1003 &num_refs, &consumed);
1004 if (!NT_STATUS_IS_OK(status)) {
1005 return status;
1008 if (!num_refs || !refs[0].dfspath) {
1009 return NT_STATUS_NOT_FOUND;
1013 * Bug#10123 - DFS referal entries can be provided in a random order,
1014 * so check the connection cache for each item to avoid unnecessary
1015 * reconnections.
1017 dfs_refs = talloc_array(ctx, struct cli_dfs_path_split, num_refs);
1018 if (dfs_refs == NULL) {
1019 return NT_STATUS_NO_MEMORY;
1022 for (count = 0; count < num_refs; count++) {
1023 if (!split_dfs_path(dfs_refs, refs[count].dfspath,
1024 &dfs_refs[count].server,
1025 &dfs_refs[count].share,
1026 &dfs_refs[count].extrapath)) {
1027 TALLOC_FREE(dfs_refs);
1028 return NT_STATUS_NOT_FOUND;
1031 ccli = cli_cm_find(rootcli, dfs_refs[count].server,
1032 dfs_refs[count].share);
1033 if (ccli != NULL) {
1034 extrapath = dfs_refs[count].extrapath;
1035 *targetcli = ccli;
1036 break;
1041 * If no cached connection was found, then connect to the first live
1042 * referral server in the list.
1044 for (count = 0; (ccli == NULL) && (count < num_refs); count++) {
1045 /* Connect to the target server & share */
1046 status = cli_cm_connect(ctx, rootcli,
1047 dfs_refs[count].server,
1048 dfs_refs[count].share,
1049 dfs_auth_info,
1050 smb1cli_conn_encryption_on(rootcli->conn),
1051 smbXcli_conn_protocol(rootcli->conn),
1053 0x20,
1054 targetcli);
1055 if (!NT_STATUS_IS_OK(status)) {
1056 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
1057 dfs_refs[count].server,
1058 dfs_refs[count].share);
1059 continue;
1060 } else {
1061 extrapath = dfs_refs[count].extrapath;
1062 break;
1066 /* No available referral server for the connection */
1067 if (*targetcli == NULL) {
1068 TALLOC_FREE(dfs_refs);
1069 return status;
1072 /* Make sure to recreate the original string including any wildcards. */
1074 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
1075 if (!dfs_path) {
1076 TALLOC_FREE(dfs_refs);
1077 return NT_STATUS_NO_MEMORY;
1079 pathlen = strlen(dfs_path);
1080 consumed = MIN(pathlen, consumed);
1081 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
1082 if (!*pp_targetpath) {
1083 TALLOC_FREE(dfs_refs);
1084 return NT_STATUS_NO_MEMORY;
1086 dfs_path[consumed] = '\0';
1089 * *pp_targetpath is now the unconsumed part of the path.
1090 * dfs_path is now the consumed part of the path
1091 * (in \server\share\path format).
1094 if (extrapath && strlen(extrapath) > 0) {
1095 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
1096 /* put the trailing \ on the path, so to be save we put one in if needed */
1097 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
1098 *pp_targetpath = talloc_asprintf(ctx,
1099 "%s\\%s",
1100 extrapath,
1101 *pp_targetpath);
1102 } else {
1103 *pp_targetpath = talloc_asprintf(ctx,
1104 "%s%s",
1105 extrapath,
1106 *pp_targetpath);
1108 if (!*pp_targetpath) {
1109 TALLOC_FREE(dfs_refs);
1110 return NT_STATUS_NO_MEMORY;
1114 /* parse out the consumed mount path */
1115 /* trim off the \server\share\ */
1117 ppath = dfs_path;
1119 if (*ppath != '\\') {
1120 d_printf("cli_resolve_path: "
1121 "dfs_path (%s) not in correct format.\n",
1122 dfs_path );
1123 TALLOC_FREE(dfs_refs);
1124 return NT_STATUS_NOT_FOUND;
1127 ppath++; /* Now pointing at start of server name. */
1129 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
1130 TALLOC_FREE(dfs_refs);
1131 return NT_STATUS_NOT_FOUND;
1134 ppath++; /* Now pointing at start of share name. */
1136 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
1137 TALLOC_FREE(dfs_refs);
1138 return NT_STATUS_NOT_FOUND;
1141 ppath++; /* Now pointing at path component. */
1143 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
1144 if (!newmount) {
1145 TALLOC_FREE(dfs_refs);
1146 return NT_STATUS_NOT_FOUND;
1149 cli_set_mntpoint(*targetcli, newmount);
1151 /* Check for another dfs referral, note that we are not
1152 checking for loops here. */
1154 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
1155 status = cli_resolve_path(ctx,
1156 newmount,
1157 dfs_auth_info,
1158 *targetcli,
1159 *pp_targetpath,
1160 &newcli,
1161 &newpath);
1162 if (NT_STATUS_IS_OK(status)) {
1164 * When cli_resolve_path returns true here it's always
1165 * returning the complete path in newpath, so we're done
1166 * here.
1168 *targetcli = newcli;
1169 *pp_targetpath = newpath;
1170 TALLOC_FREE(dfs_refs);
1171 return status;
1175 done:
1177 if (smbXcli_conn_protocol((*targetcli)->conn) >= PROTOCOL_SMB2_02) {
1178 target_tcon = (*targetcli)->smb2.tcon;
1179 } else {
1180 target_tcon = (*targetcli)->smb1.tcon;
1183 /* If returning true ensure we return a dfs root full path. */
1184 if (smbXcli_tcon_is_dfs_share(target_tcon)) {
1185 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1186 if (!dfs_path) {
1187 TALLOC_FREE(dfs_refs);
1188 return NT_STATUS_NO_MEMORY;
1190 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1191 if (*pp_targetpath == NULL) {
1192 TALLOC_FREE(dfs_refs);
1193 return NT_STATUS_NO_MEMORY;
1197 TALLOC_FREE(dfs_refs);
1198 return NT_STATUS_OK;
1201 /********************************************************************
1202 ********************************************************************/
1204 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1205 struct cli_state *cli,
1206 const char *sharename,
1207 char **pp_newserver,
1208 char **pp_newshare,
1209 bool force_encrypt,
1210 struct cli_credentials *creds)
1212 struct client_dfs_referral *refs = NULL;
1213 size_t num_refs = 0;
1214 size_t consumed = 0;
1215 char *fullpath = NULL;
1216 bool res;
1217 struct smbXcli_tcon *orig_tcon = NULL;
1218 char *newextrapath = NULL;
1219 NTSTATUS status;
1220 const char *remote_name;
1222 if (!cli || !sharename) {
1223 return false;
1226 remote_name = smbXcli_conn_remote_name(cli->conn);
1228 /* special case. never check for a referral on the IPC$ share */
1230 if (strequal(sharename, "IPC$")) {
1231 return false;
1234 /* send a trans2_query_path_info to check for a referral */
1236 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1237 if (!fullpath) {
1238 return false;
1241 /* Store tcon state. */
1242 if (cli_state_has_tcon(cli)) {
1243 orig_tcon = cli_state_save_tcon(cli);
1244 if (orig_tcon == NULL) {
1245 return false;
1249 /* check for the referral */
1251 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL))) {
1252 cli_state_restore_tcon(cli, orig_tcon);
1253 return false;
1256 if (force_encrypt) {
1257 status = cli_cm_force_encryption_creds(cli, creds, "IPC$");
1258 if (!NT_STATUS_IS_OK(status)) {
1259 cli_state_restore_tcon(cli, orig_tcon);
1260 return false;
1264 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1265 &num_refs, &consumed);
1266 res = NT_STATUS_IS_OK(status);
1268 status = cli_tdis(cli);
1270 cli_state_restore_tcon(cli, orig_tcon);
1272 if (!NT_STATUS_IS_OK(status)) {
1273 return false;
1276 if (!res || !num_refs) {
1277 return false;
1280 if (!refs[0].dfspath) {
1281 return false;
1284 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1285 pp_newshare, &newextrapath)) {
1286 return false;
1289 /* check that this is not a self-referral */
1291 if (strequal(remote_name, *pp_newserver) &&
1292 strequal(sharename, *pp_newshare)) {
1293 return false;
1296 return true;