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