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