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