Remove cli_cm_set_dest_ss() - removes the global dest_ss
[mat/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
58 static struct client_connection *connections;
59
60 static bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
61                                 struct cli_state *cli,
62                                 const char *sharename,
63                                 char **pp_newserver,
64                                 char **pp_newshare,
65                                 bool force_encrypt,
66                                 const char *username,
67                                 const char *password,
68                                 const char *domain);
69
70 /********************************************************************
71  Ensure a connection is encrypted.
72 ********************************************************************/
73
74 NTSTATUS cli_cm_force_encryption(struct cli_state *c,
75                         const char *username,
76                         const char *password,
77                         const char *domain,
78                         const char *sharename)
79 {
80         NTSTATUS status = cli_force_encryption(c,
81                                         username,
82                                         password,
83                                         domain);
84
85         if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
86                 d_printf("Encryption required and "
87                         "server that doesn't support "
88                         "UNIX extensions - failing connect\n");
89         } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNKNOWN_REVISION)) {
90                 d_printf("Encryption required and "
91                         "can't get UNIX CIFS extensions "
92                         "version from server.\n");
93         } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
94                 d_printf("Encryption required and "
95                         "share %s doesn't support "
96                         "encryption.\n", sharename);
97         } else if (!NT_STATUS_IS_OK(status)) {
98                 d_printf("Encryption required and "
99                         "setup failed with error %s.\n",
100                         nt_errstr(status));
101         }
102
103         return status;
104 }
105         
106 /********************************************************************
107  Return a connection to a server.
108 ********************************************************************/
109
110 static struct cli_state *do_connect(TALLOC_CTX *ctx,
111                                         const char *server,
112                                         const char *share,
113                                         bool show_sessetup,
114                                         bool force_encrypt)
115 {
116         struct cli_state *c = NULL;
117         struct nmb_name called, calling;
118         const char *server_n;
119         struct sockaddr_storage ss;
120         char *servicename;
121         char *sharename;
122         char *newserver, *newshare;
123         const char *username;
124         const char *password;
125         NTSTATUS status;
126
127         /* make a copy so we don't modify the global string 'service' */
128         servicename = talloc_strdup(ctx,share);
129         if (!servicename) {
130                 return NULL;
131         }
132         sharename = servicename;
133         if (*sharename == '\\') {
134                 sharename += 2;
135                 if (server == NULL) {
136                         server = sharename;
137                 }
138                 sharename = strchr_m(sharename,'\\');
139                 if (!sharename) {
140                         return NULL;
141                 }
142                 *sharename = 0;
143                 sharename++;
144         }
145
146         server_n = server;
147
148         zero_sockaddr(&ss);
149
150         make_nmb_name(&calling, global_myname(), 0x0);
151         make_nmb_name(&called , server, name_type);
152
153  again:
154         zero_sockaddr(&ss);
155
156         /* have to open a new connection */
157         if (!(c=cli_initialise()) || (cli_set_port(c, port) != port)) {
158                 d_printf("Connection to %s failed\n", server_n);
159                 if (c) {
160                         cli_shutdown(c);
161                 }
162                 return NULL;
163         }
164         status = cli_connect(c, server_n, &ss);
165         if (!NT_STATUS_IS_OK(status)) {
166                 d_printf("Connection to %s failed (Error %s)\n",
167                                 server_n,
168                                 nt_errstr(status));
169                 cli_shutdown(c);
170                 return NULL;
171         }
172
173         c->protocol = max_protocol;
174         c->use_kerberos = cm_creds.use_kerberos;
175         c->fallback_after_kerberos = cm_creds.fallback_after_kerberos;
176         cli_setup_signing_state(c, cm_creds.signing_state);
177
178         if (!cli_session_request(c, &calling, &called)) {
179                 char *p;
180                 d_printf("session request to %s failed (%s)\n",
181                          called.name, cli_errstr(c));
182                 cli_shutdown(c);
183                 c = NULL;
184                 if ((p=strchr_m(called.name, '.'))) {
185                         *p = 0;
186                         goto again;
187                 }
188                 if (strcmp(called.name, "*SMBSERVER")) {
189                         make_nmb_name(&called , "*SMBSERVER", 0x20);
190                         goto again;
191                 }
192                 return NULL;
193         }
194
195         DEBUG(4,(" session request ok\n"));
196
197         status = cli_negprot(c);
198
199         if (!NT_STATUS_IS_OK(status)) {
200                 d_printf("protocol negotiation failed: %s\n",
201                          nt_errstr(status));
202                 cli_shutdown(c);
203                 return NULL;
204         }
205
206         if (!cm_creds.got_pass && !cm_creds.use_kerberos) {
207                 char *label = NULL;
208                 char *pass;
209                 label = talloc_asprintf(ctx, "Enter %s's password: ",
210                         cm_creds.username);
211                 pass = getpass(label);
212                 if (pass) {
213                         cm_set_password(pass);
214                 }
215                 TALLOC_FREE(label);
216         }
217
218         username = cm_creds.username ? cm_creds.username : "";
219         password = cm_creds.password ? cm_creds.password : "";
220
221         if (!NT_STATUS_IS_OK(cli_session_setup(c, username,
222                                                password, strlen(password),
223                                                password, strlen(password),
224                                                lp_workgroup()))) {
225                 /* If a password was not supplied then
226                  * try again with a null username. */
227                 if (password[0] || !username[0] || cm_creds.use_kerberos ||
228                     !NT_STATUS_IS_OK(cli_session_setup(c, "",
229                                                 "", 0,
230                                                 "", 0,
231                                                lp_workgroup()))) {
232                         d_printf("session setup failed: %s\n", cli_errstr(c));
233                         if (NT_STATUS_V(cli_nt_error(c)) ==
234                             NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED))
235                                 d_printf("did you forget to run kinit?\n");
236                         cli_shutdown(c);
237                         return NULL;
238                 }
239                 d_printf("Anonymous login successful\n");
240         }
241
242         if ( show_sessetup ) {
243                 if (*c->server_domain) {
244                         DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
245                                 c->server_domain,c->server_os,c->server_type));
246                 } else if (*c->server_os || *c->server_type) {
247                         DEBUG(0,("OS=[%s] Server=[%s]\n",
248                                  c->server_os,c->server_type));
249                 }
250         }
251         DEBUG(4,(" session setup ok\n"));
252
253         /* here's the fun part....to support 'msdfs proxy' shares
254            (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
255            here before trying to connect to the original share.
256            check_dfs_proxy() will fail if it is a normal share. */
257
258         if ((c->capabilities & CAP_DFS) &&
259                         cli_check_msdfs_proxy(ctx, c, sharename,
260                                 &newserver, &newshare,
261                                 force_encrypt,
262                                 username,
263                                 password,
264                                 lp_workgroup())) {
265                 cli_shutdown(c);
266                 return do_connect(ctx, newserver,
267                                 newshare, false, force_encrypt);
268         }
269
270         /* must be a normal share */
271
272         if (!cli_send_tconX(c, sharename, "?????",
273                                 password, strlen(password)+1)) {
274                 d_printf("tree connect failed: %s\n", cli_errstr(c));
275                 cli_shutdown(c);
276                 return NULL;
277         }
278
279         if (force_encrypt) {
280                 status = cli_cm_force_encryption(c,
281                                         username,
282                                         password,
283                                         lp_workgroup(),
284                                         sharename);
285                 if (!NT_STATUS_IS_OK(status)) {
286                         cli_shutdown(c);
287                         return NULL;
288                 }
289         }
290
291         DEBUG(4,(" tconx ok\n"));
292         return c;
293 }
294
295 /****************************************************************************
296 ****************************************************************************/
297
298 static void cli_cm_set_mntpoint(struct cli_state *c, const char *mnt)
299 {
300         struct client_connection *p;
301         int i;
302
303         for (p=connections,i=0; p; p=p->next,i++) {
304                 if (strequal(p->cli->desthost, c->desthost) &&
305                                 strequal(p->cli->share, c->share)) {
306                         break;
307                 }
308         }
309
310         if (p) {
311                 char *name = clean_name(NULL, p->mount);
312                 if (!name) {
313                         return;
314                 }
315                 p->mount = talloc_strdup(p, name);
316                 TALLOC_FREE(name);
317         }
318 }
319
320 /****************************************************************************
321 ****************************************************************************/
322
323 const char *cli_cm_get_mntpoint(struct cli_state *c)
324 {
325         struct client_connection *p;
326         int i;
327
328         for (p=connections,i=0; p; p=p->next,i++) {
329                 if (strequal(p->cli->desthost, c->desthost) &&
330                                 strequal(p->cli->share, c->share)) {
331                         break;
332                 }
333         }
334
335         if (p) {
336                 return p->mount;
337         }
338         return NULL;
339 }
340
341 /********************************************************************
342  Add a new connection to the list
343 ********************************************************************/
344
345 static struct cli_state *cli_cm_connect(TALLOC_CTX *ctx,
346                                         struct cli_state *referring_cli,
347                                         const char *server,
348                                         const char *share,
349                                         bool show_hdr,
350                                         bool force_encrypt)
351 {
352         struct client_connection *node;
353
354         /* NB This must be the null context here... JRA. */
355         node = TALLOC_ZERO_ARRAY(NULL, struct client_connection, 1);
356         if (!node) {
357                 return NULL;
358         }
359
360         node->cli = do_connect(ctx, server, share, show_hdr, force_encrypt);
361
362         if ( !node->cli ) {
363                 TALLOC_FREE( node );
364                 return NULL;
365         }
366
367         DLIST_ADD( connections, node );
368
369         cli_cm_set_mntpoint(node->cli, "");
370
371         if (referring_cli && referring_cli->posix_capabilities) {
372                 uint16 major, minor;
373                 uint32 caplow, caphigh;
374                 if (cli_unix_extensions_version(node->cli, &major,
375                                         &minor, &caplow, &caphigh)) {
376                         cli_set_unix_extensions_capabilities(node->cli,
377                                         major, minor,
378                                         caplow, caphigh);
379                 }
380         }
381
382         return node->cli;
383 }
384
385 /********************************************************************
386  Return a connection to a server.
387 ********************************************************************/
388
389 static struct cli_state *cli_cm_find(const char *server, const char *share)
390 {
391         struct client_connection *p;
392
393         for (p=connections; p; p=p->next) {
394                 if ( strequal(server, p->cli->desthost) &&
395                                 strequal(share,p->cli->share)) {
396                         return p->cli;
397                 }
398         }
399
400         return NULL;
401 }
402
403 /****************************************************************************
404  Open a client connection to a \\server\share.  Set's the current *cli
405  global variable as a side-effect (but only if the connection is successful).
406 ****************************************************************************/
407
408 struct cli_state *cli_cm_open(TALLOC_CTX *ctx,
409                                 struct cli_state *referring_cli,
410                                 const char *server,
411                                 const char *share,
412                                 bool show_hdr,
413                                 bool force_encrypt)
414 {
415         struct cli_state *c;
416
417         /* try to reuse an existing connection */
418
419         c = cli_cm_find(server, share);
420         if (!c) {
421                 c = cli_cm_connect(ctx, referring_cli,
422                                 server, share, show_hdr, force_encrypt);
423         }
424
425         return c;
426 }
427
428 /****************************************************************************
429 ****************************************************************************/
430
431 void cli_cm_shutdown(void)
432 {
433         struct client_connection *p, *x;
434
435         for (p=connections; p;) {
436                 cli_shutdown(p->cli);
437                 x = p;
438                 p = p->next;
439
440                 TALLOC_FREE(x);
441         }
442
443         connections = NULL;
444         return;
445 }
446
447 /****************************************************************************
448 ****************************************************************************/
449
450 void cli_cm_display(void)
451 {
452         struct client_connection *p;
453         int i;
454
455         for ( p=connections,i=0; p; p=p->next,i++ ) {
456                 d_printf("%d:\tserver=%s, share=%s\n",
457                         i, p->cli->desthost, p->cli->share );
458         }
459 }
460
461 /****************************************************************************
462 ****************************************************************************/
463
464 static void cm_set_password(const char *newpass)
465 {
466         SAFE_FREE(cm_creds.password);
467         cm_creds.password = SMB_STRDUP(newpass);
468         if (cm_creds.password) {
469                 cm_creds.got_pass = true;
470         }
471 }
472
473 /****************************************************************************
474 ****************************************************************************/
475
476 void cli_cm_set_credentials(struct user_auth_info *auth_info)
477 {
478         SAFE_FREE(cm_creds.username);
479         cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
480                                                auth_info));
481
482         if (get_cmdline_auth_info_got_pass(auth_info)) {
483                 cm_set_password(get_cmdline_auth_info_password(auth_info));
484         }
485
486         cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
487         cm_creds.fallback_after_kerberos = false;
488         cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
489 }
490
491 /****************************************************************************
492 ****************************************************************************/
493
494 void cli_cm_set_port(int port_number)
495 {
496         port = port_number;
497 }
498
499 /****************************************************************************
500 ****************************************************************************/
501
502 void cli_cm_set_dest_name_type(int type)
503 {
504         name_type = type;
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, &referrals[i].dfspath,
789                                 p+node_offset, -1,
790                                 STR_TERMINATE|STR_UNICODE );
791
792                         if (!referrals[i].dfspath) {
793                                 goto out;
794                         }
795                         p += ref_size;
796                 }
797                 if (i < num_referrals) {
798                         goto out;
799                 }
800         }
801
802         ret = true;
803
804         *num_refs = num_referrals;
805         *refs = referrals;
806
807   out:
808
809         SAFE_FREE(rdata);
810         SAFE_FREE(rparam);
811         return ret;
812 }
813
814 /********************************************************************
815 ********************************************************************/
816
817 bool cli_resolve_path(TALLOC_CTX *ctx,
818                         const char *mountpt,
819                         struct cli_state *rootcli,
820                         const char *path,
821                         struct cli_state **targetcli,
822                         char **pp_targetpath)
823 {
824         CLIENT_DFS_REFERRAL *refs = NULL;
825         size_t num_refs = 0;
826         uint16 consumed;
827         struct cli_state *cli_ipc = NULL;
828         char *dfs_path = NULL;
829         char *cleanpath = NULL;
830         char *extrapath = NULL;
831         int pathlen;
832         char *server = NULL;
833         char *share = NULL;
834         struct cli_state *newcli = NULL;
835         char *newpath = NULL;
836         char *newmount = NULL;
837         char *ppath = NULL;
838         SMB_STRUCT_STAT sbuf;
839         uint32 attributes;
840
841         if ( !rootcli || !path || !targetcli ) {
842                 return false;
843         }
844
845         /* Don't do anything if this is not a DFS root. */
846
847         if ( !rootcli->dfsroot) {
848                 *targetcli = rootcli;
849                 *pp_targetpath = talloc_strdup(ctx, path);
850                 if (!*pp_targetpath) {
851                         return false;
852                 }
853                 return true;
854         }
855
856         *targetcli = NULL;
857
858         /* Send a trans2_query_path_info to check for a referral. */
859
860         cleanpath = clean_path(ctx, path);
861         if (!cleanpath) {
862                 return false;
863         }
864
865         dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
866         if (!dfs_path) {
867                 return false;
868         }
869
870         if (cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes)) {
871                 /* This is an ordinary path, just return it. */
872                 *targetcli = rootcli;
873                 *pp_targetpath = talloc_strdup(ctx, path);
874                 if (!*pp_targetpath) {
875                         return false;
876                 }
877                 goto done;
878         }
879
880         /* Special case where client asked for a path that does not exist */
881
882         if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
883                 *targetcli = rootcli;
884                 *pp_targetpath = talloc_strdup(ctx, path);
885                 if (!*pp_targetpath) {
886                         return false;
887                 }
888                 goto done;
889         }
890
891         /* We got an error, check for DFS referral. */
892
893         if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED)) {
894                 return false;
895         }
896
897         /* Check for the referral. */
898
899         if (!(cli_ipc = cli_cm_open(ctx, rootcli,
900                                         rootcli->desthost,
901                                         "IPC$", false,
902                                         (rootcli->trans_enc_state != NULL)))) {
903                 return false;
904         }
905
906         if (!cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
907                         &num_refs, &consumed) || !num_refs) {
908                 return false;
909         }
910
911         /* Just store the first referral for now. */
912
913         if (!refs[0].dfspath) {
914                 return false;
915         }
916         split_dfs_path(ctx, refs[0].dfspath, &server, &share, &extrapath );
917
918         if (!server || !share) {
919                 return false;
920         }
921
922         /* Make sure to recreate the original string including any wildcards. */
923
924         dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
925         if (!dfs_path) {
926                 return false;
927         }
928         pathlen = strlen(dfs_path)*2;
929         consumed = MIN(pathlen, consumed);
930         *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed/2]);
931         if (!*pp_targetpath) {
932                 return false;
933         }
934         dfs_path[consumed/2] = '\0';
935
936         /*
937          * *pp_targetpath is now the unconsumed part of the path.
938          * dfs_path is now the consumed part of the path
939          * (in \server\share\path format).
940          */
941
942         /* Open the connection to the target server & share */
943         if ((*targetcli = cli_cm_open(ctx, rootcli,
944                                         server,
945                                         share,
946                                         false,
947                                         (rootcli->trans_enc_state != NULL))) == NULL) {
948                 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
949                         server, share );
950                 return false;
951         }
952
953         if (extrapath && strlen(extrapath) > 0) {
954                 *pp_targetpath = talloc_asprintf(ctx,
955                                                 "%s%s",
956                                                 extrapath,
957                                                 *pp_targetpath);
958                 if (!*pp_targetpath) {
959                         return false;
960                 }
961         }
962
963         /* parse out the consumed mount path */
964         /* trim off the \server\share\ */
965
966         ppath = dfs_path;
967
968         if (*ppath != '\\') {
969                 d_printf("cli_resolve_path: "
970                         "dfs_path (%s) not in correct format.\n",
971                         dfs_path );
972                 return false;
973         }
974
975         ppath++; /* Now pointing at start of server name. */
976
977         if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
978                 return false;
979         }
980
981         ppath++; /* Now pointing at start of share name. */
982
983         if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
984                 return false;
985         }
986
987         ppath++; /* Now pointing at path component. */
988
989         newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
990         if (!newmount) {
991                 return false;
992         }
993
994         cli_cm_set_mntpoint(*targetcli, newmount);
995
996         /* Check for another dfs referral, note that we are not
997            checking for loops here. */
998
999         if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
1000                 if (cli_resolve_path(ctx,
1001                                         newmount,
1002                                         *targetcli,
1003                                         *pp_targetpath,
1004                                         &newcli,
1005                                         &newpath)) {
1006                         /*
1007                          * When cli_resolve_path returns true here it's always
1008                          * returning the complete path in newpath, so we're done
1009                          * here.
1010                          */
1011                         *targetcli = newcli;
1012                         *pp_targetpath = newpath;
1013                         return true;
1014                 }
1015         }
1016
1017   done:
1018
1019         /* If returning true ensure we return a dfs root full path. */
1020         if ((*targetcli)->dfsroot) {
1021                 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1022                 if (!dfs_path) {
1023                         return false;
1024                 }
1025                 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1026         }
1027
1028         return true;
1029 }
1030
1031 /********************************************************************
1032 ********************************************************************/
1033
1034 static bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1035                                 struct cli_state *cli,
1036                                 const char *sharename,
1037                                 char **pp_newserver,
1038                                 char **pp_newshare,
1039                                 bool force_encrypt,
1040                                 const char *username,
1041                                 const char *password,
1042                                 const char *domain)
1043 {
1044         CLIENT_DFS_REFERRAL *refs = NULL;
1045         size_t num_refs = 0;
1046         uint16 consumed;
1047         char *fullpath = NULL;
1048         bool res;
1049         uint16 cnum;
1050         char *newextrapath = NULL;
1051
1052         if (!cli || !sharename) {
1053                 return false;
1054         }
1055
1056         cnum = cli->cnum;
1057
1058         /* special case.  never check for a referral on the IPC$ share */
1059
1060         if (strequal(sharename, "IPC$")) {
1061                 return false;
1062         }
1063
1064         /* send a trans2_query_path_info to check for a referral */
1065
1066         fullpath = talloc_asprintf(ctx, "\\%s\\%s", cli->desthost, sharename );
1067         if (!fullpath) {
1068                 return false;
1069         }
1070
1071         /* check for the referral */
1072
1073         if (!cli_send_tconX(cli, "IPC$", "IPC", NULL, 0)) {
1074                 return false;
1075         }
1076
1077         if (force_encrypt) {
1078                 NTSTATUS status = cli_cm_force_encryption(cli,
1079                                         username,
1080                                         password,
1081                                         lp_workgroup(),
1082                                         "IPC$");
1083                 if (!NT_STATUS_IS_OK(status)) {
1084                         return false;
1085                 }
1086         }
1087
1088         res = cli_dfs_get_referral(ctx, cli, fullpath, &refs, &num_refs, &consumed);
1089
1090         if (!cli_tdis(cli)) {
1091                 return false;
1092         }
1093
1094         cli->cnum = cnum;
1095
1096         if (!res || !num_refs) {
1097                 return false;
1098         }
1099
1100         if (!refs[0].dfspath) {
1101                 return false;
1102         }
1103
1104         split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1105                         pp_newshare, &newextrapath );
1106
1107         if ((*pp_newserver == NULL) || (*pp_newshare == NULL)) {
1108                 return false;
1109         }
1110
1111         /* check that this is not a self-referral */
1112
1113         if (strequal(cli->desthost, *pp_newserver) &&
1114                         strequal(sharename, *pp_newshare)) {
1115                 return false;
1116         }
1117
1118         return true;
1119 }