libcli/smb: move smb2cli_close.c from source3 to the toplevel
[Samba/gebeck_regimport.git] / source3 / libsmb / clidfs.c
blob4e9ac0cd2627df3c9f79e7e13ebdd925876de64f
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"
29 /********************************************************************
30 Important point.
32 DFS paths are *always* of the form \server\share\<pathname> (the \ characters
33 are not C escaped here).
35 - but if we're using POSIX paths then <pathname> may contain
36 '/' separators, not '\\' separators. So cope with '\\' or '/'
37 as a separator when looking at the pathname part.... JRA.
38 ********************************************************************/
40 /********************************************************************
41 Ensure a connection is encrypted.
42 ********************************************************************/
44 NTSTATUS cli_cm_force_encryption(struct cli_state *c,
45 const char *username,
46 const char *password,
47 const char *domain,
48 const char *sharename)
50 NTSTATUS status = cli_force_encryption(c,
51 username,
52 password,
53 domain);
55 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
56 d_printf("Encryption required and "
57 "server that doesn't support "
58 "UNIX extensions - failing connect\n");
59 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNKNOWN_REVISION)) {
60 d_printf("Encryption required and "
61 "can't get UNIX CIFS extensions "
62 "version from server.\n");
63 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
64 d_printf("Encryption required and "
65 "share %s doesn't support "
66 "encryption.\n", sharename);
67 } else if (!NT_STATUS_IS_OK(status)) {
68 d_printf("Encryption required and "
69 "setup failed with error %s.\n",
70 nt_errstr(status));
73 return status;
76 /********************************************************************
77 Return a connection to a server.
78 ********************************************************************/
80 static NTSTATUS do_connect(TALLOC_CTX *ctx,
81 const char *server,
82 const char *share,
83 const struct user_auth_info *auth_info,
84 bool show_sessetup,
85 bool force_encrypt,
86 int max_protocol,
87 int port,
88 int name_type,
89 struct cli_state **pcli)
91 struct cli_state *c = NULL;
92 char *servicename;
93 char *sharename;
94 char *newserver, *newshare;
95 const char *username;
96 const char *password;
97 NTSTATUS status;
98 int flags = 0;
100 /* make a copy so we don't modify the global string 'service' */
101 servicename = talloc_strdup(ctx,share);
102 if (!servicename) {
103 return NT_STATUS_NO_MEMORY;
105 sharename = servicename;
106 if (*sharename == '\\') {
107 sharename += 2;
108 if (server == NULL) {
109 server = sharename;
111 sharename = strchr_m(sharename,'\\');
112 if (!sharename) {
113 return NT_STATUS_NO_MEMORY;
115 *sharename = 0;
116 sharename++;
118 if (server == NULL) {
119 return NT_STATUS_INVALID_PARAMETER;
122 if (get_cmdline_auth_info_use_kerberos(auth_info)) {
123 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
125 if (get_cmdline_auth_info_fallback_after_kerberos(auth_info)) {
126 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
128 if (get_cmdline_auth_info_use_ccache(auth_info)) {
129 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
132 status = cli_connect_nb(
133 server, NULL, port, name_type, NULL,
134 get_cmdline_auth_info_signing_state(auth_info),
135 flags, &c);
137 if (!NT_STATUS_IS_OK(status)) {
138 d_printf("Connection to %s failed (Error %s)\n",
139 server,
140 nt_errstr(status));
141 return status;
144 if (max_protocol == 0) {
145 max_protocol = PROTOCOL_NT1;
147 DEBUG(4,(" session request ok\n"));
149 status = cli_negprot(c, max_protocol);
151 if (!NT_STATUS_IS_OK(status)) {
152 d_printf("protocol negotiation failed: %s\n",
153 nt_errstr(status));
154 cli_shutdown(c);
155 return status;
158 username = get_cmdline_auth_info_username(auth_info);
159 password = get_cmdline_auth_info_password(auth_info);
161 status = cli_session_setup(c, username,
162 password, strlen(password),
163 password, strlen(password),
164 lp_workgroup());
165 if (!NT_STATUS_IS_OK(status)) {
166 /* If a password was not supplied then
167 * try again with a null username. */
168 if (password[0] || !username[0] ||
169 get_cmdline_auth_info_use_kerberos(auth_info) ||
170 !NT_STATUS_IS_OK(status = cli_session_setup(c, "",
171 "", 0,
172 "", 0,
173 lp_workgroup()))) {
174 d_printf("session setup failed: %s\n",
175 nt_errstr(status));
176 if (NT_STATUS_EQUAL(status,
177 NT_STATUS_MORE_PROCESSING_REQUIRED))
178 d_printf("did you forget to run kinit?\n");
179 cli_shutdown(c);
180 return status;
182 d_printf("Anonymous login successful\n");
183 status = cli_init_creds(c, "", lp_workgroup(), "");
184 } else {
185 status = cli_init_creds(c, username, lp_workgroup(), password);
188 if (!NT_STATUS_IS_OK(status)) {
189 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
190 cli_shutdown(c);
191 return status;
194 if ( show_sessetup ) {
195 if (*c->server_domain) {
196 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
197 c->server_domain,c->server_os,c->server_type));
198 } else if (*c->server_os || *c->server_type) {
199 DEBUG(0,("OS=[%s] Server=[%s]\n",
200 c->server_os,c->server_type));
203 DEBUG(4,(" session setup ok\n"));
205 /* here's the fun part....to support 'msdfs proxy' shares
206 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
207 here before trying to connect to the original share.
208 cli_check_msdfs_proxy() will fail if it is a normal share. */
210 if ((cli_state_capabilities(c) & CAP_DFS) &&
211 cli_check_msdfs_proxy(ctx, c, sharename,
212 &newserver, &newshare,
213 force_encrypt,
214 username,
215 password,
216 lp_workgroup())) {
217 cli_shutdown(c);
218 return do_connect(ctx, newserver,
219 newshare, auth_info, false,
220 force_encrypt, max_protocol,
221 port, name_type, pcli);
224 /* must be a normal share */
226 status = cli_tree_connect(c, sharename, "?????",
227 password, strlen(password)+1);
228 if (!NT_STATUS_IS_OK(status)) {
229 d_printf("tree connect failed: %s\n", nt_errstr(status));
230 cli_shutdown(c);
231 return status;
234 if (force_encrypt) {
235 status = cli_cm_force_encryption(c,
236 username,
237 password,
238 lp_workgroup(),
239 sharename);
240 if (!NT_STATUS_IS_OK(status)) {
241 cli_shutdown(c);
242 return status;
246 DEBUG(4,(" tconx ok\n"));
247 *pcli = c;
248 return NT_STATUS_OK;
251 /****************************************************************************
252 ****************************************************************************/
254 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
256 char *name = clean_name(NULL, mnt);
257 if (!name) {
258 return;
260 TALLOC_FREE(cli->dfs_mountpoint);
261 cli->dfs_mountpoint = talloc_strdup(cli, name);
262 TALLOC_FREE(name);
265 /********************************************************************
266 Add a new connection to the list.
267 referring_cli == NULL means a new initial connection.
268 ********************************************************************/
270 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
271 struct cli_state *referring_cli,
272 const char *server,
273 const char *share,
274 const struct user_auth_info *auth_info,
275 bool show_hdr,
276 bool force_encrypt,
277 int max_protocol,
278 int port,
279 int name_type,
280 struct cli_state **pcli)
282 struct cli_state *cli;
283 NTSTATUS status;
285 status = do_connect(ctx, server, share,
286 auth_info,
287 show_hdr, force_encrypt, max_protocol,
288 port, name_type, &cli);
290 if (!NT_STATUS_IS_OK(status)) {
291 return status;
294 /* Enter into the list. */
295 if (referring_cli) {
296 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
299 if (referring_cli && referring_cli->requested_posix_capabilities) {
300 uint16 major, minor;
301 uint32 caplow, caphigh;
302 status = cli_unix_extensions_version(cli, &major, &minor,
303 &caplow, &caphigh);
304 if (NT_STATUS_IS_OK(status)) {
305 cli_set_unix_extensions_capabilities(cli,
306 major, minor,
307 caplow, caphigh);
311 *pcli = cli;
312 return NT_STATUS_OK;
315 /********************************************************************
316 Return a connection to a server on a particular share.
317 ********************************************************************/
319 static struct cli_state *cli_cm_find(struct cli_state *cli,
320 const char *server,
321 const char *share)
323 struct cli_state *p;
325 if (cli == NULL) {
326 return NULL;
329 /* Search to the start of the list. */
330 for (p = cli; p; p = DLIST_PREV(p)) {
331 const char *remote_name =
332 cli_state_remote_name(p);
334 if (strequal(server, remote_name) &&
335 strequal(share,p->share)) {
336 return p;
340 /* Search to the end of the list. */
341 for (p = cli->next; p; p = p->next) {
342 const char *remote_name =
343 cli_state_remote_name(p);
345 if (strequal(server, remote_name) &&
346 strequal(share,p->share)) {
347 return p;
351 return NULL;
354 /****************************************************************************
355 Open a client connection to a \\server\share.
356 ****************************************************************************/
358 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
359 struct cli_state *referring_cli,
360 const char *server,
361 const char *share,
362 const struct user_auth_info *auth_info,
363 bool show_hdr,
364 bool force_encrypt,
365 int max_protocol,
366 int port,
367 int name_type,
368 struct cli_state **pcli)
370 /* Try to reuse an existing connection in this list. */
371 struct cli_state *c = cli_cm_find(referring_cli, server, share);
372 NTSTATUS status;
374 if (c) {
375 *pcli = c;
376 return NT_STATUS_OK;
379 if (auth_info == NULL) {
380 /* Can't do a new connection
381 * without auth info. */
382 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
383 "without auth info\n",
384 server, share );
385 return NT_STATUS_INVALID_PARAMETER;
388 status = cli_cm_connect(ctx,
389 referring_cli,
390 server,
391 share,
392 auth_info,
393 show_hdr,
394 force_encrypt,
395 max_protocol,
396 port,
397 name_type,
398 &c);
399 if (!NT_STATUS_IS_OK(status)) {
400 return status;
402 *pcli = c;
403 return NT_STATUS_OK;
406 /****************************************************************************
407 ****************************************************************************/
409 void cli_cm_display(struct cli_state *cli)
411 int i;
413 for (i=0; cli; cli = cli->next,i++ ) {
414 d_printf("%d:\tserver=%s, share=%s\n",
415 i, cli_state_remote_name(cli), cli->share);
419 /****************************************************************************
420 ****************************************************************************/
422 /****************************************************************************
423 ****************************************************************************/
425 #if 0
426 void cli_cm_set_credentials(struct user_auth_info *auth_info)
428 SAFE_FREE(cm_creds.username);
429 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
430 auth_info));
432 if (get_cmdline_auth_info_got_pass(auth_info)) {
433 cm_set_password(get_cmdline_auth_info_password(auth_info));
436 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
437 cm_creds.fallback_after_kerberos = false;
438 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
440 #endif
442 /**********************************************************************
443 split a dfs path into the server, share name, and extrapath components
444 **********************************************************************/
446 static bool split_dfs_path(TALLOC_CTX *ctx,
447 const char *nodepath,
448 char **pp_server,
449 char **pp_share,
450 char **pp_extrapath)
452 char *p, *q;
453 char *path;
455 *pp_server = NULL;
456 *pp_share = NULL;
457 *pp_extrapath = NULL;
459 path = talloc_strdup(ctx, nodepath);
460 if (!path) {
461 goto fail;
464 if ( path[0] != '\\' ) {
465 goto fail;
468 p = strchr_m( path + 1, '\\' );
469 if ( !p ) {
470 goto fail;
473 *p = '\0';
474 p++;
476 /* Look for any extra/deep path */
477 q = strchr_m(p, '\\');
478 if (q != NULL) {
479 *q = '\0';
480 q++;
481 *pp_extrapath = talloc_strdup(ctx, q);
482 } else {
483 *pp_extrapath = talloc_strdup(ctx, "");
485 if (*pp_extrapath == NULL) {
486 goto fail;
489 *pp_share = talloc_strdup(ctx, p);
490 if (*pp_share == NULL) {
491 goto fail;
494 *pp_server = talloc_strdup(ctx, &path[1]);
495 if (*pp_server == NULL) {
496 goto fail;
499 TALLOC_FREE(path);
500 return true;
502 fail:
503 TALLOC_FREE(*pp_share);
504 TALLOC_FREE(*pp_extrapath);
505 TALLOC_FREE(path);
506 return false;
509 /****************************************************************************
510 Return the original path truncated at the directory component before
511 the first wildcard character. Trust the caller to provide a NULL
512 terminated string
513 ****************************************************************************/
515 static char *clean_path(TALLOC_CTX *ctx, const char *path)
517 size_t len;
518 char *p1, *p2, *p;
519 char *path_out;
521 /* No absolute paths. */
522 while (IS_DIRECTORY_SEP(*path)) {
523 path++;
526 path_out = talloc_strdup(ctx, path);
527 if (!path_out) {
528 return NULL;
531 p1 = strchr_m(path_out, '*');
532 p2 = strchr_m(path_out, '?');
534 if (p1 || p2) {
535 if (p1 && p2) {
536 p = MIN(p1,p2);
537 } else if (!p1) {
538 p = p2;
539 } else {
540 p = p1;
542 *p = '\0';
544 /* Now go back to the start of this component. */
545 p1 = strrchr_m(path_out, '/');
546 p2 = strrchr_m(path_out, '\\');
547 p = MAX(p1,p2);
548 if (p) {
549 *p = '\0';
553 /* Strip any trailing separator */
555 len = strlen(path_out);
556 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
557 path_out[len-1] = '\0';
560 return path_out;
563 /****************************************************************************
564 ****************************************************************************/
566 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
567 struct cli_state *cli,
568 const char *dir)
570 char path_sep = '\\';
572 /* Ensure the extrapath doesn't start with a separator. */
573 while (IS_DIRECTORY_SEP(*dir)) {
574 dir++;
577 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
578 path_sep = '/';
580 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
581 path_sep,
582 cli_state_remote_name(cli),
583 path_sep,
584 cli->share,
585 path_sep,
586 dir);
589 /********************************************************************
590 check for dfs referral
591 ********************************************************************/
593 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
594 NTSTATUS status)
596 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
598 if (!(cli_state_capabilities(cli) & CAP_UNICODE)) {
599 return false;
601 if (!(cli_state_capabilities(cli) & CAP_STATUS32)) {
602 return false;
604 if (NT_STATUS_EQUAL(status, expected)) {
605 return true;
607 return false;
610 /********************************************************************
611 Get the dfs referral link.
612 ********************************************************************/
614 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
615 struct cli_state *cli,
616 const char *path,
617 struct client_dfs_referral **refs,
618 size_t *num_refs,
619 size_t *consumed)
621 unsigned int data_len = 0;
622 unsigned int param_len = 0;
623 uint16_t setup[1];
624 uint16_t recv_flags2;
625 uint8_t *param = NULL;
626 uint8_t *rdata = NULL;
627 char *p;
628 char *endp;
629 smb_ucs2_t *path_ucs;
630 char *consumed_path = NULL;
631 uint16_t consumed_ucs;
632 uint16 num_referrals;
633 struct client_dfs_referral *referrals = NULL;
634 NTSTATUS status;
635 TALLOC_CTX *frame = talloc_stackframe();
637 *num_refs = 0;
638 *refs = NULL;
640 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
642 param = talloc_array(talloc_tos(), uint8_t, 2);
643 if (!param) {
644 status = NT_STATUS_NO_MEMORY;
645 goto out;
647 SSVAL(param, 0, 0x03); /* max referral level */
649 param = trans2_bytes_push_str(param, cli_ucs2(cli),
650 path, strlen(path)+1,
651 NULL);
652 if (!param) {
653 status = NT_STATUS_NO_MEMORY;
654 goto out;
656 param_len = talloc_get_size(param);
657 path_ucs = (smb_ucs2_t *)&param[2];
659 status = cli_trans(talloc_tos(), cli, SMBtrans2,
660 NULL, 0xffff, 0, 0,
661 setup, 1, 0,
662 param, param_len, 2,
663 NULL, 0, CLI_BUFFER_SIZE,
664 &recv_flags2,
665 NULL, 0, NULL, /* rsetup */
666 NULL, 0, NULL,
667 &rdata, 4, &data_len);
668 if (!NT_STATUS_IS_OK(status)) {
669 goto out;
672 endp = (char *)rdata + data_len;
674 consumed_ucs = SVAL(rdata, 0);
675 num_referrals = SVAL(rdata, 2);
677 /* consumed_ucs is the number of bytes
678 * of the UCS2 path consumed not counting any
679 * terminating null. We need to convert
680 * back to unix charset and count again
681 * to get the number of bytes consumed from
682 * the incoming path. */
684 errno = 0;
685 if (pull_string_talloc(talloc_tos(),
686 NULL,
688 &consumed_path,
689 path_ucs,
690 consumed_ucs,
691 STR_UNICODE) == 0) {
692 if (errno != 0) {
693 status = map_nt_error_from_unix(errno);
694 } else {
695 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
697 goto out;
699 if (consumed_path == NULL) {
700 status = map_nt_error_from_unix(errno);
701 goto out;
703 *consumed = strlen(consumed_path);
705 if (num_referrals != 0) {
706 uint16 ref_version;
707 uint16 ref_size;
708 int i;
709 uint16 node_offset;
711 referrals = talloc_array(ctx, struct client_dfs_referral,
712 num_referrals);
714 if (!referrals) {
715 status = NT_STATUS_NO_MEMORY;
716 goto out;
718 /* start at the referrals array */
720 p = (char *)rdata+8;
721 for (i=0; i<num_referrals && p < endp; i++) {
722 if (p + 18 > endp) {
723 goto out;
725 ref_version = SVAL(p, 0);
726 ref_size = SVAL(p, 2);
727 node_offset = SVAL(p, 16);
729 if (ref_version != 3) {
730 p += ref_size;
731 continue;
734 referrals[i].proximity = SVAL(p, 8);
735 referrals[i].ttl = SVAL(p, 10);
737 if (p + node_offset > endp) {
738 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
739 goto out;
741 clistr_pull_talloc(referrals,
742 (const char *)rdata,
743 recv_flags2,
744 &referrals[i].dfspath,
745 p+node_offset,
746 PTR_DIFF(endp, p+node_offset),
747 STR_TERMINATE|STR_UNICODE);
749 if (!referrals[i].dfspath) {
750 status = map_nt_error_from_unix(errno);
751 goto out;
753 p += ref_size;
755 if (i < num_referrals) {
756 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
757 goto out;
761 *num_refs = num_referrals;
762 *refs = referrals;
764 out:
766 TALLOC_FREE(frame);
767 return status;
770 /********************************************************************
771 ********************************************************************/
773 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
774 const char *mountpt,
775 const struct user_auth_info *dfs_auth_info,
776 struct cli_state *rootcli,
777 const char *path,
778 struct cli_state **targetcli,
779 char **pp_targetpath)
781 struct client_dfs_referral *refs = NULL;
782 size_t num_refs = 0;
783 size_t consumed = 0;
784 struct cli_state *cli_ipc = NULL;
785 char *dfs_path = NULL;
786 char *cleanpath = NULL;
787 char *extrapath = NULL;
788 int pathlen;
789 char *server = NULL;
790 char *share = NULL;
791 struct cli_state *newcli = NULL;
792 char *newpath = NULL;
793 char *newmount = NULL;
794 char *ppath = NULL;
795 SMB_STRUCT_STAT sbuf;
796 uint32 attributes;
797 NTSTATUS status;
799 if ( !rootcli || !path || !targetcli ) {
800 return NT_STATUS_INVALID_PARAMETER;
803 /* Don't do anything if this is not a DFS root. */
805 if ( !rootcli->dfsroot) {
806 *targetcli = rootcli;
807 *pp_targetpath = talloc_strdup(ctx, path);
808 if (!*pp_targetpath) {
809 return NT_STATUS_NO_MEMORY;
811 return NT_STATUS_OK;
814 *targetcli = NULL;
816 /* Send a trans2_query_path_info to check for a referral. */
818 cleanpath = clean_path(ctx, path);
819 if (!cleanpath) {
820 return NT_STATUS_NO_MEMORY;
823 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
824 if (!dfs_path) {
825 return NT_STATUS_NO_MEMORY;
828 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
829 if (NT_STATUS_IS_OK(status)) {
830 /* This is an ordinary path, just return it. */
831 *targetcli = rootcli;
832 *pp_targetpath = talloc_strdup(ctx, path);
833 if (!*pp_targetpath) {
834 return NT_STATUS_NO_MEMORY;
836 goto done;
839 /* Special case where client asked for a path that does not exist */
841 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
842 status)) {
843 *targetcli = rootcli;
844 *pp_targetpath = talloc_strdup(ctx, path);
845 if (!*pp_targetpath) {
846 return NT_STATUS_NO_MEMORY;
848 goto done;
851 /* We got an error, check for DFS referral. */
853 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
854 status)) {
855 return status;
858 /* Check for the referral. */
860 status = cli_cm_open(ctx,
861 rootcli,
862 cli_state_remote_name(rootcli),
863 "IPC$",
864 dfs_auth_info,
865 false,
866 cli_state_encryption_on(rootcli),
867 cli_state_protocol(rootcli),
869 0x20,
870 &cli_ipc);
871 if (!NT_STATUS_IS_OK(status)) {
872 return status;
875 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
876 &num_refs, &consumed);
877 if (!NT_STATUS_IS_OK(status) || !num_refs) {
878 return status;
881 /* Just store the first referral for now. */
883 if (!refs[0].dfspath) {
884 return NT_STATUS_NOT_FOUND;
886 if (!split_dfs_path(ctx, refs[0].dfspath, &server, &share,
887 &extrapath)) {
888 return NT_STATUS_NOT_FOUND;
891 /* Make sure to recreate the original string including any wildcards. */
893 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
894 if (!dfs_path) {
895 return NT_STATUS_NO_MEMORY;
897 pathlen = strlen(dfs_path);
898 consumed = MIN(pathlen, consumed);
899 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
900 if (!*pp_targetpath) {
901 return NT_STATUS_NO_MEMORY;
903 dfs_path[consumed] = '\0';
906 * *pp_targetpath is now the unconsumed part of the path.
907 * dfs_path is now the consumed part of the path
908 * (in \server\share\path format).
911 /* Open the connection to the target server & share */
912 status = cli_cm_open(ctx, rootcli,
913 server,
914 share,
915 dfs_auth_info,
916 false,
917 cli_state_encryption_on(rootcli),
918 cli_state_protocol(rootcli),
920 0x20,
921 targetcli);
922 if (!NT_STATUS_IS_OK(status)) {
923 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
924 server, share );
925 return status;
928 if (extrapath && strlen(extrapath) > 0) {
929 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
930 /* put the trailing \ on the path, so to be save we put one in if needed */
931 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
932 *pp_targetpath = talloc_asprintf(ctx,
933 "%s\\%s",
934 extrapath,
935 *pp_targetpath);
936 } else {
937 *pp_targetpath = talloc_asprintf(ctx,
938 "%s%s",
939 extrapath,
940 *pp_targetpath);
942 if (!*pp_targetpath) {
943 return NT_STATUS_NO_MEMORY;
947 /* parse out the consumed mount path */
948 /* trim off the \server\share\ */
950 ppath = dfs_path;
952 if (*ppath != '\\') {
953 d_printf("cli_resolve_path: "
954 "dfs_path (%s) not in correct format.\n",
955 dfs_path );
956 return NT_STATUS_NOT_FOUND;
959 ppath++; /* Now pointing at start of server name. */
961 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
962 return NT_STATUS_NOT_FOUND;
965 ppath++; /* Now pointing at start of share name. */
967 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
968 return NT_STATUS_NOT_FOUND;
971 ppath++; /* Now pointing at path component. */
973 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
974 if (!newmount) {
975 return NT_STATUS_NOT_FOUND;
978 cli_set_mntpoint(*targetcli, newmount);
980 /* Check for another dfs referral, note that we are not
981 checking for loops here. */
983 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
984 status = cli_resolve_path(ctx,
985 newmount,
986 dfs_auth_info,
987 *targetcli,
988 *pp_targetpath,
989 &newcli,
990 &newpath);
991 if (NT_STATUS_IS_OK(status)) {
993 * When cli_resolve_path returns true here it's always
994 * returning the complete path in newpath, so we're done
995 * here.
997 *targetcli = newcli;
998 *pp_targetpath = newpath;
999 return status;
1003 done:
1005 /* If returning true ensure we return a dfs root full path. */
1006 if ((*targetcli)->dfsroot) {
1007 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1008 if (!dfs_path) {
1009 return NT_STATUS_NO_MEMORY;
1011 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1012 if (*pp_targetpath == NULL) {
1013 return NT_STATUS_NO_MEMORY;
1017 return NT_STATUS_OK;
1020 /********************************************************************
1021 ********************************************************************/
1023 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1024 struct cli_state *cli,
1025 const char *sharename,
1026 char **pp_newserver,
1027 char **pp_newshare,
1028 bool force_encrypt,
1029 const char *username,
1030 const char *password,
1031 const char *domain)
1033 struct client_dfs_referral *refs = NULL;
1034 size_t num_refs = 0;
1035 size_t consumed = 0;
1036 char *fullpath = NULL;
1037 bool res;
1038 uint16 cnum;
1039 char *newextrapath = NULL;
1040 NTSTATUS status;
1041 const char *remote_name;
1043 if (!cli || !sharename) {
1044 return false;
1047 remote_name = cli_state_remote_name(cli);
1048 cnum = cli_state_get_tid(cli);
1050 /* special case. never check for a referral on the IPC$ share */
1052 if (strequal(sharename, "IPC$")) {
1053 return false;
1056 /* send a trans2_query_path_info to check for a referral */
1058 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1059 if (!fullpath) {
1060 return false;
1063 /* check for the referral */
1065 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL, 0))) {
1066 return false;
1069 if (force_encrypt) {
1070 status = cli_cm_force_encryption(cli,
1071 username,
1072 password,
1073 lp_workgroup(),
1074 "IPC$");
1075 if (!NT_STATUS_IS_OK(status)) {
1076 return false;
1080 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1081 &num_refs, &consumed);
1082 res = NT_STATUS_IS_OK(status);
1084 status = cli_tdis(cli);
1085 if (!NT_STATUS_IS_OK(status)) {
1086 return false;
1089 cli_state_set_tid(cli, cnum);
1091 if (!res || !num_refs) {
1092 return false;
1095 if (!refs[0].dfspath) {
1096 return false;
1099 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1100 pp_newshare, &newextrapath)) {
1101 return false;
1104 /* check that this is not a self-referral */
1106 if (strequal(remote_name, *pp_newserver) &&
1107 strequal(sharename, *pp_newshare)) {
1108 return false;
1111 return true;