regedit: Print help on left side of screen instead of the right.
[Samba.git] / source3 / libsmb / clidfs.c
blob95f8817a94b70765027f1340f5b0ee45f66aba85
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 = cli_force_encryption(c,
52 username,
53 password,
54 domain);
56 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
57 d_printf("Encryption required and "
58 "server that doesn't support "
59 "UNIX extensions - failing connect\n");
60 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNKNOWN_REVISION)) {
61 d_printf("Encryption required and "
62 "can't get UNIX CIFS extensions "
63 "version from server.\n");
64 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
65 d_printf("Encryption required and "
66 "share %s doesn't support "
67 "encryption.\n", sharename);
68 } else if (!NT_STATUS_IS_OK(status)) {
69 d_printf("Encryption required and "
70 "setup failed with error %s.\n",
71 nt_errstr(status));
74 return status;
77 /********************************************************************
78 Return a connection to a server.
79 ********************************************************************/
81 static NTSTATUS do_connect(TALLOC_CTX *ctx,
82 const char *server,
83 const char *share,
84 const struct user_auth_info *auth_info,
85 bool show_sessetup,
86 bool force_encrypt,
87 int max_protocol,
88 int port,
89 int name_type,
90 struct cli_state **pcli)
92 struct cli_state *c = NULL;
93 char *servicename;
94 char *sharename;
95 char *newserver, *newshare;
96 const char *username;
97 const char *password;
98 NTSTATUS status;
99 int flags = 0;
101 /* make a copy so we don't modify the global string 'service' */
102 servicename = talloc_strdup(ctx,share);
103 if (!servicename) {
104 return NT_STATUS_NO_MEMORY;
106 sharename = servicename;
107 if (*sharename == '\\') {
108 sharename += 2;
109 if (server == NULL) {
110 server = sharename;
112 sharename = strchr_m(sharename,'\\');
113 if (!sharename) {
114 return NT_STATUS_NO_MEMORY;
116 *sharename = 0;
117 sharename++;
119 if (server == NULL) {
120 return NT_STATUS_INVALID_PARAMETER;
123 if (get_cmdline_auth_info_use_kerberos(auth_info)) {
124 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
126 if (get_cmdline_auth_info_fallback_after_kerberos(auth_info)) {
127 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
129 if (get_cmdline_auth_info_use_ccache(auth_info)) {
130 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
132 if (get_cmdline_auth_info_use_pw_nt_hash(auth_info)) {
133 flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
136 status = cli_connect_nb(
137 server, NULL, port, name_type, NULL,
138 get_cmdline_auth_info_signing_state(auth_info),
139 flags, &c);
141 if (!NT_STATUS_IS_OK(status)) {
142 d_printf("Connection to %s failed (Error %s)\n",
143 server,
144 nt_errstr(status));
145 return status;
148 if (max_protocol == 0) {
149 max_protocol = PROTOCOL_NT1;
151 DEBUG(4,(" session request ok\n"));
153 status = smbXcli_negprot(c->conn, c->timeout, PROTOCOL_CORE,
154 max_protocol);
156 if (!NT_STATUS_IS_OK(status)) {
157 d_printf("protocol negotiation failed: %s\n",
158 nt_errstr(status));
159 cli_shutdown(c);
160 return status;
163 username = get_cmdline_auth_info_username(auth_info);
164 password = get_cmdline_auth_info_password(auth_info);
166 status = cli_session_setup(c, username,
167 password, strlen(password),
168 password, strlen(password),
169 lp_workgroup());
170 if (!NT_STATUS_IS_OK(status)) {
171 /* If a password was not supplied then
172 * try again with a null username. */
173 if (password[0] || !username[0] ||
174 get_cmdline_auth_info_use_kerberos(auth_info) ||
175 !NT_STATUS_IS_OK(status = cli_session_setup(c, "",
176 "", 0,
177 "", 0,
178 lp_workgroup()))) {
179 d_printf("session setup failed: %s\n",
180 nt_errstr(status));
181 if (NT_STATUS_EQUAL(status,
182 NT_STATUS_MORE_PROCESSING_REQUIRED))
183 d_printf("did you forget to run kinit?\n");
184 cli_shutdown(c);
185 return status;
187 d_printf("Anonymous login successful\n");
188 status = cli_init_creds(c, "", lp_workgroup(), "");
189 } else {
190 status = cli_init_creds(c, username, lp_workgroup(), password);
193 if (!NT_STATUS_IS_OK(status)) {
194 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
195 cli_shutdown(c);
196 return status;
199 if ( show_sessetup ) {
200 if (*c->server_domain) {
201 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
202 c->server_domain,c->server_os,c->server_type));
203 } else if (*c->server_os || *c->server_type) {
204 DEBUG(0,("OS=[%s] Server=[%s]\n",
205 c->server_os,c->server_type));
208 DEBUG(4,(" session setup ok\n"));
210 /* here's the fun part....to support 'msdfs proxy' shares
211 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
212 here before trying to connect to the original share.
213 cli_check_msdfs_proxy() will fail if it is a normal share. */
215 if ((smb1cli_conn_capabilities(c->conn) & CAP_DFS) &&
216 cli_check_msdfs_proxy(ctx, c, sharename,
217 &newserver, &newshare,
218 force_encrypt,
219 username,
220 password,
221 lp_workgroup())) {
222 cli_shutdown(c);
223 return do_connect(ctx, newserver,
224 newshare, auth_info, false,
225 force_encrypt, max_protocol,
226 port, name_type, pcli);
229 /* must be a normal share */
231 status = cli_tree_connect(c, sharename, "?????",
232 password, strlen(password)+1);
233 if (!NT_STATUS_IS_OK(status)) {
234 d_printf("tree connect failed: %s\n", nt_errstr(status));
235 cli_shutdown(c);
236 return status;
239 if (force_encrypt) {
240 status = cli_cm_force_encryption(c,
241 username,
242 password,
243 lp_workgroup(),
244 sharename);
245 if (!NT_STATUS_IS_OK(status)) {
246 cli_shutdown(c);
247 return status;
251 DEBUG(4,(" tconx ok\n"));
252 *pcli = c;
253 return NT_STATUS_OK;
256 /****************************************************************************
257 ****************************************************************************/
259 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
261 char *name = clean_name(NULL, mnt);
262 if (!name) {
263 return;
265 TALLOC_FREE(cli->dfs_mountpoint);
266 cli->dfs_mountpoint = talloc_strdup(cli, name);
267 TALLOC_FREE(name);
270 /********************************************************************
271 Add a new connection to the list.
272 referring_cli == NULL means a new initial connection.
273 ********************************************************************/
275 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
276 struct cli_state *referring_cli,
277 const char *server,
278 const char *share,
279 const struct user_auth_info *auth_info,
280 bool show_hdr,
281 bool force_encrypt,
282 int max_protocol,
283 int port,
284 int name_type,
285 struct cli_state **pcli)
287 struct cli_state *cli;
288 NTSTATUS status;
290 status = do_connect(ctx, server, share,
291 auth_info,
292 show_hdr, force_encrypt, max_protocol,
293 port, name_type, &cli);
295 if (!NT_STATUS_IS_OK(status)) {
296 return status;
299 /* Enter into the list. */
300 if (referring_cli) {
301 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
304 if (referring_cli && referring_cli->requested_posix_capabilities) {
305 uint16 major, minor;
306 uint32 caplow, caphigh;
307 status = cli_unix_extensions_version(cli, &major, &minor,
308 &caplow, &caphigh);
309 if (NT_STATUS_IS_OK(status)) {
310 cli_set_unix_extensions_capabilities(cli,
311 major, minor,
312 caplow, caphigh);
316 *pcli = cli;
317 return NT_STATUS_OK;
320 /********************************************************************
321 Return a connection to a server on a particular share.
322 ********************************************************************/
324 static struct cli_state *cli_cm_find(struct cli_state *cli,
325 const char *server,
326 const char *share)
328 struct cli_state *p;
330 if (cli == NULL) {
331 return NULL;
334 /* Search to the start of the list. */
335 for (p = cli; p; p = DLIST_PREV(p)) {
336 const char *remote_name =
337 smbXcli_conn_remote_name(p->conn);
339 if (strequal(server, remote_name) &&
340 strequal(share,p->share)) {
341 return p;
345 /* Search to the end of the list. */
346 for (p = cli->next; p; p = p->next) {
347 const char *remote_name =
348 smbXcli_conn_remote_name(p->conn);
350 if (strequal(server, remote_name) &&
351 strequal(share,p->share)) {
352 return p;
356 return NULL;
359 /****************************************************************************
360 Open a client connection to a \\server\share.
361 ****************************************************************************/
363 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
364 struct cli_state *referring_cli,
365 const char *server,
366 const char *share,
367 const struct user_auth_info *auth_info,
368 bool show_hdr,
369 bool force_encrypt,
370 int max_protocol,
371 int port,
372 int name_type,
373 struct cli_state **pcli)
375 /* Try to reuse an existing connection in this list. */
376 struct cli_state *c = cli_cm_find(referring_cli, server, share);
377 NTSTATUS status;
379 if (c) {
380 *pcli = c;
381 return NT_STATUS_OK;
384 if (auth_info == NULL) {
385 /* Can't do a new connection
386 * without auth info. */
387 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
388 "without auth info\n",
389 server, share );
390 return NT_STATUS_INVALID_PARAMETER;
393 status = cli_cm_connect(ctx,
394 referring_cli,
395 server,
396 share,
397 auth_info,
398 show_hdr,
399 force_encrypt,
400 max_protocol,
401 port,
402 name_type,
403 &c);
404 if (!NT_STATUS_IS_OK(status)) {
405 return status;
407 *pcli = c;
408 return NT_STATUS_OK;
411 /****************************************************************************
412 ****************************************************************************/
414 void cli_cm_display(struct cli_state *cli)
416 int i;
418 for (i=0; cli; cli = cli->next,i++ ) {
419 d_printf("%d:\tserver=%s, share=%s\n",
420 i, smbXcli_conn_remote_name(cli->conn), cli->share);
424 /****************************************************************************
425 ****************************************************************************/
427 /****************************************************************************
428 ****************************************************************************/
430 #if 0
431 void cli_cm_set_credentials(struct user_auth_info *auth_info)
433 SAFE_FREE(cm_creds.username);
434 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
435 auth_info));
437 if (get_cmdline_auth_info_got_pass(auth_info)) {
438 cm_set_password(get_cmdline_auth_info_password(auth_info));
441 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
442 cm_creds.fallback_after_kerberos = false;
443 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
445 #endif
447 /**********************************************************************
448 split a dfs path into the server, share name, and extrapath components
449 **********************************************************************/
451 static bool split_dfs_path(TALLOC_CTX *ctx,
452 const char *nodepath,
453 char **pp_server,
454 char **pp_share,
455 char **pp_extrapath)
457 char *p, *q;
458 char *path;
460 *pp_server = NULL;
461 *pp_share = NULL;
462 *pp_extrapath = NULL;
464 path = talloc_strdup(ctx, nodepath);
465 if (!path) {
466 goto fail;
469 if ( path[0] != '\\' ) {
470 goto fail;
473 p = strchr_m( path + 1, '\\' );
474 if ( !p ) {
475 goto fail;
478 *p = '\0';
479 p++;
481 /* Look for any extra/deep path */
482 q = strchr_m(p, '\\');
483 if (q != NULL) {
484 *q = '\0';
485 q++;
486 *pp_extrapath = talloc_strdup(ctx, q);
487 } else {
488 *pp_extrapath = talloc_strdup(ctx, "");
490 if (*pp_extrapath == NULL) {
491 goto fail;
494 *pp_share = talloc_strdup(ctx, p);
495 if (*pp_share == NULL) {
496 goto fail;
499 *pp_server = talloc_strdup(ctx, &path[1]);
500 if (*pp_server == NULL) {
501 goto fail;
504 TALLOC_FREE(path);
505 return true;
507 fail:
508 TALLOC_FREE(*pp_share);
509 TALLOC_FREE(*pp_extrapath);
510 TALLOC_FREE(path);
511 return false;
514 /****************************************************************************
515 Return the original path truncated at the directory component before
516 the first wildcard character. Trust the caller to provide a NULL
517 terminated string
518 ****************************************************************************/
520 static char *clean_path(TALLOC_CTX *ctx, const char *path)
522 size_t len;
523 char *p1, *p2, *p;
524 char *path_out;
526 /* No absolute paths. */
527 while (IS_DIRECTORY_SEP(*path)) {
528 path++;
531 path_out = talloc_strdup(ctx, path);
532 if (!path_out) {
533 return NULL;
536 p1 = strchr_m(path_out, '*');
537 p2 = strchr_m(path_out, '?');
539 if (p1 || p2) {
540 if (p1 && p2) {
541 p = MIN(p1,p2);
542 } else if (!p1) {
543 p = p2;
544 } else {
545 p = p1;
547 *p = '\0';
549 /* Now go back to the start of this component. */
550 p1 = strrchr_m(path_out, '/');
551 p2 = strrchr_m(path_out, '\\');
552 p = MAX(p1,p2);
553 if (p) {
554 *p = '\0';
558 /* Strip any trailing separator */
560 len = strlen(path_out);
561 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
562 path_out[len-1] = '\0';
565 return path_out;
568 /****************************************************************************
569 ****************************************************************************/
571 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
572 struct cli_state *cli,
573 const char *dir)
575 char path_sep = '\\';
577 /* Ensure the extrapath doesn't start with a separator. */
578 while (IS_DIRECTORY_SEP(*dir)) {
579 dir++;
582 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
583 path_sep = '/';
585 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
586 path_sep,
587 smbXcli_conn_remote_name(cli->conn),
588 path_sep,
589 cli->share,
590 path_sep,
591 dir);
594 /********************************************************************
595 check for dfs referral
596 ********************************************************************/
598 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
599 NTSTATUS status)
601 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
603 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_UNICODE)) {
604 return false;
606 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_STATUS32)) {
607 return false;
609 if (NT_STATUS_EQUAL(status, expected)) {
610 return true;
612 return false;
615 /********************************************************************
616 Get the dfs referral link.
617 ********************************************************************/
619 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
620 struct cli_state *cli,
621 const char *path,
622 struct client_dfs_referral **refs,
623 size_t *num_refs,
624 size_t *consumed)
626 unsigned int data_len = 0;
627 unsigned int param_len = 0;
628 uint16_t setup[1];
629 uint16_t recv_flags2;
630 uint8_t *param = NULL;
631 uint8_t *rdata = NULL;
632 char *p;
633 char *endp;
634 smb_ucs2_t *path_ucs;
635 char *consumed_path = NULL;
636 uint16_t consumed_ucs;
637 uint16 num_referrals;
638 struct client_dfs_referral *referrals = NULL;
639 NTSTATUS status;
640 TALLOC_CTX *frame = talloc_stackframe();
642 *num_refs = 0;
643 *refs = NULL;
645 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
647 param = talloc_array(talloc_tos(), uint8_t, 2);
648 if (!param) {
649 status = NT_STATUS_NO_MEMORY;
650 goto out;
652 SSVAL(param, 0, 0x03); /* max referral level */
654 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
655 path, strlen(path)+1,
656 NULL);
657 if (!param) {
658 status = NT_STATUS_NO_MEMORY;
659 goto out;
661 param_len = talloc_get_size(param);
662 path_ucs = (smb_ucs2_t *)&param[2];
664 status = cli_trans(talloc_tos(), cli, SMBtrans2,
665 NULL, 0xffff, 0, 0,
666 setup, 1, 0,
667 param, param_len, 2,
668 NULL, 0, CLI_BUFFER_SIZE,
669 &recv_flags2,
670 NULL, 0, NULL, /* rsetup */
671 NULL, 0, NULL,
672 &rdata, 4, &data_len);
673 if (!NT_STATUS_IS_OK(status)) {
674 goto out;
677 endp = (char *)rdata + data_len;
679 consumed_ucs = SVAL(rdata, 0);
680 num_referrals = SVAL(rdata, 2);
682 /* consumed_ucs is the number of bytes
683 * of the UCS2 path consumed not counting any
684 * terminating null. We need to convert
685 * back to unix charset and count again
686 * to get the number of bytes consumed from
687 * the incoming path. */
689 errno = 0;
690 if (pull_string_talloc(talloc_tos(),
691 NULL,
693 &consumed_path,
694 path_ucs,
695 consumed_ucs,
696 STR_UNICODE) == 0) {
697 if (errno != 0) {
698 status = map_nt_error_from_unix(errno);
699 } else {
700 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
702 goto out;
704 if (consumed_path == NULL) {
705 status = map_nt_error_from_unix(errno);
706 goto out;
708 *consumed = strlen(consumed_path);
710 if (num_referrals != 0) {
711 uint16 ref_version;
712 uint16 ref_size;
713 int i;
714 uint16 node_offset;
716 referrals = talloc_array(ctx, struct client_dfs_referral,
717 num_referrals);
719 if (!referrals) {
720 status = NT_STATUS_NO_MEMORY;
721 goto out;
723 /* start at the referrals array */
725 p = (char *)rdata+8;
726 for (i=0; i<num_referrals && p < endp; i++) {
727 if (p + 18 > endp) {
728 goto out;
730 ref_version = SVAL(p, 0);
731 ref_size = SVAL(p, 2);
732 node_offset = SVAL(p, 16);
734 if (ref_version != 3) {
735 p += ref_size;
736 continue;
739 referrals[i].proximity = SVAL(p, 8);
740 referrals[i].ttl = SVAL(p, 10);
742 if (p + node_offset > endp) {
743 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
744 goto out;
746 clistr_pull_talloc(referrals,
747 (const char *)rdata,
748 recv_flags2,
749 &referrals[i].dfspath,
750 p+node_offset,
751 PTR_DIFF(endp, p+node_offset),
752 STR_TERMINATE|STR_UNICODE);
754 if (!referrals[i].dfspath) {
755 status = map_nt_error_from_unix(errno);
756 goto out;
758 p += ref_size;
760 if (i < num_referrals) {
761 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
762 goto out;
766 *num_refs = num_referrals;
767 *refs = referrals;
769 out:
771 TALLOC_FREE(frame);
772 return status;
775 /********************************************************************
776 ********************************************************************/
778 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
779 const char *mountpt,
780 const struct user_auth_info *dfs_auth_info,
781 struct cli_state *rootcli,
782 const char *path,
783 struct cli_state **targetcli,
784 char **pp_targetpath)
786 struct client_dfs_referral *refs = NULL;
787 size_t num_refs = 0;
788 size_t consumed = 0;
789 struct cli_state *cli_ipc = NULL;
790 char *dfs_path = NULL;
791 char *cleanpath = NULL;
792 char *extrapath = NULL;
793 int pathlen;
794 char *server = NULL;
795 char *share = NULL;
796 struct cli_state *newcli = NULL;
797 char *newpath = NULL;
798 char *newmount = NULL;
799 char *ppath = NULL;
800 SMB_STRUCT_STAT sbuf;
801 uint32 attributes;
802 NTSTATUS status;
804 if ( !rootcli || !path || !targetcli ) {
805 return NT_STATUS_INVALID_PARAMETER;
808 /* Don't do anything if this is not a DFS root. */
810 if ( !rootcli->dfsroot) {
811 *targetcli = rootcli;
812 *pp_targetpath = talloc_strdup(ctx, path);
813 if (!*pp_targetpath) {
814 return NT_STATUS_NO_MEMORY;
816 return NT_STATUS_OK;
819 *targetcli = NULL;
821 /* Send a trans2_query_path_info to check for a referral. */
823 cleanpath = clean_path(ctx, path);
824 if (!cleanpath) {
825 return NT_STATUS_NO_MEMORY;
828 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
829 if (!dfs_path) {
830 return NT_STATUS_NO_MEMORY;
833 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
834 if (NT_STATUS_IS_OK(status)) {
835 /* This is an ordinary path, just return it. */
836 *targetcli = rootcli;
837 *pp_targetpath = talloc_strdup(ctx, path);
838 if (!*pp_targetpath) {
839 return NT_STATUS_NO_MEMORY;
841 goto done;
844 /* Special case where client asked for a path that does not exist */
846 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
847 status)) {
848 *targetcli = rootcli;
849 *pp_targetpath = talloc_strdup(ctx, path);
850 if (!*pp_targetpath) {
851 return NT_STATUS_NO_MEMORY;
853 goto done;
856 /* We got an error, check for DFS referral. */
858 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
859 status)) {
860 return status;
863 /* Check for the referral. */
865 status = cli_cm_open(ctx,
866 rootcli,
867 smbXcli_conn_remote_name(rootcli->conn),
868 "IPC$",
869 dfs_auth_info,
870 false,
871 smb1cli_conn_encryption_on(rootcli->conn),
872 smbXcli_conn_protocol(rootcli->conn),
874 0x20,
875 &cli_ipc);
876 if (!NT_STATUS_IS_OK(status)) {
877 return status;
880 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
881 &num_refs, &consumed);
882 if (!NT_STATUS_IS_OK(status) || !num_refs) {
883 return status;
886 /* Just store the first referral for now. */
888 if (!refs[0].dfspath) {
889 return NT_STATUS_NOT_FOUND;
891 if (!split_dfs_path(ctx, refs[0].dfspath, &server, &share,
892 &extrapath)) {
893 return NT_STATUS_NOT_FOUND;
896 /* Make sure to recreate the original string including any wildcards. */
898 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
899 if (!dfs_path) {
900 return NT_STATUS_NO_MEMORY;
902 pathlen = strlen(dfs_path);
903 consumed = MIN(pathlen, consumed);
904 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
905 if (!*pp_targetpath) {
906 return NT_STATUS_NO_MEMORY;
908 dfs_path[consumed] = '\0';
911 * *pp_targetpath is now the unconsumed part of the path.
912 * dfs_path is now the consumed part of the path
913 * (in \server\share\path format).
916 /* Open the connection to the target server & share */
917 status = cli_cm_open(ctx, rootcli,
918 server,
919 share,
920 dfs_auth_info,
921 false,
922 smb1cli_conn_encryption_on(rootcli->conn),
923 smbXcli_conn_protocol(rootcli->conn),
925 0x20,
926 targetcli);
927 if (!NT_STATUS_IS_OK(status)) {
928 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
929 server, share );
930 return status;
933 if (extrapath && strlen(extrapath) > 0) {
934 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
935 /* put the trailing \ on the path, so to be save we put one in if needed */
936 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
937 *pp_targetpath = talloc_asprintf(ctx,
938 "%s\\%s",
939 extrapath,
940 *pp_targetpath);
941 } else {
942 *pp_targetpath = talloc_asprintf(ctx,
943 "%s%s",
944 extrapath,
945 *pp_targetpath);
947 if (!*pp_targetpath) {
948 return NT_STATUS_NO_MEMORY;
952 /* parse out the consumed mount path */
953 /* trim off the \server\share\ */
955 ppath = dfs_path;
957 if (*ppath != '\\') {
958 d_printf("cli_resolve_path: "
959 "dfs_path (%s) not in correct format.\n",
960 dfs_path );
961 return NT_STATUS_NOT_FOUND;
964 ppath++; /* Now pointing at start of server name. */
966 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
967 return NT_STATUS_NOT_FOUND;
970 ppath++; /* Now pointing at start of share name. */
972 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
973 return NT_STATUS_NOT_FOUND;
976 ppath++; /* Now pointing at path component. */
978 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
979 if (!newmount) {
980 return NT_STATUS_NOT_FOUND;
983 cli_set_mntpoint(*targetcli, newmount);
985 /* Check for another dfs referral, note that we are not
986 checking for loops here. */
988 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
989 status = cli_resolve_path(ctx,
990 newmount,
991 dfs_auth_info,
992 *targetcli,
993 *pp_targetpath,
994 &newcli,
995 &newpath);
996 if (NT_STATUS_IS_OK(status)) {
998 * When cli_resolve_path returns true here it's always
999 * returning the complete path in newpath, so we're done
1000 * here.
1002 *targetcli = newcli;
1003 *pp_targetpath = newpath;
1004 return status;
1008 done:
1010 /* If returning true ensure we return a dfs root full path. */
1011 if ((*targetcli)->dfsroot) {
1012 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1013 if (!dfs_path) {
1014 return NT_STATUS_NO_MEMORY;
1016 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1017 if (*pp_targetpath == NULL) {
1018 return NT_STATUS_NO_MEMORY;
1022 return NT_STATUS_OK;
1025 /********************************************************************
1026 ********************************************************************/
1028 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1029 struct cli_state *cli,
1030 const char *sharename,
1031 char **pp_newserver,
1032 char **pp_newshare,
1033 bool force_encrypt,
1034 const char *username,
1035 const char *password,
1036 const char *domain)
1038 struct client_dfs_referral *refs = NULL;
1039 size_t num_refs = 0;
1040 size_t consumed = 0;
1041 char *fullpath = NULL;
1042 bool res;
1043 uint16 cnum;
1044 char *newextrapath = NULL;
1045 NTSTATUS status;
1046 const char *remote_name;
1048 if (!cli || !sharename) {
1049 return false;
1052 remote_name = smbXcli_conn_remote_name(cli->conn);
1053 cnum = cli_state_get_tid(cli);
1055 /* special case. never check for a referral on the IPC$ share */
1057 if (strequal(sharename, "IPC$")) {
1058 return false;
1061 /* send a trans2_query_path_info to check for a referral */
1063 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1064 if (!fullpath) {
1065 return false;
1068 /* check for the referral */
1070 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL, 0))) {
1071 return false;
1074 if (force_encrypt) {
1075 status = cli_cm_force_encryption(cli,
1076 username,
1077 password,
1078 lp_workgroup(),
1079 "IPC$");
1080 if (!NT_STATUS_IS_OK(status)) {
1081 return false;
1085 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1086 &num_refs, &consumed);
1087 res = NT_STATUS_IS_OK(status);
1089 status = cli_tdis(cli);
1090 if (!NT_STATUS_IS_OK(status)) {
1091 return false;
1094 cli_state_set_tid(cli, cnum);
1096 if (!res || !num_refs) {
1097 return false;
1100 if (!refs[0].dfspath) {
1101 return false;
1104 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1105 pp_newshare, &newextrapath)) {
1106 return false;
1109 /* check that this is not a self-referral */
1111 if (strequal(remote_name, *pp_newserver) &&
1112 strequal(sharename, *pp_newshare)) {
1113 return false;
1116 return true;