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