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