s3:libsmb: use lp_cli_minprotocol() in do_connect()
[Samba.git] / source3 / libsmb / clidfs.c
blob1d92843f48fec3e6e5827324d126eacf47c403d9
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,
154 lp_cli_minprotocol(),
155 max_protocol);
157 if (!NT_STATUS_IS_OK(status)) {
158 d_printf("protocol negotiation failed: %s\n",
159 nt_errstr(status));
160 cli_shutdown(c);
161 return status;
164 if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
165 /* Ensure we ask for some initial credits. */
166 smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
169 username = get_cmdline_auth_info_username(auth_info);
170 password = get_cmdline_auth_info_password(auth_info);
172 status = cli_session_setup(c, username,
173 password, strlen(password),
174 password, strlen(password),
175 lp_workgroup());
176 if (!NT_STATUS_IS_OK(status)) {
177 /* If a password was not supplied then
178 * try again with a null username. */
179 if (password[0] || !username[0] ||
180 get_cmdline_auth_info_use_kerberos(auth_info) ||
181 !NT_STATUS_IS_OK(status = cli_session_setup(c, "",
182 "", 0,
183 "", 0,
184 lp_workgroup()))) {
185 d_printf("session setup failed: %s\n",
186 nt_errstr(status));
187 if (NT_STATUS_EQUAL(status,
188 NT_STATUS_MORE_PROCESSING_REQUIRED))
189 d_printf("did you forget to run kinit?\n");
190 cli_shutdown(c);
191 return status;
193 d_printf("Anonymous login successful\n");
194 status = cli_init_creds(c, "", lp_workgroup(), "");
195 } else {
196 status = cli_init_creds(c, username, lp_workgroup(), password);
199 if (!NT_STATUS_IS_OK(status)) {
200 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
201 cli_shutdown(c);
202 return status;
205 if ( show_sessetup ) {
206 if (*c->server_domain) {
207 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
208 c->server_domain,c->server_os,c->server_type));
209 } else if (*c->server_os || *c->server_type) {
210 DEBUG(0,("OS=[%s] Server=[%s]\n",
211 c->server_os,c->server_type));
214 DEBUG(4,(" session setup ok\n"));
216 /* here's the fun part....to support 'msdfs proxy' shares
217 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
218 here before trying to connect to the original share.
219 cli_check_msdfs_proxy() will fail if it is a normal share. */
221 if ((smb1cli_conn_capabilities(c->conn) & CAP_DFS) &&
222 cli_check_msdfs_proxy(ctx, c, sharename,
223 &newserver, &newshare,
224 force_encrypt,
225 username,
226 password,
227 lp_workgroup())) {
228 cli_shutdown(c);
229 return do_connect(ctx, newserver,
230 newshare, auth_info, false,
231 force_encrypt, max_protocol,
232 port, name_type, pcli);
235 /* must be a normal share */
237 status = cli_tree_connect(c, sharename, "?????",
238 password, strlen(password)+1);
239 if (!NT_STATUS_IS_OK(status)) {
240 d_printf("tree connect failed: %s\n", nt_errstr(status));
241 cli_shutdown(c);
242 return status;
245 if (force_encrypt) {
246 status = cli_cm_force_encryption(c,
247 username,
248 password,
249 lp_workgroup(),
250 sharename);
251 if (!NT_STATUS_IS_OK(status)) {
252 cli_shutdown(c);
253 return status;
257 DEBUG(4,(" tconx ok\n"));
258 *pcli = c;
259 return NT_STATUS_OK;
262 /****************************************************************************
263 ****************************************************************************/
265 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
267 char *name = clean_name(NULL, mnt);
268 if (!name) {
269 return;
271 TALLOC_FREE(cli->dfs_mountpoint);
272 cli->dfs_mountpoint = talloc_strdup(cli, name);
273 TALLOC_FREE(name);
276 /********************************************************************
277 Add a new connection to the list.
278 referring_cli == NULL means a new initial connection.
279 ********************************************************************/
281 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
282 struct cli_state *referring_cli,
283 const char *server,
284 const char *share,
285 const struct user_auth_info *auth_info,
286 bool show_hdr,
287 bool force_encrypt,
288 int max_protocol,
289 int port,
290 int name_type,
291 struct cli_state **pcli)
293 struct cli_state *cli;
294 NTSTATUS status;
296 status = do_connect(ctx, server, share,
297 auth_info,
298 show_hdr, force_encrypt, max_protocol,
299 port, name_type, &cli);
301 if (!NT_STATUS_IS_OK(status)) {
302 return status;
305 /* Enter into the list. */
306 if (referring_cli) {
307 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
310 if (referring_cli && referring_cli->requested_posix_capabilities) {
311 uint16 major, minor;
312 uint32 caplow, caphigh;
313 status = cli_unix_extensions_version(cli, &major, &minor,
314 &caplow, &caphigh);
315 if (NT_STATUS_IS_OK(status)) {
316 cli_set_unix_extensions_capabilities(cli,
317 major, minor,
318 caplow, caphigh);
322 *pcli = cli;
323 return NT_STATUS_OK;
326 /********************************************************************
327 Return a connection to a server on a particular share.
328 ********************************************************************/
330 static struct cli_state *cli_cm_find(struct cli_state *cli,
331 const char *server,
332 const char *share)
334 struct cli_state *p;
336 if (cli == NULL) {
337 return NULL;
340 /* Search to the start of the list. */
341 for (p = cli; p; p = DLIST_PREV(p)) {
342 const char *remote_name =
343 smbXcli_conn_remote_name(p->conn);
345 if (strequal(server, remote_name) &&
346 strequal(share,p->share)) {
347 return p;
351 /* Search to the end of the list. */
352 for (p = cli->next; p; p = p->next) {
353 const char *remote_name =
354 smbXcli_conn_remote_name(p->conn);
356 if (strequal(server, remote_name) &&
357 strequal(share,p->share)) {
358 return p;
362 return NULL;
365 /****************************************************************************
366 Open a client connection to a \\server\share.
367 ****************************************************************************/
369 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
370 struct cli_state *referring_cli,
371 const char *server,
372 const char *share,
373 const struct user_auth_info *auth_info,
374 bool show_hdr,
375 bool force_encrypt,
376 int max_protocol,
377 int port,
378 int name_type,
379 struct cli_state **pcli)
381 /* Try to reuse an existing connection in this list. */
382 struct cli_state *c = cli_cm_find(referring_cli, server, share);
383 NTSTATUS status;
385 if (c) {
386 *pcli = c;
387 return NT_STATUS_OK;
390 if (auth_info == NULL) {
391 /* Can't do a new connection
392 * without auth info. */
393 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
394 "without auth info\n",
395 server, share );
396 return NT_STATUS_INVALID_PARAMETER;
399 status = cli_cm_connect(ctx,
400 referring_cli,
401 server,
402 share,
403 auth_info,
404 show_hdr,
405 force_encrypt,
406 max_protocol,
407 port,
408 name_type,
409 &c);
410 if (!NT_STATUS_IS_OK(status)) {
411 return status;
413 *pcli = c;
414 return NT_STATUS_OK;
417 /****************************************************************************
418 ****************************************************************************/
420 void cli_cm_display(struct cli_state *cli)
422 int i;
424 for (i=0; cli; cli = cli->next,i++ ) {
425 d_printf("%d:\tserver=%s, share=%s\n",
426 i, smbXcli_conn_remote_name(cli->conn), cli->share);
430 /****************************************************************************
431 ****************************************************************************/
433 /****************************************************************************
434 ****************************************************************************/
436 #if 0
437 void cli_cm_set_credentials(struct user_auth_info *auth_info)
439 SAFE_FREE(cm_creds.username);
440 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
441 auth_info));
443 if (get_cmdline_auth_info_got_pass(auth_info)) {
444 cm_set_password(get_cmdline_auth_info_password(auth_info));
447 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
448 cm_creds.fallback_after_kerberos = false;
449 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
451 #endif
453 /**********************************************************************
454 split a dfs path into the server, share name, and extrapath components
455 **********************************************************************/
457 static bool split_dfs_path(TALLOC_CTX *ctx,
458 const char *nodepath,
459 char **pp_server,
460 char **pp_share,
461 char **pp_extrapath)
463 char *p, *q;
464 char *path;
466 *pp_server = NULL;
467 *pp_share = NULL;
468 *pp_extrapath = NULL;
470 path = talloc_strdup(ctx, nodepath);
471 if (!path) {
472 goto fail;
475 if ( path[0] != '\\' ) {
476 goto fail;
479 p = strchr_m( path + 1, '\\' );
480 if ( !p ) {
481 goto fail;
484 *p = '\0';
485 p++;
487 /* Look for any extra/deep path */
488 q = strchr_m(p, '\\');
489 if (q != NULL) {
490 *q = '\0';
491 q++;
492 *pp_extrapath = talloc_strdup(ctx, q);
493 } else {
494 *pp_extrapath = talloc_strdup(ctx, "");
496 if (*pp_extrapath == NULL) {
497 goto fail;
500 *pp_share = talloc_strdup(ctx, p);
501 if (*pp_share == NULL) {
502 goto fail;
505 *pp_server = talloc_strdup(ctx, &path[1]);
506 if (*pp_server == NULL) {
507 goto fail;
510 TALLOC_FREE(path);
511 return true;
513 fail:
514 TALLOC_FREE(*pp_share);
515 TALLOC_FREE(*pp_extrapath);
516 TALLOC_FREE(path);
517 return false;
520 /****************************************************************************
521 Return the original path truncated at the directory component before
522 the first wildcard character. Trust the caller to provide a NULL
523 terminated string
524 ****************************************************************************/
526 static char *clean_path(TALLOC_CTX *ctx, const char *path)
528 size_t len;
529 char *p1, *p2, *p;
530 char *path_out;
532 /* No absolute paths. */
533 while (IS_DIRECTORY_SEP(*path)) {
534 path++;
537 path_out = talloc_strdup(ctx, path);
538 if (!path_out) {
539 return NULL;
542 p1 = strchr_m(path_out, '*');
543 p2 = strchr_m(path_out, '?');
545 if (p1 || p2) {
546 if (p1 && p2) {
547 p = MIN(p1,p2);
548 } else if (!p1) {
549 p = p2;
550 } else {
551 p = p1;
553 *p = '\0';
555 /* Now go back to the start of this component. */
556 p1 = strrchr_m(path_out, '/');
557 p2 = strrchr_m(path_out, '\\');
558 p = MAX(p1,p2);
559 if (p) {
560 *p = '\0';
564 /* Strip any trailing separator */
566 len = strlen(path_out);
567 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
568 path_out[len-1] = '\0';
571 return path_out;
574 /****************************************************************************
575 ****************************************************************************/
577 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
578 struct cli_state *cli,
579 const char *dir)
581 char path_sep = '\\';
583 /* Ensure the extrapath doesn't start with a separator. */
584 while (IS_DIRECTORY_SEP(*dir)) {
585 dir++;
588 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
589 path_sep = '/';
591 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
592 path_sep,
593 smbXcli_conn_remote_name(cli->conn),
594 path_sep,
595 cli->share,
596 path_sep,
597 dir);
600 /********************************************************************
601 check for dfs referral
602 ********************************************************************/
604 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
605 NTSTATUS status)
607 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
609 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_UNICODE)) {
610 return false;
612 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_STATUS32)) {
613 return false;
615 if (NT_STATUS_EQUAL(status, expected)) {
616 return true;
618 return false;
621 /********************************************************************
622 Get the dfs referral link.
623 ********************************************************************/
625 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
626 struct cli_state *cli,
627 const char *path,
628 struct client_dfs_referral **refs,
629 size_t *num_refs,
630 size_t *consumed)
632 unsigned int data_len = 0;
633 unsigned int param_len = 0;
634 uint16_t setup[1];
635 uint16_t recv_flags2;
636 uint8_t *param = NULL;
637 uint8_t *rdata = NULL;
638 char *p;
639 char *endp;
640 smb_ucs2_t *path_ucs;
641 char *consumed_path = NULL;
642 uint16_t consumed_ucs;
643 uint16 num_referrals;
644 struct client_dfs_referral *referrals = NULL;
645 NTSTATUS status;
646 TALLOC_CTX *frame = talloc_stackframe();
648 *num_refs = 0;
649 *refs = NULL;
651 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
653 param = talloc_array(talloc_tos(), uint8_t, 2);
654 if (!param) {
655 status = NT_STATUS_NO_MEMORY;
656 goto out;
658 SSVAL(param, 0, 0x03); /* max referral level */
660 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
661 path, strlen(path)+1,
662 NULL);
663 if (!param) {
664 status = NT_STATUS_NO_MEMORY;
665 goto out;
667 param_len = talloc_get_size(param);
668 path_ucs = (smb_ucs2_t *)&param[2];
670 status = cli_trans(talloc_tos(), cli, SMBtrans2,
671 NULL, 0xffff, 0, 0,
672 setup, 1, 0,
673 param, param_len, 2,
674 NULL, 0, CLI_BUFFER_SIZE,
675 &recv_flags2,
676 NULL, 0, NULL, /* rsetup */
677 NULL, 0, NULL,
678 &rdata, 4, &data_len);
679 if (!NT_STATUS_IS_OK(status)) {
680 goto out;
683 endp = (char *)rdata + data_len;
685 consumed_ucs = SVAL(rdata, 0);
686 num_referrals = SVAL(rdata, 2);
688 /* consumed_ucs is the number of bytes
689 * of the UCS2 path consumed not counting any
690 * terminating null. We need to convert
691 * back to unix charset and count again
692 * to get the number of bytes consumed from
693 * the incoming path. */
695 errno = 0;
696 if (pull_string_talloc(talloc_tos(),
697 NULL,
699 &consumed_path,
700 path_ucs,
701 consumed_ucs,
702 STR_UNICODE) == 0) {
703 if (errno != 0) {
704 status = map_nt_error_from_unix(errno);
705 } else {
706 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
708 goto out;
710 if (consumed_path == NULL) {
711 status = map_nt_error_from_unix(errno);
712 goto out;
714 *consumed = strlen(consumed_path);
716 if (num_referrals != 0) {
717 uint16 ref_version;
718 uint16 ref_size;
719 int i;
720 uint16 node_offset;
722 referrals = talloc_array(ctx, struct client_dfs_referral,
723 num_referrals);
725 if (!referrals) {
726 status = NT_STATUS_NO_MEMORY;
727 goto out;
729 /* start at the referrals array */
731 p = (char *)rdata+8;
732 for (i=0; i<num_referrals && p < endp; i++) {
733 if (p + 18 > endp) {
734 goto out;
736 ref_version = SVAL(p, 0);
737 ref_size = SVAL(p, 2);
738 node_offset = SVAL(p, 16);
740 if (ref_version != 3) {
741 p += ref_size;
742 continue;
745 referrals[i].proximity = SVAL(p, 8);
746 referrals[i].ttl = SVAL(p, 10);
748 if (p + node_offset > endp) {
749 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
750 goto out;
752 clistr_pull_talloc(referrals,
753 (const char *)rdata,
754 recv_flags2,
755 &referrals[i].dfspath,
756 p+node_offset,
757 PTR_DIFF(endp, p+node_offset),
758 STR_TERMINATE|STR_UNICODE);
760 if (!referrals[i].dfspath) {
761 status = map_nt_error_from_unix(errno);
762 goto out;
764 p += ref_size;
766 if (i < num_referrals) {
767 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
768 goto out;
772 *num_refs = num_referrals;
773 *refs = referrals;
775 out:
777 TALLOC_FREE(frame);
778 return status;
781 /********************************************************************
782 ********************************************************************/
784 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
785 const char *mountpt,
786 const struct user_auth_info *dfs_auth_info,
787 struct cli_state *rootcli,
788 const char *path,
789 struct cli_state **targetcli,
790 char **pp_targetpath)
792 struct client_dfs_referral *refs = NULL;
793 size_t num_refs = 0;
794 size_t consumed = 0;
795 struct cli_state *cli_ipc = NULL;
796 char *dfs_path = NULL;
797 char *cleanpath = NULL;
798 char *extrapath = NULL;
799 int pathlen;
800 char *server = NULL;
801 char *share = NULL;
802 struct cli_state *newcli = NULL;
803 char *newpath = NULL;
804 char *newmount = NULL;
805 char *ppath = NULL;
806 SMB_STRUCT_STAT sbuf;
807 uint32 attributes;
808 NTSTATUS status;
810 if ( !rootcli || !path || !targetcli ) {
811 return NT_STATUS_INVALID_PARAMETER;
814 /* Don't do anything if this is not a DFS root. */
816 if ( !rootcli->dfsroot) {
817 *targetcli = rootcli;
818 *pp_targetpath = talloc_strdup(ctx, path);
819 if (!*pp_targetpath) {
820 return NT_STATUS_NO_MEMORY;
822 return NT_STATUS_OK;
825 *targetcli = NULL;
827 /* Send a trans2_query_path_info to check for a referral. */
829 cleanpath = clean_path(ctx, path);
830 if (!cleanpath) {
831 return NT_STATUS_NO_MEMORY;
834 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
835 if (!dfs_path) {
836 return NT_STATUS_NO_MEMORY;
839 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
840 if (NT_STATUS_IS_OK(status)) {
841 /* This is an ordinary path, just return it. */
842 *targetcli = rootcli;
843 *pp_targetpath = talloc_strdup(ctx, path);
844 if (!*pp_targetpath) {
845 return NT_STATUS_NO_MEMORY;
847 goto done;
850 /* Special case where client asked for a path that does not exist */
852 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
853 status)) {
854 *targetcli = rootcli;
855 *pp_targetpath = talloc_strdup(ctx, path);
856 if (!*pp_targetpath) {
857 return NT_STATUS_NO_MEMORY;
859 goto done;
862 /* We got an error, check for DFS referral. */
864 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
865 status)) {
866 return status;
869 /* Check for the referral. */
871 status = cli_cm_open(ctx,
872 rootcli,
873 smbXcli_conn_remote_name(rootcli->conn),
874 "IPC$",
875 dfs_auth_info,
876 false,
877 smb1cli_conn_encryption_on(rootcli->conn),
878 smbXcli_conn_protocol(rootcli->conn),
880 0x20,
881 &cli_ipc);
882 if (!NT_STATUS_IS_OK(status)) {
883 return status;
886 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
887 &num_refs, &consumed);
888 if (!NT_STATUS_IS_OK(status) || !num_refs) {
889 return status;
892 /* Just store the first referral for now. */
894 if (!refs[0].dfspath) {
895 return NT_STATUS_NOT_FOUND;
897 if (!split_dfs_path(ctx, refs[0].dfspath, &server, &share,
898 &extrapath)) {
899 return NT_STATUS_NOT_FOUND;
902 /* Make sure to recreate the original string including any wildcards. */
904 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
905 if (!dfs_path) {
906 return NT_STATUS_NO_MEMORY;
908 pathlen = strlen(dfs_path);
909 consumed = MIN(pathlen, consumed);
910 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
911 if (!*pp_targetpath) {
912 return NT_STATUS_NO_MEMORY;
914 dfs_path[consumed] = '\0';
917 * *pp_targetpath is now the unconsumed part of the path.
918 * dfs_path is now the consumed part of the path
919 * (in \server\share\path format).
922 /* Open the connection to the target server & share */
923 status = cli_cm_open(ctx, rootcli,
924 server,
925 share,
926 dfs_auth_info,
927 false,
928 smb1cli_conn_encryption_on(rootcli->conn),
929 smbXcli_conn_protocol(rootcli->conn),
931 0x20,
932 targetcli);
933 if (!NT_STATUS_IS_OK(status)) {
934 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
935 server, share );
936 return status;
939 if (extrapath && strlen(extrapath) > 0) {
940 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
941 /* put the trailing \ on the path, so to be save we put one in if needed */
942 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
943 *pp_targetpath = talloc_asprintf(ctx,
944 "%s\\%s",
945 extrapath,
946 *pp_targetpath);
947 } else {
948 *pp_targetpath = talloc_asprintf(ctx,
949 "%s%s",
950 extrapath,
951 *pp_targetpath);
953 if (!*pp_targetpath) {
954 return NT_STATUS_NO_MEMORY;
958 /* parse out the consumed mount path */
959 /* trim off the \server\share\ */
961 ppath = dfs_path;
963 if (*ppath != '\\') {
964 d_printf("cli_resolve_path: "
965 "dfs_path (%s) not in correct format.\n",
966 dfs_path );
967 return NT_STATUS_NOT_FOUND;
970 ppath++; /* Now pointing at start of server name. */
972 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
973 return NT_STATUS_NOT_FOUND;
976 ppath++; /* Now pointing at start of share name. */
978 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
979 return NT_STATUS_NOT_FOUND;
982 ppath++; /* Now pointing at path component. */
984 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
985 if (!newmount) {
986 return NT_STATUS_NOT_FOUND;
989 cli_set_mntpoint(*targetcli, newmount);
991 /* Check for another dfs referral, note that we are not
992 checking for loops here. */
994 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
995 status = cli_resolve_path(ctx,
996 newmount,
997 dfs_auth_info,
998 *targetcli,
999 *pp_targetpath,
1000 &newcli,
1001 &newpath);
1002 if (NT_STATUS_IS_OK(status)) {
1004 * When cli_resolve_path returns true here it's always
1005 * returning the complete path in newpath, so we're done
1006 * here.
1008 *targetcli = newcli;
1009 *pp_targetpath = newpath;
1010 return status;
1014 done:
1016 /* If returning true ensure we return a dfs root full path. */
1017 if ((*targetcli)->dfsroot) {
1018 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1019 if (!dfs_path) {
1020 return NT_STATUS_NO_MEMORY;
1022 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1023 if (*pp_targetpath == NULL) {
1024 return NT_STATUS_NO_MEMORY;
1028 return NT_STATUS_OK;
1031 /********************************************************************
1032 ********************************************************************/
1034 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1035 struct cli_state *cli,
1036 const char *sharename,
1037 char **pp_newserver,
1038 char **pp_newshare,
1039 bool force_encrypt,
1040 const char *username,
1041 const char *password,
1042 const char *domain)
1044 struct client_dfs_referral *refs = NULL;
1045 size_t num_refs = 0;
1046 size_t consumed = 0;
1047 char *fullpath = NULL;
1048 bool res;
1049 uint16 cnum;
1050 char *newextrapath = NULL;
1051 NTSTATUS status;
1052 const char *remote_name;
1054 if (!cli || !sharename) {
1055 return false;
1058 remote_name = smbXcli_conn_remote_name(cli->conn);
1059 cnum = cli_state_get_tid(cli);
1061 /* special case. never check for a referral on the IPC$ share */
1063 if (strequal(sharename, "IPC$")) {
1064 return false;
1067 /* send a trans2_query_path_info to check for a referral */
1069 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1070 if (!fullpath) {
1071 return false;
1074 /* check for the referral */
1076 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL, 0))) {
1077 return false;
1080 if (force_encrypt) {
1081 status = cli_cm_force_encryption(cli,
1082 username,
1083 password,
1084 lp_workgroup(),
1085 "IPC$");
1086 if (!NT_STATUS_IS_OK(status)) {
1087 return false;
1091 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1092 &num_refs, &consumed);
1093 res = NT_STATUS_IS_OK(status);
1095 status = cli_tdis(cli);
1096 if (!NT_STATUS_IS_OK(status)) {
1097 return false;
1100 cli_state_set_tid(cli, cnum);
1102 if (!res || !num_refs) {
1103 return false;
1106 if (!refs[0].dfspath) {
1107 return false;
1110 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1111 pp_newshare, &newextrapath)) {
1112 return false;
1115 /* check that this is not a self-referral */
1117 if (strequal(remote_name, *pp_newserver) &&
1118 strequal(sharename, *pp_newshare)) {
1119 return false;
1122 return true;