s3 OneFS: Add an atomic sendfile implementation
[Samba/fernandojvsilva.git] / source3 / libsmb / clidfs.c
blobe642f169f974eebf442d2adf3b2efd8504c9594a
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
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"
24 /********************************************************************
25 Important point.
27 DFS paths are *always* of the form \server\share\<pathname> (the \ characters
28 are not C escaped here).
30 - but if we're using POSIX paths then <pathname> may contain
31 '/' separators, not '\\' separators. So cope with '\\' or '/'
32 as a separator when looking at the pathname part.... JRA.
33 ********************************************************************/
35 struct client_connection {
36 struct client_connection *prev, *next;
37 struct cli_state *cli;
38 char *mount;
41 static struct cm_cred_struct {
42 char *username;
43 char *password;
44 bool got_pass;
45 bool use_kerberos;
46 bool fallback_after_kerberos;
47 int signing_state;
48 } cm_creds;
50 static void cm_set_password(const char *newpass);
52 static struct client_connection *connections;
54 static bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
55 struct cli_state *cli,
56 const char *sharename,
57 char **pp_newserver,
58 char **pp_newshare,
59 bool force_encrypt,
60 const char *username,
61 const char *password,
62 const char *domain);
64 /********************************************************************
65 Ensure a connection is encrypted.
66 ********************************************************************/
68 NTSTATUS cli_cm_force_encryption(struct cli_state *c,
69 const char *username,
70 const char *password,
71 const char *domain,
72 const char *sharename)
74 NTSTATUS status = cli_force_encryption(c,
75 username,
76 password,
77 domain);
79 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
80 d_printf("Encryption required and "
81 "server that doesn't support "
82 "UNIX extensions - failing connect\n");
83 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNKNOWN_REVISION)) {
84 d_printf("Encryption required and "
85 "can't get UNIX CIFS extensions "
86 "version from server.\n");
87 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
88 d_printf("Encryption required and "
89 "share %s doesn't support "
90 "encryption.\n", sharename);
91 } else if (!NT_STATUS_IS_OK(status)) {
92 d_printf("Encryption required and "
93 "setup failed with error %s.\n",
94 nt_errstr(status));
97 return status;
100 /********************************************************************
101 Return a connection to a server.
102 ********************************************************************/
104 static struct cli_state *do_connect(TALLOC_CTX *ctx,
105 const char *server,
106 const char *share,
107 bool show_sessetup,
108 bool force_encrypt,
109 int max_protocol,
110 int port,
111 int name_type)
113 struct cli_state *c = NULL;
114 struct nmb_name called, calling;
115 const char *server_n;
116 struct sockaddr_storage ss;
117 char *servicename;
118 char *sharename;
119 char *newserver, *newshare;
120 const char *username;
121 const char *password;
122 NTSTATUS status;
124 /* make a copy so we don't modify the global string 'service' */
125 servicename = talloc_strdup(ctx,share);
126 if (!servicename) {
127 return NULL;
129 sharename = servicename;
130 if (*sharename == '\\') {
131 sharename += 2;
132 if (server == NULL) {
133 server = sharename;
135 sharename = strchr_m(sharename,'\\');
136 if (!sharename) {
137 return NULL;
139 *sharename = 0;
140 sharename++;
143 server_n = server;
145 zero_sockaddr(&ss);
147 make_nmb_name(&calling, global_myname(), 0x0);
148 make_nmb_name(&called , server, name_type);
150 again:
151 zero_sockaddr(&ss);
153 /* have to open a new connection */
154 if (!(c=cli_initialise())) {
155 d_printf("Connection to %s failed\n", server_n);
156 if (c) {
157 cli_shutdown(c);
159 return NULL;
161 if (port) {
162 cli_set_port(c, port);
165 status = cli_connect(c, server_n, &ss);
166 if (!NT_STATUS_IS_OK(status)) {
167 d_printf("Connection to %s failed (Error %s)\n",
168 server_n,
169 nt_errstr(status));
170 cli_shutdown(c);
171 return NULL;
174 if (max_protocol == 0) {
175 max_protocol = PROTOCOL_NT1;
177 c->protocol = max_protocol;
178 c->use_kerberos = cm_creds.use_kerberos;
179 c->fallback_after_kerberos = cm_creds.fallback_after_kerberos;
180 cli_setup_signing_state(c, cm_creds.signing_state);
182 if (!cli_session_request(c, &calling, &called)) {
183 char *p;
184 d_printf("session request to %s failed (%s)\n",
185 called.name, cli_errstr(c));
186 cli_shutdown(c);
187 c = NULL;
188 if ((p=strchr_m(called.name, '.'))) {
189 *p = 0;
190 goto again;
192 if (strcmp(called.name, "*SMBSERVER")) {
193 make_nmb_name(&called , "*SMBSERVER", 0x20);
194 goto again;
196 return NULL;
199 DEBUG(4,(" session request ok\n"));
201 status = cli_negprot(c);
203 if (!NT_STATUS_IS_OK(status)) {
204 d_printf("protocol negotiation failed: %s\n",
205 nt_errstr(status));
206 cli_shutdown(c);
207 return NULL;
210 if (!cm_creds.got_pass && !cm_creds.use_kerberos) {
211 char *label = NULL;
212 char *pass;
213 label = talloc_asprintf(ctx, "Enter %s's password: ",
214 cm_creds.username);
215 pass = getpass(label);
216 if (pass) {
217 cm_set_password(pass);
219 TALLOC_FREE(label);
222 username = cm_creds.username ? cm_creds.username : "";
223 password = cm_creds.password ? cm_creds.password : "";
225 if (!NT_STATUS_IS_OK(cli_session_setup(c, username,
226 password, strlen(password),
227 password, strlen(password),
228 lp_workgroup()))) {
229 /* If a password was not supplied then
230 * try again with a null username. */
231 if (password[0] || !username[0] || cm_creds.use_kerberos ||
232 !NT_STATUS_IS_OK(cli_session_setup(c, "",
233 "", 0,
234 "", 0,
235 lp_workgroup()))) {
236 d_printf("session setup failed: %s\n", cli_errstr(c));
237 if (NT_STATUS_V(cli_nt_error(c)) ==
238 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED))
239 d_printf("did you forget to run kinit?\n");
240 cli_shutdown(c);
241 return NULL;
243 d_printf("Anonymous login successful\n");
246 if ( show_sessetup ) {
247 if (*c->server_domain) {
248 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
249 c->server_domain,c->server_os,c->server_type));
250 } else if (*c->server_os || *c->server_type) {
251 DEBUG(0,("OS=[%s] Server=[%s]\n",
252 c->server_os,c->server_type));
255 DEBUG(4,(" session setup ok\n"));
257 /* here's the fun part....to support 'msdfs proxy' shares
258 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
259 here before trying to connect to the original share.
260 check_dfs_proxy() will fail if it is a normal share. */
262 if ((c->capabilities & CAP_DFS) &&
263 cli_check_msdfs_proxy(ctx, c, sharename,
264 &newserver, &newshare,
265 force_encrypt,
266 username,
267 password,
268 lp_workgroup())) {
269 cli_shutdown(c);
270 return do_connect(ctx, newserver,
271 newshare, false,
272 force_encrypt, max_protocol,
273 port, name_type);
276 /* must be a normal share */
278 status = cli_tcon_andx(c, sharename, "?????",
279 password, strlen(password)+1);
280 if (!NT_STATUS_IS_OK(status)) {
281 d_printf("tree connect failed: %s\n", nt_errstr(status));
282 cli_shutdown(c);
283 return NULL;
286 if (force_encrypt) {
287 status = cli_cm_force_encryption(c,
288 username,
289 password,
290 lp_workgroup(),
291 sharename);
292 if (!NT_STATUS_IS_OK(status)) {
293 cli_shutdown(c);
294 return NULL;
298 DEBUG(4,(" tconx ok\n"));
299 return c;
302 /****************************************************************************
303 ****************************************************************************/
305 static void cli_cm_set_mntpoint(struct cli_state *c, const char *mnt)
307 struct client_connection *p;
308 int i;
310 for (p=connections,i=0; p; p=p->next,i++) {
311 if (strequal(p->cli->desthost, c->desthost) &&
312 strequal(p->cli->share, c->share)) {
313 break;
317 if (p) {
318 char *name = clean_name(NULL, mnt);
319 if (!name) {
320 return;
322 TALLOC_FREE(p->mount);
323 p->mount = talloc_strdup(p, name);
324 TALLOC_FREE(name);
328 /****************************************************************************
329 ****************************************************************************/
331 const char *cli_cm_get_mntpoint(struct cli_state *c)
333 struct client_connection *p;
334 int i;
336 for (p=connections,i=0; p; p=p->next,i++) {
337 if (strequal(p->cli->desthost, c->desthost) &&
338 strequal(p->cli->share, c->share)) {
339 break;
343 if (p) {
344 return p->mount;
346 return NULL;
349 /********************************************************************
350 Add a new connection to the list
351 ********************************************************************/
353 static struct cli_state *cli_cm_connect(TALLOC_CTX *ctx,
354 struct cli_state *referring_cli,
355 const char *server,
356 const char *share,
357 bool show_hdr,
358 bool force_encrypt,
359 int max_protocol,
360 int port,
361 int name_type)
363 struct client_connection *node;
365 /* NB This must be the null context here... JRA. */
366 node = TALLOC_ZERO_ARRAY(NULL, struct client_connection, 1);
367 if (!node) {
368 return NULL;
371 node->cli = do_connect(ctx, server, share,
372 show_hdr, force_encrypt, max_protocol,
373 port, name_type);
375 if ( !node->cli ) {
376 TALLOC_FREE( node );
377 return NULL;
380 DLIST_ADD( connections, node );
382 cli_cm_set_mntpoint(node->cli, "");
384 if (referring_cli && referring_cli->posix_capabilities) {
385 uint16 major, minor;
386 uint32 caplow, caphigh;
387 if (cli_unix_extensions_version(node->cli, &major,
388 &minor, &caplow, &caphigh)) {
389 cli_set_unix_extensions_capabilities(node->cli,
390 major, minor,
391 caplow, caphigh);
395 return node->cli;
398 /********************************************************************
399 Return a connection to a server.
400 ********************************************************************/
402 static struct cli_state *cli_cm_find(const char *server, const char *share)
404 struct client_connection *p;
406 for (p=connections; p; p=p->next) {
407 if ( strequal(server, p->cli->desthost) &&
408 strequal(share,p->cli->share)) {
409 return p->cli;
413 return NULL;
416 /****************************************************************************
417 Open a client connection to a \\server\share. Set's the current *cli
418 global variable as a side-effect (but only if the connection is successful).
419 ****************************************************************************/
421 struct cli_state *cli_cm_open(TALLOC_CTX *ctx,
422 struct cli_state *referring_cli,
423 const char *server,
424 const char *share,
425 bool show_hdr,
426 bool force_encrypt,
427 int max_protocol,
428 int port,
429 int name_type)
431 struct cli_state *c;
433 /* try to reuse an existing connection */
435 c = cli_cm_find(server, share);
436 if (!c) {
437 c = cli_cm_connect(ctx, referring_cli,
438 server, share, show_hdr, force_encrypt,
439 max_protocol, port, name_type);
442 return c;
445 /****************************************************************************
446 ****************************************************************************/
448 void cli_cm_shutdown(void)
450 struct client_connection *p, *x;
452 for (p=connections; p;) {
453 cli_shutdown(p->cli);
454 x = p;
455 p = p->next;
457 TALLOC_FREE(x);
460 connections = NULL;
461 return;
464 /****************************************************************************
465 ****************************************************************************/
467 void cli_cm_display(void)
469 struct client_connection *p;
470 int i;
472 for ( p=connections,i=0; p; p=p->next,i++ ) {
473 d_printf("%d:\tserver=%s, share=%s\n",
474 i, p->cli->desthost, p->cli->share );
478 /****************************************************************************
479 ****************************************************************************/
481 static void cm_set_password(const char *newpass)
483 SAFE_FREE(cm_creds.password);
484 cm_creds.password = SMB_STRDUP(newpass);
485 if (cm_creds.password) {
486 cm_creds.got_pass = true;
490 /****************************************************************************
491 ****************************************************************************/
493 void cli_cm_set_credentials(struct user_auth_info *auth_info)
495 SAFE_FREE(cm_creds.username);
496 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
497 auth_info));
499 if (get_cmdline_auth_info_got_pass(auth_info)) {
500 cm_set_password(get_cmdline_auth_info_password(auth_info));
503 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
504 cm_creds.fallback_after_kerberos = false;
505 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
508 /****************************************************************************
509 ****************************************************************************/
511 void cli_cm_set_signing_state(int state)
513 cm_creds.signing_state = state;
516 /****************************************************************************
517 ****************************************************************************/
519 void cli_cm_set_username(const char *username)
521 SAFE_FREE(cm_creds.username);
522 cm_creds.username = SMB_STRDUP(username);
525 /****************************************************************************
526 ****************************************************************************/
528 void cli_cm_set_password(const char *newpass)
530 SAFE_FREE(cm_creds.password);
531 cm_creds.password = SMB_STRDUP(newpass);
532 if (cm_creds.password) {
533 cm_creds.got_pass = true;
537 /****************************************************************************
538 ****************************************************************************/
540 void cli_cm_set_use_kerberos(void)
542 cm_creds.use_kerberos = true;
545 /****************************************************************************
546 ****************************************************************************/
548 void cli_cm_set_fallback_after_kerberos(void)
550 cm_creds.fallback_after_kerberos = true;
553 /**********************************************************************
554 split a dfs path into the server, share name, and extrapath components
555 **********************************************************************/
557 static void split_dfs_path(TALLOC_CTX *ctx,
558 const char *nodepath,
559 char **pp_server,
560 char **pp_share,
561 char **pp_extrapath)
563 char *p, *q;
564 char *path;
566 *pp_server = NULL;
567 *pp_share = NULL;
568 *pp_extrapath = NULL;
570 path = talloc_strdup(ctx, nodepath);
571 if (!path) {
572 return;
575 if ( path[0] != '\\' ) {
576 return;
579 p = strchr_m( path + 1, '\\' );
580 if ( !p ) {
581 return;
584 *p = '\0';
585 p++;
587 /* Look for any extra/deep path */
588 q = strchr_m(p, '\\');
589 if (q != NULL) {
590 *q = '\0';
591 q++;
592 *pp_extrapath = talloc_strdup(ctx, q);
593 } else {
594 *pp_extrapath = talloc_strdup(ctx, "");
597 *pp_share = talloc_strdup(ctx, p);
598 *pp_server = talloc_strdup(ctx, &path[1]);
601 /****************************************************************************
602 Return the original path truncated at the directory component before
603 the first wildcard character. Trust the caller to provide a NULL
604 terminated string
605 ****************************************************************************/
607 static char *clean_path(TALLOC_CTX *ctx, const char *path)
609 size_t len;
610 char *p1, *p2, *p;
611 char *path_out;
613 /* No absolute paths. */
614 while (IS_DIRECTORY_SEP(*path)) {
615 path++;
618 path_out = talloc_strdup(ctx, path);
619 if (!path_out) {
620 return NULL;
623 p1 = strchr_m(path_out, '*');
624 p2 = strchr_m(path_out, '?');
626 if (p1 || p2) {
627 if (p1 && p2) {
628 p = MIN(p1,p2);
629 } else if (!p1) {
630 p = p2;
631 } else {
632 p = p1;
634 *p = '\0';
636 /* Now go back to the start of this component. */
637 p1 = strrchr_m(path_out, '/');
638 p2 = strrchr_m(path_out, '\\');
639 p = MAX(p1,p2);
640 if (p) {
641 *p = '\0';
645 /* Strip any trailing separator */
647 len = strlen(path_out);
648 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
649 path_out[len-1] = '\0';
652 return path_out;
655 /****************************************************************************
656 ****************************************************************************/
658 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
659 struct cli_state *cli,
660 const char *dir)
662 /* Ensure the extrapath doesn't start with a separator. */
663 while (IS_DIRECTORY_SEP(*dir)) {
664 dir++;
667 return talloc_asprintf(ctx, "\\%s\\%s\\%s",
668 cli->desthost, cli->share, dir);
671 /********************************************************************
672 check for dfs referral
673 ********************************************************************/
675 static bool cli_dfs_check_error( struct cli_state *cli, NTSTATUS status )
677 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
679 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
681 if (!((flgs2&FLAGS2_32_BIT_ERROR_CODES) &&
682 (flgs2&FLAGS2_UNICODE_STRINGS)))
683 return false;
685 if (NT_STATUS_EQUAL(status, NT_STATUS(IVAL(cli->inbuf,smb_rcls))))
686 return true;
688 return false;
691 /********************************************************************
692 Get the dfs referral link.
693 ********************************************************************/
695 bool cli_dfs_get_referral(TALLOC_CTX *ctx,
696 struct cli_state *cli,
697 const char *path,
698 CLIENT_DFS_REFERRAL**refs,
699 size_t *num_refs,
700 uint16 *consumed)
702 unsigned int data_len = 0;
703 unsigned int param_len = 0;
704 uint16 setup = TRANSACT2_GET_DFS_REFERRAL;
705 char *param;
706 char *rparam=NULL, *rdata=NULL;
707 char *p;
708 char *endp;
709 size_t pathlen = 2*(strlen(path)+1);
710 uint16 num_referrals;
711 CLIENT_DFS_REFERRAL *referrals = NULL;
712 bool ret = false;
714 *num_refs = 0;
715 *refs = NULL;
717 param = SMB_MALLOC_ARRAY(char, 2+pathlen+2);
718 if (!param) {
719 return false;
721 SSVAL(param, 0, 0x03); /* max referral level */
722 p = &param[2];
724 p += clistr_push(cli, p, path, pathlen, STR_TERMINATE);
725 param_len = PTR_DIFF(p, param);
727 if (!cli_send_trans(cli, SMBtrans2,
728 NULL, /* name */
729 -1, 0, /* fid, flags */
730 &setup, 1, 0, /* setup, length, max */
731 param, param_len, 2, /* param, length, max */
732 NULL, 0, cli->max_xmit /* data, length, max */
733 )) {
734 SAFE_FREE(param);
735 return false;
738 SAFE_FREE(param);
740 if (!cli_receive_trans(cli, SMBtrans2,
741 &rparam, &param_len,
742 &rdata, &data_len)) {
743 return false;
746 if (data_len < 4) {
747 goto out;
750 endp = rdata + data_len;
752 *consumed = SVAL(rdata, 0);
753 num_referrals = SVAL(rdata, 2);
755 if (num_referrals != 0) {
756 uint16 ref_version;
757 uint16 ref_size;
758 int i;
759 uint16 node_offset;
761 referrals = TALLOC_ARRAY(ctx, CLIENT_DFS_REFERRAL,
762 num_referrals);
764 if (!referrals) {
765 goto out;
767 /* start at the referrals array */
769 p = rdata+8;
770 for (i=0; i<num_referrals && p < endp; i++) {
771 if (p + 18 > endp) {
772 goto out;
774 ref_version = SVAL(p, 0);
775 ref_size = SVAL(p, 2);
776 node_offset = SVAL(p, 16);
778 if (ref_version != 3) {
779 p += ref_size;
780 continue;
783 referrals[i].proximity = SVAL(p, 8);
784 referrals[i].ttl = SVAL(p, 10);
786 if (p + node_offset > endp) {
787 goto out;
789 clistr_pull_talloc(ctx, cli->inbuf,
790 &referrals[i].dfspath,
791 p+node_offset, -1,
792 STR_TERMINATE|STR_UNICODE);
794 if (!referrals[i].dfspath) {
795 goto out;
797 p += ref_size;
799 if (i < num_referrals) {
800 goto out;
804 ret = true;
806 *num_refs = num_referrals;
807 *refs = referrals;
809 out:
811 SAFE_FREE(rdata);
812 SAFE_FREE(rparam);
813 return ret;
816 /********************************************************************
817 ********************************************************************/
819 bool cli_resolve_path(TALLOC_CTX *ctx,
820 const char *mountpt,
821 struct cli_state *rootcli,
822 const char *path,
823 struct cli_state **targetcli,
824 char **pp_targetpath)
826 CLIENT_DFS_REFERRAL *refs = NULL;
827 size_t num_refs = 0;
828 uint16 consumed;
829 struct cli_state *cli_ipc = NULL;
830 char *dfs_path = NULL;
831 char *cleanpath = NULL;
832 char *extrapath = NULL;
833 int pathlen;
834 char *server = NULL;
835 char *share = NULL;
836 struct cli_state *newcli = NULL;
837 char *newpath = NULL;
838 char *newmount = NULL;
839 char *ppath = NULL;
840 SMB_STRUCT_STAT sbuf;
841 uint32 attributes;
843 if ( !rootcli || !path || !targetcli ) {
844 return false;
847 /* Don't do anything if this is not a DFS root. */
849 if ( !rootcli->dfsroot) {
850 *targetcli = rootcli;
851 *pp_targetpath = talloc_strdup(ctx, path);
852 if (!*pp_targetpath) {
853 return false;
855 return true;
858 *targetcli = NULL;
860 /* Send a trans2_query_path_info to check for a referral. */
862 cleanpath = clean_path(ctx, path);
863 if (!cleanpath) {
864 return false;
867 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
868 if (!dfs_path) {
869 return false;
872 if (cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes)) {
873 /* This is an ordinary path, just return it. */
874 *targetcli = rootcli;
875 *pp_targetpath = talloc_strdup(ctx, path);
876 if (!*pp_targetpath) {
877 return false;
879 goto done;
882 /* Special case where client asked for a path that does not exist */
884 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
885 *targetcli = rootcli;
886 *pp_targetpath = talloc_strdup(ctx, path);
887 if (!*pp_targetpath) {
888 return false;
890 goto done;
893 /* We got an error, check for DFS referral. */
895 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED)) {
896 return false;
899 /* Check for the referral. */
901 if (!(cli_ipc = cli_cm_open(ctx, rootcli,
902 rootcli->desthost,
903 "IPC$", false,
904 (rootcli->trans_enc_state != NULL),
905 rootcli->protocol,
907 0x20))) {
908 return false;
911 if (!cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
912 &num_refs, &consumed) || !num_refs) {
913 return false;
916 /* Just store the first referral for now. */
918 if (!refs[0].dfspath) {
919 return false;
921 split_dfs_path(ctx, refs[0].dfspath, &server, &share, &extrapath );
923 if (!server || !share) {
924 return false;
927 /* Make sure to recreate the original string including any wildcards. */
929 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
930 if (!dfs_path) {
931 return false;
933 pathlen = strlen(dfs_path)*2;
934 consumed = MIN(pathlen, consumed);
935 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed/2]);
936 if (!*pp_targetpath) {
937 return false;
939 dfs_path[consumed/2] = '\0';
942 * *pp_targetpath is now the unconsumed part of the path.
943 * dfs_path is now the consumed part of the path
944 * (in \server\share\path format).
947 /* Open the connection to the target server & share */
948 if ((*targetcli = cli_cm_open(ctx, rootcli,
949 server,
950 share,
951 false,
952 (rootcli->trans_enc_state != NULL),
953 rootcli->protocol,
955 0x20)) == NULL) {
956 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
957 server, share );
958 return false;
961 if (extrapath && strlen(extrapath) > 0) {
962 *pp_targetpath = talloc_asprintf(ctx,
963 "%s%s",
964 extrapath,
965 *pp_targetpath);
966 if (!*pp_targetpath) {
967 return false;
971 /* parse out the consumed mount path */
972 /* trim off the \server\share\ */
974 ppath = dfs_path;
976 if (*ppath != '\\') {
977 d_printf("cli_resolve_path: "
978 "dfs_path (%s) not in correct format.\n",
979 dfs_path );
980 return false;
983 ppath++; /* Now pointing at start of server name. */
985 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
986 return false;
989 ppath++; /* Now pointing at start of share name. */
991 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
992 return false;
995 ppath++; /* Now pointing at path component. */
997 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
998 if (!newmount) {
999 return false;
1002 cli_cm_set_mntpoint(*targetcli, newmount);
1004 /* Check for another dfs referral, note that we are not
1005 checking for loops here. */
1007 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
1008 if (cli_resolve_path(ctx,
1009 newmount,
1010 *targetcli,
1011 *pp_targetpath,
1012 &newcli,
1013 &newpath)) {
1015 * When cli_resolve_path returns true here it's always
1016 * returning the complete path in newpath, so we're done
1017 * here.
1019 *targetcli = newcli;
1020 *pp_targetpath = newpath;
1021 return true;
1025 done:
1027 /* If returning true ensure we return a dfs root full path. */
1028 if ((*targetcli)->dfsroot) {
1029 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1030 if (!dfs_path) {
1031 return false;
1033 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1036 return true;
1039 /********************************************************************
1040 ********************************************************************/
1042 static bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1043 struct cli_state *cli,
1044 const char *sharename,
1045 char **pp_newserver,
1046 char **pp_newshare,
1047 bool force_encrypt,
1048 const char *username,
1049 const char *password,
1050 const char *domain)
1052 CLIENT_DFS_REFERRAL *refs = NULL;
1053 size_t num_refs = 0;
1054 uint16 consumed;
1055 char *fullpath = NULL;
1056 bool res;
1057 uint16 cnum;
1058 char *newextrapath = NULL;
1060 if (!cli || !sharename) {
1061 return false;
1064 cnum = cli->cnum;
1066 /* special case. never check for a referral on the IPC$ share */
1068 if (strequal(sharename, "IPC$")) {
1069 return false;
1072 /* send a trans2_query_path_info to check for a referral */
1074 fullpath = talloc_asprintf(ctx, "\\%s\\%s", cli->desthost, sharename );
1075 if (!fullpath) {
1076 return false;
1079 /* check for the referral */
1081 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, "IPC$", "IPC", NULL, 0))) {
1082 return false;
1085 if (force_encrypt) {
1086 NTSTATUS status = cli_cm_force_encryption(cli,
1087 username,
1088 password,
1089 lp_workgroup(),
1090 "IPC$");
1091 if (!NT_STATUS_IS_OK(status)) {
1092 return false;
1096 res = cli_dfs_get_referral(ctx, cli, fullpath, &refs, &num_refs, &consumed);
1098 if (!cli_tdis(cli)) {
1099 return false;
1102 cli->cnum = cnum;
1104 if (!res || !num_refs) {
1105 return false;
1108 if (!refs[0].dfspath) {
1109 return false;
1112 split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1113 pp_newshare, &newextrapath );
1115 if ((*pp_newserver == NULL) || (*pp_newshare == NULL)) {
1116 return false;
1119 /* check that this is not a self-referral */
1121 if (strequal(cli->desthost, *pp_newserver) &&
1122 strequal(sharename, *pp_newshare)) {
1123 return false;
1126 return true;