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