s3:libsmb: Ensure we ask for DEFAULT_SMB2_MAX_CREDITS on successful negprot.
[Samba/wip.git] / source3 / libsmb / clidfs.c
blob1ed6ac5fc146191a5f9fc68d702803501de6888d
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 if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
164 /* Ensure we ask for some initial credits. */
165 smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
168 username = get_cmdline_auth_info_username(auth_info);
169 password = get_cmdline_auth_info_password(auth_info);
171 status = cli_session_setup(c, username,
172 password, strlen(password),
173 password, strlen(password),
174 lp_workgroup());
175 if (!NT_STATUS_IS_OK(status)) {
176 /* If a password was not supplied then
177 * try again with a null username. */
178 if (password[0] || !username[0] ||
179 get_cmdline_auth_info_use_kerberos(auth_info) ||
180 !NT_STATUS_IS_OK(status = cli_session_setup(c, "",
181 "", 0,
182 "", 0,
183 lp_workgroup()))) {
184 d_printf("session setup failed: %s\n",
185 nt_errstr(status));
186 if (NT_STATUS_EQUAL(status,
187 NT_STATUS_MORE_PROCESSING_REQUIRED))
188 d_printf("did you forget to run kinit?\n");
189 cli_shutdown(c);
190 return status;
192 d_printf("Anonymous login successful\n");
193 status = cli_init_creds(c, "", lp_workgroup(), "");
194 } else {
195 status = cli_init_creds(c, username, lp_workgroup(), password);
198 if (!NT_STATUS_IS_OK(status)) {
199 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
200 cli_shutdown(c);
201 return status;
204 if ( show_sessetup ) {
205 if (*c->server_domain) {
206 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
207 c->server_domain,c->server_os,c->server_type));
208 } else if (*c->server_os || *c->server_type) {
209 DEBUG(0,("OS=[%s] Server=[%s]\n",
210 c->server_os,c->server_type));
213 DEBUG(4,(" session setup ok\n"));
215 /* here's the fun part....to support 'msdfs proxy' shares
216 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
217 here before trying to connect to the original share.
218 cli_check_msdfs_proxy() will fail if it is a normal share. */
220 if ((smb1cli_conn_capabilities(c->conn) & CAP_DFS) &&
221 cli_check_msdfs_proxy(ctx, c, sharename,
222 &newserver, &newshare,
223 force_encrypt,
224 username,
225 password,
226 lp_workgroup())) {
227 cli_shutdown(c);
228 return do_connect(ctx, newserver,
229 newshare, auth_info, false,
230 force_encrypt, max_protocol,
231 port, name_type, pcli);
234 /* must be a normal share */
236 status = cli_tree_connect(c, sharename, "?????",
237 password, strlen(password)+1);
238 if (!NT_STATUS_IS_OK(status)) {
239 d_printf("tree connect failed: %s\n", nt_errstr(status));
240 cli_shutdown(c);
241 return status;
244 if (force_encrypt) {
245 status = cli_cm_force_encryption(c,
246 username,
247 password,
248 lp_workgroup(),
249 sharename);
250 if (!NT_STATUS_IS_OK(status)) {
251 cli_shutdown(c);
252 return status;
256 DEBUG(4,(" tconx ok\n"));
257 *pcli = c;
258 return NT_STATUS_OK;
261 /****************************************************************************
262 ****************************************************************************/
264 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
266 char *name = clean_name(NULL, mnt);
267 if (!name) {
268 return;
270 TALLOC_FREE(cli->dfs_mountpoint);
271 cli->dfs_mountpoint = talloc_strdup(cli, name);
272 TALLOC_FREE(name);
275 /********************************************************************
276 Add a new connection to the list.
277 referring_cli == NULL means a new initial connection.
278 ********************************************************************/
280 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
281 struct cli_state *referring_cli,
282 const char *server,
283 const char *share,
284 const struct user_auth_info *auth_info,
285 bool show_hdr,
286 bool force_encrypt,
287 int max_protocol,
288 int port,
289 int name_type,
290 struct cli_state **pcli)
292 struct cli_state *cli;
293 NTSTATUS status;
295 status = do_connect(ctx, server, share,
296 auth_info,
297 show_hdr, force_encrypt, max_protocol,
298 port, name_type, &cli);
300 if (!NT_STATUS_IS_OK(status)) {
301 return status;
304 /* Enter into the list. */
305 if (referring_cli) {
306 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
309 if (referring_cli && referring_cli->requested_posix_capabilities) {
310 uint16 major, minor;
311 uint32 caplow, caphigh;
312 status = cli_unix_extensions_version(cli, &major, &minor,
313 &caplow, &caphigh);
314 if (NT_STATUS_IS_OK(status)) {
315 cli_set_unix_extensions_capabilities(cli,
316 major, minor,
317 caplow, caphigh);
321 *pcli = cli;
322 return NT_STATUS_OK;
325 /********************************************************************
326 Return a connection to a server on a particular share.
327 ********************************************************************/
329 static struct cli_state *cli_cm_find(struct cli_state *cli,
330 const char *server,
331 const char *share)
333 struct cli_state *p;
335 if (cli == NULL) {
336 return NULL;
339 /* Search to the start of the list. */
340 for (p = cli; p; p = DLIST_PREV(p)) {
341 const char *remote_name =
342 smbXcli_conn_remote_name(p->conn);
344 if (strequal(server, remote_name) &&
345 strequal(share,p->share)) {
346 return p;
350 /* Search to the end of the list. */
351 for (p = cli->next; p; p = p->next) {
352 const char *remote_name =
353 smbXcli_conn_remote_name(p->conn);
355 if (strequal(server, remote_name) &&
356 strequal(share,p->share)) {
357 return p;
361 return NULL;
364 /****************************************************************************
365 Open a client connection to a \\server\share.
366 ****************************************************************************/
368 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
369 struct cli_state *referring_cli,
370 const char *server,
371 const char *share,
372 const struct user_auth_info *auth_info,
373 bool show_hdr,
374 bool force_encrypt,
375 int max_protocol,
376 int port,
377 int name_type,
378 struct cli_state **pcli)
380 /* Try to reuse an existing connection in this list. */
381 struct cli_state *c = cli_cm_find(referring_cli, server, share);
382 NTSTATUS status;
384 if (c) {
385 *pcli = c;
386 return NT_STATUS_OK;
389 if (auth_info == NULL) {
390 /* Can't do a new connection
391 * without auth info. */
392 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
393 "without auth info\n",
394 server, share );
395 return NT_STATUS_INVALID_PARAMETER;
398 status = cli_cm_connect(ctx,
399 referring_cli,
400 server,
401 share,
402 auth_info,
403 show_hdr,
404 force_encrypt,
405 max_protocol,
406 port,
407 name_type,
408 &c);
409 if (!NT_STATUS_IS_OK(status)) {
410 return status;
412 *pcli = c;
413 return NT_STATUS_OK;
416 /****************************************************************************
417 ****************************************************************************/
419 void cli_cm_display(struct cli_state *cli)
421 int i;
423 for (i=0; cli; cli = cli->next,i++ ) {
424 d_printf("%d:\tserver=%s, share=%s\n",
425 i, smbXcli_conn_remote_name(cli->conn), cli->share);
429 /****************************************************************************
430 ****************************************************************************/
432 /****************************************************************************
433 ****************************************************************************/
435 #if 0
436 void cli_cm_set_credentials(struct user_auth_info *auth_info)
438 SAFE_FREE(cm_creds.username);
439 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
440 auth_info));
442 if (get_cmdline_auth_info_got_pass(auth_info)) {
443 cm_set_password(get_cmdline_auth_info_password(auth_info));
446 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
447 cm_creds.fallback_after_kerberos = false;
448 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
450 #endif
452 /**********************************************************************
453 split a dfs path into the server, share name, and extrapath components
454 **********************************************************************/
456 static bool split_dfs_path(TALLOC_CTX *ctx,
457 const char *nodepath,
458 char **pp_server,
459 char **pp_share,
460 char **pp_extrapath)
462 char *p, *q;
463 char *path;
465 *pp_server = NULL;
466 *pp_share = NULL;
467 *pp_extrapath = NULL;
469 path = talloc_strdup(ctx, nodepath);
470 if (!path) {
471 goto fail;
474 if ( path[0] != '\\' ) {
475 goto fail;
478 p = strchr_m( path + 1, '\\' );
479 if ( !p ) {
480 goto fail;
483 *p = '\0';
484 p++;
486 /* Look for any extra/deep path */
487 q = strchr_m(p, '\\');
488 if (q != NULL) {
489 *q = '\0';
490 q++;
491 *pp_extrapath = talloc_strdup(ctx, q);
492 } else {
493 *pp_extrapath = talloc_strdup(ctx, "");
495 if (*pp_extrapath == NULL) {
496 goto fail;
499 *pp_share = talloc_strdup(ctx, p);
500 if (*pp_share == NULL) {
501 goto fail;
504 *pp_server = talloc_strdup(ctx, &path[1]);
505 if (*pp_server == NULL) {
506 goto fail;
509 TALLOC_FREE(path);
510 return true;
512 fail:
513 TALLOC_FREE(*pp_share);
514 TALLOC_FREE(*pp_extrapath);
515 TALLOC_FREE(path);
516 return false;
519 /****************************************************************************
520 Return the original path truncated at the directory component before
521 the first wildcard character. Trust the caller to provide a NULL
522 terminated string
523 ****************************************************************************/
525 static char *clean_path(TALLOC_CTX *ctx, const char *path)
527 size_t len;
528 char *p1, *p2, *p;
529 char *path_out;
531 /* No absolute paths. */
532 while (IS_DIRECTORY_SEP(*path)) {
533 path++;
536 path_out = talloc_strdup(ctx, path);
537 if (!path_out) {
538 return NULL;
541 p1 = strchr_m(path_out, '*');
542 p2 = strchr_m(path_out, '?');
544 if (p1 || p2) {
545 if (p1 && p2) {
546 p = MIN(p1,p2);
547 } else if (!p1) {
548 p = p2;
549 } else {
550 p = p1;
552 *p = '\0';
554 /* Now go back to the start of this component. */
555 p1 = strrchr_m(path_out, '/');
556 p2 = strrchr_m(path_out, '\\');
557 p = MAX(p1,p2);
558 if (p) {
559 *p = '\0';
563 /* Strip any trailing separator */
565 len = strlen(path_out);
566 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
567 path_out[len-1] = '\0';
570 return path_out;
573 /****************************************************************************
574 ****************************************************************************/
576 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
577 struct cli_state *cli,
578 const char *dir)
580 char path_sep = '\\';
582 /* Ensure the extrapath doesn't start with a separator. */
583 while (IS_DIRECTORY_SEP(*dir)) {
584 dir++;
587 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
588 path_sep = '/';
590 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
591 path_sep,
592 smbXcli_conn_remote_name(cli->conn),
593 path_sep,
594 cli->share,
595 path_sep,
596 dir);
599 /********************************************************************
600 check for dfs referral
601 ********************************************************************/
603 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
604 NTSTATUS status)
606 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
608 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_UNICODE)) {
609 return false;
611 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_STATUS32)) {
612 return false;
614 if (NT_STATUS_EQUAL(status, expected)) {
615 return true;
617 return false;
620 /********************************************************************
621 Get the dfs referral link.
622 ********************************************************************/
624 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
625 struct cli_state *cli,
626 const char *path,
627 struct client_dfs_referral **refs,
628 size_t *num_refs,
629 size_t *consumed)
631 unsigned int data_len = 0;
632 unsigned int param_len = 0;
633 uint16_t setup[1];
634 uint16_t recv_flags2;
635 uint8_t *param = NULL;
636 uint8_t *rdata = NULL;
637 char *p;
638 char *endp;
639 smb_ucs2_t *path_ucs;
640 char *consumed_path = NULL;
641 uint16_t consumed_ucs;
642 uint16 num_referrals;
643 struct client_dfs_referral *referrals = NULL;
644 NTSTATUS status;
645 TALLOC_CTX *frame = talloc_stackframe();
647 *num_refs = 0;
648 *refs = NULL;
650 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
652 param = talloc_array(talloc_tos(), uint8_t, 2);
653 if (!param) {
654 status = NT_STATUS_NO_MEMORY;
655 goto out;
657 SSVAL(param, 0, 0x03); /* max referral level */
659 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
660 path, strlen(path)+1,
661 NULL);
662 if (!param) {
663 status = NT_STATUS_NO_MEMORY;
664 goto out;
666 param_len = talloc_get_size(param);
667 path_ucs = (smb_ucs2_t *)&param[2];
669 status = cli_trans(talloc_tos(), cli, SMBtrans2,
670 NULL, 0xffff, 0, 0,
671 setup, 1, 0,
672 param, param_len, 2,
673 NULL, 0, CLI_BUFFER_SIZE,
674 &recv_flags2,
675 NULL, 0, NULL, /* rsetup */
676 NULL, 0, NULL,
677 &rdata, 4, &data_len);
678 if (!NT_STATUS_IS_OK(status)) {
679 goto out;
682 endp = (char *)rdata + data_len;
684 consumed_ucs = SVAL(rdata, 0);
685 num_referrals = SVAL(rdata, 2);
687 /* consumed_ucs is the number of bytes
688 * of the UCS2 path consumed not counting any
689 * terminating null. We need to convert
690 * back to unix charset and count again
691 * to get the number of bytes consumed from
692 * the incoming path. */
694 errno = 0;
695 if (pull_string_talloc(talloc_tos(),
696 NULL,
698 &consumed_path,
699 path_ucs,
700 consumed_ucs,
701 STR_UNICODE) == 0) {
702 if (errno != 0) {
703 status = map_nt_error_from_unix(errno);
704 } else {
705 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
707 goto out;
709 if (consumed_path == NULL) {
710 status = map_nt_error_from_unix(errno);
711 goto out;
713 *consumed = strlen(consumed_path);
715 if (num_referrals != 0) {
716 uint16 ref_version;
717 uint16 ref_size;
718 int i;
719 uint16 node_offset;
721 referrals = talloc_array(ctx, struct client_dfs_referral,
722 num_referrals);
724 if (!referrals) {
725 status = NT_STATUS_NO_MEMORY;
726 goto out;
728 /* start at the referrals array */
730 p = (char *)rdata+8;
731 for (i=0; i<num_referrals && p < endp; i++) {
732 if (p + 18 > endp) {
733 goto out;
735 ref_version = SVAL(p, 0);
736 ref_size = SVAL(p, 2);
737 node_offset = SVAL(p, 16);
739 if (ref_version != 3) {
740 p += ref_size;
741 continue;
744 referrals[i].proximity = SVAL(p, 8);
745 referrals[i].ttl = SVAL(p, 10);
747 if (p + node_offset > endp) {
748 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
749 goto out;
751 clistr_pull_talloc(referrals,
752 (const char *)rdata,
753 recv_flags2,
754 &referrals[i].dfspath,
755 p+node_offset,
756 PTR_DIFF(endp, p+node_offset),
757 STR_TERMINATE|STR_UNICODE);
759 if (!referrals[i].dfspath) {
760 status = map_nt_error_from_unix(errno);
761 goto out;
763 p += ref_size;
765 if (i < num_referrals) {
766 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
767 goto out;
771 *num_refs = num_referrals;
772 *refs = referrals;
774 out:
776 TALLOC_FREE(frame);
777 return status;
780 /********************************************************************
781 ********************************************************************/
783 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
784 const char *mountpt,
785 const struct user_auth_info *dfs_auth_info,
786 struct cli_state *rootcli,
787 const char *path,
788 struct cli_state **targetcli,
789 char **pp_targetpath)
791 struct client_dfs_referral *refs = NULL;
792 size_t num_refs = 0;
793 size_t consumed = 0;
794 struct cli_state *cli_ipc = NULL;
795 char *dfs_path = NULL;
796 char *cleanpath = NULL;
797 char *extrapath = NULL;
798 int pathlen;
799 char *server = NULL;
800 char *share = NULL;
801 struct cli_state *newcli = NULL;
802 char *newpath = NULL;
803 char *newmount = NULL;
804 char *ppath = NULL;
805 SMB_STRUCT_STAT sbuf;
806 uint32 attributes;
807 NTSTATUS status;
809 if ( !rootcli || !path || !targetcli ) {
810 return NT_STATUS_INVALID_PARAMETER;
813 /* Don't do anything if this is not a DFS root. */
815 if ( !rootcli->dfsroot) {
816 *targetcli = rootcli;
817 *pp_targetpath = talloc_strdup(ctx, path);
818 if (!*pp_targetpath) {
819 return NT_STATUS_NO_MEMORY;
821 return NT_STATUS_OK;
824 *targetcli = NULL;
826 /* Send a trans2_query_path_info to check for a referral. */
828 cleanpath = clean_path(ctx, path);
829 if (!cleanpath) {
830 return NT_STATUS_NO_MEMORY;
833 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
834 if (!dfs_path) {
835 return NT_STATUS_NO_MEMORY;
838 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
839 if (NT_STATUS_IS_OK(status)) {
840 /* This is an ordinary path, just return it. */
841 *targetcli = rootcli;
842 *pp_targetpath = talloc_strdup(ctx, path);
843 if (!*pp_targetpath) {
844 return NT_STATUS_NO_MEMORY;
846 goto done;
849 /* Special case where client asked for a path that does not exist */
851 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
852 status)) {
853 *targetcli = rootcli;
854 *pp_targetpath = talloc_strdup(ctx, path);
855 if (!*pp_targetpath) {
856 return NT_STATUS_NO_MEMORY;
858 goto done;
861 /* We got an error, check for DFS referral. */
863 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
864 status)) {
865 return status;
868 /* Check for the referral. */
870 status = cli_cm_open(ctx,
871 rootcli,
872 smbXcli_conn_remote_name(rootcli->conn),
873 "IPC$",
874 dfs_auth_info,
875 false,
876 smb1cli_conn_encryption_on(rootcli->conn),
877 smbXcli_conn_protocol(rootcli->conn),
879 0x20,
880 &cli_ipc);
881 if (!NT_STATUS_IS_OK(status)) {
882 return status;
885 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
886 &num_refs, &consumed);
887 if (!NT_STATUS_IS_OK(status) || !num_refs) {
888 return status;
891 /* Just store the first referral for now. */
893 if (!refs[0].dfspath) {
894 return NT_STATUS_NOT_FOUND;
896 if (!split_dfs_path(ctx, refs[0].dfspath, &server, &share,
897 &extrapath)) {
898 return NT_STATUS_NOT_FOUND;
901 /* Make sure to recreate the original string including any wildcards. */
903 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
904 if (!dfs_path) {
905 return NT_STATUS_NO_MEMORY;
907 pathlen = strlen(dfs_path);
908 consumed = MIN(pathlen, consumed);
909 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
910 if (!*pp_targetpath) {
911 return NT_STATUS_NO_MEMORY;
913 dfs_path[consumed] = '\0';
916 * *pp_targetpath is now the unconsumed part of the path.
917 * dfs_path is now the consumed part of the path
918 * (in \server\share\path format).
921 /* Open the connection to the target server & share */
922 status = cli_cm_open(ctx, rootcli,
923 server,
924 share,
925 dfs_auth_info,
926 false,
927 smb1cli_conn_encryption_on(rootcli->conn),
928 smbXcli_conn_protocol(rootcli->conn),
930 0x20,
931 targetcli);
932 if (!NT_STATUS_IS_OK(status)) {
933 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
934 server, share );
935 return status;
938 if (extrapath && strlen(extrapath) > 0) {
939 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
940 /* put the trailing \ on the path, so to be save we put one in if needed */
941 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
942 *pp_targetpath = talloc_asprintf(ctx,
943 "%s\\%s",
944 extrapath,
945 *pp_targetpath);
946 } else {
947 *pp_targetpath = talloc_asprintf(ctx,
948 "%s%s",
949 extrapath,
950 *pp_targetpath);
952 if (!*pp_targetpath) {
953 return NT_STATUS_NO_MEMORY;
957 /* parse out the consumed mount path */
958 /* trim off the \server\share\ */
960 ppath = dfs_path;
962 if (*ppath != '\\') {
963 d_printf("cli_resolve_path: "
964 "dfs_path (%s) not in correct format.\n",
965 dfs_path );
966 return NT_STATUS_NOT_FOUND;
969 ppath++; /* Now pointing at start of server name. */
971 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
972 return NT_STATUS_NOT_FOUND;
975 ppath++; /* Now pointing at start of share name. */
977 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
978 return NT_STATUS_NOT_FOUND;
981 ppath++; /* Now pointing at path component. */
983 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
984 if (!newmount) {
985 return NT_STATUS_NOT_FOUND;
988 cli_set_mntpoint(*targetcli, newmount);
990 /* Check for another dfs referral, note that we are not
991 checking for loops here. */
993 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
994 status = cli_resolve_path(ctx,
995 newmount,
996 dfs_auth_info,
997 *targetcli,
998 *pp_targetpath,
999 &newcli,
1000 &newpath);
1001 if (NT_STATUS_IS_OK(status)) {
1003 * When cli_resolve_path returns true here it's always
1004 * returning the complete path in newpath, so we're done
1005 * here.
1007 *targetcli = newcli;
1008 *pp_targetpath = newpath;
1009 return status;
1013 done:
1015 /* If returning true ensure we return a dfs root full path. */
1016 if ((*targetcli)->dfsroot) {
1017 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1018 if (!dfs_path) {
1019 return NT_STATUS_NO_MEMORY;
1021 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1022 if (*pp_targetpath == NULL) {
1023 return NT_STATUS_NO_MEMORY;
1027 return NT_STATUS_OK;
1030 /********************************************************************
1031 ********************************************************************/
1033 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1034 struct cli_state *cli,
1035 const char *sharename,
1036 char **pp_newserver,
1037 char **pp_newshare,
1038 bool force_encrypt,
1039 const char *username,
1040 const char *password,
1041 const char *domain)
1043 struct client_dfs_referral *refs = NULL;
1044 size_t num_refs = 0;
1045 size_t consumed = 0;
1046 char *fullpath = NULL;
1047 bool res;
1048 uint16 cnum;
1049 char *newextrapath = NULL;
1050 NTSTATUS status;
1051 const char *remote_name;
1053 if (!cli || !sharename) {
1054 return false;
1057 remote_name = smbXcli_conn_remote_name(cli->conn);
1058 cnum = cli_state_get_tid(cli);
1060 /* special case. never check for a referral on the IPC$ share */
1062 if (strequal(sharename, "IPC$")) {
1063 return false;
1066 /* send a trans2_query_path_info to check for a referral */
1068 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1069 if (!fullpath) {
1070 return false;
1073 /* check for the referral */
1075 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL, 0))) {
1076 return false;
1079 if (force_encrypt) {
1080 status = cli_cm_force_encryption(cli,
1081 username,
1082 password,
1083 lp_workgroup(),
1084 "IPC$");
1085 if (!NT_STATUS_IS_OK(status)) {
1086 return false;
1090 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1091 &num_refs, &consumed);
1092 res = NT_STATUS_IS_OK(status);
1094 status = cli_tdis(cli);
1095 if (!NT_STATUS_IS_OK(status)) {
1096 return false;
1099 cli_state_set_tid(cli, cnum);
1101 if (!res || !num_refs) {
1102 return false;
1105 if (!refs[0].dfspath) {
1106 return false;
1109 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1110 pp_newshare, &newextrapath)) {
1111 return false;
1114 /* check that this is not a self-referral */
1116 if (strequal(remote_name, *pp_newserver) &&
1117 strequal(sharename, *pp_newshare)) {
1118 return false;
1121 return true;