s3-libsmb: move protos to libsmb/proto.h
[Samba/vl.git] / source3 / libsmb / clidfs.c
blob89dcf944827e8e48ed98f0bf49a3cb03396473cf
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 struct cli_state *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)
90 struct cli_state *c = NULL;
91 struct nmb_name called, calling;
92 const char *called_str;
93 const char *server_n;
94 struct sockaddr_storage ss;
95 char *servicename;
96 char *sharename;
97 char *newserver, *newshare;
98 const char *username;
99 const char *password;
100 NTSTATUS status;
102 /* make a copy so we don't modify the global string 'service' */
103 servicename = talloc_strdup(ctx,share);
104 if (!servicename) {
105 return NULL;
107 sharename = servicename;
108 if (*sharename == '\\') {
109 sharename += 2;
110 called_str = sharename;
111 if (server == NULL) {
112 server = sharename;
114 sharename = strchr_m(sharename,'\\');
115 if (!sharename) {
116 return NULL;
118 *sharename = 0;
119 sharename++;
120 } else {
121 called_str = server;
124 server_n = server;
126 zero_sockaddr(&ss);
128 make_nmb_name(&calling, global_myname(), 0x0);
129 make_nmb_name(&called , called_str, name_type);
131 again:
132 zero_sockaddr(&ss);
134 /* have to open a new connection */
135 c = cli_initialise_ex(get_cmdline_auth_info_signing_state(auth_info));
136 if (c == NULL) {
137 d_printf("Connection to %s failed\n", server_n);
138 return NULL;
140 if (port) {
141 cli_set_port(c, port);
144 status = cli_connect(c, server_n, &ss);
145 if (!NT_STATUS_IS_OK(status)) {
146 d_printf("Connection to %s failed (Error %s)\n",
147 server_n,
148 nt_errstr(status));
149 cli_shutdown(c);
150 return NULL;
153 if (max_protocol == 0) {
154 max_protocol = PROTOCOL_NT1;
156 c->protocol = max_protocol;
157 c->use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
158 c->fallback_after_kerberos =
159 get_cmdline_auth_info_fallback_after_kerberos(auth_info);
160 c->use_ccache = get_cmdline_auth_info_use_ccache(auth_info);
162 if (!cli_session_request(c, &calling, &called)) {
163 char *p;
164 d_printf("session request to %s failed (%s)\n",
165 called.name, cli_errstr(c));
166 cli_shutdown(c);
167 c = NULL;
168 if ((p=strchr_m(called.name, '.'))) {
169 *p = 0;
170 goto again;
172 if (strcmp(called.name, "*SMBSERVER")) {
173 make_nmb_name(&called , "*SMBSERVER", 0x20);
174 goto again;
176 return NULL;
179 DEBUG(4,(" session request ok\n"));
181 status = cli_negprot(c);
183 if (!NT_STATUS_IS_OK(status)) {
184 d_printf("protocol negotiation failed: %s\n",
185 nt_errstr(status));
186 cli_shutdown(c);
187 return NULL;
190 username = get_cmdline_auth_info_username(auth_info);
191 password = get_cmdline_auth_info_password(auth_info);
193 if (!NT_STATUS_IS_OK(cli_session_setup(c, username,
194 password, strlen(password),
195 password, strlen(password),
196 lp_workgroup()))) {
197 /* If a password was not supplied then
198 * try again with a null username. */
199 if (password[0] || !username[0] ||
200 get_cmdline_auth_info_use_kerberos(auth_info) ||
201 !NT_STATUS_IS_OK(cli_session_setup(c, "",
202 "", 0,
203 "", 0,
204 lp_workgroup()))) {
205 d_printf("session setup failed: %s\n", cli_errstr(c));
206 if (NT_STATUS_V(cli_nt_error(c)) ==
207 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED))
208 d_printf("did you forget to run kinit?\n");
209 cli_shutdown(c);
210 return NULL;
212 d_printf("Anonymous login successful\n");
213 status = cli_init_creds(c, "", lp_workgroup(), "");
214 } else {
215 status = cli_init_creds(c, username, lp_workgroup(), password);
218 if (!NT_STATUS_IS_OK(status)) {
219 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
220 cli_shutdown(c);
221 return NULL;
224 if ( show_sessetup ) {
225 if (*c->server_domain) {
226 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
227 c->server_domain,c->server_os,c->server_type));
228 } else if (*c->server_os || *c->server_type) {
229 DEBUG(0,("OS=[%s] Server=[%s]\n",
230 c->server_os,c->server_type));
233 DEBUG(4,(" session setup ok\n"));
235 /* here's the fun part....to support 'msdfs proxy' shares
236 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
237 here before trying to connect to the original share.
238 cli_check_msdfs_proxy() will fail if it is a normal share. */
240 if ((c->capabilities & CAP_DFS) &&
241 cli_check_msdfs_proxy(ctx, c, sharename,
242 &newserver, &newshare,
243 force_encrypt,
244 username,
245 password,
246 lp_workgroup())) {
247 cli_shutdown(c);
248 return do_connect(ctx, newserver,
249 newshare, auth_info, false,
250 force_encrypt, max_protocol,
251 port, name_type);
254 /* must be a normal share */
256 status = cli_tcon_andx(c, sharename, "?????",
257 password, strlen(password)+1);
258 if (!NT_STATUS_IS_OK(status)) {
259 d_printf("tree connect failed: %s\n", nt_errstr(status));
260 cli_shutdown(c);
261 return NULL;
264 if (force_encrypt) {
265 status = cli_cm_force_encryption(c,
266 username,
267 password,
268 lp_workgroup(),
269 sharename);
270 if (!NT_STATUS_IS_OK(status)) {
271 cli_shutdown(c);
272 return NULL;
276 DEBUG(4,(" tconx ok\n"));
277 return c;
280 /****************************************************************************
281 ****************************************************************************/
283 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
285 char *name = clean_name(NULL, mnt);
286 if (!name) {
287 return;
289 TALLOC_FREE(cli->dfs_mountpoint);
290 cli->dfs_mountpoint = talloc_strdup(cli, name);
291 TALLOC_FREE(name);
294 /********************************************************************
295 Add a new connection to the list.
296 referring_cli == NULL means a new initial connection.
297 ********************************************************************/
299 static struct cli_state *cli_cm_connect(TALLOC_CTX *ctx,
300 struct cli_state *referring_cli,
301 const char *server,
302 const char *share,
303 const struct user_auth_info *auth_info,
304 bool show_hdr,
305 bool force_encrypt,
306 int max_protocol,
307 int port,
308 int name_type)
310 struct cli_state *cli;
312 cli = do_connect(ctx, server, share,
313 auth_info,
314 show_hdr, force_encrypt, max_protocol,
315 port, name_type);
317 if (!cli ) {
318 return NULL;
321 /* Enter into the list. */
322 if (referring_cli) {
323 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
326 if (referring_cli && referring_cli->requested_posix_capabilities) {
327 uint16 major, minor;
328 uint32 caplow, caphigh;
329 NTSTATUS status;
330 status = cli_unix_extensions_version(cli, &major, &minor,
331 &caplow, &caphigh);
332 if (NT_STATUS_IS_OK(status)) {
333 cli_set_unix_extensions_capabilities(cli,
334 major, minor,
335 caplow, caphigh);
339 return cli;
342 /********************************************************************
343 Return a connection to a server on a particular share.
344 ********************************************************************/
346 static struct cli_state *cli_cm_find(struct cli_state *cli,
347 const char *server,
348 const char *share)
350 struct cli_state *p;
352 if (cli == NULL) {
353 return NULL;
356 /* Search to the start of the list. */
357 for (p = cli; p; p = DLIST_PREV(p)) {
358 if (strequal(server, p->desthost) &&
359 strequal(share,p->share)) {
360 return p;
364 /* Search to the end of the list. */
365 for (p = cli->next; p; p = p->next) {
366 if (strequal(server, p->desthost) &&
367 strequal(share,p->share)) {
368 return p;
372 return NULL;
375 /****************************************************************************
376 Open a client connection to a \\server\share.
377 ****************************************************************************/
379 struct cli_state *cli_cm_open(TALLOC_CTX *ctx,
380 struct cli_state *referring_cli,
381 const char *server,
382 const char *share,
383 const struct user_auth_info *auth_info,
384 bool show_hdr,
385 bool force_encrypt,
386 int max_protocol,
387 int port,
388 int name_type)
390 /* Try to reuse an existing connection in this list. */
391 struct cli_state *c = cli_cm_find(referring_cli, server, share);
393 if (c) {
394 return c;
397 if (auth_info == NULL) {
398 /* Can't do a new connection
399 * without auth info. */
400 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
401 "without auth info\n",
402 server, share );
403 return NULL;
406 return cli_cm_connect(ctx,
407 referring_cli,
408 server,
409 share,
410 auth_info,
411 show_hdr,
412 force_encrypt,
413 max_protocol,
414 port,
415 name_type);
418 /****************************************************************************
419 ****************************************************************************/
421 void cli_cm_display(const struct cli_state *cli)
423 int i;
425 for (i=0; cli; cli = cli->next,i++ ) {
426 d_printf("%d:\tserver=%s, share=%s\n",
427 i, cli->desthost, cli->share );
431 /****************************************************************************
432 ****************************************************************************/
434 /****************************************************************************
435 ****************************************************************************/
437 #if 0
438 void cli_cm_set_credentials(struct user_auth_info *auth_info)
440 SAFE_FREE(cm_creds.username);
441 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
442 auth_info));
444 if (get_cmdline_auth_info_got_pass(auth_info)) {
445 cm_set_password(get_cmdline_auth_info_password(auth_info));
448 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
449 cm_creds.fallback_after_kerberos = false;
450 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
452 #endif
454 /**********************************************************************
455 split a dfs path into the server, share name, and extrapath components
456 **********************************************************************/
458 static bool split_dfs_path(TALLOC_CTX *ctx,
459 const char *nodepath,
460 char **pp_server,
461 char **pp_share,
462 char **pp_extrapath)
464 char *p, *q;
465 char *path;
467 *pp_server = NULL;
468 *pp_share = NULL;
469 *pp_extrapath = NULL;
471 path = talloc_strdup(ctx, nodepath);
472 if (!path) {
473 goto fail;
476 if ( path[0] != '\\' ) {
477 goto fail;
480 p = strchr_m( path + 1, '\\' );
481 if ( !p ) {
482 goto fail;
485 *p = '\0';
486 p++;
488 /* Look for any extra/deep path */
489 q = strchr_m(p, '\\');
490 if (q != NULL) {
491 *q = '\0';
492 q++;
493 *pp_extrapath = talloc_strdup(ctx, q);
494 } else {
495 *pp_extrapath = talloc_strdup(ctx, "");
497 if (*pp_extrapath == NULL) {
498 goto fail;
501 *pp_share = talloc_strdup(ctx, p);
502 if (*pp_share == NULL) {
503 goto fail;
506 *pp_server = talloc_strdup(ctx, &path[1]);
507 if (*pp_server == NULL) {
508 goto fail;
511 TALLOC_FREE(path);
512 return true;
514 fail:
515 TALLOC_FREE(*pp_share);
516 TALLOC_FREE(*pp_extrapath);
517 TALLOC_FREE(path);
518 return false;
521 /****************************************************************************
522 Return the original path truncated at the directory component before
523 the first wildcard character. Trust the caller to provide a NULL
524 terminated string
525 ****************************************************************************/
527 static char *clean_path(TALLOC_CTX *ctx, const char *path)
529 size_t len;
530 char *p1, *p2, *p;
531 char *path_out;
533 /* No absolute paths. */
534 while (IS_DIRECTORY_SEP(*path)) {
535 path++;
538 path_out = talloc_strdup(ctx, path);
539 if (!path_out) {
540 return NULL;
543 p1 = strchr_m(path_out, '*');
544 p2 = strchr_m(path_out, '?');
546 if (p1 || p2) {
547 if (p1 && p2) {
548 p = MIN(p1,p2);
549 } else if (!p1) {
550 p = p2;
551 } else {
552 p = p1;
554 *p = '\0';
556 /* Now go back to the start of this component. */
557 p1 = strrchr_m(path_out, '/');
558 p2 = strrchr_m(path_out, '\\');
559 p = MAX(p1,p2);
560 if (p) {
561 *p = '\0';
565 /* Strip any trailing separator */
567 len = strlen(path_out);
568 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
569 path_out[len-1] = '\0';
572 return path_out;
575 /****************************************************************************
576 ****************************************************************************/
578 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
579 struct cli_state *cli,
580 const char *dir)
582 char path_sep = '\\';
584 /* Ensure the extrapath doesn't start with a separator. */
585 while (IS_DIRECTORY_SEP(*dir)) {
586 dir++;
589 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
590 path_sep = '/';
592 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
593 path_sep,
594 cli->desthost,
595 path_sep,
596 cli->share,
597 path_sep,
598 dir);
601 /********************************************************************
602 check for dfs referral
603 ********************************************************************/
605 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
606 NTSTATUS status)
608 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
610 if (!(cli->capabilities & CAP_UNICODE)) {
611 return false;
613 if (!(cli->capabilities & CAP_STATUS32)) {
614 return false;
616 if (NT_STATUS_EQUAL(status, expected)) {
617 return true;
619 return false;
622 /********************************************************************
623 Get the dfs referral link.
624 ********************************************************************/
626 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
627 struct cli_state *cli,
628 const char *path,
629 struct client_dfs_referral **refs,
630 size_t *num_refs,
631 size_t *consumed)
633 unsigned int data_len = 0;
634 unsigned int param_len = 0;
635 uint16 setup[1];
636 uint8_t *param = NULL;
637 uint8_t *rdata = NULL;
638 char *p;
639 char *endp;
640 size_t pathlen = 2*(strlen(path)+1);
641 smb_ucs2_t *path_ucs;
642 char *consumed_path = NULL;
643 uint16_t consumed_ucs;
644 uint16 num_referrals;
645 struct client_dfs_referral *referrals = NULL;
646 NTSTATUS status;
648 *num_refs = 0;
649 *refs = NULL;
651 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
653 param = SMB_MALLOC_ARRAY(uint8_t, 2+pathlen+2);
654 if (!param) {
655 status = NT_STATUS_NO_MEMORY;
656 goto out;
658 SSVAL(param, 0, 0x03); /* max referral level */
659 p = (char *)(&param[2]);
661 path_ucs = (smb_ucs2_t *)p;
662 p += clistr_push(cli, p, path, pathlen, STR_TERMINATE);
663 param_len = PTR_DIFF(p, param);
665 status = cli_trans(talloc_tos(), cli, SMBtrans2,
666 NULL, 0xffff, 0, 0,
667 setup, 1, 0,
668 param, param_len, 2,
669 NULL, 0, cli->max_xmit,
670 NULL,
671 NULL, 0, NULL, /* rsetup */
672 NULL, 0, NULL,
673 &rdata, 4, &data_len);
674 if (!NT_STATUS_IS_OK(status)) {
675 goto out;
677 if (data_len < 4) {
678 goto out;
681 endp = (char *)rdata + data_len;
683 consumed_ucs = SVAL(rdata, 0);
684 num_referrals = SVAL(rdata, 2);
686 /* consumed_ucs is the number of bytes
687 * of the UCS2 path consumed not counting any
688 * terminating null. We need to convert
689 * back to unix charset and count again
690 * to get the number of bytes consumed from
691 * the incoming path. */
693 if (pull_string_talloc(talloc_tos(),
694 NULL,
696 &consumed_path,
697 path_ucs,
698 consumed_ucs,
699 STR_UNICODE) == 0) {
700 goto out;
702 if (consumed_path == NULL) {
703 goto out;
705 *consumed = strlen(consumed_path);
707 if (num_referrals != 0) {
708 uint16 ref_version;
709 uint16 ref_size;
710 int i;
711 uint16 node_offset;
713 referrals = talloc_array(ctx, struct client_dfs_referral,
714 num_referrals);
716 if (!referrals) {
717 goto out;
719 /* start at the referrals array */
721 p = (char *)rdata+8;
722 for (i=0; i<num_referrals && p < endp; i++) {
723 if (p + 18 > endp) {
724 goto out;
726 ref_version = SVAL(p, 0);
727 ref_size = SVAL(p, 2);
728 node_offset = SVAL(p, 16);
730 if (ref_version != 3) {
731 p += ref_size;
732 continue;
735 referrals[i].proximity = SVAL(p, 8);
736 referrals[i].ttl = SVAL(p, 10);
738 if (p + node_offset > endp) {
739 goto out;
741 clistr_pull_talloc(ctx, cli->inbuf,
742 SVAL(cli->inbuf, smb_flg2),
743 &referrals[i].dfspath,
744 p+node_offset,
745 cli->bufsize - ((p+node_offset)-cli->inbuf),
746 STR_TERMINATE|STR_UNICODE);
748 if (!referrals[i].dfspath) {
749 goto out;
751 p += ref_size;
753 if (i < num_referrals) {
754 goto out;
758 *num_refs = num_referrals;
759 *refs = referrals;
761 out:
763 TALLOC_FREE(consumed_path);
764 SAFE_FREE(param);
765 TALLOC_FREE(rdata);
766 return status;
769 /********************************************************************
770 ********************************************************************/
772 bool cli_resolve_path(TALLOC_CTX *ctx,
773 const char *mountpt,
774 const struct user_auth_info *dfs_auth_info,
775 struct cli_state *rootcli,
776 const char *path,
777 struct cli_state **targetcli,
778 char **pp_targetpath)
780 struct client_dfs_referral *refs = NULL;
781 size_t num_refs = 0;
782 size_t consumed = 0;
783 struct cli_state *cli_ipc = NULL;
784 char *dfs_path = NULL;
785 char *cleanpath = NULL;
786 char *extrapath = NULL;
787 int pathlen;
788 char *server = NULL;
789 char *share = NULL;
790 struct cli_state *newcli = NULL;
791 char *newpath = NULL;
792 char *newmount = NULL;
793 char *ppath = NULL;
794 SMB_STRUCT_STAT sbuf;
795 uint32 attributes;
796 NTSTATUS status;
798 if ( !rootcli || !path || !targetcli ) {
799 return false;
802 /* Don't do anything if this is not a DFS root. */
804 if ( !rootcli->dfsroot) {
805 *targetcli = rootcli;
806 *pp_targetpath = talloc_strdup(ctx, path);
807 if (!*pp_targetpath) {
808 return false;
810 return true;
813 *targetcli = NULL;
815 /* Send a trans2_query_path_info to check for a referral. */
817 cleanpath = clean_path(ctx, path);
818 if (!cleanpath) {
819 return false;
822 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
823 if (!dfs_path) {
824 return false;
827 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
828 if (NT_STATUS_IS_OK(status)) {
829 /* This is an ordinary path, just return it. */
830 *targetcli = rootcli;
831 *pp_targetpath = talloc_strdup(ctx, path);
832 if (!*pp_targetpath) {
833 return false;
835 goto done;
838 /* Special case where client asked for a path that does not exist */
840 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
841 status)) {
842 *targetcli = rootcli;
843 *pp_targetpath = talloc_strdup(ctx, path);
844 if (!*pp_targetpath) {
845 return false;
847 goto done;
850 /* We got an error, check for DFS referral. */
852 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
853 status)) {
854 return false;
857 /* Check for the referral. */
859 if (!(cli_ipc = cli_cm_open(ctx,
860 rootcli,
861 rootcli->desthost,
862 "IPC$",
863 dfs_auth_info,
864 false,
865 (rootcli->trans_enc_state != NULL),
866 rootcli->protocol,
868 0x20))) {
869 return false;
872 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
873 &num_refs, &consumed);
874 if (!NT_STATUS_IS_OK(status) || !num_refs) {
875 return false;
878 /* Just store the first referral for now. */
880 if (!refs[0].dfspath) {
881 return false;
883 if (!split_dfs_path(ctx, refs[0].dfspath, &server, &share,
884 &extrapath)) {
885 return false;
888 /* Make sure to recreate the original string including any wildcards. */
890 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
891 if (!dfs_path) {
892 return false;
894 pathlen = strlen(dfs_path);
895 consumed = MIN(pathlen, consumed);
896 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
897 if (!*pp_targetpath) {
898 return false;
900 dfs_path[consumed] = '\0';
903 * *pp_targetpath is now the unconsumed part of the path.
904 * dfs_path is now the consumed part of the path
905 * (in \server\share\path format).
908 /* Open the connection to the target server & share */
909 if ((*targetcli = cli_cm_open(ctx, rootcli,
910 server,
911 share,
912 dfs_auth_info,
913 false,
914 (rootcli->trans_enc_state != NULL),
915 rootcli->protocol,
917 0x20)) == NULL) {
918 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
919 server, share );
920 return false;
923 if (extrapath && strlen(extrapath) > 0) {
924 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
925 /* put the trailing \ on the path, so to be save we put one in if needed */
926 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
927 *pp_targetpath = talloc_asprintf(ctx,
928 "%s\\%s",
929 extrapath,
930 *pp_targetpath);
931 } else {
932 *pp_targetpath = talloc_asprintf(ctx,
933 "%s%s",
934 extrapath,
935 *pp_targetpath);
937 if (!*pp_targetpath) {
938 return false;
942 /* parse out the consumed mount path */
943 /* trim off the \server\share\ */
945 ppath = dfs_path;
947 if (*ppath != '\\') {
948 d_printf("cli_resolve_path: "
949 "dfs_path (%s) not in correct format.\n",
950 dfs_path );
951 return false;
954 ppath++; /* Now pointing at start of server name. */
956 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
957 return false;
960 ppath++; /* Now pointing at start of share name. */
962 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
963 return false;
966 ppath++; /* Now pointing at path component. */
968 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
969 if (!newmount) {
970 return false;
973 cli_set_mntpoint(*targetcli, newmount);
975 /* Check for another dfs referral, note that we are not
976 checking for loops here. */
978 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
979 if (cli_resolve_path(ctx,
980 newmount,
981 dfs_auth_info,
982 *targetcli,
983 *pp_targetpath,
984 &newcli,
985 &newpath)) {
987 * When cli_resolve_path returns true here it's always
988 * returning the complete path in newpath, so we're done
989 * here.
991 *targetcli = newcli;
992 *pp_targetpath = newpath;
993 return true;
997 done:
999 /* If returning true ensure we return a dfs root full path. */
1000 if ((*targetcli)->dfsroot) {
1001 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1002 if (!dfs_path) {
1003 return false;
1005 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1008 return true;
1011 /********************************************************************
1012 ********************************************************************/
1014 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1015 struct cli_state *cli,
1016 const char *sharename,
1017 char **pp_newserver,
1018 char **pp_newshare,
1019 bool force_encrypt,
1020 const char *username,
1021 const char *password,
1022 const char *domain)
1024 struct client_dfs_referral *refs = NULL;
1025 size_t num_refs = 0;
1026 size_t consumed = 0;
1027 char *fullpath = NULL;
1028 bool res;
1029 uint16 cnum;
1030 char *newextrapath = NULL;
1031 NTSTATUS status;
1033 if (!cli || !sharename) {
1034 return false;
1037 cnum = cli->cnum;
1039 /* special case. never check for a referral on the IPC$ share */
1041 if (strequal(sharename, "IPC$")) {
1042 return false;
1045 /* send a trans2_query_path_info to check for a referral */
1047 fullpath = talloc_asprintf(ctx, "\\%s\\%s", cli->desthost, sharename );
1048 if (!fullpath) {
1049 return false;
1052 /* check for the referral */
1054 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, "IPC$", "IPC", NULL, 0))) {
1055 return false;
1058 if (force_encrypt) {
1059 status = cli_cm_force_encryption(cli,
1060 username,
1061 password,
1062 lp_workgroup(),
1063 "IPC$");
1064 if (!NT_STATUS_IS_OK(status)) {
1065 return false;
1069 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1070 &num_refs, &consumed);
1071 res = NT_STATUS_IS_OK(status);
1073 status = cli_tdis(cli);
1074 if (!NT_STATUS_IS_OK(status)) {
1075 return false;
1078 cli->cnum = cnum;
1080 if (!res || !num_refs) {
1081 return false;
1084 if (!refs[0].dfspath) {
1085 return false;
1088 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1089 pp_newshare, &newextrapath)) {
1090 return false;
1093 /* check that this is not a self-referral */
1095 if (strequal(cli->desthost, *pp_newserver) &&
1096 strequal(sharename, *pp_newshare)) {
1097 return false;
1100 return true;