Fix bug 6572 - libsmbclient: unable to access 'msdfs proxy' share.
[metze/samba/wip.git] / source3 / libsmb / clidfs.c
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
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23
24 /********************************************************************
25  Important point.
26
27  DFS paths are *always* of the form \server\share\<pathname> (the \ characters
28  are not C escaped here).
29
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 ********************************************************************/
34
35 /********************************************************************
36  Ensure a connection is encrypted.
37 ********************************************************************/
38
39 NTSTATUS cli_cm_force_encryption(struct cli_state *c,
40                         const char *username,
41                         const char *password,
42                         const char *domain,
43                         const char *sharename)
44 {
45         NTSTATUS status = cli_force_encryption(c,
46                                         username,
47                                         password,
48                                         domain);
49
50         if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
51                 d_printf("Encryption required and "
52                         "server that doesn't support "
53                         "UNIX extensions - failing connect\n");
54         } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNKNOWN_REVISION)) {
55                 d_printf("Encryption required and "
56                         "can't get UNIX CIFS extensions "
57                         "version from server.\n");
58         } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
59                 d_printf("Encryption required and "
60                         "share %s doesn't support "
61                         "encryption.\n", sharename);
62         } else if (!NT_STATUS_IS_OK(status)) {
63                 d_printf("Encryption required and "
64                         "setup failed with error %s.\n",
65                         nt_errstr(status));
66         }
67
68         return status;
69 }
70
71 /********************************************************************
72  Return a connection to a server.
73 ********************************************************************/
74
75 static struct cli_state *do_connect(TALLOC_CTX *ctx,
76                                         const char *server,
77                                         const char *share,
78                                         const struct user_auth_info *auth_info,
79                                         bool show_sessetup,
80                                         bool force_encrypt,
81                                         int max_protocol,
82                                         int port,
83                                         int name_type)
84 {
85         struct cli_state *c = NULL;
86         struct nmb_name called, calling;
87         const char *called_str;
88         const char *server_n;
89         struct sockaddr_storage ss;
90         char *servicename;
91         char *sharename;
92         char *newserver, *newshare;
93         const char *username;
94         const char *password;
95         NTSTATUS status;
96
97         /* make a copy so we don't modify the global string 'service' */
98         servicename = talloc_strdup(ctx,share);
99         if (!servicename) {
100                 return NULL;
101         }
102         sharename = servicename;
103         if (*sharename == '\\') {
104                 sharename += 2;
105                 called_str = sharename;
106                 if (server == NULL) {
107                         server = sharename;
108                 }
109                 sharename = strchr_m(sharename,'\\');
110                 if (!sharename) {
111                         return NULL;
112                 }
113                 *sharename = 0;
114                 sharename++;
115         } else {
116                 called_str = server;
117         }
118
119         server_n = server;
120
121         zero_sockaddr(&ss);
122
123         make_nmb_name(&calling, global_myname(), 0x0);
124         make_nmb_name(&called , called_str, name_type);
125
126  again:
127         zero_sockaddr(&ss);
128
129         /* have to open a new connection */
130         if (!(c=cli_initialise_ex(get_cmdline_auth_info_signing_state(auth_info)))) {
131                 d_printf("Connection to %s failed\n", server_n);
132                 if (c) {
133                         cli_shutdown(c);
134                 }
135                 return NULL;
136         }
137         if (port) {
138                 cli_set_port(c, port);
139         }
140
141         status = cli_connect(c, server_n, &ss);
142         if (!NT_STATUS_IS_OK(status)) {
143                 d_printf("Connection to %s failed (Error %s)\n",
144                                 server_n,
145                                 nt_errstr(status));
146                 cli_shutdown(c);
147                 return NULL;
148         }
149
150         if (max_protocol == 0) {
151                 max_protocol = PROTOCOL_NT1;
152         }
153         c->protocol = max_protocol;
154         c->use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
155         c->fallback_after_kerberos =
156                 get_cmdline_auth_info_fallback_after_kerberos(auth_info);
157
158         if (!cli_session_request(c, &calling, &called)) {
159                 char *p;
160                 d_printf("session request to %s failed (%s)\n",
161                          called.name, cli_errstr(c));
162                 cli_shutdown(c);
163                 c = NULL;
164                 if ((p=strchr_m(called.name, '.'))) {
165                         *p = 0;
166                         goto again;
167                 }
168                 if (strcmp(called.name, "*SMBSERVER")) {
169                         make_nmb_name(&called , "*SMBSERVER", 0x20);
170                         goto again;
171                 }
172                 return NULL;
173         }
174
175         DEBUG(4,(" session request ok\n"));
176
177         status = cli_negprot(c);
178
179         if (!NT_STATUS_IS_OK(status)) {
180                 d_printf("protocol negotiation failed: %s\n",
181                          nt_errstr(status));
182                 cli_shutdown(c);
183                 return NULL;
184         }
185
186         username = get_cmdline_auth_info_username(auth_info);
187         password = get_cmdline_auth_info_password(auth_info);
188
189         if (!NT_STATUS_IS_OK(cli_session_setup(c, username,
190                                                password, strlen(password),
191                                                password, strlen(password),
192                                                lp_workgroup()))) {
193                 /* If a password was not supplied then
194                  * try again with a null username. */
195                 if (password[0] || !username[0] ||
196                         get_cmdline_auth_info_use_kerberos(auth_info) ||
197                         !NT_STATUS_IS_OK(cli_session_setup(c, "",
198                                                 "", 0,
199                                                 "", 0,
200                                                lp_workgroup()))) {
201                         d_printf("session setup failed: %s\n", cli_errstr(c));
202                         if (NT_STATUS_V(cli_nt_error(c)) ==
203                             NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED))
204                                 d_printf("did you forget to run kinit?\n");
205                         cli_shutdown(c);
206                         return NULL;
207                 }
208                 d_printf("Anonymous login successful\n");
209                 status = cli_init_creds(c, "", lp_workgroup(), "");
210         } else {
211                 status = cli_init_creds(c, username, lp_workgroup(), password);
212         }
213
214         if (!NT_STATUS_IS_OK(status)) {
215                 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
216                 cli_shutdown(c);
217                 return NULL;
218         }
219
220         if ( show_sessetup ) {
221                 if (*c->server_domain) {
222                         DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
223                                 c->server_domain,c->server_os,c->server_type));
224                 } else if (*c->server_os || *c->server_type) {
225                         DEBUG(0,("OS=[%s] Server=[%s]\n",
226                                  c->server_os,c->server_type));
227                 }
228         }
229         DEBUG(4,(" session setup ok\n"));
230
231         /* here's the fun part....to support 'msdfs proxy' shares
232            (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
233            here before trying to connect to the original share.
234            cli_check_msdfs_proxy() will fail if it is a normal share. */
235
236         if ((c->capabilities & CAP_DFS) &&
237                         cli_check_msdfs_proxy(ctx, c, sharename,
238                                 &newserver, &newshare,
239                                 force_encrypt,
240                                 username,
241                                 password,
242                                 lp_workgroup())) {
243                 cli_shutdown(c);
244                 return do_connect(ctx, newserver,
245                                 newshare, auth_info, false,
246                                 force_encrypt, max_protocol,
247                                 port, name_type);
248         }
249
250         /* must be a normal share */
251
252         status = cli_tcon_andx(c, sharename, "?????",
253                                password, strlen(password)+1);
254         if (!NT_STATUS_IS_OK(status)) {
255                 d_printf("tree connect failed: %s\n", nt_errstr(status));
256                 cli_shutdown(c);
257                 return NULL;
258         }
259
260         if (force_encrypt) {
261                 status = cli_cm_force_encryption(c,
262                                         username,
263                                         password,
264                                         lp_workgroup(),
265                                         sharename);
266                 if (!NT_STATUS_IS_OK(status)) {
267                         cli_shutdown(c);
268                         return NULL;
269                 }
270         }
271
272         DEBUG(4,(" tconx ok\n"));
273         return c;
274 }
275
276 /****************************************************************************
277 ****************************************************************************/
278
279 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
280 {
281         char *name = clean_name(NULL, mnt);
282         if (!name) {
283                 return;
284         }
285         TALLOC_FREE(cli->dfs_mountpoint);
286         cli->dfs_mountpoint = talloc_strdup(cli, name);
287         TALLOC_FREE(name);
288 }
289
290 /********************************************************************
291  Add a new connection to the list.
292  referring_cli == NULL means a new initial connection.
293 ********************************************************************/
294
295 static struct cli_state *cli_cm_connect(TALLOC_CTX *ctx,
296                                         struct cli_state *referring_cli,
297                                         const char *server,
298                                         const char *share,
299                                         const struct user_auth_info *auth_info,
300                                         bool show_hdr,
301                                         bool force_encrypt,
302                                         int max_protocol,
303                                         int port,
304                                         int name_type)
305 {
306         struct cli_state *cli;
307
308         cli = do_connect(ctx, server, share,
309                                 auth_info,
310                                 show_hdr, force_encrypt, max_protocol,
311                                 port, name_type);
312
313         if (!cli ) {
314                 return NULL;
315         }
316
317         /* Enter into the list. */
318         if (referring_cli) {
319                 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
320         }
321
322         if (referring_cli && referring_cli->posix_capabilities) {
323                 uint16 major, minor;
324                 uint32 caplow, caphigh;
325                 if (cli_unix_extensions_version(cli, &major,
326                                         &minor, &caplow, &caphigh)) {
327                         cli_set_unix_extensions_capabilities(cli,
328                                         major, minor,
329                                         caplow, caphigh);
330                 }
331         }
332
333         return cli;
334 }
335
336 /********************************************************************
337  Return a connection to a server on a particular share.
338 ********************************************************************/
339
340 static struct cli_state *cli_cm_find(struct cli_state *cli,
341                                 const char *server,
342                                 const char *share)
343 {
344         struct cli_state *p;
345
346         if (cli == NULL) {
347                 return NULL;
348         }
349
350         /* Search to the start of the list. */
351         for (p = cli; p; p = p->prev) {
352                 if (strequal(server, p->desthost) &&
353                                 strequal(share,p->share)) {
354                         return p;
355                 }
356         }
357
358         /* Search to the end of the list. */
359         for (p = cli->next; p; p = p->next) {
360                 if (strequal(server, p->desthost) &&
361                                 strequal(share,p->share)) {
362                         return p;
363                 }
364         }
365
366         return NULL;
367 }
368
369 /****************************************************************************
370  Open a client connection to a \\server\share.
371 ****************************************************************************/
372
373 struct cli_state *cli_cm_open(TALLOC_CTX *ctx,
374                                 struct cli_state *referring_cli,
375                                 const char *server,
376                                 const char *share,
377                                 const struct user_auth_info *auth_info,
378                                 bool show_hdr,
379                                 bool force_encrypt,
380                                 int max_protocol,
381                                 int port,
382                                 int name_type)
383 {
384         /* Try to reuse an existing connection in this list. */
385         struct cli_state *c = cli_cm_find(referring_cli, server, share);
386
387         if (c) {
388                 return c;
389         }
390
391         if (auth_info == NULL) {
392                 /* Can't do a new connection
393                  * without auth info. */
394                 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
395                         "without auth info\n",
396                         server, share );
397                 return NULL;
398         }
399
400         return cli_cm_connect(ctx,
401                                 referring_cli,
402                                 server,
403                                 share,
404                                 auth_info,
405                                 show_hdr,
406                                 force_encrypt,
407                                 max_protocol,
408                                 port,
409                                 name_type);
410 }
411
412 /****************************************************************************
413 ****************************************************************************/
414
415 void cli_cm_display(const struct cli_state *cli)
416 {
417         int i;
418
419         for (i=0; cli; cli = cli->next,i++ ) {
420                 d_printf("%d:\tserver=%s, share=%s\n",
421                         i, cli->desthost, cli->share );
422         }
423 }
424
425 /****************************************************************************
426 ****************************************************************************/
427
428 /****************************************************************************
429 ****************************************************************************/
430
431 #if 0
432 void cli_cm_set_credentials(struct user_auth_info *auth_info)
433 {
434         SAFE_FREE(cm_creds.username);
435         cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
436                                                auth_info));
437
438         if (get_cmdline_auth_info_got_pass(auth_info)) {
439                 cm_set_password(get_cmdline_auth_info_password(auth_info));
440         }
441
442         cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
443         cm_creds.fallback_after_kerberos = false;
444         cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
445 }
446 #endif
447
448 /**********************************************************************
449  split a dfs path into the server, share name, and extrapath components
450 **********************************************************************/
451
452 static void split_dfs_path(TALLOC_CTX *ctx,
453                                 const char *nodepath,
454                                 char **pp_server,
455                                 char **pp_share,
456                                 char **pp_extrapath)
457 {
458         char *p, *q;
459         char *path;
460
461         *pp_server = NULL;
462         *pp_share = NULL;
463         *pp_extrapath = NULL;
464
465         path = talloc_strdup(ctx, nodepath);
466         if (!path) {
467                 return;
468         }
469
470         if ( path[0] != '\\' ) {
471                 return;
472         }
473
474         p = strchr_m( path + 1, '\\' );
475         if ( !p ) {
476                 return;
477         }
478
479         *p = '\0';
480         p++;
481
482         /* Look for any extra/deep path */
483         q = strchr_m(p, '\\');
484         if (q != NULL) {
485                 *q = '\0';
486                 q++;
487                 *pp_extrapath = talloc_strdup(ctx, q);
488         } else {
489                 *pp_extrapath = talloc_strdup(ctx, "");
490         }
491
492         *pp_share = talloc_strdup(ctx, p);
493         *pp_server = talloc_strdup(ctx, &path[1]);
494 }
495
496 /****************************************************************************
497  Return the original path truncated at the directory component before
498  the first wildcard character. Trust the caller to provide a NULL
499  terminated string
500 ****************************************************************************/
501
502 static char *clean_path(TALLOC_CTX *ctx, const char *path)
503 {
504         size_t len;
505         char *p1, *p2, *p;
506         char *path_out;
507
508         /* No absolute paths. */
509         while (IS_DIRECTORY_SEP(*path)) {
510                 path++;
511         }
512
513         path_out = talloc_strdup(ctx, path);
514         if (!path_out) {
515                 return NULL;
516         }
517
518         p1 = strchr_m(path_out, '*');
519         p2 = strchr_m(path_out, '?');
520
521         if (p1 || p2) {
522                 if (p1 && p2) {
523                         p = MIN(p1,p2);
524                 } else if (!p1) {
525                         p = p2;
526                 } else {
527                         p = p1;
528                 }
529                 *p = '\0';
530
531                 /* Now go back to the start of this component. */
532                 p1 = strrchr_m(path_out, '/');
533                 p2 = strrchr_m(path_out, '\\');
534                 p = MAX(p1,p2);
535                 if (p) {
536                         *p = '\0';
537                 }
538         }
539
540         /* Strip any trailing separator */
541
542         len = strlen(path_out);
543         if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
544                 path_out[len-1] = '\0';
545         }
546
547         return path_out;
548 }
549
550 /****************************************************************************
551 ****************************************************************************/
552
553 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
554                                         struct cli_state *cli,
555                                         const char *dir)
556 {
557         char path_sep = '\\';
558
559         /* Ensure the extrapath doesn't start with a separator. */
560         while (IS_DIRECTORY_SEP(*dir)) {
561                 dir++;
562         }
563
564         if (cli->posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
565                 path_sep = '/';
566         }
567         return talloc_asprintf(ctx, "%c%s%c%s%c%s",
568                         path_sep,
569                         cli->desthost,
570                         path_sep,
571                         cli->share,
572                         path_sep,
573                         dir);
574 }
575
576 /********************************************************************
577  check for dfs referral
578 ********************************************************************/
579
580 static bool cli_dfs_check_error( struct cli_state *cli, NTSTATUS status )
581 {
582         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
583
584         /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
585
586         if (!((flgs2&FLAGS2_32_BIT_ERROR_CODES) &&
587                                 (flgs2&FLAGS2_UNICODE_STRINGS)))
588                 return false;
589
590         if (NT_STATUS_EQUAL(status, NT_STATUS(IVAL(cli->inbuf,smb_rcls))))
591                 return true;
592
593         return false;
594 }
595
596 /********************************************************************
597  Get the dfs referral link.
598 ********************************************************************/
599
600 bool cli_dfs_get_referral(TALLOC_CTX *ctx,
601                         struct cli_state *cli,
602                         const char *path,
603                         CLIENT_DFS_REFERRAL**refs,
604                         size_t *num_refs,
605                         size_t *consumed)
606 {
607         unsigned int data_len = 0;
608         unsigned int param_len = 0;
609         uint16 setup = TRANSACT2_GET_DFS_REFERRAL;
610         char *param = NULL;
611         char *rparam=NULL, *rdata=NULL;
612         char *p;
613         char *endp;
614         size_t pathlen = 2*(strlen(path)+1);
615         smb_ucs2_t *path_ucs;
616         char *consumed_path = NULL;
617         uint16_t consumed_ucs;
618         uint16 num_referrals;
619         CLIENT_DFS_REFERRAL *referrals = NULL;
620         bool ret = false;
621
622         *num_refs = 0;
623         *refs = NULL;
624
625         param = SMB_MALLOC_ARRAY(char, 2+pathlen+2);
626         if (!param) {
627                 goto out;
628         }
629         SSVAL(param, 0, 0x03);  /* max referral level */
630         p = &param[2];
631
632         path_ucs = (smb_ucs2_t *)p;
633         p += clistr_push(cli, p, path, pathlen, STR_TERMINATE);
634         param_len = PTR_DIFF(p, param);
635
636         if (!cli_send_trans(cli, SMBtrans2,
637                         NULL,                        /* name */
638                         -1, 0,                          /* fid, flags */
639                         &setup, 1, 0,                   /* setup, length, max */
640                         param, param_len, 2,            /* param, length, max */
641                         NULL, 0, cli->max_xmit /* data, length, max */
642                         )) {
643                 goto out;
644         }
645
646         if (!cli_receive_trans(cli, SMBtrans2,
647                 &rparam, &param_len,
648                 &rdata, &data_len)) {
649                 goto out;
650         }
651
652         if (data_len < 4) {
653                 goto out;
654         }
655
656         endp = rdata + data_len;
657
658         consumed_ucs  = SVAL(rdata, 0);
659         num_referrals = SVAL(rdata, 2);
660
661         /* consumed_ucs is the number of bytes
662          * of the UCS2 path consumed not counting any
663          * terminating null. We need to convert
664          * back to unix charset and count again
665          * to get the number of bytes consumed from
666          * the incoming path. */
667
668         if (pull_string_talloc(talloc_tos(),
669                         NULL,
670                         0,
671                         &consumed_path,
672                         path_ucs,
673                         consumed_ucs,
674                         STR_UNICODE) == 0) {
675                 goto out;
676         }
677         if (consumed_path == NULL) {
678                 goto out;
679         }
680         *consumed = strlen(consumed_path);
681
682         if (num_referrals != 0) {
683                 uint16 ref_version;
684                 uint16 ref_size;
685                 int i;
686                 uint16 node_offset;
687
688                 referrals = TALLOC_ARRAY(ctx, CLIENT_DFS_REFERRAL,
689                                 num_referrals);
690
691                 if (!referrals) {
692                         goto out;
693                 }
694                 /* start at the referrals array */
695
696                 p = rdata+8;
697                 for (i=0; i<num_referrals && p < endp; i++) {
698                         if (p + 18 > endp) {
699                                 goto out;
700                         }
701                         ref_version = SVAL(p, 0);
702                         ref_size    = SVAL(p, 2);
703                         node_offset = SVAL(p, 16);
704
705                         if (ref_version != 3) {
706                                 p += ref_size;
707                                 continue;
708                         }
709
710                         referrals[i].proximity = SVAL(p, 8);
711                         referrals[i].ttl       = SVAL(p, 10);
712
713                         if (p + node_offset > endp) {
714                                 goto out;
715                         }
716                         clistr_pull_talloc(ctx, cli->inbuf,
717                                            &referrals[i].dfspath,
718                                            p+node_offset, -1,
719                                            STR_TERMINATE|STR_UNICODE);
720
721                         if (!referrals[i].dfspath) {
722                                 goto out;
723                         }
724                         p += ref_size;
725                 }
726                 if (i < num_referrals) {
727                         goto out;
728                 }
729         }
730
731         ret = true;
732
733         *num_refs = num_referrals;
734         *refs = referrals;
735
736   out:
737
738         TALLOC_FREE(consumed_path);
739         SAFE_FREE(param);
740         SAFE_FREE(rdata);
741         SAFE_FREE(rparam);
742         return ret;
743 }
744
745 /********************************************************************
746 ********************************************************************/
747
748 bool cli_resolve_path(TALLOC_CTX *ctx,
749                         const char *mountpt,
750                         const struct user_auth_info *dfs_auth_info,
751                         struct cli_state *rootcli,
752                         const char *path,
753                         struct cli_state **targetcli,
754                         char **pp_targetpath)
755 {
756         CLIENT_DFS_REFERRAL *refs = NULL;
757         size_t num_refs = 0;
758         size_t consumed = 0;
759         struct cli_state *cli_ipc = NULL;
760         char *dfs_path = NULL;
761         char *cleanpath = NULL;
762         char *extrapath = NULL;
763         int pathlen;
764         char *server = NULL;
765         char *share = NULL;
766         struct cli_state *newcli = NULL;
767         char *newpath = NULL;
768         char *newmount = NULL;
769         char *ppath = NULL;
770         SMB_STRUCT_STAT sbuf;
771         uint32 attributes;
772
773         if ( !rootcli || !path || !targetcli ) {
774                 return false;
775         }
776
777         /* Don't do anything if this is not a DFS root. */
778
779         if ( !rootcli->dfsroot) {
780                 *targetcli = rootcli;
781                 *pp_targetpath = talloc_strdup(ctx, path);
782                 if (!*pp_targetpath) {
783                         return false;
784                 }
785                 return true;
786         }
787
788         *targetcli = NULL;
789
790         /* Send a trans2_query_path_info to check for a referral. */
791
792         cleanpath = clean_path(ctx, path);
793         if (!cleanpath) {
794                 return false;
795         }
796
797         dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
798         if (!dfs_path) {
799                 return false;
800         }
801
802         if (cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes)) {
803                 /* This is an ordinary path, just return it. */
804                 *targetcli = rootcli;
805                 *pp_targetpath = talloc_strdup(ctx, path);
806                 if (!*pp_targetpath) {
807                         return false;
808                 }
809                 goto done;
810         }
811
812         /* Special case where client asked for a path that does not exist */
813
814         if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
815                 *targetcli = rootcli;
816                 *pp_targetpath = talloc_strdup(ctx, path);
817                 if (!*pp_targetpath) {
818                         return false;
819                 }
820                 goto done;
821         }
822
823         /* We got an error, check for DFS referral. */
824
825         if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED)) {
826                 return false;
827         }
828
829         /* Check for the referral. */
830
831         if (!(cli_ipc = cli_cm_open(ctx,
832                                 rootcli,
833                                 rootcli->desthost,
834                                 "IPC$",
835                                 dfs_auth_info,
836                                 false,
837                                 (rootcli->trans_enc_state != NULL),
838                                 rootcli->protocol,
839                                 0,
840                                 0x20))) {
841                 return false;
842         }
843
844         if (!cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
845                         &num_refs, &consumed) || !num_refs) {
846                 return false;
847         }
848
849         /* Just store the first referral for now. */
850
851         if (!refs[0].dfspath) {
852                 return false;
853         }
854         split_dfs_path(ctx, refs[0].dfspath, &server, &share, &extrapath );
855
856         if (!server || !share) {
857                 return false;
858         }
859
860         /* Make sure to recreate the original string including any wildcards. */
861
862         dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
863         if (!dfs_path) {
864                 return false;
865         }
866         pathlen = strlen(dfs_path);
867         consumed = MIN(pathlen, consumed);
868         *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
869         if (!*pp_targetpath) {
870                 return false;
871         }
872         dfs_path[consumed] = '\0';
873
874         /*
875          * *pp_targetpath is now the unconsumed part of the path.
876          * dfs_path is now the consumed part of the path
877          * (in \server\share\path format).
878          */
879
880         /* Open the connection to the target server & share */
881         if ((*targetcli = cli_cm_open(ctx, rootcli,
882                                         server,
883                                         share,
884                                         dfs_auth_info,
885                                         false,
886                                         (rootcli->trans_enc_state != NULL),
887                                         rootcli->protocol,
888                                         0,
889                                         0x20)) == NULL) {
890                 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
891                         server, share );
892                 return false;
893         }
894
895         if (extrapath && strlen(extrapath) > 0) {
896                 *pp_targetpath = talloc_asprintf(ctx,
897                                                 "%s%s",
898                                                 extrapath,
899                                                 *pp_targetpath);
900                 if (!*pp_targetpath) {
901                         return false;
902                 }
903         }
904
905         /* parse out the consumed mount path */
906         /* trim off the \server\share\ */
907
908         ppath = dfs_path;
909
910         if (*ppath != '\\') {
911                 d_printf("cli_resolve_path: "
912                         "dfs_path (%s) not in correct format.\n",
913                         dfs_path );
914                 return false;
915         }
916
917         ppath++; /* Now pointing at start of server name. */
918
919         if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
920                 return false;
921         }
922
923         ppath++; /* Now pointing at start of share name. */
924
925         if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
926                 return false;
927         }
928
929         ppath++; /* Now pointing at path component. */
930
931         newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
932         if (!newmount) {
933                 return false;
934         }
935
936         cli_set_mntpoint(*targetcli, newmount);
937
938         /* Check for another dfs referral, note that we are not
939            checking for loops here. */
940
941         if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
942                 if (cli_resolve_path(ctx,
943                                         newmount,
944                                         dfs_auth_info,
945                                         *targetcli,
946                                         *pp_targetpath,
947                                         &newcli,
948                                         &newpath)) {
949                         /*
950                          * When cli_resolve_path returns true here it's always
951                          * returning the complete path in newpath, so we're done
952                          * here.
953                          */
954                         *targetcli = newcli;
955                         *pp_targetpath = newpath;
956                         return true;
957                 }
958         }
959
960   done:
961
962         /* If returning true ensure we return a dfs root full path. */
963         if ((*targetcli)->dfsroot) {
964                 dfs_path = talloc_strdup(ctx, *pp_targetpath);
965                 if (!dfs_path) {
966                         return false;
967                 }
968                 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
969         }
970
971         return true;
972 }
973
974 /********************************************************************
975 ********************************************************************/
976
977 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
978                                 struct cli_state *cli,
979                                 const char *sharename,
980                                 char **pp_newserver,
981                                 char **pp_newshare,
982                                 bool force_encrypt,
983                                 const char *username,
984                                 const char *password,
985                                 const char *domain)
986 {
987         CLIENT_DFS_REFERRAL *refs = NULL;
988         size_t num_refs = 0;
989         size_t consumed = 0;
990         char *fullpath = NULL;
991         bool res;
992         uint16 cnum;
993         char *newextrapath = NULL;
994
995         if (!cli || !sharename) {
996                 return false;
997         }
998
999         cnum = cli->cnum;
1000
1001         /* special case.  never check for a referral on the IPC$ share */
1002
1003         if (strequal(sharename, "IPC$")) {
1004                 return false;
1005         }
1006
1007         /* send a trans2_query_path_info to check for a referral */
1008
1009         fullpath = talloc_asprintf(ctx, "\\%s\\%s", cli->desthost, sharename );
1010         if (!fullpath) {
1011                 return false;
1012         }
1013
1014         /* check for the referral */
1015
1016         if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, "IPC$", "IPC", NULL, 0))) {
1017                 return false;
1018         }
1019
1020         if (force_encrypt) {
1021                 NTSTATUS status = cli_cm_force_encryption(cli,
1022                                         username,
1023                                         password,
1024                                         lp_workgroup(),
1025                                         "IPC$");
1026                 if (!NT_STATUS_IS_OK(status)) {
1027                         return false;
1028                 }
1029         }
1030
1031         res = cli_dfs_get_referral(ctx, cli, fullpath, &refs, &num_refs, &consumed);
1032
1033         if (!cli_tdis(cli)) {
1034                 return false;
1035         }
1036
1037         cli->cnum = cnum;
1038
1039         if (!res || !num_refs) {
1040                 return false;
1041         }
1042
1043         if (!refs[0].dfspath) {
1044                 return false;
1045         }
1046
1047         split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1048                         pp_newshare, &newextrapath );
1049
1050         if ((*pp_newserver == NULL) || (*pp_newshare == NULL)) {
1051                 return false;
1052         }
1053
1054         /* check that this is not a self-referral */
1055
1056         if (strequal(cli->desthost, *pp_newserver) &&
1057                         strequal(sharename, *pp_newshare)) {
1058                 return false;
1059         }
1060
1061         return true;
1062 }