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