talloc: Add a warning to talloc_reference() documentation.
[Samba.git] / source3 / libsmb / clidfs.c
blob729f4fe0edf79fcff7d8e5946f466b677bc25a59
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(struct cli_state *c,
46 const char *username,
47 const char *password,
48 const char *domain,
49 const char *sharename)
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 status = cli_force_encryption(c,
68 username,
69 password,
70 domain);
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));
90 return status;
93 /********************************************************************
94 Return a connection to a server.
95 ********************************************************************/
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)
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;
118 /* make a copy so we don't modify the global string 'service' */
119 servicename = talloc_strdup(ctx,share);
120 if (!servicename) {
121 return NT_STATUS_NO_MEMORY;
123 sharename = servicename;
124 if (*sharename == '\\') {
125 sharename += 2;
126 if (server == NULL) {
127 server = sharename;
129 sharename = strchr_m(sharename,'\\');
130 if (!sharename) {
131 return NT_STATUS_NO_MEMORY;
133 *sharename = 0;
134 sharename++;
136 if (server == NULL) {
137 return NT_STATUS_INVALID_PARAMETER;
140 if (get_cmdline_auth_info_use_kerberos(auth_info)) {
141 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
143 if (get_cmdline_auth_info_fallback_after_kerberos(auth_info)) {
144 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
146 if (get_cmdline_auth_info_use_ccache(auth_info)) {
147 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
149 if (get_cmdline_auth_info_use_pw_nt_hash(auth_info)) {
150 flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
153 status = cli_connect_nb(
154 server, NULL, port, name_type, NULL,
155 get_cmdline_auth_info_signing_state(auth_info),
156 flags, &c);
158 if (!NT_STATUS_IS_OK(status)) {
159 d_printf("Connection to %s failed (Error %s)\n",
160 server,
161 nt_errstr(status));
162 return status;
165 if (max_protocol == 0) {
166 max_protocol = PROTOCOL_NT1;
168 DEBUG(4,(" session request ok\n"));
170 status = smbXcli_negprot(c->conn, c->timeout,
171 lp_cli_minprotocol(),
172 max_protocol);
174 if (!NT_STATUS_IS_OK(status)) {
175 d_printf("protocol negotiation failed: %s\n",
176 nt_errstr(status));
177 cli_shutdown(c);
178 return status;
181 if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
182 /* Ensure we ask for some initial credits. */
183 smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
186 username = get_cmdline_auth_info_username(auth_info);
187 password = get_cmdline_auth_info_password(auth_info);
188 domain = get_cmdline_auth_info_domain(auth_info);
189 if ((domain == NULL) || (domain[0] == '\0')) {
190 domain = lp_workgroup();
193 status = cli_session_setup(c, username,
194 password, strlen(password),
195 password, strlen(password),
196 domain);
197 if (!NT_STATUS_IS_OK(status)) {
198 /* If a password was not supplied then
199 * try again with a null username. */
200 if (password[0] || !username[0] ||
201 get_cmdline_auth_info_use_kerberos(auth_info) ||
202 !NT_STATUS_IS_OK(status = cli_session_setup(c, "",
203 "", 0,
204 "", 0,
205 lp_workgroup()))) {
206 d_printf("session setup failed: %s\n",
207 nt_errstr(status));
208 if (NT_STATUS_EQUAL(status,
209 NT_STATUS_MORE_PROCESSING_REQUIRED))
210 d_printf("did you forget to run kinit?\n");
211 cli_shutdown(c);
212 return status;
214 d_printf("Anonymous login successful\n");
215 status = cli_init_creds(c, "", lp_workgroup(), "");
216 } else {
217 status = cli_init_creds(c, username, domain, password);
220 if (!NT_STATUS_IS_OK(status)) {
221 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
222 cli_shutdown(c);
223 return status;
226 if ( show_sessetup ) {
227 if (*c->server_domain) {
228 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
229 c->server_domain,c->server_os,c->server_type));
230 } else if (*c->server_os || *c->server_type) {
231 DEBUG(0,("OS=[%s] Server=[%s]\n",
232 c->server_os,c->server_type));
235 DEBUG(4,(" session setup ok\n"));
237 /* here's the fun part....to support 'msdfs proxy' shares
238 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
239 here before trying to connect to the original share.
240 cli_check_msdfs_proxy() will fail if it is a normal share. */
242 if (smbXcli_conn_dfs_supported(c->conn) &&
243 cli_check_msdfs_proxy(ctx, c, sharename,
244 &newserver, &newshare,
245 force_encrypt,
246 username,
247 password,
248 domain)) {
249 cli_shutdown(c);
250 return do_connect(ctx, newserver,
251 newshare, auth_info, false,
252 force_encrypt, max_protocol,
253 port, name_type, pcli);
256 /* must be a normal share */
258 status = cli_tree_connect(c, sharename, "?????",
259 password, strlen(password)+1);
260 if (!NT_STATUS_IS_OK(status)) {
261 d_printf("tree connect failed: %s\n", nt_errstr(status));
262 cli_shutdown(c);
263 return status;
266 if (force_encrypt) {
267 status = cli_cm_force_encryption(c,
268 username,
269 password,
270 domain,
271 sharename);
272 if (!NT_STATUS_IS_OK(status)) {
273 cli_shutdown(c);
274 return status;
278 DEBUG(4,(" tconx ok\n"));
279 *pcli = c;
280 return NT_STATUS_OK;
283 /****************************************************************************
284 ****************************************************************************/
286 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
288 TALLOC_CTX *frame = talloc_stackframe();
289 char *name = clean_name(frame, mnt);
290 if (!name) {
291 TALLOC_FREE(frame);
292 return;
294 TALLOC_FREE(cli->dfs_mountpoint);
295 cli->dfs_mountpoint = talloc_strdup(cli, name);
296 TALLOC_FREE(frame);
299 /********************************************************************
300 Add a new connection to the list.
301 referring_cli == NULL means a new initial connection.
302 ********************************************************************/
304 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
305 struct cli_state *referring_cli,
306 const char *server,
307 const char *share,
308 const struct user_auth_info *auth_info,
309 bool show_hdr,
310 bool force_encrypt,
311 int max_protocol,
312 int port,
313 int name_type,
314 struct cli_state **pcli)
316 struct cli_state *cli;
317 NTSTATUS status;
319 status = do_connect(ctx, server, share,
320 auth_info,
321 show_hdr, force_encrypt, max_protocol,
322 port, name_type, &cli);
324 if (!NT_STATUS_IS_OK(status)) {
325 return status;
328 /* Enter into the list. */
329 if (referring_cli) {
330 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
333 if (referring_cli && referring_cli->requested_posix_capabilities) {
334 uint16 major, minor;
335 uint32 caplow, caphigh;
336 status = cli_unix_extensions_version(cli, &major, &minor,
337 &caplow, &caphigh);
338 if (NT_STATUS_IS_OK(status)) {
339 cli_set_unix_extensions_capabilities(cli,
340 major, minor,
341 caplow, caphigh);
345 *pcli = cli;
346 return NT_STATUS_OK;
349 /********************************************************************
350 Return a connection to a server on a particular share.
351 ********************************************************************/
353 static struct cli_state *cli_cm_find(struct cli_state *cli,
354 const char *server,
355 const char *share)
357 struct cli_state *p;
359 if (cli == NULL) {
360 return NULL;
363 /* Search to the start of the list. */
364 for (p = cli; p; p = DLIST_PREV(p)) {
365 const char *remote_name =
366 smbXcli_conn_remote_name(p->conn);
368 if (strequal(server, remote_name) &&
369 strequal(share,p->share)) {
370 return p;
374 /* Search to the end of the list. */
375 for (p = cli->next; p; p = p->next) {
376 const char *remote_name =
377 smbXcli_conn_remote_name(p->conn);
379 if (strequal(server, remote_name) &&
380 strequal(share,p->share)) {
381 return p;
385 return NULL;
388 /****************************************************************************
389 Open a client connection to a \\server\share.
390 ****************************************************************************/
392 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
393 struct cli_state *referring_cli,
394 const char *server,
395 const char *share,
396 const struct user_auth_info *auth_info,
397 bool show_hdr,
398 bool force_encrypt,
399 int max_protocol,
400 int port,
401 int name_type,
402 struct cli_state **pcli)
404 /* Try to reuse an existing connection in this list. */
405 struct cli_state *c = cli_cm_find(referring_cli, server, share);
406 NTSTATUS status;
408 if (c) {
409 *pcli = c;
410 return NT_STATUS_OK;
413 if (auth_info == NULL) {
414 /* Can't do a new connection
415 * without auth info. */
416 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
417 "without auth info\n",
418 server, share );
419 return NT_STATUS_INVALID_PARAMETER;
422 status = cli_cm_connect(ctx,
423 referring_cli,
424 server,
425 share,
426 auth_info,
427 show_hdr,
428 force_encrypt,
429 max_protocol,
430 port,
431 name_type,
432 &c);
433 if (!NT_STATUS_IS_OK(status)) {
434 return status;
436 *pcli = c;
437 return NT_STATUS_OK;
440 /****************************************************************************
441 ****************************************************************************/
443 void cli_cm_display(struct cli_state *cli)
445 int i;
447 for (i=0; cli; cli = cli->next,i++ ) {
448 d_printf("%d:\tserver=%s, share=%s\n",
449 i, smbXcli_conn_remote_name(cli->conn), cli->share);
453 /****************************************************************************
454 ****************************************************************************/
456 /****************************************************************************
457 ****************************************************************************/
459 #if 0
460 void cli_cm_set_credentials(struct user_auth_info *auth_info)
462 SAFE_FREE(cm_creds.username);
463 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
464 auth_info));
466 if (get_cmdline_auth_info_got_pass(auth_info)) {
467 cm_set_password(get_cmdline_auth_info_password(auth_info));
470 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
471 cm_creds.fallback_after_kerberos = false;
472 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
474 #endif
476 /**********************************************************************
477 split a dfs path into the server, share name, and extrapath components
478 **********************************************************************/
480 static bool split_dfs_path(TALLOC_CTX *ctx,
481 const char *nodepath,
482 char **pp_server,
483 char **pp_share,
484 char **pp_extrapath)
486 char *p, *q;
487 char *path;
489 *pp_server = NULL;
490 *pp_share = NULL;
491 *pp_extrapath = NULL;
493 path = talloc_strdup(ctx, nodepath);
494 if (!path) {
495 goto fail;
498 if ( path[0] != '\\' ) {
499 goto fail;
502 p = strchr_m( path + 1, '\\' );
503 if ( !p ) {
504 goto fail;
507 *p = '\0';
508 p++;
510 /* Look for any extra/deep path */
511 q = strchr_m(p, '\\');
512 if (q != NULL) {
513 *q = '\0';
514 q++;
515 *pp_extrapath = talloc_strdup(ctx, q);
516 } else {
517 *pp_extrapath = talloc_strdup(ctx, "");
519 if (*pp_extrapath == NULL) {
520 goto fail;
523 *pp_share = talloc_strdup(ctx, p);
524 if (*pp_share == NULL) {
525 goto fail;
528 *pp_server = talloc_strdup(ctx, &path[1]);
529 if (*pp_server == NULL) {
530 goto fail;
533 TALLOC_FREE(path);
534 return true;
536 fail:
537 TALLOC_FREE(*pp_share);
538 TALLOC_FREE(*pp_extrapath);
539 TALLOC_FREE(path);
540 return false;
543 /****************************************************************************
544 Return the original path truncated at the directory component before
545 the first wildcard character. Trust the caller to provide a NULL
546 terminated string
547 ****************************************************************************/
549 static char *clean_path(TALLOC_CTX *ctx, const char *path)
551 size_t len;
552 char *p1, *p2, *p;
553 char *path_out;
555 /* No absolute paths. */
556 while (IS_DIRECTORY_SEP(*path)) {
557 path++;
560 path_out = talloc_strdup(ctx, path);
561 if (!path_out) {
562 return NULL;
565 p1 = strchr_m(path_out, '*');
566 p2 = strchr_m(path_out, '?');
568 if (p1 || p2) {
569 if (p1 && p2) {
570 p = MIN(p1,p2);
571 } else if (!p1) {
572 p = p2;
573 } else {
574 p = p1;
576 *p = '\0';
578 /* Now go back to the start of this component. */
579 p1 = strrchr_m(path_out, '/');
580 p2 = strrchr_m(path_out, '\\');
581 p = MAX(p1,p2);
582 if (p) {
583 *p = '\0';
587 /* Strip any trailing separator */
589 len = strlen(path_out);
590 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
591 path_out[len-1] = '\0';
594 return path_out;
597 /****************************************************************************
598 ****************************************************************************/
600 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
601 struct cli_state *cli,
602 const char *dir)
604 char path_sep = '\\';
606 /* Ensure the extrapath doesn't start with a separator. */
607 while (IS_DIRECTORY_SEP(*dir)) {
608 dir++;
611 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
612 path_sep = '/';
614 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
615 path_sep,
616 smbXcli_conn_remote_name(cli->conn),
617 path_sep,
618 cli->share,
619 path_sep,
620 dir);
623 /********************************************************************
624 check for dfs referral
625 ********************************************************************/
627 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
628 NTSTATUS status)
630 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
632 if (!(smbXcli_conn_use_unicode(cli->conn))) {
633 return false;
635 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_STATUS32)) {
636 return false;
638 if (NT_STATUS_EQUAL(status, expected)) {
639 return true;
641 return false;
644 /********************************************************************
645 Get the dfs referral link.
646 ********************************************************************/
648 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
649 struct cli_state *cli,
650 const char *path,
651 struct client_dfs_referral **refs,
652 size_t *num_refs,
653 size_t *consumed)
655 unsigned int param_len = 0;
656 uint16_t recv_flags2;
657 uint8_t *param = NULL;
658 uint8_t *rdata = NULL;
659 char *p;
660 char *endp;
661 smb_ucs2_t *path_ucs;
662 char *consumed_path = NULL;
663 uint16_t consumed_ucs;
664 uint16 num_referrals;
665 struct client_dfs_referral *referrals = NULL;
666 NTSTATUS status;
667 TALLOC_CTX *frame = talloc_stackframe();
669 *num_refs = 0;
670 *refs = NULL;
672 param = talloc_array(talloc_tos(), uint8_t, 2);
673 if (!param) {
674 status = NT_STATUS_NO_MEMORY;
675 goto out;
677 SSVAL(param, 0, 0x03); /* max referral level */
679 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
680 path, strlen(path)+1,
681 NULL);
682 if (!param) {
683 status = NT_STATUS_NO_MEMORY;
684 goto out;
686 param_len = talloc_get_size(param);
687 path_ucs = (smb_ucs2_t *)&param[2];
689 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
690 DATA_BLOB in_input_buffer;
691 DATA_BLOB in_output_buffer = data_blob_null;
692 DATA_BLOB out_input_buffer = data_blob_null;
693 DATA_BLOB out_output_buffer = data_blob_null;
695 in_input_buffer.data = param;
696 in_input_buffer.length = param_len;
698 status = smb2cli_ioctl(cli->conn,
699 cli->timeout,
700 cli->smb2.session,
701 cli->smb2.tcon,
702 UINT64_MAX, /* in_fid_persistent */
703 UINT64_MAX, /* in_fid_volatile */
704 FSCTL_DFS_GET_REFERRALS,
705 0, /* in_max_input_length */
706 &in_input_buffer,
707 CLI_BUFFER_SIZE, /* in_max_output_length */
708 &in_output_buffer,
709 SMB2_IOCTL_FLAG_IS_FSCTL,
710 talloc_tos(),
711 &out_input_buffer,
712 &out_output_buffer);
713 if (!NT_STATUS_IS_OK(status)) {
714 goto out;
717 if (out_output_buffer.length < 4) {
718 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
719 goto out;
722 recv_flags2 = FLAGS2_UNICODE_STRINGS;
723 rdata = out_output_buffer.data;
724 endp = (char *)rdata + out_output_buffer.length;
725 } else {
726 unsigned int data_len = 0;
727 uint16_t setup[1];
729 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
731 status = cli_trans(talloc_tos(), cli, SMBtrans2,
732 NULL, 0xffff, 0, 0,
733 setup, 1, 0,
734 param, param_len, 2,
735 NULL, 0, CLI_BUFFER_SIZE,
736 &recv_flags2,
737 NULL, 0, NULL, /* rsetup */
738 NULL, 0, NULL,
739 &rdata, 4, &data_len);
740 if (!NT_STATUS_IS_OK(status)) {
741 goto out;
744 endp = (char *)rdata + data_len;
747 consumed_ucs = SVAL(rdata, 0);
748 num_referrals = SVAL(rdata, 2);
750 /* consumed_ucs is the number of bytes
751 * of the UCS2 path consumed not counting any
752 * terminating null. We need to convert
753 * back to unix charset and count again
754 * to get the number of bytes consumed from
755 * the incoming path. */
757 errno = 0;
758 if (pull_string_talloc(talloc_tos(),
759 NULL,
761 &consumed_path,
762 path_ucs,
763 consumed_ucs,
764 STR_UNICODE) == 0) {
765 if (errno != 0) {
766 status = map_nt_error_from_unix(errno);
767 } else {
768 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
770 goto out;
772 if (consumed_path == NULL) {
773 status = map_nt_error_from_unix(errno);
774 goto out;
776 *consumed = strlen(consumed_path);
778 if (num_referrals != 0) {
779 uint16 ref_version;
780 uint16 ref_size;
781 int i;
782 uint16 node_offset;
784 referrals = talloc_array(ctx, struct client_dfs_referral,
785 num_referrals);
787 if (!referrals) {
788 status = NT_STATUS_NO_MEMORY;
789 goto out;
791 /* start at the referrals array */
793 p = (char *)rdata+8;
794 for (i=0; i<num_referrals && p < endp; i++) {
795 if (p + 18 > endp) {
796 goto out;
798 ref_version = SVAL(p, 0);
799 ref_size = SVAL(p, 2);
800 node_offset = SVAL(p, 16);
802 if (ref_version != 3) {
803 p += ref_size;
804 continue;
807 referrals[i].proximity = SVAL(p, 8);
808 referrals[i].ttl = SVAL(p, 10);
810 if (p + node_offset > endp) {
811 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
812 goto out;
814 clistr_pull_talloc(referrals,
815 (const char *)rdata,
816 recv_flags2,
817 &referrals[i].dfspath,
818 p+node_offset,
819 PTR_DIFF(endp, p+node_offset),
820 STR_TERMINATE|STR_UNICODE);
822 if (!referrals[i].dfspath) {
823 status = map_nt_error_from_unix(errno);
824 goto out;
826 p += ref_size;
828 if (i < num_referrals) {
829 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
830 goto out;
834 *num_refs = num_referrals;
835 *refs = referrals;
837 out:
839 TALLOC_FREE(frame);
840 return status;
843 /********************************************************************
844 ********************************************************************/
845 struct cli_dfs_path_split {
846 char *server;
847 char *share;
848 char *extrapath;
851 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
852 const char *mountpt,
853 const struct user_auth_info *dfs_auth_info,
854 struct cli_state *rootcli,
855 const char *path,
856 struct cli_state **targetcli,
857 char **pp_targetpath)
859 struct client_dfs_referral *refs = NULL;
860 size_t num_refs = 0;
861 size_t consumed = 0;
862 struct cli_state *cli_ipc = NULL;
863 char *dfs_path = NULL;
864 char *cleanpath = NULL;
865 char *extrapath = NULL;
866 int pathlen;
867 struct cli_state *newcli = NULL;
868 struct cli_state *ccli = NULL;
869 int count = 0;
870 char *newpath = NULL;
871 char *newmount = NULL;
872 char *ppath = NULL;
873 SMB_STRUCT_STAT sbuf;
874 uint32 attributes;
875 NTSTATUS status;
876 struct smbXcli_tcon *root_tcon = NULL;
877 struct smbXcli_tcon *target_tcon = NULL;
878 struct cli_dfs_path_split *dfs_refs = NULL;
880 if ( !rootcli || !path || !targetcli ) {
881 return NT_STATUS_INVALID_PARAMETER;
884 /* Don't do anything if this is not a DFS root. */
886 if (smbXcli_conn_protocol(rootcli->conn) >= PROTOCOL_SMB2_02) {
887 root_tcon = rootcli->smb2.tcon;
888 } else {
889 root_tcon = rootcli->smb1.tcon;
892 if (!smbXcli_tcon_is_dfs_share(root_tcon)) {
893 *targetcli = rootcli;
894 *pp_targetpath = talloc_strdup(ctx, path);
895 if (!*pp_targetpath) {
896 return NT_STATUS_NO_MEMORY;
898 return NT_STATUS_OK;
901 *targetcli = NULL;
903 /* Send a trans2_query_path_info to check for a referral. */
905 cleanpath = clean_path(ctx, path);
906 if (!cleanpath) {
907 return NT_STATUS_NO_MEMORY;
910 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
911 if (!dfs_path) {
912 return NT_STATUS_NO_MEMORY;
915 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
916 if (NT_STATUS_IS_OK(status)) {
917 /* This is an ordinary path, just return it. */
918 *targetcli = rootcli;
919 *pp_targetpath = talloc_strdup(ctx, path);
920 if (!*pp_targetpath) {
921 return NT_STATUS_NO_MEMORY;
923 goto done;
926 /* Special case where client asked for a path that does not exist */
928 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
929 status)) {
930 *targetcli = rootcli;
931 *pp_targetpath = talloc_strdup(ctx, path);
932 if (!*pp_targetpath) {
933 return NT_STATUS_NO_MEMORY;
935 goto done;
938 /* We got an error, check for DFS referral. */
940 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
941 status)) {
942 return status;
945 /* Check for the referral. */
947 status = cli_cm_open(ctx,
948 rootcli,
949 smbXcli_conn_remote_name(rootcli->conn),
950 "IPC$",
951 dfs_auth_info,
952 false,
953 smb1cli_conn_encryption_on(rootcli->conn),
954 smbXcli_conn_protocol(rootcli->conn),
956 0x20,
957 &cli_ipc);
958 if (!NT_STATUS_IS_OK(status)) {
959 return status;
962 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
963 &num_refs, &consumed);
964 if (!NT_STATUS_IS_OK(status) || !num_refs) {
965 return status;
968 if (!refs[0].dfspath) {
969 return NT_STATUS_NOT_FOUND;
973 * Bug#10123 - DFS referal entries can be provided in a random order,
974 * so check the connection cache for each item to avoid unnecessary
975 * reconnections.
977 dfs_refs = talloc_array(ctx, struct cli_dfs_path_split, num_refs);
978 if (dfs_refs == NULL) {
979 return NT_STATUS_NO_MEMORY;
982 for (count = 0; count < num_refs; count++) {
983 if (!split_dfs_path(dfs_refs, refs[count].dfspath,
984 &dfs_refs[count].server,
985 &dfs_refs[count].share,
986 &dfs_refs[count].extrapath)) {
987 TALLOC_FREE(dfs_refs);
988 return NT_STATUS_NOT_FOUND;
991 ccli = cli_cm_find(rootcli, dfs_refs[count].server,
992 dfs_refs[count].share);
993 if (ccli != NULL) {
994 extrapath = dfs_refs[count].extrapath;
995 *targetcli = ccli;
996 break;
1001 * If no cached connection was found, then connect to the first live
1002 * referral server in the list.
1004 for (count = 0; (ccli == NULL) && (count < num_refs); count++) {
1005 /* Connect to the target server & share */
1006 status = cli_cm_connect(ctx, rootcli,
1007 dfs_refs[count].server,
1008 dfs_refs[count].share,
1009 dfs_auth_info,
1010 false,
1011 smb1cli_conn_encryption_on(rootcli->conn),
1012 smbXcli_conn_protocol(rootcli->conn),
1014 0x20,
1015 targetcli);
1016 if (!NT_STATUS_IS_OK(status)) {
1017 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
1018 dfs_refs[count].server,
1019 dfs_refs[count].share);
1020 continue;
1021 } else {
1022 extrapath = dfs_refs[count].extrapath;
1023 break;
1027 /* No available referral server for the connection */
1028 if (*targetcli == NULL) {
1029 TALLOC_FREE(dfs_refs);
1030 return status;
1033 /* Make sure to recreate the original string including any wildcards. */
1035 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
1036 if (!dfs_path) {
1037 TALLOC_FREE(dfs_refs);
1038 return NT_STATUS_NO_MEMORY;
1040 pathlen = strlen(dfs_path);
1041 consumed = MIN(pathlen, consumed);
1042 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
1043 if (!*pp_targetpath) {
1044 TALLOC_FREE(dfs_refs);
1045 return NT_STATUS_NO_MEMORY;
1047 dfs_path[consumed] = '\0';
1050 * *pp_targetpath is now the unconsumed part of the path.
1051 * dfs_path is now the consumed part of the path
1052 * (in \server\share\path format).
1055 if (extrapath && strlen(extrapath) > 0) {
1056 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
1057 /* put the trailing \ on the path, so to be save we put one in if needed */
1058 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
1059 *pp_targetpath = talloc_asprintf(ctx,
1060 "%s\\%s",
1061 extrapath,
1062 *pp_targetpath);
1063 } else {
1064 *pp_targetpath = talloc_asprintf(ctx,
1065 "%s%s",
1066 extrapath,
1067 *pp_targetpath);
1069 if (!*pp_targetpath) {
1070 TALLOC_FREE(dfs_refs);
1071 return NT_STATUS_NO_MEMORY;
1075 /* parse out the consumed mount path */
1076 /* trim off the \server\share\ */
1078 ppath = dfs_path;
1080 if (*ppath != '\\') {
1081 d_printf("cli_resolve_path: "
1082 "dfs_path (%s) not in correct format.\n",
1083 dfs_path );
1084 TALLOC_FREE(dfs_refs);
1085 return NT_STATUS_NOT_FOUND;
1088 ppath++; /* Now pointing at start of server name. */
1090 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
1091 TALLOC_FREE(dfs_refs);
1092 return NT_STATUS_NOT_FOUND;
1095 ppath++; /* Now pointing at start of share name. */
1097 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
1098 TALLOC_FREE(dfs_refs);
1099 return NT_STATUS_NOT_FOUND;
1102 ppath++; /* Now pointing at path component. */
1104 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
1105 if (!newmount) {
1106 TALLOC_FREE(dfs_refs);
1107 return NT_STATUS_NOT_FOUND;
1110 cli_set_mntpoint(*targetcli, newmount);
1112 /* Check for another dfs referral, note that we are not
1113 checking for loops here. */
1115 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
1116 status = cli_resolve_path(ctx,
1117 newmount,
1118 dfs_auth_info,
1119 *targetcli,
1120 *pp_targetpath,
1121 &newcli,
1122 &newpath);
1123 if (NT_STATUS_IS_OK(status)) {
1125 * When cli_resolve_path returns true here it's always
1126 * returning the complete path in newpath, so we're done
1127 * here.
1129 *targetcli = newcli;
1130 *pp_targetpath = newpath;
1131 TALLOC_FREE(dfs_refs);
1132 return status;
1136 done:
1138 if (smbXcli_conn_protocol((*targetcli)->conn) >= PROTOCOL_SMB2_02) {
1139 target_tcon = (*targetcli)->smb2.tcon;
1140 } else {
1141 target_tcon = (*targetcli)->smb1.tcon;
1144 /* If returning true ensure we return a dfs root full path. */
1145 if (smbXcli_tcon_is_dfs_share(target_tcon)) {
1146 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1147 if (!dfs_path) {
1148 TALLOC_FREE(dfs_refs);
1149 return NT_STATUS_NO_MEMORY;
1151 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1152 if (*pp_targetpath == NULL) {
1153 TALLOC_FREE(dfs_refs);
1154 return NT_STATUS_NO_MEMORY;
1158 TALLOC_FREE(dfs_refs);
1159 return NT_STATUS_OK;
1162 /********************************************************************
1163 ********************************************************************/
1165 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1166 struct cli_state *cli,
1167 const char *sharename,
1168 char **pp_newserver,
1169 char **pp_newshare,
1170 bool force_encrypt,
1171 const char *username,
1172 const char *password,
1173 const char *domain)
1175 struct client_dfs_referral *refs = NULL;
1176 size_t num_refs = 0;
1177 size_t consumed = 0;
1178 char *fullpath = NULL;
1179 bool res;
1180 uint16 cnum;
1181 char *newextrapath = NULL;
1182 NTSTATUS status;
1183 const char *remote_name;
1185 if (!cli || !sharename) {
1186 return false;
1189 remote_name = smbXcli_conn_remote_name(cli->conn);
1190 cnum = cli_state_get_tid(cli);
1192 /* special case. never check for a referral on the IPC$ share */
1194 if (strequal(sharename, "IPC$")) {
1195 return false;
1198 /* send a trans2_query_path_info to check for a referral */
1200 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1201 if (!fullpath) {
1202 return false;
1205 /* check for the referral */
1207 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL, 0))) {
1208 return false;
1211 if (force_encrypt) {
1212 status = cli_cm_force_encryption(cli,
1213 username,
1214 password,
1215 domain,
1216 "IPC$");
1217 if (!NT_STATUS_IS_OK(status)) {
1218 return false;
1222 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1223 &num_refs, &consumed);
1224 res = NT_STATUS_IS_OK(status);
1226 status = cli_tdis(cli);
1227 if (!NT_STATUS_IS_OK(status)) {
1228 return false;
1231 cli_state_set_tid(cli, cnum);
1233 if (!res || !num_refs) {
1234 return false;
1237 if (!refs[0].dfspath) {
1238 return false;
1241 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1242 pp_newshare, &newextrapath)) {
1243 return false;
1246 /* check that this is not a self-referral */
1248 if (strequal(remote_name, *pp_newserver) &&
1249 strequal(sharename, *pp_newshare)) {
1250 return false;
1253 return true;