s3: Fix Coverity ID 2596, REVERSE_INULL
[Samba/gebeck_regimport.git] / source3 / libsmb / clidfs.c
blobd729649286f251aaa87f58ba0222231be662492a
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 NTSTATUS 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,
89 struct cli_state **pcli)
91 struct cli_state *c = NULL;
92 char *servicename;
93 char *sharename;
94 char *newserver, *newshare;
95 const char *username;
96 const char *password;
97 NTSTATUS status;
99 /* make a copy so we don't modify the global string 'service' */
100 servicename = talloc_strdup(ctx,share);
101 if (!servicename) {
102 return NT_STATUS_NO_MEMORY;
104 sharename = servicename;
105 if (*sharename == '\\') {
106 sharename += 2;
107 if (server == NULL) {
108 server = sharename;
110 sharename = strchr_m(sharename,'\\');
111 if (!sharename) {
112 return NT_STATUS_NO_MEMORY;
114 *sharename = 0;
115 sharename++;
117 if (server == NULL) {
118 return NT_STATUS_INVALID_PARAMETER;
121 status = cli_connect_nb(
122 server, NULL, port, name_type, NULL,
123 get_cmdline_auth_info_signing_state(auth_info), &c);
125 if (!NT_STATUS_IS_OK(status)) {
126 d_printf("Connection to %s failed (Error %s)\n",
127 server,
128 nt_errstr(status));
129 return status;
132 if (max_protocol == 0) {
133 max_protocol = PROTOCOL_NT1;
135 c->protocol = max_protocol;
136 c->use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
137 c->fallback_after_kerberos =
138 get_cmdline_auth_info_fallback_after_kerberos(auth_info);
139 c->use_ccache = get_cmdline_auth_info_use_ccache(auth_info);
141 DEBUG(4,(" session request ok\n"));
143 status = cli_negprot(c);
145 if (!NT_STATUS_IS_OK(status)) {
146 d_printf("protocol negotiation failed: %s\n",
147 nt_errstr(status));
148 cli_shutdown(c);
149 return status;
152 username = get_cmdline_auth_info_username(auth_info);
153 password = get_cmdline_auth_info_password(auth_info);
155 status = cli_session_setup(c, username,
156 password, strlen(password),
157 password, strlen(password),
158 lp_workgroup());
159 if (!NT_STATUS_IS_OK(status)) {
160 /* If a password was not supplied then
161 * try again with a null username. */
162 if (password[0] || !username[0] ||
163 get_cmdline_auth_info_use_kerberos(auth_info) ||
164 !NT_STATUS_IS_OK(status = cli_session_setup(c, "",
165 "", 0,
166 "", 0,
167 lp_workgroup()))) {
168 d_printf("session setup failed: %s\n",
169 nt_errstr(status));
170 if (NT_STATUS_V(cli_nt_error(c)) ==
171 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED))
172 d_printf("did you forget to run kinit?\n");
173 cli_shutdown(c);
174 return status;
176 d_printf("Anonymous login successful\n");
177 status = cli_init_creds(c, "", lp_workgroup(), "");
178 } else {
179 status = cli_init_creds(c, username, lp_workgroup(), password);
182 if (!NT_STATUS_IS_OK(status)) {
183 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
184 cli_shutdown(c);
185 return status;
188 if ( show_sessetup ) {
189 if (*c->server_domain) {
190 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
191 c->server_domain,c->server_os,c->server_type));
192 } else if (*c->server_os || *c->server_type) {
193 DEBUG(0,("OS=[%s] Server=[%s]\n",
194 c->server_os,c->server_type));
197 DEBUG(4,(" session setup ok\n"));
199 /* here's the fun part....to support 'msdfs proxy' shares
200 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
201 here before trying to connect to the original share.
202 cli_check_msdfs_proxy() will fail if it is a normal share. */
204 if ((c->capabilities & CAP_DFS) &&
205 cli_check_msdfs_proxy(ctx, c, sharename,
206 &newserver, &newshare,
207 force_encrypt,
208 username,
209 password,
210 lp_workgroup())) {
211 cli_shutdown(c);
212 return do_connect(ctx, newserver,
213 newshare, auth_info, false,
214 force_encrypt, max_protocol,
215 port, name_type, pcli);
218 /* must be a normal share */
220 status = cli_tcon_andx(c, sharename, "?????",
221 password, strlen(password)+1);
222 if (!NT_STATUS_IS_OK(status)) {
223 d_printf("tree connect failed: %s\n", nt_errstr(status));
224 cli_shutdown(c);
225 return status;
228 if (force_encrypt) {
229 status = cli_cm_force_encryption(c,
230 username,
231 password,
232 lp_workgroup(),
233 sharename);
234 if (!NT_STATUS_IS_OK(status)) {
235 cli_shutdown(c);
236 return status;
240 DEBUG(4,(" tconx ok\n"));
241 *pcli = c;
242 return NT_STATUS_OK;
245 /****************************************************************************
246 ****************************************************************************/
248 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
250 char *name = clean_name(NULL, mnt);
251 if (!name) {
252 return;
254 TALLOC_FREE(cli->dfs_mountpoint);
255 cli->dfs_mountpoint = talloc_strdup(cli, name);
256 TALLOC_FREE(name);
259 /********************************************************************
260 Add a new connection to the list.
261 referring_cli == NULL means a new initial connection.
262 ********************************************************************/
264 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
265 struct cli_state *referring_cli,
266 const char *server,
267 const char *share,
268 const struct user_auth_info *auth_info,
269 bool show_hdr,
270 bool force_encrypt,
271 int max_protocol,
272 int port,
273 int name_type,
274 struct cli_state **pcli)
276 struct cli_state *cli;
277 NTSTATUS status;
279 status = do_connect(ctx, server, share,
280 auth_info,
281 show_hdr, force_encrypt, max_protocol,
282 port, name_type, &cli);
284 if (!NT_STATUS_IS_OK(status)) {
285 return status;
288 /* Enter into the list. */
289 if (referring_cli) {
290 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
293 if (referring_cli && referring_cli->requested_posix_capabilities) {
294 uint16 major, minor;
295 uint32 caplow, caphigh;
296 status = cli_unix_extensions_version(cli, &major, &minor,
297 &caplow, &caphigh);
298 if (NT_STATUS_IS_OK(status)) {
299 cli_set_unix_extensions_capabilities(cli,
300 major, minor,
301 caplow, caphigh);
305 *pcli = cli;
306 return NT_STATUS_OK;
309 /********************************************************************
310 Return a connection to a server on a particular share.
311 ********************************************************************/
313 static struct cli_state *cli_cm_find(struct cli_state *cli,
314 const char *server,
315 const char *share)
317 struct cli_state *p;
319 if (cli == NULL) {
320 return NULL;
323 /* Search to the start of the list. */
324 for (p = cli; p; p = DLIST_PREV(p)) {
325 const char *remote_name =
326 cli_state_remote_name(p);
328 if (strequal(server, remote_name) &&
329 strequal(share,p->share)) {
330 return p;
334 /* Search to the end of the list. */
335 for (p = cli->next; p; p = p->next) {
336 const char *remote_name =
337 cli_state_remote_name(p);
339 if (strequal(server, remote_name) &&
340 strequal(share,p->share)) {
341 return p;
345 return NULL;
348 /****************************************************************************
349 Open a client connection to a \\server\share.
350 ****************************************************************************/
352 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
353 struct cli_state *referring_cli,
354 const char *server,
355 const char *share,
356 const struct user_auth_info *auth_info,
357 bool show_hdr,
358 bool force_encrypt,
359 int max_protocol,
360 int port,
361 int name_type,
362 struct cli_state **pcli)
364 /* Try to reuse an existing connection in this list. */
365 struct cli_state *c = cli_cm_find(referring_cli, server, share);
366 NTSTATUS status;
368 if (c) {
369 *pcli = c;
370 return NT_STATUS_OK;
373 if (auth_info == NULL) {
374 /* Can't do a new connection
375 * without auth info. */
376 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
377 "without auth info\n",
378 server, share );
379 return NT_STATUS_INVALID_PARAMETER;
382 status = cli_cm_connect(ctx,
383 referring_cli,
384 server,
385 share,
386 auth_info,
387 show_hdr,
388 force_encrypt,
389 max_protocol,
390 port,
391 name_type,
392 &c);
393 if (!NT_STATUS_IS_OK(status)) {
394 return status;
396 *pcli = c;
397 return NT_STATUS_OK;
400 /****************************************************************************
401 ****************************************************************************/
403 void cli_cm_display(struct cli_state *cli)
405 int i;
407 for (i=0; cli; cli = cli->next,i++ ) {
408 d_printf("%d:\tserver=%s, share=%s\n",
409 i, cli_state_remote_name(cli), cli->share);
413 /****************************************************************************
414 ****************************************************************************/
416 /****************************************************************************
417 ****************************************************************************/
419 #if 0
420 void cli_cm_set_credentials(struct user_auth_info *auth_info)
422 SAFE_FREE(cm_creds.username);
423 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
424 auth_info));
426 if (get_cmdline_auth_info_got_pass(auth_info)) {
427 cm_set_password(get_cmdline_auth_info_password(auth_info));
430 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
431 cm_creds.fallback_after_kerberos = false;
432 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
434 #endif
436 /**********************************************************************
437 split a dfs path into the server, share name, and extrapath components
438 **********************************************************************/
440 static bool split_dfs_path(TALLOC_CTX *ctx,
441 const char *nodepath,
442 char **pp_server,
443 char **pp_share,
444 char **pp_extrapath)
446 char *p, *q;
447 char *path;
449 *pp_server = NULL;
450 *pp_share = NULL;
451 *pp_extrapath = NULL;
453 path = talloc_strdup(ctx, nodepath);
454 if (!path) {
455 goto fail;
458 if ( path[0] != '\\' ) {
459 goto fail;
462 p = strchr_m( path + 1, '\\' );
463 if ( !p ) {
464 goto fail;
467 *p = '\0';
468 p++;
470 /* Look for any extra/deep path */
471 q = strchr_m(p, '\\');
472 if (q != NULL) {
473 *q = '\0';
474 q++;
475 *pp_extrapath = talloc_strdup(ctx, q);
476 } else {
477 *pp_extrapath = talloc_strdup(ctx, "");
479 if (*pp_extrapath == NULL) {
480 goto fail;
483 *pp_share = talloc_strdup(ctx, p);
484 if (*pp_share == NULL) {
485 goto fail;
488 *pp_server = talloc_strdup(ctx, &path[1]);
489 if (*pp_server == NULL) {
490 goto fail;
493 TALLOC_FREE(path);
494 return true;
496 fail:
497 TALLOC_FREE(*pp_share);
498 TALLOC_FREE(*pp_extrapath);
499 TALLOC_FREE(path);
500 return false;
503 /****************************************************************************
504 Return the original path truncated at the directory component before
505 the first wildcard character. Trust the caller to provide a NULL
506 terminated string
507 ****************************************************************************/
509 static char *clean_path(TALLOC_CTX *ctx, const char *path)
511 size_t len;
512 char *p1, *p2, *p;
513 char *path_out;
515 /* No absolute paths. */
516 while (IS_DIRECTORY_SEP(*path)) {
517 path++;
520 path_out = talloc_strdup(ctx, path);
521 if (!path_out) {
522 return NULL;
525 p1 = strchr_m(path_out, '*');
526 p2 = strchr_m(path_out, '?');
528 if (p1 || p2) {
529 if (p1 && p2) {
530 p = MIN(p1,p2);
531 } else if (!p1) {
532 p = p2;
533 } else {
534 p = p1;
536 *p = '\0';
538 /* Now go back to the start of this component. */
539 p1 = strrchr_m(path_out, '/');
540 p2 = strrchr_m(path_out, '\\');
541 p = MAX(p1,p2);
542 if (p) {
543 *p = '\0';
547 /* Strip any trailing separator */
549 len = strlen(path_out);
550 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
551 path_out[len-1] = '\0';
554 return path_out;
557 /****************************************************************************
558 ****************************************************************************/
560 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
561 struct cli_state *cli,
562 const char *dir)
564 char path_sep = '\\';
566 /* Ensure the extrapath doesn't start with a separator. */
567 while (IS_DIRECTORY_SEP(*dir)) {
568 dir++;
571 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
572 path_sep = '/';
574 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
575 path_sep,
576 cli_state_remote_name(cli),
577 path_sep,
578 cli->share,
579 path_sep,
580 dir);
583 /********************************************************************
584 check for dfs referral
585 ********************************************************************/
587 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
588 NTSTATUS status)
590 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
592 if (!(cli->capabilities & CAP_UNICODE)) {
593 return false;
595 if (!(cli->capabilities & CAP_STATUS32)) {
596 return false;
598 if (NT_STATUS_EQUAL(status, expected)) {
599 return true;
601 return false;
604 /********************************************************************
605 Get the dfs referral link.
606 ********************************************************************/
608 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
609 struct cli_state *cli,
610 const char *path,
611 struct client_dfs_referral **refs,
612 size_t *num_refs,
613 size_t *consumed)
615 unsigned int data_len = 0;
616 unsigned int param_len = 0;
617 uint16_t setup[1];
618 uint16_t recv_flags2;
619 uint8_t *param = NULL;
620 uint8_t *rdata = NULL;
621 char *p;
622 char *endp;
623 smb_ucs2_t *path_ucs;
624 char *consumed_path = NULL;
625 uint16_t consumed_ucs;
626 uint16 num_referrals;
627 struct client_dfs_referral *referrals = NULL;
628 NTSTATUS status;
629 TALLOC_CTX *frame = talloc_stackframe();
631 *num_refs = 0;
632 *refs = NULL;
634 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
636 param = talloc_array(talloc_tos(), uint8_t, 2);
637 if (!param) {
638 status = NT_STATUS_NO_MEMORY;
639 goto out;
641 SSVAL(param, 0, 0x03); /* max referral level */
643 param = trans2_bytes_push_str(param, cli_ucs2(cli),
644 path, strlen(path)+1,
645 NULL);
646 if (!param) {
647 status = NT_STATUS_NO_MEMORY;
648 goto out;
650 param_len = talloc_get_size(param);
651 path_ucs = (smb_ucs2_t *)&param[2];
653 status = cli_trans(talloc_tos(), cli, SMBtrans2,
654 NULL, 0xffff, 0, 0,
655 setup, 1, 0,
656 param, param_len, 2,
657 NULL, 0, cli->max_xmit,
658 &recv_flags2,
659 NULL, 0, NULL, /* rsetup */
660 NULL, 0, NULL,
661 &rdata, 4, &data_len);
662 if (!NT_STATUS_IS_OK(status)) {
663 goto out;
666 endp = (char *)rdata + data_len;
668 consumed_ucs = SVAL(rdata, 0);
669 num_referrals = SVAL(rdata, 2);
671 /* consumed_ucs is the number of bytes
672 * of the UCS2 path consumed not counting any
673 * terminating null. We need to convert
674 * back to unix charset and count again
675 * to get the number of bytes consumed from
676 * the incoming path. */
678 errno = 0;
679 if (pull_string_talloc(talloc_tos(),
680 NULL,
682 &consumed_path,
683 path_ucs,
684 consumed_ucs,
685 STR_UNICODE) == 0) {
686 if (errno != 0) {
687 status = map_nt_error_from_unix(errno);
688 } else {
689 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
691 goto out;
693 if (consumed_path == NULL) {
694 status = map_nt_error_from_unix(errno);
695 goto out;
697 *consumed = strlen(consumed_path);
699 if (num_referrals != 0) {
700 uint16 ref_version;
701 uint16 ref_size;
702 int i;
703 uint16 node_offset;
705 referrals = talloc_array(ctx, struct client_dfs_referral,
706 num_referrals);
708 if (!referrals) {
709 status = NT_STATUS_NO_MEMORY;
710 goto out;
712 /* start at the referrals array */
714 p = (char *)rdata+8;
715 for (i=0; i<num_referrals && p < endp; i++) {
716 if (p + 18 > endp) {
717 goto out;
719 ref_version = SVAL(p, 0);
720 ref_size = SVAL(p, 2);
721 node_offset = SVAL(p, 16);
723 if (ref_version != 3) {
724 p += ref_size;
725 continue;
728 referrals[i].proximity = SVAL(p, 8);
729 referrals[i].ttl = SVAL(p, 10);
731 if (p + node_offset > endp) {
732 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
733 goto out;
735 clistr_pull_talloc(referrals,
736 (const char *)rdata,
737 recv_flags2,
738 &referrals[i].dfspath,
739 p+node_offset,
740 PTR_DIFF(endp, p+node_offset),
741 STR_TERMINATE|STR_UNICODE);
743 if (!referrals[i].dfspath) {
744 status = map_nt_error_from_unix(errno);
745 goto out;
747 p += ref_size;
749 if (i < num_referrals) {
750 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
751 goto out;
755 *num_refs = num_referrals;
756 *refs = referrals;
758 out:
760 TALLOC_FREE(frame);
761 return status;
764 /********************************************************************
765 ********************************************************************/
767 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
768 const char *mountpt,
769 const struct user_auth_info *dfs_auth_info,
770 struct cli_state *rootcli,
771 const char *path,
772 struct cli_state **targetcli,
773 char **pp_targetpath)
775 struct client_dfs_referral *refs = NULL;
776 size_t num_refs = 0;
777 size_t consumed = 0;
778 struct cli_state *cli_ipc = NULL;
779 char *dfs_path = NULL;
780 char *cleanpath = NULL;
781 char *extrapath = NULL;
782 int pathlen;
783 char *server = NULL;
784 char *share = NULL;
785 struct cli_state *newcli = NULL;
786 char *newpath = NULL;
787 char *newmount = NULL;
788 char *ppath = NULL;
789 SMB_STRUCT_STAT sbuf;
790 uint32 attributes;
791 NTSTATUS status;
793 if ( !rootcli || !path || !targetcli ) {
794 return NT_STATUS_INVALID_PARAMETER;
797 /* Don't do anything if this is not a DFS root. */
799 if ( !rootcli->dfsroot) {
800 *targetcli = rootcli;
801 *pp_targetpath = talloc_strdup(ctx, path);
802 if (!*pp_targetpath) {
803 return NT_STATUS_NO_MEMORY;
805 return NT_STATUS_OK;
808 *targetcli = NULL;
810 /* Send a trans2_query_path_info to check for a referral. */
812 cleanpath = clean_path(ctx, path);
813 if (!cleanpath) {
814 return NT_STATUS_NO_MEMORY;
817 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
818 if (!dfs_path) {
819 return NT_STATUS_NO_MEMORY;
822 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
823 if (NT_STATUS_IS_OK(status)) {
824 /* This is an ordinary path, just return it. */
825 *targetcli = rootcli;
826 *pp_targetpath = talloc_strdup(ctx, path);
827 if (!*pp_targetpath) {
828 return NT_STATUS_NO_MEMORY;
830 goto done;
833 /* Special case where client asked for a path that does not exist */
835 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
836 status)) {
837 *targetcli = rootcli;
838 *pp_targetpath = talloc_strdup(ctx, path);
839 if (!*pp_targetpath) {
840 return NT_STATUS_NO_MEMORY;
842 goto done;
845 /* We got an error, check for DFS referral. */
847 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
848 status)) {
849 return status;
852 /* Check for the referral. */
854 status = cli_cm_open(ctx,
855 rootcli,
856 cli_state_remote_name(rootcli),
857 "IPC$",
858 dfs_auth_info,
859 false,
860 (rootcli->trans_enc_state != NULL),
861 rootcli->protocol,
863 0x20,
864 &cli_ipc);
865 if (!NT_STATUS_IS_OK(status)) {
866 return status;
869 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
870 &num_refs, &consumed);
871 if (!NT_STATUS_IS_OK(status) || !num_refs) {
872 return status;
875 /* Just store the first referral for now. */
877 if (!refs[0].dfspath) {
878 return NT_STATUS_NOT_FOUND;
880 if (!split_dfs_path(ctx, refs[0].dfspath, &server, &share,
881 &extrapath)) {
882 return NT_STATUS_NOT_FOUND;
885 /* Make sure to recreate the original string including any wildcards. */
887 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
888 if (!dfs_path) {
889 return NT_STATUS_NO_MEMORY;
891 pathlen = strlen(dfs_path);
892 consumed = MIN(pathlen, consumed);
893 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
894 if (!*pp_targetpath) {
895 return NT_STATUS_NO_MEMORY;
897 dfs_path[consumed] = '\0';
900 * *pp_targetpath is now the unconsumed part of the path.
901 * dfs_path is now the consumed part of the path
902 * (in \server\share\path format).
905 /* Open the connection to the target server & share */
906 status = cli_cm_open(ctx, rootcli,
907 server,
908 share,
909 dfs_auth_info,
910 false,
911 (rootcli->trans_enc_state != NULL),
912 rootcli->protocol,
914 0x20,
915 targetcli);
916 if (!NT_STATUS_IS_OK(status)) {
917 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
918 server, share );
919 return status;
922 if (extrapath && strlen(extrapath) > 0) {
923 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
924 /* put the trailing \ on the path, so to be save we put one in if needed */
925 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
926 *pp_targetpath = talloc_asprintf(ctx,
927 "%s\\%s",
928 extrapath,
929 *pp_targetpath);
930 } else {
931 *pp_targetpath = talloc_asprintf(ctx,
932 "%s%s",
933 extrapath,
934 *pp_targetpath);
936 if (!*pp_targetpath) {
937 return NT_STATUS_NO_MEMORY;
941 /* parse out the consumed mount path */
942 /* trim off the \server\share\ */
944 ppath = dfs_path;
946 if (*ppath != '\\') {
947 d_printf("cli_resolve_path: "
948 "dfs_path (%s) not in correct format.\n",
949 dfs_path );
950 return NT_STATUS_NOT_FOUND;
953 ppath++; /* Now pointing at start of server name. */
955 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
956 return NT_STATUS_NOT_FOUND;
959 ppath++; /* Now pointing at start of share name. */
961 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
962 return NT_STATUS_NOT_FOUND;
965 ppath++; /* Now pointing at path component. */
967 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
968 if (!newmount) {
969 return NT_STATUS_NOT_FOUND;
972 cli_set_mntpoint(*targetcli, newmount);
974 /* Check for another dfs referral, note that we are not
975 checking for loops here. */
977 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
978 status = cli_resolve_path(ctx,
979 newmount,
980 dfs_auth_info,
981 *targetcli,
982 *pp_targetpath,
983 &newcli,
984 &newpath);
985 if (NT_STATUS_IS_OK(status)) {
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 status;
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 NT_STATUS_NO_MEMORY;
1005 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1006 if (*pp_targetpath == NULL) {
1007 return NT_STATUS_NO_MEMORY;
1011 return NT_STATUS_OK;
1014 /********************************************************************
1015 ********************************************************************/
1017 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1018 struct cli_state *cli,
1019 const char *sharename,
1020 char **pp_newserver,
1021 char **pp_newshare,
1022 bool force_encrypt,
1023 const char *username,
1024 const char *password,
1025 const char *domain)
1027 struct client_dfs_referral *refs = NULL;
1028 size_t num_refs = 0;
1029 size_t consumed = 0;
1030 char *fullpath = NULL;
1031 bool res;
1032 uint16 cnum;
1033 char *newextrapath = NULL;
1034 NTSTATUS status;
1035 const char *remote_name;
1037 if (!cli || !sharename) {
1038 return false;
1041 remote_name = cli_state_remote_name(cli);
1042 cnum = cli_state_get_tid(cli);
1044 /* special case. never check for a referral on the IPC$ share */
1046 if (strequal(sharename, "IPC$")) {
1047 return false;
1050 /* send a trans2_query_path_info to check for a referral */
1052 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1053 if (!fullpath) {
1054 return false;
1057 /* check for the referral */
1059 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, "IPC$", "IPC", NULL, 0))) {
1060 return false;
1063 if (force_encrypt) {
1064 status = cli_cm_force_encryption(cli,
1065 username,
1066 password,
1067 lp_workgroup(),
1068 "IPC$");
1069 if (!NT_STATUS_IS_OK(status)) {
1070 return false;
1074 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1075 &num_refs, &consumed);
1076 res = NT_STATUS_IS_OK(status);
1078 status = cli_tdis(cli);
1079 if (!NT_STATUS_IS_OK(status)) {
1080 return false;
1083 cli_state_set_tid(cli, cnum);
1085 if (!res || !num_refs) {
1086 return false;
1089 if (!refs[0].dfspath) {
1090 return false;
1093 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1094 pp_newshare, &newextrapath)) {
1095 return false;
1098 /* check that this is not a self-referral */
1100 if (strequal(remote_name, *pp_newserver) &&
1101 strequal(sharename, *pp_newshare)) {
1102 return false;
1105 return true;