s3: Use cli_connect_nb in do_connect
[Samba.git] / source3 / libsmb / clidfs.c
blob5c5257fb6f88efca6f440cd975543328337d5d8c
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 char *servicename;
92 char *sharename;
93 char *newserver, *newshare;
94 const char *username;
95 const char *password;
96 NTSTATUS status;
98 /* make a copy so we don't modify the global string 'service' */
99 servicename = talloc_strdup(ctx,share);
100 if (!servicename) {
101 return NULL;
103 sharename = servicename;
104 if (*sharename == '\\') {
105 sharename += 2;
106 if (server == NULL) {
107 server = sharename;
109 sharename = strchr_m(sharename,'\\');
110 if (!sharename) {
111 return NULL;
113 *sharename = 0;
114 sharename++;
116 if (server == NULL) {
117 return NULL;
120 status = cli_connect_nb(
121 server, NULL, port, name_type, NULL,
122 get_cmdline_auth_info_signing_state(auth_info), &c);
124 if (!NT_STATUS_IS_OK(status)) {
125 d_printf("Connection to %s failed (Error %s)\n",
126 server,
127 nt_errstr(status));
128 return NULL;
131 if (max_protocol == 0) {
132 max_protocol = PROTOCOL_NT1;
134 c->protocol = max_protocol;
135 c->use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
136 c->fallback_after_kerberos =
137 get_cmdline_auth_info_fallback_after_kerberos(auth_info);
138 c->use_ccache = get_cmdline_auth_info_use_ccache(auth_info);
140 DEBUG(4,(" session request ok\n"));
142 status = cli_negprot(c);
144 if (!NT_STATUS_IS_OK(status)) {
145 d_printf("protocol negotiation failed: %s\n",
146 nt_errstr(status));
147 cli_shutdown(c);
148 return NULL;
151 username = get_cmdline_auth_info_username(auth_info);
152 password = get_cmdline_auth_info_password(auth_info);
154 if (!NT_STATUS_IS_OK(cli_session_setup(c, username,
155 password, strlen(password),
156 password, strlen(password),
157 lp_workgroup()))) {
158 /* If a password was not supplied then
159 * try again with a null username. */
160 if (password[0] || !username[0] ||
161 get_cmdline_auth_info_use_kerberos(auth_info) ||
162 !NT_STATUS_IS_OK(cli_session_setup(c, "",
163 "", 0,
164 "", 0,
165 lp_workgroup()))) {
166 d_printf("session setup failed: %s\n", cli_errstr(c));
167 if (NT_STATUS_V(cli_nt_error(c)) ==
168 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED))
169 d_printf("did you forget to run kinit?\n");
170 cli_shutdown(c);
171 return NULL;
173 d_printf("Anonymous login successful\n");
174 status = cli_init_creds(c, "", lp_workgroup(), "");
175 } else {
176 status = cli_init_creds(c, username, lp_workgroup(), password);
179 if (!NT_STATUS_IS_OK(status)) {
180 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
181 cli_shutdown(c);
182 return NULL;
185 if ( show_sessetup ) {
186 if (*c->server_domain) {
187 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
188 c->server_domain,c->server_os,c->server_type));
189 } else if (*c->server_os || *c->server_type) {
190 DEBUG(0,("OS=[%s] Server=[%s]\n",
191 c->server_os,c->server_type));
194 DEBUG(4,(" session setup ok\n"));
196 /* here's the fun part....to support 'msdfs proxy' shares
197 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
198 here before trying to connect to the original share.
199 cli_check_msdfs_proxy() will fail if it is a normal share. */
201 if ((c->capabilities & CAP_DFS) &&
202 cli_check_msdfs_proxy(ctx, c, sharename,
203 &newserver, &newshare,
204 force_encrypt,
205 username,
206 password,
207 lp_workgroup())) {
208 cli_shutdown(c);
209 return do_connect(ctx, newserver,
210 newshare, auth_info, false,
211 force_encrypt, max_protocol,
212 port, name_type);
215 /* must be a normal share */
217 status = cli_tcon_andx(c, sharename, "?????",
218 password, strlen(password)+1);
219 if (!NT_STATUS_IS_OK(status)) {
220 d_printf("tree connect failed: %s\n", nt_errstr(status));
221 cli_shutdown(c);
222 return NULL;
225 if (force_encrypt) {
226 status = cli_cm_force_encryption(c,
227 username,
228 password,
229 lp_workgroup(),
230 sharename);
231 if (!NT_STATUS_IS_OK(status)) {
232 cli_shutdown(c);
233 return NULL;
237 DEBUG(4,(" tconx ok\n"));
238 return c;
241 /****************************************************************************
242 ****************************************************************************/
244 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
246 char *name = clean_name(NULL, mnt);
247 if (!name) {
248 return;
250 TALLOC_FREE(cli->dfs_mountpoint);
251 cli->dfs_mountpoint = talloc_strdup(cli, name);
252 TALLOC_FREE(name);
255 /********************************************************************
256 Add a new connection to the list.
257 referring_cli == NULL means a new initial connection.
258 ********************************************************************/
260 static struct cli_state *cli_cm_connect(TALLOC_CTX *ctx,
261 struct cli_state *referring_cli,
262 const char *server,
263 const char *share,
264 const struct user_auth_info *auth_info,
265 bool show_hdr,
266 bool force_encrypt,
267 int max_protocol,
268 int port,
269 int name_type)
271 struct cli_state *cli;
273 cli = do_connect(ctx, server, share,
274 auth_info,
275 show_hdr, force_encrypt, max_protocol,
276 port, name_type);
278 if (!cli ) {
279 return NULL;
282 /* Enter into the list. */
283 if (referring_cli) {
284 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
287 if (referring_cli && referring_cli->requested_posix_capabilities) {
288 uint16 major, minor;
289 uint32 caplow, caphigh;
290 NTSTATUS status;
291 status = cli_unix_extensions_version(cli, &major, &minor,
292 &caplow, &caphigh);
293 if (NT_STATUS_IS_OK(status)) {
294 cli_set_unix_extensions_capabilities(cli,
295 major, minor,
296 caplow, caphigh);
300 return cli;
303 /********************************************************************
304 Return a connection to a server on a particular share.
305 ********************************************************************/
307 static struct cli_state *cli_cm_find(struct cli_state *cli,
308 const char *server,
309 const char *share)
311 struct cli_state *p;
313 if (cli == NULL) {
314 return NULL;
317 /* Search to the start of the list. */
318 for (p = cli; p; p = DLIST_PREV(p)) {
319 if (strequal(server, p->desthost) &&
320 strequal(share,p->share)) {
321 return p;
325 /* Search to the end of the list. */
326 for (p = cli->next; p; p = p->next) {
327 if (strequal(server, p->desthost) &&
328 strequal(share,p->share)) {
329 return p;
333 return NULL;
336 /****************************************************************************
337 Open a client connection to a \\server\share.
338 ****************************************************************************/
340 struct cli_state *cli_cm_open(TALLOC_CTX *ctx,
341 struct cli_state *referring_cli,
342 const char *server,
343 const char *share,
344 const struct user_auth_info *auth_info,
345 bool show_hdr,
346 bool force_encrypt,
347 int max_protocol,
348 int port,
349 int name_type)
351 /* Try to reuse an existing connection in this list. */
352 struct cli_state *c = cli_cm_find(referring_cli, server, share);
354 if (c) {
355 return c;
358 if (auth_info == NULL) {
359 /* Can't do a new connection
360 * without auth info. */
361 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
362 "without auth info\n",
363 server, share );
364 return NULL;
367 return cli_cm_connect(ctx,
368 referring_cli,
369 server,
370 share,
371 auth_info,
372 show_hdr,
373 force_encrypt,
374 max_protocol,
375 port,
376 name_type);
379 /****************************************************************************
380 ****************************************************************************/
382 void cli_cm_display(const struct cli_state *cli)
384 int i;
386 for (i=0; cli; cli = cli->next,i++ ) {
387 d_printf("%d:\tserver=%s, share=%s\n",
388 i, cli->desthost, cli->share );
392 /****************************************************************************
393 ****************************************************************************/
395 /****************************************************************************
396 ****************************************************************************/
398 #if 0
399 void cli_cm_set_credentials(struct user_auth_info *auth_info)
401 SAFE_FREE(cm_creds.username);
402 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
403 auth_info));
405 if (get_cmdline_auth_info_got_pass(auth_info)) {
406 cm_set_password(get_cmdline_auth_info_password(auth_info));
409 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
410 cm_creds.fallback_after_kerberos = false;
411 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
413 #endif
415 /**********************************************************************
416 split a dfs path into the server, share name, and extrapath components
417 **********************************************************************/
419 static bool split_dfs_path(TALLOC_CTX *ctx,
420 const char *nodepath,
421 char **pp_server,
422 char **pp_share,
423 char **pp_extrapath)
425 char *p, *q;
426 char *path;
428 *pp_server = NULL;
429 *pp_share = NULL;
430 *pp_extrapath = NULL;
432 path = talloc_strdup(ctx, nodepath);
433 if (!path) {
434 goto fail;
437 if ( path[0] != '\\' ) {
438 goto fail;
441 p = strchr_m( path + 1, '\\' );
442 if ( !p ) {
443 goto fail;
446 *p = '\0';
447 p++;
449 /* Look for any extra/deep path */
450 q = strchr_m(p, '\\');
451 if (q != NULL) {
452 *q = '\0';
453 q++;
454 *pp_extrapath = talloc_strdup(ctx, q);
455 } else {
456 *pp_extrapath = talloc_strdup(ctx, "");
458 if (*pp_extrapath == NULL) {
459 goto fail;
462 *pp_share = talloc_strdup(ctx, p);
463 if (*pp_share == NULL) {
464 goto fail;
467 *pp_server = talloc_strdup(ctx, &path[1]);
468 if (*pp_server == NULL) {
469 goto fail;
472 TALLOC_FREE(path);
473 return true;
475 fail:
476 TALLOC_FREE(*pp_share);
477 TALLOC_FREE(*pp_extrapath);
478 TALLOC_FREE(path);
479 return false;
482 /****************************************************************************
483 Return the original path truncated at the directory component before
484 the first wildcard character. Trust the caller to provide a NULL
485 terminated string
486 ****************************************************************************/
488 static char *clean_path(TALLOC_CTX *ctx, const char *path)
490 size_t len;
491 char *p1, *p2, *p;
492 char *path_out;
494 /* No absolute paths. */
495 while (IS_DIRECTORY_SEP(*path)) {
496 path++;
499 path_out = talloc_strdup(ctx, path);
500 if (!path_out) {
501 return NULL;
504 p1 = strchr_m(path_out, '*');
505 p2 = strchr_m(path_out, '?');
507 if (p1 || p2) {
508 if (p1 && p2) {
509 p = MIN(p1,p2);
510 } else if (!p1) {
511 p = p2;
512 } else {
513 p = p1;
515 *p = '\0';
517 /* Now go back to the start of this component. */
518 p1 = strrchr_m(path_out, '/');
519 p2 = strrchr_m(path_out, '\\');
520 p = MAX(p1,p2);
521 if (p) {
522 *p = '\0';
526 /* Strip any trailing separator */
528 len = strlen(path_out);
529 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
530 path_out[len-1] = '\0';
533 return path_out;
536 /****************************************************************************
537 ****************************************************************************/
539 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
540 struct cli_state *cli,
541 const char *dir)
543 char path_sep = '\\';
545 /* Ensure the extrapath doesn't start with a separator. */
546 while (IS_DIRECTORY_SEP(*dir)) {
547 dir++;
550 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
551 path_sep = '/';
553 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
554 path_sep,
555 cli->desthost,
556 path_sep,
557 cli->share,
558 path_sep,
559 dir);
562 /********************************************************************
563 check for dfs referral
564 ********************************************************************/
566 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
567 NTSTATUS status)
569 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
571 if (!(cli->capabilities & CAP_UNICODE)) {
572 return false;
574 if (!(cli->capabilities & CAP_STATUS32)) {
575 return false;
577 if (NT_STATUS_EQUAL(status, expected)) {
578 return true;
580 return false;
583 /********************************************************************
584 Get the dfs referral link.
585 ********************************************************************/
587 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
588 struct cli_state *cli,
589 const char *path,
590 struct client_dfs_referral **refs,
591 size_t *num_refs,
592 size_t *consumed)
594 unsigned int data_len = 0;
595 unsigned int param_len = 0;
596 uint16 setup[1];
597 uint8_t *param = NULL;
598 uint8_t *rdata = NULL;
599 char *p;
600 char *endp;
601 size_t pathlen = 2*(strlen(path)+1);
602 smb_ucs2_t *path_ucs;
603 char *consumed_path = NULL;
604 uint16_t consumed_ucs;
605 uint16 num_referrals;
606 struct client_dfs_referral *referrals = NULL;
607 NTSTATUS status;
609 *num_refs = 0;
610 *refs = NULL;
612 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
614 param = SMB_MALLOC_ARRAY(uint8_t, 2+pathlen+2);
615 if (!param) {
616 status = NT_STATUS_NO_MEMORY;
617 goto out;
619 SSVAL(param, 0, 0x03); /* max referral level */
620 p = (char *)(&param[2]);
622 path_ucs = (smb_ucs2_t *)p;
623 p += clistr_push(cli, p, path, pathlen, STR_TERMINATE);
624 param_len = PTR_DIFF(p, param);
626 status = cli_trans(talloc_tos(), cli, SMBtrans2,
627 NULL, 0xffff, 0, 0,
628 setup, 1, 0,
629 param, param_len, 2,
630 NULL, 0, cli->max_xmit,
631 NULL,
632 NULL, 0, NULL, /* rsetup */
633 NULL, 0, NULL,
634 &rdata, 4, &data_len);
635 if (!NT_STATUS_IS_OK(status)) {
636 goto out;
638 if (data_len < 4) {
639 goto out;
642 endp = (char *)rdata + data_len;
644 consumed_ucs = SVAL(rdata, 0);
645 num_referrals = SVAL(rdata, 2);
647 /* consumed_ucs is the number of bytes
648 * of the UCS2 path consumed not counting any
649 * terminating null. We need to convert
650 * back to unix charset and count again
651 * to get the number of bytes consumed from
652 * the incoming path. */
654 if (pull_string_talloc(talloc_tos(),
655 NULL,
657 &consumed_path,
658 path_ucs,
659 consumed_ucs,
660 STR_UNICODE) == 0) {
661 goto out;
663 if (consumed_path == NULL) {
664 goto out;
666 *consumed = strlen(consumed_path);
668 if (num_referrals != 0) {
669 uint16 ref_version;
670 uint16 ref_size;
671 int i;
672 uint16 node_offset;
674 referrals = talloc_array(ctx, struct client_dfs_referral,
675 num_referrals);
677 if (!referrals) {
678 goto out;
680 /* start at the referrals array */
682 p = (char *)rdata+8;
683 for (i=0; i<num_referrals && p < endp; i++) {
684 if (p + 18 > endp) {
685 goto out;
687 ref_version = SVAL(p, 0);
688 ref_size = SVAL(p, 2);
689 node_offset = SVAL(p, 16);
691 if (ref_version != 3) {
692 p += ref_size;
693 continue;
696 referrals[i].proximity = SVAL(p, 8);
697 referrals[i].ttl = SVAL(p, 10);
699 if (p + node_offset > endp) {
700 goto out;
702 clistr_pull_talloc(ctx, cli->inbuf,
703 SVAL(cli->inbuf, smb_flg2),
704 &referrals[i].dfspath,
705 p+node_offset,
706 cli->bufsize - ((p+node_offset)-cli->inbuf),
707 STR_TERMINATE|STR_UNICODE);
709 if (!referrals[i].dfspath) {
710 goto out;
712 p += ref_size;
714 if (i < num_referrals) {
715 goto out;
719 *num_refs = num_referrals;
720 *refs = referrals;
722 out:
724 TALLOC_FREE(consumed_path);
725 SAFE_FREE(param);
726 TALLOC_FREE(rdata);
727 return status;
730 /********************************************************************
731 ********************************************************************/
733 bool cli_resolve_path(TALLOC_CTX *ctx,
734 const char *mountpt,
735 const struct user_auth_info *dfs_auth_info,
736 struct cli_state *rootcli,
737 const char *path,
738 struct cli_state **targetcli,
739 char **pp_targetpath)
741 struct client_dfs_referral *refs = NULL;
742 size_t num_refs = 0;
743 size_t consumed = 0;
744 struct cli_state *cli_ipc = NULL;
745 char *dfs_path = NULL;
746 char *cleanpath = NULL;
747 char *extrapath = NULL;
748 int pathlen;
749 char *server = NULL;
750 char *share = NULL;
751 struct cli_state *newcli = NULL;
752 char *newpath = NULL;
753 char *newmount = NULL;
754 char *ppath = NULL;
755 SMB_STRUCT_STAT sbuf;
756 uint32 attributes;
757 NTSTATUS status;
759 if ( !rootcli || !path || !targetcli ) {
760 return false;
763 /* Don't do anything if this is not a DFS root. */
765 if ( !rootcli->dfsroot) {
766 *targetcli = rootcli;
767 *pp_targetpath = talloc_strdup(ctx, path);
768 if (!*pp_targetpath) {
769 return false;
771 return true;
774 *targetcli = NULL;
776 /* Send a trans2_query_path_info to check for a referral. */
778 cleanpath = clean_path(ctx, path);
779 if (!cleanpath) {
780 return false;
783 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
784 if (!dfs_path) {
785 return false;
788 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
789 if (NT_STATUS_IS_OK(status)) {
790 /* This is an ordinary path, just return it. */
791 *targetcli = rootcli;
792 *pp_targetpath = talloc_strdup(ctx, path);
793 if (!*pp_targetpath) {
794 return false;
796 goto done;
799 /* Special case where client asked for a path that does not exist */
801 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
802 status)) {
803 *targetcli = rootcli;
804 *pp_targetpath = talloc_strdup(ctx, path);
805 if (!*pp_targetpath) {
806 return false;
808 goto done;
811 /* We got an error, check for DFS referral. */
813 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
814 status)) {
815 return false;
818 /* Check for the referral. */
820 if (!(cli_ipc = cli_cm_open(ctx,
821 rootcli,
822 rootcli->desthost,
823 "IPC$",
824 dfs_auth_info,
825 false,
826 (rootcli->trans_enc_state != NULL),
827 rootcli->protocol,
829 0x20))) {
830 return false;
833 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
834 &num_refs, &consumed);
835 if (!NT_STATUS_IS_OK(status) || !num_refs) {
836 return false;
839 /* Just store the first referral for now. */
841 if (!refs[0].dfspath) {
842 return false;
844 if (!split_dfs_path(ctx, refs[0].dfspath, &server, &share,
845 &extrapath)) {
846 return false;
849 /* Make sure to recreate the original string including any wildcards. */
851 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
852 if (!dfs_path) {
853 return false;
855 pathlen = strlen(dfs_path);
856 consumed = MIN(pathlen, consumed);
857 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
858 if (!*pp_targetpath) {
859 return false;
861 dfs_path[consumed] = '\0';
864 * *pp_targetpath is now the unconsumed part of the path.
865 * dfs_path is now the consumed part of the path
866 * (in \server\share\path format).
869 /* Open the connection to the target server & share */
870 if ((*targetcli = cli_cm_open(ctx, rootcli,
871 server,
872 share,
873 dfs_auth_info,
874 false,
875 (rootcli->trans_enc_state != NULL),
876 rootcli->protocol,
878 0x20)) == NULL) {
879 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
880 server, share );
881 return false;
884 if (extrapath && strlen(extrapath) > 0) {
885 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
886 /* put the trailing \ on the path, so to be save we put one in if needed */
887 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
888 *pp_targetpath = talloc_asprintf(ctx,
889 "%s\\%s",
890 extrapath,
891 *pp_targetpath);
892 } else {
893 *pp_targetpath = talloc_asprintf(ctx,
894 "%s%s",
895 extrapath,
896 *pp_targetpath);
898 if (!*pp_targetpath) {
899 return false;
903 /* parse out the consumed mount path */
904 /* trim off the \server\share\ */
906 ppath = dfs_path;
908 if (*ppath != '\\') {
909 d_printf("cli_resolve_path: "
910 "dfs_path (%s) not in correct format.\n",
911 dfs_path );
912 return false;
915 ppath++; /* Now pointing at start of server name. */
917 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
918 return false;
921 ppath++; /* Now pointing at start of share name. */
923 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
924 return false;
927 ppath++; /* Now pointing at path component. */
929 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
930 if (!newmount) {
931 return false;
934 cli_set_mntpoint(*targetcli, newmount);
936 /* Check for another dfs referral, note that we are not
937 checking for loops here. */
939 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
940 if (cli_resolve_path(ctx,
941 newmount,
942 dfs_auth_info,
943 *targetcli,
944 *pp_targetpath,
945 &newcli,
946 &newpath)) {
948 * When cli_resolve_path returns true here it's always
949 * returning the complete path in newpath, so we're done
950 * here.
952 *targetcli = newcli;
953 *pp_targetpath = newpath;
954 return true;
958 done:
960 /* If returning true ensure we return a dfs root full path. */
961 if ((*targetcli)->dfsroot) {
962 dfs_path = talloc_strdup(ctx, *pp_targetpath);
963 if (!dfs_path) {
964 return false;
966 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
969 return true;
972 /********************************************************************
973 ********************************************************************/
975 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
976 struct cli_state *cli,
977 const char *sharename,
978 char **pp_newserver,
979 char **pp_newshare,
980 bool force_encrypt,
981 const char *username,
982 const char *password,
983 const char *domain)
985 struct client_dfs_referral *refs = NULL;
986 size_t num_refs = 0;
987 size_t consumed = 0;
988 char *fullpath = NULL;
989 bool res;
990 uint16 cnum;
991 char *newextrapath = NULL;
992 NTSTATUS status;
994 if (!cli || !sharename) {
995 return false;
998 cnum = cli->cnum;
1000 /* special case. never check for a referral on the IPC$ share */
1002 if (strequal(sharename, "IPC$")) {
1003 return false;
1006 /* send a trans2_query_path_info to check for a referral */
1008 fullpath = talloc_asprintf(ctx, "\\%s\\%s", cli->desthost, sharename );
1009 if (!fullpath) {
1010 return false;
1013 /* check for the referral */
1015 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, "IPC$", "IPC", NULL, 0))) {
1016 return false;
1019 if (force_encrypt) {
1020 status = cli_cm_force_encryption(cli,
1021 username,
1022 password,
1023 lp_workgroup(),
1024 "IPC$");
1025 if (!NT_STATUS_IS_OK(status)) {
1026 return false;
1030 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1031 &num_refs, &consumed);
1032 res = NT_STATUS_IS_OK(status);
1034 status = cli_tdis(cli);
1035 if (!NT_STATUS_IS_OK(status)) {
1036 return false;
1039 cli->cnum = cnum;
1041 if (!res || !num_refs) {
1042 return false;
1045 if (!refs[0].dfspath) {
1046 return false;
1049 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1050 pp_newshare, &newextrapath)) {
1051 return false;
1054 /* check that this is not a self-referral */
1056 if (strequal(cli->desthost, *pp_newserver) &&
1057 strequal(sharename, *pp_newshare)) {
1058 return false;
1061 return true;